Imported from ../bash-4.0-rc1.tar.gz.
[platform/upstream/bash.git] / y.tab.c
1 /* A Bison parser, made by GNU Bison 2.3.  */
2
3 /* Skeleton implementation for Bison's Yacc-like parsers in C
4
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.  */
167 #line 21 "/Users/chet/src/bash/src/parse.y"
168
169 #include "config.h"
170
171 #include "bashtypes.h"
172 #include "bashansi.h"
173
174 #include "filecntl.h"
175
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>
185 #include "chartypes.h"
186 #include <signal.h>
187
188 #include "memalloc.h"
189
190 #include "bashintl.h"
191
192 #define NEED_STRFTIME_DECL      /* used in externs.h */
193
194 #include "shell.h"
195 #include "trap.h"
196 #include "flags.h"
197 #include "parser.h"
198 #include "mailcheck.h"
199 #include "test.h"
200 #include "builtins.h"
201 #include "builtins/common.h"
202 #include "builtins/builtext.h"
203
204 #include "shmbutil.h"
205
206 #if defined (READLINE)
207 #  include "bashline.h"
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"
222 #else
223 typedef void *alias_t;
224 #endif /* ALIAS */
225
226 #if defined (PROMPT_STRING_DECODE)
227 #  ifndef _MINIX
228 #    include <sys/param.h>
229 #  endif
230 #  include <time.h>
231 #  if defined (TM_IN_SYS_TIME)
232 #    include <sys/types.h>
233 #    include <sys/time.h>
234 #  endif /* TM_IN_SYS_TIME */
235 #  include "maxpath.h"
236 #endif /* PROMPT_STRING_DECODE */
237
238 #define RE_READ_TOKEN   -99
239 #define NO_EXPANSION    -100
240
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
257
258 #if defined (EXTENDED_GLOB)
259 extern int extended_glob;
260 #endif
261
262 extern int eof_encountered;
263 extern int no_line_editing, running_under_emacs;
264 extern int current_command_number;
265 extern int sourcelevel, parse_and_execute_level;
266 extern int posixly_correct;
267 extern int last_command_exit_value;
268 extern char *shell_name, *current_host_name;
269 extern char *dist_version;
270 extern int patch_level;
271 extern int dump_translatable_strings, dump_po_strings;
272 extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
273 #if defined (BUFFERED_INPUT)
274 extern int bash_input_fd_changed;
275 #endif
276
277 extern int errno;
278 /* **************************************************************** */
279 /*                                                                  */
280 /*                  "Forward" declarations                          */
281 /*                                                                  */
282 /* **************************************************************** */
283
284 #ifdef DEBUG
285 static void debug_parser __P((int));
286 #endif
287
288 static int yy_getc __P((void));
289 static int yy_ungetc __P((int));
290
291 #if defined (READLINE)
292 static int yy_readline_get __P((void));
293 static int yy_readline_unget __P((int));
294 #endif
295
296 static int yy_string_get __P((void));
297 static int yy_string_unget __P((int));
298 static void rewind_input_string __P((void));
299 static int yy_stream_get __P((void));
300 static int yy_stream_unget __P((int));
301
302 static int shell_getc __P((int));
303 static void shell_ungetc __P((int));
304 static void discard_until __P((int));
305
306 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
307 static void push_string __P((char *, int, alias_t *));
308 static void pop_string __P((void));
309 static void free_string_list __P((void));
310 #endif
311
312 static char *read_a_line __P((int));
313
314 static int reserved_word_acceptable __P((int));
315 static int yylex __P((void));
316 static int alias_expand_token __P((char *));
317 static int time_command_acceptable __P((void));
318 static int special_case_tokens __P((char *));
319 static int read_token __P((int));
320 static char *parse_matched_pair __P((int, int, int, int *, int));
321 static char *parse_comsub __P((int, int, int, int *, int));
322 #if defined (ARRAY_VARS)
323 static char *parse_compound_assignment __P((int *));
324 #endif
325 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
326 static int parse_dparen __P((int));
327 static int parse_arith_cmd __P((char **, int));
328 #endif
329 #if defined (COND_COMMAND)
330 static void cond_error __P((void));
331 static COND_COM *cond_expr __P((void));
332 static COND_COM *cond_or __P((void));
333 static COND_COM *cond_and __P((void));
334 static COND_COM *cond_term __P((void));
335 static int cond_skip_newlines __P((void));
336 static COMMAND *parse_cond_command __P((void));
337 #endif
338 #if defined (ARRAY_VARS)
339 static int token_is_assignment __P((char *, int));
340 static int token_is_ident __P((char *, int));
341 #endif
342 static int read_token_word __P((int));
343 static void discard_parser_constructs __P((int));
344
345 static char *error_token_from_token __P((int));
346 static char *error_token_from_text __P((void));
347 static void print_offending_line __P((void));
348 static void report_syntax_error __P((char *));
349
350 static void handle_eof_input_unit __P((void));
351 static void prompt_again __P((void));
352 #if 0
353 static void reset_readline_prompt __P((void));
354 #endif
355 static void print_prompt __P((void));
356
357 #if defined (HANDLE_MULTIBYTE)
358 static void set_line_mbstate __P((void));
359 static char *shell_input_line_property = NULL;
360 #else
361 #  define set_line_mbstate()
362 #endif
363
364 extern int yyerror __P((const char *));
365
366 #ifdef DEBUG
367 extern int yydebug;
368 #endif
369
370 /* Default prompt strings */
371 char *primary_prompt = PPROMPT;
372 char *secondary_prompt = SPROMPT;
373
374 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
375 char *ps1_prompt, *ps2_prompt;
376
377 /* Handle on the current prompt string.  Indirectly points through
378    ps1_ or ps2_prompt. */
379 char **prompt_string_pointer = (char **)NULL;
380 char *current_prompt_string;
381
382 /* Non-zero means we expand aliases in commands. */
383 int 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. */
389 int promptvars = 1;
390
391 /* If non-zero, $'...' and $"..." are expanded when they appear within
392    a ${...} expansion, even when the expansion appears within double
393    quotes. */
394 int extended_quote = 1;
395
396 /* The decoded prompt string.  Used if READLINE is not defined or if
397    editing is turned off.  Analogous to current_readline_prompt. */
398 static char *current_decoded_prompt;
399
400 /* The number of lines read from input while creating the current command. */
401 int current_command_line_count;
402
403 /* The token that currently denotes the end of parse. */
404 int shell_eof_token;
405
406 /* The token currently being read. */
407 int current_token;
408
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. */
411 static REDIRECT *redir_stack[10];
412 int need_here_doc;
413
414 /* Where shell input comes from.  History expansion is performed on each
415    line when the shell is interactive. */
416 static char *shell_input_line = (char *)NULL;
417 static int shell_input_line_index;
418 static int shell_input_line_size;       /* Amount allocated for shell_input_line. */
419 static int shell_input_line_len;        /* strlen (shell_input_line) */
420
421 /* Either zero or EOF. */
422 static int shell_input_line_terminator;
423
424 /* The line number in a script on which a function definition starts. */
425 static int function_dstart;
426
427 /* The line number in a script on which a function body starts. */
428 static int function_bstart;
429
430 /* The line number in a script at which an arithmetic for command starts. */
431 static int arith_for_lineno;
432
433 /* The current parser state. */
434 static int parser_state;
435
436 /* The last read token, or NULL.  read_token () uses this for context
437    checking. */
438 static int last_read_token;
439
440 /* The token read prior to last_read_token. */
441 static int token_before_that;
442
443 /* The token read prior to token_before_that. */
444 static int two_tokens_ago;
445
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
450 static int word_lineno[MAX_CASE_NEST];
451 static 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. */
457 static int token_to_read;
458 static WORD_DESC *word_desc_to_read;
459
460 static REDIRECTEE redir;
461
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
482 typedef union YYSTYPE
483 #line 316 "/Users/chet/src/bash/src/parse.y"
484 {
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;
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"
508
509 #ifdef short
510 # undef short
511 #endif
512
513 #ifdef YYTYPE_UINT8
514 typedef YYTYPE_UINT8 yytype_uint8;
515 #else
516 typedef unsigned char yytype_uint8;
517 #endif
518
519 #ifdef YYTYPE_INT8
520 typedef YYTYPE_INT8 yytype_int8;
521 #elif (defined __STDC__ || defined __C99__FUNC__ \
522      || defined __cplusplus || defined _MSC_VER)
523 typedef signed char yytype_int8;
524 #else
525 typedef short int yytype_int8;
526 #endif
527
528 #ifdef YYTYPE_UINT16
529 typedef YYTYPE_UINT16 yytype_uint16;
530 #else
531 typedef unsigned short int yytype_uint16;
532 #endif
533
534 #ifdef YYTYPE_INT16
535 typedef YYTYPE_INT16 yytype_int16;
536 #else
537 typedef short int yytype_int16;
538 #endif
539
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
553
554 #define YYSIZE_MAXIMUM ((YYSIZE_T) -1)
555
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
566 #endif
567
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 */
573 #endif
574
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)
581 static int
582 YYID (int i)
583 #else
584 static int
585 YYID (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)
649 void *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)
656 void 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.  */
668 union 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))
716
717 #endif
718
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.  */
741 static 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
774 };
775
776 #if YYDEBUG
777 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
778    YYRHS.  */
779 static 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
797 };
798
799 /* YYRHS -- A `-1'-separated list of the rules' RHS.  */
800 static 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
865 };
866
867 /* YYRLINE[YYN] -- source line where rule number YYN was defined.  */
868 static 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
886 };
887 #endif
888
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.  */
892 static 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
913 };
914 #endif
915
916 # ifdef YYPRINT
917 /* YYTOKNUM[YYLEX-NUM] -- Internal token number corresponding to
918    token YYLEX-NUM.  */
919 static 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.  */
931 static 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
949 };
950
951 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN.  */
952 static 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 };
971
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.  */
975 static 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
1009 };
1010
1011 /* YYDEFGOTO[NTERM-NUM].  */
1012 static 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
1018 };
1019
1020 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
1021    STATE-NUM.  */
1022 #define YYPACT_NINF -188
1023 static 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 };
1058
1059 /* YYPGOTO[NTERM-NUM].  */
1060 static 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 };
1067
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
1073 static 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 };
1149
1150 static 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 };
1226
1227 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
1228    symbol of state STATE-NUM.  */
1229 static 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 };
1264
1265 #define yyerrok         (yyerrstatus = 0)
1266 #define yyclearin       (yychar = YYEMPTY)
1267 #define YYEMPTY         (-2)
1268 #define YYEOF           0
1269
1270 #define YYACCEPT        goto yyacceptlab
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.
1277    Once GCC version 2 has supplanted version 1, this can go.  */
1278
1279 #define YYFAIL          goto yyerrlab
1280
1281 #define YYRECOVERING()  (!!yyerrstatus)
1282
1283 #define YYBACKUP(Token, Value)                                  \
1284 do                                                              \
1285   if (yychar == YYEMPTY && yylen == 1)                          \
1286     {                                                           \
1287       yychar = (Token);                                         \
1288       yylval = (Value);                                         \
1289       yytoken = YYTRANSLATE (yychar);                           \
1290       YYPOPSTACK (1);                                           \
1291       goto yybackup;                                            \
1292     }                                                           \
1293   else                                                          \
1294     {                                                           \
1295       yyerror (YY_("syntax error: cannot back up")); \
1296       YYERROR;                                                  \
1297     }                                                           \
1298 while (YYID (0))
1299
1300
1301 #define YYTERROR        1
1302 #define YYERRCODE       256
1303
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))
1328 #endif
1329
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
1344 #endif
1345
1346
1347 /* YYLEX -- calling `yylex' with the right arguments.  */
1348
1349 #ifdef YYLEX_PARAM
1350 # define YYLEX yylex (YYLEX_PARAM)
1351 #else
1352 # define YYLEX yylex ()
1353 #endif
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)                        \
1364 do {                                            \
1365   if (yydebug)                                  \
1366     YYFPRINTF Args;                             \
1367 } while (YYID (0))
1368
1369 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)                    \
1370 do {                                                                      \
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)
1388 static void
1389 yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
1390 #else
1391 static void
1392 yy_symbol_value_print (yyoutput, yytype, yyvaluep)
1393     FILE *yyoutput;
1394     int yytype;
1395     YYSTYPE const * const yyvaluep;
1396 #endif
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 `--------------------------------*/
1417
1418 #if (defined __STDC__ || defined __C99__FUNC__ \
1419      || defined __cplusplus || defined _MSC_VER)
1420 static void
1421 yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
1422 #else
1423 static void
1424 yy_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]);
1434
1435   yy_symbol_value_print (yyoutput, yytype, yyvaluep);
1436   YYFPRINTF (yyoutput, ")");
1437 }
1438
1439 /*------------------------------------------------------------------.
1440 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
1441 | TOP (included).                                                   |
1442 `------------------------------------------------------------------*/
1443
1444 #if (defined __STDC__ || defined __C99__FUNC__ \
1445      || defined __cplusplus || defined _MSC_VER)
1446 static void
1447 yy_stack_print (yytype_int16 *bottom, yytype_int16 *top)
1448 #else
1449 static void
1450 yy_stack_print (bottom, top)
1451     yytype_int16 *bottom;
1452     yytype_int16 *top;
1453 #endif
1454 {
1455   YYFPRINTF (stderr, "Stack now");
1456   for (; bottom <= top; ++bottom)
1457     YYFPRINTF (stderr, " %d", *bottom);
1458   YYFPRINTF (stderr, "\n");
1459 }
1460
1461 # define YY_STACK_PRINT(Bottom, Top)                            \
1462 do {                                                            \
1463   if (yydebug)                                                  \
1464     yy_stack_print ((Bottom), (Top));                           \
1465 } while (YYID (0))
1466
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)
1474 static void
1475 yy_reduce_print (YYSTYPE *yyvsp, int yyrule)
1476 #else
1477 static void
1478 yy_reduce_print (yyvsp, yyrule)
1479     YYSTYPE *yyvsp;
1480     int yyrule;
1481 #endif
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)          \
1500 do {                                    \
1501   if (yydebug)                          \
1502     yy_reduce_print (yyvsp, Rule); \
1503 } while (YYID (0))
1504
1505 /* Nonzero means print parse trace.  It is left uninitialized so that
1506    multiple parsers can coexist.  */
1507 int 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 */
1514
1515
1516 /* YYINITDEPTH -- initial size of the parser's stacks.  */
1517 #ifndef YYINITDEPTH
1518 # define YYINITDEPTH 200
1519 #endif
1520
1521 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
1522    if the built-in stack extension method is used).
1523
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.  */
1527
1528 #ifndef YYMAXDEPTH
1529 # define YYMAXDEPTH 10000
1530 #endif
1531
1532 \f
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)
1543 static YYSIZE_T
1544 yystrlen (const char *yystr)
1545 #else
1546 static YYSIZE_T
1547 yystrlen (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)
1567 static char *
1568 yystpcpy (char *yydest, const char *yysrc)
1569 #else
1570 static char *
1571 yystpcpy (yydest, yysrc)
1572     char *yydest;
1573     const char *yysrc;
1574 #endif
1575 {
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.  */
1595 static YYSIZE_T
1596 yytnamerr (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     }
1627
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.  */
1642 static YYSIZE_T
1643 yysyntax_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     }
1741 }
1742 #endif /* YYERROR_VERBOSE */
1743 \f
1744
1745 /*-----------------------------------------------.
1746 | Release the memory associated to this symbol.  |
1747 `-----------------------------------------------*/
1748
1749 /*ARGSUSED*/
1750 #if (defined __STDC__ || defined __C99__FUNC__ \
1751      || defined __cplusplus || defined _MSC_VER)
1752 static void
1753 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep)
1754 #else
1755 static void
1756 yydestruct (yymsg, yytype, yyvaluep)
1757     const char *yymsg;
1758     int yytype;
1759     YYSTYPE *yyvaluep;
1760 #endif
1761 {
1762   YYUSE (yyvaluep);
1763
1764   if (!yymsg)
1765     yymsg = "Deleting";
1766   YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
1767
1768   switch (yytype)
1769     {
1770
1771       default:
1772         break;
1773     }
1774 }
1775 \f
1776
1777 /* Prevent warnings from -Wmissing-prototypes.  */
1778
1779 #ifdef YYPARSE_PARAM
1780 #if defined __STDC__ || defined __cplusplus
1781 int yyparse (void *YYPARSE_PARAM);
1782 #else
1783 int yyparse ();
1784 #endif
1785 #else /* ! YYPARSE_PARAM */
1786 #if defined __STDC__ || defined __cplusplus
1787 int yyparse (void);
1788 #else
1789 int yyparse ();
1790 #endif
1791 #endif /* ! YYPARSE_PARAM */
1792
1793
1794
1795 /* The look-ahead symbol.  */
1796 int yychar;
1797
1798 /* The semantic value of the look-ahead symbol.  */
1799 YYSTYPE yylval;
1800
1801 /* Number of syntax errors so far.  */
1802 int yynerrs;
1803
1804
1805
1806 /*----------.
1807 | yyparse.  |
1808 `----------*/
1809
1810 #ifdef YYPARSE_PARAM
1811 #if (defined __STDC__ || defined __C99__FUNC__ \
1812      || defined __cplusplus || defined _MSC_VER)
1813 int
1814 yyparse (void *YYPARSE_PARAM)
1815 #else
1816 int
1817 yyparse (YYPARSE_PARAM)
1818     void *YYPARSE_PARAM;
1819 #endif
1820 #else /* ! YYPARSE_PARAM */
1821 #if (defined __STDC__ || defined __C99__FUNC__ \
1822      || defined __cplusplus || defined _MSC_VER)
1823 int
1824 yyparse (void)
1825 #else
1826 int
1827 yyparse ()
1828
1829 #endif
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;
1845 #endif
1846
1847   /* Three stacks and their tools:
1848      `yyss': related to states,
1849      `yyvs': related to semantic values,
1850      `yyls': related to locations.
1851
1852      Refer to the stacks thru separate pointers, to allow yyoverflow
1853      to reallocate them elsewhere.  */
1854
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"));
1881
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
1892   yyssp = yyss;
1893   yyvsp = yyvs;
1894
1895   goto yysetstate;
1896
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++;
1904
1905  yysetstate:
1906   *yyssp = yystate;
1907
1908   if (yyss + yystacksize - 1 <= yyssp)
1909     {
1910       /* Get the current used size of the three stacks, in elements.  */
1911       YYSIZE_T yysize = yyssp - yyss + 1;
1912
1913 #ifdef yyoverflow
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;
1920
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       }
1935 #else /* no yyoverflow */
1936 # ifndef YYSTACK_RELOCATE
1937       goto yyexhaustedlab;
1938 # else
1939       /* Extend the stack our own way.  */
1940       if (YYMAXDEPTH <= yystacksize)
1941         goto yyexhaustedlab;
1942       yystacksize *= 2;
1943       if (YYMAXDEPTH < yystacksize)
1944         yystacksize = YYMAXDEPTH;
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
1960 #endif /* no yyoverflow */
1961
1962       yyssp = yyss + yysize - 1;
1963       yyvsp = yyvs + yysize - 1;
1964
1965
1966       YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1967                   (unsigned long int) yystacksize));
1968
1969       if (yyss + yystacksize - 1 <= yyssp)
1970         YYABORT;
1971     }
1972
1973   YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1974
1975   goto yybackup;
1976
1977 /*-----------.
1978 | yybackup.  |
1979 `-----------*/
1980 yybackup:
1981
1982   /* Do appropriate processing given the current state.  Read a
1983      look-ahead token if we need one and don't already have one.  */
1984
1985   /* First try to decide what to do without reference to look-ahead token.  */
1986   yyn = yypact[yystate];
1987   if (yyn == YYPACT_NINF)
1988     goto yydefault;
1989
1990   /* Not known => get a look-ahead token if don't already have one.  */
1991
1992   /* YYCHAR is either YYEMPTY or YYEOF or a valid look-ahead symbol.  */
1993   if (yychar == YYEMPTY)
1994     {
1995       YYDPRINTF ((stderr, "Reading a token: "));
1996       yychar = YYLEX;
1997     }
1998
1999   if (yychar <= YYEOF)
2000     {
2001       yychar = yytoken = YYEOF;
2002       YYDPRINTF ((stderr, "Now at end of input.\n"));
2003     }
2004   else
2005     {
2006       yytoken = YYTRANSLATE (yychar);
2007       YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
2008     }
2009
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)
2014     goto yydefault;
2015   yyn = yytable[yyn];
2016   if (yyn <= 0)
2017     {
2018       if (yyn == 0 || yyn == YYTABLE_NINF)
2019         goto yyerrlab;
2020       yyn = -yyn;
2021       goto yyreduce;
2022     }
2023
2024   if (yyn == YYFINAL)
2025     YYACCEPT;
2026
2027   /* Count tokens shifted since error; after three, turn off error
2028      status.  */
2029   if (yyerrstatus)
2030     yyerrstatus--;
2031
2032   /* Shift the look-ahead token.  */
2033   YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
2034
2035   /* Discard the shifted token unless it is eof.  */
2036   if (yychar != YYEOF)
2037     yychar = YYEMPTY;
2038
2039   yystate = yyn;
2040   *++yyvsp = yylval;
2041
2042   goto yynewstate;
2043
2044
2045 /*-----------------------------------------------------------.
2046 | yydefault -- do the default action for the current state.  |
2047 `-----------------------------------------------------------*/
2048 yydefault:
2049   yyn = yydefact[yystate];
2050   if (yyn == 0)
2051     goto yyerrlab;
2052   goto yyreduce;
2053
2054
2055 /*-----------------------------.
2056 | yyreduce -- Do a reduction.  |
2057 `-----------------------------*/
2058 yyreduce:
2059   /* yyn is the number of a rule to reduce with.  */
2060   yylen = yyr2[yyn];
2061
2062   /* If YYLEN is nonzero, implement the default value of the action:
2063      `$$ = $1'.
2064
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];
2071
2072
2073   YY_REDUCE_PRINT (yyn);
2074   switch (yyn)
2075     {
2076         case 2:
2077 #line 370 "/Users/chet/src/bash/src/parse.y"
2078     {
2079                           /* Case of regular command.  Discard the error
2080                              safety net,and return the command just parsed. */
2081                           global_command = (yyvsp[(1) - (2)].command);
2082                           eof_encountered = 0;
2083                           /* discard_parser_constructs (0); */
2084                           if (parser_state & PST_CMDSUBST)
2085                             parser_state |= PST_EOFTOKEN;
2086                           YYACCEPT;
2087                         }
2088     break;
2089
2090   case 3:
2091 #line 381 "/Users/chet/src/bash/src/parse.y"
2092     {
2093                           /* Case of regular command, but not a very
2094                              interesting one.  Return a NULL command. */
2095                           global_command = (COMMAND *)NULL;
2096                           if (parser_state & PST_CMDSUBST)
2097                             parser_state |= PST_EOFTOKEN;
2098                           YYACCEPT;
2099                         }
2100     break;
2101
2102   case 4:
2103 #line 390 "/Users/chet/src/bash/src/parse.y"
2104     {
2105                           /* Error during parsing.  Return NULL command. */
2106                           global_command = (COMMAND *)NULL;
2107                           eof_encountered = 0;
2108                           /* discard_parser_constructs (1); */
2109                           if (interactive && parse_and_execute_level == 0)
2110                             {
2111                               YYACCEPT;
2112                             }
2113                           else
2114                             {
2115                               YYABORT;
2116                             }
2117                         }
2118     break;
2119
2120   case 5:
2121 #line 405 "/Users/chet/src/bash/src/parse.y"
2122     {
2123                           /* Case of EOF seen by itself.  Do ignoreeof or
2124                              not. */
2125                           global_command = (COMMAND *)NULL;
2126                           handle_eof_input_unit ();
2127                           YYACCEPT;
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:
2166 #line 436 "/Users/chet/src/bash/src/parse.y"
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:
2174 #line 441 "/Users/chet/src/bash/src/parse.y"
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:
2182 #line 446 "/Users/chet/src/bash/src/parse.y"
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:
2190 #line 451 "/Users/chet/src/bash/src/parse.y"
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:
2256 #line 493 "/Users/chet/src/bash/src/parse.y"
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     {
2310                           redir.dest = 0;
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     {
2318                           redir.dest = 0;
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     {
2326                           redir.dest = 0;
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     {
2334                           redir.dest = 0;
2335                           (yyval.redirect) = make_redirection ((yyvsp[(1) - (3)].number), r_close_this, redir);
2336                         }
2337     break;
2338
2339   case 32:
2340 #line 547 "/Users/chet/src/bash/src/parse.y"
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     {
2412                           register REDIRECT *t;
2413
2414                           for (t = (yyvsp[(1) - (2)].redirect); t->next; t = t->next)
2415                             ;
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     {
2444                           COMMAND *tc;
2445
2446                           tc = (yyvsp[(1) - (2)].command);
2447                           if (tc->redirects)
2448                             {
2449                               register REDIRECT *t;
2450                               for (t = tc->redirects; t->next; t = t->next)
2451                                 ;
2452                               t->next = (yyvsp[(2) - (2)].redirect);
2453                             }
2454                           else
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]);
2529                           if (word_top > 0) word_top--;
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]);
2537                           if (word_top > 0) word_top--;
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]);
2545                           if (word_top > 0) word_top--;
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]);
2553                           if (word_top > 0) word_top--;
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]);
2561                           if (word_top > 0) word_top--;
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]);
2569                           if (word_top > 0) word_top--;
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]);
2577                           if (word_top > 0) word_top--;
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]);
2585                           if (word_top > 0) word_top--;
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);
2593                                   if (word_top > 0) word_top--;
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);
2601                                   if (word_top > 0) word_top--;
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);
2609                                   if (word_top > 0) word_top--;
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);
2617                                   if (word_top > 0) word_top--;
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]);
2625                           if (word_top > 0) word_top--;
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]);
2633                           if (word_top > 0) word_top--;
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]);
2641                           if (word_top > 0) word_top--;
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]);
2649                           if (word_top > 0) word_top--;
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]);
2657                           if (word_top > 0) word_top--;
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]);
2665                           if (word_top > 0) word_top--;
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]);
2673                           if (word_top > 0) word_top--;
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]);
2681                           if (word_top > 0) word_top--;
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]);
2689                           if (word_top > 0) word_top--;
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     {
2716                           COMMAND *tc;
2717
2718                           tc = (yyvsp[(1) - (2)].command);
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                                 ;
2737                               t->next = (yyvsp[(2) - (2)].redirect);
2738                             }
2739                           else
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:
2762 #line 826 "/Users/chet/src/bash/src/parse.y"
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:
2790 #line 848 "/Users/chet/src/bash/src/parse.y"
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);
2931                           if (need_here_doc)
2932                             gather_here_documents ();
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, '&');
2948                           else
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), '&');
2968                           else
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);
3007                           if (need_here_doc)
3008                             gather_here_documents ();
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, '&');
3024                           else
3025                             (yyval.command) = command_connect ((yyvsp[(1) - (2)].command), (COMMAND *)NULL, '&');
3026                           if (need_here_doc)
3027                             gather_here_documents ();
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);
3042                           if (need_here_doc)
3043                             gather_here_documents ();
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), '&');
3069                           else
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     {
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;
3137                           (yyval.command) = make_simple_command (x, (COMMAND *)NULL);
3138                           (yyval.command)->flags |= (yyvsp[(1) - (2)].number);
3139                           /* XXX - let's cheat and push a newline back */
3140                           if ((yyvsp[(2) - (2)].number) == '\n')
3141                             token_to_read = '\n';
3142                         }
3143     break;
3144
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"
3152     {
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;
3194     }
3195   YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
3196
3197   YYPOPSTACK (yylen);
3198   yylen = 0;
3199   YY_STACK_PRINT (yyss, yyssp);
3200
3201   *++yyvsp = yyval;
3202
3203
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.  */
3207
3208   yyn = yyr1[yyn];
3209
3210   yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
3211   if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
3212     yystate = yytable[yystate];
3213   else
3214     yystate = yydefgoto[yyn - YYNTOKENS];
3215
3216   goto yynewstate;
3217
3218
3219 /*------------------------------------.
3220 | yyerrlab -- here on detecting error |
3221 `------------------------------------*/
3222 yyerrlab:
3223   /* If not already recovering from an error, report this error.  */
3224   if (!yyerrstatus)
3225     {
3226       ++yynerrs;
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           }
3248
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
3262     }
3263
3264
3265
3266   if (yyerrstatus == 3)
3267     {
3268       /* If just tried and failed to reuse look-ahead token after an
3269          error, discard it.  */
3270
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         }
3283     }
3284
3285   /* Else will try to reuse look-ahead token after shifting the error
3286      token.  */
3287   goto yyerrlab1;
3288
3289
3290 /*---------------------------------------------------.
3291 | yyerrorlab -- error raised explicitly by YYERROR.  |
3292 `---------------------------------------------------*/
3293 yyerrorlab:
3294
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;
3300
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;
3308
3309
3310 /*-------------------------------------------------------------.
3311 | yyerrlab1 -- common code for both syntax error and YYERROR.  |
3312 `-------------------------------------------------------------*/
3313 yyerrlab1:
3314   yyerrstatus = 3;      /* Each real token shifted decrements this.  */
3315
3316   for (;;)
3317     {
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         }
3329
3330       /* Pop the current state because it cannot handle the error token.  */
3331       if (yyssp == yyss)
3332         YYABORT;
3333
3334
3335       yydestruct ("Error: popping",
3336                   yystos[yystate], yyvsp);
3337       YYPOPSTACK (1);
3338       yystate = *yyssp;
3339       YY_STACK_PRINT (yyss, yyssp);
3340     }
3341
3342   if (yyn == YYFINAL)
3343     YYACCEPT;
3344
3345   *++yyvsp = yylval;
3346
3347
3348   /* Shift the error token.  */
3349   YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
3350
3351   yystate = yyn;
3352   goto yynewstate;
3353
3354
3355 /*-------------------------------------.
3356 | yyacceptlab -- YYACCEPT comes here.  |
3357 `-------------------------------------*/
3358 yyacceptlab:
3359   yyresult = 0;
3360   goto yyreturn;
3361
3362 /*-----------------------------------.
3363 | yyabortlab -- YYABORT comes here.  |
3364 `-----------------------------------*/
3365 yyabortlab:
3366   yyresult = 1;
3367   goto yyreturn;
3368
3369 #ifndef yyoverflow
3370 /*-------------------------------------------------.
3371 | yyexhaustedlab -- memory exhaustion comes here.  |
3372 `-------------------------------------------------*/
3373 yyexhaustedlab:
3374   yyerror (YY_("memory exhausted"));
3375   yyresult = 2;
3376   /* Fall through.  */
3377 #endif
3378
3379 yyreturn:
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)
3388     {
3389       yydestruct ("Cleanup: popping",
3390                   yystos[*yyssp], yyvsp);
3391       YYPOPSTACK (1);
3392     }
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);
3403 }
3404
3405
3406 #line 1149 "/Users/chet/src/bash/src/parse.y"
3407
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
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
3424 /* Global var is non-zero when end of file has been reached. */
3425 int EOF_Reached = 0;
3426
3427 #ifdef DEBUG
3428 static void
3429 debug_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. */
3447 int
3448 return_EOF ()
3449 {
3450   return (EOF);
3451 }
3452
3453 /* Variable containing the current get and unget functions.
3454    See ./input.h for a clearer description. */
3455 BASH_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. */
3459 void
3460 initialize_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. */
3473 void
3474 init_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
3495 char *
3496 yy_input_name ()
3497 {
3498   return (bash_input.name ? bash_input.name : "stdin");
3499 }
3500
3501 /* Call this to get the next character of input. */
3502 static int
3503 yy_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. */
3510 static int
3511 yy_ungetc (c)
3512      int c;
3513 {
3514   return (*(bash_input.ungetter)) (c);
3515 }
3516
3517 #if defined (BUFFERED_INPUT)
3518 #ifdef INCLUDE_UNUSED
3519 int
3520 input_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)
3543 char *current_readline_prompt = (char *)NULL;
3544 char *current_readline_line = (char *)NULL;
3545 int current_readline_line_index = 0;
3546
3547 static int
3548 yy_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         }
3570       terminate_immediately = 1;
3571
3572       current_readline_line = readline (current_readline_prompt ?
3573                                           current_readline_prompt : "");
3574
3575       terminate_immediately = 0;
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
3611 static int
3612 yy_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
3620 void
3621 with_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
3635 void
3636 with_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
3648 static int
3649 yy_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
3667 static int
3668 yy_string_unget (c)
3669      int c;
3670 {
3671   *(--bash_input.location.string) = c;
3672   return (c);
3673 }
3674
3675 void
3676 with_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
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. */
3691 static void
3692 rewind_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
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
3725 static int
3726 yy_stream_get ()
3727 {
3728   int result;
3729
3730   result = EOF;
3731   if (bash_input.location.file)
3732     {
3733       if (interactive)
3734         {
3735           interrupt_immediately++;
3736           terminate_immediately++;
3737         }
3738       result = getc_with_restart (bash_input.location.file);
3739       if (interactive)
3740         {
3741           interrupt_immediately--;
3742           terminate_immediately--;
3743         }
3744     }
3745   return (result);
3746 }
3747
3748 static int
3749 yy_stream_unget (c)
3750      int c;
3751 {
3752   return (ungetc_with_restart (c, bash_input.location.file));
3753 }
3754
3755 void
3756 with_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
3766 typedef 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. */
3776 int line_number = 0;
3777
3778 #if defined (COND_COMMAND)
3779 static int cond_lineno;
3780 static int cond_token;
3781 #endif
3782
3783 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
3784
3785 void
3786 push_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
3810 void
3811 pop_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. */
3858 int
3859 stream_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. */
3871 int *
3872 save_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
3883 void
3884 restore_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
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
3916 typedef 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
3926 STRING_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  */
3936 static void
3937 push_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';
3964 #if 0
3965   parser_state &= ~PST_ALEXPNEXT;       /* XXX */
3966 #endif
3967
3968   set_line_mbstate ();
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  */
3977 static void
3978 pop_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);
4002
4003   set_line_mbstate ();
4004 }
4005
4006 static void
4007 free_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
4027 void
4028 free_pushed_string_input ()
4029 {
4030 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4031   free_string_list ();
4032 #endif
4033 }
4034
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. */
4039 static char *
4040 read_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)
4048   if (no_line_editing && SHOULD_PROMPT ())
4049 #else
4050   if (SHOULD_PROMPT ())
4051 #endif
4052     print_prompt ();
4053
4054   pass_next = 0;
4055   while (1)
4056     {
4057       /* Allow immediate exit if interrupted during input. */
4058       QUIT;
4059
4060       c = yy_getc ();
4061
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')
4099             {
4100               line_number++;
4101               continue; /* Make the unquoted \<newline> pair disappear. */
4102             }
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. */
4126 char *
4127 read_secondary_line (remove_quoted_newline)
4128      int remove_quoted_newline;
4129 {
4130   char *ret;
4131   int n, c;
4132
4133   prompt_string_pointer = &ps2_prompt;
4134   if (SHOULD_PROMPT())
4135     prompt_again ();
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;
4151 }
4152
4153 /* **************************************************************** */
4154 /*                                                                  */
4155 /*                              YYLEX ()                            */
4156 /*                                                                  */
4157 /* **************************************************************** */
4158
4159 /* Reserved words.  These are only recognized as the first word of a
4160    command. */
4161 STRING_INT_ALIST word_token_alist[] = {
4162   { "if", IF },
4163   { "then", THEN },
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 },
4172 #endif
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 },
4188 #endif
4189 #if defined (COPROCESS_SUPPORT)
4190   { "coproc", COPROC },
4191 #endif
4192   { (char *)NULL, 0}
4193 };
4194
4195 /* other tokens that can be returned by read_token() */
4196 STRING_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 },
4206   { ";&", SEMI_AND },
4207   { ";;&", SEMI_SEMI_AND },
4208   { "<<-", LESS_LESS_MINUS },
4209   { "<<<", LESS_LESS_LESS },
4210   { "&>", AND_GREATER },
4211   { "&>>", AND_GREATER_GREATER },
4212   { "<>", LESS_GREATER },
4213   { ">|", GREATER_BAR },
4214   { "|&", BAR_AND },
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 */
4239
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. */
4242
4243 /* The primary delimiter stack. */
4244 struct dstack dstack = {  (char *)NULL, 0, 0 };
4245
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. */
4249 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
4250
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)
4255
4256 #define push_delimiter(ds, character) \
4257   do \
4258     { \
4259       if (ds.delimiter_depth + 2 > ds.delimiter_space) \
4260         ds.delimiters = (char *)xrealloc \
4261           (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
4262       ds.delimiters[ds.delimiter_depth] = character; \
4263       ds.delimiter_depth++; \
4264     } \
4265   while (0)
4266
4267 #define pop_delimiter(ds)       ds.delimiter_depth--
4268
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. */
4273
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. */
4277 static int eol_ungetc_lookahead = 0;
4278
4279 static int
4280 shell_getc (remove_quoted_newline)
4281      int remove_quoted_newline;
4282 {
4283   register int i;
4284   int c;
4285   unsigned char uc;
4286
4287   QUIT;
4288
4289   if (sigwinch_received)
4290     {
4291       sigwinch_received = 0;
4292       get_new_window_size (0, (int *)0, (int *)0);
4293     }
4294       
4295   if (eol_ungetc_lookahead)
4296     {
4297       c = eol_ungetc_lookahead;
4298       eol_ungetc_lookahead = 0;
4299       return (c);
4300     }
4301
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. */
4306
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++;
4314
4315     restart_read:
4316
4317       /* Allow immediate exit if interrupted during input. */
4318       QUIT;
4319
4320       i = 0;
4321       shell_input_line_terminator = 0;
4322
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         {
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. */
4334           notify_and_cleanup ();
4335 #else /* !JOB_CONTROL */
4336           cleanup_dead_jobs ();
4337 #endif /* !JOB_CONTROL */
4338         }
4339
4340 #if defined (READLINE)
4341       if (no_line_editing && SHOULD_PROMPT())
4342 #else
4343       if (SHOULD_PROMPT())
4344 #endif
4345         print_prompt ();
4346
4347       if (bash_input.type == st_stream)
4348         clearerr (stdin);
4349
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");
4361 #endif
4362               continue;
4363             }
4364
4365           RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
4366
4367           if (c == EOF)
4368             {
4369               if (bash_input.type == st_stream)
4370                 clearerr (stdin);
4371
4372               if (i == 0)
4373                 shell_input_line_terminator = EOF;
4374
4375               shell_input_line[i] = '\0';
4376               break;
4377             }
4378
4379           shell_input_line[i++] = c;
4380
4381           if (c == '\n')
4382             {
4383               shell_input_line[--i] = '\0';
4384               current_command_line_count++;
4385               break;
4386             }
4387         }
4388
4389       shell_input_line_index = 0;
4390       shell_input_line_len = i;         /* == strlen (shell_input_line) */
4391
4392       set_line_mbstate ();
4393
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;
4400
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--;
4420
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;
4424
4425               set_line_mbstate ();
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)
4433         {
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             }
4447         }
4448
4449 #endif /* HISTORY */
4450
4451       if (shell_input_line)
4452         {
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;
4463           if (SHOULD_PROMPT ())
4464             prompt_again ();
4465           goto restart_read;
4466         }
4467
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)
4473             shell_input_line = (char *)xrealloc (shell_input_line,
4474                                         1 + (shell_input_line_size += 2));
4475
4476           shell_input_line[shell_input_line_len] = '\n';
4477           shell_input_line[shell_input_line_len + 1] = '\0';
4478
4479           set_line_mbstate ();
4480         }
4481     }
4482
4483   uc = shell_input_line[shell_input_line_index];
4484
4485   if (uc)
4486     shell_input_line_index++;
4487
4488 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4489   /* If UC is NULL, we have reached the end of the current input string.  If
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. */
4494   if (!uc && (pushed_string_list != (STRING_SAVER *)NULL))
4495     {
4496       pop_string ();
4497       uc = shell_input_line[shell_input_line_index];
4498       if (uc)
4499         shell_input_line_index++;
4500     }
4501 #endif /* ALIAS || DPAREN_ARITHMETIC */
4502
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
4511   if (!uc && shell_input_line_terminator == EOF)
4512     return ((shell_input_line_index != 0) ? '\n' : EOF);
4513
4514   return (uc);
4515 }
4516
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. */
4522 static void
4523 shell_ungetc (c)
4524      int c;
4525 {
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;
4530 }
4531
4532 #ifdef INCLUDE_UNUSED
4533 /* Back the input pointer up by one, effectively `ungetting' a character. */
4534 static void
4535 shell_ungetchar ()
4536 {
4537   if (shell_input_line && shell_input_line_index)
4538     shell_input_line_index--;
4539 }
4540 #endif
4541
4542 /* Discard input until CHARACTER is seen, then push that character back
4543    onto the input stream. */
4544 static void
4545 discard_until (character)
4546      int character;
4547 {
4548   int c;
4549
4550   while ((c = shell_getc (0)) != EOF && c != character)
4551     ;
4552
4553   if (c != EOF)
4554     shell_ungetc (c);
4555 }
4556
4557 void
4558 execute_variable_command (command, vname)
4559      char *command, *vname;
4560 {
4561   char *last_lastarg;
4562   sh_parser_state_t ps;
4563
4564   save_parser_state (&ps);
4565   last_lastarg = get_string_value ("_");
4566   if (last_lastarg)
4567     last_lastarg = savestring (last_lastarg);
4568
4569   parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST);
4570
4571   restore_parser_state (&ps);
4572   bind_variable ("_", last_lastarg, 0);
4573   FREE (last_lastarg);
4574
4575   if (token_to_read == '\n')    /* reset_parser was called */
4576     token_to_read = 0;
4577 }
4578
4579 /* Place to remember the token.  We try to keep the buffer
4580    at a reasonable size, but it can grow. */
4581 static char *token = (char *)NULL;
4582
4583 /* Current size of the token buffer. */
4584 static 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)
4591
4592 /* Function for yyparse to call.  yylex keeps track of
4593    the last two tokens read, and calls read_token.  */
4594 static int
4595 yylex ()
4596 {
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         }
4608
4609       /* Avoid printing a prompt if we're not going to read anything, e.g.
4610          after resetting the parser with read_token (RESET). */
4611       if (token_to_read == 0 && SHOULD_PROMPT ())
4612         prompt_again ();
4613     }
4614
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);
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
4628   return (current_token);
4629 }
4630
4631 /* When non-zero, we have read the required tokens
4632    which allow ESAC to be the next one read. */
4633 static int esacs_needed_count;
4634
4635 void
4636 gather_here_documents ()
4637 {
4638   int r;
4639
4640   r = 0;
4641   while (need_here_doc)
4642     {
4643       parser_state |= PST_HEREDOC;
4644       make_here_document (redir_stack[r++], line_number);
4645       parser_state &= ~PST_HEREDOC;
4646       need_here_doc--;
4647     }
4648 }
4649
4650 /* When non-zero, an open-brace used to create a group is awaiting a close
4651    brace partner. */
4652 static int open_brace_count;
4653
4654 #define command_token_position(token) \
4655   (((token) == ASSIGNMENT_WORD) || \
4656    ((token) != SEMI_SEMI && (token) != SEMI_AND && (token) != SEMI_SEMI_AND && reserved_word_acceptable(token)))
4657
4658 #define assignment_acceptable(token) \
4659   (command_token_position(token) && ((parser_state & PST_CASEPAT) == 0))
4660
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; \
4674               if (word_token_alist[i].token == TIME && time_command_acceptable () == 0) \
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)
4692
4693 #if defined (ALIAS)
4694
4695     /* OK, we have a token.  Let's try to alias expand it, if (and only if)
4696        it's eligible.
4697
4698        It is eligible for expansion if EXPAND_ALIASES is set, and
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.
4704
4705        Special cases that disqualify:
4706          In a pattern list in a case statement (parser_state & PST_CASEPAT). */
4707
4708 static char *
4709 mk_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
4724 static int
4725 alias_expand_token (tokstr)
4726      char *tokstr;
4727 {
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)
4733     {
4734       ap = find_alias (tokstr);
4735
4736       /* Currently expanding this token. */
4737       if (ap && (ap->flags & AL_BEINGEXPANDED))
4738         return (NO_EXPANSION);
4739
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
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 */
4758
4759 static int
4760 time_command_acceptable ()
4761 {
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 '(':           /* ) */
4776       return 1;
4777     default:
4778       return 0;
4779     }
4780 #else
4781   return 0;
4782 #endif /* COMMAND_TIMING */
4783 }
4784
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.
4788
4789         DO is recognized if the last token was WORD and the token
4790         before that was FOR or SELECT.
4791
4792         ESAC is recognized if the last token caused `esacs_needed_count'
4793         to be set
4794
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.
4798
4799         `}' is recognized if there is an unclosed `{' present.
4800
4801         `-p' is returned as TIMEOPT if the last read token was TIME.
4802
4803         ']]' is returned as COND_END if the parser is currently parsing
4804         a conditional expression ((parser_state & PST_CONDEXPR) != 0)
4805
4806         `time' is returned as TIME if and only if it is immediately
4807         preceded by one of `;', `\n', `||', `&&', or `&'.
4808 */
4809
4810 static int
4811 special_case_tokens (tokstr)
4812      char *tokstr;
4813 {
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)) &&
4819 #endif
4820       (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
4821     {
4822       if (token_before_that == CASE)
4823         {
4824           parser_state |= PST_CASEPAT;
4825           esacs_needed_count++;
4826         }
4827       return (IN);
4828     }
4829
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) &&
4835 #endif
4836       (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
4837     return (DO);
4838
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--;
4848       if (STREQ (tokstr, "esac"))
4849         {
4850           parser_state &= ~PST_CASEPAT;
4851           return (ESAC);
4852         }
4853     }
4854
4855   /* The start of a shell function definition. */
4856   if (parser_state & PST_ALLOWOPNBRC)
4857     {
4858       parser_state &= ~PST_ALLOWOPNBRC;
4859       if (tokstr[0] == '{' && tokstr[1] == '\0')                /* } */
4860         {
4861           open_brace_count++;
4862           function_bstart = line_number;
4863           return ('{');                                 /* } */
4864         }
4865     }
4866
4867   /* We allow a `do' after a for ((...)) without an intervening
4868      list_terminator */
4869   if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
4870     return (DO);
4871   if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0')      /* } */
4872     {
4873       open_brace_count++;
4874       return ('{');                     /* } */
4875     }
4876
4877   if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
4878     {
4879       open_brace_count--;               /* { */
4880       return ('}');
4881     }
4882
4883 #if defined (COMMAND_TIMING)
4884   /* Handle -p after `time'. */
4885   if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
4886     return (TIMEOPT);
4887 #endif
4888
4889 #if 0
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 */
4894 #endif
4895
4896 #if defined (COND_COMMAND) /* [[ */
4897   if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
4898     return (COND_END);
4899 #endif
4900
4901   return (-1);
4902 }
4903
4904 /* Called from shell.c when Control-C is typed at top level.  Or
4905    by the error rule at top level. */
4906 void
4907 reset_parser ()
4908 {
4909   dstack.delimiter_depth = 0;   /* No delimiters found so far. */
4910   open_brace_count = 0;
4911
4912   parser_state = 0;
4913
4914 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
4915   if (pushed_string_list)
4916     free_string_list ();
4917 #endif /* ALIAS || DPAREN_ARITHMETIC */
4918
4919   if (shell_input_line)
4920     {
4921       free (shell_input_line);
4922       shell_input_line = (char *)NULL;
4923       shell_input_line_size = shell_input_line_index = 0;
4924     }
4925
4926   FREE (word_desc_to_read);
4927   word_desc_to_read = (WORD_DESC *)NULL;
4928
4929   last_read_token = '\n';
4930   token_to_read = '\n';
4931 }
4932
4933 /* Read the next token.  Command can be READ (normal operation) or
4934    RESET (to normalize state). */
4935 static int
4936 read_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. */
4942
4943   if (command == RESET)
4944     {
4945       reset_parser ();
4946       return ('\n');
4947     }
4948
4949   if (token_to_read)
4950     {
4951       result = token_to_read;
4952       if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
4953         {
4954           yylval.word = word_desc_to_read;
4955           word_desc_to_read = (WORD_DESC *)NULL;
4956         }
4957       token_to_read = 0;
4958       return (result);
4959     }
4960
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)
4968         {
4969           cond_error ();
4970           return (-1);
4971         }
4972       token_to_read = COND_END;
4973       parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
4974       return (COND_CMD);
4975     }
4976 #endif
4977
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 */
4983
4984   /* Read a single word from input.  Start by skipping blanks. */
4985   while ((character = shell_getc (1)) != EOF && shellblank (character))
4986     ;
4987
4988   if (character == EOF)
4989     {
4990       EOF_Reached = 1;
4991       return (yacc_EOF);
4992     }
4993
4994   if MBTEST(character == '#' && (!interactive || interactive_comments))
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     }
5001
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 ();
5008
5009 #if defined (ALIAS)
5010       parser_state &= ~PST_ALEXPNEXT;
5011 #endif /* ALIAS */
5012
5013       parser_state &= ~PST_ASSIGNOK;
5014
5015       return (character);
5016     }
5017
5018   if (parser_state & PST_REGEXP)
5019     goto tokword;
5020
5021   /* Shell meta-characters. */
5022   if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
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 */
5030
5031       parser_state &= ~PST_ASSIGNOK;
5032
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);
5042               if MBTEST(peek_char == '-')
5043                 return (LESS_LESS_MINUS);
5044               else if MBTEST(peek_char == '<')
5045                 return (LESS_LESS_LESS);
5046               else
5047                 {
5048                   shell_ungetc (peek_char);
5049                   return (LESS_LESS);
5050                 }
5051
5052             case '>':
5053               return (GREATER_GREATER);
5054
5055             case ';':
5056               parser_state |= PST_CASEPAT;
5057 #if defined (ALIAS)
5058               parser_state &= ~PST_ALEXPNEXT;
5059 #endif /* ALIAS */
5060
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                 }
5069
5070             case '&':
5071               return (AND_AND);
5072
5073             case '|':
5074               return (OR_OR);
5075
5076 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
5077             case '(':           /* ) */
5078               result = parse_dparen (character);
5079               if (result == -2)
5080                 break;
5081               else
5082                 return result;
5083 #endif
5084             }
5085         }
5086       else if MBTEST(character == '<' && peek_char == '&')
5087         return (LESS_AND);
5088       else if MBTEST(character == '>' && peek_char == '&')
5089         return (GREATER_AND);
5090       else if MBTEST(character == '<' && peek_char == '>')
5091         return (LESS_GREATER);
5092       else if MBTEST(character == '>' && peek_char == '|')
5093         return (GREATER_BAR);
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         }
5115
5116       shell_ungetc (peek_char);
5117
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 `{'. */
5121       if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
5122         {
5123           parser_state |= PST_ALLOWOPNBRC;
5124 #if defined (ALIAS)
5125           parser_state &= ~PST_ALEXPNEXT;
5126 #endif /* ALIAS */
5127           function_dstart = line_number;
5128         }
5129
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. */
5133       if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
5134         parser_state |= PST_SUBSHELL;
5135       /*(*/
5136       else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
5137         parser_state &= ~PST_CASEPAT;
5138       /*(*/
5139       else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
5140         parser_state &= ~PST_SUBSHELL;
5141
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. */
5145       if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/
5146 #endif /* PROCESS_SUBSTITUTION */
5147         return (character);
5148     }
5149
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
5154 tokword:
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
5210 static char matched_pair_error;
5211
5212 static char *
5213 parse_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 ();
5417
5418           FREE (nestret);
5419         }
5420       if MBTEST(ch == '$')
5421         tflags |= LEX_WASDOL;
5422       else
5423         tflags &= ~LEX_WASDOL;
5424     }
5425
5426   ret[retind] = '\0';
5427   if (lenp)
5428     *lenp = retind;
5429   return ret;
5430 }
5431
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. */
5434 static char *
5435 parse_comsub (qc, open, close, lenp, flags)
5436      int qc;    /* `"' if this construct is within double quotes */
5437      int open, close;
5438      int *lenp, flags;
5439 {
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;
5444
5445 /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/
5446   count = 1;
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;
5453
5454   /* RFLAGS is the set of flags we want to pass to recursive calls. */
5455   rflags = (flags & P_DQUOTE);
5456
5457   ret = (char *)xmalloc (retsize = 64);
5458   retind = 0;
5459
5460   start_lineno = line_number;
5461   lex_rwlen = 0;
5462
5463   heredelim = 0;
5464   lex_firstind = -1;
5465
5466   while (count)
5467     {
5468 comsub_readchar:
5469       ch = shell_getc (qc != '\'' && (tflags & LEX_PASSNEXT) == 0);
5470
5471       if (ch == EOF)
5472         {
5473 eof_error:
5474           free (ret);
5475           FREE (heredelim);
5476           parser_error (start_lineno, _("unexpected EOF while looking for matching `%c'"), close);
5477           EOF_Reached = 1;      /* XXX */
5478           return (&matched_pair_error);
5479         }
5480
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
5511       /* Possible reprompting. */
5512       if (ch == '\n' && SHOULD_PROMPT ())
5513         prompt_again ();
5514
5515       /* Don't bother counting parens or doing anything else if in a comment */
5516       if (tflags & (LEX_INCOMMENT|LEX_INHEREDOC))
5517         {
5518           /* Add this character. */
5519           RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
5520           ret[retind++] = ch;
5521
5522           if ((tflags & LEX_INCOMMENT) && ch == '\n')
5523             tflags &= ~LEX_INCOMMENT;
5524
5525           continue;
5526         }
5527
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)
5545         {
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             }
5564         }
5565
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))
5568         {
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;
5671           if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
5672             {
5673               if (retind > 0)
5674                 retind--;       /* swallow previously-added backslash */
5675               continue;
5676             }
5677
5678           RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
5679           if MBTEST(ch == CTLESC || ch == CTLNUL)
5680             ret[retind++] = CTLESC;
5681           ret[retind++] = ch;
5682           continue;
5683         }
5684       else if MBTEST(ch == CTLESC || ch == CTLNUL)      /* special shell escapes */
5685         {
5686           RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
5687           ret[retind++] = CTLESC;
5688           ret[retind++] = ch;
5689           continue;
5690         }
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 {
5697         count--;
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 */
5701         count++;
5702
5703       /* Add this character. */
5704       RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
5705       ret[retind++] = ch;
5706
5707       /* If we just read the ending character, don't bother continuing. */
5708       if (count == 0)
5709         break;
5710
5711       if MBTEST(ch == '\\')                     /* backslashes */
5712         tflags |= LEX_PASSNEXT;
5713
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))
5726             {
5727               /* Translate $'...' here. */
5728               ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
5729               xfree (nestret);
5730
5731               if ((rflags & P_DQUOTE) == 0)
5732                 {
5733                   nestret = sh_single_quote (ttrans);
5734                   free (ttrans);
5735                   nestlen = strlen (nestret);
5736                 }
5737               else
5738                 {
5739                   nestret = ttrans;
5740                   nestlen = ttranslen;
5741                 }
5742               retind -= 2;              /* back up before the $' */
5743             }
5744           else if MBTEST((tflags & LEX_WASDOL) && ch == '"' && (extended_quote || (rflags & P_DQUOTE) == 0))
5745             {
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 $" */
5754             }
5755
5756           APPEND_NESTRET ();
5757           FREE (nestret);
5758         }
5759       else if MBTEST((tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '['))    /* ) } ] */
5760         /* check for $(), $[], or ${} inside command substitution. */
5761         {
5762           if ((tflags & LEX_INCASE) == 0 && open == ch) /* undo previous increment */
5763             count--;
5764           if (ch == '(')                /* ) */
5765             nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE);
5766           else if (ch == '{')           /* } */
5767             nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|rflags);
5768           else if (ch == '[')           /* ] */
5769             nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags);
5770
5771           CHECK_NESTRET_ERROR ();
5772           APPEND_NESTRET ();
5773
5774           FREE (nestret);
5775         }
5776       if MBTEST(ch == '$')
5777         tflags |= LEX_WASDOL;
5778       else
5779         tflags &= ~LEX_WASDOL;
5780     }
5781
5782   FREE (heredelim);
5783   ret[retind] = '\0';
5784   if (lenp)
5785     *lenp = retind;
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. */
5792 char *
5793 xparse_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
5856   return ret;
5857 }
5858
5859 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
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. */
5864 static int
5865 parse_dparen (c)
5866      int c;
5867 {
5868   int cmdtyp, sline;
5869   char *wval;
5870   WORD_DESC *wd;
5871
5872 #if defined (ARITH_FOR_COMMAND)
5873   if (last_read_token == FOR)
5874     {
5875       arith_for_lineno = line_number;
5876       cmdtyp = parse_arith_cmd (&wval, 0);
5877       if (cmdtyp == 1)
5878         {
5879           wd = alloc_word_desc ();
5880           wd->word = wval;
5881           yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
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;
5893
5894       cmdtyp = parse_arith_cmd (&wval, 0);
5895       if (cmdtyp == 1)  /* arithmetic command */
5896         {
5897           wd = alloc_word_desc ();
5898           wd->word = wval;
5899           wd->flags = W_QUOTED|W_NOSPLIT|W_NOGLOB|W_DQUOTE;
5900           yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
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
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. */
5923 static int
5924 parse_arith_cmd (ep, adddq)
5925      char **ep;
5926      int adddq;
5927 {
5928   int exp_lineno, rval, c;
5929   char *ttok, *tokstr;
5930   int ttoklen;
5931
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. ( */
5939   c = shell_getc (0);
5940   if MBTEST(c != ')')
5941     rval = 0;
5942
5943   tokstr = (char *)xmalloc (ttoklen + 4);
5944
5945   /* if ADDDQ != 0 then (( ... )) -> "..." */
5946   if (rval == 1 && adddq)       /* arith cmd, add double quotes */
5947     {
5948       tokstr[0] = '"';
5949       strncpy (tokstr + 1, ttok, ttoklen - 1);
5950       tokstr[ttoklen] = '"';
5951       tokstr[ttoklen+1] = '\0';
5952     }
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 */
5959     {
5960       tokstr[0] = '(';
5961       strncpy (tokstr + 1, ttok, ttoklen - 1);
5962       tokstr[ttoklen] = ')';
5963       tokstr[ttoklen+1] = c;
5964       tokstr[ttoklen+2] = '\0';
5965     }
5966
5967   *ep = tokstr;
5968   FREE (ttok);
5969   return rval;
5970 }
5971 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
5972
5973 #if defined (COND_COMMAND)
5974 static void
5975 cond_error ()
5976 {
5977   char *etext;
5978
5979   if (EOF_Reached && cond_token != COND_ERROR)          /* [[ */
5980     parser_error (cond_lineno, _("unexpected EOF while looking for `]]'"));
5981   else if (cond_token != COND_ERROR)
5982     {
5983       if (etext = error_token_from_token (cond_token))
5984         {
5985           parser_error (cond_lineno, _("syntax error in conditional expression: unexpected token `%s'"), etext);
5986           free (etext);
5987         }
5988       else
5989         parser_error (cond_lineno, _("syntax error in conditional expression"));
5990     }
5991 }
5992
5993 static COND_COM *
5994 cond_expr ()
5995 {
5996   return (cond_or ());  
5997 }
5998
5999 static COND_COM *
6000 cond_or ()
6001 {
6002   COND_COM *l, *r;
6003
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;
6011 }
6012
6013 static COND_COM *
6014 cond_and ()
6015 {
6016   COND_COM *l, *r;
6017
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;
6025 }
6026
6027 static int
6028 cond_skip_newlines ()
6029 {
6030   while ((cond_token = read_token (READ)) == '\n')
6031     {
6032       if (SHOULD_PROMPT ())
6033         prompt_again ();
6034     }
6035   return (cond_token);
6036 }
6037
6038 #define COND_RETURN_ERROR() \
6039   do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
6040
6041 static COND_COM *
6042 cond_term ()
6043 {
6044   WORD_DESC *op;
6045   COND_COM *term, *tleft, *tright;
6046   int tok, lineno;
6047   char *etext;
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)
6055     {
6056       COND_RETURN_ERROR ();
6057     }
6058   else if (tok == '(')
6059     {
6060       term = cond_expr ();
6061       if (cond_token != ')')
6062         {
6063           if (term)
6064             dispose_cond_node (term);           /* ( */
6065           if (etext = error_token_from_token (cond_token))
6066             {
6067               parser_error (lineno, _("unexpected token `%s', expected `)'"), etext);
6068               free (etext);
6069             }
6070           else
6071             parser_error (lineno, _("expected `)'"));
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     }
6085   else if (tok == WORD && yylval.word->word[0] == '-' && yylval.word->word[2] == 0 && test_unop (yylval.word->word))
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);
6097           if (etext = error_token_from_token (tok))
6098             {
6099               parser_error (line_number, _("unexpected argument `%s' to conditional unary operator"), etext);
6100               free (etext);
6101             }
6102           else
6103             parser_error (line_number, _("unexpected argument to conditional unary operator"));
6104           COND_RETURN_ERROR ();
6105         }
6106
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);
6113
6114       /* binop */
6115       tok = read_token (READ);
6116       if (tok == WORD && test_binop (yylval.word->word))
6117         op = yylval.word;
6118 #if defined (COND_REGEXP)
6119       else if (tok == WORD && STREQ (yylval.word->word, "=~"))
6120         {
6121           op = yylval.word;
6122           parser_state |= PST_REGEXP;
6123         }
6124 #endif
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         {
6141           if (etext = error_token_from_token (tok))
6142             {
6143               parser_error (line_number, _("unexpected token `%s', conditional binary operator expected"), etext);
6144               free (etext);
6145             }
6146           else
6147             parser_error (line_number, _("conditional binary operator expected"));
6148           dispose_cond_node (tleft);
6149           COND_RETURN_ERROR ();
6150         }
6151
6152       /* rhs */
6153       tok = read_token (READ);
6154       parser_state &= ~PST_REGEXP;
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         {
6162           if (etext = error_token_from_token (tok))
6163             {
6164               parser_error (line_number, _("unexpected argument `%s' to conditional binary operator"), etext);
6165               free (etext);
6166             }
6167           else
6168             parser_error (line_number, _("unexpected argument to conditional binary operator"));
6169           dispose_cond_node (tleft);
6170           dispose_word (op);
6171           COND_RETURN_ERROR ();
6172         }
6173
6174       (void)cond_skip_newlines ();
6175     }
6176   else
6177     {
6178       if (tok < 256)
6179         parser_error (line_number, _("unexpected token `%c' in conditional command"), tok);
6180       else if (etext = error_token_from_token (tok))
6181         {
6182           parser_error (line_number, _("unexpected token `%s' in conditional command"), etext);
6183           free (etext);
6184         }
6185       else
6186         parser_error (line_number, _("unexpected token %d in conditional command"), tok);
6187       COND_RETURN_ERROR ();
6188     }
6189   return (term);
6190 }      
6191
6192 /* This is kind of bogus -- we slip a mini recursive-descent parser in
6193    here to handle the conditional statement syntax. */
6194 static COMMAND *
6195 parse_cond_command ()
6196 {
6197   COND_COM *cexp;
6198
6199   cexp = cond_expr ();
6200   return (make_cond_command (cexp));
6201 }
6202 #endif
6203
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. */
6207 static int
6208 token_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';
6217   r = assignment (t, (parser_state & PST_COMPASSIGN) != 0);
6218   t[i] = c; t[i+1] = c1;
6219   return r;
6220 }
6221
6222 /* XXX - possible changes here for `+=' */
6223 static int
6224 token_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
6239 static int
6240 read_token_word (character)
6241      int character;
6242 {
6243   /* The value for YYLVAL when a WORD is read. */
6244   WORD_DESC *the_word;
6245
6246   /* Index into the token that we are building. */
6247   int token_index;
6248
6249   /* ALL_DIGITS becomes zero when we see a non-digit. */
6250   int all_digit_token;
6251
6252   /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
6253   int dollar_present;
6254
6255   /* COMPOUND_ASSIGNMENT becomes non-zero if we are parsing a compound
6256      assignment. */
6257   int compound_assignment;
6258
6259   /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
6260   int quoted;
6261
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;
6265
6266   /* The current delimiting character. */
6267   int cd;
6268   int result, peek_char;
6269   char *ttok, *ttrans;
6270   int ttoklen, ttranslen;
6271   intmax_t lvalue;
6272
6273   if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
6274     token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
6275
6276   token_index = 0;
6277   all_digit_token = DIGIT (character);
6278   dollar_present = quoted = pass_next_character = compound_assignment = 0;
6279
6280   for (;;)
6281     {
6282       if (character == EOF)
6283         goto got_token;
6284
6285       if (pass_next_character)
6286         {
6287           pass_next_character = 0;
6288           goto got_escaped_character;
6289         }
6290
6291       cd = current_delimiter (dstack);
6292
6293       /* Handle backslashes.  Quote lots of things when not inside of
6294          double-quotes, quote some things inside of double-quotes. */
6295       if MBTEST(character == '\\')
6296         {
6297           peek_char = shell_getc (0);
6298
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);
6309
6310               /* If the next character is to be quoted, note it now. */
6311               if (cd == 0 || cd == '`' ||
6312                   (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
6313                 pass_next_character++;
6314
6315               quoted = 1;
6316               goto got_character;
6317             }
6318         }
6319
6320       /* Parse a matched pair of quote characters. */
6321       if MBTEST(shellquote (character))
6322         {
6323           push_delimiter (dstack, character);
6324           ttok = parse_matched_pair (character, character, character, &ttoklen, (character == '`') ? P_COMMAND : 0);
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;
6333           all_digit_token = 0;
6334           quoted = 1;
6335           dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
6336           FREE (ttok);
6337           goto next_character;
6338         }
6339
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
6365 #ifdef EXTENDED_GLOB
6366       /* Parse a ksh-style extended pattern matching specification. */
6367       if MBTEST(extended_glob && PATTERN_CHAR (character))
6368         {
6369           peek_char = shell_getc (1);
6370           if MBTEST(peek_char == '(')           /* ) */
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);
6385               dollar_present = all_digit_token = 0;
6386               goto next_character;
6387             }
6388           else
6389             shell_ungetc (peek_char);
6390         }
6391 #endif /* EXTENDED_GLOB */
6392
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 */
6399           if MBTEST(peek_char == '(' || \
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);
6412                   ttok = parse_comsub (cd, '(', ')', &ttoklen, P_COMMAND);
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;
6428               all_digit_token = 0;
6429               goto next_character;
6430             }
6431           /* This handles $'...' and $"..." new-style quoted strings. */
6432           else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
6433             {
6434               int first_line;
6435
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);
6448
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);
6454                   ttranslen = strlen (ttok);
6455                   ttrans = ttok;
6456                 }
6457               else
6458                 {
6459                   /* Try to locale-expand the converted string. */
6460                   ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
6461                   free (ttok);
6462
6463                   /* Add the double quotes back */
6464                   ttok = sh_mkdoublequoted (ttrans, ttranslen, 0);
6465                   free (ttrans);
6466                   ttranslen += 2;
6467                   ttrans = ttok;
6468                 }
6469
6470               RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2,
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.*/
6482           else if MBTEST(character == '$' && peek_char == '$')
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         }
6500
6501 #if defined (ARRAY_VARS)
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))))
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         }
6522       /* Identify possible compound array variable assignment. */
6523       else if MBTEST(character == '=' && token_index > 0 && (assignment_acceptable (last_read_token) || (parser_state & PST_ASSIGNOK)) && token_is_assignment (token, token_index))
6524         {
6525           peek_char = shell_getc (1);
6526           if MBTEST(peek_char == '(')           /* ) */
6527             {
6528               ttok = parse_compound_assignment (&ttoklen);
6529
6530               RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
6531                                       token_buffer_size,
6532                                       TOKEN_DEFAULT_GROW_SIZE);
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++] = ')';
6542               FREE (ttok);
6543               all_digit_token = 0;
6544               compound_assignment = 1;
6545 #if 1
6546               goto next_character;
6547 #else
6548               goto got_token;           /* ksh93 seems to do this */
6549 #endif
6550             }
6551           else
6552             shell_ungetc (peek_char);
6553         }
6554 #endif
6555
6556       /* When not parsing a multi-character word construct, shell meta-
6557          characters break words. */
6558       if MBTEST(shellbreak (character))
6559         {
6560           shell_ungetc (character);
6561           goto got_token;
6562         }
6563
6564     got_character:
6565
6566       if (character == CTLESC || character == CTLNUL)
6567         token[token_index++] = CTLESC;
6568
6569     got_escaped_character:
6570
6571       all_digit_token &= DIGIT (character);
6572       dollar_present |= character == '$';
6573
6574       token[token_index++] = character;
6575
6576       RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
6577                               TOKEN_DEFAULT_GROW_SIZE);
6578
6579     next_character:
6580       if (character == '\n' && SHOULD_PROMPT ())
6581         prompt_again ();
6582
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 (;;) */
6589
6590 got_token:
6591
6592   token[token_index] = '\0';
6593
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. */
6598   if MBTEST(all_digit_token && (character == '<' || character == '>' || \
6599                     last_read_token == LESS_AND || \
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       }
6608
6609   /* Check for special case tokens. */
6610   result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
6611   if (result >= 0)
6612     return result;
6613
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. */
6618   if MBTEST(posixly_correct)
6619     CHECK_FOR_RESERVED_WORD (token);
6620
6621   /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
6622      inhibits alias expansion. */
6623   if (expand_aliases && quoted == 0)
6624     {
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;
6630     }
6631
6632   /* If not in Posix.2 mode, check for reserved words after alias
6633      expansion. */
6634   if MBTEST(posixly_correct == 0)
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)
6645     the_word->flags |= W_QUOTED;                /*(*/
6646   if (compound_assignment && token[token_index-1] == ')')
6647     the_word->flags |= W_COMPASSIGN;
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. */
6651   if (assignment (token, (parser_state & PST_COMPASSIGN) != 0))
6652     {
6653       the_word->flags |= W_ASSIGNMENT;
6654       /* Don't perform word splitting on assignment statements. */
6655       if (assignment_acceptable (last_read_token) || (parser_state & PST_COMPASSIGN) != 0)
6656         the_word->flags |= W_NOSPLIT;
6657     }
6658
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))
6664         parser_state |= PST_ASSIGNOK;
6665       else if (STREQ (token, "eval") || STREQ (token, "let"))
6666         parser_state |= PST_ASSIGNOK;
6667     }
6668
6669   yylval.word = the_word;
6670
6671   result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
6672                 ? ASSIGNMENT_WORD : WORD;
6673
6674   switch (last_read_token)
6675     {
6676     case FUNCTION:
6677       parser_state |= PST_ALLOWOPNBRC;
6678       function_dstart = line_number;
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;
6687     }
6688
6689   return (result);
6690 }
6691
6692 /* Return 1 if TOKSYM is a token that after being read would allow
6693    a reserved word to be seen, else 0. */
6694 static int
6695 reserved_word_acceptable (toksym)
6696      int toksym;
6697 {
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:
6719     case SEMI_AND:
6720     case SEMI_SEMI_AND:
6721     case THEN:
6722     case TIME:
6723     case TIMEOPT:
6724     case COPROC:
6725     case UNTIL:
6726     case WHILE:
6727     case 0:
6728       return 1;
6729     default:
6730 #if defined (COPROCESS_SUPPORT)
6731       if (last_read_token == WORD && token_before_that == COPROC)
6732         return 1;
6733 #endif
6734       return 0;
6735     }
6736 }
6737     
6738 /* Return the index of TOKEN in the alist of reserved words, or -1 if
6739    TOKEN is not a shell reserved word. */
6740 int
6741 find_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 }
6750
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. */
6756 static void
6757 reset_readline_prompt ()
6758 {
6759   char *temp_prompt;
6760
6761   if (prompt_string_pointer)
6762     {
6763       temp_prompt = (*prompt_string_pointer)
6764                         ? decode_prompt_string (*prompt_string_pointer)
6765                         : (char *)NULL;
6766
6767       if (temp_prompt == 0)
6768         {
6769           temp_prompt = (char *)xmalloc (1);
6770           temp_prompt[0] = '\0';
6771         }
6772
6773       FREE (current_readline_prompt);
6774       current_readline_prompt = temp_prompt;
6775     }
6776 }
6777 #endif /* READLINE */
6778 #endif /* 0 */
6779
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. */
6784 static const int no_semi_successors[] = {
6785   '\n', '{', '(', ')', ';', '&', '|',
6786   CASE, DO, ELSE, IF, SEMI_SEMI, SEMI_AND, SEMI_SEMI_AND, THEN, UNTIL,
6787   WHILE, AND_AND, OR_OR, IN,
6788   0
6789 };
6790
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. */
6795 char *
6796 history_delimiting_chars ()
6797 {
6798   register int i;
6799
6800   if (dstack.delimiter_depth != 0)
6801     return ("\n");
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
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 `()' */
6829
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. */
6834       for (i = shell_input_line_index; whitespace (shell_input_line[i]); i++)
6835         ;
6836       if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
6837         return " ";
6838       return ";";
6839     }
6840   else if (two_tokens_ago == CASE && token_before_that == WORD && (parser_state & PST_CASESTMT))
6841     return " ";
6842
6843   for (i = 0; no_semi_successors[i]; i++)
6844     {
6845       if (token_before_that == no_semi_successors[i])
6846         return (" ");
6847     }
6848
6849   return ("; ");
6850 }
6851 #endif /* HISTORY */
6852
6853 /* Issue a prompt, or prepare to issue a prompt when the next character
6854    is read. */
6855 static void
6856 prompt_again ()
6857 {
6858   char *temp_prompt;
6859
6860   if (interactive == 0 || expanding_alias())    /* XXX */
6861     return;
6862
6863   ps1_prompt = get_string_value ("PS1");
6864   ps2_prompt = get_string_value ("PS2");
6865
6866   if (!prompt_string_pointer)
6867     prompt_string_pointer = &ps1_prompt;
6868
6869   temp_prompt = *prompt_string_pointer
6870                         ? decode_prompt_string (*prompt_string_pointer)
6871                         : (char *)NULL;
6872
6873   if (temp_prompt == 0)
6874     {
6875       temp_prompt = (char *)xmalloc (1);
6876       temp_prompt[0] = '\0';
6877     }
6878
6879   current_prompt_string = *prompt_string_pointer;
6880   prompt_string_pointer = &ps2_prompt;
6881
6882 #if defined (READLINE)
6883   if (!no_line_editing)
6884     {
6885       FREE (current_readline_prompt);
6886       current_readline_prompt = temp_prompt;
6887     }
6888   else
6889 #endif  /* READLINE */
6890     {
6891       FREE (current_decoded_prompt);
6892       current_decoded_prompt = temp_prompt;
6893     }
6894 }
6895
6896 int
6897 get_current_prompt_level ()
6898 {
6899   return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
6900 }
6901
6902 void
6903 set_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       
6910 static void
6911 print_prompt ()
6912 {
6913   fprintf (stderr, "%s", current_decoded_prompt);
6914   fflush (stderr);
6915 }
6916
6917 /* Return a string which will be printed as a prompt.  The string
6918    may contain special characters which are decoded as follows:
6919
6920         \a      bell (ascii 07)
6921         \d      the date in Day Mon Date format
6922         \e      escape (ascii 033)
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
6928         \r      CR
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
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
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
6940         \!      the history number of this command
6941         \#      the command number of this command
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
6949 char *
6950 decode_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;
6959   int c, n, i;
6960   char *temp, octal_string[4];
6961   struct tm *tm;  
6962   time_t the_time;
6963   char timebuf[128];
6964   char *timefmt;
6965
6966   result = (char *)xmalloc (result_size = PROMPT_GROWTH);
6967   result[result_index = 0] = 0;
6968   temp = (char *)NULL;
6969
6970   while (c = *string++)
6971     {
6972       if (posixly_correct && c == '!')
6973         {
6974           if (*string == '!')
6975             {
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;
6988             }
6989         }
6990       if (c == '\\')
6991         {
6992           c = *string;
6993
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';
7006
7007               n = read_octal (octal_string);
7008               temp = (char *)xmalloc (3);
7009
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                 }
7026
7027               for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
7028                 string++;
7029
7030               c = 0;            /* tested at add_string: */
7031               goto add_string;
7032
7033             case 'd':
7034             case 't':
7035             case 'T':
7036             case '@':
7037             case 'A':
7038               /* Make the current time/date into a string. */
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
7053               if (n == 0)
7054                 timebuf[0] = '\0';
7055               else
7056                 timebuf[sizeof(timebuf) - 1] = '\0';
7057
7058               temp = savestring (timebuf);
7059               goto add_string;
7060
7061             case 'D':           /* strftime format */
7062               if (string[1] != '{')             /* } */
7063                 goto not_escape;
7064
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')
7074                 {
7075                   timefmt[0] = '%';
7076                   timefmt[1] = 'X';     /* locale-specific current time */
7077                   timefmt[2] = '\0';
7078                 }
7079               n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
7080               free (timefmt);
7081
7082               if (n == 0)
7083                 timebuf[0] = '\0';
7084               else
7085                 timebuf[sizeof(timebuf) - 1] = '\0';
7086
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);
7094               goto add_string;
7095               
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;
7102
7103             case 's':
7104               temp = base_pathname (shell_name);
7105               temp = savestring (temp);
7106               goto add_string;
7107
7108             case 'v':
7109             case 'V':
7110               temp = (char *)xmalloc (16);
7111               if (c == 'v')
7112                 strcpy (temp, dist_version);
7113               else
7114                 sprintf (temp, "%s.%d", dist_version, patch_level);
7115               goto add_string;
7116
7117             case 'w':
7118             case 'W':
7119               {
7120                 /* Use the value of PWD because it is much more efficient. */
7121                 char t_string[PATH_MAX];
7122                 int tlen;
7123
7124                 temp = get_string_value ("PWD");
7125
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';
7142
7143 #define ROOT_PATH(x)    ((x)[0] == '/' && (x)[1] == 0)
7144 #define DOUBLE_SLASH_ROOT(x)    ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
7145                 /* Abbreviate \W as ~ if $PWD == $HOME */
7146                 if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || STREQ (t, t_string) == 0))
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));
7161
7162                 temp = trim_pathname (t_string, PATH_MAX - 1);
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
7167                      second argument of Q_DOUBLE_QUOTES if we use this
7168                      function here. */
7169                   temp = sh_backslash_quote_for_double_quotes (t_string);
7170                 else
7171                   temp = savestring (t_string);
7172
7173                 goto add_string;
7174               }
7175
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;
7181
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;
7188
7189             case '#':
7190               temp = itos (current_command_number);
7191               goto add_string;
7192
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;
7200
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;
7208
7209             case 'j':
7210               temp = itos (count_all_jobs ());
7211               goto add_string;
7212
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;
7222
7223 #if defined (READLINE)
7224             case '[':
7225             case ']':
7226               if (no_line_editing)
7227                 {
7228                   string++;
7229                   break;
7230                 }
7231               temp = (char *)xmalloc (3);
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';
7238               goto add_string;
7239 #endif /* READLINE */
7240
7241             case '\\':
7242             case 'a':
7243             case 'e':
7244             case 'r':
7245               temp = (char *)xmalloc (2);
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;
7254               temp[1] = '\0';
7255               goto add_string;
7256
7257             default:
7258 not_escape:
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;
7297       list = expand_prompt_string (result, Q_DOUBLE_QUOTES, 0);
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
7315 /************************************************
7316  *                                              *
7317  *              ERROR HANDLING                  *
7318  *                                              *
7319  ************************************************/
7320
7321 /* Report a syntax error, and restart the parser.  Call here for fatal
7322    errors. */
7323 int
7324 yyerror (msg)
7325      const char *msg;
7326 {
7327   report_syntax_error ((char *)NULL);
7328   reset_parser ();
7329   return (0);
7330 }
7331
7332 static char *
7333 error_token_from_token (tok)
7334      int tok;
7335 {
7336   char *t;
7337
7338   if (t = find_token_in_alist (tok, word_token_alist, 0))
7339     return t;
7340
7341   if (t = find_token_in_alist (tok, other_token_alist, 0))
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
7372 static char *
7373 error_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
7414 static void
7415 print_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
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. */
7433 static void
7434 report_syntax_error (message)
7435      char *message;
7436 {
7437   char *msg;
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
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)))
7452     {
7453       parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
7454       free (msg);
7455
7456       if (interactive == 0)
7457         print_offending_line ();
7458
7459       last_command_exit_value = EX_USAGE;
7460       return;
7461     }
7462
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)
7470         {
7471           parser_error (line_number, _("syntax error near `%s'"), msg);
7472           free (msg);
7473         }
7474
7475       /* If not interactive, print the line containing the error. */
7476       if (interactive == 0)
7477         print_offending_line ();
7478     }
7479   else
7480     {
7481       msg = EOF_Reached ? _("syntax error: unexpected end of file") : _("syntax error");
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     }
7489
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.
7497    (dispose_command () will actually free the command.) */
7498 static void
7499 discard_parser_constructs (error_p)
7500      int error_p;
7501 {
7502 }
7503
7504 /************************************************
7505  *                                              *
7506  *              EOF HANDLING                    *
7507  *                                              *
7508  ************************************************/
7509
7510 /* Do that silly `type "bye" to exit' stuff.  You know, "ignoreeof". */
7511
7512 /* A flag denoting whether or not ignoreeof is set. */
7513 int 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. */
7518 int eof_encountered = 0;
7519
7520 /* The limit for eof_encountered. */
7521 int 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. */
7527 static void
7528 handle_eof_input_unit ()
7529 {
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             {
7543               fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
7544                        login_shell ? "logout" : "exit");
7545               eof_encountered++;
7546               /* Reset the parsing state. */
7547               last_read_token = current_token = '\n';
7548               /* Reset the prompt string to be $PS1. */
7549               prompt_string_pointer = (char **)NULL;
7550               prompt_again ();
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);
7558     }
7559   else
7560     {
7561       /* We don't write history files, etc., for non-interactive shells. */
7562       EOF_Reached = 1;
7563     }
7564 }
7565
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
7575 static 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. */
7579 WORD_LIST *
7580 parse_string_to_word_list (s, flags, whom)
7581      char *s;
7582      int flags;
7583      const char *whom;
7584 {
7585   WORD_LIST *wl;
7586   int tok, orig_current_token, orig_line_number, orig_input_terminator;
7587   int orig_line_count;
7588   int old_echo_input, old_expand_aliases;
7589 #if defined (HISTORY)
7590   int old_remember_on_history, old_history_expansion_inhibited;
7591 #endif
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 ();
7599 #endif
7600
7601   orig_line_number = line_number;
7602   orig_line_count = current_command_line_count;
7603   orig_input_terminator = shell_input_line_terminator;
7604   old_echo_input = echo_input_at_read;
7605   old_expand_aliases = expand_aliases;
7606
7607   push_stream (1);
7608   last_read_token = WORD;               /* WORD to allow reserved words here */
7609   current_command_line_count = 0;
7610   echo_input_at_read = expand_aliases = 0;
7611
7612   with_input_from_string (s, whom);
7613   wl = (WORD_LIST *)NULL;
7614
7615   if (flags & 1)
7616     parser_state |= PST_COMPASSIGN|PST_REPARSE;
7617
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;
7627           orig_current_token = current_token;
7628           current_token = tok;
7629           yyerror (NULL);       /* does the right thing */
7630           current_token = orig_current_token;
7631           if (wl)
7632             dispose_words (wl);
7633           wl = &parse_string_error;
7634           break;
7635         }
7636       wl = make_word_list (yylval.word, wl);
7637     }
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
7649   echo_input_at_read = old_echo_input;
7650   expand_aliases = old_expand_aliases;
7651
7652   current_command_line_count = orig_line_count;
7653   shell_input_line_terminator = orig_input_terminator;
7654
7655   if (flags & 1)
7656     parser_state &= ~(PST_COMPASSIGN|PST_REPARSE);
7657
7658   if (wl == &parse_string_error)
7659     {
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);
7665     }
7666
7667   return (REVERSE_LIST (wl, WORD_LIST *));
7668 }
7669
7670 static char *
7671 parse_compound_assignment (retlenp)
7672      int *retlenp;
7673 {
7674   WORD_LIST *wl, *rl;
7675   int tok, orig_line_number, orig_token_size, orig_last_token, assignok;
7676   char *saved_token, *ret;
7677
7678   saved_token = token;
7679   orig_token_size = token_buffer_size;
7680   orig_line_number = line_number;
7681   orig_last_token = last_read_token;
7682
7683   last_read_token = WORD;       /* WORD to allow reserved words here */
7684
7685   token = (char *)NULL;
7686   token_buffer_size = 0;
7687
7688   assignok = parser_state&PST_ASSIGNOK;         /* XXX */
7689
7690   wl = (WORD_LIST *)NULL;       /* ( */
7691   parser_state |= PST_COMPASSIGN;
7692
7693   while ((tok = read_token (READ)) != ')')
7694     {
7695       if (tok == '\n')                  /* Allow newlines in compound assignments */
7696         {
7697           if (SHOULD_PROMPT ())
7698             prompt_again ();
7699           continue;
7700         }
7701       if (tok != WORD && tok != ASSIGNMENT_WORD)
7702         {
7703           current_token = tok;  /* for error reporting */
7704           if (tok == yacc_EOF)  /* ( */
7705             parser_error (orig_line_number, _("unexpected EOF while looking for matching `)'"));
7706           else
7707             yyerror(NULL);      /* does the right thing */
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
7720   parser_state &= ~PST_COMPASSIGN;
7721
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
7732   last_read_token = orig_last_token;            /* XXX - was WORD? */
7733
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;
7745
7746   if (assignok)
7747     parser_state |= PST_ASSIGNOK;
7748
7749   return ret;
7750 }
7751
7752 /************************************************
7753  *                                              *
7754  *   SAVING AND RESTORING PARTIAL PARSE STATE   *
7755  *                                              *
7756  ************************************************/
7757
7758 sh_parser_state_t *
7759 save_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)
7767     ps = (sh_parser_state_t *)xmalloc (sizeof (sh_parser_state_t));
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
7804 void
7805 restore_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
7851 /************************************************
7852  *                                              *
7853  *      MULTIBYTE CHARACTER HANDLING            *
7854  *                                              *
7855  ************************************************/
7856
7857 #if defined (HANDLE_MULTIBYTE)
7858 static void
7859 set_line_mbstate ()
7860 {
7861   int i, previ, len, c;
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
7876       c = shell_input_line[i];
7877       if (c == EOF)
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         {
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;
7906         }
7907
7908       shell_input_line_property[i] = mbclen;
7909     }
7910 }
7911 #endif /* HANDLE_MULTIBYTE */
7912