]> git.ipfire.org Git - thirdparty/glibc.git/blame - posix/wordexp.c
posix: Use posix_spawn for wordexp
[thirdparty/glibc.git] / posix / wordexp.c
CommitLineData
8f2ece69 1/* POSIX.2 wordexp implementation.
04277e02 2 Copyright (C) 1997-2019 Free Software Foundation, Inc.
c84142e8 3 This file is part of the GNU C Library.
8f2ece69 4 Contributed by Tim Waugh <tim@cyberelk.demon.co.uk>.
28f540f4 5
c84142e8 6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
28f540f4 10
c84142e8
UD
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
28f540f4 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
28f540f4 19
51028f34
UD
20#include <ctype.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <fnmatch.h>
24#include <glob.h>
25#include <libintl.h>
26#include <paths.h>
27#include <pwd.h>
bea9b193 28#include <stdbool.h>
51028f34 29#include <stdio.h>
8f2ece69 30#include <string.h>
51028f34 31#include <sys/param.h>
8f2ece69
UD
32#include <sys/wait.h>
33#include <unistd.h>
51028f34 34#include <wordexp.h>
db8cbc6a 35#include <spawn.h>
4272059d 36#include <scratch_buffer.h>
eb96ffb0 37#include <_itoa.h>
8f2ece69 38#include <assert.h>
28f540f4 39
8f2ece69
UD
40/*
41 * This is a recursive-descent-style word expansion routine.
42 */
43
14c44e2e 44/* These variables are defined and initialized in the startup code. */
100351c3
UD
45extern int __libc_argc attribute_hidden;
46extern char **__libc_argv attribute_hidden;
2bcf29ba 47
8f2ece69 48/* Some forward declarations */
40a55d20
UD
49static int parse_dollars (char **word, size_t *word_length, size_t *max_length,
50 const char *words, size_t *offset, int flags,
af69217f 51 wordexp_t *pwordexp, const char *ifs,
b41bd5bc 52 const char *ifs_white, int quoted);
40a55d20
UD
53static int parse_backtick (char **word, size_t *word_length,
54 size_t *max_length, const char *words,
af69217f 55 size_t *offset, int flags, wordexp_t *pwordexp,
b41bd5bc 56 const char *ifs, const char *ifs_white);
22bc7978
UD
57static int parse_dquote (char **word, size_t *word_length, size_t *max_length,
58 const char *words, size_t *offset, int flags,
59 wordexp_t *pwordexp, const char *ifs,
b41bd5bc
FW
60 const char *ifs_white);
61static int eval_expr (char *expr, long int *result);
8f2ece69 62
55c14926 63/* The w_*() functions manipulate word lists. */
8f2ece69 64
40a55d20
UD
65#define W_CHUNK (100)
66
bac660f8 67/* Result of w_newword will be ignored if it's the last word. */
e9fc7bbb
UD
68static inline char *
69w_newword (size_t *actlen, size_t *maxlen)
70{
71 *actlen = *maxlen = 0;
72 return NULL;
73}
74
9c7ff11a 75static char *
40a55d20
UD
76w_addchar (char *buffer, size_t *actlen, size_t *maxlen, char ch)
77 /* (lengths exclude trailing zero) */
8f2ece69 78{
9c7ff11a 79 /* Add a character to the buffer, allocating room for it if needed. */
28f540f4 80
40a55d20 81 if (*actlen == *maxlen)
8f2ece69 82 {
40a55d20
UD
83 char *old_buffer = buffer;
84 assert (buffer == NULL || *maxlen != 0);
85 *maxlen += W_CHUNK;
9c7ff11a 86 buffer = (char *) realloc (buffer, 1 + *maxlen);
40a55d20
UD
87
88 if (buffer == NULL)
89 free (old_buffer);
90 }
28f540f4 91
40a55d20
UD
92 if (buffer != NULL)
93 {
94 buffer[*actlen] = ch;
95 buffer[++(*actlen)] = '\0';
8f2ece69 96 }
40a55d20
UD
97
98 return buffer;
8f2ece69
UD
99}
100
40a55d20 101static char *
3db52d94
UD
102w_addmem (char *buffer, size_t *actlen, size_t *maxlen, const char *str,
103 size_t len)
8f2ece69 104{
40a55d20 105 /* Add a string to the buffer, allocating room for it if needed.
8f2ece69 106 */
40a55d20 107 if (*actlen + len > *maxlen)
8f2ece69 108 {
40a55d20
UD
109 char *old_buffer = buffer;
110 assert (buffer == NULL || *maxlen != 0);
111 *maxlen += MAX (2 * len, W_CHUNK);
112 buffer = realloc (old_buffer, 1 + *maxlen);
113
114 if (buffer == NULL)
115 free (old_buffer);
8f2ece69 116 }
8f2ece69 117
40a55d20
UD
118 if (buffer != NULL)
119 {
86187531 120 *((char *) __mempcpy (&buffer[*actlen], str, len)) = '\0';
40a55d20 121 *actlen += len;
40a55d20 122 }
8f2ece69 123
40a55d20 124 return buffer;
8f2ece69
UD
125}
126
3db52d94 127static char *
3db52d94
UD
128w_addstr (char *buffer, size_t *actlen, size_t *maxlen, const char *str)
129 /* (lengths exclude trailing zero) */
130{
131 /* Add a string to the buffer, allocating room for it if needed.
132 */
133 size_t len;
134
135 assert (str != NULL); /* w_addstr only called from this file */
136 len = strlen (str);
137
138 return w_addmem (buffer, actlen, maxlen, str, len);
139}
140
8f2ece69
UD
141static int
142w_addword (wordexp_t *pwordexp, char *word)
143{
144 /* Add a word to the wordlist */
145 size_t num_p;
22bc7978 146 char **new_wordv;
e7c8359e 147 bool allocated = false;
8f2ece69 148
c06cc21c
UD
149 /* Internally, NULL acts like "". Convert NULLs to "" before
150 * the caller sees them.
151 */
152 if (word == NULL)
153 {
154 word = __strdup ("");
155 if (word == NULL)
156 goto no_space;
e7c8359e 157 allocated = true;
c06cc21c
UD
158 }
159
8f2ece69 160 num_p = 2 + pwordexp->we_wordc + pwordexp->we_offs;
22bc7978
UD
161 new_wordv = realloc (pwordexp->we_wordv, sizeof (char *) * num_p);
162 if (new_wordv != NULL)
8f2ece69 163 {
22bc7978 164 pwordexp->we_wordv = new_wordv;
05d5dedc
UD
165 pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc++] = word;
166 pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc] = NULL;
8f2ece69
UD
167 return 0;
168 }
169
e7c8359e
UD
170 if (allocated)
171 free (word);
172
c06cc21c 173no_space:
8f2ece69
UD
174 return WRDE_NOSPACE;
175}
176
177/* The parse_*() functions should leave *offset being the offset in 'words'
178 * to the last character processed.
179 */
180
181static int
40a55d20
UD
182parse_backslash (char **word, size_t *word_length, size_t *max_length,
183 const char *words, size_t *offset)
8f2ece69
UD
184{
185 /* We are poised _at_ a backslash, not in quotes */
186
187 switch (words[1 + *offset])
188 {
189 case 0:
190 /* Backslash is last character of input words */
191 return WRDE_SYNTAX;
192
193 case '\n':
86187531 194 ++(*offset);
8f2ece69
UD
195 break;
196
197 default:
40a55d20 198 *word = w_addchar (*word, word_length, max_length, words[1 + *offset]);
8f2ece69
UD
199 if (*word == NULL)
200 return WRDE_NOSPACE;
201
86187531 202 ++(*offset);
8f2ece69
UD
203 break;
204 }
205
206 return 0;
207}
208
209static int
40a55d20
UD
210parse_qtd_backslash (char **word, size_t *word_length, size_t *max_length,
211 const char *words, size_t *offset)
8f2ece69
UD
212{
213 /* We are poised _at_ a backslash, inside quotes */
214
215 switch (words[1 + *offset])
216 {
217 case 0:
218 /* Backslash is last character of input words */
219 return WRDE_SYNTAX;
220
221 case '\n':
222 ++(*offset);
223 break;
224
225 case '$':
226 case '`':
227 case '"':
228 case '\\':
40a55d20 229 *word = w_addchar (*word, word_length, max_length, words[1 + *offset]);
8f2ece69
UD
230 if (*word == NULL)
231 return WRDE_NOSPACE;
232
40a55d20 233 ++(*offset);
8f2ece69
UD
234 break;
235
236 default:
40a55d20
UD
237 *word = w_addchar (*word, word_length, max_length, words[*offset]);
238 if (*word != NULL)
239 *word = w_addchar (*word, word_length, max_length, words[1 + *offset]);
240
8f2ece69
UD
241 if (*word == NULL)
242 return WRDE_NOSPACE;
243
40a55d20 244 ++(*offset);
8f2ece69
UD
245 break;
246 }
247
248 return 0;
249}
250
251static int
40a55d20
UD
252parse_tilde (char **word, size_t *word_length, size_t *max_length,
253 const char *words, size_t *offset, size_t wordc)
8f2ece69
UD
254{
255 /* We are poised _at_ a tilde */
55c14926 256 size_t i;
8f2ece69
UD
257
258 if (*word_length != 0)
259 {
260 if (!((*word)[*word_length - 1] == '=' && wordc == 0))
261 {
3db52d94
UD
262 if (!((*word)[*word_length - 1] == ':'
263 && strchr (*word, '=') && wordc == 0))
8f2ece69 264 {
40a55d20 265 *word = w_addchar (*word, word_length, max_length, '~');
8f2ece69
UD
266 return *word ? 0 : WRDE_NOSPACE;
267 }
268 }
269 }
270
271 for (i = 1 + *offset; words[i]; i++)
272 {
34a5a146
JM
273 if (words[i] == ':' || words[i] == '/' || words[i] == ' '
274 || words[i] == '\t' || words[i] == 0 )
8f2ece69
UD
275 break;
276
277 if (words[i] == '\\')
278 {
40a55d20 279 *word = w_addchar (*word, word_length, max_length, '~');
8f2ece69
UD
280 return *word ? 0 : WRDE_NOSPACE;
281 }
282 }
283
284 if (i == 1 + *offset)
285 {
286 /* Tilde appears on its own */
05d5dedc 287 char* home;
8f2ece69 288
05d5dedc
UD
289 /* POSIX.2 says ~ expands to $HOME and if HOME is unset the
290 results are unspecified. We do a lookup on the uid if
291 HOME is unset. */
8f2ece69 292
05d5dedc
UD
293 home = getenv ("HOME");
294 if (home != NULL)
8f2ece69 295 {
05d5dedc 296 *word = w_addstr (*word, word_length, max_length, home);
8f2ece69
UD
297 if (*word == NULL)
298 return WRDE_NOSPACE;
299 }
300 else
301 {
4272059d
FW
302 struct passwd pwd, *tpwd;
303 uid_t uid = __getuid ();
304 int result;
305 struct scratch_buffer tmpbuf;
306 scratch_buffer_init (&tmpbuf);
307
308 while ((result = __getpwuid_r (uid, &pwd,
309 tmpbuf.data, tmpbuf.length,
310 &tpwd)) != 0
05d5dedc 311 && errno == ERANGE)
4272059d
FW
312 if (!scratch_buffer_grow (&tmpbuf))
313 return WRDE_NOSPACE;
05d5dedc
UD
314
315 if (result == 0 && tpwd != NULL && pwd.pw_dir != NULL)
316 {
317 *word = w_addstr (*word, word_length, max_length, pwd.pw_dir);
318 if (*word == NULL)
4272059d
FW
319 {
320 scratch_buffer_free (&tmpbuf);
321 return WRDE_NOSPACE;
322 }
05d5dedc
UD
323 }
324 else
325 {
326 *word = w_addchar (*word, word_length, max_length, '~');
327 if (*word == NULL)
4272059d
FW
328 {
329 scratch_buffer_free (&tmpbuf);
330 return WRDE_NOSPACE;
331 }
05d5dedc 332 }
4272059d 333 scratch_buffer_free (&tmpbuf);
8f2ece69
UD
334 }
335 }
336 else
337 {
338 /* Look up user name in database to get home directory */
344af000 339 char *user = strndupa (&words[1 + *offset], i - (1 + *offset));
8f2ece69 340 struct passwd pwd, *tpwd;
8f2ece69 341 int result;
4272059d
FW
342 struct scratch_buffer tmpbuf;
343 scratch_buffer_init (&tmpbuf);
8f2ece69 344
4272059d
FW
345 while ((result = __getpwnam_r (user, &pwd, tmpbuf.data, tmpbuf.length,
346 &tpwd)) != 0
8f2ece69 347 && errno == ERANGE)
4272059d
FW
348 if (!scratch_buffer_grow (&tmpbuf))
349 return WRDE_NOSPACE;
8f2ece69 350
c5f57c58 351 if (result == 0 && tpwd != NULL && pwd.pw_dir)
40a55d20 352 *word = w_addstr (*word, word_length, max_length, pwd.pw_dir);
8f2ece69
UD
353 else
354 {
355 /* (invalid login name) */
40a55d20 356 *word = w_addchar (*word, word_length, max_length, '~');
8f2ece69 357 if (*word != NULL)
40a55d20 358 *word = w_addstr (*word, word_length, max_length, user);
8f2ece69
UD
359 }
360
4272059d
FW
361 scratch_buffer_free (&tmpbuf);
362
8f2ece69
UD
363 *offset = i - 1;
364 }
365 return *word ? 0 : WRDE_NOSPACE;
366}
367
e9fc7bbb
UD
368
369static int
e9fc7bbb
UD
370do_parse_glob (const char *glob_word, char **word, size_t *word_length,
371 size_t *max_length, wordexp_t *pwordexp, const char *ifs,
372 const char *ifs_white)
373{
374 int error;
2e47aff5 375 unsigned int match;
e9fc7bbb
UD
376 glob_t globbuf;
377
378 error = glob (glob_word, GLOB_NOCHECK, NULL, &globbuf);
379
380 if (error != 0)
381 {
382 /* We can only run into memory problems. */
383 assert (error == GLOB_NOSPACE);
384 return WRDE_NOSPACE;
385 }
386
387 if (ifs && !*ifs)
388 {
389 /* No field splitting allowed. */
390 assert (globbuf.gl_pathv[0] != NULL);
391 *word = w_addstr (*word, word_length, max_length, globbuf.gl_pathv[0]);
392 for (match = 1; match < globbuf.gl_pathc && *word != NULL; ++match)
393 {
394 *word = w_addchar (*word, word_length, max_length, ' ');
395 if (*word != NULL)
396 *word = w_addstr (*word, word_length, max_length,
397 globbuf.gl_pathv[match]);
398 }
399
400 globfree (&globbuf);
401 return *word ? 0 : WRDE_NOSPACE;
402 }
403
404 assert (ifs == NULL || *ifs != '\0');
405 if (*word != NULL)
406 {
407 free (*word);
408 *word = w_newword (word_length, max_length);
409 }
410
411 for (match = 0; match < globbuf.gl_pathc; ++match)
412 {
413 char *matching_word = __strdup (globbuf.gl_pathv[match]);
414 if (matching_word == NULL || w_addword (pwordexp, matching_word))
415 {
416 globfree (&globbuf);
417 return WRDE_NOSPACE;
418 }
419 }
420
421 globfree (&globbuf);
422 return 0;
423}
424
8f2ece69 425static int
40a55d20
UD
426parse_glob (char **word, size_t *word_length, size_t *max_length,
427 const char *words, size_t *offset, int flags,
af69217f 428 wordexp_t *pwordexp, const char *ifs, const char *ifs_white)
8f2ece69 429{
2bcf29ba 430 /* We are poised just after a '*', a '[' or a '?'. */
e9fc7bbb 431 int error = WRDE_NOSPACE;
2bcf29ba 432 int quoted = 0; /* 1 if singly-quoted, 2 if doubly */
57b36a0a 433 size_t i;
e9fc7bbb 434 wordexp_t glob_list; /* List of words to glob */
8f2ece69 435
e9fc7bbb
UD
436 glob_list.we_wordc = 0;
437 glob_list.we_wordv = NULL;
438 glob_list.we_offs = 0;
439 for (; words[*offset] != '\0'; ++*offset)
2bcf29ba 440 {
16d620db 441 if (strchr (ifs, words[*offset]) != NULL)
9b3c7c3c 442 /* Reached IFS */
8f2ece69
UD
443 break;
444
2bcf29ba
UD
445 /* Sort out quoting */
446 if (words[*offset] == '\'')
6e4c40ba 447 {
2bcf29ba
UD
448 if (quoted == 0)
449 {
450 quoted = 1;
451 continue;
452 }
453 else if (quoted == 1)
454 {
455 quoted = 0;
456 continue;
457 }
6e4c40ba 458 }
2bcf29ba 459 else if (words[*offset] == '"')
6e4c40ba
UD
460 {
461 if (quoted == 0)
462 {
463 quoted = 2;
464 continue;
465 }
466 else if (quoted == 2)
467 {
468 quoted = 0;
469 continue;
470 }
471 }
8f2ece69 472
2bcf29ba
UD
473 /* Sort out other special characters */
474 if (quoted != 1 && words[*offset] == '$')
475 {
e9fc7bbb
UD
476 error = parse_dollars (word, word_length, max_length, words,
477 offset, flags, &glob_list, ifs, ifs_white,
9b3c7c3c 478 quoted == 2);
2bcf29ba 479 if (error)
e9fc7bbb 480 goto tidy_up;
2bcf29ba
UD
481
482 continue;
483 }
484 else if (words[*offset] == '\\')
485 {
486 if (quoted)
e9fc7bbb
UD
487 error = parse_qtd_backslash (word, word_length, max_length,
488 words, offset);
2bcf29ba 489 else
e9fc7bbb
UD
490 error = parse_backslash (word, word_length, max_length,
491 words, offset);
2bcf29ba
UD
492
493 if (error)
e9fc7bbb 494 goto tidy_up;
2bcf29ba
UD
495
496 continue;
497 }
498
499 *word = w_addchar (*word, word_length, max_length, words[*offset]);
500 if (*word == NULL)
e9fc7bbb 501 goto tidy_up;
2bcf29ba 502 }
8f2ece69 503
e9fc7bbb
UD
504 /* Don't forget to re-parse the character we stopped at. */
505 --*offset;
8f2ece69 506
e9fc7bbb 507 /* Glob the words */
9b3c7c3c 508 error = w_addword (&glob_list, *word);
e9fc7bbb
UD
509 *word = w_newword (word_length, max_length);
510 for (i = 0; error == 0 && i < glob_list.we_wordc; i++)
511 error = do_parse_glob (glob_list.we_wordv[i], word, word_length,
512 max_length, pwordexp, ifs, ifs_white);
8f2ece69 513
e9fc7bbb
UD
514 /* Now tidy up */
515tidy_up:
516 wordfree (&glob_list);
517 return error;
8f2ece69
UD
518}
519
520static int
40a55d20
UD
521parse_squote (char **word, size_t *word_length, size_t *max_length,
522 const char *words, size_t *offset)
8f2ece69
UD
523{
524 /* We are poised just after a single quote */
525 for (; words[*offset]; ++(*offset))
526 {
527 if (words[*offset] != '\'')
528 {
40a55d20 529 *word = w_addchar (*word, word_length, max_length, words[*offset]);
8f2ece69
UD
530 if (*word == NULL)
531 return WRDE_NOSPACE;
532 }
533 else return 0;
534 }
535
536 /* Unterminated string */
537 return WRDE_SYNTAX;
538}
539
540/* Functions to evaluate an arithmetic expression */
541static int
3db52d94 542eval_expr_val (char **expr, long int *result)
28f540f4 543{
8f2ece69
UD
544 char *digit;
545
546 /* Skip white space */
547 for (digit = *expr; digit && *digit && isspace (*digit); ++digit);
548
bcc86889 549 if (*digit == '(')
8f2ece69 550 {
8f2ece69
UD
551 /* Scan for closing paren */
552 for (++digit; **expr && **expr != ')'; ++(*expr));
553
554 /* Is there one? */
555 if (!**expr)
556 return WRDE_SYNTAX;
557
558 *(*expr)++ = 0;
559
40a55d20 560 if (eval_expr (digit, result))
8f2ece69
UD
561 return WRDE_SYNTAX;
562
563 return 0;
8f2ece69
UD
564 }
565
bcc86889
UD
566 /* POSIX requires that decimal, octal, and hexadecimal constants are
567 recognized. Therefore we pass 0 as the third parameter to strtol. */
568 *result = strtol (digit, expr, 0);
569 if (digit == *expr)
570 return WRDE_SYNTAX;
8f2ece69 571
8f2ece69
UD
572 return 0;
573}
574
575static int
3db52d94 576eval_expr_multdiv (char **expr, long int *result)
8f2ece69 577{
3db52d94 578 long int arg;
8f2ece69
UD
579
580 /* Read a Value */
3db52d94 581 if (eval_expr_val (expr, result) != 0)
8f2ece69
UD
582 return WRDE_SYNTAX;
583
584 while (**expr)
585 {
586 /* Skip white space */
587 for (; *expr && **expr && isspace (**expr); ++(*expr));
588
589 if (**expr == '*')
590 {
3db52d94
UD
591 ++(*expr);
592 if (eval_expr_val (expr, &arg) != 0)
8f2ece69
UD
593 return WRDE_SYNTAX;
594
595 *result *= arg;
596 }
597 else if (**expr == '/')
598 {
3db52d94
UD
599 ++(*expr);
600 if (eval_expr_val (expr, &arg) != 0)
8f2ece69
UD
601 return WRDE_SYNTAX;
602
2b028564
FW
603 /* Division by zero or integer overflow. */
604 if (arg == 0 || (arg == -1 && *result == LONG_MIN))
605 return WRDE_SYNTAX;
606
8f2ece69
UD
607 *result /= arg;
608 }
609 else break;
610 }
611
612 return 0;
613}
614
615static int
3db52d94 616eval_expr (char *expr, long int *result)
8f2ece69 617{
3db52d94 618 long int arg;
8f2ece69
UD
619
620 /* Read a Multdiv */
3db52d94 621 if (eval_expr_multdiv (&expr, result) != 0)
8f2ece69
UD
622 return WRDE_SYNTAX;
623
624 while (*expr)
625 {
626 /* Skip white space */
627 for (; expr && *expr && isspace (*expr); ++expr);
628
629 if (*expr == '+')
630 {
3db52d94
UD
631 ++expr;
632 if (eval_expr_multdiv (&expr, &arg) != 0)
8f2ece69
UD
633 return WRDE_SYNTAX;
634
635 *result += arg;
636 }
637 else if (*expr == '-')
638 {
3db52d94
UD
639 ++expr;
640 if (eval_expr_multdiv (&expr, &arg) != 0)
8f2ece69
UD
641 return WRDE_SYNTAX;
642
643 *result -= arg;
644 }
645 else break;
646 }
647
648 return 0;
649}
650
651static int
40a55d20
UD
652parse_arith (char **word, size_t *word_length, size_t *max_length,
653 const char *words, size_t *offset, int flags, int bracket)
8f2ece69
UD
654{
655 /* We are poised just after "$((" or "$[" */
28f540f4 656 int error;
8f2ece69 657 int paren_depth = 1;
e9fc7bbb
UD
658 size_t expr_length;
659 size_t expr_maxlen;
660 char *expr;
8f2ece69 661
e9fc7bbb 662 expr = w_newword (&expr_length, &expr_maxlen);
8f2ece69
UD
663 for (; words[*offset]; ++(*offset))
664 {
665 switch (words[*offset])
666 {
667 case '$':
40a55d20 668 error = parse_dollars (&expr, &expr_length, &expr_maxlen,
9b3c7c3c 669 words, offset, flags, NULL, NULL, NULL, 1);
2bcf29ba 670 /* The ``1'' here is to tell parse_dollars not to
8f2ece69
UD
671 * split the fields.
672 */
673 if (error)
674 {
675 free (expr);
676 return error;
677 }
678 break;
679
680 case '`':
681 (*offset)++;
40a55d20 682 error = parse_backtick (&expr, &expr_length, &expr_maxlen,
af69217f
UD
683 words, offset, flags, NULL, NULL, NULL);
684 /* The first NULL here is to tell parse_backtick not to
8f2ece69
UD
685 * split the fields.
686 */
687 if (error)
688 {
689 free (expr);
690 return error;
691 }
692 break;
693
694 case '\\':
40a55d20
UD
695 error = parse_qtd_backslash (&expr, &expr_length, &expr_maxlen,
696 words, offset);
8f2ece69
UD
697 if (error)
698 {
699 free (expr);
700 return error;
701 }
702 /* I think that a backslash within an
703 * arithmetic expansion is bound to
704 * cause an error sooner or later anyway though.
705 */
706 break;
707
708 case ')':
709 if (--paren_depth == 0)
710 {
86187531 711 char result[21]; /* 21 = ceil(log10(2^64)) + 1 */
3db52d94
UD
712 long int numresult = 0;
713 long long int convertme;
8f2ece69
UD
714
715 if (bracket || words[1 + *offset] != ')')
33c3e20e
UD
716 {
717 free (expr);
718 return WRDE_SYNTAX;
719 }
8f2ece69
UD
720
721 ++(*offset);
722
723 /* Go - evaluate. */
3db52d94 724 if (*expr && eval_expr (expr, &numresult) != 0)
33c3e20e
UD
725 {
726 free (expr);
727 return WRDE_SYNTAX;
728 }
8f2ece69 729
3db52d94
UD
730 if (numresult < 0)
731 {
732 convertme = -numresult;
733 *word = w_addchar (*word, word_length, max_length, '-');
734 if (!*word)
735 {
736 free (expr);
737 return WRDE_NOSPACE;
738 }
739 }
740 else
741 convertme = numresult;
742
86187531 743 result[20] = '\0';
7fbc8784
UD
744 *word = w_addstr (*word, word_length, max_length,
745 _itoa (convertme, &result[20], 10, 0));
8f2ece69
UD
746 free (expr);
747 return *word ? 0 : WRDE_NOSPACE;
748 }
40a55d20 749 expr = w_addchar (expr, &expr_length, &expr_maxlen, words[*offset]);
8f2ece69
UD
750 if (expr == NULL)
751 return WRDE_NOSPACE;
752
753 break;
754
755 case ']':
756 if (bracket && paren_depth == 1)
757 {
86187531 758 char result[21]; /* 21 = ceil(log10(2^64)) + 1 */
3db52d94 759 long int numresult = 0;
8f2ece69
UD
760
761 /* Go - evaluate. */
762 if (*expr && eval_expr (expr, &numresult) != 0)
33c3e20e
UD
763 {
764 free (expr);
765 return WRDE_SYNTAX;
766 }
8f2ece69 767
86187531
UD
768 result[20] = '\0';
769 *word = w_addstr (*word, word_length, max_length,
770 _itoa_word (numresult, &result[20], 10, 0));
8f2ece69
UD
771 free (expr);
772 return *word ? 0 : WRDE_NOSPACE;
773 }
774
775 free (expr);
776 return WRDE_SYNTAX;
777
778 case '\n':
779 case ';':
780 case '{':
781 case '}':
782 free (expr);
783 return WRDE_BADCHAR;
784
785 case '(':
786 ++paren_depth;
32db86d5 787 /* Fall through. */
8f2ece69 788 default:
40a55d20 789 expr = w_addchar (expr, &expr_length, &expr_maxlen, words[*offset]);
8f2ece69
UD
790 if (expr == NULL)
791 return WRDE_NOSPACE;
792 }
793 }
794
795 /* Premature end */
796 free (expr);
797 return WRDE_SYNTAX;
798}
799
db8cbc6a
AZ
800#define DYNARRAY_STRUCT strlist
801#define DYNARRAY_ELEMENT char *
802#define DYNARRAY_PREFIX strlist_
803/* Allocates about 512/1024 (32/64 bit) on stack. */
804#define DYNARRAY_INITIAL_SIZE 128
805#include <malloc/dynarray-skeleton.c>
806
05d5dedc 807/* Function called by child process in exec_comm() */
db8cbc6a
AZ
808static pid_t
809exec_comm_child (char *comm, int *fildes, bool showerr, bool noexec)
05d5dedc 810{
db8cbc6a 811 pid_t pid = -1;
05d5dedc 812
db8cbc6a
AZ
813 /* Execute the command, or just check syntax? */
814 const char *args[] = { _PATH_BSHELL, noexec ? "-nc" : "-c", comm, NULL };
815
816 posix_spawn_file_actions_t fa;
817 /* posix_spawn_file_actions_init does not fail. */
818 __posix_spawn_file_actions_init (&fa);
05d5dedc 819
db8cbc6a
AZ
820 /* Redirect output. For check syntax only (noexec being true), exec_comm
821 explicits sets fildes[1] to -1, so check its value to avoid a failure in
822 __posix_spawn_file_actions_adddup2. */
823 if (fildes[1] != -1)
e038616f 824 {
db8cbc6a
AZ
825 if (__glibc_likely (fildes[1] != STDOUT_FILENO))
826 {
827 if (__posix_spawn_file_actions_adddup2 (&fa, fildes[1],
828 STDOUT_FILENO) != 0
829 || __posix_spawn_file_actions_addclose (&fa, fildes[1]) != 0)
830 goto out;
831 }
832 else
833 /* Reset the close-on-exec flag (if necessary). */
834 if (__posix_spawn_file_actions_adddup2 (&fa, fildes[1], fildes[1])
835 != 0)
836 goto out;
e038616f 837 }
05d5dedc
UD
838
839 /* Redirect stderr to /dev/null if we have to. */
db8cbc6a
AZ
840 if (!showerr)
841 if (__posix_spawn_file_actions_addopen (&fa, STDERR_FILENO, _PATH_DEVNULL,
842 O_WRONLY, 0) != 0)
843 goto out;
844
845 struct strlist newenv;
846 strlist_init (&newenv);
847
848 bool recreate_env = getenv ("IFS") != NULL;
849 if (recreate_env)
05d5dedc 850 {
db8cbc6a
AZ
851 for (char **ep = __environ; *ep != NULL; ep++)
852 if (strncmp (*ep, "IFS=", strlen ("IFS=")) != 0)
853 strlist_add (&newenv, *ep);
854 strlist_add (&newenv, NULL);
855 if (strlist_has_failed (&newenv))
856 goto out;
05d5dedc
UD
857 }
858
db8cbc6a
AZ
859 /* pid is not set if posix_spawn fails, so it keep the original value
860 of -1. */
861 __posix_spawn (&pid, _PATH_BSHELL, &fa, NULL, (char *const *) args,
862 recreate_env ? strlist_begin (&newenv) : __environ);
05d5dedc 863
db8cbc6a
AZ
864 strlist_free (&newenv);
865
866out:
867 __posix_spawn_file_actions_destroy (&fa);
05d5dedc 868
db8cbc6a 869 return pid;
05d5dedc
UD
870}
871
8f2ece69
UD
872/* Function to execute a command and retrieve the results */
873/* pwordexp contains NULL if field-splitting is forbidden */
874static int
40a55d20 875exec_comm (char *comm, char **word, size_t *word_length, size_t *max_length,
af69217f
UD
876 int flags, wordexp_t *pwordexp, const char *ifs,
877 const char *ifs_white)
8f2ece69 878{
40a55d20 879 int fildes[2];
f89c41c1 880#define bufsize 128
8f2ece69 881 int buflen;
9b3c7c3c 882 int i;
05d5dedc
UD
883 int status = 0;
884 size_t maxnewlines = 0;
f89c41c1 885 char buffer[bufsize];
28f540f4 886 pid_t pid;
db8cbc6a 887 bool noexec = false;
28f540f4 888
a39208bd
CD
889 /* Do nothing if command substitution should not succeed. */
890 if (flags & WRDE_NOCMD)
891 return WRDE_CMDSUB;
892
db8cbc6a 893 /* Don't posix_spawn unless necessary */
8f2ece69
UD
894 if (!comm || !*comm)
895 return 0;
28f540f4 896
46d8874d
FW
897 if (__pipe2 (fildes, O_CLOEXEC) < 0)
898 return WRDE_NOSPACE;
28f540f4 899
f89c41c1 900 again:
db8cbc6a
AZ
901 pid = exec_comm_child (comm, fildes, noexec ? false : flags & WRDE_SHOWERR,
902 noexec);
903 if (pid < 0)
8f2ece69 904 {
e038616f
UD
905 __close (fildes[0]);
906 __close (fildes[1]);
8f2ece69
UD
907 return WRDE_NOSPACE;
908 }
28f540f4 909
f89c41c1
UD
910 /* If we are just testing the syntax, only wait. */
911 if (noexec)
912 return (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) == pid
913 && status != 0) ? WRDE_SYNTAX : 0;
914
50304ef0 915 __close (fildes[1]);
f89c41c1 916 fildes[1] = -1;
28f540f4 917
9b3c7c3c 918 if (!pwordexp)
05d5dedc
UD
919 /* Quoted - no field splitting */
920 {
9b3c7c3c 921 while (1)
8f2ece69 922 {
f89c41c1
UD
923 if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
924 bufsize))) < 1)
9b3c7c3c 925 {
ab087e0a
AS
926 /* If read returned 0 then the process has closed its
927 stdout. Don't use WNOHANG in that case to avoid busy
928 looping until the process eventually exits. */
929 if (TEMP_FAILURE_RETRY (__waitpid (pid, &status,
930 buflen == 0 ? 0 : WNOHANG))
931 == 0)
9b3c7c3c 932 continue;
f89c41c1
UD
933 if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
934 bufsize))) < 1)
9b3c7c3c
UD
935 break;
936 }
937
05d5dedc
UD
938 maxnewlines += buflen;
939
9b3c7c3c
UD
940 *word = w_addmem (*word, word_length, max_length, buffer, buflen);
941 if (*word == NULL)
942 goto no_space;
8f2ece69 943 }
9b3c7c3c
UD
944 }
945 else
946 /* Not quoted - split fields */
947 {
948 int copying = 0;
949 /* 'copying' is:
950 * 0 when searching for first character in a field not IFS white space
951 * 1 when copying the text of a field
952 * 2 when searching for possible non-whitespace IFS
05d5dedc 953 * 3 when searching for non-newline after copying field
9b3c7c3c
UD
954 */
955
956 while (1)
957 {
f89c41c1
UD
958 if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
959 bufsize))) < 1)
9b3c7c3c 960 {
ab087e0a
AS
961 /* If read returned 0 then the process has closed its
962 stdout. Don't use WNOHANG in that case to avoid busy
963 looping until the process eventually exits. */
964 if (TEMP_FAILURE_RETRY (__waitpid (pid, &status,
965 buflen == 0 ? 0 : WNOHANG))
966 == 0)
9b3c7c3c 967 continue;
f89c41c1
UD
968 if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
969 bufsize))) < 1)
9b3c7c3c
UD
970 break;
971 }
a8125d85 972
9b3c7c3c
UD
973 for (i = 0; i < buflen; ++i)
974 {
975 if (strchr (ifs, buffer[i]) != NULL)
976 {
977 /* Current character is IFS */
978 if (strchr (ifs_white, buffer[i]) == NULL)
979 {
980 /* Current character is IFS but not whitespace */
981 if (copying == 2)
982 {
983 /* current character
984 * |
985 * V
986 * eg: text<space><comma><space>moretext
987 *
988 * So, strip whitespace IFS (like at the start)
989 */
990 copying = 0;
991 continue;
992 }
993
994 copying = 0;
995 /* fall through and delimit field.. */
996 }
997 else
998 {
05d5dedc
UD
999 if (buffer[i] == '\n')
1000 {
1001 /* Current character is (IFS) newline */
9b3c7c3c 1002
05d5dedc
UD
1003 /* If copying a field, this is the end of it,
1004 but maybe all that's left is trailing newlines.
1005 So start searching for a non-newline. */
1006 if (copying == 1)
1007 copying = 3;
9b3c7c3c 1008
05d5dedc
UD
1009 continue;
1010 }
1011 else
1012 {
1013 /* Current character is IFS white space, but
1014 not a newline */
1015
1016 /* If not either copying a field or searching
1017 for non-newline after a field, ignore it */
1018 if (copying != 1 && copying != 3)
1019 continue;
1020
1021 /* End of field (search for non-ws IFS afterwards) */
1022 copying = 2;
1023 }
9b3c7c3c
UD
1024 }
1025
05d5dedc 1026 /* First IFS white space (non-newline), or IFS non-whitespace.
9b3c7c3c
UD
1027 * Delimit the field. Nulls are converted by w_addword. */
1028 if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
1029 goto no_space;
1030
1031 *word = w_newword (word_length, max_length);
05d5dedc
UD
1032
1033 maxnewlines = 0;
9b3c7c3c
UD
1034 /* fall back round the loop.. */
1035 }
1036 else
1037 {
1038 /* Not IFS character */
05d5dedc
UD
1039
1040 if (copying == 3)
1041 {
1042 /* Nothing but (IFS) newlines since the last field,
3ce1f295 1043 so delimit it here before starting new word */
05d5dedc
UD
1044 if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
1045 goto no_space;
1046
1047 *word = w_newword (word_length, max_length);
1048 }
1049
9b3c7c3c
UD
1050 copying = 1;
1051
05d5dedc
UD
1052 if (buffer[i] == '\n') /* happens if newline not in IFS */
1053 maxnewlines++;
1054 else
1055 maxnewlines = 0;
1056
9b3c7c3c
UD
1057 *word = w_addchar (*word, word_length, max_length,
1058 buffer[i]);
1059 if (*word == NULL)
1060 goto no_space;
1061 }
1062 }
1063 }
8f2ece69 1064 }
28f540f4 1065
05d5dedc
UD
1066 /* Chop off trailing newlines (required by POSIX.2) */
1067 /* Ensure we don't go back further than the beginning of the
1068 substitution (i.e. remove maxnewlines bytes at most) */
34a5a146
JM
1069 while (maxnewlines-- != 0
1070 && *word_length > 0 && (*word)[*word_length - 1] == '\n')
1720f4d0
UD
1071 {
1072 (*word)[--*word_length] = '\0';
1073
1ffaaca2 1074 /* If the last word was entirely newlines, turn it into a new word
1720f4d0 1075 * which can be ignored if there's nothing following it. */
1ffaaca2 1076 if (*word_length == 0)
1720f4d0
UD
1077 {
1078 free (*word);
1079 *word = w_newword (word_length, max_length);
1ffaaca2 1080 break;
1720f4d0
UD
1081 }
1082 }
22bc7978 1083
50304ef0 1084 __close (fildes[0]);
f89c41c1 1085 fildes[0] = -1;
05d5dedc
UD
1086
1087 /* Check for syntax error (re-execute but with "-n" flag) */
1088 if (buflen < 1 && status != 0)
1089 {
db8cbc6a 1090 noexec = true;
f89c41c1 1091 goto again;
05d5dedc
UD
1092 }
1093
8f2ece69 1094 return 0;
33c3e20e
UD
1095
1096no_space:
1097 __kill (pid, SIGKILL);
f89c41c1 1098 TEMP_FAILURE_RETRY (__waitpid (pid, NULL, 0));
33c3e20e
UD
1099 __close (fildes[0]);
1100 return WRDE_NOSPACE;
8f2ece69
UD
1101}
1102
1103static int
40a55d20 1104parse_comm (char **word, size_t *word_length, size_t *max_length,
af69217f
UD
1105 const char *words, size_t *offset, int flags, wordexp_t *pwordexp,
1106 const char *ifs, const char *ifs_white)
8f2ece69
UD
1107{
1108 /* We are poised just after "$(" */
1109 int paren_depth = 1;
3c20b9b6 1110 int error = 0;
3c20b9b6 1111 int quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
e9fc7bbb
UD
1112 size_t comm_length;
1113 size_t comm_maxlen;
1114 char *comm = w_newword (&comm_length, &comm_maxlen);
8f2ece69
UD
1115
1116 for (; words[*offset]; ++(*offset))
28f540f4 1117 {
8f2ece69 1118 switch (words[*offset])
28f540f4 1119 {
3c20b9b6
UD
1120 case '\'':
1121 if (quoted == 0)
1122 quoted = 1;
1123 else if (quoted == 1)
1124 quoted = 0;
1125
1126 break;
1127
1128 case '"':
1129 if (quoted == 0)
1130 quoted = 2;
1131 else if (quoted == 2)
1132 quoted = 0;
1133
1134 break;
1135
8f2ece69 1136 case ')':
3c20b9b6 1137 if (!quoted && --paren_depth == 0)
8f2ece69
UD
1138 {
1139 /* Go -- give script to the shell */
3c20b9b6
UD
1140 if (comm)
1141 {
db8cbc6a 1142 /* posix_spawn already handles thread cancellation. */
3c20b9b6
UD
1143 error = exec_comm (comm, word, word_length, max_length,
1144 flags, pwordexp, ifs, ifs_white);
1145 free (comm);
1146 }
1147
8f2ece69
UD
1148 return error;
1149 }
1150
1151 /* This is just part of the script */
8f2ece69
UD
1152 break;
1153
1154 case '(':
3c20b9b6
UD
1155 if (!quoted)
1156 ++paren_depth;
28f540f4 1157 }
3c20b9b6
UD
1158
1159 comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]);
1160 if (comm == NULL)
1161 return WRDE_NOSPACE;
28f540f4 1162 }
28f540f4 1163
ecf359c6
UD
1164 /* Premature end. */
1165 free (comm);
3c20b9b6 1166
8f2ece69
UD
1167 return WRDE_SYNTAX;
1168}
28f540f4 1169
01d032e0
PP
1170#define CHAR_IN_SET(ch, char_set) \
1171 (memchr (char_set "", ch, sizeof (char_set) - 1) != NULL)
1172
8f2ece69 1173static int
40a55d20 1174parse_param (char **word, size_t *word_length, size_t *max_length,
af69217f 1175 const char *words, size_t *offset, int flags, wordexp_t *pwordexp,
2bcf29ba 1176 const char *ifs, const char *ifs_white, int quoted)
8f2ece69
UD
1177{
1178 /* We are poised just after "$" */
3c20b9b6 1179 enum action
af6f3906 1180 {
3c20b9b6
UD
1181 ACT_NONE,
1182 ACT_RP_SHORT_LEFT = '#',
1183 ACT_RP_LONG_LEFT = 'L',
1184 ACT_RP_SHORT_RIGHT = '%',
1185 ACT_RP_LONG_RIGHT = 'R',
1186 ACT_NULL_ERROR = '?',
1187 ACT_NULL_SUBST = '-',
1188 ACT_NONNULL_SUBST = '+',
1189 ACT_NULL_ASSIGN = '='
af6f3906 1190 };
e9fc7bbb
UD
1191 size_t env_length;
1192 size_t env_maxlen;
1193 size_t pat_length;
1194 size_t pat_maxlen;
3c20b9b6 1195 size_t start = *offset;
e9fc7bbb
UD
1196 char *env;
1197 char *pattern;
2bcf29ba 1198 char *value = NULL;
3c20b9b6 1199 enum action action = ACT_NONE;
8f2ece69 1200 int depth = 0;
3c20b9b6 1201 int colon_seen = 0;
14c44e2e 1202 int seen_hash = 0;
76fbcfdd 1203 int free_value = 0;
3c20b9b6 1204 int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
8f2ece69 1205 int error;
1f4a9ebd
UD
1206 int special = 0;
1207 char buffer[21];
3c20b9b6
UD
1208 int brace = words[*offset] == '{';
1209
e9fc7bbb
UD
1210 env = w_newword (&env_length, &env_maxlen);
1211 pattern = w_newword (&pat_length, &pat_maxlen);
1212
3c20b9b6
UD
1213 if (brace)
1214 ++*offset;
8f2ece69 1215
1f4a9ebd
UD
1216 /* First collect the parameter name. */
1217
1218 if (words[*offset] == '#')
8f2ece69 1219 {
1f4a9ebd
UD
1220 seen_hash = 1;
1221 if (!brace)
1222 goto envsubst;
1223 ++*offset;
1224 }
af6f3906 1225
1f4a9ebd
UD
1226 if (isalpha (words[*offset]) || words[*offset] == '_')
1227 {
1228 /* Normal parameter name. */
1229 do
1230 {
1231 env = w_addchar (env, &env_length, &env_maxlen,
1232 words[*offset]);
1233 if (env == NULL)
1234 goto no_space;
1235 }
1236 while (isalnum (words[++*offset]) || words[*offset] == '_');
1237 }
1238 else if (isdigit (words[*offset]))
1239 {
1240 /* Numeric parameter name. */
1241 special = 1;
1242 do
1243 {
1244 env = w_addchar (env, &env_length, &env_maxlen,
1245 words[*offset]);
1246 if (env == NULL)
1247 goto no_space;
1248 if (!brace)
1249 goto envsubst;
1250 }
1251 while (isdigit(words[++*offset]));
1252 }
01d032e0 1253 else if (CHAR_IN_SET (words[*offset], "*@$"))
1f4a9ebd
UD
1254 {
1255 /* Special parameter. */
1256 special = 1;
1257 env = w_addchar (env, &env_length, &env_maxlen,
1258 words[*offset]);
1259 if (env == NULL)
1260 goto no_space;
1261 ++*offset;
1262 }
1263 else
1264 {
1265 if (brace)
1266 goto syntax;
1267 }
1268
1269 if (brace)
1270 {
1271 /* Check for special action to be applied to the value. */
1272 switch (words[*offset])
1273 {
1274 case '}':
68b50604 1275 /* Evaluate. */
1f4a9ebd
UD
1276 goto envsubst;
1277
1278 case '#':
1279 action = ACT_RP_SHORT_LEFT;
1280 if (words[1 + *offset] == '#')
1281 {
1282 ++*offset;
1283 action = ACT_RP_LONG_LEFT;
1284 }
1285 break;
1286
1287 case '%':
1288 action = ACT_RP_SHORT_RIGHT;
1289 if (words[1 + *offset] == '%')
1290 {
1291 ++*offset;
1292 action = ACT_RP_LONG_RIGHT;
1293 }
1294 break;
1295
1296 case ':':
01d032e0 1297 if (!CHAR_IN_SET (words[1 + *offset], "-=?+"))
1f4a9ebd
UD
1298 goto syntax;
1299
1300 colon_seen = 1;
1301 action = words[++*offset];
1302 break;
1303
1304 case '-':
1305 case '=':
1306 case '?':
1307 case '+':
1308 action = words[*offset];
1309 break;
1310
1311 default:
1312 goto syntax;
1313 }
1314
bac660f8 1315 /* Now collect the pattern, but don't expand it yet. */
1f4a9ebd
UD
1316 ++*offset;
1317 for (; words[*offset]; ++(*offset))
3c20b9b6
UD
1318 {
1319 switch (words[*offset])
8f2ece69 1320 {
3c20b9b6
UD
1321 case '{':
1322 if (!pattern_is_quoted)
1323 ++depth;
1324 break;
8f2ece69 1325
3c20b9b6
UD
1326 case '}':
1327 if (!pattern_is_quoted)
1328 {
1329 if (depth == 0)
1330 goto envsubst;
1331 --depth;
1332 }
8f2ece69 1333 break;
8f2ece69 1334
3c20b9b6 1335 case '\\':
bac660f8
UD
1336 if (pattern_is_quoted)
1337 /* Quoted; treat as normal character. */
1338 break;
1339
1340 /* Otherwise, it's an escape: next character is literal. */
1341 if (words[++*offset] == '\0')
3c20b9b6 1342 goto syntax;
bac660f8
UD
1343
1344 pattern = w_addchar (pattern, &pat_length, &pat_maxlen, '\\');
1345 if (pattern == NULL)
1346 goto no_space;
1347
3c20b9b6 1348 break;
af6f3906 1349
3c20b9b6
UD
1350 case '\'':
1351 if (pattern_is_quoted == 0)
1352 pattern_is_quoted = 1;
1353 else if (pattern_is_quoted == 1)
1354 pattern_is_quoted = 0;
8f2ece69 1355
3c20b9b6 1356 break;
8f2ece69 1357
3c20b9b6
UD
1358 case '"':
1359 if (pattern_is_quoted == 0)
1360 pattern_is_quoted = 2;
1361 else if (pattern_is_quoted == 2)
1362 pattern_is_quoted = 0;
8f2ece69 1363
3c20b9b6 1364 break;
8f2ece69
UD
1365 }
1366
3c20b9b6
UD
1367 pattern = w_addchar (pattern, &pat_length, &pat_maxlen,
1368 words[*offset]);
1369 if (pattern == NULL)
1370 goto no_space;
8f2ece69
UD
1371 }
1372 }
1373
1f4a9ebd
UD
1374 /* End of input string -- remember to reparse the character that we
1375 * stopped at. */
8f2ece69
UD
1376 --(*offset);
1377
1378envsubst:
1379 if (words[start] == '{' && words[*offset] != '}')
af6f3906 1380 goto syntax;
8f2ece69 1381
1f4a9ebd 1382 if (env == NULL)
8f2ece69 1383 {
14c44e2e
UD
1384 if (seen_hash)
1385 {
1386 /* $# expands to the number of positional parameters */
14c44e2e 1387 buffer[20] = '\0';
1f4a9ebd
UD
1388 value = _itoa_word (__libc_argc - 1, &buffer[20], 10, 0);
1389 seen_hash = 0;
14c44e2e
UD
1390 }
1391 else
1392 {
1393 /* Just $ on its own */
1394 *offset = start - 1;
1395 *word = w_addchar (*word, word_length, max_length, '$');
1f4a9ebd 1396 return *word ? 0 : WRDE_NOSPACE;
14c44e2e 1397 }
8f2ece69 1398 }
e9fc7bbb 1399 /* Is it a numeric parameter? */
1f4a9ebd
UD
1400 else if (isdigit (env[0]))
1401 {
1402 int n = atoi (env);
8f2ece69 1403
1f4a9ebd
UD
1404 if (n >= __libc_argc)
1405 /* Substitute NULL. */
1406 value = NULL;
1407 else
1408 /* Replace with appropriate positional parameter. */
1409 value = __libc_argv[n];
1410 }
14c44e2e 1411 /* Is it a special parameter? */
1f4a9ebd 1412 else if (special)
2bcf29ba 1413 {
1f4a9ebd
UD
1414 /* Is it `$$'? */
1415 if (*env == '$')
14c44e2e 1416 {
1f4a9ebd 1417 buffer[20] = '\0';
50304ef0 1418 value = _itoa_word (__getpid (), &buffer[20], 10, 0);
14c44e2e 1419 }
1f4a9ebd
UD
1420 /* Is it `${#*}' or `${#@}'? */
1421 else if ((*env == '*' || *env == '@') && seen_hash)
2bcf29ba 1422 {
1f4a9ebd
UD
1423 buffer[20] = '\0';
1424 value = _itoa_word (__libc_argc > 0 ? __libc_argc - 1 : 0,
1425 &buffer[20], 10, 0);
1426 *word = w_addstr (*word, word_length, max_length, value);
14c44e2e 1427 free (env);
ecf359c6 1428 free (pattern);
14c44e2e
UD
1429 return *word ? 0 : WRDE_NOSPACE;
1430 }
1431 /* Is it `$*' or `$@' (unquoted) ? */
1432 else if (*env == '*' || (*env == '@' && !quoted))
1433 {
68b50604 1434 size_t plist_len = 0;
14c44e2e 1435 int p;
68b50604 1436 char *end;
14c44e2e 1437
2bcf29ba 1438 /* Build up value parameter by parameter (copy them) */
68b50604
UD
1439 for (p = 1; __libc_argv[p]; ++p)
1440 plist_len += strlen (__libc_argv[p]) + 1; /* for space */
1441 value = malloc (plist_len);
1442 if (value == NULL)
1443 goto no_space;
1444 end = value;
1445 *end = 0;
8d9618b7 1446 for (p = 1; __libc_argv[p]; ++p)
2bcf29ba 1447 {
68b50604
UD
1448 if (p > 1)
1449 *end++ = ' ';
1450 end = __stpcpy (end, __libc_argv[p]);
2bcf29ba
UD
1451 }
1452
76fbcfdd 1453 free_value = 1;
2bcf29ba 1454 }
1f4a9ebd
UD
1455 else
1456 {
1457 /* Must be a quoted `$@' */
1458 assert (*env == '@' && quoted);
2bcf29ba 1459
1f4a9ebd
UD
1460 /* Each parameter is a separate word ("$@") */
1461 if (__libc_argc == 2)
68b50604 1462 value = __libc_argv[1];
1f4a9ebd
UD
1463 else if (__libc_argc > 2)
1464 {
1465 int p;
14c44e2e 1466
1f4a9ebd
UD
1467 /* Append first parameter to current word. */
1468 value = w_addstr (*word, word_length, max_length,
1469 __libc_argv[1]);
1470 if (value == NULL || w_addword (pwordexp, value))
1471 goto no_space;
14c44e2e 1472
1f4a9ebd
UD
1473 for (p = 2; __libc_argv[p + 1]; p++)
1474 {
1475 char *newword = __strdup (__libc_argv[p]);
1476 if (newword == NULL || w_addword (pwordexp, newword))
1477 goto no_space;
1478 }
2bcf29ba 1479
1f4a9ebd 1480 /* Start a new word with the last parameter. */
e9fc7bbb 1481 *word = w_newword (word_length, max_length);
fdacb17d 1482 value = __libc_argv[p];
1f4a9ebd
UD
1483 }
1484 else
2bcf29ba 1485 {
1f4a9ebd
UD
1486 free (env);
1487 free (pattern);
1488 return 0;
2bcf29ba
UD
1489 }
1490 }
2bcf29ba 1491 }
1f4a9ebd 1492 else
68b50604
UD
1493 value = getenv (env);
1494
1495 if (value == NULL && (flags & WRDE_UNDEF))
1f4a9ebd 1496 {
68b50604 1497 /* Variable not defined. */
33c3e20e
UD
1498 error = WRDE_BADVAL;
1499 goto do_error;
1f4a9ebd 1500 }
8f2ece69 1501
3c20b9b6 1502 if (action != ACT_NONE)
8f2ece69 1503 {
bac660f8
UD
1504 int expand_pattern = 0;
1505
1506 /* First, find out if we need to expand pattern (i.e. if we will
1507 * use it). */
1508 switch (action)
1509 {
1510 case ACT_RP_SHORT_LEFT:
1511 case ACT_RP_LONG_LEFT:
1512 case ACT_RP_SHORT_RIGHT:
1513 case ACT_RP_LONG_RIGHT:
1514 /* Always expand for these. */
1515 expand_pattern = 1;
1516 break;
1517
1518 case ACT_NULL_ERROR:
1519 case ACT_NULL_SUBST:
1520 case ACT_NULL_ASSIGN:
1521 if (!value || (!*value && colon_seen))
1522 /* If param is unset, or set but null and a colon has been seen,
1523 the expansion of the pattern will be needed. */
1524 expand_pattern = 1;
1525
1526 break;
1527
1528 case ACT_NONNULL_SUBST:
1529 /* Expansion of word will be needed if parameter is set and not null,
1530 or set null but no colon has been seen. */
1531 if (value && (*value || !colon_seen))
1532 expand_pattern = 1;
1533
1534 break;
1535
1536 default:
1537 assert (! "Unrecognised action!");
1538 }
1539
1540 if (expand_pattern)
1541 {
1542 /* We need to perform tilde expansion, parameter expansion,
3ce1f295 1543 command substitution, and arithmetic expansion. We also
bac660f8
UD
1544 have to be a bit careful with wildcard characters, as
1545 pattern might be given to fnmatch soon. To do this, we
1546 convert quotes to escapes. */
1547
1548 char *expanded;
1549 size_t exp_len;
1550 size_t exp_maxl;
1551 char *p;
1552 int quoted = 0; /* 1: single quotes; 2: double */
1553
1554 expanded = w_newword (&exp_len, &exp_maxl);
1555 for (p = pattern; p && *p; p++)
1556 {
4ad1f026 1557 size_t offset;
bac660f8
UD
1558
1559 switch (*p)
1560 {
1561 case '"':
1562 if (quoted == 2)
1563 quoted = 0;
1564 else if (quoted == 0)
1565 quoted = 2;
1566 else break;
1567
1568 continue;
1569
1570 case '\'':
1571 if (quoted == 1)
1572 quoted = 0;
1573 else if (quoted == 0)
1574 quoted = 1;
1575 else break;
1576
1577 continue;
1578
1579 case '*':
1580 case '?':
1581 if (quoted)
1582 {
1583 /* Convert quoted wildchar to escaped wildchar. */
1584 expanded = w_addchar (expanded, &exp_len,
1585 &exp_maxl, '\\');
1586
1587 if (expanded == NULL)
1588 goto no_space;
1589 }
1590 break;
1591
1592 case '$':
1593 offset = 0;
1594 error = parse_dollars (&expanded, &exp_len, &exp_maxl, p,
1595 &offset, flags, NULL, NULL, NULL, 1);
1596 if (error)
1597 {
1598 if (free_value)
1599 free (value);
1600
ecf359c6 1601 free (expanded);
bac660f8
UD
1602
1603 goto do_error;
1604 }
1605
1606 p += offset;
1607 continue;
1608
1609 case '~':
1610 if (quoted || exp_len)
1611 break;
1612
1613 offset = 0;
1614 error = parse_tilde (&expanded, &exp_len, &exp_maxl, p,
1615 &offset, 0);
1616 if (error)
1617 {
1618 if (free_value)
1619 free (value);
1620
ecf359c6 1621 free (expanded);
bac660f8
UD
1622
1623 goto do_error;
1624 }
1625
1626 p += offset;
1627 continue;
1628
1629 case '\\':
1630 expanded = w_addchar (expanded, &exp_len, &exp_maxl, '\\');
1631 ++p;
1632 assert (*p); /* checked when extracted initially */
1633 if (expanded == NULL)
1634 goto no_space;
1635 }
1636
1637 expanded = w_addchar (expanded, &exp_len, &exp_maxl, *p);
1638
1639 if (expanded == NULL)
1640 goto no_space;
1641 }
1642
ecf359c6 1643 free (pattern);
bac660f8
UD
1644
1645 pattern = expanded;
1646 }
1647
8f2ece69
UD
1648 switch (action)
1649 {
3c20b9b6
UD
1650 case ACT_RP_SHORT_LEFT:
1651 case ACT_RP_LONG_LEFT:
1652 case ACT_RP_SHORT_RIGHT:
1653 case ACT_RP_LONG_RIGHT:
af6f3906
UD
1654 {
1655 char *p;
1656 char c;
1657 char *end;
1658
1f4a9ebd 1659 if (value == NULL || pattern == NULL || *pattern == '\0')
af6f3906
UD
1660 break;
1661
1662 end = value + strlen (value);
1663
3c20b9b6 1664 switch (action)
af6f3906 1665 {
3c20b9b6 1666 case ACT_RP_SHORT_LEFT:
3db52d94 1667 for (p = value; p <= end; ++p)
af6f3906
UD
1668 {
1669 c = *p;
1670 *p = '\0';
1671 if (fnmatch (pattern, value, 0) != FNM_NOMATCH)
1672 {
1673 *p = c;
68b50604
UD
1674 if (free_value)
1675 {
1676 char *newval = __strdup (p);
1677 if (newval == NULL)
1678 {
1679 free (value);
1680 goto no_space;
1681 }
1682 free (value);
1683 value = newval;
1684 }
1685 else
1686 value = p;
af6f3906
UD
1687 break;
1688 }
1689 *p = c;
1690 }
1691
1692 break;
1693
3c20b9b6 1694 case ACT_RP_LONG_LEFT:
3db52d94 1695 for (p = end; p >= value; --p)
af6f3906
UD
1696 {
1697 c = *p;
1698 *p = '\0';
1699 if (fnmatch (pattern, value, 0) != FNM_NOMATCH)
1700 {
1701 *p = c;
68b50604
UD
1702 if (free_value)
1703 {
1704 char *newval = __strdup (p);
1705 if (newval == NULL)
1706 {
1707 free (value);
1708 goto no_space;
1709 }
1710 free (value);
1711 value = newval;
1712 }
1713 else
1714 value = p;
af6f3906
UD
1715 break;
1716 }
1717 *p = c;
1718 }
1719
1720 break;
1721
3c20b9b6 1722 case ACT_RP_SHORT_RIGHT:
3db52d94 1723 for (p = end; p >= value; --p)
af6f3906
UD
1724 {
1725 if (fnmatch (pattern, p, 0) != FNM_NOMATCH)
1726 {
1f4a9ebd
UD
1727 char *newval;
1728 newval = malloc (p - value + 1);
33c3e20e 1729
1f4a9ebd 1730 if (newval == NULL)
33c3e20e
UD
1731 {
1732 if (free_value)
1733 free (value);
1734 goto no_space;
1735 }
1736
1f4a9ebd
UD
1737 *(char *) __mempcpy (newval, value, p - value) = '\0';
1738 if (free_value)
1739 free (value);
1740 value = newval;
1741 free_value = 1;
af6f3906
UD
1742 break;
1743 }
1744 }
1745
1746 break;
1747
3c20b9b6 1748 case ACT_RP_LONG_RIGHT:
3db52d94 1749 for (p = value; p <= end; ++p)
af6f3906
UD
1750 {
1751 if (fnmatch (pattern, p, 0) != FNM_NOMATCH)
1752 {
1f4a9ebd
UD
1753 char *newval;
1754 newval = malloc (p - value + 1);
33c3e20e 1755
1f4a9ebd 1756 if (newval == NULL)
33c3e20e
UD
1757 {
1758 if (free_value)
1759 free (value);
1760 goto no_space;
1761 }
1762
1f4a9ebd
UD
1763 *(char *) __mempcpy (newval, value, p - value) = '\0';
1764 if (free_value)
1765 free (value);
1766 value = newval;
1767 free_value = 1;
af6f3906
UD
1768 break;
1769 }
1770 }
1771
1772 break;
1773
1774 default:
3c20b9b6 1775 break;
af6f3906
UD
1776 }
1777
1778 break;
1779 }
8f2ece69 1780
3c20b9b6 1781 case ACT_NULL_ERROR:
8f2ece69 1782 if (value && *value)
3db52d94 1783 /* Substitute parameter */
8f2ece69
UD
1784 break;
1785
bac660f8 1786 error = 0;
8f2ece69 1787 if (!colon_seen && value)
1f4a9ebd 1788 /* Substitute NULL */
bac660f8 1789 ;
1f4a9ebd
UD
1790 else
1791 {
51028f34
UD
1792 const char *str = pattern;
1793
1794 if (str[0] == '\0')
6293b803 1795 str = _("parameter null or not set");
51028f34 1796
8a259a23 1797 __fxprintf (NULL, "%s: %s\n", env, str);
8f2ece69
UD
1798 }
1799
68b50604
UD
1800 if (free_value)
1801 free (value);
33c3e20e 1802 goto do_error;
8f2ece69 1803
3c20b9b6 1804 case ACT_NULL_SUBST:
3db52d94
UD
1805 if (value && *value)
1806 /* Substitute parameter */
1807 break;
1808
11bf311e 1809 if (free_value)
bac660f8 1810 free (value);
3db52d94 1811
bac660f8
UD
1812 if (!colon_seen && value)
1813 /* Substitute NULL */
1814 goto success;
1f4a9ebd 1815
bac660f8
UD
1816 value = pattern ? __strdup (pattern) : pattern;
1817 free_value = 1;
3db52d94 1818
bac660f8
UD
1819 if (pattern && !value)
1820 goto no_space;
3db52d94 1821
bac660f8 1822 break;
8d9618b7 1823
bac660f8
UD
1824 case ACT_NONNULL_SUBST:
1825 if (value && (*value || !colon_seen))
1826 {
11bf311e 1827 if (free_value)
bac660f8 1828 free (value);
8d9618b7 1829
bac660f8
UD
1830 value = pattern ? __strdup (pattern) : pattern;
1831 free_value = 1;
3db52d94 1832
bac660f8
UD
1833 if (pattern && !value)
1834 goto no_space;
3db52d94 1835
bac660f8
UD
1836 break;
1837 }
3db52d94
UD
1838
1839 /* Substitute NULL */
68b50604
UD
1840 if (free_value)
1841 free (value);
33c3e20e 1842 goto success;
3db52d94 1843
3c20b9b6 1844 case ACT_NULL_ASSIGN:
3db52d94
UD
1845 if (value && *value)
1846 /* Substitute parameter */
1847 break;
1848
1849 if (!colon_seen && value)
1850 {
1851 /* Substitute NULL */
68b50604
UD
1852 if (free_value)
1853 free (value);
33c3e20e 1854 goto success;
3db52d94
UD
1855 }
1856
ecf359c6 1857 if (free_value)
bac660f8
UD
1858 free (value);
1859
1860 value = pattern ? __strdup (pattern) : pattern;
1861 free_value = 1;
1862
1863 if (pattern && !value)
1864 goto no_space;
1865
a2d4cf72 1866 __setenv (env, value ?: "", 1);
bac660f8 1867 break;
3db52d94 1868
8f2ece69 1869 default:
3db52d94 1870 assert (! "Unrecognised action!");
8f2ece69
UD
1871 }
1872 }
1873
ecf359c6
UD
1874 free (env);
1875 env = NULL;
1876 free (pattern);
1877 pattern = NULL;
8f2ece69 1878
14c44e2e 1879 if (seen_hash)
3db52d94
UD
1880 {
1881 char param_length[21];
1882 param_length[20] = '\0';
1883 *word = w_addstr (*word, word_length, max_length,
1f4a9ebd
UD
1884 _itoa_word (value ? strlen (value) : 0,
1885 &param_length[20], 10, 0));
1886 if (free_value)
1887 {
1888 assert (value != NULL);
1889 free (value);
1890 }
1891
3db52d94
UD
1892 return *word ? 0 : WRDE_NOSPACE;
1893 }
1894
1f4a9ebd
UD
1895 if (value == NULL)
1896 return 0;
1897
9b3c7c3c
UD
1898 if (quoted || !pwordexp)
1899 {
1900 /* Quoted - no field split */
1901 *word = w_addstr (*word, word_length, max_length, value);
1902 if (free_value)
1903 free (value);
af69217f 1904
9b3c7c3c
UD
1905 return *word ? 0 : WRDE_NOSPACE;
1906 }
1907 else
1908 {
1909 /* Need to field-split */
1910 char *value_copy = __strdup (value); /* Don't modify value */
1911 char *field_begin = value_copy;
1912 int seen_nonws_ifs = 0;
1913
1914 if (free_value)
1915 free (value);
1916
1917 if (value_copy == NULL)
1918 goto no_space;
1919
1920 do
1921 {
1922 char *field_end = field_begin;
1923 char *next_field;
1924
1925 /* If this isn't the first field, start a new word */
1926 if (field_begin != value_copy)
1927 {
1928 if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
1929 {
1930 free (value_copy);
1931 goto no_space;
1932 }
1933
1934 *word = w_newword (word_length, max_length);
1935 }
1936
1937 /* Skip IFS whitespace before the field */
1938 field_begin += strspn (field_begin, ifs_white);
1939
1940 if (!seen_nonws_ifs && *field_begin == 0)
1941 /* Nothing but whitespace */
1942 break;
1943
1944 /* Search for the end of the field */
1945 field_end = field_begin + strcspn (field_begin, ifs);
1946
1947 /* Set up pointer to the character after end of field and
3ce1f295 1948 skip whitespace IFS after it. */
9b3c7c3c
UD
1949 next_field = field_end + strspn (field_end, ifs_white);
1950
1951 /* Skip at most one non-whitespace IFS character after the field */
1952 seen_nonws_ifs = 0;
1953 if (*next_field && strchr (ifs, *next_field))
1954 {
1955 seen_nonws_ifs = 1;
1956 next_field++;
1957 }
1958
1959 /* Null-terminate it */
1960 *field_end = 0;
1961
1962 /* Tag a copy onto the current word */
1963 *word = w_addstr (*word, word_length, max_length, field_begin);
1964
1965 if (*word == NULL && *field_begin != '\0')
1966 {
1967 free (value_copy);
1968 goto no_space;
1969 }
1970
1971 field_begin = next_field;
1972 }
1973 while (seen_nonws_ifs || *field_begin);
1974
1975 free (value_copy);
1976 }
1977
1978 return 0;
8f2ece69 1979
33c3e20e
UD
1980success:
1981 error = 0;
1982 goto do_error;
8f2ece69 1983
33c3e20e
UD
1984no_space:
1985 error = WRDE_NOSPACE;
1986 goto do_error;
af6f3906
UD
1987
1988syntax:
33c3e20e
UD
1989 error = WRDE_SYNTAX;
1990
1991do_error:
ecf359c6 1992 free (env);
af6f3906 1993
ecf359c6 1994 free (pattern);
af6f3906 1995
33c3e20e 1996 return error;
8f2ece69
UD
1997}
1998
01d032e0
PP
1999#undef CHAR_IN_SET
2000
8f2ece69 2001static int
40a55d20
UD
2002parse_dollars (char **word, size_t *word_length, size_t *max_length,
2003 const char *words, size_t *offset, int flags,
2bcf29ba 2004 wordexp_t *pwordexp, const char *ifs, const char *ifs_white,
9b3c7c3c 2005 int quoted)
8f2ece69
UD
2006{
2007 /* We are poised _at_ "$" */
2008 switch (words[1 + *offset])
28f540f4 2009 {
8f2ece69
UD
2010 case '"':
2011 case '\'':
2012 case 0:
40a55d20 2013 *word = w_addchar (*word, word_length, max_length, '$');
8f2ece69
UD
2014 return *word ? 0 : WRDE_NOSPACE;
2015
2016 case '(':
2017 if (words[2 + *offset] == '(')
2018 {
2bcf29ba
UD
2019 /* Differentiate between $((1+3)) and $((echo);(ls)) */
2020 int i = 3 + *offset;
3c20b9b6
UD
2021 int depth = 0;
2022 while (words[i] && !(depth == 0 && words[i] == ')'))
2023 {
2024 if (words[i] == '(')
2025 ++depth;
2026 else if (words[i] == ')')
2027 --depth;
2028
2029 ++i;
2030 }
2031
2bcf29ba
UD
2032 if (words[i] == ')' && words[i + 1] == ')')
2033 {
2034 (*offset) += 3;
2035 /* Call parse_arith -- 0 is for "no brackets" */
2036 return parse_arith (word, word_length, max_length, words, offset,
2037 flags, 0);
2038 }
8f2ece69
UD
2039 }
2040
8f2ece69 2041 (*offset) += 2;
40a55d20 2042 return parse_comm (word, word_length, max_length, words, offset, flags,
8d9618b7 2043 quoted? NULL : pwordexp, ifs, ifs_white);
8f2ece69
UD
2044
2045 case '[':
2046 (*offset) += 2;
2047 /* Call parse_arith -- 1 is for "brackets" */
40a55d20
UD
2048 return parse_arith (word, word_length, max_length, words, offset, flags,
2049 1);
8f2ece69
UD
2050
2051 case '{':
2052 default:
2053 ++(*offset); /* parse_param needs to know if "{" is there */
40a55d20 2054 return parse_param (word, word_length, max_length, words, offset, flags,
2bcf29ba 2055 pwordexp, ifs, ifs_white, quoted);
28f540f4 2056 }
8f2ece69 2057}
28f540f4 2058
8f2ece69 2059static int
40a55d20
UD
2060parse_backtick (char **word, size_t *word_length, size_t *max_length,
2061 const char *words, size_t *offset, int flags,
af69217f 2062 wordexp_t *pwordexp, const char *ifs, const char *ifs_white)
8f2ece69
UD
2063{
2064 /* We are poised just after "`" */
2065 int error;
8f2ece69 2066 int squoting = 0;
e9fc7bbb
UD
2067 size_t comm_length;
2068 size_t comm_maxlen;
2069 char *comm = w_newword (&comm_length, &comm_maxlen);
8f2ece69
UD
2070
2071 for (; words[*offset]; ++(*offset))
28f540f4 2072 {
8f2ece69
UD
2073 switch (words[*offset])
2074 {
2075 case '`':
2076 /* Go -- give the script to the shell */
40a55d20 2077 error = exec_comm (comm, word, word_length, max_length, flags,
af69217f 2078 pwordexp, ifs, ifs_white);
8f2ece69
UD
2079 free (comm);
2080 return error;
2081
2082 case '\\':
2083 if (squoting)
2084 {
40a55d20
UD
2085 error = parse_qtd_backslash (&comm, &comm_length, &comm_maxlen,
2086 words, offset);
8f2ece69
UD
2087
2088 if (error)
2089 {
2090 free (comm);
2091 return error;
2092 }
2093
2094 break;
2095 }
2096
40a55d20
UD
2097 error = parse_backslash (&comm, &comm_length, &comm_maxlen, words,
2098 offset);
8f2ece69
UD
2099
2100 if (error)
2101 {
2102 free (comm);
2103 return error;
2104 }
2105
2106 break;
2107
2108 case '\'':
2109 squoting = 1 - squoting;
32db86d5 2110 /* Fall through. */
8f2ece69 2111 default:
40a55d20 2112 comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]);
8f2ece69
UD
2113 if (comm == NULL)
2114 return WRDE_NOSPACE;
2115 }
28f540f4
RM
2116 }
2117
8f2ece69
UD
2118 /* Premature end */
2119 free (comm);
2120 return WRDE_SYNTAX;
2121}
28f540f4 2122
8f2ece69 2123static int
40a55d20 2124parse_dquote (char **word, size_t *word_length, size_t *max_length,
2bcf29ba
UD
2125 const char *words, size_t *offset, int flags,
2126 wordexp_t *pwordexp, const char * ifs, const char * ifs_white)
8f2ece69
UD
2127{
2128 /* We are poised just after a double-quote */
2129 int error;
2130
2131 for (; words[*offset]; ++(*offset))
2132 {
2133 switch (words[*offset])
2134 {
2135 case '"':
2136 return 0;
2137
2138 case '$':
40a55d20 2139 error = parse_dollars (word, word_length, max_length, words, offset,
9b3c7c3c 2140 flags, pwordexp, ifs, ifs_white, 1);
2bcf29ba
UD
2141 /* The ``1'' here is to tell parse_dollars not to
2142 * split the fields. It may need to, however ("$@").
8f2ece69
UD
2143 */
2144 if (error)
2145 return error;
28f540f4 2146
8f2ece69
UD
2147 break;
2148
2149 case '`':
8f2ece69 2150 ++(*offset);
40a55d20 2151 error = parse_backtick (word, word_length, max_length, words,
af69217f
UD
2152 offset, flags, NULL, NULL, NULL);
2153 /* The first NULL here is to tell parse_backtick not to
8f2ece69
UD
2154 * split the fields.
2155 */
2156 if (error)
2157 return error;
2158
2159 break;
2160
2161 case '\\':
40a55d20
UD
2162 error = parse_qtd_backslash (word, word_length, max_length, words,
2163 offset);
8f2ece69
UD
2164
2165 if (error)
2166 return error;
2167
2168 break;
2169
2170 default:
40a55d20 2171 *word = w_addchar (*word, word_length, max_length, words[*offset]);
8f2ece69
UD
2172 if (*word == NULL)
2173 return WRDE_NOSPACE;
2174 }
2175 }
2176
2177 /* Unterminated string */
2178 return WRDE_SYNTAX;
28f540f4
RM
2179}
2180
8f2ece69
UD
2181/*
2182 * wordfree() is to be called after pwordexp is finished with.
2183 */
28f540f4
RM
2184
2185void
8f2ece69 2186wordfree (wordexp_t *pwordexp)
28f540f4 2187{
8f2ece69
UD
2188
2189 /* wordexp can set pwordexp to NULL */
2190 if (pwordexp && pwordexp->we_wordv)
2191 {
2192 char **wordv = pwordexp->we_wordv;
2193
2194 for (wordv += pwordexp->we_offs; *wordv; ++wordv)
2195 free (*wordv);
2196
2197 free (pwordexp->we_wordv);
2198 pwordexp->we_wordv = NULL;
2199 }
2200}
c41f555e 2201libc_hidden_def (wordfree)
8f2ece69
UD
2202
2203/*
2204 * wordexp()
2205 */
2206
2207int
2208wordexp (const char *words, wordexp_t *pwordexp, int flags)
2209{
8f2ece69 2210 size_t words_offset;
e9fc7bbb
UD
2211 size_t word_length;
2212 size_t max_length;
2213 char *word = w_newword (&word_length, &max_length);
40a55d20 2214 int error;
8f2ece69
UD
2215 char *ifs;
2216 char ifs_white[4];
05d5dedc 2217 wordexp_t old_word = *pwordexp;
8f2ece69
UD
2218
2219 if (flags & WRDE_REUSE)
22bc7978
UD
2220 {
2221 /* Minimal implementation of WRDE_REUSE for now */
2222 wordfree (pwordexp);
05d5dedc 2223 old_word.we_wordv = NULL;
22bc7978 2224 }
8f2ece69 2225
05d5dedc 2226 if ((flags & WRDE_APPEND) == 0)
8f2ece69 2227 {
05d5dedc
UD
2228 pwordexp->we_wordc = 0;
2229
2230 if (flags & WRDE_DOOFFS)
33c3e20e 2231 {
05d5dedc
UD
2232 pwordexp->we_wordv = calloc (1 + pwordexp->we_offs, sizeof (char *));
2233 if (pwordexp->we_wordv == NULL)
2234 {
2235 error = WRDE_NOSPACE;
2236 goto do_error;
2237 }
33c3e20e 2238 }
05d5dedc 2239 else
33c3e20e 2240 {
05d5dedc
UD
2241 pwordexp->we_wordv = calloc (1, sizeof (char *));
2242 if (pwordexp->we_wordv == NULL)
2243 {
2244 error = WRDE_NOSPACE;
2245 goto do_error;
2246 }
8f2ece69 2247
05d5dedc
UD
2248 pwordexp->we_offs = 0;
2249 }
8f2ece69
UD
2250 }
2251
8f2ece69
UD
2252 /* Find out what the field separators are.
2253 * There are two types: whitespace and non-whitespace.
2254 */
2255 ifs = getenv ("IFS");
2256
16d620db 2257 if (ifs == NULL)
6796bc80
UD
2258 /* IFS unset - use <space><tab><newline>. */
2259 ifs = strcpy (ifs_white, " \t\n");
8f2ece69
UD
2260 else
2261 {
2262 char *ifsch = ifs;
2263 char *whch = ifs_white;
2264
2265 while (*ifsch != '\0')
af69217f 2266 {
16d620db 2267 if (*ifsch == ' ' || *ifsch == '\t' || *ifsch == '\n')
af69217f
UD
2268 {
2269 /* Whitespace IFS. See first whether it is already in our
2270 collection. */
2271 char *runp = ifs_white;
8f2ece69 2272
16d620db 2273 while (runp < whch && *runp != *ifsch)
af69217f 2274 ++runp;
8f2ece69 2275
af69217f
UD
2276 if (runp == whch)
2277 *whch++ = *ifsch;
2278 }
2279
3db52d94 2280 ++ifsch;
af69217f 2281 }
8f2ece69
UD
2282 *whch = '\0';
2283 }
2284
9b3c7c3c 2285 for (words_offset = 0 ; words[words_offset] ; ++words_offset)
8f2ece69
UD
2286 switch (words[words_offset])
2287 {
8f2ece69 2288 case '\\':
40a55d20
UD
2289 error = parse_backslash (&word, &word_length, &max_length, words,
2290 &words_offset);
8f2ece69
UD
2291
2292 if (error)
2293 goto do_error;
2294
2295 break;
2296
2297 case '$':
40a55d20 2298 error = parse_dollars (&word, &word_length, &max_length, words,
2bcf29ba 2299 &words_offset, flags, pwordexp, ifs, ifs_white,
9b3c7c3c 2300 0);
8f2ece69
UD
2301
2302 if (error)
2303 goto do_error;
2304
2305 break;
2306
2307 case '`':
8f2ece69 2308 ++words_offset;
40a55d20 2309 error = parse_backtick (&word, &word_length, &max_length, words,
af69217f
UD
2310 &words_offset, flags, pwordexp, ifs,
2311 ifs_white);
8f2ece69
UD
2312
2313 if (error)
2314 goto do_error;
2315
2316 break;
2317
2318 case '"':
2319 ++words_offset;
40a55d20 2320 error = parse_dquote (&word, &word_length, &max_length, words,
2bcf29ba 2321 &words_offset, flags, pwordexp, ifs, ifs_white);
8f2ece69
UD
2322
2323 if (error)
2324 goto do_error;
2325
a9c27b3e
UD
2326 if (!word_length)
2327 {
2328 error = w_addword (pwordexp, NULL);
2329
2330 if (error)
2331 return error;
2332 }
2333
8f2ece69
UD
2334 break;
2335
2336 case '\'':
2337 ++words_offset;
40a55d20
UD
2338 error = parse_squote (&word, &word_length, &max_length, words,
2339 &words_offset);
8f2ece69
UD
2340
2341 if (error)
2342 goto do_error;
2343
a9c27b3e
UD
2344 if (!word_length)
2345 {
2346 error = w_addword (pwordexp, NULL);
2347
2348 if (error)
2349 return error;
2350 }
2351
8f2ece69
UD
2352 break;
2353
2354 case '~':
40a55d20
UD
2355 error = parse_tilde (&word, &word_length, &max_length, words,
2356 &words_offset, pwordexp->we_wordc);
8f2ece69
UD
2357
2358 if (error)
2359 goto do_error;
2360
2361 break;
2362
2363 case '*':
2bcf29ba
UD
2364 case '[':
2365 case '?':
40a55d20 2366 error = parse_glob (&word, &word_length, &max_length, words,
af69217f 2367 &words_offset, flags, pwordexp, ifs, ifs_white);
8f2ece69
UD
2368
2369 if (error)
2370 goto do_error;
2371
2372 break;
2373
2374 default:
c06cc21c 2375 /* Is it a word separator? */
9b3c7c3c 2376 if (strchr (" \t", words[words_offset]) == NULL)
8f2ece69 2377 {
c06cc21c
UD
2378 char ch = words[words_offset];
2379
2380 /* Not a word separator -- but is it a valid word char? */
2381 if (strchr ("\n|&;<>(){}", ch))
6796bc80
UD
2382 {
2383 /* Fail */
bac660f8
UD
2384 error = WRDE_BADCHAR;
2385 goto do_error;
6796bc80
UD
2386 }
2387
8f2ece69 2388 /* "Ordinary" character -- add it to word */
40a55d20 2389 word = w_addchar (word, &word_length, &max_length,
c06cc21c 2390 ch);
8f2ece69
UD
2391 if (word == NULL)
2392 {
2393 error = WRDE_NOSPACE;
2394 goto do_error;
2395 }
2396
2397 break;
2398 }
2399
c06cc21c 2400 /* If a word has been delimited, add it to the list. */
40a55d20 2401 if (word != NULL)
8f2ece69 2402 {
9b3c7c3c 2403 error = w_addword (pwordexp, word);
c06cc21c
UD
2404 if (error)
2405 goto do_error;
8f2ece69
UD
2406 }
2407
a8125d85
UD
2408 word = w_newword (&word_length, &max_length);
2409 }
8f2ece69 2410
9b3c7c3c
UD
2411 /* End of string */
2412
2413 /* There was a word separator at the end */
2414 if (word == NULL) /* i.e. w_newword */
2415 return 0;
2416
2417 /* There was no field separator at the end */
2418 return w_addword (pwordexp, word);
8f2ece69
UD
2419
2420do_error:
2421 /* Error:
2bcf29ba 2422 * free memory used (unless error is WRDE_NOSPACE), and
05d5dedc 2423 * set pwordexp members back to what they were.
8f2ece69 2424 */
2bcf29ba 2425
ecf359c6 2426 free (word);
8f2ece69 2427
c06cc21c
UD
2428 if (error == WRDE_NOSPACE)
2429 return WRDE_NOSPACE;
2430
05d5dedc
UD
2431 if ((flags & WRDE_APPEND) == 0)
2432 wordfree (pwordexp);
2433
2434 *pwordexp = old_word;
8f2ece69 2435 return error;
28f540f4 2436}