1 /* make_cmd.c -- Functions for making instances of the various
4 /* Copyright (C) 1989-2002 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 it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
25 #include "bashtypes.h"
27 # include <sys/file.h>
31 #if defined (HAVE_UNISTD_H)
41 #include "dispose_cmd.h"
42 #include "variables.h"
48 #if defined (JOB_CONTROL)
54 extern int line_number, current_command_line_count;
55 extern int last_command_exit_value;
58 sh_obj_cache_t wdcache = {0, 0, 0};
59 sh_obj_cache_t wlcache = {0, 0, 0};
61 #define WDCACHESIZE 60
62 #define WLCACHESIZE 60
64 static COMMAND *make_for_or_select __P((enum command_type, WORD_DESC *, WORD_LIST *, COMMAND *));
65 #if defined (ARITH_FOR_COMMAND)
66 static WORD_LIST *make_arith_for_expr __P((char *));
68 static COMMAND *make_until_or_while __P((enum command_type, COMMAND *, COMMAND *));
73 ocache_create (wdcache, WORD_DESC, WDCACHESIZE);
74 ocache_create (wlcache, WORD_LIST, WLCACHESIZE);
78 make_bare_word (string)
83 temp = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
85 ocache_alloc (wdcache, WORD_DESC, temp);
89 temp->word = savestring (string);
92 temp->word = (char *)xmalloc (1);
101 make_word_flags (w, string)
110 slen = strlen (string);
116 w->flags |= W_HASDOLLAR;
119 break; /* continue the loop */
123 w->flags |= W_QUOTED;
127 ADVANCE_CHAR (string, slen, i);
139 temp = make_bare_word (string);
140 return (make_word_flags (temp, string));
144 make_word_from_token (token)
149 tokenizer[0] = token;
152 return (make_word (tokenizer));
156 make_word_list (word, wlink)
163 temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
165 ocache_alloc (wlcache, WORD_LIST, temp);
173 make_command (type, pointer)
174 enum command_type type;
179 temp = (COMMAND *)xmalloc (sizeof (COMMAND));
181 temp->value.Simple = pointer;
182 temp->value.Simple->flags = temp->flags = 0;
183 temp->redirects = (REDIRECT *)NULL;
188 command_connect (com1, com2, connector)
189 COMMAND *com1, *com2;
194 temp = (CONNECTION *)xmalloc (sizeof (CONNECTION));
195 temp->connector = connector;
198 return (make_command (cm_connection, (SIMPLE_COM *)temp));
202 make_for_or_select (type, name, map_list, action)
203 enum command_type type;
210 temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
213 temp->map_list = map_list;
214 temp->action = action;
215 return (make_command (type, (SIMPLE_COM *)temp));
219 make_for_command (name, map_list, action)
224 return (make_for_or_select (cm_for, name, map_list, action));
228 make_select_command (name, map_list, action)
233 #if defined (SELECT_COMMAND)
234 return (make_for_or_select (cm_select, name, map_list, action));
236 last_command_exit_value = 2;
237 return ((COMMAND *)NULL);
241 #if defined (ARITH_FOR_COMMAND)
243 make_arith_for_expr (s)
248 if (s == 0 || *s == '\0')
249 return ((WORD_LIST *)NULL);
250 result = make_word_list (make_word (s), (WORD_LIST *)NULL);
256 make_arith_for_command (exprs, action, lineno)
261 #if defined (ARITH_FOR_COMMAND)
263 WORD_LIST *init, *test, *step;
267 init = test = step = (WORD_LIST *)NULL;
268 /* Parse the string into the three component sub-expressions. */
269 start = t = s = exprs->word->word;
272 /* skip whitespace at the start of each sub-expression. */
273 while (whitespace (*s))
276 /* skip to the semicolon or EOS */
277 while (*s && *s != ';')
280 t = (s > start) ? substring (start, 0, s - start) : (char *)NULL;
286 init = make_arith_for_expr (t);
289 test = make_arith_for_expr (t);
292 step = make_arith_for_expr (t);
299 s++; /* skip over semicolon */
305 parser_error (lineno, "syntax error: arithmetic expression required");
307 parser_error (lineno, "syntax error: `;' unexpected");
308 parser_error (lineno, "syntax error: `((%s))'", exprs->word->word);
309 last_command_exit_value = 2;
310 return ((COMMAND *)NULL);
313 temp = (ARITH_FOR_COM *)xmalloc (sizeof (ARITH_FOR_COM));
316 temp->init = init ? init : make_arith_for_expr ("1");
317 temp->test = test ? test : make_arith_for_expr ("1");
318 temp->step = step ? step : make_arith_for_expr ("1");
319 temp->action = action;
321 return (make_command (cm_arith_for, (SIMPLE_COM *)temp));
323 last_command_exit_value = 2;
324 return ((COMMAND *)NULL);
325 #endif /* ARITH_FOR_COMMAND */
329 make_group_command (command)
334 temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
335 temp->command = command;
336 return (make_command (cm_group, (SIMPLE_COM *)temp));
340 make_case_command (word, clauses)
342 PATTERN_LIST *clauses;
346 temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
349 temp->clauses = REVERSE_LIST (clauses, PATTERN_LIST *);
350 return (make_command (cm_case, (SIMPLE_COM *)temp));
354 make_pattern_list (patterns, action)
360 temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
361 temp->patterns = REVERSE_LIST (patterns, WORD_LIST *);
362 temp->action = action;
368 make_if_command (test, true_case, false_case)
369 COMMAND *test, *true_case, *false_case;
373 temp = (IF_COM *)xmalloc (sizeof (IF_COM));
376 temp->true_case = true_case;
377 temp->false_case = false_case;
378 return (make_command (cm_if, (SIMPLE_COM *)temp));
382 make_until_or_while (which, test, action)
383 enum command_type which;
384 COMMAND *test, *action;
388 temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
391 temp->action = action;
392 return (make_command (which, (SIMPLE_COM *)temp));
396 make_while_command (test, action)
397 COMMAND *test, *action;
399 return (make_until_or_while (cm_while, test, action));
403 make_until_command (test, action)
404 COMMAND *test, *action;
406 return (make_until_or_while (cm_until, test, action));
410 make_arith_command (exp)
413 #if defined (DPAREN_ARITHMETIC)
417 command = (COMMAND *)xmalloc (sizeof (COMMAND));
418 command->value.Arith = temp = (ARITH_COM *)xmalloc (sizeof (ARITH_COM));
421 temp->line = line_number;
424 command->type = cm_arith;
425 command->redirects = (REDIRECT *)NULL;
430 last_command_exit_value = 2;
431 return ((COMMAND *)NULL);
435 #if defined (COND_COMMAND)
437 make_cond_node (type, op, left, right)
440 struct cond_com *left, *right;
444 temp = (COND_COM *)xmalloc (sizeof (COND_COM));
446 temp->line = line_number;
457 make_cond_command (cond_node)
460 #if defined (COND_COMMAND)
463 command = (COMMAND *)xmalloc (sizeof (COMMAND));
464 command->value.Cond = cond_node;
466 command->type = cm_cond;
467 command->redirects = (REDIRECT *)NULL;
469 command->line = cond_node ? cond_node->line : 0;
473 last_command_exit_value = 2;
474 return ((COMMAND *)NULL);
479 make_bare_simple_command ()
484 command = (COMMAND *)xmalloc (sizeof (COMMAND));
485 command->value.Simple = temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
488 temp->line = line_number;
489 temp->words = (WORD_LIST *)NULL;
490 temp->redirects = (REDIRECT *)NULL;
492 command->type = cm_simple;
493 command->redirects = (REDIRECT *)NULL;
499 /* Return a command which is the connection of the word or redirection
500 in ELEMENT, and the command * or NULL in COMMAND. */
502 make_simple_command (element, command)
506 /* If we are starting from scratch, then make the initial command
507 structure. Also note that we have to fill in all the slots, since
508 malloc doesn't return zeroed space. */
510 command = make_bare_simple_command ();
513 command->value.Simple->words = make_word_list (element.word, command->value.Simple->words);
516 REDIRECT *r = element.redirect;
517 /* Due to the way <> is implemented, there may be more than a single
518 redirection in element.redirect. We just follow the chain as far
519 as it goes, and hook onto the end. */
522 r->next = command->value.Simple->redirects;
523 command->value.Simple->redirects = element.redirect;
528 /* Because we are Bourne compatible, we read the input for this
529 << or <<- redirection now, from wherever input is coming from.
530 We store the input read into a WORD_DESC. Replace the text of
531 the redirectee.word with the new input text. If <<- is on,
532 then remove leading TABS from each line. */
534 make_here_document (temp)
537 int kill_leading, redir_len;
538 char *redir_word, *document, *full_line;
539 int document_index, document_size, delim_unquoted;
541 if (temp->instruction != r_deblank_reading_until &&
542 temp->instruction != r_reading_until)
544 internal_error ("make_here_document: bad instruction type %d", temp->instruction);
548 kill_leading = temp->instruction == r_deblank_reading_until;
550 document = (char *)NULL;
551 document_index = document_size = 0;
553 /* Quote removal is the only expansion performed on the delimiter
554 for here documents, making it an extremely special case. */
555 redir_word = string_quote_removal (temp->redirectee.filename->word, 0);
557 /* redirection_expand will return NULL if the expansion results in
558 multiple words or no words. Check for that here, and just abort
559 this here document if it does. */
561 redir_len = strlen (redir_word);
564 temp->here_doc_eof = (char *)xmalloc (1);
565 temp->here_doc_eof[0] = '\0';
569 free (temp->redirectee.filename->word);
570 temp->here_doc_eof = redir_word;
572 /* Read lines from wherever lines are coming from.
573 For each line read, if kill_leading, then kill the
574 leading tab characters.
575 If the line matches redir_word exactly, then we have
576 manufactured the document. Otherwise, add the line to the
577 list of lines in the document. */
579 /* If the here-document delimiter was quoted, the lines should
580 be read verbatim from the input. If it was not quoted, we
581 need to perform backslash-quoted newline removal. */
582 delim_unquoted = (temp->redirectee.filename->flags & W_QUOTED) == 0;
583 while (full_line = read_secondary_line (delim_unquoted))
591 if (kill_leading && *line)
593 /* Hack: To be compatible with some Bourne shells, we
594 check the word before stripping the whitespace. This
595 is a hack, though. */
596 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
599 while (*line == '\t')
606 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
610 if (len + document_index >= document_size)
612 document_size = document_size ? 2 * (document_size + len) : len + 2;
613 document = (char *)xrealloc (document, document_size);
616 /* len is guaranteed to be > 0 because of the check for line
617 being an empty string before the call to strlen. */
618 FASTCOPY (line, document + document_index, len);
619 document_index += len;
624 document[document_index] = '\0';
627 document = (char *)xmalloc (1);
630 temp->redirectee.filename->word = document;
633 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
634 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
635 and DEST is a file descriptor or a WORD_DESC *. */
637 make_redirection (source, instruction, dest_and_filename)
639 enum r_instruction instruction;
640 REDIRECTEE dest_and_filename;
647 temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
649 /* First do the common cases. */
650 temp->redirector = source;
651 temp->redirectee = dest_and_filename;
652 temp->instruction = instruction;
654 temp->next = (REDIRECT *)NULL;
659 case r_output_direction: /* >foo */
660 case r_output_force: /* >| foo */
661 case r_err_and_out: /* command &>filename */
662 temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
665 case r_appending_to: /* >>foo */
666 temp->flags = O_APPEND | O_WRONLY | O_CREAT;
669 case r_input_direction: /* <foo */
670 case r_inputa_direction: /* foo & makes this. */
671 temp->flags = O_RDONLY;
674 case r_input_output: /* <>foo */
675 temp->flags = O_RDWR | O_CREAT;
678 case r_deblank_reading_until: /* <<-foo */
679 case r_reading_until: /* << foo */
680 case r_reading_string: /* <<< foo */
681 case r_close_this: /* <&- */
682 case r_duplicating_input: /* 1<&2 */
683 case r_duplicating_output: /* 1>&2 */
686 /* the parser doesn't pass these. */
687 case r_move_input: /* 1<&2- */
688 case r_move_output: /* 1>&2- */
689 case r_move_input_word: /* 1<&$foo- */
690 case r_move_output_word: /* 1>&$foo- */
693 /* The way the lexer works we have to do this here. */
694 case r_duplicating_input_word: /* 1<&$foo */
695 case r_duplicating_output_word: /* 1>&$foo */
696 w = dest_and_filename.filename;
697 wlen = strlen (w->word) - 1;
698 if (w->word[wlen] == '-') /* Yuck */
700 w->word[wlen] = '\0';
701 if (all_digits (w->word) && legal_number (w->word, &lfd) && lfd == (int)lfd)
704 temp->instruction = (instruction == r_duplicating_input_word) ? r_move_input : r_move_output;
705 temp->redirectee.dest = lfd;
708 temp->instruction = (instruction == r_duplicating_input_word) ? r_move_input_word : r_move_output_word;
714 programming_error ("make_redirection: redirection instruction `%d' out of range", instruction);
722 make_function_def (name, command, lineno, lstart)
729 temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
730 temp->command = command;
734 command->line = lstart;
735 return (make_command (cm_function_def, (SIMPLE_COM *)temp));
739 make_subshell_command (command)
744 temp = (SUBSHELL_COM *)xmalloc (sizeof (SUBSHELL_COM));
745 temp->command = command;
746 temp->flags = CMD_WANT_SUBSHELL;
747 return (make_command (cm_subshell, (SIMPLE_COM *)temp));
750 /* Reverse the word list and redirection list in the simple command
751 has just been parsed. It seems simpler to do this here the one
752 time then by any other method that I can think of. */
754 clean_simple_command (command)
757 if (command->type != cm_simple)
758 command_error ("clean_simple_command", CMDERR_BADTYPE, command->type, 0);
761 command->value.Simple->words =
762 REVERSE_LIST (command->value.Simple->words, WORD_LIST *);
763 command->value.Simple->redirects =
764 REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
770 /* The Yacc grammar productions have a problem, in that they take a
771 list followed by an ampersand (`&') and do a simple command connection,
772 making the entire list effectively asynchronous, instead of just
773 the last command. This means that when the list is executed, all
774 the commands have stdin set to /dev/null when job control is not
775 active, instead of just the last. This is wrong, and needs fixing
776 up. This function takes the `&' and applies it to the last command
777 in the list. This is done only for lists connected by `;'; it makes
778 `;' bind `tighter' than `&'. */
780 connect_async_list (command, command2, connector)
781 COMMAND *command, *command2;
784 COMMAND *t, *t1, *t2;
787 t = command->value.Connection->second;
789 if (!t || (command->flags & CMD_WANT_SUBSHELL) ||
790 command->value.Connection->connector != ';')
792 t = command_connect (command, command2, connector);
796 /* This is just defensive programming. The Yacc precedence rules
797 will generally hand this function a command where t points directly
798 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
799 to the `a ; b ; c' list and t will be the `d'). We only want to do
800 this if the list is not being executed as a unit in the background
801 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
802 the only way to tell. */
803 while (((t->flags & CMD_WANT_SUBSHELL) == 0) && t->type == cm_connection &&
804 t->value.Connection->connector == ';')
807 t = t->value.Connection->second;
809 /* Now we have t pointing to the last command in the list, and
810 t1->value.Connection->second == t. */
811 t2 = command_connect (t, command2, connector);
812 t1->value.Connection->second = t2;