Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1 | /* parse.y - Yacc grammar for bash. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3 | /* Copyright (C) 1989-2012 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 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 7 | Bash is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 12 | Bash is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 16 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 17 | You should have received a copy of the GNU General Public License |
| 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
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" |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 48 | #include "typemax.h" /* SIZE_MAX if needed */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 49 | #include "trap.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 50 | #include "flags.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 51 | #include "parser.h" |
| 52 | #include "mailcheck.h" |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 53 | #include "test.h" |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 54 | #include "builtins.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 55 | #include "builtins/common.h" |
| 56 | #include "builtins/builtext.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 57 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 58 | #include "shmbutil.h" |
| 59 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 60 | #if defined (READLINE) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 61 | # include "bashline.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 62 | # include <readline/readline.h> |
| 63 | #endif /* READLINE */ |
| 64 | |
| 65 | #if defined (HISTORY) |
| 66 | # include "bashhist.h" |
| 67 | # include <readline/history.h> |
| 68 | #endif /* HISTORY */ |
| 69 | |
| 70 | #if defined (JOB_CONTROL) |
| 71 | # include "jobs.h" |
| 72 | #endif /* JOB_CONTROL */ |
| 73 | |
| 74 | #if defined (ALIAS) |
| 75 | # include "alias.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 76 | #else |
| 77 | typedef void *alias_t; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 78 | #endif /* ALIAS */ |
| 79 | |
| 80 | #if defined (PROMPT_STRING_DECODE) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 81 | # ifndef _MINIX |
| 82 | # include <sys/param.h> |
| 83 | # endif |
| 84 | # include <time.h> |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 85 | # if defined (TM_IN_SYS_TIME) |
| 86 | # include <sys/types.h> |
| 87 | # include <sys/time.h> |
| 88 | # endif /* TM_IN_SYS_TIME */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 89 | # include "maxpath.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 90 | #endif /* PROMPT_STRING_DECODE */ |
| 91 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 92 | #define RE_READ_TOKEN -99 |
| 93 | #define NO_EXPANSION -100 |
| 94 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 95 | #ifdef DEBUG |
| 96 | # define YYDEBUG 1 |
| 97 | #else |
| 98 | # define YYDEBUG 0 |
| 99 | #endif |
| 100 | |
| 101 | #if defined (HANDLE_MULTIBYTE) |
| 102 | # define last_shell_getc_is_singlebyte \ |
| 103 | ((shell_input_line_index > 1) \ |
| 104 | ? shell_input_line_property[shell_input_line_index - 1] \ |
| 105 | : 1) |
| 106 | # define MBTEST(x) ((x) && last_shell_getc_is_singlebyte) |
| 107 | #else |
| 108 | # define last_shell_getc_is_singlebyte 1 |
| 109 | # define MBTEST(x) ((x)) |
| 110 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 111 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 112 | #if defined (EXTENDED_GLOB) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 113 | extern int extended_glob; |
| 114 | #endif |
| 115 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 116 | extern int eof_encountered; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 117 | extern int no_line_editing, running_under_emacs; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 118 | extern int current_command_number; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 119 | extern int sourcelevel, parse_and_execute_level; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 120 | extern int posixly_correct; |
| 121 | extern int last_command_exit_value; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 122 | extern pid_t last_command_subst_pid; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 123 | extern char *shell_name, *current_host_name; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 124 | extern char *dist_version; |
| 125 | extern int patch_level; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 126 | extern int dump_translatable_strings, dump_po_strings; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 127 | extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 128 | #if defined (BUFFERED_INPUT) |
| 129 | extern int bash_input_fd_changed; |
| 130 | #endif |
| 131 | |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 132 | #include <errno.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 133 | /* **************************************************************** */ |
| 134 | /* */ |
| 135 | /* "Forward" declarations */ |
| 136 | /* */ |
| 137 | /* **************************************************************** */ |
| 138 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 139 | #ifdef DEBUG |
| 140 | static void debug_parser __P((int)); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 141 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 142 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 143 | static int yy_getc __P((void)); |
| 144 | static int yy_ungetc __P((int)); |
| 145 | |
| 146 | #if defined (READLINE) |
| 147 | static int yy_readline_get __P((void)); |
| 148 | static int yy_readline_unget __P((int)); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 149 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 150 | |
| 151 | static int yy_string_get __P((void)); |
| 152 | static int yy_string_unget __P((int)); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 153 | static void rewind_input_string __P((void)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 154 | static int yy_stream_get __P((void)); |
| 155 | static int yy_stream_unget __P((int)); |
| 156 | |
| 157 | static int shell_getc __P((int)); |
| 158 | static void shell_ungetc __P((int)); |
| 159 | static void discard_until __P((int)); |
| 160 | |
| 161 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| 162 | static void push_string __P((char *, int, alias_t *)); |
| 163 | static void pop_string __P((void)); |
| 164 | static void free_string_list __P((void)); |
| 165 | #endif |
| 166 | |
| 167 | static char *read_a_line __P((int)); |
| 168 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 169 | static int reserved_word_acceptable __P((int)); |
| 170 | static int yylex __P((void)); |
Chet Ramey | 90a39f3 | 2014-10-02 09:07:21 -0400 | [diff] [blame] | 171 | |
| 172 | static void push_heredoc __P((REDIRECT *)); |
| 173 | static char *mk_alexpansion __P((char *)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 174 | static int alias_expand_token __P((char *)); |
| 175 | static int time_command_acceptable __P((void)); |
| 176 | static int special_case_tokens __P((char *)); |
| 177 | static int read_token __P((int)); |
| 178 | static char *parse_matched_pair __P((int, int, int, int *, int)); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 179 | static char *parse_comsub __P((int, int, int, int *, int)); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 180 | #if defined (ARRAY_VARS) |
| 181 | static char *parse_compound_assignment __P((int *)); |
| 182 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 183 | #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 184 | static int parse_dparen __P((int)); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 185 | static int parse_arith_cmd __P((char **, int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 186 | #endif |
| 187 | #if defined (COND_COMMAND) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 188 | static void cond_error __P((void)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 189 | static COND_COM *cond_expr __P((void)); |
| 190 | static COND_COM *cond_or __P((void)); |
| 191 | static COND_COM *cond_and __P((void)); |
| 192 | static COND_COM *cond_term __P((void)); |
| 193 | static int cond_skip_newlines __P((void)); |
| 194 | static COMMAND *parse_cond_command __P((void)); |
| 195 | #endif |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 196 | #if defined (ARRAY_VARS) |
| 197 | static int token_is_assignment __P((char *, int)); |
| 198 | static int token_is_ident __P((char *, int)); |
| 199 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 200 | static int read_token_word __P((int)); |
| 201 | static void discard_parser_constructs __P((int)); |
| 202 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 203 | static char *error_token_from_token __P((int)); |
| 204 | static char *error_token_from_text __P((void)); |
| 205 | static void print_offending_line __P((void)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 206 | static void report_syntax_error __P((char *)); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 207 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 208 | static void handle_eof_input_unit __P((void)); |
| 209 | static void prompt_again __P((void)); |
| 210 | #if 0 |
| 211 | static void reset_readline_prompt __P((void)); |
| 212 | #endif |
| 213 | static void print_prompt __P((void)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 214 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 215 | #if defined (HANDLE_MULTIBYTE) |
| 216 | static void set_line_mbstate __P((void)); |
| 217 | static char *shell_input_line_property = NULL; |
| 218 | #else |
| 219 | # define set_line_mbstate() |
| 220 | #endif |
| 221 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 222 | extern int yyerror __P((const char *)); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 223 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 224 | #ifdef DEBUG |
| 225 | extern int yydebug; |
| 226 | #endif |
| 227 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 228 | /* Default prompt strings */ |
| 229 | char *primary_prompt = PPROMPT; |
| 230 | char *secondary_prompt = SPROMPT; |
| 231 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 232 | /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */ |
| 233 | char *ps1_prompt, *ps2_prompt; |
| 234 | |
| 235 | /* Handle on the current prompt string. Indirectly points through |
| 236 | ps1_ or ps2_prompt. */ |
| 237 | char **prompt_string_pointer = (char **)NULL; |
| 238 | char *current_prompt_string; |
| 239 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 240 | /* Non-zero means we expand aliases in commands. */ |
| 241 | int expand_aliases = 0; |
| 242 | |
| 243 | /* If non-zero, the decoded prompt string undergoes parameter and |
| 244 | variable substitution, command substitution, arithmetic substitution, |
| 245 | string expansion, process substitution, and quote removal in |
| 246 | decode_prompt_string. */ |
| 247 | int promptvars = 1; |
| 248 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 249 | /* If non-zero, $'...' and $"..." are expanded when they appear within |
| 250 | a ${...} expansion, even when the expansion appears within double |
| 251 | quotes. */ |
| 252 | int extended_quote = 1; |
| 253 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 254 | /* The number of lines read from input while creating the current command. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 255 | int current_command_line_count; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 256 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 257 | /* The number of lines in a command saved while we run parse_and_execute */ |
| 258 | int saved_command_line_count; |
| 259 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 260 | /* The token that currently denotes the end of parse. */ |
| 261 | int shell_eof_token; |
| 262 | |
| 263 | /* The token currently being read. */ |
| 264 | int current_token; |
| 265 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 266 | /* The current parser state. */ |
| 267 | int parser_state; |
| 268 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 269 | /* Variables to manage the task of reading here documents, because we need to |
| 270 | defer the reading until after a complete command has been collected. */ |
Chet Ramey | 90a39f3 | 2014-10-02 09:07:21 -0400 | [diff] [blame] | 271 | #define HEREDOC_MAX 16 |
| 272 | |
| 273 | static REDIRECT *redir_stack[HEREDOC_MAX]; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 274 | int need_here_doc; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 275 | |
| 276 | /* Where shell input comes from. History expansion is performed on each |
| 277 | line when the shell is interactive. */ |
| 278 | static char *shell_input_line = (char *)NULL; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 279 | static size_t shell_input_line_index; |
| 280 | static size_t shell_input_line_size; /* Amount allocated for shell_input_line. */ |
| 281 | static size_t shell_input_line_len; /* strlen (shell_input_line) */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 282 | |
| 283 | /* Either zero or EOF. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 284 | static int shell_input_line_terminator; |
| 285 | |
| 286 | /* The line number in a script on which a function definition starts. */ |
| 287 | static int function_dstart; |
| 288 | |
| 289 | /* The line number in a script on which a function body starts. */ |
| 290 | static int function_bstart; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 291 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 292 | /* The line number in a script at which an arithmetic for command starts. */ |
| 293 | static int arith_for_lineno; |
| 294 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 295 | /* The decoded prompt string. Used if READLINE is not defined or if |
| 296 | editing is turned off. Analogous to current_readline_prompt. */ |
| 297 | static char *current_decoded_prompt; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 298 | |
| 299 | /* The last read token, or NULL. read_token () uses this for context |
| 300 | checking. */ |
| 301 | static int last_read_token; |
| 302 | |
| 303 | /* The token read prior to last_read_token. */ |
| 304 | static int token_before_that; |
| 305 | |
| 306 | /* The token read prior to token_before_that. */ |
| 307 | static int two_tokens_ago; |
| 308 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 309 | static int global_extglob; |
| 310 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 311 | /* The line number in a script where the word in a `case WORD', `select WORD' |
| 312 | or `for WORD' begins. This is a nested command maximum, since the array |
| 313 | index is decremented after a case, select, or for command is parsed. */ |
| 314 | #define MAX_CASE_NEST 128 |
Chet Ramey | 90a39f3 | 2014-10-02 09:07:21 -0400 | [diff] [blame] | 315 | static int word_lineno[MAX_CASE_NEST+1]; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 316 | static int word_top = -1; |
| 317 | |
| 318 | /* If non-zero, it is the token that we want read_token to return |
| 319 | regardless of what text is (or isn't) present to be read. This |
| 320 | is reset by read_token. If token_to_read == WORD or |
| 321 | ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */ |
| 322 | static int token_to_read; |
| 323 | static WORD_DESC *word_desc_to_read; |
| 324 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 325 | static REDIRECTEE source; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 326 | static REDIRECTEE redir; |
| 327 | %} |
| 328 | |
| 329 | %union { |
| 330 | WORD_DESC *word; /* the word that we read. */ |
| 331 | int number; /* the number that we read. */ |
| 332 | WORD_LIST *word_list; |
| 333 | COMMAND *command; |
| 334 | REDIRECT *redirect; |
| 335 | ELEMENT element; |
| 336 | PATTERN_LIST *pattern; |
| 337 | } |
| 338 | |
| 339 | /* Reserved words. Members of the first group are only recognized |
| 340 | in the case that they are preceded by a list_terminator. Members |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 341 | of the second group are for [[...]] commands. Members of the |
| 342 | third group are recognized only under special circumstances. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 343 | %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION COPROC |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 344 | %token COND_START COND_END COND_ERROR |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 345 | %token IN BANG TIME TIMEOPT TIMEIGN |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 346 | |
| 347 | /* More general tokens. yylex () knows how to make these. */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 348 | %token <word> WORD ASSIGNMENT_WORD REDIR_WORD |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 349 | %token <number> NUMBER |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 350 | %token <word_list> ARITH_CMD ARITH_FOR_EXPRS |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 351 | %token <command> COND_CMD |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 352 | %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND LESS_LESS_LESS |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 353 | %token GREATER_AND SEMI_SEMI SEMI_AND SEMI_SEMI_AND |
| 354 | %token LESS_LESS_MINUS AND_GREATER AND_GREATER_GREATER LESS_GREATER |
| 355 | %token GREATER_BAR BAR_AND |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 356 | |
| 357 | /* The types that the various syntactical units return. */ |
| 358 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 359 | %type <command> inputunit command pipeline pipeline_command |
| 360 | %type <command> list list0 list1 compound_list simple_list simple_list1 |
| 361 | %type <command> simple_command shell_command |
| 362 | %type <command> for_command select_command case_command group_command |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 363 | %type <command> arith_command |
| 364 | %type <command> cond_command |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 365 | %type <command> arith_for_command |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 366 | %type <command> coproc |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 367 | %type <command> function_def function_body if_command elif_clause subshell |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 368 | %type <redirect> redirection redirection_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 369 | %type <element> simple_command_element |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 370 | %type <word_list> word_list pattern |
| 371 | %type <pattern> pattern_list case_clause_sequence case_clause |
| 372 | %type <number> timespec |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 373 | %type <number> list_terminator |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 374 | |
| 375 | %start inputunit |
| 376 | |
| 377 | %left '&' ';' '\n' yacc_EOF |
| 378 | %left AND_AND OR_OR |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 379 | %right '|' BAR_AND |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 380 | %% |
| 381 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 382 | inputunit: simple_list simple_list_terminator |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 383 | { |
| 384 | /* Case of regular command. Discard the error |
| 385 | safety net,and return the command just parsed. */ |
| 386 | global_command = $1; |
| 387 | eof_encountered = 0; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 388 | /* discard_parser_constructs (0); */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 389 | if (parser_state & PST_CMDSUBST) |
| 390 | parser_state |= PST_EOFTOKEN; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 391 | YYACCEPT; |
| 392 | } |
| 393 | | '\n' |
| 394 | { |
| 395 | /* Case of regular command, but not a very |
| 396 | interesting one. Return a NULL command. */ |
| 397 | global_command = (COMMAND *)NULL; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 398 | if (parser_state & PST_CMDSUBST) |
| 399 | parser_state |= PST_EOFTOKEN; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 400 | YYACCEPT; |
| 401 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 402 | | error '\n' |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 403 | { |
| 404 | /* Error during parsing. Return NULL command. */ |
| 405 | global_command = (COMMAND *)NULL; |
| 406 | eof_encountered = 0; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 407 | /* discard_parser_constructs (1); */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 408 | if (interactive && parse_and_execute_level == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 409 | { |
| 410 | YYACCEPT; |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | YYABORT; |
| 415 | } |
| 416 | } |
| 417 | | yacc_EOF |
| 418 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 419 | /* Case of EOF seen by itself. Do ignoreeof or |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 420 | not. */ |
| 421 | global_command = (COMMAND *)NULL; |
| 422 | handle_eof_input_unit (); |
| 423 | YYACCEPT; |
| 424 | } |
| 425 | ; |
| 426 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 427 | word_list: WORD |
| 428 | { $$ = make_word_list ($1, (WORD_LIST *)NULL); } |
| 429 | | word_list WORD |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 430 | { $$ = make_word_list ($2, $1); } |
| 431 | ; |
| 432 | |
| 433 | redirection: '>' WORD |
| 434 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 435 | source.dest = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 436 | redir.filename = $2; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 437 | $$ = make_redirection (source, r_output_direction, redir, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 438 | } |
| 439 | | '<' WORD |
| 440 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 441 | source.dest = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 442 | redir.filename = $2; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 443 | $$ = make_redirection (source, r_input_direction, redir, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 444 | } |
| 445 | | NUMBER '>' WORD |
| 446 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 447 | source.dest = $1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 448 | redir.filename = $3; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 449 | $$ = make_redirection (source, r_output_direction, redir, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 450 | } |
| 451 | | NUMBER '<' WORD |
| 452 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 453 | source.dest = $1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 454 | redir.filename = $3; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 455 | $$ = make_redirection (source, r_input_direction, redir, 0); |
| 456 | } |
| 457 | | REDIR_WORD '>' WORD |
| 458 | { |
| 459 | source.filename = $1; |
| 460 | redir.filename = $3; |
| 461 | $$ = make_redirection (source, r_output_direction, redir, REDIR_VARASSIGN); |
| 462 | } |
| 463 | | REDIR_WORD '<' WORD |
| 464 | { |
| 465 | source.filename = $1; |
| 466 | redir.filename = $3; |
| 467 | $$ = make_redirection (source, r_input_direction, redir, REDIR_VARASSIGN); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 468 | } |
| 469 | | GREATER_GREATER WORD |
| 470 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 471 | source.dest = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 472 | redir.filename = $2; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 473 | $$ = make_redirection (source, r_appending_to, redir, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 474 | } |
| 475 | | NUMBER GREATER_GREATER WORD |
| 476 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 477 | source.dest = $1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 478 | redir.filename = $3; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 479 | $$ = make_redirection (source, r_appending_to, redir, 0); |
| 480 | } |
| 481 | | REDIR_WORD GREATER_GREATER WORD |
| 482 | { |
| 483 | source.filename = $1; |
| 484 | redir.filename = $3; |
| 485 | $$ = make_redirection (source, r_appending_to, redir, REDIR_VARASSIGN); |
| 486 | } |
| 487 | | GREATER_BAR WORD |
| 488 | { |
| 489 | source.dest = 1; |
| 490 | redir.filename = $2; |
| 491 | $$ = make_redirection (source, r_output_force, redir, 0); |
| 492 | } |
| 493 | | NUMBER GREATER_BAR WORD |
| 494 | { |
| 495 | source.dest = $1; |
| 496 | redir.filename = $3; |
| 497 | $$ = make_redirection (source, r_output_force, redir, 0); |
| 498 | } |
| 499 | | REDIR_WORD GREATER_BAR WORD |
| 500 | { |
| 501 | source.filename = $1; |
| 502 | redir.filename = $3; |
| 503 | $$ = make_redirection (source, r_output_force, redir, REDIR_VARASSIGN); |
| 504 | } |
| 505 | | LESS_GREATER WORD |
| 506 | { |
| 507 | source.dest = 0; |
| 508 | redir.filename = $2; |
| 509 | $$ = make_redirection (source, r_input_output, redir, 0); |
| 510 | } |
| 511 | | NUMBER LESS_GREATER WORD |
| 512 | { |
| 513 | source.dest = $1; |
| 514 | redir.filename = $3; |
| 515 | $$ = make_redirection (source, r_input_output, redir, 0); |
| 516 | } |
| 517 | | REDIR_WORD LESS_GREATER WORD |
| 518 | { |
| 519 | source.filename = $1; |
| 520 | redir.filename = $3; |
| 521 | $$ = make_redirection (source, r_input_output, redir, REDIR_VARASSIGN); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 522 | } |
| 523 | | LESS_LESS WORD |
| 524 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 525 | source.dest = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 526 | redir.filename = $2; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 527 | $$ = make_redirection (source, r_reading_until, redir, 0); |
Chet Ramey | 90a39f3 | 2014-10-02 09:07:21 -0400 | [diff] [blame] | 528 | push_heredoc ($$); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 529 | } |
| 530 | | NUMBER LESS_LESS WORD |
| 531 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 532 | source.dest = $1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 533 | redir.filename = $3; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 534 | $$ = make_redirection (source, r_reading_until, redir, 0); |
Chet Ramey | 90a39f3 | 2014-10-02 09:07:21 -0400 | [diff] [blame] | 535 | push_heredoc ($$); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 536 | } |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 537 | | REDIR_WORD LESS_LESS WORD |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 538 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 539 | source.filename = $1; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 540 | redir.filename = $3; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 541 | $$ = make_redirection (source, r_reading_until, redir, REDIR_VARASSIGN); |
Chet Ramey | 90a39f3 | 2014-10-02 09:07:21 -0400 | [diff] [blame] | 542 | push_heredoc ($$); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 543 | } |
| 544 | | LESS_LESS_MINUS WORD |
| 545 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 546 | source.dest = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 547 | redir.filename = $2; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 548 | $$ = make_redirection (source, r_deblank_reading_until, redir, 0); |
Chet Ramey | 90a39f3 | 2014-10-02 09:07:21 -0400 | [diff] [blame] | 549 | push_heredoc ($$); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 550 | } |
| 551 | | NUMBER LESS_LESS_MINUS WORD |
| 552 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 553 | source.dest = $1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 554 | redir.filename = $3; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 555 | $$ = make_redirection (source, r_deblank_reading_until, redir, 0); |
Chet Ramey | 90a39f3 | 2014-10-02 09:07:21 -0400 | [diff] [blame] | 556 | push_heredoc ($$); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 557 | } |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 558 | | REDIR_WORD LESS_LESS_MINUS WORD |
| 559 | { |
| 560 | source.filename = $1; |
| 561 | redir.filename = $3; |
| 562 | $$ = make_redirection (source, r_deblank_reading_until, redir, REDIR_VARASSIGN); |
Chet Ramey | 90a39f3 | 2014-10-02 09:07:21 -0400 | [diff] [blame] | 563 | push_heredoc ($$); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 564 | } |
| 565 | | LESS_LESS_LESS WORD |
| 566 | { |
| 567 | source.dest = 0; |
| 568 | redir.filename = $2; |
| 569 | $$ = make_redirection (source, r_reading_string, redir, 0); |
| 570 | } |
| 571 | | NUMBER LESS_LESS_LESS WORD |
| 572 | { |
| 573 | source.dest = $1; |
| 574 | redir.filename = $3; |
| 575 | $$ = make_redirection (source, r_reading_string, redir, 0); |
| 576 | } |
| 577 | | REDIR_WORD LESS_LESS_LESS WORD |
| 578 | { |
| 579 | source.filename = $1; |
| 580 | redir.filename = $3; |
| 581 | $$ = make_redirection (source, r_reading_string, redir, REDIR_VARASSIGN); |
| 582 | } |
| 583 | | LESS_AND NUMBER |
| 584 | { |
| 585 | source.dest = 0; |
| 586 | redir.dest = $2; |
| 587 | $$ = make_redirection (source, r_duplicating_input, redir, 0); |
| 588 | } |
| 589 | | NUMBER LESS_AND NUMBER |
| 590 | { |
| 591 | source.dest = $1; |
| 592 | redir.dest = $3; |
| 593 | $$ = make_redirection (source, r_duplicating_input, redir, 0); |
| 594 | } |
| 595 | | REDIR_WORD LESS_AND NUMBER |
| 596 | { |
| 597 | source.filename = $1; |
| 598 | redir.dest = $3; |
| 599 | $$ = make_redirection (source, r_duplicating_input, redir, REDIR_VARASSIGN); |
| 600 | } |
| 601 | | GREATER_AND NUMBER |
| 602 | { |
| 603 | source.dest = 1; |
| 604 | redir.dest = $2; |
| 605 | $$ = make_redirection (source, r_duplicating_output, redir, 0); |
| 606 | } |
| 607 | | NUMBER GREATER_AND NUMBER |
| 608 | { |
| 609 | source.dest = $1; |
| 610 | redir.dest = $3; |
| 611 | $$ = make_redirection (source, r_duplicating_output, redir, 0); |
| 612 | } |
| 613 | | REDIR_WORD GREATER_AND NUMBER |
| 614 | { |
| 615 | source.filename = $1; |
| 616 | redir.dest = $3; |
| 617 | $$ = make_redirection (source, r_duplicating_output, redir, REDIR_VARASSIGN); |
| 618 | } |
| 619 | | LESS_AND WORD |
| 620 | { |
| 621 | source.dest = 0; |
| 622 | redir.filename = $2; |
| 623 | $$ = make_redirection (source, r_duplicating_input_word, redir, 0); |
| 624 | } |
| 625 | | NUMBER LESS_AND WORD |
| 626 | { |
| 627 | source.dest = $1; |
| 628 | redir.filename = $3; |
| 629 | $$ = make_redirection (source, r_duplicating_input_word, redir, 0); |
| 630 | } |
| 631 | | REDIR_WORD LESS_AND WORD |
| 632 | { |
| 633 | source.filename = $1; |
| 634 | redir.filename = $3; |
| 635 | $$ = make_redirection (source, r_duplicating_input_word, redir, REDIR_VARASSIGN); |
| 636 | } |
| 637 | | GREATER_AND WORD |
| 638 | { |
| 639 | source.dest = 1; |
| 640 | redir.filename = $2; |
| 641 | $$ = make_redirection (source, r_duplicating_output_word, redir, 0); |
| 642 | } |
| 643 | | NUMBER GREATER_AND WORD |
| 644 | { |
| 645 | source.dest = $1; |
| 646 | redir.filename = $3; |
| 647 | $$ = make_redirection (source, r_duplicating_output_word, redir, 0); |
| 648 | } |
| 649 | | REDIR_WORD GREATER_AND WORD |
| 650 | { |
| 651 | source.filename = $1; |
| 652 | redir.filename = $3; |
| 653 | $$ = make_redirection (source, r_duplicating_output_word, redir, REDIR_VARASSIGN); |
| 654 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 655 | | GREATER_AND '-' |
| 656 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 657 | source.dest = 1; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 658 | redir.dest = 0; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 659 | $$ = make_redirection (source, r_close_this, redir, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 660 | } |
| 661 | | NUMBER GREATER_AND '-' |
| 662 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 663 | source.dest = $1; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 664 | redir.dest = 0; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 665 | $$ = make_redirection (source, r_close_this, redir, 0); |
| 666 | } |
| 667 | | REDIR_WORD GREATER_AND '-' |
| 668 | { |
| 669 | source.filename = $1; |
| 670 | redir.dest = 0; |
| 671 | $$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 672 | } |
| 673 | | LESS_AND '-' |
| 674 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 675 | source.dest = 0; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 676 | redir.dest = 0; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 677 | $$ = make_redirection (source, r_close_this, redir, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 678 | } |
| 679 | | NUMBER LESS_AND '-' |
| 680 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 681 | source.dest = $1; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 682 | redir.dest = 0; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 683 | $$ = make_redirection (source, r_close_this, redir, 0); |
| 684 | } |
| 685 | | REDIR_WORD LESS_AND '-' |
| 686 | { |
| 687 | source.filename = $1; |
| 688 | redir.dest = 0; |
| 689 | $$ = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 690 | } |
| 691 | | AND_GREATER WORD |
| 692 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 693 | source.dest = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 694 | redir.filename = $2; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 695 | $$ = make_redirection (source, r_err_and_out, redir, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 696 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 697 | | AND_GREATER_GREATER WORD |
| 698 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 699 | source.dest = 1; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 700 | redir.filename = $2; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 701 | $$ = make_redirection (source, r_append_err_and_out, redir, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 702 | } |
| 703 | ; |
| 704 | |
| 705 | simple_command_element: WORD |
| 706 | { $$.word = $1; $$.redirect = 0; } |
| 707 | | ASSIGNMENT_WORD |
| 708 | { $$.word = $1; $$.redirect = 0; } |
| 709 | | redirection |
| 710 | { $$.redirect = $1; $$.word = 0; } |
| 711 | ; |
| 712 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 713 | redirection_list: redirection |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 714 | { |
| 715 | $$ = $1; |
| 716 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 717 | | redirection_list redirection |
| 718 | { |
| 719 | register REDIRECT *t; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 720 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 721 | for (t = $1; t->next; t = t->next) |
| 722 | ; |
| 723 | t->next = $2; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 724 | $$ = $1; |
| 725 | } |
| 726 | ; |
| 727 | |
| 728 | simple_command: simple_command_element |
| 729 | { $$ = make_simple_command ($1, (COMMAND *)NULL); } |
| 730 | | simple_command simple_command_element |
| 731 | { $$ = make_simple_command ($2, $1); } |
| 732 | ; |
| 733 | |
| 734 | command: simple_command |
| 735 | { $$ = clean_simple_command ($1); } |
| 736 | | shell_command |
| 737 | { $$ = $1; } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 738 | | shell_command redirection_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 739 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 740 | COMMAND *tc; |
| 741 | |
| 742 | tc = $1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 743 | if (tc->redirects) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 744 | { |
| 745 | register REDIRECT *t; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 746 | for (t = tc->redirects; t->next; t = t->next) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 747 | ; |
| 748 | t->next = $2; |
| 749 | } |
| 750 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 751 | tc->redirects = $2; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 752 | $$ = $1; |
| 753 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 754 | | function_def |
| 755 | { $$ = $1; } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 756 | | coproc |
| 757 | { $$ = $1; } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 758 | ; |
| 759 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 760 | shell_command: for_command |
| 761 | { $$ = $1; } |
| 762 | | case_command |
| 763 | { $$ = $1; } |
| 764 | | WHILE compound_list DO compound_list DONE |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 765 | { $$ = make_while_command ($2, $4); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 766 | | UNTIL compound_list DO compound_list DONE |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 767 | { $$ = make_until_command ($2, $4); } |
| 768 | | select_command |
| 769 | { $$ = $1; } |
| 770 | | if_command |
| 771 | { $$ = $1; } |
| 772 | | subshell |
| 773 | { $$ = $1; } |
| 774 | | group_command |
| 775 | { $$ = $1; } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 776 | | arith_command |
| 777 | { $$ = $1; } |
| 778 | | cond_command |
| 779 | { $$ = $1; } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 780 | | arith_for_command |
| 781 | { $$ = $1; } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 782 | ; |
| 783 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 784 | for_command: FOR WORD newline_list DO compound_list DONE |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 785 | { |
| 786 | $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]); |
| 787 | if (word_top > 0) word_top--; |
| 788 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 789 | | FOR WORD newline_list '{' compound_list '}' |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 790 | { |
| 791 | $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]); |
| 792 | if (word_top > 0) word_top--; |
| 793 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 794 | | FOR WORD ';' newline_list DO compound_list DONE |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 795 | { |
| 796 | $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]); |
| 797 | if (word_top > 0) word_top--; |
| 798 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 799 | | FOR WORD ';' newline_list '{' compound_list '}' |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 800 | { |
| 801 | $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]); |
| 802 | if (word_top > 0) word_top--; |
| 803 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 804 | | 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] | 805 | { |
| 806 | $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]); |
| 807 | if (word_top > 0) word_top--; |
| 808 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 809 | | 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] | 810 | { |
| 811 | $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]); |
| 812 | if (word_top > 0) word_top--; |
| 813 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 814 | | 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] | 815 | { |
| 816 | $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]); |
| 817 | if (word_top > 0) word_top--; |
| 818 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 819 | | FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}' |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 820 | { |
| 821 | $$ = make_for_command ($2, (WORD_LIST *)NULL, $8, word_lineno[word_top]); |
| 822 | if (word_top > 0) word_top--; |
| 823 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 824 | ; |
| 825 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 826 | 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] | 827 | { |
| 828 | $$ = make_arith_for_command ($2, $6, arith_for_lineno); |
| 829 | if (word_top > 0) word_top--; |
| 830 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 831 | | FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}' |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 832 | { |
| 833 | $$ = make_arith_for_command ($2, $6, arith_for_lineno); |
| 834 | if (word_top > 0) word_top--; |
| 835 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 836 | | FOR ARITH_FOR_EXPRS DO compound_list DONE |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 837 | { |
| 838 | $$ = make_arith_for_command ($2, $4, arith_for_lineno); |
| 839 | if (word_top > 0) word_top--; |
| 840 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 841 | | FOR ARITH_FOR_EXPRS '{' compound_list '}' |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 842 | { |
| 843 | $$ = make_arith_for_command ($2, $4, arith_for_lineno); |
| 844 | if (word_top > 0) word_top--; |
| 845 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 846 | ; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 847 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 848 | select_command: SELECT WORD newline_list DO list DONE |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 849 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 850 | $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]); |
| 851 | if (word_top > 0) word_top--; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 852 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 853 | | SELECT WORD newline_list '{' list '}' |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 854 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 855 | $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]); |
| 856 | if (word_top > 0) word_top--; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 857 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 858 | | SELECT WORD ';' newline_list DO list DONE |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 859 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 860 | $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]); |
| 861 | if (word_top > 0) word_top--; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 862 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 863 | | SELECT WORD ';' newline_list '{' list '}' |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 864 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 865 | $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]); |
| 866 | if (word_top > 0) word_top--; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 867 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 868 | | 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] | 869 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 870 | $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]); |
| 871 | if (word_top > 0) word_top--; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 872 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 873 | | SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}' |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 874 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 875 | $$ = make_select_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9, word_lineno[word_top]); |
| 876 | if (word_top > 0) word_top--; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 877 | } |
| 878 | ; |
| 879 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 880 | case_command: CASE WORD newline_list IN newline_list ESAC |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 881 | { |
| 882 | $$ = make_case_command ($2, (PATTERN_LIST *)NULL, word_lineno[word_top]); |
| 883 | if (word_top > 0) word_top--; |
| 884 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 885 | | CASE WORD newline_list IN case_clause_sequence newline_list ESAC |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 886 | { |
| 887 | $$ = make_case_command ($2, $5, word_lineno[word_top]); |
| 888 | if (word_top > 0) word_top--; |
| 889 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 890 | | CASE WORD newline_list IN case_clause ESAC |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 891 | { |
| 892 | $$ = make_case_command ($2, $5, word_lineno[word_top]); |
| 893 | if (word_top > 0) word_top--; |
| 894 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 895 | ; |
| 896 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 897 | function_def: WORD '(' ')' newline_list function_body |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 898 | { $$ = make_function_def ($1, $5, function_dstart, function_bstart); } |
| 899 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 900 | | FUNCTION WORD '(' ')' newline_list function_body |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 901 | { $$ = make_function_def ($2, $6, function_dstart, function_bstart); } |
| 902 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 903 | | FUNCTION WORD newline_list function_body |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 904 | { $$ = make_function_def ($2, $4, function_dstart, function_bstart); } |
| 905 | ; |
| 906 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 907 | function_body: shell_command |
| 908 | { $$ = $1; } |
| 909 | | shell_command redirection_list |
| 910 | { |
| 911 | COMMAND *tc; |
| 912 | |
| 913 | tc = $1; |
| 914 | /* According to Posix.2 3.9.5, redirections |
| 915 | specified after the body of a function should |
| 916 | be attached to the function and performed when |
| 917 | the function is executed, not as part of the |
| 918 | function definition command. */ |
| 919 | /* XXX - I don't think it matters, but we might |
| 920 | want to change this in the future to avoid |
| 921 | problems differentiating between a function |
| 922 | definition with a redirection and a function |
| 923 | definition containing a single command with a |
| 924 | redirection. The two are semantically equivalent, |
| 925 | though -- the only difference is in how the |
| 926 | command printing code displays the redirections. */ |
| 927 | if (tc->redirects) |
| 928 | { |
| 929 | register REDIRECT *t; |
| 930 | for (t = tc->redirects; t->next; t = t->next) |
| 931 | ; |
| 932 | t->next = $2; |
| 933 | } |
| 934 | else |
| 935 | tc->redirects = $2; |
| 936 | $$ = $1; |
| 937 | } |
| 938 | ; |
| 939 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 940 | subshell: '(' compound_list ')' |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 941 | { |
| 942 | $$ = make_subshell_command ($2); |
| 943 | $$->flags |= CMD_WANT_SUBSHELL; |
| 944 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 945 | ; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 946 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 947 | coproc: COPROC shell_command |
| 948 | { |
| 949 | $$ = make_coproc_command ("COPROC", $2); |
| 950 | $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL; |
| 951 | } |
| 952 | | COPROC shell_command redirection_list |
| 953 | { |
| 954 | COMMAND *tc; |
| 955 | |
| 956 | tc = $2; |
| 957 | if (tc->redirects) |
| 958 | { |
| 959 | register REDIRECT *t; |
| 960 | for (t = tc->redirects; t->next; t = t->next) |
| 961 | ; |
| 962 | t->next = $3; |
| 963 | } |
| 964 | else |
| 965 | tc->redirects = $3; |
| 966 | $$ = make_coproc_command ("COPROC", $2); |
| 967 | $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL; |
| 968 | } |
| 969 | | COPROC WORD shell_command |
| 970 | { |
| 971 | $$ = make_coproc_command ($2->word, $3); |
| 972 | $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL; |
| 973 | } |
| 974 | | COPROC WORD shell_command redirection_list |
| 975 | { |
| 976 | COMMAND *tc; |
| 977 | |
| 978 | tc = $3; |
| 979 | if (tc->redirects) |
| 980 | { |
| 981 | register REDIRECT *t; |
| 982 | for (t = tc->redirects; t->next; t = t->next) |
| 983 | ; |
| 984 | t->next = $4; |
| 985 | } |
| 986 | else |
| 987 | tc->redirects = $4; |
| 988 | $$ = make_coproc_command ($2->word, $3); |
| 989 | $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL; |
| 990 | } |
| 991 | | COPROC simple_command |
| 992 | { |
| 993 | $$ = make_coproc_command ("COPROC", clean_simple_command ($2)); |
| 994 | $$->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL; |
| 995 | } |
| 996 | ; |
| 997 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 998 | if_command: IF compound_list THEN compound_list FI |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 999 | { $$ = make_if_command ($2, $4, (COMMAND *)NULL); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1000 | | IF compound_list THEN compound_list ELSE compound_list FI |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1001 | { $$ = make_if_command ($2, $4, $6); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1002 | | IF compound_list THEN compound_list elif_clause FI |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1003 | { $$ = make_if_command ($2, $4, $5); } |
| 1004 | ; |
| 1005 | |
| 1006 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1007 | group_command: '{' compound_list '}' |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1008 | { $$ = make_group_command ($2); } |
| 1009 | ; |
| 1010 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1011 | arith_command: ARITH_CMD |
| 1012 | { $$ = make_arith_command ($1); } |
| 1013 | ; |
| 1014 | |
| 1015 | cond_command: COND_START COND_CMD COND_END |
| 1016 | { $$ = $2; } |
| 1017 | ; |
| 1018 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1019 | elif_clause: ELIF compound_list THEN compound_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1020 | { $$ = make_if_command ($2, $4, (COMMAND *)NULL); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1021 | | ELIF compound_list THEN compound_list ELSE compound_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1022 | { $$ = make_if_command ($2, $4, $6); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1023 | | ELIF compound_list THEN compound_list elif_clause |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1024 | { $$ = make_if_command ($2, $4, $5); } |
| 1025 | ; |
| 1026 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1027 | case_clause: pattern_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1028 | | case_clause_sequence pattern_list |
| 1029 | { $2->next = $1; $$ = $2; } |
| 1030 | ; |
| 1031 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1032 | pattern_list: newline_list pattern ')' compound_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1033 | { $$ = make_pattern_list ($2, $4); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1034 | | newline_list pattern ')' newline_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1035 | { $$ = make_pattern_list ($2, (COMMAND *)NULL); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1036 | | newline_list '(' pattern ')' compound_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1037 | { $$ = make_pattern_list ($3, $5); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1038 | | newline_list '(' pattern ')' newline_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1039 | { $$ = make_pattern_list ($3, (COMMAND *)NULL); } |
| 1040 | ; |
| 1041 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1042 | case_clause_sequence: pattern_list SEMI_SEMI |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1043 | { $$ = $1; } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1044 | | case_clause_sequence pattern_list SEMI_SEMI |
| 1045 | { $2->next = $1; $$ = $2; } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1046 | | pattern_list SEMI_AND |
| 1047 | { $1->flags |= CASEPAT_FALLTHROUGH; $$ = $1; } |
| 1048 | | case_clause_sequence pattern_list SEMI_AND |
| 1049 | { $2->flags |= CASEPAT_FALLTHROUGH; $2->next = $1; $$ = $2; } |
| 1050 | | pattern_list SEMI_SEMI_AND |
| 1051 | { $1->flags |= CASEPAT_TESTNEXT; $$ = $1; } |
| 1052 | | case_clause_sequence pattern_list SEMI_SEMI_AND |
| 1053 | { $2->flags |= CASEPAT_TESTNEXT; $2->next = $1; $$ = $2; } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1054 | ; |
| 1055 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1056 | pattern: WORD |
| 1057 | { $$ = make_word_list ($1, (WORD_LIST *)NULL); } |
| 1058 | | pattern '|' WORD |
| 1059 | { $$ = make_word_list ($3, $1); } |
| 1060 | ; |
| 1061 | |
| 1062 | /* A list allows leading or trailing newlines and |
| 1063 | newlines as operators (equivalent to semicolons). |
| 1064 | It must end with a newline or semicolon. |
| 1065 | Lists are used within commands such as if, for, while. */ |
| 1066 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1067 | list: newline_list list0 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1068 | { |
| 1069 | $$ = $2; |
| 1070 | if (need_here_doc) |
| 1071 | gather_here_documents (); |
| 1072 | } |
| 1073 | ; |
| 1074 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1075 | compound_list: list |
| 1076 | | newline_list list1 |
| 1077 | { |
| 1078 | $$ = $2; |
| 1079 | } |
| 1080 | ; |
| 1081 | |
| 1082 | list0: list1 '\n' newline_list |
| 1083 | | list1 '&' newline_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1084 | { |
| 1085 | if ($1->type == cm_connection) |
| 1086 | $$ = connect_async_list ($1, (COMMAND *)NULL, '&'); |
| 1087 | else |
| 1088 | $$ = command_connect ($1, (COMMAND *)NULL, '&'); |
| 1089 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1090 | | list1 ';' newline_list |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1091 | |
| 1092 | ; |
| 1093 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1094 | list1: list1 AND_AND newline_list list1 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1095 | { $$ = command_connect ($1, $4, AND_AND); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1096 | | list1 OR_OR newline_list list1 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1097 | { $$ = command_connect ($1, $4, OR_OR); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1098 | | list1 '&' newline_list list1 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1099 | { |
| 1100 | if ($1->type == cm_connection) |
| 1101 | $$ = connect_async_list ($1, $4, '&'); |
| 1102 | else |
| 1103 | $$ = command_connect ($1, $4, '&'); |
| 1104 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1105 | | list1 ';' newline_list list1 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1106 | { $$ = command_connect ($1, $4, ';'); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1107 | | list1 '\n' newline_list list1 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1108 | { $$ = command_connect ($1, $4, ';'); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1109 | | pipeline_command |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1110 | { $$ = $1; } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1111 | ; |
| 1112 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1113 | simple_list_terminator: '\n' |
| 1114 | | yacc_EOF |
| 1115 | ; |
| 1116 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1117 | list_terminator:'\n' |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1118 | { $$ = '\n'; } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1119 | | ';' |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1120 | { $$ = ';'; } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1121 | | yacc_EOF |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1122 | { $$ = yacc_EOF; } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1123 | ; |
| 1124 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1125 | newline_list: |
| 1126 | | newline_list '\n' |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1127 | ; |
| 1128 | |
| 1129 | /* A simple_list is a list that contains no significant newlines |
| 1130 | and no leading or trailing newlines. Newlines are allowed |
| 1131 | only following operators, where they are not significant. |
| 1132 | |
| 1133 | This is what an inputunit consists of. */ |
| 1134 | |
| 1135 | simple_list: simple_list1 |
| 1136 | { |
| 1137 | $$ = $1; |
| 1138 | if (need_here_doc) |
| 1139 | gather_here_documents (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1140 | if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token) |
| 1141 | { |
| 1142 | global_command = $1; |
| 1143 | eof_encountered = 0; |
| 1144 | rewind_input_string (); |
| 1145 | YYACCEPT; |
| 1146 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1147 | } |
| 1148 | | simple_list1 '&' |
| 1149 | { |
| 1150 | if ($1->type == cm_connection) |
| 1151 | $$ = connect_async_list ($1, (COMMAND *)NULL, '&'); |
| 1152 | else |
| 1153 | $$ = command_connect ($1, (COMMAND *)NULL, '&'); |
| 1154 | if (need_here_doc) |
| 1155 | gather_here_documents (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1156 | if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token) |
| 1157 | { |
| 1158 | global_command = $1; |
| 1159 | eof_encountered = 0; |
| 1160 | rewind_input_string (); |
| 1161 | YYACCEPT; |
| 1162 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1163 | } |
| 1164 | | simple_list1 ';' |
| 1165 | { |
| 1166 | $$ = $1; |
| 1167 | if (need_here_doc) |
| 1168 | gather_here_documents (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1169 | if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token) |
| 1170 | { |
| 1171 | global_command = $1; |
| 1172 | eof_encountered = 0; |
| 1173 | rewind_input_string (); |
| 1174 | YYACCEPT; |
| 1175 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1176 | } |
| 1177 | ; |
| 1178 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1179 | simple_list1: simple_list1 AND_AND newline_list simple_list1 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1180 | { $$ = command_connect ($1, $4, AND_AND); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1181 | | simple_list1 OR_OR newline_list simple_list1 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1182 | { $$ = command_connect ($1, $4, OR_OR); } |
| 1183 | | simple_list1 '&' simple_list1 |
| 1184 | { |
| 1185 | if ($1->type == cm_connection) |
| 1186 | $$ = connect_async_list ($1, $3, '&'); |
| 1187 | else |
| 1188 | $$ = command_connect ($1, $3, '&'); |
| 1189 | } |
| 1190 | | simple_list1 ';' simple_list1 |
| 1191 | { $$ = command_connect ($1, $3, ';'); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1192 | |
| 1193 | | pipeline_command |
| 1194 | { $$ = $1; } |
| 1195 | ; |
| 1196 | |
| 1197 | pipeline_command: pipeline |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1198 | { $$ = $1; } |
| 1199 | | BANG pipeline_command |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1200 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1201 | if ($2) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1202 | $2->flags ^= CMD_INVERT_RETURN; /* toggle */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1203 | $$ = $2; |
| 1204 | } |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1205 | | timespec pipeline_command |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1206 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1207 | if ($2) |
| 1208 | $2->flags |= $1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1209 | $$ = $2; |
| 1210 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1211 | | timespec list_terminator |
| 1212 | { |
| 1213 | ELEMENT x; |
| 1214 | |
| 1215 | /* Boy, this is unclean. `time' by itself can |
| 1216 | time a null command. We cheat and push a |
| 1217 | newline back if the list_terminator was a newline |
| 1218 | to avoid the double-newline problem (one to |
| 1219 | terminate this, one to terminate the command) */ |
| 1220 | x.word = 0; |
| 1221 | x.redirect = 0; |
| 1222 | $$ = make_simple_command (x, (COMMAND *)NULL); |
| 1223 | $$->flags |= $1; |
| 1224 | /* XXX - let's cheat and push a newline back */ |
| 1225 | if ($2 == '\n') |
| 1226 | token_to_read = '\n'; |
| 1227 | } |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1228 | | BANG list_terminator |
| 1229 | { |
| 1230 | ELEMENT x; |
| 1231 | |
| 1232 | /* This is just as unclean. Posix says that `!' |
| 1233 | by itself should be equivalent to `false'. |
| 1234 | We cheat and push a |
| 1235 | newline back if the list_terminator was a newline |
| 1236 | to avoid the double-newline problem (one to |
| 1237 | terminate this, one to terminate the command) */ |
| 1238 | x.word = 0; |
| 1239 | x.redirect = 0; |
| 1240 | $$ = make_simple_command (x, (COMMAND *)NULL); |
| 1241 | $$->flags |= CMD_INVERT_RETURN; |
| 1242 | /* XXX - let's cheat and push a newline back */ |
| 1243 | if ($2 == '\n') |
| 1244 | token_to_read = '\n'; |
| 1245 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1246 | ; |
| 1247 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1248 | pipeline: pipeline '|' newline_list pipeline |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1249 | { $$ = command_connect ($1, $4, '|'); } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1250 | | pipeline BAR_AND newline_list pipeline |
| 1251 | { |
| 1252 | /* Make cmd1 |& cmd2 equivalent to cmd1 2>&1 | cmd2 */ |
| 1253 | COMMAND *tc; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1254 | REDIRECTEE rd, sd; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1255 | REDIRECT *r; |
| 1256 | |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 1257 | tc = $1->type == cm_simple ? (COMMAND *)$1->value.Simple : $1; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1258 | sd.dest = 2; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1259 | rd.dest = 1; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1260 | r = make_redirection (sd, r_duplicating_output, rd, 0); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1261 | if (tc->redirects) |
| 1262 | { |
| 1263 | register REDIRECT *t; |
| 1264 | for (t = tc->redirects; t->next; t = t->next) |
| 1265 | ; |
| 1266 | t->next = r; |
| 1267 | } |
| 1268 | else |
| 1269 | tc->redirects = r; |
| 1270 | |
| 1271 | $$ = command_connect ($1, $4, '|'); |
| 1272 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1273 | | command |
| 1274 | { $$ = $1; } |
| 1275 | ; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1276 | |
| 1277 | timespec: TIME |
| 1278 | { $$ = CMD_TIME_PIPELINE; } |
| 1279 | | TIME TIMEOPT |
| 1280 | { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; } |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1281 | | TIME TIMEOPT TIMEIGN |
| 1282 | { $$ = CMD_TIME_PIPELINE|CMD_TIME_POSIX; } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1283 | ; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1284 | %% |
| 1285 | |
| 1286 | /* Initial size to allocate for tokens, and the |
| 1287 | amount to grow them by. */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1288 | #define TOKEN_DEFAULT_INITIAL_SIZE 496 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1289 | #define TOKEN_DEFAULT_GROW_SIZE 512 |
| 1290 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1291 | /* Should we call prompt_again? */ |
| 1292 | #define SHOULD_PROMPT() \ |
| 1293 | (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream)) |
| 1294 | |
| 1295 | #if defined (ALIAS) |
| 1296 | # define expanding_alias() (pushed_string_list && pushed_string_list->expander) |
| 1297 | #else |
| 1298 | # define expanding_alias() 0 |
| 1299 | #endif |
| 1300 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1301 | /* Global var is non-zero when end of file has been reached. */ |
| 1302 | int EOF_Reached = 0; |
| 1303 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1304 | #ifdef DEBUG |
| 1305 | static void |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1306 | debug_parser (i) |
| 1307 | int i; |
| 1308 | { |
| 1309 | #if YYDEBUG != 0 |
| 1310 | yydebug = i; |
| 1311 | #endif |
| 1312 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1313 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1314 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1315 | /* yy_getc () returns the next available character from input or EOF. |
| 1316 | yy_ungetc (c) makes `c' the next character to read. |
| 1317 | init_yy_io (get, unget, type, location) makes the function GET the |
| 1318 | installed function for getting the next character, makes UNGET the |
| 1319 | installed function for un-getting a character, sets the type of stream |
| 1320 | (either string or file) from TYPE, and makes LOCATION point to where |
| 1321 | the input is coming from. */ |
| 1322 | |
| 1323 | /* Unconditionally returns end-of-file. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1324 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1325 | return_EOF () |
| 1326 | { |
| 1327 | return (EOF); |
| 1328 | } |
| 1329 | |
| 1330 | /* Variable containing the current get and unget functions. |
| 1331 | See ./input.h for a clearer description. */ |
| 1332 | BASH_INPUT bash_input; |
| 1333 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1334 | /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it |
| 1335 | is non-null, avoiding a memory leak. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1336 | void |
| 1337 | initialize_bash_input () |
| 1338 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1339 | bash_input.type = st_none; |
| 1340 | FREE (bash_input.name); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1341 | bash_input.name = (char *)NULL; |
| 1342 | bash_input.location.file = (FILE *)NULL; |
| 1343 | bash_input.location.string = (char *)NULL; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1344 | bash_input.getter = (sh_cget_func_t *)NULL; |
| 1345 | bash_input.ungetter = (sh_cunget_func_t *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | /* Set the contents of the current bash input stream from |
| 1349 | GET, UNGET, TYPE, NAME, and LOCATION. */ |
| 1350 | void |
| 1351 | init_yy_io (get, unget, type, name, location) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1352 | sh_cget_func_t *get; |
| 1353 | sh_cunget_func_t *unget; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1354 | enum stream_type type; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1355 | const char *name; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1356 | INPUT_STREAM location; |
| 1357 | { |
| 1358 | bash_input.type = type; |
| 1359 | FREE (bash_input.name); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1360 | bash_input.name = name ? savestring (name) : (char *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1361 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1362 | /* XXX */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1363 | #if defined (CRAY) |
| 1364 | memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location)); |
| 1365 | #else |
| 1366 | bash_input.location = location; |
| 1367 | #endif |
| 1368 | bash_input.getter = get; |
| 1369 | bash_input.ungetter = unget; |
| 1370 | } |
| 1371 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1372 | char * |
| 1373 | yy_input_name () |
| 1374 | { |
| 1375 | return (bash_input.name ? bash_input.name : "stdin"); |
| 1376 | } |
| 1377 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1378 | /* Call this to get the next character of input. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1379 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1380 | yy_getc () |
| 1381 | { |
| 1382 | return (*(bash_input.getter)) (); |
| 1383 | } |
| 1384 | |
| 1385 | /* Call this to unget C. That is, to make C the next character |
| 1386 | to be read. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1387 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1388 | yy_ungetc (c) |
| 1389 | int c; |
| 1390 | { |
| 1391 | return (*(bash_input.ungetter)) (c); |
| 1392 | } |
| 1393 | |
| 1394 | #if defined (BUFFERED_INPUT) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1395 | #ifdef INCLUDE_UNUSED |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1396 | int |
| 1397 | input_file_descriptor () |
| 1398 | { |
| 1399 | switch (bash_input.type) |
| 1400 | { |
| 1401 | case st_stream: |
| 1402 | return (fileno (bash_input.location.file)); |
| 1403 | case st_bstream: |
| 1404 | return (bash_input.location.buffered_fd); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1405 | case st_stdin: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1406 | default: |
| 1407 | return (fileno (stdin)); |
| 1408 | } |
| 1409 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1410 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1411 | #endif /* BUFFERED_INPUT */ |
| 1412 | |
| 1413 | /* **************************************************************** */ |
| 1414 | /* */ |
| 1415 | /* Let input be read from readline (). */ |
| 1416 | /* */ |
| 1417 | /* **************************************************************** */ |
| 1418 | |
| 1419 | #if defined (READLINE) |
| 1420 | char *current_readline_prompt = (char *)NULL; |
| 1421 | char *current_readline_line = (char *)NULL; |
| 1422 | int current_readline_line_index = 0; |
| 1423 | |
| 1424 | static int |
| 1425 | yy_readline_get () |
| 1426 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1427 | SigHandler *old_sigint; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1428 | int line_len; |
| 1429 | unsigned char c; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1430 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1431 | if (!current_readline_line) |
| 1432 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1433 | if (!bash_readline_initialized) |
| 1434 | initialize_readline (); |
| 1435 | |
| 1436 | #if defined (JOB_CONTROL) |
| 1437 | if (job_control) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1438 | give_terminal_to (shell_pgrp, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1439 | #endif /* JOB_CONTROL */ |
| 1440 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1441 | old_sigint = (SigHandler *)IMPOSSIBLE_TRAP_HANDLER; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1442 | if (signal_is_ignored (SIGINT) == 0) |
| 1443 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1444 | /* interrupt_immediately++; */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1445 | old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1448 | current_readline_line = readline (current_readline_prompt ? |
| 1449 | current_readline_prompt : ""); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1450 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1451 | CHECK_TERMSIG; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1452 | if (signal_is_ignored (SIGINT) == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1453 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1454 | /* interrupt_immediately--; */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1455 | if (old_sigint != IMPOSSIBLE_TRAP_HANDLER) |
| 1456 | set_signal_handler (SIGINT, old_sigint); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1459 | #if 0 |
| 1460 | /* Reset the prompt to the decoded value of prompt_string_pointer. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1461 | reset_readline_prompt (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1462 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1463 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1464 | if (current_readline_line == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1465 | return (EOF); |
| 1466 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1467 | current_readline_line_index = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1468 | line_len = strlen (current_readline_line); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1469 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1470 | current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1471 | current_readline_line[line_len++] = '\n'; |
| 1472 | current_readline_line[line_len] = '\0'; |
| 1473 | } |
| 1474 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1475 | if (current_readline_line[current_readline_line_index] == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1476 | { |
| 1477 | free (current_readline_line); |
| 1478 | current_readline_line = (char *)NULL; |
| 1479 | return (yy_readline_get ()); |
| 1480 | } |
| 1481 | else |
| 1482 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1483 | c = current_readline_line[current_readline_line_index++]; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1484 | return (c); |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | static int |
| 1489 | yy_readline_unget (c) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1490 | int c; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1491 | { |
| 1492 | if (current_readline_line_index && current_readline_line) |
| 1493 | current_readline_line[--current_readline_line_index] = c; |
| 1494 | return (c); |
| 1495 | } |
| 1496 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1497 | void |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1498 | with_input_from_stdin () |
| 1499 | { |
| 1500 | INPUT_STREAM location; |
| 1501 | |
| 1502 | if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0) |
| 1503 | { |
| 1504 | location.string = current_readline_line; |
| 1505 | init_yy_io (yy_readline_get, yy_readline_unget, |
| 1506 | st_stdin, "readline stdin", location); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | #else /* !READLINE */ |
| 1511 | |
| 1512 | void |
| 1513 | with_input_from_stdin () |
| 1514 | { |
| 1515 | with_input_from_stream (stdin, "stdin"); |
| 1516 | } |
| 1517 | #endif /* !READLINE */ |
| 1518 | |
| 1519 | /* **************************************************************** */ |
| 1520 | /* */ |
| 1521 | /* Let input come from STRING. STRING is zero terminated. */ |
| 1522 | /* */ |
| 1523 | /* **************************************************************** */ |
| 1524 | |
| 1525 | static int |
| 1526 | yy_string_get () |
| 1527 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1528 | register char *string; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1529 | register unsigned char c; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1530 | |
| 1531 | string = bash_input.location.string; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1532 | |
| 1533 | /* If the string doesn't exist, or is empty, EOF found. */ |
| 1534 | if (string && *string) |
| 1535 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1536 | c = *string++; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1537 | bash_input.location.string = string; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1538 | return (c); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1539 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1540 | else |
| 1541 | return (EOF); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static int |
| 1545 | yy_string_unget (c) |
| 1546 | int c; |
| 1547 | { |
| 1548 | *(--bash_input.location.string) = c; |
| 1549 | return (c); |
| 1550 | } |
| 1551 | |
| 1552 | void |
| 1553 | with_input_from_string (string, name) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1554 | char *string; |
| 1555 | const char *name; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1556 | { |
| 1557 | INPUT_STREAM location; |
| 1558 | |
| 1559 | location.string = string; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1560 | init_yy_io (yy_string_get, yy_string_unget, st_string, name, location); |
| 1561 | } |
| 1562 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1563 | /* Count the number of characters we've consumed from bash_input.location.string |
| 1564 | and read into shell_input_line, but have not returned from shell_getc. |
| 1565 | That is the true input location. Rewind bash_input.location.string by |
| 1566 | that number of characters, so it points to the last character actually |
| 1567 | consumed by the parser. */ |
| 1568 | static void |
| 1569 | rewind_input_string () |
| 1570 | { |
| 1571 | int xchars; |
| 1572 | |
| 1573 | /* number of unconsumed characters in the input -- XXX need to take newlines |
| 1574 | into account, e.g., $(...\n) */ |
| 1575 | xchars = shell_input_line_len - shell_input_line_index; |
| 1576 | if (bash_input.location.string[-1] == '\n') |
| 1577 | xchars++; |
| 1578 | |
| 1579 | /* XXX - how to reflect bash_input.location.string back to string passed to |
| 1580 | parse_and_execute or xparse_dolparen? xparse_dolparen needs to know how |
| 1581 | far into the string we parsed. parse_and_execute knows where bash_input. |
| 1582 | location.string is, and how far from orig_string that is -- that's the |
| 1583 | number of characters the command consumed. */ |
| 1584 | |
| 1585 | /* bash_input.location.string - xchars should be where we parsed to */ |
| 1586 | /* need to do more validation on xchars value for sanity -- test cases. */ |
| 1587 | bash_input.location.string -= xchars; |
| 1588 | } |
| 1589 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1590 | /* **************************************************************** */ |
| 1591 | /* */ |
| 1592 | /* Let input come from STREAM. */ |
| 1593 | /* */ |
| 1594 | /* **************************************************************** */ |
| 1595 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1596 | /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS |
| 1597 | define, and just use getc/ungetc if it was defined, but since bash |
| 1598 | installs its signal handlers without the SA_RESTART flag, some signals |
| 1599 | (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause |
| 1600 | the read to be restarted. We need to restart it ourselves. */ |
| 1601 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1602 | static int |
| 1603 | yy_stream_get () |
| 1604 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1605 | int result; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1606 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1607 | result = EOF; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1608 | if (bash_input.location.file) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1609 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1610 | #if 0 |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1611 | if (interactive) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1612 | interrupt_immediately++; |
| 1613 | #endif |
| 1614 | |
| 1615 | /* XXX - don't need terminate_immediately; getc_with_restart checks |
| 1616 | for terminating signals itself if read returns < 0 */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1617 | result = getc_with_restart (bash_input.location.file); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1618 | |
| 1619 | #if 0 |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1620 | if (interactive) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1621 | interrupt_immediately--; |
| 1622 | #endif |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1623 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1624 | return (result); |
| 1625 | } |
| 1626 | |
| 1627 | static int |
| 1628 | yy_stream_unget (c) |
| 1629 | int c; |
| 1630 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1631 | return (ungetc_with_restart (c, bash_input.location.file)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | void |
| 1635 | with_input_from_stream (stream, name) |
| 1636 | FILE *stream; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1637 | const char *name; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1638 | { |
| 1639 | INPUT_STREAM location; |
| 1640 | |
| 1641 | location.file = stream; |
| 1642 | init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location); |
| 1643 | } |
| 1644 | |
| 1645 | typedef struct stream_saver { |
| 1646 | struct stream_saver *next; |
| 1647 | BASH_INPUT bash_input; |
| 1648 | int line; |
| 1649 | #if defined (BUFFERED_INPUT) |
| 1650 | BUFFERED_STREAM *bstream; |
| 1651 | #endif /* BUFFERED_INPUT */ |
| 1652 | } STREAM_SAVER; |
| 1653 | |
| 1654 | /* The globally known line number. */ |
| 1655 | int line_number = 0; |
| 1656 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1657 | /* The line number offset set by assigning to LINENO. Not currently used. */ |
| 1658 | int line_number_base = 0; |
| 1659 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1660 | #if defined (COND_COMMAND) |
| 1661 | static int cond_lineno; |
| 1662 | static int cond_token; |
| 1663 | #endif |
| 1664 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1665 | STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL; |
| 1666 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1667 | void |
| 1668 | push_stream (reset_lineno) |
| 1669 | int reset_lineno; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1670 | { |
| 1671 | STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER)); |
| 1672 | |
| 1673 | xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT)); |
| 1674 | |
| 1675 | #if defined (BUFFERED_INPUT) |
| 1676 | saver->bstream = (BUFFERED_STREAM *)NULL; |
| 1677 | /* If we have a buffered stream, clear out buffers[fd]. */ |
| 1678 | if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1679 | saver->bstream = set_buffered_stream (bash_input.location.buffered_fd, |
| 1680 | (BUFFERED_STREAM *)NULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1681 | #endif /* BUFFERED_INPUT */ |
| 1682 | |
| 1683 | saver->line = line_number; |
| 1684 | bash_input.name = (char *)NULL; |
| 1685 | saver->next = stream_list; |
| 1686 | stream_list = saver; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1687 | EOF_Reached = 0; |
| 1688 | if (reset_lineno) |
| 1689 | line_number = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1692 | void |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1693 | pop_stream () |
| 1694 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1695 | if (!stream_list) |
| 1696 | EOF_Reached = 1; |
| 1697 | else |
| 1698 | { |
| 1699 | STREAM_SAVER *saver = stream_list; |
| 1700 | |
| 1701 | EOF_Reached = 0; |
| 1702 | stream_list = stream_list->next; |
| 1703 | |
| 1704 | init_yy_io (saver->bash_input.getter, |
| 1705 | saver->bash_input.ungetter, |
| 1706 | saver->bash_input.type, |
| 1707 | saver->bash_input.name, |
| 1708 | saver->bash_input.location); |
| 1709 | |
| 1710 | #if defined (BUFFERED_INPUT) |
| 1711 | /* If we have a buffered stream, restore buffers[fd]. */ |
| 1712 | /* If the input file descriptor was changed while this was on the |
| 1713 | save stack, update the buffered fd to the new file descriptor and |
| 1714 | re-establish the buffer <-> bash_input fd correspondence. */ |
| 1715 | if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1716 | { |
| 1717 | if (bash_input_fd_changed) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1718 | { |
| 1719 | bash_input_fd_changed = 0; |
| 1720 | if (default_buffered_input >= 0) |
| 1721 | { |
| 1722 | bash_input.location.buffered_fd = default_buffered_input; |
| 1723 | saver->bstream->b_fd = default_buffered_input; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1724 | SET_CLOSE_ON_EXEC (default_buffered_input); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1725 | } |
| 1726 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1727 | /* XXX could free buffered stream returned as result here. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1728 | set_buffered_stream (bash_input.location.buffered_fd, saver->bstream); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1729 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1730 | #endif /* BUFFERED_INPUT */ |
| 1731 | |
| 1732 | line_number = saver->line; |
| 1733 | |
| 1734 | FREE (saver->bash_input.name); |
| 1735 | free (saver); |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | /* Return 1 if a stream of type TYPE is saved on the stack. */ |
| 1740 | int |
| 1741 | stream_on_stack (type) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1742 | enum stream_type type; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1743 | { |
| 1744 | register STREAM_SAVER *s; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1745 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1746 | for (s = stream_list; s; s = s->next) |
| 1747 | if (s->bash_input.type == type) |
| 1748 | return 1; |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1752 | /* Save the current token state and return it in a malloced array. */ |
| 1753 | int * |
| 1754 | save_token_state () |
| 1755 | { |
| 1756 | int *ret; |
| 1757 | |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 1758 | ret = (int *)xmalloc (4 * sizeof (int)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1759 | ret[0] = last_read_token; |
| 1760 | ret[1] = token_before_that; |
| 1761 | ret[2] = two_tokens_ago; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 1762 | ret[3] = current_token; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1763 | return ret; |
| 1764 | } |
| 1765 | |
| 1766 | void |
| 1767 | restore_token_state (ts) |
| 1768 | int *ts; |
| 1769 | { |
| 1770 | if (ts == 0) |
| 1771 | return; |
| 1772 | last_read_token = ts[0]; |
| 1773 | token_before_that = ts[1]; |
| 1774 | two_tokens_ago = ts[2]; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 1775 | current_token = ts[3]; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1778 | /* |
| 1779 | * This is used to inhibit alias expansion and reserved word recognition |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1780 | * inside case statement pattern lists. A `case statement pattern list' is: |
| 1781 | * |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1782 | * everything between the `in' in a `case word in' and the next ')' |
| 1783 | * or `esac' |
| 1784 | * everything between a `;;' and the next `)' or `esac' |
| 1785 | */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1786 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1787 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| 1788 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1789 | #define END_OF_ALIAS 0 |
| 1790 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1791 | /* |
| 1792 | * Pseudo-global variables used in implementing token-wise alias expansion. |
| 1793 | */ |
| 1794 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1795 | /* |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1796 | * Pushing and popping strings. This works together with shell_getc to |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1797 | * implement alias expansion on a per-token basis. |
| 1798 | */ |
| 1799 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1800 | #define PSH_ALIAS 0x01 |
| 1801 | #define PSH_DPAREN 0x02 |
| 1802 | #define PSH_SOURCE 0x04 |
| 1803 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1804 | typedef struct string_saver { |
| 1805 | struct string_saver *next; |
| 1806 | int expand_alias; /* Value to set expand_alias to when string is popped. */ |
| 1807 | char *saved_line; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1808 | #if defined (ALIAS) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1809 | alias_t *expander; /* alias that caused this line to be pushed. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1810 | #endif |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1811 | size_t saved_line_size, saved_line_index; |
| 1812 | int saved_line_terminator; |
| 1813 | int flags; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1814 | } STRING_SAVER; |
| 1815 | |
| 1816 | STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL; |
| 1817 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1818 | /* |
| 1819 | * Push the current shell_input_line onto a stack of such lines and make S |
| 1820 | * the current input. Used when expanding aliases. EXPAND is used to set |
| 1821 | * the value of expand_next_token when the string is popped, so that the |
| 1822 | * word after the alias in the original line is handled correctly when the |
| 1823 | * alias expands to multiple words. TOKEN is the token that was expanded |
| 1824 | * into S; it is saved and used to prevent infinite recursive expansion. |
| 1825 | */ |
| 1826 | static void |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1827 | push_string (s, expand, ap) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1828 | char *s; |
| 1829 | int expand; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1830 | alias_t *ap; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1831 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1832 | STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1833 | |
| 1834 | temp->expand_alias = expand; |
| 1835 | temp->saved_line = shell_input_line; |
| 1836 | temp->saved_line_size = shell_input_line_size; |
| 1837 | temp->saved_line_index = shell_input_line_index; |
| 1838 | temp->saved_line_terminator = shell_input_line_terminator; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1839 | temp->flags = 0; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1840 | #if defined (ALIAS) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1841 | temp->expander = ap; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1842 | if (ap) |
| 1843 | temp->flags = PSH_ALIAS; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1844 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1845 | temp->next = pushed_string_list; |
| 1846 | pushed_string_list = temp; |
| 1847 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1848 | #if defined (ALIAS) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1849 | if (ap) |
| 1850 | ap->flags |= AL_BEINGEXPANDED; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1851 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1852 | |
| 1853 | shell_input_line = s; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1854 | shell_input_line_size = STRLEN (s); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1855 | shell_input_line_index = 0; |
| 1856 | shell_input_line_terminator = '\0'; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1857 | #if 0 |
| 1858 | parser_state &= ~PST_ALEXPNEXT; /* XXX */ |
| 1859 | #endif |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1860 | |
| 1861 | set_line_mbstate (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | /* |
| 1865 | * Make the top of the pushed_string stack be the current shell input. |
| 1866 | * Only called when there is something on the stack. Called from shell_getc |
| 1867 | * when it thinks it has consumed the string generated by an alias expansion |
| 1868 | * and needs to return to the original input line. |
| 1869 | */ |
| 1870 | static void |
| 1871 | pop_string () |
| 1872 | { |
| 1873 | STRING_SAVER *t; |
| 1874 | |
| 1875 | FREE (shell_input_line); |
| 1876 | shell_input_line = pushed_string_list->saved_line; |
| 1877 | shell_input_line_index = pushed_string_list->saved_line_index; |
| 1878 | shell_input_line_size = pushed_string_list->saved_line_size; |
| 1879 | shell_input_line_terminator = pushed_string_list->saved_line_terminator; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1880 | |
| 1881 | if (pushed_string_list->expand_alias) |
| 1882 | parser_state |= PST_ALEXPNEXT; |
| 1883 | else |
| 1884 | parser_state &= ~PST_ALEXPNEXT; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1885 | |
| 1886 | t = pushed_string_list; |
| 1887 | pushed_string_list = pushed_string_list->next; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1888 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1889 | #if defined (ALIAS) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1890 | if (t->expander) |
| 1891 | t->expander->flags &= ~AL_BEINGEXPANDED; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1892 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1893 | |
| 1894 | free ((char *)t); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1895 | |
| 1896 | set_line_mbstate (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | static void |
| 1900 | free_string_list () |
| 1901 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1902 | register STRING_SAVER *t, *t1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1903 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1904 | for (t = pushed_string_list; t; ) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1905 | { |
| 1906 | t1 = t->next; |
| 1907 | FREE (t->saved_line); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1908 | #if defined (ALIAS) |
| 1909 | if (t->expander) |
| 1910 | t->expander->flags &= ~AL_BEINGEXPANDED; |
| 1911 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1912 | free ((char *)t); |
| 1913 | t = t1; |
| 1914 | } |
| 1915 | pushed_string_list = (STRING_SAVER *)NULL; |
| 1916 | } |
| 1917 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1918 | #endif /* ALIAS || DPAREN_ARITHMETIC */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1919 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1920 | void |
| 1921 | free_pushed_string_input () |
| 1922 | { |
| 1923 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| 1924 | free_string_list (); |
| 1925 | #endif |
| 1926 | } |
| 1927 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1928 | int |
| 1929 | parser_expanding_alias () |
| 1930 | { |
| 1931 | return (expanding_alias ()); |
| 1932 | } |
| 1933 | |
| 1934 | void |
| 1935 | parser_save_alias () |
| 1936 | { |
| 1937 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| 1938 | push_string ((char *)NULL, 0, (alias_t *)NULL); |
| 1939 | pushed_string_list->flags = PSH_SOURCE; /* XXX - for now */ |
| 1940 | #else |
| 1941 | ; |
| 1942 | #endif |
| 1943 | } |
| 1944 | |
| 1945 | void |
| 1946 | parser_restore_alias () |
| 1947 | { |
| 1948 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| 1949 | if (pushed_string_list) |
| 1950 | pop_string (); |
| 1951 | #else |
| 1952 | ; |
| 1953 | #endif |
| 1954 | } |
| 1955 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1956 | /* Return a line of text, taken from wherever yylex () reads input. |
| 1957 | If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE |
| 1958 | is non-zero, we remove unquoted \<newline> pairs. This is used by |
| 1959 | read_secondary_line to read here documents. */ |
| 1960 | static char * |
| 1961 | read_a_line (remove_quoted_newline) |
| 1962 | int remove_quoted_newline; |
| 1963 | { |
| 1964 | static char *line_buffer = (char *)NULL; |
| 1965 | static int buffer_size = 0; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1966 | int indx, c, peekc, pass_next; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1967 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1968 | #if defined (READLINE) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1969 | if (no_line_editing && SHOULD_PROMPT ()) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1970 | #else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1971 | if (SHOULD_PROMPT ()) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1972 | #endif |
| 1973 | print_prompt (); |
| 1974 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1975 | pass_next = indx = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1976 | while (1) |
| 1977 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1978 | /* Allow immediate exit if interrupted during input. */ |
| 1979 | QUIT; |
| 1980 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1981 | c = yy_getc (); |
| 1982 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1983 | /* Ignore null bytes in input. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1984 | if (c == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1985 | { |
| 1986 | #if 0 |
| 1987 | internal_warning ("read_a_line: ignored null byte in input"); |
| 1988 | #endif |
| 1989 | continue; |
| 1990 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1991 | |
| 1992 | /* If there is no more input, then we return NULL. */ |
| 1993 | if (c == EOF) |
| 1994 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1995 | if (interactive && bash_input.type == st_stream) |
| 1996 | clearerr (stdin); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1997 | if (indx == 0) |
| 1998 | return ((char *)NULL); |
| 1999 | c = '\n'; |
| 2000 | } |
| 2001 | |
| 2002 | /* `+2' in case the final character in the buffer is a newline. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2003 | RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2004 | |
| 2005 | /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a |
| 2006 | here document with an unquoted delimiter. In this case, |
| 2007 | the line will be expanded as if it were in double quotes. |
| 2008 | We allow a backslash to escape the next character, but we |
| 2009 | need to treat the backslash specially only if a backslash |
| 2010 | quoting a backslash-newline pair appears in the line. */ |
| 2011 | if (pass_next) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2012 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2013 | line_buffer[indx++] = c; |
| 2014 | pass_next = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2015 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2016 | else if (c == '\\' && remove_quoted_newline) |
| 2017 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2018 | QUIT; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2019 | peekc = yy_getc (); |
| 2020 | if (peekc == '\n') |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2021 | { |
| 2022 | line_number++; |
| 2023 | continue; /* Make the unquoted \<newline> pair disappear. */ |
| 2024 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2025 | else |
| 2026 | { |
| 2027 | yy_ungetc (peekc); |
| 2028 | pass_next = 1; |
| 2029 | line_buffer[indx++] = c; /* Preserve the backslash. */ |
| 2030 | } |
| 2031 | } |
| 2032 | else |
| 2033 | line_buffer[indx++] = c; |
| 2034 | |
| 2035 | if (c == '\n') |
| 2036 | { |
| 2037 | line_buffer[indx] = '\0'; |
| 2038 | return (line_buffer); |
| 2039 | } |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | /* Return a line as in read_a_line (), but insure that the prompt is |
| 2044 | the secondary prompt. This is used to read the lines of a here |
| 2045 | document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove |
| 2046 | newlines quoted with backslashes while reading the line. It is |
| 2047 | non-zero unless the delimiter of the here document was quoted. */ |
| 2048 | char * |
| 2049 | read_secondary_line (remove_quoted_newline) |
| 2050 | int remove_quoted_newline; |
| 2051 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2052 | char *ret; |
| 2053 | int n, c; |
| 2054 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2055 | prompt_string_pointer = &ps2_prompt; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2056 | if (SHOULD_PROMPT()) |
| 2057 | prompt_again (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2058 | ret = read_a_line (remove_quoted_newline); |
| 2059 | #if defined (HISTORY) |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 2060 | if (ret && remember_on_history && (parser_state & PST_HEREDOC)) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2061 | { |
| 2062 | /* To make adding the the here-document body right, we need to rely |
| 2063 | on history_delimiting_chars() returning \n for the first line of |
| 2064 | the here-document body and the null string for the second and |
| 2065 | subsequent lines, so we avoid double newlines. |
| 2066 | current_command_line_count == 2 for the first line of the body. */ |
| 2067 | |
| 2068 | current_command_line_count++; |
| 2069 | maybe_add_history (ret); |
| 2070 | } |
| 2071 | #endif /* HISTORY */ |
| 2072 | return ret; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2075 | /* **************************************************************** */ |
| 2076 | /* */ |
| 2077 | /* YYLEX () */ |
| 2078 | /* */ |
| 2079 | /* **************************************************************** */ |
| 2080 | |
| 2081 | /* Reserved words. These are only recognized as the first word of a |
| 2082 | command. */ |
| 2083 | STRING_INT_ALIST word_token_alist[] = { |
| 2084 | { "if", IF }, |
| 2085 | { "then", THEN }, |
| 2086 | { "else", ELSE }, |
| 2087 | { "elif", ELIF }, |
| 2088 | { "fi", FI }, |
| 2089 | { "case", CASE }, |
| 2090 | { "esac", ESAC }, |
| 2091 | { "for", FOR }, |
| 2092 | #if defined (SELECT_COMMAND) |
| 2093 | { "select", SELECT }, |
| 2094 | #endif |
| 2095 | { "while", WHILE }, |
| 2096 | { "until", UNTIL }, |
| 2097 | { "do", DO }, |
| 2098 | { "done", DONE }, |
| 2099 | { "in", IN }, |
| 2100 | { "function", FUNCTION }, |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2101 | #if defined (COMMAND_TIMING) |
| 2102 | { "time", TIME }, |
| 2103 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2104 | { "{", '{' }, |
| 2105 | { "}", '}' }, |
| 2106 | { "!", BANG }, |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2107 | #if defined (COND_COMMAND) |
| 2108 | { "[[", COND_START }, |
| 2109 | { "]]", COND_END }, |
| 2110 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2111 | #if defined (COPROCESS_SUPPORT) |
| 2112 | { "coproc", COPROC }, |
| 2113 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2114 | { (char *)NULL, 0} |
| 2115 | }; |
| 2116 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2117 | /* other tokens that can be returned by read_token() */ |
| 2118 | STRING_INT_ALIST other_token_alist[] = { |
| 2119 | /* Multiple-character tokens with special values */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2120 | { "--", TIMEIGN }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2121 | { "-p", TIMEOPT }, |
| 2122 | { "&&", AND_AND }, |
| 2123 | { "||", OR_OR }, |
| 2124 | { ">>", GREATER_GREATER }, |
| 2125 | { "<<", LESS_LESS }, |
| 2126 | { "<&", LESS_AND }, |
| 2127 | { ">&", GREATER_AND }, |
| 2128 | { ";;", SEMI_SEMI }, |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2129 | { ";&", SEMI_AND }, |
| 2130 | { ";;&", SEMI_SEMI_AND }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2131 | { "<<-", LESS_LESS_MINUS }, |
| 2132 | { "<<<", LESS_LESS_LESS }, |
| 2133 | { "&>", AND_GREATER }, |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2134 | { "&>>", AND_GREATER_GREATER }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2135 | { "<>", LESS_GREATER }, |
| 2136 | { ">|", GREATER_BAR }, |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2137 | { "|&", BAR_AND }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2138 | { "EOF", yacc_EOF }, |
| 2139 | /* Tokens whose value is the character itself */ |
| 2140 | { ">", '>' }, |
| 2141 | { "<", '<' }, |
| 2142 | { "-", '-' }, |
| 2143 | { "{", '{' }, |
| 2144 | { "}", '}' }, |
| 2145 | { ";", ';' }, |
| 2146 | { "(", '(' }, |
| 2147 | { ")", ')' }, |
| 2148 | { "|", '|' }, |
| 2149 | { "&", '&' }, |
| 2150 | { "newline", '\n' }, |
| 2151 | { (char *)NULL, 0} |
| 2152 | }; |
| 2153 | |
| 2154 | /* others not listed here: |
| 2155 | WORD look at yylval.word |
| 2156 | ASSIGNMENT_WORD look at yylval.word |
| 2157 | NUMBER look at yylval.number |
| 2158 | ARITH_CMD look at yylval.word_list |
| 2159 | ARITH_FOR_EXPRS look at yylval.word_list |
| 2160 | COND_CMD look at yylval.command |
| 2161 | */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2162 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2163 | /* These are used by read_token_word, but appear up here so that shell_getc |
| 2164 | can use them to decide when to add otherwise blank lines to the history. */ |
| 2165 | |
| 2166 | /* The primary delimiter stack. */ |
| 2167 | struct dstack dstack = { (char *)NULL, 0, 0 }; |
| 2168 | |
| 2169 | /* A temporary delimiter stack to be used when decoding prompt strings. |
| 2170 | This is needed because command substitutions in prompt strings (e.g., PS2) |
| 2171 | can screw up the parser's quoting state. */ |
| 2172 | static struct dstack temp_dstack = { (char *)NULL, 0, 0 }; |
| 2173 | |
| 2174 | /* Macro for accessing the top delimiter on the stack. Returns the |
| 2175 | delimiter or zero if none. */ |
| 2176 | #define current_delimiter(ds) \ |
| 2177 | (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0) |
| 2178 | |
| 2179 | #define push_delimiter(ds, character) \ |
| 2180 | do \ |
| 2181 | { \ |
| 2182 | if (ds.delimiter_depth + 2 > ds.delimiter_space) \ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2183 | ds.delimiters = (char *)xrealloc \ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2184 | (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \ |
| 2185 | ds.delimiters[ds.delimiter_depth] = character; \ |
| 2186 | ds.delimiter_depth++; \ |
| 2187 | } \ |
| 2188 | while (0) |
| 2189 | |
| 2190 | #define pop_delimiter(ds) ds.delimiter_depth-- |
| 2191 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2192 | /* Return the next shell input character. This always reads characters |
| 2193 | from shell_input_line; when that line is exhausted, it is time to |
| 2194 | read the next line. This is called by read_token when the shell is |
| 2195 | processing normal command input. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2196 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2197 | /* This implements one-character lookahead/lookbehind across physical input |
| 2198 | lines, to avoid something being lost because it's pushed back with |
| 2199 | shell_ungetc when we're at the start of a line. */ |
| 2200 | static int eol_ungetc_lookahead = 0; |
| 2201 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2202 | static int |
| 2203 | shell_getc (remove_quoted_newline) |
| 2204 | int remove_quoted_newline; |
| 2205 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2206 | register int i; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2207 | int c, truncating; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2208 | unsigned char uc; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2209 | |
| 2210 | QUIT; |
| 2211 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2212 | if (sigwinch_received) |
| 2213 | { |
| 2214 | sigwinch_received = 0; |
| 2215 | get_new_window_size (0, (int *)0, (int *)0); |
| 2216 | } |
| 2217 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2218 | if (eol_ungetc_lookahead) |
| 2219 | { |
| 2220 | c = eol_ungetc_lookahead; |
| 2221 | eol_ungetc_lookahead = 0; |
| 2222 | return (c); |
| 2223 | } |
| 2224 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2225 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2226 | /* If shell_input_line[shell_input_line_index] == 0, but there is |
| 2227 | something on the pushed list of strings, then we don't want to go |
| 2228 | off and get another line. We let the code down below handle it. */ |
| 2229 | |
| 2230 | if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) && |
| 2231 | (pushed_string_list == (STRING_SAVER *)NULL))) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2232 | #else /* !ALIAS && !DPAREN_ARITHMETIC */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2233 | if (!shell_input_line || !shell_input_line[shell_input_line_index]) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2234 | #endif /* !ALIAS && !DPAREN_ARITHMETIC */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2235 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2236 | line_number++; |
| 2237 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2238 | /* Let's not let one really really long line blow up memory allocation */ |
| 2239 | if (shell_input_line && shell_input_line_size >= 32768) |
| 2240 | { |
| 2241 | free (shell_input_line); |
| 2242 | shell_input_line = 0; |
| 2243 | shell_input_line_size = 0; |
| 2244 | } |
| 2245 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2246 | restart_read: |
| 2247 | |
| 2248 | /* Allow immediate exit if interrupted during input. */ |
| 2249 | QUIT; |
| 2250 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2251 | i = truncating = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2252 | shell_input_line_terminator = 0; |
| 2253 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2254 | /* If the shell is interatctive, but not currently printing a prompt |
| 2255 | (interactive_shell && interactive == 0), we don't want to print |
| 2256 | notifies or cleanup the jobs -- we want to defer it until we do |
| 2257 | print the next prompt. */ |
| 2258 | if (interactive_shell == 0 || SHOULD_PROMPT()) |
| 2259 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2260 | #if defined (JOB_CONTROL) |
| 2261 | /* This can cause a problem when reading a command as the result |
| 2262 | of a trap, when the trap is called from flush_child. This call |
| 2263 | had better not cause jobs to disappear from the job table in |
| 2264 | that case, or we will have big trouble. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2265 | notify_and_cleanup (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2266 | #else /* !JOB_CONTROL */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2267 | cleanup_dead_jobs (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2268 | #endif /* !JOB_CONTROL */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2269 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2270 | |
| 2271 | #if defined (READLINE) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2272 | if (no_line_editing && SHOULD_PROMPT()) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2273 | #else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2274 | if (SHOULD_PROMPT()) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2275 | #endif |
| 2276 | print_prompt (); |
| 2277 | |
| 2278 | if (bash_input.type == st_stream) |
| 2279 | clearerr (stdin); |
| 2280 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2281 | while (1) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2282 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2283 | c = yy_getc (); |
| 2284 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2285 | /* Allow immediate exit if interrupted during input. */ |
| 2286 | QUIT; |
| 2287 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2288 | if (c == '\0') |
| 2289 | { |
| 2290 | #if 0 |
| 2291 | internal_warning ("shell_getc: ignored null byte in input"); |
| 2292 | #endif |
| 2293 | continue; |
| 2294 | } |
| 2295 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2296 | /* Theoretical overflow */ |
| 2297 | /* If we can't put 256 bytes more into the buffer, allocate |
| 2298 | everything we can and fill it as full as we can. */ |
| 2299 | /* XXX - we ignore rest of line using `truncating' flag */ |
| 2300 | if (shell_input_line_size > (SIZE_MAX - 256)) |
| 2301 | { |
| 2302 | size_t n; |
| 2303 | |
| 2304 | n = SIZE_MAX - i; /* how much more can we put into the buffer? */ |
| 2305 | if (n <= 2) /* we have to save 1 for the newline added below */ |
| 2306 | { |
| 2307 | if (truncating == 0) |
| 2308 | internal_warning("shell_getc: shell_input_line_size (%zu) exceeds SIZE_MAX (%llu): line truncated", shell_input_line_size, SIZE_MAX); |
| 2309 | shell_input_line[i] = '\0'; |
| 2310 | truncating = 1; |
| 2311 | } |
| 2312 | if (shell_input_line_size < SIZE_MAX) |
| 2313 | { |
| 2314 | shell_input_line_size = SIZE_MAX; |
| 2315 | shell_input_line = xrealloc (shell_input_line, shell_input_line_size); |
| 2316 | } |
| 2317 | } |
| 2318 | else |
| 2319 | 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] | 2320 | |
| 2321 | if (c == EOF) |
| 2322 | { |
| 2323 | if (bash_input.type == st_stream) |
| 2324 | clearerr (stdin); |
| 2325 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2326 | if (i == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2327 | shell_input_line_terminator = EOF; |
| 2328 | |
| 2329 | shell_input_line[i] = '\0'; |
| 2330 | break; |
| 2331 | } |
| 2332 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2333 | if (truncating == 0 || c == '\n') |
| 2334 | shell_input_line[i++] = c; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2335 | |
| 2336 | if (c == '\n') |
| 2337 | { |
| 2338 | shell_input_line[--i] = '\0'; |
| 2339 | current_command_line_count++; |
| 2340 | break; |
| 2341 | } |
| 2342 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2343 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2344 | shell_input_line_index = 0; |
| 2345 | shell_input_line_len = i; /* == strlen (shell_input_line) */ |
| 2346 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2347 | set_line_mbstate (); |
| 2348 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2349 | #if defined (HISTORY) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2350 | if (remember_on_history && shell_input_line && shell_input_line[0]) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2351 | { |
| 2352 | char *expansions; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2353 | # if defined (BANG_HISTORY) |
| 2354 | int old_hist; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2355 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2356 | /* If the current delimiter is a single quote, we should not be |
| 2357 | performing history expansion, even if we're on a different |
| 2358 | line from the original single quote. */ |
| 2359 | old_hist = history_expansion_inhibited; |
| 2360 | if (current_delimiter (dstack) == '\'') |
| 2361 | history_expansion_inhibited = 1; |
| 2362 | # endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2363 | expansions = pre_process_line (shell_input_line, 1, 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2364 | # if defined (BANG_HISTORY) |
| 2365 | history_expansion_inhibited = old_hist; |
| 2366 | # endif |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2367 | if (expansions != shell_input_line) |
| 2368 | { |
| 2369 | free (shell_input_line); |
| 2370 | shell_input_line = expansions; |
| 2371 | shell_input_line_len = shell_input_line ? |
| 2372 | strlen (shell_input_line) : 0; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2373 | if (shell_input_line_len == 0) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2374 | current_command_line_count--; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2375 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2376 | /* We have to force the xrealloc below because we don't know |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2377 | the true allocated size of shell_input_line anymore. */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2378 | shell_input_line_size = shell_input_line_len; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2379 | |
| 2380 | set_line_mbstate (); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2381 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2382 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2383 | /* Try to do something intelligent with blank lines encountered while |
| 2384 | entering multi-line commands. XXX - this is grotesque */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2385 | else if (remember_on_history && shell_input_line && |
| 2386 | shell_input_line[0] == '\0' && |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2387 | current_command_line_count > 1) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2388 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2389 | if (current_delimiter (dstack)) |
| 2390 | /* We know shell_input_line[0] == 0 and we're reading some sort of |
| 2391 | quoted string. This means we've got a line consisting of only |
| 2392 | a newline in a quoted string. We want to make sure this line |
| 2393 | gets added to the history. */ |
| 2394 | maybe_add_history (shell_input_line); |
| 2395 | else |
| 2396 | { |
| 2397 | char *hdcs; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2398 | hdcs = history_delimiting_chars (shell_input_line); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2399 | if (hdcs && hdcs[0] == ';') |
| 2400 | maybe_add_history (shell_input_line); |
| 2401 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2404 | #endif /* HISTORY */ |
| 2405 | |
| 2406 | if (shell_input_line) |
| 2407 | { |
| 2408 | /* Lines that signify the end of the shell's input should not be |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2409 | echoed. We should not echo lines while parsing command |
| 2410 | substitutions with recursive calls into the parsing engine; those |
| 2411 | should only be echoed once when we read the word. That is the |
| 2412 | reason for the test against shell_eof_token, which is set to a |
| 2413 | right paren when parsing the contents of command substitutions. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2414 | if (echo_input_at_read && (shell_input_line[0] || |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2415 | shell_input_line_terminator != EOF) && |
| 2416 | shell_eof_token == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2417 | fprintf (stderr, "%s\n", shell_input_line); |
| 2418 | } |
| 2419 | else |
| 2420 | { |
| 2421 | shell_input_line_size = 0; |
| 2422 | prompt_string_pointer = ¤t_prompt_string; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2423 | if (SHOULD_PROMPT ()) |
| 2424 | prompt_again (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2425 | goto restart_read; |
| 2426 | } |
| 2427 | |
| 2428 | /* Add the newline to the end of this string, iff the string does |
| 2429 | not already end in an EOF character. */ |
| 2430 | if (shell_input_line_terminator != EOF) |
| 2431 | { |
Chet Ramey | 6ebbb24 | 2014-04-11 11:04:26 -0400 | [diff] [blame] | 2432 | if (shell_input_line_size < SIZE_MAX-3 && (shell_input_line_len+3 > shell_input_line_size)) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2433 | shell_input_line = (char *)xrealloc (shell_input_line, |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2434 | 1 + (shell_input_line_size += 2)); |
| 2435 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2436 | shell_input_line[shell_input_line_len] = '\n'; |
| 2437 | shell_input_line[shell_input_line_len + 1] = '\0'; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2438 | |
| 2439 | set_line_mbstate (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2440 | } |
| 2441 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2442 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2443 | next_alias_char: |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2444 | uc = shell_input_line[shell_input_line_index]; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2445 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2446 | if (uc) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2447 | shell_input_line_index++; |
| 2448 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2449 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2450 | /* 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] | 2451 | pushed_string_list is non-empty, it's time to pop to the previous string |
| 2452 | because we have fully consumed the result of the last alias expansion. |
| 2453 | Do it transparently; just return the next character of the string popped |
| 2454 | to. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2455 | /* If pushed_string_list != 0 but pushed_string_list->expander == 0 (not |
| 2456 | currently tested) and the flags value is not PSH_SOURCE, we are not |
| 2457 | parsing an alias, we have just saved one (push_string, when called by |
| 2458 | the parse_dparen code) In this case, just go on as well. The PSH_SOURCE |
| 2459 | case is handled below. */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2460 | pop_alias: |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2461 | if (uc == 0 && pushed_string_list && pushed_string_list->flags != PSH_SOURCE) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2462 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2463 | pop_string (); |
| 2464 | uc = shell_input_line[shell_input_line_index]; |
| 2465 | if (uc) |
| 2466 | shell_input_line_index++; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2467 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2468 | #endif /* ALIAS || DPAREN_ARITHMETIC */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2469 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2470 | if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n') |
| 2471 | { |
| 2472 | if (SHOULD_PROMPT ()) |
| 2473 | prompt_again (); |
| 2474 | line_number++; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2475 | /* What do we do here if we're expanding an alias whose definition |
| 2476 | includes an escaped newline? If that's the last character in the |
| 2477 | alias expansion, we just pop the pushed string list (recall that |
| 2478 | we inhibit the appending of a space in mk_alexpansion() if newline |
| 2479 | is the last character). If it's not the last character, we need |
| 2480 | to consume the quoted newline and move to the next character in |
| 2481 | the expansion. */ |
Chet Ramey | 509a443 | 2013-01-10 19:53:16 -0500 | [diff] [blame] | 2482 | #if defined (ALIAS) |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2483 | if (expanding_alias () && shell_input_line[shell_input_line_index+1] == '\0') |
| 2484 | { |
| 2485 | uc = 0; |
| 2486 | goto pop_alias; |
| 2487 | } |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2488 | else if (expanding_alias () && shell_input_line[shell_input_line_index+1] != '\0') |
| 2489 | { |
| 2490 | shell_input_line_index++; /* skip newline */ |
| 2491 | goto next_alias_char; /* and get next character */ |
| 2492 | } |
Chet Ramey | 509a443 | 2013-01-10 19:53:16 -0500 | [diff] [blame] | 2493 | else |
| 2494 | #endif |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2495 | goto restart_read; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2498 | if (uc == 0 && shell_input_line_terminator == EOF) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2499 | return ((shell_input_line_index != 0) ? '\n' : EOF); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2500 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2501 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
| 2502 | /* We already know that we are not parsing an alias expansion because of the |
| 2503 | check for expanding_alias() above. This knows how parse_and_execute |
| 2504 | handles switching to st_string input while an alias is being expanded, |
| 2505 | hence the check for pushed_string_list without pushed_string_list->expander |
| 2506 | and the check for PSH_SOURCE as pushed_string_list->flags. |
| 2507 | parse_and_execute and parse_string both change the input type to st_string |
| 2508 | and place the string to be parsed and executed into location.string, so |
| 2509 | we should not stop reading that until the pointer is '\0'. |
| 2510 | The check for shell_input_line_terminator may be superfluous. |
| 2511 | |
| 2512 | This solves the problem of `.' inside a multi-line alias with embedded |
| 2513 | newlines executing things out of order. */ |
| 2514 | if (uc == 0 && bash_input.type == st_string && *bash_input.location.string && |
| 2515 | pushed_string_list && pushed_string_list->flags == PSH_SOURCE && |
| 2516 | shell_input_line_terminator == 0) |
| 2517 | { |
| 2518 | shell_input_line_index = 0; |
| 2519 | goto restart_read; |
| 2520 | } |
| 2521 | #endif |
| 2522 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2523 | return (uc); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2526 | /* Put C back into the input for the shell. This might need changes for |
| 2527 | HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a |
| 2528 | character different than we read, shell_input_line_property doesn't need |
| 2529 | to change when manipulating shell_input_line. The define for |
| 2530 | last_shell_getc_is_singlebyte should take care of it, though. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2531 | static void |
| 2532 | shell_ungetc (c) |
| 2533 | int c; |
| 2534 | { |
| 2535 | if (shell_input_line && shell_input_line_index) |
| 2536 | shell_input_line[--shell_input_line_index] = c; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2537 | else |
| 2538 | eol_ungetc_lookahead = c; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Chet Ramey | 80b4d71 | 2014-10-05 19:12:20 -0400 | [diff] [blame] | 2541 | char * |
| 2542 | parser_remaining_input () |
| 2543 | { |
| 2544 | if (shell_input_line == 0) |
| 2545 | return 0; |
| 2546 | if (shell_input_line_index < 0 || shell_input_line_index >= shell_input_line_len) |
| 2547 | return '\0'; /* XXX */ |
| 2548 | return (shell_input_line + shell_input_line_index); |
| 2549 | } |
| 2550 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2551 | #ifdef INCLUDE_UNUSED |
| 2552 | /* Back the input pointer up by one, effectively `ungetting' a character. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2553 | static void |
| 2554 | shell_ungetchar () |
| 2555 | { |
| 2556 | if (shell_input_line && shell_input_line_index) |
| 2557 | shell_input_line_index--; |
| 2558 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2559 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2560 | |
| 2561 | /* Discard input until CHARACTER is seen, then push that character back |
| 2562 | onto the input stream. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2563 | static void |
| 2564 | discard_until (character) |
| 2565 | int character; |
| 2566 | { |
| 2567 | int c; |
| 2568 | |
| 2569 | while ((c = shell_getc (0)) != EOF && c != character) |
| 2570 | ; |
| 2571 | |
| 2572 | if (c != EOF) |
| 2573 | shell_ungetc (c); |
| 2574 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2575 | |
| 2576 | void |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2577 | execute_variable_command (command, vname) |
| 2578 | char *command, *vname; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2579 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2580 | char *last_lastarg; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2581 | sh_parser_state_t ps; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2582 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2583 | save_parser_state (&ps); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2584 | last_lastarg = get_string_value ("_"); |
| 2585 | if (last_lastarg) |
| 2586 | last_lastarg = savestring (last_lastarg); |
| 2587 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2588 | parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2589 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2590 | restore_parser_state (&ps); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2591 | bind_variable ("_", last_lastarg, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2592 | FREE (last_lastarg); |
| 2593 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2594 | if (token_to_read == '\n') /* reset_parser was called */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2595 | token_to_read = 0; |
| 2596 | } |
| 2597 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2598 | /* Place to remember the token. We try to keep the buffer |
| 2599 | at a reasonable size, but it can grow. */ |
| 2600 | static char *token = (char *)NULL; |
| 2601 | |
| 2602 | /* Current size of the token buffer. */ |
| 2603 | static int token_buffer_size; |
| 2604 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2605 | /* Command to read_token () explaining what we want it to do. */ |
| 2606 | #define READ 0 |
| 2607 | #define RESET 1 |
| 2608 | #define prompt_is_ps1 \ |
| 2609 | (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt) |
| 2610 | |
| 2611 | /* Function for yyparse to call. yylex keeps track of |
| 2612 | the last two tokens read, and calls read_token. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2613 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2614 | yylex () |
| 2615 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2616 | if (interactive && (current_token == 0 || current_token == '\n')) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2617 | { |
| 2618 | /* Before we print a prompt, we might have to check mailboxes. |
| 2619 | We do this only if it is time to do so. Notice that only here |
| 2620 | is the mail alarm reset; nothing takes place in check_mail () |
| 2621 | except the checking of mail. Please don't change this. */ |
Chet Ramey | b4a8651 | 2011-11-22 20:03:03 -0500 | [diff] [blame] | 2622 | if (prompt_is_ps1 && parse_and_execute_level == 0 && time_to_check_mail ()) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2623 | { |
| 2624 | check_mail (); |
| 2625 | reset_mail_timer (); |
| 2626 | } |
| 2627 | |
| 2628 | /* Avoid printing a prompt if we're not going to read anything, e.g. |
| 2629 | after resetting the parser with read_token (RESET). */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2630 | if (token_to_read == 0 && SHOULD_PROMPT ()) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2631 | prompt_again (); |
| 2632 | } |
| 2633 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2634 | two_tokens_ago = token_before_that; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2635 | token_before_that = last_read_token; |
| 2636 | last_read_token = current_token; |
| 2637 | current_token = read_token (READ); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2638 | |
| 2639 | if ((parser_state & PST_EOFTOKEN) && current_token == shell_eof_token) |
| 2640 | { |
| 2641 | current_token = yacc_EOF; |
| 2642 | if (bash_input.type == st_string) |
| 2643 | rewind_input_string (); |
| 2644 | } |
| 2645 | parser_state &= ~PST_EOFTOKEN; |
| 2646 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2647 | return (current_token); |
| 2648 | } |
| 2649 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2650 | /* When non-zero, we have read the required tokens |
| 2651 | which allow ESAC to be the next one read. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2652 | static int esacs_needed_count; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2653 | |
Chet Ramey | 90a39f3 | 2014-10-02 09:07:21 -0400 | [diff] [blame] | 2654 | static void |
| 2655 | push_heredoc (r) |
| 2656 | REDIRECT *r; |
| 2657 | { |
| 2658 | if (need_here_doc >= HEREDOC_MAX) |
| 2659 | { |
| 2660 | last_command_exit_value = EX_BADUSAGE; |
| 2661 | need_here_doc = 0; |
| 2662 | report_syntax_error (_("maximum here-document count exceeded")); |
| 2663 | reset_parser (); |
| 2664 | exit_shell (last_command_exit_value); |
| 2665 | } |
| 2666 | redir_stack[need_here_doc++] = r; |
| 2667 | } |
| 2668 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2669 | void |
| 2670 | gather_here_documents () |
| 2671 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2672 | int r; |
| 2673 | |
| 2674 | r = 0; |
Chet Ramey | daefb2c | 2014-08-01 15:30:48 -0400 | [diff] [blame] | 2675 | while (need_here_doc > 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2676 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2677 | parser_state |= PST_HEREDOC; |
| 2678 | make_here_document (redir_stack[r++], line_number); |
| 2679 | parser_state &= ~PST_HEREDOC; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2680 | need_here_doc--; |
| 2681 | } |
| 2682 | } |
| 2683 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2684 | /* When non-zero, an open-brace used to create a group is awaiting a close |
| 2685 | brace partner. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2686 | static int open_brace_count; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2687 | |
| 2688 | #define command_token_position(token) \ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2689 | (((token) == ASSIGNMENT_WORD) || (parser_state&PST_REDIRLIST) || \ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2690 | ((token) != SEMI_SEMI && (token) != SEMI_AND && (token) != SEMI_SEMI_AND && reserved_word_acceptable(token))) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2691 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2692 | #define assignment_acceptable(token) \ |
| 2693 | (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2694 | |
| 2695 | /* Check to see if TOKEN is a reserved word and return the token |
| 2696 | value if it is. */ |
| 2697 | #define CHECK_FOR_RESERVED_WORD(tok) \ |
| 2698 | do { \ |
| 2699 | if (!dollar_present && !quoted && \ |
| 2700 | reserved_word_acceptable (last_read_token)) \ |
| 2701 | { \ |
| 2702 | int i; \ |
| 2703 | for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \ |
| 2704 | if (STREQ (tok, word_token_alist[i].word)) \ |
| 2705 | { \ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2706 | if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2707 | break; \ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2708 | if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2709 | break; \ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2710 | if (word_token_alist[i].token == ESAC) \ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2711 | parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \ |
| 2712 | else if (word_token_alist[i].token == CASE) \ |
| 2713 | parser_state |= PST_CASESTMT; \ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2714 | else if (word_token_alist[i].token == COND_END) \ |
| 2715 | parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \ |
| 2716 | else if (word_token_alist[i].token == COND_START) \ |
| 2717 | parser_state |= PST_CONDCMD; \ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2718 | else if (word_token_alist[i].token == '{') \ |
| 2719 | open_brace_count++; \ |
| 2720 | else if (word_token_alist[i].token == '}' && open_brace_count) \ |
| 2721 | open_brace_count--; \ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2722 | return (word_token_alist[i].token); \ |
| 2723 | } \ |
| 2724 | } \ |
| 2725 | } while (0) |
| 2726 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2727 | #if defined (ALIAS) |
| 2728 | |
| 2729 | /* OK, we have a token. Let's try to alias expand it, if (and only if) |
| 2730 | it's eligible. |
| 2731 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2732 | It is eligible for expansion if EXPAND_ALIASES is set, and |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2733 | the token is unquoted and the last token read was a command |
| 2734 | separator (or expand_next_token is set), and we are currently |
| 2735 | processing an alias (pushed_string_list is non-empty) and this |
| 2736 | token is not the same as the current or any previously |
| 2737 | processed alias. |
| 2738 | |
| 2739 | Special cases that disqualify: |
| 2740 | In a pattern list in a case statement (parser_state & PST_CASEPAT). */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2741 | |
| 2742 | static char * |
| 2743 | mk_alexpansion (s) |
| 2744 | char *s; |
| 2745 | { |
| 2746 | int l; |
| 2747 | char *r; |
| 2748 | |
| 2749 | l = strlen (s); |
| 2750 | r = xmalloc (l + 2); |
| 2751 | strcpy (r, s); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2752 | /* If the last character in the alias is a newline, don't add a trailing |
| 2753 | space to the expansion. Works with shell_getc above. */ |
| 2754 | if (r[l - 1] != ' ' && r[l - 1] != '\n') |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2755 | r[l++] = ' '; |
| 2756 | r[l] = '\0'; |
| 2757 | return r; |
| 2758 | } |
| 2759 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2760 | static int |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2761 | alias_expand_token (tokstr) |
| 2762 | char *tokstr; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2763 | { |
| 2764 | char *expanded; |
| 2765 | alias_t *ap; |
| 2766 | |
| 2767 | if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) && |
| 2768 | (parser_state & PST_CASEPAT) == 0) |
| 2769 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2770 | ap = find_alias (tokstr); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2771 | |
| 2772 | /* Currently expanding this token. */ |
| 2773 | if (ap && (ap->flags & AL_BEINGEXPANDED)) |
| 2774 | return (NO_EXPANSION); |
| 2775 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2776 | /* mk_alexpansion puts an extra space on the end of the alias expansion, |
| 2777 | so the lookahead by the parser works right. If this gets changed, |
| 2778 | make sure the code in shell_getc that deals with reaching the end of |
| 2779 | an expanded alias is changed with it. */ |
| 2780 | expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL; |
| 2781 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2782 | if (expanded) |
| 2783 | { |
| 2784 | push_string (expanded, ap->flags & AL_EXPANDNEXT, ap); |
| 2785 | return (RE_READ_TOKEN); |
| 2786 | } |
| 2787 | else |
| 2788 | /* This is an eligible token that does not have an expansion. */ |
| 2789 | return (NO_EXPANSION); |
| 2790 | } |
| 2791 | return (NO_EXPANSION); |
| 2792 | } |
| 2793 | #endif /* ALIAS */ |
| 2794 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2795 | static int |
| 2796 | time_command_acceptable () |
| 2797 | { |
| 2798 | #if defined (COMMAND_TIMING) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2799 | int i; |
| 2800 | |
| 2801 | if (posixly_correct && shell_compatibility_level > 41) |
| 2802 | { |
| 2803 | /* Quick check of the rest of the line to find the next token. If it |
| 2804 | begins with a `-', Posix says to not return `time' as the token. |
| 2805 | This was interp 267. */ |
| 2806 | i = shell_input_line_index; |
| 2807 | while (i < shell_input_line_len && (shell_input_line[i] == ' ' || shell_input_line[i] == '\t')) |
| 2808 | i++; |
| 2809 | if (shell_input_line[i] == '-') |
| 2810 | return 0; |
| 2811 | } |
| 2812 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2813 | switch (last_read_token) |
| 2814 | { |
| 2815 | case 0: |
| 2816 | case ';': |
| 2817 | case '\n': |
| 2818 | case AND_AND: |
| 2819 | case OR_OR: |
| 2820 | case '&': |
Chet Ramey | ccfc303 | 2015-05-19 15:49:39 -0400 | [diff] [blame] | 2821 | case WHILE: |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2822 | case DO: |
Chet Ramey | ccfc303 | 2015-05-19 15:49:39 -0400 | [diff] [blame] | 2823 | case UNTIL: |
| 2824 | case IF: |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2825 | case THEN: |
Chet Ramey | ccfc303 | 2015-05-19 15:49:39 -0400 | [diff] [blame] | 2826 | case ELIF: |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2827 | case ELSE: |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2828 | case '{': /* } */ |
Chet Ramey | ccfc303 | 2015-05-19 15:49:39 -0400 | [diff] [blame] | 2829 | case '(': /* )( */ |
| 2830 | case ')': /* only valid in case statement */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2831 | case BANG: /* ! time pipeline */ |
| 2832 | case TIME: /* time time pipeline */ |
| 2833 | case TIMEOPT: /* time -p time pipeline */ |
| 2834 | case TIMEIGN: /* time -p -- ... */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2835 | return 1; |
| 2836 | default: |
| 2837 | return 0; |
| 2838 | } |
| 2839 | #else |
| 2840 | return 0; |
| 2841 | #endif /* COMMAND_TIMING */ |
| 2842 | } |
| 2843 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2844 | /* Handle special cases of token recognition: |
| 2845 | IN is recognized if the last token was WORD and the token |
| 2846 | before that was FOR or CASE or SELECT. |
| 2847 | |
| 2848 | DO is recognized if the last token was WORD and the token |
| 2849 | before that was FOR or SELECT. |
| 2850 | |
| 2851 | ESAC is recognized if the last token caused `esacs_needed_count' |
| 2852 | to be set |
| 2853 | |
| 2854 | `{' is recognized if the last token as WORD and the token |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2855 | before that was FUNCTION, or if we just parsed an arithmetic |
| 2856 | `for' command. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2857 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2858 | `}' is recognized if there is an unclosed `{' present. |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2859 | |
| 2860 | `-p' is returned as TIMEOPT if the last read token was TIME. |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2861 | `--' is returned as TIMEIGN if the last read token was TIMEOPT. |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2862 | |
| 2863 | ']]' is returned as COND_END if the parser is currently parsing |
| 2864 | a conditional expression ((parser_state & PST_CONDEXPR) != 0) |
| 2865 | |
| 2866 | `time' is returned as TIME if and only if it is immediately |
| 2867 | preceded by one of `;', `\n', `||', `&&', or `&'. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2868 | */ |
| 2869 | |
| 2870 | static int |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2871 | special_case_tokens (tokstr) |
| 2872 | char *tokstr; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2873 | { |
| 2874 | if ((last_read_token == WORD) && |
| 2875 | #if defined (SELECT_COMMAND) |
| 2876 | ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) && |
| 2877 | #else |
| 2878 | ((token_before_that == FOR) || (token_before_that == CASE)) && |
| 2879 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2880 | (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2881 | { |
| 2882 | if (token_before_that == CASE) |
| 2883 | { |
| 2884 | parser_state |= PST_CASEPAT; |
| 2885 | esacs_needed_count++; |
| 2886 | } |
| 2887 | return (IN); |
| 2888 | } |
| 2889 | |
| 2890 | if (last_read_token == WORD && |
| 2891 | #if defined (SELECT_COMMAND) |
| 2892 | (token_before_that == FOR || token_before_that == SELECT) && |
| 2893 | #else |
| 2894 | (token_before_that == FOR) && |
| 2895 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2896 | (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0')) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2897 | return (DO); |
| 2898 | |
| 2899 | /* Ditto for ESAC in the CASE case. |
| 2900 | Specifically, this handles "case word in esac", which is a legal |
| 2901 | construct, certainly because someone will pass an empty arg to the |
| 2902 | case construct, and we don't want it to barf. Of course, we should |
| 2903 | insist that the case construct has at least one pattern in it, but |
| 2904 | the designers disagree. */ |
| 2905 | if (esacs_needed_count) |
| 2906 | { |
| 2907 | esacs_needed_count--; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2908 | if (STREQ (tokstr, "esac")) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2909 | { |
| 2910 | parser_state &= ~PST_CASEPAT; |
| 2911 | return (ESAC); |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | /* The start of a shell function definition. */ |
| 2916 | if (parser_state & PST_ALLOWOPNBRC) |
| 2917 | { |
| 2918 | parser_state &= ~PST_ALLOWOPNBRC; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2919 | if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2920 | { |
| 2921 | open_brace_count++; |
| 2922 | function_bstart = line_number; |
| 2923 | return ('{'); /* } */ |
| 2924 | } |
| 2925 | } |
| 2926 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2927 | /* We allow a `do' after a for ((...)) without an intervening |
| 2928 | list_terminator */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2929 | 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] | 2930 | return (DO); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2931 | if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2932 | { |
| 2933 | open_brace_count++; |
| 2934 | return ('{'); /* } */ |
| 2935 | } |
| 2936 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2937 | 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] | 2938 | { |
| 2939 | open_brace_count--; /* { */ |
| 2940 | return ('}'); |
| 2941 | } |
| 2942 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2943 | #if defined (COMMAND_TIMING) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2944 | /* Handle -p after `time'. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2945 | if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2]) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2946 | return (TIMEOPT); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2947 | /* Handle -- after `time -p'. */ |
| 2948 | if (last_read_token == TIMEOPT && tokstr[0] == '-' && tokstr[1] == '-' && !tokstr[2]) |
| 2949 | return (TIMEIGN); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2950 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2951 | |
| 2952 | #if defined (COND_COMMAND) /* [[ */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2953 | if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0') |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2954 | return (COND_END); |
| 2955 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2956 | |
| 2957 | return (-1); |
| 2958 | } |
| 2959 | |
| 2960 | /* Called from shell.c when Control-C is typed at top level. Or |
| 2961 | by the error rule at top level. */ |
| 2962 | void |
| 2963 | reset_parser () |
| 2964 | { |
| 2965 | dstack.delimiter_depth = 0; /* No delimiters found so far. */ |
| 2966 | open_brace_count = 0; |
| 2967 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2968 | #if defined (EXTENDED_GLOB) |
| 2969 | /* Reset to global value of extended glob */ |
| 2970 | if (parser_state & PST_EXTPAT) |
| 2971 | extended_glob = global_extglob; |
| 2972 | #endif |
| 2973 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2974 | parser_state = 0; |
| 2975 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2976 | #if defined (ALIAS) || defined (DPAREN_ARITHMETIC) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2977 | if (pushed_string_list) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2978 | free_string_list (); |
| 2979 | #endif /* ALIAS || DPAREN_ARITHMETIC */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2980 | |
| 2981 | if (shell_input_line) |
| 2982 | { |
| 2983 | free (shell_input_line); |
| 2984 | shell_input_line = (char *)NULL; |
| 2985 | shell_input_line_size = shell_input_line_index = 0; |
| 2986 | } |
| 2987 | |
| 2988 | FREE (word_desc_to_read); |
| 2989 | word_desc_to_read = (WORD_DESC *)NULL; |
| 2990 | |
Chet Ramey | 836a17b | 2014-09-26 17:00:50 -0400 | [diff] [blame] | 2991 | eol_ungetc_lookahead = 0; |
| 2992 | |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 2993 | current_token = '\n'; /* XXX */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2994 | last_read_token = '\n'; |
| 2995 | token_to_read = '\n'; |
| 2996 | } |
| 2997 | |
| 2998 | /* Read the next token. Command can be READ (normal operation) or |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2999 | RESET (to normalize state). */ |
| 3000 | static int |
| 3001 | read_token (command) |
| 3002 | int command; |
| 3003 | { |
| 3004 | int character; /* Current character. */ |
| 3005 | int peek_char; /* Temporary look-ahead character. */ |
| 3006 | int result; /* The thing to return. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3007 | |
| 3008 | if (command == RESET) |
| 3009 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3010 | reset_parser (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3011 | return ('\n'); |
| 3012 | } |
| 3013 | |
| 3014 | if (token_to_read) |
| 3015 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3016 | result = token_to_read; |
| 3017 | if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3018 | { |
| 3019 | yylval.word = word_desc_to_read; |
| 3020 | word_desc_to_read = (WORD_DESC *)NULL; |
| 3021 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3022 | token_to_read = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3023 | return (result); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3024 | } |
| 3025 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3026 | #if defined (COND_COMMAND) |
| 3027 | if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD) |
| 3028 | { |
| 3029 | cond_lineno = line_number; |
| 3030 | parser_state |= PST_CONDEXPR; |
| 3031 | yylval.command = parse_cond_command (); |
| 3032 | if (cond_token != COND_END) |
| 3033 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3034 | cond_error (); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3035 | return (-1); |
| 3036 | } |
| 3037 | token_to_read = COND_END; |
| 3038 | parser_state &= ~(PST_CONDEXPR|PST_CONDCMD); |
| 3039 | return (COND_CMD); |
| 3040 | } |
| 3041 | #endif |
| 3042 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3043 | #if defined (ALIAS) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3044 | /* This is a place to jump back to once we have successfully expanded a |
| 3045 | token with an alias and pushed the string with push_string () */ |
| 3046 | re_read_token: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3047 | #endif /* ALIAS */ |
| 3048 | |
| 3049 | /* Read a single word from input. Start by skipping blanks. */ |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 3050 | while ((character = shell_getc (1)) != EOF && shellblank (character)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3051 | ; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3052 | |
| 3053 | if (character == EOF) |
| 3054 | { |
| 3055 | EOF_Reached = 1; |
| 3056 | return (yacc_EOF); |
| 3057 | } |
| 3058 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3059 | if MBTEST(character == '#' && (!interactive || interactive_comments)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3060 | { |
| 3061 | /* A comment. Discard until EOL or EOF, and then return a newline. */ |
| 3062 | discard_until ('\n'); |
| 3063 | shell_getc (0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3064 | character = '\n'; /* this will take the next if statement and return. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | if (character == '\n') |
| 3068 | { |
| 3069 | /* If we're about to return an unquoted newline, we can go and collect |
| 3070 | the text of any pending here document. */ |
| 3071 | if (need_here_doc) |
| 3072 | gather_here_documents (); |
| 3073 | |
| 3074 | #if defined (ALIAS) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3075 | parser_state &= ~PST_ALEXPNEXT; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3076 | #endif /* ALIAS */ |
| 3077 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3078 | parser_state &= ~PST_ASSIGNOK; |
| 3079 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3080 | return (character); |
| 3081 | } |
| 3082 | |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 3083 | if (parser_state & PST_REGEXP) |
| 3084 | goto tokword; |
| 3085 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3086 | /* Shell meta-characters. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3087 | if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3088 | { |
| 3089 | #if defined (ALIAS) |
| 3090 | /* Turn off alias tokenization iff this character sequence would |
| 3091 | not leave us ready to read a command. */ |
| 3092 | if (character == '<' || character == '>') |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3093 | parser_state &= ~PST_ALEXPNEXT; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3094 | #endif /* ALIAS */ |
| 3095 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3096 | parser_state &= ~PST_ASSIGNOK; |
| 3097 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3098 | peek_char = shell_getc (1); |
| 3099 | if (character == peek_char) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3100 | { |
| 3101 | switch (character) |
| 3102 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3103 | case '<': |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3104 | /* If '<' then we could be at "<<" or at "<<-". We have to |
| 3105 | look ahead one more character. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3106 | peek_char = shell_getc (1); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3107 | if MBTEST(peek_char == '-') |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3108 | return (LESS_LESS_MINUS); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3109 | else if MBTEST(peek_char == '<') |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3110 | return (LESS_LESS_LESS); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3111 | else |
| 3112 | { |
| 3113 | shell_ungetc (peek_char); |
| 3114 | return (LESS_LESS); |
| 3115 | } |
| 3116 | |
| 3117 | case '>': |
| 3118 | return (GREATER_GREATER); |
| 3119 | |
| 3120 | case ';': |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3121 | parser_state |= PST_CASEPAT; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3122 | #if defined (ALIAS) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3123 | parser_state &= ~PST_ALEXPNEXT; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3124 | #endif /* ALIAS */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3125 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3126 | peek_char = shell_getc (1); |
| 3127 | if MBTEST(peek_char == '&') |
| 3128 | return (SEMI_SEMI_AND); |
| 3129 | else |
| 3130 | { |
| 3131 | shell_ungetc (peek_char); |
| 3132 | return (SEMI_SEMI); |
| 3133 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3134 | |
| 3135 | case '&': |
| 3136 | return (AND_AND); |
| 3137 | |
| 3138 | case '|': |
| 3139 | return (OR_OR); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3140 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3141 | #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3142 | case '(': /* ) */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3143 | result = parse_dparen (character); |
| 3144 | if (result == -2) |
| 3145 | break; |
| 3146 | else |
| 3147 | return result; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3148 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3149 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3150 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3151 | else if MBTEST(character == '<' && peek_char == '&') |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3152 | return (LESS_AND); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3153 | else if MBTEST(character == '>' && peek_char == '&') |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3154 | return (GREATER_AND); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3155 | else if MBTEST(character == '<' && peek_char == '>') |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3156 | return (LESS_GREATER); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3157 | else if MBTEST(character == '>' && peek_char == '|') |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3158 | return (GREATER_BAR); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3159 | else if MBTEST(character == '&' && peek_char == '>') |
| 3160 | { |
| 3161 | peek_char = shell_getc (1); |
| 3162 | if MBTEST(peek_char == '>') |
| 3163 | return (AND_GREATER_GREATER); |
| 3164 | else |
| 3165 | { |
| 3166 | shell_ungetc (peek_char); |
| 3167 | return (AND_GREATER); |
| 3168 | } |
| 3169 | } |
| 3170 | else if MBTEST(character == '|' && peek_char == '&') |
| 3171 | return (BAR_AND); |
| 3172 | else if MBTEST(character == ';' && peek_char == '&') |
| 3173 | { |
| 3174 | parser_state |= PST_CASEPAT; |
| 3175 | #if defined (ALIAS) |
| 3176 | parser_state &= ~PST_ALEXPNEXT; |
| 3177 | #endif /* ALIAS */ |
| 3178 | return (SEMI_AND); |
| 3179 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3180 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3181 | shell_ungetc (peek_char); |
| 3182 | |
| 3183 | /* If we look like we are reading the start of a function |
| 3184 | definition, then let the reader know about it so that |
| 3185 | we will do the right thing with `{'. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3186 | if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3187 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3188 | parser_state |= PST_ALLOWOPNBRC; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3189 | #if defined (ALIAS) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3190 | parser_state &= ~PST_ALEXPNEXT; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3191 | #endif /* ALIAS */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3192 | function_dstart = line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3195 | /* case pattern lists may be preceded by an optional left paren. If |
| 3196 | we're not trying to parse a case pattern list, the left paren |
| 3197 | indicates a subshell. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3198 | if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3199 | parser_state |= PST_SUBSHELL; |
| 3200 | /*(*/ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3201 | else if MBTEST((parser_state & PST_CASEPAT) && character == ')') |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3202 | parser_state &= ~PST_CASEPAT; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3203 | /*(*/ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3204 | else if MBTEST((parser_state & PST_SUBSHELL) && character == ')') |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3205 | parser_state &= ~PST_SUBSHELL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3206 | |
| 3207 | #if defined (PROCESS_SUBSTITUTION) |
| 3208 | /* Check for the constructs which introduce process substitution. |
| 3209 | Shells running in `posix mode' don't do process substitution. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3210 | if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3211 | #endif /* PROCESS_SUBSTITUTION */ |
| 3212 | return (character); |
| 3213 | } |
| 3214 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3215 | /* Hack <&- (close stdin) case. Also <&N- (dup and close). */ |
| 3216 | 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] | 3217 | return (character); |
| 3218 | |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 3219 | tokword: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3220 | /* Okay, if we got this far, we have to read a word. Read one, |
| 3221 | and then check it against the known ones. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3222 | result = read_token_word (character); |
| 3223 | #if defined (ALIAS) |
| 3224 | if (result == RE_READ_TOKEN) |
| 3225 | goto re_read_token; |
| 3226 | #endif |
| 3227 | return result; |
| 3228 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3229 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3230 | /* |
| 3231 | * Match a $(...) or other grouping construct. This has to handle embedded |
| 3232 | * quoted strings ('', ``, "") and nested constructs. It also must handle |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3233 | * reprompting the user, if necessary, after reading a newline, and returning |
| 3234 | * correct error values if it reads EOF. |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3235 | */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3236 | #define P_FIRSTCLOSE 0x0001 |
| 3237 | #define P_ALLOWESC 0x0002 |
| 3238 | #define P_DQUOTE 0x0004 |
| 3239 | #define P_COMMAND 0x0008 /* parsing a command, so look for comments */ |
| 3240 | #define P_BACKQUOTE 0x0010 /* parsing a backquoted command substitution */ |
| 3241 | #define P_ARRAYSUB 0x0020 /* parsing a [...] array subscript for assignment */ |
| 3242 | #define P_DOLBRACE 0x0040 /* parsing a ${...} construct */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3243 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3244 | /* Lexical state while parsing a grouping construct or $(...). */ |
| 3245 | #define LEX_WASDOL 0x001 |
| 3246 | #define LEX_CKCOMMENT 0x002 |
| 3247 | #define LEX_INCOMMENT 0x004 |
| 3248 | #define LEX_PASSNEXT 0x008 |
| 3249 | #define LEX_RESWDOK 0x010 |
| 3250 | #define LEX_CKCASE 0x020 |
| 3251 | #define LEX_INCASE 0x040 |
| 3252 | #define LEX_INHEREDOC 0x080 |
| 3253 | #define LEX_HEREDELIM 0x100 /* reading here-doc delimiter */ |
| 3254 | #define LEX_STRIPDOC 0x200 /* <<- strip tabs from here doc delim */ |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3255 | #define LEX_INWORD 0x400 |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3256 | |
| 3257 | #define COMSUB_META(ch) ((ch) == ';' || (ch) == '&' || (ch) == '|') |
| 3258 | |
| 3259 | #define CHECK_NESTRET_ERROR() \ |
| 3260 | do { \ |
| 3261 | if (nestret == &matched_pair_error) \ |
| 3262 | { \ |
| 3263 | free (ret); \ |
| 3264 | return &matched_pair_error; \ |
| 3265 | } \ |
| 3266 | } while (0) |
| 3267 | |
| 3268 | #define APPEND_NESTRET() \ |
| 3269 | do { \ |
| 3270 | if (nestlen) \ |
| 3271 | { \ |
| 3272 | RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); \ |
| 3273 | strcpy (ret + retind, nestret); \ |
| 3274 | retind += nestlen; \ |
| 3275 | } \ |
| 3276 | } while (0) |
| 3277 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3278 | static char matched_pair_error; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3279 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3280 | static char * |
| 3281 | parse_matched_pair (qc, open, close, lenp, flags) |
| 3282 | int qc; /* `"' if this construct is within double quotes */ |
| 3283 | int open, close; |
| 3284 | int *lenp, flags; |
| 3285 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3286 | int count, ch, tflags; |
| 3287 | int nestlen, ttranslen, start_lineno; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3288 | char *ret, *nestret, *ttrans; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3289 | int retind, retsize, rflags; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3290 | int dolbrace_state; |
| 3291 | |
| 3292 | dolbrace_state = (flags & P_DOLBRACE) ? DOLBRACE_PARAM : 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3293 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3294 | /*itrace("parse_matched_pair[%d]: open = %c close = %c flags = %d", line_number, open, close, flags);*/ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3295 | count = 1; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3296 | tflags = 0; |
| 3297 | |
| 3298 | if ((flags & P_COMMAND) && qc != '`' && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0) |
| 3299 | tflags |= LEX_CKCOMMENT; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3300 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3301 | /* RFLAGS is the set of flags we want to pass to recursive calls. */ |
| 3302 | rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE); |
| 3303 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3304 | ret = (char *)xmalloc (retsize = 64); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3305 | retind = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3306 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3307 | start_lineno = line_number; |
| 3308 | while (count) |
| 3309 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3310 | ch = shell_getc (qc != '\'' && (tflags & (LEX_PASSNEXT)) == 0); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 3311 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3312 | if (ch == EOF) |
| 3313 | { |
| 3314 | free (ret); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3315 | parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3316 | EOF_Reached = 1; /* XXX */ |
| 3317 | return (&matched_pair_error); |
| 3318 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3319 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3320 | /* Possible reprompting. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3321 | if (ch == '\n' && SHOULD_PROMPT ()) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3322 | prompt_again (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3323 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3324 | /* Don't bother counting parens or doing anything else if in a comment |
| 3325 | or part of a case statement */ |
| 3326 | if (tflags & LEX_INCOMMENT) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3327 | { |
| 3328 | /* Add this character. */ |
| 3329 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3330 | ret[retind++] = ch; |
| 3331 | |
| 3332 | if (ch == '\n') |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3333 | tflags &= ~LEX_INCOMMENT; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3334 | |
| 3335 | continue; |
| 3336 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3337 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 3338 | /* Not exactly right yet, should handle shell metacharacters, too. If |
| 3339 | any changes are made to this test, make analogous changes to subst.c: |
| 3340 | extract_delimited_string(). */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3341 | else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (retind == 0 || ret[retind-1] == '\n' || shellblank (ret[retind - 1]))) |
| 3342 | tflags |= LEX_INCOMMENT; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3343 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3344 | if (tflags & LEX_PASSNEXT) /* last char was backslash */ |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 3345 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3346 | tflags &= ~LEX_PASSNEXT; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3347 | if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */ |
| 3348 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3349 | if (retind > 0) |
| 3350 | retind--; /* swallow previously-added backslash */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3351 | continue; |
| 3352 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3353 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3354 | RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3355 | if MBTEST(ch == CTLESC) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3356 | ret[retind++] = CTLESC; |
| 3357 | ret[retind++] = ch; |
| 3358 | continue; |
| 3359 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3360 | /* If we're reparsing the input (e.g., from parse_string_to_word_list), |
| 3361 | we've already prepended CTLESC to single-quoted results of $'...'. |
| 3362 | We may want to do this for other CTLESC-quoted characters in |
| 3363 | reparse, too. */ |
| 3364 | else if MBTEST((parser_state & PST_REPARSE) && open == '\'' && (ch == CTLESC || ch == CTLNUL)) |
| 3365 | { |
| 3366 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3367 | ret[retind++] = ch; |
| 3368 | continue; |
| 3369 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3370 | else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3371 | { |
| 3372 | RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64); |
| 3373 | ret[retind++] = CTLESC; |
| 3374 | ret[retind++] = ch; |
| 3375 | continue; |
| 3376 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3377 | else if MBTEST(ch == close) /* ending delimiter */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3378 | count--; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3379 | /* handle nested ${...} specially. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3380 | else if MBTEST(open != close && (tflags & LEX_WASDOL) && open == '{' && ch == open) /* } */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3381 | count++; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3382 | else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3383 | count++; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3384 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3385 | /* Add this character. */ |
| 3386 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3387 | ret[retind++] = ch; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3388 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3389 | /* If we just read the ending character, don't bother continuing. */ |
| 3390 | if (count == 0) |
| 3391 | break; |
| 3392 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3393 | if (open == '\'') /* '' inside grouping construct */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3394 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3395 | if MBTEST((flags & P_ALLOWESC) && ch == '\\') |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3396 | tflags |= LEX_PASSNEXT; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3397 | continue; |
| 3398 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3399 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3400 | if MBTEST(ch == '\\') /* backslashes */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3401 | tflags |= LEX_PASSNEXT; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3402 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3403 | /* Based on which dolstate is currently in (param, op, or word), |
| 3404 | decide what the op is. We're really only concerned if it's % or |
| 3405 | #, so we can turn on a flag that says whether or not we should |
| 3406 | treat single quotes as special when inside a double-quoted |
| 3407 | ${...}. This logic must agree with subst.c:extract_dollar_brace_string |
| 3408 | since they share the same defines. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3409 | /* FLAG POSIX INTERP 221 */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3410 | if (flags & P_DOLBRACE) |
| 3411 | { |
| 3412 | /* ${param%[%]word} */ |
| 3413 | if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '%' && retind > 1) |
| 3414 | dolbrace_state = DOLBRACE_QUOTE; |
| 3415 | /* ${param#[#]word} */ |
| 3416 | else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '#' && retind > 1) |
| 3417 | dolbrace_state = DOLBRACE_QUOTE; |
| 3418 | /* ${param/[/]pat/rep} */ |
| 3419 | else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '/' && retind > 1) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3420 | dolbrace_state = DOLBRACE_QUOTE2; /* XXX */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3421 | /* ${param^[^]pat} */ |
| 3422 | else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '^' && retind > 1) |
| 3423 | dolbrace_state = DOLBRACE_QUOTE; |
| 3424 | /* ${param,[,]pat} */ |
| 3425 | else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == ',' && retind > 1) |
| 3426 | dolbrace_state = DOLBRACE_QUOTE; |
| 3427 | else if MBTEST(dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", ch) != 0) |
| 3428 | dolbrace_state = DOLBRACE_OP; |
| 3429 | else if MBTEST(dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", ch) == 0) |
| 3430 | dolbrace_state = DOLBRACE_WORD; |
| 3431 | } |
| 3432 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3433 | /* The big hammer. Single quotes aren't special in double quotes. The |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3434 | problem is that Posix used to say the single quotes are semi-special: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3435 | within a double-quoted ${...} construct "an even number of |
| 3436 | unescaped double-quotes or single-quotes, if any, shall occur." */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3437 | /* This was changed in Austin Group Interp 221 */ |
Chet Ramey | bc63a08 | 2014-03-28 11:53:44 -0400 | [diff] [blame] | 3438 | if MBTEST(posixly_correct && shell_compatibility_level > 41 && dolbrace_state != DOLBRACE_QUOTE && dolbrace_state != DOLBRACE_QUOTE2 && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'') |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3439 | continue; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3440 | |
| 3441 | /* Could also check open == '`' if we want to parse grouping constructs |
| 3442 | inside old-style command substitution. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3443 | if (open != close) /* a grouping construct */ |
| 3444 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3445 | if MBTEST(shellquote (ch)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3446 | { |
| 3447 | /* '', ``, or "" inside $(...) or other grouping construct. */ |
| 3448 | push_delimiter (dstack, ch); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3449 | if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3450 | nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3451 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3452 | nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3453 | pop_delimiter (dstack); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3454 | CHECK_NESTRET_ERROR (); |
| 3455 | |
| 3456 | if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0)) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3457 | { |
| 3458 | /* Translate $'...' here. */ |
| 3459 | ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3460 | xfree (nestret); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3461 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3462 | /* If we're parsing a double-quoted brace expansion and we are |
| 3463 | not in a place where single quotes are treated specially, |
| 3464 | make sure we single-quote the results of the ansi |
| 3465 | expansion because quote removal should remove them later */ |
| 3466 | /* FLAG POSIX INTERP 221 */ |
| 3467 | if ((shell_compatibility_level > 42) && (rflags & P_DQUOTE) && (dolbrace_state == DOLBRACE_QUOTE2) && (flags & P_DOLBRACE)) |
| 3468 | { |
| 3469 | nestret = sh_single_quote (ttrans); |
| 3470 | free (ttrans); |
| 3471 | nestlen = strlen (nestret); |
| 3472 | } |
| 3473 | else if ((rflags & P_DQUOTE) == 0) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3474 | { |
| 3475 | nestret = sh_single_quote (ttrans); |
| 3476 | free (ttrans); |
| 3477 | nestlen = strlen (nestret); |
| 3478 | } |
| 3479 | else |
| 3480 | { |
| 3481 | nestret = ttrans; |
| 3482 | nestlen = ttranslen; |
| 3483 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3484 | retind -= 2; /* back up before the $' */ |
| 3485 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3486 | else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0)) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3487 | { |
| 3488 | /* Locale expand $"..." here. */ |
| 3489 | ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3490 | xfree (nestret); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3491 | |
| 3492 | nestret = sh_mkdoublequoted (ttrans, ttranslen, 0); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3493 | free (ttrans); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3494 | nestlen = ttranslen + 2; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3495 | retind -= 2; /* back up before the $" */ |
| 3496 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3497 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3498 | APPEND_NESTRET (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3499 | FREE (nestret); |
| 3500 | } |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3501 | else if ((flags & P_ARRAYSUB) && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */ |
| 3502 | goto parse_dollar_word; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3503 | } |
| 3504 | /* Parse an old-style command substitution within double quotes as a |
| 3505 | single word. */ |
| 3506 | /* XXX - sh and ksh93 don't do this - XXX */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3507 | else if MBTEST(open == '"' && ch == '`') |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3508 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3509 | nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3510 | |
| 3511 | CHECK_NESTRET_ERROR (); |
| 3512 | APPEND_NESTRET (); |
| 3513 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3514 | FREE (nestret); |
| 3515 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3516 | else if MBTEST(open != '`' && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3517 | /* check for $(), $[], or ${} inside quoted string. */ |
| 3518 | { |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3519 | parse_dollar_word: |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3520 | if (open == ch) /* undo previous increment */ |
| 3521 | count--; |
| 3522 | if (ch == '(') /* ) */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3523 | nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3524 | else if (ch == '{') /* } */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3525 | nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3526 | else if (ch == '[') /* ] */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3527 | nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3528 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3529 | CHECK_NESTRET_ERROR (); |
| 3530 | APPEND_NESTRET (); |
| 3531 | |
| 3532 | FREE (nestret); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3533 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3534 | if MBTEST(ch == '$') |
| 3535 | tflags |= LEX_WASDOL; |
| 3536 | else |
| 3537 | tflags &= ~LEX_WASDOL; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3538 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3539 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3540 | ret[retind] = '\0'; |
| 3541 | if (lenp) |
| 3542 | *lenp = retind; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3543 | /*itrace("parse_matched_pair[%d]: returning %s", line_number, ret);*/ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3544 | return ret; |
| 3545 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3546 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3547 | /* Parse a $(...) command substitution. This is messier than I'd like, and |
| 3548 | reproduces a lot more of the token-reading code than I'd like. */ |
| 3549 | static char * |
| 3550 | parse_comsub (qc, open, close, lenp, flags) |
| 3551 | int qc; /* `"' if this construct is within double quotes */ |
| 3552 | int open, close; |
| 3553 | int *lenp, flags; |
| 3554 | { |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3555 | int count, ch, peekc, tflags, lex_rwlen, lex_wlen, lex_firstind; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3556 | int nestlen, ttranslen, start_lineno; |
| 3557 | char *ret, *nestret, *ttrans, *heredelim; |
| 3558 | int retind, retsize, rflags, hdlen; |
| 3559 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3560 | /* Posix interp 217 says arithmetic expressions have precedence, so |
| 3561 | assume $(( introduces arithmetic expansion and parse accordingly. */ |
| 3562 | peekc = shell_getc (0); |
| 3563 | shell_ungetc (peekc); |
| 3564 | if (peekc == '(') |
| 3565 | return (parse_matched_pair (qc, open, close, lenp, 0)); |
| 3566 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3567 | /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/ |
| 3568 | count = 1; |
| 3569 | tflags = LEX_RESWDOK; |
| 3570 | |
| 3571 | if ((flags & P_COMMAND) && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0) |
| 3572 | tflags |= LEX_CKCASE; |
| 3573 | if ((tflags & LEX_CKCASE) && (interactive == 0 || interactive_comments)) |
| 3574 | tflags |= LEX_CKCOMMENT; |
| 3575 | |
| 3576 | /* RFLAGS is the set of flags we want to pass to recursive calls. */ |
| 3577 | rflags = (flags & P_DQUOTE); |
| 3578 | |
| 3579 | ret = (char *)xmalloc (retsize = 64); |
| 3580 | retind = 0; |
| 3581 | |
| 3582 | start_lineno = line_number; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3583 | lex_rwlen = lex_wlen = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3584 | |
| 3585 | heredelim = 0; |
| 3586 | lex_firstind = -1; |
| 3587 | |
| 3588 | while (count) |
| 3589 | { |
| 3590 | comsub_readchar: |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3591 | ch = shell_getc (qc != '\'' && (tflags & (LEX_INCOMMENT|LEX_PASSNEXT)) == 0); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3592 | |
| 3593 | if (ch == EOF) |
| 3594 | { |
| 3595 | eof_error: |
| 3596 | free (ret); |
| 3597 | FREE (heredelim); |
| 3598 | parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close); |
| 3599 | EOF_Reached = 1; /* XXX */ |
| 3600 | return (&matched_pair_error); |
| 3601 | } |
| 3602 | |
| 3603 | /* If we hit the end of a line and are reading the contents of a here |
| 3604 | document, and it's not the same line that the document starts on, |
| 3605 | check for this line being the here doc delimiter. Otherwise, if |
| 3606 | we're in a here document, mark the next character as the beginning |
| 3607 | of a line. */ |
| 3608 | if (ch == '\n') |
| 3609 | { |
| 3610 | if ((tflags & LEX_HEREDELIM) && heredelim) |
| 3611 | { |
| 3612 | tflags &= ~LEX_HEREDELIM; |
| 3613 | tflags |= LEX_INHEREDOC; |
| 3614 | lex_firstind = retind + 1; |
| 3615 | } |
| 3616 | else if (tflags & LEX_INHEREDOC) |
| 3617 | { |
| 3618 | int tind; |
| 3619 | tind = lex_firstind; |
| 3620 | while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t') |
| 3621 | tind++; |
| 3622 | if (STREQN (ret + tind, heredelim, hdlen)) |
| 3623 | { |
| 3624 | tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC); |
| 3625 | /*itrace("parse_comsub:%d: found here doc end `%s'", line_number, ret + tind);*/ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3626 | free (heredelim); |
| 3627 | heredelim = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3628 | lex_firstind = -1; |
| 3629 | } |
| 3630 | else |
| 3631 | lex_firstind = retind + 1; |
| 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | /* Possible reprompting. */ |
| 3636 | if (ch == '\n' && SHOULD_PROMPT ()) |
| 3637 | prompt_again (); |
| 3638 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3639 | /* XXX -- possibly allow here doc to be delimited by ending right |
| 3640 | paren. */ |
| 3641 | if ((tflags & LEX_INHEREDOC) && ch == close && count == 1) |
| 3642 | { |
| 3643 | int tind; |
| 3644 | /*itrace("parse_comsub: in here doc, ch == close, retind - firstind = %d hdlen = %d retind = %d", retind-lex_firstind, hdlen, retind);*/ |
| 3645 | tind = lex_firstind; |
| 3646 | while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t') |
| 3647 | tind++; |
| 3648 | if (retind-tind == hdlen && STREQN (ret + tind, heredelim, hdlen)) |
| 3649 | { |
| 3650 | tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC); |
| 3651 | /*itrace("parse_comsub:%d: found here doc end `%s'", line_number, ret + tind);*/ |
| 3652 | free (heredelim); |
| 3653 | heredelim = 0; |
| 3654 | lex_firstind = -1; |
| 3655 | } |
| 3656 | } |
| 3657 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3658 | /* Don't bother counting parens or doing anything else if in a comment */ |
| 3659 | if (tflags & (LEX_INCOMMENT|LEX_INHEREDOC)) |
| 3660 | { |
| 3661 | /* Add this character. */ |
| 3662 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3663 | ret[retind++] = ch; |
| 3664 | |
| 3665 | if ((tflags & LEX_INCOMMENT) && ch == '\n') |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3666 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3667 | /*itrace("parse_comsub:%d: lex_incomment -> 0 ch = `%c'", line_number, ch);*/ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3668 | tflags &= ~LEX_INCOMMENT; |
| 3669 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3670 | |
| 3671 | continue; |
| 3672 | } |
| 3673 | |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3674 | if (tflags & LEX_PASSNEXT) /* last char was backslash */ |
| 3675 | { |
| 3676 | /*itrace("parse_comsub:%d: lex_passnext -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/ |
| 3677 | tflags &= ~LEX_PASSNEXT; |
| 3678 | if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */ |
| 3679 | { |
| 3680 | if (retind > 0) |
| 3681 | retind--; /* swallow previously-added backslash */ |
| 3682 | continue; |
| 3683 | } |
| 3684 | |
| 3685 | RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3686 | if MBTEST(ch == CTLESC) |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3687 | ret[retind++] = CTLESC; |
| 3688 | ret[retind++] = ch; |
| 3689 | continue; |
| 3690 | } |
| 3691 | |
| 3692 | /* If this is a shell break character, we are not in a word. If not, |
| 3693 | we either start or continue a word. */ |
| 3694 | if MBTEST(shellbreak (ch)) |
| 3695 | { |
| 3696 | tflags &= ~LEX_INWORD; |
| 3697 | /*itrace("parse_comsub:%d: lex_inword -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/ |
| 3698 | } |
| 3699 | else |
| 3700 | { |
| 3701 | if (tflags & LEX_INWORD) |
| 3702 | { |
| 3703 | lex_wlen++; |
| 3704 | /*itrace("parse_comsub:%d: lex_inword == 1 ch = `%c' lex_wlen = %d (%d)", line_number, ch, lex_wlen, __LINE__);*/ |
| 3705 | } |
| 3706 | else |
| 3707 | { |
| 3708 | /*itrace("parse_comsub:%d: lex_inword -> 1 ch = `%c' (%d)", line_number, ch, __LINE__);*/ |
| 3709 | tflags |= LEX_INWORD; |
| 3710 | lex_wlen = 0; |
Chet Ramey | f88c544 | 2015-08-13 15:38:42 -0400 | [diff] [blame] | 3711 | if (tflags & LEX_RESWDOK) |
| 3712 | lex_rwlen = 0; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3713 | } |
| 3714 | } |
| 3715 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3716 | /* Skip whitespace */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3717 | if MBTEST(shellblank (ch) && (tflags & LEX_HEREDELIM) == 0 && lex_rwlen == 0) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3718 | { |
| 3719 | /* Add this character. */ |
| 3720 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3721 | ret[retind++] = ch; |
| 3722 | continue; |
| 3723 | } |
| 3724 | |
| 3725 | /* Either we are looking for the start of the here-doc delimiter |
| 3726 | (lex_firstind == -1) or we are reading one (lex_firstind >= 0). |
| 3727 | If this character is a shell break character and we are reading |
| 3728 | the delimiter, save it and note that we are now reading a here |
| 3729 | document. If we've found the start of the delimiter, note it by |
| 3730 | setting lex_firstind. Backslashes can quote shell metacharacters |
| 3731 | in here-doc delimiters. */ |
| 3732 | if (tflags & LEX_HEREDELIM) |
| 3733 | { |
| 3734 | if (lex_firstind == -1 && shellbreak (ch) == 0) |
| 3735 | lex_firstind = retind; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3736 | #if 0 |
| 3737 | else if (heredelim && (tflags & LEX_PASSNEXT) == 0 && ch == '\n') |
| 3738 | { |
| 3739 | tflags |= LEX_INHEREDOC; |
| 3740 | tflags &= ~LEX_HEREDELIM; |
| 3741 | lex_firstind = retind + 1; |
| 3742 | } |
| 3743 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3744 | else if (lex_firstind >= 0 && (tflags & LEX_PASSNEXT) == 0 && shellbreak (ch)) |
| 3745 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3746 | if (heredelim == 0) |
| 3747 | { |
| 3748 | nestret = substring (ret, lex_firstind, retind); |
| 3749 | heredelim = string_quote_removal (nestret, 0); |
| 3750 | free (nestret); |
| 3751 | hdlen = STRLEN(heredelim); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3752 | /*itrace("parse_comsub:%d: found here doc delimiter `%s' (%d)", line_number, heredelim, hdlen);*/ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3753 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3754 | if (ch == '\n') |
| 3755 | { |
| 3756 | tflags |= LEX_INHEREDOC; |
| 3757 | tflags &= ~LEX_HEREDELIM; |
| 3758 | lex_firstind = retind + 1; |
| 3759 | } |
| 3760 | else |
| 3761 | lex_firstind = -1; |
| 3762 | } |
| 3763 | } |
| 3764 | |
| 3765 | /* Meta-characters that can introduce a reserved word. Not perfect yet. */ |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3766 | if MBTEST((tflags & LEX_RESWDOK) == 0 && (tflags & LEX_CKCASE) && (tflags & LEX_INCOMMENT) == 0 && (shellmeta(ch) || ch == '\n')) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3767 | { |
| 3768 | /* Add this character. */ |
| 3769 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3770 | ret[retind++] = ch; |
| 3771 | peekc = shell_getc (1); |
| 3772 | if (ch == peekc && (ch == '&' || ch == '|' || ch == ';')) /* two-character tokens */ |
| 3773 | { |
| 3774 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3775 | ret[retind++] = peekc; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3776 | /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3777 | tflags |= LEX_RESWDOK; |
| 3778 | lex_rwlen = 0; |
| 3779 | continue; |
| 3780 | } |
| 3781 | else if (ch == '\n' || COMSUB_META(ch)) |
| 3782 | { |
| 3783 | shell_ungetc (peekc); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3784 | /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3785 | tflags |= LEX_RESWDOK; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3786 | lex_rwlen = 0; |
| 3787 | continue; |
| 3788 | } |
| 3789 | else if (ch == EOF) |
| 3790 | goto eof_error; |
| 3791 | else |
| 3792 | { |
| 3793 | /* `unget' the character we just added and fall through */ |
| 3794 | retind--; |
| 3795 | shell_ungetc (peekc); |
| 3796 | } |
| 3797 | } |
| 3798 | |
| 3799 | /* If we can read a reserved word, try to read one. */ |
| 3800 | if (tflags & LEX_RESWDOK) |
| 3801 | { |
| 3802 | if MBTEST(islower (ch)) |
| 3803 | { |
| 3804 | /* Add this character. */ |
| 3805 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3806 | ret[retind++] = ch; |
| 3807 | lex_rwlen++; |
| 3808 | continue; |
| 3809 | } |
| 3810 | else if MBTEST(lex_rwlen == 4 && shellbreak (ch)) |
| 3811 | { |
| 3812 | if (STREQN (ret + retind - 4, "case", 4)) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3813 | { |
| 3814 | tflags |= LEX_INCASE; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3815 | /*itrace("parse_comsub:%d: found `case', lex_incase -> 1 lex_reswdok -> 0", line_number);*/ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3816 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3817 | else if (STREQN (ret + retind - 4, "esac", 4)) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3818 | { |
| 3819 | tflags &= ~LEX_INCASE; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3820 | /*itrace("parse_comsub:%d: found `esac', lex_incase -> 0 lex_reswdok -> 0", line_number);*/ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3821 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3822 | tflags &= ~LEX_RESWDOK; |
| 3823 | } |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3824 | else if MBTEST((tflags & LEX_CKCOMMENT) && ch == '#' && (lex_rwlen == 0 || ((tflags & LEX_INWORD) && lex_wlen == 0))) |
| 3825 | ; /* don't modify LEX_RESWDOK if we're starting a comment */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3826 | /* Allow `do' followed by space, tab, or newline to preserve the |
| 3827 | RESWDOK flag, but reset the reserved word length counter so we |
| 3828 | can read another one. */ |
| 3829 | else if MBTEST(((tflags & LEX_INCASE) == 0) && |
| 3830 | (isblank(ch) || ch == '\n') && |
| 3831 | lex_rwlen == 2 && |
| 3832 | STREQN (ret + retind - 2, "do", 2)) |
| 3833 | { |
| 3834 | /*itrace("parse_comsub:%d: lex_incase == 1 found `%c', found \"do\"", line_number, ch);*/ |
| 3835 | lex_rwlen = 0; |
| 3836 | } |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3837 | else if MBTEST((tflags & LEX_INCASE) && ch != '\n') |
| 3838 | /* If we can read a reserved word and we're in case, we're at the |
| 3839 | point where we can read a new pattern list or an esac. We |
| 3840 | handle the esac case above. If we read a newline, we want to |
| 3841 | leave LEX_RESWDOK alone. If we read anything else, we want to |
| 3842 | turn off LEX_RESWDOK, since we're going to read a pattern list. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3843 | { |
| 3844 | tflags &= ~LEX_RESWDOK; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3845 | /*itrace("parse_comsub:%d: lex_incase == 1 found `%c', lex_reswordok -> 0", line_number, ch);*/ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3846 | } |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3847 | else if MBTEST(shellbreak (ch) == 0) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3848 | { |
| 3849 | tflags &= ~LEX_RESWDOK; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3850 | /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3851 | } |
| 3852 | #if 0 |
| 3853 | /* If we find a space or tab but have read something and it's not |
| 3854 | `do', turn off the reserved-word-ok flag */ |
| 3855 | else if MBTEST(isblank (ch) && lex_rwlen > 0) |
| 3856 | { |
| 3857 | tflags &= ~LEX_RESWDOK; |
| 3858 | /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/ |
| 3859 | } |
| 3860 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3863 | /* Might be the start of a here-doc delimiter */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3864 | if MBTEST((tflags & LEX_INCOMMENT) == 0 && (tflags & LEX_CKCASE) && ch == '<') |
| 3865 | { |
| 3866 | /* Add this character. */ |
| 3867 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3868 | ret[retind++] = ch; |
| 3869 | peekc = shell_getc (1); |
| 3870 | if (peekc == EOF) |
| 3871 | goto eof_error; |
| 3872 | if (peekc == ch) |
| 3873 | { |
| 3874 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3875 | ret[retind++] = peekc; |
| 3876 | peekc = shell_getc (1); |
| 3877 | if (peekc == EOF) |
| 3878 | goto eof_error; |
| 3879 | if (peekc == '-') |
| 3880 | { |
| 3881 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3882 | ret[retind++] = peekc; |
| 3883 | tflags |= LEX_STRIPDOC; |
| 3884 | } |
| 3885 | else |
| 3886 | shell_ungetc (peekc); |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3887 | if (peekc != '<') |
| 3888 | { |
| 3889 | tflags |= LEX_HEREDELIM; |
| 3890 | lex_firstind = -1; |
| 3891 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3892 | continue; |
| 3893 | } |
| 3894 | else |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3895 | ch = peekc; /* fall through and continue XXX */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3896 | } |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3897 | else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (((tflags & LEX_RESWDOK) && lex_rwlen == 0) || ((tflags & LEX_INWORD) && lex_wlen == 0))) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3898 | { |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3899 | /*itrace("parse_comsub:%d: lex_incomment -> 1 (%d)", line_number, __LINE__);*/ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3900 | tflags |= LEX_INCOMMENT; |
| 3901 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3902 | |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 3903 | if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3904 | { |
| 3905 | RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64); |
| 3906 | ret[retind++] = CTLESC; |
| 3907 | ret[retind++] = ch; |
| 3908 | continue; |
| 3909 | } |
| 3910 | #if 0 |
| 3911 | else if MBTEST((tflags & LEX_INCASE) && ch == close && close == ')') |
| 3912 | tflags &= ~LEX_INCASE; /* XXX */ |
| 3913 | #endif |
| 3914 | else if MBTEST(ch == close && (tflags & LEX_INCASE) == 0) /* ending delimiter */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3915 | { |
| 3916 | count--; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3917 | /*itrace("parse_comsub:%d: found close: count = %d", line_number, count);*/ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3918 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3919 | else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && (tflags & LEX_INCASE) == 0 && ch == open) /* nested begin */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3920 | { |
| 3921 | count++; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3922 | /*itrace("parse_comsub:%d: found open: count = %d", line_number, count);*/ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3923 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3924 | |
| 3925 | /* Add this character. */ |
| 3926 | RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64); |
| 3927 | ret[retind++] = ch; |
| 3928 | |
| 3929 | /* If we just read the ending character, don't bother continuing. */ |
| 3930 | if (count == 0) |
| 3931 | break; |
| 3932 | |
| 3933 | if MBTEST(ch == '\\') /* backslashes */ |
| 3934 | tflags |= LEX_PASSNEXT; |
| 3935 | |
| 3936 | if MBTEST(shellquote (ch)) |
| 3937 | { |
| 3938 | /* '', ``, or "" inside $(...). */ |
| 3939 | push_delimiter (dstack, ch); |
| 3940 | if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */ |
| 3941 | nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags); |
| 3942 | else |
| 3943 | nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags); |
| 3944 | pop_delimiter (dstack); |
| 3945 | CHECK_NESTRET_ERROR (); |
| 3946 | |
| 3947 | if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0)) |
| 3948 | { |
| 3949 | /* Translate $'...' here. */ |
| 3950 | ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen); |
| 3951 | xfree (nestret); |
| 3952 | |
| 3953 | if ((rflags & P_DQUOTE) == 0) |
| 3954 | { |
| 3955 | nestret = sh_single_quote (ttrans); |
| 3956 | free (ttrans); |
| 3957 | nestlen = strlen (nestret); |
| 3958 | } |
| 3959 | else |
| 3960 | { |
| 3961 | nestret = ttrans; |
| 3962 | nestlen = ttranslen; |
| 3963 | } |
| 3964 | retind -= 2; /* back up before the $' */ |
| 3965 | } |
| 3966 | else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0)) |
| 3967 | { |
| 3968 | /* Locale expand $"..." here. */ |
| 3969 | ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen); |
| 3970 | xfree (nestret); |
| 3971 | |
| 3972 | nestret = sh_mkdoublequoted (ttrans, ttranslen, 0); |
| 3973 | free (ttrans); |
| 3974 | nestlen = ttranslen + 2; |
| 3975 | retind -= 2; /* back up before the $" */ |
| 3976 | } |
| 3977 | |
| 3978 | APPEND_NESTRET (); |
| 3979 | FREE (nestret); |
| 3980 | } |
| 3981 | else if MBTEST((tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */ |
| 3982 | /* check for $(), $[], or ${} inside command substitution. */ |
| 3983 | { |
| 3984 | if ((tflags & LEX_INCASE) == 0 && open == ch) /* undo previous increment */ |
| 3985 | count--; |
| 3986 | if (ch == '(') /* ) */ |
| 3987 | nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE); |
| 3988 | else if (ch == '{') /* } */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3989 | nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3990 | else if (ch == '[') /* ] */ |
| 3991 | nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags); |
| 3992 | |
| 3993 | CHECK_NESTRET_ERROR (); |
| 3994 | APPEND_NESTRET (); |
| 3995 | |
| 3996 | FREE (nestret); |
| 3997 | } |
| 3998 | if MBTEST(ch == '$') |
| 3999 | tflags |= LEX_WASDOL; |
| 4000 | else |
| 4001 | tflags &= ~LEX_WASDOL; |
| 4002 | } |
| 4003 | |
| 4004 | FREE (heredelim); |
| 4005 | ret[retind] = '\0'; |
| 4006 | if (lenp) |
| 4007 | *lenp = retind; |
| 4008 | /*itrace("parse_comsub:%d: returning `%s'", line_number, ret);*/ |
| 4009 | return ret; |
| 4010 | } |
| 4011 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 4012 | /* Recursively call the parser to parse a $(...) command substitution. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4013 | char * |
| 4014 | xparse_dolparen (base, string, indp, flags) |
| 4015 | char *base; |
| 4016 | char *string; |
| 4017 | int *indp; |
| 4018 | int flags; |
| 4019 | { |
| 4020 | sh_parser_state_t ps; |
Chet Ramey | b4839c2 | 2011-11-22 20:02:35 -0500 | [diff] [blame] | 4021 | sh_input_line_state_t ls; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4022 | int orig_ind, nc, sflags, orig_eof_token; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4023 | char *ret, *s, *ep, *ostring; |
| 4024 | |
| 4025 | /*yydebug = 1;*/ |
| 4026 | orig_ind = *indp; |
| 4027 | ostring = string; |
| 4028 | |
Chet Ramey | b4839c2 | 2011-11-22 20:02:35 -0500 | [diff] [blame] | 4029 | /*itrace("xparse_dolparen: size = %d shell_input_line = `%s'", shell_input_line_size, shell_input_line);*/ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4030 | sflags = SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOFREE; |
| 4031 | if (flags & SX_NOLONGJMP) |
| 4032 | sflags |= SEVAL_NOLONGJMP; |
| 4033 | save_parser_state (&ps); |
Chet Ramey | b4839c2 | 2011-11-22 20:02:35 -0500 | [diff] [blame] | 4034 | save_input_line_state (&ls); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4035 | orig_eof_token = shell_eof_token; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4036 | |
| 4037 | /*(*/ |
| 4038 | parser_state |= PST_CMDSUBST|PST_EOFTOKEN; /* allow instant ')' */ /*(*/ |
| 4039 | shell_eof_token = ')'; |
| 4040 | parse_string (string, "command substitution", sflags, &ep); |
| 4041 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4042 | shell_eof_token = orig_eof_token; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4043 | restore_parser_state (&ps); |
| 4044 | reset_parser (); |
Chet Ramey | b4839c2 | 2011-11-22 20:02:35 -0500 | [diff] [blame] | 4045 | /* reset_parser clears shell_input_line and associated variables */ |
| 4046 | restore_input_line_state (&ls); |
Chet Ramey | 80b4d71 | 2014-10-05 19:12:20 -0400 | [diff] [blame] | 4047 | |
| 4048 | token_to_read = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4049 | |
| 4050 | /* Need to find how many characters parse_and_execute consumed, update |
| 4051 | *indp, if flags != 0, copy the portion of the string parsed into RET |
| 4052 | and return it. If flags & 1 (EX_NOALLOC) we can return NULL. */ |
| 4053 | |
| 4054 | /*(*/ |
| 4055 | if (ep[-1] != ')') |
| 4056 | { |
| 4057 | #if DEBUG |
| 4058 | if (ep[-1] != '\n') |
| 4059 | itrace("xparse_dolparen:%d: ep[-1] != RPAREN (%d), ep = `%s'", line_number, ep[-1], ep); |
| 4060 | #endif |
| 4061 | while (ep > ostring && ep[-1] == '\n') ep--; |
| 4062 | } |
| 4063 | |
| 4064 | nc = ep - ostring; |
| 4065 | *indp = ep - base - 1; |
| 4066 | |
| 4067 | /*(*/ |
| 4068 | #if DEBUG |
| 4069 | if (base[*indp] != ')') |
| 4070 | itrace("xparse_dolparen:%d: base[%d] != RPAREN (%d), base = `%s'", line_number, *indp, base[*indp], base); |
| 4071 | #endif |
| 4072 | |
| 4073 | if (flags & SX_NOALLOC) |
| 4074 | return (char *)NULL; |
| 4075 | |
| 4076 | if (nc == 0) |
| 4077 | { |
| 4078 | ret = xmalloc (1); |
| 4079 | ret[0] = '\0'; |
| 4080 | } |
| 4081 | else |
| 4082 | ret = substring (ostring, 0, nc - 1); |
| 4083 | |
| 4084 | return ret; |
| 4085 | } |
| 4086 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4087 | #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4088 | /* Parse a double-paren construct. It can be either an arithmetic |
| 4089 | command, an arithmetic `for' command, or a nested subshell. Returns |
| 4090 | the parsed token, -1 on error, or -2 if we didn't do anything and |
| 4091 | should just go on. */ |
| 4092 | static int |
| 4093 | parse_dparen (c) |
| 4094 | int c; |
| 4095 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 4096 | int cmdtyp, sline; |
| 4097 | char *wval; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4098 | WORD_DESC *wd; |
| 4099 | |
| 4100 | #if defined (ARITH_FOR_COMMAND) |
| 4101 | if (last_read_token == FOR) |
| 4102 | { |
| 4103 | arith_for_lineno = line_number; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4104 | cmdtyp = parse_arith_cmd (&wval, 0); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4105 | if (cmdtyp == 1) |
| 4106 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4107 | wd = alloc_word_desc (); |
| 4108 | wd->word = wval; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4109 | yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4110 | return (ARITH_FOR_EXPRS); |
| 4111 | } |
| 4112 | else |
| 4113 | return -1; /* ERROR */ |
| 4114 | } |
| 4115 | #endif |
| 4116 | |
| 4117 | #if defined (DPAREN_ARITHMETIC) |
| 4118 | if (reserved_word_acceptable (last_read_token)) |
| 4119 | { |
| 4120 | sline = line_number; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4121 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4122 | cmdtyp = parse_arith_cmd (&wval, 0); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4123 | if (cmdtyp == 1) /* arithmetic command */ |
| 4124 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4125 | wd = alloc_word_desc (); |
| 4126 | wd->word = wval; |
| 4127 | wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4128 | yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4129 | return (ARITH_CMD); |
| 4130 | } |
| 4131 | else if (cmdtyp == 0) /* nested subshell */ |
| 4132 | { |
| 4133 | push_string (wval, 0, (alias_t *)NULL); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4134 | pushed_string_list->flags = PSH_DPAREN; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4135 | if ((parser_state & PST_CASEPAT) == 0) |
| 4136 | parser_state |= PST_SUBSHELL; |
| 4137 | return (c); |
| 4138 | } |
| 4139 | else /* ERROR */ |
| 4140 | return -1; |
| 4141 | } |
| 4142 | #endif |
| 4143 | |
| 4144 | return -2; /* XXX */ |
| 4145 | } |
| 4146 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4147 | /* We've seen a `(('. Look for the matching `))'. If we get it, return 1. |
| 4148 | If not, assume it's a nested subshell for backwards compatibility and |
| 4149 | return 0. In any case, put the characters we've consumed into a locally- |
| 4150 | allocated buffer and make *ep point to that buffer. Return -1 on an |
| 4151 | error, for example EOF. */ |
| 4152 | static int |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4153 | parse_arith_cmd (ep, adddq) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4154 | char **ep; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4155 | int adddq; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4156 | { |
| 4157 | int exp_lineno, rval, c; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4158 | char *ttok, *tokstr; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4159 | int ttoklen; |
| 4160 | |
| 4161 | exp_lineno = line_number; |
| 4162 | ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0); |
| 4163 | rval = 1; |
| 4164 | if (ttok == &matched_pair_error) |
| 4165 | return -1; |
| 4166 | /* Check that the next character is the closing right paren. If |
| 4167 | not, this is a syntax error. ( */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4168 | c = shell_getc (0); |
| 4169 | if MBTEST(c != ')') |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4170 | rval = 0; |
| 4171 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4172 | tokstr = (char *)xmalloc (ttoklen + 4); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4173 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4174 | /* if ADDDQ != 0 then (( ... )) -> "..." */ |
| 4175 | if (rval == 1 && adddq) /* arith cmd, add double quotes */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4176 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4177 | tokstr[0] = '"'; |
| 4178 | strncpy (tokstr + 1, ttok, ttoklen - 1); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4179 | tokstr[ttoklen] = '"'; |
| 4180 | tokstr[ttoklen+1] = '\0'; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4181 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4182 | else if (rval == 1) /* arith cmd, don't add double quotes */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4183 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4184 | strncpy (tokstr, ttok, ttoklen - 1); |
| 4185 | tokstr[ttoklen-1] = '\0'; |
| 4186 | } |
| 4187 | else /* nested subshell */ |
| 4188 | { |
| 4189 | tokstr[0] = '('; |
| 4190 | strncpy (tokstr + 1, ttok, ttoklen - 1); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4191 | tokstr[ttoklen] = ')'; |
| 4192 | tokstr[ttoklen+1] = c; |
| 4193 | tokstr[ttoklen+2] = '\0'; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4194 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4195 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4196 | *ep = tokstr; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4197 | FREE (ttok); |
| 4198 | return rval; |
| 4199 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4200 | #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4201 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4202 | #if defined (COND_COMMAND) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4203 | static void |
| 4204 | cond_error () |
| 4205 | { |
| 4206 | char *etext; |
| 4207 | |
| 4208 | if (EOF_Reached && cond_token != COND_ERROR) /* [[ */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4209 | parser_error (cond_lineno, _("unexpected EOF while looking for `]]'")); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4210 | else if (cond_token != COND_ERROR) |
| 4211 | { |
| 4212 | if (etext = error_token_from_token (cond_token)) |
| 4213 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4214 | 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] | 4215 | free (etext); |
| 4216 | } |
| 4217 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4218 | parser_error (cond_lineno, _("syntax error in conditional expression")); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4219 | } |
| 4220 | } |
| 4221 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4222 | static COND_COM * |
| 4223 | cond_expr () |
| 4224 | { |
| 4225 | return (cond_or ()); |
| 4226 | } |
| 4227 | |
| 4228 | static COND_COM * |
| 4229 | cond_or () |
| 4230 | { |
| 4231 | COND_COM *l, *r; |
| 4232 | |
| 4233 | l = cond_and (); |
| 4234 | if (cond_token == OR_OR) |
| 4235 | { |
| 4236 | r = cond_or (); |
| 4237 | l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r); |
| 4238 | } |
| 4239 | return l; |
| 4240 | } |
| 4241 | |
| 4242 | static COND_COM * |
| 4243 | cond_and () |
| 4244 | { |
| 4245 | COND_COM *l, *r; |
| 4246 | |
| 4247 | l = cond_term (); |
| 4248 | if (cond_token == AND_AND) |
| 4249 | { |
| 4250 | r = cond_and (); |
| 4251 | l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r); |
| 4252 | } |
| 4253 | return l; |
| 4254 | } |
| 4255 | |
| 4256 | static int |
| 4257 | cond_skip_newlines () |
| 4258 | { |
| 4259 | while ((cond_token = read_token (READ)) == '\n') |
| 4260 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4261 | if (SHOULD_PROMPT ()) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4262 | prompt_again (); |
| 4263 | } |
| 4264 | return (cond_token); |
| 4265 | } |
| 4266 | |
| 4267 | #define COND_RETURN_ERROR() \ |
| 4268 | do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0) |
| 4269 | |
| 4270 | static COND_COM * |
| 4271 | cond_term () |
| 4272 | { |
| 4273 | WORD_DESC *op; |
| 4274 | COND_COM *term, *tleft, *tright; |
| 4275 | int tok, lineno; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4276 | char *etext; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4277 | |
| 4278 | /* Read a token. It can be a left paren, a `!', a unary operator, or a |
| 4279 | word that should be the first argument of a binary operator. Start by |
| 4280 | skipping newlines, since this is a compound command. */ |
| 4281 | tok = cond_skip_newlines (); |
| 4282 | lineno = line_number; |
| 4283 | if (tok == COND_END) |
| 4284 | { |
| 4285 | COND_RETURN_ERROR (); |
| 4286 | } |
| 4287 | else if (tok == '(') |
| 4288 | { |
| 4289 | term = cond_expr (); |
| 4290 | if (cond_token != ')') |
| 4291 | { |
| 4292 | if (term) |
| 4293 | dispose_cond_node (term); /* ( */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4294 | if (etext = error_token_from_token (cond_token)) |
| 4295 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4296 | parser_error (lineno, _("unexpected token `%s', expected `)'"), etext); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4297 | free (etext); |
| 4298 | } |
| 4299 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4300 | parser_error (lineno, _("expected `)'")); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4301 | COND_RETURN_ERROR (); |
| 4302 | } |
| 4303 | term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL); |
| 4304 | (void)cond_skip_newlines (); |
| 4305 | } |
| 4306 | else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0'))) |
| 4307 | { |
| 4308 | if (tok == WORD) |
| 4309 | dispose_word (yylval.word); /* not needed */ |
| 4310 | term = cond_term (); |
| 4311 | if (term) |
| 4312 | term->flags |= CMD_INVERT_RETURN; |
| 4313 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4314 | else if (tok == WORD && yylval.word->word[0] == '-' && yylval.word->word[2] == 0 && test_unop (yylval.word->word)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4315 | { |
| 4316 | op = yylval.word; |
| 4317 | tok = read_token (READ); |
| 4318 | if (tok == WORD) |
| 4319 | { |
| 4320 | tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL); |
| 4321 | term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL); |
| 4322 | } |
| 4323 | else |
| 4324 | { |
| 4325 | dispose_word (op); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4326 | if (etext = error_token_from_token (tok)) |
| 4327 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4328 | parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4329 | free (etext); |
| 4330 | } |
| 4331 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4332 | parser_error (line_number, _("unexpected argument to conditional unary operator")); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4333 | COND_RETURN_ERROR (); |
| 4334 | } |
| 4335 | |
| 4336 | (void)cond_skip_newlines (); |
| 4337 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4338 | else if (tok == WORD) /* left argument to binary operator */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4339 | { |
| 4340 | /* lhs */ |
| 4341 | tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL); |
| 4342 | |
| 4343 | /* binop */ |
| 4344 | tok = read_token (READ); |
| 4345 | if (tok == WORD && test_binop (yylval.word->word)) |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4346 | { |
| 4347 | op = yylval.word; |
| 4348 | if (op->word[0] == '=' && (op->word[1] == '\0' || (op->word[1] == '=' && op->word[2] == '\0'))) |
| 4349 | parser_state |= PST_EXTPAT; |
| 4350 | else if (op->word[0] == '!' && op->word[1] == '=' && op->word[2] == '\0') |
| 4351 | parser_state |= PST_EXTPAT; |
| 4352 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4353 | #if defined (COND_REGEXP) |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 4354 | else if (tok == WORD && STREQ (yylval.word->word, "=~")) |
| 4355 | { |
| 4356 | op = yylval.word; |
| 4357 | parser_state |= PST_REGEXP; |
| 4358 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4359 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4360 | else if (tok == '<' || tok == '>') |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4361 | op = make_word_from_token (tok); /* ( */ |
| 4362 | /* There should be a check before blindly accepting the `)' that we have |
| 4363 | seen the opening `('. */ |
| 4364 | else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')') |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4365 | { |
| 4366 | /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like |
| 4367 | the test command. Similarly for [[ x && expr ]] or |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4368 | [[ x || expr ]] or [[ (x) ]]. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4369 | op = make_word ("-n"); |
| 4370 | term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL); |
| 4371 | cond_token = tok; |
| 4372 | return (term); |
| 4373 | } |
| 4374 | else |
| 4375 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4376 | if (etext = error_token_from_token (tok)) |
| 4377 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4378 | parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4379 | free (etext); |
| 4380 | } |
| 4381 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4382 | parser_error (line_number, _("conditional binary operator expected")); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4383 | dispose_cond_node (tleft); |
| 4384 | COND_RETURN_ERROR (); |
| 4385 | } |
| 4386 | |
| 4387 | /* rhs */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4388 | if (parser_state & PST_EXTPAT) |
| 4389 | extended_glob = 1; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4390 | tok = read_token (READ); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4391 | if (parser_state & PST_EXTPAT) |
| 4392 | extended_glob = global_extglob; |
| 4393 | parser_state &= ~(PST_REGEXP|PST_EXTPAT); |
| 4394 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4395 | if (tok == WORD) |
| 4396 | { |
| 4397 | tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL); |
| 4398 | term = make_cond_node (COND_BINARY, op, tleft, tright); |
| 4399 | } |
| 4400 | else |
| 4401 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4402 | if (etext = error_token_from_token (tok)) |
| 4403 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4404 | parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4405 | free (etext); |
| 4406 | } |
| 4407 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4408 | parser_error (line_number, _("unexpected argument to conditional binary operator")); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4409 | dispose_cond_node (tleft); |
| 4410 | dispose_word (op); |
| 4411 | COND_RETURN_ERROR (); |
| 4412 | } |
| 4413 | |
| 4414 | (void)cond_skip_newlines (); |
| 4415 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4416 | else |
| 4417 | { |
| 4418 | if (tok < 256) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4419 | parser_error (line_number, _("unexpected token `%c' in conditional command"), tok); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4420 | else if (etext = error_token_from_token (tok)) |
| 4421 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4422 | parser_error (line_number, _("unexpected token `%s' in conditional command"), etext); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4423 | free (etext); |
| 4424 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4425 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4426 | parser_error (line_number, _("unexpected token %d in conditional command"), tok); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4427 | COND_RETURN_ERROR (); |
| 4428 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4429 | return (term); |
| 4430 | } |
| 4431 | |
| 4432 | /* This is kind of bogus -- we slip a mini recursive-descent parser in |
| 4433 | here to handle the conditional statement syntax. */ |
| 4434 | static COMMAND * |
| 4435 | parse_cond_command () |
| 4436 | { |
| 4437 | COND_COM *cexp; |
| 4438 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4439 | global_extglob = extended_glob; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4440 | cexp = cond_expr (); |
| 4441 | return (make_cond_command (cexp)); |
| 4442 | } |
| 4443 | #endif |
| 4444 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4445 | #if defined (ARRAY_VARS) |
| 4446 | /* When this is called, it's guaranteed that we don't care about anything |
| 4447 | in t beyond i. We do save and restore the chars, though. */ |
| 4448 | static int |
| 4449 | token_is_assignment (t, i) |
| 4450 | char *t; |
| 4451 | int i; |
| 4452 | { |
| 4453 | unsigned char c, c1; |
| 4454 | int r; |
| 4455 | |
| 4456 | c = t[i]; c1 = t[i+1]; |
| 4457 | t[i] = '='; t[i+1] = '\0'; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4458 | r = assignment (t, (parser_state & PST_COMPASSIGN) != 0); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4459 | t[i] = c; t[i+1] = c1; |
| 4460 | return r; |
| 4461 | } |
| 4462 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4463 | /* XXX - possible changes here for `+=' */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4464 | static int |
| 4465 | token_is_ident (t, i) |
| 4466 | char *t; |
| 4467 | int i; |
| 4468 | { |
| 4469 | unsigned char c; |
| 4470 | int r; |
| 4471 | |
| 4472 | c = t[i]; |
| 4473 | t[i] = '\0'; |
| 4474 | r = legal_identifier (t); |
| 4475 | t[i] = c; |
| 4476 | return r; |
| 4477 | } |
| 4478 | #endif |
| 4479 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4480 | static int |
| 4481 | read_token_word (character) |
| 4482 | int character; |
| 4483 | { |
| 4484 | /* The value for YYLVAL when a WORD is read. */ |
| 4485 | WORD_DESC *the_word; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4486 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4487 | /* Index into the token that we are building. */ |
| 4488 | int token_index; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4489 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4490 | /* ALL_DIGITS becomes zero when we see a non-digit. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4491 | int all_digit_token; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4492 | |
| 4493 | /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */ |
| 4494 | int dollar_present; |
| 4495 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4496 | /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound |
| 4497 | assignment. */ |
| 4498 | int compound_assignment; |
| 4499 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4500 | /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */ |
| 4501 | int quoted; |
| 4502 | |
| 4503 | /* Non-zero means to ignore the value of the next character, and just |
| 4504 | to add it no matter what. */ |
| 4505 | int pass_next_character; |
| 4506 | |
| 4507 | /* The current delimiting character. */ |
| 4508 | int cd; |
| 4509 | int result, peek_char; |
| 4510 | char *ttok, *ttrans; |
| 4511 | int ttoklen, ttranslen; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4512 | intmax_t lvalue; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4513 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4514 | if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4515 | token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4516 | |
| 4517 | token_index = 0; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4518 | all_digit_token = DIGIT (character); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4519 | dollar_present = quoted = pass_next_character = compound_assignment = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4520 | |
| 4521 | for (;;) |
| 4522 | { |
| 4523 | if (character == EOF) |
| 4524 | goto got_token; |
| 4525 | |
| 4526 | if (pass_next_character) |
| 4527 | { |
| 4528 | pass_next_character = 0; |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 4529 | goto got_escaped_character; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | cd = current_delimiter (dstack); |
| 4533 | |
| 4534 | /* Handle backslashes. Quote lots of things when not inside of |
| 4535 | double-quotes, quote some things inside of double-quotes. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4536 | if MBTEST(character == '\\') |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4537 | { |
| 4538 | peek_char = shell_getc (0); |
| 4539 | |
| 4540 | /* Backslash-newline is ignored in all cases except |
| 4541 | when quoted with single quotes. */ |
| 4542 | if (peek_char == '\n') |
| 4543 | { |
| 4544 | character = '\n'; |
| 4545 | goto next_character; |
| 4546 | } |
| 4547 | else |
| 4548 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4549 | shell_ungetc (peek_char); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4550 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4551 | /* If the next character is to be quoted, note it now. */ |
| 4552 | if (cd == 0 || cd == '`' || |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4553 | (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE))) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4554 | pass_next_character++; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4555 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4556 | quoted = 1; |
| 4557 | goto got_character; |
| 4558 | } |
| 4559 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4560 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4561 | /* Parse a matched pair of quote characters. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4562 | if MBTEST(shellquote (character)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4563 | { |
| 4564 | push_delimiter (dstack, character); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4565 | ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4566 | pop_delimiter (dstack); |
| 4567 | if (ttok == &matched_pair_error) |
| 4568 | return -1; /* Bail immediately. */ |
| 4569 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2, |
| 4570 | token_buffer_size, TOKEN_DEFAULT_GROW_SIZE); |
| 4571 | token[token_index++] = character; |
| 4572 | strcpy (token + token_index, ttok); |
| 4573 | token_index += ttoklen; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4574 | all_digit_token = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4575 | quoted = 1; |
| 4576 | dollar_present |= (character == '"' && strchr (ttok, '$') != 0); |
| 4577 | FREE (ttok); |
| 4578 | goto next_character; |
| 4579 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4580 | |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 4581 | #ifdef COND_REGEXP |
| 4582 | /* When parsing a regexp as a single word inside a conditional command, |
| 4583 | we need to special-case characters special to both the shell and |
| 4584 | regular expressions. Right now, that is only '(' and '|'. */ /*)*/ |
| 4585 | if MBTEST((parser_state & PST_REGEXP) && (character == '(' || character == '|')) /*)*/ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4586 | { |
| 4587 | if (character == '|') |
| 4588 | goto got_character; |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 4589 | |
| 4590 | push_delimiter (dstack, character); |
| 4591 | ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0); |
| 4592 | pop_delimiter (dstack); |
| 4593 | if (ttok == &matched_pair_error) |
| 4594 | return -1; /* Bail immediately. */ |
| 4595 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2, |
| 4596 | token_buffer_size, TOKEN_DEFAULT_GROW_SIZE); |
| 4597 | token[token_index++] = character; |
| 4598 | strcpy (token + token_index, ttok); |
| 4599 | token_index += ttoklen; |
| 4600 | FREE (ttok); |
| 4601 | dollar_present = all_digit_token = 0; |
| 4602 | goto next_character; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4603 | } |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 4604 | #endif /* COND_REGEXP */ |
| 4605 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4606 | #ifdef EXTENDED_GLOB |
| 4607 | /* Parse a ksh-style extended pattern matching specification. */ |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 4608 | if MBTEST(extended_glob && PATTERN_CHAR (character)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4609 | { |
| 4610 | peek_char = shell_getc (1); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4611 | if MBTEST(peek_char == '(') /* ) */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4612 | { |
| 4613 | push_delimiter (dstack, peek_char); |
| 4614 | ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0); |
| 4615 | pop_delimiter (dstack); |
| 4616 | if (ttok == &matched_pair_error) |
| 4617 | return -1; /* Bail immediately. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4618 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3, |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4619 | token_buffer_size, |
| 4620 | TOKEN_DEFAULT_GROW_SIZE); |
| 4621 | token[token_index++] = character; |
| 4622 | token[token_index++] = peek_char; |
| 4623 | strcpy (token + token_index, ttok); |
| 4624 | token_index += ttoklen; |
| 4625 | FREE (ttok); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4626 | dollar_present = all_digit_token = 0; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4627 | goto next_character; |
| 4628 | } |
| 4629 | else |
| 4630 | shell_ungetc (peek_char); |
| 4631 | } |
| 4632 | #endif /* EXTENDED_GLOB */ |
| 4633 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4634 | /* If the delimiter character is not single quote, parse some of |
| 4635 | the shell expansions that must be read as a single word. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4636 | if (shellexp (character)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4637 | { |
| 4638 | peek_char = shell_getc (1); |
| 4639 | /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4640 | if MBTEST(peek_char == '(' || |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4641 | ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */ |
| 4642 | { |
| 4643 | if (peek_char == '{') /* } */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 4644 | ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE|P_DOLBRACE); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4645 | else if (peek_char == '(') /* ) */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4646 | { |
| 4647 | /* XXX - push and pop the `(' as a delimiter for use by |
| 4648 | the command-oriented-history code. This way newlines |
| 4649 | appearing in the $(...) string get added to the |
| 4650 | history literally rather than causing a possibly- |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4651 | incorrect `;' to be added. ) */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4652 | push_delimiter (dstack, peek_char); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4653 | ttok = parse_comsub (cd, '(', ')', &ttoklen, P_COMMAND); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4654 | pop_delimiter (dstack); |
| 4655 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4656 | else |
| 4657 | ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0); |
| 4658 | if (ttok == &matched_pair_error) |
| 4659 | return -1; /* Bail immediately. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4660 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3, |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4661 | token_buffer_size, |
| 4662 | TOKEN_DEFAULT_GROW_SIZE); |
| 4663 | token[token_index++] = character; |
| 4664 | token[token_index++] = peek_char; |
| 4665 | strcpy (token + token_index, ttok); |
| 4666 | token_index += ttoklen; |
| 4667 | FREE (ttok); |
| 4668 | dollar_present = 1; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4669 | all_digit_token = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4670 | goto next_character; |
| 4671 | } |
| 4672 | /* This handles $'...' and $"..." new-style quoted strings. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4673 | else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"')) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4674 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4675 | int first_line; |
| 4676 | |
| 4677 | first_line = line_number; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4678 | push_delimiter (dstack, peek_char); |
| 4679 | ttok = parse_matched_pair (peek_char, peek_char, peek_char, |
| 4680 | &ttoklen, |
| 4681 | (peek_char == '\'') ? P_ALLOWESC : 0); |
| 4682 | pop_delimiter (dstack); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4683 | if (ttok == &matched_pair_error) |
| 4684 | return -1; |
| 4685 | if (peek_char == '\'') |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4686 | { |
| 4687 | ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen); |
| 4688 | free (ttok); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4689 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4690 | /* Insert the single quotes and correctly quote any |
| 4691 | embedded single quotes (allowed because P_ALLOWESC was |
| 4692 | passed to parse_matched_pair). */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4693 | ttok = sh_single_quote (ttrans); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4694 | free (ttrans); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4695 | ttranslen = strlen (ttok); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4696 | ttrans = ttok; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4697 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4698 | else |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4699 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 4700 | /* Try to locale-expand the converted string. */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4701 | ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen); |
| 4702 | free (ttok); |
| 4703 | |
| 4704 | /* Add the double quotes back */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4705 | ttok = sh_mkdoublequoted (ttrans, ttranslen, 0); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4706 | free (ttrans); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4707 | ttranslen += 2; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4708 | ttrans = ttok; |
| 4709 | } |
| 4710 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4711 | RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 1, |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4712 | token_buffer_size, |
| 4713 | TOKEN_DEFAULT_GROW_SIZE); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4714 | strcpy (token + token_index, ttrans); |
| 4715 | token_index += ttranslen; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4716 | FREE (ttrans); |
| 4717 | quoted = 1; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4718 | all_digit_token = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4719 | goto next_character; |
| 4720 | } |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 4721 | /* This could eventually be extended to recognize all of the |
| 4722 | shell's single-character parameter expansions, and set flags.*/ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4723 | else if MBTEST(character == '$' && peek_char == '$') |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 4724 | { |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 4725 | RESIZE_MALLOCED_BUFFER (token, token_index, 3, |
| 4726 | token_buffer_size, |
| 4727 | TOKEN_DEFAULT_GROW_SIZE); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4728 | token[token_index++] = '$'; |
| 4729 | token[token_index++] = peek_char; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 4730 | dollar_present = 1; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4731 | all_digit_token = 0; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 4732 | goto next_character; |
| 4733 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4734 | else |
| 4735 | shell_ungetc (peek_char); |
| 4736 | } |
| 4737 | |
| 4738 | #if defined (ARRAY_VARS) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4739 | /* Identify possible array subscript assignment; match [...]. If |
| 4740 | parser_state&PST_COMPASSIGN, we need to parse [sub]=words treating |
| 4741 | `sub' as if it were enclosed in double quotes. */ |
| 4742 | else if MBTEST(character == '[' && /* ] */ |
| 4743 | ((token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) || |
| 4744 | (token_index == 0 && (parser_state&PST_COMPASSIGN)))) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4745 | { |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 4746 | ttok = parse_matched_pair (cd, '[', ']', &ttoklen, P_ARRAYSUB); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4747 | if (ttok == &matched_pair_error) |
| 4748 | return -1; /* Bail immediately. */ |
| 4749 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2, |
| 4750 | token_buffer_size, |
| 4751 | TOKEN_DEFAULT_GROW_SIZE); |
| 4752 | token[token_index++] = character; |
| 4753 | strcpy (token + token_index, ttok); |
| 4754 | token_index += ttoklen; |
| 4755 | FREE (ttok); |
| 4756 | all_digit_token = 0; |
| 4757 | goto next_character; |
| 4758 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4759 | /* Identify possible compound array variable assignment. */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4760 | else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4761 | { |
| 4762 | peek_char = shell_getc (1); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4763 | if MBTEST(peek_char == '(') /* ) */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4764 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4765 | ttok = parse_compound_assignment (&ttoklen); |
| 4766 | |
| 4767 | RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4, |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4768 | token_buffer_size, |
| 4769 | TOKEN_DEFAULT_GROW_SIZE); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4770 | |
| 4771 | token[token_index++] = '='; |
| 4772 | token[token_index++] = '('; |
| 4773 | if (ttok) |
| 4774 | { |
| 4775 | strcpy (token + token_index, ttok); |
| 4776 | token_index += ttoklen; |
| 4777 | } |
| 4778 | token[token_index++] = ')'; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4779 | FREE (ttok); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4780 | all_digit_token = 0; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4781 | compound_assignment = 1; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 4782 | #if 1 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4783 | goto next_character; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4784 | #else |
| 4785 | goto got_token; /* ksh93 seems to do this */ |
| 4786 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4787 | } |
| 4788 | else |
| 4789 | shell_ungetc (peek_char); |
| 4790 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4791 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4792 | |
| 4793 | /* When not parsing a multi-character word construct, shell meta- |
| 4794 | characters break words. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4795 | if MBTEST(shellbreak (character)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4796 | { |
| 4797 | shell_ungetc (character); |
| 4798 | goto got_token; |
| 4799 | } |
| 4800 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4801 | got_character: |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4802 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4803 | if (character == CTLESC || character == CTLNUL) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4804 | { |
| 4805 | RESIZE_MALLOCED_BUFFER (token, token_index, 2, token_buffer_size, |
| 4806 | TOKEN_DEFAULT_GROW_SIZE); |
| 4807 | token[token_index++] = CTLESC; |
| 4808 | } |
| 4809 | else |
| 4810 | got_escaped_character: |
| 4811 | RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size, |
| 4812 | TOKEN_DEFAULT_GROW_SIZE); |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 4813 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4814 | token[token_index++] = character; |
| 4815 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4816 | all_digit_token &= DIGIT (character); |
| 4817 | dollar_present |= character == '$'; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4818 | |
| 4819 | next_character: |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4820 | if (character == '\n' && SHOULD_PROMPT ()) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4821 | prompt_again (); |
| 4822 | |
| 4823 | /* We want to remove quoted newlines (that is, a \<newline> pair) |
| 4824 | unless we are within single quotes or pass_next_character is |
| 4825 | set (the shell equivalent of literal-next). */ |
| 4826 | cd = current_delimiter (dstack); |
| 4827 | character = shell_getc (cd != '\'' && pass_next_character == 0); |
| 4828 | } /* end for (;;) */ |
| 4829 | |
| 4830 | got_token: |
| 4831 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4832 | /* Calls to RESIZE_MALLOCED_BUFFER ensure there is sufficient room. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4833 | token[token_index] = '\0'; |
| 4834 | |
| 4835 | /* Check to see what thing we should return. If the last_read_token |
| 4836 | is a `<', or a `&', or the character which ended this token is |
| 4837 | a '>' or '<', then, and ONLY then, is this input token a NUMBER. |
| 4838 | Otherwise, it is just a word, and should be returned as such. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4839 | if MBTEST(all_digit_token && (character == '<' || character == '>' || |
| 4840 | last_read_token == LESS_AND || |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4841 | last_read_token == GREATER_AND)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4842 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4843 | if (legal_number (token, &lvalue) && (int)lvalue == lvalue) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4844 | { |
| 4845 | yylval.number = lvalue; |
| 4846 | return (NUMBER); |
| 4847 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4850 | /* Check for special case tokens. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4851 | result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4852 | if (result >= 0) |
| 4853 | return result; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4854 | |
| 4855 | #if defined (ALIAS) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4856 | /* Posix.2 does not allow reserved words to be aliased, so check for all |
| 4857 | of them, including special cases, before expanding the current token |
| 4858 | as an alias. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4859 | if MBTEST(posixly_correct) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4860 | CHECK_FOR_RESERVED_WORD (token); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4861 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4862 | /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting |
| 4863 | inhibits alias expansion. */ |
| 4864 | if (expand_aliases && quoted == 0) |
| 4865 | { |
| 4866 | result = alias_expand_token (token); |
| 4867 | if (result == RE_READ_TOKEN) |
| 4868 | return (RE_READ_TOKEN); |
| 4869 | else if (result == NO_EXPANSION) |
| 4870 | parser_state &= ~PST_ALEXPNEXT; |
| 4871 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4872 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4873 | /* If not in Posix.2 mode, check for reserved words after alias |
| 4874 | expansion. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4875 | if MBTEST(posixly_correct == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4876 | #endif |
| 4877 | CHECK_FOR_RESERVED_WORD (token); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4878 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4879 | the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4880 | the_word->word = (char *)xmalloc (1 + token_index); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4881 | the_word->flags = 0; |
| 4882 | strcpy (the_word->word, token); |
| 4883 | if (dollar_present) |
| 4884 | the_word->flags |= W_HASDOLLAR; |
| 4885 | if (quoted) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 4886 | the_word->flags |= W_QUOTED; /*(*/ |
| 4887 | if (compound_assignment && token[token_index-1] == ')') |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4888 | the_word->flags |= W_COMPASSIGN; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4889 | /* A word is an assignment if it appears at the beginning of a |
| 4890 | simple command, or after another assignment word. This is |
| 4891 | context-dependent, so it cannot be handled in the grammar. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4892 | if (assignment (token, (parser_state & PST_COMPASSIGN) != 0)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4893 | { |
| 4894 | the_word->flags |= W_ASSIGNMENT; |
| 4895 | /* Don't perform word splitting on assignment statements. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4896 | if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4897 | { |
| 4898 | the_word->flags |= W_NOSPLIT; |
| 4899 | if (parser_state & PST_COMPASSIGN) |
| 4900 | the_word->flags |= W_NOGLOB; /* XXX - W_NOBRACE? */ |
| 4901 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4902 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4903 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4904 | if (command_token_position (last_read_token)) |
| 4905 | { |
| 4906 | struct builtin *b; |
| 4907 | b = builtin_address_internal (token, 0); |
| 4908 | if (b && (b->flags & ASSIGNMENT_BUILTIN)) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 4909 | parser_state |= PST_ASSIGNOK; |
| 4910 | else if (STREQ (token, "eval") || STREQ (token, "let")) |
| 4911 | parser_state |= PST_ASSIGNOK; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4914 | yylval.word = the_word; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4915 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4916 | if (token[0] == '{' && token[token_index-1] == '}' && |
| 4917 | (character == '<' || character == '>')) |
| 4918 | { |
| 4919 | /* can use token; already copied to the_word */ |
| 4920 | token[token_index-1] = '\0'; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4921 | #if defined (ARRAY_VARS) |
| 4922 | if (legal_identifier (token+1) || valid_array_reference (token+1)) |
| 4923 | #else |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4924 | if (legal_identifier (token+1)) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4925 | #endif |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4926 | { |
| 4927 | strcpy (the_word->word, token+1); |
| 4928 | /*itrace("read_token_word: returning REDIR_WORD for %s", the_word->word);*/ |
| 4929 | return (REDIR_WORD); |
| 4930 | } |
| 4931 | } |
| 4932 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4933 | result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT)) |
| 4934 | ? ASSIGNMENT_WORD : WORD; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4935 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4936 | switch (last_read_token) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4937 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4938 | case FUNCTION: |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4939 | parser_state |= PST_ALLOWOPNBRC; |
| 4940 | function_dstart = line_number; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4941 | break; |
| 4942 | case CASE: |
| 4943 | case SELECT: |
| 4944 | case FOR: |
| 4945 | if (word_top < MAX_CASE_NEST) |
| 4946 | word_top++; |
| 4947 | word_lineno[word_top] = line_number; |
| 4948 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4949 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4950 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4951 | return (result); |
| 4952 | } |
| 4953 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4954 | /* 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] | 4955 | a reserved word to be seen, else 0. */ |
| 4956 | static int |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4957 | reserved_word_acceptable (toksym) |
| 4958 | int toksym; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4959 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4960 | switch (toksym) |
| 4961 | { |
| 4962 | case '\n': |
| 4963 | case ';': |
| 4964 | case '(': |
| 4965 | case ')': |
| 4966 | case '|': |
| 4967 | case '&': |
| 4968 | case '{': |
| 4969 | case '}': /* XXX */ |
| 4970 | case AND_AND: |
| 4971 | case BANG: |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 4972 | case BAR_AND: |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4973 | case DO: |
| 4974 | case DONE: |
| 4975 | case ELIF: |
| 4976 | case ELSE: |
| 4977 | case ESAC: |
| 4978 | case FI: |
| 4979 | case IF: |
| 4980 | case OR_OR: |
| 4981 | case SEMI_SEMI: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4982 | case SEMI_AND: |
| 4983 | case SEMI_SEMI_AND: |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4984 | case THEN: |
| 4985 | case TIME: |
| 4986 | case TIMEOPT: |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 4987 | case TIMEIGN: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4988 | case COPROC: |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4989 | case UNTIL: |
| 4990 | case WHILE: |
| 4991 | case 0: |
| 4992 | return 1; |
| 4993 | default: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4994 | #if defined (COPROCESS_SUPPORT) |
| 4995 | if (last_read_token == WORD && token_before_that == COPROC) |
| 4996 | return 1; |
| 4997 | #endif |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 4998 | if (last_read_token == WORD && token_before_that == FUNCTION) |
| 4999 | return 1; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5000 | return 0; |
| 5001 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5002 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5003 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5004 | /* Return the index of TOKEN in the alist of reserved words, or -1 if |
| 5005 | TOKEN is not a shell reserved word. */ |
| 5006 | int |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5007 | find_reserved_word (tokstr) |
| 5008 | char *tokstr; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5009 | { |
| 5010 | int i; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5011 | for (i = 0; word_token_alist[i].word; i++) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5012 | if (STREQ (tokstr, word_token_alist[i].word)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5013 | return i; |
| 5014 | return -1; |
| 5015 | } |
| 5016 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 5017 | /* An interface to let the rest of the shell (primarily the completion |
| 5018 | system) know what the parser is expecting. */ |
| 5019 | int |
| 5020 | parser_in_command_position () |
| 5021 | { |
| 5022 | return (command_token_position (last_read_token)); |
| 5023 | } |
| 5024 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5025 | #if 0 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5026 | #if defined (READLINE) |
| 5027 | /* Called after each time readline is called. This insures that whatever |
| 5028 | the new prompt string is gets propagated to readline's local prompt |
| 5029 | variable. */ |
| 5030 | static void |
| 5031 | reset_readline_prompt () |
| 5032 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5033 | char *temp_prompt; |
| 5034 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5035 | if (prompt_string_pointer) |
| 5036 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5037 | temp_prompt = (*prompt_string_pointer) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5038 | ? decode_prompt_string (*prompt_string_pointer) |
| 5039 | : (char *)NULL; |
| 5040 | |
| 5041 | if (temp_prompt == 0) |
| 5042 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5043 | temp_prompt = (char *)xmalloc (1); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5044 | temp_prompt[0] = '\0'; |
| 5045 | } |
| 5046 | |
| 5047 | FREE (current_readline_prompt); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5048 | current_readline_prompt = temp_prompt; |
| 5049 | } |
| 5050 | } |
| 5051 | #endif /* READLINE */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5052 | #endif /* 0 */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5053 | |
| 5054 | #if defined (HISTORY) |
| 5055 | /* A list of tokens which can be followed by newlines, but not by |
| 5056 | semi-colons. When concatenating multiple lines of history, the |
| 5057 | newline separator for such tokens is replaced with a space. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5058 | static const int no_semi_successors[] = { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5059 | '\n', '{', '(', ')', ';', '&', '|', |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5060 | CASE, DO, ELSE, IF, SEMI_SEMI, SEMI_AND, SEMI_SEMI_AND, THEN, UNTIL, |
| 5061 | WHILE, AND_AND, OR_OR, IN, |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5062 | 0 |
| 5063 | }; |
| 5064 | |
| 5065 | /* If we are not within a delimited expression, try to be smart |
| 5066 | about which separators can be semi-colons and which must be |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5067 | newlines. Returns the string that should be added into the |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 5068 | history entry. LINE is the line we're about to add; it helps |
| 5069 | make some more intelligent decisions in certain cases. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5070 | char * |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 5071 | history_delimiting_chars (line) |
| 5072 | const char *line; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5073 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 5074 | static int last_was_heredoc = 0; /* was the last entry the start of a here document? */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5075 | register int i; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5076 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 5077 | if ((parser_state & PST_HEREDOC) == 0) |
| 5078 | last_was_heredoc = 0; |
| 5079 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5080 | if (dstack.delimiter_depth != 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5081 | return ("\n"); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5082 | |
| 5083 | /* We look for current_command_line_count == 2 because we are looking to |
| 5084 | add the first line of the body of the here document (the second line |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 5085 | of the command). We also keep LAST_WAS_HEREDOC as a private sentinel |
| 5086 | variable to note when we think we added the first line of a here doc |
| 5087 | (the one with a "<<" somewhere in it) */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5088 | if (parser_state & PST_HEREDOC) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 5089 | { |
| 5090 | if (last_was_heredoc) |
| 5091 | { |
| 5092 | last_was_heredoc = 0; |
| 5093 | return "\n"; |
| 5094 | } |
| 5095 | return (current_command_line_count == 2 ? "\n" : ""); |
| 5096 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5097 | |
Chet Ramey | 801b47c | 2012-07-10 09:44:32 -0400 | [diff] [blame] | 5098 | if (parser_state & PST_COMPASSIGN) |
| 5099 | return (" "); |
| 5100 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5101 | /* First, handle some special cases. */ |
| 5102 | /*(*/ |
| 5103 | /* If we just read `()', assume it's a function definition, and don't |
| 5104 | add a semicolon. If the token before the `)' was not `(', and we're |
| 5105 | not in the midst of parsing a case statement, assume it's a |
| 5106 | parenthesized command and add the semicolon. */ |
| 5107 | /*)(*/ |
| 5108 | if (token_before_that == ')') |
| 5109 | { |
| 5110 | if (two_tokens_ago == '(') /*)*/ /* function def */ |
| 5111 | return " "; |
| 5112 | /* This does not work for subshells inside case statement |
| 5113 | command lists. It's a suboptimal solution. */ |
| 5114 | else if (parser_state & PST_CASESTMT) /* case statement pattern */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5115 | return " "; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5116 | else |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5117 | return "; "; /* (...) subshell */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5118 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 5119 | else if (token_before_that == WORD && two_tokens_ago == FUNCTION) |
| 5120 | return " "; /* function def using `function name' without `()' */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5121 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 5122 | /* If we're not in a here document, but we think we're about to parse one, |
| 5123 | and we would otherwise return a `;', return a newline to delimit the |
| 5124 | line with the here-doc delimiter */ |
| 5125 | else if ((parser_state & PST_HEREDOC) == 0 && current_command_line_count > 1 && last_read_token == '\n' && strstr (line, "<<")) |
| 5126 | { |
| 5127 | last_was_heredoc = 1; |
| 5128 | return "\n"; |
| 5129 | } |
| 5130 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5131 | else if (token_before_that == WORD && two_tokens_ago == FOR) |
| 5132 | { |
| 5133 | /* Tricky. `for i\nin ...' should not have a semicolon, but |
| 5134 | `for i\ndo ...' should. We do what we can. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5135 | for (i = shell_input_line_index; whitespace (shell_input_line[i]); i++) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5136 | ; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5137 | if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n') |
| 5138 | return " "; |
| 5139 | return ";"; |
| 5140 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 5141 | else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT)) |
| 5142 | return " "; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5143 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5144 | for (i = 0; no_semi_successors[i]; i++) |
| 5145 | { |
| 5146 | if (token_before_that == no_semi_successors[i]) |
| 5147 | return (" "); |
| 5148 | } |
| 5149 | |
| 5150 | return ("; "); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5151 | } |
| 5152 | #endif /* HISTORY */ |
| 5153 | |
| 5154 | /* Issue a prompt, or prepare to issue a prompt when the next character |
| 5155 | is read. */ |
| 5156 | static void |
| 5157 | prompt_again () |
| 5158 | { |
| 5159 | char *temp_prompt; |
| 5160 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 5161 | if (interactive == 0 || expanding_alias ()) /* XXX */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5162 | return; |
| 5163 | |
| 5164 | ps1_prompt = get_string_value ("PS1"); |
| 5165 | ps2_prompt = get_string_value ("PS2"); |
| 5166 | |
| 5167 | if (!prompt_string_pointer) |
| 5168 | prompt_string_pointer = &ps1_prompt; |
| 5169 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5170 | temp_prompt = *prompt_string_pointer |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5171 | ? decode_prompt_string (*prompt_string_pointer) |
| 5172 | : (char *)NULL; |
| 5173 | |
| 5174 | if (temp_prompt == 0) |
| 5175 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5176 | temp_prompt = (char *)xmalloc (1); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5177 | temp_prompt[0] = '\0'; |
| 5178 | } |
| 5179 | |
| 5180 | current_prompt_string = *prompt_string_pointer; |
| 5181 | prompt_string_pointer = &ps2_prompt; |
| 5182 | |
| 5183 | #if defined (READLINE) |
| 5184 | if (!no_line_editing) |
| 5185 | { |
| 5186 | FREE (current_readline_prompt); |
| 5187 | current_readline_prompt = temp_prompt; |
| 5188 | } |
| 5189 | else |
| 5190 | #endif /* READLINE */ |
| 5191 | { |
| 5192 | FREE (current_decoded_prompt); |
| 5193 | current_decoded_prompt = temp_prompt; |
| 5194 | } |
| 5195 | } |
| 5196 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5197 | int |
| 5198 | get_current_prompt_level () |
| 5199 | { |
| 5200 | return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1); |
| 5201 | } |
| 5202 | |
| 5203 | void |
| 5204 | set_current_prompt_level (x) |
| 5205 | int x; |
| 5206 | { |
| 5207 | prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt; |
| 5208 | current_prompt_string = *prompt_string_pointer; |
| 5209 | } |
| 5210 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5211 | static void |
| 5212 | print_prompt () |
| 5213 | { |
| 5214 | fprintf (stderr, "%s", current_decoded_prompt); |
| 5215 | fflush (stderr); |
| 5216 | } |
| 5217 | |
| 5218 | /* Return a string which will be printed as a prompt. The string |
| 5219 | may contain special characters which are decoded as follows: |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5220 | |
| 5221 | \a bell (ascii 07) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5222 | \d the date in Day Mon Date format |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5223 | \e escape (ascii 033) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5224 | \h the hostname up to the first `.' |
| 5225 | \H the hostname |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5226 | \j the number of active jobs |
| 5227 | \l the basename of the shell's tty device name |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5228 | \n CRLF |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5229 | \r CR |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5230 | \s the name of the shell |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5231 | \t the time in 24-hour hh:mm:ss format |
| 5232 | \T the time in 12-hour hh:mm:ss format |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5233 | \@ the time in 12-hour hh:mm am/pm format |
| 5234 | \A the time in 24-hour hh:mm format |
| 5235 | \D{fmt} the result of passing FMT to strftime(3) |
| 5236 | \u your username |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5237 | \v the version of bash (e.g., 2.00) |
| 5238 | \V the release of bash, version + patchlevel (e.g., 2.00.0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5239 | \w the current working directory |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5240 | \W the last element of $PWD |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5241 | \! the history number of this command |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5242 | \# the command number of this command |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5243 | \$ a $ or a # if you are root |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5244 | \nnn character code nnn in octal |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5245 | \\ a backslash |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5246 | \[ begin a sequence of non-printing chars |
| 5247 | \] end a sequence of non-printing chars |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5248 | */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 5249 | #define PROMPT_GROWTH 48 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5250 | char * |
| 5251 | decode_prompt_string (string) |
| 5252 | char *string; |
| 5253 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5254 | WORD_LIST *list; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5255 | char *result, *t; |
| 5256 | struct dstack save_dstack; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 5257 | int last_exit_value, last_comsub_pid; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5258 | #if defined (PROMPT_STRING_DECODE) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5259 | int result_size, result_index; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 5260 | int c, n, i; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5261 | char *temp, octal_string[4]; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5262 | struct tm *tm; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5263 | time_t the_time; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5264 | char timebuf[128]; |
| 5265 | char *timefmt; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5266 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5267 | result = (char *)xmalloc (result_size = PROMPT_GROWTH); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5268 | result[result_index = 0] = 0; |
| 5269 | temp = (char *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5270 | |
| 5271 | while (c = *string++) |
| 5272 | { |
| 5273 | if (posixly_correct && c == '!') |
| 5274 | { |
| 5275 | if (*string == '!') |
| 5276 | { |
| 5277 | temp = savestring ("!"); |
| 5278 | goto add_string; |
| 5279 | } |
| 5280 | else |
| 5281 | { |
| 5282 | #if !defined (HISTORY) |
| 5283 | temp = savestring ("1"); |
| 5284 | #else /* HISTORY */ |
| 5285 | temp = itos (history_number ()); |
| 5286 | #endif /* HISTORY */ |
| 5287 | string--; /* add_string increments string again. */ |
| 5288 | goto add_string; |
| 5289 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5290 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5291 | if (c == '\\') |
| 5292 | { |
| 5293 | c = *string; |
| 5294 | |
| 5295 | switch (c) |
| 5296 | { |
| 5297 | case '0': |
| 5298 | case '1': |
| 5299 | case '2': |
| 5300 | case '3': |
| 5301 | case '4': |
| 5302 | case '5': |
| 5303 | case '6': |
| 5304 | case '7': |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5305 | strncpy (octal_string, string, 3); |
| 5306 | octal_string[3] = '\0'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5307 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5308 | n = read_octal (octal_string); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5309 | temp = (char *)xmalloc (3); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5310 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5311 | if (n == CTLESC || n == CTLNUL) |
| 5312 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5313 | temp[0] = CTLESC; |
| 5314 | temp[1] = n; |
| 5315 | temp[2] = '\0'; |
| 5316 | } |
| 5317 | else if (n == -1) |
| 5318 | { |
| 5319 | temp[0] = '\\'; |
| 5320 | temp[1] = '\0'; |
| 5321 | } |
| 5322 | else |
| 5323 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5324 | temp[0] = n; |
| 5325 | temp[1] = '\0'; |
| 5326 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5327 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5328 | for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++) |
| 5329 | string++; |
| 5330 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5331 | c = 0; /* tested at add_string: */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5332 | goto add_string; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5333 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5334 | case 'd': |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5335 | case 't': |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5336 | case 'T': |
| 5337 | case '@': |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5338 | case 'A': |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5339 | /* Make the current time/date into a string. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5340 | (void) time (&the_time); |
Chet Ramey | 2bbe805 | 2011-11-22 20:00:19 -0500 | [diff] [blame] | 5341 | #if defined (HAVE_TZSET) |
| 5342 | sv_tz ("TZ"); /* XXX -- just make sure */ |
| 5343 | #endif |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5344 | tm = localtime (&the_time); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5345 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5346 | if (c == 'd') |
| 5347 | n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm); |
| 5348 | else if (c == 't') |
| 5349 | n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm); |
| 5350 | else if (c == 'T') |
| 5351 | n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm); |
| 5352 | else if (c == '@') |
| 5353 | n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm); |
| 5354 | else if (c == 'A') |
| 5355 | n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5356 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5357 | if (n == 0) |
| 5358 | timebuf[0] = '\0'; |
| 5359 | else |
| 5360 | timebuf[sizeof(timebuf) - 1] = '\0'; |
| 5361 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5362 | temp = savestring (timebuf); |
| 5363 | goto add_string; |
| 5364 | |
| 5365 | case 'D': /* strftime format */ |
| 5366 | if (string[1] != '{') /* } */ |
| 5367 | goto not_escape; |
| 5368 | |
| 5369 | (void) time (&the_time); |
| 5370 | tm = localtime (&the_time); |
| 5371 | string += 2; /* skip { */ |
| 5372 | timefmt = xmalloc (strlen (string) + 3); |
| 5373 | for (t = timefmt; *string && *string != '}'; ) |
| 5374 | *t++ = *string++; |
| 5375 | *t = '\0'; |
| 5376 | c = *string; /* tested at add_string */ |
| 5377 | if (timefmt[0] == '\0') |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5378 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5379 | timefmt[0] = '%'; |
| 5380 | timefmt[1] = 'X'; /* locale-specific current time */ |
| 5381 | timefmt[2] = '\0'; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5382 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5383 | n = strftime (timebuf, sizeof (timebuf), timefmt, tm); |
| 5384 | free (timefmt); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5385 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5386 | if (n == 0) |
| 5387 | timebuf[0] = '\0'; |
| 5388 | else |
| 5389 | timebuf[sizeof(timebuf) - 1] = '\0'; |
| 5390 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5391 | if (promptvars || posixly_correct) |
| 5392 | /* Make sure that expand_prompt_string is called with a |
| 5393 | second argument of Q_DOUBLE_QUOTES if we use this |
| 5394 | function here. */ |
| 5395 | temp = sh_backslash_quote_for_double_quotes (timebuf); |
| 5396 | else |
| 5397 | temp = savestring (timebuf); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 5398 | goto add_string; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5399 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5400 | case 'n': |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5401 | temp = (char *)xmalloc (3); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5402 | temp[0] = no_line_editing ? '\n' : '\r'; |
| 5403 | temp[1] = no_line_editing ? '\0' : '\n'; |
| 5404 | temp[2] = '\0'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5405 | goto add_string; |
| 5406 | |
| 5407 | case 's': |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5408 | temp = base_pathname (shell_name); |
| 5409 | temp = savestring (temp); |
| 5410 | goto add_string; |
| 5411 | |
| 5412 | case 'v': |
| 5413 | case 'V': |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5414 | temp = (char *)xmalloc (16); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5415 | if (c == 'v') |
| 5416 | strcpy (temp, dist_version); |
| 5417 | else |
| 5418 | sprintf (temp, "%s.%d", dist_version, patch_level); |
| 5419 | goto add_string; |
| 5420 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5421 | case 'w': |
| 5422 | case 'W': |
| 5423 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5424 | /* Use the value of PWD because it is much more efficient. */ |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 5425 | char t_string[PATH_MAX]; |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 5426 | int tlen; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5427 | |
| 5428 | temp = get_string_value ("PWD"); |
| 5429 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5430 | if (temp == 0) |
| 5431 | { |
| 5432 | if (getcwd (t_string, sizeof(t_string)) == 0) |
| 5433 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5434 | t_string[0] = '.'; |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 5435 | tlen = 1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5436 | } |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 5437 | else |
| 5438 | tlen = strlen (t_string); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5439 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5440 | else |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 5441 | { |
| 5442 | tlen = sizeof (t_string) - 1; |
| 5443 | strncpy (t_string, temp, tlen); |
| 5444 | } |
| 5445 | t_string[tlen] = '\0'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5446 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 5447 | #if defined (MACOSX) |
| 5448 | /* Convert from "fs" format to "input" format */ |
| 5449 | temp = fnx_fromfs (t_string, strlen (t_string)); |
| 5450 | if (temp != t_string) |
| 5451 | strcpy (t_string, temp); |
| 5452 | #endif |
| 5453 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5454 | #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0) |
| 5455 | #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5456 | /* Abbreviate \W as ~ if $PWD == $HOME */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 5457 | if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5458 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5459 | if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0) |
| 5460 | { |
| 5461 | t = strrchr (t_string, '/'); |
| 5462 | if (t) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 5463 | memmove (t_string, t + 1, strlen (t)); /* strlen(t) to copy NULL */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5464 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5465 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5466 | #undef ROOT_PATH |
| 5467 | #undef DOUBLE_SLASH_ROOT |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5468 | else |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 5469 | { |
| 5470 | /* polite_directory_format is guaranteed to return a string |
| 5471 | no longer than PATH_MAX - 1 characters. */ |
| 5472 | temp = polite_directory_format (t_string); |
| 5473 | if (temp != t_string) |
| 5474 | strcpy (t_string, temp); |
| 5475 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5476 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5477 | temp = trim_pathname (t_string, PATH_MAX - 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5478 | /* If we're going to be expanding the prompt string later, |
| 5479 | quote the directory name. */ |
| 5480 | if (promptvars || posixly_correct) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5481 | /* Make sure that expand_prompt_string is called with a |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5482 | second argument of Q_DOUBLE_QUOTES if we use this |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5483 | function here. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5484 | temp = sh_backslash_quote_for_double_quotes (t_string); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5485 | else |
| 5486 | temp = savestring (t_string); |
| 5487 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5488 | goto add_string; |
| 5489 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5490 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5491 | case 'u': |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5492 | if (current_user.user_name == 0) |
| 5493 | get_current_user_info (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5494 | temp = savestring (current_user.user_name); |
| 5495 | goto add_string; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5496 | |
| 5497 | case 'h': |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5498 | case 'H': |
| 5499 | temp = savestring (current_host_name); |
| 5500 | if (c == 'h' && (t = (char *)strchr (temp, '.'))) |
| 5501 | *t = '\0'; |
| 5502 | goto add_string; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5503 | |
| 5504 | case '#': |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5505 | temp = itos (current_command_number); |
| 5506 | goto add_string; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5507 | |
| 5508 | case '!': |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5509 | #if !defined (HISTORY) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5510 | temp = savestring ("1"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5511 | #else /* HISTORY */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5512 | temp = itos (history_number ()); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5513 | #endif /* HISTORY */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5514 | goto add_string; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5515 | |
| 5516 | case '$': |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5517 | t = temp = (char *)xmalloc (3); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 5518 | if ((promptvars || posixly_correct) && (current_user.euid != 0)) |
| 5519 | *t++ = '\\'; |
| 5520 | *t++ = current_user.euid == 0 ? '#' : '$'; |
| 5521 | *t = '\0'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5522 | goto add_string; |
| 5523 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5524 | case 'j': |
| 5525 | temp = itos (count_all_jobs ()); |
| 5526 | goto add_string; |
| 5527 | |
| 5528 | case 'l': |
| 5529 | #if defined (HAVE_TTYNAME) |
| 5530 | temp = (char *)ttyname (fileno (stdin)); |
| 5531 | t = temp ? base_pathname (temp) : "tty"; |
| 5532 | temp = savestring (t); |
| 5533 | #else |
| 5534 | temp = savestring ("tty"); |
| 5535 | #endif /* !HAVE_TTYNAME */ |
| 5536 | goto add_string; |
| 5537 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5538 | #if defined (READLINE) |
| 5539 | case '[': |
| 5540 | case ']': |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5541 | if (no_line_editing) |
| 5542 | { |
| 5543 | string++; |
| 5544 | break; |
| 5545 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5546 | temp = (char *)xmalloc (3); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 5547 | n = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE; |
| 5548 | i = 0; |
| 5549 | if (n == CTLESC || n == CTLNUL) |
| 5550 | temp[i++] = CTLESC; |
| 5551 | temp[i++] = n; |
| 5552 | temp[i] = '\0'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5553 | goto add_string; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5554 | #endif /* READLINE */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5555 | |
| 5556 | case '\\': |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5557 | case 'a': |
| 5558 | case 'e': |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5559 | case 'r': |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5560 | temp = (char *)xmalloc (2); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5561 | if (c == 'a') |
| 5562 | temp[0] = '\07'; |
| 5563 | else if (c == 'e') |
| 5564 | temp[0] = '\033'; |
| 5565 | else if (c == 'r') |
| 5566 | temp[0] = '\r'; |
| 5567 | else /* (c == '\\') */ |
| 5568 | temp[0] = c; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5569 | temp[1] = '\0'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5570 | goto add_string; |
| 5571 | |
| 5572 | default: |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5573 | not_escape: |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5574 | temp = (char *)xmalloc (3); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5575 | temp[0] = '\\'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5576 | temp[1] = c; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5577 | temp[2] = '\0'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5578 | |
| 5579 | add_string: |
| 5580 | if (c) |
| 5581 | string++; |
| 5582 | result = |
| 5583 | sub_append_string (temp, result, &result_index, &result_size); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5584 | temp = (char *)NULL; /* Freed in sub_append_string (). */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5585 | result[result_index] = '\0'; |
| 5586 | break; |
| 5587 | } |
| 5588 | } |
| 5589 | else |
| 5590 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5591 | RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5592 | result[result_index++] = c; |
| 5593 | result[result_index] = '\0'; |
| 5594 | } |
| 5595 | } |
| 5596 | #else /* !PROMPT_STRING_DECODE */ |
| 5597 | result = savestring (string); |
| 5598 | #endif /* !PROMPT_STRING_DECODE */ |
| 5599 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5600 | /* Save the delimiter stack and point `dstack' to temp space so any |
| 5601 | command substitutions in the prompt string won't result in screwing |
| 5602 | up the parser's quoting state. */ |
| 5603 | save_dstack = dstack; |
| 5604 | dstack = temp_dstack; |
| 5605 | dstack.delimiter_depth = 0; |
| 5606 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5607 | /* Perform variable and parameter expansion and command substitution on |
| 5608 | the prompt string. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5609 | if (promptvars || posixly_correct) |
| 5610 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5611 | last_exit_value = last_command_exit_value; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 5612 | last_comsub_pid = last_command_subst_pid; |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 5613 | list = expand_prompt_string (result, Q_DOUBLE_QUOTES, 0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5614 | free (result); |
| 5615 | result = string_list (list); |
| 5616 | dispose_words (list); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5617 | last_command_exit_value = last_exit_value; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 5618 | last_command_subst_pid = last_comsub_pid; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5619 | } |
| 5620 | else |
| 5621 | { |
| 5622 | t = dequote_string (result); |
| 5623 | free (result); |
| 5624 | result = t; |
| 5625 | } |
| 5626 | |
| 5627 | dstack = save_dstack; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5628 | |
| 5629 | return (result); |
| 5630 | } |
| 5631 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5632 | /************************************************ |
| 5633 | * * |
| 5634 | * ERROR HANDLING * |
| 5635 | * * |
| 5636 | ************************************************/ |
| 5637 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5638 | /* Report a syntax error, and restart the parser. Call here for fatal |
| 5639 | errors. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5640 | int |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5641 | yyerror (msg) |
| 5642 | const char *msg; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5643 | { |
| 5644 | report_syntax_error ((char *)NULL); |
| 5645 | reset_parser (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5646 | return (0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5649 | static char * |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 5650 | error_token_from_token (tok) |
| 5651 | int tok; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5652 | { |
| 5653 | char *t; |
| 5654 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 5655 | if (t = find_token_in_alist (tok, word_token_alist, 0)) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5656 | return t; |
| 5657 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 5658 | if (t = find_token_in_alist (tok, other_token_alist, 0)) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5659 | return t; |
| 5660 | |
| 5661 | t = (char *)NULL; |
| 5662 | /* This stuff is dicy and needs closer inspection */ |
| 5663 | switch (current_token) |
| 5664 | { |
| 5665 | case WORD: |
| 5666 | case ASSIGNMENT_WORD: |
| 5667 | if (yylval.word) |
| 5668 | t = savestring (yylval.word->word); |
| 5669 | break; |
| 5670 | case NUMBER: |
| 5671 | t = itos (yylval.number); |
| 5672 | break; |
| 5673 | case ARITH_CMD: |
| 5674 | if (yylval.word_list) |
| 5675 | t = string_list (yylval.word_list); |
| 5676 | break; |
| 5677 | case ARITH_FOR_EXPRS: |
| 5678 | if (yylval.word_list) |
| 5679 | t = string_list_internal (yylval.word_list, " ; "); |
| 5680 | break; |
| 5681 | case COND_CMD: |
| 5682 | t = (char *)NULL; /* punt */ |
| 5683 | break; |
| 5684 | } |
| 5685 | |
| 5686 | return t; |
| 5687 | } |
| 5688 | |
| 5689 | static char * |
| 5690 | error_token_from_text () |
| 5691 | { |
| 5692 | char *msg, *t; |
| 5693 | int token_end, i; |
| 5694 | |
| 5695 | t = shell_input_line; |
| 5696 | i = shell_input_line_index; |
| 5697 | token_end = 0; |
| 5698 | msg = (char *)NULL; |
| 5699 | |
| 5700 | if (i && t[i] == '\0') |
| 5701 | i--; |
| 5702 | |
| 5703 | while (i && (whitespace (t[i]) || t[i] == '\n')) |
| 5704 | i--; |
| 5705 | |
| 5706 | if (i) |
| 5707 | token_end = i + 1; |
| 5708 | |
| 5709 | while (i && (member (t[i], " \n\t;|&") == 0)) |
| 5710 | i--; |
| 5711 | |
| 5712 | while (i != token_end && (whitespace (t[i]) || t[i] == '\n')) |
| 5713 | i++; |
| 5714 | |
| 5715 | /* Return our idea of the offending token. */ |
| 5716 | if (token_end || (i == 0 && token_end == 0)) |
| 5717 | { |
| 5718 | if (token_end) |
| 5719 | msg = substring (t, i, token_end); |
| 5720 | else /* one-character token */ |
| 5721 | { |
| 5722 | msg = (char *)xmalloc (2); |
| 5723 | msg[0] = t[i]; |
| 5724 | msg[1] = '\0'; |
| 5725 | } |
| 5726 | } |
| 5727 | |
| 5728 | return (msg); |
| 5729 | } |
| 5730 | |
| 5731 | static void |
| 5732 | print_offending_line () |
| 5733 | { |
| 5734 | char *msg; |
| 5735 | int token_end; |
| 5736 | |
| 5737 | msg = savestring (shell_input_line); |
| 5738 | token_end = strlen (msg); |
| 5739 | while (token_end && msg[token_end - 1] == '\n') |
| 5740 | msg[--token_end] = '\0'; |
| 5741 | |
| 5742 | parser_error (line_number, "`%s'", msg); |
| 5743 | free (msg); |
| 5744 | } |
| 5745 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5746 | /* Report a syntax error with line numbers, etc. |
| 5747 | Call here for recoverable errors. If you have a message to print, |
| 5748 | then place it in MESSAGE, otherwise pass NULL and this will figure |
| 5749 | out an appropriate message for you. */ |
| 5750 | static void |
| 5751 | report_syntax_error (message) |
| 5752 | char *message; |
| 5753 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 5754 | char *msg, *p; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5755 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5756 | if (message) |
| 5757 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5758 | parser_error (line_number, "%s", message); |
| 5759 | if (interactive && EOF_Reached) |
| 5760 | EOF_Reached = 0; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 5761 | last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5762 | return; |
| 5763 | } |
| 5764 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5765 | /* 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] | 5766 | objectionable token. First, try to figure out what token the |
| 5767 | parser's complaining about by looking at current_token. */ |
| 5768 | if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token))) |
| 5769 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 5770 | if (ansic_shouldquote (msg)) |
| 5771 | { |
| 5772 | p = ansic_quote (msg, 0, NULL); |
| 5773 | free (msg); |
| 5774 | msg = p; |
| 5775 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5776 | parser_error (line_number, _("syntax error near unexpected token `%s'"), msg); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5777 | free (msg); |
| 5778 | |
| 5779 | if (interactive == 0) |
| 5780 | print_offending_line (); |
| 5781 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 5782 | last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5783 | return; |
| 5784 | } |
| 5785 | |
| 5786 | /* If looking at the current token doesn't prove fruitful, try to find the |
| 5787 | offending token by analyzing the text of the input line near the current |
| 5788 | input line index and report what we find. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5789 | if (shell_input_line && *shell_input_line) |
| 5790 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5791 | msg = error_token_from_text (); |
| 5792 | if (msg) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5793 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5794 | parser_error (line_number, _("syntax error near `%s'"), msg); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5795 | free (msg); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5798 | /* If not interactive, print the line containing the error. */ |
| 5799 | if (interactive == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5800 | print_offending_line (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5801 | } |
| 5802 | else |
| 5803 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5804 | msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error"); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5805 | parser_error (line_number, "%s", msg); |
| 5806 | /* When the shell is interactive, this file uses EOF_Reached |
| 5807 | only for error reporting. Other mechanisms are used to |
| 5808 | decide whether or not to exit. */ |
| 5809 | if (interactive && EOF_Reached) |
| 5810 | EOF_Reached = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5811 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5812 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 5813 | last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5814 | } |
| 5815 | |
| 5816 | /* ??? Needed function. ??? We have to be able to discard the constructs |
| 5817 | created during parsing. In the case of error, we want to return |
| 5818 | allocated objects to the memory pool. In the case of no error, we want |
| 5819 | to throw away the information about where the allocated objects live. |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5820 | (dispose_command () will actually free the command.) */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5821 | static void |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5822 | discard_parser_constructs (error_p) |
| 5823 | int error_p; |
| 5824 | { |
| 5825 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5826 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5827 | /************************************************ |
| 5828 | * * |
| 5829 | * EOF HANDLING * |
| 5830 | * * |
| 5831 | ************************************************/ |
| 5832 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5833 | /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */ |
| 5834 | |
| 5835 | /* A flag denoting whether or not ignoreeof is set. */ |
| 5836 | int ignoreeof = 0; |
| 5837 | |
| 5838 | /* The number of times that we have encountered an EOF character without |
| 5839 | another character intervening. When this gets above the limit, the |
| 5840 | shell terminates. */ |
| 5841 | int eof_encountered = 0; |
| 5842 | |
| 5843 | /* The limit for eof_encountered. */ |
| 5844 | int eof_encountered_limit = 10; |
| 5845 | |
| 5846 | /* If we have EOF as the only input unit, this user wants to leave |
| 5847 | the shell. If the shell is not interactive, then just leave. |
| 5848 | Otherwise, if ignoreeof is set, and we haven't done this the |
| 5849 | required number of times in a row, print a message. */ |
| 5850 | static void |
| 5851 | handle_eof_input_unit () |
| 5852 | { |
| 5853 | if (interactive) |
| 5854 | { |
| 5855 | /* shell.c may use this to decide whether or not to write out the |
| 5856 | history, among other things. We use it only for error reporting |
| 5857 | in this file. */ |
| 5858 | if (EOF_Reached) |
| 5859 | EOF_Reached = 0; |
| 5860 | |
| 5861 | /* If the user wants to "ignore" eof, then let her do so, kind of. */ |
| 5862 | if (ignoreeof) |
| 5863 | { |
| 5864 | if (eof_encountered < eof_encountered_limit) |
| 5865 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5866 | fprintf (stderr, _("Use \"%s\" to leave the shell.\n"), |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5867 | login_shell ? "logout" : "exit"); |
| 5868 | eof_encountered++; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5869 | /* Reset the parsing state. */ |
| 5870 | last_read_token = current_token = '\n'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5871 | /* Reset the prompt string to be $PS1. */ |
| 5872 | prompt_string_pointer = (char **)NULL; |
| 5873 | prompt_again (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5874 | return; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 5875 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5876 | } |
| 5877 | |
| 5878 | /* In this case EOF should exit the shell. Do it now. */ |
| 5879 | reset_parser (); |
| 5880 | exit_builtin ((WORD_LIST *)NULL); |
| 5881 | } |
| 5882 | else |
| 5883 | { |
| 5884 | /* We don't write history files, etc., for non-interactive shells. */ |
| 5885 | EOF_Reached = 1; |
| 5886 | } |
| 5887 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5888 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5889 | /************************************************ |
| 5890 | * * |
| 5891 | * STRING PARSING FUNCTIONS * |
| 5892 | * * |
| 5893 | ************************************************/ |
| 5894 | |
| 5895 | /* It's very important that these two functions treat the characters |
| 5896 | between ( and ) identically. */ |
| 5897 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5898 | static WORD_LIST parse_string_error; |
| 5899 | |
| 5900 | /* Take a string and run it through the shell parser, returning the |
| 5901 | resultant word list. Used by compound array assignment. */ |
| 5902 | WORD_LIST * |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5903 | parse_string_to_word_list (s, flags, whom) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5904 | char *s; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5905 | int flags; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 5906 | const char *whom; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5907 | { |
| 5908 | WORD_LIST *wl; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5909 | int tok, orig_current_token, orig_line_number, orig_input_terminator; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5910 | int orig_line_count; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5911 | int old_echo_input, old_expand_aliases; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5912 | #if defined (HISTORY) |
| 5913 | int old_remember_on_history, old_history_expansion_inhibited; |
| 5914 | #endif |
| 5915 | |
| 5916 | #if defined (HISTORY) |
| 5917 | old_remember_on_history = remember_on_history; |
| 5918 | # if defined (BANG_HISTORY) |
| 5919 | old_history_expansion_inhibited = history_expansion_inhibited; |
| 5920 | # endif |
| 5921 | bash_history_disable (); |
| 5922 | #endif |
| 5923 | |
| 5924 | orig_line_number = line_number; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5925 | orig_line_count = current_command_line_count; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5926 | orig_input_terminator = shell_input_line_terminator; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5927 | old_echo_input = echo_input_at_read; |
| 5928 | old_expand_aliases = expand_aliases; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5929 | |
| 5930 | push_stream (1); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5931 | last_read_token = WORD; /* WORD to allow reserved words here */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5932 | current_command_line_count = 0; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5933 | echo_input_at_read = expand_aliases = 0; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5934 | |
| 5935 | with_input_from_string (s, whom); |
| 5936 | wl = (WORD_LIST *)NULL; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5937 | |
| 5938 | if (flags & 1) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5939 | parser_state |= PST_COMPASSIGN|PST_REPARSE; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5940 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5941 | while ((tok = read_token (READ)) != yacc_EOF) |
| 5942 | { |
| 5943 | if (tok == '\n' && *bash_input.location.string == '\0') |
| 5944 | break; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5945 | if (tok == '\n') /* Allow newlines in compound assignments */ |
| 5946 | continue; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5947 | if (tok != WORD && tok != ASSIGNMENT_WORD) |
| 5948 | { |
| 5949 | line_number = orig_line_number + line_number - 1; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5950 | orig_current_token = current_token; |
| 5951 | current_token = tok; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 5952 | yyerror (NULL); /* does the right thing */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5953 | current_token = orig_current_token; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5954 | if (wl) |
| 5955 | dispose_words (wl); |
| 5956 | wl = &parse_string_error; |
| 5957 | break; |
| 5958 | } |
| 5959 | wl = make_word_list (yylval.word, wl); |
| 5960 | } |
| 5961 | |
| 5962 | last_read_token = '\n'; |
| 5963 | pop_stream (); |
| 5964 | |
| 5965 | #if defined (HISTORY) |
| 5966 | remember_on_history = old_remember_on_history; |
| 5967 | # if defined (BANG_HISTORY) |
| 5968 | history_expansion_inhibited = old_history_expansion_inhibited; |
| 5969 | # endif /* BANG_HISTORY */ |
| 5970 | #endif /* HISTORY */ |
| 5971 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5972 | echo_input_at_read = old_echo_input; |
| 5973 | expand_aliases = old_expand_aliases; |
| 5974 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 5975 | current_command_line_count = orig_line_count; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5976 | shell_input_line_terminator = orig_input_terminator; |
| 5977 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5978 | if (flags & 1) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5979 | parser_state &= ~(PST_COMPASSIGN|PST_REPARSE); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 5980 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5981 | if (wl == &parse_string_error) |
| 5982 | { |
| 5983 | last_command_exit_value = EXECUTION_FAILURE; |
| 5984 | if (interactive_shell == 0 && posixly_correct) |
| 5985 | jump_to_top_level (FORCE_EOF); |
| 5986 | else |
| 5987 | jump_to_top_level (DISCARD); |
| 5988 | } |
| 5989 | |
| 5990 | return (REVERSE_LIST (wl, WORD_LIST *)); |
| 5991 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5992 | |
| 5993 | static char * |
| 5994 | parse_compound_assignment (retlenp) |
| 5995 | int *retlenp; |
| 5996 | { |
| 5997 | WORD_LIST *wl, *rl; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 5998 | int tok, orig_line_number, orig_token_size, orig_last_token, assignok; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 5999 | char *saved_token, *ret; |
| 6000 | |
| 6001 | saved_token = token; |
| 6002 | orig_token_size = token_buffer_size; |
| 6003 | orig_line_number = line_number; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 6004 | orig_last_token = last_read_token; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6005 | |
| 6006 | last_read_token = WORD; /* WORD to allow reserved words here */ |
| 6007 | |
| 6008 | token = (char *)NULL; |
| 6009 | token_buffer_size = 0; |
| 6010 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 6011 | assignok = parser_state&PST_ASSIGNOK; /* XXX */ |
| 6012 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6013 | wl = (WORD_LIST *)NULL; /* ( */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6014 | parser_state |= PST_COMPASSIGN; |
| 6015 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6016 | while ((tok = read_token (READ)) != ')') |
| 6017 | { |
| 6018 | if (tok == '\n') /* Allow newlines in compound assignments */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6019 | { |
| 6020 | if (SHOULD_PROMPT ()) |
| 6021 | prompt_again (); |
| 6022 | continue; |
| 6023 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6024 | if (tok != WORD && tok != ASSIGNMENT_WORD) |
| 6025 | { |
| 6026 | current_token = tok; /* for error reporting */ |
| 6027 | if (tok == yacc_EOF) /* ( */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6028 | parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'")); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6029 | else |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 6030 | yyerror(NULL); /* does the right thing */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6031 | if (wl) |
| 6032 | dispose_words (wl); |
| 6033 | wl = &parse_string_error; |
| 6034 | break; |
| 6035 | } |
| 6036 | wl = make_word_list (yylval.word, wl); |
| 6037 | } |
| 6038 | |
| 6039 | FREE (token); |
| 6040 | token = saved_token; |
| 6041 | token_buffer_size = orig_token_size; |
| 6042 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6043 | parser_state &= ~PST_COMPASSIGN; |
| 6044 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6045 | if (wl == &parse_string_error) |
| 6046 | { |
| 6047 | last_command_exit_value = EXECUTION_FAILURE; |
| 6048 | last_read_token = '\n'; /* XXX */ |
| 6049 | if (interactive_shell == 0 && posixly_correct) |
| 6050 | jump_to_top_level (FORCE_EOF); |
| 6051 | else |
| 6052 | jump_to_top_level (DISCARD); |
| 6053 | } |
| 6054 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 6055 | last_read_token = orig_last_token; /* XXX - was WORD? */ |
| 6056 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6057 | if (wl) |
| 6058 | { |
| 6059 | rl = REVERSE_LIST (wl, WORD_LIST *); |
| 6060 | ret = string_list (rl); |
| 6061 | dispose_words (rl); |
| 6062 | } |
| 6063 | else |
| 6064 | ret = (char *)NULL; |
| 6065 | |
| 6066 | if (retlenp) |
| 6067 | *retlenp = (ret && *ret) ? strlen (ret) : 0; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 6068 | |
| 6069 | if (assignok) |
| 6070 | parser_state |= PST_ASSIGNOK; |
| 6071 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6072 | return ret; |
| 6073 | } |
| 6074 | |
| 6075 | /************************************************ |
| 6076 | * * |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6077 | * SAVING AND RESTORING PARTIAL PARSE STATE * |
| 6078 | * * |
| 6079 | ************************************************/ |
| 6080 | |
| 6081 | sh_parser_state_t * |
| 6082 | save_parser_state (ps) |
| 6083 | sh_parser_state_t *ps; |
| 6084 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6085 | if (ps == 0) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 6086 | ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t)); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6087 | if (ps == 0) |
| 6088 | return ((sh_parser_state_t *)NULL); |
| 6089 | |
| 6090 | ps->parser_state = parser_state; |
| 6091 | ps->token_state = save_token_state (); |
| 6092 | |
| 6093 | ps->input_line_terminator = shell_input_line_terminator; |
| 6094 | ps->eof_encountered = eof_encountered; |
| 6095 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 6096 | ps->prompt_string_pointer = prompt_string_pointer; |
| 6097 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6098 | ps->current_command_line_count = current_command_line_count; |
| 6099 | |
| 6100 | #if defined (HISTORY) |
| 6101 | ps->remember_on_history = remember_on_history; |
| 6102 | # if defined (BANG_HISTORY) |
| 6103 | ps->history_expansion_inhibited = history_expansion_inhibited; |
| 6104 | # endif |
| 6105 | #endif |
| 6106 | |
| 6107 | ps->last_command_exit_value = last_command_exit_value; |
| 6108 | #if defined (ARRAY_VARS) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 6109 | ps->pipestatus = save_pipestatus_array (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6110 | #endif |
| 6111 | |
| 6112 | ps->last_shell_builtin = last_shell_builtin; |
| 6113 | ps->this_shell_builtin = this_shell_builtin; |
| 6114 | |
| 6115 | ps->expand_aliases = expand_aliases; |
| 6116 | ps->echo_input_at_read = echo_input_at_read; |
Chet Ramey | daefb2c | 2014-08-01 15:30:48 -0400 | [diff] [blame] | 6117 | ps->need_here_doc = need_here_doc; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6118 | |
Chet Ramey | b4839c2 | 2011-11-22 20:02:35 -0500 | [diff] [blame] | 6119 | ps->token = token; |
| 6120 | ps->token_buffer_size = token_buffer_size; |
| 6121 | /* Force reallocation on next call to read_token_word */ |
| 6122 | token = 0; |
| 6123 | token_buffer_size = 0; |
| 6124 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6125 | return (ps); |
| 6126 | } |
| 6127 | |
| 6128 | void |
| 6129 | restore_parser_state (ps) |
| 6130 | sh_parser_state_t *ps; |
| 6131 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6132 | if (ps == 0) |
| 6133 | return; |
| 6134 | |
| 6135 | parser_state = ps->parser_state; |
| 6136 | if (ps->token_state) |
| 6137 | { |
| 6138 | restore_token_state (ps->token_state); |
| 6139 | free (ps->token_state); |
| 6140 | } |
| 6141 | |
| 6142 | shell_input_line_terminator = ps->input_line_terminator; |
| 6143 | eof_encountered = ps->eof_encountered; |
| 6144 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 6145 | prompt_string_pointer = ps->prompt_string_pointer; |
| 6146 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6147 | current_command_line_count = ps->current_command_line_count; |
| 6148 | |
| 6149 | #if defined (HISTORY) |
| 6150 | remember_on_history = ps->remember_on_history; |
| 6151 | # if defined (BANG_HISTORY) |
| 6152 | history_expansion_inhibited = ps->history_expansion_inhibited; |
| 6153 | # endif |
| 6154 | #endif |
| 6155 | |
| 6156 | last_command_exit_value = ps->last_command_exit_value; |
| 6157 | #if defined (ARRAY_VARS) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 6158 | restore_pipestatus_array (ps->pipestatus); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6159 | #endif |
| 6160 | |
| 6161 | last_shell_builtin = ps->last_shell_builtin; |
| 6162 | this_shell_builtin = ps->this_shell_builtin; |
| 6163 | |
| 6164 | expand_aliases = ps->expand_aliases; |
| 6165 | echo_input_at_read = ps->echo_input_at_read; |
Chet Ramey | daefb2c | 2014-08-01 15:30:48 -0400 | [diff] [blame] | 6166 | need_here_doc = ps->need_here_doc; |
Chet Ramey | b4839c2 | 2011-11-22 20:02:35 -0500 | [diff] [blame] | 6167 | |
| 6168 | FREE (token); |
| 6169 | token = ps->token; |
| 6170 | token_buffer_size = ps->token_buffer_size; |
| 6171 | } |
| 6172 | |
| 6173 | sh_input_line_state_t * |
| 6174 | save_input_line_state (ls) |
| 6175 | sh_input_line_state_t *ls; |
| 6176 | { |
| 6177 | if (ls == 0) |
| 6178 | ls = (sh_input_line_state_t *)xmalloc (sizeof (sh_input_line_state_t)); |
| 6179 | if (ls == 0) |
| 6180 | return ((sh_input_line_state_t *)NULL); |
| 6181 | |
| 6182 | ls->input_line = shell_input_line; |
| 6183 | ls->input_line_size = shell_input_line_size; |
| 6184 | ls->input_line_len = shell_input_line_len; |
| 6185 | ls->input_line_index = shell_input_line_index; |
| 6186 | |
| 6187 | /* force reallocation */ |
| 6188 | shell_input_line = 0; |
| 6189 | shell_input_line_size = shell_input_line_len = shell_input_line_index = 0; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 6190 | |
| 6191 | return ls; |
Chet Ramey | b4839c2 | 2011-11-22 20:02:35 -0500 | [diff] [blame] | 6192 | } |
| 6193 | |
| 6194 | void |
| 6195 | restore_input_line_state (ls) |
| 6196 | sh_input_line_state_t *ls; |
| 6197 | { |
| 6198 | FREE (shell_input_line); |
| 6199 | shell_input_line = ls->input_line; |
| 6200 | shell_input_line_size = ls->input_line_size; |
| 6201 | shell_input_line_len = ls->input_line_len; |
| 6202 | shell_input_line_index = ls->input_line_index; |
| 6203 | |
| 6204 | set_line_mbstate (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | /************************************************ |
| 6208 | * * |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6209 | * MULTIBYTE CHARACTER HANDLING * |
| 6210 | * * |
| 6211 | ************************************************/ |
| 6212 | |
| 6213 | #if defined (HANDLE_MULTIBYTE) |
| 6214 | static void |
| 6215 | set_line_mbstate () |
| 6216 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 6217 | int c; |
| 6218 | size_t i, previ, len; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6219 | mbstate_t mbs, prevs; |
| 6220 | size_t mbclen; |
| 6221 | |
| 6222 | if (shell_input_line == NULL) |
| 6223 | return; |
| 6224 | len = strlen (shell_input_line); /* XXX - shell_input_line_len ? */ |
| 6225 | FREE (shell_input_line_property); |
| 6226 | shell_input_line_property = (char *)xmalloc (len + 1); |
| 6227 | |
| 6228 | memset (&prevs, '\0', sizeof (mbstate_t)); |
| 6229 | for (i = previ = 0; i < len; i++) |
| 6230 | { |
| 6231 | mbs = prevs; |
| 6232 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6233 | c = shell_input_line[i]; |
| 6234 | if (c == EOF) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6235 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 6236 | size_t j; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6237 | for (j = i; j < len; j++) |
| 6238 | shell_input_line_property[j] = 1; |
| 6239 | break; |
| 6240 | } |
| 6241 | |
| 6242 | mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs); |
| 6243 | if (mbclen == 1 || mbclen == (size_t)-1) |
| 6244 | { |
| 6245 | mbclen = 1; |
| 6246 | previ = i + 1; |
| 6247 | } |
| 6248 | else if (mbclen == (size_t)-2) |
| 6249 | mbclen = 0; |
| 6250 | else if (mbclen > 1) |
| 6251 | { |
| 6252 | mbclen = 0; |
| 6253 | previ = i + 1; |
| 6254 | prevs = mbs; |
| 6255 | } |
| 6256 | else |
| 6257 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6258 | /* XXX - what to do if mbrlen returns 0? (null wide character) */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 6259 | size_t j; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6260 | for (j = i; j < len; j++) |
| 6261 | shell_input_line_property[j] = 1; |
| 6262 | break; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 6263 | } |
| 6264 | |
| 6265 | shell_input_line_property[i] = mbclen; |
| 6266 | } |
| 6267 | } |
| 6268 | #endif /* HANDLE_MULTIBYTE */ |