1 /* parse.y - Yacc grammar for bash. */
3 /* Copyright (C) 1989-2012 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)
72 #endif /* JOB_CONTROL */
77 typedef void *alias_t;
80 #if defined (PROMPT_STRING_DECODE)
82 # include <sys/param.h>
85 # if defined (TM_IN_SYS_TIME)
86 # include <sys/types.h>
87 # include <sys/time.h>
88 # endif /* TM_IN_SYS_TIME */
90 #endif /* PROMPT_STRING_DECODE */
92 #define RE_READ_TOKEN -99
93 #define NO_EXPANSION -100
101 #if defined (HANDLE_MULTIBYTE)
102 # define last_shell_getc_is_singlebyte \
103 ((shell_input_line_index > 1) \
104 ? shell_input_line_property[shell_input_line_index - 1] \
106 # define MBTEST(x) ((x) && last_shell_getc_is_singlebyte)
108 # define last_shell_getc_is_singlebyte 1
109 # define MBTEST(x) ((x))
112 #if defined (EXTENDED_GLOB)
113 extern int extended_glob;
116 extern int eof_encountered;
117 extern int no_line_editing, running_under_emacs;
118 extern int current_command_number;
119 extern int sourcelevel, parse_and_execute_level;
120 extern int posixly_correct;
121 extern int last_command_exit_value;
122 extern pid_t last_command_subst_pid;
123 extern char *shell_name, *current_host_name;
124 extern char *dist_version;
125 extern int patch_level;
126 extern int dump_translatable_strings, dump_po_strings;
127 extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
128 #if defined (BUFFERED_INPUT)
129 extern int bash_input_fd_changed;
133 /* **************************************************************** */
135 /* "Forward" declarations */
137 /* **************************************************************** */
140 static void debug_parser __P((int));
143 static int yy_getc __P((void));
144 static int yy_ungetc __P((int));
146 #if defined (READLINE)
147 static int yy_readline_get __P((void));
148 static int yy_readline_unget __P((int));
151 static int yy_string_get __P((void));
152 static int yy_string_unget __P((int));
153 static void rewind_input_string __P((void));
154 static int yy_stream_get __P((void));
155 static int yy_stream_unget __P((int));
157 static int shell_getc __P((int));
158 static void shell_ungetc __P((int));
159 static void discard_until __P((int));
161 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
162 static void push_string __P((char *, int, alias_t *));
163 static void pop_string __P((void));
164 static void free_string_list __P((void));
167 static char *read_a_line __P((int));
169 static int reserved_word_acceptable __P((int));
170 static int yylex __P((void));
172 static void push_heredoc __P((REDIRECT *));
173 static char *mk_alexpansion __P((char *));
174 static int alias_expand_token __P((char *));
175 static int time_command_acceptable __P((void));
176 static int special_case_tokens __P((char *));
177 static int read_token __P((int));
178 static char *parse_matched_pair __P((int, int, int, int *, int));
179 static char *parse_comsub __P((int, int, int, int *, int));
180 #if defined (ARRAY_VARS)
181 static char *parse_compound_assignment __P((int *));
183 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
184 static int parse_dparen __P((int));
185 static int parse_arith_cmd __P((char **, int));
187 #if defined (COND_COMMAND)
188 static void cond_error __P((void));
189 static COND_COM *cond_expr __P((void));
190 static COND_COM *cond_or __P((void));
191 static COND_COM *cond_and __P((void));
192 static COND_COM *cond_term __P((void));
193 static int cond_skip_newlines __P((void));
194 static COMMAND *parse_cond_command __P((void));
196 #if defined (ARRAY_VARS)
197 static int token_is_assignment __P((char *, int));
198 static int token_is_ident __P((char *, int));
200 static int read_token_word __P((int));
201 static void discard_parser_constructs __P((int));
203 static char *error_token_from_token __P((int));
204 static char *error_token_from_text __P((void));
205 static void print_offending_line __P((void));
206 static void report_syntax_error __P((char *));
208 static void handle_eof_input_unit __P((void));
209 static void prompt_again __P((void));
211 static void reset_readline_prompt __P((void));
213 static void print_prompt __P((void));
215 #if defined (HANDLE_MULTIBYTE)
216 static void set_line_mbstate __P((void));
217 static char *shell_input_line_property = NULL;
219 # define set_line_mbstate()
222 extern int yyerror __P((const char *));
228 /* Default prompt strings */
229 char *primary_prompt = PPROMPT;
230 char *secondary_prompt = SPROMPT;
232 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
233 char *ps1_prompt, *ps2_prompt;
235 /* Handle on the current prompt string. Indirectly points through
236 ps1_ or ps2_prompt. */
237 char **prompt_string_pointer = (char **)NULL;
238 char *current_prompt_string;
240 /* Non-zero means we expand aliases in commands. */
241 int expand_aliases = 0;
243 /* If non-zero, the decoded prompt string undergoes parameter and
244 variable substitution, command substitution, arithmetic substitution,
245 string expansion, process substitution, and quote removal in
246 decode_prompt_string. */
249 /* If non-zero, $'...' and $"..." are expanded when they appear within
250 a ${...} expansion, even when the expansion appears within double
252 int extended_quote = 1;
254 /* The number of lines read from input while creating the current command. */
255 int current_command_line_count;
257 /* The number of lines in a command saved while we run parse_and_execute */
258 int saved_command_line_count;
260 /* The token that currently denotes the end of parse. */
263 /* The token currently being read. */
266 /* The current parser state. */
269 /* Variables to manage the task of reading here documents, because we need to
270 defer the reading until after a complete command has been collected. */
271 #define HEREDOC_MAX 16
273 static REDIRECT *redir_stack[HEREDOC_MAX];
276 /* Where shell input comes from. History expansion is performed on each
277 line when the shell is interactive. */
278 static char *shell_input_line = (char *)NULL;
279 static size_t shell_input_line_index;
280 static size_t shell_input_line_size; /* Amount allocated for shell_input_line. */
281 static size_t shell_input_line_len; /* strlen (shell_input_line) */
283 /* Either zero or EOF. */
284 static int shell_input_line_terminator;
286 /* The line number in a script on which a function definition starts. */
287 static int function_dstart;
289 /* The line number in a script on which a function body starts. */
290 static int function_bstart;
292 /* The line number in a script at which an arithmetic for command starts. */
293 static int arith_for_lineno;
295 /* The decoded prompt string. Used if READLINE is not defined or if
296 editing is turned off. Analogous to current_readline_prompt. */
297 static char *current_decoded_prompt;
299 /* The last read token, or NULL. read_token () uses this for context
301 static int last_read_token;
303 /* The token read prior to last_read_token. */
304 static int token_before_that;
306 /* The token read prior to token_before_that. */
307 static int two_tokens_ago;
309 static int global_extglob;
311 /* The line number in a script where the word in a `case WORD', `select WORD'
312 or `for WORD' begins. This is a nested command maximum, since the array
313 index is decremented after a case, select, or for command is parsed. */
314 #define MAX_CASE_NEST 128
315 static int word_lineno[MAX_CASE_NEST+1];
316 static int word_top = -1;
318 /* If non-zero, it is the token that we want read_token to return
319 regardless of what text is (or isn't) present to be read. This
320 is reset by read_token. If token_to_read == WORD or
321 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
322 static int token_to_read;
323 static WORD_DESC *word_desc_to_read;
325 static REDIRECTEE source;
326 static REDIRECTEE redir;
330 WORD_DESC *word; /* the word that we read. */
331 int number; /* the number that we read. */
332 WORD_LIST *word_list;
336 PATTERN_LIST *pattern;
339 /* Reserved words. Members of the first group are only recognized
340 in the case that they are preceded by a list_terminator. Members
341 of the second group are for [[...]] commands. Members of the
342 third group are recognized only under special circumstances. */
343 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION COPROC
344 %token COND_START COND_END COND_ERROR
345 %token IN BANG TIME TIMEOPT TIMEIGN
347 /* More general tokens. yylex () knows how to make these. */
348 %token <word> WORD ASSIGNMENT_WORD REDIR_WORD
349 %token <number> NUMBER
350 %token <word_list> ARITH_CMD ARITH_FOR_EXPRS
351 %token <command> COND_CMD
352 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS
353 %token GREATER_AND SEMI_SEMI SEMI_AND SEMI_SEMI_AND
354 %token LESS_LESS_MINUS AND_GREATER AND_GREATER_GREATER LESS_GREATER
355 %token GREATER_BAR BAR_AND
357 /* The types that the various syntactical units return. */
359 %type <command> inputunit command pipeline pipeline_command
360 %type <command> list list0 list1 compound_list simple_list simple_list1
361 %type <command> simple_command shell_command
362 %type <command> for_command select_command case_command group_command
363 %type <command> arith_command
364 %type <command> cond_command
365 %type <command> arith_for_command
366 %type <command> coproc
367 %type <command> function_def function_body if_command elif_clause subshell
368 %type <redirect> redirection redirection_list
369 %type <element> simple_command_element
370 %type <word_list> word_list pattern
371 %type <pattern> pattern_list case_clause_sequence case_clause
372 %type <number> timespec
373 %type <number> list_terminator
377 %left '&' ';' '\n' yacc_EOF
382 inputunit: simple_list simple_list_terminator
384 /* Case of regular command. Discard the error
385 safety net,and return the command just parsed. */
388 /* discard_parser_constructs (0); */
389 if (parser_state & PST_CMDSUBST)
390 parser_state |= PST_EOFTOKEN;
395 /* Case of regular command, but not a very
396 interesting one. Return a NULL command. */
397 global_command = (COMMAND *)NULL;
398 if (parser_state & PST_CMDSUBST)
399 parser_state |= PST_EOFTOKEN;
404 /* Error during parsing. Return NULL command. */
405 global_command = (COMMAND *)NULL;
407 /* discard_parser_constructs (1); */
408 if (interactive && parse_and_execute_level == 0)
419 /* Case of EOF seen by itself. Do ignoreeof or
421 global_command = (COMMAND *)NULL;
422 handle_eof_input_unit ();
428 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
430 { $$ = make_word_list ($2, $1); }
433 redirection: '>' WORD
437 $$ = make_redirection (source, r_output_direction, redir, 0);
443 $$ = make_redirection (source, r_input_direction, redir, 0);
449 $$ = make_redirection (source, r_output_direction, redir, 0);
455 $$ = make_redirection (source, r_input_direction, redir, 0);
457 | REDIR_WORD '>' WORD
459 source.filename = $1;
461 $$ = make_redirection (source, r_output_direction, redir, REDIR_VARASSIGN);
463 | REDIR_WORD '<' WORD
465 source.filename = $1;
467 $$ = make_redirection (source, r_input_direction, redir, REDIR_VARASSIGN);
469 | GREATER_GREATER WORD
473 $$ = make_redirection (source, r_appending_to, redir, 0);
475 | NUMBER GREATER_GREATER WORD
479 $$ = make_redirection (source, r_appending_to, redir, 0);
481 | REDIR_WORD GREATER_GREATER WORD
483 source.filename = $1;
485 $$ = make_redirection (source, r_appending_to, redir, REDIR_VARASSIGN);
491 $$ = make_redirection (source, r_output_force, redir, 0);
493 | NUMBER GREATER_BAR WORD
497 $$ = make_redirection (source, r_output_force, redir, 0);
499 | REDIR_WORD GREATER_BAR WORD
501 source.filename = $1;
503 $$ = make_redirection (source, r_output_force, redir, REDIR_VARASSIGN);
509 $$ = make_redirection (source, r_input_output, redir, 0);
511 | NUMBER LESS_GREATER WORD
515 $$ = make_redirection (source, r_input_output, redir, 0);
517 | REDIR_WORD LESS_GREATER WORD
519 source.filename = $1;
521 $$ = make_redirection (source, r_input_output, redir, REDIR_VARASSIGN);
527 $$ = make_redirection (source, r_reading_until, redir, 0);
530 | NUMBER LESS_LESS WORD
534 $$ = make_redirection (source, r_reading_until, redir, 0);
537 | REDIR_WORD LESS_LESS WORD
539 source.filename = $1;
541 $$ = make_redirection (source, r_reading_until, redir, REDIR_VARASSIGN);
544 | LESS_LESS_MINUS WORD
548 $$ = make_redirection (source, r_deblank_reading_until, redir, 0);
551 | NUMBER LESS_LESS_MINUS WORD
555 $$ = make_redirection (source, r_deblank_reading_until, redir, 0);
558 | REDIR_WORD LESS_LESS_MINUS WORD
560 source.filename = $1;
562 $$ = make_redirection (source, r_deblank_reading_until, redir, REDIR_VARASSIGN);
565 | LESS_LESS_LESS WORD
569 $$ = make_redirection (source, r_reading_string, redir, 0);
571 | NUMBER LESS_LESS_LESS WORD
575 $$ = make_redirection (source, r_reading_string, redir, 0);
577 | REDIR_WORD LESS_LESS_LESS WORD
579 source.filename = $1;
581 $$ = make_redirection (source, r_reading_string, redir, REDIR_VARASSIGN);
587 $$ = make_redirection (source, r_duplicating_input, redir, 0);
589 | NUMBER LESS_AND NUMBER
593 $$ = make_redirection (source, r_duplicating_input, redir, 0);
595 | REDIR_WORD LESS_AND NUMBER
597 source.filename = $1;
599 $$ = make_redirection (source, r_duplicating_input, redir, REDIR_VARASSIGN);
605 $$ = make_redirection (source, r_duplicating_output, redir, 0);
607 | NUMBER GREATER_AND NUMBER
611 $$ = make_redirection (source, r_duplicating_output, redir, 0);
613 | REDIR_WORD GREATER_AND NUMBER
615 source.filename = $1;
617 $$ = make_redirection (source, r_duplicating_output, redir, REDIR_VARASSIGN);
623 $$ = make_redirection (source, r_duplicating_input_word, redir, 0);
625 | NUMBER LESS_AND WORD
629 $$ = make_redirection (source, r_duplicating_input_word, redir, 0);
631 | REDIR_WORD LESS_AND WORD
633 source.filename = $1;
635 $$ = make_redirection (source, r_duplicating_input_word, redir, REDIR_VARASSIGN);
641 $$ = make_redirection (source, r_duplicating_output_word, redir, 0);
643 | NUMBER GREATER_AND WORD
647 $$ = make_redirection (source, r_duplicating_output_word, redir, 0);
649 | REDIR_WORD GREATER_AND WORD
651 source.filename = $1;
653 $$ = make_redirection (source, r_duplicating_output_word, redir, REDIR_VARASSIGN);
659 $$ = make_redirection (source, r_close_this, redir, 0);
661 | NUMBER GREATER_AND '-'
665 $$ = make_redirection (source, r_close_this, redir, 0);
667 | REDIR_WORD GREATER_AND '-'
669 source.filename = $1;
671 $$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
677 $$ = make_redirection (source, r_close_this, redir, 0);
679 | NUMBER LESS_AND '-'
683 $$ = make_redirection (source, r_close_this, redir, 0);
685 | REDIR_WORD LESS_AND '-'
687 source.filename = $1;
689 $$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
695 $$ = make_redirection (source, r_err_and_out, redir, 0);
697 | AND_GREATER_GREATER WORD
701 $$ = make_redirection (source, r_append_err_and_out, redir, 0);
705 simple_command_element: WORD
706 { $$.word = $1; $$.redirect = 0; }
708 { $$.word = $1; $$.redirect = 0; }
710 { $$.redirect = $1; $$.word = 0; }
713 redirection_list: redirection
717 | redirection_list redirection
719 register REDIRECT *t;
721 for (t = $1; t->next; t = t->next)
728 simple_command: simple_command_element
729 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
730 | simple_command simple_command_element
731 { $$ = make_simple_command ($2, $1); }
734 command: simple_command
735 { $$ = clean_simple_command ($1); }
738 | shell_command redirection_list
745 register REDIRECT *t;
746 for (t = tc->redirects; t->next; t = t->next)
760 shell_command: for_command
764 | WHILE compound_list DO compound_list DONE
765 { $$ = make_while_command ($2, $4); }
766 | UNTIL compound_list DO compound_list DONE
767 { $$ = make_until_command ($2, $4); }
784 for_command: FOR WORD newline_list DO compound_list DONE
786 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
787 if (word_top > 0) word_top--;
789 | FOR WORD newline_list '{' compound_list '}'
791 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
792 if (word_top > 0) word_top--;
794 | FOR WORD ';' newline_list DO compound_list DONE
796 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
797 if (word_top > 0) word_top--;
799 | FOR WORD ';' newline_list '{' compound_list '}'
801 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
802 if (word_top > 0) word_top--;
804 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
806 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
807 if (word_top > 0) word_top--;
809 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
811 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
812 if (word_top > 0) word_top--;
814 | FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
816 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
817 if (word_top > 0) word_top--;
819 | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
821 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
822 if (word_top > 0) word_top--;
826 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
828 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
829 if (word_top > 0) word_top--;
831 | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
833 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
834 if (word_top > 0) word_top--;
836 | FOR ARITH_FOR_EXPRS DO compound_list DONE
838 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
839 if (word_top > 0) word_top--;
841 | FOR ARITH_FOR_EXPRS '{' compound_list '}'
843 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
844 if (word_top > 0) word_top--;
848 select_command: SELECT WORD newline_list DO list DONE
850 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
851 if (word_top > 0) word_top--;
853 | SELECT WORD newline_list '{' list '}'
855 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
856 if (word_top > 0) word_top--;
858 | SELECT WORD ';' newline_list DO list DONE
860 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
861 if (word_top > 0) word_top--;
863 | SELECT WORD ';' newline_list '{' list '}'
865 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
866 if (word_top > 0) word_top--;
868 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
870 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
871 if (word_top > 0) word_top--;
873 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
875 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
876 if (word_top > 0) word_top--;
880 case_command: CASE WORD newline_list IN newline_list ESAC
882 $$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]);
883 if (word_top > 0) word_top--;
885 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
887 $$ = make_case_command ($2, $5, word_lineno[word_top]);
888 if (word_top > 0) word_top--;
890 | CASE WORD newline_list IN case_clause ESAC
892 $$ = make_case_command ($2, $5, word_lineno[word_top]);
893 if (word_top > 0) word_top--;
897 function_def: WORD '(' ')' newline_list function_body
898 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
900 | FUNCTION WORD '(' ')' newline_list function_body
901 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
903 | FUNCTION WORD newline_list function_body
904 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
907 function_body: shell_command
909 | shell_command redirection_list
914 /* According to Posix.2 3.9.5, redirections
915 specified after the body of a function should
916 be attached to the function and performed when
917 the function is executed, not as part of the
918 function definition command. */
919 /* XXX - I don't think it matters, but we might
920 want to change this in the future to avoid
921 problems differentiating between a function
922 definition with a redirection and a function
923 definition containing a single command with a
924 redirection. The two are semantically equivalent,
925 though -- the only difference is in how the
926 command printing code displays the redirections. */
929 register REDIRECT *t;
930 for (t = tc->redirects; t->next; t = t->next)
940 subshell: '(' compound_list ')'
942 $$ = make_subshell_command ($2);
943 $$->flags |= CMD_WANT_SUBSHELL;
947 coproc: COPROC shell_command
949 $$ = make_coproc_command ("COPROC", $2);
950 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
952 | COPROC shell_command redirection_list
959 register REDIRECT *t;
960 for (t = tc->redirects; t->next; t = t->next)
966 $$ = make_coproc_command ("COPROC", $2);
967 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
969 | COPROC WORD shell_command
971 $$ = make_coproc_command ($2->word, $3);
972 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
974 | COPROC WORD shell_command redirection_list
981 register REDIRECT *t;
982 for (t = tc->redirects; t->next; t = t->next)
988 $$ = make_coproc_command ($2->word, $3);
989 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
991 | COPROC simple_command
993 $$ = make_coproc_command ("COPROC", clean_simple_command ($2));
994 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
998 if_command: IF compound_list THEN compound_list FI
999 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
1000 | IF compound_list THEN compound_list ELSE compound_list FI
1001 { $$ = make_if_command ($2, $4, $6); }
1002 | IF compound_list THEN compound_list elif_clause FI
1003 { $$ = make_if_command ($2, $4, $5); }
1007 group_command: '{' compound_list '}'
1008 { $$ = make_group_command ($2); }
1011 arith_command: ARITH_CMD
1012 { $$ = make_arith_command ($1); }
1015 cond_command: COND_START COND_CMD COND_END
1019 elif_clause: ELIF compound_list THEN compound_list
1020 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
1021 | ELIF compound_list THEN compound_list ELSE compound_list
1022 { $$ = make_if_command ($2, $4, $6); }
1023 | ELIF compound_list THEN compound_list elif_clause
1024 { $$ = make_if_command ($2, $4, $5); }
1027 case_clause: pattern_list
1028 | case_clause_sequence pattern_list
1029 { $2->next = $1; $$ = $2; }
1032 pattern_list: newline_list pattern ')' compound_list
1033 { $$ = make_pattern_list ($2, $4); }
1034 | newline_list pattern ')' newline_list
1035 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
1036 | newline_list '(' pattern ')' compound_list
1037 { $$ = make_pattern_list ($3, $5); }
1038 | newline_list '(' pattern ')' newline_list
1039 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
1042 case_clause_sequence: pattern_list SEMI_SEMI
1044 | case_clause_sequence pattern_list SEMI_SEMI
1045 { $2->next = $1; $$ = $2; }
1046 | pattern_list SEMI_AND
1047 { $1->flags |= CASEPAT_FALLTHROUGH; $$ = $1; }
1048 | case_clause_sequence pattern_list SEMI_AND
1049 { $2->flags |= CASEPAT_FALLTHROUGH; $2->next = $1; $$ = $2; }
1050 | pattern_list SEMI_SEMI_AND
1051 { $1->flags |= CASEPAT_TESTNEXT; $$ = $1; }
1052 | case_clause_sequence pattern_list SEMI_SEMI_AND
1053 { $2->flags |= CASEPAT_TESTNEXT; $2->next = $1; $$ = $2; }
1057 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
1059 { $$ = make_word_list ($3, $1); }
1062 /* A list allows leading or trailing newlines and
1063 newlines as operators (equivalent to semicolons).
1064 It must end with a newline or semicolon.
1065 Lists are used within commands such as if, for, while. */
1067 list: newline_list list0
1071 gather_here_documents ();
1076 | newline_list list1
1082 list0: list1 '\n' newline_list
1083 | list1 '&' newline_list
1085 if ($1->type == cm_connection)
1086 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
1088 $$ = command_connect ($1, (COMMAND *)NULL, '&');
1090 | list1 ';' newline_list
1094 list1: list1 AND_AND newline_list list1
1095 { $$ = command_connect ($1, $4, AND_AND); }
1096 | list1 OR_OR newline_list list1
1097 { $$ = command_connect ($1, $4, OR_OR); }
1098 | list1 '&' newline_list list1
1100 if ($1->type == cm_connection)
1101 $$ = connect_async_list ($1, $4, '&');
1103 $$ = command_connect ($1, $4, '&');
1105 | list1 ';' newline_list list1
1106 { $$ = command_connect ($1, $4, ';'); }
1107 | list1 '\n' newline_list list1
1108 { $$ = command_connect ($1, $4, ';'); }
1113 simple_list_terminator: '\n'
1117 list_terminator:'\n'
1129 /* A simple_list is a list that contains no significant newlines
1130 and no leading or trailing newlines. Newlines are allowed
1131 only following operators, where they are not significant.
1133 This is what an inputunit consists of. */
1135 simple_list: simple_list1
1139 gather_here_documents ();
1140 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1142 global_command = $1;
1143 eof_encountered = 0;
1144 rewind_input_string ();
1150 if ($1->type == cm_connection)
1151 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
1153 $$ = command_connect ($1, (COMMAND *)NULL, '&');
1155 gather_here_documents ();
1156 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1158 global_command = $1;
1159 eof_encountered = 0;
1160 rewind_input_string ();
1168 gather_here_documents ();
1169 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1171 global_command = $1;
1172 eof_encountered = 0;
1173 rewind_input_string ();
1179 simple_list1: simple_list1 AND_AND newline_list simple_list1
1180 { $$ = command_connect ($1, $4, AND_AND); }
1181 | simple_list1 OR_OR newline_list simple_list1
1182 { $$ = command_connect ($1, $4, OR_OR); }
1183 | simple_list1 '&' simple_list1
1185 if ($1->type == cm_connection)
1186 $$ = connect_async_list ($1, $3, '&');
1188 $$ = command_connect ($1, $3, '&');
1190 | simple_list1 ';' simple_list1
1191 { $$ = command_connect ($1, $3, ';'); }
1197 pipeline_command: pipeline
1199 | BANG pipeline_command
1202 $2->flags ^= CMD_INVERT_RETURN; /* toggle */
1205 | timespec pipeline_command
1211 | timespec list_terminator
1215 /* Boy, this is unclean. `time' by itself can
1216 time a null command. We cheat and push a
1217 newline back if the list_terminator was a newline
1218 to avoid the double-newline problem (one to
1219 terminate this, one to terminate the command) */
1222 $$ = make_simple_command (x, (COMMAND *)NULL);
1224 /* XXX - let's cheat and push a newline back */
1226 token_to_read = '\n';
1228 | BANG list_terminator
1232 /* This is just as unclean. Posix says that `!'
1233 by itself should be equivalent to `false'.
1235 newline back if the list_terminator was a newline
1236 to avoid the double-newline problem (one to
1237 terminate this, one to terminate the command) */
1240 $$ = make_simple_command (x, (COMMAND *)NULL);
1241 $$->flags |= CMD_INVERT_RETURN;
1242 /* XXX - let's cheat and push a newline back */
1244 token_to_read = '\n';
1248 pipeline: pipeline '|' newline_list pipeline
1249 { $$ = command_connect ($1, $4, '|'); }
1250 | pipeline BAR_AND newline_list pipeline
1252 /* Make cmd1 |& cmd2 equivalent to cmd1 2>&1 | cmd2 */
1257 tc = $1->type == cm_simple ? (COMMAND *)$1->value.Simple : $1;
1260 r = make_redirection (sd, r_duplicating_output, rd, 0);
1263 register REDIRECT *t;
1264 for (t = tc->redirects; t->next; t = t->next)
1271 $$ = command_connect ($1, $4, '|');
1278 { $$ = CMD_TIME_PIPELINE; }
1280 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1281 | TIME TIMEOPT TIMEIGN
1282 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1286 /* Initial size to allocate for tokens, and the
1287 amount to grow them by. */
1288 #define TOKEN_DEFAULT_INITIAL_SIZE 496
1289 #define TOKEN_DEFAULT_GROW_SIZE 512
1291 /* Should we call prompt_again? */
1292 #define SHOULD_PROMPT() \
1293 (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
1296 # define expanding_alias() (pushed_string_list && pushed_string_list->expander)
1298 # define expanding_alias() 0
1301 /* Global var is non-zero when end of file has been reached. */
1302 int EOF_Reached = 0;
1315 /* yy_getc () returns the next available character from input or EOF.
1316 yy_ungetc (c) makes `c' the next character to read.
1317 init_yy_io (get, unget, type, location) makes the function GET the
1318 installed function for getting the next character, makes UNGET the
1319 installed function for un-getting a character, sets the type of stream
1320 (either string or file) from TYPE, and makes LOCATION point to where
1321 the input is coming from. */
1323 /* Unconditionally returns end-of-file. */
1330 /* Variable containing the current get and unget functions.
1331 See ./input.h for a clearer description. */
1332 BASH_INPUT bash_input;
1334 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
1335 is non-null, avoiding a memory leak. */
1337 initialize_bash_input ()
1339 bash_input.type = st_none;
1340 FREE (bash_input.name);
1341 bash_input.name = (char *)NULL;
1342 bash_input.location.file = (FILE *)NULL;
1343 bash_input.location.string = (char *)NULL;
1344 bash_input.getter = (sh_cget_func_t *)NULL;
1345 bash_input.ungetter = (sh_cunget_func_t *)NULL;
1348 /* Set the contents of the current bash input stream from
1349 GET, UNGET, TYPE, NAME, and LOCATION. */
1351 init_yy_io (get, unget, type, name, location)
1352 sh_cget_func_t *get;
1353 sh_cunget_func_t *unget;
1354 enum stream_type type;
1356 INPUT_STREAM location;
1358 bash_input.type = type;
1359 FREE (bash_input.name);
1360 bash_input.name = name ? savestring (name) : (char *)NULL;
1364 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
1366 bash_input.location = location;
1368 bash_input.getter = get;
1369 bash_input.ungetter = unget;
1375 return (bash_input.name ? bash_input.name : "stdin");
1378 /* Call this to get the next character of input. */
1382 return (*(bash_input.getter)) ();
1385 /* Call this to unget C. That is, to make C the next character
1391 return (*(bash_input.ungetter)) (c);
1394 #if defined (BUFFERED_INPUT)
1395 #ifdef INCLUDE_UNUSED
1397 input_file_descriptor ()
1399 switch (bash_input.type)
1402 return (fileno (bash_input.location.file));
1404 return (bash_input.location.buffered_fd);
1407 return (fileno (stdin));
1411 #endif /* BUFFERED_INPUT */
1413 /* **************************************************************** */
1415 /* Let input be read from readline (). */
1417 /* **************************************************************** */
1419 #if defined (READLINE)
1420 char *current_readline_prompt = (char *)NULL;
1421 char *current_readline_line = (char *)NULL;
1422 int current_readline_line_index = 0;
1427 SigHandler *old_sigint;
1431 if (!current_readline_line)
1433 if (!bash_readline_initialized)
1434 initialize_readline ();
1436 #if defined (JOB_CONTROL)
1438 give_terminal_to (shell_pgrp, 0);
1439 #endif /* JOB_CONTROL */
1441 old_sigint = (SigHandler *)IMPOSSIBLE_TRAP_HANDLER;
1442 if (signal_is_ignored (SIGINT) == 0)
1444 /* interrupt_immediately++; */
1445 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
1448 current_readline_line = readline (current_readline_prompt ?
1449 current_readline_prompt : "");
1452 if (signal_is_ignored (SIGINT) == 0)
1454 /* interrupt_immediately--; */
1455 if (old_sigint != IMPOSSIBLE_TRAP_HANDLER)
1456 set_signal_handler (SIGINT, old_sigint);
1460 /* Reset the prompt to the decoded value of prompt_string_pointer. */
1461 reset_readline_prompt ();
1464 if (current_readline_line == 0)
1467 current_readline_line_index = 0;
1468 line_len = strlen (current_readline_line);
1470 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
1471 current_readline_line[line_len++] = '\n';
1472 current_readline_line[line_len] = '\0';
1475 if (current_readline_line[current_readline_line_index] == 0)
1477 free (current_readline_line);
1478 current_readline_line = (char *)NULL;
1479 return (yy_readline_get ());
1483 c = current_readline_line[current_readline_line_index++];
1489 yy_readline_unget (c)
1492 if (current_readline_line_index && current_readline_line)
1493 current_readline_line[--current_readline_line_index] = c;
1498 with_input_from_stdin ()
1500 INPUT_STREAM location;
1502 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1504 location.string = current_readline_line;
1505 init_yy_io (yy_readline_get, yy_readline_unget,
1506 st_stdin, "readline stdin", location);
1510 #else /* !READLINE */
1513 with_input_from_stdin ()
1515 with_input_from_stream (stdin, "stdin");
1517 #endif /* !READLINE */
1519 /* **************************************************************** */
1521 /* Let input come from STRING. STRING is zero terminated. */
1523 /* **************************************************************** */
1528 register char *string;
1529 register unsigned char c;
1531 string = bash_input.location.string;
1533 /* If the string doesn't exist, or is empty, EOF found. */
1534 if (string && *string)
1537 bash_input.location.string = string;
1548 *(--bash_input.location.string) = c;
1553 with_input_from_string (string, name)
1557 INPUT_STREAM location;
1559 location.string = string;
1560 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1563 /* Count the number of characters we've consumed from bash_input.location.string
1564 and read into shell_input_line, but have not returned from shell_getc.
1565 That is the true input location. Rewind bash_input.location.string by
1566 that number of characters, so it points to the last character actually
1567 consumed by the parser. */
1569 rewind_input_string ()
1573 /* number of unconsumed characters in the input -- XXX need to take newlines
1574 into account, e.g., $(...\n) */
1575 xchars = shell_input_line_len - shell_input_line_index;
1576 if (bash_input.location.string[-1] == '\n')
1579 /* XXX - how to reflect bash_input.location.string back to string passed to
1580 parse_and_execute or xparse_dolparen? xparse_dolparen needs to know how
1581 far into the string we parsed. parse_and_execute knows where bash_input.
1582 location.string is, and how far from orig_string that is -- that's the
1583 number of characters the command consumed. */
1585 /* bash_input.location.string - xchars should be where we parsed to */
1586 /* need to do more validation on xchars value for sanity -- test cases. */
1587 bash_input.location.string -= xchars;
1590 /* **************************************************************** */
1592 /* Let input come from STREAM. */
1594 /* **************************************************************** */
1596 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1597 define, and just use getc/ungetc if it was defined, but since bash
1598 installs its signal handlers without the SA_RESTART flag, some signals
1599 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
1600 the read to be restarted. We need to restart it ourselves. */
1608 if (bash_input.location.file)
1612 interrupt_immediately++;
1615 /* XXX - don't need terminate_immediately; getc_with_restart checks
1616 for terminating signals itself if read returns < 0 */
1617 result = getc_with_restart (bash_input.location.file);
1621 interrupt_immediately--;
1631 return (ungetc_with_restart (c, bash_input.location.file));
1635 with_input_from_stream (stream, name)
1639 INPUT_STREAM location;
1641 location.file = stream;
1642 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1645 typedef struct stream_saver {
1646 struct stream_saver *next;
1647 BASH_INPUT bash_input;
1649 #if defined (BUFFERED_INPUT)
1650 BUFFERED_STREAM *bstream;
1651 #endif /* BUFFERED_INPUT */
1654 /* The globally known line number. */
1655 int line_number = 0;
1657 /* The line number offset set by assigning to LINENO. Not currently used. */
1658 int line_number_base = 0;
1660 #if defined (COND_COMMAND)
1661 static int cond_lineno;
1662 static int cond_token;
1665 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1668 push_stream (reset_lineno)
1671 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1673 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1675 #if defined (BUFFERED_INPUT)
1676 saver->bstream = (BUFFERED_STREAM *)NULL;
1677 /* If we have a buffered stream, clear out buffers[fd]. */
1678 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1679 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1680 (BUFFERED_STREAM *)NULL);
1681 #endif /* BUFFERED_INPUT */
1683 saver->line = line_number;
1684 bash_input.name = (char *)NULL;
1685 saver->next = stream_list;
1686 stream_list = saver;
1699 STREAM_SAVER *saver = stream_list;
1702 stream_list = stream_list->next;
1704 init_yy_io (saver->bash_input.getter,
1705 saver->bash_input.ungetter,
1706 saver->bash_input.type,
1707 saver->bash_input.name,
1708 saver->bash_input.location);
1710 #if defined (BUFFERED_INPUT)
1711 /* If we have a buffered stream, restore buffers[fd]. */
1712 /* If the input file descriptor was changed while this was on the
1713 save stack, update the buffered fd to the new file descriptor and
1714 re-establish the buffer <-> bash_input fd correspondence. */
1715 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1717 if (bash_input_fd_changed)
1719 bash_input_fd_changed = 0;
1720 if (default_buffered_input >= 0)
1722 bash_input.location.buffered_fd = default_buffered_input;
1723 saver->bstream->b_fd = default_buffered_input;
1724 SET_CLOSE_ON_EXEC (default_buffered_input);
1727 /* XXX could free buffered stream returned as result here. */
1728 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1730 #endif /* BUFFERED_INPUT */
1732 line_number = saver->line;
1734 FREE (saver->bash_input.name);
1739 /* Return 1 if a stream of type TYPE is saved on the stack. */
1741 stream_on_stack (type)
1742 enum stream_type type;
1744 register STREAM_SAVER *s;
1746 for (s = stream_list; s; s = s->next)
1747 if (s->bash_input.type == type)
1752 /* Save the current token state and return it in a malloced array. */
1758 ret = (int *)xmalloc (4 * sizeof (int));
1759 ret[0] = last_read_token;
1760 ret[1] = token_before_that;
1761 ret[2] = two_tokens_ago;
1762 ret[3] = current_token;
1767 restore_token_state (ts)
1772 last_read_token = ts[0];
1773 token_before_that = ts[1];
1774 two_tokens_ago = ts[2];
1775 current_token = ts[3];
1779 * This is used to inhibit alias expansion and reserved word recognition
1780 * inside case statement pattern lists. A `case statement pattern list' is:
1782 * everything between the `in' in a `case word in' and the next ')'
1784 * everything between a `;;' and the next `)' or `esac'
1787 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1789 #define END_OF_ALIAS 0
1792 * Pseudo-global variables used in implementing token-wise alias expansion.
1796 * Pushing and popping strings. This works together with shell_getc to
1797 * implement alias expansion on a per-token basis.
1800 #define PSH_ALIAS 0x01
1801 #define PSH_DPAREN 0x02
1802 #define PSH_SOURCE 0x04
1804 typedef struct string_saver {
1805 struct string_saver *next;
1806 int expand_alias; /* Value to set expand_alias to when string is popped. */
1809 alias_t *expander; /* alias that caused this line to be pushed. */
1811 size_t saved_line_size, saved_line_index;
1812 int saved_line_terminator;
1816 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1819 * Push the current shell_input_line onto a stack of such lines and make S
1820 * the current input. Used when expanding aliases. EXPAND is used to set
1821 * the value of expand_next_token when the string is popped, so that the
1822 * word after the alias in the original line is handled correctly when the
1823 * alias expands to multiple words. TOKEN is the token that was expanded
1824 * into S; it is saved and used to prevent infinite recursive expansion.
1827 push_string (s, expand, ap)
1832 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
1834 temp->expand_alias = expand;
1835 temp->saved_line = shell_input_line;
1836 temp->saved_line_size = shell_input_line_size;
1837 temp->saved_line_index = shell_input_line_index;
1838 temp->saved_line_terminator = shell_input_line_terminator;
1841 temp->expander = ap;
1843 temp->flags = PSH_ALIAS;
1845 temp->next = pushed_string_list;
1846 pushed_string_list = temp;
1850 ap->flags |= AL_BEINGEXPANDED;
1853 shell_input_line = s;
1854 shell_input_line_size = STRLEN (s);
1855 shell_input_line_index = 0;
1856 shell_input_line_terminator = '\0';
1858 parser_state &= ~PST_ALEXPNEXT; /* XXX */
1861 set_line_mbstate ();
1865 * Make the top of the pushed_string stack be the current shell input.
1866 * Only called when there is something on the stack. Called from shell_getc
1867 * when it thinks it has consumed the string generated by an alias expansion
1868 * and needs to return to the original input line.
1875 FREE (shell_input_line);
1876 shell_input_line = pushed_string_list->saved_line;
1877 shell_input_line_index = pushed_string_list->saved_line_index;
1878 shell_input_line_size = pushed_string_list->saved_line_size;
1879 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1881 if (pushed_string_list->expand_alias)
1882 parser_state |= PST_ALEXPNEXT;
1884 parser_state &= ~PST_ALEXPNEXT;
1886 t = pushed_string_list;
1887 pushed_string_list = pushed_string_list->next;
1891 t->expander->flags &= ~AL_BEINGEXPANDED;
1896 set_line_mbstate ();
1902 register STRING_SAVER *t, *t1;
1904 for (t = pushed_string_list; t; )
1907 FREE (t->saved_line);
1910 t->expander->flags &= ~AL_BEINGEXPANDED;
1915 pushed_string_list = (STRING_SAVER *)NULL;
1918 #endif /* ALIAS || DPAREN_ARITHMETIC */
1921 free_pushed_string_input ()
1923 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1924 free_string_list ();
1929 parser_expanding_alias ()
1931 return (expanding_alias ());
1935 parser_save_alias ()
1937 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1938 push_string ((char *)NULL, 0, (alias_t *)NULL);
1939 pushed_string_list->flags = PSH_SOURCE; /* XXX - for now */
1946 parser_restore_alias ()
1948 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1949 if (pushed_string_list)
1956 /* Return a line of text, taken from wherever yylex () reads input.
1957 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1958 is non-zero, we remove unquoted \<newline> pairs. This is used by
1959 read_secondary_line to read here documents. */
1961 read_a_line (remove_quoted_newline)
1962 int remove_quoted_newline;
1964 static char *line_buffer = (char *)NULL;
1965 static int buffer_size = 0;
1966 int indx, c, peekc, pass_next;
1968 #if defined (READLINE)
1969 if (no_line_editing && SHOULD_PROMPT ())
1971 if (SHOULD_PROMPT ())
1975 pass_next = indx = 0;
1978 /* Allow immediate exit if interrupted during input. */
1983 /* Ignore null bytes in input. */
1987 internal_warning ("read_a_line: ignored null byte in input");
1992 /* If there is no more input, then we return NULL. */
1995 if (interactive && bash_input.type == st_stream)
1998 return ((char *)NULL);
2002 /* `+2' in case the final character in the buffer is a newline. */
2003 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
2005 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
2006 here document with an unquoted delimiter. In this case,
2007 the line will be expanded as if it were in double quotes.
2008 We allow a backslash to escape the next character, but we
2009 need to treat the backslash specially only if a backslash
2010 quoting a backslash-newline pair appears in the line. */
2013 line_buffer[indx++] = c;
2016 else if (c == '\\' && remove_quoted_newline)
2023 continue; /* Make the unquoted \<newline> pair disappear. */
2029 line_buffer[indx++] = c; /* Preserve the backslash. */
2033 line_buffer[indx++] = c;
2037 line_buffer[indx] = '\0';
2038 return (line_buffer);
2043 /* Return a line as in read_a_line (), but insure that the prompt is
2044 the secondary prompt. This is used to read the lines of a here
2045 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
2046 newlines quoted with backslashes while reading the line. It is
2047 non-zero unless the delimiter of the here document was quoted. */
2049 read_secondary_line (remove_quoted_newline)
2050 int remove_quoted_newline;
2055 prompt_string_pointer = &ps2_prompt;
2056 if (SHOULD_PROMPT())
2058 ret = read_a_line (remove_quoted_newline);
2059 #if defined (HISTORY)
2060 if (ret && remember_on_history && (parser_state & PST_HEREDOC))
2062 /* To make adding the the here-document body right, we need to rely
2063 on history_delimiting_chars() returning \n for the first line of
2064 the here-document body and the null string for the second and
2065 subsequent lines, so we avoid double newlines.
2066 current_command_line_count == 2 for the first line of the body. */
2068 current_command_line_count++;
2069 maybe_add_history (ret);
2071 #endif /* HISTORY */
2075 /* **************************************************************** */
2079 /* **************************************************************** */
2081 /* Reserved words. These are only recognized as the first word of a
2083 STRING_INT_ALIST word_token_alist[] = {
2092 #if defined (SELECT_COMMAND)
2093 { "select", SELECT },
2100 { "function", FUNCTION },
2101 #if defined (COMMAND_TIMING)
2107 #if defined (COND_COMMAND)
2108 { "[[", COND_START },
2111 #if defined (COPROCESS_SUPPORT)
2112 { "coproc", COPROC },
2117 /* other tokens that can be returned by read_token() */
2118 STRING_INT_ALIST other_token_alist[] = {
2119 /* Multiple-character tokens with special values */
2124 { ">>", GREATER_GREATER },
2125 { "<<", LESS_LESS },
2127 { ">&", GREATER_AND },
2128 { ";;", SEMI_SEMI },
2130 { ";;&", SEMI_SEMI_AND },
2131 { "<<-", LESS_LESS_MINUS },
2132 { "<<<", LESS_LESS_LESS },
2133 { "&>", AND_GREATER },
2134 { "&>>", AND_GREATER_GREATER },
2135 { "<>", LESS_GREATER },
2136 { ">|", GREATER_BAR },
2138 { "EOF", yacc_EOF },
2139 /* Tokens whose value is the character itself */
2150 { "newline", '\n' },
2154 /* others not listed here:
2155 WORD look at yylval.word
2156 ASSIGNMENT_WORD look at yylval.word
2157 NUMBER look at yylval.number
2158 ARITH_CMD look at yylval.word_list
2159 ARITH_FOR_EXPRS look at yylval.word_list
2160 COND_CMD look at yylval.command
2163 /* These are used by read_token_word, but appear up here so that shell_getc
2164 can use them to decide when to add otherwise blank lines to the history. */
2166 /* The primary delimiter stack. */
2167 struct dstack dstack = { (char *)NULL, 0, 0 };
2169 /* A temporary delimiter stack to be used when decoding prompt strings.
2170 This is needed because command substitutions in prompt strings (e.g., PS2)
2171 can screw up the parser's quoting state. */
2172 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
2174 /* Macro for accessing the top delimiter on the stack. Returns the
2175 delimiter or zero if none. */
2176 #define current_delimiter(ds) \
2177 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
2179 #define push_delimiter(ds, character) \
2182 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
2183 ds.delimiters = (char *)xrealloc \
2184 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
2185 ds.delimiters[ds.delimiter_depth] = character; \
2186 ds.delimiter_depth++; \
2190 #define pop_delimiter(ds) ds.delimiter_depth--
2192 /* Return the next shell input character. This always reads characters
2193 from shell_input_line; when that line is exhausted, it is time to
2194 read the next line. This is called by read_token when the shell is
2195 processing normal command input. */
2197 /* This implements one-character lookahead/lookbehind across physical input
2198 lines, to avoid something being lost because it's pushed back with
2199 shell_ungetc when we're at the start of a line. */
2200 static int eol_ungetc_lookahead = 0;
2203 shell_getc (remove_quoted_newline)
2204 int remove_quoted_newline;
2212 if (sigwinch_received)
2214 sigwinch_received = 0;
2215 get_new_window_size (0, (int *)0, (int *)0);
2218 if (eol_ungetc_lookahead)
2220 c = eol_ungetc_lookahead;
2221 eol_ungetc_lookahead = 0;
2225 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2226 /* If shell_input_line[shell_input_line_index] == 0, but there is
2227 something on the pushed list of strings, then we don't want to go
2228 off and get another line. We let the code down below handle it. */
2230 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
2231 (pushed_string_list == (STRING_SAVER *)NULL)))
2232 #else /* !ALIAS && !DPAREN_ARITHMETIC */
2233 if (!shell_input_line || !shell_input_line[shell_input_line_index])
2234 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
2238 /* Let's not let one really really long line blow up memory allocation */
2239 if (shell_input_line && shell_input_line_size >= 32768)
2241 free (shell_input_line);
2242 shell_input_line = 0;
2243 shell_input_line_size = 0;
2248 /* Allow immediate exit if interrupted during input. */
2252 shell_input_line_terminator = 0;
2254 /* If the shell is interatctive, but not currently printing a prompt
2255 (interactive_shell && interactive == 0), we don't want to print
2256 notifies or cleanup the jobs -- we want to defer it until we do
2257 print the next prompt. */
2258 if (interactive_shell == 0 || SHOULD_PROMPT())
2260 #if defined (JOB_CONTROL)
2261 /* This can cause a problem when reading a command as the result
2262 of a trap, when the trap is called from flush_child. This call
2263 had better not cause jobs to disappear from the job table in
2264 that case, or we will have big trouble. */
2265 notify_and_cleanup ();
2266 #else /* !JOB_CONTROL */
2267 cleanup_dead_jobs ();
2268 #endif /* !JOB_CONTROL */
2271 #if defined (READLINE)
2272 if (no_line_editing && SHOULD_PROMPT())
2274 if (SHOULD_PROMPT())
2278 if (bash_input.type == st_stream)
2285 /* Allow immediate exit if interrupted during input. */
2291 internal_warning ("shell_getc: ignored null byte in input");
2296 /* Theoretical overflow */
2297 /* If we can't put 256 bytes more into the buffer, allocate
2298 everything we can and fill it as full as we can. */
2299 /* XXX - we ignore rest of line using `truncating' flag */
2300 if (shell_input_line_size > (SIZE_MAX - 256))
2304 n = SIZE_MAX - i; /* how much more can we put into the buffer? */
2305 if (n <= 2) /* we have to save 1 for the newline added below */
2307 if (truncating == 0)
2308 internal_warning("shell_getc: shell_input_line_size (%zu) exceeds SIZE_MAX (%llu): line truncated", shell_input_line_size, SIZE_MAX);
2309 shell_input_line[i] = '\0';
2312 if (shell_input_line_size < SIZE_MAX)
2314 shell_input_line_size = SIZE_MAX;
2315 shell_input_line = xrealloc (shell_input_line, shell_input_line_size);
2319 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
2323 if (bash_input.type == st_stream)
2327 shell_input_line_terminator = EOF;
2329 shell_input_line[i] = '\0';
2333 if (truncating == 0 || c == '\n')
2334 shell_input_line[i++] = c;
2338 shell_input_line[--i] = '\0';
2339 current_command_line_count++;
2344 shell_input_line_index = 0;
2345 shell_input_line_len = i; /* == strlen (shell_input_line) */
2347 set_line_mbstate ();
2349 #if defined (HISTORY)
2350 if (remember_on_history && shell_input_line && shell_input_line[0])
2353 # if defined (BANG_HISTORY)
2356 /* If the current delimiter is a single quote, we should not be
2357 performing history expansion, even if we're on a different
2358 line from the original single quote. */
2359 old_hist = history_expansion_inhibited;
2360 if (current_delimiter (dstack) == '\'')
2361 history_expansion_inhibited = 1;
2363 expansions = pre_process_line (shell_input_line, 1, 1);
2364 # if defined (BANG_HISTORY)
2365 history_expansion_inhibited = old_hist;
2367 if (expansions != shell_input_line)
2369 free (shell_input_line);
2370 shell_input_line = expansions;
2371 shell_input_line_len = shell_input_line ?
2372 strlen (shell_input_line) : 0;
2373 if (shell_input_line_len == 0)
2374 current_command_line_count--;
2376 /* We have to force the xrealloc below because we don't know
2377 the true allocated size of shell_input_line anymore. */
2378 shell_input_line_size = shell_input_line_len;
2380 set_line_mbstate ();
2383 /* Try to do something intelligent with blank lines encountered while
2384 entering multi-line commands. XXX - this is grotesque */
2385 else if (remember_on_history && shell_input_line &&
2386 shell_input_line[0] == '\0' &&
2387 current_command_line_count > 1)
2389 if (current_delimiter (dstack))
2390 /* We know shell_input_line[0] == 0 and we're reading some sort of
2391 quoted string. This means we've got a line consisting of only
2392 a newline in a quoted string. We want to make sure this line
2393 gets added to the history. */
2394 maybe_add_history (shell_input_line);
2398 hdcs = history_delimiting_chars (shell_input_line);
2399 if (hdcs && hdcs[0] == ';')
2400 maybe_add_history (shell_input_line);
2404 #endif /* HISTORY */
2406 if (shell_input_line)
2408 /* Lines that signify the end of the shell's input should not be
2409 echoed. We should not echo lines while parsing command
2410 substitutions with recursive calls into the parsing engine; those
2411 should only be echoed once when we read the word. That is the
2412 reason for the test against shell_eof_token, which is set to a
2413 right paren when parsing the contents of command substitutions. */
2414 if (echo_input_at_read && (shell_input_line[0] ||
2415 shell_input_line_terminator != EOF) &&
2416 shell_eof_token == 0)
2417 fprintf (stderr, "%s\n", shell_input_line);
2421 shell_input_line_size = 0;
2422 prompt_string_pointer = ¤t_prompt_string;
2423 if (SHOULD_PROMPT ())
2428 /* Add the newline to the end of this string, iff the string does
2429 not already end in an EOF character. */
2430 if (shell_input_line_terminator != EOF)
2432 if (shell_input_line_size < SIZE_MAX-3 && (shell_input_line_len+3 > shell_input_line_size))
2433 shell_input_line = (char *)xrealloc (shell_input_line,
2434 1 + (shell_input_line_size += 2));
2436 shell_input_line[shell_input_line_len] = '\n';
2437 shell_input_line[shell_input_line_len + 1] = '\0';
2439 set_line_mbstate ();
2444 uc = shell_input_line[shell_input_line_index];
2447 shell_input_line_index++;
2449 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2450 /* If UC is NULL, we have reached the end of the current input string. If
2451 pushed_string_list is non-empty, it's time to pop to the previous string
2452 because we have fully consumed the result of the last alias expansion.
2453 Do it transparently; just return the next character of the string popped
2455 /* If pushed_string_list != 0 but pushed_string_list->expander == 0 (not
2456 currently tested) and the flags value is not PSH_SOURCE, we are not
2457 parsing an alias, we have just saved one (push_string, when called by
2458 the parse_dparen code) In this case, just go on as well. The PSH_SOURCE
2459 case is handled below. */
2461 if (uc == 0 && pushed_string_list && pushed_string_list->flags != PSH_SOURCE)
2464 uc = shell_input_line[shell_input_line_index];
2466 shell_input_line_index++;
2468 #endif /* ALIAS || DPAREN_ARITHMETIC */
2470 if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n')
2472 if (SHOULD_PROMPT ())
2475 /* What do we do here if we're expanding an alias whose definition
2476 includes an escaped newline? If that's the last character in the
2477 alias expansion, we just pop the pushed string list (recall that
2478 we inhibit the appending of a space in mk_alexpansion() if newline
2479 is the last character). If it's not the last character, we need
2480 to consume the quoted newline and move to the next character in
2483 if (expanding_alias () && shell_input_line[shell_input_line_index+1] == '\0')
2488 else if (expanding_alias () && shell_input_line[shell_input_line_index+1] != '\0')
2490 shell_input_line_index++; /* skip newline */
2491 goto next_alias_char; /* and get next character */
2498 if (uc == 0 && shell_input_line_terminator == EOF)
2499 return ((shell_input_line_index != 0) ? '\n' : EOF);
2501 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2502 /* We already know that we are not parsing an alias expansion because of the
2503 check for expanding_alias() above. This knows how parse_and_execute
2504 handles switching to st_string input while an alias is being expanded,
2505 hence the check for pushed_string_list without pushed_string_list->expander
2506 and the check for PSH_SOURCE as pushed_string_list->flags.
2507 parse_and_execute and parse_string both change the input type to st_string
2508 and place the string to be parsed and executed into location.string, so
2509 we should not stop reading that until the pointer is '\0'.
2510 The check for shell_input_line_terminator may be superfluous.
2512 This solves the problem of `.' inside a multi-line alias with embedded
2513 newlines executing things out of order. */
2514 if (uc == 0 && bash_input.type == st_string && *bash_input.location.string &&
2515 pushed_string_list && pushed_string_list->flags == PSH_SOURCE &&
2516 shell_input_line_terminator == 0)
2518 shell_input_line_index = 0;
2526 /* Put C back into the input for the shell. This might need changes for
2527 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
2528 character different than we read, shell_input_line_property doesn't need
2529 to change when manipulating shell_input_line. The define for
2530 last_shell_getc_is_singlebyte should take care of it, though. */
2535 if (shell_input_line && shell_input_line_index)
2536 shell_input_line[--shell_input_line_index] = c;
2538 eol_ungetc_lookahead = c;
2541 #ifdef INCLUDE_UNUSED
2542 /* Back the input pointer up by one, effectively `ungetting' a character. */
2546 if (shell_input_line && shell_input_line_index)
2547 shell_input_line_index--;
2551 /* Discard input until CHARACTER is seen, then push that character back
2552 onto the input stream. */
2554 discard_until (character)
2559 while ((c = shell_getc (0)) != EOF && c != character)
2567 execute_variable_command (command, vname)
2568 char *command, *vname;
2571 sh_parser_state_t ps;
2573 save_parser_state (&ps);
2574 last_lastarg = get_string_value ("_");
2576 last_lastarg = savestring (last_lastarg);
2578 parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST);
2580 restore_parser_state (&ps);
2581 bind_variable ("_", last_lastarg, 0);
2582 FREE (last_lastarg);
2584 if (token_to_read == '\n') /* reset_parser was called */
2588 /* Place to remember the token. We try to keep the buffer
2589 at a reasonable size, but it can grow. */
2590 static char *token = (char *)NULL;
2592 /* Current size of the token buffer. */
2593 static int token_buffer_size;
2595 /* Command to read_token () explaining what we want it to do. */
2598 #define prompt_is_ps1 \
2599 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
2601 /* Function for yyparse to call. yylex keeps track of
2602 the last two tokens read, and calls read_token. */
2606 if (interactive && (current_token == 0 || current_token == '\n'))
2608 /* Before we print a prompt, we might have to check mailboxes.
2609 We do this only if it is time to do so. Notice that only here
2610 is the mail alarm reset; nothing takes place in check_mail ()
2611 except the checking of mail. Please don't change this. */
2612 if (prompt_is_ps1 && parse_and_execute_level == 0 && time_to_check_mail ())
2615 reset_mail_timer ();
2618 /* Avoid printing a prompt if we're not going to read anything, e.g.
2619 after resetting the parser with read_token (RESET). */
2620 if (token_to_read == 0 && SHOULD_PROMPT ())
2624 two_tokens_ago = token_before_that;
2625 token_before_that = last_read_token;
2626 last_read_token = current_token;
2627 current_token = read_token (READ);
2629 if ((parser_state & PST_EOFTOKEN) && current_token == shell_eof_token)
2631 current_token = yacc_EOF;
2632 if (bash_input.type == st_string)
2633 rewind_input_string ();
2635 parser_state &= ~PST_EOFTOKEN;
2637 return (current_token);
2640 /* When non-zero, we have read the required tokens
2641 which allow ESAC to be the next one read. */
2642 static int esacs_needed_count;
2648 if (need_here_doc >= HEREDOC_MAX)
2650 last_command_exit_value = EX_BADUSAGE;
2652 report_syntax_error (_("maximum here-document count exceeded"));
2654 exit_shell (last_command_exit_value);
2656 redir_stack[need_here_doc++] = r;
2660 gather_here_documents ()
2665 while (need_here_doc > 0)
2667 parser_state |= PST_HEREDOC;
2668 make_here_document (redir_stack[r++], line_number);
2669 parser_state &= ~PST_HEREDOC;
2674 /* When non-zero, an open-brace used to create a group is awaiting a close
2676 static int open_brace_count;
2678 #define command_token_position(token) \
2679 (((token) == ASSIGNMENT_WORD) || (parser_state&PST_REDIRLIST) || \
2680 ((token) != SEMI_SEMI && (token) != SEMI_AND && (token) != SEMI_SEMI_AND && reserved_word_acceptable(token)))
2682 #define assignment_acceptable(token) \
2683 (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0))
2685 /* Check to see if TOKEN is a reserved word and return the token
2687 #define CHECK_FOR_RESERVED_WORD(tok) \
2689 if (!dollar_present && !quoted && \
2690 reserved_word_acceptable (last_read_token)) \
2693 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
2694 if (STREQ (tok, word_token_alist[i].word)) \
2696 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
2698 if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \
2700 if (word_token_alist[i].token == ESAC) \
2701 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
2702 else if (word_token_alist[i].token == CASE) \
2703 parser_state |= PST_CASESTMT; \
2704 else if (word_token_alist[i].token == COND_END) \
2705 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
2706 else if (word_token_alist[i].token == COND_START) \
2707 parser_state |= PST_CONDCMD; \
2708 else if (word_token_alist[i].token == '{') \
2709 open_brace_count++; \
2710 else if (word_token_alist[i].token == '}' && open_brace_count) \
2711 open_brace_count--; \
2712 return (word_token_alist[i].token); \
2719 /* OK, we have a token. Let's try to alias expand it, if (and only if)
2722 It is eligible for expansion if EXPAND_ALIASES is set, and
2723 the token is unquoted and the last token read was a command
2724 separator (or expand_next_token is set), and we are currently
2725 processing an alias (pushed_string_list is non-empty) and this
2726 token is not the same as the current or any previously
2729 Special cases that disqualify:
2730 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
2740 r = xmalloc (l + 2);
2742 /* If the last character in the alias is a newline, don't add a trailing
2743 space to the expansion. Works with shell_getc above. */
2744 if (r[l - 1] != ' ' && r[l - 1] != '\n')
2751 alias_expand_token (tokstr)
2757 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
2758 (parser_state & PST_CASEPAT) == 0)
2760 ap = find_alias (tokstr);
2762 /* Currently expanding this token. */
2763 if (ap && (ap->flags & AL_BEINGEXPANDED))
2764 return (NO_EXPANSION);
2766 /* mk_alexpansion puts an extra space on the end of the alias expansion,
2767 so the lookahead by the parser works right. If this gets changed,
2768 make sure the code in shell_getc that deals with reaching the end of
2769 an expanded alias is changed with it. */
2770 expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL;
2774 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
2775 return (RE_READ_TOKEN);
2778 /* This is an eligible token that does not have an expansion. */
2779 return (NO_EXPANSION);
2781 return (NO_EXPANSION);
2786 time_command_acceptable ()
2788 #if defined (COMMAND_TIMING)
2791 if (posixly_correct && shell_compatibility_level > 41)
2793 /* Quick check of the rest of the line to find the next token. If it
2794 begins with a `-', Posix says to not return `time' as the token.
2795 This was interp 267. */
2796 i = shell_input_line_index;
2797 while (i < shell_input_line_len && (shell_input_line[i] == ' ' || shell_input_line[i] == '\t'))
2799 if (shell_input_line[i] == '-')
2803 switch (last_read_token)
2816 case BANG: /* ! time pipeline */
2817 case TIME: /* time time pipeline */
2818 case TIMEOPT: /* time -p time pipeline */
2819 case TIMEIGN: /* time -p -- ... */
2826 #endif /* COMMAND_TIMING */
2829 /* Handle special cases of token recognition:
2830 IN is recognized if the last token was WORD and the token
2831 before that was FOR or CASE or SELECT.
2833 DO is recognized if the last token was WORD and the token
2834 before that was FOR or SELECT.
2836 ESAC is recognized if the last token caused `esacs_needed_count'
2839 `{' is recognized if the last token as WORD and the token
2840 before that was FUNCTION, or if we just parsed an arithmetic
2843 `}' is recognized if there is an unclosed `{' present.
2845 `-p' is returned as TIMEOPT if the last read token was TIME.
2846 `--' is returned as TIMEIGN if the last read token was TIMEOPT.
2848 ']]' is returned as COND_END if the parser is currently parsing
2849 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
2851 `time' is returned as TIME if and only if it is immediately
2852 preceded by one of `;', `\n', `||', `&&', or `&'.
2856 special_case_tokens (tokstr)
2859 if ((last_read_token == WORD) &&
2860 #if defined (SELECT_COMMAND)
2861 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
2863 ((token_before_that == FOR) || (token_before_that == CASE)) &&
2865 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
2867 if (token_before_that == CASE)
2869 parser_state |= PST_CASEPAT;
2870 esacs_needed_count++;
2875 if (last_read_token == WORD &&
2876 #if defined (SELECT_COMMAND)
2877 (token_before_that == FOR || token_before_that == SELECT) &&
2879 (token_before_that == FOR) &&
2881 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
2884 /* Ditto for ESAC in the CASE case.
2885 Specifically, this handles "case word in esac", which is a legal
2886 construct, certainly because someone will pass an empty arg to the
2887 case construct, and we don't want it to barf. Of course, we should
2888 insist that the case construct has at least one pattern in it, but
2889 the designers disagree. */
2890 if (esacs_needed_count)
2892 esacs_needed_count--;
2893 if (STREQ (tokstr, "esac"))
2895 parser_state &= ~PST_CASEPAT;
2900 /* The start of a shell function definition. */
2901 if (parser_state & PST_ALLOWOPNBRC)
2903 parser_state &= ~PST_ALLOWOPNBRC;
2904 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2907 function_bstart = line_number;
2908 return ('{'); /* } */
2912 /* We allow a `do' after a for ((...)) without an intervening
2914 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
2916 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2919 return ('{'); /* } */
2922 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
2924 open_brace_count--; /* { */
2928 #if defined (COMMAND_TIMING)
2929 /* Handle -p after `time'. */
2930 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
2932 /* Handle -- after `time -p'. */
2933 if (last_read_token == TIMEOPT && tokstr[0] == '-' && tokstr[1] == '-' && !tokstr[2])
2937 #if defined (COND_COMMAND) /* [[ */
2938 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
2945 /* Called from shell.c when Control-C is typed at top level. Or
2946 by the error rule at top level. */
2950 dstack.delimiter_depth = 0; /* No delimiters found so far. */
2951 open_brace_count = 0;
2953 #if defined (EXTENDED_GLOB)
2954 /* Reset to global value of extended glob */
2955 if (parser_state & PST_EXTPAT)
2956 extended_glob = global_extglob;
2961 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2962 if (pushed_string_list)
2963 free_string_list ();
2964 #endif /* ALIAS || DPAREN_ARITHMETIC */
2966 if (shell_input_line)
2968 free (shell_input_line);
2969 shell_input_line = (char *)NULL;
2970 shell_input_line_size = shell_input_line_index = 0;
2973 FREE (word_desc_to_read);
2974 word_desc_to_read = (WORD_DESC *)NULL;
2976 eol_ungetc_lookahead = 0;
2978 current_token = '\n'; /* XXX */
2979 last_read_token = '\n';
2980 token_to_read = '\n';
2983 /* Read the next token. Command can be READ (normal operation) or
2984 RESET (to normalize state). */
2986 read_token (command)
2989 int character; /* Current character. */
2990 int peek_char; /* Temporary look-ahead character. */
2991 int result; /* The thing to return. */
2993 if (command == RESET)
3001 result = token_to_read;
3002 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
3004 yylval.word = word_desc_to_read;
3005 word_desc_to_read = (WORD_DESC *)NULL;
3011 #if defined (COND_COMMAND)
3012 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
3014 cond_lineno = line_number;
3015 parser_state |= PST_CONDEXPR;
3016 yylval.command = parse_cond_command ();
3017 if (cond_token != COND_END)
3022 token_to_read = COND_END;
3023 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
3029 /* This is a place to jump back to once we have successfully expanded a
3030 token with an alias and pushed the string with push_string () */
3034 /* Read a single word from input. Start by skipping blanks. */
3035 while ((character = shell_getc (1)) != EOF && shellblank (character))
3038 if (character == EOF)
3044 if MBTEST(character == '#' && (!interactive || interactive_comments))
3046 /* A comment. Discard until EOL or EOF, and then return a newline. */
3047 discard_until ('\n');
3049 character = '\n'; /* this will take the next if statement and return. */
3052 if (character == '\n')
3054 /* If we're about to return an unquoted newline, we can go and collect
3055 the text of any pending here document. */
3057 gather_here_documents ();
3060 parser_state &= ~PST_ALEXPNEXT;
3063 parser_state &= ~PST_ASSIGNOK;
3068 if (parser_state & PST_REGEXP)
3071 /* Shell meta-characters. */
3072 if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
3075 /* Turn off alias tokenization iff this character sequence would
3076 not leave us ready to read a command. */
3077 if (character == '<' || character == '>')
3078 parser_state &= ~PST_ALEXPNEXT;
3081 parser_state &= ~PST_ASSIGNOK;
3083 peek_char = shell_getc (1);
3084 if (character == peek_char)
3089 /* If '<' then we could be at "<<" or at "<<-". We have to
3090 look ahead one more character. */
3091 peek_char = shell_getc (1);
3092 if MBTEST(peek_char == '-')
3093 return (LESS_LESS_MINUS);
3094 else if MBTEST(peek_char == '<')
3095 return (LESS_LESS_LESS);
3098 shell_ungetc (peek_char);
3103 return (GREATER_GREATER);
3106 parser_state |= PST_CASEPAT;
3108 parser_state &= ~PST_ALEXPNEXT;
3111 peek_char = shell_getc (1);
3112 if MBTEST(peek_char == '&')
3113 return (SEMI_SEMI_AND);
3116 shell_ungetc (peek_char);
3126 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
3128 result = parse_dparen (character);
3136 else if MBTEST(character == '<' && peek_char == '&')
3138 else if MBTEST(character == '>' && peek_char == '&')
3139 return (GREATER_AND);
3140 else if MBTEST(character == '<' && peek_char == '>')
3141 return (LESS_GREATER);
3142 else if MBTEST(character == '>' && peek_char == '|')
3143 return (GREATER_BAR);
3144 else if MBTEST(character == '&' && peek_char == '>')
3146 peek_char = shell_getc (1);
3147 if MBTEST(peek_char == '>')
3148 return (AND_GREATER_GREATER);
3151 shell_ungetc (peek_char);
3152 return (AND_GREATER);
3155 else if MBTEST(character == '|' && peek_char == '&')
3157 else if MBTEST(character == ';' && peek_char == '&')
3159 parser_state |= PST_CASEPAT;
3161 parser_state &= ~PST_ALEXPNEXT;
3166 shell_ungetc (peek_char);
3168 /* If we look like we are reading the start of a function
3169 definition, then let the reader know about it so that
3170 we will do the right thing with `{'. */
3171 if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
3173 parser_state |= PST_ALLOWOPNBRC;
3175 parser_state &= ~PST_ALEXPNEXT;
3177 function_dstart = line_number;
3180 /* case pattern lists may be preceded by an optional left paren. If
3181 we're not trying to parse a case pattern list, the left paren
3182 indicates a subshell. */
3183 if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
3184 parser_state |= PST_SUBSHELL;
3186 else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
3187 parser_state &= ~PST_CASEPAT;
3189 else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
3190 parser_state &= ~PST_SUBSHELL;
3192 #if defined (PROCESS_SUBSTITUTION)
3193 /* Check for the constructs which introduce process substitution.
3194 Shells running in `posix mode' don't do process substitution. */
3195 if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/
3196 #endif /* PROCESS_SUBSTITUTION */
3200 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
3201 if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
3205 /* Okay, if we got this far, we have to read a word. Read one,
3206 and then check it against the known ones. */
3207 result = read_token_word (character);
3209 if (result == RE_READ_TOKEN)
3216 * Match a $(...) or other grouping construct. This has to handle embedded
3217 * quoted strings ('', ``, "") and nested constructs. It also must handle
3218 * reprompting the user, if necessary, after reading a newline, and returning
3219 * correct error values if it reads EOF.
3221 #define P_FIRSTCLOSE 0x0001
3222 #define P_ALLOWESC 0x0002
3223 #define P_DQUOTE 0x0004
3224 #define P_COMMAND 0x0008 /* parsing a command, so look for comments */
3225 #define P_BACKQUOTE 0x0010 /* parsing a backquoted command substitution */
3226 #define P_ARRAYSUB 0x0020 /* parsing a [...] array subscript for assignment */
3227 #define P_DOLBRACE 0x0040 /* parsing a ${...} construct */
3229 /* Lexical state while parsing a grouping construct or $(...). */
3230 #define LEX_WASDOL 0x001
3231 #define LEX_CKCOMMENT 0x002
3232 #define LEX_INCOMMENT 0x004
3233 #define LEX_PASSNEXT 0x008
3234 #define LEX_RESWDOK 0x010
3235 #define LEX_CKCASE 0x020
3236 #define LEX_INCASE 0x040
3237 #define LEX_INHEREDOC 0x080
3238 #define LEX_HEREDELIM 0x100 /* reading here-doc delimiter */
3239 #define LEX_STRIPDOC 0x200 /* <<- strip tabs from here doc delim */
3240 #define LEX_INWORD 0x400
3242 #define COMSUB_META(ch) ((ch) == ';' || (ch) == '&' || (ch) == '|')
3244 #define CHECK_NESTRET_ERROR() \
3246 if (nestret == &matched_pair_error) \
3249 return &matched_pair_error; \
3253 #define APPEND_NESTRET() \
3257 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); \
3258 strcpy (ret + retind, nestret); \
3259 retind += nestlen; \
3263 static char matched_pair_error;
3266 parse_matched_pair (qc, open, close, lenp, flags)
3267 int qc; /* `"' if this construct is within double quotes */
3271 int count, ch, tflags;
3272 int nestlen, ttranslen, start_lineno;
3273 char *ret, *nestret, *ttrans;
3274 int retind, retsize, rflags;
3277 dolbrace_state = (flags & P_DOLBRACE) ? DOLBRACE_PARAM : 0;
3279 /*itrace("parse_matched_pair[%d]: open = %c close = %c flags = %d", line_number, open, close, flags);*/
3283 if ((flags & P_COMMAND) && qc != '`' && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
3284 tflags |= LEX_CKCOMMENT;
3286 /* RFLAGS is the set of flags we want to pass to recursive calls. */
3287 rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE);
3289 ret = (char *)xmalloc (retsize = 64);
3292 start_lineno = line_number;
3295 ch = shell_getc (qc != '\'' && (tflags & (LEX_PASSNEXT)) == 0);
3300 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
3301 EOF_Reached = 1; /* XXX */
3302 return (&matched_pair_error);
3305 /* Possible reprompting. */
3306 if (ch == '\n' && SHOULD_PROMPT ())
3309 /* Don't bother counting parens or doing anything else if in a comment
3310 or part of a case statement */
3311 if (tflags & LEX_INCOMMENT)
3313 /* Add this character. */
3314 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3318 tflags &= ~LEX_INCOMMENT;
3323 /* Not exactly right yet, should handle shell metacharacters, too. If
3324 any changes are made to this test, make analogous changes to subst.c:
3325 extract_delimited_string(). */
3326 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (retind == 0 || ret[retind-1] == '\n' || shellblank (ret[retind - 1])))
3327 tflags |= LEX_INCOMMENT;
3329 if (tflags & LEX_PASSNEXT) /* last char was backslash */
3331 tflags &= ~LEX_PASSNEXT;
3332 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
3335 retind--; /* swallow previously-added backslash */
3339 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3340 if MBTEST(ch == CTLESC)
3341 ret[retind++] = CTLESC;
3345 /* If we're reparsing the input (e.g., from parse_string_to_word_list),
3346 we've already prepended CTLESC to single-quoted results of $'...'.
3347 We may want to do this for other CTLESC-quoted characters in
3349 else if MBTEST((parser_state & PST_REPARSE) && open == '\'' && (ch == CTLESC || ch == CTLNUL))
3351 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3355 else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
3357 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3358 ret[retind++] = CTLESC;
3362 else if MBTEST(ch == close) /* ending delimiter */
3364 /* handle nested ${...} specially. */
3365 else if MBTEST(open != close && (tflags & LEX_WASDOL) && open == '{' && ch == open) /* } */
3367 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
3370 /* Add this character. */
3371 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3374 /* If we just read the ending character, don't bother continuing. */
3378 if (open == '\'') /* '' inside grouping construct */
3380 if MBTEST((flags & P_ALLOWESC) && ch == '\\')
3381 tflags |= LEX_PASSNEXT;
3385 if MBTEST(ch == '\\') /* backslashes */
3386 tflags |= LEX_PASSNEXT;
3388 /* Based on which dolstate is currently in (param, op, or word),
3389 decide what the op is. We're really only concerned if it's % or
3390 #, so we can turn on a flag that says whether or not we should
3391 treat single quotes as special when inside a double-quoted
3392 ${...}. This logic must agree with subst.c:extract_dollar_brace_string
3393 since they share the same defines. */
3394 /* FLAG POSIX INTERP 221 */
3395 if (flags & P_DOLBRACE)
3397 /* ${param%[%]word} */
3398 if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '%' && retind > 1)
3399 dolbrace_state = DOLBRACE_QUOTE;
3400 /* ${param#[#]word} */
3401 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '#' && retind > 1)
3402 dolbrace_state = DOLBRACE_QUOTE;
3403 /* ${param/[/]pat/rep} */
3404 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '/' && retind > 1)
3405 dolbrace_state = DOLBRACE_QUOTE2; /* XXX */
3406 /* ${param^[^]pat} */
3407 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '^' && retind > 1)
3408 dolbrace_state = DOLBRACE_QUOTE;
3409 /* ${param,[,]pat} */
3410 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == ',' && retind > 1)
3411 dolbrace_state = DOLBRACE_QUOTE;
3412 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", ch) != 0)
3413 dolbrace_state = DOLBRACE_OP;
3414 else if MBTEST(dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", ch) == 0)
3415 dolbrace_state = DOLBRACE_WORD;
3418 /* The big hammer. Single quotes aren't special in double quotes. The
3419 problem is that Posix used to say the single quotes are semi-special:
3420 within a double-quoted ${...} construct "an even number of
3421 unescaped double-quotes or single-quotes, if any, shall occur." */
3422 /* This was changed in Austin Group Interp 221 */
3423 if MBTEST(posixly_correct && shell_compatibility_level > 41 && dolbrace_state != DOLBRACE_QUOTE && dolbrace_state != DOLBRACE_QUOTE2 && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'')
3426 /* Could also check open == '`' if we want to parse grouping constructs
3427 inside old-style command substitution. */
3428 if (open != close) /* a grouping construct */
3430 if MBTEST(shellquote (ch))
3432 /* '', ``, or "" inside $(...) or other grouping construct. */
3433 push_delimiter (dstack, ch);
3434 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
3435 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
3437 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
3438 pop_delimiter (dstack);
3439 CHECK_NESTRET_ERROR ();
3441 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
3443 /* Translate $'...' here. */
3444 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
3447 /* If we're parsing a double-quoted brace expansion and we are
3448 not in a place where single quotes are treated specially,
3449 make sure we single-quote the results of the ansi
3450 expansion because quote removal should remove them later */
3451 /* FLAG POSIX INTERP 221 */
3452 if ((shell_compatibility_level > 42) && (rflags & P_DQUOTE) && (dolbrace_state == DOLBRACE_QUOTE2) && (flags & P_DOLBRACE))
3454 nestret = sh_single_quote (ttrans);
3456 nestlen = strlen (nestret);
3458 else if ((rflags & P_DQUOTE) == 0)
3460 nestret = sh_single_quote (ttrans);
3462 nestlen = strlen (nestret);
3467 nestlen = ttranslen;
3469 retind -= 2; /* back up before the $' */
3471 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
3473 /* Locale expand $"..." here. */
3474 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
3477 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
3479 nestlen = ttranslen + 2;
3480 retind -= 2; /* back up before the $" */
3486 else if ((flags & P_ARRAYSUB) && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3487 goto parse_dollar_word;
3489 /* Parse an old-style command substitution within double quotes as a
3491 /* XXX - sh and ksh93 don't do this - XXX */
3492 else if MBTEST(open == '"' && ch == '`')
3494 nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags);
3496 CHECK_NESTRET_ERROR ();
3501 else if MBTEST(open != '`' && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3502 /* check for $(), $[], or ${} inside quoted string. */
3505 if (open == ch) /* undo previous increment */
3507 if (ch == '(') /* ) */
3508 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
3509 else if (ch == '{') /* } */
3510 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags);
3511 else if (ch == '[') /* ] */
3512 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
3514 CHECK_NESTRET_ERROR ();
3519 if MBTEST(ch == '$')
3520 tflags |= LEX_WASDOL;
3522 tflags &= ~LEX_WASDOL;
3528 /*itrace("parse_matched_pair[%d]: returning %s", line_number, ret);*/
3532 /* Parse a $(...) command substitution. This is messier than I'd like, and
3533 reproduces a lot more of the token-reading code than I'd like. */
3535 parse_comsub (qc, open, close, lenp, flags)
3536 int qc; /* `"' if this construct is within double quotes */
3540 int count, ch, peekc, tflags, lex_rwlen, lex_wlen, lex_firstind;
3541 int nestlen, ttranslen, start_lineno;
3542 char *ret, *nestret, *ttrans, *heredelim;
3543 int retind, retsize, rflags, hdlen;
3545 /* Posix interp 217 says arithmetic expressions have precedence, so
3546 assume $(( introduces arithmetic expansion and parse accordingly. */
3547 peekc = shell_getc (0);
3548 shell_ungetc (peekc);
3550 return (parse_matched_pair (qc, open, close, lenp, 0));
3552 /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/
3554 tflags = LEX_RESWDOK;
3556 if ((flags & P_COMMAND) && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
3557 tflags |= LEX_CKCASE;
3558 if ((tflags & LEX_CKCASE) && (interactive == 0 || interactive_comments))
3559 tflags |= LEX_CKCOMMENT;
3561 /* RFLAGS is the set of flags we want to pass to recursive calls. */
3562 rflags = (flags & P_DQUOTE);
3564 ret = (char *)xmalloc (retsize = 64);
3567 start_lineno = line_number;
3568 lex_rwlen = lex_wlen = 0;
3576 ch = shell_getc (qc != '\'' && (tflags & (LEX_INCOMMENT|LEX_PASSNEXT)) == 0);
3583 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
3584 EOF_Reached = 1; /* XXX */
3585 return (&matched_pair_error);
3588 /* If we hit the end of a line and are reading the contents of a here
3589 document, and it's not the same line that the document starts on,
3590 check for this line being the here doc delimiter. Otherwise, if
3591 we're in a here document, mark the next character as the beginning
3595 if ((tflags & LEX_HEREDELIM) && heredelim)
3597 tflags &= ~LEX_HEREDELIM;
3598 tflags |= LEX_INHEREDOC;
3599 lex_firstind = retind + 1;
3601 else if (tflags & LEX_INHEREDOC)
3604 tind = lex_firstind;
3605 while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t')
3607 if (STREQN (ret + tind, heredelim, hdlen))
3609 tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC);
3610 /*itrace("parse_comsub:%d: found here doc end `%s'", line_number, ret + tind);*/
3616 lex_firstind = retind + 1;
3620 /* Possible reprompting. */
3621 if (ch == '\n' && SHOULD_PROMPT ())
3624 /* XXX -- possibly allow here doc to be delimited by ending right
3626 if ((tflags & LEX_INHEREDOC) && ch == close && count == 1)
3629 /*itrace("parse_comsub: in here doc, ch == close, retind - firstind = %d hdlen = %d retind = %d", retind-lex_firstind, hdlen, retind);*/
3630 tind = lex_firstind;
3631 while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t')
3633 if (retind-tind == hdlen && STREQN (ret + tind, heredelim, hdlen))
3635 tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC);
3636 /*itrace("parse_comsub:%d: found here doc end `%s'", line_number, ret + tind);*/
3643 /* Don't bother counting parens or doing anything else if in a comment */
3644 if (tflags & (LEX_INCOMMENT|LEX_INHEREDOC))
3646 /* Add this character. */
3647 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3650 if ((tflags & LEX_INCOMMENT) && ch == '\n')
3652 /*itrace("parse_comsub:%d: lex_incomment -> 0 ch = `%c'", line_number, ch);*/
3653 tflags &= ~LEX_INCOMMENT;
3659 if (tflags & LEX_PASSNEXT) /* last char was backslash */
3661 /*itrace("parse_comsub:%d: lex_passnext -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3662 tflags &= ~LEX_PASSNEXT;
3663 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
3666 retind--; /* swallow previously-added backslash */
3670 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3671 if MBTEST(ch == CTLESC)
3672 ret[retind++] = CTLESC;
3677 /* If this is a shell break character, we are not in a word. If not,
3678 we either start or continue a word. */
3679 if MBTEST(shellbreak (ch))
3681 tflags &= ~LEX_INWORD;
3682 /*itrace("parse_comsub:%d: lex_inword -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3686 if (tflags & LEX_INWORD)
3689 /*itrace("parse_comsub:%d: lex_inword == 1 ch = `%c' lex_wlen = %d (%d)", line_number, ch, lex_wlen, __LINE__);*/
3693 /*itrace("parse_comsub:%d: lex_inword -> 1 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3694 tflags |= LEX_INWORD;
3699 /* Skip whitespace */
3700 if MBTEST(shellblank (ch) && (tflags & LEX_HEREDELIM) == 0 && lex_rwlen == 0)
3702 /* Add this character. */
3703 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3708 /* Either we are looking for the start of the here-doc delimiter
3709 (lex_firstind == -1) or we are reading one (lex_firstind >= 0).
3710 If this character is a shell break character and we are reading
3711 the delimiter, save it and note that we are now reading a here
3712 document. If we've found the start of the delimiter, note it by
3713 setting lex_firstind. Backslashes can quote shell metacharacters
3714 in here-doc delimiters. */
3715 if (tflags & LEX_HEREDELIM)
3717 if (lex_firstind == -1 && shellbreak (ch) == 0)
3718 lex_firstind = retind;
3720 else if (heredelim && (tflags & LEX_PASSNEXT) == 0 && ch == '\n')
3722 tflags |= LEX_INHEREDOC;
3723 tflags &= ~LEX_HEREDELIM;
3724 lex_firstind = retind + 1;
3727 else if (lex_firstind >= 0 && (tflags & LEX_PASSNEXT) == 0 && shellbreak (ch))
3731 nestret = substring (ret, lex_firstind, retind);
3732 heredelim = string_quote_removal (nestret, 0);
3734 hdlen = STRLEN(heredelim);
3735 /*itrace("parse_comsub:%d: found here doc delimiter `%s' (%d)", line_number, heredelim, hdlen);*/
3739 tflags |= LEX_INHEREDOC;
3740 tflags &= ~LEX_HEREDELIM;
3741 lex_firstind = retind + 1;
3748 /* Meta-characters that can introduce a reserved word. Not perfect yet. */
3749 if MBTEST((tflags & LEX_RESWDOK) == 0 && (tflags & LEX_CKCASE) && (tflags & LEX_INCOMMENT) == 0 && (shellmeta(ch) || ch == '\n'))
3751 /* Add this character. */
3752 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3754 peekc = shell_getc (1);
3755 if (ch == peekc && (ch == '&' || ch == '|' || ch == ';')) /* two-character tokens */
3757 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3758 ret[retind++] = peekc;
3759 /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/
3760 tflags |= LEX_RESWDOK;
3764 else if (ch == '\n' || COMSUB_META(ch))
3766 shell_ungetc (peekc);
3767 /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/
3768 tflags |= LEX_RESWDOK;
3776 /* `unget' the character we just added and fall through */
3778 shell_ungetc (peekc);
3782 /* If we can read a reserved word, try to read one. */
3783 if (tflags & LEX_RESWDOK)
3785 if MBTEST(islower (ch))
3787 /* Add this character. */
3788 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3793 else if MBTEST(lex_rwlen == 4 && shellbreak (ch))
3795 if (STREQN (ret + retind - 4, "case", 4))
3797 tflags |= LEX_INCASE;
3798 /*itrace("parse_comsub:%d: found `case', lex_incase -> 1 lex_reswdok -> 0", line_number);*/
3800 else if (STREQN (ret + retind - 4, "esac", 4))
3802 tflags &= ~LEX_INCASE;
3803 /*itrace("parse_comsub:%d: found `esac', lex_incase -> 0 lex_reswdok -> 0", line_number);*/
3805 tflags &= ~LEX_RESWDOK;
3807 else if MBTEST((tflags & LEX_CKCOMMENT) && ch == '#' && (lex_rwlen == 0 || ((tflags & LEX_INWORD) && lex_wlen == 0)))
3808 ; /* don't modify LEX_RESWDOK if we're starting a comment */
3809 /* Allow `do' followed by space, tab, or newline to preserve the
3810 RESWDOK flag, but reset the reserved word length counter so we
3811 can read another one. */
3812 else if MBTEST(((tflags & LEX_INCASE) == 0) &&
3813 (isblank(ch) || ch == '\n') &&
3815 STREQN (ret + retind - 2, "do", 2))
3817 /*itrace("parse_comsub:%d: lex_incase == 1 found `%c', found \"do\"", line_number, ch);*/
3820 else if MBTEST((tflags & LEX_INCASE) && ch != '\n')
3821 /* If we can read a reserved word and we're in case, we're at the
3822 point where we can read a new pattern list or an esac. We
3823 handle the esac case above. If we read a newline, we want to
3824 leave LEX_RESWDOK alone. If we read anything else, we want to
3825 turn off LEX_RESWDOK, since we're going to read a pattern list. */
3827 tflags &= ~LEX_RESWDOK;
3828 /*itrace("parse_comsub:%d: lex_incase == 1 found `%c', lex_reswordok -> 0", line_number, ch);*/
3830 else if MBTEST(shellbreak (ch) == 0)
3832 tflags &= ~LEX_RESWDOK;
3833 /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/
3836 /* If we find a space or tab but have read something and it's not
3837 `do', turn off the reserved-word-ok flag */
3838 else if MBTEST(isblank (ch) && lex_rwlen > 0)
3840 tflags &= ~LEX_RESWDOK;
3841 /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/
3846 /* Might be the start of a here-doc delimiter */
3847 if MBTEST((tflags & LEX_INCOMMENT) == 0 && (tflags & LEX_CKCASE) && ch == '<')
3849 /* Add this character. */
3850 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3852 peekc = shell_getc (1);
3857 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3858 ret[retind++] = peekc;
3859 peekc = shell_getc (1);
3864 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3865 ret[retind++] = peekc;
3866 tflags |= LEX_STRIPDOC;
3869 shell_ungetc (peekc);
3872 tflags |= LEX_HEREDELIM;
3878 ch = peekc; /* fall through and continue XXX */
3880 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (((tflags & LEX_RESWDOK) && lex_rwlen == 0) || ((tflags & LEX_INWORD) && lex_wlen == 0)))
3882 /*itrace("parse_comsub:%d: lex_incomment -> 1 (%d)", line_number, __LINE__);*/
3883 tflags |= LEX_INCOMMENT;
3886 if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
3888 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3889 ret[retind++] = CTLESC;
3894 else if MBTEST((tflags & LEX_INCASE) && ch == close && close == ')')
3895 tflags &= ~LEX_INCASE; /* XXX */
3897 else if MBTEST(ch == close && (tflags & LEX_INCASE) == 0) /* ending delimiter */
3900 /*itrace("parse_comsub:%d: found close: count = %d", line_number, count);*/
3902 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && (tflags & LEX_INCASE) == 0 && ch == open) /* nested begin */
3905 /*itrace("parse_comsub:%d: found open: count = %d", line_number, count);*/
3908 /* Add this character. */
3909 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3912 /* If we just read the ending character, don't bother continuing. */
3916 if MBTEST(ch == '\\') /* backslashes */
3917 tflags |= LEX_PASSNEXT;
3919 if MBTEST(shellquote (ch))
3921 /* '', ``, or "" inside $(...). */
3922 push_delimiter (dstack, ch);
3923 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
3924 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
3926 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
3927 pop_delimiter (dstack);
3928 CHECK_NESTRET_ERROR ();
3930 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
3932 /* Translate $'...' here. */
3933 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
3936 if ((rflags & P_DQUOTE) == 0)
3938 nestret = sh_single_quote (ttrans);
3940 nestlen = strlen (nestret);
3945 nestlen = ttranslen;
3947 retind -= 2; /* back up before the $' */
3949 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
3951 /* Locale expand $"..." here. */
3952 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
3955 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
3957 nestlen = ttranslen + 2;
3958 retind -= 2; /* back up before the $" */
3964 else if MBTEST((tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3965 /* check for $(), $[], or ${} inside command substitution. */
3967 if ((tflags & LEX_INCASE) == 0 && open == ch) /* undo previous increment */
3969 if (ch == '(') /* ) */
3970 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
3971 else if (ch == '{') /* } */
3972 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags);
3973 else if (ch == '[') /* ] */
3974 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
3976 CHECK_NESTRET_ERROR ();
3981 if MBTEST(ch == '$')
3982 tflags |= LEX_WASDOL;
3984 tflags &= ~LEX_WASDOL;
3991 /*itrace("parse_comsub:%d: returning `%s'", line_number, ret);*/
3995 /* Recursively call the parser to parse a $(...) command substitution. */
3997 xparse_dolparen (base, string, indp, flags)
4003 sh_parser_state_t ps;
4004 sh_input_line_state_t ls;
4005 int orig_ind, nc, sflags, orig_eof_token;
4006 char *ret, *s, *ep, *ostring;
4012 /*itrace("xparse_dolparen: size = %d shell_input_line = `%s'", shell_input_line_size, shell_input_line);*/
4013 sflags = SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOFREE;
4014 if (flags & SX_NOLONGJMP)
4015 sflags |= SEVAL_NOLONGJMP;
4016 save_parser_state (&ps);
4017 save_input_line_state (&ls);
4018 orig_eof_token = shell_eof_token;
4021 parser_state |= PST_CMDSUBST|PST_EOFTOKEN; /* allow instant ')' */ /*(*/
4022 shell_eof_token = ')';
4023 parse_string (string, "command substitution", sflags, &ep);
4025 shell_eof_token = orig_eof_token;
4026 restore_parser_state (&ps);
4028 /* reset_parser clears shell_input_line and associated variables */
4029 restore_input_line_state (&ls);
4033 /* Need to find how many characters parse_and_execute consumed, update
4034 *indp, if flags != 0, copy the portion of the string parsed into RET
4035 and return it. If flags & 1 (EX_NOALLOC) we can return NULL. */
4042 itrace("xparse_dolparen:%d: ep[-1] != RPAREN (%d), ep = `%s'", line_number, ep[-1], ep);
4044 while (ep > ostring && ep[-1] == '\n') ep--;
4048 *indp = ep - base - 1;
4052 if (base[*indp] != ')')
4053 itrace("xparse_dolparen:%d: base[%d] != RPAREN (%d), base = `%s'", line_number, *indp, base[*indp], base);
4056 if (flags & SX_NOALLOC)
4057 return (char *)NULL;
4065 ret = substring (ostring, 0, nc - 1);
4070 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
4071 /* Parse a double-paren construct. It can be either an arithmetic
4072 command, an arithmetic `for' command, or a nested subshell. Returns
4073 the parsed token, -1 on error, or -2 if we didn't do anything and
4074 should just go on. */
4083 #if defined (ARITH_FOR_COMMAND)
4084 if (last_read_token == FOR)
4086 arith_for_lineno = line_number;
4087 cmdtyp = parse_arith_cmd (&wval, 0);
4090 wd = alloc_word_desc ();
4092 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
4093 return (ARITH_FOR_EXPRS);
4096 return -1; /* ERROR */
4100 #if defined (DPAREN_ARITHMETIC)
4101 if (reserved_word_acceptable (last_read_token))
4103 sline = line_number;
4105 cmdtyp = parse_arith_cmd (&wval, 0);
4106 if (cmdtyp == 1) /* arithmetic command */
4108 wd = alloc_word_desc ();
4110 wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE;
4111 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
4114 else if (cmdtyp == 0) /* nested subshell */
4116 push_string (wval, 0, (alias_t *)NULL);
4117 pushed_string_list->flags = PSH_DPAREN;
4118 if ((parser_state & PST_CASEPAT) == 0)
4119 parser_state |= PST_SUBSHELL;
4127 return -2; /* XXX */
4130 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
4131 If not, assume it's a nested subshell for backwards compatibility and
4132 return 0. In any case, put the characters we've consumed into a locally-
4133 allocated buffer and make *ep point to that buffer. Return -1 on an
4134 error, for example EOF. */
4136 parse_arith_cmd (ep, adddq)
4140 int exp_lineno, rval, c;
4141 char *ttok, *tokstr;
4144 exp_lineno = line_number;
4145 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
4147 if (ttok == &matched_pair_error)
4149 /* Check that the next character is the closing right paren. If
4150 not, this is a syntax error. ( */
4155 tokstr = (char *)xmalloc (ttoklen + 4);
4157 /* if ADDDQ != 0 then (( ... )) -> "..." */
4158 if (rval == 1 && adddq) /* arith cmd, add double quotes */
4161 strncpy (tokstr + 1, ttok, ttoklen - 1);
4162 tokstr[ttoklen] = '"';
4163 tokstr[ttoklen+1] = '\0';
4165 else if (rval == 1) /* arith cmd, don't add double quotes */
4167 strncpy (tokstr, ttok, ttoklen - 1);
4168 tokstr[ttoklen-1] = '\0';
4170 else /* nested subshell */
4173 strncpy (tokstr + 1, ttok, ttoklen - 1);
4174 tokstr[ttoklen] = ')';
4175 tokstr[ttoklen+1] = c;
4176 tokstr[ttoklen+2] = '\0';
4183 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
4185 #if defined (COND_COMMAND)
4191 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
4192 parser_error (cond_lineno, _("unexpected EOF while looking for `]]'"));
4193 else if (cond_token != COND_ERROR)
4195 if (etext = error_token_from_token (cond_token))
4197 parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext);
4201 parser_error (cond_lineno, _("syntax error in conditional expression"));
4208 return (cond_or ());
4217 if (cond_token == OR_OR)
4220 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
4231 if (cond_token == AND_AND)
4234 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
4240 cond_skip_newlines ()
4242 while ((cond_token = read_token (READ)) == '\n')
4244 if (SHOULD_PROMPT ())
4247 return (cond_token);
4250 #define COND_RETURN_ERROR() \
4251 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
4257 COND_COM *term, *tleft, *tright;
4261 /* Read a token. It can be a left paren, a `!', a unary operator, or a
4262 word that should be the first argument of a binary operator. Start by
4263 skipping newlines, since this is a compound command. */
4264 tok = cond_skip_newlines ();
4265 lineno = line_number;
4266 if (tok == COND_END)
4268 COND_RETURN_ERROR ();
4270 else if (tok == '(')
4272 term = cond_expr ();
4273 if (cond_token != ')')
4276 dispose_cond_node (term); /* ( */
4277 if (etext = error_token_from_token (cond_token))
4279 parser_error (lineno, _("unexpected token `%s', expected `)'"), etext);
4283 parser_error (lineno, _("expected `)'"));
4284 COND_RETURN_ERROR ();
4286 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
4287 (void)cond_skip_newlines ();
4289 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
4292 dispose_word (yylval.word); /* not needed */
4293 term = cond_term ();
4295 term->flags |= CMD_INVERT_RETURN;
4297 else if (tok == WORD && yylval.word->word[0] == '-' && yylval.word->word[2] == 0 && test_unop (yylval.word->word))
4300 tok = read_token (READ);
4303 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4304 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
4309 if (etext = error_token_from_token (tok))
4311 parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext);
4315 parser_error (line_number, _("unexpected argument to conditional unary operator"));
4316 COND_RETURN_ERROR ();
4319 (void)cond_skip_newlines ();
4321 else if (tok == WORD) /* left argument to binary operator */
4324 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4327 tok = read_token (READ);
4328 if (tok == WORD && test_binop (yylval.word->word))
4331 if (op->word[0] == '=' && (op->word[1] == '\0' || (op->word[1] == '=' && op->word[2] == '\0')))
4332 parser_state |= PST_EXTPAT;
4333 else if (op->word[0] == '!' && op->word[1] == '=' && op->word[2] == '\0')
4334 parser_state |= PST_EXTPAT;
4336 #if defined (COND_REGEXP)
4337 else if (tok == WORD && STREQ (yylval.word->word, "=~"))
4340 parser_state |= PST_REGEXP;
4343 else if (tok == '<' || tok == '>')
4344 op = make_word_from_token (tok); /* ( */
4345 /* There should be a check before blindly accepting the `)' that we have
4346 seen the opening `('. */
4347 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
4349 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
4350 the test command. Similarly for [[ x && expr ]] or
4351 [[ x || expr ]] or [[ (x) ]]. */
4352 op = make_word ("-n");
4353 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
4359 if (etext = error_token_from_token (tok))
4361 parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext);
4365 parser_error (line_number, _("conditional binary operator expected"));
4366 dispose_cond_node (tleft);
4367 COND_RETURN_ERROR ();
4371 if (parser_state & PST_EXTPAT)
4373 tok = read_token (READ);
4374 if (parser_state & PST_EXTPAT)
4375 extended_glob = global_extglob;
4376 parser_state &= ~(PST_REGEXP|PST_EXTPAT);
4380 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4381 term = make_cond_node (COND_BINARY, op, tleft, tright);
4385 if (etext = error_token_from_token (tok))
4387 parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext);
4391 parser_error (line_number, _("unexpected argument to conditional binary operator"));
4392 dispose_cond_node (tleft);
4394 COND_RETURN_ERROR ();
4397 (void)cond_skip_newlines ();
4402 parser_error (line_number, _("unexpected token `%c' in conditional command"), tok);
4403 else if (etext = error_token_from_token (tok))
4405 parser_error (line_number, _("unexpected token `%s' in conditional command"), etext);
4409 parser_error (line_number, _("unexpected token %d in conditional command"), tok);
4410 COND_RETURN_ERROR ();
4415 /* This is kind of bogus -- we slip a mini recursive-descent parser in
4416 here to handle the conditional statement syntax. */
4418 parse_cond_command ()
4422 global_extglob = extended_glob;
4423 cexp = cond_expr ();
4424 return (make_cond_command (cexp));
4428 #if defined (ARRAY_VARS)
4429 /* When this is called, it's guaranteed that we don't care about anything
4430 in t beyond i. We do save and restore the chars, though. */
4432 token_is_assignment (t, i)
4436 unsigned char c, c1;
4439 c = t[i]; c1 = t[i+1];
4440 t[i] = '='; t[i+1] = '\0';
4441 r = assignment (t, (parser_state & PST_COMPASSIGN) != 0);
4442 t[i] = c; t[i+1] = c1;
4446 /* XXX - possible changes here for `+=' */
4448 token_is_ident (t, i)
4457 r = legal_identifier (t);
4464 read_token_word (character)
4467 /* The value for YYLVAL when a WORD is read. */
4468 WORD_DESC *the_word;
4470 /* Index into the token that we are building. */
4473 /* ALL_DIGITS becomes zero when we see a non-digit. */
4474 int all_digit_token;
4476 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
4479 /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
4481 int compound_assignment;
4483 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
4486 /* Non-zero means to ignore the value of the next character, and just
4487 to add it no matter what. */
4488 int pass_next_character;
4490 /* The current delimiting character. */
4492 int result, peek_char;
4493 char *ttok, *ttrans;
4494 int ttoklen, ttranslen;
4497 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
4498 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
4501 all_digit_token = DIGIT (character);
4502 dollar_present = quoted = pass_next_character = compound_assignment = 0;
4506 if (character == EOF)
4509 if (pass_next_character)
4511 pass_next_character = 0;
4512 goto got_escaped_character;
4515 cd = current_delimiter (dstack);
4517 /* Handle backslashes. Quote lots of things when not inside of
4518 double-quotes, quote some things inside of double-quotes. */
4519 if MBTEST(character == '\\')
4521 peek_char = shell_getc (0);
4523 /* Backslash-newline is ignored in all cases except
4524 when quoted with single quotes. */
4525 if (peek_char == '\n')
4528 goto next_character;
4532 shell_ungetc (peek_char);
4534 /* If the next character is to be quoted, note it now. */
4535 if (cd == 0 || cd == '`' ||
4536 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
4537 pass_next_character++;
4544 /* Parse a matched pair of quote characters. */
4545 if MBTEST(shellquote (character))
4547 push_delimiter (dstack, character);
4548 ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0);
4549 pop_delimiter (dstack);
4550 if (ttok == &matched_pair_error)
4551 return -1; /* Bail immediately. */
4552 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4553 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4554 token[token_index++] = character;
4555 strcpy (token + token_index, ttok);
4556 token_index += ttoklen;
4557 all_digit_token = 0;
4559 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
4561 goto next_character;
4565 /* When parsing a regexp as a single word inside a conditional command,
4566 we need to special-case characters special to both the shell and
4567 regular expressions. Right now, that is only '(' and '|'. */ /*)*/
4568 if MBTEST((parser_state & PST_REGEXP) && (character == '(' || character == '|')) /*)*/
4570 if (character == '|')
4573 push_delimiter (dstack, character);
4574 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4575 pop_delimiter (dstack);
4576 if (ttok == &matched_pair_error)
4577 return -1; /* Bail immediately. */
4578 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4579 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4580 token[token_index++] = character;
4581 strcpy (token + token_index, ttok);
4582 token_index += ttoklen;
4584 dollar_present = all_digit_token = 0;
4585 goto next_character;
4587 #endif /* COND_REGEXP */
4589 #ifdef EXTENDED_GLOB
4590 /* Parse a ksh-style extended pattern matching specification. */
4591 if MBTEST(extended_glob && PATTERN_CHAR (character))
4593 peek_char = shell_getc (1);
4594 if MBTEST(peek_char == '(') /* ) */
4596 push_delimiter (dstack, peek_char);
4597 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4598 pop_delimiter (dstack);
4599 if (ttok == &matched_pair_error)
4600 return -1; /* Bail immediately. */
4601 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,
4603 TOKEN_DEFAULT_GROW_SIZE);
4604 token[token_index++] = character;
4605 token[token_index++] = peek_char;
4606 strcpy (token + token_index, ttok);
4607 token_index += ttoklen;
4609 dollar_present = all_digit_token = 0;
4610 goto next_character;
4613 shell_ungetc (peek_char);
4615 #endif /* EXTENDED_GLOB */
4617 /* If the delimiter character is not single quote, parse some of
4618 the shell expansions that must be read as a single word. */
4619 if (shellexp (character))
4621 peek_char = shell_getc (1);
4622 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
4623 if MBTEST(peek_char == '(' ||
4624 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
4626 if (peek_char == '{') /* } */
4627 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE|P_DOLBRACE);
4628 else if (peek_char == '(') /* ) */
4630 /* XXX - push and pop the `(' as a delimiter for use by
4631 the command-oriented-history code. This way newlines
4632 appearing in the $(...) string get added to the
4633 history literally rather than causing a possibly-
4634 incorrect `;' to be added. ) */
4635 push_delimiter (dstack, peek_char);
4636 ttok = parse_comsub (cd, '(', ')', &ttoklen, P_COMMAND);
4637 pop_delimiter (dstack);
4640 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
4641 if (ttok == &matched_pair_error)
4642 return -1; /* Bail immediately. */
4643 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,
4645 TOKEN_DEFAULT_GROW_SIZE);
4646 token[token_index++] = character;
4647 token[token_index++] = peek_char;
4648 strcpy (token + token_index, ttok);
4649 token_index += ttoklen;
4652 all_digit_token = 0;
4653 goto next_character;
4655 /* This handles $'...' and $"..." new-style quoted strings. */
4656 else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
4660 first_line = line_number;
4661 push_delimiter (dstack, peek_char);
4662 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
4664 (peek_char == '\'') ? P_ALLOWESC : 0);
4665 pop_delimiter (dstack);
4666 if (ttok == &matched_pair_error)
4668 if (peek_char == '\'')
4670 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
4673 /* Insert the single quotes and correctly quote any
4674 embedded single quotes (allowed because P_ALLOWESC was
4675 passed to parse_matched_pair). */
4676 ttok = sh_single_quote (ttrans);
4678 ttranslen = strlen (ttok);
4683 /* Try to locale-expand the converted string. */
4684 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
4687 /* Add the double quotes back */
4688 ttok = sh_mkdoublequoted (ttrans, ttranslen, 0);
4694 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 1,
4696 TOKEN_DEFAULT_GROW_SIZE);
4697 strcpy (token + token_index, ttrans);
4698 token_index += ttranslen;
4701 all_digit_token = 0;
4702 goto next_character;
4704 /* This could eventually be extended to recognize all of the
4705 shell's single-character parameter expansions, and set flags.*/
4706 else if MBTEST(character == '$' && peek_char == '$')
4708 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
4710 TOKEN_DEFAULT_GROW_SIZE);
4711 token[token_index++] = '$';
4712 token[token_index++] = peek_char;
4714 all_digit_token = 0;
4715 goto next_character;
4718 shell_ungetc (peek_char);
4721 #if defined (ARRAY_VARS)
4722 /* Identify possible array subscript assignment; match [...]. If
4723 parser_state&PST_COMPASSIGN, we need to parse [sub]=words treating
4724 `sub' as if it were enclosed in double quotes. */
4725 else if MBTEST(character == '[' && /* ] */
4726 ((token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) ||
4727 (token_index == 0 && (parser_state&PST_COMPASSIGN))))
4729 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, P_ARRAYSUB);
4730 if (ttok == &matched_pair_error)
4731 return -1; /* Bail immediately. */
4732 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4734 TOKEN_DEFAULT_GROW_SIZE);
4735 token[token_index++] = character;
4736 strcpy (token + token_index, ttok);
4737 token_index += ttoklen;
4739 all_digit_token = 0;
4740 goto next_character;
4742 /* Identify possible compound array variable assignment. */
4743 else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index))
4745 peek_char = shell_getc (1);
4746 if MBTEST(peek_char == '(') /* ) */
4748 ttok = parse_compound_assignment (&ttoklen);
4750 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
4752 TOKEN_DEFAULT_GROW_SIZE);
4754 token[token_index++] = '=';
4755 token[token_index++] = '(';
4758 strcpy (token + token_index, ttok);
4759 token_index += ttoklen;
4761 token[token_index++] = ')';
4763 all_digit_token = 0;
4764 compound_assignment = 1;
4766 goto next_character;
4768 goto got_token; /* ksh93 seems to do this */
4772 shell_ungetc (peek_char);
4776 /* When not parsing a multi-character word construct, shell meta-
4777 characters break words. */
4778 if MBTEST(shellbreak (character))
4780 shell_ungetc (character);
4786 if (character == CTLESC || character == CTLNUL)
4788 RESIZE_MALLOCED_BUFFER (token, token_index, 2, token_buffer_size,
4789 TOKEN_DEFAULT_GROW_SIZE);
4790 token[token_index++] = CTLESC;
4793 got_escaped_character:
4794 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
4795 TOKEN_DEFAULT_GROW_SIZE);
4797 token[token_index++] = character;
4799 all_digit_token &= DIGIT (character);
4800 dollar_present |= character == '$';
4803 if (character == '\n' && SHOULD_PROMPT ())
4806 /* We want to remove quoted newlines (that is, a \<newline> pair)
4807 unless we are within single quotes or pass_next_character is
4808 set (the shell equivalent of literal-next). */
4809 cd = current_delimiter (dstack);
4810 character = shell_getc (cd != '\'' && pass_next_character == 0);
4811 } /* end for (;;) */
4815 /* Calls to RESIZE_MALLOCED_BUFFER ensure there is sufficient room. */
4816 token[token_index] = '\0';
4818 /* Check to see what thing we should return. If the last_read_token
4819 is a `<', or a `&', or the character which ended this token is
4820 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
4821 Otherwise, it is just a word, and should be returned as such. */
4822 if MBTEST(all_digit_token && (character == '<' || character == '>' ||
4823 last_read_token == LESS_AND ||
4824 last_read_token == GREATER_AND))
4826 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
4828 yylval.number = lvalue;
4833 /* Check for special case tokens. */
4834 result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
4839 /* Posix.2 does not allow reserved words to be aliased, so check for all
4840 of them, including special cases, before expanding the current token
4842 if MBTEST(posixly_correct)
4843 CHECK_FOR_RESERVED_WORD (token);
4845 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
4846 inhibits alias expansion. */
4847 if (expand_aliases && quoted == 0)
4849 result = alias_expand_token (token);
4850 if (result == RE_READ_TOKEN)
4851 return (RE_READ_TOKEN);
4852 else if (result == NO_EXPANSION)
4853 parser_state &= ~PST_ALEXPNEXT;
4856 /* If not in Posix.2 mode, check for reserved words after alias
4858 if MBTEST(posixly_correct == 0)
4860 CHECK_FOR_RESERVED_WORD (token);
4862 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
4863 the_word->word = (char *)xmalloc (1 + token_index);
4864 the_word->flags = 0;
4865 strcpy (the_word->word, token);
4867 the_word->flags |= W_HASDOLLAR;
4869 the_word->flags |= W_QUOTED; /*(*/
4870 if (compound_assignment && token[token_index-1] == ')')
4871 the_word->flags |= W_COMPASSIGN;
4872 /* A word is an assignment if it appears at the beginning of a
4873 simple command, or after another assignment word. This is
4874 context-dependent, so it cannot be handled in the grammar. */
4875 if (assignment (token, (parser_state & PST_COMPASSIGN) != 0))
4877 the_word->flags |= W_ASSIGNMENT;
4878 /* Don't perform word splitting on assignment statements. */
4879 if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0)
4881 the_word->flags |= W_NOSPLIT;
4882 if (parser_state & PST_COMPASSIGN)
4883 the_word->flags |= W_NOGLOB; /* XXX - W_NOBRACE? */
4887 if (command_token_position (last_read_token))
4890 b = builtin_address_internal (token, 0);
4891 if (b && (b->flags & ASSIGNMENT_BUILTIN))
4892 parser_state |= PST_ASSIGNOK;
4893 else if (STREQ (token, "eval") || STREQ (token, "let"))
4894 parser_state |= PST_ASSIGNOK;
4897 yylval.word = the_word;
4899 if (token[0] == '{' && token[token_index-1] == '}' &&
4900 (character == '<' || character == '>'))
4902 /* can use token; already copied to the_word */
4903 token[token_index-1] = '\0';
4904 #if defined (ARRAY_VARS)
4905 if (legal_identifier (token+1) || valid_array_reference (token+1))
4907 if (legal_identifier (token+1))
4910 strcpy (the_word->word, token+1);
4911 /*itrace("read_token_word: returning REDIR_WORD for %s", the_word->word);*/
4912 return (REDIR_WORD);
4916 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
4917 ? ASSIGNMENT_WORD : WORD;
4919 switch (last_read_token)
4922 parser_state |= PST_ALLOWOPNBRC;
4923 function_dstart = line_number;
4928 if (word_top < MAX_CASE_NEST)
4930 word_lineno[word_top] = line_number;
4937 /* Return 1 if TOKSYM is a token that after being read would allow
4938 a reserved word to be seen, else 0. */
4940 reserved_word_acceptable (toksym)
4977 #if defined (COPROCESS_SUPPORT)
4978 if (last_read_token == WORD && token_before_that == COPROC)
4981 if (last_read_token == WORD && token_before_that == FUNCTION)
4987 /* Return the index of TOKEN in the alist of reserved words, or -1 if
4988 TOKEN is not a shell reserved word. */
4990 find_reserved_word (tokstr)
4994 for (i = 0; word_token_alist[i].word; i++)
4995 if (STREQ (tokstr, word_token_alist[i].word))
5000 /* An interface to let the rest of the shell (primarily the completion
5001 system) know what the parser is expecting. */
5003 parser_in_command_position ()
5005 return (command_token_position (last_read_token));
5009 #if defined (READLINE)
5010 /* Called after each time readline is called. This insures that whatever
5011 the new prompt string is gets propagated to readline's local prompt
5014 reset_readline_prompt ()
5018 if (prompt_string_pointer)
5020 temp_prompt = (*prompt_string_pointer)
5021 ? decode_prompt_string (*prompt_string_pointer)
5024 if (temp_prompt == 0)
5026 temp_prompt = (char *)xmalloc (1);
5027 temp_prompt[0] = '\0';
5030 FREE (current_readline_prompt);
5031 current_readline_prompt = temp_prompt;
5034 #endif /* READLINE */
5037 #if defined (HISTORY)
5038 /* A list of tokens which can be followed by newlines, but not by
5039 semi-colons. When concatenating multiple lines of history, the
5040 newline separator for such tokens is replaced with a space. */
5041 static const int no_semi_successors[] = {
5042 '\n', '{', '(', ')', ';', '&', '|',
5043 CASE, DO, ELSE, IF, SEMI_SEMI, SEMI_AND, SEMI_SEMI_AND, THEN, UNTIL,
5044 WHILE, AND_AND, OR_OR, IN,
5048 /* If we are not within a delimited expression, try to be smart
5049 about which separators can be semi-colons and which must be
5050 newlines. Returns the string that should be added into the
5051 history entry. LINE is the line we're about to add; it helps
5052 make some more intelligent decisions in certain cases. */
5054 history_delimiting_chars (line)
5057 static int last_was_heredoc = 0; /* was the last entry the start of a here document? */
5060 if ((parser_state & PST_HEREDOC) == 0)
5061 last_was_heredoc = 0;
5063 if (dstack.delimiter_depth != 0)
5066 /* We look for current_command_line_count == 2 because we are looking to
5067 add the first line of the body of the here document (the second line
5068 of the command). We also keep LAST_WAS_HEREDOC as a private sentinel
5069 variable to note when we think we added the first line of a here doc
5070 (the one with a "<<" somewhere in it) */
5071 if (parser_state & PST_HEREDOC)
5073 if (last_was_heredoc)
5075 last_was_heredoc = 0;
5078 return (current_command_line_count == 2 ? "\n" : "");
5081 if (parser_state & PST_COMPASSIGN)
5084 /* First, handle some special cases. */
5086 /* If we just read `()', assume it's a function definition, and don't
5087 add a semicolon. If the token before the `)' was not `(', and we're
5088 not in the midst of parsing a case statement, assume it's a
5089 parenthesized command and add the semicolon. */
5091 if (token_before_that == ')')
5093 if (two_tokens_ago == '(') /*)*/ /* function def */
5095 /* This does not work for subshells inside case statement
5096 command lists. It's a suboptimal solution. */
5097 else if (parser_state & PST_CASESTMT) /* case statement pattern */
5100 return "; "; /* (...) subshell */
5102 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
5103 return " "; /* function def using `function name' without `()' */
5105 /* If we're not in a here document, but we think we're about to parse one,
5106 and we would otherwise return a `;', return a newline to delimit the
5107 line with the here-doc delimiter */
5108 else if ((parser_state & PST_HEREDOC) == 0 && current_command_line_count > 1 && last_read_token == '\n' && strstr (line, "<<"))
5110 last_was_heredoc = 1;
5114 else if (token_before_that == WORD && two_tokens_ago == FOR)
5116 /* Tricky. `for i\nin ...' should not have a semicolon, but
5117 `for i\ndo ...' should. We do what we can. */
5118 for (i = shell_input_line_index; whitespace (shell_input_line[i]); i++)
5120 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
5124 else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT))
5127 for (i = 0; no_semi_successors[i]; i++)
5129 if (token_before_that == no_semi_successors[i])
5135 #endif /* HISTORY */
5137 /* Issue a prompt, or prepare to issue a prompt when the next character
5144 if (interactive == 0 || expanding_alias ()) /* XXX */
5147 ps1_prompt = get_string_value ("PS1");
5148 ps2_prompt = get_string_value ("PS2");
5150 if (!prompt_string_pointer)
5151 prompt_string_pointer = &ps1_prompt;
5153 temp_prompt = *prompt_string_pointer
5154 ? decode_prompt_string (*prompt_string_pointer)
5157 if (temp_prompt == 0)
5159 temp_prompt = (char *)xmalloc (1);
5160 temp_prompt[0] = '\0';
5163 current_prompt_string = *prompt_string_pointer;
5164 prompt_string_pointer = &ps2_prompt;
5166 #if defined (READLINE)
5167 if (!no_line_editing)
5169 FREE (current_readline_prompt);
5170 current_readline_prompt = temp_prompt;
5173 #endif /* READLINE */
5175 FREE (current_decoded_prompt);
5176 current_decoded_prompt = temp_prompt;
5181 get_current_prompt_level ()
5183 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
5187 set_current_prompt_level (x)
5190 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
5191 current_prompt_string = *prompt_string_pointer;
5197 fprintf (stderr, "%s", current_decoded_prompt);
5201 /* Return a string which will be printed as a prompt. The string
5202 may contain special characters which are decoded as follows:
5205 \d the date in Day Mon Date format
5206 \e escape (ascii 033)
5207 \h the hostname up to the first `.'
5209 \j the number of active jobs
5210 \l the basename of the shell's tty device name
5213 \s the name of the shell
5214 \t the time in 24-hour hh:mm:ss format
5215 \T the time in 12-hour hh:mm:ss format
5216 \@ the time in 12-hour hh:mm am/pm format
5217 \A the time in 24-hour hh:mm format
5218 \D{fmt} the result of passing FMT to strftime(3)
5220 \v the version of bash (e.g., 2.00)
5221 \V the release of bash, version + patchlevel (e.g., 2.00.0)
5222 \w the current working directory
5223 \W the last element of $PWD
5224 \! the history number of this command
5225 \# the command number of this command
5226 \$ a $ or a # if you are root
5227 \nnn character code nnn in octal
5229 \[ begin a sequence of non-printing chars
5230 \] end a sequence of non-printing chars
5232 #define PROMPT_GROWTH 48
5234 decode_prompt_string (string)
5239 struct dstack save_dstack;
5240 int last_exit_value, last_comsub_pid;
5241 #if defined (PROMPT_STRING_DECODE)
5242 int result_size, result_index;
5244 char *temp, octal_string[4];
5250 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
5251 result[result_index = 0] = 0;
5252 temp = (char *)NULL;
5254 while (c = *string++)
5256 if (posixly_correct && c == '!')
5260 temp = savestring ("!");
5265 #if !defined (HISTORY)
5266 temp = savestring ("1");
5268 temp = itos (history_number ());
5269 #endif /* HISTORY */
5270 string--; /* add_string increments string again. */
5288 strncpy (octal_string, string, 3);
5289 octal_string[3] = '\0';
5291 n = read_octal (octal_string);
5292 temp = (char *)xmalloc (3);
5294 if (n == CTLESC || n == CTLNUL)
5311 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
5314 c = 0; /* tested at add_string: */
5322 /* Make the current time/date into a string. */
5323 (void) time (&the_time);
5324 #if defined (HAVE_TZSET)
5325 sv_tz ("TZ"); /* XXX -- just make sure */
5327 tm = localtime (&the_time);
5330 n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm);
5332 n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm);
5334 n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm);
5336 n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm);
5338 n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm);
5343 timebuf[sizeof(timebuf) - 1] = '\0';
5345 temp = savestring (timebuf);
5348 case 'D': /* strftime format */
5349 if (string[1] != '{') /* } */
5352 (void) time (&the_time);
5353 tm = localtime (&the_time);
5354 string += 2; /* skip { */
5355 timefmt = xmalloc (strlen (string) + 3);
5356 for (t = timefmt; *string && *string != '}'; )
5359 c = *string; /* tested at add_string */
5360 if (timefmt[0] == '\0')
5363 timefmt[1] = 'X'; /* locale-specific current time */
5366 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
5372 timebuf[sizeof(timebuf) - 1] = '\0';
5374 if (promptvars || posixly_correct)
5375 /* Make sure that expand_prompt_string is called with a
5376 second argument of Q_DOUBLE_QUOTES if we use this
5378 temp = sh_backslash_quote_for_double_quotes (timebuf);
5380 temp = savestring (timebuf);
5384 temp = (char *)xmalloc (3);
5385 temp[0] = no_line_editing ? '\n' : '\r';
5386 temp[1] = no_line_editing ? '\0' : '\n';
5391 temp = base_pathname (shell_name);
5392 temp = savestring (temp);
5397 temp = (char *)xmalloc (16);
5399 strcpy (temp, dist_version);
5401 sprintf (temp, "%s.%d", dist_version, patch_level);
5407 /* Use the value of PWD because it is much more efficient. */
5408 char t_string[PATH_MAX];
5411 temp = get_string_value ("PWD");
5415 if (getcwd (t_string, sizeof(t_string)) == 0)
5421 tlen = strlen (t_string);
5425 tlen = sizeof (t_string) - 1;
5426 strncpy (t_string, temp, tlen);
5428 t_string[tlen] = '\0';
5430 #if defined (MACOSX)
5431 /* Convert from "fs" format to "input" format */
5432 temp = fnx_fromfs (t_string, strlen (t_string));
5433 if (temp != t_string)
5434 strcpy (t_string, temp);
5437 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
5438 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
5439 /* Abbreviate \W as ~ if $PWD == $HOME */
5440 if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0))
5442 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
5444 t = strrchr (t_string, '/');
5446 memmove (t_string, t + 1, strlen (t)); /* strlen(t) to copy NULL */
5450 #undef DOUBLE_SLASH_ROOT
5453 /* polite_directory_format is guaranteed to return a string
5454 no longer than PATH_MAX - 1 characters. */
5455 temp = polite_directory_format (t_string);
5456 if (temp != t_string)
5457 strcpy (t_string, temp);
5460 temp = trim_pathname (t_string, PATH_MAX - 1);
5461 /* If we're going to be expanding the prompt string later,
5462 quote the directory name. */
5463 if (promptvars || posixly_correct)
5464 /* Make sure that expand_prompt_string is called with a
5465 second argument of Q_DOUBLE_QUOTES if we use this
5467 temp = sh_backslash_quote_for_double_quotes (t_string);
5469 temp = savestring (t_string);
5475 if (current_user.user_name == 0)
5476 get_current_user_info ();
5477 temp = savestring (current_user.user_name);
5482 temp = savestring (current_host_name);
5483 if (c == 'h' && (t = (char *)strchr (temp, '.')))
5488 temp = itos (current_command_number);
5492 #if !defined (HISTORY)
5493 temp = savestring ("1");
5495 temp = itos (history_number ());
5496 #endif /* HISTORY */
5500 t = temp = (char *)xmalloc (3);
5501 if ((promptvars || posixly_correct) && (current_user.euid != 0))
5503 *t++ = current_user.euid == 0 ? '#' : '$';
5508 temp = itos (count_all_jobs ());
5512 #if defined (HAVE_TTYNAME)
5513 temp = (char *)ttyname (fileno (stdin));
5514 t = temp ? base_pathname (temp) : "tty";
5515 temp = savestring (t);
5517 temp = savestring ("tty");
5518 #endif /* !HAVE_TTYNAME */
5521 #if defined (READLINE)
5524 if (no_line_editing)
5529 temp = (char *)xmalloc (3);
5530 n = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
5532 if (n == CTLESC || n == CTLNUL)
5537 #endif /* READLINE */
5543 temp = (char *)xmalloc (2);
5550 else /* (c == '\\') */
5557 temp = (char *)xmalloc (3);
5566 sub_append_string (temp, result, &result_index, &result_size);
5567 temp = (char *)NULL; /* Freed in sub_append_string (). */
5568 result[result_index] = '\0';
5574 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
5575 result[result_index++] = c;
5576 result[result_index] = '\0';
5579 #else /* !PROMPT_STRING_DECODE */
5580 result = savestring (string);
5581 #endif /* !PROMPT_STRING_DECODE */
5583 /* Save the delimiter stack and point `dstack' to temp space so any
5584 command substitutions in the prompt string won't result in screwing
5585 up the parser's quoting state. */
5586 save_dstack = dstack;
5587 dstack = temp_dstack;
5588 dstack.delimiter_depth = 0;
5590 /* Perform variable and parameter expansion and command substitution on
5591 the prompt string. */
5592 if (promptvars || posixly_correct)
5594 last_exit_value = last_command_exit_value;
5595 last_comsub_pid = last_command_subst_pid;
5596 list = expand_prompt_string (result, Q_DOUBLE_QUOTES, 0);
5598 result = string_list (list);
5599 dispose_words (list);
5600 last_command_exit_value = last_exit_value;
5601 last_command_subst_pid = last_comsub_pid;
5605 t = dequote_string (result);
5610 dstack = save_dstack;
5615 /************************************************
5619 ************************************************/
5621 /* Report a syntax error, and restart the parser. Call here for fatal
5627 report_syntax_error ((char *)NULL);
5633 error_token_from_token (tok)
5638 if (t = find_token_in_alist (tok, word_token_alist, 0))
5641 if (t = find_token_in_alist (tok, other_token_alist, 0))
5645 /* This stuff is dicy and needs closer inspection */
5646 switch (current_token)
5649 case ASSIGNMENT_WORD:
5651 t = savestring (yylval.word->word);
5654 t = itos (yylval.number);
5657 if (yylval.word_list)
5658 t = string_list (yylval.word_list);
5660 case ARITH_FOR_EXPRS:
5661 if (yylval.word_list)
5662 t = string_list_internal (yylval.word_list, " ; ");
5665 t = (char *)NULL; /* punt */
5673 error_token_from_text ()
5678 t = shell_input_line;
5679 i = shell_input_line_index;
5683 if (i && t[i] == '\0')
5686 while (i && (whitespace (t[i]) || t[i] == '\n'))
5692 while (i && (member (t[i], " \n\t;|&") == 0))
5695 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
5698 /* Return our idea of the offending token. */
5699 if (token_end || (i == 0 && token_end == 0))
5702 msg = substring (t, i, token_end);
5703 else /* one-character token */
5705 msg = (char *)xmalloc (2);
5715 print_offending_line ()
5720 msg = savestring (shell_input_line);
5721 token_end = strlen (msg);
5722 while (token_end && msg[token_end - 1] == '\n')
5723 msg[--token_end] = '\0';
5725 parser_error (line_number, "`%s'", msg);
5729 /* Report a syntax error with line numbers, etc.
5730 Call here for recoverable errors. If you have a message to print,
5731 then place it in MESSAGE, otherwise pass NULL and this will figure
5732 out an appropriate message for you. */
5734 report_syntax_error (message)
5741 parser_error (line_number, "%s", message);
5742 if (interactive && EOF_Reached)
5744 last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE;
5748 /* If the line of input we're reading is not null, try to find the
5749 objectionable token. First, try to figure out what token the
5750 parser's complaining about by looking at current_token. */
5751 if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
5753 if (ansic_shouldquote (msg))
5755 p = ansic_quote (msg, 0, NULL);
5759 parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
5762 if (interactive == 0)
5763 print_offending_line ();
5765 last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE;
5769 /* If looking at the current token doesn't prove fruitful, try to find the
5770 offending token by analyzing the text of the input line near the current
5771 input line index and report what we find. */
5772 if (shell_input_line && *shell_input_line)
5774 msg = error_token_from_text ();
5777 parser_error (line_number, _("syntax error near `%s'"), msg);
5781 /* If not interactive, print the line containing the error. */
5782 if (interactive == 0)
5783 print_offending_line ();
5787 msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error");
5788 parser_error (line_number, "%s", msg);
5789 /* When the shell is interactive, this file uses EOF_Reached
5790 only for error reporting. Other mechanisms are used to
5791 decide whether or not to exit. */
5792 if (interactive && EOF_Reached)
5796 last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE;
5799 /* ??? Needed function. ??? We have to be able to discard the constructs
5800 created during parsing. In the case of error, we want to return
5801 allocated objects to the memory pool. In the case of no error, we want
5802 to throw away the information about where the allocated objects live.
5803 (dispose_command () will actually free the command.) */
5805 discard_parser_constructs (error_p)
5810 /************************************************
5814 ************************************************/
5816 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
5818 /* A flag denoting whether or not ignoreeof is set. */
5821 /* The number of times that we have encountered an EOF character without
5822 another character intervening. When this gets above the limit, the
5823 shell terminates. */
5824 int eof_encountered = 0;
5826 /* The limit for eof_encountered. */
5827 int eof_encountered_limit = 10;
5829 /* If we have EOF as the only input unit, this user wants to leave
5830 the shell. If the shell is not interactive, then just leave.
5831 Otherwise, if ignoreeof is set, and we haven't done this the
5832 required number of times in a row, print a message. */
5834 handle_eof_input_unit ()
5838 /* shell.c may use this to decide whether or not to write out the
5839 history, among other things. We use it only for error reporting
5844 /* If the user wants to "ignore" eof, then let her do so, kind of. */
5847 if (eof_encountered < eof_encountered_limit)
5849 fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
5850 login_shell ? "logout" : "exit");
5852 /* Reset the parsing state. */
5853 last_read_token = current_token = '\n';
5854 /* Reset the prompt string to be $PS1. */
5855 prompt_string_pointer = (char **)NULL;
5861 /* In this case EOF should exit the shell. Do it now. */
5863 exit_builtin ((WORD_LIST *)NULL);
5867 /* We don't write history files, etc., for non-interactive shells. */
5872 /************************************************
5874 * STRING PARSING FUNCTIONS *
5876 ************************************************/
5878 /* It's very important that these two functions treat the characters
5879 between ( and ) identically. */
5881 static WORD_LIST parse_string_error;
5883 /* Take a string and run it through the shell parser, returning the
5884 resultant word list. Used by compound array assignment. */
5886 parse_string_to_word_list (s, flags, whom)
5892 int tok, orig_current_token, orig_line_number, orig_input_terminator;
5893 int orig_line_count;
5894 int old_echo_input, old_expand_aliases;
5895 #if defined (HISTORY)
5896 int old_remember_on_history, old_history_expansion_inhibited;
5899 #if defined (HISTORY)
5900 old_remember_on_history = remember_on_history;
5901 # if defined (BANG_HISTORY)
5902 old_history_expansion_inhibited = history_expansion_inhibited;
5904 bash_history_disable ();
5907 orig_line_number = line_number;
5908 orig_line_count = current_command_line_count;
5909 orig_input_terminator = shell_input_line_terminator;
5910 old_echo_input = echo_input_at_read;
5911 old_expand_aliases = expand_aliases;
5914 last_read_token = WORD; /* WORD to allow reserved words here */
5915 current_command_line_count = 0;
5916 echo_input_at_read = expand_aliases = 0;
5918 with_input_from_string (s, whom);
5919 wl = (WORD_LIST *)NULL;
5922 parser_state |= PST_COMPASSIGN|PST_REPARSE;
5924 while ((tok = read_token (READ)) != yacc_EOF)
5926 if (tok == '\n' && *bash_input.location.string == '\0')
5928 if (tok == '\n') /* Allow newlines in compound assignments */
5930 if (tok != WORD && tok != ASSIGNMENT_WORD)
5932 line_number = orig_line_number + line_number - 1;
5933 orig_current_token = current_token;
5934 current_token = tok;
5935 yyerror (NULL); /* does the right thing */
5936 current_token = orig_current_token;
5939 wl = &parse_string_error;
5942 wl = make_word_list (yylval.word, wl);
5945 last_read_token = '\n';
5948 #if defined (HISTORY)
5949 remember_on_history = old_remember_on_history;
5950 # if defined (BANG_HISTORY)
5951 history_expansion_inhibited = old_history_expansion_inhibited;
5952 # endif /* BANG_HISTORY */
5953 #endif /* HISTORY */
5955 echo_input_at_read = old_echo_input;
5956 expand_aliases = old_expand_aliases;
5958 current_command_line_count = orig_line_count;
5959 shell_input_line_terminator = orig_input_terminator;
5962 parser_state &= ~(PST_COMPASSIGN|PST_REPARSE);
5964 if (wl == &parse_string_error)
5966 last_command_exit_value = EXECUTION_FAILURE;
5967 if (interactive_shell == 0 && posixly_correct)
5968 jump_to_top_level (FORCE_EOF);
5970 jump_to_top_level (DISCARD);
5973 return (REVERSE_LIST (wl, WORD_LIST *));
5977 parse_compound_assignment (retlenp)
5981 int tok, orig_line_number, orig_token_size, orig_last_token, assignok;
5982 char *saved_token, *ret;
5984 saved_token = token;
5985 orig_token_size = token_buffer_size;
5986 orig_line_number = line_number;
5987 orig_last_token = last_read_token;
5989 last_read_token = WORD; /* WORD to allow reserved words here */
5991 token = (char *)NULL;
5992 token_buffer_size = 0;
5994 assignok = parser_state&PST_ASSIGNOK; /* XXX */
5996 wl = (WORD_LIST *)NULL; /* ( */
5997 parser_state |= PST_COMPASSIGN;
5999 while ((tok = read_token (READ)) != ')')
6001 if (tok == '\n') /* Allow newlines in compound assignments */
6003 if (SHOULD_PROMPT ())
6007 if (tok != WORD && tok != ASSIGNMENT_WORD)
6009 current_token = tok; /* for error reporting */
6010 if (tok == yacc_EOF) /* ( */
6011 parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'"));
6013 yyerror(NULL); /* does the right thing */
6016 wl = &parse_string_error;
6019 wl = make_word_list (yylval.word, wl);
6023 token = saved_token;
6024 token_buffer_size = orig_token_size;
6026 parser_state &= ~PST_COMPASSIGN;
6028 if (wl == &parse_string_error)
6030 last_command_exit_value = EXECUTION_FAILURE;
6031 last_read_token = '\n'; /* XXX */
6032 if (interactive_shell == 0 && posixly_correct)
6033 jump_to_top_level (FORCE_EOF);
6035 jump_to_top_level (DISCARD);
6038 last_read_token = orig_last_token; /* XXX - was WORD? */
6042 rl = REVERSE_LIST (wl, WORD_LIST *);
6043 ret = string_list (rl);
6050 *retlenp = (ret && *ret) ? strlen (ret) : 0;
6053 parser_state |= PST_ASSIGNOK;
6058 /************************************************
6060 * SAVING AND RESTORING PARTIAL PARSE STATE *
6062 ************************************************/
6065 save_parser_state (ps)
6066 sh_parser_state_t *ps;
6069 ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t));
6071 return ((sh_parser_state_t *)NULL);
6073 ps->parser_state = parser_state;
6074 ps->token_state = save_token_state ();
6076 ps->input_line_terminator = shell_input_line_terminator;
6077 ps->eof_encountered = eof_encountered;
6079 ps->prompt_string_pointer = prompt_string_pointer;
6081 ps->current_command_line_count = current_command_line_count;
6083 #if defined (HISTORY)
6084 ps->remember_on_history = remember_on_history;
6085 # if defined (BANG_HISTORY)
6086 ps->history_expansion_inhibited = history_expansion_inhibited;
6090 ps->last_command_exit_value = last_command_exit_value;
6091 #if defined (ARRAY_VARS)
6092 ps->pipestatus = save_pipestatus_array ();
6095 ps->last_shell_builtin = last_shell_builtin;
6096 ps->this_shell_builtin = this_shell_builtin;
6098 ps->expand_aliases = expand_aliases;
6099 ps->echo_input_at_read = echo_input_at_read;
6100 ps->need_here_doc = need_here_doc;
6103 ps->token_buffer_size = token_buffer_size;
6104 /* Force reallocation on next call to read_token_word */
6106 token_buffer_size = 0;
6112 restore_parser_state (ps)
6113 sh_parser_state_t *ps;
6118 parser_state = ps->parser_state;
6119 if (ps->token_state)
6121 restore_token_state (ps->token_state);
6122 free (ps->token_state);
6125 shell_input_line_terminator = ps->input_line_terminator;
6126 eof_encountered = ps->eof_encountered;
6128 prompt_string_pointer = ps->prompt_string_pointer;
6130 current_command_line_count = ps->current_command_line_count;
6132 #if defined (HISTORY)
6133 remember_on_history = ps->remember_on_history;
6134 # if defined (BANG_HISTORY)
6135 history_expansion_inhibited = ps->history_expansion_inhibited;
6139 last_command_exit_value = ps->last_command_exit_value;
6140 #if defined (ARRAY_VARS)
6141 restore_pipestatus_array (ps->pipestatus);
6144 last_shell_builtin = ps->last_shell_builtin;
6145 this_shell_builtin = ps->this_shell_builtin;
6147 expand_aliases = ps->expand_aliases;
6148 echo_input_at_read = ps->echo_input_at_read;
6149 need_here_doc = ps->need_here_doc;
6153 token_buffer_size = ps->token_buffer_size;
6156 sh_input_line_state_t *
6157 save_input_line_state (ls)
6158 sh_input_line_state_t *ls;
6161 ls = (sh_input_line_state_t *)xmalloc (sizeof (sh_input_line_state_t));
6163 return ((sh_input_line_state_t *)NULL);
6165 ls->input_line = shell_input_line;
6166 ls->input_line_size = shell_input_line_size;
6167 ls->input_line_len = shell_input_line_len;
6168 ls->input_line_index = shell_input_line_index;
6170 /* force reallocation */
6171 shell_input_line = 0;
6172 shell_input_line_size = shell_input_line_len = shell_input_line_index = 0;
6178 restore_input_line_state (ls)
6179 sh_input_line_state_t *ls;
6181 FREE (shell_input_line);
6182 shell_input_line = ls->input_line;
6183 shell_input_line_size = ls->input_line_size;
6184 shell_input_line_len = ls->input_line_len;
6185 shell_input_line_index = ls->input_line_index;
6187 set_line_mbstate ();
6190 /************************************************
6192 * MULTIBYTE CHARACTER HANDLING *
6194 ************************************************/
6196 #if defined (HANDLE_MULTIBYTE)
6201 size_t i, previ, len;
6202 mbstate_t mbs, prevs;
6205 if (shell_input_line == NULL)
6207 len = strlen (shell_input_line); /* XXX - shell_input_line_len ? */
6208 FREE (shell_input_line_property);
6209 shell_input_line_property = (char *)xmalloc (len + 1);
6211 memset (&prevs, '\0', sizeof (mbstate_t));
6212 for (i = previ = 0; i < len; i++)
6216 c = shell_input_line[i];
6220 for (j = i; j < len; j++)
6221 shell_input_line_property[j] = 1;
6225 mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs);
6226 if (mbclen == 1 || mbclen == (size_t)-1)
6231 else if (mbclen == (size_t)-2)
6233 else if (mbclen > 1)
6241 /* XXX - what to do if mbrlen returns 0? (null wide character) */
6243 for (j = i; j < len; j++)
6244 shell_input_line_property[j] = 1;
6248 shell_input_line_property[i] = mbclen;
6251 #endif /* HANDLE_MULTIBYTE */