1 /* parse.y - Yacc grammar for bash. */
3 /* Copyright (C) 1989-2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 #include "bashtypes.h"
29 #if defined (HAVE_UNISTD_H)
33 #if defined (HAVE_LOCALE_H)
38 #include "chartypes.h"
45 #define NEED_STRFTIME_DECL /* used in externs.h */
51 #include "mailcheck.h"
54 #include "builtins/common.h"
55 #include "builtins/builtext.h"
59 #if defined (READLINE)
60 # include "bashline.h"
61 # include <readline/readline.h>
65 # include "bashhist.h"
66 # include <readline/history.h>
69 #if defined (JOB_CONTROL)
71 #endif /* JOB_CONTROL */
76 typedef void *alias_t;
79 #if defined (PROMPT_STRING_DECODE)
81 # include <sys/param.h>
84 # if defined (TM_IN_SYS_TIME)
85 # include <sys/types.h>
86 # include <sys/time.h>
87 # endif /* TM_IN_SYS_TIME */
89 #endif /* PROMPT_STRING_DECODE */
91 #define RE_READ_TOKEN -99
92 #define NO_EXPANSION -100
100 #if defined (HANDLE_MULTIBYTE)
101 # define last_shell_getc_is_singlebyte \
102 ((shell_input_line_index > 1) \
103 ? shell_input_line_property[shell_input_line_index - 1] \
105 # define MBTEST(x) ((x) && last_shell_getc_is_singlebyte)
107 # define last_shell_getc_is_singlebyte 1
108 # define MBTEST(x) ((x))
111 #if defined (EXTENDED_GLOB)
112 extern int extended_glob;
115 extern int eof_encountered;
116 extern int no_line_editing, running_under_emacs;
117 extern int current_command_number;
118 extern int sourcelevel, parse_and_execute_level;
119 extern int posixly_correct;
120 extern int last_command_exit_value;
121 extern char *shell_name, *current_host_name;
122 extern char *dist_version;
123 extern int patch_level;
124 extern int dump_translatable_strings, dump_po_strings;
125 extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
126 #if defined (BUFFERED_INPUT)
127 extern int bash_input_fd_changed;
131 /* **************************************************************** */
133 /* "Forward" declarations */
135 /* **************************************************************** */
138 static void debug_parser __P((int));
141 static int yy_getc __P((void));
142 static int yy_ungetc __P((int));
144 #if defined (READLINE)
145 static int yy_readline_get __P((void));
146 static int yy_readline_unget __P((int));
149 static int yy_string_get __P((void));
150 static int yy_string_unget __P((int));
151 static void rewind_input_string __P((void));
152 static int yy_stream_get __P((void));
153 static int yy_stream_unget __P((int));
155 static int shell_getc __P((int));
156 static void shell_ungetc __P((int));
157 static void discard_until __P((int));
159 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
160 static void push_string __P((char *, int, alias_t *));
161 static void pop_string __P((void));
162 static void free_string_list __P((void));
165 static char *read_a_line __P((int));
167 static int reserved_word_acceptable __P((int));
168 static int yylex __P((void));
169 static int alias_expand_token __P((char *));
170 static int time_command_acceptable __P((void));
171 static int special_case_tokens __P((char *));
172 static int read_token __P((int));
173 static char *parse_matched_pair __P((int, int, int, int *, int));
174 static char *parse_comsub __P((int, int, int, int *, int));
175 #if defined (ARRAY_VARS)
176 static char *parse_compound_assignment __P((int *));
178 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
179 static int parse_dparen __P((int));
180 static int parse_arith_cmd __P((char **, int));
182 #if defined (COND_COMMAND)
183 static void cond_error __P((void));
184 static COND_COM *cond_expr __P((void));
185 static COND_COM *cond_or __P((void));
186 static COND_COM *cond_and __P((void));
187 static COND_COM *cond_term __P((void));
188 static int cond_skip_newlines __P((void));
189 static COMMAND *parse_cond_command __P((void));
191 #if defined (ARRAY_VARS)
192 static int token_is_assignment __P((char *, int));
193 static int token_is_ident __P((char *, int));
195 static int read_token_word __P((int));
196 static void discard_parser_constructs __P((int));
198 static char *error_token_from_token __P((int));
199 static char *error_token_from_text __P((void));
200 static void print_offending_line __P((void));
201 static void report_syntax_error __P((char *));
203 static void handle_eof_input_unit __P((void));
204 static void prompt_again __P((void));
206 static void reset_readline_prompt __P((void));
208 static void print_prompt __P((void));
210 #if defined (HANDLE_MULTIBYTE)
211 static void set_line_mbstate __P((void));
212 static char *shell_input_line_property = NULL;
214 # define set_line_mbstate()
217 extern int yyerror __P((const char *));
223 /* Default prompt strings */
224 char *primary_prompt = PPROMPT;
225 char *secondary_prompt = SPROMPT;
227 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
228 char *ps1_prompt, *ps2_prompt;
230 /* Handle on the current prompt string. Indirectly points through
231 ps1_ or ps2_prompt. */
232 char **prompt_string_pointer = (char **)NULL;
233 char *current_prompt_string;
235 /* Non-zero means we expand aliases in commands. */
236 int expand_aliases = 0;
238 /* If non-zero, the decoded prompt string undergoes parameter and
239 variable substitution, command substitution, arithmetic substitution,
240 string expansion, process substitution, and quote removal in
241 decode_prompt_string. */
244 /* If non-zero, $'...' and $"..." are expanded when they appear within
245 a ${...} expansion, even when the expansion appears within double
247 int extended_quote = 1;
249 /* The decoded prompt string. Used if READLINE is not defined or if
250 editing is turned off. Analogous to current_readline_prompt. */
251 static char *current_decoded_prompt;
253 /* The number of lines read from input while creating the current command. */
254 int current_command_line_count;
256 /* The token that currently denotes the end of parse. */
259 /* The token currently being read. */
262 /* Variables to manage the task of reading here documents, because we need to
263 defer the reading until after a complete command has been collected. */
264 static REDIRECT *redir_stack[10];
267 /* Where shell input comes from. History expansion is performed on each
268 line when the shell is interactive. */
269 static char *shell_input_line = (char *)NULL;
270 static int shell_input_line_index;
271 static int shell_input_line_size; /* Amount allocated for shell_input_line. */
272 static int shell_input_line_len; /* strlen (shell_input_line) */
274 /* Either zero or EOF. */
275 static int shell_input_line_terminator;
277 /* The line number in a script on which a function definition starts. */
278 static int function_dstart;
280 /* The line number in a script on which a function body starts. */
281 static int function_bstart;
283 /* The line number in a script at which an arithmetic for command starts. */
284 static int arith_for_lineno;
286 /* The current parser state. */
287 static int parser_state;
289 /* The last read token, or NULL. read_token () uses this for context
291 static int last_read_token;
293 /* The token read prior to last_read_token. */
294 static int token_before_that;
296 /* The token read prior to token_before_that. */
297 static int two_tokens_ago;
299 /* The line number in a script where the word in a `case WORD', `select WORD'
300 or `for WORD' begins. This is a nested command maximum, since the array
301 index is decremented after a case, select, or for command is parsed. */
302 #define MAX_CASE_NEST 128
303 static int word_lineno[MAX_CASE_NEST];
304 static int word_top = -1;
306 /* If non-zero, it is the token that we want read_token to return
307 regardless of what text is (or isn't) present to be read. This
308 is reset by read_token. If token_to_read == WORD or
309 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
310 static int token_to_read;
311 static WORD_DESC *word_desc_to_read;
313 static REDIRECTEE redir;
317 WORD_DESC *word; /* the word that we read. */
318 int number; /* the number that we read. */
319 WORD_LIST *word_list;
323 PATTERN_LIST *pattern;
326 /* Reserved words. Members of the first group are only recognized
327 in the case that they are preceded by a list_terminator. Members
328 of the second group are for [[...]] commands. Members of the
329 third group are recognized only under special circumstances. */
330 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION COPROC
331 %token COND_START COND_END COND_ERROR
332 %token IN BANG TIME TIMEOPT
334 /* More general tokens. yylex () knows how to make these. */
335 %token <word> WORD ASSIGNMENT_WORD
336 %token <number> NUMBER
337 %token <word_list> ARITH_CMD ARITH_FOR_EXPRS
338 %token <command> COND_CMD
339 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS
340 %token GREATER_AND SEMI_SEMI SEMI_AND SEMI_SEMI_AND
341 %token LESS_LESS_MINUS AND_GREATER AND_GREATER_GREATER LESS_GREATER
342 %token GREATER_BAR BAR_AND
344 /* The types that the various syntactical units return. */
346 %type <command> inputunit command pipeline pipeline_command
347 %type <command> list list0 list1 compound_list simple_list simple_list1
348 %type <command> simple_command shell_command
349 %type <command> for_command select_command case_command group_command
350 %type <command> arith_command
351 %type <command> cond_command
352 %type <command> arith_for_command
353 %type <command> coproc
354 %type <command> function_def function_body if_command elif_clause subshell
355 %type <redirect> redirection redirection_list
356 %type <element> simple_command_element
357 %type <word_list> word_list pattern
358 %type <pattern> pattern_list case_clause_sequence case_clause
359 %type <number> timespec
360 %type <number> list_terminator
364 %left '&' ';' '\n' yacc_EOF
369 inputunit: simple_list simple_list_terminator
371 /* Case of regular command. Discard the error
372 safety net,and return the command just parsed. */
375 /* discard_parser_constructs (0); */
376 if (parser_state & PST_CMDSUBST)
377 parser_state |= PST_EOFTOKEN;
382 /* Case of regular command, but not a very
383 interesting one. Return a NULL command. */
384 global_command = (COMMAND *)NULL;
385 if (parser_state & PST_CMDSUBST)
386 parser_state |= PST_EOFTOKEN;
391 /* Error during parsing. Return NULL command. */
392 global_command = (COMMAND *)NULL;
394 /* discard_parser_constructs (1); */
395 if (interactive && parse_and_execute_level == 0)
406 /* Case of EOF seen by itself. Do ignoreeof or
408 global_command = (COMMAND *)NULL;
409 handle_eof_input_unit ();
415 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
417 { $$ = make_word_list ($2, $1); }
420 redirection: '>' WORD
423 $$ = make_redirection (1, r_output_direction, redir);
428 $$ = make_redirection (0, r_input_direction, redir);
433 $$ = make_redirection ($1, r_output_direction, redir);
438 $$ = make_redirection ($1, r_input_direction, redir);
440 | GREATER_GREATER WORD
443 $$ = make_redirection (1, r_appending_to, redir);
445 | NUMBER GREATER_GREATER WORD
448 $$ = make_redirection ($1, r_appending_to, redir);
453 $$ = make_redirection (0, r_reading_until, redir);
454 redir_stack[need_here_doc++] = $$;
456 | NUMBER LESS_LESS WORD
459 $$ = make_redirection ($1, r_reading_until, redir);
460 redir_stack[need_here_doc++] = $$;
462 | LESS_LESS_LESS WORD
465 $$ = make_redirection (0, r_reading_string, redir);
467 | NUMBER LESS_LESS_LESS WORD
470 $$ = make_redirection ($1, r_reading_string, redir);
475 $$ = make_redirection (0, r_duplicating_input, redir);
477 | NUMBER LESS_AND NUMBER
480 $$ = make_redirection ($1, r_duplicating_input, redir);
485 $$ = make_redirection (1, r_duplicating_output, redir);
487 | NUMBER GREATER_AND NUMBER
490 $$ = make_redirection ($1, r_duplicating_output, redir);
495 $$ = make_redirection (0, r_duplicating_input_word, redir);
497 | NUMBER LESS_AND WORD
500 $$ = make_redirection ($1, r_duplicating_input_word, redir);
505 $$ = make_redirection (1, r_duplicating_output_word, redir);
507 | NUMBER GREATER_AND WORD
510 $$ = make_redirection ($1, r_duplicating_output_word, redir);
512 | LESS_LESS_MINUS WORD
515 $$ = make_redirection
516 (0, r_deblank_reading_until, redir);
517 redir_stack[need_here_doc++] = $$;
519 | NUMBER LESS_LESS_MINUS WORD
522 $$ = make_redirection
523 ($1, r_deblank_reading_until, redir);
524 redir_stack[need_here_doc++] = $$;
529 $$ = make_redirection (1, r_close_this, redir);
531 | NUMBER GREATER_AND '-'
534 $$ = make_redirection ($1, r_close_this, redir);
539 $$ = make_redirection (0, r_close_this, redir);
541 | NUMBER LESS_AND '-'
544 $$ = make_redirection ($1, r_close_this, redir);
549 $$ = make_redirection (1, r_err_and_out, redir);
551 | AND_GREATER_GREATER WORD
554 $$ = make_redirection (1, r_append_err_and_out, redir);
556 | NUMBER LESS_GREATER WORD
559 $$ = make_redirection ($1, r_input_output, redir);
564 $$ = make_redirection (0, r_input_output, redir);
569 $$ = make_redirection (1, r_output_force, redir);
571 | NUMBER GREATER_BAR WORD
574 $$ = make_redirection ($1, r_output_force, redir);
578 simple_command_element: WORD
579 { $$.word = $1; $$.redirect = 0; }
581 { $$.word = $1; $$.redirect = 0; }
583 { $$.redirect = $1; $$.word = 0; }
586 redirection_list: redirection
590 | redirection_list redirection
592 register REDIRECT *t;
594 for (t = $1; t->next; t = t->next)
601 simple_command: simple_command_element
602 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
603 | simple_command simple_command_element
604 { $$ = make_simple_command ($2, $1); }
607 command: simple_command
608 { $$ = clean_simple_command ($1); }
611 | shell_command redirection_list
618 register REDIRECT *t;
619 for (t = tc->redirects; t->next; t = t->next)
633 shell_command: for_command
637 | WHILE compound_list DO compound_list DONE
638 { $$ = make_while_command ($2, $4); }
639 | UNTIL compound_list DO compound_list DONE
640 { $$ = make_until_command ($2, $4); }
657 for_command: FOR WORD newline_list DO compound_list DONE
659 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
660 if (word_top > 0) word_top--;
662 | FOR WORD newline_list '{' compound_list '}'
664 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
665 if (word_top > 0) word_top--;
667 | FOR WORD ';' newline_list DO compound_list DONE
669 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
670 if (word_top > 0) word_top--;
672 | FOR WORD ';' newline_list '{' compound_list '}'
674 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
675 if (word_top > 0) word_top--;
677 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
679 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
680 if (word_top > 0) word_top--;
682 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
684 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
685 if (word_top > 0) word_top--;
687 | FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
689 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
690 if (word_top > 0) word_top--;
692 | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
694 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
695 if (word_top > 0) word_top--;
699 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
701 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
702 if (word_top > 0) word_top--;
704 | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
706 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
707 if (word_top > 0) word_top--;
709 | FOR ARITH_FOR_EXPRS DO compound_list DONE
711 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
712 if (word_top > 0) word_top--;
714 | FOR ARITH_FOR_EXPRS '{' compound_list '}'
716 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
717 if (word_top > 0) word_top--;
721 select_command: SELECT WORD newline_list DO list DONE
723 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
724 if (word_top > 0) word_top--;
726 | SELECT WORD newline_list '{' list '}'
728 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
729 if (word_top > 0) word_top--;
731 | SELECT WORD ';' newline_list DO list DONE
733 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
734 if (word_top > 0) word_top--;
736 | SELECT WORD ';' newline_list '{' list '}'
738 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
739 if (word_top > 0) word_top--;
741 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
743 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
744 if (word_top > 0) word_top--;
746 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
748 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
749 if (word_top > 0) word_top--;
753 case_command: CASE WORD newline_list IN newline_list ESAC
755 $$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]);
756 if (word_top > 0) word_top--;
758 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
760 $$ = make_case_command ($2, $5, word_lineno[word_top]);
761 if (word_top > 0) word_top--;
763 | CASE WORD newline_list IN case_clause ESAC
765 $$ = make_case_command ($2, $5, word_lineno[word_top]);
766 if (word_top > 0) word_top--;
770 function_def: WORD '(' ')' newline_list function_body
771 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
773 | FUNCTION WORD '(' ')' newline_list function_body
774 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
776 | FUNCTION WORD newline_list function_body
777 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
780 function_body: shell_command
782 | shell_command redirection_list
787 /* According to Posix.2 3.9.5, redirections
788 specified after the body of a function should
789 be attached to the function and performed when
790 the function is executed, not as part of the
791 function definition command. */
792 /* XXX - I don't think it matters, but we might
793 want to change this in the future to avoid
794 problems differentiating between a function
795 definition with a redirection and a function
796 definition containing a single command with a
797 redirection. The two are semantically equivalent,
798 though -- the only difference is in how the
799 command printing code displays the redirections. */
802 register REDIRECT *t;
803 for (t = tc->redirects; t->next; t = t->next)
813 subshell: '(' compound_list ')'
815 $$ = make_subshell_command ($2);
816 $$->flags |= CMD_WANT_SUBSHELL;
820 coproc: COPROC shell_command
822 $$ = make_coproc_command ("COPROC", $2);
823 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
825 | COPROC shell_command redirection_list
832 register REDIRECT *t;
833 for (t = tc->redirects; t->next; t = t->next)
839 $$ = make_coproc_command ("COPROC", $2);
840 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
842 | COPROC WORD shell_command
844 $$ = make_coproc_command ($2->word, $3);
845 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
847 | COPROC WORD shell_command redirection_list
854 register REDIRECT *t;
855 for (t = tc->redirects; t->next; t = t->next)
861 $$ = make_coproc_command ($2->word, $3);
862 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
864 | COPROC simple_command
866 $$ = make_coproc_command ("COPROC", clean_simple_command ($2));
867 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
871 if_command: IF compound_list THEN compound_list FI
872 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
873 | IF compound_list THEN compound_list ELSE compound_list FI
874 { $$ = make_if_command ($2, $4, $6); }
875 | IF compound_list THEN compound_list elif_clause FI
876 { $$ = make_if_command ($2, $4, $5); }
880 group_command: '{' compound_list '}'
881 { $$ = make_group_command ($2); }
884 arith_command: ARITH_CMD
885 { $$ = make_arith_command ($1); }
888 cond_command: COND_START COND_CMD COND_END
892 elif_clause: ELIF compound_list THEN compound_list
893 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
894 | ELIF compound_list THEN compound_list ELSE compound_list
895 { $$ = make_if_command ($2, $4, $6); }
896 | ELIF compound_list THEN compound_list elif_clause
897 { $$ = make_if_command ($2, $4, $5); }
900 case_clause: pattern_list
901 | case_clause_sequence pattern_list
902 { $2->next = $1; $$ = $2; }
905 pattern_list: newline_list pattern ')' compound_list
906 { $$ = make_pattern_list ($2, $4); }
907 | newline_list pattern ')' newline_list
908 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
909 | newline_list '(' pattern ')' compound_list
910 { $$ = make_pattern_list ($3, $5); }
911 | newline_list '(' pattern ')' newline_list
912 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
915 case_clause_sequence: pattern_list SEMI_SEMI
917 | case_clause_sequence pattern_list SEMI_SEMI
918 { $2->next = $1; $$ = $2; }
919 | pattern_list SEMI_AND
920 { $1->flags |= CASEPAT_FALLTHROUGH; $$ = $1; }
921 | case_clause_sequence pattern_list SEMI_AND
922 { $2->flags |= CASEPAT_FALLTHROUGH; $2->next = $1; $$ = $2; }
923 | pattern_list SEMI_SEMI_AND
924 { $1->flags |= CASEPAT_TESTNEXT; $$ = $1; }
925 | case_clause_sequence pattern_list SEMI_SEMI_AND
926 { $2->flags |= CASEPAT_TESTNEXT; $2->next = $1; $$ = $2; }
930 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
932 { $$ = make_word_list ($3, $1); }
935 /* A list allows leading or trailing newlines and
936 newlines as operators (equivalent to semicolons).
937 It must end with a newline or semicolon.
938 Lists are used within commands such as if, for, while. */
940 list: newline_list list0
944 gather_here_documents ();
955 list0: list1 '\n' newline_list
956 | list1 '&' newline_list
958 if ($1->type == cm_connection)
959 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
961 $$ = command_connect ($1, (COMMAND *)NULL, '&');
963 | list1 ';' newline_list
967 list1: list1 AND_AND newline_list list1
968 { $$ = command_connect ($1, $4, AND_AND); }
969 | list1 OR_OR newline_list list1
970 { $$ = command_connect ($1, $4, OR_OR); }
971 | list1 '&' newline_list list1
973 if ($1->type == cm_connection)
974 $$ = connect_async_list ($1, $4, '&');
976 $$ = command_connect ($1, $4, '&');
978 | list1 ';' newline_list list1
979 { $$ = command_connect ($1, $4, ';'); }
980 | list1 '\n' newline_list list1
981 { $$ = command_connect ($1, $4, ';'); }
986 simple_list_terminator: '\n'
1002 /* A simple_list is a list that contains no significant newlines
1003 and no leading or trailing newlines. Newlines are allowed
1004 only following operators, where they are not significant.
1006 This is what an inputunit consists of. */
1008 simple_list: simple_list1
1012 gather_here_documents ();
1013 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1015 global_command = $1;
1016 eof_encountered = 0;
1017 rewind_input_string ();
1023 if ($1->type == cm_connection)
1024 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
1026 $$ = command_connect ($1, (COMMAND *)NULL, '&');
1028 gather_here_documents ();
1029 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1031 global_command = $1;
1032 eof_encountered = 0;
1033 rewind_input_string ();
1041 gather_here_documents ();
1042 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1044 global_command = $1;
1045 eof_encountered = 0;
1046 rewind_input_string ();
1052 simple_list1: simple_list1 AND_AND newline_list simple_list1
1053 { $$ = command_connect ($1, $4, AND_AND); }
1054 | simple_list1 OR_OR newline_list simple_list1
1055 { $$ = command_connect ($1, $4, OR_OR); }
1056 | simple_list1 '&' simple_list1
1058 if ($1->type == cm_connection)
1059 $$ = connect_async_list ($1, $3, '&');
1061 $$ = command_connect ($1, $3, '&');
1063 | simple_list1 ';' simple_list1
1064 { $$ = command_connect ($1, $3, ';'); }
1070 pipeline_command: pipeline
1075 $2->flags |= CMD_INVERT_RETURN;
1084 | timespec BANG pipeline
1087 $3->flags |= $1|CMD_INVERT_RETURN;
1090 | BANG timespec pipeline
1093 $3->flags |= $2|CMD_INVERT_RETURN;
1096 | timespec list_terminator
1100 /* Boy, this is unclean. `time' by itself can
1101 time a null command. We cheat and push a
1102 newline back if the list_terminator was a newline
1103 to avoid the double-newline problem (one to
1104 terminate this, one to terminate the command) */
1107 $$ = make_simple_command (x, (COMMAND *)NULL);
1109 /* XXX - let's cheat and push a newline back */
1111 token_to_read = '\n';
1116 pipeline: pipeline '|' newline_list pipeline
1117 { $$ = command_connect ($1, $4, '|'); }
1118 | pipeline BAR_AND newline_list pipeline
1120 /* Make cmd1 |& cmd2 equivalent to cmd1 2>&1 | cmd2 */
1127 r = make_redirection (2, r_duplicating_output, rd);
1130 register REDIRECT *t;
1131 for (t = tc->redirects; t->next; t = t->next)
1138 $$ = command_connect ($1, $4, '|');
1145 { $$ = CMD_TIME_PIPELINE; }
1147 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1151 /* Initial size to allocate for tokens, and the
1152 amount to grow them by. */
1153 #define TOKEN_DEFAULT_INITIAL_SIZE 496
1154 #define TOKEN_DEFAULT_GROW_SIZE 512
1156 /* Should we call prompt_again? */
1157 #define SHOULD_PROMPT() \
1158 (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
1161 # define expanding_alias() (pushed_string_list && pushed_string_list->expander)
1163 # define expanding_alias() 0
1166 /* Global var is non-zero when end of file has been reached. */
1167 int EOF_Reached = 0;
1180 /* yy_getc () returns the next available character from input or EOF.
1181 yy_ungetc (c) makes `c' the next character to read.
1182 init_yy_io (get, unget, type, location) makes the function GET the
1183 installed function for getting the next character, makes UNGET the
1184 installed function for un-getting a character, sets the type of stream
1185 (either string or file) from TYPE, and makes LOCATION point to where
1186 the input is coming from. */
1188 /* Unconditionally returns end-of-file. */
1195 /* Variable containing the current get and unget functions.
1196 See ./input.h for a clearer description. */
1197 BASH_INPUT bash_input;
1199 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
1200 is non-null, avoiding a memory leak. */
1202 initialize_bash_input ()
1204 bash_input.type = st_none;
1205 FREE (bash_input.name);
1206 bash_input.name = (char *)NULL;
1207 bash_input.location.file = (FILE *)NULL;
1208 bash_input.location.string = (char *)NULL;
1209 bash_input.getter = (sh_cget_func_t *)NULL;
1210 bash_input.ungetter = (sh_cunget_func_t *)NULL;
1213 /* Set the contents of the current bash input stream from
1214 GET, UNGET, TYPE, NAME, and LOCATION. */
1216 init_yy_io (get, unget, type, name, location)
1217 sh_cget_func_t *get;
1218 sh_cunget_func_t *unget;
1219 enum stream_type type;
1221 INPUT_STREAM location;
1223 bash_input.type = type;
1224 FREE (bash_input.name);
1225 bash_input.name = name ? savestring (name) : (char *)NULL;
1229 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
1231 bash_input.location = location;
1233 bash_input.getter = get;
1234 bash_input.ungetter = unget;
1240 return (bash_input.name ? bash_input.name : "stdin");
1243 /* Call this to get the next character of input. */
1247 return (*(bash_input.getter)) ();
1250 /* Call this to unget C. That is, to make C the next character
1256 return (*(bash_input.ungetter)) (c);
1259 #if defined (BUFFERED_INPUT)
1260 #ifdef INCLUDE_UNUSED
1262 input_file_descriptor ()
1264 switch (bash_input.type)
1267 return (fileno (bash_input.location.file));
1269 return (bash_input.location.buffered_fd);
1272 return (fileno (stdin));
1276 #endif /* BUFFERED_INPUT */
1278 /* **************************************************************** */
1280 /* Let input be read from readline (). */
1282 /* **************************************************************** */
1284 #if defined (READLINE)
1285 char *current_readline_prompt = (char *)NULL;
1286 char *current_readline_line = (char *)NULL;
1287 int current_readline_line_index = 0;
1292 SigHandler *old_sigint;
1296 if (!current_readline_line)
1298 if (!bash_readline_initialized)
1299 initialize_readline ();
1301 #if defined (JOB_CONTROL)
1303 give_terminal_to (shell_pgrp, 0);
1304 #endif /* JOB_CONTROL */
1306 old_sigint = (SigHandler *)NULL;
1307 if (signal_is_ignored (SIGINT) == 0)
1309 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
1310 interrupt_immediately++;
1312 terminate_immediately = 1;
1314 current_readline_line = readline (current_readline_prompt ?
1315 current_readline_prompt : "");
1317 terminate_immediately = 0;
1318 if (signal_is_ignored (SIGINT) == 0 && old_sigint)
1320 interrupt_immediately--;
1321 set_signal_handler (SIGINT, old_sigint);
1325 /* Reset the prompt to the decoded value of prompt_string_pointer. */
1326 reset_readline_prompt ();
1329 if (current_readline_line == 0)
1332 current_readline_line_index = 0;
1333 line_len = strlen (current_readline_line);
1335 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
1336 current_readline_line[line_len++] = '\n';
1337 current_readline_line[line_len] = '\0';
1340 if (current_readline_line[current_readline_line_index] == 0)
1342 free (current_readline_line);
1343 current_readline_line = (char *)NULL;
1344 return (yy_readline_get ());
1348 c = current_readline_line[current_readline_line_index++];
1354 yy_readline_unget (c)
1357 if (current_readline_line_index && current_readline_line)
1358 current_readline_line[--current_readline_line_index] = c;
1363 with_input_from_stdin ()
1365 INPUT_STREAM location;
1367 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1369 location.string = current_readline_line;
1370 init_yy_io (yy_readline_get, yy_readline_unget,
1371 st_stdin, "readline stdin", location);
1375 #else /* !READLINE */
1378 with_input_from_stdin ()
1380 with_input_from_stream (stdin, "stdin");
1382 #endif /* !READLINE */
1384 /* **************************************************************** */
1386 /* Let input come from STRING. STRING is zero terminated. */
1388 /* **************************************************************** */
1393 register char *string;
1394 register unsigned char c;
1396 string = bash_input.location.string;
1398 /* If the string doesn't exist, or is empty, EOF found. */
1399 if (string && *string)
1402 bash_input.location.string = string;
1413 *(--bash_input.location.string) = c;
1418 with_input_from_string (string, name)
1422 INPUT_STREAM location;
1424 location.string = string;
1425 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1428 /* Count the number of characters we've consumed from bash_input.location.string
1429 and read into shell_input_line, but have not returned from shell_getc.
1430 That is the true input location. Rewind bash_input.location.string by
1431 that number of characters, so it points to the last character actually
1432 consumed by the parser. */
1434 rewind_input_string ()
1438 /* number of unconsumed characters in the input -- XXX need to take newlines
1439 into account, e.g., $(...\n) */
1440 xchars = shell_input_line_len - shell_input_line_index;
1441 if (bash_input.location.string[-1] == '\n')
1444 /* XXX - how to reflect bash_input.location.string back to string passed to
1445 parse_and_execute or xparse_dolparen? xparse_dolparen needs to know how
1446 far into the string we parsed. parse_and_execute knows where bash_input.
1447 location.string is, and how far from orig_string that is -- that's the
1448 number of characters the command consumed. */
1450 /* bash_input.location.string - xchars should be where we parsed to */
1451 /* need to do more validation on xchars value for sanity -- test cases. */
1452 bash_input.location.string -= xchars;
1455 /* **************************************************************** */
1457 /* Let input come from STREAM. */
1459 /* **************************************************************** */
1461 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1462 define, and just use getc/ungetc if it was defined, but since bash
1463 installs its signal handlers without the SA_RESTART flag, some signals
1464 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
1465 the read to be restarted. We need to restart it ourselves. */
1473 if (bash_input.location.file)
1477 interrupt_immediately++;
1478 terminate_immediately++;
1480 result = getc_with_restart (bash_input.location.file);
1483 interrupt_immediately--;
1484 terminate_immediately--;
1494 return (ungetc_with_restart (c, bash_input.location.file));
1498 with_input_from_stream (stream, name)
1502 INPUT_STREAM location;
1504 location.file = stream;
1505 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1508 typedef struct stream_saver {
1509 struct stream_saver *next;
1510 BASH_INPUT bash_input;
1512 #if defined (BUFFERED_INPUT)
1513 BUFFERED_STREAM *bstream;
1514 #endif /* BUFFERED_INPUT */
1517 /* The globally known line number. */
1518 int line_number = 0;
1520 #if defined (COND_COMMAND)
1521 static int cond_lineno;
1522 static int cond_token;
1525 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1528 push_stream (reset_lineno)
1531 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1533 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1535 #if defined (BUFFERED_INPUT)
1536 saver->bstream = (BUFFERED_STREAM *)NULL;
1537 /* If we have a buffered stream, clear out buffers[fd]. */
1538 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1539 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1540 (BUFFERED_STREAM *)NULL);
1541 #endif /* BUFFERED_INPUT */
1543 saver->line = line_number;
1544 bash_input.name = (char *)NULL;
1545 saver->next = stream_list;
1546 stream_list = saver;
1559 STREAM_SAVER *saver = stream_list;
1562 stream_list = stream_list->next;
1564 init_yy_io (saver->bash_input.getter,
1565 saver->bash_input.ungetter,
1566 saver->bash_input.type,
1567 saver->bash_input.name,
1568 saver->bash_input.location);
1570 #if defined (BUFFERED_INPUT)
1571 /* If we have a buffered stream, restore buffers[fd]. */
1572 /* If the input file descriptor was changed while this was on the
1573 save stack, update the buffered fd to the new file descriptor and
1574 re-establish the buffer <-> bash_input fd correspondence. */
1575 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1577 if (bash_input_fd_changed)
1579 bash_input_fd_changed = 0;
1580 if (default_buffered_input >= 0)
1582 bash_input.location.buffered_fd = default_buffered_input;
1583 saver->bstream->b_fd = default_buffered_input;
1584 SET_CLOSE_ON_EXEC (default_buffered_input);
1587 /* XXX could free buffered stream returned as result here. */
1588 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1590 #endif /* BUFFERED_INPUT */
1592 line_number = saver->line;
1594 FREE (saver->bash_input.name);
1599 /* Return 1 if a stream of type TYPE is saved on the stack. */
1601 stream_on_stack (type)
1602 enum stream_type type;
1604 register STREAM_SAVER *s;
1606 for (s = stream_list; s; s = s->next)
1607 if (s->bash_input.type == type)
1612 /* Save the current token state and return it in a malloced array. */
1618 ret = (int *)xmalloc (4 * sizeof (int));
1619 ret[0] = last_read_token;
1620 ret[1] = token_before_that;
1621 ret[2] = two_tokens_ago;
1622 ret[3] = current_token;
1627 restore_token_state (ts)
1632 last_read_token = ts[0];
1633 token_before_that = ts[1];
1634 two_tokens_ago = ts[2];
1635 current_token = ts[3];
1639 * This is used to inhibit alias expansion and reserved word recognition
1640 * inside case statement pattern lists. A `case statement pattern list' is:
1642 * everything between the `in' in a `case word in' and the next ')'
1644 * everything between a `;;' and the next `)' or `esac'
1647 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1649 #define END_OF_ALIAS 0
1652 * Pseudo-global variables used in implementing token-wise alias expansion.
1656 * Pushing and popping strings. This works together with shell_getc to
1657 * implement alias expansion on a per-token basis.
1660 typedef struct string_saver {
1661 struct string_saver *next;
1662 int expand_alias; /* Value to set expand_alias to when string is popped. */
1665 alias_t *expander; /* alias that caused this line to be pushed. */
1667 int saved_line_size, saved_line_index, saved_line_terminator;
1670 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1673 * Push the current shell_input_line onto a stack of such lines and make S
1674 * the current input. Used when expanding aliases. EXPAND is used to set
1675 * the value of expand_next_token when the string is popped, so that the
1676 * word after the alias in the original line is handled correctly when the
1677 * alias expands to multiple words. TOKEN is the token that was expanded
1678 * into S; it is saved and used to prevent infinite recursive expansion.
1681 push_string (s, expand, ap)
1686 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
1688 temp->expand_alias = expand;
1689 temp->saved_line = shell_input_line;
1690 temp->saved_line_size = shell_input_line_size;
1691 temp->saved_line_index = shell_input_line_index;
1692 temp->saved_line_terminator = shell_input_line_terminator;
1694 temp->expander = ap;
1696 temp->next = pushed_string_list;
1697 pushed_string_list = temp;
1701 ap->flags |= AL_BEINGEXPANDED;
1704 shell_input_line = s;
1705 shell_input_line_size = strlen (s);
1706 shell_input_line_index = 0;
1707 shell_input_line_terminator = '\0';
1709 parser_state &= ~PST_ALEXPNEXT; /* XXX */
1712 set_line_mbstate ();
1716 * Make the top of the pushed_string stack be the current shell input.
1717 * Only called when there is something on the stack. Called from shell_getc
1718 * when it thinks it has consumed the string generated by an alias expansion
1719 * and needs to return to the original input line.
1726 FREE (shell_input_line);
1727 shell_input_line = pushed_string_list->saved_line;
1728 shell_input_line_index = pushed_string_list->saved_line_index;
1729 shell_input_line_size = pushed_string_list->saved_line_size;
1730 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1732 if (pushed_string_list->expand_alias)
1733 parser_state |= PST_ALEXPNEXT;
1735 parser_state &= ~PST_ALEXPNEXT;
1737 t = pushed_string_list;
1738 pushed_string_list = pushed_string_list->next;
1742 t->expander->flags &= ~AL_BEINGEXPANDED;
1747 set_line_mbstate ();
1753 register STRING_SAVER *t, *t1;
1755 for (t = pushed_string_list; t; )
1758 FREE (t->saved_line);
1761 t->expander->flags &= ~AL_BEINGEXPANDED;
1766 pushed_string_list = (STRING_SAVER *)NULL;
1769 #endif /* ALIAS || DPAREN_ARITHMETIC */
1772 free_pushed_string_input ()
1774 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1775 free_string_list ();
1779 /* Return a line of text, taken from wherever yylex () reads input.
1780 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1781 is non-zero, we remove unquoted \<newline> pairs. This is used by
1782 read_secondary_line to read here documents. */
1784 read_a_line (remove_quoted_newline)
1785 int remove_quoted_newline;
1787 static char *line_buffer = (char *)NULL;
1788 static int buffer_size = 0;
1789 int indx = 0, c, peekc, pass_next;
1791 #if defined (READLINE)
1792 if (no_line_editing && SHOULD_PROMPT ())
1794 if (SHOULD_PROMPT ())
1801 /* Allow immediate exit if interrupted during input. */
1806 /* Ignore null bytes in input. */
1810 internal_warning ("read_a_line: ignored null byte in input");
1815 /* If there is no more input, then we return NULL. */
1818 if (interactive && bash_input.type == st_stream)
1821 return ((char *)NULL);
1825 /* `+2' in case the final character in the buffer is a newline. */
1826 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
1828 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
1829 here document with an unquoted delimiter. In this case,
1830 the line will be expanded as if it were in double quotes.
1831 We allow a backslash to escape the next character, but we
1832 need to treat the backslash specially only if a backslash
1833 quoting a backslash-newline pair appears in the line. */
1836 line_buffer[indx++] = c;
1839 else if (c == '\\' && remove_quoted_newline)
1845 continue; /* Make the unquoted \<newline> pair disappear. */
1851 line_buffer[indx++] = c; /* Preserve the backslash. */
1855 line_buffer[indx++] = c;
1859 line_buffer[indx] = '\0';
1860 return (line_buffer);
1865 /* Return a line as in read_a_line (), but insure that the prompt is
1866 the secondary prompt. This is used to read the lines of a here
1867 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
1868 newlines quoted with backslashes while reading the line. It is
1869 non-zero unless the delimiter of the here document was quoted. */
1871 read_secondary_line (remove_quoted_newline)
1872 int remove_quoted_newline;
1877 prompt_string_pointer = &ps2_prompt;
1878 if (SHOULD_PROMPT())
1880 ret = read_a_line (remove_quoted_newline);
1881 #if defined (HISTORY)
1882 if (remember_on_history && (parser_state & PST_HEREDOC))
1884 /* To make adding the the here-document body right, we need to rely
1885 on history_delimiting_chars() returning \n for the first line of
1886 the here-document body and the null string for the second and
1887 subsequent lines, so we avoid double newlines.
1888 current_command_line_count == 2 for the first line of the body. */
1890 current_command_line_count++;
1891 maybe_add_history (ret);
1893 #endif /* HISTORY */
1897 /* **************************************************************** */
1901 /* **************************************************************** */
1903 /* Reserved words. These are only recognized as the first word of a
1905 STRING_INT_ALIST word_token_alist[] = {
1914 #if defined (SELECT_COMMAND)
1915 { "select", SELECT },
1922 { "function", FUNCTION },
1923 #if defined (COMMAND_TIMING)
1929 #if defined (COND_COMMAND)
1930 { "[[", COND_START },
1933 #if defined (COPROCESS_SUPPORT)
1934 { "coproc", COPROC },
1939 /* other tokens that can be returned by read_token() */
1940 STRING_INT_ALIST other_token_alist[] = {
1941 /* Multiple-character tokens with special values */
1945 { ">>", GREATER_GREATER },
1946 { "<<", LESS_LESS },
1948 { ">&", GREATER_AND },
1949 { ";;", SEMI_SEMI },
1951 { ";;&", SEMI_SEMI_AND },
1952 { "<<-", LESS_LESS_MINUS },
1953 { "<<<", LESS_LESS_LESS },
1954 { "&>", AND_GREATER },
1955 { "&>>", AND_GREATER_GREATER },
1956 { "<>", LESS_GREATER },
1957 { ">|", GREATER_BAR },
1959 { "EOF", yacc_EOF },
1960 /* Tokens whose value is the character itself */
1971 { "newline", '\n' },
1975 /* others not listed here:
1976 WORD look at yylval.word
1977 ASSIGNMENT_WORD look at yylval.word
1978 NUMBER look at yylval.number
1979 ARITH_CMD look at yylval.word_list
1980 ARITH_FOR_EXPRS look at yylval.word_list
1981 COND_CMD look at yylval.command
1984 /* These are used by read_token_word, but appear up here so that shell_getc
1985 can use them to decide when to add otherwise blank lines to the history. */
1987 /* The primary delimiter stack. */
1988 struct dstack dstack = { (char *)NULL, 0, 0 };
1990 /* A temporary delimiter stack to be used when decoding prompt strings.
1991 This is needed because command substitutions in prompt strings (e.g., PS2)
1992 can screw up the parser's quoting state. */
1993 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
1995 /* Macro for accessing the top delimiter on the stack. Returns the
1996 delimiter or zero if none. */
1997 #define current_delimiter(ds) \
1998 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
2000 #define push_delimiter(ds, character) \
2003 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
2004 ds.delimiters = (char *)xrealloc \
2005 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
2006 ds.delimiters[ds.delimiter_depth] = character; \
2007 ds.delimiter_depth++; \
2011 #define pop_delimiter(ds) ds.delimiter_depth--
2013 /* Return the next shell input character. This always reads characters
2014 from shell_input_line; when that line is exhausted, it is time to
2015 read the next line. This is called by read_token when the shell is
2016 processing normal command input. */
2018 /* This implements one-character lookahead/lookbehind across physical input
2019 lines, to avoid something being lost because it's pushed back with
2020 shell_ungetc when we're at the start of a line. */
2021 static int eol_ungetc_lookahead = 0;
2024 shell_getc (remove_quoted_newline)
2025 int remove_quoted_newline;
2033 if (sigwinch_received)
2035 sigwinch_received = 0;
2036 get_new_window_size (0, (int *)0, (int *)0);
2039 if (eol_ungetc_lookahead)
2041 c = eol_ungetc_lookahead;
2042 eol_ungetc_lookahead = 0;
2046 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2047 /* If shell_input_line[shell_input_line_index] == 0, but there is
2048 something on the pushed list of strings, then we don't want to go
2049 off and get another line. We let the code down below handle it. */
2051 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
2052 (pushed_string_list == (STRING_SAVER *)NULL)))
2053 #else /* !ALIAS && !DPAREN_ARITHMETIC */
2054 if (!shell_input_line || !shell_input_line[shell_input_line_index])
2055 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
2061 /* Allow immediate exit if interrupted during input. */
2065 shell_input_line_terminator = 0;
2067 /* If the shell is interatctive, but not currently printing a prompt
2068 (interactive_shell && interactive == 0), we don't want to print
2069 notifies or cleanup the jobs -- we want to defer it until we do
2070 print the next prompt. */
2071 if (interactive_shell == 0 || SHOULD_PROMPT())
2073 #if defined (JOB_CONTROL)
2074 /* This can cause a problem when reading a command as the result
2075 of a trap, when the trap is called from flush_child. This call
2076 had better not cause jobs to disappear from the job table in
2077 that case, or we will have big trouble. */
2078 notify_and_cleanup ();
2079 #else /* !JOB_CONTROL */
2080 cleanup_dead_jobs ();
2081 #endif /* !JOB_CONTROL */
2084 #if defined (READLINE)
2085 if (no_line_editing && SHOULD_PROMPT())
2087 if (SHOULD_PROMPT())
2091 if (bash_input.type == st_stream)
2098 /* Allow immediate exit if interrupted during input. */
2104 internal_warning ("shell_getc: ignored null byte in input");
2109 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
2113 if (bash_input.type == st_stream)
2117 shell_input_line_terminator = EOF;
2119 shell_input_line[i] = '\0';
2123 shell_input_line[i++] = c;
2127 shell_input_line[--i] = '\0';
2128 current_command_line_count++;
2133 shell_input_line_index = 0;
2134 shell_input_line_len = i; /* == strlen (shell_input_line) */
2136 set_line_mbstate ();
2138 #if defined (HISTORY)
2139 if (remember_on_history && shell_input_line && shell_input_line[0])
2142 # if defined (BANG_HISTORY)
2145 /* If the current delimiter is a single quote, we should not be
2146 performing history expansion, even if we're on a different
2147 line from the original single quote. */
2148 old_hist = history_expansion_inhibited;
2149 if (current_delimiter (dstack) == '\'')
2150 history_expansion_inhibited = 1;
2152 expansions = pre_process_line (shell_input_line, 1, 1);
2153 # if defined (BANG_HISTORY)
2154 history_expansion_inhibited = old_hist;
2156 if (expansions != shell_input_line)
2158 free (shell_input_line);
2159 shell_input_line = expansions;
2160 shell_input_line_len = shell_input_line ?
2161 strlen (shell_input_line) : 0;
2162 if (!shell_input_line_len)
2163 current_command_line_count--;
2165 /* We have to force the xrealloc below because we don't know
2166 the true allocated size of shell_input_line anymore. */
2167 shell_input_line_size = shell_input_line_len;
2169 set_line_mbstate ();
2172 /* Try to do something intelligent with blank lines encountered while
2173 entering multi-line commands. XXX - this is grotesque */
2174 else if (remember_on_history && shell_input_line &&
2175 shell_input_line[0] == '\0' &&
2176 current_command_line_count > 1)
2178 if (current_delimiter (dstack))
2179 /* We know shell_input_line[0] == 0 and we're reading some sort of
2180 quoted string. This means we've got a line consisting of only
2181 a newline in a quoted string. We want to make sure this line
2182 gets added to the history. */
2183 maybe_add_history (shell_input_line);
2187 hdcs = history_delimiting_chars ();
2188 if (hdcs && hdcs[0] == ';')
2189 maybe_add_history (shell_input_line);
2193 #endif /* HISTORY */
2195 if (shell_input_line)
2197 /* Lines that signify the end of the shell's input should not be
2199 if (echo_input_at_read && (shell_input_line[0] ||
2200 shell_input_line_terminator != EOF))
2201 fprintf (stderr, "%s\n", shell_input_line);
2205 shell_input_line_size = 0;
2206 prompt_string_pointer = ¤t_prompt_string;
2207 if (SHOULD_PROMPT ())
2212 /* Add the newline to the end of this string, iff the string does
2213 not already end in an EOF character. */
2214 if (shell_input_line_terminator != EOF)
2216 if (shell_input_line_len + 3 > shell_input_line_size)
2217 shell_input_line = (char *)xrealloc (shell_input_line,
2218 1 + (shell_input_line_size += 2));
2220 shell_input_line[shell_input_line_len] = '\n';
2221 shell_input_line[shell_input_line_len + 1] = '\0';
2223 set_line_mbstate ();
2227 uc = shell_input_line[shell_input_line_index];
2230 shell_input_line_index++;
2232 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2233 /* If UC is NULL, we have reached the end of the current input string. If
2234 pushed_string_list is non-empty, it's time to pop to the previous string
2235 because we have fully consumed the result of the last alias expansion.
2236 Do it transparently; just return the next character of the string popped
2238 if (!uc && (pushed_string_list != (STRING_SAVER *)NULL))
2241 uc = shell_input_line[shell_input_line_index];
2243 shell_input_line_index++;
2245 #endif /* ALIAS || DPAREN_ARITHMETIC */
2247 if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n')
2249 if (SHOULD_PROMPT ())
2255 if (!uc && shell_input_line_terminator == EOF)
2256 return ((shell_input_line_index != 0) ? '\n' : EOF);
2261 /* Put C back into the input for the shell. This might need changes for
2262 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
2263 character different than we read, shell_input_line_property doesn't need
2264 to change when manipulating shell_input_line. The define for
2265 last_shell_getc_is_singlebyte should take care of it, though. */
2270 if (shell_input_line && shell_input_line_index)
2271 shell_input_line[--shell_input_line_index] = c;
2273 eol_ungetc_lookahead = c;
2276 #ifdef INCLUDE_UNUSED
2277 /* Back the input pointer up by one, effectively `ungetting' a character. */
2281 if (shell_input_line && shell_input_line_index)
2282 shell_input_line_index--;
2286 /* Discard input until CHARACTER is seen, then push that character back
2287 onto the input stream. */
2289 discard_until (character)
2294 while ((c = shell_getc (0)) != EOF && c != character)
2302 execute_variable_command (command, vname)
2303 char *command, *vname;
2306 sh_parser_state_t ps;
2308 save_parser_state (&ps);
2309 last_lastarg = get_string_value ("_");
2311 last_lastarg = savestring (last_lastarg);
2313 parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST);
2315 restore_parser_state (&ps);
2316 bind_variable ("_", last_lastarg, 0);
2317 FREE (last_lastarg);
2319 if (token_to_read == '\n') /* reset_parser was called */
2323 /* Place to remember the token. We try to keep the buffer
2324 at a reasonable size, but it can grow. */
2325 static char *token = (char *)NULL;
2327 /* Current size of the token buffer. */
2328 static int token_buffer_size;
2330 /* Command to read_token () explaining what we want it to do. */
2333 #define prompt_is_ps1 \
2334 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
2336 /* Function for yyparse to call. yylex keeps track of
2337 the last two tokens read, and calls read_token. */
2341 if (interactive && (current_token == 0 || current_token == '\n'))
2343 /* Before we print a prompt, we might have to check mailboxes.
2344 We do this only if it is time to do so. Notice that only here
2345 is the mail alarm reset; nothing takes place in check_mail ()
2346 except the checking of mail. Please don't change this. */
2347 if (prompt_is_ps1 && time_to_check_mail ())
2350 reset_mail_timer ();
2353 /* Avoid printing a prompt if we're not going to read anything, e.g.
2354 after resetting the parser with read_token (RESET). */
2355 if (token_to_read == 0 && SHOULD_PROMPT ())
2359 two_tokens_ago = token_before_that;
2360 token_before_that = last_read_token;
2361 last_read_token = current_token;
2362 current_token = read_token (READ);
2364 if ((parser_state & PST_EOFTOKEN) && current_token == shell_eof_token)
2366 current_token = yacc_EOF;
2367 if (bash_input.type == st_string)
2368 rewind_input_string ();
2370 parser_state &= ~PST_EOFTOKEN;
2372 return (current_token);
2375 /* When non-zero, we have read the required tokens
2376 which allow ESAC to be the next one read. */
2377 static int esacs_needed_count;
2380 gather_here_documents ()
2385 while (need_here_doc)
2387 parser_state |= PST_HEREDOC;
2388 make_here_document (redir_stack[r++], line_number);
2389 parser_state &= ~PST_HEREDOC;
2394 /* When non-zero, an open-brace used to create a group is awaiting a close
2396 static int open_brace_count;
2398 #define command_token_position(token) \
2399 (((token) == ASSIGNMENT_WORD) || \
2400 ((token) != SEMI_SEMI && (token) != SEMI_AND && (token) != SEMI_SEMI_AND && reserved_word_acceptable(token)))
2402 #define assignment_acceptable(token) \
2403 (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0))
2405 /* Check to see if TOKEN is a reserved word and return the token
2407 #define CHECK_FOR_RESERVED_WORD(tok) \
2409 if (!dollar_present && !quoted && \
2410 reserved_word_acceptable (last_read_token)) \
2413 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
2414 if (STREQ (tok, word_token_alist[i].word)) \
2416 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
2418 if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \
2420 if (word_token_alist[i].token == ESAC) \
2421 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
2422 else if (word_token_alist[i].token == CASE) \
2423 parser_state |= PST_CASESTMT; \
2424 else if (word_token_alist[i].token == COND_END) \
2425 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
2426 else if (word_token_alist[i].token == COND_START) \
2427 parser_state |= PST_CONDCMD; \
2428 else if (word_token_alist[i].token == '{') \
2429 open_brace_count++; \
2430 else if (word_token_alist[i].token == '}' && open_brace_count) \
2431 open_brace_count--; \
2432 return (word_token_alist[i].token); \
2439 /* OK, we have a token. Let's try to alias expand it, if (and only if)
2442 It is eligible for expansion if EXPAND_ALIASES is set, and
2443 the token is unquoted and the last token read was a command
2444 separator (or expand_next_token is set), and we are currently
2445 processing an alias (pushed_string_list is non-empty) and this
2446 token is not the same as the current or any previously
2449 Special cases that disqualify:
2450 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
2460 r = xmalloc (l + 2);
2469 alias_expand_token (tokstr)
2475 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
2476 (parser_state & PST_CASEPAT) == 0)
2478 ap = find_alias (tokstr);
2480 /* Currently expanding this token. */
2481 if (ap && (ap->flags & AL_BEINGEXPANDED))
2482 return (NO_EXPANSION);
2484 /* mk_alexpansion puts an extra space on the end of the alias expansion,
2485 so the lookahead by the parser works right. If this gets changed,
2486 make sure the code in shell_getc that deals with reaching the end of
2487 an expanded alias is changed with it. */
2488 expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL;
2492 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
2493 return (RE_READ_TOKEN);
2496 /* This is an eligible token that does not have an expansion. */
2497 return (NO_EXPANSION);
2499 return (NO_EXPANSION);
2504 time_command_acceptable ()
2506 #if defined (COMMAND_TIMING)
2507 switch (last_read_token)
2526 #endif /* COMMAND_TIMING */
2529 /* Handle special cases of token recognition:
2530 IN is recognized if the last token was WORD and the token
2531 before that was FOR or CASE or SELECT.
2533 DO is recognized if the last token was WORD and the token
2534 before that was FOR or SELECT.
2536 ESAC is recognized if the last token caused `esacs_needed_count'
2539 `{' is recognized if the last token as WORD and the token
2540 before that was FUNCTION, or if we just parsed an arithmetic
2543 `}' is recognized if there is an unclosed `{' present.
2545 `-p' is returned as TIMEOPT if the last read token was TIME.
2547 ']]' is returned as COND_END if the parser is currently parsing
2548 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
2550 `time' is returned as TIME if and only if it is immediately
2551 preceded by one of `;', `\n', `||', `&&', or `&'.
2555 special_case_tokens (tokstr)
2558 if ((last_read_token == WORD) &&
2559 #if defined (SELECT_COMMAND)
2560 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
2562 ((token_before_that == FOR) || (token_before_that == CASE)) &&
2564 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
2566 if (token_before_that == CASE)
2568 parser_state |= PST_CASEPAT;
2569 esacs_needed_count++;
2574 if (last_read_token == WORD &&
2575 #if defined (SELECT_COMMAND)
2576 (token_before_that == FOR || token_before_that == SELECT) &&
2578 (token_before_that == FOR) &&
2580 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
2583 /* Ditto for ESAC in the CASE case.
2584 Specifically, this handles "case word in esac", which is a legal
2585 construct, certainly because someone will pass an empty arg to the
2586 case construct, and we don't want it to barf. Of course, we should
2587 insist that the case construct has at least one pattern in it, but
2588 the designers disagree. */
2589 if (esacs_needed_count)
2591 esacs_needed_count--;
2592 if (STREQ (tokstr, "esac"))
2594 parser_state &= ~PST_CASEPAT;
2599 /* The start of a shell function definition. */
2600 if (parser_state & PST_ALLOWOPNBRC)
2602 parser_state &= ~PST_ALLOWOPNBRC;
2603 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2606 function_bstart = line_number;
2607 return ('{'); /* } */
2611 /* We allow a `do' after a for ((...)) without an intervening
2613 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
2615 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2618 return ('{'); /* } */
2621 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
2623 open_brace_count--; /* { */
2627 #if defined (COMMAND_TIMING)
2628 /* Handle -p after `time'. */
2629 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
2634 #if defined (COMMAND_TIMING)
2635 if (STREQ (token, "time") && ((parser_state & PST_CASEPAT) == 0) && time_command_acceptable ())
2637 #endif /* COMMAND_TIMING */
2640 #if defined (COND_COMMAND) /* [[ */
2641 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
2648 /* Called from shell.c when Control-C is typed at top level. Or
2649 by the error rule at top level. */
2653 dstack.delimiter_depth = 0; /* No delimiters found so far. */
2654 open_brace_count = 0;
2658 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2659 if (pushed_string_list)
2660 free_string_list ();
2661 #endif /* ALIAS || DPAREN_ARITHMETIC */
2663 if (shell_input_line)
2665 free (shell_input_line);
2666 shell_input_line = (char *)NULL;
2667 shell_input_line_size = shell_input_line_index = 0;
2670 FREE (word_desc_to_read);
2671 word_desc_to_read = (WORD_DESC *)NULL;
2673 current_token = '\n'; /* XXX */
2674 last_read_token = '\n';
2675 token_to_read = '\n';
2678 /* Read the next token. Command can be READ (normal operation) or
2679 RESET (to normalize state). */
2681 read_token (command)
2684 int character; /* Current character. */
2685 int peek_char; /* Temporary look-ahead character. */
2686 int result; /* The thing to return. */
2688 if (command == RESET)
2696 result = token_to_read;
2697 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
2699 yylval.word = word_desc_to_read;
2700 word_desc_to_read = (WORD_DESC *)NULL;
2706 #if defined (COND_COMMAND)
2707 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
2709 cond_lineno = line_number;
2710 parser_state |= PST_CONDEXPR;
2711 yylval.command = parse_cond_command ();
2712 if (cond_token != COND_END)
2717 token_to_read = COND_END;
2718 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
2724 /* This is a place to jump back to once we have successfully expanded a
2725 token with an alias and pushed the string with push_string () */
2729 /* Read a single word from input. Start by skipping blanks. */
2730 while ((character = shell_getc (1)) != EOF && shellblank (character))
2733 if (character == EOF)
2739 if MBTEST(character == '#' && (!interactive || interactive_comments))
2741 /* A comment. Discard until EOL or EOF, and then return a newline. */
2742 discard_until ('\n');
2744 character = '\n'; /* this will take the next if statement and return. */
2747 if (character == '\n')
2749 /* If we're about to return an unquoted newline, we can go and collect
2750 the text of any pending here document. */
2752 gather_here_documents ();
2755 parser_state &= ~PST_ALEXPNEXT;
2758 parser_state &= ~PST_ASSIGNOK;
2763 if (parser_state & PST_REGEXP)
2766 /* Shell meta-characters. */
2767 if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
2770 /* Turn off alias tokenization iff this character sequence would
2771 not leave us ready to read a command. */
2772 if (character == '<' || character == '>')
2773 parser_state &= ~PST_ALEXPNEXT;
2776 parser_state &= ~PST_ASSIGNOK;
2778 peek_char = shell_getc (1);
2779 if (character == peek_char)
2784 /* If '<' then we could be at "<<" or at "<<-". We have to
2785 look ahead one more character. */
2786 peek_char = shell_getc (1);
2787 if MBTEST(peek_char == '-')
2788 return (LESS_LESS_MINUS);
2789 else if MBTEST(peek_char == '<')
2790 return (LESS_LESS_LESS);
2793 shell_ungetc (peek_char);
2798 return (GREATER_GREATER);
2801 parser_state |= PST_CASEPAT;
2803 parser_state &= ~PST_ALEXPNEXT;
2806 peek_char = shell_getc (1);
2807 if MBTEST(peek_char == '&')
2808 return (SEMI_SEMI_AND);
2811 shell_ungetc (peek_char);
2821 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2823 result = parse_dparen (character);
2831 else if MBTEST(character == '<' && peek_char == '&')
2833 else if MBTEST(character == '>' && peek_char == '&')
2834 return (GREATER_AND);
2835 else if MBTEST(character == '<' && peek_char == '>')
2836 return (LESS_GREATER);
2837 else if MBTEST(character == '>' && peek_char == '|')
2838 return (GREATER_BAR);
2839 else if MBTEST(character == '&' && peek_char == '>')
2841 peek_char = shell_getc (1);
2842 if MBTEST(peek_char == '>')
2843 return (AND_GREATER_GREATER);
2846 shell_ungetc (peek_char);
2847 return (AND_GREATER);
2850 else if MBTEST(character == '|' && peek_char == '&')
2852 else if MBTEST(character == ';' && peek_char == '&')
2854 parser_state |= PST_CASEPAT;
2856 parser_state &= ~PST_ALEXPNEXT;
2861 shell_ungetc (peek_char);
2863 /* If we look like we are reading the start of a function
2864 definition, then let the reader know about it so that
2865 we will do the right thing with `{'. */
2866 if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
2868 parser_state |= PST_ALLOWOPNBRC;
2870 parser_state &= ~PST_ALEXPNEXT;
2872 function_dstart = line_number;
2875 /* case pattern lists may be preceded by an optional left paren. If
2876 we're not trying to parse a case pattern list, the left paren
2877 indicates a subshell. */
2878 if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
2879 parser_state |= PST_SUBSHELL;
2881 else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
2882 parser_state &= ~PST_CASEPAT;
2884 else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
2885 parser_state &= ~PST_SUBSHELL;
2887 #if defined (PROCESS_SUBSTITUTION)
2888 /* Check for the constructs which introduce process substitution.
2889 Shells running in `posix mode' don't do process substitution. */
2890 if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/
2891 #endif /* PROCESS_SUBSTITUTION */
2895 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
2896 if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
2900 /* Okay, if we got this far, we have to read a word. Read one,
2901 and then check it against the known ones. */
2902 result = read_token_word (character);
2904 if (result == RE_READ_TOKEN)
2911 * Match a $(...) or other grouping construct. This has to handle embedded
2912 * quoted strings ('', ``, "") and nested constructs. It also must handle
2913 * reprompting the user, if necessary, after reading a newline, and returning
2914 * correct error values if it reads EOF.
2916 #define P_FIRSTCLOSE 0x01
2917 #define P_ALLOWESC 0x02
2918 #define P_DQUOTE 0x04
2919 #define P_COMMAND 0x08 /* parsing a command, so look for comments */
2920 #define P_BACKQUOTE 0x10 /* parsing a backquoted command substitution */
2921 #define P_ARRAYSUB 0x20 /* parsing a [...] array subscript for assignment */
2923 /* Lexical state while parsing a grouping construct or $(...). */
2924 #define LEX_WASDOL 0x001
2925 #define LEX_CKCOMMENT 0x002
2926 #define LEX_INCOMMENT 0x004
2927 #define LEX_PASSNEXT 0x008
2928 #define LEX_RESWDOK 0x010
2929 #define LEX_CKCASE 0x020
2930 #define LEX_INCASE 0x040
2931 #define LEX_INHEREDOC 0x080
2932 #define LEX_HEREDELIM 0x100 /* reading here-doc delimiter */
2933 #define LEX_STRIPDOC 0x200 /* <<- strip tabs from here doc delim */
2934 #define LEX_INWORD 0x400
2936 #define COMSUB_META(ch) ((ch) == ';' || (ch) == '&' || (ch) == '|')
2938 #define CHECK_NESTRET_ERROR() \
2940 if (nestret == &matched_pair_error) \
2943 return &matched_pair_error; \
2947 #define APPEND_NESTRET() \
2951 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); \
2952 strcpy (ret + retind, nestret); \
2953 retind += nestlen; \
2957 static char matched_pair_error;
2960 parse_matched_pair (qc, open, close, lenp, flags)
2961 int qc; /* `"' if this construct is within double quotes */
2965 int count, ch, tflags;
2966 int nestlen, ttranslen, start_lineno;
2967 char *ret, *nestret, *ttrans;
2968 int retind, retsize, rflags;
2970 /*itrace("parse_matched_pair: open = %c close = %c flags = %d", open, close, flags); */
2974 if ((flags & P_COMMAND) && qc != '`' && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
2975 tflags |= LEX_CKCOMMENT;
2977 /* RFLAGS is the set of flags we want to pass to recursive calls. */
2978 rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE);
2980 ret = (char *)xmalloc (retsize = 64);
2983 start_lineno = line_number;
2986 ch = shell_getc (qc != '\'' && (tflags & LEX_PASSNEXT) == 0);
2991 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
2992 EOF_Reached = 1; /* XXX */
2993 return (&matched_pair_error);
2996 /* Possible reprompting. */
2997 if (ch == '\n' && SHOULD_PROMPT ())
3000 /* Don't bother counting parens or doing anything else if in a comment
3001 or part of a case statement */
3002 if (tflags & LEX_INCOMMENT)
3004 /* Add this character. */
3005 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3009 tflags &= ~LEX_INCOMMENT;
3014 /* Not exactly right yet, should handle shell metacharacters, too. If
3015 any changes are made to this test, make analogous changes to subst.c:
3016 extract_delimited_string(). */
3017 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (retind == 0 || ret[retind-1] == '\n' || shellblank (ret[retind - 1])))
3018 tflags |= LEX_INCOMMENT;
3020 if (tflags & LEX_PASSNEXT) /* last char was backslash */
3022 tflags &= ~LEX_PASSNEXT;
3023 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
3026 retind--; /* swallow previously-added backslash */
3030 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3031 if MBTEST(ch == CTLESC || ch == CTLNUL)
3032 ret[retind++] = CTLESC;
3036 /* If we're reparsing the input (e.g., from parse_string_to_word_list),
3037 we've already prepended CTLESC to single-quoted results of $'...'.
3038 We may want to do this for other CTLESC-quoted characters in
3040 else if MBTEST((parser_state & PST_REPARSE) && open == '\'' && (ch == CTLESC || ch == CTLNUL))
3042 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3046 else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
3048 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3049 ret[retind++] = CTLESC;
3053 else if MBTEST(ch == close) /* ending delimiter */
3055 /* handle nested ${...} specially. */
3056 else if MBTEST(open != close && (tflags & LEX_WASDOL) && open == '{' && ch == open) /* } */
3058 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
3061 /* Add this character. */
3062 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3065 /* If we just read the ending character, don't bother continuing. */
3069 if (open == '\'') /* '' inside grouping construct */
3071 if MBTEST((flags & P_ALLOWESC) && ch == '\\')
3072 tflags |= LEX_PASSNEXT;
3076 if MBTEST(ch == '\\') /* backslashes */
3077 tflags |= LEX_PASSNEXT;
3080 /* The big hammer. Single quotes aren't special in double quotes. The
3081 problem is that Posix says the single quotes are semi-special:
3082 within a double-quoted ${...} construct "an even number of
3083 unescaped double-quotes or single-quotes, if any, shall occur." */
3084 if MBTEST(open == '{' && (flags & P_DQUOTE) && ch == '\'') /* } */
3088 /* Could also check open == '`' if we want to parse grouping constructs
3089 inside old-style command substitution. */
3090 if (open != close) /* a grouping construct */
3092 if MBTEST(shellquote (ch))
3094 /* '', ``, or "" inside $(...) or other grouping construct. */
3095 push_delimiter (dstack, ch);
3096 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
3097 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
3099 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
3100 pop_delimiter (dstack);
3101 CHECK_NESTRET_ERROR ();
3103 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
3105 /* Translate $'...' here. */
3106 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
3109 if ((rflags & P_DQUOTE) == 0)
3111 nestret = sh_single_quote (ttrans);
3113 nestlen = strlen (nestret);
3118 nestlen = ttranslen;
3120 retind -= 2; /* back up before the $' */
3122 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
3124 /* Locale expand $"..." here. */
3125 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
3128 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
3130 nestlen = ttranslen + 2;
3131 retind -= 2; /* back up before the $" */
3137 else if ((flags & P_ARRAYSUB) && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3138 goto parse_dollar_word;
3140 /* Parse an old-style command substitution within double quotes as a
3142 /* XXX - sh and ksh93 don't do this - XXX */
3143 else if MBTEST(open == '"' && ch == '`')
3145 nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags);
3147 CHECK_NESTRET_ERROR ();
3152 else if MBTEST(open != '`' && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3153 /* check for $(), $[], or ${} inside quoted string. */
3156 if (open == ch) /* undo previous increment */
3158 if (ch == '(') /* ) */
3159 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
3160 else if (ch == '{') /* } */
3161 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|rflags);
3162 else if (ch == '[') /* ] */
3163 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
3165 CHECK_NESTRET_ERROR ();
3170 if MBTEST(ch == '$')
3171 tflags |= LEX_WASDOL;
3173 tflags &= ~LEX_WASDOL;
3182 /* Parse a $(...) command substitution. This is messier than I'd like, and
3183 reproduces a lot more of the token-reading code than I'd like. */
3185 parse_comsub (qc, open, close, lenp, flags)
3186 int qc; /* `"' if this construct is within double quotes */
3190 int count, ch, peekc, tflags, lex_rwlen, lex_wlen, lex_firstind;
3191 int nestlen, ttranslen, start_lineno;
3192 char *ret, *nestret, *ttrans, *heredelim;
3193 int retind, retsize, rflags, hdlen;
3195 /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/
3197 tflags = LEX_RESWDOK;
3199 if ((flags & P_COMMAND) && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
3200 tflags |= LEX_CKCASE;
3201 if ((tflags & LEX_CKCASE) && (interactive == 0 || interactive_comments))
3202 tflags |= LEX_CKCOMMENT;
3204 /* RFLAGS is the set of flags we want to pass to recursive calls. */
3205 rflags = (flags & P_DQUOTE);
3207 ret = (char *)xmalloc (retsize = 64);
3210 start_lineno = line_number;
3211 lex_rwlen = lex_wlen = 0;
3219 ch = shell_getc (qc != '\'' && (tflags & LEX_PASSNEXT) == 0);
3226 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
3227 EOF_Reached = 1; /* XXX */
3228 return (&matched_pair_error);
3231 /* If we hit the end of a line and are reading the contents of a here
3232 document, and it's not the same line that the document starts on,
3233 check for this line being the here doc delimiter. Otherwise, if
3234 we're in a here document, mark the next character as the beginning
3238 if ((tflags & LEX_HEREDELIM) && heredelim)
3240 tflags &= ~LEX_HEREDELIM;
3241 tflags |= LEX_INHEREDOC;
3242 lex_firstind = retind + 1;
3244 else if (tflags & LEX_INHEREDOC)
3247 tind = lex_firstind;
3248 while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t')
3250 if (STREQN (ret + tind, heredelim, hdlen))
3252 tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC);
3253 /*itrace("parse_comsub:%d: found here doc end `%s'", line_number, ret + tind);*/
3257 lex_firstind = retind + 1;
3261 /* Possible reprompting. */
3262 if (ch == '\n' && SHOULD_PROMPT ())
3265 /* Don't bother counting parens or doing anything else if in a comment */
3266 if (tflags & (LEX_INCOMMENT|LEX_INHEREDOC))
3268 /* Add this character. */
3269 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3272 if ((tflags & LEX_INCOMMENT) && ch == '\n')
3274 /*itrace("parse_comsub:%d: lex_incomment -> 0 ch = `%c'", line_number, ch);*/
3275 tflags &= ~LEX_INCOMMENT;
3281 if (tflags & LEX_PASSNEXT) /* last char was backslash */
3283 /*itrace("parse_comsub:%d: lex_passnext -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3284 tflags &= ~LEX_PASSNEXT;
3285 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
3288 retind--; /* swallow previously-added backslash */
3292 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3293 if MBTEST(ch == CTLESC || ch == CTLNUL)
3294 ret[retind++] = CTLESC;
3299 /* If this is a shell break character, we are not in a word. If not,
3300 we either start or continue a word. */
3301 if MBTEST(shellbreak (ch))
3303 tflags &= ~LEX_INWORD;
3304 /*itrace("parse_comsub:%d: lex_inword -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3308 if (tflags & LEX_INWORD)
3311 /*itrace("parse_comsub:%d: lex_inword == 1 ch = `%c' lex_wlen = %d (%d)", line_number, ch, lex_wlen, __LINE__);*/
3315 /*itrace("parse_comsub:%d: lex_inword -> 1 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3316 tflags |= LEX_INWORD;
3321 /* Skip whitespace */
3322 if MBTEST(shellblank (ch) && lex_rwlen == 0)
3324 /* Add this character. */
3325 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3330 /* Either we are looking for the start of the here-doc delimiter
3331 (lex_firstind == -1) or we are reading one (lex_firstind >= 0).
3332 If this character is a shell break character and we are reading
3333 the delimiter, save it and note that we are now reading a here
3334 document. If we've found the start of the delimiter, note it by
3335 setting lex_firstind. Backslashes can quote shell metacharacters
3336 in here-doc delimiters. */
3337 if (tflags & LEX_HEREDELIM)
3339 if (lex_firstind == -1 && shellbreak (ch) == 0)
3340 lex_firstind = retind;
3341 else if (lex_firstind >= 0 && (tflags & LEX_PASSNEXT) == 0 && shellbreak (ch))
3343 nestret = substring (ret, lex_firstind, retind);
3344 heredelim = string_quote_removal (nestret, 0);
3346 hdlen = STRLEN(heredelim);
3347 /*itrace("parse_comsub:%d: found here doc delimiter `%s' (%d)", line_number, heredelim, hdlen);*/
3350 tflags |= LEX_INHEREDOC;
3351 tflags &= ~LEX_HEREDELIM;
3352 lex_firstind = retind + 1;
3359 /* Meta-characters that can introduce a reserved word. Not perfect yet. */
3360 if MBTEST((tflags & LEX_RESWDOK) == 0 && (tflags & LEX_CKCASE) && (tflags & LEX_INCOMMENT) == 0 && shellmeta(ch))
3362 /* Add this character. */
3363 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3365 peekc = shell_getc (1);
3366 if (ch == peekc && (ch == '&' || ch == '|' || ch == ';')) /* two-character tokens */
3368 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3369 ret[retind++] = peekc;
3370 /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/
3371 tflags |= LEX_RESWDOK;
3375 else if (ch == '\n' || COMSUB_META(ch))
3377 shell_ungetc (peekc);
3378 /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/
3379 tflags |= LEX_RESWDOK;
3387 /* `unget' the character we just added and fall through */
3389 shell_ungetc (peekc);
3393 /* If we can read a reserved word, try to read one. */
3394 if (tflags & LEX_RESWDOK)
3396 if MBTEST(islower (ch))
3398 /* Add this character. */
3399 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3404 else if MBTEST(lex_rwlen == 4 && shellbreak (ch))
3406 if (STREQN (ret + retind - 4, "case", 4))
3408 tflags |= LEX_INCASE;
3409 /*itrace("parse_comsub:%d: found `case', lex_incase -> 1 lex_reswdok -> 0", line_number);*/
3411 else if (STREQN (ret + retind - 4, "esac", 4))
3413 tflags &= ~LEX_INCASE;
3414 /*itrace("parse_comsub:%d: found `esac', lex_incase -> 0 lex_reswdok -> 0", line_number);*/
3416 tflags &= ~LEX_RESWDOK;
3418 else if MBTEST((tflags & LEX_CKCOMMENT) && ch == '#' && (lex_rwlen == 0 || ((tflags & LEX_INWORD) && lex_wlen == 0)))
3419 ; /* don't modify LEX_RESWDOK if we're starting a comment */
3420 else if MBTEST((tflags & LEX_INCASE) && ch != '\n')
3421 /* If we can read a reserved word and we're in case, we're at the
3422 point where we can read a new pattern list or an esac. We
3423 handle the esac case above. If we read a newline, we want to
3424 leave LEX_RESWDOK alone. If we read anything else, we want to
3425 turn off LEX_RESWDOK, since we're going to read a pattern list. */
3427 tflags &= ~LEX_RESWDOK;
3428 /*itrace("parse_comsub:%d: lex_incase == 1 found `%c', lex_reswordok -> 0", line_number, ch);*/
3430 else if MBTEST(shellbreak (ch) == 0)
3432 tflags &= ~LEX_RESWDOK;
3433 /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/
3437 /* Might be the start of a here-doc delimiter */
3438 if MBTEST((tflags & LEX_INCOMMENT) == 0 && (tflags & LEX_CKCASE) && ch == '<')
3440 /* Add this character. */
3441 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3443 peekc = shell_getc (1);
3448 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3449 ret[retind++] = peekc;
3450 peekc = shell_getc (1);
3455 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3456 ret[retind++] = peekc;
3457 tflags |= LEX_STRIPDOC;
3460 shell_ungetc (peekc);
3463 tflags |= LEX_HEREDELIM;
3469 ch = peekc; /* fall through and continue XXX */
3471 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (((tflags & LEX_RESWDOK) && lex_rwlen == 0) || ((tflags & LEX_INWORD) && lex_wlen == 0)))
3473 /*itrace("parse_comsub:%d: lex_incomment -> 1 (%d)", line_number, __LINE__);*/
3474 tflags |= LEX_INCOMMENT;
3477 if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
3479 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3480 ret[retind++] = CTLESC;
3485 else if MBTEST((tflags & LEX_INCASE) && ch == close && close == ')')
3486 tflags &= ~LEX_INCASE; /* XXX */
3488 else if MBTEST(ch == close && (tflags & LEX_INCASE) == 0) /* ending delimiter */
3491 /*itrace("parse_comsub:%d: found close: count = %d", line_number, count);*/
3493 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && (tflags & LEX_INCASE) == 0 && ch == open) /* nested begin */
3496 /*itrace("parse_comsub:%d: found open: count = %d", line_number, count);*/
3499 /* Add this character. */
3500 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3503 /* If we just read the ending character, don't bother continuing. */
3507 if MBTEST(ch == '\\') /* backslashes */
3508 tflags |= LEX_PASSNEXT;
3510 if MBTEST(shellquote (ch))
3512 /* '', ``, or "" inside $(...). */
3513 push_delimiter (dstack, ch);
3514 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
3515 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
3517 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
3518 pop_delimiter (dstack);
3519 CHECK_NESTRET_ERROR ();
3521 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
3523 /* Translate $'...' here. */
3524 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
3527 if ((rflags & P_DQUOTE) == 0)
3529 nestret = sh_single_quote (ttrans);
3531 nestlen = strlen (nestret);
3536 nestlen = ttranslen;
3538 retind -= 2; /* back up before the $' */
3540 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
3542 /* Locale expand $"..." here. */
3543 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
3546 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
3548 nestlen = ttranslen + 2;
3549 retind -= 2; /* back up before the $" */
3555 else if MBTEST((tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3556 /* check for $(), $[], or ${} inside command substitution. */
3558 if ((tflags & LEX_INCASE) == 0 && open == ch) /* undo previous increment */
3560 if (ch == '(') /* ) */
3561 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
3562 else if (ch == '{') /* } */
3563 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|rflags);
3564 else if (ch == '[') /* ] */
3565 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
3567 CHECK_NESTRET_ERROR ();
3572 if MBTEST(ch == '$')
3573 tflags |= LEX_WASDOL;
3575 tflags &= ~LEX_WASDOL;
3582 /*itrace("parse_comsub:%d: returning `%s'", line_number, ret);*/
3586 /* XXX - this needs to handle functionality like subst.c:no_longjmp_on_fatal_error;
3587 maybe extract_command_subst should handle it. */
3589 xparse_dolparen (base, string, indp, flags)
3595 sh_parser_state_t ps;
3596 int orig_ind, nc, sflags;
3597 char *ret, *s, *ep, *ostring;
3603 sflags = SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOFREE;
3604 if (flags & SX_NOLONGJMP)
3605 sflags |= SEVAL_NOLONGJMP;
3606 save_parser_state (&ps);
3609 parser_state |= PST_CMDSUBST|PST_EOFTOKEN; /* allow instant ')' */ /*(*/
3610 shell_eof_token = ')';
3611 parse_string (string, "command substitution", sflags, &ep);
3613 restore_parser_state (&ps);
3618 /* Need to find how many characters parse_and_execute consumed, update
3619 *indp, if flags != 0, copy the portion of the string parsed into RET
3620 and return it. If flags & 1 (EX_NOALLOC) we can return NULL. */
3627 itrace("xparse_dolparen:%d: ep[-1] != RPAREN (%d), ep = `%s'", line_number, ep[-1], ep);
3629 while (ep > ostring && ep[-1] == '\n') ep--;
3633 *indp = ep - base - 1;
3637 if (base[*indp] != ')')
3638 itrace("xparse_dolparen:%d: base[%d] != RPAREN (%d), base = `%s'", line_number, *indp, base[*indp], base);
3641 if (flags & SX_NOALLOC)
3642 return (char *)NULL;
3650 ret = substring (ostring, 0, nc - 1);
3655 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
3656 /* Parse a double-paren construct. It can be either an arithmetic
3657 command, an arithmetic `for' command, or a nested subshell. Returns
3658 the parsed token, -1 on error, or -2 if we didn't do anything and
3659 should just go on. */
3668 #if defined (ARITH_FOR_COMMAND)
3669 if (last_read_token == FOR)
3671 arith_for_lineno = line_number;
3672 cmdtyp = parse_arith_cmd (&wval, 0);
3675 wd = alloc_word_desc ();
3677 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
3678 return (ARITH_FOR_EXPRS);
3681 return -1; /* ERROR */
3685 #if defined (DPAREN_ARITHMETIC)
3686 if (reserved_word_acceptable (last_read_token))
3688 sline = line_number;
3690 cmdtyp = parse_arith_cmd (&wval, 0);
3691 if (cmdtyp == 1) /* arithmetic command */
3693 wd = alloc_word_desc ();
3695 wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE;
3696 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
3699 else if (cmdtyp == 0) /* nested subshell */
3701 push_string (wval, 0, (alias_t *)NULL);
3702 if ((parser_state & PST_CASEPAT) == 0)
3703 parser_state |= PST_SUBSHELL;
3711 return -2; /* XXX */
3714 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
3715 If not, assume it's a nested subshell for backwards compatibility and
3716 return 0. In any case, put the characters we've consumed into a locally-
3717 allocated buffer and make *ep point to that buffer. Return -1 on an
3718 error, for example EOF. */
3720 parse_arith_cmd (ep, adddq)
3724 int exp_lineno, rval, c;
3725 char *ttok, *tokstr;
3728 exp_lineno = line_number;
3729 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
3731 if (ttok == &matched_pair_error)
3733 /* Check that the next character is the closing right paren. If
3734 not, this is a syntax error. ( */
3739 tokstr = (char *)xmalloc (ttoklen + 4);
3741 /* if ADDDQ != 0 then (( ... )) -> "..." */
3742 if (rval == 1 && adddq) /* arith cmd, add double quotes */
3745 strncpy (tokstr + 1, ttok, ttoklen - 1);
3746 tokstr[ttoklen] = '"';
3747 tokstr[ttoklen+1] = '\0';
3749 else if (rval == 1) /* arith cmd, don't add double quotes */
3751 strncpy (tokstr, ttok, ttoklen - 1);
3752 tokstr[ttoklen-1] = '\0';
3754 else /* nested subshell */
3757 strncpy (tokstr + 1, ttok, ttoklen - 1);
3758 tokstr[ttoklen] = ')';
3759 tokstr[ttoklen+1] = c;
3760 tokstr[ttoklen+2] = '\0';
3767 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
3769 #if defined (COND_COMMAND)
3775 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
3776 parser_error (cond_lineno, _("unexpected EOF while looking for `]]'"));
3777 else if (cond_token != COND_ERROR)
3779 if (etext = error_token_from_token (cond_token))
3781 parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext);
3785 parser_error (cond_lineno, _("syntax error in conditional expression"));
3792 return (cond_or ());
3801 if (cond_token == OR_OR)
3804 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
3815 if (cond_token == AND_AND)
3818 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
3824 cond_skip_newlines ()
3826 while ((cond_token = read_token (READ)) == '\n')
3828 if (SHOULD_PROMPT ())
3831 return (cond_token);
3834 #define COND_RETURN_ERROR() \
3835 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
3841 COND_COM *term, *tleft, *tright;
3845 /* Read a token. It can be a left paren, a `!', a unary operator, or a
3846 word that should be the first argument of a binary operator. Start by
3847 skipping newlines, since this is a compound command. */
3848 tok = cond_skip_newlines ();
3849 lineno = line_number;
3850 if (tok == COND_END)
3852 COND_RETURN_ERROR ();
3854 else if (tok == '(')
3856 term = cond_expr ();
3857 if (cond_token != ')')
3860 dispose_cond_node (term); /* ( */
3861 if (etext = error_token_from_token (cond_token))
3863 parser_error (lineno, _("unexpected token `%s', expected `)'"), etext);
3867 parser_error (lineno, _("expected `)'"));
3868 COND_RETURN_ERROR ();
3870 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
3871 (void)cond_skip_newlines ();
3873 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
3876 dispose_word (yylval.word); /* not needed */
3877 term = cond_term ();
3879 term->flags |= CMD_INVERT_RETURN;
3881 else if (tok == WORD && yylval.word->word[0] == '-' && yylval.word->word[2] == 0 && test_unop (yylval.word->word))
3884 tok = read_token (READ);
3887 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3888 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
3893 if (etext = error_token_from_token (tok))
3895 parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext);
3899 parser_error (line_number, _("unexpected argument to conditional unary operator"));
3900 COND_RETURN_ERROR ();
3903 (void)cond_skip_newlines ();
3905 else if (tok == WORD) /* left argument to binary operator */
3908 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3911 tok = read_token (READ);
3912 if (tok == WORD && test_binop (yylval.word->word))
3914 #if defined (COND_REGEXP)
3915 else if (tok == WORD && STREQ (yylval.word->word, "=~"))
3918 parser_state |= PST_REGEXP;
3921 else if (tok == '<' || tok == '>')
3922 op = make_word_from_token (tok); /* ( */
3923 /* There should be a check before blindly accepting the `)' that we have
3924 seen the opening `('. */
3925 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
3927 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
3928 the test command. Similarly for [[ x && expr ]] or
3929 [[ x || expr ]] or [[ (x) ]]. */
3930 op = make_word ("-n");
3931 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
3937 if (etext = error_token_from_token (tok))
3939 parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext);
3943 parser_error (line_number, _("conditional binary operator expected"));
3944 dispose_cond_node (tleft);
3945 COND_RETURN_ERROR ();
3949 tok = read_token (READ);
3950 parser_state &= ~PST_REGEXP;
3953 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3954 term = make_cond_node (COND_BINARY, op, tleft, tright);
3958 if (etext = error_token_from_token (tok))
3960 parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext);
3964 parser_error (line_number, _("unexpected argument to conditional binary operator"));
3965 dispose_cond_node (tleft);
3967 COND_RETURN_ERROR ();
3970 (void)cond_skip_newlines ();
3975 parser_error (line_number, _("unexpected token `%c' in conditional command"), tok);
3976 else if (etext = error_token_from_token (tok))
3978 parser_error (line_number, _("unexpected token `%s' in conditional command"), etext);
3982 parser_error (line_number, _("unexpected token %d in conditional command"), tok);
3983 COND_RETURN_ERROR ();
3988 /* This is kind of bogus -- we slip a mini recursive-descent parser in
3989 here to handle the conditional statement syntax. */
3991 parse_cond_command ()
3995 cexp = cond_expr ();
3996 return (make_cond_command (cexp));
4000 #if defined (ARRAY_VARS)
4001 /* When this is called, it's guaranteed that we don't care about anything
4002 in t beyond i. We do save and restore the chars, though. */
4004 token_is_assignment (t, i)
4008 unsigned char c, c1;
4011 c = t[i]; c1 = t[i+1];
4012 t[i] = '='; t[i+1] = '\0';
4013 r = assignment (t, (parser_state & PST_COMPASSIGN) != 0);
4014 t[i] = c; t[i+1] = c1;
4018 /* XXX - possible changes here for `+=' */
4020 token_is_ident (t, i)
4029 r = legal_identifier (t);
4036 read_token_word (character)
4039 /* The value for YYLVAL when a WORD is read. */
4040 WORD_DESC *the_word;
4042 /* Index into the token that we are building. */
4045 /* ALL_DIGITS becomes zero when we see a non-digit. */
4046 int all_digit_token;
4048 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
4051 /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
4053 int compound_assignment;
4055 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
4058 /* Non-zero means to ignore the value of the next character, and just
4059 to add it no matter what. */
4060 int pass_next_character;
4062 /* The current delimiting character. */
4064 int result, peek_char;
4065 char *ttok, *ttrans;
4066 int ttoklen, ttranslen;
4069 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
4070 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
4073 all_digit_token = DIGIT (character);
4074 dollar_present = quoted = pass_next_character = compound_assignment = 0;
4078 if (character == EOF)
4081 if (pass_next_character)
4083 pass_next_character = 0;
4084 goto got_escaped_character;
4087 cd = current_delimiter (dstack);
4089 /* Handle backslashes. Quote lots of things when not inside of
4090 double-quotes, quote some things inside of double-quotes. */
4091 if MBTEST(character == '\\')
4093 peek_char = shell_getc (0);
4095 /* Backslash-newline is ignored in all cases except
4096 when quoted with single quotes. */
4097 if (peek_char == '\n')
4100 goto next_character;
4104 shell_ungetc (peek_char);
4106 /* If the next character is to be quoted, note it now. */
4107 if (cd == 0 || cd == '`' ||
4108 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
4109 pass_next_character++;
4116 /* Parse a matched pair of quote characters. */
4117 if MBTEST(shellquote (character))
4119 push_delimiter (dstack, character);
4120 ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0);
4121 pop_delimiter (dstack);
4122 if (ttok == &matched_pair_error)
4123 return -1; /* Bail immediately. */
4124 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4125 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4126 token[token_index++] = character;
4127 strcpy (token + token_index, ttok);
4128 token_index += ttoklen;
4129 all_digit_token = 0;
4131 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
4133 goto next_character;
4137 /* When parsing a regexp as a single word inside a conditional command,
4138 we need to special-case characters special to both the shell and
4139 regular expressions. Right now, that is only '(' and '|'. */ /*)*/
4140 if MBTEST((parser_state & PST_REGEXP) && (character == '(' || character == '|')) /*)*/
4142 if (character == '|')
4145 push_delimiter (dstack, character);
4146 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4147 pop_delimiter (dstack);
4148 if (ttok == &matched_pair_error)
4149 return -1; /* Bail immediately. */
4150 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4151 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4152 token[token_index++] = character;
4153 strcpy (token + token_index, ttok);
4154 token_index += ttoklen;
4156 dollar_present = all_digit_token = 0;
4157 goto next_character;
4159 #endif /* COND_REGEXP */
4161 #ifdef EXTENDED_GLOB
4162 /* Parse a ksh-style extended pattern matching specification. */
4163 if MBTEST(extended_glob && PATTERN_CHAR (character))
4165 peek_char = shell_getc (1);
4166 if MBTEST(peek_char == '(') /* ) */
4168 push_delimiter (dstack, peek_char);
4169 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4170 pop_delimiter (dstack);
4171 if (ttok == &matched_pair_error)
4172 return -1; /* Bail immediately. */
4173 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4175 TOKEN_DEFAULT_GROW_SIZE);
4176 token[token_index++] = character;
4177 token[token_index++] = peek_char;
4178 strcpy (token + token_index, ttok);
4179 token_index += ttoklen;
4181 dollar_present = all_digit_token = 0;
4182 goto next_character;
4185 shell_ungetc (peek_char);
4187 #endif /* EXTENDED_GLOB */
4189 /* If the delimiter character is not single quote, parse some of
4190 the shell expansions that must be read as a single word. */
4191 if (shellexp (character))
4193 peek_char = shell_getc (1);
4194 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
4195 if MBTEST(peek_char == '(' || \
4196 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
4198 if (peek_char == '{') /* } */
4199 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE);
4200 else if (peek_char == '(') /* ) */
4202 /* XXX - push and pop the `(' as a delimiter for use by
4203 the command-oriented-history code. This way newlines
4204 appearing in the $(...) string get added to the
4205 history literally rather than causing a possibly-
4206 incorrect `;' to be added. ) */
4207 push_delimiter (dstack, peek_char);
4208 ttok = parse_comsub (cd, '(', ')', &ttoklen, P_COMMAND);
4209 pop_delimiter (dstack);
4212 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
4213 if (ttok == &matched_pair_error)
4214 return -1; /* Bail immediately. */
4215 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4217 TOKEN_DEFAULT_GROW_SIZE);
4218 token[token_index++] = character;
4219 token[token_index++] = peek_char;
4220 strcpy (token + token_index, ttok);
4221 token_index += ttoklen;
4224 all_digit_token = 0;
4225 goto next_character;
4227 /* This handles $'...' and $"..." new-style quoted strings. */
4228 else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
4232 first_line = line_number;
4233 push_delimiter (dstack, peek_char);
4234 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
4236 (peek_char == '\'') ? P_ALLOWESC : 0);
4237 pop_delimiter (dstack);
4238 if (ttok == &matched_pair_error)
4240 if (peek_char == '\'')
4242 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
4245 /* Insert the single quotes and correctly quote any
4246 embedded single quotes (allowed because P_ALLOWESC was
4247 passed to parse_matched_pair). */
4248 ttok = sh_single_quote (ttrans);
4250 ttranslen = strlen (ttok);
4255 /* Try to locale-expand the converted string. */
4256 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
4259 /* Add the double quotes back */
4260 ttok = sh_mkdoublequoted (ttrans, ttranslen, 0);
4266 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2,
4268 TOKEN_DEFAULT_GROW_SIZE);
4269 strcpy (token + token_index, ttrans);
4270 token_index += ttranslen;
4273 all_digit_token = 0;
4274 goto next_character;
4276 /* This could eventually be extended to recognize all of the
4277 shell's single-character parameter expansions, and set flags.*/
4278 else if MBTEST(character == '$' && peek_char == '$')
4280 ttok = (char *)xmalloc (3);
4281 ttok[0] = ttok[1] = '$';
4283 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
4285 TOKEN_DEFAULT_GROW_SIZE);
4286 strcpy (token + token_index, ttok);
4289 all_digit_token = 0;
4291 goto next_character;
4294 shell_ungetc (peek_char);
4297 #if defined (ARRAY_VARS)
4298 /* Identify possible array subscript assignment; match [...]. If
4299 parser_state&PST_COMPASSIGN, we need to parse [sub]=words treating
4300 `sub' as if it were enclosed in double quotes. */
4301 else if MBTEST(character == '[' && /* ] */
4302 ((token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) ||
4303 (token_index == 0 && (parser_state&PST_COMPASSIGN))))
4305 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, P_ARRAYSUB);
4306 if (ttok == &matched_pair_error)
4307 return -1; /* Bail immediately. */
4308 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4310 TOKEN_DEFAULT_GROW_SIZE);
4311 token[token_index++] = character;
4312 strcpy (token + token_index, ttok);
4313 token_index += ttoklen;
4315 all_digit_token = 0;
4316 goto next_character;
4318 /* Identify possible compound array variable assignment. */
4319 else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index))
4321 peek_char = shell_getc (1);
4322 if MBTEST(peek_char == '(') /* ) */
4324 ttok = parse_compound_assignment (&ttoklen);
4326 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
4328 TOKEN_DEFAULT_GROW_SIZE);
4330 token[token_index++] = '=';
4331 token[token_index++] = '(';
4334 strcpy (token + token_index, ttok);
4335 token_index += ttoklen;
4337 token[token_index++] = ')';
4339 all_digit_token = 0;
4340 compound_assignment = 1;
4342 goto next_character;
4344 goto got_token; /* ksh93 seems to do this */
4348 shell_ungetc (peek_char);
4352 /* When not parsing a multi-character word construct, shell meta-
4353 characters break words. */
4354 if MBTEST(shellbreak (character))
4356 shell_ungetc (character);
4362 if (character == CTLESC || character == CTLNUL)
4363 token[token_index++] = CTLESC;
4365 got_escaped_character:
4367 all_digit_token &= DIGIT (character);
4368 dollar_present |= character == '$';
4370 token[token_index++] = character;
4372 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
4373 TOKEN_DEFAULT_GROW_SIZE);
4376 if (character == '\n' && SHOULD_PROMPT ())
4379 /* We want to remove quoted newlines (that is, a \<newline> pair)
4380 unless we are within single quotes or pass_next_character is
4381 set (the shell equivalent of literal-next). */
4382 cd = current_delimiter (dstack);
4383 character = shell_getc (cd != '\'' && pass_next_character == 0);
4384 } /* end for (;;) */
4388 token[token_index] = '\0';
4390 /* Check to see what thing we should return. If the last_read_token
4391 is a `<', or a `&', or the character which ended this token is
4392 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
4393 Otherwise, it is just a word, and should be returned as such. */
4394 if MBTEST(all_digit_token && (character == '<' || character == '>' || \
4395 last_read_token == LESS_AND || \
4396 last_read_token == GREATER_AND))
4398 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
4399 yylval.number = lvalue;
4405 /* Check for special case tokens. */
4406 result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
4411 /* Posix.2 does not allow reserved words to be aliased, so check for all
4412 of them, including special cases, before expanding the current token
4414 if MBTEST(posixly_correct)
4415 CHECK_FOR_RESERVED_WORD (token);
4417 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
4418 inhibits alias expansion. */
4419 if (expand_aliases && quoted == 0)
4421 result = alias_expand_token (token);
4422 if (result == RE_READ_TOKEN)
4423 return (RE_READ_TOKEN);
4424 else if (result == NO_EXPANSION)
4425 parser_state &= ~PST_ALEXPNEXT;
4428 /* If not in Posix.2 mode, check for reserved words after alias
4430 if MBTEST(posixly_correct == 0)
4432 CHECK_FOR_RESERVED_WORD (token);
4434 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
4435 the_word->word = (char *)xmalloc (1 + token_index);
4436 the_word->flags = 0;
4437 strcpy (the_word->word, token);
4439 the_word->flags |= W_HASDOLLAR;
4441 the_word->flags |= W_QUOTED; /*(*/
4442 if (compound_assignment && token[token_index-1] == ')')
4443 the_word->flags |= W_COMPASSIGN;
4444 /* A word is an assignment if it appears at the beginning of a
4445 simple command, or after another assignment word. This is
4446 context-dependent, so it cannot be handled in the grammar. */
4447 if (assignment (token, (parser_state & PST_COMPASSIGN) != 0))
4449 the_word->flags |= W_ASSIGNMENT;
4450 /* Don't perform word splitting on assignment statements. */
4451 if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0)
4452 the_word->flags |= W_NOSPLIT;
4455 if (command_token_position (last_read_token))
4458 b = builtin_address_internal (token, 0);
4459 if (b && (b->flags & ASSIGNMENT_BUILTIN))
4460 parser_state |= PST_ASSIGNOK;
4461 else if (STREQ (token, "eval") || STREQ (token, "let"))
4462 parser_state |= PST_ASSIGNOK;
4465 yylval.word = the_word;
4467 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
4468 ? ASSIGNMENT_WORD : WORD;
4470 switch (last_read_token)
4473 parser_state |= PST_ALLOWOPNBRC;
4474 function_dstart = line_number;
4479 if (word_top < MAX_CASE_NEST)
4481 word_lineno[word_top] = line_number;
4488 /* Return 1 if TOKSYM is a token that after being read would allow
4489 a reserved word to be seen, else 0. */
4491 reserved_word_acceptable (toksym)
4527 #if defined (COPROCESS_SUPPORT)
4528 if (last_read_token == WORD && token_before_that == COPROC)
4535 /* Return the index of TOKEN in the alist of reserved words, or -1 if
4536 TOKEN is not a shell reserved word. */
4538 find_reserved_word (tokstr)
4542 for (i = 0; word_token_alist[i].word; i++)
4543 if (STREQ (tokstr, word_token_alist[i].word))
4549 #if defined (READLINE)
4550 /* Called after each time readline is called. This insures that whatever
4551 the new prompt string is gets propagated to readline's local prompt
4554 reset_readline_prompt ()
4558 if (prompt_string_pointer)
4560 temp_prompt = (*prompt_string_pointer)
4561 ? decode_prompt_string (*prompt_string_pointer)
4564 if (temp_prompt == 0)
4566 temp_prompt = (char *)xmalloc (1);
4567 temp_prompt[0] = '\0';
4570 FREE (current_readline_prompt);
4571 current_readline_prompt = temp_prompt;
4574 #endif /* READLINE */
4577 #if defined (HISTORY)
4578 /* A list of tokens which can be followed by newlines, but not by
4579 semi-colons. When concatenating multiple lines of history, the
4580 newline separator for such tokens is replaced with a space. */
4581 static const int no_semi_successors[] = {
4582 '\n', '{', '(', ')', ';', '&', '|',
4583 CASE, DO, ELSE, IF, SEMI_SEMI, SEMI_AND, SEMI_SEMI_AND, THEN, UNTIL,
4584 WHILE, AND_AND, OR_OR, IN,
4588 /* If we are not within a delimited expression, try to be smart
4589 about which separators can be semi-colons and which must be
4590 newlines. Returns the string that should be added into the
4593 history_delimiting_chars ()
4597 if (dstack.delimiter_depth != 0)
4600 /* We look for current_command_line_count == 2 because we are looking to
4601 add the first line of the body of the here document (the second line
4603 if (parser_state & PST_HEREDOC)
4604 return (current_command_line_count == 2 ? "\n" : "");
4606 /* First, handle some special cases. */
4608 /* If we just read `()', assume it's a function definition, and don't
4609 add a semicolon. If the token before the `)' was not `(', and we're
4610 not in the midst of parsing a case statement, assume it's a
4611 parenthesized command and add the semicolon. */
4613 if (token_before_that == ')')
4615 if (two_tokens_ago == '(') /*)*/ /* function def */
4617 /* This does not work for subshells inside case statement
4618 command lists. It's a suboptimal solution. */
4619 else if (parser_state & PST_CASESTMT) /* case statement pattern */
4622 return "; "; /* (...) subshell */
4624 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
4625 return " "; /* function def using `function name' without `()' */
4627 else if (token_before_that == WORD && two_tokens_ago == FOR)
4629 /* Tricky. `for i\nin ...' should not have a semicolon, but
4630 `for i\ndo ...' should. We do what we can. */
4631 for (i = shell_input_line_index; whitespace (shell_input_line[i]); i++)
4633 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
4637 else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT))
4640 for (i = 0; no_semi_successors[i]; i++)
4642 if (token_before_that == no_semi_successors[i])
4648 #endif /* HISTORY */
4650 /* Issue a prompt, or prepare to issue a prompt when the next character
4657 if (interactive == 0 || expanding_alias()) /* XXX */
4660 ps1_prompt = get_string_value ("PS1");
4661 ps2_prompt = get_string_value ("PS2");
4663 if (!prompt_string_pointer)
4664 prompt_string_pointer = &ps1_prompt;
4666 temp_prompt = *prompt_string_pointer
4667 ? decode_prompt_string (*prompt_string_pointer)
4670 if (temp_prompt == 0)
4672 temp_prompt = (char *)xmalloc (1);
4673 temp_prompt[0] = '\0';
4676 current_prompt_string = *prompt_string_pointer;
4677 prompt_string_pointer = &ps2_prompt;
4679 #if defined (READLINE)
4680 if (!no_line_editing)
4682 FREE (current_readline_prompt);
4683 current_readline_prompt = temp_prompt;
4686 #endif /* READLINE */
4688 FREE (current_decoded_prompt);
4689 current_decoded_prompt = temp_prompt;
4694 get_current_prompt_level ()
4696 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
4700 set_current_prompt_level (x)
4703 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
4704 current_prompt_string = *prompt_string_pointer;
4710 fprintf (stderr, "%s", current_decoded_prompt);
4714 /* Return a string which will be printed as a prompt. The string
4715 may contain special characters which are decoded as follows:
4718 \d the date in Day Mon Date format
4719 \e escape (ascii 033)
4720 \h the hostname up to the first `.'
4722 \j the number of active jobs
4723 \l the basename of the shell's tty device name
4726 \s the name of the shell
4727 \t the time in 24-hour hh:mm:ss format
4728 \T the time in 12-hour hh:mm:ss format
4729 \@ the time in 12-hour hh:mm am/pm format
4730 \A the time in 24-hour hh:mm format
4731 \D{fmt} the result of passing FMT to strftime(3)
4733 \v the version of bash (e.g., 2.00)
4734 \V the release of bash, version + patchlevel (e.g., 2.00.0)
4735 \w the current working directory
4736 \W the last element of $PWD
4737 \! the history number of this command
4738 \# the command number of this command
4739 \$ a $ or a # if you are root
4740 \nnn character code nnn in octal
4742 \[ begin a sequence of non-printing chars
4743 \] end a sequence of non-printing chars
4745 #define PROMPT_GROWTH 48
4747 decode_prompt_string (string)
4752 struct dstack save_dstack;
4753 int last_exit_value;
4754 #if defined (PROMPT_STRING_DECODE)
4755 int result_size, result_index;
4757 char *temp, octal_string[4];
4763 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
4764 result[result_index = 0] = 0;
4765 temp = (char *)NULL;
4767 while (c = *string++)
4769 if (posixly_correct && c == '!')
4773 temp = savestring ("!");
4778 #if !defined (HISTORY)
4779 temp = savestring ("1");
4781 temp = itos (history_number ());
4782 #endif /* HISTORY */
4783 string--; /* add_string increments string again. */
4801 strncpy (octal_string, string, 3);
4802 octal_string[3] = '\0';
4804 n = read_octal (octal_string);
4805 temp = (char *)xmalloc (3);
4807 if (n == CTLESC || n == CTLNUL)
4824 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
4827 c = 0; /* tested at add_string: */
4835 /* Make the current time/date into a string. */
4836 (void) time (&the_time);
4837 tm = localtime (&the_time);
4840 n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm);
4842 n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm);
4844 n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm);
4846 n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm);
4848 n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm);
4853 timebuf[sizeof(timebuf) - 1] = '\0';
4855 temp = savestring (timebuf);
4858 case 'D': /* strftime format */
4859 if (string[1] != '{') /* } */
4862 (void) time (&the_time);
4863 tm = localtime (&the_time);
4864 string += 2; /* skip { */
4865 timefmt = xmalloc (strlen (string) + 3);
4866 for (t = timefmt; *string && *string != '}'; )
4869 c = *string; /* tested at add_string */
4870 if (timefmt[0] == '\0')
4873 timefmt[1] = 'X'; /* locale-specific current time */
4876 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
4882 timebuf[sizeof(timebuf) - 1] = '\0';
4884 if (promptvars || posixly_correct)
4885 /* Make sure that expand_prompt_string is called with a
4886 second argument of Q_DOUBLE_QUOTES if we use this
4888 temp = sh_backslash_quote_for_double_quotes (timebuf);
4890 temp = savestring (timebuf);
4894 temp = (char *)xmalloc (3);
4895 temp[0] = no_line_editing ? '\n' : '\r';
4896 temp[1] = no_line_editing ? '\0' : '\n';
4901 temp = base_pathname (shell_name);
4902 temp = savestring (temp);
4907 temp = (char *)xmalloc (16);
4909 strcpy (temp, dist_version);
4911 sprintf (temp, "%s.%d", dist_version, patch_level);
4917 /* Use the value of PWD because it is much more efficient. */
4918 char t_string[PATH_MAX];
4921 temp = get_string_value ("PWD");
4925 if (getcwd (t_string, sizeof(t_string)) == 0)
4931 tlen = strlen (t_string);
4935 tlen = sizeof (t_string) - 1;
4936 strncpy (t_string, temp, tlen);
4938 t_string[tlen] = '\0';
4940 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
4941 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
4942 /* Abbreviate \W as ~ if $PWD == $HOME */
4943 if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0))
4945 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
4947 t = strrchr (t_string, '/');
4949 strcpy (t_string, t + 1);
4953 #undef DOUBLE_SLASH_ROOT
4955 /* polite_directory_format is guaranteed to return a string
4956 no longer than PATH_MAX - 1 characters. */
4957 strcpy (t_string, polite_directory_format (t_string));
4959 temp = trim_pathname (t_string, PATH_MAX - 1);
4960 /* If we're going to be expanding the prompt string later,
4961 quote the directory name. */
4962 if (promptvars || posixly_correct)
4963 /* Make sure that expand_prompt_string is called with a
4964 second argument of Q_DOUBLE_QUOTES if we use this
4966 temp = sh_backslash_quote_for_double_quotes (t_string);
4968 temp = savestring (t_string);
4974 if (current_user.user_name == 0)
4975 get_current_user_info ();
4976 temp = savestring (current_user.user_name);
4981 temp = savestring (current_host_name);
4982 if (c == 'h' && (t = (char *)strchr (temp, '.')))
4987 temp = itos (current_command_number);
4991 #if !defined (HISTORY)
4992 temp = savestring ("1");
4994 temp = itos (history_number ());
4995 #endif /* HISTORY */
4999 t = temp = (char *)xmalloc (3);
5000 if ((promptvars || posixly_correct) && (current_user.euid != 0))
5002 *t++ = current_user.euid == 0 ? '#' : '$';
5007 temp = itos (count_all_jobs ());
5011 #if defined (HAVE_TTYNAME)
5012 temp = (char *)ttyname (fileno (stdin));
5013 t = temp ? base_pathname (temp) : "tty";
5014 temp = savestring (t);
5016 temp = savestring ("tty");
5017 #endif /* !HAVE_TTYNAME */
5020 #if defined (READLINE)
5023 if (no_line_editing)
5028 temp = (char *)xmalloc (3);
5029 n = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
5031 if (n == CTLESC || n == CTLNUL)
5036 #endif /* READLINE */
5042 temp = (char *)xmalloc (2);
5049 else /* (c == '\\') */
5056 temp = (char *)xmalloc (3);
5065 sub_append_string (temp, result, &result_index, &result_size);
5066 temp = (char *)NULL; /* Freed in sub_append_string (). */
5067 result[result_index] = '\0';
5073 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
5074 result[result_index++] = c;
5075 result[result_index] = '\0';
5078 #else /* !PROMPT_STRING_DECODE */
5079 result = savestring (string);
5080 #endif /* !PROMPT_STRING_DECODE */
5082 /* Save the delimiter stack and point `dstack' to temp space so any
5083 command substitutions in the prompt string won't result in screwing
5084 up the parser's quoting state. */
5085 save_dstack = dstack;
5086 dstack = temp_dstack;
5087 dstack.delimiter_depth = 0;
5089 /* Perform variable and parameter expansion and command substitution on
5090 the prompt string. */
5091 if (promptvars || posixly_correct)
5093 last_exit_value = last_command_exit_value;
5094 list = expand_prompt_string (result, Q_DOUBLE_QUOTES, 0);
5096 result = string_list (list);
5097 dispose_words (list);
5098 last_command_exit_value = last_exit_value;
5102 t = dequote_string (result);
5107 dstack = save_dstack;
5112 /************************************************
5116 ************************************************/
5118 /* Report a syntax error, and restart the parser. Call here for fatal
5124 report_syntax_error ((char *)NULL);
5130 error_token_from_token (tok)
5135 if (t = find_token_in_alist (tok, word_token_alist, 0))
5138 if (t = find_token_in_alist (tok, other_token_alist, 0))
5142 /* This stuff is dicy and needs closer inspection */
5143 switch (current_token)
5146 case ASSIGNMENT_WORD:
5148 t = savestring (yylval.word->word);
5151 t = itos (yylval.number);
5154 if (yylval.word_list)
5155 t = string_list (yylval.word_list);
5157 case ARITH_FOR_EXPRS:
5158 if (yylval.word_list)
5159 t = string_list_internal (yylval.word_list, " ; ");
5162 t = (char *)NULL; /* punt */
5170 error_token_from_text ()
5175 t = shell_input_line;
5176 i = shell_input_line_index;
5180 if (i && t[i] == '\0')
5183 while (i && (whitespace (t[i]) || t[i] == '\n'))
5189 while (i && (member (t[i], " \n\t;|&") == 0))
5192 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
5195 /* Return our idea of the offending token. */
5196 if (token_end || (i == 0 && token_end == 0))
5199 msg = substring (t, i, token_end);
5200 else /* one-character token */
5202 msg = (char *)xmalloc (2);
5212 print_offending_line ()
5217 msg = savestring (shell_input_line);
5218 token_end = strlen (msg);
5219 while (token_end && msg[token_end - 1] == '\n')
5220 msg[--token_end] = '\0';
5222 parser_error (line_number, "`%s'", msg);
5226 /* Report a syntax error with line numbers, etc.
5227 Call here for recoverable errors. If you have a message to print,
5228 then place it in MESSAGE, otherwise pass NULL and this will figure
5229 out an appropriate message for you. */
5231 report_syntax_error (message)
5238 parser_error (line_number, "%s", message);
5239 if (interactive && EOF_Reached)
5241 last_command_exit_value = EX_BADUSAGE;
5245 /* If the line of input we're reading is not null, try to find the
5246 objectionable token. First, try to figure out what token the
5247 parser's complaining about by looking at current_token. */
5248 if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
5250 parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
5253 if (interactive == 0)
5254 print_offending_line ();
5256 last_command_exit_value = EX_BADUSAGE;
5260 /* If looking at the current token doesn't prove fruitful, try to find the
5261 offending token by analyzing the text of the input line near the current
5262 input line index and report what we find. */
5263 if (shell_input_line && *shell_input_line)
5265 msg = error_token_from_text ();
5268 parser_error (line_number, _("syntax error near `%s'"), msg);
5272 /* If not interactive, print the line containing the error. */
5273 if (interactive == 0)
5274 print_offending_line ();
5278 msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error");
5279 parser_error (line_number, "%s", msg);
5280 /* When the shell is interactive, this file uses EOF_Reached
5281 only for error reporting. Other mechanisms are used to
5282 decide whether or not to exit. */
5283 if (interactive && EOF_Reached)
5287 last_command_exit_value = EX_BADUSAGE;
5290 /* ??? Needed function. ??? We have to be able to discard the constructs
5291 created during parsing. In the case of error, we want to return
5292 allocated objects to the memory pool. In the case of no error, we want
5293 to throw away the information about where the allocated objects live.
5294 (dispose_command () will actually free the command.) */
5296 discard_parser_constructs (error_p)
5301 /************************************************
5305 ************************************************/
5307 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
5309 /* A flag denoting whether or not ignoreeof is set. */
5312 /* The number of times that we have encountered an EOF character without
5313 another character intervening. When this gets above the limit, the
5314 shell terminates. */
5315 int eof_encountered = 0;
5317 /* The limit for eof_encountered. */
5318 int eof_encountered_limit = 10;
5320 /* If we have EOF as the only input unit, this user wants to leave
5321 the shell. If the shell is not interactive, then just leave.
5322 Otherwise, if ignoreeof is set, and we haven't done this the
5323 required number of times in a row, print a message. */
5325 handle_eof_input_unit ()
5329 /* shell.c may use this to decide whether or not to write out the
5330 history, among other things. We use it only for error reporting
5335 /* If the user wants to "ignore" eof, then let her do so, kind of. */
5338 if (eof_encountered < eof_encountered_limit)
5340 fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
5341 login_shell ? "logout" : "exit");
5343 /* Reset the parsing state. */
5344 last_read_token = current_token = '\n';
5345 /* Reset the prompt string to be $PS1. */
5346 prompt_string_pointer = (char **)NULL;
5352 /* In this case EOF should exit the shell. Do it now. */
5354 exit_builtin ((WORD_LIST *)NULL);
5358 /* We don't write history files, etc., for non-interactive shells. */
5363 /************************************************
5365 * STRING PARSING FUNCTIONS *
5367 ************************************************/
5369 /* It's very important that these two functions treat the characters
5370 between ( and ) identically. */
5372 static WORD_LIST parse_string_error;
5374 /* Take a string and run it through the shell parser, returning the
5375 resultant word list. Used by compound array assignment. */
5377 parse_string_to_word_list (s, flags, whom)
5383 int tok, orig_current_token, orig_line_number, orig_input_terminator;
5384 int orig_line_count;
5385 int old_echo_input, old_expand_aliases;
5386 #if defined (HISTORY)
5387 int old_remember_on_history, old_history_expansion_inhibited;
5390 #if defined (HISTORY)
5391 old_remember_on_history = remember_on_history;
5392 # if defined (BANG_HISTORY)
5393 old_history_expansion_inhibited = history_expansion_inhibited;
5395 bash_history_disable ();
5398 orig_line_number = line_number;
5399 orig_line_count = current_command_line_count;
5400 orig_input_terminator = shell_input_line_terminator;
5401 old_echo_input = echo_input_at_read;
5402 old_expand_aliases = expand_aliases;
5405 last_read_token = WORD; /* WORD to allow reserved words here */
5406 current_command_line_count = 0;
5407 echo_input_at_read = expand_aliases = 0;
5409 with_input_from_string (s, whom);
5410 wl = (WORD_LIST *)NULL;
5413 parser_state |= PST_COMPASSIGN|PST_REPARSE;
5415 while ((tok = read_token (READ)) != yacc_EOF)
5417 if (tok == '\n' && *bash_input.location.string == '\0')
5419 if (tok == '\n') /* Allow newlines in compound assignments */
5421 if (tok != WORD && tok != ASSIGNMENT_WORD)
5423 line_number = orig_line_number + line_number - 1;
5424 orig_current_token = current_token;
5425 current_token = tok;
5426 yyerror (NULL); /* does the right thing */
5427 current_token = orig_current_token;
5430 wl = &parse_string_error;
5433 wl = make_word_list (yylval.word, wl);
5436 last_read_token = '\n';
5439 #if defined (HISTORY)
5440 remember_on_history = old_remember_on_history;
5441 # if defined (BANG_HISTORY)
5442 history_expansion_inhibited = old_history_expansion_inhibited;
5443 # endif /* BANG_HISTORY */
5444 #endif /* HISTORY */
5446 echo_input_at_read = old_echo_input;
5447 expand_aliases = old_expand_aliases;
5449 current_command_line_count = orig_line_count;
5450 shell_input_line_terminator = orig_input_terminator;
5453 parser_state &= ~(PST_COMPASSIGN|PST_REPARSE);
5455 if (wl == &parse_string_error)
5457 last_command_exit_value = EXECUTION_FAILURE;
5458 if (interactive_shell == 0 && posixly_correct)
5459 jump_to_top_level (FORCE_EOF);
5461 jump_to_top_level (DISCARD);
5464 return (REVERSE_LIST (wl, WORD_LIST *));
5468 parse_compound_assignment (retlenp)
5472 int tok, orig_line_number, orig_token_size, orig_last_token, assignok;
5473 char *saved_token, *ret;
5475 saved_token = token;
5476 orig_token_size = token_buffer_size;
5477 orig_line_number = line_number;
5478 orig_last_token = last_read_token;
5480 last_read_token = WORD; /* WORD to allow reserved words here */
5482 token = (char *)NULL;
5483 token_buffer_size = 0;
5485 assignok = parser_state&PST_ASSIGNOK; /* XXX */
5487 wl = (WORD_LIST *)NULL; /* ( */
5488 parser_state |= PST_COMPASSIGN;
5490 while ((tok = read_token (READ)) != ')')
5492 if (tok == '\n') /* Allow newlines in compound assignments */
5494 if (SHOULD_PROMPT ())
5498 if (tok != WORD && tok != ASSIGNMENT_WORD)
5500 current_token = tok; /* for error reporting */
5501 if (tok == yacc_EOF) /* ( */
5502 parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'"));
5504 yyerror(NULL); /* does the right thing */
5507 wl = &parse_string_error;
5510 wl = make_word_list (yylval.word, wl);
5514 token = saved_token;
5515 token_buffer_size = orig_token_size;
5517 parser_state &= ~PST_COMPASSIGN;
5519 if (wl == &parse_string_error)
5521 last_command_exit_value = EXECUTION_FAILURE;
5522 last_read_token = '\n'; /* XXX */
5523 if (interactive_shell == 0 && posixly_correct)
5524 jump_to_top_level (FORCE_EOF);
5526 jump_to_top_level (DISCARD);
5529 last_read_token = orig_last_token; /* XXX - was WORD? */
5533 rl = REVERSE_LIST (wl, WORD_LIST *);
5534 ret = string_list (rl);
5541 *retlenp = (ret && *ret) ? strlen (ret) : 0;
5544 parser_state |= PST_ASSIGNOK;
5549 /************************************************
5551 * SAVING AND RESTORING PARTIAL PARSE STATE *
5553 ************************************************/
5556 save_parser_state (ps)
5557 sh_parser_state_t *ps;
5559 #if defined (ARRAY_VARS)
5564 ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t));
5566 return ((sh_parser_state_t *)NULL);
5568 ps->parser_state = parser_state;
5569 ps->token_state = save_token_state ();
5571 ps->input_line_terminator = shell_input_line_terminator;
5572 ps->eof_encountered = eof_encountered;
5574 ps->current_command_line_count = current_command_line_count;
5576 #if defined (HISTORY)
5577 ps->remember_on_history = remember_on_history;
5578 # if defined (BANG_HISTORY)
5579 ps->history_expansion_inhibited = history_expansion_inhibited;
5583 ps->last_command_exit_value = last_command_exit_value;
5584 #if defined (ARRAY_VARS)
5585 v = find_variable ("PIPESTATUS");
5586 if (v && array_p (v) && array_cell (v))
5587 ps->pipestatus = array_copy (array_cell (v));
5589 ps->pipestatus = (ARRAY *)NULL;
5592 ps->last_shell_builtin = last_shell_builtin;
5593 ps->this_shell_builtin = this_shell_builtin;
5595 ps->expand_aliases = expand_aliases;
5596 ps->echo_input_at_read = echo_input_at_read;
5602 restore_parser_state (ps)
5603 sh_parser_state_t *ps;
5605 #if defined (ARRAY_VARS)
5612 parser_state = ps->parser_state;
5613 if (ps->token_state)
5615 restore_token_state (ps->token_state);
5616 free (ps->token_state);
5619 shell_input_line_terminator = ps->input_line_terminator;
5620 eof_encountered = ps->eof_encountered;
5622 current_command_line_count = ps->current_command_line_count;
5624 #if defined (HISTORY)
5625 remember_on_history = ps->remember_on_history;
5626 # if defined (BANG_HISTORY)
5627 history_expansion_inhibited = ps->history_expansion_inhibited;
5631 last_command_exit_value = ps->last_command_exit_value;
5632 #if defined (ARRAY_VARS)
5633 v = find_variable ("PIPESTATUS");
5634 if (v && array_p (v) && array_cell (v))
5636 array_dispose (array_cell (v));
5637 var_setarray (v, ps->pipestatus);
5641 last_shell_builtin = ps->last_shell_builtin;
5642 this_shell_builtin = ps->this_shell_builtin;
5644 expand_aliases = ps->expand_aliases;
5645 echo_input_at_read = ps->echo_input_at_read;
5648 /************************************************
5650 * MULTIBYTE CHARACTER HANDLING *
5652 ************************************************/
5654 #if defined (HANDLE_MULTIBYTE)
5658 int i, previ, len, c;
5659 mbstate_t mbs, prevs;
5662 if (shell_input_line == NULL)
5664 len = strlen (shell_input_line); /* XXX - shell_input_line_len ? */
5665 FREE (shell_input_line_property);
5666 shell_input_line_property = (char *)xmalloc (len + 1);
5668 memset (&prevs, '\0', sizeof (mbstate_t));
5669 for (i = previ = 0; i < len; i++)
5673 c = shell_input_line[i];
5677 for (j = i; j < len; j++)
5678 shell_input_line_property[j] = 1;
5682 mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs);
5683 if (mbclen == 1 || mbclen == (size_t)-1)
5688 else if (mbclen == (size_t)-2)
5690 else if (mbclen > 1)
5698 /* XXX - what to do if mbrlen returns 0? (null wide character) */
5700 for (j = i; j < len; j++)
5701 shell_input_line_property[j] = 1;
5705 shell_input_line_property[i] = mbclen;
5708 #endif /* HANDLE_MULTIBYTE */