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));
171 static int alias_expand_token __P((char *));
172 static int time_command_acceptable __P((void));
173 static int special_case_tokens __P((char *));
174 static int read_token __P((int));
175 static char *parse_matched_pair __P((int, int, int, int *, int));
176 static char *parse_comsub __P((int, int, int, int *, int));
177 #if defined (ARRAY_VARS)
178 static char *parse_compound_assignment __P((int *));
180 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
181 static int parse_dparen __P((int));
182 static int parse_arith_cmd __P((char **, int));
184 #if defined (COND_COMMAND)
185 static void cond_error __P((void));
186 static COND_COM *cond_expr __P((void));
187 static COND_COM *cond_or __P((void));
188 static COND_COM *cond_and __P((void));
189 static COND_COM *cond_term __P((void));
190 static int cond_skip_newlines __P((void));
191 static COMMAND *parse_cond_command __P((void));
193 #if defined (ARRAY_VARS)
194 static int token_is_assignment __P((char *, int));
195 static int token_is_ident __P((char *, int));
197 static int read_token_word __P((int));
198 static void discard_parser_constructs __P((int));
200 static char *error_token_from_token __P((int));
201 static char *error_token_from_text __P((void));
202 static void print_offending_line __P((void));
203 static void report_syntax_error __P((char *));
205 static void handle_eof_input_unit __P((void));
206 static void prompt_again __P((void));
208 static void reset_readline_prompt __P((void));
210 static void print_prompt __P((void));
212 #if defined (HANDLE_MULTIBYTE)
213 static void set_line_mbstate __P((void));
214 static char *shell_input_line_property = NULL;
216 # define set_line_mbstate()
219 extern int yyerror __P((const char *));
225 /* Default prompt strings */
226 char *primary_prompt = PPROMPT;
227 char *secondary_prompt = SPROMPT;
229 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
230 char *ps1_prompt, *ps2_prompt;
232 /* Handle on the current prompt string. Indirectly points through
233 ps1_ or ps2_prompt. */
234 char **prompt_string_pointer = (char **)NULL;
235 char *current_prompt_string;
237 /* Non-zero means we expand aliases in commands. */
238 int expand_aliases = 0;
240 /* If non-zero, the decoded prompt string undergoes parameter and
241 variable substitution, command substitution, arithmetic substitution,
242 string expansion, process substitution, and quote removal in
243 decode_prompt_string. */
246 /* If non-zero, $'...' and $"..." are expanded when they appear within
247 a ${...} expansion, even when the expansion appears within double
249 int extended_quote = 1;
251 /* The number of lines read from input while creating the current command. */
252 int current_command_line_count;
254 /* The number of lines in a command saved while we run parse_and_execute */
255 int saved_command_line_count;
257 /* The token that currently denotes the end of parse. */
260 /* The token currently being read. */
263 /* The current parser state. */
266 /* Variables to manage the task of reading here documents, because we need to
267 defer the reading until after a complete command has been collected. */
268 static REDIRECT *redir_stack[10];
271 /* Where shell input comes from. History expansion is performed on each
272 line when the shell is interactive. */
273 static char *shell_input_line = (char *)NULL;
274 static size_t shell_input_line_index;
275 static size_t shell_input_line_size; /* Amount allocated for shell_input_line. */
276 static size_t shell_input_line_len; /* strlen (shell_input_line) */
278 /* Either zero or EOF. */
279 static int shell_input_line_terminator;
281 /* The line number in a script on which a function definition starts. */
282 static int function_dstart;
284 /* The line number in a script on which a function body starts. */
285 static int function_bstart;
287 /* The line number in a script at which an arithmetic for command starts. */
288 static int arith_for_lineno;
290 /* The decoded prompt string. Used if READLINE is not defined or if
291 editing is turned off. Analogous to current_readline_prompt. */
292 static char *current_decoded_prompt;
294 /* The last read token, or NULL. read_token () uses this for context
296 static int last_read_token;
298 /* The token read prior to last_read_token. */
299 static int token_before_that;
301 /* The token read prior to token_before_that. */
302 static int two_tokens_ago;
304 static int global_extglob;
306 /* The line number in a script where the word in a `case WORD', `select WORD'
307 or `for WORD' begins. This is a nested command maximum, since the array
308 index is decremented after a case, select, or for command is parsed. */
309 #define MAX_CASE_NEST 128
310 static int word_lineno[MAX_CASE_NEST];
311 static int word_top = -1;
313 /* If non-zero, it is the token that we want read_token to return
314 regardless of what text is (or isn't) present to be read. This
315 is reset by read_token. If token_to_read == WORD or
316 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
317 static int token_to_read;
318 static WORD_DESC *word_desc_to_read;
320 static REDIRECTEE source;
321 static REDIRECTEE redir;
325 WORD_DESC *word; /* the word that we read. */
326 int number; /* the number that we read. */
327 WORD_LIST *word_list;
331 PATTERN_LIST *pattern;
334 /* Reserved words. Members of the first group are only recognized
335 in the case that they are preceded by a list_terminator. Members
336 of the second group are for [[...]] commands. Members of the
337 third group are recognized only under special circumstances. */
338 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION COPROC
339 %token COND_START COND_END COND_ERROR
340 %token IN BANG TIME TIMEOPT TIMEIGN
342 /* More general tokens. yylex () knows how to make these. */
343 %token <word> WORD ASSIGNMENT_WORD REDIR_WORD
344 %token <number> NUMBER
345 %token <word_list> ARITH_CMD ARITH_FOR_EXPRS
346 %token <command> COND_CMD
347 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS
348 %token GREATER_AND SEMI_SEMI SEMI_AND SEMI_SEMI_AND
349 %token LESS_LESS_MINUS AND_GREATER AND_GREATER_GREATER LESS_GREATER
350 %token GREATER_BAR BAR_AND
352 /* The types that the various syntactical units return. */
354 %type <command> inputunit command pipeline pipeline_command
355 %type <command> list list0 list1 compound_list simple_list simple_list1
356 %type <command> simple_command shell_command
357 %type <command> for_command select_command case_command group_command
358 %type <command> arith_command
359 %type <command> cond_command
360 %type <command> arith_for_command
361 %type <command> coproc
362 %type <command> function_def function_body if_command elif_clause subshell
363 %type <redirect> redirection redirection_list
364 %type <element> simple_command_element
365 %type <word_list> word_list pattern
366 %type <pattern> pattern_list case_clause_sequence case_clause
367 %type <number> timespec
368 %type <number> list_terminator
372 %left '&' ';' '\n' yacc_EOF
377 inputunit: simple_list simple_list_terminator
379 /* Case of regular command. Discard the error
380 safety net,and return the command just parsed. */
383 /* discard_parser_constructs (0); */
384 if (parser_state & PST_CMDSUBST)
385 parser_state |= PST_EOFTOKEN;
390 /* Case of regular command, but not a very
391 interesting one. Return a NULL command. */
392 global_command = (COMMAND *)NULL;
393 if (parser_state & PST_CMDSUBST)
394 parser_state |= PST_EOFTOKEN;
399 /* Error during parsing. Return NULL command. */
400 global_command = (COMMAND *)NULL;
402 /* discard_parser_constructs (1); */
403 if (interactive && parse_and_execute_level == 0)
414 /* Case of EOF seen by itself. Do ignoreeof or
416 global_command = (COMMAND *)NULL;
417 handle_eof_input_unit ();
423 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
425 { $$ = make_word_list ($2, $1); }
428 redirection: '>' WORD
432 $$ = make_redirection (source, r_output_direction, redir, 0);
438 $$ = make_redirection (source, r_input_direction, redir, 0);
444 $$ = make_redirection (source, r_output_direction, redir, 0);
450 $$ = make_redirection (source, r_input_direction, redir, 0);
452 | REDIR_WORD '>' WORD
454 source.filename = $1;
456 $$ = make_redirection (source, r_output_direction, redir, REDIR_VARASSIGN);
458 | REDIR_WORD '<' WORD
460 source.filename = $1;
462 $$ = make_redirection (source, r_input_direction, redir, REDIR_VARASSIGN);
464 | GREATER_GREATER WORD
468 $$ = make_redirection (source, r_appending_to, redir, 0);
470 | NUMBER GREATER_GREATER WORD
474 $$ = make_redirection (source, r_appending_to, redir, 0);
476 | REDIR_WORD GREATER_GREATER WORD
478 source.filename = $1;
480 $$ = make_redirection (source, r_appending_to, redir, REDIR_VARASSIGN);
486 $$ = make_redirection (source, r_output_force, redir, 0);
488 | NUMBER GREATER_BAR WORD
492 $$ = make_redirection (source, r_output_force, redir, 0);
494 | REDIR_WORD GREATER_BAR WORD
496 source.filename = $1;
498 $$ = make_redirection (source, r_output_force, redir, REDIR_VARASSIGN);
504 $$ = make_redirection (source, r_input_output, redir, 0);
506 | NUMBER LESS_GREATER WORD
510 $$ = make_redirection (source, r_input_output, redir, 0);
512 | REDIR_WORD LESS_GREATER WORD
514 source.filename = $1;
516 $$ = make_redirection (source, r_input_output, redir, REDIR_VARASSIGN);
522 $$ = make_redirection (source, r_reading_until, redir, 0);
523 redir_stack[need_here_doc++] = $$;
525 | NUMBER LESS_LESS WORD
529 $$ = make_redirection (source, r_reading_until, redir, 0);
530 redir_stack[need_here_doc++] = $$;
532 | REDIR_WORD LESS_LESS WORD
534 source.filename = $1;
536 $$ = make_redirection (source, r_reading_until, redir, REDIR_VARASSIGN);
537 redir_stack[need_here_doc++] = $$;
539 | LESS_LESS_MINUS WORD
543 $$ = make_redirection (source, r_deblank_reading_until, redir, 0);
544 redir_stack[need_here_doc++] = $$;
546 | NUMBER LESS_LESS_MINUS WORD
550 $$ = make_redirection (source, r_deblank_reading_until, redir, 0);
551 redir_stack[need_here_doc++] = $$;
553 | REDIR_WORD LESS_LESS_MINUS WORD
555 source.filename = $1;
557 $$ = make_redirection (source, r_deblank_reading_until, redir, REDIR_VARASSIGN);
558 redir_stack[need_here_doc++] = $$;
560 | LESS_LESS_LESS WORD
564 $$ = make_redirection (source, r_reading_string, redir, 0);
566 | NUMBER LESS_LESS_LESS WORD
570 $$ = make_redirection (source, r_reading_string, redir, 0);
572 | REDIR_WORD LESS_LESS_LESS WORD
574 source.filename = $1;
576 $$ = make_redirection (source, r_reading_string, redir, REDIR_VARASSIGN);
582 $$ = make_redirection (source, r_duplicating_input, redir, 0);
584 | NUMBER LESS_AND NUMBER
588 $$ = make_redirection (source, r_duplicating_input, redir, 0);
590 | REDIR_WORD LESS_AND NUMBER
592 source.filename = $1;
594 $$ = make_redirection (source, r_duplicating_input, redir, REDIR_VARASSIGN);
600 $$ = make_redirection (source, r_duplicating_output, redir, 0);
602 | NUMBER GREATER_AND NUMBER
606 $$ = make_redirection (source, r_duplicating_output, redir, 0);
608 | REDIR_WORD GREATER_AND NUMBER
610 source.filename = $1;
612 $$ = make_redirection (source, r_duplicating_output, redir, REDIR_VARASSIGN);
618 $$ = make_redirection (source, r_duplicating_input_word, redir, 0);
620 | NUMBER LESS_AND WORD
624 $$ = make_redirection (source, r_duplicating_input_word, redir, 0);
626 | REDIR_WORD LESS_AND WORD
628 source.filename = $1;
630 $$ = make_redirection (source, r_duplicating_input_word, redir, REDIR_VARASSIGN);
636 $$ = make_redirection (source, r_duplicating_output_word, redir, 0);
638 | NUMBER GREATER_AND WORD
642 $$ = make_redirection (source, r_duplicating_output_word, redir, 0);
644 | REDIR_WORD GREATER_AND WORD
646 source.filename = $1;
648 $$ = make_redirection (source, r_duplicating_output_word, redir, REDIR_VARASSIGN);
654 $$ = make_redirection (source, r_close_this, redir, 0);
656 | NUMBER GREATER_AND '-'
660 $$ = make_redirection (source, r_close_this, redir, 0);
662 | REDIR_WORD GREATER_AND '-'
664 source.filename = $1;
666 $$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
672 $$ = make_redirection (source, r_close_this, redir, 0);
674 | NUMBER LESS_AND '-'
678 $$ = make_redirection (source, r_close_this, redir, 0);
680 | REDIR_WORD LESS_AND '-'
682 source.filename = $1;
684 $$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
690 $$ = make_redirection (source, r_err_and_out, redir, 0);
692 | AND_GREATER_GREATER WORD
696 $$ = make_redirection (source, r_append_err_and_out, redir, 0);
700 simple_command_element: WORD
701 { $$.word = $1; $$.redirect = 0; }
703 { $$.word = $1; $$.redirect = 0; }
705 { $$.redirect = $1; $$.word = 0; }
708 redirection_list: redirection
712 | redirection_list redirection
714 register REDIRECT *t;
716 for (t = $1; t->next; t = t->next)
723 simple_command: simple_command_element
724 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
725 | simple_command simple_command_element
726 { $$ = make_simple_command ($2, $1); }
729 command: simple_command
730 { $$ = clean_simple_command ($1); }
733 | shell_command redirection_list
740 register REDIRECT *t;
741 for (t = tc->redirects; t->next; t = t->next)
755 shell_command: for_command
759 | WHILE compound_list DO compound_list DONE
760 { $$ = make_while_command ($2, $4); }
761 | UNTIL compound_list DO compound_list DONE
762 { $$ = make_until_command ($2, $4); }
779 for_command: FOR WORD newline_list DO compound_list DONE
781 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
782 if (word_top > 0) word_top--;
784 | FOR WORD newline_list '{' compound_list '}'
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 DO compound_list DONE
791 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
792 if (word_top > 0) word_top--;
794 | FOR WORD ';' newline_list '{' compound_list '}'
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 IN word_list list_terminator newline_list DO compound_list DONE
801 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
802 if (word_top > 0) word_top--;
804 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
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 list_terminator newline_list DO compound_list DONE
811 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
812 if (word_top > 0) word_top--;
814 | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
816 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
817 if (word_top > 0) word_top--;
821 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
823 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
824 if (word_top > 0) word_top--;
826 | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
828 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
829 if (word_top > 0) word_top--;
831 | FOR ARITH_FOR_EXPRS DO compound_list DONE
833 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
834 if (word_top > 0) word_top--;
836 | FOR ARITH_FOR_EXPRS '{' compound_list '}'
838 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
839 if (word_top > 0) word_top--;
843 select_command: SELECT WORD newline_list DO list DONE
845 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
846 if (word_top > 0) word_top--;
848 | SELECT WORD newline_list '{' list '}'
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 DO list DONE
855 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
856 if (word_top > 0) word_top--;
858 | SELECT WORD ';' newline_list '{' list '}'
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 IN word_list list_terminator newline_list DO list DONE
865 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
866 if (word_top > 0) word_top--;
868 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
870 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
871 if (word_top > 0) word_top--;
875 case_command: CASE WORD newline_list IN newline_list ESAC
877 $$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]);
878 if (word_top > 0) word_top--;
880 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
882 $$ = make_case_command ($2, $5, word_lineno[word_top]);
883 if (word_top > 0) word_top--;
885 | CASE WORD newline_list IN case_clause ESAC
887 $$ = make_case_command ($2, $5, word_lineno[word_top]);
888 if (word_top > 0) word_top--;
892 function_def: WORD '(' ')' newline_list function_body
893 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
895 | FUNCTION WORD '(' ')' newline_list function_body
896 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
898 | FUNCTION WORD newline_list function_body
899 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
902 function_body: shell_command
904 | shell_command redirection_list
909 /* According to Posix.2 3.9.5, redirections
910 specified after the body of a function should
911 be attached to the function and performed when
912 the function is executed, not as part of the
913 function definition command. */
914 /* XXX - I don't think it matters, but we might
915 want to change this in the future to avoid
916 problems differentiating between a function
917 definition with a redirection and a function
918 definition containing a single command with a
919 redirection. The two are semantically equivalent,
920 though -- the only difference is in how the
921 command printing code displays the redirections. */
924 register REDIRECT *t;
925 for (t = tc->redirects; t->next; t = t->next)
935 subshell: '(' compound_list ')'
937 $$ = make_subshell_command ($2);
938 $$->flags |= CMD_WANT_SUBSHELL;
942 coproc: COPROC shell_command
944 $$ = make_coproc_command ("COPROC", $2);
945 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
947 | COPROC shell_command redirection_list
954 register REDIRECT *t;
955 for (t = tc->redirects; t->next; t = t->next)
961 $$ = make_coproc_command ("COPROC", $2);
962 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
964 | COPROC WORD shell_command
966 $$ = make_coproc_command ($2->word, $3);
967 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
969 | COPROC WORD shell_command redirection_list
976 register REDIRECT *t;
977 for (t = tc->redirects; t->next; t = t->next)
983 $$ = make_coproc_command ($2->word, $3);
984 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
986 | COPROC simple_command
988 $$ = make_coproc_command ("COPROC", clean_simple_command ($2));
989 $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
993 if_command: IF compound_list THEN compound_list FI
994 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
995 | IF compound_list THEN compound_list ELSE compound_list FI
996 { $$ = make_if_command ($2, $4, $6); }
997 | IF compound_list THEN compound_list elif_clause FI
998 { $$ = make_if_command ($2, $4, $5); }
1002 group_command: '{' compound_list '}'
1003 { $$ = make_group_command ($2); }
1006 arith_command: ARITH_CMD
1007 { $$ = make_arith_command ($1); }
1010 cond_command: COND_START COND_CMD COND_END
1014 elif_clause: ELIF compound_list THEN compound_list
1015 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
1016 | ELIF compound_list THEN compound_list ELSE compound_list
1017 { $$ = make_if_command ($2, $4, $6); }
1018 | ELIF compound_list THEN compound_list elif_clause
1019 { $$ = make_if_command ($2, $4, $5); }
1022 case_clause: pattern_list
1023 | case_clause_sequence pattern_list
1024 { $2->next = $1; $$ = $2; }
1027 pattern_list: newline_list pattern ')' compound_list
1028 { $$ = make_pattern_list ($2, $4); }
1029 | newline_list pattern ')' newline_list
1030 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
1031 | newline_list '(' pattern ')' compound_list
1032 { $$ = make_pattern_list ($3, $5); }
1033 | newline_list '(' pattern ')' newline_list
1034 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
1037 case_clause_sequence: pattern_list SEMI_SEMI
1039 | case_clause_sequence pattern_list SEMI_SEMI
1040 { $2->next = $1; $$ = $2; }
1041 | pattern_list SEMI_AND
1042 { $1->flags |= CASEPAT_FALLTHROUGH; $$ = $1; }
1043 | case_clause_sequence pattern_list SEMI_AND
1044 { $2->flags |= CASEPAT_FALLTHROUGH; $2->next = $1; $$ = $2; }
1045 | pattern_list SEMI_SEMI_AND
1046 { $1->flags |= CASEPAT_TESTNEXT; $$ = $1; }
1047 | case_clause_sequence pattern_list SEMI_SEMI_AND
1048 { $2->flags |= CASEPAT_TESTNEXT; $2->next = $1; $$ = $2; }
1052 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
1054 { $$ = make_word_list ($3, $1); }
1057 /* A list allows leading or trailing newlines and
1058 newlines as operators (equivalent to semicolons).
1059 It must end with a newline or semicolon.
1060 Lists are used within commands such as if, for, while. */
1062 list: newline_list list0
1066 gather_here_documents ();
1071 | newline_list list1
1077 list0: list1 '\n' newline_list
1078 | list1 '&' newline_list
1080 if ($1->type == cm_connection)
1081 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
1083 $$ = command_connect ($1, (COMMAND *)NULL, '&');
1085 | list1 ';' newline_list
1089 list1: list1 AND_AND newline_list list1
1090 { $$ = command_connect ($1, $4, AND_AND); }
1091 | list1 OR_OR newline_list list1
1092 { $$ = command_connect ($1, $4, OR_OR); }
1093 | list1 '&' newline_list list1
1095 if ($1->type == cm_connection)
1096 $$ = connect_async_list ($1, $4, '&');
1098 $$ = command_connect ($1, $4, '&');
1100 | list1 ';' newline_list list1
1101 { $$ = command_connect ($1, $4, ';'); }
1102 | list1 '\n' newline_list list1
1103 { $$ = command_connect ($1, $4, ';'); }
1108 simple_list_terminator: '\n'
1112 list_terminator:'\n'
1124 /* A simple_list is a list that contains no significant newlines
1125 and no leading or trailing newlines. Newlines are allowed
1126 only following operators, where they are not significant.
1128 This is what an inputunit consists of. */
1130 simple_list: simple_list1
1134 gather_here_documents ();
1135 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1137 global_command = $1;
1138 eof_encountered = 0;
1139 rewind_input_string ();
1145 if ($1->type == cm_connection)
1146 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
1148 $$ = command_connect ($1, (COMMAND *)NULL, '&');
1150 gather_here_documents ();
1151 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1153 global_command = $1;
1154 eof_encountered = 0;
1155 rewind_input_string ();
1163 gather_here_documents ();
1164 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
1166 global_command = $1;
1167 eof_encountered = 0;
1168 rewind_input_string ();
1174 simple_list1: simple_list1 AND_AND newline_list simple_list1
1175 { $$ = command_connect ($1, $4, AND_AND); }
1176 | simple_list1 OR_OR newline_list simple_list1
1177 { $$ = command_connect ($1, $4, OR_OR); }
1178 | simple_list1 '&' simple_list1
1180 if ($1->type == cm_connection)
1181 $$ = connect_async_list ($1, $3, '&');
1183 $$ = command_connect ($1, $3, '&');
1185 | simple_list1 ';' simple_list1
1186 { $$ = command_connect ($1, $3, ';'); }
1192 pipeline_command: pipeline
1194 | BANG pipeline_command
1197 $2->flags ^= CMD_INVERT_RETURN; /* toggle */
1200 | timespec pipeline_command
1206 | timespec list_terminator
1210 /* Boy, this is unclean. `time' by itself can
1211 time a null command. We cheat and push a
1212 newline back if the list_terminator was a newline
1213 to avoid the double-newline problem (one to
1214 terminate this, one to terminate the command) */
1217 $$ = make_simple_command (x, (COMMAND *)NULL);
1219 /* XXX - let's cheat and push a newline back */
1221 token_to_read = '\n';
1223 | BANG list_terminator
1227 /* This is just as unclean. Posix says that `!'
1228 by itself should be equivalent to `false'.
1230 newline back if the list_terminator was a newline
1231 to avoid the double-newline problem (one to
1232 terminate this, one to terminate the command) */
1235 $$ = make_simple_command (x, (COMMAND *)NULL);
1236 $$->flags |= CMD_INVERT_RETURN;
1237 /* XXX - let's cheat and push a newline back */
1239 token_to_read = '\n';
1243 pipeline: pipeline '|' newline_list pipeline
1244 { $$ = command_connect ($1, $4, '|'); }
1245 | pipeline BAR_AND newline_list pipeline
1247 /* Make cmd1 |& cmd2 equivalent to cmd1 2>&1 | cmd2 */
1252 tc = $1->type == cm_simple ? (COMMAND *)$1->value.Simple : $1;
1255 r = make_redirection (sd, r_duplicating_output, rd, 0);
1258 register REDIRECT *t;
1259 for (t = tc->redirects; t->next; t = t->next)
1266 $$ = command_connect ($1, $4, '|');
1273 { $$ = CMD_TIME_PIPELINE; }
1275 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1276 | TIME TIMEOPT TIMEIGN
1277 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1281 /* Initial size to allocate for tokens, and the
1282 amount to grow them by. */
1283 #define TOKEN_DEFAULT_INITIAL_SIZE 496
1284 #define TOKEN_DEFAULT_GROW_SIZE 512
1286 /* Should we call prompt_again? */
1287 #define SHOULD_PROMPT() \
1288 (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
1291 # define expanding_alias() (pushed_string_list && pushed_string_list->expander)
1293 # define expanding_alias() 0
1296 /* Global var is non-zero when end of file has been reached. */
1297 int EOF_Reached = 0;
1310 /* yy_getc () returns the next available character from input or EOF.
1311 yy_ungetc (c) makes `c' the next character to read.
1312 init_yy_io (get, unget, type, location) makes the function GET the
1313 installed function for getting the next character, makes UNGET the
1314 installed function for un-getting a character, sets the type of stream
1315 (either string or file) from TYPE, and makes LOCATION point to where
1316 the input is coming from. */
1318 /* Unconditionally returns end-of-file. */
1325 /* Variable containing the current get and unget functions.
1326 See ./input.h for a clearer description. */
1327 BASH_INPUT bash_input;
1329 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
1330 is non-null, avoiding a memory leak. */
1332 initialize_bash_input ()
1334 bash_input.type = st_none;
1335 FREE (bash_input.name);
1336 bash_input.name = (char *)NULL;
1337 bash_input.location.file = (FILE *)NULL;
1338 bash_input.location.string = (char *)NULL;
1339 bash_input.getter = (sh_cget_func_t *)NULL;
1340 bash_input.ungetter = (sh_cunget_func_t *)NULL;
1343 /* Set the contents of the current bash input stream from
1344 GET, UNGET, TYPE, NAME, and LOCATION. */
1346 init_yy_io (get, unget, type, name, location)
1347 sh_cget_func_t *get;
1348 sh_cunget_func_t *unget;
1349 enum stream_type type;
1351 INPUT_STREAM location;
1353 bash_input.type = type;
1354 FREE (bash_input.name);
1355 bash_input.name = name ? savestring (name) : (char *)NULL;
1359 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
1361 bash_input.location = location;
1363 bash_input.getter = get;
1364 bash_input.ungetter = unget;
1370 return (bash_input.name ? bash_input.name : "stdin");
1373 /* Call this to get the next character of input. */
1377 return (*(bash_input.getter)) ();
1380 /* Call this to unget C. That is, to make C the next character
1386 return (*(bash_input.ungetter)) (c);
1389 #if defined (BUFFERED_INPUT)
1390 #ifdef INCLUDE_UNUSED
1392 input_file_descriptor ()
1394 switch (bash_input.type)
1397 return (fileno (bash_input.location.file));
1399 return (bash_input.location.buffered_fd);
1402 return (fileno (stdin));
1406 #endif /* BUFFERED_INPUT */
1408 /* **************************************************************** */
1410 /* Let input be read from readline (). */
1412 /* **************************************************************** */
1414 #if defined (READLINE)
1415 char *current_readline_prompt = (char *)NULL;
1416 char *current_readline_line = (char *)NULL;
1417 int current_readline_line_index = 0;
1422 SigHandler *old_sigint;
1426 if (!current_readline_line)
1428 if (!bash_readline_initialized)
1429 initialize_readline ();
1431 #if defined (JOB_CONTROL)
1433 give_terminal_to (shell_pgrp, 0);
1434 #endif /* JOB_CONTROL */
1436 old_sigint = (SigHandler *)IMPOSSIBLE_TRAP_HANDLER;
1437 if (signal_is_ignored (SIGINT) == 0)
1439 /* interrupt_immediately++; */
1440 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
1443 current_readline_line = readline (current_readline_prompt ?
1444 current_readline_prompt : "");
1447 if (signal_is_ignored (SIGINT) == 0)
1449 /* interrupt_immediately--; */
1450 if (old_sigint != IMPOSSIBLE_TRAP_HANDLER)
1451 set_signal_handler (SIGINT, old_sigint);
1455 /* Reset the prompt to the decoded value of prompt_string_pointer. */
1456 reset_readline_prompt ();
1459 if (current_readline_line == 0)
1462 current_readline_line_index = 0;
1463 line_len = strlen (current_readline_line);
1465 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
1466 current_readline_line[line_len++] = '\n';
1467 current_readline_line[line_len] = '\0';
1470 if (current_readline_line[current_readline_line_index] == 0)
1472 free (current_readline_line);
1473 current_readline_line = (char *)NULL;
1474 return (yy_readline_get ());
1478 c = current_readline_line[current_readline_line_index++];
1484 yy_readline_unget (c)
1487 if (current_readline_line_index && current_readline_line)
1488 current_readline_line[--current_readline_line_index] = c;
1493 with_input_from_stdin ()
1495 INPUT_STREAM location;
1497 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1499 location.string = current_readline_line;
1500 init_yy_io (yy_readline_get, yy_readline_unget,
1501 st_stdin, "readline stdin", location);
1505 #else /* !READLINE */
1508 with_input_from_stdin ()
1510 with_input_from_stream (stdin, "stdin");
1512 #endif /* !READLINE */
1514 /* **************************************************************** */
1516 /* Let input come from STRING. STRING is zero terminated. */
1518 /* **************************************************************** */
1523 register char *string;
1524 register unsigned char c;
1526 string = bash_input.location.string;
1528 /* If the string doesn't exist, or is empty, EOF found. */
1529 if (string && *string)
1532 bash_input.location.string = string;
1543 *(--bash_input.location.string) = c;
1548 with_input_from_string (string, name)
1552 INPUT_STREAM location;
1554 location.string = string;
1555 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1558 /* Count the number of characters we've consumed from bash_input.location.string
1559 and read into shell_input_line, but have not returned from shell_getc.
1560 That is the true input location. Rewind bash_input.location.string by
1561 that number of characters, so it points to the last character actually
1562 consumed by the parser. */
1564 rewind_input_string ()
1568 /* number of unconsumed characters in the input -- XXX need to take newlines
1569 into account, e.g., $(...\n) */
1570 xchars = shell_input_line_len - shell_input_line_index;
1571 if (bash_input.location.string[-1] == '\n')
1574 /* XXX - how to reflect bash_input.location.string back to string passed to
1575 parse_and_execute or xparse_dolparen? xparse_dolparen needs to know how
1576 far into the string we parsed. parse_and_execute knows where bash_input.
1577 location.string is, and how far from orig_string that is -- that's the
1578 number of characters the command consumed. */
1580 /* bash_input.location.string - xchars should be where we parsed to */
1581 /* need to do more validation on xchars value for sanity -- test cases. */
1582 bash_input.location.string -= xchars;
1585 /* **************************************************************** */
1587 /* Let input come from STREAM. */
1589 /* **************************************************************** */
1591 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1592 define, and just use getc/ungetc if it was defined, but since bash
1593 installs its signal handlers without the SA_RESTART flag, some signals
1594 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
1595 the read to be restarted. We need to restart it ourselves. */
1603 if (bash_input.location.file)
1607 interrupt_immediately++;
1610 /* XXX - don't need terminate_immediately; getc_with_restart checks
1611 for terminating signals itself if read returns < 0 */
1612 result = getc_with_restart (bash_input.location.file);
1616 interrupt_immediately--;
1626 return (ungetc_with_restart (c, bash_input.location.file));
1630 with_input_from_stream (stream, name)
1634 INPUT_STREAM location;
1636 location.file = stream;
1637 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1640 typedef struct stream_saver {
1641 struct stream_saver *next;
1642 BASH_INPUT bash_input;
1644 #if defined (BUFFERED_INPUT)
1645 BUFFERED_STREAM *bstream;
1646 #endif /* BUFFERED_INPUT */
1649 /* The globally known line number. */
1650 int line_number = 0;
1652 /* The line number offset set by assigning to LINENO. Not currently used. */
1653 int line_number_base = 0;
1655 #if defined (COND_COMMAND)
1656 static int cond_lineno;
1657 static int cond_token;
1660 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1663 push_stream (reset_lineno)
1666 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1668 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1670 #if defined (BUFFERED_INPUT)
1671 saver->bstream = (BUFFERED_STREAM *)NULL;
1672 /* If we have a buffered stream, clear out buffers[fd]. */
1673 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1674 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1675 (BUFFERED_STREAM *)NULL);
1676 #endif /* BUFFERED_INPUT */
1678 saver->line = line_number;
1679 bash_input.name = (char *)NULL;
1680 saver->next = stream_list;
1681 stream_list = saver;
1694 STREAM_SAVER *saver = stream_list;
1697 stream_list = stream_list->next;
1699 init_yy_io (saver->bash_input.getter,
1700 saver->bash_input.ungetter,
1701 saver->bash_input.type,
1702 saver->bash_input.name,
1703 saver->bash_input.location);
1705 #if defined (BUFFERED_INPUT)
1706 /* If we have a buffered stream, restore buffers[fd]. */
1707 /* If the input file descriptor was changed while this was on the
1708 save stack, update the buffered fd to the new file descriptor and
1709 re-establish the buffer <-> bash_input fd correspondence. */
1710 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1712 if (bash_input_fd_changed)
1714 bash_input_fd_changed = 0;
1715 if (default_buffered_input >= 0)
1717 bash_input.location.buffered_fd = default_buffered_input;
1718 saver->bstream->b_fd = default_buffered_input;
1719 SET_CLOSE_ON_EXEC (default_buffered_input);
1722 /* XXX could free buffered stream returned as result here. */
1723 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1725 #endif /* BUFFERED_INPUT */
1727 line_number = saver->line;
1729 FREE (saver->bash_input.name);
1734 /* Return 1 if a stream of type TYPE is saved on the stack. */
1736 stream_on_stack (type)
1737 enum stream_type type;
1739 register STREAM_SAVER *s;
1741 for (s = stream_list; s; s = s->next)
1742 if (s->bash_input.type == type)
1747 /* Save the current token state and return it in a malloced array. */
1753 ret = (int *)xmalloc (4 * sizeof (int));
1754 ret[0] = last_read_token;
1755 ret[1] = token_before_that;
1756 ret[2] = two_tokens_ago;
1757 ret[3] = current_token;
1762 restore_token_state (ts)
1767 last_read_token = ts[0];
1768 token_before_that = ts[1];
1769 two_tokens_ago = ts[2];
1770 current_token = ts[3];
1774 * This is used to inhibit alias expansion and reserved word recognition
1775 * inside case statement pattern lists. A `case statement pattern list' is:
1777 * everything between the `in' in a `case word in' and the next ')'
1779 * everything between a `;;' and the next `)' or `esac'
1782 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1784 #define END_OF_ALIAS 0
1787 * Pseudo-global variables used in implementing token-wise alias expansion.
1791 * Pushing and popping strings. This works together with shell_getc to
1792 * implement alias expansion on a per-token basis.
1795 #define PSH_ALIAS 0x01
1796 #define PSH_DPAREN 0x02
1797 #define PSH_SOURCE 0x04
1799 typedef struct string_saver {
1800 struct string_saver *next;
1801 int expand_alias; /* Value to set expand_alias to when string is popped. */
1804 alias_t *expander; /* alias that caused this line to be pushed. */
1806 size_t saved_line_size, saved_line_index;
1807 int saved_line_terminator;
1811 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1814 * Push the current shell_input_line onto a stack of such lines and make S
1815 * the current input. Used when expanding aliases. EXPAND is used to set
1816 * the value of expand_next_token when the string is popped, so that the
1817 * word after the alias in the original line is handled correctly when the
1818 * alias expands to multiple words. TOKEN is the token that was expanded
1819 * into S; it is saved and used to prevent infinite recursive expansion.
1822 push_string (s, expand, ap)
1827 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
1829 temp->expand_alias = expand;
1830 temp->saved_line = shell_input_line;
1831 temp->saved_line_size = shell_input_line_size;
1832 temp->saved_line_index = shell_input_line_index;
1833 temp->saved_line_terminator = shell_input_line_terminator;
1836 temp->expander = ap;
1838 temp->flags = PSH_ALIAS;
1840 temp->next = pushed_string_list;
1841 pushed_string_list = temp;
1845 ap->flags |= AL_BEINGEXPANDED;
1848 shell_input_line = s;
1849 shell_input_line_size = STRLEN (s);
1850 shell_input_line_index = 0;
1851 shell_input_line_terminator = '\0';
1853 parser_state &= ~PST_ALEXPNEXT; /* XXX */
1856 set_line_mbstate ();
1860 * Make the top of the pushed_string stack be the current shell input.
1861 * Only called when there is something on the stack. Called from shell_getc
1862 * when it thinks it has consumed the string generated by an alias expansion
1863 * and needs to return to the original input line.
1870 FREE (shell_input_line);
1871 shell_input_line = pushed_string_list->saved_line;
1872 shell_input_line_index = pushed_string_list->saved_line_index;
1873 shell_input_line_size = pushed_string_list->saved_line_size;
1874 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1876 if (pushed_string_list->expand_alias)
1877 parser_state |= PST_ALEXPNEXT;
1879 parser_state &= ~PST_ALEXPNEXT;
1881 t = pushed_string_list;
1882 pushed_string_list = pushed_string_list->next;
1886 t->expander->flags &= ~AL_BEINGEXPANDED;
1891 set_line_mbstate ();
1897 register STRING_SAVER *t, *t1;
1899 for (t = pushed_string_list; t; )
1902 FREE (t->saved_line);
1905 t->expander->flags &= ~AL_BEINGEXPANDED;
1910 pushed_string_list = (STRING_SAVER *)NULL;
1913 #endif /* ALIAS || DPAREN_ARITHMETIC */
1916 free_pushed_string_input ()
1918 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1919 free_string_list ();
1924 parser_expanding_alias ()
1926 return (expanding_alias ());
1930 parser_save_alias ()
1932 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1933 push_string ((char *)NULL, 0, (alias_t *)NULL);
1934 pushed_string_list->flags = PSH_SOURCE; /* XXX - for now */
1941 parser_restore_alias ()
1943 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1944 if (pushed_string_list)
1951 /* Return a line of text, taken from wherever yylex () reads input.
1952 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1953 is non-zero, we remove unquoted \<newline> pairs. This is used by
1954 read_secondary_line to read here documents. */
1956 read_a_line (remove_quoted_newline)
1957 int remove_quoted_newline;
1959 static char *line_buffer = (char *)NULL;
1960 static int buffer_size = 0;
1961 int indx, c, peekc, pass_next;
1963 #if defined (READLINE)
1964 if (no_line_editing && SHOULD_PROMPT ())
1966 if (SHOULD_PROMPT ())
1970 pass_next = indx = 0;
1973 /* Allow immediate exit if interrupted during input. */
1978 /* Ignore null bytes in input. */
1982 internal_warning ("read_a_line: ignored null byte in input");
1987 /* If there is no more input, then we return NULL. */
1990 if (interactive && bash_input.type == st_stream)
1993 return ((char *)NULL);
1997 /* `+2' in case the final character in the buffer is a newline. */
1998 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
2000 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
2001 here document with an unquoted delimiter. In this case,
2002 the line will be expanded as if it were in double quotes.
2003 We allow a backslash to escape the next character, but we
2004 need to treat the backslash specially only if a backslash
2005 quoting a backslash-newline pair appears in the line. */
2008 line_buffer[indx++] = c;
2011 else if (c == '\\' && remove_quoted_newline)
2018 continue; /* Make the unquoted \<newline> pair disappear. */
2024 line_buffer[indx++] = c; /* Preserve the backslash. */
2028 line_buffer[indx++] = c;
2032 line_buffer[indx] = '\0';
2033 return (line_buffer);
2038 /* Return a line as in read_a_line (), but insure that the prompt is
2039 the secondary prompt. This is used to read the lines of a here
2040 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
2041 newlines quoted with backslashes while reading the line. It is
2042 non-zero unless the delimiter of the here document was quoted. */
2044 read_secondary_line (remove_quoted_newline)
2045 int remove_quoted_newline;
2050 prompt_string_pointer = &ps2_prompt;
2051 if (SHOULD_PROMPT())
2053 ret = read_a_line (remove_quoted_newline);
2054 #if defined (HISTORY)
2055 if (ret && remember_on_history && (parser_state & PST_HEREDOC))
2057 /* To make adding the the here-document body right, we need to rely
2058 on history_delimiting_chars() returning \n for the first line of
2059 the here-document body and the null string for the second and
2060 subsequent lines, so we avoid double newlines.
2061 current_command_line_count == 2 for the first line of the body. */
2063 current_command_line_count++;
2064 maybe_add_history (ret);
2066 #endif /* HISTORY */
2070 /* **************************************************************** */
2074 /* **************************************************************** */
2076 /* Reserved words. These are only recognized as the first word of a
2078 STRING_INT_ALIST word_token_alist[] = {
2087 #if defined (SELECT_COMMAND)
2088 { "select", SELECT },
2095 { "function", FUNCTION },
2096 #if defined (COMMAND_TIMING)
2102 #if defined (COND_COMMAND)
2103 { "[[", COND_START },
2106 #if defined (COPROCESS_SUPPORT)
2107 { "coproc", COPROC },
2112 /* other tokens that can be returned by read_token() */
2113 STRING_INT_ALIST other_token_alist[] = {
2114 /* Multiple-character tokens with special values */
2119 { ">>", GREATER_GREATER },
2120 { "<<", LESS_LESS },
2122 { ">&", GREATER_AND },
2123 { ";;", SEMI_SEMI },
2125 { ";;&", SEMI_SEMI_AND },
2126 { "<<-", LESS_LESS_MINUS },
2127 { "<<<", LESS_LESS_LESS },
2128 { "&>", AND_GREATER },
2129 { "&>>", AND_GREATER_GREATER },
2130 { "<>", LESS_GREATER },
2131 { ">|", GREATER_BAR },
2133 { "EOF", yacc_EOF },
2134 /* Tokens whose value is the character itself */
2145 { "newline", '\n' },
2149 /* others not listed here:
2150 WORD look at yylval.word
2151 ASSIGNMENT_WORD look at yylval.word
2152 NUMBER look at yylval.number
2153 ARITH_CMD look at yylval.word_list
2154 ARITH_FOR_EXPRS look at yylval.word_list
2155 COND_CMD look at yylval.command
2158 /* These are used by read_token_word, but appear up here so that shell_getc
2159 can use them to decide when to add otherwise blank lines to the history. */
2161 /* The primary delimiter stack. */
2162 struct dstack dstack = { (char *)NULL, 0, 0 };
2164 /* A temporary delimiter stack to be used when decoding prompt strings.
2165 This is needed because command substitutions in prompt strings (e.g., PS2)
2166 can screw up the parser's quoting state. */
2167 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
2169 /* Macro for accessing the top delimiter on the stack. Returns the
2170 delimiter or zero if none. */
2171 #define current_delimiter(ds) \
2172 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
2174 #define push_delimiter(ds, character) \
2177 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
2178 ds.delimiters = (char *)xrealloc \
2179 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
2180 ds.delimiters[ds.delimiter_depth] = character; \
2181 ds.delimiter_depth++; \
2185 #define pop_delimiter(ds) ds.delimiter_depth--
2187 /* Return the next shell input character. This always reads characters
2188 from shell_input_line; when that line is exhausted, it is time to
2189 read the next line. This is called by read_token when the shell is
2190 processing normal command input. */
2192 /* This implements one-character lookahead/lookbehind across physical input
2193 lines, to avoid something being lost because it's pushed back with
2194 shell_ungetc when we're at the start of a line. */
2195 static int eol_ungetc_lookahead = 0;
2198 shell_getc (remove_quoted_newline)
2199 int remove_quoted_newline;
2207 if (sigwinch_received)
2209 sigwinch_received = 0;
2210 get_new_window_size (0, (int *)0, (int *)0);
2213 if (eol_ungetc_lookahead)
2215 c = eol_ungetc_lookahead;
2216 eol_ungetc_lookahead = 0;
2220 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2221 /* If shell_input_line[shell_input_line_index] == 0, but there is
2222 something on the pushed list of strings, then we don't want to go
2223 off and get another line. We let the code down below handle it. */
2225 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
2226 (pushed_string_list == (STRING_SAVER *)NULL)))
2227 #else /* !ALIAS && !DPAREN_ARITHMETIC */
2228 if (!shell_input_line || !shell_input_line[shell_input_line_index])
2229 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
2233 /* Let's not let one really really long line blow up memory allocation */
2234 if (shell_input_line && shell_input_line_size >= 32768)
2236 free (shell_input_line);
2237 shell_input_line = 0;
2238 shell_input_line_size = 0;
2243 /* Allow immediate exit if interrupted during input. */
2247 shell_input_line_terminator = 0;
2249 /* If the shell is interatctive, but not currently printing a prompt
2250 (interactive_shell && interactive == 0), we don't want to print
2251 notifies or cleanup the jobs -- we want to defer it until we do
2252 print the next prompt. */
2253 if (interactive_shell == 0 || SHOULD_PROMPT())
2255 #if defined (JOB_CONTROL)
2256 /* This can cause a problem when reading a command as the result
2257 of a trap, when the trap is called from flush_child. This call
2258 had better not cause jobs to disappear from the job table in
2259 that case, or we will have big trouble. */
2260 notify_and_cleanup ();
2261 #else /* !JOB_CONTROL */
2262 cleanup_dead_jobs ();
2263 #endif /* !JOB_CONTROL */
2266 #if defined (READLINE)
2267 if (no_line_editing && SHOULD_PROMPT())
2269 if (SHOULD_PROMPT())
2273 if (bash_input.type == st_stream)
2280 /* Allow immediate exit if interrupted during input. */
2286 internal_warning ("shell_getc: ignored null byte in input");
2291 /* Theoretical overflow */
2292 /* If we can't put 256 bytes more into the buffer, allocate
2293 everything we can and fill it as full as we can. */
2294 /* XXX - we ignore rest of line using `truncating' flag */
2295 if (shell_input_line_size > (SIZE_MAX - 256))
2299 n = SIZE_MAX - i; /* how much more can we put into the buffer? */
2300 if (n <= 2) /* we have to save 1 for the newline added below */
2302 if (truncating == 0)
2303 internal_warning("shell_getc: shell_input_line_size (%zu) exceeds SIZE_MAX (%llu): line truncated", shell_input_line_size, SIZE_MAX);
2304 shell_input_line[i] = '\0';
2307 if (shell_input_line_size < SIZE_MAX)
2309 shell_input_line_size = SIZE_MAX;
2310 shell_input_line = xrealloc (shell_input_line, shell_input_line_size);
2314 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
2318 if (bash_input.type == st_stream)
2322 shell_input_line_terminator = EOF;
2324 shell_input_line[i] = '\0';
2328 if (truncating == 0 || c == '\n')
2329 shell_input_line[i++] = c;
2333 shell_input_line[--i] = '\0';
2334 current_command_line_count++;
2339 shell_input_line_index = 0;
2340 shell_input_line_len = i; /* == strlen (shell_input_line) */
2342 set_line_mbstate ();
2344 #if defined (HISTORY)
2345 if (remember_on_history && shell_input_line && shell_input_line[0])
2348 # if defined (BANG_HISTORY)
2351 /* If the current delimiter is a single quote, we should not be
2352 performing history expansion, even if we're on a different
2353 line from the original single quote. */
2354 old_hist = history_expansion_inhibited;
2355 if (current_delimiter (dstack) == '\'')
2356 history_expansion_inhibited = 1;
2358 expansions = pre_process_line (shell_input_line, 1, 1);
2359 # if defined (BANG_HISTORY)
2360 history_expansion_inhibited = old_hist;
2362 if (expansions != shell_input_line)
2364 free (shell_input_line);
2365 shell_input_line = expansions;
2366 shell_input_line_len = shell_input_line ?
2367 strlen (shell_input_line) : 0;
2368 if (shell_input_line_len == 0)
2369 current_command_line_count--;
2371 /* We have to force the xrealloc below because we don't know
2372 the true allocated size of shell_input_line anymore. */
2373 shell_input_line_size = shell_input_line_len;
2375 set_line_mbstate ();
2378 /* Try to do something intelligent with blank lines encountered while
2379 entering multi-line commands. XXX - this is grotesque */
2380 else if (remember_on_history && shell_input_line &&
2381 shell_input_line[0] == '\0' &&
2382 current_command_line_count > 1)
2384 if (current_delimiter (dstack))
2385 /* We know shell_input_line[0] == 0 and we're reading some sort of
2386 quoted string. This means we've got a line consisting of only
2387 a newline in a quoted string. We want to make sure this line
2388 gets added to the history. */
2389 maybe_add_history (shell_input_line);
2393 hdcs = history_delimiting_chars (shell_input_line);
2394 if (hdcs && hdcs[0] == ';')
2395 maybe_add_history (shell_input_line);
2399 #endif /* HISTORY */
2401 if (shell_input_line)
2403 /* Lines that signify the end of the shell's input should not be
2404 echoed. We should not echo lines while parsing command
2405 substitutions with recursive calls into the parsing engine; those
2406 should only be echoed once when we read the word. That is the
2407 reason for the test against shell_eof_token, which is set to a
2408 right paren when parsing the contents of command substitutions. */
2409 if (echo_input_at_read && (shell_input_line[0] ||
2410 shell_input_line_terminator != EOF) &&
2411 shell_eof_token == 0)
2412 fprintf (stderr, "%s\n", shell_input_line);
2416 shell_input_line_size = 0;
2417 prompt_string_pointer = ¤t_prompt_string;
2418 if (SHOULD_PROMPT ())
2423 /* Add the newline to the end of this string, iff the string does
2424 not already end in an EOF character. */
2425 if (shell_input_line_terminator != EOF)
2427 if (shell_input_line_size < SIZE_MAX-3 && (shell_input_line_len+3 > shell_input_line_size))
2428 shell_input_line = (char *)xrealloc (shell_input_line,
2429 1 + (shell_input_line_size += 2));
2431 shell_input_line[shell_input_line_len] = '\n';
2432 shell_input_line[shell_input_line_len + 1] = '\0';
2434 set_line_mbstate ();
2439 uc = shell_input_line[shell_input_line_index];
2442 shell_input_line_index++;
2444 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2445 /* If UC is NULL, we have reached the end of the current input string. If
2446 pushed_string_list is non-empty, it's time to pop to the previous string
2447 because we have fully consumed the result of the last alias expansion.
2448 Do it transparently; just return the next character of the string popped
2450 /* If pushed_string_list != 0 but pushed_string_list->expander == 0 (not
2451 currently tested) and the flags value is not PSH_SOURCE, we are not
2452 parsing an alias, we have just saved one (push_string, when called by
2453 the parse_dparen code) In this case, just go on as well. The PSH_SOURCE
2454 case is handled below. */
2456 if (uc == 0 && pushed_string_list && pushed_string_list->flags != PSH_SOURCE)
2459 uc = shell_input_line[shell_input_line_index];
2461 shell_input_line_index++;
2463 #endif /* ALIAS || DPAREN_ARITHMETIC */
2465 if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n')
2467 if (SHOULD_PROMPT ())
2470 /* What do we do here if we're expanding an alias whose definition
2471 includes an escaped newline? If that's the last character in the
2472 alias expansion, we just pop the pushed string list (recall that
2473 we inhibit the appending of a space in mk_alexpansion() if newline
2474 is the last character). If it's not the last character, we need
2475 to consume the quoted newline and move to the next character in
2478 if (expanding_alias () && shell_input_line[shell_input_line_index+1] == '\0')
2483 else if (expanding_alias () && shell_input_line[shell_input_line_index+1] != '\0')
2485 shell_input_line_index++; /* skip newline */
2486 goto next_alias_char; /* and get next character */
2493 if (uc == 0 && shell_input_line_terminator == EOF)
2494 return ((shell_input_line_index != 0) ? '\n' : EOF);
2496 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2497 /* We already know that we are not parsing an alias expansion because of the
2498 check for expanding_alias() above. This knows how parse_and_execute
2499 handles switching to st_string input while an alias is being expanded,
2500 hence the check for pushed_string_list without pushed_string_list->expander
2501 and the check for PSH_SOURCE as pushed_string_list->flags.
2502 parse_and_execute and parse_string both change the input type to st_string
2503 and place the string to be parsed and executed into location.string, so
2504 we should not stop reading that until the pointer is '\0'.
2505 The check for shell_input_line_terminator may be superfluous.
2507 This solves the problem of `.' inside a multi-line alias with embedded
2508 newlines executing things out of order. */
2509 if (uc == 0 && bash_input.type == st_string && *bash_input.location.string &&
2510 pushed_string_list && pushed_string_list->flags == PSH_SOURCE &&
2511 shell_input_line_terminator == 0)
2513 shell_input_line_index = 0;
2521 /* Put C back into the input for the shell. This might need changes for
2522 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
2523 character different than we read, shell_input_line_property doesn't need
2524 to change when manipulating shell_input_line. The define for
2525 last_shell_getc_is_singlebyte should take care of it, though. */
2530 if (shell_input_line && shell_input_line_index)
2531 shell_input_line[--shell_input_line_index] = c;
2533 eol_ungetc_lookahead = c;
2536 #ifdef INCLUDE_UNUSED
2537 /* Back the input pointer up by one, effectively `ungetting' a character. */
2541 if (shell_input_line && shell_input_line_index)
2542 shell_input_line_index--;
2546 /* Discard input until CHARACTER is seen, then push that character back
2547 onto the input stream. */
2549 discard_until (character)
2554 while ((c = shell_getc (0)) != EOF && c != character)
2562 execute_variable_command (command, vname)
2563 char *command, *vname;
2566 sh_parser_state_t ps;
2568 save_parser_state (&ps);
2569 last_lastarg = get_string_value ("_");
2571 last_lastarg = savestring (last_lastarg);
2573 parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST);
2575 restore_parser_state (&ps);
2576 bind_variable ("_", last_lastarg, 0);
2577 FREE (last_lastarg);
2579 if (token_to_read == '\n') /* reset_parser was called */
2583 /* Place to remember the token. We try to keep the buffer
2584 at a reasonable size, but it can grow. */
2585 static char *token = (char *)NULL;
2587 /* Current size of the token buffer. */
2588 static int token_buffer_size;
2590 /* Command to read_token () explaining what we want it to do. */
2593 #define prompt_is_ps1 \
2594 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
2596 /* Function for yyparse to call. yylex keeps track of
2597 the last two tokens read, and calls read_token. */
2601 if (interactive && (current_token == 0 || current_token == '\n'))
2603 /* Before we print a prompt, we might have to check mailboxes.
2604 We do this only if it is time to do so. Notice that only here
2605 is the mail alarm reset; nothing takes place in check_mail ()
2606 except the checking of mail. Please don't change this. */
2607 if (prompt_is_ps1 && parse_and_execute_level == 0 && time_to_check_mail ())
2610 reset_mail_timer ();
2613 /* Avoid printing a prompt if we're not going to read anything, e.g.
2614 after resetting the parser with read_token (RESET). */
2615 if (token_to_read == 0 && SHOULD_PROMPT ())
2619 two_tokens_ago = token_before_that;
2620 token_before_that = last_read_token;
2621 last_read_token = current_token;
2622 current_token = read_token (READ);
2624 if ((parser_state & PST_EOFTOKEN) && current_token == shell_eof_token)
2626 current_token = yacc_EOF;
2627 if (bash_input.type == st_string)
2628 rewind_input_string ();
2630 parser_state &= ~PST_EOFTOKEN;
2632 return (current_token);
2635 /* When non-zero, we have read the required tokens
2636 which allow ESAC to be the next one read. */
2637 static int esacs_needed_count;
2640 gather_here_documents ()
2645 while (need_here_doc > 0)
2647 parser_state |= PST_HEREDOC;
2648 make_here_document (redir_stack[r++], line_number);
2649 parser_state &= ~PST_HEREDOC;
2654 /* When non-zero, an open-brace used to create a group is awaiting a close
2656 static int open_brace_count;
2658 #define command_token_position(token) \
2659 (((token) == ASSIGNMENT_WORD) || (parser_state&PST_REDIRLIST) || \
2660 ((token) != SEMI_SEMI && (token) != SEMI_AND && (token) != SEMI_SEMI_AND && reserved_word_acceptable(token)))
2662 #define assignment_acceptable(token) \
2663 (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0))
2665 /* Check to see if TOKEN is a reserved word and return the token
2667 #define CHECK_FOR_RESERVED_WORD(tok) \
2669 if (!dollar_present && !quoted && \
2670 reserved_word_acceptable (last_read_token)) \
2673 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
2674 if (STREQ (tok, word_token_alist[i].word)) \
2676 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
2678 if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \
2680 if (word_token_alist[i].token == ESAC) \
2681 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
2682 else if (word_token_alist[i].token == CASE) \
2683 parser_state |= PST_CASESTMT; \
2684 else if (word_token_alist[i].token == COND_END) \
2685 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
2686 else if (word_token_alist[i].token == COND_START) \
2687 parser_state |= PST_CONDCMD; \
2688 else if (word_token_alist[i].token == '{') \
2689 open_brace_count++; \
2690 else if (word_token_alist[i].token == '}' && open_brace_count) \
2691 open_brace_count--; \
2692 return (word_token_alist[i].token); \
2699 /* OK, we have a token. Let's try to alias expand it, if (and only if)
2702 It is eligible for expansion if EXPAND_ALIASES is set, and
2703 the token is unquoted and the last token read was a command
2704 separator (or expand_next_token is set), and we are currently
2705 processing an alias (pushed_string_list is non-empty) and this
2706 token is not the same as the current or any previously
2709 Special cases that disqualify:
2710 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
2720 r = xmalloc (l + 2);
2722 /* If the last character in the alias is a newline, don't add a trailing
2723 space to the expansion. Works with shell_getc above. */
2724 if (r[l - 1] != ' ' && r[l - 1] != '\n')
2731 alias_expand_token (tokstr)
2737 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
2738 (parser_state & PST_CASEPAT) == 0)
2740 ap = find_alias (tokstr);
2742 /* Currently expanding this token. */
2743 if (ap && (ap->flags & AL_BEINGEXPANDED))
2744 return (NO_EXPANSION);
2746 /* mk_alexpansion puts an extra space on the end of the alias expansion,
2747 so the lookahead by the parser works right. If this gets changed,
2748 make sure the code in shell_getc that deals with reaching the end of
2749 an expanded alias is changed with it. */
2750 expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL;
2754 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
2755 return (RE_READ_TOKEN);
2758 /* This is an eligible token that does not have an expansion. */
2759 return (NO_EXPANSION);
2761 return (NO_EXPANSION);
2766 time_command_acceptable ()
2768 #if defined (COMMAND_TIMING)
2771 if (posixly_correct && shell_compatibility_level > 41)
2773 /* Quick check of the rest of the line to find the next token. If it
2774 begins with a `-', Posix says to not return `time' as the token.
2775 This was interp 267. */
2776 i = shell_input_line_index;
2777 while (i < shell_input_line_len && (shell_input_line[i] == ' ' || shell_input_line[i] == '\t'))
2779 if (shell_input_line[i] == '-')
2783 switch (last_read_token)
2796 case BANG: /* ! time pipeline */
2797 case TIME: /* time time pipeline */
2798 case TIMEOPT: /* time -p time pipeline */
2799 case TIMEIGN: /* time -p -- ... */
2806 #endif /* COMMAND_TIMING */
2809 /* Handle special cases of token recognition:
2810 IN is recognized if the last token was WORD and the token
2811 before that was FOR or CASE or SELECT.
2813 DO is recognized if the last token was WORD and the token
2814 before that was FOR or SELECT.
2816 ESAC is recognized if the last token caused `esacs_needed_count'
2819 `{' is recognized if the last token as WORD and the token
2820 before that was FUNCTION, or if we just parsed an arithmetic
2823 `}' is recognized if there is an unclosed `{' present.
2825 `-p' is returned as TIMEOPT if the last read token was TIME.
2826 `--' is returned as TIMEIGN if the last read token was TIMEOPT.
2828 ']]' is returned as COND_END if the parser is currently parsing
2829 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
2831 `time' is returned as TIME if and only if it is immediately
2832 preceded by one of `;', `\n', `||', `&&', or `&'.
2836 special_case_tokens (tokstr)
2839 if ((last_read_token == WORD) &&
2840 #if defined (SELECT_COMMAND)
2841 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
2843 ((token_before_that == FOR) || (token_before_that == CASE)) &&
2845 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
2847 if (token_before_that == CASE)
2849 parser_state |= PST_CASEPAT;
2850 esacs_needed_count++;
2855 if (last_read_token == WORD &&
2856 #if defined (SELECT_COMMAND)
2857 (token_before_that == FOR || token_before_that == SELECT) &&
2859 (token_before_that == FOR) &&
2861 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
2864 /* Ditto for ESAC in the CASE case.
2865 Specifically, this handles "case word in esac", which is a legal
2866 construct, certainly because someone will pass an empty arg to the
2867 case construct, and we don't want it to barf. Of course, we should
2868 insist that the case construct has at least one pattern in it, but
2869 the designers disagree. */
2870 if (esacs_needed_count)
2872 esacs_needed_count--;
2873 if (STREQ (tokstr, "esac"))
2875 parser_state &= ~PST_CASEPAT;
2880 /* The start of a shell function definition. */
2881 if (parser_state & PST_ALLOWOPNBRC)
2883 parser_state &= ~PST_ALLOWOPNBRC;
2884 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2887 function_bstart = line_number;
2888 return ('{'); /* } */
2892 /* We allow a `do' after a for ((...)) without an intervening
2894 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
2896 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2899 return ('{'); /* } */
2902 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
2904 open_brace_count--; /* { */
2908 #if defined (COMMAND_TIMING)
2909 /* Handle -p after `time'. */
2910 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
2912 /* Handle -- after `time -p'. */
2913 if (last_read_token == TIMEOPT && tokstr[0] == '-' && tokstr[1] == '-' && !tokstr[2])
2917 #if defined (COND_COMMAND) /* [[ */
2918 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
2925 /* Called from shell.c when Control-C is typed at top level. Or
2926 by the error rule at top level. */
2930 dstack.delimiter_depth = 0; /* No delimiters found so far. */
2931 open_brace_count = 0;
2933 #if defined (EXTENDED_GLOB)
2934 /* Reset to global value of extended glob */
2935 if (parser_state & PST_EXTPAT)
2936 extended_glob = global_extglob;
2941 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2942 if (pushed_string_list)
2943 free_string_list ();
2944 #endif /* ALIAS || DPAREN_ARITHMETIC */
2946 if (shell_input_line)
2948 free (shell_input_line);
2949 shell_input_line = (char *)NULL;
2950 shell_input_line_size = shell_input_line_index = 0;
2953 FREE (word_desc_to_read);
2954 word_desc_to_read = (WORD_DESC *)NULL;
2956 eol_ungetc_lookahead = 0;
2958 current_token = '\n'; /* XXX */
2959 last_read_token = '\n';
2960 token_to_read = '\n';
2963 /* Read the next token. Command can be READ (normal operation) or
2964 RESET (to normalize state). */
2966 read_token (command)
2969 int character; /* Current character. */
2970 int peek_char; /* Temporary look-ahead character. */
2971 int result; /* The thing to return. */
2973 if (command == RESET)
2981 result = token_to_read;
2982 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
2984 yylval.word = word_desc_to_read;
2985 word_desc_to_read = (WORD_DESC *)NULL;
2991 #if defined (COND_COMMAND)
2992 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
2994 cond_lineno = line_number;
2995 parser_state |= PST_CONDEXPR;
2996 yylval.command = parse_cond_command ();
2997 if (cond_token != COND_END)
3002 token_to_read = COND_END;
3003 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
3009 /* This is a place to jump back to once we have successfully expanded a
3010 token with an alias and pushed the string with push_string () */
3014 /* Read a single word from input. Start by skipping blanks. */
3015 while ((character = shell_getc (1)) != EOF && shellblank (character))
3018 if (character == EOF)
3024 if MBTEST(character == '#' && (!interactive || interactive_comments))
3026 /* A comment. Discard until EOL or EOF, and then return a newline. */
3027 discard_until ('\n');
3029 character = '\n'; /* this will take the next if statement and return. */
3032 if (character == '\n')
3034 /* If we're about to return an unquoted newline, we can go and collect
3035 the text of any pending here document. */
3037 gather_here_documents ();
3040 parser_state &= ~PST_ALEXPNEXT;
3043 parser_state &= ~PST_ASSIGNOK;
3048 if (parser_state & PST_REGEXP)
3051 /* Shell meta-characters. */
3052 if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
3055 /* Turn off alias tokenization iff this character sequence would
3056 not leave us ready to read a command. */
3057 if (character == '<' || character == '>')
3058 parser_state &= ~PST_ALEXPNEXT;
3061 parser_state &= ~PST_ASSIGNOK;
3063 peek_char = shell_getc (1);
3064 if (character == peek_char)
3069 /* If '<' then we could be at "<<" or at "<<-". We have to
3070 look ahead one more character. */
3071 peek_char = shell_getc (1);
3072 if MBTEST(peek_char == '-')
3073 return (LESS_LESS_MINUS);
3074 else if MBTEST(peek_char == '<')
3075 return (LESS_LESS_LESS);
3078 shell_ungetc (peek_char);
3083 return (GREATER_GREATER);
3086 parser_state |= PST_CASEPAT;
3088 parser_state &= ~PST_ALEXPNEXT;
3091 peek_char = shell_getc (1);
3092 if MBTEST(peek_char == '&')
3093 return (SEMI_SEMI_AND);
3096 shell_ungetc (peek_char);
3106 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
3108 result = parse_dparen (character);
3116 else if MBTEST(character == '<' && peek_char == '&')
3118 else if MBTEST(character == '>' && peek_char == '&')
3119 return (GREATER_AND);
3120 else if MBTEST(character == '<' && peek_char == '>')
3121 return (LESS_GREATER);
3122 else if MBTEST(character == '>' && peek_char == '|')
3123 return (GREATER_BAR);
3124 else if MBTEST(character == '&' && peek_char == '>')
3126 peek_char = shell_getc (1);
3127 if MBTEST(peek_char == '>')
3128 return (AND_GREATER_GREATER);
3131 shell_ungetc (peek_char);
3132 return (AND_GREATER);
3135 else if MBTEST(character == '|' && peek_char == '&')
3137 else if MBTEST(character == ';' && peek_char == '&')
3139 parser_state |= PST_CASEPAT;
3141 parser_state &= ~PST_ALEXPNEXT;
3146 shell_ungetc (peek_char);
3148 /* If we look like we are reading the start of a function
3149 definition, then let the reader know about it so that
3150 we will do the right thing with `{'. */
3151 if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
3153 parser_state |= PST_ALLOWOPNBRC;
3155 parser_state &= ~PST_ALEXPNEXT;
3157 function_dstart = line_number;
3160 /* case pattern lists may be preceded by an optional left paren. If
3161 we're not trying to parse a case pattern list, the left paren
3162 indicates a subshell. */
3163 if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
3164 parser_state |= PST_SUBSHELL;
3166 else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
3167 parser_state &= ~PST_CASEPAT;
3169 else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
3170 parser_state &= ~PST_SUBSHELL;
3172 #if defined (PROCESS_SUBSTITUTION)
3173 /* Check for the constructs which introduce process substitution.
3174 Shells running in `posix mode' don't do process substitution. */
3175 if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/
3176 #endif /* PROCESS_SUBSTITUTION */
3180 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
3181 if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
3185 /* Okay, if we got this far, we have to read a word. Read one,
3186 and then check it against the known ones. */
3187 result = read_token_word (character);
3189 if (result == RE_READ_TOKEN)
3196 * Match a $(...) or other grouping construct. This has to handle embedded
3197 * quoted strings ('', ``, "") and nested constructs. It also must handle
3198 * reprompting the user, if necessary, after reading a newline, and returning
3199 * correct error values if it reads EOF.
3201 #define P_FIRSTCLOSE 0x0001
3202 #define P_ALLOWESC 0x0002
3203 #define P_DQUOTE 0x0004
3204 #define P_COMMAND 0x0008 /* parsing a command, so look for comments */
3205 #define P_BACKQUOTE 0x0010 /* parsing a backquoted command substitution */
3206 #define P_ARRAYSUB 0x0020 /* parsing a [...] array subscript for assignment */
3207 #define P_DOLBRACE 0x0040 /* parsing a ${...} construct */
3209 /* Lexical state while parsing a grouping construct or $(...). */
3210 #define LEX_WASDOL 0x001
3211 #define LEX_CKCOMMENT 0x002
3212 #define LEX_INCOMMENT 0x004
3213 #define LEX_PASSNEXT 0x008
3214 #define LEX_RESWDOK 0x010
3215 #define LEX_CKCASE 0x020
3216 #define LEX_INCASE 0x040
3217 #define LEX_INHEREDOC 0x080
3218 #define LEX_HEREDELIM 0x100 /* reading here-doc delimiter */
3219 #define LEX_STRIPDOC 0x200 /* <<- strip tabs from here doc delim */
3220 #define LEX_INWORD 0x400
3222 #define COMSUB_META(ch) ((ch) == ';' || (ch) == '&' || (ch) == '|')
3224 #define CHECK_NESTRET_ERROR() \
3226 if (nestret == &matched_pair_error) \
3229 return &matched_pair_error; \
3233 #define APPEND_NESTRET() \
3237 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); \
3238 strcpy (ret + retind, nestret); \
3239 retind += nestlen; \
3243 static char matched_pair_error;
3246 parse_matched_pair (qc, open, close, lenp, flags)
3247 int qc; /* `"' if this construct is within double quotes */
3251 int count, ch, tflags;
3252 int nestlen, ttranslen, start_lineno;
3253 char *ret, *nestret, *ttrans;
3254 int retind, retsize, rflags;
3257 dolbrace_state = (flags & P_DOLBRACE) ? DOLBRACE_PARAM : 0;
3259 /*itrace("parse_matched_pair[%d]: open = %c close = %c flags = %d", line_number, open, close, flags);*/
3263 if ((flags & P_COMMAND) && qc != '`' && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
3264 tflags |= LEX_CKCOMMENT;
3266 /* RFLAGS is the set of flags we want to pass to recursive calls. */
3267 rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE);
3269 ret = (char *)xmalloc (retsize = 64);
3272 start_lineno = line_number;
3275 ch = shell_getc (qc != '\'' && (tflags & (LEX_PASSNEXT)) == 0);
3280 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
3281 EOF_Reached = 1; /* XXX */
3282 return (&matched_pair_error);
3285 /* Possible reprompting. */
3286 if (ch == '\n' && SHOULD_PROMPT ())
3289 /* Don't bother counting parens or doing anything else if in a comment
3290 or part of a case statement */
3291 if (tflags & LEX_INCOMMENT)
3293 /* Add this character. */
3294 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3298 tflags &= ~LEX_INCOMMENT;
3303 /* Not exactly right yet, should handle shell metacharacters, too. If
3304 any changes are made to this test, make analogous changes to subst.c:
3305 extract_delimited_string(). */
3306 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (retind == 0 || ret[retind-1] == '\n' || shellblank (ret[retind - 1])))
3307 tflags |= LEX_INCOMMENT;
3309 if (tflags & LEX_PASSNEXT) /* last char was backslash */
3311 tflags &= ~LEX_PASSNEXT;
3312 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
3315 retind--; /* swallow previously-added backslash */
3319 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3320 if MBTEST(ch == CTLESC)
3321 ret[retind++] = CTLESC;
3325 /* If we're reparsing the input (e.g., from parse_string_to_word_list),
3326 we've already prepended CTLESC to single-quoted results of $'...'.
3327 We may want to do this for other CTLESC-quoted characters in
3329 else if MBTEST((parser_state & PST_REPARSE) && open == '\'' && (ch == CTLESC || ch == CTLNUL))
3331 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3335 else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
3337 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3338 ret[retind++] = CTLESC;
3342 else if MBTEST(ch == close) /* ending delimiter */
3344 /* handle nested ${...} specially. */
3345 else if MBTEST(open != close && (tflags & LEX_WASDOL) && open == '{' && ch == open) /* } */
3347 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
3350 /* Add this character. */
3351 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3354 /* If we just read the ending character, don't bother continuing. */
3358 if (open == '\'') /* '' inside grouping construct */
3360 if MBTEST((flags & P_ALLOWESC) && ch == '\\')
3361 tflags |= LEX_PASSNEXT;
3365 if MBTEST(ch == '\\') /* backslashes */
3366 tflags |= LEX_PASSNEXT;
3368 /* Based on which dolstate is currently in (param, op, or word),
3369 decide what the op is. We're really only concerned if it's % or
3370 #, so we can turn on a flag that says whether or not we should
3371 treat single quotes as special when inside a double-quoted
3372 ${...}. This logic must agree with subst.c:extract_dollar_brace_string
3373 since they share the same defines. */
3374 /* FLAG POSIX INTERP 221 */
3375 if (flags & P_DOLBRACE)
3377 /* ${param%[%]word} */
3378 if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '%' && retind > 1)
3379 dolbrace_state = DOLBRACE_QUOTE;
3380 /* ${param#[#]word} */
3381 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '#' && retind > 1)
3382 dolbrace_state = DOLBRACE_QUOTE;
3383 /* ${param/[/]pat/rep} */
3384 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '/' && retind > 1)
3385 dolbrace_state = DOLBRACE_QUOTE2; /* XXX */
3386 /* ${param^[^]pat} */
3387 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '^' && retind > 1)
3388 dolbrace_state = DOLBRACE_QUOTE;
3389 /* ${param,[,]pat} */
3390 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == ',' && retind > 1)
3391 dolbrace_state = DOLBRACE_QUOTE;
3392 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", ch) != 0)
3393 dolbrace_state = DOLBRACE_OP;
3394 else if MBTEST(dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", ch) == 0)
3395 dolbrace_state = DOLBRACE_WORD;
3398 /* The big hammer. Single quotes aren't special in double quotes. The
3399 problem is that Posix used to say the single quotes are semi-special:
3400 within a double-quoted ${...} construct "an even number of
3401 unescaped double-quotes or single-quotes, if any, shall occur." */
3402 /* This was changed in Austin Group Interp 221 */
3403 if MBTEST(posixly_correct && shell_compatibility_level > 41 && dolbrace_state != DOLBRACE_QUOTE && dolbrace_state != DOLBRACE_QUOTE2 && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'')
3406 /* Could also check open == '`' if we want to parse grouping constructs
3407 inside old-style command substitution. */
3408 if (open != close) /* a grouping construct */
3410 if MBTEST(shellquote (ch))
3412 /* '', ``, or "" inside $(...) or other grouping construct. */
3413 push_delimiter (dstack, ch);
3414 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
3415 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
3417 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
3418 pop_delimiter (dstack);
3419 CHECK_NESTRET_ERROR ();
3421 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
3423 /* Translate $'...' here. */
3424 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
3427 /* If we're parsing a double-quoted brace expansion and we are
3428 not in a place where single quotes are treated specially,
3429 make sure we single-quote the results of the ansi
3430 expansion because quote removal should remove them later */
3431 /* FLAG POSIX INTERP 221 */
3432 if ((shell_compatibility_level > 42) && (rflags & P_DQUOTE) && (dolbrace_state == DOLBRACE_QUOTE2) && (flags & P_DOLBRACE))
3434 nestret = sh_single_quote (ttrans);
3436 nestlen = strlen (nestret);
3438 else if ((rflags & P_DQUOTE) == 0)
3440 nestret = sh_single_quote (ttrans);
3442 nestlen = strlen (nestret);
3447 nestlen = ttranslen;
3449 retind -= 2; /* back up before the $' */
3451 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
3453 /* Locale expand $"..." here. */
3454 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
3457 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
3459 nestlen = ttranslen + 2;
3460 retind -= 2; /* back up before the $" */
3466 else if ((flags & P_ARRAYSUB) && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3467 goto parse_dollar_word;
3469 /* Parse an old-style command substitution within double quotes as a
3471 /* XXX - sh and ksh93 don't do this - XXX */
3472 else if MBTEST(open == '"' && ch == '`')
3474 nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags);
3476 CHECK_NESTRET_ERROR ();
3481 else if MBTEST(open != '`' && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3482 /* check for $(), $[], or ${} inside quoted string. */
3485 if (open == ch) /* undo previous increment */
3487 if (ch == '(') /* ) */
3488 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
3489 else if (ch == '{') /* } */
3490 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags);
3491 else if (ch == '[') /* ] */
3492 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
3494 CHECK_NESTRET_ERROR ();
3499 if MBTEST(ch == '$')
3500 tflags |= LEX_WASDOL;
3502 tflags &= ~LEX_WASDOL;
3508 /*itrace("parse_matched_pair[%d]: returning %s", line_number, ret);*/
3512 /* Parse a $(...) command substitution. This is messier than I'd like, and
3513 reproduces a lot more of the token-reading code than I'd like. */
3515 parse_comsub (qc, open, close, lenp, flags)
3516 int qc; /* `"' if this construct is within double quotes */
3520 int count, ch, peekc, tflags, lex_rwlen, lex_wlen, lex_firstind;
3521 int nestlen, ttranslen, start_lineno;
3522 char *ret, *nestret, *ttrans, *heredelim;
3523 int retind, retsize, rflags, hdlen;
3525 /* Posix interp 217 says arithmetic expressions have precedence, so
3526 assume $(( introduces arithmetic expansion and parse accordingly. */
3527 peekc = shell_getc (0);
3528 shell_ungetc (peekc);
3530 return (parse_matched_pair (qc, open, close, lenp, 0));
3532 /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/
3534 tflags = LEX_RESWDOK;
3536 if ((flags & P_COMMAND) && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
3537 tflags |= LEX_CKCASE;
3538 if ((tflags & LEX_CKCASE) && (interactive == 0 || interactive_comments))
3539 tflags |= LEX_CKCOMMENT;
3541 /* RFLAGS is the set of flags we want to pass to recursive calls. */
3542 rflags = (flags & P_DQUOTE);
3544 ret = (char *)xmalloc (retsize = 64);
3547 start_lineno = line_number;
3548 lex_rwlen = lex_wlen = 0;
3556 ch = shell_getc (qc != '\'' && (tflags & (LEX_INCOMMENT|LEX_PASSNEXT)) == 0);
3563 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
3564 EOF_Reached = 1; /* XXX */
3565 return (&matched_pair_error);
3568 /* If we hit the end of a line and are reading the contents of a here
3569 document, and it's not the same line that the document starts on,
3570 check for this line being the here doc delimiter. Otherwise, if
3571 we're in a here document, mark the next character as the beginning
3575 if ((tflags & LEX_HEREDELIM) && heredelim)
3577 tflags &= ~LEX_HEREDELIM;
3578 tflags |= LEX_INHEREDOC;
3579 lex_firstind = retind + 1;
3581 else if (tflags & LEX_INHEREDOC)
3584 tind = lex_firstind;
3585 while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t')
3587 if (STREQN (ret + tind, heredelim, hdlen))
3589 tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC);
3590 /*itrace("parse_comsub:%d: found here doc end `%s'", line_number, ret + tind);*/
3596 lex_firstind = retind + 1;
3600 /* Possible reprompting. */
3601 if (ch == '\n' && SHOULD_PROMPT ())
3604 /* XXX -- possibly allow here doc to be delimited by ending right
3606 if ((tflags & LEX_INHEREDOC) && ch == close && count == 1)
3609 /*itrace("parse_comsub: in here doc, ch == close, retind - firstind = %d hdlen = %d retind = %d", retind-lex_firstind, hdlen, retind);*/
3610 tind = lex_firstind;
3611 while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t')
3613 if (retind-tind == hdlen && STREQN (ret + tind, heredelim, hdlen))
3615 tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC);
3616 /*itrace("parse_comsub:%d: found here doc end `%s'", line_number, ret + tind);*/
3623 /* Don't bother counting parens or doing anything else if in a comment */
3624 if (tflags & (LEX_INCOMMENT|LEX_INHEREDOC))
3626 /* Add this character. */
3627 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3630 if ((tflags & LEX_INCOMMENT) && ch == '\n')
3632 /*itrace("parse_comsub:%d: lex_incomment -> 0 ch = `%c'", line_number, ch);*/
3633 tflags &= ~LEX_INCOMMENT;
3639 if (tflags & LEX_PASSNEXT) /* last char was backslash */
3641 /*itrace("parse_comsub:%d: lex_passnext -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3642 tflags &= ~LEX_PASSNEXT;
3643 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
3646 retind--; /* swallow previously-added backslash */
3650 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3651 if MBTEST(ch == CTLESC)
3652 ret[retind++] = CTLESC;
3657 /* If this is a shell break character, we are not in a word. If not,
3658 we either start or continue a word. */
3659 if MBTEST(shellbreak (ch))
3661 tflags &= ~LEX_INWORD;
3662 /*itrace("parse_comsub:%d: lex_inword -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3666 if (tflags & LEX_INWORD)
3669 /*itrace("parse_comsub:%d: lex_inword == 1 ch = `%c' lex_wlen = %d (%d)", line_number, ch, lex_wlen, __LINE__);*/
3673 /*itrace("parse_comsub:%d: lex_inword -> 1 ch = `%c' (%d)", line_number, ch, __LINE__);*/
3674 tflags |= LEX_INWORD;
3679 /* Skip whitespace */
3680 if MBTEST(shellblank (ch) && (tflags & LEX_HEREDELIM) == 0 && lex_rwlen == 0)
3682 /* Add this character. */
3683 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3688 /* Either we are looking for the start of the here-doc delimiter
3689 (lex_firstind == -1) or we are reading one (lex_firstind >= 0).
3690 If this character is a shell break character and we are reading
3691 the delimiter, save it and note that we are now reading a here
3692 document. If we've found the start of the delimiter, note it by
3693 setting lex_firstind. Backslashes can quote shell metacharacters
3694 in here-doc delimiters. */
3695 if (tflags & LEX_HEREDELIM)
3697 if (lex_firstind == -1 && shellbreak (ch) == 0)
3698 lex_firstind = retind;
3700 else if (heredelim && (tflags & LEX_PASSNEXT) == 0 && ch == '\n')
3702 tflags |= LEX_INHEREDOC;
3703 tflags &= ~LEX_HEREDELIM;
3704 lex_firstind = retind + 1;
3707 else if (lex_firstind >= 0 && (tflags & LEX_PASSNEXT) == 0 && shellbreak (ch))
3711 nestret = substring (ret, lex_firstind, retind);
3712 heredelim = string_quote_removal (nestret, 0);
3714 hdlen = STRLEN(heredelim);
3715 /*itrace("parse_comsub:%d: found here doc delimiter `%s' (%d)", line_number, heredelim, hdlen);*/
3719 tflags |= LEX_INHEREDOC;
3720 tflags &= ~LEX_HEREDELIM;
3721 lex_firstind = retind + 1;
3728 /* Meta-characters that can introduce a reserved word. Not perfect yet. */
3729 if MBTEST((tflags & LEX_RESWDOK) == 0 && (tflags & LEX_CKCASE) && (tflags & LEX_INCOMMENT) == 0 && (shellmeta(ch) || ch == '\n'))
3731 /* Add this character. */
3732 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3734 peekc = shell_getc (1);
3735 if (ch == peekc && (ch == '&' || ch == '|' || ch == ';')) /* two-character tokens */
3737 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3738 ret[retind++] = peekc;
3739 /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/
3740 tflags |= LEX_RESWDOK;
3744 else if (ch == '\n' || COMSUB_META(ch))
3746 shell_ungetc (peekc);
3747 /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/
3748 tflags |= LEX_RESWDOK;
3756 /* `unget' the character we just added and fall through */
3758 shell_ungetc (peekc);
3762 /* If we can read a reserved word, try to read one. */
3763 if (tflags & LEX_RESWDOK)
3765 if MBTEST(islower (ch))
3767 /* Add this character. */
3768 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3773 else if MBTEST(lex_rwlen == 4 && shellbreak (ch))
3775 if (STREQN (ret + retind - 4, "case", 4))
3777 tflags |= LEX_INCASE;
3778 /*itrace("parse_comsub:%d: found `case', lex_incase -> 1 lex_reswdok -> 0", line_number);*/
3780 else if (STREQN (ret + retind - 4, "esac", 4))
3782 tflags &= ~LEX_INCASE;
3783 /*itrace("parse_comsub:%d: found `esac', lex_incase -> 0 lex_reswdok -> 0", line_number);*/
3785 tflags &= ~LEX_RESWDOK;
3787 else if MBTEST((tflags & LEX_CKCOMMENT) && ch == '#' && (lex_rwlen == 0 || ((tflags & LEX_INWORD) && lex_wlen == 0)))
3788 ; /* don't modify LEX_RESWDOK if we're starting a comment */
3789 /* Allow `do' followed by space, tab, or newline to preserve the
3790 RESWDOK flag, but reset the reserved word length counter so we
3791 can read another one. */
3792 else if MBTEST(((tflags & LEX_INCASE) == 0) &&
3793 (isblank(ch) || ch == '\n') &&
3795 STREQN (ret + retind - 2, "do", 2))
3797 /*itrace("parse_comsub:%d: lex_incase == 1 found `%c', found \"do\"", line_number, ch);*/
3800 else if MBTEST((tflags & LEX_INCASE) && ch != '\n')
3801 /* If we can read a reserved word and we're in case, we're at the
3802 point where we can read a new pattern list or an esac. We
3803 handle the esac case above. If we read a newline, we want to
3804 leave LEX_RESWDOK alone. If we read anything else, we want to
3805 turn off LEX_RESWDOK, since we're going to read a pattern list. */
3807 tflags &= ~LEX_RESWDOK;
3808 /*itrace("parse_comsub:%d: lex_incase == 1 found `%c', lex_reswordok -> 0", line_number, ch);*/
3810 else if MBTEST(shellbreak (ch) == 0)
3812 tflags &= ~LEX_RESWDOK;
3813 /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/
3816 /* If we find a space or tab but have read something and it's not
3817 `do', turn off the reserved-word-ok flag */
3818 else if MBTEST(isblank (ch) && lex_rwlen > 0)
3820 tflags &= ~LEX_RESWDOK;
3821 /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/
3826 /* Might be the start of a here-doc delimiter */
3827 if MBTEST((tflags & LEX_INCOMMENT) == 0 && (tflags & LEX_CKCASE) && ch == '<')
3829 /* Add this character. */
3830 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3832 peekc = shell_getc (1);
3837 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3838 ret[retind++] = peekc;
3839 peekc = shell_getc (1);
3844 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3845 ret[retind++] = peekc;
3846 tflags |= LEX_STRIPDOC;
3849 shell_ungetc (peekc);
3852 tflags |= LEX_HEREDELIM;
3858 ch = peekc; /* fall through and continue XXX */
3860 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (((tflags & LEX_RESWDOK) && lex_rwlen == 0) || ((tflags & LEX_INWORD) && lex_wlen == 0)))
3862 /*itrace("parse_comsub:%d: lex_incomment -> 1 (%d)", line_number, __LINE__);*/
3863 tflags |= LEX_INCOMMENT;
3866 if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
3868 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
3869 ret[retind++] = CTLESC;
3874 else if MBTEST((tflags & LEX_INCASE) && ch == close && close == ')')
3875 tflags &= ~LEX_INCASE; /* XXX */
3877 else if MBTEST(ch == close && (tflags & LEX_INCASE) == 0) /* ending delimiter */
3880 /*itrace("parse_comsub:%d: found close: count = %d", line_number, count);*/
3882 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && (tflags & LEX_INCASE) == 0 && ch == open) /* nested begin */
3885 /*itrace("parse_comsub:%d: found open: count = %d", line_number, count);*/
3888 /* Add this character. */
3889 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
3892 /* If we just read the ending character, don't bother continuing. */
3896 if MBTEST(ch == '\\') /* backslashes */
3897 tflags |= LEX_PASSNEXT;
3899 if MBTEST(shellquote (ch))
3901 /* '', ``, or "" inside $(...). */
3902 push_delimiter (dstack, ch);
3903 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
3904 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
3906 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
3907 pop_delimiter (dstack);
3908 CHECK_NESTRET_ERROR ();
3910 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
3912 /* Translate $'...' here. */
3913 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
3916 if ((rflags & P_DQUOTE) == 0)
3918 nestret = sh_single_quote (ttrans);
3920 nestlen = strlen (nestret);
3925 nestlen = ttranslen;
3927 retind -= 2; /* back up before the $' */
3929 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
3931 /* Locale expand $"..." here. */
3932 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
3935 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
3937 nestlen = ttranslen + 2;
3938 retind -= 2; /* back up before the $" */
3944 else if MBTEST((tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3945 /* check for $(), $[], or ${} inside command substitution. */
3947 if ((tflags & LEX_INCASE) == 0 && open == ch) /* undo previous increment */
3949 if (ch == '(') /* ) */
3950 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
3951 else if (ch == '{') /* } */
3952 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags);
3953 else if (ch == '[') /* ] */
3954 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
3956 CHECK_NESTRET_ERROR ();
3961 if MBTEST(ch == '$')
3962 tflags |= LEX_WASDOL;
3964 tflags &= ~LEX_WASDOL;
3971 /*itrace("parse_comsub:%d: returning `%s'", line_number, ret);*/
3975 /* Recursively call the parser to parse a $(...) command substitution. */
3977 xparse_dolparen (base, string, indp, flags)
3983 sh_parser_state_t ps;
3984 sh_input_line_state_t ls;
3985 int orig_ind, nc, sflags, orig_eof_token;
3986 char *ret, *s, *ep, *ostring;
3992 /*itrace("xparse_dolparen: size = %d shell_input_line = `%s'", shell_input_line_size, shell_input_line);*/
3993 sflags = SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOFREE;
3994 if (flags & SX_NOLONGJMP)
3995 sflags |= SEVAL_NOLONGJMP;
3996 save_parser_state (&ps);
3997 save_input_line_state (&ls);
3998 orig_eof_token = shell_eof_token;
4001 parser_state |= PST_CMDSUBST|PST_EOFTOKEN; /* allow instant ')' */ /*(*/
4002 shell_eof_token = ')';
4003 parse_string (string, "command substitution", sflags, &ep);
4005 shell_eof_token = orig_eof_token;
4006 restore_parser_state (&ps);
4008 /* reset_parser clears shell_input_line and associated variables */
4009 restore_input_line_state (&ls);
4013 /* Need to find how many characters parse_and_execute consumed, update
4014 *indp, if flags != 0, copy the portion of the string parsed into RET
4015 and return it. If flags & 1 (EX_NOALLOC) we can return NULL. */
4022 itrace("xparse_dolparen:%d: ep[-1] != RPAREN (%d), ep = `%s'", line_number, ep[-1], ep);
4024 while (ep > ostring && ep[-1] == '\n') ep--;
4028 *indp = ep - base - 1;
4032 if (base[*indp] != ')')
4033 itrace("xparse_dolparen:%d: base[%d] != RPAREN (%d), base = `%s'", line_number, *indp, base[*indp], base);
4036 if (flags & SX_NOALLOC)
4037 return (char *)NULL;
4045 ret = substring (ostring, 0, nc - 1);
4050 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
4051 /* Parse a double-paren construct. It can be either an arithmetic
4052 command, an arithmetic `for' command, or a nested subshell. Returns
4053 the parsed token, -1 on error, or -2 if we didn't do anything and
4054 should just go on. */
4063 #if defined (ARITH_FOR_COMMAND)
4064 if (last_read_token == FOR)
4066 arith_for_lineno = line_number;
4067 cmdtyp = parse_arith_cmd (&wval, 0);
4070 wd = alloc_word_desc ();
4072 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
4073 return (ARITH_FOR_EXPRS);
4076 return -1; /* ERROR */
4080 #if defined (DPAREN_ARITHMETIC)
4081 if (reserved_word_acceptable (last_read_token))
4083 sline = line_number;
4085 cmdtyp = parse_arith_cmd (&wval, 0);
4086 if (cmdtyp == 1) /* arithmetic command */
4088 wd = alloc_word_desc ();
4090 wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE;
4091 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
4094 else if (cmdtyp == 0) /* nested subshell */
4096 push_string (wval, 0, (alias_t *)NULL);
4097 pushed_string_list->flags = PSH_DPAREN;
4098 if ((parser_state & PST_CASEPAT) == 0)
4099 parser_state |= PST_SUBSHELL;
4107 return -2; /* XXX */
4110 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
4111 If not, assume it's a nested subshell for backwards compatibility and
4112 return 0. In any case, put the characters we've consumed into a locally-
4113 allocated buffer and make *ep point to that buffer. Return -1 on an
4114 error, for example EOF. */
4116 parse_arith_cmd (ep, adddq)
4120 int exp_lineno, rval, c;
4121 char *ttok, *tokstr;
4124 exp_lineno = line_number;
4125 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
4127 if (ttok == &matched_pair_error)
4129 /* Check that the next character is the closing right paren. If
4130 not, this is a syntax error. ( */
4135 tokstr = (char *)xmalloc (ttoklen + 4);
4137 /* if ADDDQ != 0 then (( ... )) -> "..." */
4138 if (rval == 1 && adddq) /* arith cmd, add double quotes */
4141 strncpy (tokstr + 1, ttok, ttoklen - 1);
4142 tokstr[ttoklen] = '"';
4143 tokstr[ttoklen+1] = '\0';
4145 else if (rval == 1) /* arith cmd, don't add double quotes */
4147 strncpy (tokstr, ttok, ttoklen - 1);
4148 tokstr[ttoklen-1] = '\0';
4150 else /* nested subshell */
4153 strncpy (tokstr + 1, ttok, ttoklen - 1);
4154 tokstr[ttoklen] = ')';
4155 tokstr[ttoklen+1] = c;
4156 tokstr[ttoklen+2] = '\0';
4163 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
4165 #if defined (COND_COMMAND)
4171 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
4172 parser_error (cond_lineno, _("unexpected EOF while looking for `]]'"));
4173 else if (cond_token != COND_ERROR)
4175 if (etext = error_token_from_token (cond_token))
4177 parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext);
4181 parser_error (cond_lineno, _("syntax error in conditional expression"));
4188 return (cond_or ());
4197 if (cond_token == OR_OR)
4200 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
4211 if (cond_token == AND_AND)
4214 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
4220 cond_skip_newlines ()
4222 while ((cond_token = read_token (READ)) == '\n')
4224 if (SHOULD_PROMPT ())
4227 return (cond_token);
4230 #define COND_RETURN_ERROR() \
4231 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
4237 COND_COM *term, *tleft, *tright;
4241 /* Read a token. It can be a left paren, a `!', a unary operator, or a
4242 word that should be the first argument of a binary operator. Start by
4243 skipping newlines, since this is a compound command. */
4244 tok = cond_skip_newlines ();
4245 lineno = line_number;
4246 if (tok == COND_END)
4248 COND_RETURN_ERROR ();
4250 else if (tok == '(')
4252 term = cond_expr ();
4253 if (cond_token != ')')
4256 dispose_cond_node (term); /* ( */
4257 if (etext = error_token_from_token (cond_token))
4259 parser_error (lineno, _("unexpected token `%s', expected `)'"), etext);
4263 parser_error (lineno, _("expected `)'"));
4264 COND_RETURN_ERROR ();
4266 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
4267 (void)cond_skip_newlines ();
4269 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
4272 dispose_word (yylval.word); /* not needed */
4273 term = cond_term ();
4275 term->flags |= CMD_INVERT_RETURN;
4277 else if (tok == WORD && yylval.word->word[0] == '-' && yylval.word->word[2] == 0 && test_unop (yylval.word->word))
4280 tok = read_token (READ);
4283 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4284 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
4289 if (etext = error_token_from_token (tok))
4291 parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext);
4295 parser_error (line_number, _("unexpected argument to conditional unary operator"));
4296 COND_RETURN_ERROR ();
4299 (void)cond_skip_newlines ();
4301 else if (tok == WORD) /* left argument to binary operator */
4304 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4307 tok = read_token (READ);
4308 if (tok == WORD && test_binop (yylval.word->word))
4311 if (op->word[0] == '=' && (op->word[1] == '\0' || (op->word[1] == '=' && op->word[2] == '\0')))
4312 parser_state |= PST_EXTPAT;
4313 else if (op->word[0] == '!' && op->word[1] == '=' && op->word[2] == '\0')
4314 parser_state |= PST_EXTPAT;
4316 #if defined (COND_REGEXP)
4317 else if (tok == WORD && STREQ (yylval.word->word, "=~"))
4320 parser_state |= PST_REGEXP;
4323 else if (tok == '<' || tok == '>')
4324 op = make_word_from_token (tok); /* ( */
4325 /* There should be a check before blindly accepting the `)' that we have
4326 seen the opening `('. */
4327 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
4329 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
4330 the test command. Similarly for [[ x && expr ]] or
4331 [[ x || expr ]] or [[ (x) ]]. */
4332 op = make_word ("-n");
4333 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
4339 if (etext = error_token_from_token (tok))
4341 parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext);
4345 parser_error (line_number, _("conditional binary operator expected"));
4346 dispose_cond_node (tleft);
4347 COND_RETURN_ERROR ();
4351 if (parser_state & PST_EXTPAT)
4353 tok = read_token (READ);
4354 if (parser_state & PST_EXTPAT)
4355 extended_glob = global_extglob;
4356 parser_state &= ~(PST_REGEXP|PST_EXTPAT);
4360 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4361 term = make_cond_node (COND_BINARY, op, tleft, tright);
4365 if (etext = error_token_from_token (tok))
4367 parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext);
4371 parser_error (line_number, _("unexpected argument to conditional binary operator"));
4372 dispose_cond_node (tleft);
4374 COND_RETURN_ERROR ();
4377 (void)cond_skip_newlines ();
4382 parser_error (line_number, _("unexpected token `%c' in conditional command"), tok);
4383 else if (etext = error_token_from_token (tok))
4385 parser_error (line_number, _("unexpected token `%s' in conditional command"), etext);
4389 parser_error (line_number, _("unexpected token %d in conditional command"), tok);
4390 COND_RETURN_ERROR ();
4395 /* This is kind of bogus -- we slip a mini recursive-descent parser in
4396 here to handle the conditional statement syntax. */
4398 parse_cond_command ()
4402 global_extglob = extended_glob;
4403 cexp = cond_expr ();
4404 return (make_cond_command (cexp));
4408 #if defined (ARRAY_VARS)
4409 /* When this is called, it's guaranteed that we don't care about anything
4410 in t beyond i. We do save and restore the chars, though. */
4412 token_is_assignment (t, i)
4416 unsigned char c, c1;
4419 c = t[i]; c1 = t[i+1];
4420 t[i] = '='; t[i+1] = '\0';
4421 r = assignment (t, (parser_state & PST_COMPASSIGN) != 0);
4422 t[i] = c; t[i+1] = c1;
4426 /* XXX - possible changes here for `+=' */
4428 token_is_ident (t, i)
4437 r = legal_identifier (t);
4444 read_token_word (character)
4447 /* The value for YYLVAL when a WORD is read. */
4448 WORD_DESC *the_word;
4450 /* Index into the token that we are building. */
4453 /* ALL_DIGITS becomes zero when we see a non-digit. */
4454 int all_digit_token;
4456 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
4459 /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
4461 int compound_assignment;
4463 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
4466 /* Non-zero means to ignore the value of the next character, and just
4467 to add it no matter what. */
4468 int pass_next_character;
4470 /* The current delimiting character. */
4472 int result, peek_char;
4473 char *ttok, *ttrans;
4474 int ttoklen, ttranslen;
4477 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
4478 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
4481 all_digit_token = DIGIT (character);
4482 dollar_present = quoted = pass_next_character = compound_assignment = 0;
4486 if (character == EOF)
4489 if (pass_next_character)
4491 pass_next_character = 0;
4492 goto got_escaped_character;
4495 cd = current_delimiter (dstack);
4497 /* Handle backslashes. Quote lots of things when not inside of
4498 double-quotes, quote some things inside of double-quotes. */
4499 if MBTEST(character == '\\')
4501 peek_char = shell_getc (0);
4503 /* Backslash-newline is ignored in all cases except
4504 when quoted with single quotes. */
4505 if (peek_char == '\n')
4508 goto next_character;
4512 shell_ungetc (peek_char);
4514 /* If the next character is to be quoted, note it now. */
4515 if (cd == 0 || cd == '`' ||
4516 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
4517 pass_next_character++;
4524 /* Parse a matched pair of quote characters. */
4525 if MBTEST(shellquote (character))
4527 push_delimiter (dstack, character);
4528 ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0);
4529 pop_delimiter (dstack);
4530 if (ttok == &matched_pair_error)
4531 return -1; /* Bail immediately. */
4532 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4533 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4534 token[token_index++] = character;
4535 strcpy (token + token_index, ttok);
4536 token_index += ttoklen;
4537 all_digit_token = 0;
4539 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
4541 goto next_character;
4545 /* When parsing a regexp as a single word inside a conditional command,
4546 we need to special-case characters special to both the shell and
4547 regular expressions. Right now, that is only '(' and '|'. */ /*)*/
4548 if MBTEST((parser_state & PST_REGEXP) && (character == '(' || character == '|')) /*)*/
4550 if (character == '|')
4553 push_delimiter (dstack, character);
4554 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4555 pop_delimiter (dstack);
4556 if (ttok == &matched_pair_error)
4557 return -1; /* Bail immediately. */
4558 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4559 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4560 token[token_index++] = character;
4561 strcpy (token + token_index, ttok);
4562 token_index += ttoklen;
4564 dollar_present = all_digit_token = 0;
4565 goto next_character;
4567 #endif /* COND_REGEXP */
4569 #ifdef EXTENDED_GLOB
4570 /* Parse a ksh-style extended pattern matching specification. */
4571 if MBTEST(extended_glob && PATTERN_CHAR (character))
4573 peek_char = shell_getc (1);
4574 if MBTEST(peek_char == '(') /* ) */
4576 push_delimiter (dstack, peek_char);
4577 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4578 pop_delimiter (dstack);
4579 if (ttok == &matched_pair_error)
4580 return -1; /* Bail immediately. */
4581 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,
4583 TOKEN_DEFAULT_GROW_SIZE);
4584 token[token_index++] = character;
4585 token[token_index++] = peek_char;
4586 strcpy (token + token_index, ttok);
4587 token_index += ttoklen;
4589 dollar_present = all_digit_token = 0;
4590 goto next_character;
4593 shell_ungetc (peek_char);
4595 #endif /* EXTENDED_GLOB */
4597 /* If the delimiter character is not single quote, parse some of
4598 the shell expansions that must be read as a single word. */
4599 if (shellexp (character))
4601 peek_char = shell_getc (1);
4602 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
4603 if MBTEST(peek_char == '(' ||
4604 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
4606 if (peek_char == '{') /* } */
4607 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE|P_DOLBRACE);
4608 else if (peek_char == '(') /* ) */
4610 /* XXX - push and pop the `(' as a delimiter for use by
4611 the command-oriented-history code. This way newlines
4612 appearing in the $(...) string get added to the
4613 history literally rather than causing a possibly-
4614 incorrect `;' to be added. ) */
4615 push_delimiter (dstack, peek_char);
4616 ttok = parse_comsub (cd, '(', ')', &ttoklen, P_COMMAND);
4617 pop_delimiter (dstack);
4620 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
4621 if (ttok == &matched_pair_error)
4622 return -1; /* Bail immediately. */
4623 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,
4625 TOKEN_DEFAULT_GROW_SIZE);
4626 token[token_index++] = character;
4627 token[token_index++] = peek_char;
4628 strcpy (token + token_index, ttok);
4629 token_index += ttoklen;
4632 all_digit_token = 0;
4633 goto next_character;
4635 /* This handles $'...' and $"..." new-style quoted strings. */
4636 else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
4640 first_line = line_number;
4641 push_delimiter (dstack, peek_char);
4642 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
4644 (peek_char == '\'') ? P_ALLOWESC : 0);
4645 pop_delimiter (dstack);
4646 if (ttok == &matched_pair_error)
4648 if (peek_char == '\'')
4650 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
4653 /* Insert the single quotes and correctly quote any
4654 embedded single quotes (allowed because P_ALLOWESC was
4655 passed to parse_matched_pair). */
4656 ttok = sh_single_quote (ttrans);
4658 ttranslen = strlen (ttok);
4663 /* Try to locale-expand the converted string. */
4664 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
4667 /* Add the double quotes back */
4668 ttok = sh_mkdoublequoted (ttrans, ttranslen, 0);
4674 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 1,
4676 TOKEN_DEFAULT_GROW_SIZE);
4677 strcpy (token + token_index, ttrans);
4678 token_index += ttranslen;
4681 all_digit_token = 0;
4682 goto next_character;
4684 /* This could eventually be extended to recognize all of the
4685 shell's single-character parameter expansions, and set flags.*/
4686 else if MBTEST(character == '$' && peek_char == '$')
4688 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
4690 TOKEN_DEFAULT_GROW_SIZE);
4691 token[token_index++] = '$';
4692 token[token_index++] = peek_char;
4694 all_digit_token = 0;
4695 goto next_character;
4698 shell_ungetc (peek_char);
4701 #if defined (ARRAY_VARS)
4702 /* Identify possible array subscript assignment; match [...]. If
4703 parser_state&PST_COMPASSIGN, we need to parse [sub]=words treating
4704 `sub' as if it were enclosed in double quotes. */
4705 else if MBTEST(character == '[' && /* ] */
4706 ((token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) ||
4707 (token_index == 0 && (parser_state&PST_COMPASSIGN))))
4709 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, P_ARRAYSUB);
4710 if (ttok == &matched_pair_error)
4711 return -1; /* Bail immediately. */
4712 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4714 TOKEN_DEFAULT_GROW_SIZE);
4715 token[token_index++] = character;
4716 strcpy (token + token_index, ttok);
4717 token_index += ttoklen;
4719 all_digit_token = 0;
4720 goto next_character;
4722 /* Identify possible compound array variable assignment. */
4723 else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index))
4725 peek_char = shell_getc (1);
4726 if MBTEST(peek_char == '(') /* ) */
4728 ttok = parse_compound_assignment (&ttoklen);
4730 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
4732 TOKEN_DEFAULT_GROW_SIZE);
4734 token[token_index++] = '=';
4735 token[token_index++] = '(';
4738 strcpy (token + token_index, ttok);
4739 token_index += ttoklen;
4741 token[token_index++] = ')';
4743 all_digit_token = 0;
4744 compound_assignment = 1;
4746 goto next_character;
4748 goto got_token; /* ksh93 seems to do this */
4752 shell_ungetc (peek_char);
4756 /* When not parsing a multi-character word construct, shell meta-
4757 characters break words. */
4758 if MBTEST(shellbreak (character))
4760 shell_ungetc (character);
4766 if (character == CTLESC || character == CTLNUL)
4768 RESIZE_MALLOCED_BUFFER (token, token_index, 2, token_buffer_size,
4769 TOKEN_DEFAULT_GROW_SIZE);
4770 token[token_index++] = CTLESC;
4773 got_escaped_character:
4774 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
4775 TOKEN_DEFAULT_GROW_SIZE);
4777 token[token_index++] = character;
4779 all_digit_token &= DIGIT (character);
4780 dollar_present |= character == '$';
4783 if (character == '\n' && SHOULD_PROMPT ())
4786 /* We want to remove quoted newlines (that is, a \<newline> pair)
4787 unless we are within single quotes or pass_next_character is
4788 set (the shell equivalent of literal-next). */
4789 cd = current_delimiter (dstack);
4790 character = shell_getc (cd != '\'' && pass_next_character == 0);
4791 } /* end for (;;) */
4795 /* Calls to RESIZE_MALLOCED_BUFFER ensure there is sufficient room. */
4796 token[token_index] = '\0';
4798 /* Check to see what thing we should return. If the last_read_token
4799 is a `<', or a `&', or the character which ended this token is
4800 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
4801 Otherwise, it is just a word, and should be returned as such. */
4802 if MBTEST(all_digit_token && (character == '<' || character == '>' ||
4803 last_read_token == LESS_AND ||
4804 last_read_token == GREATER_AND))
4806 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
4808 yylval.number = lvalue;
4813 /* Check for special case tokens. */
4814 result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
4819 /* Posix.2 does not allow reserved words to be aliased, so check for all
4820 of them, including special cases, before expanding the current token
4822 if MBTEST(posixly_correct)
4823 CHECK_FOR_RESERVED_WORD (token);
4825 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
4826 inhibits alias expansion. */
4827 if (expand_aliases && quoted == 0)
4829 result = alias_expand_token (token);
4830 if (result == RE_READ_TOKEN)
4831 return (RE_READ_TOKEN);
4832 else if (result == NO_EXPANSION)
4833 parser_state &= ~PST_ALEXPNEXT;
4836 /* If not in Posix.2 mode, check for reserved words after alias
4838 if MBTEST(posixly_correct == 0)
4840 CHECK_FOR_RESERVED_WORD (token);
4842 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
4843 the_word->word = (char *)xmalloc (1 + token_index);
4844 the_word->flags = 0;
4845 strcpy (the_word->word, token);
4847 the_word->flags |= W_HASDOLLAR;
4849 the_word->flags |= W_QUOTED; /*(*/
4850 if (compound_assignment && token[token_index-1] == ')')
4851 the_word->flags |= W_COMPASSIGN;
4852 /* A word is an assignment if it appears at the beginning of a
4853 simple command, or after another assignment word. This is
4854 context-dependent, so it cannot be handled in the grammar. */
4855 if (assignment (token, (parser_state & PST_COMPASSIGN) != 0))
4857 the_word->flags |= W_ASSIGNMENT;
4858 /* Don't perform word splitting on assignment statements. */
4859 if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0)
4861 the_word->flags |= W_NOSPLIT;
4862 if (parser_state & PST_COMPASSIGN)
4863 the_word->flags |= W_NOGLOB; /* XXX - W_NOBRACE? */
4867 if (command_token_position (last_read_token))
4870 b = builtin_address_internal (token, 0);
4871 if (b && (b->flags & ASSIGNMENT_BUILTIN))
4872 parser_state |= PST_ASSIGNOK;
4873 else if (STREQ (token, "eval") || STREQ (token, "let"))
4874 parser_state |= PST_ASSIGNOK;
4877 yylval.word = the_word;
4879 if (token[0] == '{' && token[token_index-1] == '}' &&
4880 (character == '<' || character == '>'))
4882 /* can use token; already copied to the_word */
4883 token[token_index-1] = '\0';
4884 #if defined (ARRAY_VARS)
4885 if (legal_identifier (token+1) || valid_array_reference (token+1))
4887 if (legal_identifier (token+1))
4890 strcpy (the_word->word, token+1);
4891 /*itrace("read_token_word: returning REDIR_WORD for %s", the_word->word);*/
4892 return (REDIR_WORD);
4896 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
4897 ? ASSIGNMENT_WORD : WORD;
4899 switch (last_read_token)
4902 parser_state |= PST_ALLOWOPNBRC;
4903 function_dstart = line_number;
4908 if (word_top < MAX_CASE_NEST)
4910 word_lineno[word_top] = line_number;
4917 /* Return 1 if TOKSYM is a token that after being read would allow
4918 a reserved word to be seen, else 0. */
4920 reserved_word_acceptable (toksym)
4957 #if defined (COPROCESS_SUPPORT)
4958 if (last_read_token == WORD && token_before_that == COPROC)
4961 if (last_read_token == WORD && token_before_that == FUNCTION)
4967 /* Return the index of TOKEN in the alist of reserved words, or -1 if
4968 TOKEN is not a shell reserved word. */
4970 find_reserved_word (tokstr)
4974 for (i = 0; word_token_alist[i].word; i++)
4975 if (STREQ (tokstr, word_token_alist[i].word))
4980 /* An interface to let the rest of the shell (primarily the completion
4981 system) know what the parser is expecting. */
4983 parser_in_command_position ()
4985 return (command_token_position (last_read_token));
4989 #if defined (READLINE)
4990 /* Called after each time readline is called. This insures that whatever
4991 the new prompt string is gets propagated to readline's local prompt
4994 reset_readline_prompt ()
4998 if (prompt_string_pointer)
5000 temp_prompt = (*prompt_string_pointer)
5001 ? decode_prompt_string (*prompt_string_pointer)
5004 if (temp_prompt == 0)
5006 temp_prompt = (char *)xmalloc (1);
5007 temp_prompt[0] = '\0';
5010 FREE (current_readline_prompt);
5011 current_readline_prompt = temp_prompt;
5014 #endif /* READLINE */
5017 #if defined (HISTORY)
5018 /* A list of tokens which can be followed by newlines, but not by
5019 semi-colons. When concatenating multiple lines of history, the
5020 newline separator for such tokens is replaced with a space. */
5021 static const int no_semi_successors[] = {
5022 '\n', '{', '(', ')', ';', '&', '|',
5023 CASE, DO, ELSE, IF, SEMI_SEMI, SEMI_AND, SEMI_SEMI_AND, THEN, UNTIL,
5024 WHILE, AND_AND, OR_OR, IN,
5028 /* If we are not within a delimited expression, try to be smart
5029 about which separators can be semi-colons and which must be
5030 newlines. Returns the string that should be added into the
5031 history entry. LINE is the line we're about to add; it helps
5032 make some more intelligent decisions in certain cases. */
5034 history_delimiting_chars (line)
5037 static int last_was_heredoc = 0; /* was the last entry the start of a here document? */
5040 if ((parser_state & PST_HEREDOC) == 0)
5041 last_was_heredoc = 0;
5043 if (dstack.delimiter_depth != 0)
5046 /* We look for current_command_line_count == 2 because we are looking to
5047 add the first line of the body of the here document (the second line
5048 of the command). We also keep LAST_WAS_HEREDOC as a private sentinel
5049 variable to note when we think we added the first line of a here doc
5050 (the one with a "<<" somewhere in it) */
5051 if (parser_state & PST_HEREDOC)
5053 if (last_was_heredoc)
5055 last_was_heredoc = 0;
5058 return (current_command_line_count == 2 ? "\n" : "");
5061 if (parser_state & PST_COMPASSIGN)
5064 /* First, handle some special cases. */
5066 /* If we just read `()', assume it's a function definition, and don't
5067 add a semicolon. If the token before the `)' was not `(', and we're
5068 not in the midst of parsing a case statement, assume it's a
5069 parenthesized command and add the semicolon. */
5071 if (token_before_that == ')')
5073 if (two_tokens_ago == '(') /*)*/ /* function def */
5075 /* This does not work for subshells inside case statement
5076 command lists. It's a suboptimal solution. */
5077 else if (parser_state & PST_CASESTMT) /* case statement pattern */
5080 return "; "; /* (...) subshell */
5082 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
5083 return " "; /* function def using `function name' without `()' */
5085 /* If we're not in a here document, but we think we're about to parse one,
5086 and we would otherwise return a `;', return a newline to delimit the
5087 line with the here-doc delimiter */
5088 else if ((parser_state & PST_HEREDOC) == 0 && current_command_line_count > 1 && last_read_token == '\n' && strstr (line, "<<"))
5090 last_was_heredoc = 1;
5094 else if (token_before_that == WORD && two_tokens_ago == FOR)
5096 /* Tricky. `for i\nin ...' should not have a semicolon, but
5097 `for i\ndo ...' should. We do what we can. */
5098 for (i = shell_input_line_index; whitespace (shell_input_line[i]); i++)
5100 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
5104 else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT))
5107 for (i = 0; no_semi_successors[i]; i++)
5109 if (token_before_that == no_semi_successors[i])
5115 #endif /* HISTORY */
5117 /* Issue a prompt, or prepare to issue a prompt when the next character
5124 if (interactive == 0 || expanding_alias ()) /* XXX */
5127 ps1_prompt = get_string_value ("PS1");
5128 ps2_prompt = get_string_value ("PS2");
5130 if (!prompt_string_pointer)
5131 prompt_string_pointer = &ps1_prompt;
5133 temp_prompt = *prompt_string_pointer
5134 ? decode_prompt_string (*prompt_string_pointer)
5137 if (temp_prompt == 0)
5139 temp_prompt = (char *)xmalloc (1);
5140 temp_prompt[0] = '\0';
5143 current_prompt_string = *prompt_string_pointer;
5144 prompt_string_pointer = &ps2_prompt;
5146 #if defined (READLINE)
5147 if (!no_line_editing)
5149 FREE (current_readline_prompt);
5150 current_readline_prompt = temp_prompt;
5153 #endif /* READLINE */
5155 FREE (current_decoded_prompt);
5156 current_decoded_prompt = temp_prompt;
5161 get_current_prompt_level ()
5163 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
5167 set_current_prompt_level (x)
5170 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
5171 current_prompt_string = *prompt_string_pointer;
5177 fprintf (stderr, "%s", current_decoded_prompt);
5181 /* Return a string which will be printed as a prompt. The string
5182 may contain special characters which are decoded as follows:
5185 \d the date in Day Mon Date format
5186 \e escape (ascii 033)
5187 \h the hostname up to the first `.'
5189 \j the number of active jobs
5190 \l the basename of the shell's tty device name
5193 \s the name of the shell
5194 \t the time in 24-hour hh:mm:ss format
5195 \T the time in 12-hour hh:mm:ss format
5196 \@ the time in 12-hour hh:mm am/pm format
5197 \A the time in 24-hour hh:mm format
5198 \D{fmt} the result of passing FMT to strftime(3)
5200 \v the version of bash (e.g., 2.00)
5201 \V the release of bash, version + patchlevel (e.g., 2.00.0)
5202 \w the current working directory
5203 \W the last element of $PWD
5204 \! the history number of this command
5205 \# the command number of this command
5206 \$ a $ or a # if you are root
5207 \nnn character code nnn in octal
5209 \[ begin a sequence of non-printing chars
5210 \] end a sequence of non-printing chars
5212 #define PROMPT_GROWTH 48
5214 decode_prompt_string (string)
5219 struct dstack save_dstack;
5220 int last_exit_value, last_comsub_pid;
5221 #if defined (PROMPT_STRING_DECODE)
5222 int result_size, result_index;
5224 char *temp, octal_string[4];
5230 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
5231 result[result_index = 0] = 0;
5232 temp = (char *)NULL;
5234 while (c = *string++)
5236 if (posixly_correct && c == '!')
5240 temp = savestring ("!");
5245 #if !defined (HISTORY)
5246 temp = savestring ("1");
5248 temp = itos (history_number ());
5249 #endif /* HISTORY */
5250 string--; /* add_string increments string again. */
5268 strncpy (octal_string, string, 3);
5269 octal_string[3] = '\0';
5271 n = read_octal (octal_string);
5272 temp = (char *)xmalloc (3);
5274 if (n == CTLESC || n == CTLNUL)
5291 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
5294 c = 0; /* tested at add_string: */
5302 /* Make the current time/date into a string. */
5303 (void) time (&the_time);
5304 #if defined (HAVE_TZSET)
5305 sv_tz ("TZ"); /* XXX -- just make sure */
5307 tm = localtime (&the_time);
5310 n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm);
5312 n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm);
5314 n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm);
5316 n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm);
5318 n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm);
5323 timebuf[sizeof(timebuf) - 1] = '\0';
5325 temp = savestring (timebuf);
5328 case 'D': /* strftime format */
5329 if (string[1] != '{') /* } */
5332 (void) time (&the_time);
5333 tm = localtime (&the_time);
5334 string += 2; /* skip { */
5335 timefmt = xmalloc (strlen (string) + 3);
5336 for (t = timefmt; *string && *string != '}'; )
5339 c = *string; /* tested at add_string */
5340 if (timefmt[0] == '\0')
5343 timefmt[1] = 'X'; /* locale-specific current time */
5346 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
5352 timebuf[sizeof(timebuf) - 1] = '\0';
5354 if (promptvars || posixly_correct)
5355 /* Make sure that expand_prompt_string is called with a
5356 second argument of Q_DOUBLE_QUOTES if we use this
5358 temp = sh_backslash_quote_for_double_quotes (timebuf);
5360 temp = savestring (timebuf);
5364 temp = (char *)xmalloc (3);
5365 temp[0] = no_line_editing ? '\n' : '\r';
5366 temp[1] = no_line_editing ? '\0' : '\n';
5371 temp = base_pathname (shell_name);
5372 temp = savestring (temp);
5377 temp = (char *)xmalloc (16);
5379 strcpy (temp, dist_version);
5381 sprintf (temp, "%s.%d", dist_version, patch_level);
5387 /* Use the value of PWD because it is much more efficient. */
5388 char t_string[PATH_MAX];
5391 temp = get_string_value ("PWD");
5395 if (getcwd (t_string, sizeof(t_string)) == 0)
5401 tlen = strlen (t_string);
5405 tlen = sizeof (t_string) - 1;
5406 strncpy (t_string, temp, tlen);
5408 t_string[tlen] = '\0';
5410 #if defined (MACOSX)
5411 /* Convert from "fs" format to "input" format */
5412 temp = fnx_fromfs (t_string, strlen (t_string));
5413 if (temp != t_string)
5414 strcpy (t_string, temp);
5417 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
5418 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
5419 /* Abbreviate \W as ~ if $PWD == $HOME */
5420 if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0))
5422 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
5424 t = strrchr (t_string, '/');
5426 memmove (t_string, t + 1, strlen (t)); /* strlen(t) to copy NULL */
5430 #undef DOUBLE_SLASH_ROOT
5433 /* polite_directory_format is guaranteed to return a string
5434 no longer than PATH_MAX - 1 characters. */
5435 temp = polite_directory_format (t_string);
5436 if (temp != t_string)
5437 strcpy (t_string, temp);
5440 temp = trim_pathname (t_string, PATH_MAX - 1);
5441 /* If we're going to be expanding the prompt string later,
5442 quote the directory name. */
5443 if (promptvars || posixly_correct)
5444 /* Make sure that expand_prompt_string is called with a
5445 second argument of Q_DOUBLE_QUOTES if we use this
5447 temp = sh_backslash_quote_for_double_quotes (t_string);
5449 temp = savestring (t_string);
5455 if (current_user.user_name == 0)
5456 get_current_user_info ();
5457 temp = savestring (current_user.user_name);
5462 temp = savestring (current_host_name);
5463 if (c == 'h' && (t = (char *)strchr (temp, '.')))
5468 temp = itos (current_command_number);
5472 #if !defined (HISTORY)
5473 temp = savestring ("1");
5475 temp = itos (history_number ());
5476 #endif /* HISTORY */
5480 t = temp = (char *)xmalloc (3);
5481 if ((promptvars || posixly_correct) && (current_user.euid != 0))
5483 *t++ = current_user.euid == 0 ? '#' : '$';
5488 temp = itos (count_all_jobs ());
5492 #if defined (HAVE_TTYNAME)
5493 temp = (char *)ttyname (fileno (stdin));
5494 t = temp ? base_pathname (temp) : "tty";
5495 temp = savestring (t);
5497 temp = savestring ("tty");
5498 #endif /* !HAVE_TTYNAME */
5501 #if defined (READLINE)
5504 if (no_line_editing)
5509 temp = (char *)xmalloc (3);
5510 n = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
5512 if (n == CTLESC || n == CTLNUL)
5517 #endif /* READLINE */
5523 temp = (char *)xmalloc (2);
5530 else /* (c == '\\') */
5537 temp = (char *)xmalloc (3);
5546 sub_append_string (temp, result, &result_index, &result_size);
5547 temp = (char *)NULL; /* Freed in sub_append_string (). */
5548 result[result_index] = '\0';
5554 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
5555 result[result_index++] = c;
5556 result[result_index] = '\0';
5559 #else /* !PROMPT_STRING_DECODE */
5560 result = savestring (string);
5561 #endif /* !PROMPT_STRING_DECODE */
5563 /* Save the delimiter stack and point `dstack' to temp space so any
5564 command substitutions in the prompt string won't result in screwing
5565 up the parser's quoting state. */
5566 save_dstack = dstack;
5567 dstack = temp_dstack;
5568 dstack.delimiter_depth = 0;
5570 /* Perform variable and parameter expansion and command substitution on
5571 the prompt string. */
5572 if (promptvars || posixly_correct)
5574 last_exit_value = last_command_exit_value;
5575 last_comsub_pid = last_command_subst_pid;
5576 list = expand_prompt_string (result, Q_DOUBLE_QUOTES, 0);
5578 result = string_list (list);
5579 dispose_words (list);
5580 last_command_exit_value = last_exit_value;
5581 last_command_subst_pid = last_comsub_pid;
5585 t = dequote_string (result);
5590 dstack = save_dstack;
5595 /************************************************
5599 ************************************************/
5601 /* Report a syntax error, and restart the parser. Call here for fatal
5607 report_syntax_error ((char *)NULL);
5613 error_token_from_token (tok)
5618 if (t = find_token_in_alist (tok, word_token_alist, 0))
5621 if (t = find_token_in_alist (tok, other_token_alist, 0))
5625 /* This stuff is dicy and needs closer inspection */
5626 switch (current_token)
5629 case ASSIGNMENT_WORD:
5631 t = savestring (yylval.word->word);
5634 t = itos (yylval.number);
5637 if (yylval.word_list)
5638 t = string_list (yylval.word_list);
5640 case ARITH_FOR_EXPRS:
5641 if (yylval.word_list)
5642 t = string_list_internal (yylval.word_list, " ; ");
5645 t = (char *)NULL; /* punt */
5653 error_token_from_text ()
5658 t = shell_input_line;
5659 i = shell_input_line_index;
5663 if (i && t[i] == '\0')
5666 while (i && (whitespace (t[i]) || t[i] == '\n'))
5672 while (i && (member (t[i], " \n\t;|&") == 0))
5675 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
5678 /* Return our idea of the offending token. */
5679 if (token_end || (i == 0 && token_end == 0))
5682 msg = substring (t, i, token_end);
5683 else /* one-character token */
5685 msg = (char *)xmalloc (2);
5695 print_offending_line ()
5700 msg = savestring (shell_input_line);
5701 token_end = strlen (msg);
5702 while (token_end && msg[token_end - 1] == '\n')
5703 msg[--token_end] = '\0';
5705 parser_error (line_number, "`%s'", msg);
5709 /* Report a syntax error with line numbers, etc.
5710 Call here for recoverable errors. If you have a message to print,
5711 then place it in MESSAGE, otherwise pass NULL and this will figure
5712 out an appropriate message for you. */
5714 report_syntax_error (message)
5721 parser_error (line_number, "%s", message);
5722 if (interactive && EOF_Reached)
5724 last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE;
5728 /* If the line of input we're reading is not null, try to find the
5729 objectionable token. First, try to figure out what token the
5730 parser's complaining about by looking at current_token. */
5731 if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
5733 if (ansic_shouldquote (msg))
5735 p = ansic_quote (msg, 0, NULL);
5739 parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
5742 if (interactive == 0)
5743 print_offending_line ();
5745 last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE;
5749 /* If looking at the current token doesn't prove fruitful, try to find the
5750 offending token by analyzing the text of the input line near the current
5751 input line index and report what we find. */
5752 if (shell_input_line && *shell_input_line)
5754 msg = error_token_from_text ();
5757 parser_error (line_number, _("syntax error near `%s'"), msg);
5761 /* If not interactive, print the line containing the error. */
5762 if (interactive == 0)
5763 print_offending_line ();
5767 msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error");
5768 parser_error (line_number, "%s", msg);
5769 /* When the shell is interactive, this file uses EOF_Reached
5770 only for error reporting. Other mechanisms are used to
5771 decide whether or not to exit. */
5772 if (interactive && EOF_Reached)
5776 last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE;
5779 /* ??? Needed function. ??? We have to be able to discard the constructs
5780 created during parsing. In the case of error, we want to return
5781 allocated objects to the memory pool. In the case of no error, we want
5782 to throw away the information about where the allocated objects live.
5783 (dispose_command () will actually free the command.) */
5785 discard_parser_constructs (error_p)
5790 /************************************************
5794 ************************************************/
5796 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
5798 /* A flag denoting whether or not ignoreeof is set. */
5801 /* The number of times that we have encountered an EOF character without
5802 another character intervening. When this gets above the limit, the
5803 shell terminates. */
5804 int eof_encountered = 0;
5806 /* The limit for eof_encountered. */
5807 int eof_encountered_limit = 10;
5809 /* If we have EOF as the only input unit, this user wants to leave
5810 the shell. If the shell is not interactive, then just leave.
5811 Otherwise, if ignoreeof is set, and we haven't done this the
5812 required number of times in a row, print a message. */
5814 handle_eof_input_unit ()
5818 /* shell.c may use this to decide whether or not to write out the
5819 history, among other things. We use it only for error reporting
5824 /* If the user wants to "ignore" eof, then let her do so, kind of. */
5827 if (eof_encountered < eof_encountered_limit)
5829 fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
5830 login_shell ? "logout" : "exit");
5832 /* Reset the parsing state. */
5833 last_read_token = current_token = '\n';
5834 /* Reset the prompt string to be $PS1. */
5835 prompt_string_pointer = (char **)NULL;
5841 /* In this case EOF should exit the shell. Do it now. */
5843 exit_builtin ((WORD_LIST *)NULL);
5847 /* We don't write history files, etc., for non-interactive shells. */
5852 /************************************************
5854 * STRING PARSING FUNCTIONS *
5856 ************************************************/
5858 /* It's very important that these two functions treat the characters
5859 between ( and ) identically. */
5861 static WORD_LIST parse_string_error;
5863 /* Take a string and run it through the shell parser, returning the
5864 resultant word list. Used by compound array assignment. */
5866 parse_string_to_word_list (s, flags, whom)
5872 int tok, orig_current_token, orig_line_number, orig_input_terminator;
5873 int orig_line_count;
5874 int old_echo_input, old_expand_aliases;
5875 #if defined (HISTORY)
5876 int old_remember_on_history, old_history_expansion_inhibited;
5879 #if defined (HISTORY)
5880 old_remember_on_history = remember_on_history;
5881 # if defined (BANG_HISTORY)
5882 old_history_expansion_inhibited = history_expansion_inhibited;
5884 bash_history_disable ();
5887 orig_line_number = line_number;
5888 orig_line_count = current_command_line_count;
5889 orig_input_terminator = shell_input_line_terminator;
5890 old_echo_input = echo_input_at_read;
5891 old_expand_aliases = expand_aliases;
5894 last_read_token = WORD; /* WORD to allow reserved words here */
5895 current_command_line_count = 0;
5896 echo_input_at_read = expand_aliases = 0;
5898 with_input_from_string (s, whom);
5899 wl = (WORD_LIST *)NULL;
5902 parser_state |= PST_COMPASSIGN|PST_REPARSE;
5904 while ((tok = read_token (READ)) != yacc_EOF)
5906 if (tok == '\n' && *bash_input.location.string == '\0')
5908 if (tok == '\n') /* Allow newlines in compound assignments */
5910 if (tok != WORD && tok != ASSIGNMENT_WORD)
5912 line_number = orig_line_number + line_number - 1;
5913 orig_current_token = current_token;
5914 current_token = tok;
5915 yyerror (NULL); /* does the right thing */
5916 current_token = orig_current_token;
5919 wl = &parse_string_error;
5922 wl = make_word_list (yylval.word, wl);
5925 last_read_token = '\n';
5928 #if defined (HISTORY)
5929 remember_on_history = old_remember_on_history;
5930 # if defined (BANG_HISTORY)
5931 history_expansion_inhibited = old_history_expansion_inhibited;
5932 # endif /* BANG_HISTORY */
5933 #endif /* HISTORY */
5935 echo_input_at_read = old_echo_input;
5936 expand_aliases = old_expand_aliases;
5938 current_command_line_count = orig_line_count;
5939 shell_input_line_terminator = orig_input_terminator;
5942 parser_state &= ~(PST_COMPASSIGN|PST_REPARSE);
5944 if (wl == &parse_string_error)
5946 last_command_exit_value = EXECUTION_FAILURE;
5947 if (interactive_shell == 0 && posixly_correct)
5948 jump_to_top_level (FORCE_EOF);
5950 jump_to_top_level (DISCARD);
5953 return (REVERSE_LIST (wl, WORD_LIST *));
5957 parse_compound_assignment (retlenp)
5961 int tok, orig_line_number, orig_token_size, orig_last_token, assignok;
5962 char *saved_token, *ret;
5964 saved_token = token;
5965 orig_token_size = token_buffer_size;
5966 orig_line_number = line_number;
5967 orig_last_token = last_read_token;
5969 last_read_token = WORD; /* WORD to allow reserved words here */
5971 token = (char *)NULL;
5972 token_buffer_size = 0;
5974 assignok = parser_state&PST_ASSIGNOK; /* XXX */
5976 wl = (WORD_LIST *)NULL; /* ( */
5977 parser_state |= PST_COMPASSIGN;
5979 while ((tok = read_token (READ)) != ')')
5981 if (tok == '\n') /* Allow newlines in compound assignments */
5983 if (SHOULD_PROMPT ())
5987 if (tok != WORD && tok != ASSIGNMENT_WORD)
5989 current_token = tok; /* for error reporting */
5990 if (tok == yacc_EOF) /* ( */
5991 parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'"));
5993 yyerror(NULL); /* does the right thing */
5996 wl = &parse_string_error;
5999 wl = make_word_list (yylval.word, wl);
6003 token = saved_token;
6004 token_buffer_size = orig_token_size;
6006 parser_state &= ~PST_COMPASSIGN;
6008 if (wl == &parse_string_error)
6010 last_command_exit_value = EXECUTION_FAILURE;
6011 last_read_token = '\n'; /* XXX */
6012 if (interactive_shell == 0 && posixly_correct)
6013 jump_to_top_level (FORCE_EOF);
6015 jump_to_top_level (DISCARD);
6018 last_read_token = orig_last_token; /* XXX - was WORD? */
6022 rl = REVERSE_LIST (wl, WORD_LIST *);
6023 ret = string_list (rl);
6030 *retlenp = (ret && *ret) ? strlen (ret) : 0;
6033 parser_state |= PST_ASSIGNOK;
6038 /************************************************
6040 * SAVING AND RESTORING PARTIAL PARSE STATE *
6042 ************************************************/
6045 save_parser_state (ps)
6046 sh_parser_state_t *ps;
6049 ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t));
6051 return ((sh_parser_state_t *)NULL);
6053 ps->parser_state = parser_state;
6054 ps->token_state = save_token_state ();
6056 ps->input_line_terminator = shell_input_line_terminator;
6057 ps->eof_encountered = eof_encountered;
6059 ps->prompt_string_pointer = prompt_string_pointer;
6061 ps->current_command_line_count = current_command_line_count;
6063 #if defined (HISTORY)
6064 ps->remember_on_history = remember_on_history;
6065 # if defined (BANG_HISTORY)
6066 ps->history_expansion_inhibited = history_expansion_inhibited;
6070 ps->last_command_exit_value = last_command_exit_value;
6071 #if defined (ARRAY_VARS)
6072 ps->pipestatus = save_pipestatus_array ();
6075 ps->last_shell_builtin = last_shell_builtin;
6076 ps->this_shell_builtin = this_shell_builtin;
6078 ps->expand_aliases = expand_aliases;
6079 ps->echo_input_at_read = echo_input_at_read;
6080 ps->need_here_doc = need_here_doc;
6083 ps->token_buffer_size = token_buffer_size;
6084 /* Force reallocation on next call to read_token_word */
6086 token_buffer_size = 0;
6092 restore_parser_state (ps)
6093 sh_parser_state_t *ps;
6098 parser_state = ps->parser_state;
6099 if (ps->token_state)
6101 restore_token_state (ps->token_state);
6102 free (ps->token_state);
6105 shell_input_line_terminator = ps->input_line_terminator;
6106 eof_encountered = ps->eof_encountered;
6108 prompt_string_pointer = ps->prompt_string_pointer;
6110 current_command_line_count = ps->current_command_line_count;
6112 #if defined (HISTORY)
6113 remember_on_history = ps->remember_on_history;
6114 # if defined (BANG_HISTORY)
6115 history_expansion_inhibited = ps->history_expansion_inhibited;
6119 last_command_exit_value = ps->last_command_exit_value;
6120 #if defined (ARRAY_VARS)
6121 restore_pipestatus_array (ps->pipestatus);
6124 last_shell_builtin = ps->last_shell_builtin;
6125 this_shell_builtin = ps->this_shell_builtin;
6127 expand_aliases = ps->expand_aliases;
6128 echo_input_at_read = ps->echo_input_at_read;
6129 need_here_doc = ps->need_here_doc;
6133 token_buffer_size = ps->token_buffer_size;
6136 sh_input_line_state_t *
6137 save_input_line_state (ls)
6138 sh_input_line_state_t *ls;
6141 ls = (sh_input_line_state_t *)xmalloc (sizeof (sh_input_line_state_t));
6143 return ((sh_input_line_state_t *)NULL);
6145 ls->input_line = shell_input_line;
6146 ls->input_line_size = shell_input_line_size;
6147 ls->input_line_len = shell_input_line_len;
6148 ls->input_line_index = shell_input_line_index;
6150 /* force reallocation */
6151 shell_input_line = 0;
6152 shell_input_line_size = shell_input_line_len = shell_input_line_index = 0;
6158 restore_input_line_state (ls)
6159 sh_input_line_state_t *ls;
6161 FREE (shell_input_line);
6162 shell_input_line = ls->input_line;
6163 shell_input_line_size = ls->input_line_size;
6164 shell_input_line_len = ls->input_line_len;
6165 shell_input_line_index = ls->input_line_index;
6167 set_line_mbstate ();
6170 /************************************************
6172 * MULTIBYTE CHARACTER HANDLING *
6174 ************************************************/
6176 #if defined (HANDLE_MULTIBYTE)
6181 size_t i, previ, len;
6182 mbstate_t mbs, prevs;
6185 if (shell_input_line == NULL)
6187 len = strlen (shell_input_line); /* XXX - shell_input_line_len ? */
6188 FREE (shell_input_line_property);
6189 shell_input_line_property = (char *)xmalloc (len + 1);
6191 memset (&prevs, '\0', sizeof (mbstate_t));
6192 for (i = previ = 0; i < len; i++)
6196 c = shell_input_line[i];
6200 for (j = i; j < len; j++)
6201 shell_input_line_property[j] = 1;
6205 mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs);
6206 if (mbclen == 1 || mbclen == (size_t)-1)
6211 else if (mbclen == (size_t)-2)
6213 else if (mbclen > 1)
6221 /* XXX - what to do if mbrlen returns 0? (null wide character) */
6223 for (j = i; j < len; j++)
6224 shell_input_line_property[j] = 1;
6228 shell_input_line_property[i] = mbclen;
6231 #endif /* HANDLE_MULTIBYTE */