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