]> git.ipfire.org Git - thirdparty/bash.git/blob - y.tab.c
Imported from ../bash-2.05b.tar.gz.
[thirdparty/bash.git] / y.tab.c
1 /* A Bison parser, made from /usr/homes/chet/src/bash/src/parse.y
2 by GNU bison 1.34. */
3
4 #define YYBISON 1 /* Identify Bison output. */
5
6 # define IF 257
7 # define THEN 258
8 # define ELSE 259
9 # define ELIF 260
10 # define FI 261
11 # define CASE 262
12 # define ESAC 263
13 # define FOR 264
14 # define SELECT 265
15 # define WHILE 266
16 # define UNTIL 267
17 # define DO 268
18 # define DONE 269
19 # define FUNCTION 270
20 # define COND_START 271
21 # define COND_END 272
22 # define COND_ERROR 273
23 # define IN 274
24 # define BANG 275
25 # define TIME 276
26 # define TIMEOPT 277
27 # define WORD 278
28 # define ASSIGNMENT_WORD 279
29 # define NUMBER 280
30 # define ARITH_CMD 281
31 # define ARITH_FOR_EXPRS 282
32 # define COND_CMD 283
33 # define AND_AND 284
34 # define OR_OR 285
35 # define GREATER_GREATER 286
36 # define LESS_LESS 287
37 # define LESS_AND 288
38 # define LESS_LESS_LESS 289
39 # define GREATER_AND 290
40 # define SEMI_SEMI 291
41 # define LESS_LESS_MINUS 292
42 # define AND_GREATER 293
43 # define LESS_GREATER 294
44 # define GREATER_BAR 295
45 # define yacc_EOF 296
46
47 #line 21 "/usr/homes/chet/src/bash/src/parse.y"
48
49 #include "config.h"
50
51 #include "bashtypes.h"
52 #include "bashansi.h"
53
54 #include "filecntl.h"
55
56 #if defined (HAVE_UNISTD_H)
57 # include <unistd.h>
58 #endif
59
60 #if defined (HAVE_LOCALE_H)
61 # include <locale.h>
62 #endif
63
64 #include <stdio.h>
65 #include "chartypes.h"
66 #include <signal.h>
67
68 #include "memalloc.h"
69
70 #define NEED_STRFTIME_DECL /* used in externs.h */
71
72 #include "shell.h"
73 #include "trap.h"
74 #include "flags.h"
75 #include "parser.h"
76 #include "mailcheck.h"
77 #include "test.h"
78 #include "builtins/common.h"
79 #include "builtins/builtext.h"
80
81 #include "shmbutil.h"
82
83 #if defined (READLINE)
84 # include "bashline.h"
85 # include <readline/readline.h>
86 #endif /* READLINE */
87
88 #if defined (HISTORY)
89 # include "bashhist.h"
90 # include <readline/history.h>
91 #endif /* HISTORY */
92
93 #if defined (JOB_CONTROL)
94 # include "jobs.h"
95 #endif /* JOB_CONTROL */
96
97 #if defined (ALIAS)
98 # include "alias.h"
99 #endif /* ALIAS */
100
101 #if defined (PROMPT_STRING_DECODE)
102 # ifndef _MINIX
103 # include <sys/param.h>
104 # endif
105 # include <time.h>
106 # if defined (TM_IN_SYS_TIME)
107 # include <sys/types.h>
108 # include <sys/time.h>
109 # endif /* TM_IN_SYS_TIME */
110 # include "maxpath.h"
111 #endif /* PROMPT_STRING_DECODE */
112
113 #define RE_READ_TOKEN -99
114 #define NO_EXPANSION -100
115
116 #ifdef DEBUG
117 # define YYDEBUG 1
118 #else
119 # define YYDEBUG 0
120 #endif
121
122 #if defined (HANDLE_MULTIBYTE)
123 # define last_shell_getc_is_singlebyte \
124 ((shell_input_line_index > 1) \
125 ? shell_input_line_property[shell_input_line_index - 1] \
126 : 1)
127 # define MBTEST(x) ((x) && last_shell_getc_is_singlebyte)
128 #else
129 # define last_shell_getc_is_singlebyte 1
130 # define MBTEST(x) ((x))
131 #endif
132
133 #if defined (EXTENDED_GLOB)
134 extern int extended_glob;
135 #endif
136
137 extern int eof_encountered;
138 extern int no_line_editing, running_under_emacs;
139 extern int current_command_number;
140 extern int sourcelevel;
141 extern int posixly_correct;
142 extern int last_command_exit_value;
143 extern int interrupt_immediately;
144 extern char *shell_name, *current_host_name;
145 extern char *dist_version;
146 extern int patch_level;
147 extern int dump_translatable_strings, dump_po_strings;
148 extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
149 #if defined (BUFFERED_INPUT)
150 extern int bash_input_fd_changed;
151 #endif
152
153 extern int errno;
154 /* **************************************************************** */
155 /* */
156 /* "Forward" declarations */
157 /* */
158 /* **************************************************************** */
159
160 #ifdef DEBUG
161 static void debug_parser __P((int));
162 #endif
163
164 static int yy_getc __P((void));
165 static int yy_ungetc __P((int));
166
167 #if defined (READLINE)
168 static int yy_readline_get __P((void));
169 static int yy_readline_unget __P((int));
170 #endif
171
172 static int yy_string_get __P((void));
173 static int yy_string_unget __P((int));
174 static int yy_stream_get __P((void));
175 static int yy_stream_unget __P((int));
176
177 static int shell_getc __P((int));
178 static void shell_ungetc __P((int));
179 static void discard_until __P((int));
180
181 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
182 static void push_string __P((char *, int, alias_t *));
183 static void pop_string __P((void));
184 static void free_string_list __P((void));
185 #endif
186
187 static char *read_a_line __P((int));
188
189 static int reserved_word_acceptable __P((int));
190 static int yylex __P((void));
191 static int alias_expand_token __P((char *));
192 static int time_command_acceptable __P((void));
193 static int special_case_tokens __P((char *));
194 static int read_token __P((int));
195 static char *parse_matched_pair __P((int, int, int, int *, int));
196 #if defined (ARRAY_VARS)
197 static char *parse_compound_assignment __P((int *));
198 #endif
199 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
200 static int parse_dparen __P((int));
201 static int parse_arith_cmd __P((char **));
202 #endif
203 #if defined (COND_COMMAND)
204 static void cond_error __P((void));
205 static COND_COM *cond_expr __P((void));
206 static COND_COM *cond_or __P((void));
207 static COND_COM *cond_and __P((void));
208 static COND_COM *cond_term __P((void));
209 static int cond_skip_newlines __P((void));
210 static COMMAND *parse_cond_command __P((void));
211 #endif
212 #if defined (ARRAY_VARS)
213 static int token_is_assignment __P((char *, int));
214 static int token_is_ident __P((char *, int));
215 #endif
216 static int read_token_word __P((int));
217 static void discard_parser_constructs __P((int));
218
219 static char *error_token_from_token __P((int));
220 static char *error_token_from_text __P((void));
221 static void print_offending_line __P((void));
222 static void report_syntax_error __P((char *));
223
224 static void handle_eof_input_unit __P((void));
225 static void prompt_again __P((void));
226 #if 0
227 static void reset_readline_prompt __P((void));
228 #endif
229 static void print_prompt __P((void));
230
231 #if defined (HISTORY)
232 char *history_delimiting_chars __P((void));
233 #endif
234
235 #if defined (HANDLE_MULTIBYTE)
236 static void set_line_mbstate __P((void));
237 static char *shell_input_line_property = NULL;
238 #else
239 # define set_line_mbstate()
240 #endif
241
242 extern int yyerror __P((const char *));
243
244 #ifdef DEBUG
245 extern int yydebug;
246 #endif
247
248 /* Default prompt strings */
249 char *primary_prompt = PPROMPT;
250 char *secondary_prompt = SPROMPT;
251
252 /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
253 char *ps1_prompt, *ps2_prompt;
254
255 /* Handle on the current prompt string. Indirectly points through
256 ps1_ or ps2_prompt. */
257 char **prompt_string_pointer = (char **)NULL;
258 char *current_prompt_string;
259
260 /* Non-zero means we expand aliases in commands. */
261 int expand_aliases = 0;
262
263 /* If non-zero, the decoded prompt string undergoes parameter and
264 variable substitution, command substitution, arithmetic substitution,
265 string expansion, process substitution, and quote removal in
266 decode_prompt_string. */
267 int promptvars = 1;
268
269 /* The decoded prompt string. Used if READLINE is not defined or if
270 editing is turned off. Analogous to current_readline_prompt. */
271 static char *current_decoded_prompt;
272
273 /* The number of lines read from input while creating the current command. */
274 int current_command_line_count;
275
276 /* Variables to manage the task of reading here documents, because we need to
277 defer the reading until after a complete command has been collected. */
278 static REDIRECT *redir_stack[10];
279 int need_here_doc;
280
281 /* Where shell input comes from. History expansion is performed on each
282 line when the shell is interactive. */
283 static char *shell_input_line = (char *)NULL;
284 static int shell_input_line_index;
285 static int shell_input_line_size; /* Amount allocated for shell_input_line. */
286 static int shell_input_line_len; /* strlen (shell_input_line) */
287
288 /* Either zero or EOF. */
289 static int shell_input_line_terminator;
290
291 /* The line number in a script on which a function definition starts. */
292 static int function_dstart;
293
294 /* The line number in a script on which a function body starts. */
295 static int function_bstart;
296
297 /* The line number in a script at which an arithmetic for command starts. */
298 static int arith_for_lineno;
299
300 static REDIRECTEE redir;
301
302 #line 276 "/usr/homes/chet/src/bash/src/parse.y"
303 #ifndef YYSTYPE
304 typedef union {
305 WORD_DESC *word; /* the word that we read. */
306 int number; /* the number that we read. */
307 WORD_LIST *word_list;
308 COMMAND *command;
309 REDIRECT *redirect;
310 ELEMENT element;
311 PATTERN_LIST *pattern;
312 } yystype;
313 # define YYSTYPE yystype
314 #endif
315 #ifndef YYDEBUG
316 # define YYDEBUG 0
317 #endif
318
319
320
321 #define YYFINAL 301
322 #define YYFLAG -32768
323 #define YYNTBASE 54
324
325 /* YYTRANSLATE(YYLEX) -- Bison token number corresponding to YYLEX. */
326 #define YYTRANSLATE(x) ((unsigned)(x) <= 296 ? yytranslate[x] : 90)
327
328 /* YYTRANSLATE[YYLEX] -- Bison token number corresponding to YYLEX. */
329 static const char yytranslate[] =
330 {
331 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
332 44, 2, 2, 2, 2, 2, 2, 2, 2, 2,
333 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
334 2, 2, 2, 2, 2, 2, 2, 2, 42, 2,
335 52, 53, 2, 2, 2, 49, 2, 2, 2, 2,
336 2, 2, 2, 2, 2, 2, 2, 2, 2, 43,
337 48, 2, 47, 2, 2, 2, 2, 2, 2, 2,
338 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
339 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
340 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
341 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
342 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
343 2, 2, 2, 50, 46, 51, 2, 2, 2, 2,
344 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
345 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
346 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
347 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
348 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
349 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
350 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
351 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
352 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
353 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
354 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
355 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
356 2, 2, 2, 2, 2, 2, 1, 3, 4, 5,
357 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
358 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
359 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
360 36, 37, 38, 39, 40, 41, 45
361 };
362
363 #if YYDEBUG
364 static const short yyprhs[] =
365 {
366 0, 0, 3, 5, 8, 10, 12, 15, 18, 21,
367 25, 29, 32, 36, 39, 43, 46, 50, 53, 57,
368 60, 64, 67, 71, 74, 78, 81, 85, 88, 92,
369 95, 99, 102, 106, 109, 112, 116, 118, 120, 122,
370 124, 127, 129, 132, 134, 136, 139, 141, 143, 145,
371 151, 157, 159, 161, 163, 165, 167, 169, 171, 178,
372 185, 193, 201, 212, 223, 233, 243, 251, 259, 265,
373 271, 278, 285, 293, 301, 312, 323, 330, 338, 345,
374 351, 358, 363, 365, 368, 372, 378, 386, 393, 397,
375 399, 403, 408, 415, 421, 423, 426, 431, 436, 442,
376 448, 451, 455, 457, 461, 464, 466, 469, 473, 477,
377 481, 486, 491, 496, 501, 506, 508, 510, 512, 514,
378 516, 518, 519, 522, 524, 527, 530, 535, 540, 544,
379 548, 550, 552, 555, 558, 562, 566, 571, 573, 575
380 };
381 static const short yyrhs[] =
382 {
383 85, 82, 0, 44, 0, 1, 44, 0, 45, 0,
384 24, 0, 55, 24, 0, 47, 24, 0, 48, 24,
385 0, 26, 47, 24, 0, 26, 48, 24, 0, 32,
386 24, 0, 26, 32, 24, 0, 33, 24, 0, 26,
387 33, 24, 0, 35, 24, 0, 26, 35, 24, 0,
388 34, 26, 0, 26, 34, 26, 0, 36, 26, 0,
389 26, 36, 26, 0, 34, 24, 0, 26, 34, 24,
390 0, 36, 24, 0, 26, 36, 24, 0, 38, 24,
391 0, 26, 38, 24, 0, 36, 49, 0, 26, 36,
392 49, 0, 34, 49, 0, 26, 34, 49, 0, 39,
393 24, 0, 26, 40, 24, 0, 40, 24, 0, 41,
394 24, 0, 26, 41, 24, 0, 24, 0, 25, 0,
395 56, 0, 56, 0, 58, 56, 0, 57, 0, 59,
396 57, 0, 59, 0, 61, 0, 61, 58, 0, 66,
397 0, 62, 0, 65, 0, 12, 79, 14, 79, 15,
398 0, 13, 79, 14, 79, 15, 0, 64, 0, 69,
399 0, 68, 0, 70, 0, 71, 0, 72, 0, 63,
400 0, 10, 24, 84, 14, 79, 15, 0, 10, 24,
401 84, 50, 79, 51, 0, 10, 24, 43, 84, 14,
402 79, 15, 0, 10, 24, 43, 84, 50, 79, 51,
403 0, 10, 24, 84, 20, 55, 83, 84, 14, 79,
404 15, 0, 10, 24, 84, 20, 55, 83, 84, 50,
405 79, 51, 0, 10, 24, 84, 20, 83, 84, 14,
406 79, 15, 0, 10, 24, 84, 20, 83, 84, 50,
407 79, 51, 0, 10, 28, 83, 84, 14, 79, 15,
408 0, 10, 28, 83, 84, 50, 79, 51, 0, 10,
409 28, 14, 79, 15, 0, 10, 28, 50, 79, 51,
410 0, 11, 24, 84, 14, 78, 15, 0, 11, 24,
411 84, 50, 78, 51, 0, 11, 24, 43, 84, 14,
412 78, 15, 0, 11, 24, 43, 84, 50, 78, 51,
413 0, 11, 24, 84, 20, 55, 83, 84, 14, 78,
414 15, 0, 11, 24, 84, 20, 55, 83, 84, 50,
415 78, 51, 0, 8, 24, 84, 20, 84, 9, 0,
416 8, 24, 84, 20, 76, 84, 9, 0, 8, 24,
417 84, 20, 74, 9, 0, 24, 52, 53, 84, 67,
418 0, 16, 24, 52, 53, 84, 67, 0, 16, 24,
419 84, 67, 0, 61, 0, 61, 58, 0, 52, 79,
420 53, 0, 3, 79, 4, 79, 7, 0, 3, 79,
421 4, 79, 5, 79, 7, 0, 3, 79, 4, 79,
422 73, 7, 0, 50, 79, 51, 0, 27, 0, 17,
423 29, 18, 0, 6, 79, 4, 79, 0, 6, 79,
424 4, 79, 5, 79, 0, 6, 79, 4, 79, 73,
425 0, 75, 0, 76, 75, 0, 84, 77, 53, 79,
426 0, 84, 77, 53, 84, 0, 84, 52, 77, 53,
427 79, 0, 84, 52, 77, 53, 84, 0, 75, 37,
428 0, 76, 75, 37, 0, 24, 0, 77, 46, 24,
429 0, 84, 80, 0, 78, 0, 84, 81, 0, 81,
430 44, 84, 0, 81, 42, 84, 0, 81, 43, 84,
431 0, 81, 30, 84, 81, 0, 81, 31, 84, 81,
432 0, 81, 42, 84, 81, 0, 81, 43, 84, 81,
433 0, 81, 44, 84, 81, 0, 87, 0, 44, 0,
434 45, 0, 44, 0, 43, 0, 45, 0, 0, 84,
435 44, 0, 86, 0, 86, 42, 0, 86, 43, 0,
436 86, 30, 84, 86, 0, 86, 31, 84, 86, 0,
437 86, 42, 86, 0, 86, 43, 86, 0, 87, 0,
438 88, 0, 21, 88, 0, 89, 88, 0, 89, 21,
439 88, 0, 21, 89, 88, 0, 88, 46, 84, 88,
440 0, 60, 0, 22, 0, 22, 23, 0
441 };
442
443 #endif
444
445 #if YYDEBUG
446 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
447 static const short yyrline[] =
448 {
449 0, 326, 335, 342, 357, 367, 369, 373, 378, 383,
450 388, 393, 398, 403, 409, 415, 420, 425, 430, 435,
451 440, 445, 450, 455, 460, 465, 472, 479, 484, 489,
452 494, 499, 504, 509, 514, 519, 526, 528, 530, 534,
453 538, 549, 551, 555, 557, 559, 575, 579, 581, 583,
454 585, 587, 589, 591, 593, 595, 597, 599, 603, 605,
455 607, 609, 611, 613, 615, 617, 621, 623, 625, 627,
456 631, 635, 639, 643, 647, 651, 657, 659, 661, 665,
457 668, 671, 676, 678, 709, 716, 718, 720, 725, 729,
458 733, 737, 739, 741, 745, 746, 750, 752, 754, 756,
459 760, 761, 765, 767, 776, 784, 785, 791, 792, 799,
460 803, 805, 807, 814, 816, 818, 822, 823, 826, 827,
461 828, 831, 832, 841, 847, 856, 864, 866, 868, 875,
462 878, 882, 884, 889, 894, 899, 906, 909, 913, 915
463 };
464 #endif
465
466
467 #if (YYDEBUG) || defined YYERROR_VERBOSE
468
469 /* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */
470 static const char *const yytname[] =
471 {
472 "$", "error", "$undefined.", "IF", "THEN", "ELSE", "ELIF", "FI", "CASE",
473 "ESAC", "FOR", "SELECT", "WHILE", "UNTIL", "DO", "DONE", "FUNCTION",
474 "COND_START", "COND_END", "COND_ERROR", "IN", "BANG", "TIME", "TIMEOPT",
475 "WORD", "ASSIGNMENT_WORD", "NUMBER", "ARITH_CMD", "ARITH_FOR_EXPRS",
476 "COND_CMD", "AND_AND", "OR_OR", "GREATER_GREATER", "LESS_LESS",
477 "LESS_AND", "LESS_LESS_LESS", "GREATER_AND", "SEMI_SEMI",
478 "LESS_LESS_MINUS", "AND_GREATER", "LESS_GREATER", "GREATER_BAR", "'&'",
479 "';'", "'\\n'", "yacc_EOF", "'|'", "'>'", "'<'", "'-'", "'{'", "'}'",
480 "'('", "')'", "inputunit", "word_list", "redirection",
481 "simple_command_element", "redirection_list", "simple_command",
482 "command", "shell_command", "for_command", "arith_for_command",
483 "select_command", "case_command", "function_def", "function_body",
484 "subshell", "if_command", "group_command", "arith_command",
485 "cond_command", "elif_clause", "case_clause", "pattern_list",
486 "case_clause_sequence", "pattern", "list", "compound_list", "list0",
487 "list1", "simple_list_terminator", "list_terminator", "newline_list",
488 "simple_list", "simple_list1", "pipeline_command", "pipeline",
489 "timespec", 0
490 };
491 #endif
492
493 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
494 static const short yyr1[] =
495 {
496 0, 54, 54, 54, 54, 55, 55, 56, 56, 56,
497 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
498 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
499 56, 56, 56, 56, 56, 56, 57, 57, 57, 58,
500 58, 59, 59, 60, 60, 60, 60, 61, 61, 61,
501 61, 61, 61, 61, 61, 61, 61, 61, 62, 62,
502 62, 62, 62, 62, 62, 62, 63, 63, 63, 63,
503 64, 64, 64, 64, 64, 64, 65, 65, 65, 66,
504 66, 66, 67, 67, 68, 69, 69, 69, 70, 71,
505 72, 73, 73, 73, 74, 74, 75, 75, 75, 75,
506 76, 76, 77, 77, 78, 79, 79, 80, 80, 80,
507 81, 81, 81, 81, 81, 81, 82, 82, 83, 83,
508 83, 84, 84, 85, 85, 85, 86, 86, 86, 86,
509 86, 87, 87, 87, 87, 87, 88, 88, 89, 89
510 };
511
512 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
513 static const short yyr2[] =
514 {
515 0, 2, 1, 2, 1, 1, 2, 2, 2, 3,
516 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,
517 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,
518 3, 2, 3, 2, 2, 3, 1, 1, 1, 1,
519 2, 1, 2, 1, 1, 2, 1, 1, 1, 5,
520 5, 1, 1, 1, 1, 1, 1, 1, 6, 6,
521 7, 7, 10, 10, 9, 9, 7, 7, 5, 5,
522 6, 6, 7, 7, 10, 10, 6, 7, 6, 5,
523 6, 4, 1, 2, 3, 5, 7, 6, 3, 1,
524 3, 4, 6, 5, 1, 2, 4, 4, 5, 5,
525 2, 3, 1, 3, 2, 1, 2, 3, 3, 3,
526 4, 4, 4, 4, 4, 1, 1, 1, 1, 1,
527 1, 0, 2, 1, 2, 2, 4, 4, 3, 3,
528 1, 1, 2, 2, 3, 3, 4, 1, 1, 2
529 };
530
531 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
532 doesn't specify something else to do. Zero means the default is an
533 error. */
534 static const short yydefact[] =
535 {
536 0, 0, 121, 0, 0, 0, 121, 121, 0, 0,
537 0, 138, 36, 37, 0, 89, 0, 0, 0, 0,
538 0, 0, 0, 0, 0, 2, 4, 0, 0, 121,
539 121, 38, 41, 43, 137, 44, 47, 57, 51, 48,
540 46, 53, 52, 54, 55, 56, 0, 123, 130, 131,
541 0, 3, 105, 0, 0, 121, 121, 0, 121, 0,
542 0, 121, 0, 132, 0, 139, 0, 0, 0, 0,
543 0, 0, 0, 0, 0, 0, 0, 11, 13, 21,
544 17, 29, 15, 23, 19, 27, 25, 31, 33, 34,
545 7, 8, 0, 0, 36, 42, 39, 45, 116, 117,
546 1, 121, 121, 124, 125, 121, 0, 133, 121, 122,
547 104, 106, 115, 0, 121, 0, 121, 119, 118, 120,
548 121, 121, 121, 0, 121, 121, 0, 0, 90, 135,
549 121, 12, 14, 22, 18, 30, 16, 24, 20, 28,
550 26, 32, 35, 9, 10, 88, 84, 40, 0, 0,
551 128, 129, 0, 134, 0, 121, 121, 121, 121, 121,
552 121, 0, 121, 0, 121, 0, 0, 0, 0, 121,
553 0, 121, 0, 0, 121, 82, 81, 0, 126, 127,
554 0, 0, 136, 121, 121, 85, 0, 0, 0, 108,
555 109, 107, 0, 94, 121, 0, 121, 121, 0, 5,
556 0, 121, 0, 68, 69, 121, 121, 121, 121, 0,
557 0, 0, 0, 49, 50, 0, 83, 79, 0, 0,
558 87, 110, 111, 112, 113, 114, 78, 100, 95, 0,
559 76, 102, 0, 0, 0, 0, 58, 6, 121, 0,
560 59, 0, 0, 0, 0, 70, 0, 121, 71, 80,
561 86, 121, 121, 121, 121, 101, 77, 0, 0, 121,
562 60, 61, 0, 121, 121, 66, 67, 72, 73, 0,
563 91, 0, 0, 0, 121, 103, 96, 97, 121, 121,
564 0, 0, 121, 121, 121, 93, 98, 99, 0, 0,
565 64, 65, 0, 0, 92, 62, 63, 74, 75, 0,
566 0, 0
567 };
568
569 static const short yydefgoto[] =
570 {
571 299, 200, 31, 32, 97, 33, 34, 35, 36, 37,
572 38, 39, 40, 176, 41, 42, 43, 44, 45, 186,
573 192, 193, 194, 233, 52, 53, 110, 111, 100, 121,
574 54, 46, 150, 112, 49, 50
575 };
576
577 static const short yypact[] =
578 {
579 273, -24,-32768, -2, 11, 6,-32768,-32768, 12, 9,
580 402, 41, 10,-32768, 552,-32768, 46, 52, -5, 58,
581 64, 68, 102, 117, 135,-32768,-32768, 146, 149,-32768,
582 -32768,-32768,-32768, 169,-32768, 202,-32768,-32768,-32768,-32768,
583 -32768,-32768,-32768,-32768,-32768,-32768, -33, 42,-32768, 91,
584 445,-32768,-32768, 142, 316,-32768, 133, 72, 136, 172,
585 174, 97, 171, 91, 531,-32768, 139, 173, 182, 99,
586 188, 138, 189, 190, 191, 194, 195,-32768,-32768,-32768,
587 -32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,
588 -32768,-32768, 145, 168,-32768,-32768,-32768, 202,-32768,-32768,
589 -32768,-32768,-32768, 359, 359,-32768, 531, 91,-32768,-32768,
590 -32768, 249,-32768, -12,-32768, 13,-32768,-32768,-32768,-32768,
591 -32768,-32768,-32768, 57,-32768,-32768, 170, 39,-32768, 91,
592 -32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,
593 -32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768, 316, 316,
594 28, 28, 488, 91, 74,-32768,-32768,-32768,-32768,-32768,
595 -32768, 90,-32768, 220,-32768, 183, 178, 110, 113,-32768,
596 198,-32768, 209, 215,-32768, 202,-32768, 39,-32768,-32768,
597 359, 359, 91,-32768,-32768,-32768, 224, 316, 316, 316,
598 316, 316, 230, 196,-32768, 16,-32768,-32768, 231,-32768,
599 258,-32768, 197,-32768,-32768,-32768,-32768,-32768,-32768, 236,
600 316, 258, 201,-32768,-32768, 39, 202,-32768, 247, 255,
601 -32768,-32768,-32768, 66, 66, 66,-32768,-32768, 223, 17,
602 -32768,-32768, 237, 92, 251, 211,-32768,-32768,-32768, 121,
603 -32768, 252, 218, 256, 219,-32768, 249,-32768,-32768,-32768,
604 -32768,-32768,-32768,-32768,-32768,-32768,-32768, 115, 253,-32768,
605 -32768,-32768, 122,-32768,-32768,-32768,-32768,-32768,-32768, 125,
606 106, 316, 316, 316,-32768,-32768,-32768, 316,-32768,-32768,
607 260, 245,-32768,-32768,-32768,-32768,-32768, 316, 272, 259,
608 -32768,-32768, 289, 264,-32768,-32768,-32768,-32768,-32768, 288,
609 322,-32768
610 };
611
612 static const short yypgoto[] =
613 {
614 -32768, 160, -32, 283, 156,-32768,-32768, -122,-32768,-32768,
615 -32768,-32768,-32768, -167,-32768,-32768,-32768,-32768,-32768, 65,
616 -32768, 140,-32768, 104, -162, -6,-32768, -173,-32768, -157,
617 -27,-32768, 4, 2, 3, 329
618 };
619
620
621 #define YYLAST 600
622
623
624 static const short yytable[] =
625 {
626 59, 60, 48, 96, 47, 175, 201, 209, 160, 212,
627 217, 98, 99, 63, 221, 222, 223, 224, 225, 79,
628 51, 80, 55, 92, 93, 230, 256, 162, 113, 115,
629 58, 123, 109, 163, 127, 56, 61, 246, 62, 57,
630 231, 231, 2, 238, 81, 243, 244, 3, 249, 4,
631 5, 6, 7, 107, 247, 175, 9, 109, 101, 102,
632 109, 109, 66, 164, 65, 147, 15, 129, 232, 232,
633 77, 169, 101, 102, 148, 149, 78, 170, 152, 183,
634 184, 185, 82, 109, 103, 104, 116, 161, 83, 29,
635 84, 30, 86, 175, 167, 168, 155, 156, 223, 224,
636 225, 109, 154, 177, 196, 48, 48, 171, 151, 153,
637 165, 284, 184, 85, 166, 117, 118, 119, 172, 173,
638 292, 293, 120, 133, 205, 134, 87, 207, 187, 188,
639 189, 190, 191, 195, 109, 263, 278, 105, 258, 282,
640 197, 88, 210, 96, 210, 259, 108, 215, 135, 126,
641 48, 48, 178, 179, 109, 182, 198, 109, 202, 89,
642 206, 258, 137, 208, 138, 109, 109, 229, 274, 109,
643 90, 264, 279, 91, 239, 283, 114, 218, 219, 122,
644 210, 210, 48, 48, 147, 151, 124, 139, 125, 128,
645 234, 235, 130, 94, 13, 14, 145, 131, 203, 241,
646 242, 16, 17, 18, 19, 20, 132, 21, 22, 23,
647 24, 262, 136, 140, 141, 142, 27, 28, 143, 144,
648 269, 146, 199, 174, 213, 271, 272, 273, 14, 204,
649 214, 220, 277, 227, 16, 17, 18, 19, 20, 226,
650 21, 22, 23, 24, 199, 270, 236, 287, 240, 27,
651 28, 245, 248, 276, 250, 210, 210, 280, 281, 251,
652 255, 231, 261, 117, 118, 119, 260, 265, 286, 266,
653 268, 267, 288, 289, 1, 290, 2, 275, 294, 155,
654 156, 3, 237, 4, 5, 6, 7, 295, 300, 8,
655 9, 157, 158, 159, 10, 11, 291, 12, 13, 14,
656 15, 117, 118, 119, 297, 16, 17, 18, 19, 20,
657 296, 21, 22, 23, 24, 298, 95, 25, 26, 2,
658 27, 28, 301, 29, 3, 30, 4, 5, 6, 7,
659 211, 216, 8, 9, 228, 285, 257, 10, 11, 64,
660 12, 13, 14, 15, 0, 0, 0, 0, 16, 17,
661 18, 19, 20, 0, 21, 22, 23, 24, 0, 0,
662 109, 0, 2, 27, 28, 0, 29, 3, 30, 4,
663 5, 6, 7, 0, 0, 8, 9, 0, 0, 0,
664 10, 11, 0, 12, 13, 14, 15, 0, 0, 0,
665 0, 16, 17, 18, 19, 20, 0, 21, 22, 23,
666 24, 0, 0, 0, 0, 2, 27, 28, 0, 29,
667 3, 30, 4, 5, 6, 7, 0, 0, 8, 9,
668 0, 0, 0, 0, 11, 0, 12, 13, 14, 15,
669 0, 0, 0, 0, 16, 17, 18, 19, 20, 0,
670 21, 22, 23, 24, 0, 0, 0, 0, 2, 27,
671 28, 0, 29, 3, 30, 4, 5, 6, 7, 0,
672 0, 8, 9, 0, 0, 0, 106, 0, 0, 12,
673 13, 14, 15, 0, 0, 0, 0, 16, 17, 18,
674 19, 20, 0, 21, 22, 23, 24, 0, 0, 0,
675 0, 2, 27, 28, 0, 29, 3, 30, 4, 5,
676 6, 7, 0, 0, 8, 9, 0, 0, 0, 0,
677 0, 0, 12, 13, 14, 15, 0, 0, 0, 0,
678 16, 17, 18, 19, 20, 0, 21, 22, 23, 24,
679 0, 0, 109, 0, 2, 27, 28, 0, 29, 3,
680 30, 4, 5, 6, 7, 0, 0, 8, 9, 0,
681 0, 0, 0, 0, 0, 12, 13, 14, 15, 0,
682 0, 0, 0, 16, 17, 18, 19, 20, 0, 21,
683 22, 23, 24, 0, 0, 0, 0, 0, 27, 28,
684 0, 29, 0, 30, 67, 68, 69, 70, 71, 0,
685 72, 0, 73, 74, 0, 0, 0, 0, 0, 75,
686 76
687 };
688
689 static const short yycheck[] =
690 {
691 6, 7, 0, 35, 0, 127, 163, 169, 20, 171,
692 177, 44, 45, 10, 187, 188, 189, 190, 191, 24,
693 44, 26, 24, 29, 30, 9, 9, 14, 55, 56,
694 24, 58, 44, 20, 61, 24, 24, 210, 29, 28,
695 24, 24, 3, 200, 49, 207, 208, 8, 215, 10,
696 11, 12, 13, 50, 211, 177, 17, 44, 30, 31,
697 44, 44, 52, 50, 23, 97, 27, 64, 52, 52,
698 24, 14, 30, 31, 101, 102, 24, 20, 105, 5,
699 6, 7, 24, 44, 42, 43, 14, 114, 24, 50,
700 26, 52, 24, 215, 121, 122, 30, 31, 271, 272,
701 273, 44, 108, 130, 14, 103, 104, 50, 104, 106,
702 116, 5, 6, 49, 120, 43, 44, 45, 124, 125,
703 282, 283, 50, 24, 14, 26, 24, 14, 155, 156,
704 157, 158, 159, 160, 44, 14, 14, 46, 46, 14,
705 50, 24, 169, 175, 171, 53, 4, 174, 49, 52,
706 148, 149, 148, 149, 44, 152, 162, 44, 164, 24,
707 50, 46, 24, 50, 26, 44, 44, 194, 53, 44,
708 24, 50, 50, 24, 201, 50, 43, 183, 184, 43,
709 207, 208, 180, 181, 216, 181, 14, 49, 14, 18,
710 196, 197, 53, 24, 25, 26, 51, 24, 15, 205,
711 206, 32, 33, 34, 35, 36, 24, 38, 39, 40,
712 41, 238, 24, 24, 24, 24, 47, 48, 24, 24,
713 247, 53, 24, 53, 15, 252, 253, 254, 26, 51,
714 15, 7, 259, 37, 32, 33, 34, 35, 36, 9,
715 38, 39, 40, 41, 24, 251, 15, 274, 51, 47,
716 48, 15, 51, 259, 7, 282, 283, 263, 264, 4,
717 37, 24, 51, 43, 44, 45, 15, 15, 274, 51,
718 51, 15, 278, 279, 1, 15, 3, 24, 284, 30,
719 31, 8, 24, 10, 11, 12, 13, 15, 0, 16,
720 17, 42, 43, 44, 21, 22, 51, 24, 25, 26,
721 27, 43, 44, 45, 15, 32, 33, 34, 35, 36,
722 51, 38, 39, 40, 41, 51, 33, 44, 45, 3,
723 47, 48, 0, 50, 8, 52, 10, 11, 12, 13,
724 170, 175, 16, 17, 194, 270, 232, 21, 22, 10,
725 24, 25, 26, 27, -1, -1, -1, -1, 32, 33,
726 34, 35, 36, -1, 38, 39, 40, 41, -1, -1,
727 44, -1, 3, 47, 48, -1, 50, 8, 52, 10,
728 11, 12, 13, -1, -1, 16, 17, -1, -1, -1,
729 21, 22, -1, 24, 25, 26, 27, -1, -1, -1,
730 -1, 32, 33, 34, 35, 36, -1, 38, 39, 40,
731 41, -1, -1, -1, -1, 3, 47, 48, -1, 50,
732 8, 52, 10, 11, 12, 13, -1, -1, 16, 17,
733 -1, -1, -1, -1, 22, -1, 24, 25, 26, 27,
734 -1, -1, -1, -1, 32, 33, 34, 35, 36, -1,
735 38, 39, 40, 41, -1, -1, -1, -1, 3, 47,
736 48, -1, 50, 8, 52, 10, 11, 12, 13, -1,
737 -1, 16, 17, -1, -1, -1, 21, -1, -1, 24,
738 25, 26, 27, -1, -1, -1, -1, 32, 33, 34,
739 35, 36, -1, 38, 39, 40, 41, -1, -1, -1,
740 -1, 3, 47, 48, -1, 50, 8, 52, 10, 11,
741 12, 13, -1, -1, 16, 17, -1, -1, -1, -1,
742 -1, -1, 24, 25, 26, 27, -1, -1, -1, -1,
743 32, 33, 34, 35, 36, -1, 38, 39, 40, 41,
744 -1, -1, 44, -1, 3, 47, 48, -1, 50, 8,
745 52, 10, 11, 12, 13, -1, -1, 16, 17, -1,
746 -1, -1, -1, -1, -1, 24, 25, 26, 27, -1,
747 -1, -1, -1, 32, 33, 34, 35, 36, -1, 38,
748 39, 40, 41, -1, -1, -1, -1, -1, 47, 48,
749 -1, 50, -1, 52, 32, 33, 34, 35, 36, -1,
750 38, -1, 40, 41, -1, -1, -1, -1, -1, 47,
751 48
752 };
753 /* -*-C-*- Note some compilers choke on comments on `#line' lines. */
754 #line 3 "/usr/local/share/bison/bison.simple"
755
756 /* Skeleton output parser for bison,
757
758 Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002 Free Software
759 Foundation, Inc.
760
761 This program is free software; you can redistribute it and/or modify
762 it under the terms of the GNU General Public License as published by
763 the Free Software Foundation; either version 2, or (at your option)
764 any later version.
765
766 This program is distributed in the hope that it will be useful,
767 but WITHOUT ANY WARRANTY; without even the implied warranty of
768 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
769 GNU General Public License for more details.
770
771 You should have received a copy of the GNU General Public License
772 along with this program; if not, write to the Free Software
773 Foundation, Inc., 59 Temple Place - Suite 330,
774 Boston, MA 02111-1307, USA. */
775
776 /* As a special exception, when this file is copied by Bison into a
777 Bison output file, you may use that output file without restriction.
778 This special exception was added by the Free Software Foundation
779 in version 1.24 of Bison. */
780
781 /* This is the parser code that is written into each bison parser when
782 the %semantic_parser declaration is not specified in the grammar.
783 It was written by Richard Stallman by simplifying the hairy parser
784 used when %semantic_parser is specified. */
785
786 /* All symbols defined below should begin with yy or YY, to avoid
787 infringing on user name space. This should be done even for local
788 variables, as they might otherwise be expanded by user macros.
789 There are some unavoidable exceptions within include files to
790 define necessary library symbols; they are noted "INFRINGES ON
791 USER NAME SPACE" below. */
792
793 #if ! defined (yyoverflow) || defined (YYERROR_VERBOSE)
794
795 /* The parser invokes alloca or malloc; define the necessary symbols. */
796
797 # if YYSTACK_USE_ALLOCA
798 # define YYSTACK_ALLOC alloca
799 # else
800 # ifndef YYSTACK_USE_ALLOCA
801 # if defined (alloca) || defined (_ALLOCA_H)
802 # define YYSTACK_ALLOC alloca
803 # else
804 # ifdef __GNUC__
805 # define YYSTACK_ALLOC __builtin_alloca
806 # endif
807 # endif
808 # endif
809 # endif
810
811 # ifdef YYSTACK_ALLOC
812 /* Pacify GCC's `empty if-body' warning. */
813 # define YYSTACK_FREE(Ptr) do { /* empty */; } while (0)
814 # else
815 # if defined (__STDC__) || defined (__cplusplus)
816 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
817 # define YYSIZE_T size_t
818 # endif
819 # define YYSTACK_ALLOC malloc
820 # define YYSTACK_FREE free
821 # endif
822
823 /* A type that is properly aligned for any stack member. */
824 union yyalloc
825 {
826 short yyss;
827 YYSTYPE yyvs;
828 # if YYLSP_NEEDED
829 YYLTYPE yyls;
830 # endif
831 };
832
833 /* The size of the maximum gap between one aligned stack and the next. */
834 # define YYSTACK_GAP_MAX (sizeof (union yyalloc) - 1)
835
836 /* The size of an array large to enough to hold all stacks, each with
837 N elements. */
838 # if YYLSP_NEEDED
839 # define YYSTACK_BYTES(N) \
840 ((N) * (sizeof (short) + sizeof (YYSTYPE) + sizeof (YYLTYPE)) \
841 + 2 * YYSTACK_GAP_MAX)
842 # else
843 # define YYSTACK_BYTES(N) \
844 ((N) * (sizeof (short) + sizeof (YYSTYPE)) \
845 + YYSTACK_GAP_MAX)
846 # endif
847
848 /* Relocate the TYPE STACK from its old location to the new one. The
849 local variables YYSIZE and YYSTACKSIZE give the old and new number of
850 elements in the stack, and YYPTR gives the new location of the
851 stack. Advance YYPTR to a properly aligned location for the next
852 stack. */
853 # define YYSTACK_RELOCATE(Type, Stack) \
854 do \
855 { \
856 YYSIZE_T yynewbytes; \
857 yymemcpy ((char *) yyptr, (char *) (Stack), \
858 yysize * (YYSIZE_T) sizeof (Type)); \
859 Stack = &yyptr->Stack; \
860 yynewbytes = yystacksize * sizeof (Type) + YYSTACK_GAP_MAX; \
861 yyptr += yynewbytes / sizeof (*yyptr); \
862 } \
863 while (0)
864
865 #endif /* ! defined (yyoverflow) || defined (YYERROR_VERBOSE) */
866
867
868 #if ! defined (YYSIZE_T) && defined (__SIZE_TYPE__)
869 # define YYSIZE_T __SIZE_TYPE__
870 #endif
871 #if ! defined (YYSIZE_T) && defined (size_t)
872 # define YYSIZE_T size_t
873 #endif
874 #if ! defined (YYSIZE_T)
875 # if defined (__STDC__) || defined (__cplusplus)
876 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
877 # define YYSIZE_T size_t
878 # endif
879 #endif
880 #if ! defined (YYSIZE_T)
881 # define YYSIZE_T unsigned int
882 #endif
883
884 #define yyerrok (yyerrstatus = 0)
885 #define yyclearin (yychar = YYEMPTY)
886 #define YYEMPTY -2
887 #define YYEOF 0
888 #define YYACCEPT goto yyacceptlab
889 #define YYABORT goto yyabortlab
890 #define YYERROR goto yyerrlab1
891 /* Like YYERROR except do call yyerror. This remains here temporarily
892 to ease the transition to the new meaning of YYERROR, for GCC.
893 Once GCC version 2 has supplanted version 1, this can go. */
894 #define YYFAIL goto yyerrlab
895 #define YYRECOVERING() (!!yyerrstatus)
896 #define YYBACKUP(Token, Value) \
897 do \
898 if (yychar == YYEMPTY && yylen == 1) \
899 { \
900 yychar = (Token); \
901 yylval = (Value); \
902 yychar1 = YYTRANSLATE (yychar); \
903 YYPOPSTACK; \
904 goto yybackup; \
905 } \
906 else \
907 { \
908 yyerror ("syntax error: cannot back up"); \
909 YYERROR; \
910 } \
911 while (0)
912
913 #define YYTERROR 1
914 #define YYERRCODE 256
915
916
917 /* YYLLOC_DEFAULT -- Compute the default location (before the actions
918 are run).
919
920 When YYLLOC_DEFAULT is run, CURRENT is set the location of the
921 first token. By default, to implement support for ranges, extend
922 its range to the last symbol. */
923
924 #ifndef YYLLOC_DEFAULT
925 # define YYLLOC_DEFAULT(Current, Rhs, N) \
926 Current.last_line = Rhs[N].last_line; \
927 Current.last_column = Rhs[N].last_column;
928 #endif
929
930
931 /* YYLEX -- calling `yylex' with the right arguments. */
932
933 #if YYPURE
934 # if YYLSP_NEEDED
935 # ifdef YYLEX_PARAM
936 # define YYLEX yylex (&yylval, &yylloc, YYLEX_PARAM)
937 # else
938 # define YYLEX yylex (&yylval, &yylloc)
939 # endif
940 # else /* !YYLSP_NEEDED */
941 # ifdef YYLEX_PARAM
942 # define YYLEX yylex (&yylval, YYLEX_PARAM)
943 # else
944 # define YYLEX yylex (&yylval)
945 # endif
946 # endif /* !YYLSP_NEEDED */
947 #else /* !YYPURE */
948 # define YYLEX yylex ()
949 #endif /* !YYPURE */
950
951
952 /* Enable debugging if requested. */
953 #if YYDEBUG
954
955 # ifndef YYFPRINTF
956 # include <stdio.h> /* INFRINGES ON USER NAME SPACE */
957 # define YYFPRINTF fprintf
958 # endif
959
960 # define YYDPRINTF(Args) \
961 do { \
962 if (yydebug) \
963 YYFPRINTF Args; \
964 } while (0)
965 /* Nonzero means print parse trace. It is left uninitialized so that
966 multiple parsers can coexist. */
967 int yydebug;
968 #else /* !YYDEBUG */
969 # define YYDPRINTF(Args)
970 #endif /* !YYDEBUG */
971
972 /* YYINITDEPTH -- initial size of the parser's stacks. */
973 #ifndef YYINITDEPTH
974 # define YYINITDEPTH 200
975 #endif
976
977 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
978 if the built-in stack extension method is used).
979
980 Do not make this value too large; the results are undefined if
981 SIZE_MAX < YYSTACK_BYTES (YYMAXDEPTH)
982 evaluated with infinite-precision integer arithmetic. */
983
984 #if YYMAXDEPTH == 0
985 # undef YYMAXDEPTH
986 #endif
987
988 #ifndef YYMAXDEPTH
989 # define YYMAXDEPTH 10000
990 #endif
991 \f
992 #if ! defined (yyoverflow) && ! defined (yymemcpy)
993 # if __GNUC__ > 1 /* GNU C and GNU C++ define this. */
994 # define yymemcpy __builtin_memcpy
995 # else /* not GNU C or C++ */
996
997 /* This is the most reliable way to avoid incompatibilities
998 in available built-in functions on various systems. */
999 static void
1000 # if defined (__STDC__) || defined (__cplusplus)
1001 yymemcpy (char *yyto, const char *yyfrom, YYSIZE_T yycount)
1002 # else
1003 yymemcpy (yyto, yyfrom, yycount)
1004 char *yyto;
1005 const char *yyfrom;
1006 YYSIZE_T yycount;
1007 # endif
1008 {
1009 register const char *yyf = yyfrom;
1010 register char *yyt = yyto;
1011 register YYSIZE_T yyi = yycount;
1012
1013 while (yyi-- != 0)
1014 *yyt++ = *yyf++;
1015 }
1016 # endif
1017 #endif
1018
1019 #ifdef YYERROR_VERBOSE
1020
1021 # ifndef yystrlen
1022 # if defined (__GLIBC__) && defined (_STRING_H)
1023 # define yystrlen strlen
1024 # else
1025 /* Return the length of YYSTR. */
1026 static YYSIZE_T
1027 # if defined (__STDC__) || defined (__cplusplus)
1028 yystrlen (const char *yystr)
1029 # else
1030 yystrlen (yystr)
1031 const char *yystr;
1032 # endif
1033 {
1034 register const char *yys = yystr;
1035
1036 while (*yys++ != '\0')
1037 continue;
1038
1039 return yys - yystr - 1;
1040 }
1041 # endif
1042 # endif
1043
1044 # ifndef yystpcpy
1045 # if defined (__GLIBC__) && defined (_STRING_H) && defined (_GNU_SOURCE)
1046 # define yystpcpy stpcpy
1047 # else
1048 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
1049 YYDEST. */
1050 static char *
1051 # if defined (__STDC__) || defined (__cplusplus)
1052 yystpcpy (char *yydest, const char *yysrc)
1053 # else
1054 yystpcpy (yydest, yysrc)
1055 char *yydest;
1056 const char *yysrc;
1057 # endif
1058 {
1059 register char *yyd = yydest;
1060 register const char *yys = yysrc;
1061
1062 while ((*yyd++ = *yys++) != '\0')
1063 continue;
1064
1065 return yyd - 1;
1066 }
1067 # endif
1068 # endif
1069 #endif
1070 \f
1071 #line 319 "/usr/local/share/bison/bison.simple"
1072
1073
1074 /* The user can define YYPARSE_PARAM as the name of an argument to be passed
1075 into yyparse. The argument should have type void *.
1076 It should actually point to an object.
1077 Grammar actions can access the variable by casting it
1078 to the proper pointer type. */
1079
1080 #ifdef YYPARSE_PARAM
1081 # if defined (__STDC__) || defined (__cplusplus)
1082 # define YYPARSE_PARAM_ARG void *YYPARSE_PARAM
1083 # define YYPARSE_PARAM_DECL
1084 # else
1085 # define YYPARSE_PARAM_ARG YYPARSE_PARAM
1086 # define YYPARSE_PARAM_DECL void *YYPARSE_PARAM;
1087 # endif
1088 #else /* !YYPARSE_PARAM */
1089 # define YYPARSE_PARAM_ARG
1090 # define YYPARSE_PARAM_DECL
1091 #endif /* !YYPARSE_PARAM */
1092
1093 /* Prevent warning if -Wstrict-prototypes. */
1094 #ifdef __GNUC__
1095 # ifdef YYPARSE_PARAM
1096 int yyparse (void *);
1097 # else
1098 int yyparse (void);
1099 # endif
1100 #endif
1101
1102 /* YY_DECL_VARIABLES -- depending whether we use a pure parser,
1103 variables are global, or local to YYPARSE. */
1104
1105 #define YY_DECL_NON_LSP_VARIABLES \
1106 /* The lookahead symbol. */ \
1107 int yychar; \
1108 \
1109 /* The semantic value of the lookahead symbol. */ \
1110 YYSTYPE yylval; \
1111 \
1112 /* Number of parse errors so far. */ \
1113 int yynerrs;
1114
1115 #if YYLSP_NEEDED
1116 # define YY_DECL_VARIABLES \
1117 YY_DECL_NON_LSP_VARIABLES \
1118 \
1119 /* Location data for the lookahead symbol. */ \
1120 YYLTYPE yylloc;
1121 #else
1122 # define YY_DECL_VARIABLES \
1123 YY_DECL_NON_LSP_VARIABLES
1124 #endif
1125
1126
1127 /* If nonreentrant, generate the variables here. */
1128
1129 #if !YYPURE
1130 YY_DECL_VARIABLES
1131 #endif /* !YYPURE */
1132
1133 int
1134 yyparse (YYPARSE_PARAM_ARG)
1135 YYPARSE_PARAM_DECL
1136 {
1137 /* If reentrant, generate the variables here. */
1138 #if YYPURE
1139 YY_DECL_VARIABLES
1140 #endif /* !YYPURE */
1141
1142 register int yystate;
1143 register int yyn;
1144 int yyresult;
1145 /* Number of tokens to shift before error messages enabled. */
1146 int yyerrstatus;
1147 /* Lookahead token as an internal (translated) token number. */
1148 int yychar1 = 0;
1149
1150 /* Three stacks and their tools:
1151 `yyss': related to states,
1152 `yyvs': related to semantic values,
1153 `yyls': related to locations.
1154
1155 Refer to the stacks thru separate pointers, to allow yyoverflow
1156 to reallocate them elsewhere. */
1157
1158 /* The state stack. */
1159 short yyssa[YYINITDEPTH];
1160 short *yyss = yyssa;
1161 register short *yyssp;
1162
1163 /* The semantic value stack. */
1164 YYSTYPE yyvsa[YYINITDEPTH];
1165 YYSTYPE *yyvs = yyvsa;
1166 register YYSTYPE *yyvsp;
1167
1168 #if YYLSP_NEEDED
1169 /* The location stack. */
1170 YYLTYPE yylsa[YYINITDEPTH];
1171 YYLTYPE *yyls = yylsa;
1172 YYLTYPE *yylsp;
1173 #endif
1174
1175 #if YYLSP_NEEDED
1176 # define YYPOPSTACK (yyvsp--, yyssp--, yylsp--)
1177 #else
1178 # define YYPOPSTACK (yyvsp--, yyssp--)
1179 #endif
1180
1181 YYSIZE_T yystacksize = YYINITDEPTH;
1182
1183
1184 /* The variables used to return semantic value and location from the
1185 action routines. */
1186 YYSTYPE yyval;
1187 #if YYLSP_NEEDED
1188 YYLTYPE yyloc;
1189 #endif
1190
1191 /* When reducing, the number of symbols on the RHS of the reduced
1192 rule. */
1193 int yylen;
1194
1195 YYDPRINTF ((stderr, "Starting parse\n"));
1196
1197 yystate = 0;
1198 yyerrstatus = 0;
1199 yynerrs = 0;
1200 yychar = YYEMPTY; /* Cause a token to be read. */
1201
1202 /* Initialize stack pointers.
1203 Waste one element of value and location stack
1204 so that they stay on the same level as the state stack.
1205 The wasted elements are never initialized. */
1206
1207 yyssp = yyss;
1208 yyvsp = yyvs;
1209 #if YYLSP_NEEDED
1210 yylsp = yyls;
1211 #endif
1212 goto yysetstate;
1213
1214 /*------------------------------------------------------------.
1215 | yynewstate -- Push a new state, which is found in yystate. |
1216 `------------------------------------------------------------*/
1217 yynewstate:
1218 /* In all cases, when you get here, the value and location stacks
1219 have just been pushed. so pushing a state here evens the stacks.
1220 */
1221 yyssp++;
1222
1223 yysetstate:
1224 *yyssp = yystate;
1225
1226 if (yyssp >= yyss + yystacksize - 1)
1227 {
1228 /* Get the current used size of the three stacks, in elements. */
1229 YYSIZE_T yysize = yyssp - yyss + 1;
1230
1231 #ifdef yyoverflow
1232 {
1233 /* Give user a chance to reallocate the stack. Use copies of
1234 these so that the &'s don't force the real ones into
1235 memory. */
1236 YYSTYPE *yyvs1 = yyvs;
1237 short *yyss1 = yyss;
1238
1239 /* Each stack pointer address is followed by the size of the
1240 data in use in that stack, in bytes. */
1241 # if YYLSP_NEEDED
1242 YYLTYPE *yyls1 = yyls;
1243 /* This used to be a conditional around just the two extra args,
1244 but that might be undefined if yyoverflow is a macro. */
1245 yyoverflow ("parser stack overflow",
1246 &yyss1, yysize * sizeof (*yyssp),
1247 &yyvs1, yysize * sizeof (*yyvsp),
1248 &yyls1, yysize * sizeof (*yylsp),
1249 &yystacksize);
1250 yyls = yyls1;
1251 # else
1252 yyoverflow ("parser stack overflow",
1253 &yyss1, yysize * sizeof (*yyssp),
1254 &yyvs1, yysize * sizeof (*yyvsp),
1255 &yystacksize);
1256 # endif
1257 yyss = yyss1;
1258 yyvs = yyvs1;
1259 }
1260 #else /* no yyoverflow */
1261 /* Extend the stack our own way. */
1262 if (yystacksize >= YYMAXDEPTH)
1263 goto yyoverflowlab;
1264 yystacksize *= 2;
1265 if (yystacksize > YYMAXDEPTH)
1266 yystacksize = YYMAXDEPTH;
1267
1268 {
1269 short *yyss1 = yyss;
1270 union yyalloc *yyptr =
1271 (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
1272 if (! yyptr)
1273 goto yyoverflowlab;
1274 YYSTACK_RELOCATE (short, yyss);
1275 YYSTACK_RELOCATE (YYSTYPE, yyvs);
1276 # if YYLSP_NEEDED
1277 YYSTACK_RELOCATE (YYLTYPE, yyls);
1278 # endif
1279 # undef YYSTACK_RELOCATE
1280 if (yyss1 != yyssa)
1281 YYSTACK_FREE (yyss1);
1282 }
1283 #endif /* no yyoverflow */
1284
1285 yyssp = yyss + yysize - 1;
1286 yyvsp = yyvs + yysize - 1;
1287 #if YYLSP_NEEDED
1288 yylsp = yyls + yysize - 1;
1289 #endif
1290
1291 YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1292 (unsigned long int) yystacksize));
1293
1294 if (yyssp >= yyss + yystacksize - 1)
1295 YYABORT;
1296 }
1297
1298 YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1299
1300 goto yybackup;
1301
1302
1303 /*-----------.
1304 | yybackup. |
1305 `-----------*/
1306 yybackup:
1307
1308 /* Do appropriate processing given the current state. */
1309 /* Read a lookahead token if we need one and don't already have one. */
1310 /* yyresume: */
1311
1312 /* First try to decide what to do without reference to lookahead token. */
1313
1314 yyn = yypact[yystate];
1315 if (yyn == YYFLAG)
1316 goto yydefault;
1317
1318 /* Not known => get a lookahead token if don't already have one. */
1319
1320 /* yychar is either YYEMPTY or YYEOF
1321 or a valid token in external form. */
1322
1323 if (yychar == YYEMPTY)
1324 {
1325 YYDPRINTF ((stderr, "Reading a token: "));
1326 yychar = YYLEX;
1327 }
1328
1329 /* Convert token to internal form (in yychar1) for indexing tables with */
1330
1331 if (yychar <= 0) /* This means end of input. */
1332 {
1333 yychar1 = 0;
1334 yychar = YYEOF; /* Don't call YYLEX any more */
1335
1336 YYDPRINTF ((stderr, "Now at end of input.\n"));
1337 }
1338 else
1339 {
1340 yychar1 = YYTRANSLATE (yychar);
1341
1342 #if YYDEBUG
1343 /* We have to keep this `#if YYDEBUG', since we use variables
1344 which are defined only if `YYDEBUG' is set. */
1345 if (yydebug)
1346 {
1347 YYFPRINTF (stderr, "Next token is %d (%s",
1348 yychar, yytname[yychar1]);
1349 /* Give the individual parser a way to print the precise
1350 meaning of a token, for further debugging info. */
1351 # ifdef YYPRINT
1352 YYPRINT (stderr, yychar, yylval);
1353 # endif
1354 YYFPRINTF (stderr, ")\n");
1355 }
1356 #endif
1357 }
1358
1359 yyn += yychar1;
1360 if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
1361 goto yydefault;
1362
1363 yyn = yytable[yyn];
1364
1365 /* yyn is what to do for this token type in this state.
1366 Negative => reduce, -yyn is rule number.
1367 Positive => shift, yyn is new state.
1368 New state is final state => don't bother to shift,
1369 just return success.
1370 0, or most negative number => error. */
1371
1372 if (yyn < 0)
1373 {
1374 if (yyn == YYFLAG)
1375 goto yyerrlab;
1376 yyn = -yyn;
1377 goto yyreduce;
1378 }
1379 else if (yyn == 0)
1380 goto yyerrlab;
1381
1382 if (yyn == YYFINAL)
1383 YYACCEPT;
1384
1385 /* Shift the lookahead token. */
1386 YYDPRINTF ((stderr, "Shifting token %d (%s), ",
1387 yychar, yytname[yychar1]));
1388
1389 /* Discard the token being shifted unless it is eof. */
1390 if (yychar != YYEOF)
1391 yychar = YYEMPTY;
1392
1393 *++yyvsp = yylval;
1394 #if YYLSP_NEEDED
1395 *++yylsp = yylloc;
1396 #endif
1397
1398 /* Count tokens shifted since error; after three, turn off error
1399 status. */
1400 if (yyerrstatus)
1401 yyerrstatus--;
1402
1403 yystate = yyn;
1404 goto yynewstate;
1405
1406
1407 /*-----------------------------------------------------------.
1408 | yydefault -- do the default action for the current state. |
1409 `-----------------------------------------------------------*/
1410 yydefault:
1411 yyn = yydefact[yystate];
1412 if (yyn == 0)
1413 goto yyerrlab;
1414 goto yyreduce;
1415
1416
1417 /*-----------------------------.
1418 | yyreduce -- Do a reduction. |
1419 `-----------------------------*/
1420 yyreduce:
1421 /* yyn is the number of a rule to reduce with. */
1422 yylen = yyr2[yyn];
1423
1424 /* If YYLEN is nonzero, implement the default value of the action:
1425 `$$ = $1'.
1426
1427 Otherwise, the following line sets YYVAL to the semantic value of
1428 the lookahead token. This behavior is undocumented and Bison
1429 users should not rely upon it. Assigning to YYVAL
1430 unconditionally makes the parser a bit smaller, and it avoids a
1431 GCC warning that YYVAL may be used uninitialized. */
1432 yyval = yyvsp[1-yylen];
1433
1434 #if YYLSP_NEEDED
1435 /* Similarly for the default location. Let the user run additional
1436 commands if for instance locations are ranges. */
1437 yyloc = yylsp[1-yylen];
1438 YYLLOC_DEFAULT (yyloc, (yylsp - yylen), yylen);
1439 #endif
1440
1441 #if YYDEBUG
1442 /* We have to keep this `#if YYDEBUG', since we use variables which
1443 are defined only if `YYDEBUG' is set. */
1444 if (yydebug)
1445 {
1446 int yyi;
1447
1448 YYFPRINTF (stderr, "Reducing via rule %d (line %d), ",
1449 yyn, yyrline[yyn]);
1450
1451 /* Print the symbols being reduced, and their result. */
1452 for (yyi = yyprhs[yyn]; yyrhs[yyi] > 0; yyi++)
1453 YYFPRINTF (stderr, "%s ", yytname[yyrhs[yyi]]);
1454 YYFPRINTF (stderr, " -> %s\n", yytname[yyr1[yyn]]);
1455 }
1456 #endif
1457
1458 switch (yyn) {
1459
1460 case 1:
1461 #line 327 "/usr/homes/chet/src/bash/src/parse.y"
1462 {
1463 /* Case of regular command. Discard the error
1464 safety net,and return the command just parsed. */
1465 global_command = yyvsp[-1].command;
1466 eof_encountered = 0;
1467 /* discard_parser_constructs (0); */
1468 YYACCEPT;
1469 }
1470 break;
1471 case 2:
1472 #line 336 "/usr/homes/chet/src/bash/src/parse.y"
1473 {
1474 /* Case of regular command, but not a very
1475 interesting one. Return a NULL command. */
1476 global_command = (COMMAND *)NULL;
1477 YYACCEPT;
1478 }
1479 break;
1480 case 3:
1481 #line 343 "/usr/homes/chet/src/bash/src/parse.y"
1482 {
1483 /* Error during parsing. Return NULL command. */
1484 global_command = (COMMAND *)NULL;
1485 eof_encountered = 0;
1486 /* discard_parser_constructs (1); */
1487 if (interactive)
1488 {
1489 YYACCEPT;
1490 }
1491 else
1492 {
1493 YYABORT;
1494 }
1495 }
1496 break;
1497 case 4:
1498 #line 358 "/usr/homes/chet/src/bash/src/parse.y"
1499 {
1500 /* Case of EOF seen by itself. Do ignoreeof or
1501 not. */
1502 global_command = (COMMAND *)NULL;
1503 handle_eof_input_unit ();
1504 YYACCEPT;
1505 }
1506 break;
1507 case 5:
1508 #line 368 "/usr/homes/chet/src/bash/src/parse.y"
1509 { yyval.word_list = make_word_list (yyvsp[0].word, (WORD_LIST *)NULL); }
1510 break;
1511 case 6:
1512 #line 370 "/usr/homes/chet/src/bash/src/parse.y"
1513 { yyval.word_list = make_word_list (yyvsp[0].word, yyvsp[-1].word_list); }
1514 break;
1515 case 7:
1516 #line 374 "/usr/homes/chet/src/bash/src/parse.y"
1517 {
1518 redir.filename = yyvsp[0].word;
1519 yyval.redirect = make_redirection (1, r_output_direction, redir);
1520 }
1521 break;
1522 case 8:
1523 #line 379 "/usr/homes/chet/src/bash/src/parse.y"
1524 {
1525 redir.filename = yyvsp[0].word;
1526 yyval.redirect = make_redirection (0, r_input_direction, redir);
1527 }
1528 break;
1529 case 9:
1530 #line 384 "/usr/homes/chet/src/bash/src/parse.y"
1531 {
1532 redir.filename = yyvsp[0].word;
1533 yyval.redirect = make_redirection (yyvsp[-2].number, r_output_direction, redir);
1534 }
1535 break;
1536 case 10:
1537 #line 389 "/usr/homes/chet/src/bash/src/parse.y"
1538 {
1539 redir.filename = yyvsp[0].word;
1540 yyval.redirect = make_redirection (yyvsp[-2].number, r_input_direction, redir);
1541 }
1542 break;
1543 case 11:
1544 #line 394 "/usr/homes/chet/src/bash/src/parse.y"
1545 {
1546 redir.filename = yyvsp[0].word;
1547 yyval.redirect = make_redirection (1, r_appending_to, redir);
1548 }
1549 break;
1550 case 12:
1551 #line 399 "/usr/homes/chet/src/bash/src/parse.y"
1552 {
1553 redir.filename = yyvsp[0].word;
1554 yyval.redirect = make_redirection (yyvsp[-2].number, r_appending_to, redir);
1555 }
1556 break;
1557 case 13:
1558 #line 404 "/usr/homes/chet/src/bash/src/parse.y"
1559 {
1560 redir.filename = yyvsp[0].word;
1561 yyval.redirect = make_redirection (0, r_reading_until, redir);
1562 redir_stack[need_here_doc++] = yyval.redirect;
1563 }
1564 break;
1565 case 14:
1566 #line 410 "/usr/homes/chet/src/bash/src/parse.y"
1567 {
1568 redir.filename = yyvsp[0].word;
1569 yyval.redirect = make_redirection (yyvsp[-2].number, r_reading_until, redir);
1570 redir_stack[need_here_doc++] = yyval.redirect;
1571 }
1572 break;
1573 case 15:
1574 #line 416 "/usr/homes/chet/src/bash/src/parse.y"
1575 {
1576 redir.filename = yyvsp[0].word;
1577 yyval.redirect = make_redirection (0, r_reading_string, redir);
1578 }
1579 break;
1580 case 16:
1581 #line 421 "/usr/homes/chet/src/bash/src/parse.y"
1582 {
1583 redir.filename = yyvsp[0].word;
1584 yyval.redirect = make_redirection (yyvsp[-2].number, r_reading_string, redir);
1585 }
1586 break;
1587 case 17:
1588 #line 426 "/usr/homes/chet/src/bash/src/parse.y"
1589 {
1590 redir.dest = yyvsp[0].number;
1591 yyval.redirect = make_redirection (0, r_duplicating_input, redir);
1592 }
1593 break;
1594 case 18:
1595 #line 431 "/usr/homes/chet/src/bash/src/parse.y"
1596 {
1597 redir.dest = yyvsp[0].number;
1598 yyval.redirect = make_redirection (yyvsp[-2].number, r_duplicating_input, redir);
1599 }
1600 break;
1601 case 19:
1602 #line 436 "/usr/homes/chet/src/bash/src/parse.y"
1603 {
1604 redir.dest = yyvsp[0].number;
1605 yyval.redirect = make_redirection (1, r_duplicating_output, redir);
1606 }
1607 break;
1608 case 20:
1609 #line 441 "/usr/homes/chet/src/bash/src/parse.y"
1610 {
1611 redir.dest = yyvsp[0].number;
1612 yyval.redirect = make_redirection (yyvsp[-2].number, r_duplicating_output, redir);
1613 }
1614 break;
1615 case 21:
1616 #line 446 "/usr/homes/chet/src/bash/src/parse.y"
1617 {
1618 redir.filename = yyvsp[0].word;
1619 yyval.redirect = make_redirection (0, r_duplicating_input_word, redir);
1620 }
1621 break;
1622 case 22:
1623 #line 451 "/usr/homes/chet/src/bash/src/parse.y"
1624 {
1625 redir.filename = yyvsp[0].word;
1626 yyval.redirect = make_redirection (yyvsp[-2].number, r_duplicating_input_word, redir);
1627 }
1628 break;
1629 case 23:
1630 #line 456 "/usr/homes/chet/src/bash/src/parse.y"
1631 {
1632 redir.filename = yyvsp[0].word;
1633 yyval.redirect = make_redirection (1, r_duplicating_output_word, redir);
1634 }
1635 break;
1636 case 24:
1637 #line 461 "/usr/homes/chet/src/bash/src/parse.y"
1638 {
1639 redir.filename = yyvsp[0].word;
1640 yyval.redirect = make_redirection (yyvsp[-2].number, r_duplicating_output_word, redir);
1641 }
1642 break;
1643 case 25:
1644 #line 466 "/usr/homes/chet/src/bash/src/parse.y"
1645 {
1646 redir.filename = yyvsp[0].word;
1647 yyval.redirect = make_redirection
1648 (0, r_deblank_reading_until, redir);
1649 redir_stack[need_here_doc++] = yyval.redirect;
1650 }
1651 break;
1652 case 26:
1653 #line 473 "/usr/homes/chet/src/bash/src/parse.y"
1654 {
1655 redir.filename = yyvsp[0].word;
1656 yyval.redirect = make_redirection
1657 (yyvsp[-2].number, r_deblank_reading_until, redir);
1658 redir_stack[need_here_doc++] = yyval.redirect;
1659 }
1660 break;
1661 case 27:
1662 #line 480 "/usr/homes/chet/src/bash/src/parse.y"
1663 {
1664 redir.dest = 0;
1665 yyval.redirect = make_redirection (1, r_close_this, redir);
1666 }
1667 break;
1668 case 28:
1669 #line 485 "/usr/homes/chet/src/bash/src/parse.y"
1670 {
1671 redir.dest = 0;
1672 yyval.redirect = make_redirection (yyvsp[-2].number, r_close_this, redir);
1673 }
1674 break;
1675 case 29:
1676 #line 490 "/usr/homes/chet/src/bash/src/parse.y"
1677 {
1678 redir.dest = 0;
1679 yyval.redirect = make_redirection (0, r_close_this, redir);
1680 }
1681 break;
1682 case 30:
1683 #line 495 "/usr/homes/chet/src/bash/src/parse.y"
1684 {
1685 redir.dest = 0;
1686 yyval.redirect = make_redirection (yyvsp[-2].number, r_close_this, redir);
1687 }
1688 break;
1689 case 31:
1690 #line 500 "/usr/homes/chet/src/bash/src/parse.y"
1691 {
1692 redir.filename = yyvsp[0].word;
1693 yyval.redirect = make_redirection (1, r_err_and_out, redir);
1694 }
1695 break;
1696 case 32:
1697 #line 505 "/usr/homes/chet/src/bash/src/parse.y"
1698 {
1699 redir.filename = yyvsp[0].word;
1700 yyval.redirect = make_redirection (yyvsp[-2].number, r_input_output, redir);
1701 }
1702 break;
1703 case 33:
1704 #line 510 "/usr/homes/chet/src/bash/src/parse.y"
1705 {
1706 redir.filename = yyvsp[0].word;
1707 yyval.redirect = make_redirection (0, r_input_output, redir);
1708 }
1709 break;
1710 case 34:
1711 #line 515 "/usr/homes/chet/src/bash/src/parse.y"
1712 {
1713 redir.filename = yyvsp[0].word;
1714 yyval.redirect = make_redirection (1, r_output_force, redir);
1715 }
1716 break;
1717 case 35:
1718 #line 520 "/usr/homes/chet/src/bash/src/parse.y"
1719 {
1720 redir.filename = yyvsp[0].word;
1721 yyval.redirect = make_redirection (yyvsp[-2].number, r_output_force, redir);
1722 }
1723 break;
1724 case 36:
1725 #line 527 "/usr/homes/chet/src/bash/src/parse.y"
1726 { yyval.element.word = yyvsp[0].word; yyval.element.redirect = 0; }
1727 break;
1728 case 37:
1729 #line 529 "/usr/homes/chet/src/bash/src/parse.y"
1730 { yyval.element.word = yyvsp[0].word; yyval.element.redirect = 0; }
1731 break;
1732 case 38:
1733 #line 531 "/usr/homes/chet/src/bash/src/parse.y"
1734 { yyval.element.redirect = yyvsp[0].redirect; yyval.element.word = 0; }
1735 break;
1736 case 39:
1737 #line 535 "/usr/homes/chet/src/bash/src/parse.y"
1738 {
1739 yyval.redirect = yyvsp[0].redirect;
1740 }
1741 break;
1742 case 40:
1743 #line 539 "/usr/homes/chet/src/bash/src/parse.y"
1744 {
1745 register REDIRECT *t;
1746
1747 for (t = yyvsp[-1].redirect; t->next; t = t->next)
1748 ;
1749 t->next = yyvsp[0].redirect;
1750 yyval.redirect = yyvsp[-1].redirect;
1751 }
1752 break;
1753 case 41:
1754 #line 550 "/usr/homes/chet/src/bash/src/parse.y"
1755 { yyval.command = make_simple_command (yyvsp[0].element, (COMMAND *)NULL); }
1756 break;
1757 case 42:
1758 #line 552 "/usr/homes/chet/src/bash/src/parse.y"
1759 { yyval.command = make_simple_command (yyvsp[0].element, yyvsp[-1].command); }
1760 break;
1761 case 43:
1762 #line 556 "/usr/homes/chet/src/bash/src/parse.y"
1763 { yyval.command = clean_simple_command (yyvsp[0].command); }
1764 break;
1765 case 44:
1766 #line 558 "/usr/homes/chet/src/bash/src/parse.y"
1767 { yyval.command = yyvsp[0].command; }
1768 break;
1769 case 45:
1770 #line 560 "/usr/homes/chet/src/bash/src/parse.y"
1771 {
1772 COMMAND *tc;
1773
1774 tc = yyvsp[-1].command;
1775 if (tc->redirects)
1776 {
1777 register REDIRECT *t;
1778 for (t = tc->redirects; t->next; t = t->next)
1779 ;
1780 t->next = yyvsp[0].redirect;
1781 }
1782 else
1783 tc->redirects = yyvsp[0].redirect;
1784 yyval.command = yyvsp[-1].command;
1785 }
1786 break;
1787 case 46:
1788 #line 576 "/usr/homes/chet/src/bash/src/parse.y"
1789 { yyval.command = yyvsp[0].command; }
1790 break;
1791 case 47:
1792 #line 580 "/usr/homes/chet/src/bash/src/parse.y"
1793 { yyval.command = yyvsp[0].command; }
1794 break;
1795 case 48:
1796 #line 582 "/usr/homes/chet/src/bash/src/parse.y"
1797 { yyval.command = yyvsp[0].command; }
1798 break;
1799 case 49:
1800 #line 584 "/usr/homes/chet/src/bash/src/parse.y"
1801 { yyval.command = make_while_command (yyvsp[-3].command, yyvsp[-1].command); }
1802 break;
1803 case 50:
1804 #line 586 "/usr/homes/chet/src/bash/src/parse.y"
1805 { yyval.command = make_until_command (yyvsp[-3].command, yyvsp[-1].command); }
1806 break;
1807 case 51:
1808 #line 588 "/usr/homes/chet/src/bash/src/parse.y"
1809 { yyval.command = yyvsp[0].command; }
1810 break;
1811 case 52:
1812 #line 590 "/usr/homes/chet/src/bash/src/parse.y"
1813 { yyval.command = yyvsp[0].command; }
1814 break;
1815 case 53:
1816 #line 592 "/usr/homes/chet/src/bash/src/parse.y"
1817 { yyval.command = yyvsp[0].command; }
1818 break;
1819 case 54:
1820 #line 594 "/usr/homes/chet/src/bash/src/parse.y"
1821 { yyval.command = yyvsp[0].command; }
1822 break;
1823 case 55:
1824 #line 596 "/usr/homes/chet/src/bash/src/parse.y"
1825 { yyval.command = yyvsp[0].command; }
1826 break;
1827 case 56:
1828 #line 598 "/usr/homes/chet/src/bash/src/parse.y"
1829 { yyval.command = yyvsp[0].command; }
1830 break;
1831 case 57:
1832 #line 600 "/usr/homes/chet/src/bash/src/parse.y"
1833 { yyval.command = yyvsp[0].command; }
1834 break;
1835 case 58:
1836 #line 604 "/usr/homes/chet/src/bash/src/parse.y"
1837 { yyval.command = make_for_command (yyvsp[-4].word, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), yyvsp[-1].command); }
1838 break;
1839 case 59:
1840 #line 606 "/usr/homes/chet/src/bash/src/parse.y"
1841 { yyval.command = make_for_command (yyvsp[-4].word, add_string_to_list ("$@", (WORD_LIST *)NULL), yyvsp[-1].command); }
1842 break;
1843 case 60:
1844 #line 608 "/usr/homes/chet/src/bash/src/parse.y"
1845 { yyval.command = make_for_command (yyvsp[-5].word, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), yyvsp[-1].command); }
1846 break;
1847 case 61:
1848 #line 610 "/usr/homes/chet/src/bash/src/parse.y"
1849 { yyval.command = make_for_command (yyvsp[-5].word, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), yyvsp[-1].command); }
1850 break;
1851 case 62:
1852 #line 612 "/usr/homes/chet/src/bash/src/parse.y"
1853 { yyval.command = make_for_command (yyvsp[-8].word, REVERSE_LIST (yyvsp[-5].word_list, WORD_LIST *), yyvsp[-1].command); }
1854 break;
1855 case 63:
1856 #line 614 "/usr/homes/chet/src/bash/src/parse.y"
1857 { yyval.command = make_for_command (yyvsp[-8].word, REVERSE_LIST (yyvsp[-5].word_list, WORD_LIST *), yyvsp[-1].command); }
1858 break;
1859 case 64:
1860 #line 616 "/usr/homes/chet/src/bash/src/parse.y"
1861 { yyval.command = make_for_command (yyvsp[-7].word, (WORD_LIST *)NULL, yyvsp[-1].command); }
1862 break;
1863 case 65:
1864 #line 618 "/usr/homes/chet/src/bash/src/parse.y"
1865 { yyval.command = make_for_command (yyvsp[-7].word, (WORD_LIST *)NULL, yyvsp[-1].command); }
1866 break;
1867 case 66:
1868 #line 622 "/usr/homes/chet/src/bash/src/parse.y"
1869 { yyval.command = make_arith_for_command (yyvsp[-5].word_list, yyvsp[-1].command, arith_for_lineno); }
1870 break;
1871 case 67:
1872 #line 624 "/usr/homes/chet/src/bash/src/parse.y"
1873 { yyval.command = make_arith_for_command (yyvsp[-5].word_list, yyvsp[-1].command, arith_for_lineno); }
1874 break;
1875 case 68:
1876 #line 626 "/usr/homes/chet/src/bash/src/parse.y"
1877 { yyval.command = make_arith_for_command (yyvsp[-3].word_list, yyvsp[-1].command, arith_for_lineno); }
1878 break;
1879 case 69:
1880 #line 628 "/usr/homes/chet/src/bash/src/parse.y"
1881 { yyval.command = make_arith_for_command (yyvsp[-3].word_list, yyvsp[-1].command, arith_for_lineno); }
1882 break;
1883 case 70:
1884 #line 632 "/usr/homes/chet/src/bash/src/parse.y"
1885 {
1886 yyval.command = make_select_command (yyvsp[-4].word, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), yyvsp[-1].command);
1887 }
1888 break;
1889 case 71:
1890 #line 636 "/usr/homes/chet/src/bash/src/parse.y"
1891 {
1892 yyval.command = make_select_command (yyvsp[-4].word, add_string_to_list ("$@", (WORD_LIST *)NULL), yyvsp[-1].command);
1893 }
1894 break;
1895 case 72:
1896 #line 640 "/usr/homes/chet/src/bash/src/parse.y"
1897 {
1898 yyval.command = make_select_command (yyvsp[-5].word, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), yyvsp[-1].command);
1899 }
1900 break;
1901 case 73:
1902 #line 644 "/usr/homes/chet/src/bash/src/parse.y"
1903 {
1904 yyval.command = make_select_command (yyvsp[-5].word, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), yyvsp[-1].command);
1905 }
1906 break;
1907 case 74:
1908 #line 648 "/usr/homes/chet/src/bash/src/parse.y"
1909 {
1910 yyval.command = make_select_command (yyvsp[-8].word, REVERSE_LIST (yyvsp[-5].word_list, WORD_LIST *), yyvsp[-1].command);
1911 }
1912 break;
1913 case 75:
1914 #line 652 "/usr/homes/chet/src/bash/src/parse.y"
1915 {
1916 yyval.command = make_select_command (yyvsp[-8].word, REVERSE_LIST (yyvsp[-5].word_list, WORD_LIST *), yyvsp[-1].command);
1917 }
1918 break;
1919 case 76:
1920 #line 658 "/usr/homes/chet/src/bash/src/parse.y"
1921 { yyval.command = make_case_command (yyvsp[-4].word, (PATTERN_LIST *)NULL); }
1922 break;
1923 case 77:
1924 #line 660 "/usr/homes/chet/src/bash/src/parse.y"
1925 { yyval.command = make_case_command (yyvsp[-5].word, yyvsp[-2].pattern); }
1926 break;
1927 case 78:
1928 #line 662 "/usr/homes/chet/src/bash/src/parse.y"
1929 { yyval.command = make_case_command (yyvsp[-4].word, yyvsp[-1].pattern); }
1930 break;
1931 case 79:
1932 #line 666 "/usr/homes/chet/src/bash/src/parse.y"
1933 { yyval.command = make_function_def (yyvsp[-4].word, yyvsp[0].command, function_dstart, function_bstart); }
1934 break;
1935 case 80:
1936 #line 669 "/usr/homes/chet/src/bash/src/parse.y"
1937 { yyval.command = make_function_def (yyvsp[-4].word, yyvsp[0].command, function_dstart, function_bstart); }
1938 break;
1939 case 81:
1940 #line 672 "/usr/homes/chet/src/bash/src/parse.y"
1941 { yyval.command = make_function_def (yyvsp[-2].word, yyvsp[0].command, function_dstart, function_bstart); }
1942 break;
1943 case 82:
1944 #line 677 "/usr/homes/chet/src/bash/src/parse.y"
1945 { yyval.command = yyvsp[0].command; }
1946 break;
1947 case 83:
1948 #line 679 "/usr/homes/chet/src/bash/src/parse.y"
1949 {
1950 COMMAND *tc;
1951
1952 tc = yyvsp[-1].command;
1953 /* According to Posix.2 3.9.5, redirections
1954 specified after the body of a function should
1955 be attached to the function and performed when
1956 the function is executed, not as part of the
1957 function definition command. */
1958 /* XXX - I don't think it matters, but we might
1959 want to change this in the future to avoid
1960 problems differentiating between a function
1961 definition with a redirection and a function
1962 definition containing a single command with a
1963 redirection. The two are semantically equivalent,
1964 though -- the only difference is in how the
1965 command printing code displays the redirections. */
1966 if (tc->redirects)
1967 {
1968 register REDIRECT *t;
1969 for (t = tc->redirects; t->next; t = t->next)
1970 ;
1971 t->next = yyvsp[0].redirect;
1972 }
1973 else
1974 tc->redirects = yyvsp[0].redirect;
1975 yyval.command = yyvsp[-1].command;
1976 }
1977 break;
1978 case 84:
1979 #line 710 "/usr/homes/chet/src/bash/src/parse.y"
1980 {
1981 yyval.command = make_subshell_command (yyvsp[-1].command);
1982 yyval.command->flags |= CMD_WANT_SUBSHELL;
1983 }
1984 break;
1985 case 85:
1986 #line 717 "/usr/homes/chet/src/bash/src/parse.y"
1987 { yyval.command = make_if_command (yyvsp[-3].command, yyvsp[-1].command, (COMMAND *)NULL); }
1988 break;
1989 case 86:
1990 #line 719 "/usr/homes/chet/src/bash/src/parse.y"
1991 { yyval.command = make_if_command (yyvsp[-5].command, yyvsp[-3].command, yyvsp[-1].command); }
1992 break;
1993 case 87:
1994 #line 721 "/usr/homes/chet/src/bash/src/parse.y"
1995 { yyval.command = make_if_command (yyvsp[-4].command, yyvsp[-2].command, yyvsp[-1].command); }
1996 break;
1997 case 88:
1998 #line 726 "/usr/homes/chet/src/bash/src/parse.y"
1999 { yyval.command = make_group_command (yyvsp[-1].command); }
2000 break;
2001 case 89:
2002 #line 730 "/usr/homes/chet/src/bash/src/parse.y"
2003 { yyval.command = make_arith_command (yyvsp[0].word_list); }
2004 break;
2005 case 90:
2006 #line 734 "/usr/homes/chet/src/bash/src/parse.y"
2007 { yyval.command = yyvsp[-1].command; }
2008 break;
2009 case 91:
2010 #line 738 "/usr/homes/chet/src/bash/src/parse.y"
2011 { yyval.command = make_if_command (yyvsp[-2].command, yyvsp[0].command, (COMMAND *)NULL); }
2012 break;
2013 case 92:
2014 #line 740 "/usr/homes/chet/src/bash/src/parse.y"
2015 { yyval.command = make_if_command (yyvsp[-4].command, yyvsp[-2].command, yyvsp[0].command); }
2016 break;
2017 case 93:
2018 #line 742 "/usr/homes/chet/src/bash/src/parse.y"
2019 { yyval.command = make_if_command (yyvsp[-3].command, yyvsp[-1].command, yyvsp[0].command); }
2020 break;
2021 case 95:
2022 #line 747 "/usr/homes/chet/src/bash/src/parse.y"
2023 { yyvsp[0].pattern->next = yyvsp[-1].pattern; yyval.pattern = yyvsp[0].pattern; }
2024 break;
2025 case 96:
2026 #line 751 "/usr/homes/chet/src/bash/src/parse.y"
2027 { yyval.pattern = make_pattern_list (yyvsp[-2].word_list, yyvsp[0].command); }
2028 break;
2029 case 97:
2030 #line 753 "/usr/homes/chet/src/bash/src/parse.y"
2031 { yyval.pattern = make_pattern_list (yyvsp[-2].word_list, (COMMAND *)NULL); }
2032 break;
2033 case 98:
2034 #line 755 "/usr/homes/chet/src/bash/src/parse.y"
2035 { yyval.pattern = make_pattern_list (yyvsp[-2].word_list, yyvsp[0].command); }
2036 break;
2037 case 99:
2038 #line 757 "/usr/homes/chet/src/bash/src/parse.y"
2039 { yyval.pattern = make_pattern_list (yyvsp[-2].word_list, (COMMAND *)NULL); }
2040 break;
2041 case 101:
2042 #line 762 "/usr/homes/chet/src/bash/src/parse.y"
2043 { yyvsp[-1].pattern->next = yyvsp[-2].pattern; yyval.pattern = yyvsp[-1].pattern; }
2044 break;
2045 case 102:
2046 #line 766 "/usr/homes/chet/src/bash/src/parse.y"
2047 { yyval.word_list = make_word_list (yyvsp[0].word, (WORD_LIST *)NULL); }
2048 break;
2049 case 103:
2050 #line 768 "/usr/homes/chet/src/bash/src/parse.y"
2051 { yyval.word_list = make_word_list (yyvsp[0].word, yyvsp[-2].word_list); }
2052 break;
2053 case 104:
2054 #line 777 "/usr/homes/chet/src/bash/src/parse.y"
2055 {
2056 yyval.command = yyvsp[0].command;
2057 if (need_here_doc)
2058 gather_here_documents ();
2059 }
2060 break;
2061 case 106:
2062 #line 786 "/usr/homes/chet/src/bash/src/parse.y"
2063 {
2064 yyval.command = yyvsp[0].command;
2065 }
2066 break;
2067 case 108:
2068 #line 793 "/usr/homes/chet/src/bash/src/parse.y"
2069 {
2070 if (yyvsp[-2].command->type == cm_connection)
2071 yyval.command = connect_async_list (yyvsp[-2].command, (COMMAND *)NULL, '&');
2072 else
2073 yyval.command = command_connect (yyvsp[-2].command, (COMMAND *)NULL, '&');
2074 }
2075 break;
2076 case 110:
2077 #line 804 "/usr/homes/chet/src/bash/src/parse.y"
2078 { yyval.command = command_connect (yyvsp[-3].command, yyvsp[0].command, AND_AND); }
2079 break;
2080 case 111:
2081 #line 806 "/usr/homes/chet/src/bash/src/parse.y"
2082 { yyval.command = command_connect (yyvsp[-3].command, yyvsp[0].command, OR_OR); }
2083 break;
2084 case 112:
2085 #line 808 "/usr/homes/chet/src/bash/src/parse.y"
2086 {
2087 if (yyvsp[-3].command->type == cm_connection)
2088 yyval.command = connect_async_list (yyvsp[-3].command, yyvsp[0].command, '&');
2089 else
2090 yyval.command = command_connect (yyvsp[-3].command, yyvsp[0].command, '&');
2091 }
2092 break;
2093 case 113:
2094 #line 815 "/usr/homes/chet/src/bash/src/parse.y"
2095 { yyval.command = command_connect (yyvsp[-3].command, yyvsp[0].command, ';'); }
2096 break;
2097 case 114:
2098 #line 817 "/usr/homes/chet/src/bash/src/parse.y"
2099 { yyval.command = command_connect (yyvsp[-3].command, yyvsp[0].command, ';'); }
2100 break;
2101 case 115:
2102 #line 819 "/usr/homes/chet/src/bash/src/parse.y"
2103 { yyval.command = yyvsp[0].command; }
2104 break;
2105 case 123:
2106 #line 842 "/usr/homes/chet/src/bash/src/parse.y"
2107 {
2108 yyval.command = yyvsp[0].command;
2109 if (need_here_doc)
2110 gather_here_documents ();
2111 }
2112 break;
2113 case 124:
2114 #line 848 "/usr/homes/chet/src/bash/src/parse.y"
2115 {
2116 if (yyvsp[-1].command->type == cm_connection)
2117 yyval.command = connect_async_list (yyvsp[-1].command, (COMMAND *)NULL, '&');
2118 else
2119 yyval.command = command_connect (yyvsp[-1].command, (COMMAND *)NULL, '&');
2120 if (need_here_doc)
2121 gather_here_documents ();
2122 }
2123 break;
2124 case 125:
2125 #line 857 "/usr/homes/chet/src/bash/src/parse.y"
2126 {
2127 yyval.command = yyvsp[-1].command;
2128 if (need_here_doc)
2129 gather_here_documents ();
2130 }
2131 break;
2132 case 126:
2133 #line 865 "/usr/homes/chet/src/bash/src/parse.y"
2134 { yyval.command = command_connect (yyvsp[-3].command, yyvsp[0].command, AND_AND); }
2135 break;
2136 case 127:
2137 #line 867 "/usr/homes/chet/src/bash/src/parse.y"
2138 { yyval.command = command_connect (yyvsp[-3].command, yyvsp[0].command, OR_OR); }
2139 break;
2140 case 128:
2141 #line 869 "/usr/homes/chet/src/bash/src/parse.y"
2142 {
2143 if (yyvsp[-2].command->type == cm_connection)
2144 yyval.command = connect_async_list (yyvsp[-2].command, yyvsp[0].command, '&');
2145 else
2146 yyval.command = command_connect (yyvsp[-2].command, yyvsp[0].command, '&');
2147 }
2148 break;
2149 case 129:
2150 #line 876 "/usr/homes/chet/src/bash/src/parse.y"
2151 { yyval.command = command_connect (yyvsp[-2].command, yyvsp[0].command, ';'); }
2152 break;
2153 case 130:
2154 #line 879 "/usr/homes/chet/src/bash/src/parse.y"
2155 { yyval.command = yyvsp[0].command; }
2156 break;
2157 case 131:
2158 #line 883 "/usr/homes/chet/src/bash/src/parse.y"
2159 { yyval.command = yyvsp[0].command; }
2160 break;
2161 case 132:
2162 #line 885 "/usr/homes/chet/src/bash/src/parse.y"
2163 {
2164 yyvsp[0].command->flags |= CMD_INVERT_RETURN;
2165 yyval.command = yyvsp[0].command;
2166 }
2167 break;
2168 case 133:
2169 #line 890 "/usr/homes/chet/src/bash/src/parse.y"
2170 {
2171 yyvsp[0].command->flags |= yyvsp[-1].number;
2172 yyval.command = yyvsp[0].command;
2173 }
2174 break;
2175 case 134:
2176 #line 895 "/usr/homes/chet/src/bash/src/parse.y"
2177 {
2178 yyvsp[0].command->flags |= yyvsp[-2].number|CMD_INVERT_RETURN;
2179 yyval.command = yyvsp[0].command;
2180 }
2181 break;
2182 case 135:
2183 #line 900 "/usr/homes/chet/src/bash/src/parse.y"
2184 {
2185 yyvsp[0].command->flags |= yyvsp[-1].number|CMD_INVERT_RETURN;
2186 yyval.command = yyvsp[0].command;
2187 }
2188 break;
2189 case 136:
2190 #line 908 "/usr/homes/chet/src/bash/src/parse.y"
2191 { yyval.command = command_connect (yyvsp[-3].command, yyvsp[0].command, '|'); }
2192 break;
2193 case 137:
2194 #line 910 "/usr/homes/chet/src/bash/src/parse.y"
2195 { yyval.command = yyvsp[0].command; }
2196 break;
2197 case 138:
2198 #line 914 "/usr/homes/chet/src/bash/src/parse.y"
2199 { yyval.number = CMD_TIME_PIPELINE; }
2200 break;
2201 case 139:
2202 #line 916 "/usr/homes/chet/src/bash/src/parse.y"
2203 { yyval.number = CMD_TIME_PIPELINE|CMD_TIME_POSIX; }
2204 break;
2205 }
2206
2207 #line 705 "/usr/local/share/bison/bison.simple"
2208
2209 \f
2210 yyvsp -= yylen;
2211 yyssp -= yylen;
2212 #if YYLSP_NEEDED
2213 yylsp -= yylen;
2214 #endif
2215
2216 #if YYDEBUG
2217 if (yydebug)
2218 {
2219 short *yyssp1 = yyss - 1;
2220 YYFPRINTF (stderr, "state stack now");
2221 while (yyssp1 != yyssp)
2222 YYFPRINTF (stderr, " %d", *++yyssp1);
2223 YYFPRINTF (stderr, "\n");
2224 }
2225 #endif
2226
2227 *++yyvsp = yyval;
2228 #if YYLSP_NEEDED
2229 *++yylsp = yyloc;
2230 #endif
2231
2232 /* Now `shift' the result of the reduction. Determine what state
2233 that goes to, based on the state we popped back to and the rule
2234 number reduced by. */
2235
2236 yyn = yyr1[yyn];
2237
2238 yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
2239 if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
2240 yystate = yytable[yystate];
2241 else
2242 yystate = yydefgoto[yyn - YYNTBASE];
2243
2244 goto yynewstate;
2245
2246
2247 /*------------------------------------.
2248 | yyerrlab -- here on detecting error |
2249 `------------------------------------*/
2250 yyerrlab:
2251 /* If not already recovering from an error, report this error. */
2252 if (!yyerrstatus)
2253 {
2254 ++yynerrs;
2255
2256 #ifdef YYERROR_VERBOSE
2257 yyn = yypact[yystate];
2258
2259 if (yyn > YYFLAG && yyn < YYLAST)
2260 {
2261 YYSIZE_T yysize = 0;
2262 char *yymsg;
2263 int yyx, yycount;
2264
2265 yycount = 0;
2266 /* Start YYX at -YYN if negative to avoid negative indexes in
2267 YYCHECK. */
2268 for (yyx = yyn < 0 ? -yyn : 0;
2269 yyx < (int) (sizeof (yytname) / sizeof (char *)); yyx++)
2270 if (yycheck[yyx + yyn] == yyx)
2271 yysize += yystrlen (yytname[yyx]) + 15, yycount++;
2272 yysize += yystrlen ("parse error, unexpected ") + 1;
2273 yysize += yystrlen (yytname[YYTRANSLATE (yychar)]);
2274 yymsg = (char *) YYSTACK_ALLOC (yysize);
2275 if (yymsg != 0)
2276 {
2277 char *yyp = yystpcpy (yymsg, "parse error, unexpected ");
2278 yyp = yystpcpy (yyp, yytname[YYTRANSLATE (yychar)]);
2279
2280 if (yycount < 5)
2281 {
2282 yycount = 0;
2283 for (yyx = yyn < 0 ? -yyn : 0;
2284 yyx < (int) (sizeof (yytname) / sizeof (char *));
2285 yyx++)
2286 if (yycheck[yyx + yyn] == yyx)
2287 {
2288 const char *yyq = ! yycount ? ", expecting " : " or ";
2289 yyp = yystpcpy (yyp, yyq);
2290 yyp = yystpcpy (yyp, yytname[yyx]);
2291 yycount++;
2292 }
2293 }
2294 yyerror (yymsg);
2295 YYSTACK_FREE (yymsg);
2296 }
2297 else
2298 yyerror ("parse error; also virtual memory exhausted");
2299 }
2300 else
2301 #endif /* defined (YYERROR_VERBOSE) */
2302 yyerror ("parse error");
2303 }
2304 goto yyerrlab1;
2305
2306
2307 /*--------------------------------------------------.
2308 | yyerrlab1 -- error raised explicitly by an action |
2309 `--------------------------------------------------*/
2310 yyerrlab1:
2311 if (yyerrstatus == 3)
2312 {
2313 /* If just tried and failed to reuse lookahead token after an
2314 error, discard it. */
2315
2316 /* return failure if at end of input */
2317 if (yychar == YYEOF)
2318 YYABORT;
2319 YYDPRINTF ((stderr, "Discarding token %d (%s).\n",
2320 yychar, yytname[yychar1]));
2321 yychar = YYEMPTY;
2322 }
2323
2324 /* Else will try to reuse lookahead token after shifting the error
2325 token. */
2326
2327 yyerrstatus = 3; /* Each real token shifted decrements this */
2328
2329 goto yyerrhandle;
2330
2331
2332 /*-------------------------------------------------------------------.
2333 | yyerrdefault -- current state does not do anything special for the |
2334 | error token. |
2335 `-------------------------------------------------------------------*/
2336 yyerrdefault:
2337 #if 0
2338 /* This is wrong; only states that explicitly want error tokens
2339 should shift them. */
2340
2341 /* If its default is to accept any token, ok. Otherwise pop it. */
2342 yyn = yydefact[yystate];
2343 if (yyn)
2344 goto yydefault;
2345 #endif
2346
2347
2348 /*---------------------------------------------------------------.
2349 | yyerrpop -- pop the current state because it cannot handle the |
2350 | error token |
2351 `---------------------------------------------------------------*/
2352 yyerrpop:
2353 if (yyssp == yyss)
2354 YYABORT;
2355 yyvsp--;
2356 yystate = *--yyssp;
2357 #if YYLSP_NEEDED
2358 yylsp--;
2359 #endif
2360
2361 #if YYDEBUG
2362 if (yydebug)
2363 {
2364 short *yyssp1 = yyss - 1;
2365 YYFPRINTF (stderr, "Error: state stack now");
2366 while (yyssp1 != yyssp)
2367 YYFPRINTF (stderr, " %d", *++yyssp1);
2368 YYFPRINTF (stderr, "\n");
2369 }
2370 #endif
2371
2372 /*--------------.
2373 | yyerrhandle. |
2374 `--------------*/
2375 yyerrhandle:
2376 yyn = yypact[yystate];
2377 if (yyn == YYFLAG)
2378 goto yyerrdefault;
2379
2380 yyn += YYTERROR;
2381 if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
2382 goto yyerrdefault;
2383
2384 yyn = yytable[yyn];
2385 if (yyn < 0)
2386 {
2387 if (yyn == YYFLAG)
2388 goto yyerrpop;
2389 yyn = -yyn;
2390 goto yyreduce;
2391 }
2392 else if (yyn == 0)
2393 goto yyerrpop;
2394
2395 if (yyn == YYFINAL)
2396 YYACCEPT;
2397
2398 YYDPRINTF ((stderr, "Shifting error token, "));
2399
2400 *++yyvsp = yylval;
2401 #if YYLSP_NEEDED
2402 *++yylsp = yylloc;
2403 #endif
2404
2405 yystate = yyn;
2406 goto yynewstate;
2407
2408
2409 /*-------------------------------------.
2410 | yyacceptlab -- YYACCEPT comes here. |
2411 `-------------------------------------*/
2412 yyacceptlab:
2413 yyresult = 0;
2414 goto yyreturn;
2415
2416 /*-----------------------------------.
2417 | yyabortlab -- YYABORT comes here. |
2418 `-----------------------------------*/
2419 yyabortlab:
2420 yyresult = 1;
2421 goto yyreturn;
2422
2423 /*---------------------------------------------.
2424 | yyoverflowab -- parser overflow comes here. |
2425 `---------------------------------------------*/
2426 yyoverflowlab:
2427 yyerror ("parser stack overflow");
2428 yyresult = 2;
2429 /* Fall through. */
2430
2431 yyreturn:
2432 #ifndef yyoverflow
2433 if (yyss != yyssa)
2434 YYSTACK_FREE (yyss);
2435 #endif
2436 return yyresult;
2437 }
2438 #line 918 "/usr/homes/chet/src/bash/src/parse.y"
2439
2440
2441 /* Possible states for the parser that require it to do special things. */
2442 #define PST_CASEPAT 0x001 /* in a case pattern list */
2443 #define PST_ALEXPNEXT 0x002 /* expand next word for aliases */
2444 #define PST_ALLOWOPNBRC 0x004 /* allow open brace for function def */
2445 #define PST_NEEDCLOSBRC 0x008 /* need close brace */
2446 #define PST_DBLPAREN 0x010 /* double-paren parsing */
2447 #define PST_SUBSHELL 0x020 /* ( ... ) subshell */
2448 #define PST_CMDSUBST 0x040 /* $( ... ) command substitution */
2449 #define PST_CASESTMT 0x080 /* parsing a case statement */
2450 #define PST_CONDCMD 0x100 /* parsing a [[...]] command */
2451 #define PST_CONDEXPR 0x200 /* parsing the guts of [[...]] */
2452 #define PST_ARITHFOR 0x400 /* parsing an arithmetic for command */
2453
2454 /* Initial size to allocate for tokens, and the
2455 amount to grow them by. */
2456 #define TOKEN_DEFAULT_INITIAL_SIZE 496
2457 #define TOKEN_DEFAULT_GROW_SIZE 512
2458
2459 /* The token currently being read. */
2460 static int current_token;
2461
2462 /* The last read token, or NULL. read_token () uses this for context
2463 checking. */
2464 static int last_read_token;
2465
2466 /* The token read prior to last_read_token. */
2467 static int token_before_that;
2468
2469 /* The token read prior to token_before_that. */
2470 static int two_tokens_ago;
2471
2472 /* If non-zero, it is the token that we want read_token to return
2473 regardless of what text is (or isn't) present to be read. This
2474 is reset by read_token. If token_to_read == WORD or
2475 ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
2476 static int token_to_read;
2477 static WORD_DESC *word_desc_to_read;
2478
2479 /* The current parser state. */
2480 static int parser_state;
2481
2482 /* Global var is non-zero when end of file has been reached. */
2483 int EOF_Reached = 0;
2484
2485 #ifdef DEBUG
2486 static void
2487 debug_parser (i)
2488 int i;
2489 {
2490 #if YYDEBUG != 0
2491 yydebug = i;
2492 #endif
2493 }
2494 #endif
2495
2496 /* yy_getc () returns the next available character from input or EOF.
2497 yy_ungetc (c) makes `c' the next character to read.
2498 init_yy_io (get, unget, type, location) makes the function GET the
2499 installed function for getting the next character, makes UNGET the
2500 installed function for un-getting a character, sets the type of stream
2501 (either string or file) from TYPE, and makes LOCATION point to where
2502 the input is coming from. */
2503
2504 /* Unconditionally returns end-of-file. */
2505 int
2506 return_EOF ()
2507 {
2508 return (EOF);
2509 }
2510
2511 /* Variable containing the current get and unget functions.
2512 See ./input.h for a clearer description. */
2513 BASH_INPUT bash_input;
2514
2515 /* Set all of the fields in BASH_INPUT to NULL. Free bash_input.name if it
2516 is non-null, avoiding a memory leak. */
2517 void
2518 initialize_bash_input ()
2519 {
2520 bash_input.type = st_none;
2521 FREE (bash_input.name);
2522 bash_input.name = (char *)NULL;
2523 bash_input.location.file = (FILE *)NULL;
2524 bash_input.location.string = (char *)NULL;
2525 bash_input.getter = (sh_cget_func_t *)NULL;
2526 bash_input.ungetter = (sh_cunget_func_t *)NULL;
2527 }
2528
2529 /* Set the contents of the current bash input stream from
2530 GET, UNGET, TYPE, NAME, and LOCATION. */
2531 void
2532 init_yy_io (get, unget, type, name, location)
2533 sh_cget_func_t *get;
2534 sh_cunget_func_t *unget;
2535 enum stream_type type;
2536 const char *name;
2537 INPUT_STREAM location;
2538 {
2539 bash_input.type = type;
2540 FREE (bash_input.name);
2541 bash_input.name = name ? savestring (name) : (char *)NULL;
2542
2543 /* XXX */
2544 #if defined (CRAY)
2545 memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
2546 #else
2547 bash_input.location = location;
2548 #endif
2549 bash_input.getter = get;
2550 bash_input.ungetter = unget;
2551 }
2552
2553 char *
2554 yy_input_name ()
2555 {
2556 return (bash_input.name ? bash_input.name : "stdin");
2557 }
2558
2559 /* Call this to get the next character of input. */
2560 static int
2561 yy_getc ()
2562 {
2563 return (*(bash_input.getter)) ();
2564 }
2565
2566 /* Call this to unget C. That is, to make C the next character
2567 to be read. */
2568 static int
2569 yy_ungetc (c)
2570 int c;
2571 {
2572 return (*(bash_input.ungetter)) (c);
2573 }
2574
2575 #if defined (BUFFERED_INPUT)
2576 #ifdef INCLUDE_UNUSED
2577 int
2578 input_file_descriptor ()
2579 {
2580 switch (bash_input.type)
2581 {
2582 case st_stream:
2583 return (fileno (bash_input.location.file));
2584 case st_bstream:
2585 return (bash_input.location.buffered_fd);
2586 case st_stdin:
2587 default:
2588 return (fileno (stdin));
2589 }
2590 }
2591 #endif
2592 #endif /* BUFFERED_INPUT */
2593
2594 /* **************************************************************** */
2595 /* */
2596 /* Let input be read from readline (). */
2597 /* */
2598 /* **************************************************************** */
2599
2600 #if defined (READLINE)
2601 char *current_readline_prompt = (char *)NULL;
2602 char *current_readline_line = (char *)NULL;
2603 int current_readline_line_index = 0;
2604
2605 static int
2606 yy_readline_get ()
2607 {
2608 SigHandler *old_sigint;
2609 int line_len;
2610 unsigned char c;
2611
2612 if (!current_readline_line)
2613 {
2614 if (!bash_readline_initialized)
2615 initialize_readline ();
2616
2617 #if defined (JOB_CONTROL)
2618 if (job_control)
2619 give_terminal_to (shell_pgrp, 0);
2620 #endif /* JOB_CONTROL */
2621
2622 old_sigint = (SigHandler *)NULL;
2623 if (signal_is_ignored (SIGINT) == 0)
2624 {
2625 old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
2626 interrupt_immediately++;
2627 }
2628
2629 current_readline_line = readline (current_readline_prompt ?
2630 current_readline_prompt : "");
2631
2632 if (signal_is_ignored (SIGINT) == 0 && old_sigint)
2633 {
2634 interrupt_immediately--;
2635 set_signal_handler (SIGINT, old_sigint);
2636 }
2637
2638 #if 0
2639 /* Reset the prompt to the decoded value of prompt_string_pointer. */
2640 reset_readline_prompt ();
2641 #endif
2642
2643 if (current_readline_line == 0)
2644 return (EOF);
2645
2646 current_readline_line_index = 0;
2647 line_len = strlen (current_readline_line);
2648
2649 current_readline_line = (char *)xrealloc (current_readline_line, 2 + line_len);
2650 current_readline_line[line_len++] = '\n';
2651 current_readline_line[line_len] = '\0';
2652 }
2653
2654 if (current_readline_line[current_readline_line_index] == 0)
2655 {
2656 free (current_readline_line);
2657 current_readline_line = (char *)NULL;
2658 return (yy_readline_get ());
2659 }
2660 else
2661 {
2662 c = current_readline_line[current_readline_line_index++];
2663 return (c);
2664 }
2665 }
2666
2667 static int
2668 yy_readline_unget (c)
2669 int c;
2670 {
2671 if (current_readline_line_index && current_readline_line)
2672 current_readline_line[--current_readline_line_index] = c;
2673 return (c);
2674 }
2675
2676 void
2677 with_input_from_stdin ()
2678 {
2679 INPUT_STREAM location;
2680
2681 if (bash_input.type != st_stdin && stream_on_stack (st_stdin) == 0)
2682 {
2683 location.string = current_readline_line;
2684 init_yy_io (yy_readline_get, yy_readline_unget,
2685 st_stdin, "readline stdin", location);
2686 }
2687 }
2688
2689 #else /* !READLINE */
2690
2691 void
2692 with_input_from_stdin ()
2693 {
2694 with_input_from_stream (stdin, "stdin");
2695 }
2696 #endif /* !READLINE */
2697
2698 /* **************************************************************** */
2699 /* */
2700 /* Let input come from STRING. STRING is zero terminated. */
2701 /* */
2702 /* **************************************************************** */
2703
2704 static int
2705 yy_string_get ()
2706 {
2707 register char *string;
2708 register unsigned char c;
2709
2710 string = bash_input.location.string;
2711
2712 /* If the string doesn't exist, or is empty, EOF found. */
2713 if (string && *string)
2714 {
2715 c = *string++;
2716 bash_input.location.string = string;
2717 return (c);
2718 }
2719 else
2720 return (EOF);
2721 }
2722
2723 static int
2724 yy_string_unget (c)
2725 int c;
2726 {
2727 *(--bash_input.location.string) = c;
2728 return (c);
2729 }
2730
2731 void
2732 with_input_from_string (string, name)
2733 char *string;
2734 const char *name;
2735 {
2736 INPUT_STREAM location;
2737
2738 location.string = string;
2739 init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
2740 }
2741
2742 /* **************************************************************** */
2743 /* */
2744 /* Let input come from STREAM. */
2745 /* */
2746 /* **************************************************************** */
2747
2748 /* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
2749 define, and just use getc/ungetc if it was defined, but since bash
2750 installs its signal handlers without the SA_RESTART flag, some signals
2751 (like SIGCHLD, SIGWINCH, etc.) received during a read(2) will not cause
2752 the read to be restarted. We need to restart it ourselves. */
2753
2754 static int
2755 yy_stream_get ()
2756 {
2757 int result;
2758
2759 result = EOF;
2760 if (bash_input.location.file)
2761 result = getc_with_restart (bash_input.location.file);
2762
2763 return (result);
2764 }
2765
2766 static int
2767 yy_stream_unget (c)
2768 int c;
2769 {
2770 return (ungetc_with_restart (c, bash_input.location.file));
2771 }
2772
2773 void
2774 with_input_from_stream (stream, name)
2775 FILE *stream;
2776 const char *name;
2777 {
2778 INPUT_STREAM location;
2779
2780 location.file = stream;
2781 init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
2782 }
2783
2784 typedef struct stream_saver {
2785 struct stream_saver *next;
2786 BASH_INPUT bash_input;
2787 int line;
2788 #if defined (BUFFERED_INPUT)
2789 BUFFERED_STREAM *bstream;
2790 #endif /* BUFFERED_INPUT */
2791 } STREAM_SAVER;
2792
2793 /* The globally known line number. */
2794 int line_number = 0;
2795
2796 #if defined (COND_COMMAND)
2797 static int cond_lineno;
2798 static int cond_token;
2799 #endif
2800
2801 STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
2802
2803 void
2804 push_stream (reset_lineno)
2805 int reset_lineno;
2806 {
2807 STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
2808
2809 xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
2810
2811 #if defined (BUFFERED_INPUT)
2812 saver->bstream = (BUFFERED_STREAM *)NULL;
2813 /* If we have a buffered stream, clear out buffers[fd]. */
2814 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
2815 saver->bstream = set_buffered_stream (bash_input.location.buffered_fd,
2816 (BUFFERED_STREAM *)NULL);
2817 #endif /* BUFFERED_INPUT */
2818
2819 saver->line = line_number;
2820 bash_input.name = (char *)NULL;
2821 saver->next = stream_list;
2822 stream_list = saver;
2823 EOF_Reached = 0;
2824 if (reset_lineno)
2825 line_number = 0;
2826 }
2827
2828 void
2829 pop_stream ()
2830 {
2831 if (!stream_list)
2832 EOF_Reached = 1;
2833 else
2834 {
2835 STREAM_SAVER *saver = stream_list;
2836
2837 EOF_Reached = 0;
2838 stream_list = stream_list->next;
2839
2840 init_yy_io (saver->bash_input.getter,
2841 saver->bash_input.ungetter,
2842 saver->bash_input.type,
2843 saver->bash_input.name,
2844 saver->bash_input.location);
2845
2846 #if defined (BUFFERED_INPUT)
2847 /* If we have a buffered stream, restore buffers[fd]. */
2848 /* If the input file descriptor was changed while this was on the
2849 save stack, update the buffered fd to the new file descriptor and
2850 re-establish the buffer <-> bash_input fd correspondence. */
2851 if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
2852 {
2853 if (bash_input_fd_changed)
2854 {
2855 bash_input_fd_changed = 0;
2856 if (default_buffered_input >= 0)
2857 {
2858 bash_input.location.buffered_fd = default_buffered_input;
2859 saver->bstream->b_fd = default_buffered_input;
2860 SET_CLOSE_ON_EXEC (default_buffered_input);
2861 }
2862 }
2863 /* XXX could free buffered stream returned as result here. */
2864 set_buffered_stream (bash_input.location.buffered_fd, saver->bstream);
2865 }
2866 #endif /* BUFFERED_INPUT */
2867
2868 line_number = saver->line;
2869
2870 FREE (saver->bash_input.name);
2871 free (saver);
2872 }
2873 }
2874
2875 /* Return 1 if a stream of type TYPE is saved on the stack. */
2876 int
2877 stream_on_stack (type)
2878 enum stream_type type;
2879 {
2880 register STREAM_SAVER *s;
2881
2882 for (s = stream_list; s; s = s->next)
2883 if (s->bash_input.type == type)
2884 return 1;
2885 return 0;
2886 }
2887
2888 /* Save the current token state and return it in a malloced array. */
2889 int *
2890 save_token_state ()
2891 {
2892 int *ret;
2893
2894 ret = (int *)xmalloc (3 * sizeof (int));
2895 ret[0] = last_read_token;
2896 ret[1] = token_before_that;
2897 ret[2] = two_tokens_ago;
2898 return ret;
2899 }
2900
2901 void
2902 restore_token_state (ts)
2903 int *ts;
2904 {
2905 if (ts == 0)
2906 return;
2907 last_read_token = ts[0];
2908 token_before_that = ts[1];
2909 two_tokens_ago = ts[2];
2910 }
2911
2912 /*
2913 * This is used to inhibit alias expansion and reserved word recognition
2914 * inside case statement pattern lists. A `case statement pattern list' is:
2915 *
2916 * everything between the `in' in a `case word in' and the next ')'
2917 * or `esac'
2918 * everything between a `;;' and the next `)' or `esac'
2919 */
2920
2921 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
2922
2923 #if !defined (ALIAS)
2924 typedef void *alias_t;
2925 #endif
2926
2927 #define END_OF_ALIAS 0
2928
2929 /*
2930 * Pseudo-global variables used in implementing token-wise alias expansion.
2931 */
2932
2933 /*
2934 * Pushing and popping strings. This works together with shell_getc to
2935 * implement alias expansion on a per-token basis.
2936 */
2937
2938 typedef struct string_saver {
2939 struct string_saver *next;
2940 int expand_alias; /* Value to set expand_alias to when string is popped. */
2941 char *saved_line;
2942 #if defined (ALIAS)
2943 alias_t *expander; /* alias that caused this line to be pushed. */
2944 #endif
2945 int saved_line_size, saved_line_index, saved_line_terminator;
2946 } STRING_SAVER;
2947
2948 STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
2949
2950 /*
2951 * Push the current shell_input_line onto a stack of such lines and make S
2952 * the current input. Used when expanding aliases. EXPAND is used to set
2953 * the value of expand_next_token when the string is popped, so that the
2954 * word after the alias in the original line is handled correctly when the
2955 * alias expands to multiple words. TOKEN is the token that was expanded
2956 * into S; it is saved and used to prevent infinite recursive expansion.
2957 */
2958 static void
2959 push_string (s, expand, ap)
2960 char *s;
2961 int expand;
2962 alias_t *ap;
2963 {
2964 STRING_SAVER *temp = (STRING_SAVER *)xmalloc (sizeof (STRING_SAVER));
2965
2966 temp->expand_alias = expand;
2967 temp->saved_line = shell_input_line;
2968 temp->saved_line_size = shell_input_line_size;
2969 temp->saved_line_index = shell_input_line_index;
2970 temp->saved_line_terminator = shell_input_line_terminator;
2971 #if defined (ALIAS)
2972 temp->expander = ap;
2973 #endif
2974 temp->next = pushed_string_list;
2975 pushed_string_list = temp;
2976
2977 #if defined (ALIAS)
2978 if (ap)
2979 ap->flags |= AL_BEINGEXPANDED;
2980 #endif
2981
2982 shell_input_line = s;
2983 shell_input_line_size = strlen (s);
2984 shell_input_line_index = 0;
2985 shell_input_line_terminator = '\0';
2986 parser_state &= ~PST_ALEXPNEXT;
2987
2988 set_line_mbstate ();
2989 }
2990
2991 /*
2992 * Make the top of the pushed_string stack be the current shell input.
2993 * Only called when there is something on the stack. Called from shell_getc
2994 * when it thinks it has consumed the string generated by an alias expansion
2995 * and needs to return to the original input line.
2996 */
2997 static void
2998 pop_string ()
2999 {
3000 STRING_SAVER *t;
3001
3002 FREE (shell_input_line);
3003 shell_input_line = pushed_string_list->saved_line;
3004 shell_input_line_index = pushed_string_list->saved_line_index;
3005 shell_input_line_size = pushed_string_list->saved_line_size;
3006 shell_input_line_terminator = pushed_string_list->saved_line_terminator;
3007
3008 if (pushed_string_list->expand_alias)
3009 parser_state |= PST_ALEXPNEXT;
3010 else
3011 parser_state &= ~PST_ALEXPNEXT;
3012
3013 t = pushed_string_list;
3014 pushed_string_list = pushed_string_list->next;
3015
3016 #if defined (ALIAS)
3017 if (t->expander)
3018 t->expander->flags &= ~AL_BEINGEXPANDED;
3019 #endif
3020
3021 free ((char *)t);
3022
3023 set_line_mbstate ();
3024 }
3025
3026 static void
3027 free_string_list ()
3028 {
3029 register STRING_SAVER *t, *t1;
3030
3031 for (t = pushed_string_list; t; )
3032 {
3033 t1 = t->next;
3034 FREE (t->saved_line);
3035 #if defined (ALIAS)
3036 if (t->expander)
3037 t->expander->flags &= ~AL_BEINGEXPANDED;
3038 #endif
3039 free ((char *)t);
3040 t = t1;
3041 }
3042 pushed_string_list = (STRING_SAVER *)NULL;
3043 }
3044
3045 #endif /* ALIAS || DPAREN_ARITHMETIC */
3046
3047 /* Return a line of text, taken from wherever yylex () reads input.
3048 If there is no more input, then we return NULL. If REMOVE_QUOTED_NEWLINE
3049 is non-zero, we remove unquoted \<newline> pairs. This is used by
3050 read_secondary_line to read here documents. */
3051 static char *
3052 read_a_line (remove_quoted_newline)
3053 int remove_quoted_newline;
3054 {
3055 static char *line_buffer = (char *)NULL;
3056 static int buffer_size = 0;
3057 int indx = 0, c, peekc, pass_next;
3058
3059 #if defined (READLINE)
3060 if (interactive && bash_input.type != st_string && no_line_editing)
3061 #else
3062 if (interactive && bash_input.type != st_string)
3063 #endif
3064 print_prompt ();
3065
3066 pass_next = 0;
3067 while (1)
3068 {
3069 c = yy_getc ();
3070
3071 /* Allow immediate exit if interrupted during input. */
3072 QUIT;
3073
3074 /* Ignore null bytes in input. */
3075 if (c == 0)
3076 {
3077 #if 0
3078 internal_warning ("read_a_line: ignored null byte in input");
3079 #endif
3080 continue;
3081 }
3082
3083 /* If there is no more input, then we return NULL. */
3084 if (c == EOF)
3085 {
3086 if (interactive && bash_input.type == st_stream)
3087 clearerr (stdin);
3088 if (indx == 0)
3089 return ((char *)NULL);
3090 c = '\n';
3091 }
3092
3093 /* `+2' in case the final character in the buffer is a newline. */
3094 RESIZE_MALLOCED_BUFFER (line_buffer, indx, 2, buffer_size, 128);
3095
3096 /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
3097 here document with an unquoted delimiter. In this case,
3098 the line will be expanded as if it were in double quotes.
3099 We allow a backslash to escape the next character, but we
3100 need to treat the backslash specially only if a backslash
3101 quoting a backslash-newline pair appears in the line. */
3102 if (pass_next)
3103 {
3104 line_buffer[indx++] = c;
3105 pass_next = 0;
3106 }
3107 else if (c == '\\' && remove_quoted_newline)
3108 {
3109 peekc = yy_getc ();
3110 if (peekc == '\n')
3111 continue; /* Make the unquoted \<newline> pair disappear. */
3112 else
3113 {
3114 yy_ungetc (peekc);
3115 pass_next = 1;
3116 line_buffer[indx++] = c; /* Preserve the backslash. */
3117 }
3118 }
3119 else
3120 line_buffer[indx++] = c;
3121
3122 if (c == '\n')
3123 {
3124 line_buffer[indx] = '\0';
3125 return (line_buffer);
3126 }
3127 }
3128 }
3129
3130 /* Return a line as in read_a_line (), but insure that the prompt is
3131 the secondary prompt. This is used to read the lines of a here
3132 document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
3133 newlines quoted with backslashes while reading the line. It is
3134 non-zero unless the delimiter of the here document was quoted. */
3135 char *
3136 read_secondary_line (remove_quoted_newline)
3137 int remove_quoted_newline;
3138 {
3139 prompt_string_pointer = &ps2_prompt;
3140 prompt_again ();
3141 return (read_a_line (remove_quoted_newline));
3142 }
3143
3144 /* **************************************************************** */
3145 /* */
3146 /* YYLEX () */
3147 /* */
3148 /* **************************************************************** */
3149
3150 /* Reserved words. These are only recognized as the first word of a
3151 command. */
3152 STRING_INT_ALIST word_token_alist[] = {
3153 { "if", IF },
3154 { "then", THEN },
3155 { "else", ELSE },
3156 { "elif", ELIF },
3157 { "fi", FI },
3158 { "case", CASE },
3159 { "esac", ESAC },
3160 { "for", FOR },
3161 #if defined (SELECT_COMMAND)
3162 { "select", SELECT },
3163 #endif
3164 { "while", WHILE },
3165 { "until", UNTIL },
3166 { "do", DO },
3167 { "done", DONE },
3168 { "in", IN },
3169 { "function", FUNCTION },
3170 #if defined (COMMAND_TIMING)
3171 { "time", TIME },
3172 #endif
3173 { "{", '{' },
3174 { "}", '}' },
3175 { "!", BANG },
3176 #if defined (COND_COMMAND)
3177 { "[[", COND_START },
3178 { "]]", COND_END },
3179 #endif
3180 { (char *)NULL, 0}
3181 };
3182
3183 /* other tokens that can be returned by read_token() */
3184 STRING_INT_ALIST other_token_alist[] = {
3185 /* Multiple-character tokens with special values */
3186 { "-p", TIMEOPT },
3187 { "&&", AND_AND },
3188 { "||", OR_OR },
3189 { ">>", GREATER_GREATER },
3190 { "<<", LESS_LESS },
3191 { "<&", LESS_AND },
3192 { ">&", GREATER_AND },
3193 { ";;", SEMI_SEMI },
3194 { "<<-", LESS_LESS_MINUS },
3195 { "<<<", LESS_LESS_LESS },
3196 { "&>", AND_GREATER },
3197 { "<>", LESS_GREATER },
3198 { ">|", GREATER_BAR },
3199 { "EOF", yacc_EOF },
3200 /* Tokens whose value is the character itself */
3201 { ">", '>' },
3202 { "<", '<' },
3203 { "-", '-' },
3204 { "{", '{' },
3205 { "}", '}' },
3206 { ";", ';' },
3207 { "(", '(' },
3208 { ")", ')' },
3209 { "|", '|' },
3210 { "&", '&' },
3211 { "newline", '\n' },
3212 { (char *)NULL, 0}
3213 };
3214
3215 /* others not listed here:
3216 WORD look at yylval.word
3217 ASSIGNMENT_WORD look at yylval.word
3218 NUMBER look at yylval.number
3219 ARITH_CMD look at yylval.word_list
3220 ARITH_FOR_EXPRS look at yylval.word_list
3221 COND_CMD look at yylval.command
3222 */
3223
3224 /* These are used by read_token_word, but appear up here so that shell_getc
3225 can use them to decide when to add otherwise blank lines to the history. */
3226
3227 /* The primary delimiter stack. */
3228 struct dstack dstack = { (char *)NULL, 0, 0 };
3229
3230 /* A temporary delimiter stack to be used when decoding prompt strings.
3231 This is needed because command substitutions in prompt strings (e.g., PS2)
3232 can screw up the parser's quoting state. */
3233 static struct dstack temp_dstack = { (char *)NULL, 0, 0 };
3234
3235 /* Macro for accessing the top delimiter on the stack. Returns the
3236 delimiter or zero if none. */
3237 #define current_delimiter(ds) \
3238 (ds.delimiter_depth ? ds.delimiters[ds.delimiter_depth - 1] : 0)
3239
3240 #define push_delimiter(ds, character) \
3241 do \
3242 { \
3243 if (ds.delimiter_depth + 2 > ds.delimiter_space) \
3244 ds.delimiters = (char *)xrealloc \
3245 (ds.delimiters, (ds.delimiter_space += 10) * sizeof (char)); \
3246 ds.delimiters[ds.delimiter_depth] = character; \
3247 ds.delimiter_depth++; \
3248 } \
3249 while (0)
3250
3251 #define pop_delimiter(ds) ds.delimiter_depth--
3252
3253 /* Return the next shell input character. This always reads characters
3254 from shell_input_line; when that line is exhausted, it is time to
3255 read the next line. This is called by read_token when the shell is
3256 processing normal command input. */
3257
3258 /* This implements one-character lookahead/lookbehind across physical input
3259 lines, to avoid something being lost because it's pushed back with
3260 shell_ungetc when we're at the start of a line. */
3261 static int eol_ungetc_lookahead = 0;
3262
3263 static int
3264 shell_getc (remove_quoted_newline)
3265 int remove_quoted_newline;
3266 {
3267 register int i;
3268 int c;
3269 unsigned char uc;
3270 static int mustpop = 0;
3271
3272 QUIT;
3273
3274 if (eol_ungetc_lookahead)
3275 {
3276 c = eol_ungetc_lookahead;
3277 eol_ungetc_lookahead = 0;
3278 return (c);
3279 }
3280
3281 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
3282 /* If shell_input_line[shell_input_line_index] == 0, but there is
3283 something on the pushed list of strings, then we don't want to go
3284 off and get another line. We let the code down below handle it. */
3285
3286 if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
3287 (pushed_string_list == (STRING_SAVER *)NULL)))
3288 #else /* !ALIAS && !DPAREN_ARITHMETIC */
3289 if (!shell_input_line || !shell_input_line[shell_input_line_index])
3290 #endif /* !ALIAS && !DPAREN_ARITHMETIC */
3291 {
3292 line_number++;
3293
3294 restart_read:
3295
3296 /* Allow immediate exit if interrupted during input. */
3297 QUIT;
3298
3299 i = 0;
3300 shell_input_line_terminator = 0;
3301
3302 #if defined (JOB_CONTROL)
3303 /* This can cause a problem when reading a command as the result
3304 of a trap, when the trap is called from flush_child. This call
3305 had better not cause jobs to disappear from the job table in
3306 that case, or we will have big trouble. */
3307 notify_and_cleanup ();
3308 #else /* !JOB_CONTROL */
3309 cleanup_dead_jobs ();
3310 #endif /* !JOB_CONTROL */
3311
3312 #if defined (READLINE)
3313 if (interactive && bash_input.type != st_string && no_line_editing)
3314 #else
3315 if (interactive && bash_input.type != st_string)
3316 #endif
3317 print_prompt ();
3318
3319 if (bash_input.type == st_stream)
3320 clearerr (stdin);
3321
3322 while (1)
3323 {
3324 c = yy_getc ();
3325
3326 /* Allow immediate exit if interrupted during input. */
3327 QUIT;
3328
3329 if (c == '\0')
3330 {
3331 #if 0
3332 internal_warning ("shell_getc: ignored null byte in input");
3333 #endif
3334 continue;
3335 }
3336
3337 RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
3338
3339 if (c == EOF)
3340 {
3341 if (bash_input.type == st_stream)
3342 clearerr (stdin);
3343
3344 if (i == 0)
3345 shell_input_line_terminator = EOF;
3346
3347 shell_input_line[i] = '\0';
3348 break;
3349 }
3350
3351 shell_input_line[i++] = c;
3352
3353 if (c == '\n')
3354 {
3355 shell_input_line[--i] = '\0';
3356 current_command_line_count++;
3357 break;
3358 }
3359 }
3360
3361 shell_input_line_index = 0;
3362 shell_input_line_len = i; /* == strlen (shell_input_line) */
3363
3364 set_line_mbstate ();
3365
3366 #if defined (HISTORY)
3367 if (remember_on_history && shell_input_line && shell_input_line[0])
3368 {
3369 char *expansions;
3370 # if defined (BANG_HISTORY)
3371 int old_hist;
3372
3373 /* If the current delimiter is a single quote, we should not be
3374 performing history expansion, even if we're on a different
3375 line from the original single quote. */
3376 old_hist = history_expansion_inhibited;
3377 if (current_delimiter (dstack) == '\'')
3378 history_expansion_inhibited = 1;
3379 # endif
3380 expansions = pre_process_line (shell_input_line, 1, 1);
3381 # if defined (BANG_HISTORY)
3382 history_expansion_inhibited = old_hist;
3383 # endif
3384 if (expansions != shell_input_line)
3385 {
3386 free (shell_input_line);
3387 shell_input_line = expansions;
3388 shell_input_line_len = shell_input_line ?
3389 strlen (shell_input_line) : 0;
3390 if (!shell_input_line_len)
3391 current_command_line_count--;
3392
3393 /* We have to force the xrealloc below because we don't know
3394 the true allocated size of shell_input_line anymore. */
3395 shell_input_line_size = shell_input_line_len;
3396
3397 set_line_mbstate ();
3398 }
3399 }
3400 /* Try to do something intelligent with blank lines encountered while
3401 entering multi-line commands. XXX - this is grotesque */
3402 else if (remember_on_history && shell_input_line &&
3403 shell_input_line[0] == '\0' &&
3404 current_command_line_count > 1)
3405 {
3406 if (current_delimiter (dstack))
3407 /* We know shell_input_line[0] == 0 and we're reading some sort of
3408 quoted string. This means we've got a line consisting of only
3409 a newline in a quoted string. We want to make sure this line
3410 gets added to the history. */
3411 maybe_add_history (shell_input_line);
3412 else
3413 {
3414 char *hdcs;
3415 hdcs = history_delimiting_chars ();
3416 if (hdcs && hdcs[0] == ';')
3417 maybe_add_history (shell_input_line);
3418 }
3419 }
3420
3421 #endif /* HISTORY */
3422
3423 if (shell_input_line)
3424 {
3425 /* Lines that signify the end of the shell's input should not be
3426 echoed. */
3427 if (echo_input_at_read && (shell_input_line[0] ||
3428 shell_input_line_terminator != EOF))
3429 fprintf (stderr, "%s\n", shell_input_line);
3430 }
3431 else
3432 {
3433 shell_input_line_size = 0;
3434 prompt_string_pointer = &current_prompt_string;
3435 prompt_again ();
3436 goto restart_read;
3437 }
3438
3439 /* Add the newline to the end of this string, iff the string does
3440 not already end in an EOF character. */
3441 if (shell_input_line_terminator != EOF)
3442 {
3443 if (shell_input_line_len + 3 > shell_input_line_size)
3444 shell_input_line = (char *)xrealloc (shell_input_line,
3445 1 + (shell_input_line_size += 2));
3446
3447 shell_input_line[shell_input_line_len] = '\n';
3448 shell_input_line[shell_input_line_len + 1] = '\0';
3449
3450 set_line_mbstate ();
3451 }
3452 }
3453
3454 uc = shell_input_line[shell_input_line_index];
3455
3456 if (uc)
3457 shell_input_line_index++;
3458
3459 if MBTEST(uc == '\\' && remove_quoted_newline && shell_input_line[shell_input_line_index] == '\n')
3460 {
3461 prompt_again ();
3462 line_number++;
3463 goto restart_read;
3464 }
3465
3466 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
3467 /* If UC is NULL, we have reached the end of the current input string. If
3468 pushed_string_list is non-empty, it's time to pop to the previous string
3469 because we have fully consumed the result of the last alias expansion.
3470 Do it transparently; just return the next character of the string popped
3471 to. */
3472 if (!uc && (pushed_string_list != (STRING_SAVER *)NULL))
3473 {
3474 if (mustpop)
3475 {
3476 pop_string ();
3477 uc = shell_input_line[shell_input_line_index];
3478 if (uc)
3479 shell_input_line_index++;
3480 mustpop--;
3481 }
3482 else
3483 {
3484 mustpop++;
3485 uc = ' ';
3486 }
3487 }
3488 #endif /* ALIAS || DPAREN_ARITHMETIC */
3489
3490 if (!uc && shell_input_line_terminator == EOF)
3491 return ((shell_input_line_index != 0) ? '\n' : EOF);
3492
3493 return (uc);
3494 }
3495
3496 /* Put C back into the input for the shell. This might need changes for
3497 HANDLE_MULTIBYTE around EOLs. Since we (currently) never push back a
3498 character different than we read, shell_input_line_property doesn't need
3499 to change when manipulating shell_input_line. The define for
3500 last_shell_getc_is_singlebyte should take care of it, though. */
3501 static void
3502 shell_ungetc (c)
3503 int c;
3504 {
3505 if (shell_input_line && shell_input_line_index)
3506 shell_input_line[--shell_input_line_index] = c;
3507 else
3508 eol_ungetc_lookahead = c;
3509 }
3510
3511 #ifdef INCLUDE_UNUSED
3512 /* Back the input pointer up by one, effectively `ungetting' a character. */
3513 static void
3514 shell_ungetchar ()
3515 {
3516 if (shell_input_line && shell_input_line_index)
3517 shell_input_line_index--;
3518 }
3519 #endif
3520
3521 /* Discard input until CHARACTER is seen, then push that character back
3522 onto the input stream. */
3523 static void
3524 discard_until (character)
3525 int character;
3526 {
3527 int c;
3528
3529 while ((c = shell_getc (0)) != EOF && c != character)
3530 ;
3531
3532 if (c != EOF)
3533 shell_ungetc (c);
3534 }
3535
3536 void
3537 execute_prompt_command (command)
3538 char *command;
3539 {
3540 sh_builtin_func_t *temp_last, *temp_this;
3541 char *last_lastarg;
3542 int temp_exit_value, temp_eof_encountered;
3543
3544 temp_last = last_shell_builtin;
3545 temp_this = this_shell_builtin;
3546 temp_exit_value = last_command_exit_value;
3547 temp_eof_encountered = eof_encountered;
3548 last_lastarg = get_string_value ("_");
3549 if (last_lastarg)
3550 last_lastarg = savestring (last_lastarg);
3551
3552 parse_and_execute (savestring (command), "PROMPT_COMMAND", SEVAL_NONINT|SEVAL_NOHIST);
3553
3554 last_shell_builtin = temp_last;
3555 this_shell_builtin = temp_this;
3556 last_command_exit_value = temp_exit_value;
3557 eof_encountered = temp_eof_encountered;
3558
3559 bind_variable ("_", last_lastarg);
3560 FREE (last_lastarg);
3561
3562 if (token_to_read == '\n') /* reset_parser was called */
3563 token_to_read = 0;
3564 }
3565
3566 /* Place to remember the token. We try to keep the buffer
3567 at a reasonable size, but it can grow. */
3568 static char *token = (char *)NULL;
3569
3570 /* Current size of the token buffer. */
3571 static int token_buffer_size;
3572
3573 /* Command to read_token () explaining what we want it to do. */
3574 #define READ 0
3575 #define RESET 1
3576 #define prompt_is_ps1 \
3577 (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
3578
3579 /* Function for yyparse to call. yylex keeps track of
3580 the last two tokens read, and calls read_token. */
3581 static int
3582 yylex ()
3583 {
3584 if (interactive && (current_token == 0 || current_token == '\n'))
3585 {
3586 /* Before we print a prompt, we might have to check mailboxes.
3587 We do this only if it is time to do so. Notice that only here
3588 is the mail alarm reset; nothing takes place in check_mail ()
3589 except the checking of mail. Please don't change this. */
3590 if (prompt_is_ps1 && time_to_check_mail ())
3591 {
3592 check_mail ();
3593 reset_mail_timer ();
3594 }
3595
3596 /* Avoid printing a prompt if we're not going to read anything, e.g.
3597 after resetting the parser with read_token (RESET). */
3598 if (token_to_read == 0 && interactive)
3599 prompt_again ();
3600 }
3601
3602 two_tokens_ago = token_before_that;
3603 token_before_that = last_read_token;
3604 last_read_token = current_token;
3605 current_token = read_token (READ);
3606 return (current_token);
3607 }
3608
3609 /* When non-zero, we have read the required tokens
3610 which allow ESAC to be the next one read. */
3611 static int esacs_needed_count;
3612
3613 void
3614 gather_here_documents ()
3615 {
3616 int r = 0;
3617 while (need_here_doc)
3618 {
3619 make_here_document (redir_stack[r++]);
3620 need_here_doc--;
3621 }
3622 }
3623
3624 /* When non-zero, an open-brace used to create a group is awaiting a close
3625 brace partner. */
3626 static int open_brace_count;
3627
3628 #define command_token_position(token) \
3629 (((token) == ASSIGNMENT_WORD) || \
3630 ((token) != SEMI_SEMI && reserved_word_acceptable(token)))
3631
3632 #define assignment_acceptable(token) command_token_position(token) && \
3633 ((parser_state & PST_CASEPAT) == 0)
3634
3635 /* Check to see if TOKEN is a reserved word and return the token
3636 value if it is. */
3637 #define CHECK_FOR_RESERVED_WORD(tok) \
3638 do { \
3639 if (!dollar_present && !quoted && \
3640 reserved_word_acceptable (last_read_token)) \
3641 { \
3642 int i; \
3643 for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
3644 if (STREQ (tok, word_token_alist[i].word)) \
3645 { \
3646 if ((parser_state & PST_CASEPAT) && (word_token_alist[i].token != ESAC)) \
3647 break; \
3648 if (word_token_alist[i].token == TIME) \
3649 break; \
3650 if (word_token_alist[i].token == ESAC) \
3651 parser_state &= ~(PST_CASEPAT|PST_CASESTMT); \
3652 else if (word_token_alist[i].token == CASE) \
3653 parser_state |= PST_CASESTMT; \
3654 else if (word_token_alist[i].token == COND_END) \
3655 parser_state &= ~(PST_CONDCMD|PST_CONDEXPR); \
3656 else if (word_token_alist[i].token == COND_START) \
3657 parser_state |= PST_CONDCMD; \
3658 else if (word_token_alist[i].token == '{') \
3659 open_brace_count++; \
3660 else if (word_token_alist[i].token == '}' && open_brace_count) \
3661 open_brace_count--; \
3662 return (word_token_alist[i].token); \
3663 } \
3664 } \
3665 } while (0)
3666
3667 #if defined (ALIAS)
3668
3669 /* OK, we have a token. Let's try to alias expand it, if (and only if)
3670 it's eligible.
3671
3672 It is eligible for expansion if EXPAND_ALIASES is set, and
3673 the token is unquoted and the last token read was a command
3674 separator (or expand_next_token is set), and we are currently
3675 processing an alias (pushed_string_list is non-empty) and this
3676 token is not the same as the current or any previously
3677 processed alias.
3678
3679 Special cases that disqualify:
3680 In a pattern list in a case statement (parser_state & PST_CASEPAT). */
3681 static int
3682 alias_expand_token (tokstr)
3683 char *tokstr;
3684 {
3685 char *expanded;
3686 alias_t *ap;
3687
3688 if (((parser_state & PST_ALEXPNEXT) || command_token_position (last_read_token)) &&
3689 (parser_state & PST_CASEPAT) == 0)
3690 {
3691 ap = find_alias (tokstr);
3692
3693 /* Currently expanding this token. */
3694 if (ap && (ap->flags & AL_BEINGEXPANDED))
3695 return (NO_EXPANSION);
3696
3697 expanded = ap ? savestring (ap->value) : (char *)NULL;
3698 if (expanded)
3699 {
3700 push_string (expanded, ap->flags & AL_EXPANDNEXT, ap);
3701 return (RE_READ_TOKEN);
3702 }
3703 else
3704 /* This is an eligible token that does not have an expansion. */
3705 return (NO_EXPANSION);
3706 }
3707 return (NO_EXPANSION);
3708 }
3709 #endif /* ALIAS */
3710
3711 static int
3712 time_command_acceptable ()
3713 {
3714 #if defined (COMMAND_TIMING)
3715 switch (last_read_token)
3716 {
3717 case 0:
3718 case ';':
3719 case '\n':
3720 case AND_AND:
3721 case OR_OR:
3722 case '&':
3723 case DO:
3724 case THEN:
3725 case ELSE:
3726 case '{': /* } */
3727 case '(': /* ) */
3728 return 1;
3729 default:
3730 return 0;
3731 }
3732 #else
3733 return 0;
3734 #endif /* COMMAND_TIMING */
3735 }
3736
3737 /* Handle special cases of token recognition:
3738 IN is recognized if the last token was WORD and the token
3739 before that was FOR or CASE or SELECT.
3740
3741 DO is recognized if the last token was WORD and the token
3742 before that was FOR or SELECT.
3743
3744 ESAC is recognized if the last token caused `esacs_needed_count'
3745 to be set
3746
3747 `{' is recognized if the last token as WORD and the token
3748 before that was FUNCTION, or if we just parsed an arithmetic
3749 `for' command.
3750
3751 `}' is recognized if there is an unclosed `{' present.
3752
3753 `-p' is returned as TIMEOPT if the last read token was TIME.
3754
3755 ']]' is returned as COND_END if the parser is currently parsing
3756 a conditional expression ((parser_state & PST_CONDEXPR) != 0)
3757
3758 `time' is returned as TIME if and only if it is immediately
3759 preceded by one of `;', `\n', `||', `&&', or `&'.
3760 */
3761
3762 static int
3763 special_case_tokens (tokstr)
3764 char *tokstr;
3765 {
3766 if ((last_read_token == WORD) &&
3767 #if defined (SELECT_COMMAND)
3768 ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
3769 #else
3770 ((token_before_that == FOR) || (token_before_that == CASE)) &&
3771 #endif
3772 (tokstr[0] == 'i' && tokstr[1] == 'n' && tokstr[2] == 0))
3773 {
3774 if (token_before_that == CASE)
3775 {
3776 parser_state |= PST_CASEPAT;
3777 esacs_needed_count++;
3778 }
3779 return (IN);
3780 }
3781
3782 if (last_read_token == WORD &&
3783 #if defined (SELECT_COMMAND)
3784 (token_before_that == FOR || token_before_that == SELECT) &&
3785 #else
3786 (token_before_that == FOR) &&
3787 #endif
3788 (tokstr[0] == 'd' && tokstr[1] == 'o' && tokstr[2] == '\0'))
3789 return (DO);
3790
3791 /* Ditto for ESAC in the CASE case.
3792 Specifically, this handles "case word in esac", which is a legal
3793 construct, certainly because someone will pass an empty arg to the
3794 case construct, and we don't want it to barf. Of course, we should
3795 insist that the case construct has at least one pattern in it, but
3796 the designers disagree. */
3797 if (esacs_needed_count)
3798 {
3799 esacs_needed_count--;
3800 if (STREQ (tokstr, "esac"))
3801 {
3802 parser_state &= ~PST_CASEPAT;
3803 return (ESAC);
3804 }
3805 }
3806
3807 /* The start of a shell function definition. */
3808 if (parser_state & PST_ALLOWOPNBRC)
3809 {
3810 parser_state &= ~PST_ALLOWOPNBRC;
3811 if (tokstr[0] == '{' && tokstr[1] == '\0') /* } */
3812 {
3813 open_brace_count++;
3814 function_bstart = line_number;
3815 return ('{'); /* } */
3816 }
3817 }
3818
3819 /* We allow a `do' after a for ((...)) without an intervening
3820 list_terminator */
3821 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == 'd' && tokstr[1] == 'o' && !tokstr[2])
3822 return (DO);
3823 if (last_read_token == ARITH_FOR_EXPRS && tokstr[0] == '{' && tokstr[1] == '\0') /* } */
3824 {
3825 open_brace_count++;
3826 return ('{'); /* } */
3827 }
3828
3829 if (open_brace_count && reserved_word_acceptable (last_read_token) && tokstr[0] == '}' && !tokstr[1])
3830 {
3831 open_brace_count--; /* { */
3832 return ('}');
3833 }
3834
3835 #if defined (COMMAND_TIMING)
3836 /* Handle -p after `time'. */
3837 if (last_read_token == TIME && tokstr[0] == '-' && tokstr[1] == 'p' && !tokstr[2])
3838 return (TIMEOPT);
3839 #endif
3840
3841 #if defined (COMMAND_TIMING)
3842 if (STREQ (token, "time") && ((parser_state & PST_CASEPAT) == 0) && time_command_acceptable ())
3843 return (TIME);
3844 #endif /* COMMAND_TIMING */
3845
3846 #if defined (COND_COMMAND) /* [[ */
3847 if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0')
3848 return (COND_END);
3849 #endif
3850
3851 return (-1);
3852 }
3853
3854 /* Called from shell.c when Control-C is typed at top level. Or
3855 by the error rule at top level. */
3856 void
3857 reset_parser ()
3858 {
3859 dstack.delimiter_depth = 0; /* No delimiters found so far. */
3860 open_brace_count = 0;
3861
3862 parser_state = 0;
3863
3864 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
3865 if (pushed_string_list)
3866 free_string_list ();
3867 #endif /* ALIAS || DPAREN_ARITHMETIC */
3868
3869 if (shell_input_line)
3870 {
3871 free (shell_input_line);
3872 shell_input_line = (char *)NULL;
3873 shell_input_line_size = shell_input_line_index = 0;
3874 }
3875
3876 FREE (word_desc_to_read);
3877 word_desc_to_read = (WORD_DESC *)NULL;
3878
3879 last_read_token = '\n';
3880 token_to_read = '\n';
3881 }
3882
3883 /* Read the next token. Command can be READ (normal operation) or
3884 RESET (to normalize state). */
3885 static int
3886 read_token (command)
3887 int command;
3888 {
3889 int character; /* Current character. */
3890 int peek_char; /* Temporary look-ahead character. */
3891 int result; /* The thing to return. */
3892
3893 if (command == RESET)
3894 {
3895 reset_parser ();
3896 return ('\n');
3897 }
3898
3899 if (token_to_read)
3900 {
3901 result = token_to_read;
3902 if (token_to_read == WORD || token_to_read == ASSIGNMENT_WORD)
3903 {
3904 yylval.word = word_desc_to_read;
3905 word_desc_to_read = (WORD_DESC *)NULL;
3906 }
3907 token_to_read = 0;
3908 return (result);
3909 }
3910
3911 #if defined (COND_COMMAND)
3912 if ((parser_state & (PST_CONDCMD|PST_CONDEXPR)) == PST_CONDCMD)
3913 {
3914 cond_lineno = line_number;
3915 parser_state |= PST_CONDEXPR;
3916 yylval.command = parse_cond_command ();
3917 if (cond_token != COND_END)
3918 {
3919 cond_error ();
3920 return (-1);
3921 }
3922 token_to_read = COND_END;
3923 parser_state &= ~(PST_CONDEXPR|PST_CONDCMD);
3924 return (COND_CMD);
3925 }
3926 #endif
3927
3928 #if defined (ALIAS)
3929 /* This is a place to jump back to once we have successfully expanded a
3930 token with an alias and pushed the string with push_string () */
3931 re_read_token:
3932 #endif /* ALIAS */
3933
3934 /* Read a single word from input. Start by skipping blanks. */
3935 while ((character = shell_getc (1)) != EOF && whitespace (character))
3936 ;
3937
3938 if (character == EOF)
3939 {
3940 EOF_Reached = 1;
3941 return (yacc_EOF);
3942 }
3943
3944 if MBTEST(character == '#' && (!interactive || interactive_comments))
3945 {
3946 /* A comment. Discard until EOL or EOF, and then return a newline. */
3947 discard_until ('\n');
3948 shell_getc (0);
3949 character = '\n'; /* this will take the next if statement and return. */
3950 }
3951
3952 if (character == '\n')
3953 {
3954 /* If we're about to return an unquoted newline, we can go and collect
3955 the text of any pending here document. */
3956 if (need_here_doc)
3957 gather_here_documents ();
3958
3959 #if defined (ALIAS)
3960 parser_state &= ~PST_ALEXPNEXT;
3961 #endif /* ALIAS */
3962
3963 return (character);
3964 }
3965
3966 /* Shell meta-characters. */
3967 if MBTEST(shellmeta (character) && ((parser_state & PST_DBLPAREN) == 0))
3968 {
3969 #if defined (ALIAS)
3970 /* Turn off alias tokenization iff this character sequence would
3971 not leave us ready to read a command. */
3972 if (character == '<' || character == '>')
3973 parser_state &= ~PST_ALEXPNEXT;
3974 #endif /* ALIAS */
3975
3976 peek_char = shell_getc (1);
3977 if (character == peek_char)
3978 {
3979 switch (character)
3980 {
3981 case '<':
3982 /* If '<' then we could be at "<<" or at "<<-". We have to
3983 look ahead one more character. */
3984 peek_char = shell_getc (1);
3985 if (peek_char == '-')
3986 return (LESS_LESS_MINUS);
3987 else if (peek_char == '<')
3988 return (LESS_LESS_LESS);
3989 else
3990 {
3991 shell_ungetc (peek_char);
3992 return (LESS_LESS);
3993 }
3994
3995 case '>':
3996 return (GREATER_GREATER);
3997
3998 case ';':
3999 parser_state |= PST_CASEPAT;
4000 #if defined (ALIAS)
4001 parser_state &= ~PST_ALEXPNEXT;
4002 #endif /* ALIAS */
4003 return (SEMI_SEMI);
4004
4005 case '&':
4006 return (AND_AND);
4007
4008 case '|':
4009 return (OR_OR);
4010
4011 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
4012 case '(': /* ) */
4013 result = parse_dparen (character);
4014 if (result == -2)
4015 break;
4016 else
4017 return result;
4018 #endif
4019 }
4020 }
4021 else if MBTEST(character == '<' && peek_char == '&')
4022 return (LESS_AND);
4023 else if MBTEST(character == '>' && peek_char == '&')
4024 return (GREATER_AND);
4025 else if MBTEST(character == '<' && peek_char == '>')
4026 return (LESS_GREATER);
4027 else if MBTEST(character == '>' && peek_char == '|')
4028 return (GREATER_BAR);
4029 else if MBTEST(peek_char == '>' && character == '&')
4030 return (AND_GREATER);
4031
4032 shell_ungetc (peek_char);
4033
4034 /* If we look like we are reading the start of a function
4035 definition, then let the reader know about it so that
4036 we will do the right thing with `{'. */
4037 if MBTEST(character == ')' && last_read_token == '(' && token_before_that == WORD)
4038 {
4039 parser_state |= PST_ALLOWOPNBRC;
4040 #if defined (ALIAS)
4041 parser_state &= ~PST_ALEXPNEXT;
4042 #endif /* ALIAS */
4043 function_dstart = line_number;
4044 }
4045
4046 /* case pattern lists may be preceded by an optional left paren. If
4047 we're not trying to parse a case pattern list, the left paren
4048 indicates a subshell. */
4049 if MBTEST(character == '(' && (parser_state & PST_CASEPAT) == 0) /* ) */
4050 parser_state |= PST_SUBSHELL;
4051 /*(*/
4052 else if MBTEST((parser_state & PST_CASEPAT) && character == ')')
4053 parser_state &= ~PST_CASEPAT;
4054 /*(*/
4055 else if MBTEST((parser_state & PST_SUBSHELL) && character == ')')
4056 parser_state &= ~PST_SUBSHELL;
4057
4058 #if defined (PROCESS_SUBSTITUTION)
4059 /* Check for the constructs which introduce process substitution.
4060 Shells running in `posix mode' don't do process substitution. */
4061 if MBTEST(posixly_correct || ((character != '>' && character != '<') || peek_char != '(')) /*)*/
4062 #endif /* PROCESS_SUBSTITUTION */
4063 return (character);
4064 }
4065
4066 /* Hack <&- (close stdin) case. Also <&N- (dup and close). */
4067 if MBTEST(character == '-' && (last_read_token == LESS_AND || last_read_token == GREATER_AND))
4068 return (character);
4069
4070 /* Okay, if we got this far, we have to read a word. Read one,
4071 and then check it against the known ones. */
4072 result = read_token_word (character);
4073 #if defined (ALIAS)
4074 if (result == RE_READ_TOKEN)
4075 goto re_read_token;
4076 #endif
4077 return result;
4078 }
4079
4080 /*
4081 * Match a $(...) or other grouping construct. This has to handle embedded
4082 * quoted strings ('', ``, "") and nested constructs. It also must handle
4083 * reprompting the user, if necessary, after reading a newline (unless the
4084 * P_NONL flag is passed), and returning correct error values if it reads
4085 * EOF.
4086 */
4087 #define P_FIRSTCLOSE 0x01
4088 #define P_ALLOWESC 0x02
4089
4090 static char matched_pair_error;
4091 static char *
4092 parse_matched_pair (qc, open, close, lenp, flags)
4093 int qc; /* `"' if this construct is within double quotes */
4094 int open, close;
4095 int *lenp, flags;
4096 {
4097 int count, ch, was_dollar;
4098 int pass_next_character, nestlen, ttranslen, start_lineno;
4099 char *ret, *nestret, *ttrans;
4100 int retind, retsize;
4101
4102 count = 1;
4103 pass_next_character = was_dollar = 0;
4104
4105 ret = (char *)xmalloc (retsize = 64);
4106 retind = 0;
4107
4108 start_lineno = line_number;
4109 while (count)
4110 {
4111 ch = shell_getc ((qc != '\'' || (flags & P_ALLOWESC)) && pass_next_character == 0);
4112 if (ch == EOF)
4113 {
4114 free (ret);
4115 parser_error (start_lineno, "unexpected EOF while looking for matching `%c'", close);
4116 EOF_Reached = 1; /* XXX */
4117 return (&matched_pair_error);
4118 }
4119
4120 /* Possible reprompting. */
4121 if (ch == '\n' && interactive &&
4122 (bash_input.type == st_stdin || bash_input.type == st_stream))
4123 prompt_again ();
4124
4125 if (pass_next_character) /* last char was backslash */
4126 {
4127 pass_next_character = 0;
4128 if (qc != '\'' && ch == '\n') /* double-quoted \<newline> disappears. */
4129 {
4130 if (retind > 0) retind--; /* swallow previously-added backslash */
4131 continue;
4132 }
4133
4134 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
4135 if MBTEST(ch == CTLESC || ch == CTLNUL)
4136 ret[retind++] = CTLESC;
4137 ret[retind++] = ch;
4138 continue;
4139 }
4140 else if MBTEST(ch == CTLESC || ch == CTLNUL) /* special shell escapes */
4141 {
4142 RESIZE_MALLOCED_BUFFER (ret, retind, 2, retsize, 64);
4143 ret[retind++] = CTLESC;
4144 ret[retind++] = ch;
4145 continue;
4146 }
4147 else if MBTEST(ch == close) /* ending delimiter */
4148 count--;
4149 #if 1
4150 /* handle nested ${...} specially. */
4151 else if MBTEST(open != close && was_dollar && open == '{' && ch == open) /* } */
4152 count++;
4153 #endif
4154 else if MBTEST(((flags & P_FIRSTCLOSE) == 0) && ch == open) /* nested begin */
4155 count++;
4156
4157 /* Add this character. */
4158 RESIZE_MALLOCED_BUFFER (ret, retind, 1, retsize, 64);
4159 ret[retind++] = ch;
4160
4161 if (open == '\'') /* '' inside grouping construct */
4162 {
4163 if MBTEST((flags & P_ALLOWESC) && ch == '\\')
4164 pass_next_character++;
4165 continue;
4166 }
4167
4168 if MBTEST(ch == '\\') /* backslashes */
4169 pass_next_character++;
4170
4171 if (open != close) /* a grouping construct */
4172 {
4173 if MBTEST(shellquote (ch))
4174 {
4175 /* '', ``, or "" inside $(...) or other grouping construct. */
4176 push_delimiter (dstack, ch);
4177 if MBTEST(was_dollar && ch == '\'') /* $'...' inside group */
4178 nestret = parse_matched_pair (ch, ch, ch, &nestlen, P_ALLOWESC);
4179 else
4180 nestret = parse_matched_pair (ch, ch, ch, &nestlen, 0);
4181 pop_delimiter (dstack);
4182 if (nestret == &matched_pair_error)
4183 {
4184 free (ret);
4185 return &matched_pair_error;
4186 }
4187 if MBTEST(was_dollar && ch == '\'')
4188 {
4189 /* Translate $'...' here. */
4190 ttrans = ansiexpand (nestret, 0, nestlen - 1, &ttranslen);
4191 xfree (nestret);
4192 nestret = sh_single_quote (ttrans);
4193 free (ttrans);
4194 nestlen = strlen (nestret);
4195 retind -= 2; /* back up before the $' */
4196 }
4197 else if MBTEST(was_dollar && ch == '"')
4198 {
4199 /* Locale expand $"..." here. */
4200 ttrans = localeexpand (nestret, 0, nestlen - 1, start_lineno, &ttranslen);
4201 xfree (nestret);
4202 nestret = (char *)xmalloc (ttranslen + 3);
4203 nestret[0] = '"';
4204 strcpy (nestret + 1, ttrans);
4205 nestret[ttranslen + 1] = '"';
4206 nestret[ttranslen += 2] = '\0';
4207 free (ttrans);
4208 nestlen = ttranslen;
4209 retind -= 2; /* back up before the $" */
4210 }
4211
4212 if (nestlen)
4213 {
4214 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
4215 strcpy (ret + retind, nestret);
4216 retind += nestlen;
4217 }
4218 FREE (nestret);
4219 }
4220 }
4221 /* Parse an old-style command substitution within double quotes as a
4222 single word. */
4223 /* XXX - sh and ksh93 don't do this - XXX */
4224 else if MBTEST(open == '"' && ch == '`')
4225 {
4226 nestret = parse_matched_pair (0, '`', '`', &nestlen, 0);
4227 if (nestret == &matched_pair_error)
4228 {
4229 free (ret);
4230 return &matched_pair_error;
4231 }
4232 if (nestlen)
4233 {
4234 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
4235 strcpy (ret + retind, nestret);
4236 retind += nestlen;
4237 }
4238 FREE (nestret);
4239 }
4240 else if MBTEST(was_dollar && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */
4241 /* check for $(), $[], or ${} inside quoted string. */
4242 {
4243 if (open == ch) /* undo previous increment */
4244 count--;
4245 if (ch == '(') /* ) */
4246 nestret = parse_matched_pair (0, '(', ')', &nestlen, 0);
4247 else if (ch == '{') /* } */
4248 nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE);
4249 else if (ch == '[') /* ] */
4250 nestret = parse_matched_pair (0, '[', ']', &nestlen, 0);
4251 if (nestret == &matched_pair_error)
4252 {
4253 free (ret);
4254 return &matched_pair_error;
4255 }
4256 if (nestlen)
4257 {
4258 RESIZE_MALLOCED_BUFFER (ret, retind, nestlen, retsize, 64);
4259 strcpy (ret + retind, nestret);
4260 retind += nestlen;
4261 }
4262 FREE (nestret);
4263 }
4264 was_dollar = MBTEST(ch == '$');
4265 }
4266
4267 ret[retind] = '\0';
4268 if (lenp)
4269 *lenp = retind;
4270 return ret;
4271 }
4272
4273 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
4274 /* Parse a double-paren construct. It can be either an arithmetic
4275 command, an arithmetic `for' command, or a nested subshell. Returns
4276 the parsed token, -1 on error, or -2 if we didn't do anything and
4277 should just go on. */
4278 static int
4279 parse_dparen (c)
4280 int c;
4281 {
4282 int cmdtyp, len, sline;
4283 char *wval, *wv2;
4284 WORD_DESC *wd;
4285
4286 #if defined (ARITH_FOR_COMMAND)
4287 if (last_read_token == FOR)
4288 {
4289 arith_for_lineno = line_number;
4290 cmdtyp = parse_arith_cmd (&wval);
4291 if (cmdtyp == 1)
4292 {
4293 /* parse_arith_cmd adds quotes at the beginning and end
4294 of the string it returns; we need to take those out. */
4295 len = strlen (wval);
4296 wv2 = (char *)xmalloc (len);
4297 strncpy (wv2, wval + 1, len - 2);
4298 wv2[len - 2] = '\0';
4299 wd = make_word (wv2);
4300 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
4301 free (wval);
4302 free (wv2);
4303 return (ARITH_FOR_EXPRS);
4304 }
4305 else
4306 return -1; /* ERROR */
4307 }
4308 #endif
4309
4310 #if defined (DPAREN_ARITHMETIC)
4311 if (reserved_word_acceptable (last_read_token))
4312 {
4313 sline = line_number;
4314 cmdtyp = parse_arith_cmd (&wval);
4315 if (cmdtyp == 1) /* arithmetic command */
4316 {
4317 wd = make_word (wval);
4318 wd->flags = W_QUOTED;
4319 yylval.word_list = make_word_list (wd, (WORD_LIST *)NULL);
4320 free (wval); /* make_word copies it */
4321 return (ARITH_CMD);
4322 }
4323 else if (cmdtyp == 0) /* nested subshell */
4324 {
4325 push_string (wval, 0, (alias_t *)NULL);
4326 if ((parser_state & PST_CASEPAT) == 0)
4327 parser_state |= PST_SUBSHELL;
4328 return (c);
4329 }
4330 else /* ERROR */
4331 return -1;
4332 }
4333 #endif
4334
4335 return -2; /* XXX */
4336 }
4337
4338 /* We've seen a `(('. Look for the matching `))'. If we get it, return 1.
4339 If not, assume it's a nested subshell for backwards compatibility and
4340 return 0. In any case, put the characters we've consumed into a locally-
4341 allocated buffer and make *ep point to that buffer. Return -1 on an
4342 error, for example EOF. */
4343 static int
4344 parse_arith_cmd (ep)
4345 char **ep;
4346 {
4347 int exp_lineno, rval, c;
4348 char *ttok, *tokstr;
4349 int ttoklen;
4350
4351 exp_lineno = line_number;
4352 ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0);
4353 rval = 1;
4354 if (ttok == &matched_pair_error)
4355 return -1;
4356 /* Check that the next character is the closing right paren. If
4357 not, this is a syntax error. ( */
4358 c = shell_getc (0);
4359 if MBTEST(c != ')')
4360 rval = 0;
4361
4362 tokstr = (char *)xmalloc (ttoklen + 4);
4363
4364 /* (( ... )) -> "..." */
4365 tokstr[0] = (rval == 1) ? '"' : '(';
4366 strncpy (tokstr + 1, ttok, ttoklen - 1); /* don't copy the final `)' */
4367 if (rval == 1)
4368 {
4369 tokstr[ttoklen] = '"';
4370 tokstr[ttoklen+1] = '\0';
4371 }
4372 else
4373 {
4374 tokstr[ttoklen] = ')';
4375 tokstr[ttoklen+1] = c;
4376 tokstr[ttoklen+2] = '\0';
4377 }
4378 *ep = tokstr;
4379 FREE (ttok);
4380 return rval;
4381 }
4382 #endif /* DPAREN_ARITHMETIC || ARITH_FOR_COMMAND */
4383
4384 #if defined (COND_COMMAND)
4385 static void
4386 cond_error ()
4387 {
4388 char *etext;
4389
4390 if (EOF_Reached && cond_token != COND_ERROR) /* [[ */
4391 parser_error (cond_lineno, "unexpected EOF while looking for `]]'");
4392 else if (cond_token != COND_ERROR)
4393 {
4394 if (etext = error_token_from_token (cond_token))
4395 {
4396 parser_error (cond_lineno, "syntax error in conditional expression: unexpected token `%s'", etext);
4397 free (etext);
4398 }
4399 else
4400 parser_error (cond_lineno, "syntax error in conditional expression");
4401 }
4402 }
4403
4404 static COND_COM *
4405 cond_expr ()
4406 {
4407 return (cond_or ());
4408 }
4409
4410 static COND_COM *
4411 cond_or ()
4412 {
4413 COND_COM *l, *r;
4414
4415 l = cond_and ();
4416 if (cond_token == OR_OR)
4417 {
4418 r = cond_or ();
4419 l = make_cond_node (COND_OR, (WORD_DESC *)NULL, l, r);
4420 }
4421 return l;
4422 }
4423
4424 static COND_COM *
4425 cond_and ()
4426 {
4427 COND_COM *l, *r;
4428
4429 l = cond_term ();
4430 if (cond_token == AND_AND)
4431 {
4432 r = cond_and ();
4433 l = make_cond_node (COND_AND, (WORD_DESC *)NULL, l, r);
4434 }
4435 return l;
4436 }
4437
4438 static int
4439 cond_skip_newlines ()
4440 {
4441 while ((cond_token = read_token (READ)) == '\n')
4442 {
4443 if (interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
4444 prompt_again ();
4445 }
4446 return (cond_token);
4447 }
4448
4449 #define COND_RETURN_ERROR() \
4450 do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
4451
4452 static COND_COM *
4453 cond_term ()
4454 {
4455 WORD_DESC *op;
4456 COND_COM *term, *tleft, *tright;
4457 int tok, lineno;
4458 char *etext;
4459
4460 /* Read a token. It can be a left paren, a `!', a unary operator, or a
4461 word that should be the first argument of a binary operator. Start by
4462 skipping newlines, since this is a compound command. */
4463 tok = cond_skip_newlines ();
4464 lineno = line_number;
4465 if (tok == COND_END)
4466 {
4467 COND_RETURN_ERROR ();
4468 }
4469 else if (tok == '(')
4470 {
4471 term = cond_expr ();
4472 if (cond_token != ')')
4473 {
4474 if (term)
4475 dispose_cond_node (term); /* ( */
4476 if (etext = error_token_from_token (cond_token))
4477 {
4478 parser_error (lineno, "unexpected token `%s', expected `)'", etext);
4479 free (etext);
4480 }
4481 else
4482 parser_error (lineno, "expected `)'");
4483 COND_RETURN_ERROR ();
4484 }
4485 term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM *)NULL);
4486 (void)cond_skip_newlines ();
4487 }
4488 else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && yylval.word->word[1] == '\0')))
4489 {
4490 if (tok == WORD)
4491 dispose_word (yylval.word); /* not needed */
4492 term = cond_term ();
4493 if (term)
4494 term->flags |= CMD_INVERT_RETURN;
4495 }
4496 else if (tok == WORD && test_unop (yylval.word->word))
4497 {
4498 op = yylval.word;
4499 tok = read_token (READ);
4500 if (tok == WORD)
4501 {
4502 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4503 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
4504 }
4505 else
4506 {
4507 dispose_word (op);
4508 if (etext = error_token_from_token (tok))
4509 {
4510 parser_error (line_number, "unexpected argument `%s' to conditional unary operator", etext);
4511 free (etext);
4512 }
4513 else
4514 parser_error (line_number, "unexpected argument to conditional unary operator");
4515 COND_RETURN_ERROR ();
4516 }
4517
4518 (void)cond_skip_newlines ();
4519 }
4520 else if (tok == WORD) /* left argument to binary operator */
4521 {
4522 /* lhs */
4523 tleft = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4524
4525 /* binop */
4526 tok = read_token (READ);
4527 if (tok == WORD && test_binop (yylval.word->word))
4528 op = yylval.word;
4529 else if (tok == '<' || tok == '>')
4530 op = make_word_from_token (tok); /* ( */
4531 /* There should be a check before blindly accepting the `)' that we have
4532 seen the opening `('. */
4533 else if (tok == COND_END || tok == AND_AND || tok == OR_OR || tok == ')')
4534 {
4535 /* Special case. [[ x ]] is equivalent to [[ -n x ]], just like
4536 the test command. Similarly for [[ x && expr ]] or
4537 [[ x || expr ]] or [[ (x) ]]. */
4538 op = make_word ("-n");
4539 term = make_cond_node (COND_UNARY, op, tleft, (COND_COM *)NULL);
4540 cond_token = tok;
4541 return (term);
4542 }
4543 else
4544 {
4545 if (etext = error_token_from_token (tok))
4546 {
4547 parser_error (line_number, "unexpected token `%s', conditional binary operator expected", etext);
4548 free (etext);
4549 }
4550 else
4551 parser_error (line_number, "conditional binary operator expected");
4552 dispose_cond_node (tleft);
4553 COND_RETURN_ERROR ();
4554 }
4555
4556 /* rhs */
4557 tok = read_token (READ);
4558 if (tok == WORD)
4559 {
4560 tright = make_cond_node (COND_TERM, yylval.word, (COND_COM *)NULL, (COND_COM *)NULL);
4561 term = make_cond_node (COND_BINARY, op, tleft, tright);
4562 }
4563 else
4564 {
4565 if (etext = error_token_from_token (tok))
4566 {
4567 parser_error (line_number, "unexpected argument `%s' to conditional binary operator", etext);
4568 free (etext);
4569 }
4570 else
4571 parser_error (line_number, "unexpected argument to conditional binary operator");
4572 dispose_cond_node (tleft);
4573 dispose_word (op);
4574 COND_RETURN_ERROR ();
4575 }
4576
4577 (void)cond_skip_newlines ();
4578 }
4579 else
4580 {
4581 if (tok < 256)
4582 parser_error (line_number, "unexpected token `%c' in conditional command", tok);
4583 else if (etext = error_token_from_token (tok))
4584 {
4585 parser_error (line_number, "unexpected token `%s' in conditional command", etext);
4586 free (etext);
4587 }
4588 else
4589 parser_error (line_number, "unexpected token %d in conditional command", tok);
4590 COND_RETURN_ERROR ();
4591 }
4592 return (term);
4593 }
4594
4595 /* This is kind of bogus -- we slip a mini recursive-descent parser in
4596 here to handle the conditional statement syntax. */
4597 static COMMAND *
4598 parse_cond_command ()
4599 {
4600 COND_COM *cexp;
4601
4602 cexp = cond_expr ();
4603 return (make_cond_command (cexp));
4604 }
4605 #endif
4606
4607 #if defined (ARRAY_VARS)
4608 /* When this is called, it's guaranteed that we don't care about anything
4609 in t beyond i. We do save and restore the chars, though. */
4610 static int
4611 token_is_assignment (t, i)
4612 char *t;
4613 int i;
4614 {
4615 unsigned char c, c1;
4616 int r;
4617
4618 c = t[i]; c1 = t[i+1];
4619 t[i] = '='; t[i+1] = '\0';
4620 r = assignment (t);
4621 t[i] = c; t[i+1] = c1;
4622 return r;
4623 }
4624
4625 static int
4626 token_is_ident (t, i)
4627 char *t;
4628 int i;
4629 {
4630 unsigned char c;
4631 int r;
4632
4633 c = t[i];
4634 t[i] = '\0';
4635 r = legal_identifier (t);
4636 t[i] = c;
4637 return r;
4638 }
4639 #endif
4640
4641 static int
4642 read_token_word (character)
4643 int character;
4644 {
4645 /* The value for YYLVAL when a WORD is read. */
4646 WORD_DESC *the_word;
4647
4648 /* Index into the token that we are building. */
4649 int token_index;
4650
4651 /* ALL_DIGITS becomes zero when we see a non-digit. */
4652 int all_digit_token;
4653
4654 /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
4655 int dollar_present;
4656
4657 /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
4658 int quoted;
4659
4660 /* Non-zero means to ignore the value of the next character, and just
4661 to add it no matter what. */
4662 int pass_next_character;
4663
4664 /* The current delimiting character. */
4665 int cd;
4666 int result, peek_char;
4667 char *ttok, *ttrans;
4668 int ttoklen, ttranslen;
4669 intmax_t lvalue;
4670
4671 if (token_buffer_size < TOKEN_DEFAULT_INITIAL_SIZE)
4672 token = (char *)xrealloc (token, token_buffer_size = TOKEN_DEFAULT_INITIAL_SIZE);
4673
4674 token_index = 0;
4675 all_digit_token = DIGIT (character);
4676 dollar_present = quoted = pass_next_character = 0;
4677
4678 for (;;)
4679 {
4680 if (character == EOF)
4681 goto got_token;
4682
4683 if (pass_next_character)
4684 {
4685 pass_next_character = 0;
4686 goto got_character;
4687 }
4688
4689 cd = current_delimiter (dstack);
4690
4691 /* Handle backslashes. Quote lots of things when not inside of
4692 double-quotes, quote some things inside of double-quotes. */
4693 if MBTEST(character == '\\')
4694 {
4695 peek_char = shell_getc (0);
4696
4697 /* Backslash-newline is ignored in all cases except
4698 when quoted with single quotes. */
4699 if (peek_char == '\n')
4700 {
4701 character = '\n';
4702 goto next_character;
4703 }
4704 else
4705 {
4706 shell_ungetc (peek_char);
4707
4708 /* If the next character is to be quoted, note it now. */
4709 if (cd == 0 || cd == '`' ||
4710 (cd == '"' && peek_char >= 0 && (sh_syntaxtab[peek_char] & CBSDQUOTE)))
4711 pass_next_character++;
4712
4713 quoted = 1;
4714 goto got_character;
4715 }
4716 }
4717
4718 /* Parse a matched pair of quote characters. */
4719 if MBTEST(shellquote (character))
4720 {
4721 push_delimiter (dstack, character);
4722 ttok = parse_matched_pair (character, character, character, &ttoklen, 0);
4723 pop_delimiter (dstack);
4724 if (ttok == &matched_pair_error)
4725 return -1; /* Bail immediately. */
4726 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4727 token_buffer_size, TOKEN_DEFAULT_GROW_SIZE);
4728 token[token_index++] = character;
4729 strcpy (token + token_index, ttok);
4730 token_index += ttoklen;
4731 all_digit_token = 0;
4732 quoted = 1;
4733 dollar_present |= (character == '"' && strchr (ttok, '$') != 0);
4734 FREE (ttok);
4735 goto next_character;
4736 }
4737
4738 #ifdef EXTENDED_GLOB
4739 /* Parse a ksh-style extended pattern matching specification. */
4740 if (extended_glob && PATTERN_CHAR (character))
4741 {
4742 peek_char = shell_getc (1);
4743 if MBTEST(peek_char == '(') /* ) */
4744 {
4745 push_delimiter (dstack, peek_char);
4746 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4747 pop_delimiter (dstack);
4748 if (ttok == &matched_pair_error)
4749 return -1; /* Bail immediately. */
4750 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4751 token_buffer_size,
4752 TOKEN_DEFAULT_GROW_SIZE);
4753 token[token_index++] = character;
4754 token[token_index++] = peek_char;
4755 strcpy (token + token_index, ttok);
4756 token_index += ttoklen;
4757 FREE (ttok);
4758 dollar_present = all_digit_token = 0;
4759 goto next_character;
4760 }
4761 else
4762 shell_ungetc (peek_char);
4763 }
4764 #endif /* EXTENDED_GLOB */
4765
4766 /* If the delimiter character is not single quote, parse some of
4767 the shell expansions that must be read as a single word. */
4768 if (shellexp (character))
4769 {
4770 peek_char = shell_getc (1);
4771 /* $(...), <(...), >(...), $((...)), ${...}, and $[...] constructs */
4772 if MBTEST(peek_char == '(' || \
4773 ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */
4774 {
4775 if (peek_char == '{') /* } */
4776 ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE);
4777 else if (peek_char == '(') /* ) */
4778 {
4779 /* XXX - push and pop the `(' as a delimiter for use by
4780 the command-oriented-history code. This way newlines
4781 appearing in the $(...) string get added to the
4782 history literally rather than causing a possibly-
4783 incorrect `;' to be added. ) */
4784 push_delimiter (dstack, peek_char);
4785 ttok = parse_matched_pair (cd, '(', ')', &ttoklen, 0);
4786 pop_delimiter (dstack);
4787 }
4788 else
4789 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
4790 if (ttok == &matched_pair_error)
4791 return -1; /* Bail immediately. */
4792 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4793 token_buffer_size,
4794 TOKEN_DEFAULT_GROW_SIZE);
4795 token[token_index++] = character;
4796 token[token_index++] = peek_char;
4797 strcpy (token + token_index, ttok);
4798 token_index += ttoklen;
4799 FREE (ttok);
4800 dollar_present = 1;
4801 all_digit_token = 0;
4802 goto next_character;
4803 }
4804 /* This handles $'...' and $"..." new-style quoted strings. */
4805 else if MBTEST(character == '$' && (peek_char == '\'' || peek_char == '"'))
4806 {
4807 int first_line;
4808
4809 first_line = line_number;
4810 push_delimiter (dstack, peek_char);
4811 ttok = parse_matched_pair (peek_char, peek_char, peek_char,
4812 &ttoklen,
4813 (peek_char == '\'') ? P_ALLOWESC : 0);
4814 pop_delimiter (dstack);
4815 if (ttok == &matched_pair_error)
4816 return -1;
4817 if (peek_char == '\'')
4818 {
4819 ttrans = ansiexpand (ttok, 0, ttoklen - 1, &ttranslen);
4820 free (ttok);
4821 /* Insert the single quotes and correctly quote any
4822 embedded single quotes (allowed because P_ALLOWESC was
4823 passed to parse_matched_pair). */
4824 ttok = sh_single_quote (ttrans);
4825 free (ttrans);
4826 ttrans = ttok;
4827 ttranslen = strlen (ttrans);
4828 }
4829 else
4830 {
4831 /* Try to locale-expand the converted string. */
4832 ttrans = localeexpand (ttok, 0, ttoklen - 1, first_line, &ttranslen);
4833 free (ttok);
4834
4835 /* Add the double quotes back */
4836 ttok = (char *)xmalloc (ttranslen + 3);
4837 ttok[0] = '"';
4838 strcpy (ttok + 1, ttrans);
4839 ttok[ttranslen + 1] = '"';
4840 ttok[ttranslen += 2] = '\0';
4841 free (ttrans);
4842 ttrans = ttok;
4843 }
4844
4845 RESIZE_MALLOCED_BUFFER (token, token_index, ttranslen + 2,
4846 token_buffer_size,
4847 TOKEN_DEFAULT_GROW_SIZE);
4848 strcpy (token + token_index, ttrans);
4849 token_index += ttranslen;
4850 FREE (ttrans);
4851 quoted = 1;
4852 all_digit_token = 0;
4853 goto next_character;
4854 }
4855 /* This could eventually be extended to recognize all of the
4856 shell's single-character parameter expansions, and set flags.*/
4857 else if MBTEST(character == '$' && peek_char == '$')
4858 {
4859 ttok = (char *)xmalloc (3);
4860 ttok[0] = ttok[1] = '$';
4861 ttok[2] = '\0';
4862 RESIZE_MALLOCED_BUFFER (token, token_index, 3,
4863 token_buffer_size,
4864 TOKEN_DEFAULT_GROW_SIZE);
4865 strcpy (token + token_index, ttok);
4866 token_index += 2;
4867 dollar_present = 1;
4868 all_digit_token = 0;
4869 FREE (ttok);
4870 goto next_character;
4871 }
4872 else
4873 shell_ungetc (peek_char);
4874 }
4875
4876 #if defined (ARRAY_VARS)
4877 /* Identify possible array subscript assignment; match [...] */
4878 else if MBTEST(character == '[' && token_index > 0 && assignment_acceptable (last_read_token) && token_is_ident (token, token_index)) /* ] */
4879 {
4880 ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0);
4881 if (ttok == &matched_pair_error)
4882 return -1; /* Bail immediately. */
4883 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 2,
4884 token_buffer_size,
4885 TOKEN_DEFAULT_GROW_SIZE);
4886 token[token_index++] = character;
4887 strcpy (token + token_index, ttok);
4888 token_index += ttoklen;
4889 FREE (ttok);
4890 all_digit_token = 0;
4891 goto next_character;
4892 }
4893 /* Identify possible compound array variable assignment. */
4894 else if MBTEST(character == '=' && token_index > 0 && token_is_assignment (token, token_index))
4895 {
4896 peek_char = shell_getc (1);
4897 if MBTEST(peek_char == '(') /* ) */
4898 {
4899 ttok = parse_compound_assignment (&ttoklen);
4900
4901 RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 4,
4902 token_buffer_size,
4903 TOKEN_DEFAULT_GROW_SIZE);
4904
4905 token[token_index++] = '=';
4906 token[token_index++] = '(';
4907 if (ttok)
4908 {
4909 strcpy (token + token_index, ttok);
4910 token_index += ttoklen;
4911 }
4912 token[token_index++] = ')';
4913 FREE (ttok);
4914 all_digit_token = 0;
4915 goto next_character;
4916 }
4917 else
4918 shell_ungetc (peek_char);
4919 }
4920 #endif
4921
4922 /* When not parsing a multi-character word construct, shell meta-
4923 characters break words. */
4924 if MBTEST(shellbreak (character))
4925 {
4926 shell_ungetc (character);
4927 goto got_token;
4928 }
4929
4930 got_character:
4931
4932 all_digit_token &= DIGIT (character);
4933 dollar_present |= character == '$';
4934
4935 if (character == CTLESC || character == CTLNUL)
4936 token[token_index++] = CTLESC;
4937
4938 token[token_index++] = character;
4939
4940 RESIZE_MALLOCED_BUFFER (token, token_index, 1, token_buffer_size,
4941 TOKEN_DEFAULT_GROW_SIZE);
4942
4943 next_character:
4944 if (character == '\n' && interactive &&
4945 (bash_input.type == st_stdin || bash_input.type == st_stream))
4946 prompt_again ();
4947
4948 /* We want to remove quoted newlines (that is, a \<newline> pair)
4949 unless we are within single quotes or pass_next_character is
4950 set (the shell equivalent of literal-next). */
4951 cd = current_delimiter (dstack);
4952 character = shell_getc (cd != '\'' && pass_next_character == 0);
4953 } /* end for (;;) */
4954
4955 got_token:
4956
4957 token[token_index] = '\0';
4958
4959 /* Check to see what thing we should return. If the last_read_token
4960 is a `<', or a `&', or the character which ended this token is
4961 a '>' or '<', then, and ONLY then, is this input token a NUMBER.
4962 Otherwise, it is just a word, and should be returned as such. */
4963 if MBTEST(all_digit_token && (character == '<' || character == '>' || \
4964 last_read_token == LESS_AND || \
4965 last_read_token == GREATER_AND))
4966 {
4967 if (legal_number (token, &lvalue) && (int)lvalue == lvalue)
4968 yylval.number = lvalue;
4969 else
4970 yylval.number = -1;
4971 return (NUMBER);
4972 }
4973
4974 /* Check for special case tokens. */
4975 result = (last_shell_getc_is_singlebyte) ? special_case_tokens (token) : -1;
4976 if (result >= 0)
4977 return result;
4978
4979 #if defined (ALIAS)
4980 /* Posix.2 does not allow reserved words to be aliased, so check for all
4981 of them, including special cases, before expanding the current token
4982 as an alias. */
4983 if MBTEST(posixly_correct)
4984 CHECK_FOR_RESERVED_WORD (token);
4985
4986 /* Aliases are expanded iff EXPAND_ALIASES is non-zero, and quoting
4987 inhibits alias expansion. */
4988 if (expand_aliases && quoted == 0)
4989 {
4990 result = alias_expand_token (token);
4991 if (result == RE_READ_TOKEN)
4992 return (RE_READ_TOKEN);
4993 else if (result == NO_EXPANSION)
4994 parser_state &= ~PST_ALEXPNEXT;
4995 }
4996
4997 /* If not in Posix.2 mode, check for reserved words after alias
4998 expansion. */
4999 if MBTEST(posixly_correct == 0)
5000 #endif
5001 CHECK_FOR_RESERVED_WORD (token);
5002
5003 the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
5004 the_word->word = (char *)xmalloc (1 + token_index);
5005 the_word->flags = 0;
5006 strcpy (the_word->word, token);
5007 if (dollar_present)
5008 the_word->flags |= W_HASDOLLAR;
5009 if (quoted)
5010 the_word->flags |= W_QUOTED;
5011 /* A word is an assignment if it appears at the beginning of a
5012 simple command, or after another assignment word. This is
5013 context-dependent, so it cannot be handled in the grammar. */
5014 if (assignment (token))
5015 {
5016 the_word->flags |= W_ASSIGNMENT;
5017 /* Don't perform word splitting on assignment statements. */
5018 if (assignment_acceptable (last_read_token))
5019 the_word->flags |= W_NOSPLIT;
5020 }
5021
5022 yylval.word = the_word;
5023
5024 result = ((the_word->flags & (W_ASSIGNMENT|W_NOSPLIT)) == (W_ASSIGNMENT|W_NOSPLIT))
5025 ? ASSIGNMENT_WORD : WORD;
5026
5027 if (last_read_token == FUNCTION)
5028 {
5029 parser_state |= PST_ALLOWOPNBRC;
5030 function_dstart = line_number;
5031 }
5032
5033 return (result);
5034 }
5035
5036 /* Return 1 if TOKSYM is a token that after being read would allow
5037 a reserved word to be seen, else 0. */
5038 static int
5039 reserved_word_acceptable (toksym)
5040 int toksym;
5041 {
5042 switch (toksym)
5043 {
5044 case '\n':
5045 case ';':
5046 case '(':
5047 case ')':
5048 case '|':
5049 case '&':
5050 case '{':
5051 case '}': /* XXX */
5052 case AND_AND:
5053 case BANG:
5054 case DO:
5055 case DONE:
5056 case ELIF:
5057 case ELSE:
5058 case ESAC:
5059 case FI:
5060 case IF:
5061 case OR_OR:
5062 case SEMI_SEMI:
5063 case THEN:
5064 case TIME:
5065 case TIMEOPT:
5066 case UNTIL:
5067 case WHILE:
5068 case 0:
5069 return 1;
5070 default:
5071 return 0;
5072 }
5073 }
5074
5075 /* Return the index of TOKEN in the alist of reserved words, or -1 if
5076 TOKEN is not a shell reserved word. */
5077 int
5078 find_reserved_word (tokstr)
5079 char *tokstr;
5080 {
5081 int i;
5082 for (i = 0; word_token_alist[i].word; i++)
5083 if (STREQ (tokstr, word_token_alist[i].word))
5084 return i;
5085 return -1;
5086 }
5087
5088 #if 0
5089 #if defined (READLINE)
5090 /* Called after each time readline is called. This insures that whatever
5091 the new prompt string is gets propagated to readline's local prompt
5092 variable. */
5093 static void
5094 reset_readline_prompt ()
5095 {
5096 char *temp_prompt;
5097
5098 if (prompt_string_pointer)
5099 {
5100 temp_prompt = (*prompt_string_pointer)
5101 ? decode_prompt_string (*prompt_string_pointer)
5102 : (char *)NULL;
5103
5104 if (temp_prompt == 0)
5105 {
5106 temp_prompt = (char *)xmalloc (1);
5107 temp_prompt[0] = '\0';
5108 }
5109
5110 FREE (current_readline_prompt);
5111 current_readline_prompt = temp_prompt;
5112 }
5113 }
5114 #endif /* READLINE */
5115 #endif /* 0 */
5116
5117 #if defined (HISTORY)
5118 /* A list of tokens which can be followed by newlines, but not by
5119 semi-colons. When concatenating multiple lines of history, the
5120 newline separator for such tokens is replaced with a space. */
5121 static int no_semi_successors[] = {
5122 '\n', '{', '(', ')', ';', '&', '|',
5123 CASE, DO, ELSE, IF, SEMI_SEMI, THEN, UNTIL, WHILE, AND_AND, OR_OR, IN,
5124 0
5125 };
5126
5127 /* If we are not within a delimited expression, try to be smart
5128 about which separators can be semi-colons and which must be
5129 newlines. Returns the string that should be added into the
5130 history entry. */
5131 char *
5132 history_delimiting_chars ()
5133 {
5134 register int i;
5135
5136 if (dstack.delimiter_depth != 0)
5137 return ("\n");
5138
5139 /* First, handle some special cases. */
5140 /*(*/
5141 /* If we just read `()', assume it's a function definition, and don't
5142 add a semicolon. If the token before the `)' was not `(', and we're
5143 not in the midst of parsing a case statement, assume it's a
5144 parenthesized command and add the semicolon. */
5145 /*)(*/
5146 if (token_before_that == ')')
5147 {
5148 if (two_tokens_ago == '(') /*)*/ /* function def */
5149 return " ";
5150 /* This does not work for subshells inside case statement
5151 command lists. It's a suboptimal solution. */
5152 else if (parser_state & PST_CASESTMT) /* case statement pattern */
5153 return " ";
5154 else
5155 return "; "; /* (...) subshell */
5156 }
5157 else if (token_before_that == WORD && two_tokens_ago == FUNCTION)
5158 return " "; /* function def using `function name' without `()' */
5159
5160 else if (token_before_that == WORD && two_tokens_ago == FOR)
5161 {
5162 /* Tricky. `for i\nin ...' should not have a semicolon, but
5163 `for i\ndo ...' should. We do what we can. */
5164 for (i = shell_input_line_index; whitespace(shell_input_line[i]); i++)
5165 ;
5166 if (shell_input_line[i] && shell_input_line[i] == 'i' && shell_input_line[i+1] == 'n')
5167 return " ";
5168 return ";";
5169 }
5170
5171 for (i = 0; no_semi_successors[i]; i++)
5172 {
5173 if (token_before_that == no_semi_successors[i])
5174 return (" ");
5175 }
5176
5177 return ("; ");
5178 }
5179 #endif /* HISTORY */
5180
5181 /* Issue a prompt, or prepare to issue a prompt when the next character
5182 is read. */
5183 static void
5184 prompt_again ()
5185 {
5186 char *temp_prompt;
5187
5188 if (!interactive) /* XXX */
5189 return;
5190
5191 ps1_prompt = get_string_value ("PS1");
5192 ps2_prompt = get_string_value ("PS2");
5193
5194 if (!prompt_string_pointer)
5195 prompt_string_pointer = &ps1_prompt;
5196
5197 temp_prompt = *prompt_string_pointer
5198 ? decode_prompt_string (*prompt_string_pointer)
5199 : (char *)NULL;
5200
5201 if (temp_prompt == 0)
5202 {
5203 temp_prompt = (char *)xmalloc (1);
5204 temp_prompt[0] = '\0';
5205 }
5206
5207 current_prompt_string = *prompt_string_pointer;
5208 prompt_string_pointer = &ps2_prompt;
5209
5210 #if defined (READLINE)
5211 if (!no_line_editing)
5212 {
5213 FREE (current_readline_prompt);
5214 current_readline_prompt = temp_prompt;
5215 }
5216 else
5217 #endif /* READLINE */
5218 {
5219 FREE (current_decoded_prompt);
5220 current_decoded_prompt = temp_prompt;
5221 }
5222 }
5223
5224 int
5225 get_current_prompt_level ()
5226 {
5227 return ((current_prompt_string && current_prompt_string == ps2_prompt) ? 2 : 1);
5228 }
5229
5230 void
5231 set_current_prompt_level (x)
5232 int x;
5233 {
5234 prompt_string_pointer = (x == 2) ? &ps2_prompt : &ps1_prompt;
5235 current_prompt_string = *prompt_string_pointer;
5236 }
5237
5238 static void
5239 print_prompt ()
5240 {
5241 fprintf (stderr, "%s", current_decoded_prompt);
5242 fflush (stderr);
5243 }
5244
5245 /* Return a string which will be printed as a prompt. The string
5246 may contain special characters which are decoded as follows:
5247
5248 \a bell (ascii 07)
5249 \d the date in Day Mon Date format
5250 \e escape (ascii 033)
5251 \h the hostname up to the first `.'
5252 \H the hostname
5253 \j the number of active jobs
5254 \l the basename of the shell's tty device name
5255 \n CRLF
5256 \r CR
5257 \s the name of the shell
5258 \t the time in 24-hour hh:mm:ss format
5259 \T the time in 12-hour hh:mm:ss format
5260 \@ the time in 12-hour hh:mm am/pm format
5261 \A the time in 24-hour hh:mm format
5262 \D{fmt} the result of passing FMT to strftime(3)
5263 \u your username
5264 \v the version of bash (e.g., 2.00)
5265 \V the release of bash, version + patchlevel (e.g., 2.00.0)
5266 \w the current working directory
5267 \W the last element of $PWD
5268 \! the history number of this command
5269 \# the command number of this command
5270 \$ a $ or a # if you are root
5271 \nnn character code nnn in octal
5272 \\ a backslash
5273 \[ begin a sequence of non-printing chars
5274 \] end a sequence of non-printing chars
5275 */
5276 #define PROMPT_GROWTH 48
5277 char *
5278 decode_prompt_string (string)
5279 char *string;
5280 {
5281 WORD_LIST *list;
5282 char *result, *t;
5283 struct dstack save_dstack;
5284 int last_exit_value;
5285 #if defined (PROMPT_STRING_DECODE)
5286 int result_size, result_index;
5287 int c, n;
5288 char *temp, octal_string[4];
5289 struct tm *tm;
5290 time_t the_time;
5291 char timebuf[128];
5292 char *timefmt;
5293
5294 result = (char *)xmalloc (result_size = PROMPT_GROWTH);
5295 result[result_index = 0] = 0;
5296 temp = (char *)NULL;
5297
5298 while (c = *string++)
5299 {
5300 if (posixly_correct && c == '!')
5301 {
5302 if (*string == '!')
5303 {
5304 temp = savestring ("!");
5305 goto add_string;
5306 }
5307 else
5308 {
5309 #if !defined (HISTORY)
5310 temp = savestring ("1");
5311 #else /* HISTORY */
5312 temp = itos (history_number ());
5313 #endif /* HISTORY */
5314 string--; /* add_string increments string again. */
5315 goto add_string;
5316 }
5317 }
5318 if (c == '\\')
5319 {
5320 c = *string;
5321
5322 switch (c)
5323 {
5324 case '0':
5325 case '1':
5326 case '2':
5327 case '3':
5328 case '4':
5329 case '5':
5330 case '6':
5331 case '7':
5332 strncpy (octal_string, string, 3);
5333 octal_string[3] = '\0';
5334
5335 n = read_octal (octal_string);
5336 temp = (char *)xmalloc (3);
5337
5338 if (n == CTLESC || n == CTLNUL)
5339 {
5340 temp[0] = CTLESC;
5341 temp[1] = n;
5342 temp[2] = '\0';
5343 }
5344 else if (n == -1)
5345 {
5346 temp[0] = '\\';
5347 temp[1] = '\0';
5348 }
5349 else
5350 {
5351 temp[0] = n;
5352 temp[1] = '\0';
5353 }
5354
5355 for (c = 0; n != -1 && c < 3 && ISOCTAL (*string); c++)
5356 string++;
5357
5358 c = 0; /* tested at add_string: */
5359 goto add_string;
5360
5361 case 'd':
5362 case 't':
5363 case 'T':
5364 case '@':
5365 case 'A':
5366 /* Make the current time/date into a string. */
5367 (void) time (&the_time);
5368 tm = localtime (&the_time);
5369
5370 if (c == 'd')
5371 n = strftime (timebuf, sizeof (timebuf), "%a %b %d", tm);
5372 else if (c == 't')
5373 n = strftime (timebuf, sizeof (timebuf), "%H:%M:%S", tm);
5374 else if (c == 'T')
5375 n = strftime (timebuf, sizeof (timebuf), "%I:%M:%S", tm);
5376 else if (c == '@')
5377 n = strftime (timebuf, sizeof (timebuf), "%I:%M %p", tm);
5378 else if (c == 'A')
5379 n = strftime (timebuf, sizeof (timebuf), "%H:%M", tm);
5380
5381 timebuf[sizeof(timebuf) - 1] = '\0';
5382 temp = savestring (timebuf);
5383 goto add_string;
5384
5385 case 'D': /* strftime format */
5386 if (string[1] != '{') /* } */
5387 goto not_escape;
5388
5389 (void) time (&the_time);
5390 tm = localtime (&the_time);
5391 string += 2; /* skip { */
5392 timefmt = xmalloc (strlen (string) + 3);
5393 for (t = timefmt; *string && *string != '}'; )
5394 *t++ = *string++;
5395 *t = '\0';
5396 c = *string; /* tested at add_string */
5397 if (timefmt[0] == '\0')
5398 {
5399 timefmt[0] = '%';
5400 timefmt[1] = 'X'; /* locale-specific current time */
5401 timefmt[2] = '\0';
5402 }
5403 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
5404 free (timefmt);
5405
5406 timebuf[sizeof(timebuf) - 1] = '\0';
5407 if (promptvars || posixly_correct)
5408 /* Make sure that expand_prompt_string is called with a
5409 second argument of Q_DOUBLE_QUOTES if we use this
5410 function here. */
5411 temp = sh_backslash_quote_for_double_quotes (timebuf);
5412 else
5413 temp = savestring (timebuf);
5414 goto add_string;
5415
5416 case 'n':
5417 temp = (char *)xmalloc (3);
5418 temp[0] = no_line_editing ? '\n' : '\r';
5419 temp[1] = no_line_editing ? '\0' : '\n';
5420 temp[2] = '\0';
5421 goto add_string;
5422
5423 case 's':
5424 temp = base_pathname (shell_name);
5425 temp = savestring (temp);
5426 goto add_string;
5427
5428 case 'v':
5429 case 'V':
5430 temp = (char *)xmalloc (16);
5431 if (c == 'v')
5432 strcpy (temp, dist_version);
5433 else
5434 sprintf (temp, "%s.%d", dist_version, patch_level);
5435 goto add_string;
5436
5437 case 'w':
5438 case 'W':
5439 {
5440 /* Use the value of PWD because it is much more efficient. */
5441 char t_string[PATH_MAX];
5442 int tlen;
5443
5444 temp = get_string_value ("PWD");
5445
5446 if (temp == 0)
5447 {
5448 if (getcwd (t_string, sizeof(t_string)) == 0)
5449 {
5450 t_string[0] = '.';
5451 tlen = 1;
5452 }
5453 else
5454 tlen = strlen (t_string);
5455 }
5456 else
5457 {
5458 tlen = sizeof (t_string) - 1;
5459 strncpy (t_string, temp, tlen);
5460 }
5461 t_string[tlen] = '\0';
5462
5463 #define ROOT_PATH(x) ((x)[0] == '/' && (x)[1] == 0)
5464 #define DOUBLE_SLASH_ROOT(x) ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
5465 if (c == 'W')
5466 {
5467 if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT (t_string) == 0)
5468 {
5469 t = strrchr (t_string, '/');
5470 if (t)
5471 strcpy (t_string, t + 1);
5472 }
5473 }
5474 #undef ROOT_PATH
5475 #undef DOUBLE_SLASH_ROOT
5476 else
5477 /* polite_directory_format is guaranteed to return a string
5478 no longer than PATH_MAX - 1 characters. */
5479 strcpy (t_string, polite_directory_format (t_string));
5480
5481 /* If we're going to be expanding the prompt string later,
5482 quote the directory name. */
5483 if (promptvars || posixly_correct)
5484 /* Make sure that expand_prompt_string is called with a
5485 second argument of Q_DOUBLE_QUOTES if we use this
5486 function here. */
5487 temp = sh_backslash_quote_for_double_quotes (t_string);
5488 else
5489 temp = savestring (t_string);
5490
5491 goto add_string;
5492 }
5493
5494 case 'u':
5495 if (current_user.user_name == 0)
5496 get_current_user_info ();
5497 temp = savestring (current_user.user_name);
5498 goto add_string;
5499
5500 case 'h':
5501 case 'H':
5502 temp = savestring (current_host_name);
5503 if (c == 'h' && (t = (char *)strchr (temp, '.')))
5504 *t = '\0';
5505 goto add_string;
5506
5507 case '#':
5508 temp = itos (current_command_number);
5509 goto add_string;
5510
5511 case '!':
5512 #if !defined (HISTORY)
5513 temp = savestring ("1");
5514 #else /* HISTORY */
5515 temp = itos (history_number ());
5516 #endif /* HISTORY */
5517 goto add_string;
5518
5519 case '$':
5520 t = temp = (char *)xmalloc (3);
5521 if ((promptvars || posixly_correct) && (current_user.euid != 0))
5522 *t++ = '\\';
5523 *t++ = current_user.euid == 0 ? '#' : '$';
5524 *t = '\0';
5525 goto add_string;
5526
5527 case 'j':
5528 temp = itos (count_all_jobs ());
5529 goto add_string;
5530
5531 case 'l':
5532 #if defined (HAVE_TTYNAME)
5533 temp = (char *)ttyname (fileno (stdin));
5534 t = temp ? base_pathname (temp) : "tty";
5535 temp = savestring (t);
5536 #else
5537 temp = savestring ("tty");
5538 #endif /* !HAVE_TTYNAME */
5539 goto add_string;
5540
5541 #if defined (READLINE)
5542 case '[':
5543 case ']':
5544 temp = (char *)xmalloc (3);
5545 temp[0] = '\001';
5546 temp[1] = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
5547 temp[2] = '\0';
5548 goto add_string;
5549 #endif /* READLINE */
5550
5551 case '\\':
5552 case 'a':
5553 case 'e':
5554 case 'r':
5555 temp = (char *)xmalloc (2);
5556 if (c == 'a')
5557 temp[0] = '\07';
5558 else if (c == 'e')
5559 temp[0] = '\033';
5560 else if (c == 'r')
5561 temp[0] = '\r';
5562 else /* (c == '\\') */
5563 temp[0] = c;
5564 temp[1] = '\0';
5565 goto add_string;
5566
5567 default:
5568 not_escape:
5569 temp = (char *)xmalloc (3);
5570 temp[0] = '\\';
5571 temp[1] = c;
5572 temp[2] = '\0';
5573
5574 add_string:
5575 if (c)
5576 string++;
5577 result =
5578 sub_append_string (temp, result, &result_index, &result_size);
5579 temp = (char *)NULL; /* Freed in sub_append_string (). */
5580 result[result_index] = '\0';
5581 break;
5582 }
5583 }
5584 else
5585 {
5586 RESIZE_MALLOCED_BUFFER (result, result_index, 3, result_size, PROMPT_GROWTH);
5587 result[result_index++] = c;
5588 result[result_index] = '\0';
5589 }
5590 }
5591 #else /* !PROMPT_STRING_DECODE */
5592 result = savestring (string);
5593 #endif /* !PROMPT_STRING_DECODE */
5594
5595 /* Save the delimiter stack and point `dstack' to temp space so any
5596 command substitutions in the prompt string won't result in screwing
5597 up the parser's quoting state. */
5598 save_dstack = dstack;
5599 dstack = temp_dstack;
5600 dstack.delimiter_depth = 0;
5601
5602 /* Perform variable and parameter expansion and command substitution on
5603 the prompt string. */
5604 if (promptvars || posixly_correct)
5605 {
5606 last_exit_value = last_command_exit_value;
5607 list = expand_prompt_string (result, Q_DOUBLE_QUOTES);
5608 free (result);
5609 result = string_list (list);
5610 dispose_words (list);
5611 last_command_exit_value = last_exit_value;
5612 }
5613 else
5614 {
5615 t = dequote_string (result);
5616 free (result);
5617 result = t;
5618 }
5619
5620 dstack = save_dstack;
5621
5622 return (result);
5623 }
5624
5625 /************************************************
5626 * *
5627 * ERROR HANDLING *
5628 * *
5629 ************************************************/
5630
5631 /* Report a syntax error, and restart the parser. Call here for fatal
5632 errors. */
5633 int
5634 yyerror (msg)
5635 const char *msg;
5636 {
5637 report_syntax_error ((char *)NULL);
5638 reset_parser ();
5639 return (0);
5640 }
5641
5642 static char *
5643 error_token_from_token (token)
5644 int token;
5645 {
5646 char *t;
5647
5648 if (t = find_token_in_alist (token, word_token_alist, 0))
5649 return t;
5650
5651 if (t = find_token_in_alist (token, other_token_alist, 0))
5652 return t;
5653
5654 t = (char *)NULL;
5655 /* This stuff is dicy and needs closer inspection */
5656 switch (current_token)
5657 {
5658 case WORD:
5659 case ASSIGNMENT_WORD:
5660 if (yylval.word)
5661 t = savestring (yylval.word->word);
5662 break;
5663 case NUMBER:
5664 t = itos (yylval.number);
5665 break;
5666 case ARITH_CMD:
5667 if (yylval.word_list)
5668 t = string_list (yylval.word_list);
5669 break;
5670 case ARITH_FOR_EXPRS:
5671 if (yylval.word_list)
5672 t = string_list_internal (yylval.word_list, " ; ");
5673 break;
5674 case COND_CMD:
5675 t = (char *)NULL; /* punt */
5676 break;
5677 }
5678
5679 return t;
5680 }
5681
5682 static char *
5683 error_token_from_text ()
5684 {
5685 char *msg, *t;
5686 int token_end, i;
5687
5688 t = shell_input_line;
5689 i = shell_input_line_index;
5690 token_end = 0;
5691 msg = (char *)NULL;
5692
5693 if (i && t[i] == '\0')
5694 i--;
5695
5696 while (i && (whitespace (t[i]) || t[i] == '\n'))
5697 i--;
5698
5699 if (i)
5700 token_end = i + 1;
5701
5702 while (i && (member (t[i], " \n\t;|&") == 0))
5703 i--;
5704
5705 while (i != token_end && (whitespace (t[i]) || t[i] == '\n'))
5706 i++;
5707
5708 /* Return our idea of the offending token. */
5709 if (token_end || (i == 0 && token_end == 0))
5710 {
5711 if (token_end)
5712 msg = substring (t, i, token_end);
5713 else /* one-character token */
5714 {
5715 msg = (char *)xmalloc (2);
5716 msg[0] = t[i];
5717 msg[1] = '\0';
5718 }
5719 }
5720
5721 return (msg);
5722 }
5723
5724 static void
5725 print_offending_line ()
5726 {
5727 char *msg;
5728 int token_end;
5729
5730 msg = savestring (shell_input_line);
5731 token_end = strlen (msg);
5732 while (token_end && msg[token_end - 1] == '\n')
5733 msg[--token_end] = '\0';
5734
5735 parser_error (line_number, "`%s'", msg);
5736 free (msg);
5737 }
5738
5739 /* Report a syntax error with line numbers, etc.
5740 Call here for recoverable errors. If you have a message to print,
5741 then place it in MESSAGE, otherwise pass NULL and this will figure
5742 out an appropriate message for you. */
5743 static void
5744 report_syntax_error (message)
5745 char *message;
5746 {
5747 char *msg;
5748
5749 if (message)
5750 {
5751 parser_error (line_number, "%s", message);
5752 if (interactive && EOF_Reached)
5753 EOF_Reached = 0;
5754 last_command_exit_value = EX_USAGE;
5755 return;
5756 }
5757
5758 /* If the line of input we're reading is not null, try to find the
5759 objectionable token. First, try to figure out what token the
5760 parser's complaining about by looking at current_token. */
5761 if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
5762 {
5763 parser_error (line_number, "syntax error near unexpected token `%s'", msg);
5764 free (msg);
5765
5766 if (interactive == 0)
5767 print_offending_line ();
5768
5769 last_command_exit_value = EX_USAGE;
5770 return;
5771 }
5772
5773 /* If looking at the current token doesn't prove fruitful, try to find the
5774 offending token by analyzing the text of the input line near the current
5775 input line index and report what we find. */
5776 if (shell_input_line && *shell_input_line)
5777 {
5778 msg = error_token_from_text ();
5779 if (msg)
5780 {
5781 parser_error (line_number, "syntax error near `%s'", msg);
5782 free (msg);
5783 }
5784
5785 /* If not interactive, print the line containing the error. */
5786 if (interactive == 0)
5787 print_offending_line ();
5788 }
5789 else
5790 {
5791 msg = EOF_Reached ? "syntax error: unexpected end of file" : "syntax error";
5792 parser_error (line_number, "%s", msg);
5793 /* When the shell is interactive, this file uses EOF_Reached
5794 only for error reporting. Other mechanisms are used to
5795 decide whether or not to exit. */
5796 if (interactive && EOF_Reached)
5797 EOF_Reached = 0;
5798 }
5799
5800 last_command_exit_value = EX_USAGE;
5801 }
5802
5803 /* ??? Needed function. ??? We have to be able to discard the constructs
5804 created during parsing. In the case of error, we want to return
5805 allocated objects to the memory pool. In the case of no error, we want
5806 to throw away the information about where the allocated objects live.
5807 (dispose_command () will actually free the command.) */
5808 static void
5809 discard_parser_constructs (error_p)
5810 int error_p;
5811 {
5812 }
5813
5814 /************************************************
5815 * *
5816 * EOF HANDLING *
5817 * *
5818 ************************************************/
5819
5820 /* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
5821
5822 /* A flag denoting whether or not ignoreeof is set. */
5823 int ignoreeof = 0;
5824
5825 /* The number of times that we have encountered an EOF character without
5826 another character intervening. When this gets above the limit, the
5827 shell terminates. */
5828 int eof_encountered = 0;
5829
5830 /* The limit for eof_encountered. */
5831 int eof_encountered_limit = 10;
5832
5833 /* If we have EOF as the only input unit, this user wants to leave
5834 the shell. If the shell is not interactive, then just leave.
5835 Otherwise, if ignoreeof is set, and we haven't done this the
5836 required number of times in a row, print a message. */
5837 static void
5838 handle_eof_input_unit ()
5839 {
5840 if (interactive)
5841 {
5842 /* shell.c may use this to decide whether or not to write out the
5843 history, among other things. We use it only for error reporting
5844 in this file. */
5845 if (EOF_Reached)
5846 EOF_Reached = 0;
5847
5848 /* If the user wants to "ignore" eof, then let her do so, kind of. */
5849 if (ignoreeof)
5850 {
5851 if (eof_encountered < eof_encountered_limit)
5852 {
5853 fprintf (stderr, "Use \"%s\" to leave the shell.\n",
5854 login_shell ? "logout" : "exit");
5855 eof_encountered++;
5856 /* Reset the parsing state. */
5857 last_read_token = current_token = '\n';
5858 /* Reset the prompt string to be $PS1. */
5859 prompt_string_pointer = (char **)NULL;
5860 prompt_again ();
5861 return;
5862 }
5863 }
5864
5865 /* In this case EOF should exit the shell. Do it now. */
5866 reset_parser ();
5867 exit_builtin ((WORD_LIST *)NULL);
5868 }
5869 else
5870 {
5871 /* We don't write history files, etc., for non-interactive shells. */
5872 EOF_Reached = 1;
5873 }
5874 }
5875
5876 /************************************************
5877 * *
5878 * STRING PARSING FUNCTIONS *
5879 * *
5880 ************************************************/
5881
5882 /* It's very important that these two functions treat the characters
5883 between ( and ) identically. */
5884
5885 static WORD_LIST parse_string_error;
5886
5887 /* Take a string and run it through the shell parser, returning the
5888 resultant word list. Used by compound array assignment. */
5889 WORD_LIST *
5890 parse_string_to_word_list (s, whom)
5891 char *s;
5892 const char *whom;
5893 {
5894 WORD_LIST *wl;
5895 int tok, orig_current_token, orig_line_number, orig_input_terminator;
5896 int orig_line_count;
5897 int old_echo_input, old_expand_aliases;
5898 #if defined (HISTORY)
5899 int old_remember_on_history, old_history_expansion_inhibited;
5900 #endif
5901
5902 #if defined (HISTORY)
5903 old_remember_on_history = remember_on_history;
5904 # if defined (BANG_HISTORY)
5905 old_history_expansion_inhibited = history_expansion_inhibited;
5906 # endif
5907 bash_history_disable ();
5908 #endif
5909
5910 orig_line_number = line_number;
5911 orig_line_count = current_command_line_count;
5912 orig_input_terminator = shell_input_line_terminator;
5913 old_echo_input = echo_input_at_read;
5914 old_expand_aliases = expand_aliases;
5915
5916 push_stream (1);
5917 last_read_token = WORD; /* WORD to allow reserved words here */
5918 current_command_line_count = 0;
5919 echo_input_at_read = expand_aliases = 0;
5920
5921 with_input_from_string (s, whom);
5922 wl = (WORD_LIST *)NULL;
5923 while ((tok = read_token (READ)) != yacc_EOF)
5924 {
5925 if (tok == '\n' && *bash_input.location.string == '\0')
5926 break;
5927 if (tok == '\n') /* Allow newlines in compound assignments */
5928 continue;
5929 if (tok != WORD && tok != ASSIGNMENT_WORD)
5930 {
5931 line_number = orig_line_number + line_number - 1;
5932 orig_current_token = current_token;
5933 current_token = tok;
5934 yyerror ((char *)NULL); /* does the right thing */
5935 current_token = orig_current_token;
5936 if (wl)
5937 dispose_words (wl);
5938 wl = &parse_string_error;
5939 break;
5940 }
5941 wl = make_word_list (yylval.word, wl);
5942 }
5943
5944 last_read_token = '\n';
5945 pop_stream ();
5946
5947 #if defined (HISTORY)
5948 remember_on_history = old_remember_on_history;
5949 # if defined (BANG_HISTORY)
5950 history_expansion_inhibited = old_history_expansion_inhibited;
5951 # endif /* BANG_HISTORY */
5952 #endif /* HISTORY */
5953
5954 echo_input_at_read = old_echo_input;
5955 expand_aliases = old_expand_aliases;
5956
5957 current_command_line_count = orig_line_count;
5958 shell_input_line_terminator = orig_input_terminator;
5959
5960 if (wl == &parse_string_error)
5961 {
5962 last_command_exit_value = EXECUTION_FAILURE;
5963 if (interactive_shell == 0 && posixly_correct)
5964 jump_to_top_level (FORCE_EOF);
5965 else
5966 jump_to_top_level (DISCARD);
5967 }
5968
5969 return (REVERSE_LIST (wl, WORD_LIST *));
5970 }
5971
5972 static char *
5973 parse_compound_assignment (retlenp)
5974 int *retlenp;
5975 {
5976 WORD_LIST *wl, *rl;
5977 int tok, orig_line_number, orig_token_size;
5978 char *saved_token, *ret;
5979
5980 saved_token = token;
5981 orig_token_size = token_buffer_size;
5982 orig_line_number = line_number;
5983
5984 last_read_token = WORD; /* WORD to allow reserved words here */
5985
5986 token = (char *)NULL;
5987 token_buffer_size = 0;
5988
5989 wl = (WORD_LIST *)NULL; /* ( */
5990 while ((tok = read_token (READ)) != ')')
5991 {
5992 if (tok == '\n') /* Allow newlines in compound assignments */
5993 continue;
5994 if (tok != WORD && tok != ASSIGNMENT_WORD)
5995 {
5996 current_token = tok; /* for error reporting */
5997 if (tok == yacc_EOF) /* ( */
5998 parser_error (orig_line_number, "unexpected EOF while looking for matching `)'");
5999 else
6000 yyerror ((char *)NULL); /* does the right thing */
6001 if (wl)
6002 dispose_words (wl);
6003 wl = &parse_string_error;
6004 break;
6005 }
6006 wl = make_word_list (yylval.word, wl);
6007 }
6008
6009 FREE (token);
6010 token = saved_token;
6011 token_buffer_size = orig_token_size;
6012
6013 if (wl == &parse_string_error)
6014 {
6015 last_command_exit_value = EXECUTION_FAILURE;
6016 last_read_token = '\n'; /* XXX */
6017 if (interactive_shell == 0 && posixly_correct)
6018 jump_to_top_level (FORCE_EOF);
6019 else
6020 jump_to_top_level (DISCARD);
6021 }
6022
6023 last_read_token = WORD;
6024 if (wl)
6025 {
6026 rl = REVERSE_LIST (wl, WORD_LIST *);
6027 ret = string_list (rl);
6028 dispose_words (rl);
6029 }
6030 else
6031 ret = (char *)NULL;
6032
6033 if (retlenp)
6034 *retlenp = (ret && *ret) ? strlen (ret) : 0;
6035 return ret;
6036 }
6037
6038 /************************************************
6039 * *
6040 * MULTIBYTE CHARACTER HANDLING *
6041 * *
6042 ************************************************/
6043
6044 #if defined (HANDLE_MULTIBYTE)
6045 static void
6046 set_line_mbstate ()
6047 {
6048 int i, previ, len;
6049 mbstate_t mbs, prevs;
6050 size_t mbclen;
6051
6052 if (shell_input_line == NULL)
6053 return;
6054 len = strlen (shell_input_line); /* XXX - shell_input_line_len ? */
6055 FREE (shell_input_line_property);
6056 shell_input_line_property = (char *)xmalloc (len + 1);
6057
6058 memset (&prevs, '\0', sizeof (mbstate_t));
6059 for (i = previ = 0; i < len; i++)
6060 {
6061 mbs = prevs;
6062
6063 if (shell_input_line[i] == EOF)
6064 {
6065 int j;
6066 for (j = i; j < len; j++)
6067 shell_input_line_property[j] = 1;
6068 break;
6069 }
6070
6071 mbclen = mbrlen (shell_input_line + previ, i - previ + 1, &mbs);
6072 if (mbclen == 1 || mbclen == (size_t)-1)
6073 {
6074 mbclen = 1;
6075 previ = i + 1;
6076 }
6077 else if (mbclen == (size_t)-2)
6078 mbclen = 0;
6079 else if (mbclen > 1)
6080 {
6081 mbclen = 0;
6082 previ = i + 1;
6083 prevs = mbs;
6084 }
6085 else
6086 {
6087 /* mbrlen doesn't return any other values */
6088 }
6089
6090 shell_input_line_property[i] = mbclen;
6091 }
6092 }
6093 #endif /* HANDLE_MULTIBYTE */