| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* Yacc grammar for bash. */ |
| 2 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3 | /* Copyright (C) 1989-2004 Free Software Foundation, Inc. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
| 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 |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 9 | Software Foundation; either version 2, or (at your option) any later |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 10 | version. |
| 11 | |
| 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 |
| 15 | for more details. |
| 16 | |
| 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 |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 19 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | %{ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 24 | #include "bashtypes.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 25 | #include "bashansi.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 26 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 27 | #include "filecntl.h" |
| 28 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 29 | #if defined (HAVE_UNISTD_H) |
| 30 | # include <unistd.h> |
| 31 | #endif |
| 32 | |
| 33 | #if defined (HAVE_LOCALE_H) |
| 34 | # include <locale.h> |
| 35 | #endif |
| 36 | |
| 37 | #include <stdio.h> |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 38 | #include "chartypes.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 39 | #include <signal.h> |
| 40 | |
| 41 | #include "memalloc.h" |
| 42 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 43 | #include "bashintl.h" |
| 44 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 45 | #define NEED_STRFTIME_DECL /* used in externs.h */ |
| 46 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 47 | #include "shell.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 48 | #include "trap.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 49 | #include "flags.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 50 | #include "parser.h" |
| 51 | #include "mailcheck.h" |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 52 | #include "test.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 53 | #include "builtins/common.h" |
| 54 | #include "builtins/builtext.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 55 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 56 | #include "shmbutil.h" |
| 57 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 58 | #if defined (READLINE) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 59 | # include "bashline.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 60 | # include <readline/readline.h> |
| 61 | #endif /* READLINE */ |
| 62 | |
| 63 | #if defined (HISTORY) |
| 64 | # include "bashhist.h" |
| 65 | # include <readline/history.h> |
| 66 | #endif /* HISTORY */ |
| 67 | |
| 68 | #if defined (JOB_CONTROL) |
| 69 | # include "jobs.h" |
| 70 | #endif /* JOB_CONTROL */ |
| 71 | |
| 72 | #if defined (ALIAS) |
| 73 | # include "alias.h" |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 74 | #else |
| 75 | typedef void *alias_t; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 76 | #endif /* ALIAS */ |
| 77 | |
| 78 | #if defined (PROMPT_STRING_DECODE) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 79 | # ifndef _MINIX |
| 80 | # include <sys/param.h> |
| 81 | # endif |
| 82 | # include <time.h> |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 83 | # if defined (TM_IN_SYS_TIME) |
| 84 | # include <sys/types.h> |
| 85 | # include <sys/time.h> |
| 86 | # endif /* TM_IN_SYS_TIME */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 87 | # include "maxpath.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 88 | #endif /* PROMPT_STRING_DECODE */ |
| 89 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 90 | #define RE_READ_TOKEN -99 |
| 91 | #define NO_EXPANSION -100 |
| 92 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 93 | #ifdef DEBUG |
| 94 | # define YYDEBUG 1 |
| 95 | #else |
| 96 | # define YYDEBUG 0 |
| 97 | #endif |
| 98 | |
| 99 | #if defined (HANDLE_MULTIBYTE) |
| 100 | # define last_shell_getc_is_singlebyte \ |
| 101 | ((shell_input_line_index > 1) \ |
| 102 | ? shell_input_line_property[shell_input_line_index - 1] \ |
| 103 | : 1) |
| 104 | # define MBTEST(x) ((x) && last_shell_getc_is_singlebyte) |
| 105 | #else |
| 106 | # define last_shell_getc_is_singlebyte 1 |
| 107 | # define MBTEST(x) ((x)) |
| 108 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 109 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 110 | #if defined (EXTENDED_GLOB) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 111 | extern int extended_glob; |
| 112 | #endif |
| 113 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 114 | extern int eof_encountered; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 115 | extern int no_line_editing, running_under_emacs; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 116 | extern int current_command_number; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 117 | extern int sourcelevel; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 118 | extern int posixly_correct; |
| 119 | extern int last_command_exit_value; |
| 120 | extern int interrupt_immediately; |
| 121 | extern char *shell_name, *current_host_name; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 122 | extern char *dist_version; |
| 123 | extern int patch_level; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 124 | extern int dump_translatable_strings, dump_po_strings; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 125 | extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 126 | #if defined (BUFFERED_INPUT) |
| 127 | extern int bash_input_fd_changed; |
| 128 | #endif |
| 129 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 130 | extern int errno; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 131 | /* **************************************************************** */ |
| 132 | /* */ |
| 133 | /* "Forward" declarations */ |
| 134 | /* */ |
| 135 | /* **************************************************************** */ |
| 136 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 137 | #ifdef DEBUG |
| 138 | static void debug_parser __P((int)); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 139 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 140 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 141 | static int yy_getc __P((void)); |
| 142 | static int yy_ungetc __P((int)); |
| 143 | |
| 144 | #if defined (READLINE) |
| 145 | static int yy_readline_get __P((void)); |
| 146 | static int yy_readline_unget __P((int)); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 147 | #endif |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 148 | |
| 149 | static int yy_string_get __P((void)); |
| 150 | static int yy_string_unget __P((int)); |
| 151 | static int yy_stream_get __P((void)); |
| 152 | static int yy_stream_unget __P((int)); |
| 153 | |
| 154 | static int shell_getc __P((int)); |
| 155 | static void shell_ungetc __P((int)); |
| 156 | static void discard_until __P((int)); |
| 157 | |
| 158 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| 159 | static void push_string __P((char *, int, alias_t *)); |
| 160 | static void pop_string __P((void)); |
| 161 | static void free_string_list __P((void)); |
| 162 | #endif |
| 163 | |
| 164 | static char *read_a_line __P((int)); |
| 165 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 166 | static int reserved_word_acceptable __P((int)); |
| 167 | static int yylex __P((void)); |
| 168 | static int alias_expand_token __P((char *)); |
| 169 | static int time_command_acceptable __P((void)); |
| 170 | static int special_case_tokens __P((char *)); |
| 171 | static int read_token __P((int)); |
| 172 | static char *parse_matched_pair __P((int, int, int, int *, int)); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 173 | #if defined (ARRAY_VARS) |
| 174 | static char *parse_compound_assignment __P((int *)); |
| 175 | #endif |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 176 | #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 177 | static int parse_dparen __P((int)); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 178 | static int parse_arith_cmd __P((char **, int)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 179 | #endif |
| 180 | #if defined (COND_COMMAND) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 181 | static void cond_error __P((void)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 182 | static COND_COM *cond_expr __P((void)); |
| 183 | static COND_COM *cond_or __P((void)); |
| 184 | static COND_COM *cond_and __P((void)); |
| 185 | static COND_COM *cond_term __P((void)); |
| 186 | static int cond_skip_newlines __P((void)); |
| 187 | static COMMAND *parse_cond_command __P((void)); |
| 188 | #endif |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 189 | #if defined (ARRAY_VARS) |
| 190 | static int token_is_assignment __P((char *, int)); |
| 191 | static int token_is_ident __P((char *, int)); |
| 192 | #endif |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 193 | static int read_token_word __P((int)); |
| 194 | static void discard_parser_constructs __P((int)); |
| 195 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 196 | static char *error_token_from_token __P((int)); |
| 197 | static char *error_token_from_text __P((void)); |
| 198 | static void print_offending_line __P((void)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 199 | static void report_syntax_error __P((char *)); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 200 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 201 | static void handle_eof_input_unit __P((void)); |
| 202 | static void prompt_again __P((void)); |
| 203 | #if 0 |
| 204 | static void reset_readline_prompt __P((void)); |
| 205 | #endif |
| 206 | static void print_prompt __P((void)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 207 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 208 | #if defined (HISTORY) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 209 | char *history_delimiting_chars __P((void)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 210 | #endif |
| 211 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 212 | #if defined (HANDLE_MULTIBYTE) |
| 213 | static void set_line_mbstate __P((void)); |
| 214 | static char *shell_input_line_property = NULL; |
| 215 | #else |
| 216 | # define set_line_mbstate() |
| 217 | #endif |
| 218 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 219 | extern int yyerror __P((const char *)); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 220 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 221 | #ifdef DEBUG |
| 222 | extern int yydebug; |
| 223 | #endif |
| 224 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 225 | /* Default prompt strings */ |
| 226 | char *primary_prompt = PPROMPT; |
| 227 | char *secondary_prompt = SPROMPT; |
| 228 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 229 | /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */ |
| 230 | char *ps1_prompt, *ps2_prompt; |
| 231 | |
| 232 | /* Handle on the current prompt string. Indirectly points through |
| 233 | ps1_ or ps2_prompt. */ |
| 234 | char **prompt_string_pointer = (char **)NULL; |
| 235 | char *current_prompt_string; |
| 236 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 237 | /* Non-zero means we expand aliases in commands. */ |
| 238 | int expand_aliases = 0; |
| 239 | |
| 240 | /* If non-zero, the decoded prompt string undergoes parameter and |
| 241 | variable substitution, command substitution, arithmetic substitution, |
| 242 | string expansion, process substitution, and quote removal in |
| 243 | decode_prompt_string. */ |
| 244 | int promptvars = 1; |
| 245 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 246 | /* If non-zero, $'...' and $"..." are expanded when they appear within |
| 247 | a ${...} expansion, even when the expansion appears within double |
| 248 | quotes. */ |
| 249 | int extended_quote = 1; |
| 250 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 251 | /* The decoded prompt string. Used if READLINE is not defined or if |
| 252 | editing is turned off. Analogous to current_readline_prompt. */ |
| 253 | static char *current_decoded_prompt; |
| 254 | |
| 255 | /* The number of lines read from input while creating the current command. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 256 | int current_command_line_count; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 257 | |
| 258 | /* Variables to manage the task of reading here documents, because we need to |
| 259 | defer the reading until after a complete command has been collected. */ |
| 260 | static REDIRECT *redir_stack[10]; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 261 | int need_here_doc; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 262 | |
| 263 | /* Where shell input comes from. History expansion is performed on each |
| 264 | line when the shell is interactive. */ |
| 265 | static char *shell_input_line = (char *)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 266 | static int shell_input_line_index; |
| 267 | static int shell_input_line_size; /* Amount allocated for shell_input_line. */ |
| 268 | static int shell_input_line_len; /* strlen (shell_input_line) */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 269 | |
| 270 | /* Either zero or EOF. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 271 | static int shell_input_line_terminator; |
| 272 | |
| 273 | /* The line number in a script on which a function definition starts. */ |
| 274 | static int function_dstart; |
| 275 | |
| 276 | /* The line number in a script on which a function body starts. */ |
| 277 | static int function_bstart; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 278 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 279 | /* The line number in a script at which an arithmetic for command starts. */ |
| 280 | static int arith_for_lineno; |
| 281 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 282 | /* The line number in a script where the word in a `case WORD', `select WORD' |
| 283 | or `for WORD' begins. This is a nested command maximum, since the array |
| 284 | index is decremented after a case, select, or for command is parsed. */ |
| 285 | #define MAX_CASE_NEST 128 |
| 286 | static int word_lineno[MAX_CASE_NEST]; |
| 287 | static int word_top = -1; |
| 288 | |
| 289 | /* If non-zero, it is the token that we want read_token to return |
| 290 | regardless of what text is (or isn't) present to be read. This |
| 291 | is reset by read_token. If token_to_read == WORD or |
| 292 | ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */ |
| 293 | static int token_to_read; |
| 294 | static WORD_DESC *word_desc_to_read; |
| 295 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 296 | static REDIRECTEE redir; |
| 297 | %} |
| 298 | |
| 299 | %union { |
| 300 | WORD_DESC *word; /* the word that we read. */ |
| 301 | int number; /* the number that we read. */ |
| 302 | WORD_LIST *word_list; |
| 303 | COMMAND *command; |
| 304 | REDIRECT *redirect; |
| 305 | ELEMENT element; |
| 306 | PATTERN_LIST *pattern; |
| 307 | } |
| 308 | |
| 309 | /* Reserved words. Members of the first group are only recognized |
| 310 | in the case that they are preceded by a list_terminator. Members |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 311 | of the second group are for [[...]] commands. Members of the |
| 312 | third group are recognized only under special circumstances. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 313 | %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 314 | %token COND_START COND_END COND_ERROR |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 315 | %token IN BANG TIME TIMEOPT |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 316 | |
| 317 | /* More general tokens. yylex () knows how to make these. */ |
| 318 | %token <word> WORD ASSIGNMENT_WORD |
| 319 | %token <number> NUMBER |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 320 | %token <word_list> ARITH_CMD ARITH_FOR_EXPRS |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 321 | %token <command> COND_CMD |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 322 | %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 323 | %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER |
| 324 | %token GREATER_BAR |
| 325 | |
| 326 | /* The types that the various syntactical units return. */ |
| 327 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 328 | %type <command> inputunit command pipeline pipeline_command |
| 329 | %type <command> list list0 list1 compound_list simple_list simple_list1 |
| 330 | %type <command> simple_command shell_command |
| 331 | %type <command> for_command select_command case_command group_command |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 332 | %type <command> arith_command |
| 333 | %type <command> cond_command |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 334 | %type <command> arith_for_command |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 335 | %type <command> function_def function_body if_command elif_clause subshell |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 336 | %type <redirect> redirection redirection_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 337 | %type <element> simple_command_element |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 338 | %type <word_list> word_list pattern |
| 339 | %type <pattern> pattern_list case_clause_sequence case_clause |
| 340 | %type <number> timespec |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 341 | %type <number> list_terminator |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 342 | |
| 343 | %start inputunit |
| 344 | |
| 345 | %left '&' ';' '\n' yacc_EOF |
| 346 | %left AND_AND OR_OR |
| 347 | %right '|' |
| 348 | %% |
| 349 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 350 | inputunit: simple_list simple_list_terminator |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 351 | { |
| 352 | /* Case of regular command. Discard the error |
| 353 | safety net,and return the command just parsed. */ |
| 354 | global_command = $1; |
| 355 | eof_encountered = 0; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 356 | /* discard_parser_constructs (0); */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 357 | YYACCEPT; |
| 358 | } |
| 359 | | '\n' |
| 360 | { |
| 361 | /* Case of regular command, but not a very |
| 362 | interesting one. Return a NULL command. */ |
| 363 | global_command = (COMMAND *)NULL; |
| 364 | YYACCEPT; |
| 365 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 366 | | error '\n' |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 367 | { |
| 368 | /* Error during parsing. Return NULL command. */ |
| 369 | global_command = (COMMAND *)NULL; |
| 370 | eof_encountered = 0; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 371 | /* discard_parser_constructs (1); */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 372 | if (interactive) |
| 373 | { |
| 374 | YYACCEPT; |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | YYABORT; |
| 379 | } |
| 380 | } |
| 381 | | yacc_EOF |
| 382 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 383 | /* Case of EOF seen by itself. Do ignoreeof or |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 384 | not. */ |
| 385 | global_command = (COMMAND *)NULL; |
| 386 | handle_eof_input_unit (); |
| 387 | YYACCEPT; |
| 388 | } |
| 389 | ; |
| 390 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 391 | word_list: WORD |
| 392 | { $$ = make_word_list ($1, (WORD_LIST *)NULL); } |
| 393 | | word_list WORD |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 394 | { $$ = make_word_list ($2, $1); } |
| 395 | ; |
| 396 | |
| 397 | redirection: '>' WORD |
| 398 | { |
| 399 | redir.filename = $2; |
| 400 | $$ = make_redirection (1, r_output_direction, redir); |
| 401 | } |
| 402 | | '<' WORD |
| 403 | { |
| 404 | redir.filename = $2; |
| 405 | $$ = make_redirection (0, r_input_direction, redir); |
| 406 | } |
| 407 | | NUMBER '>' WORD |
| 408 | { |
| 409 | redir.filename = $3; |
| 410 | $$ = make_redirection ($1, r_output_direction, redir); |
| 411 | } |
| 412 | | NUMBER '<' WORD |
| 413 | { |
| 414 | redir.filename = $3; |
| 415 | $$ = make_redirection ($1, r_input_direction, redir); |
| 416 | } |
| 417 | | GREATER_GREATER WORD |
| 418 | { |
| 419 | redir.filename = $2; |
| 420 | $$ = make_redirection (1, r_appending_to, redir); |
| 421 | } |
| 422 | | NUMBER GREATER_GREATER WORD |
| 423 | { |
| 424 | redir.filename = $3; |
| 425 | $$ = make_redirection ($1, r_appending_to, redir); |
| 426 | } |
| 427 | | LESS_LESS WORD |
| 428 | { |
| 429 | redir.filename = $2; |
| 430 | $$ = make_redirection (0, r_reading_until, redir); |
| 431 | redir_stack[need_here_doc++] = $$; |
| 432 | } |
| 433 | | NUMBER LESS_LESS WORD |
| 434 | { |
| 435 | redir.filename = $3; |
| 436 | $$ = make_redirection ($1, r_reading_until, redir); |
| 437 | redir_stack[need_here_doc++] = $$; |
| 438 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 439 | | LESS_LESS_LESS WORD |
| 440 | { |
| 441 | redir.filename = $2; |
| 442 | $$ = make_redirection (0, r_reading_string, redir); |
| 443 | } |
| 444 | | NUMBER LESS_LESS_LESS WORD |
| 445 | { |
| 446 | redir.filename = $3; |
| 447 | $$ = make_redirection ($1, r_reading_string, redir); |
| 448 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 449 | | LESS_AND NUMBER |
| 450 | { |
| 451 | redir.dest = $2; |
| 452 | $$ = make_redirection (0, r_duplicating_input, redir); |
| 453 | } |
| 454 | | NUMBER LESS_AND NUMBER |
| 455 | { |
| 456 | redir.dest = $3; |
| 457 | $$ = make_redirection ($1, r_duplicating_input, redir); |
| 458 | } |
| 459 | | GREATER_AND NUMBER |
| 460 | { |
| 461 | redir.dest = $2; |
| 462 | $$ = make_redirection (1, r_duplicating_output, redir); |
| 463 | } |
| 464 | | NUMBER GREATER_AND NUMBER |
| 465 | { |
| 466 | redir.dest = $3; |
| 467 | $$ = make_redirection ($1, r_duplicating_output, redir); |
| 468 | } |
| 469 | | LESS_AND WORD |
| 470 | { |
| 471 | redir.filename = $2; |
| 472 | $$ = make_redirection (0, r_duplicating_input_word, redir); |
| 473 | } |
| 474 | | NUMBER LESS_AND WORD |
| 475 | { |
| 476 | redir.filename = $3; |
| 477 | $$ = make_redirection ($1, r_duplicating_input_word, redir); |
| 478 | } |
| 479 | | GREATER_AND WORD |
| 480 | { |
| 481 | redir.filename = $2; |
| 482 | $$ = make_redirection (1, r_duplicating_output_word, redir); |
| 483 | } |
| 484 | | NUMBER GREATER_AND WORD |
| 485 | { |
| 486 | redir.filename = $3; |
| 487 | $$ = make_redirection ($1, r_duplicating_output_word, redir); |
| 488 | } |
| 489 | | LESS_LESS_MINUS WORD |
| 490 | { |
| 491 | redir.filename = $2; |
| 492 | $$ = make_redirection |
| 493 | (0, r_deblank_reading_until, redir); |
| 494 | redir_stack[need_here_doc++] = $$; |
| 495 | } |
| 496 | | NUMBER LESS_LESS_MINUS WORD |
| 497 | { |
| 498 | redir.filename = $3; |
| 499 | $$ = make_redirection |
| 500 | ($1, r_deblank_reading_until, redir); |
| 501 | redir_stack[need_here_doc++] = $$; |
| 502 | } |
| 503 | | GREATER_AND '-' |
| 504 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 505 | redir.dest = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 506 | $$ = make_redirection (1, r_close_this, redir); |
| 507 | } |
| 508 | | NUMBER GREATER_AND '-' |
| 509 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 510 | redir.dest = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 511 | $$ = make_redirection ($1, r_close_this, redir); |
| 512 | } |
| 513 | | LESS_AND '-' |
| 514 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 515 | redir.dest = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 516 | $$ = make_redirection (0, r_close_this, redir); |
| 517 | } |
| 518 | | NUMBER LESS_AND '-' |
| 519 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 520 | redir.dest = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 521 | $$ = make_redirection ($1, r_close_this, redir); |
| 522 | } |
| 523 | | AND_GREATER WORD |
| 524 | { |
| 525 | redir.filename = $2; |
| 526 | $$ = make_redirection (1, r_err_and_out, redir); |
| 527 | } |
| 528 | | NUMBER LESS_GREATER WORD |
| 529 | { |
| 530 | redir.filename = $3; |
| 531 | $$ = make_redirection ($1, r_input_output, redir); |
| 532 | } |
| 533 | | LESS_GREATER WORD |
| 534 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 535 | redir.filename = $2; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 536 | $$ = make_redirection (0, r_input_output, redir); |
| 537 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 538 | | GREATER_BAR WORD |
| 539 | { |
| 540 | redir.filename = $2; |
| 541 | $$ = make_redirection (1, r_output_force, redir); |
| 542 | } |
| 543 | | NUMBER GREATER_BAR WORD |
| 544 | { |
| 545 | redir.filename = $3; |
| 546 | $$ = make_redirection ($1, r_output_force, redir); |
| 547 | } |
| 548 | ; |
| 549 | |
| 550 | simple_command_element: WORD |
| 551 | { $$.word = $1; $$.redirect = 0; } |
| 552 | | ASSIGNMENT_WORD |
| 553 | { $$.word = $1; $$.redirect = 0; } |
| 554 | | redirection |
| 555 | { $$.redirect = $1; $$.word = 0; } |
| 556 | ; |
| 557 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 558 | redirection_list: redirection |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 559 | { |
| 560 | $$ = $1; |
| 561 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 562 | | redirection_list redirection |
| 563 | { |
| 564 | register REDIRECT *t; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 565 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 566 | for (t = $1; t->next; t = t->next) |
| 567 | ; |
| 568 | t->next = $2; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 569 | $$ = $1; |
| 570 | } |
| 571 | ; |
| 572 | |
| 573 | simple_command: simple_command_element |
| 574 | { $$ = make_simple_command ($1, (COMMAND *)NULL); } |
| 575 | | simple_command simple_command_element |
| 576 | { $$ = make_simple_command ($2, $1); } |
| 577 | ; |
| 578 | |
| 579 | command: simple_command |
| 580 | { $$ = clean_simple_command ($1); } |
| 581 | | shell_command |
| 582 | { $$ = $1; } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 583 | | shell_command redirection_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 584 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 585 | COMMAND *tc; |
| 586 | |
| 587 | tc = $1; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 588 | if (tc->redirects) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 589 | { |
| 590 | register REDIRECT *t; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 591 | for (t = tc->redirects; t->next; t = t->next) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 592 | ; |
| 593 | t->next = $2; |
| 594 | } |
| 595 | else |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 596 | tc->redirects = $2; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 597 | $$ = $1; |
| 598 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 599 | | function_def |
| 600 | { $$ = $1; } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 601 | ; |
| 602 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 603 | shell_command: for_command |
| 604 | { $$ = $1; } |
| 605 | | case_command |
| 606 | { $$ = $1; } |
| 607 | | WHILE compound_list DO compound_list DONE |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 608 | { $$ = make_while_command ($2, $4); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 609 | | UNTIL compound_list DO compound_list DONE |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 610 | { $$ = make_until_command ($2, $4); } |
| 611 | | select_command |
| 612 | { $$ = $1; } |
| 613 | | if_command |
| 614 | { $$ = $1; } |
| 615 | | subshell |
| 616 | { $$ = $1; } |
| 617 | | group_command |
| 618 | { $$ = $1; } |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 619 | | arith_command |
| 620 | { $$ = $1; } |
| 621 | | cond_command |
| 622 | { $$ = $1; } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 623 | | arith_for_command |
| 624 | { $$ = $1; } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 625 | ; |
| 626 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 627 | for_command: FOR WORD newline_list DO compound_list DONE |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 628 | { |
| 629 | $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]); |
| 630 | if (word_top > 0) word_top--; |
| 631 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 632 | | FOR WORD newline_list '{' compound_list '}' |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 633 | { |
| 634 | $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]); |
| 635 | if (word_top > 0) word_top--; |
| 636 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 637 | | FOR WORD ';' newline_list DO compound_list DONE |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 638 | { |
| 639 | $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]); |
| 640 | if (word_top > 0) word_top--; |
| 641 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 642 | | FOR WORD ';' newline_list '{' compound_list '}' |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 643 | { |
| 644 | $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]); |
| 645 | if (word_top > 0) word_top--; |
| 646 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 647 | | FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 648 | { |
| 649 | $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]); |
| 650 | if (word_top > 0) word_top--; |
| 651 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 652 | | FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}' |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 653 | { |
| 654 | $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]); |
| 655 | if (word_top > 0) word_top--; |
| 656 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 657 | | FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 658 | { |
| 659 | $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]); |
| 660 | if (word_top > 0) word_top--; |
| 661 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 662 | | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}' |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 663 | { |
| 664 | $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]); |
| 665 | if (word_top > 0) word_top--; |
| 666 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 667 | ; |
| 668 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 669 | arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 670 | { |
| 671 | $$ = make_arith_for_command ($2, $6, arith_for_lineno); |
| 672 | if (word_top > 0) word_top--; |
| 673 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 674 | | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}' |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 675 | { |
| 676 | $$ = make_arith_for_command ($2, $6, arith_for_lineno); |
| 677 | if (word_top > 0) word_top--; |
| 678 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 679 | | FOR ARITH_FOR_EXPRS DO compound_list DONE |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 680 | { |
| 681 | $$ = make_arith_for_command ($2, $4, arith_for_lineno); |
| 682 | if (word_top > 0) word_top--; |
| 683 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 684 | | FOR ARITH_FOR_EXPRS '{' compound_list '}' |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 685 | { |
| 686 | $$ = make_arith_for_command ($2, $4, arith_for_lineno); |
| 687 | if (word_top > 0) word_top--; |
| 688 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 689 | ; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 690 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 691 | select_command: SELECT WORD newline_list DO list DONE |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 692 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 693 | $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]); |
| 694 | if (word_top > 0) word_top--; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 695 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 696 | | SELECT WORD newline_list '{' list '}' |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 697 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 698 | $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]); |
| 699 | if (word_top > 0) word_top--; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 700 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 701 | | SELECT WORD ';' newline_list DO list DONE |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 702 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 703 | $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]); |
| 704 | if (word_top > 0) word_top--; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 705 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 706 | | SELECT WORD ';' newline_list '{' list '}' |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 707 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 708 | $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]); |
| 709 | if (word_top > 0) word_top--; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 710 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 711 | | SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 712 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 713 | $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]); |
| 714 | if (word_top > 0) word_top--; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 715 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 716 | | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}' |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 717 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 718 | $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]); |
| 719 | if (word_top > 0) word_top--; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 720 | } |
| 721 | ; |
| 722 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 723 | case_command: CASE WORD newline_list IN newline_list ESAC |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 724 | { |
| 725 | $$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]); |
| 726 | if (word_top > 0) word_top--; |
| 727 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 728 | | CASE WORD newline_list IN case_clause_sequence newline_list ESAC |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 729 | { |
| 730 | $$ = make_case_command ($2, $5, word_lineno[word_top]); |
| 731 | if (word_top > 0) word_top--; |
| 732 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 733 | | CASE WORD newline_list IN case_clause ESAC |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 734 | { |
| 735 | $$ = make_case_command ($2, $5, word_lineno[word_top]); |
| 736 | if (word_top > 0) word_top--; |
| 737 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 738 | ; |
| 739 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 740 | function_def: WORD '(' ')' newline_list function_body |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 741 | { $$ = make_function_def ($1, $5, function_dstart, function_bstart); } |
| 742 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 743 | | FUNCTION WORD '(' ')' newline_list function_body |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 744 | { $$ = make_function_def ($2, $6, function_dstart, function_bstart); } |
| 745 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 746 | | FUNCTION WORD newline_list function_body |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 747 | { $$ = make_function_def ($2, $4, function_dstart, function_bstart); } |
| 748 | ; |
| 749 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 750 | |
| 751 | function_body: shell_command |
| 752 | { $$ = $1; } |
| 753 | | shell_command redirection_list |
| 754 | { |
| 755 | COMMAND *tc; |
| 756 | |
| 757 | tc = $1; |
| 758 | /* According to Posix.2 3.9.5, redirections |
| 759 | specified after the body of a function should |
| 760 | be attached to the function and performed when |
| 761 | the function is executed, not as part of the |
| 762 | function definition command. */ |
| 763 | /* XXX - I don't think it matters, but we might |
| 764 | want to change this in the future to avoid |
| 765 | problems differentiating between a function |
| 766 | definition with a redirection and a function |
| 767 | definition containing a single command with a |
| 768 | redirection. The two are semantically equivalent, |
| 769 | though -- the only difference is in how the |
| 770 | command printing code displays the redirections. */ |
| 771 | if (tc->redirects) |
| 772 | { |
| 773 | register REDIRECT *t; |
| 774 | for (t = tc->redirects; t->next; t = t->next) |
| 775 | ; |
| 776 | t->next = $2; |
| 777 | } |
| 778 | else |
| 779 | tc->redirects = $2; |
| 780 | $$ = $1; |
| 781 | } |
| 782 | ; |
| 783 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 784 | subshell: '(' compound_list ')' |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 785 | { |
| 786 | $$ = make_subshell_command ($2); |
| 787 | $$->flags |= CMD_WANT_SUBSHELL; |
| 788 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 789 | ; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 790 | |
| 791 | if_command: IF compound_list THEN compound_list FI |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 792 | { $$ = make_if_command ($2, $4, (COMMAND *)NULL); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 793 | | IF compound_list THEN compound_list ELSE compound_list FI |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 794 | { $$ = make_if_command ($2, $4, $6); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 795 | | IF compound_list THEN compound_list elif_clause FI |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 796 | { $$ = make_if_command ($2, $4, $5); } |
| 797 | ; |
| 798 | |
| 799 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 800 | group_command: '{' compound_list '}' |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 801 | { $$ = make_group_command ($2); } |
| 802 | ; |
| 803 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 804 | arith_command: ARITH_CMD |
| 805 | { $$ = make_arith_command ($1); } |
| 806 | ; |
| 807 | |
| 808 | cond_command: COND_START COND_CMD COND_END |
| 809 | { $$ = $2; } |
| 810 | ; |
| 811 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 812 | elif_clause: ELIF compound_list THEN compound_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 813 | { $$ = make_if_command ($2, $4, (COMMAND *)NULL); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 814 | | ELIF compound_list THEN compound_list ELSE compound_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 815 | { $$ = make_if_command ($2, $4, $6); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 816 | | ELIF compound_list THEN compound_list elif_clause |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 817 | { $$ = make_if_command ($2, $4, $5); } |
| 818 | ; |
| 819 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 820 | case_clause: pattern_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 821 | | case_clause_sequence pattern_list |
| 822 | { $2->next = $1; $$ = $2; } |
| 823 | ; |
| 824 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 825 | pattern_list: newline_list pattern ')' compound_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 826 | { $$ = make_pattern_list ($2, $4); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 827 | | newline_list pattern ')' newline_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 828 | { $$ = make_pattern_list ($2, (COMMAND *)NULL); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 829 | | newline_list '(' pattern ')' compound_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 830 | { $$ = make_pattern_list ($3, $5); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 831 | | newline_list '(' pattern ')' newline_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 832 | { $$ = make_pattern_list ($3, (COMMAND *)NULL); } |
| 833 | ; |
| 834 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 835 | case_clause_sequence: pattern_list SEMI_SEMI |
| 836 | | case_clause_sequence pattern_list SEMI_SEMI |
| 837 | { $2->next = $1; $$ = $2; } |
| 838 | ; |
| 839 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 840 | pattern: WORD |
| 841 | { $$ = make_word_list ($1, (WORD_LIST *)NULL); } |
| 842 | | pattern '|' WORD |
| 843 | { $$ = make_word_list ($3, $1); } |
| 844 | ; |
| 845 | |
| 846 | /* A list allows leading or trailing newlines and |
| 847 | newlines as operators (equivalent to semicolons). |
| 848 | It must end with a newline or semicolon. |
| 849 | Lists are used within commands such as if, for, while. */ |
| 850 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 851 | list: newline_list list0 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 852 | { |
| 853 | $$ = $2; |
| 854 | if (need_here_doc) |
| 855 | gather_here_documents (); |
| 856 | } |
| 857 | ; |
| 858 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 859 | compound_list: list |
| 860 | | newline_list list1 |
| 861 | { |
| 862 | $$ = $2; |
| 863 | } |
| 864 | ; |
| 865 | |
| 866 | list0: list1 '\n' newline_list |
| 867 | | list1 '&' newline_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 868 | { |
| 869 | if ($1->type == cm_connection) |
| 870 | $$ = connect_async_list ($1, (COMMAND *)NULL, '&'); |
| 871 | else |
| 872 | $$ = command_connect ($1, (COMMAND *)NULL, '&'); |
| 873 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 874 | | list1 ';' newline_list |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 875 | |
| 876 | ; |
| 877 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 878 | list1: list1 AND_AND newline_list list1 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 879 | { $$ = command_connect ($1, $4, AND_AND); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 880 | | list1 OR_OR newline_list list1 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 881 | { $$ = command_connect ($1, $4, OR_OR); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 882 | | list1 '&' newline_list list1 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 883 | { |
| 884 | if ($1->type == cm_connection) |
| 885 | $$ = connect_async_list ($1, $4, '&'); |
| 886 | else |
| 887 | $$ = command_connect ($1, $4, '&'); |
| 888 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 889 | | list1 ';' newline_list list1 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 890 | { $$ = command_connect ($1, $4, ';'); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 891 | | list1 '\n' newline_list list1 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 892 | { $$ = command_connect ($1, $4, ';'); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 893 | | pipeline_command |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 894 | { $$ = $1; } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 895 | ; |
| 896 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 897 | simple_list_terminator: '\n' |
| 898 | | yacc_EOF |
| 899 | ; |
| 900 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 901 | list_terminator:'\n' |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 902 | { $$ = '\n'; } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 903 | | ';' |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 904 | { $$ = ';'; } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 905 | | yacc_EOF |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 906 | { $$ = yacc_EOF; } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 907 | ; |
| 908 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 909 | newline_list: |
| 910 | | newline_list '\n' |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 911 | ; |
| 912 | |
| 913 | /* A simple_list is a list that contains no significant newlines |
| 914 | and no leading or trailing newlines. Newlines are allowed |
| 915 | only following operators, where they are not significant. |
| 916 | |
| 917 | This is what an inputunit consists of. */ |
| 918 | |
| 919 | simple_list: simple_list1 |
| 920 | { |
| 921 | $$ = $1; |
| 922 | if (need_here_doc) |
| 923 | gather_here_documents (); |
| 924 | } |
| 925 | | simple_list1 '&' |
| 926 | { |
| 927 | if ($1->type == cm_connection) |
| 928 | $$ = connect_async_list ($1, (COMMAND *)NULL, '&'); |
| 929 | else |
| 930 | $$ = command_connect ($1, (COMMAND *)NULL, '&'); |
| 931 | if (need_here_doc) |
| 932 | gather_here_documents (); |
| 933 | } |
| 934 | | simple_list1 ';' |
| 935 | { |
| 936 | $$ = $1; |
| 937 | if (need_here_doc) |
| 938 | gather_here_documents (); |
| 939 | } |
| 940 | ; |
| 941 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 942 | simple_list1: simple_list1 AND_AND newline_list simple_list1 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 943 | { $$ = command_connect ($1, $4, AND_AND); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 944 | | simple_list1 OR_OR newline_list simple_list1 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 945 | { $$ = command_connect ($1, $4, OR_OR); } |
| 946 | | simple_list1 '&' simple_list1 |
| 947 | { |
| 948 | if ($1->type == cm_connection) |
| 949 | $$ = connect_async_list ($1, $3, '&'); |
| 950 | else |
| 951 | $$ = command_connect ($1, $3, '&'); |
| 952 | } |
| 953 | | simple_list1 ';' simple_list1 |
| 954 | { $$ = command_connect ($1, $3, ';'); } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 955 | |
| 956 | | pipeline_command |
| 957 | { $$ = $1; } |
| 958 | ; |
| 959 | |
| 960 | pipeline_command: pipeline |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 961 | { $$ = $1; } |
| 962 | | BANG pipeline |
| 963 | { |
| 964 | $2->flags |= CMD_INVERT_RETURN; |
| 965 | $$ = $2; |
| 966 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 967 | | timespec pipeline |
| 968 | { |
| 969 | $2->flags |= $1; |
| 970 | $$ = $2; |
| 971 | } |
| 972 | | timespec BANG pipeline |
| 973 | { |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 974 | $3->flags |= $1|CMD_INVERT_RETURN; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 975 | $$ = $3; |
| 976 | } |
| 977 | | BANG timespec pipeline |
| 978 | { |
| 979 | $3->flags |= $2|CMD_INVERT_RETURN; |
| 980 | $$ = $3; |
| 981 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 982 | | timespec list_terminator |
| 983 | { |
| 984 | ELEMENT x; |
| 985 | |
| 986 | /* Boy, this is unclean. `time' by itself can |
| 987 | time a null command. We cheat and push a |
| 988 | newline back if the list_terminator was a newline |
| 989 | to avoid the double-newline problem (one to |
| 990 | terminate this, one to terminate the command) */ |
| 991 | x.word = 0; |
| 992 | x.redirect = 0; |
| 993 | $$ = make_simple_command (x, (COMMAND *)NULL); |
| 994 | $$->flags |= $1; |
| 995 | /* XXX - let's cheat and push a newline back */ |
| 996 | if ($2 == '\n') |
| 997 | token_to_read = '\n'; |
| 998 | } |
| 999 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1000 | ; |
| 1001 | |
| 1002 | pipeline: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1003 | pipeline '|' newline_list pipeline |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1004 | { $$ = command_connect ($1, $4, '|'); } |
| 1005 | | command |
| 1006 | { $$ = $1; } |
| 1007 | ; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1008 | |
| 1009 | timespec: TIME |
| 1010 | { $$ = CMD_TIME_PIPELINE; } |
| 1011 | | TIME TIMEOPT |
| 1012 | { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; } |
| 1013 | ; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1014 | %% |
| 1015 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1016 | /* Possible states for the parser that require it to do special things. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1017 | #define PST_CASEPAT 0x0001 /* in a case pattern list */ |
| 1018 | #define PST_ALEXPNEXT 0x0002 /* expand next word for aliases */ |
| 1019 | #define PST_ALLOWOPNBRC 0x0004 /* allow open brace for function def */ |
| 1020 | #define PST_NEEDCLOSBRC 0x0008 /* need close brace */ |
| 1021 | #define PST_DBLPAREN 0x0010 /* double-paren parsing */ |
| 1022 | #define PST_SUBSHELL 0x0020 /* ( ... ) subshell */ |
| 1023 | #define PST_CMDSUBST 0x0040 /* $( ... ) command substitution */ |
| 1024 | #define PST_CASESTMT 0x0080 /* parsing a case statement */ |
| 1025 | #define PST_CONDCMD 0x0100 /* parsing a [[...]] command */ |
| 1026 | #define PST_CONDEXPR 0x0200 /* parsing the guts of [[...]] */ |
| 1027 | #define PST_ARITHFOR 0x0400 /* parsing an arithmetic for command */ |
| 1028 | #define PST_ALEXPAND 0x0800 /* OK to expand aliases - unused */ |
| 1029 | #define PST_CMDTOKEN 0x1000 /* command token OK - unused */ |
| 1030 | #define PST_COMPASSIGN 0x2000 /* parsing x=(...) compound assignment */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1031 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1032 | /* Initial size to allocate for tokens, and the |
| 1033 | amount to grow them by. */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1034 | #define TOKEN_DEFAULT_INITIAL_SIZE 496 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1035 | #define TOKEN_DEFAULT_GROW_SIZE 512 |
| 1036 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1037 | /* Should we call prompt_again? */ |
| 1038 | #define SHOULD_PROMPT() \ |
| 1039 | (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream)) |
| 1040 | |
| 1041 | #if defined (ALIAS) |
| 1042 | # define expanding_alias() (pushed_string_list && pushed_string_list->expander) |
| 1043 | #else |
| 1044 | # define expanding_alias() 0 |
| 1045 | #endif |
| 1046 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1047 | /* The token currently being read. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1048 | static int current_token; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1049 | |
| 1050 | /* The last read token, or NULL. read_token () uses this for context |
| 1051 | checking. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1052 | static int last_read_token; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1053 | |
| 1054 | /* The token read prior to last_read_token. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1055 | static int token_before_that; |
| 1056 | |
| 1057 | /* The token read prior to token_before_that. */ |
| 1058 | static int two_tokens_ago; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1059 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1060 | /* The current parser state. */ |
| 1061 | static int parser_state; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1062 | |
| 1063 | /* Global var is non-zero when end of file has been reached. */ |
| 1064 | int EOF_Reached = 0; |
| 1065 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1066 | #ifdef DEBUG |
| 1067 | static void |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1068 | debug_parser (i) |
| 1069 | int i; |
| 1070 | { |
| 1071 | #if YYDEBUG != 0 |
| 1072 | yydebug = i; |
| 1073 | #endif |
| 1074 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1075 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1076 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1077 | /* yy_getc () returns the next available character from input or EOF. |
| 1078 | yy_ungetc (c) makes `c' the next character to read. |
| 1079 | init_yy_io (get, unget, type, location) makes the function GET the |
| 1080 | installed function for getting the next character, makes UNGET the |
| 1081 | installed function for un-getting a character, sets the type of stream |
| 1082 | (either string or file) from TYPE, and makes LOCATION point to where |
| 1083 | the input is coming from. */ |
| 1084 | |
| 1085 | /* Unconditionally returns end-of-file. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1086 | int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1087 | return_EOF () |
| 1088 | { |
| 1089 | return (EOF); |
| 1090 | } |
| 1091 | |
| 1092 | /* Variable containing the current get and unget functions. |
| 1093 | See ./input.h for a clearer description. */ |
| 1094 | BASH_INPUT bash_input; |
| 1095 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1096 | /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it |
| 1097 | is non-null, avoiding a memory leak. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1098 | void |
| 1099 | initialize_bash_input () |
| 1100 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1101 | bash_input.type = st_none; |
| 1102 | FREE (bash_input.name); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1103 | bash_input.name = (char *)NULL; |
| 1104 | bash_input.location.file = (FILE *)NULL; |
| 1105 | bash_input.location.string = (char *)NULL; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1106 | bash_input.getter = (sh_cget_func_t *)NULL; |
| 1107 | bash_input.ungetter = (sh_cunget_func_t *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | /* Set the contents of the current bash input stream from |
| 1111 | GET, UNGET, TYPE, NAME, and LOCATION. */ |
| 1112 | void |
| 1113 | init_yy_io (get, unget, type, name, location) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1114 | sh_cget_func_t *get; |
| 1115 | sh_cunget_func_t *unget; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1116 | enum stream_type type; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1117 | const char *name; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1118 | INPUT_STREAM location; |
| 1119 | { |
| 1120 | bash_input.type = type; |
| 1121 | FREE (bash_input.name); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1122 | bash_input.name = name ? savestring (name) : (char *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1123 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1124 | /* XXX */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1125 | #if defined (CRAY) |
| 1126 | memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location)); |
| 1127 | #else |
| 1128 | bash_input.location = location; |
| 1129 | #endif |
| 1130 | bash_input.getter = get; |
| 1131 | bash_input.ungetter = unget; |
| 1132 | } |
| 1133 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1134 | char * |
| 1135 | yy_input_name () |
| 1136 | { |
| 1137 | return (bash_input.name ? bash_input.name : "stdin"); |
| 1138 | } |
| 1139 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1140 | /* Call this to get the next character of input. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1141 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1142 | yy_getc () |
| 1143 | { |
| 1144 | return (*(bash_input.getter)) (); |
| 1145 | } |
| 1146 | |
| 1147 | /* Call this to unget C. That is, to make C the next character |
| 1148 | to be read. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1149 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1150 | yy_ungetc (c) |
| 1151 | int c; |
| 1152 | { |
| 1153 | return (*(bash_input.ungetter)) (c); |
| 1154 | } |
| 1155 | |
| 1156 | #if defined (BUFFERED_INPUT) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1157 | #ifdef INCLUDE_UNUSED |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1158 | int |
| 1159 | input_file_descriptor () |
| 1160 | { |
| 1161 | switch (bash_input.type) |
| 1162 | { |
| 1163 | case st_stream: |
| 1164 | return (fileno (bash_input.location.file)); |
| 1165 | case st_bstream: |
| 1166 | return (bash_input.location.buffered_fd); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1167 | case st_stdin: |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1168 | default: |
| 1169 | return (fileno (stdin)); |
| 1170 | } |
| 1171 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1172 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1173 | #endif /* BUFFERED_INPUT */ |
| 1174 | |
| 1175 | /* **************************************************************** */ |
| 1176 | /* */ |
| 1177 | /* Let input be read from readline (). */ |
| 1178 | /* */ |
| 1179 | /* **************************************************************** */ |
| 1180 | |
| 1181 | #if defined (READLINE) |
| 1182 | char *current_readline_prompt = (char *)NULL; |
| 1183 | char *current_readline_line = (char *)NULL; |
| 1184 | int current_readline_line_index = 0; |
| 1185 | |
| 1186 | static int |
| 1187 | yy_readline_get () |
| 1188 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1189 | SigHandler *old_sigint; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1190 | int line_len; |
| 1191 | unsigned char c; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1192 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1193 | if (!current_readline_line) |
| 1194 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1195 | if (!bash_readline_initialized) |
| 1196 | initialize_readline (); |
| 1197 | |
| 1198 | #if defined (JOB_CONTROL) |
| 1199 | if (job_control) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1200 | give_terminal_to (shell_pgrp, 0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1201 | #endif /* JOB_CONTROL */ |
| 1202 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1203 | old_sigint = (SigHandler *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1204 | if (signal_is_ignored (SIGINT) == 0) |
| 1205 | { |
| 1206 | old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler); |
| 1207 | interrupt_immediately++; |
| 1208 | } |
| 1209 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1210 | current_readline_line = readline (current_readline_prompt ? |
| 1211 | current_readline_prompt : ""); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1212 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1213 | if (signal_is_ignored (SIGINT) == 0 && old_sigint) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1214 | { |
| 1215 | interrupt_immediately--; |
| 1216 | set_signal_handler (SIGINT, old_sigint); |
| 1217 | } |
| 1218 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1219 | #if 0 |
| 1220 | /* Reset the prompt to the decoded value of prompt_string_pointer. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1221 | reset_readline_prompt (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1222 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1223 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1224 | if (current_readline_line == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1225 | return (EOF); |
| 1226 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1227 | current_readline_line_index = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1228 | line_len = strlen (current_readline_line); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1229 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1230 | current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1231 | current_readline_line[line_len++] = '\n'; |
| 1232 | current_readline_line[line_len] = '\0'; |
| 1233 | } |
| 1234 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1235 | if (current_readline_line[current_readline_line_index] == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1236 | { |
| 1237 | free (current_readline_line); |
| 1238 | current_readline_line = (char *)NULL; |
| 1239 | return (yy_readline_get ()); |
| 1240 | } |
| 1241 | else |
| 1242 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1243 | c = current_readline_line[current_readline_line_index++]; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1244 | return (c); |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | static int |
| 1249 | yy_readline_unget (c) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1250 | int c; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1251 | { |
| 1252 | if (current_readline_line_index && current_readline_line) |
| 1253 | current_readline_line[--current_readline_line_index] = c; |
| 1254 | return (c); |
| 1255 | } |
| 1256 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1257 | void |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1258 | with_input_from_stdin () |
| 1259 | { |
| 1260 | INPUT_STREAM location; |
| 1261 | |
| 1262 | if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0) |
| 1263 | { |
| 1264 | location.string = current_readline_line; |
| 1265 | init_yy_io (yy_readline_get, yy_readline_unget, |
| 1266 | st_stdin, "readline stdin", location); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | #else /* !READLINE */ |
| 1271 | |
| 1272 | void |
| 1273 | with_input_from_stdin () |
| 1274 | { |
| 1275 | with_input_from_stream (stdin, "stdin"); |
| 1276 | } |
| 1277 | #endif /* !READLINE */ |
| 1278 | |
| 1279 | /* **************************************************************** */ |
| 1280 | /* */ |
| 1281 | /* Let input come from STRING. STRING is zero terminated. */ |
| 1282 | /* */ |
| 1283 | /* **************************************************************** */ |
| 1284 | |
| 1285 | static int |
| 1286 | yy_string_get () |
| 1287 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1288 | register char *string; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1289 | register unsigned char c; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1290 | |
| 1291 | string = bash_input.location.string; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1292 | |
| 1293 | /* If the string doesn't exist, or is empty, EOF found. */ |
| 1294 | if (string && *string) |
| 1295 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1296 | c = *string++; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1297 | bash_input.location.string = string; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1298 | return (c); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1299 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1300 | else |
| 1301 | return (EOF); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | static int |
| 1305 | yy_string_unget (c) |
| 1306 | int c; |
| 1307 | { |
| 1308 | *(--bash_input.location.string) = c; |
| 1309 | return (c); |
| 1310 | } |
| 1311 | |
| 1312 | void |
| 1313 | with_input_from_string (string, name) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1314 | char *string; |
| 1315 | const char *name; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1316 | { |
| 1317 | INPUT_STREAM location; |
| 1318 | |
| 1319 | location.string = string; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1320 | init_yy_io (yy_string_get, yy_string_unget, st_string, name, location); |
| 1321 | } |
| 1322 | |
| 1323 | /* **************************************************************** */ |
| 1324 | /* */ |
| 1325 | /* Let input come from STREAM. */ |
| 1326 | /* */ |
| 1327 | /* **************************************************************** */ |
| 1328 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1329 | /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS |
| 1330 | define, and just use getc/ungetc if it was defined, but since bash |
| 1331 | installs its signal handlers without the SA_RESTART flag, some signals |
| 1332 | (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause |
| 1333 | the read to be restarted. We need to restart it ourselves. */ |
| 1334 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1335 | static int |
| 1336 | yy_stream_get () |
| 1337 | { |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1338 | int result; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1339 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1340 | result = EOF; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1341 | if (bash_input.location.file) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1342 | result = getc_with_restart (bash_input.location.file); |
| 1343 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1344 | return (result); |
| 1345 | } |
| 1346 | |
| 1347 | static int |
| 1348 | yy_stream_unget (c) |
| 1349 | int c; |
| 1350 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1351 | return (ungetc_with_restart (c, bash_input.location.file)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | void |
| 1355 | with_input_from_stream (stream, name) |
| 1356 | FILE *stream; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1357 | const char *name; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1358 | { |
| 1359 | INPUT_STREAM location; |
| 1360 | |
| 1361 | location.file = stream; |
| 1362 | init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location); |
| 1363 | } |
| 1364 | |
| 1365 | typedef struct stream_saver { |
| 1366 | struct stream_saver *next; |
| 1367 | BASH_INPUT bash_input; |
| 1368 | int line; |
| 1369 | #if defined (BUFFERED_INPUT) |
| 1370 | BUFFERED_STREAM *bstream; |
| 1371 | #endif /* BUFFERED_INPUT */ |
| 1372 | } STREAM_SAVER; |
| 1373 | |
| 1374 | /* The globally known line number. */ |
| 1375 | int line_number = 0; |
| 1376 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1377 | #if defined (COND_COMMAND) |
| 1378 | static int cond_lineno; |
| 1379 | static int cond_token; |
| 1380 | #endif |
| 1381 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1382 | STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL; |
| 1383 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1384 | void |
| 1385 | push_stream (reset_lineno) |
| 1386 | int reset_lineno; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1387 | { |
| 1388 | STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER)); |
| 1389 | |
| 1390 | xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT)); |
| 1391 | |
| 1392 | #if defined (BUFFERED_INPUT) |
| 1393 | saver->bstream = (BUFFERED_STREAM *)NULL; |
| 1394 | /* If we have a buffered stream, clear out buffers[fd]. */ |
| 1395 | if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1396 | saver->bstream = set_buffered_stream (bash_input.location.buffered_fd, |
| 1397 | (BUFFERED_STREAM *)NULL); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1398 | #endif /* BUFFERED_INPUT */ |
| 1399 | |
| 1400 | saver->line = line_number; |
| 1401 | bash_input.name = (char *)NULL; |
| 1402 | saver->next = stream_list; |
| 1403 | stream_list = saver; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1404 | EOF_Reached = 0; |
| 1405 | if (reset_lineno) |
| 1406 | line_number = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1409 | void |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1410 | pop_stream () |
| 1411 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1412 | if (!stream_list) |
| 1413 | EOF_Reached = 1; |
| 1414 | else |
| 1415 | { |
| 1416 | STREAM_SAVER *saver = stream_list; |
| 1417 | |
| 1418 | EOF_Reached = 0; |
| 1419 | stream_list = stream_list->next; |
| 1420 | |
| 1421 | init_yy_io (saver->bash_input.getter, |
| 1422 | saver->bash_input.ungetter, |
| 1423 | saver->bash_input.type, |
| 1424 | saver->bash_input.name, |
| 1425 | saver->bash_input.location); |
| 1426 | |
| 1427 | #if defined (BUFFERED_INPUT) |
| 1428 | /* If we have a buffered stream, restore buffers[fd]. */ |
| 1429 | /* If the input file descriptor was changed while this was on the |
| 1430 | save stack, update the buffered fd to the new file descriptor and |
| 1431 | re-establish the buffer <-> bash_input fd correspondence. */ |
| 1432 | if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1433 | { |
| 1434 | if (bash_input_fd_changed) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1435 | { |
| 1436 | bash_input_fd_changed = 0; |
| 1437 | if (default_buffered_input >= 0) |
| 1438 | { |
| 1439 | bash_input.location.buffered_fd = default_buffered_input; |
| 1440 | saver->bstream->b_fd = default_buffered_input; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1441 | SET_CLOSE_ON_EXEC (default_buffered_input); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1442 | } |
| 1443 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1444 | /* XXX could free buffered stream returned as result here. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1445 | set_buffered_stream (bash_input.location.buffered_fd, saver->bstream); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1446 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1447 | #endif /* BUFFERED_INPUT */ |
| 1448 | |
| 1449 | line_number = saver->line; |
| 1450 | |
| 1451 | FREE (saver->bash_input.name); |
| 1452 | free (saver); |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | /* Return 1 if a stream of type TYPE is saved on the stack. */ |
| 1457 | int |
| 1458 | stream_on_stack (type) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1459 | enum stream_type type; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1460 | { |
| 1461 | register STREAM_SAVER *s; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1462 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1463 | for (s = stream_list; s; s = s->next) |
| 1464 | if (s->bash_input.type == type) |
| 1465 | return 1; |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1469 | /* Save the current token state and return it in a malloced array. */ |
| 1470 | int * |
| 1471 | save_token_state () |
| 1472 | { |
| 1473 | int *ret; |
| 1474 | |
| 1475 | ret = (int *)xmalloc (3 * sizeof (int)); |
| 1476 | ret[0] = last_read_token; |
| 1477 | ret[1] = token_before_that; |
| 1478 | ret[2] = two_tokens_ago; |
| 1479 | return ret; |
| 1480 | } |
| 1481 | |
| 1482 | void |
| 1483 | restore_token_state (ts) |
| 1484 | int *ts; |
| 1485 | { |
| 1486 | if (ts == 0) |
| 1487 | return; |
| 1488 | last_read_token = ts[0]; |
| 1489 | token_before_that = ts[1]; |
| 1490 | two_tokens_ago = ts[2]; |
| 1491 | } |
| 1492 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1493 | /* |
| 1494 | * This is used to inhibit alias expansion and reserved word recognition |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1495 | * inside case statement pattern lists. A `case statement pattern list' is: |
| 1496 | * |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1497 | * everything between the `in' in a `case word in' and the next ')' |
| 1498 | * or `esac' |
| 1499 | * everything between a `;;' and the next `)' or `esac' |
| 1500 | */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1501 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1502 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| 1503 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1504 | #define END_OF_ALIAS 0 |
| 1505 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1506 | /* |
| 1507 | * Pseudo-global variables used in implementing token-wise alias expansion. |
| 1508 | */ |
| 1509 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1510 | /* |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1511 | * Pushing and popping strings. This works together with shell_getc to |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1512 | * implement alias expansion on a per-token basis. |
| 1513 | */ |
| 1514 | |
| 1515 | typedef struct string_saver { |
| 1516 | struct string_saver *next; |
| 1517 | int expand_alias; /* Value to set expand_alias to when string is popped. */ |
| 1518 | char *saved_line; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1519 | #if defined (ALIAS) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1520 | alias_t *expander; /* alias that caused this line to be pushed. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1521 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1522 | int saved_line_size, saved_line_index, saved_line_terminator; |
| 1523 | } STRING_SAVER; |
| 1524 | |
| 1525 | STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL; |
| 1526 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1527 | /* |
| 1528 | * Push the current shell_input_line onto a stack of such lines and make S |
| 1529 | * the current input. Used when expanding aliases. EXPAND is used to set |
| 1530 | * the value of expand_next_token when the string is popped, so that the |
| 1531 | * word after the alias in the original line is handled correctly when the |
| 1532 | * alias expands to multiple words. TOKEN is the token that was expanded |
| 1533 | * into S; it is saved and used to prevent infinite recursive expansion. |
| 1534 | */ |
| 1535 | static void |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1536 | push_string (s, expand, ap) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1537 | char *s; |
| 1538 | int expand; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1539 | alias_t *ap; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1540 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1541 | STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1542 | |
| 1543 | temp->expand_alias = expand; |
| 1544 | temp->saved_line = shell_input_line; |
| 1545 | temp->saved_line_size = shell_input_line_size; |
| 1546 | temp->saved_line_index = shell_input_line_index; |
| 1547 | temp->saved_line_terminator = shell_input_line_terminator; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1548 | #if defined (ALIAS) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1549 | temp->expander = ap; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1550 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1551 | temp->next = pushed_string_list; |
| 1552 | pushed_string_list = temp; |
| 1553 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1554 | #if defined (ALIAS) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1555 | if (ap) |
| 1556 | ap->flags |= AL_BEINGEXPANDED; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1557 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1558 | |
| 1559 | shell_input_line = s; |
| 1560 | shell_input_line_size = strlen (s); |
| 1561 | shell_input_line_index = 0; |
| 1562 | shell_input_line_terminator = '\0'; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1563 | #if 0 |
| 1564 | parser_state &= ~PST_ALEXPNEXT; /* XXX */ |
| 1565 | #endif |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1566 | |
| 1567 | set_line_mbstate (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * Make the top of the pushed_string stack be the current shell input. |
| 1572 | * Only called when there is something on the stack. Called from shell_getc |
| 1573 | * when it thinks it has consumed the string generated by an alias expansion |
| 1574 | * and needs to return to the original input line. |
| 1575 | */ |
| 1576 | static void |
| 1577 | pop_string () |
| 1578 | { |
| 1579 | STRING_SAVER *t; |
| 1580 | |
| 1581 | FREE (shell_input_line); |
| 1582 | shell_input_line = pushed_string_list->saved_line; |
| 1583 | shell_input_line_index = pushed_string_list->saved_line_index; |
| 1584 | shell_input_line_size = pushed_string_list->saved_line_size; |
| 1585 | shell_input_line_terminator = pushed_string_list->saved_line_terminator; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1586 | |
| 1587 | if (pushed_string_list->expand_alias) |
| 1588 | parser_state |= PST_ALEXPNEXT; |
| 1589 | else |
| 1590 | parser_state &= ~PST_ALEXPNEXT; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1591 | |
| 1592 | t = pushed_string_list; |
| 1593 | pushed_string_list = pushed_string_list->next; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1594 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1595 | #if defined (ALIAS) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1596 | if (t->expander) |
| 1597 | t->expander->flags &= ~AL_BEINGEXPANDED; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1598 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1599 | |
| 1600 | free ((char *)t); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1601 | |
| 1602 | set_line_mbstate (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | static void |
| 1606 | free_string_list () |
| 1607 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1608 | register STRING_SAVER *t, *t1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1609 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1610 | for (t = pushed_string_list; t; ) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1611 | { |
| 1612 | t1 = t->next; |
| 1613 | FREE (t->saved_line); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1614 | #if defined (ALIAS) |
| 1615 | if (t->expander) |
| 1616 | t->expander->flags &= ~AL_BEINGEXPANDED; |
| 1617 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1618 | free ((char *)t); |
| 1619 | t = t1; |
| 1620 | } |
| 1621 | pushed_string_list = (STRING_SAVER *)NULL; |
| 1622 | } |
| 1623 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1624 | #endif /* ALIAS || DPAREN_ARITHMETIC */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1625 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1626 | void |
| 1627 | free_pushed_string_input () |
| 1628 | { |
| 1629 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| 1630 | free_string_list (); |
| 1631 | #endif |
| 1632 | } |
| 1633 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1634 | /* Return a line of text, taken from wherever yylex () reads input. |
| 1635 | If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE |
| 1636 | is non-zero, we remove unquoted \<newline> pairs. This is used by |
| 1637 | read_secondary_line to read here documents. */ |
| 1638 | static char * |
| 1639 | read_a_line (remove_quoted_newline) |
| 1640 | int remove_quoted_newline; |
| 1641 | { |
| 1642 | static char *line_buffer = (char *)NULL; |
| 1643 | static int buffer_size = 0; |
| 1644 | int indx = 0, c, peekc, pass_next; |
| 1645 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1646 | #if defined (READLINE) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1647 | if (no_line_editing && SHOULD_PROMPT ()) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1648 | #else |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1649 | if (SHOULD_PROMPT ()) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1650 | #endif |
| 1651 | print_prompt (); |
| 1652 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1653 | pass_next = 0; |
| 1654 | while (1) |
| 1655 | { |
| 1656 | c = yy_getc (); |
| 1657 | |
| 1658 | /* Allow immediate exit if interrupted during input. */ |
| 1659 | QUIT; |
| 1660 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1661 | /* Ignore null bytes in input. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1662 | if (c == 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1663 | { |
| 1664 | #if 0 |
| 1665 | internal_warning ("read_a_line: ignored null byte in input"); |
| 1666 | #endif |
| 1667 | continue; |
| 1668 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1669 | |
| 1670 | /* If there is no more input, then we return NULL. */ |
| 1671 | if (c == EOF) |
| 1672 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1673 | if (interactive && bash_input.type == st_stream) |
| 1674 | clearerr (stdin); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1675 | if (indx == 0) |
| 1676 | return ((char *)NULL); |
| 1677 | c = '\n'; |
| 1678 | } |
| 1679 | |
| 1680 | /* `+2' in case the final character in the buffer is a newline. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1681 | RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1682 | |
| 1683 | /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a |
| 1684 | here document with an unquoted delimiter. In this case, |
| 1685 | the line will be expanded as if it were in double quotes. |
| 1686 | We allow a backslash to escape the next character, but we |
| 1687 | need to treat the backslash specially only if a backslash |
| 1688 | quoting a backslash-newline pair appears in the line. */ |
| 1689 | if (pass_next) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1690 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1691 | line_buffer[indx++] = c; |
| 1692 | pass_next = 0; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1693 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1694 | else if (c == '\\' && remove_quoted_newline) |
| 1695 | { |
| 1696 | peekc = yy_getc (); |
| 1697 | if (peekc == '\n') |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1698 | { |
| 1699 | line_number++; |
| 1700 | continue; /* Make the unquoted \<newline> pair disappear. */ |
| 1701 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1702 | else |
| 1703 | { |
| 1704 | yy_ungetc (peekc); |
| 1705 | pass_next = 1; |
| 1706 | line_buffer[indx++] = c; /* Preserve the backslash. */ |
| 1707 | } |
| 1708 | } |
| 1709 | else |
| 1710 | line_buffer[indx++] = c; |
| 1711 | |
| 1712 | if (c == '\n') |
| 1713 | { |
| 1714 | line_buffer[indx] = '\0'; |
| 1715 | return (line_buffer); |
| 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | /* Return a line as in read_a_line (), but insure that the prompt is |
| 1721 | the secondary prompt. This is used to read the lines of a here |
| 1722 | document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove |
| 1723 | newlines quoted with backslashes while reading the line. It is |
| 1724 | non-zero unless the delimiter of the here document was quoted. */ |
| 1725 | char * |
| 1726 | read_secondary_line (remove_quoted_newline) |
| 1727 | int remove_quoted_newline; |
| 1728 | { |
| 1729 | prompt_string_pointer = &ps2_prompt; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1730 | if (SHOULD_PROMPT()) |
| 1731 | prompt_again (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1732 | return (read_a_line (remove_quoted_newline)); |
| 1733 | } |
| 1734 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1735 | /* **************************************************************** */ |
| 1736 | /* */ |
| 1737 | /* YYLEX () */ |
| 1738 | /* */ |
| 1739 | /* **************************************************************** */ |
| 1740 | |
| 1741 | /* Reserved words. These are only recognized as the first word of a |
| 1742 | command. */ |
| 1743 | STRING_INT_ALIST word_token_alist[] = { |
| 1744 | { "if", IF }, |
| 1745 | { "then", THEN }, |
| 1746 | { "else", ELSE }, |
| 1747 | { "elif", ELIF }, |
| 1748 | { "fi", FI }, |
| 1749 | { "case", CASE }, |
| 1750 | { "esac", ESAC }, |
| 1751 | { "for", FOR }, |
| 1752 | #if defined (SELECT_COMMAND) |
| 1753 | { "select", SELECT }, |
| 1754 | #endif |
| 1755 | { "while", WHILE }, |
| 1756 | { "until", UNTIL }, |
| 1757 | { "do", DO }, |
| 1758 | { "done", DONE }, |
| 1759 | { "in", IN }, |
| 1760 | { "function", FUNCTION }, |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1761 | #if defined (COMMAND_TIMING) |
| 1762 | { "time", TIME }, |
| 1763 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1764 | { "{", '{' }, |
| 1765 | { "}", '}' }, |
| 1766 | { "!", BANG }, |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1767 | #if defined (COND_COMMAND) |
| 1768 | { "[[", COND_START }, |
| 1769 | { "]]", COND_END }, |
| 1770 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1771 | { (char *)NULL, 0} |
| 1772 | }; |
| 1773 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1774 | /* other tokens that can be returned by read_token() */ |
| 1775 | STRING_INT_ALIST other_token_alist[] = { |
| 1776 | /* Multiple-character tokens with special values */ |
| 1777 | { "-p", TIMEOPT }, |
| 1778 | { "&&", AND_AND }, |
| 1779 | { "||", OR_OR }, |
| 1780 | { ">>", GREATER_GREATER }, |
| 1781 | { "<<", LESS_LESS }, |
| 1782 | { "<&", LESS_AND }, |
| 1783 | { ">&", GREATER_AND }, |
| 1784 | { ";;", SEMI_SEMI }, |
| 1785 | { "<<-", LESS_LESS_MINUS }, |
| 1786 | { "<<<", LESS_LESS_LESS }, |
| 1787 | { "&>", AND_GREATER }, |
| 1788 | { "<>", LESS_GREATER }, |
| 1789 | { ">|", GREATER_BAR }, |
| 1790 | { "EOF", yacc_EOF }, |
| 1791 | /* Tokens whose value is the character itself */ |
| 1792 | { ">", '>' }, |
| 1793 | { "<", '<' }, |
| 1794 | { "-", '-' }, |
| 1795 | { "{", '{' }, |
| 1796 | { "}", '}' }, |
| 1797 | { ";", ';' }, |
| 1798 | { "(", '(' }, |
| 1799 | { ")", ')' }, |
| 1800 | { "|", '|' }, |
| 1801 | { "&", '&' }, |
| 1802 | { "newline", '\n' }, |
| 1803 | { (char *)NULL, 0} |
| 1804 | }; |
| 1805 | |
| 1806 | /* others not listed here: |
| 1807 | WORD look at yylval.word |
| 1808 | ASSIGNMENT_WORD look at yylval.word |
| 1809 | NUMBER look at yylval.number |
| 1810 | ARITH_CMD look at yylval.word_list |
| 1811 | ARITH_FOR_EXPRS look at yylval.word_list |
| 1812 | COND_CMD look at yylval.command |
| 1813 | */ |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1814 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1815 | /* These are used by read_token_word, but appear up here so that shell_getc |
| 1816 | can use them to decide when to add otherwise blank lines to the history. */ |
| 1817 | |
| 1818 | /* The primary delimiter stack. */ |
| 1819 | struct dstack dstack = { (char *)NULL, 0, 0 }; |
| 1820 | |
| 1821 | /* A temporary delimiter stack to be used when decoding prompt strings. |
| 1822 | This is needed because command substitutions in prompt strings (e.g., PS2) |
| 1823 | can screw up the parser's quoting state. */ |
| 1824 | static struct dstack temp_dstack = { (char *)NULL, 0, 0 }; |
| 1825 | |
| 1826 | /* Macro for accessing the top delimiter on the stack. Returns the |
| 1827 | delimiter or zero if none. */ |
| 1828 | #define current_delimiter(ds) \ |
| 1829 | (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0) |
| 1830 | |
| 1831 | #define push_delimiter(ds, character) \ |
| 1832 | do \ |
| 1833 | { \ |
| 1834 | if (ds.delimiter_depth + 2 > ds.delimiter_space) \ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1835 | ds.delimiters = (char *)xrealloc \ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1836 | (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \ |
| 1837 | ds.delimiters[ds.delimiter_depth] = character; \ |
| 1838 | ds.delimiter_depth++; \ |
| 1839 | } \ |
| 1840 | while (0) |
| 1841 | |
| 1842 | #define pop_delimiter(ds) ds.delimiter_depth-- |
| 1843 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1844 | /* Return the next shell input character. This always reads characters |
| 1845 | from shell_input_line; when that line is exhausted, it is time to |
| 1846 | read the next line. This is called by read_token when the shell is |
| 1847 | processing normal command input. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1848 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1849 | /* This implements one-character lookahead/lookbehind across physical input |
| 1850 | lines, to avoid something being lost because it's pushed back with |
| 1851 | shell_ungetc when we're at the start of a line. */ |
| 1852 | static int eol_ungetc_lookahead = 0; |
| 1853 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1854 | static int |
| 1855 | shell_getc (remove_quoted_newline) |
| 1856 | int remove_quoted_newline; |
| 1857 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1858 | register int i; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1859 | int c; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1860 | unsigned char uc; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1861 | static int mustpop = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1862 | |
| 1863 | QUIT; |
| 1864 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1865 | if (eol_ungetc_lookahead) |
| 1866 | { |
| 1867 | c = eol_ungetc_lookahead; |
| 1868 | eol_ungetc_lookahead = 0; |
| 1869 | return (c); |
| 1870 | } |
| 1871 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1872 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1873 | /* If shell_input_line[shell_input_line_index] == 0, but there is |
| 1874 | something on the pushed list of strings, then we don't want to go |
| 1875 | off and get another line. We let the code down below handle it. */ |
| 1876 | |
| 1877 | if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) && |
| 1878 | (pushed_string_list == (STRING_SAVER *)NULL))) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1879 | #else /* !ALIAS && !DPAREN_ARITHMETIC */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1880 | if (!shell_input_line || !shell_input_line[shell_input_line_index]) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1881 | #endif /* !ALIAS && !DPAREN_ARITHMETIC */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1882 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1883 | line_number++; |
| 1884 | |
| 1885 | restart_read: |
| 1886 | |
| 1887 | /* Allow immediate exit if interrupted during input. */ |
| 1888 | QUIT; |
| 1889 | |
| 1890 | i = 0; |
| 1891 | shell_input_line_terminator = 0; |
| 1892 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1893 | /* If the shell is interatctive, but not currently printing a prompt |
| 1894 | (interactive_shell && interactive == 0), we don't want to print |
| 1895 | notifies or cleanup the jobs -- we want to defer it until we do |
| 1896 | print the next prompt. */ |
| 1897 | if (interactive_shell == 0 || SHOULD_PROMPT()) |
| 1898 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1899 | #if defined (JOB_CONTROL) |
| 1900 | /* This can cause a problem when reading a command as the result |
| 1901 | of a trap, when the trap is called from flush_child. This call |
| 1902 | had better not cause jobs to disappear from the job table in |
| 1903 | that case, or we will have big trouble. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1904 | notify_and_cleanup (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1905 | #else /* !JOB_CONTROL */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1906 | cleanup_dead_jobs (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1907 | #endif /* !JOB_CONTROL */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1908 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1909 | |
| 1910 | #if defined (READLINE) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1911 | if (no_line_editing && SHOULD_PROMPT()) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1912 | #else |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 1913 | if (SHOULD_PROMPT()) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1914 | #endif |
| 1915 | print_prompt (); |
| 1916 | |
| 1917 | if (bash_input.type == st_stream) |
| 1918 | clearerr (stdin); |
| 1919 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1920 | while (1) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1921 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1922 | c = yy_getc (); |
| 1923 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1924 | /* Allow immediate exit if interrupted during input. */ |
| 1925 | QUIT; |
| 1926 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1927 | if (c == '\0') |
| 1928 | { |
| 1929 | #if 0 |
| 1930 | internal_warning ("shell_getc: ignored null byte in input"); |
| 1931 | #endif |
| 1932 | continue; |
| 1933 | } |
| 1934 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1935 | RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1936 | |
| 1937 | if (c == EOF) |
| 1938 | { |
| 1939 | if (bash_input.type == st_stream) |
| 1940 | clearerr (stdin); |
| 1941 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1942 | if (i == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1943 | shell_input_line_terminator = EOF; |
| 1944 | |
| 1945 | shell_input_line[i] = '\0'; |
| 1946 | break; |
| 1947 | } |
| 1948 | |
| 1949 | shell_input_line[i++] = c; |
| 1950 | |
| 1951 | if (c == '\n') |
| 1952 | { |
| 1953 | shell_input_line[--i] = '\0'; |
| 1954 | current_command_line_count++; |
| 1955 | break; |
| 1956 | } |
| 1957 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1958 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1959 | shell_input_line_index = 0; |
| 1960 | shell_input_line_len = i; /* == strlen (shell_input_line) */ |
| 1961 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1962 | set_line_mbstate (); |
| 1963 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1964 | #if defined (HISTORY) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1965 | if (remember_on_history && shell_input_line && shell_input_line[0]) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1966 | { |
| 1967 | char *expansions; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1968 | # if defined (BANG_HISTORY) |
| 1969 | int old_hist; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1970 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1971 | /* If the current delimiter is a single quote, we should not be |
| 1972 | performing history expansion, even if we're on a different |
| 1973 | line from the original single quote. */ |
| 1974 | old_hist = history_expansion_inhibited; |
| 1975 | if (current_delimiter (dstack) == '\'') |
| 1976 | history_expansion_inhibited = 1; |
| 1977 | # endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1978 | expansions = pre_process_line (shell_input_line, 1, 1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1979 | # if defined (BANG_HISTORY) |
| 1980 | history_expansion_inhibited = old_hist; |
| 1981 | # endif |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1982 | if (expansions != shell_input_line) |
| 1983 | { |
| 1984 | free (shell_input_line); |
| 1985 | shell_input_line = expansions; |
| 1986 | shell_input_line_len = shell_input_line ? |
| 1987 | strlen (shell_input_line) : 0; |
| 1988 | if (!shell_input_line_len) |
| 1989 | current_command_line_count--; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1990 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1991 | /* We have to force the xrealloc below because we don't know |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1992 | the true allocated size of shell_input_line anymore. */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1993 | shell_input_line_size = shell_input_line_len; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1994 | |
| 1995 | set_line_mbstate (); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1996 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1997 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1998 | /* Try to do something intelligent with blank lines encountered while |
| 1999 | entering multi-line commands. XXX - this is grotesque */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2000 | else if (remember_on_history && shell_input_line && |
| 2001 | shell_input_line[0] == '\0' && |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2002 | current_command_line_count > 1) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2003 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2004 | if (current_delimiter (dstack)) |
| 2005 | /* We know shell_input_line[0] == 0 and we're reading some sort of |
| 2006 | quoted string. This means we've got a line consisting of only |
| 2007 | a newline in a quoted string. We want to make sure this line |
| 2008 | gets added to the history. */ |
| 2009 | maybe_add_history (shell_input_line); |
| 2010 | else |
| 2011 | { |
| 2012 | char *hdcs; |
| 2013 | hdcs = history_delimiting_chars (); |
| 2014 | if (hdcs && hdcs[0] == ';') |
| 2015 | maybe_add_history (shell_input_line); |
| 2016 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2019 | #endif /* HISTORY */ |
| 2020 | |
| 2021 | if (shell_input_line) |
| 2022 | { |
| 2023 | /* Lines that signify the end of the shell's input should not be |
| 2024 | echoed. */ |
| 2025 | if (echo_input_at_read && (shell_input_line[0] || |
| 2026 | shell_input_line_terminator != EOF)) |
| 2027 | fprintf (stderr, "%s\n", shell_input_line); |
| 2028 | } |
| 2029 | else |
| 2030 | { |
| 2031 | shell_input_line_size = 0; |
| 2032 | prompt_string_pointer = ¤t_prompt_string; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2033 | if (SHOULD_PROMPT ()) |
| 2034 | prompt_again (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2035 | goto restart_read; |
| 2036 | } |
| 2037 | |
| 2038 | /* Add the newline to the end of this string, iff the string does |
| 2039 | not already end in an EOF character. */ |
| 2040 | if (shell_input_line_terminator != EOF) |
| 2041 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2042 | if (shell_input_line_len + 3 > shell_input_line_size) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2043 | shell_input_line = (char *)xrealloc (shell_input_line, |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2044 | 1 + (shell_input_line_size += 2)); |
| 2045 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2046 | shell_input_line[shell_input_line_len] = '\n'; |
| 2047 | shell_input_line[shell_input_line_len + 1] = '\0'; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2048 | |
| 2049 | set_line_mbstate (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2050 | } |
| 2051 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2052 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2053 | uc = shell_input_line[shell_input_line_index]; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2054 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2055 | if (uc) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2056 | shell_input_line_index++; |
| 2057 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2058 | if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n') |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2059 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2060 | if (SHOULD_PROMPT ()) |
| 2061 | prompt_again (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2062 | line_number++; |
| 2063 | goto restart_read; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2066 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2067 | /* If UC is NULL, we have reached the end of the current input string. If |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2068 | pushed_string_list is non-empty, it's time to pop to the previous string |
| 2069 | because we have fully consumed the result of the last alias expansion. |
| 2070 | Do it transparently; just return the next character of the string popped |
| 2071 | to. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2072 | if (!uc && (pushed_string_list != (STRING_SAVER *)NULL)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2073 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2074 | pop_string (); |
| 2075 | uc = shell_input_line[shell_input_line_index]; |
| 2076 | if (uc) |
| 2077 | shell_input_line_index++; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2078 | } |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2079 | #endif /* ALIAS || DPAREN_ARITHMETIC */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2080 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2081 | if (!uc && shell_input_line_terminator == EOF) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2082 | return ((shell_input_line_index != 0) ? '\n' : EOF); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2083 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2084 | return (uc); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2087 | /* Put C back into the input for the shell. This might need changes for |
| 2088 | HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a |
| 2089 | character different than we read, shell_input_line_property doesn't need |
| 2090 | to change when manipulating shell_input_line. The define for |
| 2091 | last_shell_getc_is_singlebyte should take care of it, though. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2092 | static void |
| 2093 | shell_ungetc (c) |
| 2094 | int c; |
| 2095 | { |
| 2096 | if (shell_input_line && shell_input_line_index) |
| 2097 | shell_input_line[--shell_input_line_index] = c; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2098 | else |
| 2099 | eol_ungetc_lookahead = c; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2102 | #ifdef INCLUDE_UNUSED |
| 2103 | /* Back the input pointer up by one, effectively `ungetting' a character. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2104 | static void |
| 2105 | shell_ungetchar () |
| 2106 | { |
| 2107 | if (shell_input_line && shell_input_line_index) |
| 2108 | shell_input_line_index--; |
| 2109 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2110 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2111 | |
| 2112 | /* Discard input until CHARACTER is seen, then push that character back |
| 2113 | onto the input stream. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2114 | static void |
| 2115 | discard_until (character) |
| 2116 | int character; |
| 2117 | { |
| 2118 | int c; |
| 2119 | |
| 2120 | while ((c = shell_getc (0)) != EOF && c != character) |
| 2121 | ; |
| 2122 | |
| 2123 | if (c != EOF) |
| 2124 | shell_ungetc (c); |
| 2125 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2126 | |
| 2127 | void |
| 2128 | execute_prompt_command (command) |
| 2129 | char *command; |
| 2130 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2131 | char *last_lastarg; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2132 | sh_parser_state_t ps; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2133 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2134 | save_parser_state (&ps); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2135 | last_lastarg = get_string_value ("_"); |
| 2136 | if (last_lastarg) |
| 2137 | last_lastarg = savestring (last_lastarg); |
| 2138 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2139 | parse_and_execute (savestring (command), "PROMPT_COMMAND", SEVAL_NONINT|SEVAL_NOHIST); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2140 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2141 | restore_parser_state (&ps); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2142 | bind_variable ("_", last_lastarg); |
| 2143 | FREE (last_lastarg); |
| 2144 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2145 | if (token_to_read == '\n') /* reset_parser was called */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2146 | token_to_read = 0; |
| 2147 | } |
| 2148 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2149 | /* Place to remember the token. We try to keep the buffer |
| 2150 | at a reasonable size, but it can grow. */ |
| 2151 | static char *token = (char *)NULL; |
| 2152 | |
| 2153 | /* Current size of the token buffer. */ |
| 2154 | static int token_buffer_size; |
| 2155 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2156 | /* Command to read_token () explaining what we want it to do. */ |
| 2157 | #define READ 0 |
| 2158 | #define RESET 1 |
| 2159 | #define prompt_is_ps1 \ |
| 2160 | (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt) |
| 2161 | |
| 2162 | /* Function for yyparse to call. yylex keeps track of |
| 2163 | the last two tokens read, and calls read_token. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2164 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2165 | yylex () |
| 2166 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2167 | if (interactive && (current_token == 0 || current_token == '\n')) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2168 | { |
| 2169 | /* Before we print a prompt, we might have to check mailboxes. |
| 2170 | We do this only if it is time to do so. Notice that only here |
| 2171 | is the mail alarm reset; nothing takes place in check_mail () |
| 2172 | except the checking of mail. Please don't change this. */ |
| 2173 | if (prompt_is_ps1 && time_to_check_mail ()) |
| 2174 | { |
| 2175 | check_mail (); |
| 2176 | reset_mail_timer (); |
| 2177 | } |
| 2178 | |
| 2179 | /* Avoid printing a prompt if we're not going to read anything, e.g. |
| 2180 | after resetting the parser with read_token (RESET). */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2181 | if (token_to_read == 0 && SHOULD_PROMPT ()) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2182 | prompt_again (); |
| 2183 | } |
| 2184 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2185 | two_tokens_ago = token_before_that; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2186 | token_before_that = last_read_token; |
| 2187 | last_read_token = current_token; |
| 2188 | current_token = read_token (READ); |
| 2189 | return (current_token); |
| 2190 | } |
| 2191 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2192 | /* When non-zero, we have read the required tokens |
| 2193 | which allow ESAC to be the next one read. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2194 | static int esacs_needed_count; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2195 | |
| 2196 | void |
| 2197 | gather_here_documents () |
| 2198 | { |
| 2199 | int r = 0; |
| 2200 | while (need_here_doc) |
| 2201 | { |
| 2202 | make_here_document (redir_stack[r++]); |
| 2203 | need_here_doc--; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2207 | /* When non-zero, an open-brace used to create a group is awaiting a close |
| 2208 | brace partner. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2209 | static int open_brace_count; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2210 | |
| 2211 | #define command_token_position(token) \ |
| 2212 | (((token) == ASSIGNMENT_WORD) || \ |
| 2213 | ((token) != SEMI_SEMI && reserved_word_acceptable(token))) |
| 2214 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2215 | #define assignment_acceptable(token) \ |
| 2216 | (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2217 | |
| 2218 | /* Check to see if TOKEN is a reserved word and return the token |
| 2219 | value if it is. */ |
| 2220 | #define CHECK_FOR_RESERVED_WORD(tok) \ |
| 2221 | do { \ |
| 2222 | if (!dollar_present && !quoted && \ |
| 2223 | reserved_word_acceptable (last_read_token)) \ |
| 2224 | { \ |
| 2225 | int i; \ |
| 2226 | for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \ |
| 2227 | if (STREQ (tok, word_token_alist[i].word)) \ |
| 2228 | { \ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2229 | if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2230 | break; \ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2231 | if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2232 | break; \ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2233 | if (word_token_alist[i].token == ESAC) \ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2234 | parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \ |
| 2235 | else if (word_token_alist[i].token == CASE) \ |
| 2236 | parser_state |= PST_CASESTMT; \ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2237 | else if (word_token_alist[i].token == COND_END) \ |
| 2238 | parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \ |
| 2239 | else if (word_token_alist[i].token == COND_START) \ |
| 2240 | parser_state |= PST_CONDCMD; \ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2241 | else if (word_token_alist[i].token == '{') \ |
| 2242 | open_brace_count++; \ |
| 2243 | else if (word_token_alist[i].token == '}' && open_brace_count) \ |
| 2244 | open_brace_count--; \ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2245 | return (word_token_alist[i].token); \ |
| 2246 | } \ |
| 2247 | } \ |
| 2248 | } while (0) |
| 2249 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2250 | #if defined (ALIAS) |
| 2251 | |
| 2252 | /* OK, we have a token. Let's try to alias expand it, if (and only if) |
| 2253 | it's eligible. |
| 2254 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2255 | It is eligible for expansion if EXPAND_ALIASES is set, and |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2256 | the token is unquoted and the last token read was a command |
| 2257 | separator (or expand_next_token is set), and we are currently |
| 2258 | processing an alias (pushed_string_list is non-empty) and this |
| 2259 | token is not the same as the current or any previously |
| 2260 | processed alias. |
| 2261 | |
| 2262 | Special cases that disqualify: |
| 2263 | In a pattern list in a case statement (parser_state & PST_CASEPAT). */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2264 | |
| 2265 | static char * |
| 2266 | mk_alexpansion (s) |
| 2267 | char *s; |
| 2268 | { |
| 2269 | int l; |
| 2270 | char *r; |
| 2271 | |
| 2272 | l = strlen (s); |
| 2273 | r = xmalloc (l + 2); |
| 2274 | strcpy (r, s); |
| 2275 | if (r[l -1] != ' ') |
| 2276 | r[l++] = ' '; |
| 2277 | r[l] = '\0'; |
| 2278 | return r; |
| 2279 | } |
| 2280 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2281 | static int |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2282 | alias_expand_token (tokstr) |
| 2283 | char *tokstr; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2284 | { |
| 2285 | char *expanded; |
| 2286 | alias_t *ap; |
| 2287 | |
| 2288 | if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) && |
| 2289 | (parser_state & PST_CASEPAT) == 0) |
| 2290 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2291 | ap = find_alias (tokstr); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2292 | |
| 2293 | /* Currently expanding this token. */ |
| 2294 | if (ap && (ap->flags & AL_BEINGEXPANDED)) |
| 2295 | return (NO_EXPANSION); |
| 2296 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2297 | /* mk_alexpansion puts an extra space on the end of the alias expansion, |
| 2298 | so the lookahead by the parser works right. If this gets changed, |
| 2299 | make sure the code in shell_getc that deals with reaching the end of |
| 2300 | an expanded alias is changed with it. */ |
| 2301 | expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL; |
| 2302 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2303 | if (expanded) |
| 2304 | { |
| 2305 | push_string (expanded, ap->flags & AL_EXPANDNEXT, ap); |
| 2306 | return (RE_READ_TOKEN); |
| 2307 | } |
| 2308 | else |
| 2309 | /* This is an eligible token that does not have an expansion. */ |
| 2310 | return (NO_EXPANSION); |
| 2311 | } |
| 2312 | return (NO_EXPANSION); |
| 2313 | } |
| 2314 | #endif /* ALIAS */ |
| 2315 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2316 | static int |
| 2317 | time_command_acceptable () |
| 2318 | { |
| 2319 | #if defined (COMMAND_TIMING) |
| 2320 | switch (last_read_token) |
| 2321 | { |
| 2322 | case 0: |
| 2323 | case ';': |
| 2324 | case '\n': |
| 2325 | case AND_AND: |
| 2326 | case OR_OR: |
| 2327 | case '&': |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2328 | case DO: |
| 2329 | case THEN: |
| 2330 | case ELSE: |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2331 | case '{': /* } */ |
| 2332 | case '(': /* ) */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2333 | return 1; |
| 2334 | default: |
| 2335 | return 0; |
| 2336 | } |
| 2337 | #else |
| 2338 | return 0; |
| 2339 | #endif /* COMMAND_TIMING */ |
| 2340 | } |
| 2341 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2342 | /* Handle special cases of token recognition: |
| 2343 | IN is recognized if the last token was WORD and the token |
| 2344 | before that was FOR or CASE or SELECT. |
| 2345 | |
| 2346 | DO is recognized if the last token was WORD and the token |
| 2347 | before that was FOR or SELECT. |
| 2348 | |
| 2349 | ESAC is recognized if the last token caused `esacs_needed_count' |
| 2350 | to be set |
| 2351 | |
| 2352 | `{' is recognized if the last token as WORD and the token |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2353 | before that was FUNCTION, or if we just parsed an arithmetic |
| 2354 | `for' command. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2355 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2356 | `}' is recognized if there is an unclosed `{' present. |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2357 | |
| 2358 | `-p' is returned as TIMEOPT if the last read token was TIME. |
| 2359 | |
| 2360 | ']]' is returned as COND_END if the parser is currently parsing |
| 2361 | a conditional expression ((parser_state & PST_CONDEXPR) != 0) |
| 2362 | |
| 2363 | `time' is returned as TIME if and only if it is immediately |
| 2364 | preceded by one of `;', `\n', `||', `&&', or `&'. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2365 | */ |
| 2366 | |
| 2367 | static int |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2368 | special_case_tokens (tokstr) |
| 2369 | char *tokstr; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2370 | { |
| 2371 | if ((last_read_token == WORD) && |
| 2372 | #if defined (SELECT_COMMAND) |
| 2373 | ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) && |
| 2374 | #else |
| 2375 | ((token_before_that == FOR) || (token_before_that == CASE)) && |
| 2376 | #endif |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2377 | (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2378 | { |
| 2379 | if (token_before_that == CASE) |
| 2380 | { |
| 2381 | parser_state |= PST_CASEPAT; |
| 2382 | esacs_needed_count++; |
| 2383 | } |
| 2384 | return (IN); |
| 2385 | } |
| 2386 | |
| 2387 | if (last_read_token == WORD && |
| 2388 | #if defined (SELECT_COMMAND) |
| 2389 | (token_before_that == FOR || token_before_that == SELECT) && |
| 2390 | #else |
| 2391 | (token_before_that == FOR) && |
| 2392 | #endif |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2393 | (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0')) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2394 | return (DO); |
| 2395 | |
| 2396 | /* Ditto for ESAC in the CASE case. |
| 2397 | Specifically, this handles "case word in esac", which is a legal |
| 2398 | construct, certainly because someone will pass an empty arg to the |
| 2399 | case construct, and we don't want it to barf. Of course, we should |
| 2400 | insist that the case construct has at least one pattern in it, but |
| 2401 | the designers disagree. */ |
| 2402 | if (esacs_needed_count) |
| 2403 | { |
| 2404 | esacs_needed_count--; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2405 | if (STREQ (tokstr, "esac")) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2406 | { |
| 2407 | parser_state &= ~PST_CASEPAT; |
| 2408 | return (ESAC); |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | /* The start of a shell function definition. */ |
| 2413 | if (parser_state & PST_ALLOWOPNBRC) |
| 2414 | { |
| 2415 | parser_state &= ~PST_ALLOWOPNBRC; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2416 | if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2417 | { |
| 2418 | open_brace_count++; |
| 2419 | function_bstart = line_number; |
| 2420 | return ('{'); /* } */ |
| 2421 | } |
| 2422 | } |
| 2423 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2424 | /* We allow a `do' after a for ((...)) without an intervening |
| 2425 | list_terminator */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2426 | if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2]) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2427 | return (DO); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2428 | if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2429 | { |
| 2430 | open_brace_count++; |
| 2431 | return ('{'); /* } */ |
| 2432 | } |
| 2433 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2434 | if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1]) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2435 | { |
| 2436 | open_brace_count--; /* { */ |
| 2437 | return ('}'); |
| 2438 | } |
| 2439 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2440 | #if defined (COMMAND_TIMING) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2441 | /* Handle -p after `time'. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2442 | if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2]) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2443 | return (TIMEOPT); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2444 | #endif |
| 2445 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2446 | #if 0 |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2447 | #if defined (COMMAND_TIMING) |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2448 | if (STREQ (token, "time") && ((parser_state & PST_CASEPAT) == 0) && time_command_acceptable ()) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2449 | return (TIME); |
| 2450 | #endif /* COMMAND_TIMING */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2451 | #endif |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2452 | |
| 2453 | #if defined (COND_COMMAND) /* [[ */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2454 | if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0') |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2455 | return (COND_END); |
| 2456 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2457 | |
| 2458 | return (-1); |
| 2459 | } |
| 2460 | |
| 2461 | /* Called from shell.c when Control-C is typed at top level. Or |
| 2462 | by the error rule at top level. */ |
| 2463 | void |
| 2464 | reset_parser () |
| 2465 | { |
| 2466 | dstack.delimiter_depth = 0; /* No delimiters found so far. */ |
| 2467 | open_brace_count = 0; |
| 2468 | |
| 2469 | parser_state = 0; |
| 2470 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2471 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2472 | if (pushed_string_list) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2473 | free_string_list (); |
| 2474 | #endif /* ALIAS || DPAREN_ARITHMETIC */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2475 | |
| 2476 | if (shell_input_line) |
| 2477 | { |
| 2478 | free (shell_input_line); |
| 2479 | shell_input_line = (char *)NULL; |
| 2480 | shell_input_line_size = shell_input_line_index = 0; |
| 2481 | } |
| 2482 | |
| 2483 | FREE (word_desc_to_read); |
| 2484 | word_desc_to_read = (WORD_DESC *)NULL; |
| 2485 | |
| 2486 | last_read_token = '\n'; |
| 2487 | token_to_read = '\n'; |
| 2488 | } |
| 2489 | |
| 2490 | /* Read the next token. Command can be READ (normal operation) or |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2491 | RESET (to normalize state). */ |
| 2492 | static int |
| 2493 | read_token (command) |
| 2494 | int command; |
| 2495 | { |
| 2496 | int character; /* Current character. */ |
| 2497 | int peek_char; /* Temporary look-ahead character. */ |
| 2498 | int result; /* The thing to return. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2499 | |
| 2500 | if (command == RESET) |
| 2501 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2502 | reset_parser (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2503 | return ('\n'); |
| 2504 | } |
| 2505 | |
| 2506 | if (token_to_read) |
| 2507 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2508 | result = token_to_read; |
| 2509 | if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2510 | { |
| 2511 | yylval.word = word_desc_to_read; |
| 2512 | word_desc_to_read = (WORD_DESC *)NULL; |
| 2513 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2514 | token_to_read = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2515 | return (result); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2518 | #if defined (COND_COMMAND) |
| 2519 | if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD) |
| 2520 | { |
| 2521 | cond_lineno = line_number; |
| 2522 | parser_state |= PST_CONDEXPR; |
| 2523 | yylval.command = parse_cond_command (); |
| 2524 | if (cond_token != COND_END) |
| 2525 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2526 | cond_error (); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2527 | return (-1); |
| 2528 | } |
| 2529 | token_to_read = COND_END; |
| 2530 | parser_state &= ~(PST_CONDEXPR|PST_CONDCMD); |
| 2531 | return (COND_CMD); |
| 2532 | } |
| 2533 | #endif |
| 2534 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2535 | #if defined (ALIAS) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2536 | /* This is a place to jump back to once we have successfully expanded a |
| 2537 | token with an alias and pushed the string with push_string () */ |
| 2538 | re_read_token: |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2539 | #endif /* ALIAS */ |
| 2540 | |
| 2541 | /* Read a single word from input. Start by skipping blanks. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2542 | while ((character = shell_getc (1)) != EOF && whitespace (character)) |
| 2543 | ; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2544 | |
| 2545 | if (character == EOF) |
| 2546 | { |
| 2547 | EOF_Reached = 1; |
| 2548 | return (yacc_EOF); |
| 2549 | } |
| 2550 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2551 | if MBTEST(character == '#' && (!interactive || interactive_comments)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2552 | { |
| 2553 | /* A comment. Discard until EOL or EOF, and then return a newline. */ |
| 2554 | discard_until ('\n'); |
| 2555 | shell_getc (0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2556 | character = '\n'; /* this will take the next if statement and return. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | if (character == '\n') |
| 2560 | { |
| 2561 | /* If we're about to return an unquoted newline, we can go and collect |
| 2562 | the text of any pending here document. */ |
| 2563 | if (need_here_doc) |
| 2564 | gather_here_documents (); |
| 2565 | |
| 2566 | #if defined (ALIAS) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2567 | parser_state &= ~PST_ALEXPNEXT; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2568 | #endif /* ALIAS */ |
| 2569 | |
| 2570 | return (character); |
| 2571 | } |
| 2572 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2573 | /* Shell meta-characters. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2574 | if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2575 | { |
| 2576 | #if defined (ALIAS) |
| 2577 | /* Turn off alias tokenization iff this character sequence would |
| 2578 | not leave us ready to read a command. */ |
| 2579 | if (character == '<' || character == '>') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2580 | parser_state &= ~PST_ALEXPNEXT; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2581 | #endif /* ALIAS */ |
| 2582 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2583 | peek_char = shell_getc (1); |
| 2584 | if (character == peek_char) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2585 | { |
| 2586 | switch (character) |
| 2587 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2588 | case '<': |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2589 | /* If '<' then we could be at "<<" or at "<<-". We have to |
| 2590 | look ahead one more character. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2591 | peek_char = shell_getc (1); |
| 2592 | if (peek_char == '-') |
| 2593 | return (LESS_LESS_MINUS); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2594 | else if (peek_char == '<') |
| 2595 | return (LESS_LESS_LESS); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2596 | else |
| 2597 | { |
| 2598 | shell_ungetc (peek_char); |
| 2599 | return (LESS_LESS); |
| 2600 | } |
| 2601 | |
| 2602 | case '>': |
| 2603 | return (GREATER_GREATER); |
| 2604 | |
| 2605 | case ';': |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2606 | parser_state |= PST_CASEPAT; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2607 | #if defined (ALIAS) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2608 | parser_state &= ~PST_ALEXPNEXT; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2609 | #endif /* ALIAS */ |
| 2610 | return (SEMI_SEMI); |
| 2611 | |
| 2612 | case '&': |
| 2613 | return (AND_AND); |
| 2614 | |
| 2615 | case '|': |
| 2616 | return (OR_OR); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2617 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2618 | #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2619 | case '(': /* ) */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2620 | result = parse_dparen (character); |
| 2621 | if (result == -2) |
| 2622 | break; |
| 2623 | else |
| 2624 | return result; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2625 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2626 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2627 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2628 | else if MBTEST(character == '<' && peek_char == '&') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2629 | return (LESS_AND); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2630 | else if MBTEST(character == '>' && peek_char == '&') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2631 | return (GREATER_AND); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2632 | else if MBTEST(character == '<' && peek_char == '>') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2633 | return (LESS_GREATER); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2634 | else if MBTEST(character == '>' && peek_char == '|') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2635 | return (GREATER_BAR); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2636 | else if MBTEST(peek_char == '>' && character == '&') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2637 | return (AND_GREATER); |
| 2638 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2639 | shell_ungetc (peek_char); |
| 2640 | |
| 2641 | /* If we look like we are reading the start of a function |
| 2642 | definition, then let the reader know about it so that |
| 2643 | we will do the right thing with `{'. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2644 | if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2645 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2646 | parser_state |= PST_ALLOWOPNBRC; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2647 | #if defined (ALIAS) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2648 | parser_state &= ~PST_ALEXPNEXT; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2649 | #endif /* ALIAS */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2650 | function_dstart = line_number; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2653 | /* case pattern lists may be preceded by an optional left paren. If |
| 2654 | we're not trying to parse a case pattern list, the left paren |
| 2655 | indicates a subshell. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2656 | if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2657 | parser_state |= PST_SUBSHELL; |
| 2658 | /*(*/ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2659 | else if MBTEST((parser_state & PST_CASEPAT) && character == ')') |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2660 | parser_state &= ~PST_CASEPAT; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2661 | /*(*/ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2662 | else if MBTEST((parser_state & PST_SUBSHELL) && character == ')') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2663 | parser_state &= ~PST_SUBSHELL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2664 | |
| 2665 | #if defined (PROCESS_SUBSTITUTION) |
| 2666 | /* Check for the constructs which introduce process substitution. |
| 2667 | Shells running in `posix mode' don't do process substitution. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2668 | if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2669 | #endif /* PROCESS_SUBSTITUTION */ |
| 2670 | return (character); |
| 2671 | } |
| 2672 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2673 | /* Hack <&- (close stdin) case. Also <&N- (dup and close). */ |
| 2674 | if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2675 | return (character); |
| 2676 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2677 | /* Okay, if we got this far, we have to read a word. Read one, |
| 2678 | and then check it against the known ones. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2679 | result = read_token_word (character); |
| 2680 | #if defined (ALIAS) |
| 2681 | if (result == RE_READ_TOKEN) |
| 2682 | goto re_read_token; |
| 2683 | #endif |
| 2684 | return result; |
| 2685 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2686 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2687 | /* |
| 2688 | * Match a $(...) or other grouping construct. This has to handle embedded |
| 2689 | * quoted strings ('', ``, "") and nested constructs. It also must handle |
| 2690 | * reprompting the user, if necessary, after reading a newline (unless the |
| 2691 | * P_NONL flag is passed), and returning correct error values if it reads |
| 2692 | * EOF. |
| 2693 | */ |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2694 | #define P_FIRSTCLOSE 0x01 |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2695 | #define P_ALLOWESC 0x02 |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2696 | #define P_DQUOTE 0x04 |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2697 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2698 | static char matched_pair_error; |
| 2699 | static char * |
| 2700 | parse_matched_pair (qc, open, close, lenp, flags) |
| 2701 | int qc; /* `"' if this construct is within double quotes */ |
| 2702 | int open, close; |
| 2703 | int *lenp, flags; |
| 2704 | { |
| 2705 | int count, ch, was_dollar; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2706 | int pass_next_character, nestlen, ttranslen, start_lineno; |
| 2707 | char *ret, *nestret, *ttrans; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2708 | int retind, retsize, rflags; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2709 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2710 | count = 1; |
| 2711 | pass_next_character = was_dollar = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2712 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2713 | /* RFLAGS is the set of flags we want to pass to recursive calls. */ |
| 2714 | rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE); |
| 2715 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2716 | ret = (char *)xmalloc (retsize = 64); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2717 | retind = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2718 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2719 | start_lineno = line_number; |
| 2720 | while (count) |
| 2721 | { |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2722 | ch = shell_getc ((qc != '\'' || (flags & P_ALLOWESC)) && pass_next_character == 0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2723 | if (ch == EOF) |
| 2724 | { |
| 2725 | free (ret); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2726 | parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2727 | EOF_Reached = 1; /* XXX */ |
| 2728 | return (&matched_pair_error); |
| 2729 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2730 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2731 | /* Possible reprompting. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2732 | if (ch == '\n' && SHOULD_PROMPT ()) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2733 | prompt_again (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2734 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2735 | if (pass_next_character) /* last char was backslash */ |
| 2736 | { |
| 2737 | pass_next_character = 0; |
| 2738 | if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */ |
| 2739 | { |
| 2740 | if (retind > 0) retind--; /* swallow previously-added backslash */ |
| 2741 | continue; |
| 2742 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2743 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2744 | RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2745 | if MBTEST(ch == CTLESC || ch == CTLNUL) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2746 | ret[retind++] = CTLESC; |
| 2747 | ret[retind++] = ch; |
| 2748 | continue; |
| 2749 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2750 | else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2751 | { |
| 2752 | RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64); |
| 2753 | ret[retind++] = CTLESC; |
| 2754 | ret[retind++] = ch; |
| 2755 | continue; |
| 2756 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2757 | else if MBTEST(ch == close) /* ending delimiter */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2758 | count--; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2759 | #if 1 |
| 2760 | /* handle nested ${...} specially. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2761 | else if MBTEST(open != close && was_dollar && open == '{' && ch == open) /* } */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2762 | count++; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2763 | #endif |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2764 | else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2765 | count++; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2766 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2767 | /* Add this character. */ |
| 2768 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 2769 | ret[retind++] = ch; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2770 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2771 | if (open == '\'') /* '' inside grouping construct */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2772 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2773 | if MBTEST((flags & P_ALLOWESC) && ch == '\\') |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2774 | pass_next_character++; |
| 2775 | continue; |
| 2776 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2777 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2778 | if MBTEST(ch == '\\') /* backslashes */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2779 | pass_next_character++; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2780 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2781 | if (open != close) /* a grouping construct */ |
| 2782 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2783 | if MBTEST(shellquote (ch)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2784 | { |
| 2785 | /* '', ``, or "" inside $(...) or other grouping construct. */ |
| 2786 | push_delimiter (dstack, ch); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2787 | if MBTEST(was_dollar && ch == '\'') /* $'...' inside group */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2788 | nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2789 | else |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2790 | nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2791 | pop_delimiter (dstack); |
| 2792 | if (nestret == &matched_pair_error) |
| 2793 | { |
| 2794 | free (ret); |
| 2795 | return &matched_pair_error; |
| 2796 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2797 | if MBTEST(was_dollar && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0)) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2798 | { |
| 2799 | /* Translate $'...' here. */ |
| 2800 | ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2801 | xfree (nestret); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2802 | nestret = sh_single_quote (ttrans); |
| 2803 | free (ttrans); |
| 2804 | nestlen = strlen (nestret); |
| 2805 | retind -= 2; /* back up before the $' */ |
| 2806 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2807 | else if MBTEST(was_dollar && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0)) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2808 | { |
| 2809 | /* Locale expand $"..." here. */ |
| 2810 | ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2811 | xfree (nestret); |
| 2812 | nestret = (char *)xmalloc (ttranslen + 3); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2813 | nestret[0] = '"'; |
| 2814 | strcpy (nestret + 1, ttrans); |
| 2815 | nestret[ttranslen + 1] = '"'; |
| 2816 | nestret[ttranslen += 2] = '\0'; |
| 2817 | free (ttrans); |
| 2818 | nestlen = ttranslen; |
| 2819 | retind -= 2; /* back up before the $" */ |
| 2820 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2821 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2822 | if (nestlen) |
| 2823 | { |
| 2824 | RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); |
| 2825 | strcpy (ret + retind, nestret); |
| 2826 | retind += nestlen; |
| 2827 | } |
| 2828 | FREE (nestret); |
| 2829 | } |
| 2830 | } |
| 2831 | /* Parse an old-style command substitution within double quotes as a |
| 2832 | single word. */ |
| 2833 | /* XXX - sh and ksh93 don't do this - XXX */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2834 | else if MBTEST(open == '"' && ch == '`') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2835 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2836 | nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2837 | if (nestret == &matched_pair_error) |
| 2838 | { |
| 2839 | free (ret); |
| 2840 | return &matched_pair_error; |
| 2841 | } |
| 2842 | if (nestlen) |
| 2843 | { |
| 2844 | RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); |
| 2845 | strcpy (ret + retind, nestret); |
| 2846 | retind += nestlen; |
| 2847 | } |
| 2848 | FREE (nestret); |
| 2849 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2850 | else if MBTEST(was_dollar && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2851 | /* check for $(), $[], or ${} inside quoted string. */ |
| 2852 | { |
| 2853 | if (open == ch) /* undo previous increment */ |
| 2854 | count--; |
| 2855 | if (ch == '(') /* ) */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2856 | nestret = parse_matched_pair (0, '(', ')', &nestlen, rflags); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2857 | else if (ch == '{') /* } */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2858 | nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|rflags); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2859 | else if (ch == '[') /* ] */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2860 | nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2861 | if (nestret == &matched_pair_error) |
| 2862 | { |
| 2863 | free (ret); |
| 2864 | return &matched_pair_error; |
| 2865 | } |
| 2866 | if (nestlen) |
| 2867 | { |
| 2868 | RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); |
| 2869 | strcpy (ret + retind, nestret); |
| 2870 | retind += nestlen; |
| 2871 | } |
| 2872 | FREE (nestret); |
| 2873 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2874 | was_dollar = MBTEST(ch == '$'); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2875 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2876 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2877 | ret[retind] = '\0'; |
| 2878 | if (lenp) |
| 2879 | *lenp = retind; |
| 2880 | return ret; |
| 2881 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2882 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2883 | #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2884 | /* Parse a double-paren construct. It can be either an arithmetic |
| 2885 | command, an arithmetic `for' command, or a nested subshell. Returns |
| 2886 | the parsed token, -1 on error, or -2 if we didn't do anything and |
| 2887 | should just go on. */ |
| 2888 | static int |
| 2889 | parse_dparen (c) |
| 2890 | int c; |
| 2891 | { |
| 2892 | int cmdtyp, len, sline; |
| 2893 | char *wval, *wv2; |
| 2894 | WORD_DESC *wd; |
| 2895 | |
| 2896 | #if defined (ARITH_FOR_COMMAND) |
| 2897 | if (last_read_token == FOR) |
| 2898 | { |
| 2899 | arith_for_lineno = line_number; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2900 | cmdtyp = parse_arith_cmd (&wval, 0); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2901 | if (cmdtyp == 1) |
| 2902 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2903 | wd = make_word (wval); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2904 | yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL); |
| 2905 | free (wval); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2906 | return (ARITH_FOR_EXPRS); |
| 2907 | } |
| 2908 | else |
| 2909 | return -1; /* ERROR */ |
| 2910 | } |
| 2911 | #endif |
| 2912 | |
| 2913 | #if defined (DPAREN_ARITHMETIC) |
| 2914 | if (reserved_word_acceptable (last_read_token)) |
| 2915 | { |
| 2916 | sline = line_number; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2917 | #if 0 |
| 2918 | cmdtyp = parse_arith_cmd (&wval, 1); |
| 2919 | #else |
| 2920 | cmdtyp = parse_arith_cmd (&wval, 0); |
| 2921 | #endif |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2922 | if (cmdtyp == 1) /* arithmetic command */ |
| 2923 | { |
| 2924 | wd = make_word (wval); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2925 | #if 0 |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2926 | wd->flags = W_QUOTED; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2927 | #else |
| 2928 | wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB; |
| 2929 | #endif |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2930 | yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL); |
| 2931 | free (wval); /* make_word copies it */ |
| 2932 | return (ARITH_CMD); |
| 2933 | } |
| 2934 | else if (cmdtyp == 0) /* nested subshell */ |
| 2935 | { |
| 2936 | push_string (wval, 0, (alias_t *)NULL); |
| 2937 | if ((parser_state & PST_CASEPAT) == 0) |
| 2938 | parser_state |= PST_SUBSHELL; |
| 2939 | return (c); |
| 2940 | } |
| 2941 | else /* ERROR */ |
| 2942 | return -1; |
| 2943 | } |
| 2944 | #endif |
| 2945 | |
| 2946 | return -2; /* XXX */ |
| 2947 | } |
| 2948 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2949 | /* We've seen a `(('. Look for the matching `))'. If we get it, return 1. |
| 2950 | If not, assume it's a nested subshell for backwards compatibility and |
| 2951 | return 0. In any case, put the characters we've consumed into a locally- |
| 2952 | allocated buffer and make *ep point to that buffer. Return -1 on an |
| 2953 | error, for example EOF. */ |
| 2954 | static int |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2955 | parse_arith_cmd (ep, adddq) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2956 | char **ep; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2957 | int adddq; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2958 | { |
| 2959 | int exp_lineno, rval, c; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2960 | char *ttok, *tokstr; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2961 | int ttoklen; |
| 2962 | |
| 2963 | exp_lineno = line_number; |
| 2964 | ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0); |
| 2965 | rval = 1; |
| 2966 | if (ttok == &matched_pair_error) |
| 2967 | return -1; |
| 2968 | /* Check that the next character is the closing right paren. If |
| 2969 | not, this is a syntax error. ( */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2970 | c = shell_getc (0); |
| 2971 | if MBTEST(c != ')') |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2972 | rval = 0; |
| 2973 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2974 | tokstr = (char *)xmalloc (ttoklen + 4); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2975 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2976 | /* if ADDDQ != 0 then (( ... )) -> "..." */ |
| 2977 | if (rval == 1 && adddq) /* arith cmd, add double quotes */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2978 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2979 | tokstr[0] = '"'; |
| 2980 | strncpy (tokstr + 1, ttok, ttoklen - 1); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2981 | tokstr[ttoklen] = '"'; |
| 2982 | tokstr[ttoklen+1] = '\0'; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2983 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2984 | else if (rval == 1) /* arith cmd, don't add double quotes */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2985 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2986 | strncpy (tokstr, ttok, ttoklen - 1); |
| 2987 | tokstr[ttoklen-1] = '\0'; |
| 2988 | } |
| 2989 | else /* nested subshell */ |
| 2990 | { |
| 2991 | tokstr[0] = '('; |
| 2992 | strncpy (tokstr + 1, ttok, ttoklen - 1); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2993 | tokstr[ttoklen] = ')'; |
| 2994 | tokstr[ttoklen+1] = c; |
| 2995 | tokstr[ttoklen+2] = '\0'; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2996 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 2997 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2998 | *ep = tokstr; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2999 | FREE (ttok); |
| 3000 | return rval; |
| 3001 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3002 | #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3003 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3004 | #if defined (COND_COMMAND) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3005 | static void |
| 3006 | cond_error () |
| 3007 | { |
| 3008 | char *etext; |
| 3009 | |
| 3010 | if (EOF_Reached && cond_token != COND_ERROR) /* [[ */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3011 | parser_error (cond_lineno, _("unexpected EOF while looking for `]]'")); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3012 | else if (cond_token != COND_ERROR) |
| 3013 | { |
| 3014 | if (etext = error_token_from_token (cond_token)) |
| 3015 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3016 | parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3017 | free (etext); |
| 3018 | } |
| 3019 | else |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3020 | parser_error (cond_lineno, _("syntax error in conditional expression")); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3021 | } |
| 3022 | } |
| 3023 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3024 | static COND_COM * |
| 3025 | cond_expr () |
| 3026 | { |
| 3027 | return (cond_or ()); |
| 3028 | } |
| 3029 | |
| 3030 | static COND_COM * |
| 3031 | cond_or () |
| 3032 | { |
| 3033 | COND_COM *l, *r; |
| 3034 | |
| 3035 | l = cond_and (); |
| 3036 | if (cond_token == OR_OR) |
| 3037 | { |
| 3038 | r = cond_or (); |
| 3039 | l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r); |
| 3040 | } |
| 3041 | return l; |
| 3042 | } |
| 3043 | |
| 3044 | static COND_COM * |
| 3045 | cond_and () |
| 3046 | { |
| 3047 | COND_COM *l, *r; |
| 3048 | |
| 3049 | l = cond_term (); |
| 3050 | if (cond_token == AND_AND) |
| 3051 | { |
| 3052 | r = cond_and (); |
| 3053 | l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r); |
| 3054 | } |
| 3055 | return l; |
| 3056 | } |
| 3057 | |
| 3058 | static int |
| 3059 | cond_skip_newlines () |
| 3060 | { |
| 3061 | while ((cond_token = read_token (READ)) == '\n') |
| 3062 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3063 | if (SHOULD_PROMPT ()) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3064 | prompt_again (); |
| 3065 | } |
| 3066 | return (cond_token); |
| 3067 | } |
| 3068 | |
| 3069 | #define COND_RETURN_ERROR() \ |
| 3070 | do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0) |
| 3071 | |
| 3072 | static COND_COM * |
| 3073 | cond_term () |
| 3074 | { |
| 3075 | WORD_DESC *op; |
| 3076 | COND_COM *term, *tleft, *tright; |
| 3077 | int tok, lineno; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3078 | char *etext; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3079 | |
| 3080 | /* Read a token. It can be a left paren, a `!', a unary operator, or a |
| 3081 | word that should be the first argument of a binary operator. Start by |
| 3082 | skipping newlines, since this is a compound command. */ |
| 3083 | tok = cond_skip_newlines (); |
| 3084 | lineno = line_number; |
| 3085 | if (tok == COND_END) |
| 3086 | { |
| 3087 | COND_RETURN_ERROR (); |
| 3088 | } |
| 3089 | else if (tok == '(') |
| 3090 | { |
| 3091 | term = cond_expr (); |
| 3092 | if (cond_token != ')') |
| 3093 | { |
| 3094 | if (term) |
| 3095 | dispose_cond_node (term); /* ( */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3096 | if (etext = error_token_from_token (cond_token)) |
| 3097 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3098 | parser_error (lineno, _("unexpected token `%s', expected `)'"), etext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3099 | free (etext); |
| 3100 | } |
| 3101 | else |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3102 | parser_error (lineno, _("expected `)'")); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3103 | COND_RETURN_ERROR (); |
| 3104 | } |
| 3105 | term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL); |
| 3106 | (void)cond_skip_newlines (); |
| 3107 | } |
| 3108 | else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0'))) |
| 3109 | { |
| 3110 | if (tok == WORD) |
| 3111 | dispose_word (yylval.word); /* not needed */ |
| 3112 | term = cond_term (); |
| 3113 | if (term) |
| 3114 | term->flags |= CMD_INVERT_RETURN; |
| 3115 | } |
| 3116 | else if (tok == WORD && test_unop (yylval.word->word)) |
| 3117 | { |
| 3118 | op = yylval.word; |
| 3119 | tok = read_token (READ); |
| 3120 | if (tok == WORD) |
| 3121 | { |
| 3122 | tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL); |
| 3123 | term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL); |
| 3124 | } |
| 3125 | else |
| 3126 | { |
| 3127 | dispose_word (op); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3128 | if (etext = error_token_from_token (tok)) |
| 3129 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3130 | parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3131 | free (etext); |
| 3132 | } |
| 3133 | else |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3134 | parser_error (line_number, _("unexpected argument to conditional unary operator")); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3135 | COND_RETURN_ERROR (); |
| 3136 | } |
| 3137 | |
| 3138 | (void)cond_skip_newlines (); |
| 3139 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3140 | else if (tok == WORD) /* left argument to binary operator */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3141 | { |
| 3142 | /* lhs */ |
| 3143 | tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL); |
| 3144 | |
| 3145 | /* binop */ |
| 3146 | tok = read_token (READ); |
| 3147 | if (tok == WORD && test_binop (yylval.word->word)) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3148 | op = yylval.word; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3149 | #if defined (COND_REGEXP) |
| 3150 | else if (tok == WORD && STREQ (yylval.word->word,"=~")) |
| 3151 | op = yylval.word; |
| 3152 | #endif |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3153 | else if (tok == '<' || tok == '>') |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3154 | op = make_word_from_token (tok); /* ( */ |
| 3155 | /* There should be a check before blindly accepting the `)' that we have |
| 3156 | seen the opening `('. */ |
| 3157 | else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')') |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3158 | { |
| 3159 | /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like |
| 3160 | the test command. Similarly for [[ x && expr ]] or |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3161 | [[ x || expr ]] or [[ (x) ]]. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3162 | op = make_word ("-n"); |
| 3163 | term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL); |
| 3164 | cond_token = tok; |
| 3165 | return (term); |
| 3166 | } |
| 3167 | else |
| 3168 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3169 | if (etext = error_token_from_token (tok)) |
| 3170 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3171 | parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3172 | free (etext); |
| 3173 | } |
| 3174 | else |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3175 | parser_error (line_number, _("conditional binary operator expected")); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3176 | dispose_cond_node (tleft); |
| 3177 | COND_RETURN_ERROR (); |
| 3178 | } |
| 3179 | |
| 3180 | /* rhs */ |
| 3181 | tok = read_token (READ); |
| 3182 | if (tok == WORD) |
| 3183 | { |
| 3184 | tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL); |
| 3185 | term = make_cond_node (COND_BINARY, op, tleft, tright); |
| 3186 | } |
| 3187 | else |
| 3188 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3189 | if (etext = error_token_from_token (tok)) |
| 3190 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3191 | parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3192 | free (etext); |
| 3193 | } |
| 3194 | else |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3195 | parser_error (line_number, _("unexpected argument to conditional binary operator")); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3196 | dispose_cond_node (tleft); |
| 3197 | dispose_word (op); |
| 3198 | COND_RETURN_ERROR (); |
| 3199 | } |
| 3200 | |
| 3201 | (void)cond_skip_newlines (); |
| 3202 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3203 | else |
| 3204 | { |
| 3205 | if (tok < 256) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3206 | parser_error (line_number, _("unexpected token `%c' in conditional command"), tok); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3207 | else if (etext = error_token_from_token (tok)) |
| 3208 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3209 | parser_error (line_number, _("unexpected token `%s' in conditional command"), etext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3210 | free (etext); |
| 3211 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3212 | else |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3213 | parser_error (line_number, _("unexpected token %d in conditional command"), tok); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3214 | COND_RETURN_ERROR (); |
| 3215 | } |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3216 | return (term); |
| 3217 | } |
| 3218 | |
| 3219 | /* This is kind of bogus -- we slip a mini recursive-descent parser in |
| 3220 | here to handle the conditional statement syntax. */ |
| 3221 | static COMMAND * |
| 3222 | parse_cond_command () |
| 3223 | { |
| 3224 | COND_COM *cexp; |
| 3225 | |
| 3226 | cexp = cond_expr (); |
| 3227 | return (make_cond_command (cexp)); |
| 3228 | } |
| 3229 | #endif |
| 3230 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3231 | #if defined (ARRAY_VARS) |
| 3232 | /* When this is called, it's guaranteed that we don't care about anything |
| 3233 | in t beyond i. We do save and restore the chars, though. */ |
| 3234 | static int |
| 3235 | token_is_assignment (t, i) |
| 3236 | char *t; |
| 3237 | int i; |
| 3238 | { |
| 3239 | unsigned char c, c1; |
| 3240 | int r; |
| 3241 | |
| 3242 | c = t[i]; c1 = t[i+1]; |
| 3243 | t[i] = '='; t[i+1] = '\0'; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3244 | r = assignment (t, (parser_state & PST_COMPASSIGN) != 0); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3245 | t[i] = c; t[i+1] = c1; |
| 3246 | return r; |
| 3247 | } |
| 3248 | |
| 3249 | static int |
| 3250 | token_is_ident (t, i) |
| 3251 | char *t; |
| 3252 | int i; |
| 3253 | { |
| 3254 | unsigned char c; |
| 3255 | int r; |
| 3256 | |
| 3257 | c = t[i]; |
| 3258 | t[i] = '\0'; |
| 3259 | r = legal_identifier (t); |
| 3260 | t[i] = c; |
| 3261 | return r; |
| 3262 | } |
| 3263 | #endif |
| 3264 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3265 | static int |
| 3266 | read_token_word (character) |
| 3267 | int character; |
| 3268 | { |
| 3269 | /* The value for YYLVAL when a WORD is read. */ |
| 3270 | WORD_DESC *the_word; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3271 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3272 | /* Index into the token that we are building. */ |
| 3273 | int token_index; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3274 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3275 | /* ALL_DIGITS becomes zero when we see a non-digit. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3276 | int all_digit_token; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3277 | |
| 3278 | /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */ |
| 3279 | int dollar_present; |
| 3280 | |
| 3281 | /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */ |
| 3282 | int quoted; |
| 3283 | |
| 3284 | /* Non-zero means to ignore the value of the next character, and just |
| 3285 | to add it no matter what. */ |
| 3286 | int pass_next_character; |
| 3287 | |
| 3288 | /* The current delimiting character. */ |
| 3289 | int cd; |
| 3290 | int result, peek_char; |
| 3291 | char *ttok, *ttrans; |
| 3292 | int ttoklen, ttranslen; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3293 | intmax_t lvalue; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3294 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3295 | if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3296 | token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3297 | |
| 3298 | token_index = 0; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3299 | all_digit_token = DIGIT (character); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3300 | dollar_present = quoted = pass_next_character = 0; |
| 3301 | |
| 3302 | for (;;) |
| 3303 | { |
| 3304 | if (character == EOF) |
| 3305 | goto got_token; |
| 3306 | |
| 3307 | if (pass_next_character) |
| 3308 | { |
| 3309 | pass_next_character = 0; |
| 3310 | goto got_character; |
| 3311 | } |
| 3312 | |
| 3313 | cd = current_delimiter (dstack); |
| 3314 | |
| 3315 | /* Handle backslashes. Quote lots of things when not inside of |
| 3316 | double-quotes, quote some things inside of double-quotes. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3317 | if MBTEST(character == '\\') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3318 | { |
| 3319 | peek_char = shell_getc (0); |
| 3320 | |
| 3321 | /* Backslash-newline is ignored in all cases except |
| 3322 | when quoted with single quotes. */ |
| 3323 | if (peek_char == '\n') |
| 3324 | { |
| 3325 | character = '\n'; |
| 3326 | goto next_character; |
| 3327 | } |
| 3328 | else |
| 3329 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3330 | shell_ungetc (peek_char); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3331 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3332 | /* If the next character is to be quoted, note it now. */ |
| 3333 | if (cd == 0 || cd == '`' || |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3334 | (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE))) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3335 | pass_next_character++; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3336 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3337 | quoted = 1; |
| 3338 | goto got_character; |
| 3339 | } |
| 3340 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3341 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3342 | /* Parse a matched pair of quote characters. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3343 | if MBTEST(shellquote (character)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3344 | { |
| 3345 | push_delimiter (dstack, character); |
| 3346 | ttok = parse_matched_pair (character, character, character, &ttoklen, 0); |
| 3347 | pop_delimiter (dstack); |
| 3348 | if (ttok == &matched_pair_error) |
| 3349 | return -1; /* Bail immediately. */ |
| 3350 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2, |
| 3351 | token_buffer_size, TOKEN_DEFAULT_GROW_SIZE); |
| 3352 | token[token_index++] = character; |
| 3353 | strcpy (token + token_index, ttok); |
| 3354 | token_index += ttoklen; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3355 | all_digit_token = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3356 | quoted = 1; |
| 3357 | dollar_present |= (character == '"' && strchr (ttok, '$') != 0); |
| 3358 | FREE (ttok); |
| 3359 | goto next_character; |
| 3360 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3361 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3362 | #ifdef EXTENDED_GLOB |
| 3363 | /* Parse a ksh-style extended pattern matching specification. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3364 | if (extended_glob && PATTERN_CHAR (character)) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3365 | { |
| 3366 | peek_char = shell_getc (1); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3367 | if MBTEST(peek_char == '(') /* ) */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3368 | { |
| 3369 | push_delimiter (dstack, peek_char); |
| 3370 | ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0); |
| 3371 | pop_delimiter (dstack); |
| 3372 | if (ttok == &matched_pair_error) |
| 3373 | return -1; /* Bail immediately. */ |
| 3374 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2, |
| 3375 | token_buffer_size, |
| 3376 | TOKEN_DEFAULT_GROW_SIZE); |
| 3377 | token[token_index++] = character; |
| 3378 | token[token_index++] = peek_char; |
| 3379 | strcpy (token + token_index, ttok); |
| 3380 | token_index += ttoklen; |
| 3381 | FREE (ttok); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3382 | dollar_present = all_digit_token = 0; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3383 | goto next_character; |
| 3384 | } |
| 3385 | else |
| 3386 | shell_ungetc (peek_char); |
| 3387 | } |
| 3388 | #endif /* EXTENDED_GLOB */ |
| 3389 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3390 | /* If the delimiter character is not single quote, parse some of |
| 3391 | the shell expansions that must be read as a single word. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3392 | if (shellexp (character)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3393 | { |
| 3394 | peek_char = shell_getc (1); |
| 3395 | /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3396 | if MBTEST(peek_char == '(' || \ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3397 | ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */ |
| 3398 | { |
| 3399 | if (peek_char == '{') /* } */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3400 | ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3401 | else if (peek_char == '(') /* ) */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3402 | { |
| 3403 | /* XXX - push and pop the `(' as a delimiter for use by |
| 3404 | the command-oriented-history code. This way newlines |
| 3405 | appearing in the $(...) string get added to the |
| 3406 | history literally rather than causing a possibly- |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3407 | incorrect `;' to be added. ) */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3408 | push_delimiter (dstack, peek_char); |
| 3409 | ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0); |
| 3410 | pop_delimiter (dstack); |
| 3411 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3412 | else |
| 3413 | ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0); |
| 3414 | if (ttok == &matched_pair_error) |
| 3415 | return -1; /* Bail immediately. */ |
| 3416 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2, |
| 3417 | token_buffer_size, |
| 3418 | TOKEN_DEFAULT_GROW_SIZE); |
| 3419 | token[token_index++] = character; |
| 3420 | token[token_index++] = peek_char; |
| 3421 | strcpy (token + token_index, ttok); |
| 3422 | token_index += ttoklen; |
| 3423 | FREE (ttok); |
| 3424 | dollar_present = 1; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3425 | all_digit_token = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3426 | goto next_character; |
| 3427 | } |
| 3428 | /* This handles $'...' and $"..." new-style quoted strings. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3429 | else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"')) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3430 | { |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3431 | int first_line; |
| 3432 | |
| 3433 | first_line = line_number; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3434 | push_delimiter (dstack, peek_char); |
| 3435 | ttok = parse_matched_pair (peek_char, peek_char, peek_char, |
| 3436 | &ttoklen, |
| 3437 | (peek_char == '\'') ? P_ALLOWESC : 0); |
| 3438 | pop_delimiter (dstack); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3439 | if (ttok == &matched_pair_error) |
| 3440 | return -1; |
| 3441 | if (peek_char == '\'') |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3442 | { |
| 3443 | ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen); |
| 3444 | free (ttok); |
| 3445 | /* Insert the single quotes and correctly quote any |
| 3446 | embedded single quotes (allowed because P_ALLOWESC was |
| 3447 | passed to parse_matched_pair). */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3448 | ttok = sh_single_quote (ttrans); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3449 | free (ttrans); |
| 3450 | ttrans = ttok; |
| 3451 | ttranslen = strlen (ttrans); |
| 3452 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3453 | else |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3454 | { |
| 3455 | /* Try to locale-expand the converted string. */ |
| 3456 | ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen); |
| 3457 | free (ttok); |
| 3458 | |
| 3459 | /* Add the double quotes back */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3460 | ttok = (char *)xmalloc (ttranslen + 3); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3461 | ttok[0] = '"'; |
| 3462 | strcpy (ttok + 1, ttrans); |
| 3463 | ttok[ttranslen + 1] = '"'; |
| 3464 | ttok[ttranslen += 2] = '\0'; |
| 3465 | free (ttrans); |
| 3466 | ttrans = ttok; |
| 3467 | } |
| 3468 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3469 | RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2, |
| 3470 | token_buffer_size, |
| 3471 | TOKEN_DEFAULT_GROW_SIZE); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3472 | strcpy (token + token_index, ttrans); |
| 3473 | token_index += ttranslen; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3474 | FREE (ttrans); |
| 3475 | quoted = 1; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3476 | all_digit_token = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3477 | goto next_character; |
| 3478 | } |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3479 | /* This could eventually be extended to recognize all of the |
| 3480 | shell's single-character parameter expansions, and set flags.*/ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3481 | else if MBTEST(character == '$' && peek_char == '$') |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3482 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3483 | ttok = (char *)xmalloc (3); |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3484 | ttok[0] = ttok[1] = '$'; |
| 3485 | ttok[2] = '\0'; |
| 3486 | RESIZE_MALLOCED_BUFFER (token, token_index, 3, |
| 3487 | token_buffer_size, |
| 3488 | TOKEN_DEFAULT_GROW_SIZE); |
| 3489 | strcpy (token + token_index, ttok); |
| 3490 | token_index += 2; |
| 3491 | dollar_present = 1; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3492 | all_digit_token = 0; |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3493 | FREE (ttok); |
| 3494 | goto next_character; |
| 3495 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3496 | else |
| 3497 | shell_ungetc (peek_char); |
| 3498 | } |
| 3499 | |
| 3500 | #if defined (ARRAY_VARS) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3501 | /* Identify possible array subscript assignment; match [...] */ |
| 3502 | else if MBTEST(character == '[' && token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) /* ] */ |
| 3503 | { |
| 3504 | ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0); |
| 3505 | if (ttok == &matched_pair_error) |
| 3506 | return -1; /* Bail immediately. */ |
| 3507 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2, |
| 3508 | token_buffer_size, |
| 3509 | TOKEN_DEFAULT_GROW_SIZE); |
| 3510 | token[token_index++] = character; |
| 3511 | strcpy (token + token_index, ttok); |
| 3512 | token_index += ttoklen; |
| 3513 | FREE (ttok); |
| 3514 | all_digit_token = 0; |
| 3515 | goto next_character; |
| 3516 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3517 | /* Identify possible compound array variable assignment. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3518 | else if MBTEST(character == '=' && token_index > 0 && token_is_assignment (token, token_index)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3519 | { |
| 3520 | peek_char = shell_getc (1); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3521 | if MBTEST(peek_char == '(') /* ) */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3522 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3523 | ttok = parse_compound_assignment (&ttoklen); |
| 3524 | |
| 3525 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4, |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3526 | token_buffer_size, |
| 3527 | TOKEN_DEFAULT_GROW_SIZE); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3528 | |
| 3529 | token[token_index++] = '='; |
| 3530 | token[token_index++] = '('; |
| 3531 | if (ttok) |
| 3532 | { |
| 3533 | strcpy (token + token_index, ttok); |
| 3534 | token_index += ttoklen; |
| 3535 | } |
| 3536 | token[token_index++] = ')'; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3537 | FREE (ttok); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3538 | all_digit_token = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3539 | goto next_character; |
| 3540 | } |
| 3541 | else |
| 3542 | shell_ungetc (peek_char); |
| 3543 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3544 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3545 | |
| 3546 | /* When not parsing a multi-character word construct, shell meta- |
| 3547 | characters break words. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3548 | if MBTEST(shellbreak (character)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3549 | { |
| 3550 | shell_ungetc (character); |
| 3551 | goto got_token; |
| 3552 | } |
| 3553 | |
| 3554 | got_character: |
| 3555 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3556 | all_digit_token &= DIGIT (character); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3557 | dollar_present |= character == '$'; |
| 3558 | |
| 3559 | if (character == CTLESC || character == CTLNUL) |
| 3560 | token[token_index++] = CTLESC; |
| 3561 | |
| 3562 | token[token_index++] = character; |
| 3563 | |
| 3564 | RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size, |
| 3565 | TOKEN_DEFAULT_GROW_SIZE); |
| 3566 | |
| 3567 | next_character: |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3568 | if (character == '\n' && SHOULD_PROMPT ()) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3569 | prompt_again (); |
| 3570 | |
| 3571 | /* We want to remove quoted newlines (that is, a \<newline> pair) |
| 3572 | unless we are within single quotes or pass_next_character is |
| 3573 | set (the shell equivalent of literal-next). */ |
| 3574 | cd = current_delimiter (dstack); |
| 3575 | character = shell_getc (cd != '\'' && pass_next_character == 0); |
| 3576 | } /* end for (;;) */ |
| 3577 | |
| 3578 | got_token: |
| 3579 | |
| 3580 | token[token_index] = '\0'; |
| 3581 | |
| 3582 | /* Check to see what thing we should return. If the last_read_token |
| 3583 | is a `<', or a `&', or the character which ended this token is |
| 3584 | a '>' or '<', then, and ONLY then, is this input token a NUMBER. |
| 3585 | Otherwise, it is just a word, and should be returned as such. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3586 | if MBTEST(all_digit_token && (character == '<' || character == '>' || \ |
| 3587 | last_read_token == LESS_AND || \ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3588 | last_read_token == GREATER_AND)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3589 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3590 | if (legal_number (token, &lvalue) && (int)lvalue == lvalue) |
| 3591 | yylval.number = lvalue; |
| 3592 | else |
| 3593 | yylval.number = -1; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3594 | return (NUMBER); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3597 | /* Check for special case tokens. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3598 | result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3599 | if (result >= 0) |
| 3600 | return result; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3601 | |
| 3602 | #if defined (ALIAS) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3603 | /* Posix.2 does not allow reserved words to be aliased, so check for all |
| 3604 | of them, including special cases, before expanding the current token |
| 3605 | as an alias. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3606 | if MBTEST(posixly_correct) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3607 | CHECK_FOR_RESERVED_WORD (token); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3608 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3609 | /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting |
| 3610 | inhibits alias expansion. */ |
| 3611 | if (expand_aliases && quoted == 0) |
| 3612 | { |
| 3613 | result = alias_expand_token (token); |
| 3614 | if (result == RE_READ_TOKEN) |
| 3615 | return (RE_READ_TOKEN); |
| 3616 | else if (result == NO_EXPANSION) |
| 3617 | parser_state &= ~PST_ALEXPNEXT; |
| 3618 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3619 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3620 | /* If not in Posix.2 mode, check for reserved words after alias |
| 3621 | expansion. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3622 | if MBTEST(posixly_correct == 0) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3623 | #endif |
| 3624 | CHECK_FOR_RESERVED_WORD (token); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3625 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3626 | the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3627 | the_word->word = (char *)xmalloc (1 + token_index); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3628 | the_word->flags = 0; |
| 3629 | strcpy (the_word->word, token); |
| 3630 | if (dollar_present) |
| 3631 | the_word->flags |= W_HASDOLLAR; |
| 3632 | if (quoted) |
| 3633 | the_word->flags |= W_QUOTED; |
| 3634 | /* A word is an assignment if it appears at the beginning of a |
| 3635 | simple command, or after another assignment word. This is |
| 3636 | context-dependent, so it cannot be handled in the grammar. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3637 | if (assignment (token, (parser_state & PST_COMPASSIGN) != 0)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3638 | { |
| 3639 | the_word->flags |= W_ASSIGNMENT; |
| 3640 | /* Don't perform word splitting on assignment statements. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3641 | if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3642 | the_word->flags |= W_NOSPLIT; |
| 3643 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3644 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3645 | yylval.word = the_word; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3646 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3647 | result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT)) |
| 3648 | ? ASSIGNMENT_WORD : WORD; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3649 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3650 | switch (last_read_token) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3651 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3652 | case FUNCTION: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3653 | parser_state |= PST_ALLOWOPNBRC; |
| 3654 | function_dstart = line_number; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3655 | break; |
| 3656 | case CASE: |
| 3657 | case SELECT: |
| 3658 | case FOR: |
| 3659 | if (word_top < MAX_CASE_NEST) |
| 3660 | word_top++; |
| 3661 | word_lineno[word_top] = line_number; |
| 3662 | break; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3663 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3664 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3665 | return (result); |
| 3666 | } |
| 3667 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3668 | /* Return 1 if TOKSYM is a token that after being read would allow |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3669 | a reserved word to be seen, else 0. */ |
| 3670 | static int |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3671 | reserved_word_acceptable (toksym) |
| 3672 | int toksym; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3673 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3674 | switch (toksym) |
| 3675 | { |
| 3676 | case '\n': |
| 3677 | case ';': |
| 3678 | case '(': |
| 3679 | case ')': |
| 3680 | case '|': |
| 3681 | case '&': |
| 3682 | case '{': |
| 3683 | case '}': /* XXX */ |
| 3684 | case AND_AND: |
| 3685 | case BANG: |
| 3686 | case DO: |
| 3687 | case DONE: |
| 3688 | case ELIF: |
| 3689 | case ELSE: |
| 3690 | case ESAC: |
| 3691 | case FI: |
| 3692 | case IF: |
| 3693 | case OR_OR: |
| 3694 | case SEMI_SEMI: |
| 3695 | case THEN: |
| 3696 | case TIME: |
| 3697 | case TIMEOPT: |
| 3698 | case UNTIL: |
| 3699 | case WHILE: |
| 3700 | case 0: |
| 3701 | return 1; |
| 3702 | default: |
| 3703 | return 0; |
| 3704 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3705 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3706 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3707 | /* Return the index of TOKEN in the alist of reserved words, or -1 if |
| 3708 | TOKEN is not a shell reserved word. */ |
| 3709 | int |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3710 | find_reserved_word (tokstr) |
| 3711 | char *tokstr; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3712 | { |
| 3713 | int i; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3714 | for (i = 0; word_token_alist[i].word; i++) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3715 | if (STREQ (tokstr, word_token_alist[i].word)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3716 | return i; |
| 3717 | return -1; |
| 3718 | } |
| 3719 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3720 | #if 0 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3721 | #if defined (READLINE) |
| 3722 | /* Called after each time readline is called. This insures that whatever |
| 3723 | the new prompt string is gets propagated to readline's local prompt |
| 3724 | variable. */ |
| 3725 | static void |
| 3726 | reset_readline_prompt () |
| 3727 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3728 | char *temp_prompt; |
| 3729 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3730 | if (prompt_string_pointer) |
| 3731 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3732 | temp_prompt = (*prompt_string_pointer) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3733 | ? decode_prompt_string (*prompt_string_pointer) |
| 3734 | : (char *)NULL; |
| 3735 | |
| 3736 | if (temp_prompt == 0) |
| 3737 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3738 | temp_prompt = (char *)xmalloc (1); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3739 | temp_prompt[0] = '\0'; |
| 3740 | } |
| 3741 | |
| 3742 | FREE (current_readline_prompt); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3743 | current_readline_prompt = temp_prompt; |
| 3744 | } |
| 3745 | } |
| 3746 | #endif /* READLINE */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3747 | #endif /* 0 */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3748 | |
| 3749 | #if defined (HISTORY) |
| 3750 | /* A list of tokens which can be followed by newlines, but not by |
| 3751 | semi-colons. When concatenating multiple lines of history, the |
| 3752 | newline separator for such tokens is replaced with a space. */ |
| 3753 | static int no_semi_successors[] = { |
| 3754 | '\n', '{', '(', ')', ';', '&', '|', |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3755 | CASE, DO, ELSE, IF, SEMI_SEMI, THEN, UNTIL, WHILE, AND_AND, OR_OR, IN, |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3756 | 0 |
| 3757 | }; |
| 3758 | |
| 3759 | /* If we are not within a delimited expression, try to be smart |
| 3760 | about which separators can be semi-colons and which must be |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3761 | newlines. Returns the string that should be added into the |
| 3762 | history entry. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3763 | char * |
| 3764 | history_delimiting_chars () |
| 3765 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3766 | register int i; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3767 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3768 | if (dstack.delimiter_depth != 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3769 | return ("\n"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3770 | |
| 3771 | /* First, handle some special cases. */ |
| 3772 | /*(*/ |
| 3773 | /* If we just read `()', assume it's a function definition, and don't |
| 3774 | add a semicolon. If the token before the `)' was not `(', and we're |
| 3775 | not in the midst of parsing a case statement, assume it's a |
| 3776 | parenthesized command and add the semicolon. */ |
| 3777 | /*)(*/ |
| 3778 | if (token_before_that == ')') |
| 3779 | { |
| 3780 | if (two_tokens_ago == '(') /*)*/ /* function def */ |
| 3781 | return " "; |
| 3782 | /* This does not work for subshells inside case statement |
| 3783 | command lists. It's a suboptimal solution. */ |
| 3784 | else if (parser_state & PST_CASESTMT) /* case statement pattern */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3785 | return " "; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3786 | else |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3787 | return "; "; /* (...) subshell */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3788 | } |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3789 | else if (token_before_that == WORD && two_tokens_ago == FUNCTION) |
| 3790 | return " "; /* function def using `function name' without `()' */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3791 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3792 | else if (token_before_that == WORD && two_tokens_ago == FOR) |
| 3793 | { |
| 3794 | /* Tricky. `for i\nin ...' should not have a semicolon, but |
| 3795 | `for i\ndo ...' should. We do what we can. */ |
| 3796 | for (i = shell_input_line_index; whitespace(shell_input_line[i]); i++) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3797 | ; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3798 | if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n') |
| 3799 | return " "; |
| 3800 | return ";"; |
| 3801 | } |
| 3802 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3803 | for (i = 0; no_semi_successors[i]; i++) |
| 3804 | { |
| 3805 | if (token_before_that == no_semi_successors[i]) |
| 3806 | return (" "); |
| 3807 | } |
| 3808 | |
| 3809 | return ("; "); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3810 | } |
| 3811 | #endif /* HISTORY */ |
| 3812 | |
| 3813 | /* Issue a prompt, or prepare to issue a prompt when the next character |
| 3814 | is read. */ |
| 3815 | static void |
| 3816 | prompt_again () |
| 3817 | { |
| 3818 | char *temp_prompt; |
| 3819 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 3820 | if (interactive == 0 || expanding_alias()) /* XXX */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3821 | return; |
| 3822 | |
| 3823 | ps1_prompt = get_string_value ("PS1"); |
| 3824 | ps2_prompt = get_string_value ("PS2"); |
| 3825 | |
| 3826 | if (!prompt_string_pointer) |
| 3827 | prompt_string_pointer = &ps1_prompt; |
| 3828 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3829 | temp_prompt = *prompt_string_pointer |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3830 | ? decode_prompt_string (*prompt_string_pointer) |
| 3831 | : (char *)NULL; |
| 3832 | |
| 3833 | if (temp_prompt == 0) |
| 3834 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3835 | temp_prompt = (char *)xmalloc (1); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3836 | temp_prompt[0] = '\0'; |
| 3837 | } |
| 3838 | |
| 3839 | current_prompt_string = *prompt_string_pointer; |
| 3840 | prompt_string_pointer = &ps2_prompt; |
| 3841 | |
| 3842 | #if defined (READLINE) |
| 3843 | if (!no_line_editing) |
| 3844 | { |
| 3845 | FREE (current_readline_prompt); |
| 3846 | current_readline_prompt = temp_prompt; |
| 3847 | } |
| 3848 | else |
| 3849 | #endif /* READLINE */ |
| 3850 | { |
| 3851 | FREE (current_decoded_prompt); |
| 3852 | current_decoded_prompt = temp_prompt; |
| 3853 | } |
| 3854 | } |
| 3855 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3856 | int |
| 3857 | get_current_prompt_level () |
| 3858 | { |
| 3859 | return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1); |
| 3860 | } |
| 3861 | |
| 3862 | void |
| 3863 | set_current_prompt_level (x) |
| 3864 | int x; |
| 3865 | { |
| 3866 | prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt; |
| 3867 | current_prompt_string = *prompt_string_pointer; |
| 3868 | } |
| 3869 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3870 | static void |
| 3871 | print_prompt () |
| 3872 | { |
| 3873 | fprintf (stderr, "%s", current_decoded_prompt); |
| 3874 | fflush (stderr); |
| 3875 | } |
| 3876 | |
| 3877 | /* Return a string which will be printed as a prompt. The string |
| 3878 | may contain special characters which are decoded as follows: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3879 | |
| 3880 | \a bell (ascii 07) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3881 | \d the date in Day Mon Date format |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3882 | \e escape (ascii 033) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3883 | \h the hostname up to the first `.' |
| 3884 | \H the hostname |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3885 | \j the number of active jobs |
| 3886 | \l the basename of the shell's tty device name |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3887 | \n CRLF |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3888 | \r CR |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3889 | \s the name of the shell |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3890 | \t the time in 24-hour hh:mm:ss format |
| 3891 | \T the time in 12-hour hh:mm:ss format |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3892 | \@ the time in 12-hour hh:mm am/pm format |
| 3893 | \A the time in 24-hour hh:mm format |
| 3894 | \D{fmt} the result of passing FMT to strftime(3) |
| 3895 | \u your username |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3896 | \v the version of bash (e.g., 2.00) |
| 3897 | \V the release of bash, version + patchlevel (e.g., 2.00.0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3898 | \w the current working directory |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3899 | \W the last element of $PWD |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3900 | \! the history number of this command |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3901 | \# the command number of this command |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3902 | \$ a $ or a # if you are root |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3903 | \nnn character code nnn in octal |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3904 | \\ a backslash |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3905 | \[ begin a sequence of non-printing chars |
| 3906 | \] end a sequence of non-printing chars |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3907 | */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3908 | #define PROMPT_GROWTH 48 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3909 | char * |
| 3910 | decode_prompt_string (string) |
| 3911 | char *string; |
| 3912 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3913 | WORD_LIST *list; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3914 | char *result, *t; |
| 3915 | struct dstack save_dstack; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3916 | int last_exit_value; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3917 | #if defined (PROMPT_STRING_DECODE) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3918 | int result_size, result_index; |
| 3919 | int c, n; |
| 3920 | char *temp, octal_string[4]; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3921 | struct tm *tm; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3922 | time_t the_time; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3923 | char timebuf[128]; |
| 3924 | char *timefmt; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3925 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3926 | result = (char *)xmalloc (result_size = PROMPT_GROWTH); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3927 | result[result_index = 0] = 0; |
| 3928 | temp = (char *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3929 | |
| 3930 | while (c = *string++) |
| 3931 | { |
| 3932 | if (posixly_correct && c == '!') |
| 3933 | { |
| 3934 | if (*string == '!') |
| 3935 | { |
| 3936 | temp = savestring ("!"); |
| 3937 | goto add_string; |
| 3938 | } |
| 3939 | else |
| 3940 | { |
| 3941 | #if !defined (HISTORY) |
| 3942 | temp = savestring ("1"); |
| 3943 | #else /* HISTORY */ |
| 3944 | temp = itos (history_number ()); |
| 3945 | #endif /* HISTORY */ |
| 3946 | string--; /* add_string increments string again. */ |
| 3947 | goto add_string; |
| 3948 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3949 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3950 | if (c == '\\') |
| 3951 | { |
| 3952 | c = *string; |
| 3953 | |
| 3954 | switch (c) |
| 3955 | { |
| 3956 | case '0': |
| 3957 | case '1': |
| 3958 | case '2': |
| 3959 | case '3': |
| 3960 | case '4': |
| 3961 | case '5': |
| 3962 | case '6': |
| 3963 | case '7': |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3964 | strncpy (octal_string, string, 3); |
| 3965 | octal_string[3] = '\0'; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3966 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3967 | n = read_octal (octal_string); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3968 | temp = (char *)xmalloc (3); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3969 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3970 | if (n == CTLESC || n == CTLNUL) |
| 3971 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3972 | temp[0] = CTLESC; |
| 3973 | temp[1] = n; |
| 3974 | temp[2] = '\0'; |
| 3975 | } |
| 3976 | else if (n == -1) |
| 3977 | { |
| 3978 | temp[0] = '\\'; |
| 3979 | temp[1] = '\0'; |
| 3980 | } |
| 3981 | else |
| 3982 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3983 | temp[0] = n; |
| 3984 | temp[1] = '\0'; |
| 3985 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3986 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3987 | for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++) |
| 3988 | string++; |
| 3989 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3990 | c = 0; /* tested at add_string: */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3991 | goto add_string; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3992 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3993 | case 'd': |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3994 | case 't': |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3995 | case 'T': |
| 3996 | case '@': |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3997 | case 'A': |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3998 | /* Make the current time/date into a string. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3999 | (void) time (&the_time); |
| 4000 | tm = localtime (&the_time); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4001 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4002 | if (c == 'd') |
| 4003 | n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm); |
| 4004 | else if (c == 't') |
| 4005 | n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm); |
| 4006 | else if (c == 'T') |
| 4007 | n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm); |
| 4008 | else if (c == '@') |
| 4009 | n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm); |
| 4010 | else if (c == 'A') |
| 4011 | n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4012 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4013 | if (n == 0) |
| 4014 | timebuf[0] = '\0'; |
| 4015 | else |
| 4016 | timebuf[sizeof(timebuf) - 1] = '\0'; |
| 4017 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4018 | temp = savestring (timebuf); |
| 4019 | goto add_string; |
| 4020 | |
| 4021 | case 'D': /* strftime format */ |
| 4022 | if (string[1] != '{') /* } */ |
| 4023 | goto not_escape; |
| 4024 | |
| 4025 | (void) time (&the_time); |
| 4026 | tm = localtime (&the_time); |
| 4027 | string += 2; /* skip { */ |
| 4028 | timefmt = xmalloc (strlen (string) + 3); |
| 4029 | for (t = timefmt; *string && *string != '}'; ) |
| 4030 | *t++ = *string++; |
| 4031 | *t = '\0'; |
| 4032 | c = *string; /* tested at add_string */ |
| 4033 | if (timefmt[0] == '\0') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4034 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4035 | timefmt[0] = '%'; |
| 4036 | timefmt[1] = 'X'; /* locale-specific current time */ |
| 4037 | timefmt[2] = '\0'; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4038 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4039 | n = strftime (timebuf, sizeof (timebuf), timefmt, tm); |
| 4040 | free (timefmt); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4041 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4042 | if (n == 0) |
| 4043 | timebuf[0] = '\0'; |
| 4044 | else |
| 4045 | timebuf[sizeof(timebuf) - 1] = '\0'; |
| 4046 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4047 | if (promptvars || posixly_correct) |
| 4048 | /* Make sure that expand_prompt_string is called with a |
| 4049 | second argument of Q_DOUBLE_QUOTES if we use this |
| 4050 | function here. */ |
| 4051 | temp = sh_backslash_quote_for_double_quotes (timebuf); |
| 4052 | else |
| 4053 | temp = savestring (timebuf); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4054 | goto add_string; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4055 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4056 | case 'n': |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4057 | temp = (char *)xmalloc (3); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4058 | temp[0] = no_line_editing ? '\n' : '\r'; |
| 4059 | temp[1] = no_line_editing ? '\0' : '\n'; |
| 4060 | temp[2] = '\0'; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4061 | goto add_string; |
| 4062 | |
| 4063 | case 's': |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4064 | temp = base_pathname (shell_name); |
| 4065 | temp = savestring (temp); |
| 4066 | goto add_string; |
| 4067 | |
| 4068 | case 'v': |
| 4069 | case 'V': |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4070 | temp = (char *)xmalloc (16); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4071 | if (c == 'v') |
| 4072 | strcpy (temp, dist_version); |
| 4073 | else |
| 4074 | sprintf (temp, "%s.%d", dist_version, patch_level); |
| 4075 | goto add_string; |
| 4076 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4077 | case 'w': |
| 4078 | case 'W': |
| 4079 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4080 | /* Use the value of PWD because it is much more efficient. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4081 | char t_string[PATH_MAX], *t; |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 4082 | int tlen; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4083 | |
| 4084 | temp = get_string_value ("PWD"); |
| 4085 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4086 | if (temp == 0) |
| 4087 | { |
| 4088 | if (getcwd (t_string, sizeof(t_string)) == 0) |
| 4089 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4090 | t_string[0] = '.'; |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 4091 | tlen = 1; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4092 | } |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 4093 | else |
| 4094 | tlen = strlen (t_string); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4095 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4096 | else |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 4097 | { |
| 4098 | tlen = sizeof (t_string) - 1; |
| 4099 | strncpy (t_string, temp, tlen); |
| 4100 | } |
| 4101 | t_string[tlen] = '\0'; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4102 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4103 | #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0) |
| 4104 | #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4105 | /* Abbreviate \W as ~ if $PWD == $HOME */ |
| 4106 | if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, temp) == 0)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4107 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4108 | if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0) |
| 4109 | { |
| 4110 | t = strrchr (t_string, '/'); |
| 4111 | if (t) |
| 4112 | strcpy (t_string, t + 1); |
| 4113 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4114 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4115 | #undef ROOT_PATH |
| 4116 | #undef DOUBLE_SLASH_ROOT |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4117 | else |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 4118 | /* polite_directory_format is guaranteed to return a string |
| 4119 | no longer than PATH_MAX - 1 characters. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4120 | strcpy (t_string, polite_directory_format (t_string)); |
| 4121 | |
| 4122 | /* If we're going to be expanding the prompt string later, |
| 4123 | quote the directory name. */ |
| 4124 | if (promptvars || posixly_correct) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4125 | /* Make sure that expand_prompt_string is called with a |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4126 | second argument of Q_DOUBLE_QUOTES if we use this |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4127 | function here. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4128 | temp = sh_backslash_quote_for_double_quotes (t_string); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4129 | else |
| 4130 | temp = savestring (t_string); |
| 4131 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4132 | goto add_string; |
| 4133 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4134 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4135 | case 'u': |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4136 | if (current_user.user_name == 0) |
| 4137 | get_current_user_info (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4138 | temp = savestring (current_user.user_name); |
| 4139 | goto add_string; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4140 | |
| 4141 | case 'h': |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4142 | case 'H': |
| 4143 | temp = savestring (current_host_name); |
| 4144 | if (c == 'h' && (t = (char *)strchr (temp, '.'))) |
| 4145 | *t = '\0'; |
| 4146 | goto add_string; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4147 | |
| 4148 | case '#': |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4149 | temp = itos (current_command_number); |
| 4150 | goto add_string; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4151 | |
| 4152 | case '!': |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4153 | #if !defined (HISTORY) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4154 | temp = savestring ("1"); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4155 | #else /* HISTORY */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4156 | temp = itos (history_number ()); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4157 | #endif /* HISTORY */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4158 | goto add_string; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4159 | |
| 4160 | case '$': |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4161 | t = temp = (char *)xmalloc (3); |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 4162 | if ((promptvars || posixly_correct) && (current_user.euid != 0)) |
| 4163 | *t++ = '\\'; |
| 4164 | *t++ = current_user.euid == 0 ? '#' : '$'; |
| 4165 | *t = '\0'; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4166 | goto add_string; |
| 4167 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4168 | case 'j': |
| 4169 | temp = itos (count_all_jobs ()); |
| 4170 | goto add_string; |
| 4171 | |
| 4172 | case 'l': |
| 4173 | #if defined (HAVE_TTYNAME) |
| 4174 | temp = (char *)ttyname (fileno (stdin)); |
| 4175 | t = temp ? base_pathname (temp) : "tty"; |
| 4176 | temp = savestring (t); |
| 4177 | #else |
| 4178 | temp = savestring ("tty"); |
| 4179 | #endif /* !HAVE_TTYNAME */ |
| 4180 | goto add_string; |
| 4181 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4182 | #if defined (READLINE) |
| 4183 | case '[': |
| 4184 | case ']': |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4185 | if (no_line_editing) |
| 4186 | { |
| 4187 | string++; |
| 4188 | break; |
| 4189 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4190 | temp = (char *)xmalloc (3); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4191 | temp[0] = '\001'; |
| 4192 | temp[1] = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE; |
| 4193 | temp[2] = '\0'; |
| 4194 | goto add_string; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4195 | #endif /* READLINE */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4196 | |
| 4197 | case '\\': |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4198 | case 'a': |
| 4199 | case 'e': |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4200 | case 'r': |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4201 | temp = (char *)xmalloc (2); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4202 | if (c == 'a') |
| 4203 | temp[0] = '\07'; |
| 4204 | else if (c == 'e') |
| 4205 | temp[0] = '\033'; |
| 4206 | else if (c == 'r') |
| 4207 | temp[0] = '\r'; |
| 4208 | else /* (c == '\\') */ |
| 4209 | temp[0] = c; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4210 | temp[1] = '\0'; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4211 | goto add_string; |
| 4212 | |
| 4213 | default: |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4214 | not_escape: |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4215 | temp = (char *)xmalloc (3); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4216 | temp[0] = '\\'; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4217 | temp[1] = c; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4218 | temp[2] = '\0'; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4219 | |
| 4220 | add_string: |
| 4221 | if (c) |
| 4222 | string++; |
| 4223 | result = |
| 4224 | sub_append_string (temp, result, &result_index, &result_size); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4225 | temp = (char *)NULL; /* Freed in sub_append_string (). */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4226 | result[result_index] = '\0'; |
| 4227 | break; |
| 4228 | } |
| 4229 | } |
| 4230 | else |
| 4231 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4232 | RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4233 | result[result_index++] = c; |
| 4234 | result[result_index] = '\0'; |
| 4235 | } |
| 4236 | } |
| 4237 | #else /* !PROMPT_STRING_DECODE */ |
| 4238 | result = savestring (string); |
| 4239 | #endif /* !PROMPT_STRING_DECODE */ |
| 4240 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4241 | /* Save the delimiter stack and point `dstack' to temp space so any |
| 4242 | command substitutions in the prompt string won't result in screwing |
| 4243 | up the parser's quoting state. */ |
| 4244 | save_dstack = dstack; |
| 4245 | dstack = temp_dstack; |
| 4246 | dstack.delimiter_depth = 0; |
| 4247 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4248 | /* Perform variable and parameter expansion and command substitution on |
| 4249 | the prompt string. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4250 | if (promptvars || posixly_correct) |
| 4251 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4252 | last_exit_value = last_command_exit_value; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4253 | list = expand_prompt_string (result, Q_DOUBLE_QUOTES); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4254 | free (result); |
| 4255 | result = string_list (list); |
| 4256 | dispose_words (list); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4257 | last_command_exit_value = last_exit_value; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4258 | } |
| 4259 | else |
| 4260 | { |
| 4261 | t = dequote_string (result); |
| 4262 | free (result); |
| 4263 | result = t; |
| 4264 | } |
| 4265 | |
| 4266 | dstack = save_dstack; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4267 | |
| 4268 | return (result); |
| 4269 | } |
| 4270 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4271 | /************************************************ |
| 4272 | * * |
| 4273 | * ERROR HANDLING * |
| 4274 | * * |
| 4275 | ************************************************/ |
| 4276 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4277 | /* Report a syntax error, and restart the parser. Call here for fatal |
| 4278 | errors. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4279 | int |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4280 | yyerror (msg) |
| 4281 | const char *msg; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4282 | { |
| 4283 | report_syntax_error ((char *)NULL); |
| 4284 | reset_parser (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4285 | return (0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4288 | static char * |
| 4289 | error_token_from_token (token) |
| 4290 | int token; |
| 4291 | { |
| 4292 | char *t; |
| 4293 | |
| 4294 | if (t = find_token_in_alist (token, word_token_alist, 0)) |
| 4295 | return t; |
| 4296 | |
| 4297 | if (t = find_token_in_alist (token, other_token_alist, 0)) |
| 4298 | return t; |
| 4299 | |
| 4300 | t = (char *)NULL; |
| 4301 | /* This stuff is dicy and needs closer inspection */ |
| 4302 | switch (current_token) |
| 4303 | { |
| 4304 | case WORD: |
| 4305 | case ASSIGNMENT_WORD: |
| 4306 | if (yylval.word) |
| 4307 | t = savestring (yylval.word->word); |
| 4308 | break; |
| 4309 | case NUMBER: |
| 4310 | t = itos (yylval.number); |
| 4311 | break; |
| 4312 | case ARITH_CMD: |
| 4313 | if (yylval.word_list) |
| 4314 | t = string_list (yylval.word_list); |
| 4315 | break; |
| 4316 | case ARITH_FOR_EXPRS: |
| 4317 | if (yylval.word_list) |
| 4318 | t = string_list_internal (yylval.word_list, " ; "); |
| 4319 | break; |
| 4320 | case COND_CMD: |
| 4321 | t = (char *)NULL; /* punt */ |
| 4322 | break; |
| 4323 | } |
| 4324 | |
| 4325 | return t; |
| 4326 | } |
| 4327 | |
| 4328 | static char * |
| 4329 | error_token_from_text () |
| 4330 | { |
| 4331 | char *msg, *t; |
| 4332 | int token_end, i; |
| 4333 | |
| 4334 | t = shell_input_line; |
| 4335 | i = shell_input_line_index; |
| 4336 | token_end = 0; |
| 4337 | msg = (char *)NULL; |
| 4338 | |
| 4339 | if (i && t[i] == '\0') |
| 4340 | i--; |
| 4341 | |
| 4342 | while (i && (whitespace (t[i]) || t[i] == '\n')) |
| 4343 | i--; |
| 4344 | |
| 4345 | if (i) |
| 4346 | token_end = i + 1; |
| 4347 | |
| 4348 | while (i && (member (t[i], " \n\t;|&") == 0)) |
| 4349 | i--; |
| 4350 | |
| 4351 | while (i != token_end && (whitespace (t[i]) || t[i] == '\n')) |
| 4352 | i++; |
| 4353 | |
| 4354 | /* Return our idea of the offending token. */ |
| 4355 | if (token_end || (i == 0 && token_end == 0)) |
| 4356 | { |
| 4357 | if (token_end) |
| 4358 | msg = substring (t, i, token_end); |
| 4359 | else /* one-character token */ |
| 4360 | { |
| 4361 | msg = (char *)xmalloc (2); |
| 4362 | msg[0] = t[i]; |
| 4363 | msg[1] = '\0'; |
| 4364 | } |
| 4365 | } |
| 4366 | |
| 4367 | return (msg); |
| 4368 | } |
| 4369 | |
| 4370 | static void |
| 4371 | print_offending_line () |
| 4372 | { |
| 4373 | char *msg; |
| 4374 | int token_end; |
| 4375 | |
| 4376 | msg = savestring (shell_input_line); |
| 4377 | token_end = strlen (msg); |
| 4378 | while (token_end && msg[token_end - 1] == '\n') |
| 4379 | msg[--token_end] = '\0'; |
| 4380 | |
| 4381 | parser_error (line_number, "`%s'", msg); |
| 4382 | free (msg); |
| 4383 | } |
| 4384 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4385 | /* Report a syntax error with line numbers, etc. |
| 4386 | Call here for recoverable errors. If you have a message to print, |
| 4387 | then place it in MESSAGE, otherwise pass NULL and this will figure |
| 4388 | out an appropriate message for you. */ |
| 4389 | static void |
| 4390 | report_syntax_error (message) |
| 4391 | char *message; |
| 4392 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4393 | char *msg; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4394 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4395 | if (message) |
| 4396 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4397 | parser_error (line_number, "%s", message); |
| 4398 | if (interactive && EOF_Reached) |
| 4399 | EOF_Reached = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4400 | last_command_exit_value = EX_USAGE; |
| 4401 | return; |
| 4402 | } |
| 4403 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4404 | /* If the line of input we're reading is not null, try to find the |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4405 | objectionable token. First, try to figure out what token the |
| 4406 | parser's complaining about by looking at current_token. */ |
| 4407 | if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token))) |
| 4408 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4409 | parser_error (line_number, _("syntax error near unexpected token `%s'"), msg); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4410 | free (msg); |
| 4411 | |
| 4412 | if (interactive == 0) |
| 4413 | print_offending_line (); |
| 4414 | |
| 4415 | last_command_exit_value = EX_USAGE; |
| 4416 | return; |
| 4417 | } |
| 4418 | |
| 4419 | /* If looking at the current token doesn't prove fruitful, try to find the |
| 4420 | offending token by analyzing the text of the input line near the current |
| 4421 | input line index and report what we find. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4422 | if (shell_input_line && *shell_input_line) |
| 4423 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4424 | msg = error_token_from_text (); |
| 4425 | if (msg) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4426 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4427 | parser_error (line_number, _("syntax error near `%s'"), msg); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4428 | free (msg); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4431 | /* If not interactive, print the line containing the error. */ |
| 4432 | if (interactive == 0) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4433 | print_offending_line (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4434 | } |
| 4435 | else |
| 4436 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4437 | msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4438 | parser_error (line_number, "%s", msg); |
| 4439 | /* When the shell is interactive, this file uses EOF_Reached |
| 4440 | only for error reporting. Other mechanisms are used to |
| 4441 | decide whether or not to exit. */ |
| 4442 | if (interactive && EOF_Reached) |
| 4443 | EOF_Reached = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4444 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4445 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4446 | last_command_exit_value = EX_USAGE; |
| 4447 | } |
| 4448 | |
| 4449 | /* ??? Needed function. ??? We have to be able to discard the constructs |
| 4450 | created during parsing. In the case of error, we want to return |
| 4451 | allocated objects to the memory pool. In the case of no error, we want |
| 4452 | to throw away the information about where the allocated objects live. |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4453 | (dispose_command () will actually free the command.) */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4454 | static void |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4455 | discard_parser_constructs (error_p) |
| 4456 | int error_p; |
| 4457 | { |
| 4458 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4459 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4460 | /************************************************ |
| 4461 | * * |
| 4462 | * EOF HANDLING * |
| 4463 | * * |
| 4464 | ************************************************/ |
| 4465 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4466 | /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */ |
| 4467 | |
| 4468 | /* A flag denoting whether or not ignoreeof is set. */ |
| 4469 | int ignoreeof = 0; |
| 4470 | |
| 4471 | /* The number of times that we have encountered an EOF character without |
| 4472 | another character intervening. When this gets above the limit, the |
| 4473 | shell terminates. */ |
| 4474 | int eof_encountered = 0; |
| 4475 | |
| 4476 | /* The limit for eof_encountered. */ |
| 4477 | int eof_encountered_limit = 10; |
| 4478 | |
| 4479 | /* If we have EOF as the only input unit, this user wants to leave |
| 4480 | the shell. If the shell is not interactive, then just leave. |
| 4481 | Otherwise, if ignoreeof is set, and we haven't done this the |
| 4482 | required number of times in a row, print a message. */ |
| 4483 | static void |
| 4484 | handle_eof_input_unit () |
| 4485 | { |
| 4486 | if (interactive) |
| 4487 | { |
| 4488 | /* shell.c may use this to decide whether or not to write out the |
| 4489 | history, among other things. We use it only for error reporting |
| 4490 | in this file. */ |
| 4491 | if (EOF_Reached) |
| 4492 | EOF_Reached = 0; |
| 4493 | |
| 4494 | /* If the user wants to "ignore" eof, then let her do so, kind of. */ |
| 4495 | if (ignoreeof) |
| 4496 | { |
| 4497 | if (eof_encountered < eof_encountered_limit) |
| 4498 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4499 | fprintf (stderr, _("Use \"%s\" to leave the shell.\n"), |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4500 | login_shell ? "logout" : "exit"); |
| 4501 | eof_encountered++; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4502 | /* Reset the parsing state. */ |
| 4503 | last_read_token = current_token = '\n'; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4504 | /* Reset the prompt string to be $PS1. */ |
| 4505 | prompt_string_pointer = (char **)NULL; |
| 4506 | prompt_again (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4507 | return; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4508 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
| 4511 | /* In this case EOF should exit the shell. Do it now. */ |
| 4512 | reset_parser (); |
| 4513 | exit_builtin ((WORD_LIST *)NULL); |
| 4514 | } |
| 4515 | else |
| 4516 | { |
| 4517 | /* We don't write history files, etc., for non-interactive shells. */ |
| 4518 | EOF_Reached = 1; |
| 4519 | } |
| 4520 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4521 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4522 | /************************************************ |
| 4523 | * * |
| 4524 | * STRING PARSING FUNCTIONS * |
| 4525 | * * |
| 4526 | ************************************************/ |
| 4527 | |
| 4528 | /* It's very important that these two functions treat the characters |
| 4529 | between ( and ) identically. */ |
| 4530 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4531 | static WORD_LIST parse_string_error; |
| 4532 | |
| 4533 | /* Take a string and run it through the shell parser, returning the |
| 4534 | resultant word list. Used by compound array assignment. */ |
| 4535 | WORD_LIST * |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4536 | parse_string_to_word_list (s, flags, whom) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4537 | char *s; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4538 | int flags; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4539 | const char *whom; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4540 | { |
| 4541 | WORD_LIST *wl; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4542 | int tok, orig_current_token, orig_line_number, orig_input_terminator; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4543 | int orig_line_count; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4544 | int old_echo_input, old_expand_aliases; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4545 | #if defined (HISTORY) |
| 4546 | int old_remember_on_history, old_history_expansion_inhibited; |
| 4547 | #endif |
| 4548 | |
| 4549 | #if defined (HISTORY) |
| 4550 | old_remember_on_history = remember_on_history; |
| 4551 | # if defined (BANG_HISTORY) |
| 4552 | old_history_expansion_inhibited = history_expansion_inhibited; |
| 4553 | # endif |
| 4554 | bash_history_disable (); |
| 4555 | #endif |
| 4556 | |
| 4557 | orig_line_number = line_number; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4558 | orig_line_count = current_command_line_count; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4559 | orig_input_terminator = shell_input_line_terminator; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4560 | old_echo_input = echo_input_at_read; |
| 4561 | old_expand_aliases = expand_aliases; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4562 | |
| 4563 | push_stream (1); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4564 | last_read_token = WORD; /* WORD to allow reserved words here */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4565 | current_command_line_count = 0; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4566 | echo_input_at_read = expand_aliases = 0; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4567 | |
| 4568 | with_input_from_string (s, whom); |
| 4569 | wl = (WORD_LIST *)NULL; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4570 | |
| 4571 | if (flags & 1) |
| 4572 | parser_state |= PST_COMPASSIGN; |
| 4573 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4574 | while ((tok = read_token (READ)) != yacc_EOF) |
| 4575 | { |
| 4576 | if (tok == '\n' && *bash_input.location.string == '\0') |
| 4577 | break; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4578 | if (tok == '\n') /* Allow newlines in compound assignments */ |
| 4579 | continue; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4580 | if (tok != WORD && tok != ASSIGNMENT_WORD) |
| 4581 | { |
| 4582 | line_number = orig_line_number + line_number - 1; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4583 | orig_current_token = current_token; |
| 4584 | current_token = tok; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4585 | yyerror ((char *)NULL); /* does the right thing */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4586 | current_token = orig_current_token; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4587 | if (wl) |
| 4588 | dispose_words (wl); |
| 4589 | wl = &parse_string_error; |
| 4590 | break; |
| 4591 | } |
| 4592 | wl = make_word_list (yylval.word, wl); |
| 4593 | } |
| 4594 | |
| 4595 | last_read_token = '\n'; |
| 4596 | pop_stream (); |
| 4597 | |
| 4598 | #if defined (HISTORY) |
| 4599 | remember_on_history = old_remember_on_history; |
| 4600 | # if defined (BANG_HISTORY) |
| 4601 | history_expansion_inhibited = old_history_expansion_inhibited; |
| 4602 | # endif /* BANG_HISTORY */ |
| 4603 | #endif /* HISTORY */ |
| 4604 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4605 | echo_input_at_read = old_echo_input; |
| 4606 | expand_aliases = old_expand_aliases; |
| 4607 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4608 | current_command_line_count = orig_line_count; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4609 | shell_input_line_terminator = orig_input_terminator; |
| 4610 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4611 | if (flags & 1) |
| 4612 | parser_state &= ~PST_COMPASSIGN; |
| 4613 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4614 | if (wl == &parse_string_error) |
| 4615 | { |
| 4616 | last_command_exit_value = EXECUTION_FAILURE; |
| 4617 | if (interactive_shell == 0 && posixly_correct) |
| 4618 | jump_to_top_level (FORCE_EOF); |
| 4619 | else |
| 4620 | jump_to_top_level (DISCARD); |
| 4621 | } |
| 4622 | |
| 4623 | return (REVERSE_LIST (wl, WORD_LIST *)); |
| 4624 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4625 | |
| 4626 | static char * |
| 4627 | parse_compound_assignment (retlenp) |
| 4628 | int *retlenp; |
| 4629 | { |
| 4630 | WORD_LIST *wl, *rl; |
| 4631 | int tok, orig_line_number, orig_token_size; |
| 4632 | char *saved_token, *ret; |
| 4633 | |
| 4634 | saved_token = token; |
| 4635 | orig_token_size = token_buffer_size; |
| 4636 | orig_line_number = line_number; |
| 4637 | |
| 4638 | last_read_token = WORD; /* WORD to allow reserved words here */ |
| 4639 | |
| 4640 | token = (char *)NULL; |
| 4641 | token_buffer_size = 0; |
| 4642 | |
| 4643 | wl = (WORD_LIST *)NULL; /* ( */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4644 | parser_state |= PST_COMPASSIGN; |
| 4645 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4646 | while ((tok = read_token (READ)) != ')') |
| 4647 | { |
| 4648 | if (tok == '\n') /* Allow newlines in compound assignments */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4649 | { |
| 4650 | if (SHOULD_PROMPT ()) |
| 4651 | prompt_again (); |
| 4652 | continue; |
| 4653 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4654 | if (tok != WORD && tok != ASSIGNMENT_WORD) |
| 4655 | { |
| 4656 | current_token = tok; /* for error reporting */ |
| 4657 | if (tok == yacc_EOF) /* ( */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4658 | parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'")); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4659 | else |
| 4660 | yyerror ((char *)NULL); /* does the right thing */ |
| 4661 | if (wl) |
| 4662 | dispose_words (wl); |
| 4663 | wl = &parse_string_error; |
| 4664 | break; |
| 4665 | } |
| 4666 | wl = make_word_list (yylval.word, wl); |
| 4667 | } |
| 4668 | |
| 4669 | FREE (token); |
| 4670 | token = saved_token; |
| 4671 | token_buffer_size = orig_token_size; |
| 4672 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4673 | parser_state &= ~PST_COMPASSIGN; |
| 4674 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4675 | if (wl == &parse_string_error) |
| 4676 | { |
| 4677 | last_command_exit_value = EXECUTION_FAILURE; |
| 4678 | last_read_token = '\n'; /* XXX */ |
| 4679 | if (interactive_shell == 0 && posixly_correct) |
| 4680 | jump_to_top_level (FORCE_EOF); |
| 4681 | else |
| 4682 | jump_to_top_level (DISCARD); |
| 4683 | } |
| 4684 | |
| 4685 | last_read_token = WORD; |
| 4686 | if (wl) |
| 4687 | { |
| 4688 | rl = REVERSE_LIST (wl, WORD_LIST *); |
| 4689 | ret = string_list (rl); |
| 4690 | dispose_words (rl); |
| 4691 | } |
| 4692 | else |
| 4693 | ret = (char *)NULL; |
| 4694 | |
| 4695 | if (retlenp) |
| 4696 | *retlenp = (ret && *ret) ? strlen (ret) : 0; |
| 4697 | return ret; |
| 4698 | } |
| 4699 | |
| 4700 | /************************************************ |
| 4701 | * * |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4702 | * SAVING AND RESTORING PARTIAL PARSE STATE * |
| 4703 | * * |
| 4704 | ************************************************/ |
| 4705 | |
| 4706 | sh_parser_state_t * |
| 4707 | save_parser_state (ps) |
| 4708 | sh_parser_state_t *ps; |
| 4709 | { |
| 4710 | #if defined (ARRAY_VARS) |
| 4711 | SHELL_VAR *v; |
| 4712 | #endif |
| 4713 | |
| 4714 | if (ps == 0) |
| 4715 | ps = xmalloc (sizeof (sh_parser_state_t)); |
| 4716 | if (ps == 0) |
| 4717 | return ((sh_parser_state_t *)NULL); |
| 4718 | |
| 4719 | ps->parser_state = parser_state; |
| 4720 | ps->token_state = save_token_state (); |
| 4721 | |
| 4722 | ps->input_line_terminator = shell_input_line_terminator; |
| 4723 | ps->eof_encountered = eof_encountered; |
| 4724 | |
| 4725 | ps->current_command_line_count = current_command_line_count; |
| 4726 | |
| 4727 | #if defined (HISTORY) |
| 4728 | ps->remember_on_history = remember_on_history; |
| 4729 | # if defined (BANG_HISTORY) |
| 4730 | ps->history_expansion_inhibited = history_expansion_inhibited; |
| 4731 | # endif |
| 4732 | #endif |
| 4733 | |
| 4734 | ps->last_command_exit_value = last_command_exit_value; |
| 4735 | #if defined (ARRAY_VARS) |
| 4736 | v = find_variable ("PIPESTATUS"); |
| 4737 | if (v && array_p (v) && array_cell (v)) |
| 4738 | ps->pipestatus = array_copy (array_cell (v)); |
| 4739 | else |
| 4740 | ps->pipestatus = (ARRAY *)NULL; |
| 4741 | #endif |
| 4742 | |
| 4743 | ps->last_shell_builtin = last_shell_builtin; |
| 4744 | ps->this_shell_builtin = this_shell_builtin; |
| 4745 | |
| 4746 | ps->expand_aliases = expand_aliases; |
| 4747 | ps->echo_input_at_read = echo_input_at_read; |
| 4748 | |
| 4749 | return (ps); |
| 4750 | } |
| 4751 | |
| 4752 | void |
| 4753 | restore_parser_state (ps) |
| 4754 | sh_parser_state_t *ps; |
| 4755 | { |
| 4756 | #if defined (ARRAY_VARS) |
| 4757 | SHELL_VAR *v; |
| 4758 | #endif |
| 4759 | |
| 4760 | if (ps == 0) |
| 4761 | return; |
| 4762 | |
| 4763 | parser_state = ps->parser_state; |
| 4764 | if (ps->token_state) |
| 4765 | { |
| 4766 | restore_token_state (ps->token_state); |
| 4767 | free (ps->token_state); |
| 4768 | } |
| 4769 | |
| 4770 | shell_input_line_terminator = ps->input_line_terminator; |
| 4771 | eof_encountered = ps->eof_encountered; |
| 4772 | |
| 4773 | current_command_line_count = ps->current_command_line_count; |
| 4774 | |
| 4775 | #if defined (HISTORY) |
| 4776 | remember_on_history = ps->remember_on_history; |
| 4777 | # if defined (BANG_HISTORY) |
| 4778 | history_expansion_inhibited = ps->history_expansion_inhibited; |
| 4779 | # endif |
| 4780 | #endif |
| 4781 | |
| 4782 | last_command_exit_value = ps->last_command_exit_value; |
| 4783 | #if defined (ARRAY_VARS) |
| 4784 | v = find_variable ("PIPESTATUS"); |
| 4785 | if (v && array_p (v) && array_cell (v)) |
| 4786 | { |
| 4787 | array_dispose (array_cell (v)); |
| 4788 | var_setarray (v, ps->pipestatus); |
| 4789 | } |
| 4790 | #endif |
| 4791 | |
| 4792 | last_shell_builtin = ps->last_shell_builtin; |
| 4793 | this_shell_builtin = ps->this_shell_builtin; |
| 4794 | |
| 4795 | expand_aliases = ps->expand_aliases; |
| 4796 | echo_input_at_read = ps->echo_input_at_read; |
| 4797 | } |
| 4798 | |
| 4799 | /************************************************ |
| 4800 | * * |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4801 | * MULTIBYTE CHARACTER HANDLING * |
| 4802 | * * |
| 4803 | ************************************************/ |
| 4804 | |
| 4805 | #if defined (HANDLE_MULTIBYTE) |
| 4806 | static void |
| 4807 | set_line_mbstate () |
| 4808 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4809 | int i, previ, len, c; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4810 | mbstate_t mbs, prevs; |
| 4811 | size_t mbclen; |
| 4812 | |
| 4813 | if (shell_input_line == NULL) |
| 4814 | return; |
| 4815 | len = strlen (shell_input_line); /* XXX - shell_input_line_len ? */ |
| 4816 | FREE (shell_input_line_property); |
| 4817 | shell_input_line_property = (char *)xmalloc (len + 1); |
| 4818 | |
| 4819 | memset (&prevs, '\0', sizeof (mbstate_t)); |
| 4820 | for (i = previ = 0; i < len; i++) |
| 4821 | { |
| 4822 | mbs = prevs; |
| 4823 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4824 | c = shell_input_line[i]; |
| 4825 | if (c == EOF) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4826 | { |
| 4827 | int j; |
| 4828 | for (j = i; j < len; j++) |
| 4829 | shell_input_line_property[j] = 1; |
| 4830 | break; |
| 4831 | } |
| 4832 | |
| 4833 | mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs); |
| 4834 | if (mbclen == 1 || mbclen == (size_t)-1) |
| 4835 | { |
| 4836 | mbclen = 1; |
| 4837 | previ = i + 1; |
| 4838 | } |
| 4839 | else if (mbclen == (size_t)-2) |
| 4840 | mbclen = 0; |
| 4841 | else if (mbclen > 1) |
| 4842 | { |
| 4843 | mbclen = 0; |
| 4844 | previ = i + 1; |
| 4845 | prevs = mbs; |
| 4846 | } |
| 4847 | else |
| 4848 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 4849 | /* XXX - what to do if mbrlen returns 0? (null wide character) */ |
| 4850 | int j; |
| 4851 | for (j = i; j < len; j++) |
| 4852 | shell_input_line_property[j] = 1; |
| 4853 | break; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | shell_input_line_property[i] = mbclen; |
| 4857 | } |
| 4858 | } |
| 4859 | #endif /* HANDLE_MULTIBYTE */ |