blob: b01d08457e4a4852a972220803eb8e034678a9cf [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* make_cmd.c -- Functions for making instances of the various
2 parser constructs. */
Jari Aalto726f6381996-08-26 18:22:31 +00003
Jari Aalto31859422009-01-12 13:36:28 +00004/* Copyright (C) 1989-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00005
Jari Aalto31859422009-01-12 13:36:28 +00006 This file is part of GNU Bash, the Bourne Again SHell.
Jari Aalto726f6381996-08-26 18:22:31 +00007
Jari Aalto31859422009-01-12 13:36:28 +00008 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000012
Jari Aalto31859422009-01-12 13:36:28 +000013 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000017
Jari Aalto31859422009-01-12 13:36:28 +000018 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
20*/
Jari Aalto726f6381996-08-26 18:22:31 +000021
Jari Aaltoccc6cda1996-12-23 17:02:34 +000022#include "config.h"
23
Jari Aalto726f6381996-08-26 18:22:31 +000024#include <stdio.h>
25#include "bashtypes.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000026#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000027# include <sys/file.h>
28#endif
Jari Aalto726f6381996-08-26 18:22:31 +000029#include "filecntl.h"
30#include "bashansi.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000031#if defined (HAVE_UNISTD_H)
32# include <unistd.h>
33#endif
34
Jari Aaltob80f6442004-07-27 13:29:18 +000035#include "bashintl.h"
36
Chet Ramey00018032011-11-21 20:51:19 -050037#include "parser.h"
Jari Aalto28ef6c32001-04-06 19:14:31 +000038#include "syntax.h"
Jari Aalto726f6381996-08-26 18:22:31 +000039#include "command.h"
40#include "general.h"
41#include "error.h"
42#include "flags.h"
43#include "make_cmd.h"
Jari Aalto7117c2d2002-07-17 14:10:11 +000044#include "dispose_cmd.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000045#include "variables.h"
Jari Aalto726f6381996-08-26 18:22:31 +000046#include "subst.h"
47#include "input.h"
Jari Aalto7117c2d2002-07-17 14:10:11 +000048#include "ocache.h"
Jari Aalto726f6381996-08-26 18:22:31 +000049#include "externs.h"
50
51#if defined (JOB_CONTROL)
52#include "jobs.h"
53#endif
54
Jari Aalto7117c2d2002-07-17 14:10:11 +000055#include "shmbutil.h"
56
Chet Ramey00018032011-11-21 20:51:19 -050057extern int line_number, current_command_line_count, parser_state;
Jari Aaltobb706242000-03-17 21:46:59 +000058extern int last_command_exit_value;
59
Jari Aalto7117c2d2002-07-17 14:10:11 +000060/* Object caching */
61sh_obj_cache_t wdcache = {0, 0, 0};
62sh_obj_cache_t wlcache = {0, 0, 0};
63
64#define WDCACHESIZE 60
65#define WLCACHESIZE 60
66
Jari Aaltob80f6442004-07-27 13:29:18 +000067static COMMAND *make_for_or_select __P((enum command_type, WORD_DESC *, WORD_LIST *, COMMAND *, int));
Jari Aaltof73dda02001-11-13 17:56:06 +000068#if defined (ARITH_FOR_COMMAND)
69static WORD_LIST *make_arith_for_expr __P((char *));
70#endif
71static COMMAND *make_until_or_while __P((enum command_type, COMMAND *, COMMAND *));
Jari Aalto726f6381996-08-26 18:22:31 +000072
Jari Aalto7117c2d2002-07-17 14:10:11 +000073void
74cmd_init ()
75{
76 ocache_create (wdcache, WORD_DESC, WDCACHESIZE);
77 ocache_create (wlcache, WORD_LIST, WLCACHESIZE);
78}
79
Jari Aalto726f6381996-08-26 18:22:31 +000080WORD_DESC *
Jari Aalto95732b42005-12-07 14:08:12 +000081alloc_word_desc ()
82{
83 WORD_DESC *temp;
84
85 ocache_alloc (wdcache, WORD_DESC, temp);
86 temp->flags = 0;
87 temp->word = 0;
88 return temp;
89}
90
91WORD_DESC *
Jari Aaltoccc6cda1996-12-23 17:02:34 +000092make_bare_word (string)
Jari Aaltof73dda02001-11-13 17:56:06 +000093 const char *string;
Jari Aalto726f6381996-08-26 18:22:31 +000094{
95 WORD_DESC *temp;
Jari Aalto95732b42005-12-07 14:08:12 +000096
97 temp = alloc_word_desc ();
Jari Aalto7117c2d2002-07-17 14:10:11 +000098
Jari Aaltoccc6cda1996-12-23 17:02:34 +000099 if (*string)
100 temp->word = savestring (string);
101 else
Jari Aalto726f6381996-08-26 18:22:31 +0000102 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000103 temp->word = (char *)xmalloc (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000104 temp->word[0] = '\0';
Jari Aalto726f6381996-08-26 18:22:31 +0000105 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000106
Jari Aalto726f6381996-08-26 18:22:31 +0000107 return (temp);
108}
109
110WORD_DESC *
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000111make_word_flags (w, string)
112 WORD_DESC *w;
Jari Aaltof73dda02001-11-13 17:56:06 +0000113 const char *string;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000114{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000115 register int i;
116 size_t slen;
117 DECLARE_MBSTATE;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000118
Jari Aalto7117c2d2002-07-17 14:10:11 +0000119 i = 0;
120 slen = strlen (string);
121 while (i < slen)
122 {
123 switch (string[i])
124 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000125 case '$':
126 w->flags |= W_HASDOLLAR;
127 break;
128 case '\\':
129 break; /* continue the loop */
130 case '\'':
131 case '`':
132 case '"':
133 w->flags |= W_QUOTED;
134 break;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000135 }
136
137 ADVANCE_CHAR (string, slen, i);
138 }
139
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000140 return (w);
141}
142
143WORD_DESC *
144make_word (string)
Jari Aaltof73dda02001-11-13 17:56:06 +0000145 const char *string;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000146{
147 WORD_DESC *temp;
148
149 temp = make_bare_word (string);
150 return (make_word_flags (temp, string));
151}
152
153WORD_DESC *
Jari Aalto726f6381996-08-26 18:22:31 +0000154make_word_from_token (token)
155 int token;
156{
157 char tokenizer[2];
158
159 tokenizer[0] = token;
160 tokenizer[1] = '\0';
161
162 return (make_word (tokenizer));
163}
164
165WORD_LIST *
Jari Aaltof73dda02001-11-13 17:56:06 +0000166make_word_list (word, wlink)
Jari Aalto726f6381996-08-26 18:22:31 +0000167 WORD_DESC *word;
Jari Aaltof73dda02001-11-13 17:56:06 +0000168 WORD_LIST *wlink;
Jari Aalto726f6381996-08-26 18:22:31 +0000169{
170 WORD_LIST *temp;
171
Jari Aalto7117c2d2002-07-17 14:10:11 +0000172 ocache_alloc (wlcache, WORD_LIST, temp);
Jari Aalto95732b42005-12-07 14:08:12 +0000173
Jari Aalto726f6381996-08-26 18:22:31 +0000174 temp->word = word;
Jari Aaltof73dda02001-11-13 17:56:06 +0000175 temp->next = wlink;
Jari Aalto726f6381996-08-26 18:22:31 +0000176 return (temp);
177}
178
Jari Aalto726f6381996-08-26 18:22:31 +0000179COMMAND *
180make_command (type, pointer)
181 enum command_type type;
182 SIMPLE_COM *pointer;
183{
184 COMMAND *temp;
185
186 temp = (COMMAND *)xmalloc (sizeof (COMMAND));
187 temp->type = type;
188 temp->value.Simple = pointer;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000189 temp->value.Simple->flags = temp->flags = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000190 temp->redirects = (REDIRECT *)NULL;
191 return (temp);
192}
193
194COMMAND *
195command_connect (com1, com2, connector)
196 COMMAND *com1, *com2;
197 int connector;
198{
199 CONNECTION *temp;
200
201 temp = (CONNECTION *)xmalloc (sizeof (CONNECTION));
202 temp->connector = connector;
203 temp->first = com1;
204 temp->second = com2;
205 return (make_command (cm_connection, (SIMPLE_COM *)temp));
206}
207
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000208static COMMAND *
Jari Aaltob80f6442004-07-27 13:29:18 +0000209make_for_or_select (type, name, map_list, action, lineno)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000210 enum command_type type;
211 WORD_DESC *name;
212 WORD_LIST *map_list;
213 COMMAND *action;
Jari Aaltob80f6442004-07-27 13:29:18 +0000214 int lineno;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000215{
216 FOR_COM *temp;
217
218 temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
219 temp->flags = 0;
220 temp->name = name;
Jari Aaltob80f6442004-07-27 13:29:18 +0000221 temp->line = lineno;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000222 temp->map_list = map_list;
223 temp->action = action;
224 return (make_command (type, (SIMPLE_COM *)temp));
225}
226
Jari Aalto726f6381996-08-26 18:22:31 +0000227COMMAND *
Jari Aaltob80f6442004-07-27 13:29:18 +0000228make_for_command (name, map_list, action, lineno)
Jari Aalto726f6381996-08-26 18:22:31 +0000229 WORD_DESC *name;
230 WORD_LIST *map_list;
231 COMMAND *action;
Jari Aaltob80f6442004-07-27 13:29:18 +0000232 int lineno;
Jari Aalto726f6381996-08-26 18:22:31 +0000233{
Jari Aaltob80f6442004-07-27 13:29:18 +0000234 return (make_for_or_select (cm_for, name, map_list, action, lineno));
Jari Aalto726f6381996-08-26 18:22:31 +0000235}
236
Jari Aalto726f6381996-08-26 18:22:31 +0000237COMMAND *
Jari Aaltob80f6442004-07-27 13:29:18 +0000238make_select_command (name, map_list, action, lineno)
Jari Aalto726f6381996-08-26 18:22:31 +0000239 WORD_DESC *name;
240 WORD_LIST *map_list;
241 COMMAND *action;
Jari Aaltob80f6442004-07-27 13:29:18 +0000242 int lineno;
Jari Aalto726f6381996-08-26 18:22:31 +0000243{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000244#if defined (SELECT_COMMAND)
Jari Aaltob80f6442004-07-27 13:29:18 +0000245 return (make_for_or_select (cm_select, name, map_list, action, lineno));
Jari Aaltobb706242000-03-17 21:46:59 +0000246#else
247 last_command_exit_value = 2;
248 return ((COMMAND *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000249#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000250}
Jari Aalto726f6381996-08-26 18:22:31 +0000251
Jari Aaltobb706242000-03-17 21:46:59 +0000252#if defined (ARITH_FOR_COMMAND)
253static WORD_LIST *
254make_arith_for_expr (s)
255 char *s;
256{
257 WORD_LIST *result;
Jari Aaltob80f6442004-07-27 13:29:18 +0000258 WORD_DESC *wd;
Jari Aaltobb706242000-03-17 21:46:59 +0000259
260 if (s == 0 || *s == '\0')
261 return ((WORD_LIST *)NULL);
Jari Aaltob80f6442004-07-27 13:29:18 +0000262 wd = make_word (s);
Jari Aalto95732b42005-12-07 14:08:12 +0000263 wd->flags |= W_NOGLOB|W_NOSPLIT|W_QUOTED|W_DQUOTE; /* no word splitting or globbing */
Jari Aaltob80f6442004-07-27 13:29:18 +0000264 result = make_word_list (wd, (WORD_LIST *)NULL);
Jari Aaltobb706242000-03-17 21:46:59 +0000265 return result;
266}
267#endif
268
Jari Aaltob80f6442004-07-27 13:29:18 +0000269/* Note that this function calls dispose_words on EXPRS, since it doesn't
270 use the word list directly. We free it here rather than at the caller
271 because no other function in this file requires that the caller free
272 any arguments. */
Jari Aaltobb706242000-03-17 21:46:59 +0000273COMMAND *
274make_arith_for_command (exprs, action, lineno)
275 WORD_LIST *exprs;
276 COMMAND *action;
277 int lineno;
278{
279#if defined (ARITH_FOR_COMMAND)
280 ARITH_FOR_COM *temp;
281 WORD_LIST *init, *test, *step;
282 char *s, *t, *start;
Chet Rameyac50fba2014-02-26 09:36:43 -0500283 int nsemi, i;
Jari Aaltobb706242000-03-17 21:46:59 +0000284
285 init = test = step = (WORD_LIST *)NULL;
286 /* Parse the string into the three component sub-expressions. */
287 start = t = s = exprs->word->word;
288 for (nsemi = 0; ;)
289 {
290 /* skip whitespace at the start of each sub-expression. */
291 while (whitespace (*s))
292 s++;
293 start = s;
294 /* skip to the semicolon or EOS */
Chet Rameyac50fba2014-02-26 09:36:43 -0500295 i = skip_to_delim (start, 0, ";", SD_NOJMP);
296 s = start + i;
Jari Aaltobb706242000-03-17 21:46:59 +0000297
Chet Rameyac50fba2014-02-26 09:36:43 -0500298 t = (i > 0) ? substring (start, 0, i) : (char *)NULL;
Jari Aaltobb706242000-03-17 21:46:59 +0000299
300 nsemi++;
301 switch (nsemi)
302 {
303 case 1:
304 init = make_arith_for_expr (t);
305 break;
306 case 2:
307 test = make_arith_for_expr (t);
308 break;
309 case 3:
310 step = make_arith_for_expr (t);
311 break;
312 }
313
314 FREE (t);
315 if (*s == '\0')
Jari Aalto28ef6c32001-04-06 19:14:31 +0000316 break;
Jari Aaltobb706242000-03-17 21:46:59 +0000317 s++; /* skip over semicolon */
318 }
319
320 if (nsemi != 3)
321 {
322 if (nsemi < 3)
Jari Aaltob80f6442004-07-27 13:29:18 +0000323 parser_error (lineno, _("syntax error: arithmetic expression required"));
Jari Aaltobb706242000-03-17 21:46:59 +0000324 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000325 parser_error (lineno, _("syntax error: `;' unexpected"));
326 parser_error (lineno, _("syntax error: `((%s))'"), exprs->word->word);
Chet Rameyac50fba2014-02-26 09:36:43 -0500327 free (init);
328 free (test);
329 free (step);
Jari Aaltobb706242000-03-17 21:46:59 +0000330 last_command_exit_value = 2;
331 return ((COMMAND *)NULL);
332 }
333
334 temp = (ARITH_FOR_COM *)xmalloc (sizeof (ARITH_FOR_COM));
335 temp->flags = 0;
336 temp->line = lineno;
337 temp->init = init ? init : make_arith_for_expr ("1");
338 temp->test = test ? test : make_arith_for_expr ("1");
339 temp->step = step ? step : make_arith_for_expr ("1");
340 temp->action = action;
341
Jari Aaltob80f6442004-07-27 13:29:18 +0000342 dispose_words (exprs);
Jari Aaltobb706242000-03-17 21:46:59 +0000343 return (make_command (cm_arith_for, (SIMPLE_COM *)temp));
344#else
Jari Aaltob80f6442004-07-27 13:29:18 +0000345 dispose_words (exprs);
Jari Aaltobb706242000-03-17 21:46:59 +0000346 last_command_exit_value = 2;
347 return ((COMMAND *)NULL);
348#endif /* ARITH_FOR_COMMAND */
349}
350
Jari Aalto726f6381996-08-26 18:22:31 +0000351COMMAND *
352make_group_command (command)
353 COMMAND *command;
354{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000355 GROUP_COM *temp;
Jari Aalto726f6381996-08-26 18:22:31 +0000356
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000357 temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
Jari Aalto726f6381996-08-26 18:22:31 +0000358 temp->command = command;
359 return (make_command (cm_group, (SIMPLE_COM *)temp));
360}
361
362COMMAND *
Jari Aaltob80f6442004-07-27 13:29:18 +0000363make_case_command (word, clauses, lineno)
Jari Aalto726f6381996-08-26 18:22:31 +0000364 WORD_DESC *word;
365 PATTERN_LIST *clauses;
Jari Aalto95732b42005-12-07 14:08:12 +0000366 int lineno;
Jari Aalto726f6381996-08-26 18:22:31 +0000367{
368 CASE_COM *temp;
369
370 temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
371 temp->flags = 0;
Jari Aaltob80f6442004-07-27 13:29:18 +0000372 temp->line = lineno;
Jari Aalto726f6381996-08-26 18:22:31 +0000373 temp->word = word;
374 temp->clauses = REVERSE_LIST (clauses, PATTERN_LIST *);
375 return (make_command (cm_case, (SIMPLE_COM *)temp));
376}
377
378PATTERN_LIST *
379make_pattern_list (patterns, action)
380 WORD_LIST *patterns;
381 COMMAND *action;
382{
383 PATTERN_LIST *temp;
384
385 temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
386 temp->patterns = REVERSE_LIST (patterns, WORD_LIST *);
387 temp->action = action;
388 temp->next = NULL;
Jari Aalto95732b42005-12-07 14:08:12 +0000389 temp->flags = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000390 return (temp);
391}
392
393COMMAND *
394make_if_command (test, true_case, false_case)
395 COMMAND *test, *true_case, *false_case;
396{
397 IF_COM *temp;
398
399 temp = (IF_COM *)xmalloc (sizeof (IF_COM));
400 temp->flags = 0;
401 temp->test = test;
402 temp->true_case = true_case;
403 temp->false_case = false_case;
404 return (make_command (cm_if, (SIMPLE_COM *)temp));
405}
406
407static COMMAND *
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000408make_until_or_while (which, test, action)
Jari Aalto726f6381996-08-26 18:22:31 +0000409 enum command_type which;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000410 COMMAND *test, *action;
Jari Aalto726f6381996-08-26 18:22:31 +0000411{
412 WHILE_COM *temp;
413
414 temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
415 temp->flags = 0;
416 temp->test = test;
417 temp->action = action;
418 return (make_command (which, (SIMPLE_COM *)temp));
419}
420
421COMMAND *
422make_while_command (test, action)
423 COMMAND *test, *action;
424{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000425 return (make_until_or_while (cm_while, test, action));
Jari Aalto726f6381996-08-26 18:22:31 +0000426}
427
428COMMAND *
429make_until_command (test, action)
430 COMMAND *test, *action;
431{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000432 return (make_until_or_while (cm_until, test, action));
Jari Aalto726f6381996-08-26 18:22:31 +0000433}
434
435COMMAND *
Jari Aaltocce855b1998-04-17 19:52:44 +0000436make_arith_command (exp)
437 WORD_LIST *exp;
438{
439#if defined (DPAREN_ARITHMETIC)
440 COMMAND *command;
441 ARITH_COM *temp;
442
443 command = (COMMAND *)xmalloc (sizeof (COMMAND));
444 command->value.Arith = temp = (ARITH_COM *)xmalloc (sizeof (ARITH_COM));
445
446 temp->flags = 0;
447 temp->line = line_number;
448 temp->exp = exp;
449
450 command->type = cm_arith;
451 command->redirects = (REDIRECT *)NULL;
452 command->flags = 0;
453
454 return (command);
455#else
Jari Aaltobb706242000-03-17 21:46:59 +0000456 last_command_exit_value = 2;
Jari Aaltocce855b1998-04-17 19:52:44 +0000457 return ((COMMAND *)NULL);
458#endif
459}
460
461#if defined (COND_COMMAND)
462struct cond_com *
463make_cond_node (type, op, left, right)
464 int type;
465 WORD_DESC *op;
466 struct cond_com *left, *right;
467{
468 COND_COM *temp;
469
470 temp = (COND_COM *)xmalloc (sizeof (COND_COM));
471 temp->flags = 0;
472 temp->line = line_number;
473 temp->type = type;
474 temp->op = op;
475 temp->left = left;
476 temp->right = right;
477
478 return (temp);
479}
480#endif
481
482COMMAND *
483make_cond_command (cond_node)
484 COND_COM *cond_node;
485{
486#if defined (COND_COMMAND)
487 COMMAND *command;
488
489 command = (COMMAND *)xmalloc (sizeof (COMMAND));
490 command->value.Cond = cond_node;
491
492 command->type = cm_cond;
493 command->redirects = (REDIRECT *)NULL;
494 command->flags = 0;
Jari Aaltob72432f1999-02-19 17:11:39 +0000495 command->line = cond_node ? cond_node->line : 0;
Jari Aaltocce855b1998-04-17 19:52:44 +0000496
497 return (command);
498#else
Jari Aaltobb706242000-03-17 21:46:59 +0000499 last_command_exit_value = 2;
Jari Aaltocce855b1998-04-17 19:52:44 +0000500 return ((COMMAND *)NULL);
501#endif
502}
503
504COMMAND *
Jari Aalto726f6381996-08-26 18:22:31 +0000505make_bare_simple_command ()
506{
507 COMMAND *command;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000508 SIMPLE_COM *temp;
509
510 command = (COMMAND *)xmalloc (sizeof (COMMAND));
511 command->value.Simple = temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
Jari Aalto726f6381996-08-26 18:22:31 +0000512
513 temp->flags = 0;
514 temp->line = line_number;
515 temp->words = (WORD_LIST *)NULL;
516 temp->redirects = (REDIRECT *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000517
Jari Aalto726f6381996-08-26 18:22:31 +0000518 command->type = cm_simple;
519 command->redirects = (REDIRECT *)NULL;
520 command->flags = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000521
Jari Aalto726f6381996-08-26 18:22:31 +0000522 return (command);
523}
524
525/* Return a command which is the connection of the word or redirection
526 in ELEMENT, and the command * or NULL in COMMAND. */
527COMMAND *
528make_simple_command (element, command)
529 ELEMENT element;
530 COMMAND *command;
531{
532 /* If we are starting from scratch, then make the initial command
533 structure. Also note that we have to fill in all the slots, since
534 malloc doesn't return zeroed space. */
Chet Ramey00018032011-11-21 20:51:19 -0500535 if (command == 0)
536 {
537 command = make_bare_simple_command ();
538 parser_state |= PST_REDIRLIST;
539 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000540
Jari Aalto726f6381996-08-26 18:22:31 +0000541 if (element.word)
Chet Ramey00018032011-11-21 20:51:19 -0500542 {
543 command->value.Simple->words = make_word_list (element.word, command->value.Simple->words);
544 parser_state &= ~PST_REDIRLIST;
545 }
Jari Aaltob80f6442004-07-27 13:29:18 +0000546 else if (element.redirect)
Jari Aalto726f6381996-08-26 18:22:31 +0000547 {
548 REDIRECT *r = element.redirect;
549 /* Due to the way <> is implemented, there may be more than a single
550 redirection in element.redirect. We just follow the chain as far
551 as it goes, and hook onto the end. */
552 while (r->next)
553 r = r->next;
554 r->next = command->value.Simple->redirects;
555 command->value.Simple->redirects = element.redirect;
556 }
Chet Ramey00018032011-11-21 20:51:19 -0500557
Jari Aalto726f6381996-08-26 18:22:31 +0000558 return (command);
559}
560
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000561/* Because we are Bourne compatible, we read the input for this
562 << or <<- redirection now, from wherever input is coming from.
563 We store the input read into a WORD_DESC. Replace the text of
564 the redirectee.word with the new input text. If <<- is on,
565 then remove leading TABS from each line. */
Jari Aalto726f6381996-08-26 18:22:31 +0000566void
Jari Aalto31859422009-01-12 13:36:28 +0000567make_here_document (temp, lineno)
Jari Aalto726f6381996-08-26 18:22:31 +0000568 REDIRECT *temp;
Jari Aalto31859422009-01-12 13:36:28 +0000569 int lineno;
Jari Aalto726f6381996-08-26 18:22:31 +0000570{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000571 int kill_leading, redir_len;
572 char *redir_word, *document, *full_line;
573 int document_index, document_size, delim_unquoted;
Jari Aalto726f6381996-08-26 18:22:31 +0000574
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000575 if (temp->instruction != r_deblank_reading_until &&
576 temp->instruction != r_reading_until)
Jari Aalto726f6381996-08-26 18:22:31 +0000577 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000578 internal_error (_("make_here_document: bad instruction type %d"), temp->instruction);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000579 return;
Jari Aalto726f6381996-08-26 18:22:31 +0000580 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000581
582 kill_leading = temp->instruction == r_deblank_reading_until;
583
584 document = (char *)NULL;
585 document_index = document_size = 0;
586
587 /* Quote removal is the only expansion performed on the delimiter
588 for here documents, making it an extremely special case. */
589 redir_word = string_quote_removal (temp->redirectee.filename->word, 0);
590
591 /* redirection_expand will return NULL if the expansion results in
592 multiple words or no words. Check for that here, and just abort
593 this here document if it does. */
594 if (redir_word)
595 redir_len = strlen (redir_word);
596 else
597 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000598 temp->here_doc_eof = (char *)xmalloc (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000599 temp->here_doc_eof[0] = '\0';
600 goto document_done;
601 }
602
603 free (temp->redirectee.filename->word);
604 temp->here_doc_eof = redir_word;
605
606 /* Read lines from wherever lines are coming from.
607 For each line read, if kill_leading, then kill the
608 leading tab characters.
609 If the line matches redir_word exactly, then we have
610 manufactured the document. Otherwise, add the line to the
611 list of lines in the document. */
612
613 /* If the here-document delimiter was quoted, the lines should
614 be read verbatim from the input. If it was not quoted, we
615 need to perform backslash-quoted newline removal. */
616 delim_unquoted = (temp->redirectee.filename->flags & W_QUOTED) == 0;
617 while (full_line = read_secondary_line (delim_unquoted))
618 {
619 register char *line;
620 int len;
621
622 line = full_line;
623 line_number++;
624
Jari Aalto06285672006-10-10 14:15:34 +0000625 /* If set -v is in effect, echo the line read. read_secondary_line/
626 read_a_line leaves the newline at the end, so don't print another. */
627 if (echo_input_at_read)
628 fprintf (stderr, "%s", line);
629
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000630 if (kill_leading && *line)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000631 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000632 /* Hack: To be compatible with some Bourne shells, we
633 check the word before stripping the whitespace. This
634 is a hack, though. */
635 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
636 goto document_done;
637
638 while (*line == '\t')
639 line++;
640 }
641
642 if (*line == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000643 continue;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000644
645 if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n')
646 goto document_done;
647
648 len = strlen (line);
649 if (len + document_index >= document_size)
650 {
Jari Aaltobc4cd231998-07-23 14:37:54 +0000651 document_size = document_size ? 2 * (document_size + len) : len + 2;
Jari Aaltof73dda02001-11-13 17:56:06 +0000652 document = (char *)xrealloc (document, document_size);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000653 }
654
655 /* len is guaranteed to be > 0 because of the check for line
656 being an empty string before the call to strlen. */
657 FASTCOPY (line, document + document_index, len);
658 document_index += len;
659 }
660
Jari Aalto31859422009-01-12 13:36:28 +0000661 if (full_line == 0)
662 internal_warning (_("here-document at line %d delimited by end-of-file (wanted `%s')"), lineno, redir_word);
663
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000664document_done:
665 if (document)
666 document[document_index] = '\0';
667 else
668 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000669 document = (char *)xmalloc (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000670 document[0] = '\0';
671 }
672 temp->redirectee.filename->word = document;
Jari Aalto726f6381996-08-26 18:22:31 +0000673}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000674
675/* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
Jari Aalto726f6381996-08-26 18:22:31 +0000676 INSTRUCTION is the instruction type, SOURCE is a file descriptor,
677 and DEST is a file descriptor or a WORD_DESC *. */
678REDIRECT *
Chet Ramey00018032011-11-21 20:51:19 -0500679make_redirection (source, instruction, dest_and_filename, flags)
680 REDIRECTEE source;
Jari Aalto726f6381996-08-26 18:22:31 +0000681 enum r_instruction instruction;
682 REDIRECTEE dest_and_filename;
Chet Ramey00018032011-11-21 20:51:19 -0500683 int flags;
Jari Aalto726f6381996-08-26 18:22:31 +0000684{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000685 REDIRECT *temp;
686 WORD_DESC *w;
687 int wlen;
688 intmax_t lfd;
689
690 temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
Jari Aalto726f6381996-08-26 18:22:31 +0000691
692 /* First do the common cases. */
693 temp->redirector = source;
694 temp->redirectee = dest_and_filename;
Chet Rameya417a512014-10-02 22:50:19 -0400695 temp->here_doc_eof = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000696 temp->instruction = instruction;
697 temp->flags = 0;
Chet Ramey00018032011-11-21 20:51:19 -0500698 temp->rflags = flags;
Jari Aalto726f6381996-08-26 18:22:31 +0000699 temp->next = (REDIRECT *)NULL;
700
701 switch (instruction)
702 {
703
Jari Aaltod166f041997-06-05 14:59:13 +0000704 case r_output_direction: /* >foo */
705 case r_output_force: /* >| foo */
Jari Aalto31859422009-01-12 13:36:28 +0000706 case r_err_and_out: /* &>filename */
Jari Aalto726f6381996-08-26 18:22:31 +0000707 temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
708 break;
709
Jari Aaltod166f041997-06-05 14:59:13 +0000710 case r_appending_to: /* >>foo */
Jari Aalto31859422009-01-12 13:36:28 +0000711 case r_append_err_and_out: /* &>> filename */
Jari Aalto726f6381996-08-26 18:22:31 +0000712 temp->flags = O_APPEND | O_WRONLY | O_CREAT;
713 break;
714
Jari Aaltod166f041997-06-05 14:59:13 +0000715 case r_input_direction: /* <foo */
716 case r_inputa_direction: /* foo & makes this. */
717 temp->flags = O_RDONLY;
Jari Aalto726f6381996-08-26 18:22:31 +0000718 break;
719
Jari Aaltod166f041997-06-05 14:59:13 +0000720 case r_input_output: /* <>foo */
721 temp->flags = O_RDWR | O_CREAT;
722 break;
723
724 case r_deblank_reading_until: /* <<-foo */
725 case r_reading_until: /* << foo */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000726 case r_reading_string: /* <<< foo */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000727 case r_close_this: /* <&- */
Jari Aalto726f6381996-08-26 18:22:31 +0000728 case r_duplicating_input: /* 1<&2 */
729 case r_duplicating_output: /* 1>&2 */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000730 break;
731
732 /* the parser doesn't pass these. */
733 case r_move_input: /* 1<&2- */
734 case r_move_output: /* 1>&2- */
735 case r_move_input_word: /* 1<&$foo- */
736 case r_move_output_word: /* 1>&$foo- */
737 break;
738
739 /* The way the lexer works we have to do this here. */
Jari Aalto726f6381996-08-26 18:22:31 +0000740 case r_duplicating_input_word: /* 1<&$foo */
741 case r_duplicating_output_word: /* 1>&$foo */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000742 w = dest_and_filename.filename;
743 wlen = strlen (w->word) - 1;
744 if (w->word[wlen] == '-') /* Yuck */
745 {
746 w->word[wlen] = '\0';
747 if (all_digits (w->word) && legal_number (w->word, &lfd) && lfd == (int)lfd)
748 {
749 dispose_word (w);
750 temp->instruction = (instruction == r_duplicating_input_word) ? r_move_input : r_move_output;
751 temp->redirectee.dest = lfd;
752 }
753 else
754 temp->instruction = (instruction == r_duplicating_input_word) ? r_move_input_word : r_move_output_word;
755 }
756
Jari Aalto726f6381996-08-26 18:22:31 +0000757 break;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000758
Jari Aalto726f6381996-08-26 18:22:31 +0000759 default:
Jari Aaltob80f6442004-07-27 13:29:18 +0000760 programming_error (_("make_redirection: redirection instruction `%d' out of range"), instruction);
Jari Aalto726f6381996-08-26 18:22:31 +0000761 abort ();
762 break;
763 }
764 return (temp);
765}
766
767COMMAND *
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000768make_function_def (name, command, lineno, lstart)
Jari Aalto726f6381996-08-26 18:22:31 +0000769 WORD_DESC *name;
770 COMMAND *command;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000771 int lineno, lstart;
Jari Aalto726f6381996-08-26 18:22:31 +0000772{
773 FUNCTION_DEF *temp;
Jari Aaltob80f6442004-07-27 13:29:18 +0000774#if defined (ARRAY_VARS)
775 SHELL_VAR *bash_source_v;
776 ARRAY *bash_source_a;
Jari Aaltob80f6442004-07-27 13:29:18 +0000777#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000778
779 temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
780 temp->command = command;
781 temp->name = name;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000782 temp->line = lineno;
Jari Aaltocce855b1998-04-17 19:52:44 +0000783 temp->flags = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000784 command->line = lstart;
Jari Aaltob80f6442004-07-27 13:29:18 +0000785
786 /* Information used primarily for debugging. */
787 temp->source_file = 0;
788#if defined (ARRAY_VARS)
789 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
790 if (bash_source_a && array_num_elements (bash_source_a) > 0)
791 temp->source_file = array_reference (bash_source_a, 0);
792#endif
Jari Aalto31859422009-01-12 13:36:28 +0000793#if defined (DEBUGGER)
Jari Aaltob80f6442004-07-27 13:29:18 +0000794 bind_function_def (name->word, temp);
Jari Aalto31859422009-01-12 13:36:28 +0000795#endif
Jari Aaltob80f6442004-07-27 13:29:18 +0000796
Chet Rameyac50fba2014-02-26 09:36:43 -0500797 temp->source_file = temp->source_file ? savestring (temp->source_file) : 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000798 return (make_command (cm_function_def, (SIMPLE_COM *)temp));
799}
800
Jari Aaltobb706242000-03-17 21:46:59 +0000801COMMAND *
802make_subshell_command (command)
803 COMMAND *command;
804{
805 SUBSHELL_COM *temp;
806
807 temp = (SUBSHELL_COM *)xmalloc (sizeof (SUBSHELL_COM));
808 temp->command = command;
809 temp->flags = CMD_WANT_SUBSHELL;
810 return (make_command (cm_subshell, (SIMPLE_COM *)temp));
811}
812
Jari Aalto31859422009-01-12 13:36:28 +0000813COMMAND *
814make_coproc_command (name, command)
815 char *name;
816 COMMAND *command;
817{
818 COPROC_COM *temp;
819
820 temp = (COPROC_COM *)xmalloc (sizeof (COPROC_COM));
821 temp->name = savestring (name);
822 temp->command = command;
823 temp->flags = CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
824 return (make_command (cm_coproc, (SIMPLE_COM *)temp));
825}
826
Jari Aalto726f6381996-08-26 18:22:31 +0000827/* Reverse the word list and redirection list in the simple command
828 has just been parsed. It seems simpler to do this here the one
829 time then by any other method that I can think of. */
830COMMAND *
831clean_simple_command (command)
832 COMMAND *command;
833{
834 if (command->type != cm_simple)
Jari Aaltob72432f1999-02-19 17:11:39 +0000835 command_error ("clean_simple_command", CMDERR_BADTYPE, command->type, 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000836 else
837 {
838 command->value.Simple->words =
839 REVERSE_LIST (command->value.Simple->words, WORD_LIST *);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000840 command->value.Simple->redirects =
Jari Aalto726f6381996-08-26 18:22:31 +0000841 REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
842 }
843
Chet Ramey00018032011-11-21 20:51:19 -0500844 parser_state &= ~PST_REDIRLIST;
Jari Aalto726f6381996-08-26 18:22:31 +0000845 return (command);
846}
847
Jari Aalto726f6381996-08-26 18:22:31 +0000848/* The Yacc grammar productions have a problem, in that they take a
849 list followed by an ampersand (`&') and do a simple command connection,
850 making the entire list effectively asynchronous, instead of just
851 the last command. This means that when the list is executed, all
852 the commands have stdin set to /dev/null when job control is not
853 active, instead of just the last. This is wrong, and needs fixing
854 up. This function takes the `&' and applies it to the last command
855 in the list. This is done only for lists connected by `;'; it makes
856 `;' bind `tighter' than `&'. */
857COMMAND *
858connect_async_list (command, command2, connector)
859 COMMAND *command, *command2;
860 int connector;
861{
862 COMMAND *t, *t1, *t2;
863
864 t1 = command;
865 t = command->value.Connection->second;
866
867 if (!t || (command->flags & CMD_WANT_SUBSHELL) ||
868 command->value.Connection->connector != ';')
869 {
870 t = command_connect (command, command2, connector);
871 return t;
872 }
873
874 /* This is just defensive programming. The Yacc precedence rules
875 will generally hand this function a command where t points directly
876 to the command we want (e.g. given a ; b ; c ; d &, t1 will point
877 to the `a ; b ; c' list and t will be the `d'). We only want to do
878 this if the list is not being executed as a unit in the background
879 with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's
880 the only way to tell. */
881 while (((t->flags & CMD_WANT_SUBSHELL) == 0) && t->type == cm_connection &&
882 t->value.Connection->connector == ';')
883 {
884 t1 = t;
885 t = t->value.Connection->second;
886 }
887 /* Now we have t pointing to the last command in the list, and
888 t1->value.Connection->second == t. */
889 t2 = command_connect (t, command2, connector);
890 t1->value.Connection->second = t2;
891 return command;
892}