1 /* make_cmd.c -- Functions for making instances of the various
4 /* Copyright (C) 1989 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)
40 #include "variables.h"
45 #if defined (JOB_CONTROL)
49 extern int line_number, current_command_line_count;
50 extern int disallow_filename_globbing;
51 extern int last_command_exit_value;
55 make_bare_word (string)
60 temp = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
62 temp->word = savestring (string);
65 temp->word = xmalloc (1);
74 make_word_flags (w, string)
80 for (s = string; *s; s++)
84 w->flags |= W_HASDOLLAR;
87 break; /* continue the loop */
103 temp = make_bare_word (string);
104 return (make_word_flags (temp, string));
108 make_word_from_token (token)
113 tokenizer[0] = token;
116 return (make_word (tokenizer));
120 make_word_list (word, link)
126 temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
133 add_string_to_list (string, list)
139 temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
140 temp->word = make_word (string);
146 make_command (type, pointer)
147 enum command_type type;
152 temp = (COMMAND *)xmalloc (sizeof (COMMAND));
154 temp->value.Simple = pointer;
155 temp->value.Simple->flags = temp->flags = 0;
156 temp->redirects = (REDIRECT *)NULL;
161 command_connect (com1, com2, connector)
162 COMMAND *com1, *com2;
167 temp = (CONNECTION *)xmalloc (sizeof (CONNECTION));
168 temp->connector = connector;
171 return (make_command (cm_connection, (SIMPLE_COM *)temp));
175 make_for_or_select (type, name, map_list, action)
176 enum command_type type;
183 temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
186 temp->map_list = map_list;
187 temp->action = action;
188 return (make_command (type, (SIMPLE_COM *)temp));
192 make_for_command (name, map_list, action)
197 return (make_for_or_select (cm_for, name, map_list, action));
201 make_select_command (name, map_list, action)
206 #if defined (SELECT_COMMAND)
207 return (make_for_or_select (cm_select, name, map_list, action));
209 last_command_exit_value = 2;
210 return ((COMMAND *)NULL);
214 #if defined (ARITH_FOR_COMMAND)
216 make_arith_for_expr (s)
222 if (s == 0 || *s == '\0')
223 return ((WORD_LIST *)NULL);
225 result = make_word_list (w, (WORD_LIST *)NULL);
231 make_arith_for_command (exprs, action, lineno)
236 #if defined (ARITH_FOR_COMMAND)
238 WORD_LIST *init, *test, *step;
242 init = test = step = (WORD_LIST *)NULL;
243 /* Parse the string into the three component sub-expressions. */
244 start = t = s = exprs->word->word;
247 /* skip whitespace at the start of each sub-expression. */
248 while (whitespace (*s))
251 /* skip to the semicolon or EOS */
252 while (*s && *s != ';')
255 t = (s > start) ? substring (start, 0, s - start) : (char *)NULL;
261 init = make_arith_for_expr (t);
264 test = make_arith_for_expr (t);
267 step = make_arith_for_expr (t);
274 s++; /* skip over semicolon */
280 parser_error (lineno, "syntax error: arithmetic expression required");
282 parser_error (lineno, "syntax error: `;' unexpected");
283 parser_error (lineno, "syntax error: `((%s))'", exprs->word->word);
284 last_command_exit_value = 2;
285 return ((COMMAND *)NULL);
288 temp = (ARITH_FOR_COM *)xmalloc (sizeof (ARITH_FOR_COM));
291 temp->init = init ? init : make_arith_for_expr ("1");
292 temp->test = test ? test : make_arith_for_expr ("1");
293 temp->step = step ? step : make_arith_for_expr ("1");
294 temp->action = action;
296 return (make_command (cm_arith_for, (SIMPLE_COM *)temp));
298 last_command_exit_value = 2;
299 return ((COMMAND *)NULL);
300 #endif /* ARITH_FOR_COMMAND */
304 make_group_command (command)
309 temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
310 temp->command = command;
311 return (make_command (cm_group, (SIMPLE_COM *)temp));
315 make_case_command (word, clauses)
317 PATTERN_LIST *clauses;
321 temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
324 temp->clauses = REVERSE_LIST (clauses, PATTERN_LIST *);
325 return (make_command (cm_case, (SIMPLE_COM *)temp));
329 make_pattern_list (patterns, action)
335 temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
336 temp->patterns = REVERSE_LIST (patterns, WORD_LIST *);
337 temp->action = action;
343 make_if_command (test, true_case, false_case)
344 COMMAND *test, *true_case, *false_case;
348 temp = (IF_COM *)xmalloc (sizeof (IF_COM));
351 temp->true_case = true_case;
352 temp->false_case = false_case;
353 return (make_command (cm_if, (SIMPLE_COM *)temp));
357 make_until_or_while (which, test, action)
358 enum command_type which;
359 COMMAND *test, *action;
363 temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
366 temp->action = action;
367 return (make_command (which, (SIMPLE_COM *)temp));
371 make_while_command (test, action)
372 COMMAND *test, *action;
374 return (make_until_or_while (cm_while, test, action));
378 make_until_command (test, action)
379 COMMAND *test, *action;
381 return (make_until_or_while (cm_until, test, action));
385 make_arith_command (exp)
388 #if defined (DPAREN_ARITHMETIC)
392 command = (COMMAND *)xmalloc (sizeof (COMMAND));
393 command->value.Arith = temp = (ARITH_COM *)xmalloc (sizeof (ARITH_COM));
396 temp->line = line_number;
399 command->type = cm_arith;
400 command->redirects = (REDIRECT *)NULL;
405 last_command_exit_value = 2;
406 return ((COMMAND *)NULL);
410 #if defined (COND_COMMAND)
412 make_cond_node (type, op, left, right)
415 struct cond_com *left, *right;
419 temp = (COND_COM *)xmalloc (sizeof (COND_COM));
421 temp->line = line_number;
432 make_cond_command (cond_node)
435 #if defined (COND_COMMAND)
438 command = (COMMAND *)xmalloc (sizeof (COMMAND));
439 command->value.Cond = cond_node;
441 command->type = cm_cond;
442 command->redirects = (REDIRECT *)NULL;
444 command->line = cond_node ? cond_node->line : 0;
448 last_command_exit_value = 2;
449 return ((COMMAND *)NULL);
454 make_bare_simple_command ()
459 command = (COMMAND *)xmalloc (sizeof (COMMAND));
460 command->value.Simple = temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
463 temp->line = line_number;
464 temp->words = (WORD_LIST *)NULL;
465 temp->redirects = (REDIRECT *)NULL;
467 command->type = cm_simple;
468 command->redirects = (REDIRECT *)NULL;
474 /* Return a command which is the connection of the word or redirection
475 in ELEMENT, and the command * or NULL in COMMAND. */
477 make_simple_command (element, command)
481 /* If we are starting from scratch, then make the initial command
482 structure. Also note that we have to fill in all the slots, since
483 malloc doesn't return zeroed space. */
485 command = make_bare_simple_command ();
489 WORD_LIST *tw = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
490 tw->word = element.word;
491 tw->next = command->value.Simple->words;
492 command->value.Simple->words = tw;
496 REDIRECT *r = element.redirect;
497 /* Due to the way <> is implemented, there may be more than a single
498 redirection in element.redirect. We just follow the chain as far
499 as it goes, and hook onto the end. */
502 r->next = command->value.Simple->redirects;
503 command->value.Simple->redirects = element.redirect;
508 /* Because we are Bourne compatible, we read the input for this
509 << or <<- redirection now, from wherever input is coming from.
510 We store the input read into a WORD_DESC. Replace the text of
511 the redirectee.word with the new input text. If <<- is on,
512 then remove leading TABS from each line. */
514 make_here_document (temp)
517 int kill_leading, redir_len;
518 char *redir_word, *document, *full_line;
519 int document_index, document_size, delim_unquoted;
521 if (temp->instruction != r_deblank_reading_until &&
522 temp->instruction != r_reading_until)
524 internal_error ("make_here_document: bad instruction type %d", temp->instruction);
528 kill_leading = temp->instruction == r_deblank_reading_until;
530 document = (char *)NULL;
531 document_index = document_size = 0;
533 /* Quote removal is the only expansion performed on the delimiter
534 for here documents, making it an extremely special case. */
535 redir_word = string_quote_removal (temp->redirectee.filename->word, 0);
537 /* redirection_expand will return NULL if the expansion results in
538 multiple words or no words. Check for that here, and just abort
539 this here document if it does. */
541 redir_len = strlen (redir_word);
544 temp->here_doc_eof = xmalloc (1);
545 temp->here_doc_eof[0] = '\0';
549 free (temp->redirectee.filename->word);
550 temp->here_doc_eof = redir_word;
552 /* Read lines from wherever lines are coming from.
553 For each line read, if kill_leading, then kill the
554 leading tab characters.
555 If the line matches redir_word exactly, then we have
556 manufactured the document. Otherwise, add the line to the
557 list of lines in the document. */
559 /* If the here-document delimiter was quoted, the lines should
560 be read verbatim from the input. If it was not quoted, we
561 need to perform backslash-quoted newline removal. */
562 delim_unquoted = (temp->redirectee.filename->flags & W_QUOTED) == 0;
563 while (full_line = read_secondary_line (delim_unquoted))
571 if (kill_leading && *line)
573 /* Hack: To be compatible with some Bourne shells, we
574 check the word before stripping the whitespace. This
575 is a hack, though. */
576 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
579 while (*line == '\t')
586 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
590 if (len + document_index >= document_size)
592 document_size = document_size ? 2 * (document_size + len) : len + 2;
593 document = xrealloc (document, document_size);
596 /* len is guaranteed to be > 0 because of the check for line
597 being an empty string before the call to strlen. */
598 FASTCOPY (line, document + document_index, len);
599 document_index += len;
604 document[document_index] = '\0';
607 document = xmalloc (1);
610 temp->redirectee.filename->word = document;
613 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
614 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
615 and DEST is a file descriptor or a WORD_DESC *. */
617 make_redirection (source, instruction, dest_and_filename)
619 enum r_instruction instruction;
620 REDIRECTEE dest_and_filename;
622 REDIRECT *temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
624 /* First do the common cases. */
625 temp->redirector = source;
626 temp->redirectee = dest_and_filename;
627 temp->instruction = instruction;
629 temp->next = (REDIRECT *)NULL;
634 case r_output_direction: /* >foo */
635 case r_output_force: /* >| foo */
636 case r_err_and_out: /* command &>filename */
637 temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
640 case r_appending_to: /* >>foo */
641 temp->flags = O_APPEND | O_WRONLY | O_CREAT;
644 case r_input_direction: /* <foo */
645 case r_inputa_direction: /* foo & makes this. */
646 temp->flags = O_RDONLY;
649 case r_input_output: /* <>foo */
650 temp->flags = O_RDWR | O_CREAT;
653 case r_deblank_reading_until: /* <<-foo */
654 case r_reading_until: /* << foo */
655 case r_close_this: /* <&- */
656 case r_duplicating_input: /* 1<&2 */
657 case r_duplicating_output: /* 1>&2 */
658 case r_duplicating_input_word: /* 1<&$foo */
659 case r_duplicating_output_word: /* 1>&$foo */
663 programming_error ("make_redirection: redirection instruction `%d' out of range", instruction);
671 make_function_def (name, command, lineno, lstart)
678 temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
679 temp->command = command;
683 command->line = lstart;
684 return (make_command (cm_function_def, (SIMPLE_COM *)temp));
688 make_subshell_command (command)
693 temp = (SUBSHELL_COM *)xmalloc (sizeof (SUBSHELL_COM));
694 temp->command = command;
695 temp->flags = CMD_WANT_SUBSHELL;
696 return (make_command (cm_subshell, (SIMPLE_COM *)temp));
699 /* Reverse the word list and redirection list in the simple command
700 has just been parsed. It seems simpler to do this here the one
701 time then by any other method that I can think of. */
703 clean_simple_command (command)
706 if (command->type != cm_simple)
707 command_error ("clean_simple_command", CMDERR_BADTYPE, command->type, 0);
710 command->value.Simple->words =
711 REVERSE_LIST (command->value.Simple->words, WORD_LIST *);
712 command->value.Simple->redirects =
713 REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
719 /* The Yacc grammar productions have a problem, in that they take a
720 list followed by an ampersand (`&') and do a simple command connection,
721 making the entire list effectively asynchronous, instead of just
722 the last command. This means that when the list is executed, all
723 the commands have stdin set to /dev/null when job control is not
724 active, instead of just the last. This is wrong, and needs fixing
725 up. This function takes the `&' and applies it to the last command
726 in the list. This is done only for lists connected by `;'; it makes
727 `;' bind `tighter' than `&'. */
729 connect_async_list (command, command2, connector)
730 COMMAND *command, *command2;
733 COMMAND *t, *t1, *t2;
736 t = command->value.Connection->second;
738 if (!t || (command->flags & CMD_WANT_SUBSHELL) ||
739 command->value.Connection->connector != ';')
741 t = command_connect (command, command2, connector);
745 /* This is just defensive programming. The Yacc precedence rules
746 will generally hand this function a command where t points directly
747 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
748 to the `a ; b ; c' list and t will be the `d'). We only want to do
749 this if the list is not being executed as a unit in the background
750 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
751 the only way to tell. */
752 while (((t->flags & CMD_WANT_SUBSHELL) == 0) && t->type == cm_connection &&
753 t->value.Connection->connector == ';')
756 t = t->value.Connection->second;
758 /* Now we have t pointing to the last command in the list, and
759 t1->value.Connection->second == t. */
760 t2 = command_connect (t, command2, connector);
761 t1->value.Connection->second = t2;