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