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 1, 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, 675 Mass Ave, Cambridge, MA 02139, 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
;
53 make_bare_word (string
)
58 temp
= (WORD_DESC
*)xmalloc (sizeof (WORD_DESC
));
60 temp
->word
= savestring (string
);
63 temp
->word
= xmalloc (1);
72 make_word_flags (w
, string
)
78 for (s
= string
; *s
; s
++)
82 w
->flags
|= W_HASDOLLAR
;
85 break; /* continue the loop */
101 temp
= make_bare_word (string
);
102 return (make_word_flags (temp
, string
));
106 make_word_from_token (token
)
111 tokenizer
[0] = token
;
114 return (make_word (tokenizer
));
118 make_word_list (word
, link
)
124 temp
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
131 add_string_to_list (string
, list
)
137 temp
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
138 temp
->word
= make_word (string
);
144 make_command (type
, pointer
)
145 enum command_type type
;
150 temp
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
152 temp
->value
.Simple
= pointer
;
153 temp
->value
.Simple
->flags
= temp
->flags
= 0;
154 temp
->redirects
= (REDIRECT
*)NULL
;
159 command_connect (com1
, com2
, connector
)
160 COMMAND
*com1
, *com2
;
165 temp
= (CONNECTION
*)xmalloc (sizeof (CONNECTION
));
166 temp
->connector
= connector
;
169 return (make_command (cm_connection
, (SIMPLE_COM
*)temp
));
173 make_for_or_select (type
, name
, map_list
, action
)
174 enum command_type type
;
181 temp
= (FOR_COM
*)xmalloc (sizeof (FOR_COM
));
184 temp
->map_list
= map_list
;
185 temp
->action
= action
;
186 return (make_command (type
, (SIMPLE_COM
*)temp
));
190 make_for_command (name
, map_list
, action
)
195 return (make_for_or_select (cm_for
, name
, map_list
, action
));
199 make_select_command (name
, map_list
, action
)
204 #if defined (SELECT_COMMAND)
205 return (make_for_or_select (cm_select
, name
, map_list
, action
));
210 make_group_command (command
)
215 temp
= (GROUP_COM
*)xmalloc (sizeof (GROUP_COM
));
216 temp
->command
= command
;
217 return (make_command (cm_group
, (SIMPLE_COM
*)temp
));
221 make_case_command (word
, clauses
)
223 PATTERN_LIST
*clauses
;
227 temp
= (CASE_COM
*)xmalloc (sizeof (CASE_COM
));
230 temp
->clauses
= REVERSE_LIST (clauses
, PATTERN_LIST
*);
231 return (make_command (cm_case
, (SIMPLE_COM
*)temp
));
235 make_pattern_list (patterns
, action
)
241 temp
= (PATTERN_LIST
*)xmalloc (sizeof (PATTERN_LIST
));
242 temp
->patterns
= REVERSE_LIST (patterns
, WORD_LIST
*);
243 temp
->action
= action
;
249 make_if_command (test
, true_case
, false_case
)
250 COMMAND
*test
, *true_case
, *false_case
;
254 temp
= (IF_COM
*)xmalloc (sizeof (IF_COM
));
257 temp
->true_case
= true_case
;
258 temp
->false_case
= false_case
;
259 return (make_command (cm_if
, (SIMPLE_COM
*)temp
));
263 make_until_or_while (which
, test
, action
)
264 enum command_type which
;
265 COMMAND
*test
, *action
;
269 temp
= (WHILE_COM
*)xmalloc (sizeof (WHILE_COM
));
272 temp
->action
= action
;
273 return (make_command (which
, (SIMPLE_COM
*)temp
));
277 make_while_command (test
, action
)
278 COMMAND
*test
, *action
;
280 return (make_until_or_while (cm_while
, test
, action
));
284 make_until_command (test
, action
)
285 COMMAND
*test
, *action
;
287 return (make_until_or_while (cm_until
, test
, action
));
291 make_arith_command (exp
)
294 #if defined (DPAREN_ARITHMETIC)
298 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
299 command
->value
.Arith
= temp
= (ARITH_COM
*)xmalloc (sizeof (ARITH_COM
));
302 temp
->line
= line_number
;
305 command
->type
= cm_arith
;
306 command
->redirects
= (REDIRECT
*)NULL
;
311 return ((COMMAND
*)NULL
);
315 #if defined (COND_COMMAND)
317 make_cond_node (type
, op
, left
, right
)
320 struct cond_com
*left
, *right
;
324 temp
= (COND_COM
*)xmalloc (sizeof (COND_COM
));
326 temp
->line
= line_number
;
337 make_cond_command (cond_node
)
340 #if defined (COND_COMMAND)
343 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
344 command
->value
.Cond
= cond_node
;
346 command
->type
= cm_cond
;
347 command
->redirects
= (REDIRECT
*)NULL
;
352 return ((COMMAND
*)NULL
);
357 make_bare_simple_command ()
362 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
363 command
->value
.Simple
= temp
= (SIMPLE_COM
*)xmalloc (sizeof (SIMPLE_COM
));
366 temp
->line
= line_number
;
367 temp
->words
= (WORD_LIST
*)NULL
;
368 temp
->redirects
= (REDIRECT
*)NULL
;
370 command
->type
= cm_simple
;
371 command
->redirects
= (REDIRECT
*)NULL
;
377 /* Return a command which is the connection of the word or redirection
378 in ELEMENT, and the command * or NULL in COMMAND. */
380 make_simple_command (element
, command
)
384 /* If we are starting from scratch, then make the initial command
385 structure. Also note that we have to fill in all the slots, since
386 malloc doesn't return zeroed space. */
388 command
= make_bare_simple_command ();
392 WORD_LIST
*tw
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
393 tw
->word
= element
.word
;
394 tw
->next
= command
->value
.Simple
->words
;
395 command
->value
.Simple
->words
= tw
;
399 REDIRECT
*r
= element
.redirect
;
400 /* Due to the way <> is implemented, there may be more than a single
401 redirection in element.redirect. We just follow the chain as far
402 as it goes, and hook onto the end. */
405 r
->next
= command
->value
.Simple
->redirects
;
406 command
->value
.Simple
->redirects
= element
.redirect
;
411 /* Because we are Bourne compatible, we read the input for this
412 << or <<- redirection now, from wherever input is coming from.
413 We store the input read into a WORD_DESC. Replace the text of
414 the redirectee.word with the new input text. If <<- is on,
415 then remove leading TABS from each line. */
417 make_here_document (temp
)
420 int kill_leading
, redir_len
;
421 char *redir_word
, *document
, *full_line
;
422 int document_index
, document_size
, delim_unquoted
;
424 if (temp
->instruction
!= r_deblank_reading_until
&&
425 temp
->instruction
!= r_reading_until
)
427 internal_error ("make_here_document: bad instruction type %d", temp
->instruction
);
431 kill_leading
= temp
->instruction
== r_deblank_reading_until
;
433 document
= (char *)NULL
;
434 document_index
= document_size
= 0;
436 /* Quote removal is the only expansion performed on the delimiter
437 for here documents, making it an extremely special case. */
438 redir_word
= string_quote_removal (temp
->redirectee
.filename
->word
, 0);
440 /* redirection_expand will return NULL if the expansion results in
441 multiple words or no words. Check for that here, and just abort
442 this here document if it does. */
444 redir_len
= strlen (redir_word
);
447 temp
->here_doc_eof
= xmalloc (1);
448 temp
->here_doc_eof
[0] = '\0';
452 free (temp
->redirectee
.filename
->word
);
453 temp
->here_doc_eof
= redir_word
;
455 /* Read lines from wherever lines are coming from.
456 For each line read, if kill_leading, then kill the
457 leading tab characters.
458 If the line matches redir_word exactly, then we have
459 manufactured the document. Otherwise, add the line to the
460 list of lines in the document. */
462 /* If the here-document delimiter was quoted, the lines should
463 be read verbatim from the input. If it was not quoted, we
464 need to perform backslash-quoted newline removal. */
465 delim_unquoted
= (temp
->redirectee
.filename
->flags
& W_QUOTED
) == 0;
466 while (full_line
= read_secondary_line (delim_unquoted
))
474 if (kill_leading
&& *line
)
476 /* Hack: To be compatible with some Bourne shells, we
477 check the word before stripping the whitespace. This
478 is a hack, though. */
479 if (STREQN (line
, redir_word
, redir_len
) && line
[redir_len
] == '\n')
482 while (*line
== '\t')
489 if (STREQN (line
, redir_word
, redir_len
) && line
[redir_len
] == '\n')
493 if (len
+ document_index
>= document_size
)
495 document_size
= document_size
? 2 * (document_size
+ len
) : 1000;
496 document
= xrealloc (document
, document_size
);
499 /* len is guaranteed to be > 0 because of the check for line
500 being an empty string before the call to strlen. */
501 FASTCOPY (line
, document
+ document_index
, len
);
502 document_index
+= len
;
507 document
[document_index
] = '\0';
510 document
= xmalloc (1);
513 temp
->redirectee
.filename
->word
= document
;
516 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
517 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
518 and DEST is a file descriptor or a WORD_DESC *. */
520 make_redirection (source
, instruction
, dest_and_filename
)
522 enum r_instruction instruction
;
523 REDIRECTEE dest_and_filename
;
525 REDIRECT
*temp
= (REDIRECT
*)xmalloc (sizeof (REDIRECT
));
527 /* First do the common cases. */
528 temp
->redirector
= source
;
529 temp
->redirectee
= dest_and_filename
;
530 temp
->instruction
= instruction
;
532 temp
->next
= (REDIRECT
*)NULL
;
537 case r_output_direction
: /* >foo */
538 case r_output_force
: /* >| foo */
539 case r_err_and_out
: /* command &>filename */
540 temp
->flags
= O_TRUNC
| O_WRONLY
| O_CREAT
;
543 case r_appending_to
: /* >>foo */
544 temp
->flags
= O_APPEND
| O_WRONLY
| O_CREAT
;
547 case r_input_direction
: /* <foo */
548 case r_inputa_direction
: /* foo & makes this. */
549 temp
->flags
= O_RDONLY
;
552 case r_input_output
: /* <>foo */
553 temp
->flags
= O_RDWR
| O_CREAT
;
556 case r_deblank_reading_until
: /* <<-foo */
557 case r_reading_until
: /* << foo */
558 case r_close_this
: /* <&- */
559 case r_duplicating_input
: /* 1<&2 */
560 case r_duplicating_output
: /* 1>&2 */
561 case r_duplicating_input_word
: /* 1<&$foo */
562 case r_duplicating_output_word
: /* 1>&$foo */
566 programming_error ("make_redirection: redirection instruction `%d' out of range", instruction
);
574 make_function_def (name
, command
, lineno
, lstart
)
581 temp
= (FUNCTION_DEF
*)xmalloc (sizeof (FUNCTION_DEF
));
582 temp
->command
= command
;
586 command
->line
= lstart
;
587 return (make_command (cm_function_def
, (SIMPLE_COM
*)temp
));
590 /* Reverse the word list and redirection list in the simple command
591 has just been parsed. It seems simpler to do this here the one
592 time then by any other method that I can think of. */
594 clean_simple_command (command
)
597 if (command
->type
!= cm_simple
)
598 programming_error ("clean_simple_command: bad command type `%d'", command
->type
);
601 command
->value
.Simple
->words
=
602 REVERSE_LIST (command
->value
.Simple
->words
, WORD_LIST
*);
603 command
->value
.Simple
->redirects
=
604 REVERSE_LIST (command
->value
.Simple
->redirects
, REDIRECT
*);
610 /* The Yacc grammar productions have a problem, in that they take a
611 list followed by an ampersand (`&') and do a simple command connection,
612 making the entire list effectively asynchronous, instead of just
613 the last command. This means that when the list is executed, all
614 the commands have stdin set to /dev/null when job control is not
615 active, instead of just the last. This is wrong, and needs fixing
616 up. This function takes the `&' and applies it to the last command
617 in the list. This is done only for lists connected by `;'; it makes
618 `;' bind `tighter' than `&'. */
620 connect_async_list (command
, command2
, connector
)
621 COMMAND
*command
, *command2
;
624 COMMAND
*t
, *t1
, *t2
;
627 t
= command
->value
.Connection
->second
;
629 if (!t
|| (command
->flags
& CMD_WANT_SUBSHELL
) ||
630 command
->value
.Connection
->connector
!= ';')
632 t
= command_connect (command
, command2
, connector
);
636 /* This is just defensive programming. The Yacc precedence rules
637 will generally hand this function a command where t points directly
638 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
639 to the `a ; b ; c' list and t will be the `d'). We only want to do
640 this if the list is not being executed as a unit in the background
641 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
642 the only way to tell. */
643 while (((t
->flags
& CMD_WANT_SUBSHELL
) == 0) && t
->type
== cm_connection
&&
644 t
->value
.Connection
->connector
== ';')
647 t
= t
->value
.Connection
->second
;
649 /* Now we have t pointing to the last command in the list, and
650 t1->value.Connection->second == t. */
651 t2
= command_connect (t
, command2
, connector
);
652 t1
->value
.Connection
->second
= t2
;