-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathlex.c
More file actions
2571 lines (2536 loc) · 56.9 KB
/
Copy pathlex.c
File metadata and controls
2571 lines (2536 loc) · 56.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2012 AT&T Intellectual Property *
* Copyright (c) 2020-2025 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
* A copy of the License is available at *
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html *
* (with md5 checksum 84283fa8859daf213bdda5a9f8d1be1d) *
* *
* David Korn <dgk@research.att.com> *
* Martijn Dekker <martijn@inlv.org> *
* atheik <14833674+atheik@users.noreply.github.com> *
* Johnothan King <johnothanking@protonmail.com> *
* hyenias <58673227+hyenias@users.noreply.github.com> *
* *
***********************************************************************/
/*
* KornShell lexical analyzer
*
* Written by David Korn
* AT&T Labs
*
*/
#include "shopt.h"
#include <ast.h>
#include <fcin.h>
#include <nval.h>
#include "defs.h"
#include "argnod.h"
#include "test.h"
#include "lexstates.h"
#include "io.h"
#include "shlex.h"
#include <ast_release.h>
#define TEST_RE 3
#define SYNBAD 3 /* exit value for syntax errors */
#define STACK_ARRAY 3 /* size of depth match stack growth */
#if _lib_iswblank < 0 /* set in lexstates.h to enable this code */
int
local_iswblank(wchar_t wc)
{
static int initialized;
static wctype_t wt;
if (!initialized)
{
initialized = 1;
wt = wctype("blank");
}
return iswctype(wc, wt);
}
#endif
#define pushlevel(lp,c,s) ((lp->lexd.level>=lex_max?stack_grow():1) &&\
((lex_match[lp->lexd.level++]=lp->lexd.lastc),\
lp->lexd.lastc=(((s)<<CHAR_BIT)|(c))))
#define oldmode(lp) (lp->lexd.lastc>>CHAR_BIT)
#define endchar(lp) (lp->lexd.lastc&0xff)
#define setchar(lp,c) (lp->lexd.lastc = ((lp->lexd.lastc&~0xff)|(c)))
#define poplevel(lp) (lp->lexd.lastc=lex_match[--lp->lexd.level])
static char *fmttoken(Lex_t*, int);
static struct argnod *endword(int);
static int alias_exceptf(Sfio_t*, int, void*, Sfdisc_t*);
static void setupalias(Lex_t*,const char*, Namval_t*);
static int comsub(Lex_t*,int);
static void nested_here(Lex_t*);
static int here_copy(Lex_t*, struct ionod*);
static int stack_grow(void);
static const Sfdisc_t alias_disc = { NULL, NULL, NULL, alias_exceptf, NULL };
/* these were taken out of the Lex_t struct because they should never be saved and restored (see stack_grow()) */
static int lex_max, *lex_match;
#if SHOPT_KIA
static void refvar(Lex_t *lp, int type)
{
off_t off = (fcseek(0)-(type+1))-(lp->lexd.first?lp->lexd.first:fcfirst());
unsigned long r;
if(lp->lexd.first)
{
off = (fcseek(0)-(type+1)) - lp->lexd.first;
r=kiaentity(lp,lp->lexd.first+kia.offset+type,off-kia.offset,'v',-1,-1,kia.current,'v',0,"");
}
else
{
int n,offset = stktell(sh.stk);
void *savptr;
char *begin;
off = offset + (fcseek(0)-(type+1)) - fcfirst();
if(kia.offset < offset)
{
/* variable starts on stack, copy remainder */
if(off>offset)
sfwrite(sh.stk,fcfirst()+type,off-offset);
n = stktell(sh.stk)-kia.offset;
begin = stkptr(sh.stk,kia.offset);
}
else
{
/* variable in data buffer */
begin = fcfirst()+(type+kia.offset-offset);
n = off-kia.offset;
}
savptr = stkfreeze(sh.stk,0);
r=kiaentity(lp,begin,n,'v',-1,-1,kia.current,'v',0,"");
stkset(sh.stk,savptr,offset);
}
sfprintf(kia.tmp,"p;%..64d;v;%..64d;%d;%d;r;\n",kia.current,r,sh.inlineno,sh.inlineno);
}
#endif /* SHOPT_KIA */
/*
* This routine gets called when reading across a buffer boundary
* If lexd.nocopy is off, then current token is saved on the stack
*/
static void lex_advance(Sfio_t *iop, const char *buff, int size, void *context)
{
Lex_t *lp = (Lex_t*)context;
/* write to history file and to stderr if necessary */
if(iop && !sfstacked(iop))
{
if(sh_isstate(SH_HISTORY) && sh.hist_ptr)
sfwrite(sh.hist_ptr->histfp, buff, size);
if(sh_isstate(SH_VERBOSE))
sfwrite(sfstderr, buff, size);
}
if(lp->lexd.nocopy)
return;
if(lp->lexd.dolparen && lp->lexd.docword && lp->lexd.docend)
{
int n = size - (lp->lexd.docend-(char*)buff);
sfwrite(sh.strbuf,lp->lexd.docend,n);
lp->lexd.docextra += n;
if(sffileno(iop)>=0)
lp->lexd.docend = sfsetbuf(iop,iop,0);
else
lp->lexd.docend = fcfirst();
}
if(lp->lexd.first)
{
size -= (lp->lexd.first-(char*)buff);
buff = lp->lexd.first;
if(!lp->lexd.inlexskip)
lp->arg = stkseek(sh.stk,ARGVAL);
#if SHOPT_KIA
kia.offset += ARGVAL;
#endif /* SHOPT_KIA */
}
if(size>0 && (lp->arg||lp->lexd.inlexskip))
{
sfwrite(sh.stk,buff,size);
lp->lexd.first = 0;
}
}
/*
* fill up another input buffer
* preserves lexical state
*/
static int lexfill(Lex_t *lp)
{
int c;
Lex_t savelex;
struct argnod *ap;
int aok,docextra;
savelex = *lp;
ap = lp->arg;
c = fcfill();
if(ap)
lp->arg = ap;
docextra = lp->lexd.docextra;
lp->lex = savelex.lex;
lp->lexd = savelex.lexd;
if(fcfile() || c)
lp->lexd.first = 0;
aok= lp->aliasok;
ap = lp->arg;
memcpy(lp, &savelex, offsetof(Lex_t,lexd));
lp->arg = ap;
lp->aliasok = aok;
if(lp->lexd.docword && docextra)
{
lp->lexd.docextra = docextra;
lp->lexd.docend = fcseek(0)-1;
}
return c;
}
/*
* mode=1 for reinitialization
*/
Lex_t *sh_lexopen(Lex_t *lp, int mode)
{
if(!lp)
lp = sh_newof(0,Lex_t,1,0);
else if(mode)
lp->lex.intest = lp->lex.incase = lp->lex.skipword = lp->comp_assign = lp->comsub = lp->assignok = 0;
else
{ /* full reset: everything except LINENO */
int lastline = lp->lastline, inlineno = lp->inlineno, firstline = lp->firstline;
memset(lp,0,sizeof(Lex_t));
lp->lastline = lastline, lp->inlineno = inlineno, lp->firstline = firstline;
}
lp->lex.reservok = 1;
lp->lexd.warn = !sh_isoption(SH_DICTIONARY) && sh_isoption(SH_NOEXEC);
fcnotify(lex_advance,lp);
return lp;
}
#ifdef DBUG
extern int lextoken(Lex_t*);
int sh_lex(Lex_t *lp)
{
int flag;
char *quoted, *macro, *split, *expand;
int tok = lextoken(lp);
quoted = macro = split = expand = "";
if(tok==0 && (flag=lp->arg->argflag))
{
if(flag&ARG_MAC)
macro = "macro:";
if(flag&ARG_EXP)
expand = "expand:";
if(flag&ARG_QUOTED)
quoted = "quoted:";
}
sfprintf(sfstderr,"%jd: line %d: %o:%s%s%s%s %s\n",(Sflong_t)sh.current_pid,sh.inlineno,tok,quoted,
macro, split, expand, fmttoken(lp,tok));
return tok;
}
#define sh_lex lextoken
#endif
/*
* Get the next word and put it on the top of the stack
* A pointer to the current word is stored in lp->arg
* Returns the token type
*/
int sh_lex(Lex_t* lp)
{
const char *state;
int n, c, mode=ST_BEGIN, wordflags=0;
int inlevel=lp->lexd.level, assignment=0, ingrave=0;
int epatchar=0;
int varnametry = 0, varnamecount = 0, varnamelength = 0;
SETLEN(1);
if(lp->lexd.paren)
{
lp->lexd.paren = 0;
return lp->token=LPAREN;
}
if(lp->noreserv)
{
lp->lex.reservok = 0;
while((c = fcgetc()) && (c==' ' || c== '\t' || c=='\n'))
if(c=='\n')
sh.inlineno++;
fcseek(-LEN);
if(c=='[')
lp->assignok = SH_ASSIGN;
}
if(lp->lex.incase)
lp->assignok = 0;
else
lp->assignok |= lp->lex.reservok;
if(lp->comp_assign==2)
lp->comp_assign = lp->lex.reservok = 0;
lp->lexd.arith = (lp->lexd.nest==1);
if(lp->lexd.nest)
{
pushlevel(lp,lp->lexd.nest,ST_NONE);
lp->lexd.nest = 0;
mode = lp->lexd.lex_state;
}
else if(lp->lexd.docword)
{
if((c = fcgetc())=='-' || c=='#')
{
lp->lexd.docword++;
lp->digits=(c=='#'?3:1);
}
else if(c=='<')
{
lp->digits=2;
lp->lexd.docword=0;
}
else if(c>0)
fcseek(-LEN);
}
if(!lp->lexd.dolparen)
{
lp->arg = 0;
if(mode!=ST_BEGIN)
lp->lexd.first = fcseek(0);
else
lp->lexd.first = 0;
}
lp->lastline = sh.inlineno;
while(1)
{
/* skip over characters in the current state */
state = sh_lexstates[mode];
do {
n = STATE(state,c);
/*
* Bug-691: Phi gross hack
* Greedy ${list} will treat all '}' not on a reserved
* word (i.e after [;\n]) as regular char.
* Non greedy close ${ on first '}' (ATT code)
*/
if( sh_isoption(SH_COMSUB_BRACE_GREEDY) &&
lp->lex.inbracecomsub && c=='}' &&
( state==sh_lexstates[ ST_BEGIN] ||
state==sh_lexstates[ST_NORM]
)
)
{ n=S_REG ; /* treat '}' as reg char */
}
if (varnametry)
varnamecount += LEN;
} while (n == 0);
switch(n)
{
case S_BREAK:
if(lp->lex.incase>TEST_RE && mode==ST_NORM && c==LPAREN)
{
pushlevel(lp,RPAREN,mode);
mode = ST_NESTED;
continue;
}
fcseek(-LEN);
goto breakloop;
case S_EOF:
if((n=lexfill(lp)) > 0)
{
fcseek(-1);
continue;
}
/* check for zero byte in file */
if(n==0 && fcfile())
{
if(sh.readscript)
{
char *cp = error_info.id;
errno = ENOEXEC;
error_info.id = sh.readscript;
errormsg(SH_DICT,ERROR_system(ERROR_NOEXEC),e_exec,cp);
UNREACHABLE();
}
else
{
lp->token = -1;
sh_syntax(lp,0);
}
}
/* end-of-file */
if(mode==ST_BEGIN)
return lp->token=EOFSYM;
if(mode >ST_NORM && lp->lexd.level>0 && !lp->lexd.inlexskip)
{
switch(c=endchar(lp))
{
case '$':
if(mode==ST_LIT)
{
c = '\'';
break;
}
mode = oldmode(lp);
poplevel(lp);
continue;
case RBRACT:
c = LBRACT;
break;
case 1: /* for ((...)) */
case RPAREN:
c = LPAREN;
break;
default:
c = LBRACE;
break;
case '"': case '`': case '\'':
break;
}
lp->lasttok = c;
lp->token = EOFSYM;
sh_syntax(lp,0);
}
goto breakloop;
case S_COM:
/* skip one or more comment line(s) */
lp->lex.reservok = !lp->lex.intest;
if((n=lp->lexd.nocopy) && lp->lexd.dolparen)
lp->lexd.nocopy--;
do
{
while((c = fcgetc()) > 0 && c!='\n');
if(c<=0 || lp->heredoc)
{
sh.inlineno++;
break;
}
while(sh.inlineno++,fcpeek(0)=='\n')
fcseek(1);
while(state[c=fcpeek(0)]==0)
fcseek(1);
}
while(c=='#');
lp->lexd.nocopy = n;
if(c<0)
return lp->token=EOFSYM;
n = S_NLTOK;
sh.inlineno--;
/* FALLTHROUGH */
case S_NLTOK:
/* check for here-document */
if(lp->heredoc)
{
if(!lp->lexd.dolparen)
lp->lexd.nocopy++;
c = sh.inlineno;
if(here_copy(lp,lp->heredoc)<=0 && lp->lasttok)
{
lp->lasttok = IODOCSYM;
lp->token = EOFSYM;
lp->lastline = c;
sh_syntax(lp,0);
}
if(!lp->lexd.dolparen)
lp->lexd.nocopy--;
lp->heredoc = 0;
}
lp->lex.reservok = !lp->lex.intest;
lp->lex.skipword = 0;
/* FALLTHROUGH */
case S_NL:
/* bug-691: Phi: */
if( sh_isoption(SH_COMSUB_BRACE_GREEDY) )
lp->lex.inbracecomsub=0;
/* skip over new-lines */
lp->lex.last_quote = 0;
while(sh.inlineno++,fcget()=='\n');
fcseek(-LEN);
if(n==S_NLTOK)
{
lp->comp_assign = 0;
return lp->token='\n';
}
/* FALLTHROUGH */
case S_BLNK:
if(lp->lex.incase<=TEST_RE)
continue;
/* implicit RPAREN for =~ test operator */
if(inlevel+1==lp->lexd.level)
{
if(lp->lex.intest)
fcseek(-LEN);
c = RPAREN;
goto do_pop;
}
continue;
case S_OP:
/* bug-691: Phi: */
if( sh_isoption(SH_COMSUB_BRACE_GREEDY) )
lp->lex.inbracecomsub=0;
/* return operator token */
if(c=='<' || c=='>')
{
if(lp->lex.testop2)
lp->lex.testop2 = 0;
else
{
lp->digits = (c=='>');
lp->lex.skipword = 1;
lp->aliasok = lp->lex.reservok;
if(lp->lex.incase<2)
lp->lex.reservok = 0;
}
}
else
{
lp->lex.reservok = !lp->lex.intest;
if(c==RPAREN)
{
if(!lp->lexd.dolparen)
lp->lex.incase = 0;
return lp->token=c;
}
lp->lex.testop1 = lp->lex.intest;
}
if((n = fcgetc()) > 0)
fcseek(-LEN);
if(state[n]==S_OP || n=='#')
{
if(n==c)
{
if(c==LPAREN)
{
int r;
/* Avoid misdetecting EXPRSYM in [[ ... ]] or compound assignments */
if(lp->lex.intest || lp->comp_assign)
return lp->token=c;
/* The comsub() reading hack avoids the parser, so comp_assign is never
* set; try to detect compound assignments with this workaround instead */
if(lp->lexd.dolparen && !lp->lexd.dolparen_arithexp
&& (fcpeek(-2)=='=' || lp->lexd.dolparen_eqparen))
return lp->token=c;
/* OK, maybe this is EXPRSYM (arith '((', possibly following '$').
* But this cannot be concluded until a final '))' is detected.
* Use a recursive lexer invocation for that. */
lp->lexd.nest=1;
lp->lastline = sh.inlineno;
lp->lexd.lex_state = ST_NESTED;
fcseek(1);
r = sh_lex(lp);
if(r!=LPAREN && r!=EXPRSYM)
{ /* throw "`(' unmatched" error */
lp->lasttok = LPAREN;
lp->token = EOFSYM;
sh_syntax(lp,0);
}
return r;
}
c |= SYMREP;
/* Check for repeated operator character plus &, like ;;& (FALLMATCHSYM) */
fcgetc();
if(fcpeek(0)=='&')
c |= SYMAMP;
else
fcseek(-1);
/* Here document redirection operator '<<' */
if(c==IODOCSYM)
lp->lexd.docword = 1;
}
else if(c=='(' || c==')')
return lp->token=c;
else if(c=='&')
{
if(n=='>' && !sh_isoption(SH_POSIX))
{
/* bash-style "&>file" shorthand for ">file 2>&1" */
lp->digits = -1;
c = '>';
/* bash 4.0-style "&>>file" shorthand for ">>file 2>&1" */
fcgetc();
if(fcpeek(0)==c)
c |= SYMREP;
else
fcseek(-1);
}
else if(n=='|')
c |= SYMPIPE;
else
n = 0;
}
else if(n=='&')
c |= SYMAMP;
else if(c!='<' && c!='>')
n = 0;
else if(n==LPAREN)
{
/* process substitution <(...) or >(...) */
c |= SYMLPAR;
lp->lex.reservok = 1;
lp->lex.skipword = 0;
}
else if(n=='|')
c |= SYMPIPE;
else if(c=='<' && n=='>')
{
lp->digits = sh_isoption(SH_POSIX) ? 0 : 1;
c = IORDWRSYM;
fcgetc();
if((n = fcgetc())==';')
{
lp->token = c = IORDWRSYMT;
if(lp->inexec)
sh_syntax(lp,0);
}
else if(n>0)
fcseek(-LEN);
n= 0;
}
else if(n=='#' && (c=='<'||c=='>'))
c |= SYMSHARP;
else if(n==';' && c=='>')
{
c |= SYMSEMI;
if(lp->inexec)
{
lp->token = c;
sh_syntax(lp,0);
}
}
else
n = 0;
if(n)
{
fcseek(1);
lp->lex.incase = (c==BREAKCASESYM || c==FALLTHRUSYM || c==FALLMATCHSYM);
}
else
{
if(lp->lexd.warn && (n=fcpeek(0))!=RPAREN && n!=' ' && n!='\t')
errormsg(SH_DICT,ERROR_warn(0),e_lexspace,sh.inlineno,c,n);
}
}
if(c==LPAREN && lp->comp_assign && !lp->lex.intest && !lp->lex.incase)
lp->comp_assign = 2;
else
lp->comp_assign = 0;
return lp->token=c;
case S_ESC:
/* check for \<new-line> */
n = fcgetc();
c=2;
#if SHOPT_CRNL
if(n=='\r')
{
if((n = fcgetc())=='\n')
c=3;
else
{
n='\r';
fcseek(-LEN);
}
}
#endif /* SHOPT_CRNL */
if(n=='\n')
{
Sfio_t *sp;
struct argnod *ap;
sh.inlineno++;
/* synchronize */
if(!(sp=fcfile()))
state=fcseek(0);
fcclose();
ap = lp->arg;
if(sp)
fcfopen(sp);
else
fcsopen((char*)state);
/* remove \new-line */
n = stktell(sh.stk)-c;
stkseek(sh.stk,n);
lp->arg = ap;
if(n<=ARGVAL)
{
mode = 0;
lp->lexd.first = 0;
}
continue;
}
wordflags |= ARG_QUOTED;
if(mode==ST_DOL)
goto err;
#ifndef STR_MAXIMAL
else if(mode==ST_NESTED && lp->lexd.warn &&
endchar(lp)==RBRACE &&
sh_lexstates[ST_DOL][n]==S_DIG
)
errormsg(SH_DICT,ERROR_warn(0),e_lexfuture,sh.inlineno,n);
#endif /* STR_MAXIMAL */
break;
case S_NAME:
if(!lp->lex.skipword)
lp->lex.reservok *= 2;
/* FALLTHROUGH */
case S_TILDE:
if(c=='~' && mode==ST_NESTED)
{
if(endchar(lp)==RBRACE)
goto tilde;
continue;
}
/* FALLTHROUGH */
case S_RES:
varnametry = 1;
varnamecount = LEN;
if(!lp->lexd.dolparen)
lp->lexd.first = fcseek(0)-LEN;
else if(lp->lexd.docword)
lp->lexd.docend = fcseek(0)-LEN;
mode = ST_NAME;
if(c=='.')
{
fcseek(-LEN);
varnamecount -= LEN;
}
if(n!=S_TILDE)
continue;
tilde:
n = fcgetc();
if(n>0)
{
if(c=='~' && n==LPAREN && lp->lex.incase)
lp->lex.incase = TEST_RE;
fcseek(-LEN);
}
if(n==LPAREN)
goto epat;
wordflags = ARG_MAC;
mode = ST_NORM;
continue;
case S_REG:
if(mode==ST_BEGIN)
{
do_reg:
/* skip new-line joining if called from comsub() */
if(c=='\\' && fcpeek(0)=='\n' && !lp->lexd.dolparen)
{
sh.inlineno++;
fcseek(1);
continue;
}
fcseek(-LEN);
if(!lp->lexd.dolparen)
lp->lexd.first = fcseek(0);
else if(lp->lexd.docword)
lp->lexd.docend = fcseek(0);
if(c=='[' && lp->assignok>=SH_ASSIGN)
{
mode = ST_NAME;
continue;
}
}
mode = ST_NORM;
continue;
case S_LIT:
if(oldmode(lp)==ST_NONE && !lp->lexd.inlexskip) /* in ((...)) */
{
if((c=fcpeek(0))==LPAREN || c==RPAREN || c=='$' || c==LBRACE || c==RBRACE || c=='[' || c==']')
{
if(fcpeek(1)=='\'')
fcseek(2);
}
continue;
}
wordflags |= ARG_QUOTED;
if(mode==ST_DOL)
{
if(endchar(lp)!='$')
goto err;
if(oldmode(lp)==ST_QUOTE) /* $' within "" or `` */
{
if(lp->lexd.warn)
errormsg(SH_DICT,ERROR_warn(0),e_lexslash,sh.inlineno);
mode = ST_LIT;
}
}
if(mode!=ST_LIT)
{
if(lp->lexd.warn && lp->lex.last_quote && sh.inlineno > lp->lastline && fcpeek(-2)!='$')
errormsg(SH_DICT,ERROR_warn(0),e_lexlongquote,lp->lastline,lp->lex.last_quote);
lp->lex.last_quote = 0;
lp->lastline = sh.inlineno;
if(mode!=ST_DOL)
pushlevel(lp,'\'',mode);
mode = ST_LIT;
continue;
}
/* check for multi-line single-quoted string */
else if(sh.inlineno > lp->lastline)
lp->lex.last_quote = '\'';
mode = oldmode(lp);
poplevel(lp);
break;
case S_ESC2:
/* \ inside '' */
if(endchar(lp)=='$')
{
n = fcgetc();
if(n=='\n')
sh.inlineno++;
}
continue;
case S_GRAVE:
if(lp->lexd.warn && (mode!=ST_QUOTE || endchar(lp)!='`'))
errormsg(SH_DICT,ERROR_warn(0),e_lexobsolete1,sh.inlineno);
wordflags |=(ARG_MAC|ARG_EXP);
if(mode==ST_QUOTE)
ingrave = !ingrave;
/* FALLTHROUGH */
case S_QUOTE:
if(oldmode(lp)==ST_NONE && lp->lexd.arith) /* in ((...)) */
{
if(n!=S_GRAVE || fcpeek(0)=='\'')
continue;
}
if(n==S_QUOTE)
wordflags |=ARG_QUOTED;
if(mode!=ST_QUOTE)
{
if(c!='"' || mode!=ST_QNEST)
{
if(lp->lexd.warn && lp->lex.last_quote && sh.inlineno > lp->lastline)
errormsg(SH_DICT,ERROR_warn(0),e_lexlongquote,lp->lastline,lp->lex.last_quote);
lp->lex.last_quote=0;
lp->lastline = sh.inlineno;
pushlevel(lp,c,mode);
}
ingrave ^= (c=='`');
mode = ST_QUOTE;
continue;
}
else if((n=endchar(lp))==c)
{
if(sh.inlineno > lp->lastline)
lp->lex.last_quote = c;
/*
* At this point, we know that the previous skipping of characters was done
* according to the ST_QUOTE state table. We also know that the character that
* stopped the skipping is also the ending character for the current level. If
* that character was " and if we are in a `...` statement, then don't switch
* to the old mode, as we are actually at the innermost section of a "`"..."`"
* statement, which should be skipped again using the ST_QUOTE state table.
*/
if(c!='"' || !ingrave)
{
mode = oldmode(lp);
poplevel(lp);
}
}
else if(c=='"' && n==RBRACE)
mode = ST_QNEST;
break;
case S_DOL:
/* don't check syntax inside `` */
if(mode==ST_QUOTE && ingrave)
continue;
#if SHOPT_KIA
if(lp->lexd.first)
kia.offset = fcseek(0)-lp->lexd.first;
else
kia.offset = stktell(sh.stk)+fcseek(0)-fcfirst();
#endif /* SHOPT_KIA */
pushlevel(lp,'$',mode);
mode = ST_DOL;
continue;
case S_PAR:
do_comsub:
wordflags |= ARG_MAC;
mode = oldmode(lp);
poplevel(lp);
fcseek(-LEN);
n = lp->digits;
wordflags |= comsub(lp,c);
lp->digits = n;
continue;
case S_RBRA:
if((n=endchar(lp)) == '$')
goto err;
if(mode!=ST_QUOTE || n==RBRACE)
{
mode = oldmode(lp);
poplevel(lp);
}
break;
case S_EDOL:
/* end $identifier */
#if SHOPT_KIA
if(kia.file)
refvar(lp,0);
#endif /* SHOPT_KIA */
if(lp->lexd.warn && c==LBRACT && !lp->lex.intest && !lp->lexd.arith && oldmode(lp)!= ST_NESTED)
errormsg(SH_DICT,ERROR_warn(0),e_lexusebrace,sh.inlineno);
fcseek(-LEN);
mode = oldmode(lp);
poplevel(lp);
break;
case S_DOT:
if(varnamelength && fcpeek(-LEN - 1)==']')
varnamelength = 0;
/* make sure next character is alpha */
if((n = fcgetc()) > 0)
{
if(n=='.')
n = fcgetc();
if(n>0)
fcseek(-LEN);
}
if(isaletter(n) || n==LBRACT)
continue;
if(mode==ST_NAME)
{
if(n=='=')
continue;
break;
}
else if(n==RBRACE)
continue;
if(isastchar(n))
continue;
goto err;
case S_SPC1:
wordflags |= ARG_MAC;
if(endchar(lp)==RBRACE)
{
setchar(lp,c);
continue;
}
/* FALLTHROUGH */
case S_ALP:
if(c=='.' && endchar(lp)=='$')
goto err;
/* FALLTHROUGH */
case S_SPC2:
case S_DIG:
wordflags |= ARG_MAC;
switch(endchar(lp))
{
case '$':
if(n==S_ALP) /* $identifier */
mode = ST_DOLNAME;
else
{
mode = oldmode(lp);
poplevel(lp);
}
break;
case '@':
case '!':
if(n!=S_ALP)
goto dolerr;
/* FALLTHROUGH */
case '#':
if(c=='#')
n = S_ALP;
/* FALLTHROUGH */
case RBRACE:
if(n==S_ALP)
{
setchar(lp,RBRACE);
if(c=='.')
fcseek(-LEN);
mode = ST_BRACE;
}
else
{
if((c = fcgetc()) > 0)
fcseek(-LEN);
if(state[c]==S_ALP)
goto err;
if(n==S_DIG)
setchar(lp,'0');
else
setchar(lp,'!');
}
break;
case '0':
if(n==S_DIG)
break;
default:
goto dolerr;
}
break;
dolerr:
case S_ERR:
if((n=endchar(lp)) == '$')
goto err;
if(c=='*' || (n=sh_lexstates[ST_BRACE][c])!=S_MOD1 && n!=S_MOD2)
{
/* see whether inside `...` */
if((n = endchar(lp)) != '`')
goto err;
mode = oldmode(lp);
poplevel(lp);
pushlevel(lp,RBRACE,mode);
}
else
setchar(lp,RBRACE);
mode = ST_NESTED;
continue;
case S_MOD1:
mode = ST_MOD1;
continue;
case S_MOD2:
#if SHOPT_KIA
if(kia.file)
refvar(lp,1);
#endif /* SHOPT_KIA */
if(c!=':' && (n = fcgetc()) > 0)
{
if(n!=c)
c = 0;
if(!c || (n = fcgetc()) > 0)
{
fcseek(-LEN);