1 /* Yacc grammar for bash. */
3 /* Copyright (C) 1989-2006 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 (HISTORY)
209 char *history_delimiting_chars __P((void));
212 #if defined (HANDLE_MULTIBYTE)
213 static void set_line_mbstate __P((void));
214 static char *shell_input_line_property = NULL;
216 # define set_line_mbstate()
219 extern int yyerror __P((const char *));
225 /* Default prompt strings */
226 char *primary_prompt = PPROMPT;
227 char *secondary_prompt = SPROMPT;
229 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
230 char *ps1_prompt, *ps2_prompt;
232 /* Handle on the current prompt string. Indirectly points through
233 ps1_ or ps2_prompt. */
234 char **prompt_string_pointer = (char **)NULL;
235 char *current_prompt_string;
237 /* Non-zero means we expand aliases in commands. */
238 int expand_aliases = 0;
240 /* If non-zero, the decoded prompt string undergoes parameter and
241 variable substitution, command substitution, arithmetic substitution,
242 string expansion, process substitution, and quote removal in
243 decode_prompt_string. */
246 /* If non-zero, $'...' and $"..." are expanded when they appear within
247 a ${...} expansion, even when the expansion appears within double
249 int extended_quote = 1;
251 /* The decoded prompt string. Used if READLINE is not defined or if
252 editing is turned off. Analogous to current_readline_prompt. */
253 static char *current_decoded_prompt;
255 /* The number of lines read from input while creating the current command. */
256 int current_command_line_count;
258 /* Variables to manage the task of reading here documents, because we need to
259 defer the reading until after a complete command has been collected. */
260 static REDIRECT *redir_stack[10];
263 /* Where shell input comes from. History expansion is performed on each
264 line when the shell is interactive. */
265 static char *shell_input_line = (char *)NULL;
266 static int shell_input_line_index;
267 static int shell_input_line_size; /* Amount allocated for shell_input_line. */
268 static int shell_input_line_len; /* strlen (shell_input_line) */
270 /* Either zero or EOF. */
271 static int shell_input_line_terminator;
273 /* The line number in a script on which a function definition starts. */
274 static int function_dstart;
276 /* The line number in a script on which a function body starts. */
277 static int function_bstart;
279 /* The line number in a script at which an arithmetic for command starts. */
280 static int arith_for_lineno;
282 /* The line number in a script where the word in a `case WORD', `select WORD'
283 or `for WORD' begins. This is a nested command maximum, since the array
284 index is decremented after a case, select, or for command is parsed. */
285 #define MAX_CASE_NEST 128
286 static int word_lineno[MAX_CASE_NEST];
287 static int word_top = -1;
289 /* If non-zero, it is the token that we want read_token to return
290 regardless of what text is (or isn't) present to be read. This
291 is reset by read_token. If token_to_read == WORD or
292 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
293 static int token_to_read;
294 static WORD_DESC *word_desc_to_read;
296 static REDIRECTEE redir;
300 WORD_DESC *word; /* the word that we read. */
301 int number; /* the number that we read. */
302 WORD_LIST *word_list;
306 PATTERN_LIST *pattern;
309 /* Reserved words. Members of the first group are only recognized
310 in the case that they are preceded by a list_terminator. Members
311 of the second group are for [[...]] commands. Members of the
312 third group are recognized only under special circumstances. */
313 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION
314 %token COND_START COND_END COND_ERROR
315 %token IN BANG TIME TIMEOPT
317 /* More general tokens. yylex () knows how to make these. */
318 %token <word> WORD ASSIGNMENT_WORD
319 %token <number> NUMBER
320 %token <word_list> ARITH_CMD ARITH_FOR_EXPRS
321 %token <command> COND_CMD
322 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS
323 %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
326 /* The types that the various syntactical units return. */
328 %type <command> inputunit command pipeline pipeline_command
329 %type <command> list list0 list1 compound_list simple_list simple_list1
330 %type <command> simple_command shell_command
331 %type <command> for_command select_command case_command group_command
332 %type <command> arith_command
333 %type <command> cond_command
334 %type <command> arith_for_command
335 %type <command> function_def function_body if_command elif_clause subshell
336 %type <redirect> redirection redirection_list
337 %type <element> simple_command_element
338 %type <word_list> word_list pattern
339 %type <pattern> pattern_list case_clause_sequence case_clause
340 %type <number> timespec
341 %type <number> list_terminator
345 %left '&' ';' '\n' yacc_EOF
350 inputunit: simple_list simple_list_terminator
352 /* Case of regular command. Discard the error
353 safety net,and return the command just parsed. */
356 /* discard_parser_constructs (0); */
361 /* Case of regular command, but not a very
362 interesting one. Return a NULL command. */
363 global_command = (COMMAND *)NULL;
368 /* Error during parsing. Return NULL command. */
369 global_command = (COMMAND *)NULL;
371 /* discard_parser_constructs (1); */
383 /* Case of EOF seen by itself. Do ignoreeof or
385 global_command = (COMMAND *)NULL;
386 handle_eof_input_unit ();
392 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
394 { $$ = make_word_list ($2, $1); }
397 redirection: '>' WORD
400 $$ = make_redirection (1, r_output_direction, redir);
405 $$ = make_redirection (0, r_input_direction, redir);
410 $$ = make_redirection ($1, r_output_direction, redir);
415 $$ = make_redirection ($1, r_input_direction, redir);
417 | GREATER_GREATER WORD
420 $$ = make_redirection (1, r_appending_to, redir);
422 | NUMBER GREATER_GREATER WORD
425 $$ = make_redirection ($1, r_appending_to, redir);
430 $$ = make_redirection (0, r_reading_until, redir);
431 redir_stack[need_here_doc++] = $$;
433 | NUMBER LESS_LESS WORD
436 $$ = make_redirection ($1, r_reading_until, redir);
437 redir_stack[need_here_doc++] = $$;
439 | LESS_LESS_LESS WORD
442 $$ = make_redirection (0, r_reading_string, redir);
444 | NUMBER LESS_LESS_LESS WORD
447 $$ = make_redirection ($1, r_reading_string, redir);
452 $$ = make_redirection (0, r_duplicating_input, redir);
454 | NUMBER LESS_AND NUMBER
457 $$ = make_redirection ($1, r_duplicating_input, redir);
462 $$ = make_redirection (1, r_duplicating_output, redir);
464 | NUMBER GREATER_AND NUMBER
467 $$ = make_redirection ($1, r_duplicating_output, redir);
472 $$ = make_redirection (0, r_duplicating_input_word, redir);
474 | NUMBER LESS_AND WORD
477 $$ = make_redirection ($1, r_duplicating_input_word, redir);
482 $$ = make_redirection (1, r_duplicating_output_word, redir);
484 | NUMBER GREATER_AND WORD
487 $$ = make_redirection ($1, r_duplicating_output_word, redir);
489 | LESS_LESS_MINUS WORD
492 $$ = make_redirection
493 (0, r_deblank_reading_until, redir);
494 redir_stack[need_here_doc++] = $$;
496 | NUMBER LESS_LESS_MINUS WORD
499 $$ = make_redirection
500 ($1, r_deblank_reading_until, redir);
501 redir_stack[need_here_doc++] = $$;
506 $$ = make_redirection (1, r_close_this, redir);
508 | NUMBER GREATER_AND '-'
511 $$ = make_redirection ($1, r_close_this, redir);
516 $$ = make_redirection (0, r_close_this, redir);
518 | NUMBER LESS_AND '-'
521 $$ = make_redirection ($1, r_close_this, redir);
526 $$ = make_redirection (1, r_err_and_out, redir);
528 | NUMBER LESS_GREATER WORD
531 $$ = make_redirection ($1, r_input_output, redir);
536 $$ = make_redirection (0, r_input_output, redir);
541 $$ = make_redirection (1, r_output_force, redir);
543 | NUMBER GREATER_BAR WORD
546 $$ = make_redirection ($1, r_output_force, redir);
550 simple_command_element: WORD
551 { $$.word = $1; $$.redirect = 0; }
553 { $$.word = $1; $$.redirect = 0; }
555 { $$.redirect = $1; $$.word = 0; }
558 redirection_list: redirection
562 | redirection_list redirection
564 register REDIRECT *t;
566 for (t = $1; t->next; t = t->next)
573 simple_command: simple_command_element
574 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
575 | simple_command simple_command_element
576 { $$ = make_simple_command ($2, $1); }
579 command: simple_command
580 { $$ = clean_simple_command ($1); }
583 | shell_command redirection_list
590 register REDIRECT *t;
591 for (t = tc->redirects; t->next; t = t->next)
603 shell_command: for_command
607 | WHILE compound_list DO compound_list DONE
608 { $$ = make_while_command ($2, $4); }
609 | UNTIL compound_list DO compound_list DONE
610 { $$ = make_until_command ($2, $4); }
627 for_command: FOR WORD newline_list DO compound_list DONE
629 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
630 if (word_top > 0) word_top--;
632 | FOR WORD newline_list '{' compound_list '}'
634 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
635 if (word_top > 0) word_top--;
637 | FOR WORD ';' newline_list DO compound_list DONE
639 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
640 if (word_top > 0) word_top--;
642 | FOR WORD ';' newline_list '{' compound_list '}'
644 $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
645 if (word_top > 0) word_top--;
647 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
649 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
650 if (word_top > 0) word_top--;
652 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
654 $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
655 if (word_top > 0) word_top--;
657 | FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
659 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
660 if (word_top > 0) word_top--;
662 | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
664 $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]);
665 if (word_top > 0) word_top--;
669 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
671 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
672 if (word_top > 0) word_top--;
674 | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
676 $$ = make_arith_for_command ($2, $6, arith_for_lineno);
677 if (word_top > 0) word_top--;
679 | FOR ARITH_FOR_EXPRS DO compound_list DONE
681 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
682 if (word_top > 0) word_top--;
684 | FOR ARITH_FOR_EXPRS '{' compound_list '}'
686 $$ = make_arith_for_command ($2, $4, arith_for_lineno);
687 if (word_top > 0) word_top--;
691 select_command: SELECT WORD newline_list DO list DONE
693 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
694 if (word_top > 0) word_top--;
696 | SELECT WORD newline_list '{' list '}'
698 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
699 if (word_top > 0) word_top--;
701 | SELECT WORD ';' newline_list DO list DONE
703 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
704 if (word_top > 0) word_top--;
706 | SELECT WORD ';' newline_list '{' list '}'
708 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
709 if (word_top > 0) word_top--;
711 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
713 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
714 if (word_top > 0) word_top--;
716 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
718 $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]);
719 if (word_top > 0) word_top--;
723 case_command: CASE WORD newline_list IN newline_list ESAC
725 $$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]);
726 if (word_top > 0) word_top--;
728 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
730 $$ = make_case_command ($2, $5, word_lineno[word_top]);
731 if (word_top > 0) word_top--;
733 | CASE WORD newline_list IN case_clause ESAC
735 $$ = make_case_command ($2, $5, word_lineno[word_top]);
736 if (word_top > 0) word_top--;
740 function_def: WORD '(' ')' newline_list function_body
741 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
743 | FUNCTION WORD '(' ')' newline_list function_body
744 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
746 | FUNCTION WORD newline_list function_body
747 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
751 function_body: shell_command
753 | shell_command redirection_list
758 /* According to Posix.2 3.9.5, redirections
759 specified after the body of a function should
760 be attached to the function and performed when
761 the function is executed, not as part of the
762 function definition command. */
763 /* XXX - I don't think it matters, but we might
764 want to change this in the future to avoid
765 problems differentiating between a function
766 definition with a redirection and a function
767 definition containing a single command with a
768 redirection. The two are semantically equivalent,
769 though -- the only difference is in how the
770 command printing code displays the redirections. */
773 register REDIRECT *t;
774 for (t = tc->redirects; t->next; t = t->next)
784 subshell: '(' compound_list ')'
786 $$ = make_subshell_command ($2);
787 $$->flags |= CMD_WANT_SUBSHELL;
791 if_command: IF compound_list THEN compound_list FI
792 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
793 | IF compound_list THEN compound_list ELSE compound_list FI
794 { $$ = make_if_command ($2, $4, $6); }
795 | IF compound_list THEN compound_list elif_clause FI
796 { $$ = make_if_command ($2, $4, $5); }
800 group_command: '{' compound_list '}'
801 { $$ = make_group_command ($2); }
804 arith_command: ARITH_CMD
805 { $$ = make_arith_command ($1); }
808 cond_command: COND_START COND_CMD COND_END
812 elif_clause: ELIF compound_list THEN compound_list
813 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
814 | ELIF compound_list THEN compound_list ELSE compound_list
815 { $$ = make_if_command ($2, $4, $6); }
816 | ELIF compound_list THEN compound_list elif_clause
817 { $$ = make_if_command ($2, $4, $5); }
820 case_clause: pattern_list
821 | case_clause_sequence pattern_list
822 { $2->next = $1; $$ = $2; }
825 pattern_list: newline_list pattern ')' compound_list
826 { $$ = make_pattern_list ($2, $4); }
827 | newline_list pattern ')' newline_list
828 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
829 | newline_list '(' pattern ')' compound_list
830 { $$ = make_pattern_list ($3, $5); }
831 | newline_list '(' pattern ')' newline_list
832 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
835 case_clause_sequence: pattern_list SEMI_SEMI
836 | case_clause_sequence pattern_list SEMI_SEMI
837 { $2->next = $1; $$ = $2; }
841 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
843 { $$ = make_word_list ($3, $1); }
846 /* A list allows leading or trailing newlines and
847 newlines as operators (equivalent to semicolons).
848 It must end with a newline or semicolon.
849 Lists are used within commands such as if, for, while. */
851 list: newline_list list0
855 gather_here_documents ();
866 list0: list1 '\n' newline_list
867 | list1 '&' newline_list
869 if ($1->type == cm_connection)
870 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
872 $$ = command_connect ($1, (COMMAND *)NULL, '&');
874 | list1 ';' newline_list
878 list1: list1 AND_AND newline_list list1
879 { $$ = command_connect ($1, $4, AND_AND); }
880 | list1 OR_OR newline_list list1
881 { $$ = command_connect ($1, $4, OR_OR); }
882 | list1 '&' newline_list list1
884 if ($1->type == cm_connection)
885 $$ = connect_async_list ($1, $4, '&');
887 $$ = command_connect ($1, $4, '&');
889 | list1 ';' newline_list list1
890 { $$ = command_connect ($1, $4, ';'); }
891 | list1 '\n' newline_list list1
892 { $$ = command_connect ($1, $4, ';'); }
897 simple_list_terminator: '\n'
913 /* A simple_list is a list that contains no significant newlines
914 and no leading or trailing newlines. Newlines are allowed
915 only following operators, where they are not significant.
917 This is what an inputunit consists of. */
919 simple_list: simple_list1
923 gather_here_documents ();
927 if ($1->type == cm_connection)
928 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
930 $$ = command_connect ($1, (COMMAND *)NULL, '&');
932 gather_here_documents ();
938 gather_here_documents ();
942 simple_list1: simple_list1 AND_AND newline_list simple_list1
943 { $$ = command_connect ($1, $4, AND_AND); }
944 | simple_list1 OR_OR newline_list simple_list1
945 { $$ = command_connect ($1, $4, OR_OR); }
946 | simple_list1 '&' simple_list1
948 if ($1->type == cm_connection)
949 $$ = connect_async_list ($1, $3, '&');
951 $$ = command_connect ($1, $3, '&');
953 | simple_list1 ';' simple_list1
954 { $$ = command_connect ($1, $3, ';'); }
960 pipeline_command: pipeline
965 $2->flags |= CMD_INVERT_RETURN;
974 | timespec BANG pipeline
977 $3->flags |= $1|CMD_INVERT_RETURN;
980 | BANG timespec pipeline
983 $3->flags |= $2|CMD_INVERT_RETURN;
986 | timespec list_terminator
990 /* Boy, this is unclean. `time' by itself can
991 time a null command. We cheat and push a
992 newline back if the list_terminator was a newline
993 to avoid the double-newline problem (one to
994 terminate this, one to terminate the command) */
997 $$ = make_simple_command (x, (COMMAND *)NULL);
999 /* XXX - let's cheat and push a newline back */
1001 token_to_read = '\n';
1007 pipeline '|' newline_list pipeline
1008 { $$ = command_connect ($1, $4, '|'); }
1014 { $$ = CMD_TIME_PIPELINE; }
1016 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
1020 /* Possible states for the parser that require it to do special things. */
1021 #define PST_CASEPAT 0x0001 /* in a case pattern list */
1022 #define PST_ALEXPNEXT 0x0002 /* expand next word for aliases */
1023 #define PST_ALLOWOPNBRC 0x0004 /* allow open brace for function def */
1024 #define PST_NEEDCLOSBRC 0x0008 /* need close brace */
1025 #define PST_DBLPAREN 0x0010 /* double-paren parsing */
1026 #define PST_SUBSHELL 0x0020 /* ( ... ) subshell */
1027 #define PST_CMDSUBST 0x0040 /* $( ... ) command substitution */
1028 #define PST_CASESTMT 0x0080 /* parsing a case statement */
1029 #define PST_CONDCMD 0x0100 /* parsing a [[...]] command */
1030 #define PST_CONDEXPR 0x0200 /* parsing the guts of [[...]] */
1031 #define PST_ARITHFOR 0x0400 /* parsing an arithmetic for command */
1032 #define PST_ALEXPAND 0x0800 /* OK to expand aliases - unused */
1033 #define PST_CMDTOKEN 0x1000 /* command token OK - unused */
1034 #define PST_COMPASSIGN 0x2000 /* parsing x=(...) compound assignment */
1035 #define PST_ASSIGNOK 0x4000 /* assignment statement ok in this context */
1037 /* Initial size to allocate for tokens, and the
1038 amount to grow them by. */
1039 #define TOKEN_DEFAULT_INITIAL_SIZE 496
1040 #define TOKEN_DEFAULT_GROW_SIZE 512
1042 /* Should we call prompt_again? */
1043 #define SHOULD_PROMPT() \
1044 (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
1047 # define expanding_alias() (pushed_string_list && pushed_string_list->expander)
1049 # define expanding_alias() 0
1052 /* The token currently being read. */
1053 static int current_token;
1055 /* The last read token, or NULL. read_token () uses this for context
1057 static int last_read_token;
1059 /* The token read prior to last_read_token. */
1060 static int token_before_that;
1062 /* The token read prior to token_before_that. */
1063 static int two_tokens_ago;
1065 /* The current parser state. */
1066 static int parser_state;
1068 /* Global var is non-zero when end of file has been reached. */
1069 int EOF_Reached = 0;
1082 /* yy_getc () returns the next available character from input or EOF.
1083 yy_ungetc (c) makes `c' the next character to read.
1084 init_yy_io (get, unget, type, location) makes the function GET the
1085 installed function for getting the next character, makes UNGET the
1086 installed function for un-getting a character, sets the type of stream
1087 (either string or file) from TYPE, and makes LOCATION point to where
1088 the input is coming from. */
1090 /* Unconditionally returns end-of-file. */
1097 /* Variable containing the current get and unget functions.
1098 See ./input.h for a clearer description. */
1099 BASH_INPUT bash_input;
1101 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
1102 is non-null, avoiding a memory leak. */
1104 initialize_bash_input ()
1106 bash_input.type = st_none;
1107 FREE (bash_input.name);
1108 bash_input.name = (char *)NULL;
1109 bash_input.location.file = (FILE *)NULL;
1110 bash_input.location.string = (char *)NULL;
1111 bash_input.getter = (sh_cget_func_t *)NULL;
1112 bash_input.ungetter = (sh_cunget_func_t *)NULL;
1115 /* Set the contents of the current bash input stream from
1116 GET, UNGET, TYPE, NAME, and LOCATION. */
1118 init_yy_io (get, unget, type, name, location)
1119 sh_cget_func_t *get;
1120 sh_cunget_func_t *unget;
1121 enum stream_type type;
1123 INPUT_STREAM location;
1125 bash_input.type = type;
1126 FREE (bash_input.name);
1127 bash_input.name = name ? savestring (name) : (char *)NULL;
1131 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
1133 bash_input.location = location;
1135 bash_input.getter = get;
1136 bash_input.ungetter = unget;
1142 return (bash_input.name ? bash_input.name : "stdin");
1145 /* Call this to get the next character of input. */
1149 return (*(bash_input.getter)) ();
1152 /* Call this to unget C. That is, to make C the next character
1158 return (*(bash_input.ungetter)) (c);
1161 #if defined (BUFFERED_INPUT)
1162 #ifdef INCLUDE_UNUSED
1164 input_file_descriptor ()
1166 switch (bash_input.type)
1169 return (fileno (bash_input.location.file));
1171 return (bash_input.location.buffered_fd);
1174 return (fileno (stdin));
1178 #endif /* BUFFERED_INPUT */
1180 /* **************************************************************** */
1182 /* Let input be read from readline (). */
1184 /* **************************************************************** */
1186 #if defined (READLINE)
1187 char *current_readline_prompt = (char *)NULL;
1188 char *current_readline_line = (char *)NULL;
1189 int current_readline_line_index = 0;
1194 SigHandler *old_sigint;
1198 if (!current_readline_line)
1200 if (!bash_readline_initialized)
1201 initialize_readline ();
1203 #if defined (JOB_CONTROL)
1205 give_terminal_to (shell_pgrp, 0);
1206 #endif /* JOB_CONTROL */
1208 old_sigint = (SigHandler *)NULL;
1209 if (signal_is_ignored (SIGINT) == 0)
1211 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
1212 interrupt_immediately++;
1214 terminate_immediately = 1;
1216 current_readline_line = readline (current_readline_prompt ?
1217 current_readline_prompt : "");
1219 terminate_immediately = 0;
1220 if (signal_is_ignored (SIGINT) == 0 && old_sigint)
1222 interrupt_immediately--;
1223 set_signal_handler (SIGINT, old_sigint);
1227 /* Reset the prompt to the decoded value of prompt_string_pointer. */
1228 reset_readline_prompt ();
1231 if (current_readline_line == 0)
1234 current_readline_line_index = 0;
1235 line_len = strlen (current_readline_line);
1237 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
1238 current_readline_line[line_len++] = '\n';
1239 current_readline_line[line_len] = '\0';
1242 if (current_readline_line[current_readline_line_index] == 0)
1244 free (current_readline_line);
1245 current_readline_line = (char *)NULL;
1246 return (yy_readline_get ());
1250 c = current_readline_line[current_readline_line_index++];
1256 yy_readline_unget (c)
1259 if (current_readline_line_index && current_readline_line)
1260 current_readline_line[--current_readline_line_index] = c;
1265 with_input_from_stdin ()
1267 INPUT_STREAM location;
1269 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1271 location.string = current_readline_line;
1272 init_yy_io (yy_readline_get, yy_readline_unget,
1273 st_stdin, "readline stdin", location);
1277 #else /* !READLINE */
1280 with_input_from_stdin ()
1282 with_input_from_stream (stdin, "stdin");
1284 #endif /* !READLINE */
1286 /* **************************************************************** */
1288 /* Let input come from STRING. STRING is zero terminated. */
1290 /* **************************************************************** */
1295 register char *string;
1296 register unsigned char c;
1298 string = bash_input.location.string;
1300 /* If the string doesn't exist, or is empty, EOF found. */
1301 if (string && *string)
1304 bash_input.location.string = string;
1315 *(--bash_input.location.string) = c;
1320 with_input_from_string (string, name)
1324 INPUT_STREAM location;
1326 location.string = string;
1327 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1330 /* **************************************************************** */
1332 /* Let input come from STREAM. */
1334 /* **************************************************************** */
1336 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1337 define, and just use getc/ungetc if it was defined, but since bash
1338 installs its signal handlers without the SA_RESTART flag, some signals
1339 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
1340 the read to be restarted. We need to restart it ourselves. */
1348 if (bash_input.location.file)
1352 interrupt_immediately++;
1353 terminate_immediately++;
1355 result = getc_with_restart (bash_input.location.file);
1358 interrupt_immediately--;
1359 terminate_immediately--;
1369 return (ungetc_with_restart (c, bash_input.location.file));
1373 with_input_from_stream (stream, name)
1377 INPUT_STREAM location;
1379 location.file = stream;
1380 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1383 typedef struct stream_saver {
1384 struct stream_saver *next;
1385 BASH_INPUT bash_input;
1387 #if defined (BUFFERED_INPUT)
1388 BUFFERED_STREAM *bstream;
1389 #endif /* BUFFERED_INPUT */
1392 /* The globally known line number. */
1393 int line_number = 0;
1395 #if defined (COND_COMMAND)
1396 static int cond_lineno;
1397 static int cond_token;
1400 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1403 push_stream (reset_lineno)
1406 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1408 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1410 #if defined (BUFFERED_INPUT)
1411 saver->bstream = (BUFFERED_STREAM *)NULL;
1412 /* If we have a buffered stream, clear out buffers[fd]. */
1413 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1414 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1415 (BUFFERED_STREAM *)NULL);
1416 #endif /* BUFFERED_INPUT */
1418 saver->line = line_number;
1419 bash_input.name = (char *)NULL;
1420 saver->next = stream_list;
1421 stream_list = saver;
1434 STREAM_SAVER *saver = stream_list;
1437 stream_list = stream_list->next;
1439 init_yy_io (saver->bash_input.getter,
1440 saver->bash_input.ungetter,
1441 saver->bash_input.type,
1442 saver->bash_input.name,
1443 saver->bash_input.location);
1445 #if defined (BUFFERED_INPUT)
1446 /* If we have a buffered stream, restore buffers[fd]. */
1447 /* If the input file descriptor was changed while this was on the
1448 save stack, update the buffered fd to the new file descriptor and
1449 re-establish the buffer <-> bash_input fd correspondence. */
1450 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1452 if (bash_input_fd_changed)
1454 bash_input_fd_changed = 0;
1455 if (default_buffered_input >= 0)
1457 bash_input.location.buffered_fd = default_buffered_input;
1458 saver->bstream->b_fd = default_buffered_input;
1459 SET_CLOSE_ON_EXEC (default_buffered_input);
1462 /* XXX could free buffered stream returned as result here. */
1463 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1465 #endif /* BUFFERED_INPUT */
1467 line_number = saver->line;
1469 FREE (saver->bash_input.name);
1474 /* Return 1 if a stream of type TYPE is saved on the stack. */
1476 stream_on_stack (type)
1477 enum stream_type type;
1479 register STREAM_SAVER *s;
1481 for (s = stream_list; s; s = s->next)
1482 if (s->bash_input.type == type)
1487 /* Save the current token state and return it in a malloced array. */
1493 ret = (int *)xmalloc (3 * sizeof (int));
1494 ret[0] = last_read_token;
1495 ret[1] = token_before_that;
1496 ret[2] = two_tokens_ago;
1501 restore_token_state (ts)
1506 last_read_token = ts[0];
1507 token_before_that = ts[1];
1508 two_tokens_ago = ts[2];
1512 * This is used to inhibit alias expansion and reserved word recognition
1513 * inside case statement pattern lists. A `case statement pattern list' is:
1515 * everything between the `in' in a `case word in' and the next ')'
1517 * everything between a `;;' and the next `)' or `esac'
1520 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1522 #define END_OF_ALIAS 0
1525 * Pseudo-global variables used in implementing token-wise alias expansion.
1529 * Pushing and popping strings. This works together with shell_getc to
1530 * implement alias expansion on a per-token basis.
1533 typedef struct string_saver {
1534 struct string_saver *next;
1535 int expand_alias; /* Value to set expand_alias to when string is popped. */
1538 alias_t *expander; /* alias that caused this line to be pushed. */
1540 int saved_line_size, saved_line_index, saved_line_terminator;
1543 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1546 * Push the current shell_input_line onto a stack of such lines and make S
1547 * the current input. Used when expanding aliases. EXPAND is used to set
1548 * the value of expand_next_token when the string is popped, so that the
1549 * word after the alias in the original line is handled correctly when the
1550 * alias expands to multiple words. TOKEN is the token that was expanded
1551 * into S; it is saved and used to prevent infinite recursive expansion.
1554 push_string (s, expand, ap)
1559 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
1561 temp->expand_alias = expand;
1562 temp->saved_line = shell_input_line;
1563 temp->saved_line_size = shell_input_line_size;
1564 temp->saved_line_index = shell_input_line_index;
1565 temp->saved_line_terminator = shell_input_line_terminator;
1567 temp->expander = ap;
1569 temp->next = pushed_string_list;
1570 pushed_string_list = temp;
1574 ap->flags |= AL_BEINGEXPANDED;
1577 shell_input_line = s;
1578 shell_input_line_size = strlen (s);
1579 shell_input_line_index = 0;
1580 shell_input_line_terminator = '\0';
1582 parser_state &= ~PST_ALEXPNEXT; /* XXX */
1585 set_line_mbstate ();
1589 * Make the top of the pushed_string stack be the current shell input.
1590 * Only called when there is something on the stack. Called from shell_getc
1591 * when it thinks it has consumed the string generated by an alias expansion
1592 * and needs to return to the original input line.
1599 FREE (shell_input_line);
1600 shell_input_line = pushed_string_list->saved_line;
1601 shell_input_line_index = pushed_string_list->saved_line_index;
1602 shell_input_line_size = pushed_string_list->saved_line_size;
1603 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1605 if (pushed_string_list->expand_alias)
1606 parser_state |= PST_ALEXPNEXT;
1608 parser_state &= ~PST_ALEXPNEXT;
1610 t = pushed_string_list;
1611 pushed_string_list = pushed_string_list->next;
1615 t->expander->flags &= ~AL_BEINGEXPANDED;
1620 set_line_mbstate ();
1626 register STRING_SAVER *t, *t1;
1628 for (t = pushed_string_list; t; )
1631 FREE (t->saved_line);
1634 t->expander->flags &= ~AL_BEINGEXPANDED;
1639 pushed_string_list = (STRING_SAVER *)NULL;
1642 #endif /* ALIAS || DPAREN_ARITHMETIC */
1645 free_pushed_string_input ()
1647 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1648 free_string_list ();
1652 /* Return a line of text, taken from wherever yylex () reads input.
1653 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1654 is non-zero, we remove unquoted \<newline> pairs. This is used by
1655 read_secondary_line to read here documents. */
1657 read_a_line (remove_quoted_newline)
1658 int remove_quoted_newline;
1660 static char *line_buffer = (char *)NULL;
1661 static int buffer_size = 0;
1662 int indx = 0, c, peekc, pass_next;
1664 #if defined (READLINE)
1665 if (no_line_editing && SHOULD_PROMPT ())
1667 if (SHOULD_PROMPT ())
1674 /* Allow immediate exit if interrupted during input. */
1679 /* Ignore null bytes in input. */
1683 internal_warning ("read_a_line: ignored null byte in input");
1688 /* If there is no more input, then we return NULL. */
1691 if (interactive && bash_input.type == st_stream)
1694 return ((char *)NULL);
1698 /* `+2' in case the final character in the buffer is a newline. */
1699 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
1701 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
1702 here document with an unquoted delimiter. In this case,
1703 the line will be expanded as if it were in double quotes.
1704 We allow a backslash to escape the next character, but we
1705 need to treat the backslash specially only if a backslash
1706 quoting a backslash-newline pair appears in the line. */
1709 line_buffer[indx++] = c;
1712 else if (c == '\\' && remove_quoted_newline)
1718 continue; /* Make the unquoted \<newline> pair disappear. */
1724 line_buffer[indx++] = c; /* Preserve the backslash. */
1728 line_buffer[indx++] = c;
1732 line_buffer[indx] = '\0';
1733 return (line_buffer);
1738 /* Return a line as in read_a_line (), but insure that the prompt is
1739 the secondary prompt. This is used to read the lines of a here
1740 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
1741 newlines quoted with backslashes while reading the line. It is
1742 non-zero unless the delimiter of the here document was quoted. */
1744 read_secondary_line (remove_quoted_newline)
1745 int remove_quoted_newline;
1747 prompt_string_pointer = &ps2_prompt;
1748 if (SHOULD_PROMPT())
1750 return (read_a_line (remove_quoted_newline));
1753 /* **************************************************************** */
1757 /* **************************************************************** */
1759 /* Reserved words. These are only recognized as the first word of a
1761 STRING_INT_ALIST word_token_alist[] = {
1770 #if defined (SELECT_COMMAND)
1771 { "select", SELECT },
1778 { "function", FUNCTION },
1779 #if defined (COMMAND_TIMING)
1785 #if defined (COND_COMMAND)
1786 { "[[", COND_START },
1792 /* other tokens that can be returned by read_token() */
1793 STRING_INT_ALIST other_token_alist[] = {
1794 /* Multiple-character tokens with special values */
1798 { ">>", GREATER_GREATER },
1799 { "<<", LESS_LESS },
1801 { ">&", GREATER_AND },
1802 { ";;", SEMI_SEMI },
1803 { "<<-", LESS_LESS_MINUS },
1804 { "<<<", LESS_LESS_LESS },
1805 { "&>", AND_GREATER },
1806 { "<>", LESS_GREATER },
1807 { ">|", GREATER_BAR },
1808 { "EOF", yacc_EOF },
1809 /* Tokens whose value is the character itself */
1820 { "newline", '\n' },
1824 /* others not listed here:
1825 WORD look at yylval.word
1826 ASSIGNMENT_WORD look at yylval.word
1827 NUMBER look at yylval.number
1828 ARITH_CMD look at yylval.word_list
1829 ARITH_FOR_EXPRS look at yylval.word_list
1830 COND_CMD look at yylval.command
1833 /* These are used by read_token_word, but appear up here so that shell_getc
1834 can use them to decide when to add otherwise blank lines to the history. */
1836 /* The primary delimiter stack. */
1837 struct dstack dstack = { (char *)NULL, 0, 0 };
1839 /* A temporary delimiter stack to be used when decoding prompt strings.
1840 This is needed because command substitutions in prompt strings (e.g., PS2)
1841 can screw up the parser's quoting state. */
1842 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
1844 /* Macro for accessing the top delimiter on the stack. Returns the
1845 delimiter or zero if none. */
1846 #define current_delimiter(ds) \
1847 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
1849 #define push_delimiter(ds, character) \
1852 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
1853 ds.delimiters = (char *)xrealloc \
1854 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
1855 ds.delimiters[ds.delimiter_depth] = character; \
1856 ds.delimiter_depth++; \
1860 #define pop_delimiter(ds) ds.delimiter_depth--
1862 /* Return the next shell input character. This always reads characters
1863 from shell_input_line; when that line is exhausted, it is time to
1864 read the next line. This is called by read_token when the shell is
1865 processing normal command input. */
1867 /* This implements one-character lookahead/lookbehind across physical input
1868 lines, to avoid something being lost because it's pushed back with
1869 shell_ungetc when we're at the start of a line. */
1870 static int eol_ungetc_lookahead = 0;
1873 shell_getc (remove_quoted_newline)
1874 int remove_quoted_newline;
1879 static int mustpop = 0;
1883 if (sigwinch_received)
1885 sigwinch_received = 0;
1886 get_new_window_size (0, (int *)0, (int *)0);
1889 if (eol_ungetc_lookahead)
1891 c = eol_ungetc_lookahead;
1892 eol_ungetc_lookahead = 0;
1896 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1897 /* If shell_input_line[shell_input_line_index] == 0, but there is
1898 something on the pushed list of strings, then we don't want to go
1899 off and get another line. We let the code down below handle it. */
1901 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
1902 (pushed_string_list == (STRING_SAVER *)NULL)))
1903 #else /* !ALIAS && !DPAREN_ARITHMETIC */
1904 if (!shell_input_line || !shell_input_line[shell_input_line_index])
1905 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
1911 /* Allow immediate exit if interrupted during input. */
1915 shell_input_line_terminator = 0;
1917 /* If the shell is interatctive, but not currently printing a prompt
1918 (interactive_shell && interactive == 0), we don't want to print
1919 notifies or cleanup the jobs -- we want to defer it until we do
1920 print the next prompt. */
1921 if (interactive_shell == 0 || SHOULD_PROMPT())
1923 #if defined (JOB_CONTROL)
1924 /* This can cause a problem when reading a command as the result
1925 of a trap, when the trap is called from flush_child. This call
1926 had better not cause jobs to disappear from the job table in
1927 that case, or we will have big trouble. */
1928 notify_and_cleanup ();
1929 #else /* !JOB_CONTROL */
1930 cleanup_dead_jobs ();
1931 #endif /* !JOB_CONTROL */
1934 #if defined (READLINE)
1935 if (no_line_editing && SHOULD_PROMPT())
1937 if (SHOULD_PROMPT())
1941 if (bash_input.type == st_stream)
1948 /* Allow immediate exit if interrupted during input. */
1954 internal_warning ("shell_getc: ignored null byte in input");
1959 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
1963 if (bash_input.type == st_stream)
1967 shell_input_line_terminator = EOF;
1969 shell_input_line[i] = '\0';
1973 shell_input_line[i++] = c;
1977 shell_input_line[--i] = '\0';
1978 current_command_line_count++;
1983 shell_input_line_index = 0;
1984 shell_input_line_len = i; /* == strlen (shell_input_line) */
1986 set_line_mbstate ();
1988 #if defined (HISTORY)
1989 if (remember_on_history && shell_input_line && shell_input_line[0])
1992 # if defined (BANG_HISTORY)
1995 /* If the current delimiter is a single quote, we should not be
1996 performing history expansion, even if we're on a different
1997 line from the original single quote. */
1998 old_hist = history_expansion_inhibited;
1999 if (current_delimiter (dstack) == '\'')
2000 history_expansion_inhibited = 1;
2002 expansions = pre_process_line (shell_input_line, 1, 1);
2003 # if defined (BANG_HISTORY)
2004 history_expansion_inhibited = old_hist;
2006 if (expansions != shell_input_line)
2008 free (shell_input_line);
2009 shell_input_line = expansions;
2010 shell_input_line_len = shell_input_line ?
2011 strlen (shell_input_line) : 0;
2012 if (!shell_input_line_len)
2013 current_command_line_count--;
2015 /* We have to force the xrealloc below because we don't know
2016 the true allocated size of shell_input_line anymore. */
2017 shell_input_line_size = shell_input_line_len;
2019 set_line_mbstate ();
2022 /* Try to do something intelligent with blank lines encountered while
2023 entering multi-line commands. XXX - this is grotesque */
2024 else if (remember_on_history && shell_input_line &&
2025 shell_input_line[0] == '\0' &&
2026 current_command_line_count > 1)
2028 if (current_delimiter (dstack))
2029 /* We know shell_input_line[0] == 0 and we're reading some sort of
2030 quoted string. This means we've got a line consisting of only
2031 a newline in a quoted string. We want to make sure this line
2032 gets added to the history. */
2033 maybe_add_history (shell_input_line);
2037 hdcs = history_delimiting_chars ();
2038 if (hdcs && hdcs[0] == ';')
2039 maybe_add_history (shell_input_line);
2043 #endif /* HISTORY */
2045 if (shell_input_line)
2047 /* Lines that signify the end of the shell's input should not be
2049 if (echo_input_at_read && (shell_input_line[0] ||
2050 shell_input_line_terminator != EOF))
2051 fprintf (stderr, "%s\n", shell_input_line);
2055 shell_input_line_size = 0;
2056 prompt_string_pointer = ¤t_prompt_string;
2057 if (SHOULD_PROMPT ())
2062 /* Add the newline to the end of this string, iff the string does
2063 not already end in an EOF character. */
2064 if (shell_input_line_terminator != EOF)
2066 if (shell_input_line_len + 3 > shell_input_line_size)
2067 shell_input_line = (char *)xrealloc (shell_input_line,
2068 1 + (shell_input_line_size += 2));
2070 shell_input_line[shell_input_line_len] = '\n';
2071 shell_input_line[shell_input_line_len + 1] = '\0';
2073 set_line_mbstate ();
2077 uc = shell_input_line[shell_input_line_index];
2080 shell_input_line_index++;
2082 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2083 /* If UC is NULL, we have reached the end of the current input string. If
2084 pushed_string_list is non-empty, it's time to pop to the previous string
2085 because we have fully consumed the result of the last alias expansion.
2086 Do it transparently; just return the next character of the string popped
2088 if (!uc && (pushed_string_list != (STRING_SAVER *)NULL))
2091 uc = shell_input_line[shell_input_line_index];
2093 shell_input_line_index++;
2095 #endif /* ALIAS || DPAREN_ARITHMETIC */
2097 if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n')
2099 if (SHOULD_PROMPT ())
2105 if (!uc && shell_input_line_terminator == EOF)
2106 return ((shell_input_line_index != 0) ? '\n' : EOF);
2111 /* Put C back into the input for the shell. This might need changes for
2112 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
2113 character different than we read, shell_input_line_property doesn't need
2114 to change when manipulating shell_input_line. The define for
2115 last_shell_getc_is_singlebyte should take care of it, though. */
2120 if (shell_input_line && shell_input_line_index)
2121 shell_input_line[--shell_input_line_index] = c;
2123 eol_ungetc_lookahead = c;
2126 #ifdef INCLUDE_UNUSED
2127 /* Back the input pointer up by one, effectively `ungetting' a character. */
2131 if (shell_input_line && shell_input_line_index)
2132 shell_input_line_index--;
2136 /* Discard input until CHARACTER is seen, then push that character back
2137 onto the input stream. */
2139 discard_until (character)
2144 while ((c = shell_getc (0)) != EOF && c != character)
2152 execute_prompt_command (command)
2156 sh_parser_state_t ps;
2158 save_parser_state (&ps);
2159 last_lastarg = get_string_value ("_");
2161 last_lastarg = savestring (last_lastarg);
2163 parse_and_execute (savestring (command), "PROMPT_COMMAND", SEVAL_NONINT|SEVAL_NOHIST);
2165 restore_parser_state (&ps);
2166 bind_variable ("_", last_lastarg, 0);
2167 FREE (last_lastarg);
2169 if (token_to_read == '\n') /* reset_parser was called */
2173 /* Place to remember the token. We try to keep the buffer
2174 at a reasonable size, but it can grow. */
2175 static char *token = (char *)NULL;
2177 /* Current size of the token buffer. */
2178 static int token_buffer_size;
2180 /* Command to read_token () explaining what we want it to do. */
2183 #define prompt_is_ps1 \
2184 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
2186 /* Function for yyparse to call. yylex keeps track of
2187 the last two tokens read, and calls read_token. */
2191 if (interactive && (current_token == 0 || current_token == '\n'))
2193 /* Before we print a prompt, we might have to check mailboxes.
2194 We do this only if it is time to do so. Notice that only here
2195 is the mail alarm reset; nothing takes place in check_mail ()
2196 except the checking of mail. Please don't change this. */
2197 if (prompt_is_ps1 && time_to_check_mail ())
2200 reset_mail_timer ();
2203 /* Avoid printing a prompt if we're not going to read anything, e.g.
2204 after resetting the parser with read_token (RESET). */
2205 if (token_to_read == 0 && SHOULD_PROMPT ())
2209 two_tokens_ago = token_before_that;
2210 token_before_that = last_read_token;
2211 last_read_token = current_token;
2212 current_token = read_token (READ);
2213 return (current_token);
2216 /* When non-zero, we have read the required tokens
2217 which allow ESAC to be the next one read. */
2218 static int esacs_needed_count;
2221 gather_here_documents ()
2224 while (need_here_doc)
2226 make_here_document (redir_stack[r++]);
2231 /* When non-zero, an open-brace used to create a group is awaiting a close
2233 static int open_brace_count;
2235 #define command_token_position(token) \
2236 (((token) == ASSIGNMENT_WORD) || \
2237 ((token) != SEMI_SEMI && reserved_word_acceptable(token)))
2239 #define assignment_acceptable(token) \
2240 (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0))
2242 /* Check to see if TOKEN is a reserved word and return the token
2244 #define CHECK_FOR_RESERVED_WORD(tok) \
2246 if (!dollar_present && !quoted && \
2247 reserved_word_acceptable (last_read_token)) \
2250 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
2251 if (STREQ (tok, word_token_alist[i].word)) \
2253 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
2255 if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \
2257 if (word_token_alist[i].token == ESAC) \
2258 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
2259 else if (word_token_alist[i].token == CASE) \
2260 parser_state |= PST_CASESTMT; \
2261 else if (word_token_alist[i].token == COND_END) \
2262 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
2263 else if (word_token_alist[i].token == COND_START) \
2264 parser_state |= PST_CONDCMD; \
2265 else if (word_token_alist[i].token == '{') \
2266 open_brace_count++; \
2267 else if (word_token_alist[i].token == '}' && open_brace_count) \
2268 open_brace_count--; \
2269 return (word_token_alist[i].token); \
2276 /* OK, we have a token. Let's try to alias expand it, if (and only if)
2279 It is eligible for expansion if EXPAND_ALIASES is set, and
2280 the token is unquoted and the last token read was a command
2281 separator (or expand_next_token is set), and we are currently
2282 processing an alias (pushed_string_list is non-empty) and this
2283 token is not the same as the current or any previously
2286 Special cases that disqualify:
2287 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
2297 r = xmalloc (l + 2);
2306 alias_expand_token (tokstr)
2312 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
2313 (parser_state & PST_CASEPAT) == 0)
2315 ap = find_alias (tokstr);
2317 /* Currently expanding this token. */
2318 if (ap && (ap->flags & AL_BEINGEXPANDED))
2319 return (NO_EXPANSION);
2321 /* mk_alexpansion puts an extra space on the end of the alias expansion,
2322 so the lookahead by the parser works right. If this gets changed,
2323 make sure the code in shell_getc that deals with reaching the end of
2324 an expanded alias is changed with it. */
2325 expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL;
2329 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
2330 return (RE_READ_TOKEN);
2333 /* This is an eligible token that does not have an expansion. */
2334 return (NO_EXPANSION);
2336 return (NO_EXPANSION);
2341 time_command_acceptable ()
2343 #if defined (COMMAND_TIMING)
2344 switch (last_read_token)
2363 #endif /* COMMAND_TIMING */
2366 /* Handle special cases of token recognition:
2367 IN is recognized if the last token was WORD and the token
2368 before that was FOR or CASE or SELECT.
2370 DO is recognized if the last token was WORD and the token
2371 before that was FOR or SELECT.
2373 ESAC is recognized if the last token caused `esacs_needed_count'
2376 `{' is recognized if the last token as WORD and the token
2377 before that was FUNCTION, or if we just parsed an arithmetic
2380 `}' is recognized if there is an unclosed `{' present.
2382 `-p' is returned as TIMEOPT if the last read token was TIME.
2384 ']]' is returned as COND_END if the parser is currently parsing
2385 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
2387 `time' is returned as TIME if and only if it is immediately
2388 preceded by one of `;', `\n', `||', `&&', or `&'.
2392 special_case_tokens (tokstr)
2395 if ((last_read_token == WORD) &&
2396 #if defined (SELECT_COMMAND)
2397 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
2399 ((token_before_that == FOR) || (token_before_that == CASE)) &&
2401 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
2403 if (token_before_that == CASE)
2405 parser_state |= PST_CASEPAT;
2406 esacs_needed_count++;
2411 if (last_read_token == WORD &&
2412 #if defined (SELECT_COMMAND)
2413 (token_before_that == FOR || token_before_that == SELECT) &&
2415 (token_before_that == FOR) &&
2417 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
2420 /* Ditto for ESAC in the CASE case.
2421 Specifically, this handles "case word in esac", which is a legal
2422 construct, certainly because someone will pass an empty arg to the
2423 case construct, and we don't want it to barf. Of course, we should
2424 insist that the case construct has at least one pattern in it, but
2425 the designers disagree. */
2426 if (esacs_needed_count)
2428 esacs_needed_count--;
2429 if (STREQ (tokstr, "esac"))
2431 parser_state &= ~PST_CASEPAT;
2436 /* The start of a shell function definition. */
2437 if (parser_state & PST_ALLOWOPNBRC)
2439 parser_state &= ~PST_ALLOWOPNBRC;
2440 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2443 function_bstart = line_number;
2444 return ('{'); /* } */
2448 /* We allow a `do' after a for ((...)) without an intervening
2450 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
2452 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
2455 return ('{'); /* } */
2458 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
2460 open_brace_count--; /* { */
2464 #if defined (COMMAND_TIMING)
2465 /* Handle -p after `time'. */
2466 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
2471 #if defined (COMMAND_TIMING)
2472 if (STREQ (token, "time") && ((parser_state & PST_CASEPAT) == 0) && time_command_acceptable ())
2474 #endif /* COMMAND_TIMING */
2477 #if defined (COND_COMMAND) /* [[ */
2478 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
2485 /* Called from shell.c when Control-C is typed at top level. Or
2486 by the error rule at top level. */
2490 dstack.delimiter_depth = 0; /* No delimiters found so far. */
2491 open_brace_count = 0;
2495 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2496 if (pushed_string_list)
2497 free_string_list ();
2498 #endif /* ALIAS || DPAREN_ARITHMETIC */
2500 if (shell_input_line)
2502 free (shell_input_line);
2503 shell_input_line = (char *)NULL;
2504 shell_input_line_size = shell_input_line_index = 0;
2507 FREE (word_desc_to_read);
2508 word_desc_to_read = (WORD_DESC *)NULL;
2510 last_read_token = '\n';
2511 token_to_read = '\n';
2514 /* Read the next token. Command can be READ (normal operation) or
2515 RESET (to normalize state). */
2517 read_token (command)
2520 int character; /* Current character. */
2521 int peek_char; /* Temporary look-ahead character. */
2522 int result; /* The thing to return. */
2524 if (command == RESET)
2532 result = token_to_read;
2533 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
2535 yylval.word = word_desc_to_read;
2536 word_desc_to_read = (WORD_DESC *)NULL;
2542 #if defined (COND_COMMAND)
2543 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
2545 cond_lineno = line_number;
2546 parser_state |= PST_CONDEXPR;
2547 yylval.command = parse_cond_command ();
2548 if (cond_token != COND_END)
2553 token_to_read = COND_END;
2554 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
2560 /* This is a place to jump back to once we have successfully expanded a
2561 token with an alias and pushed the string with push_string () */
2565 /* Read a single word from input. Start by skipping blanks. */
2566 while ((character = shell_getc (1)) != EOF && whitespace (character))
2569 if (character == EOF)
2575 if MBTEST(character == '#' && (!interactive || interactive_comments))
2577 /* A comment. Discard until EOL or EOF, and then return a newline. */
2578 discard_until ('\n');
2580 character = '\n'; /* this will take the next if statement and return. */
2583 if (character == '\n')
2585 /* If we're about to return an unquoted newline, we can go and collect
2586 the text of any pending here document. */
2588 gather_here_documents ();
2591 parser_state &= ~PST_ALEXPNEXT;
2594 parser_state &= ~PST_ASSIGNOK;
2599 /* Shell meta-characters. */
2600 if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
2603 /* Turn off alias tokenization iff this character sequence would
2604 not leave us ready to read a command. */
2605 if (character == '<' || character == '>')
2606 parser_state &= ~PST_ALEXPNEXT;
2609 parser_state &= ~PST_ASSIGNOK;
2611 peek_char = shell_getc (1);
2612 if (character == peek_char)
2617 /* If '<' then we could be at "<<" or at "<<-". We have to
2618 look ahead one more character. */
2619 peek_char = shell_getc (1);
2620 if (peek_char == '-')
2621 return (LESS_LESS_MINUS);
2622 else if (peek_char == '<')
2623 return (LESS_LESS_LESS);
2626 shell_ungetc (peek_char);
2631 return (GREATER_GREATER);
2634 parser_state |= PST_CASEPAT;
2636 parser_state &= ~PST_ALEXPNEXT;
2647 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2649 result = parse_dparen (character);
2657 else if MBTEST(character == '<' && peek_char == '&')
2659 else if MBTEST(character == '>' && peek_char == '&')
2660 return (GREATER_AND);
2661 else if MBTEST(character == '<' && peek_char == '>')
2662 return (LESS_GREATER);
2663 else if MBTEST(character == '>' && peek_char == '|')
2664 return (GREATER_BAR);
2665 else if MBTEST(peek_char == '>' && character == '&')
2666 return (AND_GREATER);
2668 shell_ungetc (peek_char);
2670 /* If we look like we are reading the start of a function
2671 definition, then let the reader know about it so that
2672 we will do the right thing with `{'. */
2673 if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
2675 parser_state |= PST_ALLOWOPNBRC;
2677 parser_state &= ~PST_ALEXPNEXT;
2679 function_dstart = line_number;
2682 /* case pattern lists may be preceded by an optional left paren. If
2683 we're not trying to parse a case pattern list, the left paren
2684 indicates a subshell. */
2685 if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
2686 parser_state |= PST_SUBSHELL;
2688 else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
2689 parser_state &= ~PST_CASEPAT;
2691 else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
2692 parser_state &= ~PST_SUBSHELL;
2694 #if defined (PROCESS_SUBSTITUTION)
2695 /* Check for the constructs which introduce process substitution.
2696 Shells running in `posix mode' don't do process substitution. */
2697 if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/
2698 #endif /* PROCESS_SUBSTITUTION */
2702 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
2703 if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
2706 /* Okay, if we got this far, we have to read a word. Read one,
2707 and then check it against the known ones. */
2708 result = read_token_word (character);
2710 if (result == RE_READ_TOKEN)
2717 * Match a $(...) or other grouping construct. This has to handle embedded
2718 * quoted strings ('', ``, "") and nested constructs. It also must handle
2719 * reprompting the user, if necessary, after reading a newline, and returning
2720 * correct error values if it reads EOF.
2722 #define P_FIRSTCLOSE 0x01
2723 #define P_ALLOWESC 0x02
2724 #define P_DQUOTE 0x04
2725 #define P_COMMAND 0x08 /* parsing a command, so look for comments */
2726 #define P_BACKQUOTE 0x10 /* parsing a backquoted command substitution */
2728 static char matched_pair_error;
2730 parse_matched_pair (qc, open, close, lenp, flags)
2731 int qc; /* `"' if this construct is within double quotes */
2735 int count, ch, was_dollar, in_comment, check_comment;
2736 int pass_next_character, backq_backslash, nestlen, ttranslen, start_lineno;
2737 char *ret, *nestret, *ttrans;
2738 int retind, retsize, rflags;
2741 pass_next_character = backq_backslash = was_dollar = in_comment = 0;
2742 check_comment = (flags & P_COMMAND) && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0;
2744 /* RFLAGS is the set of flags we want to pass to recursive calls. */
2745 rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE);
2747 ret = (char *)xmalloc (retsize = 64);
2750 start_lineno = line_number;
2753 ch = shell_getc (qc != '\'' && pass_next_character == 0 && backq_backslash == 0);
2758 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
2759 EOF_Reached = 1; /* XXX */
2760 return (&matched_pair_error);
2763 /* Possible reprompting. */
2764 if (ch == '\n' && SHOULD_PROMPT ())
2769 /* Add this character. */
2770 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
2778 /* Not exactly right yet */
2779 else if MBTEST(check_comment && in_comment == 0 && ch == '#' && (retind == 0 || ret[retind-1] == '\n' || whitespace (ret[retind - 1])))
2782 /* last char was backslash inside backquoted command substitution */
2783 if (backq_backslash)
2785 backq_backslash = 0;
2786 /* Placeholder for adding special characters */
2789 if (pass_next_character) /* last char was backslash */
2791 pass_next_character = 0;
2792 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
2794 if (retind > 0) retind--; /* swallow previously-added backslash */
2798 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2799 if MBTEST(ch == CTLESC || ch == CTLNUL)
2800 ret[retind++] = CTLESC;
2804 else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
2806 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2807 ret[retind++] = CTLESC;
2811 else if MBTEST(ch == close) /* ending delimiter */
2813 /* handle nested ${...} specially. */
2814 else if MBTEST(open != close && was_dollar && open == '{' && ch == open) /* } */
2816 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
2819 /* Add this character. */
2820 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
2823 if (open == '\'') /* '' inside grouping construct */
2825 if MBTEST((flags & P_ALLOWESC) && ch == '\\')
2826 pass_next_character++;
2827 else if MBTEST((flags & P_BACKQUOTE) && ch == '\\')
2832 if MBTEST(ch == '\\') /* backslashes */
2833 pass_next_character++;
2835 if (open != close) /* a grouping construct */
2837 if MBTEST(shellquote (ch))
2839 /* '', ``, or "" inside $(...) or other grouping construct. */
2840 push_delimiter (dstack, ch);
2841 if MBTEST(was_dollar && ch == '\'') /* $'...' inside group */
2842 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
2844 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
2845 pop_delimiter (dstack);
2846 if (nestret == &matched_pair_error)
2849 return &matched_pair_error;
2851 if MBTEST(was_dollar && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
2853 /* Translate $'...' here. */
2854 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
2857 if ((rflags & P_DQUOTE) == 0)
2859 nestret = sh_single_quote (ttrans);
2861 nestlen = strlen (nestret);
2866 nestlen = ttranslen;
2868 retind -= 2; /* back up before the $' */
2870 else if MBTEST(was_dollar && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
2872 /* Locale expand $"..." here. */
2873 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
2876 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
2878 nestlen = ttranslen + 2;
2879 retind -= 2; /* back up before the $" */
2884 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2885 strcpy (ret + retind, nestret);
2891 /* Parse an old-style command substitution within double quotes as a
2893 /* XXX - sh and ksh93 don't do this - XXX */
2894 else if MBTEST(open == '"' && ch == '`')
2896 nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags);
2898 if (nestret == &matched_pair_error)
2901 return &matched_pair_error;
2905 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2906 strcpy (ret + retind, nestret);
2911 else if MBTEST(qc == '`' && (ch == '"' || ch == '\'') && in_comment == 0)
2913 /* Add P_BACKQUOTE so backslash quotes the next character and
2914 shell_getc does the right thing with \<newline>. We do this for
2915 a measure of backwards compatibility -- it's not strictly the
2916 right POSIX thing. */
2917 nestret = parse_matched_pair (0, ch, ch, &nestlen, rflags|P_BACKQUOTE);
2920 else if MBTEST(was_dollar && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
2921 /* check for $(), $[], or ${} inside quoted string. */
2923 if (open == ch) /* undo previous increment */
2925 if (ch == '(') /* ) */
2926 nestret = parse_matched_pair (0, '(', ')', &nestlen, rflags & ~P_DQUOTE);
2927 else if (ch == '{') /* } */
2928 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|rflags);
2929 else if (ch == '[') /* ] */
2930 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
2934 was_dollar = MBTEST(ch == '$');
2943 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2944 /* Parse a double-paren construct. It can be either an arithmetic
2945 command, an arithmetic `for' command, or a nested subshell. Returns
2946 the parsed token, -1 on error, or -2 if we didn't do anything and
2947 should just go on. */
2952 int cmdtyp, len, sline;
2956 #if defined (ARITH_FOR_COMMAND)
2957 if (last_read_token == FOR)
2959 arith_for_lineno = line_number;
2960 cmdtyp = parse_arith_cmd (&wval, 0);
2963 wd = alloc_word_desc ();
2965 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
2966 return (ARITH_FOR_EXPRS);
2969 return -1; /* ERROR */
2973 #if defined (DPAREN_ARITHMETIC)
2974 if (reserved_word_acceptable (last_read_token))
2976 sline = line_number;
2978 cmdtyp = parse_arith_cmd (&wval, 0);
2979 if (cmdtyp == 1) /* arithmetic command */
2981 wd = alloc_word_desc ();
2983 wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE;
2984 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
2987 else if (cmdtyp == 0) /* nested subshell */
2989 push_string (wval, 0, (alias_t *)NULL);
2990 if ((parser_state & PST_CASEPAT) == 0)
2991 parser_state |= PST_SUBSHELL;
2999 return -2; /* XXX */
3002 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
3003 If not, assume it's a nested subshell for backwards compatibility and
3004 return 0. In any case, put the characters we've consumed into a locally-
3005 allocated buffer and make *ep point to that buffer. Return -1 on an
3006 error, for example EOF. */
3008 parse_arith_cmd (ep, adddq)
3012 int exp_lineno, rval, c;
3013 char *ttok, *tokstr;
3016 exp_lineno = line_number;
3017 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
3019 if (ttok == &matched_pair_error)
3021 /* Check that the next character is the closing right paren. If
3022 not, this is a syntax error. ( */
3027 tokstr = (char *)xmalloc (ttoklen + 4);
3029 /* if ADDDQ != 0 then (( ... )) -> "..." */
3030 if (rval == 1 && adddq) /* arith cmd, add double quotes */
3033 strncpy (tokstr + 1, ttok, ttoklen - 1);
3034 tokstr[ttoklen] = '"';
3035 tokstr[ttoklen+1] = '\0';
3037 else if (rval == 1) /* arith cmd, don't add double quotes */
3039 strncpy (tokstr, ttok, ttoklen - 1);
3040 tokstr[ttoklen-1] = '\0';
3042 else /* nested subshell */
3045 strncpy (tokstr + 1, ttok, ttoklen - 1);
3046 tokstr[ttoklen] = ')';
3047 tokstr[ttoklen+1] = c;
3048 tokstr[ttoklen+2] = '\0';
3055 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
3057 #if defined (COND_COMMAND)
3063 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
3064 parser_error (cond_lineno, _("unexpected EOF while looking for `]]'"));
3065 else if (cond_token != COND_ERROR)
3067 if (etext = error_token_from_token (cond_token))
3069 parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext);
3073 parser_error (cond_lineno, _("syntax error in conditional expression"));
3080 return (cond_or ());
3089 if (cond_token == OR_OR)
3092 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
3103 if (cond_token == AND_AND)
3106 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
3112 cond_skip_newlines ()
3114 while ((cond_token = read_token (READ)) == '\n')
3116 if (SHOULD_PROMPT ())
3119 return (cond_token);
3122 #define COND_RETURN_ERROR() \
3123 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
3129 COND_COM *term, *tleft, *tright;
3133 /* Read a token. It can be a left paren, a `!', a unary operator, or a
3134 word that should be the first argument of a binary operator. Start by
3135 skipping newlines, since this is a compound command. */
3136 tok = cond_skip_newlines ();
3137 lineno = line_number;
3138 if (tok == COND_END)
3140 COND_RETURN_ERROR ();
3142 else if (tok == '(')
3144 term = cond_expr ();
3145 if (cond_token != ')')
3148 dispose_cond_node (term); /* ( */
3149 if (etext = error_token_from_token (cond_token))
3151 parser_error (lineno, _("unexpected token `%s', expected `)'"), etext);
3155 parser_error (lineno, _("expected `)'"));
3156 COND_RETURN_ERROR ();
3158 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
3159 (void)cond_skip_newlines ();
3161 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
3164 dispose_word (yylval.word); /* not needed */
3165 term = cond_term ();
3167 term->flags |= CMD_INVERT_RETURN;
3169 else if (tok == WORD && test_unop (yylval.word->word))
3172 tok = read_token (READ);
3175 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3176 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
3181 if (etext = error_token_from_token (tok))
3183 parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext);
3187 parser_error (line_number, _("unexpected argument to conditional unary operator"));
3188 COND_RETURN_ERROR ();
3191 (void)cond_skip_newlines ();
3193 else if (tok == WORD) /* left argument to binary operator */
3196 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3199 tok = read_token (READ);
3200 if (tok == WORD && test_binop (yylval.word->word))
3202 #if defined (COND_REGEXP)
3203 else if (tok == WORD && STREQ (yylval.word->word,"=~"))
3206 else if (tok == '<' || tok == '>')
3207 op = make_word_from_token (tok); /* ( */
3208 /* There should be a check before blindly accepting the `)' that we have
3209 seen the opening `('. */
3210 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
3212 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
3213 the test command. Similarly for [[ x && expr ]] or
3214 [[ x || expr ]] or [[ (x) ]]. */
3215 op = make_word ("-n");
3216 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
3222 if (etext = error_token_from_token (tok))
3224 parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext);
3228 parser_error (line_number, _("conditional binary operator expected"));
3229 dispose_cond_node (tleft);
3230 COND_RETURN_ERROR ();
3234 tok = read_token (READ);
3237 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
3238 term = make_cond_node (COND_BINARY, op, tleft, tright);
3242 if (etext = error_token_from_token (tok))
3244 parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext);
3248 parser_error (line_number, _("unexpected argument to conditional binary operator"));
3249 dispose_cond_node (tleft);
3251 COND_RETURN_ERROR ();
3254 (void)cond_skip_newlines ();
3259 parser_error (line_number, _("unexpected token `%c' in conditional command"), tok);
3260 else if (etext = error_token_from_token (tok))
3262 parser_error (line_number, _("unexpected token `%s' in conditional command"), etext);
3266 parser_error (line_number, _("unexpected token %d in conditional command"), tok);
3267 COND_RETURN_ERROR ();
3272 /* This is kind of bogus -- we slip a mini recursive-descent parser in
3273 here to handle the conditional statement syntax. */
3275 parse_cond_command ()
3279 cexp = cond_expr ();
3280 return (make_cond_command (cexp));
3284 #if defined (ARRAY_VARS)
3285 /* When this is called, it's guaranteed that we don't care about anything
3286 in t beyond i. We do save and restore the chars, though. */
3288 token_is_assignment (t, i)
3292 unsigned char c, c1;
3295 c = t[i]; c1 = t[i+1];
3296 t[i] = '='; t[i+1] = '\0';
3297 r = assignment (t, (parser_state & PST_COMPASSIGN) != 0);
3298 t[i] = c; t[i+1] = c1;
3302 /* XXX - possible changes here for `+=' */
3304 token_is_ident (t, i)
3313 r = legal_identifier (t);
3320 read_token_word (character)
3323 /* The value for YYLVAL when a WORD is read. */
3324 WORD_DESC *the_word;
3326 /* Index into the token that we are building. */
3329 /* ALL_DIGITS becomes zero when we see a non-digit. */
3330 int all_digit_token;
3332 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
3335 /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
3337 int compound_assignment;
3339 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
3342 /* Non-zero means to ignore the value of the next character, and just
3343 to add it no matter what. */
3344 int pass_next_character;
3346 /* The current delimiting character. */
3348 int result, peek_char;
3349 char *ttok, *ttrans;
3350 int ttoklen, ttranslen;
3353 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
3354 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
3357 all_digit_token = DIGIT (character);
3358 dollar_present = quoted = pass_next_character = compound_assignment = 0;
3362 if (character == EOF)
3365 if (pass_next_character)
3367 pass_next_character = 0;
3371 cd = current_delimiter (dstack);
3373 /* Handle backslashes. Quote lots of things when not inside of
3374 double-quotes, quote some things inside of double-quotes. */
3375 if MBTEST(character == '\\')
3377 peek_char = shell_getc (0);
3379 /* Backslash-newline is ignored in all cases except
3380 when quoted with single quotes. */
3381 if (peek_char == '\n')
3384 goto next_character;
3388 shell_ungetc (peek_char);
3390 /* If the next character is to be quoted, note it now. */
3391 if (cd == 0 || cd == '`' ||
3392 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
3393 pass_next_character++;
3400 /* Parse a matched pair of quote characters. */
3401 if MBTEST(shellquote (character))
3403 push_delimiter (dstack, character);
3404 ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0);
3405 pop_delimiter (dstack);
3406 if (ttok == &matched_pair_error)
3407 return -1; /* Bail immediately. */
3408 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3409 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
3410 token[token_index++] = character;
3411 strcpy (token + token_index, ttok);
3412 token_index += ttoklen;
3413 all_digit_token = 0;
3415 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
3417 goto next_character;
3420 #ifdef EXTENDED_GLOB
3421 /* Parse a ksh-style extended pattern matching specification. */
3422 if (extended_glob && PATTERN_CHAR (character))
3424 peek_char = shell_getc (1);
3425 if MBTEST(peek_char == '(') /* ) */
3427 push_delimiter (dstack, peek_char);
3428 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
3429 pop_delimiter (dstack);
3430 if (ttok == &matched_pair_error)
3431 return -1; /* Bail immediately. */
3432 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3434 TOKEN_DEFAULT_GROW_SIZE);
3435 token[token_index++] = character;
3436 token[token_index++] = peek_char;
3437 strcpy (token + token_index, ttok);
3438 token_index += ttoklen;
3440 dollar_present = all_digit_token = 0;
3441 goto next_character;
3444 shell_ungetc (peek_char);
3446 #endif /* EXTENDED_GLOB */
3448 /* If the delimiter character is not single quote, parse some of
3449 the shell expansions that must be read as a single word. */
3450 if (shellexp (character))
3452 peek_char = shell_getc (1);
3453 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
3454 if MBTEST(peek_char == '(' || \
3455 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
3457 if (peek_char == '{') /* } */
3458 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE);
3459 else if (peek_char == '(') /* ) */
3461 /* XXX - push and pop the `(' as a delimiter for use by
3462 the command-oriented-history code. This way newlines
3463 appearing in the $(...) string get added to the
3464 history literally rather than causing a possibly-
3465 incorrect `;' to be added. ) */
3466 push_delimiter (dstack, peek_char);
3467 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, P_COMMAND);
3468 pop_delimiter (dstack);
3471 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
3472 if (ttok == &matched_pair_error)
3473 return -1; /* Bail immediately. */
3474 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3476 TOKEN_DEFAULT_GROW_SIZE);
3477 token[token_index++] = character;
3478 token[token_index++] = peek_char;
3479 strcpy (token + token_index, ttok);
3480 token_index += ttoklen;
3483 all_digit_token = 0;
3484 goto next_character;
3486 /* This handles $'...' and $"..." new-style quoted strings. */
3487 else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
3491 first_line = line_number;
3492 push_delimiter (dstack, peek_char);
3493 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
3495 (peek_char == '\'') ? P_ALLOWESC : 0);
3496 pop_delimiter (dstack);
3497 if (ttok == &matched_pair_error)
3499 if (peek_char == '\'')
3501 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
3504 /* Insert the single quotes and correctly quote any
3505 embedded single quotes (allowed because P_ALLOWESC was
3506 passed to parse_matched_pair). */
3507 ttok = sh_single_quote (ttrans);
3509 ttranslen = strlen (ttok);
3514 /* Try to locale-expand the converted string. */
3515 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
3518 /* Add the double quotes back */
3519 ttok = sh_mkdoublequoted (ttrans, ttranslen, 0);
3525 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2,
3527 TOKEN_DEFAULT_GROW_SIZE);
3528 strcpy (token + token_index, ttrans);
3529 token_index += ttranslen;
3532 all_digit_token = 0;
3533 goto next_character;
3535 /* This could eventually be extended to recognize all of the
3536 shell's single-character parameter expansions, and set flags.*/
3537 else if MBTEST(character == '$' && peek_char == '$')
3539 ttok = (char *)xmalloc (3);
3540 ttok[0] = ttok[1] = '$';
3542 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
3544 TOKEN_DEFAULT_GROW_SIZE);
3545 strcpy (token + token_index, ttok);
3548 all_digit_token = 0;
3550 goto next_character;
3553 shell_ungetc (peek_char);
3556 #if defined (ARRAY_VARS)
3557 /* Identify possible array subscript assignment; match [...] */
3558 else if MBTEST(character == '[' && token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) /* ] */
3560 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
3561 if (ttok == &matched_pair_error)
3562 return -1; /* Bail immediately. */
3563 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
3565 TOKEN_DEFAULT_GROW_SIZE);
3566 token[token_index++] = character;
3567 strcpy (token + token_index, ttok);
3568 token_index += ttoklen;
3570 all_digit_token = 0;
3571 goto next_character;
3573 /* Identify possible compound array variable assignment. */
3574 else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index))
3576 peek_char = shell_getc (1);
3577 if MBTEST(peek_char == '(') /* ) */
3579 ttok = parse_compound_assignment (&ttoklen);
3581 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
3583 TOKEN_DEFAULT_GROW_SIZE);
3585 token[token_index++] = '=';
3586 token[token_index++] = '(';
3589 strcpy (token + token_index, ttok);
3590 token_index += ttoklen;
3592 token[token_index++] = ')';
3594 all_digit_token = 0;
3595 compound_assignment = 1;
3597 goto next_character;
3599 goto got_token; /* ksh93 seems to do this */
3603 shell_ungetc (peek_char);
3607 /* When not parsing a multi-character word construct, shell meta-
3608 characters break words. */
3609 if MBTEST(shellbreak (character))
3611 shell_ungetc (character);
3617 all_digit_token &= DIGIT (character);
3618 dollar_present |= character == '$';
3620 if (character == CTLESC || character == CTLNUL)
3621 token[token_index++] = CTLESC;
3623 token[token_index++] = character;
3625 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
3626 TOKEN_DEFAULT_GROW_SIZE);
3629 if (character == '\n' && SHOULD_PROMPT ())
3632 /* We want to remove quoted newlines (that is, a \<newline> pair)
3633 unless we are within single quotes or pass_next_character is
3634 set (the shell equivalent of literal-next). */
3635 cd = current_delimiter (dstack);
3636 character = shell_getc (cd != '\'' && pass_next_character == 0);
3637 } /* end for (;;) */
3641 token[token_index] = '\0';
3643 /* Check to see what thing we should return. If the last_read_token
3644 is a `<', or a `&', or the character which ended this token is
3645 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
3646 Otherwise, it is just a word, and should be returned as such. */
3647 if MBTEST(all_digit_token && (character == '<' || character == '>' || \
3648 last_read_token == LESS_AND || \
3649 last_read_token == GREATER_AND))
3651 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
3652 yylval.number = lvalue;
3658 /* Check for special case tokens. */
3659 result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
3664 /* Posix.2 does not allow reserved words to be aliased, so check for all
3665 of them, including special cases, before expanding the current token
3667 if MBTEST(posixly_correct)
3668 CHECK_FOR_RESERVED_WORD (token);
3670 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
3671 inhibits alias expansion. */
3672 if (expand_aliases && quoted == 0)
3674 result = alias_expand_token (token);
3675 if (result == RE_READ_TOKEN)
3676 return (RE_READ_TOKEN);
3677 else if (result == NO_EXPANSION)
3678 parser_state &= ~PST_ALEXPNEXT;
3681 /* If not in Posix.2 mode, check for reserved words after alias
3683 if MBTEST(posixly_correct == 0)
3685 CHECK_FOR_RESERVED_WORD (token);
3687 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
3688 the_word->word = (char *)xmalloc (1 + token_index);
3689 the_word->flags = 0;
3690 strcpy (the_word->word, token);
3692 the_word->flags |= W_HASDOLLAR;
3694 the_word->flags |= W_QUOTED;
3695 if (compound_assignment)
3696 the_word->flags |= W_COMPASSIGN;
3697 /* A word is an assignment if it appears at the beginning of a
3698 simple command, or after another assignment word. This is
3699 context-dependent, so it cannot be handled in the grammar. */
3700 if (assignment (token, (parser_state & PST_COMPASSIGN) != 0))
3702 the_word->flags |= W_ASSIGNMENT;
3703 /* Don't perform word splitting on assignment statements. */
3704 if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0)
3705 the_word->flags |= W_NOSPLIT;
3708 if (command_token_position (last_read_token))
3711 b = builtin_address_internal (token, 0);
3712 if (b && (b->flags & ASSIGNMENT_BUILTIN))
3713 parser_state |= PST_ASSIGNOK;
3714 else if (STREQ (token, "eval") || STREQ (token, "let"))
3715 parser_state |= PST_ASSIGNOK;
3718 yylval.word = the_word;
3720 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
3721 ? ASSIGNMENT_WORD : WORD;
3723 switch (last_read_token)
3726 parser_state |= PST_ALLOWOPNBRC;
3727 function_dstart = line_number;
3732 if (word_top < MAX_CASE_NEST)
3734 word_lineno[word_top] = line_number;
3741 /* Return 1 if TOKSYM is a token that after being read would allow
3742 a reserved word to be seen, else 0. */
3744 reserved_word_acceptable (toksym)
3780 /* Return the index of TOKEN in the alist of reserved words, or -1 if
3781 TOKEN is not a shell reserved word. */
3783 find_reserved_word (tokstr)
3787 for (i = 0; word_token_alist[i].word; i++)
3788 if (STREQ (tokstr, word_token_alist[i].word))
3794 #if defined (READLINE)
3795 /* Called after each time readline is called. This insures that whatever
3796 the new prompt string is gets propagated to readline's local prompt
3799 reset_readline_prompt ()
3803 if (prompt_string_pointer)
3805 temp_prompt = (*prompt_string_pointer)
3806 ? decode_prompt_string (*prompt_string_pointer)
3809 if (temp_prompt == 0)
3811 temp_prompt = (char *)xmalloc (1);
3812 temp_prompt[0] = '\0';
3815 FREE (current_readline_prompt);
3816 current_readline_prompt = temp_prompt;
3819 #endif /* READLINE */
3822 #if defined (HISTORY)
3823 /* A list of tokens which can be followed by newlines, but not by
3824 semi-colons. When concatenating multiple lines of history, the
3825 newline separator for such tokens is replaced with a space. */
3826 static int no_semi_successors[] = {
3827 '\n', '{', '(', ')', ';', '&', '|',
3828 CASE, DO, ELSE, IF, SEMI_SEMI, THEN, UNTIL, WHILE, AND_AND, OR_OR, IN,
3832 /* If we are not within a delimited expression, try to be smart
3833 about which separators can be semi-colons and which must be
3834 newlines. Returns the string that should be added into the
3837 history_delimiting_chars ()
3841 if (dstack.delimiter_depth != 0)
3844 /* First, handle some special cases. */
3846 /* If we just read `()', assume it's a function definition, and don't
3847 add a semicolon. If the token before the `)' was not `(', and we're
3848 not in the midst of parsing a case statement, assume it's a
3849 parenthesized command and add the semicolon. */
3851 if (token_before_that == ')')
3853 if (two_tokens_ago == '(') /*)*/ /* function def */
3855 /* This does not work for subshells inside case statement
3856 command lists. It's a suboptimal solution. */
3857 else if (parser_state & PST_CASESTMT) /* case statement pattern */
3860 return "; "; /* (...) subshell */
3862 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
3863 return " "; /* function def using `function name' without `()' */
3865 else if (token_before_that == WORD && two_tokens_ago == FOR)
3867 /* Tricky. `for i\nin ...' should not have a semicolon, but
3868 `for i\ndo ...' should. We do what we can. */
3869 for (i = shell_input_line_index; whitespace(shell_input_line[i]); i++)
3871 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
3875 else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT))
3878 for (i = 0; no_semi_successors[i]; i++)
3880 if (token_before_that == no_semi_successors[i])
3886 #endif /* HISTORY */
3888 /* Issue a prompt, or prepare to issue a prompt when the next character
3895 if (interactive == 0 || expanding_alias()) /* XXX */
3898 ps1_prompt = get_string_value ("PS1");
3899 ps2_prompt = get_string_value ("PS2");
3901 if (!prompt_string_pointer)
3902 prompt_string_pointer = &ps1_prompt;
3904 temp_prompt = *prompt_string_pointer
3905 ? decode_prompt_string (*prompt_string_pointer)
3908 if (temp_prompt == 0)
3910 temp_prompt = (char *)xmalloc (1);
3911 temp_prompt[0] = '\0';
3914 current_prompt_string = *prompt_string_pointer;
3915 prompt_string_pointer = &ps2_prompt;
3917 #if defined (READLINE)
3918 if (!no_line_editing)
3920 FREE (current_readline_prompt);
3921 current_readline_prompt = temp_prompt;
3924 #endif /* READLINE */
3926 FREE (current_decoded_prompt);
3927 current_decoded_prompt = temp_prompt;
3932 get_current_prompt_level ()
3934 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
3938 set_current_prompt_level (x)
3941 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
3942 current_prompt_string = *prompt_string_pointer;
3948 fprintf (stderr, "%s", current_decoded_prompt);
3952 /* Return a string which will be printed as a prompt. The string
3953 may contain special characters which are decoded as follows:
3956 \d the date in Day Mon Date format
3957 \e escape (ascii 033)
3958 \h the hostname up to the first `.'
3960 \j the number of active jobs
3961 \l the basename of the shell's tty device name
3964 \s the name of the shell
3965 \t the time in 24-hour hh:mm:ss format
3966 \T the time in 12-hour hh:mm:ss format
3967 \@ the time in 12-hour hh:mm am/pm format
3968 \A the time in 24-hour hh:mm format
3969 \D{fmt} the result of passing FMT to strftime(3)
3971 \v the version of bash (e.g., 2.00)
3972 \V the release of bash, version + patchlevel (e.g., 2.00.0)
3973 \w the current working directory
3974 \W the last element of $PWD
3975 \! the history number of this command
3976 \# the command number of this command
3977 \$ a $ or a # if you are root
3978 \nnn character code nnn in octal
3980 \[ begin a sequence of non-printing chars
3981 \] end a sequence of non-printing chars
3983 #define PROMPT_GROWTH 48
3985 decode_prompt_string (string)
3990 struct dstack save_dstack;
3991 int last_exit_value;
3992 #if defined (PROMPT_STRING_DECODE)
3993 int result_size, result_index;
3995 char *temp, octal_string[4];
4001 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
4002 result[result_index = 0] = 0;
4003 temp = (char *)NULL;
4005 while (c = *string++)
4007 if (posixly_correct && c == '!')
4011 temp = savestring ("!");
4016 #if !defined (HISTORY)
4017 temp = savestring ("1");
4019 temp = itos (history_number ());
4020 #endif /* HISTORY */
4021 string--; /* add_string increments string again. */
4039 strncpy (octal_string, string, 3);
4040 octal_string[3] = '\0';
4042 n = read_octal (octal_string);
4043 temp = (char *)xmalloc (3);
4045 if (n == CTLESC || n == CTLNUL)
4062 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
4065 c = 0; /* tested at add_string: */
4073 /* Make the current time/date into a string. */
4074 (void) time (&the_time);
4075 tm = localtime (&the_time);
4078 n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm);
4080 n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm);
4082 n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm);
4084 n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm);
4086 n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm);
4091 timebuf[sizeof(timebuf) - 1] = '\0';
4093 temp = savestring (timebuf);
4096 case 'D': /* strftime format */
4097 if (string[1] != '{') /* } */
4100 (void) time (&the_time);
4101 tm = localtime (&the_time);
4102 string += 2; /* skip { */
4103 timefmt = xmalloc (strlen (string) + 3);
4104 for (t = timefmt; *string && *string != '}'; )
4107 c = *string; /* tested at add_string */
4108 if (timefmt[0] == '\0')
4111 timefmt[1] = 'X'; /* locale-specific current time */
4114 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
4120 timebuf[sizeof(timebuf) - 1] = '\0';
4122 if (promptvars || posixly_correct)
4123 /* Make sure that expand_prompt_string is called with a
4124 second argument of Q_DOUBLE_QUOTES if we use this
4126 temp = sh_backslash_quote_for_double_quotes (timebuf);
4128 temp = savestring (timebuf);
4132 temp = (char *)xmalloc (3);
4133 temp[0] = no_line_editing ? '\n' : '\r';
4134 temp[1] = no_line_editing ? '\0' : '\n';
4139 temp = base_pathname (shell_name);
4140 temp = savestring (temp);
4145 temp = (char *)xmalloc (16);
4147 strcpy (temp, dist_version);
4149 sprintf (temp, "%s.%d", dist_version, patch_level);
4155 /* Use the value of PWD because it is much more efficient. */
4156 char t_string[PATH_MAX], *t;
4159 temp = get_string_value ("PWD");
4163 if (getcwd (t_string, sizeof(t_string)) == 0)
4169 tlen = strlen (t_string);
4173 tlen = sizeof (t_string) - 1;
4174 strncpy (t_string, temp, tlen);
4176 t_string[tlen] = '\0';
4178 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
4179 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
4180 /* Abbreviate \W as ~ if $PWD == $HOME */
4181 if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0))
4183 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
4185 t = strrchr (t_string, '/');
4187 strcpy (t_string, t + 1);
4191 #undef DOUBLE_SLASH_ROOT
4193 /* polite_directory_format is guaranteed to return a string
4194 no longer than PATH_MAX - 1 characters. */
4195 strcpy (t_string, polite_directory_format (t_string));
4197 /* If we're going to be expanding the prompt string later,
4198 quote the directory name. */
4199 if (promptvars || posixly_correct)
4200 /* Make sure that expand_prompt_string is called with a
4201 second argument of Q_DOUBLE_QUOTES if we use this
4203 temp = sh_backslash_quote_for_double_quotes (t_string);
4205 temp = savestring (t_string);
4211 if (current_user.user_name == 0)
4212 get_current_user_info ();
4213 temp = savestring (current_user.user_name);
4218 temp = savestring (current_host_name);
4219 if (c == 'h' && (t = (char *)strchr (temp, '.')))
4224 temp = itos (current_command_number);
4228 #if !defined (HISTORY)
4229 temp = savestring ("1");
4231 temp = itos (history_number ());
4232 #endif /* HISTORY */
4236 t = temp = (char *)xmalloc (3);
4237 if ((promptvars || posixly_correct) && (current_user.euid != 0))
4239 *t++ = current_user.euid == 0 ? '#' : '$';
4244 temp = itos (count_all_jobs ());
4248 #if defined (HAVE_TTYNAME)
4249 temp = (char *)ttyname (fileno (stdin));
4250 t = temp ? base_pathname (temp) : "tty";
4251 temp = savestring (t);
4253 temp = savestring ("tty");
4254 #endif /* !HAVE_TTYNAME */
4257 #if defined (READLINE)
4260 if (no_line_editing)
4265 temp = (char *)xmalloc (3);
4267 temp[1] = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
4270 #endif /* READLINE */
4276 temp = (char *)xmalloc (2);
4283 else /* (c == '\\') */
4290 temp = (char *)xmalloc (3);
4299 sub_append_string (temp, result, &result_index, &result_size);
4300 temp = (char *)NULL; /* Freed in sub_append_string (). */
4301 result[result_index] = '\0';
4307 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
4308 result[result_index++] = c;
4309 result[result_index] = '\0';
4312 #else /* !PROMPT_STRING_DECODE */
4313 result = savestring (string);
4314 #endif /* !PROMPT_STRING_DECODE */
4316 /* Save the delimiter stack and point `dstack' to temp space so any
4317 command substitutions in the prompt string won't result in screwing
4318 up the parser's quoting state. */
4319 save_dstack = dstack;
4320 dstack = temp_dstack;
4321 dstack.delimiter_depth = 0;
4323 /* Perform variable and parameter expansion and command substitution on
4324 the prompt string. */
4325 if (promptvars || posixly_correct)
4327 last_exit_value = last_command_exit_value;
4328 list = expand_prompt_string (result, Q_DOUBLE_QUOTES);
4330 result = string_list (list);
4331 dispose_words (list);
4332 last_command_exit_value = last_exit_value;
4336 t = dequote_string (result);
4341 dstack = save_dstack;
4346 /************************************************
4350 ************************************************/
4352 /* Report a syntax error, and restart the parser. Call here for fatal
4358 report_syntax_error ((char *)NULL);
4364 error_token_from_token (token)
4369 if (t = find_token_in_alist (token, word_token_alist, 0))
4372 if (t = find_token_in_alist (token, other_token_alist, 0))
4376 /* This stuff is dicy and needs closer inspection */
4377 switch (current_token)
4380 case ASSIGNMENT_WORD:
4382 t = savestring (yylval.word->word);
4385 t = itos (yylval.number);
4388 if (yylval.word_list)
4389 t = string_list (yylval.word_list);
4391 case ARITH_FOR_EXPRS:
4392 if (yylval.word_list)
4393 t = string_list_internal (yylval.word_list, " ; ");
4396 t = (char *)NULL; /* punt */
4404 error_token_from_text ()
4409 t = shell_input_line;
4410 i = shell_input_line_index;
4414 if (i && t[i] == '\0')
4417 while (i && (whitespace (t[i]) || t[i] == '\n'))
4423 while (i && (member (t[i], " \n\t;|&") == 0))
4426 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
4429 /* Return our idea of the offending token. */
4430 if (token_end || (i == 0 && token_end == 0))
4433 msg = substring (t, i, token_end);
4434 else /* one-character token */
4436 msg = (char *)xmalloc (2);
4446 print_offending_line ()
4451 msg = savestring (shell_input_line);
4452 token_end = strlen (msg);
4453 while (token_end && msg[token_end - 1] == '\n')
4454 msg[--token_end] = '\0';
4456 parser_error (line_number, "`%s'", msg);
4460 /* Report a syntax error with line numbers, etc.
4461 Call here for recoverable errors. If you have a message to print,
4462 then place it in MESSAGE, otherwise pass NULL and this will figure
4463 out an appropriate message for you. */
4465 report_syntax_error (message)
4472 parser_error (line_number, "%s", message);
4473 if (interactive && EOF_Reached)
4475 last_command_exit_value = EX_USAGE;
4479 /* If the line of input we're reading is not null, try to find the
4480 objectionable token. First, try to figure out what token the
4481 parser's complaining about by looking at current_token. */
4482 if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
4484 parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
4487 if (interactive == 0)
4488 print_offending_line ();
4490 last_command_exit_value = EX_USAGE;
4494 /* If looking at the current token doesn't prove fruitful, try to find the
4495 offending token by analyzing the text of the input line near the current
4496 input line index and report what we find. */
4497 if (shell_input_line && *shell_input_line)
4499 msg = error_token_from_text ();
4502 parser_error (line_number, _("syntax error near `%s'"), msg);
4506 /* If not interactive, print the line containing the error. */
4507 if (interactive == 0)
4508 print_offending_line ();
4512 msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error");
4513 parser_error (line_number, "%s", msg);
4514 /* When the shell is interactive, this file uses EOF_Reached
4515 only for error reporting. Other mechanisms are used to
4516 decide whether or not to exit. */
4517 if (interactive && EOF_Reached)
4521 last_command_exit_value = EX_USAGE;
4524 /* ??? Needed function. ??? We have to be able to discard the constructs
4525 created during parsing. In the case of error, we want to return
4526 allocated objects to the memory pool. In the case of no error, we want
4527 to throw away the information about where the allocated objects live.
4528 (dispose_command () will actually free the command.) */
4530 discard_parser_constructs (error_p)
4535 /************************************************
4539 ************************************************/
4541 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
4543 /* A flag denoting whether or not ignoreeof is set. */
4546 /* The number of times that we have encountered an EOF character without
4547 another character intervening. When this gets above the limit, the
4548 shell terminates. */
4549 int eof_encountered = 0;
4551 /* The limit for eof_encountered. */
4552 int eof_encountered_limit = 10;
4554 /* If we have EOF as the only input unit, this user wants to leave
4555 the shell. If the shell is not interactive, then just leave.
4556 Otherwise, if ignoreeof is set, and we haven't done this the
4557 required number of times in a row, print a message. */
4559 handle_eof_input_unit ()
4563 /* shell.c may use this to decide whether or not to write out the
4564 history, among other things. We use it only for error reporting
4569 /* If the user wants to "ignore" eof, then let her do so, kind of. */
4572 if (eof_encountered < eof_encountered_limit)
4574 fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
4575 login_shell ? "logout" : "exit");
4577 /* Reset the parsing state. */
4578 last_read_token = current_token = '\n';
4579 /* Reset the prompt string to be $PS1. */
4580 prompt_string_pointer = (char **)NULL;
4586 /* In this case EOF should exit the shell. Do it now. */
4588 exit_builtin ((WORD_LIST *)NULL);
4592 /* We don't write history files, etc., for non-interactive shells. */
4597 /************************************************
4599 * STRING PARSING FUNCTIONS *
4601 ************************************************/
4603 /* It's very important that these two functions treat the characters
4604 between ( and ) identically. */
4606 static WORD_LIST parse_string_error;
4608 /* Take a string and run it through the shell parser, returning the
4609 resultant word list. Used by compound array assignment. */
4611 parse_string_to_word_list (s, flags, whom)
4617 int tok, orig_current_token, orig_line_number, orig_input_terminator;
4618 int orig_line_count;
4619 int old_echo_input, old_expand_aliases;
4620 #if defined (HISTORY)
4621 int old_remember_on_history, old_history_expansion_inhibited;
4624 #if defined (HISTORY)
4625 old_remember_on_history = remember_on_history;
4626 # if defined (BANG_HISTORY)
4627 old_history_expansion_inhibited = history_expansion_inhibited;
4629 bash_history_disable ();
4632 orig_line_number = line_number;
4633 orig_line_count = current_command_line_count;
4634 orig_input_terminator = shell_input_line_terminator;
4635 old_echo_input = echo_input_at_read;
4636 old_expand_aliases = expand_aliases;
4639 last_read_token = WORD; /* WORD to allow reserved words here */
4640 current_command_line_count = 0;
4641 echo_input_at_read = expand_aliases = 0;
4643 with_input_from_string (s, whom);
4644 wl = (WORD_LIST *)NULL;
4647 parser_state |= PST_COMPASSIGN;
4649 while ((tok = read_token (READ)) != yacc_EOF)
4651 if (tok == '\n' && *bash_input.location.string == '\0')
4653 if (tok == '\n') /* Allow newlines in compound assignments */
4655 if (tok != WORD && tok != ASSIGNMENT_WORD)
4657 line_number = orig_line_number + line_number - 1;
4658 orig_current_token = current_token;
4659 current_token = tok;
4660 yyerror (NULL); /* does the right thing */
4661 current_token = orig_current_token;
4664 wl = &parse_string_error;
4667 wl = make_word_list (yylval.word, wl);
4670 last_read_token = '\n';
4673 #if defined (HISTORY)
4674 remember_on_history = old_remember_on_history;
4675 # if defined (BANG_HISTORY)
4676 history_expansion_inhibited = old_history_expansion_inhibited;
4677 # endif /* BANG_HISTORY */
4678 #endif /* HISTORY */
4680 echo_input_at_read = old_echo_input;
4681 expand_aliases = old_expand_aliases;
4683 current_command_line_count = orig_line_count;
4684 shell_input_line_terminator = orig_input_terminator;
4687 parser_state &= ~PST_COMPASSIGN;
4689 if (wl == &parse_string_error)
4691 last_command_exit_value = EXECUTION_FAILURE;
4692 if (interactive_shell == 0 && posixly_correct)
4693 jump_to_top_level (FORCE_EOF);
4695 jump_to_top_level (DISCARD);
4698 return (REVERSE_LIST (wl, WORD_LIST *));
4702 parse_compound_assignment (retlenp)
4706 int tok, orig_line_number, orig_token_size, orig_last_token, assignok;
4707 char *saved_token, *ret;
4709 saved_token = token;
4710 orig_token_size = token_buffer_size;
4711 orig_line_number = line_number;
4712 orig_last_token = last_read_token;
4714 last_read_token = WORD; /* WORD to allow reserved words here */
4716 token = (char *)NULL;
4717 token_buffer_size = 0;
4719 assignok = parser_state&PST_ASSIGNOK; /* XXX */
4721 wl = (WORD_LIST *)NULL; /* ( */
4722 parser_state |= PST_COMPASSIGN;
4724 while ((tok = read_token (READ)) != ')')
4726 if (tok == '\n') /* Allow newlines in compound assignments */
4728 if (SHOULD_PROMPT ())
4732 if (tok != WORD && tok != ASSIGNMENT_WORD)
4734 current_token = tok; /* for error reporting */
4735 if (tok == yacc_EOF) /* ( */
4736 parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'"));
4738 yyerror(NULL); /* does the right thing */
4741 wl = &parse_string_error;
4744 wl = make_word_list (yylval.word, wl);
4748 token = saved_token;
4749 token_buffer_size = orig_token_size;
4751 parser_state &= ~PST_COMPASSIGN;
4753 if (wl == &parse_string_error)
4755 last_command_exit_value = EXECUTION_FAILURE;
4756 last_read_token = '\n'; /* XXX */
4757 if (interactive_shell == 0 && posixly_correct)
4758 jump_to_top_level (FORCE_EOF);
4760 jump_to_top_level (DISCARD);
4763 last_read_token = orig_last_token; /* XXX - was WORD? */
4767 rl = REVERSE_LIST (wl, WORD_LIST *);
4768 ret = string_list (rl);
4775 *retlenp = (ret && *ret) ? strlen (ret) : 0;
4778 parser_state |= PST_ASSIGNOK;
4783 /************************************************
4785 * SAVING AND RESTORING PARTIAL PARSE STATE *
4787 ************************************************/
4790 save_parser_state (ps)
4791 sh_parser_state_t *ps;
4793 #if defined (ARRAY_VARS)
4798 ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t));
4800 return ((sh_parser_state_t *)NULL);
4802 ps->parser_state = parser_state;
4803 ps->token_state = save_token_state ();
4805 ps->input_line_terminator = shell_input_line_terminator;
4806 ps->eof_encountered = eof_encountered;
4808 ps->current_command_line_count = current_command_line_count;
4810 #if defined (HISTORY)
4811 ps->remember_on_history = remember_on_history;
4812 # if defined (BANG_HISTORY)
4813 ps->history_expansion_inhibited = history_expansion_inhibited;
4817 ps->last_command_exit_value = last_command_exit_value;
4818 #if defined (ARRAY_VARS)
4819 v = find_variable ("PIPESTATUS");
4820 if (v && array_p (v) && array_cell (v))
4821 ps->pipestatus = array_copy (array_cell (v));
4823 ps->pipestatus = (ARRAY *)NULL;
4826 ps->last_shell_builtin = last_shell_builtin;
4827 ps->this_shell_builtin = this_shell_builtin;
4829 ps->expand_aliases = expand_aliases;
4830 ps->echo_input_at_read = echo_input_at_read;
4836 restore_parser_state (ps)
4837 sh_parser_state_t *ps;
4839 #if defined (ARRAY_VARS)
4846 parser_state = ps->parser_state;
4847 if (ps->token_state)
4849 restore_token_state (ps->token_state);
4850 free (ps->token_state);
4853 shell_input_line_terminator = ps->input_line_terminator;
4854 eof_encountered = ps->eof_encountered;
4856 current_command_line_count = ps->current_command_line_count;
4858 #if defined (HISTORY)
4859 remember_on_history = ps->remember_on_history;
4860 # if defined (BANG_HISTORY)
4861 history_expansion_inhibited = ps->history_expansion_inhibited;
4865 last_command_exit_value = ps->last_command_exit_value;
4866 #if defined (ARRAY_VARS)
4867 v = find_variable ("PIPESTATUS");
4868 if (v && array_p (v) && array_cell (v))
4870 array_dispose (array_cell (v));
4871 var_setarray (v, ps->pipestatus);
4875 last_shell_builtin = ps->last_shell_builtin;
4876 this_shell_builtin = ps->this_shell_builtin;
4878 expand_aliases = ps->expand_aliases;
4879 echo_input_at_read = ps->echo_input_at_read;
4882 /************************************************
4884 * MULTIBYTE CHARACTER HANDLING *
4886 ************************************************/
4888 #if defined (HANDLE_MULTIBYTE)
4892 int i, previ, len, c;
4893 mbstate_t mbs, prevs;
4896 if (shell_input_line == NULL)
4898 len = strlen (shell_input_line); /* XXX - shell_input_line_len ? */
4899 FREE (shell_input_line_property);
4900 shell_input_line_property = (char *)xmalloc (len + 1);
4902 memset (&prevs, '\0', sizeof (mbstate_t));
4903 for (i = previ = 0; i < len; i++)
4907 c = shell_input_line[i];
4911 for (j = i; j < len; j++)
4912 shell_input_line_property[j] = 1;
4916 mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs);
4917 if (mbclen == 1 || mbclen == (size_t)-1)
4922 else if (mbclen == (size_t)-2)
4924 else if (mbclen > 1)
4932 /* XXX - what to do if mbrlen returns 0? (null wide character) */
4934 for (j = i; j < len; j++)
4935 shell_input_line_property[j] = 1;
4939 shell_input_line_property[i] = mbclen;
4942 #endif /* HANDLE_MULTIBYTE */