1 /* Yacc grammar for bash. */
3 /* Copyright (C) 1989-2008 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file LICENSE. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 #include "bashtypes.h"
29 #if defined (HAVE_UNISTD_H)
33 #if defined (HAVE_LOCALE_H)
38 #include "chartypes.h"
45 #define NEED_STRFTIME_DECL /* used in externs.h */
51 #include "mailcheck.h"
54 #include "builtins/common.h"
55 #include "builtins/builtext.h"
59 #if defined (READLINE)
60 # include "bashline.h"
61 # include <readline/readline.h>
65 # include "bashhist.h"
66 # include <readline/history.h>
69 #if defined (JOB_CONTROL)
71 #endif /* JOB_CONTROL */
76 typedef void *alias_t;
79 #if defined (PROMPT_STRING_DECODE)
81 # include <sys/param.h>
84 # if defined (TM_IN_SYS_TIME)
85 # include <sys/types.h>
86 # include <sys/time.h>
87 # endif /* TM_IN_SYS_TIME */
89 #endif /* PROMPT_STRING_DECODE */
91 #define RE_READ_TOKEN -99
92 #define NO_EXPANSION -100
100 #if defined (HANDLE_MULTIBYTE)
101 # define last_shell_getc_is_singlebyte \
102 ((shell_input_line_index > 1) \
103 ? shell_input_line_property[shell_input_line_index - 1] \
105 # define MBTEST(x) ((x) && last_shell_getc_is_singlebyte)
107 # define last_shell_getc_is_singlebyte 1
108 # define MBTEST(x) ((x))
111 #if defined (EXTENDED_GLOB)
112 extern int extended_glob;
115 extern int eof_encountered;
116 extern int no_line_editing, running_under_emacs;
117 extern int current_command_number;
118 extern int sourcelevel;
119 extern int posixly_correct;
120 extern int last_command_exit_value;
121 extern char *shell_name, *current_host_name;
122 extern char *dist_version;
123 extern int patch_level;
124 extern int dump_translatable_strings, dump_po_strings;
125 extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
126 #if defined (BUFFERED_INPUT)
127 extern int bash_input_fd_changed;
131 /* **************************************************************** */
133 /* "Forward" declarations */
135 /* **************************************************************** */
138 static void debug_parser __P((int));
141 static int yy_getc __P((void));
142 static int yy_ungetc __P((int));
144 #if defined (READLINE)
145 static int yy_readline_get __P((void));
146 static int yy_readline_unget __P((int));
149 static int yy_string_get __P((void));
150 static int yy_string_unget __P((int));
151 static int yy_stream_get __P((void));
152 static int yy_stream_unget __P((int));
154 static int shell_getc __P((int));
155 static void shell_ungetc __P((int));
156 static void discard_until __P((int));
158 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
159 static void push_string __P((char *, int, alias_t *));
160 static void pop_string __P((void));
161 static void free_string_list __P((void));
164 static char *read_a_line __P((int));
166 static int reserved_word_acceptable __P((int));
167 static int yylex __P((void));
168 static int alias_expand_token __P((char *));
169 static int time_command_acceptable __P((void));
170 static int special_case_tokens __P((char *));
171 static int read_token __P((int));
172 static char *parse_matched_pair __P((int, int, int, int *, int));
173 #if defined (ARRAY_VARS)
174 static char *parse_compound_assignment __P((int *));
176 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
177 static int parse_dparen __P((int));
178 static int parse_arith_cmd __P((char **, int));
180 #if defined (COND_COMMAND)
181 static void cond_error __P((void));
182 static COND_COM *cond_expr __P((void));
183 static COND_COM *cond_or __P((void));
184 static COND_COM *cond_and __P((void));
185 static COND_COM *cond_term __P((void));
186 static int cond_skip_newlines __P((void));
187 static COMMAND *parse_cond_command __P((void));
189 #if defined (ARRAY_VARS)
190 static int token_is_assignment __P((char *, int));
191 static int token_is_ident __P((char *, int));
193 static int read_token_word __P((int));
194 static void discard_parser_constructs __P((int));
196 static char *error_token_from_token __P((int));
197 static char *error_token_from_text __P((void));
198 static void print_offending_line __P((void));
199 static void report_syntax_error __P((char *));
201 static void handle_eof_input_unit __P((void));
202 static void prompt_again __P((void));
204 static void reset_readline_prompt __P((void));
206 static void print_prompt __P((void));
208 #if defined (HANDLE_MULTIBYTE)
209 static void set_line_mbstate __P((void));
210 static char *shell_input_line_property = NULL;
212 # define set_line_mbstate()
215 extern int yyerror __P((const char *));
221 /* Default prompt strings */
222 char *primary_prompt = PPROMPT;
223 char *secondary_prompt = SPROMPT;
225 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
226 char *ps1_prompt, *ps2_prompt;
228 /* Handle on the current prompt string. Indirectly points through
229 ps1_ or ps2_prompt. */
230 char **prompt_string_pointer = (char **)NULL;
231 char *current_prompt_string;
233 /* Non-zero means we expand aliases in commands. */
234 int expand_aliases = 0;
236 /* If non-zero, the decoded prompt string undergoes parameter and
237 variable substitution, command substitution, arithmetic substitution,
238 string expansion, process substitution, and quote removal in
239 decode_prompt_string. */
242 /* If non-zero, $'...' and $"..." are expanded when they appear within
243 a ${...} expansion, even when the expansion appears within double
245 int extended_quote = 1;
247 /* The decoded prompt string. Used if READLINE is not defined or if
248 editing is turned off. Analogous to current_readline_prompt. */
249 static char *current_decoded_prompt;
251 /* The number of lines read from input while creating the current command. */
252 int current_command_line_count;
254 /* The token that currently denotes the end of parse. */
257 /* The token currently being read. */
260 /* Variables to manage the task of reading here documents, because we need to
261 defer the reading until after a complete command has been collected. */
262 static REDIRECT *redir_stack[10];
265 /* Where shell input comes from. History expansion is performed on each
266 line when the shell is interactive. */
267 static char *shell_input_line = (char *)NULL;
268 static int shell_input_line_index;
269 static int shell_input_line_size; /* Amount allocated for shell_input_line. */
270 static int shell_input_line_len; /* strlen (shell_input_line) */
272 /* Either zero or EOF. */
273 static int shell_input_line_terminator;
275 /* The line number in a script on which a function definition starts. */
276 static int function_dstart;
278 /* The line number in a script on which a function body starts. */
279 static int function_bstart;
281 /* The line number in a script at which an arithmetic for command starts. */
282 static int arith_for_lineno;
284 /* The current parser state. */
285 static int parser_state;
287 /* The last read token, or NULL. read_token () uses this for context
289 static int last_read_token;
291 /* The token read prior to last_read_token. */
292 static int token_before_that;
294 /* The token read prior to token_before_that. */
295 static int two_tokens_ago;
297 /* The line number in a script where the word in a `case WORD', `select WORD'
298 or `for WORD' begins. This is a nested command maximum, since the array
299 index is decremented after a case, select, or for command is parsed. */
300 #define MAX_CASE_NEST 128
301 static int word_lineno[MAX_CASE_NEST];
302 static int word_top = -1;
304 /* If non-zero, it is the token that we want read_token to return
305 regardless of what text is (or isn't) present to be read. This
306 is reset by read_token. If token_to_read == WORD or
307 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
308 static int token_to_read;
309 static WORD_DESC *word_desc_to_read;
311 static REDIRECTEE redir;
315 WORD_DESC *word; /* the word that we read. */
316 int number; /* the number that we read. */
317 WORD_LIST *word_list;
321 PATTERN_LIST *pattern;
324 /* Reserved words. Members of the first group are only recognized
325 in the case that they are preceded by a list_terminator. Members
326 of the second group are for [[...]] commands. Members of the
327 third group are recognized only under special circumstances. */
328 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION
329 %token COND_START COND_END COND_ERROR
330 %token IN BANG TIME TIMEOPT
332 /* More general tokens. yylex () knows how to make these. */
333 %token <word> WORD ASSIGNMENT_WORD
334 %token <number> NUMBER
335 %token <word_list> ARITH_CMD ARITH_FOR_EXPRS
336 %token <command> COND_CMD
337 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS
338 %token GREATER_AND SEMI_SEMI SEMI_AND SEMI_SEMI_AND
339 %token LESS_LESS_MINUS AND_GREATER AND_GREATER_GREATER LESS_GREATER
340 %token GREATER_BAR BAR_AND
342 /* The types that the various syntactical units return. */
344 %type <command> inputunit command pipeline pipeline_command
345 %type <command> list list0 list1 compound_list simple_list simple_list1
346 %type <command> simple_command shell_command
347 %type <command> for_command select_command case_command group_command
348 %type <command> arith_command
349 %type <command> cond_command
350 %type <command> arith_for_command
351 %type <command> function_def function_body if_command elif_clause subshell
352 %type <redirect> redirection redirection_list
353 %type <element> simple_command_element
354 %type <word_list> word_list pattern
355 %type <pattern> pattern_list case_clause_sequence case_clause
356 %type <number> timespec
357 %type <number> list_terminator
361 %left '&' ';' '\n' yacc_EOF
366 inputunit: simple_list simple_list_terminator
368 /* Case of regular command. Discard the error
369 safety net,and return the command just parsed. */
372 /* discard_parser_constructs (0); */
377 /* Case of regular command, but not a very
378 interesting one. Return a NULL command. */
379 global_command = (COMMAND *)NULL;
384 /* Error during parsing. Return NULL command. */
385 global_command = (COMMAND *)NULL;
387 /* discard_parser_constructs (1); */
399 /* Case of EOF seen by itself. Do ignoreeof or
401 global_command = (COMMAND *)NULL;
402 handle_eof_input_unit ();
408 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
410 { $$ = make_word_list ($2, $1); }
413 redirection: '>' WORD
416 $$ = make_redirection (1, r_output_direction, redir);
421 $$ = make_redirection (0, r_input_direction, redir);
426 $$ = make_redirection ($1, r_output_direction, redir);
431 $$ = make_redirection ($1, r_input_direction, redir);
433 | GREATER_GREATER WORD
436 $$ = make_redirection (1, r_appending_to, redir);
438 | NUMBER GREATER_GREATER WORD
441 $$ = make_redirection ($1, r_appending_to, redir);
446 $$ = make_redirection (0, r_reading_until, redir);
447 redir_stack[need_here_doc++] = $$;
449 | NUMBER LESS_LESS WORD
452 $$ = make_redirection ($1, r_reading_until, redir);
453 redir_stack[need_here_doc++] = $$;
455 | LESS_LESS_LESS WORD
458 $$ = make_redirection (0, r_reading_string, redir);
460 | NUMBER LESS_LESS_LESS WORD
463 $$ = make_redirection ($1, r_reading_string, redir);
468 $$ = make_redirection (0, r_duplicating_input, redir);
470 | NUMBER LESS_AND NUMBER
473 $$ = make_redirection ($1, r_duplicating_input, redir);
478 $$ = make_redirection (1, r_duplicating_output, redir);
480 | NUMBER GREATER_AND NUMBER
483 $$ = make_redirection ($1, r_duplicating_output, redir);
488 $$ = make_redirection (0, r_duplicating_input_word, redir);
490 | NUMBER LESS_AND WORD
493 $$ = make_redirection ($1, r_duplicating_input_word, redir);
498 $$ = make_redirection (1, r_duplicating_output_word, redir);
500 | NUMBER GREATER_AND WORD
503 $$ = make_redirection ($1, r_duplicating_output_word, redir);
505 | LESS_LESS_MINUS WORD
508 $$ = make_redirection
509 (0, r_deblank_reading_until, redir);
510 redir_stack[need_here_doc++] = $$;
512 | NUMBER LESS_LESS_MINUS WORD
515 $$ = make_redirection
516 ($1, r_deblank_reading_until, redir);
517 redir_stack[need_here_doc++] = $$;
522 $$ = make_redirection (1, r_close_this, redir);
524 | NUMBER GREATER_AND '-'
527 $$ = make_redirection ($1, r_close_this, redir);
532 $$ = make_redirection (0, r_close_this, redir);
534 | NUMBER LESS_AND '-'
537 $$ = make_redirection ($1, r_close_this, redir);
542 $$ = make_redirection (1, r_err_and_out, redir);
544 | AND_GREATER_GREATER WORD
547 $$ = make_redirection (1, r_append_err_and_out, redir);
549 | NUMBER LESS_GREATER WORD
552 $$ = make_redirection ($1, r_input_output, redir);
557 $$ = make_redirection (0, r_input_output, redir);
562 $$ = make_redirection (1, r_output_force, redir);
564 | NUMBER GREATER_BAR WORD
567 $$ = make_redirection ($1, r_output_force, redir);
571 simple_command_element: WORD
572 { $$.word = $1; $$.redirect = 0; }
574 { $$.word = $1; $$.redirect = 0; }
576 { $$.redirect = $1; $$.word = 0; }
579 redirection_list: redirection
583 | redirection_list redirection
585 register REDIRECT *t;
587 for (t = $1; t->next; t = t->next)
594 simple_command: simple_command_element
595 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
596 | simple_command simple_command_element
597 { $$ = make_simple_command ($2, $1); }
600 command: simple_command
601 { $$ = clean_simple_command ($1); }
604 | shell_command redirection_list
611 register REDIRECT *t;
612 for (t = tc->redirects; t->next; t = t->next)
624 shell_command: for_command
628 | WHILE compound_list DO compound_list DONE
629 { $$ = make_while_command ($2, $4); }
630 | UNTIL compound_list DO compound_list DONE
631 { $$ = make_until_command ($2, $4); }
648 for_command: FOR WORD newline_list DO compound_list DONE
650 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
651 if (word_top > 0) word_top--;
653 | FOR WORD newline_list '{' compound_list '}'
655 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
656 if (word_top > 0) word_top--;
658 | FOR WORD ';' newline_list DO compound_list DONE
660 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
661 if (word_top > 0) word_top--;
663 | FOR WORD ';' newline_list '{' compound_list '}'
665 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
666 if (word_top > 0) word_top--;
668 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
670 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
671 if (word_top > 0) word_top--;
673 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
675 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
676 if (word_top > 0) word_top--;
678 | FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
680 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
681 if (word_top > 0) word_top--;
683 | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
685 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
686 if (word_top > 0) word_top--;
690 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
692 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
693 if (word_top > 0) word_top--;
695 | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
697 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
698 if (word_top > 0) word_top--;
700 | FOR ARITH_FOR_EXPRS DO compound_list DONE
702 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
703 if (word_top > 0) word_top--;
705 | FOR ARITH_FOR_EXPRS '{' compound_list '}'
707 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
708 if (word_top > 0) word_top--;
712 select_command: SELECT WORD newline_list DO list DONE
714 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
715 if (word_top > 0) word_top--;
717 | SELECT WORD newline_list '{' list '}'
719 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
720 if (word_top > 0) word_top--;
722 | SELECT WORD ';' newline_list DO list DONE
724 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
725 if (word_top > 0) word_top--;
727 | SELECT WORD ';' newline_list '{' list '}'
729 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
730 if (word_top > 0) word_top--;
732 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
734 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
735 if (word_top > 0) word_top--;
737 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
739 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
740 if (word_top > 0) word_top--;
744 case_command: CASE WORD newline_list IN newline_list ESAC
746 $$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]);
747 if (word_top > 0) word_top--;
749 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
751 $$ = make_case_command ($2, $5, word_lineno[word_top]);
752 if (word_top > 0) word_top--;
754 | CASE WORD newline_list IN case_clause ESAC
756 $$ = make_case_command ($2, $5, word_lineno[word_top]);
757 if (word_top > 0) word_top--;
761 function_def: WORD '(' ')' newline_list function_body
762 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
764 | FUNCTION WORD '(' ')' newline_list function_body
765 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
767 | FUNCTION WORD newline_list function_body
768 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
772 function_body: shell_command
774 | shell_command redirection_list
779 /* According to Posix.2 3.9.5, redirections
780 specified after the body of a function should
781 be attached to the function and performed when
782 the function is executed, not as part of the
783 function definition command. */
784 /* XXX - I don't think it matters, but we might
785 want to change this in the future to avoid
786 problems differentiating between a function
787 definition with a redirection and a function
788 definition containing a single command with a
789 redirection. The two are semantically equivalent,
790 though -- the only difference is in how the
791 command printing code displays the redirections. */
794 register REDIRECT *t;
795 for (t = tc->redirects; t->next; t = t->next)
805 subshell: '(' compound_list ')'
807 $$ = make_subshell_command ($2);
808 $$->flags |= CMD_WANT_SUBSHELL;
812 if_command: IF compound_list THEN compound_list FI
813 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
814 | IF compound_list THEN compound_list ELSE compound_list FI
815 { $$ = make_if_command ($2, $4, $6); }
816 | IF compound_list THEN compound_list elif_clause FI
817 { $$ = make_if_command ($2, $4, $5); }
821 group_command: '{' compound_list '}'
822 { $$ = make_group_command ($2); }
825 arith_command: ARITH_CMD
826 { $$ = make_arith_command ($1); }
829 cond_command: COND_START COND_CMD COND_END
833 elif_clause: ELIF compound_list THEN compound_list
834 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
835 | ELIF compound_list THEN compound_list ELSE compound_list
836 { $$ = make_if_command ($2, $4, $6); }
837 | ELIF compound_list THEN compound_list elif_clause
838 { $$ = make_if_command ($2, $4, $5); }
841 case_clause: pattern_list
842 | case_clause_sequence pattern_list
843 { $2->next = $1; $$ = $2; }
846 pattern_list: newline_list pattern ')' compound_list
847 { $$ = make_pattern_list ($2, $4); }
848 | newline_list pattern ')' newline_list
849 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
850 | newline_list '(' pattern ')' compound_list
851 { $$ = make_pattern_list ($3, $5); }
852 | newline_list '(' pattern ')' newline_list
853 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
856 case_clause_sequence: pattern_list SEMI_SEMI
858 | case_clause_sequence pattern_list SEMI_SEMI
859 { $2->next = $1; $$ = $2; }
860 | pattern_list SEMI_AND
861 { $1->flags |= CASEPAT_FALLTHROUGH; $$ = $1; }
862 | case_clause_sequence pattern_list SEMI_AND
863 { $2->flags |= CASEPAT_FALLTHROUGH; $2->next = $1; $$ = $2; }
864 | pattern_list SEMI_SEMI_AND
865 { $1->flags |= CASEPAT_TESTNEXT; $$ = $1; }
866 | case_clause_sequence pattern_list SEMI_SEMI_AND
867 { $2->flags |= CASEPAT_TESTNEXT; $2->next = $1; $$ = $2; }
871 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
873 { $$ = make_word_list ($3, $1); }
876 /* A list allows leading or trailing newlines and
877 newlines as operators (equivalent to semicolons).
878 It must end with a newline or semicolon.
879 Lists are used within commands such as if, for, while. */
881 list: newline_list list0
885 gather_here_documents ();
896 list0: list1 '\n' newline_list
897 | list1 '&' newline_list
899 if ($1->type == cm_connection)
900 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
902 $$ = command_connect ($1, (COMMAND *)NULL, '&');
904 | list1 ';' newline_list
908 list1: list1 AND_AND newline_list list1
909 { $$ = command_connect ($1, $4, AND_AND); }
910 | list1 OR_OR newline_list list1
911 { $$ = command_connect ($1, $4, OR_OR); }
912 | list1 '&' newline_list list1
914 if ($1->type == cm_connection)
915 $$ = connect_async_list ($1, $4, '&');
917 $$ = command_connect ($1, $4, '&');
919 | list1 ';' newline_list list1
920 { $$ = command_connect ($1, $4, ';'); }
921 | list1 '\n' newline_list list1
922 { $$ = command_connect ($1, $4, ';'); }
927 simple_list_terminator: '\n'
943 /* A simple_list is a list that contains no significant newlines
944 and no leading or trailing newlines. Newlines are allowed
945 only following operators, where they are not significant.
947 This is what an inputunit consists of. */
949 simple_list: simple_list1
953 gather_here_documents ();
957 if ($1->type == cm_connection)
958 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
960 $$ = command_connect ($1, (COMMAND *)NULL, '&');
962 gather_here_documents ();
968 gather_here_documents ();
972 simple_list1: simple_list1 AND_AND newline_list simple_list1
973 { $$ = command_connect ($1, $4, AND_AND); }
974 | simple_list1 OR_OR newline_list simple_list1
975 { $$ = command_connect ($1, $4, OR_OR); }
976 | simple_list1 '&' simple_list1
978 if ($1->type == cm_connection)
979 $$ = connect_async_list ($1, $3, '&');
981 $$ = command_connect ($1, $3, '&');
983 | simple_list1 ';' simple_list1
984 { $$ = command_connect ($1, $3, ';'); }
990 pipeline_command: pipeline
995 $2->flags |= CMD_INVERT_RETURN;
1004 | timespec BANG pipeline
1007 $3->flags |= $1|CMD_INVERT_RETURN;
1010 | BANG timespec pipeline
1013 $3->flags |= $2|CMD_INVERT_RETURN;
1016 | timespec list_terminator
1020 /* Boy, this is unclean. `time' by itself can
1021 time a null command. We cheat and push a
1022 newline back if the list_terminator was a newline
1023 to avoid the double-newline problem (one to
1024 terminate this, one to terminate the command) */
1027 $$ = make_simple_command (x, (COMMAND *)NULL);
1029 /* XXX - let's cheat and push a newline back */
1031 token_to_read = '\n';
1036 pipeline: pipeline '|' newline_list pipeline
1037 { $$ = command_connect ($1, $4, '|'); }
1038 | pipeline BAR_AND newline_list pipeline
1040 /* Make cmd1 |& cmd2 equivalent to cmd1 2>&1 | cmd2 */
1047 r = make_redirection (2, r_duplicating_output, rd);
1050 register REDIRECT *t;
1051 for (t = tc->redirects; t->next; t = t->next)
1058 $$ = command_connect ($1, $4, '|');
1065 { $$ = CMD_TIME_PIPELINE; }
1067 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1071 /* Initial size to allocate for tokens, and the
1072 amount to grow them by. */
1073 #define TOKEN_DEFAULT_INITIAL_SIZE 496
1074 #define TOKEN_DEFAULT_GROW_SIZE 512
1076 /* Should we call prompt_again? */
1077 #define SHOULD_PROMPT() \
1078 (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
1081 # define expanding_alias() (pushed_string_list && pushed_string_list->expander)
1083 # define expanding_alias() 0
1086 /* Global var is non-zero when end of file has been reached. */
1087 int EOF_Reached = 0;
1100 /* yy_getc () returns the next available character from input or EOF.
1101 yy_ungetc (c) makes `c' the next character to read.
1102 init_yy_io (get, unget, type, location) makes the function GET the
1103 installed function for getting the next character, makes UNGET the
1104 installed function for un-getting a character, sets the type of stream
1105 (either string or file) from TYPE, and makes LOCATION point to where
1106 the input is coming from. */
1108 /* Unconditionally returns end-of-file. */
1115 /* Variable containing the current get and unget functions.
1116 See ./input.h for a clearer description. */
1117 BASH_INPUT bash_input;
1119 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
1120 is non-null, avoiding a memory leak. */
1122 initialize_bash_input ()
1124 bash_input.type = st_none;
1125 FREE (bash_input.name);
1126 bash_input.name = (char *)NULL;
1127 bash_input.location.file = (FILE *)NULL;
1128 bash_input.location.string = (char *)NULL;
1129 bash_input.getter = (sh_cget_func_t *)NULL;
1130 bash_input.ungetter = (sh_cunget_func_t *)NULL;
1133 /* Set the contents of the current bash input stream from
1134 GET, UNGET, TYPE, NAME, and LOCATION. */
1136 init_yy_io (get, unget, type, name, location)
1137 sh_cget_func_t *get;
1138 sh_cunget_func_t *unget;
1139 enum stream_type type;
1141 INPUT_STREAM location;
1143 bash_input.type = type;
1144 FREE (bash_input.name);
1145 bash_input.name = name ? savestring (name) : (char *)NULL;
1149 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
1151 bash_input.location = location;
1153 bash_input.getter = get;
1154 bash_input.ungetter = unget;
1160 return (bash_input.name ? bash_input.name : "stdin");
1163 /* Call this to get the next character of input. */
1167 return (*(bash_input.getter)) ();
1170 /* Call this to unget C. That is, to make C the next character
1176 return (*(bash_input.ungetter)) (c);
1179 #if defined (BUFFERED_INPUT)
1180 #ifdef INCLUDE_UNUSED
1182 input_file_descriptor ()
1184 switch (bash_input.type)
1187 return (fileno (bash_input.location.file));
1189 return (bash_input.location.buffered_fd);
1192 return (fileno (stdin));
1196 #endif /* BUFFERED_INPUT */
1198 /* **************************************************************** */
1200 /* Let input be read from readline (). */
1202 /* **************************************************************** */
1204 #if defined (READLINE)
1205 char *current_readline_prompt = (char *)NULL;
1206 char *current_readline_line = (char *)NULL;
1207 int current_readline_line_index = 0;
1212 SigHandler *old_sigint;
1216 if (!current_readline_line)
1218 if (!bash_readline_initialized)
1219 initialize_readline ();
1221 #if defined (JOB_CONTROL)
1223 give_terminal_to (shell_pgrp, 0);
1224 #endif /* JOB_CONTROL */
1226 old_sigint = (SigHandler *)NULL;
1227 if (signal_is_ignored (SIGINT) == 0)
1229 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
1230 interrupt_immediately++;
1232 terminate_immediately = 1;
1234 current_readline_line = readline (current_readline_prompt ?
1235 current_readline_prompt : "");
1237 terminate_immediately = 0;
1238 if (signal_is_ignored (SIGINT) == 0 && old_sigint)
1240 interrupt_immediately--;
1241 set_signal_handler (SIGINT, old_sigint);
1245 /* Reset the prompt to the decoded value of prompt_string_pointer. */
1246 reset_readline_prompt ();
1249 if (current_readline_line == 0)
1252 current_readline_line_index = 0;
1253 line_len = strlen (current_readline_line);
1255 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
1256 current_readline_line[line_len++] = '\n';
1257 current_readline_line[line_len] = '\0';
1260 if (current_readline_line[current_readline_line_index] == 0)
1262 free (current_readline_line);
1263 current_readline_line = (char *)NULL;
1264 return (yy_readline_get ());
1268 c = current_readline_line[current_readline_line_index++];
1274 yy_readline_unget (c)
1277 if (current_readline_line_index && current_readline_line)
1278 current_readline_line[--current_readline_line_index] = c;
1283 with_input_from_stdin ()
1285 INPUT_STREAM location;
1287 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1289 location.string = current_readline_line;
1290 init_yy_io (yy_readline_get, yy_readline_unget,
1291 st_stdin, "readline stdin", location);
1295 #else /* !READLINE */
1298 with_input_from_stdin ()
1300 with_input_from_stream (stdin, "stdin");
1302 #endif /* !READLINE */
1304 /* **************************************************************** */
1306 /* Let input come from STRING. STRING is zero terminated. */
1308 /* **************************************************************** */
1313 register char *string;
1314 register unsigned char c;
1316 string = bash_input.location.string;
1318 /* If the string doesn't exist, or is empty, EOF found. */
1319 if (string && *string)
1322 bash_input.location.string = string;
1333 *(--bash_input.location.string) = c;
1338 with_input_from_string (string, name)
1342 INPUT_STREAM location;
1344 location.string = string;
1345 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1348 /* **************************************************************** */
1350 /* Let input come from STREAM. */
1352 /* **************************************************************** */
1354 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1355 define, and just use getc/ungetc if it was defined, but since bash
1356 installs its signal handlers without the SA_RESTART flag, some signals
1357 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
1358 the read to be restarted. We need to restart it ourselves. */
1366 if (bash_input.location.file)
1370 interrupt_immediately++;
1371 terminate_immediately++;
1373 result = getc_with_restart (bash_input.location.file);
1376 interrupt_immediately--;
1377 terminate_immediately--;
1387 return (ungetc_with_restart (c, bash_input.location.file));
1391 with_input_from_stream (stream, name)
1395 INPUT_STREAM location;
1397 location.file = stream;
1398 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1401 typedef struct stream_saver {
1402 struct stream_saver *next;
1403 BASH_INPUT bash_input;
1405 #if defined (BUFFERED_INPUT)
1406 BUFFERED_STREAM *bstream;
1407 #endif /* BUFFERED_INPUT */
1410 /* The globally known line number. */
1411 int line_number = 0;
1413 #if defined (COND_COMMAND)
1414 static int cond_lineno;
1415 static int cond_token;
1418 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1421 push_stream (reset_lineno)
1424 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1426 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1428 #if defined (BUFFERED_INPUT)
1429 saver->bstream = (BUFFERED_STREAM *)NULL;
1430 /* If we have a buffered stream, clear out buffers[fd]. */
1431 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1432 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1433 (BUFFERED_STREAM *)NULL);
1434 #endif /* BUFFERED_INPUT */
1436 saver->line = line_number;
1437 bash_input.name = (char *)NULL;
1438 saver->next = stream_list;
1439 stream_list = saver;
1452 STREAM_SAVER *saver = stream_list;
1455 stream_list = stream_list->next;
1457 init_yy_io (saver->bash_input.getter,
1458 saver->bash_input.ungetter,
1459 saver->bash_input.type,
1460 saver->bash_input.name,
1461 saver->bash_input.location);
1463 #if defined (BUFFERED_INPUT)
1464 /* If we have a buffered stream, restore buffers[fd]. */
1465 /* If the input file descriptor was changed while this was on the
1466 save stack, update the buffered fd to the new file descriptor and
1467 re-establish the buffer <-> bash_input fd correspondence. */
1468 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1470 if (bash_input_fd_changed)
1472 bash_input_fd_changed = 0;
1473 if (default_buffered_input >= 0)
1475 bash_input.location.buffered_fd = default_buffered_input;
1476 saver->bstream->b_fd = default_buffered_input;
1477 SET_CLOSE_ON_EXEC (default_buffered_input);
1480 /* XXX could free buffered stream returned as result here. */
1481 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1483 #endif /* BUFFERED_INPUT */
1485 line_number = saver->line;
1487 FREE (saver->bash_input.name);
1492 /* Return 1 if a stream of type TYPE is saved on the stack. */
1494 stream_on_stack (type)
1495 enum stream_type type;
1497 register STREAM_SAVER *s;
1499 for (s = stream_list; s; s = s->next)
1500 if (s->bash_input.type == type)
1505 /* Save the current token state and return it in a malloced array. */
1511 ret = (int *)xmalloc (3 * sizeof (int));
1512 ret[0] = last_read_token;
1513 ret[1] = token_before_that;
1514 ret[2] = two_tokens_ago;
1519 restore_token_state (ts)
1524 last_read_token = ts[0];
1525 token_before_that = ts[1];
1526 two_tokens_ago = ts[2];
1530 * This is used to inhibit alias expansion and reserved word recognition
1531 * inside case statement pattern lists. A `case statement pattern list' is:
1533 * everything between the `in' in a `case word in' and the next ')'
1535 * everything between a `;;' and the next `)' or `esac'
1538 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1540 #define END_OF_ALIAS 0
1543 * Pseudo-global variables used in implementing token-wise alias expansion.
1547 * Pushing and popping strings. This works together with shell_getc to
1548 * implement alias expansion on a per-token basis.
1551 typedef struct string_saver {
1552 struct string_saver *next;
1553 int expand_alias; /* Value to set expand_alias to when string is popped. */
1556 alias_t *expander; /* alias that caused this line to be pushed. */
1558 int saved_line_size, saved_line_index, saved_line_terminator;
1561 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1564 * Push the current shell_input_line onto a stack of such lines and make S
1565 * the current input. Used when expanding aliases. EXPAND is used to set
1566 * the value of expand_next_token when the string is popped, so that the
1567 * word after the alias in the original line is handled correctly when the
1568 * alias expands to multiple words. TOKEN is the token that was expanded
1569 * into S; it is saved and used to prevent infinite recursive expansion.
1572 push_string (s, expand, ap)
1577 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
1579 temp->expand_alias = expand;
1580 temp->saved_line = shell_input_line;
1581 temp->saved_line_size = shell_input_line_size;
1582 temp->saved_line_index = shell_input_line_index;
1583 temp->saved_line_terminator = shell_input_line_terminator;
1585 temp->expander = ap;
1587 temp->next = pushed_string_list;
1588 pushed_string_list = temp;
1592 ap->flags |= AL_BEINGEXPANDED;
1595 shell_input_line = s;
1596 shell_input_line_size = strlen (s);
1597 shell_input_line_index = 0;
1598 shell_input_line_terminator = '\0';
1600 parser_state &= ~PST_ALEXPNEXT; /* XXX */
1603 set_line_mbstate ();
1607 * Make the top of the pushed_string stack be the current shell input.
1608 * Only called when there is something on the stack. Called from shell_getc
1609 * when it thinks it has consumed the string generated by an alias expansion
1610 * and needs to return to the original input line.
1617 FREE (shell_input_line);
1618 shell_input_line = pushed_string_list->saved_line;
1619 shell_input_line_index = pushed_string_list->saved_line_index;
1620 shell_input_line_size = pushed_string_list->saved_line_size;
1621 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1623 if (pushed_string_list->expand_alias)
1624 parser_state |= PST_ALEXPNEXT;
1626 parser_state &= ~PST_ALEXPNEXT;
1628 t = pushed_string_list;
1629 pushed_string_list = pushed_string_list->next;
1633 t->expander->flags &= ~AL_BEINGEXPANDED;
1638 set_line_mbstate ();
1644 register STRING_SAVER *t, *t1;
1646 for (t = pushed_string_list; t; )
1649 FREE (t->saved_line);
1652 t->expander->flags &= ~AL_BEINGEXPANDED;
1657 pushed_string_list = (STRING_SAVER *)NULL;
1660 #endif /* ALIAS || DPAREN_ARITHMETIC */
1663 free_pushed_string_input ()
1665 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1666 free_string_list ();
1670 /* Return a line of text, taken from wherever yylex () reads input.
1671 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1672 is non-zero, we remove unquoted \<newline> pairs. This is used by
1673 read_secondary_line to read here documents. */
1675 read_a_line (remove_quoted_newline)
1676 int remove_quoted_newline;
1678 static char *line_buffer = (char *)NULL;
1679 static int buffer_size = 0;
1680 int indx = 0, c, peekc, pass_next;
1682 #if defined (READLINE)
1683 if (no_line_editing && SHOULD_PROMPT ())
1685 if (SHOULD_PROMPT ())
1692 /* Allow immediate exit if interrupted during input. */
1697 /* Ignore null bytes in input. */
1701 internal_warning ("read_a_line: ignored null byte in input");
1706 /* If there is no more input, then we return NULL. */
1709 if (interactive && bash_input.type == st_stream)
1712 return ((char *)NULL);
1716 /* `+2' in case the final character in the buffer is a newline. */
1717 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
1719 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
1720 here document with an unquoted delimiter. In this case,
1721 the line will be expanded as if it were in double quotes.
1722 We allow a backslash to escape the next character, but we
1723 need to treat the backslash specially only if a backslash
1724 quoting a backslash-newline pair appears in the line. */
1727 line_buffer[indx++] = c;
1730 else if (c == '\\' && remove_quoted_newline)
1736 continue; /* Make the unquoted \<newline> pair disappear. */
1742 line_buffer[indx++] = c; /* Preserve the backslash. */
1746 line_buffer[indx++] = c;
1750 line_buffer[indx] = '\0';
1751 return (line_buffer);
1756 /* Return a line as in read_a_line (), but insure that the prompt is
1757 the secondary prompt. This is used to read the lines of a here
1758 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
1759 newlines quoted with backslashes while reading the line. It is
1760 non-zero unless the delimiter of the here document was quoted. */
1762 read_secondary_line (remove_quoted_newline)
1763 int remove_quoted_newline;
1765 prompt_string_pointer = &ps2_prompt;
1766 if (SHOULD_PROMPT())
1768 return (read_a_line (remove_quoted_newline));
1771 /* **************************************************************** */
1775 /* **************************************************************** */
1777 /* Reserved words. These are only recognized as the first word of a
1779 STRING_INT_ALIST word_token_alist[] = {
1788 #if defined (SELECT_COMMAND)
1789 { "select", SELECT },
1796 { "function", FUNCTION },
1797 #if defined (COMMAND_TIMING)
1803 #if defined (COND_COMMAND)
1804 { "[[", COND_START },
1810 /* other tokens that can be returned by read_token() */
1811 STRING_INT_ALIST other_token_alist[] = {
1812 /* Multiple-character tokens with special values */
1816 { ">>", GREATER_GREATER },
1817 { "<<", LESS_LESS },
1819 { ">&", GREATER_AND },
1820 { ";;", SEMI_SEMI },
1822 { ";;&", SEMI_SEMI_AND },
1823 { "<<-", LESS_LESS_MINUS },
1824 { "<<<", LESS_LESS_LESS },
1825 { "&>", AND_GREATER },
1826 { "&>>", AND_GREATER_GREATER },
1827 { "<>", LESS_GREATER },
1828 { ">|", GREATER_BAR },
1830 { "EOF", yacc_EOF },
1831 /* Tokens whose value is the character itself */
1842 { "newline", '\n' },
1846 /* others not listed here:
1847 WORD look at yylval.word
1848 ASSIGNMENT_WORD look at yylval.word
1849 NUMBER look at yylval.number
1850 ARITH_CMD look at yylval.word_list
1851 ARITH_FOR_EXPRS look at yylval.word_list
1852 COND_CMD look at yylval.command
1855 /* These are used by read_token_word, but appear up here so that shell_getc
1856 can use them to decide when to add otherwise blank lines to the history. */
1858 /* The primary delimiter stack. */
1859 struct dstack dstack = { (char *)NULL, 0, 0 };
1861 /* A temporary delimiter stack to be used when decoding prompt strings.
1862 This is needed because command substitutions in prompt strings (e.g., PS2)
1863 can screw up the parser's quoting state. */
1864 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
1866 /* Macro for accessing the top delimiter on the stack. Returns the
1867 delimiter or zero if none. */
1868 #define current_delimiter(ds) \
1869 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
1871 #define push_delimiter(ds, character) \
1874 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
1875 ds.delimiters = (char *)xrealloc \
1876 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
1877 ds.delimiters[ds.delimiter_depth] = character; \
1878 ds.delimiter_depth++; \
1882 #define pop_delimiter(ds) ds.delimiter_depth--
1884 /* Return the next shell input character. This always reads characters
1885 from shell_input_line; when that line is exhausted, it is time to
1886 read the next line. This is called by read_token when the shell is
1887 processing normal command input. */
1889 /* This implements one-character lookahead/lookbehind across physical input
1890 lines, to avoid something being lost because it's pushed back with
1891 shell_ungetc when we're at the start of a line. */
1892 static int eol_ungetc_lookahead = 0;
1895 shell_getc (remove_quoted_newline)
1896 int remove_quoted_newline;
1904 if (sigwinch_received)
1906 sigwinch_received = 0;
1907 get_new_window_size (0, (int *)0, (int *)0);
1910 if (eol_ungetc_lookahead)
1912 c = eol_ungetc_lookahead;
1913 eol_ungetc_lookahead = 0;
1917 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1918 /* If shell_input_line[shell_input_line_index] == 0, but there is
1919 something on the pushed list of strings, then we don't want to go
1920 off and get another line. We let the code down below handle it. */
1922 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
1923 (pushed_string_list == (STRING_SAVER *)NULL)))
1924 #else /* !ALIAS && !DPAREN_ARITHMETIC */
1925 if (!shell_input_line || !shell_input_line[shell_input_line_index])
1926 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
1932 /* Allow immediate exit if interrupted during input. */
1936 shell_input_line_terminator = 0;
1938 /* If the shell is interatctive, but not currently printing a prompt
1939 (interactive_shell && interactive == 0), we don't want to print
1940 notifies or cleanup the jobs -- we want to defer it until we do
1941 print the next prompt. */
1942 if (interactive_shell == 0 || SHOULD_PROMPT())
1944 #if defined (JOB_CONTROL)
1945 /* This can cause a problem when reading a command as the result
1946 of a trap, when the trap is called from flush_child. This call
1947 had better not cause jobs to disappear from the job table in
1948 that case, or we will have big trouble. */
1949 notify_and_cleanup ();
1950 #else /* !JOB_CONTROL */
1951 cleanup_dead_jobs ();
1952 #endif /* !JOB_CONTROL */
1955 #if defined (READLINE)
1956 if (no_line_editing && SHOULD_PROMPT())
1958 if (SHOULD_PROMPT())
1962 if (bash_input.type == st_stream)
1969 /* Allow immediate exit if interrupted during input. */
1975 internal_warning ("shell_getc: ignored null byte in input");
1980 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
1984 if (bash_input.type == st_stream)
1988 shell_input_line_terminator = EOF;
1990 shell_input_line[i] = '\0';
1994 shell_input_line[i++] = c;
1998 shell_input_line[--i] = '\0';
1999 current_command_line_count++;
2004 shell_input_line_index = 0;
2005 shell_input_line_len = i; /* == strlen (shell_input_line) */
2007 set_line_mbstate ();
2009 #if defined (HISTORY)
2010 if (remember_on_history && shell_input_line && shell_input_line[0])
2013 # if defined (BANG_HISTORY)
2016 /* If the current delimiter is a single quote, we should not be
2017 performing history expansion, even if we're on a different
2018 line from the original single quote. */
2019 old_hist = history_expansion_inhibited;
2020 if (current_delimiter (dstack) == '\'')
2021 history_expansion_inhibited = 1;
2023 expansions = pre_process_line (shell_input_line, 1, 1);
2024 # if defined (BANG_HISTORY)
2025 history_expansion_inhibited = old_hist;
2027 if (expansions != shell_input_line)
2029 free (shell_input_line);
2030 shell_input_line = expansions;
2031 shell_input_line_len = shell_input_line ?
2032 strlen (shell_input_line) : 0;
2033 if (!shell_input_line_len)
2034 current_command_line_count--;
2036 /* We have to force the xrealloc below because we don't know
2037 the true allocated size of shell_input_line anymore. */
2038 shell_input_line_size = shell_input_line_len;
2040 set_line_mbstate ();
2043 /* Try to do something intelligent with blank lines encountered while
2044 entering multi-line commands. XXX - this is grotesque */
2045 else if (remember_on_history && shell_input_line &&
2046 shell_input_line[0] == '\0' &&
2047 current_command_line_count > 1)
2049 if (current_delimiter (dstack))
2050 /* We know shell_input_line[0] == 0 and we're reading some sort of
2051 quoted string. This means we've got a line consisting of only
2052 a newline in a quoted string. We want to make sure this line
2053 gets added to the history. */
2054 maybe_add_history (shell_input_line);
2058 hdcs = history_delimiting_chars ();
2059 if (hdcs && hdcs[0] == ';')
2060 maybe_add_history (shell_input_line);
2064 #endif /* HISTORY */
2066 if (shell_input_line)
2068 /* Lines that signify the end of the shell's input should not be
2070 if (echo_input_at_read && (shell_input_line[0] ||
2071 shell_input_line_terminator != EOF))
2072 fprintf (stderr, "%s\n", shell_input_line);
2076 shell_input_line_size = 0;
2077 prompt_string_pointer = ¤t_prompt_string;
2078 if (SHOULD_PROMPT ())
2083 /* Add the newline to the end of this string, iff the string does
2084 not already end in an EOF character. */
2085 if (shell_input_line_terminator != EOF)
2087 if (shell_input_line_len + 3 > shell_input_line_size)
2088 shell_input_line = (char *)xrealloc (shell_input_line,
2089 1 + (shell_input_line_size += 2));
2091 shell_input_line[shell_input_line_len] = '\n';
2092 shell_input_line[shell_input_line_len + 1] = '\0';
2094 set_line_mbstate ();
2098 uc = shell_input_line[shell_input_line_index];
2101 shell_input_line_index++;
2103 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2104 /* If UC is NULL, we have reached the end of the current input string. If
2105 pushed_string_list is non-empty, it's time to pop to the previous string
2106 because we have fully consumed the result of the last alias expansion.
2107 Do it transparently; just return the next character of the string popped
2109 if (!uc && (pushed_string_list != (STRING_SAVER *)NULL))
2112 uc = shell_input_line[shell_input_line_index];
2114 shell_input_line_index++;
2116 #endif /* ALIAS || DPAREN_ARITHMETIC */
2118 if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n')
2120 if (SHOULD_PROMPT ())
2126 if (!uc && shell_input_line_terminator == EOF)
2127 return ((shell_input_line_index != 0) ? '\n' : EOF);
2132 /* Put C back into the input for the shell. This might need changes for
2133 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
2134 character different than we read, shell_input_line_property doesn't need
2135 to change when manipulating shell_input_line. The define for
2136 last_shell_getc_is_singlebyte should take care of it, though. */
2141 if (shell_input_line && shell_input_line_index)
2142 shell_input_line[--shell_input_line_index] = c;
2144 eol_ungetc_lookahead = c;
2147 #ifdef INCLUDE_UNUSED
2148 /* Back the input pointer up by one, effectively `ungetting' a character. */
2152 if (shell_input_line && shell_input_line_index)
2153 shell_input_line_index--;
2157 /* Discard input until CHARACTER is seen, then push that character back
2158 onto the input stream. */
2160 discard_until (character)
2165 while ((c = shell_getc (0)) != EOF && c != character)
2173 execute_variable_command (command, vname)
2174 char *command, *vname;
2177 sh_parser_state_t ps;
2179 save_parser_state (&ps);
2180 last_lastarg = get_string_value ("_");
2182 last_lastarg = savestring (last_lastarg);
2184 parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST);
2186 restore_parser_state (&ps);
2187 bind_variable ("_", last_lastarg, 0);
2188 FREE (last_lastarg);
2190 if (token_to_read == '\n') /* reset_parser was called */
2194 /* Place to remember the token. We try to keep the buffer
2195 at a reasonable size, but it can grow. */
2196 static char *token = (char *)NULL;
2198 /* Current size of the token buffer. */
2199 static int token_buffer_size;
2201 /* Command to read_token () explaining what we want it to do. */
2204 #define prompt_is_ps1 \
2205 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
2207 /* Function for yyparse to call. yylex keeps track of
2208 the last two tokens read, and calls read_token. */
2212 if (interactive && (current_token == 0 || current_token == '\n'))
2214 /* Before we print a prompt, we might have to check mailboxes.
2215 We do this only if it is time to do so. Notice that only here
2216 is the mail alarm reset; nothing takes place in check_mail ()
2217 except the checking of mail. Please don't change this. */
2218 if (prompt_is_ps1 && time_to_check_mail ())
2221 reset_mail_timer ();
2224 /* Avoid printing a prompt if we're not going to read anything, e.g.
2225 after resetting the parser with read_token (RESET). */
2226 if (token_to_read == 0 && SHOULD_PROMPT ())
2230 two_tokens_ago = token_before_that;
2231 token_before_that = last_read_token;
2232 last_read_token = current_token;
2233 current_token = read_token (READ);
2234 return (current_token);
2237 /* When non-zero, we have read the required tokens
2238 which allow ESAC to be the next one read. */
2239 static int esacs_needed_count;
2242 gather_here_documents ()
2245 while (need_here_doc)
2247 make_here_document (redir_stack[r++]);
2252 /* When non-zero, an open-brace used to create a group is awaiting a close
2254 static int open_brace_count;
2256 #define command_token_position(token) \
2257 (((token) == ASSIGNMENT_WORD) || \
2258 ((token) != SEMI_SEMI && (token) != SEMI_AND && (token) != SEMI_SEMI_AND && reserved_word_acceptable(token)))
2260 #define assignment_acceptable(token) \
2261 (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0))
2263 /* Check to see if TOKEN is a reserved word and return the token
2265 #define CHECK_FOR_RESERVED_WORD(tok) \
2267 if (!dollar_present && !quoted && \
2268 reserved_word_acceptable (last_read_token)) \
2271 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
2272 if (STREQ (tok, word_token_alist[i].word)) \
2274 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
2276 if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \
2278 if (word_token_alist[i].token == ESAC) \
2279 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
2280 else if (word_token_alist[i].token == CASE) \
2281 parser_state |= PST_CASESTMT; \
2282 else if (word_token_alist[i].token == COND_END) \
2283 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
2284 else if (word_token_alist[i].token == COND_START) \
2285 parser_state |= PST_CONDCMD; \
2286 else if (word_token_alist[i].token == '{') \
2287 open_brace_count++; \
2288 else if (word_token_alist[i].token == '}' && open_brace_count) \
2289 open_brace_count--; \
2290 return (word_token_alist[i].token); \
2297 /* OK, we have a token. Let's try to alias expand it, if (and only if)
2300 It is eligible for expansion if EXPAND_ALIASES is set, and
2301 the token is unquoted and the last token read was a command
2302 separator (or expand_next_token is set), and we are currently
2303 processing an alias (pushed_string_list is non-empty) and this
2304 token is not the same as the current or any previously
2307 Special cases that disqualify:
2308 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
2318 r = xmalloc (l + 2);
2327 alias_expand_token (tokstr)
2333 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
2334 (parser_state & PST_CASEPAT) == 0)
2336 ap = find_alias (tokstr);
2338 /* Currently expanding this token. */
2339 if (ap && (ap->flags & AL_BEINGEXPANDED))
2340 return (NO_EXPANSION);
2342 /* mk_alexpansion puts an extra space on the end of the alias expansion,
2343 so the lookahead by the parser works right. If this gets changed,
2344 make sure the code in shell_getc that deals with reaching the end of
2345 an expanded alias is changed with it. */
2346 expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL;
2350 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
2351 return (RE_READ_TOKEN);
2354 /* This is an eligible token that does not have an expansion. */
2355 return (NO_EXPANSION);
2357 return (NO_EXPANSION);
2362 time_command_acceptable ()
2364 #if defined (COMMAND_TIMING)
2365 switch (last_read_token)
2384 #endif /* COMMAND_TIMING */
2387 /* Handle special cases of token recognition:
2388 IN is recognized if the last token was WORD and the token
2389 before that was FOR or CASE or SELECT.
2391 DO is recognized if the last token was WORD and the token
2392 before that was FOR or SELECT.
2394 ESAC is recognized if the last token caused `esacs_needed_count'
2397 `{' is recognized if the last token as WORD and the token
2398 before that was FUNCTION, or if we just parsed an arithmetic
2401 `}' is recognized if there is an unclosed `{' present.
2403 `-p' is returned as TIMEOPT if the last read token was TIME.
2405 ']]' is returned as COND_END if the parser is currently parsing
2406 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
2408 `time' is returned as TIME if and only if it is immediately
2409 preceded by one of `;', `\n', `||', `&&', or `&'.
2413 special_case_tokens (tokstr)
2416 if ((last_read_token == WORD) &&
2417 #if defined (SELECT_COMMAND)
2418 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
2420 ((token_before_that == FOR) || (token_before_that == CASE)) &&
2422 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
2424 if (token_before_that == CASE)
2426 parser_state |= PST_CASEPAT;
2427 esacs_needed_count++;
2432 if (last_read_token == WORD &&
2433 #if defined (SELECT_COMMAND)
2434 (token_before_that == FOR || token_before_that == SELECT) &&
2436 (token_before_that == FOR) &&
2438 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
2441 /* Ditto for ESAC in the CASE case.
2442 Specifically, this handles "case word in esac", which is a legal
2443 construct, certainly because someone will pass an empty arg to the
2444 case construct, and we don't want it to barf. Of course, we should
2445 insist that the case construct has at least one pattern in it, but
2446 the designers disagree. */
2447 if (esacs_needed_count)
2449 esacs_needed_count--;
2450 if (STREQ (tokstr, "esac"))
2452 parser_state &= ~PST_CASEPAT;
2457 /* The start of a shell function definition. */
2458 if (parser_state & PST_ALLOWOPNBRC)
2460 parser_state &= ~PST_ALLOWOPNBRC;
2461 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2464 function_bstart = line_number;
2465 return ('{'); /* } */
2469 /* We allow a `do' after a for ((...)) without an intervening
2471 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
2473 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2476 return ('{'); /* } */
2479 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
2481 open_brace_count--; /* { */
2485 #if defined (COMMAND_TIMING)
2486 /* Handle -p after `time'. */
2487 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
2492 #if defined (COMMAND_TIMING)
2493 if (STREQ (token, "time") && ((parser_state & PST_CASEPAT) == 0) && time_command_acceptable ())
2495 #endif /* COMMAND_TIMING */
2498 #if defined (COND_COMMAND) /* [[ */
2499 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
2506 /* Called from shell.c when Control-C is typed at top level. Or
2507 by the error rule at top level. */
2511 dstack.delimiter_depth = 0; /* No delimiters found so far. */
2512 open_brace_count = 0;
2516 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2517 if (pushed_string_list)
2518 free_string_list ();
2519 #endif /* ALIAS || DPAREN_ARITHMETIC */
2521 if (shell_input_line)
2523 free (shell_input_line);
2524 shell_input_line = (char *)NULL;
2525 shell_input_line_size = shell_input_line_index = 0;
2528 FREE (word_desc_to_read);
2529 word_desc_to_read = (WORD_DESC *)NULL;
2531 last_read_token = '\n';
2532 token_to_read = '\n';
2535 /* Read the next token. Command can be READ (normal operation) or
2536 RESET (to normalize state). */
2538 read_token (command)
2541 int character; /* Current character. */
2542 int peek_char; /* Temporary look-ahead character. */
2543 int result; /* The thing to return. */
2545 if (command == RESET)
2553 result = token_to_read;
2554 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
2556 yylval.word = word_desc_to_read;
2557 word_desc_to_read = (WORD_DESC *)NULL;
2563 #if defined (COND_COMMAND)
2564 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
2566 cond_lineno = line_number;
2567 parser_state |= PST_CONDEXPR;
2568 yylval.command = parse_cond_command ();
2569 if (cond_token != COND_END)
2574 token_to_read = COND_END;
2575 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
2581 /* This is a place to jump back to once we have successfully expanded a
2582 token with an alias and pushed the string with push_string () */
2586 /* Read a single word from input. Start by skipping blanks. */
2587 while ((character = shell_getc (1)) != EOF && shellblank (character))
2590 if (character == EOF)
2596 if MBTEST(character == '#' && (!interactive || interactive_comments))
2598 /* A comment. Discard until EOL or EOF, and then return a newline. */
2599 discard_until ('\n');
2601 character = '\n'; /* this will take the next if statement and return. */
2604 if (character == '\n')
2606 /* If we're about to return an unquoted newline, we can go and collect
2607 the text of any pending here document. */
2609 gather_here_documents ();
2612 parser_state &= ~PST_ALEXPNEXT;
2615 parser_state &= ~PST_ASSIGNOK;
2620 if (parser_state & PST_REGEXP)
2623 /* Shell meta-characters. */
2624 if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
2627 /* Turn off alias tokenization iff this character sequence would
2628 not leave us ready to read a command. */
2629 if (character == '<' || character == '>')
2630 parser_state &= ~PST_ALEXPNEXT;
2633 parser_state &= ~PST_ASSIGNOK;
2635 peek_char = shell_getc (1);
2636 if (character == peek_char)
2641 /* If '<' then we could be at "<<" or at "<<-". We have to
2642 look ahead one more character. */
2643 peek_char = shell_getc (1);
2644 if MBTEST(peek_char == '-')
2645 return (LESS_LESS_MINUS);
2646 else if MBTEST(peek_char == '<')
2647 return (LESS_LESS_LESS);
2650 shell_ungetc (peek_char);
2655 return (GREATER_GREATER);
2658 parser_state |= PST_CASEPAT;
2660 parser_state &= ~PST_ALEXPNEXT;
2662 peek_char = shell_getc (1);
2663 if MBTEST(peek_char == '&')
2664 return (SEMI_SEMI_AND);
2667 shell_ungetc (peek_char);
2677 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2679 result = parse_dparen (character);
2687 else if MBTEST(character == '<' && peek_char == '&')
2689 else if MBTEST(character == '>' && peek_char == '&')
2690 return (GREATER_AND);
2691 else if MBTEST(character == '<' && peek_char == '>')
2692 return (LESS_GREATER);
2693 else if MBTEST(character == '>' && peek_char == '|')
2694 return (GREATER_BAR);
2695 else if MBTEST(character == '&' && peek_char == '>')
2697 peek_char = shell_getc (1);
2698 if MBTEST(peek_char == '>')
2699 return (AND_GREATER_GREATER);
2702 shell_ungetc (peek_char);
2703 return (AND_GREATER);
2706 else if MBTEST(character == '|' && peek_char == '&')
2708 else if MBTEST(character == ';' && peek_char == '&')
2710 parser_state |= PST_CASEPAT;
2712 parser_state &= ~PST_ALEXPNEXT;
2717 shell_ungetc (peek_char);
2719 /* If we look like we are reading the start of a function
2720 definition, then let the reader know about it so that
2721 we will do the right thing with `{'. */
2722 if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
2724 parser_state |= PST_ALLOWOPNBRC;
2726 parser_state &= ~PST_ALEXPNEXT;
2728 function_dstart = line_number;
2731 /* case pattern lists may be preceded by an optional left paren. If
2732 we're not trying to parse a case pattern list, the left paren
2733 indicates a subshell. */
2734 if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
2735 parser_state |= PST_SUBSHELL;
2737 else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
2738 parser_state &= ~PST_CASEPAT;
2740 else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
2741 parser_state &= ~PST_SUBSHELL;
2743 #if defined (PROCESS_SUBSTITUTION)
2744 /* Check for the constructs which introduce process substitution.
2745 Shells running in `posix mode' don't do process substitution. */
2746 if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/
2747 #endif /* PROCESS_SUBSTITUTION */
2751 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
2752 if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
2756 /* Okay, if we got this far, we have to read a word. Read one,
2757 and then check it against the known ones. */
2758 result = read_token_word (character);
2760 if (result == RE_READ_TOKEN)
2767 * Match a $(...) or other grouping construct. This has to handle embedded
2768 * quoted strings ('', ``, "") and nested constructs. It also must handle
2769 * reprompting the user, if necessary, after reading a newline, and returning
2770 * correct error values if it reads EOF.
2772 #define P_FIRSTCLOSE 0x01
2773 #define P_ALLOWESC 0x02
2774 #define P_DQUOTE 0x04
2775 #define P_COMMAND 0x08 /* parsing a command, so look for comments */
2776 #define P_BACKQUOTE 0x10 /* parsing a backquoted command substitution */
2778 /* Lexical state while parsing a grouping construct or $(...). */
2779 #define LEX_WASDOL 0x001
2780 #define LEX_CKCOMMENT 0x002
2781 #define LEX_INCOMMENT 0x004
2782 #define LEX_PASSNEXT 0x008
2783 #define LEX_RESWDOK 0x010
2784 #define LEX_CKCASE 0x020
2785 #define LEX_INCASE 0x040
2786 #define LEX_INHEREDOC 0x080
2787 #define LEX_HEREDELIM 0x100 /* reading here-doc delimiter */
2788 #define LEX_STRIPDOC 0x200 /* <<- strip tabs from here doc delim */
2790 #define COMSUB_META(ch) ((ch) == ';' || (ch) == '&' || (ch) = '|')
2792 #define CHECK_NESTRET_ERROR() \
2794 if (nestret == &matched_pair_error) \
2797 return &matched_pair_error; \
2801 #define APPEND_NESTRET() \
2805 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); \
2806 strcpy (ret + retind, nestret); \
2807 retind += nestlen; \
2811 static char matched_pair_error;
2814 parse_matched_pair (qc, open, close, lenp, flags)
2815 int qc; /* `"' if this construct is within double quotes */
2819 int count, ch, was_dollar, in_comment, check_comment;
2820 int pass_next_character, backq_backslash, nestlen, ttranslen, start_lineno;
2821 char *ret, *nestret, *ttrans;
2822 int retind, retsize, rflags;
2824 /* itrace("parse_matched_pair: open = %c close = %c", open, close); */
2826 pass_next_character = backq_backslash = was_dollar = in_comment = 0;
2827 check_comment = (flags & P_COMMAND) && qc != '`' && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0;
2829 /* RFLAGS is the set of flags we want to pass to recursive calls. */
2830 rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE);
2832 ret = (char *)xmalloc (retsize = 64);
2835 start_lineno = line_number;
2838 ch = shell_getc (qc != '\'' && pass_next_character == 0 && backq_backslash == 0);
2843 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
2844 EOF_Reached = 1; /* XXX */
2845 return (&matched_pair_error);
2848 /* Possible reprompting. */
2849 if (ch == '\n' && SHOULD_PROMPT ())
2854 /* Add this character. */
2855 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
2863 /* Not exactly right yet, should handle shell metacharacters, too. If
2864 any changes are made to this test, make analogous changes to subst.c:
2865 extract_delimited_string(). */
2866 else if MBTEST(check_comment && in_comment == 0 && ch == '#' && (retind == 0 || ret[retind-1] == '\n' || whitespace (ret[retind - 1])))
2869 /* last char was backslash inside backquoted command substitution */
2870 if (backq_backslash)
2872 backq_backslash = 0;
2873 /* Placeholder for adding special characters */
2876 if (pass_next_character) /* last char was backslash */
2878 pass_next_character = 0;
2879 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
2881 if (retind > 0) retind--; /* swallow previously-added backslash */
2885 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2886 if MBTEST(ch == CTLESC || ch == CTLNUL)
2887 ret[retind++] = CTLESC;
2891 else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
2893 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2894 ret[retind++] = CTLESC;
2898 else if MBTEST(ch == close) /* ending delimiter */
2900 /* handle nested ${...} specially. */
2901 else if MBTEST(open != close && was_dollar && open == '{' && ch == open) /* } */
2903 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
2906 /* Add this character. */
2907 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
2910 if (open == '\'') /* '' inside grouping construct */
2912 if MBTEST((flags & P_ALLOWESC) && ch == '\\')
2913 pass_next_character++;
2915 else if MBTEST((flags & P_BACKQUOTE) && ch == '\\')
2921 if MBTEST(ch == '\\') /* backslashes */
2922 pass_next_character++;
2924 if (open != close) /* a grouping construct */
2926 if MBTEST(shellquote (ch))
2928 /* '', ``, or "" inside $(...) or other grouping construct. */
2929 push_delimiter (dstack, ch);
2930 if MBTEST(was_dollar && ch == '\'') /* $'...' inside group */
2931 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
2933 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
2934 pop_delimiter (dstack);
2935 if (nestret == &matched_pair_error)
2938 return &matched_pair_error;
2940 if MBTEST(was_dollar && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
2942 /* Translate $'...' here. */
2943 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
2946 if ((rflags & P_DQUOTE) == 0)
2948 nestret = sh_single_quote (ttrans);
2950 nestlen = strlen (nestret);
2955 nestlen = ttranslen;
2957 retind -= 2; /* back up before the $' */
2959 else if MBTEST(was_dollar && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
2961 /* Locale expand $"..." here. */
2962 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
2965 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
2967 nestlen = ttranslen + 2;
2968 retind -= 2; /* back up before the $" */
2973 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2974 strcpy (ret + retind, nestret);
2980 /* Parse an old-style command substitution within double quotes as a
2982 /* XXX - sh and ksh93 don't do this - XXX */
2983 else if MBTEST(open == '"' && ch == '`')
2985 nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags);
2987 if (nestret == &matched_pair_error)
2990 return &matched_pair_error;
2994 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2995 strcpy (ret + retind, nestret);
3001 else if MBTEST(qc == '`' && (ch == '"' || ch == '\'') && in_comment == 0)
3003 /* Add P_BACKQUOTE so backslash quotes the next character and
3004 shell_getc does the right thing with \<newline>. We do this for
3005 a measure of backwards compatibility -- it's not strictly the
3006 right POSIX thing. */
3007 nestret = parse_matched_pair (0, ch, ch, &nestlen, rflags|P_BACKQUOTE);
3011 else if MBTEST(open != '`' && was_dollar && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
3012 /* check for $(), $[], or ${} inside quoted string. */
3014 if (open == ch) /* undo previous increment */
3016 if (ch == '(') /* ) */
3017 nestret = parse_matched_pair (0, '(', ')', &nestlen, rflags & ~P_DQUOTE);
3018 else if (ch == '{') /* } */
3019 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|rflags);
3020 else if (ch == '[') /* ] */
3021 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
3025 was_dollar = MBTEST(ch == '$');
3034 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
3035 /* Parse a double-paren construct. It can be either an arithmetic
3036 command, an arithmetic `for' command, or a nested subshell. Returns
3037 the parsed token, -1 on error, or -2 if we didn't do anything and
3038 should just go on. */
3047 #if defined (ARITH_FOR_COMMAND)
3048 if (last_read_token == FOR)
3050 arith_for_lineno = line_number;
3051 cmdtyp = parse_arith_cmd (&wval, 0);
3054 wd = alloc_word_desc ();
3056 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
3057 return (ARITH_FOR_EXPRS);
3060 return -1; /* ERROR */
3064 #if defined (DPAREN_ARITHMETIC)
3065 if (reserved_word_acceptable (last_read_token))
3067 sline = line_number;
3069 cmdtyp = parse_arith_cmd (&wval, 0);
3070 if (cmdtyp == 1) /* arithmetic command */
3072 wd = alloc_word_desc ();
3074 wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE;
3075 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
3078 else if (cmdtyp == 0) /* nested subshell */
3080 push_string (wval, 0, (alias_t *)NULL);
3081 if ((parser_state & PST_CASEPAT) == 0)
3082 parser_state |= PST_SUBSHELL;
3090 return -2; /* XXX */
3093 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
3094 If not, assume it's a nested subshell for backwards compatibility and
3095 return 0. In any case, put the characters we've consumed into a locally-
3096 allocated buffer and make *ep point to that buffer. Return -1 on an
3097 error, for example EOF. */
3099 parse_arith_cmd (ep, adddq)
3103 int exp_lineno, rval, c;
3104 char *ttok, *tokstr;
3107 exp_lineno = line_number;
3108 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
3110 if (ttok == &matched_pair_error)
3112 /* Check that the next character is the closing right paren. If
3113 not, this is a syntax error. ( */
3118 tokstr = (char *)xmalloc (ttoklen + 4);
3120 /* if ADDDQ != 0 then (( ... )) -> "..." */
3121 if (rval == 1 && adddq) /* arith cmd, add double quotes */
3124 strncpy (tokstr + 1, ttok, ttoklen - 1);
3125 tokstr[ttoklen] = '"';
3126 tokstr[ttoklen+1] = '\0';
3128 else if (rval == 1) /* arith cmd, don't add double quotes */
3130 strncpy (tokstr, ttok, ttoklen - 1);
3131 tokstr[ttoklen-1] = '\0';
3133 else /* nested subshell */
3136 strncpy (tokstr + 1, ttok, ttoklen - 1);
3137 tokstr[ttoklen] = ')';
3138 tokstr[ttoklen+1] = c;
3139 tokstr[ttoklen+2] = '\0';
3146 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
3148 #if defined (COND_COMMAND)
3154 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
3155 parser_error (cond_lineno, _("unexpected EOF while looking for `]]'"));
3156 else if (cond_token != COND_ERROR)
3158 if (etext = error_token_from_token (cond_token))
3160 parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext);
3164 parser_error (cond_lineno, _("syntax error in conditional expression"));
3171 return (cond_or ());
3180 if (cond_token == OR_OR)
3183 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
3194 if (cond_token == AND_AND)
3197 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
3203 cond_skip_newlines ()
3205 while ((cond_token = read_token (READ)) == '\n')
3207 if (SHOULD_PROMPT ())
3210 return (cond_token);
3213 #define COND_RETURN_ERROR() \
3214 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
3220 COND_COM *term, *tleft, *tright;
3224 /* Read a token. It can be a left paren, a `!', a unary operator, or a
3225 word that should be the first argument of a binary operator. Start by
3226 skipping newlines, since this is a compound command. */
3227 tok = cond_skip_newlines ();
3228 lineno = line_number;
3229 if (tok == COND_END)
3231 COND_RETURN_ERROR ();
3233 else if (tok == '(')
3235 term = cond_expr ();
3236 if (cond_token != ')')
3239 dispose_cond_node (term); /* ( */
3240 if (etext = error_token_from_token (cond_token))
3242 parser_error (lineno, _("unexpected token `%s', expected `)'"), etext);
3246 parser_error (lineno, _("expected `)'"));
3247 COND_RETURN_ERROR ();
3249 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
3250 (void)cond_skip_newlines ();
3252 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
3255 dispose_word (yylval.word); /* not needed */
3256 term = cond_term ();
3258 term->flags |= CMD_INVERT_RETURN;
3260 else if (tok == WORD && yylval.word->word[0] == '-' && yylval.word->word[2] == 0 && test_unop (yylval.word->word))
3263 tok = read_token (READ);
3266 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3267 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
3272 if (etext = error_token_from_token (tok))
3274 parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext);
3278 parser_error (line_number, _("unexpected argument to conditional unary operator"));
3279 COND_RETURN_ERROR ();
3282 (void)cond_skip_newlines ();
3284 else if (tok == WORD) /* left argument to binary operator */
3287 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3290 tok = read_token (READ);
3291 if (tok == WORD && test_binop (yylval.word->word))
3293 #if defined (COND_REGEXP)
3294 else if (tok == WORD && STREQ (yylval.word->word, "=~"))
3297 parser_state |= PST_REGEXP;
3300 else if (tok == '<' || tok == '>')
3301 op = make_word_from_token (tok); /* ( */
3302 /* There should be a check before blindly accepting the `)' that we have
3303 seen the opening `('. */
3304 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
3306 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
3307 the test command. Similarly for [[ x && expr ]] or
3308 [[ x || expr ]] or [[ (x) ]]. */
3309 op = make_word ("-n");
3310 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
3316 if (etext = error_token_from_token (tok))
3318 parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext);
3322 parser_error (line_number, _("conditional binary operator expected"));
3323 dispose_cond_node (tleft);
3324 COND_RETURN_ERROR ();
3328 tok = read_token (READ);
3329 parser_state &= ~PST_REGEXP;
3332 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3333 term = make_cond_node (COND_BINARY, op, tleft, tright);
3337 if (etext = error_token_from_token (tok))
3339 parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext);
3343 parser_error (line_number, _("unexpected argument to conditional binary operator"));
3344 dispose_cond_node (tleft);
3346 COND_RETURN_ERROR ();
3349 (void)cond_skip_newlines ();
3354 parser_error (line_number, _("unexpected token `%c' in conditional command"), tok);
3355 else if (etext = error_token_from_token (tok))
3357 parser_error (line_number, _("unexpected token `%s' in conditional command"), etext);
3361 parser_error (line_number, _("unexpected token %d in conditional command"), tok);
3362 COND_RETURN_ERROR ();
3367 /* This is kind of bogus -- we slip a mini recursive-descent parser in
3368 here to handle the conditional statement syntax. */
3370 parse_cond_command ()
3374 cexp = cond_expr ();
3375 return (make_cond_command (cexp));
3379 #if defined (ARRAY_VARS)
3380 /* When this is called, it's guaranteed that we don't care about anything
3381 in t beyond i. We do save and restore the chars, though. */
3383 token_is_assignment (t, i)
3387 unsigned char c, c1;
3390 c = t[i]; c1 = t[i+1];
3391 t[i] = '='; t[i+1] = '\0';
3392 r = assignment (t, (parser_state & PST_COMPASSIGN) != 0);
3393 t[i] = c; t[i+1] = c1;
3397 /* XXX - possible changes here for `+=' */
3399 token_is_ident (t, i)
3408 r = legal_identifier (t);
3415 read_token_word (character)
3418 /* The value for YYLVAL when a WORD is read. */
3419 WORD_DESC *the_word;
3421 /* Index into the token that we are building. */
3424 /* ALL_DIGITS becomes zero when we see a non-digit. */
3425 int all_digit_token;
3427 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
3430 /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
3432 int compound_assignment;
3434 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
3437 /* Non-zero means to ignore the value of the next character, and just
3438 to add it no matter what. */
3439 int pass_next_character;
3441 /* The current delimiting character. */
3443 int result, peek_char;
3444 char *ttok, *ttrans;
3445 int ttoklen, ttranslen;
3448 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
3449 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
3452 all_digit_token = DIGIT (character);
3453 dollar_present = quoted = pass_next_character = compound_assignment = 0;
3457 if (character == EOF)
3460 if (pass_next_character)
3462 pass_next_character = 0;
3463 goto got_escaped_character;
3466 cd = current_delimiter (dstack);
3468 /* Handle backslashes. Quote lots of things when not inside of
3469 double-quotes, quote some things inside of double-quotes. */
3470 if MBTEST(character == '\\')
3472 peek_char = shell_getc (0);
3474 /* Backslash-newline is ignored in all cases except
3475 when quoted with single quotes. */
3476 if (peek_char == '\n')
3479 goto next_character;
3483 shell_ungetc (peek_char);
3485 /* If the next character is to be quoted, note it now. */
3486 if (cd == 0 || cd == '`' ||
3487 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
3488 pass_next_character++;
3495 /* Parse a matched pair of quote characters. */
3496 if MBTEST(shellquote (character))
3498 push_delimiter (dstack, character);
3499 ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0);
3500 pop_delimiter (dstack);
3501 if (ttok == &matched_pair_error)
3502 return -1; /* Bail immediately. */
3503 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3504 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
3505 token[token_index++] = character;
3506 strcpy (token + token_index, ttok);
3507 token_index += ttoklen;
3508 all_digit_token = 0;
3510 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
3512 goto next_character;
3516 /* When parsing a regexp as a single word inside a conditional command,
3517 we need to special-case characters special to both the shell and
3518 regular expressions. Right now, that is only '(' and '|'. */ /*)*/
3519 if MBTEST((parser_state & PST_REGEXP) && (character == '(' || character == '|')) /*)*/
3521 if (character == '|')
3524 push_delimiter (dstack, character);
3525 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
3526 pop_delimiter (dstack);
3527 if (ttok == &matched_pair_error)
3528 return -1; /* Bail immediately. */
3529 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3530 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
3531 token[token_index++] = character;
3532 strcpy (token + token_index, ttok);
3533 token_index += ttoklen;
3535 dollar_present = all_digit_token = 0;
3536 goto next_character;
3538 #endif /* COND_REGEXP */
3540 #ifdef EXTENDED_GLOB
3541 /* Parse a ksh-style extended pattern matching specification. */
3542 if MBTEST(extended_glob && PATTERN_CHAR (character))
3544 peek_char = shell_getc (1);
3545 if MBTEST(peek_char == '(') /* ) */
3547 push_delimiter (dstack, peek_char);
3548 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
3549 pop_delimiter (dstack);
3550 if (ttok == &matched_pair_error)
3551 return -1; /* Bail immediately. */
3552 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3554 TOKEN_DEFAULT_GROW_SIZE);
3555 token[token_index++] = character;
3556 token[token_index++] = peek_char;
3557 strcpy (token + token_index, ttok);
3558 token_index += ttoklen;
3560 dollar_present = all_digit_token = 0;
3561 goto next_character;
3564 shell_ungetc (peek_char);
3566 #endif /* EXTENDED_GLOB */
3568 /* If the delimiter character is not single quote, parse some of
3569 the shell expansions that must be read as a single word. */
3570 if (shellexp (character))
3572 peek_char = shell_getc (1);
3573 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
3574 if MBTEST(peek_char == '(' || \
3575 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
3577 if (peek_char == '{') /* } */
3578 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE);
3579 else if (peek_char == '(') /* ) */
3581 /* XXX - push and pop the `(' as a delimiter for use by
3582 the command-oriented-history code. This way newlines
3583 appearing in the $(...) string get added to the
3584 history literally rather than causing a possibly-
3585 incorrect `;' to be added. ) */
3586 push_delimiter (dstack, peek_char);
3587 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, P_COMMAND);
3588 pop_delimiter (dstack);
3591 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
3592 if (ttok == &matched_pair_error)
3593 return -1; /* Bail immediately. */
3594 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3596 TOKEN_DEFAULT_GROW_SIZE);
3597 token[token_index++] = character;
3598 token[token_index++] = peek_char;
3599 strcpy (token + token_index, ttok);
3600 token_index += ttoklen;
3603 all_digit_token = 0;
3604 goto next_character;
3606 /* This handles $'...' and $"..." new-style quoted strings. */
3607 else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
3611 first_line = line_number;
3612 push_delimiter (dstack, peek_char);
3613 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
3615 (peek_char == '\'') ? P_ALLOWESC : 0);
3616 pop_delimiter (dstack);
3617 if (ttok == &matched_pair_error)
3619 if (peek_char == '\'')
3621 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
3624 /* Insert the single quotes and correctly quote any
3625 embedded single quotes (allowed because P_ALLOWESC was
3626 passed to parse_matched_pair). */
3627 ttok = sh_single_quote (ttrans);
3629 ttranslen = strlen (ttok);
3634 /* Try to locale-expand the converted string. */
3635 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
3638 /* Add the double quotes back */
3639 ttok = sh_mkdoublequoted (ttrans, ttranslen, 0);
3645 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2,
3647 TOKEN_DEFAULT_GROW_SIZE);
3648 strcpy (token + token_index, ttrans);
3649 token_index += ttranslen;
3652 all_digit_token = 0;
3653 goto next_character;
3655 /* This could eventually be extended to recognize all of the
3656 shell's single-character parameter expansions, and set flags.*/
3657 else if MBTEST(character == '$' && peek_char == '$')
3659 ttok = (char *)xmalloc (3);
3660 ttok[0] = ttok[1] = '$';
3662 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
3664 TOKEN_DEFAULT_GROW_SIZE);
3665 strcpy (token + token_index, ttok);
3668 all_digit_token = 0;
3670 goto next_character;
3673 shell_ungetc (peek_char);
3676 #if defined (ARRAY_VARS)
3677 /* Identify possible array subscript assignment; match [...] */
3678 else if MBTEST(character == '[' && token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) /* ] */
3680 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
3681 if (ttok == &matched_pair_error)
3682 return -1; /* Bail immediately. */
3683 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3685 TOKEN_DEFAULT_GROW_SIZE);
3686 token[token_index++] = character;
3687 strcpy (token + token_index, ttok);
3688 token_index += ttoklen;
3690 all_digit_token = 0;
3691 goto next_character;
3693 /* Identify possible compound array variable assignment. */
3694 else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index))
3696 peek_char = shell_getc (1);
3697 if MBTEST(peek_char == '(') /* ) */
3699 ttok = parse_compound_assignment (&ttoklen);
3701 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
3703 TOKEN_DEFAULT_GROW_SIZE);
3705 token[token_index++] = '=';
3706 token[token_index++] = '(';
3709 strcpy (token + token_index, ttok);
3710 token_index += ttoklen;
3712 token[token_index++] = ')';
3714 all_digit_token = 0;
3715 compound_assignment = 1;
3717 goto next_character;
3719 goto got_token; /* ksh93 seems to do this */
3723 shell_ungetc (peek_char);
3727 /* When not parsing a multi-character word construct, shell meta-
3728 characters break words. */
3729 if MBTEST(shellbreak (character))
3731 shell_ungetc (character);
3737 if (character == CTLESC || character == CTLNUL)
3738 token[token_index++] = CTLESC;
3740 got_escaped_character:
3742 all_digit_token &= DIGIT (character);
3743 dollar_present |= character == '$';
3745 token[token_index++] = character;
3747 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
3748 TOKEN_DEFAULT_GROW_SIZE);
3751 if (character == '\n' && SHOULD_PROMPT ())
3754 /* We want to remove quoted newlines (that is, a \<newline> pair)
3755 unless we are within single quotes or pass_next_character is
3756 set (the shell equivalent of literal-next). */
3757 cd = current_delimiter (dstack);
3758 character = shell_getc (cd != '\'' && pass_next_character == 0);
3759 } /* end for (;;) */
3763 token[token_index] = '\0';
3765 /* Check to see what thing we should return. If the last_read_token
3766 is a `<', or a `&', or the character which ended this token is
3767 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
3768 Otherwise, it is just a word, and should be returned as such. */
3769 if MBTEST(all_digit_token && (character == '<' || character == '>' || \
3770 last_read_token == LESS_AND || \
3771 last_read_token == GREATER_AND))
3773 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
3774 yylval.number = lvalue;
3780 /* Check for special case tokens. */
3781 result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
3786 /* Posix.2 does not allow reserved words to be aliased, so check for all
3787 of them, including special cases, before expanding the current token
3789 if MBTEST(posixly_correct)
3790 CHECK_FOR_RESERVED_WORD (token);
3792 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
3793 inhibits alias expansion. */
3794 if (expand_aliases && quoted == 0)
3796 result = alias_expand_token (token);
3797 if (result == RE_READ_TOKEN)
3798 return (RE_READ_TOKEN);
3799 else if (result == NO_EXPANSION)
3800 parser_state &= ~PST_ALEXPNEXT;
3803 /* If not in Posix.2 mode, check for reserved words after alias
3805 if MBTEST(posixly_correct == 0)
3807 CHECK_FOR_RESERVED_WORD (token);
3809 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
3810 the_word->word = (char *)xmalloc (1 + token_index);
3811 the_word->flags = 0;
3812 strcpy (the_word->word, token);
3814 the_word->flags |= W_HASDOLLAR;
3816 the_word->flags |= W_QUOTED; /*(*/
3817 if (compound_assignment && token[token_index-1] == ')')
3818 the_word->flags |= W_COMPASSIGN;
3819 /* A word is an assignment if it appears at the beginning of a
3820 simple command, or after another assignment word. This is
3821 context-dependent, so it cannot be handled in the grammar. */
3822 if (assignment (token, (parser_state & PST_COMPASSIGN) != 0))
3824 the_word->flags |= W_ASSIGNMENT;
3825 /* Don't perform word splitting on assignment statements. */
3826 if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0)
3827 the_word->flags |= W_NOSPLIT;
3830 if (command_token_position (last_read_token))
3833 b = builtin_address_internal (token, 0);
3834 if (b && (b->flags & ASSIGNMENT_BUILTIN))
3835 parser_state |= PST_ASSIGNOK;
3836 else if (STREQ (token, "eval") || STREQ (token, "let"))
3837 parser_state |= PST_ASSIGNOK;
3840 yylval.word = the_word;
3842 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
3843 ? ASSIGNMENT_WORD : WORD;
3845 switch (last_read_token)
3848 parser_state |= PST_ALLOWOPNBRC;
3849 function_dstart = line_number;
3854 if (word_top < MAX_CASE_NEST)
3856 word_lineno[word_top] = line_number;
3863 /* Return 1 if TOKSYM is a token that after being read would allow
3864 a reserved word to be seen, else 0. */
3866 reserved_word_acceptable (toksym)
3904 /* Return the index of TOKEN in the alist of reserved words, or -1 if
3905 TOKEN is not a shell reserved word. */
3907 find_reserved_word (tokstr)
3911 for (i = 0; word_token_alist[i].word; i++)
3912 if (STREQ (tokstr, word_token_alist[i].word))
3918 #if defined (READLINE)
3919 /* Called after each time readline is called. This insures that whatever
3920 the new prompt string is gets propagated to readline's local prompt
3923 reset_readline_prompt ()
3927 if (prompt_string_pointer)
3929 temp_prompt = (*prompt_string_pointer)
3930 ? decode_prompt_string (*prompt_string_pointer)
3933 if (temp_prompt == 0)
3935 temp_prompt = (char *)xmalloc (1);
3936 temp_prompt[0] = '\0';
3939 FREE (current_readline_prompt);
3940 current_readline_prompt = temp_prompt;
3943 #endif /* READLINE */
3946 #if defined (HISTORY)
3947 /* A list of tokens which can be followed by newlines, but not by
3948 semi-colons. When concatenating multiple lines of history, the
3949 newline separator for such tokens is replaced with a space. */
3950 static const int no_semi_successors[] = {
3951 '\n', '{', '(', ')', ';', '&', '|',
3952 CASE, DO, ELSE, IF, SEMI_SEMI, SEMI_AND, SEMI_SEMI_AND, THEN, UNTIL,
3953 WHILE, AND_AND, OR_OR, IN,
3957 /* If we are not within a delimited expression, try to be smart
3958 about which separators can be semi-colons and which must be
3959 newlines. Returns the string that should be added into the
3962 history_delimiting_chars ()
3966 if (dstack.delimiter_depth != 0)
3969 /* First, handle some special cases. */
3971 /* If we just read `()', assume it's a function definition, and don't
3972 add a semicolon. If the token before the `)' was not `(', and we're
3973 not in the midst of parsing a case statement, assume it's a
3974 parenthesized command and add the semicolon. */
3976 if (token_before_that == ')')
3978 if (two_tokens_ago == '(') /*)*/ /* function def */
3980 /* This does not work for subshells inside case statement
3981 command lists. It's a suboptimal solution. */
3982 else if (parser_state & PST_CASESTMT) /* case statement pattern */
3985 return "; "; /* (...) subshell */
3987 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
3988 return " "; /* function def using `function name' without `()' */
3990 else if (token_before_that == WORD && two_tokens_ago == FOR)
3992 /* Tricky. `for i\nin ...' should not have a semicolon, but
3993 `for i\ndo ...' should. We do what we can. */
3994 for (i = shell_input_line_index; whitespace(shell_input_line[i]); i++)
3996 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
4000 else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT))
4003 for (i = 0; no_semi_successors[i]; i++)
4005 if (token_before_that == no_semi_successors[i])
4011 #endif /* HISTORY */
4013 /* Issue a prompt, or prepare to issue a prompt when the next character
4020 if (interactive == 0 || expanding_alias()) /* XXX */
4023 ps1_prompt = get_string_value ("PS1");
4024 ps2_prompt = get_string_value ("PS2");
4026 if (!prompt_string_pointer)
4027 prompt_string_pointer = &ps1_prompt;
4029 temp_prompt = *prompt_string_pointer
4030 ? decode_prompt_string (*prompt_string_pointer)
4033 if (temp_prompt == 0)
4035 temp_prompt = (char *)xmalloc (1);
4036 temp_prompt[0] = '\0';
4039 current_prompt_string = *prompt_string_pointer;
4040 prompt_string_pointer = &ps2_prompt;
4042 #if defined (READLINE)
4043 if (!no_line_editing)
4045 FREE (current_readline_prompt);
4046 current_readline_prompt = temp_prompt;
4049 #endif /* READLINE */
4051 FREE (current_decoded_prompt);
4052 current_decoded_prompt = temp_prompt;
4057 get_current_prompt_level ()
4059 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
4063 set_current_prompt_level (x)
4066 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
4067 current_prompt_string = *prompt_string_pointer;
4073 fprintf (stderr, "%s", current_decoded_prompt);
4077 /* Return a string which will be printed as a prompt. The string
4078 may contain special characters which are decoded as follows:
4081 \d the date in Day Mon Date format
4082 \e escape (ascii 033)
4083 \h the hostname up to the first `.'
4085 \j the number of active jobs
4086 \l the basename of the shell's tty device name
4089 \s the name of the shell
4090 \t the time in 24-hour hh:mm:ss format
4091 \T the time in 12-hour hh:mm:ss format
4092 \@ the time in 12-hour hh:mm am/pm format
4093 \A the time in 24-hour hh:mm format
4094 \D{fmt} the result of passing FMT to strftime(3)
4096 \v the version of bash (e.g., 2.00)
4097 \V the release of bash, version + patchlevel (e.g., 2.00.0)
4098 \w the current working directory
4099 \W the last element of $PWD
4100 \! the history number of this command
4101 \# the command number of this command
4102 \$ a $ or a # if you are root
4103 \nnn character code nnn in octal
4105 \[ begin a sequence of non-printing chars
4106 \] end a sequence of non-printing chars
4108 #define PROMPT_GROWTH 48
4110 decode_prompt_string (string)
4115 struct dstack save_dstack;
4116 int last_exit_value;
4117 #if defined (PROMPT_STRING_DECODE)
4118 int result_size, result_index;
4120 char *temp, octal_string[4];
4126 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
4127 result[result_index = 0] = 0;
4128 temp = (char *)NULL;
4130 while (c = *string++)
4132 if (posixly_correct && c == '!')
4136 temp = savestring ("!");
4141 #if !defined (HISTORY)
4142 temp = savestring ("1");
4144 temp = itos (history_number ());
4145 #endif /* HISTORY */
4146 string--; /* add_string increments string again. */
4164 strncpy (octal_string, string, 3);
4165 octal_string[3] = '\0';
4167 n = read_octal (octal_string);
4168 temp = (char *)xmalloc (3);
4170 if (n == CTLESC || n == CTLNUL)
4187 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
4190 c = 0; /* tested at add_string: */
4198 /* Make the current time/date into a string. */
4199 (void) time (&the_time);
4200 tm = localtime (&the_time);
4203 n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm);
4205 n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm);
4207 n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm);
4209 n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm);
4211 n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm);
4216 timebuf[sizeof(timebuf) - 1] = '\0';
4218 temp = savestring (timebuf);
4221 case 'D': /* strftime format */
4222 if (string[1] != '{') /* } */
4225 (void) time (&the_time);
4226 tm = localtime (&the_time);
4227 string += 2; /* skip { */
4228 timefmt = xmalloc (strlen (string) + 3);
4229 for (t = timefmt; *string && *string != '}'; )
4232 c = *string; /* tested at add_string */
4233 if (timefmt[0] == '\0')
4236 timefmt[1] = 'X'; /* locale-specific current time */
4239 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
4245 timebuf[sizeof(timebuf) - 1] = '\0';
4247 if (promptvars || posixly_correct)
4248 /* Make sure that expand_prompt_string is called with a
4249 second argument of Q_DOUBLE_QUOTES if we use this
4251 temp = sh_backslash_quote_for_double_quotes (timebuf);
4253 temp = savestring (timebuf);
4257 temp = (char *)xmalloc (3);
4258 temp[0] = no_line_editing ? '\n' : '\r';
4259 temp[1] = no_line_editing ? '\0' : '\n';
4264 temp = base_pathname (shell_name);
4265 temp = savestring (temp);
4270 temp = (char *)xmalloc (16);
4272 strcpy (temp, dist_version);
4274 sprintf (temp, "%s.%d", dist_version, patch_level);
4280 /* Use the value of PWD because it is much more efficient. */
4281 char t_string[PATH_MAX];
4284 temp = get_string_value ("PWD");
4288 if (getcwd (t_string, sizeof(t_string)) == 0)
4294 tlen = strlen (t_string);
4298 tlen = sizeof (t_string) - 1;
4299 strncpy (t_string, temp, tlen);
4301 t_string[tlen] = '\0';
4303 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
4304 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
4305 /* Abbreviate \W as ~ if $PWD == $HOME */
4306 if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0))
4308 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
4310 t = strrchr (t_string, '/');
4312 strcpy (t_string, t + 1);
4316 #undef DOUBLE_SLASH_ROOT
4318 /* polite_directory_format is guaranteed to return a string
4319 no longer than PATH_MAX - 1 characters. */
4320 strcpy (t_string, polite_directory_format (t_string));
4322 /* If we're going to be expanding the prompt string later,
4323 quote the directory name. */
4324 if (promptvars || posixly_correct)
4325 /* Make sure that expand_prompt_string is called with a
4326 second argument of Q_DOUBLE_QUOTES if we use this
4328 temp = sh_backslash_quote_for_double_quotes (t_string);
4330 temp = savestring (t_string);
4336 if (current_user.user_name == 0)
4337 get_current_user_info ();
4338 temp = savestring (current_user.user_name);
4343 temp = savestring (current_host_name);
4344 if (c == 'h' && (t = (char *)strchr (temp, '.')))
4349 temp = itos (current_command_number);
4353 #if !defined (HISTORY)
4354 temp = savestring ("1");
4356 temp = itos (history_number ());
4357 #endif /* HISTORY */
4361 t = temp = (char *)xmalloc (3);
4362 if ((promptvars || posixly_correct) && (current_user.euid != 0))
4364 *t++ = current_user.euid == 0 ? '#' : '$';
4369 temp = itos (count_all_jobs ());
4373 #if defined (HAVE_TTYNAME)
4374 temp = (char *)ttyname (fileno (stdin));
4375 t = temp ? base_pathname (temp) : "tty";
4376 temp = savestring (t);
4378 temp = savestring ("tty");
4379 #endif /* !HAVE_TTYNAME */
4382 #if defined (READLINE)
4385 if (no_line_editing)
4390 temp = (char *)xmalloc (3);
4391 n = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
4393 if (n == CTLESC || n == CTLNUL)
4398 #endif /* READLINE */
4404 temp = (char *)xmalloc (2);
4411 else /* (c == '\\') */
4418 temp = (char *)xmalloc (3);
4427 sub_append_string (temp, result, &result_index, &result_size);
4428 temp = (char *)NULL; /* Freed in sub_append_string (). */
4429 result[result_index] = '\0';
4435 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
4436 result[result_index++] = c;
4437 result[result_index] = '\0';
4440 #else /* !PROMPT_STRING_DECODE */
4441 result = savestring (string);
4442 #endif /* !PROMPT_STRING_DECODE */
4444 /* Save the delimiter stack and point `dstack' to temp space so any
4445 command substitutions in the prompt string won't result in screwing
4446 up the parser's quoting state. */
4447 save_dstack = dstack;
4448 dstack = temp_dstack;
4449 dstack.delimiter_depth = 0;
4451 /* Perform variable and parameter expansion and command substitution on
4452 the prompt string. */
4453 if (promptvars || posixly_correct)
4455 last_exit_value = last_command_exit_value;
4456 list = expand_prompt_string (result, Q_DOUBLE_QUOTES, 0);
4458 result = string_list (list);
4459 dispose_words (list);
4460 last_command_exit_value = last_exit_value;
4464 t = dequote_string (result);
4469 dstack = save_dstack;
4474 /************************************************
4478 ************************************************/
4480 /* Report a syntax error, and restart the parser. Call here for fatal
4486 report_syntax_error ((char *)NULL);
4492 error_token_from_token (tok)
4497 if (t = find_token_in_alist (tok, word_token_alist, 0))
4500 if (t = find_token_in_alist (tok, other_token_alist, 0))
4504 /* This stuff is dicy and needs closer inspection */
4505 switch (current_token)
4508 case ASSIGNMENT_WORD:
4510 t = savestring (yylval.word->word);
4513 t = itos (yylval.number);
4516 if (yylval.word_list)
4517 t = string_list (yylval.word_list);
4519 case ARITH_FOR_EXPRS:
4520 if (yylval.word_list)
4521 t = string_list_internal (yylval.word_list, " ; ");
4524 t = (char *)NULL; /* punt */
4532 error_token_from_text ()
4537 t = shell_input_line;
4538 i = shell_input_line_index;
4542 if (i && t[i] == '\0')
4545 while (i && (whitespace (t[i]) || t[i] == '\n'))
4551 while (i && (member (t[i], " \n\t;|&") == 0))
4554 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
4557 /* Return our idea of the offending token. */
4558 if (token_end || (i == 0 && token_end == 0))
4561 msg = substring (t, i, token_end);
4562 else /* one-character token */
4564 msg = (char *)xmalloc (2);
4574 print_offending_line ()
4579 msg = savestring (shell_input_line);
4580 token_end = strlen (msg);
4581 while (token_end && msg[token_end - 1] == '\n')
4582 msg[--token_end] = '\0';
4584 parser_error (line_number, "`%s'", msg);
4588 /* Report a syntax error with line numbers, etc.
4589 Call here for recoverable errors. If you have a message to print,
4590 then place it in MESSAGE, otherwise pass NULL and this will figure
4591 out an appropriate message for you. */
4593 report_syntax_error (message)
4600 parser_error (line_number, "%s", message);
4601 if (interactive && EOF_Reached)
4603 last_command_exit_value = EX_USAGE;
4607 /* If the line of input we're reading is not null, try to find the
4608 objectionable token. First, try to figure out what token the
4609 parser's complaining about by looking at current_token. */
4610 if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
4612 parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
4615 if (interactive == 0)
4616 print_offending_line ();
4618 last_command_exit_value = EX_USAGE;
4622 /* If looking at the current token doesn't prove fruitful, try to find the
4623 offending token by analyzing the text of the input line near the current
4624 input line index and report what we find. */
4625 if (shell_input_line && *shell_input_line)
4627 msg = error_token_from_text ();
4630 parser_error (line_number, _("syntax error near `%s'"), msg);
4634 /* If not interactive, print the line containing the error. */
4635 if (interactive == 0)
4636 print_offending_line ();
4640 msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error");
4641 parser_error (line_number, "%s", msg);
4642 /* When the shell is interactive, this file uses EOF_Reached
4643 only for error reporting. Other mechanisms are used to
4644 decide whether or not to exit. */
4645 if (interactive && EOF_Reached)
4649 last_command_exit_value = EX_USAGE;
4652 /* ??? Needed function. ??? We have to be able to discard the constructs
4653 created during parsing. In the case of error, we want to return
4654 allocated objects to the memory pool. In the case of no error, we want
4655 to throw away the information about where the allocated objects live.
4656 (dispose_command () will actually free the command.) */
4658 discard_parser_constructs (error_p)
4663 /************************************************
4667 ************************************************/
4669 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
4671 /* A flag denoting whether or not ignoreeof is set. */
4674 /* The number of times that we have encountered an EOF character without
4675 another character intervening. When this gets above the limit, the
4676 shell terminates. */
4677 int eof_encountered = 0;
4679 /* The limit for eof_encountered. */
4680 int eof_encountered_limit = 10;
4682 /* If we have EOF as the only input unit, this user wants to leave
4683 the shell. If the shell is not interactive, then just leave.
4684 Otherwise, if ignoreeof is set, and we haven't done this the
4685 required number of times in a row, print a message. */
4687 handle_eof_input_unit ()
4691 /* shell.c may use this to decide whether or not to write out the
4692 history, among other things. We use it only for error reporting
4697 /* If the user wants to "ignore" eof, then let her do so, kind of. */
4700 if (eof_encountered < eof_encountered_limit)
4702 fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
4703 login_shell ? "logout" : "exit");
4705 /* Reset the parsing state. */
4706 last_read_token = current_token = '\n';
4707 /* Reset the prompt string to be $PS1. */
4708 prompt_string_pointer = (char **)NULL;
4714 /* In this case EOF should exit the shell. Do it now. */
4716 exit_builtin ((WORD_LIST *)NULL);
4720 /* We don't write history files, etc., for non-interactive shells. */
4725 /************************************************
4727 * STRING PARSING FUNCTIONS *
4729 ************************************************/
4731 /* It's very important that these two functions treat the characters
4732 between ( and ) identically. */
4734 static WORD_LIST parse_string_error;
4736 /* Take a string and run it through the shell parser, returning the
4737 resultant word list. Used by compound array assignment. */
4739 parse_string_to_word_list (s, flags, whom)
4745 int tok, orig_current_token, orig_line_number, orig_input_terminator;
4746 int orig_line_count;
4747 int old_echo_input, old_expand_aliases;
4748 #if defined (HISTORY)
4749 int old_remember_on_history, old_history_expansion_inhibited;
4752 #if defined (HISTORY)
4753 old_remember_on_history = remember_on_history;
4754 # if defined (BANG_HISTORY)
4755 old_history_expansion_inhibited = history_expansion_inhibited;
4757 bash_history_disable ();
4760 orig_line_number = line_number;
4761 orig_line_count = current_command_line_count;
4762 orig_input_terminator = shell_input_line_terminator;
4763 old_echo_input = echo_input_at_read;
4764 old_expand_aliases = expand_aliases;
4767 last_read_token = WORD; /* WORD to allow reserved words here */
4768 current_command_line_count = 0;
4769 echo_input_at_read = expand_aliases = 0;
4771 with_input_from_string (s, whom);
4772 wl = (WORD_LIST *)NULL;
4775 parser_state |= PST_COMPASSIGN;
4777 while ((tok = read_token (READ)) != yacc_EOF)
4779 if (tok == '\n' && *bash_input.location.string == '\0')
4781 if (tok == '\n') /* Allow newlines in compound assignments */
4783 if (tok != WORD && tok != ASSIGNMENT_WORD)
4785 line_number = orig_line_number + line_number - 1;
4786 orig_current_token = current_token;
4787 current_token = tok;
4788 yyerror (NULL); /* does the right thing */
4789 current_token = orig_current_token;
4792 wl = &parse_string_error;
4795 wl = make_word_list (yylval.word, wl);
4798 last_read_token = '\n';
4801 #if defined (HISTORY)
4802 remember_on_history = old_remember_on_history;
4803 # if defined (BANG_HISTORY)
4804 history_expansion_inhibited = old_history_expansion_inhibited;
4805 # endif /* BANG_HISTORY */
4806 #endif /* HISTORY */
4808 echo_input_at_read = old_echo_input;
4809 expand_aliases = old_expand_aliases;
4811 current_command_line_count = orig_line_count;
4812 shell_input_line_terminator = orig_input_terminator;
4815 parser_state &= ~PST_COMPASSIGN;
4817 if (wl == &parse_string_error)
4819 last_command_exit_value = EXECUTION_FAILURE;
4820 if (interactive_shell == 0 && posixly_correct)
4821 jump_to_top_level (FORCE_EOF);
4823 jump_to_top_level (DISCARD);
4826 return (REVERSE_LIST (wl, WORD_LIST *));
4830 parse_compound_assignment (retlenp)
4834 int tok, orig_line_number, orig_token_size, orig_last_token, assignok;
4835 char *saved_token, *ret;
4837 saved_token = token;
4838 orig_token_size = token_buffer_size;
4839 orig_line_number = line_number;
4840 orig_last_token = last_read_token;
4842 last_read_token = WORD; /* WORD to allow reserved words here */
4844 token = (char *)NULL;
4845 token_buffer_size = 0;
4847 assignok = parser_state&PST_ASSIGNOK; /* XXX */
4849 wl = (WORD_LIST *)NULL; /* ( */
4850 parser_state |= PST_COMPASSIGN;
4852 while ((tok = read_token (READ)) != ')')
4854 if (tok == '\n') /* Allow newlines in compound assignments */
4856 if (SHOULD_PROMPT ())
4860 if (tok != WORD && tok != ASSIGNMENT_WORD)
4862 current_token = tok; /* for error reporting */
4863 if (tok == yacc_EOF) /* ( */
4864 parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'"));
4866 yyerror(NULL); /* does the right thing */
4869 wl = &parse_string_error;
4872 wl = make_word_list (yylval.word, wl);
4876 token = saved_token;
4877 token_buffer_size = orig_token_size;
4879 parser_state &= ~PST_COMPASSIGN;
4881 if (wl == &parse_string_error)
4883 last_command_exit_value = EXECUTION_FAILURE;
4884 last_read_token = '\n'; /* XXX */
4885 if (interactive_shell == 0 && posixly_correct)
4886 jump_to_top_level (FORCE_EOF);
4888 jump_to_top_level (DISCARD);
4891 last_read_token = orig_last_token; /* XXX - was WORD? */
4895 rl = REVERSE_LIST (wl, WORD_LIST *);
4896 ret = string_list (rl);
4903 *retlenp = (ret && *ret) ? strlen (ret) : 0;
4906 parser_state |= PST_ASSIGNOK;
4911 /************************************************
4913 * SAVING AND RESTORING PARTIAL PARSE STATE *
4915 ************************************************/
4918 save_parser_state (ps)
4919 sh_parser_state_t *ps;
4921 #if defined (ARRAY_VARS)
4926 ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t));
4928 return ((sh_parser_state_t *)NULL);
4930 ps->parser_state = parser_state;
4931 ps->token_state = save_token_state ();
4933 ps->input_line_terminator = shell_input_line_terminator;
4934 ps->eof_encountered = eof_encountered;
4936 ps->current_command_line_count = current_command_line_count;
4938 #if defined (HISTORY)
4939 ps->remember_on_history = remember_on_history;
4940 # if defined (BANG_HISTORY)
4941 ps->history_expansion_inhibited = history_expansion_inhibited;
4945 ps->last_command_exit_value = last_command_exit_value;
4946 #if defined (ARRAY_VARS)
4947 v = find_variable ("PIPESTATUS");
4948 if (v && array_p (v) && array_cell (v))
4949 ps->pipestatus = array_copy (array_cell (v));
4951 ps->pipestatus = (ARRAY *)NULL;
4954 ps->last_shell_builtin = last_shell_builtin;
4955 ps->this_shell_builtin = this_shell_builtin;
4957 ps->expand_aliases = expand_aliases;
4958 ps->echo_input_at_read = echo_input_at_read;
4964 restore_parser_state (ps)
4965 sh_parser_state_t *ps;
4967 #if defined (ARRAY_VARS)
4974 parser_state = ps->parser_state;
4975 if (ps->token_state)
4977 restore_token_state (ps->token_state);
4978 free (ps->token_state);
4981 shell_input_line_terminator = ps->input_line_terminator;
4982 eof_encountered = ps->eof_encountered;
4984 current_command_line_count = ps->current_command_line_count;
4986 #if defined (HISTORY)
4987 remember_on_history = ps->remember_on_history;
4988 # if defined (BANG_HISTORY)
4989 history_expansion_inhibited = ps->history_expansion_inhibited;
4993 last_command_exit_value = ps->last_command_exit_value;
4994 #if defined (ARRAY_VARS)
4995 v = find_variable ("PIPESTATUS");
4996 if (v && array_p (v) && array_cell (v))
4998 array_dispose (array_cell (v));
4999 var_setarray (v, ps->pipestatus);
5003 last_shell_builtin = ps->last_shell_builtin;
5004 this_shell_builtin = ps->this_shell_builtin;
5006 expand_aliases = ps->expand_aliases;
5007 echo_input_at_read = ps->echo_input_at_read;
5010 /************************************************
5012 * MULTIBYTE CHARACTER HANDLING *
5014 ************************************************/
5016 #if defined (HANDLE_MULTIBYTE)
5020 int i, previ, len, c;
5021 mbstate_t mbs, prevs;
5024 if (shell_input_line == NULL)
5026 len = strlen (shell_input_line); /* XXX - shell_input_line_len ? */
5027 FREE (shell_input_line_property);
5028 shell_input_line_property = (char *)xmalloc (len + 1);
5030 memset (&prevs, '\0', sizeof (mbstate_t));
5031 for (i = previ = 0; i < len; i++)
5035 c = shell_input_line[i];
5039 for (j = i; j < len; j++)
5040 shell_input_line_property[j] = 1;
5044 mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs);
5045 if (mbclen == 1 || mbclen == (size_t)-1)
5050 else if (mbclen == (size_t)-2)
5052 else if (mbclen > 1)
5060 /* XXX - what to do if mbrlen returns 0? (null wide character) */
5062 for (j = i; j < len; j++)
5063 shell_input_line_property[j] = 1;
5067 shell_input_line_property[i] = mbclen;
5070 #endif /* HANDLE_MULTIBYTE */