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"
29 #if defined (HAVE_UNISTD_H)
38 #include "variables.h"
43 #if defined (JOB_CONTROL)
47 extern int line_number, current_command_line_count;
48 extern int disallow_filename_globbing;
51 make_bare_word (string)
56 temp = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
58 temp->word = savestring (string);
61 temp->word = xmalloc (1);
70 make_word_flags (w, string)
76 for (s = string; *s; s++)
80 w->flags |= W_HASDOLLAR;
83 break; /* continue the loop */
99 temp = make_bare_word (string);
100 return (make_word_flags (temp, string));
104 make_word_from_token (token)
109 tokenizer[0] = token;
112 return (make_word (tokenizer));
116 make_word_list (word, link)
122 temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
129 add_string_to_list (string, list)
135 temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
136 temp->word = make_word (string);
142 make_command (type, pointer)
143 enum command_type type;
148 temp = (COMMAND *)xmalloc (sizeof (COMMAND));
150 temp->value.Simple = pointer;
151 temp->value.Simple->flags = temp->flags = 0;
152 temp->redirects = (REDIRECT *)NULL;
157 command_connect (com1, com2, connector)
158 COMMAND *com1, *com2;
163 temp = (CONNECTION *)xmalloc (sizeof (CONNECTION));
164 temp->connector = connector;
167 return (make_command (cm_connection, (SIMPLE_COM *)temp));
171 make_for_or_select (type, name, map_list, action)
172 enum command_type type;
179 temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
182 temp->map_list = map_list;
183 temp->action = action;
184 return (make_command (type, (SIMPLE_COM *)temp));
188 make_for_command (name, map_list, action)
193 return (make_for_or_select (cm_for, name, map_list, action));
197 make_select_command (name, map_list, action)
202 #if defined (SELECT_COMMAND)
203 return (make_for_or_select (cm_select, name, map_list, action));
208 make_group_command (command)
213 temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
214 temp->command = command;
215 return (make_command (cm_group, (SIMPLE_COM *)temp));
219 make_case_command (word, clauses)
221 PATTERN_LIST *clauses;
225 temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
228 temp->clauses = REVERSE_LIST (clauses, PATTERN_LIST *);
229 return (make_command (cm_case, (SIMPLE_COM *)temp));
233 make_pattern_list (patterns, action)
239 temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
240 temp->patterns = REVERSE_LIST (patterns, WORD_LIST *);
241 temp->action = action;
247 make_if_command (test, true_case, false_case)
248 COMMAND *test, *true_case, *false_case;
252 temp = (IF_COM *)xmalloc (sizeof (IF_COM));
255 temp->true_case = true_case;
256 temp->false_case = false_case;
257 return (make_command (cm_if, (SIMPLE_COM *)temp));
261 make_until_or_while (which, test, action)
262 enum command_type which;
263 COMMAND *test, *action;
267 temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
270 temp->action = action;
271 return (make_command (which, (SIMPLE_COM *)temp));
275 make_while_command (test, action)
276 COMMAND *test, *action;
278 return (make_until_or_while (cm_while, test, action));
282 make_until_command (test, action)
283 COMMAND *test, *action;
285 return (make_until_or_while (cm_until, test, action));
289 make_bare_simple_command ()
294 command = (COMMAND *)xmalloc (sizeof (COMMAND));
295 command->value.Simple = temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
298 temp->line = line_number;
299 temp->words = (WORD_LIST *)NULL;
300 temp->redirects = (REDIRECT *)NULL;
302 command->type = cm_simple;
303 command->redirects = (REDIRECT *)NULL;
309 /* Return a command which is the connection of the word or redirection
310 in ELEMENT, and the command * or NULL in COMMAND. */
312 make_simple_command (element, command)
316 /* If we are starting from scratch, then make the initial command
317 structure. Also note that we have to fill in all the slots, since
318 malloc doesn't return zeroed space. */
320 command = make_bare_simple_command ();
324 WORD_LIST *tw = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
325 tw->word = element.word;
326 tw->next = command->value.Simple->words;
327 command->value.Simple->words = tw;
331 REDIRECT *r = element.redirect;
332 /* Due to the way <> is implemented, there may be more than a single
333 redirection in element.redirect. We just follow the chain as far
334 as it goes, and hook onto the end. */
337 r->next = command->value.Simple->redirects;
338 command->value.Simple->redirects = element.redirect;
343 /* Because we are Bourne compatible, we read the input for this
344 << or <<- redirection now, from wherever input is coming from.
345 We store the input read into a WORD_DESC. Replace the text of
346 the redirectee.word with the new input text. If <<- is on,
347 then remove leading TABS from each line. */
349 make_here_document (temp)
352 int kill_leading, redir_len;
353 char *redir_word, *document, *full_line;
354 int document_index, document_size, delim_unquoted;
356 if (temp->instruction != r_deblank_reading_until &&
357 temp->instruction != r_reading_until)
359 internal_error ("make_here_document: bad instruction type %d", temp->instruction);
363 kill_leading = temp->instruction == r_deblank_reading_until;
365 document = (char *)NULL;
366 document_index = document_size = 0;
368 /* Quote removal is the only expansion performed on the delimiter
369 for here documents, making it an extremely special case. */
370 redir_word = string_quote_removal (temp->redirectee.filename->word, 0);
372 /* redirection_expand will return NULL if the expansion results in
373 multiple words or no words. Check for that here, and just abort
374 this here document if it does. */
376 redir_len = strlen (redir_word);
379 temp->here_doc_eof = xmalloc (1);
380 temp->here_doc_eof[0] = '\0';
384 free (temp->redirectee.filename->word);
385 temp->here_doc_eof = redir_word;
387 /* Read lines from wherever lines are coming from.
388 For each line read, if kill_leading, then kill the
389 leading tab characters.
390 If the line matches redir_word exactly, then we have
391 manufactured the document. Otherwise, add the line to the
392 list of lines in the document. */
394 /* If the here-document delimiter was quoted, the lines should
395 be read verbatim from the input. If it was not quoted, we
396 need to perform backslash-quoted newline removal. */
397 delim_unquoted = (temp->redirectee.filename->flags & W_QUOTED) == 0;
398 while (full_line = read_secondary_line (delim_unquoted))
406 if (kill_leading && *line)
408 /* Hack: To be compatible with some Bourne shells, we
409 check the word before stripping the whitespace. This
410 is a hack, though. */
411 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
414 while (*line == '\t')
421 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
425 if (len + document_index >= document_size)
427 document_size = document_size ? 2 * (document_size + len) : 1000;
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';
442 document = xmalloc (1);
445 temp->redirectee.filename->word = document;
448 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
449 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
450 and DEST is a file descriptor or a WORD_DESC *. */
452 make_redirection (source, instruction, dest_and_filename)
454 enum r_instruction instruction;
455 REDIRECTEE dest_and_filename;
457 REDIRECT *temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
459 /* First do the common cases. */
460 temp->redirector = source;
461 temp->redirectee = dest_and_filename;
462 temp->instruction = instruction;
464 temp->next = (REDIRECT *)NULL;
469 case r_output_direction: /* >foo */
470 case r_output_force: /* >| foo */
471 temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
474 case r_input_direction: /* <foo */
475 case r_inputa_direction: /* foo & makes this. */
476 temp->flags = O_RDONLY;
479 case r_appending_to: /* >>foo */
480 temp->flags = O_APPEND | O_WRONLY | O_CREAT;
483 case r_deblank_reading_until: /* <<-foo */
484 case r_reading_until: /* << foo */
487 case r_close_this: /* <&- */
488 case r_duplicating_input: /* 1<&2 */
489 case r_duplicating_output: /* 1>&2 */
490 case r_duplicating_input_word: /* 1<&$foo */
491 case r_duplicating_output_word: /* 1>&$foo */
494 case r_err_and_out: /* command &>filename */
495 temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
499 temp->flags = O_RDWR | O_CREAT;
503 programming_error ("make_redirection: redirection instruction `%d' out of range", instruction);
511 make_function_def (name, command, lineno, lstart)
518 temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
519 temp->command = command;
523 command->line = lstart;
524 return (make_command (cm_function_def, (SIMPLE_COM *)temp));
527 /* Reverse the word list and redirection list in the simple command
528 has just been parsed. It seems simpler to do this here the one
529 time then by any other method that I can think of. */
531 clean_simple_command (command)
534 if (command->type != cm_simple)
535 programming_error ("clean_simple_command: bad command 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 /* The Yacc grammar productions have a problem, in that they take a
548 list followed by an ampersand (`&') and do a simple command connection,
549 making the entire list effectively asynchronous, instead of just
550 the last command. This means that when the list is executed, all
551 the commands have stdin set to /dev/null when job control is not
552 active, instead of just the last. This is wrong, and needs fixing
553 up. This function takes the `&' and applies it to the last command
554 in the list. This is done only for lists connected by `;'; it makes
555 `;' bind `tighter' than `&'. */
557 connect_async_list (command, command2, connector)
558 COMMAND *command, *command2;
561 COMMAND *t, *t1, *t2;
564 t = command->value.Connection->second;
566 if (!t || (command->flags & CMD_WANT_SUBSHELL) ||
567 command->value.Connection->connector != ';')
569 t = command_connect (command, command2, connector);
573 /* This is just defensive programming. The Yacc precedence rules
574 will generally hand this function a command where t points directly
575 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
576 to the `a ; b ; c' list and t will be the `d'). We only want to do
577 this if the list is not being executed as a unit in the background
578 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
579 the only way to tell. */
580 while (((t->flags & CMD_WANT_SUBSHELL) == 0) && t->type == cm_connection &&
581 t->value.Connection->connector == ';')
584 t = t->value.Connection->second;
586 /* Now we have t pointing to the last command in the list, and
587 t1->value.Connection->second == t. */
588 t2 = command_connect (t, command2, connector);
589 t1->value.Connection->second = t2;