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 (HANDLE_MULTIBYTE)
209 static void set_line_mbstate __P
((void));
210 static char *shell_input_line_property
= NULL
;
212 # define set_line_mbstate()
215 extern
int yyerror __P
((const char *));
221 /* Default prompt strings */
222 char *primary_prompt
= PPROMPT
;
223 char *secondary_prompt
= SPROMPT
;
225 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
226 char *ps1_prompt
, *ps2_prompt
;
228 /* Handle on the current prompt string. Indirectly points through
229 ps1_ or ps2_prompt. */
230 char **prompt_string_pointer
= (char **)NULL
;
231 char *current_prompt_string
;
233 /* Non-zero means we expand aliases in commands. */
234 int expand_aliases
= 0;
236 /* If non-zero, the decoded prompt string undergoes parameter and
237 variable substitution, command substitution, arithmetic substitution,
238 string expansion, process substitution, and quote removal in
239 decode_prompt_string. */
242 /* If non-zero, $'...' and $"..." are expanded when they appear within
243 a ${...} expansion, even when the expansion appears within double
245 int extended_quote
= 1;
247 /* The decoded prompt string. Used if READLINE is not defined or if
248 editing is turned off. Analogous to current_readline_prompt. */
249 static char *current_decoded_prompt
;
251 /* The number of lines read from input while creating the current command. */
252 int current_command_line_count
;
254 /* Variables to manage the task of reading here documents, because we need to
255 defer the reading until after a complete command has been collected. */
256 static REDIRECT
*redir_stack
[10];
259 /* Where shell input comes from. History expansion is performed on each
260 line when the shell is interactive. */
261 static char *shell_input_line
= (char *)NULL
;
262 static int shell_input_line_index
;
263 static int shell_input_line_size
; /* Amount allocated for shell_input_line. */
264 static int shell_input_line_len
; /* strlen (shell_input_line) */
266 /* Either zero or EOF. */
267 static int shell_input_line_terminator
;
269 /* The line number in a script on which a function definition starts. */
270 static int function_dstart
;
272 /* The line number in a script on which a function body starts. */
273 static int function_bstart
;
275 /* The line number in a script at which an arithmetic for command starts. */
276 static int arith_for_lineno
;
278 /* The line number in a script where the word in a `case WORD', `select WORD'
279 or `for WORD' begins. This is a nested command maximum, since the array
280 index is decremented after a case, select, or for command is parsed. */
281 #define MAX_CASE_NEST 128
282 static int word_lineno
[MAX_CASE_NEST
];
283 static int word_top
= -1;
285 /* If non-zero, it is the token that we want read_token to return
286 regardless of what text is (or isn't) present to be read. This
287 is reset by read_token. If token_to_read == WORD or
288 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
289 static int token_to_read
;
290 static WORD_DESC
*word_desc_to_read
;
292 static REDIRECTEE redir
;
296 WORD_DESC
*word
; /* the word that we read. */
297 int number
; /* the number that we read. */
298 WORD_LIST
*word_list
;
302 PATTERN_LIST
*pattern
;
305 /* Reserved words. Members of the first group are only recognized
306 in the case that they are preceded by a list_terminator. Members
307 of the second group are for [[...]] commands. Members of the
308 third group are recognized only under special circumstances. */
309 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION
310 %token COND_START COND_END COND_ERROR
311 %token IN BANG TIME TIMEOPT
313 /* More general tokens. yylex () knows how to make these. */
314 %token
<word
> WORD ASSIGNMENT_WORD
315 %token
<number
> NUMBER
316 %token
<word_list
> ARITH_CMD ARITH_FOR_EXPRS
317 %token
<command
> COND_CMD
318 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS
319 %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
322 /* The types that the various syntactical units return. */
324 %type
<command
> inputunit command pipeline pipeline_command
325 %type
<command
> list list0 list1 compound_list simple_list simple_list1
326 %type
<command
> simple_command shell_command
327 %type
<command
> for_command select_command case_command group_command
328 %type
<command
> arith_command
329 %type
<command
> cond_command
330 %type
<command
> arith_for_command
331 %type
<command
> function_def function_body if_command elif_clause subshell
332 %type
<redirect
> redirection redirection_list
333 %type
<element
> simple_command_element
334 %type
<word_list
> word_list pattern
335 %type
<pattern
> pattern_list case_clause_sequence case_clause
336 %type
<number
> timespec
337 %type
<number
> list_terminator
341 %left
'&' ';' '\n' yacc_EOF
346 inputunit: simple_list simple_list_terminator
348 /* Case of regular command. Discard the error
349 safety net,and return the command just parsed. */
352 /* discard_parser_constructs (0); */
357 /* Case of regular command, but not a very
358 interesting one. Return a NULL command. */
359 global_command
= (COMMAND
*)NULL
;
364 /* Error during parsing. Return NULL command. */
365 global_command
= (COMMAND
*)NULL
;
367 /* discard_parser_constructs (1); */
379 /* Case of EOF seen by itself. Do ignoreeof or
381 global_command
= (COMMAND
*)NULL
;
382 handle_eof_input_unit
();
388 { $$
= make_word_list
($1, (WORD_LIST
*)NULL
); }
390 { $$
= make_word_list
($2, $1); }
393 redirection: '>' WORD
396 $$
= make_redirection
(1, r_output_direction
, redir
);
401 $$
= make_redirection
(0, r_input_direction
, redir
);
406 $$
= make_redirection
($1, r_output_direction
, redir
);
411 $$
= make_redirection
($1, r_input_direction
, redir
);
413 | GREATER_GREATER WORD
416 $$
= make_redirection
(1, r_appending_to
, redir
);
418 | NUMBER GREATER_GREATER WORD
421 $$
= make_redirection
($1, r_appending_to
, redir
);
426 $$
= make_redirection
(0, r_reading_until
, redir
);
427 redir_stack
[need_here_doc
++] = $$
;
429 | NUMBER LESS_LESS WORD
432 $$
= make_redirection
($1, r_reading_until
, redir
);
433 redir_stack
[need_here_doc
++] = $$
;
435 | LESS_LESS_LESS WORD
438 $$
= make_redirection
(0, r_reading_string
, redir
);
440 | NUMBER LESS_LESS_LESS WORD
443 $$
= make_redirection
($1, r_reading_string
, redir
);
448 $$
= make_redirection
(0, r_duplicating_input
, redir
);
450 | NUMBER LESS_AND NUMBER
453 $$
= make_redirection
($1, r_duplicating_input
, redir
);
458 $$
= make_redirection
(1, r_duplicating_output
, redir
);
460 | NUMBER GREATER_AND NUMBER
463 $$
= make_redirection
($1, r_duplicating_output
, redir
);
468 $$
= make_redirection
(0, r_duplicating_input_word
, redir
);
470 | NUMBER LESS_AND WORD
473 $$
= make_redirection
($1, r_duplicating_input_word
, redir
);
478 $$
= make_redirection
(1, r_duplicating_output_word
, redir
);
480 | NUMBER GREATER_AND WORD
483 $$
= make_redirection
($1, r_duplicating_output_word
, redir
);
485 | LESS_LESS_MINUS WORD
488 $$
= make_redirection
489 (0, r_deblank_reading_until
, redir
);
490 redir_stack
[need_here_doc
++] = $$
;
492 | NUMBER LESS_LESS_MINUS WORD
495 $$
= make_redirection
496 ($1, r_deblank_reading_until
, redir
);
497 redir_stack
[need_here_doc
++] = $$
;
502 $$
= make_redirection
(1, r_close_this
, redir
);
504 | NUMBER GREATER_AND
'-'
507 $$
= make_redirection
($1, r_close_this
, redir
);
512 $$
= make_redirection
(0, r_close_this
, redir
);
514 | NUMBER LESS_AND
'-'
517 $$
= make_redirection
($1, r_close_this
, redir
);
522 $$
= make_redirection
(1, r_err_and_out
, redir
);
524 | NUMBER LESS_GREATER WORD
527 $$
= make_redirection
($1, r_input_output
, redir
);
532 $$
= make_redirection
(0, r_input_output
, redir
);
537 $$
= make_redirection
(1, r_output_force
, redir
);
539 | NUMBER GREATER_BAR WORD
542 $$
= make_redirection
($1, r_output_force
, redir
);
546 simple_command_element: WORD
547 { $$.word
= $1; $$.redirect
= 0; }
549 { $$.word
= $1; $$.redirect
= 0; }
551 { $$.redirect
= $1; $$.word
= 0; }
554 redirection_list: redirection
558 | redirection_list redirection
560 register REDIRECT
*t
;
562 for
(t
= $1; t
->next
; t
= t
->next
)
569 simple_command: simple_command_element
570 { $$
= make_simple_command
($1, (COMMAND
*)NULL
); }
571 | simple_command simple_command_element
572 { $$
= make_simple_command
($2, $1); }
575 command: simple_command
576 { $$
= clean_simple_command
($1); }
579 | shell_command redirection_list
586 register REDIRECT
*t
;
587 for
(t
= tc
->redirects
; t
->next
; t
= t
->next
)
599 shell_command: for_command
603 | WHILE compound_list DO compound_list DONE
604 { $$
= make_while_command
($2, $4); }
605 | UNTIL compound_list DO compound_list DONE
606 { $$
= make_until_command
($2, $4); }
623 for_command: FOR WORD newline_list DO compound_list DONE
625 $$
= make_for_command
($2, add_string_to_list
("\"$@\"", (WORD_LIST
*)NULL
), $5, word_lineno
[word_top
]);
626 if
(word_top
> 0) word_top
--;
628 | FOR WORD newline_list
'{' compound_list
'}'
630 $$
= make_for_command
($2, add_string_to_list
("\"$@\"", (WORD_LIST
*)NULL
), $5, word_lineno
[word_top
]);
631 if
(word_top
> 0) word_top
--;
633 | FOR WORD
';' newline_list DO compound_list DONE
635 $$
= make_for_command
($2, add_string_to_list
("\"$@\"", (WORD_LIST
*)NULL
), $6, word_lineno
[word_top
]);
636 if
(word_top
> 0) word_top
--;
638 | FOR WORD
';' newline_list
'{' compound_list
'}'
640 $$
= make_for_command
($2, add_string_to_list
("\"$@\"", (WORD_LIST
*)NULL
), $6, word_lineno
[word_top
]);
641 if
(word_top
> 0) word_top
--;
643 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
645 $$
= make_for_command
($2, REVERSE_LIST
($5, WORD_LIST
*), $9, word_lineno
[word_top
]);
646 if
(word_top
> 0) word_top
--;
648 | FOR WORD newline_list IN word_list list_terminator newline_list
'{' compound_list
'}'
650 $$
= make_for_command
($2, REVERSE_LIST
($5, WORD_LIST
*), $9, word_lineno
[word_top
]);
651 if
(word_top
> 0) word_top
--;
653 | FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
655 $$
= make_for_command
($2, (WORD_LIST
*)NULL
, $8, word_lineno
[word_top
]);
656 if
(word_top
> 0) word_top
--;
658 | FOR WORD newline_list IN list_terminator newline_list
'{' compound_list
'}'
660 $$
= make_for_command
($2, (WORD_LIST
*)NULL
, $8, word_lineno
[word_top
]);
661 if
(word_top
> 0) word_top
--;
665 arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
667 $$
= make_arith_for_command
($2, $6, arith_for_lineno
);
668 if
(word_top
> 0) word_top
--;
670 | FOR ARITH_FOR_EXPRS list_terminator newline_list
'{' compound_list
'}'
672 $$
= make_arith_for_command
($2, $6, arith_for_lineno
);
673 if
(word_top
> 0) word_top
--;
675 | FOR ARITH_FOR_EXPRS DO compound_list DONE
677 $$
= make_arith_for_command
($2, $4, arith_for_lineno
);
678 if
(word_top
> 0) word_top
--;
680 | FOR ARITH_FOR_EXPRS
'{' compound_list
'}'
682 $$
= make_arith_for_command
($2, $4, arith_for_lineno
);
683 if
(word_top
> 0) word_top
--;
687 select_command: SELECT WORD newline_list DO list DONE
689 $$
= make_select_command
($2, add_string_to_list
("\"$@\"", (WORD_LIST
*)NULL
), $5, word_lineno
[word_top
]);
690 if
(word_top
> 0) word_top
--;
692 | SELECT WORD newline_list
'{' list
'}'
694 $$
= make_select_command
($2, add_string_to_list
("\"$@\"", (WORD_LIST
*)NULL
), $5, word_lineno
[word_top
]);
695 if
(word_top
> 0) word_top
--;
697 | SELECT WORD
';' newline_list DO list DONE
699 $$
= make_select_command
($2, add_string_to_list
("\"$@\"", (WORD_LIST
*)NULL
), $6, word_lineno
[word_top
]);
700 if
(word_top
> 0) word_top
--;
702 | SELECT WORD
';' newline_list
'{' list
'}'
704 $$
= make_select_command
($2, add_string_to_list
("\"$@\"", (WORD_LIST
*)NULL
), $6, word_lineno
[word_top
]);
705 if
(word_top
> 0) word_top
--;
707 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
709 $$
= make_select_command
($2, REVERSE_LIST
($5, WORD_LIST
*), $9, word_lineno
[word_top
]);
710 if
(word_top
> 0) word_top
--;
712 | SELECT WORD newline_list IN word_list list_terminator newline_list
'{' list
'}'
714 $$
= make_select_command
($2, REVERSE_LIST
($5, WORD_LIST
*), $9, word_lineno
[word_top
]);
715 if
(word_top
> 0) word_top
--;
719 case_command: CASE WORD newline_list IN newline_list ESAC
721 $$
= make_case_command
($2, (PATTERN_LIST
*)NULL
, word_lineno
[word_top
]);
722 if
(word_top
> 0) word_top
--;
724 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
726 $$
= make_case_command
($2, $5, word_lineno
[word_top
]);
727 if
(word_top
> 0) word_top
--;
729 | CASE WORD newline_list IN case_clause ESAC
731 $$
= make_case_command
($2, $5, word_lineno
[word_top
]);
732 if
(word_top
> 0) word_top
--;
736 function_def: WORD
'(' ')' newline_list function_body
737 { $$
= make_function_def
($1, $5, function_dstart
, function_bstart
); }
739 | FUNCTION WORD
'(' ')' newline_list function_body
740 { $$
= make_function_def
($2, $6, function_dstart
, function_bstart
); }
742 | FUNCTION WORD newline_list function_body
743 { $$
= make_function_def
($2, $4, function_dstart
, function_bstart
); }
747 function_body: shell_command
749 | shell_command redirection_list
754 /* According to Posix.2 3.9.5, redirections
755 specified after the body of a function should
756 be attached to the function and performed when
757 the function is executed, not as part of the
758 function definition command. */
759 /* XXX - I don't think it matters, but we might
760 want to change this in the future to avoid
761 problems differentiating between a function
762 definition with a redirection and a function
763 definition containing a single command with a
764 redirection. The two are semantically equivalent,
765 though -- the only difference is in how the
766 command printing code displays the redirections. */
769 register REDIRECT
*t
;
770 for
(t
= tc
->redirects
; t
->next
; t
= t
->next
)
780 subshell: '(' compound_list
')'
782 $$
= make_subshell_command
($2);
783 $$
->flags |
= CMD_WANT_SUBSHELL
;
787 if_command: IF compound_list THEN compound_list FI
788 { $$
= make_if_command
($2, $4, (COMMAND
*)NULL
); }
789 | IF compound_list THEN compound_list ELSE compound_list FI
790 { $$
= make_if_command
($2, $4, $6); }
791 | IF compound_list THEN compound_list elif_clause FI
792 { $$
= make_if_command
($2, $4, $5); }
796 group_command: '{' compound_list
'}'
797 { $$
= make_group_command
($2); }
800 arith_command: ARITH_CMD
801 { $$
= make_arith_command
($1); }
804 cond_command: COND_START COND_CMD COND_END
808 elif_clause: ELIF compound_list THEN compound_list
809 { $$
= make_if_command
($2, $4, (COMMAND
*)NULL
); }
810 | ELIF compound_list THEN compound_list ELSE compound_list
811 { $$
= make_if_command
($2, $4, $6); }
812 | ELIF compound_list THEN compound_list elif_clause
813 { $$
= make_if_command
($2, $4, $5); }
816 case_clause: pattern_list
817 | case_clause_sequence pattern_list
818 { $2->next
= $1; $$
= $2; }
821 pattern_list: newline_list pattern
')' compound_list
822 { $$
= make_pattern_list
($2, $4); }
823 | newline_list pattern
')' newline_list
824 { $$
= make_pattern_list
($2, (COMMAND
*)NULL
); }
825 | newline_list
'(' pattern
')' compound_list
826 { $$
= make_pattern_list
($3, $5); }
827 | newline_list
'(' pattern
')' newline_list
828 { $$
= make_pattern_list
($3, (COMMAND
*)NULL
); }
831 case_clause_sequence: pattern_list SEMI_SEMI
832 | case_clause_sequence pattern_list SEMI_SEMI
833 { $2->next
= $1; $$
= $2; }
837 { $$
= make_word_list
($1, (WORD_LIST
*)NULL
); }
839 { $$
= make_word_list
($3, $1); }
842 /* A list allows leading or trailing newlines and
843 newlines as operators (equivalent to semicolons).
844 It must end with a newline or semicolon.
845 Lists are used within commands such as if, for, while. */
847 list: newline_list list0
851 gather_here_documents
();
862 list0: list1
'\n' newline_list
863 | list1
'&' newline_list
865 if
($1->type
== cm_connection
)
866 $$
= connect_async_list
($1, (COMMAND
*)NULL
, '&');
868 $$
= command_connect
($1, (COMMAND
*)NULL
, '&');
870 | list1
';' newline_list
874 list1: list1 AND_AND newline_list list1
875 { $$
= command_connect
($1, $4, AND_AND
); }
876 | list1 OR_OR newline_list list1
877 { $$
= command_connect
($1, $4, OR_OR
); }
878 | list1
'&' newline_list list1
880 if
($1->type
== cm_connection
)
881 $$
= connect_async_list
($1, $4, '&');
883 $$
= command_connect
($1, $4, '&');
885 | list1
';' newline_list list1
886 { $$
= command_connect
($1, $4, ';'); }
887 | list1
'\n' newline_list list1
888 { $$
= command_connect
($1, $4, ';'); }
893 simple_list_terminator: '\n'
909 /* A simple_list is a list that contains no significant newlines
910 and no leading or trailing newlines. Newlines are allowed
911 only following operators, where they are not significant.
913 This is what an inputunit consists of. */
915 simple_list: simple_list1
919 gather_here_documents
();
923 if
($1->type
== cm_connection
)
924 $$
= connect_async_list
($1, (COMMAND
*)NULL
, '&');
926 $$
= command_connect
($1, (COMMAND
*)NULL
, '&');
928 gather_here_documents
();
934 gather_here_documents
();
938 simple_list1: simple_list1 AND_AND newline_list simple_list1
939 { $$
= command_connect
($1, $4, AND_AND
); }
940 | simple_list1 OR_OR newline_list simple_list1
941 { $$
= command_connect
($1, $4, OR_OR
); }
942 | simple_list1
'&' simple_list1
944 if
($1->type
== cm_connection
)
945 $$
= connect_async_list
($1, $3, '&');
947 $$
= command_connect
($1, $3, '&');
949 | simple_list1
';' simple_list1
950 { $$
= command_connect
($1, $3, ';'); }
956 pipeline_command: pipeline
961 $2->flags |
= CMD_INVERT_RETURN
;
970 | timespec BANG pipeline
973 $3->flags |
= $1|CMD_INVERT_RETURN
;
976 | BANG timespec pipeline
979 $3->flags |
= $2|CMD_INVERT_RETURN
;
982 | timespec list_terminator
986 /* Boy, this is unclean. `time' by itself can
987 time a null command. We cheat and push a
988 newline back if the list_terminator was a newline
989 to avoid the double-newline problem (one to
990 terminate this, one to terminate the command) */
993 $$
= make_simple_command
(x
, (COMMAND
*)NULL
);
995 /* XXX - let's cheat and push a newline back */
997 token_to_read
= '\n';
1003 pipeline
'|' newline_list pipeline
1004 { $$
= command_connect
($1, $4, '|'); }
1010 { $$
= CMD_TIME_PIPELINE
; }
1012 { $$
= CMD_TIME_PIPELINE|CMD_TIME_POSIX
; }
1016 /* Possible states for the parser that require it to do special things. */
1017 #define PST_CASEPAT 0x0001 /* in a case pattern list */
1018 #define PST_ALEXPNEXT 0x0002 /* expand next word for aliases */
1019 #define PST_ALLOWOPNBRC 0x0004 /* allow open brace for function def */
1020 #define PST_NEEDCLOSBRC 0x0008 /* need close brace */
1021 #define PST_DBLPAREN 0x0010 /* double-paren parsing */
1022 #define PST_SUBSHELL 0x0020 /* ( ... ) subshell */
1023 #define PST_CMDSUBST 0x0040 /* $( ... ) command substitution */
1024 #define PST_CASESTMT 0x0080 /* parsing a case statement */
1025 #define PST_CONDCMD 0x0100 /* parsing a [[...]] command */
1026 #define PST_CONDEXPR 0x0200 /* parsing the guts of [[...]] */
1027 #define PST_ARITHFOR 0x0400 /* parsing an arithmetic for command */
1028 #define PST_ALEXPAND 0x0800 /* OK to expand aliases - unused */
1029 #define PST_CMDTOKEN 0x1000 /* command token OK - unused */
1030 #define PST_COMPASSIGN 0x2000 /* parsing x=(...) compound assignment */
1031 #define PST_ASSIGNOK 0x4000 /* assignment statement ok in this context */
1032 #define PST_REGEXP 0x8000 /* parsing an ERE/BRE as a single word */
1034 /* Initial size to allocate for tokens, and the
1035 amount to grow them by. */
1036 #define TOKEN_DEFAULT_INITIAL_SIZE 496
1037 #define TOKEN_DEFAULT_GROW_SIZE 512
1039 /* Should we call prompt_again? */
1040 #define SHOULD_PROMPT() \
1041 (interactive
&& (bash_input.type
== st_stdin || bash_input.type
== st_stream
))
1044 # define expanding_alias() (pushed_string_list && pushed_string_list->expander)
1046 # define expanding_alias() 0
1049 /* The token currently being read. */
1050 static int current_token
;
1052 /* The last read token, or NULL. read_token () uses this for context
1054 static int last_read_token
;
1056 /* The token read prior to last_read_token. */
1057 static int token_before_that
;
1059 /* The token read prior to token_before_that. */
1060 static int two_tokens_ago
;
1062 /* The current parser state. */
1063 static int parser_state
;
1065 /* Global var is non-zero when end of file has been reached. */
1066 int EOF_Reached
= 0;
1079 /* yy_getc () returns the next available character from input or EOF.
1080 yy_ungetc (c) makes `c' the next character to read.
1081 init_yy_io (get, unget, type, location) makes the function GET the
1082 installed function for getting the next character, makes UNGET the
1083 installed function for un-getting a character, sets the type of stream
1084 (either string or file) from TYPE, and makes LOCATION point to where
1085 the input is coming from. */
1087 /* Unconditionally returns end-of-file. */
1094 /* Variable containing the current get and unget functions.
1095 See ./input.h for a clearer description. */
1096 BASH_INPUT bash_input
;
1098 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
1099 is non-null, avoiding a memory leak. */
1101 initialize_bash_input
()
1103 bash_input.type
= st_none
;
1104 FREE
(bash_input.name
);
1105 bash_input.name
= (char *)NULL
;
1106 bash_input.location.file
= (FILE *)NULL
;
1107 bash_input.location.
string = (char *)NULL
;
1108 bash_input.getter
= (sh_cget_func_t
*)NULL
;
1109 bash_input.ungetter
= (sh_cunget_func_t
*)NULL
;
1112 /* Set the contents of the current bash input stream from
1113 GET, UNGET, TYPE, NAME, and LOCATION. */
1115 init_yy_io
(get
, unget
, type
, name
, location
)
1116 sh_cget_func_t
*get
;
1117 sh_cunget_func_t
*unget
;
1118 enum stream_type type
;
1120 INPUT_STREAM location
;
1122 bash_input.type
= type
;
1123 FREE
(bash_input.name
);
1124 bash_input.name
= name ? savestring
(name
) : (char *)NULL
;
1128 memcpy
((char *)&bash_input.location.
string, (char *)&location.
string, sizeof
(location
));
1130 bash_input.location
= location
;
1132 bash_input.getter
= get
;
1133 bash_input.ungetter
= unget
;
1139 return
(bash_input.name ? bash_input.name
: "stdin");
1142 /* Call this to get the next character of input. */
1146 return
(*(bash_input.getter
)) ();
1149 /* Call this to unget C. That is, to make C the next character
1155 return
(*(bash_input.ungetter
)) (c
);
1158 #if defined (BUFFERED_INPUT)
1159 #ifdef INCLUDE_UNUSED
1161 input_file_descriptor
()
1163 switch
(bash_input.type
)
1166 return
(fileno
(bash_input.location.file
));
1168 return
(bash_input.location.buffered_fd
);
1171 return
(fileno
(stdin
));
1175 #endif /* BUFFERED_INPUT */
1177 /* **************************************************************** */
1179 /* Let input be read from readline (). */
1181 /* **************************************************************** */
1183 #if defined (READLINE)
1184 char *current_readline_prompt
= (char *)NULL
;
1185 char *current_readline_line
= (char *)NULL
;
1186 int current_readline_line_index
= 0;
1191 SigHandler
*old_sigint
;
1195 if
(!current_readline_line
)
1197 if
(!bash_readline_initialized
)
1198 initialize_readline
();
1200 #if defined (JOB_CONTROL)
1202 give_terminal_to
(shell_pgrp
, 0);
1203 #endif /* JOB_CONTROL */
1205 old_sigint
= (SigHandler
*)NULL
;
1206 if
(signal_is_ignored
(SIGINT
) == 0)
1208 old_sigint
= (SigHandler
*)set_signal_handler
(SIGINT
, sigint_sighandler
);
1209 interrupt_immediately
++;
1211 terminate_immediately
= 1;
1213 current_readline_line
= readline
(current_readline_prompt ?
1214 current_readline_prompt
: "");
1216 terminate_immediately
= 0;
1217 if
(signal_is_ignored
(SIGINT
) == 0 && old_sigint
)
1219 interrupt_immediately
--;
1220 set_signal_handler
(SIGINT
, old_sigint
);
1224 /* Reset the prompt to the decoded value of prompt_string_pointer. */
1225 reset_readline_prompt
();
1228 if
(current_readline_line
== 0)
1231 current_readline_line_index
= 0;
1232 line_len
= strlen
(current_readline_line
);
1234 current_readline_line
= (char *)xrealloc
(current_readline_line
, 2 + line_len
);
1235 current_readline_line
[line_len
++] = '\n';
1236 current_readline_line
[line_len
] = '\0';
1239 if
(current_readline_line
[current_readline_line_index
] == 0)
1241 free
(current_readline_line
);
1242 current_readline_line
= (char *)NULL
;
1243 return
(yy_readline_get
());
1247 c
= current_readline_line
[current_readline_line_index
++];
1253 yy_readline_unget
(c
)
1256 if
(current_readline_line_index
&& current_readline_line
)
1257 current_readline_line
[--current_readline_line_index
] = c
;
1262 with_input_from_stdin
()
1264 INPUT_STREAM location
;
1266 if
(bash_input.type
!= st_stdin
&& stream_on_stack
(st_stdin
) == 0)
1268 location.
string = current_readline_line
;
1269 init_yy_io
(yy_readline_get
, yy_readline_unget
,
1270 st_stdin
, "readline stdin", location
);
1274 #else /* !READLINE */
1277 with_input_from_stdin
()
1279 with_input_from_stream
(stdin
, "stdin");
1281 #endif /* !READLINE */
1283 /* **************************************************************** */
1285 /* Let input come from STRING. STRING is zero terminated. */
1287 /* **************************************************************** */
1292 register
char *string;
1293 register
unsigned char c
;
1295 string = bash_input.location.
string;
1297 /* If the string doesn't exist, or is empty, EOF found. */
1298 if
(string && *string)
1301 bash_input.location.
string = string;
1312 *(--bash_input.location.
string) = c
;
1317 with_input_from_string
(string, name
)
1321 INPUT_STREAM location
;
1323 location.
string = string;
1324 init_yy_io
(yy_string_get
, yy_string_unget
, st_string
, name
, location
);
1327 /* **************************************************************** */
1329 /* Let input come from STREAM. */
1331 /* **************************************************************** */
1333 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
1334 define, and just use getc/ungetc if it was defined, but since bash
1335 installs its signal handlers without the SA_RESTART flag, some signals
1336 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
1337 the read to be restarted. We need to restart it ourselves. */
1345 if
(bash_input.location.file
)
1349 interrupt_immediately
++;
1350 terminate_immediately
++;
1352 result
= getc_with_restart
(bash_input.location.file
);
1355 interrupt_immediately
--;
1356 terminate_immediately
--;
1366 return
(ungetc_with_restart
(c
, bash_input.location.file
));
1370 with_input_from_stream
(stream
, name
)
1374 INPUT_STREAM location
;
1376 location.file
= stream
;
1377 init_yy_io
(yy_stream_get
, yy_stream_unget
, st_stream
, name
, location
);
1380 typedef
struct stream_saver
{
1381 struct stream_saver
*next
;
1382 BASH_INPUT bash_input
;
1384 #if defined (BUFFERED_INPUT)
1385 BUFFERED_STREAM
*bstream
;
1386 #endif /* BUFFERED_INPUT */
1389 /* The globally known line number. */
1390 int line_number
= 0;
1392 #if defined (COND_COMMAND)
1393 static int cond_lineno
;
1394 static int cond_token
;
1397 STREAM_SAVER
*stream_list
= (STREAM_SAVER
*)NULL
;
1400 push_stream
(reset_lineno
)
1403 STREAM_SAVER
*saver
= (STREAM_SAVER
*)xmalloc
(sizeof
(STREAM_SAVER
));
1405 xbcopy
((char *)&bash_input
, (char *)&(saver
->bash_input
), sizeof
(BASH_INPUT
));
1407 #if defined (BUFFERED_INPUT)
1408 saver
->bstream
= (BUFFERED_STREAM
*)NULL
;
1409 /* If we have a buffered stream, clear out buffers[fd]. */
1410 if
(bash_input.type
== st_bstream
&& bash_input.location.buffered_fd
>= 0)
1411 saver
->bstream
= set_buffered_stream
(bash_input.location.buffered_fd
,
1412 (BUFFERED_STREAM
*)NULL
);
1413 #endif /* BUFFERED_INPUT */
1415 saver
->line
= line_number
;
1416 bash_input.name
= (char *)NULL
;
1417 saver
->next
= stream_list
;
1418 stream_list
= saver
;
1431 STREAM_SAVER
*saver
= stream_list
;
1434 stream_list
= stream_list
->next
;
1436 init_yy_io
(saver
->bash_input.getter
,
1437 saver
->bash_input.ungetter
,
1438 saver
->bash_input.type
,
1439 saver
->bash_input.name
,
1440 saver
->bash_input.location
);
1442 #if defined (BUFFERED_INPUT)
1443 /* If we have a buffered stream, restore buffers[fd]. */
1444 /* If the input file descriptor was changed while this was on the
1445 save stack, update the buffered fd to the new file descriptor and
1446 re-establish the buffer <-> bash_input fd correspondence. */
1447 if
(bash_input.type
== st_bstream
&& bash_input.location.buffered_fd
>= 0)
1449 if
(bash_input_fd_changed
)
1451 bash_input_fd_changed
= 0;
1452 if
(default_buffered_input
>= 0)
1454 bash_input.location.buffered_fd
= default_buffered_input
;
1455 saver
->bstream
->b_fd
= default_buffered_input
;
1456 SET_CLOSE_ON_EXEC
(default_buffered_input
);
1459 /* XXX could free buffered stream returned as result here. */
1460 set_buffered_stream
(bash_input.location.buffered_fd
, saver
->bstream
);
1462 #endif /* BUFFERED_INPUT */
1464 line_number
= saver
->line
;
1466 FREE
(saver
->bash_input.name
);
1471 /* Return 1 if a stream of type TYPE is saved on the stack. */
1473 stream_on_stack
(type
)
1474 enum stream_type type
;
1476 register STREAM_SAVER
*s
;
1478 for
(s
= stream_list
; s
; s
= s
->next
)
1479 if
(s
->bash_input.type
== type
)
1484 /* Save the current token state and return it in a malloced array. */
1490 ret
= (int *)xmalloc
(3 * sizeof
(int));
1491 ret
[0] = last_read_token
;
1492 ret
[1] = token_before_that
;
1493 ret
[2] = two_tokens_ago
;
1498 restore_token_state
(ts
)
1503 last_read_token
= ts
[0];
1504 token_before_that
= ts
[1];
1505 two_tokens_ago
= ts
[2];
1509 * This is used to inhibit alias expansion and reserved word recognition
1510 * inside case statement pattern lists. A `case statement pattern list' is:
1512 * everything between the `in' in a `case word in' and the next ')'
1514 * everything between a `;;' and the next `)' or `esac'
1517 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1519 #define END_OF_ALIAS 0
1522 * Pseudo-global variables used in implementing token-wise alias expansion.
1526 * Pushing and popping strings. This works together with shell_getc to
1527 * implement alias expansion on a per-token basis.
1530 typedef
struct string_saver
{
1531 struct string_saver
*next
;
1532 int expand_alias
; /* Value to set expand_alias to when string is popped. */
1535 alias_t
*expander
; /* alias that caused this line to be pushed. */
1537 int saved_line_size
, saved_line_index
, saved_line_terminator
;
1540 STRING_SAVER
*pushed_string_list
= (STRING_SAVER
*)NULL
;
1543 * Push the current shell_input_line onto a stack of such lines and make S
1544 * the current input. Used when expanding aliases. EXPAND is used to set
1545 * the value of expand_next_token when the string is popped, so that the
1546 * word after the alias in the original line is handled correctly when the
1547 * alias expands to multiple words. TOKEN is the token that was expanded
1548 * into S; it is saved and used to prevent infinite recursive expansion.
1551 push_string
(s
, expand
, ap
)
1556 STRING_SAVER
*temp
= (STRING_SAVER
*)xmalloc
(sizeof
(STRING_SAVER
));
1558 temp
->expand_alias
= expand
;
1559 temp
->saved_line
= shell_input_line
;
1560 temp
->saved_line_size
= shell_input_line_size
;
1561 temp
->saved_line_index
= shell_input_line_index
;
1562 temp
->saved_line_terminator
= shell_input_line_terminator
;
1564 temp
->expander
= ap
;
1566 temp
->next
= pushed_string_list
;
1567 pushed_string_list
= temp
;
1571 ap
->flags |
= AL_BEINGEXPANDED
;
1574 shell_input_line
= s
;
1575 shell_input_line_size
= strlen
(s
);
1576 shell_input_line_index
= 0;
1577 shell_input_line_terminator
= '\0';
1579 parser_state
&= ~PST_ALEXPNEXT
; /* XXX */
1582 set_line_mbstate
();
1586 * Make the top of the pushed_string stack be the current shell input.
1587 * Only called when there is something on the stack. Called from shell_getc
1588 * when it thinks it has consumed the string generated by an alias expansion
1589 * and needs to return to the original input line.
1596 FREE
(shell_input_line
);
1597 shell_input_line
= pushed_string_list
->saved_line
;
1598 shell_input_line_index
= pushed_string_list
->saved_line_index
;
1599 shell_input_line_size
= pushed_string_list
->saved_line_size
;
1600 shell_input_line_terminator
= pushed_string_list
->saved_line_terminator
;
1602 if
(pushed_string_list
->expand_alias
)
1603 parser_state |
= PST_ALEXPNEXT
;
1605 parser_state
&= ~PST_ALEXPNEXT
;
1607 t
= pushed_string_list
;
1608 pushed_string_list
= pushed_string_list
->next
;
1612 t
->expander
->flags
&= ~AL_BEINGEXPANDED
;
1617 set_line_mbstate
();
1623 register STRING_SAVER
*t
, *t1
;
1625 for
(t
= pushed_string_list
; t
; )
1628 FREE
(t
->saved_line
);
1631 t
->expander
->flags
&= ~AL_BEINGEXPANDED
;
1636 pushed_string_list
= (STRING_SAVER
*)NULL
;
1639 #endif /* ALIAS || DPAREN_ARITHMETIC */
1642 free_pushed_string_input
()
1644 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1645 free_string_list
();
1649 /* Return a line of text, taken from wherever yylex () reads input.
1650 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1651 is non-zero, we remove unquoted \<newline> pairs. This is used by
1652 read_secondary_line to read here documents. */
1654 read_a_line
(remove_quoted_newline
)
1655 int remove_quoted_newline
;
1657 static char *line_buffer
= (char *)NULL
;
1658 static int buffer_size
= 0;
1659 int indx
= 0, c
, peekc
, pass_next
;
1661 #if defined (READLINE)
1662 if
(no_line_editing
&& SHOULD_PROMPT
())
1664 if
(SHOULD_PROMPT
())
1671 /* Allow immediate exit if interrupted during input. */
1676 /* Ignore null bytes in input. */
1680 internal_warning
("read_a_line: ignored null byte in input");
1685 /* If there is no more input, then we return NULL. */
1688 if
(interactive
&& bash_input.type
== st_stream
)
1691 return
((char *)NULL
);
1695 /* `+2' in case the final character in the buffer is a newline. */
1696 RESIZE_MALLOCED_BUFFER
(line_buffer
, indx
, 2, buffer_size
, 128);
1698 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
1699 here document with an unquoted delimiter. In this case,
1700 the line will be expanded as if it were in double quotes.
1701 We allow a backslash to escape the next character, but we
1702 need to treat the backslash specially only if a backslash
1703 quoting a backslash-newline pair appears in the line. */
1706 line_buffer
[indx
++] = c
;
1709 else if
(c
== '\\' && remove_quoted_newline
)
1715 continue
; /* Make the unquoted \<newline> pair disappear. */
1721 line_buffer
[indx
++] = c
; /* Preserve the backslash. */
1725 line_buffer
[indx
++] = c
;
1729 line_buffer
[indx
] = '\0';
1730 return
(line_buffer
);
1735 /* Return a line as in read_a_line (), but insure that the prompt is
1736 the secondary prompt. This is used to read the lines of a here
1737 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
1738 newlines quoted with backslashes while reading the line. It is
1739 non-zero unless the delimiter of the here document was quoted. */
1741 read_secondary_line
(remove_quoted_newline
)
1742 int remove_quoted_newline
;
1744 prompt_string_pointer
= &ps2_prompt
;
1745 if
(SHOULD_PROMPT
())
1747 return
(read_a_line
(remove_quoted_newline
));
1750 /* **************************************************************** */
1754 /* **************************************************************** */
1756 /* Reserved words. These are only recognized as the first word of a
1758 STRING_INT_ALIST word_token_alist
[] = {
1767 #if defined (SELECT_COMMAND)
1768 { "select", SELECT
},
1775 { "function", FUNCTION
},
1776 #if defined (COMMAND_TIMING)
1782 #if defined (COND_COMMAND)
1783 { "[[", COND_START
},
1789 /* other tokens that can be returned by read_token() */
1790 STRING_INT_ALIST other_token_alist
[] = {
1791 /* Multiple-character tokens with special values */
1795 { ">>", GREATER_GREATER
},
1796 { "<<", LESS_LESS
},
1798 { ">&", GREATER_AND
},
1799 { ";;", SEMI_SEMI
},
1800 { "<<-", LESS_LESS_MINUS
},
1801 { "<<<", LESS_LESS_LESS
},
1802 { "&>", AND_GREATER
},
1803 { "<>", LESS_GREATER
},
1804 { ">|", GREATER_BAR
},
1805 { "EOF", yacc_EOF
},
1806 /* Tokens whose value is the character itself */
1817 { "newline", '\n' },
1821 /* others not listed here:
1822 WORD look at yylval.word
1823 ASSIGNMENT_WORD look at yylval.word
1824 NUMBER look at yylval.number
1825 ARITH_CMD look at yylval.word_list
1826 ARITH_FOR_EXPRS look at yylval.word_list
1827 COND_CMD look at yylval.command
1830 /* These are used by read_token_word, but appear up here so that shell_getc
1831 can use them to decide when to add otherwise blank lines to the history. */
1833 /* The primary delimiter stack. */
1834 struct dstack dstack
= { (char *)NULL
, 0, 0 };
1836 /* A temporary delimiter stack to be used when decoding prompt strings.
1837 This is needed because command substitutions in prompt strings (e.g., PS2)
1838 can screw up the parser's quoting state. */
1839 static struct dstack temp_dstack
= { (char *)NULL
, 0, 0 };
1841 /* Macro for accessing the top delimiter on the stack. Returns the
1842 delimiter or zero if none. */
1843 #define current_delimiter(ds) \
1844 (ds.delimiter_depth ? ds.delimiters
[ds.delimiter_depth
- 1] : 0)
1846 #define push_delimiter(ds, character) \
1849 if
(ds.delimiter_depth
+ 2 > ds.delimiter_space
) \
1850 ds.delimiters
= (char *)xrealloc \
1851 (ds.delimiters
, (ds.delimiter_space
+= 10) * sizeof
(char)); \
1852 ds.delimiters
[ds.delimiter_depth
] = character
; \
1853 ds.delimiter_depth
++; \
1857 #define pop_delimiter(ds) ds.delimiter_depth--
1859 /* Return the next shell input character. This always reads characters
1860 from shell_input_line; when that line is exhausted, it is time to
1861 read the next line. This is called by read_token when the shell is
1862 processing normal command input. */
1864 /* This implements one-character lookahead/lookbehind across physical input
1865 lines, to avoid something being lost because it's pushed back with
1866 shell_ungetc when we're at the start of a line. */
1867 static int eol_ungetc_lookahead
= 0;
1870 shell_getc
(remove_quoted_newline
)
1871 int remove_quoted_newline
;
1879 if
(sigwinch_received
)
1881 sigwinch_received
= 0;
1882 get_new_window_size
(0, (int *)0, (int *)0);
1885 if
(eol_ungetc_lookahead
)
1887 c
= eol_ungetc_lookahead
;
1888 eol_ungetc_lookahead
= 0;
1892 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1893 /* If shell_input_line[shell_input_line_index] == 0, but there is
1894 something on the pushed list of strings, then we don't want to go
1895 off and get another line. We let the code down below handle it. */
1897 if
(!shell_input_line ||
((!shell_input_line
[shell_input_line_index
]) &&
1898 (pushed_string_list
== (STRING_SAVER
*)NULL
)))
1899 #else /* !ALIAS && !DPAREN_ARITHMETIC */
1900 if
(!shell_input_line ||
!shell_input_line
[shell_input_line_index
])
1901 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
1907 /* Allow immediate exit if interrupted during input. */
1911 shell_input_line_terminator
= 0;
1913 /* If the shell is interatctive, but not currently printing a prompt
1914 (interactive_shell && interactive == 0), we don't want to print
1915 notifies or cleanup the jobs -- we want to defer it until we do
1916 print the next prompt. */
1917 if
(interactive_shell
== 0 || SHOULD_PROMPT
())
1919 #if defined (JOB_CONTROL)
1920 /* This can cause a problem when reading a command as the result
1921 of a trap, when the trap is called from flush_child. This call
1922 had better not cause jobs to disappear from the job table in
1923 that case, or we will have big trouble. */
1924 notify_and_cleanup
();
1925 #else /* !JOB_CONTROL */
1926 cleanup_dead_jobs
();
1927 #endif /* !JOB_CONTROL */
1930 #if defined (READLINE)
1931 if
(no_line_editing
&& SHOULD_PROMPT
())
1933 if
(SHOULD_PROMPT
())
1937 if
(bash_input.type
== st_stream
)
1944 /* Allow immediate exit if interrupted during input. */
1950 internal_warning
("shell_getc: ignored null byte in input");
1955 RESIZE_MALLOCED_BUFFER
(shell_input_line
, i
, 2, shell_input_line_size
, 256);
1959 if
(bash_input.type
== st_stream
)
1963 shell_input_line_terminator
= EOF
;
1965 shell_input_line
[i
] = '\0';
1969 shell_input_line
[i
++] = c
;
1973 shell_input_line
[--i
] = '\0';
1974 current_command_line_count
++;
1979 shell_input_line_index
= 0;
1980 shell_input_line_len
= i
; /* == strlen (shell_input_line) */
1982 set_line_mbstate
();
1984 #if defined (HISTORY)
1985 if
(remember_on_history
&& shell_input_line
&& shell_input_line
[0])
1988 # if defined (BANG_HISTORY)
1991 /* If the current delimiter is a single quote, we should not be
1992 performing history expansion, even if we're on a different
1993 line from the original single quote. */
1994 old_hist
= history_expansion_inhibited
;
1995 if
(current_delimiter
(dstack
) == '\'')
1996 history_expansion_inhibited
= 1;
1998 expansions
= pre_process_line
(shell_input_line
, 1, 1);
1999 # if defined (BANG_HISTORY)
2000 history_expansion_inhibited
= old_hist
;
2002 if
(expansions
!= shell_input_line
)
2004 free
(shell_input_line
);
2005 shell_input_line
= expansions
;
2006 shell_input_line_len
= shell_input_line ?
2007 strlen
(shell_input_line
) : 0;
2008 if
(!shell_input_line_len
)
2009 current_command_line_count
--;
2011 /* We have to force the xrealloc below because we don't know
2012 the true allocated size of shell_input_line anymore. */
2013 shell_input_line_size
= shell_input_line_len
;
2015 set_line_mbstate
();
2018 /* Try to do something intelligent with blank lines encountered while
2019 entering multi-line commands. XXX - this is grotesque */
2020 else if
(remember_on_history
&& shell_input_line
&&
2021 shell_input_line
[0] == '\0' &&
2022 current_command_line_count
> 1)
2024 if
(current_delimiter
(dstack
))
2025 /* We know shell_input_line[0] == 0 and we're reading some sort of
2026 quoted string. This means we've got a line consisting of only
2027 a newline in a quoted string. We want to make sure this line
2028 gets added to the history. */
2029 maybe_add_history
(shell_input_line
);
2033 hdcs
= history_delimiting_chars
();
2034 if
(hdcs
&& hdcs
[0] == ';')
2035 maybe_add_history
(shell_input_line
);
2039 #endif /* HISTORY */
2041 if
(shell_input_line
)
2043 /* Lines that signify the end of the shell's input should not be
2045 if
(echo_input_at_read
&& (shell_input_line
[0] ||
2046 shell_input_line_terminator
!= EOF
))
2047 fprintf
(stderr
, "%s\n", shell_input_line
);
2051 shell_input_line_size
= 0;
2052 prompt_string_pointer
= ¤t_prompt_string
;
2053 if
(SHOULD_PROMPT
())
2058 /* Add the newline to the end of this string, iff the string does
2059 not already end in an EOF character. */
2060 if
(shell_input_line_terminator
!= EOF
)
2062 if
(shell_input_line_len
+ 3 > shell_input_line_size
)
2063 shell_input_line
= (char *)xrealloc
(shell_input_line
,
2064 1 + (shell_input_line_size
+= 2));
2066 shell_input_line
[shell_input_line_len
] = '\n';
2067 shell_input_line
[shell_input_line_len
+ 1] = '\0';
2069 set_line_mbstate
();
2073 uc
= shell_input_line
[shell_input_line_index
];
2076 shell_input_line_index
++;
2078 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2079 /* If UC is NULL, we have reached the end of the current input string. If
2080 pushed_string_list is non-empty, it's time to pop to the previous string
2081 because we have fully consumed the result of the last alias expansion.
2082 Do it transparently; just return the next character of the string popped
2084 if
(!uc
&& (pushed_string_list
!= (STRING_SAVER
*)NULL
))
2087 uc
= shell_input_line
[shell_input_line_index
];
2089 shell_input_line_index
++;
2091 #endif /* ALIAS || DPAREN_ARITHMETIC */
2093 if MBTEST
(uc
== '\\' && remove_quoted_newline
&& shell_input_line
[shell_input_line_index
] == '\n')
2095 if
(SHOULD_PROMPT
())
2101 if
(!uc
&& shell_input_line_terminator
== EOF
)
2102 return
((shell_input_line_index
!= 0) ?
'\n' : EOF
);
2107 /* Put C back into the input for the shell. This might need changes for
2108 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
2109 character different than we read, shell_input_line_property doesn't need
2110 to change when manipulating shell_input_line. The define for
2111 last_shell_getc_is_singlebyte should take care of it, though. */
2116 if
(shell_input_line
&& shell_input_line_index
)
2117 shell_input_line
[--shell_input_line_index
] = c
;
2119 eol_ungetc_lookahead
= c
;
2122 #ifdef INCLUDE_UNUSED
2123 /* Back the input pointer up by one, effectively `ungetting' a character. */
2127 if
(shell_input_line
&& shell_input_line_index
)
2128 shell_input_line_index
--;
2132 /* Discard input until CHARACTER is seen, then push that character back
2133 onto the input stream. */
2135 discard_until
(character
)
2140 while
((c
= shell_getc
(0)) != EOF
&& c
!= character
)
2148 execute_variable_command
(command
, vname
)
2149 char *command
, *vname
;
2152 sh_parser_state_t ps
;
2154 save_parser_state
(&ps
);
2155 last_lastarg
= get_string_value
("_");
2157 last_lastarg
= savestring
(last_lastarg
);
2159 parse_and_execute
(savestring
(command
), vname
, SEVAL_NONINT|SEVAL_NOHIST
);
2161 restore_parser_state
(&ps
);
2162 bind_variable
("_", last_lastarg
, 0);
2163 FREE
(last_lastarg
);
2165 if
(token_to_read
== '\n') /* reset_parser was called */
2169 /* Place to remember the token. We try to keep the buffer
2170 at a reasonable size, but it can grow. */
2171 static char *token
= (char *)NULL
;
2173 /* Current size of the token buffer. */
2174 static int token_buffer_size
;
2176 /* Command to read_token () explaining what we want it to do. */
2179 #define prompt_is_ps1 \
2180 (!prompt_string_pointer || prompt_string_pointer
== &ps1_prompt
)
2182 /* Function for yyparse to call. yylex keeps track of
2183 the last two tokens read, and calls read_token. */
2187 if
(interactive
&& (current_token
== 0 || current_token
== '\n'))
2189 /* Before we print a prompt, we might have to check mailboxes.
2190 We do this only if it is time to do so. Notice that only here
2191 is the mail alarm reset; nothing takes place in check_mail ()
2192 except the checking of mail. Please don't change this. */
2193 if
(prompt_is_ps1
&& time_to_check_mail
())
2196 reset_mail_timer
();
2199 /* Avoid printing a prompt if we're not going to read anything, e.g.
2200 after resetting the parser with read_token (RESET). */
2201 if
(token_to_read
== 0 && SHOULD_PROMPT
())
2205 two_tokens_ago
= token_before_that
;
2206 token_before_that
= last_read_token
;
2207 last_read_token
= current_token
;
2208 current_token
= read_token
(READ
);
2209 return
(current_token
);
2212 /* When non-zero, we have read the required tokens
2213 which allow ESAC to be the next one read. */
2214 static int esacs_needed_count
;
2217 gather_here_documents
()
2220 while
(need_here_doc
)
2222 make_here_document
(redir_stack
[r
++]);
2227 /* When non-zero, an open-brace used to create a group is awaiting a close
2229 static int open_brace_count
;
2231 #define command_token_position(token) \
2232 (((token
) == ASSIGNMENT_WORD
) || \
2233 ((token
) != SEMI_SEMI
&& reserved_word_acceptable
(token
)))
2235 #define assignment_acceptable(token) \
2236 (command_token_position
(token
) && ((parser_state
& PST_CASEPAT
) == 0))
2238 /* Check to see if TOKEN is a reserved word and return the token
2240 #define CHECK_FOR_RESERVED_WORD(tok) \
2242 if
(!dollar_present
&& !quoted
&& \
2243 reserved_word_acceptable
(last_read_token
)) \
2246 for
(i
= 0; word_token_alist
[i
].word
!= (char *)NULL
; i
++) \
2247 if
(STREQ
(tok
, word_token_alist
[i
].word
)) \
2249 if
((parser_state
& PST_CASEPAT
) && (word_token_alist
[i
].token
!= ESAC
)) \
2251 if
(word_token_alist
[i
].token
== TIME
&& time_command_acceptable
() == 0) \
2253 if
(word_token_alist
[i
].token
== ESAC
) \
2254 parser_state
&= ~
(PST_CASEPAT|PST_CASESTMT
); \
2255 else if
(word_token_alist
[i
].token
== CASE
) \
2256 parser_state |
= PST_CASESTMT
; \
2257 else if
(word_token_alist
[i
].token
== COND_END
) \
2258 parser_state
&= ~
(PST_CONDCMD|PST_CONDEXPR
); \
2259 else if
(word_token_alist
[i
].token
== COND_START
) \
2260 parser_state |
= PST_CONDCMD
; \
2261 else if
(word_token_alist
[i
].token
== '{') \
2262 open_brace_count
++; \
2263 else if
(word_token_alist
[i
].token
== '}' && open_brace_count
) \
2264 open_brace_count
--; \
2265 return
(word_token_alist
[i
].token
); \
2272 /* OK, we have a token. Let's try to alias expand it, if (and only if)
2275 It is eligible for expansion if EXPAND_ALIASES is set, and
2276 the token is unquoted and the last token read was a command
2277 separator (or expand_next_token is set), and we are currently
2278 processing an alias (pushed_string_list is non-empty) and this
2279 token is not the same as the current or any previously
2282 Special cases that disqualify:
2283 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
2293 r
= xmalloc
(l
+ 2);
2302 alias_expand_token
(tokstr
)
2308 if
(((parser_state
& PST_ALEXPNEXT
) || command_token_position
(last_read_token
)) &&
2309 (parser_state
& PST_CASEPAT
) == 0)
2311 ap
= find_alias
(tokstr
);
2313 /* Currently expanding this token. */
2314 if
(ap
&& (ap
->flags
& AL_BEINGEXPANDED
))
2315 return
(NO_EXPANSION
);
2317 /* mk_alexpansion puts an extra space on the end of the alias expansion,
2318 so the lookahead by the parser works right. If this gets changed,
2319 make sure the code in shell_getc that deals with reaching the end of
2320 an expanded alias is changed with it. */
2321 expanded
= ap ? mk_alexpansion
(ap
->value
) : (char *)NULL
;
2325 push_string
(expanded
, ap
->flags
& AL_EXPANDNEXT
, ap
);
2326 return
(RE_READ_TOKEN
);
2329 /* This is an eligible token that does not have an expansion. */
2330 return
(NO_EXPANSION
);
2332 return
(NO_EXPANSION
);
2337 time_command_acceptable
()
2339 #if defined (COMMAND_TIMING)
2340 switch
(last_read_token
)
2359 #endif /* COMMAND_TIMING */
2362 /* Handle special cases of token recognition:
2363 IN is recognized if the last token was WORD and the token
2364 before that was FOR or CASE or SELECT.
2366 DO is recognized if the last token was WORD and the token
2367 before that was FOR or SELECT.
2369 ESAC is recognized if the last token caused `esacs_needed_count'
2372 `{' is recognized if the last token as WORD and the token
2373 before that was FUNCTION, or if we just parsed an arithmetic
2376 `}' is recognized if there is an unclosed `{' present.
2378 `-p' is returned as TIMEOPT if the last read token was TIME.
2380 ']]' is returned as COND_END if the parser is currently parsing
2381 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
2383 `time' is returned as TIME if and only if it is immediately
2384 preceded by one of `;', `\n', `||', `&&', or `&'.
2388 special_case_tokens
(tokstr
)
2391 if
((last_read_token
== WORD
) &&
2392 #if defined (SELECT_COMMAND)
2393 ((token_before_that
== FOR
) ||
(token_before_that
== CASE
) ||
(token_before_that
== SELECT
)) &&
2395 ((token_before_that
== FOR
) ||
(token_before_that
== CASE
)) &&
2397 (tokstr
[0] == 'i' && tokstr
[1] == 'n' && tokstr
[2] == 0))
2399 if
(token_before_that
== CASE
)
2401 parser_state |
= PST_CASEPAT
;
2402 esacs_needed_count
++;
2407 if
(last_read_token
== WORD
&&
2408 #if defined (SELECT_COMMAND)
2409 (token_before_that
== FOR || token_before_that
== SELECT
) &&
2411 (token_before_that
== FOR
) &&
2413 (tokstr
[0] == 'd' && tokstr
[1] == 'o' && tokstr
[2] == '\0'))
2416 /* Ditto for ESAC in the CASE case.
2417 Specifically, this handles "case word in esac", which is a legal
2418 construct, certainly because someone will pass an empty arg to the
2419 case construct, and we don't want it to barf. Of course, we should
2420 insist that the case construct has at least one pattern in it, but
2421 the designers disagree. */
2422 if
(esacs_needed_count
)
2424 esacs_needed_count
--;
2425 if
(STREQ
(tokstr
, "esac"))
2427 parser_state
&= ~PST_CASEPAT
;
2432 /* The start of a shell function definition. */
2433 if
(parser_state
& PST_ALLOWOPNBRC
)
2435 parser_state
&= ~PST_ALLOWOPNBRC
;
2436 if
(tokstr
[0] == '{' && tokstr
[1] == '\0') /* } */
2439 function_bstart
= line_number
;
2440 return
('{'); /* } */
2444 /* We allow a `do' after a for ((...)) without an intervening
2446 if
(last_read_token
== ARITH_FOR_EXPRS
&& tokstr
[0] == 'd' && tokstr
[1] == 'o' && !tokstr
[2])
2448 if
(last_read_token
== ARITH_FOR_EXPRS
&& tokstr
[0] == '{' && tokstr
[1] == '\0') /* } */
2451 return
('{'); /* } */
2454 if
(open_brace_count
&& reserved_word_acceptable
(last_read_token
) && tokstr
[0] == '}' && !tokstr
[1])
2456 open_brace_count
--; /* { */
2460 #if defined (COMMAND_TIMING)
2461 /* Handle -p after `time'. */
2462 if
(last_read_token
== TIME
&& tokstr
[0] == '-' && tokstr
[1] == 'p' && !tokstr
[2])
2467 #if defined (COMMAND_TIMING)
2468 if
(STREQ
(token
, "time") && ((parser_state
& PST_CASEPAT
) == 0) && time_command_acceptable
())
2470 #endif /* COMMAND_TIMING */
2473 #if defined (COND_COMMAND) /* [[ */
2474 if
((parser_state
& PST_CONDEXPR
) && tokstr
[0] == ']' && tokstr
[1] == ']' && tokstr
[2] == '\0')
2481 /* Called from shell.c when Control-C is typed at top level. Or
2482 by the error rule at top level. */
2486 dstack.delimiter_depth
= 0; /* No delimiters found so far. */
2487 open_brace_count
= 0;
2491 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2492 if
(pushed_string_list
)
2493 free_string_list
();
2494 #endif /* ALIAS || DPAREN_ARITHMETIC */
2496 if
(shell_input_line
)
2498 free
(shell_input_line
);
2499 shell_input_line
= (char *)NULL
;
2500 shell_input_line_size
= shell_input_line_index
= 0;
2503 FREE
(word_desc_to_read
);
2504 word_desc_to_read
= (WORD_DESC
*)NULL
;
2506 last_read_token
= '\n';
2507 token_to_read
= '\n';
2510 /* Read the next token. Command can be READ (normal operation) or
2511 RESET (to normalize state). */
2513 read_token
(command
)
2516 int character
; /* Current character. */
2517 int peek_char
; /* Temporary look-ahead character. */
2518 int result
; /* The thing to return. */
2520 if
(command
== RESET
)
2528 result
= token_to_read
;
2529 if
(token_to_read
== WORD || token_to_read
== ASSIGNMENT_WORD
)
2531 yylval.word
= word_desc_to_read
;
2532 word_desc_to_read
= (WORD_DESC
*)NULL
;
2538 #if defined (COND_COMMAND)
2539 if
((parser_state
& (PST_CONDCMD|PST_CONDEXPR
)) == PST_CONDCMD
)
2541 cond_lineno
= line_number
;
2542 parser_state |
= PST_CONDEXPR
;
2543 yylval.command
= parse_cond_command
();
2544 if
(cond_token
!= COND_END
)
2549 token_to_read
= COND_END
;
2550 parser_state
&= ~
(PST_CONDEXPR|PST_CONDCMD
);
2556 /* This is a place to jump back to once we have successfully expanded a
2557 token with an alias and pushed the string with push_string () */
2561 /* Read a single word from input. Start by skipping blanks. */
2562 while
((character
= shell_getc
(1)) != EOF
&& shellblank
(character
))
2565 if
(character
== EOF
)
2571 if MBTEST
(character
== '#' && (!interactive || interactive_comments
))
2573 /* A comment. Discard until EOL or EOF, and then return a newline. */
2574 discard_until
('\n');
2576 character
= '\n'; /* this will take the next if statement and return. */
2579 if
(character
== '\n')
2581 /* If we're about to return an unquoted newline, we can go and collect
2582 the text of any pending here document. */
2584 gather_here_documents
();
2587 parser_state
&= ~PST_ALEXPNEXT
;
2590 parser_state
&= ~PST_ASSIGNOK
;
2595 if
(parser_state
& PST_REGEXP
)
2598 /* Shell meta-characters. */
2599 if MBTEST
(shellmeta
(character
) && ((parser_state
& PST_DBLPAREN
) == 0))
2602 /* Turn off alias tokenization iff this character sequence would
2603 not leave us ready to read a command. */
2604 if
(character
== '<' || character
== '>')
2605 parser_state
&= ~PST_ALEXPNEXT
;
2608 parser_state
&= ~PST_ASSIGNOK
;
2610 peek_char
= shell_getc
(1);
2611 if
(character
== peek_char
)
2616 /* If '<' then we could be at "<<" or at "<<-". We have to
2617 look ahead one more character. */
2618 peek_char
= shell_getc
(1);
2619 if
(peek_char
== '-')
2620 return
(LESS_LESS_MINUS
);
2621 else if
(peek_char
== '<')
2622 return
(LESS_LESS_LESS
);
2625 shell_ungetc
(peek_char
);
2630 return
(GREATER_GREATER
);
2633 parser_state |
= PST_CASEPAT
;
2635 parser_state
&= ~PST_ALEXPNEXT
;
2646 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2648 result
= parse_dparen
(character
);
2656 else if MBTEST
(character
== '<' && peek_char
== '&')
2658 else if MBTEST
(character
== '>' && peek_char
== '&')
2659 return
(GREATER_AND
);
2660 else if MBTEST
(character
== '<' && peek_char
== '>')
2661 return
(LESS_GREATER
);
2662 else if MBTEST
(character
== '>' && peek_char
== '|')
2663 return
(GREATER_BAR
);
2664 else if MBTEST
(peek_char
== '>' && character
== '&')
2665 return
(AND_GREATER
);
2667 shell_ungetc
(peek_char
);
2669 /* If we look like we are reading the start of a function
2670 definition, then let the reader know about it so that
2671 we will do the right thing with `{'. */
2672 if MBTEST
(character
== ')' && last_read_token
== '(' && token_before_that
== WORD
)
2674 parser_state |
= PST_ALLOWOPNBRC
;
2676 parser_state
&= ~PST_ALEXPNEXT
;
2678 function_dstart
= line_number
;
2681 /* case pattern lists may be preceded by an optional left paren. If
2682 we're not trying to parse a case pattern list, the left paren
2683 indicates a subshell. */
2684 if MBTEST
(character
== '(' && (parser_state
& PST_CASEPAT
) == 0) /* ) */
2685 parser_state |
= PST_SUBSHELL
;
2687 else if MBTEST
((parser_state
& PST_CASEPAT
) && character
== ')')
2688 parser_state
&= ~PST_CASEPAT
;
2690 else if MBTEST
((parser_state
& PST_SUBSHELL
) && character
== ')')
2691 parser_state
&= ~PST_SUBSHELL
;
2693 #if defined (PROCESS_SUBSTITUTION)
2694 /* Check for the constructs which introduce process substitution.
2695 Shells running in `posix mode' don't do process substitution. */
2696 if MBTEST
(posixly_correct ||
((character
!= '>' && character
!= '<') || peek_char
!= '(')) /*)*/
2697 #endif /* PROCESS_SUBSTITUTION */
2701 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
2702 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
;
2740 /* itrace("parse_matched_pair: open = %c close = %c", open, close); */
2742 pass_next_character
= backq_backslash
= was_dollar
= in_comment
= 0;
2743 check_comment
= (flags
& P_COMMAND
) && qc
!= '`' && qc
!= '\'' && qc
!= '"' && (flags
& P_DQUOTE
) == 0;
2745 /* RFLAGS is the set of flags we want to pass to recursive calls. */
2746 rflags
= (qc
== '"') ? P_DQUOTE
: (flags
& P_DQUOTE
);
2748 ret
= (char *)xmalloc
(retsize
= 64);
2751 start_lineno
= line_number
;
2754 ch
= shell_getc
(qc
!= '\'' && pass_next_character
== 0 && backq_backslash
== 0);
2759 parser_error
(start_lineno
, _
("unexpected EOF while looking for matching `%c'"), close
);
2760 EOF_Reached
= 1; /* XXX */
2761 return
(&matched_pair_error
);
2764 /* Possible reprompting. */
2765 if
(ch
== '\n' && SHOULD_PROMPT
())
2770 /* Add this character. */
2771 RESIZE_MALLOCED_BUFFER
(ret
, retind
, 1, retsize
, 64);
2779 /* Not exactly right yet, should handle shell metacharacters, too. If
2780 any changes are made to this test, make analogous changes to subst.c:
2781 extract_delimited_string(). */
2782 else if MBTEST
(check_comment
&& in_comment
== 0 && ch
== '#' && (retind
== 0 || ret
[retind
-1] == '\n' || whitespace
(ret
[retind
- 1])))
2785 /* last char was backslash inside backquoted command substitution */
2786 if
(backq_backslash
)
2788 backq_backslash
= 0;
2789 /* Placeholder for adding special characters */
2792 if
(pass_next_character
) /* last char was backslash */
2794 pass_next_character
= 0;
2795 if
(qc
!= '\'' && ch
== '\n') /* double-quoted \<newline> disappears. */
2797 if
(retind
> 0) retind
--; /* swallow previously-added backslash */
2801 RESIZE_MALLOCED_BUFFER
(ret
, retind
, 2, retsize
, 64);
2802 if MBTEST
(ch
== CTLESC || ch
== CTLNUL
)
2803 ret
[retind
++] = CTLESC
;
2807 else if MBTEST
(ch
== CTLESC || ch
== CTLNUL
) /* special shell escapes */
2809 RESIZE_MALLOCED_BUFFER
(ret
, retind
, 2, retsize
, 64);
2810 ret
[retind
++] = CTLESC
;
2814 else if MBTEST
(ch
== close
) /* ending delimiter */
2816 /* handle nested ${...} specially. */
2817 else if MBTEST
(open
!= close
&& was_dollar
&& open
== '{' && ch
== open
) /* } */
2819 else if MBTEST
(((flags
& P_FIRSTCLOSE
) == 0) && ch
== open
) /* nested begin */
2822 /* Add this character. */
2823 RESIZE_MALLOCED_BUFFER
(ret
, retind
, 1, retsize
, 64);
2826 if
(open
== '\'') /* '' inside grouping construct */
2828 if MBTEST
((flags
& P_ALLOWESC
) && ch
== '\\')
2829 pass_next_character
++;
2831 else if MBTEST
((flags
& P_BACKQUOTE
) && ch
== '\\')
2837 if MBTEST
(ch
== '\\') /* backslashes */
2838 pass_next_character
++;
2840 if
(open
!= close
) /* a grouping construct */
2842 if MBTEST
(shellquote
(ch
))
2844 /* '', ``, or "" inside $(...) or other grouping construct. */
2845 push_delimiter
(dstack
, ch
);
2846 if MBTEST
(was_dollar
&& ch
== '\'') /* $'...' inside group */
2847 nestret
= parse_matched_pair
(ch
, ch
, ch
, &nestlen
, P_ALLOWESC|rflags
);
2849 nestret
= parse_matched_pair
(ch
, ch
, ch
, &nestlen
, rflags
);
2850 pop_delimiter
(dstack
);
2851 if
(nestret
== &matched_pair_error
)
2854 return
&matched_pair_error
;
2856 if MBTEST
(was_dollar
&& ch
== '\'' && (extended_quote ||
(rflags
& P_DQUOTE
) == 0))
2858 /* Translate $'...' here. */
2859 ttrans
= ansiexpand
(nestret
, 0, nestlen
- 1, &ttranslen
);
2862 if
((rflags
& P_DQUOTE
) == 0)
2864 nestret
= sh_single_quote
(ttrans
);
2866 nestlen
= strlen
(nestret
);
2871 nestlen
= ttranslen
;
2873 retind
-= 2; /* back up before the $' */
2875 else if MBTEST
(was_dollar
&& ch
== '"' && (extended_quote ||
(rflags
& P_DQUOTE
) == 0))
2877 /* Locale expand $"..." here. */
2878 ttrans
= localeexpand
(nestret
, 0, nestlen
- 1, start_lineno
, &ttranslen
);
2881 nestret
= sh_mkdoublequoted
(ttrans
, ttranslen
, 0);
2883 nestlen
= ttranslen
+ 2;
2884 retind
-= 2; /* back up before the $" */
2889 RESIZE_MALLOCED_BUFFER
(ret
, retind
, nestlen
, retsize
, 64);
2890 strcpy
(ret
+ retind
, nestret
);
2896 /* Parse an old-style command substitution within double quotes as a
2898 /* XXX - sh and ksh93 don't do this - XXX */
2899 else if MBTEST
(open
== '"' && ch
== '`')
2901 nestret
= parse_matched_pair
(0, '`', '`', &nestlen
, rflags
);
2903 if
(nestret
== &matched_pair_error
)
2906 return
&matched_pair_error
;
2910 RESIZE_MALLOCED_BUFFER
(ret
, retind
, nestlen
, retsize
, 64);
2911 strcpy
(ret
+ retind
, nestret
);
2917 else if MBTEST
(qc
== '`' && (ch
== '"' || ch
== '\'') && in_comment
== 0)
2919 /* Add P_BACKQUOTE so backslash quotes the next character and
2920 shell_getc does the right thing with \<newline>. We do this for
2921 a measure of backwards compatibility -- it's not strictly the
2922 right POSIX thing. */
2923 nestret
= parse_matched_pair
(0, ch
, ch
, &nestlen
, rflags|P_BACKQUOTE
);
2927 else if MBTEST
(open
!= '`' && was_dollar
&& (ch
== '(' || ch
== '{' || ch
== '[')) /* ) } ] */
2928 /* check for $(), $[], or ${} inside quoted string. */
2930 if
(open
== ch
) /* undo previous increment */
2932 if
(ch
== '(') /* ) */
2933 nestret
= parse_matched_pair
(0, '(', ')', &nestlen
, rflags
& ~P_DQUOTE
);
2934 else if
(ch
== '{') /* } */
2935 nestret
= parse_matched_pair
(0, '{', '}', &nestlen
, P_FIRSTCLOSE|rflags
);
2936 else if
(ch
== '[') /* ] */
2937 nestret
= parse_matched_pair
(0, '[', ']', &nestlen
, rflags
);
2941 was_dollar
= MBTEST
(ch
== '$');
2950 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
2951 /* Parse a double-paren construct. It can be either an arithmetic
2952 command, an arithmetic `for' command, or a nested subshell. Returns
2953 the parsed token, -1 on error, or -2 if we didn't do anything and
2954 should just go on. */
2963 #if defined (ARITH_FOR_COMMAND)
2964 if
(last_read_token
== FOR
)
2966 arith_for_lineno
= line_number
;
2967 cmdtyp
= parse_arith_cmd
(&wval
, 0);
2970 wd
= alloc_word_desc
();
2972 yylval.word_list
= make_word_list
(wd
, (WORD_LIST
*)NULL
);
2973 return
(ARITH_FOR_EXPRS
);
2976 return
-1; /* ERROR */
2980 #if defined (DPAREN_ARITHMETIC)
2981 if
(reserved_word_acceptable
(last_read_token
))
2983 sline
= line_number
;
2985 cmdtyp
= parse_arith_cmd
(&wval
, 0);
2986 if
(cmdtyp
== 1) /* arithmetic command */
2988 wd
= alloc_word_desc
();
2990 wd
->flags
= W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE
;
2991 yylval.word_list
= make_word_list
(wd
, (WORD_LIST
*)NULL
);
2994 else if
(cmdtyp
== 0) /* nested subshell */
2996 push_string
(wval
, 0, (alias_t
*)NULL
);
2997 if
((parser_state
& PST_CASEPAT
) == 0)
2998 parser_state |
= PST_SUBSHELL
;
3006 return
-2; /* XXX */
3009 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
3010 If not, assume it's a nested subshell for backwards compatibility and
3011 return 0. In any case, put the characters we've consumed into a locally-
3012 allocated buffer and make *ep point to that buffer. Return -1 on an
3013 error, for example EOF. */
3015 parse_arith_cmd
(ep
, adddq
)
3019 int exp_lineno
, rval
, c
;
3020 char *ttok
, *tokstr
;
3023 exp_lineno
= line_number
;
3024 ttok
= parse_matched_pair
(0, '(', ')', &ttoklen
, 0);
3026 if
(ttok
== &matched_pair_error
)
3028 /* Check that the next character is the closing right paren. If
3029 not, this is a syntax error. ( */
3034 tokstr
= (char *)xmalloc
(ttoklen
+ 4);
3036 /* if ADDDQ != 0 then (( ... )) -> "..." */
3037 if
(rval
== 1 && adddq
) /* arith cmd, add double quotes */
3040 strncpy
(tokstr
+ 1, ttok
, ttoklen
- 1);
3041 tokstr
[ttoklen
] = '"';
3042 tokstr
[ttoklen
+1] = '\0';
3044 else if
(rval
== 1) /* arith cmd, don't add double quotes */
3046 strncpy
(tokstr
, ttok
, ttoklen
- 1);
3047 tokstr
[ttoklen
-1] = '\0';
3049 else
/* nested subshell */
3052 strncpy
(tokstr
+ 1, ttok
, ttoklen
- 1);
3053 tokstr
[ttoklen
] = ')';
3054 tokstr
[ttoklen
+1] = c
;
3055 tokstr
[ttoklen
+2] = '\0';
3062 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
3064 #if defined (COND_COMMAND)
3070 if
(EOF_Reached
&& cond_token
!= COND_ERROR
) /* [[ */
3071 parser_error
(cond_lineno
, _
("unexpected EOF while looking for `]]'"));
3072 else if
(cond_token
!= COND_ERROR
)
3074 if
(etext
= error_token_from_token
(cond_token
))
3076 parser_error
(cond_lineno
, _
("syntax error in conditional expression: unexpected token `%s'"), etext
);
3080 parser_error
(cond_lineno
, _
("syntax error in conditional expression"));
3087 return
(cond_or
());
3096 if
(cond_token
== OR_OR
)
3099 l
= make_cond_node
(COND_OR
, (WORD_DESC
*)NULL
, l
, r
);
3110 if
(cond_token
== AND_AND
)
3113 l
= make_cond_node
(COND_AND
, (WORD_DESC
*)NULL
, l
, r
);
3119 cond_skip_newlines
()
3121 while
((cond_token
= read_token
(READ
)) == '\n')
3123 if
(SHOULD_PROMPT
())
3126 return
(cond_token
);
3129 #define COND_RETURN_ERROR() \
3130 do
{ cond_token
= COND_ERROR
; return
((COND_COM
*)NULL
); } while
(0)
3136 COND_COM
*term
, *tleft
, *tright
;
3140 /* Read a token. It can be a left paren, a `!', a unary operator, or a
3141 word that should be the first argument of a binary operator. Start by
3142 skipping newlines, since this is a compound command. */
3143 tok
= cond_skip_newlines
();
3144 lineno
= line_number
;
3145 if
(tok
== COND_END
)
3147 COND_RETURN_ERROR
();
3149 else if
(tok
== '(')
3151 term
= cond_expr
();
3152 if
(cond_token
!= ')')
3155 dispose_cond_node
(term
); /* ( */
3156 if
(etext
= error_token_from_token
(cond_token
))
3158 parser_error
(lineno
, _
("unexpected token `%s', expected `)'"), etext
);
3162 parser_error
(lineno
, _
("expected `)'"));
3163 COND_RETURN_ERROR
();
3165 term
= make_cond_node
(COND_EXPR
, (WORD_DESC
*)NULL
, term
, (COND_COM
*)NULL
);
3166 (void)cond_skip_newlines
();
3168 else if
(tok
== BANG ||
(tok
== WORD
&& (yylval.word
->word
[0] == '!' && yylval.word
->word
[1] == '\0')))
3171 dispose_word
(yylval.word
); /* not needed */
3172 term
= cond_term
();
3174 term
->flags |
= CMD_INVERT_RETURN
;
3176 else if
(tok
== WORD
&& test_unop
(yylval.word
->word
))
3179 tok
= read_token
(READ
);
3182 tleft
= make_cond_node
(COND_TERM
, yylval.word
, (COND_COM
*)NULL
, (COND_COM
*)NULL
);
3183 term
= make_cond_node
(COND_UNARY
, op
, tleft
, (COND_COM
*)NULL
);
3188 if
(etext
= error_token_from_token
(tok
))
3190 parser_error
(line_number
, _
("unexpected argument `%s' to conditional unary operator"), etext
);
3194 parser_error
(line_number
, _
("unexpected argument to conditional unary operator"));
3195 COND_RETURN_ERROR
();
3198 (void)cond_skip_newlines
();
3200 else if
(tok
== WORD
) /* left argument to binary operator */
3203 tleft
= make_cond_node
(COND_TERM
, yylval.word
, (COND_COM
*)NULL
, (COND_COM
*)NULL
);
3206 tok
= read_token
(READ
);
3207 if
(tok
== WORD
&& test_binop
(yylval.word
->word
))
3209 #if defined (COND_REGEXP)
3210 else if
(tok
== WORD
&& STREQ
(yylval.word
->word
, "=~"))
3213 parser_state |
= PST_REGEXP
;
3216 else if
(tok
== '<' || tok
== '>')
3217 op
= make_word_from_token
(tok
); /* ( */
3218 /* There should be a check before blindly accepting the `)' that we have
3219 seen the opening `('. */
3220 else if
(tok
== COND_END || tok
== AND_AND || tok
== OR_OR || tok
== ')')
3222 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
3223 the test command. Similarly for [[ x && expr ]] or
3224 [[ x || expr ]] or [[ (x) ]]. */
3225 op
= make_word
("-n");
3226 term
= make_cond_node
(COND_UNARY
, op
, tleft
, (COND_COM
*)NULL
);
3232 if
(etext
= error_token_from_token
(tok
))
3234 parser_error
(line_number
, _
("unexpected token `%s', conditional binary operator expected"), etext
);
3238 parser_error
(line_number
, _
("conditional binary operator expected"));
3239 dispose_cond_node
(tleft
);
3240 COND_RETURN_ERROR
();
3244 tok
= read_token
(READ
);
3245 parser_state
&= ~PST_REGEXP
;
3248 tright
= make_cond_node
(COND_TERM
, yylval.word
, (COND_COM
*)NULL
, (COND_COM
*)NULL
);
3249 term
= make_cond_node
(COND_BINARY
, op
, tleft
, tright
);
3253 if
(etext
= error_token_from_token
(tok
))
3255 parser_error
(line_number
, _
("unexpected argument `%s' to conditional binary operator"), etext
);
3259 parser_error
(line_number
, _
("unexpected argument to conditional binary operator"));
3260 dispose_cond_node
(tleft
);
3262 COND_RETURN_ERROR
();
3265 (void)cond_skip_newlines
();
3270 parser_error
(line_number
, _
("unexpected token `%c' in conditional command"), tok
);
3271 else if
(etext
= error_token_from_token
(tok
))
3273 parser_error
(line_number
, _
("unexpected token `%s' in conditional command"), etext
);
3277 parser_error
(line_number
, _
("unexpected token %d in conditional command"), tok
);
3278 COND_RETURN_ERROR
();
3283 /* This is kind of bogus -- we slip a mini recursive-descent parser in
3284 here to handle the conditional statement syntax. */
3286 parse_cond_command
()
3290 cexp
= cond_expr
();
3291 return
(make_cond_command
(cexp
));
3295 #if defined (ARRAY_VARS)
3296 /* When this is called, it's guaranteed that we don't care about anything
3297 in t beyond i. We do save and restore the chars, though. */
3299 token_is_assignment
(t
, i
)
3303 unsigned char c
, c1
;
3306 c
= t
[i
]; c1
= t
[i
+1];
3307 t
[i
] = '='; t
[i
+1] = '\0';
3308 r
= assignment
(t
, (parser_state
& PST_COMPASSIGN
) != 0);
3309 t
[i
] = c
; t
[i
+1] = c1
;
3313 /* XXX - possible changes here for `+=' */
3315 token_is_ident
(t
, i
)
3324 r
= legal_identifier
(t
);
3331 read_token_word
(character
)
3334 /* The value for YYLVAL when a WORD is read. */
3335 WORD_DESC
*the_word
;
3337 /* Index into the token that we are building. */
3340 /* ALL_DIGITS becomes zero when we see a non-digit. */
3341 int all_digit_token
;
3343 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
3346 /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
3348 int compound_assignment
;
3350 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
3353 /* Non-zero means to ignore the value of the next character, and just
3354 to add it no matter what. */
3355 int pass_next_character
;
3357 /* The current delimiting character. */
3359 int result
, peek_char
;
3360 char *ttok
, *ttrans
;
3361 int ttoklen
, ttranslen
;
3364 if
(token_buffer_size
< TOKEN_DEFAULT_INITIAL_SIZE
)
3365 token
= (char *)xrealloc
(token
, token_buffer_size
= TOKEN_DEFAULT_INITIAL_SIZE
);
3368 all_digit_token
= DIGIT
(character
);
3369 dollar_present
= quoted
= pass_next_character
= compound_assignment
= 0;
3373 if
(character
== EOF
)
3376 if
(pass_next_character
)
3378 pass_next_character
= 0;
3382 cd
= current_delimiter
(dstack
);
3384 /* Handle backslashes. Quote lots of things when not inside of
3385 double-quotes, quote some things inside of double-quotes. */
3386 if MBTEST
(character
== '\\')
3388 peek_char
= shell_getc
(0);
3390 /* Backslash-newline is ignored in all cases except
3391 when quoted with single quotes. */
3392 if
(peek_char
== '\n')
3395 goto next_character
;
3399 shell_ungetc
(peek_char
);
3401 /* If the next character is to be quoted, note it now. */
3402 if
(cd
== 0 || cd
== '`' ||
3403 (cd
== '"' && peek_char
>= 0 && (sh_syntaxtab
[peek_char
] & CBSDQUOTE
)))
3404 pass_next_character
++;
3411 /* Parse a matched pair of quote characters. */
3412 if MBTEST
(shellquote
(character
))
3414 push_delimiter
(dstack
, character
);
3415 ttok
= parse_matched_pair
(character
, character
, character
, &ttoklen
, (character
== '`') ? P_COMMAND
: 0);
3416 pop_delimiter
(dstack
);
3417 if
(ttok
== &matched_pair_error
)
3418 return
-1; /* Bail immediately. */
3419 RESIZE_MALLOCED_BUFFER
(token
, token_index
, ttoklen
+ 2,
3420 token_buffer_size
, TOKEN_DEFAULT_GROW_SIZE
);
3421 token
[token_index
++] = character
;
3422 strcpy
(token
+ token_index
, ttok
);
3423 token_index
+= ttoklen
;
3424 all_digit_token
= 0;
3426 dollar_present |
= (character
== '"' && strchr
(ttok
, '$') != 0);
3428 goto next_character
;
3432 /* When parsing a regexp as a single word inside a conditional command,
3433 we need to special-case characters special to both the shell and
3434 regular expressions. Right now, that is only '(' and '|'. */ /*)*/
3435 if MBTEST
((parser_state
& PST_REGEXP
) && (character
== '(' || character
== '|')) /*)*/
3437 if
(character
== '|')
3440 push_delimiter
(dstack
, character
);
3441 ttok
= parse_matched_pair
(cd
, '(', ')', &ttoklen
, 0);
3442 pop_delimiter
(dstack
);
3443 if
(ttok
== &matched_pair_error
)
3444 return
-1; /* Bail immediately. */
3445 RESIZE_MALLOCED_BUFFER
(token
, token_index
, ttoklen
+ 2,
3446 token_buffer_size
, TOKEN_DEFAULT_GROW_SIZE
);
3447 token
[token_index
++] = character
;
3448 strcpy
(token
+ token_index
, ttok
);
3449 token_index
+= ttoklen
;
3451 dollar_present
= all_digit_token
= 0;
3452 goto next_character
;
3454 #endif /* COND_REGEXP */
3456 #ifdef EXTENDED_GLOB
3457 /* Parse a ksh-style extended pattern matching specification. */
3458 if MBTEST
(extended_glob
&& PATTERN_CHAR
(character
))
3460 peek_char
= shell_getc
(1);
3461 if MBTEST
(peek_char
== '(') /* ) */
3463 push_delimiter
(dstack
, peek_char
);
3464 ttok
= parse_matched_pair
(cd
, '(', ')', &ttoklen
, 0);
3465 pop_delimiter
(dstack
);
3466 if
(ttok
== &matched_pair_error
)
3467 return
-1; /* Bail immediately. */
3468 RESIZE_MALLOCED_BUFFER
(token
, token_index
, ttoklen
+ 2,
3470 TOKEN_DEFAULT_GROW_SIZE
);
3471 token
[token_index
++] = character
;
3472 token
[token_index
++] = peek_char
;
3473 strcpy
(token
+ token_index
, ttok
);
3474 token_index
+= ttoklen
;
3476 dollar_present
= all_digit_token
= 0;
3477 goto next_character
;
3480 shell_ungetc
(peek_char
);
3482 #endif /* EXTENDED_GLOB */
3484 /* If the delimiter character is not single quote, parse some of
3485 the shell expansions that must be read as a single word. */
3486 if
(shellexp
(character
))
3488 peek_char
= shell_getc
(1);
3489 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
3490 if MBTEST
(peek_char
== '(' || \
3491 ((peek_char
== '{' || peek_char
== '[') && character
== '$')) /* ) ] } */
3493 if
(peek_char
== '{') /* } */
3494 ttok
= parse_matched_pair
(cd
, '{', '}', &ttoklen
, P_FIRSTCLOSE
);
3495 else if
(peek_char
== '(') /* ) */
3497 /* XXX - push and pop the `(' as a delimiter for use by
3498 the command-oriented-history code. This way newlines
3499 appearing in the $(...) string get added to the
3500 history literally rather than causing a possibly-
3501 incorrect `;' to be added. ) */
3502 push_delimiter
(dstack
, peek_char
);
3503 ttok
= parse_matched_pair
(cd
, '(', ')', &ttoklen
, P_COMMAND
);
3504 pop_delimiter
(dstack
);
3507 ttok
= parse_matched_pair
(cd
, '[', ']', &ttoklen
, 0);
3508 if
(ttok
== &matched_pair_error
)
3509 return
-1; /* Bail immediately. */
3510 RESIZE_MALLOCED_BUFFER
(token
, token_index
, ttoklen
+ 2,
3512 TOKEN_DEFAULT_GROW_SIZE
);
3513 token
[token_index
++] = character
;
3514 token
[token_index
++] = peek_char
;
3515 strcpy
(token
+ token_index
, ttok
);
3516 token_index
+= ttoklen
;
3519 all_digit_token
= 0;
3520 goto next_character
;
3522 /* This handles $'...' and $"..." new-style quoted strings. */
3523 else if MBTEST
(character
== '$' && (peek_char
== '\'' || peek_char
== '"'))
3527 first_line
= line_number
;
3528 push_delimiter
(dstack
, peek_char
);
3529 ttok
= parse_matched_pair
(peek_char
, peek_char
, peek_char
,
3531 (peek_char
== '\'') ? P_ALLOWESC
: 0);
3532 pop_delimiter
(dstack
);
3533 if
(ttok
== &matched_pair_error
)
3535 if
(peek_char
== '\'')
3537 ttrans
= ansiexpand
(ttok
, 0, ttoklen
- 1, &ttranslen
);
3540 /* Insert the single quotes and correctly quote any
3541 embedded single quotes (allowed because P_ALLOWESC was
3542 passed to parse_matched_pair). */
3543 ttok
= sh_single_quote
(ttrans
);
3545 ttranslen
= strlen
(ttok
);
3550 /* Try to locale-expand the converted string. */
3551 ttrans
= localeexpand
(ttok
, 0, ttoklen
- 1, first_line
, &ttranslen
);
3554 /* Add the double quotes back */
3555 ttok
= sh_mkdoublequoted
(ttrans
, ttranslen
, 0);
3561 RESIZE_MALLOCED_BUFFER
(token
, token_index
, ttranslen
+ 2,
3563 TOKEN_DEFAULT_GROW_SIZE
);
3564 strcpy
(token
+ token_index
, ttrans
);
3565 token_index
+= ttranslen
;
3568 all_digit_token
= 0;
3569 goto next_character
;
3571 /* This could eventually be extended to recognize all of the
3572 shell's single-character parameter expansions, and set flags.*/
3573 else if MBTEST
(character
== '$' && peek_char
== '$')
3575 ttok
= (char *)xmalloc
(3);
3576 ttok
[0] = ttok
[1] = '$';
3578 RESIZE_MALLOCED_BUFFER
(token
, token_index
, 3,
3580 TOKEN_DEFAULT_GROW_SIZE
);
3581 strcpy
(token
+ token_index
, ttok
);
3584 all_digit_token
= 0;
3586 goto next_character
;
3589 shell_ungetc
(peek_char
);
3592 #if defined (ARRAY_VARS)
3593 /* Identify possible array subscript assignment; match [...] */
3594 else if MBTEST
(character
== '[' && token_index
> 0 && assignment_acceptable
(last_read_token
) && token_is_ident
(token
, token_index
)) /* ] */
3596 ttok
= parse_matched_pair
(cd
, '[', ']', &ttoklen
, 0);
3597 if
(ttok
== &matched_pair_error
)
3598 return
-1; /* Bail immediately. */
3599 RESIZE_MALLOCED_BUFFER
(token
, token_index
, ttoklen
+ 2,
3601 TOKEN_DEFAULT_GROW_SIZE
);
3602 token
[token_index
++] = character
;
3603 strcpy
(token
+ token_index
, ttok
);
3604 token_index
+= ttoklen
;
3606 all_digit_token
= 0;
3607 goto next_character
;
3609 /* Identify possible compound array variable assignment. */
3610 else if MBTEST
(character
== '=' && token_index
> 0 && (assignment_acceptable
(last_read_token
) ||
(parser_state
& PST_ASSIGNOK
)) && token_is_assignment
(token
, token_index
))
3612 peek_char
= shell_getc
(1);
3613 if MBTEST
(peek_char
== '(') /* ) */
3615 ttok
= parse_compound_assignment
(&ttoklen
);
3617 RESIZE_MALLOCED_BUFFER
(token
, token_index
, ttoklen
+ 4,
3619 TOKEN_DEFAULT_GROW_SIZE
);
3621 token
[token_index
++] = '=';
3622 token
[token_index
++] = '(';
3625 strcpy
(token
+ token_index
, ttok
);
3626 token_index
+= ttoklen
;
3628 token
[token_index
++] = ')';
3630 all_digit_token
= 0;
3631 compound_assignment
= 1;
3633 goto next_character
;
3635 goto got_token
; /* ksh93 seems to do this */
3639 shell_ungetc
(peek_char
);
3643 /* When not parsing a multi-character word construct, shell meta-
3644 characters break words. */
3645 if MBTEST
(shellbreak
(character
))
3647 shell_ungetc
(character
);
3653 all_digit_token
&= DIGIT
(character
);
3654 dollar_present |
= character
== '$';
3656 if
(character
== CTLESC || character
== CTLNUL
)
3657 token
[token_index
++] = CTLESC
;
3659 token
[token_index
++] = character
;
3661 RESIZE_MALLOCED_BUFFER
(token
, token_index
, 1, token_buffer_size
,
3662 TOKEN_DEFAULT_GROW_SIZE
);
3665 if
(character
== '\n' && SHOULD_PROMPT
())
3668 /* We want to remove quoted newlines (that is, a \<newline> pair)
3669 unless we are within single quotes or pass_next_character is
3670 set (the shell equivalent of literal-next). */
3671 cd
= current_delimiter
(dstack
);
3672 character
= shell_getc
(cd
!= '\'' && pass_next_character
== 0);
3673 } /* end for (;;) */
3677 token
[token_index
] = '\0';
3679 /* Check to see what thing we should return. If the last_read_token
3680 is a `<', or a `&', or the character which ended this token is
3681 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
3682 Otherwise, it is just a word, and should be returned as such. */
3683 if MBTEST
(all_digit_token
&& (character
== '<' || character
== '>' || \
3684 last_read_token
== LESS_AND || \
3685 last_read_token
== GREATER_AND
))
3687 if
(legal_number
(token
, &lvalue
) && (int)lvalue
== lvalue
)
3688 yylval.number
= lvalue
;
3694 /* Check for special case tokens. */
3695 result
= (last_shell_getc_is_singlebyte
) ? special_case_tokens
(token
) : -1;
3700 /* Posix.2 does not allow reserved words to be aliased, so check for all
3701 of them, including special cases, before expanding the current token
3703 if MBTEST
(posixly_correct
)
3704 CHECK_FOR_RESERVED_WORD
(token
);
3706 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
3707 inhibits alias expansion. */
3708 if
(expand_aliases
&& quoted
== 0)
3710 result
= alias_expand_token
(token
);
3711 if
(result
== RE_READ_TOKEN
)
3712 return
(RE_READ_TOKEN
);
3713 else if
(result
== NO_EXPANSION
)
3714 parser_state
&= ~PST_ALEXPNEXT
;
3717 /* If not in Posix.2 mode, check for reserved words after alias
3719 if MBTEST
(posixly_correct
== 0)
3721 CHECK_FOR_RESERVED_WORD
(token
);
3723 the_word
= (WORD_DESC
*)xmalloc
(sizeof
(WORD_DESC
));
3724 the_word
->word
= (char *)xmalloc
(1 + token_index
);
3725 the_word
->flags
= 0;
3726 strcpy
(the_word
->word
, token
);
3728 the_word
->flags |
= W_HASDOLLAR
;
3730 the_word
->flags |
= W_QUOTED
; /*(*/
3731 if
(compound_assignment
&& token
[token_index
-1] == ')')
3732 the_word
->flags |
= W_COMPASSIGN
;
3733 /* A word is an assignment if it appears at the beginning of a
3734 simple command, or after another assignment word. This is
3735 context-dependent, so it cannot be handled in the grammar. */
3736 if
(assignment
(token
, (parser_state
& PST_COMPASSIGN
) != 0))
3738 the_word
->flags |
= W_ASSIGNMENT
;
3739 /* Don't perform word splitting on assignment statements. */
3740 if
(assignment_acceptable
(last_read_token
) ||
(parser_state
& PST_COMPASSIGN
) != 0)
3741 the_word
->flags |
= W_NOSPLIT
;
3744 if
(command_token_position
(last_read_token
))
3747 b
= builtin_address_internal
(token
, 0);
3748 if
(b
&& (b
->flags
& ASSIGNMENT_BUILTIN
))
3749 parser_state |
= PST_ASSIGNOK
;
3750 else if
(STREQ
(token
, "eval") || STREQ
(token
, "let"))
3751 parser_state |
= PST_ASSIGNOK
;
3754 yylval.word
= the_word
;
3756 result
= ((the_word
->flags
& (W_ASSIGNMENT|W_NOSPLIT
)) == (W_ASSIGNMENT|W_NOSPLIT
))
3757 ? ASSIGNMENT_WORD
: WORD
;
3759 switch
(last_read_token
)
3762 parser_state |
= PST_ALLOWOPNBRC
;
3763 function_dstart
= line_number
;
3768 if
(word_top
< MAX_CASE_NEST
)
3770 word_lineno
[word_top
] = line_number
;
3777 /* Return 1 if TOKSYM is a token that after being read would allow
3778 a reserved word to be seen, else 0. */
3780 reserved_word_acceptable
(toksym
)
3816 /* Return the index of TOKEN in the alist of reserved words, or -1 if
3817 TOKEN is not a shell reserved word. */
3819 find_reserved_word
(tokstr
)
3823 for
(i
= 0; word_token_alist
[i
].word
; i
++)
3824 if
(STREQ
(tokstr
, word_token_alist
[i
].word
))
3830 #if defined (READLINE)
3831 /* Called after each time readline is called. This insures that whatever
3832 the new prompt string is gets propagated to readline's local prompt
3835 reset_readline_prompt
()
3839 if
(prompt_string_pointer
)
3841 temp_prompt
= (*prompt_string_pointer
)
3842 ? decode_prompt_string
(*prompt_string_pointer
)
3845 if
(temp_prompt
== 0)
3847 temp_prompt
= (char *)xmalloc
(1);
3848 temp_prompt
[0] = '\0';
3851 FREE
(current_readline_prompt
);
3852 current_readline_prompt
= temp_prompt
;
3855 #endif /* READLINE */
3858 #if defined (HISTORY)
3859 /* A list of tokens which can be followed by newlines, but not by
3860 semi-colons. When concatenating multiple lines of history, the
3861 newline separator for such tokens is replaced with a space. */
3862 static int no_semi_successors
[] = {
3863 '\n', '{', '(', ')', ';', '&', '|',
3864 CASE
, DO
, ELSE
, IF
, SEMI_SEMI
, THEN
, UNTIL
, WHILE
, AND_AND
, OR_OR
, IN
,
3868 /* If we are not within a delimited expression, try to be smart
3869 about which separators can be semi-colons and which must be
3870 newlines. Returns the string that should be added into the
3873 history_delimiting_chars
()
3877 if
(dstack.delimiter_depth
!= 0)
3880 /* First, handle some special cases. */
3882 /* If we just read `()', assume it's a function definition, and don't
3883 add a semicolon. If the token before the `)' was not `(', and we're
3884 not in the midst of parsing a case statement, assume it's a
3885 parenthesized command and add the semicolon. */
3887 if
(token_before_that
== ')')
3889 if
(two_tokens_ago
== '(') /*)*/ /* function def */
3891 /* This does not work for subshells inside case statement
3892 command lists. It's a suboptimal solution. */
3893 else if
(parser_state
& PST_CASESTMT
) /* case statement pattern */
3896 return
"; "; /* (...) subshell */
3898 else if
(token_before_that
== WORD
&& two_tokens_ago
== FUNCTION
)
3899 return
" "; /* function def using `function name' without `()' */
3901 else if
(token_before_that
== WORD
&& two_tokens_ago
== FOR
)
3903 /* Tricky. `for i\nin ...' should not have a semicolon, but
3904 `for i\ndo ...' should. We do what we can. */
3905 for
(i
= shell_input_line_index
; whitespace
(shell_input_line
[i
]); i
++)
3907 if
(shell_input_line
[i
] && shell_input_line
[i
] == 'i' && shell_input_line
[i
+1] == 'n')
3911 else if
(two_tokens_ago
== CASE
&& token_before_that
== WORD
&& (parser_state
& PST_CASESTMT
))
3914 for
(i
= 0; no_semi_successors
[i
]; i
++)
3916 if
(token_before_that
== no_semi_successors
[i
])
3922 #endif /* HISTORY */
3924 /* Issue a prompt, or prepare to issue a prompt when the next character
3931 if
(interactive
== 0 || expanding_alias
()) /* XXX */
3934 ps1_prompt
= get_string_value
("PS1");
3935 ps2_prompt
= get_string_value
("PS2");
3937 if
(!prompt_string_pointer
)
3938 prompt_string_pointer
= &ps1_prompt
;
3940 temp_prompt
= *prompt_string_pointer
3941 ? decode_prompt_string
(*prompt_string_pointer
)
3944 if
(temp_prompt
== 0)
3946 temp_prompt
= (char *)xmalloc
(1);
3947 temp_prompt
[0] = '\0';
3950 current_prompt_string
= *prompt_string_pointer
;
3951 prompt_string_pointer
= &ps2_prompt
;
3953 #if defined (READLINE)
3954 if
(!no_line_editing
)
3956 FREE
(current_readline_prompt
);
3957 current_readline_prompt
= temp_prompt
;
3960 #endif /* READLINE */
3962 FREE
(current_decoded_prompt
);
3963 current_decoded_prompt
= temp_prompt
;
3968 get_current_prompt_level
()
3970 return
((current_prompt_string
&& current_prompt_string
== ps2_prompt
) ?
2 : 1);
3974 set_current_prompt_level
(x
)
3977 prompt_string_pointer
= (x
== 2) ?
&ps2_prompt
: &ps1_prompt
;
3978 current_prompt_string
= *prompt_string_pointer
;
3984 fprintf
(stderr
, "%s", current_decoded_prompt
);
3988 /* Return a string which will be printed as a prompt. The string
3989 may contain special characters which are decoded as follows:
3992 \d the date in Day Mon Date format
3993 \e escape (ascii 033)
3994 \h the hostname up to the first `.'
3996 \j the number of active jobs
3997 \l the basename of the shell's tty device name
4000 \s the name of the shell
4001 \t the time in 24-hour hh:mm:ss format
4002 \T the time in 12-hour hh:mm:ss format
4003 \@ the time in 12-hour hh:mm am/pm format
4004 \A the time in 24-hour hh:mm format
4005 \D{fmt} the result of passing FMT to strftime(3)
4007 \v the version of bash (e.g., 2.00)
4008 \V the release of bash, version + patchlevel (e.g., 2.00.0)
4009 \w the current working directory
4010 \W the last element of $PWD
4011 \! the history number of this command
4012 \# the command number of this command
4013 \$ a $ or a # if you are root
4014 \nnn character code nnn in octal
4016 \[ begin a sequence of non-printing chars
4017 \] end a sequence of non-printing chars
4019 #define PROMPT_GROWTH 48
4021 decode_prompt_string
(string)
4026 struct dstack save_dstack
;
4027 int last_exit_value
;
4028 #if defined (PROMPT_STRING_DECODE)
4029 int result_size
, result_index
;
4031 char *temp
, octal_string
[4];
4037 result
= (char *)xmalloc
(result_size
= PROMPT_GROWTH
);
4038 result
[result_index
= 0] = 0;
4039 temp
= (char *)NULL
;
4041 while
(c
= *string++)
4043 if
(posixly_correct
&& c
== '!')
4047 temp
= savestring
("!");
4052 #if !defined (HISTORY)
4053 temp
= savestring
("1");
4055 temp
= itos
(history_number
());
4056 #endif /* HISTORY */
4057 string--; /* add_string increments string again. */
4075 strncpy
(octal_string
, string, 3);
4076 octal_string
[3] = '\0';
4078 n
= read_octal
(octal_string
);
4079 temp
= (char *)xmalloc
(3);
4081 if
(n
== CTLESC || n
== CTLNUL
)
4098 for
(c
= 0; n
!= -1 && c
< 3 && ISOCTAL
(*string); c
++)
4101 c
= 0; /* tested at add_string: */
4109 /* Make the current time/date into a string. */
4110 (void) time
(&the_time
);
4111 tm
= localtime
(&the_time
);
4114 n
= strftime
(timebuf
, sizeof
(timebuf
), "%a %b %d", tm
);
4116 n
= strftime
(timebuf
, sizeof
(timebuf
), "%H:%M:%S", tm
);
4118 n
= strftime
(timebuf
, sizeof
(timebuf
), "%I:%M:%S", tm
);
4120 n
= strftime
(timebuf
, sizeof
(timebuf
), "%I:%M %p", tm
);
4122 n
= strftime
(timebuf
, sizeof
(timebuf
), "%H:%M", tm
);
4127 timebuf
[sizeof
(timebuf
) - 1] = '\0';
4129 temp
= savestring
(timebuf
);
4132 case
'D': /* strftime format */
4133 if
(string[1] != '{') /* } */
4136 (void) time
(&the_time
);
4137 tm
= localtime
(&the_time
);
4138 string += 2; /* skip { */
4139 timefmt
= xmalloc
(strlen
(string) + 3);
4140 for
(t
= timefmt
; *string && *string != '}'; )
4143 c
= *string; /* tested at add_string */
4144 if
(timefmt
[0] == '\0')
4147 timefmt
[1] = 'X'; /* locale-specific current time */
4150 n
= strftime
(timebuf
, sizeof
(timebuf
), timefmt
, tm
);
4156 timebuf
[sizeof
(timebuf
) - 1] = '\0';
4158 if
(promptvars || posixly_correct
)
4159 /* Make sure that expand_prompt_string is called with a
4160 second argument of Q_DOUBLE_QUOTES if we use this
4162 temp
= sh_backslash_quote_for_double_quotes
(timebuf
);
4164 temp
= savestring
(timebuf
);
4168 temp
= (char *)xmalloc
(3);
4169 temp
[0] = no_line_editing ?
'\n' : '\r';
4170 temp
[1] = no_line_editing ?
'\0' : '\n';
4175 temp
= base_pathname
(shell_name
);
4176 temp
= savestring
(temp
);
4181 temp
= (char *)xmalloc
(16);
4183 strcpy
(temp
, dist_version
);
4185 sprintf
(temp
, "%s.%d", dist_version
, patch_level
);
4191 /* Use the value of PWD because it is much more efficient. */
4192 char t_string
[PATH_MAX
];
4195 temp
= get_string_value
("PWD");
4199 if
(getcwd
(t_string
, sizeof
(t_string
)) == 0)
4205 tlen
= strlen
(t_string
);
4209 tlen
= sizeof
(t_string
) - 1;
4210 strncpy
(t_string
, temp
, tlen
);
4212 t_string
[tlen
] = '\0';
4214 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
4215 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
4216 /* Abbreviate \W as ~ if $PWD == $HOME */
4217 if
(c
== 'W' && (((t
= get_string_value
("HOME")) == 0) || STREQ
(t
, t_string
) == 0))
4219 if
(ROOT_PATH
(t_string
) == 0 && DOUBLE_SLASH_ROOT
(t_string
) == 0)
4221 t
= strrchr
(t_string
, '/');
4223 strcpy
(t_string
, t
+ 1);
4227 #undef DOUBLE_SLASH_ROOT
4229 /* polite_directory_format is guaranteed to return a string
4230 no longer than PATH_MAX - 1 characters. */
4231 strcpy
(t_string
, polite_directory_format
(t_string
));
4233 /* If we're going to be expanding the prompt string later,
4234 quote the directory name. */
4235 if
(promptvars || posixly_correct
)
4236 /* Make sure that expand_prompt_string is called with a
4237 second argument of Q_DOUBLE_QUOTES if we use this
4239 temp
= sh_backslash_quote_for_double_quotes
(t_string
);
4241 temp
= savestring
(t_string
);
4247 if
(current_user.user_name
== 0)
4248 get_current_user_info
();
4249 temp
= savestring
(current_user.user_name
);
4254 temp
= savestring
(current_host_name
);
4255 if
(c
== 'h' && (t
= (char *)strchr
(temp
, '.')))
4260 temp
= itos
(current_command_number
);
4264 #if !defined (HISTORY)
4265 temp
= savestring
("1");
4267 temp
= itos
(history_number
());
4268 #endif /* HISTORY */
4272 t
= temp
= (char *)xmalloc
(3);
4273 if
((promptvars || posixly_correct
) && (current_user.euid
!= 0))
4275 *t
++ = current_user.euid
== 0 ?
'#' : '$';
4280 temp
= itos
(count_all_jobs
());
4284 #if defined (HAVE_TTYNAME)
4285 temp
= (char *)ttyname
(fileno
(stdin
));
4286 t
= temp ? base_pathname
(temp
) : "tty";
4287 temp
= savestring
(t
);
4289 temp
= savestring
("tty");
4290 #endif /* !HAVE_TTYNAME */
4293 #if defined (READLINE)
4296 if
(no_line_editing
)
4301 temp
= (char *)xmalloc
(3);
4302 n
= (c
== '[') ? RL_PROMPT_START_IGNORE
: RL_PROMPT_END_IGNORE
;
4304 if
(n
== CTLESC || n
== CTLNUL
)
4309 #endif /* READLINE */
4315 temp
= (char *)xmalloc
(2);
4322 else
/* (c == '\\') */
4329 temp
= (char *)xmalloc
(3);
4338 sub_append_string
(temp
, result
, &result_index
, &result_size
);
4339 temp
= (char *)NULL
; /* Freed in sub_append_string (). */
4340 result
[result_index
] = '\0';
4346 RESIZE_MALLOCED_BUFFER
(result
, result_index
, 3, result_size
, PROMPT_GROWTH
);
4347 result
[result_index
++] = c
;
4348 result
[result_index
] = '\0';
4351 #else /* !PROMPT_STRING_DECODE */
4352 result
= savestring
(string);
4353 #endif /* !PROMPT_STRING_DECODE */
4355 /* Save the delimiter stack and point `dstack' to temp space so any
4356 command substitutions in the prompt string won't result in screwing
4357 up the parser's quoting state. */
4358 save_dstack
= dstack
;
4359 dstack
= temp_dstack
;
4360 dstack.delimiter_depth
= 0;
4362 /* Perform variable and parameter expansion and command substitution on
4363 the prompt string. */
4364 if
(promptvars || posixly_correct
)
4366 last_exit_value
= last_command_exit_value
;
4367 list
= expand_prompt_string
(result
, Q_DOUBLE_QUOTES
);
4369 result
= string_list
(list
);
4370 dispose_words
(list
);
4371 last_command_exit_value
= last_exit_value
;
4375 t
= dequote_string
(result
);
4380 dstack
= save_dstack
;
4385 /************************************************
4389 ************************************************/
4391 /* Report a syntax error, and restart the parser. Call here for fatal
4397 report_syntax_error
((char *)NULL
);
4403 error_token_from_token
(tok
)
4408 if
(t
= find_token_in_alist
(tok
, word_token_alist
, 0))
4411 if
(t
= find_token_in_alist
(tok
, other_token_alist
, 0))
4415 /* This stuff is dicy and needs closer inspection */
4416 switch
(current_token
)
4419 case ASSIGNMENT_WORD
:
4421 t
= savestring
(yylval.word
->word
);
4424 t
= itos
(yylval.number
);
4427 if
(yylval.word_list
)
4428 t
= string_list
(yylval.word_list
);
4430 case ARITH_FOR_EXPRS
:
4431 if
(yylval.word_list
)
4432 t
= string_list_internal
(yylval.word_list
, " ; ");
4435 t
= (char *)NULL
; /* punt */
4443 error_token_from_text
()
4448 t
= shell_input_line
;
4449 i
= shell_input_line_index
;
4453 if
(i
&& t
[i
] == '\0')
4456 while
(i
&& (whitespace
(t
[i
]) || t
[i
] == '\n'))
4462 while
(i
&& (member
(t
[i
], " \n\t;|&") == 0))
4465 while
(i
!= token_end
&& (whitespace
(t
[i
]) || t
[i
] == '\n'))
4468 /* Return our idea of the offending token. */
4469 if
(token_end ||
(i
== 0 && token_end
== 0))
4472 msg
= substring
(t
, i
, token_end
);
4473 else
/* one-character token */
4475 msg
= (char *)xmalloc
(2);
4485 print_offending_line
()
4490 msg
= savestring
(shell_input_line
);
4491 token_end
= strlen
(msg
);
4492 while
(token_end
&& msg
[token_end
- 1] == '\n')
4493 msg
[--token_end
] = '\0';
4495 parser_error
(line_number
, "`%s'", msg
);
4499 /* Report a syntax error with line numbers, etc.
4500 Call here for recoverable errors. If you have a message to print,
4501 then place it in MESSAGE, otherwise pass NULL and this will figure
4502 out an appropriate message for you. */
4504 report_syntax_error
(message
)
4511 parser_error
(line_number
, "%s", message
);
4512 if
(interactive
&& EOF_Reached
)
4514 last_command_exit_value
= EX_USAGE
;
4518 /* If the line of input we're reading is not null, try to find the
4519 objectionable token. First, try to figure out what token the
4520 parser's complaining about by looking at current_token. */
4521 if
(current_token
!= 0 && EOF_Reached
== 0 && (msg
= error_token_from_token
(current_token
)))
4523 parser_error
(line_number
, _
("syntax error near unexpected token `%s'"), msg
);
4526 if
(interactive
== 0)
4527 print_offending_line
();
4529 last_command_exit_value
= EX_USAGE
;
4533 /* If looking at the current token doesn't prove fruitful, try to find the
4534 offending token by analyzing the text of the input line near the current
4535 input line index and report what we find. */
4536 if
(shell_input_line
&& *shell_input_line
)
4538 msg
= error_token_from_text
();
4541 parser_error
(line_number
, _
("syntax error near `%s'"), msg
);
4545 /* If not interactive, print the line containing the error. */
4546 if
(interactive
== 0)
4547 print_offending_line
();
4551 msg
= EOF_Reached ? _
("syntax error: unexpected end of file") : _
("syntax error");
4552 parser_error
(line_number
, "%s", msg
);
4553 /* When the shell is interactive, this file uses EOF_Reached
4554 only for error reporting. Other mechanisms are used to
4555 decide whether or not to exit. */
4556 if
(interactive
&& EOF_Reached
)
4560 last_command_exit_value
= EX_USAGE
;
4563 /* ??? Needed function. ??? We have to be able to discard the constructs
4564 created during parsing. In the case of error, we want to return
4565 allocated objects to the memory pool. In the case of no error, we want
4566 to throw away the information about where the allocated objects live.
4567 (dispose_command () will actually free the command.) */
4569 discard_parser_constructs
(error_p
)
4574 /************************************************
4578 ************************************************/
4580 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
4582 /* A flag denoting whether or not ignoreeof is set. */
4585 /* The number of times that we have encountered an EOF character without
4586 another character intervening. When this gets above the limit, the
4587 shell terminates. */
4588 int eof_encountered
= 0;
4590 /* The limit for eof_encountered. */
4591 int eof_encountered_limit
= 10;
4593 /* If we have EOF as the only input unit, this user wants to leave
4594 the shell. If the shell is not interactive, then just leave.
4595 Otherwise, if ignoreeof is set, and we haven't done this the
4596 required number of times in a row, print a message. */
4598 handle_eof_input_unit
()
4602 /* shell.c may use this to decide whether or not to write out the
4603 history, among other things. We use it only for error reporting
4608 /* If the user wants to "ignore" eof, then let her do so, kind of. */
4611 if
(eof_encountered
< eof_encountered_limit
)
4613 fprintf
(stderr
, _
("Use \"%s\" to leave the shell.\n"),
4614 login_shell ?
"logout" : "exit");
4616 /* Reset the parsing state. */
4617 last_read_token
= current_token
= '\n';
4618 /* Reset the prompt string to be $PS1. */
4619 prompt_string_pointer
= (char **)NULL
;
4625 /* In this case EOF should exit the shell. Do it now. */
4627 exit_builtin
((WORD_LIST
*)NULL
);
4631 /* We don't write history files, etc., for non-interactive shells. */
4636 /************************************************
4638 * STRING PARSING FUNCTIONS *
4640 ************************************************/
4642 /* It's very important that these two functions treat the characters
4643 between ( and ) identically. */
4645 static WORD_LIST parse_string_error
;
4647 /* Take a string and run it through the shell parser, returning the
4648 resultant word list. Used by compound array assignment. */
4650 parse_string_to_word_list
(s
, flags
, whom
)
4656 int tok
, orig_current_token
, orig_line_number
, orig_input_terminator
;
4657 int orig_line_count
;
4658 int old_echo_input
, old_expand_aliases
;
4659 #if defined (HISTORY)
4660 int old_remember_on_history
, old_history_expansion_inhibited
;
4663 #if defined (HISTORY)
4664 old_remember_on_history
= remember_on_history
;
4665 # if defined (BANG_HISTORY)
4666 old_history_expansion_inhibited
= history_expansion_inhibited
;
4668 bash_history_disable
();
4671 orig_line_number
= line_number
;
4672 orig_line_count
= current_command_line_count
;
4673 orig_input_terminator
= shell_input_line_terminator
;
4674 old_echo_input
= echo_input_at_read
;
4675 old_expand_aliases
= expand_aliases
;
4678 last_read_token
= WORD
; /* WORD to allow reserved words here */
4679 current_command_line_count
= 0;
4680 echo_input_at_read
= expand_aliases
= 0;
4682 with_input_from_string
(s
, whom
);
4683 wl
= (WORD_LIST
*)NULL
;
4686 parser_state |
= PST_COMPASSIGN
;
4688 while
((tok
= read_token
(READ
)) != yacc_EOF
)
4690 if
(tok
== '\n' && *bash_input.location.
string == '\0')
4692 if
(tok
== '\n') /* Allow newlines in compound assignments */
4694 if
(tok
!= WORD
&& tok
!= ASSIGNMENT_WORD
)
4696 line_number
= orig_line_number
+ line_number
- 1;
4697 orig_current_token
= current_token
;
4698 current_token
= tok
;
4699 yyerror (NULL
); /* does the right thing */
4700 current_token
= orig_current_token
;
4703 wl
= &parse_string_error
;
4706 wl
= make_word_list
(yylval.word
, wl
);
4709 last_read_token
= '\n';
4712 #if defined (HISTORY)
4713 remember_on_history
= old_remember_on_history
;
4714 # if defined (BANG_HISTORY)
4715 history_expansion_inhibited
= old_history_expansion_inhibited
;
4716 # endif /* BANG_HISTORY */
4717 #endif /* HISTORY */
4719 echo_input_at_read
= old_echo_input
;
4720 expand_aliases
= old_expand_aliases
;
4722 current_command_line_count
= orig_line_count
;
4723 shell_input_line_terminator
= orig_input_terminator
;
4726 parser_state
&= ~PST_COMPASSIGN
;
4728 if
(wl
== &parse_string_error
)
4730 last_command_exit_value
= EXECUTION_FAILURE
;
4731 if
(interactive_shell
== 0 && posixly_correct
)
4732 jump_to_top_level
(FORCE_EOF
);
4734 jump_to_top_level
(DISCARD
);
4737 return
(REVERSE_LIST
(wl
, WORD_LIST
*));
4741 parse_compound_assignment
(retlenp
)
4745 int tok
, orig_line_number
, orig_token_size
, orig_last_token
, assignok
;
4746 char *saved_token
, *ret
;
4748 saved_token
= token
;
4749 orig_token_size
= token_buffer_size
;
4750 orig_line_number
= line_number
;
4751 orig_last_token
= last_read_token
;
4753 last_read_token
= WORD
; /* WORD to allow reserved words here */
4755 token
= (char *)NULL
;
4756 token_buffer_size
= 0;
4758 assignok
= parser_state
&PST_ASSIGNOK
; /* XXX */
4760 wl
= (WORD_LIST
*)NULL
; /* ( */
4761 parser_state |
= PST_COMPASSIGN
;
4763 while
((tok
= read_token
(READ
)) != ')')
4765 if
(tok
== '\n') /* Allow newlines in compound assignments */
4767 if
(SHOULD_PROMPT
())
4771 if
(tok
!= WORD
&& tok
!= ASSIGNMENT_WORD
)
4773 current_token
= tok
; /* for error reporting */
4774 if
(tok
== yacc_EOF
) /* ( */
4775 parser_error
(orig_line_number
, _
("unexpected EOF while looking for matching `)'"));
4777 yyerror(NULL
); /* does the right thing */
4780 wl
= &parse_string_error
;
4783 wl
= make_word_list
(yylval.word
, wl
);
4787 token
= saved_token
;
4788 token_buffer_size
= orig_token_size
;
4790 parser_state
&= ~PST_COMPASSIGN
;
4792 if
(wl
== &parse_string_error
)
4794 last_command_exit_value
= EXECUTION_FAILURE
;
4795 last_read_token
= '\n'; /* XXX */
4796 if
(interactive_shell
== 0 && posixly_correct
)
4797 jump_to_top_level
(FORCE_EOF
);
4799 jump_to_top_level
(DISCARD
);
4802 last_read_token
= orig_last_token
; /* XXX - was WORD? */
4806 rl
= REVERSE_LIST
(wl
, WORD_LIST
*);
4807 ret
= string_list
(rl
);
4814 *retlenp
= (ret
&& *ret
) ? strlen
(ret
) : 0;
4817 parser_state |
= PST_ASSIGNOK
;
4822 /************************************************
4824 * SAVING AND RESTORING PARTIAL PARSE STATE *
4826 ************************************************/
4829 save_parser_state
(ps
)
4830 sh_parser_state_t
*ps
;
4832 #if defined (ARRAY_VARS)
4837 ps
= (sh_parser_state_t
*)xmalloc
(sizeof
(sh_parser_state_t
));
4839 return
((sh_parser_state_t
*)NULL
);
4841 ps
->parser_state
= parser_state
;
4842 ps
->token_state
= save_token_state
();
4844 ps
->input_line_terminator
= shell_input_line_terminator
;
4845 ps
->eof_encountered
= eof_encountered
;
4847 ps
->current_command_line_count
= current_command_line_count
;
4849 #if defined (HISTORY)
4850 ps
->remember_on_history
= remember_on_history
;
4851 # if defined (BANG_HISTORY)
4852 ps
->history_expansion_inhibited
= history_expansion_inhibited
;
4856 ps
->last_command_exit_value
= last_command_exit_value
;
4857 #if defined (ARRAY_VARS)
4858 v
= find_variable
("PIPESTATUS");
4859 if
(v
&& array_p
(v
) && array_cell
(v
))
4860 ps
->pipestatus
= array_copy
(array_cell
(v
));
4862 ps
->pipestatus
= (ARRAY
*)NULL
;
4865 ps
->last_shell_builtin
= last_shell_builtin
;
4866 ps
->this_shell_builtin
= this_shell_builtin
;
4868 ps
->expand_aliases
= expand_aliases
;
4869 ps
->echo_input_at_read
= echo_input_at_read
;
4875 restore_parser_state
(ps
)
4876 sh_parser_state_t
*ps
;
4878 #if defined (ARRAY_VARS)
4885 parser_state
= ps
->parser_state
;
4886 if
(ps
->token_state
)
4888 restore_token_state
(ps
->token_state
);
4889 free
(ps
->token_state
);
4892 shell_input_line_terminator
= ps
->input_line_terminator
;
4893 eof_encountered
= ps
->eof_encountered
;
4895 current_command_line_count
= ps
->current_command_line_count
;
4897 #if defined (HISTORY)
4898 remember_on_history
= ps
->remember_on_history
;
4899 # if defined (BANG_HISTORY)
4900 history_expansion_inhibited
= ps
->history_expansion_inhibited
;
4904 last_command_exit_value
= ps
->last_command_exit_value
;
4905 #if defined (ARRAY_VARS)
4906 v
= find_variable
("PIPESTATUS");
4907 if
(v
&& array_p
(v
) && array_cell
(v
))
4909 array_dispose
(array_cell
(v
));
4910 var_setarray
(v
, ps
->pipestatus
);
4914 last_shell_builtin
= ps
->last_shell_builtin
;
4915 this_shell_builtin
= ps
->this_shell_builtin
;
4917 expand_aliases
= ps
->expand_aliases
;
4918 echo_input_at_read
= ps
->echo_input_at_read
;
4921 /************************************************
4923 * MULTIBYTE CHARACTER HANDLING *
4925 ************************************************/
4927 #if defined (HANDLE_MULTIBYTE)
4931 int i
, previ
, len
, c
;
4932 mbstate_t mbs
, prevs
;
4935 if
(shell_input_line
== NULL
)
4937 len
= strlen
(shell_input_line
); /* XXX - shell_input_line_len ? */
4938 FREE
(shell_input_line_property
);
4939 shell_input_line_property
= (char *)xmalloc
(len
+ 1);
4941 memset
(&prevs
, '\0', sizeof
(mbstate_t));
4942 for
(i
= previ
= 0; i
< len
; i
++)
4946 c
= shell_input_line
[i
];
4950 for
(j
= i
; j
< len
; j
++)
4951 shell_input_line_property
[j
] = 1;
4955 mbclen
= mbrlen
(shell_input_line
+ previ
, i
- previ
+ 1, &mbs
);
4956 if
(mbclen
== 1 || mbclen
== (size_t)-1)
4961 else if
(mbclen
== (size_t)-2)
4963 else if
(mbclen
> 1)
4971 /* XXX - what to do if mbrlen returns 0? (null wide character) */
4973 for
(j
= i
; j
< len
; j
++)
4974 shell_input_line_property
[j
] = 1;
4978 shell_input_line_property
[i
] = mbclen
;
4981 #endif /* HANDLE_MULTIBYTE */