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"
29 #if defined (HAVE_UNISTD_H)
33 #if defined (HAVE_LOCALE_H)
38 #include "chartypes.h"
47 #include "mailcheck.h"
49 #include "builtins/common.h"
50 #include "builtins/builtext.h"
52 #if defined (READLINE)
53 # include "bashline.h"
54 # include <readline/readline.h>
58 # include "bashhist.h"
59 # include <readline/history.h>
62 #if defined (JOB_CONTROL)
64 #endif /* JOB_CONTROL */
70 #if defined (PROMPT_STRING_DECODE)
72 # include <sys/param.h>
76 #endif /* PROMPT_STRING_DECODE */
78 #define RE_READ_TOKEN -99
79 #define NO_EXPANSION -100
83 #if defined (EXTENDED_GLOB)
84 extern int extended_glob;
87 extern int eof_encountered;
88 extern int no_line_editing, running_under_emacs;
89 extern int current_command_number;
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 sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
99 #if defined (BUFFERED_INPUT)
100 extern int bash_input_fd_changed;
104 /* **************************************************************** */
106 /* "Forward" declarations */
108 /* **************************************************************** */
111 static void debug_parser __P((int));
114 static int yy_getc __P((void));
115 static int yy_ungetc __P((int));
117 #if defined (READLINE)
118 static int yy_readline_get __P((void));
119 static int yy_readline_unget __P((int));
122 static int yy_string_get __P((void));
123 static int yy_string_unget __P((int));
124 static int yy_stream_get __P((void));
125 static int yy_stream_unget __P((int));
127 static int shell_getc __P((int));
128 static void shell_ungetc __P((int));
129 static void discard_until __P((int));
131 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
132 static void push_string __P((char *, int, alias_t *));
133 static void pop_string __P((void));
134 static void free_string_list __P((void));
137 static char *read_a_line __P((int));
139 static char *ansiexpand __P((char *, int, int, int *));
140 static char *mk_msgstr __P((char *, int *));
141 static char *localeexpand __P((char *, int, int, int, int *));
142 static int reserved_word_acceptable __P((int));
143 static int yylex __P((void));
144 static int alias_expand_token __P((char *));
145 static int time_command_acceptable __P((void));
146 static int special_case_tokens __P((char *));
147 static int read_token __P((int));
148 static char *parse_matched_pair __P((int, int, int, int *, int));
149 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
150 static int parse_arith_cmd __P((char **));
152 #if defined (COND_COMMAND)
153 static COND_COM *cond_expr __P((void));
154 static COND_COM *cond_or __P((void));
155 static COND_COM *cond_and __P((void));
156 static COND_COM *cond_term __P((void));
157 static int cond_skip_newlines __P((void));
158 static COMMAND *parse_cond_command __P((void));
160 static int read_token_word __P((int));
161 static void discard_parser_constructs __P((int));
163 static void report_syntax_error __P((char *));
164 static void handle_eof_input_unit __P((void));
165 static void prompt_again __P((void));
167 static void reset_readline_prompt __P((void));
169 static void print_prompt __P((void));
171 #if defined (HISTORY)
172 char *history_delimiting_chars __P((void));
175 extern int yyerror __P((const char *));
177 /* Default prompt strings */
178 char *primary_prompt = PPROMPT;
179 char *secondary_prompt = SPROMPT;
181 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
182 char *ps1_prompt, *ps2_prompt;
184 /* Handle on the current prompt string. Indirectly points through
185 ps1_ or ps2_prompt. */
186 char **prompt_string_pointer = (char **)NULL;
187 char *current_prompt_string;
189 /* Non-zero means we expand aliases in commands. */
190 int expand_aliases = 0;
192 /* If non-zero, the decoded prompt string undergoes parameter and
193 variable substitution, command substitution, arithmetic substitution,
194 string expansion, process substitution, and quote removal in
195 decode_prompt_string. */
198 /* The decoded prompt string. Used if READLINE is not defined or if
199 editing is turned off. Analogous to current_readline_prompt. */
200 static char *current_decoded_prompt;
202 /* The number of lines read from input while creating the current command. */
203 int current_command_line_count;
205 /* Variables to manage the task of reading here documents, because we need to
206 defer the reading until after a complete command has been collected. */
207 static REDIRECT *redir_stack[10];
210 /* Where shell input comes from. History expansion is performed on each
211 line when the shell is interactive. */
212 static char *shell_input_line = (char *)NULL;
213 static int shell_input_line_index;
214 static int shell_input_line_size; /* Amount allocated for shell_input_line. */
215 static int shell_input_line_len; /* strlen (shell_input_line) */
217 /* Either zero or EOF. */
218 static int shell_input_line_terminator;
220 /* The line number in a script on which a function definition starts. */
221 static int function_dstart;
223 /* The line number in a script on which a function body starts. */
224 static int function_bstart;
226 /* The line number in a script at which an arithmetic for command starts. */
227 static int arith_for_lineno;
229 static REDIRECTEE redir;
233 WORD_DESC *word; /* the word that we read. */
234 int number; /* the number that we read. */
235 WORD_LIST *word_list;
239 PATTERN_LIST *pattern;
242 /* Reserved words. Members of the first group are only recognized
243 in the case that they are preceded by a list_terminator. Members
244 of the second group are for [[...]] commands. Members of the
245 third group are recognized only under special circumstances. */
246 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION
247 %token COND_START COND_END COND_ERROR
248 %token IN BANG TIME TIMEOPT
250 /* More general tokens. yylex () knows how to make these. */
251 %token <word> WORD ASSIGNMENT_WORD
252 %token <number> NUMBER
253 %token <word_list> ARITH_CMD ARITH_FOR_EXPRS
254 %token <command> COND_CMD
255 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND
256 %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
259 /* The types that the various syntactical units return. */
261 %type <command> inputunit command pipeline pipeline_command
262 %type <command> list list0 list1 compound_list simple_list simple_list1
263 %type <command> simple_command shell_command
264 %type <command> for_command select_command case_command group_command
265 %type <command> arith_command
266 %type <command> cond_command
267 %type <command> arith_for_command
268 %type <command> function_def function_body if_command elif_clause subshell
269 %type <redirect> redirection redirection_list
270 %type <element> simple_command_element
271 %type <word_list> word_list pattern
272 %type <pattern> pattern_list case_clause_sequence case_clause
273 %type <number> timespec
277 %left '&' ';' '\n' yacc_EOF
282 inputunit: simple_list '\n'
284 /* Case of regular command. Discard the error
285 safety net,and return the command just parsed. */
288 discard_parser_constructs (0);
293 /* Case of regular command, but not a very
294 interesting one. Return a NULL command. */
295 global_command = (COMMAND *)NULL;
300 /* Error during parsing. Return NULL command. */
301 global_command = (COMMAND *)NULL;
303 discard_parser_constructs (1);
315 /* Case of EOF seen by itself. Do ignoreeof or
317 global_command = (COMMAND *)NULL;
318 handle_eof_input_unit ();
324 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
326 { $$ = make_word_list ($2, $1); }
329 redirection: '>' WORD
332 $$ = make_redirection (1, r_output_direction, redir);
337 $$ = make_redirection (0, r_input_direction, redir);
342 $$ = make_redirection ($1, r_output_direction, redir);
347 $$ = make_redirection ($1, r_input_direction, redir);
349 | GREATER_GREATER WORD
352 $$ = make_redirection (1, r_appending_to, redir);
354 | NUMBER GREATER_GREATER WORD
357 $$ = make_redirection ($1, r_appending_to, redir);
362 $$ = make_redirection (0, r_reading_until, redir);
363 redir_stack[need_here_doc++] = $$;
365 | NUMBER LESS_LESS WORD
368 $$ = make_redirection ($1, r_reading_until, redir);
369 redir_stack[need_here_doc++] = $$;
374 $$ = make_redirection (0, r_duplicating_input, redir);
376 | NUMBER LESS_AND NUMBER
379 $$ = make_redirection ($1, r_duplicating_input, redir);
384 $$ = make_redirection (1, r_duplicating_output, redir);
386 | NUMBER GREATER_AND NUMBER
389 $$ = make_redirection ($1, r_duplicating_output, redir);
394 $$ = make_redirection (0, r_duplicating_input_word, redir);
396 | NUMBER LESS_AND WORD
399 $$ = make_redirection ($1, r_duplicating_input_word, redir);
404 $$ = make_redirection (1, r_duplicating_output_word, redir);
406 | NUMBER GREATER_AND WORD
409 $$ = make_redirection ($1, r_duplicating_output_word, redir);
411 | LESS_LESS_MINUS WORD
414 $$ = make_redirection
415 (0, r_deblank_reading_until, redir);
416 redir_stack[need_here_doc++] = $$;
418 | NUMBER LESS_LESS_MINUS WORD
421 $$ = make_redirection
422 ($1, r_deblank_reading_until, redir);
423 redir_stack[need_here_doc++] = $$;
428 $$ = make_redirection (1, r_close_this, redir);
430 | NUMBER GREATER_AND '-'
433 $$ = make_redirection ($1, r_close_this, redir);
438 $$ = make_redirection (0, r_close_this, redir);
440 | NUMBER LESS_AND '-'
443 $$ = make_redirection ($1, r_close_this, redir);
448 $$ = make_redirection (1, r_err_and_out, redir);
450 | NUMBER LESS_GREATER WORD
453 $$ = make_redirection ($1, r_input_output, redir);
458 $$ = make_redirection (0, r_input_output, redir);
463 $$ = make_redirection (1, r_output_force, redir);
465 | NUMBER GREATER_BAR WORD
468 $$ = make_redirection ($1, r_output_force, redir);
472 simple_command_element: WORD
473 { $$.word = $1; $$.redirect = 0; }
475 { $$.word = $1; $$.redirect = 0; }
477 { $$.redirect = $1; $$.word = 0; }
480 redirection_list: redirection
484 | redirection_list redirection
486 register REDIRECT *t;
488 for (t = $1; t->next; t = t->next)
495 simple_command: simple_command_element
496 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
497 | simple_command simple_command_element
498 { $$ = make_simple_command ($2, $1); }
501 command: simple_command
502 { $$ = clean_simple_command ($1); }
505 | shell_command redirection_list
512 register REDIRECT *t;
513 for (t = tc->redirects; t->next; t = t->next)
525 shell_command: for_command
529 | WHILE compound_list DO compound_list DONE
530 { $$ = make_while_command ($2, $4); }
531 | UNTIL compound_list DO compound_list DONE
532 { $$ = make_until_command ($2, $4); }
549 for_command: FOR WORD newline_list DO compound_list DONE
550 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5); }
551 | FOR WORD newline_list '{' compound_list '}'
552 { $$ = make_for_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5); }
553 | FOR WORD ';' newline_list DO compound_list DONE
554 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
555 | FOR WORD ';' newline_list '{' compound_list '}'
556 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
557 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
558 { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
559 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
560 { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
561 | FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
562 { $$ = make_for_command ($2, (WORD_LIST *)NULL, $8); }
563 | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
564 { $$ = make_for_command ($2, (WORD_LIST *)NULL, $8); }
567 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
568 { $$ = make_arith_for_command ($2, $6, arith_for_lineno); }
569 | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
570 { $$ = make_arith_for_command ($2, $6, arith_for_lineno); }
571 | FOR ARITH_FOR_EXPRS DO compound_list DONE
572 { $$ = make_arith_for_command ($2, $4, arith_for_lineno); }
573 | FOR ARITH_FOR_EXPRS '{' compound_list '}'
574 { $$ = make_arith_for_command ($2, $4, arith_for_lineno); }
577 select_command: SELECT WORD newline_list DO list DONE
579 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5);
581 | SELECT WORD newline_list '{' list '}'
583 $$ = make_select_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5);
585 | SELECT WORD ';' newline_list DO list DONE
587 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
589 | SELECT WORD ';' newline_list '{' list '}'
591 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
593 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
595 $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
597 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
599 $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
603 case_command: CASE WORD newline_list IN newline_list ESAC
604 { $$ = make_case_command ($2, (PATTERN_LIST *)NULL); }
605 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
606 { $$ = make_case_command ($2, $5); }
607 | CASE WORD newline_list IN case_clause ESAC
608 { $$ = make_case_command ($2, $5); }
611 function_def: WORD '(' ')' newline_list function_body
612 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
614 | FUNCTION WORD '(' ')' newline_list function_body
615 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
617 | FUNCTION WORD newline_list function_body
618 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
622 function_body: shell_command
624 | shell_command redirection_list
629 /* According to Posix.2 3.9.5, redirections
630 specified after the body of a function should
631 be attached to the function and performed when
632 the function is executed, not as part of the
633 function definition command. */
634 /* XXX - I don't think it matters, but we might
635 want to change this in the future to avoid
636 problems differentiating between a function
637 definition with a redirection and a function
638 definition containing a single command with a
639 redirection. The two are semantically equivalent,
640 though -- the only difference is in how the
641 command printing code displays the redirections. */
644 register REDIRECT *t;
645 for (t = tc->redirects; t->next; t = t->next)
655 subshell: '(' compound_list ')'
657 $$ = make_subshell_command ($2);
658 $$->flags |= CMD_WANT_SUBSHELL;
662 if_command: IF compound_list THEN compound_list FI
663 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
664 | IF compound_list THEN compound_list ELSE compound_list FI
665 { $$ = make_if_command ($2, $4, $6); }
666 | IF compound_list THEN compound_list elif_clause FI
667 { $$ = make_if_command ($2, $4, $5); }
671 group_command: '{' compound_list '}'
672 { $$ = make_group_command ($2); }
675 arith_command: ARITH_CMD
676 { $$ = make_arith_command ($1); }
679 cond_command: COND_START COND_CMD COND_END
683 elif_clause: ELIF compound_list THEN compound_list
684 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
685 | ELIF compound_list THEN compound_list ELSE compound_list
686 { $$ = make_if_command ($2, $4, $6); }
687 | ELIF compound_list THEN compound_list elif_clause
688 { $$ = make_if_command ($2, $4, $5); }
691 case_clause: pattern_list
692 | case_clause_sequence pattern_list
693 { $2->next = $1; $$ = $2; }
696 pattern_list: newline_list pattern ')' compound_list
697 { $$ = make_pattern_list ($2, $4); }
698 | newline_list pattern ')' newline_list
699 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
700 | newline_list '(' pattern ')' compound_list
701 { $$ = make_pattern_list ($3, $5); }
702 | newline_list '(' pattern ')' newline_list
703 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
706 case_clause_sequence: pattern_list SEMI_SEMI
707 | case_clause_sequence pattern_list SEMI_SEMI
708 { $2->next = $1; $$ = $2; }
712 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
714 { $$ = make_word_list ($3, $1); }
717 /* A list allows leading or trailing newlines and
718 newlines as operators (equivalent to semicolons).
719 It must end with a newline or semicolon.
720 Lists are used within commands such as if, for, while. */
722 list: newline_list list0
726 gather_here_documents ();
737 list0: list1 '\n' newline_list
738 | list1 '&' newline_list
740 if ($1->type == cm_connection)
741 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
743 $$ = command_connect ($1, (COMMAND *)NULL, '&');
745 | list1 ';' newline_list
749 list1: list1 AND_AND newline_list list1
750 { $$ = command_connect ($1, $4, AND_AND); }
751 | list1 OR_OR newline_list list1
752 { $$ = command_connect ($1, $4, OR_OR); }
753 | list1 '&' newline_list list1
755 if ($1->type == cm_connection)
756 $$ = connect_async_list ($1, $4, '&');
758 $$ = command_connect ($1, $4, '&');
760 | list1 ';' newline_list list1
761 { $$ = command_connect ($1, $4, ';'); }
762 | list1 '\n' newline_list list1
763 { $$ = command_connect ($1, $4, ';'); }
777 /* A simple_list is a list that contains no significant newlines
778 and no leading or trailing newlines. Newlines are allowed
779 only following operators, where they are not significant.
781 This is what an inputunit consists of. */
783 simple_list: simple_list1
787 gather_here_documents ();
791 if ($1->type == cm_connection)
792 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
794 $$ = command_connect ($1, (COMMAND *)NULL, '&');
796 gather_here_documents ();
802 gather_here_documents ();
806 simple_list1: simple_list1 AND_AND newline_list simple_list1
807 { $$ = command_connect ($1, $4, AND_AND); }
808 | simple_list1 OR_OR newline_list simple_list1
809 { $$ = command_connect ($1, $4, OR_OR); }
810 | simple_list1 '&' simple_list1
812 if ($1->type == cm_connection)
813 $$ = connect_async_list ($1, $3, '&');
815 $$ = command_connect ($1, $3, '&');
817 | simple_list1 ';' simple_list1
818 { $$ = command_connect ($1, $3, ';'); }
824 pipeline_command: pipeline
828 $2->flags |= CMD_INVERT_RETURN;
836 | timespec BANG pipeline
838 $3->flags |= $1|CMD_INVERT_RETURN;
841 | BANG timespec pipeline
843 $3->flags |= $2|CMD_INVERT_RETURN;
849 pipeline '|' newline_list pipeline
850 { $$ = command_connect ($1, $4, '|'); }
856 { $$ = CMD_TIME_PIPELINE; }
858 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
862 /* Possible states for the parser that require it to do special things. */
863 #define PST_CASEPAT 0x001 /* in a case pattern list */
864 #define PST_ALEXPNEXT 0x002 /* expand next word for aliases */
865 #define PST_ALLOWOPNBRC 0x004 /* allow open brace for function def */
866 #define PST_NEEDCLOSBRC 0x008 /* need close brace */
867 #define PST_DBLPAREN 0x010 /* double-paren parsing */
868 #define PST_SUBSHELL 0x020 /* ( ... ) subshell */
869 #define PST_CMDSUBST 0x040 /* $( ... ) command substitution */
870 #define PST_CASESTMT 0x080 /* parsing a case statement */
871 #define PST_CONDCMD 0x100 /* parsing a [[...]] command */
872 #define PST_CONDEXPR 0x200 /* parsing the guts of [[...]] */
873 #define PST_ARITHFOR 0x400 /* parsing an arithmetic for command */
875 /* Initial size to allocate for tokens, and the
876 amount to grow them by. */
877 #define TOKEN_DEFAULT_INITIAL_SIZE 496
878 #define TOKEN_DEFAULT_GROW_SIZE 512
880 /* The token currently being read. */
881 static int current_token;
883 /* The last read token, or NULL. read_token () uses this for context
885 static int last_read_token;
887 /* The token read prior to last_read_token. */
888 static int token_before_that;
890 /* The token read prior to token_before_that. */
891 static int two_tokens_ago;
893 /* If non-zero, it is the token that we want read_token to return
894 regardless of what text is (or isn't) present to be read. This
895 is reset by read_token. If token_to_read == WORD or
896 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
897 static int token_to_read;
898 static WORD_DESC *word_desc_to_read;
900 /* The current parser state. */
901 static int parser_state;
903 /* Global var is non-zero when end of file has been reached. */
917 /* yy_getc () returns the next available character from input or EOF.
918 yy_ungetc (c) makes `c' the next character to read.
919 init_yy_io (get, unget, type, location) makes the function GET the
920 installed function for getting the next character, makes UNGET the
921 installed function for un-getting a character, sets the type of stream
922 (either string or file) from TYPE, and makes LOCATION point to where
923 the input is coming from. */
925 /* Unconditionally returns end-of-file. */
932 /* Variable containing the current get and unget functions.
933 See ./input.h for a clearer description. */
934 BASH_INPUT bash_input;
936 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
937 is non-null, avoiding a memory leak. */
939 initialize_bash_input ()
941 bash_input.type = st_none;
942 FREE (bash_input.name);
943 bash_input.name = (char *)NULL;
944 bash_input.location.file = (FILE *)NULL;
945 bash_input.location.string = (char *)NULL;
946 bash_input.getter = (sh_cget_func_t *)NULL;
947 bash_input.ungetter = (sh_cunget_func_t *)NULL;
950 /* Set the contents of the current bash input stream from
951 GET, UNGET, TYPE, NAME, and LOCATION. */
953 init_yy_io (get, unget, type, name, location)
955 sh_cunget_func_t *unget;
956 enum stream_type type;
958 INPUT_STREAM location;
960 bash_input.type = type;
961 FREE (bash_input.name);
962 bash_input.name = name ? savestring (name) : (char *)NULL;
966 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
968 bash_input.location = location;
970 bash_input.getter = get;
971 bash_input.ungetter = unget;
974 /* Call this to get the next character of input. */
978 return (*(bash_input.getter)) ();
981 /* Call this to unget C. That is, to make C the next character
987 return (*(bash_input.ungetter)) (c);
990 #if defined (BUFFERED_INPUT)
991 #ifdef INCLUDE_UNUSED
993 input_file_descriptor ()
995 switch (bash_input.type)
998 return (fileno (bash_input.location.file));
1000 return (bash_input.location.buffered_fd);
1003 return (fileno (stdin));
1007 #endif /* BUFFERED_INPUT */
1009 /* **************************************************************** */
1011 /* Let input be read from readline (). */
1013 /* **************************************************************** */
1015 #if defined (READLINE)
1016 char *current_readline_prompt = (char *)NULL;
1017 char *current_readline_line = (char *)NULL;
1018 int current_readline_line_index = 0;
1023 SigHandler *old_sigint;
1027 if (!current_readline_line)
1029 if (!bash_readline_initialized)
1030 initialize_readline ();
1032 #if defined (JOB_CONTROL)
1034 give_terminal_to (shell_pgrp, 0);
1035 #endif /* JOB_CONTROL */
1037 old_sigint = (SigHandler *)NULL;
1038 if (signal_is_ignored (SIGINT) == 0)
1040 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
1041 interrupt_immediately++;
1044 current_readline_line = readline (current_readline_prompt ?
1045 current_readline_prompt : "");
1047 if (signal_is_ignored (SIGINT) == 0 && old_sigint)
1049 interrupt_immediately--;
1050 set_signal_handler (SIGINT, old_sigint);
1054 /* Reset the prompt to the decoded value of prompt_string_pointer. */
1055 reset_readline_prompt ();
1058 if (current_readline_line == 0)
1061 current_readline_line_index = 0;
1062 line_len = strlen (current_readline_line);
1064 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
1065 current_readline_line[line_len++] = '\n';
1066 current_readline_line[line_len] = '\0';
1069 if (current_readline_line[current_readline_line_index] == 0)
1071 free (current_readline_line);
1072 current_readline_line = (char *)NULL;
1073 return (yy_readline_get ());
1077 c = current_readline_line[current_readline_line_index++];
1083 yy_readline_unget (c)
1086 if (current_readline_line_index && current_readline_line)
1087 current_readline_line[--current_readline_line_index] = c;
1092 with_input_from_stdin ()
1094 INPUT_STREAM location;
1096 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1098 location.string = current_readline_line;
1099 init_yy_io (yy_readline_get, yy_readline_unget,
1100 st_stdin, "readline stdin", location);
1104 #else /* !READLINE */
1107 with_input_from_stdin ()
1109 with_input_from_stream (stdin, "stdin");
1111 #endif /* !READLINE */
1113 /* **************************************************************** */
1115 /* Let input come from STRING. STRING is zero terminated. */
1117 /* **************************************************************** */
1122 register char *string;
1123 register unsigned char c;
1125 string = bash_input.location.string;
1127 /* If the string doesn't exist, or is empty, EOF found. */
1128 if (string && *string)
1131 bash_input.location.string = string;
1142 *(--bash_input.location.string) = c;
1147 with_input_from_string (string, name)
1151 INPUT_STREAM location;
1153 location.string = string;
1154 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1157 /* **************************************************************** */
1159 /* Let input come from STREAM. */
1161 /* **************************************************************** */
1163 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1164 define, and just use getc/ungetc if it was defined, but since bash
1165 installs its signal handlers without the SA_RESTART flag, some signals
1166 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
1167 the read to be restarted. We need to restart it ourselves. */
1175 if (bash_input.location.file)
1176 result = getc_with_restart (bash_input.location.file);
1185 return (ungetc_with_restart (c, bash_input.location.file));
1189 with_input_from_stream (stream, name)
1193 INPUT_STREAM location;
1195 location.file = stream;
1196 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1199 typedef struct stream_saver {
1200 struct stream_saver *next;
1201 BASH_INPUT bash_input;
1203 #if defined (BUFFERED_INPUT)
1204 BUFFERED_STREAM *bstream;
1205 #endif /* BUFFERED_INPUT */
1208 /* The globally known line number. */
1209 int line_number = 0;
1211 #if defined (COND_COMMAND)
1212 static int cond_lineno;
1213 static int cond_token;
1216 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1219 push_stream (reset_lineno)
1222 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1224 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1226 #if defined (BUFFERED_INPUT)
1227 saver->bstream = (BUFFERED_STREAM *)NULL;
1228 /* If we have a buffered stream, clear out buffers[fd]. */
1229 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1230 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1231 (BUFFERED_STREAM *)NULL);
1232 #endif /* BUFFERED_INPUT */
1234 saver->line = line_number;
1235 bash_input.name = (char *)NULL;
1236 saver->next = stream_list;
1237 stream_list = saver;
1250 STREAM_SAVER *saver = stream_list;
1253 stream_list = stream_list->next;
1255 init_yy_io (saver->bash_input.getter,
1256 saver->bash_input.ungetter,
1257 saver->bash_input.type,
1258 saver->bash_input.name,
1259 saver->bash_input.location);
1261 #if defined (BUFFERED_INPUT)
1262 /* If we have a buffered stream, restore buffers[fd]. */
1263 /* If the input file descriptor was changed while this was on the
1264 save stack, update the buffered fd to the new file descriptor and
1265 re-establish the buffer <-> bash_input fd correspondence. */
1266 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1268 if (bash_input_fd_changed)
1270 bash_input_fd_changed = 0;
1271 if (default_buffered_input >= 0)
1273 bash_input.location.buffered_fd = default_buffered_input;
1274 saver->bstream->b_fd = default_buffered_input;
1275 SET_CLOSE_ON_EXEC (default_buffered_input);
1278 /* XXX could free buffered stream returned as result here. */
1279 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1281 #endif /* BUFFERED_INPUT */
1283 line_number = saver->line;
1285 FREE (saver->bash_input.name);
1290 /* Return 1 if a stream of type TYPE is saved on the stack. */
1292 stream_on_stack (type)
1293 enum stream_type type;
1295 register STREAM_SAVER *s;
1297 for (s = stream_list; s; s = s->next)
1298 if (s->bash_input.type == type)
1303 /* Save the current token state and return it in a malloced array. */
1309 ret = (int *)xmalloc (3 * sizeof (int));
1310 ret[0] = last_read_token;
1311 ret[1] = token_before_that;
1312 ret[2] = two_tokens_ago;
1317 restore_token_state (ts)
1322 last_read_token = ts[0];
1323 token_before_that = ts[1];
1324 two_tokens_ago = ts[2];
1328 * This is used to inhibit alias expansion and reserved word recognition
1329 * inside case statement pattern lists. A `case statement pattern list' is:
1331 * everything between the `in' in a `case word in' and the next ')'
1333 * everything between a `;;' and the next `)' or `esac'
1336 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1338 #if !defined (ALIAS)
1339 typedef void *alias_t;
1342 #define END_OF_ALIAS 0
1345 * Pseudo-global variables used in implementing token-wise alias expansion.
1349 * Pushing and popping strings. This works together with shell_getc to
1350 * implement alias expansion on a per-token basis.
1353 typedef struct string_saver {
1354 struct string_saver *next;
1355 int expand_alias; /* Value to set expand_alias to when string is popped. */
1358 alias_t *expander; /* alias that caused this line to be pushed. */
1360 int saved_line_size, saved_line_index, saved_line_terminator;
1363 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1366 * Push the current shell_input_line onto a stack of such lines and make S
1367 * the current input. Used when expanding aliases. EXPAND is used to set
1368 * the value of expand_next_token when the string is popped, so that the
1369 * word after the alias in the original line is handled correctly when the
1370 * alias expands to multiple words. TOKEN is the token that was expanded
1371 * into S; it is saved and used to prevent infinite recursive expansion.
1374 push_string (s, expand, ap)
1379 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
1381 temp->expand_alias = expand;
1382 temp->saved_line = shell_input_line;
1383 temp->saved_line_size = shell_input_line_size;
1384 temp->saved_line_index = shell_input_line_index;
1385 temp->saved_line_terminator = shell_input_line_terminator;
1387 temp->expander = ap;
1389 temp->next = pushed_string_list;
1390 pushed_string_list = temp;
1394 ap->flags |= AL_BEINGEXPANDED;
1397 shell_input_line = s;
1398 shell_input_line_size = strlen (s);
1399 shell_input_line_index = 0;
1400 shell_input_line_terminator = '\0';
1401 parser_state &= ~PST_ALEXPNEXT;
1405 * Make the top of the pushed_string stack be the current shell input.
1406 * Only called when there is something on the stack. Called from shell_getc
1407 * when it thinks it has consumed the string generated by an alias expansion
1408 * and needs to return to the original input line.
1415 FREE (shell_input_line);
1416 shell_input_line = pushed_string_list->saved_line;
1417 shell_input_line_index = pushed_string_list->saved_line_index;
1418 shell_input_line_size = pushed_string_list->saved_line_size;
1419 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1421 if (pushed_string_list->expand_alias)
1422 parser_state |= PST_ALEXPNEXT;
1424 parser_state &= ~PST_ALEXPNEXT;
1426 t = pushed_string_list;
1427 pushed_string_list = pushed_string_list->next;
1431 t->expander->flags &= ~AL_BEINGEXPANDED;
1440 register STRING_SAVER *t, *t1;
1442 for (t = pushed_string_list; t; )
1445 FREE (t->saved_line);
1448 t->expander->flags &= ~AL_BEINGEXPANDED;
1453 pushed_string_list = (STRING_SAVER *)NULL;
1456 #endif /* ALIAS || DPAREN_ARITHMETIC */
1458 /* Return a line of text, taken from wherever yylex () reads input.
1459 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1460 is non-zero, we remove unquoted \<newline> pairs. This is used by
1461 read_secondary_line to read here documents. */
1463 read_a_line (remove_quoted_newline)
1464 int remove_quoted_newline;
1466 static char *line_buffer = (char *)NULL;
1467 static int buffer_size = 0;
1468 int indx = 0, c, peekc, pass_next;
1470 #if defined (READLINE)
1471 if (interactive && bash_input.type != st_string && no_line_editing)
1473 if (interactive && bash_input.type != st_string)
1482 /* Allow immediate exit if interrupted during input. */
1485 /* Ignore null bytes in input. */
1489 internal_warning ("read_a_line: ignored null byte in input");
1494 /* If there is no more input, then we return NULL. */
1497 if (interactive && bash_input.type == st_stream)
1500 return ((char *)NULL);
1504 /* `+2' in case the final character in the buffer is a newline. */
1505 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
1507 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
1508 here document with an unquoted delimiter. In this case,
1509 the line will be expanded as if it were in double quotes.
1510 We allow a backslash to escape the next character, but we
1511 need to treat the backslash specially only if a backslash
1512 quoting a backslash-newline pair appears in the line. */
1515 line_buffer[indx++] = c;
1518 else if (c == '\\' && remove_quoted_newline)
1522 continue; /* Make the unquoted \<newline> pair disappear. */
1527 line_buffer[indx++] = c; /* Preserve the backslash. */
1531 line_buffer[indx++] = c;
1535 line_buffer[indx] = '\0';
1536 return (line_buffer);
1541 /* Return a line as in read_a_line (), but insure that the prompt is
1542 the secondary prompt. This is used to read the lines of a here
1543 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
1544 newlines quoted with backslashes while reading the line. It is
1545 non-zero unless the delimiter of the here document was quoted. */
1547 read_secondary_line (remove_quoted_newline)
1548 int remove_quoted_newline;
1550 prompt_string_pointer = &ps2_prompt;
1552 return (read_a_line (remove_quoted_newline));
1555 /* **************************************************************** */
1559 /* **************************************************************** */
1561 /* Reserved words. These are only recognized as the first word of a
1563 STRING_INT_ALIST word_token_alist[] = {
1572 #if defined (SELECT_COMMAND)
1573 { "select", SELECT },
1580 { "function", FUNCTION },
1581 #if defined (COMMAND_TIMING)
1587 #if defined (COND_COMMAND)
1588 { "[[", COND_START },
1594 /* XXX - we should also have an alist with strings for other tokens, so we
1595 can give more descriptive error messages. Look at y.tab.h for the
1598 /* These are used by read_token_word, but appear up here so that shell_getc
1599 can use them to decide when to add otherwise blank lines to the history. */
1601 /* The primary delimiter stack. */
1602 struct dstack dstack = { (char *)NULL, 0, 0 };
1604 /* A temporary delimiter stack to be used when decoding prompt strings.
1605 This is needed because command substitutions in prompt strings (e.g., PS2)
1606 can screw up the parser's quoting state. */
1607 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
1609 /* Macro for accessing the top delimiter on the stack. Returns the
1610 delimiter or zero if none. */
1611 #define current_delimiter(ds) \
1612 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
1614 #define push_delimiter(ds, character) \
1617 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
1618 ds.delimiters = (char *)xrealloc \
1619 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
1620 ds.delimiters[ds.delimiter_depth] = character; \
1621 ds.delimiter_depth++; \
1625 #define pop_delimiter(ds) ds.delimiter_depth--
1627 /* Return the next shell input character. This always reads characters
1628 from shell_input_line; when that line is exhausted, it is time to
1629 read the next line. This is called by read_token when the shell is
1630 processing normal command input. */
1632 /* This implements one-character lookahead/lookbehind across physical input
1633 lines, to avoid something being lost because it's pushed back with
1634 shell_ungetc when we're at the start of a line. */
1635 static int eol_ungetc_lookahead = 0;
1638 shell_getc (remove_quoted_newline)
1639 int remove_quoted_newline;
1644 static int mustpop = 0;
1648 if (eol_ungetc_lookahead)
1650 c = eol_ungetc_lookahead;
1651 eol_ungetc_lookahead = 0;
1655 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1656 /* If shell_input_line[shell_input_line_index] == 0, but there is
1657 something on the pushed list of strings, then we don't want to go
1658 off and get another line. We let the code down below handle it. */
1660 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
1661 (pushed_string_list == (STRING_SAVER *)NULL)))
1662 #else /* !ALIAS && !DPAREN_ARITHMETIC */
1663 if (!shell_input_line || !shell_input_line[shell_input_line_index])
1664 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
1670 /* Allow immediate exit if interrupted during input. */
1674 shell_input_line_terminator = 0;
1676 #if defined (JOB_CONTROL)
1677 /* This can cause a problem when reading a command as the result
1678 of a trap, when the trap is called from flush_child. This call
1679 had better not cause jobs to disappear from the job table in
1680 that case, or we will have big trouble. */
1681 notify_and_cleanup ();
1682 #else /* !JOB_CONTROL */
1683 cleanup_dead_jobs ();
1684 #endif /* !JOB_CONTROL */
1686 #if defined (READLINE)
1687 if (interactive && bash_input.type != st_string && no_line_editing)
1689 if (interactive && bash_input.type != st_string)
1693 if (bash_input.type == st_stream)
1700 /* Allow immediate exit if interrupted during input. */
1706 internal_warning ("shell_getc: ignored null byte in input");
1711 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
1715 if (bash_input.type == st_stream)
1719 shell_input_line_terminator = EOF;
1721 shell_input_line[i] = '\0';
1725 shell_input_line[i++] = c;
1729 shell_input_line[--i] = '\0';
1730 current_command_line_count++;
1735 shell_input_line_index = 0;
1736 shell_input_line_len = i; /* == strlen (shell_input_line) */
1738 #if defined (HISTORY)
1739 if (remember_on_history && shell_input_line && shell_input_line[0])
1742 # if defined (BANG_HISTORY)
1745 /* If the current delimiter is a single quote, we should not be
1746 performing history expansion, even if we're on a different
1747 line from the original single quote. */
1748 old_hist = history_expansion_inhibited;
1749 if (current_delimiter (dstack) == '\'')
1750 history_expansion_inhibited = 1;
1752 expansions = pre_process_line (shell_input_line, 1, 1);
1753 # if defined (BANG_HISTORY)
1754 history_expansion_inhibited = old_hist;
1756 if (expansions != shell_input_line)
1758 free (shell_input_line);
1759 shell_input_line = expansions;
1760 shell_input_line_len = shell_input_line ?
1761 strlen (shell_input_line) : 0;
1762 if (!shell_input_line_len)
1763 current_command_line_count--;
1765 /* We have to force the xrealloc below because we don't know
1766 the true allocated size of shell_input_line anymore. */
1767 shell_input_line_size = shell_input_line_len;
1770 /* Try to do something intelligent with blank lines encountered while
1771 entering multi-line commands. XXX - this is grotesque */
1772 else if (remember_on_history && shell_input_line &&
1773 shell_input_line[0] == '\0' &&
1774 current_command_line_count > 1)
1776 if (current_delimiter (dstack))
1777 /* We know shell_input_line[0] == 0 and we're reading some sort of
1778 quoted string. This means we've got a line consisting of only
1779 a newline in a quoted string. We want to make sure this line
1780 gets added to the history. */
1781 maybe_add_history (shell_input_line);
1785 hdcs = history_delimiting_chars ();
1786 if (hdcs && hdcs[0] == ';')
1787 maybe_add_history (shell_input_line);
1791 #endif /* HISTORY */
1793 if (shell_input_line)
1795 /* Lines that signify the end of the shell's input should not be
1797 if (echo_input_at_read && (shell_input_line[0] ||
1798 shell_input_line_terminator != EOF))
1799 fprintf (stderr, "%s\n", shell_input_line);
1803 shell_input_line_size = 0;
1804 prompt_string_pointer = ¤t_prompt_string;
1809 /* Add the newline to the end of this string, iff the string does
1810 not already end in an EOF character. */
1811 if (shell_input_line_terminator != EOF)
1813 if (shell_input_line_len + 3 > shell_input_line_size)
1814 shell_input_line = (char *)xrealloc (shell_input_line,
1815 1 + (shell_input_line_size += 2));
1817 shell_input_line[shell_input_line_len] = '\n';
1818 shell_input_line[shell_input_line_len + 1] = '\0';
1822 uc = shell_input_line[shell_input_line_index];
1825 shell_input_line_index++;
1827 if (uc == '\\' && remove_quoted_newline &&
1828 shell_input_line[shell_input_line_index] == '\n')
1835 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1836 /* If UC is NULL, we have reached the end of the current input string. If
1837 pushed_string_list is non-empty, it's time to pop to the previous string
1838 because we have fully consumed the result of the last alias expansion.
1839 Do it transparently; just return the next character of the string popped
1841 if (!uc && (pushed_string_list != (STRING_SAVER *)NULL))
1846 uc = shell_input_line[shell_input_line_index];
1848 shell_input_line_index++;
1857 #endif /* ALIAS || DPAREN_ARITHMETIC */
1859 if (!uc && shell_input_line_terminator == EOF)
1860 return ((shell_input_line_index != 0) ? '\n' : EOF);
1865 /* Put C back into the input for the shell. */
1870 if (shell_input_line && shell_input_line_index)
1871 shell_input_line[--shell_input_line_index] = c;
1873 eol_ungetc_lookahead = c;
1876 #ifdef INCLUDE_UNUSED
1877 /* Back the input pointer up by one, effectively `ungetting' a character. */
1881 if (shell_input_line && shell_input_line_index)
1882 shell_input_line_index--;
1886 /* Discard input until CHARACTER is seen, then push that character back
1887 onto the input stream. */
1889 discard_until (character)
1894 while ((c = shell_getc (0)) != EOF && c != character)
1902 execute_prompt_command (command)
1905 sh_builtin_func_t *temp_last, *temp_this;
1907 int temp_exit_value, temp_eof_encountered;
1909 temp_last = last_shell_builtin;
1910 temp_this = this_shell_builtin;
1911 temp_exit_value = last_command_exit_value;
1912 temp_eof_encountered = eof_encountered;
1913 last_lastarg = get_string_value ("_");
1915 last_lastarg = savestring (last_lastarg);
1917 parse_and_execute (savestring (command), "PROMPT_COMMAND", SEVAL_NONINT|SEVAL_NOHIST);
1919 last_shell_builtin = temp_last;
1920 this_shell_builtin = temp_this;
1921 last_command_exit_value = temp_exit_value;
1922 eof_encountered = temp_eof_encountered;
1924 bind_variable ("_", last_lastarg);
1925 FREE (last_lastarg);
1927 if (token_to_read == '\n') /* reset_parser was called */
1931 /* Place to remember the token. We try to keep the buffer
1932 at a reasonable size, but it can grow. */
1933 static char *token = (char *)NULL;
1935 /* Current size of the token buffer. */
1936 static int token_buffer_size;
1938 /* Command to read_token () explaining what we want it to do. */
1941 #define prompt_is_ps1 \
1942 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
1944 /* Function for yyparse to call. yylex keeps track of
1945 the last two tokens read, and calls read_token. */
1949 if (interactive && (current_token == 0 || current_token == '\n'))
1951 /* Before we print a prompt, we might have to check mailboxes.
1952 We do this only if it is time to do so. Notice that only here
1953 is the mail alarm reset; nothing takes place in check_mail ()
1954 except the checking of mail. Please don't change this. */
1955 if (prompt_is_ps1 && time_to_check_mail ())
1958 reset_mail_timer ();
1961 /* Avoid printing a prompt if we're not going to read anything, e.g.
1962 after resetting the parser with read_token (RESET). */
1963 if (token_to_read == 0 && interactive)
1967 two_tokens_ago = token_before_that;
1968 token_before_that = last_read_token;
1969 last_read_token = current_token;
1970 current_token = read_token (READ);
1971 return (current_token);
1974 /* When non-zero, we have read the required tokens
1975 which allow ESAC to be the next one read. */
1976 static int esacs_needed_count;
1979 gather_here_documents ()
1982 while (need_here_doc)
1984 make_here_document (redir_stack[r++]);
1989 /* When non-zero, an open-brace used to create a group is awaiting a close
1991 static int open_brace_count;
1993 #define command_token_position(token) \
1994 (((token) == ASSIGNMENT_WORD) || \
1995 ((token) != SEMI_SEMI && reserved_word_acceptable(token)))
1997 #define assignment_acceptable(token) command_token_position(token) && \
1998 ((parser_state & PST_CASEPAT) == 0)
2000 /* Check to see if TOKEN is a reserved word and return the token
2002 #define CHECK_FOR_RESERVED_WORD(tok) \
2004 if (!dollar_present && !quoted && \
2005 reserved_word_acceptable (last_read_token)) \
2008 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
2009 if (STREQ (tok, word_token_alist[i].word)) \
2011 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
2013 if (word_token_alist[i].token == TIME) \
2015 if (word_token_alist[i].token == ESAC) \
2016 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
2017 else if (word_token_alist[i].token == CASE) \
2018 parser_state |= PST_CASESTMT; \
2019 else if (word_token_alist[i].token == COND_END) \
2020 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
2021 else if (word_token_alist[i].token == COND_START) \
2022 parser_state |= PST_CONDCMD; \
2023 else if (word_token_alist[i].token == '{') \
2024 open_brace_count++; \
2025 else if (word_token_alist[i].token == '}' && open_brace_count) \
2026 open_brace_count--; \
2027 return (word_token_alist[i].token); \
2034 /* OK, we have a token. Let's try to alias expand it, if (and only if)
2037 It is eligible for expansion if the shell is in interactive mode, and
2038 the token is unquoted and the last token read was a command
2039 separator (or expand_next_token is set), and we are currently
2040 processing an alias (pushed_string_list is non-empty) and this
2041 token is not the same as the current or any previously
2044 Special cases that disqualify:
2045 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
2047 alias_expand_token (tokstr)
2053 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
2054 (parser_state & PST_CASEPAT) == 0)
2056 ap = find_alias (tokstr);
2058 /* Currently expanding this token. */
2059 if (ap && (ap->flags & AL_BEINGEXPANDED))
2060 return (NO_EXPANSION);
2062 expanded = ap ? savestring (ap->value) : (char *)NULL;
2065 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
2066 return (RE_READ_TOKEN);
2069 /* This is an eligible token that does not have an expansion. */
2070 return (NO_EXPANSION);
2072 return (NO_EXPANSION);
2077 time_command_acceptable ()
2079 #if defined (COMMAND_TIMING)
2080 switch (last_read_token)
2099 #endif /* COMMAND_TIMING */
2102 /* Handle special cases of token recognition:
2103 IN is recognized if the last token was WORD and the token
2104 before that was FOR or CASE or SELECT.
2106 DO is recognized if the last token was WORD and the token
2107 before that was FOR or SELECT.
2109 ESAC is recognized if the last token caused `esacs_needed_count'
2112 `{' is recognized if the last token as WORD and the token
2113 before that was FUNCTION, or if we just parsed an arithmetic
2116 `}' is recognized if there is an unclosed `{' present.
2118 `-p' is returned as TIMEOPT if the last read token was TIME.
2120 ']]' is returned as COND_END if the parser is currently parsing
2121 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
2123 `time' is returned as TIME if and only if it is immediately
2124 preceded by one of `;', `\n', `||', `&&', or `&'.
2128 special_case_tokens (tokstr)
2131 if ((last_read_token == WORD) &&
2132 #if defined (SELECT_COMMAND)
2133 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
2135 ((token_before_that == FOR) || (token_before_that == CASE)) &&
2137 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
2139 if (token_before_that == CASE)
2141 parser_state |= PST_CASEPAT;
2142 esacs_needed_count++;
2147 if (last_read_token == WORD &&
2148 #if defined (SELECT_COMMAND)
2149 (token_before_that == FOR || token_before_that == SELECT) &&
2151 (token_before_that == FOR) &&
2153 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
2156 /* Ditto for ESAC in the CASE case.
2157 Specifically, this handles "case word in esac", which is a legal
2158 construct, certainly because someone will pass an empty arg to the
2159 case construct, and we don't want it to barf. Of course, we should
2160 insist that the case construct has at least one pattern in it, but
2161 the designers disagree. */
2162 if (esacs_needed_count)
2164 esacs_needed_count--;
2165 if (STREQ (tokstr, "esac"))
2167 parser_state &= ~PST_CASEPAT;
2172 /* The start of a shell function definition. */
2173 if (parser_state & PST_ALLOWOPNBRC)
2175 parser_state &= ~PST_ALLOWOPNBRC;
2176 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2179 function_bstart = line_number;
2180 return ('{'); /* } */
2184 /* We allow a `do' after a for ((...)) without an intervening
2186 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
2188 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2191 return ('{'); /* } */
2194 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
2196 open_brace_count--; /* { */
2200 #if defined (COMMAND_TIMING)
2201 /* Handle -p after `time'. */
2202 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
2206 #if defined (COMMAND_TIMING)
2207 if (STREQ (token, "time") && ((parser_state & PST_CASEPAT) == 0) && time_command_acceptable ())
2209 #endif /* COMMAND_TIMING */
2211 #if defined (COND_COMMAND) /* [[ */
2212 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
2219 /* Called from shell.c when Control-C is typed at top level. Or
2220 by the error rule at top level. */
2224 dstack.delimiter_depth = 0; /* No delimiters found so far. */
2225 open_brace_count = 0;
2229 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2230 if (pushed_string_list)
2231 free_string_list ();
2232 #endif /* ALIAS || DPAREN_ARITHMETIC */
2234 if (shell_input_line)
2236 free (shell_input_line);
2237 shell_input_line = (char *)NULL;
2238 shell_input_line_size = shell_input_line_index = 0;
2241 FREE (word_desc_to_read);
2242 word_desc_to_read = (WORD_DESC *)NULL;
2244 last_read_token = '\n';
2245 token_to_read = '\n';
2248 /* Read the next token. Command can be READ (normal operation) or
2249 RESET (to normalize state). */
2251 read_token (command)
2254 int character; /* Current character. */
2255 int peek_char; /* Temporary look-ahead character. */
2256 int result; /* The thing to return. */
2258 if (command == RESET)
2266 result = token_to_read;
2267 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
2269 yylval.word = word_desc_to_read;
2270 word_desc_to_read = (WORD_DESC *)NULL;
2276 #if defined (COND_COMMAND)
2277 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
2279 cond_lineno = line_number;
2280 parser_state |= PST_CONDEXPR;
2281 yylval.command = parse_cond_command ();
2282 if (cond_token != COND_END)
2284 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
2285 parser_error (cond_lineno, "unexpected EOF while looking for `]]'");
2286 else if (cond_token != COND_ERROR)
2287 parser_error (cond_lineno, "syntax error in conditional expression");
2290 token_to_read = COND_END;
2291 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
2297 /* This is a place to jump back to once we have successfully expanded a
2298 token with an alias and pushed the string with push_string () */
2302 /* Read a single word from input. Start by skipping blanks. */
2303 while ((character = shell_getc (1)) != EOF && whitespace (character))
2306 if (character == EOF)
2312 if (character == '#' && (!interactive || interactive_comments))
2314 /* A comment. Discard until EOL or EOF, and then return a newline. */
2315 discard_until ('\n');
2317 character = '\n'; /* this will take the next if statement and return. */
2320 if (character == '\n')
2322 /* If we're about to return an unquoted newline, we can go and collect
2323 the text of any pending here document. */
2325 gather_here_documents ();
2328 parser_state &= ~PST_ALEXPNEXT;
2334 /* Shell meta-characters. */
2335 if (shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
2338 /* Turn off alias tokenization iff this character sequence would
2339 not leave us ready to read a command. */
2340 if (character == '<' || character == '>')
2341 parser_state &= ~PST_ALEXPNEXT;
2344 peek_char = shell_getc (1);
2345 if (character == peek_char)
2350 /* If '<' then we could be at "<<" or at "<<-". We have to
2351 look ahead one more character. */
2352 peek_char = shell_getc (1);
2353 if (peek_char == '-')
2354 return (LESS_LESS_MINUS);
2357 shell_ungetc (peek_char);
2362 return (GREATER_GREATER);
2365 parser_state |= PST_CASEPAT;
2367 parser_state &= ~PST_ALEXPNEXT;
2377 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2379 # if defined (ARITH_FOR_COMMAND)
2380 if (last_read_token == FOR)
2386 arith_for_lineno = line_number;
2387 cmdtyp = parse_arith_cmd (&wval);
2390 /* parse_arith_cmd adds quotes at the beginning and end
2391 of the string it returns; we need to take those out. */
2392 len = strlen (wval);
2393 wv2 = (char *)xmalloc (len);
2394 strncpy (wv2, wval + 1, len - 2);
2395 wv2[len - 2] = '\0';
2396 wd = make_word (wv2);
2397 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
2400 return (ARITH_FOR_EXPRS);
2403 return -1; /* ERROR */
2406 # if defined (DPAREN_ARITHMETIC)
2407 if (reserved_word_acceptable (last_read_token))
2413 sline = line_number;
2414 cmdtyp = parse_arith_cmd (&wval);
2415 if (cmdtyp == 1) /* arithmetic command */
2417 wd = make_word (wval);
2418 wd->flags = W_QUOTED;
2419 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
2420 free (wval); /* make_word copies it */
2423 else if (cmdtyp == 0) /* nested subshell */
2425 push_string (wval, 0, (alias_t *)NULL);
2426 if ((parser_state & PST_CASEPAT) == 0)
2427 parser_state |= PST_SUBSHELL;
2438 else if (character == '<' && peek_char == '&')
2440 else if (character == '>' && peek_char == '&')
2441 return (GREATER_AND);
2442 else if (character == '<' && peek_char == '>')
2443 return (LESS_GREATER);
2444 else if (character == '>' && peek_char == '|')
2445 return (GREATER_BAR);
2446 else if (peek_char == '>' && character == '&')
2447 return (AND_GREATER);
2449 shell_ungetc (peek_char);
2451 /* If we look like we are reading the start of a function
2452 definition, then let the reader know about it so that
2453 we will do the right thing with `{'. */
2454 if (character == ')' && last_read_token == '(' && token_before_that == WORD)
2456 parser_state |= PST_ALLOWOPNBRC;
2458 parser_state &= ~PST_ALEXPNEXT;
2460 function_dstart = line_number;
2463 /* case pattern lists may be preceded by an optional left paren. If
2464 we're not trying to parse a case pattern list, the left paren
2465 indicates a subshell. */
2466 if (character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
2467 parser_state |= PST_SUBSHELL;
2469 else if ((parser_state & PST_CASEPAT) && character == ')')
2470 parser_state &= ~PST_CASEPAT;
2472 else if ((parser_state & PST_SUBSHELL) && character == ')')
2473 parser_state &= ~PST_SUBSHELL;
2475 #if defined (PROCESS_SUBSTITUTION)
2476 /* Check for the constructs which introduce process substitution.
2477 Shells running in `posix mode' don't do process substitution. */
2478 if (posixly_correct ||
2479 ((character != '>' && character != '<') || peek_char != '('))
2480 #endif /* PROCESS_SUBSTITUTION */
2484 /* Hack <&- (close stdin) case. */
2485 if (character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
2488 /* Okay, if we got this far, we have to read a word. Read one,
2489 and then check it against the known ones. */
2490 result = read_token_word (character);
2492 if (result == RE_READ_TOKEN)
2498 /* Match a $(...) or other grouping construct. This has to handle embedded
2499 quoted strings ('', ``, "") and nested constructs. It also must handle
2500 reprompting the user, if necessary, after reading a newline, and returning
2501 correct error values if it reads EOF. */
2503 #define P_FIRSTCLOSE 0x01
2504 #define P_ALLOWESC 0x02
2506 static char matched_pair_error;
2508 parse_matched_pair (qc, open, close, lenp, flags)
2509 int qc; /* `"' if this construct is within double quotes */
2513 int count, ch, was_dollar;
2514 int pass_next_character, nestlen, ttranslen, start_lineno;
2515 char *ret, *nestret, *ttrans;
2516 int retind, retsize;
2519 pass_next_character = was_dollar = 0;
2521 ret = (char *)xmalloc (retsize = 64);
2524 start_lineno = line_number;
2527 ch = shell_getc ((qc != '\'' || (flags & P_ALLOWESC)) && pass_next_character == 0);
2531 parser_error (start_lineno, "unexpected EOF while looking for matching `%c'", close);
2532 EOF_Reached = 1; /* XXX */
2533 return (&matched_pair_error);
2536 /* Possible reprompting. */
2537 if (ch == '\n' && interactive &&
2538 (bash_input.type == st_stdin || bash_input.type == st_stream))
2541 if (pass_next_character) /* last char was backslash */
2543 pass_next_character = 0;
2544 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
2546 if (retind > 0) retind--; /* swallow previously-added backslash */
2550 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2551 if (ch == CTLESC || ch == CTLNUL)
2552 ret[retind++] = CTLESC;
2556 else if (ch == CTLESC || ch == CTLNUL) /* special shell escapes */
2558 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2559 ret[retind++] = CTLESC;
2563 else if (ch == close) /* ending delimiter */
2566 /* handle nested ${...} specially. */
2567 else if (open != close && was_dollar && open == '{' && ch == open) /* } */
2570 else if (((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
2573 /* Add this character. */
2574 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
2577 if (open == '\'') /* '' inside grouping construct */
2579 if ((flags & P_ALLOWESC) && ch == '\\')
2580 pass_next_character++;
2584 if (ch == '\\') /* backslashes */
2585 pass_next_character++;
2587 if (open != close) /* a grouping construct */
2589 if (shellquote (ch))
2591 /* '', ``, or "" inside $(...) or other grouping construct. */
2592 push_delimiter (dstack, ch);
2593 if (was_dollar && ch == '\'') /* $'...' inside group */
2594 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC);
2596 nestret = parse_matched_pair (ch, ch, ch, &nestlen, 0);
2597 pop_delimiter (dstack);
2598 if (nestret == &matched_pair_error)
2601 return &matched_pair_error;
2603 if (was_dollar && ch == '\'')
2605 /* Translate $'...' here. */
2606 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
2608 nestret = sh_single_quote (ttrans);
2610 nestlen = strlen (nestret);
2611 retind -= 2; /* back up before the $' */
2613 else if (was_dollar && ch == '"')
2615 /* Locale expand $"..." here. */
2616 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
2618 nestret = (char *)xmalloc (ttranslen + 3);
2620 strcpy (nestret + 1, ttrans);
2621 nestret[ttranslen + 1] = '"';
2622 nestret[ttranslen += 2] = '\0';
2624 nestlen = ttranslen;
2625 retind -= 2; /* back up before the $" */
2629 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2630 strcpy (ret + retind, nestret);
2636 /* Parse an old-style command substitution within double quotes as a
2638 /* XXX - sh and ksh93 don't do this - XXX */
2639 else if (open == '"' && ch == '`')
2641 nestret = parse_matched_pair (0, '`', '`', &nestlen, 0);
2642 if (nestret == &matched_pair_error)
2645 return &matched_pair_error;
2649 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2650 strcpy (ret + retind, nestret);
2655 else if (was_dollar && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
2656 /* check for $(), $[], or ${} inside quoted string. */
2658 if (open == ch) /* undo previous increment */
2660 if (ch == '(') /* ) */
2661 nestret = parse_matched_pair (0, '(', ')', &nestlen, 0);
2662 else if (ch == '{') /* } */
2663 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE);
2664 else if (ch == '[') /* ] */
2665 nestret = parse_matched_pair (0, '[', ']', &nestlen, 0);
2666 if (nestret == &matched_pair_error)
2669 return &matched_pair_error;
2673 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2674 strcpy (ret + retind, nestret);
2679 was_dollar = (ch == '$');
2688 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2689 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
2690 If not, assume it's a nested subshell for backwards compatibility and
2691 return 0. In any case, put the characters we've consumed into a locally-
2692 allocated buffer and make *ep point to that buffer. Return -1 on an
2693 error, for example EOF. */
2695 parse_arith_cmd (ep)
2698 int exp_lineno, rval, c;
2699 char *ttok, *tokstr;
2702 exp_lineno = line_number;
2703 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
2705 if (ttok == &matched_pair_error)
2707 /* Check that the next character is the closing right paren. If
2708 not, this is a syntax error. ( */
2709 if ((c = shell_getc (0)) != ')')
2712 tokstr = (char *)xmalloc (ttoklen + 4);
2714 /* (( ... )) -> "..." */
2715 tokstr[0] = (rval == 1) ? '"' : '(';
2716 strncpy (tokstr + 1, ttok, ttoklen - 1); /* don't copy the final `)' */
2719 tokstr[ttoklen] = '"';
2720 tokstr[ttoklen+1] = '\0';
2724 tokstr[ttoklen] = ')';
2725 tokstr[ttoklen+1] = c;
2726 tokstr[ttoklen+2] = '\0';
2732 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
2734 #if defined (COND_COMMAND)
2738 return (cond_or ());
2747 if (cond_token == OR_OR)
2750 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
2761 if (cond_token == AND_AND)
2764 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
2770 cond_skip_newlines ()
2772 while ((cond_token = read_token (READ)) == '\n')
2774 if (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
2777 return (cond_token);
2780 #define COND_RETURN_ERROR() \
2781 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
2787 COND_COM *term, *tleft, *tright;
2790 /* Read a token. It can be a left paren, a `!', a unary operator, or a
2791 word that should be the first argument of a binary operator. Start by
2792 skipping newlines, since this is a compound command. */
2793 tok = cond_skip_newlines ();
2794 lineno = line_number;
2795 if (tok == COND_END)
2797 COND_RETURN_ERROR ();
2799 else if (tok == '(')
2801 term = cond_expr ();
2802 if (cond_token != ')')
2805 dispose_cond_node (term); /* ( */
2806 parser_error (lineno, "expected `)'");
2807 COND_RETURN_ERROR ();
2809 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
2810 (void)cond_skip_newlines ();
2812 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
2815 dispose_word (yylval.word); /* not needed */
2816 term = cond_term ();
2818 term->flags |= CMD_INVERT_RETURN;
2820 else if (tok == WORD && test_unop (yylval.word->word))
2823 tok = read_token (READ);
2826 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
2827 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
2832 parser_error (line_number, "unexpected argument to conditional unary operator");
2833 COND_RETURN_ERROR ();
2836 (void)cond_skip_newlines ();
2838 else if (tok == WORD) /* left argument to binary operator */
2841 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
2844 tok = read_token (READ);
2845 if (tok == WORD && test_binop (yylval.word->word))
2847 else if (tok == '<' || tok == '>')
2848 op = make_word_from_token (tok); /* ( */
2849 /* There should be a check before blindly accepting the `)' that we have
2850 seen the opening `('. */
2851 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
2853 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
2854 the test command. Similarly for [[ x && expr ]] or
2855 [[ x || expr ]] or [[ (x) ]]. */
2856 op = make_word ("-n");
2857 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
2863 parser_error (line_number, "conditional binary operator expected");
2864 dispose_cond_node (tleft);
2865 COND_RETURN_ERROR ();
2869 tok = read_token (READ);
2872 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
2873 term = make_cond_node (COND_BINARY, op, tleft, tright);
2877 parser_error (line_number, "unexpected argument to conditional binary operator");
2878 dispose_cond_node (tleft);
2880 COND_RETURN_ERROR ();
2883 (void)cond_skip_newlines ();
2888 parser_error (line_number, "unexpected token `%c' in conditional command", tok);
2890 parser_error (line_number, "unexpected token %d in conditional command", tok);
2891 COND_RETURN_ERROR ();
2896 /* This is kind of bogus -- we slip a mini recursive-descent parser in
2897 here to handle the conditional statement syntax. */
2899 parse_cond_command ()
2903 cexp = cond_expr ();
2904 return (make_cond_command (cexp));
2909 read_token_word (character)
2912 /* The value for YYLVAL when a WORD is read. */
2913 WORD_DESC *the_word;
2915 /* Index into the token that we are building. */
2918 /* ALL_DIGITS becomes zero when we see a non-digit. */
2919 int all_digit_token;
2921 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
2924 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
2927 /* Non-zero means to ignore the value of the next character, and just
2928 to add it no matter what. */
2929 int pass_next_character;
2931 /* The current delimiting character. */
2933 int result, peek_char;
2934 char *ttok, *ttrans;
2935 int ttoklen, ttranslen;
2938 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
2939 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
2942 all_digit_token = DIGIT (character);
2943 dollar_present = quoted = pass_next_character = 0;
2947 if (character == EOF)
2950 if (pass_next_character)
2952 pass_next_character = 0;
2956 cd = current_delimiter (dstack);
2958 /* Handle backslashes. Quote lots of things when not inside of
2959 double-quotes, quote some things inside of double-quotes. */
2960 if (character == '\\')
2962 peek_char = shell_getc (0);
2964 /* Backslash-newline is ignored in all cases except
2965 when quoted with single quotes. */
2966 if (peek_char == '\n')
2969 goto next_character;
2973 shell_ungetc (peek_char);
2975 /* If the next character is to be quoted, note it now. */
2976 if (cd == 0 || cd == '`' ||
2977 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
2978 pass_next_character++;
2985 /* Parse a matched pair of quote characters. */
2986 if (shellquote (character))
2988 push_delimiter (dstack, character);
2989 ttok = parse_matched_pair (character, character, character, &ttoklen, 0);
2990 pop_delimiter (dstack);
2991 if (ttok == &matched_pair_error)
2992 return -1; /* Bail immediately. */
2993 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2994 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
2995 token[token_index++] = character;
2996 strcpy (token + token_index, ttok);
2997 token_index += ttoklen;
2998 all_digit_token = 0;
3000 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
3002 goto next_character;
3005 #ifdef EXTENDED_GLOB
3006 /* Parse a ksh-style extended pattern matching specification. */
3007 if (extended_glob && PATTERN_CHAR (character))
3009 peek_char = shell_getc (1);
3010 if (peek_char == '(') /* ) */
3012 push_delimiter (dstack, peek_char);
3013 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
3014 pop_delimiter (dstack);
3015 if (ttok == &matched_pair_error)
3016 return -1; /* Bail immediately. */
3017 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3019 TOKEN_DEFAULT_GROW_SIZE);
3020 token[token_index++] = character;
3021 token[token_index++] = peek_char;
3022 strcpy (token + token_index, ttok);
3023 token_index += ttoklen;
3025 dollar_present = all_digit_token = 0;
3026 goto next_character;
3029 shell_ungetc (peek_char);
3031 #endif /* EXTENDED_GLOB */
3033 /* If the delimiter character is not single quote, parse some of
3034 the shell expansions that must be read as a single word. */
3035 if (shellexp (character))
3037 peek_char = shell_getc (1);
3038 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
3039 if (peek_char == '(' ||
3040 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
3042 if (peek_char == '{') /* } */
3043 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE);
3044 else if (peek_char == '(') /* ) */
3046 /* XXX - push and pop the `(' as a delimiter for use by
3047 the command-oriented-history code. This way newlines
3048 appearing in the $(...) string get added to the
3049 history literally rather than causing a possibly-
3050 incorrect `;' to be added. ) */
3051 push_delimiter (dstack, peek_char);
3052 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
3053 pop_delimiter (dstack);
3056 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
3057 if (ttok == &matched_pair_error)
3058 return -1; /* Bail immediately. */
3059 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3061 TOKEN_DEFAULT_GROW_SIZE);
3062 token[token_index++] = character;
3063 token[token_index++] = peek_char;
3064 strcpy (token + token_index, ttok);
3065 token_index += ttoklen;
3068 all_digit_token = 0;
3069 goto next_character;
3071 /* This handles $'...' and $"..." new-style quoted strings. */
3072 else if (character == '$' && (peek_char == '\'' || peek_char == '"'))
3076 first_line = line_number;
3077 push_delimiter (dstack, peek_char);
3078 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
3080 (peek_char == '\'') ? P_ALLOWESC : 0);
3081 pop_delimiter (dstack);
3082 if (ttok == &matched_pair_error)
3084 if (peek_char == '\'')
3086 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
3088 /* Insert the single quotes and correctly quote any
3089 embedded single quotes (allowed because P_ALLOWESC was
3090 passed to parse_matched_pair). */
3091 ttok = sh_single_quote (ttrans);
3094 ttranslen = strlen (ttrans);
3098 /* Try to locale-expand the converted string. */
3099 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
3102 /* Add the double quotes back */
3103 ttok = (char *)xmalloc (ttranslen + 3);
3105 strcpy (ttok + 1, ttrans);
3106 ttok[ttranslen + 1] = '"';
3107 ttok[ttranslen += 2] = '\0';
3112 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2,
3114 TOKEN_DEFAULT_GROW_SIZE);
3115 strcpy (token + token_index, ttrans);
3116 token_index += ttranslen;
3119 all_digit_token = 0;
3120 goto next_character;
3122 /* This could eventually be extended to recognize all of the
3123 shell's single-character parameter expansions, and set flags.*/
3124 else if (character == '$' && peek_char == '$')
3126 ttok = (char *)xmalloc (3);
3127 ttok[0] = ttok[1] = '$';
3129 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
3131 TOKEN_DEFAULT_GROW_SIZE);
3132 strcpy (token + token_index, ttok);
3135 all_digit_token = 0;
3137 goto next_character;
3140 shell_ungetc (peek_char);
3143 #if defined (ARRAY_VARS)
3144 /* Identify possible compound array variable assignment. */
3145 else if (character == '=' && token_index > 0)
3147 peek_char = shell_getc (1);
3148 if (peek_char == '(') /* ) */
3150 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
3151 if (ttok == &matched_pair_error)
3152 return -1; /* Bail immediately. */
3153 if (ttok[0] == '(') /* ) */
3158 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3160 TOKEN_DEFAULT_GROW_SIZE);
3161 token[token_index++] = character;
3162 token[token_index++] = peek_char;
3163 strcpy (token + token_index, ttok);
3164 token_index += ttoklen;
3166 all_digit_token = 0;
3167 goto next_character;
3170 shell_ungetc (peek_char);
3174 /* When not parsing a multi-character word construct, shell meta-
3175 characters break words. */
3176 if (shellbreak (character))
3178 shell_ungetc (character);
3184 all_digit_token &= DIGIT (character);
3185 dollar_present |= character == '$';
3187 if (character == CTLESC || character == CTLNUL)
3188 token[token_index++] = CTLESC;
3190 token[token_index++] = character;
3192 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
3193 TOKEN_DEFAULT_GROW_SIZE);
3196 if (character == '\n' && interactive &&
3197 (bash_input.type == st_stdin || bash_input.type == st_stream))
3200 /* We want to remove quoted newlines (that is, a \<newline> pair)
3201 unless we are within single quotes or pass_next_character is
3202 set (the shell equivalent of literal-next). */
3203 cd = current_delimiter (dstack);
3204 character = shell_getc (cd != '\'' && pass_next_character == 0);
3205 } /* end for (;;) */
3209 token[token_index] = '\0';
3211 /* Check to see what thing we should return. If the last_read_token
3212 is a `<', or a `&', or the character which ended this token is
3213 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
3214 Otherwise, it is just a word, and should be returned as such. */
3215 if (all_digit_token && (character == '<' || character == '>' ||
3216 last_read_token == LESS_AND ||
3217 last_read_token == GREATER_AND))
3219 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
3220 yylval.number = lvalue;
3226 /* Check for special case tokens. */
3227 result = special_case_tokens (token);
3232 /* Posix.2 does not allow reserved words to be aliased, so check for all
3233 of them, including special cases, before expanding the current token
3235 if (posixly_correct)
3236 CHECK_FOR_RESERVED_WORD (token);
3238 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
3239 inhibits alias expansion. */
3240 if (expand_aliases && quoted == 0)
3242 result = alias_expand_token (token);
3243 if (result == RE_READ_TOKEN)
3244 return (RE_READ_TOKEN);
3245 else if (result == NO_EXPANSION)
3246 parser_state &= ~PST_ALEXPNEXT;
3249 /* If not in Posix.2 mode, check for reserved words after alias
3251 if (posixly_correct == 0)
3253 CHECK_FOR_RESERVED_WORD (token);
3255 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
3256 the_word->word = (char *)xmalloc (1 + token_index);
3257 the_word->flags = 0;
3258 strcpy (the_word->word, token);
3260 the_word->flags |= W_HASDOLLAR;
3262 the_word->flags |= W_QUOTED;
3263 /* A word is an assignment if it appears at the beginning of a
3264 simple command, or after another assignment word. This is
3265 context-dependent, so it cannot be handled in the grammar. */
3266 if (assignment (token))
3268 the_word->flags |= W_ASSIGNMENT;
3269 /* Don't perform word splitting on assignment statements. */
3270 if (assignment_acceptable (last_read_token))
3271 the_word->flags |= W_NOSPLIT;
3274 yylval.word = the_word;
3276 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
3277 ? ASSIGNMENT_WORD : WORD;
3279 if (last_read_token == FUNCTION)
3281 parser_state |= PST_ALLOWOPNBRC;
3282 function_dstart = line_number;
3288 /* $'...' ANSI-C expand the portion of STRING between START and END and
3289 return the result. The result cannot be longer than the input string. */
3291 ansiexpand (string, start, end, lenp)
3293 int start, end, *lenp;
3298 temp = (char *)xmalloc (end - start + 1);
3299 for (tlen = 0, len = start; len < end; )
3300 temp[tlen++] = string[len++];
3305 t = ansicstr (temp, tlen, 0, (int *)NULL, lenp);
3317 /* Change a bash string into a string suitable for inclusion in a `po' file.
3318 This backslash-escapes `"' and `\' and changes newlines into \\\n"\n". */
3320 mk_msgstr (string, foundnlp)
3324 register int c, len;
3325 char *result, *r, *s;
3327 for (len = 0, s = string; s && *s; s++)
3330 if (*s == '"' || *s == '\\')
3332 else if (*s == '\n')
3336 r = result = (char *)xmalloc (len + 3);
3339 for (s = string; s && (c = *s); s++)
3341 if (c == '\n') /* <NL> -> \n"<NL>" */
3352 if (c == '"' || c == '\\')
3363 /* $"..." -- Translate the portion of STRING between START and END
3364 according to current locale using gettext (if available) and return
3365 the result. The caller will take care of leaving the quotes intact.
3366 The string will be left without the leading `$' by the caller.
3367 If translation is performed, the translated string will be double-quoted
3368 by the caller. The length of the translated string is returned in LENP,
3371 localeexpand (string, start, end, lineno, lenp)
3373 int start, end, lineno, *lenp;
3375 int len, tlen, foundnl;
3376 char *temp, *t, *t2;
3378 temp = (char *)xmalloc (end - start + 1);
3379 for (tlen = 0, len = start; len < end; )
3380 temp[tlen++] = string[len++];
3383 /* If we're just dumping translatable strings, don't do anything with the
3384 string itself, but if we're dumping in `po' file format, convert it into a form more palatable to gettext(3)
3385 and friends by quoting `"' and `\' with backslashes and converting <NL>
3386 into `\n"<NL>"'. If we find a newline in TEMP, we first output a
3387 `msgid ""' line and then the translated string; otherwise we output the
3388 `msgid' and translated string all on one line. */
3389 if (dump_translatable_strings)
3391 if (dump_po_strings)
3394 t = mk_msgstr (temp, &foundnl);
3395 t2 = foundnl ? "\"\"\n" : "";
3397 printf ("#: %s:%d\nmsgid %s%s\nmsgstr \"\"\n",
3398 (bash_input.name ? bash_input.name : "stdin"), lineno, t2, t);
3402 printf ("\"%s\"\n", temp);
3410 t = localetrans (temp, tlen, &len);
3424 /* Return 1 if TOKSYM is a token that after being read would allow
3425 a reserved word to be seen, else 0. */
3427 reserved_word_acceptable (toksym)
3430 if (toksym == '\n' || toksym == ';' || toksym == '(' || toksym == ')' ||
3431 toksym == '|' || toksym == '&' || toksym == '{' ||
3432 toksym == '}' || /* XXX */
3433 toksym == AND_AND ||
3435 toksym == TIME || toksym == TIMEOPT ||
3442 toksym == SEMI_SEMI ||
3446 toksym == DONE || /* XXX these two are experimental */
3454 /* Return the index of TOKEN in the alist of reserved words, or -1 if
3455 TOKEN is not a shell reserved word. */
3457 find_reserved_word (tokstr)
3461 for (i = 0; word_token_alist[i].word; i++)
3462 if (STREQ (tokstr, word_token_alist[i].word))
3468 #if defined (READLINE)
3469 /* Called after each time readline is called. This insures that whatever
3470 the new prompt string is gets propagated to readline's local prompt
3473 reset_readline_prompt ()
3477 if (prompt_string_pointer)
3479 temp_prompt = (*prompt_string_pointer)
3480 ? decode_prompt_string (*prompt_string_pointer)
3483 if (temp_prompt == 0)
3485 temp_prompt = (char *)xmalloc (1);
3486 temp_prompt[0] = '\0';
3489 FREE (current_readline_prompt);
3490 current_readline_prompt = temp_prompt;
3493 #endif /* READLINE */
3496 #if defined (HISTORY)
3497 /* A list of tokens which can be followed by newlines, but not by
3498 semi-colons. When concatenating multiple lines of history, the
3499 newline separator for such tokens is replaced with a space. */
3500 static int no_semi_successors[] = {
3501 '\n', '{', '(', ')', ';', '&', '|',
3502 CASE, DO, ELSE, IF, SEMI_SEMI, THEN, UNTIL, WHILE, AND_AND, OR_OR, IN,
3506 /* If we are not within a delimited expression, try to be smart
3507 about which separators can be semi-colons and which must be
3508 newlines. Returns the string that should be added into the
3511 history_delimiting_chars ()
3515 if (dstack.delimiter_depth != 0)
3518 /* First, handle some special cases. */
3520 /* If we just read `()', assume it's a function definition, and don't
3521 add a semicolon. If the token before the `)' was not `(', and we're
3522 not in the midst of parsing a case statement, assume it's a
3523 parenthesized command and add the semicolon. */
3525 if (token_before_that == ')')
3527 if (two_tokens_ago == '(') /*)*/ /* function def */
3529 /* This does not work for subshells inside case statement
3530 command lists. It's a suboptimal solution. */
3531 else if (parser_state & PST_CASESTMT) /* case statement pattern */
3534 return "; "; /* (...) subshell */
3536 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
3537 return " "; /* function def using `function name' without `()' */
3539 else if (token_before_that == WORD && two_tokens_ago == FOR)
3541 /* Tricky. `for i\nin ...' should not have a semicolon, but
3542 `for i\ndo ...' should. We do what we can. */
3543 for (i = shell_input_line_index; whitespace(shell_input_line[i]); i++)
3545 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
3550 for (i = 0; no_semi_successors[i]; i++)
3552 if (token_before_that == no_semi_successors[i])
3558 #endif /* HISTORY */
3560 /* Issue a prompt, or prepare to issue a prompt when the next character
3567 if (!interactive) /* XXX */
3570 ps1_prompt = get_string_value ("PS1");
3571 ps2_prompt = get_string_value ("PS2");
3573 if (!prompt_string_pointer)
3574 prompt_string_pointer = &ps1_prompt;
3576 temp_prompt = *prompt_string_pointer
3577 ? decode_prompt_string (*prompt_string_pointer)
3580 if (temp_prompt == 0)
3582 temp_prompt = (char *)xmalloc (1);
3583 temp_prompt[0] = '\0';
3586 current_prompt_string = *prompt_string_pointer;
3587 prompt_string_pointer = &ps2_prompt;
3589 #if defined (READLINE)
3590 if (!no_line_editing)
3592 FREE (current_readline_prompt);
3593 current_readline_prompt = temp_prompt;
3596 #endif /* READLINE */
3598 FREE (current_decoded_prompt);
3599 current_decoded_prompt = temp_prompt;
3604 get_current_prompt_level ()
3606 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
3610 set_current_prompt_level (x)
3613 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
3614 current_prompt_string = *prompt_string_pointer;
3620 fprintf (stderr, "%s", current_decoded_prompt);
3624 /* Return a string which will be printed as a prompt. The string
3625 may contain special characters which are decoded as follows:
3628 \e escape (ascii 033)
3629 \d the date in Day Mon Date format
3630 \h the hostname up to the first `.'
3632 \j the number of active jobs
3633 \l the basename of the shell's tty device name
3635 \s the name of the shell
3636 \t the time in 24-hour hh:mm:ss format
3637 \T the time in 12-hour hh:mm:ss format
3638 \@ the time in 12-hour am/pm format
3639 \v the version of bash (e.g., 2.00)
3640 \V the release of bash, version + patchlevel (e.g., 2.00.0)
3641 \w the current working directory
3642 \W the last element of $PWD
3644 \# the command number of this command
3645 \! the history number of this command
3646 \$ a $ or a # if you are root
3647 \nnn character code nnn in octal
3649 \[ begin a sequence of non-printing chars
3650 \] end a sequence of non-printing chars
3652 #define PROMPT_GROWTH 48
3654 decode_prompt_string (string)
3659 struct dstack save_dstack;
3660 int last_exit_value;
3661 #if defined (PROMPT_STRING_DECODE)
3662 int result_size, result_index;
3664 char *temp, octal_string[4];
3667 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
3668 result[result_index = 0] = 0;
3669 temp = (char *)NULL;
3671 while (c = *string++)
3673 if (posixly_correct && c == '!')
3677 temp = savestring ("!");
3682 #if !defined (HISTORY)
3683 temp = savestring ("1");
3685 temp = itos (history_number ());
3686 #endif /* HISTORY */
3687 string--; /* add_string increments string again. */
3705 strncpy (octal_string, string, 3);
3706 octal_string[3] = '\0';
3708 n = read_octal (octal_string);
3709 temp = (char *)xmalloc (3);
3711 if (n == CTLESC || n == CTLNUL)
3728 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
3739 /* Make the current time/date into a string. */
3740 the_time = time (0);
3741 temp = ctime (&the_time);
3743 temp = (c != 'd') ? savestring (temp + 11) : savestring (temp);
3744 temp[(c != 'd') ? 8 : 10] = '\0';
3745 temp[(c != 'A') ? 10 : 5] = '\0';
3747 /* quick and dirty conversion to 12-hour time */
3748 if (c == 'T' || c == '@')
3752 temp[5] = 'a'; /* am/pm format */
3763 temp[0] = (n / 10) + '0';
3764 temp[1] = (n % 10) + '0';
3766 if (n >= 0 && temp[5] == 'a')
3772 temp = (char *)xmalloc (2);
3778 temp = (char *)xmalloc (3);
3779 temp[0] = no_line_editing ? '\n' : '\r';
3780 temp[1] = no_line_editing ? '\0' : '\n';
3785 temp = base_pathname (shell_name);
3786 temp = savestring (temp);
3791 temp = (char *)xmalloc (8);
3793 strcpy (temp, dist_version);
3795 sprintf (temp, "%s.%d", dist_version, patch_level);
3801 /* Use the value of PWD because it is much more efficient. */
3802 char t_string[PATH_MAX];
3805 temp = get_string_value ("PWD");
3809 if (getcwd (t_string, sizeof(t_string)) == 0)
3815 tlen = strlen (t_string);
3819 tlen = sizeof (t_string) - 1;
3820 strncpy (t_string, temp, tlen);
3822 t_string[tlen] = '\0';
3824 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
3825 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
3828 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
3830 t = strrchr (t_string, '/');
3832 strcpy (t_string, t + 1);
3836 #undef DOUBLE_SLASH_ROOT
3838 /* polite_directory_format is guaranteed to return a string
3839 no longer than PATH_MAX - 1 characters. */
3840 strcpy (t_string, polite_directory_format (t_string));
3842 /* If we're going to be expanding the prompt string later,
3843 quote the directory name. */
3844 if (promptvars || posixly_correct)
3845 /* Make sure that expand_prompt_string is called with a
3846 second argument of Q_DOUBLE_QUOTE if we use this
3848 temp = sh_backslash_quote_for_double_quotes (t_string);
3850 temp = savestring (t_string);
3856 if (current_user.user_name == 0)
3857 get_current_user_info ();
3858 temp = savestring (current_user.user_name);
3863 temp = savestring (current_host_name);
3864 if (c == 'h' && (t = (char *)strchr (temp, '.')))
3869 temp = itos (current_command_number);
3873 #if !defined (HISTORY)
3874 temp = savestring ("1");
3876 temp = itos (history_number ());
3877 #endif /* HISTORY */
3881 t = temp = (char *)xmalloc (3);
3882 if ((promptvars || posixly_correct) && (current_user.euid != 0))
3884 *t++ = current_user.euid == 0 ? '#' : '$';
3889 temp = itos (count_all_jobs ());
3893 #if defined (HAVE_TTYNAME)
3894 temp = (char *)ttyname (fileno (stdin));
3895 t = temp ? base_pathname (temp) : "tty";
3896 temp = savestring (t);
3898 temp = savestring ("tty");
3899 #endif /* !HAVE_TTYNAME */
3902 #if defined (READLINE)
3905 temp = (char *)xmalloc (3);
3907 temp[1] = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
3910 #endif /* READLINE */
3913 temp = (char *)xmalloc (2);
3920 temp = (char *)xmalloc (2);
3921 temp[0] = (c == 'a') ? '\07' : '\033';
3926 temp = (char *)xmalloc (3);
3935 sub_append_string (temp, result, &result_index, &result_size);
3936 temp = (char *)NULL; /* Freed in sub_append_string (). */
3937 result[result_index] = '\0';
3943 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
3944 result[result_index++] = c;
3945 result[result_index] = '\0';
3948 #else /* !PROMPT_STRING_DECODE */
3949 result = savestring (string);
3950 #endif /* !PROMPT_STRING_DECODE */
3952 /* Save the delimiter stack and point `dstack' to temp space so any
3953 command substitutions in the prompt string won't result in screwing
3954 up the parser's quoting state. */
3955 save_dstack = dstack;
3956 dstack = temp_dstack;
3957 dstack.delimiter_depth = 0;
3959 /* Perform variable and parameter expansion and command substitution on
3960 the prompt string. */
3961 if (promptvars || posixly_correct)
3963 last_exit_value = last_command_exit_value;
3964 list = expand_prompt_string (result, Q_DOUBLE_QUOTES);
3966 result = string_list (list);
3967 dispose_words (list);
3968 last_command_exit_value = last_exit_value;
3972 t = dequote_string (result);
3977 dstack = save_dstack;
3982 /* Report a syntax error, and restart the parser. Call here for fatal
3988 report_syntax_error ((char *)NULL);
3993 /* Report a syntax error with line numbers, etc.
3994 Call here for recoverable errors. If you have a message to print,
3995 then place it in MESSAGE, otherwise pass NULL and this will figure
3996 out an appropriate message for you. */
3998 report_syntax_error (message)
4007 parser_error (line_number, "%s", message);
4008 if (interactive && EOF_Reached)
4010 last_command_exit_value = EX_USAGE;
4014 /* If the line of input we're reading is not null, try to find the
4015 objectionable token. */
4016 if (shell_input_line && *shell_input_line)
4018 t = shell_input_line;
4019 i = shell_input_line_index;
4022 if (i && t[i] == '\0')
4025 while (i && (whitespace (t[i]) || t[i] == '\n'))
4031 while (i && (member (t[i], " \n\t;|&") == 0))
4034 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
4037 /* Print the offending token. */
4038 if (token_end || (i == 0 && token_end == 0))
4041 msg = substring (t, i, token_end);
4042 else /* one-character token */
4049 parser_error (line_number, "syntax error near unexpected token `%s'", msg);
4055 /* If not interactive, print the line containing the error. */
4056 if (interactive == 0)
4058 msg = savestring (shell_input_line);
4059 token_end = strlen (msg);
4060 while (token_end && msg[token_end - 1] == '\n')
4061 msg[--token_end] = '\0';
4063 parser_error (line_number, "`%s'", msg);
4069 msg = EOF_Reached ? "syntax error: unexpected end of file" : "syntax error";
4070 parser_error (line_number, "%s", msg);
4071 /* When the shell is interactive, this file uses EOF_Reached
4072 only for error reporting. Other mechanisms are used to
4073 decide whether or not to exit. */
4074 if (interactive && EOF_Reached)
4077 last_command_exit_value = EX_USAGE;
4080 /* ??? Needed function. ??? We have to be able to discard the constructs
4081 created during parsing. In the case of error, we want to return
4082 allocated objects to the memory pool. In the case of no error, we want
4083 to throw away the information about where the allocated objects live.
4084 (dispose_command () will actually free the command. */
4086 discard_parser_constructs (error_p)
4091 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
4093 /* A flag denoting whether or not ignoreeof is set. */
4096 /* The number of times that we have encountered an EOF character without
4097 another character intervening. When this gets above the limit, the
4098 shell terminates. */
4099 int eof_encountered = 0;
4101 /* The limit for eof_encountered. */
4102 int eof_encountered_limit = 10;
4104 /* If we have EOF as the only input unit, this user wants to leave
4105 the shell. If the shell is not interactive, then just leave.
4106 Otherwise, if ignoreeof is set, and we haven't done this the
4107 required number of times in a row, print a message. */
4109 handle_eof_input_unit ()
4113 /* shell.c may use this to decide whether or not to write out the
4114 history, among other things. We use it only for error reporting
4119 /* If the user wants to "ignore" eof, then let her do so, kind of. */
4122 if (eof_encountered < eof_encountered_limit)
4124 fprintf (stderr, "Use \"%s\" to leave the shell.\n",
4125 login_shell ? "logout" : "exit");
4127 /* Reset the prompt string to be $PS1. */
4128 prompt_string_pointer = (char **)NULL;
4130 last_read_token = current_token = '\n';
4135 /* In this case EOF should exit the shell. Do it now. */
4137 exit_builtin ((WORD_LIST *)NULL);
4141 /* We don't write history files, etc., for non-interactive shells. */
4146 static WORD_LIST parse_string_error;
4148 /* Take a string and run it through the shell parser, returning the
4149 resultant word list. Used by compound array assignment. */
4151 parse_string_to_word_list (s, whom)
4156 int tok, orig_line_number, orig_input_terminator;
4157 int orig_line_count;
4158 #if defined (HISTORY)
4159 int old_remember_on_history, old_history_expansion_inhibited;
4162 #if defined (HISTORY)
4163 old_remember_on_history = remember_on_history;
4164 # if defined (BANG_HISTORY)
4165 old_history_expansion_inhibited = history_expansion_inhibited;
4167 bash_history_disable ();
4170 orig_line_number = line_number;
4171 orig_line_count = current_command_line_count;
4172 orig_input_terminator = shell_input_line_terminator;
4175 last_read_token = '\n';
4176 current_command_line_count = 0;
4178 with_input_from_string (s, whom);
4179 wl = (WORD_LIST *)NULL;
4180 while ((tok = read_token (READ)) != yacc_EOF)
4182 if (tok == '\n' && *bash_input.location.string == '\0')
4184 if (tok == '\n') /* Allow newlines in compound assignments */
4186 if (tok != WORD && tok != ASSIGNMENT_WORD)
4188 line_number = orig_line_number + line_number - 1;
4189 yyerror ((char *)NULL); /* does the right thing */
4192 wl = &parse_string_error;
4195 wl = make_word_list (yylval.word, wl);
4198 last_read_token = '\n';
4201 #if defined (HISTORY)
4202 remember_on_history = old_remember_on_history;
4203 # if defined (BANG_HISTORY)
4204 history_expansion_inhibited = old_history_expansion_inhibited;
4205 # endif /* BANG_HISTORY */
4206 #endif /* HISTORY */
4208 current_command_line_count = orig_line_count;
4209 shell_input_line_terminator = orig_input_terminator;
4211 if (wl == &parse_string_error)
4213 last_command_exit_value = EXECUTION_FAILURE;
4214 if (interactive_shell == 0 && posixly_correct)
4215 jump_to_top_level (FORCE_EOF);
4217 jump_to_top_level (DISCARD);
4220 return (REVERSE_LIST (wl, WORD_LIST *));