2 Functions for making instances of the various parser constructs. */
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. */
23 #include "bashtypes.h"
37 #if defined (JOB_CONTROL)
41 extern int line_number
, current_command_line_count
;
42 extern int disallow_filename_globbing
;
50 temp
= (WORD_DESC
*)xmalloc (sizeof (WORD_DESC
));
51 temp
->word
= savestring (string
);
52 temp
->quoted
= temp
->dollar_present
= temp
->assignment
= 0;
56 if (*string
== '$') temp
->dollar_present
= 1;
59 if (member (*string
, "'`\\\""))
86 make_word_from_token (token
)
94 return (make_word (tokenizer
));
98 make_word_list (word
, link
)
104 temp
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
111 add_string_to_list (string
, list
)
115 WORD_LIST
*temp
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
116 temp
->word
= make_word (string
);
123 coerce_to_word (number
)
128 sprintf (string
, "%d", number
);
129 return (make_word (string
));
134 make_command (type
, pointer
)
135 enum command_type type
;
140 temp
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
142 temp
->value
.Simple
= pointer
;
143 temp
->value
.Simple
->flags
= 0;
145 temp
->redirects
= (REDIRECT
*)NULL
;
150 command_connect (com1
, com2
, connector
)
151 COMMAND
*com1
, *com2
;
156 temp
= (CONNECTION
*)xmalloc (sizeof (CONNECTION
));
157 temp
->connector
= connector
;
160 return (make_command (cm_connection
, (SIMPLE_COM
*)temp
));
164 make_for_command (name
, map_list
, action
)
169 FOR_COM
*temp
= (FOR_COM
*)xmalloc (sizeof (FOR_COM
));
173 temp
->map_list
= map_list
;
174 temp
->action
= action
;
175 return (make_command (cm_for
, (SIMPLE_COM
*)temp
));
178 #if defined (SELECT_COMMAND)
180 make_select_command (name
, map_list
, action
)
185 SELECT_COM
*temp
= (SELECT_COM
*)xmalloc (sizeof (SELECT_COM
));
189 temp
->map_list
= map_list
;
190 temp
->action
= action
;
191 return (make_command (cm_select
, (SIMPLE_COM
*)temp
));
196 make_group_command (command
)
199 GROUP_COM
*temp
= (GROUP_COM
*)xmalloc (sizeof (GROUP_COM
));
201 temp
->command
= command
;
202 return (make_command (cm_group
, (SIMPLE_COM
*)temp
));
206 make_case_command (word
, clauses
)
208 PATTERN_LIST
*clauses
;
212 temp
= (CASE_COM
*)xmalloc (sizeof (CASE_COM
));
215 temp
->clauses
= REVERSE_LIST (clauses
, PATTERN_LIST
*);
216 return (make_command (cm_case
, (SIMPLE_COM
*)temp
));
220 make_pattern_list (patterns
, action
)
226 temp
= (PATTERN_LIST
*)xmalloc (sizeof (PATTERN_LIST
));
227 temp
->patterns
= REVERSE_LIST (patterns
, WORD_LIST
*);
228 temp
->action
= action
;
234 make_if_command (test
, true_case
, false_case
)
235 COMMAND
*test
, *true_case
, *false_case
;
239 temp
= (IF_COM
*)xmalloc (sizeof (IF_COM
));
242 temp
->true_case
= true_case
;
243 temp
->false_case
= false_case
;
244 return (make_command (cm_if
, (SIMPLE_COM
*)temp
));
248 make_until_or_while (test
, action
, which
)
249 COMMAND
*test
, *action
;
250 enum command_type which
;
254 temp
= (WHILE_COM
*)xmalloc (sizeof (WHILE_COM
));
257 temp
->action
= action
;
258 return (make_command (which
, (SIMPLE_COM
*)temp
));
262 make_while_command (test
, action
)
263 COMMAND
*test
, *action
;
265 return (make_until_or_while (test
, action
, cm_while
));
269 make_until_command (test
, action
)
270 COMMAND
*test
, *action
;
272 return (make_until_or_while (test
, action
, cm_until
));
276 make_bare_simple_command ()
279 SIMPLE_COM
*temp
= (SIMPLE_COM
*)xmalloc (sizeof (SIMPLE_COM
));
282 temp
->line
= line_number
;
283 temp
->words
= (WORD_LIST
*)NULL
;
284 temp
->redirects
= (REDIRECT
*)NULL
;
285 command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
286 command
->type
= cm_simple
;
287 command
->redirects
= (REDIRECT
*)NULL
;
289 command
->value
.Simple
= temp
;
293 /* Return a command which is the connection of the word or redirection
294 in ELEMENT, and the command * or NULL in COMMAND. */
296 make_simple_command (element
, command
)
300 /* If we are starting from scratch, then make the initial command
301 structure. Also note that we have to fill in all the slots, since
302 malloc doesn't return zeroed space. */
304 command
= make_bare_simple_command ();
308 WORD_LIST
*tw
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
309 tw
->word
= element
.word
;
310 tw
->next
= command
->value
.Simple
->words
;
311 command
->value
.Simple
->words
= tw
;
315 REDIRECT
*r
= element
.redirect
;
316 /* Due to the way <> is implemented, there may be more than a single
317 redirection in element.redirect. We just follow the chain as far
318 as it goes, and hook onto the end. */
321 r
->next
= command
->value
.Simple
->redirects
;
322 command
->value
.Simple
->redirects
= element
.redirect
;
327 #define POSIX_HERE_DOCUMENTS
329 make_here_document (temp
)
332 int kill_leading
= 0;
334 switch (temp
->instruction
)
336 /* Because we are Bourne compatible, we read the input for this
337 << or <<- redirection now, from wherever input is coming from.
338 We store the input read into a WORD_DESC. Replace the text of
339 the redirectee.word with the new input text. If <<- is on,
340 then remove leading TABS from each line. */
342 case r_deblank_reading_until
: /* <<-foo */
345 case r_reading_until
: /* <<foo */
350 char *document
= (char *)NULL
;
351 int document_index
= 0, document_size
= 0;
353 #if !defined (POSIX_HERE_DOCUMENTS)
354 /* Because of Bourne shell semantics, we turn off globbing, but
355 only for this style of redirection. I feel a little ill. */
357 int old_value
= disallow_filename_globbing
;
358 disallow_filename_globbing
= 1;
360 redir_word
= redirection_expand (temp
->redirectee
.filename
);
362 disallow_filename_globbing
= old_value
;
364 #else /* POSIX_HERE_DOCUMENTS */
365 /* Quote removal is the only expansion performed on the delimiter
366 for here documents, making it an extremely special case. I
368 redir_word
= string_quote_removal (temp
->redirectee
.filename
->word
, 0);
369 #endif /* POSIX_HERE_DOCUMENTS */
371 /* redirection_expand will return NULL if the expansion results in
372 multiple words or no words. Check for that here, and just abort
373 this here document if it does. */
375 redir_len
= strlen (redir_word
);
378 temp
->here_doc_eof
= savestring ("");
382 free (temp
->redirectee
.filename
->word
);
383 temp
->here_doc_eof
= redir_word
;
385 /* Read lines from wherever lines are coming from.
386 For each line read, if kill_leading, then kill the
387 leading tab characters.
388 If the line matches redir_word exactly, then we have
389 manufactured the document. Otherwise, add the line to the
390 list of lines in the document. */
392 /* If the here-document delimiter was quoted, the lines should
393 be read verbatim from the input. If it was not quoted, we
394 need to perform backslash-quoted newline removal. */
395 while (full_line
= read_secondary_line
396 (temp
->redirectee
.filename
->quoted
== 0))
398 register char *line
= full_line
;
403 if (kill_leading
&& *line
)
405 /* Hack: To be compatible with some Bourne shells, we
406 check the word before stripping the whitespace. This
407 is a hack, though. */
408 if (STREQN (line
, redir_word
, redir_len
) &&
409 line
[redir_len
] == '\n')
412 while (*line
== '\t')
419 if (STREQN (line
, redir_word
, redir_len
) &&
420 line
[redir_len
] == '\n')
424 if (len
+ document_index
>= document_size
)
426 document_size
= document_size
? 2 * (document_size
+ len
)
428 document
= xrealloc (document
, document_size
);
431 /* len is guaranteed to be > 0 because of the check for line
432 being an empty string before the call to strlen. */
433 FASTCOPY (line
, document
+ document_index
, len
);
434 document_index
+= len
;
439 document
[document_index
] = '\0';
441 document
= savestring ("");
442 temp
->redirectee
.filename
->word
= document
;
447 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
448 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
449 and DEST is a file descriptor or a WORD_DESC *. */
451 make_redirection (source
, instruction
, dest_and_filename
)
453 enum r_instruction instruction
;
454 REDIRECTEE dest_and_filename
;
456 REDIRECT
*temp
= (REDIRECT
*)xmalloc (sizeof (REDIRECT
));
458 /* First do the common cases. */
459 temp
->redirector
= source
;
460 temp
->redirectee
= dest_and_filename
;
461 temp
->instruction
= instruction
;
463 temp
->next
= (REDIRECT
*)NULL
;
468 case r_output_direction
: /* >foo */
469 case r_output_force
: /* >| foo */
470 temp
->flags
= O_TRUNC
| O_WRONLY
| O_CREAT
;
473 case r_input_direction
: /* <foo */
474 case r_inputa_direction
: /* foo & makes this. */
475 temp
->flags
= O_RDONLY
;
478 case r_appending_to
: /* >>foo */
479 temp
->flags
= O_APPEND
| O_WRONLY
| O_CREAT
;
482 case r_deblank_reading_until
: /* <<-foo */
483 case r_reading_until
: /* << foo */
486 case r_duplicating_input
: /* 1<&2 */
487 case r_duplicating_output
: /* 1>&2 */
488 case r_close_this
: /* <&- */
489 case r_duplicating_input_word
: /* 1<&$foo */
490 case r_duplicating_output_word
: /* 1>&$foo */
493 case r_err_and_out
: /* command &>filename */
494 temp
->flags
= O_TRUNC
| O_WRONLY
| O_CREAT
;
498 temp
->flags
= O_RDWR
| O_CREAT
;
502 programming_error ("Redirection instruction from yyparse () '%d' is\n\
503 out of range in make_redirection ().", instruction
);
511 make_function_def (name
, command
)
517 temp
= (FUNCTION_DEF
*)xmalloc (sizeof (FUNCTION_DEF
));
518 temp
->command
= command
;
520 command
->line
= line_number
- current_command_line_count
+ 1;
521 return (make_command (cm_function_def
, (SIMPLE_COM
*)temp
));
524 /* Reverse the word list and redirection list in the simple command
525 has just been parsed. It seems simpler to do this here the one
526 time then by any other method that I can think of. */
528 clean_simple_command (command
)
531 if (command
->type
!= cm_simple
)
534 ("clean_simple_command () got a command with type %d.", command
->type
);
538 command
->value
.Simple
->words
=
539 REVERSE_LIST (command
->value
.Simple
->words
, WORD_LIST
*);
540 command
->value
.Simple
->redirects
=
541 REVERSE_LIST (command
->value
.Simple
->redirects
, REDIRECT
*);
547 /* Cons up a new array of words. The words are taken from LIST,
548 which is a WORD_LIST *. Absolutely everything is malloc'ed,
549 so you should free everything in this array when you are done.
550 The array is NULL terminated. */
552 make_word_array (list
)
555 int count
= list_length (list
);
556 char **array
= (char **)xmalloc ((1 + count
) * sizeof (char *));
558 for (count
= 0; list
; count
++)
560 array
[count
] = xmalloc (1 + strlen (list
->word
->word
));
561 strcpy (array
[count
], list
->word
->word
);
564 array
[count
] = (char *)NULL
;
568 /* The Yacc grammar productions have a problem, in that they take a
569 list followed by an ampersand (`&') and do a simple command connection,
570 making the entire list effectively asynchronous, instead of just
571 the last command. This means that when the list is executed, all
572 the commands have stdin set to /dev/null when job control is not
573 active, instead of just the last. This is wrong, and needs fixing
574 up. This function takes the `&' and applies it to the last command
575 in the list. This is done only for lists connected by `;'; it makes
576 `;' bind `tighter' than `&'. */
578 connect_async_list (command
, command2
, connector
)
579 COMMAND
*command
, *command2
;
582 COMMAND
*t
, *t1
, *t2
;
585 t
= command
->value
.Connection
->second
;
587 if (!t
|| (command
->flags
& CMD_WANT_SUBSHELL
) ||
588 command
->value
.Connection
->connector
!= ';')
590 t
= command_connect (command
, command2
, connector
);
594 /* This is just defensive programming. The Yacc precedence rules
595 will generally hand this function a command where t points directly
596 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
597 to the `a ; b ; c' list and t will be the `d'). We only want to do
598 this if the list is not being executed as a unit in the background
599 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
600 the only way to tell. */
601 while (((t
->flags
& CMD_WANT_SUBSHELL
) == 0) && t
->type
== cm_connection
&&
602 t
->value
.Connection
->connector
== ';')
605 t
= t
->value
.Connection
->second
;
607 /* Now we have t pointing to the last command in the list, and
608 t1->value.Connection->second == t. */
609 t2
= command_connect (t
, command2
, connector
);
610 t1
->value
.Connection
->second
= t2
;