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