1 /* Yacc grammar for bash. */
3 /* Copyright (C) 1989 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"
27 #if defined (HAVE_UNISTD_H)
31 #if defined (HAVE_LOCALE_H)
44 #include "mailcheck.h"
45 #include "builtins/common.h"
46 #include "builtins/builtext.h"
48 #if defined (READLINE)
49 # include "bashline.h"
50 # include <readline/readline.h>
54 # include "bashhist.h"
55 # include <readline/history.h>
58 #if defined (JOB_CONTROL)
60 #endif /* JOB_CONTROL */
66 #if defined (PROMPT_STRING_DECODE)
68 # include <sys/param.h>
72 #endif /* PROMPT_STRING_DECODE */
74 #define RE_READ_TOKEN -99
75 #define NO_EXPANSION -100
79 #if defined (EXTENDED_GLOB)
80 #define PATTERN_CHAR(c) \
81 ((c) == '@' || (c) == '*' || (c) == '+' || (c) == '?' || (c) == '!')
83 extern int extended_glob;
86 extern int eof_encountered;
87 extern int no_line_editing, running_under_emacs;
88 extern int current_command_number;
89 extern int interactive, interactive_shell, login_shell;
90 extern int sourcelevel;
91 extern int posixly_correct;
92 extern int last_command_exit_value;
93 extern int interrupt_immediately;
94 extern char *shell_name, *current_host_name;
95 extern char *dist_version;
96 extern int patch_level;
97 extern int dump_translatable_strings, dump_po_strings;
98 extern Function *last_shell_builtin, *this_shell_builtin;
99 #if defined (BUFFERED_INPUT)
100 extern int bash_input_fd_changed;
104 /* **************************************************************** */
106 /* "Forward" declarations */
108 /* **************************************************************** */
110 static char *ansiexpand ();
111 static char *localeexpand ();
112 static int reserved_word_acceptable ();
113 static int read_token ();
115 static int parse_arith_cmd ();
116 #if defined (COND_COMMAND)
117 static COMMAND *parse_cond_command ();
119 static int read_token_word ();
120 static void discard_parser_constructs ();
122 static void report_syntax_error ();
123 static void handle_eof_input_unit ();
124 static void prompt_again ();
126 static void reset_readline_prompt ();
128 static void print_prompt ();
130 extern int yyerror ();
132 /* Default prompt strings */
133 char *primary_prompt = PPROMPT;
134 char *secondary_prompt = SPROMPT;
136 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
137 char *ps1_prompt, *ps2_prompt;
139 /* Handle on the current prompt string. Indirectly points through
140 ps1_ or ps2_prompt. */
141 char **prompt_string_pointer = (char **)NULL;
142 char *current_prompt_string;
144 /* Non-zero means we expand aliases in commands. */
145 int expand_aliases = 0;
147 /* If non-zero, the decoded prompt string undergoes parameter and
148 variable substitution, command substitution, arithmetic substitution,
149 string expansion, process substitution, and quote removal in
150 decode_prompt_string. */
153 /* The decoded prompt string. Used if READLINE is not defined or if
154 editing is turned off. Analogous to current_readline_prompt. */
155 static char *current_decoded_prompt;
157 /* The number of lines read from input while creating the current command. */
158 int current_command_line_count;
160 /* Variables to manage the task of reading here documents, because we need to
161 defer the reading until after a complete command has been collected. */
162 static REDIRECT *redir_stack[10];
165 /* Where shell input comes from. History expansion is performed on each
166 line when the shell is interactive. */
167 static char *shell_input_line = (char *)NULL;
168 static int shell_input_line_index;
169 static int shell_input_line_size; /* Amount allocated for shell_input_line. */
170 static int shell_input_line_len; /* strlen (shell_input_line) */
172 /* Either zero or EOF. */
173 static int shell_input_line_terminator;
175 /* The line number in a script on which a function definition starts. */
176 static int function_dstart;
178 /* The line number in a script on which a function body starts. */
179 static int function_bstart;
181 /* The line number in a script at which an arithmetic for command starts. */
182 static int arith_for_lineno;
184 static REDIRECTEE redir;
188 WORD_DESC *word; /* the word that we read. */
189 int number; /* the number that we read. */
190 WORD_LIST *word_list;
194 PATTERN_LIST *pattern;
197 /* Reserved words. Members of the first group are only recognized
198 in the case that they are preceded by a list_terminator. Members
199 of the second group are for [[...]] commands. Members of the
200 third group are recognized only under special circumstances. */
201 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION
202 %token COND_START COND_END COND_ERROR
203 %token IN BANG TIME TIMEOPT
205 /* More general tokens. yylex () knows how to make these. */
206 %token <word> WORD ASSIGNMENT_WORD
207 %token <number> NUMBER
208 %token <word_list> ARITH_CMD ARITH_FOR_EXPRS
209 %token <command> COND_CMD
210 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND
211 %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
214 /* The types that the various syntactical units return. */
216 %type <command> inputunit command pipeline pipeline_command
217 %type <command> list list0 list1 compound_list simple_list simple_list1
218 %type <command> simple_command shell_command
219 %type <command> for_command select_command case_command group_command
220 %type <command> arith_command
221 %type <command> cond_command
222 %type <command> arith_for_command
223 %type <command> function_def if_command elif_clause subshell
224 %type <redirect> redirection redirection_list
225 %type <element> simple_command_element
226 %type <word_list> word_list pattern
227 %type <pattern> pattern_list case_clause_sequence case_clause
228 %type <number> timespec
232 %left '&' ';' '\n' yacc_EOF
237 inputunit: simple_list '\n'
239 /* Case of regular command. Discard the error
240 safety net,and return the command just parsed. */
243 discard_parser_constructs (0);
248 /* Case of regular command, but not a very
249 interesting one. Return a NULL command. */
250 global_command = (COMMAND *)NULL;
255 /* Error during parsing. Return NULL command. */
256 global_command = (COMMAND *)NULL;
258 discard_parser_constructs (1);
270 /* Case of EOF seen by itself. Do ignoreeof or
272 global_command = (COMMAND *)NULL;
273 handle_eof_input_unit ();
279 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
281 { $$ = make_word_list ($2, $1); }
284 redirection: '>' WORD
287 $$ = make_redirection (1, r_output_direction, redir);
292 $$ = make_redirection (0, r_input_direction, redir);
297 $$ = make_redirection ($1, r_output_direction, redir);
302 $$ = make_redirection ($1, r_input_direction, redir);
304 | GREATER_GREATER WORD
307 $$ = make_redirection (1, r_appending_to, redir);
309 | NUMBER GREATER_GREATER WORD
312 $$ = make_redirection ($1, r_appending_to, redir);
317 $$ = make_redirection (0, r_reading_until, redir);
318 redir_stack[need_here_doc++] = $$;
320 | NUMBER LESS_LESS WORD
323 $$ = make_redirection ($1, r_reading_until, redir);
324 redir_stack[need_here_doc++] = $$;
329 $$ = make_redirection (0, r_duplicating_input, redir);
331 | NUMBER LESS_AND NUMBER
334 $$ = make_redirection ($1, r_duplicating_input, redir);
339 $$ = make_redirection (1, r_duplicating_output, redir);
341 | NUMBER GREATER_AND NUMBER
344 $$ = make_redirection ($1, r_duplicating_output, redir);
349 $$ = make_redirection (0, r_duplicating_input_word, redir);
351 | NUMBER LESS_AND WORD
354 $$ = make_redirection ($1, r_duplicating_input_word, redir);
359 $$ = make_redirection (1, r_duplicating_output_word, redir);
361 | NUMBER GREATER_AND WORD
364 $$ = make_redirection ($1, r_duplicating_output_word, redir);
366 | LESS_LESS_MINUS WORD
369 $$ = make_redirection
370 (0, r_deblank_reading_until, redir);
371 redir_stack[need_here_doc++] = $$;
373 | NUMBER LESS_LESS_MINUS WORD
376 $$ = make_redirection
377 ($1, r_deblank_reading_until, redir);
378 redir_stack[need_here_doc++] = $$;
383 $$ = make_redirection (1, r_close_this, redir);
385 | NUMBER GREATER_AND '-'
388 $$ = make_redirection ($1, r_close_this, redir);
393 $$ = make_redirection (0, r_close_this, redir);
395 | NUMBER LESS_AND '-'
398 $$ = make_redirection ($1, r_close_this, redir);
403 $$ = make_redirection (1, r_err_and_out, redir);
405 | NUMBER LESS_GREATER WORD
408 $$ = make_redirection ($1, r_input_output, redir);
413 $$ = make_redirection (0, r_input_output, redir);
418 $$ = make_redirection (1, r_output_force, redir);
420 | NUMBER GREATER_BAR WORD
423 $$ = make_redirection ($1, r_output_force, redir);
427 simple_command_element: WORD
428 { $$.word = $1; $$.redirect = 0; }
430 { $$.word = $1; $$.redirect = 0; }
432 { $$.redirect = $1; $$.word = 0; }
435 redirection_list: redirection
439 | redirection_list redirection
441 register REDIRECT *t;
443 for (t = $1; t->next; t = t->next)
450 simple_command: simple_command_element
451 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
452 | simple_command simple_command_element
453 { $$ = make_simple_command ($2, $1); }
456 command: simple_command
457 { $$ = clean_simple_command ($1); }
460 | shell_command redirection_list
465 /* According to Posix.2 3.9.5, redirections
466 specified after the body of a function should
467 be attached to the function and performed when
468 the function is executed, not as part of the
469 function definition command. */
470 /* XXX - I don't think it matters, but we might
471 want to change this in the future to avoid
472 problems differentiating between a function
473 definition with a redirection and a function
474 definition containing a single command with a
475 redirection. The two are semantically equivalent,
476 though -- the only difference is in how the
477 command printing code displays the redirections. */
478 if (tc->type == cm_function_def)
480 tc = tc->value.Function_def->command;
481 if (tc->type == cm_group)
482 tc = tc->value.Group->command;
486 register REDIRECT *t;
487 for (t = tc->redirects; t->next; t = t->next)
497 shell_command: for_command
501 | WHILE compound_list DO compound_list DONE
502 { $$ = make_while_command ($2, $4); }
503 | UNTIL compound_list DO compound_list DONE
504 { $$ = make_until_command ($2, $4); }
523 for_command: FOR WORD newline_list DO compound_list DONE
524 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5); }
525 | FOR WORD newline_list '{' compound_list '}'
526 { $$ = make_for_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5); }
527 | FOR WORD ';' newline_list DO compound_list DONE
528 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
529 | FOR WORD ';' newline_list '{' compound_list '}'
530 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
531 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
532 { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
533 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
534 { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
537 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
538 { $$ = make_arith_for_command ($2, $6, arith_for_lineno); }
539 | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
540 { $$ = make_arith_for_command ($2, $6, arith_for_lineno); }
542 select_command: SELECT WORD newline_list DO list DONE
544 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5);
546 | SELECT WORD newline_list '{' list '}'
548 $$ = make_select_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5);
550 | SELECT WORD ';' newline_list DO list DONE
552 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
554 | SELECT WORD ';' newline_list '{' list '}'
556 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
558 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
560 $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
562 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
564 $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
568 case_command: CASE WORD newline_list IN newline_list ESAC
569 { $$ = make_case_command ($2, (PATTERN_LIST *)NULL); }
570 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
571 { $$ = make_case_command ($2, $5); }
572 | CASE WORD newline_list IN case_clause ESAC
573 { $$ = make_case_command ($2, $5); }
576 function_def: WORD '(' ')' newline_list group_command
577 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
580 | FUNCTION WORD '(' ')' newline_list group_command
581 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
583 | FUNCTION WORD newline_list group_command
584 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
587 subshell: '(' compound_list ')'
589 $$ = make_subshell_command ($2);
590 $$->flags |= CMD_WANT_SUBSHELL;
594 if_command: IF compound_list THEN compound_list FI
595 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
596 | IF compound_list THEN compound_list ELSE compound_list FI
597 { $$ = make_if_command ($2, $4, $6); }
598 | IF compound_list THEN compound_list elif_clause FI
599 { $$ = make_if_command ($2, $4, $5); }
603 group_command: '{' list '}'
604 { $$ = make_group_command ($2); }
607 arith_command: ARITH_CMD
608 { $$ = make_arith_command ($1); }
611 cond_command: COND_START COND_CMD COND_END
615 elif_clause: ELIF compound_list THEN compound_list
616 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
617 | ELIF compound_list THEN compound_list ELSE compound_list
618 { $$ = make_if_command ($2, $4, $6); }
619 | ELIF compound_list THEN compound_list elif_clause
620 { $$ = make_if_command ($2, $4, $5); }
623 case_clause: pattern_list
624 | case_clause_sequence pattern_list
625 { $2->next = $1; $$ = $2; }
628 pattern_list: newline_list pattern ')' compound_list
629 { $$ = make_pattern_list ($2, $4); }
630 | newline_list pattern ')' newline_list
631 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
632 | newline_list '(' pattern ')' compound_list
633 { $$ = make_pattern_list ($3, $5); }
634 | newline_list '(' pattern ')' newline_list
635 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
638 case_clause_sequence: pattern_list SEMI_SEMI
639 | case_clause_sequence pattern_list SEMI_SEMI
640 { $2->next = $1; $$ = $2; }
644 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
646 { $$ = make_word_list ($3, $1); }
649 /* A list allows leading or trailing newlines and
650 newlines as operators (equivalent to semicolons).
651 It must end with a newline or semicolon.
652 Lists are used within commands such as if, for, while. */
654 list: newline_list list0
658 gather_here_documents ();
669 list0: list1 '\n' newline_list
670 | list1 '&' newline_list
672 if ($1->type == cm_connection)
673 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
675 $$ = command_connect ($1, (COMMAND *)NULL, '&');
677 | list1 ';' newline_list
681 list1: list1 AND_AND newline_list list1
682 { $$ = command_connect ($1, $4, AND_AND); }
683 | list1 OR_OR newline_list list1
684 { $$ = command_connect ($1, $4, OR_OR); }
685 | list1 '&' newline_list list1
687 if ($1->type == cm_connection)
688 $$ = connect_async_list ($1, $4, '&');
690 $$ = command_connect ($1, $4, '&');
692 | list1 ';' newline_list list1
693 { $$ = command_connect ($1, $4, ';'); }
694 | list1 '\n' newline_list list1
695 { $$ = command_connect ($1, $4, ';'); }
709 /* A simple_list is a list that contains no significant newlines
710 and no leading or trailing newlines. Newlines are allowed
711 only following operators, where they are not significant.
713 This is what an inputunit consists of. */
715 simple_list: simple_list1
719 gather_here_documents ();
723 if ($1->type == cm_connection)
724 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
726 $$ = command_connect ($1, (COMMAND *)NULL, '&');
728 gather_here_documents ();
734 gather_here_documents ();
738 simple_list1: simple_list1 AND_AND newline_list simple_list1
739 { $$ = command_connect ($1, $4, AND_AND); }
740 | simple_list1 OR_OR newline_list simple_list1
741 { $$ = command_connect ($1, $4, OR_OR); }
742 | simple_list1 '&' simple_list1
744 if ($1->type == cm_connection)
745 $$ = connect_async_list ($1, $3, '&');
747 $$ = command_connect ($1, $3, '&');
749 | simple_list1 ';' simple_list1
750 { $$ = command_connect ($1, $3, ';'); }
756 pipeline_command: pipeline
760 $2->flags |= CMD_INVERT_RETURN;
768 | timespec BANG pipeline
770 $3->flags |= $1|CMD_INVERT_RETURN;
773 | BANG timespec pipeline
775 $3->flags |= $2|CMD_INVERT_RETURN;
781 pipeline '|' newline_list pipeline
782 { $$ = command_connect ($1, $4, '|'); }
788 { $$ = CMD_TIME_PIPELINE; }
790 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
794 /* Possible states for the parser that require it to do special things. */
795 #define PST_CASEPAT 0x001 /* in a case pattern list */
796 #define PST_ALEXPNEXT 0x002 /* expand next word for aliases */
797 #define PST_ALLOWOPNBRC 0x004 /* allow open brace for function def */
798 #define PST_NEEDCLOSBRC 0x008 /* need close brace */
799 #define PST_DBLPAREN 0x010 /* double-paren parsing */
800 #define PST_SUBSHELL 0x020 /* ( ... ) subshell */
801 #define PST_CMDSUBST 0x040 /* $( ... ) command substitution */
802 #define PST_CASESTMT 0x080 /* parsing a case statement */
803 #define PST_CONDCMD 0x100 /* parsing a [[...]] command */
804 #define PST_CONDEXPR 0x200 /* parsing the guts of [[...]] */
805 #define PST_ARITHFOR 0x400 /* parsing an arithmetic for command */
807 /* Initial size to allocate for tokens, and the
808 amount to grow them by. */
809 #define TOKEN_DEFAULT_INITIAL_SIZE 496
810 #define TOKEN_DEFAULT_GROW_SIZE 512
812 /* Shell meta-characters that, when unquoted, separate words. */
813 #define shellmeta(c) (strchr (shell_meta_chars, (c)) != 0)
814 #define shellbreak(c) (strchr (shell_break_chars, (c)) != 0)
815 #define shellquote(c) ((c) == '"' || (c) == '`' || (c) == '\'')
816 #define shellexp(c) ((c) == '$' || (c) == '<' || (c) == '>')
818 char *shell_meta_chars = "()<>;&|";
819 char *shell_break_chars = "()<>;&| \t\n";
821 /* The token currently being read. */
822 static int current_token;
824 /* The last read token, or NULL. read_token () uses this for context
826 static int last_read_token;
828 /* The token read prior to last_read_token. */
829 static int token_before_that;
831 /* The token read prior to token_before_that. */
832 static int two_tokens_ago;
834 /* If non-zero, it is the token that we want read_token to return
835 regardless of what text is (or isn't) present to be read. This
836 is reset by read_token. If token_to_read == WORD or
837 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
838 static int token_to_read;
839 static WORD_DESC *word_desc_to_read;
841 /* The current parser state. */
842 static int parser_state;
844 /* Global var is non-zero when end of file has been reached. */
856 /* yy_getc () returns the next available character from input or EOF.
857 yy_ungetc (c) makes `c' the next character to read.
858 init_yy_io (get, unget, type, location) makes the function GET the
859 installed function for getting the next character, makes UNGET the
860 installed function for un-getting a character, sets the type of stream
861 (either string or file) from TYPE, and makes LOCATION point to where
862 the input is coming from. */
864 /* Unconditionally returns end-of-file. */
871 /* Variable containing the current get and unget functions.
872 See ./input.h for a clearer description. */
873 BASH_INPUT bash_input;
875 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
876 is non-null, avoiding a memory leak. */
878 initialize_bash_input ()
880 bash_input.type = st_none;
881 FREE (bash_input.name);
882 bash_input.name = (char *)NULL;
883 bash_input.location.file = (FILE *)NULL;
884 bash_input.location.string = (char *)NULL;
885 bash_input.getter = (Function *)NULL;
886 bash_input.ungetter = (Function *)NULL;
889 /* Set the contents of the current bash input stream from
890 GET, UNGET, TYPE, NAME, and LOCATION. */
892 init_yy_io (get, unget, type, name, location)
893 Function *get, *unget;
894 enum stream_type type;
896 INPUT_STREAM location;
898 bash_input.type = type;
899 FREE (bash_input.name);
900 bash_input.name = name ? savestring (name) : (char *)NULL;
904 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
906 bash_input.location = location;
908 bash_input.getter = get;
909 bash_input.ungetter = unget;
912 /* Call this to get the next character of input. */
916 return (*(bash_input.getter)) ();
919 /* Call this to unget C. That is, to make C the next character
925 return (*(bash_input.ungetter)) (c);
928 #if defined (BUFFERED_INPUT)
930 input_file_descriptor ()
932 switch (bash_input.type)
935 return (fileno (bash_input.location.file));
937 return (bash_input.location.buffered_fd);
940 return (fileno (stdin));
943 #endif /* BUFFERED_INPUT */
945 /* **************************************************************** */
947 /* Let input be read from readline (). */
949 /* **************************************************************** */
951 #if defined (READLINE)
952 char *current_readline_prompt = (char *)NULL;
953 char *current_readline_line = (char *)NULL;
954 int current_readline_line_index = 0;
959 SigHandler *old_sigint;
962 if (!current_readline_line)
964 if (!bash_readline_initialized)
965 initialize_readline ();
967 #if defined (JOB_CONTROL)
969 give_terminal_to (shell_pgrp);
970 #endif /* JOB_CONTROL */
972 if (signal_is_ignored (SIGINT) == 0)
974 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
975 interrupt_immediately++;
978 current_readline_line = readline (current_readline_prompt ?
979 current_readline_prompt : "");
981 if (signal_is_ignored (SIGINT) == 0)
983 interrupt_immediately--;
984 set_signal_handler (SIGINT, old_sigint);
988 /* Reset the prompt to the decoded value of prompt_string_pointer. */
989 reset_readline_prompt ();
992 if (current_readline_line == 0)
995 current_readline_line_index = 0;
996 line_len = strlen (current_readline_line);
998 current_readline_line = xrealloc (current_readline_line, 2 + line_len);
999 current_readline_line[line_len++] = '\n';
1000 current_readline_line[line_len] = '\0';
1003 if (current_readline_line[current_readline_line_index] == 0)
1005 free (current_readline_line);
1006 current_readline_line = (char *)NULL;
1007 return (yy_readline_get ());
1011 c = (unsigned char)current_readline_line[current_readline_line_index++];
1017 yy_readline_unget (c)
1020 if (current_readline_line_index && current_readline_line)
1021 current_readline_line[--current_readline_line_index] = c;
1026 with_input_from_stdin ()
1028 INPUT_STREAM location;
1030 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1032 location.string = current_readline_line;
1033 init_yy_io (yy_readline_get, yy_readline_unget,
1034 st_stdin, "readline stdin", location);
1038 #else /* !READLINE */
1041 with_input_from_stdin ()
1043 with_input_from_stream (stdin, "stdin");
1045 #endif /* !READLINE */
1047 /* **************************************************************** */
1049 /* Let input come from STRING. STRING is zero terminated. */
1051 /* **************************************************************** */
1056 register char *string;
1059 string = bash_input.location.string;
1062 /* If the string doesn't exist, or is empty, EOF found. */
1063 if (string && *string)
1065 c = *(unsigned char *)string++;
1066 bash_input.location.string = string;
1075 *(--bash_input.location.string) = c;
1080 with_input_from_string (string, name)
1081 char *string, *name;
1083 INPUT_STREAM location;
1085 location.string = string;
1086 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1089 /* **************************************************************** */
1091 /* Let input come from STREAM. */
1093 /* **************************************************************** */
1095 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1096 define, and just use getc/ungetc if it was defined, but since bash
1097 installs its signal handlers without the SA_RESTART flag, some signals
1098 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
1099 the read to be restarted. We need to restart it ourselves. */
1107 if (bash_input.location.file)
1108 result = getc_with_restart (bash_input.location.file);
1117 return (ungetc_with_restart (c, bash_input.location.file));
1121 with_input_from_stream (stream, name)
1125 INPUT_STREAM location;
1127 location.file = stream;
1128 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1131 typedef struct stream_saver {
1132 struct stream_saver *next;
1133 BASH_INPUT bash_input;
1135 #if defined (BUFFERED_INPUT)
1136 BUFFERED_STREAM *bstream;
1137 #endif /* BUFFERED_INPUT */
1140 /* The globally known line number. */
1141 int line_number = 0;
1143 #if defined (COND_COMMAND)
1144 static int cond_lineno;
1145 static int cond_token;
1148 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1151 push_stream (reset_lineno)
1154 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1156 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1158 #if defined (BUFFERED_INPUT)
1159 saver->bstream = (BUFFERED_STREAM *)NULL;
1160 /* If we have a buffered stream, clear out buffers[fd]. */
1161 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1162 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1163 (BUFFERED_STREAM *)NULL);
1164 #endif /* BUFFERED_INPUT */
1166 saver->line = line_number;
1167 bash_input.name = (char *)NULL;
1168 saver->next = stream_list;
1169 stream_list = saver;
1182 STREAM_SAVER *saver = stream_list;
1185 stream_list = stream_list->next;
1187 init_yy_io (saver->bash_input.getter,
1188 saver->bash_input.ungetter,
1189 saver->bash_input.type,
1190 saver->bash_input.name,
1191 saver->bash_input.location);
1193 #if defined (BUFFERED_INPUT)
1194 /* If we have a buffered stream, restore buffers[fd]. */
1195 /* If the input file descriptor was changed while this was on the
1196 save stack, update the buffered fd to the new file descriptor and
1197 re-establish the buffer <-> bash_input fd correspondence. */
1198 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1200 if (bash_input_fd_changed)
1202 bash_input_fd_changed = 0;
1203 if (default_buffered_input >= 0)
1205 bash_input.location.buffered_fd = default_buffered_input;
1206 saver->bstream->b_fd = default_buffered_input;
1209 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1211 #endif /* BUFFERED_INPUT */
1213 line_number = saver->line;
1215 FREE (saver->bash_input.name);
1220 /* Return 1 if a stream of type TYPE is saved on the stack. */
1222 stream_on_stack (type)
1223 enum stream_type type;
1225 register STREAM_SAVER *s;
1227 for (s = stream_list; s; s = s->next)
1228 if (s->bash_input.type == type)
1233 /* Save the current token state and return it in a malloced array. */
1239 ret = (int *)xmalloc (3 * sizeof (int));
1240 ret[0] = last_read_token;
1241 ret[1] = token_before_that;
1242 ret[2] = two_tokens_ago;
1247 restore_token_state (ts)
1252 last_read_token = ts[0];
1253 token_before_that = ts[1];
1254 two_tokens_ago = ts[2];
1258 * This is used to inhibit alias expansion and reserved word recognition
1259 * inside case statement pattern lists. A `case statement pattern list' is:
1261 * everything between the `in' in a `case word in' and the next ')'
1263 * everything between a `;;' and the next `)' or `esac'
1266 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1268 #if !defined (ALIAS)
1269 typedef void *alias_t;
1272 #define END_OF_ALIAS 0
1275 * Pseudo-global variables used in implementing token-wise alias expansion.
1279 * Pushing and popping strings. This works together with shell_getc to
1280 * implement alias expansion on a per-token basis.
1283 typedef struct string_saver {
1284 struct string_saver *next;
1285 int expand_alias; /* Value to set expand_alias to when string is popped. */
1288 alias_t *expander; /* alias that caused this line to be pushed. */
1290 int saved_line_size, saved_line_index, saved_line_terminator;
1293 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1296 * Push the current shell_input_line onto a stack of such lines and make S
1297 * the current input. Used when expanding aliases. EXPAND is used to set
1298 * the value of expand_next_token when the string is popped, so that the
1299 * word after the alias in the original line is handled correctly when the
1300 * alias expands to multiple words. TOKEN is the token that was expanded
1301 * into S; it is saved and used to prevent infinite recursive expansion.
1304 push_string (s, expand, ap)
1309 STRING_SAVER *temp = (STRING_SAVER *) xmalloc (sizeof (STRING_SAVER));
1311 temp->expand_alias = expand;
1312 temp->saved_line = shell_input_line;
1313 temp->saved_line_size = shell_input_line_size;
1314 temp->saved_line_index = shell_input_line_index;
1315 temp->saved_line_terminator = shell_input_line_terminator;
1317 temp->expander = ap;
1319 temp->next = pushed_string_list;
1320 pushed_string_list = temp;
1324 ap->flags |= AL_BEINGEXPANDED;
1327 shell_input_line = s;
1328 shell_input_line_size = strlen (s);
1329 shell_input_line_index = 0;
1330 shell_input_line_terminator = '\0';
1331 parser_state &= ~PST_ALEXPNEXT;
1335 * Make the top of the pushed_string stack be the current shell input.
1336 * Only called when there is something on the stack. Called from shell_getc
1337 * when it thinks it has consumed the string generated by an alias expansion
1338 * and needs to return to the original input line.
1345 FREE (shell_input_line);
1346 shell_input_line = pushed_string_list->saved_line;
1347 shell_input_line_index = pushed_string_list->saved_line_index;
1348 shell_input_line_size = pushed_string_list->saved_line_size;
1349 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1351 if (pushed_string_list->expand_alias)
1352 parser_state |= PST_ALEXPNEXT;
1354 parser_state &= ~PST_ALEXPNEXT;
1356 t = pushed_string_list;
1357 pushed_string_list = pushed_string_list->next;
1361 t->expander->flags &= ~AL_BEINGEXPANDED;
1370 register STRING_SAVER *t, *t1;
1372 for (t = pushed_string_list; t; )
1375 FREE (t->saved_line);
1378 t->expander->flags &= ~AL_BEINGEXPANDED;
1383 pushed_string_list = (STRING_SAVER *)NULL;
1386 #endif /* ALIAS || DPAREN_ARITHMETIC */
1388 /* Return a line of text, taken from wherever yylex () reads input.
1389 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1390 is non-zero, we remove unquoted \<newline> pairs. This is used by
1391 read_secondary_line to read here documents. */
1393 read_a_line (remove_quoted_newline)
1394 int remove_quoted_newline;
1396 static char *line_buffer = (char *)NULL;
1397 static int buffer_size = 0;
1398 int indx = 0, c, peekc, pass_next;
1400 #if defined (READLINE)
1401 if (interactive && bash_input.type != st_string && no_line_editing)
1403 if (interactive && bash_input.type != st_string)
1412 /* Allow immediate exit if interrupted during input. */
1418 /* If there is no more input, then we return NULL. */
1421 if (interactive && bash_input.type == st_stream)
1424 return ((char *)NULL);
1428 /* `+2' in case the final character in the buffer is a newline. */
1429 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
1431 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
1432 here document with an unquoted delimiter. In this case,
1433 the line will be expanded as if it were in double quotes.
1434 We allow a backslash to escape the next character, but we
1435 need to treat the backslash specially only if a backslash
1436 quoting a backslash-newline pair appears in the line. */
1439 line_buffer[indx++] = c;
1442 else if (c == '\\' && remove_quoted_newline)
1446 continue; /* Make the unquoted \<newline> pair disappear. */
1451 line_buffer[indx++] = c; /* Preserve the backslash. */
1455 line_buffer[indx++] = c;
1459 line_buffer[indx] = '\0';
1460 return (line_buffer);
1465 /* Return a line as in read_a_line (), but insure that the prompt is
1466 the secondary prompt. This is used to read the lines of a here
1467 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
1468 newlines quoted with backslashes while reading the line. It is
1469 non-zero unless the delimiter of the here document was quoted. */
1471 read_secondary_line (remove_quoted_newline)
1472 int remove_quoted_newline;
1474 prompt_string_pointer = &ps2_prompt;
1476 return (read_a_line (remove_quoted_newline));
1479 /* **************************************************************** */
1483 /* **************************************************************** */
1485 /* Reserved words. These are only recognized as the first word of a
1487 STRING_INT_ALIST word_token_alist[] = {
1496 #if defined (SELECT_COMMAND)
1497 { "select", SELECT },
1504 { "function", FUNCTION },
1505 #if defined (COMMAND_TIMING)
1511 #if defined (COND_COMMAND)
1512 { "[[", COND_START },
1518 /* XXX - we should also have an alist with strings for other tokens, so we
1519 can give more descriptive error messages. Look at y.tab.h for the
1522 /* These are used by read_token_word, but appear up here so that shell_getc
1523 can use them to decide when to add otherwise blank lines to the history. */
1525 /* The primary delimiter stack. */
1526 struct dstack dstack = { (char *)NULL, 0, 0 };
1528 /* A temporary delimiter stack to be used when decoding prompt strings.
1529 This is needed because command substitutions in prompt strings (e.g., PS2)
1530 can screw up the parser's quoting state. */
1531 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
1533 /* Macro for accessing the top delimiter on the stack. Returns the
1534 delimiter or zero if none. */
1535 #define current_delimiter(ds) \
1536 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
1538 #define push_delimiter(ds, character) \
1541 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
1542 ds.delimiters = xrealloc \
1543 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
1544 ds.delimiters[ds.delimiter_depth] = character; \
1545 ds.delimiter_depth++; \
1549 #define pop_delimiter(ds) ds.delimiter_depth--
1551 /* Return the next shell input character. This always reads characters
1552 from shell_input_line; when that line is exhausted, it is time to
1553 read the next line. This is called by read_token when the shell is
1554 processing normal command input. */
1557 shell_getc (remove_quoted_newline)
1558 int remove_quoted_newline;
1562 static int mustpop = 0;
1566 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1567 /* If shell_input_line[shell_input_line_index] == 0, but there is
1568 something on the pushed list of strings, then we don't want to go
1569 off and get another line. We let the code down below handle it. */
1571 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
1572 (pushed_string_list == (STRING_SAVER *)NULL)))
1573 #else /* !ALIAS && !DPAREN_ARITHMETIC */
1574 if (!shell_input_line || !shell_input_line[shell_input_line_index])
1575 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
1581 /* Allow immediate exit if interrupted during input. */
1585 shell_input_line_terminator = 0;
1587 #if defined (JOB_CONTROL)
1588 /* This can cause a problem when reading a command as the result
1589 of a trap, when the trap is called from flush_child. This call
1590 had better not cause jobs to disappear from the job table in
1591 that case, or we will have big trouble. */
1592 notify_and_cleanup ();
1593 #else /* !JOB_CONTROL */
1594 cleanup_dead_jobs ();
1595 #endif /* !JOB_CONTROL */
1597 #if defined (READLINE)
1598 if (interactive && bash_input.type != st_string && no_line_editing)
1600 if (interactive && bash_input.type != st_string)
1604 if (bash_input.type == st_stream)
1607 while (c = yy_getc ())
1609 /* Allow immediate exit if interrupted during input. */
1612 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
1616 if (bash_input.type == st_stream)
1620 shell_input_line_terminator = EOF;
1622 shell_input_line[i] = '\0';
1626 shell_input_line[i++] = c;
1630 shell_input_line[--i] = '\0';
1631 current_command_line_count++;
1635 shell_input_line_index = 0;
1636 shell_input_line_len = i; /* == strlen (shell_input_line) */
1638 #if defined (HISTORY)
1639 if (remember_on_history && shell_input_line && shell_input_line[0])
1642 # if defined (BANG_HISTORY)
1645 /* If the current delimiter is a single quote, we should not be
1646 performing history expansion, even if we're on a different
1647 line from the original single quote. */
1648 old_hist = history_expansion_inhibited;
1649 if (current_delimiter (dstack) == '\'')
1650 history_expansion_inhibited = 1;
1652 expansions = pre_process_line (shell_input_line, 1, 1);
1653 # if defined (BANG_HISTORY)
1654 history_expansion_inhibited = old_hist;
1656 if (expansions != shell_input_line)
1658 free (shell_input_line);
1659 shell_input_line = expansions;
1660 shell_input_line_len = shell_input_line ?
1661 strlen (shell_input_line) : 0;
1662 if (!shell_input_line_len)
1663 current_command_line_count--;
1665 /* We have to force the xrealloc below because we don't know
1666 the true allocated size of shell_input_line anymore. */
1667 shell_input_line_size = shell_input_line_len;
1670 /* XXX - this is grotesque */
1671 else if (remember_on_history && shell_input_line &&
1672 shell_input_line[0] == '\0' &&
1673 current_command_line_count > 1 && current_delimiter (dstack))
1675 /* We know shell_input_line[0] == 0 and we're reading some sort of
1676 quoted string. This means we've got a line consisting of only
1677 a newline in a quoted string. We want to make sure this line
1678 gets added to the history. */
1679 maybe_add_history (shell_input_line);
1682 #endif /* HISTORY */
1684 if (shell_input_line)
1686 /* Lines that signify the end of the shell's input should not be
1688 if (echo_input_at_read && (shell_input_line[0] ||
1689 shell_input_line_terminator != EOF))
1690 fprintf (stderr, "%s\n", shell_input_line);
1694 shell_input_line_size = 0;
1695 prompt_string_pointer = ¤t_prompt_string;
1700 /* Add the newline to the end of this string, iff the string does
1701 not already end in an EOF character. */
1702 if (shell_input_line_terminator != EOF)
1704 if (shell_input_line_len + 3 > shell_input_line_size)
1705 shell_input_line = xrealloc (shell_input_line,
1706 1 + (shell_input_line_size += 2));
1708 shell_input_line[shell_input_line_len] = '\n';
1709 shell_input_line[shell_input_line_len + 1] = '\0';
1713 c = shell_input_line[shell_input_line_index];
1716 shell_input_line_index++;
1718 if (c == '\\' && remove_quoted_newline &&
1719 shell_input_line[shell_input_line_index] == '\n')
1726 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1727 /* If C is NULL, we have reached the end of the current input string. If
1728 pushed_string_list is non-empty, it's time to pop to the previous string
1729 because we have fully consumed the result of the last alias expansion.
1730 Do it transparently; just return the next character of the string popped
1732 if (!c && (pushed_string_list != (STRING_SAVER *)NULL))
1737 c = shell_input_line[shell_input_line_index];
1739 shell_input_line_index++;
1748 #endif /* ALIAS || DPAREN_ARITHMETIC */
1750 if (!c && shell_input_line_terminator == EOF)
1751 return ((shell_input_line_index != 0) ? '\n' : EOF);
1753 return ((unsigned char)c);
1756 /* Put C back into the input for the shell. */
1761 if (shell_input_line && shell_input_line_index)
1762 shell_input_line[--shell_input_line_index] = c;
1768 if (shell_input_line && shell_input_line_index)
1769 shell_input_line_index--;
1772 /* Discard input until CHARACTER is seen, then push that character back
1773 onto the input stream. */
1775 discard_until (character)
1780 while ((c = shell_getc (0)) != EOF && c != character)
1788 execute_prompt_command (command)
1791 Function *temp_last, *temp_this;
1793 int temp_exit_value, temp_eof_encountered;
1795 temp_last = last_shell_builtin;
1796 temp_this = this_shell_builtin;
1797 temp_exit_value = last_command_exit_value;
1798 temp_eof_encountered = eof_encountered;
1799 last_lastarg = get_string_value ("_");
1801 last_lastarg = savestring (last_lastarg);
1803 parse_and_execute (savestring (command), "PROMPT_COMMAND", SEVAL_NONINT|SEVAL_NOHIST);
1805 last_shell_builtin = temp_last;
1806 this_shell_builtin = temp_this;
1807 last_command_exit_value = temp_exit_value;
1808 eof_encountered = temp_eof_encountered;
1810 bind_variable ("_", last_lastarg);
1811 FREE (last_lastarg);
1813 if (token_to_read == '\n') /* reset_parser was called */
1817 /* Place to remember the token. We try to keep the buffer
1818 at a reasonable size, but it can grow. */
1819 static char *token = (char *)NULL;
1821 /* Current size of the token buffer. */
1822 static int token_buffer_size;
1824 /* Command to read_token () explaining what we want it to do. */
1827 #define prompt_is_ps1 \
1828 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
1830 /* Function for yyparse to call. yylex keeps track of
1831 the last two tokens read, and calls read_token. */
1835 if (interactive && (current_token == 0 || current_token == '\n'))
1837 /* Before we print a prompt, we might have to check mailboxes.
1838 We do this only if it is time to do so. Notice that only here
1839 is the mail alarm reset; nothing takes place in check_mail ()
1840 except the checking of mail. Please don't change this. */
1841 if (prompt_is_ps1 && time_to_check_mail ())
1844 reset_mail_timer ();
1847 /* Avoid printing a prompt if we're not going to read anything, e.g.
1848 after resetting the parser with read_token (RESET). */
1849 if (token_to_read == 0 && interactive)
1853 two_tokens_ago = token_before_that;
1854 token_before_that = last_read_token;
1855 last_read_token = current_token;
1856 current_token = read_token (READ);
1857 return (current_token);
1860 /* When non-zero, we have read the required tokens
1861 which allow ESAC to be the next one read. */
1862 static int esacs_needed_count;
1865 gather_here_documents ()
1868 while (need_here_doc)
1870 make_here_document (redir_stack[r++]);
1875 /* When non-zero, an open-brace used to create a group is awaiting a close
1877 static int open_brace_count;
1879 #define command_token_position(token) \
1880 (((token) == ASSIGNMENT_WORD) || \
1881 ((token) != SEMI_SEMI && reserved_word_acceptable(token)))
1883 #define assignment_acceptable(token) command_token_position(token) && \
1884 ((parser_state & PST_CASEPAT) == 0)
1886 /* Check to see if TOKEN is a reserved word and return the token
1888 #define CHECK_FOR_RESERVED_WORD(tok) \
1890 if (!dollar_present && !quoted && \
1891 reserved_word_acceptable (last_read_token)) \
1894 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
1895 if (STREQ (tok, word_token_alist[i].word)) \
1897 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
1899 if (word_token_alist[i].token == TIME) \
1901 if (word_token_alist[i].token == ESAC) \
1902 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
1903 else if (word_token_alist[i].token == CASE) \
1904 parser_state |= PST_CASESTMT; \
1905 else if (word_token_alist[i].token == COND_END) \
1906 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
1907 else if (word_token_alist[i].token == COND_START) \
1908 parser_state |= PST_CONDCMD; \
1909 else if (word_token_alist[i].token == '{') \
1910 open_brace_count++; \
1911 else if (word_token_alist[i].token == '}' && open_brace_count) \
1912 open_brace_count--; \
1913 return (word_token_alist[i].token); \
1920 /* OK, we have a token. Let's try to alias expand it, if (and only if)
1923 It is eligible for expansion if the shell is in interactive mode, and
1924 the token is unquoted and the last token read was a command
1925 separator (or expand_next_token is set), and we are currently
1926 processing an alias (pushed_string_list is non-empty) and this
1927 token is not the same as the current or any previously
1930 Special cases that disqualify:
1931 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
1933 alias_expand_token (token)
1939 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
1940 (parser_state & PST_CASEPAT) == 0)
1942 ap = find_alias (token);
1944 /* Currently expanding this token. */
1945 if (ap && (ap->flags & AL_BEINGEXPANDED))
1946 return (NO_EXPANSION);
1948 expanded = ap ? savestring (ap->value) : (char *)NULL;
1951 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
1952 return (RE_READ_TOKEN);
1955 /* This is an eligible token that does not have an expansion. */
1956 return (NO_EXPANSION);
1958 return (NO_EXPANSION);
1963 time_command_acceptable ()
1965 #if defined (COMMAND_TIMING)
1966 switch (last_read_token)
1985 #endif /* COMMAND_TIMING */
1988 /* Handle special cases of token recognition:
1989 IN is recognized if the last token was WORD and the token
1990 before that was FOR or CASE or SELECT.
1992 DO is recognized if the last token was WORD and the token
1993 before that was FOR or SELECT.
1995 ESAC is recognized if the last token caused `esacs_needed_count'
1998 `{' is recognized if the last token as WORD and the token
1999 before that was FUNCTION.
2001 `}' is recognized if there is an unclosed `{' prsent.
2003 `-p' is returned as TIMEOPT if the last read token was TIME.
2005 ']]' is returned as COND_END if the parser is currently parsing
2006 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
2008 `time' is returned as TIME if and only if it is immediately
2009 preceded by one of `;', `\n', `||', `&&', or `&'.
2013 special_case_tokens (token)
2016 if ((last_read_token == WORD) &&
2017 #if defined (SELECT_COMMAND)
2018 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
2020 ((token_before_that == FOR) || (token_before_that == CASE)) &&
2022 (token[0] == 'i' && token[1] == 'n' && token[2] == 0))
2024 if (token_before_that == CASE)
2026 parser_state |= PST_CASEPAT;
2027 esacs_needed_count++;
2032 if (last_read_token == WORD &&
2033 #if defined (SELECT_COMMAND)
2034 (token_before_that == FOR || token_before_that == SELECT) &&
2036 (token_before_that == FOR) &&
2038 (token[0] == 'd' && token[1] == 'o' && token[2] == '\0'))
2041 /* Ditto for ESAC in the CASE case.
2042 Specifically, this handles "case word in esac", which is a legal
2043 construct, certainly because someone will pass an empty arg to the
2044 case construct, and we don't want it to barf. Of course, we should
2045 insist that the case construct has at least one pattern in it, but
2046 the designers disagree. */
2047 if (esacs_needed_count)
2049 esacs_needed_count--;
2050 if (STREQ (token, "esac"))
2052 parser_state &= ~PST_CASEPAT;
2057 /* The start of a shell function definition. */
2058 if (parser_state & PST_ALLOWOPNBRC)
2060 parser_state &= ~PST_ALLOWOPNBRC;
2061 if (token[0] == '{' && token[1] == '\0') /* } */
2064 function_bstart = line_number;
2065 return ('{'); /* } */
2069 if (open_brace_count && reserved_word_acceptable (last_read_token) && token[0] == '}' && !token[1])
2071 open_brace_count--; /* { */
2075 #if defined (COMMAND_TIMING)
2076 /* Handle -p after `time'. */
2077 if (last_read_token == TIME && token[0] == '-' && token[1] == 'p' && !token[2])
2081 #if defined (COMMAND_TIMING)
2082 if (STREQ (token, "time") && ((parser_state & PST_CASEPAT) == 0) && time_command_acceptable ())
2084 #endif /* COMMAND_TIMING */
2086 #if defined (COND_COMMAND) /* [[ */
2087 if ((parser_state & PST_CONDEXPR) && token[0] == ']' && token[1] == ']' && token[2] == '\0')
2094 /* Called from shell.c when Control-C is typed at top level. Or
2095 by the error rule at top level. */
2099 dstack.delimiter_depth = 0; /* No delimiters found so far. */
2100 open_brace_count = 0;
2104 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2105 if (pushed_string_list)
2106 free_string_list ();
2107 #endif /* ALIAS || DPAREN_ARITHMETIC */
2109 if (shell_input_line)
2111 free (shell_input_line);
2112 shell_input_line = (char *)NULL;
2113 shell_input_line_size = shell_input_line_index = 0;
2116 FREE (word_desc_to_read);
2117 word_desc_to_read = (WORD_DESC *)NULL;
2119 last_read_token = '\n';
2120 token_to_read = '\n';
2123 /* Read the next token. Command can be READ (normal operation) or
2124 RESET (to normalize state). */
2126 read_token (command)
2129 int character; /* Current character. */
2130 int peek_char; /* Temporary look-ahead character. */
2131 int result; /* The thing to return. */
2133 if (command == RESET)
2141 result = token_to_read;
2142 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
2144 yylval.word = word_desc_to_read;
2145 word_desc_to_read = (WORD_DESC *)NULL;
2151 #if defined (COND_COMMAND)
2152 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
2154 cond_lineno = line_number;
2155 parser_state |= PST_CONDEXPR;
2156 yylval.command = parse_cond_command ();
2157 if (cond_token != COND_END)
2159 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
2160 parser_error (cond_lineno, "unexpected EOF while looking for `]]'");
2161 else if (cond_token != COND_ERROR)
2162 parser_error (cond_lineno, "syntax error in conditional expression");
2165 token_to_read = COND_END;
2166 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
2172 /* This is a place to jump back to once we have successfully expanded a
2173 token with an alias and pushed the string with push_string () */
2177 /* Read a single word from input. Start by skipping blanks. */
2178 while ((character = shell_getc (1)) != EOF && whitespace (character))
2181 if (character == EOF)
2187 if (character == '#' && (!interactive || interactive_comments))
2189 /* A comment. Discard until EOL or EOF, and then return a newline. */
2190 discard_until ('\n');
2192 character = '\n'; /* this will take the next if statement and return. */
2195 if (character == '\n')
2197 /* If we're about to return an unquoted newline, we can go and collect
2198 the text of any pending here document. */
2200 gather_here_documents ();
2203 parser_state &= ~PST_ALEXPNEXT;
2209 /* Shell meta-characters. */
2210 if (shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
2213 /* Turn off alias tokenization iff this character sequence would
2214 not leave us ready to read a command. */
2215 if (character == '<' || character == '>')
2216 parser_state &= ~PST_ALEXPNEXT;
2219 peek_char = shell_getc (1);
2220 if (character == peek_char)
2225 /* If '<' then we could be at "<<" or at "<<-". We have to
2226 look ahead one more character. */
2227 peek_char = shell_getc (1);
2228 if (peek_char == '-')
2229 return (LESS_LESS_MINUS);
2232 shell_ungetc (peek_char);
2237 return (GREATER_GREATER);
2240 parser_state |= PST_CASEPAT;
2242 parser_state &= ~PST_ALEXPNEXT;
2252 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2254 # if defined (ARITH_FOR_COMMAND)
2255 if (last_read_token == FOR)
2261 arith_for_lineno = line_number;
2262 cmdtyp = parse_arith_cmd (&wval);
2265 /* parse_arith_cmd adds quotes at the beginning and end
2266 of the string it returns; we need to take those out. */
2267 len = strlen (wval);
2268 wv2 = xmalloc (len);
2269 strncpy (wv2, wval + 1, len - 2);
2270 wv2[len - 2] = '\0';
2271 wd = make_word (wv2);
2272 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
2275 return (ARITH_FOR_EXPRS);
2278 return -1; /* ERROR */
2281 # if defined (DPAREN_ARITHMETIC)
2282 if (reserved_word_acceptable (last_read_token))
2288 sline = line_number;
2289 cmdtyp = parse_arith_cmd (&wval);
2290 if (cmdtyp == 1) /* arithmetic command */
2292 wd = make_word (wval);
2293 wd->flags = W_QUOTED;
2294 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
2295 free (wval); /* make_word copies it */
2298 else if (cmdtyp == 0) /* nested subshell */
2300 push_string (wval, 0, (alias_t *)NULL);
2301 if ((parser_state & PST_CASEPAT) == 0)
2302 parser_state |= PST_SUBSHELL;
2313 else if (character == '<' && peek_char == '&')
2315 else if (character == '>' && peek_char == '&')
2316 return (GREATER_AND);
2317 else if (character == '<' && peek_char == '>')
2318 return (LESS_GREATER);
2319 else if (character == '>' && peek_char == '|')
2320 return (GREATER_BAR);
2321 else if (peek_char == '>' && character == '&')
2322 return (AND_GREATER);
2324 shell_ungetc (peek_char);
2326 /* If we look like we are reading the start of a function
2327 definition, then let the reader know about it so that
2328 we will do the right thing with `{'. */
2329 if (character == ')' && last_read_token == '(' && token_before_that == WORD)
2331 parser_state |= PST_ALLOWOPNBRC;
2333 parser_state &= ~PST_ALEXPNEXT;
2335 function_dstart = line_number;
2338 /* case pattern lists may be preceded by an optional left paren. If
2339 we're not trying to parse a case pattern list, the left paren
2340 indicates a subshell. */
2341 if (character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
2342 parser_state |= PST_SUBSHELL;
2344 else if ((parser_state & PST_CASEPAT) && character == ')')
2345 parser_state &= ~PST_CASEPAT;
2347 else if ((parser_state & PST_SUBSHELL) && character == ')')
2348 parser_state &= ~PST_SUBSHELL;
2350 #if defined (PROCESS_SUBSTITUTION)
2351 /* Check for the constructs which introduce process substitution.
2352 Shells running in `posix mode' don't do process substitution. */
2353 if (posixly_correct ||
2354 ((character != '>' && character != '<') || peek_char != '('))
2355 #endif /* PROCESS_SUBSTITUTION */
2359 /* Hack <&- (close stdin) case. */
2360 if (character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
2363 /* Okay, if we got this far, we have to read a word. Read one,
2364 and then check it against the known ones. */
2365 result = read_token_word (character);
2367 if (result == RE_READ_TOKEN)
2373 /* Match a $(...) or other grouping construct. This has to handle embedded
2374 quoted strings ('', ``, "") and nested constructs. It also must handle
2375 reprompting the user, if necessary, after reading a newline, and returning
2376 correct error values if it reads EOF. */
2378 #define P_FIRSTCLOSE 0x01
2379 #define P_ALLOWESC 0x02
2381 static char matched_pair_error;
2383 parse_matched_pair (qc, open, close, lenp, flags)
2384 int qc; /* `"' if this construct is within double quotes */
2388 int count, ch, was_dollar;
2389 int pass_next_character, nestlen, start_lineno;
2390 char *ret, *nestret;
2391 int retind, retsize;
2394 pass_next_character = was_dollar = 0;
2396 ret = xmalloc (retsize = 64);
2399 start_lineno = line_number;
2402 ch = shell_getc ((qc != '\'' || (flags & P_ALLOWESC)) && pass_next_character == 0);
2406 parser_error (start_lineno, "unexpected EOF while looking for matching `%c'", close);
2407 EOF_Reached = 1; /* XXX */
2408 return (&matched_pair_error);
2411 /* Possible reprompting. */
2412 if (ch == '\n' && interactive &&
2413 (bash_input.type == st_stdin || bash_input.type == st_stream))
2416 if (pass_next_character) /* last char was backslash */
2418 pass_next_character = 0;
2419 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
2421 if (retind > 0) retind--; /* swallow previously-added backslash */
2425 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2426 if (ch == CTLESC || ch == CTLNUL)
2427 ret[retind++] = CTLESC;
2431 else if (ch == CTLESC || ch == CTLNUL) /* special shell escapes */
2433 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2434 ret[retind++] = CTLESC;
2438 else if (ch == close) /* ending delimiter */
2441 /* handle nested ${...} specially. */
2442 else if (open != close && was_dollar && open == '{' && ch == open) /* } */
2445 else if (((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
2448 /* Add this character. */
2449 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
2452 if (open == '\'') /* '' inside grouping construct */
2454 if ((flags & P_ALLOWESC) && ch == '\\')
2455 pass_next_character++;
2459 if (ch == '\\') /* backslashes */
2460 pass_next_character++;
2462 if (open != close) /* a grouping construct */
2464 if (shellquote (ch))
2466 /* '', ``, or "" inside $(...) or other grouping construct. */
2467 push_delimiter (dstack, ch);
2468 nestret = parse_matched_pair (ch, ch, ch, &nestlen, 0);
2469 pop_delimiter (dstack);
2470 if (nestret == &matched_pair_error)
2473 return &matched_pair_error;
2477 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2478 strcpy (ret + retind, nestret);
2484 /* Parse an old-style command substitution within double quotes as a
2486 /* XXX - sh and ksh93 don't do this - XXX */
2487 else if (open == '"' && ch == '`')
2489 nestret = parse_matched_pair (0, '`', '`', &nestlen, 0);
2490 if (nestret == &matched_pair_error)
2493 return &matched_pair_error;
2497 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2498 strcpy (ret + retind, nestret);
2503 else if (was_dollar && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
2504 /* check for $(), $[], or ${} inside quoted string. */
2506 if (open == ch) /* undo previous increment */
2508 if (ch == '(') /* ) */
2509 nestret = parse_matched_pair (0, '(', ')', &nestlen, 0);
2510 else if (ch == '{') /* } */
2511 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE);
2512 else if (ch == '[') /* ] */
2513 nestret = parse_matched_pair (0, '[', ']', &nestlen, 0);
2514 if (nestret == &matched_pair_error)
2517 return &matched_pair_error;
2521 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2522 strcpy (ret + retind, nestret);
2527 was_dollar = (ch == '$');
2536 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2537 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
2538 If not, assume it's a nested subshell for backwards compatibility and
2539 return 0. In any case, put the characters we've consumed into a locally-
2540 allocated buffer and make *ep point to that buffer. Return -1 on an
2541 error, for example EOF. */
2543 parse_arith_cmd (ep)
2546 int exp_lineno, rval, c;
2550 exp_lineno = line_number;
2551 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
2553 if (ttok == &matched_pair_error)
2555 /* Check that the next character is the closing right paren. If
2556 not, this is a syntax error. ( */
2557 if ((c = shell_getc (0)) != ')')
2560 token = xmalloc (ttoklen + 4);
2562 /* (( ... )) -> "..." */
2563 token[0] = (rval == 1) ? '"' : '(';
2564 strncpy (token + 1, ttok, ttoklen - 1); /* don't copy the final `)' */
2567 token[ttoklen] = '"';
2568 token[ttoklen+1] = '\0';
2572 token[ttoklen] = ')';
2573 token[ttoklen+1] = c;
2574 token[ttoklen+2] = '\0';
2580 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
2582 #if defined (COND_COMMAND)
2583 static COND_COM *cond_term ();
2584 static COND_COM *cond_and ();
2585 static COND_COM *cond_or ();
2586 static COND_COM *cond_expr ();
2591 return (cond_or ());
2600 if (cond_token == OR_OR)
2603 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
2614 if (cond_token == AND_AND)
2617 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
2623 cond_skip_newlines ()
2625 while ((cond_token = read_token (READ)) == '\n')
2627 if (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
2630 return (cond_token);
2633 #define COND_RETURN_ERROR() \
2634 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
2640 COND_COM *term, *tleft, *tright;
2643 /* Read a token. It can be a left paren, a `!', a unary operator, or a
2644 word that should be the first argument of a binary operator. Start by
2645 skipping newlines, since this is a compound command. */
2646 tok = cond_skip_newlines ();
2647 lineno = line_number;
2648 if (tok == COND_END)
2650 COND_RETURN_ERROR ();
2652 else if (tok == '(')
2654 term = cond_expr ();
2655 if (cond_token != ')')
2658 dispose_cond_node (term); /* ( */
2659 parser_error (lineno, "expected `)'");
2660 COND_RETURN_ERROR ();
2662 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
2663 (void)cond_skip_newlines ();
2665 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
2668 dispose_word (yylval.word); /* not needed */
2669 term = cond_term ();
2671 term->flags |= CMD_INVERT_RETURN;
2673 else if (tok == WORD && test_unop (yylval.word->word))
2676 tok = read_token (READ);
2679 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
2680 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
2685 parser_error (line_number, "unexpected argument to conditional unary operator");
2686 COND_RETURN_ERROR ();
2689 (void)cond_skip_newlines ();
2691 else if (tok == WORD) /* left argument to binary operator */
2694 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
2697 tok = read_token (READ);
2698 if (tok == WORD && test_binop (yylval.word->word))
2700 else if (tok == '<' || tok == '>')
2701 op = make_word_from_token (tok);
2702 else if (tok == COND_END || tok == AND_AND || tok == OR_OR)
2704 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
2705 the test command. Similarly for [[ x && expr ]] or
2707 op = make_word ("-n");
2708 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
2714 parser_error (line_number, "conditional binary operator expected");
2715 dispose_cond_node (tleft);
2716 COND_RETURN_ERROR ();
2720 tok = read_token (READ);
2723 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
2724 term = make_cond_node (COND_BINARY, op, tleft, tright);
2728 parser_error (line_number, "unexpected argument to conditional binary operator");
2729 dispose_cond_node (tleft);
2731 COND_RETURN_ERROR ();
2734 (void)cond_skip_newlines ();
2739 parser_error (line_number, "unexpected token `%c' in conditional command", tok);
2741 parser_error (line_number, "unexpected token %d in conditional command", tok);
2742 COND_RETURN_ERROR ();
2747 /* This is kind of bogus -- we slip a mini recursive-descent parser in
2748 here to handle the conditional statement syntax. */
2750 parse_cond_command ()
2754 cexp = cond_expr ();
2755 return (make_cond_command (cexp));
2760 read_token_word (character)
2763 /* The value for YYLVAL when a WORD is read. */
2764 WORD_DESC *the_word;
2766 /* Index into the token that we are building. */
2769 /* ALL_DIGITS becomes zero when we see a non-digit. */
2772 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
2775 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
2778 /* Non-zero means to ignore the value of the next character, and just
2779 to add it no matter what. */
2780 int pass_next_character;
2782 /* The current delimiting character. */
2784 int result, peek_char;
2785 char *ttok, *ttrans;
2786 int ttoklen, ttranslen;
2788 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
2789 token = xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
2792 all_digits = digit (character);
2793 dollar_present = quoted = pass_next_character = 0;
2797 if (character == EOF)
2800 if (pass_next_character)
2802 pass_next_character = 0;
2806 cd = current_delimiter (dstack);
2808 /* Handle backslashes. Quote lots of things when not inside of
2809 double-quotes, quote some things inside of double-quotes. */
2810 if (character == '\\')
2812 peek_char = shell_getc (0);
2814 /* Backslash-newline is ignored in all cases except
2815 when quoted with single quotes. */
2816 if (peek_char == '\n')
2819 goto next_character;
2823 shell_ungetc (peek_char);
2825 /* If the next character is to be quoted, note it now. */
2826 if (cd == 0 || cd == '`' ||
2827 (cd == '"' && member (peek_char, slashify_in_quotes)))
2828 pass_next_character++;
2835 /* Parse a matched pair of quote characters. */
2836 if (shellquote (character))
2838 push_delimiter (dstack, character);
2839 ttok = parse_matched_pair (character, character, character, &ttoklen, 0);
2840 pop_delimiter (dstack);
2841 if (ttok == &matched_pair_error)
2842 return -1; /* Bail immediately. */
2843 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2844 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
2845 token[token_index++] = character;
2846 strcpy (token + token_index, ttok);
2847 token_index += ttoklen;
2850 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
2852 goto next_character;
2855 #ifdef EXTENDED_GLOB
2856 /* Parse a ksh-style extended pattern matching specification. */
2857 if (extended_glob && PATTERN_CHAR(character))
2859 peek_char = shell_getc (1);
2860 if (peek_char == '(') /* ) */
2862 push_delimiter (dstack, peek_char);
2863 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
2864 pop_delimiter (dstack);
2865 if (ttok == &matched_pair_error)
2866 return -1; /* Bail immediately. */
2867 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2869 TOKEN_DEFAULT_GROW_SIZE);
2870 token[token_index++] = character;
2871 token[token_index++] = peek_char;
2872 strcpy (token + token_index, ttok);
2873 token_index += ttoklen;
2875 dollar_present = all_digits = 0;
2876 goto next_character;
2879 shell_ungetc (peek_char);
2881 #endif /* EXTENDED_GLOB */
2883 /* If the delimiter character is not single quote, parse some of
2884 the shell expansions that must be read as a single word. */
2885 #if defined (PROCESS_SUBSTITUTION)
2886 if (character == '$' || character == '<' || character == '>')
2888 if (character == '$')
2889 #endif /* !PROCESS_SUBSTITUTION */
2891 peek_char = shell_getc (1);
2892 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
2893 if (peek_char == '(' ||
2894 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
2896 if (peek_char == '{') /* } */
2897 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE);
2898 else if (peek_char == '(') /* ) */
2900 /* XXX - push and pop the `(' as a delimiter for use by
2901 the command-oriented-history code. This way newlines
2902 appearing in the $(...) string get added to the
2903 history literally rather than causing a possibly-
2904 incorrect `;' to be added. ) */
2905 push_delimiter (dstack, peek_char);
2906 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
2907 pop_delimiter (dstack);
2910 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
2911 if (ttok == &matched_pair_error)
2912 return -1; /* Bail immediately. */
2913 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2915 TOKEN_DEFAULT_GROW_SIZE);
2916 token[token_index++] = character;
2917 token[token_index++] = peek_char;
2918 strcpy (token + token_index, ttok);
2919 token_index += ttoklen;
2923 goto next_character;
2925 /* This handles $'...' and $"..." new-style quoted strings. */
2926 else if (character == '$' && (peek_char == '\'' || peek_char == '"'))
2930 first_line = line_number;
2931 push_delimiter (dstack, peek_char);
2932 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
2934 (peek_char == '\'') ? P_ALLOWESC : 0);
2935 pop_delimiter (dstack);
2936 if (ttok == &matched_pair_error)
2938 if (peek_char == '\'')
2940 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
2942 /* Insert the single quotes and correctly quote any
2943 embedded single quotes (allowed because P_ALLOWESC was
2944 passed to parse_matched_pair). */
2945 ttok = single_quote (ttrans);
2948 ttranslen = strlen (ttrans);
2952 /* Try to locale-expand the converted string. */
2953 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
2956 /* Add the double quotes back */
2957 ttok = xmalloc (ttranslen + 3);
2959 strcpy (ttok + 1, ttrans);
2960 ttok[ttranslen + 1] = '"';
2961 ttok[ttranslen += 2] = '\0';
2966 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2,
2968 TOKEN_DEFAULT_GROW_SIZE);
2969 strcpy (token + token_index, ttrans);
2970 token_index += ttranslen;
2974 goto next_character;
2976 /* This could eventually be extended to recognize all of the
2977 shell's single-character parameter expansions, and set flags.*/
2978 else if (character == '$' && peek_char == '$')
2981 ttok[0] = ttok[1] = '$';
2983 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
2985 TOKEN_DEFAULT_GROW_SIZE);
2986 strcpy (token + token_index, ttok);
2991 goto next_character;
2994 shell_ungetc (peek_char);
2997 #if defined (ARRAY_VARS)
2998 /* Identify possible compound array variable assignment. */
2999 else if (character == '=' && token_index > 0)
3001 peek_char = shell_getc (1);
3002 if (peek_char == '(') /* ) */
3004 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
3005 if (ttok == &matched_pair_error)
3006 return -1; /* Bail immediately. */
3007 if (ttok[0] == '(') /* ) */
3012 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3014 TOKEN_DEFAULT_GROW_SIZE);
3015 token[token_index++] = character;
3016 token[token_index++] = peek_char;
3017 strcpy (token + token_index, ttok);
3018 token_index += ttoklen;
3021 goto next_character;
3024 shell_ungetc (peek_char);
3028 /* When not parsing a multi-character word construct, shell meta-
3029 characters break words. */
3030 if (shellbreak (character))
3032 shell_ungetc (character);
3038 all_digits &= digit (character);
3039 dollar_present |= character == '$';
3041 if (character == CTLESC || character == CTLNUL)
3042 token[token_index++] = CTLESC;
3044 token[token_index++] = character;
3046 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
3047 TOKEN_DEFAULT_GROW_SIZE);
3050 if (character == '\n' && interactive &&
3051 (bash_input.type == st_stdin || bash_input.type == st_stream))
3054 /* We want to remove quoted newlines (that is, a \<newline> pair)
3055 unless we are within single quotes or pass_next_character is
3056 set (the shell equivalent of literal-next). */
3057 cd = current_delimiter (dstack);
3058 character = shell_getc (cd != '\'' && pass_next_character == 0);
3059 } /* end for (;;) */
3063 token[token_index] = '\0';
3065 /* Check to see what thing we should return. If the last_read_token
3066 is a `<', or a `&', or the character which ended this token is
3067 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
3068 Otherwise, it is just a word, and should be returned as such. */
3069 if (all_digits && (character == '<' || character == '>' ||
3070 last_read_token == LESS_AND ||
3071 last_read_token == GREATER_AND))
3073 yylval.number = atoi (token);
3077 /* Check for special case tokens. */
3078 result = special_case_tokens (token);
3083 /* Posix.2 does not allow reserved words to be aliased, so check for all
3084 of them, including special cases, before expanding the current token
3086 if (posixly_correct)
3087 CHECK_FOR_RESERVED_WORD (token);
3089 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
3090 inhibits alias expansion. */
3091 if (expand_aliases && quoted == 0)
3093 result = alias_expand_token (token);
3094 if (result == RE_READ_TOKEN)
3095 return (RE_READ_TOKEN);
3096 else if (result == NO_EXPANSION)
3097 parser_state &= ~PST_ALEXPNEXT;
3100 /* If not in Posix.2 mode, check for reserved words after alias
3102 if (posixly_correct == 0)
3104 CHECK_FOR_RESERVED_WORD (token);
3106 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
3107 the_word->word = xmalloc (1 + token_index);
3108 the_word->flags = 0;
3109 strcpy (the_word->word, token);
3111 the_word->flags |= W_HASDOLLAR;
3113 the_word->flags |= W_QUOTED;
3114 /* A word is an assignment if it appears at the beginning of a
3115 simple command, or after another assignment word. This is
3116 context-dependent, so it cannot be handled in the grammar. */
3117 if (assignment (token))
3119 the_word->flags |= W_ASSIGNMENT;
3120 /* Don't perform word splitting on assignment statements. */
3121 if (assignment_acceptable (last_read_token))
3122 the_word->flags |= W_NOSPLIT;
3125 yylval.word = the_word;
3127 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
3128 ? ASSIGNMENT_WORD : WORD;
3130 if (last_read_token == FUNCTION)
3132 parser_state |= PST_ALLOWOPNBRC;
3133 function_dstart = line_number;
3139 /* $'...' ANSI-C expand the portion of STRING between START and END and
3140 return the result. The result cannot be longer than the input string. */
3142 ansiexpand (string, start, end, lenp)
3144 int start, end, *lenp;
3149 temp = xmalloc (end - start + 1);
3150 for (tlen = 0, len = start; len < end; )
3151 temp[tlen++] = string[len++];
3156 t = ansicstr (temp, tlen, 0, (int *)NULL, lenp);
3168 /* Change a bash string into a string suitable for inclusion in a `po' file.
3169 This backslash-escapes `"' and `\' and changes newlines into \\\n"\n". */
3171 mk_msgstr (string, foundnlp)
3175 register int c, len;
3176 char *result, *r, *s;
3178 for (len = 0, s = string; s && *s; s++)
3181 if (*s == '"' || *s == '\\')
3183 else if (*s == '\n')
3187 r = result = xmalloc (len + 3);
3190 for (s = string; s && (c = *s); s++)
3192 if (c == '\n') /* <NL> -> \n"<NL>" */
3203 if (c == '"' || c == '\\')
3214 /* $"..." -- Translate the portion of STRING between START and END
3215 according to current locale using gettext (if available) and return
3216 the result. The caller will take care of leaving the quotes intact.
3217 The string will be left without the leading `$' by the caller.
3218 If translation is performed, the translated string will be double-quoted
3219 by the caller. The length of the translated string is returned in LENP,
3222 localeexpand (string, start, end, lineno, lenp)
3224 int start, end, lineno, *lenp;
3226 int len, tlen, foundnl;
3227 char *temp, *t, *t2;
3229 temp = xmalloc (end - start + 1);
3230 for (tlen = 0, len = start; len < end; )
3231 temp[tlen++] = string[len++];
3234 /* If we're just dumping translatable strings, don't do anything with the
3235 string itself, but if we're dumping in `po' file format, convert it into a form more palatable to gettext(3)
3236 and friends by quoting `"' and `\' with backslashes and converting <NL>
3237 into `\n"<NL>"'. If we find a newline in TEMP, we first output a
3238 `msgid ""' line and then the translated string; otherwise we output the
3239 `msgid' and translated string all on one line. */
3240 if (dump_translatable_strings)
3242 if (dump_po_strings)
3245 t = mk_msgstr (temp, &foundnl);
3246 t2 = foundnl ? "\"\"\n" : "";
3248 printf ("#: %s:%d\nmsgid %s%s\nmsgstr \"\"\n",
3249 (bash_input.name ? bash_input.name : "stdin"), lineno, t2, t);
3253 printf ("\"%s\"\n", temp);
3261 t = localetrans (temp, tlen, &len);
3275 /* Return 1 if TOKEN is a token that after being read would allow
3276 a reserved word to be seen, else 0. */
3278 reserved_word_acceptable (token)
3281 if (token == '\n' || token == ';' || token == '(' || token == ')' ||
3282 token == '|' || token == '&' || token == '{' ||
3283 token == '}' || /* XXX */
3286 token == TIME || token == TIMEOPT ||
3293 token == SEMI_SEMI ||
3297 token == DONE || /* XXX these two are experimental */
3305 /* Return the index of TOKEN in the alist of reserved words, or -1 if
3306 TOKEN is not a shell reserved word. */
3308 find_reserved_word (token)
3312 for (i = 0; word_token_alist[i].word; i++)
3313 if (STREQ (token, word_token_alist[i].word))
3319 #if defined (READLINE)
3320 /* Called after each time readline is called. This insures that whatever
3321 the new prompt string is gets propagated to readline's local prompt
3324 reset_readline_prompt ()
3328 if (prompt_string_pointer)
3330 temp_prompt = (*prompt_string_pointer)
3331 ? decode_prompt_string (*prompt_string_pointer)
3334 if (temp_prompt == 0)
3336 temp_prompt = xmalloc (1);
3337 temp_prompt[0] = '\0';
3340 FREE (current_readline_prompt);
3341 current_readline_prompt = temp_prompt;
3344 #endif /* READLINE */
3347 #if defined (HISTORY)
3348 /* A list of tokens which can be followed by newlines, but not by
3349 semi-colons. When concatenating multiple lines of history, the
3350 newline separator for such tokens is replaced with a space. */
3351 static int no_semi_successors[] = {
3352 '\n', '{', '(', ')', ';', '&', '|',
3353 CASE, DO, ELSE, IF, SEMI_SEMI, THEN, UNTIL, WHILE, AND_AND, OR_OR, IN,
3357 /* If we are not within a delimited expression, try to be smart
3358 about which separators can be semi-colons and which must be
3359 newlines. Returns the string that should be added into the
3362 history_delimiting_chars ()
3366 if (dstack.delimiter_depth != 0)
3369 /* First, handle some special cases. */
3371 /* If we just read `()', assume it's a function definition, and don't
3372 add a semicolon. If the token before the `)' was not `(', and we're
3373 not in the midst of parsing a case statement, assume it's a
3374 parenthesized command and add the semicolon. */
3376 if (token_before_that == ')')
3378 if (two_tokens_ago == '(') /*)*/ /* function def */
3380 /* This does not work for subshells inside case statement
3381 command lists. It's a suboptimal solution. */
3382 else if (parser_state & PST_CASESTMT) /* case statement pattern */
3385 return "; "; /* (...) subshell */
3387 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
3388 return " "; /* function def using `function name' without `()' */
3390 else if (token_before_that == WORD && two_tokens_ago == FOR)
3392 /* Tricky. `for i\nin ...' should not have a semicolon, but
3393 `for i\ndo ...' should. We do what we can. */
3394 for (i = shell_input_line_index; whitespace(shell_input_line[i]); i++)
3396 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
3401 for (i = 0; no_semi_successors[i]; i++)
3403 if (token_before_that == no_semi_successors[i])
3409 #endif /* HISTORY */
3411 /* Issue a prompt, or prepare to issue a prompt when the next character
3418 if (!interactive) /* XXX */
3421 ps1_prompt = get_string_value ("PS1");
3422 ps2_prompt = get_string_value ("PS2");
3424 if (!prompt_string_pointer)
3425 prompt_string_pointer = &ps1_prompt;
3427 temp_prompt = *prompt_string_pointer
3428 ? decode_prompt_string (*prompt_string_pointer)
3431 if (temp_prompt == 0)
3433 temp_prompt = xmalloc (1);
3434 temp_prompt[0] = '\0';
3437 current_prompt_string = *prompt_string_pointer;
3438 prompt_string_pointer = &ps2_prompt;
3440 #if defined (READLINE)
3441 if (!no_line_editing)
3443 FREE (current_readline_prompt);
3444 current_readline_prompt = temp_prompt;
3447 #endif /* READLINE */
3449 FREE (current_decoded_prompt);
3450 current_decoded_prompt = temp_prompt;
3455 get_current_prompt_level ()
3457 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
3461 set_current_prompt_level (x)
3464 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
3465 current_prompt_string = *prompt_string_pointer;
3471 fprintf (stderr, "%s", current_decoded_prompt);
3475 /* Return a string which will be printed as a prompt. The string
3476 may contain special characters which are decoded as follows:
3479 \e escape (ascii 033)
3480 \d the date in Day Mon Date format
3481 \h the hostname up to the first `.'
3483 \j the number of active jobs
3484 \l the basename of the shell's tty device name
3486 \s the name of the shell
3487 \t the time in 24-hour hh:mm:ss format
3488 \T the time in 12-hour hh:mm:ss format
3489 \@ the time in 12-hour am/pm format
3490 \v the version of bash (e.g., 2.00)
3491 \V the release of bash, version + patchlevel (e.g., 2.00.0)
3492 \w the current working directory
3493 \W the last element of $PWD
3495 \# the command number of this command
3496 \! the history number of this command
3497 \$ a $ or a # if you are root
3498 \nnn character code nnn in octal
3500 \[ begin a sequence of non-printing chars
3501 \] end a sequence of non-printing chars
3503 #define PROMPT_GROWTH 48
3505 decode_prompt_string (string)
3510 struct dstack save_dstack;
3511 #if defined (PROMPT_STRING_DECODE)
3512 int result_size, result_index;
3514 char *temp, octal_string[4];
3517 result = xmalloc (result_size = PROMPT_GROWTH);
3518 result[result_index = 0] = 0;
3519 temp = (char *)NULL;
3521 while (c = *string++)
3523 if (posixly_correct && c == '!')
3527 temp = savestring ("!");
3532 #if !defined (HISTORY)
3533 temp = savestring ("1");
3535 temp = itos (history_number ());
3536 #endif /* HISTORY */
3537 string--; /* add_string increments string again. */
3555 strncpy (octal_string, string, 3);
3556 octal_string[3] = '\0';
3558 n = read_octal (octal_string);
3561 if (n == CTLESC || n == CTLNUL)
3587 /* Make the current time/date into a string. */
3588 the_time = time (0);
3589 temp = ctime (&the_time);
3591 temp = (c != 'd') ? savestring (temp + 11) : savestring (temp);
3592 temp[(c != 'd') ? 8 : 10] = '\0';
3594 /* quick and dirty conversion to 12-hour time */
3595 if (c == 'T' || c == '@')
3599 temp[5] = 'a'; /* am/pm format */
3610 temp[0] = (n / 10) + '0';
3611 temp[1] = (n % 10) + '0';
3613 if (n >= 0 && temp[5] == 'a')
3626 temp[0] = no_line_editing ? '\n' : '\r';
3627 temp[1] = no_line_editing ? '\0' : '\n';
3632 temp = base_pathname (shell_name);
3633 temp = savestring (temp);
3640 strcpy (temp, dist_version);
3642 sprintf (temp, "%s.%d", dist_version, patch_level);
3648 /* Use the value of PWD because it is much more efficient. */
3649 char t_string[PATH_MAX];
3652 temp = get_string_value ("PWD");
3656 if (getcwd (t_string, sizeof(t_string)) == 0)
3662 tlen = strlen (t_string);
3666 tlen = sizeof (t_string) - 1;
3667 strncpy (t_string, temp, tlen);
3669 t_string[tlen] = '\0';
3673 t = strrchr (t_string, '/');
3674 if (t && t != t_string)
3675 strcpy (t_string, t + 1);
3678 /* polite_directory_format is guaranteed to return a string
3679 no longer than PATH_MAX - 1 characters. */
3680 strcpy (t_string, polite_directory_format (t_string));
3682 /* If we're going to be expanding the prompt string later,
3683 quote the directory name. */
3684 if (promptvars || posixly_correct)
3686 temp = backslash_quote (t_string);
3688 /* Make sure that expand_prompt_string is called with a
3689 second argument of Q_DOUBLE_QUOTE if we use this
3691 temp = backslash_quote_for_double_quotes (t_string);
3694 temp = savestring (t_string);
3700 if (current_user.user_name == 0)
3701 get_current_user_info ();
3702 temp = savestring (current_user.user_name);
3707 temp = savestring (current_host_name);
3708 if (c == 'h' && (t = (char *)strchr (temp, '.')))
3713 temp = itos (current_command_number);
3717 #if !defined (HISTORY)
3718 temp = savestring ("1");
3720 temp = itos (history_number ());
3721 #endif /* HISTORY */
3725 t = temp = xmalloc (3);
3726 if ((promptvars || posixly_correct) && (current_user.euid != 0))
3728 *t++ = current_user.euid == 0 ? '#' : '$';
3733 temp = itos (count_all_jobs ());
3737 #if defined (HAVE_TTYNAME)
3738 temp = (char *)ttyname (fileno (stdin));
3739 t = temp ? base_pathname (temp) : "tty";
3740 temp = savestring (t);
3742 temp = savestring ("tty");
3743 #endif /* !HAVE_TTYNAME */
3746 #if defined (READLINE)
3751 temp[1] = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
3754 #endif /* READLINE */
3765 temp[0] = (c == 'a') ? '\07' : '\033';
3779 sub_append_string (temp, result, &result_index, &result_size);
3780 temp = (char *)NULL; /* Freed in sub_append_string (). */
3781 result[result_index] = '\0';
3787 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
3788 result[result_index++] = c;
3789 result[result_index] = '\0';
3792 #else /* !PROMPT_STRING_DECODE */
3793 result = savestring (string);
3794 #endif /* !PROMPT_STRING_DECODE */
3796 /* Save the delimiter stack and point `dstack' to temp space so any
3797 command substitutions in the prompt string won't result in screwing
3798 up the parser's quoting state. */
3799 save_dstack = dstack;
3800 dstack = temp_dstack;
3801 dstack.delimiter_depth = 0;
3803 /* Perform variable and parameter expansion and command substitution on
3804 the prompt string. */
3805 if (promptvars || posixly_correct)
3808 list = expand_string_unsplit (result, Q_DOUBLE_QUOTES);
3810 list = expand_prompt_string (result, Q_DOUBLE_QUOTES);
3813 result = string_list (list);
3814 dispose_words (list);
3818 t = dequote_string (result);
3823 dstack = save_dstack;
3828 /* Report a syntax error, and restart the parser. Call here for fatal
3833 report_syntax_error ((char *)NULL);
3838 /* Report a syntax error with line numbers, etc.
3839 Call here for recoverable errors. If you have a message to print,
3840 then place it in MESSAGE, otherwise pass NULL and this will figure
3841 out an appropriate message for you. */
3843 report_syntax_error (message)
3852 parser_error (line_number, "%s", message);
3853 if (interactive && EOF_Reached)
3855 last_command_exit_value = EX_USAGE;
3859 /* If the line of input we're reading is not null, try to find the
3860 objectionable token. */
3861 if (shell_input_line && *shell_input_line)
3863 t = shell_input_line;
3864 i = shell_input_line_index;
3867 if (i && t[i] == '\0')
3870 while (i && (whitespace (t[i]) || t[i] == '\n'))
3876 while (i && (member (t[i], " \n\t;|&") == 0))
3879 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
3882 /* Print the offending token. */
3883 if (token_end || (i == 0 && token_end == 0))
3886 msg = substring (t, i, token_end);
3887 else /* one-character token */
3894 parser_error (line_number, "syntax error near unexpected token `%s'", msg);
3900 /* If not interactive, print the line containing the error. */
3901 if (interactive == 0)
3903 msg = savestring (shell_input_line);
3904 token_end = strlen (msg);
3905 while (token_end && msg[token_end - 1] == '\n')
3906 msg[--token_end] = '\0';
3908 parser_error (line_number, "`%s'", msg);
3914 msg = EOF_Reached ? "syntax error: unexpected end of file" : "syntax error";
3915 parser_error (line_number, "%s", msg);
3916 /* When the shell is interactive, this file uses EOF_Reached
3917 only for error reporting. Other mechanisms are used to
3918 decide whether or not to exit. */
3919 if (interactive && EOF_Reached)
3922 last_command_exit_value = EX_USAGE;
3925 /* ??? Needed function. ??? We have to be able to discard the constructs
3926 created during parsing. In the case of error, we want to return
3927 allocated objects to the memory pool. In the case of no error, we want
3928 to throw away the information about where the allocated objects live.
3929 (dispose_command () will actually free the command. */
3931 discard_parser_constructs (error_p)
3936 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
3938 /* A flag denoting whether or not ignoreeof is set. */
3941 /* The number of times that we have encountered an EOF character without
3942 another character intervening. When this gets above the limit, the
3943 shell terminates. */
3944 int eof_encountered = 0;
3946 /* The limit for eof_encountered. */
3947 int eof_encountered_limit = 10;
3949 /* If we have EOF as the only input unit, this user wants to leave
3950 the shell. If the shell is not interactive, then just leave.
3951 Otherwise, if ignoreeof is set, and we haven't done this the
3952 required number of times in a row, print a message. */
3954 handle_eof_input_unit ()
3958 /* shell.c may use this to decide whether or not to write out the
3959 history, among other things. We use it only for error reporting
3964 /* If the user wants to "ignore" eof, then let her do so, kind of. */
3967 if (eof_encountered < eof_encountered_limit)
3969 fprintf (stderr, "Use \"%s\" to leave the shell.\n",
3970 login_shell ? "logout" : "exit");
3972 /* Reset the prompt string to be $PS1. */
3973 prompt_string_pointer = (char **)NULL;
3975 last_read_token = current_token = '\n';
3980 /* In this case EOF should exit the shell. Do it now. */
3982 exit_builtin ((WORD_LIST *)NULL);
3986 /* We don't write history files, etc., for non-interactive shells. */
3991 static WORD_LIST parse_string_error;
3993 /* Take a string and run it through the shell parser, returning the
3994 resultant word list. Used by compound array assignment. */
3996 parse_string_to_word_list (s, whom)
4000 int tok, orig_line_number, orig_input_terminator;
4001 #if defined (HISTORY)
4002 int old_remember_on_history, old_history_expansion_inhibited;
4005 #if defined (HISTORY)
4006 old_remember_on_history = remember_on_history;
4007 # if defined (BANG_HISTORY)
4008 old_history_expansion_inhibited = history_expansion_inhibited;
4010 bash_history_disable ();
4013 orig_line_number = line_number;
4014 orig_input_terminator = shell_input_line_terminator;
4017 last_read_token = '\n';
4019 with_input_from_string (s, whom);
4020 wl = (WORD_LIST *)NULL;
4021 while ((tok = read_token (READ)) != yacc_EOF)
4023 if (tok == '\n' && *bash_input.location.string == '\0')
4025 if (tok != WORD && tok != ASSIGNMENT_WORD)
4027 line_number = orig_line_number + line_number - 1;
4028 yyerror (); /* does the right thing */
4031 wl = &parse_string_error;
4034 wl = make_word_list (yylval.word, wl);
4037 last_read_token = '\n';
4040 #if defined (HISTORY)
4041 remember_on_history = old_remember_on_history;
4042 # if defined (BANG_HISTORY)
4043 history_expansion_inhibited = old_history_expansion_inhibited;
4044 # endif /* BANG_HISTORY */
4045 #endif /* HISTORY */
4047 shell_input_line_terminator = orig_input_terminator;
4049 if (wl == &parse_string_error)
4051 last_command_exit_value = EXECUTION_FAILURE;
4052 if (interactive_shell == 0 && posixly_correct)
4053 jump_to_top_level (FORCE_EOF);
4055 jump_to_top_level (DISCARD);
4058 return (REVERSE_LIST (wl, WORD_LIST *));