]> git.ipfire.org Git - thirdparty/bash.git/blob - make_cmd.c
654895f7b88709b19b68f5be72f919f28ae2b77a
[thirdparty/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 #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
52 WORD_DESC *
53 make_bare_word (string)
54 char *string;
55 {
56 WORD_DESC *temp;
57
58 temp = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
59 if (*string)
60 temp->word = savestring (string);
61 else
62 {
63 temp->word = xmalloc (1);
64 temp->word[0] = '\0';
65 }
66
67 temp->flags = 0;
68 return (temp);
69 }
70
71 WORD_DESC *
72 make_word_flags (w, string)
73 WORD_DESC *w;
74 char *string;
75 {
76 register char *s;
77
78 for (s = string; *s; s++)
79 switch (*s)
80 {
81 case '$':
82 w->flags |= W_HASDOLLAR;
83 break;
84 case '\\':
85 break; /* continue the loop */
86 case '\'':
87 case '`':
88 case '"':
89 w->flags |= W_QUOTED;
90 break;
91 }
92 return (w);
93 }
94
95 WORD_DESC *
96 make_word (string)
97 char *string;
98 {
99 WORD_DESC *temp;
100
101 temp = make_bare_word (string);
102 return (make_word_flags (temp, string));
103 }
104
105 WORD_DESC *
106 make_word_from_token (token)
107 int token;
108 {
109 char tokenizer[2];
110
111 tokenizer[0] = token;
112 tokenizer[1] = '\0';
113
114 return (make_word (tokenizer));
115 }
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_arith_command (exp)
292 WORD_LIST *exp;
293 {
294 #if defined (DPAREN_ARITHMETIC)
295 COMMAND *command;
296 ARITH_COM *temp;
297
298 command = (COMMAND *)xmalloc (sizeof (COMMAND));
299 command->value.Arith = temp = (ARITH_COM *)xmalloc (sizeof (ARITH_COM));
300
301 temp->flags = 0;
302 temp->line = line_number;
303 temp->exp = exp;
304
305 command->type = cm_arith;
306 command->redirects = (REDIRECT *)NULL;
307 command->flags = 0;
308
309 return (command);
310 #else
311 return ((COMMAND *)NULL);
312 #endif
313 }
314
315 #if defined (COND_COMMAND)
316 struct cond_com *
317 make_cond_node (type, op, left, right)
318 int type;
319 WORD_DESC *op;
320 struct cond_com *left, *right;
321 {
322 COND_COM *temp;
323
324 temp = (COND_COM *)xmalloc (sizeof (COND_COM));
325 temp->flags = 0;
326 temp->line = line_number;
327 temp->type = type;
328 temp->op = op;
329 temp->left = left;
330 temp->right = right;
331
332 return (temp);
333 }
334 #endif
335
336 COMMAND *
337 make_cond_command (cond_node)
338 COND_COM *cond_node;
339 {
340 #if defined (COND_COMMAND)
341 COMMAND *command;
342
343 command = (COMMAND *)xmalloc (sizeof (COMMAND));
344 command->value.Cond = cond_node;
345
346 command->type = cm_cond;
347 command->redirects = (REDIRECT *)NULL;
348 command->flags = 0;
349 command->line = cond_node ? cond_node->line : 0;
350
351 return (command);
352 #else
353 return ((COMMAND *)NULL);
354 #endif
355 }
356
357 COMMAND *
358 make_bare_simple_command ()
359 {
360 COMMAND *command;
361 SIMPLE_COM *temp;
362
363 command = (COMMAND *)xmalloc (sizeof (COMMAND));
364 command->value.Simple = temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
365
366 temp->flags = 0;
367 temp->line = line_number;
368 temp->words = (WORD_LIST *)NULL;
369 temp->redirects = (REDIRECT *)NULL;
370
371 command->type = cm_simple;
372 command->redirects = (REDIRECT *)NULL;
373 command->flags = 0;
374
375 return (command);
376 }
377
378 /* Return a command which is the connection of the word or redirection
379 in ELEMENT, and the command * or NULL in COMMAND. */
380 COMMAND *
381 make_simple_command (element, command)
382 ELEMENT element;
383 COMMAND *command;
384 {
385 /* If we are starting from scratch, then make the initial command
386 structure. Also note that we have to fill in all the slots, since
387 malloc doesn't return zeroed space. */
388 if (!command)
389 command = make_bare_simple_command ();
390
391 if (element.word)
392 {
393 WORD_LIST *tw = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
394 tw->word = element.word;
395 tw->next = command->value.Simple->words;
396 command->value.Simple->words = tw;
397 }
398 else
399 {
400 REDIRECT *r = element.redirect;
401 /* Due to the way <> is implemented, there may be more than a single
402 redirection in element.redirect. We just follow the chain as far
403 as it goes, and hook onto the end. */
404 while (r->next)
405 r = r->next;
406 r->next = command->value.Simple->redirects;
407 command->value.Simple->redirects = element.redirect;
408 }
409 return (command);
410 }
411
412 /* Because we are Bourne compatible, we read the input for this
413 << or <<- redirection now, from wherever input is coming from.
414 We store the input read into a WORD_DESC. Replace the text of
415 the redirectee.word with the new input text. If <<- is on,
416 then remove leading TABS from each line. */
417 void
418 make_here_document (temp)
419 REDIRECT *temp;
420 {
421 int kill_leading, redir_len;
422 char *redir_word, *document, *full_line;
423 int document_index, document_size, delim_unquoted;
424
425 if (temp->instruction != r_deblank_reading_until &&
426 temp->instruction != r_reading_until)
427 {
428 internal_error ("make_here_document: bad instruction type %d", temp->instruction);
429 return;
430 }
431
432 kill_leading = temp->instruction == r_deblank_reading_until;
433
434 document = (char *)NULL;
435 document_index = document_size = 0;
436
437 /* Quote removal is the only expansion performed on the delimiter
438 for here documents, making it an extremely special case. */
439 redir_word = string_quote_removal (temp->redirectee.filename->word, 0);
440
441 /* redirection_expand will return NULL if the expansion results in
442 multiple words or no words. Check for that here, and just abort
443 this here document if it does. */
444 if (redir_word)
445 redir_len = strlen (redir_word);
446 else
447 {
448 temp->here_doc_eof = xmalloc (1);
449 temp->here_doc_eof[0] = '\0';
450 goto document_done;
451 }
452
453 free (temp->redirectee.filename->word);
454 temp->here_doc_eof = redir_word;
455
456 /* Read lines from wherever lines are coming from.
457 For each line read, if kill_leading, then kill the
458 leading tab characters.
459 If the line matches redir_word exactly, then we have
460 manufactured the document. Otherwise, add the line to the
461 list of lines in the document. */
462
463 /* If the here-document delimiter was quoted, the lines should
464 be read verbatim from the input. If it was not quoted, we
465 need to perform backslash-quoted newline removal. */
466 delim_unquoted = (temp->redirectee.filename->flags & W_QUOTED) == 0;
467 while (full_line = read_secondary_line (delim_unquoted))
468 {
469 register char *line;
470 int len;
471
472 line = full_line;
473 line_number++;
474
475 if (kill_leading && *line)
476 {
477 /* Hack: To be compatible with some Bourne shells, we
478 check the word before stripping the whitespace. This
479 is a hack, though. */
480 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
481 goto document_done;
482
483 while (*line == '\t')
484 line++;
485 }
486
487 if (*line == 0)
488 continue;
489
490 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
491 goto document_done;
492
493 len = strlen (line);
494 if (len + document_index >= document_size)
495 {
496 document_size = document_size ? 2 * (document_size + len) : len + 2;
497 document = xrealloc (document, document_size);
498 }
499
500 /* len is guaranteed to be > 0 because of the check for line
501 being an empty string before the call to strlen. */
502 FASTCOPY (line, document + document_index, len);
503 document_index += len;
504 }
505
506 document_done:
507 if (document)
508 document[document_index] = '\0';
509 else
510 {
511 document = xmalloc (1);
512 document[0] = '\0';
513 }
514 temp->redirectee.filename->word = document;
515 }
516
517 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
518 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
519 and DEST is a file descriptor or a WORD_DESC *. */
520 REDIRECT *
521 make_redirection (source, instruction, dest_and_filename)
522 int source;
523 enum r_instruction instruction;
524 REDIRECTEE dest_and_filename;
525 {
526 REDIRECT *temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
527
528 /* First do the common cases. */
529 temp->redirector = source;
530 temp->redirectee = dest_and_filename;
531 temp->instruction = instruction;
532 temp->flags = 0;
533 temp->next = (REDIRECT *)NULL;
534
535 switch (instruction)
536 {
537
538 case r_output_direction: /* >foo */
539 case r_output_force: /* >| foo */
540 case r_err_and_out: /* command &>filename */
541 temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
542 break;
543
544 case r_appending_to: /* >>foo */
545 temp->flags = O_APPEND | O_WRONLY | O_CREAT;
546 break;
547
548 case r_input_direction: /* <foo */
549 case r_inputa_direction: /* foo & makes this. */
550 temp->flags = O_RDONLY;
551 break;
552
553 case r_input_output: /* <>foo */
554 temp->flags = O_RDWR | O_CREAT;
555 break;
556
557 case r_deblank_reading_until: /* <<-foo */
558 case r_reading_until: /* << foo */
559 case r_close_this: /* <&- */
560 case r_duplicating_input: /* 1<&2 */
561 case r_duplicating_output: /* 1>&2 */
562 case r_duplicating_input_word: /* 1<&$foo */
563 case r_duplicating_output_word: /* 1>&$foo */
564 break;
565
566 default:
567 programming_error ("make_redirection: redirection instruction `%d' out of range", instruction);
568 abort ();
569 break;
570 }
571 return (temp);
572 }
573
574 COMMAND *
575 make_function_def (name, command, lineno, lstart)
576 WORD_DESC *name;
577 COMMAND *command;
578 int lineno, lstart;
579 {
580 FUNCTION_DEF *temp;
581
582 temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
583 temp->command = command;
584 temp->name = name;
585 temp->line = lineno;
586 temp->flags = 0;
587 command->line = lstart;
588 return (make_command (cm_function_def, (SIMPLE_COM *)temp));
589 }
590
591 /* Reverse the word list and redirection list in the simple command
592 has just been parsed. It seems simpler to do this here the one
593 time then by any other method that I can think of. */
594 COMMAND *
595 clean_simple_command (command)
596 COMMAND *command;
597 {
598 if (command->type != cm_simple)
599 command_error ("clean_simple_command", CMDERR_BADTYPE, command->type, 0);
600 else
601 {
602 command->value.Simple->words =
603 REVERSE_LIST (command->value.Simple->words, WORD_LIST *);
604 command->value.Simple->redirects =
605 REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
606 }
607
608 return (command);
609 }
610
611 /* The Yacc grammar productions have a problem, in that they take a
612 list followed by an ampersand (`&') and do a simple command connection,
613 making the entire list effectively asynchronous, instead of just
614 the last command. This means that when the list is executed, all
615 the commands have stdin set to /dev/null when job control is not
616 active, instead of just the last. This is wrong, and needs fixing
617 up. This function takes the `&' and applies it to the last command
618 in the list. This is done only for lists connected by `;'; it makes
619 `;' bind `tighter' than `&'. */
620 COMMAND *
621 connect_async_list (command, command2, connector)
622 COMMAND *command, *command2;
623 int connector;
624 {
625 COMMAND *t, *t1, *t2;
626
627 t1 = command;
628 t = command->value.Connection->second;
629
630 if (!t || (command->flags & CMD_WANT_SUBSHELL) ||
631 command->value.Connection->connector != ';')
632 {
633 t = command_connect (command, command2, connector);
634 return t;
635 }
636
637 /* This is just defensive programming. The Yacc precedence rules
638 will generally hand this function a command where t points directly
639 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
640 to the `a ; b ; c' list and t will be the `d'). We only want to do
641 this if the list is not being executed as a unit in the background
642 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
643 the only way to tell. */
644 while (((t->flags & CMD_WANT_SUBSHELL) == 0) && t->type == cm_connection &&
645 t->value.Connection->connector == ';')
646 {
647 t1 = t;
648 t = t->value.Connection->second;
649 }
650 /* Now we have t pointing to the last command in the list, and
651 t1->value.Connection->second == t. */
652 t2 = command_connect (t, command2, connector);
653 t1->value.Connection->second = t2;
654 return command;
655 }