1 /* make_cmd.c -- Functions for making instances of the various
4 /* Copyright (C) 1989-2020 Free Software Foundation, Inc.
6 This file is part of GNU Bush, the Bourne Again SHell.
8 Bush 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 Bush 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 Bush. If not, see <http://www.gnu.org/licenses/>.
25 #include "bushtypes.h"
26 #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
27 # include <sys/file.h>
31 #if defined (HAVE_UNISTD_H)
38 #include "execute_cmd.h"
43 #if defined (JOB_CONTROL)
49 int here_doc_first_line
= 0;
52 sh_obj_cache_t wdcache
= {0, 0, 0};
53 sh_obj_cache_t wlcache
= {0, 0, 0};
55 #define WDCACHESIZE 128
56 #define WLCACHESIZE 128
58 static COMMAND
*make_for_or_select
PARAMS((enum command_type
, WORD_DESC
*, WORD_LIST
*, COMMAND
*, int));
59 #if defined (ARITH_FOR_COMMAND)
60 static WORD_LIST
*make_arith_for_expr
PARAMS((char *));
62 static COMMAND
*make_until_or_while
PARAMS((enum command_type
, COMMAND
*, COMMAND
*));
67 ocache_create (wdcache
, WORD_DESC
, WDCACHESIZE
);
68 ocache_create (wlcache
, WORD_LIST
, WLCACHESIZE
);
76 ocache_alloc (wdcache
, WORD_DESC
, temp
);
83 make_bare_word (string
)
88 temp
= alloc_word_desc ();
91 temp
->word
= savestring (string
);
94 temp
->word
= (char *)xmalloc (1);
102 make_word_flags (w
, string
)
111 slen
= strlen (string
);
117 w
->flags
|= W_HASDOLLAR
;
120 break; /* continue the loop */
124 w
->flags
|= W_QUOTED
;
128 ADVANCE_CHAR (string
, slen
, i
);
140 temp
= make_bare_word (string
);
141 return (make_word_flags (temp
, string
));
145 make_word_from_token (token
)
150 tokenizer
[0] = token
;
153 return (make_word (tokenizer
));
157 make_word_list (word
, wlink
)
163 ocache_alloc (wlcache
, WORD_LIST
, temp
);
171 make_command (type
, pointer
)
172 enum command_type type
;
177 temp
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
179 temp
->value
.Simple
= pointer
;
180 temp
->value
.Simple
->flags
= temp
->flags
= 0;
181 temp
->redirects
= (REDIRECT
*)NULL
;
186 command_connect (com1
, com2
, connector
)
187 COMMAND
*com1
, *com2
;
192 temp
= (CONNECTION
*)xmalloc (sizeof (CONNECTION
));
193 temp
->connector
= connector
;
196 return (make_command (cm_connection
, (SIMPLE_COM
*)temp
));
200 make_for_or_select (type
, name
, map_list
, action
, lineno
)
201 enum command_type type
;
209 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
, lineno
)
225 return (make_for_or_select (cm_for
, name
, map_list
, action
, lineno
));
229 make_select_command (name
, map_list
, action
, lineno
)
235 #if defined (SELECT_COMMAND)
236 return (make_for_or_select (cm_select
, name
, map_list
, action
, lineno
));
239 return ((COMMAND
*)NULL
);
243 #if defined (ARITH_FOR_COMMAND)
245 make_arith_for_expr (s
)
251 if (s
== 0 || *s
== '\0')
252 return ((WORD_LIST
*)NULL
);
254 wd
->flags
|= W_NOGLOB
|W_NOSPLIT
|W_QUOTED
|W_DQUOTE
; /* no word splitting or globbing */
255 #if defined (PROCESS_SUBSTITUTION)
256 wd
->flags
|= W_NOPROCSUB
; /* no process substitution */
258 result
= make_word_list (wd
, (WORD_LIST
*)NULL
);
263 /* Note that this function calls dispose_words on EXPRS, since it doesn't
264 use the word list directly. We free it here rather than at the caller
265 because no other function in this file requires that the caller free
268 make_arith_for_command (exprs
, action
, lineno
)
273 #if defined (ARITH_FOR_COMMAND)
275 WORD_LIST
*init
, *test
, *step
;
279 init
= test
= step
= (WORD_LIST
*)NULL
;
280 /* Parse the string into the three component sub-expressions. */
281 start
= t
= s
= exprs
->word
->word
;
284 /* skip whitespace at the start of each sub-expression. */
285 while (whitespace (*s
))
288 /* skip to the semicolon or EOS */
289 i
= skip_to_delim (start
, 0, ";", SD_NOJMP
|SD_NOPROCSUB
);
292 t
= (i
> 0) ? substring (start
, 0, i
) : (char *)NULL
;
298 init
= make_arith_for_expr (t
);
301 test
= make_arith_for_expr (t
);
304 step
= make_arith_for_expr (t
);
311 s
++; /* skip over semicolon */
317 parser_error (lineno
, _("syntax error: arithmetic expression required"));
319 parser_error (lineno
, _("syntax error: `;' unexpected"));
320 parser_error (lineno
, _("syntax error: `((%s))'"), exprs
->word
->word
);
325 return ((COMMAND
*)NULL
);
328 temp
= (ARITH_FOR_COM
*)xmalloc (sizeof (ARITH_FOR_COM
));
331 temp
->init
= init
? init
: make_arith_for_expr ("1");
332 temp
->test
= test
? test
: make_arith_for_expr ("1");
333 temp
->step
= step
? step
: make_arith_for_expr ("1");
334 temp
->action
= action
;
336 dispose_words (exprs
);
337 return (make_command (cm_arith_for
, (SIMPLE_COM
*)temp
));
339 dispose_words (exprs
);
341 return ((COMMAND
*)NULL
);
342 #endif /* ARITH_FOR_COMMAND */
346 make_group_command (command
)
351 temp
= (GROUP_COM
*)xmalloc (sizeof (GROUP_COM
));
352 temp
->command
= command
;
353 return (make_command (cm_group
, (SIMPLE_COM
*)temp
));
357 make_case_command (word
, clauses
, lineno
)
359 PATTERN_LIST
*clauses
;
364 temp
= (CASE_COM
*)xmalloc (sizeof (CASE_COM
));
368 temp
->clauses
= REVERSE_LIST (clauses
, PATTERN_LIST
*);
369 return (make_command (cm_case
, (SIMPLE_COM
*)temp
));
373 make_pattern_list (patterns
, action
)
379 temp
= (PATTERN_LIST
*)xmalloc (sizeof (PATTERN_LIST
));
380 temp
->patterns
= REVERSE_LIST (patterns
, WORD_LIST
*);
381 temp
->action
= action
;
388 make_if_command (test
, true_case
, false_case
)
389 COMMAND
*test
, *true_case
, *false_case
;
393 temp
= (IF_COM
*)xmalloc (sizeof (IF_COM
));
396 temp
->true_case
= true_case
;
397 temp
->false_case
= false_case
;
398 return (make_command (cm_if
, (SIMPLE_COM
*)temp
));
402 make_until_or_while (which
, test
, action
)
403 enum command_type which
;
404 COMMAND
*test
, *action
;
408 temp
= (WHILE_COM
*)xmalloc (sizeof (WHILE_COM
));
411 temp
->action
= action
;
412 return (make_command (which
, (SIMPLE_COM
*)temp
));
416 make_while_command (test
, action
)
417 COMMAND
*test
, *action
;
419 return (make_until_or_while (cm_while
, test
, action
));
423 make_until_command (test
, action
)
424 COMMAND
*test
, *action
;
426 return (make_until_or_while (cm_until
, test
, action
));
430 make_arith_command (exp
)
433 #if defined (DPAREN_ARITHMETIC)
437 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
438 command
->value
.Arith
= temp
= (ARITH_COM
*)xmalloc (sizeof (ARITH_COM
));
441 temp
->line
= line_number
;
444 command
->type
= cm_arith
;
445 command
->redirects
= (REDIRECT
*)NULL
;
451 return ((COMMAND
*)NULL
);
455 #if defined (COND_COMMAND)
457 make_cond_node (type
, op
, left
, right
)
460 struct cond_com
*left
, *right
;
464 temp
= (COND_COM
*)xmalloc (sizeof (COND_COM
));
466 temp
->line
= line_number
;
477 make_cond_command (cond_node
)
480 #if defined (COND_COMMAND)
483 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
484 command
->value
.Cond
= cond_node
;
486 command
->type
= cm_cond
;
487 command
->redirects
= (REDIRECT
*)NULL
;
489 command
->line
= cond_node
? cond_node
->line
: 0;
494 return ((COMMAND
*)NULL
);
499 make_bare_simple_command ()
504 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
505 command
->value
.Simple
= temp
= (SIMPLE_COM
*)xmalloc (sizeof (SIMPLE_COM
));
508 temp
->line
= line_number
;
509 temp
->words
= (WORD_LIST
*)NULL
;
510 temp
->redirects
= (REDIRECT
*)NULL
;
512 command
->type
= cm_simple
;
513 command
->redirects
= (REDIRECT
*)NULL
;
519 /* Return a command which is the connection of the word or redirection
520 in ELEMENT, and the command * or NULL in COMMAND. */
522 make_simple_command (element
, command
)
526 /* If we are starting from scratch, then make the initial command
527 structure. Also note that we have to fill in all the slots, since
528 malloc doesn't return zeroed space. */
531 command
= make_bare_simple_command ();
532 parser_state
|= PST_REDIRLIST
;
537 command
->value
.Simple
->words
= make_word_list (element
.word
, command
->value
.Simple
->words
);
538 parser_state
&= ~PST_REDIRLIST
;
540 else if (element
.redirect
)
542 REDIRECT
*r
= element
.redirect
;
543 /* Due to the way <> is implemented, there may be more than a single
544 redirection in element.redirect. We just follow the chain as far
545 as it goes, and hook onto the end. */
548 r
->next
= command
->value
.Simple
->redirects
;
549 command
->value
.Simple
->redirects
= element
.redirect
;
555 /* Because we are Bourne compatible, we read the input for this
556 << or <<- redirection now, from wherever input is coming from.
557 We store the input read into a WORD_DESC. Replace the text of
558 the redirectee.word with the new input text. If <<- is on,
559 then remove leading TABS from each line. */
561 make_here_document (temp
, lineno
)
565 int kill_leading
, redir_len
;
566 char *redir_word
, *document
, *full_line
;
567 int document_index
, document_size
, delim_unquoted
;
569 if (temp
->instruction
!= r_deblank_reading_until
&&
570 temp
->instruction
!= r_reading_until
)
572 internal_error (_("make_here_document: bad instruction type %d"), temp
->instruction
);
576 kill_leading
= temp
->instruction
== r_deblank_reading_until
;
578 document
= (char *)NULL
;
579 document_index
= document_size
= 0;
581 /* Quote removal is the only expansion performed on the delimiter
582 for here documents, making it an extremely special case. */
583 redir_word
= string_quote_removal (temp
->redirectee
.filename
->word
, 0);
585 /* redirection_expand will return NULL if the expansion results in
586 multiple words or no words. Check for that here, and just abort
587 this here document if it does. */
589 redir_len
= strlen (redir_word
);
592 temp
->here_doc_eof
= (char *)xmalloc (1);
593 temp
->here_doc_eof
[0] = '\0';
597 free (temp
->redirectee
.filename
->word
);
598 temp
->here_doc_eof
= redir_word
;
600 /* Read lines from wherever lines are coming from.
601 For each line read, if kill_leading, then kill the
602 leading tab characters.
603 If the line matches redir_word exactly, then we have
604 manufactured the document. Otherwise, add the line to the
605 list of lines in the document. */
607 /* If the here-document delimiter was quoted, the lines should
608 be read verbatim from the input. If it was not quoted, we
609 need to perform backslash-quoted newline removal. */
610 delim_unquoted
= (temp
->redirectee
.filename
->flags
& W_QUOTED
) == 0;
611 while (full_line
= read_secondary_line (delim_unquoted
))
616 here_doc_first_line
= 0;
620 /* If set -v is in effect, echo the line read. read_secondary_line/
621 read_a_line leaves the newline at the end, so don't print another. */
622 if (echo_input_at_read
)
623 fprintf (stderr
, "%s", line
);
625 if (kill_leading
&& *line
)
627 /* Hack: To be compatible with some Bourne shells, we
628 check the word before stripping the whitespace. This
629 is a hack, though. */
630 if (STREQN (line
, redir_word
, redir_len
) && line
[redir_len
] == '\n')
633 while (*line
== '\t')
640 if (STREQN (line
, redir_word
, redir_len
) && line
[redir_len
] == '\n')
644 if (len
+ document_index
>= document_size
)
646 document_size
= document_size
? 2 * (document_size
+ len
) : len
+ 2;
647 document
= (char *)xrealloc (document
, document_size
);
650 /* len is guaranteed to be > 0 because of the check for line
651 being an empty string before the call to strlen. */
652 FASTCOPY (line
, document
+ document_index
, len
);
653 document_index
+= len
;
657 internal_warning (_("here-document at line %d delimited by end-of-file (wanted `%s')"), lineno
, redir_word
);
661 document
[document_index
] = '\0';
664 document
= (char *)xmalloc (1);
667 temp
->redirectee
.filename
->word
= document
;
668 here_doc_first_line
= 0;
671 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
672 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
673 and DEST is a file descriptor or a WORD_DESC *. */
675 make_redirection (source
, instruction
, dest_and_filename
, flags
)
677 enum r_instruction instruction
;
678 REDIRECTEE dest_and_filename
;
686 temp
= (REDIRECT
*)xmalloc (sizeof (REDIRECT
));
688 /* First do the common cases. */
689 temp
->redirector
= source
;
690 temp
->redirectee
= dest_and_filename
;
691 temp
->here_doc_eof
= 0;
692 temp
->instruction
= instruction
;
694 temp
->rflags
= flags
;
695 temp
->next
= (REDIRECT
*)NULL
;
700 case r_output_direction
: /* >foo */
701 case r_output_force
: /* >| foo */
702 case r_err_and_out
: /* &>filename */
703 temp
->flags
= O_TRUNC
| O_WRONLY
| O_CREAT
;
706 case r_appending_to
: /* >>foo */
707 case r_append_err_and_out
: /* &>> filename */
708 temp
->flags
= O_APPEND
| O_WRONLY
| O_CREAT
;
711 case r_input_direction
: /* <foo */
712 case r_inputa_direction
: /* foo & makes this. */
713 temp
->flags
= O_RDONLY
;
716 case r_input_output
: /* <>foo */
717 temp
->flags
= O_RDWR
| O_CREAT
;
720 case r_deblank_reading_until
: /* <<-foo */
721 case r_reading_until
: /* << foo */
722 case r_reading_string
: /* <<< foo */
723 case r_close_this
: /* <&- */
724 case r_duplicating_input
: /* 1<&2 */
725 case r_duplicating_output
: /* 1>&2 */
728 /* the parser doesn't pass these. */
729 case r_move_input
: /* 1<&2- */
730 case r_move_output
: /* 1>&2- */
731 case r_move_input_word
: /* 1<&$foo- */
732 case r_move_output_word
: /* 1>&$foo- */
735 /* The way the lexer works we have to do this here. */
736 case r_duplicating_input_word
: /* 1<&$foo */
737 case r_duplicating_output_word
: /* 1>&$foo */
738 w
= dest_and_filename
.filename
;
739 wlen
= strlen (w
->word
) - 1;
740 if (w
->word
[wlen
] == '-') /* Yuck */
742 w
->word
[wlen
] = '\0';
743 if (all_digits (w
->word
) && legal_number (w
->word
, &lfd
) && lfd
== (int)lfd
)
746 temp
->instruction
= (instruction
== r_duplicating_input_word
) ? r_move_input
: r_move_output
;
747 temp
->redirectee
.dest
= lfd
;
750 temp
->instruction
= (instruction
== r_duplicating_input_word
) ? r_move_input_word
: r_move_output_word
;
756 programming_error (_("make_redirection: redirection instruction `%d' out of range"), instruction
);
764 make_function_def (name
, command
, lineno
, lstart
)
770 #if defined (ARRAY_VARS)
771 SHELL_VAR
*bush_source_v
;
772 ARRAY
*bush_source_a
;
775 temp
= (FUNCTION_DEF
*)xmalloc (sizeof (FUNCTION_DEF
));
776 temp
->command
= command
;
780 command
->line
= lstart
;
782 /* Information used primarily for debugging. */
783 temp
->source_file
= 0;
784 #if defined (ARRAY_VARS)
785 GET_ARRAY_FROM_VAR ("BUSH_SOURCE", bush_source_v
, bush_source_a
);
786 if (bush_source_a
&& array_num_elements (bush_source_a
) > 0)
787 temp
->source_file
= array_reference (bush_source_a
, 0);
789 /* Assume that shell functions without a source file before the shell is
790 initialized come from the environment. Otherwise default to "main"
791 (usually functions being defined interactively) */
792 if (temp
->source_file
== 0)
793 temp
->source_file
= shell_initialized
? "main" : "environment";
795 #if defined (DEBUGGER)
796 bind_function_def (name
->word
, temp
, 0);
799 temp
->source_file
= temp
->source_file
? savestring (temp
->source_file
) : 0;
801 return (make_command (cm_function_def
, (SIMPLE_COM
*)temp
));
805 make_subshell_command (command
)
810 temp
= (SUBSHELL_COM
*)xmalloc (sizeof (SUBSHELL_COM
));
811 temp
->command
= command
;
812 temp
->flags
= CMD_WANT_SUBSHELL
;
813 temp
->line
= line_number
;
814 return (make_command (cm_subshell
, (SIMPLE_COM
*)temp
));
818 make_coproc_command (name
, command
)
824 temp
= (COPROC_COM
*)xmalloc (sizeof (COPROC_COM
));
825 temp
->name
= savestring (name
);
826 temp
->command
= command
;
827 temp
->flags
= CMD_WANT_SUBSHELL
|CMD_COPROC_SUBSHELL
;
828 return (make_command (cm_coproc
, (SIMPLE_COM
*)temp
));
831 /* Reverse the word list and redirection list in the simple command
832 has just been parsed. It seems simpler to do this here the one
833 time then by any other method that I can think of. */
835 clean_simple_command (command
)
838 if (command
->type
!= cm_simple
)
839 command_error ("clean_simple_command", CMDERR_BADTYPE
, command
->type
, 0);
842 command
->value
.Simple
->words
=
843 REVERSE_LIST (command
->value
.Simple
->words
, WORD_LIST
*);
844 command
->value
.Simple
->redirects
=
845 REVERSE_LIST (command
->value
.Simple
->redirects
, REDIRECT
*);
848 parser_state
&= ~PST_REDIRLIST
;
852 /* The Yacc grammar productions have a problem, in that they take a
853 list followed by an ampersand (`&') and do a simple command connection,
854 making the entire list effectively asynchronous, instead of just
855 the last command. This means that when the list is executed, all
856 the commands have stdin set to /dev/null when job control is not
857 active, instead of just the last. This is wrong, and needs fixing
858 up. This function takes the `&' and applies it to the last command
859 in the list. This is done only for lists connected by `;'; it makes
860 `;' bind `tighter' than `&'. */
862 connect_async_list (command
, command2
, connector
)
863 COMMAND
*command
, *command2
;
866 COMMAND
*t
, *t1
, *t2
;
869 t
= command
->value
.Connection
->second
;
871 if (!t
|| (command
->flags
& CMD_WANT_SUBSHELL
) ||
872 command
->value
.Connection
->connector
!= ';')
874 t
= command_connect (command
, command2
, connector
);
878 /* This is just defensive programming. The Yacc precedence rules
879 will generally hand this function a command where t points directly
880 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
881 to the `a ; b ; c' list and t will be the `d'). We only want to do
882 this if the list is not being executed as a unit in the background
883 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
884 the only way to tell. */
885 while (((t
->flags
& CMD_WANT_SUBSHELL
) == 0) && t
->type
== cm_connection
&&
886 t
->value
.Connection
->connector
== ';')
889 t
= t
->value
.Connection
->second
;
891 /* Now we have t pointing to the last command in the list, and
892 t1->value.Connection->second == t. */
893 t2
= command_connect (t
, command2
, connector
);
894 t1
->value
.Connection
->second
= t2
;