1 /* parse.y - Yacc grammar for bash. */
3 /* Copyright (C) 1989-2022 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 */
48 #include "execute_cmd.h"
49 #include "typemax.h" /* SIZE_MAX if needed */
53 #include "mailcheck.h"
56 #include "builtins/common.h"
57 #include "builtins/builtext.h"
61 #if defined (READLINE)
62 # include "bashline.h"
63 # include <readline/readline.h>
67 # include "bashhist.h"
68 # include <readline/history.h>
71 #if defined (JOB_CONTROL)
74 extern int cleanup_dead_jobs PARAMS((void));
75 #endif /* JOB_CONTROL */
80 typedef void *alias_t;
83 #if defined (PROMPT_STRING_DECODE)
85 # include <sys/param.h>
88 # if defined (TM_IN_SYS_TIME)
89 # include <sys/types.h>
90 # include <sys/time.h>
91 # endif /* TM_IN_SYS_TIME */
93 #endif /* PROMPT_STRING_DECODE */
95 #define RE_READ_TOKEN -99
96 #define NO_EXPANSION -100
106 #if defined (HANDLE_MULTIBYTE)
107 # define last_shell_getc_is_singlebyte \
108 ((shell_input_line_index > 1) \
109 ? shell_input_line_property[shell_input_line_index - 1] \
111 # define MBTEST(x) ((x) && last_shell_getc_is_singlebyte)
113 # define last_shell_getc_is_singlebyte 1
114 # define MBTEST(x) ((x))
117 #define EXTEND_SHELL_INPUT_LINE_PROPERTY() \
119 if (shell_input_line_len + 2 > shell_input_line_propsize) \
121 shell_input_line_propsize = shell_input_line_len + 2; \
122 shell_input_line_property = (char *)xrealloc (shell_input_line_property, \
123 shell_input_line_propsize); \
127 #if defined (EXTENDED_GLOB)
128 extern int extended_glob;
131 #if defined (TRANSLATABLE_STRINGS)
132 extern int dump_translatable_strings, dump_po_strings;
133 extern int singlequote_translations;
134 #endif /* TRANSLATABLE_STRINGS */
140 /* **************************************************************** */
142 /* "Forward" declarations */
144 /* **************************************************************** */
147 static void debug_parser PARAMS((int));
150 static int yy_getc PARAMS((void));
151 static int yy_ungetc PARAMS((int));
153 #if defined (READLINE)
154 static int yy_readline_get PARAMS((void));
155 static int yy_readline_unget PARAMS((int));
158 static int yy_string_get PARAMS((void));
159 static int yy_string_unget PARAMS((int));
160 static int yy_stream_get PARAMS((void));
161 static int yy_stream_unget PARAMS((int));
163 static int shell_getc PARAMS((int));
164 static void shell_ungetc PARAMS((int));
165 static void discard_until PARAMS((int));
167 static void push_string PARAMS((char *, int, alias_t *));
168 static void pop_string PARAMS((void));
169 static void free_string_list PARAMS((void));
171 static char *read_a_line PARAMS((int));
173 static int reserved_word_acceptable PARAMS((int));
174 static int yylex PARAMS((void));
176 static void push_heredoc PARAMS((REDIRECT *));
177 static char *mk_alexpansion PARAMS((char *));
178 static int alias_expand_token PARAMS((char *));
179 static int time_command_acceptable PARAMS((void));
180 static int special_case_tokens PARAMS((char *));
181 static int read_token PARAMS((int));
182 static char *parse_matched_pair PARAMS((int, int, int, int *, int));
183 static char *parse_comsub PARAMS((int, int, int, int *, int));
184 #if defined (ARRAY_VARS)
185 static char *parse_compound_assignment PARAMS((int *));
187 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
188 static int parse_dparen PARAMS((int));
189 static int parse_arith_cmd PARAMS((char **, int));
191 #if defined (COND_COMMAND)
192 static void cond_error PARAMS((void));
193 static COND_COM *cond_expr PARAMS((void));
194 static COND_COM *cond_or PARAMS((void));
195 static COND_COM *cond_and PARAMS((void));
196 static COND_COM *cond_term PARAMS((void));
197 static int cond_skip_newlines PARAMS((void));
198 static COMMAND *parse_cond_command PARAMS((void));
200 #if defined (ARRAY_VARS)
201 static int token_is_assignment PARAMS((char *, int));
202 static int token_is_ident PARAMS((char *, int));
204 static int read_token_word PARAMS((int));
205 static void discard_parser_constructs PARAMS((int));
207 static char *error_token_from_token PARAMS((int));
208 static char *error_token_from_text PARAMS((void));
209 static void print_offending_line PARAMS((void));
210 static void report_syntax_error PARAMS((char *));
212 static void handle_eof_input_unit PARAMS((void));
213 static void prompt_again PARAMS((int));
215 static void reset_readline_prompt PARAMS((void));
217 static void print_prompt PARAMS((void));
219 #if defined (HANDLE_MULTIBYTE)
220 static void set_line_mbstate PARAMS((void));
221 static char *shell_input_line_property = NULL;
222 static size_t shell_input_line_propsize = 0;
224 # define set_line_mbstate()
227 extern int yyerror PARAMS((const char *));
233 /* Default prompt strings */
234 char *primary_prompt = PPROMPT;
235 char *secondary_prompt = SPROMPT;
237 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
238 char *ps1_prompt, *ps2_prompt;
240 /* Displayed after reading a command but before executing it in an interactive shell */
243 /* Handle on the current prompt string. Indirectly points through
244 ps1_ or ps2_prompt. */
245 char **prompt_string_pointer = (char **)NULL;
246 char *current_prompt_string;
248 /* Non-zero means we expand aliases in commands. */
249 int expand_aliases = 0;
251 /* If non-zero, the decoded prompt string undergoes parameter and
252 variable substitution, command substitution, arithmetic substitution,
253 string expansion, process substitution, and quote removal in
254 decode_prompt_string. */
257 /* If non-zero, $'...' and $"..." are expanded when they appear within
258 a ${...} expansion, even when the expansion appears within double
260 int extended_quote = 1;
262 /* The number of lines read from input while creating the current command. */
263 int current_command_line_count;
265 /* The number of lines in a command saved while we run parse_and_execute */
266 int saved_command_line_count;
268 /* The token that currently denotes the end of parse. */
271 /* The token currently being read. */
274 /* The current parser state. */
277 /* Variables to manage the task of reading here documents, because we need to
278 defer the reading until after a complete command has been collected. */
279 static REDIRECT *redir_stack[HEREDOC_MAX];
282 /* Where shell input comes from. History expansion is performed on each
283 line when the shell is interactive. */
284 static char *shell_input_line = (char *)NULL;
285 static size_t shell_input_line_index;
286 static size_t shell_input_line_size; /* Amount allocated for shell_input_line. */
287 static size_t shell_input_line_len; /* strlen (shell_input_line) */
289 /* Either zero or EOF. */
290 static int shell_input_line_terminator;
292 /* The line number in a script on which a function definition starts. */
293 static int function_dstart;
295 /* The line number in a script on which a function body starts. */
296 static int function_bstart;
298 /* The line number in a script at which an arithmetic for command starts. */
299 static int arith_for_lineno;
301 /* The decoded prompt string. Used if READLINE is not defined or if
302 editing is turned off. Analogous to current_readline_prompt. */
303 static char *current_decoded_prompt;
305 /* The last read token, or NULL. read_token () uses this for context
307 static int last_read_token;
309 /* The token read prior to last_read_token. */
310 static int token_before_that;
312 /* The token read prior to token_before_that. */
313 static int two_tokens_ago;
315 static int global_extglob;
317 /* The line number in a script where the word in a `case WORD', `select WORD'
318 or `for WORD' begins. This is a nested command maximum, since the array
319 index is decremented after a case, select, or for command is parsed. */
320 #define MAX_CASE_NEST 128
321 static int word_lineno[MAX_CASE_NEST+1];
322 static int word_top = -1;
324 /* If non-zero, it is the token that we want read_token to return
325 regardless of what text is (or isn't) present to be read. This
326 is reset by read_token. If token_to_read == WORD or
327 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
328 static int token_to_read;
329 static WORD_DESC *word_desc_to_read;
331 static REDIRECTEE source;
332 static REDIRECTEE redir;
334 static FILE *yyoutstream;
335 static FILE *yyerrstream;
339 WORD_DESC *word; /* the word that we read. */
340 int number; /* the number that we read. */
341 WORD_LIST *word_list;
345 PATTERN_LIST *pattern;
348 /* Reserved words. Members of the first group are only recognized
349 in the case that they are preceded by a list_terminator. Members
350 of the second group are for [[...]] commands. Members of the
351 third group are recognized only under special circumstances. */
352 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION COPROC
353 %token COND_START COND_END COND_ERROR
354 %token IN BANG TIME TIMEOPT TIMEIGN
356 /* More general tokens. yylex () knows how to make these. */
357 %token <word> WORD ASSIGNMENT_WORD REDIR_WORD
358 %token <number> NUMBER
359 %token <word_list> ARITH_CMD ARITH_FOR_EXPRS
360 %token <command> COND_CMD
361 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS
362 %token GREATER_AND SEMI_SEMI SEMI_AND SEMI_SEMI_AND
363 %token LESS_LESS_MINUS AND_GREATER AND_GREATER_GREATER LESS_GREATER
364 %token GREATER_BAR BAR_AND
366 /* Special; never created by yylex; only set by parse_comsub and xparse_dolparen */
369 /* The types that the various syntactical units return. */
371 %type <command> inputunit command pipeline pipeline_command
372 %type <command> list0 list1 compound_list simple_list simple_list1
373 %type <command> simple_command shell_command
374 %type <command> for_command select_command case_command group_command
375 %type <command> arith_command
376 %type <command> cond_command
377 %type <command> arith_for_command
378 %type <command> coproc
379 %type <command> comsub
380 %type <command> function_def function_body if_command elif_clause subshell
381 %type <redirect> redirection redirection_list
382 %type <element> simple_command_element
383 %type <word_list> word_list pattern
384 %type <pattern> pattern_list case_clause_sequence case_clause
385 %type <number> timespec
386 %type <number> list_terminator
390 %left '&' ';' '\n' yacc_EOF
395 inputunit: simple_list simple_list_terminator
397 /* Case of regular command. Discard the error
398 safety net,and return the command just parsed. */
401 /* discard_parser_constructs (0); */
402 if (parser_state & PST_CMDSUBST)
403 parser_state |= PST_EOFTOKEN;
408 /* This is special; look at the production and how
409 parse_comsub sets token_to_read */
416 /* Case of regular command, but not a very
417 interesting one. Return a NULL command. */
418 global_command = (COMMAND *)NULL;
419 if (parser_state & PST_CMDSUBST)
420 parser_state |= PST_EOFTOKEN;
425 /* Error during parsing. Return NULL command. */
426 global_command = (COMMAND *)NULL;
428 /* discard_parser_constructs (1); */
429 if (interactive && parse_and_execute_level == 0)
440 /* EOF after an error. Do ignoreeof or not. Really
441 only interesting in non-interactive shells */
442 global_command = (COMMAND *)NULL;
443 if (last_command_exit_value == 0)
444 last_command_exit_value = EX_BADUSAGE; /* force error return */
445 if (interactive && parse_and_execute_level == 0)
447 handle_eof_input_unit ();
457 /* Case of EOF seen by itself. Do ignoreeof or
459 global_command = (COMMAND *)NULL;
460 handle_eof_input_unit ();
466 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
468 { $$ = make_word_list ($2, $1); }
471 redirection: '>' WORD
475 $$ = make_redirection (source, r_output_direction, redir, 0);
481 $$ = make_redirection (source, r_input_direction, redir, 0);
487 $$ = make_redirection (source, r_output_direction, redir, 0);
493 $$ = make_redirection (source, r_input_direction, redir, 0);
495 | REDIR_WORD '>' WORD
497 source.filename = $1;
499 $$ = make_redirection (source, r_output_direction, redir, REDIR_VARASSIGN);
501 | REDIR_WORD '<' WORD
503 source.filename = $1;
505 $$ = make_redirection (source, r_input_direction, redir, REDIR_VARASSIGN);
507 | GREATER_GREATER WORD
511 $$ = make_redirection (source, r_appending_to, redir, 0);
513 | NUMBER GREATER_GREATER WORD
517 $$ = make_redirection (source, r_appending_to, redir, 0);
519 | REDIR_WORD GREATER_GREATER WORD
521 source.filename = $1;
523 $$ = make_redirection (source, r_appending_to, redir, REDIR_VARASSIGN);
529 $$ = make_redirection (source, r_output_force, redir, 0);
531 | NUMBER GREATER_BAR WORD
535 $$ = make_redirection (source, r_output_force, redir, 0);
537 | REDIR_WORD GREATER_BAR WORD
539 source.filename = $1;
541 $$ = make_redirection (source, r_output_force, redir, REDIR_VARASSIGN);
547 $$ = make_redirection (source, r_input_output, redir, 0);
549 | NUMBER LESS_GREATER WORD
553 $$ = make_redirection (source, r_input_output, redir, 0);
555 | REDIR_WORD LESS_GREATER WORD
557 source.filename = $1;
559 $$ = make_redirection (source, r_input_output, redir, REDIR_VARASSIGN);
565 $$ = make_redirection (source, r_reading_until, redir, 0);
568 | NUMBER LESS_LESS WORD
572 $$ = make_redirection (source, r_reading_until, redir, 0);
575 | REDIR_WORD LESS_LESS WORD
577 source.filename = $1;
579 $$ = make_redirection (source, r_reading_until, redir, REDIR_VARASSIGN);
582 | LESS_LESS_MINUS WORD
586 $$ = make_redirection (source, r_deblank_reading_until, redir, 0);
589 | NUMBER LESS_LESS_MINUS WORD
593 $$ = make_redirection (source, r_deblank_reading_until, redir, 0);
596 | REDIR_WORD LESS_LESS_MINUS WORD
598 source.filename = $1;
600 $$ = make_redirection (source, r_deblank_reading_until, redir, REDIR_VARASSIGN);
603 | LESS_LESS_LESS WORD
607 $$ = make_redirection (source, r_reading_string, redir, 0);
609 | NUMBER LESS_LESS_LESS WORD
613 $$ = make_redirection (source, r_reading_string, redir, 0);
615 | REDIR_WORD LESS_LESS_LESS WORD
617 source.filename = $1;
619 $$ = make_redirection (source, r_reading_string, redir, REDIR_VARASSIGN);
625 $$ = make_redirection (source, r_duplicating_input, redir, 0);
627 | NUMBER LESS_AND NUMBER
631 $$ = make_redirection (source, r_duplicating_input, redir, 0);
633 | REDIR_WORD LESS_AND NUMBER
635 source.filename = $1;
637 $$ = make_redirection (source, r_duplicating_input, redir, REDIR_VARASSIGN);
643 $$ = make_redirection (source, r_duplicating_output, redir, 0);
645 | NUMBER GREATER_AND NUMBER
649 $$ = make_redirection (source, r_duplicating_output, redir, 0);
651 | REDIR_WORD GREATER_AND NUMBER
653 source.filename = $1;
655 $$ = make_redirection (source, r_duplicating_output, redir, REDIR_VARASSIGN);
661 $$ = make_redirection (source, r_duplicating_input_word, redir, 0);
663 | NUMBER LESS_AND WORD
667 $$ = make_redirection (source, r_duplicating_input_word, redir, 0);
669 | REDIR_WORD LESS_AND WORD
671 source.filename = $1;
673 $$ = make_redirection (source, r_duplicating_input_word, redir, REDIR_VARASSIGN);
679 $$ = make_redirection (source, r_duplicating_output_word, redir, 0);
681 | NUMBER GREATER_AND WORD
685 $$ = make_redirection (source, r_duplicating_output_word, redir, 0);
687 | REDIR_WORD GREATER_AND WORD
689 source.filename = $1;
691 $$ = make_redirection (source, r_duplicating_output_word, redir, REDIR_VARASSIGN);
697 $$ = make_redirection (source, r_close_this, redir, 0);
699 | NUMBER GREATER_AND '-'
703 $$ = make_redirection (source, r_close_this, redir, 0);
705 | REDIR_WORD GREATER_AND '-'
707 source.filename = $1;
709 $$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
715 $$ = make_redirection (source, r_close_this, redir, 0);
717 | NUMBER LESS_AND '-'
721 $$ = make_redirection (source, r_close_this, redir, 0);
723 | REDIR_WORD LESS_AND '-'
725 source.filename = $1;
727 $$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
733 $$ = make_redirection (source, r_err_and_out, redir, 0);
735 | AND_GREATER_GREATER WORD
739 $$ = make_redirection (source, r_append_err_and_out, redir, 0);
743 simple_command_element: WORD
744 { $$.word = $1; $$.redirect = 0; }
746 { $$.word = $1; $$.redirect = 0; }
748 { $$.redirect = $1; $$.word = 0; }
751 redirection_list: redirection
755 | redirection_list redirection
757 register REDIRECT *t;
759 for (t = $1; t->next; t = t->next)
766 simple_command: simple_command_element
767 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
768 | simple_command simple_command_element
769 { $$ = make_simple_command ($2, $1); }
772 command: simple_command
773 { $$ = clean_simple_command ($1); }
776 | shell_command redirection_list
781 if (tc && tc->redirects)
783 register REDIRECT *t;
784 for (t = tc->redirects; t->next; t = t->next)
798 shell_command: for_command
802 | WHILE compound_list DO compound_list DONE
803 { $$ = make_while_command ($2, $4); }
804 | UNTIL compound_list DO compound_list DONE
805 { $$ = make_until_command ($2, $4); }
822 for_command: FOR WORD newline_list DO compound_list DONE
824 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
825 if (word_top > 0) word_top--;
827 | FOR WORD newline_list '{' compound_list '}'
829 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
830 if (word_top > 0) word_top--;
832 | FOR WORD ';' newline_list DO compound_list DONE
834 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
835 if (word_top > 0) word_top--;
837 | FOR WORD ';' newline_list '{' compound_list '}'
839 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
840 if (word_top > 0) word_top--;
842 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
844 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
845 if (word_top > 0) word_top--;
847 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
849 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
850 if (word_top > 0) word_top--;
852 | FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
854 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
855 if (word_top > 0) word_top--;
857 | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
859 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
860 if (word_top > 0) word_top--;
864 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
866 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
867 if ($$ == 0) YYERROR;
868 if (word_top > 0) word_top--;
870 | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
872 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
873 if ($$ == 0) YYERROR;
874 if (word_top > 0) word_top--;
876 | FOR ARITH_FOR_EXPRS DO compound_list DONE
878 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
879 if ($$ == 0) YYERROR;
880 if (word_top > 0) word_top--;
882 | FOR ARITH_FOR_EXPRS '{' compound_list '}'
884 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
885 if ($$ == 0) YYERROR;
886 if (word_top > 0) word_top--;
890 select_command: SELECT WORD newline_list DO compound_list DONE
892 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
893 if (word_top > 0) word_top--;
895 | SELECT WORD newline_list '{' compound_list '}'
897 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
898 if (word_top > 0) word_top--;
900 | SELECT WORD ';' newline_list DO compound_list DONE
902 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
903 if (word_top > 0) word_top--;
905 | SELECT WORD ';' newline_list '{' compound_list '}'
907 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
908 if (word_top > 0) word_top--;
910 | SELECT WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
912 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
913 if (word_top > 0) word_top--;
915 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
917 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
918 if (word_top > 0) word_top--;
920 | SELECT WORD newline_list IN list_terminator newline_list DO compound_list DONE
922 $$ = make_select_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
923 if (word_top > 0) word_top--;
925 | SELECT WORD newline_list IN list_terminator newline_list '{' compound_list '}'
927 $$ = make_select_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
928 if (word_top > 0) word_top--;
932 case_command: CASE WORD newline_list IN newline_list ESAC
934 $$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]);
935 if (word_top > 0) word_top--;
937 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
939 $$ = make_case_command ($2, $5, word_lineno[word_top]);
940 if (word_top > 0) word_top--;
942 | CASE WORD newline_list IN case_clause ESAC
944 $$ = make_case_command ($2, $5, word_lineno[word_top]);
945 if (word_top > 0) word_top--;
949 function_def: WORD '(' ')' newline_list function_body
950 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
951 | FUNCTION WORD '(' ')' newline_list function_body
952 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
953 | FUNCTION WORD function_body
954 { $$ = make_function_def ($2, $3, function_dstart, function_bstart); }
955 | FUNCTION WORD '\n' newline_list function_body
956 { $$ = make_function_def ($2, $5, function_dstart, function_bstart); }
959 function_body: shell_command
961 | shell_command redirection_list
966 /* According to Posix.2 3.9.5, redirections
967 specified after the body of a function should
968 be attached to the function and performed when
969 the function is executed, not as part of the
970 function definition command. */
971 /* XXX - I don't think it matters, but we might
972 want to change this in the future to avoid
973 problems differentiating between a function
974 definition with a redirection and a function
975 definition containing a single command with a
976 redirection. The two are semantically equivalent,
977 though -- the only difference is in how the
978 command printing code displays the redirections. */
979 if (tc && tc->redirects)
981 register REDIRECT *t;
982 for (t = tc->redirects; t->next; t = t->next)
992 subshell: '(' compound_list ')'
994 $$ = make_subshell_command ($2);
995 $$->flags |= CMD_WANT_SUBSHELL;
999 comsub: DOLPAREN compound_list ')'
1003 | DOLPAREN newline_list ')'
1005 $$ = (COMMAND *)NULL;
1009 coproc: COPROC shell_command
1011 $$ = make_coproc_command ("COPROC", $2);
1012 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
1014 | COPROC shell_command redirection_list
1019 if (tc && tc->redirects)
1021 register REDIRECT *t;
1022 for (t = tc->redirects; t->next; t = t->next)
1028 $$ = make_coproc_command ("COPROC", $2);
1029 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
1031 | COPROC WORD shell_command
1033 $$ = make_coproc_command ($2->word, $3);
1034 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
1036 | COPROC WORD shell_command redirection_list
1041 if (tc && tc->redirects)
1043 register REDIRECT *t;
1044 for (t = tc->redirects; t->next; t = t->next)
1050 $$ = make_coproc_command ($2->word, $3);
1051 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
1053 | COPROC simple_command
1055 $$ = make_coproc_command ("COPROC", clean_simple_command ($2));
1056 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
1060 if_command: IF compound_list THEN compound_list FI
1061 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
1062 | IF compound_list THEN compound_list ELSE compound_list FI
1063 { $$ = make_if_command ($2, $4, $6); }
1064 | IF compound_list THEN compound_list elif_clause FI
1065 { $$ = make_if_command ($2, $4, $5); }
1069 group_command: '{' compound_list '}'
1070 { $$ = make_group_command ($2); }
1073 arith_command: ARITH_CMD
1074 { $$ = make_arith_command ($1); }
1077 cond_command: COND_START COND_CMD COND_END
1081 elif_clause: ELIF compound_list THEN compound_list
1082 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
1083 | ELIF compound_list THEN compound_list ELSE compound_list
1084 { $$ = make_if_command ($2, $4, $6); }
1085 | ELIF compound_list THEN compound_list elif_clause
1086 { $$ = make_if_command ($2, $4, $5); }
1089 case_clause: pattern_list
1090 | case_clause_sequence pattern_list
1091 { $2->next = $1; $$ = $2; }
1094 pattern_list: newline_list pattern ')' compound_list
1095 { $$ = make_pattern_list ($2, $4); }
1096 | newline_list pattern ')' newline_list
1097 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
1098 | newline_list '(' pattern ')' compound_list
1099 { $$ = make_pattern_list ($3, $5); }
1100 | newline_list '(' pattern ')' newline_list
1101 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
1104 case_clause_sequence: pattern_list SEMI_SEMI
1106 | case_clause_sequence pattern_list SEMI_SEMI
1107 { $2->next = $1; $$ = $2; }
1108 | pattern_list SEMI_AND
1109 { $1->flags |= CASEPAT_FALLTHROUGH; $$ = $1; }
1110 | case_clause_sequence pattern_list SEMI_AND
1111 { $2->flags |= CASEPAT_FALLTHROUGH; $2->next = $1; $$ = $2; }
1112 | pattern_list SEMI_SEMI_AND
1113 { $1->flags |= CASEPAT_TESTNEXT; $$ = $1; }
1114 | case_clause_sequence pattern_list SEMI_SEMI_AND
1115 { $2->flags |= CASEPAT_TESTNEXT; $2->next = $1; $$ = $2; }
1119 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
1121 { $$ = make_word_list ($3, $1); }
1124 /* A list allows leading or trailing newlines and
1125 newlines as operators (equivalent to semicolons).
1126 It must end with a newline or semicolon.
1127 Lists are used within commands such as if, for, while. */
1129 compound_list: newline_list list0
1132 if (need_here_doc && last_read_token == '\n')
1133 gather_here_documents ();
1135 | newline_list list1
1141 list0: list1 '\n' newline_list
1142 | list1 '&' newline_list
1144 if ($1->type == cm_connection)
1145 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
1147 $$ = command_connect ($1, (COMMAND *)NULL, '&');
1149 | list1 ';' newline_list
1153 list1: list1 AND_AND newline_list list1
1154 { $$ = command_connect ($1, $4, AND_AND); }
1155 | list1 OR_OR newline_list list1
1156 { $$ = command_connect ($1, $4, OR_OR); }
1157 | list1 '&' newline_list list1
1159 if ($1->type == cm_connection)
1160 $$ = connect_async_list ($1, $4, '&');
1162 $$ = command_connect ($1, $4, '&');
1164 | list1 ';' newline_list list1
1165 { $$ = command_connect ($1, $4, ';'); }
1166 | list1 '\n' newline_list list1
1168 if (parser_state & PST_CMDSUBST)
1169 $$ = command_connect ($1, $4, '\n');
1171 $$ = command_connect ($1, $4, ';');
1177 simple_list_terminator: '\n'
1181 list_terminator:'\n'
1193 /* A simple_list is a list that contains no significant newlines
1194 and no leading or trailing newlines. Newlines are allowed
1195 only following operators, where they are not significant.
1197 This is what an inputunit consists of. */
1199 simple_list: simple_list1
1203 gather_here_documents (); /* XXX */
1204 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1206 INTERNAL_DEBUG (("LEGACY: parser: command substitution simple_list1 -> simple_list"));
1207 global_command = $1;
1208 eof_encountered = 0;
1209 if (bash_input.type == st_string)
1210 rewind_input_string ();
1216 if ($1->type == cm_connection)
1217 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
1219 $$ = command_connect ($1, (COMMAND *)NULL, '&');
1221 gather_here_documents (); /* XXX */
1222 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1224 INTERNAL_DEBUG (("LEGACY: parser: command substitution simple_list1 '&' -> simple_list"));
1225 global_command = $1;
1226 eof_encountered = 0;
1227 if (bash_input.type == st_string)
1228 rewind_input_string ();
1236 gather_here_documents (); /* XXX */
1237 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1239 INTERNAL_DEBUG (("LEGACY: parser: command substitution simple_list1 ';' -> simple_list"));
1240 global_command = $1;
1241 eof_encountered = 0;
1242 if (bash_input.type == st_string)
1243 rewind_input_string ();
1249 simple_list1: simple_list1 AND_AND newline_list simple_list1
1250 { $$ = command_connect ($1, $4, AND_AND); }
1251 | simple_list1 OR_OR newline_list simple_list1
1252 { $$ = command_connect ($1, $4, OR_OR); }
1253 | simple_list1 '&' simple_list1
1255 if ($1->type == cm_connection)
1256 $$ = connect_async_list ($1, $3, '&');
1258 $$ = command_connect ($1, $3, '&');
1260 | simple_list1 ';' simple_list1
1261 { $$ = command_connect ($1, $3, ';'); }
1267 pipeline_command: pipeline
1269 | BANG pipeline_command
1272 $2->flags ^= CMD_INVERT_RETURN; /* toggle */
1275 | timespec pipeline_command
1281 | timespec list_terminator
1285 /* Boy, this is unclean. `time' by itself can
1286 time a null command. We cheat and push a
1287 newline back if the list_terminator was a newline
1288 to avoid the double-newline problem (one to
1289 terminate this, one to terminate the command) */
1292 $$ = make_simple_command (x, (COMMAND *)NULL);
1294 /* XXX - let's cheat and push a newline back */
1296 token_to_read = '\n';
1298 token_to_read = ';';
1299 parser_state &= ~PST_REDIRLIST; /* make_simple_command sets this */
1301 | BANG list_terminator
1305 /* This is just as unclean. Posix says that `!'
1306 by itself should be equivalent to `false'.
1308 newline back if the list_terminator was a newline
1309 to avoid the double-newline problem (one to
1310 terminate this, one to terminate the command) */
1313 $$ = make_simple_command (x, (COMMAND *)NULL);
1314 $$->flags |= CMD_INVERT_RETURN;
1315 /* XXX - let's cheat and push a newline back */
1317 token_to_read = '\n';
1319 token_to_read = ';';
1320 parser_state &= ~PST_REDIRLIST; /* make_simple_command sets this */
1324 pipeline: pipeline '|' newline_list pipeline
1325 { $$ = command_connect ($1, $4, '|'); }
1326 | pipeline BAR_AND newline_list pipeline
1328 /* Make cmd1 |& cmd2 equivalent to cmd1 2>&1 | cmd2 */
1333 tc = $1->type == cm_simple ? (COMMAND *)$1->value.Simple : $1;
1336 r = make_redirection (sd, r_duplicating_output, rd, 0);
1339 register REDIRECT *t;
1340 for (t = tc->redirects; t->next; t = t->next)
1347 $$ = command_connect ($1, $4, '|');
1354 { $$ = CMD_TIME_PIPELINE; }
1356 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1358 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1359 | TIME TIMEOPT TIMEIGN
1360 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1364 /* Initial size to allocate for tokens, and the
1365 amount to grow them by. */
1366 #define TOKEN_DEFAULT_INITIAL_SIZE 496
1367 #define TOKEN_DEFAULT_GROW_SIZE 512
1369 /* Should we call prompt_again? */
1370 #define SHOULD_PROMPT() \
1371 (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
1374 # define expanding_alias() (pushed_string_list && pushed_string_list->expander)
1376 # define expanding_alias() 0
1379 /* Global var is non-zero when end of file has been reached. */
1380 int EOF_Reached = 0;
1389 yyoutstream = stdout;
1390 yyerrstream = stderr;
1395 /* yy_getc () returns the next available character from input or EOF.
1396 yy_ungetc (c) makes `c' the next character to read.
1397 init_yy_io (get, unget, type, location) makes the function GET the
1398 installed function for getting the next character, makes UNGET the
1399 installed function for un-getting a character, sets the type of stream
1400 (either string or file) from TYPE, and makes LOCATION point to where
1401 the input is coming from. */
1403 /* Unconditionally returns end-of-file. */
1410 /* Variable containing the current get and unget functions.
1411 See ./input.h for a clearer description. */
1412 BASH_INPUT bash_input;
1414 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
1415 is non-null, avoiding a memory leak. */
1417 initialize_bash_input ()
1419 bash_input.type = st_none;
1420 FREE (bash_input.name);
1421 bash_input.name = (char *)NULL;
1422 bash_input.location.file = (FILE *)NULL;
1423 bash_input.location.string = (char *)NULL;
1424 bash_input.getter = (sh_cget_func_t *)NULL;
1425 bash_input.ungetter = (sh_cunget_func_t *)NULL;
1428 /* Set the contents of the current bash input stream from
1429 GET, UNGET, TYPE, NAME, and LOCATION. */
1431 init_yy_io (get, unget, type, name, location)
1432 sh_cget_func_t *get;
1433 sh_cunget_func_t *unget;
1434 enum stream_type type;
1436 INPUT_STREAM location;
1438 bash_input.type = type;
1439 FREE (bash_input.name);
1440 bash_input.name = name ? savestring (name) : (char *)NULL;
1444 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
1446 bash_input.location = location;
1448 bash_input.getter = get;
1449 bash_input.ungetter = unget;
1455 return (bash_input.name ? bash_input.name : "stdin");
1458 /* Call this to get the next character of input. */
1462 return (*(bash_input.getter)) ();
1465 /* Call this to unget C. That is, to make C the next character
1471 return (*(bash_input.ungetter)) (c);
1474 #if defined (BUFFERED_INPUT)
1475 #ifdef INCLUDE_UNUSED
1477 input_file_descriptor ()
1479 switch (bash_input.type)
1482 return (fileno (bash_input.location.file));
1484 return (bash_input.location.buffered_fd);
1487 return (fileno (stdin));
1491 #endif /* BUFFERED_INPUT */
1493 /* **************************************************************** */
1495 /* Let input be read from readline (). */
1497 /* **************************************************************** */
1499 #if defined (READLINE)
1500 char *current_readline_prompt = (char *)NULL;
1501 char *current_readline_line = (char *)NULL;
1502 int current_readline_line_index = 0;
1507 SigHandler *old_sigint;
1511 if (current_readline_line == 0)
1513 if (bash_readline_initialized == 0)
1514 initialize_readline ();
1516 #if defined (JOB_CONTROL)
1518 give_terminal_to (shell_pgrp, 0);
1519 #endif /* JOB_CONTROL */
1521 old_sigint = IMPOSSIBLE_TRAP_HANDLER;
1522 if (signal_is_ignored (SIGINT) == 0)
1524 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
1527 sh_unset_nodelay_mode (fileno (rl_instream)); /* just in case */
1528 current_readline_line = readline (current_readline_prompt ?
1529 current_readline_prompt : "");
1532 if (signal_is_ignored (SIGINT) == 0)
1534 if (old_sigint != IMPOSSIBLE_TRAP_HANDLER)
1535 set_signal_handler (SIGINT, old_sigint);
1539 /* Reset the prompt to the decoded value of prompt_string_pointer. */
1540 reset_readline_prompt ();
1543 if (current_readline_line == 0)
1546 current_readline_line_index = 0;
1547 line_len = strlen (current_readline_line);
1549 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
1550 current_readline_line[line_len++] = '\n';
1551 current_readline_line[line_len] = '\0';
1554 if (current_readline_line[current_readline_line_index] == 0)
1556 free (current_readline_line);
1557 current_readline_line = (char *)NULL;
1558 return (yy_readline_get ());
1562 c = current_readline_line[current_readline_line_index++];
1568 yy_readline_unget (c)
1571 if (current_readline_line_index && current_readline_line)
1572 current_readline_line[--current_readline_line_index] = c;
1577 with_input_from_stdin ()
1579 INPUT_STREAM location;
1581 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1583 location.string = current_readline_line;
1584 init_yy_io (yy_readline_get, yy_readline_unget,
1585 st_stdin, "readline stdin", location);
1589 /* Will we be collecting another input line and printing a prompt? This uses
1590 different conditions than SHOULD_PROMPT(), since readline allows a user to
1591 embed a newline in the middle of the line it collects, which the parser
1592 will interpret as a line break and command delimiter. */
1594 parser_will_prompt ()
1596 return (current_readline_line == 0 || current_readline_line[current_readline_line_index] == 0);
1599 #else /* !READLINE */
1602 with_input_from_stdin ()
1604 with_input_from_stream (stdin, "stdin");
1606 #endif /* !READLINE */
1608 /* **************************************************************** */
1610 /* Let input come from STRING. STRING is zero terminated. */
1612 /* **************************************************************** */
1617 register char *string;
1618 register unsigned char c;
1620 string = bash_input.location.string;
1622 /* If the string doesn't exist, or is empty, EOF found. */
1623 if (string && *string)
1626 bash_input.location.string = string;
1637 *(--bash_input.location.string) = c;
1642 with_input_from_string (string, name)
1646 INPUT_STREAM location;
1648 location.string = string;
1649 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1652 /* Count the number of characters we've consumed from bash_input.location.string
1653 and read into shell_input_line, but have not returned from shell_getc.
1654 That is the true input location. Rewind bash_input.location.string by
1655 that number of characters, so it points to the last character actually
1656 consumed by the parser. */
1658 rewind_input_string ()
1662 /* number of unconsumed characters in the input -- XXX need to take newlines
1663 into account, e.g., $(...\n) */
1664 xchars = shell_input_line_len - shell_input_line_index;
1665 if (bash_input.location.string[-1] == '\n')
1668 /* XXX - how to reflect bash_input.location.string back to string passed to
1669 parse_and_execute or xparse_dolparen? xparse_dolparen needs to know how
1670 far into the string we parsed. parse_and_execute knows where bash_input.
1671 location.string is, and how far from orig_string that is -- that's the
1672 number of characters the command consumed. */
1674 /* bash_input.location.string - xchars should be where we parsed to */
1675 /* need to do more validation on xchars value for sanity -- test cases. */
1676 bash_input.location.string -= xchars;
1679 /* **************************************************************** */
1681 /* Let input come from STREAM. */
1683 /* **************************************************************** */
1685 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1686 define, and just use getc/ungetc if it was defined, but since bash
1687 installs most of its signal handlers without the SA_RESTART flag, some
1688 signals received during a read(2) will not cause the read to be restarted.
1689 We will need to restart it ourselves. */
1697 if (bash_input.location.file)
1699 /* XXX - don't need terminate_immediately; getc_with_restart checks
1700 for terminating signals itself if read returns < 0 */
1701 result = getc_with_restart (bash_input.location.file);
1710 return (ungetc_with_restart (c, bash_input.location.file));
1714 with_input_from_stream (stream, name)
1718 INPUT_STREAM location;
1720 location.file = stream;
1721 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1724 typedef struct stream_saver {
1725 struct stream_saver *next;
1726 BASH_INPUT bash_input;
1728 #if defined (BUFFERED_INPUT)
1729 BUFFERED_STREAM *bstream;
1730 #endif /* BUFFERED_INPUT */
1733 /* The globally known line number. */
1734 int line_number = 0;
1736 /* The line number offset set by assigning to LINENO. Not currently used. */
1737 int line_number_base = 0;
1739 #if defined (COND_COMMAND)
1740 static int cond_lineno;
1741 static int cond_token;
1744 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1747 push_stream (reset_lineno)
1750 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1752 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1754 #if defined (BUFFERED_INPUT)
1755 saver->bstream = (BUFFERED_STREAM *)NULL;
1756 /* If we have a buffered stream, clear out buffers[fd]. */
1757 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1758 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1759 (BUFFERED_STREAM *)NULL);
1760 #endif /* BUFFERED_INPUT */
1762 saver->line = line_number;
1763 bash_input.name = (char *)NULL;
1764 saver->next = stream_list;
1765 stream_list = saver;
1778 STREAM_SAVER *saver = stream_list;
1781 stream_list = stream_list->next;
1783 init_yy_io (saver->bash_input.getter,
1784 saver->bash_input.ungetter,
1785 saver->bash_input.type,
1786 saver->bash_input.name,
1787 saver->bash_input.location);
1789 #if defined (BUFFERED_INPUT)
1790 /* If we have a buffered stream, restore buffers[fd]. */
1791 /* If the input file descriptor was changed while this was on the
1792 save stack, update the buffered fd to the new file descriptor and
1793 re-establish the buffer <-> bash_input fd correspondence. */
1794 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1796 if (bash_input_fd_changed)
1798 bash_input_fd_changed = 0;
1799 if (default_buffered_input >= 0)
1801 bash_input.location.buffered_fd = default_buffered_input;
1802 saver->bstream->b_fd = default_buffered_input;
1803 SET_CLOSE_ON_EXEC (default_buffered_input);
1806 /* XXX could free buffered stream returned as result here. */
1807 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1809 #endif /* BUFFERED_INPUT */
1811 line_number = saver->line;
1813 FREE (saver->bash_input.name);
1818 /* Return 1 if a stream of type TYPE is saved on the stack. */
1820 stream_on_stack (type)
1821 enum stream_type type;
1823 register STREAM_SAVER *s;
1825 for (s = stream_list; s; s = s->next)
1826 if (s->bash_input.type == type)
1831 /* Save the current token state and return it in a malloced array. */
1837 ret = (int *)xmalloc (4 * sizeof (int));
1838 ret[0] = last_read_token;
1839 ret[1] = token_before_that;
1840 ret[2] = two_tokens_ago;
1841 ret[3] = current_token;
1846 restore_token_state (ts)
1851 last_read_token = ts[0];
1852 token_before_that = ts[1];
1853 two_tokens_ago = ts[2];
1854 current_token = ts[3];
1858 * This is used to inhibit alias expansion and reserved word recognition
1859 * inside case statement pattern lists. A `case statement pattern list' is:
1861 * everything between the `in' in a `case word in' and the next ')'
1863 * everything between a `;;' and the next `)' or `esac'
1866 #define END_OF_ALIAS 0
1869 * Pseudo-global variables used in implementing token-wise alias expansion.
1873 * Pushing and popping strings. This works together with shell_getc to
1874 * implement alias expansion on a per-token basis.
1877 #define PSH_ALIAS 0x01
1878 #define PSH_DPAREN 0x02
1879 #define PSH_SOURCE 0x04
1880 #define PSH_ARRAY 0x08
1882 typedef struct string_saver {
1883 struct string_saver *next;
1884 int expand_alias; /* Value to set expand_alias to when string is popped. */
1887 alias_t *expander; /* alias that caused this line to be pushed. */
1889 size_t saved_line_size, saved_line_index, saved_line_len;
1890 int saved_line_terminator;
1894 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1897 * Push the current shell_input_line onto a stack of such lines and make S
1898 * the current input. Used when expanding aliases. EXPAND is used to set
1899 * the value of expand_next_token when the string is popped, so that the
1900 * word after the alias in the original line is handled correctly when the
1901 * alias expands to multiple words. TOKEN is the token that was expanded
1902 * into S; it is saved and used to prevent infinite recursive expansion.
1905 push_string (s, expand, ap)
1910 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
1912 temp->expand_alias = expand;
1913 temp->saved_line = shell_input_line;
1914 temp->saved_line_size = shell_input_line_size;
1915 temp->saved_line_len = shell_input_line_len;
1916 temp->saved_line_index = shell_input_line_index;
1917 temp->saved_line_terminator = shell_input_line_terminator;
1920 temp->expander = ap;
1922 temp->flags = PSH_ALIAS;
1924 temp->next = pushed_string_list;
1925 pushed_string_list = temp;
1929 ap->flags |= AL_BEINGEXPANDED;
1932 shell_input_line = s;
1933 shell_input_line_size = shell_input_line_len = STRLEN (s);
1934 shell_input_line_index = 0;
1935 shell_input_line_terminator = '\0';
1937 parser_state &= ~PST_ALEXPNEXT; /* XXX */
1940 set_line_mbstate ();
1944 * Make the top of the pushed_string stack be the current shell input.
1945 * Only called when there is something on the stack. Called from shell_getc
1946 * when it thinks it has consumed the string generated by an alias expansion
1947 * and needs to return to the original input line.
1954 FREE (shell_input_line);
1955 shell_input_line = pushed_string_list->saved_line;
1956 shell_input_line_index = pushed_string_list->saved_line_index;
1957 shell_input_line_size = pushed_string_list->saved_line_size;
1958 shell_input_line_len = pushed_string_list->saved_line_len;
1959 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1962 if (pushed_string_list->expand_alias)
1963 parser_state |= PST_ALEXPNEXT;
1965 parser_state &= ~PST_ALEXPNEXT;
1968 t = pushed_string_list;
1969 pushed_string_list = pushed_string_list->next;
1973 t->expander->flags &= ~AL_BEINGEXPANDED;
1978 set_line_mbstate ();
1984 register STRING_SAVER *t, *t1;
1986 for (t = pushed_string_list; t; )
1989 FREE (t->saved_line);
1992 t->expander->flags &= ~AL_BEINGEXPANDED;
1997 pushed_string_list = (STRING_SAVER *)NULL;
2001 free_pushed_string_input ()
2003 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2004 free_string_list ();
2009 parser_expanding_alias ()
2011 return (expanding_alias ());
2015 parser_save_alias ()
2017 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2018 push_string ((char *)NULL, 0, (alias_t *)NULL);
2019 pushed_string_list->flags = PSH_SOURCE; /* XXX - for now */
2026 parser_restore_alias ()
2028 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2029 if (pushed_string_list)
2037 /* Before freeing AP, make sure that there aren't any cases of pointer
2038 aliasing that could cause us to reference freed memory later on. */
2040 clear_string_list_expander (ap)
2043 register STRING_SAVER *t;
2045 for (t = pushed_string_list; t; t = t->next)
2047 if (t->expander && t->expander == ap)
2054 clear_shell_input_line ()
2056 if (shell_input_line)
2057 shell_input_line[shell_input_line_index = 0] = '\0';
2060 /* Return a line of text, taken from wherever yylex () reads input.
2061 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
2062 is non-zero, we remove unquoted \<newline> pairs. This is used by
2063 read_secondary_line to read here documents. */
2065 read_a_line (remove_quoted_newline)
2066 int remove_quoted_newline;
2068 static char *line_buffer = (char *)NULL;
2069 static int buffer_size = 0;
2070 int indx, c, peekc, pass_next;
2072 #if defined (READLINE)
2073 if (no_line_editing && SHOULD_PROMPT ())
2075 if (SHOULD_PROMPT ())
2079 pass_next = indx = 0;
2082 /* Allow immediate exit if interrupted during input. */
2087 /* Ignore null bytes in input. */
2091 /* If there is no more input, then we return NULL. */
2094 if (interactive && bash_input.type == st_stream)
2097 return ((char *)NULL);
2101 /* `+2' in case the final character in the buffer is a newline or we
2102 have to handle CTLESC or CTLNUL. */
2103 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
2105 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
2106 here document with an unquoted delimiter. In this case,
2107 the line will be expanded as if it were in double quotes.
2108 We allow a backslash to escape the next character, but we
2109 need to treat the backslash specially only if a backslash
2110 quoting a backslash-newline pair appears in the line. */
2113 line_buffer[indx++] = c;
2116 else if (c == '\\' && remove_quoted_newline)
2123 continue; /* Make the unquoted \<newline> pair disappear. */
2129 line_buffer[indx++] = c; /* Preserve the backslash. */
2134 /* remove_quoted_newline is non-zero if the here-document delimiter
2135 is unquoted. In this case, we will be expanding the lines and
2136 need to make sure CTLESC and CTLNUL in the input are quoted. */
2137 if (remove_quoted_newline && (c == CTLESC || c == CTLNUL))
2138 line_buffer[indx++] = CTLESC;
2139 line_buffer[indx++] = c;
2144 line_buffer[indx] = '\0';
2145 return (line_buffer);
2150 /* Return a line as in read_a_line (), but insure that the prompt is
2151 the secondary prompt. This is used to read the lines of a here
2152 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
2153 newlines quoted with backslashes while reading the line. It is
2154 non-zero unless the delimiter of the here document was quoted. */
2156 read_secondary_line (remove_quoted_newline)
2157 int remove_quoted_newline;
2162 prompt_string_pointer = &ps2_prompt;
2163 if (SHOULD_PROMPT ())
2165 ret = read_a_line (remove_quoted_newline);
2166 #if defined (HISTORY)
2167 if (ret && remember_on_history && (parser_state & PST_HEREDOC))
2169 /* To make adding the here-document body right, we need to rely on
2170 history_delimiting_chars() returning \n for the first line of the
2171 here-document body and the null string for the second and subsequent
2172 lines, so we avoid double newlines.
2173 current_command_line_count == 2 for the first line of the body. */
2175 current_command_line_count++;
2176 maybe_add_history (ret);
2178 #endif /* HISTORY */
2182 /* **************************************************************** */
2186 /* **************************************************************** */
2188 /* Reserved words. These are only recognized as the first word of a
2190 STRING_INT_ALIST word_token_alist[] = {
2199 #if defined (SELECT_COMMAND)
2200 { "select", SELECT },
2207 { "function", FUNCTION },
2208 #if defined (COMMAND_TIMING)
2214 #if defined (COND_COMMAND)
2215 { "[[", COND_START },
2218 #if defined (COPROCESS_SUPPORT)
2219 { "coproc", COPROC },
2224 /* other tokens that can be returned by read_token() */
2225 STRING_INT_ALIST other_token_alist[] = {
2226 /* Multiple-character tokens with special values */
2231 { ">>", GREATER_GREATER },
2232 { "<<", LESS_LESS },
2234 { ">&", GREATER_AND },
2235 { ";;", SEMI_SEMI },
2237 { ";;&", SEMI_SEMI_AND },
2238 { "<<-", LESS_LESS_MINUS },
2239 { "<<<", LESS_LESS_LESS },
2240 { "&>", AND_GREATER },
2241 { "&>>", AND_GREATER_GREATER },
2242 { "<>", LESS_GREATER },
2243 { ">|", GREATER_BAR },
2245 { "EOF", yacc_EOF },
2246 /* Tokens whose value is the character itself */
2257 { "newline", '\n' },
2261 /* others not listed here:
2262 WORD look at yylval.word
2263 ASSIGNMENT_WORD look at yylval.word
2264 NUMBER look at yylval.number
2265 ARITH_CMD look at yylval.word_list
2266 ARITH_FOR_EXPRS look at yylval.word_list
2267 COND_CMD look at yylval.command
2270 /* These are used by read_token_word, but appear up here so that shell_getc
2271 can use them to decide when to add otherwise blank lines to the history. */
2273 /* The primary delimiter stack. */
2274 struct dstack dstack = { (char *)NULL, 0, 0 };
2276 /* A temporary delimiter stack to be used when decoding prompt strings.
2277 This is needed because command substitutions in prompt strings (e.g., PS2)
2278 can screw up the parser's quoting state. */
2279 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
2281 /* Macro for accessing the top delimiter on the stack. Returns the
2282 delimiter or zero if none. */
2283 #define current_delimiter(ds) \
2284 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
2286 #define push_delimiter(ds, character) \
2289 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
2290 ds.delimiters = (char *)xrealloc \
2291 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
2292 ds.delimiters[ds.delimiter_depth] = character; \
2293 ds.delimiter_depth++; \
2297 #define pop_delimiter(ds) ds.delimiter_depth--
2299 /* Return the next shell input character. This always reads characters
2300 from shell_input_line; when that line is exhausted, it is time to
2301 read the next line. This is called by read_token when the shell is
2302 processing normal command input. */
2304 /* This implements one-character lookahead/lookbehind across physical input
2305 lines, to avoid something being lost because it's pushed back with
2306 shell_ungetc when we're at the start of a line. */
2307 static int eol_ungetc_lookahead = 0;
2309 static int unquoted_backslash = 0;
2312 shell_getc (remove_quoted_newline)
2313 int remove_quoted_newline;
2316 int c, truncating, last_was_backslash;
2321 last_was_backslash = 0;
2322 if (sigwinch_received)
2324 sigwinch_received = 0;
2325 get_new_window_size (0, (int *)0, (int *)0);
2328 if (eol_ungetc_lookahead)
2330 c = eol_ungetc_lookahead;
2331 eol_ungetc_lookahead = 0;
2335 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2336 /* If shell_input_line[shell_input_line_index] == 0, but there is
2337 something on the pushed list of strings, then we don't want to go
2338 off and get another line. We let the code down below handle it. */
2340 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
2341 (pushed_string_list == (STRING_SAVER *)NULL)))
2342 #else /* !ALIAS && !DPAREN_ARITHMETIC */
2343 if (!shell_input_line || !shell_input_line[shell_input_line_index])
2344 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
2348 /* Let's not let one really really long line blow up memory allocation */
2349 if (shell_input_line && shell_input_line_size >= 32768)
2351 free (shell_input_line);
2352 shell_input_line = 0;
2353 shell_input_line_size = 0;
2358 /* Allow immediate exit if interrupted during input. */
2362 shell_input_line_terminator = 0;
2364 /* If the shell is interactive, but not currently printing a prompt
2365 (interactive_shell && interactive == 0), we don't want to print
2366 notifies or cleanup the jobs -- we want to defer it until we do
2367 print the next prompt. */
2368 if (interactive_shell == 0 || SHOULD_PROMPT())
2370 #if defined (JOB_CONTROL)
2371 /* This can cause a problem when reading a command as the result
2372 of a trap, when the trap is called from flush_child. This call
2373 had better not cause jobs to disappear from the job table in
2374 that case, or we will have big trouble. */
2375 notify_and_cleanup ();
2376 #else /* !JOB_CONTROL */
2377 cleanup_dead_jobs ();
2378 #endif /* !JOB_CONTROL */
2381 #if defined (READLINE)
2382 if (no_line_editing && SHOULD_PROMPT())
2384 if (SHOULD_PROMPT())
2388 if (bash_input.type == st_stream)
2395 /* Allow immediate exit if interrupted during input. */
2400 /* If we get EOS while parsing a string, treat it as EOF so we
2401 don't just keep looping. Happens very rarely */
2402 if (bash_input.type == st_string)
2405 shell_input_line_terminator = EOF;
2406 shell_input_line[i] = '\0';
2413 /* Theoretical overflow */
2414 /* If we can't put 256 bytes more into the buffer, allocate
2415 everything we can and fill it as full as we can. */
2416 /* XXX - we ignore rest of line using `truncating' flag */
2417 if (shell_input_line_size > (SIZE_MAX - 256))
2421 n = SIZE_MAX - i; /* how much more can we put into the buffer? */
2422 if (n <= 2) /* we have to save 1 for the newline added below */
2424 if (truncating == 0)
2425 internal_warning(_("shell_getc: shell_input_line_size (%zu) exceeds SIZE_MAX (%lu): line truncated"), shell_input_line_size, (unsigned long)SIZE_MAX);
2426 shell_input_line[i] = '\0';
2429 if (shell_input_line_size < SIZE_MAX)
2431 shell_input_line_size = SIZE_MAX;
2432 shell_input_line = xrealloc (shell_input_line, shell_input_line_size);
2436 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
2440 if (bash_input.type == st_stream)
2444 shell_input_line_terminator = EOF;
2446 shell_input_line[i] = '\0';
2450 if (truncating == 0 || c == '\n')
2451 shell_input_line[i++] = c;
2455 shell_input_line[--i] = '\0';
2456 current_command_line_count++;
2460 last_was_backslash = last_was_backslash == 0 && c == '\\';
2463 shell_input_line_index = 0;
2464 shell_input_line_len = i; /* == strlen (shell_input_line) */
2466 set_line_mbstate ();
2468 #if defined (HISTORY)
2469 if (remember_on_history && shell_input_line && shell_input_line[0])
2472 # if defined (BANG_HISTORY)
2473 /* If the current delimiter is a single quote, we should not be
2474 performing history expansion, even if we're on a different
2475 line from the original single quote. */
2476 if (current_delimiter (dstack) == '\'')
2477 history_quoting_state = '\'';
2478 else if (current_delimiter (dstack) == '"')
2479 history_quoting_state = '"';
2481 history_quoting_state = 0;
2483 /* Calling with a third argument of 1 allows remember_on_history to
2484 determine whether or not the line is saved to the history list */
2485 expansions = pre_process_line (shell_input_line, 1, 1);
2486 # if defined (BANG_HISTORY)
2487 history_quoting_state = 0;
2489 if (expansions != shell_input_line)
2491 free (shell_input_line);
2492 shell_input_line = expansions;
2493 shell_input_line_len = shell_input_line ?
2494 strlen (shell_input_line) : 0;
2495 if (shell_input_line_len == 0)
2496 current_command_line_count--;
2498 /* We have to force the xrealloc below because we don't know
2499 the true allocated size of shell_input_line anymore. */
2500 shell_input_line_size = shell_input_line_len;
2502 set_line_mbstate ();
2505 /* Try to do something intelligent with blank lines encountered while
2506 entering multi-line commands. XXX - this is grotesque */
2507 else if (remember_on_history && shell_input_line &&
2508 shell_input_line[0] == '\0' &&
2509 current_command_line_count > 1)
2511 if (current_delimiter (dstack))
2512 /* We know shell_input_line[0] == 0 and we're reading some sort of
2513 quoted string. This means we've got a line consisting of only
2514 a newline in a quoted string. We want to make sure this line
2515 gets added to the history. */
2516 maybe_add_history (shell_input_line);
2520 hdcs = history_delimiting_chars (shell_input_line);
2521 if (hdcs && hdcs[0] == ';')
2522 maybe_add_history (shell_input_line);
2526 #endif /* HISTORY */
2528 if (shell_input_line)
2530 /* Lines that signify the end of the shell's input should not be
2531 echoed. We should not echo lines while parsing command
2532 substitutions with recursive calls into the parsing engine; those
2533 should only be echoed once when we read the word. That is the
2534 reason for the test against shell_eof_token, which is set to a
2535 right paren when parsing the contents of command substitutions. */
2536 if (echo_input_at_read && (shell_input_line[0] ||
2537 shell_input_line_terminator != EOF) &&
2538 shell_eof_token == 0)
2539 fprintf (stderr, "%s\n", shell_input_line);
2543 shell_input_line_size = 0;
2544 prompt_string_pointer = ¤t_prompt_string;
2545 if (SHOULD_PROMPT ())
2550 /* Add the newline to the end of this string, iff the string does
2551 not already end in an EOF character. */
2552 if (shell_input_line_terminator != EOF)
2554 if (shell_input_line_size < SIZE_MAX-3 && (shell_input_line_len+3 > shell_input_line_size))
2555 shell_input_line = (char *)xrealloc (shell_input_line,
2556 1 + (shell_input_line_size += 2));
2558 /* Don't add a newline to a string that ends with a backslash if we're
2559 going to be removing quoted newlines, since that will eat the
2560 backslash. Add another backslash instead (will be removed by
2562 if (bash_input.type == st_string && expanding_alias() == 0 && last_was_backslash && c == EOF && remove_quoted_newline)
2563 shell_input_line[shell_input_line_len] = '\\';
2565 shell_input_line[shell_input_line_len] = '\n';
2566 shell_input_line[shell_input_line_len + 1] = '\0';
2568 #if defined (HANDLE_MULTIBYTE)
2569 /* This is kind of an abstraction violation, but there's no need to
2570 go through the entire shell_input_line again with a call to
2571 set_line_mbstate(). */
2572 EXTEND_SHELL_INPUT_LINE_PROPERTY();
2573 shell_input_line_property[shell_input_line_len] = 1;
2579 if (shell_input_line_index == 0)
2580 unquoted_backslash = 0;
2582 uc = shell_input_line[shell_input_line_index];
2586 unquoted_backslash = unquoted_backslash == 0 && uc == '\\';
2587 shell_input_line_index++;
2590 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2591 /* If UC is NULL, we have reached the end of the current input string. If
2592 pushed_string_list is non-empty, it's time to pop to the previous string
2593 because we have fully consumed the result of the last alias expansion.
2594 Do it transparently; just return the next character of the string popped
2596 /* If pushed_string_list != 0 but pushed_string_list->expander == 0 (not
2597 currently tested) and the flags value is not PSH_SOURCE, we are not
2598 parsing an alias, we have just saved one (push_string, when called by
2599 the parse_dparen code) In this case, just go on as well. The PSH_SOURCE
2600 case is handled below. */
2602 /* If we're at the end of an alias expansion add a space to make sure that
2603 the alias remains marked as being in use while we expand its last word.
2604 This makes sure that pop_string doesn't mark the alias as not in use
2605 before the string resulting from the alias expansion is tokenized and
2606 checked for alias expansion, preventing recursion. At this point, the
2607 last character in shell_input_line is the last character of the alias
2608 expansion. We test that last character to determine whether or not to
2609 return the space that will delimit the token and postpone the pop_string.
2610 This set of conditions duplicates what used to be in mk_alexpansion ()
2611 below, with the addition that we don't add a space if we're currently
2612 reading a quoted string or in a shell comment. */
2613 #ifndef OLD_ALIAS_HACK
2614 if (uc == 0 && pushed_string_list && pushed_string_list->flags != PSH_SOURCE &&
2615 pushed_string_list->flags != PSH_DPAREN &&
2616 (parser_state & PST_COMMENT) == 0 &&
2617 (parser_state & PST_ENDALIAS) == 0 && /* only once */
2618 shell_input_line_index > 0 &&
2619 shellblank (shell_input_line[shell_input_line_index-1]) == 0 &&
2620 shell_input_line[shell_input_line_index-1] != '\n' &&
2621 unquoted_backslash == 0 &&
2622 shellmeta (shell_input_line[shell_input_line_index-1]) == 0 &&
2623 (current_delimiter (dstack) != '\'' && current_delimiter (dstack) != '"'))
2625 parser_state |= PST_ENDALIAS;
2626 /* We need to do this to make sure last_shell_getc_is_singlebyte returns
2627 true, since we are returning a single-byte space. */
2628 if (shell_input_line_index == shell_input_line_len && last_shell_getc_is_singlebyte == 0)
2631 EXTEND_SHELL_INPUT_LINE_PROPERTY();
2632 shell_input_line_property[shell_input_line_len++] = 1;
2633 /* extend shell_input_line to accommodate the shell_ungetc that
2634 read_token_word() will perform, since we're extending the index */
2635 RESIZE_MALLOCED_BUFFER (shell_input_line, shell_input_line_index, 2, shell_input_line_size, 16);
2636 shell_input_line[++shell_input_line_index] = '\0'; /* XXX */
2638 shell_input_line_property[shell_input_line_index - 1] = 1;
2641 return ' '; /* END_ALIAS */
2646 #endif /* ALIAS || DPAREN_ARITHMETIC */
2647 /* This case works for PSH_DPAREN as well as the shell_ungets() case that uses
2649 if (uc == 0 && pushed_string_list && pushed_string_list->flags != PSH_SOURCE)
2651 parser_state &= ~PST_ENDALIAS;
2653 uc = shell_input_line[shell_input_line_index];
2655 shell_input_line_index++;
2658 if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n')
2660 if (SHOULD_PROMPT ())
2664 /* What do we do here if we're expanding an alias whose definition
2665 includes an escaped newline? If that's the last character in the
2666 alias expansion, we just pop the pushed string list (recall that
2667 we inhibit the appending of a space if newline is the last
2668 character). If it's not the last character, we need to consume the
2669 quoted newline and move to the next character in the expansion. */
2671 if (expanding_alias () && shell_input_line[shell_input_line_index+1] == '\0')
2676 else if (expanding_alias () && shell_input_line[shell_input_line_index+1] != '\0')
2678 shell_input_line_index++; /* skip newline */
2679 goto next_alias_char; /* and get next character */
2686 if (uc == 0 && shell_input_line_terminator == EOF)
2687 return ((shell_input_line_index != 0) ? '\n' : EOF);
2689 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2690 /* We already know that we are not parsing an alias expansion because of the
2691 check for expanding_alias() above. This knows how parse_and_execute
2692 handles switching to st_string input while an alias is being expanded,
2693 hence the check for pushed_string_list without pushed_string_list->expander
2694 and the check for PSH_SOURCE as pushed_string_list->flags.
2695 parse_and_execute and parse_string both change the input type to st_string
2696 and place the string to be parsed and executed into location.string, so
2697 we should not stop reading that until the pointer is '\0'.
2698 The check for shell_input_line_terminator may be superfluous.
2700 This solves the problem of `.' inside a multi-line alias with embedded
2701 newlines executing things out of order. */
2702 if (uc == 0 && bash_input.type == st_string && *bash_input.location.string &&
2703 pushed_string_list && pushed_string_list->flags == PSH_SOURCE &&
2704 shell_input_line_terminator == 0)
2706 shell_input_line_index = 0;
2714 /* Put C back into the input for the shell. This might need changes for
2715 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
2716 character different than we read, shell_input_line_property doesn't need
2717 to change when manipulating shell_input_line. The define for
2718 last_shell_getc_is_singlebyte should take care of it, though. */
2723 if (shell_input_line && shell_input_line_index)
2724 shell_input_line[--shell_input_line_index] = c;
2726 eol_ungetc_lookahead = c;
2729 /* Push S back into shell_input_line; updating shell_input_line_index */
2734 size_t slen, chars_left;
2738 if (shell_input_line[shell_input_line_index] == '\0')
2740 /* Easy, just overwrite shell_input_line. This is preferred because it
2741 saves on set_line_mbstate () and other overhead like push_string */
2742 if (shell_input_line_size <= slen)
2743 RESIZE_MALLOCED_BUFFER (shell_input_line, shell_input_line_index, slen + 1, shell_input_line_size, 64);
2744 strcpy (shell_input_line, s);
2745 shell_input_line_index = 0;
2746 shell_input_line_len = slen;
2747 shell_input_line_terminator = 0;
2749 else if (shell_input_line_index >= slen)
2751 /* Just as easy, just back up shell_input_line_index, but it means we
2752 will re-process some characters in set_line_mbstate(). Need to
2753 watch pushing back newlines here. */
2755 shell_input_line[--shell_input_line_index] = s[--slen];
2757 else if (s[slen - 1] == '\n')
2759 push_string (savestring (s), 0, (alias_t *)NULL);
2760 /* push_string does set_line_mbstate () */
2765 /* Harder case: pushing back input string that's longer than what we've
2766 consumed from shell_input_line so far. */
2767 INTERNAL_DEBUG (("shell_ungets: not at end of shell_input_line"));
2769 chars_left = shell_input_line_len - shell_input_line_index;
2770 if (shell_input_line_size <= (slen + chars_left))
2771 RESIZE_MALLOCED_BUFFER (shell_input_line, shell_input_line_index, chars_left + slen + 1, shell_input_line_size, 64);
2772 memmove (shell_input_line + slen, shell_input_line + shell_input_line_index, shell_input_line_len - shell_input_line_index);
2773 strcpy (shell_input_line, s);
2774 shell_input_line_index = 0;
2775 shell_input_line_len = strlen (shell_input_line); /* chars_left + slen? */
2778 #if defined (HANDLE_MULTIBYTE)
2779 set_line_mbstate (); /* XXX */
2784 parser_remaining_input ()
2786 if (shell_input_line == 0)
2788 if ((int)shell_input_line_index < 0 || shell_input_line_index >= shell_input_line_len)
2789 return ""; /* XXX */
2790 return (shell_input_line + shell_input_line_index);
2793 #ifdef INCLUDE_UNUSED
2794 /* Back the input pointer up by one, effectively `ungetting' a character. */
2798 if (shell_input_line && shell_input_line_index)
2799 shell_input_line_index--;
2803 /* Discard input until CHARACTER is seen, then push that character back
2804 onto the input stream. */
2806 discard_until (character)
2811 while ((c = shell_getc (0)) != EOF && c != character)
2819 execute_variable_command (command, vname)
2820 char *command, *vname;
2823 sh_parser_state_t ps;
2825 save_parser_state (&ps);
2826 last_lastarg = get_string_value ("_");
2828 last_lastarg = savestring (last_lastarg);
2830 parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST);
2832 restore_parser_state (&ps);
2833 bind_variable ("_", last_lastarg, 0);
2834 FREE (last_lastarg);
2836 if (token_to_read == '\n') /* reset_parser was called */
2844 two_tokens_ago = token_before_that;
2845 token_before_that = last_read_token;
2846 last_read_token = current_token;
2851 /* Place to remember the token. We try to keep the buffer
2852 at a reasonable size, but it can grow. */
2853 static char *token = (char *)NULL;
2855 /* Current size of the token buffer. */
2856 static size_t token_buffer_size;
2858 /* Command to read_token () explaining what we want it to do. */
2861 #define prompt_is_ps1 \
2862 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
2864 /* Function for yyparse to call. yylex keeps track of
2865 the last two tokens read, and calls read_token. */
2869 if (interactive && (current_token == 0 || current_token == '\n'))
2871 /* Before we print a prompt, we might have to check mailboxes.
2872 We do this only if it is time to do so. Notice that only here
2873 is the mail alarm reset; nothing takes place in check_mail ()
2874 except the checking of mail. Please don't change this. */
2875 if (prompt_is_ps1 && parse_and_execute_level == 0 && time_to_check_mail ())
2878 reset_mail_timer ();
2881 /* Avoid printing a prompt if we're not going to read anything, e.g.
2882 after resetting the parser with read_token (RESET). */
2883 if (token_to_read == 0 && SHOULD_PROMPT ())
2887 two_tokens_ago = token_before_that;
2888 token_before_that = last_read_token;
2889 last_read_token = current_token;
2890 current_token = read_token (READ);
2892 if ((parser_state & PST_EOFTOKEN) && current_token == shell_eof_token)
2894 /* placeholder for any special handling. */
2895 return (current_token);
2898 if (current_token < 0)
2899 #if defined (YYERRCODE) && !defined (YYUNDEF)
2900 current_token = YYERRCODE;
2902 current_token = YYerror;
2905 return (current_token);
2908 /* When non-zero, we have read the required tokens
2909 which allow ESAC to be the next one read. */
2910 static int esacs_needed_count;
2912 /* When non-zero, we can read IN as an acceptable token, regardless of how
2913 many newlines we read. */
2914 static int expecting_in_token;
2920 if (need_here_doc >= HEREDOC_MAX)
2922 last_command_exit_value = EX_BADUSAGE;
2924 report_syntax_error (_("maximum here-document count exceeded"));
2926 exit_shell (last_command_exit_value);
2928 redir_stack[need_here_doc++] = r;
2932 gather_here_documents ()
2937 here_doc_first_line = 1;
2938 while (need_here_doc > 0)
2940 parser_state |= PST_HEREDOC;
2941 make_here_document (redir_stack[r++], line_number);
2942 parser_state &= ~PST_HEREDOC;
2944 redir_stack[r - 1] = 0; /* XXX */
2946 here_doc_first_line = 0; /* just in case */
2949 /* When non-zero, an open-brace used to create a group is awaiting a close
2951 static int open_brace_count;
2953 /* In the following three macros, `token' is always last_read_token */
2955 /* Are we in the middle of parsing a redirection where we are about to read
2956 a word? This is used to make sure alias expansion doesn't happen in the
2957 middle of a redirection, even though we're parsing a simple command. */
2958 #define parsing_redirection(token) \
2959 (token == '<' || token == '>' || \
2960 token == GREATER_GREATER || token == GREATER_BAR || \
2961 token == LESS_GREATER || token == LESS_LESS_MINUS || \
2962 token == LESS_LESS || token == LESS_LESS_LESS || \
2963 token == LESS_AND || token == GREATER_AND || token == AND_GREATER)
2965 /* Is `token' one that will allow a WORD to be read in a command position?
2966 We can read a simple command name on which we should attempt alias expansion
2967 or we can read an assignment statement. */
2968 #define command_token_position(token) \
2969 (((token) == ASSIGNMENT_WORD) || \
2970 ((parser_state&PST_REDIRLIST) && parsing_redirection(token) == 0) || \
2971 ((token) != SEMI_SEMI && (token) != SEMI_AND && (token) != SEMI_SEMI_AND && reserved_word_acceptable(token)))
2973 /* Are we in a position where we can read an assignment statement? */
2974 #define assignment_acceptable(token) \
2975 (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0))
2977 /* Check to see if TOKEN is a reserved word and return the token
2979 #define CHECK_FOR_RESERVED_WORD(tok) \
2981 if (!dollar_present && !quoted && \
2982 reserved_word_acceptable (last_read_token)) \
2985 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
2986 if (STREQ (tok, word_token_alist[i].word)) \
2988 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
2990 if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \
2992 if ((parser_state & PST_CASEPAT) && last_read_token == '|' && word_token_alist[i].token == ESAC) \
2993 break; /* Posix grammar rule 4 */ \
2994 if ((parser_state & PST_CASEPAT) && last_read_token == '(' && word_token_alist[i].token == ESAC) /*)*/ \
2995 break; /* phantom Posix grammar rule 4 */ \
2996 if (word_token_alist[i].token == ESAC) { \
2997 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
2998 esacs_needed_count--; \
2999 } else if (word_token_alist[i].token == CASE) \
3000 parser_state |= PST_CASESTMT; \
3001 else if (word_token_alist[i].token == COND_END) \
3002 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
3003 else if (word_token_alist[i].token == COND_START) \
3004 parser_state |= PST_CONDCMD; \
3005 else if (word_token_alist[i].token == '{') \
3006 open_brace_count++; \
3007 else if (word_token_alist[i].token == '}' && open_brace_count) \
3008 open_brace_count--; \
3009 return (word_token_alist[i].token); \
3016 /* OK, we have a token. Let's try to alias expand it, if (and only if)
3019 It is eligible for expansion if EXPAND_ALIASES is set, and
3020 the token is unquoted and the last token read was a command
3021 separator (or expand_next_token is set), and we are currently
3022 processing an alias (pushed_string_list is non-empty) and this
3023 token is not the same as the current or any previously
3026 Special cases that disqualify:
3027 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
3037 r = xmalloc (l + 2);
3039 #ifdef OLD_ALIAS_HACK
3040 /* If the last character in the alias is a newline, don't add a trailing
3041 space to the expansion. Works with shell_getc above. */
3042 /* Need to do something about the case where the alias expansion contains
3043 an unmatched quoted string, since appending this space affects the
3044 subsequent output. */
3045 if (l > 0 && r[l - 1] != ' ' && r[l - 1] != '\n' && shellmeta(r[l - 1]) == 0)
3053 alias_expand_token (tokstr)
3060 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
3061 (parser_state & PST_CASEPAT) == 0)
3063 if ((parser_state & PST_ALEXPNEXT) || assignment_acceptable (last_read_token))
3066 ap = find_alias (tokstr);
3068 /* Currently expanding this token. */
3069 if (ap && (ap->flags & AL_BEINGEXPANDED))
3070 return (NO_EXPANSION);
3072 #ifdef OLD_ALIAS_HACK
3073 /* mk_alexpansion puts an extra space on the end of the alias expansion,
3074 so the lookahead by the parser works right (the alias needs to remain
3075 `in use' while parsing its last word to avoid alias recursion for
3076 something like "alias echo=echo"). If this gets changed, make sure
3077 the code in shell_getc that deals with reaching the end of an
3078 expanded alias is changed with it. */
3080 expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL;
3084 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
3085 return (RE_READ_TOKEN);
3088 /* This is an eligible token that does not have an expansion. */
3089 return (NO_EXPANSION);
3091 return (NO_EXPANSION);
3096 time_command_acceptable ()
3098 #if defined (COMMAND_TIMING)
3101 if (posixly_correct && shell_compatibility_level > 41)
3103 /* Quick check of the rest of the line to find the next token. If it
3104 begins with a `-', Posix says to not return `time' as the token.
3105 This was interp 267. */
3106 i = shell_input_line_index;
3107 while (i < shell_input_line_len && (shell_input_line[i] == ' ' || shell_input_line[i] == '\t'))
3109 if (shell_input_line[i] == '-')
3113 switch (last_read_token)
3118 if (token_before_that == '|')
3133 case ')': /* only valid in case statement */
3134 case BANG: /* ! time pipeline */
3135 case TIME: /* time time pipeline */
3136 case TIMEOPT: /* time -p time pipeline */
3137 case TIMEIGN: /* time -p -- ... */
3144 #endif /* COMMAND_TIMING */
3147 /* Handle special cases of token recognition:
3148 IN is recognized if the last token was WORD and the token
3149 before that was FOR or CASE or SELECT.
3151 DO is recognized if the last token was WORD and the token
3152 before that was FOR or SELECT.
3154 ESAC is recognized if the last token caused `esacs_needed_count'
3157 `{' is recognized if the last token as WORD and the token
3158 before that was FUNCTION, or if we just parsed an arithmetic
3161 `}' is recognized if there is an unclosed `{' present.
3163 `-p' is returned as TIMEOPT if the last read token was TIME.
3164 `--' is returned as TIMEIGN if the last read token was TIME or TIMEOPT.
3166 ']]' is returned as COND_END if the parser is currently parsing
3167 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
3169 `time' is returned as TIME if and only if it is immediately
3170 preceded by one of `;', `\n', `||', `&&', or `&'.
3174 special_case_tokens (tokstr)
3177 /* Posix grammar rule 6 */
3178 if ((last_read_token == WORD) &&
3179 #if defined (SELECT_COMMAND)
3180 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
3182 ((token_before_that == FOR) || (token_before_that == CASE)) &&
3184 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
3186 if (token_before_that == CASE)
3188 parser_state |= PST_CASEPAT;
3189 esacs_needed_count++;
3191 if (expecting_in_token)
3192 expecting_in_token--;
3196 /* XXX - leaving above code intact for now, but it should eventually be
3197 removed in favor of this clause. */
3198 /* Posix grammar rule 6 */
3199 if (expecting_in_token && (last_read_token == WORD || last_read_token == '\n') &&
3200 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
3202 if (parser_state & PST_CASESTMT)
3204 parser_state |= PST_CASEPAT;
3205 esacs_needed_count++;
3207 expecting_in_token--;
3210 /* Posix grammar rule 6, third word in FOR: for i; do command-list; done */
3211 else if (expecting_in_token && (last_read_token == '\n' || last_read_token == ';') &&
3212 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
3214 expecting_in_token--;
3218 /* for i do; command-list; done */
3219 if (last_read_token == WORD &&
3220 #if defined (SELECT_COMMAND)
3221 (token_before_that == FOR || token_before_that == SELECT) &&
3223 (token_before_that == FOR) &&
3225 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
3227 if (expecting_in_token)
3228 expecting_in_token--;
3232 /* Ditto for ESAC in the CASE case.
3233 Specifically, this handles "case word in esac", which is a legal
3234 construct, certainly because someone will pass an empty arg to the
3235 case construct, and we don't want it to barf. Of course, we should
3236 insist that the case construct has at least one pattern in it, but
3237 the designers disagree. */
3238 if (esacs_needed_count)
3240 if (last_read_token == IN && STREQ (tokstr, "esac"))
3242 esacs_needed_count--;
3243 parser_state &= ~PST_CASEPAT;
3248 /* The start of a shell function definition. */
3249 if (parser_state & PST_ALLOWOPNBRC)
3251 parser_state &= ~PST_ALLOWOPNBRC;
3252 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
3255 function_bstart = line_number;
3256 return ('{'); /* } */
3260 /* We allow a `do' after a for ((...)) without an intervening
3262 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
3264 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
3267 return ('{'); /* } */
3270 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
3272 open_brace_count--; /* { */
3276 #if defined (COMMAND_TIMING)
3277 /* Handle -p after `time'. */
3278 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
3280 /* Handle -- after `time'. */
3281 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == '-' && !tokstr[2])
3283 /* Handle -- after `time -p'. */
3284 if (last_read_token == TIMEOPT && tokstr[0] == '-' && tokstr[1] == '-' && !tokstr[2])
3288 #if defined (COND_COMMAND) /* [[ */
3289 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
3296 /* Called from shell.c when Control-C is typed at top level. Or
3297 by the error rule at top level. */
3301 dstack.delimiter_depth = 0; /* No delimiters found so far. */
3302 open_brace_count = 0;
3304 #if defined (EXTENDED_GLOB)
3305 /* Reset to global value of extended glob */
3306 if (parser_state & PST_EXTPAT)
3307 extended_glob = global_extglob;
3311 here_doc_first_line = 0;
3313 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
3314 if (pushed_string_list)
3315 free_string_list ();
3316 #endif /* ALIAS || DPAREN_ARITHMETIC */
3318 /* This is where we resynchronize to the next newline on error/reset */
3319 if (shell_input_line)
3321 free (shell_input_line);
3322 shell_input_line = (char *)NULL;
3323 shell_input_line_size = shell_input_line_index = 0;
3326 FREE (word_desc_to_read);
3327 word_desc_to_read = (WORD_DESC *)NULL;
3329 eol_ungetc_lookahead = 0;
3331 current_token = '\n'; /* XXX */
3332 last_read_token = '\n';
3333 token_to_read = '\n';
3337 reset_readahead_token ()
3339 if (token_to_read == '\n')
3343 /* Read the next token. Command can be READ (normal operation) or
3344 RESET (to normalize state). */
3346 read_token (command)
3349 int character; /* Current character. */
3350 int peek_char; /* Temporary look-ahead character. */
3351 int result; /* The thing to return. */
3353 if (command == RESET)
3361 result = token_to_read;
3362 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
3364 yylval.word = word_desc_to_read;
3365 word_desc_to_read = (WORD_DESC *)NULL;
3371 #if defined (COND_COMMAND)
3372 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
3374 cond_lineno = line_number;
3375 parser_state |= PST_CONDEXPR;
3376 yylval.command = parse_cond_command ();
3377 if (cond_token != COND_END)
3382 token_to_read = COND_END;
3383 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
3389 /* This is a place to jump back to once we have successfully expanded a
3390 token with an alias and pushed the string with push_string () */
3394 /* Read a single word from input. Start by skipping blanks. */
3395 while ((character = shell_getc (1)) != EOF && shellblank (character))
3398 if (character == EOF)
3404 /* If we hit the end of the string and we're not expanding an alias (e.g.,
3405 we are eval'ing a string that is an incomplete command), return EOF */
3406 if (character == '\0' && bash_input.type == st_string && expanding_alias() == 0)
3408 INTERNAL_DEBUG (("shell_getc: bash_input.location.string = `%s'", bash_input.location.string));
3413 if MBTEST(character == '#' && (!interactive || interactive_comments))
3415 /* A comment. Discard until EOL or EOF, and then return a newline. */
3416 parser_state |= PST_COMMENT;
3417 discard_until ('\n');
3419 parser_state &= ~PST_COMMENT;
3420 character = '\n'; /* this will take the next if statement and return. */
3423 if MBTEST(character == '\n')
3425 /* If we're about to return an unquoted newline, we can go and collect
3426 the text of any pending here document. */
3428 gather_here_documents ();
3431 parser_state &= ~PST_ALEXPNEXT;
3434 parser_state &= ~PST_ASSIGNOK;
3439 if (parser_state & PST_REGEXP)
3442 /* Shell meta-characters. */
3443 if MBTEST(shellmeta (character))
3446 /* Turn off alias tokenization iff this character sequence would
3447 not leave us ready to read a command. */
3448 if (character == '<' || character == '>')
3449 parser_state &= ~PST_ALEXPNEXT;
3452 parser_state &= ~PST_ASSIGNOK;
3454 /* If we are parsing a command substitution and we have read a character
3455 that marks the end of it, don't bother to skip over quoted newlines
3456 when we read the next token. We're just interested in a character
3457 that will turn this into a two-character token, so we let the higher
3458 layers deal with quoted newlines following the command substitution. */
3459 if ((parser_state & PST_CMDSUBST) && character == shell_eof_token)
3460 peek_char = shell_getc (0);
3462 peek_char = shell_getc (1);
3464 if MBTEST(character == peek_char)
3469 /* If '<' then we could be at "<<" or at "<<-". We have to
3470 look ahead one more character. */
3471 peek_char = shell_getc (1);
3472 if MBTEST(peek_char == '-')
3473 return (LESS_LESS_MINUS);
3474 else if MBTEST(peek_char == '<')
3475 return (LESS_LESS_LESS);
3478 shell_ungetc (peek_char);
3483 return (GREATER_GREATER);
3486 parser_state |= PST_CASEPAT;
3488 parser_state &= ~PST_ALEXPNEXT;
3491 peek_char = shell_getc (1);
3492 if MBTEST(peek_char == '&')
3493 return (SEMI_SEMI_AND);
3496 shell_ungetc (peek_char);
3506 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
3508 result = parse_dparen (character);
3516 else if MBTEST(character == '<' && peek_char == '&')
3518 else if MBTEST(character == '>' && peek_char == '&')
3519 return (GREATER_AND);
3520 else if MBTEST(character == '<' && peek_char == '>')
3521 return (LESS_GREATER);
3522 else if MBTEST(character == '>' && peek_char == '|')
3523 return (GREATER_BAR);
3524 else if MBTEST(character == '&' && peek_char == '>')
3526 peek_char = shell_getc (1);
3527 if MBTEST(peek_char == '>')
3528 return (AND_GREATER_GREATER);
3531 shell_ungetc (peek_char);
3532 return (AND_GREATER);
3535 else if MBTEST(character == '|' && peek_char == '&')
3537 else if MBTEST(character == ';' && peek_char == '&')
3539 parser_state |= PST_CASEPAT;
3541 parser_state &= ~PST_ALEXPNEXT;
3546 shell_ungetc (peek_char);
3548 /* If we look like we are reading the start of a function
3549 definition, then let the reader know about it so that
3550 we will do the right thing with `{'. */
3551 if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
3553 parser_state |= PST_ALLOWOPNBRC;
3555 parser_state &= ~PST_ALEXPNEXT;
3557 function_dstart = line_number;
3560 /* case pattern lists may be preceded by an optional left paren. If
3561 we're not trying to parse a case pattern list, the left paren
3562 indicates a subshell. */
3563 if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
3564 parser_state |= PST_SUBSHELL;
3566 else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
3567 parser_state &= ~PST_CASEPAT;
3569 else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
3570 parser_state &= ~PST_SUBSHELL;
3572 #if defined (PROCESS_SUBSTITUTION)
3573 /* Check for the constructs which introduce process substitution.
3574 Shells running in `posix mode' don't do process substitution. */
3575 if MBTEST((character != '>' && character != '<') || peek_char != '(') /*)*/
3576 #endif /* PROCESS_SUBSTITUTION */
3580 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
3581 if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
3585 /* Okay, if we got this far, we have to read a word. Read one,
3586 and then check it against the known ones. */
3587 result = read_token_word (character);
3589 if (result == RE_READ_TOKEN)
3596 * Match a $(...) or other grouping construct. This has to handle embedded
3597 * quoted strings ('', ``, "") and nested constructs. It also must handle
3598 * reprompting the user, if necessary, after reading a newline, and returning
3599 * correct error values if it reads EOF.
3601 #define P_FIRSTCLOSE 0x0001
3602 #define P_ALLOWESC 0x0002
3603 #define P_DQUOTE 0x0004
3604 #define P_COMMAND 0x0008 /* parsing a command, so look for comments */
3605 #define P_BACKQUOTE 0x0010 /* parsing a backquoted command substitution */
3606 #define P_ARRAYSUB 0x0020 /* parsing a [...] array subscript for assignment */
3607 #define P_DOLBRACE 0x0040 /* parsing a ${...} construct */
3609 /* Lexical state while parsing a grouping construct or $(...). */
3610 #define LEX_WASDOL 0x0001
3611 #define LEX_CKCOMMENT 0x0002
3612 #define LEX_INCOMMENT 0x0004
3613 #define LEX_PASSNEXT 0x0008
3614 #define LEX_RESWDOK 0x0010
3615 #define LEX_CKCASE 0x0020
3616 #define LEX_INCASE 0x0040
3617 #define LEX_INHEREDOC 0x0080
3618 #define LEX_HEREDELIM 0x0100 /* reading here-doc delimiter */
3619 #define LEX_STRIPDOC 0x0200 /* <<- strip tabs from here doc delim */
3620 #define LEX_QUOTEDDOC 0x0400 /* here doc with quoted delim */
3621 #define LEX_INWORD 0x0800
3622 #define LEX_GTLT 0x1000
3623 #define LEX_CKESAC 0x2000 /* check esac after in -- for later */
3624 #define LEX_CASEWD 0x4000 /* word after case */
3625 #define LEX_PATLIST 0x8000 /* case statement pattern list */
3627 #define COMSUB_META(ch) ((ch) == ';' || (ch) == '&' || (ch) == '|')
3629 #define CHECK_NESTRET_ERROR() \
3631 if (nestret == &matched_pair_error) \
3634 return &matched_pair_error; \
3638 #define APPEND_NESTRET() \
3642 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); \
3643 strcpy (ret + retind, nestret); \
3644 retind += nestlen; \
3648 static char matched_pair_error;
3651 parse_matched_pair (qc, open, close, lenp, flags)
3652 int qc; /* `"' if this construct is within double quotes */
3656 int count, ch, prevch, tflags;
3657 int nestlen, ttranslen, start_lineno;
3658 char *ret, *nestret, *ttrans;
3659 int retind, retsize, rflags;
3662 dolbrace_state = (flags & P_DOLBRACE) ? DOLBRACE_PARAM : 0;
3664 /*itrace("parse_matched_pair[%d]: open = %c close = %c flags = %d", line_number, open, close, flags);*/
3668 if ((flags & P_COMMAND) && qc != '`' && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
3669 tflags |= LEX_CKCOMMENT;
3671 /* RFLAGS is the set of flags we want to pass to recursive calls. */
3672 rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE);
3674 ret = (char *)xmalloc (retsize = 64);
3677 start_lineno = line_number;
3678 ch = EOF; /* just in case */
3682 ch = shell_getc (qc != '\'' && (tflags & (LEX_PASSNEXT)) == 0);
3687 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
3688 EOF_Reached = 1; /* XXX */
3689 return (&matched_pair_error);
3692 /* Possible reprompting. */
3693 if MBTEST(ch == '\n' && SHOULD_PROMPT ())
3696 /* Don't bother counting parens or doing anything else if in a comment
3697 or part of a case statement */
3698 if (tflags & LEX_INCOMMENT)
3700 /* Add this character. */
3701 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3704 if MBTEST(ch == '\n')
3705 tflags &= ~LEX_INCOMMENT;
3710 /* Not exactly right yet, should handle shell metacharacters, too. If
3711 any changes are made to this test, make analogous changes to subst.c:
3712 extract_delimited_string(). */
3713 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (retind == 0 || ret[retind-1] == '\n' || shellblank (ret[retind - 1])))
3714 tflags |= LEX_INCOMMENT;
3716 if (tflags & LEX_PASSNEXT) /* last char was backslash */
3718 tflags &= ~LEX_PASSNEXT;
3719 /* XXX - PST_NOEXPAND? */
3720 if MBTEST(qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
3723 retind--; /* swallow previously-added backslash */
3727 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3728 if MBTEST(ch == CTLESC)
3729 ret[retind++] = CTLESC;
3733 /* If we're reparsing the input (e.g., from parse_string_to_word_list),
3734 we've already prepended CTLESC to single-quoted results of $'...'.
3735 We may want to do this for other CTLESC-quoted characters in
3737 else if MBTEST((parser_state & PST_REPARSE) && open == '\'' && (ch == CTLESC || ch == CTLNUL))
3739 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3743 else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
3745 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3746 ret[retind++] = CTLESC;
3750 else if MBTEST(ch == close) /* ending delimiter */
3752 /* handle nested ${...} specially. */
3753 else if MBTEST(open != close && (tflags & LEX_WASDOL) && open == '{' && ch == open) /* } */
3755 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
3758 /* Add this character. */
3759 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3762 /* If we just read the ending character, don't bother continuing. */
3766 if (open == '\'') /* '' inside grouping construct */
3768 if MBTEST((flags & P_ALLOWESC) && ch == '\\')
3769 tflags |= LEX_PASSNEXT;
3773 if MBTEST(ch == '\\') /* backslashes */
3774 tflags |= LEX_PASSNEXT;
3776 /* Based on which dolstate is currently in (param, op, or word),
3777 decide what the op is. We're really only concerned if it's % or
3778 #, so we can turn on a flag that says whether or not we should
3779 treat single quotes as special when inside a double-quoted
3780 ${...}. This logic must agree with subst.c:extract_dollar_brace_string
3781 since they share the same defines. */
3782 /* FLAG POSIX INTERP 221 */
3783 if (flags & P_DOLBRACE)
3785 /* ${param%[%]word} */
3786 if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '%' && retind > 1)
3787 dolbrace_state = DOLBRACE_QUOTE;
3788 /* ${param#[#]word} */
3789 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '#' && retind > 1)
3790 dolbrace_state = DOLBRACE_QUOTE;
3791 /* ${param/[/]pat/rep} */
3792 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '/' && retind > 1)
3793 dolbrace_state = DOLBRACE_QUOTE2; /* XXX */
3794 /* ${param^[^]pat} */
3795 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '^' && retind > 1)
3796 dolbrace_state = DOLBRACE_QUOTE;
3797 /* ${param,[,]pat} */
3798 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == ',' && retind > 1)
3799 dolbrace_state = DOLBRACE_QUOTE;
3800 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", ch) != 0)
3801 dolbrace_state = DOLBRACE_OP;
3802 else if MBTEST(dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", ch) == 0)
3803 dolbrace_state = DOLBRACE_WORD;
3806 /* The big hammer. Single quotes aren't special in double quotes. The
3807 problem is that Posix used to say the single quotes are semi-special:
3808 within a double-quoted ${...} construct "an even number of
3809 unescaped double-quotes or single-quotes, if any, shall occur." */
3810 /* This was changed in Austin Group Interp 221 */
3811 if MBTEST(posixly_correct && shell_compatibility_level > 41 && dolbrace_state != DOLBRACE_QUOTE && dolbrace_state != DOLBRACE_QUOTE2 && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'')
3814 /* Could also check open == '`' if we want to parse grouping constructs
3815 inside old-style command substitution. */
3816 if (open != close) /* a grouping construct */
3818 if MBTEST(shellquote (ch))
3820 /* '', ``, or "" inside $(...) or other grouping construct. */
3821 push_delimiter (dstack, ch);
3822 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
3823 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
3825 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
3826 pop_delimiter (dstack);
3827 CHECK_NESTRET_ERROR ();
3829 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0 || dolbrace_state == DOLBRACE_QUOTE || dolbrace_state == DOLBRACE_QUOTE2))
3831 /* Translate $'...' here. */
3833 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
3836 /* If we're parsing a double-quoted brace expansion and we are
3837 not in a place where single quotes are treated specially,
3838 make sure we single-quote the results of the ansi
3839 expansion because quote removal should remove them later */
3840 /* FLAG POSIX INTERP 221 */
3841 if ((shell_compatibility_level > 42) && (rflags & P_DQUOTE) && (dolbrace_state == DOLBRACE_QUOTE2 || dolbrace_state == DOLBRACE_QUOTE) && (flags & P_DOLBRACE))
3843 nestret = sh_single_quote (ttrans);
3845 nestlen = strlen (nestret);
3847 #if 0 /* TAG:bash-5.3 */
3848 /* This single-quotes PARAM in ${PARAM OP WORD} when PARAM
3849 contains a $'...' even when extended_quote is set. */
3850 else if ((rflags & P_DQUOTE) && (dolbrace_state == DOLBRACE_PARAM) && (flags & P_DOLBRACE))
3852 nestret = sh_single_quote (ttrans);
3854 nestlen = strlen (nestret);
3857 else if ((rflags & P_DQUOTE) == 0)
3859 nestret = sh_single_quote (ttrans);
3861 nestlen = strlen (nestret);
3865 /* Should we quote CTLESC here? */
3867 nestlen = ttranslen;
3869 retind -= 2; /* back up before the $' */
3871 #if defined (TRANSLATABLE_STRINGS)
3872 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
3874 /* Locale expand $"..." here. */
3876 ttrans = locale_expand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
3879 /* If we're supposed to single-quote translated strings,
3880 check whether the translated result is different from
3881 the original and single-quote the string if it is. */
3882 if (singlequote_translations &&
3883 ((nestlen - 1) != ttranslen || STREQN (nestret, ttrans, ttranslen) == 0))
3885 if ((rflags & P_DQUOTE) == 0)
3886 nestret = sh_single_quote (ttrans);
3887 else if ((rflags & P_DQUOTE) && (dolbrace_state == DOLBRACE_QUOTE2) && (flags & P_DOLBRACE))
3888 nestret = sh_single_quote (ttrans);
3890 /* single quotes aren't special, use backslash instead */
3891 nestret = sh_backslash_quote_for_double_quotes (ttrans, 0);
3894 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
3896 nestlen = strlen (nestret);
3897 retind -= 2; /* back up before the $" */
3899 #endif /* TRANSLATABLE_STRINGS */
3904 else if ((flags & (P_ARRAYSUB|P_DOLBRACE)) && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3905 goto parse_dollar_word;
3906 #if defined (PROCESS_SUBSTITUTION)
3907 /* XXX - technically this should only be recognized at the start of
3909 else if ((flags & (P_ARRAYSUB|P_DOLBRACE)) && (tflags & LEX_GTLT) && (ch == '(')) /* ) */
3910 goto parse_dollar_word;
3913 /* Parse an old-style command substitution within double quotes as a
3915 /* XXX - sh and ksh93 don't do this - XXX */
3916 else if MBTEST(open == '"' && ch == '`')
3918 nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags);
3920 CHECK_NESTRET_ERROR ();
3925 else if MBTEST(open != '`' && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3926 /* check for $(), $[], or ${} inside quoted string. */
3929 if (open == ch) /* undo previous increment */
3931 if (ch == '(') /* ) */
3932 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
3933 else if (ch == '{') /* } */
3934 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags);
3935 else if (ch == '[') /* ] */
3936 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
3938 CHECK_NESTRET_ERROR ();
3943 #if defined (PROCESS_SUBSTITUTION)
3944 if MBTEST((ch == '<' || ch == '>') && (tflags & LEX_GTLT) == 0)
3947 tflags &= ~LEX_GTLT;
3949 if MBTEST(ch == '$' && (tflags & LEX_WASDOL) == 0)
3950 tflags |= LEX_WASDOL;
3952 tflags &= ~LEX_WASDOL;
3958 /*itrace("parse_matched_pair[%d]: returning %s", line_number, ret);*/
3970 fprintf (stderr, "%d -> ", f);
3974 fprintf (stderr, "LEX_WASDOL%s", f ? "|" : "");
3976 if (f & LEX_CKCOMMENT)
3978 f &= ~LEX_CKCOMMENT;
3979 fprintf (stderr, "LEX_CKCOMMENT%s", f ? "|" : "");
3981 if (f & LEX_INCOMMENT)
3983 f &= ~LEX_INCOMMENT;
3984 fprintf (stderr, "LEX_INCOMMENT%s", f ? "|" : "");
3986 if (f & LEX_PASSNEXT)
3989 fprintf (stderr, "LEX_PASSNEXT%s", f ? "|" : "");
3991 if (f & LEX_RESWDOK)
3994 fprintf (stderr, "LEX_RESWDOK%s", f ? "|" : "");
3999 fprintf (stderr, "LEX_CKCASE%s", f ? "|" : "");
4004 fprintf (stderr, "LEX_CKESAC%s", f ? "|" : "");
4009 fprintf (stderr, "LEX_INCASE%s", f ? "|" : "");
4014 fprintf (stderr, "LEX_CASEWD%s", f ? "|" : "");
4016 if (f & LEX_PATLIST)
4019 fprintf (stderr, "LEX_PATLIST%s", f ? "|" : "");
4021 if (f & LEX_INHEREDOC)
4023 f &= ~LEX_INHEREDOC;
4024 fprintf (stderr, "LEX_INHEREDOC%s", f ? "|" : "");
4026 if (f & LEX_HEREDELIM)
4028 f &= ~LEX_HEREDELIM;
4029 fprintf (stderr, "LEX_HEREDELIM%s", f ? "|" : "");
4031 if (f & LEX_STRIPDOC)
4034 fprintf (stderr, "LEX_WASDOL%s", f ? "|" : "");
4036 if (f & LEX_QUOTEDDOC)
4038 f &= ~LEX_QUOTEDDOC;
4039 fprintf (stderr, "LEX_QUOTEDDOC%s", f ? "|" : "");
4044 fprintf (stderr, "LEX_INWORD%s", f ? "|" : "");
4047 fprintf (stderr, "\n");
4052 /* Parse a $(...) command substitution. This reads input from the current
4055 parse_comsub (qc, open, close, lenp, flags)
4056 int qc; /* `"' if this construct is within double quotes */
4064 sh_parser_state_t ps;
4065 COMMAND *saved_global, *parsed_command;
4067 /* Posix interp 217 says arithmetic expressions have precedence, so
4068 assume $(( introduces arithmetic expansion and parse accordingly. */
4069 if (open == '(') /*)*/
4071 peekc = shell_getc (1);
4072 shell_ungetc (peekc);
4073 if (peekc == '(') /*)*/
4074 return (parse_matched_pair (qc, open, close, lenp, 0));
4077 /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/
4079 /*debug_parser(1);*/
4080 start_lineno = line_number;
4082 save_parser_state (&ps);
4084 pushed_string_list = (STRING_SAVER *)NULL;
4086 /* State flags we don't want to persist into command substitutions. */
4087 parser_state &= ~(PST_REGEXP|PST_EXTPAT|PST_CONDCMD|PST_CONDEXPR|PST_COMPASSIGN);
4088 /* Could do PST_CASESTMT too, but that also affects history. Setting
4089 expecting_in_token below should take care of the parsing requirements.
4090 Unsetting PST_REDIRLIST isn't strictly necessary because of how we set
4091 token_to_read below, but we do it anyway. */
4092 parser_state &= ~(PST_CASEPAT|PST_ALEXPNEXT|PST_SUBSHELL|PST_REDIRLIST);
4093 /* State flags we want to set for this run through the parser. */
4094 parser_state |= PST_CMDSUBST|PST_EOFTOKEN|PST_NOEXPAND;
4096 shell_eof_token = close;
4098 saved_global = global_command; /* might not be necessary */
4099 global_command = (COMMAND *)NULL;
4102 esacs_needed_count = expecting_in_token = 0;
4104 /* We want to expand aliases on this pass if we're in posix mode, since the
4105 standard says you have to take aliases into account when looking for the
4106 terminating right paren. Otherwise, we defer until execution time for
4107 backwards compatibility. */
4109 expand_aliases = posixly_correct != 0;
4111 current_token = '\n'; /* XXX */
4113 token_to_read = DOLPAREN; /* let's trick the parser */
4117 if (need_here_doc > 0)
4119 internal_warning ("command substitution: %d unterminated here-document%s", need_here_doc, (need_here_doc == 1) ? "" : "s");
4120 gather_here_documents (); /* XXX check compatibility level? */
4123 parsed_command = global_command;
4126 /* yyparse() has already called yyerror() */
4127 return (&matched_pair_error);
4130 /* parser_error (start_lineno, _("could not parse command substitution")); */
4131 /* Non-interactive shells exit on parse error in a command substitution. */
4132 if (last_command_exit_value == 0)
4133 last_command_exit_value = EXECUTION_FAILURE;
4134 set_exit_status (last_command_exit_value);
4135 if (interactive_shell == 0)
4136 jump_to_top_level (FORCE_EOF); /* This is like reader_loop() */
4138 jump_to_top_level (DISCARD);
4141 if (current_token != shell_eof_token)
4143 INTERNAL_DEBUG(("current_token (%d) != shell_eof_token (%c)", current_token, shell_eof_token));
4144 token_to_read = current_token;
4145 return (&matched_pair_error);
4148 restore_parser_state (&ps);
4150 tcmd = print_comsub (parsed_command); /* returns static memory */
4151 retlen = strlen (tcmd);
4152 if (tcmd[0] == '(') /* ) need a space to prevent arithmetic expansion */
4154 ret = xmalloc (retlen + 2);
4155 if (tcmd[0] == '(') /* ) */
4158 strcpy (ret + 1, tcmd);
4162 ret[retlen++] = ')';
4165 dispose_command (parsed_command);
4166 global_command = saved_global;
4171 /*itrace("parse_comsub:%d: returning `%s'", line_number, ret);*/
4175 /* Recursively call the parser to parse a $(...) command substitution. This is
4176 called by the word expansion code and so does not have to reset as much
4177 parser state before calling yyparse(). */
4179 xparse_dolparen (base, string, indp, flags)
4185 sh_parser_state_t ps;
4186 sh_input_line_state_t ls;
4187 int orig_ind, nc, sflags, start_lineno;
4188 char *ret, *ep, *ostring;
4190 /*debug_parser(1);*/
4193 start_lineno = line_number;
4197 if (flags & SX_NOALLOC)
4198 return (char *)NULL;
4205 /*itrace("xparse_dolparen: size = %d shell_input_line = `%s' string=`%s'", shell_input_line_size, shell_input_line, string);*/
4207 sflags = SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOFREE;
4208 if (flags & SX_NOLONGJMP)
4209 sflags |= SEVAL_NOLONGJMP;
4211 save_parser_state (&ps);
4212 save_input_line_state (&ls);
4214 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4215 pushed_string_list = (STRING_SAVER *)NULL;
4218 parser_state |= PST_CMDSUBST|PST_EOFTOKEN; /* allow instant ')' */ /*(*/
4219 shell_eof_token = ')';
4220 if (flags & SX_COMPLETE)
4221 parser_state |= PST_NOERROR;
4223 /* Don't expand aliases on this pass at all. Either parse_comsub() does it
4224 at parse time, in which case this string already has aliases expanded,
4225 or command_substitute() does it in the child process executing the
4226 command substitution and we want to defer it completely until then. The
4227 old value will be restored by restore_parser_state(). */
4230 token_to_read = DOLPAREN; /* let's trick the parser */
4232 nc = parse_string (string, "command substitution", sflags, (COMMAND **)NULL, &ep);
4234 /* Should we save and restore the bison/yacc lookahead token (yychar) here?
4235 Or only if it's not YYEMPTY? */
4236 if (current_token == shell_eof_token)
4237 yyclearin; /* might want to clear lookahead token unconditionally */
4240 /* reset_parser() clears shell_input_line and associated variables, including
4241 parser_state, so we want to reset things, then restore what we need. */
4242 restore_input_line_state (&ls);
4243 restore_parser_state (&ps);
4247 /* If parse_string returns < 0, we need to jump to top level with the
4248 negative of the return value. We abandon the rest of this input line
4252 clear_shell_input_line (); /* XXX */
4253 if (bash_input.type != st_string) /* paranoia */
4254 parser_state &= ~(PST_CMDSUBST|PST_EOFTOKEN);
4255 if ((flags & SX_NOLONGJMP) == 0)
4256 jump_to_top_level (-nc); /* XXX */
4259 /* Need to find how many characters parse_string() consumed, update
4260 *indp, if flags != 0, copy the portion of the string parsed into RET
4261 and return it. If flags & 1 (SX_NOALLOC) we can return NULL. */
4268 itrace("xparse_dolparen:%d: ep[-1] != RPAREN (%d), ep = `%s'", line_number, ep[-1], ep);
4271 while (ep > ostring && ep[-1] == '\n') ep--;
4275 *indp = ep - base - 1;
4279 if (base[*indp] != ')')
4280 itrace("xparse_dolparen:%d: base[%d] != RPAREN (%d), base = `%s'", line_number, *indp, base[*indp], base);
4281 if (*indp < orig_ind)
4282 itrace("xparse_dolparen:%d: *indp (%d) < orig_ind (%d), orig_string = `%s'", line_number, *indp, orig_ind, ostring);
4285 if (base[*indp] != ')' && (flags & SX_NOLONGJMP) == 0)
4288 if ((flags & SX_NOERROR) == 0)
4289 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), ')');
4290 jump_to_top_level (DISCARD);
4293 if (flags & SX_NOALLOC)
4294 return (char *)NULL;
4302 ret = substring (ostring, 0, nc - 1);
4307 /* Recursively call the parser to parse the string from a $(...) command
4308 substitution to a COMMAND *. This is called from command_substitute() and
4309 has the same parser state constraints as xparse_dolparen(). */
4311 parse_string_to_command (string, flags)
4315 sh_parser_state_t ps;
4316 sh_input_line_state_t ls;
4323 return (COMMAND *)NULL;
4326 slen = STRLEN (string);
4328 /*itrace("parse_string_to_command: size = %d shell_input_line = `%s' string=`%s'", shell_input_line_size, shell_input_line, string);*/
4330 sflags = SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOFREE;
4331 if (flags & SX_NOLONGJMP)
4332 sflags |= SEVAL_NOLONGJMP;
4334 save_parser_state (&ps);
4335 save_input_line_state (&ls);
4337 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4338 pushed_string_list = (STRING_SAVER *)NULL;
4340 if (flags & SX_COMPLETE)
4341 parser_state |= PST_NOERROR;
4346 nc = parse_string (string, "command substitution", sflags, &cmd, &ep);
4349 /* reset_parser() clears shell_input_line and associated variables, including
4350 parser_state, so we want to reset things, then restore what we need. */
4351 restore_input_line_state (&ls);
4352 restore_parser_state (&ps);
4354 /* If parse_string returns < 0, we need to jump to top level with the
4355 negative of the return value. We abandon the rest of this input line
4359 clear_shell_input_line (); /* XXX */
4360 if ((flags & SX_NOLONGJMP) == 0)
4361 jump_to_top_level (-nc); /* XXX */
4364 /* Need to check how many characters parse_string() consumed, make sure it's
4365 the entire string. */
4368 dispose_command (cmd);
4369 return (COMMAND *)NULL;
4375 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
4376 /* Parse a double-paren construct. It can be either an arithmetic
4377 command, an arithmetic `for' command, or a nested subshell. Returns
4378 the parsed token, -1 on error, or -2 if we didn't do anything and
4379 should just go on. */
4388 #if defined (ARITH_FOR_COMMAND)
4389 if (last_read_token == FOR)
4391 if (word_top < MAX_CASE_NEST)
4393 arith_for_lineno = word_lineno[word_top] = line_number;
4394 cmdtyp = parse_arith_cmd (&wval, 0);
4397 wd = alloc_word_desc ();
4399 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
4400 return (ARITH_FOR_EXPRS);
4403 return -1; /* ERROR */
4407 #if defined (DPAREN_ARITHMETIC)
4408 if (reserved_word_acceptable (last_read_token))
4410 sline = line_number;
4412 cmdtyp = parse_arith_cmd (&wval, 0);
4413 if (cmdtyp == 1) /* arithmetic command */
4415 wd = alloc_word_desc ();
4417 wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_NOTILDE|W_NOPROCSUB;
4418 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
4421 else if (cmdtyp == 0) /* nested subshell */
4423 push_string (wval, 0, (alias_t *)NULL);
4424 pushed_string_list->flags = PSH_DPAREN;
4425 if ((parser_state & PST_CASEPAT) == 0)
4426 parser_state |= PST_SUBSHELL;
4434 return -2; /* XXX */
4437 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
4438 If not, assume it's a nested subshell for backwards compatibility and
4439 return 0. In any case, put the characters we've consumed into a locally-
4440 allocated buffer and make *ep point to that buffer. Return -1 on an
4441 error, for example EOF. */
4443 parse_arith_cmd (ep, adddq)
4447 int exp_lineno, rval, c;
4448 char *ttok, *tokstr;
4451 exp_lineno = line_number;
4452 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
4454 if (ttok == &matched_pair_error)
4456 /* Check that the next character is the closing right paren. If
4457 not, this is a syntax error. ( */
4462 tokstr = (char *)xmalloc (ttoklen + 4);
4464 /* if ADDDQ != 0 then (( ... )) -> "..." */
4465 if (rval == 1 && adddq) /* arith cmd, add double quotes */
4468 strncpy (tokstr + 1, ttok, ttoklen - 1);
4469 tokstr[ttoklen] = '"';
4470 tokstr[ttoklen+1] = '\0';
4472 else if (rval == 1) /* arith cmd, don't add double quotes */
4474 strncpy (tokstr, ttok, ttoklen - 1);
4475 tokstr[ttoklen-1] = '\0';
4477 else /* nested subshell */
4480 strncpy (tokstr + 1, ttok, ttoklen - 1);
4481 tokstr[ttoklen] = ')';
4482 tokstr[ttoklen+1] = c;
4483 tokstr[ttoklen+2] = '\0';
4490 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
4492 #if defined (COND_COMMAND)
4498 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
4499 parser_error (cond_lineno, _("unexpected EOF while looking for `]]'"));
4500 else if (cond_token != COND_ERROR)
4502 if (etext = error_token_from_token (cond_token))
4504 parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext);
4508 parser_error (cond_lineno, _("syntax error in conditional expression"));
4515 return (cond_or ());
4524 if (cond_token == OR_OR)
4527 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
4538 if (cond_token == AND_AND)
4541 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
4547 cond_skip_newlines ()
4549 while ((cond_token = read_token (READ)) == '\n')
4551 if (SHOULD_PROMPT ())
4554 return (cond_token);
4557 #define COND_RETURN_ERROR() \
4558 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
4564 COND_COM *term, *tleft, *tright;
4568 /* Read a token. It can be a left paren, a `!', a unary operator, or a
4569 word that should be the first argument of a binary operator. Start by
4570 skipping newlines, since this is a compound command. */
4571 tok = cond_skip_newlines ();
4572 lineno = line_number;
4573 if (tok == COND_END)
4575 COND_RETURN_ERROR ();
4577 else if (tok == '(')
4579 term = cond_expr ();
4580 if (cond_token != ')')
4583 dispose_cond_node (term); /* ( */
4584 if (etext = error_token_from_token (cond_token))
4586 parser_error (lineno, _("unexpected token `%s', expected `)'"), etext);
4590 parser_error (lineno, _("expected `)'"));
4591 COND_RETURN_ERROR ();
4593 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
4594 (void)cond_skip_newlines ();
4596 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
4599 dispose_word (yylval.word); /* not needed */
4600 term = cond_term ();
4602 term->flags ^= CMD_INVERT_RETURN;
4604 else if (tok == WORD && yylval.word->word[0] == '-' && yylval.word->word[1] && yylval.word->word[2] == 0 && test_unop (yylval.word->word))
4607 tok = read_token (READ);
4610 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4611 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
4616 if (etext = error_token_from_token (tok))
4618 parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext);
4622 parser_error (line_number, _("unexpected argument to conditional unary operator"));
4623 COND_RETURN_ERROR ();
4626 (void)cond_skip_newlines ();
4628 else if (tok == WORD) /* left argument to binary operator */
4631 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4634 /* tok = cond_skip_newlines (); ? */
4635 tok = read_token (READ);
4636 if (tok == WORD && test_binop (yylval.word->word))
4639 if (op->word[0] == '=' && (op->word[1] == '\0' || (op->word[1] == '=' && op->word[2] == '\0')))
4640 parser_state |= PST_EXTPAT;
4641 else if (op->word[0] == '!' && op->word[1] == '=' && op->word[2] == '\0')
4642 parser_state |= PST_EXTPAT;
4644 #if defined (COND_REGEXP)
4645 else if (tok == WORD && STREQ (yylval.word->word, "=~"))
4648 parser_state |= PST_REGEXP;
4651 else if (tok == '<' || tok == '>')
4652 op = make_word_from_token (tok); /* ( */
4653 /* There should be a check before blindly accepting the `)' that we have
4654 seen the opening `('. */
4655 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
4657 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
4658 the test command. Similarly for [[ x && expr ]] or
4659 [[ x || expr ]] or [[ (x) ]]. */
4660 op = make_word ("-n");
4661 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
4667 if (etext = error_token_from_token (tok))
4669 parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext);
4673 parser_error (line_number, _("conditional binary operator expected"));
4674 dispose_cond_node (tleft);
4675 COND_RETURN_ERROR ();
4679 if (parser_state & PST_EXTPAT)
4681 tok = read_token (READ);
4682 if (parser_state & PST_EXTPAT)
4683 extended_glob = global_extglob;
4684 parser_state &= ~(PST_REGEXP|PST_EXTPAT);
4688 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4689 term = make_cond_node (COND_BINARY, op, tleft, tright);
4693 if (etext = error_token_from_token (tok))
4695 parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext);
4699 parser_error (line_number, _("unexpected argument to conditional binary operator"));
4700 dispose_cond_node (tleft);
4702 COND_RETURN_ERROR ();
4705 (void)cond_skip_newlines ();
4710 parser_error (line_number, _("unexpected token `%c' in conditional command"), tok);
4711 else if (etext = error_token_from_token (tok))
4713 parser_error (line_number, _("unexpected token `%s' in conditional command"), etext);
4717 parser_error (line_number, _("unexpected token %d in conditional command"), tok);
4718 COND_RETURN_ERROR ();
4723 /* This is kind of bogus -- we slip a mini recursive-descent parser in
4724 here to handle the conditional statement syntax. */
4726 parse_cond_command ()
4730 global_extglob = extended_glob;
4731 cexp = cond_expr ();
4732 return (make_cond_command (cexp));
4736 #if defined (ARRAY_VARS)
4737 /* When this is called, it's guaranteed that we don't care about anything
4738 in t beyond i. We use a buffer with room for the characters we add just
4739 in case assignment() ends up doing something like parsing a command
4740 substitution that will reallocate atoken. We don't want to write beyond
4741 the end of an allocated buffer. */
4743 token_is_assignment (t, i)
4750 atoken = xmalloc (i + 3);
4751 memcpy (atoken, t, i);
4755 r = assignment (atoken, (parser_state & PST_COMPASSIGN) != 0);
4759 /* XXX - check that r == i to avoid returning false positive for
4760 t containing `=' before t[i]. */
4761 return (r > 0 && r == i);
4764 /* XXX - possible changes here for `+=' */
4766 token_is_ident (t, i)
4775 r = legal_identifier (t);
4782 read_token_word (character)
4785 /* The value for YYLVAL when a WORD is read. */
4786 WORD_DESC *the_word;
4788 /* Index into the token that we are building. */
4791 /* ALL_DIGITS becomes zero when we see a non-digit. */
4792 int all_digit_token;
4794 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
4797 /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
4799 int compound_assignment;
4801 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
4804 /* Non-zero means to ignore the value of the next character, and just
4805 to add it no matter what. */
4806 int pass_next_character;
4808 /* The current delimiting character. */
4810 int result, peek_char;
4811 char *ttok, *ttrans;
4812 int ttoklen, ttranslen;
4815 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
4816 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
4819 all_digit_token = DIGIT (character);
4820 dollar_present = quoted = pass_next_character = compound_assignment = 0;
4824 if (character == EOF)
4827 if (pass_next_character)
4829 pass_next_character = 0;
4830 goto got_escaped_character;
4833 cd = current_delimiter (dstack);
4835 /* Handle backslashes. Quote lots of things when not inside of
4836 double-quotes, quote some things inside of double-quotes. */
4837 if MBTEST(character == '\\')
4839 if (parser_state & PST_NOEXPAND)
4841 pass_next_character++;
4846 peek_char = shell_getc (0);
4848 /* Backslash-newline is ignored in all cases except
4849 when quoted with single quotes. */
4850 if MBTEST(peek_char == '\n')
4853 goto next_character;
4857 shell_ungetc (peek_char);
4859 /* If the next character is to be quoted, note it now. */
4860 if MBTEST(cd == 0 || cd == '`' ||
4861 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
4862 pass_next_character++;
4869 /* Parse a matched pair of quote characters. */
4870 if MBTEST(shellquote (character))
4872 push_delimiter (dstack, character);
4873 ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0);
4874 pop_delimiter (dstack);
4875 if (ttok == &matched_pair_error)
4876 return -1; /* Bail immediately. */
4877 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4878 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4879 token[token_index++] = character;
4880 strcpy (token + token_index, ttok);
4881 token_index += ttoklen;
4882 all_digit_token = 0;
4883 if (character != '`')
4885 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
4887 goto next_character;
4891 /* When parsing a regexp as a single word inside a conditional command,
4892 we need to special-case characters special to both the shell and
4893 regular expressions. Right now, that is only '(' and '|'. */ /*)*/
4894 if MBTEST((parser_state & PST_REGEXP) && (character == '(' || character == '|')) /*)*/
4896 if (character == '|')
4899 push_delimiter (dstack, character);
4900 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4901 pop_delimiter (dstack);
4902 if (ttok == &matched_pair_error)
4903 return -1; /* Bail immediately. */
4904 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4905 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4906 token[token_index++] = character;
4907 strcpy (token + token_index, ttok);
4908 token_index += ttoklen;
4910 dollar_present = all_digit_token = 0;
4911 goto next_character;
4913 #endif /* COND_REGEXP */
4915 #ifdef EXTENDED_GLOB
4916 /* Parse a ksh-style extended pattern matching specification. */
4917 if MBTEST(extended_glob && PATTERN_CHAR (character))
4919 peek_char = shell_getc (1);
4920 if MBTEST(peek_char == '(') /* ) */
4922 push_delimiter (dstack, peek_char);
4923 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4924 pop_delimiter (dstack);
4925 if (ttok == &matched_pair_error)
4926 return -1; /* Bail immediately. */
4927 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,
4929 TOKEN_DEFAULT_GROW_SIZE);
4930 token[token_index++] = character;
4931 token[token_index++] = peek_char;
4932 strcpy (token + token_index, ttok);
4933 token_index += ttoklen;
4935 dollar_present = all_digit_token = 0;
4936 goto next_character;
4939 shell_ungetc (peek_char);
4941 #endif /* EXTENDED_GLOB */
4943 /* If the delimiter character is not single quote, parse some of
4944 the shell expansions that must be read as a single word. */
4945 if MBTEST(shellexp (character))
4947 peek_char = shell_getc (1);
4948 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
4949 if MBTEST(peek_char == '(' ||
4950 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
4952 if (peek_char == '{') /* } */
4953 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE|P_DOLBRACE);
4954 else if (peek_char == '(') /* ) */
4956 /* XXX - push and pop the `(' as a delimiter for use by
4957 the command-oriented-history code. This way newlines
4958 appearing in the $(...) string get added to the
4959 history literally rather than causing a possibly-
4960 incorrect `;' to be added. ) */
4961 push_delimiter (dstack, peek_char);
4962 ttok = parse_comsub (cd, '(', ')', &ttoklen, P_COMMAND);
4963 pop_delimiter (dstack);
4966 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
4967 if (ttok == &matched_pair_error)
4968 return -1; /* Bail immediately. */
4969 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,
4971 TOKEN_DEFAULT_GROW_SIZE);
4972 token[token_index++] = character;
4973 token[token_index++] = peek_char;
4974 strcpy (token + token_index, ttok);
4975 token_index += ttoklen;
4978 all_digit_token = 0;
4979 goto next_character;
4981 /* This handles $'...' and $"..." new-style quoted strings. */
4982 #if defined (TRANSLATABLE_STRINGS)
4983 else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
4985 else if MBTEST(character == '$' && peek_char == '\'')
4990 first_line = line_number;
4991 push_delimiter (dstack, peek_char);
4992 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
4994 (peek_char == '\'') ? P_ALLOWESC : 0);
4995 pop_delimiter (dstack);
4996 if (ttok == &matched_pair_error)
4998 if (peek_char == '\'')
5001 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
5004 /* Insert the single quotes and correctly quote any
5005 embedded single quotes (allowed because P_ALLOWESC was
5006 passed to parse_matched_pair). */
5007 ttok = sh_single_quote (ttrans);
5009 ttranslen = strlen (ttok);
5012 #if defined (TRANSLATABLE_STRINGS)
5016 /* Try to locale-expand the converted string. */
5017 ttrans = locale_expand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
5020 /* Add the double quotes back (or single quotes if the user
5021 has set that option). */
5022 if (singlequote_translations &&
5023 ((ttoklen - 1) != ttranslen || STREQN (ttok, ttrans, ttranslen) == 0))
5024 ttok = sh_single_quote (ttrans);
5026 ttok = sh_mkdoublequoted (ttrans, ttranslen, 0);
5030 ttranslen = strlen (ttrans);
5032 #endif /* TRANSLATABLE_STRINGS */
5034 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 1,
5036 TOKEN_DEFAULT_GROW_SIZE);
5037 strcpy (token + token_index, ttrans);
5038 token_index += ttranslen;
5041 all_digit_token = 0;
5042 goto next_character;
5044 /* This could eventually be extended to recognize all of the
5045 shell's single-character parameter expansions, and set flags.*/
5046 else if MBTEST(character == '$' && peek_char == '$')
5048 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
5050 TOKEN_DEFAULT_GROW_SIZE);
5051 token[token_index++] = '$';
5052 token[token_index++] = peek_char;
5054 all_digit_token = 0;
5055 goto next_character;
5058 shell_ungetc (peek_char);
5061 #if defined (ARRAY_VARS)
5062 /* Identify possible array subscript assignment; match [...]. If
5063 parser_state&PST_COMPASSIGN, we need to parse [sub]=words treating
5064 `sub' as if it were enclosed in double quotes. */
5065 else if MBTEST(character == '[' && /* ] */
5066 ((token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) ||
5067 (token_index == 0 && (parser_state&PST_COMPASSIGN))))
5069 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, P_ARRAYSUB);
5070 if (ttok == &matched_pair_error)
5071 return -1; /* Bail immediately. */
5072 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
5074 TOKEN_DEFAULT_GROW_SIZE);
5075 token[token_index++] = character;
5076 strcpy (token + token_index, ttok);
5077 token_index += ttoklen;
5079 all_digit_token = 0;
5080 goto next_character;
5082 /* Identify possible compound array variable assignment. */
5083 else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index))
5085 peek_char = shell_getc (1);
5086 if MBTEST(peek_char == '(') /* ) */
5088 ttok = parse_compound_assignment (&ttoklen);
5090 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
5092 TOKEN_DEFAULT_GROW_SIZE);
5094 token[token_index++] = '=';
5095 token[token_index++] = '(';
5098 strcpy (token + token_index, ttok);
5099 token_index += ttoklen;
5101 token[token_index++] = ')';
5103 all_digit_token = 0;
5104 compound_assignment = 1;
5106 goto next_character;
5108 goto got_token; /* ksh93 seems to do this */
5112 shell_ungetc (peek_char);
5116 /* When not parsing a multi-character word construct, shell meta-
5117 characters break words. */
5118 if MBTEST(shellbreak (character))
5120 shell_ungetc (character);
5125 if MBTEST(character == CTLESC || character == CTLNUL)
5127 RESIZE_MALLOCED_BUFFER (token, token_index, 2, token_buffer_size,
5128 TOKEN_DEFAULT_GROW_SIZE);
5129 token[token_index++] = CTLESC;
5132 got_escaped_character:
5133 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
5134 TOKEN_DEFAULT_GROW_SIZE);
5136 token[token_index++] = character;
5138 all_digit_token &= DIGIT (character);
5139 dollar_present |= character == '$';
5142 if (character == '\n' && SHOULD_PROMPT ())
5145 /* We want to remove quoted newlines (that is, a \<newline> pair)
5146 unless we are within single quotes or pass_next_character is
5147 set (the shell equivalent of literal-next). */
5148 cd = current_delimiter (dstack);
5149 character = shell_getc (cd != '\'' && pass_next_character == 0);
5150 } /* end for (;;) */
5154 /* Calls to RESIZE_MALLOCED_BUFFER ensure there is sufficient room. */
5155 token[token_index] = '\0';
5157 /* Check to see what thing we should return. If the last_read_token
5158 is a `<', or a `&', or the character which ended this token is
5159 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
5160 Otherwise, it is just a word, and should be returned as such. */
5161 if MBTEST(all_digit_token && (character == '<' || character == '>' ||
5162 last_read_token == LESS_AND ||
5163 last_read_token == GREATER_AND))
5165 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
5167 yylval.number = lvalue;
5172 /* Check for special case tokens. */
5173 result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
5178 /* Posix.2 does not allow reserved words to be aliased, so check for all
5179 of them, including special cases, before expanding the current token
5181 if MBTEST(posixly_correct)
5182 CHECK_FOR_RESERVED_WORD (token);
5184 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
5185 inhibits alias expansion. */
5186 if (expand_aliases && quoted == 0)
5188 result = alias_expand_token (token);
5189 if (result == RE_READ_TOKEN)
5190 return (RE_READ_TOKEN);
5191 else if (result == NO_EXPANSION)
5192 parser_state &= ~PST_ALEXPNEXT;
5195 /* If not in Posix.2 mode, check for reserved words after alias
5197 if MBTEST(posixly_correct == 0)
5199 CHECK_FOR_RESERVED_WORD (token);
5201 the_word = alloc_word_desc ();
5202 the_word->word = (char *)xmalloc (1 + token_index);
5203 the_word->flags = 0;
5204 strcpy (the_word->word, token);
5206 the_word->flags |= W_HASDOLLAR;
5208 the_word->flags |= W_QUOTED; /*(*/
5209 if (compound_assignment && token[token_index-1] == ')')
5210 the_word->flags |= W_COMPASSIGN;
5211 /* A word is an assignment if it appears at the beginning of a
5212 simple command, or after another assignment word. This is
5213 context-dependent, so it cannot be handled in the grammar. */
5214 if (assignment (token, (parser_state & PST_COMPASSIGN) != 0))
5216 the_word->flags |= W_ASSIGNMENT;
5217 /* Don't perform word splitting on assignment statements. */
5218 if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0)
5220 the_word->flags |= W_NOSPLIT;
5221 if (parser_state & PST_COMPASSIGN)
5222 the_word->flags |= W_NOGLOB; /* XXX - W_NOBRACE? */
5226 if (command_token_position (last_read_token))
5229 b = builtin_address_internal (token, 0);
5230 if (b && (b->flags & ASSIGNMENT_BUILTIN))
5231 parser_state |= PST_ASSIGNOK;
5232 else if (STREQ (token, "eval") || STREQ (token, "let"))
5233 parser_state |= PST_ASSIGNOK;
5236 yylval.word = the_word;
5238 /* should we check that quoted == 0 as well? */
5239 if MBTEST(token[0] == '{' && token[token_index-1] == '}' &&
5240 (character == '<' || character == '>'))
5242 /* can use token; already copied to the_word */
5243 token[token_index-1] = '\0';
5244 #if defined (ARRAY_VARS)
5245 if (legal_identifier (token+1) || valid_array_reference (token+1, 0))
5247 if (legal_identifier (token+1))
5250 strcpy (the_word->word, token+1);
5251 /* itrace("read_token_word: returning REDIR_WORD for %s", the_word->word); */
5252 yylval.word = the_word; /* accommodate recursive call */
5253 return (REDIR_WORD);
5256 /* valid_array_reference can call the parser recursively; need to
5257 make sure that yylval.word doesn't change if we are going to
5258 return WORD or ASSIGNMENT_WORD */
5259 yylval.word = the_word;
5262 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
5263 ? ASSIGNMENT_WORD : WORD;
5265 switch (last_read_token)
5268 parser_state |= PST_ALLOWOPNBRC;
5269 function_dstart = line_number;
5274 if (word_top < MAX_CASE_NEST)
5276 word_lineno[word_top] = line_number;
5277 expecting_in_token++;
5284 /* Return 1 if TOKSYM is a token that after being read would allow
5285 a reserved word to be seen, else 0. */
5287 reserved_word_acceptable (toksym)
5327 #if defined (COPROCESS_SUPPORT)
5328 if (last_read_token == WORD && token_before_that == COPROC)
5331 if (last_read_token == WORD && token_before_that == FUNCTION)
5337 /* Return the index of TOKEN in the alist of reserved words, or -1 if
5338 TOKEN is not a shell reserved word. */
5340 find_reserved_word (tokstr)
5344 for (i = 0; word_token_alist[i].word; i++)
5345 if (STREQ (tokstr, word_token_alist[i].word))
5350 /* An interface to let the rest of the shell (primarily the completion
5351 system) know what the parser is expecting. */
5353 parser_in_command_position ()
5355 return (command_token_position (last_read_token));
5359 #if defined (READLINE)
5360 /* Called after each time readline is called. This insures that whatever
5361 the new prompt string is gets propagated to readline's local prompt
5364 reset_readline_prompt ()
5368 if (prompt_string_pointer)
5370 temp_prompt = (*prompt_string_pointer)
5371 ? decode_prompt_string (*prompt_string_pointer)
5374 if (temp_prompt == 0)
5376 temp_prompt = (char *)xmalloc (1);
5377 temp_prompt[0] = '\0';
5380 FREE (current_readline_prompt);
5381 current_readline_prompt = temp_prompt;
5384 #endif /* READLINE */
5387 #if defined (HISTORY)
5388 /* A list of tokens which can be followed by newlines, but not by
5389 semi-colons. When concatenating multiple lines of history, the
5390 newline separator for such tokens is replaced with a space. */
5391 static const int no_semi_successors[] = {
5392 '\n', '{', '(', ')', ';', '&', '|',
5393 CASE, DO, ELSE, IF, SEMI_SEMI, SEMI_AND, SEMI_SEMI_AND, THEN, UNTIL,
5394 WHILE, AND_AND, OR_OR, IN,
5398 /* If we are not within a delimited expression, try to be smart
5399 about which separators can be semi-colons and which must be
5400 newlines. Returns the string that should be added into the
5401 history entry. LINE is the line we're about to add; it helps
5402 make some more intelligent decisions in certain cases. */
5404 history_delimiting_chars (line)
5407 static int last_was_heredoc = 0; /* was the last entry the start of a here document? */
5410 if ((parser_state & PST_HEREDOC) == 0)
5411 last_was_heredoc = 0;
5413 if (dstack.delimiter_depth != 0)
5416 /* We look for current_command_line_count == 2 because we are looking to
5417 add the first line of the body of the here document (the second line
5418 of the command). We also keep LAST_WAS_HEREDOC as a private sentinel
5419 variable to note when we think we added the first line of a here doc
5420 (the one with a "<<" somewhere in it) */
5421 if (parser_state & PST_HEREDOC)
5423 if (last_was_heredoc)
5425 last_was_heredoc = 0;
5428 return (here_doc_first_line ? "\n" : "");
5431 if (parser_state & PST_COMPASSIGN)
5434 /* First, handle some special cases. */
5436 /* If we just read `()', assume it's a function definition, and don't
5437 add a semicolon. If the token before the `)' was not `(', and we're
5438 not in the midst of parsing a case statement, assume it's a
5439 parenthesized command and add the semicolon. */
5441 if (token_before_that == ')')
5443 if (two_tokens_ago == '(') /*)*/ /* function def */
5445 /* This does not work for subshells inside case statement
5446 command lists. It's a suboptimal solution. */
5447 else if (parser_state & PST_CASESTMT) /* case statement pattern */
5450 return "; "; /* (...) subshell */
5452 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
5453 return " "; /* function def using `function name' without `()' */
5455 /* If we're not in a here document, but we think we're about to parse one,
5456 and we would otherwise return a `;', return a newline to delimit the
5457 line with the here-doc delimiter */
5458 else if ((parser_state & PST_HEREDOC) == 0 && current_command_line_count > 1 && last_read_token == '\n' && strstr (line, "<<"))
5460 last_was_heredoc = 1;
5463 else if ((parser_state & PST_HEREDOC) == 0 && current_command_line_count > 1 && need_here_doc > 0)
5465 else if (token_before_that == WORD && two_tokens_ago == FOR)
5467 /* Tricky. `for i\nin ...' should not have a semicolon, but
5468 `for i\ndo ...' should. We do what we can. */
5469 for (i = shell_input_line_index; whitespace (shell_input_line[i]); i++)
5471 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
5475 else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT))
5478 for (i = 0; no_semi_successors[i]; i++)
5480 if (token_before_that == no_semi_successors[i])
5484 /* Assume that by this point we are reading lines in a multi-line command.
5485 If we have multiple consecutive blank lines we want to return only one
5487 if (line_isblank (line))
5488 return (current_command_line_count > 1 && last_read_token == '\n' && token_before_that != '\n') ? "; " : "";
5492 #endif /* HISTORY */
5494 /* Issue a prompt, or prepare to issue a prompt when the next character
5497 prompt_again (force)
5502 if (interactive == 0 || expanding_alias ()) /* XXX */
5505 ps1_prompt = get_string_value ("PS1");
5506 ps2_prompt = get_string_value ("PS2");
5508 ps0_prompt = get_string_value ("PS0");
5510 if (!prompt_string_pointer)
5511 prompt_string_pointer = &ps1_prompt;
5513 temp_prompt = *prompt_string_pointer
5514 ? decode_prompt_string (*prompt_string_pointer)
5517 if (temp_prompt == 0)
5519 temp_prompt = (char *)xmalloc (1);
5520 temp_prompt[0] = '\0';
5523 current_prompt_string = *prompt_string_pointer;
5524 prompt_string_pointer = &ps2_prompt;
5526 #if defined (READLINE)
5527 if (!no_line_editing)
5529 FREE (current_readline_prompt);
5530 current_readline_prompt = temp_prompt;
5533 #endif /* READLINE */
5535 FREE (current_decoded_prompt);
5536 current_decoded_prompt = temp_prompt;
5541 get_current_prompt_level ()
5543 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
5547 set_current_prompt_level (x)
5550 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
5551 current_prompt_string = *prompt_string_pointer;
5557 fprintf (stderr, "%s", current_decoded_prompt);
5561 #if defined (HISTORY)
5562 /* The history library increments the history offset as soon as it stores
5563 the first line of a potentially multi-line command, so we compensate
5564 here by returning one fewer when appropriate. */
5566 prompt_history_number (pmt)
5571 ret = history_number ();
5575 if (pmt == ps1_prompt) /* are we expanding $PS1? */
5577 else if (pmt == ps2_prompt && command_oriented_history == 0)
5578 return ret; /* not command oriented history */
5579 else if (pmt == ps2_prompt && command_oriented_history && current_command_first_line_saved)
5582 return ret - 1; /* PS0, PS4, ${var@P}, PS2 other cases */
5586 /* Return a string which will be printed as a prompt. The string
5587 may contain special characters which are decoded as follows:
5590 \d the date in Day Mon Date format
5591 \e escape (ascii 033)
5592 \h the hostname up to the first `.'
5594 \j the number of active jobs
5595 \l the basename of the shell's tty device name
5598 \s the name of the shell
5599 \t the time in 24-hour hh:mm:ss format
5600 \T the time in 12-hour hh:mm:ss format
5601 \@ the time in 12-hour hh:mm am/pm format
5602 \A the time in 24-hour hh:mm format
5603 \D{fmt} the result of passing FMT to strftime(3)
5605 \v the version of bash (e.g., 2.00)
5606 \V the release of bash, version + patchlevel (e.g., 2.00.0)
5607 \w the current working directory
5608 \W the last element of $PWD
5609 \! the history number of this command
5610 \# the command number of this command
5611 \$ a $ or a # if you are root
5612 \nnn character code nnn in octal
5614 \[ begin a sequence of non-printing chars
5615 \] end a sequence of non-printing chars
5617 #define PROMPT_GROWTH 48
5619 decode_prompt_string (string)
5623 char *result, *t, *orig_string;
5624 struct dstack save_dstack;
5625 int last_exit_value, last_comsub_pid;
5626 #if defined (PROMPT_STRING_DECODE)
5628 size_t result_index;
5630 char *temp, *t_host, octal_string[4];
5636 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
5637 result[result_index = 0] = 0;
5638 temp = (char *)NULL;
5639 orig_string = string;
5641 while (c = *string++)
5643 if (posixly_correct && c == '!')
5647 temp = savestring ("!");
5652 #if !defined (HISTORY)
5653 temp = savestring ("1");
5655 temp = itos (prompt_history_number (orig_string));
5656 #endif /* HISTORY */
5657 string--; /* add_string increments string again. */
5675 strncpy (octal_string, string, 3);
5676 octal_string[3] = '\0';
5678 n = read_octal (octal_string);
5679 temp = (char *)xmalloc (3);
5681 if (n == CTLESC || n == CTLNUL)
5698 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
5701 c = 0; /* tested at add_string: */
5709 /* Make the current time/date into a string. */
5710 (void) time (&the_time);
5711 #if defined (HAVE_TZSET)
5712 sv_tz ("TZ"); /* XXX -- just make sure */
5714 tm = localtime (&the_time);
5717 n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm);
5719 n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm);
5721 n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm);
5723 n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm);
5725 n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm);
5730 timebuf[sizeof(timebuf) - 1] = '\0';
5732 temp = savestring (timebuf);
5735 case 'D': /* strftime format */
5736 if (string[1] != '{') /* } */
5739 (void) time (&the_time);
5740 tm = localtime (&the_time);
5741 string += 2; /* skip { */
5742 timefmt = xmalloc (strlen (string) + 3);
5743 for (t = timefmt; *string && *string != '}'; )
5746 c = *string; /* tested at add_string */
5747 if (timefmt[0] == '\0')
5750 timefmt[1] = 'X'; /* locale-specific current time */
5753 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
5759 timebuf[sizeof(timebuf) - 1] = '\0';
5761 if (promptvars || posixly_correct)
5762 /* Make sure that expand_prompt_string is called with a
5763 second argument of Q_DOUBLE_QUOTES if we use this
5765 temp = sh_backslash_quote_for_double_quotes (timebuf, 0);
5767 temp = savestring (timebuf);
5771 temp = (char *)xmalloc (3);
5772 temp[0] = no_line_editing ? '\n' : '\r';
5773 temp[1] = no_line_editing ? '\0' : '\n';
5778 temp = base_pathname (shell_name);
5779 /* Try to quote anything the user can set in the file system */
5780 if (promptvars || posixly_correct)
5783 t = sh_strvis (temp);
5784 temp = sh_backslash_quote_for_double_quotes (t, 0);
5788 temp = sh_strvis (temp);
5793 temp = (char *)xmalloc (16);
5795 strcpy (temp, dist_version);
5797 sprintf (temp, "%s.%d", dist_version, patch_level);
5803 /* Use the value of PWD because it is much more efficient. */
5804 char t_string[PATH_MAX];
5807 temp = get_string_value ("PWD");
5811 if (getcwd (t_string, sizeof(t_string)) == 0)
5817 tlen = strlen (t_string);
5821 tlen = sizeof (t_string) - 1;
5822 strncpy (t_string, temp, tlen);
5824 t_string[tlen] = '\0';
5826 #if defined (MACOSX)
5827 /* Convert from "fs" format to "input" format */
5828 temp = fnx_fromfs (t_string, strlen (t_string));
5829 if (temp != t_string)
5830 strcpy (t_string, temp);
5833 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
5834 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
5835 /* Abbreviate \W as ~ if $PWD == $HOME */
5836 if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0))
5838 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
5840 t = strrchr (t_string, '/');
5842 memmove (t_string, t + 1, strlen (t)); /* strlen(t) to copy NULL */
5846 #undef DOUBLE_SLASH_ROOT
5849 /* polite_directory_format is guaranteed to return a string
5850 no longer than PATH_MAX - 1 characters. */
5851 temp = polite_directory_format (t_string);
5852 if (temp != t_string)
5853 strcpy (t_string, temp);
5856 temp = trim_pathname (t_string, PATH_MAX - 1);
5857 /* If we're going to be expanding the prompt string later,
5858 quote the directory name. */
5859 if (promptvars || posixly_correct)
5860 /* Make sure that expand_prompt_string is called with a
5861 second argument of Q_DOUBLE_QUOTES if we use this
5865 t = sh_strvis (t_string);
5866 temp = sh_backslash_quote_for_double_quotes (t, 0);
5870 temp = sh_strvis (t_string);
5876 if (current_user.user_name == 0)
5877 get_current_user_info ();
5878 temp = savestring (current_user.user_name);
5883 t_host = savestring (current_host_name);
5884 if (c == 'h' && (t = (char *)strchr (t_host, '.')))
5886 if (promptvars || posixly_correct)
5887 /* Make sure that expand_prompt_string is called with a
5888 second argument of Q_DOUBLE_QUOTES if we use this
5890 temp = sh_backslash_quote_for_double_quotes (t_host, 0);
5892 temp = savestring (t_host);
5897 n = current_command_number;
5898 /* If we have already incremented current_command_number (PS4,
5899 ${var@P}), compensate */
5900 if (orig_string != ps0_prompt && orig_string != ps1_prompt && orig_string != ps2_prompt)
5906 #if !defined (HISTORY)
5907 temp = savestring ("1");
5909 temp = itos (prompt_history_number (orig_string));
5910 #endif /* HISTORY */
5914 t = temp = (char *)xmalloc (3);
5915 if ((promptvars || posixly_correct) && (current_user.euid != 0))
5917 *t++ = current_user.euid == 0 ? '#' : '$';
5922 temp = itos (count_all_jobs ());
5926 #if defined (HAVE_TTYNAME)
5927 temp = (char *)ttyname (fileno (stdin));
5928 t = temp ? base_pathname (temp) : "tty";
5929 temp = savestring (t);
5931 temp = savestring ("tty");
5932 #endif /* !HAVE_TTYNAME */
5935 #if defined (READLINE)
5938 if (no_line_editing)
5943 temp = (char *)xmalloc (3);
5944 n = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
5946 if (n == CTLESC || n == CTLNUL)
5951 #endif /* READLINE */
5957 temp = (char *)xmalloc (2);
5964 else /* (c == '\\') */
5971 temp = (char *)xmalloc (3);
5980 sub_append_string (temp, result, &result_index, &result_size);
5981 temp = (char *)NULL; /* Freed in sub_append_string (). */
5982 result[result_index] = '\0';
5988 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
5989 /* dequote_string should take care of removing this if we are not
5990 performing the rest of the word expansions. */
5991 if (c == CTLESC || c == CTLNUL)
5992 result[result_index++] = CTLESC;
5993 result[result_index++] = c;
5994 result[result_index] = '\0';
5997 #else /* !PROMPT_STRING_DECODE */
5998 result = savestring (string);
5999 #endif /* !PROMPT_STRING_DECODE */
6001 /* Save the delimiter stack and point `dstack' to temp space so any
6002 command substitutions in the prompt string won't result in screwing
6003 up the parser's quoting state. */
6004 save_dstack = dstack;
6005 dstack = temp_dstack;
6006 dstack.delimiter_depth = 0;
6008 /* Perform variable and parameter expansion and command substitution on
6009 the prompt string. */
6010 if (promptvars || posixly_correct)
6012 last_exit_value = last_command_exit_value;
6013 last_comsub_pid = last_command_subst_pid;
6014 list = expand_prompt_string (result, Q_DOUBLE_QUOTES, 0);
6016 result = string_list (list);
6017 dispose_words (list);
6018 last_command_exit_value = last_exit_value;
6019 last_command_subst_pid = last_comsub_pid;
6023 t = dequote_string (result);
6028 dstack = save_dstack;
6033 /************************************************
6037 ************************************************/
6039 /* Report a syntax error, and restart the parser. Call here for fatal
6045 if ((parser_state & PST_NOERROR) == 0)
6046 report_syntax_error ((char *)NULL);
6052 error_token_from_token (tok)
6057 if (t = find_token_in_alist (tok, word_token_alist, 0))
6060 if (t = find_token_in_alist (tok, other_token_alist, 0))
6064 /* This stuff is dicy and needs closer inspection */
6065 switch (current_token)
6068 case ASSIGNMENT_WORD:
6070 t = savestring (yylval.word->word);
6073 t = itos (yylval.number);
6076 if (yylval.word_list)
6077 t = string_list (yylval.word_list);
6079 case ARITH_FOR_EXPRS:
6080 if (yylval.word_list)
6081 t = string_list_internal (yylval.word_list, " ; ");
6084 t = (char *)NULL; /* punt */
6092 error_token_from_text ()
6097 t = shell_input_line;
6098 i = shell_input_line_index;
6102 if (i && t[i] == '\0')
6105 while (i && (whitespace (t[i]) || t[i] == '\n'))
6111 while (i && (member (t[i], " \n\t;|&") == 0))
6114 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
6117 /* Return our idea of the offending token. */
6118 if (token_end || (i == 0 && token_end == 0))
6121 msg = substring (t, i, token_end);
6122 else /* one-character token */
6124 msg = (char *)xmalloc (2);
6134 print_offending_line ()
6139 msg = savestring (shell_input_line);
6140 token_end = strlen (msg);
6141 while (token_end && msg[token_end - 1] == '\n')
6142 msg[--token_end] = '\0';
6144 parser_error (line_number, "`%s'", msg);
6148 /* Report a syntax error with line numbers, etc.
6149 Call here for recoverable errors. If you have a message to print,
6150 then place it in MESSAGE, otherwise pass NULL and this will figure
6151 out an appropriate message for you. */
6153 report_syntax_error (message)
6160 parser_error (line_number, "%s", message);
6161 if (interactive && EOF_Reached)
6163 last_command_exit_value = (executing_builtin && parse_and_execute_level) ? EX_BADSYNTAX : EX_BADUSAGE;
6164 set_pipestatus_from_exit (last_command_exit_value);
6168 /* If the line of input we're reading is not null, try to find the
6169 objectionable token. First, try to figure out what token the
6170 parser's complaining about by looking at current_token. */
6171 if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
6173 if (ansic_shouldquote (msg))
6175 p = ansic_quote (msg, 0, NULL);
6179 parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
6182 if (interactive == 0)
6183 print_offending_line ();
6185 last_command_exit_value = (executing_builtin && parse_and_execute_level) ? EX_BADSYNTAX : EX_BADUSAGE;
6186 set_pipestatus_from_exit (last_command_exit_value);
6190 /* If looking at the current token doesn't prove fruitful, try to find the
6191 offending token by analyzing the text of the input line near the current
6192 input line index and report what we find. */
6193 if (shell_input_line && *shell_input_line)
6195 msg = error_token_from_text ();
6198 parser_error (line_number, _("syntax error near `%s'"), msg);
6202 /* If not interactive, print the line containing the error. */
6203 if (interactive == 0)
6204 print_offending_line ();
6208 if (EOF_Reached && shell_eof_token && current_token != shell_eof_token)
6209 parser_error (line_number, _("unexpected EOF while looking for matching `%c'"), shell_eof_token);
6212 msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error");
6213 parser_error (line_number, "%s", msg);
6216 /* When the shell is interactive, this file uses EOF_Reached
6217 only for error reporting. Other mechanisms are used to
6218 decide whether or not to exit. */
6219 if (interactive && EOF_Reached)
6223 last_command_exit_value = (executing_builtin && parse_and_execute_level) ? EX_BADSYNTAX : EX_BADUSAGE;
6224 set_pipestatus_from_exit (last_command_exit_value);
6227 /* ??? Needed function. ??? We have to be able to discard the constructs
6228 created during parsing. In the case of error, we want to return
6229 allocated objects to the memory pool. In the case of no error, we want
6230 to throw away the information about where the allocated objects live.
6231 (dispose_command () will actually free the command.) */
6233 discard_parser_constructs (error_p)
6238 /************************************************
6242 ************************************************/
6244 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
6246 /* A flag denoting whether or not ignoreeof is set. */
6249 /* The number of times that we have encountered an EOF character without
6250 another character intervening. When this gets above the limit, the
6251 shell terminates. */
6252 int eof_encountered = 0;
6254 /* The limit for eof_encountered. */
6255 int eof_encountered_limit = 10;
6257 /* If we have EOF as the only input unit, this user wants to leave
6258 the shell. If the shell is not interactive, then just leave.
6259 Otherwise, if ignoreeof is set, and we haven't done this the
6260 required number of times in a row, print a message. */
6262 handle_eof_input_unit ()
6266 /* shell.c may use this to decide whether or not to write out the
6267 history, among other things. We use it only for error reporting
6272 /* If the user wants to "ignore" eof, then let her do so, kind of. */
6275 if (eof_encountered < eof_encountered_limit)
6277 fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
6278 login_shell ? "logout" : "exit");
6280 /* Reset the parsing state. */
6281 last_read_token = current_token = '\n';
6282 /* Reset the prompt string to be $PS1. */
6283 prompt_string_pointer = (char **)NULL;
6289 /* In this case EOF should exit the shell. Do it now. */
6292 last_shell_builtin = this_shell_builtin;
6293 this_shell_builtin = exit_builtin;
6294 exit_builtin ((WORD_LIST *)NULL);
6298 /* We don't write history files, etc., for non-interactive shells. */
6303 /************************************************
6305 * STRING PARSING FUNCTIONS *
6307 ************************************************/
6309 /* It's very important that these two functions treat the characters
6310 between ( and ) identically. */
6312 static WORD_LIST parse_string_error;
6314 /* Take a string and run it through the shell parser, returning the
6315 resultant word list. Used by compound array assignment. */
6317 parse_string_to_word_list (s, flags, whom)
6323 int tok, orig_current_token, orig_line_number;
6324 int orig_parser_state;
6325 sh_parser_state_t ps;
6328 orig_line_number = line_number;
6329 save_parser_state (&ps);
6331 #if defined (HISTORY)
6332 bash_history_disable ();
6336 if (ea = expanding_alias ())
6337 parser_save_alias ();
6339 /* WORD to avoid parsing reserved words as themselves and just parse them as
6341 last_read_token = WORD;
6343 current_command_line_count = 0;
6344 echo_input_at_read = expand_aliases = 0;
6346 with_input_from_string (s, whom);
6347 wl = (WORD_LIST *)NULL;
6351 orig_parser_state = parser_state; /* XXX - not needed? */
6352 /* State flags we don't want to persist into compound assignments. */
6353 parser_state &= ~PST_NOEXPAND; /* parse_comsub sentinel */
6354 /* State flags we want to set for this run through the tokenizer. */
6355 parser_state |= PST_COMPASSIGN|PST_REPARSE;
6358 while ((tok = read_token (READ)) != yacc_EOF)
6360 if (tok == '\n' && *bash_input.location.string == '\0')
6362 if (tok == '\n') /* Allow newlines in compound assignments */
6364 if (tok != WORD && tok != ASSIGNMENT_WORD)
6366 line_number = orig_line_number + line_number - 1;
6367 orig_current_token = current_token;
6368 current_token = tok;
6369 yyerror (NULL); /* does the right thing */
6370 current_token = orig_current_token;
6373 wl = &parse_string_error;
6376 wl = make_word_list (yylval.word, wl);
6379 last_read_token = '\n';
6383 parser_restore_alias ();
6385 restore_parser_state (&ps);
6388 parser_state = orig_parser_state; /* XXX - not needed? */
6390 if (wl == &parse_string_error)
6392 set_exit_status (EXECUTION_FAILURE);
6393 if (interactive_shell == 0 && posixly_correct)
6394 jump_to_top_level (FORCE_EOF);
6396 jump_to_top_level (DISCARD);
6399 return (REVERSE_LIST (wl, WORD_LIST *));
6403 parse_compound_assignment (retlenp)
6407 int tok, orig_line_number, assignok;
6408 sh_parser_state_t ps;
6411 orig_line_number = line_number;
6412 save_parser_state (&ps);
6414 /* WORD to avoid parsing reserved words as themselves and just parse them as
6415 WORDs. Plus it means we won't be in a command position and so alias
6416 expansion won't happen. */
6417 last_read_token = WORD;
6419 token = (char *)NULL;
6420 token_buffer_size = 0;
6421 wl = (WORD_LIST *)NULL; /* ( */
6423 assignok = parser_state&PST_ASSIGNOK; /* XXX */
6425 /* State flags we don't want to persist into compound assignments. */
6426 parser_state &= ~(PST_NOEXPAND|PST_CONDCMD|PST_CONDEXPR|PST_REGEXP|PST_EXTPAT);
6427 /* State flags we want to set for this run through the tokenizer. */
6428 parser_state |= PST_COMPASSIGN;
6430 esacs_needed_count = expecting_in_token = 0;
6432 while ((tok = read_token (READ)) != ')')
6434 if (tok == '\n') /* Allow newlines in compound assignments */
6436 if (SHOULD_PROMPT ())
6440 if (tok != WORD && tok != ASSIGNMENT_WORD)
6442 current_token = tok; /* for error reporting */
6443 if (tok == yacc_EOF) /* ( */
6444 parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'"));
6446 yyerror(NULL); /* does the right thing */
6449 wl = &parse_string_error;
6452 wl = make_word_list (yylval.word, wl);
6455 restore_parser_state (&ps);
6457 if (wl == &parse_string_error)
6459 set_exit_status (EXECUTION_FAILURE);
6460 last_read_token = '\n'; /* XXX */
6461 if (interactive_shell == 0 && posixly_correct)
6462 jump_to_top_level (FORCE_EOF);
6464 jump_to_top_level (DISCARD);
6469 rl = REVERSE_LIST (wl, WORD_LIST *);
6470 ret = string_list (rl);
6477 *retlenp = (ret && *ret) ? strlen (ret) : 0;
6480 parser_state |= PST_ASSIGNOK;
6485 /************************************************
6487 * SAVING AND RESTORING PARTIAL PARSE STATE *
6489 ************************************************/
6492 save_parser_state (ps)
6493 sh_parser_state_t *ps;
6496 ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t));
6498 return ((sh_parser_state_t *)NULL);
6500 ps->parser_state = parser_state;
6501 ps->token_state = save_token_state ();
6503 ps->input_line_terminator = shell_input_line_terminator;
6504 ps->eof_encountered = eof_encountered;
6505 ps->eol_lookahead = eol_ungetc_lookahead;
6507 ps->prompt_string_pointer = prompt_string_pointer;
6509 ps->current_command_line_count = current_command_line_count;
6511 #if defined (HISTORY)
6512 ps->remember_on_history = remember_on_history;
6513 # if defined (BANG_HISTORY)
6514 ps->history_expansion_inhibited = history_expansion_inhibited;
6518 ps->last_command_exit_value = last_command_exit_value;
6519 #if defined (ARRAY_VARS)
6520 ps->pipestatus = save_pipestatus_array ();
6523 ps->last_shell_builtin = last_shell_builtin;
6524 ps->this_shell_builtin = this_shell_builtin;
6526 ps->expand_aliases = expand_aliases;
6527 ps->echo_input_at_read = echo_input_at_read;
6528 ps->need_here_doc = need_here_doc;
6529 ps->here_doc_first_line = here_doc_first_line;
6531 ps->esacs_needed = esacs_needed_count;
6532 ps->expecting_in = expecting_in_token;
6534 if (need_here_doc == 0)
6535 ps->redir_stack[0] = 0;
6537 memcpy (ps->redir_stack, redir_stack, sizeof (redir_stack[0]) * HEREDOC_MAX);
6539 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
6540 ps->pushed_strings = pushed_string_list;
6543 ps->eof_token = shell_eof_token;
6545 ps->token_buffer_size = token_buffer_size;
6546 /* Force reallocation on next call to read_token_word */
6548 token_buffer_size = 0;
6554 restore_parser_state (ps)
6555 sh_parser_state_t *ps;
6562 parser_state = ps->parser_state;
6563 if (ps->token_state)
6565 restore_token_state (ps->token_state);
6566 free (ps->token_state);
6569 shell_input_line_terminator = ps->input_line_terminator;
6570 eof_encountered = ps->eof_encountered;
6571 eol_ungetc_lookahead = ps->eol_lookahead;
6573 prompt_string_pointer = ps->prompt_string_pointer;
6575 current_command_line_count = ps->current_command_line_count;
6577 #if defined (HISTORY)
6578 remember_on_history = ps->remember_on_history;
6579 # if defined (BANG_HISTORY)
6580 history_expansion_inhibited = ps->history_expansion_inhibited;
6584 last_command_exit_value = ps->last_command_exit_value;
6585 #if defined (ARRAY_VARS)
6586 restore_pipestatus_array (ps->pipestatus);
6589 last_shell_builtin = ps->last_shell_builtin;
6590 this_shell_builtin = ps->this_shell_builtin;
6592 expand_aliases = ps->expand_aliases;
6593 echo_input_at_read = ps->echo_input_at_read;
6594 need_here_doc = ps->need_here_doc;
6595 here_doc_first_line = ps->here_doc_first_line;
6597 esacs_needed_count = ps->esacs_needed;
6598 expecting_in_token = ps->expecting_in;
6601 for (i = 0; i < HEREDOC_MAX; i++)
6602 redir_stack[i] = ps->redir_stack[i];
6604 if (need_here_doc == 0)
6607 memcpy (redir_stack, ps->redir_stack, sizeof (redir_stack[0]) * HEREDOC_MAX);
6610 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
6611 pushed_string_list = (STRING_SAVER *)ps->pushed_strings;
6616 token_buffer_size = ps->token_buffer_size;
6617 shell_eof_token = ps->eof_token;
6620 sh_input_line_state_t *
6621 save_input_line_state (ls)
6622 sh_input_line_state_t *ls;
6625 ls = (sh_input_line_state_t *)xmalloc (sizeof (sh_input_line_state_t));
6627 return ((sh_input_line_state_t *)NULL);
6629 ls->input_line = shell_input_line;
6630 ls->input_line_size = shell_input_line_size;
6631 ls->input_line_len = shell_input_line_len;
6632 ls->input_line_index = shell_input_line_index;
6634 #if defined (HANDLE_MULTIBYTE)
6635 ls->input_property = shell_input_line_property;
6636 ls->input_propsize = shell_input_line_propsize;
6639 /* force reallocation */
6640 shell_input_line = 0;
6641 shell_input_line_size = shell_input_line_len = shell_input_line_index = 0;
6643 #if defined (HANDLE_MULTIBYTE)
6644 shell_input_line_property = 0;
6645 shell_input_line_propsize = 0;
6652 restore_input_line_state (ls)
6653 sh_input_line_state_t *ls;
6655 FREE (shell_input_line);
6656 shell_input_line = ls->input_line;
6657 shell_input_line_size = ls->input_line_size;
6658 shell_input_line_len = ls->input_line_len;
6659 shell_input_line_index = ls->input_line_index;
6661 #if defined (HANDLE_MULTIBYTE)
6662 FREE (shell_input_line_property);
6663 shell_input_line_property = ls->input_property;
6664 shell_input_line_propsize = ls->input_propsize;
6668 set_line_mbstate ();
6672 /************************************************
6674 * MULTIBYTE CHARACTER HANDLING *
6676 ************************************************/
6678 #if defined (HANDLE_MULTIBYTE)
6680 /* We don't let the property buffer get larger than this unless the line is */
6681 #define MAX_PROPSIZE 32768
6687 size_t i, previ, len;
6688 mbstate_t mbs, prevs;
6692 if (shell_input_line == NULL)
6694 len = STRLEN (shell_input_line); /* XXX - shell_input_line_len ? */
6697 if (shell_input_line_propsize >= MAX_PROPSIZE && len < MAX_PROPSIZE>>1)
6699 free (shell_input_line_property);
6700 shell_input_line_property = 0;
6701 shell_input_line_propsize = 0;
6703 if (len+1 > shell_input_line_propsize)
6705 shell_input_line_propsize = len + 1;
6706 shell_input_line_property = (char *)xrealloc (shell_input_line_property, shell_input_line_propsize);
6709 if (locale_mb_cur_max == 1)
6711 memset (shell_input_line_property, 1, len);
6715 /* XXX - use whether or not we are in a UTF-8 locale to avoid calls to
6717 if (locale_utf8locale == 0)
6718 memset (&prevs, '\0', sizeof (mbstate_t));
6720 for (i = previ = 0; i < len; i++)
6722 if (locale_utf8locale == 0)
6725 c = shell_input_line[i];
6729 for (j = i; j < len; j++)
6730 shell_input_line_property[j] = 1;
6734 if (locale_utf8locale)
6736 if ((unsigned char)shell_input_line[previ] < 128) /* i != previ */
6740 ilen = utf8_mblen (shell_input_line + previ, i - previ + 1);
6741 mbclen = (ilen == -1) ? (size_t)-1
6742 : ((ilen == -2) ? (size_t)-2 : (size_t)ilen);
6746 mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs);
6748 if (mbclen == 1 || mbclen == (size_t)-1)
6753 else if (mbclen == (size_t)-2)
6755 else if (mbclen > 1)
6759 if (locale_utf8locale == 0)
6765 for (j = i; j < len; j++)
6766 shell_input_line_property[j] = 1;
6770 shell_input_line_property[i] = mbclen;
6773 #endif /* HANDLE_MULTIBYTE */