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