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 disallow_filename_globbing
;
52 extern int last_command_exit_value
;
56 make_bare_word (string
)
61 temp
= (WORD_DESC
*)xmalloc (sizeof (WORD_DESC
));
63 temp
->word
= savestring (string
);
66 temp
->word
= xmalloc (1);
75 make_word_flags (w
, string
)
81 for (s
= string
; *s
; s
++)
85 w
->flags
|= W_HASDOLLAR
;
88 break; /* continue the loop */
104 temp
= make_bare_word (string
);
105 return (make_word_flags (temp
, string
));
109 make_word_from_token (token
)
114 tokenizer
[0] = token
;
117 return (make_word (tokenizer
));
121 make_word_list (word
, link
)
127 temp
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
134 add_string_to_list (string
, list
)
140 temp
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
141 temp
->word
= make_word (string
);
147 make_command (type
, pointer
)
148 enum command_type type
;
153 temp
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
155 temp
->value
.Simple
= pointer
;
156 temp
->value
.Simple
->flags
= temp
->flags
= 0;
157 temp
->redirects
= (REDIRECT
*)NULL
;
162 command_connect (com1
, com2
, connector
)
163 COMMAND
*com1
, *com2
;
168 temp
= (CONNECTION
*)xmalloc (sizeof (CONNECTION
));
169 temp
->connector
= connector
;
172 return (make_command (cm_connection
, (SIMPLE_COM
*)temp
));
176 make_for_or_select (type
, name
, map_list
, action
)
177 enum command_type type
;
184 temp
= (FOR_COM
*)xmalloc (sizeof (FOR_COM
));
187 temp
->map_list
= map_list
;
188 temp
->action
= action
;
189 return (make_command (type
, (SIMPLE_COM
*)temp
));
193 make_for_command (name
, map_list
, action
)
198 return (make_for_or_select (cm_for
, name
, map_list
, action
));
202 make_select_command (name
, map_list
, action
)
207 #if defined (SELECT_COMMAND)
208 return (make_for_or_select (cm_select
, name
, map_list
, action
));
210 last_command_exit_value
= 2;
211 return ((COMMAND
*)NULL
);
215 #if defined (ARITH_FOR_COMMAND)
217 make_arith_for_expr (s
)
223 if (s
== 0 || *s
== '\0')
224 return ((WORD_LIST
*)NULL
);
226 result
= make_word_list (w
, (WORD_LIST
*)NULL
);
232 make_arith_for_command (exprs
, action
, lineno
)
237 #if defined (ARITH_FOR_COMMAND)
239 WORD_LIST
*init
, *test
, *step
;
243 init
= test
= step
= (WORD_LIST
*)NULL
;
244 /* Parse the string into the three component sub-expressions. */
245 start
= t
= s
= exprs
->word
->word
;
248 /* skip whitespace at the start of each sub-expression. */
249 while (whitespace (*s
))
252 /* skip to the semicolon or EOS */
253 while (*s
&& *s
!= ';')
256 t
= (s
> start
) ? substring (start
, 0, s
- start
) : (char *)NULL
;
262 init
= make_arith_for_expr (t
);
265 test
= make_arith_for_expr (t
);
268 step
= make_arith_for_expr (t
);
275 s
++; /* skip over semicolon */
281 parser_error (lineno
, "syntax error: arithmetic expression required");
283 parser_error (lineno
, "syntax error: `;' unexpected");
284 parser_error (lineno
, "syntax error: `((%s))'", exprs
->word
->word
);
285 last_command_exit_value
= 2;
286 return ((COMMAND
*)NULL
);
289 temp
= (ARITH_FOR_COM
*)xmalloc (sizeof (ARITH_FOR_COM
));
292 temp
->init
= init
? init
: make_arith_for_expr ("1");
293 temp
->test
= test
? test
: make_arith_for_expr ("1");
294 temp
->step
= step
? step
: make_arith_for_expr ("1");
295 temp
->action
= action
;
297 return (make_command (cm_arith_for
, (SIMPLE_COM
*)temp
));
299 last_command_exit_value
= 2;
300 return ((COMMAND
*)NULL
);
301 #endif /* ARITH_FOR_COMMAND */
305 make_group_command (command
)
310 temp
= (GROUP_COM
*)xmalloc (sizeof (GROUP_COM
));
311 temp
->command
= command
;
312 return (make_command (cm_group
, (SIMPLE_COM
*)temp
));
316 make_case_command (word
, clauses
)
318 PATTERN_LIST
*clauses
;
322 temp
= (CASE_COM
*)xmalloc (sizeof (CASE_COM
));
325 temp
->clauses
= REVERSE_LIST (clauses
, PATTERN_LIST
*);
326 return (make_command (cm_case
, (SIMPLE_COM
*)temp
));
330 make_pattern_list (patterns
, action
)
336 temp
= (PATTERN_LIST
*)xmalloc (sizeof (PATTERN_LIST
));
337 temp
->patterns
= REVERSE_LIST (patterns
, WORD_LIST
*);
338 temp
->action
= action
;
344 make_if_command (test
, true_case
, false_case
)
345 COMMAND
*test
, *true_case
, *false_case
;
349 temp
= (IF_COM
*)xmalloc (sizeof (IF_COM
));
352 temp
->true_case
= true_case
;
353 temp
->false_case
= false_case
;
354 return (make_command (cm_if
, (SIMPLE_COM
*)temp
));
358 make_until_or_while (which
, test
, action
)
359 enum command_type which
;
360 COMMAND
*test
, *action
;
364 temp
= (WHILE_COM
*)xmalloc (sizeof (WHILE_COM
));
367 temp
->action
= action
;
368 return (make_command (which
, (SIMPLE_COM
*)temp
));
372 make_while_command (test
, action
)
373 COMMAND
*test
, *action
;
375 return (make_until_or_while (cm_while
, test
, action
));
379 make_until_command (test
, action
)
380 COMMAND
*test
, *action
;
382 return (make_until_or_while (cm_until
, test
, action
));
386 make_arith_command (exp
)
389 #if defined (DPAREN_ARITHMETIC)
393 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
394 command
->value
.Arith
= temp
= (ARITH_COM
*)xmalloc (sizeof (ARITH_COM
));
397 temp
->line
= line_number
;
400 command
->type
= cm_arith
;
401 command
->redirects
= (REDIRECT
*)NULL
;
406 last_command_exit_value
= 2;
407 return ((COMMAND
*)NULL
);
411 #if defined (COND_COMMAND)
413 make_cond_node (type
, op
, left
, right
)
416 struct cond_com
*left
, *right
;
420 temp
= (COND_COM
*)xmalloc (sizeof (COND_COM
));
422 temp
->line
= line_number
;
433 make_cond_command (cond_node
)
436 #if defined (COND_COMMAND)
439 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
440 command
->value
.Cond
= cond_node
;
442 command
->type
= cm_cond
;
443 command
->redirects
= (REDIRECT
*)NULL
;
445 command
->line
= cond_node
? cond_node
->line
: 0;
449 last_command_exit_value
= 2;
450 return ((COMMAND
*)NULL
);
455 make_bare_simple_command ()
460 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
461 command
->value
.Simple
= temp
= (SIMPLE_COM
*)xmalloc (sizeof (SIMPLE_COM
));
464 temp
->line
= line_number
;
465 temp
->words
= (WORD_LIST
*)NULL
;
466 temp
->redirects
= (REDIRECT
*)NULL
;
468 command
->type
= cm_simple
;
469 command
->redirects
= (REDIRECT
*)NULL
;
475 /* Return a command which is the connection of the word or redirection
476 in ELEMENT, and the command * or NULL in COMMAND. */
478 make_simple_command (element
, command
)
482 /* If we are starting from scratch, then make the initial command
483 structure. Also note that we have to fill in all the slots, since
484 malloc doesn't return zeroed space. */
486 command
= make_bare_simple_command ();
490 WORD_LIST
*tw
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
491 tw
->word
= element
.word
;
492 tw
->next
= command
->value
.Simple
->words
;
493 command
->value
.Simple
->words
= tw
;
497 REDIRECT
*r
= element
.redirect
;
498 /* Due to the way <> is implemented, there may be more than a single
499 redirection in element.redirect. We just follow the chain as far
500 as it goes, and hook onto the end. */
503 r
->next
= command
->value
.Simple
->redirects
;
504 command
->value
.Simple
->redirects
= element
.redirect
;
509 /* Because we are Bourne compatible, we read the input for this
510 << or <<- redirection now, from wherever input is coming from.
511 We store the input read into a WORD_DESC. Replace the text of
512 the redirectee.word with the new input text. If <<- is on,
513 then remove leading TABS from each line. */
515 make_here_document (temp
)
518 int kill_leading
, redir_len
;
519 char *redir_word
, *document
, *full_line
;
520 int document_index
, document_size
, delim_unquoted
;
522 if (temp
->instruction
!= r_deblank_reading_until
&&
523 temp
->instruction
!= r_reading_until
)
525 internal_error ("make_here_document: bad instruction type %d", temp
->instruction
);
529 kill_leading
= temp
->instruction
== r_deblank_reading_until
;
531 document
= (char *)NULL
;
532 document_index
= document_size
= 0;
534 /* Quote removal is the only expansion performed on the delimiter
535 for here documents, making it an extremely special case. */
536 redir_word
= string_quote_removal (temp
->redirectee
.filename
->word
, 0);
538 /* redirection_expand will return NULL if the expansion results in
539 multiple words or no words. Check for that here, and just abort
540 this here document if it does. */
542 redir_len
= strlen (redir_word
);
545 temp
->here_doc_eof
= xmalloc (1);
546 temp
->here_doc_eof
[0] = '\0';
550 free (temp
->redirectee
.filename
->word
);
551 temp
->here_doc_eof
= redir_word
;
553 /* Read lines from wherever lines are coming from.
554 For each line read, if kill_leading, then kill the
555 leading tab characters.
556 If the line matches redir_word exactly, then we have
557 manufactured the document. Otherwise, add the line to the
558 list of lines in the document. */
560 /* If the here-document delimiter was quoted, the lines should
561 be read verbatim from the input. If it was not quoted, we
562 need to perform backslash-quoted newline removal. */
563 delim_unquoted
= (temp
->redirectee
.filename
->flags
& W_QUOTED
) == 0;
564 while (full_line
= read_secondary_line (delim_unquoted
))
572 if (kill_leading
&& *line
)
574 /* Hack: To be compatible with some Bourne shells, we
575 check the word before stripping the whitespace. This
576 is a hack, though. */
577 if (STREQN (line
, redir_word
, redir_len
) && line
[redir_len
] == '\n')
580 while (*line
== '\t')
587 if (STREQN (line
, redir_word
, redir_len
) && line
[redir_len
] == '\n')
591 if (len
+ document_index
>= document_size
)
593 document_size
= document_size
? 2 * (document_size
+ len
) : len
+ 2;
594 document
= xrealloc (document
, document_size
);
597 /* len is guaranteed to be > 0 because of the check for line
598 being an empty string before the call to strlen. */
599 FASTCOPY (line
, document
+ document_index
, len
);
600 document_index
+= len
;
605 document
[document_index
] = '\0';
608 document
= xmalloc (1);
611 temp
->redirectee
.filename
->word
= document
;
614 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
615 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
616 and DEST is a file descriptor or a WORD_DESC *. */
618 make_redirection (source
, instruction
, dest_and_filename
)
620 enum r_instruction instruction
;
621 REDIRECTEE dest_and_filename
;
623 REDIRECT
*temp
= (REDIRECT
*)xmalloc (sizeof (REDIRECT
));
625 /* First do the common cases. */
626 temp
->redirector
= source
;
627 temp
->redirectee
= dest_and_filename
;
628 temp
->instruction
= instruction
;
630 temp
->next
= (REDIRECT
*)NULL
;
635 case r_output_direction
: /* >foo */
636 case r_output_force
: /* >| foo */
637 case r_err_and_out
: /* command &>filename */
638 temp
->flags
= O_TRUNC
| O_WRONLY
| O_CREAT
;
641 case r_appending_to
: /* >>foo */
642 temp
->flags
= O_APPEND
| O_WRONLY
| O_CREAT
;
645 case r_input_direction
: /* <foo */
646 case r_inputa_direction
: /* foo & makes this. */
647 temp
->flags
= O_RDONLY
;
650 case r_input_output
: /* <>foo */
651 temp
->flags
= O_RDWR
| O_CREAT
;
654 case r_deblank_reading_until
: /* <<-foo */
655 case r_reading_until
: /* << foo */
656 case r_close_this
: /* <&- */
657 case r_duplicating_input
: /* 1<&2 */
658 case r_duplicating_output
: /* 1>&2 */
659 case r_duplicating_input_word
: /* 1<&$foo */
660 case r_duplicating_output_word
: /* 1>&$foo */
664 programming_error ("make_redirection: redirection instruction `%d' out of range", instruction
);
672 make_function_def (name
, command
, lineno
, lstart
)
679 temp
= (FUNCTION_DEF
*)xmalloc (sizeof (FUNCTION_DEF
));
680 temp
->command
= command
;
684 command
->line
= lstart
;
685 return (make_command (cm_function_def
, (SIMPLE_COM
*)temp
));
689 make_subshell_command (command
)
694 temp
= (SUBSHELL_COM
*)xmalloc (sizeof (SUBSHELL_COM
));
695 temp
->command
= command
;
696 temp
->flags
= CMD_WANT_SUBSHELL
;
697 return (make_command (cm_subshell
, (SIMPLE_COM
*)temp
));
700 /* Reverse the word list and redirection list in the simple command
701 has just been parsed. It seems simpler to do this here the one
702 time then by any other method that I can think of. */
704 clean_simple_command (command
)
707 if (command
->type
!= cm_simple
)
708 command_error ("clean_simple_command", CMDERR_BADTYPE
, command
->type
, 0);
711 command
->value
.Simple
->words
=
712 REVERSE_LIST (command
->value
.Simple
->words
, WORD_LIST
*);
713 command
->value
.Simple
->redirects
=
714 REVERSE_LIST (command
->value
.Simple
->redirects
, REDIRECT
*);
720 /* The Yacc grammar productions have a problem, in that they take a
721 list followed by an ampersand (`&') and do a simple command connection,
722 making the entire list effectively asynchronous, instead of just
723 the last command. This means that when the list is executed, all
724 the commands have stdin set to /dev/null when job control is not
725 active, instead of just the last. This is wrong, and needs fixing
726 up. This function takes the `&' and applies it to the last command
727 in the list. This is done only for lists connected by `;'; it makes
728 `;' bind `tighter' than `&'. */
730 connect_async_list (command
, command2
, connector
)
731 COMMAND
*command
, *command2
;
734 COMMAND
*t
, *t1
, *t2
;
737 t
= command
->value
.Connection
->second
;
739 if (!t
|| (command
->flags
& CMD_WANT_SUBSHELL
) ||
740 command
->value
.Connection
->connector
!= ';')
742 t
= command_connect (command
, command2
, connector
);
746 /* This is just defensive programming. The Yacc precedence rules
747 will generally hand this function a command where t points directly
748 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
749 to the `a ; b ; c' list and t will be the `d'). We only want to do
750 this if the list is not being executed as a unit in the background
751 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
752 the only way to tell. */
753 while (((t
->flags
& CMD_WANT_SUBSHELL
) == 0) && t
->type
== cm_connection
&&
754 t
->value
.Connection
->connector
== ';')
757 t
= t
->value
.Connection
->second
;
759 /* Now we have t pointing to the last command in the list, and
760 t1->value.Connection->second == t. */
761 t2
= command_connect (t
, command2
, connector
);
762 t1
->value
.Connection
->second
= t2
;