1 /* make_cmd.c -- Functions for making instances of the various
4 /* Copyright (C) 1989-2005 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"
26 #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
27 # include <sys/file.h>
31 #if defined (HAVE_UNISTD_H)
43 #include "dispose_cmd.h"
44 #include "variables.h"
50 #if defined (JOB_CONTROL)
56 extern int line_number
, current_command_line_count
;
57 extern int last_command_exit_value
;
60 sh_obj_cache_t wdcache
= {0, 0, 0};
61 sh_obj_cache_t wlcache
= {0, 0, 0};
63 #define WDCACHESIZE 60
64 #define WLCACHESIZE 60
66 static COMMAND
*make_for_or_select
__P((enum command_type
, WORD_DESC
*, WORD_LIST
*, COMMAND
*, int));
67 #if defined (ARITH_FOR_COMMAND)
68 static WORD_LIST
*make_arith_for_expr
__P((char *));
70 static COMMAND
*make_until_or_while
__P((enum command_type
, COMMAND
*, COMMAND
*));
75 ocache_create (wdcache
, WORD_DESC
, WDCACHESIZE
);
76 ocache_create (wlcache
, WORD_LIST
, WLCACHESIZE
);
84 ocache_alloc (wdcache
, WORD_DESC
, temp
);
91 make_bare_word (string
)
96 temp
= alloc_word_desc ();
99 temp
->word
= savestring (string
);
102 temp
->word
= (char *)xmalloc (1);
103 temp
->word
[0] = '\0';
110 make_word_flags (w
, string
)
119 slen
= strlen (string
);
125 w
->flags
|= W_HASDOLLAR
;
128 break; /* continue the loop */
132 w
->flags
|= W_QUOTED
;
136 ADVANCE_CHAR (string
, slen
, i
);
148 temp
= make_bare_word (string
);
149 return (make_word_flags (temp
, string
));
153 make_word_from_token (token
)
158 tokenizer
[0] = token
;
161 return (make_word (tokenizer
));
165 make_word_list (word
, wlink
)
171 ocache_alloc (wlcache
, WORD_LIST
, temp
);
179 make_command (type
, pointer
)
180 enum command_type type
;
185 temp
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
187 temp
->value
.Simple
= pointer
;
188 temp
->value
.Simple
->flags
= temp
->flags
= 0;
189 temp
->redirects
= (REDIRECT
*)NULL
;
194 command_connect (com1
, com2
, connector
)
195 COMMAND
*com1
, *com2
;
200 temp
= (CONNECTION
*)xmalloc (sizeof (CONNECTION
));
201 temp
->connector
= connector
;
204 return (make_command (cm_connection
, (SIMPLE_COM
*)temp
));
208 make_for_or_select (type
, name
, map_list
, action
, lineno
)
209 enum command_type type
;
217 temp
= (FOR_COM
*)xmalloc (sizeof (FOR_COM
));
221 temp
->map_list
= map_list
;
222 temp
->action
= action
;
223 return (make_command (type
, (SIMPLE_COM
*)temp
));
227 make_for_command (name
, map_list
, action
, lineno
)
233 return (make_for_or_select (cm_for
, name
, map_list
, action
, lineno
));
237 make_select_command (name
, map_list
, action
, lineno
)
243 #if defined (SELECT_COMMAND)
244 return (make_for_or_select (cm_select
, name
, map_list
, action
, lineno
));
246 last_command_exit_value
= 2;
247 return ((COMMAND
*)NULL
);
251 #if defined (ARITH_FOR_COMMAND)
253 make_arith_for_expr (s
)
259 if (s
== 0 || *s
== '\0')
260 return ((WORD_LIST
*)NULL
);
262 wd
->flags
|= W_NOGLOB
|W_NOSPLIT
|W_QUOTED
|W_DQUOTE
; /* no word splitting or globbing */
263 result
= make_word_list (wd
, (WORD_LIST
*)NULL
);
268 /* Note that this function calls dispose_words on EXPRS, since it doesn't
269 use the word list directly. We free it here rather than at the caller
270 because no other function in this file requires that the caller free
273 make_arith_for_command (exprs
, action
, lineno
)
278 #if defined (ARITH_FOR_COMMAND)
280 WORD_LIST
*init
, *test
, *step
;
284 init
= test
= step
= (WORD_LIST
*)NULL
;
285 /* Parse the string into the three component sub-expressions. */
286 start
= t
= s
= exprs
->word
->word
;
289 /* skip whitespace at the start of each sub-expression. */
290 while (whitespace (*s
))
293 /* skip to the semicolon or EOS */
294 while (*s
&& *s
!= ';')
297 t
= (s
> start
) ? substring (start
, 0, s
- start
) : (char *)NULL
;
303 init
= make_arith_for_expr (t
);
306 test
= make_arith_for_expr (t
);
309 step
= make_arith_for_expr (t
);
316 s
++; /* skip over semicolon */
322 parser_error (lineno
, _("syntax error: arithmetic expression required"));
324 parser_error (lineno
, _("syntax error: `;' unexpected"));
325 parser_error (lineno
, _("syntax error: `((%s))'"), exprs
->word
->word
);
326 last_command_exit_value
= 2;
327 return ((COMMAND
*)NULL
);
330 temp
= (ARITH_FOR_COM
*)xmalloc (sizeof (ARITH_FOR_COM
));
333 temp
->init
= init
? init
: make_arith_for_expr ("1");
334 temp
->test
= test
? test
: make_arith_for_expr ("1");
335 temp
->step
= step
? step
: make_arith_for_expr ("1");
336 temp
->action
= action
;
338 dispose_words (exprs
);
339 return (make_command (cm_arith_for
, (SIMPLE_COM
*)temp
));
341 dispose_words (exprs
);
342 last_command_exit_value
= 2;
343 return ((COMMAND
*)NULL
);
344 #endif /* ARITH_FOR_COMMAND */
348 make_group_command (command
)
353 temp
= (GROUP_COM
*)xmalloc (sizeof (GROUP_COM
));
354 temp
->command
= command
;
355 return (make_command (cm_group
, (SIMPLE_COM
*)temp
));
359 make_case_command (word
, clauses
, lineno
)
361 PATTERN_LIST
*clauses
;
366 temp
= (CASE_COM
*)xmalloc (sizeof (CASE_COM
));
370 temp
->clauses
= REVERSE_LIST (clauses
, PATTERN_LIST
*);
371 return (make_command (cm_case
, (SIMPLE_COM
*)temp
));
375 make_pattern_list (patterns
, action
)
381 temp
= (PATTERN_LIST
*)xmalloc (sizeof (PATTERN_LIST
));
382 temp
->patterns
= REVERSE_LIST (patterns
, WORD_LIST
*);
383 temp
->action
= action
;
390 make_if_command (test
, true_case
, false_case
)
391 COMMAND
*test
, *true_case
, *false_case
;
395 temp
= (IF_COM
*)xmalloc (sizeof (IF_COM
));
398 temp
->true_case
= true_case
;
399 temp
->false_case
= false_case
;
400 return (make_command (cm_if
, (SIMPLE_COM
*)temp
));
404 make_until_or_while (which
, test
, action
)
405 enum command_type which
;
406 COMMAND
*test
, *action
;
410 temp
= (WHILE_COM
*)xmalloc (sizeof (WHILE_COM
));
413 temp
->action
= action
;
414 return (make_command (which
, (SIMPLE_COM
*)temp
));
418 make_while_command (test
, action
)
419 COMMAND
*test
, *action
;
421 return (make_until_or_while (cm_while
, test
, action
));
425 make_until_command (test
, action
)
426 COMMAND
*test
, *action
;
428 return (make_until_or_while (cm_until
, test
, action
));
432 make_arith_command (exp
)
435 #if defined (DPAREN_ARITHMETIC)
439 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
440 command
->value
.Arith
= temp
= (ARITH_COM
*)xmalloc (sizeof (ARITH_COM
));
443 temp
->line
= line_number
;
446 command
->type
= cm_arith
;
447 command
->redirects
= (REDIRECT
*)NULL
;
452 last_command_exit_value
= 2;
453 return ((COMMAND
*)NULL
);
457 #if defined (COND_COMMAND)
459 make_cond_node (type
, op
, left
, right
)
462 struct cond_com
*left
, *right
;
466 temp
= (COND_COM
*)xmalloc (sizeof (COND_COM
));
468 temp
->line
= line_number
;
479 make_cond_command (cond_node
)
482 #if defined (COND_COMMAND)
485 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
486 command
->value
.Cond
= cond_node
;
488 command
->type
= cm_cond
;
489 command
->redirects
= (REDIRECT
*)NULL
;
491 command
->line
= cond_node
? cond_node
->line
: 0;
495 last_command_exit_value
= 2;
496 return ((COMMAND
*)NULL
);
501 make_bare_simple_command ()
506 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
507 command
->value
.Simple
= temp
= (SIMPLE_COM
*)xmalloc (sizeof (SIMPLE_COM
));
510 temp
->line
= line_number
;
511 temp
->words
= (WORD_LIST
*)NULL
;
512 temp
->redirects
= (REDIRECT
*)NULL
;
514 command
->type
= cm_simple
;
515 command
->redirects
= (REDIRECT
*)NULL
;
521 /* Return a command which is the connection of the word or redirection
522 in ELEMENT, and the command * or NULL in COMMAND. */
524 make_simple_command (element
, command
)
528 /* If we are starting from scratch, then make the initial command
529 structure. Also note that we have to fill in all the slots, since
530 malloc doesn't return zeroed space. */
532 command
= make_bare_simple_command ();
535 command
->value
.Simple
->words
= make_word_list (element
.word
, command
->value
.Simple
->words
);
536 else if (element
.redirect
)
538 REDIRECT
*r
= element
.redirect
;
539 /* Due to the way <> is implemented, there may be more than a single
540 redirection in element.redirect. We just follow the chain as far
541 as it goes, and hook onto the end. */
544 r
->next
= command
->value
.Simple
->redirects
;
545 command
->value
.Simple
->redirects
= element
.redirect
;
550 /* Because we are Bourne compatible, we read the input for this
551 << or <<- redirection now, from wherever input is coming from.
552 We store the input read into a WORD_DESC. Replace the text of
553 the redirectee.word with the new input text. If <<- is on,
554 then remove leading TABS from each line. */
556 make_here_document (temp
)
559 int kill_leading
, redir_len
;
560 char *redir_word
, *document
, *full_line
;
561 int document_index
, document_size
, delim_unquoted
;
563 if (temp
->instruction
!= r_deblank_reading_until
&&
564 temp
->instruction
!= r_reading_until
)
566 internal_error (_("make_here_document: bad instruction type %d"), temp
->instruction
);
570 kill_leading
= temp
->instruction
== r_deblank_reading_until
;
572 document
= (char *)NULL
;
573 document_index
= document_size
= 0;
575 /* Quote removal is the only expansion performed on the delimiter
576 for here documents, making it an extremely special case. */
577 redir_word
= string_quote_removal (temp
->redirectee
.filename
->word
, 0);
579 /* redirection_expand will return NULL if the expansion results in
580 multiple words or no words. Check for that here, and just abort
581 this here document if it does. */
583 redir_len
= strlen (redir_word
);
586 temp
->here_doc_eof
= (char *)xmalloc (1);
587 temp
->here_doc_eof
[0] = '\0';
591 free (temp
->redirectee
.filename
->word
);
592 temp
->here_doc_eof
= redir_word
;
594 /* Read lines from wherever lines are coming from.
595 For each line read, if kill_leading, then kill the
596 leading tab characters.
597 If the line matches redir_word exactly, then we have
598 manufactured the document. Otherwise, add the line to the
599 list of lines in the document. */
601 /* If the here-document delimiter was quoted, the lines should
602 be read verbatim from the input. If it was not quoted, we
603 need to perform backslash-quoted newline removal. */
604 delim_unquoted
= (temp
->redirectee
.filename
->flags
& W_QUOTED
) == 0;
605 while (full_line
= read_secondary_line (delim_unquoted
))
613 /* If set -v is in effect, echo the line read. read_secondary_line/
614 read_a_line leaves the newline at the end, so don't print another. */
615 if (echo_input_at_read
)
616 fprintf (stderr
, "%s", line
);
618 if (kill_leading
&& *line
)
620 /* Hack: To be compatible with some Bourne shells, we
621 check the word before stripping the whitespace. This
622 is a hack, though. */
623 if (STREQN (line
, redir_word
, redir_len
) && line
[redir_len
] == '\n')
626 while (*line
== '\t')
633 if (STREQN (line
, redir_word
, redir_len
) && line
[redir_len
] == '\n')
637 if (len
+ document_index
>= document_size
)
639 document_size
= document_size
? 2 * (document_size
+ len
) : len
+ 2;
640 document
= (char *)xrealloc (document
, document_size
);
643 /* len is guaranteed to be > 0 because of the check for line
644 being an empty string before the call to strlen. */
645 FASTCOPY (line
, document
+ document_index
, len
);
646 document_index
+= len
;
651 document
[document_index
] = '\0';
654 document
= (char *)xmalloc (1);
657 temp
->redirectee
.filename
->word
= document
;
660 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
661 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
662 and DEST is a file descriptor or a WORD_DESC *. */
664 make_redirection (source
, instruction
, dest_and_filename
)
666 enum r_instruction instruction
;
667 REDIRECTEE dest_and_filename
;
674 temp
= (REDIRECT
*)xmalloc (sizeof (REDIRECT
));
676 /* First do the common cases. */
677 temp
->redirector
= source
;
678 temp
->redirectee
= dest_and_filename
;
679 temp
->instruction
= instruction
;
681 temp
->next
= (REDIRECT
*)NULL
;
686 case r_output_direction
: /* >foo */
687 case r_output_force
: /* >| foo */
688 case r_err_and_out
: /* command &>filename */
689 temp
->flags
= O_TRUNC
| O_WRONLY
| O_CREAT
;
692 case r_appending_to
: /* >>foo */
693 temp
->flags
= O_APPEND
| O_WRONLY
| O_CREAT
;
696 case r_input_direction
: /* <foo */
697 case r_inputa_direction
: /* foo & makes this. */
698 temp
->flags
= O_RDONLY
;
701 case r_input_output
: /* <>foo */
702 temp
->flags
= O_RDWR
| O_CREAT
;
705 case r_deblank_reading_until
: /* <<-foo */
706 case r_reading_until
: /* << foo */
707 case r_reading_string
: /* <<< foo */
708 case r_close_this
: /* <&- */
709 case r_duplicating_input
: /* 1<&2 */
710 case r_duplicating_output
: /* 1>&2 */
713 /* the parser doesn't pass these. */
714 case r_move_input
: /* 1<&2- */
715 case r_move_output
: /* 1>&2- */
716 case r_move_input_word
: /* 1<&$foo- */
717 case r_move_output_word
: /* 1>&$foo- */
720 /* The way the lexer works we have to do this here. */
721 case r_duplicating_input_word
: /* 1<&$foo */
722 case r_duplicating_output_word
: /* 1>&$foo */
723 w
= dest_and_filename
.filename
;
724 wlen
= strlen (w
->word
) - 1;
725 if (w
->word
[wlen
] == '-') /* Yuck */
727 w
->word
[wlen
] = '\0';
728 if (all_digits (w
->word
) && legal_number (w
->word
, &lfd
) && lfd
== (int)lfd
)
731 temp
->instruction
= (instruction
== r_duplicating_input_word
) ? r_move_input
: r_move_output
;
732 temp
->redirectee
.dest
= lfd
;
735 temp
->instruction
= (instruction
== r_duplicating_input_word
) ? r_move_input_word
: r_move_output_word
;
741 programming_error (_("make_redirection: redirection instruction `%d' out of range"), instruction
);
749 make_function_def (name
, command
, lineno
, lstart
)
755 #if defined (ARRAY_VARS)
756 SHELL_VAR
*bash_source_v
;
757 ARRAY
*bash_source_a
;
760 temp
= (FUNCTION_DEF
*)xmalloc (sizeof (FUNCTION_DEF
));
761 temp
->command
= command
;
765 command
->line
= lstart
;
767 /* Information used primarily for debugging. */
768 temp
->source_file
= 0;
769 #if defined (ARRAY_VARS)
770 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v
, bash_source_a
);
771 if (bash_source_a
&& array_num_elements (bash_source_a
) > 0)
772 temp
->source_file
= array_reference (bash_source_a
, 0);
774 bind_function_def (name
->word
, temp
);
776 temp
->source_file
= 0;
777 return (make_command (cm_function_def
, (SIMPLE_COM
*)temp
));
781 make_subshell_command (command
)
786 temp
= (SUBSHELL_COM
*)xmalloc (sizeof (SUBSHELL_COM
));
787 temp
->command
= command
;
788 temp
->flags
= CMD_WANT_SUBSHELL
;
789 return (make_command (cm_subshell
, (SIMPLE_COM
*)temp
));
792 /* Reverse the word list and redirection list in the simple command
793 has just been parsed. It seems simpler to do this here the one
794 time then by any other method that I can think of. */
796 clean_simple_command (command
)
799 if (command
->type
!= cm_simple
)
800 command_error ("clean_simple_command", CMDERR_BADTYPE
, command
->type
, 0);
803 command
->value
.Simple
->words
=
804 REVERSE_LIST (command
->value
.Simple
->words
, WORD_LIST
*);
805 command
->value
.Simple
->redirects
=
806 REVERSE_LIST (command
->value
.Simple
->redirects
, REDIRECT
*);
812 /* The Yacc grammar productions have a problem, in that they take a
813 list followed by an ampersand (`&') and do a simple command connection,
814 making the entire list effectively asynchronous, instead of just
815 the last command. This means that when the list is executed, all
816 the commands have stdin set to /dev/null when job control is not
817 active, instead of just the last. This is wrong, and needs fixing
818 up. This function takes the `&' and applies it to the last command
819 in the list. This is done only for lists connected by `;'; it makes
820 `;' bind `tighter' than `&'. */
822 connect_async_list (command
, command2
, connector
)
823 COMMAND
*command
, *command2
;
826 COMMAND
*t
, *t1
, *t2
;
829 t
= command
->value
.Connection
->second
;
831 if (!t
|| (command
->flags
& CMD_WANT_SUBSHELL
) ||
832 command
->value
.Connection
->connector
!= ';')
834 t
= command_connect (command
, command2
, connector
);
838 /* This is just defensive programming. The Yacc precedence rules
839 will generally hand this function a command where t points directly
840 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
841 to the `a ; b ; c' list and t will be the `d'). We only want to do
842 this if the list is not being executed as a unit in the background
843 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
844 the only way to tell. */
845 while (((t
->flags
& CMD_WANT_SUBSHELL
) == 0) && t
->type
== cm_connection
&&
846 t
->value
.Connection
->connector
== ';')
849 t
= t
->value
.Connection
->second
;
851 /* Now we have t pointing to the last command in the list, and
852 t1->value.Connection->second == t. */
853 t2
= command_connect (t
, command2
, connector
);
854 t1
->value
.Connection
->second
= t2
;