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