867484ffce5470aa787460068b1a7fca4587fac7
[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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include "bashtypes.h"
26 #ifndef _MINIX
27 #  include <sys/file.h>
28 #endif
29 #include "filecntl.h"
30 #include "bashansi.h"
31 #if defined (HAVE_UNISTD_H)
32 #  include <unistd.h>
33 #endif
34
35 #include "command.h"
36 #include "general.h"
37 #include "error.h"
38 #include "flags.h"
39 #include "make_cmd.h"
40 #include "variables.h"
41 #include "subst.h"
42 #include "input.h"
43 #include "externs.h"
44
45 #if defined (JOB_CONTROL)
46 #include "jobs.h"
47 #endif
48
49 extern int line_number, current_command_line_count;
50 extern int disallow_filename_globbing;
51 extern int last_command_exit_value;
52
53
54 WORD_DESC *
55 make_bare_word (string)
56      char *string;
57 {
58   WORD_DESC *temp;
59
60   temp = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
61   if (*string)
62     temp->word = savestring (string);
63   else
64     {
65       temp->word = xmalloc (1);
66       temp->word[0] = '\0';
67     }
68
69   temp->flags = 0;
70   return (temp);
71 }
72
73 WORD_DESC *
74 make_word_flags (w, string)
75      WORD_DESC *w;
76      char *string;
77 {
78   register char *s;
79
80   for (s = string; *s; s++)
81     switch (*s)
82       {
83         case '$':
84           w->flags |= W_HASDOLLAR;
85           break;
86         case '\\':
87           break;        /* continue the loop */
88         case '\'':
89         case '`':
90         case '"':
91           w->flags |= W_QUOTED;
92           break;
93       }
94   return (w);
95 }
96
97 WORD_DESC *
98 make_word (string)
99      char *string;
100 {
101   WORD_DESC *temp;
102
103   temp = make_bare_word (string);
104   return (make_word_flags (temp, string));
105 }
106
107 WORD_DESC *
108 make_word_from_token (token)
109      int token;
110 {
111   char tokenizer[2];
112
113   tokenizer[0] = token;
114   tokenizer[1] = '\0';
115
116   return (make_word (tokenizer));
117 }
118
119 WORD_LIST *
120 make_word_list (word, link)
121      WORD_DESC *word;
122      WORD_LIST *link;
123 {
124   WORD_LIST *temp;
125
126   temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
127   temp->word = word;
128   temp->next = link;
129   return (temp);
130 }
131
132 WORD_LIST *
133 add_string_to_list (string, list)
134      char *string;
135      WORD_LIST *list;
136 {
137   WORD_LIST *temp;
138
139   temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
140   temp->word = make_word (string);
141   temp->next = list;
142   return (temp);
143 }
144
145 COMMAND *
146 make_command (type, pointer)
147      enum command_type type;
148      SIMPLE_COM *pointer;
149 {
150   COMMAND *temp;
151
152   temp = (COMMAND *)xmalloc (sizeof (COMMAND));
153   temp->type = type;
154   temp->value.Simple = pointer;
155   temp->value.Simple->flags = temp->flags = 0;
156   temp->redirects = (REDIRECT *)NULL;
157   return (temp);
158 }
159
160 COMMAND *
161 command_connect (com1, com2, connector)
162      COMMAND *com1, *com2;
163      int connector;
164 {
165   CONNECTION *temp;
166
167   temp = (CONNECTION *)xmalloc (sizeof (CONNECTION));
168   temp->connector = connector;
169   temp->first = com1;
170   temp->second = com2;
171   return (make_command (cm_connection, (SIMPLE_COM *)temp));
172 }
173
174 static COMMAND *
175 make_for_or_select (type, name, map_list, action)
176      enum command_type type;
177      WORD_DESC *name;
178      WORD_LIST *map_list;
179      COMMAND *action;
180 {
181   FOR_COM *temp;
182
183   temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
184   temp->flags = 0;
185   temp->name = name;
186   temp->map_list = map_list;
187   temp->action = action;
188   return (make_command (type, (SIMPLE_COM *)temp));
189 }
190
191 COMMAND *
192 make_for_command (name, map_list, action)
193      WORD_DESC *name;
194      WORD_LIST *map_list;
195      COMMAND *action;
196 {
197   return (make_for_or_select (cm_for, name, map_list, action));
198 }
199
200 COMMAND *
201 make_select_command (name, map_list, action)
202      WORD_DESC *name;
203      WORD_LIST *map_list;
204      COMMAND *action;
205 {
206 #if defined (SELECT_COMMAND)
207   return (make_for_or_select (cm_select, name, map_list, action));
208 #else
209   last_command_exit_value = 2;
210   return ((COMMAND *)NULL);
211 #endif
212 }
213
214 #if defined (ARITH_FOR_COMMAND)
215 static WORD_LIST *
216 make_arith_for_expr (s)
217      char *s;
218 {
219   WORD_LIST *result;
220   WORD_DESC *w;
221
222   if (s == 0 || *s == '\0')
223     return ((WORD_LIST *)NULL);
224   w = make_word (s);
225   result = make_word_list (w, (WORD_LIST *)NULL);
226   return result;
227 }
228 #endif
229
230 COMMAND *
231 make_arith_for_command (exprs, action, lineno)
232      WORD_LIST *exprs;
233      COMMAND *action;
234      int lineno;
235 {
236 #if defined (ARITH_FOR_COMMAND)
237   ARITH_FOR_COM *temp;
238   WORD_LIST *init, *test, *step;
239   char *s, *t, *start;
240   int nsemi, l;
241
242   init = test = step = (WORD_LIST *)NULL;
243   /* Parse the string into the three component sub-expressions. */
244   start = t = s = exprs->word->word;
245   for (nsemi = 0; ;)
246     {
247       /* skip whitespace at the start of each sub-expression. */
248       while (whitespace (*s))
249         s++;
250       start = s;
251       /* skip to the semicolon or EOS */
252       while (*s && *s != ';')
253         s++;
254
255       t = (s > start) ? substring (start, 0, s - start) : (char *)NULL;
256
257       nsemi++;
258       switch (nsemi)
259         {
260         case 1:
261           init = make_arith_for_expr (t);
262           break;
263         case 2:
264           test = make_arith_for_expr (t);
265           break;
266         case 3:
267           step = make_arith_for_expr (t);
268           break;
269         }
270
271       FREE (t);
272       if (*s == '\0')
273         break;
274       s++;      /* skip over semicolon */
275     }
276
277   if (nsemi != 3)
278     {
279       if (nsemi < 3)
280         parser_error (lineno, "syntax error: arithmetic expression required");
281       else
282         parser_error (lineno, "syntax error: `;' unexpected");
283       parser_error (lineno, "syntax error: `((%s))'", exprs->word->word);
284       last_command_exit_value = 2;
285       return ((COMMAND *)NULL);
286     }
287
288   temp = (ARITH_FOR_COM *)xmalloc (sizeof (ARITH_FOR_COM));
289   temp->flags = 0;
290   temp->line = lineno;
291   temp->init = init ? init : make_arith_for_expr ("1");
292   temp->test = test ? test : make_arith_for_expr ("1");
293   temp->step = step ? step : make_arith_for_expr ("1");
294   temp->action = action;
295
296   return (make_command (cm_arith_for, (SIMPLE_COM *)temp));
297 #else
298   last_command_exit_value = 2;
299   return ((COMMAND *)NULL);
300 #endif /* ARITH_FOR_COMMAND */
301 }
302
303 COMMAND *
304 make_group_command (command)
305      COMMAND *command;
306 {
307   GROUP_COM *temp;
308
309   temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
310   temp->command = command;
311   return (make_command (cm_group, (SIMPLE_COM *)temp));
312 }
313
314 COMMAND *
315 make_case_command (word, clauses)
316      WORD_DESC *word;
317      PATTERN_LIST *clauses;
318 {
319   CASE_COM *temp;
320
321   temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
322   temp->flags = 0;
323   temp->word = word;
324   temp->clauses = REVERSE_LIST (clauses, PATTERN_LIST *);
325   return (make_command (cm_case, (SIMPLE_COM *)temp));
326 }
327
328 PATTERN_LIST *
329 make_pattern_list (patterns, action)
330      WORD_LIST *patterns;
331      COMMAND *action;
332 {
333   PATTERN_LIST *temp;
334
335   temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
336   temp->patterns = REVERSE_LIST (patterns, WORD_LIST *);
337   temp->action = action;
338   temp->next = NULL;
339   return (temp);
340 }
341
342 COMMAND *
343 make_if_command (test, true_case, false_case)
344      COMMAND *test, *true_case, *false_case;
345 {
346   IF_COM *temp;
347
348   temp = (IF_COM *)xmalloc (sizeof (IF_COM));
349   temp->flags = 0;
350   temp->test = test;
351   temp->true_case = true_case;
352   temp->false_case = false_case;
353   return (make_command (cm_if, (SIMPLE_COM *)temp));
354 }
355
356 static COMMAND *
357 make_until_or_while (which, test, action)
358      enum command_type which;
359      COMMAND *test, *action;
360 {
361   WHILE_COM *temp;
362
363   temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
364   temp->flags = 0;
365   temp->test = test;
366   temp->action = action;
367   return (make_command (which, (SIMPLE_COM *)temp));
368 }
369
370 COMMAND *
371 make_while_command (test, action)
372      COMMAND *test, *action;
373 {
374   return (make_until_or_while (cm_while, test, action));
375 }
376
377 COMMAND *
378 make_until_command (test, action)
379      COMMAND *test, *action;
380 {
381   return (make_until_or_while (cm_until, test, action));
382 }
383
384 COMMAND *
385 make_arith_command (exp)
386      WORD_LIST *exp;
387 {
388 #if defined (DPAREN_ARITHMETIC)
389   COMMAND *command;
390   ARITH_COM *temp;
391
392   command = (COMMAND *)xmalloc (sizeof (COMMAND));
393   command->value.Arith = temp = (ARITH_COM *)xmalloc (sizeof (ARITH_COM));
394
395   temp->flags = 0;
396   temp->line = line_number;
397   temp->exp = exp;
398
399   command->type = cm_arith;
400   command->redirects = (REDIRECT *)NULL;
401   command->flags = 0;
402
403   return (command);
404 #else
405   last_command_exit_value = 2;
406   return ((COMMAND *)NULL);
407 #endif
408 }
409
410 #if defined (COND_COMMAND)
411 struct cond_com *
412 make_cond_node (type, op, left, right)
413      int type;
414      WORD_DESC *op;
415      struct cond_com *left, *right;
416 {
417   COND_COM *temp;
418
419   temp = (COND_COM *)xmalloc (sizeof (COND_COM));
420   temp->flags = 0;
421   temp->line = line_number;
422   temp->type = type;
423   temp->op = op;
424   temp->left = left;
425   temp->right = right;
426
427   return (temp);
428 }
429 #endif
430
431 COMMAND *
432 make_cond_command (cond_node)
433      COND_COM *cond_node;
434 {
435 #if defined (COND_COMMAND)
436   COMMAND *command;
437
438   command = (COMMAND *)xmalloc (sizeof (COMMAND));
439   command->value.Cond = cond_node;
440
441   command->type = cm_cond;
442   command->redirects = (REDIRECT *)NULL;
443   command->flags = 0;
444   command->line = cond_node ? cond_node->line : 0;
445
446   return (command);
447 #else
448   last_command_exit_value = 2;
449   return ((COMMAND *)NULL);
450 #endif
451 }
452
453 COMMAND *
454 make_bare_simple_command ()
455 {
456   COMMAND *command;
457   SIMPLE_COM *temp;
458
459   command = (COMMAND *)xmalloc (sizeof (COMMAND));
460   command->value.Simple = temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
461
462   temp->flags = 0;
463   temp->line = line_number;
464   temp->words = (WORD_LIST *)NULL;
465   temp->redirects = (REDIRECT *)NULL;
466
467   command->type = cm_simple;
468   command->redirects = (REDIRECT *)NULL;
469   command->flags = 0;
470
471   return (command);
472 }
473
474 /* Return a command which is the connection of the word or redirection
475    in ELEMENT, and the command * or NULL in COMMAND. */
476 COMMAND *
477 make_simple_command (element, command)
478      ELEMENT element;
479      COMMAND *command;
480 {
481   /* If we are starting from scratch, then make the initial command
482      structure.  Also note that we have to fill in all the slots, since
483      malloc doesn't return zeroed space. */
484   if (!command)
485     command = make_bare_simple_command ();
486
487   if (element.word)
488     {
489       WORD_LIST *tw = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
490       tw->word = element.word;
491       tw->next = command->value.Simple->words;
492       command->value.Simple->words = tw;
493     }
494   else
495     {
496       REDIRECT *r = element.redirect;
497       /* Due to the way <> is implemented, there may be more than a single
498          redirection in element.redirect.  We just follow the chain as far
499          as it goes, and hook onto the end. */
500       while (r->next)
501         r = r->next;
502       r->next = command->value.Simple->redirects;
503       command->value.Simple->redirects = element.redirect;
504     }
505   return (command);
506 }
507
508 /* Because we are Bourne compatible, we read the input for this
509    << or <<- redirection now, from wherever input is coming from.
510    We store the input read into a WORD_DESC.  Replace the text of
511    the redirectee.word with the new input text.  If <<- is on,
512    then remove leading TABS from each line. */
513 void
514 make_here_document (temp)
515      REDIRECT *temp;
516 {
517   int kill_leading, redir_len;
518   char *redir_word, *document, *full_line;
519   int document_index, document_size, delim_unquoted;
520
521   if (temp->instruction != r_deblank_reading_until &&
522       temp->instruction != r_reading_until)
523     {
524       internal_error ("make_here_document: bad instruction type %d", temp->instruction);
525       return;
526     }
527
528   kill_leading = temp->instruction == r_deblank_reading_until;
529
530   document = (char *)NULL;
531   document_index = document_size = 0;
532
533   /* Quote removal is the only expansion performed on the delimiter
534      for here documents, making it an extremely special case. */
535   redir_word = string_quote_removal (temp->redirectee.filename->word, 0);
536
537   /* redirection_expand will return NULL if the expansion results in
538      multiple words or no words.  Check for that here, and just abort
539      this here document if it does. */
540   if (redir_word)
541     redir_len = strlen (redir_word);
542   else
543     {
544       temp->here_doc_eof = xmalloc (1);
545       temp->here_doc_eof[0] = '\0';
546       goto document_done;
547     }
548
549   free (temp->redirectee.filename->word);
550   temp->here_doc_eof = redir_word;
551
552   /* Read lines from wherever lines are coming from.
553      For each line read, if kill_leading, then kill the
554      leading tab characters.
555      If the line matches redir_word exactly, then we have
556      manufactured the document.  Otherwise, add the line to the
557      list of lines in the document. */
558
559   /* If the here-document delimiter was quoted, the lines should
560      be read verbatim from the input.  If it was not quoted, we
561      need to perform backslash-quoted newline removal. */
562   delim_unquoted = (temp->redirectee.filename->flags & W_QUOTED) == 0;
563   while (full_line = read_secondary_line (delim_unquoted))
564     {
565       register char *line;
566       int len;
567
568       line = full_line;
569       line_number++;
570
571       if (kill_leading && *line)
572         {
573           /* Hack:  To be compatible with some Bourne shells, we
574              check the word before stripping the whitespace.  This
575              is a hack, though. */
576           if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
577             goto document_done;
578
579           while (*line == '\t')
580             line++;
581         }
582
583       if (*line == 0)
584         continue;
585
586       if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
587         goto document_done;
588
589       len = strlen (line);
590       if (len + document_index >= document_size)
591         {
592           document_size = document_size ? 2 * (document_size + len) : len + 2;
593           document = xrealloc (document, document_size);
594         }
595
596       /* len is guaranteed to be > 0 because of the check for line
597          being an empty string before the call to strlen. */
598       FASTCOPY (line, document + document_index, len);
599       document_index += len;
600     }
601
602 document_done:
603   if (document)
604     document[document_index] = '\0';
605   else
606     {
607       document = xmalloc (1);
608       document[0] = '\0';
609     }
610   temp->redirectee.filename->word = document;
611 }
612
613 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
614    INSTRUCTION is the instruction type, SOURCE is a file descriptor,
615    and DEST is a file descriptor or a WORD_DESC *. */
616 REDIRECT *
617 make_redirection (source, instruction, dest_and_filename)
618      int source;
619      enum r_instruction instruction;
620      REDIRECTEE dest_and_filename;
621 {
622   REDIRECT *temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
623
624   /* First do the common cases. */
625   temp->redirector = source;
626   temp->redirectee = dest_and_filename;
627   temp->instruction = instruction;
628   temp->flags = 0;
629   temp->next = (REDIRECT *)NULL;
630
631   switch (instruction)
632     {
633
634     case r_output_direction:            /* >foo */
635     case r_output_force:                /* >| foo */
636     case r_err_and_out:                 /* command &>filename */
637       temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
638       break;
639
640     case r_appending_to:                /* >>foo */
641       temp->flags = O_APPEND | O_WRONLY | O_CREAT;
642       break;
643
644     case r_input_direction:             /* <foo */
645     case r_inputa_direction:            /* foo & makes this. */
646       temp->flags = O_RDONLY;
647       break;
648
649     case r_input_output:                /* <>foo */
650       temp->flags = O_RDWR | O_CREAT;
651       break;
652
653     case r_deblank_reading_until:       /* <<-foo */
654     case r_reading_until:               /* << foo */
655     case r_close_this:                  /* <&- */
656     case r_duplicating_input:           /* 1<&2 */
657     case r_duplicating_output:          /* 1>&2 */
658     case r_duplicating_input_word:      /* 1<&$foo */
659     case r_duplicating_output_word:     /* 1>&$foo */
660       break;
661
662     default:
663       programming_error ("make_redirection: redirection instruction `%d' out of range", instruction);
664       abort ();
665       break;
666     }
667   return (temp);
668 }
669
670 COMMAND *
671 make_function_def (name, command, lineno, lstart)
672      WORD_DESC *name;
673      COMMAND *command;
674      int lineno, lstart;
675 {
676   FUNCTION_DEF *temp;
677
678   temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
679   temp->command = command;
680   temp->name = name;
681   temp->line = lineno;
682   temp->flags = 0;
683   command->line = lstart;
684   return (make_command (cm_function_def, (SIMPLE_COM *)temp));
685 }
686
687 COMMAND *
688 make_subshell_command (command)
689      COMMAND *command;
690 {
691   SUBSHELL_COM *temp;
692
693   temp = (SUBSHELL_COM *)xmalloc (sizeof (SUBSHELL_COM));
694   temp->command = command;
695   temp->flags = CMD_WANT_SUBSHELL;
696   return (make_command (cm_subshell, (SIMPLE_COM *)temp));
697 }
698
699 /* Reverse the word list and redirection list in the simple command
700    has just been parsed.  It seems simpler to do this here the one
701    time then by any other method that I can think of. */
702 COMMAND *
703 clean_simple_command (command)
704      COMMAND *command;
705 {
706   if (command->type != cm_simple)
707     command_error ("clean_simple_command", CMDERR_BADTYPE, command->type, 0);
708   else
709     {
710       command->value.Simple->words =
711         REVERSE_LIST (command->value.Simple->words, WORD_LIST *);
712       command->value.Simple->redirects =
713         REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
714     }
715
716   return (command);
717 }
718
719 /* The Yacc grammar productions have a problem, in that they take a
720    list followed by an ampersand (`&') and do a simple command connection,
721    making the entire list effectively asynchronous, instead of just
722    the last command.  This means that when the list is executed, all
723    the commands have stdin set to /dev/null when job control is not
724    active, instead of just the last.  This is wrong, and needs fixing
725    up.  This function takes the `&' and applies it to the last command
726    in the list.  This is done only for lists connected by `;'; it makes
727    `;' bind `tighter' than `&'. */
728 COMMAND *
729 connect_async_list (command, command2, connector)
730      COMMAND *command, *command2;
731      int connector;
732 {
733   COMMAND *t, *t1, *t2;
734
735   t1 = command;
736   t = command->value.Connection->second;
737
738   if (!t || (command->flags & CMD_WANT_SUBSHELL) ||
739       command->value.Connection->connector != ';')
740     {
741       t = command_connect (command, command2, connector);
742       return t;
743     }
744
745   /* This is just defensive programming.  The Yacc precedence rules
746      will generally hand this function a command where t points directly
747      to the command we want (e.g. given a ; b ; c ; d &, t1 will point
748      to the `a ; b ; c' list and t will be the `d').  We only want to do
749      this if the list is not being executed as a unit in the background
750      with `( ... )', so we have to check for CMD_WANT_SUBSHELL.  That's
751      the only way to tell. */
752   while (((t->flags & CMD_WANT_SUBSHELL) == 0) && t->type == cm_connection &&
753          t->value.Connection->connector == ';')
754     {
755       t1 = t;
756       t = t->value.Connection->second;
757     }
758   /* Now we have t pointing to the last command in the list, and
759      t1->value.Connection->second == t. */
760   t2 = command_connect (t, command2, connector);
761   t1->value.Connection->second = t2;
762   return command;
763 }