1 /* parse.y - Yacc grammar for bash. */
3 /* Copyright (C) 1989-2015 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 "typemax.h" /* SIZE_MAX if needed */
52 #include "mailcheck.h"
55 #include "builtins/common.h"
56 #include "builtins/builtext.h"
60 #if defined (READLINE)
61 # include "bashline.h"
62 # include <readline/readline.h>
66 # include "bashhist.h"
67 # include <readline/history.h>
70 #if defined (JOB_CONTROL)
73 extern int cleanup_dead_jobs __P((void));
74 #endif /* JOB_CONTROL */
79 typedef void *alias_t;
82 #if defined (PROMPT_STRING_DECODE)
84 # include <sys/param.h>
87 # if defined (TM_IN_SYS_TIME)
88 # include <sys/types.h>
89 # include <sys/time.h>
90 # endif /* TM_IN_SYS_TIME */
92 #endif /* PROMPT_STRING_DECODE */
94 #define RE_READ_TOKEN -99
95 #define NO_EXPANSION -100
103 #if defined (HANDLE_MULTIBYTE)
104 # define last_shell_getc_is_singlebyte \
105 ((shell_input_line_index > 1) \
106 ? shell_input_line_property[shell_input_line_index - 1] \
108 # define MBTEST(x) ((x) && last_shell_getc_is_singlebyte)
110 # define last_shell_getc_is_singlebyte 1
111 # define MBTEST(x) ((x))
114 #if defined (EXTENDED_GLOB)
115 extern int extended_glob;
118 extern int eof_encountered;
119 extern int no_line_editing, running_under_emacs;
120 extern int current_command_number;
121 extern int sourcelevel, parse_and_execute_level;
122 extern int posixly_correct;
123 extern int last_command_exit_value;
124 extern pid_t last_command_subst_pid;
125 extern char *shell_name, *current_host_name;
126 extern char *dist_version;
127 extern int patch_level;
128 extern int dump_translatable_strings, dump_po_strings;
129 extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
130 extern int here_doc_first_line;
131 #if defined (BUFFERED_INPUT)
132 extern int bash_input_fd_changed;
136 /* **************************************************************** */
138 /* "Forward" declarations */
140 /* **************************************************************** */
143 static void debug_parser __P((int));
146 static int yy_getc __P((void));
147 static int yy_ungetc __P((int));
149 #if defined (READLINE)
150 static int yy_readline_get __P((void));
151 static int yy_readline_unget __P((int));
154 static int yy_string_get __P((void));
155 static int yy_string_unget __P((int));
156 static void rewind_input_string __P((void));
157 static int yy_stream_get __P((void));
158 static int yy_stream_unget __P((int));
160 static int shell_getc __P((int));
161 static void shell_ungetc __P((int));
162 static void discard_until __P((int));
164 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
165 static void push_string __P((char *, int, alias_t *));
166 static void pop_string __P((void));
167 static void free_string_list __P((void));
170 static char *read_a_line __P((int));
172 static int reserved_word_acceptable __P((int));
173 static int yylex __P((void));
175 static void push_heredoc __P((REDIRECT *));
176 static char *mk_alexpansion __P((char *));
177 static int alias_expand_token __P((char *));
178 static int time_command_acceptable __P((void));
179 static int special_case_tokens __P((char *));
180 static int read_token __P((int));
181 static char *parse_matched_pair __P((int, int, int, int *, int));
182 static char *parse_comsub __P((int, int, int, int *, int));
183 #if defined (ARRAY_VARS)
184 static char *parse_compound_assignment __P((int *));
186 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
187 static int parse_dparen __P((int));
188 static int parse_arith_cmd __P((char **, int));
190 #if defined (COND_COMMAND)
191 static void cond_error __P((void));
192 static COND_COM *cond_expr __P((void));
193 static COND_COM *cond_or __P((void));
194 static COND_COM *cond_and __P((void));
195 static COND_COM *cond_term __P((void));
196 static int cond_skip_newlines __P((void));
197 static COMMAND *parse_cond_command __P((void));
199 #if defined (ARRAY_VARS)
200 static int token_is_assignment __P((char *, int));
201 static int token_is_ident __P((char *, int));
203 static int read_token_word __P((int));
204 static void discard_parser_constructs __P((int));
206 static char *error_token_from_token __P((int));
207 static char *error_token_from_text __P((void));
208 static void print_offending_line __P((void));
209 static void report_syntax_error __P((char *));
211 static void handle_eof_input_unit __P((void));
212 static void prompt_again __P((void));
214 static void reset_readline_prompt __P((void));
216 static void print_prompt __P((void));
218 #if defined (HANDLE_MULTIBYTE)
219 static void set_line_mbstate __P((void));
220 static char *shell_input_line_property = NULL;
222 # define set_line_mbstate()
225 extern int yyerror __P((const char *));
231 /* Default prompt strings */
232 char *primary_prompt = PPROMPT;
233 char *secondary_prompt = SPROMPT;
235 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
236 char *ps1_prompt, *ps2_prompt;
238 /* Displayed after reading a command but before executing it in an interactive shell */
241 /* Handle on the current prompt string. Indirectly points through
242 ps1_ or ps2_prompt. */
243 char **prompt_string_pointer = (char **)NULL;
244 char *current_prompt_string;
246 /* Non-zero means we expand aliases in commands. */
247 int expand_aliases = 0;
249 /* If non-zero, the decoded prompt string undergoes parameter and
250 variable substitution, command substitution, arithmetic substitution,
251 string expansion, process substitution, and quote removal in
252 decode_prompt_string. */
255 /* If non-zero, $'...' and $"..." are expanded when they appear within
256 a ${...} expansion, even when the expansion appears within double
258 int extended_quote = 1;
260 /* The number of lines read from input while creating the current command. */
261 int current_command_line_count;
263 /* The number of lines in a command saved while we run parse_and_execute */
264 int saved_command_line_count;
266 /* The token that currently denotes the end of parse. */
269 /* The token currently being read. */
272 /* The current parser state. */
275 /* Variables to manage the task of reading here documents, because we need to
276 defer the reading until after a complete command has been collected. */
277 static REDIRECT *redir_stack[HEREDOC_MAX];
280 /* Where shell input comes from. History expansion is performed on each
281 line when the shell is interactive. */
282 static char *shell_input_line = (char *)NULL;
283 static size_t shell_input_line_index;
284 static size_t shell_input_line_size; /* Amount allocated for shell_input_line. */
285 static size_t shell_input_line_len; /* strlen (shell_input_line) */
287 /* Either zero or EOF. */
288 static int shell_input_line_terminator;
290 /* The line number in a script on which a function definition starts. */
291 static int function_dstart;
293 /* The line number in a script on which a function body starts. */
294 static int function_bstart;
296 /* The line number in a script at which an arithmetic for command starts. */
297 static int arith_for_lineno;
299 /* The decoded prompt string. Used if READLINE is not defined or if
300 editing is turned off. Analogous to current_readline_prompt. */
301 static char *current_decoded_prompt;
303 /* The last read token, or NULL. read_token () uses this for context
305 static int last_read_token;
307 /* The token read prior to last_read_token. */
308 static int token_before_that;
310 /* The token read prior to token_before_that. */
311 static int two_tokens_ago;
313 static int global_extglob;
315 /* The line number in a script where the word in a `case WORD', `select WORD'
316 or `for WORD' begins. This is a nested command maximum, since the array
317 index is decremented after a case, select, or for command is parsed. */
318 #define MAX_CASE_NEST 128
319 static int word_lineno[MAX_CASE_NEST+1];
320 static int word_top = -1;
322 /* If non-zero, it is the token that we want read_token to return
323 regardless of what text is (or isn't) present to be read. This
324 is reset by read_token. If token_to_read == WORD or
325 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
326 static int token_to_read;
327 static WORD_DESC *word_desc_to_read;
329 static REDIRECTEE source;
330 static REDIRECTEE redir;
334 WORD_DESC *word; /* the word that we read. */
335 int number; /* the number that we read. */
336 WORD_LIST *word_list;
340 PATTERN_LIST *pattern;
343 /* Reserved words. Members of the first group are only recognized
344 in the case that they are preceded by a list_terminator. Members
345 of the second group are for [[...]] commands. Members of the
346 third group are recognized only under special circumstances. */
347 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION COPROC
348 %token COND_START COND_END COND_ERROR
349 %token IN BANG TIME TIMEOPT TIMEIGN
351 /* More general tokens. yylex () knows how to make these. */
352 %token <word> WORD ASSIGNMENT_WORD REDIR_WORD
353 %token <number> NUMBER
354 %token <word_list> ARITH_CMD ARITH_FOR_EXPRS
355 %token <command> COND_CMD
356 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS
357 %token GREATER_AND SEMI_SEMI SEMI_AND SEMI_SEMI_AND
358 %token LESS_LESS_MINUS AND_GREATER AND_GREATER_GREATER LESS_GREATER
359 %token GREATER_BAR BAR_AND
361 /* The types that the various syntactical units return. */
363 %type <command> inputunit command pipeline pipeline_command
364 %type <command> list list0 list1 compound_list simple_list simple_list1
365 %type <command> simple_command shell_command
366 %type <command> for_command select_command case_command group_command
367 %type <command> arith_command
368 %type <command> cond_command
369 %type <command> arith_for_command
370 %type <command> coproc
371 %type <command> function_def function_body if_command elif_clause subshell
372 %type <redirect> redirection redirection_list
373 %type <element> simple_command_element
374 %type <word_list> word_list pattern
375 %type <pattern> pattern_list case_clause_sequence case_clause
376 %type <number> timespec
377 %type <number> list_terminator
381 %left '&' ';' '\n' yacc_EOF
386 inputunit: simple_list simple_list_terminator
388 /* Case of regular command. Discard the error
389 safety net,and return the command just parsed. */
392 /* discard_parser_constructs (0); */
393 if (parser_state & PST_CMDSUBST)
394 parser_state |= PST_EOFTOKEN;
399 /* Case of regular command, but not a very
400 interesting one. Return a NULL command. */
401 global_command = (COMMAND *)NULL;
402 if (parser_state & PST_CMDSUBST)
403 parser_state |= PST_EOFTOKEN;
408 /* Error during parsing. Return NULL command. */
409 global_command = (COMMAND *)NULL;
411 /* discard_parser_constructs (1); */
412 if (interactive && parse_and_execute_level == 0)
423 /* Case of EOF seen by itself. Do ignoreeof or
425 global_command = (COMMAND *)NULL;
426 handle_eof_input_unit ();
432 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
434 { $$ = make_word_list ($2, $1); }
437 redirection: '>' WORD
441 $$ = make_redirection (source, r_output_direction, redir, 0);
447 $$ = make_redirection (source, r_input_direction, redir, 0);
453 $$ = make_redirection (source, r_output_direction, redir, 0);
459 $$ = make_redirection (source, r_input_direction, redir, 0);
461 | REDIR_WORD '>' WORD
463 source.filename = $1;
465 $$ = make_redirection (source, r_output_direction, redir, REDIR_VARASSIGN);
467 | REDIR_WORD '<' WORD
469 source.filename = $1;
471 $$ = make_redirection (source, r_input_direction, redir, REDIR_VARASSIGN);
473 | GREATER_GREATER WORD
477 $$ = make_redirection (source, r_appending_to, redir, 0);
479 | NUMBER GREATER_GREATER WORD
483 $$ = make_redirection (source, r_appending_to, redir, 0);
485 | REDIR_WORD GREATER_GREATER WORD
487 source.filename = $1;
489 $$ = make_redirection (source, r_appending_to, redir, REDIR_VARASSIGN);
495 $$ = make_redirection (source, r_output_force, redir, 0);
497 | NUMBER GREATER_BAR WORD
501 $$ = make_redirection (source, r_output_force, redir, 0);
503 | REDIR_WORD GREATER_BAR WORD
505 source.filename = $1;
507 $$ = make_redirection (source, r_output_force, redir, REDIR_VARASSIGN);
513 $$ = make_redirection (source, r_input_output, redir, 0);
515 | NUMBER LESS_GREATER WORD
519 $$ = make_redirection (source, r_input_output, redir, 0);
521 | REDIR_WORD LESS_GREATER WORD
523 source.filename = $1;
525 $$ = make_redirection (source, r_input_output, redir, REDIR_VARASSIGN);
531 $$ = make_redirection (source, r_reading_until, redir, 0);
534 | NUMBER LESS_LESS WORD
538 $$ = make_redirection (source, r_reading_until, redir, 0);
541 | REDIR_WORD LESS_LESS WORD
543 source.filename = $1;
545 $$ = make_redirection (source, r_reading_until, redir, REDIR_VARASSIGN);
548 | LESS_LESS_MINUS WORD
552 $$ = make_redirection (source, r_deblank_reading_until, redir, 0);
555 | NUMBER LESS_LESS_MINUS WORD
559 $$ = make_redirection (source, r_deblank_reading_until, redir, 0);
562 | REDIR_WORD LESS_LESS_MINUS WORD
564 source.filename = $1;
566 $$ = make_redirection (source, r_deblank_reading_until, redir, REDIR_VARASSIGN);
569 | LESS_LESS_LESS WORD
573 $$ = make_redirection (source, r_reading_string, redir, 0);
575 | NUMBER LESS_LESS_LESS WORD
579 $$ = make_redirection (source, r_reading_string, redir, 0);
581 | REDIR_WORD LESS_LESS_LESS WORD
583 source.filename = $1;
585 $$ = make_redirection (source, r_reading_string, redir, REDIR_VARASSIGN);
591 $$ = make_redirection (source, r_duplicating_input, redir, 0);
593 | NUMBER LESS_AND NUMBER
597 $$ = make_redirection (source, r_duplicating_input, redir, 0);
599 | REDIR_WORD LESS_AND NUMBER
601 source.filename = $1;
603 $$ = make_redirection (source, r_duplicating_input, redir, REDIR_VARASSIGN);
609 $$ = make_redirection (source, r_duplicating_output, redir, 0);
611 | NUMBER GREATER_AND NUMBER
615 $$ = make_redirection (source, r_duplicating_output, redir, 0);
617 | REDIR_WORD GREATER_AND NUMBER
619 source.filename = $1;
621 $$ = make_redirection (source, r_duplicating_output, redir, REDIR_VARASSIGN);
627 $$ = make_redirection (source, r_duplicating_input_word, redir, 0);
629 | NUMBER LESS_AND WORD
633 $$ = make_redirection (source, r_duplicating_input_word, redir, 0);
635 | REDIR_WORD LESS_AND WORD
637 source.filename = $1;
639 $$ = make_redirection (source, r_duplicating_input_word, redir, REDIR_VARASSIGN);
645 $$ = make_redirection (source, r_duplicating_output_word, redir, 0);
647 | NUMBER GREATER_AND WORD
651 $$ = make_redirection (source, r_duplicating_output_word, redir, 0);
653 | REDIR_WORD GREATER_AND WORD
655 source.filename = $1;
657 $$ = make_redirection (source, r_duplicating_output_word, redir, REDIR_VARASSIGN);
663 $$ = make_redirection (source, r_close_this, redir, 0);
665 | NUMBER GREATER_AND '-'
669 $$ = make_redirection (source, r_close_this, redir, 0);
671 | REDIR_WORD GREATER_AND '-'
673 source.filename = $1;
675 $$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
681 $$ = make_redirection (source, r_close_this, redir, 0);
683 | NUMBER LESS_AND '-'
687 $$ = make_redirection (source, r_close_this, redir, 0);
689 | REDIR_WORD LESS_AND '-'
691 source.filename = $1;
693 $$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
699 $$ = make_redirection (source, r_err_and_out, redir, 0);
701 | AND_GREATER_GREATER WORD
705 $$ = make_redirection (source, r_append_err_and_out, redir, 0);
709 simple_command_element: WORD
710 { $$.word = $1; $$.redirect = 0; }
712 { $$.word = $1; $$.redirect = 0; }
714 { $$.redirect = $1; $$.word = 0; }
717 redirection_list: redirection
721 | redirection_list redirection
723 register REDIRECT *t;
725 for (t = $1; t->next; t = t->next)
732 simple_command: simple_command_element
733 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
734 | simple_command simple_command_element
735 { $$ = make_simple_command ($2, $1); }
738 command: simple_command
739 { $$ = clean_simple_command ($1); }
742 | shell_command redirection_list
749 register REDIRECT *t;
750 for (t = tc->redirects; t->next; t = t->next)
764 shell_command: for_command
768 | WHILE compound_list DO compound_list DONE
769 { $$ = make_while_command ($2, $4); }
770 | UNTIL compound_list DO compound_list DONE
771 { $$ = make_until_command ($2, $4); }
788 for_command: FOR WORD newline_list DO compound_list DONE
790 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
791 if (word_top > 0) word_top--;
793 | FOR WORD newline_list '{' compound_list '}'
795 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
796 if (word_top > 0) word_top--;
798 | FOR WORD ';' newline_list DO compound_list DONE
800 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
801 if (word_top > 0) word_top--;
803 | FOR WORD ';' newline_list '{' compound_list '}'
805 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
806 if (word_top > 0) word_top--;
808 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
810 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
811 if (word_top > 0) word_top--;
813 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
815 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
816 if (word_top > 0) word_top--;
818 | FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
820 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
821 if (word_top > 0) word_top--;
823 | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
825 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
826 if (word_top > 0) word_top--;
830 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
832 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
833 if (word_top > 0) word_top--;
835 | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
837 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
838 if (word_top > 0) word_top--;
840 | FOR ARITH_FOR_EXPRS DO compound_list DONE
842 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
843 if (word_top > 0) word_top--;
845 | FOR ARITH_FOR_EXPRS '{' compound_list '}'
847 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
848 if (word_top > 0) word_top--;
852 select_command: SELECT WORD newline_list DO list DONE
854 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
855 if (word_top > 0) word_top--;
857 | SELECT WORD newline_list '{' list '}'
859 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
860 if (word_top > 0) word_top--;
862 | SELECT WORD ';' newline_list DO list DONE
864 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
865 if (word_top > 0) word_top--;
867 | SELECT WORD ';' newline_list '{' list '}'
869 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
870 if (word_top > 0) word_top--;
872 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
874 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
875 if (word_top > 0) word_top--;
877 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
879 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
880 if (word_top > 0) word_top--;
884 case_command: CASE WORD newline_list IN newline_list ESAC
886 $$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]);
887 if (word_top > 0) word_top--;
889 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
891 $$ = make_case_command ($2, $5, word_lineno[word_top]);
892 if (word_top > 0) word_top--;
894 | CASE WORD newline_list IN case_clause ESAC
896 $$ = make_case_command ($2, $5, word_lineno[word_top]);
897 if (word_top > 0) word_top--;
901 function_def: WORD '(' ')' newline_list function_body
902 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
904 | FUNCTION WORD '(' ')' newline_list function_body
905 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
907 | FUNCTION WORD newline_list function_body
908 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
911 function_body: shell_command
913 | shell_command redirection_list
918 /* According to Posix.2 3.9.5, redirections
919 specified after the body of a function should
920 be attached to the function and performed when
921 the function is executed, not as part of the
922 function definition command. */
923 /* XXX - I don't think it matters, but we might
924 want to change this in the future to avoid
925 problems differentiating between a function
926 definition with a redirection and a function
927 definition containing a single command with a
928 redirection. The two are semantically equivalent,
929 though -- the only difference is in how the
930 command printing code displays the redirections. */
933 register REDIRECT *t;
934 for (t = tc->redirects; t->next; t = t->next)
944 subshell: '(' compound_list ')'
946 $$ = make_subshell_command ($2);
947 $$->flags |= CMD_WANT_SUBSHELL;
951 coproc: COPROC shell_command
953 $$ = make_coproc_command ("COPROC", $2);
954 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
956 | COPROC shell_command redirection_list
963 register REDIRECT *t;
964 for (t = tc->redirects; t->next; t = t->next)
970 $$ = make_coproc_command ("COPROC", $2);
971 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
973 | COPROC WORD shell_command
975 $$ = make_coproc_command ($2->word, $3);
976 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
978 | COPROC WORD shell_command redirection_list
985 register REDIRECT *t;
986 for (t = tc->redirects; t->next; t = t->next)
992 $$ = make_coproc_command ($2->word, $3);
993 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
995 | COPROC simple_command
997 $$ = make_coproc_command ("COPROC", clean_simple_command ($2));
998 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
1002 if_command: IF compound_list THEN compound_list FI
1003 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
1004 | IF compound_list THEN compound_list ELSE compound_list FI
1005 { $$ = make_if_command ($2, $4, $6); }
1006 | IF compound_list THEN compound_list elif_clause FI
1007 { $$ = make_if_command ($2, $4, $5); }
1011 group_command: '{' compound_list '}'
1012 { $$ = make_group_command ($2); }
1015 arith_command: ARITH_CMD
1016 { $$ = make_arith_command ($1); }
1019 cond_command: COND_START COND_CMD COND_END
1023 elif_clause: ELIF compound_list THEN compound_list
1024 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
1025 | ELIF compound_list THEN compound_list ELSE compound_list
1026 { $$ = make_if_command ($2, $4, $6); }
1027 | ELIF compound_list THEN compound_list elif_clause
1028 { $$ = make_if_command ($2, $4, $5); }
1031 case_clause: pattern_list
1032 | case_clause_sequence pattern_list
1033 { $2->next = $1; $$ = $2; }
1036 pattern_list: newline_list pattern ')' compound_list
1037 { $$ = make_pattern_list ($2, $4); }
1038 | newline_list pattern ')' newline_list
1039 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
1040 | newline_list '(' pattern ')' compound_list
1041 { $$ = make_pattern_list ($3, $5); }
1042 | newline_list '(' pattern ')' newline_list
1043 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
1046 case_clause_sequence: pattern_list SEMI_SEMI
1048 | case_clause_sequence pattern_list SEMI_SEMI
1049 { $2->next = $1; $$ = $2; }
1050 | pattern_list SEMI_AND
1051 { $1->flags |= CASEPAT_FALLTHROUGH; $$ = $1; }
1052 | case_clause_sequence pattern_list SEMI_AND
1053 { $2->flags |= CASEPAT_FALLTHROUGH; $2->next = $1; $$ = $2; }
1054 | pattern_list SEMI_SEMI_AND
1055 { $1->flags |= CASEPAT_TESTNEXT; $$ = $1; }
1056 | case_clause_sequence pattern_list SEMI_SEMI_AND
1057 { $2->flags |= CASEPAT_TESTNEXT; $2->next = $1; $$ = $2; }
1061 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
1063 { $$ = make_word_list ($3, $1); }
1066 /* A list allows leading or trailing newlines and
1067 newlines as operators (equivalent to semicolons).
1068 It must end with a newline or semicolon.
1069 Lists are used within commands such as if, for, while. */
1071 list: newline_list list0
1075 gather_here_documents ();
1080 | newline_list list1
1086 list0: list1 '\n' newline_list
1087 | list1 '&' newline_list
1089 if ($1->type == cm_connection)
1090 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
1092 $$ = command_connect ($1, (COMMAND *)NULL, '&');
1094 | list1 ';' newline_list
1098 list1: list1 AND_AND newline_list list1
1099 { $$ = command_connect ($1, $4, AND_AND); }
1100 | list1 OR_OR newline_list list1
1101 { $$ = command_connect ($1, $4, OR_OR); }
1102 | list1 '&' newline_list list1
1104 if ($1->type == cm_connection)
1105 $$ = connect_async_list ($1, $4, '&');
1107 $$ = command_connect ($1, $4, '&');
1109 | list1 ';' newline_list list1
1110 { $$ = command_connect ($1, $4, ';'); }
1111 | list1 '\n' newline_list list1
1112 { $$ = command_connect ($1, $4, ';'); }
1117 simple_list_terminator: '\n'
1121 list_terminator:'\n'
1133 /* A simple_list is a list that contains no significant newlines
1134 and no leading or trailing newlines. Newlines are allowed
1135 only following operators, where they are not significant.
1137 This is what an inputunit consists of. */
1139 simple_list: simple_list1
1143 gather_here_documents ();
1144 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1146 global_command = $1;
1147 eof_encountered = 0;
1148 rewind_input_string ();
1154 if ($1->type == cm_connection)
1155 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
1157 $$ = command_connect ($1, (COMMAND *)NULL, '&');
1159 gather_here_documents ();
1160 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1162 global_command = $1;
1163 eof_encountered = 0;
1164 rewind_input_string ();
1172 gather_here_documents ();
1173 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1175 global_command = $1;
1176 eof_encountered = 0;
1177 rewind_input_string ();
1183 simple_list1: simple_list1 AND_AND newline_list simple_list1
1184 { $$ = command_connect ($1, $4, AND_AND); }
1185 | simple_list1 OR_OR newline_list simple_list1
1186 { $$ = command_connect ($1, $4, OR_OR); }
1187 | simple_list1 '&' simple_list1
1189 if ($1->type == cm_connection)
1190 $$ = connect_async_list ($1, $3, '&');
1192 $$ = command_connect ($1, $3, '&');
1194 | simple_list1 ';' simple_list1
1195 { $$ = command_connect ($1, $3, ';'); }
1201 pipeline_command: pipeline
1203 | BANG pipeline_command
1206 $2->flags ^= CMD_INVERT_RETURN; /* toggle */
1209 | timespec pipeline_command
1215 | timespec list_terminator
1219 /* Boy, this is unclean. `time' by itself can
1220 time a null command. We cheat and push a
1221 newline back if the list_terminator was a newline
1222 to avoid the double-newline problem (one to
1223 terminate this, one to terminate the command) */
1226 $$ = make_simple_command (x, (COMMAND *)NULL);
1228 /* XXX - let's cheat and push a newline back */
1230 token_to_read = '\n';
1232 token_to_read = ';';
1234 | BANG list_terminator
1238 /* This is just as unclean. Posix says that `!'
1239 by itself should be equivalent to `false'.
1241 newline back if the list_terminator was a newline
1242 to avoid the double-newline problem (one to
1243 terminate this, one to terminate the command) */
1246 $$ = make_simple_command (x, (COMMAND *)NULL);
1247 $$->flags |= CMD_INVERT_RETURN;
1248 /* XXX - let's cheat and push a newline back */
1250 token_to_read = '\n';
1252 token_to_read = ';';
1256 pipeline: pipeline '|' newline_list pipeline
1257 { $$ = command_connect ($1, $4, '|'); }
1258 | pipeline BAR_AND newline_list pipeline
1260 /* Make cmd1 |& cmd2 equivalent to cmd1 2>&1 | cmd2 */
1265 tc = $1->type == cm_simple ? (COMMAND *)$1->value.Simple : $1;
1268 r = make_redirection (sd, r_duplicating_output, rd, 0);
1271 register REDIRECT *t;
1272 for (t = tc->redirects; t->next; t = t->next)
1279 $$ = command_connect ($1, $4, '|');
1286 { $$ = CMD_TIME_PIPELINE; }
1288 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1289 | TIME TIMEOPT TIMEIGN
1290 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1294 /* Initial size to allocate for tokens, and the
1295 amount to grow them by. */
1296 #define TOKEN_DEFAULT_INITIAL_SIZE 496
1297 #define TOKEN_DEFAULT_GROW_SIZE 512
1299 /* Should we call prompt_again? */
1300 #define SHOULD_PROMPT() \
1301 (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
1304 # define expanding_alias() (pushed_string_list && pushed_string_list->expander)
1306 # define expanding_alias() 0
1309 /* Global var is non-zero when end of file has been reached. */
1310 int EOF_Reached = 0;
1323 /* yy_getc () returns the next available character from input or EOF.
1324 yy_ungetc (c) makes `c' the next character to read.
1325 init_yy_io (get, unget, type, location) makes the function GET the
1326 installed function for getting the next character, makes UNGET the
1327 installed function for un-getting a character, sets the type of stream
1328 (either string or file) from TYPE, and makes LOCATION point to where
1329 the input is coming from. */
1331 /* Unconditionally returns end-of-file. */
1338 /* Variable containing the current get and unget functions.
1339 See ./input.h for a clearer description. */
1340 BASH_INPUT bash_input;
1342 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
1343 is non-null, avoiding a memory leak. */
1345 initialize_bash_input ()
1347 bash_input.type = st_none;
1348 FREE (bash_input.name);
1349 bash_input.name = (char *)NULL;
1350 bash_input.location.file = (FILE *)NULL;
1351 bash_input.location.string = (char *)NULL;
1352 bash_input.getter = (sh_cget_func_t *)NULL;
1353 bash_input.ungetter = (sh_cunget_func_t *)NULL;
1356 /* Set the contents of the current bash input stream from
1357 GET, UNGET, TYPE, NAME, and LOCATION. */
1359 init_yy_io (get, unget, type, name, location)
1360 sh_cget_func_t *get;
1361 sh_cunget_func_t *unget;
1362 enum stream_type type;
1364 INPUT_STREAM location;
1366 bash_input.type = type;
1367 FREE (bash_input.name);
1368 bash_input.name = name ? savestring (name) : (char *)NULL;
1372 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
1374 bash_input.location = location;
1376 bash_input.getter = get;
1377 bash_input.ungetter = unget;
1383 return (bash_input.name ? bash_input.name : "stdin");
1386 /* Call this to get the next character of input. */
1390 return (*(bash_input.getter)) ();
1393 /* Call this to unget C. That is, to make C the next character
1399 return (*(bash_input.ungetter)) (c);
1402 #if defined (BUFFERED_INPUT)
1403 #ifdef INCLUDE_UNUSED
1405 input_file_descriptor ()
1407 switch (bash_input.type)
1410 return (fileno (bash_input.location.file));
1412 return (bash_input.location.buffered_fd);
1415 return (fileno (stdin));
1419 #endif /* BUFFERED_INPUT */
1421 /* **************************************************************** */
1423 /* Let input be read from readline (). */
1425 /* **************************************************************** */
1427 #if defined (READLINE)
1428 char *current_readline_prompt = (char *)NULL;
1429 char *current_readline_line = (char *)NULL;
1430 int current_readline_line_index = 0;
1435 SigHandler *old_sigint;
1439 if (current_readline_line == 0)
1441 if (bash_readline_initialized == 0)
1442 initialize_readline ();
1444 #if defined (JOB_CONTROL)
1446 give_terminal_to (shell_pgrp, 0);
1447 #endif /* JOB_CONTROL */
1449 old_sigint = IMPOSSIBLE_TRAP_HANDLER;
1450 if (signal_is_ignored (SIGINT) == 0)
1452 /* interrupt_immediately++; */
1453 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
1456 current_readline_line = readline (current_readline_prompt ?
1457 current_readline_prompt : "");
1460 if (signal_is_ignored (SIGINT) == 0)
1462 /* interrupt_immediately--; */
1463 if (old_sigint != IMPOSSIBLE_TRAP_HANDLER)
1464 set_signal_handler (SIGINT, old_sigint);
1468 /* Reset the prompt to the decoded value of prompt_string_pointer. */
1469 reset_readline_prompt ();
1472 if (current_readline_line == 0)
1475 current_readline_line_index = 0;
1476 line_len = strlen (current_readline_line);
1478 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
1479 current_readline_line[line_len++] = '\n';
1480 current_readline_line[line_len] = '\0';
1483 if (current_readline_line[current_readline_line_index] == 0)
1485 free (current_readline_line);
1486 current_readline_line = (char *)NULL;
1487 return (yy_readline_get ());
1491 c = current_readline_line[current_readline_line_index++];
1497 yy_readline_unget (c)
1500 if (current_readline_line_index && current_readline_line)
1501 current_readline_line[--current_readline_line_index] = c;
1506 with_input_from_stdin ()
1508 INPUT_STREAM location;
1510 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1512 location.string = current_readline_line;
1513 init_yy_io (yy_readline_get, yy_readline_unget,
1514 st_stdin, "readline stdin", location);
1518 #else /* !READLINE */
1521 with_input_from_stdin ()
1523 with_input_from_stream (stdin, "stdin");
1525 #endif /* !READLINE */
1527 /* **************************************************************** */
1529 /* Let input come from STRING. STRING is zero terminated. */
1531 /* **************************************************************** */
1536 register char *string;
1537 register unsigned char c;
1539 string = bash_input.location.string;
1541 /* If the string doesn't exist, or is empty, EOF found. */
1542 if (string && *string)
1545 bash_input.location.string = string;
1556 *(--bash_input.location.string) = c;
1561 with_input_from_string (string, name)
1565 INPUT_STREAM location;
1567 location.string = string;
1568 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1571 /* Count the number of characters we've consumed from bash_input.location.string
1572 and read into shell_input_line, but have not returned from shell_getc.
1573 That is the true input location. Rewind bash_input.location.string by
1574 that number of characters, so it points to the last character actually
1575 consumed by the parser. */
1577 rewind_input_string ()
1581 /* number of unconsumed characters in the input -- XXX need to take newlines
1582 into account, e.g., $(...\n) */
1583 xchars = shell_input_line_len - shell_input_line_index;
1584 if (bash_input.location.string[-1] == '\n')
1587 /* XXX - how to reflect bash_input.location.string back to string passed to
1588 parse_and_execute or xparse_dolparen? xparse_dolparen needs to know how
1589 far into the string we parsed. parse_and_execute knows where bash_input.
1590 location.string is, and how far from orig_string that is -- that's the
1591 number of characters the command consumed. */
1593 /* bash_input.location.string - xchars should be where we parsed to */
1594 /* need to do more validation on xchars value for sanity -- test cases. */
1595 bash_input.location.string -= xchars;
1598 /* **************************************************************** */
1600 /* Let input come from STREAM. */
1602 /* **************************************************************** */
1604 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1605 define, and just use getc/ungetc if it was defined, but since bash
1606 installs its signal handlers without the SA_RESTART flag, some signals
1607 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
1608 the read to be restarted. We need to restart it ourselves. */
1616 if (bash_input.location.file)
1620 interrupt_immediately++;
1623 /* XXX - don't need terminate_immediately; getc_with_restart checks
1624 for terminating signals itself if read returns < 0 */
1625 result = getc_with_restart (bash_input.location.file);
1629 interrupt_immediately--;
1639 return (ungetc_with_restart (c, bash_input.location.file));
1643 with_input_from_stream (stream, name)
1647 INPUT_STREAM location;
1649 location.file = stream;
1650 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1653 typedef struct stream_saver {
1654 struct stream_saver *next;
1655 BASH_INPUT bash_input;
1657 #if defined (BUFFERED_INPUT)
1658 BUFFERED_STREAM *bstream;
1659 #endif /* BUFFERED_INPUT */
1662 /* The globally known line number. */
1663 int line_number = 0;
1665 /* The line number offset set by assigning to LINENO. Not currently used. */
1666 int line_number_base = 0;
1668 #if defined (COND_COMMAND)
1669 static int cond_lineno;
1670 static int cond_token;
1673 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1676 push_stream (reset_lineno)
1679 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1681 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1683 #if defined (BUFFERED_INPUT)
1684 saver->bstream = (BUFFERED_STREAM *)NULL;
1685 /* If we have a buffered stream, clear out buffers[fd]. */
1686 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1687 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1688 (BUFFERED_STREAM *)NULL);
1689 #endif /* BUFFERED_INPUT */
1691 saver->line = line_number;
1692 bash_input.name = (char *)NULL;
1693 saver->next = stream_list;
1694 stream_list = saver;
1707 STREAM_SAVER *saver = stream_list;
1710 stream_list = stream_list->next;
1712 init_yy_io (saver->bash_input.getter,
1713 saver->bash_input.ungetter,
1714 saver->bash_input.type,
1715 saver->bash_input.name,
1716 saver->bash_input.location);
1718 #if defined (BUFFERED_INPUT)
1719 /* If we have a buffered stream, restore buffers[fd]. */
1720 /* If the input file descriptor was changed while this was on the
1721 save stack, update the buffered fd to the new file descriptor and
1722 re-establish the buffer <-> bash_input fd correspondence. */
1723 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1725 if (bash_input_fd_changed)
1727 bash_input_fd_changed = 0;
1728 if (default_buffered_input >= 0)
1730 bash_input.location.buffered_fd = default_buffered_input;
1731 saver->bstream->b_fd = default_buffered_input;
1732 SET_CLOSE_ON_EXEC (default_buffered_input);
1735 /* XXX could free buffered stream returned as result here. */
1736 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1738 #endif /* BUFFERED_INPUT */
1740 line_number = saver->line;
1742 FREE (saver->bash_input.name);
1747 /* Return 1 if a stream of type TYPE is saved on the stack. */
1749 stream_on_stack (type)
1750 enum stream_type type;
1752 register STREAM_SAVER *s;
1754 for (s = stream_list; s; s = s->next)
1755 if (s->bash_input.type == type)
1760 /* Save the current token state and return it in a malloced array. */
1766 ret = (int *)xmalloc (4 * sizeof (int));
1767 ret[0] = last_read_token;
1768 ret[1] = token_before_that;
1769 ret[2] = two_tokens_ago;
1770 ret[3] = current_token;
1775 restore_token_state (ts)
1780 last_read_token = ts[0];
1781 token_before_that = ts[1];
1782 two_tokens_ago = ts[2];
1783 current_token = ts[3];
1787 * This is used to inhibit alias expansion and reserved word recognition
1788 * inside case statement pattern lists. A `case statement pattern list' is:
1790 * everything between the `in' in a `case word in' and the next ')'
1792 * everything between a `;;' and the next `)' or `esac'
1795 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1797 #define END_OF_ALIAS 0
1800 * Pseudo-global variables used in implementing token-wise alias expansion.
1804 * Pushing and popping strings. This works together with shell_getc to
1805 * implement alias expansion on a per-token basis.
1808 #define PSH_ALIAS 0x01
1809 #define PSH_DPAREN 0x02
1810 #define PSH_SOURCE 0x04
1812 typedef struct string_saver {
1813 struct string_saver *next;
1814 int expand_alias; /* Value to set expand_alias to when string is popped. */
1817 alias_t *expander; /* alias that caused this line to be pushed. */
1819 size_t saved_line_size, saved_line_index;
1820 int saved_line_terminator;
1824 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1827 * Push the current shell_input_line onto a stack of such lines and make S
1828 * the current input. Used when expanding aliases. EXPAND is used to set
1829 * the value of expand_next_token when the string is popped, so that the
1830 * word after the alias in the original line is handled correctly when the
1831 * alias expands to multiple words. TOKEN is the token that was expanded
1832 * into S; it is saved and used to prevent infinite recursive expansion.
1835 push_string (s, expand, ap)
1840 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
1842 temp->expand_alias = expand;
1843 temp->saved_line = shell_input_line;
1844 temp->saved_line_size = shell_input_line_size;
1845 temp->saved_line_index = shell_input_line_index;
1846 temp->saved_line_terminator = shell_input_line_terminator;
1849 temp->expander = ap;
1851 temp->flags = PSH_ALIAS;
1853 temp->next = pushed_string_list;
1854 pushed_string_list = temp;
1858 ap->flags |= AL_BEINGEXPANDED;
1861 shell_input_line = s;
1862 shell_input_line_size = STRLEN (s);
1863 shell_input_line_index = 0;
1864 shell_input_line_terminator = '\0';
1866 parser_state &= ~PST_ALEXPNEXT; /* XXX */
1869 set_line_mbstate ();
1873 * Make the top of the pushed_string stack be the current shell input.
1874 * Only called when there is something on the stack. Called from shell_getc
1875 * when it thinks it has consumed the string generated by an alias expansion
1876 * and needs to return to the original input line.
1883 FREE (shell_input_line);
1884 shell_input_line = pushed_string_list->saved_line;
1885 shell_input_line_index = pushed_string_list->saved_line_index;
1886 shell_input_line_size = pushed_string_list->saved_line_size;
1887 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1889 if (pushed_string_list->expand_alias)
1890 parser_state |= PST_ALEXPNEXT;
1892 parser_state &= ~PST_ALEXPNEXT;
1894 t = pushed_string_list;
1895 pushed_string_list = pushed_string_list->next;
1899 t->expander->flags &= ~AL_BEINGEXPANDED;
1904 set_line_mbstate ();
1910 register STRING_SAVER *t, *t1;
1912 for (t = pushed_string_list; t; )
1915 FREE (t->saved_line);
1918 t->expander->flags &= ~AL_BEINGEXPANDED;
1923 pushed_string_list = (STRING_SAVER *)NULL;
1926 #endif /* ALIAS || DPAREN_ARITHMETIC */
1929 free_pushed_string_input ()
1931 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1932 free_string_list ();
1937 parser_expanding_alias ()
1939 return (expanding_alias ());
1943 parser_save_alias ()
1945 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1946 push_string ((char *)NULL, 0, (alias_t *)NULL);
1947 pushed_string_list->flags = PSH_SOURCE; /* XXX - for now */
1954 parser_restore_alias ()
1956 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1957 if (pushed_string_list)
1965 clear_shell_input_line ()
1967 if (shell_input_line)
1968 shell_input_line[shell_input_line_index = 0] = '\0';
1971 /* Return a line of text, taken from wherever yylex () reads input.
1972 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1973 is non-zero, we remove unquoted \<newline> pairs. This is used by
1974 read_secondary_line to read here documents. */
1976 read_a_line (remove_quoted_newline)
1977 int remove_quoted_newline;
1979 static char *line_buffer = (char *)NULL;
1980 static int buffer_size = 0;
1981 int indx, c, peekc, pass_next;
1983 #if defined (READLINE)
1984 if (no_line_editing && SHOULD_PROMPT ())
1986 if (SHOULD_PROMPT ())
1990 pass_next = indx = 0;
1993 /* Allow immediate exit if interrupted during input. */
1998 /* Ignore null bytes in input. */
2002 internal_warning ("read_a_line: ignored null byte in input");
2007 /* If there is no more input, then we return NULL. */
2010 if (interactive && bash_input.type == st_stream)
2013 return ((char *)NULL);
2017 /* `+2' in case the final character in the buffer is a newline. */
2018 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
2020 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
2021 here document with an unquoted delimiter. In this case,
2022 the line will be expanded as if it were in double quotes.
2023 We allow a backslash to escape the next character, but we
2024 need to treat the backslash specially only if a backslash
2025 quoting a backslash-newline pair appears in the line. */
2028 line_buffer[indx++] = c;
2031 else if (c == '\\' && remove_quoted_newline)
2038 continue; /* Make the unquoted \<newline> pair disappear. */
2044 line_buffer[indx++] = c; /* Preserve the backslash. */
2048 line_buffer[indx++] = c;
2052 line_buffer[indx] = '\0';
2053 return (line_buffer);
2058 /* Return a line as in read_a_line (), but insure that the prompt is
2059 the secondary prompt. This is used to read the lines of a here
2060 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
2061 newlines quoted with backslashes while reading the line. It is
2062 non-zero unless the delimiter of the here document was quoted. */
2064 read_secondary_line (remove_quoted_newline)
2065 int remove_quoted_newline;
2070 prompt_string_pointer = &ps2_prompt;
2071 if (SHOULD_PROMPT())
2073 ret = read_a_line (remove_quoted_newline);
2074 #if defined (HISTORY)
2075 if (ret && remember_on_history && (parser_state & PST_HEREDOC))
2077 /* To make adding the here-document body right, we need to rely on
2078 history_delimiting_chars() returning \n for the first line of the
2079 here-document body and the null string for the second and subsequent
2080 lines, so we avoid double newlines.
2081 current_command_line_count == 2 for the first line of the body. */
2083 current_command_line_count++;
2084 maybe_add_history (ret);
2086 #endif /* HISTORY */
2090 /* **************************************************************** */
2094 /* **************************************************************** */
2096 /* Reserved words. These are only recognized as the first word of a
2098 STRING_INT_ALIST word_token_alist[] = {
2107 #if defined (SELECT_COMMAND)
2108 { "select", SELECT },
2115 { "function", FUNCTION },
2116 #if defined (COMMAND_TIMING)
2122 #if defined (COND_COMMAND)
2123 { "[[", COND_START },
2126 #if defined (COPROCESS_SUPPORT)
2127 { "coproc", COPROC },
2132 /* other tokens that can be returned by read_token() */
2133 STRING_INT_ALIST other_token_alist[] = {
2134 /* Multiple-character tokens with special values */
2139 { ">>", GREATER_GREATER },
2140 { "<<", LESS_LESS },
2142 { ">&", GREATER_AND },
2143 { ";;", SEMI_SEMI },
2145 { ";;&", SEMI_SEMI_AND },
2146 { "<<-", LESS_LESS_MINUS },
2147 { "<<<", LESS_LESS_LESS },
2148 { "&>", AND_GREATER },
2149 { "&>>", AND_GREATER_GREATER },
2150 { "<>", LESS_GREATER },
2151 { ">|", GREATER_BAR },
2153 { "EOF", yacc_EOF },
2154 /* Tokens whose value is the character itself */
2165 { "newline", '\n' },
2169 /* others not listed here:
2170 WORD look at yylval.word
2171 ASSIGNMENT_WORD look at yylval.word
2172 NUMBER look at yylval.number
2173 ARITH_CMD look at yylval.word_list
2174 ARITH_FOR_EXPRS look at yylval.word_list
2175 COND_CMD look at yylval.command
2178 /* These are used by read_token_word, but appear up here so that shell_getc
2179 can use them to decide when to add otherwise blank lines to the history. */
2181 /* The primary delimiter stack. */
2182 struct dstack dstack = { (char *)NULL, 0, 0 };
2184 /* A temporary delimiter stack to be used when decoding prompt strings.
2185 This is needed because command substitutions in prompt strings (e.g., PS2)
2186 can screw up the parser's quoting state. */
2187 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
2189 /* Macro for accessing the top delimiter on the stack. Returns the
2190 delimiter or zero if none. */
2191 #define current_delimiter(ds) \
2192 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
2194 #define push_delimiter(ds, character) \
2197 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
2198 ds.delimiters = (char *)xrealloc \
2199 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
2200 ds.delimiters[ds.delimiter_depth] = character; \
2201 ds.delimiter_depth++; \
2205 #define pop_delimiter(ds) ds.delimiter_depth--
2207 /* Return the next shell input character. This always reads characters
2208 from shell_input_line; when that line is exhausted, it is time to
2209 read the next line. This is called by read_token when the shell is
2210 processing normal command input. */
2212 /* This implements one-character lookahead/lookbehind across physical input
2213 lines, to avoid something being lost because it's pushed back with
2214 shell_ungetc when we're at the start of a line. */
2215 static int eol_ungetc_lookahead = 0;
2218 shell_getc (remove_quoted_newline)
2219 int remove_quoted_newline;
2222 int c, truncating, last_was_backslash;
2227 last_was_backslash = 0;
2228 if (sigwinch_received)
2230 sigwinch_received = 0;
2231 get_new_window_size (0, (int *)0, (int *)0);
2234 if (eol_ungetc_lookahead)
2236 c = eol_ungetc_lookahead;
2237 eol_ungetc_lookahead = 0;
2241 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2242 /* If shell_input_line[shell_input_line_index] == 0, but there is
2243 something on the pushed list of strings, then we don't want to go
2244 off and get another line. We let the code down below handle it. */
2246 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
2247 (pushed_string_list == (STRING_SAVER *)NULL)))
2248 #else /* !ALIAS && !DPAREN_ARITHMETIC */
2249 if (!shell_input_line || !shell_input_line[shell_input_line_index])
2250 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
2254 /* Let's not let one really really long line blow up memory allocation */
2255 if (shell_input_line && shell_input_line_size >= 32768)
2257 free (shell_input_line);
2258 shell_input_line = 0;
2259 shell_input_line_size = 0;
2264 /* Allow immediate exit if interrupted during input. */
2268 shell_input_line_terminator = 0;
2270 /* If the shell is interatctive, but not currently printing a prompt
2271 (interactive_shell && interactive == 0), we don't want to print
2272 notifies or cleanup the jobs -- we want to defer it until we do
2273 print the next prompt. */
2274 if (interactive_shell == 0 || SHOULD_PROMPT())
2276 #if defined (JOB_CONTROL)
2277 /* This can cause a problem when reading a command as the result
2278 of a trap, when the trap is called from flush_child. This call
2279 had better not cause jobs to disappear from the job table in
2280 that case, or we will have big trouble. */
2281 notify_and_cleanup ();
2282 #else /* !JOB_CONTROL */
2283 cleanup_dead_jobs ();
2284 #endif /* !JOB_CONTROL */
2287 #if defined (READLINE)
2288 if (no_line_editing && SHOULD_PROMPT())
2290 if (SHOULD_PROMPT())
2294 if (bash_input.type == st_stream)
2301 /* Allow immediate exit if interrupted during input. */
2307 internal_warning ("shell_getc: ignored null byte in input");
2312 /* Theoretical overflow */
2313 /* If we can't put 256 bytes more into the buffer, allocate
2314 everything we can and fill it as full as we can. */
2315 /* XXX - we ignore rest of line using `truncating' flag */
2316 if (shell_input_line_size > (SIZE_MAX - 256))
2320 n = SIZE_MAX - i; /* how much more can we put into the buffer? */
2321 if (n <= 2) /* we have to save 1 for the newline added below */
2323 if (truncating == 0)
2324 internal_warning(_("shell_getc: shell_input_line_size (%zu) exceeds SIZE_MAX (%lu): line truncated"), shell_input_line_size, (unsigned long)SIZE_MAX);
2325 shell_input_line[i] = '\0';
2328 if (shell_input_line_size < SIZE_MAX)
2330 shell_input_line_size = SIZE_MAX;
2331 shell_input_line = xrealloc (shell_input_line, shell_input_line_size);
2335 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
2339 if (bash_input.type == st_stream)
2343 shell_input_line_terminator = EOF;
2345 shell_input_line[i] = '\0';
2349 if (truncating == 0 || c == '\n')
2350 shell_input_line[i++] = c;
2354 shell_input_line[--i] = '\0';
2355 current_command_line_count++;
2359 last_was_backslash = last_was_backslash == 0 && c == '\\';
2362 shell_input_line_index = 0;
2363 shell_input_line_len = i; /* == strlen (shell_input_line) */
2365 set_line_mbstate ();
2367 #if defined (HISTORY)
2368 if (remember_on_history && shell_input_line && shell_input_line[0])
2371 # if defined (BANG_HISTORY)
2374 /* If the current delimiter is a single quote, we should not be
2375 performing history expansion, even if we're on a different
2376 line from the original single quote. */
2377 old_hist = history_expansion_inhibited;
2378 if (current_delimiter (dstack) == '\'')
2379 history_expansion_inhibited = 1;
2381 /* Calling with a third argument of 1 allows remember_on_history to
2382 determine whether or not the line is saved to the history list */
2383 expansions = pre_process_line (shell_input_line, 1, 1);
2384 # if defined (BANG_HISTORY)
2385 history_expansion_inhibited = old_hist;
2387 if (expansions != shell_input_line)
2389 free (shell_input_line);
2390 shell_input_line = expansions;
2391 shell_input_line_len = shell_input_line ?
2392 strlen (shell_input_line) : 0;
2393 if (shell_input_line_len == 0)
2394 current_command_line_count--;
2396 /* We have to force the xrealloc below because we don't know
2397 the true allocated size of shell_input_line anymore. */
2398 shell_input_line_size = shell_input_line_len;
2400 set_line_mbstate ();
2403 /* Try to do something intelligent with blank lines encountered while
2404 entering multi-line commands. XXX - this is grotesque */
2405 else if (remember_on_history && shell_input_line &&
2406 shell_input_line[0] == '\0' &&
2407 current_command_line_count > 1)
2409 if (current_delimiter (dstack))
2410 /* We know shell_input_line[0] == 0 and we're reading some sort of
2411 quoted string. This means we've got a line consisting of only
2412 a newline in a quoted string. We want to make sure this line
2413 gets added to the history. */
2414 maybe_add_history (shell_input_line);
2418 hdcs = history_delimiting_chars (shell_input_line);
2419 if (hdcs && hdcs[0] == ';')
2420 maybe_add_history (shell_input_line);
2424 #endif /* HISTORY */
2426 if (shell_input_line)
2428 /* Lines that signify the end of the shell's input should not be
2429 echoed. We should not echo lines while parsing command
2430 substitutions with recursive calls into the parsing engine; those
2431 should only be echoed once when we read the word. That is the
2432 reason for the test against shell_eof_token, which is set to a
2433 right paren when parsing the contents of command substitutions. */
2434 if (echo_input_at_read && (shell_input_line[0] ||
2435 shell_input_line_terminator != EOF) &&
2436 shell_eof_token == 0)
2437 fprintf (stderr, "%s\n", shell_input_line);
2441 shell_input_line_size = 0;
2442 prompt_string_pointer = ¤t_prompt_string;
2443 if (SHOULD_PROMPT ())
2448 /* Add the newline to the end of this string, iff the string does
2449 not already end in an EOF character. */
2450 if (shell_input_line_terminator != EOF)
2452 if (shell_input_line_size < SIZE_MAX-3 && (shell_input_line_len+3 > shell_input_line_size))
2453 shell_input_line = (char *)xrealloc (shell_input_line,
2454 1 + (shell_input_line_size += 2));
2456 /* Don't add a newline to a string that ends with a backslash if we're
2457 going to be removing quoted newlines, since that will eat the
2458 backslash. Add another backslash instead (will be removed by
2460 if (bash_input.type == st_string && expanding_alias() == 0 && last_was_backslash && c == EOF && remove_quoted_newline)
2461 shell_input_line[shell_input_line_len] = '\\';
2463 shell_input_line[shell_input_line_len] = '\n';
2464 shell_input_line[shell_input_line_len + 1] = '\0';
2466 set_line_mbstate ();
2471 uc = shell_input_line[shell_input_line_index];
2474 shell_input_line_index++;
2476 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2477 /* If UC is NULL, we have reached the end of the current input string. If
2478 pushed_string_list is non-empty, it's time to pop to the previous string
2479 because we have fully consumed the result of the last alias expansion.
2480 Do it transparently; just return the next character of the string popped
2482 /* If pushed_string_list != 0 but pushed_string_list->expander == 0 (not
2483 currently tested) and the flags value is not PSH_SOURCE, we are not
2484 parsing an alias, we have just saved one (push_string, when called by
2485 the parse_dparen code) In this case, just go on as well. The PSH_SOURCE
2486 case is handled below. */
2488 if (uc == 0 && pushed_string_list && pushed_string_list->flags != PSH_SOURCE)
2491 uc = shell_input_line[shell_input_line_index];
2493 shell_input_line_index++;
2495 #endif /* ALIAS || DPAREN_ARITHMETIC */
2497 if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n')
2499 if (SHOULD_PROMPT ())
2502 /* What do we do here if we're expanding an alias whose definition
2503 includes an escaped newline? If that's the last character in the
2504 alias expansion, we just pop the pushed string list (recall that
2505 we inhibit the appending of a space in mk_alexpansion() if newline
2506 is the last character). If it's not the last character, we need
2507 to consume the quoted newline and move to the next character in
2510 if (expanding_alias () && shell_input_line[shell_input_line_index+1] == '\0')
2515 else if (expanding_alias () && shell_input_line[shell_input_line_index+1] != '\0')
2517 shell_input_line_index++; /* skip newline */
2518 goto next_alias_char; /* and get next character */
2525 if (uc == 0 && shell_input_line_terminator == EOF)
2526 return ((shell_input_line_index != 0) ? '\n' : EOF);
2528 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2529 /* We already know that we are not parsing an alias expansion because of the
2530 check for expanding_alias() above. This knows how parse_and_execute
2531 handles switching to st_string input while an alias is being expanded,
2532 hence the check for pushed_string_list without pushed_string_list->expander
2533 and the check for PSH_SOURCE as pushed_string_list->flags.
2534 parse_and_execute and parse_string both change the input type to st_string
2535 and place the string to be parsed and executed into location.string, so
2536 we should not stop reading that until the pointer is '\0'.
2537 The check for shell_input_line_terminator may be superfluous.
2539 This solves the problem of `.' inside a multi-line alias with embedded
2540 newlines executing things out of order. */
2541 if (uc == 0 && bash_input.type == st_string && *bash_input.location.string &&
2542 pushed_string_list && pushed_string_list->flags == PSH_SOURCE &&
2543 shell_input_line_terminator == 0)
2545 shell_input_line_index = 0;
2553 /* Put C back into the input for the shell. This might need changes for
2554 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
2555 character different than we read, shell_input_line_property doesn't need
2556 to change when manipulating shell_input_line. The define for
2557 last_shell_getc_is_singlebyte should take care of it, though. */
2562 if (shell_input_line && shell_input_line_index)
2563 shell_input_line[--shell_input_line_index] = c;
2565 eol_ungetc_lookahead = c;
2569 parser_remaining_input ()
2571 if (shell_input_line == 0)
2573 if (shell_input_line_index < 0 || shell_input_line_index >= shell_input_line_len)
2574 return ""; /* XXX */
2575 return (shell_input_line + shell_input_line_index);
2578 #ifdef INCLUDE_UNUSED
2579 /* Back the input pointer up by one, effectively `ungetting' a character. */
2583 if (shell_input_line && shell_input_line_index)
2584 shell_input_line_index--;
2588 /* Discard input until CHARACTER is seen, then push that character back
2589 onto the input stream. */
2591 discard_until (character)
2596 while ((c = shell_getc (0)) != EOF && c != character)
2604 execute_variable_command (command, vname)
2605 char *command, *vname;
2608 sh_parser_state_t ps;
2610 save_parser_state (&ps);
2611 last_lastarg = get_string_value ("_");
2613 last_lastarg = savestring (last_lastarg);
2615 parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST);
2617 restore_parser_state (&ps);
2618 bind_variable ("_", last_lastarg, 0);
2619 FREE (last_lastarg);
2621 if (token_to_read == '\n') /* reset_parser was called */
2629 two_tokens_ago = token_before_that;
2630 token_before_that = last_read_token;
2631 last_read_token = current_token;
2636 /* Place to remember the token. We try to keep the buffer
2637 at a reasonable size, but it can grow. */
2638 static char *token = (char *)NULL;
2640 /* Current size of the token buffer. */
2641 static int token_buffer_size;
2643 /* Command to read_token () explaining what we want it to do. */
2646 #define prompt_is_ps1 \
2647 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
2649 /* Function for yyparse to call. yylex keeps track of
2650 the last two tokens read, and calls read_token. */
2654 if (interactive && (current_token == 0 || current_token == '\n'))
2656 /* Before we print a prompt, we might have to check mailboxes.
2657 We do this only if it is time to do so. Notice that only here
2658 is the mail alarm reset; nothing takes place in check_mail ()
2659 except the checking of mail. Please don't change this. */
2660 if (prompt_is_ps1 && parse_and_execute_level == 0 && time_to_check_mail ())
2663 reset_mail_timer ();
2666 /* Avoid printing a prompt if we're not going to read anything, e.g.
2667 after resetting the parser with read_token (RESET). */
2668 if (token_to_read == 0 && SHOULD_PROMPT ())
2672 two_tokens_ago = token_before_that;
2673 token_before_that = last_read_token;
2674 last_read_token = current_token;
2675 current_token = read_token (READ);
2677 if ((parser_state & PST_EOFTOKEN) && current_token == shell_eof_token)
2679 current_token = yacc_EOF;
2680 if (bash_input.type == st_string)
2681 rewind_input_string ();
2683 parser_state &= ~PST_EOFTOKEN;
2685 return (current_token);
2688 /* When non-zero, we have read the required tokens
2689 which allow ESAC to be the next one read. */
2690 static int esacs_needed_count;
2696 if (need_here_doc >= HEREDOC_MAX)
2698 last_command_exit_value = EX_BADUSAGE;
2700 report_syntax_error (_("maximum here-document count exceeded"));
2702 exit_shell (last_command_exit_value);
2704 redir_stack[need_here_doc++] = r;
2708 gather_here_documents ()
2713 here_doc_first_line = 1;
2714 while (need_here_doc > 0)
2716 parser_state |= PST_HEREDOC;
2717 make_here_document (redir_stack[r++], line_number);
2718 parser_state &= ~PST_HEREDOC;
2720 redir_stack[r - 1] = 0; /* XXX */
2722 here_doc_first_line = 0; /* just in case */
2725 /* When non-zero, an open-brace used to create a group is awaiting a close
2727 static int open_brace_count;
2729 /* In the following three macros, `token' is always last_read_token */
2731 /* Are we in the middle of parsing a redirection where we are about to read
2732 a word? This is used to make sure alias expansion doesn't happen in the
2733 middle of a redirection, even though we're parsing a simple command. */
2734 #define parsing_redirection(token) \
2735 (token == '<' || token == '>' || \
2736 token == GREATER_GREATER || token == GREATER_BAR || \
2737 token == LESS_GREATER || token == LESS_LESS_MINUS || \
2738 token == LESS_LESS || token == LESS_LESS_LESS || \
2739 token == LESS_AND || token == GREATER_AND || token == AND_GREATER)
2741 /* Is `token' one that will allow a WORD to be read in a command position?
2742 We can read a simple command name on which we should attempt alias expansion
2743 or we can read an assignment statement. */
2744 #define command_token_position(token) \
2745 (((token) == ASSIGNMENT_WORD) || \
2746 ((parser_state&PST_REDIRLIST) && parsing_redirection(token) == 0) || \
2747 ((token) != SEMI_SEMI && (token) != SEMI_AND && (token) != SEMI_SEMI_AND && reserved_word_acceptable(token)))
2749 /* Are we in a position where we can read an assignment statement? */
2750 #define assignment_acceptable(token) \
2751 (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0))
2753 /* Check to see if TOKEN is a reserved word and return the token
2755 #define CHECK_FOR_RESERVED_WORD(tok) \
2757 if (!dollar_present && !quoted && \
2758 reserved_word_acceptable (last_read_token)) \
2761 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
2762 if (STREQ (tok, word_token_alist[i].word)) \
2764 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
2766 if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \
2768 if ((parser_state & PST_CASEPAT) && last_read_token == '|' && word_token_alist[i].token == ESAC) \
2769 break; /* Posix grammar rule 4 */ \
2770 if (word_token_alist[i].token == ESAC) \
2771 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
2772 else if (word_token_alist[i].token == CASE) \
2773 parser_state |= PST_CASESTMT; \
2774 else if (word_token_alist[i].token == COND_END) \
2775 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
2776 else if (word_token_alist[i].token == COND_START) \
2777 parser_state |= PST_CONDCMD; \
2778 else if (word_token_alist[i].token == '{') \
2779 open_brace_count++; \
2780 else if (word_token_alist[i].token == '}' && open_brace_count) \
2781 open_brace_count--; \
2782 return (word_token_alist[i].token); \
2789 /* OK, we have a token. Let's try to alias expand it, if (and only if)
2792 It is eligible for expansion if EXPAND_ALIASES is set, and
2793 the token is unquoted and the last token read was a command
2794 separator (or expand_next_token is set), and we are currently
2795 processing an alias (pushed_string_list is non-empty) and this
2796 token is not the same as the current or any previously
2799 Special cases that disqualify:
2800 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
2810 r = xmalloc (l + 2);
2812 /* If the last character in the alias is a newline, don't add a trailing
2813 space to the expansion. Works with shell_getc above. */
2814 if (r[l - 1] != ' ' && r[l - 1] != '\n' && shellmeta(r[l - 1]) == 0)
2821 alias_expand_token (tokstr)
2827 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
2828 (parser_state & PST_CASEPAT) == 0)
2830 ap = find_alias (tokstr);
2832 /* Currently expanding this token. */
2833 if (ap && (ap->flags & AL_BEINGEXPANDED))
2834 return (NO_EXPANSION);
2836 /* mk_alexpansion puts an extra space on the end of the alias expansion,
2837 so the lookahead by the parser works right. If this gets changed,
2838 make sure the code in shell_getc that deals with reaching the end of
2839 an expanded alias is changed with it. */
2840 expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL;
2844 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
2845 return (RE_READ_TOKEN);
2848 /* This is an eligible token that does not have an expansion. */
2849 return (NO_EXPANSION);
2851 return (NO_EXPANSION);
2856 time_command_acceptable ()
2858 #if defined (COMMAND_TIMING)
2861 if (posixly_correct && shell_compatibility_level > 41)
2863 /* Quick check of the rest of the line to find the next token. If it
2864 begins with a `-', Posix says to not return `time' as the token.
2865 This was interp 267. */
2866 i = shell_input_line_index;
2867 while (i < shell_input_line_len && (shell_input_line[i] == ' ' || shell_input_line[i] == '\t'))
2869 if (shell_input_line[i] == '-')
2873 switch (last_read_token)
2878 if (token_before_that == '|')
2893 case ')': /* only valid in case statement */
2894 case BANG: /* ! time pipeline */
2895 case TIME: /* time time pipeline */
2896 case TIMEOPT: /* time -p time pipeline */
2897 case TIMEIGN: /* time -p -- ... */
2904 #endif /* COMMAND_TIMING */
2907 /* Handle special cases of token recognition:
2908 IN is recognized if the last token was WORD and the token
2909 before that was FOR or CASE or SELECT.
2911 DO is recognized if the last token was WORD and the token
2912 before that was FOR or SELECT.
2914 ESAC is recognized if the last token caused `esacs_needed_count'
2917 `{' is recognized if the last token as WORD and the token
2918 before that was FUNCTION, or if we just parsed an arithmetic
2921 `}' is recognized if there is an unclosed `{' present.
2923 `-p' is returned as TIMEOPT if the last read token was TIME.
2924 `--' is returned as TIMEIGN if the last read token was TIMEOPT.
2926 ']]' is returned as COND_END if the parser is currently parsing
2927 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
2929 `time' is returned as TIME if and only if it is immediately
2930 preceded by one of `;', `\n', `||', `&&', or `&'.
2934 special_case_tokens (tokstr)
2937 if ((last_read_token == WORD) &&
2938 #if defined (SELECT_COMMAND)
2939 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
2941 ((token_before_that == FOR) || (token_before_that == CASE)) &&
2943 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
2945 if (token_before_that == CASE)
2947 parser_state |= PST_CASEPAT;
2948 esacs_needed_count++;
2953 if (last_read_token == WORD &&
2954 #if defined (SELECT_COMMAND)
2955 (token_before_that == FOR || token_before_that == SELECT) &&
2957 (token_before_that == FOR) &&
2959 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
2962 /* Ditto for ESAC in the CASE case.
2963 Specifically, this handles "case word in esac", which is a legal
2964 construct, certainly because someone will pass an empty arg to the
2965 case construct, and we don't want it to barf. Of course, we should
2966 insist that the case construct has at least one pattern in it, but
2967 the designers disagree. */
2968 if (esacs_needed_count)
2970 esacs_needed_count--;
2971 if (STREQ (tokstr, "esac"))
2973 parser_state &= ~PST_CASEPAT;
2978 /* The start of a shell function definition. */
2979 if (parser_state & PST_ALLOWOPNBRC)
2981 parser_state &= ~PST_ALLOWOPNBRC;
2982 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2985 function_bstart = line_number;
2986 return ('{'); /* } */
2990 /* We allow a `do' after a for ((...)) without an intervening
2992 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
2994 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2997 return ('{'); /* } */
3000 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
3002 open_brace_count--; /* { */
3006 #if defined (COMMAND_TIMING)
3007 /* Handle -p after `time'. */
3008 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
3010 /* Handle -- after `time -p'. */
3011 if (last_read_token == TIMEOPT && tokstr[0] == '-' && tokstr[1] == '-' && !tokstr[2])
3015 #if defined (COND_COMMAND) /* [[ */
3016 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
3023 /* Called from shell.c when Control-C is typed at top level. Or
3024 by the error rule at top level. */
3028 dstack.delimiter_depth = 0; /* No delimiters found so far. */
3029 open_brace_count = 0;
3031 #if defined (EXTENDED_GLOB)
3032 /* Reset to global value of extended glob */
3033 if (parser_state & PST_EXTPAT)
3034 extended_glob = global_extglob;
3038 here_doc_first_line = 0;
3040 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
3041 if (pushed_string_list)
3042 free_string_list ();
3043 #endif /* ALIAS || DPAREN_ARITHMETIC */
3045 /* This is where we resynchronize to the next newline on error/reset */
3046 if (shell_input_line)
3048 free (shell_input_line);
3049 shell_input_line = (char *)NULL;
3050 shell_input_line_size = shell_input_line_index = 0;
3053 FREE (word_desc_to_read);
3054 word_desc_to_read = (WORD_DESC *)NULL;
3056 eol_ungetc_lookahead = 0;
3058 current_token = '\n'; /* XXX */
3059 last_read_token = '\n';
3060 token_to_read = '\n';
3063 /* Read the next token. Command can be READ (normal operation) or
3064 RESET (to normalize state). */
3066 read_token (command)
3069 int character; /* Current character. */
3070 int peek_char; /* Temporary look-ahead character. */
3071 int result; /* The thing to return. */
3073 if (command == RESET)
3081 result = token_to_read;
3082 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
3084 yylval.word = word_desc_to_read;
3085 word_desc_to_read = (WORD_DESC *)NULL;
3091 #if defined (COND_COMMAND)
3092 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
3094 cond_lineno = line_number;
3095 parser_state |= PST_CONDEXPR;
3096 yylval.command = parse_cond_command ();
3097 if (cond_token != COND_END)
3102 token_to_read = COND_END;
3103 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
3109 /* This is a place to jump back to once we have successfully expanded a
3110 token with an alias and pushed the string with push_string () */
3114 /* Read a single word from input. Start by skipping blanks. */
3115 while ((character = shell_getc (1)) != EOF && shellblank (character))
3118 if (character == EOF)
3124 if MBTEST(character == '#' && (!interactive || interactive_comments))
3126 /* A comment. Discard until EOL or EOF, and then return a newline. */
3127 discard_until ('\n');
3129 character = '\n'; /* this will take the next if statement and return. */
3132 if (character == '\n')
3134 /* If we're about to return an unquoted newline, we can go and collect
3135 the text of any pending here document. */
3137 gather_here_documents ();
3140 parser_state &= ~PST_ALEXPNEXT;
3143 parser_state &= ~PST_ASSIGNOK;
3148 if (parser_state & PST_REGEXP)
3151 /* Shell meta-characters. */
3152 if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
3155 /* Turn off alias tokenization iff this character sequence would
3156 not leave us ready to read a command. */
3157 if (character == '<' || character == '>')
3158 parser_state &= ~PST_ALEXPNEXT;
3161 parser_state &= ~PST_ASSIGNOK;
3163 /* If we are parsing a command substitution and we have read a character
3164 that marks the end of it, don't bother to skip over quoted newlines
3165 when we read the next token. We're just interested in a character
3166 that will turn this into a two-character token, so we let the higher
3167 layers deal with quoted newlines following the command substitution. */
3168 if ((parser_state & PST_CMDSUBST) && character == shell_eof_token)
3169 peek_char = shell_getc (0);
3171 peek_char = shell_getc (1);
3173 if (character == peek_char)
3178 /* If '<' then we could be at "<<" or at "<<-". We have to
3179 look ahead one more character. */
3180 peek_char = shell_getc (1);
3181 if MBTEST(peek_char == '-')
3182 return (LESS_LESS_MINUS);
3183 else if MBTEST(peek_char == '<')
3184 return (LESS_LESS_LESS);
3187 shell_ungetc (peek_char);
3192 return (GREATER_GREATER);
3195 parser_state |= PST_CASEPAT;
3197 parser_state &= ~PST_ALEXPNEXT;
3200 peek_char = shell_getc (1);
3201 if MBTEST(peek_char == '&')
3202 return (SEMI_SEMI_AND);
3205 shell_ungetc (peek_char);
3215 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
3217 result = parse_dparen (character);
3225 else if MBTEST(character == '<' && peek_char == '&')
3227 else if MBTEST(character == '>' && peek_char == '&')
3228 return (GREATER_AND);
3229 else if MBTEST(character == '<' && peek_char == '>')
3230 return (LESS_GREATER);
3231 else if MBTEST(character == '>' && peek_char == '|')
3232 return (GREATER_BAR);
3233 else if MBTEST(character == '&' && peek_char == '>')
3235 peek_char = shell_getc (1);
3236 if MBTEST(peek_char == '>')
3237 return (AND_GREATER_GREATER);
3240 shell_ungetc (peek_char);
3241 return (AND_GREATER);
3244 else if MBTEST(character == '|' && peek_char == '&')
3246 else if MBTEST(character == ';' && peek_char == '&')
3248 parser_state |= PST_CASEPAT;
3250 parser_state &= ~PST_ALEXPNEXT;
3255 shell_ungetc (peek_char);
3257 /* If we look like we are reading the start of a function
3258 definition, then let the reader know about it so that
3259 we will do the right thing with `{'. */
3260 if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
3262 parser_state |= PST_ALLOWOPNBRC;
3264 parser_state &= ~PST_ALEXPNEXT;
3266 function_dstart = line_number;
3269 /* case pattern lists may be preceded by an optional left paren. If
3270 we're not trying to parse a case pattern list, the left paren
3271 indicates a subshell. */
3272 if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
3273 parser_state |= PST_SUBSHELL;
3275 else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
3276 parser_state &= ~PST_CASEPAT;
3278 else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
3279 parser_state &= ~PST_SUBSHELL;
3281 #if defined (PROCESS_SUBSTITUTION)
3282 /* Check for the constructs which introduce process substitution.
3283 Shells running in `posix mode' don't do process substitution. */
3284 if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/
3285 #endif /* PROCESS_SUBSTITUTION */
3289 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
3290 if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
3294 /* Okay, if we got this far, we have to read a word. Read one,
3295 and then check it against the known ones. */
3296 result = read_token_word (character);
3298 if (result == RE_READ_TOKEN)
3305 * Match a $(...) or other grouping construct. This has to handle embedded
3306 * quoted strings ('', ``, "") and nested constructs. It also must handle
3307 * reprompting the user, if necessary, after reading a newline, and returning
3308 * correct error values if it reads EOF.
3310 #define P_FIRSTCLOSE 0x0001
3311 #define P_ALLOWESC 0x0002
3312 #define P_DQUOTE 0x0004
3313 #define P_COMMAND 0x0008 /* parsing a command, so look for comments */
3314 #define P_BACKQUOTE 0x0010 /* parsing a backquoted command substitution */
3315 #define P_ARRAYSUB 0x0020 /* parsing a [...] array subscript for assignment */
3316 #define P_DOLBRACE 0x0040 /* parsing a ${...} construct */
3318 /* Lexical state while parsing a grouping construct or $(...). */
3319 #define LEX_WASDOL 0x001
3320 #define LEX_CKCOMMENT 0x002
3321 #define LEX_INCOMMENT 0x004
3322 #define LEX_PASSNEXT 0x008
3323 #define LEX_RESWDOK 0x010
3324 #define LEX_CKCASE 0x020
3325 #define LEX_INCASE 0x040
3326 #define LEX_INHEREDOC 0x080
3327 #define LEX_HEREDELIM 0x100 /* reading here-doc delimiter */
3328 #define LEX_STRIPDOC 0x200 /* <<- strip tabs from here doc delim */
3329 #define LEX_QUOTEDDOC 0x400 /* here doc with quoted delim */
3330 #define LEX_INWORD 0x800
3332 #define COMSUB_META(ch) ((ch) == ';' || (ch) == '&' || (ch) == '|')
3334 #define CHECK_NESTRET_ERROR() \
3336 if (nestret == &matched_pair_error) \
3339 return &matched_pair_error; \
3343 #define APPEND_NESTRET() \
3347 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); \
3348 strcpy (ret + retind, nestret); \
3349 retind += nestlen; \
3353 static char matched_pair_error;
3356 parse_matched_pair (qc, open, close, lenp, flags)
3357 int qc; /* `"' if this construct is within double quotes */
3361 int count, ch, tflags;
3362 int nestlen, ttranslen, start_lineno;
3363 char *ret, *nestret, *ttrans;
3364 int retind, retsize, rflags;
3367 dolbrace_state = (flags & P_DOLBRACE) ? DOLBRACE_PARAM : 0;
3369 /*itrace("parse_matched_pair[%d]: open = %c close = %c flags = %d", line_number, open, close, flags);*/
3373 if ((flags & P_COMMAND) && qc != '`' && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
3374 tflags |= LEX_CKCOMMENT;
3376 /* RFLAGS is the set of flags we want to pass to recursive calls. */
3377 rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE);
3379 ret = (char *)xmalloc (retsize = 64);
3382 start_lineno = line_number;
3385 ch = shell_getc (qc != '\'' && (tflags & (LEX_PASSNEXT)) == 0);
3390 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
3391 EOF_Reached = 1; /* XXX */
3392 return (&matched_pair_error);
3395 /* Possible reprompting. */
3396 if (ch == '\n' && SHOULD_PROMPT ())
3399 /* Don't bother counting parens or doing anything else if in a comment
3400 or part of a case statement */
3401 if (tflags & LEX_INCOMMENT)
3403 /* Add this character. */
3404 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3408 tflags &= ~LEX_INCOMMENT;
3413 /* Not exactly right yet, should handle shell metacharacters, too. If
3414 any changes are made to this test, make analogous changes to subst.c:
3415 extract_delimited_string(). */
3416 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (retind == 0 || ret[retind-1] == '\n' || shellblank (ret[retind - 1])))
3417 tflags |= LEX_INCOMMENT;
3419 if (tflags & LEX_PASSNEXT) /* last char was backslash */
3421 tflags &= ~LEX_PASSNEXT;
3422 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
3425 retind--; /* swallow previously-added backslash */
3429 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3430 if MBTEST(ch == CTLESC)
3431 ret[retind++] = CTLESC;
3435 /* If we're reparsing the input (e.g., from parse_string_to_word_list),
3436 we've already prepended CTLESC to single-quoted results of $'...'.
3437 We may want to do this for other CTLESC-quoted characters in
3439 else if MBTEST((parser_state & PST_REPARSE) && open == '\'' && (ch == CTLESC || ch == CTLNUL))
3441 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3445 else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
3447 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3448 ret[retind++] = CTLESC;
3452 else if MBTEST(ch == close) /* ending delimiter */
3454 /* handle nested ${...} specially. */
3455 else if MBTEST(open != close && (tflags & LEX_WASDOL) && open == '{' && ch == open) /* } */
3457 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
3460 /* Add this character. */
3461 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3464 /* If we just read the ending character, don't bother continuing. */
3468 if (open == '\'') /* '' inside grouping construct */
3470 if MBTEST((flags & P_ALLOWESC) && ch == '\\')
3471 tflags |= LEX_PASSNEXT;
3475 if MBTEST(ch == '\\') /* backslashes */
3476 tflags |= LEX_PASSNEXT;
3478 /* Based on which dolstate is currently in (param, op, or word),
3479 decide what the op is. We're really only concerned if it's % or
3480 #, so we can turn on a flag that says whether or not we should
3481 treat single quotes as special when inside a double-quoted
3482 ${...}. This logic must agree with subst.c:extract_dollar_brace_string
3483 since they share the same defines. */
3484 /* FLAG POSIX INTERP 221 */
3485 if (flags & P_DOLBRACE)
3487 /* ${param%[%]word} */
3488 if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '%' && retind > 1)
3489 dolbrace_state = DOLBRACE_QUOTE;
3490 /* ${param#[#]word} */
3491 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '#' && retind > 1)
3492 dolbrace_state = DOLBRACE_QUOTE;
3493 /* ${param/[/]pat/rep} */
3494 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '/' && retind > 1)
3495 dolbrace_state = DOLBRACE_QUOTE2; /* XXX */
3496 /* ${param^[^]pat} */
3497 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '^' && retind > 1)
3498 dolbrace_state = DOLBRACE_QUOTE;
3499 /* ${param,[,]pat} */
3500 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == ',' && retind > 1)
3501 dolbrace_state = DOLBRACE_QUOTE;
3502 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", ch) != 0)
3503 dolbrace_state = DOLBRACE_OP;
3504 else if MBTEST(dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", ch) == 0)
3505 dolbrace_state = DOLBRACE_WORD;
3508 /* The big hammer. Single quotes aren't special in double quotes. The
3509 problem is that Posix used to say the single quotes are semi-special:
3510 within a double-quoted ${...} construct "an even number of
3511 unescaped double-quotes or single-quotes, if any, shall occur." */
3512 /* This was changed in Austin Group Interp 221 */
3513 if MBTEST(posixly_correct && shell_compatibility_level > 41 && dolbrace_state != DOLBRACE_QUOTE && dolbrace_state != DOLBRACE_QUOTE2 && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'')
3516 /* Could also check open == '`' if we want to parse grouping constructs
3517 inside old-style command substitution. */
3518 if (open != close) /* a grouping construct */
3520 if MBTEST(shellquote (ch))
3522 /* '', ``, or "" inside $(...) or other grouping construct. */
3523 push_delimiter (dstack, ch);
3524 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
3525 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
3527 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
3528 pop_delimiter (dstack);
3529 CHECK_NESTRET_ERROR ();
3531 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
3533 /* Translate $'...' here. */
3534 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
3537 /* If we're parsing a double-quoted brace expansion and we are
3538 not in a place where single quotes are treated specially,
3539 make sure we single-quote the results of the ansi
3540 expansion because quote removal should remove them later */
3541 /* FLAG POSIX INTERP 221 */
3542 if ((shell_compatibility_level > 42) && (rflags & P_DQUOTE) && (dolbrace_state == DOLBRACE_QUOTE2) && (flags & P_DOLBRACE))
3544 nestret = sh_single_quote (ttrans);
3546 nestlen = strlen (nestret);
3548 else if ((rflags & P_DQUOTE) == 0)
3550 nestret = sh_single_quote (ttrans);
3552 nestlen = strlen (nestret);
3557 nestlen = ttranslen;
3559 retind -= 2; /* back up before the $' */
3561 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
3563 /* Locale expand $"..." here. */
3564 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
3567 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
3569 nestlen = ttranslen + 2;
3570 retind -= 2; /* back up before the $" */
3576 else if ((flags & (P_ARRAYSUB|P_DOLBRACE)) && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3577 goto parse_dollar_word;
3579 /* Parse an old-style command substitution within double quotes as a
3581 /* XXX - sh and ksh93 don't do this - XXX */
3582 else if MBTEST(open == '"' && ch == '`')
3584 nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags);
3586 CHECK_NESTRET_ERROR ();
3591 else if MBTEST(open != '`' && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3592 /* check for $(), $[], or ${} inside quoted string. */
3595 if (open == ch) /* undo previous increment */
3597 if (ch == '(') /* ) */
3598 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
3599 else if (ch == '{') /* } */
3600 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags);
3601 else if (ch == '[') /* ] */
3602 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
3604 CHECK_NESTRET_ERROR ();
3609 if MBTEST(ch == '$')
3610 tflags |= LEX_WASDOL;
3612 tflags &= ~LEX_WASDOL;
3618 /*itrace("parse_matched_pair[%d]: returning %s", line_number, ret);*/
3630 fprintf (stderr, "%d -> ", f);
3634 fprintf (stderr, "LEX_WASDOL%s", f ? "|" : "");
3636 if (f & LEX_CKCOMMENT)
3638 f &= ~LEX_CKCOMMENT;
3639 fprintf (stderr, "LEX_CKCOMMENT%s", f ? "|" : "");
3641 if (f & LEX_INCOMMENT)
3643 f &= ~LEX_INCOMMENT;
3644 fprintf (stderr, "LEX_INCOMMENT%s", f ? "|" : "");
3646 if (f & LEX_PASSNEXT)
3649 fprintf (stderr, "LEX_PASSNEXT%s", f ? "|" : "");
3651 if (f & LEX_RESWDOK)
3654 fprintf (stderr, "LEX_RESWDOK%s", f ? "|" : "");
3659 fprintf (stderr, "LEX_CKCASE%s", f ? "|" : "");
3664 fprintf (stderr, "LEX_INCASE%s", f ? "|" : "");
3666 if (f & LEX_INHEREDOC)
3668 f &= ~LEX_INHEREDOC;
3669 fprintf (stderr, "LEX_INHEREDOC%s", f ? "|" : "");
3671 if (f & LEX_HEREDELIM)
3673 f &= ~LEX_HEREDELIM;
3674 fprintf (stderr, "LEX_HEREDELIM%s", f ? "|" : "");
3676 if (f & LEX_STRIPDOC)
3679 fprintf (stderr, "LEX_WASDOL%s", f ? "|" : "");
3681 if (f & LEX_QUOTEDDOC)
3683 f &= ~LEX_QUOTEDDOC;
3684 fprintf (stderr, "LEX_QUOTEDDOC%s", f ? "|" : "");
3689 fprintf (stderr, "LEX_INWORD%s", f ? "|" : "");
3692 fprintf (stderr, "\n");
3697 /* Parse a $(...) command substitution. This is messier than I'd like, and
3698 reproduces a lot more of the token-reading code than I'd like. */
3700 parse_comsub (qc, open, close, lenp, flags)
3701 int qc; /* `"' if this construct is within double quotes */
3705 int count, ch, peekc, tflags, lex_rwlen, lex_wlen, lex_firstind;
3706 int nestlen, ttranslen, start_lineno;
3707 char *ret, *nestret, *ttrans, *heredelim;
3708 int retind, retsize, rflags, hdlen;
3710 /* Posix interp 217 says arithmetic expressions have precedence, so
3711 assume $(( introduces arithmetic expansion and parse accordingly. */
3712 peekc = shell_getc (0);
3713 shell_ungetc (peekc);
3715 return (parse_matched_pair (qc, open, close, lenp, 0));
3717 /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/
3719 tflags = LEX_RESWDOK;
3721 if ((flags & P_COMMAND) && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
3722 tflags |= LEX_CKCASE;
3723 if ((tflags & LEX_CKCASE) && (interactive == 0 || interactive_comments))
3724 tflags |= LEX_CKCOMMENT;
3726 /* RFLAGS is the set of flags we want to pass to recursive calls. */
3727 rflags = (flags & P_DQUOTE);
3729 ret = (char *)xmalloc (retsize = 64);
3732 start_lineno = line_number;
3733 lex_rwlen = lex_wlen = 0;
3741 ch = shell_getc (qc != '\'' && (tflags & (LEX_INCOMMENT|LEX_PASSNEXT)) == 0);
3748 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
3749 EOF_Reached = 1; /* XXX */
3750 return (&matched_pair_error);
3753 /* If we hit the end of a line and are reading the contents of a here
3754 document, and it's not the same line that the document starts on,
3755 check for this line being the here doc delimiter. Otherwise, if
3756 we're in a here document, mark the next character as the beginning
3760 if ((tflags & LEX_HEREDELIM) && heredelim)
3762 tflags &= ~LEX_HEREDELIM;
3763 tflags |= LEX_INHEREDOC;
3764 lex_firstind = retind + 1;
3766 else if (tflags & LEX_INHEREDOC)
3769 tind = lex_firstind;
3770 while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t')
3772 if (STREQN (ret + tind, heredelim, hdlen))
3774 tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC|LEX_QUOTEDDOC);
3775 /*itrace("parse_comsub:%d: found here doc end `%s'", line_number, ret + tind);*/
3781 lex_firstind = retind + 1;
3785 /* Possible reprompting. */
3786 if (ch == '\n' && SHOULD_PROMPT ())
3789 /* XXX -- possibly allow here doc to be delimited by ending right
3791 if ((tflags & LEX_INHEREDOC) && ch == close && count == 1)
3794 /*itrace("parse_comsub:%d: in here doc, ch == close, retind - firstind = %d hdlen = %d retind = %d", line_number, retind-lex_firstind, hdlen, retind);*/
3795 tind = lex_firstind;
3796 while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t')
3798 if (retind-tind == hdlen && STREQN (ret + tind, heredelim, hdlen))
3800 tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC|LEX_QUOTEDDOC);
3801 /*itrace("parse_comsub:%d: found here doc end `%*s'", line_number, hdlen, ret + tind);*/
3808 /* Don't bother counting parens or doing anything else if in a comment or
3809 here document (not exactly right for here-docs -- if we want to allow
3810 recursive calls to parse_comsub to have their own here documents,
3811 change the LEX_INHEREDOC to LEX_QUOTEDDOC here and uncomment the next
3812 clause below. Note that to make this work completely, we need to make
3813 additional changes to allow xparse_dolparen to work right when the
3814 command substitution is parsed, because read_secondary_line doesn't know
3815 to recursively parse through command substitutions embedded in here-
3817 if (tflags & (LEX_INCOMMENT|LEX_INHEREDOC))
3819 /* Add this character. */
3820 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3823 if ((tflags & LEX_INCOMMENT) && ch == '\n')
3825 /*itrace("parse_comsub:%d: lex_incomment -> 0 ch = `%c'", line_number, ch);*/
3826 tflags &= ~LEX_INCOMMENT;
3832 /* If we're going to recursively parse a command substitution inside a
3833 here-document, make sure we call parse_comsub recursively below. See
3834 above for additional caveats. */
3835 if ((tflags & LEX_INHEREDOC) && ((tflags & LEX_WASDOL) == 0 || ch != '(')) /*)*/
3837 /* Add this character. */
3838 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3840 if MBTEST(ch == '$')
3841 tflags |= LEX_WASDOL;
3843 tflags &= ~LEX_WASDOL;
3847 if (tflags & LEX_PASSNEXT) /* last char was backslash */
3849 /*itrace("parse_comsub:%d: lex_passnext -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3850 tflags &= ~LEX_PASSNEXT;
3851 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
3854 retind--; /* swallow previously-added backslash */
3858 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3859 if MBTEST(ch == CTLESC)
3860 ret[retind++] = CTLESC;
3865 /* If this is a shell break character, we are not in a word. If not,
3866 we either start or continue a word. */
3867 if MBTEST(shellbreak (ch))
3869 tflags &= ~LEX_INWORD;
3870 /*itrace("parse_comsub:%d: lex_inword -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3874 if (tflags & LEX_INWORD)
3877 /*itrace("parse_comsub:%d: lex_inword == 1 ch = `%c' lex_wlen = %d (%d)", line_number, ch, lex_wlen, __LINE__);*/
3881 /*itrace("parse_comsub:%d: lex_inword -> 1 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3882 tflags |= LEX_INWORD;
3884 if (tflags & LEX_RESWDOK)
3889 /* Skip whitespace */
3890 if MBTEST(shellblank (ch) && (tflags & LEX_HEREDELIM) == 0 && lex_rwlen == 0)
3892 /* Add this character. */
3893 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3898 /* Either we are looking for the start of the here-doc delimiter
3899 (lex_firstind == -1) or we are reading one (lex_firstind >= 0).
3900 If this character is a shell break character and we are reading
3901 the delimiter, save it and note that we are now reading a here
3902 document. If we've found the start of the delimiter, note it by
3903 setting lex_firstind. Backslashes can quote shell metacharacters
3904 in here-doc delimiters. */
3905 if (tflags & LEX_HEREDELIM)
3907 if (lex_firstind == -1 && shellbreak (ch) == 0)
3908 lex_firstind = retind;
3910 else if (heredelim && (tflags & LEX_PASSNEXT) == 0 && ch == '\n')
3912 tflags |= LEX_INHEREDOC;
3913 tflags &= ~LEX_HEREDELIM;
3914 lex_firstind = retind + 1;
3917 else if (lex_firstind >= 0 && (tflags & LEX_PASSNEXT) == 0 && shellbreak (ch))
3921 nestret = substring (ret, lex_firstind, retind);
3922 heredelim = string_quote_removal (nestret, 0);
3923 hdlen = STRLEN(heredelim);
3924 /*itrace("parse_comsub:%d: found here doc delimiter `%s' (%d)", line_number, heredelim, hdlen);*/
3925 if (STREQ (heredelim, nestret) == 0)
3926 tflags |= LEX_QUOTEDDOC;
3931 tflags |= LEX_INHEREDOC;
3932 tflags &= ~LEX_HEREDELIM;
3933 lex_firstind = retind + 1;
3940 /* Meta-characters that can introduce a reserved word. Not perfect yet. */
3941 if MBTEST((tflags & LEX_RESWDOK) == 0 && (tflags & LEX_CKCASE) && (tflags & LEX_INCOMMENT) == 0 && (shellmeta(ch) || ch == '\n'))
3943 /* Add this character. */
3944 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3946 peekc = shell_getc (1);
3947 if (ch == peekc && (ch == '&' || ch == '|' || ch == ';')) /* two-character tokens */
3949 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3950 ret[retind++] = peekc;
3951 /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/
3952 tflags |= LEX_RESWDOK;
3956 else if (ch == '\n' || COMSUB_META(ch))
3958 shell_ungetc (peekc);
3959 /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/
3960 tflags |= LEX_RESWDOK;
3968 /* `unget' the character we just added and fall through */
3970 shell_ungetc (peekc);
3974 /* If we can read a reserved word, try to read one. */
3975 if (tflags & LEX_RESWDOK)
3977 if MBTEST(islower ((unsigned char)ch))
3979 /* Add this character. */
3980 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3985 else if MBTEST(lex_rwlen == 4 && shellbreak (ch))
3987 if (STREQN (ret + retind - 4, "case", 4))
3989 tflags |= LEX_INCASE;
3990 tflags &= ~LEX_RESWDOK;
3991 /*itrace("parse_comsub:%d: found `case', lex_incase -> 1 lex_reswdok -> 0", line_number);*/
3993 else if (STREQN (ret + retind - 4, "esac", 4))
3995 tflags &= ~LEX_INCASE;
3996 /*itrace("parse_comsub:%d: found `esac', lex_incase -> 0 lex_reswdok -> 1", line_number);*/
3997 tflags |= LEX_RESWDOK;
4000 else if (STREQN (ret + retind - 4, "done", 4) ||
4001 STREQN (ret + retind - 4, "then", 4) ||
4002 STREQN (ret + retind - 4, "else", 4) ||
4003 STREQN (ret + retind - 4, "elif", 4) ||
4004 STREQN (ret + retind - 4, "time", 4))
4006 /* these are four-character reserved words that can be
4007 followed by a reserved word; anything else turns off
4008 the reserved-word-ok flag */
4009 /*itrace("parse_comsub:%d: found `%.4s', lex_reswdok -> 1", line_number, ret+retind-4);*/
4010 tflags |= LEX_RESWDOK;
4015 tflags &= ~LEX_RESWDOK;
4016 /*itrace("parse_comsub:%d: found `%.4s', lex_reswdok -> 0", line_number, ret+retind-4);*/
4019 else if MBTEST((tflags & LEX_CKCOMMENT) && ch == '#' && (lex_rwlen == 0 || ((tflags & LEX_INWORD) && lex_wlen == 0)))
4020 ; /* don't modify LEX_RESWDOK if we're starting a comment */
4021 /* Allow `do' followed by space, tab, or newline to preserve the
4022 RESWDOK flag, but reset the reserved word length counter so we
4023 can read another one. */
4024 else if MBTEST(((tflags & LEX_INCASE) == 0) &&
4025 (isblank((unsigned char)ch) || ch == '\n') &&
4027 STREQN (ret + retind - 2, "do", 2))
4029 /*itrace("parse_comsub:%d: lex_incase == 0 found `%c', found \"do\"", line_number, ch);*/
4032 else if MBTEST((tflags & LEX_INCASE) && ch != '\n')
4033 /* If we can read a reserved word and we're in case, we're at the
4034 point where we can read a new pattern list or an esac. We
4035 handle the esac case above. If we read a newline, we want to
4036 leave LEX_RESWDOK alone. If we read anything else, we want to
4037 turn off LEX_RESWDOK, since we're going to read a pattern list. */
4039 tflags &= ~LEX_RESWDOK;
4040 /*itrace("parse_comsub:%d: lex_incase == 1 found `%c', lex_reswordok -> 0", line_number, ch);*/
4042 else if MBTEST(shellbreak (ch) == 0)
4044 tflags &= ~LEX_RESWDOK;
4045 /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/
4048 /* If we find a space or tab but have read something and it's not
4049 `do', turn off the reserved-word-ok flag */
4050 else if MBTEST(isblank ((unsigned char)ch) && lex_rwlen > 0)
4052 tflags &= ~LEX_RESWDOK;
4053 /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/
4058 /* Might be the start of a here-doc delimiter */
4059 if MBTEST((tflags & LEX_INCOMMENT) == 0 && (tflags & LEX_CKCASE) && ch == '<')
4061 /* Add this character. */
4062 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
4064 peekc = shell_getc (1);
4069 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
4070 ret[retind++] = peekc;
4071 peekc = shell_getc (1);
4076 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
4077 ret[retind++] = peekc;
4078 tflags |= LEX_STRIPDOC;
4081 shell_ungetc (peekc);
4084 tflags |= LEX_HEREDELIM;
4090 ch = peekc; /* fall through and continue XXX */
4092 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (((tflags & LEX_RESWDOK) && lex_rwlen == 0) || ((tflags & LEX_INWORD) && lex_wlen == 0)))
4094 /*itrace("parse_comsub:%d: lex_incomment -> 1 (%d)", line_number, __LINE__);*/
4095 tflags |= LEX_INCOMMENT;
4098 if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
4100 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
4101 ret[retind++] = CTLESC;
4106 else if MBTEST((tflags & LEX_INCASE) && ch == close && close == ')')
4107 tflags &= ~LEX_INCASE; /* XXX */
4109 else if MBTEST(ch == close && (tflags & LEX_INCASE) == 0) /* ending delimiter */
4112 /*itrace("parse_comsub:%d: found close: count = %d", line_number, count);*/
4114 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && (tflags & LEX_INCASE) == 0 && ch == open) /* nested begin */
4117 /*itrace("parse_comsub:%d: found open: count = %d", line_number, count);*/
4120 /* Add this character. */
4121 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
4124 /* If we just read the ending character, don't bother continuing. */
4128 if MBTEST(ch == '\\') /* backslashes */
4129 tflags |= LEX_PASSNEXT;
4131 if MBTEST(shellquote (ch))
4133 /* '', ``, or "" inside $(...). */
4134 push_delimiter (dstack, ch);
4135 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
4136 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
4138 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
4139 pop_delimiter (dstack);
4140 CHECK_NESTRET_ERROR ();
4142 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
4144 /* Translate $'...' here. */
4145 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
4148 if ((rflags & P_DQUOTE) == 0)
4150 nestret = sh_single_quote (ttrans);
4152 nestlen = strlen (nestret);
4157 nestlen = ttranslen;
4159 retind -= 2; /* back up before the $' */
4161 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
4163 /* Locale expand $"..." here. */
4164 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
4167 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
4169 nestlen = ttranslen + 2;
4170 retind -= 2; /* back up before the $" */
4176 else if MBTEST((tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
4177 /* check for $(), $[], or ${} inside command substitution. */
4179 if ((tflags & LEX_INCASE) == 0 && open == ch) /* undo previous increment */
4181 if (ch == '(') /* ) */
4182 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
4183 else if (ch == '{') /* } */
4184 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags);
4185 else if (ch == '[') /* ] */
4186 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
4188 CHECK_NESTRET_ERROR ();
4193 if MBTEST(ch == '$')
4194 tflags |= LEX_WASDOL;
4196 tflags &= ~LEX_WASDOL;
4203 /*itrace("parse_comsub:%d: returning `%s'", line_number, ret);*/
4207 /* Recursively call the parser to parse a $(...) command substitution. */
4209 xparse_dolparen (base, string, indp, flags)
4215 sh_parser_state_t ps;
4216 sh_input_line_state_t ls;
4217 int orig_ind, nc, sflags, orig_eof_token;
4218 char *ret, *s, *ep, *ostring;
4224 /*itrace("xparse_dolparen: size = %d shell_input_line = `%s'", shell_input_line_size, shell_input_line);*/
4225 sflags = SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOFREE;
4226 if (flags & SX_NOLONGJMP)
4227 sflags |= SEVAL_NOLONGJMP;
4228 save_parser_state (&ps);
4229 save_input_line_state (&ls);
4230 orig_eof_token = shell_eof_token;
4233 parser_state |= PST_CMDSUBST|PST_EOFTOKEN; /* allow instant ')' */ /*(*/
4234 shell_eof_token = ')';
4236 nc = parse_string (string, "command substitution", sflags, &ep);
4238 shell_eof_token = orig_eof_token;
4239 restore_parser_state (&ps);
4241 /* reset_parser clears shell_input_line and associated variables */
4242 restore_input_line_state (&ls);
4246 /* If parse_string returns < 0, we need to jump to top level with the
4247 negative of the return value */
4249 jump_to_top_level (-nc); /* XXX */
4251 /* Need to find how many characters parse_and_execute consumed, update
4252 *indp, if flags != 0, copy the portion of the string parsed into RET
4253 and return it. If flags & 1 (EX_NOALLOC) we can return NULL. */
4260 itrace("xparse_dolparen:%d: ep[-1] != RPAREN (%d), ep = `%s'", line_number, ep[-1], ep);
4262 while (ep > ostring && ep[-1] == '\n') ep--;
4266 *indp = ep - base - 1;
4270 if (base[*indp] != ')')
4271 itrace("xparse_dolparen:%d: base[%d] != RPAREN (%d), base = `%s'", line_number, *indp, base[*indp], base);
4274 if (flags & SX_NOALLOC)
4275 return (char *)NULL;
4283 ret = substring (ostring, 0, nc - 1);
4288 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
4289 /* Parse a double-paren construct. It can be either an arithmetic
4290 command, an arithmetic `for' command, or a nested subshell. Returns
4291 the parsed token, -1 on error, or -2 if we didn't do anything and
4292 should just go on. */
4301 #if defined (ARITH_FOR_COMMAND)
4302 if (last_read_token == FOR)
4304 arith_for_lineno = line_number;
4305 cmdtyp = parse_arith_cmd (&wval, 0);
4308 wd = alloc_word_desc ();
4310 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
4311 return (ARITH_FOR_EXPRS);
4314 return -1; /* ERROR */
4318 #if defined (DPAREN_ARITHMETIC)
4319 if (reserved_word_acceptable (last_read_token))
4321 sline = line_number;
4323 cmdtyp = parse_arith_cmd (&wval, 0);
4324 if (cmdtyp == 1) /* arithmetic command */
4326 wd = alloc_word_desc ();
4328 wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE;
4329 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
4332 else if (cmdtyp == 0) /* nested subshell */
4334 push_string (wval, 0, (alias_t *)NULL);
4335 pushed_string_list->flags = PSH_DPAREN;
4336 if ((parser_state & PST_CASEPAT) == 0)
4337 parser_state |= PST_SUBSHELL;
4345 return -2; /* XXX */
4348 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
4349 If not, assume it's a nested subshell for backwards compatibility and
4350 return 0. In any case, put the characters we've consumed into a locally-
4351 allocated buffer and make *ep point to that buffer. Return -1 on an
4352 error, for example EOF. */
4354 parse_arith_cmd (ep, adddq)
4358 int exp_lineno, rval, c;
4359 char *ttok, *tokstr;
4362 exp_lineno = line_number;
4363 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
4365 if (ttok == &matched_pair_error)
4367 /* Check that the next character is the closing right paren. If
4368 not, this is a syntax error. ( */
4373 tokstr = (char *)xmalloc (ttoklen + 4);
4375 /* if ADDDQ != 0 then (( ... )) -> "..." */
4376 if (rval == 1 && adddq) /* arith cmd, add double quotes */
4379 strncpy (tokstr + 1, ttok, ttoklen - 1);
4380 tokstr[ttoklen] = '"';
4381 tokstr[ttoklen+1] = '\0';
4383 else if (rval == 1) /* arith cmd, don't add double quotes */
4385 strncpy (tokstr, ttok, ttoklen - 1);
4386 tokstr[ttoklen-1] = '\0';
4388 else /* nested subshell */
4391 strncpy (tokstr + 1, ttok, ttoklen - 1);
4392 tokstr[ttoklen] = ')';
4393 tokstr[ttoklen+1] = c;
4394 tokstr[ttoklen+2] = '\0';
4401 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
4403 #if defined (COND_COMMAND)
4409 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
4410 parser_error (cond_lineno, _("unexpected EOF while looking for `]]'"));
4411 else if (cond_token != COND_ERROR)
4413 if (etext = error_token_from_token (cond_token))
4415 parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext);
4419 parser_error (cond_lineno, _("syntax error in conditional expression"));
4426 return (cond_or ());
4435 if (cond_token == OR_OR)
4438 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
4449 if (cond_token == AND_AND)
4452 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
4458 cond_skip_newlines ()
4460 while ((cond_token = read_token (READ)) == '\n')
4462 if (SHOULD_PROMPT ())
4465 return (cond_token);
4468 #define COND_RETURN_ERROR() \
4469 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
4475 COND_COM *term, *tleft, *tright;
4479 /* Read a token. It can be a left paren, a `!', a unary operator, or a
4480 word that should be the first argument of a binary operator. Start by
4481 skipping newlines, since this is a compound command. */
4482 tok = cond_skip_newlines ();
4483 lineno = line_number;
4484 if (tok == COND_END)
4486 COND_RETURN_ERROR ();
4488 else if (tok == '(')
4490 term = cond_expr ();
4491 if (cond_token != ')')
4494 dispose_cond_node (term); /* ( */
4495 if (etext = error_token_from_token (cond_token))
4497 parser_error (lineno, _("unexpected token `%s', expected `)'"), etext);
4501 parser_error (lineno, _("expected `)'"));
4502 COND_RETURN_ERROR ();
4504 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
4505 (void)cond_skip_newlines ();
4507 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
4510 dispose_word (yylval.word); /* not needed */
4511 term = cond_term ();
4513 term->flags |= CMD_INVERT_RETURN;
4515 else if (tok == WORD && yylval.word->word[0] == '-' && yylval.word->word[2] == 0 && test_unop (yylval.word->word))
4518 tok = read_token (READ);
4521 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4522 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
4527 if (etext = error_token_from_token (tok))
4529 parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext);
4533 parser_error (line_number, _("unexpected argument to conditional unary operator"));
4534 COND_RETURN_ERROR ();
4537 (void)cond_skip_newlines ();
4539 else if (tok == WORD) /* left argument to binary operator */
4542 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4545 tok = read_token (READ);
4546 if (tok == WORD && test_binop (yylval.word->word))
4549 if (op->word[0] == '=' && (op->word[1] == '\0' || (op->word[1] == '=' && op->word[2] == '\0')))
4550 parser_state |= PST_EXTPAT;
4551 else if (op->word[0] == '!' && op->word[1] == '=' && op->word[2] == '\0')
4552 parser_state |= PST_EXTPAT;
4554 #if defined (COND_REGEXP)
4555 else if (tok == WORD && STREQ (yylval.word->word, "=~"))
4558 parser_state |= PST_REGEXP;
4561 else if (tok == '<' || tok == '>')
4562 op = make_word_from_token (tok); /* ( */
4563 /* There should be a check before blindly accepting the `)' that we have
4564 seen the opening `('. */
4565 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
4567 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
4568 the test command. Similarly for [[ x && expr ]] or
4569 [[ x || expr ]] or [[ (x) ]]. */
4570 op = make_word ("-n");
4571 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
4577 if (etext = error_token_from_token (tok))
4579 parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext);
4583 parser_error (line_number, _("conditional binary operator expected"));
4584 dispose_cond_node (tleft);
4585 COND_RETURN_ERROR ();
4589 if (parser_state & PST_EXTPAT)
4591 tok = read_token (READ);
4592 if (parser_state & PST_EXTPAT)
4593 extended_glob = global_extglob;
4594 parser_state &= ~(PST_REGEXP|PST_EXTPAT);
4598 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4599 term = make_cond_node (COND_BINARY, op, tleft, tright);
4603 if (etext = error_token_from_token (tok))
4605 parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext);
4609 parser_error (line_number, _("unexpected argument to conditional binary operator"));
4610 dispose_cond_node (tleft);
4612 COND_RETURN_ERROR ();
4615 (void)cond_skip_newlines ();
4620 parser_error (line_number, _("unexpected token `%c' in conditional command"), tok);
4621 else if (etext = error_token_from_token (tok))
4623 parser_error (line_number, _("unexpected token `%s' in conditional command"), etext);
4627 parser_error (line_number, _("unexpected token %d in conditional command"), tok);
4628 COND_RETURN_ERROR ();
4633 /* This is kind of bogus -- we slip a mini recursive-descent parser in
4634 here to handle the conditional statement syntax. */
4636 parse_cond_command ()
4640 global_extglob = extended_glob;
4641 cexp = cond_expr ();
4642 return (make_cond_command (cexp));
4646 #if defined (ARRAY_VARS)
4647 /* When this is called, it's guaranteed that we don't care about anything
4648 in t beyond i. We do save and restore the chars, though. */
4650 token_is_assignment (t, i)
4654 unsigned char c, c1;
4657 c = t[i]; c1 = t[i+1];
4658 t[i] = '='; t[i+1] = '\0';
4659 r = assignment (t, (parser_state & PST_COMPASSIGN) != 0);
4660 t[i] = c; t[i+1] = c1;
4664 /* XXX - possible changes here for `+=' */
4666 token_is_ident (t, i)
4675 r = legal_identifier (t);
4682 read_token_word (character)
4685 /* The value for YYLVAL when a WORD is read. */
4686 WORD_DESC *the_word;
4688 /* Index into the token that we are building. */
4691 /* ALL_DIGITS becomes zero when we see a non-digit. */
4692 int all_digit_token;
4694 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
4697 /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
4699 int compound_assignment;
4701 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
4704 /* Non-zero means to ignore the value of the next character, and just
4705 to add it no matter what. */
4706 int pass_next_character;
4708 /* The current delimiting character. */
4710 int result, peek_char;
4711 char *ttok, *ttrans;
4712 int ttoklen, ttranslen;
4715 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
4716 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
4719 all_digit_token = DIGIT (character);
4720 dollar_present = quoted = pass_next_character = compound_assignment = 0;
4724 if (character == EOF)
4727 if (pass_next_character)
4729 pass_next_character = 0;
4730 goto got_escaped_character;
4733 cd = current_delimiter (dstack);
4735 /* Handle backslashes. Quote lots of things when not inside of
4736 double-quotes, quote some things inside of double-quotes. */
4737 if MBTEST(character == '\\')
4739 peek_char = shell_getc (0);
4741 /* Backslash-newline is ignored in all cases except
4742 when quoted with single quotes. */
4743 if (peek_char == '\n')
4746 goto next_character;
4750 shell_ungetc (peek_char);
4752 /* If the next character is to be quoted, note it now. */
4753 if (cd == 0 || cd == '`' ||
4754 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
4755 pass_next_character++;
4762 /* Parse a matched pair of quote characters. */
4763 if MBTEST(shellquote (character))
4765 push_delimiter (dstack, character);
4766 ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0);
4767 pop_delimiter (dstack);
4768 if (ttok == &matched_pair_error)
4769 return -1; /* Bail immediately. */
4770 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4771 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4772 token[token_index++] = character;
4773 strcpy (token + token_index, ttok);
4774 token_index += ttoklen;
4775 all_digit_token = 0;
4777 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
4779 goto next_character;
4783 /* When parsing a regexp as a single word inside a conditional command,
4784 we need to special-case characters special to both the shell and
4785 regular expressions. Right now, that is only '(' and '|'. */ /*)*/
4786 if MBTEST((parser_state & PST_REGEXP) && (character == '(' || character == '|')) /*)*/
4788 if (character == '|')
4791 push_delimiter (dstack, character);
4792 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4793 pop_delimiter (dstack);
4794 if (ttok == &matched_pair_error)
4795 return -1; /* Bail immediately. */
4796 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4797 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4798 token[token_index++] = character;
4799 strcpy (token + token_index, ttok);
4800 token_index += ttoklen;
4802 dollar_present = all_digit_token = 0;
4803 goto next_character;
4805 #endif /* COND_REGEXP */
4807 #ifdef EXTENDED_GLOB
4808 /* Parse a ksh-style extended pattern matching specification. */
4809 if MBTEST(extended_glob && PATTERN_CHAR (character))
4811 peek_char = shell_getc (1);
4812 if MBTEST(peek_char == '(') /* ) */
4814 push_delimiter (dstack, peek_char);
4815 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4816 pop_delimiter (dstack);
4817 if (ttok == &matched_pair_error)
4818 return -1; /* Bail immediately. */
4819 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,
4821 TOKEN_DEFAULT_GROW_SIZE);
4822 token[token_index++] = character;
4823 token[token_index++] = peek_char;
4824 strcpy (token + token_index, ttok);
4825 token_index += ttoklen;
4827 dollar_present = all_digit_token = 0;
4828 goto next_character;
4831 shell_ungetc (peek_char);
4833 #endif /* EXTENDED_GLOB */
4835 /* If the delimiter character is not single quote, parse some of
4836 the shell expansions that must be read as a single word. */
4837 if (shellexp (character))
4839 peek_char = shell_getc (1);
4840 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
4841 if MBTEST(peek_char == '(' ||
4842 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
4844 if (peek_char == '{') /* } */
4845 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE|P_DOLBRACE);
4846 else if (peek_char == '(') /* ) */
4848 /* XXX - push and pop the `(' as a delimiter for use by
4849 the command-oriented-history code. This way newlines
4850 appearing in the $(...) string get added to the
4851 history literally rather than causing a possibly-
4852 incorrect `;' to be added. ) */
4853 push_delimiter (dstack, peek_char);
4854 ttok = parse_comsub (cd, '(', ')', &ttoklen, P_COMMAND);
4855 pop_delimiter (dstack);
4858 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
4859 if (ttok == &matched_pair_error)
4860 return -1; /* Bail immediately. */
4861 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,
4863 TOKEN_DEFAULT_GROW_SIZE);
4864 token[token_index++] = character;
4865 token[token_index++] = peek_char;
4866 strcpy (token + token_index, ttok);
4867 token_index += ttoklen;
4870 all_digit_token = 0;
4871 goto next_character;
4873 /* This handles $'...' and $"..." new-style quoted strings. */
4874 else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
4878 first_line = line_number;
4879 push_delimiter (dstack, peek_char);
4880 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
4882 (peek_char == '\'') ? P_ALLOWESC : 0);
4883 pop_delimiter (dstack);
4884 if (ttok == &matched_pair_error)
4886 if (peek_char == '\'')
4888 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
4891 /* Insert the single quotes and correctly quote any
4892 embedded single quotes (allowed because P_ALLOWESC was
4893 passed to parse_matched_pair). */
4894 ttok = sh_single_quote (ttrans);
4896 ttranslen = strlen (ttok);
4901 /* Try to locale-expand the converted string. */
4902 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
4905 /* Add the double quotes back */
4906 ttok = sh_mkdoublequoted (ttrans, ttranslen, 0);
4912 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 1,
4914 TOKEN_DEFAULT_GROW_SIZE);
4915 strcpy (token + token_index, ttrans);
4916 token_index += ttranslen;
4919 all_digit_token = 0;
4920 goto next_character;
4922 /* This could eventually be extended to recognize all of the
4923 shell's single-character parameter expansions, and set flags.*/
4924 else if MBTEST(character == '$' && peek_char == '$')
4926 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
4928 TOKEN_DEFAULT_GROW_SIZE);
4929 token[token_index++] = '$';
4930 token[token_index++] = peek_char;
4932 all_digit_token = 0;
4933 goto next_character;
4936 shell_ungetc (peek_char);
4939 #if defined (ARRAY_VARS)
4940 /* Identify possible array subscript assignment; match [...]. If
4941 parser_state&PST_COMPASSIGN, we need to parse [sub]=words treating
4942 `sub' as if it were enclosed in double quotes. */
4943 else if MBTEST(character == '[' && /* ] */
4944 ((token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) ||
4945 (token_index == 0 && (parser_state&PST_COMPASSIGN))))
4947 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, P_ARRAYSUB);
4948 if (ttok == &matched_pair_error)
4949 return -1; /* Bail immediately. */
4950 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4952 TOKEN_DEFAULT_GROW_SIZE);
4953 token[token_index++] = character;
4954 strcpy (token + token_index, ttok);
4955 token_index += ttoklen;
4957 all_digit_token = 0;
4958 goto next_character;
4960 /* Identify possible compound array variable assignment. */
4961 else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index))
4963 peek_char = shell_getc (1);
4964 if MBTEST(peek_char == '(') /* ) */
4966 ttok = parse_compound_assignment (&ttoklen);
4968 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
4970 TOKEN_DEFAULT_GROW_SIZE);
4972 token[token_index++] = '=';
4973 token[token_index++] = '(';
4976 strcpy (token + token_index, ttok);
4977 token_index += ttoklen;
4979 token[token_index++] = ')';
4981 all_digit_token = 0;
4982 compound_assignment = 1;
4984 goto next_character;
4986 goto got_token; /* ksh93 seems to do this */
4990 shell_ungetc (peek_char);
4994 /* When not parsing a multi-character word construct, shell meta-
4995 characters break words. */
4996 if MBTEST(shellbreak (character))
4998 shell_ungetc (character);
5003 if (character == CTLESC || character == CTLNUL)
5005 RESIZE_MALLOCED_BUFFER (token, token_index, 2, token_buffer_size,
5006 TOKEN_DEFAULT_GROW_SIZE);
5007 token[token_index++] = CTLESC;
5010 got_escaped_character:
5011 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
5012 TOKEN_DEFAULT_GROW_SIZE);
5014 token[token_index++] = character;
5016 all_digit_token &= DIGIT (character);
5017 dollar_present |= character == '$';
5020 if (character == '\n' && SHOULD_PROMPT ())
5023 /* We want to remove quoted newlines (that is, a \<newline> pair)
5024 unless we are within single quotes or pass_next_character is
5025 set (the shell equivalent of literal-next). */
5026 cd = current_delimiter (dstack);
5027 character = shell_getc (cd != '\'' && pass_next_character == 0);
5028 } /* end for (;;) */
5032 /* Calls to RESIZE_MALLOCED_BUFFER ensure there is sufficient room. */
5033 token[token_index] = '\0';
5035 /* Check to see what thing we should return. If the last_read_token
5036 is a `<', or a `&', or the character which ended this token is
5037 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
5038 Otherwise, it is just a word, and should be returned as such. */
5039 if MBTEST(all_digit_token && (character == '<' || character == '>' ||
5040 last_read_token == LESS_AND ||
5041 last_read_token == GREATER_AND))
5043 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
5045 yylval.number = lvalue;
5050 /* Check for special case tokens. */
5051 result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
5056 /* Posix.2 does not allow reserved words to be aliased, so check for all
5057 of them, including special cases, before expanding the current token
5059 if MBTEST(posixly_correct)
5060 CHECK_FOR_RESERVED_WORD (token);
5062 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
5063 inhibits alias expansion. */
5064 if (expand_aliases && quoted == 0)
5066 result = alias_expand_token (token);
5067 if (result == RE_READ_TOKEN)
5068 return (RE_READ_TOKEN);
5069 else if (result == NO_EXPANSION)
5070 parser_state &= ~PST_ALEXPNEXT;
5073 /* If not in Posix.2 mode, check for reserved words after alias
5075 if MBTEST(posixly_correct == 0)
5077 CHECK_FOR_RESERVED_WORD (token);
5079 the_word = alloc_word_desc ();
5080 the_word->word = (char *)xmalloc (1 + token_index);
5081 the_word->flags = 0;
5082 strcpy (the_word->word, token);
5084 the_word->flags |= W_HASDOLLAR;
5086 the_word->flags |= W_QUOTED; /*(*/
5087 if (compound_assignment && token[token_index-1] == ')')
5088 the_word->flags |= W_COMPASSIGN;
5089 /* A word is an assignment if it appears at the beginning of a
5090 simple command, or after another assignment word. This is
5091 context-dependent, so it cannot be handled in the grammar. */
5092 if (assignment (token, (parser_state & PST_COMPASSIGN) != 0))
5094 the_word->flags |= W_ASSIGNMENT;
5095 /* Don't perform word splitting on assignment statements. */
5096 if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0)
5098 the_word->flags |= W_NOSPLIT;
5099 if (parser_state & PST_COMPASSIGN)
5100 the_word->flags |= W_NOGLOB; /* XXX - W_NOBRACE? */
5104 if (command_token_position (last_read_token))
5107 b = builtin_address_internal (token, 0);
5108 if (b && (b->flags & ASSIGNMENT_BUILTIN))
5109 parser_state |= PST_ASSIGNOK;
5110 else if (STREQ (token, "eval") || STREQ (token, "let"))
5111 parser_state |= PST_ASSIGNOK;
5114 yylval.word = the_word;
5116 if (token[0] == '{' && token[token_index-1] == '}' &&
5117 (character == '<' || character == '>'))
5119 /* can use token; already copied to the_word */
5120 token[token_index-1] = '\0';
5121 #if defined (ARRAY_VARS)
5122 if (legal_identifier (token+1) || valid_array_reference (token+1, 0))
5124 if (legal_identifier (token+1))
5127 strcpy (the_word->word, token+1);
5128 /*itrace("read_token_word: returning REDIR_WORD for %s", the_word->word);*/
5129 return (REDIR_WORD);
5133 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
5134 ? ASSIGNMENT_WORD : WORD;
5136 switch (last_read_token)
5139 parser_state |= PST_ALLOWOPNBRC;
5140 function_dstart = line_number;
5145 if (word_top < MAX_CASE_NEST)
5147 word_lineno[word_top] = line_number;
5154 /* Return 1 if TOKSYM is a token that after being read would allow
5155 a reserved word to be seen, else 0. */
5157 reserved_word_acceptable (toksym)
5194 #if defined (COPROCESS_SUPPORT)
5195 if (last_read_token == WORD && token_before_that == COPROC)
5198 if (last_read_token == WORD && token_before_that == FUNCTION)
5204 /* Return the index of TOKEN in the alist of reserved words, or -1 if
5205 TOKEN is not a shell reserved word. */
5207 find_reserved_word (tokstr)
5211 for (i = 0; word_token_alist[i].word; i++)
5212 if (STREQ (tokstr, word_token_alist[i].word))
5217 /* An interface to let the rest of the shell (primarily the completion
5218 system) know what the parser is expecting. */
5220 parser_in_command_position ()
5222 return (command_token_position (last_read_token));
5226 #if defined (READLINE)
5227 /* Called after each time readline is called. This insures that whatever
5228 the new prompt string is gets propagated to readline's local prompt
5231 reset_readline_prompt ()
5235 if (prompt_string_pointer)
5237 temp_prompt = (*prompt_string_pointer)
5238 ? decode_prompt_string (*prompt_string_pointer)
5241 if (temp_prompt == 0)
5243 temp_prompt = (char *)xmalloc (1);
5244 temp_prompt[0] = '\0';
5247 FREE (current_readline_prompt);
5248 current_readline_prompt = temp_prompt;
5251 #endif /* READLINE */
5254 #if defined (HISTORY)
5255 /* A list of tokens which can be followed by newlines, but not by
5256 semi-colons. When concatenating multiple lines of history, the
5257 newline separator for such tokens is replaced with a space. */
5258 static const int no_semi_successors[] = {
5259 '\n', '{', '(', ')', ';', '&', '|',
5260 CASE, DO, ELSE, IF, SEMI_SEMI, SEMI_AND, SEMI_SEMI_AND, THEN, UNTIL,
5261 WHILE, AND_AND, OR_OR, IN,
5265 /* If we are not within a delimited expression, try to be smart
5266 about which separators can be semi-colons and which must be
5267 newlines. Returns the string that should be added into the
5268 history entry. LINE is the line we're about to add; it helps
5269 make some more intelligent decisions in certain cases. */
5271 history_delimiting_chars (line)
5274 static int last_was_heredoc = 0; /* was the last entry the start of a here document? */
5277 if ((parser_state & PST_HEREDOC) == 0)
5278 last_was_heredoc = 0;
5280 if (dstack.delimiter_depth != 0)
5283 /* We look for current_command_line_count == 2 because we are looking to
5284 add the first line of the body of the here document (the second line
5285 of the command). We also keep LAST_WAS_HEREDOC as a private sentinel
5286 variable to note when we think we added the first line of a here doc
5287 (the one with a "<<" somewhere in it) */
5288 if (parser_state & PST_HEREDOC)
5290 if (last_was_heredoc)
5292 last_was_heredoc = 0;
5295 return (here_doc_first_line ? "\n" : "");
5298 if (parser_state & PST_COMPASSIGN)
5301 /* First, handle some special cases. */
5303 /* If we just read `()', assume it's a function definition, and don't
5304 add a semicolon. If the token before the `)' was not `(', and we're
5305 not in the midst of parsing a case statement, assume it's a
5306 parenthesized command and add the semicolon. */
5308 if (token_before_that == ')')
5310 if (two_tokens_ago == '(') /*)*/ /* function def */
5312 /* This does not work for subshells inside case statement
5313 command lists. It's a suboptimal solution. */
5314 else if (parser_state & PST_CASESTMT) /* case statement pattern */
5317 return "; "; /* (...) subshell */
5319 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
5320 return " "; /* function def using `function name' without `()' */
5322 /* If we're not in a here document, but we think we're about to parse one,
5323 and we would otherwise return a `;', return a newline to delimit the
5324 line with the here-doc delimiter */
5325 else if ((parser_state & PST_HEREDOC) == 0 && current_command_line_count > 1 && last_read_token == '\n' && strstr (line, "<<"))
5327 last_was_heredoc = 1;
5330 else if ((parser_state & PST_HEREDOC) == 0 && current_command_line_count > 1 && need_here_doc > 0)
5332 else if (token_before_that == WORD && two_tokens_ago == FOR)
5334 /* Tricky. `for i\nin ...' should not have a semicolon, but
5335 `for i\ndo ...' should. We do what we can. */
5336 for (i = shell_input_line_index; whitespace (shell_input_line[i]); i++)
5338 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
5342 else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT))
5345 for (i = 0; no_semi_successors[i]; i++)
5347 if (token_before_that == no_semi_successors[i])
5353 #endif /* HISTORY */
5355 /* Issue a prompt, or prepare to issue a prompt when the next character
5362 if (interactive == 0 || expanding_alias ()) /* XXX */
5365 ps1_prompt = get_string_value ("PS1");
5366 ps2_prompt = get_string_value ("PS2");
5368 ps0_prompt = get_string_value ("PS0");
5370 if (!prompt_string_pointer)
5371 prompt_string_pointer = &ps1_prompt;
5373 temp_prompt = *prompt_string_pointer
5374 ? decode_prompt_string (*prompt_string_pointer)
5377 if (temp_prompt == 0)
5379 temp_prompt = (char *)xmalloc (1);
5380 temp_prompt[0] = '\0';
5383 current_prompt_string = *prompt_string_pointer;
5384 prompt_string_pointer = &ps2_prompt;
5386 #if defined (READLINE)
5387 if (!no_line_editing)
5389 FREE (current_readline_prompt);
5390 current_readline_prompt = temp_prompt;
5393 #endif /* READLINE */
5395 FREE (current_decoded_prompt);
5396 current_decoded_prompt = temp_prompt;
5401 get_current_prompt_level ()
5403 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
5407 set_current_prompt_level (x)
5410 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
5411 current_prompt_string = *prompt_string_pointer;
5417 fprintf (stderr, "%s", current_decoded_prompt);
5421 /* Return a string which will be printed as a prompt. The string
5422 may contain special characters which are decoded as follows:
5425 \d the date in Day Mon Date format
5426 \e escape (ascii 033)
5427 \h the hostname up to the first `.'
5429 \j the number of active jobs
5430 \l the basename of the shell's tty device name
5433 \s the name of the shell
5434 \t the time in 24-hour hh:mm:ss format
5435 \T the time in 12-hour hh:mm:ss format
5436 \@ the time in 12-hour hh:mm am/pm format
5437 \A the time in 24-hour hh:mm format
5438 \D{fmt} the result of passing FMT to strftime(3)
5440 \v the version of bash (e.g., 2.00)
5441 \V the release of bash, version + patchlevel (e.g., 2.00.0)
5442 \w the current working directory
5443 \W the last element of $PWD
5444 \! the history number of this command
5445 \# the command number of this command
5446 \$ a $ or a # if you are root
5447 \nnn character code nnn in octal
5449 \[ begin a sequence of non-printing chars
5450 \] end a sequence of non-printing chars
5452 #define PROMPT_GROWTH 48
5454 decode_prompt_string (string)
5459 struct dstack save_dstack;
5460 int last_exit_value, last_comsub_pid;
5461 #if defined (PROMPT_STRING_DECODE)
5465 char *temp, *t_host, octal_string[4];
5471 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
5472 result[result_index = 0] = 0;
5473 temp = (char *)NULL;
5475 while (c = *string++)
5477 if (posixly_correct && c == '!')
5481 temp = savestring ("!");
5486 #if !defined (HISTORY)
5487 temp = savestring ("1");
5489 temp = itos (history_number ());
5490 #endif /* HISTORY */
5491 string--; /* add_string increments string again. */
5509 strncpy (octal_string, string, 3);
5510 octal_string[3] = '\0';
5512 n = read_octal (octal_string);
5513 temp = (char *)xmalloc (3);
5515 if (n == CTLESC || n == CTLNUL)
5532 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
5535 c = 0; /* tested at add_string: */
5543 /* Make the current time/date into a string. */
5544 (void) time (&the_time);
5545 #if defined (HAVE_TZSET)
5546 sv_tz ("TZ"); /* XXX -- just make sure */
5548 tm = localtime (&the_time);
5551 n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm);
5553 n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm);
5555 n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm);
5557 n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm);
5559 n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm);
5564 timebuf[sizeof(timebuf) - 1] = '\0';
5566 temp = savestring (timebuf);
5569 case 'D': /* strftime format */
5570 if (string[1] != '{') /* } */
5573 (void) time (&the_time);
5574 tm = localtime (&the_time);
5575 string += 2; /* skip { */
5576 timefmt = xmalloc (strlen (string) + 3);
5577 for (t = timefmt; *string && *string != '}'; )
5580 c = *string; /* tested at add_string */
5581 if (timefmt[0] == '\0')
5584 timefmt[1] = 'X'; /* locale-specific current time */
5587 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
5593 timebuf[sizeof(timebuf) - 1] = '\0';
5595 if (promptvars || posixly_correct)
5596 /* Make sure that expand_prompt_string is called with a
5597 second argument of Q_DOUBLE_QUOTES if we use this
5599 temp = sh_backslash_quote_for_double_quotes (timebuf);
5601 temp = savestring (timebuf);
5605 temp = (char *)xmalloc (3);
5606 temp[0] = no_line_editing ? '\n' : '\r';
5607 temp[1] = no_line_editing ? '\0' : '\n';
5612 temp = base_pathname (shell_name);
5613 /* Try to quote anything the user can set in the file system */
5614 if (promptvars || posixly_correct)
5615 temp = sh_backslash_quote_for_double_quotes (temp);
5617 temp = savestring (temp);
5622 temp = (char *)xmalloc (16);
5624 strcpy (temp, dist_version);
5626 sprintf (temp, "%s.%d", dist_version, patch_level);
5632 /* Use the value of PWD because it is much more efficient. */
5633 char t_string[PATH_MAX];
5636 temp = get_string_value ("PWD");
5640 if (getcwd (t_string, sizeof(t_string)) == 0)
5646 tlen = strlen (t_string);
5650 tlen = sizeof (t_string) - 1;
5651 strncpy (t_string, temp, tlen);
5653 t_string[tlen] = '\0';
5655 #if defined (MACOSX)
5656 /* Convert from "fs" format to "input" format */
5657 temp = fnx_fromfs (t_string, strlen (t_string));
5658 if (temp != t_string)
5659 strcpy (t_string, temp);
5662 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
5663 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
5664 /* Abbreviate \W as ~ if $PWD == $HOME */
5665 if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0))
5667 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
5669 t = strrchr (t_string, '/');
5671 memmove (t_string, t + 1, strlen (t)); /* strlen(t) to copy NULL */
5675 #undef DOUBLE_SLASH_ROOT
5678 /* polite_directory_format is guaranteed to return a string
5679 no longer than PATH_MAX - 1 characters. */
5680 temp = polite_directory_format (t_string);
5681 if (temp != t_string)
5682 strcpy (t_string, temp);
5685 temp = trim_pathname (t_string, PATH_MAX - 1);
5686 /* If we're going to be expanding the prompt string later,
5687 quote the directory name. */
5688 if (promptvars || posixly_correct)
5689 /* Make sure that expand_prompt_string is called with a
5690 second argument of Q_DOUBLE_QUOTES if we use this
5692 temp = sh_backslash_quote_for_double_quotes (t_string);
5694 temp = savestring (t_string);
5700 if (current_user.user_name == 0)
5701 get_current_user_info ();
5702 temp = savestring (current_user.user_name);
5707 t_host = savestring (current_host_name);
5708 if (c == 'h' && (t = (char *)strchr (t_host, '.')))
5710 if (promptvars || posixly_correct)
5711 /* Make sure that expand_prompt_string is called with a
5712 second argument of Q_DOUBLE_QUOTES if we use this
5714 temp = sh_backslash_quote_for_double_quotes (t_host);
5716 temp = savestring (t_host);
5721 temp = itos (current_command_number);
5725 #if !defined (HISTORY)
5726 temp = savestring ("1");
5728 temp = itos (history_number ());
5729 #endif /* HISTORY */
5733 t = temp = (char *)xmalloc (3);
5734 if ((promptvars || posixly_correct) && (current_user.euid != 0))
5736 *t++ = current_user.euid == 0 ? '#' : '$';
5741 temp = itos (count_all_jobs ());
5745 #if defined (HAVE_TTYNAME)
5746 temp = (char *)ttyname (fileno (stdin));
5747 t = temp ? base_pathname (temp) : "tty";
5748 temp = savestring (t);
5750 temp = savestring ("tty");
5751 #endif /* !HAVE_TTYNAME */
5754 #if defined (READLINE)
5757 if (no_line_editing)
5762 temp = (char *)xmalloc (3);
5763 n = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
5765 if (n == CTLESC || n == CTLNUL)
5770 #endif /* READLINE */
5776 temp = (char *)xmalloc (2);
5783 else /* (c == '\\') */
5790 temp = (char *)xmalloc (3);
5799 sub_append_string (temp, result, &result_index, &result_size);
5800 temp = (char *)NULL; /* Freed in sub_append_string (). */
5801 result[result_index] = '\0';
5807 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
5808 /* dequote_string should take care of removing this if we are not
5809 performing the rest of the word expansions. */
5810 if (c == CTLESC || c == CTLNUL)
5811 result[result_index++] = CTLESC;
5812 result[result_index++] = c;
5813 result[result_index] = '\0';
5816 #else /* !PROMPT_STRING_DECODE */
5817 result = savestring (string);
5818 #endif /* !PROMPT_STRING_DECODE */
5820 /* Save the delimiter stack and point `dstack' to temp space so any
5821 command substitutions in the prompt string won't result in screwing
5822 up the parser's quoting state. */
5823 save_dstack = dstack;
5824 dstack = temp_dstack;
5825 dstack.delimiter_depth = 0;
5827 /* Perform variable and parameter expansion and command substitution on
5828 the prompt string. */
5829 if (promptvars || posixly_correct)
5831 last_exit_value = last_command_exit_value;
5832 last_comsub_pid = last_command_subst_pid;
5833 list = expand_prompt_string (result, Q_DOUBLE_QUOTES, 0);
5835 result = string_list (list);
5836 dispose_words (list);
5837 last_command_exit_value = last_exit_value;
5838 last_command_subst_pid = last_comsub_pid;
5842 t = dequote_string (result);
5847 dstack = save_dstack;
5852 /************************************************
5856 ************************************************/
5858 /* Report a syntax error, and restart the parser. Call here for fatal
5864 report_syntax_error ((char *)NULL);
5870 error_token_from_token (tok)
5875 if (t = find_token_in_alist (tok, word_token_alist, 0))
5878 if (t = find_token_in_alist (tok, other_token_alist, 0))
5882 /* This stuff is dicy and needs closer inspection */
5883 switch (current_token)
5886 case ASSIGNMENT_WORD:
5888 t = savestring (yylval.word->word);
5891 t = itos (yylval.number);
5894 if (yylval.word_list)
5895 t = string_list (yylval.word_list);
5897 case ARITH_FOR_EXPRS:
5898 if (yylval.word_list)
5899 t = string_list_internal (yylval.word_list, " ; ");
5902 t = (char *)NULL; /* punt */
5910 error_token_from_text ()
5915 t = shell_input_line;
5916 i = shell_input_line_index;
5920 if (i && t[i] == '\0')
5923 while (i && (whitespace (t[i]) || t[i] == '\n'))
5929 while (i && (member (t[i], " \n\t;|&") == 0))
5932 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
5935 /* Return our idea of the offending token. */
5936 if (token_end || (i == 0 && token_end == 0))
5939 msg = substring (t, i, token_end);
5940 else /* one-character token */
5942 msg = (char *)xmalloc (2);
5952 print_offending_line ()
5957 msg = savestring (shell_input_line);
5958 token_end = strlen (msg);
5959 while (token_end && msg[token_end - 1] == '\n')
5960 msg[--token_end] = '\0';
5962 parser_error (line_number, "`%s'", msg);
5966 /* Report a syntax error with line numbers, etc.
5967 Call here for recoverable errors. If you have a message to print,
5968 then place it in MESSAGE, otherwise pass NULL and this will figure
5969 out an appropriate message for you. */
5971 report_syntax_error (message)
5978 parser_error (line_number, "%s", message);
5979 if (interactive && EOF_Reached)
5981 last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE;
5985 /* If the line of input we're reading is not null, try to find the
5986 objectionable token. First, try to figure out what token the
5987 parser's complaining about by looking at current_token. */
5988 if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
5990 if (ansic_shouldquote (msg))
5992 p = ansic_quote (msg, 0, NULL);
5996 parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
5999 if (interactive == 0)
6000 print_offending_line ();
6002 last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE;
6006 /* If looking at the current token doesn't prove fruitful, try to find the
6007 offending token by analyzing the text of the input line near the current
6008 input line index and report what we find. */
6009 if (shell_input_line && *shell_input_line)
6011 msg = error_token_from_text ();
6014 parser_error (line_number, _("syntax error near `%s'"), msg);
6018 /* If not interactive, print the line containing the error. */
6019 if (interactive == 0)
6020 print_offending_line ();
6024 msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error");
6025 parser_error (line_number, "%s", msg);
6026 /* When the shell is interactive, this file uses EOF_Reached
6027 only for error reporting. Other mechanisms are used to
6028 decide whether or not to exit. */
6029 if (interactive && EOF_Reached)
6033 last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE;
6036 /* ??? Needed function. ??? We have to be able to discard the constructs
6037 created during parsing. In the case of error, we want to return
6038 allocated objects to the memory pool. In the case of no error, we want
6039 to throw away the information about where the allocated objects live.
6040 (dispose_command () will actually free the command.) */
6042 discard_parser_constructs (error_p)
6047 /************************************************
6051 ************************************************/
6053 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
6055 /* A flag denoting whether or not ignoreeof is set. */
6058 /* The number of times that we have encountered an EOF character without
6059 another character intervening. When this gets above the limit, the
6060 shell terminates. */
6061 int eof_encountered = 0;
6063 /* The limit for eof_encountered. */
6064 int eof_encountered_limit = 10;
6066 /* If we have EOF as the only input unit, this user wants to leave
6067 the shell. If the shell is not interactive, then just leave.
6068 Otherwise, if ignoreeof is set, and we haven't done this the
6069 required number of times in a row, print a message. */
6071 handle_eof_input_unit ()
6075 /* shell.c may use this to decide whether or not to write out the
6076 history, among other things. We use it only for error reporting
6081 /* If the user wants to "ignore" eof, then let her do so, kind of. */
6084 if (eof_encountered < eof_encountered_limit)
6086 fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
6087 login_shell ? "logout" : "exit");
6089 /* Reset the parsing state. */
6090 last_read_token = current_token = '\n';
6091 /* Reset the prompt string to be $PS1. */
6092 prompt_string_pointer = (char **)NULL;
6098 /* In this case EOF should exit the shell. Do it now. */
6100 exit_builtin ((WORD_LIST *)NULL);
6104 /* We don't write history files, etc., for non-interactive shells. */
6109 /************************************************
6111 * STRING PARSING FUNCTIONS *
6113 ************************************************/
6115 /* It's very important that these two functions treat the characters
6116 between ( and ) identically. */
6118 static WORD_LIST parse_string_error;
6120 /* Take a string and run it through the shell parser, returning the
6121 resultant word list. Used by compound array assignment. */
6123 parse_string_to_word_list (s, flags, whom)
6129 int tok, orig_current_token, orig_line_number, orig_input_terminator;
6130 int orig_line_count;
6131 int old_echo_input, old_expand_aliases;
6132 #if defined (HISTORY)
6133 int old_remember_on_history, old_history_expansion_inhibited;
6136 #if defined (HISTORY)
6137 old_remember_on_history = remember_on_history;
6138 # if defined (BANG_HISTORY)
6139 old_history_expansion_inhibited = history_expansion_inhibited;
6141 bash_history_disable ();
6144 orig_line_number = line_number;
6145 orig_line_count = current_command_line_count;
6146 orig_input_terminator = shell_input_line_terminator;
6147 old_echo_input = echo_input_at_read;
6148 old_expand_aliases = expand_aliases;
6151 last_read_token = WORD; /* WORD to allow reserved words here */
6152 current_command_line_count = 0;
6153 echo_input_at_read = expand_aliases = 0;
6155 with_input_from_string (s, whom);
6156 wl = (WORD_LIST *)NULL;
6159 parser_state |= PST_COMPASSIGN|PST_REPARSE;
6161 while ((tok = read_token (READ)) != yacc_EOF)
6163 if (tok == '\n' && *bash_input.location.string == '\0')
6165 if (tok == '\n') /* Allow newlines in compound assignments */
6167 if (tok != WORD && tok != ASSIGNMENT_WORD)
6169 line_number = orig_line_number + line_number - 1;
6170 orig_current_token = current_token;
6171 current_token = tok;
6172 yyerror (NULL); /* does the right thing */
6173 current_token = orig_current_token;
6176 wl = &parse_string_error;
6179 wl = make_word_list (yylval.word, wl);
6182 last_read_token = '\n';
6185 #if defined (HISTORY)
6186 remember_on_history = old_remember_on_history;
6187 # if defined (BANG_HISTORY)
6188 history_expansion_inhibited = old_history_expansion_inhibited;
6189 # endif /* BANG_HISTORY */
6190 #endif /* HISTORY */
6192 echo_input_at_read = old_echo_input;
6193 expand_aliases = old_expand_aliases;
6195 current_command_line_count = orig_line_count;
6196 shell_input_line_terminator = orig_input_terminator;
6199 parser_state &= ~(PST_COMPASSIGN|PST_REPARSE);
6201 if (wl == &parse_string_error)
6203 last_command_exit_value = EXECUTION_FAILURE;
6204 if (interactive_shell == 0 && posixly_correct)
6205 jump_to_top_level (FORCE_EOF);
6207 jump_to_top_level (DISCARD);
6210 return (REVERSE_LIST (wl, WORD_LIST *));
6214 parse_compound_assignment (retlenp)
6218 int tok, orig_line_number, orig_token_size, orig_last_token, assignok;
6219 char *saved_token, *ret;
6221 saved_token = token;
6222 orig_token_size = token_buffer_size;
6223 orig_line_number = line_number;
6224 orig_last_token = last_read_token;
6226 last_read_token = WORD; /* WORD to allow reserved words here */
6228 token = (char *)NULL;
6229 token_buffer_size = 0;
6231 assignok = parser_state&PST_ASSIGNOK; /* XXX */
6233 wl = (WORD_LIST *)NULL; /* ( */
6234 parser_state |= PST_COMPASSIGN;
6236 while ((tok = read_token (READ)) != ')')
6238 if (tok == '\n') /* Allow newlines in compound assignments */
6240 if (SHOULD_PROMPT ())
6244 if (tok != WORD && tok != ASSIGNMENT_WORD)
6246 current_token = tok; /* for error reporting */
6247 if (tok == yacc_EOF) /* ( */
6248 parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'"));
6250 yyerror(NULL); /* does the right thing */
6253 wl = &parse_string_error;
6256 wl = make_word_list (yylval.word, wl);
6260 token = saved_token;
6261 token_buffer_size = orig_token_size;
6263 parser_state &= ~PST_COMPASSIGN;
6265 if (wl == &parse_string_error)
6267 last_command_exit_value = EXECUTION_FAILURE;
6268 last_read_token = '\n'; /* XXX */
6269 if (interactive_shell == 0 && posixly_correct)
6270 jump_to_top_level (FORCE_EOF);
6272 jump_to_top_level (DISCARD);
6275 last_read_token = orig_last_token; /* XXX - was WORD? */
6279 rl = REVERSE_LIST (wl, WORD_LIST *);
6280 ret = string_list (rl);
6287 *retlenp = (ret && *ret) ? strlen (ret) : 0;
6290 parser_state |= PST_ASSIGNOK;
6295 /************************************************
6297 * SAVING AND RESTORING PARTIAL PARSE STATE *
6299 ************************************************/
6302 save_parser_state (ps)
6303 sh_parser_state_t *ps;
6308 ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t));
6310 return ((sh_parser_state_t *)NULL);
6312 ps->parser_state = parser_state;
6313 ps->token_state = save_token_state ();
6315 ps->input_line_terminator = shell_input_line_terminator;
6316 ps->eof_encountered = eof_encountered;
6318 ps->prompt_string_pointer = prompt_string_pointer;
6320 ps->current_command_line_count = current_command_line_count;
6322 #if defined (HISTORY)
6323 ps->remember_on_history = remember_on_history;
6324 # if defined (BANG_HISTORY)
6325 ps->history_expansion_inhibited = history_expansion_inhibited;
6329 ps->last_command_exit_value = last_command_exit_value;
6330 #if defined (ARRAY_VARS)
6331 ps->pipestatus = save_pipestatus_array ();
6334 ps->last_shell_builtin = last_shell_builtin;
6335 ps->this_shell_builtin = this_shell_builtin;
6337 ps->expand_aliases = expand_aliases;
6338 ps->echo_input_at_read = echo_input_at_read;
6339 ps->need_here_doc = need_here_doc;
6340 ps->here_doc_first_line = here_doc_first_line;
6343 for (i = 0; i < HEREDOC_MAX; i++)
6344 ps->redir_stack[i] = redir_stack[i];
6346 if (need_here_doc == 0)
6347 ps->redir_stack[0] = 0;
6349 memcpy (ps->redir_stack, redir_stack, sizeof (redir_stack[0]) * HEREDOC_MAX);
6353 ps->token_buffer_size = token_buffer_size;
6354 /* Force reallocation on next call to read_token_word */
6356 token_buffer_size = 0;
6362 restore_parser_state (ps)
6363 sh_parser_state_t *ps;
6370 parser_state = ps->parser_state;
6371 if (ps->token_state)
6373 restore_token_state (ps->token_state);
6374 free (ps->token_state);
6377 shell_input_line_terminator = ps->input_line_terminator;
6378 eof_encountered = ps->eof_encountered;
6380 prompt_string_pointer = ps->prompt_string_pointer;
6382 current_command_line_count = ps->current_command_line_count;
6384 #if defined (HISTORY)
6385 remember_on_history = ps->remember_on_history;
6386 # if defined (BANG_HISTORY)
6387 history_expansion_inhibited = ps->history_expansion_inhibited;
6391 last_command_exit_value = ps->last_command_exit_value;
6392 #if defined (ARRAY_VARS)
6393 restore_pipestatus_array (ps->pipestatus);
6396 last_shell_builtin = ps->last_shell_builtin;
6397 this_shell_builtin = ps->this_shell_builtin;
6399 expand_aliases = ps->expand_aliases;
6400 echo_input_at_read = ps->echo_input_at_read;
6401 need_here_doc = ps->need_here_doc;
6402 here_doc_first_line = ps->here_doc_first_line;
6405 for (i = 0; i < HEREDOC_MAX; i++)
6406 redir_stack[i] = ps->redir_stack[i];
6408 if (need_here_doc == 0)
6411 memcpy (redir_stack, ps->redir_stack, sizeof (redir_stack[0]) * HEREDOC_MAX);
6416 token_buffer_size = ps->token_buffer_size;
6419 sh_input_line_state_t *
6420 save_input_line_state (ls)
6421 sh_input_line_state_t *ls;
6424 ls = (sh_input_line_state_t *)xmalloc (sizeof (sh_input_line_state_t));
6426 return ((sh_input_line_state_t *)NULL);
6428 ls->input_line = shell_input_line;
6429 ls->input_line_size = shell_input_line_size;
6430 ls->input_line_len = shell_input_line_len;
6431 ls->input_line_index = shell_input_line_index;
6433 /* force reallocation */
6434 shell_input_line = 0;
6435 shell_input_line_size = shell_input_line_len = shell_input_line_index = 0;
6441 restore_input_line_state (ls)
6442 sh_input_line_state_t *ls;
6444 FREE (shell_input_line);
6445 shell_input_line = ls->input_line;
6446 shell_input_line_size = ls->input_line_size;
6447 shell_input_line_len = ls->input_line_len;
6448 shell_input_line_index = ls->input_line_index;
6450 set_line_mbstate ();
6453 /************************************************
6455 * MULTIBYTE CHARACTER HANDLING *
6457 ************************************************/
6459 #if defined (HANDLE_MULTIBYTE)
6464 size_t i, previ, len;
6465 mbstate_t mbs, prevs;
6468 if (shell_input_line == NULL)
6470 len = strlen (shell_input_line); /* XXX - shell_input_line_len ? */
6471 shell_input_line_property = (char *)xrealloc (shell_input_line_property, len + 1);
6473 memset (&prevs, '\0', sizeof (mbstate_t));
6474 for (i = previ = 0; i < len; i++)
6478 c = shell_input_line[i];
6482 for (j = i; j < len; j++)
6483 shell_input_line_property[j] = 1;
6487 mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs);
6488 if (mbclen == 1 || mbclen == (size_t)-1)
6493 else if (mbclen == (size_t)-2)
6495 else if (mbclen > 1)
6503 /* XXX - what to do if mbrlen returns 0? (null wide character) */
6505 for (j = i; j < len; j++)
6506 shell_input_line_property[j] = 1;
6510 shell_input_line_property[i] = mbclen;
6513 #endif /* HANDLE_MULTIBYTE */