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. */
23 #include "bashtypes.h"
30 #if defined (READLINE)
31 # include <readline/readline.h>
35 # include "bashhist.h"
36 # include <readline/history.h>
39 #if defined (JOB_CONTROL)
41 #endif /* JOB_CONTROL */
47 #if defined (PROMPT_STRING_DECODE)
48 #include <sys/param.h>
51 #endif /* PROMPT_STRING_DECODE */
54 extern int eof_encountered;
55 extern int no_line_editing;
56 extern int current_command_number;
57 extern int interactive, interactive_shell, login_shell;
58 extern int posixly_correct;
59 extern int last_command_exit_value;
60 extern int interrupt_immediately;
61 extern char *shell_name, *current_host_name;
62 extern Function *last_shell_builtin, *this_shell_builtin;
63 #if defined (READLINE)
64 extern int bash_readline_initialized;
66 #if defined (BUFFERED_INPUT)
67 extern int bash_input_fd_changed;
70 /* **************************************************************** */
72 /* "Forward" declarations */
74 /* **************************************************************** */
76 /* This is kind of sickening. In order to let these variables be seen by
77 all the functions that need them, I am forced to place their declarations
78 far away from the place where they should logically be found. */
80 static int reserved_word_acceptable ();
81 static int read_token ();
83 static void report_syntax_error ();
84 static void handle_eof_input_unit ();
85 static void prompt_again ();
86 static void reset_readline_prompt ();
87 static void print_prompt ();
89 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
90 char *ps1_prompt, *ps2_prompt;
92 /* Handle on the current prompt string. Indirectly points through
93 ps1_ or ps2_prompt. */
94 char **prompt_string_pointer = (char **)NULL;
95 char *current_prompt_string;
97 /* The decoded prompt string. Used if READLINE is not defined or if
98 editing is turned off. Analogous to current_readline_prompt. */
99 static char *current_decoded_prompt;
101 /* The number of lines read from input while creating the current command. */
102 int current_command_line_count = 0;
104 /* Variables to manage the task of reading here documents, because we need to
105 defer the reading until after a complete command has been collected. */
106 static REDIRECT *redir_stack[10];
107 int need_here_doc = 0;
109 /* Where shell input comes from. History expansion is performed on each
110 line when the shell is interactive. */
111 static char *shell_input_line = (char *)NULL;
112 static int shell_input_line_index = 0;
113 static int shell_input_line_size = 0; /* Amount allocated for shell_input_line. */
114 static int shell_input_line_len = 0; /* strlen (shell_input_line) */
116 /* Either zero or EOF. */
117 static int shell_input_line_terminator = 0;
119 static REDIRECTEE redir;
123 WORD_DESC *word; /* the word that we read. */
124 int number; /* the number that we read. */
125 WORD_LIST *word_list;
129 PATTERN_LIST *pattern;
132 /* Reserved words. Members of the first group are only recognized
133 in the case that they are preceded by a list_terminator. Members
134 of the second group are recognized only under special circumstances. */
135 %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION
138 /* More general tokens. yylex () knows how to make these. */
139 %token <word> WORD ASSIGNMENT_WORD
140 %token <number> NUMBER
141 %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND
142 %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
145 /* The types that the various syntactical units return. */
147 %type <command> inputunit command pipeline
148 %type <command> list list0 list1 simple_list simple_list1
149 %type <command> simple_command shell_command_1 shell_command select_command
150 %type <command> group_command function_def if_command elif_clause subshell
151 %type <redirect> redirection redirections
152 %type <element> simple_command_element
153 %type <word_list> words pattern
154 %type <pattern> pattern_list case_clause_sequence case_clause_1 pattern_list_1
158 %left '&' ';' '\n' yacc_EOF
163 inputunit: simple_list '\n'
165 /* Case of regular command. Discard the error
166 safety net,and return the command just parsed. */
169 discard_parser_constructs (0);
174 /* Case of regular command, but not a very
175 interesting one. Return a NULL command. */
176 global_command = (COMMAND *)NULL;
182 /* Error during parsing. Return NULL command. */
183 global_command = (COMMAND *)NULL;
185 discard_parser_constructs (1);
197 /* Case of EOF seen by itself. Do ignoreeof or
199 global_command = (COMMAND *)NULL;
200 handle_eof_input_unit ();
206 { $$ = (WORD_LIST *)NULL; }
208 { $$ = make_word_list ($2, $1); }
211 redirection: '>' WORD
214 $$ = make_redirection (1, r_output_direction, redir);
219 $$ = make_redirection (0, r_input_direction, redir);
224 $$ = make_redirection ($1, r_output_direction, redir);
229 $$ = make_redirection ($1, r_input_direction, redir);
231 | GREATER_GREATER WORD
234 $$ = make_redirection (1, r_appending_to, redir);
236 | NUMBER GREATER_GREATER WORD
239 $$ = make_redirection ($1, r_appending_to, redir);
244 $$ = make_redirection (0, r_reading_until, redir);
245 redir_stack[need_here_doc++] = $$;
247 | NUMBER LESS_LESS WORD
250 $$ = make_redirection ($1, r_reading_until, redir);
251 redir_stack[need_here_doc++] = $$;
256 $$ = make_redirection (0, r_duplicating_input, redir);
258 | NUMBER LESS_AND NUMBER
261 $$ = make_redirection ($1, r_duplicating_input, redir);
266 $$ = make_redirection (1, r_duplicating_output, redir);
268 | NUMBER GREATER_AND NUMBER
271 $$ = make_redirection ($1, r_duplicating_output, redir);
276 $$ = make_redirection (0, r_duplicating_input_word, redir);
278 | NUMBER LESS_AND WORD
281 $$ = make_redirection ($1, r_duplicating_input_word, redir);
286 $$ = make_redirection (1, r_duplicating_output_word, redir);
288 | NUMBER GREATER_AND WORD
291 $$ = make_redirection ($1, r_duplicating_output_word, redir);
293 | LESS_LESS_MINUS WORD
296 $$ = make_redirection
297 (0, r_deblank_reading_until, redir);
298 redir_stack[need_here_doc++] = $$;
300 | NUMBER LESS_LESS_MINUS WORD
303 $$ = make_redirection
304 ($1, r_deblank_reading_until, redir);
305 redir_stack[need_here_doc++] = $$;
310 $$ = make_redirection (1, r_close_this, redir);
312 | NUMBER GREATER_AND '-'
315 $$ = make_redirection ($1, r_close_this, redir);
320 $$ = make_redirection (0, r_close_this, redir);
322 | NUMBER LESS_AND '-'
325 $$ = make_redirection ($1, r_close_this, redir);
330 $$ = make_redirection (1, r_err_and_out, redir);
332 | NUMBER LESS_GREATER WORD
335 $$ = make_redirection ($1, r_input_output, redir);
343 $$ = make_redirection (0, r_input_output, redir);
346 t1 = make_redirection (0, r_input_direction, redir);
347 redir.filename = copy_word ($2);
348 t2 = make_redirection (1, r_output_direction, redir);
356 $$ = make_redirection (1, r_output_force, redir);
358 | NUMBER GREATER_BAR WORD
361 $$ = make_redirection ($1, r_output_force, redir);
365 simple_command_element: WORD
366 { $$.word = $1; $$.redirect = 0; }
368 { $$.word = $1; $$.redirect = 0; }
370 { $$.redirect = $1; $$.word = 0; }
373 redirections: redirection
377 | redirections redirection
379 register REDIRECT *t = $1;
388 simple_command: simple_command_element
389 { $$ = make_simple_command ($1, (COMMAND *)NULL); }
390 | simple_command simple_command_element
391 { $$ = make_simple_command ($2, $1); }
394 command: simple_command
395 { $$ = clean_simple_command ($1); }
400 shell_command: shell_command_1
402 | shell_command_1 redirections
406 register REDIRECT *t;
407 for (t = $1->redirects; t->next; t = t->next)
417 shell_command_1: FOR WORD newlines DO list DONE
418 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5); }
419 | FOR WORD newlines '{' list '}'
420 { $$ = make_for_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5); }
421 | FOR WORD ';' newlines DO list DONE
422 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
423 | FOR WORD ';' newlines '{' list '}'
424 { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
425 | FOR WORD newlines IN words list_terminator newlines DO list DONE
426 { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
427 | FOR WORD newlines IN words list_terminator newlines '{' list '}'
428 { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
430 | CASE WORD newlines IN newlines ESAC
431 { $$ = make_case_command ($2, (PATTERN_LIST *)NULL); }
432 | CASE WORD newlines IN case_clause_sequence newlines ESAC
433 { $$ = make_case_command ($2, $5); }
434 | CASE WORD newlines IN case_clause_1 ESAC
435 { $$ = make_case_command ($2, $5); }
436 | WHILE list DO list DONE
437 { $$ = make_while_command ($2, $4); }
438 | UNTIL list DO list DONE
439 { $$ = make_until_command ($2, $4); }
452 select_command: SELECT WORD newlines DO list DONE
454 #if defined (SELECT_COMMAND)
455 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5);
458 | SELECT WORD newlines '{' list '}'
460 #if defined (SELECT_COMMAND)
461 $$ = make_select_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5);
464 | SELECT WORD ';' newlines DO list DONE
466 #if defined (SELECT_COMMAND)
467 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
470 | SELECT WORD ';' newlines '{' list '}'
472 #if defined (SELECT_COMMAND)
473 $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
476 | SELECT WORD newlines IN words list_terminator newlines DO list DONE
478 #if defined (SELECT_COMMAND)
479 $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
482 | SELECT WORD newlines IN words list_terminator newlines '{' list '}'
484 #if defined (SELECT_COMMAND)
485 $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
490 function_def: WORD '(' ')' newlines group_command
491 { $$ = make_function_def ($1, $5); }
493 | WORD '(' ')' newlines group_command redirections
494 { $5->redirects = $6; $$ = make_function_def ($1, $5); }
496 | FUNCTION WORD '(' ')' newlines group_command
497 { $$ = make_function_def ($2, $6); }
499 | FUNCTION WORD '(' ')' newlines group_command redirections
500 { $6->redirects = $7; $$ = make_function_def ($2, $6); }
502 | FUNCTION WORD newlines group_command
503 { $$ = make_function_def ($2, $4); }
505 | FUNCTION WORD newlines group_command redirections
506 { $4->redirects = $5; $$ = make_function_def ($2, $4); }
509 subshell: '(' list ')'
510 { $2->flags |= CMD_WANT_SUBSHELL; $$ = $2; }
513 if_command: IF list THEN list FI
514 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
515 | IF list THEN list ELSE list FI
516 { $$ = make_if_command ($2, $4, $6); }
517 | IF list THEN list elif_clause FI
518 { $$ = make_if_command ($2, $4, $5); }
522 group_command: '{' list '}'
523 { $$ = make_group_command ($2); }
526 elif_clause: ELIF list THEN list
527 { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
528 | ELIF list THEN list ELSE list
529 { $$ = make_if_command ($2, $4, $6); }
530 | ELIF list THEN list elif_clause
531 { $$ = make_if_command ($2, $4, $5); }
534 case_clause_1: pattern_list_1
535 | case_clause_sequence pattern_list_1
536 { $2->next = $1; $$ = $2; }
539 pattern_list_1: newlines pattern ')' list
540 { $$ = make_pattern_list ($2, $4); }
541 | newlines pattern ')' newlines
542 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
543 | newlines '(' pattern ')' list
544 { $$ = make_pattern_list ($3, $5); }
545 | newlines '(' pattern ')' newlines
546 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
549 case_clause_sequence: pattern_list
550 | case_clause_sequence pattern_list
551 { $2->next = $1; $$ = $2; }
554 pattern_list: newlines pattern ')' list SEMI_SEMI
555 { $$ = make_pattern_list ($2, $4); }
556 | newlines pattern ')' newlines SEMI_SEMI
557 { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
558 | newlines '(' pattern ')' list SEMI_SEMI
559 { $$ = make_pattern_list ($3, $5); }
560 | newlines '(' pattern ')' newlines SEMI_SEMI
561 { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
565 { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
567 { $$ = make_word_list ($3, $1); }
570 /* A list allows leading or trailing newlines and
571 newlines as operators (equivalent to semicolons).
572 It must end with a newline or semicolon.
573 Lists are used within commands such as if, for, while. */
579 gather_here_documents ();
584 | list1 '\n' newlines
587 if ($1->type == cm_connection)
588 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
590 $$ = command_connect ($1, (COMMAND *)NULL, '&');
596 list1: list1 AND_AND newlines list1
597 { $$ = command_connect ($1, $4, AND_AND); }
598 | list1 OR_OR newlines list1
599 { $$ = command_connect ($1, $4, OR_OR); }
600 | list1 '&' newlines list1
602 if ($1->type == cm_connection)
603 $$ = connect_async_list ($1, $4, '&');
605 $$ = command_connect ($1, $4, '&');
607 | list1 ';' newlines list1
608 { $$ = command_connect ($1, $4, ';'); }
609 | list1 '\n' newlines list1
610 { $$ = command_connect ($1, $4, ';'); }
615 $2->flags |= CMD_INVERT_RETURN;
629 /* A simple_list is a list that contains no significant newlines
630 and no leading or trailing newlines. Newlines are allowed
631 only following operators, where they are not significant.
633 This is what an inputunit consists of. */
635 simple_list: simple_list1
639 gather_here_documents ();
643 if ($1->type == cm_connection)
644 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
646 $$ = command_connect ($1, (COMMAND *)NULL, '&');
648 gather_here_documents ();
654 gather_here_documents ();
658 simple_list1: simple_list1 AND_AND newlines simple_list1
659 { $$ = command_connect ($1, $4, AND_AND); }
660 | simple_list1 OR_OR newlines simple_list1
661 { $$ = command_connect ($1, $4, OR_OR); }
662 | simple_list1 '&' simple_list1
664 if ($1->type == cm_connection)
665 $$ = connect_async_list ($1, $3, '&');
667 $$ = command_connect ($1, $3, '&');
669 | simple_list1 ';' simple_list1
670 { $$ = command_connect ($1, $3, ';'); }
675 $2->flags |= CMD_INVERT_RETURN;
681 pipeline '|' newlines pipeline
682 { $$ = command_connect ($1, $4, '|'); }
688 /* Initial size to allocate for tokens, and the
689 amount to grow them by. */
690 #define TOKEN_DEFAULT_GROW_SIZE 512
692 /* The token currently being read. */
693 static int current_token = 0;
695 /* The last read token, or NULL. read_token () uses this for context
697 static int last_read_token = 0;
699 /* The token read prior to last_read_token. */
700 static int token_before_that = 0;
702 /* If non-zero, it is the token that we want read_token to return
703 regardless of what text is (or isn't) present to be read. This
704 is reset by read_token. */
705 static int token_to_read = 0;
707 /* Global var is non-zero when end of file has been reached. */
710 /* yy_getc () returns the next available character from input or EOF.
711 yy_ungetc (c) makes `c' the next character to read.
712 init_yy_io (get, unget, type, location) makes the function GET the
713 installed function for getting the next character, makes UNGET the
714 installed function for un-getting a character, sets the type of stream
715 (either string or file) from TYPE, and makes LOCATION point to where
716 the input is coming from. */
718 /* Unconditionally returns end-of-file. */
724 /* Variable containing the current get and unget functions.
725 See ./input.h for a clearer description. */
726 BASH_INPUT bash_input;
728 /* Set all of the fields in BASH_INPUT to NULL. */
730 initialize_bash_input ()
733 bash_input.name = (char *)NULL;
734 bash_input.location.file = (FILE *)NULL;
735 bash_input.location.string = (char *)NULL;
736 bash_input.getter = (Function *)NULL;
737 bash_input.ungetter = (Function *)NULL;
740 /* Set the contents of the current bash input stream from
741 GET, UNGET, TYPE, NAME, and LOCATION. */
743 init_yy_io (get, unget, type, name, location)
744 Function *get, *unget;
747 INPUT_STREAM location;
749 bash_input.type = type;
750 FREE (bash_input.name);
753 bash_input.name = savestring (name);
755 bash_input.name = (char *)NULL;
758 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
760 bash_input.location = location;
762 bash_input.getter = get;
763 bash_input.ungetter = unget;
766 /* Call this to get the next character of input. */
769 return (*(bash_input.getter)) ();
772 /* Call this to unget C. That is, to make C the next character
777 return (*(bash_input.ungetter)) (c);
780 #if defined (BUFFERED_INPUT)
782 input_file_descriptor ()
784 switch (bash_input.type)
787 return (fileno (bash_input.location.file));
789 return (bash_input.location.buffered_fd);
791 return (fileno (stdin));
794 #endif /* BUFFERED_INPUT */
796 /* **************************************************************** */
798 /* Let input be read from readline (). */
800 /* **************************************************************** */
802 #if defined (READLINE)
803 char *current_readline_prompt = (char *)NULL;
804 char *current_readline_line = (char *)NULL;
805 int current_readline_line_index = 0;
810 if (!current_readline_line)
812 SigHandler *old_sigint;
815 if (!bash_readline_initialized)
816 initialize_readline ();
818 #if defined (JOB_CONTROL)
820 give_terminal_to (shell_pgrp);
821 #endif /* JOB_CONTROL */
823 if (signal_is_ignored (SIGINT) == 0)
825 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
826 interrupt_immediately++;
829 if (!current_readline_prompt)
830 current_readline_line = readline ("");
832 current_readline_line = readline (current_readline_prompt);
834 if (signal_is_ignored (SIGINT) == 0)
836 interrupt_immediately--;
837 set_signal_handler (SIGINT, old_sigint);
840 /* Reset the prompt to whatever is in the decoded value of
841 prompt_string_pointer. */
842 reset_readline_prompt ();
844 current_readline_line_index = 0;
846 if (!current_readline_line)
849 line_len = strlen (current_readline_line);
850 current_readline_line = xrealloc (current_readline_line, 2 + line_len);
851 current_readline_line[line_len++] = '\n';
852 current_readline_line[line_len] = '\0';
855 if (!current_readline_line[current_readline_line_index])
857 free (current_readline_line);
858 current_readline_line = (char *)NULL;
859 return (yy_readline_get ());
863 int c = (unsigned char)current_readline_line[current_readline_line_index++];
869 yy_readline_unget (c)
871 if (current_readline_line_index && current_readline_line)
872 current_readline_line[--current_readline_line_index] = c;
877 with_input_from_stdin ()
879 INPUT_STREAM location;
881 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
883 location.string = current_readline_line;
884 init_yy_io (yy_readline_get, yy_readline_unget,
885 st_stdin, "readline stdin", location);
889 #else /* !READLINE */
892 with_input_from_stdin ()
894 with_input_from_stream (stdin, "stdin");
896 #endif /* !READLINE */
898 /* **************************************************************** */
900 /* Let input come from STRING. STRING is zero terminated. */
902 /* **************************************************************** */
907 register unsigned char *string;
910 string = bash_input.location.string;
913 /* If the string doesn't exist, or is empty, EOF found. */
914 if (string && *string)
917 bash_input.location.string = string;
926 *(--bash_input.location.string) = c;
931 with_input_from_string (string, name)
935 INPUT_STREAM location;
937 location.string = string;
939 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
942 /* **************************************************************** */
944 /* Let input come from STREAM. */
946 /* **************************************************************** */
953 if (bash_input.location.file)
954 #if defined (NO_READ_RESTART_ON_SIGNAL)
955 result = (unsigned char)getc_with_restart (bash_input.location.file);
957 result = (unsigned char)getc (bash_input.location.file);
958 #endif /* !NO_READ_RESTART_ON_SIGNAL */
966 #if defined (NO_READ_RESTART_ON_SIGNAL)
967 return (ungetc_with_restart (c, bash_input.location.file));
969 return (ungetc (c, bash_input.location.file));
974 with_input_from_stream (stream, name)
978 INPUT_STREAM location;
980 location.file = stream;
981 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
984 typedef struct stream_saver {
985 struct stream_saver *next;
986 BASH_INPUT bash_input;
988 #if defined (BUFFERED_INPUT)
989 BUFFERED_STREAM *bstream;
990 #endif /* BUFFERED_INPUT */
993 /* The globally known line number. */
996 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
1000 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
1002 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
1004 #if defined (BUFFERED_INPUT)
1005 saver->bstream = (BUFFERED_STREAM *)NULL;
1006 /* If we have a buffered stream, clear out buffers[fd]. */
1007 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1009 saver->bstream = buffers[bash_input.location.buffered_fd];
1010 buffers[bash_input.location.buffered_fd] = (BUFFERED_STREAM *)NULL;
1012 #endif /* BUFFERED_INPUT */
1014 saver->line = line_number;
1015 bash_input.name = (char *)NULL;
1016 saver->next = stream_list;
1017 stream_list = saver;
1018 EOF_Reached = line_number = 0;
1029 STREAM_SAVER *saver = stream_list;
1032 stream_list = stream_list->next;
1034 init_yy_io (saver->bash_input.getter,
1035 saver->bash_input.ungetter,
1036 saver->bash_input.type,
1037 saver->bash_input.name,
1038 saver->bash_input.location);
1040 #if defined (BUFFERED_INPUT)
1041 /* If we have a buffered stream, restore buffers[fd]. */
1042 /* If the input file descriptor was changed while this was on the
1043 save stack, update the buffered fd to the new file descriptor and
1044 re-establish the buffer <-> bash_input fd correspondence. */
1045 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
1047 if (bash_input_fd_changed)
1049 bash_input_fd_changed = 0;
1050 if (default_buffered_input >= 0)
1052 bash_input.location.buffered_fd = default_buffered_input;
1053 saver->bstream->b_fd = default_buffered_input;
1056 buffers[bash_input.location.buffered_fd] = saver->bstream;
1058 #endif /* BUFFERED_INPUT */
1060 line_number = saver->line;
1062 FREE (saver->bash_input.name);
1067 /* Return 1 if a stream of type TYPE is saved on the stack. */
1069 stream_on_stack (type)
1072 register STREAM_SAVER *s;
1074 for (s = stream_list; s; s = s->next)
1075 if (s->bash_input.type == type)
1082 * This is used to inhibit alias expansion and reserved word recognition
1083 * inside case statement pattern lists. A `case statement pattern list'
1085 * everything between the `in' in a `case word in' and the next ')'
1087 * everything between a `;;' and the next `)' or `esac'
1089 static int in_case_pattern_list = 0;
1093 * Pseudo-global variables used in implementing token-wise alias expansion.
1096 static int expand_next_token = 0;
1099 * Pushing and popping strings. This works together with shell_getc to
1100 * implement alias expansion on a per-token basis.
1103 typedef struct string_saver {
1104 struct string_saver *next;
1105 int expand_alias; /* Value to set expand_alias to when string is popped. */
1107 int saved_line_size, saved_line_index, saved_line_terminator;
1110 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
1112 static void save_expansion ();
1115 * Push the current shell_input_line onto a stack of such lines and make S
1116 * the current input. Used when expanding aliases. EXPAND is used to set
1117 * the value of expand_next_token when the string is popped, so that the
1118 * word after the alias in the original line is handled correctly when the
1119 * alias expands to multiple words. TOKEN is the token that was expanded
1120 * into S; it is saved and used to prevent infinite recursive expansion.
1123 push_string (s, expand, token)
1128 STRING_SAVER *temp = (STRING_SAVER *) xmalloc (sizeof (STRING_SAVER));
1130 temp->expand_alias = expand;
1131 temp->saved_line = shell_input_line;
1132 temp->saved_line_size = shell_input_line_size;
1133 temp->saved_line_index = shell_input_line_index;
1134 temp->saved_line_terminator = shell_input_line_terminator;
1135 temp->next = pushed_string_list;
1136 pushed_string_list = temp;
1138 save_expansion (token);
1140 shell_input_line = s;
1141 shell_input_line_size = strlen (s);
1142 shell_input_line_index = 0;
1143 shell_input_line_terminator = '\0';
1144 expand_next_token = 0;
1148 * Make the top of the pushed_string stack be the current shell input.
1149 * Only called when there is something on the stack. Called from shell_getc
1150 * when it thinks it has consumed the string generated by an alias expansion
1151 * and needs to return to the original input line.
1158 FREE (shell_input_line);
1159 shell_input_line = pushed_string_list->saved_line;
1160 shell_input_line_index = pushed_string_list->saved_line_index;
1161 shell_input_line_size = pushed_string_list->saved_line_size;
1162 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
1163 expand_next_token = pushed_string_list->expand_alias;
1165 t = pushed_string_list;
1166 pushed_string_list = pushed_string_list->next;
1173 register STRING_SAVER *t = pushed_string_list, *t1;
1178 FREE (t->saved_line);
1182 pushed_string_list = (STRING_SAVER *)NULL;
1185 /* This is a stack to save the values of all tokens for which alias
1186 expansion has been performed during the current call to read_token ().
1187 It is used to prevent alias expansion loops:
1193 Ideally this would be taken care of by push and pop string, but because
1194 of when strings are popped the stack will not contain the correct
1195 strings to test against. (The popping is done in shell_getc, so that when
1196 the current string is exhausted, shell_getc can simply pop that string off
1197 the stack, restore the previous string, and continue with the character
1198 following the token whose expansion was originally pushed on the stack.)
1200 What we really want is a record of all tokens that have been expanded for
1201 aliases during the `current' call to read_token(). This does that, at the
1202 cost of being somewhat special-purpose (OK, OK vile and unclean). */
1204 typedef struct _exp_saver {
1205 struct _exp_saver *next;
1209 EXPANSION_SAVER *expanded_token_stack = (EXPANSION_SAVER *)NULL;
1217 t = (EXPANSION_SAVER *) xmalloc (sizeof (EXPANSION_SAVER));
1218 t->saved_token = savestring (s);
1219 t->next = expanded_token_stack;
1220 expanded_token_stack = t;
1223 /* Return 1 if TOKEN has already been expanded in the current `stack' of
1224 expansions. If it has been expanded already, it will appear as the value
1225 of saved_token for some entry in the stack of expansions created for the
1226 current token being expanded. */
1228 token_has_been_expanded (token)
1231 register EXPANSION_SAVER *t = expanded_token_stack;
1235 if (STREQ (token, t->saved_token))
1243 free_expansion_stack ()
1245 register EXPANSION_SAVER *t = expanded_token_stack, *t1;
1250 free (t->saved_token);
1254 expanded_token_stack = (EXPANSION_SAVER *)NULL;
1259 /* Return a line of text, taken from wherever yylex () reads input.
1260 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
1261 is non-zero, we remove unquoted \<newline> pairs. This is used by
1262 read_secondary_line to read here documents. */
1264 read_a_line (remove_quoted_newline)
1265 int remove_quoted_newline;
1267 static char *line_buffer = (char *)NULL;
1268 static int buffer_size = 0;
1269 int indx = 0, c, peekc, pass_next;
1276 /* Allow immediate exit if interrupted during input. */
1282 /* If there is no more input, then we return NULL. */
1286 return ((char *)NULL);
1290 /* `+2' in case the final character in the buffer is a newline. */
1291 if (indx + 2 > buffer_size)
1293 line_buffer = xmalloc (buffer_size = 128);
1295 line_buffer = xrealloc (line_buffer, buffer_size += 128);
1297 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
1298 here document with an unquoted delimiter. In this case,
1299 the line will be expanded as if it were in double quotes.
1300 We allow a backslash to escape the next character, but we
1301 need to treat the backslash specially only if a backslash
1302 quoting a backslash-newline pair appears in the line. */
1305 line_buffer[indx++] = c;
1308 else if (c == '\\' && remove_quoted_newline)
1312 continue; /* Make the unquoted \<newline> pair disappear. */
1317 line_buffer[indx++] = c; /* Preserve the backslash. */
1321 line_buffer[indx++] = c;
1325 line_buffer[indx] = '\0';
1326 return (line_buffer);
1331 /* Return a line as in read_a_line (), but insure that the prompt is
1332 the secondary prompt. This is used to read the lines of a here
1333 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
1334 newlines quoted with backslashes while reading the line. It is
1335 non-zero unless the delimiter of the here document was quoted. */
1337 read_secondary_line (remove_quoted_newline)
1338 int remove_quoted_newline;
1340 prompt_string_pointer = &ps2_prompt;
1342 return (read_a_line (remove_quoted_newline));
1346 /* **************************************************************** */
1350 /* **************************************************************** */
1352 /* Reserved words. These are only recognized as the first word of a
1354 STRING_INT_ALIST word_token_alist[] = {
1363 #if defined (SELECT_COMMAND)
1364 { "select", SELECT },
1371 { "function", FUNCTION },
1378 /* Return the next shell input character. This always reads characters
1379 from shell_input_line; when that line is exhausted, it is time to
1380 read the next line. This is called by read_token when the shell is
1381 processing normal command input. */
1383 shell_getc (remove_quoted_newline)
1384 int remove_quoted_newline;
1391 /* If shell_input_line[shell_input_line_index] == 0, but there is
1392 something on the pushed list of strings, then we don't want to go
1393 off and get another line. We let the code down below handle it. */
1395 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
1396 (pushed_string_list == (STRING_SAVER *)NULL)))
1398 if (!shell_input_line || !shell_input_line[shell_input_line_index])
1403 restart_read_next_line:
1409 /* Allow immediate exit if interrupted during input. */
1413 shell_input_line_terminator = 0;
1415 #if defined (JOB_CONTROL)
1416 /* This can cause a problem when reading a command as the result
1417 of a trap, when the trap is called from flush_child. This call
1418 had better not cause jobs to disappear from the job table in
1419 that case, or we will have big trouble. */
1420 notify_and_cleanup ();
1421 #else /* !JOB_CONTROL */
1422 cleanup_dead_jobs ();
1423 #endif /* !JOB_CONTROL */
1425 #if defined (READLINE)
1426 if (interactive && bash_input.type != st_string && no_line_editing)
1428 if (interactive && bash_input.type != st_string)
1432 if (bash_input.type == st_stream)
1435 while (c = yy_getc ())
1437 /* Allow immediate exit if interrupted during input. */
1440 if (i + 2 > shell_input_line_size)
1442 xrealloc (shell_input_line, shell_input_line_size += 256);
1446 if (bash_input.type == st_stream)
1450 shell_input_line_terminator = EOF;
1452 shell_input_line[i] = '\0';
1456 shell_input_line[i++] = c;
1460 shell_input_line[--i] = '\0';
1461 current_command_line_count++;
1465 shell_input_line_index = 0;
1466 shell_input_line_len = i; /* == strlen (shell_input_line) */
1468 #if defined (HISTORY)
1469 if (interactive && shell_input_line && shell_input_line[0])
1473 expansions = pre_process_line (shell_input_line, 1, 1);
1475 free (shell_input_line);
1476 shell_input_line = expansions;
1477 shell_input_line_len = shell_input_line ?
1478 strlen (shell_input_line) :
1480 if (!shell_input_line_len)
1481 current_command_line_count--;
1483 /* We have to force the xrealloc below because we don't know the
1484 true allocated size of shell_input_line anymore. */
1485 shell_input_line_size = shell_input_line_len;
1487 #endif /* HISTORY */
1489 if (shell_input_line)
1491 /* Lines that signify the end of the shell's input should not be
1493 if (echo_input_at_read && (shell_input_line[0] ||
1494 shell_input_line_terminator != EOF))
1495 fprintf (stderr, "%s\n", shell_input_line);
1499 shell_input_line_size = 0;
1500 prompt_string_pointer = ¤t_prompt_string;
1505 /* Add the newline to the end of this string, iff the string does
1506 not already end in an EOF character. */
1507 if (shell_input_line_terminator != EOF)
1509 l = shell_input_line_len; /* was a call to strlen */
1511 if (l + 3 > shell_input_line_size)
1512 shell_input_line = xrealloc (shell_input_line,
1513 1 + (shell_input_line_size += 2));
1515 shell_input_line[l] = '\n';
1516 shell_input_line[l + 1] = '\0';
1520 c = shell_input_line[shell_input_line_index];
1523 shell_input_line_index++;
1525 if (c == '\\' && remove_quoted_newline &&
1526 shell_input_line[shell_input_line_index] == '\n')
1529 goto restart_read_next_line;
1533 /* If C is NULL, we have reached the end of the current input string. If
1534 pushed_string_list is non-empty, it's time to pop to the previous string
1535 because we have fully consumed the result of the last alias expansion.
1536 Do it transparently; just return the next character of the string popped
1538 if (!c && (pushed_string_list != (STRING_SAVER *)NULL))
1541 c = shell_input_line[shell_input_line_index];
1543 shell_input_line_index++;
1547 if (!c && shell_input_line_terminator == EOF)
1549 if (shell_input_line_index != 0)
1555 return ((unsigned char)c);
1558 /* Put C back into the input for the shell. */
1563 if (shell_input_line && shell_input_line_index)
1564 shell_input_line[--shell_input_line_index] = c;
1567 /* Discard input until CHARACTER is seen. */
1569 discard_until (character)
1574 while ((c = shell_getc (0)) != EOF && c != character)
1581 /* Place to remember the token. We try to keep the buffer
1582 at a reasonable size, but it can grow. */
1583 static char *token = (char *)NULL;
1585 /* Current size of the token buffer. */
1586 static int token_buffer_size = 0;
1589 execute_prompt_command (command)
1592 Function *temp_last, *temp_this;
1594 int temp_exit_value, temp_eof_encountered;
1596 temp_last = last_shell_builtin;
1597 temp_this = this_shell_builtin;
1598 temp_exit_value = last_command_exit_value;
1599 temp_eof_encountered = eof_encountered;
1600 last_lastarg = get_string_value ("_");
1602 last_lastarg = savestring (last_lastarg);
1604 parse_and_execute (savestring (command), "PROMPT_COMMAND", 0);
1606 last_shell_builtin = temp_last;
1607 this_shell_builtin = temp_this;
1608 last_command_exit_value = temp_exit_value;
1609 eof_encountered = temp_eof_encountered;
1611 bind_variable ("_", last_lastarg);
1612 FREE (last_lastarg);
1614 if (token_to_read == '\n')
1618 /* Command to read_token () explaining what we want it to do. */
1621 #define prompt_is_ps1 \
1622 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
1624 /* Function for yyparse to call. yylex keeps track of
1625 the last two tokens read, and calls read_token. */
1629 if (interactive && (!current_token || current_token == '\n'))
1631 /* Before we print a prompt, we might have to check mailboxes.
1632 We do this only if it is time to do so. Notice that only here
1633 is the mail alarm reset; nothing takes place in check_mail ()
1634 except the checking of mail. Please don't change this. */
1635 if (prompt_is_ps1 && time_to_check_mail ())
1638 reset_mail_timer ();
1641 /* Avoid printing a prompt if we're not going to read anything, e.g.
1642 after resetting the parser with read_token (RESET). */
1643 if (token_to_read == 0 && interactive)
1647 token_before_that = last_read_token;
1648 last_read_token = current_token;
1649 current_token = read_token (READ);
1650 return (current_token);
1653 /* Called from shell.c when Control-C is typed at top level. Or
1654 by the error rule at top level. */
1660 /* When non-zero, we have read the required tokens
1661 which allow ESAC to be the next one read. */
1662 static int allow_esac_as_next = 0;
1664 /* When non-zero, accept single '{' as a token itself. */
1665 static int allow_open_brace = 0;
1667 /* DELIMITERS is a stack of the nested delimiters that we have
1668 encountered so far. */
1669 static char *delimiters = (char *)NULL;
1671 /* Offset into the stack of delimiters. */
1672 int delimiter_depth = 0;
1674 /* How many slots are allocated to DELIMITERS. */
1675 static int delimiter_space = 0;
1678 gather_here_documents ()
1681 while (need_here_doc)
1683 make_here_document (redir_stack[r++]);
1688 /* Macro for accessing the top delimiter on the stack. Returns the
1689 delimiter or zero if none. */
1690 #define current_delimiter() \
1691 (delimiter_depth ? delimiters[delimiter_depth - 1] : 0)
1693 #define push_delimiter(character) \
1696 if (delimiter_depth + 2 > delimiter_space) \
1697 delimiters = xrealloc \
1698 (delimiters, (delimiter_space += 10) * sizeof (char)); \
1699 delimiters[delimiter_depth] = character; \
1700 delimiter_depth++; \
1704 /* When non-zero, an open-brace used to create a group is awaiting a close
1706 static int open_brace_awaiting_satisfaction = 0;
1708 #define command_token_position(token) \
1709 (((token) == ASSIGNMENT_WORD) || \
1710 ((token) != SEMI_SEMI && reserved_word_acceptable(token)))
1712 #define assignment_acceptable(token) command_token_position(token) && \
1713 (in_case_pattern_list == 0)
1715 /* Check to see if TOKEN is a reserved word and return the token
1717 #define CHECK_FOR_RESERVED_WORD(tok) \
1719 if (!dollar_present && !quoted && \
1720 reserved_word_acceptable (last_read_token)) \
1723 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
1724 if (STREQ (tok, word_token_alist[i].word)) \
1726 if (in_case_pattern_list && (word_token_alist[i].token != ESAC)) \
1729 if (word_token_alist[i].token == ESAC) \
1730 in_case_pattern_list = 0; \
1732 if (word_token_alist[i].token == '{') \
1733 open_brace_awaiting_satisfaction++; \
1735 if (word_token_alist[i].token == '}' && open_brace_awaiting_satisfaction) \
1736 open_brace_awaiting_satisfaction--; \
1738 return (word_token_alist[i].token); \
1743 /* Read the next token. Command can be READ (normal operation) or
1744 RESET (to normalize state). */
1746 read_token (command)
1749 int character; /* Current character. */
1750 int peek_char; /* Temporary look-ahead character. */
1751 int result; /* The thing to return. */
1752 WORD_DESC *the_word; /* The value for YYLVAL when a WORD is read. */
1754 if (token_buffer_size < TOKEN_DEFAULT_GROW_SIZE)
1757 token = xmalloc (token_buffer_size = TOKEN_DEFAULT_GROW_SIZE);
1760 if (command == RESET)
1762 delimiter_depth = 0; /* No delimiters found so far. */
1763 open_brace_awaiting_satisfaction = 0;
1764 in_case_pattern_list = 0;
1767 if (pushed_string_list)
1769 free_string_list ();
1770 pushed_string_list = (STRING_SAVER *)NULL;
1773 if (expanded_token_stack)
1775 free_expansion_stack ();
1776 expanded_token_stack = (EXPANSION_SAVER *)NULL;
1779 expand_next_token = 0;
1782 if (shell_input_line)
1784 free (shell_input_line);
1785 shell_input_line = (char *)NULL;
1786 shell_input_line_size = shell_input_line_index = 0;
1788 last_read_token = '\n';
1789 token_to_read = '\n';
1795 int rt = token_to_read;
1801 /* If we hit read_token () and there are no saved strings on the
1802 pushed_string_list, then we are no longer currently expanding a
1803 token. This can't be done in pop_stream, because pop_stream
1804 may pop the stream before the current token has finished being
1805 completely expanded (consider what happens when we alias foo to foo,
1806 and then try to expand it). */
1807 if (!pushed_string_list && expanded_token_stack)
1809 free_expansion_stack ();
1810 expanded_token_stack = (EXPANSION_SAVER *)NULL;
1813 /* This is a place to jump back to once we have successfully expanded a
1814 token with an alias and pushed the string with push_string () */
1819 /* Read a single word from input. Start by skipping blanks. */
1820 while ((character = shell_getc (1)) != EOF && whitespace (character));
1822 if (character == EOF)
1828 if (character == '#' && (!interactive || interactive_comments))
1830 /* A comment. Discard until EOL or EOF, and then return a newline. */
1831 discard_until ('\n');
1834 /* If we're about to return an unquoted newline, we can go and collect
1835 the text of any pending here documents. */
1837 gather_here_documents ();
1840 expand_next_token = 0;
1846 if (character == '\n')
1848 /* If we're about to return an unquoted newline, we can go and collect
1849 the text of any pending here document. */
1851 gather_here_documents ();
1854 expand_next_token = 0;
1860 if (member (character, "()<>;&|"))
1863 /* Turn off alias tokenization iff this character sequence would
1864 not leave us ready to read a command. */
1865 if (character == '<' || character == '>')
1866 expand_next_token = 0;
1869 /* Please note that the shell does not allow whitespace to
1870 appear in between tokens which are character pairs, such as
1871 "<<" or ">>". I believe this is the correct behaviour. */
1872 if (character == (peek_char = shell_getc (1)))
1876 /* If '<' then we could be at "<<" or at "<<-". We have to
1877 look ahead one more character. */
1879 peek_char = shell_getc (1);
1880 if (peek_char == '-')
1881 return (LESS_LESS_MINUS);
1884 shell_ungetc (peek_char);
1889 return (GREATER_GREATER);
1892 in_case_pattern_list = 1;
1894 expand_next_token = 0;
1907 if (peek_char == '&')
1911 case '<': return (LESS_AND);
1912 case '>': return (GREATER_AND);
1915 if (character == '<' && peek_char == '>')
1916 return (LESS_GREATER);
1917 if (character == '>' && peek_char == '|')
1918 return (GREATER_BAR);
1919 if (peek_char == '>' && character == '&')
1920 return (AND_GREATER);
1922 shell_ungetc (peek_char);
1924 /* If we look like we are reading the start of a function
1925 definition, then let the reader know about it so that
1926 we will do the right thing with `{'. */
1927 if (character == ')' &&
1928 last_read_token == '(' && token_before_that == WORD)
1930 allow_open_brace = 1;
1932 expand_next_token = 0;
1936 if (in_case_pattern_list && (character == ')'))
1937 in_case_pattern_list = 0;
1939 #if defined (PROCESS_SUBSTITUTION)
1940 /* Check for the constructs which introduce process substitution.
1941 Shells running in `posix mode' don't do process substitution. */
1942 if (posixly_correct ||
1943 (((character == '>' || character == '<') && peek_char == '(') == 0))
1944 #endif /* PROCESS_SUBSTITUTION */
1948 /* Hack <&- (close stdin) case. */
1949 if (character == '-')
1951 switch (last_read_token)
1959 /* Okay, if we got this far, we have to read a word. Read one,
1960 and then check it against the known ones. */
1962 /* Index into the token that we are building. */
1963 int token_index = 0;
1965 /* ALL_DIGITS becomes zero when we see a non-digit. */
1966 int all_digits = digit (character);
1968 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
1969 int dollar_present = 0;
1971 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
1974 /* Non-zero means to ignore the value of the next character, and just
1975 to add it no matter what. */
1976 int pass_next_character = 0;
1978 /* Non-zero means parsing a dollar-paren construct. It is the count of
1979 un-quoted closes we need to see. */
1980 int dollar_paren_level = 0;
1982 /* Non-zero means parsing a dollar-bracket construct ($[...]). It is
1983 the count of un-quoted `]' characters we need to see. */
1984 int dollar_bracket_level = 0;
1986 /* Non-zero means parsing a `${' construct. It is the count of
1987 un-quoted `}' we need to see. */
1988 int dollar_brace_level = 0;
1990 /* A level variable for parsing '${ ... }' constructs inside of double
1992 int delimited_brace_level = 0;
1994 /* A boolean variable denoting whether or not we are currently parsing
1995 a double-quoted string embedded in a $( ) or ${ } construct. */
1996 int embedded_quoted_string = 0;
1998 /* Another level variable. This one is for dollar_parens inside of
2000 int delimited_paren_level = 0;
2002 /* The current delimiting character. */
2007 if (character == EOF)
2010 if (pass_next_character)
2012 pass_next_character = 0;
2016 cd = current_delimiter ();
2018 if (cd && character == '\\' && cd != '\'')
2020 peek_char = shell_getc (0);
2021 if (peek_char != '\\')
2022 shell_ungetc (peek_char);
2025 token[token_index++] = character;
2030 /* Handle backslashes. Quote lots of things when not inside of
2031 double-quotes, quote some things inside of double-quotes. */
2033 if (character == '\\' && (!delimiter_depth || cd != '\''))
2035 peek_char = shell_getc (0);
2037 /* Backslash-newline is ignored in all cases excepting
2038 when quoted with single quotes. */
2039 if (peek_char == '\n')
2042 goto next_character;
2046 shell_ungetc (peek_char);
2048 /* If the next character is to be quoted, do it now. */
2049 if (!cd || cd == '`' ||
2050 (cd == '"' && member (peek_char, slashify_in_quotes)))
2052 pass_next_character++;
2059 /* This is a hack, in its present form. If a backquote substitution
2060 appears within double quotes, everything within the backquotes
2061 should be read as part of a single word. Jesus. Now I see why
2062 Korn introduced the $() form. */
2063 if (delimiter_depth && (cd == '"') && (character == '`'))
2065 push_delimiter (character);
2069 cd = current_delimiter (); /* XXX - may not need */
2070 if (delimiter_depth)
2072 if (character == cd)
2074 /* If we see a double quote while parsing a double-quoted
2075 $( ) or ${ }, and we have not seen ) or }, respectively,
2076 note that we are in the middle of reading an embedded
2078 if ((delimited_paren_level || delimited_brace_level) &&
2081 embedded_quoted_string = !embedded_quoted_string;
2092 #if defined (PROCESS_SUBSTITUTION)
2093 if (character == '$' || character == '<' || character == '>')
2095 if (character == '$')
2096 #endif /* !PROCESS_SUBSTITUTION */
2098 /* If we're in the middle of parsing a $( ) or ${ }
2099 construct with an embedded quoted string, don't
2100 bother looking at this character any further. */
2101 if (embedded_quoted_string)
2104 peek_char = shell_getc (1);
2105 shell_ungetc (peek_char);
2106 if (peek_char == '(')
2108 if (!delimiter_depth)
2109 dollar_paren_level++;
2111 delimited_paren_level++;
2113 pass_next_character++;
2116 else if (peek_char == '[' && character == '$')
2118 if (!delimiter_depth)
2119 dollar_bracket_level++;
2121 pass_next_character++;
2124 /* This handles ${...} constructs. */
2125 else if (peek_char == '{' && character == '$')
2127 if (!delimiter_depth)
2128 dollar_brace_level++;
2130 delimited_brace_level++;
2132 pass_next_character++;
2137 /* If we are parsing a $() or $[] construct, we need to balance
2138 parens and brackets inside the construct. This whole function
2139 could use a rewrite. */
2140 if (character == '(' && !embedded_quoted_string)
2142 if (delimiter_depth && delimited_paren_level)
2143 delimited_paren_level++;
2145 if (!delimiter_depth && dollar_paren_level)
2146 dollar_paren_level++;
2149 if (character == '[')
2151 if (!delimiter_depth && dollar_bracket_level)
2152 dollar_bracket_level++;
2155 if (character == '{' && !embedded_quoted_string)
2157 if (delimiter_depth && delimited_brace_level)
2158 delimited_brace_level++;
2160 if (!delimiter_depth && dollar_brace_level)
2161 dollar_brace_level++;
2164 /* This code needs to take into account whether we are inside a
2165 case statement pattern list, and whether this paren is supposed
2166 to terminate it (hey, it could happen). It's not as simple
2167 as just using in_case_pattern_list, because we're not parsing
2168 anything while we're reading a $( ) construct. Maybe we
2169 should move that whole mess into the yacc parser. */
2170 if (character == ')' && !embedded_quoted_string)
2172 if (delimiter_depth && delimited_paren_level)
2173 delimited_paren_level--;
2175 if (!delimiter_depth && dollar_paren_level)
2177 dollar_paren_level--;
2182 if (character == ']')
2184 if (!delimiter_depth && dollar_bracket_level)
2186 dollar_bracket_level--;
2191 if (character == '}' && !embedded_quoted_string)
2193 if (delimiter_depth && delimited_brace_level)
2194 delimited_brace_level--;
2196 if (!delimiter_depth && dollar_brace_level)
2198 dollar_brace_level--;
2204 if (!dollar_paren_level && !dollar_bracket_level &&
2205 !dollar_brace_level && !delimiter_depth &&
2206 member (character, " \t\n;&()|<>"))
2208 shell_ungetc (character);
2212 if (!delimiter_depth)
2214 if (character == '"' || character == '`' || character == '\'')
2216 push_delimiter (character);
2224 all_digits = digit (character);
2225 if (character == '$')
2230 if (character == CTLESC || character == CTLNUL)
2231 token[token_index++] = CTLESC;
2233 token[token_index++] = character;
2235 if (token_index == (token_buffer_size - 1))
2237 token_buffer_size += TOKEN_DEFAULT_GROW_SIZE;
2238 token = xrealloc (token, token_buffer_size);
2241 if (character == '\n' && interactive && bash_input.type != st_string)
2244 /* We want to remove quoted newlines (that is, a \<newline> pair)
2245 unless we are within single quotes or pass_next_character is
2246 set (the shell equivalent of literal-next). */
2247 character = shell_getc
2248 ((current_delimiter () != '\'') && (!pass_next_character));
2253 token[token_index] = '\0';
2255 if ((delimiter_depth || dollar_paren_level || dollar_bracket_level) &&
2258 char reporter = '\0';
2260 if (!delimiter_depth)
2262 if (dollar_paren_level)
2264 else if (dollar_bracket_level)
2269 reporter = current_delimiter ();
2271 report_error ("unexpected EOF while looking for `%c'", reporter);
2277 /* Check to see what thing we should return. If the last_read_token
2278 is a `<', or a `&', or the character which ended this token is
2279 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
2280 Otherwise, it is just a word, and should be returned as such. */
2282 if (character == '<' || character == '>' ||
2283 last_read_token == LESS_AND || last_read_token == GREATER_AND)
2285 yylval.number = atoi (token);
2290 /* Handle special case. IN is recognized if the last token
2291 was WORD and the token before that was FOR or CASE. */
2292 if ((last_read_token == WORD) &&
2293 #if defined (SELECT_COMMAND)
2294 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
2296 ((token_before_that == FOR) || (token_before_that == CASE)) &&
2298 (token[0] == 'i' && token[1] == 'n' && !token[2]))
2300 if (token_before_that == CASE)
2302 in_case_pattern_list = 1;
2303 allow_esac_as_next++;
2308 /* Ditto for DO in the FOR case. */
2309 #if defined (SELECT_COMMAND)
2310 if ((last_read_token == WORD) && ((token_before_that == FOR) || (token_before_that == SELECT)) &&
2312 if ((last_read_token == WORD) && (token_before_that == FOR) &&
2314 (token[0] == 'd' && token[1] == 'o' && !token[2]))
2317 /* Ditto for ESAC in the CASE case.
2318 Specifically, this handles "case word in esac", which is a legal
2319 construct, certainly because someone will pass an empty arg to the
2320 case construct, and we don't want it to barf. Of course, we should
2321 insist that the case construct has at least one pattern in it, but
2322 the designers disagree. */
2323 if (allow_esac_as_next)
2325 allow_esac_as_next--;
2326 if (STREQ (token, "esac"))
2328 in_case_pattern_list = 0;
2333 /* Ditto for `{' in the FUNCTION case. */
2334 if (allow_open_brace)
2336 allow_open_brace = 0;
2337 if (token[0] == '{' && !token[1])
2339 open_brace_awaiting_satisfaction++;
2344 if (posixly_correct)
2345 CHECK_FOR_RESERVED_WORD (token);
2348 /* OK, we have a token. Let's try to alias expand it, if (and only if)
2351 It is eligible for expansion if the shell is in interactive mode, and
2352 the token is unquoted and the last token read was a command
2353 separator (or expand_next_token is set), and we are currently
2354 processing an alias (pushed_string_list is non-empty) and this
2355 token is not the same as the current or any previously
2358 Special cases that disqualify:
2359 In a pattern list in a case statement (in_case_pattern_list). */
2360 if (interactive_shell && !quoted && !in_case_pattern_list &&
2361 (expand_next_token || command_token_position (last_read_token)))
2363 char *alias_expand_word (), *expanded;
2365 if (expanded_token_stack && token_has_been_expanded (token))
2368 expanded = alias_expand_word (token);
2371 int len = strlen (expanded), expand_next;
2373 /* Erase the current token. */
2376 expand_next = (expanded[len - 1] == ' ') ||
2377 (expanded[len - 1] == '\t');
2379 push_string (expanded, expand_next, token);
2383 /* This is an eligible token that does not have an expansion. */
2385 expand_next_token = 0;
2389 expand_next_token = 0;
2393 if (!posixly_correct)
2394 CHECK_FOR_RESERVED_WORD (token);
2396 /* What if we are attempting to satisfy an open-brace grouper? */
2397 if (open_brace_awaiting_satisfaction && token[0] == '}' && !token[1])
2399 open_brace_awaiting_satisfaction--;
2403 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
2404 the_word->word = xmalloc (1 + token_index);
2405 strcpy (the_word->word, token);
2406 the_word->dollar_present = dollar_present;
2407 the_word->quoted = quoted;
2408 the_word->assignment = assignment (token);
2410 yylval.word = the_word;
2413 /* A word is an assignment if it appears at the beginning of a
2414 simple command, or after another assignment word. This is
2415 context-dependent, so it cannot be handled in the grammar. */
2416 if (assignment_acceptable (last_read_token) && the_word->assignment)
2417 result = ASSIGNMENT_WORD;
2419 if (last_read_token == FUNCTION)
2420 allow_open_brace = 1;
2425 /* Return 1 if TOKEN is a token that after being read would allow
2426 a reserved word to be seen, else 0. */
2428 reserved_word_acceptable (token)
2432 if (member (token, "\n;()|&{") ||
2434 if (token == '\n' || token == ';' || token == '(' || token == ')' ||
2435 token == '|' || token == '&' || token == '{' ||
2437 token == '}' || /* XXX */
2446 token == SEMI_SEMI ||
2450 token == DONE || /* XXX these two are experimental */
2458 /* Return the index of TOKEN in the alist of reserved words, or -1 if
2459 TOKEN is not a shell reserved word. */
2461 find_reserved_word (token)
2465 for (i = 0; word_token_alist[i].word != (char *)NULL; i++)
2466 if (STREQ (token, word_token_alist[i].word))
2471 #if defined (READLINE)
2472 /* Called after each time readline is called. This insures that whatever
2473 the new prompt string is gets propagated to readline's local prompt
2476 reset_readline_prompt ()
2478 if (prompt_string_pointer)
2482 temp_prompt = *prompt_string_pointer
2483 ? decode_prompt_string (*prompt_string_pointer)
2486 if (temp_prompt == 0)
2488 temp_prompt = xmalloc (1);
2489 temp_prompt[0] = '\0';
2492 FREE (current_readline_prompt);
2494 current_readline_prompt = temp_prompt;
2497 #endif /* READLINE */
2499 #if defined (HISTORY)
2500 /* A list of tokens which can be followed by newlines, but not by
2501 semi-colons. When concatenating multiple lines of history, the
2502 newline separator for such tokens is replaced with a space. */
2503 static int no_semi_successors[] = {
2504 '\n', '{', '(', ')', ';', '&', '|',
2505 CASE, DO, ELSE, IF, IN, SEMI_SEMI, THEN, UNTIL, WHILE, AND_AND, OR_OR,
2509 /* If we are not within a delimited expression, try to be smart
2510 about which separators can be semi-colons and which must be
2513 history_delimiting_chars ()
2515 if (!delimiter_depth)
2519 for (i = 0; no_semi_successors[i]; i++)
2521 if (token_before_that == no_semi_successors[i])
2529 #endif /* HISTORY */
2531 /* Issue a prompt, or prepare to issue a prompt when the next character
2538 if (!interactive) /* XXX */
2541 ps1_prompt = get_string_value ("PS1");
2542 ps2_prompt = get_string_value ("PS2");
2544 if (!prompt_string_pointer)
2545 prompt_string_pointer = &ps1_prompt;
2547 temp_prompt = (*prompt_string_pointer)
2548 ? decode_prompt_string (*prompt_string_pointer)
2551 if (temp_prompt == 0)
2553 temp_prompt = xmalloc (1);
2554 temp_prompt[0] = '\0';
2557 current_prompt_string = *prompt_string_pointer;
2558 prompt_string_pointer = &ps2_prompt;
2560 #if defined (READLINE)
2561 if (!no_line_editing)
2563 FREE (current_readline_prompt);
2564 current_readline_prompt = temp_prompt;
2567 #endif /* READLINE */
2569 FREE (current_decoded_prompt);
2570 current_decoded_prompt = temp_prompt;
2577 fprintf (stderr, "%s", current_decoded_prompt);
2581 /* Return a string which will be printed as a prompt. The string
2582 may contain special characters which are decoded as follows:
2587 \s the name of the shell
2588 \w the current working directory
2589 \W the last element of PWD
2592 \# the command number of this command
2593 \! the history number of this command
2594 \$ a $ or a # if you are root
2595 \<octal> character code in octal
2598 #define PROMPT_GROWTH 50
2600 decode_prompt_string (string)
2603 int result_size = PROMPT_GROWTH;
2604 int result_index = 0;
2607 char *temp = (char *)NULL;
2610 #if defined (PROMPT_STRING_DECODE)
2612 result = xmalloc (PROMPT_GROWTH);
2615 while (c = *string++)
2617 if (posixly_correct && c == '!')
2621 temp = savestring ("!");
2626 #if !defined (HISTORY)
2627 temp = savestring ("1");
2629 temp = itos (history_number ());
2630 #endif /* HISTORY */
2631 string--; /* add_string increments string again. */
2650 char octal_string[4];
2653 strncpy (octal_string, string, 3);
2654 octal_string[3] = '\0';
2656 n = read_octal (octal_string);
2659 if (n == CTLESC || n == CTLNUL)
2684 /* Make the current time/date into a string. */
2686 time_t the_time = time (0);
2687 char *ttemp = ctime (&the_time);
2688 temp = savestring (ttemp);
2692 strcpy (temp, temp + 11);
2702 if (!no_line_editing)
2703 temp = savestring ("\r\n");
2705 temp = savestring ("\n");
2710 temp = base_pathname (shell_name);
2711 temp = savestring (temp);
2718 /* Use the value of PWD because it is much more effecient. */
2721 char *polite_directory_format (), t_string[MAXPATHLEN];
2723 temp = get_string_value ("PWD");
2728 strcpy (t_string, temp);
2731 #endif /* EFFICIENT */
2735 char *dir = (char *)strrchr (t_string, '/');
2736 if (dir && dir != t_string)
2737 strcpy (t_string, dir + 1);
2738 temp = savestring (t_string);
2741 temp = savestring (polite_directory_format (t_string));
2747 temp = savestring (current_user.user_name);
2755 temp = savestring (current_host_name);
2756 if (t_string = (char *)strchr (temp, '.'))
2763 temp = itos (current_command_number);
2769 #if !defined (HISTORY)
2770 temp = savestring ("1");
2772 temp = itos (history_number ());
2773 #endif /* HISTORY */
2778 temp = savestring (geteuid () == 0 ? "#" : "$");
2781 #if defined (READLINE)
2786 temp[1] = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
2792 temp = savestring ("\\");
2796 temp = savestring ("\\ ");
2803 sub_append_string (temp, result, &result_index, &result_size);
2804 temp = (char *)NULL; /* Free ()'ed in sub_append_string (). */
2805 result[result_index] = '\0';
2811 while (3 + result_index > result_size)
2812 result = xrealloc (result, result_size += PROMPT_GROWTH);
2814 result[result_index++] = c;
2815 result[result_index] = '\0';
2818 #else /* !PROMPT_STRING_DECODE */
2819 result = savestring (string);
2820 #endif /* !PROMPT_STRING_DECODE */
2822 /* Perform variable and parameter expansion and command substitution on
2823 the prompt string. */
2824 list = expand_string_unsplit (result, 1);
2826 result = string_list (list);
2827 dispose_words (list);
2832 /* Report a syntax error, and restart the parser. Call here for fatal
2836 report_syntax_error ((char *)NULL);
2840 /* Report a syntax error with line numbers, etc.
2841 Call here for recoverable errors. If you have a message to print,
2842 then place it in MESSAGE, otherwise pass NULL and this will figure
2843 out an appropriate message for you. */
2845 report_syntax_error (message)
2852 char *name = bash_input.name ? bash_input.name : "stdin";
2853 report_error ("%s: line %d: `%s'", name, line_number, message);
2859 report_error ("%s", message);
2862 last_command_exit_value = EX_USAGE;
2866 if (shell_input_line && *shell_input_line)
2868 char *t = shell_input_line;
2869 register int i = shell_input_line_index;
2875 while (i && (t[i] == ' ' || t[i] == '\t' || t[i] == '\n'))
2881 while (i && !member (t[i], " \n\t;|&"))
2884 while (i != token_end && member (t[i], " \t\n"))
2890 error_token = xmalloc (1 + (token_end - i));
2891 strncpy (error_token, t + i, token_end - i);
2892 error_token[token_end - i] = '\0';
2894 report_error ("syntax error near unexpected token `%s'", error_token);
2897 else if ((i == 0) && (token_end == 0)) /* a 1-character token */
2903 report_error ("syntax error near unexpected token `%s'", etoken);
2908 char *temp = savestring (shell_input_line);
2909 char *name = bash_input.name ? bash_input.name : "stdin";
2910 int l = strlen (temp);
2912 while (l && temp[l - 1] == '\n')
2915 report_error ("%s: line %d: `%s'", name, line_number, temp);
2923 name = bash_input.name ? bash_input.name : "stdin";
2925 msg = "syntax error: unexpected end of file";
2927 msg = "syntax error";
2929 report_error ("%s: line %d: %s", name, line_number, msg);
2932 /* This file uses EOF_Reached only for error reporting
2933 when the shell is interactive. Other mechanisms are
2934 used to decide whether or not to exit. */
2939 last_command_exit_value = EX_USAGE;
2942 /* ??? Needed function. ??? We have to be able to discard the constructs
2943 created during parsing. In the case of error, we want to return
2944 allocated objects to the memory pool. In the case of no error, we want
2945 to throw away the information about where the allocated objects live.
2946 (dispose_command () will actually free the command. */
2947 discard_parser_constructs (error_p)
2952 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
2954 /* A flag denoting whether or not ignoreeof is set. */
2957 /* The number of times that we have encountered an EOF character without
2958 another character intervening. When this gets above the limit, the
2959 shell terminates. */
2960 int eof_encountered = 0;
2962 /* The limit for eof_encountered. */
2963 int eof_encountered_limit = 10;
2965 /* If we have EOF as the only input unit, this user wants to leave
2966 the shell. If the shell is not interactive, then just leave.
2967 Otherwise, if ignoreeof is set, and we haven't done this the
2968 required number of times in a row, print a message. */
2970 handle_eof_input_unit ()
2974 /* shell.c may use this to decide whether or not to write out the
2975 history, among other things. We use it only for error reporting
2980 /* If the user wants to "ignore" eof, then let her do so, kind of. */
2983 if (eof_encountered < eof_encountered_limit)
2985 fprintf (stderr, "Use \"%s\" to leave the shell.\n",
2986 login_shell ? "logout" : "exit");
2988 /* Reset the prompt string to be $PS1. */
2989 prompt_string_pointer = (char **)NULL;
2991 last_read_token = current_token = '\n';
2996 /* In this case EOF should exit the shell. Do it now. */
2998 exit_builtin ((WORD_LIST *)NULL);
3002 /* We don't write history files, etc., for non-interactive shells. */