1 /* make_cmd.c -- Functions for making instances of the various
4 /* Copyright (C) 1989-2009 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
25 #include "bashtypes.h"
26 #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
27 # include <sys/file.h>
31 #if defined (HAVE_UNISTD_H)
44 #include "dispose_cmd.h"
45 #include "variables.h"
51 #if defined (JOB_CONTROL)
57 extern int line_number, current_command_line_count, parser_state;
58 extern int last_command_exit_value;
61 sh_obj_cache_t wdcache = {0, 0, 0};
62 sh_obj_cache_t wlcache = {0, 0, 0};
64 #define WDCACHESIZE 60
65 #define WLCACHESIZE 60
67 static COMMAND *make_for_or_select __P((enum command_type, WORD_DESC *, WORD_LIST *, COMMAND *, int));
68 #if defined (ARITH_FOR_COMMAND)
69 static WORD_LIST *make_arith_for_expr __P((char *));
71 static COMMAND *make_until_or_while __P((enum command_type, COMMAND *, COMMAND *));
76 ocache_create (wdcache, WORD_DESC, WDCACHESIZE);
77 ocache_create (wlcache, WORD_LIST, WLCACHESIZE);
85 ocache_alloc (wdcache, WORD_DESC, temp);
92 make_bare_word (string)
97 temp = alloc_word_desc ();
100 temp->word = savestring (string);
103 temp->word = (char *)xmalloc (1);
104 temp->word[0] = '\0';
111 make_word_flags (w, string)
120 slen = strlen (string);
126 w->flags |= W_HASDOLLAR;
129 break; /* continue the loop */
133 w->flags |= W_QUOTED;
137 ADVANCE_CHAR (string, slen, i);
149 temp = make_bare_word (string);
150 return (make_word_flags (temp, string));
154 make_word_from_token (token)
159 tokenizer[0] = token;
162 return (make_word (tokenizer));
166 make_word_list (word, wlink)
172 ocache_alloc (wlcache, WORD_LIST, temp);
180 make_command (type, pointer)
181 enum command_type type;
186 temp = (COMMAND *)xmalloc (sizeof (COMMAND));
188 temp->value.Simple = pointer;
189 temp->value.Simple->flags = temp->flags = 0;
190 temp->redirects = (REDIRECT *)NULL;
195 command_connect (com1, com2, connector)
196 COMMAND *com1, *com2;
201 temp = (CONNECTION *)xmalloc (sizeof (CONNECTION));
202 temp->connector = connector;
205 return (make_command (cm_connection, (SIMPLE_COM *)temp));
209 make_for_or_select (type, name, map_list, action, lineno)
210 enum command_type type;
218 temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
222 temp->map_list = map_list;
223 temp->action = action;
224 return (make_command (type, (SIMPLE_COM *)temp));
228 make_for_command (name, map_list, action, lineno)
234 return (make_for_or_select (cm_for, name, map_list, action, lineno));
238 make_select_command (name, map_list, action, lineno)
244 #if defined (SELECT_COMMAND)
245 return (make_for_or_select (cm_select, name, map_list, action, lineno));
247 last_command_exit_value = 2;
248 return ((COMMAND *)NULL);
252 #if defined (ARITH_FOR_COMMAND)
254 make_arith_for_expr (s)
260 if (s == 0 || *s == '\0')
261 return ((WORD_LIST *)NULL);
263 wd->flags |= W_NOGLOB|W_NOSPLIT|W_QUOTED|W_DQUOTE; /* no word splitting or globbing */
264 result = make_word_list (wd, (WORD_LIST *)NULL);
269 /* Note that this function calls dispose_words on EXPRS, since it doesn't
270 use the word list directly. We free it here rather than at the caller
271 because no other function in this file requires that the caller free
274 make_arith_for_command (exprs, action, lineno)
279 #if defined (ARITH_FOR_COMMAND)
281 WORD_LIST *init, *test, *step;
285 init = test = step = (WORD_LIST *)NULL;
286 /* Parse the string into the three component sub-expressions. */
287 start = t = s = exprs->word->word;
290 /* skip whitespace at the start of each sub-expression. */
291 while (whitespace (*s))
294 /* skip to the semicolon or EOS */
295 i = skip_to_delim (start, 0, ";", SD_NOJMP);
298 t = (i > 0) ? substring (start, 0, i) : (char *)NULL;
304 init = make_arith_for_expr (t);
307 test = make_arith_for_expr (t);
310 step = make_arith_for_expr (t);
317 s++; /* skip over semicolon */
323 parser_error (lineno, _("syntax error: arithmetic expression required"));
325 parser_error (lineno, _("syntax error: `;' unexpected"));
326 parser_error (lineno, _("syntax error: `((%s))'"), exprs->word->word);
330 last_command_exit_value = 2;
331 return ((COMMAND *)NULL);
334 temp = (ARITH_FOR_COM *)xmalloc (sizeof (ARITH_FOR_COM));
337 temp->init = init ? init : make_arith_for_expr ("1");
338 temp->test = test ? test : make_arith_for_expr ("1");
339 temp->step = step ? step : make_arith_for_expr ("1");
340 temp->action = action;
342 dispose_words (exprs);
343 return (make_command (cm_arith_for, (SIMPLE_COM *)temp));
345 dispose_words (exprs);
346 last_command_exit_value = 2;
347 return ((COMMAND *)NULL);
348 #endif /* ARITH_FOR_COMMAND */
352 make_group_command (command)
357 temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
358 temp->command = command;
359 return (make_command (cm_group, (SIMPLE_COM *)temp));
363 make_case_command (word, clauses, lineno)
365 PATTERN_LIST *clauses;
370 temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
374 temp->clauses = REVERSE_LIST (clauses, PATTERN_LIST *);
375 return (make_command (cm_case, (SIMPLE_COM *)temp));
379 make_pattern_list (patterns, action)
385 temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
386 temp->patterns = REVERSE_LIST (patterns, WORD_LIST *);
387 temp->action = action;
394 make_if_command (test, true_case, false_case)
395 COMMAND *test, *true_case, *false_case;
399 temp = (IF_COM *)xmalloc (sizeof (IF_COM));
402 temp->true_case = true_case;
403 temp->false_case = false_case;
404 return (make_command (cm_if, (SIMPLE_COM *)temp));
408 make_until_or_while (which, test, action)
409 enum command_type which;
410 COMMAND *test, *action;
414 temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
417 temp->action = action;
418 return (make_command (which, (SIMPLE_COM *)temp));
422 make_while_command (test, action)
423 COMMAND *test, *action;
425 return (make_until_or_while (cm_while, test, action));
429 make_until_command (test, action)
430 COMMAND *test, *action;
432 return (make_until_or_while (cm_until, test, action));
436 make_arith_command (exp)
439 #if defined (DPAREN_ARITHMETIC)
443 command = (COMMAND *)xmalloc (sizeof (COMMAND));
444 command->value.Arith = temp = (ARITH_COM *)xmalloc (sizeof (ARITH_COM));
447 temp->line = line_number;
450 command->type = cm_arith;
451 command->redirects = (REDIRECT *)NULL;
456 last_command_exit_value = 2;
457 return ((COMMAND *)NULL);
461 #if defined (COND_COMMAND)
463 make_cond_node (type, op, left, right)
466 struct cond_com *left, *right;
470 temp = (COND_COM *)xmalloc (sizeof (COND_COM));
472 temp->line = line_number;
483 make_cond_command (cond_node)
486 #if defined (COND_COMMAND)
489 command = (COMMAND *)xmalloc (sizeof (COMMAND));
490 command->value.Cond = cond_node;
492 command->type = cm_cond;
493 command->redirects = (REDIRECT *)NULL;
495 command->line = cond_node ? cond_node->line : 0;
499 last_command_exit_value = 2;
500 return ((COMMAND *)NULL);
505 make_bare_simple_command ()
510 command = (COMMAND *)xmalloc (sizeof (COMMAND));
511 command->value.Simple = temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
514 temp->line = line_number;
515 temp->words = (WORD_LIST *)NULL;
516 temp->redirects = (REDIRECT *)NULL;
518 command->type = cm_simple;
519 command->redirects = (REDIRECT *)NULL;
525 /* Return a command which is the connection of the word or redirection
526 in ELEMENT, and the command * or NULL in COMMAND. */
528 make_simple_command (element, command)
532 /* If we are starting from scratch, then make the initial command
533 structure. Also note that we have to fill in all the slots, since
534 malloc doesn't return zeroed space. */
537 command = make_bare_simple_command ();
538 parser_state |= PST_REDIRLIST;
543 command->value.Simple->words = make_word_list (element.word, command->value.Simple->words);
544 parser_state &= ~PST_REDIRLIST;
546 else if (element.redirect)
548 REDIRECT *r = element.redirect;
549 /* Due to the way <> is implemented, there may be more than a single
550 redirection in element.redirect. We just follow the chain as far
551 as it goes, and hook onto the end. */
554 r->next = command->value.Simple->redirects;
555 command->value.Simple->redirects = element.redirect;
561 /* Because we are Bourne compatible, we read the input for this
562 << or <<- redirection now, from wherever input is coming from.
563 We store the input read into a WORD_DESC. Replace the text of
564 the redirectee.word with the new input text. If <<- is on,
565 then remove leading TABS from each line. */
567 make_here_document (temp, lineno)
571 int kill_leading, redir_len;
572 char *redir_word, *document, *full_line;
573 int document_index, document_size, delim_unquoted;
575 if (temp->instruction != r_deblank_reading_until &&
576 temp->instruction != r_reading_until)
578 internal_error (_("make_here_document: bad instruction type %d"), temp->instruction);
582 kill_leading = temp->instruction == r_deblank_reading_until;
584 document = (char *)NULL;
585 document_index = document_size = 0;
587 /* Quote removal is the only expansion performed on the delimiter
588 for here documents, making it an extremely special case. */
589 redir_word = string_quote_removal (temp->redirectee.filename->word, 0);
591 /* redirection_expand will return NULL if the expansion results in
592 multiple words or no words. Check for that here, and just abort
593 this here document if it does. */
595 redir_len = strlen (redir_word);
598 temp->here_doc_eof = (char *)xmalloc (1);
599 temp->here_doc_eof[0] = '\0';
603 free (temp->redirectee.filename->word);
604 temp->here_doc_eof = redir_word;
606 /* Read lines from wherever lines are coming from.
607 For each line read, if kill_leading, then kill the
608 leading tab characters.
609 If the line matches redir_word exactly, then we have
610 manufactured the document. Otherwise, add the line to the
611 list of lines in the document. */
613 /* If the here-document delimiter was quoted, the lines should
614 be read verbatim from the input. If it was not quoted, we
615 need to perform backslash-quoted newline removal. */
616 delim_unquoted = (temp->redirectee.filename->flags & W_QUOTED) == 0;
617 while (full_line = read_secondary_line (delim_unquoted))
625 /* If set -v is in effect, echo the line read. read_secondary_line/
626 read_a_line leaves the newline at the end, so don't print another. */
627 if (echo_input_at_read)
628 fprintf (stderr, "%s", line);
630 if (kill_leading && *line)
632 /* Hack: To be compatible with some Bourne shells, we
633 check the word before stripping the whitespace. This
634 is a hack, though. */
635 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
638 while (*line == '\t')
645 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
649 if (len + document_index >= document_size)
651 document_size = document_size ? 2 * (document_size + len) : len + 2;
652 document = (char *)xrealloc (document, document_size);
655 /* len is guaranteed to be > 0 because of the check for line
656 being an empty string before the call to strlen. */
657 FASTCOPY (line, document + document_index, len);
658 document_index += len;
662 internal_warning (_("here-document at line %d delimited by end-of-file (wanted `%s')"), lineno, redir_word);
666 document[document_index] = '\0';
669 document = (char *)xmalloc (1);
672 temp->redirectee.filename->word = document;
675 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
676 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
677 and DEST is a file descriptor or a WORD_DESC *. */
679 make_redirection (source, instruction, dest_and_filename, flags)
681 enum r_instruction instruction;
682 REDIRECTEE dest_and_filename;
690 temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
692 /* First do the common cases. */
693 temp->redirector = source;
694 temp->redirectee = dest_and_filename;
695 temp->here_doc_eof = 0;
696 temp->instruction = instruction;
698 temp->rflags = flags;
699 temp->next = (REDIRECT *)NULL;
704 case r_output_direction: /* >foo */
705 case r_output_force: /* >| foo */
706 case r_err_and_out: /* &>filename */
707 temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
710 case r_appending_to: /* >>foo */
711 case r_append_err_and_out: /* &>> filename */
712 temp->flags = O_APPEND | O_WRONLY | O_CREAT;
715 case r_input_direction: /* <foo */
716 case r_inputa_direction: /* foo & makes this. */
717 temp->flags = O_RDONLY;
720 case r_input_output: /* <>foo */
721 temp->flags = O_RDWR | O_CREAT;
724 case r_deblank_reading_until: /* <<-foo */
725 case r_reading_until: /* << foo */
726 case r_reading_string: /* <<< foo */
727 case r_close_this: /* <&- */
728 case r_duplicating_input: /* 1<&2 */
729 case r_duplicating_output: /* 1>&2 */
732 /* the parser doesn't pass these. */
733 case r_move_input: /* 1<&2- */
734 case r_move_output: /* 1>&2- */
735 case r_move_input_word: /* 1<&$foo- */
736 case r_move_output_word: /* 1>&$foo- */
739 /* The way the lexer works we have to do this here. */
740 case r_duplicating_input_word: /* 1<&$foo */
741 case r_duplicating_output_word: /* 1>&$foo */
742 w = dest_and_filename.filename;
743 wlen = strlen (w->word) - 1;
744 if (w->word[wlen] == '-') /* Yuck */
746 w->word[wlen] = '\0';
747 if (all_digits (w->word) && legal_number (w->word, &lfd) && lfd == (int)lfd)
750 temp->instruction = (instruction == r_duplicating_input_word) ? r_move_input : r_move_output;
751 temp->redirectee.dest = lfd;
754 temp->instruction = (instruction == r_duplicating_input_word) ? r_move_input_word : r_move_output_word;
760 programming_error (_("make_redirection: redirection instruction `%d' out of range"), instruction);
768 make_function_def (name, command, lineno, lstart)
774 #if defined (ARRAY_VARS)
775 SHELL_VAR *bash_source_v;
776 ARRAY *bash_source_a;
779 temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
780 temp->command = command;
784 command->line = lstart;
786 /* Information used primarily for debugging. */
787 temp->source_file = 0;
788 #if defined (ARRAY_VARS)
789 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
790 if (bash_source_a && array_num_elements (bash_source_a) > 0)
791 temp->source_file = array_reference (bash_source_a, 0);
793 #if defined (DEBUGGER)
794 bind_function_def (name->word, temp);
797 temp->source_file = temp->source_file ? savestring (temp->source_file) : 0;
798 return (make_command (cm_function_def, (SIMPLE_COM *)temp));
802 make_subshell_command (command)
807 temp = (SUBSHELL_COM *)xmalloc (sizeof (SUBSHELL_COM));
808 temp->command = command;
809 temp->flags = CMD_WANT_SUBSHELL;
810 return (make_command (cm_subshell, (SIMPLE_COM *)temp));
814 make_coproc_command (name, command)
820 temp = (COPROC_COM *)xmalloc (sizeof (COPROC_COM));
821 temp->name = savestring (name);
822 temp->command = command;
823 temp->flags = CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
824 return (make_command (cm_coproc, (SIMPLE_COM *)temp));
827 /* Reverse the word list and redirection list in the simple command
828 has just been parsed. It seems simpler to do this here the one
829 time then by any other method that I can think of. */
831 clean_simple_command (command)
834 if (command->type != cm_simple)
835 command_error ("clean_simple_command", CMDERR_BADTYPE, command->type, 0);
838 command->value.Simple->words =
839 REVERSE_LIST (command->value.Simple->words, WORD_LIST *);
840 command->value.Simple->redirects =
841 REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
844 parser_state &= ~PST_REDIRLIST;
848 /* The Yacc grammar productions have a problem, in that they take a
849 list followed by an ampersand (`&') and do a simple command connection,
850 making the entire list effectively asynchronous, instead of just
851 the last command. This means that when the list is executed, all
852 the commands have stdin set to /dev/null when job control is not
853 active, instead of just the last. This is wrong, and needs fixing
854 up. This function takes the `&' and applies it to the last command
855 in the list. This is done only for lists connected by `;'; it makes
856 `;' bind `tighter' than `&'. */
858 connect_async_list (command, command2, connector)
859 COMMAND *command, *command2;
862 COMMAND *t, *t1, *t2;
865 t = command->value.Connection->second;
867 if (!t || (command->flags & CMD_WANT_SUBSHELL) ||
868 command->value.Connection->connector != ';')
870 t = command_connect (command, command2, connector);
874 /* This is just defensive programming. The Yacc precedence rules
875 will generally hand this function a command where t points directly
876 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
877 to the `a ; b ; c' list and t will be the `d'). We only want to do
878 this if the list is not being executed as a unit in the background
879 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
880 the only way to tell. */
881 while (((t->flags & CMD_WANT_SUBSHELL) == 0) && t->type == cm_connection &&
882 t->value.Connection->connector == ';')
885 t = t->value.Connection->second;
887 /* Now we have t pointing to the last command in the list, and
888 t1->value.Connection->second == t. */
889 t2 = command_connect (t, command2, connector);
890 t1->value.Connection->second = t2;