1 /* Yacc grammar for bash. */
3 /* Copyright (C) 1989 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "bashtypes.h"
27 #if defined (HAVE_UNISTD_H)
31 #if defined (HAVE_LOCALE_H)
44 #include "mailcheck.h"
45 #include "builtins/common.h"
46 #include "builtins/builtext.h"
48 #if defined (READLINE)
49 # include "bashline.h"
50 # include <readline/readline.h>
54 # include "bashhist.h"
55 # include <readline/history.h>
58 #if defined (JOB_CONTROL)
60 #endif /* JOB_CONTROL */
66 #if defined (PROMPT_STRING_DECODE)
68 # include <sys/param.h>
72 #endif /* PROMPT_STRING_DECODE */
74 #define RE_READ_TOKEN -99
75 #define NO_EXPANSION -100
79 #if defined (EXTENDED_GLOB)
80 #define PATTERN_CHAR(c) \
81 ((c) == '@' || (c) == '*' || (c) == '+' || (c) == '?' || (c) == '!')
83 extern int extended_glob;
86 extern int eof_encountered;
87 extern int no_line_editing, running_under_emacs;
88 extern int current_command_number;
89 extern int interactive, interactive_shell, login_shell;
90 extern int sourcelevel;
91 extern int posixly_correct;
92 extern int last_command_exit_value;
93 extern int interrupt_immediately;
94 extern char *shell_name, *current_host_name;
95 extern char *dist_version;
96 extern int patch_level;
97 extern int dump_translatable_strings, dump_po_strings;
98 extern Function *last_shell_builtin, *this_shell_builtin;
99 #if defined (BUFFERED_INPUT)
100 extern int bash_input_fd_changed;
103 /* **************************************************************** */
105 /* "Forward" declarations */
107 /* **************************************************************** */
109 static char *ansiexpand ();
110 static char *localeexpand ();
111 static int reserved_word_acceptable ();
112 static int read_token ();
114 static int parse_arith_cmd ();
115 #if defined (COND_COMMAND)
116 static COMMAND *parse_cond_command ();
118 static int read_token_word ();
119 static void discard_parser_constructs ();
121 static void report_syntax_error ();
122 static void handle_eof_input_unit ();
123 static void prompt_again ();
125 static void reset_readline_prompt ();
127 static void print_prompt ();
129 extern int yyerror ();
131 /* Default prompt strings */
132 char *primary_prompt = PPROMPT;
133 char *secondary_prompt = SPROMPT;
135 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
136 char *ps1_prompt, *ps2_prompt;
138 /* Handle on the current prompt string. Indirectly points through
139 ps1_ or ps2_prompt. */
140 char **prompt_string_pointer = (char **)NULL;
141 char *current_prompt_string;
143 /* Non-zero means we expand aliases in commands. */
144 int expand_aliases = 0;
146 /* If non-zero, the decoded prompt string undergoes parameter and
147 variable substitution, command substitution, arithmetic substitution,
148 string expansion, process substitution, and quote removal in
149 decode_prompt_string. */
152 /* The decoded prompt string. Used if READLINE is not defined or if
153 editing is turned off. Analogous to current_readline_prompt. */
154 static char *current_decoded_prompt;
156 /* The number of lines read from input while creating the current command. */
157 int current_command_line_count;
159 /* Variables to manage the task of reading here documents, because we need to
160 defer the reading until after a complete command has been collected. */
161 static REDIRECT *redir_stack[10];
164 /* Where shell input comes from. History expansion is performed on each
165 line when the shell is interactive. */
166 static char *shell_input_line = (char *)NULL;
167 static int shell_input_line_index;
168 static int shell_input_line_size; /* Amount allocated for shell_input_line. */
169 static int shell_input_line_len; /* strlen (shell_input_line) */
171 /* Either zero or EOF. */
172 static int shell_input_line_terminator;
174 /* The line number in a script on which a function definition starts. */
175 static int function_dstart;
177 /* The line number in a script on which a function body starts. */
178 static int function_bstart;
180 static REDIRECTEE redir;
184 WORD_DESC *word; /* the word that we read. */
185 int number; /* the number that we read. */
186 WORD_LIST *word_list;
190 PATTERN_LIST *pattern;
193 /* Reserved words. Members of the first group are only recognized
194 in the case that they are preceded by a list_terminator. Members
195 of the second group are for [[...]] commands. Members of the
196 third group are recognized only under special circumstances. */
197 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION
198 %token COND_START COND_END COND_ERROR
199 %token IN BANG TIME TIMEOPT
201 /* More general tokens. yylex () knows how to make these. */
202 %token <word> WORD ASSIGNMENT_WORD
203 %token <number> NUMBER
204 %token <word_list> ARITH_CMD
205 %token <command> COND_CMD
206 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND
207 %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
210 /* The types that the various syntactical units return. */
212 %type <command> inputunit command pipeline pipeline_command
213 %type <command> list list0 list1 compound_list simple_list simple_list1
214 %type <command> simple_command shell_command
215 %type <command> for_command select_command case_command group_command
216 %type <command> arith_command
217 %type <command> cond_command
218 %type <command> function_def if_command elif_clause subshell
219 %type <redirect> redirection redirection_list
220 %type <element> simple_command_element
221 %type <word_list> word_list pattern
222 %type <pattern> pattern_list case_clause_sequence case_clause
223 %type <number> timespec
227 %left '&' ';' '\n' yacc_EOF
232 inputunit: simple_list '\n'
234 /* Case of regular command. Discard the error
235 safety net,and return the command just parsed. */
238 discard_parser_constructs (0);
243 /* Case of regular command, but not a very
244 interesting one. Return a NULL command. */
245 global_command = (COMMAND *)NULL;
250 /* Error during parsing. Return NULL command. */
251 global_command = (COMMAND *)NULL;
253 discard_parser_constructs (1);
265 /* Case of EOF seen by itself. Do ignoreeof or
267 global_command = (COMMAND *)NULL;
268 handle_eof_input_unit ();
274 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
276 { $$ = make_word_list ($2, $1); }
279 redirection: '>' WORD
282 $$ = make_redirection (1, r_output_direction, redir);
287 $$ = make_redirection (0, r_input_direction, redir);
292 $$ = make_redirection ($1, r_output_direction, redir);
297 $$ = make_redirection ($1, r_input_direction, redir);
299 | GREATER_GREATER WORD
302 $$ = make_redirection (1, r_appending_to, redir);
304 | NUMBER GREATER_GREATER WORD
307 $$ = make_redirection ($1, r_appending_to, redir);
312 $$ = make_redirection (0, r_reading_until, redir);
313 redir_stack[need_here_doc++] = $$;
315 | NUMBER LESS_LESS WORD
318 $$ = make_redirection ($1, r_reading_until, redir);
319 redir_stack[need_here_doc++] = $$;
324 $$ = make_redirection (0, r_duplicating_input, redir);
326 | NUMBER LESS_AND NUMBER
329 $$ = make_redirection ($1, r_duplicating_input, redir);
334 $$ = make_redirection (1, r_duplicating_output, redir);
336 | NUMBER GREATER_AND NUMBER
339 $$ = make_redirection ($1, r_duplicating_output, redir);
344 $$ = make_redirection (0, r_duplicating_input_word, redir);
346 | NUMBER LESS_AND WORD
349 $$ = make_redirection ($1, r_duplicating_input_word, redir);
354 $$ = make_redirection (1, r_duplicating_output_word, redir);
356 | NUMBER GREATER_AND WORD
359 $$ = make_redirection ($1, r_duplicating_output_word, redir);
361 | LESS_LESS_MINUS WORD
364 $$ = make_redirection
365 (0, r_deblank_reading_until, redir);
366 redir_stack[need_here_doc++] = $$;
368 | NUMBER LESS_LESS_MINUS WORD
371 $$ = make_redirection
372 ($1, r_deblank_reading_until, redir);
373 redir_stack[need_here_doc++] = $$;
378 $$ = make_redirection (1, r_close_this, redir);
380 | NUMBER GREATER_AND '-'
383 $$ = make_redirection ($1, r_close_this, redir);
388 $$ = make_redirection (0, r_close_this, redir);
390 | NUMBER LESS_AND '-'
393 $$ = make_redirection ($1, r_close_this, redir);
398 $$ = make_redirection (1, r_err_and_out, redir);
400 | NUMBER LESS_GREATER WORD
403 $$ = make_redirection ($1, r_input_output, redir);
408 $$ = make_redirection (0, r_input_output, redir);
413 $$ = make_redirection (1, r_output_force, redir);
415 | NUMBER GREATER_BAR WORD
418 $$ = make_redirection ($1, r_output_force, redir);
422 simple_command_element: WORD
423 { $$.word = $1; $$.redirect = 0; }
425 { $$.word = $1; $$.redirect = 0; }
427 { $$.redirect = $1; $$.word = 0; }
430 redirection_list: redirection
434 | redirection_list redirection
436 register REDIRECT *t;
438 for (t = $1; t->next; t = t->next)
445 simple_command: simple_command_element
446 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
447 | simple_command simple_command_element
448 { $$ = make_simple_command ($2, $1); }
451 command: simple_command
452 { $$ = clean_simple_command ($1); }
455 | shell_command redirection_list
460 /* According to Posix.2 3.9.5, redirections
461 specified after the body of a function should
462 be attached to the function and performed when
463 the function is executed, not as part of the
464 function definition command. */
465 if (tc->type == cm_function_def)
467 tc = tc->value.Function_def->command;
468 if (tc->type == cm_group)
469 tc = tc->value.Group->command;
473 register REDIRECT *t;
474 for (t = tc->redirects; t->next; t = t->next)
484 shell_command: for_command
488 | WHILE compound_list DO compound_list DONE
489 { $$ = make_while_command ($2, $4); }
490 | UNTIL compound_list DO compound_list DONE
491 { $$ = make_until_command ($2, $4); }
508 for_command: FOR WORD newline_list DO compound_list DONE
509 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5); }
510 | FOR WORD newline_list '{' compound_list '}'
511 { $$ = make_for_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5); }
512 | FOR WORD ';' newline_list DO compound_list DONE
513 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
514 | FOR WORD ';' newline_list '{' compound_list '}'
515 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
516 | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
517 { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
518 | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
519 { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
522 select_command: SELECT WORD newline_list DO list DONE
524 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5);
526 | SELECT WORD newline_list '{' list '}'
528 $$ = make_select_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5);
530 | SELECT WORD ';' newline_list DO list DONE
532 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
534 | SELECT WORD ';' newline_list '{' list '}'
536 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
538 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
540 $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
542 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
544 $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
548 case_command: CASE WORD newline_list IN newline_list ESAC
549 { $$ = make_case_command ($2, (PATTERN_LIST *)NULL); }
550 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
551 { $$ = make_case_command ($2, $5); }
552 | CASE WORD newline_list IN case_clause ESAC
553 { $$ = make_case_command ($2, $5); }
556 function_def: WORD '(' ')' newline_list group_command
557 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
560 | FUNCTION WORD '(' ')' newline_list group_command
561 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
563 | FUNCTION WORD newline_list group_command
564 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
567 subshell: '(' compound_list ')'
568 { $2->flags |= CMD_WANT_SUBSHELL; $$ = $2; }
571 if_command: IF compound_list THEN compound_list FI
572 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
573 | IF compound_list THEN compound_list ELSE compound_list FI
574 { $$ = make_if_command ($2, $4, $6); }
575 | IF compound_list THEN compound_list elif_clause FI
576 { $$ = make_if_command ($2, $4, $5); }
580 group_command: '{' list '}'
581 { $$ = make_group_command ($2); }
584 arith_command: ARITH_CMD
585 { $$ = make_arith_command ($1); }
588 cond_command: COND_START COND_CMD COND_END
592 elif_clause: ELIF compound_list THEN compound_list
593 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
594 | ELIF compound_list THEN compound_list ELSE compound_list
595 { $$ = make_if_command ($2, $4, $6); }
596 | ELIF compound_list THEN compound_list elif_clause
597 { $$ = make_if_command ($2, $4, $5); }
600 case_clause: pattern_list
601 | case_clause_sequence pattern_list
602 { $2->next = $1; $$ = $2; }
605 pattern_list: newline_list pattern ')' compound_list
606 { $$ = make_pattern_list ($2, $4); }
607 | newline_list pattern ')' newline_list
608 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
609 | newline_list '(' pattern ')' compound_list
610 { $$ = make_pattern_list ($3, $5); }
611 | newline_list '(' pattern ')' newline_list
612 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
615 case_clause_sequence: pattern_list SEMI_SEMI
616 | case_clause_sequence pattern_list SEMI_SEMI
617 { $2->next = $1; $$ = $2; }
621 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
623 { $$ = make_word_list ($3, $1); }
626 /* A list allows leading or trailing newlines and
627 newlines as operators (equivalent to semicolons).
628 It must end with a newline or semicolon.
629 Lists are used within commands such as if, for, while. */
631 list: newline_list list0
635 gather_here_documents ();
646 list0: list1 '\n' newline_list
647 | list1 '&' newline_list
649 if ($1->type == cm_connection)
650 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
652 $$ = command_connect ($1, (COMMAND *)NULL, '&');
654 | list1 ';' newline_list
658 list1: list1 AND_AND newline_list list1
659 { $$ = command_connect ($1, $4, AND_AND); }
660 | list1 OR_OR newline_list list1
661 { $$ = command_connect ($1, $4, OR_OR); }
662 | list1 '&' newline_list list1
664 if ($1->type == cm_connection)
665 $$ = connect_async_list ($1, $4, '&');
667 $$ = command_connect ($1, $4, '&');
669 | list1 ';' newline_list list1
670 { $$ = command_connect ($1, $4, ';'); }
671 | list1 '\n' newline_list list1
672 { $$ = command_connect ($1, $4, ';'); }
686 /* A simple_list is a list that contains no significant newlines
687 and no leading or trailing newlines. Newlines are allowed
688 only following operators, where they are not significant.
690 This is what an inputunit consists of. */
692 simple_list: simple_list1
696 gather_here_documents ();
700 if ($1->type == cm_connection)
701 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
703 $$ = command_connect ($1, (COMMAND *)NULL, '&');
705 gather_here_documents ();
711 gather_here_documents ();
715 simple_list1: simple_list1 AND_AND newline_list simple_list1
716 { $$ = command_connect ($1, $4, AND_AND); }
717 | simple_list1 OR_OR newline_list simple_list1
718 { $$ = command_connect ($1, $4, OR_OR); }
719 | simple_list1 '&' simple_list1
721 if ($1->type == cm_connection)
722 $$ = connect_async_list ($1, $3, '&');
724 $$ = command_connect ($1, $3, '&');
726 | simple_list1 ';' simple_list1
727 { $$ = command_connect ($1, $3, ';'); }
733 pipeline_command: pipeline
737 $2->flags |= CMD_INVERT_RETURN;
745 | timespec BANG pipeline
750 | BANG timespec pipeline
752 $3->flags |= $2|CMD_INVERT_RETURN;
758 pipeline '|' newline_list pipeline
759 { $$ = command_connect ($1, $4, '|'); }
765 { $$ = CMD_TIME_PIPELINE; }
767 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
771 /* Possible states for the parser that require it to do special things. */
772 #define PST_CASEPAT 0x001 /* in a case pattern list */
773 #define PST_ALEXPNEXT 0x002 /* expand next word for aliases */
774 #define PST_ALLOWOPNBRC 0x004 /* allow open brace for function def */
775 #define PST_NEEDCLOSBRC 0x008 /* need close brace */
776 #define PST_DBLPAREN 0x010 /* double-paren parsing */
777 #define PST_SUBSHELL 0x020 /* ( ... ) subshell */
778 #define PST_CMDSUBST 0x040 /* $( ... ) command substitution */
779 #define PST_CASESTMT 0x080 /* parsing a case statement */
780 #define PST_CONDCMD 0x100 /* parsing a [[...]] command */
781 #define PST_CONDEXPR 0x200 /* parsing the guts of [[...]] */
783 /* Initial size to allocate for tokens, and the
784 amount to grow them by. */
785 #define TOKEN_DEFAULT_INITIAL_SIZE 496
786 #define TOKEN_DEFAULT_GROW_SIZE 512
788 /* Shell meta-characters that, when unquoted, separate words. */
789 #define shellmeta(c) (strchr ("()<>;&|", (c)) != 0)
790 #define shellbreak(c) (strchr ("()<>;&| \t\n", (c)) != 0)
791 #define shellquote(c) ((c) == '"' || (c) == '`' || (c) == '\'')
792 #define shellexp(c) ((c) == '$' || (c) == '<' || (c) == '>')
794 /* The token currently being read. */
795 static int current_token;
797 /* The last read token, or NULL. read_token () uses this for context
799 static int last_read_token;
801 /* The token read prior to last_read_token. */
802 static int token_before_that;
804 /* The token read prior to token_before_that. */
805 static int two_tokens_ago;
807 /* If non-zero, it is the token that we want read_token to return
808 regardless of what text is (or isn't) present to be read. This
809 is reset by read_token. If token_to_read == WORD or
810 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
811 static int token_to_read;
812 static WORD_DESC *word_desc_to_read;
814 /* The current parser state. */
815 static int parser_state;
817 /* Global var is non-zero when end of file has been reached. */
829 /* yy_getc () returns the next available character from input or EOF.
830 yy_ungetc (c) makes `c' the next character to read.
831 init_yy_io (get, unget, type, location) makes the function GET the
832 installed function for getting the next character, makes UNGET the
833 installed function for un-getting a character, sets the type of stream
834 (either string or file) from TYPE, and makes LOCATION point to where
835 the input is coming from. */
837 /* Unconditionally returns end-of-file. */
844 /* Variable containing the current get and unget functions.
845 See ./input.h for a clearer description. */
846 BASH_INPUT bash_input;
848 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
849 is non-null, avoiding a memory leak. */
851 initialize_bash_input ()
853 bash_input.type = st_none;
854 FREE (bash_input.name);
855 bash_input.name = (char *)NULL;
856 bash_input.location.file = (FILE *)NULL;
857 bash_input.location.string = (char *)NULL;
858 bash_input.getter = (Function *)NULL;
859 bash_input.ungetter = (Function *)NULL;
862 /* Set the contents of the current bash input stream from
863 GET, UNGET, TYPE, NAME, and LOCATION. */
865 init_yy_io (get, unget, type, name, location)
866 Function *get, *unget;
867 enum stream_type type;
869 INPUT_STREAM location;
871 bash_input.type = type;
872 FREE (bash_input.name);
873 bash_input.name = name ? savestring (name) : (char *)NULL;
877 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
879 bash_input.location = location;
881 bash_input.getter = get;
882 bash_input.ungetter = unget;
885 /* Call this to get the next character of input. */
889 return (*(bash_input.getter)) ();
892 /* Call this to unget C. That is, to make C the next character
898 return (*(bash_input.ungetter)) (c);
901 #if defined (BUFFERED_INPUT)
903 input_file_descriptor ()
905 switch (bash_input.type)
908 return (fileno (bash_input.location.file));
910 return (bash_input.location.buffered_fd);
913 return (fileno (stdin));
916 #endif /* BUFFERED_INPUT */
918 /* **************************************************************** */
920 /* Let input be read from readline (). */
922 /* **************************************************************** */
924 #if defined (READLINE)
925 char *current_readline_prompt = (char *)NULL;
926 char *current_readline_line = (char *)NULL;
927 int current_readline_line_index = 0;
932 SigHandler *old_sigint;
935 if (!current_readline_line)
937 if (!bash_readline_initialized)
938 initialize_readline ();
940 #if defined (JOB_CONTROL)
942 give_terminal_to (shell_pgrp);
943 #endif /* JOB_CONTROL */
945 if (signal_is_ignored (SIGINT) == 0)
947 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
948 interrupt_immediately++;
951 current_readline_line = readline (current_readline_prompt ?
952 current_readline_prompt : "");
954 if (signal_is_ignored (SIGINT) == 0)
956 interrupt_immediately--;
957 set_signal_handler (SIGINT, old_sigint);
961 /* Reset the prompt to the decoded value of prompt_string_pointer. */
962 reset_readline_prompt ();
965 if (current_readline_line == 0)
968 current_readline_line_index = 0;
969 line_len = strlen (current_readline_line);
971 current_readline_line = xrealloc (current_readline_line, 2 + line_len);
972 current_readline_line[line_len++] = '\n';
973 current_readline_line[line_len] = '\0';
976 if (current_readline_line[current_readline_line_index] == 0)
978 free (current_readline_line);
979 current_readline_line = (char *)NULL;
980 return (yy_readline_get ());
984 c = (unsigned char)current_readline_line[current_readline_line_index++];
990 yy_readline_unget (c)
993 if (current_readline_line_index && current_readline_line)
994 current_readline_line[--current_readline_line_index] = c;
999 with_input_from_stdin ()
1001 INPUT_STREAM location;
1003 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
1005 location.string = current_readline_line;
1006 init_yy_io (yy_readline_get, yy_readline_unget,
1007 st_stdin, "readline stdin", location);
1011 #else /* !READLINE */
1014 with_input_from_stdin ()
1016 with_input_from_stream (stdin, "stdin");
1018 #endif /* !READLINE */
1020 /* **************************************************************** */
1022 /* Let input come from STRING. STRING is zero terminated. */
1024 /* **************************************************************** */
1029 register char *string;
1032 string = bash_input.location.string;
1035 /* If the string doesn't exist, or is empty, EOF found. */
1036 if (string && *string)
1038 c = *(unsigned char *)string++;
1039 bash_input.location.string = string;
1048 *(--bash_input.location.string) = c;
1053 with_input_from_string (string, name)
1054 char *string, *name;
1056 INPUT_STREAM location;
1058 location.string = string;
1059 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1062 /* **************************************************************** */
1064 /* Let input come from STREAM. */
1066 /* **************************************************************** */
1073 if (bash_input.location.file)
1075 #if !defined (HAVE_RESTARTABLE_SYSCALLS)
1076 result = getc_with_restart (bash_input.location.file);
1077 #else /* HAVE_RESTARTABLE_SYSCALLS */
1078 result = getc (bash_input.location.file);
1079 result = (feof (bash_input.location.file)) ? EOF : (unsigned char)result;
1080 #endif /* HAVE_RESTARTABLE_SYSCALLS */
1089 #if !defined (HAVE_RESTARTABLE_SYSCALLS)
1090 return (ungetc_with_restart (c, bash_input.location.file));
1091 #else /* HAVE_RESTARTABLE_SYSCALLS */
1092 return (ungetc (c, bash_input.location.file));
1093 #endif /* HAVE_RESTARTABLE_SYSCALLS */
1097 with_input_from_stream (stream, name)
1101 INPUT_STREAM location;
1103 location.file = stream;
1104 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1107 typedef struct stream_saver {
1108 struct stream_saver *next;
1109 BASH_INPUT bash_input;
1111 #if defined (BUFFERED_INPUT)
1112 BUFFERED_STREAM *bstream;
1113 #endif /* BUFFERED_INPUT */
1116 /* The globally known line number. */
1117 int line_number = 0;
1119 #if defined (COND_COMMAND)
1120 static int cond_lineno;
1121 static int cond_token;
1124 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1127 push_stream (reset_lineno)
1130 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1132 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1134 #if defined (BUFFERED_INPUT)
1135 saver->bstream = (BUFFERED_STREAM *)NULL;
1136 /* If we have a buffered stream, clear out buffers[fd]. */
1137 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1138 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
1139 (BUFFERED_STREAM *)NULL);
1140 #endif /* BUFFERED_INPUT */
1142 saver->line = line_number;
1143 bash_input.name = (char *)NULL;
1144 saver->next = stream_list;
1145 stream_list = saver;
1158 STREAM_SAVER *saver = stream_list;
1161 stream_list = stream_list->next;
1163 init_yy_io (saver->bash_input.getter,
1164 saver->bash_input.ungetter,
1165 saver->bash_input.type,
1166 saver->bash_input.name,
1167 saver->bash_input.location);
1169 #if defined (BUFFERED_INPUT)
1170 /* If we have a buffered stream, restore buffers[fd]. */
1171 /* If the input file descriptor was changed while this was on the
1172 save stack, update the buffered fd to the new file descriptor and
1173 re-establish the buffer <-> bash_input fd correspondence. */
1174 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1176 if (bash_input_fd_changed)
1178 bash_input_fd_changed = 0;
1179 if (default_buffered_input >= 0)
1181 bash_input.location.buffered_fd = default_buffered_input;
1182 saver->bstream->b_fd = default_buffered_input;
1185 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
1187 #endif /* BUFFERED_INPUT */
1189 line_number = saver->line;
1191 FREE (saver->bash_input.name);
1196 /* Return 1 if a stream of type TYPE is saved on the stack. */
1198 stream_on_stack (type)
1199 enum stream_type type;
1201 register STREAM_SAVER *s;
1203 for (s = stream_list; s; s = s->next)
1204 if (s->bash_input.type == type)
1210 * This is used to inhibit alias expansion and reserved word recognition
1211 * inside case statement pattern lists. A `case statement pattern list' is:
1213 * everything between the `in' in a `case word in' and the next ')'
1215 * everything between a `;;' and the next `)' or `esac'
1218 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1220 #if !defined (ALIAS)
1221 typedef void *alias_t;
1224 #define END_OF_ALIAS 0
1227 * Pseudo-global variables used in implementing token-wise alias expansion.
1231 * Pushing and popping strings. This works together with shell_getc to
1232 * implement alias expansion on a per-token basis.
1235 typedef struct string_saver {
1236 struct string_saver *next;
1237 int expand_alias; /* Value to set expand_alias to when string is popped. */
1240 alias_t *expander; /* alias that caused this line to be pushed. */
1242 int saved_line_size, saved_line_index, saved_line_terminator;
1245 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1248 * Push the current shell_input_line onto a stack of such lines and make S
1249 * the current input. Used when expanding aliases. EXPAND is used to set
1250 * the value of expand_next_token when the string is popped, so that the
1251 * word after the alias in the original line is handled correctly when the
1252 * alias expands to multiple words. TOKEN is the token that was expanded
1253 * into S; it is saved and used to prevent infinite recursive expansion.
1256 push_string (s, expand, ap)
1261 STRING_SAVER *temp = (STRING_SAVER *) xmalloc (sizeof (STRING_SAVER));
1263 temp->expand_alias = expand;
1264 temp->saved_line = shell_input_line;
1265 temp->saved_line_size = shell_input_line_size;
1266 temp->saved_line_index = shell_input_line_index;
1267 temp->saved_line_terminator = shell_input_line_terminator;
1269 temp->expander = ap;
1271 temp->next = pushed_string_list;
1272 pushed_string_list = temp;
1276 ap->flags |= AL_BEINGEXPANDED;
1279 shell_input_line = s;
1280 shell_input_line_size = strlen (s);
1281 shell_input_line_index = 0;
1282 shell_input_line_terminator = '\0';
1283 parser_state &= ~PST_ALEXPNEXT;
1287 * Make the top of the pushed_string stack be the current shell input.
1288 * Only called when there is something on the stack. Called from shell_getc
1289 * when it thinks it has consumed the string generated by an alias expansion
1290 * and needs to return to the original input line.
1297 FREE (shell_input_line);
1298 shell_input_line = pushed_string_list->saved_line;
1299 shell_input_line_index = pushed_string_list->saved_line_index;
1300 shell_input_line_size = pushed_string_list->saved_line_size;
1301 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1303 if (pushed_string_list->expand_alias)
1304 parser_state |= PST_ALEXPNEXT;
1306 parser_state &= ~PST_ALEXPNEXT;
1308 t = pushed_string_list;
1309 pushed_string_list = pushed_string_list->next;
1313 t->expander->flags &= ~AL_BEINGEXPANDED;
1322 register STRING_SAVER *t, *t1;
1324 for (t = pushed_string_list; t; )
1327 FREE (t->saved_line);
1330 t->expander->flags &= ~AL_BEINGEXPANDED;
1335 pushed_string_list = (STRING_SAVER *)NULL;
1338 #endif /* ALIAS || DPAREN_ARITHMETIC */
1340 /* Return a line of text, taken from wherever yylex () reads input.
1341 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1342 is non-zero, we remove unquoted \<newline> pairs. This is used by
1343 read_secondary_line to read here documents. */
1345 read_a_line (remove_quoted_newline)
1346 int remove_quoted_newline;
1348 static char *line_buffer = (char *)NULL;
1349 static int buffer_size = 0;
1350 int indx = 0, c, peekc, pass_next;
1352 #if defined (READLINE)
1353 if (interactive && bash_input.type != st_string && no_line_editing)
1355 if (interactive && bash_input.type != st_string)
1364 /* Allow immediate exit if interrupted during input. */
1370 /* If there is no more input, then we return NULL. */
1373 if (interactive && bash_input.type == st_stream)
1376 return ((char *)NULL);
1380 /* `+2' in case the final character in the buffer is a newline. */
1381 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
1383 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
1384 here document with an unquoted delimiter. In this case,
1385 the line will be expanded as if it were in double quotes.
1386 We allow a backslash to escape the next character, but we
1387 need to treat the backslash specially only if a backslash
1388 quoting a backslash-newline pair appears in the line. */
1391 line_buffer[indx++] = c;
1394 else if (c == '\\' && remove_quoted_newline)
1398 continue; /* Make the unquoted \<newline> pair disappear. */
1403 line_buffer[indx++] = c; /* Preserve the backslash. */
1407 line_buffer[indx++] = c;
1411 line_buffer[indx] = '\0';
1412 return (line_buffer);
1417 /* Return a line as in read_a_line (), but insure that the prompt is
1418 the secondary prompt. This is used to read the lines of a here
1419 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
1420 newlines quoted with backslashes while reading the line. It is
1421 non-zero unless the delimiter of the here document was quoted. */
1423 read_secondary_line (remove_quoted_newline)
1424 int remove_quoted_newline;
1426 prompt_string_pointer = &ps2_prompt;
1428 return (read_a_line (remove_quoted_newline));
1431 /* **************************************************************** */
1435 /* **************************************************************** */
1437 /* Reserved words. These are only recognized as the first word of a
1439 STRING_INT_ALIST word_token_alist[] = {
1448 #if defined (SELECT_COMMAND)
1449 { "select", SELECT },
1456 { "function", FUNCTION },
1457 #if defined (COMMAND_TIMING)
1463 #if defined (COND_COMMAND)
1464 { "[[", COND_START },
1470 /* These are used by read_token_word, but appear up here so that shell_getc
1471 can use them to decide when to add otherwise blank lines to the history. */
1473 /* The primary delimiter stack. */
1474 struct dstack dstack = { (char *)NULL, 0, 0 };
1476 /* A temporary delimiter stack to be used when decoding prompt strings.
1477 This is needed because command substitutions in prompt strings (e.g., PS2)
1478 can screw up the parser's quoting state. */
1479 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
1481 /* Macro for accessing the top delimiter on the stack. Returns the
1482 delimiter or zero if none. */
1483 #define current_delimiter(ds) \
1484 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
1486 #define push_delimiter(ds, character) \
1489 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
1490 ds.delimiters = xrealloc \
1491 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
1492 ds.delimiters[ds.delimiter_depth] = character; \
1493 ds.delimiter_depth++; \
1497 #define pop_delimiter(ds) ds.delimiter_depth--
1499 /* Return the next shell input character. This always reads characters
1500 from shell_input_line; when that line is exhausted, it is time to
1501 read the next line. This is called by read_token when the shell is
1502 processing normal command input. */
1505 shell_getc (remove_quoted_newline)
1506 int remove_quoted_newline;
1510 static int mustpop = 0;
1514 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1515 /* If shell_input_line[shell_input_line_index] == 0, but there is
1516 something on the pushed list of strings, then we don't want to go
1517 off and get another line. We let the code down below handle it. */
1519 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
1520 (pushed_string_list == (STRING_SAVER *)NULL)))
1521 #else /* !ALIAS && !DPAREN_ARITHMETIC */
1522 if (!shell_input_line || !shell_input_line[shell_input_line_index])
1523 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
1529 /* Allow immediate exit if interrupted during input. */
1533 shell_input_line_terminator = 0;
1535 #if defined (JOB_CONTROL)
1536 /* This can cause a problem when reading a command as the result
1537 of a trap, when the trap is called from flush_child. This call
1538 had better not cause jobs to disappear from the job table in
1539 that case, or we will have big trouble. */
1540 notify_and_cleanup ();
1541 #else /* !JOB_CONTROL */
1542 cleanup_dead_jobs ();
1543 #endif /* !JOB_CONTROL */
1545 #if defined (READLINE)
1546 if (interactive && bash_input.type != st_string && no_line_editing)
1548 if (interactive && bash_input.type != st_string)
1552 if (bash_input.type == st_stream)
1555 while (c = yy_getc ())
1557 /* Allow immediate exit if interrupted during input. */
1560 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
1564 if (bash_input.type == st_stream)
1568 shell_input_line_terminator = EOF;
1570 shell_input_line[i] = '\0';
1574 shell_input_line[i++] = c;
1578 shell_input_line[--i] = '\0';
1579 current_command_line_count++;
1583 shell_input_line_index = 0;
1584 shell_input_line_len = i; /* == strlen (shell_input_line) */
1586 #if defined (HISTORY)
1587 if (remember_on_history && shell_input_line && shell_input_line[0])
1590 # if defined (BANG_HISTORY)
1593 /* If the current delimiter is a single quote, we should not be
1594 performing history expansion, even if we're on a different
1595 line from the original single quote. */
1596 old_hist = history_expansion_inhibited;
1597 if (current_delimiter (dstack) == '\'')
1598 history_expansion_inhibited = 1;
1600 expansions = pre_process_line (shell_input_line, 1, 1);
1601 # if defined (BANG_HISTORY)
1602 history_expansion_inhibited = old_hist;
1604 if (expansions != shell_input_line)
1606 free (shell_input_line);
1607 shell_input_line = expansions;
1608 shell_input_line_len = shell_input_line ?
1609 strlen (shell_input_line) : 0;
1610 if (!shell_input_line_len)
1611 current_command_line_count--;
1613 /* We have to force the xrealloc below because we don't know
1614 the true allocated size of shell_input_line anymore. */
1615 shell_input_line_size = shell_input_line_len;
1618 /* XXX - this is grotesque */
1619 else if (remember_on_history && shell_input_line &&
1620 shell_input_line[0] == '\0' &&
1621 current_command_line_count > 1 && current_delimiter (dstack))
1623 /* We know shell_input_line[0] == 0 and we're reading some sort of
1624 quoted string. This means we've got a line consisting of only
1625 a newline in a quoted string. We want to make sure this line
1626 gets added to the history. */
1627 maybe_add_history (shell_input_line);
1630 #endif /* HISTORY */
1632 if (shell_input_line)
1634 /* Lines that signify the end of the shell's input should not be
1636 if (echo_input_at_read && (shell_input_line[0] ||
1637 shell_input_line_terminator != EOF))
1638 fprintf (stderr, "%s\n", shell_input_line);
1642 shell_input_line_size = 0;
1643 prompt_string_pointer = ¤t_prompt_string;
1648 /* Add the newline to the end of this string, iff the string does
1649 not already end in an EOF character. */
1650 if (shell_input_line_terminator != EOF)
1652 if (shell_input_line_len + 3 > shell_input_line_size)
1653 shell_input_line = xrealloc (shell_input_line,
1654 1 + (shell_input_line_size += 2));
1656 shell_input_line[shell_input_line_len] = '\n';
1657 shell_input_line[shell_input_line_len + 1] = '\0';
1661 c = shell_input_line[shell_input_line_index];
1664 shell_input_line_index++;
1666 if (c == '\\' && remove_quoted_newline &&
1667 shell_input_line[shell_input_line_index] == '\n')
1674 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
1675 /* If C is NULL, we have reached the end of the current input string. If
1676 pushed_string_list is non-empty, it's time to pop to the previous string
1677 because we have fully consumed the result of the last alias expansion.
1678 Do it transparently; just return the next character of the string popped
1680 if (!c && (pushed_string_list != (STRING_SAVER *)NULL))
1685 c = shell_input_line[shell_input_line_index];
1687 shell_input_line_index++;
1696 #endif /* ALIAS || DPAREN_ARITHMETIC */
1698 if (!c && shell_input_line_terminator == EOF)
1699 return ((shell_input_line_index != 0) ? '\n' : EOF);
1701 return ((unsigned char)c);
1704 /* Put C back into the input for the shell. */
1709 if (shell_input_line && shell_input_line_index)
1710 shell_input_line[--shell_input_line_index] = c;
1716 if (shell_input_line && shell_input_line_index)
1717 shell_input_line_index--;
1720 /* Discard input until CHARACTER is seen, then push that character back
1721 onto the input stream. */
1723 discard_until (character)
1728 while ((c = shell_getc (0)) != EOF && c != character)
1736 execute_prompt_command (command)
1739 Function *temp_last, *temp_this;
1741 int temp_exit_value, temp_eof_encountered;
1743 temp_last = last_shell_builtin;
1744 temp_this = this_shell_builtin;
1745 temp_exit_value = last_command_exit_value;
1746 temp_eof_encountered = eof_encountered;
1747 last_lastarg = get_string_value ("_");
1749 last_lastarg = savestring (last_lastarg);
1751 parse_and_execute (savestring (command), "PROMPT_COMMAND", SEVAL_NONINT|SEVAL_NOHIST);
1753 last_shell_builtin = temp_last;
1754 this_shell_builtin = temp_this;
1755 last_command_exit_value = temp_exit_value;
1756 eof_encountered = temp_eof_encountered;
1758 bind_variable ("_", last_lastarg);
1759 FREE (last_lastarg);
1761 if (token_to_read == '\n') /* reset_parser was called */
1765 /* Place to remember the token. We try to keep the buffer
1766 at a reasonable size, but it can grow. */
1767 static char *token = (char *)NULL;
1769 /* Current size of the token buffer. */
1770 static int token_buffer_size;
1772 /* Command to read_token () explaining what we want it to do. */
1775 #define prompt_is_ps1 \
1776 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
1778 /* Function for yyparse to call. yylex keeps track of
1779 the last two tokens read, and calls read_token. */
1783 if (interactive && (current_token == 0 || current_token == '\n'))
1785 /* Before we print a prompt, we might have to check mailboxes.
1786 We do this only if it is time to do so. Notice that only here
1787 is the mail alarm reset; nothing takes place in check_mail ()
1788 except the checking of mail. Please don't change this. */
1789 if (prompt_is_ps1 && time_to_check_mail ())
1792 reset_mail_timer ();
1795 /* Avoid printing a prompt if we're not going to read anything, e.g.
1796 after resetting the parser with read_token (RESET). */
1797 if (token_to_read == 0 && interactive)
1801 two_tokens_ago = token_before_that;
1802 token_before_that = last_read_token;
1803 last_read_token = current_token;
1804 current_token = read_token (READ);
1805 return (current_token);
1808 /* When non-zero, we have read the required tokens
1809 which allow ESAC to be the next one read. */
1810 static int esacs_needed_count;
1813 gather_here_documents ()
1816 while (need_here_doc)
1818 make_here_document (redir_stack[r++]);
1823 /* When non-zero, an open-brace used to create a group is awaiting a close
1825 static int open_brace_count;
1827 #define command_token_position(token) \
1828 (((token) == ASSIGNMENT_WORD) || \
1829 ((token) != SEMI_SEMI && reserved_word_acceptable(token)))
1831 #define assignment_acceptable(token) command_token_position(token) && \
1832 ((parser_state & PST_CASEPAT) == 0)
1834 /* Check to see if TOKEN is a reserved word and return the token
1836 #define CHECK_FOR_RESERVED_WORD(tok) \
1838 if (!dollar_present && !quoted && \
1839 reserved_word_acceptable (last_read_token)) \
1842 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
1843 if (STREQ (tok, word_token_alist[i].word)) \
1845 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
1847 if (word_token_alist[i].token == TIME) \
1849 if (word_token_alist[i].token == ESAC) \
1850 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
1851 else if (word_token_alist[i].token == CASE) \
1852 parser_state |= PST_CASESTMT; \
1853 else if (word_token_alist[i].token == COND_END) \
1854 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
1855 else if (word_token_alist[i].token == COND_START) \
1856 parser_state |= PST_CONDCMD; \
1857 else if (word_token_alist[i].token == '{') \
1858 open_brace_count++; \
1859 else if (word_token_alist[i].token == '}' && open_brace_count) \
1860 open_brace_count--; \
1861 return (word_token_alist[i].token); \
1868 /* OK, we have a token. Let's try to alias expand it, if (and only if)
1871 It is eligible for expansion if the shell is in interactive mode, and
1872 the token is unquoted and the last token read was a command
1873 separator (or expand_next_token is set), and we are currently
1874 processing an alias (pushed_string_list is non-empty) and this
1875 token is not the same as the current or any previously
1878 Special cases that disqualify:
1879 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
1881 alias_expand_token (token)
1887 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
1888 (parser_state & PST_CASEPAT) == 0)
1890 ap = find_alias (token);
1892 /* Currently expanding this token. */
1893 if (ap && (ap->flags & AL_BEINGEXPANDED))
1894 return (NO_EXPANSION);
1896 expanded = ap ? savestring (ap->value) : (char *)NULL;
1899 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
1900 return (RE_READ_TOKEN);
1903 /* This is an eligible token that does not have an expansion. */
1904 return (NO_EXPANSION);
1906 return (NO_EXPANSION);
1911 time_command_acceptable ()
1913 #if defined (COMMAND_TIMING)
1914 switch (last_read_token)
1928 #endif /* COMMAND_TIMING */
1931 /* Handle special cases of token recognition:
1932 IN is recognized if the last token was WORD and the token
1933 before that was FOR or CASE or SELECT.
1935 DO is recognized if the last token was WORD and the token
1936 before that was FOR or SELECT.
1938 ESAC is recognized if the last token caused `esacs_needed_count'
1941 `{' is recognized if the last token as WORD and the token
1942 before that was FUNCTION.
1944 `}' is recognized if there is an unclosed `{' prsent.
1946 `-p' is returned as TIMEOPT if the last read token was TIME.
1948 ']]' is returned as COND_END if the parser is currently parsing
1949 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
1951 `time' is returned as TIME if and only if it is immediately
1952 preceded by one of `;', `\n', `||', `&&', or `&'.
1956 special_case_tokens (token)
1959 if ((last_read_token == WORD) &&
1960 #if defined (SELECT_COMMAND)
1961 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
1963 ((token_before_that == FOR) || (token_before_that == CASE)) &&
1965 (token[0] == 'i' && token[1] == 'n' && token[2] == 0))
1967 if (token_before_that == CASE)
1969 parser_state |= PST_CASEPAT;
1970 esacs_needed_count++;
1975 if (last_read_token == WORD &&
1976 #if defined (SELECT_COMMAND)
1977 (token_before_that == FOR || token_before_that == SELECT) &&
1979 (token_before_that == FOR) &&
1981 (token[0] == 'd' && token[1] == 'o' && token[2] == '\0'))
1984 /* Ditto for ESAC in the CASE case.
1985 Specifically, this handles "case word in esac", which is a legal
1986 construct, certainly because someone will pass an empty arg to the
1987 case construct, and we don't want it to barf. Of course, we should
1988 insist that the case construct has at least one pattern in it, but
1989 the designers disagree. */
1990 if (esacs_needed_count)
1992 esacs_needed_count--;
1993 if (STREQ (token, "esac"))
1995 parser_state &= ~PST_CASEPAT;
2000 /* The start of a shell function definition. */
2001 if (parser_state & PST_ALLOWOPNBRC)
2003 parser_state &= ~PST_ALLOWOPNBRC;
2004 if (token[0] == '{' && token[1] == '\0') /* } */
2007 function_bstart = line_number;
2008 return ('{'); /* } */
2012 if (open_brace_count && reserved_word_acceptable (last_read_token) && token[0] == '}' && !token[1])
2014 open_brace_count--; /* { */
2018 #if defined (COMMAND_TIMING)
2019 /* Handle -p after `time'. */
2020 if (last_read_token == TIME && token[0] == '-' && token[1] == 'p' && !token[2])
2024 #if defined (COMMAND_TIMING)
2025 if (STREQ (token, "time") && time_command_acceptable ())
2027 #endif /* COMMAND_TIMING */
2029 #if defined (COND_COMMAND) /* [[ */
2030 if ((parser_state & PST_CONDEXPR) && token[0] == ']' && token[1] == ']' && token[2] == '\0')
2037 /* Called from shell.c when Control-C is typed at top level. Or
2038 by the error rule at top level. */
2042 dstack.delimiter_depth = 0; /* No delimiters found so far. */
2043 open_brace_count = 0;
2047 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2048 if (pushed_string_list)
2049 free_string_list ();
2050 #endif /* ALIAS || DPAREN_ARITHMETIC */
2052 if (shell_input_line)
2054 free (shell_input_line);
2055 shell_input_line = (char *)NULL;
2056 shell_input_line_size = shell_input_line_index = 0;
2059 FREE (word_desc_to_read);
2060 word_desc_to_read = (WORD_DESC *)NULL;
2062 last_read_token = '\n';
2063 token_to_read = '\n';
2066 /* Read the next token. Command can be READ (normal operation) or
2067 RESET (to normalize state). */
2069 read_token (command)
2072 int character; /* Current character. */
2073 int peek_char; /* Temporary look-ahead character. */
2074 int result; /* The thing to return. */
2076 if (command == RESET)
2084 result = token_to_read;
2085 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
2087 yylval.word = word_desc_to_read;
2088 word_desc_to_read = (WORD_DESC *)NULL;
2094 #if defined (COND_COMMAND)
2095 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
2097 cond_lineno = line_number;
2098 parser_state |= PST_CONDEXPR;
2099 yylval.command = parse_cond_command ();
2100 if (cond_token != COND_END)
2102 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
2103 parser_error (cond_lineno, "unexpected EOF while looking for `]]'");
2104 else if (cond_token != COND_ERROR)
2105 parser_error (cond_lineno, "syntax error in conditional expression");
2108 token_to_read = COND_END;
2109 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
2115 /* This is a place to jump back to once we have successfully expanded a
2116 token with an alias and pushed the string with push_string () */
2120 /* Read a single word from input. Start by skipping blanks. */
2121 while ((character = shell_getc (1)) != EOF && whitespace (character))
2124 if (character == EOF)
2130 if (character == '#' && (!interactive || interactive_comments))
2132 /* A comment. Discard until EOL or EOF, and then return a newline. */
2133 discard_until ('\n');
2135 character = '\n'; /* this will take the next if statement and return. */
2138 if (character == '\n')
2140 /* If we're about to return an unquoted newline, we can go and collect
2141 the text of any pending here document. */
2143 gather_here_documents ();
2146 parser_state &= ~PST_ALEXPNEXT;
2152 /* Shell meta-characters. */
2153 if (shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
2156 /* Turn off alias tokenization iff this character sequence would
2157 not leave us ready to read a command. */
2158 if (character == '<' || character == '>')
2159 parser_state &= ~PST_ALEXPNEXT;
2162 peek_char = shell_getc (1);
2163 if (character == peek_char)
2168 /* If '<' then we could be at "<<" or at "<<-". We have to
2169 look ahead one more character. */
2170 peek_char = shell_getc (1);
2171 if (peek_char == '-')
2172 return (LESS_LESS_MINUS);
2175 shell_ungetc (peek_char);
2180 return (GREATER_GREATER);
2183 parser_state |= PST_CASEPAT;
2185 parser_state &= ~PST_ALEXPNEXT;
2195 #if defined (DPAREN_ARITHMETIC)
2197 if (reserved_word_acceptable (last_read_token))
2203 sline = line_number;
2204 cmdtyp = parse_arith_cmd (&wval);
2205 if (cmdtyp == 1) /* arithmetic command */
2207 wd = make_word (wval);
2208 wd->flags = W_QUOTED;
2209 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
2210 free (wval); /* make_word copies it */
2213 else if (cmdtyp == 0) /* nested subshell */
2215 push_string (wval, 0, (alias_t *)NULL);
2216 if ((parser_state & PST_CASEPAT) == 0)
2217 parser_state |= PST_SUBSHELL;
2227 else if (character == '<' && peek_char == '&')
2229 else if (character == '>' && peek_char == '&')
2230 return (GREATER_AND);
2231 else if (character == '<' && peek_char == '>')
2232 return (LESS_GREATER);
2233 else if (character == '>' && peek_char == '|')
2234 return (GREATER_BAR);
2235 else if (peek_char == '>' && character == '&')
2236 return (AND_GREATER);
2238 shell_ungetc (peek_char);
2240 /* If we look like we are reading the start of a function
2241 definition, then let the reader know about it so that
2242 we will do the right thing with `{'. */
2243 if (character == ')' && last_read_token == '(' && token_before_that == WORD)
2245 parser_state |= PST_ALLOWOPNBRC;
2247 parser_state &= ~PST_ALEXPNEXT;
2249 function_dstart = line_number;
2252 /* case pattern lists may be preceded by an optional left paren. If
2253 we're not trying to parse a case pattern list, the left paren
2254 indicates a subshell. */
2255 if (character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
2256 parser_state |= PST_SUBSHELL;
2258 else if ((parser_state & PST_CASEPAT) && character == ')')
2259 parser_state &= ~PST_CASEPAT;
2261 else if ((parser_state & PST_SUBSHELL) && character == ')')
2262 parser_state &= ~PST_SUBSHELL;
2264 #if defined (PROCESS_SUBSTITUTION)
2265 /* Check for the constructs which introduce process substitution.
2266 Shells running in `posix mode' don't do process substitution. */
2267 if (posixly_correct ||
2268 ((character != '>' && character != '<') || peek_char != '('))
2269 #endif /* PROCESS_SUBSTITUTION */
2273 /* Hack <&- (close stdin) case. */
2274 if (character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
2277 /* Okay, if we got this far, we have to read a word. Read one,
2278 and then check it against the known ones. */
2279 result = read_token_word (character);
2281 if (result == RE_READ_TOKEN)
2287 /* Match a $(...) or other grouping construct. This has to handle embedded
2288 quoted strings ('', ``, "") and nested constructs. It also must handle
2289 reprompting the user, if necessary, after reading a newline, and returning
2290 correct error values if it reads EOF. */
2291 static char matched_pair_error;
2293 parse_matched_pair (qc, open, close, lenp, flags)
2294 int qc; /* `"' if this construct is within double quotes */
2298 int count, ch, was_dollar;
2299 int pass_next_character, nestlen, start_lineno;
2300 char *ret, *nestret;
2301 int retind, retsize;
2304 pass_next_character = was_dollar = 0;
2306 ret = xmalloc (retsize = 64);
2309 start_lineno = line_number;
2312 ch = shell_getc (qc != '\'' && pass_next_character == 0);
2316 parser_error (start_lineno, "unexpected EOF while looking for matching `%c'", close);
2317 EOF_Reached = 1; /* XXX */
2318 return (&matched_pair_error);
2321 /* Possible reprompting. */
2322 if (ch == '\n' && interactive &&
2323 (bash_input.type == st_stdin || bash_input.type == st_stream))
2326 if (pass_next_character) /* last char was backslash */
2328 pass_next_character = 0;
2329 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
2331 if (retind > 0) retind--; /* swallow previously-added backslash */
2335 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2336 if (ch == CTLESC || ch == CTLNUL)
2337 ret[retind++] = CTLESC;
2341 else if (ch == CTLESC || ch == CTLNUL) /* special shell escapes */
2343 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2344 ret[retind++] = CTLESC;
2348 else if (ch == close) /* ending delimiter */
2350 else if (ch == open) /* nested begin */
2353 /* Add this character. */
2354 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
2357 if (open == '\'') /* '' inside grouping construct */
2360 if (ch == '\\') /* backslashes */
2361 pass_next_character++;
2363 if (open != close) /* a grouping construct */
2365 if (shellquote (ch))
2367 /* '', ``, or "" inside $(...) or other grouping construct. */
2368 push_delimiter (dstack, ch);
2369 nestret = parse_matched_pair (ch, ch, ch, &nestlen, 0);
2370 pop_delimiter (dstack);
2371 if (nestret == &matched_pair_error)
2374 return &matched_pair_error;
2378 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2379 strcpy (ret + retind, nestret);
2385 /* Parse an old-style command substitution within double quotes as a
2387 /* XXX - sh and ksh93 don't do this - XXX */
2388 else if (open == '"' && ch == '`')
2390 nestret = parse_matched_pair (0, '`', '`', &nestlen, 0);
2391 if (nestret == &matched_pair_error)
2394 return &matched_pair_error;
2398 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2399 strcpy (ret + retind, nestret);
2404 else if (was_dollar && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
2405 /* check for $(), $[], or ${} inside quoted string. */
2407 if (open == ch) /* undo previous increment */
2409 if (ch == '(') /* ) */
2410 nestret = parse_matched_pair (0, '(', ')', &nestlen, 0);
2411 else if (ch == '{') /* } */
2412 nestret = parse_matched_pair (0, '{', '}', &nestlen, 0);
2413 else if (ch == '[') /* ] */
2414 nestret = parse_matched_pair (0, '[', ']', &nestlen, 0);
2415 if (nestret == &matched_pair_error)
2418 return &matched_pair_error;
2422 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2423 strcpy (ret + retind, nestret);
2428 was_dollar = (ch == '$');
2437 #if defined (DPAREN_ARITHMETIC)
2438 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
2439 If not, assume it's a nested subshell for backwards compatibility and
2440 return 0. In any case, put the characters we've consumed into a locally-
2441 allocated buffer and make *ep point to that buffer. Return -1 on an
2442 error, for example EOF. */
2444 parse_arith_cmd (ep)
2447 int exp_lineno, rval, c;
2451 exp_lineno = line_number;
2452 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
2454 if (ttok == &matched_pair_error)
2456 /* Check that the next character is the closing right paren. If
2457 not, this is a syntax error. ( */
2458 if ((c = shell_getc (0)) != ')')
2461 token = xmalloc(ttoklen + 4);
2463 /* (( ... )) -> "..." */
2464 token[0] = (rval == 1) ? '"' : '(';
2465 strncpy (token + 1, ttok, ttoklen - 1); /* don't copy the final `)' */
2468 token[ttoklen] = '"';
2469 token[ttoklen+1] = '\0';
2473 token[ttoklen] = ')';
2474 token[ttoklen+1] = c;
2475 token[ttoklen+2] = '\0';
2481 #endif /* DPAREN_ARITHMETIC */
2483 #if defined (COND_COMMAND)
2484 static COND_COM *cond_term ();
2485 static COND_COM *cond_and ();
2486 static COND_COM *cond_or ();
2487 static COND_COM *cond_expr ();
2492 return (cond_or ());
2501 if (cond_token == OR_OR)
2504 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
2515 if (cond_token == AND_AND)
2518 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
2524 cond_skip_newlines ()
2526 while ((cond_token = read_token (READ)) == '\n')
2528 if (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
2531 return (cond_token);
2534 #define COND_RETURN_ERROR() \
2535 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
2541 COND_COM *term, *tleft, *tright;
2544 /* Read a token. It can be a left paren, a `!', a unary operator, or a
2545 word that should be the first argument of a binary operator. Start by
2546 skipping newlines, since this is a compound command. */
2547 tok = cond_skip_newlines ();
2548 lineno = line_number;
2549 if (tok == COND_END)
2551 COND_RETURN_ERROR ();
2553 else if (tok == '(')
2555 term = cond_expr ();
2556 if (cond_token != ')')
2559 dispose_cond_node (term); /* ( */
2560 parser_error (lineno, "expected `)'");
2561 COND_RETURN_ERROR ();
2563 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
2564 (void)cond_skip_newlines ();
2566 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
2569 dispose_word (yylval.word); /* not needed */
2570 term = cond_term ();
2572 term->flags |= CMD_INVERT_RETURN;
2574 else if (tok == WORD && test_unop (yylval.word->word))
2577 tok = read_token (READ);
2580 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
2581 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
2586 parser_error (line_number, "unexpected argument to conditional unary operator");
2587 COND_RETURN_ERROR ();
2590 (void)cond_skip_newlines ();
2592 else /* left argument to binary operator */
2595 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
2598 tok = read_token (READ);
2599 if (tok == WORD && test_binop (yylval.word->word))
2601 else if (tok == '<' || tok == '>')
2602 op = make_word_from_token (tok);
2603 else if (tok == COND_END || tok == AND_AND || tok == OR_OR)
2605 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
2606 the test command. Similarly for [[ x && expr ]] or
2608 op = make_word ("-n");
2609 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
2615 parser_error (line_number, "conditional binary operator expected");
2616 dispose_cond_node (tleft);
2617 COND_RETURN_ERROR ();
2621 tok = read_token (READ);
2624 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
2625 term = make_cond_node (COND_BINARY, op, tleft, tright);
2629 parser_error (line_number, "unexpected argument to conditional binary operator");
2630 dispose_cond_node (tleft);
2632 COND_RETURN_ERROR ();
2635 (void)cond_skip_newlines ();
2640 /* This is kind of bogus -- we slip a mini recursive-descent parser in
2641 here to handle the conditional statement syntax. */
2643 parse_cond_command ()
2647 cexp = cond_expr ();
2648 return (make_cond_command (cexp));
2653 read_token_word (character)
2656 /* The value for YYLVAL when a WORD is read. */
2657 WORD_DESC *the_word;
2659 /* Index into the token that we are building. */
2662 /* ALL_DIGITS becomes zero when we see a non-digit. */
2665 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
2668 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
2671 /* Non-zero means to ignore the value of the next character, and just
2672 to add it no matter what. */
2673 int pass_next_character;
2675 /* The current delimiting character. */
2677 int result, peek_char;
2678 char *ttok, *ttrans;
2679 int ttoklen, ttranslen;
2681 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
2682 token = xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
2685 all_digits = digit (character);
2686 dollar_present = quoted = pass_next_character = 0;
2690 if (character == EOF)
2693 if (pass_next_character)
2695 pass_next_character = 0;
2699 cd = current_delimiter (dstack);
2701 /* Handle backslashes. Quote lots of things when not inside of
2702 double-quotes, quote some things inside of double-quotes. */
2703 if (character == '\\')
2705 peek_char = shell_getc (0);
2707 /* Backslash-newline is ignored in all cases except
2708 when quoted with single quotes. */
2709 if (peek_char == '\n')
2712 goto next_character;
2716 shell_ungetc (peek_char);
2718 /* If the next character is to be quoted, note it now. */
2719 if (cd == 0 || cd == '`' ||
2720 (cd == '"' && member (peek_char, slashify_in_quotes)))
2721 pass_next_character++;
2728 /* Parse a matched pair of quote characters. */
2729 if (shellquote (character))
2731 push_delimiter (dstack, character);
2732 ttok = parse_matched_pair (character, character, character, &ttoklen, 0);
2733 pop_delimiter (dstack);
2734 if (ttok == &matched_pair_error)
2735 return -1; /* Bail immediately. */
2736 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2737 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
2738 token[token_index++] = character;
2739 strcpy (token + token_index, ttok);
2740 token_index += ttoklen;
2743 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
2745 goto next_character;
2748 #ifdef EXTENDED_GLOB
2749 /* Parse a ksh-style extended pattern matching specification. */
2750 if (extended_glob && PATTERN_CHAR(character))
2752 peek_char = shell_getc (1);
2753 if (peek_char == '(') /* ) */
2755 push_delimiter (dstack, peek_char);
2756 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
2757 pop_delimiter (dstack);
2758 if (ttok == &matched_pair_error)
2759 return -1; /* Bail immediately. */
2760 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2762 TOKEN_DEFAULT_GROW_SIZE);
2763 token[token_index++] = character;
2764 token[token_index++] = peek_char;
2765 strcpy (token + token_index, ttok);
2766 token_index += ttoklen;
2768 dollar_present = all_digits = 0;
2769 goto next_character;
2772 shell_ungetc (peek_char);
2774 #endif /* EXTENDED_GLOB */
2776 /* If the delimiter character is not single quote, parse some of
2777 the shell expansions that must be read as a single word. */
2778 #if defined (PROCESS_SUBSTITUTION)
2779 if (character == '$' || character == '<' || character == '>')
2781 if (character == '$')
2782 #endif /* !PROCESS_SUBSTITUTION */
2784 peek_char = shell_getc (1);
2785 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
2786 if (peek_char == '(' ||
2787 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
2789 if (peek_char == '{') /* } */
2790 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, 0);
2791 else if (peek_char == '(') /* ) */
2793 /* XXX - push and pop the `(' as a delimiter for use by
2794 the command-oriented-history code. This way newlines
2795 appearing in the $(...) string get added to the
2796 history literally rather than causing a possibly-
2797 incorrect `;' to be added. */
2798 push_delimiter (dstack, peek_char);
2799 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
2800 pop_delimiter (dstack);
2803 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
2804 if (ttok == &matched_pair_error)
2805 return -1; /* Bail immediately. */
2806 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2808 TOKEN_DEFAULT_GROW_SIZE);
2809 token[token_index++] = character;
2810 token[token_index++] = peek_char;
2811 strcpy (token + token_index, ttok);
2812 token_index += ttoklen;
2816 goto next_character;
2818 /* This handles $'...' and $"..." new-style quoted strings. */
2819 else if (character == '$' && (peek_char == '\'' || peek_char == '"'))
2823 first_line = line_number;
2824 ttok = parse_matched_pair (peek_char, peek_char, peek_char, &ttoklen, 0);
2825 if (ttok == &matched_pair_error)
2827 if (peek_char == '\'')
2828 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
2830 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
2832 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2,
2834 TOKEN_DEFAULT_GROW_SIZE);
2835 token[token_index++] = peek_char;
2836 strcpy (token + token_index, ttrans);
2837 token_index += ttranslen;
2838 token[token_index++] = peek_char;
2842 goto next_character;
2845 shell_ungetc (peek_char);
2848 #if defined (ARRAY_VARS)
2849 /* Identify possible compound array variable assignment. */
2850 else if (character == '=' && token_index > 0)
2852 peek_char = shell_getc (1);
2853 if (peek_char == '(') /* ) */
2855 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
2856 if (ttok == &matched_pair_error)
2857 return -1; /* Bail immediately. */
2858 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2860 TOKEN_DEFAULT_GROW_SIZE);
2861 token[token_index++] = character;
2862 token[token_index++] = peek_char;
2863 strcpy (token + token_index, ttok);
2864 token_index += ttoklen;
2867 goto next_character;
2870 shell_ungetc (peek_char);
2874 /* When not parsing a multi-character word construct, shell meta-
2875 characters break words. */
2876 if (shellbreak (character))
2878 shell_ungetc (character);
2884 all_digits &= digit (character);
2885 dollar_present |= character == '$';
2887 if (character == CTLESC || character == CTLNUL)
2888 token[token_index++] = CTLESC;
2890 token[token_index++] = character;
2892 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
2893 TOKEN_DEFAULT_GROW_SIZE);
2896 if (character == '\n' && interactive &&
2897 (bash_input.type == st_stdin || bash_input.type == st_stream))
2900 /* We want to remove quoted newlines (that is, a \<newline> pair)
2901 unless we are within single quotes or pass_next_character is
2902 set (the shell equivalent of literal-next). */
2903 cd = current_delimiter (dstack);
2904 character = shell_getc (cd != '\'' && pass_next_character == 0);
2905 } /* end for (;;) */
2909 token[token_index] = '\0';
2911 /* Check to see what thing we should return. If the last_read_token
2912 is a `<', or a `&', or the character which ended this token is
2913 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
2914 Otherwise, it is just a word, and should be returned as such. */
2915 if (all_digits && (character == '<' || character == '>' ||
2916 last_read_token == LESS_AND ||
2917 last_read_token == GREATER_AND))
2919 yylval.number = atoi (token);
2923 /* Check for special case tokens. */
2924 result = special_case_tokens (token);
2929 /* Posix.2 does not allow reserved words to be aliased, so check for all
2930 of them, including special cases, before expanding the current token
2932 if (posixly_correct)
2933 CHECK_FOR_RESERVED_WORD (token);
2935 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
2936 inhibits alias expansion. */
2937 if (expand_aliases && quoted == 0)
2939 result = alias_expand_token (token);
2940 if (result == RE_READ_TOKEN)
2941 return (RE_READ_TOKEN);
2942 else if (result == NO_EXPANSION)
2943 parser_state &= ~PST_ALEXPNEXT;
2946 /* If not in Posix.2 mode, check for reserved words after alias
2948 if (posixly_correct == 0)
2950 CHECK_FOR_RESERVED_WORD (token);
2952 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
2953 the_word->word = xmalloc (1 + token_index);
2954 the_word->flags = 0;
2955 strcpy (the_word->word, token);
2957 the_word->flags |= W_HASDOLLAR;
2959 the_word->flags |= W_QUOTED;
2960 /* A word is an assignment if it appears at the beginning of a
2961 simple command, or after another assignment word. This is
2962 context-dependent, so it cannot be handled in the grammar. */
2963 if (assignment (token))
2965 the_word->flags |= W_ASSIGNMENT;
2966 /* Don't perform word splitting on assignment statements. */
2967 if (assignment_acceptable (last_read_token))
2968 the_word->flags |= W_NOSPLIT;
2971 yylval.word = the_word;
2973 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
2974 ? ASSIGNMENT_WORD : WORD;
2976 if (last_read_token == FUNCTION)
2978 parser_state |= PST_ALLOWOPNBRC;
2979 function_dstart = line_number;
2985 /* $'...' ANSI-C expand the portion of STRING between START and END and
2986 return the result. The result cannot be longer than the input string. */
2988 ansiexpand (string, start, end, lenp)
2990 int start, end, *lenp;
2995 temp = xmalloc (end - start + 1);
2996 for (tlen = 0, len = start; len < end; )
2997 temp[tlen++] = string[len++];
3002 t = ansicstr (temp, tlen, (int *)NULL, lenp);
3014 /* $"..." -- Translate the portion of STRING between START and END
3015 according to current locale using gettext (if available) and return
3016 the result. The caller will take care of leaving the quotes intact.
3017 The string will be left without the leading `$' by the caller.
3018 If translation is performed, the translated string will be double-quoted
3019 by the caller. The length of the translated string is returned in LENP,
3022 localeexpand (string, start, end, lineno, lenp)
3024 int start, end, lineno, *lenp;
3029 temp = xmalloc (end - start + 1);
3030 for (tlen = 0, len = start; len < end; )
3031 temp[tlen++] = string[len++];
3034 /* If we're just dumping translatable strings, don't do anything. */
3035 if (dump_translatable_strings)
3037 if (dump_po_strings)
3038 printf ("#: %s:%d\nmsgid \"%s\"\nmsgstr \"\"\n",
3039 (bash_input.name ? bash_input.name : "stdin"), lineno, temp);
3041 printf ("\"%s\"\n", temp);
3048 t = localetrans (temp, tlen, &len);
3062 /* Return 1 if TOKEN is a token that after being read would allow
3063 a reserved word to be seen, else 0. */
3065 reserved_word_acceptable (token)
3068 if (token == '\n' || token == ';' || token == '(' || token == ')' ||
3069 token == '|' || token == '&' || token == '{' ||
3070 token == '}' || /* XXX */
3073 token == TIME || token == TIMEOPT ||
3080 token == SEMI_SEMI ||
3084 token == DONE || /* XXX these two are experimental */
3092 /* Return the index of TOKEN in the alist of reserved words, or -1 if
3093 TOKEN is not a shell reserved word. */
3095 find_reserved_word (token)
3099 for (i = 0; word_token_alist[i].word; i++)
3100 if (STREQ (token, word_token_alist[i].word))
3106 #if defined (READLINE)
3107 /* Called after each time readline is called. This insures that whatever
3108 the new prompt string is gets propagated to readline's local prompt
3111 reset_readline_prompt ()
3115 if (prompt_string_pointer)
3117 temp_prompt = (*prompt_string_pointer)
3118 ? decode_prompt_string (*prompt_string_pointer)
3121 if (temp_prompt == 0)
3123 temp_prompt = xmalloc (1);
3124 temp_prompt[0] = '\0';
3127 FREE (current_readline_prompt);
3128 current_readline_prompt = temp_prompt;
3131 #endif /* READLINE */
3134 #if defined (HISTORY)
3135 /* A list of tokens which can be followed by newlines, but not by
3136 semi-colons. When concatenating multiple lines of history, the
3137 newline separator for such tokens is replaced with a space. */
3138 static int no_semi_successors[] = {
3139 '\n', '{', '(', ')', ';', '&', '|',
3140 CASE, DO, ELSE, IF, SEMI_SEMI, THEN, UNTIL, WHILE, AND_AND, OR_OR, IN,
3144 /* If we are not within a delimited expression, try to be smart
3145 about which separators can be semi-colons and which must be
3146 newlines. Returns the string that should be added into the
3149 history_delimiting_chars ()
3153 if (dstack.delimiter_depth != 0)
3156 /* First, handle some special cases. */
3158 /* If we just read `()', assume it's a function definition, and don't
3159 add a semicolon. If the token before the `)' was not `(', and we're
3160 not in the midst of parsing a case statement, assume it's a
3161 parenthesized command and add the semicolon. */
3163 if (token_before_that == ')')
3165 if (two_tokens_ago == '(') /*)*/ /* function def */
3167 /* This does not work for subshells inside case statement
3168 command lists. It's a suboptimal solution. */
3169 else if (parser_state & PST_CASESTMT) /* case statement pattern */
3172 return "; "; /* (...) subshell */
3174 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
3175 return " "; /* function def using `function name' without `()' */
3177 for (i = 0; no_semi_successors[i]; i++)
3179 if (token_before_that == no_semi_successors[i])
3185 #endif /* HISTORY */
3187 /* Issue a prompt, or prepare to issue a prompt when the next character
3194 if (!interactive) /* XXX */
3197 ps1_prompt = get_string_value ("PS1");
3198 ps2_prompt = get_string_value ("PS2");
3200 if (!prompt_string_pointer)
3201 prompt_string_pointer = &ps1_prompt;
3203 temp_prompt = *prompt_string_pointer
3204 ? decode_prompt_string (*prompt_string_pointer)
3207 if (temp_prompt == 0)
3209 temp_prompt = xmalloc (1);
3210 temp_prompt[0] = '\0';
3213 current_prompt_string = *prompt_string_pointer;
3214 prompt_string_pointer = &ps2_prompt;
3216 #if defined (READLINE)
3217 if (!no_line_editing)
3219 FREE (current_readline_prompt);
3220 current_readline_prompt = temp_prompt;
3223 #endif /* READLINE */
3225 FREE (current_decoded_prompt);
3226 current_decoded_prompt = temp_prompt;
3233 fprintf (stderr, "%s", current_decoded_prompt);
3237 /* Return a string which will be printed as a prompt. The string
3238 may contain special characters which are decoded as follows:
3241 \e escape (ascii 033)
3242 \d the date in Day Mon Date format
3243 \h the hostname up to the first `.'
3246 \s the name of the shell
3247 \t the time in 24-hour hh:mm:ss format
3248 \T the time in 12-hour hh:mm:ss format
3249 \@ the time in 12-hour am/pm format
3250 \v the version of bash (e.g., 2.00)
3251 \V the release of bash, version + patchlevel (e.g., 2.00.0)
3252 \w the current working directory
3253 \W the last element of $PWD
3255 \# the command number of this command
3256 \! the history number of this command
3257 \$ a $ or a # if you are root
3258 \nnn character code nnn in octal
3260 \[ begin a sequence of non-printing chars
3261 \] end a sequence of non-printing chars
3263 #define PROMPT_GROWTH 48
3265 decode_prompt_string (string)
3270 struct dstack save_dstack;
3271 #if defined (PROMPT_STRING_DECODE)
3272 int result_size, result_index;
3274 char *temp, octal_string[4];
3277 result = xmalloc (result_size = PROMPT_GROWTH);
3278 result[result_index = 0] = 0;
3279 temp = (char *)NULL;
3281 while (c = *string++)
3283 if (posixly_correct && c == '!')
3287 temp = savestring ("!");
3292 #if !defined (HISTORY)
3293 temp = savestring ("1");
3295 temp = itos (history_number ());
3296 #endif /* HISTORY */
3297 string--; /* add_string increments string again. */
3315 strncpy (octal_string, string, 3);
3316 octal_string[3] = '\0';
3318 n = read_octal (octal_string);
3321 if (n == CTLESC || n == CTLNUL)
3347 /* Make the current time/date into a string. */
3348 the_time = time (0);
3349 temp = ctime (&the_time);
3351 temp = (c != 'd') ? savestring (temp + 11) : savestring (temp);
3352 temp[(c != 'd') ? 8 : 10] = '\0';
3354 /* quick and dirty conversion to 12-hour time */
3355 if (c == 'T' || c == '@')
3359 temp[5] = 'a'; /* am/pm format */
3370 temp[0] = (n / 10) + '0';
3371 temp[1] = (n % 10) + '0';
3373 if (n >= 0 && temp[5] == 'a')
3386 temp[0] = no_line_editing ? '\n' : '\r';
3387 temp[1] = no_line_editing ? '\0' : '\n';
3392 temp = base_pathname (shell_name);
3393 temp = savestring (temp);
3400 strcpy (temp, dist_version);
3402 sprintf (temp, "%s.%d", dist_version, patch_level);
3408 /* Use the value of PWD because it is much more efficient. */
3409 char t_string[PATH_MAX];
3412 temp = get_string_value ("PWD");
3416 if (getcwd (t_string, sizeof(t_string)) == 0)
3422 tlen = strlen (t_string);
3426 tlen = sizeof (t_string) - 1;
3427 strncpy (t_string, temp, tlen);
3429 t_string[tlen] = '\0';
3433 t = strrchr (t_string, '/');
3434 if (t && t != t_string)
3435 strcpy (t_string, t + 1);
3438 /* polite_directory_format is guaranteed to return a string
3439 no longer than PATH_MAX - 1 characters. */
3440 strcpy (t_string, polite_directory_format (t_string));
3442 /* If we're going to be expanding the prompt string later,
3443 quote the directory name. */
3444 if (promptvars || posixly_correct)
3445 temp = backslash_quote (t_string);
3447 temp = savestring (t_string);
3453 temp = savestring (current_user.user_name);
3458 temp = savestring (current_host_name);
3459 if (c == 'h' && (t = (char *)strchr (temp, '.')))
3464 temp = itos (current_command_number);
3468 #if !defined (HISTORY)
3469 temp = savestring ("1");
3471 temp = itos (history_number ());
3472 #endif /* HISTORY */
3477 temp[0] = current_user.euid == 0 ? '#' : '$';
3481 #if defined (READLINE)
3486 temp[1] = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
3489 #endif /* READLINE */
3500 temp[0] = (c == 'a') ? '\07' : '\033';
3514 sub_append_string (temp, result, &result_index, &result_size);
3515 temp = (char *)NULL; /* Freed in sub_append_string (). */
3516 result[result_index] = '\0';
3522 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
3523 result[result_index++] = c;
3524 result[result_index] = '\0';
3527 #else /* !PROMPT_STRING_DECODE */
3528 result = savestring (string);
3529 #endif /* !PROMPT_STRING_DECODE */
3531 /* Save the delimiter stack and point `dstack' to temp space so any
3532 command substitutions in the prompt string won't result in screwing
3533 up the parser's quoting state. */
3534 save_dstack = dstack;
3535 dstack = temp_dstack;
3536 dstack.delimiter_depth = 0;
3538 /* Perform variable and parameter expansion and command substitution on
3539 the prompt string. */
3540 if (promptvars || posixly_correct)
3542 list = expand_string_unsplit (result, Q_DOUBLE_QUOTES);
3544 result = string_list (list);
3545 dispose_words (list);
3549 t = dequote_string (result);
3554 dstack = save_dstack;
3559 /* Report a syntax error, and restart the parser. Call here for fatal
3564 report_syntax_error ((char *)NULL);
3569 /* Report a syntax error with line numbers, etc.
3570 Call here for recoverable errors. If you have a message to print,
3571 then place it in MESSAGE, otherwise pass NULL and this will figure
3572 out an appropriate message for you. */
3574 report_syntax_error (message)
3583 parser_error (line_number, "%s", message);
3584 if (interactive && EOF_Reached)
3586 last_command_exit_value = EX_USAGE;
3590 /* If the line of input we're reading is not null, try to find the
3591 objectionable token. */
3592 if (shell_input_line && *shell_input_line)
3594 t = shell_input_line;
3595 i = shell_input_line_index;
3598 if (i && t[i] == '\0')
3601 while (i && (whitespace (t[i]) || t[i] == '\n'))
3607 while (i && (member (t[i], " \n\t;|&") == 0))
3610 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
3613 /* Print the offending token. */
3614 if (token_end || (i == 0 && token_end == 0))
3618 msg = xmalloc (1 + (token_end - i));
3619 strncpy (msg, t + i, token_end - i);
3620 msg[token_end - i] = '\0';
3622 else /* one-character token */
3629 parser_error (line_number, "syntax error near unexpected token `%s'", msg);
3635 /* If not interactive, print the line containing the error. */
3636 if (interactive == 0)
3638 msg = savestring (shell_input_line);
3639 token_end = strlen (msg);
3640 while (token_end && msg[token_end - 1] == '\n')
3641 msg[--token_end] = '\0';
3643 parser_error (line_number, "`%s'", msg);
3649 msg = EOF_Reached ? "syntax error: unexpected end of file" : "syntax error";
3650 parser_error (line_number, "%s", msg);
3651 /* When the shell is interactive, this file uses EOF_Reached
3652 only for error reporting. Other mechanisms are used to
3653 decide whether or not to exit. */
3654 if (interactive && EOF_Reached)
3657 last_command_exit_value = EX_USAGE;
3660 /* ??? Needed function. ??? We have to be able to discard the constructs
3661 created during parsing. In the case of error, we want to return
3662 allocated objects to the memory pool. In the case of no error, we want
3663 to throw away the information about where the allocated objects live.
3664 (dispose_command () will actually free the command. */
3666 discard_parser_constructs (error_p)
3671 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
3673 /* A flag denoting whether or not ignoreeof is set. */
3676 /* The number of times that we have encountered an EOF character without
3677 another character intervening. When this gets above the limit, the
3678 shell terminates. */
3679 int eof_encountered = 0;
3681 /* The limit for eof_encountered. */
3682 int eof_encountered_limit = 10;
3684 /* If we have EOF as the only input unit, this user wants to leave
3685 the shell. If the shell is not interactive, then just leave.
3686 Otherwise, if ignoreeof is set, and we haven't done this the
3687 required number of times in a row, print a message. */
3689 handle_eof_input_unit ()
3693 /* shell.c may use this to decide whether or not to write out the
3694 history, among other things. We use it only for error reporting
3699 /* If the user wants to "ignore" eof, then let her do so, kind of. */
3702 if (eof_encountered < eof_encountered_limit)
3704 fprintf (stderr, "Use \"%s\" to leave the shell.\n",
3705 login_shell ? "logout" : "exit");
3707 /* Reset the prompt string to be $PS1. */
3708 prompt_string_pointer = (char **)NULL;
3710 last_read_token = current_token = '\n';
3715 /* In this case EOF should exit the shell. Do it now. */
3717 exit_builtin ((WORD_LIST *)NULL);
3721 /* We don't write history files, etc., for non-interactive shells. */