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)
41 #include "variables.h"
46 #if defined (JOB_CONTROL)
50 extern int line_number
, current_command_line_count
;
51 extern int last_command_exit_value
;
53 static COMMAND
*make_for_or_select
__P((enum command_type
, WORD_DESC
*, WORD_LIST
*, COMMAND
*));
54 #if defined (ARITH_FOR_COMMAND)
55 static WORD_LIST
*make_arith_for_expr
__P((char *));
57 static COMMAND
*make_until_or_while
__P((enum command_type
, COMMAND
*, COMMAND
*));
60 make_bare_word (string
)
65 temp
= (WORD_DESC
*)xmalloc (sizeof (WORD_DESC
));
67 temp
->word
= savestring (string
);
70 temp
->word
= (char *)xmalloc (1);
79 make_word_flags (w
, string
)
83 register const char *s
;
85 for (s
= string
; *s
; s
++)
89 w
->flags
|= W_HASDOLLAR
;
92 break; /* continue the loop */
108 temp
= make_bare_word (string
);
109 return (make_word_flags (temp
, string
));
113 make_word_from_token (token
)
118 tokenizer
[0] = token
;
121 return (make_word (tokenizer
));
125 make_word_list (word
, wlink
)
131 temp
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
138 add_string_to_list (string
, list
)
144 temp
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
145 temp
->word
= make_word (string
);
151 make_command (type
, pointer
)
152 enum command_type type
;
157 temp
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
159 temp
->value
.Simple
= pointer
;
160 temp
->value
.Simple
->flags
= temp
->flags
= 0;
161 temp
->redirects
= (REDIRECT
*)NULL
;
166 command_connect (com1
, com2
, connector
)
167 COMMAND
*com1
, *com2
;
172 temp
= (CONNECTION
*)xmalloc (sizeof (CONNECTION
));
173 temp
->connector
= connector
;
176 return (make_command (cm_connection
, (SIMPLE_COM
*)temp
));
180 make_for_or_select (type
, name
, map_list
, action
)
181 enum command_type type
;
188 temp
= (FOR_COM
*)xmalloc (sizeof (FOR_COM
));
191 temp
->map_list
= map_list
;
192 temp
->action
= action
;
193 return (make_command (type
, (SIMPLE_COM
*)temp
));
197 make_for_command (name
, map_list
, action
)
202 return (make_for_or_select (cm_for
, name
, map_list
, action
));
206 make_select_command (name
, map_list
, action
)
211 #if defined (SELECT_COMMAND)
212 return (make_for_or_select (cm_select
, name
, map_list
, action
));
214 last_command_exit_value
= 2;
215 return ((COMMAND
*)NULL
);
219 #if defined (ARITH_FOR_COMMAND)
221 make_arith_for_expr (s
)
227 if (s
== 0 || *s
== '\0')
228 return ((WORD_LIST
*)NULL
);
230 result
= make_word_list (w
, (WORD_LIST
*)NULL
);
236 make_arith_for_command (exprs
, action
, lineno
)
241 #if defined (ARITH_FOR_COMMAND)
243 WORD_LIST
*init
, *test
, *step
;
247 init
= test
= step
= (WORD_LIST
*)NULL
;
248 /* Parse the string into the three component sub-expressions. */
249 start
= t
= s
= exprs
->word
->word
;
252 /* skip whitespace at the start of each sub-expression. */
253 while (whitespace (*s
))
256 /* skip to the semicolon or EOS */
257 while (*s
&& *s
!= ';')
260 t
= (s
> start
) ? substring (start
, 0, s
- start
) : (char *)NULL
;
266 init
= make_arith_for_expr (t
);
269 test
= make_arith_for_expr (t
);
272 step
= make_arith_for_expr (t
);
279 s
++; /* skip over semicolon */
285 parser_error (lineno
, "syntax error: arithmetic expression required");
287 parser_error (lineno
, "syntax error: `;' unexpected");
288 parser_error (lineno
, "syntax error: `((%s))'", exprs
->word
->word
);
289 last_command_exit_value
= 2;
290 return ((COMMAND
*)NULL
);
293 temp
= (ARITH_FOR_COM
*)xmalloc (sizeof (ARITH_FOR_COM
));
296 temp
->init
= init
? init
: make_arith_for_expr ("1");
297 temp
->test
= test
? test
: make_arith_for_expr ("1");
298 temp
->step
= step
? step
: make_arith_for_expr ("1");
299 temp
->action
= action
;
301 return (make_command (cm_arith_for
, (SIMPLE_COM
*)temp
));
303 last_command_exit_value
= 2;
304 return ((COMMAND
*)NULL
);
305 #endif /* ARITH_FOR_COMMAND */
309 make_group_command (command
)
314 temp
= (GROUP_COM
*)xmalloc (sizeof (GROUP_COM
));
315 temp
->command
= command
;
316 return (make_command (cm_group
, (SIMPLE_COM
*)temp
));
320 make_case_command (word
, clauses
)
322 PATTERN_LIST
*clauses
;
326 temp
= (CASE_COM
*)xmalloc (sizeof (CASE_COM
));
329 temp
->clauses
= REVERSE_LIST (clauses
, PATTERN_LIST
*);
330 return (make_command (cm_case
, (SIMPLE_COM
*)temp
));
334 make_pattern_list (patterns
, action
)
340 temp
= (PATTERN_LIST
*)xmalloc (sizeof (PATTERN_LIST
));
341 temp
->patterns
= REVERSE_LIST (patterns
, WORD_LIST
*);
342 temp
->action
= action
;
348 make_if_command (test
, true_case
, false_case
)
349 COMMAND
*test
, *true_case
, *false_case
;
353 temp
= (IF_COM
*)xmalloc (sizeof (IF_COM
));
356 temp
->true_case
= true_case
;
357 temp
->false_case
= false_case
;
358 return (make_command (cm_if
, (SIMPLE_COM
*)temp
));
362 make_until_or_while (which
, test
, action
)
363 enum command_type which
;
364 COMMAND
*test
, *action
;
368 temp
= (WHILE_COM
*)xmalloc (sizeof (WHILE_COM
));
371 temp
->action
= action
;
372 return (make_command (which
, (SIMPLE_COM
*)temp
));
376 make_while_command (test
, action
)
377 COMMAND
*test
, *action
;
379 return (make_until_or_while (cm_while
, test
, action
));
383 make_until_command (test
, action
)
384 COMMAND
*test
, *action
;
386 return (make_until_or_while (cm_until
, test
, action
));
390 make_arith_command (exp
)
393 #if defined (DPAREN_ARITHMETIC)
397 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
398 command
->value
.Arith
= temp
= (ARITH_COM
*)xmalloc (sizeof (ARITH_COM
));
401 temp
->line
= line_number
;
404 command
->type
= cm_arith
;
405 command
->redirects
= (REDIRECT
*)NULL
;
410 last_command_exit_value
= 2;
411 return ((COMMAND
*)NULL
);
415 #if defined (COND_COMMAND)
417 make_cond_node (type
, op
, left
, right
)
420 struct cond_com
*left
, *right
;
424 temp
= (COND_COM
*)xmalloc (sizeof (COND_COM
));
426 temp
->line
= line_number
;
437 make_cond_command (cond_node
)
440 #if defined (COND_COMMAND)
443 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
444 command
->value
.Cond
= cond_node
;
446 command
->type
= cm_cond
;
447 command
->redirects
= (REDIRECT
*)NULL
;
449 command
->line
= cond_node
? cond_node
->line
: 0;
453 last_command_exit_value
= 2;
454 return ((COMMAND
*)NULL
);
459 make_bare_simple_command ()
464 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
465 command
->value
.Simple
= temp
= (SIMPLE_COM
*)xmalloc (sizeof (SIMPLE_COM
));
468 temp
->line
= line_number
;
469 temp
->words
= (WORD_LIST
*)NULL
;
470 temp
->redirects
= (REDIRECT
*)NULL
;
472 command
->type
= cm_simple
;
473 command
->redirects
= (REDIRECT
*)NULL
;
479 /* Return a command which is the connection of the word or redirection
480 in ELEMENT, and the command * or NULL in COMMAND. */
482 make_simple_command (element
, command
)
486 /* If we are starting from scratch, then make the initial command
487 structure. Also note that we have to fill in all the slots, since
488 malloc doesn't return zeroed space. */
490 command
= make_bare_simple_command ();
494 WORD_LIST
*tw
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
495 tw
->word
= element
.word
;
496 tw
->next
= command
->value
.Simple
->words
;
497 command
->value
.Simple
->words
= tw
;
501 REDIRECT
*r
= element
.redirect
;
502 /* Due to the way <> is implemented, there may be more than a single
503 redirection in element.redirect. We just follow the chain as far
504 as it goes, and hook onto the end. */
507 r
->next
= command
->value
.Simple
->redirects
;
508 command
->value
.Simple
->redirects
= element
.redirect
;
513 /* Because we are Bourne compatible, we read the input for this
514 << or <<- redirection now, from wherever input is coming from.
515 We store the input read into a WORD_DESC. Replace the text of
516 the redirectee.word with the new input text. If <<- is on,
517 then remove leading TABS from each line. */
519 make_here_document (temp
)
522 int kill_leading
, redir_len
;
523 char *redir_word
, *document
, *full_line
;
524 int document_index
, document_size
, delim_unquoted
;
526 if (temp
->instruction
!= r_deblank_reading_until
&&
527 temp
->instruction
!= r_reading_until
)
529 internal_error ("make_here_document: bad instruction type %d", temp
->instruction
);
533 kill_leading
= temp
->instruction
== r_deblank_reading_until
;
535 document
= (char *)NULL
;
536 document_index
= document_size
= 0;
538 /* Quote removal is the only expansion performed on the delimiter
539 for here documents, making it an extremely special case. */
540 redir_word
= string_quote_removal (temp
->redirectee
.filename
->word
, 0);
542 /* redirection_expand will return NULL if the expansion results in
543 multiple words or no words. Check for that here, and just abort
544 this here document if it does. */
546 redir_len
= strlen (redir_word
);
549 temp
->here_doc_eof
= (char *)xmalloc (1);
550 temp
->here_doc_eof
[0] = '\0';
554 free (temp
->redirectee
.filename
->word
);
555 temp
->here_doc_eof
= redir_word
;
557 /* Read lines from wherever lines are coming from.
558 For each line read, if kill_leading, then kill the
559 leading tab characters.
560 If the line matches redir_word exactly, then we have
561 manufactured the document. Otherwise, add the line to the
562 list of lines in the document. */
564 /* If the here-document delimiter was quoted, the lines should
565 be read verbatim from the input. If it was not quoted, we
566 need to perform backslash-quoted newline removal. */
567 delim_unquoted
= (temp
->redirectee
.filename
->flags
& W_QUOTED
) == 0;
568 while (full_line
= read_secondary_line (delim_unquoted
))
576 if (kill_leading
&& *line
)
578 /* Hack: To be compatible with some Bourne shells, we
579 check the word before stripping the whitespace. This
580 is a hack, though. */
581 if (STREQN (line
, redir_word
, redir_len
) && line
[redir_len
] == '\n')
584 while (*line
== '\t')
591 if (STREQN (line
, redir_word
, redir_len
) && line
[redir_len
] == '\n')
595 if (len
+ document_index
>= document_size
)
597 document_size
= document_size
? 2 * (document_size
+ len
) : len
+ 2;
598 document
= (char *)xrealloc (document
, document_size
);
601 /* len is guaranteed to be > 0 because of the check for line
602 being an empty string before the call to strlen. */
603 FASTCOPY (line
, document
+ document_index
, len
);
604 document_index
+= len
;
609 document
[document_index
] = '\0';
612 document
= (char *)xmalloc (1);
615 temp
->redirectee
.filename
->word
= document
;
618 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
619 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
620 and DEST is a file descriptor or a WORD_DESC *. */
622 make_redirection (source
, instruction
, dest_and_filename
)
624 enum r_instruction instruction
;
625 REDIRECTEE dest_and_filename
;
627 REDIRECT
*temp
= (REDIRECT
*)xmalloc (sizeof (REDIRECT
));
629 /* First do the common cases. */
630 temp
->redirector
= source
;
631 temp
->redirectee
= dest_and_filename
;
632 temp
->instruction
= instruction
;
634 temp
->next
= (REDIRECT
*)NULL
;
639 case r_output_direction
: /* >foo */
640 case r_output_force
: /* >| foo */
641 case r_err_and_out
: /* command &>filename */
642 temp
->flags
= O_TRUNC
| O_WRONLY
| O_CREAT
;
645 case r_appending_to
: /* >>foo */
646 temp
->flags
= O_APPEND
| O_WRONLY
| O_CREAT
;
649 case r_input_direction
: /* <foo */
650 case r_inputa_direction
: /* foo & makes this. */
651 temp
->flags
= O_RDONLY
;
654 case r_input_output
: /* <>foo */
655 temp
->flags
= O_RDWR
| O_CREAT
;
658 case r_deblank_reading_until
: /* <<-foo */
659 case r_reading_until
: /* << foo */
660 case r_close_this
: /* <&- */
661 case r_duplicating_input
: /* 1<&2 */
662 case r_duplicating_output
: /* 1>&2 */
663 case r_duplicating_input_word
: /* 1<&$foo */
664 case r_duplicating_output_word
: /* 1>&$foo */
668 programming_error ("make_redirection: redirection instruction `%d' out of range", instruction
);
676 make_function_def (name
, command
, lineno
, lstart
)
683 temp
= (FUNCTION_DEF
*)xmalloc (sizeof (FUNCTION_DEF
));
684 temp
->command
= command
;
688 command
->line
= lstart
;
689 return (make_command (cm_function_def
, (SIMPLE_COM
*)temp
));
693 make_subshell_command (command
)
698 temp
= (SUBSHELL_COM
*)xmalloc (sizeof (SUBSHELL_COM
));
699 temp
->command
= command
;
700 temp
->flags
= CMD_WANT_SUBSHELL
;
701 return (make_command (cm_subshell
, (SIMPLE_COM
*)temp
));
704 /* Reverse the word list and redirection list in the simple command
705 has just been parsed. It seems simpler to do this here the one
706 time then by any other method that I can think of. */
708 clean_simple_command (command
)
711 if (command
->type
!= cm_simple
)
712 command_error ("clean_simple_command", CMDERR_BADTYPE
, command
->type
, 0);
715 command
->value
.Simple
->words
=
716 REVERSE_LIST (command
->value
.Simple
->words
, WORD_LIST
*);
717 command
->value
.Simple
->redirects
=
718 REVERSE_LIST (command
->value
.Simple
->redirects
, REDIRECT
*);
724 /* The Yacc grammar productions have a problem, in that they take a
725 list followed by an ampersand (`&') and do a simple command connection,
726 making the entire list effectively asynchronous, instead of just
727 the last command. This means that when the list is executed, all
728 the commands have stdin set to /dev/null when job control is not
729 active, instead of just the last. This is wrong, and needs fixing
730 up. This function takes the `&' and applies it to the last command
731 in the list. This is done only for lists connected by `;'; it makes
732 `;' bind `tighter' than `&'. */
734 connect_async_list (command
, command2
, connector
)
735 COMMAND
*command
, *command2
;
738 COMMAND
*t
, *t1
, *t2
;
741 t
= command
->value
.Connection
->second
;
743 if (!t
|| (command
->flags
& CMD_WANT_SUBSHELL
) ||
744 command
->value
.Connection
->connector
!= ';')
746 t
= command_connect (command
, command2
, connector
);
750 /* This is just defensive programming. The Yacc precedence rules
751 will generally hand this function a command where t points directly
752 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
753 to the `a ; b ; c' list and t will be the `d'). We only want to do
754 this if the list is not being executed as a unit in the background
755 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
756 the only way to tell. */
757 while (((t
->flags
& CMD_WANT_SUBSHELL
) == 0) && t
->type
== cm_connection
&&
758 t
->value
.Connection
->connector
== ';')
761 t
= t
->value
.Connection
->second
;
763 /* Now we have t pointing to the last command in the list, and
764 t1->value.Connection->second == t. */
765 t2
= command_connect (t
, command2
, connector
);
766 t1
->value
.Connection
->second
= t2
;