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