]>
Commit | Line | Data |
---|---|---|
ccc6cda3 JA |
1 | /* make_cmd.c -- Functions for making instances of the various |
2 | parser constructs. */ | |
726f6388 | 3 | |
3185942a | 4 | /* Copyright (C) 1989-2009 Free Software Foundation, Inc. |
726f6388 | 5 | |
3185942a | 6 | This file is part of GNU Bash, the Bourne Again SHell. |
726f6388 | 7 | |
3185942a JA |
8 | 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. | |
726f6388 | 12 | |
3185942a JA |
13 | 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. | |
726f6388 | 17 | |
3185942a JA |
18 | 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 | */ | |
726f6388 | 21 | |
ccc6cda3 JA |
22 | #include "config.h" |
23 | ||
726f6388 JA |
24 | #include <stdio.h> |
25 | #include "bashtypes.h" | |
b80f6443 | 26 | #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H) |
cce855bc JA |
27 | # include <sys/file.h> |
28 | #endif | |
726f6388 JA |
29 | #include "filecntl.h" |
30 | #include "bashansi.h" | |
ccc6cda3 JA |
31 | #if defined (HAVE_UNISTD_H) |
32 | # include <unistd.h> | |
33 | #endif | |
34 | ||
b80f6443 JA |
35 | #include "bashintl.h" |
36 | ||
0001803f | 37 | #include "parser.h" |
28ef6c31 | 38 | #include "syntax.h" |
726f6388 JA |
39 | #include "command.h" |
40 | #include "general.h" | |
41 | #include "error.h" | |
42 | #include "flags.h" | |
43 | #include "make_cmd.h" | |
7117c2d2 | 44 | #include "dispose_cmd.h" |
ccc6cda3 | 45 | #include "variables.h" |
726f6388 JA |
46 | #include "subst.h" |
47 | #include "input.h" | |
7117c2d2 | 48 | #include "ocache.h" |
726f6388 JA |
49 | #include "externs.h" |
50 | ||
51 | #if defined (JOB_CONTROL) | |
52 | #include "jobs.h" | |
53 | #endif | |
54 | ||
7117c2d2 JA |
55 | #include "shmbutil.h" |
56 | ||
0001803f | 57 | extern int line_number, current_command_line_count, parser_state; |
bb70624e | 58 | extern int last_command_exit_value; |
a0c0a00f CR |
59 | extern int shell_initialized; |
60 | ||
61 | int here_doc_first_line = 0; | |
bb70624e | 62 | |
7117c2d2 JA |
63 | /* Object caching */ |
64 | sh_obj_cache_t wdcache = {0, 0, 0}; | |
65 | sh_obj_cache_t wlcache = {0, 0, 0}; | |
66 | ||
a0c0a00f CR |
67 | #define WDCACHESIZE 128 |
68 | #define WLCACHESIZE 128 | |
7117c2d2 | 69 | |
b80f6443 | 70 | static COMMAND *make_for_or_select __P((enum command_type, WORD_DESC *, WORD_LIST *, COMMAND *, int)); |
f73dda09 JA |
71 | #if defined (ARITH_FOR_COMMAND) |
72 | static WORD_LIST *make_arith_for_expr __P((char *)); | |
73 | #endif | |
74 | static COMMAND *make_until_or_while __P((enum command_type, COMMAND *, COMMAND *)); | |
726f6388 | 75 | |
7117c2d2 JA |
76 | void |
77 | cmd_init () | |
78 | { | |
79 | ocache_create (wdcache, WORD_DESC, WDCACHESIZE); | |
80 | ocache_create (wlcache, WORD_LIST, WLCACHESIZE); | |
81 | } | |
82 | ||
95732b49 JA |
83 | WORD_DESC * |
84 | alloc_word_desc () | |
85 | { | |
86 | WORD_DESC *temp; | |
87 | ||
88 | ocache_alloc (wdcache, WORD_DESC, temp); | |
89 | temp->flags = 0; | |
90 | temp->word = 0; | |
91 | return temp; | |
92 | } | |
93 | ||
726f6388 | 94 | WORD_DESC * |
ccc6cda3 | 95 | make_bare_word (string) |
f73dda09 | 96 | const char *string; |
726f6388 JA |
97 | { |
98 | WORD_DESC *temp; | |
95732b49 JA |
99 | |
100 | temp = alloc_word_desc (); | |
7117c2d2 | 101 | |
ccc6cda3 JA |
102 | if (*string) |
103 | temp->word = savestring (string); | |
104 | else | |
726f6388 | 105 | { |
f73dda09 | 106 | temp->word = (char *)xmalloc (1); |
ccc6cda3 | 107 | temp->word[0] = '\0'; |
726f6388 | 108 | } |
ccc6cda3 | 109 | |
726f6388 JA |
110 | return (temp); |
111 | } | |
112 | ||
ccc6cda3 JA |
113 | WORD_DESC * |
114 | make_word_flags (w, string) | |
115 | WORD_DESC *w; | |
f73dda09 | 116 | const char *string; |
ccc6cda3 | 117 | { |
7117c2d2 JA |
118 | register int i; |
119 | size_t slen; | |
120 | DECLARE_MBSTATE; | |
ccc6cda3 | 121 | |
7117c2d2 JA |
122 | i = 0; |
123 | slen = strlen (string); | |
124 | while (i < slen) | |
125 | { | |
126 | switch (string[i]) | |
127 | { | |
ccc6cda3 JA |
128 | case '$': |
129 | w->flags |= W_HASDOLLAR; | |
130 | break; | |
131 | case '\\': | |
132 | break; /* continue the loop */ | |
133 | case '\'': | |
134 | case '`': | |
135 | case '"': | |
136 | w->flags |= W_QUOTED; | |
137 | break; | |
7117c2d2 JA |
138 | } |
139 | ||
140 | ADVANCE_CHAR (string, slen, i); | |
141 | } | |
142 | ||
ccc6cda3 JA |
143 | return (w); |
144 | } | |
145 | ||
146 | WORD_DESC * | |
147 | make_word (string) | |
f73dda09 | 148 | const char *string; |
ccc6cda3 JA |
149 | { |
150 | WORD_DESC *temp; | |
151 | ||
152 | temp = make_bare_word (string); | |
153 | return (make_word_flags (temp, string)); | |
154 | } | |
155 | ||
726f6388 JA |
156 | WORD_DESC * |
157 | make_word_from_token (token) | |
158 | int token; | |
159 | { | |
160 | char tokenizer[2]; | |
161 | ||
162 | tokenizer[0] = token; | |
163 | tokenizer[1] = '\0'; | |
164 | ||
165 | return (make_word (tokenizer)); | |
166 | } | |
167 | ||
168 | WORD_LIST * | |
f73dda09 | 169 | make_word_list (word, wlink) |
726f6388 | 170 | WORD_DESC *word; |
f73dda09 | 171 | WORD_LIST *wlink; |
726f6388 JA |
172 | { |
173 | WORD_LIST *temp; | |
174 | ||
7117c2d2 | 175 | ocache_alloc (wlcache, WORD_LIST, temp); |
95732b49 | 176 | |
726f6388 | 177 | temp->word = word; |
f73dda09 | 178 | temp->next = wlink; |
726f6388 JA |
179 | return (temp); |
180 | } | |
181 | ||
726f6388 JA |
182 | COMMAND * |
183 | make_command (type, pointer) | |
184 | enum command_type type; | |
185 | SIMPLE_COM *pointer; | |
186 | { | |
187 | COMMAND *temp; | |
188 | ||
189 | temp = (COMMAND *)xmalloc (sizeof (COMMAND)); | |
190 | temp->type = type; | |
191 | temp->value.Simple = pointer; | |
ccc6cda3 | 192 | temp->value.Simple->flags = temp->flags = 0; |
726f6388 JA |
193 | temp->redirects = (REDIRECT *)NULL; |
194 | return (temp); | |
195 | } | |
196 | ||
197 | COMMAND * | |
198 | command_connect (com1, com2, connector) | |
199 | COMMAND *com1, *com2; | |
200 | int connector; | |
201 | { | |
202 | CONNECTION *temp; | |
203 | ||
204 | temp = (CONNECTION *)xmalloc (sizeof (CONNECTION)); | |
205 | temp->connector = connector; | |
206 | temp->first = com1; | |
207 | temp->second = com2; | |
208 | return (make_command (cm_connection, (SIMPLE_COM *)temp)); | |
209 | } | |
210 | ||
ccc6cda3 | 211 | static COMMAND * |
b80f6443 | 212 | make_for_or_select (type, name, map_list, action, lineno) |
ccc6cda3 | 213 | enum command_type type; |
726f6388 JA |
214 | WORD_DESC *name; |
215 | WORD_LIST *map_list; | |
216 | COMMAND *action; | |
b80f6443 | 217 | int lineno; |
726f6388 | 218 | { |
ccc6cda3 | 219 | FOR_COM *temp; |
726f6388 | 220 | |
ccc6cda3 | 221 | temp = (FOR_COM *)xmalloc (sizeof (FOR_COM)); |
726f6388 JA |
222 | temp->flags = 0; |
223 | temp->name = name; | |
b80f6443 | 224 | temp->line = lineno; |
726f6388 JA |
225 | temp->map_list = map_list; |
226 | temp->action = action; | |
ccc6cda3 | 227 | return (make_command (type, (SIMPLE_COM *)temp)); |
726f6388 JA |
228 | } |
229 | ||
726f6388 | 230 | COMMAND * |
b80f6443 | 231 | make_for_command (name, map_list, action, lineno) |
726f6388 JA |
232 | WORD_DESC *name; |
233 | WORD_LIST *map_list; | |
234 | COMMAND *action; | |
b80f6443 | 235 | int lineno; |
726f6388 | 236 | { |
b80f6443 | 237 | return (make_for_or_select (cm_for, name, map_list, action, lineno)); |
726f6388 | 238 | } |
ccc6cda3 JA |
239 | |
240 | COMMAND * | |
b80f6443 | 241 | make_select_command (name, map_list, action, lineno) |
ccc6cda3 JA |
242 | WORD_DESC *name; |
243 | WORD_LIST *map_list; | |
244 | COMMAND *action; | |
b80f6443 | 245 | int lineno; |
ccc6cda3 JA |
246 | { |
247 | #if defined (SELECT_COMMAND) | |
b80f6443 | 248 | return (make_for_or_select (cm_select, name, map_list, action, lineno)); |
bb70624e JA |
249 | #else |
250 | last_command_exit_value = 2; | |
251 | return ((COMMAND *)NULL); | |
726f6388 | 252 | #endif |
ccc6cda3 | 253 | } |
726f6388 | 254 | |
bb70624e JA |
255 | #if defined (ARITH_FOR_COMMAND) |
256 | static WORD_LIST * | |
257 | make_arith_for_expr (s) | |
258 | char *s; | |
259 | { | |
260 | WORD_LIST *result; | |
b80f6443 | 261 | WORD_DESC *wd; |
bb70624e JA |
262 | |
263 | if (s == 0 || *s == '\0') | |
264 | return ((WORD_LIST *)NULL); | |
b80f6443 | 265 | wd = make_word (s); |
95732b49 | 266 | wd->flags |= W_NOGLOB|W_NOSPLIT|W_QUOTED|W_DQUOTE; /* no word splitting or globbing */ |
a0c0a00f CR |
267 | #if defined (PROCESS_SUBSTITUTION) |
268 | wd->flags |= W_NOPROCSUB; /* no process substitution */ | |
269 | #endif | |
b80f6443 | 270 | result = make_word_list (wd, (WORD_LIST *)NULL); |
bb70624e JA |
271 | return result; |
272 | } | |
273 | #endif | |
274 | ||
b80f6443 JA |
275 | /* Note that this function calls dispose_words on EXPRS, since it doesn't |
276 | use the word list directly. We free it here rather than at the caller | |
277 | because no other function in this file requires that the caller free | |
278 | any arguments. */ | |
bb70624e JA |
279 | COMMAND * |
280 | make_arith_for_command (exprs, action, lineno) | |
281 | WORD_LIST *exprs; | |
282 | COMMAND *action; | |
283 | int lineno; | |
284 | { | |
285 | #if defined (ARITH_FOR_COMMAND) | |
286 | ARITH_FOR_COM *temp; | |
287 | WORD_LIST *init, *test, *step; | |
288 | char *s, *t, *start; | |
ac50fbac | 289 | int nsemi, i; |
bb70624e JA |
290 | |
291 | init = test = step = (WORD_LIST *)NULL; | |
292 | /* Parse the string into the three component sub-expressions. */ | |
293 | start = t = s = exprs->word->word; | |
294 | for (nsemi = 0; ;) | |
295 | { | |
296 | /* skip whitespace at the start of each sub-expression. */ | |
297 | while (whitespace (*s)) | |
298 | s++; | |
299 | start = s; | |
300 | /* skip to the semicolon or EOS */ | |
a0c0a00f | 301 | i = skip_to_delim (start, 0, ";", SD_NOJMP|SD_NOPROCSUB); |
ac50fbac | 302 | s = start + i; |
bb70624e | 303 | |
ac50fbac | 304 | t = (i > 0) ? substring (start, 0, i) : (char *)NULL; |
bb70624e JA |
305 | |
306 | nsemi++; | |
307 | switch (nsemi) | |
308 | { | |
309 | case 1: | |
310 | init = make_arith_for_expr (t); | |
311 | break; | |
312 | case 2: | |
313 | test = make_arith_for_expr (t); | |
314 | break; | |
315 | case 3: | |
316 | step = make_arith_for_expr (t); | |
317 | break; | |
318 | } | |
319 | ||
320 | FREE (t); | |
321 | if (*s == '\0') | |
28ef6c31 | 322 | break; |
bb70624e JA |
323 | s++; /* skip over semicolon */ |
324 | } | |
325 | ||
326 | if (nsemi != 3) | |
327 | { | |
328 | if (nsemi < 3) | |
b80f6443 | 329 | parser_error (lineno, _("syntax error: arithmetic expression required")); |
bb70624e | 330 | else |
b80f6443 JA |
331 | parser_error (lineno, _("syntax error: `;' unexpected")); |
332 | parser_error (lineno, _("syntax error: `((%s))'"), exprs->word->word); | |
ac50fbac CR |
333 | free (init); |
334 | free (test); | |
335 | free (step); | |
bb70624e JA |
336 | last_command_exit_value = 2; |
337 | return ((COMMAND *)NULL); | |
338 | } | |
339 | ||
340 | temp = (ARITH_FOR_COM *)xmalloc (sizeof (ARITH_FOR_COM)); | |
341 | temp->flags = 0; | |
342 | temp->line = lineno; | |
343 | temp->init = init ? init : make_arith_for_expr ("1"); | |
344 | temp->test = test ? test : make_arith_for_expr ("1"); | |
345 | temp->step = step ? step : make_arith_for_expr ("1"); | |
346 | temp->action = action; | |
347 | ||
b80f6443 | 348 | dispose_words (exprs); |
bb70624e JA |
349 | return (make_command (cm_arith_for, (SIMPLE_COM *)temp)); |
350 | #else | |
b80f6443 | 351 | dispose_words (exprs); |
bb70624e JA |
352 | last_command_exit_value = 2; |
353 | return ((COMMAND *)NULL); | |
354 | #endif /* ARITH_FOR_COMMAND */ | |
355 | } | |
356 | ||
726f6388 JA |
357 | COMMAND * |
358 | make_group_command (command) | |
359 | COMMAND *command; | |
360 | { | |
ccc6cda3 | 361 | GROUP_COM *temp; |
726f6388 | 362 | |
ccc6cda3 | 363 | temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM)); |
726f6388 JA |
364 | temp->command = command; |
365 | return (make_command (cm_group, (SIMPLE_COM *)temp)); | |
366 | } | |
367 | ||
368 | COMMAND * | |
b80f6443 | 369 | make_case_command (word, clauses, lineno) |
726f6388 JA |
370 | WORD_DESC *word; |
371 | PATTERN_LIST *clauses; | |
95732b49 | 372 | int lineno; |
726f6388 JA |
373 | { |
374 | CASE_COM *temp; | |
375 | ||
376 | temp = (CASE_COM *)xmalloc (sizeof (CASE_COM)); | |
377 | temp->flags = 0; | |
b80f6443 | 378 | temp->line = lineno; |
726f6388 JA |
379 | temp->word = word; |
380 | temp->clauses = REVERSE_LIST (clauses, PATTERN_LIST *); | |
381 | return (make_command (cm_case, (SIMPLE_COM *)temp)); | |
382 | } | |
383 | ||
384 | PATTERN_LIST * | |
385 | make_pattern_list (patterns, action) | |
386 | WORD_LIST *patterns; | |
387 | COMMAND *action; | |
388 | { | |
389 | PATTERN_LIST *temp; | |
390 | ||
391 | temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST)); | |
392 | temp->patterns = REVERSE_LIST (patterns, WORD_LIST *); | |
393 | temp->action = action; | |
394 | temp->next = NULL; | |
95732b49 | 395 | temp->flags = 0; |
726f6388 JA |
396 | return (temp); |
397 | } | |
398 | ||
399 | COMMAND * | |
400 | make_if_command (test, true_case, false_case) | |
401 | COMMAND *test, *true_case, *false_case; | |
402 | { | |
403 | IF_COM *temp; | |
404 | ||
405 | temp = (IF_COM *)xmalloc (sizeof (IF_COM)); | |
406 | temp->flags = 0; | |
407 | temp->test = test; | |
408 | temp->true_case = true_case; | |
409 | temp->false_case = false_case; | |
410 | return (make_command (cm_if, (SIMPLE_COM *)temp)); | |
411 | } | |
412 | ||
413 | static COMMAND * | |
ccc6cda3 | 414 | make_until_or_while (which, test, action) |
726f6388 | 415 | enum command_type which; |
ccc6cda3 | 416 | COMMAND *test, *action; |
726f6388 JA |
417 | { |
418 | WHILE_COM *temp; | |
419 | ||
420 | temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM)); | |
421 | temp->flags = 0; | |
422 | temp->test = test; | |
423 | temp->action = action; | |
424 | return (make_command (which, (SIMPLE_COM *)temp)); | |
425 | } | |
426 | ||
427 | COMMAND * | |
428 | make_while_command (test, action) | |
429 | COMMAND *test, *action; | |
430 | { | |
ccc6cda3 | 431 | return (make_until_or_while (cm_while, test, action)); |
726f6388 JA |
432 | } |
433 | ||
434 | COMMAND * | |
435 | make_until_command (test, action) | |
436 | COMMAND *test, *action; | |
437 | { | |
ccc6cda3 | 438 | return (make_until_or_while (cm_until, test, action)); |
726f6388 JA |
439 | } |
440 | ||
cce855bc JA |
441 | COMMAND * |
442 | make_arith_command (exp) | |
443 | WORD_LIST *exp; | |
444 | { | |
445 | #if defined (DPAREN_ARITHMETIC) | |
446 | COMMAND *command; | |
447 | ARITH_COM *temp; | |
448 | ||
449 | command = (COMMAND *)xmalloc (sizeof (COMMAND)); | |
450 | command->value.Arith = temp = (ARITH_COM *)xmalloc (sizeof (ARITH_COM)); | |
451 | ||
452 | temp->flags = 0; | |
453 | temp->line = line_number; | |
454 | temp->exp = exp; | |
455 | ||
456 | command->type = cm_arith; | |
457 | command->redirects = (REDIRECT *)NULL; | |
458 | command->flags = 0; | |
459 | ||
460 | return (command); | |
461 | #else | |
bb70624e | 462 | last_command_exit_value = 2; |
cce855bc JA |
463 | return ((COMMAND *)NULL); |
464 | #endif | |
465 | } | |
466 | ||
467 | #if defined (COND_COMMAND) | |
468 | struct cond_com * | |
469 | make_cond_node (type, op, left, right) | |
470 | int type; | |
471 | WORD_DESC *op; | |
472 | struct cond_com *left, *right; | |
473 | { | |
474 | COND_COM *temp; | |
475 | ||
476 | temp = (COND_COM *)xmalloc (sizeof (COND_COM)); | |
477 | temp->flags = 0; | |
478 | temp->line = line_number; | |
479 | temp->type = type; | |
480 | temp->op = op; | |
481 | temp->left = left; | |
482 | temp->right = right; | |
483 | ||
484 | return (temp); | |
485 | } | |
486 | #endif | |
487 | ||
488 | COMMAND * | |
489 | make_cond_command (cond_node) | |
490 | COND_COM *cond_node; | |
491 | { | |
492 | #if defined (COND_COMMAND) | |
493 | COMMAND *command; | |
494 | ||
495 | command = (COMMAND *)xmalloc (sizeof (COMMAND)); | |
496 | command->value.Cond = cond_node; | |
497 | ||
498 | command->type = cm_cond; | |
499 | command->redirects = (REDIRECT *)NULL; | |
500 | command->flags = 0; | |
b72432fd | 501 | command->line = cond_node ? cond_node->line : 0; |
cce855bc JA |
502 | |
503 | return (command); | |
504 | #else | |
bb70624e | 505 | last_command_exit_value = 2; |
cce855bc JA |
506 | return ((COMMAND *)NULL); |
507 | #endif | |
508 | } | |
509 | ||
726f6388 JA |
510 | COMMAND * |
511 | make_bare_simple_command () | |
512 | { | |
513 | COMMAND *command; | |
ccc6cda3 JA |
514 | SIMPLE_COM *temp; |
515 | ||
516 | command = (COMMAND *)xmalloc (sizeof (COMMAND)); | |
517 | command->value.Simple = temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM)); | |
726f6388 JA |
518 | |
519 | temp->flags = 0; | |
520 | temp->line = line_number; | |
521 | temp->words = (WORD_LIST *)NULL; | |
522 | temp->redirects = (REDIRECT *)NULL; | |
ccc6cda3 | 523 | |
726f6388 JA |
524 | command->type = cm_simple; |
525 | command->redirects = (REDIRECT *)NULL; | |
526 | command->flags = 0; | |
ccc6cda3 | 527 | |
726f6388 JA |
528 | return (command); |
529 | } | |
530 | ||
531 | /* Return a command which is the connection of the word or redirection | |
532 | in ELEMENT, and the command * or NULL in COMMAND. */ | |
533 | COMMAND * | |
534 | make_simple_command (element, command) | |
535 | ELEMENT element; | |
536 | COMMAND *command; | |
537 | { | |
538 | /* If we are starting from scratch, then make the initial command | |
539 | structure. Also note that we have to fill in all the slots, since | |
540 | malloc doesn't return zeroed space. */ | |
0001803f CR |
541 | if (command == 0) |
542 | { | |
543 | command = make_bare_simple_command (); | |
544 | parser_state |= PST_REDIRLIST; | |
545 | } | |
ccc6cda3 | 546 | |
726f6388 | 547 | if (element.word) |
0001803f CR |
548 | { |
549 | command->value.Simple->words = make_word_list (element.word, command->value.Simple->words); | |
550 | parser_state &= ~PST_REDIRLIST; | |
551 | } | |
b80f6443 | 552 | else if (element.redirect) |
726f6388 JA |
553 | { |
554 | REDIRECT *r = element.redirect; | |
555 | /* Due to the way <> is implemented, there may be more than a single | |
556 | redirection in element.redirect. We just follow the chain as far | |
557 | as it goes, and hook onto the end. */ | |
558 | while (r->next) | |
559 | r = r->next; | |
560 | r->next = command->value.Simple->redirects; | |
561 | command->value.Simple->redirects = element.redirect; | |
562 | } | |
0001803f | 563 | |
726f6388 JA |
564 | return (command); |
565 | } | |
566 | ||
ccc6cda3 JA |
567 | /* Because we are Bourne compatible, we read the input for this |
568 | << or <<- redirection now, from wherever input is coming from. | |
569 | We store the input read into a WORD_DESC. Replace the text of | |
570 | the redirectee.word with the new input text. If <<- is on, | |
571 | then remove leading TABS from each line. */ | |
726f6388 | 572 | void |
3185942a | 573 | make_here_document (temp, lineno) |
726f6388 | 574 | REDIRECT *temp; |
3185942a | 575 | int lineno; |
726f6388 | 576 | { |
ccc6cda3 JA |
577 | int kill_leading, redir_len; |
578 | char *redir_word, *document, *full_line; | |
579 | int document_index, document_size, delim_unquoted; | |
580 | ||
581 | if (temp->instruction != r_deblank_reading_until && | |
582 | temp->instruction != r_reading_until) | |
583 | { | |
b80f6443 | 584 | internal_error (_("make_here_document: bad instruction type %d"), temp->instruction); |
ccc6cda3 JA |
585 | return; |
586 | } | |
587 | ||
588 | kill_leading = temp->instruction == r_deblank_reading_until; | |
589 | ||
590 | document = (char *)NULL; | |
591 | document_index = document_size = 0; | |
592 | ||
593 | /* Quote removal is the only expansion performed on the delimiter | |
594 | for here documents, making it an extremely special case. */ | |
595 | redir_word = string_quote_removal (temp->redirectee.filename->word, 0); | |
596 | ||
597 | /* redirection_expand will return NULL if the expansion results in | |
598 | multiple words or no words. Check for that here, and just abort | |
599 | this here document if it does. */ | |
600 | if (redir_word) | |
601 | redir_len = strlen (redir_word); | |
602 | else | |
603 | { | |
f73dda09 | 604 | temp->here_doc_eof = (char *)xmalloc (1); |
ccc6cda3 JA |
605 | temp->here_doc_eof[0] = '\0'; |
606 | goto document_done; | |
607 | } | |
726f6388 | 608 | |
ccc6cda3 JA |
609 | free (temp->redirectee.filename->word); |
610 | temp->here_doc_eof = redir_word; | |
611 | ||
612 | /* Read lines from wherever lines are coming from. | |
613 | For each line read, if kill_leading, then kill the | |
614 | leading tab characters. | |
615 | If the line matches redir_word exactly, then we have | |
616 | manufactured the document. Otherwise, add the line to the | |
617 | list of lines in the document. */ | |
618 | ||
619 | /* If the here-document delimiter was quoted, the lines should | |
620 | be read verbatim from the input. If it was not quoted, we | |
621 | need to perform backslash-quoted newline removal. */ | |
622 | delim_unquoted = (temp->redirectee.filename->flags & W_QUOTED) == 0; | |
623 | while (full_line = read_secondary_line (delim_unquoted)) | |
726f6388 | 624 | { |
ccc6cda3 JA |
625 | register char *line; |
626 | int len; | |
627 | ||
a0c0a00f | 628 | here_doc_first_line = 0; |
ccc6cda3 JA |
629 | line = full_line; |
630 | line_number++; | |
631 | ||
0628567a JA |
632 | /* If set -v is in effect, echo the line read. read_secondary_line/ |
633 | read_a_line leaves the newline at the end, so don't print another. */ | |
634 | if (echo_input_at_read) | |
635 | fprintf (stderr, "%s", line); | |
636 | ||
ccc6cda3 | 637 | if (kill_leading && *line) |
28ef6c31 | 638 | { |
ccc6cda3 JA |
639 | /* Hack: To be compatible with some Bourne shells, we |
640 | check the word before stripping the whitespace. This | |
641 | is a hack, though. */ | |
642 | if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n') | |
643 | goto document_done; | |
644 | ||
645 | while (*line == '\t') | |
646 | line++; | |
647 | } | |
648 | ||
649 | if (*line == 0) | |
28ef6c31 | 650 | continue; |
ccc6cda3 JA |
651 | |
652 | if (STREQN (line, redir_word, redir_len) && line[redir_len] == '\n') | |
653 | goto document_done; | |
654 | ||
655 | len = strlen (line); | |
656 | if (len + document_index >= document_size) | |
726f6388 | 657 | { |
bc4cd23c | 658 | document_size = document_size ? 2 * (document_size + len) : len + 2; |
f73dda09 | 659 | document = (char *)xrealloc (document, document_size); |
726f6388 | 660 | } |
ccc6cda3 JA |
661 | |
662 | /* len is guaranteed to be > 0 because of the check for line | |
663 | being an empty string before the call to strlen. */ | |
664 | FASTCOPY (line, document + document_index, len); | |
665 | document_index += len; | |
666 | } | |
667 | ||
3185942a JA |
668 | if (full_line == 0) |
669 | internal_warning (_("here-document at line %d delimited by end-of-file (wanted `%s')"), lineno, redir_word); | |
670 | ||
ccc6cda3 JA |
671 | document_done: |
672 | if (document) | |
673 | document[document_index] = '\0'; | |
674 | else | |
675 | { | |
f73dda09 | 676 | document = (char *)xmalloc (1); |
ccc6cda3 | 677 | document[0] = '\0'; |
726f6388 | 678 | } |
ccc6cda3 | 679 | temp->redirectee.filename->word = document; |
a0c0a00f | 680 | here_doc_first_line = 0; |
726f6388 | 681 | } |
ccc6cda3 JA |
682 | |
683 | /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION. | |
726f6388 JA |
684 | INSTRUCTION is the instruction type, SOURCE is a file descriptor, |
685 | and DEST is a file descriptor or a WORD_DESC *. */ | |
686 | REDIRECT * | |
0001803f CR |
687 | make_redirection (source, instruction, dest_and_filename, flags) |
688 | REDIRECTEE source; | |
726f6388 JA |
689 | enum r_instruction instruction; |
690 | REDIRECTEE dest_and_filename; | |
0001803f | 691 | int flags; |
726f6388 | 692 | { |
7117c2d2 JA |
693 | REDIRECT *temp; |
694 | WORD_DESC *w; | |
695 | int wlen; | |
696 | intmax_t lfd; | |
697 | ||
698 | temp = (REDIRECT *)xmalloc (sizeof (REDIRECT)); | |
726f6388 JA |
699 | |
700 | /* First do the common cases. */ | |
701 | temp->redirector = source; | |
702 | temp->redirectee = dest_and_filename; | |
13db572a | 703 | temp->here_doc_eof = 0; |
726f6388 JA |
704 | temp->instruction = instruction; |
705 | temp->flags = 0; | |
0001803f | 706 | temp->rflags = flags; |
726f6388 JA |
707 | temp->next = (REDIRECT *)NULL; |
708 | ||
709 | switch (instruction) | |
710 | { | |
711 | ||
d166f048 JA |
712 | case r_output_direction: /* >foo */ |
713 | case r_output_force: /* >| foo */ | |
3185942a | 714 | case r_err_and_out: /* &>filename */ |
726f6388 JA |
715 | temp->flags = O_TRUNC | O_WRONLY | O_CREAT; |
716 | break; | |
717 | ||
d166f048 | 718 | case r_appending_to: /* >>foo */ |
3185942a | 719 | case r_append_err_and_out: /* &>> filename */ |
d166f048 | 720 | temp->flags = O_APPEND | O_WRONLY | O_CREAT; |
726f6388 JA |
721 | break; |
722 | ||
d166f048 JA |
723 | case r_input_direction: /* <foo */ |
724 | case r_inputa_direction: /* foo & makes this. */ | |
725 | temp->flags = O_RDONLY; | |
726f6388 JA |
726 | break; |
727 | ||
d166f048 JA |
728 | case r_input_output: /* <>foo */ |
729 | temp->flags = O_RDWR | O_CREAT; | |
726f6388 JA |
730 | break; |
731 | ||
d166f048 JA |
732 | case r_deblank_reading_until: /* <<-foo */ |
733 | case r_reading_until: /* << foo */ | |
7117c2d2 | 734 | case r_reading_string: /* <<< foo */ |
ccc6cda3 | 735 | case r_close_this: /* <&- */ |
726f6388 JA |
736 | case r_duplicating_input: /* 1<&2 */ |
737 | case r_duplicating_output: /* 1>&2 */ | |
7117c2d2 JA |
738 | break; |
739 | ||
740 | /* the parser doesn't pass these. */ | |
741 | case r_move_input: /* 1<&2- */ | |
742 | case r_move_output: /* 1>&2- */ | |
743 | case r_move_input_word: /* 1<&$foo- */ | |
744 | case r_move_output_word: /* 1>&$foo- */ | |
745 | break; | |
746 | ||
747 | /* The way the lexer works we have to do this here. */ | |
726f6388 JA |
748 | case r_duplicating_input_word: /* 1<&$foo */ |
749 | case r_duplicating_output_word: /* 1>&$foo */ | |
7117c2d2 JA |
750 | w = dest_and_filename.filename; |
751 | wlen = strlen (w->word) - 1; | |
752 | if (w->word[wlen] == '-') /* Yuck */ | |
753 | { | |
754 | w->word[wlen] = '\0'; | |
755 | if (all_digits (w->word) && legal_number (w->word, &lfd) && lfd == (int)lfd) | |
756 | { | |
757 | dispose_word (w); | |
758 | temp->instruction = (instruction == r_duplicating_input_word) ? r_move_input : r_move_output; | |
759 | temp->redirectee.dest = lfd; | |
760 | } | |
761 | else | |
762 | temp->instruction = (instruction == r_duplicating_input_word) ? r_move_input_word : r_move_output_word; | |
763 | } | |
764 | ||
726f6388 | 765 | break; |
ccc6cda3 | 766 | |
726f6388 | 767 | default: |
b80f6443 | 768 | programming_error (_("make_redirection: redirection instruction `%d' out of range"), instruction); |
726f6388 JA |
769 | abort (); |
770 | break; | |
771 | } | |
772 | return (temp); | |
773 | } | |
774 | ||
775 | COMMAND * | |
ccc6cda3 | 776 | make_function_def (name, command, lineno, lstart) |
726f6388 JA |
777 | WORD_DESC *name; |
778 | COMMAND *command; | |
ccc6cda3 | 779 | int lineno, lstart; |
726f6388 JA |
780 | { |
781 | FUNCTION_DEF *temp; | |
b80f6443 JA |
782 | #if defined (ARRAY_VARS) |
783 | SHELL_VAR *bash_source_v; | |
784 | ARRAY *bash_source_a; | |
b80f6443 | 785 | #endif |
726f6388 JA |
786 | |
787 | temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF)); | |
788 | temp->command = command; | |
789 | temp->name = name; | |
ccc6cda3 | 790 | temp->line = lineno; |
cce855bc | 791 | temp->flags = 0; |
ccc6cda3 | 792 | command->line = lstart; |
b80f6443 JA |
793 | |
794 | /* Information used primarily for debugging. */ | |
795 | temp->source_file = 0; | |
796 | #if defined (ARRAY_VARS) | |
797 | GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a); | |
798 | if (bash_source_a && array_num_elements (bash_source_a) > 0) | |
799 | temp->source_file = array_reference (bash_source_a, 0); | |
800 | #endif | |
a0c0a00f CR |
801 | /* Assume that shell functions without a source file before the shell is |
802 | initialized come from the environment. Otherwise default to "main" | |
803 | (usually functions being defined interactively) */ | |
804 | if (temp->source_file == 0) | |
805 | temp->source_file = shell_initialized ? "main" : "environment"; | |
806 | ||
3185942a | 807 | #if defined (DEBUGGER) |
b80f6443 | 808 | bind_function_def (name->word, temp); |
3185942a | 809 | #endif |
b80f6443 | 810 | |
ac50fbac | 811 | temp->source_file = temp->source_file ? savestring (temp->source_file) : 0; |
a0c0a00f | 812 | |
726f6388 JA |
813 | return (make_command (cm_function_def, (SIMPLE_COM *)temp)); |
814 | } | |
815 | ||
bb70624e JA |
816 | COMMAND * |
817 | make_subshell_command (command) | |
818 | COMMAND *command; | |
819 | { | |
820 | SUBSHELL_COM *temp; | |
821 | ||
822 | temp = (SUBSHELL_COM *)xmalloc (sizeof (SUBSHELL_COM)); | |
823 | temp->command = command; | |
824 | temp->flags = CMD_WANT_SUBSHELL; | |
825 | return (make_command (cm_subshell, (SIMPLE_COM *)temp)); | |
826 | } | |
827 | ||
3185942a JA |
828 | COMMAND * |
829 | make_coproc_command (name, command) | |
830 | char *name; | |
831 | COMMAND *command; | |
832 | { | |
833 | COPROC_COM *temp; | |
834 | ||
835 | temp = (COPROC_COM *)xmalloc (sizeof (COPROC_COM)); | |
836 | temp->name = savestring (name); | |
837 | temp->command = command; | |
838 | temp->flags = CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL; | |
839 | return (make_command (cm_coproc, (SIMPLE_COM *)temp)); | |
840 | } | |
841 | ||
726f6388 JA |
842 | /* Reverse the word list and redirection list in the simple command |
843 | has just been parsed. It seems simpler to do this here the one | |
844 | time then by any other method that I can think of. */ | |
845 | COMMAND * | |
846 | clean_simple_command (command) | |
847 | COMMAND *command; | |
848 | { | |
849 | if (command->type != cm_simple) | |
b72432fd | 850 | command_error ("clean_simple_command", CMDERR_BADTYPE, command->type, 0); |
726f6388 JA |
851 | else |
852 | { | |
853 | command->value.Simple->words = | |
854 | REVERSE_LIST (command->value.Simple->words, WORD_LIST *); | |
ccc6cda3 | 855 | command->value.Simple->redirects = |
726f6388 JA |
856 | REVERSE_LIST (command->value.Simple->redirects, REDIRECT *); |
857 | } | |
858 | ||
0001803f | 859 | parser_state &= ~PST_REDIRLIST; |
726f6388 JA |
860 | return (command); |
861 | } | |
862 | ||
726f6388 JA |
863 | /* The Yacc grammar productions have a problem, in that they take a |
864 | list followed by an ampersand (`&') and do a simple command connection, | |
865 | making the entire list effectively asynchronous, instead of just | |
866 | the last command. This means that when the list is executed, all | |
867 | the commands have stdin set to /dev/null when job control is not | |
868 | active, instead of just the last. This is wrong, and needs fixing | |
869 | up. This function takes the `&' and applies it to the last command | |
870 | in the list. This is done only for lists connected by `;'; it makes | |
871 | `;' bind `tighter' than `&'. */ | |
872 | COMMAND * | |
873 | connect_async_list (command, command2, connector) | |
874 | COMMAND *command, *command2; | |
875 | int connector; | |
876 | { | |
877 | COMMAND *t, *t1, *t2; | |
878 | ||
879 | t1 = command; | |
880 | t = command->value.Connection->second; | |
881 | ||
882 | if (!t || (command->flags & CMD_WANT_SUBSHELL) || | |
883 | command->value.Connection->connector != ';') | |
884 | { | |
885 | t = command_connect (command, command2, connector); | |
886 | return t; | |
887 | } | |
888 | ||
889 | /* This is just defensive programming. The Yacc precedence rules | |
890 | will generally hand this function a command where t points directly | |
891 | to the command we want (e.g. given a ; b ; c ; d &, t1 will point | |
892 | to the `a ; b ; c' list and t will be the `d'). We only want to do | |
893 | this if the list is not being executed as a unit in the background | |
894 | with `( ... )', so we have to check for CMD_WANT_SUBSHELL. That's | |
895 | the only way to tell. */ | |
896 | while (((t->flags & CMD_WANT_SUBSHELL) == 0) && t->type == cm_connection && | |
897 | t->value.Connection->connector == ';') | |
898 | { | |
899 | t1 = t; | |
900 | t = t->value.Connection->second; | |
901 | } | |
902 | /* Now we have t pointing to the last command in the list, and | |
903 | t1->value.Connection->second == t. */ | |
904 | t2 = command_connect (t, command2, connector); | |
905 | t1->value.Connection->second = t2; | |
906 | return command; | |
907 | } |