-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.c
1776 lines (1657 loc) · 40 KB
/
lex.c
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
/* $OpenBSD: lex.c,v 1.51 2015/09/10 22:48:58 nicm Exp $ */
/*-
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
* 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
* 2021, 2022, 2023
* mirabilos <m@mirbsd.org>
*
* Provided that these terms and disclaimer and all copyright notices
* are retained or reproduced in an accompanying document, permission
* is granted to deal in this work without restriction, including un-
* limited rights to use, publicly perform, distribute, sell, modify,
* merge, give away, or sublicence.
*
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
* the utmost extent permitted by applicable law, neither express nor
* implied; without malicious intent or gross negligence. In no event
* may a licensor, author or contributor be held liable for indirect,
* direct, other damage, loss, or other issues arising in any way out
* of dealing in the work, even if advised of the possibility of such
* damage or existence of a defect, except proven that it results out
* of said person's immediate fault when using the work as intended.
*/
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.267 2023/04/16 00:40:13 tg Exp $");
/*
* states while lexing word
*/
#define SBASE 0 /* outside any lexical constructs */
#define SWORD 1 /* implicit quoting for substitute() */
#define SLETPAREN 2 /* inside (( )), implicit quoting */
#define SSQUOTE 3 /* inside '' */
#define SDQUOTE 4 /* inside "" */
#define SEQUOTE 5 /* inside $'' */
#define SBRACE 6 /* inside ${} */
#define SQBRACE 7 /* inside "${}" */
#define SBQUOTE 8 /* inside `` */
#define SASPAREN 9 /* inside $(( )) */
#define SHEREDELIM 10 /* parsing << or <<- delimiter */
#define SHEREDQUOTE 11 /* parsing " in << or <<- delimiter */
#define SPATTERN 12 /* parsing *(...|...) pattern (*+?@!) */
#define SADELIM 13 /* like SBASE, looking for delimiter */
#define STBRACEKORN 14 /* parsing ${...[#%]...} !FSH */
#define STBRACEBOURNE 15 /* parsing ${...[#%]...} FSH */
#define SINVALID 255 /* invalid state */
struct sretrace_info {
struct sretrace_info *next;
XString xs;
char *xp;
};
/*
* Structure to keep track of the lexing state and the various pieces of info
* needed for each particular state.
*/
typedef struct lex_state {
union {
/* point to the next state block */
struct lex_state *base;
/* marks start of state output in output string */
size_t start;
/* SBQUOTE: Ja if in double quotes: "`...`" */
/* SEQUOTE: got NUL, ignore rest of string */
Wahr sxqflag;
/* SADELIM information */
struct {
/* character to search for */
unsigned char delimiter;
/* max. number of delimiters */
unsigned char num;
} adelim;
} u;
/* count open parentheses */
short nparen;
/* type of this state */
kby type;
/* extra flags */
kby ls_flags;
} Lex_state;
#define ls_base u.base
#define ls_start u.start
#define ls_flag u.sxqflag
#define ls_adelim u.adelim
/* ls_flags */
#define LS_HEREDOC BIT(0)
typedef struct {
Lex_state *base;
Lex_state *end;
} State_info;
static void readhere(struct ioword *);
static void ungetsc(int);
static void ungetsc_i(int);
static int getsc_uu(void);
static void getsc_line(Source *);
static int getsc_bn(void);
static int getsc_i(void);
static char *get_brace_var(XString *, char *);
static Wahr arraysub(char **);
static void gethere(void);
static Lex_state *push_state_i(State_info *, Lex_state *);
static Lex_state *pop_state_i(State_info *, Lex_state *);
static int backslash_skip;
static int ignore_backslash_newline;
/* optimised getsc_bn() */
#define o_getsc() (*source->str != '\0' && *source->str != '\\' && \
!backslash_skip ? *source->str++ : getsc_bn())
/* optimised getsc_uu() */
#define o_getsc_u() ((*source->str != '\0') ? *source->str++ : getsc_uu())
/* retrace helper */
#define o_getsc_r(carg) \
int cev = (carg); \
struct sretrace_info *rp = retrace_info; \
\
while (rp) { \
Xcheck(rp->xs, rp->xp); \
*rp->xp++ = cev; \
rp = rp->next; \
} \
\
return (cev);
/* callback */
static int
getsc_i(void)
{
o_getsc_r((unsigned int)(unsigned char)o_getsc());
}
#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
#define getsc() getsc_i()
#else
static int getsc_r(int);
static int
getsc_r(int c)
{
o_getsc_r(c);
}
#define getsc() getsc_r((unsigned int)(unsigned char)o_getsc())
#endif
#define STATE_BSIZE 8
#define PUSH_STATE(s) do { \
kby state_flags = statep->ls_flags; \
if (++statep == state_info.end) \
statep = push_state_i(&state_info, statep); \
state = statep->type = (s); \
statep->ls_flags = state_flags; \
} while (/* CONSTCOND */ 0)
#define POP_STATE() do { \
if (--statep == state_info.base) \
statep = pop_state_i(&state_info, statep); \
state = statep->type; \
} while (/* CONSTCOND */ 0)
#define PUSH_SRETRACE(s) do { \
struct sretrace_info *ri; \
\
PUSH_STATE(s); \
statep->ls_start = Xsavepos(ws, wp); \
ri = alloc(sizeof(struct sretrace_info), ATEMP); \
Xinit(ri->xs, ri->xp, 64, ATEMP); \
ri->next = retrace_info; \
retrace_info = ri; \
} while (/* CONSTCOND */ 0)
#define POP_SRETRACE() do { \
wp = Xrestpos(ws, wp, statep->ls_start); \
*retrace_info->xp = '\0'; \
sp = Xstring(retrace_info->xs, retrace_info->xp); \
dp = (void *)retrace_info; \
retrace_info = retrace_info->next; \
afree(dp, ATEMP); \
POP_STATE(); \
} while (/* CONSTCOND */ 0)
/**
* Lexical analyser
*
* tokens are not regular expressions, they are LL(1).
* for example, "${var:-${PWD}}", and "$(size $(whence ksh))".
* hence the state stack. Note "$(...)" are now parsed recursively.
*/
int
yylex(int cf)
{
Lex_state states[STATE_BSIZE], *statep, *s2, *base;
State_info state_info;
int c, c2, state;
size_t cz;
XString ws; /* expandable output word */
char *wp; /* output word pointer */
char *sp, *dp;
Again:
states[0].type = SINVALID;
states[0].ls_base = NULL;
statep = &states[1];
state_info.base = states;
state_info.end = &state_info.base[STATE_BSIZE];
Xinit(ws, wp, 64, ATEMP);
backslash_skip = 0;
ignore_backslash_newline = 0;
if (cf & ONEWORD)
state = SWORD;
else if (cf & LETEXPR) {
/* enclose arguments in (double) quotes */
*wp++ = OQUOTE;
state = SLETPAREN;
statep->nparen = 0;
} else {
/* normal lexing */
state = (cf & HEREDELIM) ? SHEREDELIM : SBASE;
do {
c = getsc();
} while (ctype(c, C_BLANK));
if (c == '#') {
ignore_backslash_newline++;
do {
c = getsc();
} while (!ctype(c, C_NUL | C_LF));
ignore_backslash_newline--;
}
ungetsc(c);
}
if (source->flags & SF_ALIAS) {
/* trailing ' ' in alias definition */
source->flags &= ~SF_ALIAS;
/* POSIX: trailing space only counts if parsing simple cmd */
if (!Flag(FPOSIX) || (cf & CMDWORD))
cf |= ALIAS;
}
/* Initial state: one of SWORD SLETPAREN SHEREDELIM SBASE */
statep->type = state;
statep->ls_flags = (cf & HEREDOC) ? LS_HEREDOC : 0;
/* collect non-special or quoted characters to form word */
while ((c = getsc())) {
switch (state) {
case SBASE:
/* possibly end "${ :;}" */
if ((unsigned int)c == subshell_nesting_type)
goto Done;
/* FALLTHROUGH */
case SHEREDELIM:
if (ctype(c, C_LEX1))
goto Done;
break;
}
Xcheck(ws, wp);
switch (state) {
case SADELIM:
if ((unsigned int)c == ORD('('))
statep->nparen++;
else if ((unsigned int)c == ORD(')'))
statep->nparen--;
else if (statep->nparen == 0 &&
((unsigned int)c == ORD(/*{*/ '}') ||
c == (int)statep->ls_adelim.delimiter)) {
*wp++ = ADELIM;
*wp++ = c;
if ((unsigned int)c == ORD(/*{*/ '}') ||
--statep->ls_adelim.num == 0)
POP_STATE();
if ((unsigned int)c == ORD(/*{*/ '}'))
POP_STATE();
break;
}
/* FALLTHROUGH */
case SBASE:
if ((unsigned int)c == ORD('[') && (cf & CMDASN)) {
/* temporary */
*wp = EOS;
if (is_wdvarname(Xstring(ws, wp), Nee)) {
char *p, *tmp;
if (arraysub(&tmp)) {
*wp++ = CHAR;
*wp++ = c;
for (p = tmp; *p; ) {
Xcheck(ws, wp);
*wp++ = CHAR;
*wp++ = *p++;
}
afree(tmp, ATEMP);
break;
}
}
*wp++ = CHAR;
*wp++ = c;
break;
}
/* FALLTHROUGH */
Sbase1: /* includes *(...|...) pattern (*+?@!) */
if (ctype(c, C_PATMO)) {
c2 = getsc();
if ((unsigned int)c2 == ORD('(' /*)*/)) {
*wp++ = OPAT;
*wp++ = c;
PUSH_STATE(SPATTERN);
break;
}
ungetsc(c2);
}
/* FALLTHROUGH */
Sbase2: /* doesn't include *(...|...) pattern (*+?@!) */
switch (c) {
case ORD('\\'):
getsc_qchar:
if ((c = getsc())) {
/* trailing \ is lost */
*wp++ = QCHAR;
*wp++ = c;
}
break;
case ORD('\''):
open_ssquote_unless_heredoc:
if ((statep->ls_flags & LS_HEREDOC))
goto store_char;
*wp++ = OQUOTE;
ignore_backslash_newline++;
PUSH_STATE(SSQUOTE);
break;
case ORD('"'):
open_sdquote:
*wp++ = OQUOTE;
PUSH_STATE(SDQUOTE);
break;
case ORD('$'):
/*
* processing of dollar sign belongs into
* Subst, except for those which can open
* a string: $'…' and $"…"
*/
subst_dollar_ex:
c = getsc();
switch (c) {
case ORD('"'):
goto open_sdquote;
case ORD('\''):
goto open_sequote;
default:
goto SubstS;
}
default:
goto Subst;
}
break;
Subst:
switch (c) {
case ORD('\\'):
c = getsc();
switch (c) {
case ORD('"'):
if ((statep->ls_flags & LS_HEREDOC))
goto heredocquote;
/* FALLTHROUGH */
case ORD('\\'):
case ORD('$'):
case ORD('`'):
store_qchar:
*wp++ = QCHAR;
*wp++ = c;
break;
default:
heredocquote:
Xcheck(ws, wp);
if (c) {
/* trailing \ is lost */
*wp++ = CHAR;
*wp++ = '\\';
*wp++ = CHAR;
*wp++ = c;
}
break;
}
break;
case ORD('$'):
c = getsc();
SubstS:
if ((unsigned int)c == ORD('(' /*)*/)) {
c = getsc();
if ((unsigned int)c == ORD('(' /*)*/)) {
*wp++ = EXPRSUB;
PUSH_SRETRACE(SASPAREN);
/* unneeded? */
/*statep->ls_flags &= ~LS_HEREDOC;*/
statep->nparen = 2;
*retrace_info->xp++ = '(';
} else {
ungetsc(c);
subst_command:
c = COMSUB;
subst_command2:
sp = yyrecursive(c);
cz = strlen(sp) + 1;
XcheckN(ws, wp, cz);
*wp++ = c;
memcpy(wp, sp, cz);
wp += cz;
}
} else if ((unsigned int)c == ORD('{' /*}*/)) {
if ((unsigned int)(c = getsc()) == ORD('|')) {
/*
* non-subenvironment
* value substitution
*/
c = VALSUB;
goto subst_command2;
} else if (ctype(c, C_IFSWS)) {
/*
* non-subenvironment
* "command" substitution
*/
c = FUNSUB;
goto subst_command2;
}
ungetsc(c);
*wp++ = OSUBST;
*wp++ = '{' /*}*/;
wp = get_brace_var(&ws, wp);
c = getsc();
/* allow :# and :% (ksh88 compat) */
if ((unsigned int)c == ORD(':')) {
*wp++ = CHAR;
*wp++ = c;
c = getsc();
if ((unsigned int)c == ORD(':')) {
*wp++ = CHAR;
*wp++ = '0';
*wp++ = ADELIM;
*wp++ = ':';
PUSH_STATE(SBRACE);
/* perhaps unneeded? */
statep->ls_flags &= ~LS_HEREDOC;
PUSH_STATE(SADELIM);
statep->ls_adelim.delimiter = ':';
statep->ls_adelim.num = 1;
statep->nparen = 0;
break;
} else if (ctype(c, C_ALNUX | C_DOLAR | C_SPC) ||
c == '(' /*)*/) {
/* substring subst. */
if (c != ' ') {
*wp++ = CHAR;
*wp++ = ' ';
}
ungetsc(c);
PUSH_STATE(SBRACE);
/* perhaps unneeded? */
statep->ls_flags &= ~LS_HEREDOC;
PUSH_STATE(SADELIM);
statep->ls_adelim.delimiter = ':';
statep->ls_adelim.num = 2;
statep->nparen = 0;
break;
}
} else if (c == '/') {
c2 = ADELIM;
parse_adelim_slash:
*wp++ = CHAR;
*wp++ = c;
if ((unsigned int)(c = getsc()) == ORD('/')) {
*wp++ = c2;
*wp++ = c;
} else
ungetsc(c);
PUSH_STATE(SBRACE);
/* perhaps unneeded? */
statep->ls_flags &= ~LS_HEREDOC;
PUSH_STATE(SADELIM);
statep->ls_adelim.delimiter = '/';
statep->ls_adelim.num = 1;
statep->nparen = 0;
break;
} else if (c == '@') {
c2 = getsc();
ungetsc(c2);
if ((unsigned int)c2 == ORD('/')) {
c2 = CHAR;
goto parse_adelim_slash;
}
}
/*
* If this is a trim operation,
* treat (,|,) specially in STBRACE.
*/
if (ctype(c, C_SUB2)) {
ungetsc(c);
if (Flag(FSH))
PUSH_STATE(STBRACEBOURNE);
else
PUSH_STATE(STBRACEKORN);
/* single-quotes-in-heredoc-trim */
statep->ls_flags &= ~LS_HEREDOC;
} else {
ungetsc(c);
if (state == SDQUOTE ||
state == SQBRACE)
PUSH_STATE(SQBRACE);
else
PUSH_STATE(SBRACE);
/* here no LS_HEREDOC removal */
/* single-quotes-in-heredoc-braces */
}
} else if (ctype(c, C_ALPHX)) {
*wp++ = OSUBST;
*wp++ = 'X';
do {
Xcheck(ws, wp);
*wp++ = c;
c = getsc();
} while (ctype(c, C_ALNUX));
*wp++ = '\0';
*wp++ = CSUBST;
*wp++ = 'X';
ungetsc(c);
} else if (ctype(c, C_VAR1 | C_DIGIT)) {
Xcheck(ws, wp);
*wp++ = OSUBST;
*wp++ = 'X';
*wp++ = c;
*wp++ = '\0';
*wp++ = CSUBST;
*wp++ = 'X';
} else {
*wp++ = CHAR;
*wp++ = '$';
ungetsc(c);
}
break;
case ORD('`'):
subst_gravis:
PUSH_STATE(SBQUOTE);
*wp++ = COMASUB;
/*
* We need to know whether we are within double
* quotes in order to translate \" to " within
* "…`…\"…`…" because, unlike for COMSUBs, the
* outer double quoteing changes the backslash
* meaning for the inside. For more details:
* https://www.austingroupbugs.net/view.php?id=1015
*/
statep->ls_flag = Nee;
s2 = statep;
base = state_info.base;
while (/* CONSTCOND */ 1) {
for (; s2 != base; s2--) {
if (s2->type == SDQUOTE) {
statep->ls_flag = Ja;
break;
}
}
if (s2 != base)
break;
if (!(s2 = s2->ls_base))
break;
base = s2-- - STATE_BSIZE;
}
break;
case QCHAR:
if (cf & LQCHAR) {
*wp++ = QCHAR;
*wp++ = getsc();
break;
}
/* FALLTHROUGH */
default:
store_char:
*wp++ = CHAR;
*wp++ = c;
}
break;
case SEQUOTE:
if ((unsigned int)c == ORD('\'')) {
POP_STATE();
*wp++ = CQUOTE;
ignore_backslash_newline--;
} else if ((unsigned int)c == ORD('\\')) {
if ((c2 = unbksl(Ja, getsc_i, ungetsc)) == -1)
c2 = getsc();
if (c2 == 0)
statep->ls_flag = Ja;
if (!statep->ls_flag) {
if ((unsigned int)c2 < 0x100) {
*wp++ = QCHAR;
*wp++ = c2;
} else {
char ts[4];
cz = utf_wctomb(ts, c2 - 0x100);
c2 = 0;
while ((size_t)c2 < cz) {
*wp++ = QCHAR;
*wp++ = ts[c2++];
}
}
}
} else if (!statep->ls_flag) {
*wp++ = QCHAR;
*wp++ = c;
}
break;
case SSQUOTE:
if ((unsigned int)c == ORD('\'')) {
POP_STATE();
if ((statep->ls_flags & LS_HEREDOC) ||
state == SQBRACE)
goto store_char;
*wp++ = CQUOTE;
ignore_backslash_newline--;
} else {
*wp++ = QCHAR;
*wp++ = c;
}
break;
case SDQUOTE:
if ((unsigned int)c == ORD('"')) {
POP_STATE();
*wp++ = CQUOTE;
} else
goto Subst;
break;
/* $(( ... )) */
case SASPAREN:
if ((unsigned int)c == ORD('('))
statep->nparen++;
else if ((unsigned int)c == ORD(')')) {
statep->nparen--;
if (statep->nparen == 1) {
/* end of EXPRSUB */
POP_SRETRACE();
if ((unsigned int)(c2 = getsc()) == ORD(/*(*/ ')')) {
cz = strlen(sp) - 2;
XcheckN(ws, wp, cz);
memcpy(wp, sp + 1, cz);
wp += cz;
afree(sp, ATEMP);
*wp++ = '\0';
break;
} else {
Source *s;
ungetsc(c2);
/*
* mismatched parenthesis -
* assume we were really
* parsing a $(...) expression
*/
--wp;
s = pushs(SREREAD,
source->areap);
s->start = s->str =
s->u.freeme = sp;
s->next = source;
source = s;
goto subst_command;
}
}
}
/* reuse existing state machine */
goto Sbase2;
case SQBRACE:
if ((unsigned int)c == ORD('\\')) {
/*
* perform POSIX "quote removal" if the back-
* slash is "special", i.e. same cases as the
* {case '\\':} in Subst: plus closing brace;
* in mksh code "quote removal" on '\c' means
* write QCHAR+c, otherwise CHAR+\+CHAR+c are
* emitted (in heredocquote:)
*/
if ((unsigned int)(c = getsc()) == ORD('"') ||
(unsigned int)c == ORD('\\') ||
ctype(c, C_DOLAR | C_GRAVE) ||
(unsigned int)c == ORD(/*{*/ '}'))
goto store_qchar;
goto heredocquote;
}
goto common_SQBRACE;
case SBRACE:
if ((unsigned int)c == ORD('\''))
goto open_ssquote_unless_heredoc;
else if ((unsigned int)c == ORD('\\'))
goto getsc_qchar;
common_SQBRACE:
if ((unsigned int)c == ORD('"'))
goto open_sdquote;
else if ((unsigned int)c == ORD('$'))
goto subst_dollar_ex;
else if ((unsigned int)c == ORD('`'))
goto subst_gravis;
else if ((unsigned int)c != ORD(/*{*/ '}'))
goto store_char;
POP_STATE();
*wp++ = CSUBST;
*wp++ = /*{*/ '}';
break;
/* Same as SBASE, except (,|,) treated specially */
case STBRACEKORN:
if ((unsigned int)c == ORD('|'))
*wp++ = SPAT;
else if ((unsigned int)c == ORD('(')) {
*wp++ = OPAT;
/* simile for @ */
*wp++ = ' ';
PUSH_STATE(SPATTERN);
} else /* FALLTHROUGH */
case STBRACEBOURNE:
if ((unsigned int)c == ORD(/*{*/ '}')) {
POP_STATE();
*wp++ = CSUBST;
*wp++ = /*{*/ '}';
} else
goto Sbase1;
break;
case SBQUOTE:
if ((unsigned int)c == ORD('`')) {
*wp++ = 0;
POP_STATE();
} else if ((unsigned int)c == ORD('\\')) {
switch (c = getsc()) {
case 0:
/* trailing \ is lost */
break;
case ORD('$'):
case ORD('`'):
case ORD('\\'):
*wp++ = c;
break;
case ORD('"'):
if (statep->ls_flag) {
*wp++ = c;
break;
}
/* FALLTHROUGH */
default:
*wp++ = '\\';
*wp++ = c;
break;
}
} else
*wp++ = c;
break;
/* ONEWORD */
case SWORD:
goto Subst;
/* LETEXPR: (( ... )) */
case SLETPAREN:
if ((unsigned int)c == ORD(/*(*/ ')')) {
if (statep->nparen > 0)
--statep->nparen;
else if ((unsigned int)(c2 = getsc()) == ORD(/*(*/ ')')) {
c = 0;
*wp++ = CQUOTE;
goto Done;
} else {
Source *s;
ungetsc(c2);
ungetsc(c);
/*
* mismatched parenthesis -
* assume we were really
* parsing a (...) expression
*/
*wp = EOS;
sp = Xstring(ws, wp);
dp = wdstrip(sp + 1, WDS_TPUTS);
s = pushs(SREREAD, source->areap);
s->start = s->str = s->u.freeme = dp;
s->next = source;
source = s;
ungetsc('(' /*)*/);
return (ORD('(' /*)*/));
}
} else if ((unsigned int)c == ORD('('))
/*
* parentheses inside quotes and
* backslashes are lost, but AT&T ksh
* doesn't count them either
*/
++statep->nparen;
goto Sbase2;
/* << or <<- delimiter */
case SHEREDELIM:
/*
* here delimiters need a special case since
* $ and `...` are not to be treated specially
*/
switch (c) {
case ORD('\\'):
if ((c = getsc())) {
/* trailing \ is lost */
*wp++ = QCHAR;
*wp++ = c;
}
break;
case ORD('\''):
goto open_ssquote_unless_heredoc;
case ORD('$'):
if ((unsigned int)(c2 = getsc()) == ORD('\'')) {
open_sequote:
*wp++ = OQUOTE;
ignore_backslash_newline++;
PUSH_STATE(SEQUOTE);
statep->ls_flag = Nee;
break;
} else if ((unsigned int)c2 == ORD('"')) {
/* FALLTHROUGH */
case ORD('"'):
PUSH_SRETRACE(SHEREDQUOTE);
break;
}
ungetsc(c2);
/* FALLTHROUGH */
default:
*wp++ = CHAR;
*wp++ = c;
}
break;
/* " in << or <<- delimiter */
case SHEREDQUOTE:
if ((unsigned int)c != ORD('"'))
goto Subst;
POP_SRETRACE();
dp = strnul(sp) - 1;
/* remove the trailing double quote */
*dp = '\0';
/* store the quoted string */
*wp++ = OQUOTE;
XcheckN(ws, wp, (dp - sp) * 2);
dp = sp;
while ((c = *dp++)) {
if (c == '\\') {
switch ((c = *dp++)) {
case ORD('\\'):
case ORD('"'):
case ORD('$'):
case ORD('`'):
break;
default:
*wp++ = CHAR;
*wp++ = '\\';
break;
}
}
*wp++ = CHAR;
*wp++ = c;
}
afree(sp, ATEMP);
*wp++ = CQUOTE;
state = statep->type = SHEREDELIM;
break;
/* in *(...|...) pattern (*+?@!) */
case SPATTERN:
if ((unsigned int)c == ORD(/*(*/ ')')) {
*wp++ = CPAT;
POP_STATE();
} else if ((unsigned int)c == ORD('|')) {
*wp++ = SPAT;
} else if ((unsigned int)c == ORD('(')) {
*wp++ = OPAT;
/* simile for @ */
*wp++ = ' ';
PUSH_STATE(SPATTERN);
} else
goto Sbase1;
break;
}
}
Done:
Xcheck(ws, wp);
if (statep != &states[1])
/* XXX figure out what is missing */
yyerror("no closing quote");
/* This done to avoid tests for SHEREDELIM wherever SBASE tested */
if (state == SHEREDELIM)
state = SBASE;
dp = Xstring(ws, wp);
if (state == SBASE && (
(c == '&' && !Flag(FSH) && !Flag(FPOSIX)) ||
ctype(c, C_ANGLE)) && ((c2 = Xlength(ws, wp)) == 0 ||
(c2 == 2 && dp[0] == CHAR && ctype(dp[1], C_DIGIT)))) {
struct ioword *iop = alloc(sizeof(struct ioword), ATEMP);
iop->unit = c2 == 2 ? ksh_numdig(dp[1]) : c == '<' ? 0 : 1;
if (c == '&') {
if ((unsigned int)(c2 = getsc()) != ORD('>')) {
ungetsc(c2);
goto no_iop;
}
c = c2;
iop->ioflag = IOBASH;
} else
iop->ioflag = 0;
c2 = getsc();
/* <<, >>, <> are ok, >< is not */
if (c == c2 || ((unsigned int)c == ORD('<') &&
(unsigned int)c2 == ORD('>'))) {
iop->ioflag |= c == c2 ?
((unsigned int)c == ORD('>') ? IOCAT : IOHERE) : IORDWR;
if (iop->ioflag == IOHERE) {
if ((unsigned int)(c2 = getsc()) == ORD('-'))
iop->ioflag |= IOSKIP;
else if ((unsigned int)c2 == ORD('<'))
iop->ioflag |= IOHERESTR;
else
ungetsc(c2);
}
} else if ((unsigned int)c2 == ORD('&'))
iop->ioflag |= IODUP | ((unsigned int)c == ORD('<') ? IORDUP : 0);
else {
iop->ioflag |= (unsigned int)c == ORD('>') ? IOWRITE : IOREAD;
if ((unsigned int)c == ORD('>') && (unsigned int)c2 == ORD('|'))
iop->ioflag |= IOCLOB;
else
ungetsc(c2);
}
iop->ioname = NULL;
iop->delim = NULL;
iop->heredoc = NULL;
/* free word */
Xfree(ws, wp);
yylval.iop = iop;
return (REDIR);
no_iop:
afree(iop, ATEMP);
}
if (wp == dp && state == SBASE) {
/* free word */
Xfree(ws, wp);
/* no word, process LEX1 character */
if (((unsigned int)c == ORD('|')) ||
((unsigned int)c == ORD('&')) ||
((unsigned int)c == ORD(';')) ||
((unsigned int)c == ORD('(' /*)*/))) {
if ((c2 = getsc()) == c)
c = ((unsigned int)c == ORD(';')) ? BREAK :
((unsigned int)c == ORD('|')) ? LOGOR :
((unsigned int)c == ORD('&')) ? LOGAND :
/* (unsigned int)c == ORD('(' )) */ MDPAREN;
else if ((unsigned int)c == ORD('|') && (unsigned int)c2 == ORD('&'))
c = COPROC;
else if ((unsigned int)c == ORD(';') && (unsigned int)c2 == ORD('|'))
c = BRKEV;
else if ((unsigned int)c == ORD(';') && (unsigned int)c2 == ORD('&'))
c = BRKFT;
else
ungetsc(c2);
#ifndef MKSH_SMALL
if (c == BREAK) {
if ((unsigned int)(c2 = getsc()) == ORD('&'))
c = BRKEV;
else
ungetsc(c2);
}
#endif
} else if ((unsigned int)c == ORD('\n')) {
if (cf & HEREDELIM)
ungetsc(c);
else {