]> git.ipfire.org Git - thirdparty/bash.git/blob - y.tab.c
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / y.tab.c
1 /* A Bison parser, made by GNU Bison 3.6.4. */
2
3 /* Bison implementation for Yacc-like parsers in C
4
5 Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2020 Free Software Foundation,
6 Inc.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* As a special exception, you may create a larger work that contains
22 part or all of the Bison parser skeleton and distribute that work
23 under terms of your choice, so long as that work isn't itself a
24 parser generator using the skeleton or a modified version thereof
25 as a parser skeleton. Alternatively, if you modify or redistribute
26 the parser skeleton itself, you may (at your option) remove this
27 special exception, which will cause the skeleton and the resulting
28 Bison output files to be licensed under the GNU General Public
29 License without this special exception.
30
31 This special exception was added by the Free Software Foundation in
32 version 2.2 of Bison. */
33
34 /* C LALR(1) parser skeleton written by Richard Stallman, by
35 simplifying the original so-called "semantic" parser. */
36
37 /* DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual,
38 especially those whose name start with YY_ or yy_. They are
39 private implementation details that can be changed or removed. */
40
41 /* All symbols defined below should begin with yy or YY, to avoid
42 infringing on user name space. This should be done even for local
43 variables, as they might otherwise be expanded by user macros.
44 There are some unavoidable exceptions within include files to
45 define necessary library symbols; they are noted "INFRINGES ON
46 USER NAME SPACE" below. */
47
48 /* Identify Bison output. */
49 #define YYBISON 1
50
51 /* Bison version. */
52 #define YYBISON_VERSION "3.6.4"
53
54 /* Skeleton name. */
55 #define YYSKELETON_NAME "yacc.c"
56
57 /* Pure parsers. */
58 #define YYPURE 0
59
60 /* Push parsers. */
61 #define YYPUSH 0
62
63 /* Pull parsers. */
64 #define YYPULL 1
65
66
67
68
69 /* First part of user prologue. */
70 #line 21 "/Users/chet/src/bash/src/parse.y"
71
72 #include "config.h"
73
74 #include "bashtypes.h"
75 #include "bashansi.h"
76
77 #include "filecntl.h"
78
79 #if defined (HAVE_UNISTD_H)
80 # include <unistd.h>
81 #endif
82
83 #if defined (HAVE_LOCALE_H)
84 # include <locale.h>
85 #endif
86
87 #include <stdio.h>
88 #include "chartypes.h"
89 #include <signal.h>
90
91 #include "memalloc.h"
92
93 #include "bashintl.h"
94
95 #define NEED_STRFTIME_DECL /* used in externs.h */
96
97 #include "shell.h"
98 #include "execute_cmd.h"
99 #include "typemax.h" /* SIZE_MAX if needed */
100 #include "trap.h"
101 #include "flags.h"
102 #include "parser.h"
103 #include "mailcheck.h"
104 #include "test.h"
105 #include "builtins.h"
106 #include "builtins/common.h"
107 #include "builtins/builtext.h"
108
109 #include "shmbutil.h"
110
111 #if defined (READLINE)
112 # include "bashline.h"
113 # include <readline/readline.h>
114 #endif /* READLINE */
115
116 #if defined (HISTORY)
117 # include "bashhist.h"
118 # include <readline/history.h>
119 #endif /* HISTORY */
120
121 #if defined (JOB_CONTROL)
122 # include "jobs.h"
123 #else
124 extern int cleanup_dead_jobs PARAMS((void));
125 #endif /* JOB_CONTROL */
126
127 #if defined (ALIAS)
128 # include "alias.h"
129 #else
130 typedef void *alias_t;
131 #endif /* ALIAS */
132
133 #if defined (PROMPT_STRING_DECODE)
134 # ifndef _MINIX
135 # include <sys/param.h>
136 # endif
137 # include <time.h>
138 # if defined (TM_IN_SYS_TIME)
139 # include <sys/types.h>
140 # include <sys/time.h>
141 # endif /* TM_IN_SYS_TIME */
142 # include "maxpath.h"
143 #endif /* PROMPT_STRING_DECODE */
144
145 #define RE_READ_TOKEN -99
146 #define NO_EXPANSION -100
147
148 #define END_ALIAS -2
149
150 #ifdef DEBUG
151 # define YYDEBUG 1
152 #else
153 # define YYDEBUG 0
154 #endif
155
156 #if defined (HANDLE_MULTIBYTE)
157 # define last_shell_getc_is_singlebyte \
158 ((shell_input_line_index > 1) \
159 ? shell_input_line_property[shell_input_line_index - 1] \
160 : 1)
161 # define MBTEST(x) ((x) && last_shell_getc_is_singlebyte)
162 #else
163 # define last_shell_getc_is_singlebyte 1
164 # define MBTEST(x) ((x))
165 #endif
166
167 #if defined (EXTENDED_GLOB)
168 extern int extended_glob;
169 #endif
170
171 extern int dump_translatable_strings, dump_po_strings;
172
173 #if !defined (errno)
174 extern int errno;
175 #endif
176
177 /* **************************************************************** */
178 /* */
179 /* "Forward" declarations */
180 /* */
181 /* **************************************************************** */
182
183 #ifdef DEBUG
184 static void debug_parser PARAMS((int));
185 #endif
186
187 static int yy_getc PARAMS((void));
188 static int yy_ungetc PARAMS((int));
189
190 #if defined (READLINE)
191 static int yy_readline_get PARAMS((void));
192 static int yy_readline_unget PARAMS((int));
193 #endif
194
195 static int yy_string_get PARAMS((void));
196 static int yy_string_unget PARAMS((int));
197 static void rewind_input_string PARAMS((void));
198 static int yy_stream_get PARAMS((void));
199 static int yy_stream_unget PARAMS((int));
200
201 static int shell_getc PARAMS((int));
202 static void shell_ungetc PARAMS((int));
203 static void discard_until PARAMS((int));
204
205 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
206 static void push_string PARAMS((char *, int, alias_t *));
207 static void pop_string PARAMS((void));
208 static void free_string_list PARAMS((void));
209 #endif
210
211 static char *read_a_line PARAMS((int));
212
213 static int reserved_word_acceptable PARAMS((int));
214 static int yylex PARAMS((void));
215
216 static void push_heredoc PARAMS((REDIRECT *));
217 static char *mk_alexpansion PARAMS((char *));
218 static int alias_expand_token PARAMS((char *));
219 static int time_command_acceptable PARAMS((void));
220 static int special_case_tokens PARAMS((char *));
221 static int read_token PARAMS((int));
222 static char *parse_matched_pair PARAMS((int, int, int, int *, int));
223 static char *parse_comsub PARAMS((int, int, int, int *, int));
224 #if defined (ARRAY_VARS)
225 static char *parse_compound_assignment PARAMS((int *));
226 #endif
227 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
228 static int parse_dparen PARAMS((int));
229 static int parse_arith_cmd PARAMS((char **, int));
230 #endif
231 #if defined (COND_COMMAND)
232 static void cond_error PARAMS((void));
233 static COND_COM *cond_expr PARAMS((void));
234 static COND_COM *cond_or PARAMS((void));
235 static COND_COM *cond_and PARAMS((void));
236 static COND_COM *cond_term PARAMS((void));
237 static int cond_skip_newlines PARAMS((void));
238 static COMMAND *parse_cond_command PARAMS((void));
239 #endif
240 #if defined (ARRAY_VARS)
241 static int token_is_assignment PARAMS((char *, int));
242 static int token_is_ident PARAMS((char *, int));
243 #endif
244 static int read_token_word PARAMS((int));
245 static void discard_parser_constructs PARAMS((int));
246
247 static char *error_token_from_token PARAMS((int));
248 static char *error_token_from_text PARAMS((void));
249 static void print_offending_line PARAMS((void));
250 static void report_syntax_error PARAMS((char *));
251
252 static void handle_eof_input_unit PARAMS((void));
253 static void prompt_again PARAMS((void));
254 #if 0
255 static void reset_readline_prompt PARAMS((void));
256 #endif
257 static void print_prompt PARAMS((void));
258
259 #if defined (HANDLE_MULTIBYTE)
260 static void set_line_mbstate PARAMS((void));
261 static char *shell_input_line_property = NULL;
262 static size_t shell_input_line_propsize = 0;
263 #else
264 # define set_line_mbstate()
265 #endif
266
267 extern int yyerror PARAMS((const char *));
268
269 #ifdef DEBUG
270 extern int yydebug;
271 #endif
272
273 /* Default prompt strings */
274 char *primary_prompt = PPROMPT;
275 char *secondary_prompt = SPROMPT;
276
277 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
278 char *ps1_prompt, *ps2_prompt;
279
280 /* Displayed after reading a command but before executing it in an interactive shell */
281 char *ps0_prompt;
282
283 /* Handle on the current prompt string. Indirectly points through
284 ps1_ or ps2_prompt. */
285 char **prompt_string_pointer = (char **)NULL;
286 char *current_prompt_string;
287
288 /* Non-zero means we expand aliases in commands. */
289 int expand_aliases = 0;
290
291 /* If non-zero, the decoded prompt string undergoes parameter and
292 variable substitution, command substitution, arithmetic substitution,
293 string expansion, process substitution, and quote removal in
294 decode_prompt_string. */
295 int promptvars = 1;
296
297 /* If non-zero, $'...' and $"..." are expanded when they appear within
298 a ${...} expansion, even when the expansion appears within double
299 quotes. */
300 int extended_quote = 1;
301
302 /* The number of lines read from input while creating the current command. */
303 int current_command_line_count;
304
305 /* The number of lines in a command saved while we run parse_and_execute */
306 int saved_command_line_count;
307
308 /* The token that currently denotes the end of parse. */
309 int shell_eof_token;
310
311 /* The token currently being read. */
312 int current_token;
313
314 /* The current parser state. */
315 int parser_state;
316
317 /* Variables to manage the task of reading here documents, because we need to
318 defer the reading until after a complete command has been collected. */
319 static REDIRECT *redir_stack[HEREDOC_MAX];
320 int need_here_doc;
321
322 /* Where shell input comes from. History expansion is performed on each
323 line when the shell is interactive. */
324 static char *shell_input_line = (char *)NULL;
325 static size_t shell_input_line_index;
326 static size_t shell_input_line_size; /* Amount allocated for shell_input_line. */
327 static size_t shell_input_line_len; /* strlen (shell_input_line) */
328
329 /* Either zero or EOF. */
330 static int shell_input_line_terminator;
331
332 /* The line number in a script on which a function definition starts. */
333 static int function_dstart;
334
335 /* The line number in a script on which a function body starts. */
336 static int function_bstart;
337
338 /* The line number in a script at which an arithmetic for command starts. */
339 static int arith_for_lineno;
340
341 /* The decoded prompt string. Used if READLINE is not defined or if
342 editing is turned off. Analogous to current_readline_prompt. */
343 static char *current_decoded_prompt;
344
345 /* The last read token, or NULL. read_token () uses this for context
346 checking. */
347 static int last_read_token;
348
349 /* The token read prior to last_read_token. */
350 static int token_before_that;
351
352 /* The token read prior to token_before_that. */
353 static int two_tokens_ago;
354
355 static int global_extglob;
356
357 /* The line number in a script where the word in a `case WORD', `select WORD'
358 or `for WORD' begins. This is a nested command maximum, since the array
359 index is decremented after a case, select, or for command is parsed. */
360 #define MAX_CASE_NEST 128
361 static int word_lineno[MAX_CASE_NEST+1];
362 static int word_top = -1;
363
364 /* If non-zero, it is the token that we want read_token to return
365 regardless of what text is (or isn't) present to be read. This
366 is reset by read_token. If token_to_read == WORD or
367 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
368 static int token_to_read;
369 static WORD_DESC *word_desc_to_read;
370
371 static REDIRECTEE source;
372 static REDIRECTEE redir;
373
374 static FILE *yyoutstream;
375 static FILE *yyerrstream;
376
377 #line 378 "y.tab.c"
378
379 # ifndef YY_CAST
380 # ifdef __cplusplus
381 # define YY_CAST(Type, Val) static_cast<Type> (Val)
382 # define YY_REINTERPRET_CAST(Type, Val) reinterpret_cast<Type> (Val)
383 # else
384 # define YY_CAST(Type, Val) ((Type) (Val))
385 # define YY_REINTERPRET_CAST(Type, Val) ((Type) (Val))
386 # endif
387 # endif
388 # ifndef YY_NULLPTR
389 # if defined __cplusplus
390 # if 201103L <= __cplusplus
391 # define YY_NULLPTR nullptr
392 # else
393 # define YY_NULLPTR 0
394 # endif
395 # else
396 # define YY_NULLPTR ((void*)0)
397 # endif
398 # endif
399
400 /* Use api.header.include to #include this header
401 instead of duplicating it here. */
402 #ifndef YY_YY_Y_TAB_H_INCLUDED
403 # define YY_YY_Y_TAB_H_INCLUDED
404 /* Debug traces. */
405 #ifndef YYDEBUG
406 # define YYDEBUG 0
407 #endif
408 #if YYDEBUG
409 extern int yydebug;
410 #endif
411
412 /* Token kinds. */
413 #ifndef YYTOKENTYPE
414 # define YYTOKENTYPE
415 enum yytokentype
416 {
417 YYEMPTY = -2,
418 YYEOF = 0, /* "end of file" */
419 YYerror = 256, /* error */
420 YYUNDEF = 257, /* "invalid token" */
421 IF = 258, /* IF */
422 THEN = 259, /* THEN */
423 ELSE = 260, /* ELSE */
424 ELIF = 261, /* ELIF */
425 FI = 262, /* FI */
426 CASE = 263, /* CASE */
427 ESAC = 264, /* ESAC */
428 FOR = 265, /* FOR */
429 SELECT = 266, /* SELECT */
430 WHILE = 267, /* WHILE */
431 UNTIL = 268, /* UNTIL */
432 DO = 269, /* DO */
433 DONE = 270, /* DONE */
434 FUNCTION = 271, /* FUNCTION */
435 COPROC = 272, /* COPROC */
436 COND_START = 273, /* COND_START */
437 COND_END = 274, /* COND_END */
438 COND_ERROR = 275, /* COND_ERROR */
439 IN = 276, /* IN */
440 BANG = 277, /* BANG */
441 TIME = 278, /* TIME */
442 TIMEOPT = 279, /* TIMEOPT */
443 TIMEIGN = 280, /* TIMEIGN */
444 WORD = 281, /* WORD */
445 ASSIGNMENT_WORD = 282, /* ASSIGNMENT_WORD */
446 REDIR_WORD = 283, /* REDIR_WORD */
447 NUMBER = 284, /* NUMBER */
448 ARITH_CMD = 285, /* ARITH_CMD */
449 ARITH_FOR_EXPRS = 286, /* ARITH_FOR_EXPRS */
450 COND_CMD = 287, /* COND_CMD */
451 AND_AND = 288, /* AND_AND */
452 OR_OR = 289, /* OR_OR */
453 GREATER_GREATER = 290, /* GREATER_GREATER */
454 LESS_LESS = 291, /* LESS_LESS */
455 LESS_AND = 292, /* LESS_AND */
456 LESS_LESS_LESS = 293, /* LESS_LESS_LESS */
457 GREATER_AND = 294, /* GREATER_AND */
458 SEMI_SEMI = 295, /* SEMI_SEMI */
459 SEMI_AND = 296, /* SEMI_AND */
460 SEMI_SEMI_AND = 297, /* SEMI_SEMI_AND */
461 LESS_LESS_MINUS = 298, /* LESS_LESS_MINUS */
462 AND_GREATER = 299, /* AND_GREATER */
463 AND_GREATER_GREATER = 300, /* AND_GREATER_GREATER */
464 LESS_GREATER = 301, /* LESS_GREATER */
465 GREATER_BAR = 302, /* GREATER_BAR */
466 BAR_AND = 303, /* BAR_AND */
467 yacc_EOF = 304 /* yacc_EOF */
468 };
469 typedef enum yytokentype yytoken_kind_t;
470 #endif
471 /* Token kinds. */
472 #define YYEOF 0
473 #define YYerror 256
474 #define YYUNDEF 257
475 #define IF 258
476 #define THEN 259
477 #define ELSE 260
478 #define ELIF 261
479 #define FI 262
480 #define CASE 263
481 #define ESAC 264
482 #define FOR 265
483 #define SELECT 266
484 #define WHILE 267
485 #define UNTIL 268
486 #define DO 269
487 #define DONE 270
488 #define FUNCTION 271
489 #define COPROC 272
490 #define COND_START 273
491 #define COND_END 274
492 #define COND_ERROR 275
493 #define IN 276
494 #define BANG 277
495 #define TIME 278
496 #define TIMEOPT 279
497 #define TIMEIGN 280
498 #define WORD 281
499 #define ASSIGNMENT_WORD 282
500 #define REDIR_WORD 283
501 #define NUMBER 284
502 #define ARITH_CMD 285
503 #define ARITH_FOR_EXPRS 286
504 #define COND_CMD 287
505 #define AND_AND 288
506 #define OR_OR 289
507 #define GREATER_GREATER 290
508 #define LESS_LESS 291
509 #define LESS_AND 292
510 #define LESS_LESS_LESS 293
511 #define GREATER_AND 294
512 #define SEMI_SEMI 295
513 #define SEMI_AND 296
514 #define SEMI_SEMI_AND 297
515 #define LESS_LESS_MINUS 298
516 #define AND_GREATER 299
517 #define AND_GREATER_GREATER 300
518 #define LESS_GREATER 301
519 #define GREATER_BAR 302
520 #define BAR_AND 303
521 #define yacc_EOF 304
522
523 /* Value type. */
524 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
525 union YYSTYPE
526 {
527 #line 328 "/Users/chet/src/bash/src/parse.y"
528
529 WORD_DESC *word; /* the word that we read. */
530 int number; /* the number that we read. */
531 WORD_LIST *word_list;
532 COMMAND *command;
533 REDIRECT *redirect;
534 ELEMENT element;
535 PATTERN_LIST *pattern;
536
537 #line 538 "y.tab.c"
538
539 };
540 typedef union YYSTYPE YYSTYPE;
541 # define YYSTYPE_IS_TRIVIAL 1
542 # define YYSTYPE_IS_DECLARED 1
543 #endif
544
545
546 extern YYSTYPE yylval;
547
548 int yyparse (void);
549
550 #endif /* !YY_YY_Y_TAB_H_INCLUDED */
551 /* Symbol kind. */
552 enum yysymbol_kind_t
553 {
554 YYSYMBOL_YYEMPTY = -2,
555 YYSYMBOL_YYEOF = 0, /* "end of file" */
556 YYSYMBOL_YYerror = 1, /* error */
557 YYSYMBOL_YYUNDEF = 2, /* "invalid token" */
558 YYSYMBOL_IF = 3, /* IF */
559 YYSYMBOL_THEN = 4, /* THEN */
560 YYSYMBOL_ELSE = 5, /* ELSE */
561 YYSYMBOL_ELIF = 6, /* ELIF */
562 YYSYMBOL_FI = 7, /* FI */
563 YYSYMBOL_CASE = 8, /* CASE */
564 YYSYMBOL_ESAC = 9, /* ESAC */
565 YYSYMBOL_FOR = 10, /* FOR */
566 YYSYMBOL_SELECT = 11, /* SELECT */
567 YYSYMBOL_WHILE = 12, /* WHILE */
568 YYSYMBOL_UNTIL = 13, /* UNTIL */
569 YYSYMBOL_DO = 14, /* DO */
570 YYSYMBOL_DONE = 15, /* DONE */
571 YYSYMBOL_FUNCTION = 16, /* FUNCTION */
572 YYSYMBOL_COPROC = 17, /* COPROC */
573 YYSYMBOL_COND_START = 18, /* COND_START */
574 YYSYMBOL_COND_END = 19, /* COND_END */
575 YYSYMBOL_COND_ERROR = 20, /* COND_ERROR */
576 YYSYMBOL_IN = 21, /* IN */
577 YYSYMBOL_BANG = 22, /* BANG */
578 YYSYMBOL_TIME = 23, /* TIME */
579 YYSYMBOL_TIMEOPT = 24, /* TIMEOPT */
580 YYSYMBOL_TIMEIGN = 25, /* TIMEIGN */
581 YYSYMBOL_WORD = 26, /* WORD */
582 YYSYMBOL_ASSIGNMENT_WORD = 27, /* ASSIGNMENT_WORD */
583 YYSYMBOL_REDIR_WORD = 28, /* REDIR_WORD */
584 YYSYMBOL_NUMBER = 29, /* NUMBER */
585 YYSYMBOL_ARITH_CMD = 30, /* ARITH_CMD */
586 YYSYMBOL_ARITH_FOR_EXPRS = 31, /* ARITH_FOR_EXPRS */
587 YYSYMBOL_COND_CMD = 32, /* COND_CMD */
588 YYSYMBOL_AND_AND = 33, /* AND_AND */
589 YYSYMBOL_OR_OR = 34, /* OR_OR */
590 YYSYMBOL_GREATER_GREATER = 35, /* GREATER_GREATER */
591 YYSYMBOL_LESS_LESS = 36, /* LESS_LESS */
592 YYSYMBOL_LESS_AND = 37, /* LESS_AND */
593 YYSYMBOL_LESS_LESS_LESS = 38, /* LESS_LESS_LESS */
594 YYSYMBOL_GREATER_AND = 39, /* GREATER_AND */
595 YYSYMBOL_SEMI_SEMI = 40, /* SEMI_SEMI */
596 YYSYMBOL_SEMI_AND = 41, /* SEMI_AND */
597 YYSYMBOL_SEMI_SEMI_AND = 42, /* SEMI_SEMI_AND */
598 YYSYMBOL_LESS_LESS_MINUS = 43, /* LESS_LESS_MINUS */
599 YYSYMBOL_AND_GREATER = 44, /* AND_GREATER */
600 YYSYMBOL_AND_GREATER_GREATER = 45, /* AND_GREATER_GREATER */
601 YYSYMBOL_LESS_GREATER = 46, /* LESS_GREATER */
602 YYSYMBOL_GREATER_BAR = 47, /* GREATER_BAR */
603 YYSYMBOL_BAR_AND = 48, /* BAR_AND */
604 YYSYMBOL_49_ = 49, /* '&' */
605 YYSYMBOL_50_ = 50, /* ';' */
606 YYSYMBOL_51_n_ = 51, /* '\n' */
607 YYSYMBOL_yacc_EOF = 52, /* yacc_EOF */
608 YYSYMBOL_53_ = 53, /* '|' */
609 YYSYMBOL_54_ = 54, /* '>' */
610 YYSYMBOL_55_ = 55, /* '<' */
611 YYSYMBOL_56_ = 56, /* '-' */
612 YYSYMBOL_57_ = 57, /* '{' */
613 YYSYMBOL_58_ = 58, /* '}' */
614 YYSYMBOL_59_ = 59, /* '(' */
615 YYSYMBOL_60_ = 60, /* ')' */
616 YYSYMBOL_YYACCEPT = 61, /* $accept */
617 YYSYMBOL_inputunit = 62, /* inputunit */
618 YYSYMBOL_word_list = 63, /* word_list */
619 YYSYMBOL_redirection = 64, /* redirection */
620 YYSYMBOL_simple_command_element = 65, /* simple_command_element */
621 YYSYMBOL_redirection_list = 66, /* redirection_list */
622 YYSYMBOL_simple_command = 67, /* simple_command */
623 YYSYMBOL_command = 68, /* command */
624 YYSYMBOL_shell_command = 69, /* shell_command */
625 YYSYMBOL_for_command = 70, /* for_command */
626 YYSYMBOL_arith_for_command = 71, /* arith_for_command */
627 YYSYMBOL_select_command = 72, /* select_command */
628 YYSYMBOL_case_command = 73, /* case_command */
629 YYSYMBOL_function_def = 74, /* function_def */
630 YYSYMBOL_function_body = 75, /* function_body */
631 YYSYMBOL_subshell = 76, /* subshell */
632 YYSYMBOL_coproc = 77, /* coproc */
633 YYSYMBOL_if_command = 78, /* if_command */
634 YYSYMBOL_group_command = 79, /* group_command */
635 YYSYMBOL_arith_command = 80, /* arith_command */
636 YYSYMBOL_cond_command = 81, /* cond_command */
637 YYSYMBOL_elif_clause = 82, /* elif_clause */
638 YYSYMBOL_case_clause = 83, /* case_clause */
639 YYSYMBOL_pattern_list = 84, /* pattern_list */
640 YYSYMBOL_case_clause_sequence = 85, /* case_clause_sequence */
641 YYSYMBOL_pattern = 86, /* pattern */
642 YYSYMBOL_list = 87, /* list */
643 YYSYMBOL_compound_list = 88, /* compound_list */
644 YYSYMBOL_list0 = 89, /* list0 */
645 YYSYMBOL_list1 = 90, /* list1 */
646 YYSYMBOL_simple_list_terminator = 91, /* simple_list_terminator */
647 YYSYMBOL_list_terminator = 92, /* list_terminator */
648 YYSYMBOL_newline_list = 93, /* newline_list */
649 YYSYMBOL_simple_list = 94, /* simple_list */
650 YYSYMBOL_simple_list1 = 95, /* simple_list1 */
651 YYSYMBOL_pipeline_command = 96, /* pipeline_command */
652 YYSYMBOL_pipeline = 97, /* pipeline */
653 YYSYMBOL_timespec = 98 /* timespec */
654 };
655 typedef enum yysymbol_kind_t yysymbol_kind_t;
656
657
658
659
660 #ifdef short
661 # undef short
662 #endif
663
664 /* On compilers that do not define __PTRDIFF_MAX__ etc., make sure
665 <limits.h> and (if available) <stdint.h> are included
666 so that the code can choose integer types of a good width. */
667
668 #ifndef __PTRDIFF_MAX__
669 # include <limits.h> /* INFRINGES ON USER NAME SPACE */
670 # if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__
671 # include <stdint.h> /* INFRINGES ON USER NAME SPACE */
672 # define YY_STDINT_H
673 # endif
674 #endif
675
676 /* Narrow types that promote to a signed type and that can represent a
677 signed or unsigned integer of at least N bits. In tables they can
678 save space and decrease cache pressure. Promoting to a signed type
679 helps avoid bugs in integer arithmetic. */
680
681 #ifdef __INT_LEAST8_MAX__
682 typedef __INT_LEAST8_TYPE__ yytype_int8;
683 #elif defined YY_STDINT_H
684 typedef int_least8_t yytype_int8;
685 #else
686 typedef signed char yytype_int8;
687 #endif
688
689 #ifdef __INT_LEAST16_MAX__
690 typedef __INT_LEAST16_TYPE__ yytype_int16;
691 #elif defined YY_STDINT_H
692 typedef int_least16_t yytype_int16;
693 #else
694 typedef short yytype_int16;
695 #endif
696
697 #if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__
698 typedef __UINT_LEAST8_TYPE__ yytype_uint8;
699 #elif (!defined __UINT_LEAST8_MAX__ && defined YY_STDINT_H \
700 && UINT_LEAST8_MAX <= INT_MAX)
701 typedef uint_least8_t yytype_uint8;
702 #elif !defined __UINT_LEAST8_MAX__ && UCHAR_MAX <= INT_MAX
703 typedef unsigned char yytype_uint8;
704 #else
705 typedef short yytype_uint8;
706 #endif
707
708 #if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__
709 typedef __UINT_LEAST16_TYPE__ yytype_uint16;
710 #elif (!defined __UINT_LEAST16_MAX__ && defined YY_STDINT_H \
711 && UINT_LEAST16_MAX <= INT_MAX)
712 typedef uint_least16_t yytype_uint16;
713 #elif !defined __UINT_LEAST16_MAX__ && USHRT_MAX <= INT_MAX
714 typedef unsigned short yytype_uint16;
715 #else
716 typedef int yytype_uint16;
717 #endif
718
719 #ifndef YYPTRDIFF_T
720 # if defined __PTRDIFF_TYPE__ && defined __PTRDIFF_MAX__
721 # define YYPTRDIFF_T __PTRDIFF_TYPE__
722 # define YYPTRDIFF_MAXIMUM __PTRDIFF_MAX__
723 # elif defined PTRDIFF_MAX
724 # ifndef ptrdiff_t
725 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
726 # endif
727 # define YYPTRDIFF_T ptrdiff_t
728 # define YYPTRDIFF_MAXIMUM PTRDIFF_MAX
729 # else
730 # define YYPTRDIFF_T long
731 # define YYPTRDIFF_MAXIMUM LONG_MAX
732 # endif
733 #endif
734
735 #ifndef YYSIZE_T
736 # ifdef __SIZE_TYPE__
737 # define YYSIZE_T __SIZE_TYPE__
738 # elif defined size_t
739 # define YYSIZE_T size_t
740 # elif defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__
741 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
742 # define YYSIZE_T size_t
743 # else
744 # define YYSIZE_T unsigned
745 # endif
746 #endif
747
748 #define YYSIZE_MAXIMUM \
749 YY_CAST (YYPTRDIFF_T, \
750 (YYPTRDIFF_MAXIMUM < YY_CAST (YYSIZE_T, -1) \
751 ? YYPTRDIFF_MAXIMUM \
752 : YY_CAST (YYSIZE_T, -1)))
753
754 #define YYSIZEOF(X) YY_CAST (YYPTRDIFF_T, sizeof (X))
755
756
757 /* Stored state numbers (used for stacks). */
758 typedef yytype_int16 yy_state_t;
759
760 /* State numbers in computations. */
761 typedef int yy_state_fast_t;
762
763 #ifndef YY_
764 # if defined YYENABLE_NLS && YYENABLE_NLS
765 # if ENABLE_NLS
766 # include <libintl.h> /* INFRINGES ON USER NAME SPACE */
767 # define YY_(Msgid) dgettext ("bison-runtime", Msgid)
768 # endif
769 # endif
770 # ifndef YY_
771 # define YY_(Msgid) Msgid
772 # endif
773 #endif
774
775
776 #ifndef YY_ATTRIBUTE_PURE
777 # if defined __GNUC__ && 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
778 # define YY_ATTRIBUTE_PURE __attribute__ ((__pure__))
779 # else
780 # define YY_ATTRIBUTE_PURE
781 # endif
782 #endif
783
784 #ifndef YY_ATTRIBUTE_UNUSED
785 # if defined __GNUC__ && 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
786 # define YY_ATTRIBUTE_UNUSED __attribute__ ((__unused__))
787 # else
788 # define YY_ATTRIBUTE_UNUSED
789 # endif
790 #endif
791
792 /* Suppress unused-variable warnings by "using" E. */
793 #if ! defined lint || defined __GNUC__
794 # define YYUSE(E) ((void) (E))
795 #else
796 # define YYUSE(E) /* empty */
797 #endif
798
799 #if defined __GNUC__ && ! defined __ICC && 407 <= __GNUC__ * 100 + __GNUC_MINOR__
800 /* Suppress an incorrect diagnostic about yylval being uninitialized. */
801 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
802 _Pragma ("GCC diagnostic push") \
803 _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"") \
804 _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
805 # define YY_IGNORE_MAYBE_UNINITIALIZED_END \
806 _Pragma ("GCC diagnostic pop")
807 #else
808 # define YY_INITIAL_VALUE(Value) Value
809 #endif
810 #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
811 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
812 # define YY_IGNORE_MAYBE_UNINITIALIZED_END
813 #endif
814 #ifndef YY_INITIAL_VALUE
815 # define YY_INITIAL_VALUE(Value) /* Nothing. */
816 #endif
817
818 #if defined __cplusplus && defined __GNUC__ && ! defined __ICC && 6 <= __GNUC__
819 # define YY_IGNORE_USELESS_CAST_BEGIN \
820 _Pragma ("GCC diagnostic push") \
821 _Pragma ("GCC diagnostic ignored \"-Wuseless-cast\"")
822 # define YY_IGNORE_USELESS_CAST_END \
823 _Pragma ("GCC diagnostic pop")
824 #endif
825 #ifndef YY_IGNORE_USELESS_CAST_BEGIN
826 # define YY_IGNORE_USELESS_CAST_BEGIN
827 # define YY_IGNORE_USELESS_CAST_END
828 #endif
829
830
831 #define YY_ASSERT(E) ((void) (0 && (E)))
832
833 #if !defined yyoverflow
834
835 /* The parser invokes alloca or malloc; define the necessary symbols. */
836
837 # ifdef YYSTACK_USE_ALLOCA
838 # if YYSTACK_USE_ALLOCA
839 # ifdef __GNUC__
840 # define YYSTACK_ALLOC __builtin_alloca
841 # elif defined __BUILTIN_VA_ARG_INCR
842 # include <alloca.h> /* INFRINGES ON USER NAME SPACE */
843 # elif defined _AIX
844 # define YYSTACK_ALLOC __alloca
845 # elif defined _MSC_VER
846 # include <malloc.h> /* INFRINGES ON USER NAME SPACE */
847 # define alloca _alloca
848 # else
849 # define YYSTACK_ALLOC alloca
850 # if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS
851 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
852 /* Use EXIT_SUCCESS as a witness for stdlib.h. */
853 # ifndef EXIT_SUCCESS
854 # define EXIT_SUCCESS 0
855 # endif
856 # endif
857 # endif
858 # endif
859 # endif
860
861 # ifdef YYSTACK_ALLOC
862 /* Pacify GCC's 'empty if-body' warning. */
863 # define YYSTACK_FREE(Ptr) do { /* empty */; } while (0)
864 # ifndef YYSTACK_ALLOC_MAXIMUM
865 /* The OS might guarantee only one guard page at the bottom of the stack,
866 and a page size can be as small as 4096 bytes. So we cannot safely
867 invoke alloca (N) if N exceeds 4096. Use a slightly smaller number
868 to allow for a few compiler-allocated temporary stack slots. */
869 # define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
870 # endif
871 # else
872 # define YYSTACK_ALLOC YYMALLOC
873 # define YYSTACK_FREE YYFREE
874 # ifndef YYSTACK_ALLOC_MAXIMUM
875 # define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
876 # endif
877 # if (defined __cplusplus && ! defined EXIT_SUCCESS \
878 && ! ((defined YYMALLOC || defined malloc) \
879 && (defined YYFREE || defined free)))
880 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
881 # ifndef EXIT_SUCCESS
882 # define EXIT_SUCCESS 0
883 # endif
884 # endif
885 # ifndef YYMALLOC
886 # define YYMALLOC malloc
887 # if ! defined malloc && ! defined EXIT_SUCCESS
888 void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
889 # endif
890 # endif
891 # ifndef YYFREE
892 # define YYFREE free
893 # if ! defined free && ! defined EXIT_SUCCESS
894 void free (void *); /* INFRINGES ON USER NAME SPACE */
895 # endif
896 # endif
897 # endif
898 #endif /* !defined yyoverflow */
899
900 #if (! defined yyoverflow \
901 && (! defined __cplusplus \
902 || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
903
904 /* A type that is properly aligned for any stack member. */
905 union yyalloc
906 {
907 yy_state_t yyss_alloc;
908 YYSTYPE yyvs_alloc;
909 };
910
911 /* The size of the maximum gap between one aligned stack and the next. */
912 # define YYSTACK_GAP_MAXIMUM (YYSIZEOF (union yyalloc) - 1)
913
914 /* The size of an array large to enough to hold all stacks, each with
915 N elements. */
916 # define YYSTACK_BYTES(N) \
917 ((N) * (YYSIZEOF (yy_state_t) + YYSIZEOF (YYSTYPE)) \
918 + YYSTACK_GAP_MAXIMUM)
919
920 # define YYCOPY_NEEDED 1
921
922 /* Relocate STACK from its old location to the new one. The
923 local variables YYSIZE and YYSTACKSIZE give the old and new number of
924 elements in the stack, and YYPTR gives the new location of the
925 stack. Advance YYPTR to a properly aligned location for the next
926 stack. */
927 # define YYSTACK_RELOCATE(Stack_alloc, Stack) \
928 do \
929 { \
930 YYPTRDIFF_T yynewbytes; \
931 YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \
932 Stack = &yyptr->Stack_alloc; \
933 yynewbytes = yystacksize * YYSIZEOF (*Stack) + YYSTACK_GAP_MAXIMUM; \
934 yyptr += yynewbytes / YYSIZEOF (*yyptr); \
935 } \
936 while (0)
937
938 #endif
939
940 #if defined YYCOPY_NEEDED && YYCOPY_NEEDED
941 /* Copy COUNT objects from SRC to DST. The source and destination do
942 not overlap. */
943 # ifndef YYCOPY
944 # if defined __GNUC__ && 1 < __GNUC__
945 # define YYCOPY(Dst, Src, Count) \
946 __builtin_memcpy (Dst, Src, YY_CAST (YYSIZE_T, (Count)) * sizeof (*(Src)))
947 # else
948 # define YYCOPY(Dst, Src, Count) \
949 do \
950 { \
951 YYPTRDIFF_T yyi; \
952 for (yyi = 0; yyi < (Count); yyi++) \
953 (Dst)[yyi] = (Src)[yyi]; \
954 } \
955 while (0)
956 # endif
957 # endif
958 #endif /* !YYCOPY_NEEDED */
959
960 /* YYFINAL -- State number of the termination state. */
961 #define YYFINAL 118
962 /* YYLAST -- Last index in YYTABLE. */
963 #define YYLAST 661
964
965 /* YYNTOKENS -- Number of terminals. */
966 #define YYNTOKENS 61
967 /* YYNNTS -- Number of nonterminals. */
968 #define YYNNTS 38
969 /* YYNRULES -- Number of rules. */
970 #define YYNRULES 172
971 /* YYNSTATES -- Number of states. */
972 #define YYNSTATES 346
973
974 #define YYMAXUTOK 304
975
976
977 /* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM
978 as returned by yylex, with out-of-bounds checking. */
979 #define YYTRANSLATE(YYX) \
980 (0 <= (YYX) && (YYX) <= YYMAXUTOK \
981 ? YY_CAST (yysymbol_kind_t, yytranslate[YYX]) \
982 : YYSYMBOL_YYUNDEF)
983
984 /* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM
985 as returned by yylex. */
986 static const yytype_int8 yytranslate[] =
987 {
988 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
989 51, 2, 2, 2, 2, 2, 2, 2, 2, 2,
990 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
991 2, 2, 2, 2, 2, 2, 2, 2, 49, 2,
992 59, 60, 2, 2, 2, 56, 2, 2, 2, 2,
993 2, 2, 2, 2, 2, 2, 2, 2, 2, 50,
994 55, 2, 54, 2, 2, 2, 2, 2, 2, 2,
995 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
996 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
997 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
998 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
999 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1000 2, 2, 2, 57, 53, 58, 2, 2, 2, 2,
1001 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1002 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1003 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1004 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1005 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1006 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1007 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1008 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1009 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1010 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1011 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1012 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1013 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
1014 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
1015 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1016 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
1017 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
1018 45, 46, 47, 48, 52
1019 };
1020
1021 #if YYDEBUG
1022 /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
1023 static const yytype_int16 yyrline[] =
1024 {
1025 0, 381, 381, 392, 401, 416, 433, 443, 445, 449,
1026 455, 461, 467, 473, 479, 485, 491, 497, 503, 509,
1027 515, 521, 527, 533, 539, 546, 553, 560, 567, 574,
1028 581, 587, 593, 599, 605, 611, 617, 623, 629, 635,
1029 641, 647, 653, 659, 665, 671, 677, 683, 689, 695,
1030 701, 707, 713, 721, 723, 725, 729, 733, 744, 746,
1031 750, 752, 754, 770, 772, 776, 778, 780, 782, 784,
1032 786, 788, 790, 792, 794, 796, 800, 805, 810, 815,
1033 820, 825, 830, 835, 842, 848, 854, 860, 868, 873,
1034 878, 883, 888, 893, 898, 903, 910, 915, 920, 927,
1035 929, 931, 933, 937, 939, 970, 977, 982, 999, 1004,
1036 1021, 1028, 1030, 1032, 1037, 1041, 1045, 1049, 1051, 1053,
1037 1057, 1058, 1062, 1064, 1066, 1068, 1072, 1074, 1076, 1078,
1038 1080, 1082, 1086, 1088, 1097, 1105, 1106, 1112, 1113, 1120,
1039 1124, 1126, 1128, 1135, 1137, 1139, 1143, 1144, 1147, 1149,
1040 1151, 1155, 1156, 1165, 1178, 1194, 1209, 1211, 1213, 1220,
1041 1223, 1227, 1229, 1235, 1241, 1261, 1284, 1286, 1309, 1313,
1042 1315, 1317, 1319
1043 };
1044 #endif
1045
1046 /** Accessing symbol of state STATE. */
1047 #define YY_ACCESSING_SYMBOL(State) YY_CAST (yysymbol_kind_t, yystos[State])
1048
1049 #if YYDEBUG || 0
1050 /* The user-facing name of the symbol whose (internal) number is
1051 YYSYMBOL. No bounds checking. */
1052 static const char *yysymbol_name (yysymbol_kind_t yysymbol) YY_ATTRIBUTE_UNUSED;
1053
1054 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
1055 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
1056 static const char *const yytname[] =
1057 {
1058 "\"end of file\"", "error", "\"invalid token\"", "IF", "THEN", "ELSE",
1059 "ELIF", "FI", "CASE", "ESAC", "FOR", "SELECT", "WHILE", "UNTIL", "DO",
1060 "DONE", "FUNCTION", "COPROC", "COND_START", "COND_END", "COND_ERROR",
1061 "IN", "BANG", "TIME", "TIMEOPT", "TIMEIGN", "WORD", "ASSIGNMENT_WORD",
1062 "REDIR_WORD", "NUMBER", "ARITH_CMD", "ARITH_FOR_EXPRS", "COND_CMD",
1063 "AND_AND", "OR_OR", "GREATER_GREATER", "LESS_LESS", "LESS_AND",
1064 "LESS_LESS_LESS", "GREATER_AND", "SEMI_SEMI", "SEMI_AND",
1065 "SEMI_SEMI_AND", "LESS_LESS_MINUS", "AND_GREATER", "AND_GREATER_GREATER",
1066 "LESS_GREATER", "GREATER_BAR", "BAR_AND", "'&'", "';'", "'\\n'",
1067 "yacc_EOF", "'|'", "'>'", "'<'", "'-'", "'{'", "'}'", "'('", "')'",
1068 "$accept", "inputunit", "word_list", "redirection",
1069 "simple_command_element", "redirection_list", "simple_command",
1070 "command", "shell_command", "for_command", "arith_for_command",
1071 "select_command", "case_command", "function_def", "function_body",
1072 "subshell", "coproc", "if_command", "group_command", "arith_command",
1073 "cond_command", "elif_clause", "case_clause", "pattern_list",
1074 "case_clause_sequence", "pattern", "list", "compound_list", "list0",
1075 "list1", "simple_list_terminator", "list_terminator", "newline_list",
1076 "simple_list", "simple_list1", "pipeline_command", "pipeline",
1077 "timespec", YY_NULLPTR
1078 };
1079
1080 static const char *
1081 yysymbol_name (yysymbol_kind_t yysymbol)
1082 {
1083 return yytname[yysymbol];
1084 }
1085 #endif
1086
1087 #ifdef YYPRINT
1088 /* YYTOKNUM[NUM] -- (External) token number corresponding to the
1089 (internal) symbol number NUM (which must be that of a token). */
1090 static const yytype_int16 yytoknum[] =
1091 {
1092 0, 256, 257, 258, 259, 260, 261, 262, 263, 264,
1093 265, 266, 267, 268, 269, 270, 271, 272, 273, 274,
1094 275, 276, 277, 278, 279, 280, 281, 282, 283, 284,
1095 285, 286, 287, 288, 289, 290, 291, 292, 293, 294,
1096 295, 296, 297, 298, 299, 300, 301, 302, 303, 38,
1097 59, 10, 304, 124, 62, 60, 45, 123, 125, 40,
1098 41
1099 };
1100 #endif
1101
1102 #define YYPACT_NINF (-204)
1103
1104 #define yypact_value_is_default(Yyn) \
1105 ((Yyn) == YYPACT_NINF)
1106
1107 #define YYTABLE_NINF (-1)
1108
1109 #define yytable_value_is_error(Yyn) \
1110 0
1111
1112 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
1113 STATE-NUM. */
1114 static const yytype_int16 yypact[] =
1115 {
1116 313, 108, -204, -6, 8, 2, -204, -204, 10, 513,
1117 17, 363, 153, -21, -204, 593, 606, -204, 14, 26,
1118 113, 41, 127, 72, 85, 92, 95, 98, -204, -204,
1119 100, 105, -204, -204, 65, -204, -204, 551, -204, 572,
1120 -204, -204, -204, -204, -204, -204, -204, -204, -204, -204,
1121 -204, 146, 140, -204, 67, 363, -204, -204, -204, 133,
1122 413, -204, 93, 55, 104, 156, 161, 11, 45, 551,
1123 572, 163, -204, -204, -204, -204, -204, 167, -204, 152,
1124 208, 217, 129, 220, 150, 221, 223, 225, 233, 234,
1125 238, 239, 158, 240, 162, 241, 243, 244, 252, 253,
1126 -204, -204, -204, -204, -204, -204, -204, -204, -204, -204,
1127 -204, -204, -204, -204, -204, -204, 194, 227, -204, -204,
1128 -204, -204, 572, -204, -204, -204, -204, -204, 463, 463,
1129 -204, -204, -204, -204, -204, -204, -204, -7, -204, 59,
1130 -204, 52, -204, -204, -204, -204, 62, -204, -204, -204,
1131 235, 572, -204, 572, 572, -204, -204, -204, -204, -204,
1132 -204, -204, -204, -204, -204, -204, -204, -204, -204, -204,
1133 -204, -204, -204, -204, -204, -204, -204, -204, -204, -204,
1134 -204, -204, -204, -204, -204, -204, -204, -204, -204, 413,
1135 413, 191, 191, 245, 245, 203, -204, -204, -204, -204,
1136 -204, -204, 37, -204, 176, -204, 270, 228, 76, 79,
1137 -204, 176, -204, 278, 282, 563, -204, 572, 572, 563,
1138 -204, -204, 67, 67, -204, -204, -204, 291, 413, 413,
1139 413, 413, 413, 294, 175, -204, 28, -204, -204, 292,
1140 -204, 187, -204, 250, -204, -204, -204, -204, -204, -204,
1141 295, 413, 187, -204, 251, -204, -204, -204, 563, -204,
1142 304, 314, -204, -204, -204, 196, 196, 196, -204, -204,
1143 -204, -204, 179, 38, -204, -204, 296, -28, 302, 274,
1144 -204, -204, -204, 87, -204, 318, 276, 322, 280, -204,
1145 -7, -204, 111, -204, -204, -204, -204, -204, -204, -204,
1146 -204, 39, 319, -204, -204, -204, 114, -204, -204, -204,
1147 -204, -204, -204, 115, -204, -204, 226, -204, -204, -204,
1148 413, -204, -204, 329, 288, -204, -204, 332, 297, -204,
1149 -204, -204, 413, 338, 303, -204, -204, 339, 305, -204,
1150 -204, -204, -204, -204, -204, -204
1151 };
1152
1153 /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
1154 Performed when YYTABLE does not specify something else to do. Zero
1155 means the default is an error. */
1156 static const yytype_uint8 yydefact[] =
1157 {
1158 0, 0, 151, 0, 0, 0, 151, 151, 0, 0,
1159 0, 0, 169, 53, 54, 0, 0, 115, 0, 0,
1160 0, 0, 0, 0, 0, 0, 0, 0, 3, 6,
1161 0, 0, 151, 151, 0, 55, 58, 60, 168, 61,
1162 65, 75, 69, 66, 63, 71, 64, 70, 72, 73,
1163 74, 0, 153, 160, 161, 0, 4, 5, 135, 0,
1164 0, 151, 151, 0, 151, 0, 0, 0, 53, 110,
1165 106, 0, 149, 148, 150, 165, 162, 170, 171, 0,
1166 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1168 15, 24, 39, 33, 48, 30, 42, 36, 45, 27,
1169 51, 52, 21, 18, 9, 10, 0, 0, 1, 53,
1170 59, 56, 62, 146, 147, 2, 151, 151, 154, 155,
1171 151, 151, 164, 163, 151, 152, 134, 136, 145, 0,
1172 151, 0, 151, 151, 151, 151, 0, 151, 151, 151,
1173 151, 103, 101, 108, 107, 116, 172, 151, 17, 26,
1174 41, 35, 50, 32, 44, 38, 47, 29, 23, 20,
1175 13, 14, 16, 25, 40, 34, 49, 31, 43, 37,
1176 46, 28, 22, 19, 11, 12, 114, 105, 57, 0,
1177 0, 158, 159, 0, 0, 0, 151, 151, 151, 151,
1178 151, 151, 0, 151, 0, 151, 0, 0, 0, 0,
1179 151, 0, 151, 0, 0, 0, 151, 104, 109, 0,
1180 156, 157, 167, 166, 151, 151, 111, 0, 0, 0,
1181 138, 139, 137, 0, 120, 151, 0, 151, 151, 0,
1182 7, 0, 151, 0, 86, 87, 151, 151, 151, 151,
1183 0, 0, 0, 151, 0, 67, 68, 102, 0, 99,
1184 0, 0, 113, 140, 141, 142, 143, 144, 98, 126,
1185 128, 130, 121, 0, 96, 132, 0, 0, 0, 0,
1186 76, 8, 151, 0, 77, 0, 0, 0, 0, 88,
1187 0, 151, 0, 89, 100, 112, 151, 127, 129, 131,
1188 97, 0, 0, 151, 78, 79, 0, 151, 151, 84,
1189 85, 90, 91, 0, 151, 151, 117, 151, 133, 122,
1190 123, 151, 151, 0, 0, 151, 151, 0, 0, 151,
1191 119, 124, 125, 0, 0, 82, 83, 0, 0, 94,
1192 95, 118, 80, 81, 92, 93
1193 };
1194
1195 /* YYPGOTO[NTERM-NUM]. */
1196 static const yytype_int16 yypgoto[] =
1197 {
1198 -204, -204, 117, -37, -19, -67, 353, -204, -8, -204,
1199 -204, -204, -204, -204, -184, -204, -204, -204, -204, -204,
1200 -204, 53, -204, 142, -204, 102, -203, -2, -204, 283,
1201 -204, -47, -49, -204, -118, 6, 47, -204
1202 };
1203
1204 /* YYDEFGOTO[NTERM-NUM]. */
1205 static const yytype_int16 yydefgoto[] =
1206 {
1207 -1, 34, 241, 35, 36, 122, 37, 38, 39, 40,
1208 41, 42, 43, 44, 152, 45, 46, 47, 48, 49,
1209 50, 227, 233, 234, 235, 277, 58, 117, 136, 137,
1210 125, 75, 60, 51, 52, 138, 54, 55
1211 };
1212
1213 /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If
1214 positive, shift that token. If negative, reduce the rule whose
1215 number is the opposite. If YYTABLE_NINF, syntax error. */
1216 static const yytype_int16 yytable[] =
1217 {
1218 59, 70, 121, 154, 65, 66, 53, 250, 132, 254,
1219 191, 192, 139, 141, 2, 146, 144, 76, 120, 3,
1220 61, 4, 5, 6, 7, 302, 196, 197, 64, 10,
1221 116, 257, 303, 121, 62, 259, 67, 274, 79, 63,
1222 100, 17, 198, 199, 200, 287, 288, 300, 2, 71,
1223 120, 237, 101, 3, 275, 4, 5, 6, 7, 151,
1224 153, 133, 149, 10, 275, 118, 203, 105, 32, 142,
1225 150, 220, 221, 204, 294, 17, 210, 189, 190, 135,
1226 201, 193, 194, 211, 217, 188, 218, 276, 135, 135,
1227 246, 202, 302, 248, 238, 208, 209, 276, 109, 317,
1228 215, 307, 32, 135, 33, 72, 73, 74, 219, 205,
1229 135, 110, 143, 135, 121, 130, 121, 188, 111, 212,
1230 131, 112, 337, 338, 113, 314, 114, 135, 321, 325,
1231 135, 115, 195, 247, 53, 53, 249, 134, 135, 102,
1232 206, 207, 103, 140, 308, 213, 214, 228, 229, 230,
1233 231, 232, 236, 106, 145, 160, 107, 242, 161, 56,
1234 57, 251, 135, 251, 253, 135, 135, 258, 315, 104,
1235 147, 322, 326, 126, 127, 148, 164, 77, 78, 165,
1236 188, 188, 155, 108, 174, 162, 273, 175, 178, 128,
1237 129, 179, 156, 283, 282, 53, 53, 123, 124, 251,
1238 251, 239, 240, 243, 292, 291, 166, 151, 224, 225,
1239 226, 151, 157, 281, 176, 269, 270, 271, 180, 297,
1240 298, 299, 260, 261, 126, 127, 72, 73, 74, 196,
1241 197, 329, 225, 306, 158, 278, 279, 72, 73, 74,
1242 222, 223, 313, 159, 285, 286, 163, 167, 2, 168,
1243 151, 169, 186, 3, 320, 4, 5, 6, 7, 170,
1244 171, 8, 9, 10, 172, 173, 177, 181, 332, 182,
1245 183, 13, 14, 15, 16, 17, 251, 251, 184, 185,
1246 18, 19, 20, 21, 22, 244, 245, 187, 23, 24,
1247 25, 26, 27, 255, 316, 216, 135, 256, 262, 30,
1248 31, 319, 32, 268, 33, 323, 324, 280, 284, 293,
1249 289, 295, 327, 328, 1, 331, 2, 304, 296, 333,
1250 334, 3, 275, 4, 5, 6, 7, 341, 252, 8,
1251 9, 10, 305, 309, 310, 11, 12, 311, 312, 13,
1252 14, 15, 16, 17, 335, 318, 336, 339, 18, 19,
1253 20, 21, 22, 342, 344, 340, 23, 24, 25, 26,
1254 27, 343, 69, 345, 28, 29, 2, 30, 31, 330,
1255 32, 3, 33, 4, 5, 6, 7, 272, 301, 8,
1256 9, 10, 0, 0, 0, 11, 12, 0, 0, 13,
1257 14, 15, 16, 17, 0, 0, 0, 0, 18, 19,
1258 20, 21, 22, 0, 0, 0, 23, 24, 25, 26,
1259 27, 0, 0, 72, 73, 74, 2, 30, 31, 0,
1260 32, 3, 33, 4, 5, 6, 7, 0, 0, 8,
1261 9, 10, 0, 0, 0, 11, 12, 0, 0, 13,
1262 14, 15, 16, 17, 0, 0, 0, 0, 18, 19,
1263 20, 21, 22, 0, 0, 0, 23, 24, 25, 26,
1264 27, 0, 0, 0, 135, 0, 2, 30, 31, 0,
1265 32, 3, 33, 4, 5, 6, 7, 0, 0, 8,
1266 9, 10, 0, 0, 0, 11, 12, 0, 0, 13,
1267 14, 15, 16, 17, 0, 0, 0, 0, 18, 19,
1268 20, 21, 22, 0, 0, 0, 23, 24, 25, 26,
1269 27, 263, 264, 265, 266, 267, 2, 30, 31, 0,
1270 32, 3, 33, 4, 5, 6, 7, 0, 0, 0,
1271 0, 10, 0, 0, 290, 0, 0, 0, 0, 68,
1272 14, 15, 16, 17, 0, 0, 0, 0, 18, 19,
1273 20, 21, 22, 0, 0, 0, 23, 24, 25, 26,
1274 27, 0, 0, 0, 0, 0, 2, 30, 31, 0,
1275 32, 3, 33, 4, 5, 6, 7, 119, 14, 15,
1276 16, 10, 0, 0, 0, 0, 18, 19, 20, 21,
1277 22, 0, 0, 17, 23, 24, 25, 26, 27, 0,
1278 15, 16, 0, 0, 0, 30, 31, 18, 19, 20,
1279 21, 22, 0, 0, 135, 23, 24, 25, 26, 27,
1280 32, 0, 33, 0, 0, 0, 30, 31, 80, 81,
1281 82, 83, 84, 0, 0, 0, 85, 0, 0, 86,
1282 87, 90, 91, 92, 93, 94, 0, 88, 89, 95,
1283 0, 0, 96, 97, 0, 0, 0, 0, 0, 0,
1284 98, 99
1285 };
1286
1287 static const yytype_int16 yycheck[] =
1288 {
1289 2, 9, 39, 70, 6, 7, 0, 210, 55, 212,
1290 128, 129, 61, 62, 3, 64, 63, 11, 37, 8,
1291 26, 10, 11, 12, 13, 53, 33, 34, 26, 18,
1292 32, 215, 60, 70, 26, 219, 26, 9, 59, 31,
1293 26, 30, 49, 50, 51, 248, 249, 9, 3, 32,
1294 69, 14, 26, 8, 26, 10, 11, 12, 13, 67,
1295 68, 55, 51, 18, 26, 0, 14, 26, 57, 14,
1296 59, 189, 190, 21, 258, 30, 14, 126, 127, 51,
1297 21, 130, 131, 21, 151, 122, 153, 59, 51, 51,
1298 14, 140, 53, 14, 57, 144, 145, 59, 26, 60,
1299 149, 14, 57, 51, 59, 50, 51, 52, 157, 57,
1300 51, 26, 57, 51, 151, 48, 153, 154, 26, 57,
1301 53, 26, 325, 326, 26, 14, 26, 51, 14, 14,
1302 51, 26, 134, 57, 128, 129, 57, 4, 51, 26,
1303 142, 143, 29, 50, 57, 147, 148, 196, 197, 198,
1304 199, 200, 201, 26, 50, 26, 29, 204, 29, 51,
1305 52, 210, 51, 212, 211, 51, 51, 216, 57, 56,
1306 14, 57, 57, 33, 34, 14, 26, 24, 25, 29,
1307 217, 218, 19, 56, 26, 56, 235, 29, 26, 49,
1308 50, 29, 25, 242, 241, 189, 190, 51, 52, 248,
1309 249, 203, 26, 205, 253, 252, 56, 215, 5, 6,
1310 7, 219, 60, 26, 56, 40, 41, 42, 56, 40,
1311 41, 42, 224, 225, 33, 34, 50, 51, 52, 33,
1312 34, 5, 6, 282, 26, 237, 238, 50, 51, 52,
1313 193, 194, 291, 26, 246, 247, 26, 26, 3, 26,
1314 258, 26, 58, 8, 303, 10, 11, 12, 13, 26,
1315 26, 16, 17, 18, 26, 26, 26, 26, 317, 26,
1316 26, 26, 27, 28, 29, 30, 325, 326, 26, 26,
1317 35, 36, 37, 38, 39, 15, 58, 60, 43, 44,
1318 45, 46, 47, 15, 296, 60, 51, 15, 7, 54,
1319 55, 303, 57, 9, 59, 307, 308, 15, 58, 58,
1320 15, 7, 314, 315, 1, 317, 3, 15, 4, 321,
1321 322, 8, 26, 10, 11, 12, 13, 329, 211, 16,
1322 17, 18, 58, 15, 58, 22, 23, 15, 58, 26,
1323 27, 28, 29, 30, 15, 26, 58, 15, 35, 36,
1324 37, 38, 39, 15, 15, 58, 43, 44, 45, 46,
1325 47, 58, 9, 58, 51, 52, 3, 54, 55, 316,
1326 57, 8, 59, 10, 11, 12, 13, 235, 276, 16,
1327 17, 18, -1, -1, -1, 22, 23, -1, -1, 26,
1328 27, 28, 29, 30, -1, -1, -1, -1, 35, 36,
1329 37, 38, 39, -1, -1, -1, 43, 44, 45, 46,
1330 47, -1, -1, 50, 51, 52, 3, 54, 55, -1,
1331 57, 8, 59, 10, 11, 12, 13, -1, -1, 16,
1332 17, 18, -1, -1, -1, 22, 23, -1, -1, 26,
1333 27, 28, 29, 30, -1, -1, -1, -1, 35, 36,
1334 37, 38, 39, -1, -1, -1, 43, 44, 45, 46,
1335 47, -1, -1, -1, 51, -1, 3, 54, 55, -1,
1336 57, 8, 59, 10, 11, 12, 13, -1, -1, 16,
1337 17, 18, -1, -1, -1, 22, 23, -1, -1, 26,
1338 27, 28, 29, 30, -1, -1, -1, -1, 35, 36,
1339 37, 38, 39, -1, -1, -1, 43, 44, 45, 46,
1340 47, 228, 229, 230, 231, 232, 3, 54, 55, -1,
1341 57, 8, 59, 10, 11, 12, 13, -1, -1, -1,
1342 -1, 18, -1, -1, 251, -1, -1, -1, -1, 26,
1343 27, 28, 29, 30, -1, -1, -1, -1, 35, 36,
1344 37, 38, 39, -1, -1, -1, 43, 44, 45, 46,
1345 47, -1, -1, -1, -1, -1, 3, 54, 55, -1,
1346 57, 8, 59, 10, 11, 12, 13, 26, 27, 28,
1347 29, 18, -1, -1, -1, -1, 35, 36, 37, 38,
1348 39, -1, -1, 30, 43, 44, 45, 46, 47, -1,
1349 28, 29, -1, -1, -1, 54, 55, 35, 36, 37,
1350 38, 39, -1, -1, 51, 43, 44, 45, 46, 47,
1351 57, -1, 59, -1, -1, -1, 54, 55, 35, 36,
1352 37, 38, 39, -1, -1, -1, 43, -1, -1, 46,
1353 47, 35, 36, 37, 38, 39, -1, 54, 55, 43,
1354 -1, -1, 46, 47, -1, -1, -1, -1, -1, -1,
1355 54, 55
1356 };
1357
1358 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
1359 symbol of state STATE-NUM. */
1360 static const yytype_int8 yystos[] =
1361 {
1362 0, 1, 3, 8, 10, 11, 12, 13, 16, 17,
1363 18, 22, 23, 26, 27, 28, 29, 30, 35, 36,
1364 37, 38, 39, 43, 44, 45, 46, 47, 51, 52,
1365 54, 55, 57, 59, 62, 64, 65, 67, 68, 69,
1366 70, 71, 72, 73, 74, 76, 77, 78, 79, 80,
1367 81, 94, 95, 96, 97, 98, 51, 52, 87, 88,
1368 93, 26, 26, 31, 26, 88, 88, 26, 26, 67,
1369 69, 32, 50, 51, 52, 92, 96, 24, 25, 59,
1370 35, 36, 37, 38, 39, 43, 46, 47, 54, 55,
1371 35, 36, 37, 38, 39, 43, 46, 47, 54, 55,
1372 26, 26, 26, 29, 56, 26, 26, 29, 56, 26,
1373 26, 26, 26, 26, 26, 26, 88, 88, 0, 26,
1374 65, 64, 66, 51, 52, 91, 33, 34, 49, 50,
1375 48, 53, 92, 96, 4, 51, 89, 90, 96, 93,
1376 50, 93, 14, 57, 92, 50, 93, 14, 14, 51,
1377 59, 69, 75, 69, 66, 19, 25, 60, 26, 26,
1378 26, 29, 56, 26, 26, 29, 56, 26, 26, 26,
1379 26, 26, 26, 26, 26, 29, 56, 26, 26, 29,
1380 56, 26, 26, 26, 26, 26, 58, 60, 64, 93,
1381 93, 95, 95, 93, 93, 88, 33, 34, 49, 50,
1382 51, 21, 93, 14, 21, 57, 88, 88, 93, 93,
1383 14, 21, 57, 88, 88, 93, 60, 66, 66, 93,
1384 95, 95, 97, 97, 5, 6, 7, 82, 93, 93,
1385 93, 93, 93, 83, 84, 85, 93, 14, 57, 88,
1386 26, 63, 92, 88, 15, 58, 14, 57, 14, 57,
1387 87, 93, 63, 92, 87, 15, 15, 75, 93, 75,
1388 88, 88, 7, 90, 90, 90, 90, 90, 9, 40,
1389 41, 42, 84, 93, 9, 26, 59, 86, 88, 88,
1390 15, 26, 92, 93, 58, 88, 88, 87, 87, 15,
1391 90, 92, 93, 58, 75, 7, 4, 40, 41, 42,
1392 9, 86, 53, 60, 15, 58, 93, 14, 57, 15,
1393 58, 15, 58, 93, 14, 57, 88, 60, 26, 88,
1394 93, 14, 57, 88, 88, 14, 57, 88, 88, 5,
1395 82, 88, 93, 88, 88, 15, 58, 87, 87, 15,
1396 58, 88, 15, 58, 15, 58
1397 };
1398
1399 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
1400 static const yytype_int8 yyr1[] =
1401 {
1402 0, 61, 62, 62, 62, 62, 62, 63, 63, 64,
1403 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
1404 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
1405 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
1406 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
1407 64, 64, 64, 65, 65, 65, 66, 66, 67, 67,
1408 68, 68, 68, 68, 68, 69, 69, 69, 69, 69,
1409 69, 69, 69, 69, 69, 69, 70, 70, 70, 70,
1410 70, 70, 70, 70, 71, 71, 71, 71, 72, 72,
1411 72, 72, 72, 72, 72, 72, 73, 73, 73, 74,
1412 74, 74, 74, 75, 75, 76, 77, 77, 77, 77,
1413 77, 78, 78, 78, 79, 80, 81, 82, 82, 82,
1414 83, 83, 84, 84, 84, 84, 85, 85, 85, 85,
1415 85, 85, 86, 86, 87, 88, 88, 89, 89, 89,
1416 90, 90, 90, 90, 90, 90, 91, 91, 92, 92,
1417 92, 93, 93, 94, 94, 94, 95, 95, 95, 95,
1418 95, 96, 96, 96, 96, 96, 97, 97, 97, 98,
1419 98, 98, 98
1420 };
1421
1422 /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */
1423 static const yytype_int8 yyr2[] =
1424 {
1425 0, 2, 2, 1, 2, 2, 1, 1, 2, 2,
1426 2, 3, 3, 3, 3, 2, 3, 3, 2, 3,
1427 3, 2, 3, 3, 2, 3, 3, 2, 3, 3,
1428 2, 3, 3, 2, 3, 3, 2, 3, 3, 2,
1429 3, 3, 2, 3, 3, 2, 3, 3, 2, 3,
1430 3, 2, 2, 1, 1, 1, 1, 2, 1, 2,
1431 1, 1, 2, 1, 1, 1, 1, 5, 5, 1,
1432 1, 1, 1, 1, 1, 1, 6, 6, 7, 7,
1433 10, 10, 9, 9, 7, 7, 5, 5, 6, 6,
1434 7, 7, 10, 10, 9, 9, 6, 7, 6, 5,
1435 6, 3, 5, 1, 2, 3, 2, 3, 3, 4,
1436 2, 5, 7, 6, 3, 1, 3, 4, 6, 5,
1437 1, 2, 4, 4, 5, 5, 2, 3, 2, 3,
1438 2, 3, 1, 3, 2, 1, 2, 3, 3, 3,
1439 4, 4, 4, 4, 4, 1, 1, 1, 1, 1,
1440 1, 0, 2, 1, 2, 2, 4, 4, 3, 3,
1441 1, 1, 2, 2, 2, 2, 4, 4, 1, 1,
1442 2, 2, 3
1443 };
1444
1445
1446 enum { YYENOMEM = -2 };
1447
1448 #define yyerrok (yyerrstatus = 0)
1449 #define yyclearin (yychar = YYEMPTY)
1450
1451 #define YYACCEPT goto yyacceptlab
1452 #define YYABORT goto yyabortlab
1453 #define YYERROR goto yyerrorlab
1454
1455
1456 #define YYRECOVERING() (!!yyerrstatus)
1457
1458 #define YYBACKUP(Token, Value) \
1459 do \
1460 if (yychar == YYEMPTY) \
1461 { \
1462 yychar = (Token); \
1463 yylval = (Value); \
1464 YYPOPSTACK (yylen); \
1465 yystate = *yyssp; \
1466 goto yybackup; \
1467 } \
1468 else \
1469 { \
1470 yyerror (YY_("syntax error: cannot back up")); \
1471 YYERROR; \
1472 } \
1473 while (0)
1474
1475 /* Backward compatibility with an undocumented macro.
1476 Use YYerror or YYUNDEF. */
1477 #define YYERRCODE YYUNDEF
1478
1479
1480 /* Enable debugging if requested. */
1481 #if YYDEBUG
1482
1483 # ifndef YYFPRINTF
1484 # include <stdio.h> /* INFRINGES ON USER NAME SPACE */
1485 # define YYFPRINTF fprintf
1486 # endif
1487
1488 # define YYDPRINTF(Args) \
1489 do { \
1490 if (yydebug) \
1491 YYFPRINTF Args; \
1492 } while (0)
1493
1494 /* This macro is provided for backward compatibility. */
1495 # ifndef YY_LOCATION_PRINT
1496 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
1497 # endif
1498
1499
1500 # define YY_SYMBOL_PRINT(Title, Kind, Value, Location) \
1501 do { \
1502 if (yydebug) \
1503 { \
1504 YYFPRINTF (stderr, "%s ", Title); \
1505 yy_symbol_print (stderr, \
1506 Kind, Value); \
1507 YYFPRINTF (stderr, "\n"); \
1508 } \
1509 } while (0)
1510
1511
1512 /*-----------------------------------.
1513 | Print this symbol's value on YYO. |
1514 `-----------------------------------*/
1515
1516 static void
1517 yy_symbol_value_print (FILE *yyo,
1518 yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep)
1519 {
1520 FILE *yyoutput = yyo;
1521 YYUSE (yyoutput);
1522 if (!yyvaluep)
1523 return;
1524 # ifdef YYPRINT
1525 if (yykind < YYNTOKENS)
1526 YYPRINT (yyo, yytoknum[yykind], *yyvaluep);
1527 # endif
1528 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1529 YYUSE (yykind);
1530 YY_IGNORE_MAYBE_UNINITIALIZED_END
1531 }
1532
1533
1534 /*---------------------------.
1535 | Print this symbol on YYO. |
1536 `---------------------------*/
1537
1538 static void
1539 yy_symbol_print (FILE *yyo,
1540 yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep)
1541 {
1542 YYFPRINTF (yyo, "%s %s (",
1543 yykind < YYNTOKENS ? "token" : "nterm", yysymbol_name (yykind));
1544
1545 yy_symbol_value_print (yyo, yykind, yyvaluep);
1546 YYFPRINTF (yyo, ")");
1547 }
1548
1549 /*------------------------------------------------------------------.
1550 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
1551 | TOP (included). |
1552 `------------------------------------------------------------------*/
1553
1554 static void
1555 yy_stack_print (yy_state_t *yybottom, yy_state_t *yytop)
1556 {
1557 YYFPRINTF (stderr, "Stack now");
1558 for (; yybottom <= yytop; yybottom++)
1559 {
1560 int yybot = *yybottom;
1561 YYFPRINTF (stderr, " %d", yybot);
1562 }
1563 YYFPRINTF (stderr, "\n");
1564 }
1565
1566 # define YY_STACK_PRINT(Bottom, Top) \
1567 do { \
1568 if (yydebug) \
1569 yy_stack_print ((Bottom), (Top)); \
1570 } while (0)
1571
1572
1573 /*------------------------------------------------.
1574 | Report that the YYRULE is going to be reduced. |
1575 `------------------------------------------------*/
1576
1577 static void
1578 yy_reduce_print (yy_state_t *yyssp, YYSTYPE *yyvsp,
1579 int yyrule)
1580 {
1581 int yylno = yyrline[yyrule];
1582 int yynrhs = yyr2[yyrule];
1583 int yyi;
1584 YYFPRINTF (stderr, "Reducing stack by rule %d (line %d):\n",
1585 yyrule - 1, yylno);
1586 /* The symbols being reduced. */
1587 for (yyi = 0; yyi < yynrhs; yyi++)
1588 {
1589 YYFPRINTF (stderr, " $%d = ", yyi + 1);
1590 yy_symbol_print (stderr,
1591 YY_ACCESSING_SYMBOL (+yyssp[yyi + 1 - yynrhs]),
1592 &yyvsp[(yyi + 1) - (yynrhs)]);
1593 YYFPRINTF (stderr, "\n");
1594 }
1595 }
1596
1597 # define YY_REDUCE_PRINT(Rule) \
1598 do { \
1599 if (yydebug) \
1600 yy_reduce_print (yyssp, yyvsp, Rule); \
1601 } while (0)
1602
1603 /* Nonzero means print parse trace. It is left uninitialized so that
1604 multiple parsers can coexist. */
1605 int yydebug;
1606 #else /* !YYDEBUG */
1607 # define YYDPRINTF(Args) ((void) 0)
1608 # define YY_SYMBOL_PRINT(Title, Kind, Value, Location)
1609 # define YY_STACK_PRINT(Bottom, Top)
1610 # define YY_REDUCE_PRINT(Rule)
1611 #endif /* !YYDEBUG */
1612
1613
1614 /* YYINITDEPTH -- initial size of the parser's stacks. */
1615 #ifndef YYINITDEPTH
1616 # define YYINITDEPTH 200
1617 #endif
1618
1619 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
1620 if the built-in stack extension method is used).
1621
1622 Do not make this value too large; the results are undefined if
1623 YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
1624 evaluated with infinite-precision integer arithmetic. */
1625
1626 #ifndef YYMAXDEPTH
1627 # define YYMAXDEPTH 10000
1628 #endif
1629
1630
1631
1632
1633
1634
1635 /*-----------------------------------------------.
1636 | Release the memory associated to this symbol. |
1637 `-----------------------------------------------*/
1638
1639 static void
1640 yydestruct (const char *yymsg,
1641 yysymbol_kind_t yykind, YYSTYPE *yyvaluep)
1642 {
1643 YYUSE (yyvaluep);
1644 if (!yymsg)
1645 yymsg = "Deleting";
1646 YY_SYMBOL_PRINT (yymsg, yykind, yyvaluep, yylocationp);
1647
1648 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1649 YYUSE (yykind);
1650 YY_IGNORE_MAYBE_UNINITIALIZED_END
1651 }
1652
1653
1654 /* The lookahead symbol. */
1655 int yychar;
1656
1657 /* The semantic value of the lookahead symbol. */
1658 YYSTYPE yylval;
1659 /* Number of syntax errors so far. */
1660 int yynerrs;
1661
1662
1663
1664
1665 /*----------.
1666 | yyparse. |
1667 `----------*/
1668
1669 int
1670 yyparse (void)
1671 {
1672 yy_state_fast_t yystate;
1673 /* Number of tokens to shift before error messages enabled. */
1674 int yyerrstatus;
1675
1676 /* The stacks and their tools:
1677 'yyss': related to states.
1678 'yyvs': related to semantic values.
1679
1680 Refer to the stacks through separate pointers, to allow yyoverflow
1681 to reallocate them elsewhere. */
1682
1683 /* Their size. */
1684 YYPTRDIFF_T yystacksize;
1685
1686 /* The state stack. */
1687 yy_state_t yyssa[YYINITDEPTH];
1688 yy_state_t *yyss;
1689 yy_state_t *yyssp;
1690
1691 /* The semantic value stack. */
1692 YYSTYPE yyvsa[YYINITDEPTH];
1693 YYSTYPE *yyvs;
1694 YYSTYPE *yyvsp;
1695
1696 int yyn;
1697 /* The return value of yyparse. */
1698 int yyresult;
1699 /* Lookahead token as an internal (translated) token number. */
1700 yysymbol_kind_t yytoken = YYSYMBOL_YYEMPTY;
1701 /* The variables used to return semantic value and location from the
1702 action routines. */
1703 YYSTYPE yyval;
1704
1705
1706
1707 #define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N))
1708
1709 /* The number of symbols on the RHS of the reduced rule.
1710 Keep to zero when no symbol should be popped. */
1711 int yylen = 0;
1712
1713 yynerrs = 0;
1714 yystate = 0;
1715 yyerrstatus = 0;
1716
1717 yystacksize = YYINITDEPTH;
1718 yyssp = yyss = yyssa;
1719 yyvsp = yyvs = yyvsa;
1720
1721
1722 YYDPRINTF ((stderr, "Starting parse\n"));
1723
1724 yychar = YYEMPTY; /* Cause a token to be read. */
1725 goto yysetstate;
1726
1727
1728 /*------------------------------------------------------------.
1729 | yynewstate -- push a new state, which is found in yystate. |
1730 `------------------------------------------------------------*/
1731 yynewstate:
1732 /* In all cases, when you get here, the value and location stacks
1733 have just been pushed. So pushing a state here evens the stacks. */
1734 yyssp++;
1735
1736
1737 /*--------------------------------------------------------------------.
1738 | yysetstate -- set current state (the top of the stack) to yystate. |
1739 `--------------------------------------------------------------------*/
1740 yysetstate:
1741 YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1742 YY_ASSERT (0 <= yystate && yystate < YYNSTATES);
1743 YY_IGNORE_USELESS_CAST_BEGIN
1744 *yyssp = YY_CAST (yy_state_t, yystate);
1745 YY_IGNORE_USELESS_CAST_END
1746 YY_STACK_PRINT (yyss, yyssp);
1747
1748 if (yyss + yystacksize - 1 <= yyssp)
1749 #if !defined yyoverflow && !defined YYSTACK_RELOCATE
1750 goto yyexhaustedlab;
1751 #else
1752 {
1753 /* Get the current used size of the three stacks, in elements. */
1754 YYPTRDIFF_T yysize = yyssp - yyss + 1;
1755
1756 # if defined yyoverflow
1757 {
1758 /* Give user a chance to reallocate the stack. Use copies of
1759 these so that the &'s don't force the real ones into
1760 memory. */
1761 yy_state_t *yyss1 = yyss;
1762 YYSTYPE *yyvs1 = yyvs;
1763
1764 /* Each stack pointer address is followed by the size of the
1765 data in use in that stack, in bytes. This used to be a
1766 conditional around just the two extra args, but that might
1767 be undefined if yyoverflow is a macro. */
1768 yyoverflow (YY_("memory exhausted"),
1769 &yyss1, yysize * YYSIZEOF (*yyssp),
1770 &yyvs1, yysize * YYSIZEOF (*yyvsp),
1771 &yystacksize);
1772 yyss = yyss1;
1773 yyvs = yyvs1;
1774 }
1775 # else /* defined YYSTACK_RELOCATE */
1776 /* Extend the stack our own way. */
1777 if (YYMAXDEPTH <= yystacksize)
1778 goto yyexhaustedlab;
1779 yystacksize *= 2;
1780 if (YYMAXDEPTH < yystacksize)
1781 yystacksize = YYMAXDEPTH;
1782
1783 {
1784 yy_state_t *yyss1 = yyss;
1785 union yyalloc *yyptr =
1786 YY_CAST (union yyalloc *,
1787 YYSTACK_ALLOC (YY_CAST (YYSIZE_T, YYSTACK_BYTES (yystacksize))));
1788 if (! yyptr)
1789 goto yyexhaustedlab;
1790 YYSTACK_RELOCATE (yyss_alloc, yyss);
1791 YYSTACK_RELOCATE (yyvs_alloc, yyvs);
1792 # undef YYSTACK_RELOCATE
1793 if (yyss1 != yyssa)
1794 YYSTACK_FREE (yyss1);
1795 }
1796 # endif
1797
1798 yyssp = yyss + yysize - 1;
1799 yyvsp = yyvs + yysize - 1;
1800
1801 YY_IGNORE_USELESS_CAST_BEGIN
1802 YYDPRINTF ((stderr, "Stack size increased to %ld\n",
1803 YY_CAST (long, yystacksize)));
1804 YY_IGNORE_USELESS_CAST_END
1805
1806 if (yyss + yystacksize - 1 <= yyssp)
1807 YYABORT;
1808 }
1809 #endif /* !defined yyoverflow && !defined YYSTACK_RELOCATE */
1810
1811 if (yystate == YYFINAL)
1812 YYACCEPT;
1813
1814 goto yybackup;
1815
1816
1817 /*-----------.
1818 | yybackup. |
1819 `-----------*/
1820 yybackup:
1821 /* Do appropriate processing given the current state. Read a
1822 lookahead token if we need one and don't already have one. */
1823
1824 /* First try to decide what to do without reference to lookahead token. */
1825 yyn = yypact[yystate];
1826 if (yypact_value_is_default (yyn))
1827 goto yydefault;
1828
1829 /* Not known => get a lookahead token if don't already have one. */
1830
1831 /* YYCHAR is either empty, or end-of-input, or a valid lookahead. */
1832 if (yychar == YYEMPTY)
1833 {
1834 YYDPRINTF ((stderr, "Reading a token\n"));
1835 yychar = yylex ();
1836 }
1837
1838 if (yychar <= YYEOF)
1839 {
1840 yychar = YYEOF;
1841 yytoken = YYSYMBOL_YYEOF;
1842 YYDPRINTF ((stderr, "Now at end of input.\n"));
1843 }
1844 else if (yychar == YYerror)
1845 {
1846 /* The scanner already issued an error message, process directly
1847 to error recovery. But do not keep the error token as
1848 lookahead, it is too special and may lead us to an endless
1849 loop in error recovery. */
1850 yychar = YYUNDEF;
1851 yytoken = YYSYMBOL_YYerror;
1852 goto yyerrlab1;
1853 }
1854 else
1855 {
1856 yytoken = YYTRANSLATE (yychar);
1857 YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
1858 }
1859
1860 /* If the proper action on seeing token YYTOKEN is to reduce or to
1861 detect an error, take that action. */
1862 yyn += yytoken;
1863 if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
1864 goto yydefault;
1865 yyn = yytable[yyn];
1866 if (yyn <= 0)
1867 {
1868 if (yytable_value_is_error (yyn))
1869 goto yyerrlab;
1870 yyn = -yyn;
1871 goto yyreduce;
1872 }
1873
1874 /* Count tokens shifted since error; after three, turn off error
1875 status. */
1876 if (yyerrstatus)
1877 yyerrstatus--;
1878
1879 /* Shift the lookahead token. */
1880 YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
1881 yystate = yyn;
1882 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1883 *++yyvsp = yylval;
1884 YY_IGNORE_MAYBE_UNINITIALIZED_END
1885
1886 /* Discard the shifted token. */
1887 yychar = YYEMPTY;
1888 goto yynewstate;
1889
1890
1891 /*-----------------------------------------------------------.
1892 | yydefault -- do the default action for the current state. |
1893 `-----------------------------------------------------------*/
1894 yydefault:
1895 yyn = yydefact[yystate];
1896 if (yyn == 0)
1897 goto yyerrlab;
1898 goto yyreduce;
1899
1900
1901 /*-----------------------------.
1902 | yyreduce -- do a reduction. |
1903 `-----------------------------*/
1904 yyreduce:
1905 /* yyn is the number of a rule to reduce with. */
1906 yylen = yyr2[yyn];
1907
1908 /* If YYLEN is nonzero, implement the default value of the action:
1909 '$$ = $1'.
1910
1911 Otherwise, the following line sets YYVAL to garbage.
1912 This behavior is undocumented and Bison
1913 users should not rely upon it. Assigning to YYVAL
1914 unconditionally makes the parser a bit smaller, and it avoids a
1915 GCC warning that YYVAL may be used uninitialized. */
1916 yyval = yyvsp[1-yylen];
1917
1918
1919 YY_REDUCE_PRINT (yyn);
1920 switch (yyn)
1921 {
1922 case 2:
1923 #line 382 "/Users/chet/src/bash/src/parse.y"
1924 {
1925 /* Case of regular command. Discard the error
1926 safety net,and return the command just parsed. */
1927 global_command = (yyvsp[-1].command);
1928 eof_encountered = 0;
1929 /* discard_parser_constructs (0); */
1930 if (parser_state & PST_CMDSUBST)
1931 parser_state |= PST_EOFTOKEN;
1932 YYACCEPT;
1933 }
1934 #line 1935 "y.tab.c"
1935 break;
1936
1937 case 3:
1938 #line 393 "/Users/chet/src/bash/src/parse.y"
1939 {
1940 /* Case of regular command, but not a very
1941 interesting one. Return a NULL command. */
1942 global_command = (COMMAND *)NULL;
1943 if (parser_state & PST_CMDSUBST)
1944 parser_state |= PST_EOFTOKEN;
1945 YYACCEPT;
1946 }
1947 #line 1948 "y.tab.c"
1948 break;
1949
1950 case 4:
1951 #line 402 "/Users/chet/src/bash/src/parse.y"
1952 {
1953 /* Error during parsing. Return NULL command. */
1954 global_command = (COMMAND *)NULL;
1955 eof_encountered = 0;
1956 /* discard_parser_constructs (1); */
1957 if (interactive && parse_and_execute_level == 0)
1958 {
1959 YYACCEPT;
1960 }
1961 else
1962 {
1963 YYABORT;
1964 }
1965 }
1966 #line 1967 "y.tab.c"
1967 break;
1968
1969 case 5:
1970 #line 417 "/Users/chet/src/bash/src/parse.y"
1971 {
1972 /* EOF after an error. Do ignoreeof or not. Really
1973 only interesting in non-interactive shells */
1974 global_command = (COMMAND *)NULL;
1975 if (last_command_exit_value == 0)
1976 last_command_exit_value = EX_BADUSAGE; /* force error return */
1977 if (interactive && parse_and_execute_level == 0)
1978 {
1979 handle_eof_input_unit ();
1980 YYACCEPT;
1981 }
1982 else
1983 {
1984 YYABORT;
1985 }
1986 }
1987 #line 1988 "y.tab.c"
1988 break;
1989
1990 case 6:
1991 #line 434 "/Users/chet/src/bash/src/parse.y"
1992 {
1993 /* Case of EOF seen by itself. Do ignoreeof or
1994 not. */
1995 global_command = (COMMAND *)NULL;
1996 handle_eof_input_unit ();
1997 YYACCEPT;
1998 }
1999 #line 2000 "y.tab.c"
2000 break;
2001
2002 case 7:
2003 #line 444 "/Users/chet/src/bash/src/parse.y"
2004 { (yyval.word_list) = make_word_list ((yyvsp[0].word), (WORD_LIST *)NULL); }
2005 #line 2006 "y.tab.c"
2006 break;
2007
2008 case 8:
2009 #line 446 "/Users/chet/src/bash/src/parse.y"
2010 { (yyval.word_list) = make_word_list ((yyvsp[0].word), (yyvsp[-1].word_list)); }
2011 #line 2012 "y.tab.c"
2012 break;
2013
2014 case 9:
2015 #line 450 "/Users/chet/src/bash/src/parse.y"
2016 {
2017 source.dest = 1;
2018 redir.filename = (yyvsp[0].word);
2019 (yyval.redirect) = make_redirection (source, r_output_direction, redir, 0);
2020 }
2021 #line 2022 "y.tab.c"
2022 break;
2023
2024 case 10:
2025 #line 456 "/Users/chet/src/bash/src/parse.y"
2026 {
2027 source.dest = 0;
2028 redir.filename = (yyvsp[0].word);
2029 (yyval.redirect) = make_redirection (source, r_input_direction, redir, 0);
2030 }
2031 #line 2032 "y.tab.c"
2032 break;
2033
2034 case 11:
2035 #line 462 "/Users/chet/src/bash/src/parse.y"
2036 {
2037 source.dest = (yyvsp[-2].number);
2038 redir.filename = (yyvsp[0].word);
2039 (yyval.redirect) = make_redirection (source, r_output_direction, redir, 0);
2040 }
2041 #line 2042 "y.tab.c"
2042 break;
2043
2044 case 12:
2045 #line 468 "/Users/chet/src/bash/src/parse.y"
2046 {
2047 source.dest = (yyvsp[-2].number);
2048 redir.filename = (yyvsp[0].word);
2049 (yyval.redirect) = make_redirection (source, r_input_direction, redir, 0);
2050 }
2051 #line 2052 "y.tab.c"
2052 break;
2053
2054 case 13:
2055 #line 474 "/Users/chet/src/bash/src/parse.y"
2056 {
2057 source.filename = (yyvsp[-2].word);
2058 redir.filename = (yyvsp[0].word);
2059 (yyval.redirect) = make_redirection (source, r_output_direction, redir, REDIR_VARASSIGN);
2060 }
2061 #line 2062 "y.tab.c"
2062 break;
2063
2064 case 14:
2065 #line 480 "/Users/chet/src/bash/src/parse.y"
2066 {
2067 source.filename = (yyvsp[-2].word);
2068 redir.filename = (yyvsp[0].word);
2069 (yyval.redirect) = make_redirection (source, r_input_direction, redir, REDIR_VARASSIGN);
2070 }
2071 #line 2072 "y.tab.c"
2072 break;
2073
2074 case 15:
2075 #line 486 "/Users/chet/src/bash/src/parse.y"
2076 {
2077 source.dest = 1;
2078 redir.filename = (yyvsp[0].word);
2079 (yyval.redirect) = make_redirection (source, r_appending_to, redir, 0);
2080 }
2081 #line 2082 "y.tab.c"
2082 break;
2083
2084 case 16:
2085 #line 492 "/Users/chet/src/bash/src/parse.y"
2086 {
2087 source.dest = (yyvsp[-2].number);
2088 redir.filename = (yyvsp[0].word);
2089 (yyval.redirect) = make_redirection (source, r_appending_to, redir, 0);
2090 }
2091 #line 2092 "y.tab.c"
2092 break;
2093
2094 case 17:
2095 #line 498 "/Users/chet/src/bash/src/parse.y"
2096 {
2097 source.filename = (yyvsp[-2].word);
2098 redir.filename = (yyvsp[0].word);
2099 (yyval.redirect) = make_redirection (source, r_appending_to, redir, REDIR_VARASSIGN);
2100 }
2101 #line 2102 "y.tab.c"
2102 break;
2103
2104 case 18:
2105 #line 504 "/Users/chet/src/bash/src/parse.y"
2106 {
2107 source.dest = 1;
2108 redir.filename = (yyvsp[0].word);
2109 (yyval.redirect) = make_redirection (source, r_output_force, redir, 0);
2110 }
2111 #line 2112 "y.tab.c"
2112 break;
2113
2114 case 19:
2115 #line 510 "/Users/chet/src/bash/src/parse.y"
2116 {
2117 source.dest = (yyvsp[-2].number);
2118 redir.filename = (yyvsp[0].word);
2119 (yyval.redirect) = make_redirection (source, r_output_force, redir, 0);
2120 }
2121 #line 2122 "y.tab.c"
2122 break;
2123
2124 case 20:
2125 #line 516 "/Users/chet/src/bash/src/parse.y"
2126 {
2127 source.filename = (yyvsp[-2].word);
2128 redir.filename = (yyvsp[0].word);
2129 (yyval.redirect) = make_redirection (source, r_output_force, redir, REDIR_VARASSIGN);
2130 }
2131 #line 2132 "y.tab.c"
2132 break;
2133
2134 case 21:
2135 #line 522 "/Users/chet/src/bash/src/parse.y"
2136 {
2137 source.dest = 0;
2138 redir.filename = (yyvsp[0].word);
2139 (yyval.redirect) = make_redirection (source, r_input_output, redir, 0);
2140 }
2141 #line 2142 "y.tab.c"
2142 break;
2143
2144 case 22:
2145 #line 528 "/Users/chet/src/bash/src/parse.y"
2146 {
2147 source.dest = (yyvsp[-2].number);
2148 redir.filename = (yyvsp[0].word);
2149 (yyval.redirect) = make_redirection (source, r_input_output, redir, 0);
2150 }
2151 #line 2152 "y.tab.c"
2152 break;
2153
2154 case 23:
2155 #line 534 "/Users/chet/src/bash/src/parse.y"
2156 {
2157 source.filename = (yyvsp[-2].word);
2158 redir.filename = (yyvsp[0].word);
2159 (yyval.redirect) = make_redirection (source, r_input_output, redir, REDIR_VARASSIGN);
2160 }
2161 #line 2162 "y.tab.c"
2162 break;
2163
2164 case 24:
2165 #line 540 "/Users/chet/src/bash/src/parse.y"
2166 {
2167 source.dest = 0;
2168 redir.filename = (yyvsp[0].word);
2169 (yyval.redirect) = make_redirection (source, r_reading_until, redir, 0);
2170 push_heredoc ((yyval.redirect));
2171 }
2172 #line 2173 "y.tab.c"
2173 break;
2174
2175 case 25:
2176 #line 547 "/Users/chet/src/bash/src/parse.y"
2177 {
2178 source.dest = (yyvsp[-2].number);
2179 redir.filename = (yyvsp[0].word);
2180 (yyval.redirect) = make_redirection (source, r_reading_until, redir, 0);
2181 push_heredoc ((yyval.redirect));
2182 }
2183 #line 2184 "y.tab.c"
2184 break;
2185
2186 case 26:
2187 #line 554 "/Users/chet/src/bash/src/parse.y"
2188 {
2189 source.filename = (yyvsp[-2].word);
2190 redir.filename = (yyvsp[0].word);
2191 (yyval.redirect) = make_redirection (source, r_reading_until, redir, REDIR_VARASSIGN);
2192 push_heredoc ((yyval.redirect));
2193 }
2194 #line 2195 "y.tab.c"
2195 break;
2196
2197 case 27:
2198 #line 561 "/Users/chet/src/bash/src/parse.y"
2199 {
2200 source.dest = 0;
2201 redir.filename = (yyvsp[0].word);
2202 (yyval.redirect) = make_redirection (source, r_deblank_reading_until, redir, 0);
2203 push_heredoc ((yyval.redirect));
2204 }
2205 #line 2206 "y.tab.c"
2206 break;
2207
2208 case 28:
2209 #line 568 "/Users/chet/src/bash/src/parse.y"
2210 {
2211 source.dest = (yyvsp[-2].number);
2212 redir.filename = (yyvsp[0].word);
2213 (yyval.redirect) = make_redirection (source, r_deblank_reading_until, redir, 0);
2214 push_heredoc ((yyval.redirect));
2215 }
2216 #line 2217 "y.tab.c"
2217 break;
2218
2219 case 29:
2220 #line 575 "/Users/chet/src/bash/src/parse.y"
2221 {
2222 source.filename = (yyvsp[-2].word);
2223 redir.filename = (yyvsp[0].word);
2224 (yyval.redirect) = make_redirection (source, r_deblank_reading_until, redir, REDIR_VARASSIGN);
2225 push_heredoc ((yyval.redirect));
2226 }
2227 #line 2228 "y.tab.c"
2228 break;
2229
2230 case 30:
2231 #line 582 "/Users/chet/src/bash/src/parse.y"
2232 {
2233 source.dest = 0;
2234 redir.filename = (yyvsp[0].word);
2235 (yyval.redirect) = make_redirection (source, r_reading_string, redir, 0);
2236 }
2237 #line 2238 "y.tab.c"
2238 break;
2239
2240 case 31:
2241 #line 588 "/Users/chet/src/bash/src/parse.y"
2242 {
2243 source.dest = (yyvsp[-2].number);
2244 redir.filename = (yyvsp[0].word);
2245 (yyval.redirect) = make_redirection (source, r_reading_string, redir, 0);
2246 }
2247 #line 2248 "y.tab.c"
2248 break;
2249
2250 case 32:
2251 #line 594 "/Users/chet/src/bash/src/parse.y"
2252 {
2253 source.filename = (yyvsp[-2].word);
2254 redir.filename = (yyvsp[0].word);
2255 (yyval.redirect) = make_redirection (source, r_reading_string, redir, REDIR_VARASSIGN);
2256 }
2257 #line 2258 "y.tab.c"
2258 break;
2259
2260 case 33:
2261 #line 600 "/Users/chet/src/bash/src/parse.y"
2262 {
2263 source.dest = 0;
2264 redir.dest = (yyvsp[0].number);
2265 (yyval.redirect) = make_redirection (source, r_duplicating_input, redir, 0);
2266 }
2267 #line 2268 "y.tab.c"
2268 break;
2269
2270 case 34:
2271 #line 606 "/Users/chet/src/bash/src/parse.y"
2272 {
2273 source.dest = (yyvsp[-2].number);
2274 redir.dest = (yyvsp[0].number);
2275 (yyval.redirect) = make_redirection (source, r_duplicating_input, redir, 0);
2276 }
2277 #line 2278 "y.tab.c"
2278 break;
2279
2280 case 35:
2281 #line 612 "/Users/chet/src/bash/src/parse.y"
2282 {
2283 source.filename = (yyvsp[-2].word);
2284 redir.dest = (yyvsp[0].number);
2285 (yyval.redirect) = make_redirection (source, r_duplicating_input, redir, REDIR_VARASSIGN);
2286 }
2287 #line 2288 "y.tab.c"
2288 break;
2289
2290 case 36:
2291 #line 618 "/Users/chet/src/bash/src/parse.y"
2292 {
2293 source.dest = 1;
2294 redir.dest = (yyvsp[0].number);
2295 (yyval.redirect) = make_redirection (source, r_duplicating_output, redir, 0);
2296 }
2297 #line 2298 "y.tab.c"
2298 break;
2299
2300 case 37:
2301 #line 624 "/Users/chet/src/bash/src/parse.y"
2302 {
2303 source.dest = (yyvsp[-2].number);
2304 redir.dest = (yyvsp[0].number);
2305 (yyval.redirect) = make_redirection (source, r_duplicating_output, redir, 0);
2306 }
2307 #line 2308 "y.tab.c"
2308 break;
2309
2310 case 38:
2311 #line 630 "/Users/chet/src/bash/src/parse.y"
2312 {
2313 source.filename = (yyvsp[-2].word);
2314 redir.dest = (yyvsp[0].number);
2315 (yyval.redirect) = make_redirection (source, r_duplicating_output, redir, REDIR_VARASSIGN);
2316 }
2317 #line 2318 "y.tab.c"
2318 break;
2319
2320 case 39:
2321 #line 636 "/Users/chet/src/bash/src/parse.y"
2322 {
2323 source.dest = 0;
2324 redir.filename = (yyvsp[0].word);
2325 (yyval.redirect) = make_redirection (source, r_duplicating_input_word, redir, 0);
2326 }
2327 #line 2328 "y.tab.c"
2328 break;
2329
2330 case 40:
2331 #line 642 "/Users/chet/src/bash/src/parse.y"
2332 {
2333 source.dest = (yyvsp[-2].number);
2334 redir.filename = (yyvsp[0].word);
2335 (yyval.redirect) = make_redirection (source, r_duplicating_input_word, redir, 0);
2336 }
2337 #line 2338 "y.tab.c"
2338 break;
2339
2340 case 41:
2341 #line 648 "/Users/chet/src/bash/src/parse.y"
2342 {
2343 source.filename = (yyvsp[-2].word);
2344 redir.filename = (yyvsp[0].word);
2345 (yyval.redirect) = make_redirection (source, r_duplicating_input_word, redir, REDIR_VARASSIGN);
2346 }
2347 #line 2348 "y.tab.c"
2348 break;
2349
2350 case 42:
2351 #line 654 "/Users/chet/src/bash/src/parse.y"
2352 {
2353 source.dest = 1;
2354 redir.filename = (yyvsp[0].word);
2355 (yyval.redirect) = make_redirection (source, r_duplicating_output_word, redir, 0);
2356 }
2357 #line 2358 "y.tab.c"
2358 break;
2359
2360 case 43:
2361 #line 660 "/Users/chet/src/bash/src/parse.y"
2362 {
2363 source.dest = (yyvsp[-2].number);
2364 redir.filename = (yyvsp[0].word);
2365 (yyval.redirect) = make_redirection (source, r_duplicating_output_word, redir, 0);
2366 }
2367 #line 2368 "y.tab.c"
2368 break;
2369
2370 case 44:
2371 #line 666 "/Users/chet/src/bash/src/parse.y"
2372 {
2373 source.filename = (yyvsp[-2].word);
2374 redir.filename = (yyvsp[0].word);
2375 (yyval.redirect) = make_redirection (source, r_duplicating_output_word, redir, REDIR_VARASSIGN);
2376 }
2377 #line 2378 "y.tab.c"
2378 break;
2379
2380 case 45:
2381 #line 672 "/Users/chet/src/bash/src/parse.y"
2382 {
2383 source.dest = 1;
2384 redir.dest = 0;
2385 (yyval.redirect) = make_redirection (source, r_close_this, redir, 0);
2386 }
2387 #line 2388 "y.tab.c"
2388 break;
2389
2390 case 46:
2391 #line 678 "/Users/chet/src/bash/src/parse.y"
2392 {
2393 source.dest = (yyvsp[-2].number);
2394 redir.dest = 0;
2395 (yyval.redirect) = make_redirection (source, r_close_this, redir, 0);
2396 }
2397 #line 2398 "y.tab.c"
2398 break;
2399
2400 case 47:
2401 #line 684 "/Users/chet/src/bash/src/parse.y"
2402 {
2403 source.filename = (yyvsp[-2].word);
2404 redir.dest = 0;
2405 (yyval.redirect) = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
2406 }
2407 #line 2408 "y.tab.c"
2408 break;
2409
2410 case 48:
2411 #line 690 "/Users/chet/src/bash/src/parse.y"
2412 {
2413 source.dest = 0;
2414 redir.dest = 0;
2415 (yyval.redirect) = make_redirection (source, r_close_this, redir, 0);
2416 }
2417 #line 2418 "y.tab.c"
2418 break;
2419
2420 case 49:
2421 #line 696 "/Users/chet/src/bash/src/parse.y"
2422 {
2423 source.dest = (yyvsp[-2].number);
2424 redir.dest = 0;
2425 (yyval.redirect) = make_redirection (source, r_close_this, redir, 0);
2426 }
2427 #line 2428 "y.tab.c"
2428 break;
2429
2430 case 50:
2431 #line 702 "/Users/chet/src/bash/src/parse.y"
2432 {
2433 source.filename = (yyvsp[-2].word);
2434 redir.dest = 0;
2435 (yyval.redirect) = make_redirection (source, r_close_this, redir, REDIR_VARASSIGN);
2436 }
2437 #line 2438 "y.tab.c"
2438 break;
2439
2440 case 51:
2441 #line 708 "/Users/chet/src/bash/src/parse.y"
2442 {
2443 source.dest = 1;
2444 redir.filename = (yyvsp[0].word);
2445 (yyval.redirect) = make_redirection (source, r_err_and_out, redir, 0);
2446 }
2447 #line 2448 "y.tab.c"
2448 break;
2449
2450 case 52:
2451 #line 714 "/Users/chet/src/bash/src/parse.y"
2452 {
2453 source.dest = 1;
2454 redir.filename = (yyvsp[0].word);
2455 (yyval.redirect) = make_redirection (source, r_append_err_and_out, redir, 0);
2456 }
2457 #line 2458 "y.tab.c"
2458 break;
2459
2460 case 53:
2461 #line 722 "/Users/chet/src/bash/src/parse.y"
2462 { (yyval.element).word = (yyvsp[0].word); (yyval.element).redirect = 0; }
2463 #line 2464 "y.tab.c"
2464 break;
2465
2466 case 54:
2467 #line 724 "/Users/chet/src/bash/src/parse.y"
2468 { (yyval.element).word = (yyvsp[0].word); (yyval.element).redirect = 0; }
2469 #line 2470 "y.tab.c"
2470 break;
2471
2472 case 55:
2473 #line 726 "/Users/chet/src/bash/src/parse.y"
2474 { (yyval.element).redirect = (yyvsp[0].redirect); (yyval.element).word = 0; }
2475 #line 2476 "y.tab.c"
2476 break;
2477
2478 case 56:
2479 #line 730 "/Users/chet/src/bash/src/parse.y"
2480 {
2481 (yyval.redirect) = (yyvsp[0].redirect);
2482 }
2483 #line 2484 "y.tab.c"
2484 break;
2485
2486 case 57:
2487 #line 734 "/Users/chet/src/bash/src/parse.y"
2488 {
2489 register REDIRECT *t;
2490
2491 for (t = (yyvsp[-1].redirect); t->next; t = t->next)
2492 ;
2493 t->next = (yyvsp[0].redirect);
2494 (yyval.redirect) = (yyvsp[-1].redirect);
2495 }
2496 #line 2497 "y.tab.c"
2497 break;
2498
2499 case 58:
2500 #line 745 "/Users/chet/src/bash/src/parse.y"
2501 { (yyval.command) = make_simple_command ((yyvsp[0].element), (COMMAND *)NULL); }
2502 #line 2503 "y.tab.c"
2503 break;
2504
2505 case 59:
2506 #line 747 "/Users/chet/src/bash/src/parse.y"
2507 { (yyval.command) = make_simple_command ((yyvsp[0].element), (yyvsp[-1].command)); }
2508 #line 2509 "y.tab.c"
2509 break;
2510
2511 case 60:
2512 #line 751 "/Users/chet/src/bash/src/parse.y"
2513 { (yyval.command) = clean_simple_command ((yyvsp[0].command)); }
2514 #line 2515 "y.tab.c"
2515 break;
2516
2517 case 61:
2518 #line 753 "/Users/chet/src/bash/src/parse.y"
2519 { (yyval.command) = (yyvsp[0].command); }
2520 #line 2521 "y.tab.c"
2521 break;
2522
2523 case 62:
2524 #line 755 "/Users/chet/src/bash/src/parse.y"
2525 {
2526 COMMAND *tc;
2527
2528 tc = (yyvsp[-1].command);
2529 if (tc && tc->redirects)
2530 {
2531 register REDIRECT *t;
2532 for (t = tc->redirects; t->next; t = t->next)
2533 ;
2534 t->next = (yyvsp[0].redirect);
2535 }
2536 else if (tc)
2537 tc->redirects = (yyvsp[0].redirect);
2538 (yyval.command) = (yyvsp[-1].command);
2539 }
2540 #line 2541 "y.tab.c"
2541 break;
2542
2543 case 63:
2544 #line 771 "/Users/chet/src/bash/src/parse.y"
2545 { (yyval.command) = (yyvsp[0].command); }
2546 #line 2547 "y.tab.c"
2547 break;
2548
2549 case 64:
2550 #line 773 "/Users/chet/src/bash/src/parse.y"
2551 { (yyval.command) = (yyvsp[0].command); }
2552 #line 2553 "y.tab.c"
2553 break;
2554
2555 case 65:
2556 #line 777 "/Users/chet/src/bash/src/parse.y"
2557 { (yyval.command) = (yyvsp[0].command); }
2558 #line 2559 "y.tab.c"
2559 break;
2560
2561 case 66:
2562 #line 779 "/Users/chet/src/bash/src/parse.y"
2563 { (yyval.command) = (yyvsp[0].command); }
2564 #line 2565 "y.tab.c"
2565 break;
2566
2567 case 67:
2568 #line 781 "/Users/chet/src/bash/src/parse.y"
2569 { (yyval.command) = make_while_command ((yyvsp[-3].command), (yyvsp[-1].command)); }
2570 #line 2571 "y.tab.c"
2571 break;
2572
2573 case 68:
2574 #line 783 "/Users/chet/src/bash/src/parse.y"
2575 { (yyval.command) = make_until_command ((yyvsp[-3].command), (yyvsp[-1].command)); }
2576 #line 2577 "y.tab.c"
2577 break;
2578
2579 case 69:
2580 #line 785 "/Users/chet/src/bash/src/parse.y"
2581 { (yyval.command) = (yyvsp[0].command); }
2582 #line 2583 "y.tab.c"
2583 break;
2584
2585 case 70:
2586 #line 787 "/Users/chet/src/bash/src/parse.y"
2587 { (yyval.command) = (yyvsp[0].command); }
2588 #line 2589 "y.tab.c"
2589 break;
2590
2591 case 71:
2592 #line 789 "/Users/chet/src/bash/src/parse.y"
2593 { (yyval.command) = (yyvsp[0].command); }
2594 #line 2595 "y.tab.c"
2595 break;
2596
2597 case 72:
2598 #line 791 "/Users/chet/src/bash/src/parse.y"
2599 { (yyval.command) = (yyvsp[0].command); }
2600 #line 2601 "y.tab.c"
2601 break;
2602
2603 case 73:
2604 #line 793 "/Users/chet/src/bash/src/parse.y"
2605 { (yyval.command) = (yyvsp[0].command); }
2606 #line 2607 "y.tab.c"
2607 break;
2608
2609 case 74:
2610 #line 795 "/Users/chet/src/bash/src/parse.y"
2611 { (yyval.command) = (yyvsp[0].command); }
2612 #line 2613 "y.tab.c"
2613 break;
2614
2615 case 75:
2616 #line 797 "/Users/chet/src/bash/src/parse.y"
2617 { (yyval.command) = (yyvsp[0].command); }
2618 #line 2619 "y.tab.c"
2619 break;
2620
2621 case 76:
2622 #line 801 "/Users/chet/src/bash/src/parse.y"
2623 {
2624 (yyval.command) = make_for_command ((yyvsp[-4].word), add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), (yyvsp[-1].command), word_lineno[word_top]);
2625 if (word_top > 0) word_top--;
2626 }
2627 #line 2628 "y.tab.c"
2628 break;
2629
2630 case 77:
2631 #line 806 "/Users/chet/src/bash/src/parse.y"
2632 {
2633 (yyval.command) = make_for_command ((yyvsp[-4].word), add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), (yyvsp[-1].command), word_lineno[word_top]);
2634 if (word_top > 0) word_top--;
2635 }
2636 #line 2637 "y.tab.c"
2637 break;
2638
2639 case 78:
2640 #line 811 "/Users/chet/src/bash/src/parse.y"
2641 {
2642 (yyval.command) = make_for_command ((yyvsp[-5].word), add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), (yyvsp[-1].command), word_lineno[word_top]);
2643 if (word_top > 0) word_top--;
2644 }
2645 #line 2646 "y.tab.c"
2646 break;
2647
2648 case 79:
2649 #line 816 "/Users/chet/src/bash/src/parse.y"
2650 {
2651 (yyval.command) = make_for_command ((yyvsp[-5].word), add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), (yyvsp[-1].command), word_lineno[word_top]);
2652 if (word_top > 0) word_top--;
2653 }
2654 #line 2655 "y.tab.c"
2655 break;
2656
2657 case 80:
2658 #line 821 "/Users/chet/src/bash/src/parse.y"
2659 {
2660 (yyval.command) = make_for_command ((yyvsp[-8].word), REVERSE_LIST ((yyvsp[-5].word_list), WORD_LIST *), (yyvsp[-1].command), word_lineno[word_top]);
2661 if (word_top > 0) word_top--;
2662 }
2663 #line 2664 "y.tab.c"
2664 break;
2665
2666 case 81:
2667 #line 826 "/Users/chet/src/bash/src/parse.y"
2668 {
2669 (yyval.command) = make_for_command ((yyvsp[-8].word), REVERSE_LIST ((yyvsp[-5].word_list), WORD_LIST *), (yyvsp[-1].command), word_lineno[word_top]);
2670 if (word_top > 0) word_top--;
2671 }
2672 #line 2673 "y.tab.c"
2673 break;
2674
2675 case 82:
2676 #line 831 "/Users/chet/src/bash/src/parse.y"
2677 {
2678 (yyval.command) = make_for_command ((yyvsp[-7].word), (WORD_LIST *)NULL, (yyvsp[-1].command), word_lineno[word_top]);
2679 if (word_top > 0) word_top--;
2680 }
2681 #line 2682 "y.tab.c"
2682 break;
2683
2684 case 83:
2685 #line 836 "/Users/chet/src/bash/src/parse.y"
2686 {
2687 (yyval.command) = make_for_command ((yyvsp[-7].word), (WORD_LIST *)NULL, (yyvsp[-1].command), word_lineno[word_top]);
2688 if (word_top > 0) word_top--;
2689 }
2690 #line 2691 "y.tab.c"
2691 break;
2692
2693 case 84:
2694 #line 843 "/Users/chet/src/bash/src/parse.y"
2695 {
2696 (yyval.command) = make_arith_for_command ((yyvsp[-5].word_list), (yyvsp[-1].command), arith_for_lineno);
2697 if ((yyval.command) == 0) YYERROR;
2698 if (word_top > 0) word_top--;
2699 }
2700 #line 2701 "y.tab.c"
2701 break;
2702
2703 case 85:
2704 #line 849 "/Users/chet/src/bash/src/parse.y"
2705 {
2706 (yyval.command) = make_arith_for_command ((yyvsp[-5].word_list), (yyvsp[-1].command), arith_for_lineno);
2707 if ((yyval.command) == 0) YYERROR;
2708 if (word_top > 0) word_top--;
2709 }
2710 #line 2711 "y.tab.c"
2711 break;
2712
2713 case 86:
2714 #line 855 "/Users/chet/src/bash/src/parse.y"
2715 {
2716 (yyval.command) = make_arith_for_command ((yyvsp[-3].word_list), (yyvsp[-1].command), arith_for_lineno);
2717 if ((yyval.command) == 0) YYERROR;
2718 if (word_top > 0) word_top--;
2719 }
2720 #line 2721 "y.tab.c"
2721 break;
2722
2723 case 87:
2724 #line 861 "/Users/chet/src/bash/src/parse.y"
2725 {
2726 (yyval.command) = make_arith_for_command ((yyvsp[-3].word_list), (yyvsp[-1].command), arith_for_lineno);
2727 if ((yyval.command) == 0) YYERROR;
2728 if (word_top > 0) word_top--;
2729 }
2730 #line 2731 "y.tab.c"
2731 break;
2732
2733 case 88:
2734 #line 869 "/Users/chet/src/bash/src/parse.y"
2735 {
2736 (yyval.command) = make_select_command ((yyvsp[-4].word), add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), (yyvsp[-1].command), word_lineno[word_top]);
2737 if (word_top > 0) word_top--;
2738 }
2739 #line 2740 "y.tab.c"
2740 break;
2741
2742 case 89:
2743 #line 874 "/Users/chet/src/bash/src/parse.y"
2744 {
2745 (yyval.command) = make_select_command ((yyvsp[-4].word), add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), (yyvsp[-1].command), word_lineno[word_top]);
2746 if (word_top > 0) word_top--;
2747 }
2748 #line 2749 "y.tab.c"
2749 break;
2750
2751 case 90:
2752 #line 879 "/Users/chet/src/bash/src/parse.y"
2753 {
2754 (yyval.command) = make_select_command ((yyvsp[-5].word), add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), (yyvsp[-1].command), word_lineno[word_top]);
2755 if (word_top > 0) word_top--;
2756 }
2757 #line 2758 "y.tab.c"
2758 break;
2759
2760 case 91:
2761 #line 884 "/Users/chet/src/bash/src/parse.y"
2762 {
2763 (yyval.command) = make_select_command ((yyvsp[-5].word), add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), (yyvsp[-1].command), word_lineno[word_top]);
2764 if (word_top > 0) word_top--;
2765 }
2766 #line 2767 "y.tab.c"
2767 break;
2768
2769 case 92:
2770 #line 889 "/Users/chet/src/bash/src/parse.y"
2771 {
2772 (yyval.command) = make_select_command ((yyvsp[-8].word), REVERSE_LIST ((yyvsp[-5].word_list), WORD_LIST *), (yyvsp[-1].command), word_lineno[word_top]);
2773 if (word_top > 0) word_top--;
2774 }
2775 #line 2776 "y.tab.c"
2776 break;
2777
2778 case 93:
2779 #line 894 "/Users/chet/src/bash/src/parse.y"
2780 {
2781 (yyval.command) = make_select_command ((yyvsp[-8].word), REVERSE_LIST ((yyvsp[-5].word_list), WORD_LIST *), (yyvsp[-1].command), word_lineno[word_top]);
2782 if (word_top > 0) word_top--;
2783 }
2784 #line 2785 "y.tab.c"
2785 break;
2786
2787 case 94:
2788 #line 899 "/Users/chet/src/bash/src/parse.y"
2789 {
2790 (yyval.command) = make_select_command ((yyvsp[-7].word), (WORD_LIST *)NULL, (yyvsp[-1].command), word_lineno[word_top]);
2791 if (word_top > 0) word_top--;
2792 }
2793 #line 2794 "y.tab.c"
2794 break;
2795
2796 case 95:
2797 #line 904 "/Users/chet/src/bash/src/parse.y"
2798 {
2799 (yyval.command) = make_select_command ((yyvsp[-7].word), (WORD_LIST *)NULL, (yyvsp[-1].command), word_lineno[word_top]);
2800 if (word_top > 0) word_top--;
2801 }
2802 #line 2803 "y.tab.c"
2803 break;
2804
2805 case 96:
2806 #line 911 "/Users/chet/src/bash/src/parse.y"
2807 {
2808 (yyval.command) = make_case_command ((yyvsp[-4].word), (PATTERN_LIST *)NULL, word_lineno[word_top]);
2809 if (word_top > 0) word_top--;
2810 }
2811 #line 2812 "y.tab.c"
2812 break;
2813
2814 case 97:
2815 #line 916 "/Users/chet/src/bash/src/parse.y"
2816 {
2817 (yyval.command) = make_case_command ((yyvsp[-5].word), (yyvsp[-2].pattern), word_lineno[word_top]);
2818 if (word_top > 0) word_top--;
2819 }
2820 #line 2821 "y.tab.c"
2821 break;
2822
2823 case 98:
2824 #line 921 "/Users/chet/src/bash/src/parse.y"
2825 {
2826 (yyval.command) = make_case_command ((yyvsp[-4].word), (yyvsp[-1].pattern), word_lineno[word_top]);
2827 if (word_top > 0) word_top--;
2828 }
2829 #line 2830 "y.tab.c"
2830 break;
2831
2832 case 99:
2833 #line 928 "/Users/chet/src/bash/src/parse.y"
2834 { (yyval.command) = make_function_def ((yyvsp[-4].word), (yyvsp[0].command), function_dstart, function_bstart); }
2835 #line 2836 "y.tab.c"
2836 break;
2837
2838 case 100:
2839 #line 930 "/Users/chet/src/bash/src/parse.y"
2840 { (yyval.command) = make_function_def ((yyvsp[-4].word), (yyvsp[0].command), function_dstart, function_bstart); }
2841 #line 2842 "y.tab.c"
2842 break;
2843
2844 case 101:
2845 #line 932 "/Users/chet/src/bash/src/parse.y"
2846 { (yyval.command) = make_function_def ((yyvsp[-1].word), (yyvsp[0].command), function_dstart, function_bstart); }
2847 #line 2848 "y.tab.c"
2848 break;
2849
2850 case 102:
2851 #line 934 "/Users/chet/src/bash/src/parse.y"
2852 { (yyval.command) = make_function_def ((yyvsp[-3].word), (yyvsp[0].command), function_dstart, function_bstart); }
2853 #line 2854 "y.tab.c"
2854 break;
2855
2856 case 103:
2857 #line 938 "/Users/chet/src/bash/src/parse.y"
2858 { (yyval.command) = (yyvsp[0].command); }
2859 #line 2860 "y.tab.c"
2860 break;
2861
2862 case 104:
2863 #line 940 "/Users/chet/src/bash/src/parse.y"
2864 {
2865 COMMAND *tc;
2866
2867 tc = (yyvsp[-1].command);
2868 /* According to Posix.2 3.9.5, redirections
2869 specified after the body of a function should
2870 be attached to the function and performed when
2871 the function is executed, not as part of the
2872 function definition command. */
2873 /* XXX - I don't think it matters, but we might
2874 want to change this in the future to avoid
2875 problems differentiating between a function
2876 definition with a redirection and a function
2877 definition containing a single command with a
2878 redirection. The two are semantically equivalent,
2879 though -- the only difference is in how the
2880 command printing code displays the redirections. */
2881 if (tc && tc->redirects)
2882 {
2883 register REDIRECT *t;
2884 for (t = tc->redirects; t->next; t = t->next)
2885 ;
2886 t->next = (yyvsp[0].redirect);
2887 }
2888 else if (tc)
2889 tc->redirects = (yyvsp[0].redirect);
2890 (yyval.command) = (yyvsp[-1].command);
2891 }
2892 #line 2893 "y.tab.c"
2893 break;
2894
2895 case 105:
2896 #line 971 "/Users/chet/src/bash/src/parse.y"
2897 {
2898 (yyval.command) = make_subshell_command ((yyvsp[-1].command));
2899 (yyval.command)->flags |= CMD_WANT_SUBSHELL;
2900 }
2901 #line 2902 "y.tab.c"
2902 break;
2903
2904 case 106:
2905 #line 978 "/Users/chet/src/bash/src/parse.y"
2906 {
2907 (yyval.command) = make_coproc_command ("COPROC", (yyvsp[0].command));
2908 (yyval.command)->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
2909 }
2910 #line 2911 "y.tab.c"
2911 break;
2912
2913 case 107:
2914 #line 983 "/Users/chet/src/bash/src/parse.y"
2915 {
2916 COMMAND *tc;
2917
2918 tc = (yyvsp[-1].command);
2919 if (tc && tc->redirects)
2920 {
2921 register REDIRECT *t;
2922 for (t = tc->redirects; t->next; t = t->next)
2923 ;
2924 t->next = (yyvsp[0].redirect);
2925 }
2926 else if (tc)
2927 tc->redirects = (yyvsp[0].redirect);
2928 (yyval.command) = make_coproc_command ("COPROC", (yyvsp[-1].command));
2929 (yyval.command)->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
2930 }
2931 #line 2932 "y.tab.c"
2932 break;
2933
2934 case 108:
2935 #line 1000 "/Users/chet/src/bash/src/parse.y"
2936 {
2937 (yyval.command) = make_coproc_command ((yyvsp[-1].word)->word, (yyvsp[0].command));
2938 (yyval.command)->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
2939 }
2940 #line 2941 "y.tab.c"
2941 break;
2942
2943 case 109:
2944 #line 1005 "/Users/chet/src/bash/src/parse.y"
2945 {
2946 COMMAND *tc;
2947
2948 tc = (yyvsp[-1].command);
2949 if (tc && tc->redirects)
2950 {
2951 register REDIRECT *t;
2952 for (t = tc->redirects; t->next; t = t->next)
2953 ;
2954 t->next = (yyvsp[0].redirect);
2955 }
2956 else if (tc)
2957 tc->redirects = (yyvsp[0].redirect);
2958 (yyval.command) = make_coproc_command ((yyvsp[-2].word)->word, (yyvsp[-1].command));
2959 (yyval.command)->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
2960 }
2961 #line 2962 "y.tab.c"
2962 break;
2963
2964 case 110:
2965 #line 1022 "/Users/chet/src/bash/src/parse.y"
2966 {
2967 (yyval.command) = make_coproc_command ("COPROC", clean_simple_command ((yyvsp[0].command)));
2968 (yyval.command)->flags |= CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
2969 }
2970 #line 2971 "y.tab.c"
2971 break;
2972
2973 case 111:
2974 #line 1029 "/Users/chet/src/bash/src/parse.y"
2975 { (yyval.command) = make_if_command ((yyvsp[-3].command), (yyvsp[-1].command), (COMMAND *)NULL); }
2976 #line 2977 "y.tab.c"
2977 break;
2978
2979 case 112:
2980 #line 1031 "/Users/chet/src/bash/src/parse.y"
2981 { (yyval.command) = make_if_command ((yyvsp[-5].command), (yyvsp[-3].command), (yyvsp[-1].command)); }
2982 #line 2983 "y.tab.c"
2983 break;
2984
2985 case 113:
2986 #line 1033 "/Users/chet/src/bash/src/parse.y"
2987 { (yyval.command) = make_if_command ((yyvsp[-4].command), (yyvsp[-2].command), (yyvsp[-1].command)); }
2988 #line 2989 "y.tab.c"
2989 break;
2990
2991 case 114:
2992 #line 1038 "/Users/chet/src/bash/src/parse.y"
2993 { (yyval.command) = make_group_command ((yyvsp[-1].command)); }
2994 #line 2995 "y.tab.c"
2995 break;
2996
2997 case 115:
2998 #line 1042 "/Users/chet/src/bash/src/parse.y"
2999 { (yyval.command) = make_arith_command ((yyvsp[0].word_list)); }
3000 #line 3001 "y.tab.c"
3001 break;
3002
3003 case 116:
3004 #line 1046 "/Users/chet/src/bash/src/parse.y"
3005 { (yyval.command) = (yyvsp[-1].command); }
3006 #line 3007 "y.tab.c"
3007 break;
3008
3009 case 117:
3010 #line 1050 "/Users/chet/src/bash/src/parse.y"
3011 { (yyval.command) = make_if_command ((yyvsp[-2].command), (yyvsp[0].command), (COMMAND *)NULL); }
3012 #line 3013 "y.tab.c"
3013 break;
3014
3015 case 118:
3016 #line 1052 "/Users/chet/src/bash/src/parse.y"
3017 { (yyval.command) = make_if_command ((yyvsp[-4].command), (yyvsp[-2].command), (yyvsp[0].command)); }
3018 #line 3019 "y.tab.c"
3019 break;
3020
3021 case 119:
3022 #line 1054 "/Users/chet/src/bash/src/parse.y"
3023 { (yyval.command) = make_if_command ((yyvsp[-3].command), (yyvsp[-1].command), (yyvsp[0].command)); }
3024 #line 3025 "y.tab.c"
3025 break;
3026
3027 case 121:
3028 #line 1059 "/Users/chet/src/bash/src/parse.y"
3029 { (yyvsp[0].pattern)->next = (yyvsp[-1].pattern); (yyval.pattern) = (yyvsp[0].pattern); }
3030 #line 3031 "y.tab.c"
3031 break;
3032
3033 case 122:
3034 #line 1063 "/Users/chet/src/bash/src/parse.y"
3035 { (yyval.pattern) = make_pattern_list ((yyvsp[-2].word_list), (yyvsp[0].command)); }
3036 #line 3037 "y.tab.c"
3037 break;
3038
3039 case 123:
3040 #line 1065 "/Users/chet/src/bash/src/parse.y"
3041 { (yyval.pattern) = make_pattern_list ((yyvsp[-2].word_list), (COMMAND *)NULL); }
3042 #line 3043 "y.tab.c"
3043 break;
3044
3045 case 124:
3046 #line 1067 "/Users/chet/src/bash/src/parse.y"
3047 { (yyval.pattern) = make_pattern_list ((yyvsp[-2].word_list), (yyvsp[0].command)); }
3048 #line 3049 "y.tab.c"
3049 break;
3050
3051 case 125:
3052 #line 1069 "/Users/chet/src/bash/src/parse.y"
3053 { (yyval.pattern) = make_pattern_list ((yyvsp[-2].word_list), (COMMAND *)NULL); }
3054 #line 3055 "y.tab.c"
3055 break;
3056
3057 case 126:
3058 #line 1073 "/Users/chet/src/bash/src/parse.y"
3059 { (yyval.pattern) = (yyvsp[-1].pattern); }
3060 #line 3061 "y.tab.c"
3061 break;
3062
3063 case 127:
3064 #line 1075 "/Users/chet/src/bash/src/parse.y"
3065 { (yyvsp[-1].pattern)->next = (yyvsp[-2].pattern); (yyval.pattern) = (yyvsp[-1].pattern); }
3066 #line 3067 "y.tab.c"
3067 break;
3068
3069 case 128:
3070 #line 1077 "/Users/chet/src/bash/src/parse.y"
3071 { (yyvsp[-1].pattern)->flags |= CASEPAT_FALLTHROUGH; (yyval.pattern) = (yyvsp[-1].pattern); }
3072 #line 3073 "y.tab.c"
3073 break;
3074
3075 case 129:
3076 #line 1079 "/Users/chet/src/bash/src/parse.y"
3077 { (yyvsp[-1].pattern)->flags |= CASEPAT_FALLTHROUGH; (yyvsp[-1].pattern)->next = (yyvsp[-2].pattern); (yyval.pattern) = (yyvsp[-1].pattern); }
3078 #line 3079 "y.tab.c"
3079 break;
3080
3081 case 130:
3082 #line 1081 "/Users/chet/src/bash/src/parse.y"
3083 { (yyvsp[-1].pattern)->flags |= CASEPAT_TESTNEXT; (yyval.pattern) = (yyvsp[-1].pattern); }
3084 #line 3085 "y.tab.c"
3085 break;
3086
3087 case 131:
3088 #line 1083 "/Users/chet/src/bash/src/parse.y"
3089 { (yyvsp[-1].pattern)->flags |= CASEPAT_TESTNEXT; (yyvsp[-1].pattern)->next = (yyvsp[-2].pattern); (yyval.pattern) = (yyvsp[-1].pattern); }
3090 #line 3091 "y.tab.c"
3091 break;
3092
3093 case 132:
3094 #line 1087 "/Users/chet/src/bash/src/parse.y"
3095 { (yyval.word_list) = make_word_list ((yyvsp[0].word), (WORD_LIST *)NULL); }
3096 #line 3097 "y.tab.c"
3097 break;
3098
3099 case 133:
3100 #line 1089 "/Users/chet/src/bash/src/parse.y"
3101 { (yyval.word_list) = make_word_list ((yyvsp[0].word), (yyvsp[-2].word_list)); }
3102 #line 3103 "y.tab.c"
3103 break;
3104
3105 case 134:
3106 #line 1098 "/Users/chet/src/bash/src/parse.y"
3107 {
3108 (yyval.command) = (yyvsp[0].command);
3109 if (need_here_doc)
3110 gather_here_documents ();
3111 }
3112 #line 3113 "y.tab.c"
3113 break;
3114
3115 case 136:
3116 #line 1107 "/Users/chet/src/bash/src/parse.y"
3117 {
3118 (yyval.command) = (yyvsp[0].command);
3119 }
3120 #line 3121 "y.tab.c"
3121 break;
3122
3123 case 138:
3124 #line 1114 "/Users/chet/src/bash/src/parse.y"
3125 {
3126 if ((yyvsp[-2].command)->type == cm_connection)
3127 (yyval.command) = connect_async_list ((yyvsp[-2].command), (COMMAND *)NULL, '&');
3128 else
3129 (yyval.command) = command_connect ((yyvsp[-2].command), (COMMAND *)NULL, '&');
3130 }
3131 #line 3132 "y.tab.c"
3132 break;
3133
3134 case 140:
3135 #line 1125 "/Users/chet/src/bash/src/parse.y"
3136 { (yyval.command) = command_connect ((yyvsp[-3].command), (yyvsp[0].command), AND_AND); }
3137 #line 3138 "y.tab.c"
3138 break;
3139
3140 case 141:
3141 #line 1127 "/Users/chet/src/bash/src/parse.y"
3142 { (yyval.command) = command_connect ((yyvsp[-3].command), (yyvsp[0].command), OR_OR); }
3143 #line 3144 "y.tab.c"
3144 break;
3145
3146 case 142:
3147 #line 1129 "/Users/chet/src/bash/src/parse.y"
3148 {
3149 if ((yyvsp[-3].command)->type == cm_connection)
3150 (yyval.command) = connect_async_list ((yyvsp[-3].command), (yyvsp[0].command), '&');
3151 else
3152 (yyval.command) = command_connect ((yyvsp[-3].command), (yyvsp[0].command), '&');
3153 }
3154 #line 3155 "y.tab.c"
3155 break;
3156
3157 case 143:
3158 #line 1136 "/Users/chet/src/bash/src/parse.y"
3159 { (yyval.command) = command_connect ((yyvsp[-3].command), (yyvsp[0].command), ';'); }
3160 #line 3161 "y.tab.c"
3161 break;
3162
3163 case 144:
3164 #line 1138 "/Users/chet/src/bash/src/parse.y"
3165 { (yyval.command) = command_connect ((yyvsp[-3].command), (yyvsp[0].command), ';'); }
3166 #line 3167 "y.tab.c"
3167 break;
3168
3169 case 145:
3170 #line 1140 "/Users/chet/src/bash/src/parse.y"
3171 { (yyval.command) = (yyvsp[0].command); }
3172 #line 3173 "y.tab.c"
3173 break;
3174
3175 case 148:
3176 #line 1148 "/Users/chet/src/bash/src/parse.y"
3177 { (yyval.number) = '\n'; }
3178 #line 3179 "y.tab.c"
3179 break;
3180
3181 case 149:
3182 #line 1150 "/Users/chet/src/bash/src/parse.y"
3183 { (yyval.number) = ';'; }
3184 #line 3185 "y.tab.c"
3185 break;
3186
3187 case 150:
3188 #line 1152 "/Users/chet/src/bash/src/parse.y"
3189 { (yyval.number) = yacc_EOF; }
3190 #line 3191 "y.tab.c"
3191 break;
3192
3193 case 153:
3194 #line 1166 "/Users/chet/src/bash/src/parse.y"
3195 {
3196 (yyval.command) = (yyvsp[0].command);
3197 if (need_here_doc)
3198 gather_here_documents ();
3199 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
3200 {
3201 global_command = (yyvsp[0].command);
3202 eof_encountered = 0;
3203 rewind_input_string ();
3204 YYACCEPT;
3205 }
3206 }
3207 #line 3208 "y.tab.c"
3208 break;
3209
3210 case 154:
3211 #line 1179 "/Users/chet/src/bash/src/parse.y"
3212 {
3213 if ((yyvsp[-1].command)->type == cm_connection)
3214 (yyval.command) = connect_async_list ((yyvsp[-1].command), (COMMAND *)NULL, '&');
3215 else
3216 (yyval.command) = command_connect ((yyvsp[-1].command), (COMMAND *)NULL, '&');
3217 if (need_here_doc)
3218 gather_here_documents ();
3219 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
3220 {
3221 global_command = (yyvsp[-1].command);
3222 eof_encountered = 0;
3223 rewind_input_string ();
3224 YYACCEPT;
3225 }
3226 }
3227 #line 3228 "y.tab.c"
3228 break;
3229
3230 case 155:
3231 #line 1195 "/Users/chet/src/bash/src/parse.y"
3232 {
3233 (yyval.command) = (yyvsp[-1].command);
3234 if (need_here_doc)
3235 gather_here_documents ();
3236 if ((parser_state & PST_CMDSUBST) && current_token == shell_eof_token)
3237 {
3238 global_command = (yyvsp[-1].command);
3239 eof_encountered = 0;
3240 rewind_input_string ();
3241 YYACCEPT;
3242 }
3243 }
3244 #line 3245 "y.tab.c"
3245 break;
3246
3247 case 156:
3248 #line 1210 "/Users/chet/src/bash/src/parse.y"
3249 { (yyval.command) = command_connect ((yyvsp[-3].command), (yyvsp[0].command), AND_AND); }
3250 #line 3251 "y.tab.c"
3251 break;
3252
3253 case 157:
3254 #line 1212 "/Users/chet/src/bash/src/parse.y"
3255 { (yyval.command) = command_connect ((yyvsp[-3].command), (yyvsp[0].command), OR_OR); }
3256 #line 3257 "y.tab.c"
3257 break;
3258
3259 case 158:
3260 #line 1214 "/Users/chet/src/bash/src/parse.y"
3261 {
3262 if ((yyvsp[-2].command)->type == cm_connection)
3263 (yyval.command) = connect_async_list ((yyvsp[-2].command), (yyvsp[0].command), '&');
3264 else
3265 (yyval.command) = command_connect ((yyvsp[-2].command), (yyvsp[0].command), '&');
3266 }
3267 #line 3268 "y.tab.c"
3268 break;
3269
3270 case 159:
3271 #line 1221 "/Users/chet/src/bash/src/parse.y"
3272 { (yyval.command) = command_connect ((yyvsp[-2].command), (yyvsp[0].command), ';'); }
3273 #line 3274 "y.tab.c"
3274 break;
3275
3276 case 160:
3277 #line 1224 "/Users/chet/src/bash/src/parse.y"
3278 { (yyval.command) = (yyvsp[0].command); }
3279 #line 3280 "y.tab.c"
3280 break;
3281
3282 case 161:
3283 #line 1228 "/Users/chet/src/bash/src/parse.y"
3284 { (yyval.command) = (yyvsp[0].command); }
3285 #line 3286 "y.tab.c"
3286 break;
3287
3288 case 162:
3289 #line 1230 "/Users/chet/src/bash/src/parse.y"
3290 {
3291 if ((yyvsp[0].command))
3292 (yyvsp[0].command)->flags ^= CMD_INVERT_RETURN; /* toggle */
3293 (yyval.command) = (yyvsp[0].command);
3294 }
3295 #line 3296 "y.tab.c"
3296 break;
3297
3298 case 163:
3299 #line 1236 "/Users/chet/src/bash/src/parse.y"
3300 {
3301 if ((yyvsp[0].command))
3302 (yyvsp[0].command)->flags |= (yyvsp[-1].number);
3303 (yyval.command) = (yyvsp[0].command);
3304 }
3305 #line 3306 "y.tab.c"
3306 break;
3307
3308 case 164:
3309 #line 1242 "/Users/chet/src/bash/src/parse.y"
3310 {
3311 ELEMENT x;
3312
3313 /* Boy, this is unclean. `time' by itself can
3314 time a null command. We cheat and push a
3315 newline back if the list_terminator was a newline
3316 to avoid the double-newline problem (one to
3317 terminate this, one to terminate the command) */
3318 x.word = 0;
3319 x.redirect = 0;
3320 (yyval.command) = make_simple_command (x, (COMMAND *)NULL);
3321 (yyval.command)->flags |= (yyvsp[-1].number);
3322 /* XXX - let's cheat and push a newline back */
3323 if ((yyvsp[0].number) == '\n')
3324 token_to_read = '\n';
3325 else if ((yyvsp[0].number) == ';')
3326 token_to_read = ';';
3327 parser_state &= ~PST_REDIRLIST; /* make_simple_command sets this */
3328 }
3329 #line 3330 "y.tab.c"
3330 break;
3331
3332 case 165:
3333 #line 1262 "/Users/chet/src/bash/src/parse.y"
3334 {
3335 ELEMENT x;
3336
3337 /* This is just as unclean. Posix says that `!'
3338 by itself should be equivalent to `false'.
3339 We cheat and push a
3340 newline back if the list_terminator was a newline
3341 to avoid the double-newline problem (one to
3342 terminate this, one to terminate the command) */
3343 x.word = 0;
3344 x.redirect = 0;
3345 (yyval.command) = make_simple_command (x, (COMMAND *)NULL);
3346 (yyval.command)->flags |= CMD_INVERT_RETURN;
3347 /* XXX - let's cheat and push a newline back */
3348 if ((yyvsp[0].number) == '\n')
3349 token_to_read = '\n';
3350 if ((yyvsp[0].number) == ';')
3351 token_to_read = ';';
3352 parser_state &= ~PST_REDIRLIST; /* make_simple_command sets this */
3353 }
3354 #line 3355 "y.tab.c"
3355 break;
3356
3357 case 166:
3358 #line 1285 "/Users/chet/src/bash/src/parse.y"
3359 { (yyval.command) = command_connect ((yyvsp[-3].command), (yyvsp[0].command), '|'); }
3360 #line 3361 "y.tab.c"
3361 break;
3362
3363 case 167:
3364 #line 1287 "/Users/chet/src/bash/src/parse.y"
3365 {
3366 /* Make cmd1 |& cmd2 equivalent to cmd1 2>&1 | cmd2 */
3367 COMMAND *tc;
3368 REDIRECTEE rd, sd;
3369 REDIRECT *r;
3370
3371 tc = (yyvsp[-3].command)->type == cm_simple ? (COMMAND *)(yyvsp[-3].command)->value.Simple : (yyvsp[-3].command);
3372 sd.dest = 2;
3373 rd.dest = 1;
3374 r = make_redirection (sd, r_duplicating_output, rd, 0);
3375 if (tc->redirects)
3376 {
3377 register REDIRECT *t;
3378 for (t = tc->redirects; t->next; t = t->next)
3379 ;
3380 t->next = r;
3381 }
3382 else
3383 tc->redirects = r;
3384
3385 (yyval.command) = command_connect ((yyvsp[-3].command), (yyvsp[0].command), '|');
3386 }
3387 #line 3388 "y.tab.c"
3388 break;
3389
3390 case 168:
3391 #line 1310 "/Users/chet/src/bash/src/parse.y"
3392 { (yyval.command) = (yyvsp[0].command); }
3393 #line 3394 "y.tab.c"
3394 break;
3395
3396 case 169:
3397 #line 1314 "/Users/chet/src/bash/src/parse.y"
3398 { (yyval.number) = CMD_TIME_PIPELINE; }
3399 #line 3400 "y.tab.c"
3400 break;
3401
3402 case 170:
3403 #line 1316 "/Users/chet/src/bash/src/parse.y"
3404 { (yyval.number) = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
3405 #line 3406 "y.tab.c"
3406 break;
3407
3408 case 171:
3409 #line 1318 "/Users/chet/src/bash/src/parse.y"
3410 { (yyval.number) = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
3411 #line 3412 "y.tab.c"
3412 break;
3413
3414 case 172:
3415 #line 1320 "/Users/chet/src/bash/src/parse.y"
3416 { (yyval.number) = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
3417 #line 3418 "y.tab.c"
3418 break;
3419
3420
3421 #line 3422 "y.tab.c"
3422
3423 default: break;
3424 }
3425 /* User semantic actions sometimes alter yychar, and that requires
3426 that yytoken be updated with the new translation. We take the
3427 approach of translating immediately before every use of yytoken.
3428 One alternative is translating here after every semantic action,
3429 but that translation would be missed if the semantic action invokes
3430 YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or
3431 if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an
3432 incorrect destructor might then be invoked immediately. In the
3433 case of YYERROR or YYBACKUP, subsequent parser actions might lead
3434 to an incorrect destructor call or verbose syntax error message
3435 before the lookahead is translated. */
3436 YY_SYMBOL_PRINT ("-> $$ =", YY_CAST (yysymbol_kind_t, yyr1[yyn]), &yyval, &yyloc);
3437
3438 YYPOPSTACK (yylen);
3439 yylen = 0;
3440
3441 *++yyvsp = yyval;
3442
3443 /* Now 'shift' the result of the reduction. Determine what state
3444 that goes to, based on the state we popped back to and the rule
3445 number reduced by. */
3446 {
3447 const int yylhs = yyr1[yyn] - YYNTOKENS;
3448 const int yyi = yypgoto[yylhs] + *yyssp;
3449 yystate = (0 <= yyi && yyi <= YYLAST && yycheck[yyi] == *yyssp
3450 ? yytable[yyi]
3451 : yydefgoto[yylhs]);
3452 }
3453
3454 goto yynewstate;
3455
3456
3457 /*--------------------------------------.
3458 | yyerrlab -- here on detecting error. |
3459 `--------------------------------------*/
3460 yyerrlab:
3461 /* Make sure we have latest lookahead translation. See comments at
3462 user semantic actions for why this is necessary. */
3463 yytoken = yychar == YYEMPTY ? YYSYMBOL_YYEMPTY : YYTRANSLATE (yychar);
3464 /* If not already recovering from an error, report this error. */
3465 if (!yyerrstatus)
3466 {
3467 ++yynerrs;
3468 yyerror (YY_("syntax error"));
3469 }
3470
3471 if (yyerrstatus == 3)
3472 {
3473 /* If just tried and failed to reuse lookahead token after an
3474 error, discard it. */
3475
3476 if (yychar <= YYEOF)
3477 {
3478 /* Return failure if at end of input. */
3479 if (yychar == YYEOF)
3480 YYABORT;
3481 }
3482 else
3483 {
3484 yydestruct ("Error: discarding",
3485 yytoken, &yylval);
3486 yychar = YYEMPTY;
3487 }
3488 }
3489
3490 /* Else will try to reuse lookahead token after shifting the error
3491 token. */
3492 goto yyerrlab1;
3493
3494
3495 /*---------------------------------------------------.
3496 | yyerrorlab -- error raised explicitly by YYERROR. |
3497 `---------------------------------------------------*/
3498 yyerrorlab:
3499 /* Pacify compilers when the user code never invokes YYERROR and the
3500 label yyerrorlab therefore never appears in user code. */
3501 if (0)
3502 YYERROR;
3503
3504 /* Do not reclaim the symbols of the rule whose action triggered
3505 this YYERROR. */
3506 YYPOPSTACK (yylen);
3507 yylen = 0;
3508 YY_STACK_PRINT (yyss, yyssp);
3509 yystate = *yyssp;
3510 goto yyerrlab1;
3511
3512
3513 /*-------------------------------------------------------------.
3514 | yyerrlab1 -- common code for both syntax error and YYERROR. |
3515 `-------------------------------------------------------------*/
3516 yyerrlab1:
3517 yyerrstatus = 3; /* Each real token shifted decrements this. */
3518
3519 /* Pop stack until we find a state that shifts the error token. */
3520 for (;;)
3521 {
3522 yyn = yypact[yystate];
3523 if (!yypact_value_is_default (yyn))
3524 {
3525 yyn += YYSYMBOL_YYerror;
3526 if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYSYMBOL_YYerror)
3527 {
3528 yyn = yytable[yyn];
3529 if (0 < yyn)
3530 break;
3531 }
3532 }
3533
3534 /* Pop the current state because it cannot handle the error token. */
3535 if (yyssp == yyss)
3536 YYABORT;
3537
3538
3539 yydestruct ("Error: popping",
3540 YY_ACCESSING_SYMBOL (yystate), yyvsp);
3541 YYPOPSTACK (1);
3542 yystate = *yyssp;
3543 YY_STACK_PRINT (yyss, yyssp);
3544 }
3545
3546 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
3547 *++yyvsp = yylval;
3548 YY_IGNORE_MAYBE_UNINITIALIZED_END
3549
3550
3551 /* Shift the error token. */
3552 YY_SYMBOL_PRINT ("Shifting", YY_ACCESSING_SYMBOL (yyn), yyvsp, yylsp);
3553
3554 yystate = yyn;
3555 goto yynewstate;
3556
3557
3558 /*-------------------------------------.
3559 | yyacceptlab -- YYACCEPT comes here. |
3560 `-------------------------------------*/
3561 yyacceptlab:
3562 yyresult = 0;
3563 goto yyreturn;
3564
3565
3566 /*-----------------------------------.
3567 | yyabortlab -- YYABORT comes here. |
3568 `-----------------------------------*/
3569 yyabortlab:
3570 yyresult = 1;
3571 goto yyreturn;
3572
3573
3574 #if !defined yyoverflow
3575 /*-------------------------------------------------.
3576 | yyexhaustedlab -- memory exhaustion comes here. |
3577 `-------------------------------------------------*/
3578 yyexhaustedlab:
3579 yyerror (YY_("memory exhausted"));
3580 yyresult = 2;
3581 /* Fall through. */
3582 #endif
3583
3584
3585 /*-----------------------------------------------------.
3586 | yyreturn -- parsing is finished, return the result. |
3587 `-----------------------------------------------------*/
3588 yyreturn:
3589 if (yychar != YYEMPTY)
3590 {
3591 /* Make sure we have latest lookahead translation. See comments at
3592 user semantic actions for why this is necessary. */
3593 yytoken = YYTRANSLATE (yychar);
3594 yydestruct ("Cleanup: discarding lookahead",
3595 yytoken, &yylval);
3596 }
3597 /* Do not reclaim the symbols of the rule whose action triggered
3598 this YYABORT or YYACCEPT. */
3599 YYPOPSTACK (yylen);
3600 YY_STACK_PRINT (yyss, yyssp);
3601 while (yyssp != yyss)
3602 {
3603 yydestruct ("Cleanup: popping",
3604 YY_ACCESSING_SYMBOL (+*yyssp), yyvsp);
3605 YYPOPSTACK (1);
3606 }
3607 #ifndef yyoverflow
3608 if (yyss != yyssa)
3609 YYSTACK_FREE (yyss);
3610 #endif
3611
3612 return yyresult;
3613 }
3614
3615 #line 1322 "/Users/chet/src/bash/src/parse.y"
3616
3617
3618 /* Initial size to allocate for tokens, and the
3619 amount to grow them by. */
3620 #define TOKEN_DEFAULT_INITIAL_SIZE 496
3621 #define TOKEN_DEFAULT_GROW_SIZE 512
3622
3623 /* Should we call prompt_again? */
3624 #define SHOULD_PROMPT() \
3625 (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
3626
3627 #if defined (ALIAS)
3628 # define expanding_alias() (pushed_string_list && pushed_string_list->expander)
3629 #else
3630 # define expanding_alias() 0
3631 #endif
3632
3633 /* Global var is non-zero when end of file has been reached. */
3634 int EOF_Reached = 0;
3635
3636 #ifdef DEBUG
3637 static void
3638 debug_parser (i)
3639 int i;
3640 {
3641 #if YYDEBUG != 0
3642 yydebug = i;
3643 yyoutstream = stdout;
3644 yyerrstream = stderr;
3645 #endif
3646 }
3647 #endif
3648
3649 /* yy_getc () returns the next available character from input or EOF.
3650 yy_ungetc (c) makes `c' the next character to read.
3651 init_yy_io (get, unget, type, location) makes the function GET the
3652 installed function for getting the next character, makes UNGET the
3653 installed function for un-getting a character, sets the type of stream
3654 (either string or file) from TYPE, and makes LOCATION point to where
3655 the input is coming from. */
3656
3657 /* Unconditionally returns end-of-file. */
3658 int
3659 return_EOF ()
3660 {
3661 return (EOF);
3662 }
3663
3664 /* Variable containing the current get and unget functions.
3665 See ./input.h for a clearer description. */
3666 BASH_INPUT bash_input;
3667
3668 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
3669 is non-null, avoiding a memory leak. */
3670 void
3671 initialize_bash_input ()
3672 {
3673 bash_input.type = st_none;
3674 FREE (bash_input.name);
3675 bash_input.name = (char *)NULL;
3676 bash_input.location.file = (FILE *)NULL;
3677 bash_input.location.string = (char *)NULL;
3678 bash_input.getter = (sh_cget_func_t *)NULL;
3679 bash_input.ungetter = (sh_cunget_func_t *)NULL;
3680 }
3681
3682 /* Set the contents of the current bash input stream from
3683 GET, UNGET, TYPE, NAME, and LOCATION. */
3684 void
3685 init_yy_io (get, unget, type, name, location)
3686 sh_cget_func_t *get;
3687 sh_cunget_func_t *unget;
3688 enum stream_type type;
3689 const char *name;
3690 INPUT_STREAM location;
3691 {
3692 bash_input.type = type;
3693 FREE (bash_input.name);
3694 bash_input.name = name ? savestring (name) : (char *)NULL;
3695
3696 /* XXX */
3697 #if defined (CRAY)
3698 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
3699 #else
3700 bash_input.location = location;
3701 #endif
3702 bash_input.getter = get;
3703 bash_input.ungetter = unget;
3704 }
3705
3706 char *
3707 yy_input_name ()
3708 {
3709 return (bash_input.name ? bash_input.name : "stdin");
3710 }
3711
3712 /* Call this to get the next character of input. */
3713 static int
3714 yy_getc ()
3715 {
3716 return (*(bash_input.getter)) ();
3717 }
3718
3719 /* Call this to unget C. That is, to make C the next character
3720 to be read. */
3721 static int
3722 yy_ungetc (c)
3723 int c;
3724 {
3725 return (*(bash_input.ungetter)) (c);
3726 }
3727
3728 #if defined (BUFFERED_INPUT)
3729 #ifdef INCLUDE_UNUSED
3730 int
3731 input_file_descriptor ()
3732 {
3733 switch (bash_input.type)
3734 {
3735 case st_stream:
3736 return (fileno (bash_input.location.file));
3737 case st_bstream:
3738 return (bash_input.location.buffered_fd);
3739 case st_stdin:
3740 default:
3741 return (fileno (stdin));
3742 }
3743 }
3744 #endif
3745 #endif /* BUFFERED_INPUT */
3746
3747 /* **************************************************************** */
3748 /* */
3749 /* Let input be read from readline (). */
3750 /* */
3751 /* **************************************************************** */
3752
3753 #if defined (READLINE)
3754 char *current_readline_prompt = (char *)NULL;
3755 char *current_readline_line = (char *)NULL;
3756 int current_readline_line_index = 0;
3757
3758 static int
3759 yy_readline_get ()
3760 {
3761 SigHandler *old_sigint;
3762 int line_len;
3763 unsigned char c;
3764
3765 if (current_readline_line == 0)
3766 {
3767 if (bash_readline_initialized == 0)
3768 initialize_readline ();
3769
3770 #if defined (JOB_CONTROL)
3771 if (job_control)
3772 give_terminal_to (shell_pgrp, 0);
3773 #endif /* JOB_CONTROL */
3774
3775 old_sigint = IMPOSSIBLE_TRAP_HANDLER;
3776 if (signal_is_ignored (SIGINT) == 0)
3777 {
3778 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
3779 }
3780
3781 sh_unset_nodelay_mode (fileno (rl_instream)); /* just in case */
3782 current_readline_line = readline (current_readline_prompt ?
3783 current_readline_prompt : "");
3784
3785 CHECK_TERMSIG;
3786 if (signal_is_ignored (SIGINT) == 0)
3787 {
3788 if (old_sigint != IMPOSSIBLE_TRAP_HANDLER)
3789 set_signal_handler (SIGINT, old_sigint);
3790 }
3791
3792 #if 0
3793 /* Reset the prompt to the decoded value of prompt_string_pointer. */
3794 reset_readline_prompt ();
3795 #endif
3796
3797 if (current_readline_line == 0)
3798 return (EOF);
3799
3800 current_readline_line_index = 0;
3801 line_len = strlen (current_readline_line);
3802
3803 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
3804 current_readline_line[line_len++] = '\n';
3805 current_readline_line[line_len] = '\0';
3806 }
3807
3808 if (current_readline_line[current_readline_line_index] == 0)
3809 {
3810 free (current_readline_line);
3811 current_readline_line = (char *)NULL;
3812 return (yy_readline_get ());
3813 }
3814 else
3815 {
3816 c = current_readline_line[current_readline_line_index++];
3817 return (c);
3818 }
3819 }
3820
3821 static int
3822 yy_readline_unget (c)
3823 int c;
3824 {
3825 if (current_readline_line_index && current_readline_line)
3826 current_readline_line[--current_readline_line_index] = c;
3827 return (c);
3828 }
3829
3830 void
3831 with_input_from_stdin ()
3832 {
3833 INPUT_STREAM location;
3834
3835 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
3836 {
3837 location.string = current_readline_line;
3838 init_yy_io (yy_readline_get, yy_readline_unget,
3839 st_stdin, "readline stdin", location);
3840 }
3841 }
3842
3843 /* Will we be collecting another input line and printing a prompt? This uses
3844 different conditions than SHOULD_PROMPT(), since readline allows a user to
3845 embed a newline in the middle of the line it collects, which the parser
3846 will interpret as a line break and command delimiter. */
3847 int
3848 parser_will_prompt ()
3849 {
3850 return (current_readline_line == 0 || current_readline_line[current_readline_line_index] == 0);
3851 }
3852
3853 #else /* !READLINE */
3854
3855 void
3856 with_input_from_stdin ()
3857 {
3858 with_input_from_stream (stdin, "stdin");
3859 }
3860 #endif /* !READLINE */
3861
3862 /* **************************************************************** */
3863 /* */
3864 /* Let input come from STRING. STRING is zero terminated. */
3865 /* */
3866 /* **************************************************************** */
3867
3868 static int
3869 yy_string_get ()
3870 {
3871 register char *string;
3872 register unsigned char c;
3873
3874 string = bash_input.location.string;
3875
3876 /* If the string doesn't exist, or is empty, EOF found. */
3877 if (string && *string)
3878 {
3879 c = *string++;
3880 bash_input.location.string = string;
3881 return (c);
3882 }
3883 else
3884 return (EOF);
3885 }
3886
3887 static int
3888 yy_string_unget (c)
3889 int c;
3890 {
3891 *(--bash_input.location.string) = c;
3892 return (c);
3893 }
3894
3895 void
3896 with_input_from_string (string, name)
3897 char *string;
3898 const char *name;
3899 {
3900 INPUT_STREAM location;
3901
3902 location.string = string;
3903 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
3904 }
3905
3906 /* Count the number of characters we've consumed from bash_input.location.string
3907 and read into shell_input_line, but have not returned from shell_getc.
3908 That is the true input location. Rewind bash_input.location.string by
3909 that number of characters, so it points to the last character actually
3910 consumed by the parser. */
3911 static void
3912 rewind_input_string ()
3913 {
3914 int xchars;
3915
3916 /* number of unconsumed characters in the input -- XXX need to take newlines
3917 into account, e.g., $(...\n) */
3918 xchars = shell_input_line_len - shell_input_line_index;
3919 if (bash_input.location.string[-1] == '\n')
3920 xchars++;
3921
3922 /* XXX - how to reflect bash_input.location.string back to string passed to
3923 parse_and_execute or xparse_dolparen? xparse_dolparen needs to know how
3924 far into the string we parsed. parse_and_execute knows where bash_input.
3925 location.string is, and how far from orig_string that is -- that's the
3926 number of characters the command consumed. */
3927
3928 /* bash_input.location.string - xchars should be where we parsed to */
3929 /* need to do more validation on xchars value for sanity -- test cases. */
3930 bash_input.location.string -= xchars;
3931 }
3932
3933 /* **************************************************************** */
3934 /* */
3935 /* Let input come from STREAM. */
3936 /* */
3937 /* **************************************************************** */
3938
3939 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
3940 define, and just use getc/ungetc if it was defined, but since bash
3941 installs most of its signal handlers without the SA_RESTART flag, some
3942 signals received during a read(2) will not cause the read to be restarted.
3943 We will need to restart it ourselves. */
3944
3945 static int
3946 yy_stream_get ()
3947 {
3948 int result;
3949
3950 result = EOF;
3951 if (bash_input.location.file)
3952 {
3953 /* XXX - don't need terminate_immediately; getc_with_restart checks
3954 for terminating signals itself if read returns < 0 */
3955 result = getc_with_restart (bash_input.location.file);
3956 }
3957 return (result);
3958 }
3959
3960 static int
3961 yy_stream_unget (c)
3962 int c;
3963 {
3964 return (ungetc_with_restart (c, bash_input.location.file));
3965 }
3966
3967 void
3968 with_input_from_stream (stream, name)
3969 FILE *stream;
3970 const char *name;
3971 {
3972 INPUT_STREAM location;
3973
3974 location.file = stream;
3975 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
3976 }
3977
3978 typedef struct stream_saver {
3979 struct stream_saver *next;
3980 BASH_INPUT bash_input;
3981 int line;
3982 #if defined (BUFFERED_INPUT)
3983 BUFFERED_STREAM *bstream;
3984 #endif /* BUFFERED_INPUT */
3985 } STREAM_SAVER;
3986
3987 /* The globally known line number. */
3988 int line_number = 0;
3989
3990 /* The line number offset set by assigning to LINENO. Not currently used. */
3991 int line_number_base = 0;
3992
3993 #if defined (COND_COMMAND)
3994 static int cond_lineno;
3995 static int cond_token;
3996 #endif
3997
3998 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
3999
4000 void
4001 push_stream (reset_lineno)
4002 int reset_lineno;
4003 {
4004 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
4005
4006 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
4007
4008 #if defined (BUFFERED_INPUT)
4009 saver->bstream = (BUFFERED_STREAM *)NULL;
4010 /* If we have a buffered stream, clear out buffers[fd]. */
4011 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
4012 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
4013 (BUFFERED_STREAM *)NULL);
4014 #endif /* BUFFERED_INPUT */
4015
4016 saver->line = line_number;
4017 bash_input.name = (char *)NULL;
4018 saver->next = stream_list;
4019 stream_list = saver;
4020 EOF_Reached = 0;
4021 if (reset_lineno)
4022 line_number = 0;
4023 }
4024
4025 void
4026 pop_stream ()
4027 {
4028 if (!stream_list)
4029 EOF_Reached = 1;
4030 else
4031 {
4032 STREAM_SAVER *saver = stream_list;
4033
4034 EOF_Reached = 0;
4035 stream_list = stream_list->next;
4036
4037 init_yy_io (saver->bash_input.getter,
4038 saver->bash_input.ungetter,
4039 saver->bash_input.type,
4040 saver->bash_input.name,
4041 saver->bash_input.location);
4042
4043 #if defined (BUFFERED_INPUT)
4044 /* If we have a buffered stream, restore buffers[fd]. */
4045 /* If the input file descriptor was changed while this was on the
4046 save stack, update the buffered fd to the new file descriptor and
4047 re-establish the buffer <-> bash_input fd correspondence. */
4048 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
4049 {
4050 if (bash_input_fd_changed)
4051 {
4052 bash_input_fd_changed = 0;
4053 if (default_buffered_input >= 0)
4054 {
4055 bash_input.location.buffered_fd = default_buffered_input;
4056 saver->bstream->b_fd = default_buffered_input;
4057 SET_CLOSE_ON_EXEC (default_buffered_input);
4058 }
4059 }
4060 /* XXX could free buffered stream returned as result here. */
4061 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
4062 }
4063 #endif /* BUFFERED_INPUT */
4064
4065 line_number = saver->line;
4066
4067 FREE (saver->bash_input.name);
4068 free (saver);
4069 }
4070 }
4071
4072 /* Return 1 if a stream of type TYPE is saved on the stack. */
4073 int
4074 stream_on_stack (type)
4075 enum stream_type type;
4076 {
4077 register STREAM_SAVER *s;
4078
4079 for (s = stream_list; s; s = s->next)
4080 if (s->bash_input.type == type)
4081 return 1;
4082 return 0;
4083 }
4084
4085 /* Save the current token state and return it in a malloced array. */
4086 int *
4087 save_token_state ()
4088 {
4089 int *ret;
4090
4091 ret = (int *)xmalloc (4 * sizeof (int));
4092 ret[0] = last_read_token;
4093 ret[1] = token_before_that;
4094 ret[2] = two_tokens_ago;
4095 ret[3] = current_token;
4096 return ret;
4097 }
4098
4099 void
4100 restore_token_state (ts)
4101 int *ts;
4102 {
4103 if (ts == 0)
4104 return;
4105 last_read_token = ts[0];
4106 token_before_that = ts[1];
4107 two_tokens_ago = ts[2];
4108 current_token = ts[3];
4109 }
4110
4111 /*
4112 * This is used to inhibit alias expansion and reserved word recognition
4113 * inside case statement pattern lists. A `case statement pattern list' is:
4114 *
4115 * everything between the `in' in a `case word in' and the next ')'
4116 * or `esac'
4117 * everything between a `;;' and the next `)' or `esac'
4118 */
4119
4120 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4121
4122 #define END_OF_ALIAS 0
4123
4124 /*
4125 * Pseudo-global variables used in implementing token-wise alias expansion.
4126 */
4127
4128 /*
4129 * Pushing and popping strings. This works together with shell_getc to
4130 * implement alias expansion on a per-token basis.
4131 */
4132
4133 #define PSH_ALIAS 0x01
4134 #define PSH_DPAREN 0x02
4135 #define PSH_SOURCE 0x04
4136 #define PSH_ARRAY 0x08
4137
4138 typedef struct string_saver {
4139 struct string_saver *next;
4140 int expand_alias; /* Value to set expand_alias to when string is popped. */
4141 char *saved_line;
4142 #if defined (ALIAS)
4143 alias_t *expander; /* alias that caused this line to be pushed. */
4144 #endif
4145 size_t saved_line_size, saved_line_index;
4146 int saved_line_terminator;
4147 int flags;
4148 } STRING_SAVER;
4149
4150 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
4151
4152 /*
4153 * Push the current shell_input_line onto a stack of such lines and make S
4154 * the current input. Used when expanding aliases. EXPAND is used to set
4155 * the value of expand_next_token when the string is popped, so that the
4156 * word after the alias in the original line is handled correctly when the
4157 * alias expands to multiple words. TOKEN is the token that was expanded
4158 * into S; it is saved and used to prevent infinite recursive expansion.
4159 */
4160 static void
4161 push_string (s, expand, ap)
4162 char *s;
4163 int expand;
4164 alias_t *ap;
4165 {
4166 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
4167
4168 temp->expand_alias = expand;
4169 temp->saved_line = shell_input_line;
4170 temp->saved_line_size = shell_input_line_size;
4171 temp->saved_line_index = shell_input_line_index;
4172 temp->saved_line_terminator = shell_input_line_terminator;
4173 temp->flags = 0;
4174 #if defined (ALIAS)
4175 temp->expander = ap;
4176 if (ap)
4177 temp->flags = PSH_ALIAS;
4178 #endif
4179 temp->next = pushed_string_list;
4180 pushed_string_list = temp;
4181
4182 #if defined (ALIAS)
4183 if (ap)
4184 ap->flags |= AL_BEINGEXPANDED;
4185 #endif
4186
4187 shell_input_line = s;
4188 shell_input_line_size = STRLEN (s);
4189 shell_input_line_index = 0;
4190 shell_input_line_terminator = '\0';
4191 #if 0
4192 parser_state &= ~PST_ALEXPNEXT; /* XXX */
4193 #endif
4194
4195 set_line_mbstate ();
4196 }
4197
4198 /*
4199 * Make the top of the pushed_string stack be the current shell input.
4200 * Only called when there is something on the stack. Called from shell_getc
4201 * when it thinks it has consumed the string generated by an alias expansion
4202 * and needs to return to the original input line.
4203 */
4204 static void
4205 pop_string ()
4206 {
4207 STRING_SAVER *t;
4208
4209 FREE (shell_input_line);
4210 shell_input_line = pushed_string_list->saved_line;
4211 shell_input_line_index = pushed_string_list->saved_line_index;
4212 shell_input_line_size = pushed_string_list->saved_line_size;
4213 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
4214
4215 if (pushed_string_list->expand_alias)
4216 parser_state |= PST_ALEXPNEXT;
4217 else
4218 parser_state &= ~PST_ALEXPNEXT;
4219
4220 t = pushed_string_list;
4221 pushed_string_list = pushed_string_list->next;
4222
4223 #if defined (ALIAS)
4224 if (t->expander)
4225 t->expander->flags &= ~AL_BEINGEXPANDED;
4226 #endif
4227
4228 free ((char *)t);
4229
4230 set_line_mbstate ();
4231 }
4232
4233 static void
4234 free_string_list ()
4235 {
4236 register STRING_SAVER *t, *t1;
4237
4238 for (t = pushed_string_list; t; )
4239 {
4240 t1 = t->next;
4241 FREE (t->saved_line);
4242 #if defined (ALIAS)
4243 if (t->expander)
4244 t->expander->flags &= ~AL_BEINGEXPANDED;
4245 #endif
4246 free ((char *)t);
4247 t = t1;
4248 }
4249 pushed_string_list = (STRING_SAVER *)NULL;
4250 }
4251
4252 #endif /* ALIAS || DPAREN_ARITHMETIC */
4253
4254 void
4255 free_pushed_string_input ()
4256 {
4257 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4258 free_string_list ();
4259 #endif
4260 }
4261
4262 int
4263 parser_expanding_alias ()
4264 {
4265 return (expanding_alias ());
4266 }
4267
4268 void
4269 parser_save_alias ()
4270 {
4271 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4272 push_string ((char *)NULL, 0, (alias_t *)NULL);
4273 pushed_string_list->flags = PSH_SOURCE; /* XXX - for now */
4274 #else
4275 ;
4276 #endif
4277 }
4278
4279 void
4280 parser_restore_alias ()
4281 {
4282 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4283 if (pushed_string_list)
4284 pop_string ();
4285 #else
4286 ;
4287 #endif
4288 }
4289
4290 #if defined (ALIAS)
4291 /* Before freeing AP, make sure that there aren't any cases of pointer
4292 aliasing that could cause us to reference freed memory later on. */
4293 void
4294 clear_string_list_expander (ap)
4295 alias_t *ap;
4296 {
4297 register STRING_SAVER *t;
4298
4299 for (t = pushed_string_list; t; t = t->next)
4300 {
4301 if (t->expander && t->expander == ap)
4302 t->expander = 0;
4303 }
4304 }
4305 #endif
4306
4307 void
4308 clear_shell_input_line ()
4309 {
4310 if (shell_input_line)
4311 shell_input_line[shell_input_line_index = 0] = '\0';
4312 }
4313
4314 /* Return a line of text, taken from wherever yylex () reads input.
4315 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
4316 is non-zero, we remove unquoted \<newline> pairs. This is used by
4317 read_secondary_line to read here documents. */
4318 static char *
4319 read_a_line (remove_quoted_newline)
4320 int remove_quoted_newline;
4321 {
4322 static char *line_buffer = (char *)NULL;
4323 static int buffer_size = 0;
4324 int indx, c, peekc, pass_next;
4325
4326 #if defined (READLINE)
4327 if (no_line_editing && SHOULD_PROMPT ())
4328 #else
4329 if (SHOULD_PROMPT ())
4330 #endif
4331 print_prompt ();
4332
4333 pass_next = indx = 0;
4334 while (1)
4335 {
4336 /* Allow immediate exit if interrupted during input. */
4337 QUIT;
4338
4339 c = yy_getc ();
4340
4341 /* Ignore null bytes in input. */
4342 if (c == 0)
4343 {
4344 #if 0
4345 internal_warning ("read_a_line: ignored null byte in input");
4346 #endif
4347 continue;
4348 }
4349
4350 /* If there is no more input, then we return NULL. */
4351 if (c == EOF)
4352 {
4353 if (interactive && bash_input.type == st_stream)
4354 clearerr (stdin);
4355 if (indx == 0)
4356 return ((char *)NULL);
4357 c = '\n';
4358 }
4359
4360 /* `+2' in case the final character in the buffer is a newline or we
4361 have to handle CTLESC or CTLNUL. */
4362 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
4363
4364 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
4365 here document with an unquoted delimiter. In this case,
4366 the line will be expanded as if it were in double quotes.
4367 We allow a backslash to escape the next character, but we
4368 need to treat the backslash specially only if a backslash
4369 quoting a backslash-newline pair appears in the line. */
4370 if (pass_next)
4371 {
4372 line_buffer[indx++] = c;
4373 pass_next = 0;
4374 }
4375 else if (c == '\\' && remove_quoted_newline)
4376 {
4377 QUIT;
4378 peekc = yy_getc ();
4379 if (peekc == '\n')
4380 {
4381 line_number++;
4382 continue; /* Make the unquoted \<newline> pair disappear. */
4383 }
4384 else
4385 {
4386 yy_ungetc (peekc);
4387 pass_next = 1;
4388 line_buffer[indx++] = c; /* Preserve the backslash. */
4389 }
4390 }
4391 else
4392 {
4393 /* remove_quoted_newline is non-zero if the here-document delimiter
4394 is unquoted. In this case, we will be expanding the lines and
4395 need to make sure CTLESC and CTLNUL in the input are quoted. */
4396 if (remove_quoted_newline && (c == CTLESC || c == CTLNUL))
4397 line_buffer[indx++] = CTLESC;
4398 line_buffer[indx++] = c;
4399 }
4400
4401 if (c == '\n')
4402 {
4403 line_buffer[indx] = '\0';
4404 return (line_buffer);
4405 }
4406 }
4407 }
4408
4409 /* Return a line as in read_a_line (), but insure that the prompt is
4410 the secondary prompt. This is used to read the lines of a here
4411 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
4412 newlines quoted with backslashes while reading the line. It is
4413 non-zero unless the delimiter of the here document was quoted. */
4414 char *
4415 read_secondary_line (remove_quoted_newline)
4416 int remove_quoted_newline;
4417 {
4418 char *ret;
4419 int n, c;
4420
4421 prompt_string_pointer = &ps2_prompt;
4422 if (SHOULD_PROMPT())
4423 prompt_again ();
4424 ret = read_a_line (remove_quoted_newline);
4425 #if defined (HISTORY)
4426 if (ret && remember_on_history && (parser_state & PST_HEREDOC))
4427 {
4428 /* To make adding the here-document body right, we need to rely on
4429 history_delimiting_chars() returning \n for the first line of the
4430 here-document body and the null string for the second and subsequent
4431 lines, so we avoid double newlines.
4432 current_command_line_count == 2 for the first line of the body. */
4433
4434 current_command_line_count++;
4435 maybe_add_history (ret);
4436 }
4437 #endif /* HISTORY */
4438 return ret;
4439 }
4440
4441 /* **************************************************************** */
4442 /* */
4443 /* YYLEX () */
4444 /* */
4445 /* **************************************************************** */
4446
4447 /* Reserved words. These are only recognized as the first word of a
4448 command. */
4449 STRING_INT_ALIST word_token_alist[] = {
4450 { "if", IF },
4451 { "then", THEN },
4452 { "else", ELSE },
4453 { "elif", ELIF },
4454 { "fi", FI },
4455 { "case", CASE },
4456 { "esac", ESAC },
4457 { "for", FOR },
4458 #if defined (SELECT_COMMAND)
4459 { "select", SELECT },
4460 #endif
4461 { "while", WHILE },
4462 { "until", UNTIL },
4463 { "do", DO },
4464 { "done", DONE },
4465 { "in", IN },
4466 { "function", FUNCTION },
4467 #if defined (COMMAND_TIMING)
4468 { "time", TIME },
4469 #endif
4470 { "{", '{' },
4471 { "}", '}' },
4472 { "!", BANG },
4473 #if defined (COND_COMMAND)
4474 { "[[", COND_START },
4475 { "]]", COND_END },
4476 #endif
4477 #if defined (COPROCESS_SUPPORT)
4478 { "coproc", COPROC },
4479 #endif
4480 { (char *)NULL, 0}
4481 };
4482
4483 /* other tokens that can be returned by read_token() */
4484 STRING_INT_ALIST other_token_alist[] = {
4485 /* Multiple-character tokens with special values */
4486 { "--", TIMEIGN },
4487 { "-p", TIMEOPT },
4488 { "&&", AND_AND },
4489 { "||", OR_OR },
4490 { ">>", GREATER_GREATER },
4491 { "<<", LESS_LESS },
4492 { "<&", LESS_AND },
4493 { ">&", GREATER_AND },
4494 { ";;", SEMI_SEMI },
4495 { ";&", SEMI_AND },
4496 { ";;&", SEMI_SEMI_AND },
4497 { "<<-", LESS_LESS_MINUS },
4498 { "<<<", LESS_LESS_LESS },
4499 { "&>", AND_GREATER },
4500 { "&>>", AND_GREATER_GREATER },
4501 { "<>", LESS_GREATER },
4502 { ">|", GREATER_BAR },
4503 { "|&", BAR_AND },
4504 { "EOF", yacc_EOF },
4505 /* Tokens whose value is the character itself */
4506 { ">", '>' },
4507 { "<", '<' },
4508 { "-", '-' },
4509 { "{", '{' },
4510 { "}", '}' },
4511 { ";", ';' },
4512 { "(", '(' },
4513 { ")", ')' },
4514 { "|", '|' },
4515 { "&", '&' },
4516 { "newline", '\n' },
4517 { (char *)NULL, 0}
4518 };
4519
4520 /* others not listed here:
4521 WORD look at yylval.word
4522 ASSIGNMENT_WORD look at yylval.word
4523 NUMBER look at yylval.number
4524 ARITH_CMD look at yylval.word_list
4525 ARITH_FOR_EXPRS look at yylval.word_list
4526 COND_CMD look at yylval.command
4527 */
4528
4529 /* These are used by read_token_word, but appear up here so that shell_getc
4530 can use them to decide when to add otherwise blank lines to the history. */
4531
4532 /* The primary delimiter stack. */
4533 struct dstack dstack = { (char *)NULL, 0, 0 };
4534
4535 /* A temporary delimiter stack to be used when decoding prompt strings.
4536 This is needed because command substitutions in prompt strings (e.g., PS2)
4537 can screw up the parser's quoting state. */
4538 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
4539
4540 /* Macro for accessing the top delimiter on the stack. Returns the
4541 delimiter or zero if none. */
4542 #define current_delimiter(ds) \
4543 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
4544
4545 #define push_delimiter(ds, character) \
4546 do \
4547 { \
4548 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
4549 ds.delimiters = (char *)xrealloc \
4550 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
4551 ds.delimiters[ds.delimiter_depth] = character; \
4552 ds.delimiter_depth++; \
4553 } \
4554 while (0)
4555
4556 #define pop_delimiter(ds) ds.delimiter_depth--
4557
4558 /* Return the next shell input character. This always reads characters
4559 from shell_input_line; when that line is exhausted, it is time to
4560 read the next line. This is called by read_token when the shell is
4561 processing normal command input. */
4562
4563 /* This implements one-character lookahead/lookbehind across physical input
4564 lines, to avoid something being lost because it's pushed back with
4565 shell_ungetc when we're at the start of a line. */
4566 static int eol_ungetc_lookahead = 0;
4567
4568 static int unquoted_backslash = 0;
4569
4570 static int
4571 shell_getc (remove_quoted_newline)
4572 int remove_quoted_newline;
4573 {
4574 register int i;
4575 int c, truncating, last_was_backslash;
4576 unsigned char uc;
4577
4578 QUIT;
4579
4580 last_was_backslash = 0;
4581 if (sigwinch_received)
4582 {
4583 sigwinch_received = 0;
4584 get_new_window_size (0, (int *)0, (int *)0);
4585 }
4586
4587 if (eol_ungetc_lookahead)
4588 {
4589 c = eol_ungetc_lookahead;
4590 eol_ungetc_lookahead = 0;
4591 return (c);
4592 }
4593
4594 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4595 /* If shell_input_line[shell_input_line_index] == 0, but there is
4596 something on the pushed list of strings, then we don't want to go
4597 off and get another line. We let the code down below handle it. */
4598
4599 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
4600 (pushed_string_list == (STRING_SAVER *)NULL)))
4601 #else /* !ALIAS && !DPAREN_ARITHMETIC */
4602 if (!shell_input_line || !shell_input_line[shell_input_line_index])
4603 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
4604 {
4605 line_number++;
4606
4607 /* Let's not let one really really long line blow up memory allocation */
4608 if (shell_input_line && shell_input_line_size >= 32768)
4609 {
4610 free (shell_input_line);
4611 shell_input_line = 0;
4612 shell_input_line_size = 0;
4613 }
4614
4615 restart_read:
4616
4617 /* Allow immediate exit if interrupted during input. */
4618 QUIT;
4619
4620 i = truncating = 0;
4621 shell_input_line_terminator = 0;
4622
4623 /* If the shell is interactive, but not currently printing a prompt
4624 (interactive_shell && interactive == 0), we don't want to print
4625 notifies or cleanup the jobs -- we want to defer it until we do
4626 print the next prompt. */
4627 if (interactive_shell == 0 || SHOULD_PROMPT())
4628 {
4629 #if defined (JOB_CONTROL)
4630 /* This can cause a problem when reading a command as the result
4631 of a trap, when the trap is called from flush_child. This call
4632 had better not cause jobs to disappear from the job table in
4633 that case, or we will have big trouble. */
4634 notify_and_cleanup ();
4635 #else /* !JOB_CONTROL */
4636 cleanup_dead_jobs ();
4637 #endif /* !JOB_CONTROL */
4638 }
4639
4640 #if defined (READLINE)
4641 if (no_line_editing && SHOULD_PROMPT())
4642 #else
4643 if (SHOULD_PROMPT())
4644 #endif
4645 print_prompt ();
4646
4647 if (bash_input.type == st_stream)
4648 clearerr (stdin);
4649
4650 while (1)
4651 {
4652 c = yy_getc ();
4653
4654 /* Allow immediate exit if interrupted during input. */
4655 QUIT;
4656
4657 if (c == '\0')
4658 {
4659 #if 0
4660 internal_warning ("shell_getc: ignored null byte in input");
4661 #endif
4662 /* If we get EOS while parsing a string, treat it as EOF so we
4663 don't just keep looping. Happens very rarely */
4664 if (bash_input.type == st_string)
4665 {
4666 if (i == 0)
4667 shell_input_line_terminator = EOF;
4668 shell_input_line[i] = '\0';
4669 c = EOF;
4670 break;
4671 }
4672 continue;
4673 }
4674
4675 /* Theoretical overflow */
4676 /* If we can't put 256 bytes more into the buffer, allocate
4677 everything we can and fill it as full as we can. */
4678 /* XXX - we ignore rest of line using `truncating' flag */
4679 if (shell_input_line_size > (SIZE_MAX - 256))
4680 {
4681 size_t n;
4682
4683 n = SIZE_MAX - i; /* how much more can we put into the buffer? */
4684 if (n <= 2) /* we have to save 1 for the newline added below */
4685 {
4686 if (truncating == 0)
4687 internal_warning(_("shell_getc: shell_input_line_size (%zu) exceeds SIZE_MAX (%lu): line truncated"), shell_input_line_size, (unsigned long)SIZE_MAX);
4688 shell_input_line[i] = '\0';
4689 truncating = 1;
4690 }
4691 if (shell_input_line_size < SIZE_MAX)
4692 {
4693 shell_input_line_size = SIZE_MAX;
4694 shell_input_line = xrealloc (shell_input_line, shell_input_line_size);
4695 }
4696 }
4697 else
4698 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
4699
4700 if (c == EOF)
4701 {
4702 if (bash_input.type == st_stream)
4703 clearerr (stdin);
4704
4705 if (i == 0)
4706 shell_input_line_terminator = EOF;
4707
4708 shell_input_line[i] = '\0';
4709 break;
4710 }
4711
4712 if (truncating == 0 || c == '\n')
4713 shell_input_line[i++] = c;
4714
4715 if (c == '\n')
4716 {
4717 shell_input_line[--i] = '\0';
4718 current_command_line_count++;
4719 break;
4720 }
4721
4722 last_was_backslash = last_was_backslash == 0 && c == '\\';
4723 }
4724
4725 shell_input_line_index = 0;
4726 shell_input_line_len = i; /* == strlen (shell_input_line) */
4727
4728 set_line_mbstate ();
4729
4730 #if defined (HISTORY)
4731 if (remember_on_history && shell_input_line && shell_input_line[0])
4732 {
4733 char *expansions;
4734 # if defined (BANG_HISTORY)
4735 /* If the current delimiter is a single quote, we should not be
4736 performing history expansion, even if we're on a different
4737 line from the original single quote. */
4738 if (current_delimiter (dstack) == '\'')
4739 history_quoting_state = '\'';
4740 else if (current_delimiter (dstack) == '"')
4741 history_quoting_state = '"';
4742 else
4743 history_quoting_state = 0;
4744 # endif
4745 /* Calling with a third argument of 1 allows remember_on_history to
4746 determine whether or not the line is saved to the history list */
4747 expansions = pre_process_line (shell_input_line, 1, 1);
4748 # if defined (BANG_HISTORY)
4749 history_quoting_state = 0;
4750 # endif
4751 if (expansions != shell_input_line)
4752 {
4753 free (shell_input_line);
4754 shell_input_line = expansions;
4755 shell_input_line_len = shell_input_line ?
4756 strlen (shell_input_line) : 0;
4757 if (shell_input_line_len == 0)
4758 current_command_line_count--;
4759
4760 /* We have to force the xrealloc below because we don't know
4761 the true allocated size of shell_input_line anymore. */
4762 shell_input_line_size = shell_input_line_len;
4763
4764 set_line_mbstate ();
4765 }
4766 }
4767 /* Try to do something intelligent with blank lines encountered while
4768 entering multi-line commands. XXX - this is grotesque */
4769 else if (remember_on_history && shell_input_line &&
4770 shell_input_line[0] == '\0' &&
4771 current_command_line_count > 1)
4772 {
4773 if (current_delimiter (dstack))
4774 /* We know shell_input_line[0] == 0 and we're reading some sort of
4775 quoted string. This means we've got a line consisting of only
4776 a newline in a quoted string. We want to make sure this line
4777 gets added to the history. */
4778 maybe_add_history (shell_input_line);
4779 else
4780 {
4781 char *hdcs;
4782 hdcs = history_delimiting_chars (shell_input_line);
4783 if (hdcs && hdcs[0] == ';')
4784 maybe_add_history (shell_input_line);
4785 }
4786 }
4787
4788 #endif /* HISTORY */
4789
4790 if (shell_input_line)
4791 {
4792 /* Lines that signify the end of the shell's input should not be
4793 echoed. We should not echo lines while parsing command
4794 substitutions with recursive calls into the parsing engine; those
4795 should only be echoed once when we read the word. That is the
4796 reason for the test against shell_eof_token, which is set to a
4797 right paren when parsing the contents of command substitutions. */
4798 if (echo_input_at_read && (shell_input_line[0] ||
4799 shell_input_line_terminator != EOF) &&
4800 shell_eof_token == 0)
4801 fprintf (stderr, "%s\n", shell_input_line);
4802 }
4803 else
4804 {
4805 shell_input_line_size = 0;
4806 prompt_string_pointer = &current_prompt_string;
4807 if (SHOULD_PROMPT ())
4808 prompt_again ();
4809 goto restart_read;
4810 }
4811
4812 /* Add the newline to the end of this string, iff the string does
4813 not already end in an EOF character. */
4814 if (shell_input_line_terminator != EOF)
4815 {
4816 if (shell_input_line_size < SIZE_MAX-3 && (shell_input_line_len+3 > shell_input_line_size))
4817 shell_input_line = (char *)xrealloc (shell_input_line,
4818 1 + (shell_input_line_size += 2));
4819
4820 /* Don't add a newline to a string that ends with a backslash if we're
4821 going to be removing quoted newlines, since that will eat the
4822 backslash. Add another backslash instead (will be removed by
4823 word expansion). */
4824 if (bash_input.type == st_string && expanding_alias() == 0 && last_was_backslash && c == EOF && remove_quoted_newline)
4825 shell_input_line[shell_input_line_len] = '\\';
4826 else
4827 shell_input_line[shell_input_line_len] = '\n';
4828 shell_input_line[shell_input_line_len + 1] = '\0';
4829
4830 #if 0
4831 set_line_mbstate (); /* XXX - this is wasteful */
4832 #else
4833 # if defined (HANDLE_MULTIBYTE)
4834 /* This is kind of an abstraction violation, but there's no need to
4835 go through the entire shell_input_line again with a call to
4836 set_line_mbstate(). */
4837 if (shell_input_line_len + 2 > shell_input_line_propsize)
4838 {
4839 shell_input_line_propsize = shell_input_line_len + 2;
4840 shell_input_line_property = (char *)xrealloc (shell_input_line_property,
4841 shell_input_line_propsize);
4842 }
4843 shell_input_line_property[shell_input_line_len] = 1;
4844 # endif
4845 #endif
4846 }
4847 }
4848
4849 next_alias_char:
4850 if (shell_input_line_index == 0)
4851 unquoted_backslash = 0;
4852
4853 uc = shell_input_line[shell_input_line_index];
4854
4855 if (uc)
4856 {
4857 unquoted_backslash = unquoted_backslash == 0 && uc == '\\';
4858 shell_input_line_index++;
4859 }
4860
4861 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4862 /* If UC is NULL, we have reached the end of the current input string. If
4863 pushed_string_list is non-empty, it's time to pop to the previous string
4864 because we have fully consumed the result of the last alias expansion.
4865 Do it transparently; just return the next character of the string popped
4866 to. */
4867 /* If pushed_string_list != 0 but pushed_string_list->expander == 0 (not
4868 currently tested) and the flags value is not PSH_SOURCE, we are not
4869 parsing an alias, we have just saved one (push_string, when called by
4870 the parse_dparen code) In this case, just go on as well. The PSH_SOURCE
4871 case is handled below. */
4872
4873 /* If we're at the end of an alias expansion add a space to make sure that
4874 the alias remains marked as being in use while we expand its last word.
4875 This makes sure that pop_string doesn't mark the alias as not in use
4876 before the string resulting from the alias expansion is tokenized and
4877 checked for alias expansion, preventing recursion. At this point, the
4878 last character in shell_input_line is the last character of the alias
4879 expansion. We test that last character to determine whether or not to
4880 return the space that will delimit the token and postpone the pop_string.
4881 This set of conditions duplicates what used to be in mk_alexpansion ()
4882 below, with the addition that we don't add a space if we're currently
4883 reading a quoted string or in a shell comment. */
4884 #ifndef OLD_ALIAS_HACK
4885 if (uc == 0 && pushed_string_list && pushed_string_list->flags != PSH_SOURCE &&
4886 pushed_string_list->flags != PSH_DPAREN &&
4887 (parser_state & PST_COMMENT) == 0 &&
4888 (parser_state & PST_ENDALIAS) == 0 && /* only once */
4889 shell_input_line_index > 0 &&
4890 shellblank (shell_input_line[shell_input_line_index-1]) == 0 &&
4891 shell_input_line[shell_input_line_index-1] != '\n' &&
4892 unquoted_backslash == 0 &&
4893 shellmeta (shell_input_line[shell_input_line_index-1]) == 0 &&
4894 (current_delimiter (dstack) != '\'' && current_delimiter (dstack) != '"'))
4895 {
4896 parser_state |= PST_ENDALIAS;
4897 return ' '; /* END_ALIAS */
4898 }
4899 #endif
4900
4901 pop_alias:
4902 /* This case works for PSH_DPAREN as well */
4903 if (uc == 0 && pushed_string_list && pushed_string_list->flags != PSH_SOURCE)
4904 {
4905 parser_state &= ~PST_ENDALIAS;
4906 pop_string ();
4907 uc = shell_input_line[shell_input_line_index];
4908 if (uc)
4909 shell_input_line_index++;
4910 }
4911 #endif /* ALIAS || DPAREN_ARITHMETIC */
4912
4913 if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n')
4914 {
4915 if (SHOULD_PROMPT ())
4916 prompt_again ();
4917 line_number++;
4918
4919 /* What do we do here if we're expanding an alias whose definition
4920 includes an escaped newline? If that's the last character in the
4921 alias expansion, we just pop the pushed string list (recall that
4922 we inhibit the appending of a space if newline is the last
4923 character). If it's not the last character, we need to consume the
4924 quoted newline and move to the next character in the expansion. */
4925 #if defined (ALIAS)
4926 if (expanding_alias () && shell_input_line[shell_input_line_index+1] == '\0')
4927 {
4928 uc = 0;
4929 goto pop_alias;
4930 }
4931 else if (expanding_alias () && shell_input_line[shell_input_line_index+1] != '\0')
4932 {
4933 shell_input_line_index++; /* skip newline */
4934 goto next_alias_char; /* and get next character */
4935 }
4936 else
4937 #endif
4938 goto restart_read;
4939 }
4940
4941 if (uc == 0 && shell_input_line_terminator == EOF)
4942 return ((shell_input_line_index != 0) ? '\n' : EOF);
4943
4944 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4945 /* We already know that we are not parsing an alias expansion because of the
4946 check for expanding_alias() above. This knows how parse_and_execute
4947 handles switching to st_string input while an alias is being expanded,
4948 hence the check for pushed_string_list without pushed_string_list->expander
4949 and the check for PSH_SOURCE as pushed_string_list->flags.
4950 parse_and_execute and parse_string both change the input type to st_string
4951 and place the string to be parsed and executed into location.string, so
4952 we should not stop reading that until the pointer is '\0'.
4953 The check for shell_input_line_terminator may be superfluous.
4954
4955 This solves the problem of `.' inside a multi-line alias with embedded
4956 newlines executing things out of order. */
4957 if (uc == 0 && bash_input.type == st_string && *bash_input.location.string &&
4958 pushed_string_list && pushed_string_list->flags == PSH_SOURCE &&
4959 shell_input_line_terminator == 0)
4960 {
4961 shell_input_line_index = 0;
4962 goto restart_read;
4963 }
4964 #endif
4965
4966 return (uc);
4967 }
4968
4969 /* Put C back into the input for the shell. This might need changes for
4970 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
4971 character different than we read, shell_input_line_property doesn't need
4972 to change when manipulating shell_input_line. The define for
4973 last_shell_getc_is_singlebyte should take care of it, though. */
4974 static void
4975 shell_ungetc (c)
4976 int c;
4977 {
4978 if (shell_input_line && shell_input_line_index)
4979 shell_input_line[--shell_input_line_index] = c;
4980 else
4981 eol_ungetc_lookahead = c;
4982 }
4983
4984 char *
4985 parser_remaining_input ()
4986 {
4987 if (shell_input_line == 0)
4988 return 0;
4989 if ((int)shell_input_line_index < 0 || shell_input_line_index >= shell_input_line_len)
4990 return ""; /* XXX */
4991 return (shell_input_line + shell_input_line_index);
4992 }
4993
4994 #ifdef INCLUDE_UNUSED
4995 /* Back the input pointer up by one, effectively `ungetting' a character. */
4996 static void
4997 shell_ungetchar ()
4998 {
4999 if (shell_input_line && shell_input_line_index)
5000 shell_input_line_index--;
5001 }
5002 #endif
5003
5004 /* Discard input until CHARACTER is seen, then push that character back
5005 onto the input stream. */
5006 static void
5007 discard_until (character)
5008 int character;
5009 {
5010 int c;
5011
5012 while ((c = shell_getc (0)) != EOF && c != character)
5013 ;
5014
5015 if (c != EOF)
5016 shell_ungetc (c);
5017 }
5018
5019 void
5020 execute_variable_command (command, vname)
5021 char *command, *vname;
5022 {
5023 char *last_lastarg;
5024 sh_parser_state_t ps;
5025
5026 save_parser_state (&ps);
5027 last_lastarg = get_string_value ("_");
5028 if (last_lastarg)
5029 last_lastarg = savestring (last_lastarg);
5030
5031 parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST);
5032
5033 restore_parser_state (&ps);
5034 bind_variable ("_", last_lastarg, 0);
5035 FREE (last_lastarg);
5036
5037 if (token_to_read == '\n') /* reset_parser was called */
5038 token_to_read = 0;
5039 }
5040
5041 void
5042 push_token (x)
5043 int x;
5044 {
5045 two_tokens_ago = token_before_that;
5046 token_before_that = last_read_token;
5047 last_read_token = current_token;
5048
5049 current_token = x;
5050 }
5051
5052 /* Place to remember the token. We try to keep the buffer
5053 at a reasonable size, but it can grow. */
5054 static char *token = (char *)NULL;
5055
5056 /* Current size of the token buffer. */
5057 static int token_buffer_size;
5058
5059 /* Command to read_token () explaining what we want it to do. */
5060 #define READ 0
5061 #define RESET 1
5062 #define prompt_is_ps1 \
5063 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
5064
5065 /* Function for yyparse to call. yylex keeps track of
5066 the last two tokens read, and calls read_token. */
5067 static int
5068 yylex ()
5069 {
5070 if (interactive && (current_token == 0 || current_token == '\n'))
5071 {
5072 /* Before we print a prompt, we might have to check mailboxes.
5073 We do this only if it is time to do so. Notice that only here
5074 is the mail alarm reset; nothing takes place in check_mail ()
5075 except the checking of mail. Please don't change this. */
5076 if (prompt_is_ps1 && parse_and_execute_level == 0 && time_to_check_mail ())
5077 {
5078 check_mail ();
5079 reset_mail_timer ();
5080 }
5081
5082 /* Avoid printing a prompt if we're not going to read anything, e.g.
5083 after resetting the parser with read_token (RESET). */
5084 if (token_to_read == 0 && SHOULD_PROMPT ())
5085 prompt_again ();
5086 }
5087
5088 two_tokens_ago = token_before_that;
5089 token_before_that = last_read_token;
5090 last_read_token = current_token;
5091 current_token = read_token (READ);
5092
5093 if ((parser_state & PST_EOFTOKEN) && current_token == shell_eof_token)
5094 {
5095 current_token = yacc_EOF;
5096 if (bash_input.type == st_string)
5097 rewind_input_string ();
5098 }
5099 parser_state &= ~PST_EOFTOKEN; /* ??? */
5100
5101 return (current_token);
5102 }
5103
5104 /* When non-zero, we have read the required tokens
5105 which allow ESAC to be the next one read. */
5106 static int esacs_needed_count;
5107
5108 /* When non-zero, we can read IN as an acceptable token, regardless of how
5109 many newlines we read. */
5110 static int expecting_in_token;
5111
5112 static void
5113 push_heredoc (r)
5114 REDIRECT *r;
5115 {
5116 if (need_here_doc >= HEREDOC_MAX)
5117 {
5118 last_command_exit_value = EX_BADUSAGE;
5119 need_here_doc = 0;
5120 report_syntax_error (_("maximum here-document count exceeded"));
5121 reset_parser ();
5122 exit_shell (last_command_exit_value);
5123 }
5124 redir_stack[need_here_doc++] = r;
5125 }
5126
5127 void
5128 gather_here_documents ()
5129 {
5130 int r;
5131
5132 r = 0;
5133 here_doc_first_line = 1;
5134 while (need_here_doc > 0)
5135 {
5136 parser_state |= PST_HEREDOC;
5137 make_here_document (redir_stack[r++], line_number);
5138 parser_state &= ~PST_HEREDOC;
5139 need_here_doc--;
5140 redir_stack[r - 1] = 0; /* XXX */
5141 }
5142 here_doc_first_line = 0; /* just in case */
5143 }
5144
5145 /* When non-zero, an open-brace used to create a group is awaiting a close
5146 brace partner. */
5147 static int open_brace_count;
5148
5149 /* In the following three macros, `token' is always last_read_token */
5150
5151 /* Are we in the middle of parsing a redirection where we are about to read
5152 a word? This is used to make sure alias expansion doesn't happen in the
5153 middle of a redirection, even though we're parsing a simple command. */
5154 #define parsing_redirection(token) \
5155 (token == '<' || token == '>' || \
5156 token == GREATER_GREATER || token == GREATER_BAR || \
5157 token == LESS_GREATER || token == LESS_LESS_MINUS || \
5158 token == LESS_LESS || token == LESS_LESS_LESS || \
5159 token == LESS_AND || token == GREATER_AND || token == AND_GREATER)
5160
5161 /* Is `token' one that will allow a WORD to be read in a command position?
5162 We can read a simple command name on which we should attempt alias expansion
5163 or we can read an assignment statement. */
5164 #define command_token_position(token) \
5165 (((token) == ASSIGNMENT_WORD) || \
5166 ((parser_state&PST_REDIRLIST) && parsing_redirection(token) == 0) || \
5167 ((token) != SEMI_SEMI && (token) != SEMI_AND && (token) != SEMI_SEMI_AND && reserved_word_acceptable(token)))
5168
5169 /* Are we in a position where we can read an assignment statement? */
5170 #define assignment_acceptable(token) \
5171 (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0))
5172
5173 /* Check to see if TOKEN is a reserved word and return the token
5174 value if it is. */
5175 #define CHECK_FOR_RESERVED_WORD(tok) \
5176 do { \
5177 if (!dollar_present && !quoted && \
5178 reserved_word_acceptable (last_read_token)) \
5179 { \
5180 int i; \
5181 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
5182 if (STREQ (tok, word_token_alist[i].word)) \
5183 { \
5184 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
5185 break; \
5186 if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \
5187 break; \
5188 if ((parser_state & PST_CASEPAT) && last_read_token == '|' && word_token_alist[i].token == ESAC) \
5189 break; /* Posix grammar rule 4 */ \
5190 if (word_token_alist[i].token == ESAC) \
5191 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
5192 else if (word_token_alist[i].token == CASE) \
5193 parser_state |= PST_CASESTMT; \
5194 else if (word_token_alist[i].token == COND_END) \
5195 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
5196 else if (word_token_alist[i].token == COND_START) \
5197 parser_state |= PST_CONDCMD; \
5198 else if (word_token_alist[i].token == '{') \
5199 open_brace_count++; \
5200 else if (word_token_alist[i].token == '}' && open_brace_count) \
5201 open_brace_count--; \
5202 return (word_token_alist[i].token); \
5203 } \
5204 } \
5205 } while (0)
5206
5207 #if defined (ALIAS)
5208
5209 /* OK, we have a token. Let's try to alias expand it, if (and only if)
5210 it's eligible.
5211
5212 It is eligible for expansion if EXPAND_ALIASES is set, and
5213 the token is unquoted and the last token read was a command
5214 separator (or expand_next_token is set), and we are currently
5215 processing an alias (pushed_string_list is non-empty) and this
5216 token is not the same as the current or any previously
5217 processed alias.
5218
5219 Special cases that disqualify:
5220 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
5221
5222 static char *
5223 mk_alexpansion (s)
5224 char *s;
5225 {
5226 int l;
5227 char *r;
5228
5229 l = strlen (s);
5230 r = xmalloc (l + 2);
5231 strcpy (r, s);
5232 #ifdef OLD_ALIAS_HACK
5233 /* If the last character in the alias is a newline, don't add a trailing
5234 space to the expansion. Works with shell_getc above. */
5235 /* Need to do something about the case where the alias expansion contains
5236 an unmatched quoted string, since appending this space affects the
5237 subsequent output. */
5238 if (l > 0 && r[l - 1] != ' ' && r[l - 1] != '\n' && shellmeta(r[l - 1]) == 0)
5239 r[l++] = ' ';
5240 #endif
5241 r[l] = '\0';
5242 return r;
5243 }
5244
5245 static int
5246 alias_expand_token (tokstr)
5247 char *tokstr;
5248 {
5249 char *expanded;
5250 alias_t *ap;
5251
5252 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
5253 (parser_state & PST_CASEPAT) == 0)
5254 {
5255 ap = find_alias (tokstr);
5256
5257 /* Currently expanding this token. */
5258 if (ap && (ap->flags & AL_BEINGEXPANDED))
5259 return (NO_EXPANSION);
5260
5261 #ifdef OLD_ALIAS_HACK
5262 /* mk_alexpansion puts an extra space on the end of the alias expansion,
5263 so the lookahead by the parser works right (the alias needs to remain
5264 `in use' while parsing its last word to avoid alias recursion for
5265 something like "alias echo=echo"). If this gets changed, make sure
5266 the code in shell_getc that deals with reaching the end of an
5267 expanded alias is changed with it. */
5268 #endif
5269 expanded = ap ? mk_alexpansion (ap->value) : (char *)NULL;
5270
5271 if (expanded)
5272 {
5273 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
5274 return (RE_READ_TOKEN);
5275 }
5276 else
5277 /* This is an eligible token that does not have an expansion. */
5278 return (NO_EXPANSION);
5279 }
5280 return (NO_EXPANSION);
5281 }
5282 #endif /* ALIAS */
5283
5284 static int
5285 time_command_acceptable ()
5286 {
5287 #if defined (COMMAND_TIMING)
5288 int i;
5289
5290 if (posixly_correct && shell_compatibility_level > 41)
5291 {
5292 /* Quick check of the rest of the line to find the next token. If it
5293 begins with a `-', Posix says to not return `time' as the token.
5294 This was interp 267. */
5295 i = shell_input_line_index;
5296 while (i < shell_input_line_len && (shell_input_line[i] == ' ' || shell_input_line[i] == '\t'))
5297 i++;
5298 if (shell_input_line[i] == '-')
5299 return 0;
5300 }
5301
5302 switch (last_read_token)
5303 {
5304 case 0:
5305 case ';':
5306 case '\n':
5307 if (token_before_that == '|')
5308 return (0);
5309 /* FALLTHROUGH */
5310 case AND_AND:
5311 case OR_OR:
5312 case '&':
5313 case WHILE:
5314 case DO:
5315 case UNTIL:
5316 case IF:
5317 case THEN:
5318 case ELIF:
5319 case ELSE:
5320 case '{': /* } */
5321 case '(': /* )( */
5322 case ')': /* only valid in case statement */
5323 case BANG: /* ! time pipeline */
5324 case TIME: /* time time pipeline */
5325 case TIMEOPT: /* time -p time pipeline */
5326 case TIMEIGN: /* time -p -- ... */
5327 return 1;
5328 default:
5329 return 0;
5330 }
5331 #else
5332 return 0;
5333 #endif /* COMMAND_TIMING */
5334 }
5335
5336 /* Handle special cases of token recognition:
5337 IN is recognized if the last token was WORD and the token
5338 before that was FOR or CASE or SELECT.
5339
5340 DO is recognized if the last token was WORD and the token
5341 before that was FOR or SELECT.
5342
5343 ESAC is recognized if the last token caused `esacs_needed_count'
5344 to be set
5345
5346 `{' is recognized if the last token as WORD and the token
5347 before that was FUNCTION, or if we just parsed an arithmetic
5348 `for' command.
5349
5350 `}' is recognized if there is an unclosed `{' present.
5351
5352 `-p' is returned as TIMEOPT if the last read token was TIME.
5353 `--' is returned as TIMEIGN if the last read token was TIME or TIMEOPT.
5354
5355 ']]' is returned as COND_END if the parser is currently parsing
5356 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
5357
5358 `time' is returned as TIME if and only if it is immediately
5359 preceded by one of `;', `\n', `||', `&&', or `&'.
5360 */
5361
5362 static int
5363 special_case_tokens (tokstr)
5364 char *tokstr;
5365 {
5366 /* Posix grammar rule 6 */
5367 if ((last_read_token == WORD) &&
5368 #if defined (SELECT_COMMAND)
5369 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
5370 #else
5371 ((token_before_that == FOR) || (token_before_that == CASE)) &&
5372 #endif
5373 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
5374 {
5375 if (token_before_that == CASE)
5376 {
5377 parser_state |= PST_CASEPAT;
5378 esacs_needed_count++;
5379 }
5380 if (expecting_in_token)
5381 expecting_in_token--;
5382 return (IN);
5383 }
5384
5385 /* XXX - leaving above code intact for now, but it should eventually be
5386 removed in favor of this clause. */
5387 /* Posix grammar rule 6 */
5388 if (expecting_in_token && (last_read_token == WORD || last_read_token == '\n') &&
5389 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
5390 {
5391 if (parser_state & PST_CASESTMT)
5392 {
5393 parser_state |= PST_CASEPAT;
5394 esacs_needed_count++;
5395 }
5396 expecting_in_token--;
5397 return (IN);
5398 }
5399 /* Posix grammar rule 6, third word in FOR: for i; do command-list; done */
5400 else if (expecting_in_token && (last_read_token == '\n' || last_read_token == ';') &&
5401 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
5402 {
5403 expecting_in_token--;
5404 return (DO);
5405 }
5406
5407 /* for i do; command-list; done */
5408 if (last_read_token == WORD &&
5409 #if defined (SELECT_COMMAND)
5410 (token_before_that == FOR || token_before_that == SELECT) &&
5411 #else
5412 (token_before_that == FOR) &&
5413 #endif
5414 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
5415 {
5416 if (expecting_in_token)
5417 expecting_in_token--;
5418 return (DO);
5419 }
5420
5421 /* Ditto for ESAC in the CASE case.
5422 Specifically, this handles "case word in esac", which is a legal
5423 construct, certainly because someone will pass an empty arg to the
5424 case construct, and we don't want it to barf. Of course, we should
5425 insist that the case construct has at least one pattern in it, but
5426 the designers disagree. */
5427 if (esacs_needed_count)
5428 {
5429 if (last_read_token == IN && STREQ (tokstr, "esac"))
5430 {
5431 esacs_needed_count--;
5432 parser_state &= ~PST_CASEPAT;
5433 return (ESAC);
5434 }
5435 }
5436
5437 /* The start of a shell function definition. */
5438 if (parser_state & PST_ALLOWOPNBRC)
5439 {
5440 parser_state &= ~PST_ALLOWOPNBRC;
5441 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
5442 {
5443 open_brace_count++;
5444 function_bstart = line_number;
5445 return ('{'); /* } */
5446 }
5447 }
5448
5449 /* We allow a `do' after a for ((...)) without an intervening
5450 list_terminator */
5451 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
5452 return (DO);
5453 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
5454 {
5455 open_brace_count++;
5456 return ('{'); /* } */
5457 }
5458
5459 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
5460 {
5461 open_brace_count--; /* { */
5462 return ('}');
5463 }
5464
5465 #if defined (COMMAND_TIMING)
5466 /* Handle -p after `time'. */
5467 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
5468 return (TIMEOPT);
5469 /* Handle -- after `time'. */
5470 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == '-' && !tokstr[2])
5471 return (TIMEIGN);
5472 /* Handle -- after `time -p'. */
5473 if (last_read_token == TIMEOPT && tokstr[0] == '-' && tokstr[1] == '-' && !tokstr[2])
5474 return (TIMEIGN);
5475 #endif
5476
5477 #if defined (COND_COMMAND) /* [[ */
5478 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
5479 return (COND_END);
5480 #endif
5481
5482 return (-1);
5483 }
5484
5485 /* Called from shell.c when Control-C is typed at top level. Or
5486 by the error rule at top level. */
5487 void
5488 reset_parser ()
5489 {
5490 dstack.delimiter_depth = 0; /* No delimiters found so far. */
5491 open_brace_count = 0;
5492
5493 #if defined (EXTENDED_GLOB)
5494 /* Reset to global value of extended glob */
5495 if (parser_state & PST_EXTPAT)
5496 extended_glob = global_extglob;
5497 #endif
5498
5499 parser_state = 0;
5500 here_doc_first_line = 0;
5501
5502 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
5503 if (pushed_string_list)
5504 free_string_list ();
5505 #endif /* ALIAS || DPAREN_ARITHMETIC */
5506
5507 /* This is where we resynchronize to the next newline on error/reset */
5508 if (shell_input_line)
5509 {
5510 free (shell_input_line);
5511 shell_input_line = (char *)NULL;
5512 shell_input_line_size = shell_input_line_index = 0;
5513 }
5514
5515 FREE (word_desc_to_read);
5516 word_desc_to_read = (WORD_DESC *)NULL;
5517
5518 eol_ungetc_lookahead = 0;
5519
5520 current_token = '\n'; /* XXX */
5521 last_read_token = '\n';
5522 token_to_read = '\n';
5523 }
5524
5525 void
5526 reset_readahead_token ()
5527 {
5528 if (token_to_read == '\n')
5529 token_to_read = 0;
5530 }
5531
5532 /* Read the next token. Command can be READ (normal operation) or
5533 RESET (to normalize state). */
5534 static int
5535 read_token (command)
5536 int command;
5537 {
5538 int character; /* Current character. */
5539 int peek_char; /* Temporary look-ahead character. */
5540 int result; /* The thing to return. */
5541
5542 if (command == RESET)
5543 {
5544 reset_parser ();
5545 return ('\n');
5546 }
5547
5548 if (token_to_read)
5549 {
5550 result = token_to_read;
5551 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
5552 {
5553 yylval.word = word_desc_to_read;
5554 word_desc_to_read = (WORD_DESC *)NULL;
5555 }
5556 token_to_read = 0;
5557 return (result);
5558 }
5559
5560 #if defined (COND_COMMAND)
5561 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
5562 {
5563 cond_lineno = line_number;
5564 parser_state |= PST_CONDEXPR;
5565 yylval.command = parse_cond_command ();
5566 if (cond_token != COND_END)
5567 {
5568 cond_error ();
5569 return (-1);
5570 }
5571 token_to_read = COND_END;
5572 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
5573 return (COND_CMD);
5574 }
5575 #endif
5576
5577 #if defined (ALIAS)
5578 /* This is a place to jump back to once we have successfully expanded a
5579 token with an alias and pushed the string with push_string () */
5580 re_read_token:
5581 #endif /* ALIAS */
5582
5583 /* Read a single word from input. Start by skipping blanks. */
5584 while ((character = shell_getc (1)) != EOF && shellblank (character))
5585 ;
5586
5587 if (character == EOF)
5588 {
5589 EOF_Reached = 1;
5590 return (yacc_EOF);
5591 }
5592
5593 /* If we hit the end of the string and we're not expanding an alias (e.g.,
5594 we are eval'ing a string that is an incomplete command), return EOF */
5595 if (character == '\0' && bash_input.type == st_string && expanding_alias() == 0)
5596 {
5597 #if defined (DEBUG)
5598 itrace("shell_getc: bash_input.location.string = `%s'", bash_input.location.string);
5599 #endif
5600 EOF_Reached = 1;
5601 return (yacc_EOF);
5602 }
5603
5604 if MBTEST(character == '#' && (!interactive || interactive_comments))
5605 {
5606 /* A comment. Discard until EOL or EOF, and then return a newline. */
5607 parser_state |= PST_COMMENT;
5608 discard_until ('\n');
5609 shell_getc (0);
5610 parser_state &= ~PST_COMMENT;
5611 character = '\n'; /* this will take the next if statement and return. */
5612 }
5613
5614 if (character == '\n')
5615 {
5616 /* If we're about to return an unquoted newline, we can go and collect
5617 the text of any pending here document. */
5618 if (need_here_doc)
5619 gather_here_documents ();
5620
5621 #if defined (ALIAS)
5622 parser_state &= ~PST_ALEXPNEXT;
5623 #endif /* ALIAS */
5624
5625 parser_state &= ~PST_ASSIGNOK;
5626
5627 return (character);
5628 }
5629
5630 if (parser_state & PST_REGEXP)
5631 goto tokword;
5632
5633 /* Shell meta-characters. */
5634 if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
5635 {
5636 #if defined (ALIAS)
5637 /* Turn off alias tokenization iff this character sequence would
5638 not leave us ready to read a command. */
5639 if (character == '<' || character == '>')
5640 parser_state &= ~PST_ALEXPNEXT;
5641 #endif /* ALIAS */
5642
5643 parser_state &= ~PST_ASSIGNOK;
5644
5645 /* If we are parsing a command substitution and we have read a character
5646 that marks the end of it, don't bother to skip over quoted newlines
5647 when we read the next token. We're just interested in a character
5648 that will turn this into a two-character token, so we let the higher
5649 layers deal with quoted newlines following the command substitution. */
5650 if ((parser_state & PST_CMDSUBST) && character == shell_eof_token)
5651 peek_char = shell_getc (0);
5652 else
5653 peek_char = shell_getc (1);
5654
5655 if (character == peek_char)
5656 {
5657 switch (character)
5658 {
5659 case '<':
5660 /* If '<' then we could be at "<<" or at "<<-". We have to
5661 look ahead one more character. */
5662 peek_char = shell_getc (1);
5663 if MBTEST(peek_char == '-')
5664 return (LESS_LESS_MINUS);
5665 else if MBTEST(peek_char == '<')
5666 return (LESS_LESS_LESS);
5667 else
5668 {
5669 shell_ungetc (peek_char);
5670 return (LESS_LESS);
5671 }
5672
5673 case '>':
5674 return (GREATER_GREATER);
5675
5676 case ';':
5677 parser_state |= PST_CASEPAT;
5678 #if defined (ALIAS)
5679 parser_state &= ~PST_ALEXPNEXT;
5680 #endif /* ALIAS */
5681
5682 peek_char = shell_getc (1);
5683 if MBTEST(peek_char == '&')
5684 return (SEMI_SEMI_AND);
5685 else
5686 {
5687 shell_ungetc (peek_char);
5688 return (SEMI_SEMI);
5689 }
5690
5691 case '&':
5692 return (AND_AND);
5693
5694 case '|':
5695 return (OR_OR);
5696
5697 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
5698 case '(': /* ) */
5699 result = parse_dparen (character);
5700 if (result == -2)
5701 break;
5702 else
5703 return result;
5704 #endif
5705 }
5706 }
5707 else if MBTEST(character == '<' && peek_char == '&')
5708 return (LESS_AND);
5709 else if MBTEST(character == '>' && peek_char == '&')
5710 return (GREATER_AND);
5711 else if MBTEST(character == '<' && peek_char == '>')
5712 return (LESS_GREATER);
5713 else if MBTEST(character == '>' && peek_char == '|')
5714 return (GREATER_BAR);
5715 else if MBTEST(character == '&' && peek_char == '>')
5716 {
5717 peek_char = shell_getc (1);
5718 if MBTEST(peek_char == '>')
5719 return (AND_GREATER_GREATER);
5720 else
5721 {
5722 shell_ungetc (peek_char);
5723 return (AND_GREATER);
5724 }
5725 }
5726 else if MBTEST(character == '|' && peek_char == '&')
5727 return (BAR_AND);
5728 else if MBTEST(character == ';' && peek_char == '&')
5729 {
5730 parser_state |= PST_CASEPAT;
5731 #if defined (ALIAS)
5732 parser_state &= ~PST_ALEXPNEXT;
5733 #endif /* ALIAS */
5734 return (SEMI_AND);
5735 }
5736
5737 shell_ungetc (peek_char);
5738
5739 /* If we look like we are reading the start of a function
5740 definition, then let the reader know about it so that
5741 we will do the right thing with `{'. */
5742 if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
5743 {
5744 parser_state |= PST_ALLOWOPNBRC;
5745 #if defined (ALIAS)
5746 parser_state &= ~PST_ALEXPNEXT;
5747 #endif /* ALIAS */
5748 function_dstart = line_number;
5749 }
5750
5751 /* case pattern lists may be preceded by an optional left paren. If
5752 we're not trying to parse a case pattern list, the left paren
5753 indicates a subshell. */
5754 if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
5755 parser_state |= PST_SUBSHELL;
5756 /*(*/
5757 else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
5758 parser_state &= ~PST_CASEPAT;
5759 /*(*/
5760 else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
5761 parser_state &= ~PST_SUBSHELL;
5762
5763 #if defined (PROCESS_SUBSTITUTION)
5764 /* Check for the constructs which introduce process substitution.
5765 Shells running in `posix mode' don't do process substitution. */
5766 if MBTEST((character != '>' && character != '<') || peek_char != '(') /*)*/
5767 #endif /* PROCESS_SUBSTITUTION */
5768 return (character);
5769 }
5770
5771 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
5772 if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
5773 return (character);
5774
5775 tokword:
5776 /* Okay, if we got this far, we have to read a word. Read one,
5777 and then check it against the known ones. */
5778 result = read_token_word (character);
5779 #if defined (ALIAS)
5780 if (result == RE_READ_TOKEN)
5781 goto re_read_token;
5782 #endif
5783 return result;
5784 }
5785
5786 /*
5787 * Match a $(...) or other grouping construct. This has to handle embedded
5788 * quoted strings ('', ``, "") and nested constructs. It also must handle
5789 * reprompting the user, if necessary, after reading a newline, and returning
5790 * correct error values if it reads EOF.
5791 */
5792 #define P_FIRSTCLOSE 0x0001
5793 #define P_ALLOWESC 0x0002
5794 #define P_DQUOTE 0x0004
5795 #define P_COMMAND 0x0008 /* parsing a command, so look for comments */
5796 #define P_BACKQUOTE 0x0010 /* parsing a backquoted command substitution */
5797 #define P_ARRAYSUB 0x0020 /* parsing a [...] array subscript for assignment */
5798 #define P_DOLBRACE 0x0040 /* parsing a ${...} construct */
5799
5800 /* Lexical state while parsing a grouping construct or $(...). */
5801 #define LEX_WASDOL 0x0001
5802 #define LEX_CKCOMMENT 0x0002
5803 #define LEX_INCOMMENT 0x0004
5804 #define LEX_PASSNEXT 0x0008
5805 #define LEX_RESWDOK 0x0010
5806 #define LEX_CKCASE 0x0020
5807 #define LEX_INCASE 0x0040
5808 #define LEX_INHEREDOC 0x0080
5809 #define LEX_HEREDELIM 0x0100 /* reading here-doc delimiter */
5810 #define LEX_STRIPDOC 0x0200 /* <<- strip tabs from here doc delim */
5811 #define LEX_QUOTEDDOC 0x0400 /* here doc with quoted delim */
5812 #define LEX_INWORD 0x0800
5813 #define LEX_GTLT 0x1000
5814
5815 #define COMSUB_META(ch) ((ch) == ';' || (ch) == '&' || (ch) == '|')
5816
5817 #define CHECK_NESTRET_ERROR() \
5818 do { \
5819 if (nestret == &matched_pair_error) \
5820 { \
5821 free (ret); \
5822 return &matched_pair_error; \
5823 } \
5824 } while (0)
5825
5826 #define APPEND_NESTRET() \
5827 do { \
5828 if (nestlen) \
5829 { \
5830 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64); \
5831 strcpy (ret + retind, nestret); \
5832 retind += nestlen; \
5833 } \
5834 } while (0)
5835
5836 static char matched_pair_error;
5837
5838 static char *
5839 parse_matched_pair (qc, open, close, lenp, flags)
5840 int qc; /* `"' if this construct is within double quotes */
5841 int open, close;
5842 int *lenp, flags;
5843 {
5844 int count, ch, prevch, tflags;
5845 int nestlen, ttranslen, start_lineno;
5846 char *ret, *nestret, *ttrans;
5847 int retind, retsize, rflags;
5848 int dolbrace_state;
5849
5850 dolbrace_state = (flags & P_DOLBRACE) ? DOLBRACE_PARAM : 0;
5851
5852 /*itrace("parse_matched_pair[%d]: open = %c close = %c flags = %d", line_number, open, close, flags);*/
5853 count = 1;
5854 tflags = 0;
5855
5856 if ((flags & P_COMMAND) && qc != '`' && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
5857 tflags |= LEX_CKCOMMENT;
5858
5859 /* RFLAGS is the set of flags we want to pass to recursive calls. */
5860 rflags = (qc == '"') ? P_DQUOTE : (flags & P_DQUOTE);
5861
5862 ret = (char *)xmalloc (retsize = 64);
5863 retind = 0;
5864
5865 start_lineno = line_number;
5866 ch = EOF; /* just in case */
5867 while (count)
5868 {
5869 prevch = ch;
5870 ch = shell_getc (qc != '\'' && (tflags & (LEX_PASSNEXT)) == 0);
5871
5872 if (ch == EOF)
5873 {
5874 free (ret);
5875 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
5876 EOF_Reached = 1; /* XXX */
5877 return (&matched_pair_error);
5878 }
5879
5880 /* Possible reprompting. */
5881 if (ch == '\n' && SHOULD_PROMPT ())
5882 prompt_again ();
5883
5884 /* Don't bother counting parens or doing anything else if in a comment
5885 or part of a case statement */
5886 if (tflags & LEX_INCOMMENT)
5887 {
5888 /* Add this character. */
5889 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
5890 ret[retind++] = ch;
5891
5892 if (ch == '\n')
5893 tflags &= ~LEX_INCOMMENT;
5894
5895 continue;
5896 }
5897
5898 /* Not exactly right yet, should handle shell metacharacters, too. If
5899 any changes are made to this test, make analogous changes to subst.c:
5900 extract_delimited_string(). */
5901 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (retind == 0 || ret[retind-1] == '\n' || shellblank (ret[retind - 1])))
5902 tflags |= LEX_INCOMMENT;
5903
5904 if (tflags & LEX_PASSNEXT) /* last char was backslash */
5905 {
5906 tflags &= ~LEX_PASSNEXT;
5907 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
5908 {
5909 if (retind > 0)
5910 retind--; /* swallow previously-added backslash */
5911 continue;
5912 }
5913
5914 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
5915 if MBTEST(ch == CTLESC)
5916 ret[retind++] = CTLESC;
5917 ret[retind++] = ch;
5918 continue;
5919 }
5920 /* If we're reparsing the input (e.g., from parse_string_to_word_list),
5921 we've already prepended CTLESC to single-quoted results of $'...'.
5922 We may want to do this for other CTLESC-quoted characters in
5923 reparse, too. */
5924 else if MBTEST((parser_state & PST_REPARSE) && open == '\'' && (ch == CTLESC || ch == CTLNUL))
5925 {
5926 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
5927 ret[retind++] = ch;
5928 continue;
5929 }
5930 else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
5931 {
5932 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
5933 ret[retind++] = CTLESC;
5934 ret[retind++] = ch;
5935 continue;
5936 }
5937 else if MBTEST(ch == close) /* ending delimiter */
5938 count--;
5939 /* handle nested ${...} specially. */
5940 else if MBTEST(open != close && (tflags & LEX_WASDOL) && open == '{' && ch == open) /* } */
5941 count++;
5942 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
5943 count++;
5944
5945 /* Add this character. */
5946 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
5947 ret[retind++] = ch;
5948
5949 /* If we just read the ending character, don't bother continuing. */
5950 if (count == 0)
5951 break;
5952
5953 if (open == '\'') /* '' inside grouping construct */
5954 {
5955 if MBTEST((flags & P_ALLOWESC) && ch == '\\')
5956 tflags |= LEX_PASSNEXT;
5957 continue;
5958 }
5959
5960 if MBTEST(ch == '\\') /* backslashes */
5961 tflags |= LEX_PASSNEXT;
5962
5963 /* Based on which dolstate is currently in (param, op, or word),
5964 decide what the op is. We're really only concerned if it's % or
5965 #, so we can turn on a flag that says whether or not we should
5966 treat single quotes as special when inside a double-quoted
5967 ${...}. This logic must agree with subst.c:extract_dollar_brace_string
5968 since they share the same defines. */
5969 /* FLAG POSIX INTERP 221 */
5970 if (flags & P_DOLBRACE)
5971 {
5972 /* ${param%[%]word} */
5973 if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '%' && retind > 1)
5974 dolbrace_state = DOLBRACE_QUOTE;
5975 /* ${param#[#]word} */
5976 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '#' && retind > 1)
5977 dolbrace_state = DOLBRACE_QUOTE;
5978 /* ${param/[/]pat/rep} */
5979 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '/' && retind > 1)
5980 dolbrace_state = DOLBRACE_QUOTE2; /* XXX */
5981 /* ${param^[^]pat} */
5982 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '^' && retind > 1)
5983 dolbrace_state = DOLBRACE_QUOTE;
5984 /* ${param,[,]pat} */
5985 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == ',' && retind > 1)
5986 dolbrace_state = DOLBRACE_QUOTE;
5987 else if MBTEST(dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", ch) != 0)
5988 dolbrace_state = DOLBRACE_OP;
5989 else if MBTEST(dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", ch) == 0)
5990 dolbrace_state = DOLBRACE_WORD;
5991 }
5992
5993 /* The big hammer. Single quotes aren't special in double quotes. The
5994 problem is that Posix used to say the single quotes are semi-special:
5995 within a double-quoted ${...} construct "an even number of
5996 unescaped double-quotes or single-quotes, if any, shall occur." */
5997 /* This was changed in Austin Group Interp 221 */
5998 if MBTEST(posixly_correct && shell_compatibility_level > 41 && dolbrace_state != DOLBRACE_QUOTE && dolbrace_state != DOLBRACE_QUOTE2 && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'')
5999 continue;
6000
6001 /* Could also check open == '`' if we want to parse grouping constructs
6002 inside old-style command substitution. */
6003 if (open != close) /* a grouping construct */
6004 {
6005 if MBTEST(shellquote (ch))
6006 {
6007 /* '', ``, or "" inside $(...) or other grouping construct. */
6008 push_delimiter (dstack, ch);
6009 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
6010 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
6011 else
6012 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
6013 pop_delimiter (dstack);
6014 CHECK_NESTRET_ERROR ();
6015
6016 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
6017 {
6018 /* Translate $'...' here. */
6019 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
6020 free (nestret);
6021
6022 /* If we're parsing a double-quoted brace expansion and we are
6023 not in a place where single quotes are treated specially,
6024 make sure we single-quote the results of the ansi
6025 expansion because quote removal should remove them later */
6026 /* FLAG POSIX INTERP 221 */
6027 if ((shell_compatibility_level > 42) && (rflags & P_DQUOTE) && (dolbrace_state == DOLBRACE_QUOTE2) && (flags & P_DOLBRACE))
6028 {
6029 nestret = sh_single_quote (ttrans);
6030 free (ttrans);
6031 nestlen = strlen (nestret);
6032 }
6033 else if ((rflags & P_DQUOTE) == 0)
6034 {
6035 nestret = sh_single_quote (ttrans);
6036 free (ttrans);
6037 nestlen = strlen (nestret);
6038 }
6039 else
6040 {
6041 nestret = ttrans;
6042 nestlen = ttranslen;
6043 }
6044 retind -= 2; /* back up before the $' */
6045 }
6046 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
6047 {
6048 /* Locale expand $"..." here. */
6049 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
6050 free (nestret);
6051
6052 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
6053 free (ttrans);
6054 nestlen = ttranslen + 2;
6055 retind -= 2; /* back up before the $" */
6056 }
6057
6058 APPEND_NESTRET ();
6059 FREE (nestret);
6060 }
6061 else if ((flags & (P_ARRAYSUB|P_DOLBRACE)) && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
6062 goto parse_dollar_word;
6063 #if defined (PROCESS_SUBSTITUTION)
6064 /* XXX - technically this should only be recognized at the start of
6065 a word */
6066 else if ((flags & (P_ARRAYSUB|P_DOLBRACE)) && (tflags & LEX_GTLT) && (ch == '(')) /* ) */
6067 goto parse_dollar_word;
6068 #endif
6069 }
6070 /* Parse an old-style command substitution within double quotes as a
6071 single word. */
6072 /* XXX - sh and ksh93 don't do this - XXX */
6073 else if MBTEST(open == '"' && ch == '`')
6074 {
6075 nestret = parse_matched_pair (0, '`', '`', &nestlen, rflags);
6076
6077 CHECK_NESTRET_ERROR ();
6078 APPEND_NESTRET ();
6079
6080 FREE (nestret);
6081 }
6082 else if MBTEST(open != '`' && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
6083 /* check for $(), $[], or ${} inside quoted string. */
6084 {
6085 parse_dollar_word:
6086 if (open == ch) /* undo previous increment */
6087 count--;
6088 if (ch == '(') /* ) */
6089 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
6090 else if (ch == '{') /* } */
6091 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags);
6092 else if (ch == '[') /* ] */
6093 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
6094
6095 CHECK_NESTRET_ERROR ();
6096 APPEND_NESTRET ();
6097
6098 FREE (nestret);
6099 }
6100 #if defined (PROCESS_SUBSTITUTION)
6101 if MBTEST((ch == '<' || ch == '>') && (tflags & LEX_GTLT) == 0)
6102 tflags |= LEX_GTLT;
6103 else
6104 tflags &= ~LEX_GTLT;
6105 #endif
6106 if MBTEST(ch == '$' && (tflags & LEX_WASDOL) == 0)
6107 tflags |= LEX_WASDOL;
6108 else
6109 tflags &= ~LEX_WASDOL;
6110 }
6111
6112 ret[retind] = '\0';
6113 if (lenp)
6114 *lenp = retind;
6115 /*itrace("parse_matched_pair[%d]: returning %s", line_number, ret);*/
6116 return ret;
6117 }
6118
6119 #if defined (DEBUG)
6120 static void
6121 dump_tflags (flags)
6122 int flags;
6123 {
6124 int f;
6125
6126 f = flags;
6127 fprintf (stderr, "%d -> ", f);
6128 if (f & LEX_WASDOL)
6129 {
6130 f &= ~LEX_WASDOL;
6131 fprintf (stderr, "LEX_WASDOL%s", f ? "|" : "");
6132 }
6133 if (f & LEX_CKCOMMENT)
6134 {
6135 f &= ~LEX_CKCOMMENT;
6136 fprintf (stderr, "LEX_CKCOMMENT%s", f ? "|" : "");
6137 }
6138 if (f & LEX_INCOMMENT)
6139 {
6140 f &= ~LEX_INCOMMENT;
6141 fprintf (stderr, "LEX_INCOMMENT%s", f ? "|" : "");
6142 }
6143 if (f & LEX_PASSNEXT)
6144 {
6145 f &= ~LEX_PASSNEXT;
6146 fprintf (stderr, "LEX_PASSNEXT%s", f ? "|" : "");
6147 }
6148 if (f & LEX_RESWDOK)
6149 {
6150 f &= ~LEX_RESWDOK;
6151 fprintf (stderr, "LEX_RESWDOK%s", f ? "|" : "");
6152 }
6153 if (f & LEX_CKCASE)
6154 {
6155 f &= ~LEX_CKCASE;
6156 fprintf (stderr, "LEX_CKCASE%s", f ? "|" : "");
6157 }
6158 if (f & LEX_INCASE)
6159 {
6160 f &= ~LEX_INCASE;
6161 fprintf (stderr, "LEX_INCASE%s", f ? "|" : "");
6162 }
6163 if (f & LEX_INHEREDOC)
6164 {
6165 f &= ~LEX_INHEREDOC;
6166 fprintf (stderr, "LEX_INHEREDOC%s", f ? "|" : "");
6167 }
6168 if (f & LEX_HEREDELIM)
6169 {
6170 f &= ~LEX_HEREDELIM;
6171 fprintf (stderr, "LEX_HEREDELIM%s", f ? "|" : "");
6172 }
6173 if (f & LEX_STRIPDOC)
6174 {
6175 f &= ~LEX_STRIPDOC;
6176 fprintf (stderr, "LEX_WASDOL%s", f ? "|" : "");
6177 }
6178 if (f & LEX_QUOTEDDOC)
6179 {
6180 f &= ~LEX_QUOTEDDOC;
6181 fprintf (stderr, "LEX_QUOTEDDOC%s", f ? "|" : "");
6182 }
6183 if (f & LEX_INWORD)
6184 {
6185 f &= ~LEX_INWORD;
6186 fprintf (stderr, "LEX_INWORD%s", f ? "|" : "");
6187 }
6188
6189 fprintf (stderr, "\n");
6190 fflush (stderr);
6191 }
6192 #endif
6193
6194 /* Parse a $(...) command substitution. This is messier than I'd like, and
6195 reproduces a lot more of the token-reading code than I'd like. */
6196 static char *
6197 parse_comsub (qc, open, close, lenp, flags)
6198 int qc; /* `"' if this construct is within double quotes */
6199 int open, close;
6200 int *lenp, flags;
6201 {
6202 int count, ch, peekc, tflags, lex_rwlen, lex_wlen, lex_firstind;
6203 int nestlen, ttranslen, start_lineno, orig_histexp;
6204 char *ret, *nestret, *ttrans, *heredelim;
6205 int retind, retsize, rflags, hdlen;
6206
6207 /* Posix interp 217 says arithmetic expressions have precedence, so
6208 assume $(( introduces arithmetic expansion and parse accordingly. */
6209 peekc = shell_getc (0);
6210 shell_ungetc (peekc);
6211 if (peekc == '(')
6212 return (parse_matched_pair (qc, open, close, lenp, 0));
6213
6214 /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/
6215 count = 1;
6216 tflags = LEX_RESWDOK;
6217 #if defined (BANG_HISTORY)
6218 orig_histexp = history_expansion_inhibited;
6219 #endif
6220
6221 if ((flags & P_COMMAND) && qc != '\'' && qc != '"' && (flags & P_DQUOTE) == 0)
6222 tflags |= LEX_CKCASE;
6223 if ((tflags & LEX_CKCASE) && (interactive == 0 || interactive_comments))
6224 tflags |= LEX_CKCOMMENT;
6225
6226 /* RFLAGS is the set of flags we want to pass to recursive calls. */
6227 rflags = (flags & P_DQUOTE);
6228
6229 ret = (char *)xmalloc (retsize = 64);
6230 retind = 0;
6231
6232 start_lineno = line_number;
6233 lex_rwlen = lex_wlen = 0;
6234
6235 heredelim = 0;
6236 lex_firstind = -1;
6237
6238 while (count)
6239 {
6240 comsub_readchar:
6241 ch = shell_getc (qc != '\'' && (tflags & (LEX_INCOMMENT|LEX_PASSNEXT|LEX_QUOTEDDOC)) == 0);
6242
6243 if (ch == EOF)
6244 {
6245 eof_error:
6246 #if defined (BANG_HISTORY)
6247 history_expansion_inhibited = orig_histexp;
6248 #endif
6249 free (ret);
6250 FREE (heredelim);
6251 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
6252 EOF_Reached = 1; /* XXX */
6253 return (&matched_pair_error);
6254 }
6255
6256 /* If we hit the end of a line and are reading the contents of a here
6257 document, and it's not the same line that the document starts on,
6258 check for this line being the here doc delimiter. Otherwise, if
6259 we're in a here document, mark the next character as the beginning
6260 of a line. */
6261 if (ch == '\n')
6262 {
6263 if ((tflags & LEX_HEREDELIM) && heredelim)
6264 {
6265 tflags &= ~LEX_HEREDELIM;
6266 tflags |= LEX_INHEREDOC;
6267 #if defined (BANG_HISTORY)
6268 history_expansion_inhibited = 1;
6269 #endif
6270 lex_firstind = retind + 1;
6271 }
6272 else if (tflags & LEX_INHEREDOC)
6273 {
6274 int tind;
6275 tind = lex_firstind;
6276 while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t')
6277 tind++;
6278 if (retind-tind == hdlen && STREQN (ret + tind, heredelim, hdlen))
6279 {
6280 tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC|LEX_QUOTEDDOC);
6281 /*itrace("parse_comsub:%d: found here doc end `%s'", line_number, ret + tind);*/
6282 free (heredelim);
6283 heredelim = 0;
6284 lex_firstind = -1;
6285 #if defined (BANG_HISTORY)
6286 history_expansion_inhibited = orig_histexp;
6287 #endif
6288 }
6289 else
6290 lex_firstind = retind + 1;
6291 }
6292 }
6293
6294 /* Possible reprompting. */
6295 if (ch == '\n' && SHOULD_PROMPT ())
6296 prompt_again ();
6297
6298 /* XXX -- we currently allow here doc to be delimited by ending right
6299 paren in default mode and posix mode. To change posix mode, change
6300 the #if 1 to #if 0 below */
6301 if ((tflags & LEX_INHEREDOC) && ch == close && count == 1)
6302 {
6303 int tind;
6304 /*itrace("parse_comsub:%d: in here doc, ch == close, retind - firstind = %d hdlen = %d retind = %d", line_number, retind-lex_firstind, hdlen, retind);*/
6305 tind = lex_firstind;
6306 while ((tflags & LEX_STRIPDOC) && ret[tind] == '\t')
6307 tind++;
6308 #if 1
6309 if (retind-tind == hdlen && STREQN (ret + tind, heredelim, hdlen))
6310 #else
6311 /* Posix-mode shells require the newline after the here-document
6312 delimiter. */
6313 if (retind-tind == hdlen && STREQN (ret + tind, heredelim, hdlen) &&
6314 posixly_correct == 0)
6315 #endif
6316 {
6317 tflags &= ~(LEX_STRIPDOC|LEX_INHEREDOC|LEX_QUOTEDDOC);
6318 /*itrace("parse_comsub:%d: found here doc end `%*s'", line_number, hdlen, ret + tind);*/
6319 free (heredelim);
6320 heredelim = 0;
6321 lex_firstind = -1;
6322 #if defined (BANG_HISTORY)
6323 history_expansion_inhibited = orig_histexp;
6324 #endif
6325 }
6326 }
6327
6328 /* Don't bother counting parens or doing anything else if in a comment or
6329 here document (not exactly right for here-docs -- if we want to allow
6330 recursive calls to parse_comsub to have their own here documents,
6331 change the LEX_INHEREDOC to LEX_QUOTEDDOC here and uncomment the next
6332 clause below. Note that to make this work completely, we need to make
6333 additional changes to allow xparse_dolparen to work right when the
6334 command substitution is parsed, because read_secondary_line doesn't know
6335 to recursively parse through command substitutions embedded in here-
6336 documents */
6337 if (tflags & (LEX_INCOMMENT|LEX_INHEREDOC))
6338 {
6339 /* Add this character. */
6340 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
6341 ret[retind++] = ch;
6342
6343 if ((tflags & LEX_INCOMMENT) && ch == '\n')
6344 {
6345 /*itrace("parse_comsub:%d: lex_incomment -> 0 ch = `%c'", line_number, ch);*/
6346 tflags &= ~LEX_INCOMMENT;
6347 }
6348
6349 continue;
6350 }
6351 #if 0
6352 /* If we're going to recursively parse a command substitution inside a
6353 here-document, make sure we call parse_comsub recursively below. See
6354 above for additional caveats. */
6355 if ((tflags & LEX_INHEREDOC) && ((tflags & LEX_WASDOL) == 0 || ch != '(')) /*)*/
6356 {
6357 /* Add this character. */
6358 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
6359 ret[retind++] = ch;
6360 if MBTEST(ch == '$')
6361 tflags |= LEX_WASDOL;
6362 else
6363 tflags &= ~LEX_WASDOL;
6364 }
6365 #endif
6366
6367 if (tflags & LEX_PASSNEXT) /* last char was backslash */
6368 {
6369 /*itrace("parse_comsub:%d: lex_passnext -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/
6370 tflags &= ~LEX_PASSNEXT;
6371 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
6372 {
6373 if (retind > 0)
6374 retind--; /* swallow previously-added backslash */
6375 continue;
6376 }
6377
6378 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
6379 if MBTEST(ch == CTLESC)
6380 ret[retind++] = CTLESC;
6381 ret[retind++] = ch;
6382 continue;
6383 }
6384
6385 /* If this is a shell break character, we are not in a word. If not,
6386 we either start or continue a word. */
6387 if MBTEST(shellbreak (ch))
6388 {
6389 tflags &= ~LEX_INWORD;
6390 /*itrace("parse_comsub:%d: lex_inword -> 0 ch = `%c' (%d)", line_number, ch, __LINE__);*/
6391 }
6392 else
6393 {
6394 if (tflags & LEX_INWORD)
6395 {
6396 lex_wlen++;
6397 /*itrace("parse_comsub:%d: lex_inword == 1 ch = `%c' lex_wlen = %d (%d)", line_number, ch, lex_wlen, __LINE__);*/
6398 }
6399 else
6400 {
6401 /*itrace("parse_comsub:%d: lex_inword -> 1 ch = `%c' (%d)", line_number, ch, __LINE__);*/
6402 tflags |= LEX_INWORD;
6403 lex_wlen = 0;
6404 if (tflags & LEX_RESWDOK)
6405 lex_rwlen = 0;
6406 }
6407 }
6408
6409 /* Skip whitespace */
6410 if MBTEST(shellblank (ch) && (tflags & LEX_HEREDELIM) == 0 && lex_rwlen == 0)
6411 {
6412 /* Add this character. */
6413 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
6414 ret[retind++] = ch;
6415 continue;
6416 }
6417
6418 /* Either we are looking for the start of the here-doc delimiter
6419 (lex_firstind == -1) or we are reading one (lex_firstind >= 0).
6420 If this character is a shell break character and we are reading
6421 the delimiter, save it and note that we are now reading a here
6422 document. If we've found the start of the delimiter, note it by
6423 setting lex_firstind. Backslashes can quote shell metacharacters
6424 in here-doc delimiters. */
6425 if (tflags & LEX_HEREDELIM)
6426 {
6427 if (lex_firstind == -1 && shellbreak (ch) == 0)
6428 lex_firstind = retind;
6429 #if 0
6430 else if (heredelim && (tflags & LEX_PASSNEXT) == 0 && ch == '\n')
6431 {
6432 tflags |= LEX_INHEREDOC;
6433 tflags &= ~LEX_HEREDELIM;
6434 lex_firstind = retind + 1;
6435 #if defined (BANG_HISTORY)
6436 history_expansion_inhibited = 1;
6437 #endif
6438 }
6439 #endif
6440 else if (lex_firstind >= 0 && (tflags & LEX_PASSNEXT) == 0 && shellbreak (ch))
6441 {
6442 if (heredelim == 0)
6443 {
6444 nestret = substring (ret, lex_firstind, retind);
6445 heredelim = string_quote_removal (nestret, 0);
6446 hdlen = STRLEN(heredelim);
6447 /*itrace("parse_comsub:%d: found here doc delimiter `%s' (%d)", line_number, heredelim, hdlen);*/
6448 if (STREQ (heredelim, nestret) == 0)
6449 tflags |= LEX_QUOTEDDOC;
6450 free (nestret);
6451 }
6452 if (ch == '\n')
6453 {
6454 tflags |= LEX_INHEREDOC;
6455 tflags &= ~LEX_HEREDELIM;
6456 lex_firstind = retind + 1;
6457 #if defined (BANG_HISTORY)
6458 history_expansion_inhibited = 1;
6459 #endif
6460 }
6461 else
6462 lex_firstind = -1;
6463 }
6464 }
6465
6466 /* Meta-characters that can introduce a reserved word. Not perfect yet. */
6467 if MBTEST((tflags & LEX_RESWDOK) == 0 && (tflags & LEX_CKCASE) && (tflags & LEX_INCOMMENT) == 0 && (shellmeta(ch) || ch == '\n'))
6468 {
6469 /* Add this character. */
6470 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
6471 ret[retind++] = ch;
6472 peekc = shell_getc (1);
6473 if (ch == peekc && (ch == '&' || ch == '|' || ch == ';')) /* two-character tokens */
6474 {
6475 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
6476 ret[retind++] = peekc;
6477 /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/
6478 tflags |= LEX_RESWDOK;
6479 lex_rwlen = 0;
6480 continue;
6481 }
6482 else if (ch == '\n' || COMSUB_META(ch))
6483 {
6484 shell_ungetc (peekc);
6485 /*itrace("parse_comsub:%d: set lex_reswordok = 1, ch = `%c'", line_number, ch);*/
6486 tflags |= LEX_RESWDOK;
6487 lex_rwlen = 0;
6488 continue;
6489 }
6490 else if (ch == EOF)
6491 goto eof_error;
6492 else
6493 {
6494 /* `unget' the character we just added and fall through */
6495 retind--;
6496 shell_ungetc (peekc);
6497 }
6498 }
6499
6500 /* If we can read a reserved word, try to read one. */
6501 if (tflags & LEX_RESWDOK)
6502 {
6503 if MBTEST(islower ((unsigned char)ch))
6504 {
6505 /* Add this character. */
6506 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
6507 ret[retind++] = ch;
6508 lex_rwlen++;
6509 continue;
6510 }
6511 else if MBTEST(lex_rwlen == 4 && shellbreak (ch))
6512 {
6513 if (STREQN (ret + retind - 4, "case", 4))
6514 {
6515 tflags |= LEX_INCASE;
6516 tflags &= ~LEX_RESWDOK;
6517 /*itrace("parse_comsub:%d: found `case', lex_incase -> 1 lex_reswdok -> 0", line_number);*/
6518 }
6519 else if (STREQN (ret + retind - 4, "esac", 4))
6520 {
6521 tflags &= ~LEX_INCASE;
6522 /*itrace("parse_comsub:%d: found `esac', lex_incase -> 0 lex_reswdok -> 1", line_number);*/
6523 tflags |= LEX_RESWDOK;
6524 lex_rwlen = 0;
6525 }
6526 else if (STREQN (ret + retind - 4, "done", 4) ||
6527 STREQN (ret + retind - 4, "then", 4) ||
6528 STREQN (ret + retind - 4, "else", 4) ||
6529 STREQN (ret + retind - 4, "elif", 4) ||
6530 STREQN (ret + retind - 4, "time", 4))
6531 {
6532 /* these are four-character reserved words that can be
6533 followed by a reserved word; anything else turns off
6534 the reserved-word-ok flag */
6535 /*itrace("parse_comsub:%d: found `%.4s', lex_reswdok -> 1", line_number, ret+retind-4);*/
6536 tflags |= LEX_RESWDOK;
6537 lex_rwlen = 0;
6538 }
6539 else if (shellmeta (ch) == 0)
6540 {
6541 tflags &= ~LEX_RESWDOK;
6542 /*itrace("parse_comsub:%d: found `%.4s', lex_reswdok -> 0", line_number, ret+retind-4);*/
6543 }
6544 else /* can't be in a reserved word any more */
6545 lex_rwlen = 0;
6546 }
6547 else if MBTEST((tflags & LEX_CKCOMMENT) && ch == '#' && (lex_rwlen == 0 || ((tflags & LEX_INWORD) && lex_wlen == 0)))
6548 ; /* don't modify LEX_RESWDOK if we're starting a comment */
6549 /* Allow `do' followed by space, tab, or newline to preserve the
6550 RESWDOK flag, but reset the reserved word length counter so we
6551 can read another one. */
6552 else if MBTEST(((tflags & LEX_INCASE) == 0) &&
6553 (isblank((unsigned char)ch) || ch == '\n') &&
6554 lex_rwlen == 2 &&
6555 STREQN (ret + retind - 2, "do", 2))
6556 {
6557 /*itrace("parse_comsub:%d: lex_incase == 0 found `%c', found \"do\"", line_number, ch);*/
6558 lex_rwlen = 0;
6559 }
6560 else if MBTEST((tflags & LEX_INCASE) && ch != '\n')
6561 /* If we can read a reserved word and we're in case, we're at the
6562 point where we can read a new pattern list or an esac. We
6563 handle the esac case above. If we read a newline, we want to
6564 leave LEX_RESWDOK alone. If we read anything else, we want to
6565 turn off LEX_RESWDOK, since we're going to read a pattern list. */
6566 {
6567 tflags &= ~LEX_RESWDOK;
6568 /*itrace("parse_comsub:%d: lex_incase == 1 found `%c', lex_reswordok -> 0", line_number, ch);*/
6569 }
6570 else if MBTEST(shellbreak (ch) == 0)
6571 {
6572 tflags &= ~LEX_RESWDOK;
6573 /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/
6574 }
6575 #if 0
6576 /* If we find a space or tab but have read something and it's not
6577 `do', turn off the reserved-word-ok flag */
6578 else if MBTEST(isblank ((unsigned char)ch) && lex_rwlen > 0)
6579 {
6580 tflags &= ~LEX_RESWDOK;
6581 /*itrace("parse_comsub:%d: found `%c', lex_reswordok -> 0", line_number, ch);*/
6582 }
6583 #endif
6584 }
6585
6586 /* Might be the start of a here-doc delimiter */
6587 if MBTEST((tflags & LEX_INCOMMENT) == 0 && (tflags & LEX_CKCASE) && ch == '<')
6588 {
6589 /* Add this character. */
6590 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
6591 ret[retind++] = ch;
6592 peekc = shell_getc (1);
6593 if (peekc == EOF)
6594 goto eof_error;
6595 if (peekc == ch)
6596 {
6597 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
6598 ret[retind++] = peekc;
6599 peekc = shell_getc (1);
6600 if (peekc == EOF)
6601 goto eof_error;
6602 if (peekc == '-')
6603 {
6604 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
6605 ret[retind++] = peekc;
6606 tflags |= LEX_STRIPDOC;
6607 }
6608 else
6609 shell_ungetc (peekc);
6610 if (peekc != '<')
6611 {
6612 tflags |= LEX_HEREDELIM;
6613 lex_firstind = -1;
6614 }
6615 continue;
6616 }
6617 else
6618 {
6619 shell_ungetc (peekc); /* not a here-doc, start over */
6620 continue;
6621 }
6622 }
6623 else if MBTEST((tflags & LEX_CKCOMMENT) && (tflags & LEX_INCOMMENT) == 0 && ch == '#' && (((tflags & LEX_RESWDOK) && lex_rwlen == 0) || ((tflags & LEX_INWORD) && lex_wlen == 0)))
6624 {
6625 /*itrace("parse_comsub:%d: lex_incomment -> 1 (%d)", line_number, __LINE__);*/
6626 tflags |= LEX_INCOMMENT;
6627 }
6628
6629 if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
6630 {
6631 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
6632 ret[retind++] = CTLESC;
6633 ret[retind++] = ch;
6634 continue;
6635 }
6636 #if 0
6637 else if MBTEST((tflags & LEX_INCASE) && ch == close && close == ')')
6638 tflags &= ~LEX_INCASE; /* XXX */
6639 #endif
6640 else if MBTEST(ch == close && (tflags & LEX_INCASE) == 0) /* ending delimiter */
6641 {
6642 count--;
6643 /*itrace("parse_comsub:%d: found close: count = %d", line_number, count);*/
6644 }
6645 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && (tflags & LEX_INCASE) == 0 && ch == open) /* nested begin */
6646 {
6647 count++;
6648 /*itrace("parse_comsub:%d: found open: count = %d", line_number, count);*/
6649 }
6650
6651 /* Add this character. */
6652 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
6653 ret[retind++] = ch;
6654
6655 /* If we just read the ending character, don't bother continuing. */
6656 if (count == 0)
6657 break;
6658
6659 if MBTEST(ch == '\\') /* backslashes */
6660 tflags |= LEX_PASSNEXT;
6661
6662 if MBTEST(shellquote (ch))
6663 {
6664 /* '', ``, or "" inside $(...). */
6665 push_delimiter (dstack, ch);
6666 if MBTEST((tflags & LEX_WASDOL) && ch == '\'') /* $'...' inside group */
6667 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC|rflags);
6668 else
6669 nestret = parse_matched_pair (ch, ch, ch, &nestlen, rflags);
6670 pop_delimiter (dstack);
6671 CHECK_NESTRET_ERROR ();
6672
6673 if MBTEST((tflags & LEX_WASDOL) && ch == '\'' && (extended_quote || (rflags & P_DQUOTE) == 0))
6674 {
6675 /* Translate $'...' here. */
6676 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
6677 free (nestret);
6678
6679 if ((rflags & P_DQUOTE) == 0)
6680 {
6681 nestret = sh_single_quote (ttrans);
6682 free (ttrans);
6683 nestlen = strlen (nestret);
6684 }
6685 else
6686 {
6687 nestret = ttrans;
6688 nestlen = ttranslen;
6689 }
6690 retind -= 2; /* back up before the $' */
6691 }
6692 else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
6693 {
6694 /* Locale expand $"..." here. */
6695 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
6696 free (nestret);
6697
6698 nestret = sh_mkdoublequoted (ttrans, ttranslen, 0);
6699 free (ttrans);
6700 nestlen = ttranslen + 2;
6701 retind -= 2; /* back up before the $" */
6702 }
6703
6704 APPEND_NESTRET ();
6705 FREE (nestret);
6706 }
6707 else if MBTEST((tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
6708 /* check for $(), $[], or ${} inside command substitution. */
6709 {
6710 if ((tflags & LEX_INCASE) == 0 && open == ch) /* undo previous increment */
6711 count--;
6712 if (ch == '(') /* ) */
6713 nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
6714 else if (ch == '{') /* } */
6715 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags);
6716 else if (ch == '[') /* ] */
6717 nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
6718
6719 CHECK_NESTRET_ERROR ();
6720 APPEND_NESTRET ();
6721
6722 FREE (nestret);
6723 }
6724 if MBTEST(ch == '$' && (tflags & LEX_WASDOL) == 0)
6725 tflags |= LEX_WASDOL;
6726 else
6727 tflags &= ~LEX_WASDOL;
6728 }
6729
6730 #if defined (BANG_HISTORY)
6731 history_expansion_inhibited = orig_histexp;
6732 #endif
6733 FREE (heredelim);
6734 ret[retind] = '\0';
6735 if (lenp)
6736 *lenp = retind;
6737 /*itrace("parse_comsub:%d: returning `%s'", line_number, ret);*/
6738 return ret;
6739 }
6740
6741 /* Recursively call the parser to parse a $(...) command substitution. */
6742 char *
6743 xparse_dolparen (base, string, indp, flags)
6744 char *base;
6745 char *string;
6746 int *indp;
6747 int flags;
6748 {
6749 sh_parser_state_t ps;
6750 sh_input_line_state_t ls;
6751 int orig_ind, nc, sflags, orig_eof_token, start_lineno;
6752 char *ret, *ep, *ostring;
6753 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
6754 STRING_SAVER *saved_pushed_strings;
6755 #endif
6756
6757 /*debug_parser(1);*/
6758 orig_ind = *indp;
6759 ostring = string;
6760 start_lineno = line_number;
6761
6762 if (*string == 0)
6763 {
6764 if (flags & SX_NOALLOC)
6765 return (char *)NULL;
6766
6767 ret = xmalloc (1);
6768 ret[0] = '\0';
6769 return ret;
6770 }
6771
6772 /*itrace("xparse_dolparen: size = %d shell_input_line = `%s' string=`%s'", shell_input_line_size, shell_input_line, string);*/
6773 sflags = SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOFREE;
6774 if (flags & SX_NOLONGJMP)
6775 sflags |= SEVAL_NOLONGJMP;
6776 save_parser_state (&ps);
6777 save_input_line_state (&ls);
6778 orig_eof_token = shell_eof_token;
6779 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
6780 saved_pushed_strings = pushed_string_list; /* separate parsing context */
6781 pushed_string_list = (STRING_SAVER *)NULL;
6782 #endif
6783
6784 /*(*/
6785 parser_state |= PST_CMDSUBST|PST_EOFTOKEN; /* allow instant ')' */ /*(*/
6786 shell_eof_token = ')';
6787
6788 /* Should we save and restore the bison/yacc lookahead token (yychar) here?
6789 Or only if it's not YYEMPTY? */
6790
6791 nc = parse_string (string, "command substitution", sflags, &ep);
6792
6793 if (current_token == shell_eof_token)
6794 yyclearin; /* might want to clear lookahead token unconditionally */
6795
6796 reset_parser ();
6797 /* reset_parser() clears shell_input_line and associated variables, including
6798 parser_state, so we want to reset things, then restore what we need. */
6799 restore_input_line_state (&ls);
6800
6801 shell_eof_token = orig_eof_token;
6802 restore_parser_state (&ps);
6803
6804 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
6805 pushed_string_list = saved_pushed_strings;
6806 #endif
6807
6808 token_to_read = 0;
6809
6810 /* If parse_string returns < 0, we need to jump to top level with the
6811 negative of the return value. We abandon the rest of this input line
6812 first */
6813 if (nc < 0)
6814 {
6815 clear_shell_input_line (); /* XXX */
6816 if (bash_input.type != st_string) /* paranoia */
6817 parser_state &= ~(PST_CMDSUBST|PST_EOFTOKEN);
6818 jump_to_top_level (-nc); /* XXX */
6819 }
6820
6821 /* Need to find how many characters parse_and_execute consumed, update
6822 *indp, if flags != 0, copy the portion of the string parsed into RET
6823 and return it. If flags & 1 (SX_NOALLOC) we can return NULL. */
6824
6825 /*(*/
6826 if (ep[-1] != ')')
6827 {
6828 #if DEBUG
6829 if (ep[-1] != '\n')
6830 itrace("xparse_dolparen:%d: ep[-1] != RPAREN (%d), ep = `%s'", line_number, ep[-1], ep);
6831 #endif
6832
6833 while (ep > ostring && ep[-1] == '\n') ep--;
6834 }
6835
6836 nc = ep - ostring;
6837 *indp = ep - base - 1;
6838
6839 /*((*/
6840 #if DEBUG
6841 if (base[*indp] != ')')
6842 itrace("xparse_dolparen:%d: base[%d] != RPAREN (%d), base = `%s'", line_number, *indp, base[*indp], base);
6843 if (*indp < orig_ind)
6844 itrace("xparse_dolparen:%d: *indp (%d) < orig_ind (%d), orig_string = `%s'", line_number, *indp, orig_ind, ostring);
6845 #endif
6846
6847 if (base[*indp] != ')')
6848 {
6849 /*(*/
6850 parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), ')');
6851 jump_to_top_level (DISCARD);
6852 }
6853
6854 if (flags & SX_NOALLOC)
6855 return (char *)NULL;
6856
6857 if (nc == 0)
6858 {
6859 ret = xmalloc (1);
6860 ret[0] = '\0';
6861 }
6862 else
6863 ret = substring (ostring, 0, nc - 1);
6864
6865 return ret;
6866 }
6867
6868 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
6869 /* Parse a double-paren construct. It can be either an arithmetic
6870 command, an arithmetic `for' command, or a nested subshell. Returns
6871 the parsed token, -1 on error, or -2 if we didn't do anything and
6872 should just go on. */
6873 static int
6874 parse_dparen (c)
6875 int c;
6876 {
6877 int cmdtyp, sline;
6878 char *wval;
6879 WORD_DESC *wd;
6880
6881 #if defined (ARITH_FOR_COMMAND)
6882 if (last_read_token == FOR)
6883 {
6884 arith_for_lineno = line_number;
6885 cmdtyp = parse_arith_cmd (&wval, 0);
6886 if (cmdtyp == 1)
6887 {
6888 wd = alloc_word_desc ();
6889 wd->word = wval;
6890 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
6891 return (ARITH_FOR_EXPRS);
6892 }
6893 else
6894 return -1; /* ERROR */
6895 }
6896 #endif
6897
6898 #if defined (DPAREN_ARITHMETIC)
6899 if (reserved_word_acceptable (last_read_token))
6900 {
6901 sline = line_number;
6902
6903 cmdtyp = parse_arith_cmd (&wval, 0);
6904 if (cmdtyp == 1) /* arithmetic command */
6905 {
6906 wd = alloc_word_desc ();
6907 wd->word = wval;
6908 wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE;
6909 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
6910 return (ARITH_CMD);
6911 }
6912 else if (cmdtyp == 0) /* nested subshell */
6913 {
6914 push_string (wval, 0, (alias_t *)NULL);
6915 pushed_string_list->flags = PSH_DPAREN;
6916 if ((parser_state & PST_CASEPAT) == 0)
6917 parser_state |= PST_SUBSHELL;
6918 return (c);
6919 }
6920 else /* ERROR */
6921 return -1;
6922 }
6923 #endif
6924
6925 return -2; /* XXX */
6926 }
6927
6928 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
6929 If not, assume it's a nested subshell for backwards compatibility and
6930 return 0. In any case, put the characters we've consumed into a locally-
6931 allocated buffer and make *ep point to that buffer. Return -1 on an
6932 error, for example EOF. */
6933 static int
6934 parse_arith_cmd (ep, adddq)
6935 char **ep;
6936 int adddq;
6937 {
6938 int exp_lineno, rval, c;
6939 char *ttok, *tokstr;
6940 int ttoklen;
6941
6942 exp_lineno = line_number;
6943 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
6944 rval = 1;
6945 if (ttok == &matched_pair_error)
6946 return -1;
6947 /* Check that the next character is the closing right paren. If
6948 not, this is a syntax error. ( */
6949 c = shell_getc (0);
6950 if MBTEST(c != ')')
6951 rval = 0;
6952
6953 tokstr = (char *)xmalloc (ttoklen + 4);
6954
6955 /* if ADDDQ != 0 then (( ... )) -> "..." */
6956 if (rval == 1 && adddq) /* arith cmd, add double quotes */
6957 {
6958 tokstr[0] = '"';
6959 strncpy (tokstr + 1, ttok, ttoklen - 1);
6960 tokstr[ttoklen] = '"';
6961 tokstr[ttoklen+1] = '\0';
6962 }
6963 else if (rval == 1) /* arith cmd, don't add double quotes */
6964 {
6965 strncpy (tokstr, ttok, ttoklen - 1);
6966 tokstr[ttoklen-1] = '\0';
6967 }
6968 else /* nested subshell */
6969 {
6970 tokstr[0] = '(';
6971 strncpy (tokstr + 1, ttok, ttoklen - 1);
6972 tokstr[ttoklen] = ')';
6973 tokstr[ttoklen+1] = c;
6974 tokstr[ttoklen+2] = '\0';
6975 }
6976
6977 *ep = tokstr;
6978 FREE (ttok);
6979 return rval;
6980 }
6981 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
6982
6983 #if defined (COND_COMMAND)
6984 static void
6985 cond_error ()
6986 {
6987 char *etext;
6988
6989 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
6990 parser_error (cond_lineno, _("unexpected EOF while looking for `]]'"));
6991 else if (cond_token != COND_ERROR)
6992 {
6993 if (etext = error_token_from_token (cond_token))
6994 {
6995 parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext);
6996 free (etext);
6997 }
6998 else
6999 parser_error (cond_lineno, _("syntax error in conditional expression"));
7000 }
7001 }
7002
7003 static COND_COM *
7004 cond_expr ()
7005 {
7006 return (cond_or ());
7007 }
7008
7009 static COND_COM *
7010 cond_or ()
7011 {
7012 COND_COM *l, *r;
7013
7014 l = cond_and ();
7015 if (cond_token == OR_OR)
7016 {
7017 r = cond_or ();
7018 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
7019 }
7020 return l;
7021 }
7022
7023 static COND_COM *
7024 cond_and ()
7025 {
7026 COND_COM *l, *r;
7027
7028 l = cond_term ();
7029 if (cond_token == AND_AND)
7030 {
7031 r = cond_and ();
7032 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
7033 }
7034 return l;
7035 }
7036
7037 static int
7038 cond_skip_newlines ()
7039 {
7040 while ((cond_token = read_token (READ)) == '\n')
7041 {
7042 if (SHOULD_PROMPT ())
7043 prompt_again ();
7044 }
7045 return (cond_token);
7046 }
7047
7048 #define COND_RETURN_ERROR() \
7049 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
7050
7051 static COND_COM *
7052 cond_term ()
7053 {
7054 WORD_DESC *op;
7055 COND_COM *term, *tleft, *tright;
7056 int tok, lineno;
7057 char *etext;
7058
7059 /* Read a token. It can be a left paren, a `!', a unary operator, or a
7060 word that should be the first argument of a binary operator. Start by
7061 skipping newlines, since this is a compound command. */
7062 tok = cond_skip_newlines ();
7063 lineno = line_number;
7064 if (tok == COND_END)
7065 {
7066 COND_RETURN_ERROR ();
7067 }
7068 else if (tok == '(')
7069 {
7070 term = cond_expr ();
7071 if (cond_token != ')')
7072 {
7073 if (term)
7074 dispose_cond_node (term); /* ( */
7075 if (etext = error_token_from_token (cond_token))
7076 {
7077 parser_error (lineno, _("unexpected token `%s', expected `)'"), etext);
7078 free (etext);
7079 }
7080 else
7081 parser_error (lineno, _("expected `)'"));
7082 COND_RETURN_ERROR ();
7083 }
7084 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
7085 (void)cond_skip_newlines ();
7086 }
7087 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
7088 {
7089 if (tok == WORD)
7090 dispose_word (yylval.word); /* not needed */
7091 term = cond_term ();
7092 if (term)
7093 term->flags |= CMD_INVERT_RETURN;
7094 }
7095 else if (tok == WORD && yylval.word->word[0] == '-' && yylval.word->word[1] && yylval.word->word[2] == 0 && test_unop (yylval.word->word))
7096 {
7097 op = yylval.word;
7098 tok = read_token (READ);
7099 if (tok == WORD)
7100 {
7101 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
7102 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
7103 }
7104 else
7105 {
7106 dispose_word (op);
7107 if (etext = error_token_from_token (tok))
7108 {
7109 parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext);
7110 free (etext);
7111 }
7112 else
7113 parser_error (line_number, _("unexpected argument to conditional unary operator"));
7114 COND_RETURN_ERROR ();
7115 }
7116
7117 (void)cond_skip_newlines ();
7118 }
7119 else if (tok == WORD) /* left argument to binary operator */
7120 {
7121 /* lhs */
7122 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
7123
7124 /* binop */
7125 tok = read_token (READ);
7126 if (tok == WORD && test_binop (yylval.word->word))
7127 {
7128 op = yylval.word;
7129 if (op->word[0] == '=' && (op->word[1] == '\0' || (op->word[1] == '=' && op->word[2] == '\0')))
7130 parser_state |= PST_EXTPAT;
7131 else if (op->word[0] == '!' && op->word[1] == '=' && op->word[2] == '\0')
7132 parser_state |= PST_EXTPAT;
7133 }
7134 #if defined (COND_REGEXP)
7135 else if (tok == WORD && STREQ (yylval.word->word, "=~"))
7136 {
7137 op = yylval.word;
7138 parser_state |= PST_REGEXP;
7139 }
7140 #endif
7141 else if (tok == '<' || tok == '>')
7142 op = make_word_from_token (tok); /* ( */
7143 /* There should be a check before blindly accepting the `)' that we have
7144 seen the opening `('. */
7145 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
7146 {
7147 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
7148 the test command. Similarly for [[ x && expr ]] or
7149 [[ x || expr ]] or [[ (x) ]]. */
7150 op = make_word ("-n");
7151 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
7152 cond_token = tok;
7153 return (term);
7154 }
7155 else
7156 {
7157 if (etext = error_token_from_token (tok))
7158 {
7159 parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext);
7160 free (etext);
7161 }
7162 else
7163 parser_error (line_number, _("conditional binary operator expected"));
7164 dispose_cond_node (tleft);
7165 COND_RETURN_ERROR ();
7166 }
7167
7168 /* rhs */
7169 if (parser_state & PST_EXTPAT)
7170 extended_glob = 1;
7171 tok = read_token (READ);
7172 if (parser_state & PST_EXTPAT)
7173 extended_glob = global_extglob;
7174 parser_state &= ~(PST_REGEXP|PST_EXTPAT);
7175
7176 if (tok == WORD)
7177 {
7178 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
7179 term = make_cond_node (COND_BINARY, op, tleft, tright);
7180 }
7181 else
7182 {
7183 if (etext = error_token_from_token (tok))
7184 {
7185 parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext);
7186 free (etext);
7187 }
7188 else
7189 parser_error (line_number, _("unexpected argument to conditional binary operator"));
7190 dispose_cond_node (tleft);
7191 dispose_word (op);
7192 COND_RETURN_ERROR ();
7193 }
7194
7195 (void)cond_skip_newlines ();
7196 }
7197 else
7198 {
7199 if (tok < 256)
7200 parser_error (line_number, _("unexpected token `%c' in conditional command"), tok);
7201 else if (etext = error_token_from_token (tok))
7202 {
7203 parser_error (line_number, _("unexpected token `%s' in conditional command"), etext);
7204 free (etext);
7205 }
7206 else
7207 parser_error (line_number, _("unexpected token %d in conditional command"), tok);
7208 COND_RETURN_ERROR ();
7209 }
7210 return (term);
7211 }
7212
7213 /* This is kind of bogus -- we slip a mini recursive-descent parser in
7214 here to handle the conditional statement syntax. */
7215 static COMMAND *
7216 parse_cond_command ()
7217 {
7218 COND_COM *cexp;
7219
7220 global_extglob = extended_glob;
7221 cexp = cond_expr ();
7222 return (make_cond_command (cexp));
7223 }
7224 #endif
7225
7226 #if defined (ARRAY_VARS)
7227 /* When this is called, it's guaranteed that we don't care about anything
7228 in t beyond i. We use a buffer with room for the characters we add just
7229 in case assignment() ends up doing something like parsing a command
7230 substitution that will reallocate atoken. We don't want to write beyond
7231 the end of an allocated buffer. */
7232 static int
7233 token_is_assignment (t, i)
7234 char *t;
7235 int i;
7236 {
7237 int r;
7238 char *atoken;
7239
7240 atoken = xmalloc (i + 3);
7241 memcpy (atoken, t, i);
7242 atoken[i] = '=';
7243 atoken[i+1] = '\0';
7244
7245 r = assignment (atoken, (parser_state & PST_COMPASSIGN) != 0);
7246
7247 free (atoken);
7248
7249 /* XXX - check that r == i to avoid returning false positive for
7250 t containing `=' before t[i]. */
7251 return (r > 0 && r == i);
7252 }
7253
7254 /* XXX - possible changes here for `+=' */
7255 static int
7256 token_is_ident (t, i)
7257 char *t;
7258 int i;
7259 {
7260 unsigned char c;
7261 int r;
7262
7263 c = t[i];
7264 t[i] = '\0';
7265 r = legal_identifier (t);
7266 t[i] = c;
7267 return r;
7268 }
7269 #endif
7270
7271 static int
7272 read_token_word (character)
7273 int character;
7274 {
7275 /* The value for YYLVAL when a WORD is read. */
7276 WORD_DESC *the_word;
7277
7278 /* Index into the token that we are building. */
7279 int token_index;
7280
7281 /* ALL_DIGITS becomes zero when we see a non-digit. */
7282 int all_digit_token;
7283
7284 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
7285 int dollar_present;
7286
7287 /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
7288 assignment. */
7289 int compound_assignment;
7290
7291 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
7292 int quoted;
7293
7294 /* Non-zero means to ignore the value of the next character, and just
7295 to add it no matter what. */
7296 int pass_next_character;
7297
7298 /* The current delimiting character. */
7299 int cd;
7300 int result, peek_char;
7301 char *ttok, *ttrans;
7302 int ttoklen, ttranslen;
7303 intmax_t lvalue;
7304
7305 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
7306 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
7307
7308 token_index = 0;
7309 all_digit_token = DIGIT (character);
7310 dollar_present = quoted = pass_next_character = compound_assignment = 0;
7311
7312 for (;;)
7313 {
7314 if (character == EOF)
7315 goto got_token;
7316
7317 if (pass_next_character)
7318 {
7319 pass_next_character = 0;
7320 goto got_escaped_character;
7321 }
7322
7323 cd = current_delimiter (dstack);
7324
7325 /* Handle backslashes. Quote lots of things when not inside of
7326 double-quotes, quote some things inside of double-quotes. */
7327 if MBTEST(character == '\\')
7328 {
7329 peek_char = shell_getc (0);
7330
7331 /* Backslash-newline is ignored in all cases except
7332 when quoted with single quotes. */
7333 if (peek_char == '\n')
7334 {
7335 character = '\n';
7336 goto next_character;
7337 }
7338 else
7339 {
7340 shell_ungetc (peek_char);
7341
7342 /* If the next character is to be quoted, note it now. */
7343 if (cd == 0 || cd == '`' ||
7344 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
7345 pass_next_character++;
7346
7347 quoted = 1;
7348 goto got_character;
7349 }
7350 }
7351
7352 /* Parse a matched pair of quote characters. */
7353 if MBTEST(shellquote (character))
7354 {
7355 push_delimiter (dstack, character);
7356 ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0);
7357 pop_delimiter (dstack);
7358 if (ttok == &matched_pair_error)
7359 return -1; /* Bail immediately. */
7360 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
7361 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
7362 token[token_index++] = character;
7363 strcpy (token + token_index, ttok);
7364 token_index += ttoklen;
7365 all_digit_token = 0;
7366 if (character != '`')
7367 quoted = 1;
7368 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
7369 FREE (ttok);
7370 goto next_character;
7371 }
7372
7373 #ifdef COND_REGEXP
7374 /* When parsing a regexp as a single word inside a conditional command,
7375 we need to special-case characters special to both the shell and
7376 regular expressions. Right now, that is only '(' and '|'. */ /*)*/
7377 if MBTEST((parser_state & PST_REGEXP) && (character == '(' || character == '|')) /*)*/
7378 {
7379 if (character == '|')
7380 goto got_character;
7381
7382 push_delimiter (dstack, character);
7383 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
7384 pop_delimiter (dstack);
7385 if (ttok == &matched_pair_error)
7386 return -1; /* Bail immediately. */
7387 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
7388 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
7389 token[token_index++] = character;
7390 strcpy (token + token_index, ttok);
7391 token_index += ttoklen;
7392 FREE (ttok);
7393 dollar_present = all_digit_token = 0;
7394 goto next_character;
7395 }
7396 #endif /* COND_REGEXP */
7397
7398 #ifdef EXTENDED_GLOB
7399 /* Parse a ksh-style extended pattern matching specification. */
7400 if MBTEST(extended_glob && PATTERN_CHAR (character))
7401 {
7402 peek_char = shell_getc (1);
7403 if MBTEST(peek_char == '(') /* ) */
7404 {
7405 push_delimiter (dstack, peek_char);
7406 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
7407 pop_delimiter (dstack);
7408 if (ttok == &matched_pair_error)
7409 return -1; /* Bail immediately. */
7410 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,
7411 token_buffer_size,
7412 TOKEN_DEFAULT_GROW_SIZE);
7413 token[token_index++] = character;
7414 token[token_index++] = peek_char;
7415 strcpy (token + token_index, ttok);
7416 token_index += ttoklen;
7417 FREE (ttok);
7418 dollar_present = all_digit_token = 0;
7419 goto next_character;
7420 }
7421 else
7422 shell_ungetc (peek_char);
7423 }
7424 #endif /* EXTENDED_GLOB */
7425
7426 /* If the delimiter character is not single quote, parse some of
7427 the shell expansions that must be read as a single word. */
7428 if (shellexp (character))
7429 {
7430 peek_char = shell_getc (1);
7431 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
7432 if MBTEST(peek_char == '(' ||
7433 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
7434 {
7435 if (peek_char == '{') /* } */
7436 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE|P_DOLBRACE);
7437 else if (peek_char == '(') /* ) */
7438 {
7439 /* XXX - push and pop the `(' as a delimiter for use by
7440 the command-oriented-history code. This way newlines
7441 appearing in the $(...) string get added to the
7442 history literally rather than causing a possibly-
7443 incorrect `;' to be added. ) */
7444 push_delimiter (dstack, peek_char);
7445 ttok = parse_comsub (cd, '(', ')', &ttoklen, P_COMMAND);
7446 pop_delimiter (dstack);
7447 }
7448 else
7449 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
7450 if (ttok == &matched_pair_error)
7451 return -1; /* Bail immediately. */
7452 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,
7453 token_buffer_size,
7454 TOKEN_DEFAULT_GROW_SIZE);
7455 token[token_index++] = character;
7456 token[token_index++] = peek_char;
7457 strcpy (token + token_index, ttok);
7458 token_index += ttoklen;
7459 FREE (ttok);
7460 dollar_present = 1;
7461 all_digit_token = 0;
7462 goto next_character;
7463 }
7464 /* This handles $'...' and $"..." new-style quoted strings. */
7465 else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
7466 {
7467 int first_line;
7468
7469 first_line = line_number;
7470 push_delimiter (dstack, peek_char);
7471 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
7472 &ttoklen,
7473 (peek_char == '\'') ? P_ALLOWESC : 0);
7474 pop_delimiter (dstack);
7475 if (ttok == &matched_pair_error)
7476 return -1;
7477 if (peek_char == '\'')
7478 {
7479 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
7480 free (ttok);
7481
7482 /* Insert the single quotes and correctly quote any
7483 embedded single quotes (allowed because P_ALLOWESC was
7484 passed to parse_matched_pair). */
7485 ttok = sh_single_quote (ttrans);
7486 free (ttrans);
7487 ttranslen = strlen (ttok);
7488 ttrans = ttok;
7489 }
7490 else
7491 {
7492 /* Try to locale-expand the converted string. */
7493 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
7494 free (ttok);
7495
7496 /* Add the double quotes back */
7497 ttok = sh_mkdoublequoted (ttrans, ttranslen, 0);
7498 free (ttrans);
7499 ttranslen += 2;
7500 ttrans = ttok;
7501 }
7502
7503 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 1,
7504 token_buffer_size,
7505 TOKEN_DEFAULT_GROW_SIZE);
7506 strcpy (token + token_index, ttrans);
7507 token_index += ttranslen;
7508 FREE (ttrans);
7509 quoted = 1;
7510 all_digit_token = 0;
7511 goto next_character;
7512 }
7513 /* This could eventually be extended to recognize all of the
7514 shell's single-character parameter expansions, and set flags.*/
7515 else if MBTEST(character == '$' && peek_char == '$')
7516 {
7517 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
7518 token_buffer_size,
7519 TOKEN_DEFAULT_GROW_SIZE);
7520 token[token_index++] = '$';
7521 token[token_index++] = peek_char;
7522 dollar_present = 1;
7523 all_digit_token = 0;
7524 goto next_character;
7525 }
7526 else
7527 shell_ungetc (peek_char);
7528 }
7529
7530 #if defined (ARRAY_VARS)
7531 /* Identify possible array subscript assignment; match [...]. If
7532 parser_state&PST_COMPASSIGN, we need to parse [sub]=words treating
7533 `sub' as if it were enclosed in double quotes. */
7534 else if MBTEST(character == '[' && /* ] */
7535 ((token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) ||
7536 (token_index == 0 && (parser_state&PST_COMPASSIGN))))
7537 {
7538 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, P_ARRAYSUB);
7539 if (ttok == &matched_pair_error)
7540 return -1; /* Bail immediately. */
7541 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
7542 token_buffer_size,
7543 TOKEN_DEFAULT_GROW_SIZE);
7544 token[token_index++] = character;
7545 strcpy (token + token_index, ttok);
7546 token_index += ttoklen;
7547 FREE (ttok);
7548 all_digit_token = 0;
7549 goto next_character;
7550 }
7551 /* Identify possible compound array variable assignment. */
7552 else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index))
7553 {
7554 peek_char = shell_getc (1);
7555 if MBTEST(peek_char == '(') /* ) */
7556 {
7557 ttok = parse_compound_assignment (&ttoklen);
7558
7559 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
7560 token_buffer_size,
7561 TOKEN_DEFAULT_GROW_SIZE);
7562
7563 token[token_index++] = '=';
7564 token[token_index++] = '(';
7565 if (ttok)
7566 {
7567 strcpy (token + token_index, ttok);
7568 token_index += ttoklen;
7569 }
7570 token[token_index++] = ')';
7571 FREE (ttok);
7572 all_digit_token = 0;
7573 compound_assignment = 1;
7574 #if 1
7575 goto next_character;
7576 #else
7577 goto got_token; /* ksh93 seems to do this */
7578 #endif
7579 }
7580 else
7581 shell_ungetc (peek_char);
7582 }
7583 #endif
7584
7585 /* When not parsing a multi-character word construct, shell meta-
7586 characters break words. */
7587 if MBTEST(shellbreak (character))
7588 {
7589 shell_ungetc (character);
7590 goto got_token;
7591 }
7592
7593 got_character:
7594 if (character == CTLESC || character == CTLNUL)
7595 {
7596 RESIZE_MALLOCED_BUFFER (token, token_index, 2, token_buffer_size,
7597 TOKEN_DEFAULT_GROW_SIZE);
7598 token[token_index++] = CTLESC;
7599 }
7600 else
7601 got_escaped_character:
7602 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
7603 TOKEN_DEFAULT_GROW_SIZE);
7604
7605 token[token_index++] = character;
7606
7607 all_digit_token &= DIGIT (character);
7608 dollar_present |= character == '$';
7609
7610 next_character:
7611 if (character == '\n' && SHOULD_PROMPT ())
7612 prompt_again ();
7613
7614 /* We want to remove quoted newlines (that is, a \<newline> pair)
7615 unless we are within single quotes or pass_next_character is
7616 set (the shell equivalent of literal-next). */
7617 cd = current_delimiter (dstack);
7618 character = shell_getc (cd != '\'' && pass_next_character == 0);
7619 } /* end for (;;) */
7620
7621 got_token:
7622
7623 /* Calls to RESIZE_MALLOCED_BUFFER ensure there is sufficient room. */
7624 token[token_index] = '\0';
7625
7626 /* Check to see what thing we should return. If the last_read_token
7627 is a `<', or a `&', or the character which ended this token is
7628 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
7629 Otherwise, it is just a word, and should be returned as such. */
7630 if MBTEST(all_digit_token && (character == '<' || character == '>' ||
7631 last_read_token == LESS_AND ||
7632 last_read_token == GREATER_AND))
7633 {
7634 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
7635 {
7636 yylval.number = lvalue;
7637 return (NUMBER);
7638 }
7639 }
7640
7641 /* Check for special case tokens. */
7642 result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
7643 if (result >= 0)
7644 return result;
7645
7646 #if defined (ALIAS)
7647 /* Posix.2 does not allow reserved words to be aliased, so check for all
7648 of them, including special cases, before expanding the current token
7649 as an alias. */
7650 if MBTEST(posixly_correct)
7651 CHECK_FOR_RESERVED_WORD (token);
7652
7653 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
7654 inhibits alias expansion. */
7655 if (expand_aliases && quoted == 0)
7656 {
7657 result = alias_expand_token (token);
7658 if (result == RE_READ_TOKEN)
7659 return (RE_READ_TOKEN);
7660 else if (result == NO_EXPANSION)
7661 parser_state &= ~PST_ALEXPNEXT;
7662 }
7663
7664 /* If not in Posix.2 mode, check for reserved words after alias
7665 expansion. */
7666 if MBTEST(posixly_correct == 0)
7667 #endif
7668 CHECK_FOR_RESERVED_WORD (token);
7669
7670 the_word = alloc_word_desc ();
7671 the_word->word = (char *)xmalloc (1 + token_index);
7672 the_word->flags = 0;
7673 strcpy (the_word->word, token);
7674 if (dollar_present)
7675 the_word->flags |= W_HASDOLLAR;
7676 if (quoted)
7677 the_word->flags |= W_QUOTED; /*(*/
7678 if (compound_assignment && token[token_index-1] == ')')
7679 the_word->flags |= W_COMPASSIGN;
7680 /* A word is an assignment if it appears at the beginning of a
7681 simple command, or after another assignment word. This is
7682 context-dependent, so it cannot be handled in the grammar. */
7683 if (assignment (token, (parser_state & PST_COMPASSIGN) != 0))
7684 {
7685 the_word->flags |= W_ASSIGNMENT;
7686 /* Don't perform word splitting on assignment statements. */
7687 if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0)
7688 {
7689 the_word->flags |= W_NOSPLIT;
7690 if (parser_state & PST_COMPASSIGN)
7691 the_word->flags |= W_NOGLOB; /* XXX - W_NOBRACE? */
7692 }
7693 }
7694
7695 if (command_token_position (last_read_token))
7696 {
7697 struct builtin *b;
7698 b = builtin_address_internal (token, 0);
7699 if (b && (b->flags & ASSIGNMENT_BUILTIN))
7700 parser_state |= PST_ASSIGNOK;
7701 else if (STREQ (token, "eval") || STREQ (token, "let"))
7702 parser_state |= PST_ASSIGNOK;
7703 }
7704
7705 yylval.word = the_word;
7706
7707 /* should we check that quoted == 0 as well? */
7708 if (token[0] == '{' && token[token_index-1] == '}' &&
7709 (character == '<' || character == '>'))
7710 {
7711 /* can use token; already copied to the_word */
7712 token[token_index-1] = '\0';
7713 #if defined (ARRAY_VARS)
7714 if (legal_identifier (token+1) || valid_array_reference (token+1, 0))
7715 #else
7716 if (legal_identifier (token+1))
7717 #endif
7718 {
7719 strcpy (the_word->word, token+1);
7720 /* itrace("read_token_word: returning REDIR_WORD for %s", the_word->word); */
7721 yylval.word = the_word; /* accommodate recursive call */
7722 return (REDIR_WORD);
7723 }
7724 else
7725 /* valid_array_reference can call the parser recursively; need to
7726 make sure that yylval.word doesn't change if we are going to
7727 return WORD or ASSIGNMENT_WORD */
7728 yylval.word = the_word;
7729 }
7730
7731 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
7732 ? ASSIGNMENT_WORD : WORD;
7733
7734 switch (last_read_token)
7735 {
7736 case FUNCTION:
7737 parser_state |= PST_ALLOWOPNBRC;
7738 function_dstart = line_number;
7739 break;
7740 case CASE:
7741 case SELECT:
7742 case FOR:
7743 if (word_top < MAX_CASE_NEST)
7744 word_top++;
7745 word_lineno[word_top] = line_number;
7746 expecting_in_token++;
7747 break;
7748 }
7749
7750 return (result);
7751 }
7752
7753 /* Return 1 if TOKSYM is a token that after being read would allow
7754 a reserved word to be seen, else 0. */
7755 static int
7756 reserved_word_acceptable (toksym)
7757 int toksym;
7758 {
7759 switch (toksym)
7760 {
7761 case '\n':
7762 case ';':
7763 case '(':
7764 case ')':
7765 case '|':
7766 case '&':
7767 case '{':
7768 case '}': /* XXX */
7769 case AND_AND:
7770 case BANG:
7771 case BAR_AND:
7772 case DO:
7773 case DONE:
7774 case ELIF:
7775 case ELSE:
7776 case ESAC:
7777 case FI:
7778 case IF:
7779 case OR_OR:
7780 case SEMI_SEMI:
7781 case SEMI_AND:
7782 case SEMI_SEMI_AND:
7783 case THEN:
7784 case TIME:
7785 case TIMEOPT:
7786 case TIMEIGN:
7787 case COPROC:
7788 case UNTIL:
7789 case WHILE:
7790 case 0:
7791 return 1;
7792 default:
7793 #if defined (COPROCESS_SUPPORT)
7794 if (last_read_token == WORD && token_before_that == COPROC)
7795 return 1;
7796 #endif
7797 if (last_read_token == WORD && token_before_that == FUNCTION)
7798 return 1;
7799 return 0;
7800 }
7801 }
7802
7803 /* Return the index of TOKEN in the alist of reserved words, or -1 if
7804 TOKEN is not a shell reserved word. */
7805 int
7806 find_reserved_word (tokstr)
7807 char *tokstr;
7808 {
7809 int i;
7810 for (i = 0; word_token_alist[i].word; i++)
7811 if (STREQ (tokstr, word_token_alist[i].word))
7812 return i;
7813 return -1;
7814 }
7815
7816 /* An interface to let the rest of the shell (primarily the completion
7817 system) know what the parser is expecting. */
7818 int
7819 parser_in_command_position ()
7820 {
7821 return (command_token_position (last_read_token));
7822 }
7823
7824 #if 0
7825 #if defined (READLINE)
7826 /* Called after each time readline is called. This insures that whatever
7827 the new prompt string is gets propagated to readline's local prompt
7828 variable. */
7829 static void
7830 reset_readline_prompt ()
7831 {
7832 char *temp_prompt;
7833
7834 if (prompt_string_pointer)
7835 {
7836 temp_prompt = (*prompt_string_pointer)
7837 ? decode_prompt_string (*prompt_string_pointer)
7838 : (char *)NULL;
7839
7840 if (temp_prompt == 0)
7841 {
7842 temp_prompt = (char *)xmalloc (1);
7843 temp_prompt[0] = '\0';
7844 }
7845
7846 FREE (current_readline_prompt);
7847 current_readline_prompt = temp_prompt;
7848 }
7849 }
7850 #endif /* READLINE */
7851 #endif /* 0 */
7852
7853 #if defined (HISTORY)
7854 /* A list of tokens which can be followed by newlines, but not by
7855 semi-colons. When concatenating multiple lines of history, the
7856 newline separator for such tokens is replaced with a space. */
7857 static const int no_semi_successors[] = {
7858 '\n', '{', '(', ')', ';', '&', '|',
7859 CASE, DO, ELSE, IF, SEMI_SEMI, SEMI_AND, SEMI_SEMI_AND, THEN, UNTIL,
7860 WHILE, AND_AND, OR_OR, IN,
7861 0
7862 };
7863
7864 /* If we are not within a delimited expression, try to be smart
7865 about which separators can be semi-colons and which must be
7866 newlines. Returns the string that should be added into the
7867 history entry. LINE is the line we're about to add; it helps
7868 make some more intelligent decisions in certain cases. */
7869 char *
7870 history_delimiting_chars (line)
7871 const char *line;
7872 {
7873 static int last_was_heredoc = 0; /* was the last entry the start of a here document? */
7874 register int i;
7875
7876 if ((parser_state & PST_HEREDOC) == 0)
7877 last_was_heredoc = 0;
7878
7879 if (dstack.delimiter_depth != 0)
7880 return ("\n");
7881
7882 /* We look for current_command_line_count == 2 because we are looking to
7883 add the first line of the body of the here document (the second line
7884 of the command). We also keep LAST_WAS_HEREDOC as a private sentinel
7885 variable to note when we think we added the first line of a here doc
7886 (the one with a "<<" somewhere in it) */
7887 if (parser_state & PST_HEREDOC)
7888 {
7889 if (last_was_heredoc)
7890 {
7891 last_was_heredoc = 0;
7892 return "\n";
7893 }
7894 return (here_doc_first_line ? "\n" : "");
7895 }
7896
7897 if (parser_state & PST_COMPASSIGN)
7898 return (" ");
7899
7900 /* First, handle some special cases. */
7901 /*(*/
7902 /* If we just read `()', assume it's a function definition, and don't
7903 add a semicolon. If the token before the `)' was not `(', and we're
7904 not in the midst of parsing a case statement, assume it's a
7905 parenthesized command and add the semicolon. */
7906 /*)(*/
7907 if (token_before_that == ')')
7908 {
7909 if (two_tokens_ago == '(') /*)*/ /* function def */
7910 return " ";
7911 /* This does not work for subshells inside case statement
7912 command lists. It's a suboptimal solution. */
7913 else if (parser_state & PST_CASESTMT) /* case statement pattern */
7914 return " ";
7915 else
7916 return "; "; /* (...) subshell */
7917 }
7918 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
7919 return " "; /* function def using `function name' without `()' */
7920
7921 /* If we're not in a here document, but we think we're about to parse one,
7922 and we would otherwise return a `;', return a newline to delimit the
7923 line with the here-doc delimiter */
7924 else if ((parser_state & PST_HEREDOC) == 0 && current_command_line_count > 1 && last_read_token == '\n' && strstr (line, "<<"))
7925 {
7926 last_was_heredoc = 1;
7927 return "\n";
7928 }
7929 else if ((parser_state & PST_HEREDOC) == 0 && current_command_line_count > 1 && need_here_doc > 0)
7930 return "\n";
7931 else if (token_before_that == WORD && two_tokens_ago == FOR)
7932 {
7933 /* Tricky. `for i\nin ...' should not have a semicolon, but
7934 `for i\ndo ...' should. We do what we can. */
7935 for (i = shell_input_line_index; whitespace (shell_input_line[i]); i++)
7936 ;
7937 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
7938 return " ";
7939 return ";";
7940 }
7941 else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT))
7942 return " ";
7943
7944 for (i = 0; no_semi_successors[i]; i++)
7945 {
7946 if (token_before_that == no_semi_successors[i])
7947 return (" ");
7948 }
7949
7950 if (line_isblank (line))
7951 return ("");
7952
7953 return ("; ");
7954 }
7955 #endif /* HISTORY */
7956
7957 /* Issue a prompt, or prepare to issue a prompt when the next character
7958 is read. */
7959 static void
7960 prompt_again ()
7961 {
7962 char *temp_prompt;
7963
7964 if (interactive == 0 || expanding_alias ()) /* XXX */
7965 return;
7966
7967 ps1_prompt = get_string_value ("PS1");
7968 ps2_prompt = get_string_value ("PS2");
7969
7970 ps0_prompt = get_string_value ("PS0");
7971
7972 if (!prompt_string_pointer)
7973 prompt_string_pointer = &ps1_prompt;
7974
7975 temp_prompt = *prompt_string_pointer
7976 ? decode_prompt_string (*prompt_string_pointer)
7977 : (char *)NULL;
7978
7979 if (temp_prompt == 0)
7980 {
7981 temp_prompt = (char *)xmalloc (1);
7982 temp_prompt[0] = '\0';
7983 }
7984
7985 current_prompt_string = *prompt_string_pointer;
7986 prompt_string_pointer = &ps2_prompt;
7987
7988 #if defined (READLINE)
7989 if (!no_line_editing)
7990 {
7991 FREE (current_readline_prompt);
7992 current_readline_prompt = temp_prompt;
7993 }
7994 else
7995 #endif /* READLINE */
7996 {
7997 FREE (current_decoded_prompt);
7998 current_decoded_prompt = temp_prompt;
7999 }
8000 }
8001
8002 int
8003 get_current_prompt_level ()
8004 {
8005 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
8006 }
8007
8008 void
8009 set_current_prompt_level (x)
8010 int x;
8011 {
8012 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
8013 current_prompt_string = *prompt_string_pointer;
8014 }
8015
8016 static void
8017 print_prompt ()
8018 {
8019 fprintf (stderr, "%s", current_decoded_prompt);
8020 fflush (stderr);
8021 }
8022
8023 #if defined (HISTORY)
8024 /* The history library increments the history offset as soon as it stores
8025 the first line of a potentially multi-line command, so we compensate
8026 here by returning one fewer when appropriate. */
8027 static int
8028 prompt_history_number (pmt)
8029 char *pmt;
8030 {
8031 int ret;
8032
8033 ret = history_number ();
8034 if (ret == 1)
8035 return ret;
8036
8037 if (pmt == ps1_prompt) /* are we expanding $PS1? */
8038 return ret;
8039 else if (pmt == ps2_prompt && command_oriented_history == 0)
8040 return ret; /* not command oriented history */
8041 else if (pmt == ps2_prompt && command_oriented_history && current_command_first_line_saved)
8042 return ret - 1;
8043 else
8044 return ret - 1; /* PS0, PS4, ${var@P}, PS2 other cases */
8045 }
8046 #endif
8047
8048 /* Return a string which will be printed as a prompt. The string
8049 may contain special characters which are decoded as follows:
8050
8051 \a bell (ascii 07)
8052 \d the date in Day Mon Date format
8053 \e escape (ascii 033)
8054 \h the hostname up to the first `.'
8055 \H the hostname
8056 \j the number of active jobs
8057 \l the basename of the shell's tty device name
8058 \n CRLF
8059 \r CR
8060 \s the name of the shell
8061 \t the time in 24-hour hh:mm:ss format
8062 \T the time in 12-hour hh:mm:ss format
8063 \@ the time in 12-hour hh:mm am/pm format
8064 \A the time in 24-hour hh:mm format
8065 \D{fmt} the result of passing FMT to strftime(3)
8066 \u your username
8067 \v the version of bash (e.g., 2.00)
8068 \V the release of bash, version + patchlevel (e.g., 2.00.0)
8069 \w the current working directory
8070 \W the last element of $PWD
8071 \! the history number of this command
8072 \# the command number of this command
8073 \$ a $ or a # if you are root
8074 \nnn character code nnn in octal
8075 \\ a backslash
8076 \[ begin a sequence of non-printing chars
8077 \] end a sequence of non-printing chars
8078 */
8079 #define PROMPT_GROWTH 48
8080 char *
8081 decode_prompt_string (string)
8082 char *string;
8083 {
8084 WORD_LIST *list;
8085 char *result, *t, *orig_string;
8086 struct dstack save_dstack;
8087 int last_exit_value, last_comsub_pid;
8088 #if defined (PROMPT_STRING_DECODE)
8089 size_t result_size;
8090 int result_index;
8091 int c, n, i;
8092 char *temp, *t_host, octal_string[4];
8093 struct tm *tm;
8094 time_t the_time;
8095 char timebuf[128];
8096 char *timefmt;
8097
8098 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
8099 result[result_index = 0] = 0;
8100 temp = (char *)NULL;
8101 orig_string = string;
8102
8103 while (c = *string++)
8104 {
8105 if (posixly_correct && c == '!')
8106 {
8107 if (*string == '!')
8108 {
8109 temp = savestring ("!");
8110 goto add_string;
8111 }
8112 else
8113 {
8114 #if !defined (HISTORY)
8115 temp = savestring ("1");
8116 #else /* HISTORY */
8117 temp = itos (prompt_history_number (orig_string));
8118 #endif /* HISTORY */
8119 string--; /* add_string increments string again. */
8120 goto add_string;
8121 }
8122 }
8123 if (c == '\\')
8124 {
8125 c = *string;
8126
8127 switch (c)
8128 {
8129 case '0':
8130 case '1':
8131 case '2':
8132 case '3':
8133 case '4':
8134 case '5':
8135 case '6':
8136 case '7':
8137 strncpy (octal_string, string, 3);
8138 octal_string[3] = '\0';
8139
8140 n = read_octal (octal_string);
8141 temp = (char *)xmalloc (3);
8142
8143 if (n == CTLESC || n == CTLNUL)
8144 {
8145 temp[0] = CTLESC;
8146 temp[1] = n;
8147 temp[2] = '\0';
8148 }
8149 else if (n == -1)
8150 {
8151 temp[0] = '\\';
8152 temp[1] = '\0';
8153 }
8154 else
8155 {
8156 temp[0] = n;
8157 temp[1] = '\0';
8158 }
8159
8160 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
8161 string++;
8162
8163 c = 0; /* tested at add_string: */
8164 goto add_string;
8165
8166 case 'd':
8167 case 't':
8168 case 'T':
8169 case '@':
8170 case 'A':
8171 /* Make the current time/date into a string. */
8172 (void) time (&the_time);
8173 #if defined (HAVE_TZSET)
8174 sv_tz ("TZ"); /* XXX -- just make sure */
8175 #endif
8176 tm = localtime (&the_time);
8177
8178 if (c == 'd')
8179 n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm);
8180 else if (c == 't')
8181 n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm);
8182 else if (c == 'T')
8183 n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm);
8184 else if (c == '@')
8185 n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm);
8186 else if (c == 'A')
8187 n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm);
8188
8189 if (n == 0)
8190 timebuf[0] = '\0';
8191 else
8192 timebuf[sizeof(timebuf) - 1] = '\0';
8193
8194 temp = savestring (timebuf);
8195 goto add_string;
8196
8197 case 'D': /* strftime format */
8198 if (string[1] != '{') /* } */
8199 goto not_escape;
8200
8201 (void) time (&the_time);
8202 tm = localtime (&the_time);
8203 string += 2; /* skip { */
8204 timefmt = xmalloc (strlen (string) + 3);
8205 for (t = timefmt; *string && *string != '}'; )
8206 *t++ = *string++;
8207 *t = '\0';
8208 c = *string; /* tested at add_string */
8209 if (timefmt[0] == '\0')
8210 {
8211 timefmt[0] = '%';
8212 timefmt[1] = 'X'; /* locale-specific current time */
8213 timefmt[2] = '\0';
8214 }
8215 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
8216 free (timefmt);
8217
8218 if (n == 0)
8219 timebuf[0] = '\0';
8220 else
8221 timebuf[sizeof(timebuf) - 1] = '\0';
8222
8223 if (promptvars || posixly_correct)
8224 /* Make sure that expand_prompt_string is called with a
8225 second argument of Q_DOUBLE_QUOTES if we use this
8226 function here. */
8227 temp = sh_backslash_quote_for_double_quotes (timebuf);
8228 else
8229 temp = savestring (timebuf);
8230 goto add_string;
8231
8232 case 'n':
8233 temp = (char *)xmalloc (3);
8234 temp[0] = no_line_editing ? '\n' : '\r';
8235 temp[1] = no_line_editing ? '\0' : '\n';
8236 temp[2] = '\0';
8237 goto add_string;
8238
8239 case 's':
8240 temp = base_pathname (shell_name);
8241 /* Try to quote anything the user can set in the file system */
8242 if (promptvars || posixly_correct)
8243 temp = sh_backslash_quote_for_double_quotes (temp);
8244 else
8245 temp = savestring (temp);
8246 goto add_string;
8247
8248 case 'v':
8249 case 'V':
8250 temp = (char *)xmalloc (16);
8251 if (c == 'v')
8252 strcpy (temp, dist_version);
8253 else
8254 sprintf (temp, "%s.%d", dist_version, patch_level);
8255 goto add_string;
8256
8257 case 'w':
8258 case 'W':
8259 {
8260 /* Use the value of PWD because it is much more efficient. */
8261 char t_string[PATH_MAX];
8262 int tlen;
8263
8264 temp = get_string_value ("PWD");
8265
8266 if (temp == 0)
8267 {
8268 if (getcwd (t_string, sizeof(t_string)) == 0)
8269 {
8270 t_string[0] = '.';
8271 tlen = 1;
8272 }
8273 else
8274 tlen = strlen (t_string);
8275 }
8276 else
8277 {
8278 tlen = sizeof (t_string) - 1;
8279 strncpy (t_string, temp, tlen);
8280 }
8281 t_string[tlen] = '\0';
8282
8283 #if defined (MACOSX)
8284 /* Convert from "fs" format to "input" format */
8285 temp = fnx_fromfs (t_string, strlen (t_string));
8286 if (temp != t_string)
8287 strcpy (t_string, temp);
8288 #endif
8289
8290 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
8291 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
8292 /* Abbreviate \W as ~ if $PWD == $HOME */
8293 if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0))
8294 {
8295 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
8296 {
8297 t = strrchr (t_string, '/');
8298 if (t)
8299 memmove (t_string, t + 1, strlen (t)); /* strlen(t) to copy NULL */
8300 }
8301 }
8302 #undef ROOT_PATH
8303 #undef DOUBLE_SLASH_ROOT
8304 else
8305 {
8306 /* polite_directory_format is guaranteed to return a string
8307 no longer than PATH_MAX - 1 characters. */
8308 temp = polite_directory_format (t_string);
8309 if (temp != t_string)
8310 strcpy (t_string, temp);
8311 }
8312
8313 temp = trim_pathname (t_string, PATH_MAX - 1);
8314 /* If we're going to be expanding the prompt string later,
8315 quote the directory name. */
8316 if (promptvars || posixly_correct)
8317 /* Make sure that expand_prompt_string is called with a
8318 second argument of Q_DOUBLE_QUOTES if we use this
8319 function here. */
8320 temp = sh_backslash_quote_for_double_quotes (t_string);
8321 else
8322 temp = savestring (t_string);
8323
8324 goto add_string;
8325 }
8326
8327 case 'u':
8328 if (current_user.user_name == 0)
8329 get_current_user_info ();
8330 temp = savestring (current_user.user_name);
8331 goto add_string;
8332
8333 case 'h':
8334 case 'H':
8335 t_host = savestring (current_host_name);
8336 if (c == 'h' && (t = (char *)strchr (t_host, '.')))
8337 *t = '\0';
8338 if (promptvars || posixly_correct)
8339 /* Make sure that expand_prompt_string is called with a
8340 second argument of Q_DOUBLE_QUOTES if we use this
8341 function here. */
8342 temp = sh_backslash_quote_for_double_quotes (t_host);
8343 else
8344 temp = savestring (t_host);
8345 free (t_host);
8346 goto add_string;
8347
8348 case '#':
8349 n = current_command_number;
8350 /* If we have already incremented current_command_number (PS4,
8351 ${var@P}), compensate */
8352 if (orig_string != ps0_prompt && orig_string != ps1_prompt && orig_string != ps2_prompt)
8353 n--;
8354 temp = itos (n);
8355 goto add_string;
8356
8357 case '!':
8358 #if !defined (HISTORY)
8359 temp = savestring ("1");
8360 #else /* HISTORY */
8361 temp = itos (prompt_history_number (orig_string));
8362 #endif /* HISTORY */
8363 goto add_string;
8364
8365 case '$':
8366 t = temp = (char *)xmalloc (3);
8367 if ((promptvars || posixly_correct) && (current_user.euid != 0))
8368 *t++ = '\\';
8369 *t++ = current_user.euid == 0 ? '#' : '$';
8370 *t = '\0';
8371 goto add_string;
8372
8373 case 'j':
8374 temp = itos (count_all_jobs ());
8375 goto add_string;
8376
8377 case 'l':
8378 #if defined (HAVE_TTYNAME)
8379 temp = (char *)ttyname (fileno (stdin));
8380 t = temp ? base_pathname (temp) : "tty";
8381 temp = savestring (t);
8382 #else
8383 temp = savestring ("tty");
8384 #endif /* !HAVE_TTYNAME */
8385 goto add_string;
8386
8387 #if defined (READLINE)
8388 case '[':
8389 case ']':
8390 if (no_line_editing)
8391 {
8392 string++;
8393 break;
8394 }
8395 temp = (char *)xmalloc (3);
8396 n = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
8397 i = 0;
8398 if (n == CTLESC || n == CTLNUL)
8399 temp[i++] = CTLESC;
8400 temp[i++] = n;
8401 temp[i] = '\0';
8402 goto add_string;
8403 #endif /* READLINE */
8404
8405 case '\\':
8406 case 'a':
8407 case 'e':
8408 case 'r':
8409 temp = (char *)xmalloc (2);
8410 if (c == 'a')
8411 temp[0] = '\07';
8412 else if (c == 'e')
8413 temp[0] = '\033';
8414 else if (c == 'r')
8415 temp[0] = '\r';
8416 else /* (c == '\\') */
8417 temp[0] = c;
8418 temp[1] = '\0';
8419 goto add_string;
8420
8421 default:
8422 not_escape:
8423 temp = (char *)xmalloc (3);
8424 temp[0] = '\\';
8425 temp[1] = c;
8426 temp[2] = '\0';
8427
8428 add_string:
8429 if (c)
8430 string++;
8431 result =
8432 sub_append_string (temp, result, &result_index, &result_size);
8433 temp = (char *)NULL; /* Freed in sub_append_string (). */
8434 result[result_index] = '\0';
8435 break;
8436 }
8437 }
8438 else
8439 {
8440 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
8441 /* dequote_string should take care of removing this if we are not
8442 performing the rest of the word expansions. */
8443 if (c == CTLESC || c == CTLNUL)
8444 result[result_index++] = CTLESC;
8445 result[result_index++] = c;
8446 result[result_index] = '\0';
8447 }
8448 }
8449 #else /* !PROMPT_STRING_DECODE */
8450 result = savestring (string);
8451 #endif /* !PROMPT_STRING_DECODE */
8452
8453 /* Save the delimiter stack and point `dstack' to temp space so any
8454 command substitutions in the prompt string won't result in screwing
8455 up the parser's quoting state. */
8456 save_dstack = dstack;
8457 dstack = temp_dstack;
8458 dstack.delimiter_depth = 0;
8459
8460 /* Perform variable and parameter expansion and command substitution on
8461 the prompt string. */
8462 if (promptvars || posixly_correct)
8463 {
8464 last_exit_value = last_command_exit_value;
8465 last_comsub_pid = last_command_subst_pid;
8466 list = expand_prompt_string (result, Q_DOUBLE_QUOTES, 0);
8467 free (result);
8468 result = string_list (list);
8469 dispose_words (list);
8470 last_command_exit_value = last_exit_value;
8471 last_command_subst_pid = last_comsub_pid;
8472 }
8473 else
8474 {
8475 t = dequote_string (result);
8476 free (result);
8477 result = t;
8478 }
8479
8480 dstack = save_dstack;
8481
8482 return (result);
8483 }
8484
8485 /************************************************
8486 * *
8487 * ERROR HANDLING *
8488 * *
8489 ************************************************/
8490
8491 /* Report a syntax error, and restart the parser. Call here for fatal
8492 errors. */
8493 int
8494 yyerror (msg)
8495 const char *msg;
8496 {
8497 report_syntax_error ((char *)NULL);
8498 reset_parser ();
8499 return (0);
8500 }
8501
8502 static char *
8503 error_token_from_token (tok)
8504 int tok;
8505 {
8506 char *t;
8507
8508 if (t = find_token_in_alist (tok, word_token_alist, 0))
8509 return t;
8510
8511 if (t = find_token_in_alist (tok, other_token_alist, 0))
8512 return t;
8513
8514 t = (char *)NULL;
8515 /* This stuff is dicy and needs closer inspection */
8516 switch (current_token)
8517 {
8518 case WORD:
8519 case ASSIGNMENT_WORD:
8520 if (yylval.word)
8521 t = savestring (yylval.word->word);
8522 break;
8523 case NUMBER:
8524 t = itos (yylval.number);
8525 break;
8526 case ARITH_CMD:
8527 if (yylval.word_list)
8528 t = string_list (yylval.word_list);
8529 break;
8530 case ARITH_FOR_EXPRS:
8531 if (yylval.word_list)
8532 t = string_list_internal (yylval.word_list, " ; ");
8533 break;
8534 case COND_CMD:
8535 t = (char *)NULL; /* punt */
8536 break;
8537 }
8538
8539 return t;
8540 }
8541
8542 static char *
8543 error_token_from_text ()
8544 {
8545 char *msg, *t;
8546 int token_end, i;
8547
8548 t = shell_input_line;
8549 i = shell_input_line_index;
8550 token_end = 0;
8551 msg = (char *)NULL;
8552
8553 if (i && t[i] == '\0')
8554 i--;
8555
8556 while (i && (whitespace (t[i]) || t[i] == '\n'))
8557 i--;
8558
8559 if (i)
8560 token_end = i + 1;
8561
8562 while (i && (member (t[i], " \n\t;|&") == 0))
8563 i--;
8564
8565 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
8566 i++;
8567
8568 /* Return our idea of the offending token. */
8569 if (token_end || (i == 0 && token_end == 0))
8570 {
8571 if (token_end)
8572 msg = substring (t, i, token_end);
8573 else /* one-character token */
8574 {
8575 msg = (char *)xmalloc (2);
8576 msg[0] = t[i];
8577 msg[1] = '\0';
8578 }
8579 }
8580
8581 return (msg);
8582 }
8583
8584 static void
8585 print_offending_line ()
8586 {
8587 char *msg;
8588 int token_end;
8589
8590 msg = savestring (shell_input_line);
8591 token_end = strlen (msg);
8592 while (token_end && msg[token_end - 1] == '\n')
8593 msg[--token_end] = '\0';
8594
8595 parser_error (line_number, "`%s'", msg);
8596 free (msg);
8597 }
8598
8599 /* Report a syntax error with line numbers, etc.
8600 Call here for recoverable errors. If you have a message to print,
8601 then place it in MESSAGE, otherwise pass NULL and this will figure
8602 out an appropriate message for you. */
8603 static void
8604 report_syntax_error (message)
8605 char *message;
8606 {
8607 char *msg, *p;
8608
8609 if (message)
8610 {
8611 parser_error (line_number, "%s", message);
8612 if (interactive && EOF_Reached)
8613 EOF_Reached = 0;
8614 last_command_exit_value = (executing_builtin && parse_and_execute_level) ? EX_BADSYNTAX : EX_BADUSAGE;
8615 set_pipestatus_from_exit (last_command_exit_value);
8616 return;
8617 }
8618
8619 /* If the line of input we're reading is not null, try to find the
8620 objectionable token. First, try to figure out what token the
8621 parser's complaining about by looking at current_token. */
8622 if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
8623 {
8624 if (ansic_shouldquote (msg))
8625 {
8626 p = ansic_quote (msg, 0, NULL);
8627 free (msg);
8628 msg = p;
8629 }
8630 parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
8631 free (msg);
8632
8633 if (interactive == 0)
8634 print_offending_line ();
8635
8636 last_command_exit_value = (executing_builtin && parse_and_execute_level) ? EX_BADSYNTAX : EX_BADUSAGE;
8637 set_pipestatus_from_exit (last_command_exit_value);
8638 return;
8639 }
8640
8641 /* If looking at the current token doesn't prove fruitful, try to find the
8642 offending token by analyzing the text of the input line near the current
8643 input line index and report what we find. */
8644 if (shell_input_line && *shell_input_line)
8645 {
8646 msg = error_token_from_text ();
8647 if (msg)
8648 {
8649 parser_error (line_number, _("syntax error near `%s'"), msg);
8650 free (msg);
8651 }
8652
8653 /* If not interactive, print the line containing the error. */
8654 if (interactive == 0)
8655 print_offending_line ();
8656 }
8657 else
8658 {
8659 msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error");
8660 parser_error (line_number, "%s", msg);
8661 /* When the shell is interactive, this file uses EOF_Reached
8662 only for error reporting. Other mechanisms are used to
8663 decide whether or not to exit. */
8664 if (interactive && EOF_Reached)
8665 EOF_Reached = 0;
8666 }
8667
8668 last_command_exit_value = (executing_builtin && parse_and_execute_level) ? EX_BADSYNTAX : EX_BADUSAGE;
8669 set_pipestatus_from_exit (last_command_exit_value);
8670 }
8671
8672 /* ??? Needed function. ??? We have to be able to discard the constructs
8673 created during parsing. In the case of error, we want to return
8674 allocated objects to the memory pool. In the case of no error, we want
8675 to throw away the information about where the allocated objects live.
8676 (dispose_command () will actually free the command.) */
8677 static void
8678 discard_parser_constructs (error_p)
8679 int error_p;
8680 {
8681 }
8682
8683 /************************************************
8684 * *
8685 * EOF HANDLING *
8686 * *
8687 ************************************************/
8688
8689 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
8690
8691 /* A flag denoting whether or not ignoreeof is set. */
8692 int ignoreeof = 0;
8693
8694 /* The number of times that we have encountered an EOF character without
8695 another character intervening. When this gets above the limit, the
8696 shell terminates. */
8697 int eof_encountered = 0;
8698
8699 /* The limit for eof_encountered. */
8700 int eof_encountered_limit = 10;
8701
8702 /* If we have EOF as the only input unit, this user wants to leave
8703 the shell. If the shell is not interactive, then just leave.
8704 Otherwise, if ignoreeof is set, and we haven't done this the
8705 required number of times in a row, print a message. */
8706 static void
8707 handle_eof_input_unit ()
8708 {
8709 if (interactive)
8710 {
8711 /* shell.c may use this to decide whether or not to write out the
8712 history, among other things. We use it only for error reporting
8713 in this file. */
8714 if (EOF_Reached)
8715 EOF_Reached = 0;
8716
8717 /* If the user wants to "ignore" eof, then let her do so, kind of. */
8718 if (ignoreeof)
8719 {
8720 if (eof_encountered < eof_encountered_limit)
8721 {
8722 fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
8723 login_shell ? "logout" : "exit");
8724 eof_encountered++;
8725 /* Reset the parsing state. */
8726 last_read_token = current_token = '\n';
8727 /* Reset the prompt string to be $PS1. */
8728 prompt_string_pointer = (char **)NULL;
8729 prompt_again ();
8730 return;
8731 }
8732 }
8733
8734 /* In this case EOF should exit the shell. Do it now. */
8735 reset_parser ();
8736
8737 last_shell_builtin = this_shell_builtin;
8738 this_shell_builtin = exit_builtin;
8739 exit_builtin ((WORD_LIST *)NULL);
8740 }
8741 else
8742 {
8743 /* We don't write history files, etc., for non-interactive shells. */
8744 EOF_Reached = 1;
8745 }
8746 }
8747
8748 /************************************************
8749 * *
8750 * STRING PARSING FUNCTIONS *
8751 * *
8752 ************************************************/
8753
8754 /* It's very important that these two functions treat the characters
8755 between ( and ) identically. */
8756
8757 static WORD_LIST parse_string_error;
8758
8759 /* Take a string and run it through the shell parser, returning the
8760 resultant word list. Used by compound array assignment. */
8761 WORD_LIST *
8762 parse_string_to_word_list (s, flags, whom)
8763 char *s;
8764 int flags;
8765 const char *whom;
8766 {
8767 WORD_LIST *wl;
8768 int tok, orig_current_token, orig_line_number, orig_input_terminator;
8769 int orig_line_count;
8770 int old_echo_input, old_expand_aliases, ea;
8771 #if defined (HISTORY)
8772 int old_remember_on_history, old_history_expansion_inhibited;
8773 #endif
8774
8775 #if defined (HISTORY)
8776 old_remember_on_history = remember_on_history;
8777 # if defined (BANG_HISTORY)
8778 old_history_expansion_inhibited = history_expansion_inhibited;
8779 # endif
8780 bash_history_disable ();
8781 #endif
8782
8783 orig_line_number = line_number;
8784 orig_line_count = current_command_line_count;
8785 orig_input_terminator = shell_input_line_terminator;
8786 old_echo_input = echo_input_at_read;
8787 old_expand_aliases = expand_aliases;
8788
8789 push_stream (1);
8790 #if 0 /* TAG: bash-5.2 Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> 11/17/2020 */
8791 if (ea = expanding_alias ())
8792 parser_save_alias ();
8793 #endif
8794 last_read_token = WORD; /* WORD to allow reserved words here */
8795 current_command_line_count = 0;
8796 echo_input_at_read = expand_aliases = 0;
8797
8798 with_input_from_string (s, whom);
8799 wl = (WORD_LIST *)NULL;
8800
8801 if (flags & 1)
8802 parser_state |= PST_COMPASSIGN|PST_REPARSE;
8803
8804 while ((tok = read_token (READ)) != yacc_EOF)
8805 {
8806 if (tok == '\n' && *bash_input.location.string == '\0')
8807 break;
8808 if (tok == '\n') /* Allow newlines in compound assignments */
8809 continue;
8810 if (tok != WORD && tok != ASSIGNMENT_WORD)
8811 {
8812 line_number = orig_line_number + line_number - 1;
8813 orig_current_token = current_token;
8814 current_token = tok;
8815 yyerror (NULL); /* does the right thing */
8816 current_token = orig_current_token;
8817 if (wl)
8818 dispose_words (wl);
8819 wl = &parse_string_error;
8820 break;
8821 }
8822 wl = make_word_list (yylval.word, wl);
8823 }
8824
8825 last_read_token = '\n';
8826 pop_stream ();
8827
8828 #if 0 /* TAG: bash-5.2 */
8829 if (ea)
8830 parser_restore_alias ();
8831 #endif
8832
8833 #if defined (HISTORY)
8834 remember_on_history = old_remember_on_history;
8835 # if defined (BANG_HISTORY)
8836 history_expansion_inhibited = old_history_expansion_inhibited;
8837 # endif /* BANG_HISTORY */
8838 #endif /* HISTORY */
8839
8840 echo_input_at_read = old_echo_input;
8841 expand_aliases = old_expand_aliases;
8842
8843 current_command_line_count = orig_line_count;
8844 shell_input_line_terminator = orig_input_terminator;
8845
8846 if (flags & 1)
8847 parser_state &= ~(PST_COMPASSIGN|PST_REPARSE);
8848
8849 if (wl == &parse_string_error)
8850 {
8851 set_exit_status (EXECUTION_FAILURE);
8852 if (interactive_shell == 0 && posixly_correct)
8853 jump_to_top_level (FORCE_EOF);
8854 else
8855 jump_to_top_level (DISCARD);
8856 }
8857
8858 return (REVERSE_LIST (wl, WORD_LIST *));
8859 }
8860
8861 static char *
8862 parse_compound_assignment (retlenp)
8863 int *retlenp;
8864 {
8865 WORD_LIST *wl, *rl;
8866 int tok, orig_line_number, orig_token_size, orig_last_token, assignok;
8867 char *saved_token, *ret;
8868
8869 saved_token = token;
8870 orig_token_size = token_buffer_size;
8871 orig_line_number = line_number;
8872 orig_last_token = last_read_token;
8873
8874 last_read_token = WORD; /* WORD to allow reserved words here */
8875
8876 token = (char *)NULL;
8877 token_buffer_size = 0;
8878
8879 assignok = parser_state&PST_ASSIGNOK; /* XXX */
8880
8881 wl = (WORD_LIST *)NULL; /* ( */
8882 parser_state |= PST_COMPASSIGN;
8883
8884 while ((tok = read_token (READ)) != ')')
8885 {
8886 if (tok == '\n') /* Allow newlines in compound assignments */
8887 {
8888 if (SHOULD_PROMPT ())
8889 prompt_again ();
8890 continue;
8891 }
8892 if (tok != WORD && tok != ASSIGNMENT_WORD)
8893 {
8894 current_token = tok; /* for error reporting */
8895 if (tok == yacc_EOF) /* ( */
8896 parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'"));
8897 else
8898 yyerror(NULL); /* does the right thing */
8899 if (wl)
8900 dispose_words (wl);
8901 wl = &parse_string_error;
8902 break;
8903 }
8904 wl = make_word_list (yylval.word, wl);
8905 }
8906
8907 FREE (token);
8908 token = saved_token;
8909 token_buffer_size = orig_token_size;
8910
8911 parser_state &= ~PST_COMPASSIGN;
8912
8913 if (wl == &parse_string_error)
8914 {
8915 set_exit_status (EXECUTION_FAILURE);
8916 last_read_token = '\n'; /* XXX */
8917 if (interactive_shell == 0 && posixly_correct)
8918 jump_to_top_level (FORCE_EOF);
8919 else
8920 jump_to_top_level (DISCARD);
8921 }
8922
8923 last_read_token = orig_last_token; /* XXX - was WORD? */
8924
8925 if (wl)
8926 {
8927 rl = REVERSE_LIST (wl, WORD_LIST *);
8928 ret = string_list (rl);
8929 dispose_words (rl);
8930 }
8931 else
8932 ret = (char *)NULL;
8933
8934 if (retlenp)
8935 *retlenp = (ret && *ret) ? strlen (ret) : 0;
8936
8937 if (assignok)
8938 parser_state |= PST_ASSIGNOK;
8939
8940 return ret;
8941 }
8942
8943 /************************************************
8944 * *
8945 * SAVING AND RESTORING PARTIAL PARSE STATE *
8946 * *
8947 ************************************************/
8948
8949 sh_parser_state_t *
8950 save_parser_state (ps)
8951 sh_parser_state_t *ps;
8952 {
8953 if (ps == 0)
8954 ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t));
8955 if (ps == 0)
8956 return ((sh_parser_state_t *)NULL);
8957
8958 ps->parser_state = parser_state;
8959 ps->token_state = save_token_state ();
8960
8961 ps->input_line_terminator = shell_input_line_terminator;
8962 ps->eof_encountered = eof_encountered;
8963
8964 ps->prompt_string_pointer = prompt_string_pointer;
8965
8966 ps->current_command_line_count = current_command_line_count;
8967
8968 #if defined (HISTORY)
8969 ps->remember_on_history = remember_on_history;
8970 # if defined (BANG_HISTORY)
8971 ps->history_expansion_inhibited = history_expansion_inhibited;
8972 # endif
8973 #endif
8974
8975 ps->last_command_exit_value = last_command_exit_value;
8976 #if defined (ARRAY_VARS)
8977 ps->pipestatus = save_pipestatus_array ();
8978 #endif
8979
8980 ps->last_shell_builtin = last_shell_builtin;
8981 ps->this_shell_builtin = this_shell_builtin;
8982
8983 ps->expand_aliases = expand_aliases;
8984 ps->echo_input_at_read = echo_input_at_read;
8985 ps->need_here_doc = need_here_doc;
8986 ps->here_doc_first_line = here_doc_first_line;
8987
8988 if (need_here_doc == 0)
8989 ps->redir_stack[0] = 0;
8990 else
8991 memcpy (ps->redir_stack, redir_stack, sizeof (redir_stack[0]) * HEREDOC_MAX);
8992
8993 ps->token = token;
8994 ps->token_buffer_size = token_buffer_size;
8995 /* Force reallocation on next call to read_token_word */
8996 token = 0;
8997 token_buffer_size = 0;
8998
8999 return (ps);
9000 }
9001
9002 void
9003 restore_parser_state (ps)
9004 sh_parser_state_t *ps;
9005 {
9006 int i;
9007
9008 if (ps == 0)
9009 return;
9010
9011 parser_state = ps->parser_state;
9012 if (ps->token_state)
9013 {
9014 restore_token_state (ps->token_state);
9015 free (ps->token_state);
9016 }
9017
9018 shell_input_line_terminator = ps->input_line_terminator;
9019 eof_encountered = ps->eof_encountered;
9020
9021 prompt_string_pointer = ps->prompt_string_pointer;
9022
9023 current_command_line_count = ps->current_command_line_count;
9024
9025 #if defined (HISTORY)
9026 remember_on_history = ps->remember_on_history;
9027 # if defined (BANG_HISTORY)
9028 history_expansion_inhibited = ps->history_expansion_inhibited;
9029 # endif
9030 #endif
9031
9032 last_command_exit_value = ps->last_command_exit_value;
9033 #if defined (ARRAY_VARS)
9034 restore_pipestatus_array (ps->pipestatus);
9035 #endif
9036
9037 last_shell_builtin = ps->last_shell_builtin;
9038 this_shell_builtin = ps->this_shell_builtin;
9039
9040 expand_aliases = ps->expand_aliases;
9041 echo_input_at_read = ps->echo_input_at_read;
9042 need_here_doc = ps->need_here_doc;
9043 here_doc_first_line = ps->here_doc_first_line;
9044
9045 #if 0
9046 for (i = 0; i < HEREDOC_MAX; i++)
9047 redir_stack[i] = ps->redir_stack[i];
9048 #else
9049 if (need_here_doc == 0)
9050 redir_stack[0] = 0;
9051 else
9052 memcpy (redir_stack, ps->redir_stack, sizeof (redir_stack[0]) * HEREDOC_MAX);
9053 #endif
9054
9055 FREE (token);
9056 token = ps->token;
9057 token_buffer_size = ps->token_buffer_size;
9058 }
9059
9060 sh_input_line_state_t *
9061 save_input_line_state (ls)
9062 sh_input_line_state_t *ls;
9063 {
9064 if (ls == 0)
9065 ls = (sh_input_line_state_t *)xmalloc (sizeof (sh_input_line_state_t));
9066 if (ls == 0)
9067 return ((sh_input_line_state_t *)NULL);
9068
9069 ls->input_line = shell_input_line;
9070 ls->input_line_size = shell_input_line_size;
9071 ls->input_line_len = shell_input_line_len;
9072 ls->input_line_index = shell_input_line_index;
9073
9074 #if defined (HANDLE_MULTIBYTE)
9075 ls->input_property = shell_input_line_property;
9076 ls->input_propsize = shell_input_line_propsize;
9077 #endif
9078
9079 /* force reallocation */
9080 shell_input_line = 0;
9081 shell_input_line_size = shell_input_line_len = shell_input_line_index = 0;
9082
9083 #if defined (HANDLE_MULTIBYTE)
9084 shell_input_line_property = 0;
9085 shell_input_line_propsize = 0;
9086 #endif
9087
9088 return ls;
9089 }
9090
9091 void
9092 restore_input_line_state (ls)
9093 sh_input_line_state_t *ls;
9094 {
9095 FREE (shell_input_line);
9096 shell_input_line = ls->input_line;
9097 shell_input_line_size = ls->input_line_size;
9098 shell_input_line_len = ls->input_line_len;
9099 shell_input_line_index = ls->input_line_index;
9100
9101 #if defined (HANDLE_MULTIBYTE)
9102 FREE (shell_input_line_property);
9103 shell_input_line_property = ls->input_property;
9104 shell_input_line_propsize = ls->input_propsize;
9105 #endif
9106
9107 #if 0
9108 set_line_mbstate ();
9109 #endif
9110 }
9111
9112 /************************************************
9113 * *
9114 * MULTIBYTE CHARACTER HANDLING *
9115 * *
9116 ************************************************/
9117
9118 #if defined (HANDLE_MULTIBYTE)
9119
9120 /* We don't let the property buffer get larger than this unless the line is */
9121 #define MAX_PROPSIZE 32768
9122
9123 static void
9124 set_line_mbstate ()
9125 {
9126 int c;
9127 size_t i, previ, len;
9128 mbstate_t mbs, prevs;
9129 size_t mbclen;
9130 int ilen;
9131
9132 if (shell_input_line == NULL)
9133 return;
9134 len = STRLEN (shell_input_line); /* XXX - shell_input_line_len ? */
9135 if (len == 0)
9136 return;
9137 if (shell_input_line_propsize >= MAX_PROPSIZE && len < MAX_PROPSIZE>>1)
9138 {
9139 free (shell_input_line_property);
9140 shell_input_line_property = 0;
9141 shell_input_line_propsize = 0;
9142 }
9143 if (len+1 > shell_input_line_propsize)
9144 {
9145 shell_input_line_propsize = len + 1;
9146 shell_input_line_property = (char *)xrealloc (shell_input_line_property, shell_input_line_propsize);
9147 }
9148
9149 if (locale_mb_cur_max == 1)
9150 {
9151 memset (shell_input_line_property, 1, len);
9152 return;
9153 }
9154
9155 /* XXX - use whether or not we are in a UTF-8 locale to avoid calls to
9156 mbrlen */
9157 if (locale_utf8locale == 0)
9158 memset (&prevs, '\0', sizeof (mbstate_t));
9159
9160 for (i = previ = 0; i < len; i++)
9161 {
9162 if (locale_utf8locale == 0)
9163 mbs = prevs;
9164
9165 c = shell_input_line[i];
9166 if (c == EOF)
9167 {
9168 size_t j;
9169 for (j = i; j < len; j++)
9170 shell_input_line_property[j] = 1;
9171 break;
9172 }
9173
9174 if (locale_utf8locale)
9175 {
9176 if ((unsigned char)shell_input_line[previ] < 128) /* i != previ */
9177 mbclen = 1;
9178 else
9179 {
9180 ilen = utf8_mblen (shell_input_line + previ, i - previ + 1);
9181 mbclen = (ilen == -1) ? (size_t)-1
9182 : ((ilen == -2) ? (size_t)-2 : (size_t)ilen);
9183 }
9184 }
9185 else
9186 mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs);
9187
9188 if (mbclen == 1 || mbclen == (size_t)-1)
9189 {
9190 mbclen = 1;
9191 previ = i + 1;
9192 }
9193 else if (mbclen == (size_t)-2)
9194 mbclen = 0;
9195 else if (mbclen > 1)
9196 {
9197 mbclen = 0;
9198 previ = i + 1;
9199 if (locale_utf8locale == 0)
9200 prevs = mbs;
9201 }
9202 else
9203 {
9204 size_t j;
9205 for (j = i; j < len; j++)
9206 shell_input_line_property[j] = 1;
9207 break;
9208 }
9209
9210 shell_input_line_property[i] = mbclen;
9211 }
9212 }
9213 #endif /* HANDLE_MULTIBYTE */