Imported from ../bash-2.01.tar.gz.
[platform/upstream/bash.git] / make_cmd.c
1 /* make_cmd.c -- Functions for making instances of the various
2    parser constructs. */
3
4 /* Copyright (C) 1989 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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
11 version.
12
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
16 for more details.
17
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. */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include "bashtypes.h"
26 #include <sys/file.h>
27 #include "filecntl.h"
28 #include "bashansi.h"
29 #if defined (HAVE_UNISTD_H)
30 #  include <unistd.h>
31 #endif
32
33 #include "command.h"
34 #include "general.h"
35 #include "error.h"
36 #include "flags.h"
37 #include "make_cmd.h"
38 #include "variables.h"
39 #include "subst.h"
40 #include "input.h"
41 #include "externs.h"
42
43 #if defined (JOB_CONTROL)
44 #include "jobs.h"
45 #endif
46
47 extern int line_number, current_command_line_count;
48 extern int disallow_filename_globbing;
49
50 WORD_DESC *
51 make_bare_word (string)
52      char *string;
53 {
54   WORD_DESC *temp;
55
56   temp = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
57   if (*string)
58     temp->word = savestring (string);
59   else
60     {
61       temp->word = xmalloc (1);
62       temp->word[0] = '\0';
63     }
64
65   temp->flags = 0;
66   return (temp);
67 }
68
69 WORD_DESC *
70 make_word_flags (w, string)
71      WORD_DESC *w;
72      char *string;
73 {
74   register char *s;
75
76   for (s = string; *s; s++)
77     switch (*s)
78       {
79         case '$':
80           w->flags |= W_HASDOLLAR;
81           break;
82         case '\\':
83           break;        /* continue the loop */
84         case '\'':
85         case '`':
86         case '"':
87           w->flags |= W_QUOTED;
88           break;
89       }
90   return (w);
91 }
92
93 WORD_DESC *
94 make_word (string)
95      char *string;
96 {
97   WORD_DESC *temp;
98
99   temp = make_bare_word (string);
100   return (make_word_flags (temp, string));
101 }
102
103 #ifdef INCLUDE_UNUSED
104 WORD_DESC *
105 make_word_from_token (token)
106      int token;
107 {
108   char tokenizer[2];
109
110   tokenizer[0] = token;
111   tokenizer[1] = '\0';
112
113   return (make_word (tokenizer));
114 }
115 #endif
116
117 WORD_LIST *
118 make_word_list (word, link)
119      WORD_DESC *word;
120      WORD_LIST *link;
121 {
122   WORD_LIST *temp;
123
124   temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
125   temp->word = word;
126   temp->next = link;
127   return (temp);
128 }
129
130 WORD_LIST *
131 add_string_to_list (string, list)
132      char *string;
133      WORD_LIST *list;
134 {
135   WORD_LIST *temp;
136
137   temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
138   temp->word = make_word (string);
139   temp->next = list;
140   return (temp);
141 }
142
143 COMMAND *
144 make_command (type, pointer)
145      enum command_type type;
146      SIMPLE_COM *pointer;
147 {
148   COMMAND *temp;
149
150   temp = (COMMAND *)xmalloc (sizeof (COMMAND));
151   temp->type = type;
152   temp->value.Simple = pointer;
153   temp->value.Simple->flags = temp->flags = 0;
154   temp->redirects = (REDIRECT *)NULL;
155   return (temp);
156 }
157
158 COMMAND *
159 command_connect (com1, com2, connector)
160      COMMAND *com1, *com2;
161      int connector;
162 {
163   CONNECTION *temp;
164
165   temp = (CONNECTION *)xmalloc (sizeof (CONNECTION));
166   temp->connector = connector;
167   temp->first = com1;
168   temp->second = com2;
169   return (make_command (cm_connection, (SIMPLE_COM *)temp));
170 }
171
172 static COMMAND *
173 make_for_or_select (type, name, map_list, action)
174      enum command_type type;
175      WORD_DESC *name;
176      WORD_LIST *map_list;
177      COMMAND *action;
178 {
179   FOR_COM *temp;
180
181   temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
182   temp->flags = 0;
183   temp->name = name;
184   temp->map_list = map_list;
185   temp->action = action;
186   return (make_command (type, (SIMPLE_COM *)temp));
187 }
188
189 COMMAND *
190 make_for_command (name, map_list, action)
191      WORD_DESC *name;
192      WORD_LIST *map_list;
193      COMMAND *action;
194 {
195   return (make_for_or_select (cm_for, name, map_list, action));
196 }
197
198 COMMAND *
199 make_select_command (name, map_list, action)
200      WORD_DESC *name;
201      WORD_LIST *map_list;
202      COMMAND *action;
203 {
204 #if defined (SELECT_COMMAND)
205   return (make_for_or_select (cm_select, name, map_list, action));
206 #endif
207 }
208
209 COMMAND *
210 make_group_command (command)
211      COMMAND *command;
212 {
213   GROUP_COM *temp;
214
215   temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
216   temp->command = command;
217   return (make_command (cm_group, (SIMPLE_COM *)temp));
218 }
219
220 COMMAND *
221 make_case_command (word, clauses)
222      WORD_DESC *word;
223      PATTERN_LIST *clauses;
224 {
225   CASE_COM *temp;
226
227   temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
228   temp->flags = 0;
229   temp->word = word;
230   temp->clauses = REVERSE_LIST (clauses, PATTERN_LIST *);
231   return (make_command (cm_case, (SIMPLE_COM *)temp));
232 }
233
234 PATTERN_LIST *
235 make_pattern_list (patterns, action)
236      WORD_LIST *patterns;
237      COMMAND *action;
238 {
239   PATTERN_LIST *temp;
240
241   temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
242   temp->patterns = REVERSE_LIST (patterns, WORD_LIST *);
243   temp->action = action;
244   temp->next = NULL;
245   return (temp);
246 }
247
248 COMMAND *
249 make_if_command (test, true_case, false_case)
250      COMMAND *test, *true_case, *false_case;
251 {
252   IF_COM *temp;
253
254   temp = (IF_COM *)xmalloc (sizeof (IF_COM));
255   temp->flags = 0;
256   temp->test = test;
257   temp->true_case = true_case;
258   temp->false_case = false_case;
259   return (make_command (cm_if, (SIMPLE_COM *)temp));
260 }
261
262 static COMMAND *
263 make_until_or_while (which, test, action)
264      enum command_type which;
265      COMMAND *test, *action;
266 {
267   WHILE_COM *temp;
268
269   temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
270   temp->flags = 0;
271   temp->test = test;
272   temp->action = action;
273   return (make_command (which, (SIMPLE_COM *)temp));
274 }
275
276 COMMAND *
277 make_while_command (test, action)
278      COMMAND *test, *action;
279 {
280   return (make_until_or_while (cm_while, test, action));
281 }
282
283 COMMAND *
284 make_until_command (test, action)
285      COMMAND *test, *action;
286 {
287   return (make_until_or_while (cm_until, test, action));
288 }
289
290 COMMAND *
291 make_bare_simple_command ()
292 {
293   COMMAND *command;
294   SIMPLE_COM *temp;
295
296   command = (COMMAND *)xmalloc (sizeof (COMMAND));
297   command->value.Simple = temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
298
299   temp->flags = 0;
300   temp->line = line_number;
301   temp->words = (WORD_LIST *)NULL;
302   temp->redirects = (REDIRECT *)NULL;
303
304   command->type = cm_simple;
305   command->redirects = (REDIRECT *)NULL;
306   command->flags = 0;
307
308   return (command);
309 }
310
311 /* Return a command which is the connection of the word or redirection
312    in ELEMENT, and the command * or NULL in COMMAND. */
313 COMMAND *
314 make_simple_command (element, command)
315      ELEMENT element;
316      COMMAND *command;
317 {
318   /* If we are starting from scratch, then make the initial command
319      structure.  Also note that we have to fill in all the slots, since
320      malloc doesn't return zeroed space. */
321   if (!command)
322     command = make_bare_simple_command ();
323
324   if (element.word)
325     {
326       WORD_LIST *tw = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
327       tw->word = element.word;
328       tw->next = command->value.Simple->words;
329       command->value.Simple->words = tw;
330     }
331   else
332     {
333       REDIRECT *r = element.redirect;
334       /* Due to the way <> is implemented, there may be more than a single
335          redirection in element.redirect.  We just follow the chain as far
336          as it goes, and hook onto the end. */
337       while (r->next)
338         r = r->next;
339       r->next = command->value.Simple->redirects;
340       command->value.Simple->redirects = element.redirect;
341     }
342   return (command);
343 }
344
345 /* Because we are Bourne compatible, we read the input for this
346    << or <<- redirection now, from wherever input is coming from.
347    We store the input read into a WORD_DESC.  Replace the text of
348    the redirectee.word with the new input text.  If <<- is on,
349    then remove leading TABS from each line. */
350 void
351 make_here_document (temp)
352      REDIRECT *temp;
353 {
354   int kill_leading, redir_len;
355   char *redir_word, *document, *full_line;
356   int document_index, document_size, delim_unquoted;
357
358   if (temp->instruction != r_deblank_reading_until &&
359       temp->instruction != r_reading_until)
360     {
361       internal_error ("make_here_document: bad instruction type %d", temp->instruction);
362       return;
363     }
364
365   kill_leading = temp->instruction == r_deblank_reading_until;
366
367   document = (char *)NULL;
368   document_index = document_size = 0;
369
370   /* Quote removal is the only expansion performed on the delimiter
371      for here documents, making it an extremely special case. */
372   redir_word = string_quote_removal (temp->redirectee.filename->word, 0);
373
374   /* redirection_expand will return NULL if the expansion results in
375      multiple words or no words.  Check for that here, and just abort
376      this here document if it does. */
377   if (redir_word)
378     redir_len = strlen (redir_word);
379   else
380     {
381       temp->here_doc_eof = xmalloc (1);
382       temp->here_doc_eof[0] = '\0';
383       goto document_done;
384     }
385
386   free (temp->redirectee.filename->word);
387   temp->here_doc_eof = redir_word;
388
389   /* Read lines from wherever lines are coming from.
390      For each line read, if kill_leading, then kill the
391      leading tab characters.
392      If the line matches redir_word exactly, then we have
393      manufactured the document.  Otherwise, add the line to the
394      list of lines in the document. */
395
396   /* If the here-document delimiter was quoted, the lines should
397      be read verbatim from the input.  If it was not quoted, we
398      need to perform backslash-quoted newline removal. */
399   delim_unquoted = (temp->redirectee.filename->flags & W_QUOTED) == 0;
400   while (full_line = read_secondary_line (delim_unquoted))
401     {
402       register char *line;
403       int len;
404
405       line = full_line;
406       line_number++;
407
408       if (kill_leading && *line)
409         {
410           /* Hack:  To be compatible with some Bourne shells, we
411              check the word before stripping the whitespace.  This
412              is a hack, though. */
413           if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
414             goto document_done;
415
416           while (*line == '\t')
417             line++;
418         }
419
420       if (*line == 0)
421         continue;
422
423       if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
424         goto document_done;
425
426       len = strlen (line);
427       if (len + document_index >= document_size)
428         {
429           document_size = document_size ? 2 * (document_size + len) : 1000;
430           document = xrealloc (document, document_size);
431         }
432
433       /* len is guaranteed to be > 0 because of the check for line
434          being an empty string before the call to strlen. */
435       FASTCOPY (line, document + document_index, len);
436       document_index += len;
437     }
438
439 document_done:
440   if (document)
441     document[document_index] = '\0';
442   else
443     {
444       document = xmalloc (1);
445       document[0] = '\0';
446     }
447   temp->redirectee.filename->word = document;
448 }
449
450 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
451    INSTRUCTION is the instruction type, SOURCE is a file descriptor,
452    and DEST is a file descriptor or a WORD_DESC *. */
453 REDIRECT *
454 make_redirection (source, instruction, dest_and_filename)
455      int source;
456      enum r_instruction instruction;
457      REDIRECTEE dest_and_filename;
458 {
459   REDIRECT *temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
460
461   /* First do the common cases. */
462   temp->redirector = source;
463   temp->redirectee = dest_and_filename;
464   temp->instruction = instruction;
465   temp->flags = 0;
466   temp->next = (REDIRECT *)NULL;
467
468   switch (instruction)
469     {
470
471     case r_output_direction:            /* >foo */
472     case r_output_force:                /* >| foo */
473     case r_err_and_out:                 /* command &>filename */
474       temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
475       break;
476
477     case r_appending_to:                /* >>foo */
478       temp->flags = O_APPEND | O_WRONLY | O_CREAT;
479       break;
480
481     case r_input_direction:             /* <foo */
482     case r_inputa_direction:            /* foo & makes this. */
483       temp->flags = O_RDONLY;
484       break;
485
486     case r_input_output:                /* <>foo */
487       temp->flags = O_RDWR | O_CREAT;
488       break;
489
490     case r_deblank_reading_until:       /* <<-foo */
491     case r_reading_until:               /* << foo */
492     case r_close_this:                  /* <&- */
493     case r_duplicating_input:           /* 1<&2 */
494     case r_duplicating_output:          /* 1>&2 */
495     case r_duplicating_input_word:      /* 1<&$foo */
496     case r_duplicating_output_word:     /* 1>&$foo */
497       break;
498
499     default:
500       programming_error ("make_redirection: redirection instruction `%d' out of range", instruction);
501       abort ();
502       break;
503     }
504   return (temp);
505 }
506
507 COMMAND *
508 make_function_def (name, command, lineno, lstart)
509      WORD_DESC *name;
510      COMMAND *command;
511      int lineno, lstart;
512 {
513   FUNCTION_DEF *temp;
514
515   temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
516   temp->command = command;
517   temp->name = name;
518   temp->line = lineno;
519   temp->ignore = 0;
520   command->line = lstart;
521   return (make_command (cm_function_def, (SIMPLE_COM *)temp));
522 }
523
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. */
527 COMMAND *
528 clean_simple_command (command)
529      COMMAND *command;
530 {
531   if (command->type != cm_simple)
532     programming_error ("clean_simple_command: bad command type `%d'", command->type);
533   else
534     {
535       command->value.Simple->words =
536         REVERSE_LIST (command->value.Simple->words, WORD_LIST *);
537       command->value.Simple->redirects =
538         REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
539     }
540
541   return (command);
542 }
543
544 /* The Yacc grammar productions have a problem, in that they take a
545    list followed by an ampersand (`&') and do a simple command connection,
546    making the entire list effectively asynchronous, instead of just
547    the last command.  This means that when the list is executed, all
548    the commands have stdin set to /dev/null when job control is not
549    active, instead of just the last.  This is wrong, and needs fixing
550    up.  This function takes the `&' and applies it to the last command
551    in the list.  This is done only for lists connected by `;'; it makes
552    `;' bind `tighter' than `&'. */
553 COMMAND *
554 connect_async_list (command, command2, connector)
555      COMMAND *command, *command2;
556      int connector;
557 {
558   COMMAND *t, *t1, *t2;
559
560   t1 = command;
561   t = command->value.Connection->second;
562
563   if (!t || (command->flags & CMD_WANT_SUBSHELL) ||
564       command->value.Connection->connector != ';')
565     {
566       t = command_connect (command, command2, connector);
567       return t;
568     }
569
570   /* This is just defensive programming.  The Yacc precedence rules
571      will generally hand this function a command where t points directly
572      to the command we want (e.g. given a ; b ; c ; d &, t1 will point
573      to the `a ; b ; c' list and t will be the `d').  We only want to do
574      this if the list is not being executed as a unit in the background
575      with `( ... )', so we have to check for CMD_WANT_SUBSHELL.  That's
576      the only way to tell. */
577   while (((t->flags & CMD_WANT_SUBSHELL) == 0) && t->type == cm_connection &&
578          t->value.Connection->connector == ';')
579     {
580       t1 = t;
581       t = t->value.Connection->second;
582     }
583   /* Now we have t pointing to the last command in the list, and
584      t1->value.Connection->second == t. */
585   t2 = command_connect (t, command2, connector);
586   t1->value.Connection->second = t2;
587   return command;
588 }