1 /* Yacc grammar for bash. */
3 /* Copyright (C) 1989-2005 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 it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file LICENSE. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 #include "bashtypes.h"
29 #if defined (HAVE_UNISTD_H)
33 #if defined (HAVE_LOCALE_H)
38 #include "chartypes.h"
45 #define NEED_STRFTIME_DECL /* used in externs.h */
51 #include "mailcheck.h"
54 #include "builtins/common.h"
55 #include "builtins/builtext.h"
59 #if defined (READLINE)
60 # include "bashline.h"
61 # include <readline/readline.h>
65 # include "bashhist.h"
66 # include <readline/history.h>
69 #if defined (JOB_CONTROL)
71 #endif /* JOB_CONTROL */
76 typedef void *alias_t;
79 #if defined (PROMPT_STRING_DECODE)
81 # include <sys/param.h>
84 # if defined (TM_IN_SYS_TIME)
85 # include <sys/types.h>
86 # include <sys/time.h>
87 # endif /* TM_IN_SYS_TIME */
89 #endif /* PROMPT_STRING_DECODE */
91 #define RE_READ_TOKEN -99
92 #define NO_EXPANSION -100
100 #if defined (HANDLE_MULTIBYTE)
101 # define last_shell_getc_is_singlebyte \
102 ((shell_input_line_index > 1) \
103 ? shell_input_line_property[shell_input_line_index - 1] \
105 # define MBTEST(x) ((x) && last_shell_getc_is_singlebyte)
107 # define last_shell_getc_is_singlebyte 1
108 # define MBTEST(x) ((x))
111 #if defined (EXTENDED_GLOB)
112 extern int extended_glob;
115 extern int eof_encountered;
116 extern int no_line_editing, running_under_emacs;
117 extern int current_command_number;
118 extern int sourcelevel;
119 extern int posixly_correct;
120 extern int last_command_exit_value;
121 extern int interrupt_immediately;
122 extern char *shell_name, *current_host_name;
123 extern char *dist_version;
124 extern int patch_level;
125 extern int dump_translatable_strings, dump_po_strings;
126 extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
127 #if defined (BUFFERED_INPUT)
128 extern int bash_input_fd_changed;
132 /* **************************************************************** */
134 /* "Forward" declarations */
136 /* **************************************************************** */
139 static void debug_parser __P((int));
142 static int yy_getc __P((void));
143 static int yy_ungetc __P((int));
145 #if defined (READLINE)
146 static int yy_readline_get __P((void));
147 static int yy_readline_unget __P((int));
150 static int yy_string_get __P((void));
151 static int yy_string_unget __P((int));
152 static int yy_stream_get __P((void));
153 static int yy_stream_unget __P((int));
155 static int shell_getc __P((int));
156 static void shell_ungetc __P((int));
157 static void discard_until __P((int));
159 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
160 static void push_string __P((char *, int, alias_t *));
161 static void pop_string __P((void));
162 static void free_string_list __P((void));
165 static char *read_a_line __P((int));
167 static int reserved_word_acceptable __P((int));
168 static int yylex __P((void));
169 static int alias_expand_token __P((char *));
170 static int time_command_acceptable __P((void));
171 static int special_case_tokens __P((char *));
172 static int read_token __P((int));
173 static char *parse_matched_pair __P((int, int, int, int *, int));
174 #if defined (ARRAY_VARS)
175 static char *parse_compound_assignment __P((int *));
177 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
178 static int parse_dparen __P((int));
179 static int parse_arith_cmd __P((char **, int));
181 #if defined (COND_COMMAND)
182 static void cond_error __P((void));
183 static COND_COM *cond_expr __P((void));
184 static COND_COM *cond_or __P((void));
185 static COND_COM *cond_and __P((void));
186 static COND_COM *cond_term __P((void));
187 static int cond_skip_newlines __P((void));
188 static COMMAND *parse_cond_command __P((void));
190 #if defined (ARRAY_VARS)
191 static int token_is_assignment __P((char *, int));
192 static int token_is_ident __P((char *, int));
194 static int read_token_word __P((int));
195 static void discard_parser_constructs __P((int));
197 static char *error_token_from_token __P((int));
198 static char *error_token_from_text __P((void));
199 static void print_offending_line __P((void));
200 static void report_syntax_error __P((char *));
202 static void handle_eof_input_unit __P((void));
203 static void prompt_again __P((void));
205 static void reset_readline_prompt __P((void));
207 static void print_prompt __P((void));
209 #if defined (HISTORY)
210 char *history_delimiting_chars __P((void));
213 #if defined (HANDLE_MULTIBYTE)
214 static void set_line_mbstate __P((void));
215 static char *shell_input_line_property = NULL;
217 # define set_line_mbstate()
220 extern int yyerror __P((const char *));
226 /* Default prompt strings */
227 char *primary_prompt = PPROMPT;
228 char *secondary_prompt = SPROMPT;
230 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
231 char *ps1_prompt, *ps2_prompt;
233 /* Handle on the current prompt string. Indirectly points through
234 ps1_ or ps2_prompt. */
235 char **prompt_string_pointer = (char **)NULL;
236 char *current_prompt_string;
238 /* Non-zero means we expand aliases in commands. */
239 int expand_aliases = 0;
241 /* If non-zero, the decoded prompt string undergoes parameter and
242 variable substitution, command substitution, arithmetic substitution,
243 string expansion, process substitution, and quote removal in
244 decode_prompt_string. */
247 /* If non-zero, $'...' and $"..." are expanded when they appear within
248 a ${...} expansion, even when the expansion appears within double
250 int extended_quote = 1;
252 /* The decoded prompt string. Used if READLINE is not defined or if
253 editing is turned off. Analogous to current_readline_prompt. */
254 static char *current_decoded_prompt;
256 /* The number of lines read from input while creating the current command. */
257 int current_command_line_count;
259 /* Variables to manage the task of reading here documents, because we need to
260 defer the reading until after a complete command has been collected. */
261 static REDIRECT *redir_stack[10];
264 /* Where shell input comes from. History expansion is performed on each
265 line when the shell is interactive. */
266 static char *shell_input_line = (char *)NULL;
267 static int shell_input_line_index;
268 static int shell_input_line_size; /* Amount allocated for shell_input_line. */
269 static int shell_input_line_len; /* strlen (shell_input_line) */
271 /* Either zero or EOF. */
272 static int shell_input_line_terminator;
274 /* The line number in a script on which a function definition starts. */
275 static int function_dstart;
277 /* The line number in a script on which a function body starts. */
278 static int function_bstart;
280 /* The line number in a script at which an arithmetic for command starts. */
281 static int arith_for_lineno;
283 /* The line number in a script where the word in a `case WORD', `select WORD'
284 or `for WORD' begins. This is a nested command maximum, since the array
285 index is decremented after a case, select, or for command is parsed. */
286 #define MAX_CASE_NEST 128
287 static int word_lineno[MAX_CASE_NEST];
288 static int word_top = -1;
290 /* If non-zero, it is the token that we want read_token to return
291 regardless of what text is (or isn't) present to be read. This
292 is reset by read_token. If token_to_read == WORD or
293 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
294 static int token_to_read;
295 static WORD_DESC *word_desc_to_read;
297 static REDIRECTEE redir;
301 WORD_DESC *word; /* the word that we read. */
302 int number; /* the number that we read. */
303 WORD_LIST *word_list;
307 PATTERN_LIST *pattern;
310 /* Reserved words. Members of the first group are only recognized
311 in the case that they are preceded by a list_terminator. Members
312 of the second group are for [[...]] commands. Members of the
313 third group are recognized only under special circumstances. */
314 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION
315 %token COND_START COND_END COND_ERROR
316 %token IN BANG TIME TIMEOPT
318 /* More general tokens. yylex () knows how to make these. */
319 %token <word> WORD ASSIGNMENT_WORD
320 %token <number> NUMBER
321 %token <word_list> ARITH_CMD ARITH_FOR_EXPRS
322 %token <command> COND_CMD
323 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS
324 %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
327 /* The types that the various syntactical units return. */
329 %type <command> inputunit command pipeline pipeline_command
330 %type <command> list list0 list1 compound_list simple_list simple_list1
331 %type <command> simple_command shell_command
332 %type <command> for_command select_command case_command group_command
333 %type <command> arith_command
334 %type <command> cond_command
335 %type <command> arith_for_command
336 %type <command> function_def function_body if_command elif_clause subshell
337 %type <redirect> redirection redirection_list
338 %type <element> simple_command_element
339 %type <word_list> word_list pattern
340 %type <pattern> pattern_list case_clause_sequence case_clause
341 %type <number> timespec
342 %type <number> list_terminator
346 %left '&' ';' '\n' yacc_EOF
351 inputunit: simple_list simple_list_terminator
353 /* Case of regular command. Discard the error
354 safety net,and return the command just parsed. */
357 /* discard_parser_constructs (0); */
362 /* Case of regular command, but not a very
363 interesting one. Return a NULL command. */
364 global_command = (COMMAND *)NULL;
369 /* Error during parsing. Return NULL command. */
370 global_command = (COMMAND *)NULL;
372 /* discard_parser_constructs (1); */
384 /* Case of EOF seen by itself. Do ignoreeof or
386 global_command = (COMMAND *)NULL;
387 handle_eof_input_unit ();
393 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
395 { $$ = make_word_list ($2, $1); }
398 redirection: '>' WORD
401 $$ = make_redirection (1, r_output_direction, redir);
406 $$ = make_redirection (0, r_input_direction, redir);
411 $$ = make_redirection ($1, r_output_direction, redir);
416 $$ = make_redirection ($1, r_input_direction, redir);
418 | GREATER_GREATER WORD
421 $$ = make_redirection (1, r_appending_to, redir);
423 | NUMBER GREATER_GREATER WORD
426 $$ = make_redirection ($1, r_appending_to, redir);
431 $$ = make_redirection (0, r_reading_until, redir);
432 redir_stack[need_here_doc++] = $$;
434 | NUMBER LESS_LESS WORD
437 $$ = make_redirection ($1, r_reading_until, redir);
438 redir_stack[need_here_doc++] = $$;
440 | LESS_LESS_LESS WORD
443 $$ = make_redirection (0, r_reading_string, redir);
445 | NUMBER LESS_LESS_LESS WORD
448 $$ = make_redirection ($1, r_reading_string, redir);
453 $$ = make_redirection (0, r_duplicating_input, redir);
455 | NUMBER LESS_AND NUMBER
458 $$ = make_redirection ($1, r_duplicating_input, redir);
463 $$ = make_redirection (1, r_duplicating_output, redir);
465 | NUMBER GREATER_AND NUMBER
468 $$ = make_redirection ($1, r_duplicating_output, redir);
473 $$ = make_redirection (0, r_duplicating_input_word, redir);
475 | NUMBER LESS_AND WORD
478 $$ = make_redirection ($1, r_duplicating_input_word, redir);
483 $$ = make_redirection (1, r_duplicating_output_word, redir);
485 | NUMBER GREATER_AND WORD
488 $$ = make_redirection ($1, r_duplicating_output_word, redir);
490 | LESS_LESS_MINUS WORD
493 $$ = make_redirection
494 (0, r_deblank_reading_until, redir);
495 redir_stack[need_here_doc++] = $$;
497 | NUMBER LESS_LESS_MINUS WORD
500 $$ = make_redirection
501 ($1, r_deblank_reading_until, redir);
502 redir_stack[need_here_doc++] = $$;
507 $$ = make_redirection (1, r_close_this, redir);
509 | NUMBER GREATER_AND '-'
512 $$ = make_redirection ($1, r_close_this, redir);
517 $$ = make_redirection (0, r_close_this, redir);
519 | NUMBER LESS_AND '-'
522 $$ = make_redirection ($1, r_close_this, redir);
527 $$ = make_redirection (1, r_err_and_out, redir);
529 | NUMBER LESS_GREATER WORD
532 $$ = make_redirection ($1, r_input_output, redir);
537 $$ = make_redirection (0, r_input_output, redir);
542 $$ = make_redirection (1, r_output_force, redir);
544 | NUMBER GREATER_BAR WORD
547 $$ = make_redirection ($1, r_output_force, redir);
551 simple_command_element: WORD
552 { $$.word = $1; $$.redirect = 0; }
554 { $$.word = $1; $$.redirect = 0; }
556 { $$.redirect = $1; $$.word = 0; }
559 redirection_list: redirection
563 | redirection_list redirection
565 register REDIRECT *t;
567 for (t = $1; t->next; t = t->next)
574 simple_command: simple_command_element
575 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
576 | simple_command simple_command_element
577 { $$ = make_simple_command ($2, $1); }
580 command: simple_command
581 { $$ = clean_simple_command ($1); }
584 | shell_command redirection_list
591 register REDIRECT *t;
592 for (t = tc->redirects; t->next; t = t->next)
604 shell_command: for_command
608 | WHILE compound_list DO compound_list DONE
609 { $$ = make_while_command ($2, $4); }
610 | UNTIL compound_list DO compound_list DONE
611 { $$ = make_until_command ($2, $4); }
628 for_command: FOR WORD newline_list DO compound_list DONE
630 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
631 if (word_top > 0) word_top--;
633 | FOR WORD newline_list '{' compound_list '}'
635 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
636 if (word_top > 0) word_top--;
638 | FOR WORD ';' newline_list DO compound_list DONE
640 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
641 if (word_top > 0) word_top--;
643 | FOR WORD ';' newline_list '{' compound_list '}'
645 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
646 if (word_top > 0) word_top--;
648 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
650 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
651 if (word_top > 0) word_top--;
653 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
655 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
656 if (word_top > 0) word_top--;
658 | FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
660 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
661 if (word_top > 0) word_top--;
663 | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
665 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
666 if (word_top > 0) word_top--;
670 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
672 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
673 if (word_top > 0) word_top--;
675 | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
677 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
678 if (word_top > 0) word_top--;
680 | FOR ARITH_FOR_EXPRS DO compound_list DONE
682 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
683 if (word_top > 0) word_top--;
685 | FOR ARITH_FOR_EXPRS '{' compound_list '}'
687 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
688 if (word_top > 0) word_top--;
692 select_command: SELECT WORD newline_list DO list DONE
694 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
695 if (word_top > 0) word_top--;
697 | SELECT WORD newline_list '{' list '}'
699 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
700 if (word_top > 0) word_top--;
702 | SELECT WORD ';' newline_list DO list DONE
704 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
705 if (word_top > 0) word_top--;
707 | SELECT WORD ';' newline_list '{' list '}'
709 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
710 if (word_top > 0) word_top--;
712 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
714 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
715 if (word_top > 0) word_top--;
717 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
719 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
720 if (word_top > 0) word_top--;
724 case_command: CASE WORD newline_list IN newline_list ESAC
726 $$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]);
727 if (word_top > 0) word_top--;
729 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
731 $$ = make_case_command ($2, $5, word_lineno[word_top]);
732 if (word_top > 0) word_top--;
734 | CASE WORD newline_list IN case_clause ESAC
736 $$ = make_case_command ($2, $5, word_lineno[word_top]);
737 if (word_top > 0) word_top--;
741 function_def: WORD '(' ')' newline_list function_body
742 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
744 | FUNCTION WORD '(' ')' newline_list function_body
745 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
747 | FUNCTION WORD newline_list function_body
748 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
752 function_body: shell_command
754 | shell_command redirection_list
759 /* According to Posix.2 3.9.5, redirections
760 specified after the body of a function should
761 be attached to the function and performed when
762 the function is executed, not as part of the
763 function definition command. */
764 /* XXX - I don't think it matters, but we might
765 want to change this in the future to avoid
766 problems differentiating between a function
767 definition with a redirection and a function
768 definition containing a single command with a
769 redirection. The two are semantically equivalent,
770 though -- the only difference is in how the
771 command printing code displays the redirections. */
774 register REDIRECT *t;
775 for (t = tc->redirects; t->next; t = t->next)
785 subshell: '(' compound_list ')'
787 $$ = make_subshell_command ($2);
788 $$->flags |= CMD_WANT_SUBSHELL;
792 if_command: IF compound_list THEN compound_list FI
793 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
794 | IF compound_list THEN compound_list ELSE compound_list FI
795 { $$ = make_if_command ($2, $4, $6); }
796 | IF compound_list THEN compound_list elif_clause FI
797 { $$ = make_if_command ($2, $4, $5); }
801 group_command: '{' compound_list '}'
802 { $$ = make_group_command ($2); }
805 arith_command: ARITH_CMD
806 { $$ = make_arith_command ($1); }
809 cond_command: COND_START COND_CMD COND_END
813 elif_clause: ELIF compound_list THEN compound_list
814 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
815 | ELIF compound_list THEN compound_list ELSE compound_list
816 { $$ = make_if_command ($2, $4, $6); }
817 | ELIF compound_list THEN compound_list elif_clause
818 { $$ = make_if_command ($2, $4, $5); }
821 case_clause: pattern_list
822 | case_clause_sequence pattern_list
823 { $2->next = $1; $$ = $2; }
826 pattern_list: newline_list pattern ')' compound_list
827 { $$ = make_pattern_list ($2, $4); }
828 | newline_list pattern ')' newline_list
829 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
830 | newline_list '(' pattern ')' compound_list
831 { $$ = make_pattern_list ($3, $5); }
832 | newline_list '(' pattern ')' newline_list
833 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
836 case_clause_sequence: pattern_list SEMI_SEMI
837 | case_clause_sequence pattern_list SEMI_SEMI
838 { $2->next = $1; $$ = $2; }
842 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
844 { $$ = make_word_list ($3, $1); }
847 /* A list allows leading or trailing newlines and
848 newlines as operators (equivalent to semicolons).
849 It must end with a newline or semicolon.
850 Lists are used within commands such as if, for, while. */
852 list: newline_list list0
856 gather_here_documents ();
867 list0: list1 '\n' newline_list
868 | list1 '&' newline_list
870 if ($1->type == cm_connection)
871 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
873 $$ = command_connect ($1, (COMMAND *)NULL, '&');
875 | list1 ';' newline_list
879 list1: list1 AND_AND newline_list list1
880 { $$ = command_connect ($1, $4, AND_AND); }
881 | list1 OR_OR newline_list list1
882 { $$ = command_connect ($1, $4, OR_OR); }
883 | list1 '&' newline_list list1
885 if ($1->type == cm_connection)
886 $$ = connect_async_list ($1, $4, '&');
888 $$ = command_connect ($1, $4, '&');
890 | list1 ';' newline_list list1
891 { $$ = command_connect ($1, $4, ';'); }
892 | list1 '\n' newline_list list1
893 { $$ = command_connect ($1, $4, ';'); }
898 simple_list_terminator: '\n'
914 /* A simple_list is a list that contains no significant newlines
915 and no leading or trailing newlines. Newlines are allowed
916 only following operators, where they are not significant.
918 This is what an inputunit consists of. */
920 simple_list: simple_list1
924 gather_here_documents ();
928 if ($1->type == cm_connection)
929 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
931 $$ = command_connect ($1, (COMMAND *)NULL, '&');
933 gather_here_documents ();
939 gather_here_documents ();
943 simple_list1: simple_list1 AND_AND newline_list simple_list1
944 { $$ = command_connect ($1, $4, AND_AND); }
945 | simple_list1 OR_OR newline_list simple_list1
946 { $$ = command_connect ($1, $4, OR_OR); }
947 | simple_list1 '&' simple_list1
949 if ($1->type == cm_connection)
950 $$ = connect_async_list ($1, $3, '&');
952 $$ = command_connect ($1, $3, '&');
954 | simple_list1 ';' simple_list1
955 { $$ = command_connect ($1, $3, ';'); }
961 pipeline_command: pipeline
966 $2->flags |= CMD_INVERT_RETURN;
975 | timespec BANG pipeline
978 $3->flags |= $1|CMD_INVERT_RETURN;
981 | BANG timespec pipeline
984 $3->flags |= $2|CMD_INVERT_RETURN;
987 | timespec list_terminator
991 /* Boy, this is unclean. `time' by itself can
992 time a null command. We cheat and push a
993 newline back if the list_terminator was a newline
994 to avoid the double-newline problem (one to
995 terminate this, one to terminate the command) */
998 $$ = make_simple_command (x, (COMMAND *)NULL);
1000 /* XXX - let's cheat and push a newline back */
1002 token_to_read = '\n';
1008 pipeline '|' newline_list pipeline
1009 { $$ = command_connect ($1, $4, '|'); }
1015 { $$ = CMD_TIME_PIPELINE; }
1017 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1021 /* Possible states for the parser that require it to do special things. */
1022 #define PST_CASEPAT 0x0001 /* in a case pattern list */
1023 #define PST_ALEXPNEXT 0x0002 /* expand next word for aliases */
1024 #define PST_ALLOWOPNBRC 0x0004 /* allow open brace for function def */
1025 #define PST_NEEDCLOSBRC 0x0008 /* need close brace */
1026 #define PST_DBLPAREN 0x0010 /* double-paren parsing */
1027 #define PST_SUBSHELL 0x0020 /* ( ... ) subshell */
1028 #define PST_CMDSUBST 0x0040 /* $( ... ) command substitution */
1029 #define PST_CASESTMT 0x0080 /* parsing a case statement */
1030 #define PST_CONDCMD 0x0100 /* parsing a [[...]] command */
1031 #define PST_CONDEXPR 0x0200 /* parsing the guts of [[...]] */
1032 #define PST_ARITHFOR 0x0400 /* parsing an arithmetic for command */
1033 #define PST_ALEXPAND 0x0800 /* OK to expand aliases - unused */
1034 #define PST_CMDTOKEN 0x1000 /* command token OK - unused */
1035 #define PST_COMPASSIGN 0x2000 /* parsing x=(...) compound assignment */
1036 #define PST_ASSIGNOK 0x4000 /* assignment statement ok in this context */
1038 /* Initial size to allocate for tokens, and the
1039 amount to grow them by. */
1040 #define TOKEN_DEFAULT_INITIAL_SIZE 496
1041 #define TOKEN_DEFAULT_GROW_SIZE 512
1043 /* Should we call prompt_again? */
1044 #define SHOULD_PROMPT() \
1045 (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
1048 # define expanding_alias() (pushed_string_list && pushed_string_list->expander)
1050 # define expanding_alias() 0
1053 /* The token currently being read. */
1054 static int current_token;
1056 /* The last read token, or NULL. read_token () uses this for context
1058 static int last_read_token;
1060 /* The token read prior to last_read_token. */
1061 static int token_before_that;
1063 /* The token read prior to token_before_that. */
1064 static int two_tokens_ago;
1066 /* The current parser state. */
1067 static int parser_state;
1069 /* Global var is non-zero when end of file has been reached. */
1070 int EOF_Reached = 0;
1083 /* yy_getc () returns the next available character from input or EOF.
1084 yy_ungetc (c) makes `c' the next character to read.
1085 init_yy_io (get, unget, type, location) makes the function GET the
1086 installed function for getting the next character, makes UNGET the
1087 installed function for un-getting a character, sets the type of stream
1088 (either string or file) from TYPE, and makes LOCATION point to where
1089 the input is coming from. */
1091 /* Unconditionally returns end-of-file. */
1098 /* Variable containing the current get and unget functions.
1099 See ./input.h for a clearer description. */
1100 BASH_INPUT bash_input;
1102 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
1103 is non-null, avoiding a memory leak. */
1105 initialize_bash_input ()
1107 bash_input.type = st_none;
1108 FREE (bash_input.name);
1109 bash_input.name = (char *)NULL;
1110 bash_input.location.file = (FILE *)NULL;
1111 bash_input.location.string = (char *)NULL;
1112 bash_input.getter = (sh_cget_func_t *)NULL;
1113 bash_input.ungetter = (sh_cunget_func_t *)NULL;
1116 /* Set the contents of the current bash input stream from
1117 GET, UNGET, TYPE, NAME, and LOCATION. */
1119 init_yy_io (get, unget, type, name, location)
1120 sh_cget_func_t *get;
1121 sh_cunget_func_t *unget;
1122 enum stream_type type;
1124 INPUT_STREAM location;
1126 bash_input.type = type;
1127 FREE (bash_input.name);
1128 bash_input.name = name ? savestring (name) : (char *)NULL;
1132 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
1134 bash_input.location = location;
1136 bash_input.getter = get;
1137 bash_input.ungetter = unget;
1143 return (bash_input.name ? bash_input.name : "stdin");
1146 /* Call this to get the next character of input. */
1150 return (*(bash_input.getter)) ();
1153 /* Call this to unget C. That is, to make C the next character
1159 return (*(bash_input.ungetter)) (c);
1162 #if defined (BUFFERED_INPUT)
1163 #ifdef INCLUDE_UNUSED
1165 input_file_descriptor ()
1167 switch (bash_input.type)
1170 return (fileno (bash_input.location.file));
1172 return (bash_input.location.buffered_fd);
1175 return (fileno (stdin));
1179 #endif /* BUFFERED_INPUT */
1181 /* **************************************************************** */
1183 /* Let input be read from readline (). */
1185 /* **************************************************************** */
1187 #if defined (READLINE)
1188 char *current_readline_prompt = (char *)NULL;
1189 char *current_readline_line = (char *)NULL;
1190 int current_readline_line_index = 0;
1195 SigHandler *old_sigint;
1199 if (!current_readline_line)
1201 if (!bash_readline_initialized)
1202 initialize_readline ();
1204 #if defined (JOB_CONTROL)
1206 give_terminal_to (shell_pgrp, 0);
1207 #endif /* JOB_CONTROL */
1209 old_sigint = (SigHandler *)NULL;
1210 if (signal_is_ignored (SIGINT) == 0)
1212 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
1213 interrupt_immediately++;
1216 current_readline_line = readline (current_readline_prompt ?
1217 current_readline_prompt : "");
1219 if (signal_is_ignored (SIGINT) == 0 && old_sigint)
1221 interrupt_immediately--;
1222 set_signal_handler (SIGINT, old_sigint);
1226 /* Reset the prompt to the decoded value of prompt_string_pointer. */
1227 reset_readline_prompt ();
1230 if (current_readline_line == 0)
1233 current_readline_line_index = 0;
1234 line_len = strlen (current_readline_line);
1236 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
1237 current_readline_line[line_len++] = '\n';
1238 current_readline_line[line_len] = '\0';
1241 if (current_readline_line[current_readline_line_index] == 0)
1243 free (current_readline_line);
1244 current_readline_line = (char *)NULL;
1245 return (yy_readline_get ());
1249 c = current_readline_line[current_readline_line_index++];
1255 yy_readline_unget (c)
1258 if (current_readline_line_index && current_readline_line)
1259 current_readline_line[--current_readline_line_index] = c;
1264 with_input_from_stdin ()
1266 INPUT_STREAM location;
1268 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1270 location.string = current_readline_line;
1271 init_yy_io (yy_readline_get, yy_readline_unget,
1272 st_stdin, "readline stdin", location);
1276 #else /* !READLINE */
1279 with_input_from_stdin ()
1281 with_input_from_stream (stdin, "stdin");
1283 #endif /* !READLINE */
1285 /* **************************************************************** */
1287 /* Let input come from STRING. STRING is zero terminated. */
1289 /* **************************************************************** */
1294 register char *string;
1295 register unsigned char c;
1297 string = bash_input.location.string;
1299 /* If the string doesn't exist, or is empty, EOF found. */
1300 if (string && *string)
1303 bash_input.location.string = string;
1314 *(--bash_input.location.string) = c;
1319 with_input_from_string (string, name)
1323 INPUT_STREAM location;
1325 location.string = string;
1326 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1329 /* **************************************************************** */
1331 /* Let input come from STREAM. */
1333 /* **************************************************************** */
1335 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1336 define, and just use getc/ungetc if it was defined, but since bash
1337 installs its signal handlers without the SA_RESTART flag, some signals
1338 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
1339 the read to be restarted. We need to restart it ourselves. */
1347 if (bash_input.location.file)
1350 interrupt_immediately++;
1351 result = getc_with_restart (bash_input.location.file);
1353 interrupt_immediately--;
1362 return (ungetc_with_restart (c, bash_input.location.file));
1366 with_input_from_stream (stream, name)
1370 INPUT_STREAM location;
1372 location.file = stream;
1373 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1376 typedef struct stream_saver {
1377 struct stream_saver *next;
1378 BASH_INPUT bash_input;
1380 #if defined (BUFFERED_INPUT)
1381 BUFFERED_STREAM *bstream;
1382 #endif /* BUFFERED_INPUT */
1385 /* The globally known line number. */
1386 int line_number = 0;
1388 #if defined (COND_COMMAND)
1389 static int cond_lineno;
1390 static int cond_token;
1393 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1396 push_stream (reset_lineno)
1399 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1401 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1403 #if defined (BUFFERED_INPUT)
1404 saver->bstream = (BUFFERED_STREAM *)NULL;
1405 /* If we have a buffered stream, clear out buffers[fd]. */
1406 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1407 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1408 (BUFFERED_STREAM *)NULL);
1409 #endif /* BUFFERED_INPUT */
1411 saver->line = line_number;
1412 bash_input.name = (char *)NULL;
1413 saver->next = stream_list;
1414 stream_list = saver;
1427 STREAM_SAVER *saver = stream_list;
1430 stream_list = stream_list->next;
1432 init_yy_io (saver->bash_input.getter,
1433 saver->bash_input.ungetter,
1434 saver->bash_input.type,
1435 saver->bash_input.name,
1436 saver->bash_input.location);
1438 #if defined (BUFFERED_INPUT)
1439 /* If we have a buffered stream, restore buffers[fd]. */
1440 /* If the input file descriptor was changed while this was on the
1441 save stack, update the buffered fd to the new file descriptor and
1442 re-establish the buffer <-> bash_input fd correspondence. */
1443 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1445 if (bash_input_fd_changed)
1447 bash_input_fd_changed = 0;
1448 if (default_buffered_input >= 0)
1450 bash_input.location.buffered_fd = default_buffered_input;
1451 saver->bstream->b_fd = default_buffered_input;
1452 SET_CLOSE_ON_EXEC (default_buffered_input);
1455 /* XXX could free buffered stream returned as result here. */
1456 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1458 #endif /* BUFFERED_INPUT */
1460 line_number = saver->line;
1462 FREE (saver->bash_input.name);
1467 /* Return 1 if a stream of type TYPE is saved on the stack. */
1469 stream_on_stack (type)
1470 enum stream_type type;
1472 register STREAM_SAVER *s;
1474 for (s = stream_list; s; s = s->next)
1475 if (s->bash_input.type == type)
1480 /* Save the current token state and return it in a malloced array. */
1486 ret = (int *)xmalloc (3 * sizeof (int));
1487 ret[0] = last_read_token;
1488 ret[1] = token_before_that;
1489 ret[2] = two_tokens_ago;
1494 restore_token_state (ts)
1499 last_read_token = ts[0];
1500 token_before_that = ts[1];
1501 two_tokens_ago = ts[2];
1505 * This is used to inhibit alias expansion and reserved word recognition
1506 * inside case statement pattern lists. A `case statement pattern list' is:
1508 * everything between the `in' in a `case word in' and the next ')'
1510 * everything between a `;;' and the next `)' or `esac'
1513 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1515 #define END_OF_ALIAS 0
1518 * Pseudo-global variables used in implementing token-wise alias expansion.
1522 * Pushing and popping strings. This works together with shell_getc to
1523 * implement alias expansion on a per-token basis.
1526 typedef struct string_saver {
1527 struct string_saver *next;
1528 int expand_alias; /* Value to set expand_alias to when string is popped. */
1531 alias_t *expander; /* alias that caused this line to be pushed. */
1533 int saved_line_size, saved_line_index, saved_line_terminator;
1536 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1539 * Push the current shell_input_line onto a stack of such lines and make S
1540 * the current input. Used when expanding aliases. EXPAND is used to set
1541 * the value of expand_next_token when the string is popped, so that the
1542 * word after the alias in the original line is handled correctly when the
1543 * alias expands to multiple words. TOKEN is the token that was expanded
1544 * into S; it is saved and used to prevent infinite recursive expansion.
1547 push_string (s, expand, ap)
1552 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
1554 temp->expand_alias = expand;
1555 temp->saved_line = shell_input_line;
1556 temp->saved_line_size = shell_input_line_size;
1557 temp->saved_line_index = shell_input_line_index;
1558 temp->saved_line_terminator = shell_input_line_terminator;
1560 temp->expander = ap;
1562 temp->next = pushed_string_list;
1563 pushed_string_list = temp;
1567 ap->flags |= AL_BEINGEXPANDED;
1570 shell_input_line = s;
1571 shell_input_line_size = strlen (s);
1572 shell_input_line_index = 0;
1573 shell_input_line_terminator = '\0';
1575 parser_state &= ~PST_ALEXPNEXT; /* XXX */
1578 set_line_mbstate ();
1582 * Make the top of the pushed_string stack be the current shell input.
1583 * Only called when there is something on the stack. Called from shell_getc
1584 * when it thinks it has consumed the string generated by an alias expansion
1585 * and needs to return to the original input line.
1592 FREE (shell_input_line);
1593 shell_input_line = pushed_string_list->saved_line;
1594 shell_input_line_index = pushed_string_list->saved_line_index;
1595 shell_input_line_size = pushed_string_list->saved_line_size;
1596 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1598 if (pushed_string_list->expand_alias)
1599 parser_state |= PST_ALEXPNEXT;
1601 parser_state &= ~PST_ALEXPNEXT;
1603 t = pushed_string_list;
1604 pushed_string_list = pushed_string_list->next;
1608 t->expander->flags &= ~AL_BEINGEXPANDED;
1613 set_line_mbstate ();
1619 register STRING_SAVER *t, *t1;
1621 for (t = pushed_string_list; t; )
1624 FREE (t->saved_line);
1627 t->expander->flags &= ~AL_BEINGEXPANDED;
1632 pushed_string_list = (STRING_SAVER *)NULL;
1635 #endif /* ALIAS || DPAREN_ARITHMETIC */
1638 free_pushed_string_input ()
1640 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1641 free_string_list ();
1645 /* Return a line of text, taken from wherever yylex () reads input.
1646 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1647 is non-zero, we remove unquoted \<newline> pairs. This is used by
1648 read_secondary_line to read here documents. */
1650 read_a_line (remove_quoted_newline)
1651 int remove_quoted_newline;
1653 static char *line_buffer = (char *)NULL;
1654 static int buffer_size = 0;
1655 int indx = 0, c, peekc, pass_next;
1657 #if defined (READLINE)
1658 if (no_line_editing && SHOULD_PROMPT ())
1660 if (SHOULD_PROMPT ())
1667 /* Allow immediate exit if interrupted during input. */
1672 /* Ignore null bytes in input. */
1676 internal_warning ("read_a_line: ignored null byte in input");
1681 /* If there is no more input, then we return NULL. */
1684 if (interactive && bash_input.type == st_stream)
1687 return ((char *)NULL);
1691 /* `+2' in case the final character in the buffer is a newline. */
1692 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
1694 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
1695 here document with an unquoted delimiter. In this case,
1696 the line will be expanded as if it were in double quotes.
1697 We allow a backslash to escape the next character, but we
1698 need to treat the backslash specially only if a backslash
1699 quoting a backslash-newline pair appears in the line. */
1702 line_buffer[indx++] = c;
1705 else if (c == '\\' && remove_quoted_newline)
1711 continue; /* Make the unquoted \<newline> pair disappear. */
1717 line_buffer[indx++] = c; /* Preserve the backslash. */
1721 line_buffer[indx++] = c;
1725 line_buffer[indx] = '\0';
1726 return (line_buffer);
1731 /* Return a line as in read_a_line (), but insure that the prompt is
1732 the secondary prompt. This is used to read the lines of a here
1733 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
1734 newlines quoted with backslashes while reading the line. It is
1735 non-zero unless the delimiter of the here document was quoted. */
1737 read_secondary_line (remove_quoted_newline)
1738 int remove_quoted_newline;
1740 prompt_string_pointer = &ps2_prompt;
1741 if (SHOULD_PROMPT())
1743 return (read_a_line (remove_quoted_newline));
1746 /* **************************************************************** */
1750 /* **************************************************************** */
1752 /* Reserved words. These are only recognized as the first word of a
1754 STRING_INT_ALIST word_token_alist[] = {
1763 #if defined (SELECT_COMMAND)
1764 { "select", SELECT },
1771 { "function", FUNCTION },
1772 #if defined (COMMAND_TIMING)
1778 #if defined (COND_COMMAND)
1779 { "[[", COND_START },
1785 /* other tokens that can be returned by read_token() */
1786 STRING_INT_ALIST other_token_alist[] = {
1787 /* Multiple-character tokens with special values */
1791 { ">>", GREATER_GREATER },
1792 { "<<", LESS_LESS },
1794 { ">&", GREATER_AND },
1795 { ";;", SEMI_SEMI },
1796 { "<<-", LESS_LESS_MINUS },
1797 { "<<<", LESS_LESS_LESS },
1798 { "&>", AND_GREATER },
1799 { "<>", LESS_GREATER },
1800 { ">|", GREATER_BAR },
1801 { "EOF", yacc_EOF },
1802 /* Tokens whose value is the character itself */
1813 { "newline", '\n' },
1817 /* others not listed here:
1818 WORD look at yylval.word
1819 ASSIGNMENT_WORD look at yylval.word
1820 NUMBER look at yylval.number
1821 ARITH_CMD look at yylval.word_list
1822 ARITH_FOR_EXPRS look at yylval.word_list
1823 COND_CMD look at yylval.command
1826 /* These are used by read_token_word, but appear up here so that shell_getc
1827 can use them to decide when to add otherwise blank lines to the history. */
1829 /* The primary delimiter stack. */
1830 struct dstack dstack = { (char *)NULL, 0, 0 };
1832 /* A temporary delimiter stack to be used when decoding prompt strings.
1833 This is needed because command substitutions in prompt strings (e.g., PS2)
1834 can screw up the parser's quoting state. */
1835 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
1837 /* Macro for accessing the top delimiter on the stack. Returns the
1838 delimiter or zero if none. */
1839 #define current_delimiter(ds) \
1840 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
1842 #define push_delimiter(ds, character) \
1845 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
1846 ds.delimiters = (char *)xrealloc \
1847 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
1848 ds.delimiters[ds.delimiter_depth] = character; \
1849 ds.delimiter_depth++; \
1853 #define pop_delimiter(ds) ds.delimiter_depth--
1855 /* Return the next shell input character. This always reads characters
1856 from shell_input_line; when that line is exhausted, it is time to
1857 read the next line. This is called by read_token when the shell is
1858 processing normal command input. */
1860 /* This implements one-character lookahead/lookbehind across physical input
1861 lines, to avoid something being lost because it's pushed back with
1862 shell_ungetc when we're at the start of a line. */
1863 static int eol_ungetc_lookahead = 0;
1866 shell_getc (remove_quoted_newline)
1867 int remove_quoted_newline;
1872 static int mustpop = 0;
1876 if (eol_ungetc_lookahead)
1878 c = eol_ungetc_lookahead;
1879 eol_ungetc_lookahead = 0;
1883 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1884 /* If shell_input_line[shell_input_line_index] == 0, but there is
1885 something on the pushed list of strings, then we don't want to go
1886 off and get another line. We let the code down below handle it. */
1888 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
1889 (pushed_string_list == (STRING_SAVER *)NULL)))
1890 #else /* !ALIAS && !DPAREN_ARITHMETIC */
1891 if (!shell_input_line || !shell_input_line[shell_input_line_index])
1892 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
1898 /* Allow immediate exit if interrupted during input. */
1902 shell_input_line_terminator = 0;
1904 /* If the shell is interatctive, but not currently printing a prompt
1905 (interactive_shell && interactive == 0), we don't want to print
1906 notifies or cleanup the jobs -- we want to defer it until we do
1907 print the next prompt. */
1908 if (interactive_shell == 0 || SHOULD_PROMPT())
1910 #if defined (JOB_CONTROL)
1911 /* This can cause a problem when reading a command as the result
1912 of a trap, when the trap is called from flush_child. This call
1913 had better not cause jobs to disappear from the job table in
1914 that case, or we will have big trouble. */
1915 notify_and_cleanup ();
1916 #else /* !JOB_CONTROL */
1917 cleanup_dead_jobs ();
1918 #endif /* !JOB_CONTROL */
1921 #if defined (READLINE)
1922 if (no_line_editing && SHOULD_PROMPT())
1924 if (SHOULD_PROMPT())
1928 if (bash_input.type == st_stream)
1935 /* Allow immediate exit if interrupted during input. */
1941 internal_warning ("shell_getc: ignored null byte in input");
1946 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
1950 if (bash_input.type == st_stream)
1954 shell_input_line_terminator = EOF;
1956 shell_input_line[i] = '\0';
1960 shell_input_line[i++] = c;
1964 shell_input_line[--i] = '\0';
1965 current_command_line_count++;
1970 shell_input_line_index = 0;
1971 shell_input_line_len = i; /* == strlen (shell_input_line) */
1973 set_line_mbstate ();
1975 #if defined (HISTORY)
1976 if (remember_on_history && shell_input_line && shell_input_line[0])
1979 # if defined (BANG_HISTORY)
1982 /* If the current delimiter is a single quote, we should not be
1983 performing history expansion, even if we're on a different
1984 line from the original single quote. */
1985 old_hist = history_expansion_inhibited;
1986 if (current_delimiter (dstack) == '\'')
1987 history_expansion_inhibited = 1;
1989 expansions = pre_process_line (shell_input_line, 1, 1);
1990 # if defined (BANG_HISTORY)
1991 history_expansion_inhibited = old_hist;
1993 if (expansions != shell_input_line)
1995 free (shell_input_line);
1996 shell_input_line = expansions;
1997 shell_input_line_len = shell_input_line ?
1998 strlen (shell_input_line) : 0;
1999 if (!shell_input_line_len)
2000 current_command_line_count--;
2002 /* We have to force the xrealloc below because we don't know
2003 the true allocated size of shell_input_line anymore. */
2004 shell_input_line_size = shell_input_line_len;
2006 set_line_mbstate ();
2009 /* Try to do something intelligent with blank lines encountered while
2010 entering multi-line commands. XXX - this is grotesque */
2011 else if (remember_on_history && shell_input_line &&
2012 shell_input_line[0] == '\0' &&
2013 current_command_line_count > 1)
2015 if (current_delimiter (dstack))
2016 /* We know shell_input_line[0] == 0 and we're reading some sort of
2017 quoted string. This means we've got a line consisting of only
2018 a newline in a quoted string. We want to make sure this line
2019 gets added to the history. */
2020 maybe_add_history (shell_input_line);
2024 hdcs = history_delimiting_chars ();
2025 if (hdcs && hdcs[0] == ';')
2026 maybe_add_history (shell_input_line);
2030 #endif /* HISTORY */
2032 if (shell_input_line)
2034 /* Lines that signify the end of the shell's input should not be
2036 if (echo_input_at_read && (shell_input_line[0] ||
2037 shell_input_line_terminator != EOF))
2038 fprintf (stderr, "%s\n", shell_input_line);
2042 shell_input_line_size = 0;
2043 prompt_string_pointer = ¤t_prompt_string;
2044 if (SHOULD_PROMPT ())
2049 /* Add the newline to the end of this string, iff the string does
2050 not already end in an EOF character. */
2051 if (shell_input_line_terminator != EOF)
2053 if (shell_input_line_len + 3 > shell_input_line_size)
2054 shell_input_line = (char *)xrealloc (shell_input_line,
2055 1 + (shell_input_line_size += 2));
2057 shell_input_line[shell_input_line_len] = '\n';
2058 shell_input_line[shell_input_line_len + 1] = '\0';
2060 set_line_mbstate ();
2064 uc = shell_input_line[shell_input_line_index];
2067 shell_input_line_index++;
2069 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2070 /* If UC is NULL, we have reached the end of the current input string. If
2071 pushed_string_list is non-empty, it's time to pop to the previous string
2072 because we have fully consumed the result of the last alias expansion.
2073 Do it transparently; just return the next character of the string popped
2075 if (!uc && (pushed_string_list != (STRING_SAVER *)NULL))
2078 uc = shell_input_line[shell_input_line_index];
2080 shell_input_line_index++;
2082 #endif /* ALIAS || DPAREN_ARITHMETIC */
2084 if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n')
2086 if (SHOULD_PROMPT ())
2092 if (!uc && shell_input_line_terminator == EOF)
2093 return ((shell_input_line_index != 0) ? '\n' : EOF);
2098 /* Put C back into the input for the shell. This might need changes for
2099 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
2100 character different than we read, shell_input_line_property doesn't need
2101 to change when manipulating shell_input_line. The define for
2102 last_shell_getc_is_singlebyte should take care of it, though. */
2107 if (shell_input_line && shell_input_line_index)
2108 shell_input_line[--shell_input_line_index] = c;
2110 eol_ungetc_lookahead = c;
2113 #ifdef INCLUDE_UNUSED
2114 /* Back the input pointer up by one, effectively `ungetting' a character. */
2118 if (shell_input_line && shell_input_line_index)
2119 shell_input_line_index--;
2123 /* Discard input until CHARACTER is seen, then push that character back
2124 onto the input stream. */
2126 discard_until (character)
2131 while ((c = shell_getc (0)) != EOF && c != character)
2139 execute_prompt_command (command)
2143 sh_parser_state_t ps;
2145 save_parser_state (&ps);
2146 last_lastarg = get_string_value ("_");
2148 last_lastarg = savestring (last_lastarg);
2150 parse_and_execute (savestring (command), "PROMPT_COMMAND", SEVAL_NONINT|SEVAL_NOHIST);
2152 restore_parser_state (&ps);
2153 bind_variable ("_", last_lastarg, 0);
2154 FREE (last_lastarg);
2156 if (token_to_read == '\n') /* reset_parser was called */
2160 /* Place to remember the token. We try to keep the buffer
2161 at a reasonable size, but it can grow. */
2162 static char *token = (char *)NULL;
2164 /* Current size of the token buffer. */
2165 static int token_buffer_size;
2167 /* Command to read_token () explaining what we want it to do. */
2170 #define prompt_is_ps1 \
2171 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
2173 /* Function for yyparse to call. yylex keeps track of
2174 the last two tokens read, and calls read_token. */
2178 if (interactive && (current_token == 0 || current_token == '\n'))
2180 /* Before we print a prompt, we might have to check mailboxes.
2181 We do this only if it is time to do so. Notice that only here
2182 is the mail alarm reset; nothing takes place in check_mail ()
2183 except the checking of mail. Please don't change this. */
2184 if (prompt_is_ps1 && time_to_check_mail ())
2187 reset_mail_timer ();
2190 /* Avoid printing a prompt if we're not going to read anything, e.g.
2191 after resetting the parser with read_token (RESET). */
2192 if (token_to_read == 0 && SHOULD_PROMPT ())
2196 two_tokens_ago = token_before_that;
2197 token_before_that = last_read_token;
2198 last_read_token = current_token;
2199 current_token = read_token (READ);
2200 return (current_token);
2203 /* When non-zero, we have read the required tokens
2204 which allow ESAC to be the next one read. */
2205 static int esacs_needed_count;
2208 gather_here_documents ()
2211 while (need_here_doc)
2213 make_here_document (redir_stack[r++]);
2218 /* When non-zero, an open-brace used to create a group is awaiting a close
2220 static int open_brace_count;
2222 #define command_token_position(token) \
2223 (((token) == ASSIGNMENT_WORD) || \
2224 ((token) != SEMI_SEMI && reserved_word_acceptable(token)))
2226 #define assignment_acceptable(token) \
2227 (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0))
2229 /* Check to see if TOKEN is a reserved word and return the token
2231 #define CHECK_FOR_RESERVED_WORD(tok) \
2233 if (!dollar_present && !quoted && \
2234 reserved_word_acceptable (last_read_token)) \
2237 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
2238 if (STREQ (tok, word_token_alist[i].word)) \
2240 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
2242 if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \
2244 if (word_token_alist[i].token == ESAC) \
2245 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
2246 else if (word_token_alist[i].token == CASE) \
2247 parser_state |= PST_CASESTMT; \
2248 else if (word_token_alist[i].token == COND_END) \
2249 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
2250 else if (word_token_alist[i].token == COND_START) \
2251 parser_state |= PST_CONDCMD; \
2252 else if (word_token_alist[i].token == '{') \
2253 open_brace_count++; \
2254 else if (word_token_alist[i].token == '}' && open_brace_count) \
2255 open_brace_count--; \
2256 return (word_token_alist[i].token); \
2263 /* OK, we have a token. Let's try to alias expand it, if (and only if)
2266 It is eligible for expansion if EXPAND_ALIASES is set, and
2267 the token is unquoted and the last token read was a command
2268 separator (or expand_next_token is set), and we are currently
2269 processing an alias (pushed_string_list is non-empty) and this
2270 token is not the same as the current or any previously
2273 Special cases that disqualify:
2274 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
2284 r = xmalloc (l + 2);
2293 alias_expand_token (tokstr)
2299 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
2300 (parser_state & PST_CASEPAT) == 0)
2302 ap = find_alias (tokstr);
2304 /* Currently expanding this token. */
2305 if (ap && (ap->flags & AL_BEINGEXPANDED))
2306 return (NO_EXPANSION);
2308 /* mk_alexpansion puts an extra space on the end of the alias expansion,
2309 so the lookahead by the parser works right. If this gets changed,
2310 make sure the code in shell_getc that deals with reaching the end of
2311 an expanded alias is changed with it. */
2312 expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL;
2316 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
2317 return (RE_READ_TOKEN);
2320 /* This is an eligible token that does not have an expansion. */
2321 return (NO_EXPANSION);
2323 return (NO_EXPANSION);
2328 time_command_acceptable ()
2330 #if defined (COMMAND_TIMING)
2331 switch (last_read_token)
2350 #endif /* COMMAND_TIMING */
2353 /* Handle special cases of token recognition:
2354 IN is recognized if the last token was WORD and the token
2355 before that was FOR or CASE or SELECT.
2357 DO is recognized if the last token was WORD and the token
2358 before that was FOR or SELECT.
2360 ESAC is recognized if the last token caused `esacs_needed_count'
2363 `{' is recognized if the last token as WORD and the token
2364 before that was FUNCTION, or if we just parsed an arithmetic
2367 `}' is recognized if there is an unclosed `{' present.
2369 `-p' is returned as TIMEOPT if the last read token was TIME.
2371 ']]' is returned as COND_END if the parser is currently parsing
2372 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
2374 `time' is returned as TIME if and only if it is immediately
2375 preceded by one of `;', `\n', `||', `&&', or `&'.
2379 special_case_tokens (tokstr)
2382 if ((last_read_token == WORD) &&
2383 #if defined (SELECT_COMMAND)
2384 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
2386 ((token_before_that == FOR) || (token_before_that == CASE)) &&
2388 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
2390 if (token_before_that == CASE)
2392 parser_state |= PST_CASEPAT;
2393 esacs_needed_count++;
2398 if (last_read_token == WORD &&
2399 #if defined (SELECT_COMMAND)
2400 (token_before_that == FOR || token_before_that == SELECT) &&
2402 (token_before_that == FOR) &&
2404 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
2407 /* Ditto for ESAC in the CASE case.
2408 Specifically, this handles "case word in esac", which is a legal
2409 construct, certainly because someone will pass an empty arg to the
2410 case construct, and we don't want it to barf. Of course, we should
2411 insist that the case construct has at least one pattern in it, but
2412 the designers disagree. */
2413 if (esacs_needed_count)
2415 esacs_needed_count--;
2416 if (STREQ (tokstr, "esac"))
2418 parser_state &= ~PST_CASEPAT;
2423 /* The start of a shell function definition. */
2424 if (parser_state & PST_ALLOWOPNBRC)
2426 parser_state &= ~PST_ALLOWOPNBRC;
2427 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2430 function_bstart = line_number;
2431 return ('{'); /* } */
2435 /* We allow a `do' after a for ((...)) without an intervening
2437 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
2439 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2442 return ('{'); /* } */
2445 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
2447 open_brace_count--; /* { */
2451 #if defined (COMMAND_TIMING)
2452 /* Handle -p after `time'. */
2453 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
2458 #if defined (COMMAND_TIMING)
2459 if (STREQ (token, "time") && ((parser_state & PST_CASEPAT) == 0) && time_command_acceptable ())
2461 #endif /* COMMAND_TIMING */
2464 #if defined (COND_COMMAND) /* [[ */
2465 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
2472 /* Called from shell.c when Control-C is typed at top level. Or
2473 by the error rule at top level. */
2477 dstack.delimiter_depth = 0; /* No delimiters found so far. */
2478 open_brace_count = 0;
2482 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2483 if (pushed_string_list)
2484 free_string_list ();
2485 #endif /* ALIAS || DPAREN_ARITHMETIC */
2487 if (shell_input_line)
2489 free (shell_input_line);
2490 shell_input_line = (char *)NULL;
2491 shell_input_line_size = shell_input_line_index = 0;
2494 FREE (word_desc_to_read);
2495 word_desc_to_read = (WORD_DESC *)NULL;
2497 last_read_token = '\n';
2498 token_to_read = '\n';
2501 /* Read the next token. Command can be READ (normal operation) or
2502 RESET (to normalize state). */
2504 read_token (command)
2507 int character; /* Current character. */
2508 int peek_char; /* Temporary look-ahead character. */
2509 int result; /* The thing to return. */
2511 if (command == RESET)
2519 result = token_to_read;
2520 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
2522 yylval.word = word_desc_to_read;
2523 word_desc_to_read = (WORD_DESC *)NULL;
2529 #if defined (COND_COMMAND)
2530 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
2532 cond_lineno = line_number;
2533 parser_state |= PST_CONDEXPR;
2534 yylval.command = parse_cond_command ();
2535 if (cond_token != COND_END)
2540 token_to_read = COND_END;
2541 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
2547 /* This is a place to jump back to once we have successfully expanded a
2548 token with an alias and pushed the string with push_string () */
2552 /* Read a single word from input. Start by skipping blanks. */
2553 while ((character = shell_getc (1)) != EOF && whitespace (character))
2556 if (character == EOF)
2562 if MBTEST(character == '#' && (!interactive || interactive_comments))
2564 /* A comment. Discard until EOL or EOF, and then return a newline. */
2565 discard_until ('\n');
2567 character = '\n'; /* this will take the next if statement and return. */
2570 if (character == '\n')
2572 /* If we're about to return an unquoted newline, we can go and collect
2573 the text of any pending here document. */
2575 gather_here_documents ();
2578 parser_state &= ~PST_ALEXPNEXT;
2581 parser_state &= ~PST_ASSIGNOK;
2586 /* Shell meta-characters. */
2587 if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
2590 /* Turn off alias tokenization iff this character sequence would
2591 not leave us ready to read a command. */
2592 if (character == '<' || character == '>')
2593 parser_state &= ~PST_ALEXPNEXT;
2596 parser_state &= ~PST_ASSIGNOK;
2598 peek_char = shell_getc (1);
2599 if (character == peek_char)
2604 /* If '<' then we could be at "<<" or at "<<-". We have to
2605 look ahead one more character. */
2606 peek_char = shell_getc (1);
2607 if (peek_char == '-')
2608 return (LESS_LESS_MINUS);
2609 else if (peek_char == '<')
2610 return (LESS_LESS_LESS);
2613 shell_ungetc (peek_char);
2618 return (GREATER_GREATER);
2621 parser_state |= PST_CASEPAT;
2623 parser_state &= ~PST_ALEXPNEXT;
2634 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2636 result = parse_dparen (character);
2644 else if MBTEST(character == '<' && peek_char == '&')
2646 else if MBTEST(character == '>' && peek_char == '&')
2647 return (GREATER_AND);
2648 else if MBTEST(character == '<' && peek_char == '>')
2649 return (LESS_GREATER);
2650 else if MBTEST(character == '>' && peek_char == '|')
2651 return (GREATER_BAR);
2652 else if MBTEST(peek_char == '>' && character == '&')
2653 return (AND_GREATER);
2655 shell_ungetc (peek_char);
2657 /* If we look like we are reading the start of a function
2658 definition, then let the reader know about it so that
2659 we will do the right thing with `{'. */
2660 if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
2662 parser_state |= PST_ALLOWOPNBRC;
2664 parser_state &= ~PST_ALEXPNEXT;
2666 function_dstart = line_number;
2669 /* case pattern lists may be preceded by an optional left paren. If
2670 we're not trying to parse a case pattern list, the left paren
2671 indicates a subshell. */
2672 if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
2673 parser_state |= PST_SUBSHELL;
2675 else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
2676 parser_state &= ~PST_CASEPAT;
2678 else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
2679 parser_state &= ~PST_SUBSHELL;
2681 #if defined (PROCESS_SUBSTITUTION)
2682 /* Check for the constructs which introduce process substitution.
2683 Shells running in `posix mode' don't do process substitution. */
2684 if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/
2685 #endif /* PROCESS_SUBSTITUTION */
2689 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
2690 if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
2693 /* Okay, if we got this far, we have to read a word. Read one,
2694 and then check it against the known ones. */
2695 result = read_token_word (character);
2697 if (result == RE_READ_TOKEN)
2704 * Match a $(...) or other grouping construct. This has to handle embedded
2705 * quoted strings ('', ``, "") and nested constructs. It also must handle
2706 * reprompting the user, if necessary, after reading a newline, and returning
2707 * correct error values if it reads EOF.
2709 #define P_FIRSTCLOSE 0x01
2710 #define P_ALLOWESC 0x02
2711 #define P_DQUOTE 0x04
2712 #define P_COMMAND 0x08 /* parsing a command, so look for comments */
2714 static char matched_pair_error;
2716 parse_matched_pair (qc, open, close, lenp, flags)
2717 int qc; /* `"' if this construct is within double quotes */
2721 int count, ch, was_dollar, in_comment, check_comment;
2722 int pass_next_character, nestlen, ttranslen, start_lineno;
2723 char *ret, *nestret, *ttrans;
2724 int retind, retsize, rflags;
2727 pass_next_character = was_dollar = in_comment = 0;
2728 check_comment = (flags & P_COMMAND) && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0;
2730 itrace("parse_matched_pair: check_comment set to %d", check_comment);
2732 /* RFLAGS is the set of flags we want to pass to recursive calls. */
2733 rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE);
2735 ret = (char *)xmalloc (retsize = 64);
2738 start_lineno = line_number;
2742 ch = shell_getc ((qc != '\'' || (flags & P_ALLOWESC)) && pass_next_character == 0);
2744 ch = shell_getc (qc != '\'' && pass_next_character == 0);
2749 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
2750 EOF_Reached = 1; /* XXX */
2751 return (&matched_pair_error);
2754 /* Possible reprompting. */
2755 if (ch == '\n' && SHOULD_PROMPT ())
2758 if (in_comment && ch == '\n')
2760 else if (check_comment && in_comment == 0 && ch == '#')
2763 itrace("parse_matched_pair: set in_comment to 1");
2765 if (pass_next_character) /* last char was backslash */
2767 pass_next_character = 0;
2768 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
2770 if (retind > 0) retind--; /* swallow previously-added backslash */
2774 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2775 if MBTEST(ch == CTLESC || ch == CTLNUL)
2776 ret[retind++] = CTLESC;
2780 else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
2782 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2783 ret[retind++] = CTLESC;
2787 else if MBTEST(ch == close) /* ending delimiter */
2790 /* handle nested ${...} specially. */
2791 else if MBTEST(open != close && was_dollar && open == '{' && ch == open) /* } */
2794 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
2797 /* Add this character. */
2798 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
2801 if (open == '\'') /* '' inside grouping construct */
2803 if MBTEST((flags & P_ALLOWESC) && ch == '\\')
2804 pass_next_character++;
2808 if MBTEST(ch == '\\') /* backslashes */
2809 pass_next_character++;
2811 if (open != close) /* a grouping construct */
2813 if MBTEST(shellquote (ch))
2815 /* '', ``, or "" inside $(...) or other grouping construct. */
2816 push_delimiter (dstack, ch);
2817 if MBTEST(was_dollar && ch == '\'') /* $'...' inside group */
2818 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
2820 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
2821 pop_delimiter (dstack);
2822 if (nestret == &matched_pair_error)
2825 return &matched_pair_error;
2827 if MBTEST(was_dollar && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
2829 /* Translate $'...' here. */
2830 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
2833 if ((rflags & P_DQUOTE) == 0)
2835 nestret = sh_single_quote (ttrans);
2837 nestlen = strlen (nestret);
2842 nestlen = ttranslen;
2844 retind -= 2; /* back up before the $' */
2846 else if MBTEST(was_dollar && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
2848 /* Locale expand $"..." here. */
2849 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
2852 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
2854 nestlen = ttranslen + 2;
2855 retind -= 2; /* back up before the $" */
2860 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2861 strcpy (ret + retind, nestret);
2867 /* Parse an old-style command substitution within double quotes as a
2869 /* XXX - sh and ksh93 don't do this - XXX */
2870 else if MBTEST(open == '"' && ch == '`')
2872 nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags);
2874 if (nestret == &matched_pair_error)
2877 return &matched_pair_error;
2881 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2882 strcpy (ret + retind, nestret);
2887 else if MBTEST(qc == '`' && (ch == '"' || ch == '\'') && in_comment == 0)
2889 itrace("parse_matched_pair: parsing quoted string in backquotes");
2890 nestret = parse_matched_pair (0, ch, ch, &nestlen, rflags);
2893 else if MBTEST(was_dollar && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
2894 /* check for $(), $[], or ${} inside quoted string. */
2896 if (open == ch) /* undo previous increment */
2898 if (ch == '(') /* ) */
2899 nestret = parse_matched_pair (0, '(', ')', &nestlen, rflags);
2900 else if (ch == '{') /* } */
2901 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|rflags);
2902 else if (ch == '[') /* ] */
2903 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
2907 was_dollar = MBTEST(ch == '$');
2916 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2917 /* Parse a double-paren construct. It can be either an arithmetic
2918 command, an arithmetic `for' command, or a nested subshell. Returns
2919 the parsed token, -1 on error, or -2 if we didn't do anything and
2920 should just go on. */
2925 int cmdtyp, len, sline;
2929 #if defined (ARITH_FOR_COMMAND)
2930 if (last_read_token == FOR)
2932 arith_for_lineno = line_number;
2933 cmdtyp = parse_arith_cmd (&wval, 0);
2936 wd = alloc_word_desc ();
2938 wd = make_word (wval);
2939 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
2940 return (ARITH_FOR_EXPRS);
2943 return -1; /* ERROR */
2947 #if defined (DPAREN_ARITHMETIC)
2948 if (reserved_word_acceptable (last_read_token))
2950 sline = line_number;
2952 cmdtyp = parse_arith_cmd (&wval, 0);
2953 if (cmdtyp == 1) /* arithmetic command */
2955 wd = alloc_word_desc ();
2957 wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE;
2958 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
2961 else if (cmdtyp == 0) /* nested subshell */
2963 push_string (wval, 0, (alias_t *)NULL);
2964 if ((parser_state & PST_CASEPAT) == 0)
2965 parser_state |= PST_SUBSHELL;
2973 return -2; /* XXX */
2976 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
2977 If not, assume it's a nested subshell for backwards compatibility and
2978 return 0. In any case, put the characters we've consumed into a locally-
2979 allocated buffer and make *ep point to that buffer. Return -1 on an
2980 error, for example EOF. */
2982 parse_arith_cmd (ep, adddq)
2986 int exp_lineno, rval, c;
2987 char *ttok, *tokstr;
2990 exp_lineno = line_number;
2991 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
2993 if (ttok == &matched_pair_error)
2995 /* Check that the next character is the closing right paren. If
2996 not, this is a syntax error. ( */
3001 tokstr = (char *)xmalloc (ttoklen + 4);
3003 /* if ADDDQ != 0 then (( ... )) -> "..." */
3004 if (rval == 1 && adddq) /* arith cmd, add double quotes */
3007 strncpy (tokstr + 1, ttok, ttoklen - 1);
3008 tokstr[ttoklen] = '"';
3009 tokstr[ttoklen+1] = '\0';
3011 else if (rval == 1) /* arith cmd, don't add double quotes */
3013 strncpy (tokstr, ttok, ttoklen - 1);
3014 tokstr[ttoklen-1] = '\0';
3016 else /* nested subshell */
3019 strncpy (tokstr + 1, ttok, ttoklen - 1);
3020 tokstr[ttoklen] = ')';
3021 tokstr[ttoklen+1] = c;
3022 tokstr[ttoklen+2] = '\0';
3029 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
3031 #if defined (COND_COMMAND)
3037 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
3038 parser_error (cond_lineno, _("unexpected EOF while looking for `]]'"));
3039 else if (cond_token != COND_ERROR)
3041 if (etext = error_token_from_token (cond_token))
3043 parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext);
3047 parser_error (cond_lineno, _("syntax error in conditional expression"));
3054 return (cond_or ());
3063 if (cond_token == OR_OR)
3066 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
3077 if (cond_token == AND_AND)
3080 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
3086 cond_skip_newlines ()
3088 while ((cond_token = read_token (READ)) == '\n')
3090 if (SHOULD_PROMPT ())
3093 return (cond_token);
3096 #define COND_RETURN_ERROR() \
3097 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
3103 COND_COM *term, *tleft, *tright;
3107 /* Read a token. It can be a left paren, a `!', a unary operator, or a
3108 word that should be the first argument of a binary operator. Start by
3109 skipping newlines, since this is a compound command. */
3110 tok = cond_skip_newlines ();
3111 lineno = line_number;
3112 if (tok == COND_END)
3114 COND_RETURN_ERROR ();
3116 else if (tok == '(')
3118 term = cond_expr ();
3119 if (cond_token != ')')
3122 dispose_cond_node (term); /* ( */
3123 if (etext = error_token_from_token (cond_token))
3125 parser_error (lineno, _("unexpected token `%s', expected `)'"), etext);
3129 parser_error (lineno, _("expected `)'"));
3130 COND_RETURN_ERROR ();
3132 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
3133 (void)cond_skip_newlines ();
3135 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
3138 dispose_word (yylval.word); /* not needed */
3139 term = cond_term ();
3141 term->flags |= CMD_INVERT_RETURN;
3143 else if (tok == WORD && test_unop (yylval.word->word))
3146 tok = read_token (READ);
3149 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3150 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
3155 if (etext = error_token_from_token (tok))
3157 parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext);
3161 parser_error (line_number, _("unexpected argument to conditional unary operator"));
3162 COND_RETURN_ERROR ();
3165 (void)cond_skip_newlines ();
3167 else if (tok == WORD) /* left argument to binary operator */
3170 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3173 tok = read_token (READ);
3174 if (tok == WORD && test_binop (yylval.word->word))
3176 #if defined (COND_REGEXP)
3177 else if (tok == WORD && STREQ (yylval.word->word,"=~"))
3180 else if (tok == '<' || tok == '>')
3181 op = make_word_from_token (tok); /* ( */
3182 /* There should be a check before blindly accepting the `)' that we have
3183 seen the opening `('. */
3184 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
3186 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
3187 the test command. Similarly for [[ x && expr ]] or
3188 [[ x || expr ]] or [[ (x) ]]. */
3189 op = make_word ("-n");
3190 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
3196 if (etext = error_token_from_token (tok))
3198 parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext);
3202 parser_error (line_number, _("conditional binary operator expected"));
3203 dispose_cond_node (tleft);
3204 COND_RETURN_ERROR ();
3208 tok = read_token (READ);
3211 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3212 term = make_cond_node (COND_BINARY, op, tleft, tright);
3216 if (etext = error_token_from_token (tok))
3218 parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext);
3222 parser_error (line_number, _("unexpected argument to conditional binary operator"));
3223 dispose_cond_node (tleft);
3225 COND_RETURN_ERROR ();
3228 (void)cond_skip_newlines ();
3233 parser_error (line_number, _("unexpected token `%c' in conditional command"), tok);
3234 else if (etext = error_token_from_token (tok))
3236 parser_error (line_number, _("unexpected token `%s' in conditional command"), etext);
3240 parser_error (line_number, _("unexpected token %d in conditional command"), tok);
3241 COND_RETURN_ERROR ();
3246 /* This is kind of bogus -- we slip a mini recursive-descent parser in
3247 here to handle the conditional statement syntax. */
3249 parse_cond_command ()
3253 cexp = cond_expr ();
3254 return (make_cond_command (cexp));
3258 #if defined (ARRAY_VARS)
3259 /* When this is called, it's guaranteed that we don't care about anything
3260 in t beyond i. We do save and restore the chars, though. */
3262 token_is_assignment (t, i)
3266 unsigned char c, c1;
3269 c = t[i]; c1 = t[i+1];
3270 t[i] = '='; t[i+1] = '\0';
3271 r = assignment (t, (parser_state & PST_COMPASSIGN) != 0);
3272 t[i] = c; t[i+1] = c1;
3276 /* XXX - possible changes here for `+=' */
3278 token_is_ident (t, i)
3287 r = legal_identifier (t);
3294 read_token_word (character)
3297 /* The value for YYLVAL when a WORD is read. */
3298 WORD_DESC *the_word;
3300 /* Index into the token that we are building. */
3303 /* ALL_DIGITS becomes zero when we see a non-digit. */
3304 int all_digit_token;
3306 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
3309 /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
3311 int compound_assignment;
3313 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
3316 /* Non-zero means to ignore the value of the next character, and just
3317 to add it no matter what. */
3318 int pass_next_character;
3320 /* The current delimiting character. */
3322 int result, peek_char;
3323 char *ttok, *ttrans;
3324 int ttoklen, ttranslen;
3327 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
3328 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
3331 all_digit_token = DIGIT (character);
3332 dollar_present = quoted = pass_next_character = compound_assignment = 0;
3336 if (character == EOF)
3339 if (pass_next_character)
3341 pass_next_character = 0;
3345 cd = current_delimiter (dstack);
3347 /* Handle backslashes. Quote lots of things when not inside of
3348 double-quotes, quote some things inside of double-quotes. */
3349 if MBTEST(character == '\\')
3351 peek_char = shell_getc (0);
3353 /* Backslash-newline is ignored in all cases except
3354 when quoted with single quotes. */
3355 if (peek_char == '\n')
3358 goto next_character;
3362 shell_ungetc (peek_char);
3364 /* If the next character is to be quoted, note it now. */
3365 if (cd == 0 || cd == '`' ||
3366 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
3367 pass_next_character++;
3374 /* Parse a matched pair of quote characters. */
3375 if MBTEST(shellquote (character))
3377 push_delimiter (dstack, character);
3378 itrace("read_token_word: calling parse_matched_pair: character = %c", character);
3380 ttok = parse_matched_pair (character, character, character, &ttoklen, 0);
3382 ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0);
3384 pop_delimiter (dstack);
3385 if (ttok == &matched_pair_error)
3386 return -1; /* Bail immediately. */
3387 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3388 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
3389 token[token_index++] = character;
3390 strcpy (token + token_index, ttok);
3391 token_index += ttoklen;
3392 all_digit_token = 0;
3394 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
3396 goto next_character;
3399 #ifdef EXTENDED_GLOB
3400 /* Parse a ksh-style extended pattern matching specification. */
3401 if (extended_glob && PATTERN_CHAR (character))
3403 peek_char = shell_getc (1);
3404 if MBTEST(peek_char == '(') /* ) */
3406 push_delimiter (dstack, peek_char);
3407 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
3408 pop_delimiter (dstack);
3409 if (ttok == &matched_pair_error)
3410 return -1; /* Bail immediately. */
3411 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3413 TOKEN_DEFAULT_GROW_SIZE);
3414 token[token_index++] = character;
3415 token[token_index++] = peek_char;
3416 strcpy (token + token_index, ttok);
3417 token_index += ttoklen;
3419 dollar_present = all_digit_token = 0;
3420 goto next_character;
3423 shell_ungetc (peek_char);
3425 #endif /* EXTENDED_GLOB */
3427 /* If the delimiter character is not single quote, parse some of
3428 the shell expansions that must be read as a single word. */
3429 if (shellexp (character))
3431 peek_char = shell_getc (1);
3432 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
3433 if MBTEST(peek_char == '(' || \
3434 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
3436 if (peek_char == '{') /* } */
3437 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE);
3438 else if (peek_char == '(') /* ) */
3440 /* XXX - push and pop the `(' as a delimiter for use by
3441 the command-oriented-history code. This way newlines
3442 appearing in the $(...) string get added to the
3443 history literally rather than causing a possibly-
3444 incorrect `;' to be added. ) */
3445 push_delimiter (dstack, peek_char);
3446 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, P_COMMAND);
3447 pop_delimiter (dstack);
3450 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
3451 if (ttok == &matched_pair_error)
3452 return -1; /* Bail immediately. */
3453 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3455 TOKEN_DEFAULT_GROW_SIZE);
3456 token[token_index++] = character;
3457 token[token_index++] = peek_char;
3458 strcpy (token + token_index, ttok);
3459 token_index += ttoklen;
3462 all_digit_token = 0;
3463 goto next_character;
3465 /* This handles $'...' and $"..." new-style quoted strings. */
3466 else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
3470 first_line = line_number;
3471 push_delimiter (dstack, peek_char);
3472 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
3474 (peek_char == '\'') ? P_ALLOWESC : 0);
3475 pop_delimiter (dstack);
3476 if (ttok == &matched_pair_error)
3478 if (peek_char == '\'')
3480 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
3483 /* Insert the single quotes and correctly quote any
3484 embedded single quotes (allowed because P_ALLOWESC was
3485 passed to parse_matched_pair). */
3486 ttok = sh_single_quote (ttrans);
3488 ttranslen = strlen (ttok);
3493 /* Try to locale)-expand the converted string. */
3494 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
3497 /* Add the double quotes back */
3498 ttok = sh_mkdoublequoted (ttrans, ttranslen, 0);
3504 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2,
3506 TOKEN_DEFAULT_GROW_SIZE);
3507 strcpy (token + token_index, ttrans);
3508 token_index += ttranslen;
3511 all_digit_token = 0;
3512 goto next_character;
3514 /* This could eventually be extended to recognize all of the
3515 shell's single-character parameter expansions, and set flags.*/
3516 else if MBTEST(character == '$' && peek_char == '$')
3518 ttok = (char *)xmalloc (3);
3519 ttok[0] = ttok[1] = '$';
3521 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
3523 TOKEN_DEFAULT_GROW_SIZE);
3524 strcpy (token + token_index, ttok);
3527 all_digit_token = 0;
3529 goto next_character;
3532 shell_ungetc (peek_char);
3535 #if defined (ARRAY_VARS)
3536 /* Identify possible array subscript assignment; match [...] */
3537 else if MBTEST(character == '[' && token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) /* ] */
3539 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
3540 if (ttok == &matched_pair_error)
3541 return -1; /* Bail immediately. */
3542 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3544 TOKEN_DEFAULT_GROW_SIZE);
3545 token[token_index++] = character;
3546 strcpy (token + token_index, ttok);
3547 token_index += ttoklen;
3549 all_digit_token = 0;
3550 goto next_character;
3552 /* Identify possible compound array variable assignment. */
3553 else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index))
3555 peek_char = shell_getc (1);
3556 if MBTEST(peek_char == '(') /* ) */
3558 ttok = parse_compound_assignment (&ttoklen);
3560 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
3562 TOKEN_DEFAULT_GROW_SIZE);
3564 token[token_index++] = '=';
3565 token[token_index++] = '(';
3568 strcpy (token + token_index, ttok);
3569 token_index += ttoklen;
3571 token[token_index++] = ')';
3573 all_digit_token = 0;
3574 compound_assignment = 1;
3576 goto next_character;
3578 goto got_token; /* ksh93 seems to do this */
3582 shell_ungetc (peek_char);
3586 /* When not parsing a multi-character word construct, shell meta-
3587 characters break words. */
3588 if MBTEST(shellbreak (character))
3590 shell_ungetc (character);
3596 all_digit_token &= DIGIT (character);
3597 dollar_present |= character == '$';
3599 if (character == CTLESC || character == CTLNUL)
3600 token[token_index++] = CTLESC;
3602 token[token_index++] = character;
3604 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
3605 TOKEN_DEFAULT_GROW_SIZE);
3608 if (character == '\n' && SHOULD_PROMPT ())
3611 /* We want to remove quoted newlines (that is, a \<newline> pair)
3612 unless we are within single quotes or pass_next_character is
3613 set (the shell equivalent of literal-next). */
3614 cd = current_delimiter (dstack);
3615 character = shell_getc (cd != '\'' && pass_next_character == 0);
3616 } /* end for (;;) */
3620 token[token_index] = '\0';
3622 /* Check to see what thing we should return. If the last_read_token
3623 is a `<', or a `&', or the character which ended this token is
3624 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
3625 Otherwise, it is just a word, and should be returned as such. */
3626 if MBTEST(all_digit_token && (character == '<' || character == '>' || \
3627 last_read_token == LESS_AND || \
3628 last_read_token == GREATER_AND))
3630 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
3631 yylval.number = lvalue;
3637 /* Check for special case tokens. */
3638 result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
3643 /* Posix.2 does not allow reserved words to be aliased, so check for all
3644 of them, including special cases, before expanding the current token
3646 if MBTEST(posixly_correct)
3647 CHECK_FOR_RESERVED_WORD (token);
3649 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
3650 inhibits alias expansion. */
3651 if (expand_aliases && quoted == 0)
3653 result = alias_expand_token (token);
3654 if (result == RE_READ_TOKEN)
3655 return (RE_READ_TOKEN);
3656 else if (result == NO_EXPANSION)
3657 parser_state &= ~PST_ALEXPNEXT;
3660 /* If not in Posix.2 mode, check for reserved words after alias
3662 if MBTEST(posixly_correct == 0)
3664 CHECK_FOR_RESERVED_WORD (token);
3666 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
3667 the_word->word = (char *)xmalloc (1 + token_index);
3668 the_word->flags = 0;
3669 strcpy (the_word->word, token);
3671 the_word->flags |= W_HASDOLLAR;
3673 the_word->flags |= W_QUOTED;
3674 if (compound_assignment)
3675 the_word->flags |= W_COMPASSIGN;
3676 /* A word is an assignment if it appears at the beginning of a
3677 simple command, or after another assignment word. This is
3678 context-dependent, so it cannot be handled in the grammar. */
3679 if (assignment (token, (parser_state & PST_COMPASSIGN) != 0))
3681 the_word->flags |= W_ASSIGNMENT;
3682 /* Don't perform word splitting on assignment statements. */
3683 if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0)
3684 the_word->flags |= W_NOSPLIT;
3687 if (command_token_position (last_read_token))
3690 b = builtin_address_internal (token, 0);
3691 if (b && (b->flags & ASSIGNMENT_BUILTIN))
3692 parser_state |= PST_ASSIGNOK;
3695 yylval.word = the_word;
3697 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
3698 ? ASSIGNMENT_WORD : WORD;
3700 switch (last_read_token)
3703 parser_state |= PST_ALLOWOPNBRC;
3704 function_dstart = line_number;
3709 if (word_top < MAX_CASE_NEST)
3711 word_lineno[word_top] = line_number;
3718 /* Return 1 if TOKSYM is a token that after being read would allow
3719 a reserved word to be seen, else 0. */
3721 reserved_word_acceptable (toksym)
3757 /* Return the index of TOKEN in the alist of reserved words, or -1 if
3758 TOKEN is not a shell reserved word. */
3760 find_reserved_word (tokstr)
3764 for (i = 0; word_token_alist[i].word; i++)
3765 if (STREQ (tokstr, word_token_alist[i].word))
3771 #if defined (READLINE)
3772 /* Called after each time readline is called. This insures that whatever
3773 the new prompt string is gets propagated to readline's local prompt
3776 reset_readline_prompt ()
3780 if (prompt_string_pointer)
3782 temp_prompt = (*prompt_string_pointer)
3783 ? decode_prompt_string (*prompt_string_pointer)
3786 if (temp_prompt == 0)
3788 temp_prompt = (char *)xmalloc (1);
3789 temp_prompt[0] = '\0';
3792 FREE (current_readline_prompt);
3793 current_readline_prompt = temp_prompt;
3796 #endif /* READLINE */
3799 #if defined (HISTORY)
3800 /* A list of tokens which can be followed by newlines, but not by
3801 semi-colons. When concatenating multiple lines of history, the
3802 newline separator for such tokens is replaced with a space. */
3803 static int no_semi_successors[] = {
3804 '\n', '{', '(', ')', ';', '&', '|',
3805 CASE, DO, ELSE, IF, SEMI_SEMI, THEN, UNTIL, WHILE, AND_AND, OR_OR, IN,
3809 /* If we are not within a delimited expression, try to be smart
3810 about which separators can be semi-colons and which must be
3811 newlines. Returns the string that should be added into the
3814 history_delimiting_chars ()
3818 if (dstack.delimiter_depth != 0)
3821 /* First, handle some special cases. */
3823 /* If we just read `()', assume it's a function definition, and don't
3824 add a semicolon. If the token before the `)' was not `(', and we're
3825 not in the midst of parsing a case statement, assume it's a
3826 parenthesized command and add the semicolon. */
3828 if (token_before_that == ')')
3830 if (two_tokens_ago == '(') /*)*/ /* function def */
3832 /* This does not work for subshells inside case statement
3833 command lists. It's a suboptimal solution. */
3834 else if (parser_state & PST_CASESTMT) /* case statement pattern */
3837 return "; "; /* (...) subshell */
3839 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
3840 return " "; /* function def using `function name' without `()' */
3842 else if (token_before_that == WORD && two_tokens_ago == FOR)
3844 /* Tricky. `for i\nin ...' should not have a semicolon, but
3845 `for i\ndo ...' should. We do what we can. */
3846 for (i = shell_input_line_index; whitespace(shell_input_line[i]); i++)
3848 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
3852 else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT))
3855 for (i = 0; no_semi_successors[i]; i++)
3857 if (token_before_that == no_semi_successors[i])
3863 #endif /* HISTORY */
3865 /* Issue a prompt, or prepare to issue a prompt when the next character
3872 if (interactive == 0 || expanding_alias()) /* XXX */
3875 ps1_prompt = get_string_value ("PS1");
3876 ps2_prompt = get_string_value ("PS2");
3878 if (!prompt_string_pointer)
3879 prompt_string_pointer = &ps1_prompt;
3881 temp_prompt = *prompt_string_pointer
3882 ? decode_prompt_string (*prompt_string_pointer)
3885 if (temp_prompt == 0)
3887 temp_prompt = (char *)xmalloc (1);
3888 temp_prompt[0] = '\0';
3891 current_prompt_string = *prompt_string_pointer;
3892 prompt_string_pointer = &ps2_prompt;
3894 #if defined (READLINE)
3895 if (!no_line_editing)
3897 FREE (current_readline_prompt);
3898 current_readline_prompt = temp_prompt;
3901 #endif /* READLINE */
3903 FREE (current_decoded_prompt);
3904 current_decoded_prompt = temp_prompt;
3909 get_current_prompt_level ()
3911 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
3915 set_current_prompt_level (x)
3918 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
3919 current_prompt_string = *prompt_string_pointer;
3925 fprintf (stderr, "%s", current_decoded_prompt);
3929 /* Return a string which will be printed as a prompt. The string
3930 may contain special characters which are decoded as follows:
3933 \d the date in Day Mon Date format
3934 \e escape (ascii 033)
3935 \h the hostname up to the first `.'
3937 \j the number of active jobs
3938 \l the basename of the shell's tty device name
3941 \s the name of the shell
3942 \t the time in 24-hour hh:mm:ss format
3943 \T the time in 12-hour hh:mm:ss format
3944 \@ the time in 12-hour hh:mm am/pm format
3945 \A the time in 24-hour hh:mm format
3946 \D{fmt} the result of passing FMT to strftime(3)
3948 \v the version of bash (e.g., 2.00)
3949 \V the release of bash, version + patchlevel (e.g., 2.00.0)
3950 \w the current working directory
3951 \W the last element of $PWD
3952 \! the history number of this command
3953 \# the command number of this command
3954 \$ a $ or a # if you are root
3955 \nnn character code nnn in octal
3957 \[ begin a sequence of non-printing chars
3958 \] end a sequence of non-printing chars
3960 #define PROMPT_GROWTH 48
3962 decode_prompt_string (string)
3967 struct dstack save_dstack;
3968 int last_exit_value;
3969 #if defined (PROMPT_STRING_DECODE)
3970 int result_size, result_index;
3972 char *temp, octal_string[4];
3978 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
3979 result[result_index = 0] = 0;
3980 temp = (char *)NULL;
3982 while (c = *string++)
3984 if (posixly_correct && c == '!')
3988 temp = savestring ("!");
3993 #if !defined (HISTORY)
3994 temp = savestring ("1");
3996 temp = itos (history_number ());
3997 #endif /* HISTORY */
3998 string--; /* add_string increments string again. */
4016 strncpy (octal_string, string, 3);
4017 octal_string[3] = '\0';
4019 n = read_octal (octal_string);
4020 temp = (char *)xmalloc (3);
4022 if (n == CTLESC || n == CTLNUL)
4039 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
4042 c = 0; /* tested at add_string: */
4050 /* Make the current time/date into a string. */
4051 (void) time (&the_time);
4052 tm = localtime (&the_time);
4055 n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm);
4057 n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm);
4059 n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm);
4061 n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm);
4063 n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm);
4068 timebuf[sizeof(timebuf) - 1] = '\0';
4070 temp = savestring (timebuf);
4073 case 'D': /* strftime format */
4074 if (string[1] != '{') /* } */
4077 (void) time (&the_time);
4078 tm = localtime (&the_time);
4079 string += 2; /* skip { */
4080 timefmt = xmalloc (strlen (string) + 3);
4081 for (t = timefmt; *string && *string != '}'; )
4084 c = *string; /* tested at add_string */
4085 if (timefmt[0] == '\0')
4088 timefmt[1] = 'X'; /* locale-specific current time */
4091 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
4097 timebuf[sizeof(timebuf) - 1] = '\0';
4099 if (promptvars || posixly_correct)
4100 /* Make sure that expand_prompt_string is called with a
4101 second argument of Q_DOUBLE_QUOTES if we use this
4103 temp = sh_backslash_quote_for_double_quotes (timebuf);
4105 temp = savestring (timebuf);
4109 temp = (char *)xmalloc (3);
4110 temp[0] = no_line_editing ? '\n' : '\r';
4111 temp[1] = no_line_editing ? '\0' : '\n';
4116 temp = base_pathname (shell_name);
4117 temp = savestring (temp);
4122 temp = (char *)xmalloc (16);
4124 strcpy (temp, dist_version);
4126 sprintf (temp, "%s.%d", dist_version, patch_level);
4132 /* Use the value of PWD because it is much more efficient. */
4133 char t_string[PATH_MAX], *t;
4136 temp = get_string_value ("PWD");
4140 if (getcwd (t_string, sizeof(t_string)) == 0)
4146 tlen = strlen (t_string);
4150 tlen = sizeof (t_string) - 1;
4151 strncpy (t_string, temp, tlen);
4153 t_string[tlen] = '\0';
4155 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
4156 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
4157 /* Abbreviate \W as ~ if $PWD == $HOME */
4158 if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0))
4160 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
4162 t = strrchr (t_string, '/');
4164 strcpy (t_string, t + 1);
4168 #undef DOUBLE_SLASH_ROOT
4170 /* polite_directory_format is guaranteed to return a string
4171 no longer than PATH_MAX - 1 characters. */
4172 strcpy (t_string, polite_directory_format (t_string));
4174 /* If we're going to be expanding the prompt string later,
4175 quote the directory name. */
4176 if (promptvars || posixly_correct)
4177 /* Make sure that expand_prompt_string is called with a
4178 second argument of Q_DOUBLE_QUOTES if we use this
4180 temp = sh_backslash_quote_for_double_quotes (t_string);
4182 temp = savestring (t_string);
4188 if (current_user.user_name == 0)
4189 get_current_user_info ();
4190 temp = savestring (current_user.user_name);
4195 temp = savestring (current_host_name);
4196 if (c == 'h' && (t = (char *)strchr (temp, '.')))
4201 temp = itos (current_command_number);
4205 #if !defined (HISTORY)
4206 temp = savestring ("1");
4208 temp = itos (history_number ());
4209 #endif /* HISTORY */
4213 t = temp = (char *)xmalloc (3);
4214 if ((promptvars || posixly_correct) && (current_user.euid != 0))
4216 *t++ = current_user.euid == 0 ? '#' : '$';
4221 temp = itos (count_all_jobs ());
4225 #if defined (HAVE_TTYNAME)
4226 temp = (char *)ttyname (fileno (stdin));
4227 t = temp ? base_pathname (temp) : "tty";
4228 temp = savestring (t);
4230 temp = savestring ("tty");
4231 #endif /* !HAVE_TTYNAME */
4234 #if defined (READLINE)
4237 if (no_line_editing)
4242 temp = (char *)xmalloc (3);
4244 temp[1] = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
4247 #endif /* READLINE */
4253 temp = (char *)xmalloc (2);
4260 else /* (c == '\\') */
4267 temp = (char *)xmalloc (3);
4276 sub_append_string (temp, result, &result_index, &result_size);
4277 temp = (char *)NULL; /* Freed in sub_append_string (). */
4278 result[result_index] = '\0';
4284 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
4285 result[result_index++] = c;
4286 result[result_index] = '\0';
4289 #else /* !PROMPT_STRING_DECODE */
4290 result = savestring (string);
4291 #endif /* !PROMPT_STRING_DECODE */
4293 /* Save the delimiter stack and point `dstack' to temp space so any
4294 command substitutions in the prompt string won't result in screwing
4295 up the parser's quoting state. */
4296 save_dstack = dstack;
4297 dstack = temp_dstack;
4298 dstack.delimiter_depth = 0;
4300 /* Perform variable and parameter expansion and command substitution on
4301 the prompt string. */
4302 if (promptvars || posixly_correct)
4304 last_exit_value = last_command_exit_value;
4305 list = expand_prompt_string (result, Q_DOUBLE_QUOTES);
4307 result = string_list (list);
4308 dispose_words (list);
4309 last_command_exit_value = last_exit_value;
4313 t = dequote_string (result);
4318 dstack = save_dstack;
4323 /************************************************
4327 ************************************************/
4329 /* Report a syntax error, and restart the parser. Call here for fatal
4335 report_syntax_error ((char *)NULL);
4341 error_token_from_token (token)
4346 if (t = find_token_in_alist (token, word_token_alist, 0))
4349 if (t = find_token_in_alist (token, other_token_alist, 0))
4353 /* This stuff is dicy and needs closer inspection */
4354 switch (current_token)
4357 case ASSIGNMENT_WORD:
4359 t = savestring (yylval.word->word);
4362 t = itos (yylval.number);
4365 if (yylval.word_list)
4366 t = string_list (yylval.word_list);
4368 case ARITH_FOR_EXPRS:
4369 if (yylval.word_list)
4370 t = string_list_internal (yylval.word_list, " ; ");
4373 t = (char *)NULL; /* punt */
4381 error_token_from_text ()
4386 t = shell_input_line;
4387 i = shell_input_line_index;
4391 if (i && t[i] == '\0')
4394 while (i && (whitespace (t[i]) || t[i] == '\n'))
4400 while (i && (member (t[i], " \n\t;|&") == 0))
4403 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
4406 /* Return our idea of the offending token. */
4407 if (token_end || (i == 0 && token_end == 0))
4410 msg = substring (t, i, token_end);
4411 else /* one-character token */
4413 msg = (char *)xmalloc (2);
4423 print_offending_line ()
4428 msg = savestring (shell_input_line);
4429 token_end = strlen (msg);
4430 while (token_end && msg[token_end - 1] == '\n')
4431 msg[--token_end] = '\0';
4433 parser_error (line_number, "`%s'", msg);
4437 /* Report a syntax error with line numbers, etc.
4438 Call here for recoverable errors. If you have a message to print,
4439 then place it in MESSAGE, otherwise pass NULL and this will figure
4440 out an appropriate message for you. */
4442 report_syntax_error (message)
4449 parser_error (line_number, "%s", message);
4450 if (interactive && EOF_Reached)
4452 last_command_exit_value = EX_USAGE;
4456 /* If the line of input we're reading is not null, try to find the
4457 objectionable token. First, try to figure out what token the
4458 parser's complaining about by looking at current_token. */
4459 if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
4461 parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
4464 if (interactive == 0)
4465 print_offending_line ();
4467 last_command_exit_value = EX_USAGE;
4471 /* If looking at the current token doesn't prove fruitful, try to find the
4472 offending token by analyzing the text of the input line near the current
4473 input line index and report what we find. */
4474 if (shell_input_line && *shell_input_line)
4476 msg = error_token_from_text ();
4479 parser_error (line_number, _("syntax error near `%s'"), msg);
4483 /* If not interactive, print the line containing the error. */
4484 if (interactive == 0)
4485 print_offending_line ();
4489 msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error");
4490 parser_error (line_number, "%s", msg);
4491 /* When the shell is interactive, this file uses EOF_Reached
4492 only for error reporting. Other mechanisms are used to
4493 decide whether or not to exit. */
4494 if (interactive && EOF_Reached)
4498 last_command_exit_value = EX_USAGE;
4501 /* ??? Needed function. ??? We have to be able to discard the constructs
4502 created during parsing. In the case of error, we want to return
4503 allocated objects to the memory pool. In the case of no error, we want
4504 to throw away the information about where the allocated objects live.
4505 (dispose_command () will actually free the command.) */
4507 discard_parser_constructs (error_p)
4512 /************************************************
4516 ************************************************/
4518 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
4520 /* A flag denoting whether or not ignoreeof is set. */
4523 /* The number of times that we have encountered an EOF character without
4524 another character intervening. When this gets above the limit, the
4525 shell terminates. */
4526 int eof_encountered = 0;
4528 /* The limit for eof_encountered. */
4529 int eof_encountered_limit = 10;
4531 /* If we have EOF as the only input unit, this user wants to leave
4532 the shell. If the shell is not interactive, then just leave.
4533 Otherwise, if ignoreeof is set, and we haven't done this the
4534 required number of times in a row, print a message. */
4536 handle_eof_input_unit ()
4540 /* shell.c may use this to decide whether or not to write out the
4541 history, among other things. We use it only for error reporting
4546 /* If the user wants to "ignore" eof, then let her do so, kind of. */
4549 if (eof_encountered < eof_encountered_limit)
4551 fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
4552 login_shell ? "logout" : "exit");
4554 /* Reset the parsing state. */
4555 last_read_token = current_token = '\n';
4556 /* Reset the prompt string to be $PS1. */
4557 prompt_string_pointer = (char **)NULL;
4563 /* In this case EOF should exit the shell. Do it now. */
4565 exit_builtin ((WORD_LIST *)NULL);
4569 /* We don't write history files, etc., for non-interactive shells. */
4574 /************************************************
4576 * STRING PARSING FUNCTIONS *
4578 ************************************************/
4580 /* It's very important that these two functions treat the characters
4581 between ( and ) identically. */
4583 static WORD_LIST parse_string_error;
4585 /* Take a string and run it through the shell parser, returning the
4586 resultant word list. Used by compound array assignment. */
4588 parse_string_to_word_list (s, flags, whom)
4594 int tok, orig_current_token, orig_line_number, orig_input_terminator;
4595 int orig_line_count;
4596 int old_echo_input, old_expand_aliases;
4597 #if defined (HISTORY)
4598 int old_remember_on_history, old_history_expansion_inhibited;
4601 #if defined (HISTORY)
4602 old_remember_on_history = remember_on_history;
4603 # if defined (BANG_HISTORY)
4604 old_history_expansion_inhibited = history_expansion_inhibited;
4606 bash_history_disable ();
4609 orig_line_number = line_number;
4610 orig_line_count = current_command_line_count;
4611 orig_input_terminator = shell_input_line_terminator;
4612 old_echo_input = echo_input_at_read;
4613 old_expand_aliases = expand_aliases;
4616 last_read_token = WORD; /* WORD to allow reserved words here */
4617 current_command_line_count = 0;
4618 echo_input_at_read = expand_aliases = 0;
4620 with_input_from_string (s, whom);
4621 wl = (WORD_LIST *)NULL;
4624 parser_state |= PST_COMPASSIGN;
4626 while ((tok = read_token (READ)) != yacc_EOF)
4628 if (tok == '\n' && *bash_input.location.string == '\0')
4630 if (tok == '\n') /* Allow newlines in compound assignments */
4632 if (tok != WORD && tok != ASSIGNMENT_WORD)
4634 line_number = orig_line_number + line_number - 1;
4635 orig_current_token = current_token;
4636 current_token = tok;
4637 yyerror (NULL); /* does the right thing */
4638 current_token = orig_current_token;
4641 wl = &parse_string_error;
4644 wl = make_word_list (yylval.word, wl);
4647 last_read_token = '\n';
4650 #if defined (HISTORY)
4651 remember_on_history = old_remember_on_history;
4652 # if defined (BANG_HISTORY)
4653 history_expansion_inhibited = old_history_expansion_inhibited;
4654 # endif /* BANG_HISTORY */
4655 #endif /* HISTORY */
4657 echo_input_at_read = old_echo_input;
4658 expand_aliases = old_expand_aliases;
4660 current_command_line_count = orig_line_count;
4661 shell_input_line_terminator = orig_input_terminator;
4664 parser_state &= ~PST_COMPASSIGN;
4666 if (wl == &parse_string_error)
4668 last_command_exit_value = EXECUTION_FAILURE;
4669 if (interactive_shell == 0 && posixly_correct)
4670 jump_to_top_level (FORCE_EOF);
4672 jump_to_top_level (DISCARD);
4675 return (REVERSE_LIST (wl, WORD_LIST *));
4679 parse_compound_assignment (retlenp)
4683 int tok, orig_line_number, orig_token_size;
4684 char *saved_token, *ret;
4686 saved_token = token;
4687 orig_token_size = token_buffer_size;
4688 orig_line_number = line_number;
4690 last_read_token = WORD; /* WORD to allow reserved words here */
4692 token = (char *)NULL;
4693 token_buffer_size = 0;
4695 wl = (WORD_LIST *)NULL; /* ( */
4696 parser_state |= PST_COMPASSIGN;
4698 while ((tok = read_token (READ)) != ')')
4700 if (tok == '\n') /* Allow newlines in compound assignments */
4702 if (SHOULD_PROMPT ())
4706 if (tok != WORD && tok != ASSIGNMENT_WORD)
4708 current_token = tok; /* for error reporting */
4709 if (tok == yacc_EOF) /* ( */
4710 parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'"));
4712 yyerror(NULL); /* does the right thing */
4715 wl = &parse_string_error;
4718 wl = make_word_list (yylval.word, wl);
4722 token = saved_token;
4723 token_buffer_size = orig_token_size;
4725 parser_state &= ~PST_COMPASSIGN;
4727 if (wl == &parse_string_error)
4729 last_command_exit_value = EXECUTION_FAILURE;
4730 last_read_token = '\n'; /* XXX */
4731 if (interactive_shell == 0 && posixly_correct)
4732 jump_to_top_level (FORCE_EOF);
4734 jump_to_top_level (DISCARD);
4737 last_read_token = WORD;
4740 rl = REVERSE_LIST (wl, WORD_LIST *);
4741 ret = string_list (rl);
4748 *retlenp = (ret && *ret) ? strlen (ret) : 0;
4752 /************************************************
4754 * SAVING AND RESTORING PARTIAL PARSE STATE *
4756 ************************************************/
4759 save_parser_state (ps)
4760 sh_parser_state_t *ps;
4762 #if defined (ARRAY_VARS)
4767 ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t));
4769 return ((sh_parser_state_t *)NULL);
4771 ps->parser_state = parser_state;
4772 ps->token_state = save_token_state ();
4774 ps->input_line_terminator = shell_input_line_terminator;
4775 ps->eof_encountered = eof_encountered;
4777 ps->current_command_line_count = current_command_line_count;
4779 #if defined (HISTORY)
4780 ps->remember_on_history = remember_on_history;
4781 # if defined (BANG_HISTORY)
4782 ps->history_expansion_inhibited = history_expansion_inhibited;
4786 ps->last_command_exit_value = last_command_exit_value;
4787 #if defined (ARRAY_VARS)
4788 v = find_variable ("PIPESTATUS");
4789 if (v && array_p (v) && array_cell (v))
4790 ps->pipestatus = array_copy (array_cell (v));
4792 ps->pipestatus = (ARRAY *)NULL;
4795 ps->last_shell_builtin = last_shell_builtin;
4796 ps->this_shell_builtin = this_shell_builtin;
4798 ps->expand_aliases = expand_aliases;
4799 ps->echo_input_at_read = echo_input_at_read;
4805 restore_parser_state (ps)
4806 sh_parser_state_t *ps;
4808 #if defined (ARRAY_VARS)
4815 parser_state = ps->parser_state;
4816 if (ps->token_state)
4818 restore_token_state (ps->token_state);
4819 free (ps->token_state);
4822 shell_input_line_terminator = ps->input_line_terminator;
4823 eof_encountered = ps->eof_encountered;
4825 current_command_line_count = ps->current_command_line_count;
4827 #if defined (HISTORY)
4828 remember_on_history = ps->remember_on_history;
4829 # if defined (BANG_HISTORY)
4830 history_expansion_inhibited = ps->history_expansion_inhibited;
4834 last_command_exit_value = ps->last_command_exit_value;
4835 #if defined (ARRAY_VARS)
4836 v = find_variable ("PIPESTATUS");
4837 if (v && array_p (v) && array_cell (v))
4839 array_dispose (array_cell (v));
4840 var_setarray (v, ps->pipestatus);
4844 last_shell_builtin = ps->last_shell_builtin;
4845 this_shell_builtin = ps->this_shell_builtin;
4847 expand_aliases = ps->expand_aliases;
4848 echo_input_at_read = ps->echo_input_at_read;
4851 /************************************************
4853 * MULTIBYTE CHARACTER HANDLING *
4855 ************************************************/
4857 #if defined (HANDLE_MULTIBYTE)
4861 int i, previ, len, c;
4862 mbstate_t mbs, prevs;
4865 if (shell_input_line == NULL)
4867 len = strlen (shell_input_line); /* XXX - shell_input_line_len ? */
4868 FREE (shell_input_line_property);
4869 shell_input_line_property = (char *)xmalloc (len + 1);
4871 memset (&prevs, '\0', sizeof (mbstate_t));
4872 for (i = previ = 0; i < len; i++)
4876 c = shell_input_line[i];
4880 for (j = i; j < len; j++)
4881 shell_input_line_property[j] = 1;
4885 mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs);
4886 if (mbclen == 1 || mbclen == (size_t)-1)
4891 else if (mbclen == (size_t)-2)
4893 else if (mbclen > 1)
4901 /* XXX - what to do if mbrlen returns 0? (null wide character) */
4903 for (j = i; j < len; j++)
4904 shell_input_line_property[j] = 1;
4908 shell_input_line_property[i] = mbclen;
4911 #endif /* HANDLE_MULTIBYTE */