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