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