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