-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathparser.c
4141 lines (3745 loc) · 130 KB
/
parser.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
/* Yash: yet another shell */
/* parser.c: syntax parser */
/* (C) 2007-2025 magicant */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "common.h"
#include "parser.h"
#include <assert.h>
#include <errno.h>
#if HAVE_GETTEXT
# include <libintl.h>
#endif
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
#include "alias.h"
#include "builtin.h"
#include "expand.h"
#include "input.h"
#include "option.h"
#include "plist.h"
#include "strbuf.h"
#include "util.h"
#if YASH_ENABLE_DOUBLE_BRACKET
# include "builtins/test.h"
#endif
#if YASH_ENABLE_LINEEDIT
# include "lineedit/lineedit.h"
#endif
/********** Functions That Free Parse Trees **********/
static void pipesfree(pipeline_T *p);
static void ifcmdsfree(ifcommand_T *i);
static void caseitemsfree(caseitem_T *i);
#if YASH_ENABLE_DOUBLE_BRACKET
static void dbexpfree(dbexp_T *e);
#endif
static void wordunitfree(wordunit_T *wu)
__attribute__((nonnull));
static void wordfree_vp(void *w);
static void assignsfree(assign_T *a);
static void redirsfree(redir_T *r);
static void embedcmdfree(embedcmd_T c);
void andorsfree(and_or_T *a)
{
while (a != NULL) {
pipesfree(a->ao_pipelines);
and_or_T *next = a->next;
free(a);
a = next;
}
}
void pipesfree(pipeline_T *p)
{
while (p != NULL) {
comsfree(p->pl_commands);
pipeline_T *next = p->next;
free(p);
p = next;
}
}
void comsfree(command_T *c)
{
while (c != NULL) {
if (!refcount_decrement(&c->refcount))
break;
redirsfree(c->c_redirs);
switch (c->c_type) {
case CT_SIMPLE:
assignsfree(c->c_assigns);
plfree(c->c_words, wordfree_vp);
break;
case CT_GROUP:
case CT_SUBSHELL:
andorsfree(c->c_subcmds);
break;
case CT_IF:
ifcmdsfree(c->c_ifcmds);
break;
case CT_FOR:
free(c->c_forname);
plfree(c->c_forwords, wordfree_vp);
andorsfree(c->c_forcmds);
break;
case CT_WHILE:
andorsfree(c->c_whlcond);
andorsfree(c->c_whlcmds);
break;
case CT_CASE:
wordfree(c->c_casword);
caseitemsfree(c->c_casitems);
break;
#if YASH_ENABLE_DOUBLE_BRACKET
case CT_BRACKET:
dbexpfree(c->c_dbexp);
break;
#endif /* YASH_ENABLE_DOUBLE_BRACKET */
case CT_FUNCDEF:
wordfree(c->c_funcname);
comsfree(c->c_funcbody);
break;
}
command_T *next = c->next;
free(c);
c = next;
}
}
void ifcmdsfree(ifcommand_T *i)
{
while (i != NULL) {
andorsfree(i->ic_condition);
andorsfree(i->ic_commands);
ifcommand_T *next = i->next;
free(i);
i = next;
}
}
void caseitemsfree(caseitem_T *i)
{
while (i != NULL) {
plfree(i->ci_patterns, wordfree_vp);
andorsfree(i->ci_commands);
caseitem_T *next = i->next;
free(i);
i = next;
}
}
#if YASH_ENABLE_DOUBLE_BRACKET
void dbexpfree(dbexp_T *e)
{
if (e == NULL)
return;
free(e->operator);
switch (e->type) {
case DBE_OR:
case DBE_AND:
case DBE_NOT:
dbexpfree(e->lhs.subexp);
dbexpfree(e->rhs.subexp);
break;
case DBE_UNARY:
case DBE_BINARY:
case DBE_STRING:
wordfree(e->lhs.word);
wordfree(e->rhs.word);
break;
}
free(e);
}
#endif /* YASH_ENABLE_DOUBLE_BRACKET */
void wordunitfree(wordunit_T *wu)
{
switch (wu->wu_type) {
case WT_STRING:
free(wu->wu_string);
break;
case WT_PARAM:
paramfree(wu->wu_param);
break;
case WT_CMDSUB:
embedcmdfree(wu->wu_cmdsub);
break;
case WT_ARITH:
wordfree(wu->wu_arith);
break;
}
free(wu);
}
void wordfree(wordunit_T *w)
{
while (w != NULL) {
wordunit_T *next = w->next;
wordunitfree(w);
w = next;
}
}
void wordfree_vp(void *w)
{
wordfree((wordunit_T *) w);
}
void paramfree(paramexp_T *p)
{
if (p != NULL) {
if (p->pe_type & PT_NEST)
wordfree(p->pe_nest);
else
free(p->pe_name);
wordfree(p->pe_start);
wordfree(p->pe_end);
wordfree(p->pe_match);
wordfree(p->pe_subst);
free(p);
}
}
void assignsfree(assign_T *a)
{
while (a != NULL) {
free(a->a_name);
switch (a->a_type) {
case A_SCALAR:
wordfree(a->a_scalar);
break;
case A_ARRAY:
plfree(a->a_array, wordfree_vp);
break;
}
assign_T *next = a->next;
free(a);
a = next;
}
}
void redirsfree(redir_T *r)
{
while (r != NULL) {
switch (r->rd_type) {
case RT_INPUT: case RT_OUTPUT: case RT_CLOBBER: case RT_APPEND:
case RT_INOUT: case RT_DUPIN: case RT_DUPOUT: case RT_PIPE:
case RT_HERESTR:
wordfree(r->rd_filename);
break;
case RT_HERE: case RT_HERERT:
free(r->rd_hereend);
wordfree(r->rd_herecontent);
break;
case RT_PROCIN: case RT_PROCOUT:
embedcmdfree(r->rd_command);
break;
}
redir_T *next = r->next;
free(r);
r = next;
}
}
void embedcmdfree(embedcmd_T c)
{
if (c.is_preparsed)
andorsfree(c.value.preparsed);
else
free(c.value.unparsed);
}
/********** Auxiliary Functions for Parser **********/
typedef enum tokentype_T {
TT_UNKNOWN,
TT_END_OF_INPUT,
TT_WORD,
TT_IO_NUMBER,
/* operators */
TT_NEWLINE,
TT_AMP, TT_AMPAMP, TT_LPAREN, TT_RPAREN, TT_SEMICOLON, TT_DOUBLE_SEMICOLON,
TT_SEMICOLONAMP, TT_SEMICOLONPIPE, TT_DOUBLE_SEMICOLON_AMP,
TT_PIPE, TT_PIPEPIPE, TT_LESS, TT_LESSLESS, TT_LESSAMP, TT_LESSLESSDASH,
TT_LESSLESSLESS, TT_LESSGREATER, TT_LESSLPAREN, TT_GREATER,
TT_GREATERGREATER, TT_GREATERGREATERPIPE, TT_GREATERPIPE, TT_GREATERAMP,
TT_GREATERLPAREN,
/* reserved words */
TT_IF, TT_THEN, TT_ELSE, TT_ELIF, TT_FI, TT_DO, TT_DONE, TT_CASE, TT_ESAC,
TT_WHILE, TT_UNTIL, TT_FOR, TT_LBRACE, TT_RBRACE, TT_BANG, TT_IN,
TT_FUNCTION,
#if YASH_ENABLE_DOUBLE_BRACKET
TT_DOUBLE_LBRACKET,
#endif
} tokentype_T;
static wchar_t *skip_name(const wchar_t *s, bool predicate(wchar_t))
__attribute__((pure,nonnull));
static bool is_name_by_predicate(const wchar_t *s, bool predicate(wchar_t))
__attribute__((pure,nonnull));
static bool is_portable_name(const wchar_t *s)
__attribute__((pure,nonnull));
static tokentype_T identify_reserved_word_string(const wchar_t *s)
__attribute__((pure,nonnull));
static bool is_single_string_word(const wordunit_T *wu)
__attribute__((pure));
static bool is_digits_only(const wordunit_T *wu)
__attribute__((pure));
static bool is_name_word(const wordunit_T *wu)
__attribute__((pure));
static tokentype_T identify_reserved_word(const wordunit_T *wu)
__attribute__((pure));
static bool is_closing_tokentype(tokentype_T tt)
__attribute__((const));
/* Checks if the specified character can be used in a portable variable name.
* Returns true for a digit. */
bool is_portable_name_char(wchar_t c)
{
switch (c) {
case L'0': case L'1': case L'2': case L'3': case L'4':
case L'5': case L'6': case L'7': case L'8': case L'9':
case L'a': case L'b': case L'c': case L'd': case L'e': case L'f':
case L'g': case L'h': case L'i': case L'j': case L'k': case L'l':
case L'm': case L'n': case L'o': case L'p': case L'q': case L'r':
case L's': case L't': case L'u': case L'v': case L'w': case L'x':
case L'y': case L'z':
case L'A': case L'B': case L'C': case L'D': case L'E': case L'F':
case L'G': case L'H': case L'I': case L'J': case L'K': case L'L':
case L'M': case L'N': case L'O': case L'P': case L'Q': case L'R':
case L'S': case L'T': case L'U': case L'V': case L'W': case L'X':
case L'Y': case L'Z': case L'_':
return true;
default:
return false;
}
}
/* Checks if the specified character can be used in a variable name.
* Returns true for a digit. */
bool is_name_char(wchar_t c)
{
return c == L'_' || iswalnum(c);
}
/* Skips an identifier at the head of the specified string and returns a
* pointer to the character right after the identifier in the string.
* If there is no identifier, the argument `s' is simply returned. `predicate`
* determines if a character is valid. */
wchar_t *skip_name(const wchar_t *s, bool predicate(wchar_t))
{
if (!iswdigit(*s))
while (predicate(*s))
s++;
return (wchar_t *) s;
}
/* Returns true iff the specified string constitutes a valid identifier that
* is made up of characters accepted by `predicate'. */
bool is_name_by_predicate(const wchar_t *s, bool predicate(wchar_t))
{
return s[0] != L'\0' && skip_name(s, predicate)[0] == L'\0';
}
/* Returns true iff the specified string constitutes a valid portable name. */
bool is_portable_name(const wchar_t *s)
{
return is_name_by_predicate(s, is_portable_name_char);
}
/* Returns true iff the specified string constitutes a valid identifier. */
bool is_name(const wchar_t *s)
{
return is_name_by_predicate(s, is_name_char);
}
/* Returns true iff the specified string is a prefix (leading substring) of an
* assignment token. */
bool is_assignment_prefix(const wchar_t *s)
{
return s[0] != L'\0' && skip_name(s, is_name_char)[0] == L'=';
}
/* Converts a string to the corresponding token type. Returns TT_WORD for
* non-reserved words. */
tokentype_T identify_reserved_word_string(const wchar_t *s)
{
/* List of keywords:
* case do done elif else esac fi for function if in then until while
* { } [[ !
* The following words are currently not keywords:
* select ]] */
switch (s[0]) {
case L'c':
if (s[1] == L'a' && s[2] == L's' && s[3] == L'e' && s[4]== L'\0')
return TT_CASE;
break;
case L'd':
if (s[1] == L'o') {
if (s[2] == L'\0')
return TT_DO;
if (s[2] == L'n' && s[3] == L'e' && s[4] == L'\0')
return TT_DONE;
}
break;
case L'e':
if (s[1] == L'l') {
if (s[2] == L's' && s[3] == L'e' && s[4] == L'\0')
return TT_ELSE;
if (s[2] == L'i' && s[3] == L'f' && s[4] == L'\0')
return TT_ELIF;
}
if (s[1] == L's' && s[2] == L'a' && s[3] == L'c' && s[4] == L'\0')
return TT_ESAC;
break;
case L'f':
if (s[1] == L'i' && s[2] == L'\0')
return TT_FI;
if (s[1] == L'o' && s[2] == L'r' && s[3] == L'\0')
return TT_FOR;
if (s[1] == L'u' && s[2] == L'n' && s[3] == L'c' && s[4] == L't' &&
s[5] == L'i' && s[6] == L'o' && s[7] == L'n' &&
s[8] == L'\0')
return TT_FUNCTION;
break;
case L'i':
if (s[1] == L'f' && s[2] == L'\0')
return TT_IF;
if (s[1] == L'n' && s[2] == L'\0')
return TT_IN;
break;
case L't':
if (s[1] == L'h' && s[2] == L'e' && s[3] == L'n' && s[4]== L'\0')
return TT_THEN;
break;
case L'u':
if (s[1] == L'n' && s[2] == L't' && s[3] == L'i' && s[4] == L'l' &&
s[5] == L'\0')
return TT_UNTIL;
break;
case L'w':
if (s[1] == L'h' && s[2] == L'i' && s[3] == L'l' && s[4] == L'e' &&
s[5] == L'\0')
return TT_WHILE;
break;
case L'{':
if (s[1] == L'\0')
return TT_LBRACE;
break;
case L'}':
if (s[1] == L'\0')
return TT_RBRACE;
break;
#if YASH_ENABLE_DOUBLE_BRACKET
case L'[':
if (s[1] == L'[' && s[2] == L'\0')
return TT_DOUBLE_LBRACKET;
break;
#endif /* YASH_ENABLE_DOUBLE_BRACKET */
case L'!':
if (s[1] == L'\0')
return TT_BANG;
break;
}
return TT_WORD;
}
/* Returns true iff the string is a reserved word. */
bool is_keyword(const wchar_t *s)
{
return identify_reserved_word_string(s) != TT_WORD;
}
bool is_single_string_word(const wordunit_T *wu)
{
return wu != NULL && wu->next == NULL && wu->wu_type == WT_STRING;
}
/* Tests if a word is made up of digits only. */
bool is_digits_only(const wordunit_T *wu)
{
if (!is_single_string_word(wu))
return false;
const wchar_t *s = wu->wu_string;
assert(s[0] != L'\0');
while (iswdigit(*s))
s++;
return *s == L'\0';
}
bool is_name_word(const wordunit_T *wu)
{
if (!is_single_string_word(wu))
return false;
return (posixly_correct ? is_portable_name : is_name)(wu->wu_string);
}
/* Converts a word to the corresponding token type. Returns TT_WORD for
* non-reserved words. */
tokentype_T identify_reserved_word(const wordunit_T *wu)
{
if (!is_single_string_word(wu))
return TT_WORD;
return identify_reserved_word_string(wu->wu_string);
}
/* Determines if the specified token is a 'closing' token such as ")", "}", and
* "fi". Closing tokens delimit and/or lists. */
bool is_closing_tokentype(tokentype_T tt)
{
switch (tt) {
case TT_RPAREN:
case TT_RBRACE:
case TT_THEN:
case TT_ELIF:
case TT_ELSE:
case TT_FI:
case TT_DO:
case TT_DONE:
case TT_DOUBLE_SEMICOLON:
case TT_DOUBLE_SEMICOLON_AMP:
case TT_SEMICOLONAMP:
case TT_SEMICOLONPIPE:
case TT_ESAC:
return true;
default:
return false;
}
}
/********** Parser **********/
/* Holds data that are used in parsing. */
typedef struct parsestate_T {
/* contains parameters that affect the behavior of parsing */
parseparam_T *info;
/* true iff any parsing error occurred */
bool error;
/* the source code being parsed */
struct xwcsbuf_T src;
/* the position of the current character or `token' */
size_t index;
/* the index just past the current `token' */
size_t next_index;
/* type of the current `token' */
tokentype_T tokentype;
/* the current token (NULL when `tokentype' is an operator token) */
wordunit_T *token;
/* here-documents whose contents have not been read */
struct plist_T pending_heredocs;
/* false when alias substitution is suppressed */
bool enable_alias;
/* true when non-global alias substitution has occurred */
bool reparse;
/* record of alias substitutions that are responsible for the current
* `index' */
struct aliaslist_T *aliases;
} parsestate_T;
static void serror(parsestate_T *restrict ps, const char *restrict format, ...)
__attribute__((nonnull(1,2),format(printf,2,3)));
static void print_errmsg_token(parsestate_T *ps, const char *message)
__attribute__((nonnull));
static const char *get_errmsg_unexpected_tokentype(tokentype_T tokentype)
__attribute__((const));
static void print_errmsg_token_unexpected(parsestate_T *ps)
__attribute__((nonnull));
static void print_errmsg_token_missing(parsestate_T *ps, const wchar_t *t)
__attribute__((nonnull));
static inputresult_T read_more_input(parsestate_T *ps)
__attribute__((nonnull));
static void line_continuation(parsestate_T *ps, size_t index)
__attribute__((nonnull));
static void maybe_line_continuations(parsestate_T *ps, size_t index)
__attribute__((nonnull));
static void rewind_index(parsestate_T *ps, size_t to)
__attribute__((nonnull));
static size_t count_name_length(parsestate_T *ps, bool isnamechar(wchar_t c))
__attribute__((nonnull));
static void next_token(parsestate_T *ps)
__attribute__((nonnull));
static wordunit_T *parse_word(parsestate_T *ps, bool testfunc(wchar_t c))
__attribute__((nonnull,malloc,warn_unused_result));
static void skip_to_next_single_quote(parsestate_T *ps, bool allowescape)
__attribute__((nonnull));
static wordunit_T *parse_special_word_unit(parsestate_T *ps, bool indq)
__attribute__((nonnull,malloc,warn_unused_result));
static wordunit_T *tryparse_paramexp_raw(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static wordunit_T *parse_paramexp_in_brace(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static wordunit_T *parse_cmdsubst_in_paren(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static embedcmd_T extract_command_in_paren(parsestate_T *ps)
__attribute__((nonnull,warn_unused_result));
static wchar_t *extract_command_in_paren_unparsed(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static wordunit_T *parse_cmdsubst_in_backquote(parsestate_T *ps, bool bsdq)
__attribute__((nonnull,malloc,warn_unused_result));
static wordunit_T *tryparse_arith(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static void next_line(parsestate_T *ps)
__attribute__((nonnull));
static bool parse_newline_list(parsestate_T *ps)
__attribute__((nonnull));
static bool is_comma_or_closing_bracket(wchar_t c)
__attribute__((const));
static bool is_slash_or_closing_brace(wchar_t c)
__attribute__((const));
static bool is_closing_brace(wchar_t c)
__attribute__((const));
static bool psubstitute_alias(parsestate_T *ps, substaliasflags_T f)
__attribute__((nonnull));
static void psubstitute_alias_recursive(parsestate_T *ps, substaliasflags_T f)
__attribute__((nonnull));
static and_or_T *parse_command_list(parsestate_T *ps, bool toeol)
__attribute__((nonnull,malloc,warn_unused_result));
static and_or_T *parse_compound_list(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static and_or_T *parse_and_or_list(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static pipeline_T *parse_pipelines_in_and_or(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static pipeline_T *parse_pipeline(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static command_T *parse_commands_in_pipeline(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static command_T *parse_command(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static void **parse_simple_command_tokens(
parsestate_T *ps, assign_T **assigns, redir_T **redirs)
__attribute__((nonnull,malloc,warn_unused_result));
static void **parse_words(parsestate_T *ps, bool skip_newlines)
__attribute__((nonnull,malloc,warn_unused_result));
static void parse_redirect_list(parsestate_T *ps, redir_T **lastp)
__attribute__((nonnull));
static assign_T *tryparse_assignment(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static redir_T *tryparse_redirect(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static void validate_redir_operand(parsestate_T *ps)
__attribute__((nonnull));
static bool is_declaration_utility(void *const *words)
__attribute__((nonnull,pure,warn_unused_result));
static command_T *parse_compound_command(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static command_T *parse_group(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static command_T *parse_if(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static command_T *parse_for(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static command_T *parse_while(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static command_T *parse_case(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static caseitem_T *parse_case_list(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static void **parse_case_patterns(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
#if YASH_ENABLE_DOUBLE_BRACKET
static command_T *parse_double_bracket(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static dbexp_T *parse_double_bracket_ors(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static dbexp_T *parse_double_bracket_ands(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static dbexp_T *parse_double_bracket_nots(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static dbexp_T *parse_double_bracket_primary(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static wordunit_T *parse_double_bracket_operand(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static wordunit_T *parse_double_bracket_operand_regex(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
#endif /* YASH_ENABLE_DOUBLE_BRACKET */
static command_T *parse_function(parsestate_T *ps)
__attribute__((nonnull,malloc,warn_unused_result));
static command_T *try_reparse_as_function(parsestate_T *ps, command_T *c)
__attribute__((nonnull,warn_unused_result));
static void read_heredoc_contents(parsestate_T *ps, redir_T *redir)
__attribute__((nonnull));
static void read_heredoc_contents_without_expansion(
parsestate_T *ps, redir_T *r)
__attribute__((nonnull));
static void read_heredoc_contents_with_expansion(parsestate_T *ps, redir_T *r)
__attribute__((nonnull));
static bool is_end_of_heredoc_contents(
parsestate_T *ps, const wchar_t *eoc, bool skiptab)
__attribute__((nonnull));
static void reject_pending_heredocs(parsestate_T *ps)
__attribute__((nonnull));
static wordunit_T **parse_string_without_quotes(
parsestate_T *ps, bool backquote, bool stoponnewline,
wordunit_T **lastp)
__attribute__((nonnull));
#define QUOTES L"\"'\\"
/***** Entry points *****/
/* The functions below may return non-NULL even on error.
* The error condition must be tested by the `error' flag of the parsestate_T
* structure. It is set to true when `serror' is called. */
/* Every function named `parse_*' advances the current position (the `index'
* value of the parsestate_T structure) to the index of the first character
* that has not yet been parsed. Syntax parser functions also update the
* current `token' and `tokentype' to the first unconsumed token, in which
* case `index' points to the first character of the `token'. */
/* The main entry point to the parser.
* This function reads at least one line of input and parses it.
* All the members of `info' except `lastinputresult' must have been initialized
* beforehand.
* The resulting parse tree is assigned to `*resultp' if successful. If there is
* no command in the next line or the shell was interrupted while reading input,
* `*resultp' is assigned NULL.
* Returns PR_OK if successful,
* PR_SYNTAX_ERROR if a syntax error occurred,
* PR_INPUT_ERROR if an input error occurred, or
* PR_EOF if the input reached the end of file (EOF).
* If PR_SYNTAX_ERROR or PR_INPUT_ERROR is returned, at least one error message
* has been printed in this function.
* Note that `*resultp' is assigned if and only if the return value is PR_OK. */
parseresult_T read_and_parse(parseparam_T *info, and_or_T **restrict resultp)
{
parsestate_T ps = {
.info = info,
.error = false,
.index = 0,
.next_index = 0,
.tokentype = TT_UNKNOWN,
.token = NULL,
.enable_alias = info->enable_alias,
.reparse = false,
.aliases = NULL,
};
if (ps.info->interactive) {
struct input_interactive_info_T *intrinfo = ps.info->inputinfo;
intrinfo->prompttype = 1;
}
ps.info->lastinputresult = INPUT_OK;
wb_init(&ps.src);
pl_init(&ps.pending_heredocs);
and_or_T *r = parse_command_list(&ps, true);
reject_pending_heredocs(&ps);
size_t length = ps.src.length;
wb_destroy(&ps.src);
pl_destroy(&ps.pending_heredocs);
destroy_aliaslist(ps.aliases);
wordfree(ps.token);
switch (ps.info->lastinputresult) {
case INPUT_OK:
case INPUT_EOF:
if (ps.error) {
andorsfree(r);
return PR_SYNTAX_ERROR;
} else if (length == 0) {
andorsfree(r);
return PR_EOF;
} else {
assert(ps.index == length);
*resultp = r;
return PR_OK;
}
case INPUT_INTERRUPTED:
andorsfree(r);
*resultp = NULL;
return PR_OK;
case INPUT_ERROR:
andorsfree(r);
return PR_INPUT_ERROR;
}
assert(false);
}
/* Parses a string recognizing parameter expansions, command substitutions of
* the form "$(...)" and arithmetic expansions.
* All the members of `info' except `lastinputresult' must have been initialized
* beforehand.
* This function reads and parses the input to the end of file.
* Iff successful, the result is assigned to `*resultp' and true is returned.
* If the input is empty, NULL is assigned.
* On error, the value of `*resultp' is undefined. */
bool parse_string(parseparam_T *info, wordunit_T **restrict resultp)
{
parsestate_T ps = {
.info = info,
.error = false,
.index = 0,
.next_index = 0,
.tokentype = TT_UNKNOWN,
.token = NULL,
.enable_alias = false,
.reparse = false,
.aliases = NULL,
};
wb_init(&ps.src);
ps.info->lastinputresult = INPUT_OK;
read_more_input(&ps);
pl_init(&ps.pending_heredocs);
resultp = parse_string_without_quotes(&ps, false, false, resultp);
*resultp = NULL;
wb_destroy(&ps.src);
pl_destroy(&ps.pending_heredocs);
assert(ps.aliases == NULL);
//destroy_aliaslist(ps.aliases);
wordfree(ps.token);
if (ps.info->lastinputresult != INPUT_EOF || ps.error) {
wordfree(*resultp);
return false;
} else {
return true;
}
}
/***** Error message utility *****/
/* Prints the specified error message to the standard error.
* `format' is passed to `gettext' in this function.
* `format' need not to have a trailing newline since a newline is automatically
* appended in this function.
* The `ps->error' flag is set to true in this function. */
void serror(parsestate_T *restrict ps, const char *restrict format, ...)
{
va_list ap;
if (ps->info->print_errmsg &&
ps->info->lastinputresult != INPUT_INTERRUPTED) {
if (ps->info->filename != NULL)
fprintf(stderr, "%s:%lu: ", ps->info->filename, ps->info->lineno);
fprintf(stderr, gt("syntax error: "));
va_start(ap, format);
vfprintf(stderr, gt(format), ap);
va_end(ap);
fputc('\n', stderr);
fflush(stderr);
}
ps->error = true;
}
void print_errmsg_token(parsestate_T *ps, const char *message)
{
assert(ps->index <= ps->next_index);
assert(ps->next_index <= ps->src.length);
size_t length = ps->next_index - ps->index;
wchar_t token[length + 1];
wcsncpy(token, &ps->src.contents[ps->index], length);
token[length] = L'\0';
serror(ps, message, token);
}
const char *get_errmsg_unexpected_tokentype(tokentype_T tokentype)
{
switch (tokentype) {
case TT_RPAREN:
return Ngt("encountered `%ls' without a matching `('");
case TT_RBRACE:
return Ngt("encountered `%ls' without a matching `{'");
case TT_DOUBLE_SEMICOLON:
case TT_DOUBLE_SEMICOLON_AMP:
case TT_SEMICOLONAMP:
case TT_SEMICOLONPIPE:
return Ngt("`%ls' is used outside `case'");
case TT_BANG:
return Ngt("`%ls' cannot be used as a command name");
case TT_IN:
return Ngt("`%ls' cannot be used as a command name");
case TT_FI:
return Ngt("encountered `%ls' "
"without a matching `if' and/or `then'");
case TT_THEN:
return Ngt("encountered `%ls' without a matching `if' or `elif'");
case TT_DO:
return Ngt("encountered `%ls' "
"without a matching `for', `while', or `until'");
case TT_DONE:
return Ngt("encountered `%ls' without a matching `do'");
case TT_ESAC:
return Ngt("encountered `%ls' without a matching `case'");
case TT_ELIF:
case TT_ELSE:
return Ngt("encountered `%ls' "
"without a matching `if' and/or `then'");
default:
assert(false);
}
}
void print_errmsg_token_unexpected(parsestate_T *ps)
{
print_errmsg_token(ps, get_errmsg_unexpected_tokentype(ps->tokentype));
}
void print_errmsg_token_missing(parsestate_T *ps, const wchar_t *t)
{
if (is_closing_tokentype(ps->tokentype)) {
print_errmsg_token_unexpected(ps);
serror(ps, Ngt("(maybe you missed `%ls'?)"), t);
} else {
serror(ps, Ngt("`%ls' is missing"), t);
}
}
/***** Input buffer manipulators *****/
/* Reads the next line of input and returns the result type, which is assigned
* to `ps->info->lastinputresult'.
* If `ps->info->lastinputresult' is not INPUT_OK, it is simply returned
* without reading any input.
* If input is from an interactive terminal and `ps->error' is true, no input
* is read and INPUT_INTERRUPTED is returned. */
inputresult_T read_more_input(parsestate_T *ps)
{
if (ps->error && ps->info->interactive)
return INPUT_INTERRUPTED;
if (ps->info->lastinputresult == INPUT_OK) {
size_t savelength = ps->src.length;
ps->info->lastinputresult =
ps->info->input(&ps->src, ps->info->inputinfo);
if (ps->info->enable_verbose && shopt_verbose)
#if YASH_ENABLE_LINEEDIT
if (!(le_state & LE_STATE_ACTIVE))
#endif
fprintf(stderr, "%ls", &ps->src.contents[savelength]);
}
return ps->info->lastinputresult;
}
/* Removes a line continuation at the specified index in `ps->src', increments
* line number, and reads the next line. */
void line_continuation(parsestate_T *ps, size_t index)
{
assert(ps->src.contents[index] == L'\\' &&
ps->src.contents[index + 1] == L'\n');
wb_remove(&ps->src, index, 2);
shift_aliaslist_index(ps->aliases, index + 1, -2);
ps->info->lineno++;
if (ps->src.contents[index] == L'\0')
read_more_input(ps);
}
/* Removes line continuations at the specified index.
* The next line will be read if the removed line continuation is at the end of
* the buffer. */
void maybe_line_continuations(parsestate_T *ps, size_t index)
{
assert(index <= ps->src.length);
if (index == ps->src.length)
read_more_input(ps);
while (ps->src.contents[index] == L'\\' &&
ps->src.contents[index + 1] == L'\n')
line_continuation(ps, index);
}
/* Rewind `ps->index` to `oldindex' and decrease `ps->info->lineno' accordingly.
* Note that `ps->next_index' is not updated in this function.
*
* You MUST use this function when rewinding the index in order to correctly
* rewind the line number. The following pattern of code does not work because
* it does not account for line continuations that have been removed from
* `ps->src'.