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)
67 #include <sys/param.h>
70 #endif /* PROMPT_STRING_DECODE */
72 #define RE_READ_TOKEN -99
73 #define NO_EXPANSION -100
77 extern int eof_encountered;
78 extern int no_line_editing, running_under_emacs;
79 extern int current_command_number;
80 extern int interactive, interactive_shell, login_shell;
81 extern int sourcelevel;
82 extern int posixly_correct;
83 extern int last_command_exit_value;
84 extern int interrupt_immediately;
85 extern char *shell_name, *current_host_name;
86 extern char *dist_version;
87 extern int patch_level;
88 extern int dump_translatable_strings;
89 extern Function *last_shell_builtin, *this_shell_builtin;
90 #if defined (BUFFERED_INPUT)
91 extern int bash_input_fd_changed;
94 /* **************************************************************** */
96 /* "Forward" declarations */
98 /* **************************************************************** */
100 static char *ansiexpand ();
101 static char *localeexpand ();
102 static int reserved_word_acceptable ();
103 static int read_token ();
105 static int read_token_word ();
106 static void discard_parser_constructs ();
108 static void report_syntax_error ();
109 static void handle_eof_input_unit ();
110 static void prompt_again ();
111 static void reset_readline_prompt ();
112 static void print_prompt ();
114 /* Default prompt strings */
115 char *primary_prompt = PPROMPT;
116 char *secondary_prompt = SPROMPT;
118 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
119 char *ps1_prompt, *ps2_prompt;
121 /* Handle on the current prompt string. Indirectly points through
122 ps1_ or ps2_prompt. */
123 char **prompt_string_pointer = (char **)NULL;
124 char *current_prompt_string;
126 /* Non-zero means we expand aliases in commands. */
127 int expand_aliases = 0;
129 /* If non-zero, the decoded prompt string undergoes parameter and
130 variable substitution, command substitution, arithmetic substitution,
131 string expansion, process substitution, and quote removal in
132 decode_prompt_string. */
135 /* The decoded prompt string. Used if READLINE is not defined or if
136 editing is turned off. Analogous to current_readline_prompt. */
137 static char *current_decoded_prompt;
139 /* The number of lines read from input while creating the current command. */
140 int current_command_line_count;
142 /* Variables to manage the task of reading here documents, because we need to
143 defer the reading until after a complete command has been collected. */
144 static REDIRECT *redir_stack[10];
147 /* Where shell input comes from. History expansion is performed on each
148 line when the shell is interactive. */
149 static char *shell_input_line = (char *)NULL;
150 static int shell_input_line_index;
151 static int shell_input_line_size; /* Amount allocated for shell_input_line. */
152 static int shell_input_line_len; /* strlen (shell_input_line) */
154 /* Either zero or EOF. */
155 static int shell_input_line_terminator;
157 /* The line number in a script on which a function definition starts. */
158 static int function_dstart;
160 /* The line number in a script on which a function body starts. */
161 static int function_bstart;
163 static REDIRECTEE redir;
167 WORD_DESC *word; /* the word that we read. */
168 int number; /* the number that we read. */
169 WORD_LIST *word_list;
173 PATTERN_LIST *pattern;
176 /* Reserved words. Members of the first group are only recognized
177 in the case that they are preceded by a list_terminator. Members
178 of the second group are recognized only under special circumstances. */
179 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION
180 %token IN BANG TIME TIMEOPT
182 /* More general tokens. yylex () knows how to make these. */
183 %token <word> WORD ASSIGNMENT_WORD
184 %token <number> NUMBER
185 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND
186 %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
189 /* The types that the various syntactical units return. */
191 %type <command> inputunit command pipeline pipeline_command
192 %type <command> list list0 list1 compound_list simple_list simple_list1
193 %type <command> simple_command shell_command
194 %type <command> for_command select_command case_command group_command
195 %type <command> function_def if_command elif_clause subshell
196 %type <redirect> redirection redirection_list
197 %type <element> simple_command_element
198 %type <word_list> word_list pattern
199 %type <pattern> pattern_list case_clause_sequence case_clause
200 %type <number> timespec
204 %left '&' ';' '\n' yacc_EOF
209 inputunit: simple_list '\n'
211 /* Case of regular command. Discard the error
212 safety net,and return the command just parsed. */
215 discard_parser_constructs (0);
220 /* Case of regular command, but not a very
221 interesting one. Return a NULL command. */
222 global_command = (COMMAND *)NULL;
227 /* Error during parsing. Return NULL command. */
228 global_command = (COMMAND *)NULL;
230 discard_parser_constructs (1);
242 /* Case of EOF seen by itself. Do ignoreeof or
244 global_command = (COMMAND *)NULL;
245 handle_eof_input_unit ();
251 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
253 { $$ = make_word_list ($2, $1); }
256 redirection: '>' WORD
259 $$ = make_redirection (1, r_output_direction, redir);
264 $$ = make_redirection (0, r_input_direction, redir);
269 $$ = make_redirection ($1, r_output_direction, redir);
274 $$ = make_redirection ($1, r_input_direction, redir);
276 | GREATER_GREATER WORD
279 $$ = make_redirection (1, r_appending_to, redir);
281 | NUMBER GREATER_GREATER WORD
284 $$ = make_redirection ($1, r_appending_to, redir);
289 $$ = make_redirection (0, r_reading_until, redir);
290 redir_stack[need_here_doc++] = $$;
292 | NUMBER LESS_LESS WORD
295 $$ = make_redirection ($1, r_reading_until, redir);
296 redir_stack[need_here_doc++] = $$;
301 $$ = make_redirection (0, r_duplicating_input, redir);
303 | NUMBER LESS_AND NUMBER
306 $$ = make_redirection ($1, r_duplicating_input, redir);
311 $$ = make_redirection (1, r_duplicating_output, redir);
313 | NUMBER GREATER_AND NUMBER
316 $$ = make_redirection ($1, r_duplicating_output, redir);
321 $$ = make_redirection (0, r_duplicating_input_word, redir);
323 | NUMBER LESS_AND WORD
326 $$ = make_redirection ($1, r_duplicating_input_word, redir);
331 $$ = make_redirection (1, r_duplicating_output_word, redir);
333 | NUMBER GREATER_AND WORD
336 $$ = make_redirection ($1, r_duplicating_output_word, redir);
338 | LESS_LESS_MINUS WORD
341 $$ = make_redirection
342 (0, r_deblank_reading_until, redir);
343 redir_stack[need_here_doc++] = $$;
345 | NUMBER LESS_LESS_MINUS WORD
348 $$ = make_redirection
349 ($1, r_deblank_reading_until, redir);
350 redir_stack[need_here_doc++] = $$;
355 $$ = make_redirection (1, r_close_this, redir);
357 | NUMBER GREATER_AND '-'
360 $$ = make_redirection ($1, r_close_this, redir);
365 $$ = make_redirection (0, r_close_this, redir);
367 | NUMBER LESS_AND '-'
370 $$ = make_redirection ($1, r_close_this, redir);
375 $$ = make_redirection (1, r_err_and_out, redir);
377 | NUMBER LESS_GREATER WORD
380 $$ = make_redirection ($1, r_input_output, redir);
385 $$ = make_redirection (0, r_input_output, redir);
390 $$ = make_redirection (1, r_output_force, redir);
392 | NUMBER GREATER_BAR WORD
395 $$ = make_redirection ($1, r_output_force, redir);
399 simple_command_element: WORD
400 { $$.word = $1; $$.redirect = 0; }
402 { $$.word = $1; $$.redirect = 0; }
404 { $$.redirect = $1; $$.word = 0; }
407 redirection_list: redirection
411 | redirection_list redirection
413 register REDIRECT *t;
415 for (t = $1; t->next; t = t->next)
422 simple_command: simple_command_element
423 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
424 | simple_command simple_command_element
425 { $$ = make_simple_command ($2, $1); }
428 command: simple_command
429 { $$ = clean_simple_command ($1); }
432 | shell_command redirection_list
437 /* According to Posix.2 3.9.5, redirections
438 specified after the body of a function should
439 be attached to the function and performed when
440 the function is executed, not as part of the
441 function definition command. */
442 if (tc->type == cm_function_def)
444 tc = tc->value.Function_def->command;
445 if (tc->type == cm_group)
446 tc = tc->value.Group->command;
450 register REDIRECT *t;
451 for (t = tc->redirects; t->next; t = t->next)
461 shell_command: for_command
465 | WHILE compound_list DO compound_list DONE
466 { $$ = make_while_command ($2, $4); }
467 | UNTIL compound_list DO compound_list DONE
468 { $$ = make_until_command ($2, $4); }
481 for_command: FOR WORD newline_list DO list DONE
482 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5); }
483 | FOR WORD newline_list '{' list '}'
484 { $$ = make_for_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5); }
485 | FOR WORD ';' newline_list DO list DONE
486 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
487 | FOR WORD ';' newline_list '{' list '}'
488 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
489 | FOR WORD newline_list IN word_list list_terminator newline_list DO list DONE
490 { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
491 | FOR WORD newline_list IN word_list list_terminator newline_list '{' list '}'
492 { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
495 select_command: SELECT WORD newline_list DO list DONE
497 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5);
499 | SELECT WORD newline_list '{' list '}'
501 $$ = make_select_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5);
503 | SELECT WORD ';' newline_list DO list DONE
505 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
507 | SELECT WORD ';' newline_list '{' list '}'
509 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
511 | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
513 $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
515 | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
517 $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
521 case_command: CASE WORD newline_list IN newline_list ESAC
522 { $$ = make_case_command ($2, (PATTERN_LIST *)NULL); }
523 | CASE WORD newline_list IN case_clause_sequence newline_list ESAC
524 { $$ = make_case_command ($2, $5); }
525 | CASE WORD newline_list IN case_clause ESAC
526 { $$ = make_case_command ($2, $5); }
529 function_def: WORD '(' ')' newline_list group_command
530 { $$ = make_function_def ($1, $5, function_dstart, function_bstart); }
533 | FUNCTION WORD '(' ')' newline_list group_command
534 { $$ = make_function_def ($2, $6, function_dstart, function_bstart); }
536 | FUNCTION WORD newline_list group_command
537 { $$ = make_function_def ($2, $4, function_dstart, function_bstart); }
540 subshell: '(' compound_list ')'
541 { $2->flags |= CMD_WANT_SUBSHELL; $$ = $2; }
544 if_command: IF compound_list THEN compound_list FI
545 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
546 | IF compound_list THEN compound_list ELSE compound_list FI
547 { $$ = make_if_command ($2, $4, $6); }
548 | IF compound_list THEN compound_list elif_clause FI
549 { $$ = make_if_command ($2, $4, $5); }
553 group_command: '{' list '}'
554 { $$ = make_group_command ($2); }
557 elif_clause: ELIF compound_list THEN compound_list
558 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
559 | ELIF compound_list THEN compound_list ELSE compound_list
560 { $$ = make_if_command ($2, $4, $6); }
561 | ELIF compound_list THEN compound_list elif_clause
562 { $$ = make_if_command ($2, $4, $5); }
565 case_clause: pattern_list
566 | case_clause_sequence pattern_list
567 { $2->next = $1; $$ = $2; }
570 pattern_list: newline_list pattern ')' compound_list
571 { $$ = make_pattern_list ($2, $4); }
572 | newline_list pattern ')' newline_list
573 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
574 | newline_list '(' pattern ')' compound_list
575 { $$ = make_pattern_list ($3, $5); }
576 | newline_list '(' pattern ')' newline_list
577 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
580 case_clause_sequence: pattern_list SEMI_SEMI
581 | case_clause_sequence pattern_list SEMI_SEMI
582 { $2->next = $1; $$ = $2; }
586 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
588 { $$ = make_word_list ($3, $1); }
591 /* A list allows leading or trailing newlines and
592 newlines as operators (equivalent to semicolons).
593 It must end with a newline or semicolon.
594 Lists are used within commands such as if, for, while. */
596 list: newline_list list0
600 gather_here_documents ();
611 list0: list1 '\n' newline_list
612 | list1 '&' newline_list
614 if ($1->type == cm_connection)
615 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
617 $$ = command_connect ($1, (COMMAND *)NULL, '&');
619 | list1 ';' newline_list
623 list1: list1 AND_AND newline_list list1
624 { $$ = command_connect ($1, $4, AND_AND); }
625 | list1 OR_OR newline_list list1
626 { $$ = command_connect ($1, $4, OR_OR); }
627 | list1 '&' newline_list list1
629 if ($1->type == cm_connection)
630 $$ = connect_async_list ($1, $4, '&');
632 $$ = command_connect ($1, $4, '&');
634 | list1 ';' newline_list list1
635 { $$ = command_connect ($1, $4, ';'); }
636 | list1 '\n' newline_list list1
637 { $$ = command_connect ($1, $4, ';'); }
651 /* A simple_list is a list that contains no significant newlines
652 and no leading or trailing newlines. Newlines are allowed
653 only following operators, where they are not significant.
655 This is what an inputunit consists of. */
657 simple_list: simple_list1
661 gather_here_documents ();
665 if ($1->type == cm_connection)
666 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
668 $$ = command_connect ($1, (COMMAND *)NULL, '&');
670 gather_here_documents ();
676 gather_here_documents ();
680 simple_list1: simple_list1 AND_AND newline_list simple_list1
681 { $$ = command_connect ($1, $4, AND_AND); }
682 | simple_list1 OR_OR newline_list simple_list1
683 { $$ = command_connect ($1, $4, OR_OR); }
684 | simple_list1 '&' simple_list1
686 if ($1->type == cm_connection)
687 $$ = connect_async_list ($1, $3, '&');
689 $$ = command_connect ($1, $3, '&');
691 | simple_list1 ';' simple_list1
692 { $$ = command_connect ($1, $3, ';'); }
698 pipeline_command: pipeline
702 $2->flags |= CMD_INVERT_RETURN;
710 | timespec BANG pipeline
715 | BANG timespec pipeline
717 $3->flags |= $2|CMD_INVERT_RETURN;
723 pipeline '|' newline_list pipeline
724 { $$ = command_connect ($1, $4, '|'); }
730 { $$ = CMD_TIME_PIPELINE; }
732 { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
736 /* Possible states for the parser that require it to do special things. */
737 #define PST_CASEPAT 0x001 /* in a case pattern list */
738 #define PST_ALEXPNEXT 0x002 /* expand next word for aliases */
739 #define PST_ALLOWOPNBRC 0x004 /* allow open brace for function def */
740 #define PST_NEEDCLOSBRC 0x008 /* need close brace */
741 #define PST_DBLPAREN 0x010 /* double-paren parsing */
742 #define PST_SUBSHELL 0x020 /* ( ... ) subshell */
743 #define PST_CMDSUBST 0x040 /* $( ... ) command substitution */
744 #define PST_CASESTMT 0x080 /* parsing a case statement */
746 /* Initial size to allocate for tokens, and the
747 amount to grow them by. */
748 #define TOKEN_DEFAULT_GROW_SIZE 512
750 /* Shell meta-characters that, when unquoted, separate words. */
751 #define shellmeta(c) (strchr ("()<>;&|", (c)) != 0)
752 #define shellbreak(c) (strchr ("()<>;&| \t\n", (c)) != 0)
753 #define shellquote(c) ((c) == '"' || (c) == '`' || (c) == '\'')
754 #define shellexp(c) ((c) == '$' || (c) == '<' || (c) == '>')
756 /* The token currently being read. */
757 static int current_token;
759 /* The last read token, or NULL. read_token () uses this for context
761 static int last_read_token;
763 /* The token read prior to last_read_token. */
764 static int token_before_that;
766 /* The token read prior to token_before_that. */
767 static int two_tokens_ago;
769 /* If non-zero, it is the token that we want read_token to return
770 regardless of what text is (or isn't) present to be read. This
771 is reset by read_token. If token_to_read == WORD or
772 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
773 static int token_to_read;
774 static WORD_DESC *word_desc_to_read;
776 /* The current parser state. */
777 static int parser_state;
779 /* Global var is non-zero when end of file has been reached. */
791 /* yy_getc () returns the next available character from input or EOF.
792 yy_ungetc (c) makes `c' the next character to read.
793 init_yy_io (get, unget, type, location) makes the function GET the
794 installed function for getting the next character, makes UNGET the
795 installed function for un-getting a character, sets the type of stream
796 (either string or file) from TYPE, and makes LOCATION point to where
797 the input is coming from. */
799 /* Unconditionally returns end-of-file. */
806 /* Variable containing the current get and unget functions.
807 See ./input.h for a clearer description. */
808 BASH_INPUT bash_input;
810 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
811 is non-null, avoiding a memory leak. */
813 initialize_bash_input ()
815 bash_input.type = st_none;
816 FREE (bash_input.name);
817 bash_input.name = (char *)NULL;
818 bash_input.location.file = (FILE *)NULL;
819 bash_input.location.string = (char *)NULL;
820 bash_input.getter = (Function *)NULL;
821 bash_input.ungetter = (Function *)NULL;
824 /* Set the contents of the current bash input stream from
825 GET, UNGET, TYPE, NAME, and LOCATION. */
827 init_yy_io (get, unget, type, name, location)
828 Function *get, *unget;
831 INPUT_STREAM location;
833 bash_input.type = type;
834 FREE (bash_input.name);
835 bash_input.name = name ? savestring (name) : (char *)NULL;
839 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
841 bash_input.location = location;
843 bash_input.getter = get;
844 bash_input.ungetter = unget;
847 /* Call this to get the next character of input. */
851 return (*(bash_input.getter)) ();
854 /* Call this to unget C. That is, to make C the next character
860 return (*(bash_input.ungetter)) (c);
863 #if defined (BUFFERED_INPUT)
865 input_file_descriptor ()
867 switch (bash_input.type)
870 return (fileno (bash_input.location.file));
872 return (bash_input.location.buffered_fd);
875 return (fileno (stdin));
878 #endif /* BUFFERED_INPUT */
880 /* **************************************************************** */
882 /* Let input be read from readline (). */
884 /* **************************************************************** */
886 #if defined (READLINE)
887 char *current_readline_prompt = (char *)NULL;
888 char *current_readline_line = (char *)NULL;
889 int current_readline_line_index = 0;
894 SigHandler *old_sigint;
897 if (!current_readline_line)
899 if (!bash_readline_initialized)
900 initialize_readline ();
902 #if defined (JOB_CONTROL)
904 give_terminal_to (shell_pgrp);
905 #endif /* JOB_CONTROL */
907 if (signal_is_ignored (SIGINT) == 0)
909 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
910 interrupt_immediately++;
913 current_readline_line = readline (current_readline_prompt ?
914 current_readline_prompt : "");
916 if (signal_is_ignored (SIGINT) == 0)
918 interrupt_immediately--;
919 set_signal_handler (SIGINT, old_sigint);
923 /* Reset the prompt to the decoded value of prompt_string_pointer. */
924 reset_readline_prompt ();
927 if (current_readline_line == 0)
930 current_readline_line_index = 0;
931 line_len = strlen (current_readline_line);
933 current_readline_line = xrealloc (current_readline_line, 2 + line_len);
934 current_readline_line[line_len++] = '\n';
935 current_readline_line[line_len] = '\0';
938 if (current_readline_line[current_readline_line_index] == 0)
940 free (current_readline_line);
941 current_readline_line = (char *)NULL;
942 return (yy_readline_get ());
946 c = (unsigned char)current_readline_line[current_readline_line_index++];
952 yy_readline_unget (c)
955 if (current_readline_line_index && current_readline_line)
956 current_readline_line[--current_readline_line_index] = c;
961 with_input_from_stdin ()
963 INPUT_STREAM location;
965 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
967 location.string = current_readline_line;
968 init_yy_io (yy_readline_get, yy_readline_unget,
969 st_stdin, "readline stdin", location);
973 #else /* !READLINE */
976 with_input_from_stdin ()
978 with_input_from_stream (stdin, "stdin");
980 #endif /* !READLINE */
982 /* **************************************************************** */
984 /* Let input come from STRING. STRING is zero terminated. */
986 /* **************************************************************** */
991 register char *string;
994 string = bash_input.location.string;
997 /* If the string doesn't exist, or is empty, EOF found. */
998 if (string && *string)
1000 c = *(unsigned char *)string++;
1001 bash_input.location.string = string;
1010 *(--bash_input.location.string) = c;
1015 with_input_from_string (string, name)
1016 char *string, *name;
1018 INPUT_STREAM location;
1020 location.string = string;
1021 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
1024 /* **************************************************************** */
1026 /* Let input come from STREAM. */
1028 /* **************************************************************** */
1035 if (bash_input.location.file)
1037 #if !defined (HAVE_RESTARTABLE_SYSCALLS)
1038 result = getc_with_restart (bash_input.location.file);
1039 #else /* HAVE_RESTARTABLE_SYSCALLS */
1040 result = getc (bash_input.location.file);
1041 result = (feof (bash_input.location.file)) ? EOF : (unsigned char)result;
1042 #endif /* HAVE_RESTARTABLE_SYSCALLS */
1051 #if !defined (HAVE_RESTARTABLE_SYSCALLS)
1052 return (ungetc_with_restart (c, bash_input.location.file));
1053 #else /* HAVE_RESTARTABLE_SYSCALLS */
1054 return (ungetc (c, bash_input.location.file));
1055 #endif /* HAVE_RESTARTABLE_SYSCALLS */
1059 with_input_from_stream (stream, name)
1063 INPUT_STREAM location;
1065 location.file = stream;
1066 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
1069 typedef struct stream_saver {
1070 struct stream_saver *next;
1071 BASH_INPUT bash_input;
1073 #if defined (BUFFERED_INPUT)
1074 BUFFERED_STREAM *bstream;
1075 #endif /* BUFFERED_INPUT */
1078 /* The globally known line number. */
1079 int line_number = 0;
1081 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1084 push_stream (reset_lineno)
1087 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1089 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1091 #if defined (BUFFERED_INPUT)
1092 saver->bstream = (BUFFERED_STREAM *)NULL;
1093 /* If we have a buffered stream, clear out buffers[fd]. */
1094 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1096 saver->bstream = buffers[bash_input.location.buffered_fd];
1097 buffers[bash_input.location.buffered_fd] = (BUFFERED_STREAM *)NULL;
1099 #endif /* BUFFERED_INPUT */
1101 saver->line = line_number;
1102 bash_input.name = (char *)NULL;
1103 saver->next = stream_list;
1104 stream_list = saver;
1117 STREAM_SAVER *saver = stream_list;
1120 stream_list = stream_list->next;
1122 init_yy_io (saver->bash_input.getter,
1123 saver->bash_input.ungetter,
1124 saver->bash_input.type,
1125 saver->bash_input.name,
1126 saver->bash_input.location);
1128 #if defined (BUFFERED_INPUT)
1129 /* If we have a buffered stream, restore buffers[fd]. */
1130 /* If the input file descriptor was changed while this was on the
1131 save stack, update the buffered fd to the new file descriptor and
1132 re-establish the buffer <-> bash_input fd correspondence. */
1133 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1135 if (bash_input_fd_changed)
1137 bash_input_fd_changed = 0;
1138 if (default_buffered_input >= 0)
1140 bash_input.location.buffered_fd = default_buffered_input;
1141 saver->bstream->b_fd = default_buffered_input;
1144 buffers[bash_input.location.buffered_fd] = saver->bstream;
1146 #endif /* BUFFERED_INPUT */
1148 line_number = saver->line;
1150 FREE (saver->bash_input.name);
1155 /* Return 1 if a stream of type TYPE is saved on the stack. */
1157 stream_on_stack (type)
1158 enum stream_type type;
1160 register STREAM_SAVER *s;
1162 for (s = stream_list; s; s = s->next)
1163 if (s->bash_input.type == type)
1169 * This is used to inhibit alias expansion and reserved word recognition
1170 * inside case statement pattern lists. A `case statement pattern list' is:
1172 * everything between the `in' in a `case word in' and the next ')'
1174 * everything between a `;;' and the next `)' or `esac'
1179 #define END_OF_ALIAS 0
1182 * Pseudo-global variables used in implementing token-wise alias expansion.
1186 * Pushing and popping strings. This works together with shell_getc to
1187 * implement alias expansion on a per-token basis.
1190 typedef struct string_saver {
1191 struct string_saver *next;
1192 int expand_alias; /* Value to set expand_alias to when string is popped. */
1194 alias_t *expander; /* alias that caused this line to be pushed. */
1195 int saved_line_size, saved_line_index, saved_line_terminator;
1198 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1201 * Push the current shell_input_line onto a stack of such lines and make S
1202 * the current input. Used when expanding aliases. EXPAND is used to set
1203 * the value of expand_next_token when the string is popped, so that the
1204 * word after the alias in the original line is handled correctly when the
1205 * alias expands to multiple words. TOKEN is the token that was expanded
1206 * into S; it is saved and used to prevent infinite recursive expansion.
1209 push_string (s, expand, ap)
1214 STRING_SAVER *temp = (STRING_SAVER *) xmalloc (sizeof (STRING_SAVER));
1216 temp->expand_alias = expand;
1217 temp->saved_line = shell_input_line;
1218 temp->saved_line_size = shell_input_line_size;
1219 temp->saved_line_index = shell_input_line_index;
1220 temp->saved_line_terminator = shell_input_line_terminator;
1221 temp->expander = ap;
1222 temp->next = pushed_string_list;
1223 pushed_string_list = temp;
1225 ap->flags |= AL_BEINGEXPANDED;
1227 shell_input_line = s;
1228 shell_input_line_size = strlen (s);
1229 shell_input_line_index = 0;
1230 shell_input_line_terminator = '\0';
1231 parser_state &= ~PST_ALEXPNEXT;
1235 * Make the top of the pushed_string stack be the current shell input.
1236 * Only called when there is something on the stack. Called from shell_getc
1237 * when it thinks it has consumed the string generated by an alias expansion
1238 * and needs to return to the original input line.
1245 FREE (shell_input_line);
1246 shell_input_line = pushed_string_list->saved_line;
1247 shell_input_line_index = pushed_string_list->saved_line_index;
1248 shell_input_line_size = pushed_string_list->saved_line_size;
1249 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1251 if (pushed_string_list->expand_alias)
1252 parser_state |= PST_ALEXPNEXT;
1254 parser_state &= ~PST_ALEXPNEXT;
1256 t = pushed_string_list;
1257 pushed_string_list = pushed_string_list->next;
1259 t->expander->flags &= ~AL_BEINGEXPANDED;
1267 register STRING_SAVER *t, *t1;
1269 for (t = pushed_string_list; t; )
1272 FREE (t->saved_line);
1273 t->expander->flags &= ~AL_BEINGEXPANDED;
1277 pushed_string_list = (STRING_SAVER *)NULL;
1282 /* Return a line of text, taken from wherever yylex () reads input.
1283 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1284 is non-zero, we remove unquoted \<newline> pairs. This is used by
1285 read_secondary_line to read here documents. */
1287 read_a_line (remove_quoted_newline)
1288 int remove_quoted_newline;
1290 static char *line_buffer = (char *)NULL;
1291 static int buffer_size = 0;
1292 int indx = 0, c, peekc, pass_next;
1294 #if defined (READLINE)
1295 if (interactive && bash_input.type != st_string && no_line_editing)
1297 if (interactive && bash_input.type != st_string)
1306 /* Allow immediate exit if interrupted during input. */
1312 /* If there is no more input, then we return NULL. */
1315 if (interactive && bash_input.type == st_stream)
1318 return ((char *)NULL);
1322 /* `+2' in case the final character in the buffer is a newline. */
1323 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
1325 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
1326 here document with an unquoted delimiter. In this case,
1327 the line will be expanded as if it were in double quotes.
1328 We allow a backslash to escape the next character, but we
1329 need to treat the backslash specially only if a backslash
1330 quoting a backslash-newline pair appears in the line. */
1333 line_buffer[indx++] = c;
1336 else if (c == '\\' && remove_quoted_newline)
1340 continue; /* Make the unquoted \<newline> pair disappear. */
1345 line_buffer[indx++] = c; /* Preserve the backslash. */
1349 line_buffer[indx++] = c;
1353 line_buffer[indx] = '\0';
1354 return (line_buffer);
1359 /* Return a line as in read_a_line (), but insure that the prompt is
1360 the secondary prompt. This is used to read the lines of a here
1361 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
1362 newlines quoted with backslashes while reading the line. It is
1363 non-zero unless the delimiter of the here document was quoted. */
1365 read_secondary_line (remove_quoted_newline)
1366 int remove_quoted_newline;
1368 prompt_string_pointer = &ps2_prompt;
1370 return (read_a_line (remove_quoted_newline));
1373 /* **************************************************************** */
1377 /* **************************************************************** */
1379 /* Reserved words. These are only recognized as the first word of a
1381 STRING_INT_ALIST word_token_alist[] = {
1390 #if defined (SELECT_COMMAND)
1391 { "select", SELECT },
1398 { "function", FUNCTION },
1399 #if defined (COMMAND_TIMING)
1408 /* These are used by read_token_word, but appear up here so that shell_getc
1409 can use them to decide when to add otherwise blank lines to the history. */
1411 /* The primary delimiter stack. */
1412 struct dstack dstack = { (char *)NULL, 0, 0 };
1414 /* A temporary delimiter stack to be used when decoding prompt strings.
1415 This is needed because command substitutions in prompt strings (e.g., PS2)
1416 can screw up the parser's quoting state. */
1417 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
1419 /* Macro for accessing the top delimiter on the stack. Returns the
1420 delimiter or zero if none. */
1421 #define current_delimiter(ds) \
1422 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
1424 #define push_delimiter(ds, character) \
1427 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
1428 ds.delimiters = xrealloc \
1429 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
1430 ds.delimiters[ds.delimiter_depth] = character; \
1431 ds.delimiter_depth++; \
1435 #define pop_delimiter(ds) ds.delimiter_depth--
1437 /* Return the next shell input character. This always reads characters
1438 from shell_input_line; when that line is exhausted, it is time to
1439 read the next line. This is called by read_token when the shell is
1440 processing normal command input. */
1443 shell_getc (remove_quoted_newline)
1444 int remove_quoted_newline;
1448 static int mustpop = 0;
1453 /* If shell_input_line[shell_input_line_index] == 0, but there is
1454 something on the pushed list of strings, then we don't want to go
1455 off and get another line. We let the code down below handle it. */
1457 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
1458 (pushed_string_list == (STRING_SAVER *)NULL)))
1460 if (!shell_input_line || !shell_input_line[shell_input_line_index])
1467 /* Allow immediate exit if interrupted during input. */
1471 shell_input_line_terminator = 0;
1473 #if defined (JOB_CONTROL)
1474 /* This can cause a problem when reading a command as the result
1475 of a trap, when the trap is called from flush_child. This call
1476 had better not cause jobs to disappear from the job table in
1477 that case, or we will have big trouble. */
1478 notify_and_cleanup ();
1479 #else /* !JOB_CONTROL */
1480 cleanup_dead_jobs ();
1481 #endif /* !JOB_CONTROL */
1483 #if defined (READLINE)
1484 if (interactive && bash_input.type != st_string && no_line_editing)
1486 if (interactive && bash_input.type != st_string)
1490 if (bash_input.type == st_stream)
1493 while (c = yy_getc ())
1495 /* Allow immediate exit if interrupted during input. */
1498 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
1502 if (bash_input.type == st_stream)
1506 shell_input_line_terminator = EOF;
1508 shell_input_line[i] = '\0';
1512 shell_input_line[i++] = c;
1516 shell_input_line[--i] = '\0';
1517 current_command_line_count++;
1521 shell_input_line_index = 0;
1522 shell_input_line_len = i; /* == strlen (shell_input_line) */
1524 #if defined (HISTORY)
1525 if (remember_on_history && shell_input_line && shell_input_line[0])
1528 # if defined (BANG_HISTORY)
1531 /* If the current delimiter is a single quote, we should not be
1532 performing history expansion, even if we're on a different
1533 line from the original single quote. */
1534 old_hist = history_expansion_inhibited;
1535 if (current_delimiter (dstack) == '\'')
1536 history_expansion_inhibited = 1;
1538 expansions = pre_process_line (shell_input_line, 1, 1);
1539 # if defined (BANG_HISTORY)
1540 history_expansion_inhibited = old_hist;
1543 free (shell_input_line);
1544 shell_input_line = expansions;
1545 shell_input_line_len = shell_input_line ?
1546 strlen (shell_input_line) :
1548 if (!shell_input_line_len)
1549 current_command_line_count--;
1551 /* We have to force the xrealloc below because we don't know the
1552 true allocated size of shell_input_line anymore. */
1553 shell_input_line_size = shell_input_line_len;
1555 /* XXX - this is grotesque */
1556 else if (remember_on_history && shell_input_line &&
1557 shell_input_line[0] == '\0' &&
1558 current_command_line_count > 1 && current_delimiter (dstack))
1560 /* We know shell_input_line[0] == 0 and we're reading some sort of
1561 quoted string. This means we've got a line consisting of only
1562 a newline in a quoted string. We want to make sure this line
1563 gets added to the history. */
1564 maybe_add_history (shell_input_line);
1567 #endif /* HISTORY */
1569 if (shell_input_line)
1571 /* Lines that signify the end of the shell's input should not be
1573 if (echo_input_at_read && (shell_input_line[0] ||
1574 shell_input_line_terminator != EOF))
1575 fprintf (stderr, "%s\n", shell_input_line);
1579 shell_input_line_size = 0;
1580 prompt_string_pointer = ¤t_prompt_string;
1585 /* Add the newline to the end of this string, iff the string does
1586 not already end in an EOF character. */
1587 if (shell_input_line_terminator != EOF)
1589 if (shell_input_line_len + 3 > shell_input_line_size)
1590 shell_input_line = xrealloc (shell_input_line,
1591 1 + (shell_input_line_size += 2));
1593 shell_input_line[shell_input_line_len] = '\n';
1594 shell_input_line[shell_input_line_len + 1] = '\0';
1598 c = shell_input_line[shell_input_line_index];
1601 shell_input_line_index++;
1603 if (c == '\\' && remove_quoted_newline &&
1604 shell_input_line[shell_input_line_index] == '\n')
1612 /* If C is NULL, we have reached the end of the current input string. If
1613 pushed_string_list is non-empty, it's time to pop to the previous string
1614 because we have fully consumed the result of the last alias expansion.
1615 Do it transparently; just return the next character of the string popped
1617 if (!c && (pushed_string_list != (STRING_SAVER *)NULL))
1622 c = shell_input_line[shell_input_line_index];
1624 shell_input_line_index++;
1635 if (!c && shell_input_line_terminator == EOF)
1636 return ((shell_input_line_index != 0) ? '\n' : EOF);
1638 return ((unsigned char)c);
1641 /* Put C back into the input for the shell. */
1646 if (shell_input_line && shell_input_line_index)
1647 shell_input_line[--shell_input_line_index] = c;
1653 if (shell_input_line && shell_input_line_index)
1654 shell_input_line_index--;
1657 /* Discard input until CHARACTER is seen, then push that character back
1658 onto the input stream. */
1660 discard_until (character)
1665 while ((c = shell_getc (0)) != EOF && c != character)
1673 execute_prompt_command (command)
1676 Function *temp_last, *temp_this;
1678 int temp_exit_value, temp_eof_encountered;
1680 temp_last = last_shell_builtin;
1681 temp_this = this_shell_builtin;
1682 temp_exit_value = last_command_exit_value;
1683 temp_eof_encountered = eof_encountered;
1684 last_lastarg = get_string_value ("_");
1686 last_lastarg = savestring (last_lastarg);
1688 parse_and_execute (savestring (command), "PROMPT_COMMAND", 0);
1690 last_shell_builtin = temp_last;
1691 this_shell_builtin = temp_this;
1692 last_command_exit_value = temp_exit_value;
1693 eof_encountered = temp_eof_encountered;
1695 bind_variable ("_", last_lastarg);
1696 FREE (last_lastarg);
1698 if (token_to_read == '\n') /* reset_parser was called */
1702 /* Place to remember the token. We try to keep the buffer
1703 at a reasonable size, but it can grow. */
1704 static char *token = (char *)NULL;
1706 /* Current size of the token buffer. */
1707 static int token_buffer_size;
1709 /* Command to read_token () explaining what we want it to do. */
1712 #define prompt_is_ps1 \
1713 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
1715 /* Function for yyparse to call. yylex keeps track of
1716 the last two tokens read, and calls read_token. */
1720 if (interactive && (current_token == 0 || current_token == '\n'))
1722 /* Before we print a prompt, we might have to check mailboxes.
1723 We do this only if it is time to do so. Notice that only here
1724 is the mail alarm reset; nothing takes place in check_mail ()
1725 except the checking of mail. Please don't change this. */
1726 if (prompt_is_ps1 && time_to_check_mail ())
1729 reset_mail_timer ();
1732 /* Avoid printing a prompt if we're not going to read anything, e.g.
1733 after resetting the parser with read_token (RESET). */
1734 if (token_to_read == 0 && interactive)
1738 two_tokens_ago = token_before_that;
1739 token_before_that = last_read_token;
1740 last_read_token = current_token;
1741 current_token = read_token (READ);
1742 return (current_token);
1745 /* When non-zero, we have read the required tokens
1746 which allow ESAC to be the next one read. */
1747 static int esacs_needed_count;
1750 gather_here_documents ()
1753 while (need_here_doc)
1755 make_here_document (redir_stack[r++]);
1760 /* When non-zero, an open-brace used to create a group is awaiting a close
1762 static int open_brace_count;
1764 #define command_token_position(token) \
1765 (((token) == ASSIGNMENT_WORD) || \
1766 ((token) != SEMI_SEMI && reserved_word_acceptable(token)))
1768 #define assignment_acceptable(token) command_token_position(token) && \
1769 ((parser_state & PST_CASEPAT) == 0)
1771 /* Check to see if TOKEN is a reserved word and return the token
1773 #define CHECK_FOR_RESERVED_WORD(tok) \
1775 if (!dollar_present && !quoted && \
1776 reserved_word_acceptable (last_read_token)) \
1779 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
1780 if (STREQ (tok, word_token_alist[i].word)) \
1782 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
1784 if (word_token_alist[i].token == ESAC) \
1785 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
1786 else if (word_token_alist[i].token == CASE) \
1787 parser_state |= PST_CASESTMT; \
1788 else if (word_token_alist[i].token == '{') \
1789 open_brace_count++; \
1790 else if (word_token_alist[i].token == '}' && open_brace_count) \
1791 open_brace_count--; \
1792 return (word_token_alist[i].token); \
1799 /* OK, we have a token. Let's try to alias expand it, if (and only if)
1802 It is eligible for expansion if the shell is in interactive mode, and
1803 the token is unquoted and the last token read was a command
1804 separator (or expand_next_token is set), and we are currently
1805 processing an alias (pushed_string_list is non-empty) and this
1806 token is not the same as the current or any previously
1809 Special cases that disqualify:
1810 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
1812 alias_expand_token (token)
1818 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
1819 (parser_state & PST_CASEPAT) == 0)
1821 ap = find_alias (token);
1823 /* Currently expanding this token. */
1824 if (ap && (ap->flags & AL_BEINGEXPANDED))
1825 return (NO_EXPANSION);
1827 expanded = ap ? savestring (ap->value) : (char *)NULL;
1830 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
1831 return (RE_READ_TOKEN);
1834 /* This is an eligible token that does not have an expansion. */
1835 return (NO_EXPANSION);
1837 return (NO_EXPANSION);
1841 /* Handle special cases of token recognition:
1842 IN is recognized if the last token was WORD and the token
1843 before that was FOR or CASE or SELECT.
1845 DO is recognized if the last token was WORD and the token
1846 before that was FOR or SELECT.
1848 ESAC is recognized if the last token caused `esacs_needed_count'
1851 `{' is recognized if the last token as WORD and the token
1852 before that was FUNCTION.
1854 `}' is recognized if there is an unclosed `{' prsent.
1858 special_case_tokens (token)
1861 if ((last_read_token == WORD) &&
1862 #if defined (SELECT_COMMAND)
1863 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
1865 ((token_before_that == FOR) || (token_before_that == CASE)) &&
1867 (token[0] == 'i' && token[1] == 'n' && token[2] == 0))
1869 if (token_before_that == CASE)
1871 parser_state |= PST_CASEPAT;
1872 esacs_needed_count++;
1877 if (last_read_token == WORD &&
1878 #if defined (SELECT_COMMAND)
1879 (token_before_that == FOR || token_before_that == SELECT) &&
1881 (token_before_that == FOR) &&
1883 (token[0] == 'd' && token[1] == 'o' && token[2] == '\0'))
1886 /* Ditto for ESAC in the CASE case.
1887 Specifically, this handles "case word in esac", which is a legal
1888 construct, certainly because someone will pass an empty arg to the
1889 case construct, and we don't want it to barf. Of course, we should
1890 insist that the case construct has at least one pattern in it, but
1891 the designers disagree. */
1892 if (esacs_needed_count)
1894 esacs_needed_count--;
1895 if (STREQ (token, "esac"))
1897 parser_state &= ~PST_CASEPAT;
1902 /* The start of a shell function definition. */
1903 if (parser_state & PST_ALLOWOPNBRC)
1905 parser_state &= ~PST_ALLOWOPNBRC;
1906 if (token[0] == '{' && token[1] == '\0') /* } */
1909 function_bstart = line_number;
1910 return ('{'); /* } */
1914 if (open_brace_count && reserved_word_acceptable (last_read_token) && token[0] == '}' && !token[1])
1916 open_brace_count--; /* { */
1920 /* Handle -p after `time'. */
1921 if (last_read_token == TIME && token[0] == '-' && token[1] == 'p' && !token[2])
1927 /* Called from shell.c when Control-C is typed at top level. Or
1928 by the error rule at top level. */
1932 dstack.delimiter_depth = 0; /* No delimiters found so far. */
1933 open_brace_count = 0;
1938 if (pushed_string_list)
1940 free_string_list ();
1941 pushed_string_list = (STRING_SAVER *)NULL;
1945 if (shell_input_line)
1947 free (shell_input_line);
1948 shell_input_line = (char *)NULL;
1949 shell_input_line_size = shell_input_line_index = 0;
1952 FREE (word_desc_to_read);
1953 word_desc_to_read = (WORD_DESC *)NULL;
1955 last_read_token = '\n';
1956 token_to_read = '\n';
1959 /* Read the next token. Command can be READ (normal operation) or
1960 RESET (to normalize state). */
1962 read_token (command)
1965 int character; /* Current character. */
1966 int peek_char; /* Temporary look-ahead character. */
1967 int result; /* The thing to return. */
1969 if (command == RESET)
1977 result = token_to_read;
1978 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
1979 yylval.word = word_desc_to_read;
1985 /* This is a place to jump back to once we have successfully expanded a
1986 token with an alias and pushed the string with push_string () */
1990 /* Read a single word from input. Start by skipping blanks. */
1991 while ((character = shell_getc (1)) != EOF && whitespace (character))
1994 if (character == EOF)
2000 if (character == '#' && (!interactive || interactive_comments))
2002 /* A comment. Discard until EOL or EOF, and then return a newline. */
2003 discard_until ('\n');
2005 character = '\n'; /* this will take the next if statement and return. */
2008 if (character == '\n')
2010 /* If we're about to return an unquoted newline, we can go and collect
2011 the text of any pending here document. */
2013 gather_here_documents ();
2016 parser_state &= ~PST_ALEXPNEXT;
2022 /* Shell meta-characters. */
2023 if (shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
2026 /* Turn off alias tokenization iff this character sequence would
2027 not leave us ready to read a command. */
2028 if (character == '<' || character == '>')
2029 parser_state &= ~PST_ALEXPNEXT;
2032 peek_char = shell_getc (1);
2033 if (character == peek_char)
2038 /* If '<' then we could be at "<<" or at "<<-". We have to
2039 look ahead one more character. */
2040 peek_char = shell_getc (1);
2041 if (peek_char == '-')
2042 return (LESS_LESS_MINUS);
2045 shell_ungetc (peek_char);
2050 return (GREATER_GREATER);
2053 parser_state |= PST_CASEPAT;
2055 parser_state &= ~PST_ALEXPNEXT;
2065 #if defined (DPAREN_ARITHMETIC)
2067 if (reserved_word_acceptable (last_read_token))
2069 parser_state |= PST_DBLPAREN;
2070 yylval.word = make_word ("let");
2077 else if (character == '<' && peek_char == '&')
2079 else if (character == '>' && peek_char == '&')
2080 return (GREATER_AND);
2081 else if (character == '<' && peek_char == '>')
2082 return (LESS_GREATER);
2083 else if (character == '>' && peek_char == '|')
2084 return (GREATER_BAR);
2085 else if (peek_char == '>' && character == '&')
2086 return (AND_GREATER);
2088 shell_ungetc (peek_char);
2090 /* If we look like we are reading the start of a function
2091 definition, then let the reader know about it so that
2092 we will do the right thing with `{'. */
2093 if (character == ')' && last_read_token == '(' && token_before_that == WORD)
2095 parser_state |= PST_ALLOWOPNBRC;
2097 parser_state &= ~PST_ALEXPNEXT;
2099 function_dstart = line_number;
2102 /* case pattern lists may be preceded by an optional left paren. If
2103 we're not trying to parse a case pattern list, the left paren
2104 indicates a subshell. */
2105 if (character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
2106 parser_state |= PST_SUBSHELL;
2108 else if ((parser_state & PST_CASEPAT) && character == ')')
2109 parser_state &= ~PST_CASEPAT;
2111 else if ((parser_state & PST_SUBSHELL) && character == ')')
2112 parser_state &= ~PST_SUBSHELL;
2114 #if defined (PROCESS_SUBSTITUTION)
2115 /* Check for the constructs which introduce process substitution.
2116 Shells running in `posix mode' don't do process substitution. */
2117 if (posixly_correct ||
2118 ((character != '>' && character != '<') || peek_char != '('))
2119 #endif /* PROCESS_SUBSTITUTION */
2123 /* Hack <&- (close stdin) case. */
2124 if (character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
2127 /* Okay, if we got this far, we have to read a word. Read one,
2128 and then check it against the known ones. */
2129 result = read_token_word (character);
2131 if (result == RE_READ_TOKEN)
2137 /* Match a $(...) or other grouping construct. This has to handle embedded
2138 quoted strings ('', ``, "") and nested constructs. It also must handle
2139 reprompting the user, if necessary, after reading a newline, and returning
2140 correct error values if it reads EOF. */
2141 static char matched_pair_error;
2143 parse_matched_pair (qc, open, close, lenp, flags)
2144 int qc; /* `"' if this construct is within double quotes */
2148 int count, ch, was_dollar;
2149 int pass_next_character, nestlen, start_lineno;
2150 char *ret, *nestret;
2151 int retind, retsize;
2154 pass_next_character = was_dollar = 0;
2156 ret = xmalloc (retsize = 64);
2159 start_lineno = line_number;
2162 ch = shell_getc (qc != '\'' && pass_next_character == 0);
2166 parser_error (start_lineno, "unexpected EOF while looking for matching `%c'", close);
2167 EOF_Reached = 1; /* XXX */
2168 return (&matched_pair_error);
2171 /* Possible reprompting. */
2172 if (ch == '\n' && interactive &&
2173 (bash_input.type == st_stdin || bash_input.type == st_stream))
2176 if (pass_next_character) /* last char was backslash */
2178 pass_next_character = 0;
2179 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
2181 if (retind > 0) retind--; /* swallow previously-added backslash */
2185 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2186 if (ch == CTLESC || ch == CTLNUL)
2187 ret[retind++] = CTLESC;
2191 else if (ch == CTLESC || ch == CTLNUL) /* special shell escapes */
2193 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
2194 ret[retind++] = CTLESC;
2198 else if (ch == close) /* ending delimiter */
2200 else if (ch == open) /* nested begin */
2203 /* Add this character. */
2204 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
2207 if (open == '\'') /* '' inside grouping construct */
2210 if (ch == '\\') /* backslashes */
2211 pass_next_character++;
2213 if (open != close) /* a grouping construct */
2215 if (shellquote (ch))
2217 /* '', ``, or "" inside $(...) or other grouping construct. */
2218 push_delimiter (dstack, ch);
2219 nestret = parse_matched_pair (ch, ch, ch, &nestlen, 0);
2220 pop_delimiter (dstack);
2221 if (nestret == &matched_pair_error)
2224 return &matched_pair_error;
2228 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2229 strcpy (ret + retind, nestret);
2235 /* Parse an old-style command substitution within double quotes as a
2237 /* XXX - sh and ksh93 don't do this - XXX */
2238 else if (open == '"' && ch == '`')
2240 nestret = parse_matched_pair (0, '`', '`', &nestlen, 0);
2241 if (nestret == &matched_pair_error)
2244 return &matched_pair_error;
2248 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2249 strcpy (ret + retind, nestret);
2254 else if (was_dollar && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
2255 /* check for $(), $[], or ${} inside quoted string. */
2257 if (open == ch) /* undo previous increment */
2259 if (ch == '(') /* ) */
2260 nestret = parse_matched_pair (0, '(', ')', &nestlen, 0);
2261 else if (ch == '{') /* } */
2262 nestret = parse_matched_pair (0, '{', '}', &nestlen, 0);
2263 else if (ch == '[') /* ] */
2264 nestret = parse_matched_pair (0, '[', ']', &nestlen, 0);
2265 if (nestret == &matched_pair_error)
2268 return &matched_pair_error;
2272 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
2273 strcpy (ret + retind, nestret);
2278 was_dollar = (ch == '$');
2288 read_token_word (character)
2291 /* The value for YYLVAL when a WORD is read. */
2292 WORD_DESC *the_word;
2294 /* Index into the token that we are building. */
2297 /* ALL_DIGITS becomes zero when we see a non-digit. */
2300 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
2303 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
2306 /* Non-zero means to ignore the value of the next character, and just
2307 to add it no matter what. */
2308 int pass_next_character;
2310 /* The current delimiting character. */
2312 int result, peek_char;
2313 char *ttok, *ttrans;
2314 int ttoklen, ttranslen;
2316 if (token_buffer_size < TOKEN_DEFAULT_GROW_SIZE)
2319 token = xmalloc (token_buffer_size = TOKEN_DEFAULT_GROW_SIZE);
2323 all_digits = digit (character);
2324 dollar_present = quoted = pass_next_character = 0;
2328 if (character == EOF)
2331 if (pass_next_character)
2333 pass_next_character = 0;
2337 cd = current_delimiter (dstack);
2339 /* Handle backslashes. Quote lots of things when not inside of
2340 double-quotes, quote some things inside of double-quotes. */
2341 if (character == '\\')
2343 peek_char = shell_getc (0);
2345 /* Backslash-newline is ignored in all cases except
2346 when quoted with single quotes. */
2347 if (peek_char == '\n')
2350 goto next_character;
2354 shell_ungetc (peek_char);
2356 /* If the next character is to be quoted, note it now. */
2357 if (cd == 0 || cd == '`' ||
2358 (cd == '"' && member (peek_char, slashify_in_quotes)))
2359 pass_next_character++;
2366 #if defined (DPAREN_ARITHMETIC)
2367 /* Parse a ksh-style ((...)) expression. */
2368 if (parser_state & PST_DBLPAREN)
2372 /* If we've already consumed a right paren that should be part of
2373 the expression, push it back so the paren matching code won't
2374 return prematurely. */
2375 if (character == '(') /* ) */
2376 shell_ungetc (character);
2377 exp_lineno = line_number;
2378 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
2379 parser_state &= ~PST_DBLPAREN;
2380 if (ttok == &matched_pair_error)
2382 /* Check that the next character is the closing right paren. If
2383 not, this is a syntax error. ( */
2384 if (shell_getc (0) != ')')
2386 FREE (ttok); /* ( */
2387 parser_error (exp_lineno, "missing closing `)' for arithmetic expression");
2390 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
2391 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
2392 token[token_index++] = '"';
2393 if (character != '(') /* ) */
2394 token[token_index++] = character;
2395 strncpy (token + token_index, ttok, ttoklen - 1);
2396 token_index += ttoklen - 1;
2397 token[token_index++] = '"';
2399 dollar_present = all_digits = 0;
2403 #endif /* DPAREN_ARITHMETIC */
2405 /* Parse a matched pair of quote characters. */
2406 if (shellquote (character))
2408 push_delimiter (dstack, character);
2409 ttok = parse_matched_pair (character, character, character, &ttoklen, 0);
2410 pop_delimiter (dstack);
2411 if (ttok == &matched_pair_error)
2412 return -1; /* Bail immediately. */
2413 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2414 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
2415 token[token_index++] = character;
2416 strcpy (token + token_index, ttok);
2417 token_index += ttoklen;
2420 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
2422 goto next_character;
2425 /* If the delimiter character is not single quote, parse some of
2426 the shell expansions that must be read as a single word. */
2427 #if defined (PROCESS_SUBSTITUTION)
2428 if (character == '$' || character == '<' || character == '>')
2430 if (character == '$')
2431 #endif /* !PROCESS_SUBSTITUTION */
2433 peek_char = shell_getc (1);
2434 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
2435 if (peek_char == '(' ||
2436 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
2438 if (peek_char == '{') /* } */
2439 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, 0);
2440 else if (peek_char == '(') /* ) */
2441 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
2443 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
2444 if (ttok == &matched_pair_error)
2445 return -1; /* Bail immediately. */
2446 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2448 TOKEN_DEFAULT_GROW_SIZE);
2449 token[token_index++] = character;
2450 token[token_index++] = peek_char;
2451 strcpy (token + token_index, ttok);
2452 token_index += ttoklen;
2456 goto next_character;
2458 /* This handles $'...' and $"..." new-style quoted strings. */
2459 else if (character == '$' && (peek_char == '\'' || peek_char == '"'))
2461 ttok = parse_matched_pair (peek_char, peek_char, peek_char, &ttoklen, 0);
2462 if (ttok == &matched_pair_error)
2464 if (peek_char == '\'')
2465 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
2467 ttrans = localeexpand (ttok, 0, ttoklen - 1, &ttranslen);
2469 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2,
2471 TOKEN_DEFAULT_GROW_SIZE);
2472 token[token_index++] = peek_char;
2473 strcpy (token + token_index, ttrans);
2474 token_index += ttranslen;
2475 token[token_index++] = peek_char;
2479 goto next_character;
2482 shell_ungetc (peek_char);
2485 #if defined (ARRAY_VARS)
2486 /* Identify possible compound array variable assignment. */
2487 else if (character == '=')
2489 peek_char = shell_getc (1);
2490 if (peek_char == '(') /* ) */
2492 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
2493 if (ttok == &matched_pair_error)
2494 return -1; /* Bail immediately. */
2495 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
2497 TOKEN_DEFAULT_GROW_SIZE);
2498 token[token_index++] = character;
2499 token[token_index++] = peek_char;
2500 strcpy (token + token_index, ttok);
2501 token_index += ttoklen;
2504 goto next_character;
2507 shell_ungetc (peek_char);
2511 /* When not parsing a multi-character word construct, shell meta-
2512 characters break words. */
2513 if (shellbreak (character))
2515 shell_ungetc (character);
2521 all_digits &= digit (character);
2522 dollar_present |= character == '$';
2524 if (character == CTLESC || character == CTLNUL)
2525 token[token_index++] = CTLESC;
2527 token[token_index++] = character;
2529 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
2530 TOKEN_DEFAULT_GROW_SIZE);
2533 if (character == '\n' && interactive &&
2534 (bash_input.type == st_stdin || bash_input.type == st_stream))
2537 /* We want to remove quoted newlines (that is, a \<newline> pair)
2538 unless we are within single quotes or pass_next_character is
2539 set (the shell equivalent of literal-next). */
2540 cd = current_delimiter (dstack);
2541 character = shell_getc (cd != '\'' && pass_next_character == 0);
2542 } /* end for (;;) */
2546 token[token_index] = '\0';
2548 /* Check to see what thing we should return. If the last_read_token
2549 is a `<', or a `&', or the character which ended this token is
2550 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
2551 Otherwise, it is just a word, and should be returned as such. */
2552 if (all_digits && (character == '<' || character == '>' ||
2553 last_read_token == LESS_AND ||
2554 last_read_token == GREATER_AND))
2556 yylval.number = atoi (token);
2560 /* Check for special case tokens. */
2561 result = special_case_tokens (token);
2566 /* Posix.2 does not allow reserved words to be aliased, so check for all
2567 of them, including special cases, before expanding the current token
2569 if (posixly_correct)
2570 CHECK_FOR_RESERVED_WORD (token);
2572 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
2573 inhibits alias expansion. */
2574 if (expand_aliases && quoted == 0)
2576 result = alias_expand_token (token);
2577 if (result == RE_READ_TOKEN)
2578 return (RE_READ_TOKEN);
2579 else if (result == NO_EXPANSION)
2580 parser_state &= ~PST_ALEXPNEXT;
2583 /* If not in Posix.2 mode, check for reserved words after alias
2585 if (posixly_correct == 0)
2587 CHECK_FOR_RESERVED_WORD (token);
2589 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
2590 the_word->word = xmalloc (1 + token_index);
2591 the_word->flags = 0;
2592 strcpy (the_word->word, token);
2594 the_word->flags |= W_HASDOLLAR;
2596 the_word->flags |= W_QUOTED;
2597 /* A word is an assignment if it appears at the beginning of a
2598 simple command, or after another assignment word. This is
2599 context-dependent, so it cannot be handled in the grammar. */
2600 if (assignment (token))
2602 the_word->flags |= W_ASSIGNMENT;
2603 /* Don't perform word splitting on assignment statements. */
2604 if (assignment_acceptable (last_read_token))
2605 the_word->flags |= W_NOSPLIT;
2608 yylval.word = the_word;
2610 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
2611 ? ASSIGNMENT_WORD : WORD;
2613 if (last_read_token == FUNCTION)
2615 parser_state |= PST_ALLOWOPNBRC;
2616 function_dstart = line_number;
2622 /* $'...' ANSI-C expand the portion of STRING between START and END and
2623 return the result. The result cannot be longer than the input string. */
2625 ansiexpand (string, start, end, lenp)
2627 int start, end, *lenp;
2632 temp = xmalloc (end - start + 1);
2633 for (tlen = 0, len = start; len < end; )
2634 temp[tlen++] = string[len++];
2639 t = ansicstr (temp, tlen, (int *)NULL);
2653 /* $"..." -- Translate the portion of STRING between START and END
2654 according to current locale using gettext (if available) and return
2655 the result. The caller will take care of leaving the quotes intact.
2656 The string will be left without the leading `$' by the caller.
2657 If translation is performed, the translated string will be double-quoted
2658 by the caller. The length of the translated string is returned in LENP,
2661 localeexpand (string, start, end, lenp)
2663 int start, end, *lenp;
2668 temp = xmalloc (end - start + 1);
2669 for (tlen = 0, len = start; len < end; )
2670 temp[tlen++] = string[len++];
2673 /* If we're just dumping translatable strings, don't do anything. */
2674 if (dump_translatable_strings)
2676 printf ("\"%s\"\n", temp);
2683 t = localetrans (temp, tlen, &len);
2697 /* Return 1 if TOKEN is a token that after being read would allow
2698 a reserved word to be seen, else 0. */
2700 reserved_word_acceptable (token)
2703 if (token == '\n' || token == ';' || token == '(' || token == ')' ||
2704 token == '|' || token == '&' || token == '{' ||
2705 token == '}' || /* XXX */
2708 token == TIME || token == TIMEOPT ||
2715 token == SEMI_SEMI ||
2719 token == DONE || /* XXX these two are experimental */
2727 /* Return the index of TOKEN in the alist of reserved words, or -1 if
2728 TOKEN is not a shell reserved word. */
2730 find_reserved_word (token)
2734 for (i = 0; word_token_alist[i].word; i++)
2735 if (STREQ (token, word_token_alist[i].word))
2741 #if defined (READLINE)
2742 /* Called after each time readline is called. This insures that whatever
2743 the new prompt string is gets propagated to readline's local prompt
2746 reset_readline_prompt ()
2750 if (prompt_string_pointer)
2752 temp_prompt = (*prompt_string_pointer)
2753 ? decode_prompt_string (*prompt_string_pointer)
2756 if (temp_prompt == 0)
2758 temp_prompt = xmalloc (1);
2759 temp_prompt[0] = '\0';
2762 FREE (current_readline_prompt);
2763 current_readline_prompt = temp_prompt;
2766 #endif /* READLINE */
2769 #if defined (HISTORY)
2770 /* A list of tokens which can be followed by newlines, but not by
2771 semi-colons. When concatenating multiple lines of history, the
2772 newline separator for such tokens is replaced with a space. */
2773 static int no_semi_successors[] = {
2774 '\n', '{', '(', ')', ';', '&', '|',
2775 CASE, DO, ELSE, IF, SEMI_SEMI, THEN, UNTIL, WHILE, AND_AND, OR_OR, IN,
2779 /* If we are not within a delimited expression, try to be smart
2780 about which separators can be semi-colons and which must be
2781 newlines. Returns the string that should be added into the
2784 history_delimiting_chars ()
2788 if (dstack.delimiter_depth != 0)
2791 /* First, handle some special cases. */
2793 /* If we just read `()', assume it's a function definition, and don't
2794 add a semicolon. If the token before the `)' was not `(', and we're
2795 not in the midst of parsing a case statement, assume it's a
2796 parenthesized command and add the semicolon. */
2798 if (token_before_that == ')')
2800 if (two_tokens_ago == '(') /*)*/ /* function def */
2802 /* This does not work for subshells inside case statement
2803 command lists. It's a suboptimal solution. */
2804 else if (parser_state & PST_CASESTMT) /* case statement pattern */
2807 return "; "; /* (...) subshell */
2810 for (i = 0; no_semi_successors[i]; i++)
2812 if (token_before_that == no_semi_successors[i])
2818 #endif /* HISTORY */
2820 /* Issue a prompt, or prepare to issue a prompt when the next character
2827 if (!interactive) /* XXX */
2830 ps1_prompt = get_string_value ("PS1");
2831 ps2_prompt = get_string_value ("PS2");
2833 if (!prompt_string_pointer)
2834 prompt_string_pointer = &ps1_prompt;
2836 temp_prompt = *prompt_string_pointer
2837 ? decode_prompt_string (*prompt_string_pointer)
2840 if (temp_prompt == 0)
2842 temp_prompt = xmalloc (1);
2843 temp_prompt[0] = '\0';
2846 current_prompt_string = *prompt_string_pointer;
2847 prompt_string_pointer = &ps2_prompt;
2849 #if defined (READLINE)
2850 if (!no_line_editing)
2852 FREE (current_readline_prompt);
2853 current_readline_prompt = temp_prompt;
2856 #endif /* READLINE */
2858 FREE (current_decoded_prompt);
2859 current_decoded_prompt = temp_prompt;
2866 fprintf (stderr, "%s", current_decoded_prompt);
2870 /* Return a string which will be printed as a prompt. The string
2871 may contain special characters which are decoded as follows:
2874 \e escape (ascii 033)
2875 \d the date in Day Mon Date format
2876 \h the hostname up to the first `.'
2879 \s the name of the shell
2880 \t the time in 24-hour hh:mm:ss format
2881 \T the time in 12-hour hh:mm:ss format
2882 \@ the time in 12-hour am/pm format
2883 \v the version of bash (e.g., 2.00)
2884 \V the release of bash, version + patchlevel (e.g., 2.00.0)
2885 \w the current working directory
2886 \W the last element of $PWD
2888 \# the command number of this command
2889 \! the history number of this command
2890 \$ a $ or a # if you are root
2891 \nnn character code nnn in octal
2893 \[ begin a sequence of non-printing chars
2894 \] end a sequence of non-printing chars
2896 #define PROMPT_GROWTH 50
2898 decode_prompt_string (string)
2903 struct dstack save_dstack;
2904 #if defined (PROMPT_STRING_DECODE)
2905 int result_size, result_index;
2907 char *temp, octal_string[4];
2910 result = xmalloc (result_size = PROMPT_GROWTH);
2911 result[result_index = 0] = 0;
2912 temp = (char *)NULL;
2914 while (c = *string++)
2916 if (posixly_correct && c == '!')
2920 temp = savestring ("!");
2925 #if !defined (HISTORY)
2926 temp = savestring ("1");
2928 temp = itos (history_number ());
2929 #endif /* HISTORY */
2930 string--; /* add_string increments string again. */
2948 strncpy (octal_string, string, 3);
2949 octal_string[3] = '\0';
2951 n = read_octal (octal_string);
2954 if (n == CTLESC || n == CTLNUL)
2980 /* Make the current time/date into a string. */
2981 the_time = time (0);
2982 temp = ctime (&the_time);
2984 temp = (c != 'd') ? savestring (temp + 11) : savestring (temp);
2985 temp[(c != 'd') ? 8 : 10] = '\0';
2987 /* quick and dirty conversion to 12-hour time */
2988 if (c == 'T' || c == '@')
2992 temp[5] = 'a'; /* am/pm format */
3003 temp[0] = (n / 10) + '0';
3004 temp[1] = (n % 10) + '0';
3006 if (n >= 0 && temp[5] == 'a')
3013 temp[0] = no_line_editing ? '\n' : '\r';
3014 temp[1] = no_line_editing ? '\0' : '\n';
3019 temp = base_pathname (shell_name);
3020 temp = savestring (temp);
3027 strcpy (temp, dist_version);
3029 sprintf (temp, "%s.%d", dist_version, patch_level);
3035 /* Use the value of PWD because it is much more efficient. */
3036 char t_string[PATH_MAX];
3038 temp = get_string_value ("PWD");
3042 if (getcwd (t_string, sizeof(t_string)) == 0)
3049 strcpy (t_string, temp);
3053 t = strrchr (t_string, '/');
3054 if (t && t != t_string)
3055 strcpy (t_string, t + 1);
3058 strcpy (t_string, polite_directory_format (t_string));
3060 /* If we're going to be expanding the prompt string later,
3061 quote the directory name. */
3062 if (promptvars || posixly_correct)
3063 temp = backslash_quote (t_string);
3065 temp = savestring (t_string);
3071 temp = savestring (current_user.user_name);
3076 temp = savestring (current_host_name);
3077 if (c == 'h' && (t = (char *)strchr (temp, '.')))
3082 temp = itos (current_command_number);
3086 #if !defined (HISTORY)
3087 temp = savestring ("1");
3089 temp = itos (history_number ());
3090 #endif /* HISTORY */
3095 temp[0] = current_user.euid == 0 ? '#' : '$';
3099 #if defined (READLINE)
3104 temp[1] = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
3107 #endif /* READLINE */
3118 temp[0] = (c == 'a') ? '\07' : '\033';
3132 sub_append_string (temp, result, &result_index, &result_size);
3133 temp = (char *)NULL; /* Freed in sub_append_string (). */
3134 result[result_index] = '\0';
3140 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
3141 result[result_index++] = c;
3142 result[result_index] = '\0';
3145 #else /* !PROMPT_STRING_DECODE */
3146 result = savestring (string);
3147 #endif /* !PROMPT_STRING_DECODE */
3149 /* Save the delimiter stack and point `dstack' to temp space so any
3150 command substitutions in the prompt string won't result in screwing
3151 up the parser's quoting state. */
3152 save_dstack = dstack;
3153 dstack = temp_dstack;
3154 dstack.delimiter_depth = 0;
3156 /* Perform variable and parameter expansion and command substitution on
3157 the prompt string. */
3158 if (promptvars || posixly_correct)
3160 list = expand_string_unsplit (result, Q_DOUBLE_QUOTES);
3162 result = string_list (list);
3163 dispose_words (list);
3167 t = dequote_string (result);
3172 dstack = save_dstack;
3177 /* Report a syntax error, and restart the parser. Call here for fatal
3182 report_syntax_error ((char *)NULL);
3187 /* Report a syntax error with line numbers, etc.
3188 Call here for recoverable errors. If you have a message to print,
3189 then place it in MESSAGE, otherwise pass NULL and this will figure
3190 out an appropriate message for you. */
3192 report_syntax_error (message)
3201 parser_error (line_number, "%s", message);
3202 if (interactive && EOF_Reached)
3204 last_command_exit_value = EX_USAGE;
3208 /* If the line of input we're reading is not null, try to find the
3209 objectionable token. */
3210 if (shell_input_line && *shell_input_line)
3212 t = shell_input_line;
3213 i = shell_input_line_index;
3216 if (i && t[i] == '\0')
3219 while (i && (whitespace (t[i]) || t[i] == '\n'))
3225 while (i && (member (t[i], " \n\t;|&") == 0))
3228 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
3231 /* Print the offending token. */
3232 if (token_end || (i == 0 && token_end == 0))
3236 msg = xmalloc (1 + (token_end - i));
3237 strncpy (msg, t + i, token_end - i);
3238 msg[token_end - i] = '\0';
3240 else /* one-character token */
3247 parser_error (line_number, "syntax error near unexpected token `%s'", msg);
3253 /* If not interactive, print the line containing the error. */
3254 if (interactive == 0)
3256 msg = savestring (shell_input_line);
3257 token_end = strlen (msg);
3258 while (token_end && msg[token_end - 1] == '\n')
3259 msg[--token_end] = '\0';
3261 parser_error (line_number, "`%s'", msg);
3267 msg = EOF_Reached ? "syntax error: unexpected end of file" : "syntax error";
3268 parser_error (line_number, "%s", msg);
3269 /* When the shell is interactive, this file uses EOF_Reached
3270 only for error reporting. Other mechanisms are used to
3271 decide whether or not to exit. */
3272 if (interactive && EOF_Reached)
3275 last_command_exit_value = EX_USAGE;
3278 /* ??? Needed function. ??? We have to be able to discard the constructs
3279 created during parsing. In the case of error, we want to return
3280 allocated objects to the memory pool. In the case of no error, we want
3281 to throw away the information about where the allocated objects live.
3282 (dispose_command () will actually free the command. */
3284 discard_parser_constructs (error_p)
3289 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
3291 /* A flag denoting whether or not ignoreeof is set. */
3294 /* The number of times that we have encountered an EOF character without
3295 another character intervening. When this gets above the limit, the
3296 shell terminates. */
3297 int eof_encountered = 0;
3299 /* The limit for eof_encountered. */
3300 int eof_encountered_limit = 10;
3302 /* If we have EOF as the only input unit, this user wants to leave
3303 the shell. If the shell is not interactive, then just leave.
3304 Otherwise, if ignoreeof is set, and we haven't done this the
3305 required number of times in a row, print a message. */
3307 handle_eof_input_unit ()
3311 /* shell.c may use this to decide whether or not to write out the
3312 history, among other things. We use it only for error reporting
3317 /* If the user wants to "ignore" eof, then let her do so, kind of. */
3320 if (eof_encountered < eof_encountered_limit)
3322 fprintf (stderr, "Use \"%s\" to leave the shell.\n",
3323 login_shell ? "logout" : "exit");
3325 /* Reset the prompt string to be $PS1. */
3326 prompt_string_pointer = (char **)NULL;
3328 last_read_token = current_token = '\n';
3333 /* In this case EOF should exit the shell. Do it now. */
3335 exit_builtin ((WORD_LIST *)NULL);
3339 /* We don't write history files, etc., for non-interactive shells. */