-
Notifications
You must be signed in to change notification settings - Fork 182
/
parse.y
6878 lines (6080 loc) · 191 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-2022 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 "execute_cmd.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 PARAMS((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
#define END_ALIAS -2
#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
#define EXTEND_SHELL_INPUT_LINE_PROPERTY() \
do { \
if (shell_input_line_len + 2 > shell_input_line_propsize) \
{ \
shell_input_line_propsize = shell_input_line_len + 2; \
shell_input_line_property = (char *)xrealloc (shell_input_line_property, \
shell_input_line_propsize); \
} \
} while (0)
#if defined (EXTENDED_GLOB)
extern int extended_glob, extglob_flag;
#endif
#if defined (TRANSLATABLE_STRINGS)
extern int dump_translatable_strings, dump_po_strings;
extern int singlequote_translations;
#endif /* TRANSLATABLE_STRINGS */
#if !defined (errno)
extern int errno;
#endif
/* **************************************************************** */
/* */
/* "Forward" declarations */
/* */
/* **************************************************************** */
#ifdef DEBUG
static void debug_parser PARAMS((int));
#endif
static int yy_getc PARAMS((void));
static int yy_ungetc PARAMS((int));
#if defined (READLINE)
static int yy_readline_get PARAMS((void));
static int yy_readline_unget PARAMS((int));
#endif
static int yy_string_get PARAMS((void));
static int yy_string_unget PARAMS((int));
static int yy_stream_get PARAMS((void));
static int yy_stream_unget PARAMS((int));
static int shell_getc PARAMS((int));
static void shell_ungetc PARAMS((int));
static void discard_until PARAMS((int));
static void push_string PARAMS((char *, int, alias_t *));
static void pop_string PARAMS((void));
static void free_string_list PARAMS((void));
static char *read_a_line PARAMS((int));
static int reserved_word_acceptable PARAMS((int));
static int yylex PARAMS((void));
static void push_heredoc PARAMS((REDIRECT *));
static char *mk_alexpansion PARAMS((char *));
static int alias_expand_token PARAMS((char *));
static int time_command_acceptable PARAMS((void));
static int special_case_tokens PARAMS((char *));
static int read_token PARAMS((int));
static char *parse_matched_pair PARAMS((int, int, int, int *, int));
static char *parse_comsub PARAMS((int, int, int, int *, int));
#if defined (ARRAY_VARS)
static char *parse_compound_assignment PARAMS((int *));
#endif
#if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
static int parse_dparen PARAMS((int));
static int parse_arith_cmd PARAMS((char **, int));
#endif
#if defined (COND_COMMAND)
static void cond_error PARAMS((void));
static COND_COM *cond_expr PARAMS((void));
static COND_COM *cond_or PARAMS((void));
static COND_COM *cond_and PARAMS((void));
static COND_COM *cond_term PARAMS((void));
static int cond_skip_newlines PARAMS((void));
static COMMAND *parse_cond_command PARAMS((void));
#endif
#if defined (ARRAY_VARS)
static int token_is_assignment PARAMS((char *, int));
static int token_is_ident PARAMS((char *, int));
#endif
static int read_token_word PARAMS((int));
static void discard_parser_constructs PARAMS((int));
static char *error_token_from_token PARAMS((int));
static char *error_token_from_text PARAMS((void));
static void print_offending_line PARAMS((void));
static void report_syntax_error PARAMS((char *));
static void handle_eof_input_unit PARAMS((void));
static void prompt_again PARAMS((int));
#if 0
static void reset_readline_prompt PARAMS((void));
#endif
static void print_prompt PARAMS((void));
#if defined (HANDLE_MULTIBYTE)
static void set_line_mbstate PARAMS((void));
static char *shell_input_line_property = NULL;
static size_t shell_input_line_propsize = 0;
#else
# define set_line_mbstate()
#endif
extern int yyerror PARAMS((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;
static FILE *yyoutstream;
static FILE *yyerrstream;
%}
%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
/* Special; never created by yylex; only set by parse_comsub and xparse_dolparen */
%token DOLPAREN
/* The types that the various syntactical units return. */
%type <command> inputunit command pipeline pipeline_command
%type <command> 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> comsub
%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;
}
| comsub
{
/* This is special; look at the production and how
parse_comsub sets token_to_read */
global_command = $1;
eof_encountered = 0;
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;
}
}
| error yacc_EOF
{
/* EOF after an error. Do ignoreeof or not. Really
only interesting in non-interactive shells */
global_command = (COMMAND *)NULL;
if (last_command_exit_value == 0)
last_command_exit_value = EX_BADUSAGE; /* force error return */
if (interactive && parse_and_execute_level == 0)
{
handle_eof_input_unit ();
YYACCEPT;
}
else
{
YYABORT;
}
}
| error YYEOF
{
global_command = (COMMAND *)NULL;
if (last_command_exit_value == 0)
last_command_exit_value = EX_BADUSAGE; /* force error return */
if (interactive && parse_and_execute_level == 0)
{
handle_eof_input_unit ();
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 && tc->redirects)
{
register REDIRECT *t;
for (t = tc->redirects; t->next; t = t->next)
;
t->next = $2;
}
else if (tc)
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 ($$ == 0) YYERROR;
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 ($$ == 0) YYERROR;
if (word_top > 0) word_top--;
}
| FOR ARITH_FOR_EXPRS DO compound_list DONE
{
$$ = make_arith_for_command ($2, $4, arith_for_lineno);
if ($$ == 0) YYERROR;
if (word_top > 0) word_top--;
}
| FOR ARITH_FOR_EXPRS '{' compound_list '}'
{
$$ = make_arith_for_command ($2, $4, arith_for_lineno);
if ($$ == 0) YYERROR;
if (word_top > 0) word_top--;
}
;
select_command: SELECT WORD newline_list DO compound_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 '{' compound_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 compound_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 '{' compound_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 compound_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 '{' compound_list '}'
{
$$ = 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 list_terminator newline_list DO compound_list DONE
{
$$ = make_select_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
| SELECT WORD newline_list IN list_terminator newline_list '{' compound_list '}'
{
$$ = make_select_command ($2, (WORD_LIST *)NULL, $8, 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 function_body
{ $$ = make_function_def ($2, $3, function_dstart, function_bstart); }
| FUNCTION WORD '\n' newline_list function_body
{ $$ = make_function_def ($2, $5, 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 && tc->redirects)
{
register REDIRECT *t;
for (t = tc->redirects; t->next; t = t->next)
;
t->next = $2;
}