-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.y
6513 lines (5768 loc) · 178 KB
/
parse.y
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
/* parse.y - Yacc grammar for bash. */
/* Copyright (C) 1989-2015 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash 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 3 of the License, or
(at your option) any later version.
Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
*/
%{
#include "config.h"
#include "bashtypes.h"
#include "bashansi.h"
#include "filecntl.h"
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#if defined (HAVE_LOCALE_H)
# include <locale.h>
#endif
#include <stdio.h>
#include "chartypes.h"
#include <signal.h>
#include "memalloc.h"
#include "bashintl.h"
#define NEED_STRFTIME_DECL /* used in externs.h */
#include "shell.h"
#include "typemax.h" /* SIZE_MAX if needed */
#include "trap.h"
#include "flags.h"
#include "parser.h"
#include "mailcheck.h"
#include "test.h"
#include "builtins.h"
#include "builtins/common.h"
#include "builtins/builtext.h"
#include "shmbutil.h"
#if defined (READLINE)
# include "bashline.h"
# include <readline/readline.h>
#endif /* READLINE */
#if defined (HISTORY)
# include "bashhist.h"
# include <readline/history.h>
#endif /* HISTORY */
#if defined (JOB_CONTROL)
# include "jobs.h"
#else
extern int cleanup_dead_jobs __P((void));
#endif /* JOB_CONTROL */
#if defined (ALIAS)
# include "alias.h"
#else
typedef void *alias_t;
#endif /* ALIAS */
#if defined (PROMPT_STRING_DECODE)
# ifndef _MINIX
# include <sys/param.h>
# endif
# include <time.h>
# if defined (TM_IN_SYS_TIME)
# include <sys/types.h>
# include <sys/time.h>
# endif /* TM_IN_SYS_TIME */
# include "maxpath.h"
#endif /* PROMPT_STRING_DECODE */
#define RE_READ_TOKEN -99
#define NO_EXPANSION -100
#ifdef DEBUG
# define YYDEBUG 1
#else
# define YYDEBUG 0
#endif
#if defined (HANDLE_MULTIBYTE)
# define last_shell_getc_is_singlebyte \
((shell_input_line_index > 1) \
? shell_input_line_property[shell_input_line_index - 1] \
: 1)
# define MBTEST(x) ((x) && last_shell_getc_is_singlebyte)
#else
# define last_shell_getc_is_singlebyte 1
# define MBTEST(x) ((x))
#endif
#if defined (EXTENDED_GLOB)
extern int extended_glob;
#endif
extern int eof_encountered;
extern int no_line_editing, running_under_emacs;
extern int current_command_number;
extern int sourcelevel, parse_and_execute_level;
extern int posixly_correct;
extern int last_command_exit_value;
extern pid_t last_command_subst_pid;
extern char *shell_name, *current_host_name;
extern char *dist_version;
extern int patch_level;
extern int dump_translatable_strings, dump_po_strings;
extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
extern int here_doc_first_line;
#if defined (BUFFERED_INPUT)
extern int bash_input_fd_changed;
#endif
extern int errno;
/* **************************************************************** */
/* */
/* "Forward" declarations */
/* */
/* **************************************************************** */
#ifdef DEBUG
static void debug_parser __P((int));
#endif
static int yy_getc __P((void));
static int yy_ungetc __P((int));
#if defined (READLINE)
static int yy_readline_get __P((void));
static int yy_readline_unget __P((int));
#endif
static int yy_string_get __P((void));
static int yy_string_unget __P((int));
static void rewind_input_string __P((void));
static int yy_stream_get __P((void));
static int yy_stream_unget __P((int));
static int shell_getc __P((int));
static void shell_ungetc __P((int));
static void discard_until __P((int));
#if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
static void push_string __P((char *, int, alias_t *));
static void pop_string __P((void));
static void free_string_list __P((void));
#endif
static char *read_a_line __P((int));
static int reserved_word_acceptable __P((int));
static int yylex __P((void));
static void push_heredoc __P((REDIRECT *));
static char *mk_alexpansion __P((char *));
static int alias_expand_token __P((char *));
static int time_command_acceptable __P((void));
static int special_case_tokens __P((char *));
static int read_token __P((int));
static char *parse_matched_pair __P((int, int, int, int *, int));
static char *parse_comsub __P((int, int, int, int *, int));
#if defined (ARRAY_VARS)
static char *parse_compound_assignment __P((int *));
#endif
#if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
static int parse_dparen __P((int));
static int parse_arith_cmd __P((char **, int));
#endif
#if defined (COND_COMMAND)
static void cond_error __P((void));
static COND_COM *cond_expr __P((void));
static COND_COM *cond_or __P((void));
static COND_COM *cond_and __P((void));
static COND_COM *cond_term __P((void));
static int cond_skip_newlines __P((void));
static COMMAND *parse_cond_command __P((void));
#endif
#if defined (ARRAY_VARS)
static int token_is_assignment __P((char *, int));
static int token_is_ident __P((char *, int));
#endif
static int read_token_word __P((int));
static void discard_parser_constructs __P((int));
static char *error_token_from_token __P((int));
static char *error_token_from_text __P((void));
static void print_offending_line __P((void));
static void report_syntax_error __P((char *));
static void handle_eof_input_unit __P((void));
static void prompt_again __P((void));
#if 0
static void reset_readline_prompt __P((void));
#endif
static void print_prompt __P((void));
#if defined (HANDLE_MULTIBYTE)
static void set_line_mbstate __P((void));
static char *shell_input_line_property = NULL;
#else
# define set_line_mbstate()
#endif
extern int yyerror __P((const char *));
#ifdef DEBUG
extern int yydebug;
#endif
/* Default prompt strings */
char *primary_prompt = PPROMPT;
char *secondary_prompt = SPROMPT;
/* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
char *ps1_prompt, *ps2_prompt;
/* Displayed after reading a command but before executing it in an interactive shell */
char *ps0_prompt;
/* Handle on the current prompt string. Indirectly points through
ps1_ or ps2_prompt. */
char **prompt_string_pointer = (char **)NULL;
char *current_prompt_string;
/* Non-zero means we expand aliases in commands. */
int expand_aliases = 0;
/* If non-zero, the decoded prompt string undergoes parameter and
variable substitution, command substitution, arithmetic substitution,
string expansion, process substitution, and quote removal in
decode_prompt_string. */
int promptvars = 1;
/* If non-zero, $'...' and $"..." are expanded when they appear within
a ${...} expansion, even when the expansion appears within double
quotes. */
int extended_quote = 1;
/* The number of lines read from input while creating the current command. */
int current_command_line_count;
/* The number of lines in a command saved while we run parse_and_execute */
int saved_command_line_count;
/* The token that currently denotes the end of parse. */
int shell_eof_token;
/* The token currently being read. */
int current_token;
/* The current parser state. */
int parser_state;
/* Variables to manage the task of reading here documents, because we need to
defer the reading until after a complete command has been collected. */
static REDIRECT *redir_stack[HEREDOC_MAX];
int need_here_doc;
/* Where shell input comes from. History expansion is performed on each
line when the shell is interactive. */
static char *shell_input_line = (char *)NULL;
static size_t shell_input_line_index;
static size_t shell_input_line_size; /* Amount allocated for shell_input_line. */
static size_t shell_input_line_len; /* strlen (shell_input_line) */
/* Either zero or EOF. */
static int shell_input_line_terminator;
/* The line number in a script on which a function definition starts. */
static int function_dstart;
/* The line number in a script on which a function body starts. */
static int function_bstart;
/* The line number in a script at which an arithmetic for command starts. */
static int arith_for_lineno;
/* The decoded prompt string. Used if READLINE is not defined or if
editing is turned off. Analogous to current_readline_prompt. */
static char *current_decoded_prompt;
/* The last read token, or NULL. read_token () uses this for context
checking. */
static int last_read_token;
/* The token read prior to last_read_token. */
static int token_before_that;
/* The token read prior to token_before_that. */
static int two_tokens_ago;
static int global_extglob;
/* The line number in a script where the word in a `case WORD', `select WORD'
or `for WORD' begins. This is a nested command maximum, since the array
index is decremented after a case, select, or for command is parsed. */
#define MAX_CASE_NEST 128
static int word_lineno[MAX_CASE_NEST+1];
static int word_top = -1;
/* If non-zero, it is the token that we want read_token to return
regardless of what text is (or isn't) present to be read. This
is reset by read_token. If token_to_read == WORD or
ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
static int token_to_read;
static WORD_DESC *word_desc_to_read;
static REDIRECTEE source;
static REDIRECTEE redir;
%}
%union {
WORD_DESC *word; /* the word that we read. */
int number; /* the number that we read. */
WORD_LIST *word_list;
COMMAND *command;
REDIRECT *redirect;
ELEMENT element;
PATTERN_LIST *pattern;
}
/* Reserved words. Members of the first group are only recognized
in the case that they are preceded by a list_terminator. Members
of the second group are for [[...]] commands. Members of the
third group are recognized only under special circumstances. */
%token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION COPROC
%token COND_START COND_END COND_ERROR
%token IN BANG TIME TIMEOPT TIMEIGN
/* More general tokens. yylex () knows how to make these. */
%token <word> WORD ASSIGNMENT_WORD REDIR_WORD
%token <number> NUMBER
%token <word_list> ARITH_CMD ARITH_FOR_EXPRS
%token <command> COND_CMD
%token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS
%token GREATER_AND SEMI_SEMI SEMI_AND SEMI_SEMI_AND
%token LESS_LESS_MINUS AND_GREATER AND_GREATER_GREATER LESS_GREATER
%token GREATER_BAR BAR_AND
/* The types that the various syntactical units return. */
%type <command> inputunit command pipeline pipeline_command
%type <command> list list0 list1 compound_list simple_list simple_list1
%type <command> simple_command shell_command
%type <command> for_command select_command case_command group_command
%type <command> arith_command
%type <command> cond_command
%type <command> arith_for_command
%type <command> coproc
%type <command> function_def function_body if_command elif_clause subshell
%type <redirect> redirection redirection_list
%type <element> simple_command_element
%type <word_list> word_list pattern
%type <pattern> pattern_list case_clause_sequence case_clause
%type <number> timespec
%type <number> list_terminator
%start inputunit
%left '&' ';' '\n' yacc_EOF
%left AND_AND OR_OR
%right '|' BAR_AND
%%
inputunit: simple_list simple_list_terminator
{
/* Case of regular command. Discard the error
safety net,and return the command just parsed. */
global_command = $1;
eof_encountered = 0;
/* discard_parser_constructs (0); */
if (parser_state & PST_CMDSUBST)
parser_state |= PST_EOFTOKEN;
YYACCEPT;
}
| '\n'
{
/* Case of regular command, but not a very
interesting one. Return a NULL command. */
global_command = (COMMAND *)NULL;
if (parser_state & PST_CMDSUBST)
parser_state |= PST_EOFTOKEN;
YYACCEPT;
}
| error '\n'
{
/* Error during parsing. Return NULL command. */
global_command = (COMMAND *)NULL;
eof_encountered = 0;
/* discard_parser_constructs (1); */
if (interactive && parse_and_execute_level == 0)
{
YYACCEPT;
}
else
{
YYABORT;
}
}
| yacc_EOF
{
/* Case of EOF seen by itself. Do ignoreeof or
not. */
global_command = (COMMAND *)NULL;
handle_eof_input_unit ();
YYACCEPT;
}
;
word_list: WORD
{ $$ = make_word_list ($1, (WORD_LIST *)NULL); }
| word_list WORD
{ $$ = make_word_list ($2, $1); }
;
redirection: '>' WORD
{
source.dest = 1;
redir.filename = $2;
$$ = make_redirection (source, r_output_direction, redir, 0);
}
| '<' WORD
{
source.dest = 0;
redir.filename = $2;
$$ = make_redirection (source, r_input_direction, redir, 0);
}
| NUMBER '>' WORD
{
source.dest = $1;
redir.filename = $3;
$$ = make_redirection (source, r_output_direction, redir, 0);
}
| NUMBER '<' WORD
{
source.dest = $1;
redir.filename = $3;
$$ = make_redirection (source, r_input_direction, redir, 0);
}
| REDIR_WORD '>' WORD
{
source.filename = $1;
redir.filename = $3;
$$ = make_redirection (source, r_output_direction, redir, REDIR_VARASSIGN);
}
| REDIR_WORD '<' WORD
{
source.filename = $1;
redir.filename = $3;
$$ = make_redirection (source, r_input_direction, redir, REDIR_VARASSIGN);
}
| GREATER_GREATER WORD
{
source.dest = 1;
redir.filename = $2;
$$ = make_redirection (source, r_appending_to, redir, 0);
}
| NUMBER GREATER_GREATER WORD
{
source.dest = $1;
redir.filename = $3;
$$ = make_redirection (source, r_appending_to, redir, 0);
}
| REDIR_WORD GREATER_GREATER WORD
{
source.filename = $1;
redir.filename = $3;
$$ = make_redirection (source, r_appending_to, redir, REDIR_VARASSIGN);
}
| GREATER_BAR WORD
{
source.dest = 1;
redir.filename = $2;
$$ = make_redirection (source, r_output_force, redir, 0);
}
| NUMBER GREATER_BAR WORD
{
source.dest = $1;
redir.filename = $3;
$$ = make_redirection (source, r_output_force, redir, 0);
}
| REDIR_WORD GREATER_BAR WORD
{
source.filename = $1;
redir.filename = $3;
$$ = make_redirection (source, r_output_force, redir, REDIR_VARASSIGN);
}
| LESS_GREATER WORD
{
source.dest = 0;
redir.filename = $2;
$$ = make_redirection (source, r_input_output, redir, 0);
}
| NUMBER LESS_GREATER WORD
{
source.dest = $1;
redir.filename = $3;
$$ = make_redirection (source, r_input_output, redir, 0);
}
| REDIR_WORD LESS_GREATER WORD
{
source.filename = $1;
redir.filename = $3;
$$ = make_redirection (source, r_input_output, redir, REDIR_VARASSIGN);
}
| LESS_LESS WORD
{
source.dest = 0;
redir.filename = $2;
$$ = make_redirection (source, r_reading_until, redir, 0);
push_heredoc ($$);
}
| NUMBER LESS_LESS WORD
{
source.dest = $1;
redir.filename = $3;
$$ = make_redirection (source, r_reading_until, redir, 0);
push_heredoc ($$);
}
| REDIR_WORD LESS_LESS WORD
{
source.filename = $1;
redir.filename = $3;
$$ = make_redirection (source, r_reading_until, redir, REDIR_VARASSIGN);
push_heredoc ($$);
}
| LESS_LESS_MINUS WORD
{
source.dest = 0;
redir.filename = $2;
$$ = make_redirection (source, r_deblank_reading_until, redir, 0);
push_heredoc ($$);
}
| NUMBER LESS_LESS_MINUS WORD
{
source.dest = $1;
redir.filename = $3;
$$ = make_redirection (source, r_deblank_reading_until, redir, 0);
push_heredoc ($$);
}
| REDIR_WORD LESS_LESS_MINUS WORD
{
source.filename = $1;
redir.filename = $3;
$$ = make_redirection (source, r_deblank_reading_until, redir, REDIR_VARASSIGN);
push_heredoc ($$);
}
| LESS_LESS_LESS WORD
{
source.dest = 0;
redir.filename = $2;
$$ = make_redirection (source, r_reading_string, redir, 0);
}
| NUMBER LESS_LESS_LESS WORD
{
source.dest = $1;
redir.filename = $3;
$$ = make_redirection (source, r_reading_string, redir, 0);
}
| REDIR_WORD LESS_LESS_LESS WORD
{
source.filename = $1;
redir.filename = $3;
$$ = make_redirection (source, r_reading_string, redir, REDIR_VARASSIGN);
}
| LESS_AND NUMBER
{
source.dest = 0;
redir.dest = $2;
$$ = make_redirection (source, r_duplicating_input, redir, 0);
}
| NUMBER LESS_AND NUMBER
{
source.dest = $1;
redir.dest = $3;
$$ = make_redirection (source, r_duplicating_input, redir, 0);
}
| REDIR_WORD LESS_AND NUMBER
{
source.filename = $1;
redir.dest = $3;
$$ = make_redirection (source, r_duplicating_input, redir, REDIR_VARASSIGN);
}
| GREATER_AND NUMBER
{
source.dest = 1;
redir.dest = $2;
$$ = make_redirection (source, r_duplicating_output, redir, 0);
}
| NUMBER GREATER_AND NUMBER
{
source.dest = $1;
redir.dest = $3;
$$ = make_redirection (source, r_duplicating_output, redir, 0);
}
| REDIR_WORD GREATER_AND NUMBER
{
source.filename = $1;
redir.dest = $3;
$$ = make_redirection (source, r_duplicating_output, redir, REDIR_VARASSIGN);
}
| LESS_AND WORD
{
source.dest = 0;
redir.filename = $2;
$$ = make_redirection (source, r_duplicating_input_word, redir, 0);
}
| NUMBER LESS_AND WORD
{
source.dest = $1;
redir.filename = $3;
$$ = make_redirection (source, r_duplicating_input_word, redir, 0);
}
| REDIR_WORD LESS_AND WORD
{
source.filename = $1;
redir.filename = $3;
$$ = make_redirection (source, r_duplicating_input_word, redir, REDIR_VARASSIGN);
}
| GREATER_AND WORD
{
source.dest = 1;
redir.filename = $2;
$$ = make_redirection (source, r_duplicating_output_word, redir, 0);
}
| NUMBER GREATER_AND WORD
{
source.dest = $1;
redir.filename = $3;
$$ = make_redirection (source, r_duplicating_output_word, redir, 0);
}
| REDIR_WORD GREATER_AND WORD
{
source.filename = $1;
redir.filename = $3;
$$ = make_redirection (source, r_duplicating_output_word, redir, REDIR_VARASSIGN);
}
| GREATER_AND '-'
{
source.dest = 1;
redir.dest = 0;
$$ = make_redirection (source, r_close_this, redir, 0);
}
| NUMBER GREATER_AND '-'
{
source.dest = $1;
redir.dest = 0;
$$ = make_redirection (source, r_close_this, redir, 0);
}
| REDIR_WORD GREATER_AND '-'
{
source.filename = $1;
redir.dest = 0;
$$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
}
| LESS_AND '-'
{
source.dest = 0;
redir.dest = 0;
$$ = make_redirection (source, r_close_this, redir, 0);
}
| NUMBER LESS_AND '-'
{
source.dest = $1;
redir.dest = 0;
$$ = make_redirection (source, r_close_this, redir, 0);
}
| REDIR_WORD LESS_AND '-'
{
source.filename = $1;
redir.dest = 0;
$$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
}
| AND_GREATER WORD
{
source.dest = 1;
redir.filename = $2;
$$ = make_redirection (source, r_err_and_out, redir, 0);
}
| AND_GREATER_GREATER WORD
{
source.dest = 1;
redir.filename = $2;
$$ = make_redirection (source, r_append_err_and_out, redir, 0);
}
;
simple_command_element: WORD
{ $$.word = $1; $$.redirect = 0; }
| ASSIGNMENT_WORD
{ $$.word = $1; $$.redirect = 0; }
| redirection
{ $$.redirect = $1; $$.word = 0; }
;
redirection_list: redirection
{
$$ = $1;
}
| redirection_list redirection
{
register REDIRECT *t;
for (t = $1; t->next; t = t->next)
;
t->next = $2;
$$ = $1;
}
;
simple_command: simple_command_element
{ $$ = make_simple_command ($1, (COMMAND *)NULL); }
| simple_command simple_command_element
{ $$ = make_simple_command ($2, $1); }
;
command: simple_command
{ $$ = clean_simple_command ($1); }
| shell_command
{ $$ = $1; }
| shell_command redirection_list
{
COMMAND *tc;
tc = $1;
if (tc->redirects)
{
register REDIRECT *t;
for (t = tc->redirects; t->next; t = t->next)
;
t->next = $2;
}
else
tc->redirects = $2;
$$ = $1;
}
| function_def
{ $$ = $1; }
| coproc
{ $$ = $1; }
;
shell_command: for_command
{ $$ = $1; }
| case_command
{ $$ = $1; }
| WHILE compound_list DO compound_list DONE
{ $$ = make_while_command ($2, $4); }
| UNTIL compound_list DO compound_list DONE
{ $$ = make_until_command ($2, $4); }
| select_command
{ $$ = $1; }
| if_command
{ $$ = $1; }
| subshell
{ $$ = $1; }
| group_command
{ $$ = $1; }
| arith_command
{ $$ = $1; }
| cond_command
{ $$ = $1; }
| arith_for_command
{ $$ = $1; }
;
for_command: FOR WORD newline_list DO compound_list DONE
{
$$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| FOR WORD newline_list '{' compound_list '}'
{
$$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| FOR WORD ';' newline_list DO compound_list DONE
{
$$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| FOR WORD ';' newline_list '{' compound_list '}'
{
$$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
{
$$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
{
$$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
{
$$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
{
$$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
;
arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
{
$$ = make_arith_for_command ($2, $6, arith_for_lineno);
if (word_top > 0) word_top--;
}
| FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
{
$$ = make_arith_for_command ($2, $6, arith_for_lineno);
if (word_top > 0) word_top--;
}
| FOR ARITH_FOR_EXPRS DO compound_list DONE
{
$$ = make_arith_for_command ($2, $4, arith_for_lineno);
if (word_top > 0) word_top--;
}
| FOR ARITH_FOR_EXPRS '{' compound_list '}'
{
$$ = make_arith_for_command ($2, $4, arith_for_lineno);
if (word_top > 0) word_top--;
}
;
select_command: SELECT WORD newline_list DO list DONE
{
$$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| SELECT WORD newline_list '{' list '}'
{
$$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| SELECT WORD ';' newline_list DO list DONE
{
$$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| SELECT WORD ';' newline_list '{' list '}'
{
$$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
{
$$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
{
$$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
;
case_command: CASE WORD newline_list IN newline_list ESAC
{
$$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| CASE WORD newline_list IN case_clause_sequence newline_list ESAC
{
$$ = make_case_command ($2, $5, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| CASE WORD newline_list IN case_clause ESAC
{
$$ = make_case_command ($2, $5, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
;
function_def: WORD '(' ')' newline_list function_body
{ $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
| FUNCTION WORD '(' ')' newline_list function_body
{ $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
| FUNCTION WORD newline_list function_body
{ $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
;
function_body: shell_command
{ $$ = $1; }
| shell_command redirection_list
{
COMMAND *tc;
tc = $1;
/* According to Posix.2 3.9.5, redirections
specified after the body of a function should
be attached to the function and performed when
the function is executed, not as part of the
function definition command. */
/* XXX - I don't think it matters, but we might
want to change this in the future to avoid
problems differentiating between a function
definition with a redirection and a function
definition containing a single command with a
redirection. The two are semantically equivalent,
though -- the only difference is in how the
command printing code displays the redirections. */
if (tc->redirects)
{
register REDIRECT *t;
for (t = tc->redirects; t->next; t = t->next)
;
t->next = $2;
}
else
tc->redirects = $2;
$$ = $1;
}
;
subshell: '(' compound_list ')'
{
$$ = make_subshell_command ($2);
$$->flags |= CMD_WANT_SUBSHELL;
}
;
coproc: COPROC shell_command
{
$$ = make_coproc_command ("COPROC", $2);
$$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
}
| COPROC shell_command redirection_list
{
COMMAND *tc;
tc = $2;
if (tc->redirects)
{
register REDIRECT *t;
for (t = tc->redirects; t->next; t = t->next)
;
t->next = $3;
}
else
tc->redirects = $3;
$$ = make_coproc_command ("COPROC", $2);
$$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
}
| COPROC WORD shell_command
{
$$ = make_coproc_command ($2->word, $3);
$$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
}
| COPROC WORD shell_command redirection_list
{
COMMAND *tc;
tc = $3;
if (tc->redirects)
{
register REDIRECT *t;
for (t = tc->redirects; t->next; t = t->next)
;
t->next = $4;
}
else
tc->redirects = $4;
$$ = make_coproc_command ($2->word, $3);
$$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
}
| COPROC simple_command
{
$$ = make_coproc_command ("COPROC", clean_simple_command ($2));
$$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
}
;