S390: Optimize strncpy and wcsncpy.
[glibc.git] / posix / wordexp.c
blobe711d4335521fbb24076386d39862f0842c1a502
1 /* POSIX.2 wordexp implementation.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Tim Waugh <tim@cyberelk.demon.co.uk>.
6 The GNU C Library is free software; you can redistribute it and/or
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.
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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <alloca.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <fnmatch.h>
25 #include <glob.h>
26 #include <libintl.h>
27 #include <paths.h>
28 #include <pwd.h>
29 #include <signal.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <unistd.h>
41 #include <wchar.h>
42 #include <wordexp.h>
43 #include <kernel-features.h>
45 #include <bits/libc-lock.h>
46 #include <_itoa.h>
48 /* Undefine the following line for the production version. */
49 /* #define NDEBUG 1 */
50 #include <assert.h>
52 /* Get some device information. */
53 #include <device-nrs.h>
56 * This is a recursive-descent-style word expansion routine.
59 /* These variables are defined and initialized in the startup code. */
60 extern int __libc_argc attribute_hidden;
61 extern char **__libc_argv attribute_hidden;
63 /* Some forward declarations */
64 static int parse_dollars (char **word, size_t *word_length, size_t *max_length,
65 const char *words, size_t *offset, int flags,
66 wordexp_t *pwordexp, const char *ifs,
67 const char *ifs_white, int quoted)
68 internal_function;
69 static int parse_backtick (char **word, size_t *word_length,
70 size_t *max_length, const char *words,
71 size_t *offset, int flags, wordexp_t *pwordexp,
72 const char *ifs, const char *ifs_white)
73 internal_function;
74 static int parse_dquote (char **word, size_t *word_length, size_t *max_length,
75 const char *words, size_t *offset, int flags,
76 wordexp_t *pwordexp, const char *ifs,
77 const char *ifs_white)
78 internal_function;
79 static int eval_expr (char *expr, long int *result) internal_function;
81 /* The w_*() functions manipulate word lists. */
83 #define W_CHUNK (100)
85 /* Result of w_newword will be ignored if it's the last word. */
86 static inline char *
87 w_newword (size_t *actlen, size_t *maxlen)
89 *actlen = *maxlen = 0;
90 return NULL;
93 static char *
94 w_addchar (char *buffer, size_t *actlen, size_t *maxlen, char ch)
95 /* (lengths exclude trailing zero) */
97 /* Add a character to the buffer, allocating room for it if needed. */
99 if (*actlen == *maxlen)
101 char *old_buffer = buffer;
102 assert (buffer == NULL || *maxlen != 0);
103 *maxlen += W_CHUNK;
104 buffer = (char *) realloc (buffer, 1 + *maxlen);
106 if (buffer == NULL)
107 free (old_buffer);
110 if (buffer != NULL)
112 buffer[*actlen] = ch;
113 buffer[++(*actlen)] = '\0';
116 return buffer;
119 static char *
120 internal_function
121 w_addmem (char *buffer, size_t *actlen, size_t *maxlen, const char *str,
122 size_t len)
124 /* Add a string to the buffer, allocating room for it if needed.
126 if (*actlen + len > *maxlen)
128 char *old_buffer = buffer;
129 assert (buffer == NULL || *maxlen != 0);
130 *maxlen += MAX (2 * len, W_CHUNK);
131 buffer = realloc (old_buffer, 1 + *maxlen);
133 if (buffer == NULL)
134 free (old_buffer);
137 if (buffer != NULL)
139 *((char *) __mempcpy (&buffer[*actlen], str, len)) = '\0';
140 *actlen += len;
143 return buffer;
146 static char *
147 internal_function
148 w_addstr (char *buffer, size_t *actlen, size_t *maxlen, const char *str)
149 /* (lengths exclude trailing zero) */
151 /* Add a string to the buffer, allocating room for it if needed.
153 size_t len;
155 assert (str != NULL); /* w_addstr only called from this file */
156 len = strlen (str);
158 return w_addmem (buffer, actlen, maxlen, str, len);
161 static int
162 internal_function
163 w_addword (wordexp_t *pwordexp, char *word)
165 /* Add a word to the wordlist */
166 size_t num_p;
167 char **new_wordv;
168 bool allocated = false;
170 /* Internally, NULL acts like "". Convert NULLs to "" before
171 * the caller sees them.
173 if (word == NULL)
175 word = __strdup ("");
176 if (word == NULL)
177 goto no_space;
178 allocated = true;
181 num_p = 2 + pwordexp->we_wordc + pwordexp->we_offs;
182 new_wordv = realloc (pwordexp->we_wordv, sizeof (char *) * num_p);
183 if (new_wordv != NULL)
185 pwordexp->we_wordv = new_wordv;
186 pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc++] = word;
187 pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc] = NULL;
188 return 0;
191 if (allocated)
192 free (word);
194 no_space:
195 return WRDE_NOSPACE;
198 /* The parse_*() functions should leave *offset being the offset in 'words'
199 * to the last character processed.
202 static int
203 internal_function
204 parse_backslash (char **word, size_t *word_length, size_t *max_length,
205 const char *words, size_t *offset)
207 /* We are poised _at_ a backslash, not in quotes */
209 switch (words[1 + *offset])
211 case 0:
212 /* Backslash is last character of input words */
213 return WRDE_SYNTAX;
215 case '\n':
216 ++(*offset);
217 break;
219 default:
220 *word = w_addchar (*word, word_length, max_length, words[1 + *offset]);
221 if (*word == NULL)
222 return WRDE_NOSPACE;
224 ++(*offset);
225 break;
228 return 0;
231 static int
232 internal_function
233 parse_qtd_backslash (char **word, size_t *word_length, size_t *max_length,
234 const char *words, size_t *offset)
236 /* We are poised _at_ a backslash, inside quotes */
238 switch (words[1 + *offset])
240 case 0:
241 /* Backslash is last character of input words */
242 return WRDE_SYNTAX;
244 case '\n':
245 ++(*offset);
246 break;
248 case '$':
249 case '`':
250 case '"':
251 case '\\':
252 *word = w_addchar (*word, word_length, max_length, words[1 + *offset]);
253 if (*word == NULL)
254 return WRDE_NOSPACE;
256 ++(*offset);
257 break;
259 default:
260 *word = w_addchar (*word, word_length, max_length, words[*offset]);
261 if (*word != NULL)
262 *word = w_addchar (*word, word_length, max_length, words[1 + *offset]);
264 if (*word == NULL)
265 return WRDE_NOSPACE;
267 ++(*offset);
268 break;
271 return 0;
274 static int
275 internal_function
276 parse_tilde (char **word, size_t *word_length, size_t *max_length,
277 const char *words, size_t *offset, size_t wordc)
279 /* We are poised _at_ a tilde */
280 size_t i;
282 if (*word_length != 0)
284 if (!((*word)[*word_length - 1] == '=' && wordc == 0))
286 if (!((*word)[*word_length - 1] == ':'
287 && strchr (*word, '=') && wordc == 0))
289 *word = w_addchar (*word, word_length, max_length, '~');
290 return *word ? 0 : WRDE_NOSPACE;
295 for (i = 1 + *offset; words[i]; i++)
297 if (words[i] == ':' || words[i] == '/' || words[i] == ' ' ||
298 words[i] == '\t' || words[i] == 0 )
299 break;
301 if (words[i] == '\\')
303 *word = w_addchar (*word, word_length, max_length, '~');
304 return *word ? 0 : WRDE_NOSPACE;
308 if (i == 1 + *offset)
310 /* Tilde appears on its own */
311 uid_t uid;
312 struct passwd pwd, *tpwd;
313 int buflen = 1000;
314 char* home;
315 char* buffer;
316 int result;
318 /* POSIX.2 says ~ expands to $HOME and if HOME is unset the
319 results are unspecified. We do a lookup on the uid if
320 HOME is unset. */
322 home = getenv ("HOME");
323 if (home != NULL)
325 *word = w_addstr (*word, word_length, max_length, home);
326 if (*word == NULL)
327 return WRDE_NOSPACE;
329 else
331 uid = __getuid ();
332 buffer = __alloca (buflen);
334 while ((result = __getpwuid_r (uid, &pwd, buffer, buflen, &tpwd)) != 0
335 && errno == ERANGE)
336 buffer = extend_alloca (buffer, buflen, buflen + 1000);
338 if (result == 0 && tpwd != NULL && pwd.pw_dir != NULL)
340 *word = w_addstr (*word, word_length, max_length, pwd.pw_dir);
341 if (*word == NULL)
342 return WRDE_NOSPACE;
344 else
346 *word = w_addchar (*word, word_length, max_length, '~');
347 if (*word == NULL)
348 return WRDE_NOSPACE;
352 else
354 /* Look up user name in database to get home directory */
355 char *user = strndupa (&words[1 + *offset], i - (1 + *offset));
356 struct passwd pwd, *tpwd;
357 int buflen = 1000;
358 char* buffer = __alloca (buflen);
359 int result;
361 while ((result = __getpwnam_r (user, &pwd, buffer, buflen, &tpwd)) != 0
362 && errno == ERANGE)
363 buffer = extend_alloca (buffer, buflen, buflen + 1000);
365 if (result == 0 && tpwd != NULL && pwd.pw_dir)
366 *word = w_addstr (*word, word_length, max_length, pwd.pw_dir);
367 else
369 /* (invalid login name) */
370 *word = w_addchar (*word, word_length, max_length, '~');
371 if (*word != NULL)
372 *word = w_addstr (*word, word_length, max_length, user);
375 *offset = i - 1;
377 return *word ? 0 : WRDE_NOSPACE;
381 static int
382 internal_function
383 do_parse_glob (const char *glob_word, char **word, size_t *word_length,
384 size_t *max_length, wordexp_t *pwordexp, const char *ifs,
385 const char *ifs_white)
387 int error;
388 unsigned int match;
389 glob_t globbuf;
391 error = glob (glob_word, GLOB_NOCHECK, NULL, &globbuf);
393 if (error != 0)
395 /* We can only run into memory problems. */
396 assert (error == GLOB_NOSPACE);
397 return WRDE_NOSPACE;
400 if (ifs && !*ifs)
402 /* No field splitting allowed. */
403 assert (globbuf.gl_pathv[0] != NULL);
404 *word = w_addstr (*word, word_length, max_length, globbuf.gl_pathv[0]);
405 for (match = 1; match < globbuf.gl_pathc && *word != NULL; ++match)
407 *word = w_addchar (*word, word_length, max_length, ' ');
408 if (*word != NULL)
409 *word = w_addstr (*word, word_length, max_length,
410 globbuf.gl_pathv[match]);
413 globfree (&globbuf);
414 return *word ? 0 : WRDE_NOSPACE;
417 assert (ifs == NULL || *ifs != '\0');
418 if (*word != NULL)
420 free (*word);
421 *word = w_newword (word_length, max_length);
424 for (match = 0; match < globbuf.gl_pathc; ++match)
426 char *matching_word = __strdup (globbuf.gl_pathv[match]);
427 if (matching_word == NULL || w_addword (pwordexp, matching_word))
429 globfree (&globbuf);
430 return WRDE_NOSPACE;
434 globfree (&globbuf);
435 return 0;
438 static int
439 internal_function
440 parse_glob (char **word, size_t *word_length, size_t *max_length,
441 const char *words, size_t *offset, int flags,
442 wordexp_t *pwordexp, const char *ifs, const char *ifs_white)
444 /* We are poised just after a '*', a '[' or a '?'. */
445 int error = WRDE_NOSPACE;
446 int quoted = 0; /* 1 if singly-quoted, 2 if doubly */
447 size_t i;
448 wordexp_t glob_list; /* List of words to glob */
450 glob_list.we_wordc = 0;
451 glob_list.we_wordv = NULL;
452 glob_list.we_offs = 0;
453 for (; words[*offset] != '\0'; ++*offset)
455 if (strchr (ifs, words[*offset]) != NULL)
456 /* Reached IFS */
457 break;
459 /* Sort out quoting */
460 if (words[*offset] == '\'')
462 if (quoted == 0)
464 quoted = 1;
465 continue;
467 else if (quoted == 1)
469 quoted = 0;
470 continue;
473 else if (words[*offset] == '"')
475 if (quoted == 0)
477 quoted = 2;
478 continue;
480 else if (quoted == 2)
482 quoted = 0;
483 continue;
487 /* Sort out other special characters */
488 if (quoted != 1 && words[*offset] == '$')
490 error = parse_dollars (word, word_length, max_length, words,
491 offset, flags, &glob_list, ifs, ifs_white,
492 quoted == 2);
493 if (error)
494 goto tidy_up;
496 continue;
498 else if (words[*offset] == '\\')
500 if (quoted)
501 error = parse_qtd_backslash (word, word_length, max_length,
502 words, offset);
503 else
504 error = parse_backslash (word, word_length, max_length,
505 words, offset);
507 if (error)
508 goto tidy_up;
510 continue;
513 *word = w_addchar (*word, word_length, max_length, words[*offset]);
514 if (*word == NULL)
515 goto tidy_up;
518 /* Don't forget to re-parse the character we stopped at. */
519 --*offset;
521 /* Glob the words */
522 error = w_addword (&glob_list, *word);
523 *word = w_newword (word_length, max_length);
524 for (i = 0; error == 0 && i < glob_list.we_wordc; i++)
525 error = do_parse_glob (glob_list.we_wordv[i], word, word_length,
526 max_length, pwordexp, ifs, ifs_white);
528 /* Now tidy up */
529 tidy_up:
530 wordfree (&glob_list);
531 return error;
534 static int
535 internal_function
536 parse_squote (char **word, size_t *word_length, size_t *max_length,
537 const char *words, size_t *offset)
539 /* We are poised just after a single quote */
540 for (; words[*offset]; ++(*offset))
542 if (words[*offset] != '\'')
544 *word = w_addchar (*word, word_length, max_length, words[*offset]);
545 if (*word == NULL)
546 return WRDE_NOSPACE;
548 else return 0;
551 /* Unterminated string */
552 return WRDE_SYNTAX;
555 /* Functions to evaluate an arithmetic expression */
556 static int
557 internal_function
558 eval_expr_val (char **expr, long int *result)
560 char *digit;
562 /* Skip white space */
563 for (digit = *expr; digit && *digit && isspace (*digit); ++digit);
565 if (*digit == '(')
567 /* Scan for closing paren */
568 for (++digit; **expr && **expr != ')'; ++(*expr));
570 /* Is there one? */
571 if (!**expr)
572 return WRDE_SYNTAX;
574 *(*expr)++ = 0;
576 if (eval_expr (digit, result))
577 return WRDE_SYNTAX;
579 return 0;
582 /* POSIX requires that decimal, octal, and hexadecimal constants are
583 recognized. Therefore we pass 0 as the third parameter to strtol. */
584 *result = strtol (digit, expr, 0);
585 if (digit == *expr)
586 return WRDE_SYNTAX;
588 return 0;
591 static int
592 internal_function
593 eval_expr_multdiv (char **expr, long int *result)
595 long int arg;
597 /* Read a Value */
598 if (eval_expr_val (expr, result) != 0)
599 return WRDE_SYNTAX;
601 while (**expr)
603 /* Skip white space */
604 for (; *expr && **expr && isspace (**expr); ++(*expr));
606 if (**expr == '*')
608 ++(*expr);
609 if (eval_expr_val (expr, &arg) != 0)
610 return WRDE_SYNTAX;
612 *result *= arg;
614 else if (**expr == '/')
616 ++(*expr);
617 if (eval_expr_val (expr, &arg) != 0)
618 return WRDE_SYNTAX;
620 /* Division by zero or integer overflow. */
621 if (arg == 0 || (arg == -1 && *result == LONG_MIN))
622 return WRDE_SYNTAX;
624 *result /= arg;
626 else break;
629 return 0;
632 static int
633 internal_function
634 eval_expr (char *expr, long int *result)
636 long int arg;
638 /* Read a Multdiv */
639 if (eval_expr_multdiv (&expr, result) != 0)
640 return WRDE_SYNTAX;
642 while (*expr)
644 /* Skip white space */
645 for (; expr && *expr && isspace (*expr); ++expr);
647 if (*expr == '+')
649 ++expr;
650 if (eval_expr_multdiv (&expr, &arg) != 0)
651 return WRDE_SYNTAX;
653 *result += arg;
655 else if (*expr == '-')
657 ++expr;
658 if (eval_expr_multdiv (&expr, &arg) != 0)
659 return WRDE_SYNTAX;
661 *result -= arg;
663 else break;
666 return 0;
669 static int
670 internal_function
671 parse_arith (char **word, size_t *word_length, size_t *max_length,
672 const char *words, size_t *offset, int flags, int bracket)
674 /* We are poised just after "$((" or "$[" */
675 int error;
676 int paren_depth = 1;
677 size_t expr_length;
678 size_t expr_maxlen;
679 char *expr;
681 expr = w_newword (&expr_length, &expr_maxlen);
682 for (; words[*offset]; ++(*offset))
684 switch (words[*offset])
686 case '$':
687 error = parse_dollars (&expr, &expr_length, &expr_maxlen,
688 words, offset, flags, NULL, NULL, NULL, 1);
689 /* The ``1'' here is to tell parse_dollars not to
690 * split the fields.
692 if (error)
694 free (expr);
695 return error;
697 break;
699 case '`':
700 (*offset)++;
701 error = parse_backtick (&expr, &expr_length, &expr_maxlen,
702 words, offset, flags, NULL, NULL, NULL);
703 /* The first NULL here is to tell parse_backtick not to
704 * split the fields.
706 if (error)
708 free (expr);
709 return error;
711 break;
713 case '\\':
714 error = parse_qtd_backslash (&expr, &expr_length, &expr_maxlen,
715 words, offset);
716 if (error)
718 free (expr);
719 return error;
721 /* I think that a backslash within an
722 * arithmetic expansion is bound to
723 * cause an error sooner or later anyway though.
725 break;
727 case ')':
728 if (--paren_depth == 0)
730 char result[21]; /* 21 = ceil(log10(2^64)) + 1 */
731 long int numresult = 0;
732 long long int convertme;
734 if (bracket || words[1 + *offset] != ')')
736 free (expr);
737 return WRDE_SYNTAX;
740 ++(*offset);
742 /* Go - evaluate. */
743 if (*expr && eval_expr (expr, &numresult) != 0)
745 free (expr);
746 return WRDE_SYNTAX;
749 if (numresult < 0)
751 convertme = -numresult;
752 *word = w_addchar (*word, word_length, max_length, '-');
753 if (!*word)
755 free (expr);
756 return WRDE_NOSPACE;
759 else
760 convertme = numresult;
762 result[20] = '\0';
763 *word = w_addstr (*word, word_length, max_length,
764 _itoa (convertme, &result[20], 10, 0));
765 free (expr);
766 return *word ? 0 : WRDE_NOSPACE;
768 expr = w_addchar (expr, &expr_length, &expr_maxlen, words[*offset]);
769 if (expr == NULL)
770 return WRDE_NOSPACE;
772 break;
774 case ']':
775 if (bracket && paren_depth == 1)
777 char result[21]; /* 21 = ceil(log10(2^64)) + 1 */
778 long int numresult = 0;
780 /* Go - evaluate. */
781 if (*expr && eval_expr (expr, &numresult) != 0)
783 free (expr);
784 return WRDE_SYNTAX;
787 result[20] = '\0';
788 *word = w_addstr (*word, word_length, max_length,
789 _itoa_word (numresult, &result[20], 10, 0));
790 free (expr);
791 return *word ? 0 : WRDE_NOSPACE;
794 free (expr);
795 return WRDE_SYNTAX;
797 case '\n':
798 case ';':
799 case '{':
800 case '}':
801 free (expr);
802 return WRDE_BADCHAR;
804 case '(':
805 ++paren_depth;
806 default:
807 expr = w_addchar (expr, &expr_length, &expr_maxlen, words[*offset]);
808 if (expr == NULL)
809 return WRDE_NOSPACE;
813 /* Premature end */
814 free (expr);
815 return WRDE_SYNTAX;
818 /* Function called by child process in exec_comm() */
819 static inline void
820 internal_function __attribute__ ((always_inline))
821 exec_comm_child (char *comm, int *fildes, int showerr, int noexec)
823 const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
825 /* Execute the command, or just check syntax? */
826 if (noexec)
827 args[1] = "-nc";
829 /* Redirect output. */
830 if (__glibc_likely (fildes[1] != STDOUT_FILENO))
832 __dup2 (fildes[1], STDOUT_FILENO);
833 __close (fildes[1]);
835 else
837 #ifdef O_CLOEXEC
838 /* Reset the close-on-exec flag (if necessary). */
839 # ifndef __ASSUME_PIPE2
840 if (__have_pipe2 > 0)
841 # endif
842 __fcntl (fildes[1], F_SETFD, 0);
843 #endif
846 /* Redirect stderr to /dev/null if we have to. */
847 if (showerr == 0)
849 struct stat64 st;
850 int fd;
851 __close (STDERR_FILENO);
852 fd = __open (_PATH_DEVNULL, O_WRONLY);
853 if (fd >= 0 && fd != STDERR_FILENO)
855 __dup2 (fd, STDERR_FILENO);
856 __close (fd);
858 /* Be paranoid. Check that we actually opened the /dev/null
859 device. */
860 if (__builtin_expect (__fxstat64 (_STAT_VER, STDERR_FILENO, &st), 0) != 0
861 || __builtin_expect (S_ISCHR (st.st_mode), 1) == 0
862 #if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
863 || st.st_rdev != makedev (DEV_NULL_MAJOR, DEV_NULL_MINOR)
864 #endif
866 /* It's not the /dev/null device. Stop right here. The
867 problem is: how do we stop? We use _exit() with an
868 hopefully unusual exit code. */
869 _exit (90);
872 /* Make sure the subshell doesn't field-split on our behalf. */
873 __unsetenv ("IFS");
875 __close (fildes[0]);
876 __execve (_PATH_BSHELL, (char *const *) args, __environ);
878 /* Bad. What now? */
879 abort ();
882 /* Function to execute a command and retrieve the results */
883 /* pwordexp contains NULL if field-splitting is forbidden */
884 static int
885 internal_function
886 exec_comm (char *comm, char **word, size_t *word_length, size_t *max_length,
887 int flags, wordexp_t *pwordexp, const char *ifs,
888 const char *ifs_white)
890 int fildes[2];
891 #define bufsize 128
892 int buflen;
893 int i;
894 int status = 0;
895 size_t maxnewlines = 0;
896 char buffer[bufsize];
897 pid_t pid;
898 int noexec = 0;
900 /* Do nothing if command substitution should not succeed. */
901 if (flags & WRDE_NOCMD)
902 return WRDE_CMDSUB;
904 /* Don't fork() unless necessary */
905 if (!comm || !*comm)
906 return 0;
908 #ifdef O_CLOEXEC
909 # ifndef __ASSUME_PIPE2
910 if (__have_pipe2 >= 0)
911 # endif
913 int r = __pipe2 (fildes, O_CLOEXEC);
914 # ifndef __ASSUME_PIPE2
915 if (__have_pipe2 == 0)
916 __have_pipe2 = r != -1 || errno != ENOSYS ? 1 : -1;
918 if (__have_pipe2 > 0)
919 # endif
920 if (r < 0)
921 /* Bad */
922 return WRDE_NOSPACE;
924 #endif
925 #ifndef __ASSUME_PIPE2
926 # ifdef O_CLOEXEC
927 if (__have_pipe2 < 0)
928 # endif
929 if (__pipe (fildes) < 0)
930 /* Bad */
931 return WRDE_NOSPACE;
932 #endif
934 again:
935 if ((pid = __fork ()) < 0)
937 /* Bad */
938 __close (fildes[0]);
939 __close (fildes[1]);
940 return WRDE_NOSPACE;
943 if (pid == 0)
944 exec_comm_child (comm, fildes, noexec ? 0 : flags & WRDE_SHOWERR, noexec);
946 /* Parent */
948 /* If we are just testing the syntax, only wait. */
949 if (noexec)
950 return (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) == pid
951 && status != 0) ? WRDE_SYNTAX : 0;
953 __close (fildes[1]);
954 fildes[1] = -1;
956 if (!pwordexp)
957 /* Quoted - no field splitting */
959 while (1)
961 if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
962 bufsize))) < 1)
964 /* If read returned 0 then the process has closed its
965 stdout. Don't use WNOHANG in that case to avoid busy
966 looping until the process eventually exits. */
967 if (TEMP_FAILURE_RETRY (__waitpid (pid, &status,
968 buflen == 0 ? 0 : WNOHANG))
969 == 0)
970 continue;
971 if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
972 bufsize))) < 1)
973 break;
976 maxnewlines += buflen;
978 *word = w_addmem (*word, word_length, max_length, buffer, buflen);
979 if (*word == NULL)
980 goto no_space;
983 else
984 /* Not quoted - split fields */
986 int copying = 0;
987 /* 'copying' is:
988 * 0 when searching for first character in a field not IFS white space
989 * 1 when copying the text of a field
990 * 2 when searching for possible non-whitespace IFS
991 * 3 when searching for non-newline after copying field
994 while (1)
996 if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
997 bufsize))) < 1)
999 /* If read returned 0 then the process has closed its
1000 stdout. Don't use WNOHANG in that case to avoid busy
1001 looping until the process eventually exits. */
1002 if (TEMP_FAILURE_RETRY (__waitpid (pid, &status,
1003 buflen == 0 ? 0 : WNOHANG))
1004 == 0)
1005 continue;
1006 if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
1007 bufsize))) < 1)
1008 break;
1011 for (i = 0; i < buflen; ++i)
1013 if (strchr (ifs, buffer[i]) != NULL)
1015 /* Current character is IFS */
1016 if (strchr (ifs_white, buffer[i]) == NULL)
1018 /* Current character is IFS but not whitespace */
1019 if (copying == 2)
1021 /* current character
1024 * eg: text<space><comma><space>moretext
1026 * So, strip whitespace IFS (like at the start)
1028 copying = 0;
1029 continue;
1032 copying = 0;
1033 /* fall through and delimit field.. */
1035 else
1037 if (buffer[i] == '\n')
1039 /* Current character is (IFS) newline */
1041 /* If copying a field, this is the end of it,
1042 but maybe all that's left is trailing newlines.
1043 So start searching for a non-newline. */
1044 if (copying == 1)
1045 copying = 3;
1047 continue;
1049 else
1051 /* Current character is IFS white space, but
1052 not a newline */
1054 /* If not either copying a field or searching
1055 for non-newline after a field, ignore it */
1056 if (copying != 1 && copying != 3)
1057 continue;
1059 /* End of field (search for non-ws IFS afterwards) */
1060 copying = 2;
1064 /* First IFS white space (non-newline), or IFS non-whitespace.
1065 * Delimit the field. Nulls are converted by w_addword. */
1066 if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
1067 goto no_space;
1069 *word = w_newword (word_length, max_length);
1071 maxnewlines = 0;
1072 /* fall back round the loop.. */
1074 else
1076 /* Not IFS character */
1078 if (copying == 3)
1080 /* Nothing but (IFS) newlines since the last field,
1081 so delimit it here before starting new word */
1082 if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
1083 goto no_space;
1085 *word = w_newword (word_length, max_length);
1088 copying = 1;
1090 if (buffer[i] == '\n') /* happens if newline not in IFS */
1091 maxnewlines++;
1092 else
1093 maxnewlines = 0;
1095 *word = w_addchar (*word, word_length, max_length,
1096 buffer[i]);
1097 if (*word == NULL)
1098 goto no_space;
1104 /* Chop off trailing newlines (required by POSIX.2) */
1105 /* Ensure we don't go back further than the beginning of the
1106 substitution (i.e. remove maxnewlines bytes at most) */
1107 while (maxnewlines-- != 0 &&
1108 *word_length > 0 && (*word)[*word_length - 1] == '\n')
1110 (*word)[--*word_length] = '\0';
1112 /* If the last word was entirely newlines, turn it into a new word
1113 * which can be ignored if there's nothing following it. */
1114 if (*word_length == 0)
1116 free (*word);
1117 *word = w_newword (word_length, max_length);
1118 break;
1122 __close (fildes[0]);
1123 fildes[0] = -1;
1125 /* Check for syntax error (re-execute but with "-n" flag) */
1126 if (buflen < 1 && status != 0)
1128 noexec = 1;
1129 goto again;
1132 return 0;
1134 no_space:
1135 __kill (pid, SIGKILL);
1136 TEMP_FAILURE_RETRY (__waitpid (pid, NULL, 0));
1137 __close (fildes[0]);
1138 return WRDE_NOSPACE;
1141 static int
1142 internal_function
1143 parse_comm (char **word, size_t *word_length, size_t *max_length,
1144 const char *words, size_t *offset, int flags, wordexp_t *pwordexp,
1145 const char *ifs, const char *ifs_white)
1147 /* We are poised just after "$(" */
1148 int paren_depth = 1;
1149 int error = 0;
1150 int quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1151 size_t comm_length;
1152 size_t comm_maxlen;
1153 char *comm = w_newword (&comm_length, &comm_maxlen);
1155 for (; words[*offset]; ++(*offset))
1157 switch (words[*offset])
1159 case '\'':
1160 if (quoted == 0)
1161 quoted = 1;
1162 else if (quoted == 1)
1163 quoted = 0;
1165 break;
1167 case '"':
1168 if (quoted == 0)
1169 quoted = 2;
1170 else if (quoted == 2)
1171 quoted = 0;
1173 break;
1175 case ')':
1176 if (!quoted && --paren_depth == 0)
1178 /* Go -- give script to the shell */
1179 if (comm)
1181 #ifdef __libc_ptf_call
1182 /* We do not want the exec_comm call to be cut short
1183 by a thread cancellation since cleanup is very
1184 ugly. Therefore disable cancellation for
1185 now. */
1186 // XXX Ideally we do want the thread being cancelable.
1187 // XXX If demand is there we'll change it.
1188 int state = PTHREAD_CANCEL_ENABLE;
1189 __libc_ptf_call (pthread_setcancelstate,
1190 (PTHREAD_CANCEL_DISABLE, &state), 0);
1191 #endif
1193 error = exec_comm (comm, word, word_length, max_length,
1194 flags, pwordexp, ifs, ifs_white);
1196 #ifdef __libc_ptf_call
1197 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
1198 #endif
1200 free (comm);
1203 return error;
1206 /* This is just part of the script */
1207 break;
1209 case '(':
1210 if (!quoted)
1211 ++paren_depth;
1214 comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]);
1215 if (comm == NULL)
1216 return WRDE_NOSPACE;
1219 /* Premature end. */
1220 free (comm);
1222 return WRDE_SYNTAX;
1225 #define CHAR_IN_SET(ch, char_set) \
1226 (memchr (char_set "", ch, sizeof (char_set) - 1) != NULL)
1228 static int
1229 internal_function
1230 parse_param (char **word, size_t *word_length, size_t *max_length,
1231 const char *words, size_t *offset, int flags, wordexp_t *pwordexp,
1232 const char *ifs, const char *ifs_white, int quoted)
1234 /* We are poised just after "$" */
1235 enum action
1237 ACT_NONE,
1238 ACT_RP_SHORT_LEFT = '#',
1239 ACT_RP_LONG_LEFT = 'L',
1240 ACT_RP_SHORT_RIGHT = '%',
1241 ACT_RP_LONG_RIGHT = 'R',
1242 ACT_NULL_ERROR = '?',
1243 ACT_NULL_SUBST = '-',
1244 ACT_NONNULL_SUBST = '+',
1245 ACT_NULL_ASSIGN = '='
1247 size_t env_length;
1248 size_t env_maxlen;
1249 size_t pat_length;
1250 size_t pat_maxlen;
1251 size_t start = *offset;
1252 char *env;
1253 char *pattern;
1254 char *value = NULL;
1255 enum action action = ACT_NONE;
1256 int depth = 0;
1257 int colon_seen = 0;
1258 int seen_hash = 0;
1259 int free_value = 0;
1260 int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1261 int error;
1262 int special = 0;
1263 char buffer[21];
1264 int brace = words[*offset] == '{';
1266 env = w_newword (&env_length, &env_maxlen);
1267 pattern = w_newword (&pat_length, &pat_maxlen);
1269 if (brace)
1270 ++*offset;
1272 /* First collect the parameter name. */
1274 if (words[*offset] == '#')
1276 seen_hash = 1;
1277 if (!brace)
1278 goto envsubst;
1279 ++*offset;
1282 if (isalpha (words[*offset]) || words[*offset] == '_')
1284 /* Normal parameter name. */
1287 env = w_addchar (env, &env_length, &env_maxlen,
1288 words[*offset]);
1289 if (env == NULL)
1290 goto no_space;
1292 while (isalnum (words[++*offset]) || words[*offset] == '_');
1294 else if (isdigit (words[*offset]))
1296 /* Numeric parameter name. */
1297 special = 1;
1300 env = w_addchar (env, &env_length, &env_maxlen,
1301 words[*offset]);
1302 if (env == NULL)
1303 goto no_space;
1304 if (!brace)
1305 goto envsubst;
1307 while (isdigit(words[++*offset]));
1309 else if (CHAR_IN_SET (words[*offset], "*@$"))
1311 /* Special parameter. */
1312 special = 1;
1313 env = w_addchar (env, &env_length, &env_maxlen,
1314 words[*offset]);
1315 if (env == NULL)
1316 goto no_space;
1317 ++*offset;
1319 else
1321 if (brace)
1322 goto syntax;
1325 if (brace)
1327 /* Check for special action to be applied to the value. */
1328 switch (words[*offset])
1330 case '}':
1331 /* Evaluate. */
1332 goto envsubst;
1334 case '#':
1335 action = ACT_RP_SHORT_LEFT;
1336 if (words[1 + *offset] == '#')
1338 ++*offset;
1339 action = ACT_RP_LONG_LEFT;
1341 break;
1343 case '%':
1344 action = ACT_RP_SHORT_RIGHT;
1345 if (words[1 + *offset] == '%')
1347 ++*offset;
1348 action = ACT_RP_LONG_RIGHT;
1350 break;
1352 case ':':
1353 if (!CHAR_IN_SET (words[1 + *offset], "-=?+"))
1354 goto syntax;
1356 colon_seen = 1;
1357 action = words[++*offset];
1358 break;
1360 case '-':
1361 case '=':
1362 case '?':
1363 case '+':
1364 action = words[*offset];
1365 break;
1367 default:
1368 goto syntax;
1371 /* Now collect the pattern, but don't expand it yet. */
1372 ++*offset;
1373 for (; words[*offset]; ++(*offset))
1375 switch (words[*offset])
1377 case '{':
1378 if (!pattern_is_quoted)
1379 ++depth;
1380 break;
1382 case '}':
1383 if (!pattern_is_quoted)
1385 if (depth == 0)
1386 goto envsubst;
1387 --depth;
1389 break;
1391 case '\\':
1392 if (pattern_is_quoted)
1393 /* Quoted; treat as normal character. */
1394 break;
1396 /* Otherwise, it's an escape: next character is literal. */
1397 if (words[++*offset] == '\0')
1398 goto syntax;
1400 pattern = w_addchar (pattern, &pat_length, &pat_maxlen, '\\');
1401 if (pattern == NULL)
1402 goto no_space;
1404 break;
1406 case '\'':
1407 if (pattern_is_quoted == 0)
1408 pattern_is_quoted = 1;
1409 else if (pattern_is_quoted == 1)
1410 pattern_is_quoted = 0;
1412 break;
1414 case '"':
1415 if (pattern_is_quoted == 0)
1416 pattern_is_quoted = 2;
1417 else if (pattern_is_quoted == 2)
1418 pattern_is_quoted = 0;
1420 break;
1423 pattern = w_addchar (pattern, &pat_length, &pat_maxlen,
1424 words[*offset]);
1425 if (pattern == NULL)
1426 goto no_space;
1430 /* End of input string -- remember to reparse the character that we
1431 * stopped at. */
1432 --(*offset);
1434 envsubst:
1435 if (words[start] == '{' && words[*offset] != '}')
1436 goto syntax;
1438 if (env == NULL)
1440 if (seen_hash)
1442 /* $# expands to the number of positional parameters */
1443 buffer[20] = '\0';
1444 value = _itoa_word (__libc_argc - 1, &buffer[20], 10, 0);
1445 seen_hash = 0;
1447 else
1449 /* Just $ on its own */
1450 *offset = start - 1;
1451 *word = w_addchar (*word, word_length, max_length, '$');
1452 return *word ? 0 : WRDE_NOSPACE;
1455 /* Is it a numeric parameter? */
1456 else if (isdigit (env[0]))
1458 int n = atoi (env);
1460 if (n >= __libc_argc)
1461 /* Substitute NULL. */
1462 value = NULL;
1463 else
1464 /* Replace with appropriate positional parameter. */
1465 value = __libc_argv[n];
1467 /* Is it a special parameter? */
1468 else if (special)
1470 /* Is it `$$'? */
1471 if (*env == '$')
1473 buffer[20] = '\0';
1474 value = _itoa_word (__getpid (), &buffer[20], 10, 0);
1476 /* Is it `${#*}' or `${#@}'? */
1477 else if ((*env == '*' || *env == '@') && seen_hash)
1479 buffer[20] = '\0';
1480 value = _itoa_word (__libc_argc > 0 ? __libc_argc - 1 : 0,
1481 &buffer[20], 10, 0);
1482 *word = w_addstr (*word, word_length, max_length, value);
1483 free (env);
1484 free (pattern);
1485 return *word ? 0 : WRDE_NOSPACE;
1487 /* Is it `$*' or `$@' (unquoted) ? */
1488 else if (*env == '*' || (*env == '@' && !quoted))
1490 size_t plist_len = 0;
1491 int p;
1492 char *end;
1494 /* Build up value parameter by parameter (copy them) */
1495 for (p = 1; __libc_argv[p]; ++p)
1496 plist_len += strlen (__libc_argv[p]) + 1; /* for space */
1497 value = malloc (plist_len);
1498 if (value == NULL)
1499 goto no_space;
1500 end = value;
1501 *end = 0;
1502 for (p = 1; __libc_argv[p]; ++p)
1504 if (p > 1)
1505 *end++ = ' ';
1506 end = __stpcpy (end, __libc_argv[p]);
1509 free_value = 1;
1511 else
1513 /* Must be a quoted `$@' */
1514 assert (*env == '@' && quoted);
1516 /* Each parameter is a separate word ("$@") */
1517 if (__libc_argc == 2)
1518 value = __libc_argv[1];
1519 else if (__libc_argc > 2)
1521 int p;
1523 /* Append first parameter to current word. */
1524 value = w_addstr (*word, word_length, max_length,
1525 __libc_argv[1]);
1526 if (value == NULL || w_addword (pwordexp, value))
1527 goto no_space;
1529 for (p = 2; __libc_argv[p + 1]; p++)
1531 char *newword = __strdup (__libc_argv[p]);
1532 if (newword == NULL || w_addword (pwordexp, newword))
1533 goto no_space;
1536 /* Start a new word with the last parameter. */
1537 *word = w_newword (word_length, max_length);
1538 value = __libc_argv[p];
1540 else
1542 free (env);
1543 free (pattern);
1544 return 0;
1548 else
1549 value = getenv (env);
1551 if (value == NULL && (flags & WRDE_UNDEF))
1553 /* Variable not defined. */
1554 error = WRDE_BADVAL;
1555 goto do_error;
1558 if (action != ACT_NONE)
1560 int expand_pattern = 0;
1562 /* First, find out if we need to expand pattern (i.e. if we will
1563 * use it). */
1564 switch (action)
1566 case ACT_RP_SHORT_LEFT:
1567 case ACT_RP_LONG_LEFT:
1568 case ACT_RP_SHORT_RIGHT:
1569 case ACT_RP_LONG_RIGHT:
1570 /* Always expand for these. */
1571 expand_pattern = 1;
1572 break;
1574 case ACT_NULL_ERROR:
1575 case ACT_NULL_SUBST:
1576 case ACT_NULL_ASSIGN:
1577 if (!value || (!*value && colon_seen))
1578 /* If param is unset, or set but null and a colon has been seen,
1579 the expansion of the pattern will be needed. */
1580 expand_pattern = 1;
1582 break;
1584 case ACT_NONNULL_SUBST:
1585 /* Expansion of word will be needed if parameter is set and not null,
1586 or set null but no colon has been seen. */
1587 if (value && (*value || !colon_seen))
1588 expand_pattern = 1;
1590 break;
1592 default:
1593 assert (! "Unrecognised action!");
1596 if (expand_pattern)
1598 /* We need to perform tilde expansion, parameter expansion,
1599 command substitution, and arithmetic expansion. We also
1600 have to be a bit careful with wildcard characters, as
1601 pattern might be given to fnmatch soon. To do this, we
1602 convert quotes to escapes. */
1604 char *expanded;
1605 size_t exp_len;
1606 size_t exp_maxl;
1607 char *p;
1608 int quoted = 0; /* 1: single quotes; 2: double */
1610 expanded = w_newword (&exp_len, &exp_maxl);
1611 for (p = pattern; p && *p; p++)
1613 size_t offset;
1615 switch (*p)
1617 case '"':
1618 if (quoted == 2)
1619 quoted = 0;
1620 else if (quoted == 0)
1621 quoted = 2;
1622 else break;
1624 continue;
1626 case '\'':
1627 if (quoted == 1)
1628 quoted = 0;
1629 else if (quoted == 0)
1630 quoted = 1;
1631 else break;
1633 continue;
1635 case '*':
1636 case '?':
1637 if (quoted)
1639 /* Convert quoted wildchar to escaped wildchar. */
1640 expanded = w_addchar (expanded, &exp_len,
1641 &exp_maxl, '\\');
1643 if (expanded == NULL)
1644 goto no_space;
1646 break;
1648 case '$':
1649 offset = 0;
1650 error = parse_dollars (&expanded, &exp_len, &exp_maxl, p,
1651 &offset, flags, NULL, NULL, NULL, 1);
1652 if (error)
1654 if (free_value)
1655 free (value);
1657 free (expanded);
1659 goto do_error;
1662 p += offset;
1663 continue;
1665 case '~':
1666 if (quoted || exp_len)
1667 break;
1669 offset = 0;
1670 error = parse_tilde (&expanded, &exp_len, &exp_maxl, p,
1671 &offset, 0);
1672 if (error)
1674 if (free_value)
1675 free (value);
1677 free (expanded);
1679 goto do_error;
1682 p += offset;
1683 continue;
1685 case '\\':
1686 expanded = w_addchar (expanded, &exp_len, &exp_maxl, '\\');
1687 ++p;
1688 assert (*p); /* checked when extracted initially */
1689 if (expanded == NULL)
1690 goto no_space;
1693 expanded = w_addchar (expanded, &exp_len, &exp_maxl, *p);
1695 if (expanded == NULL)
1696 goto no_space;
1699 free (pattern);
1701 pattern = expanded;
1704 switch (action)
1706 case ACT_RP_SHORT_LEFT:
1707 case ACT_RP_LONG_LEFT:
1708 case ACT_RP_SHORT_RIGHT:
1709 case ACT_RP_LONG_RIGHT:
1711 char *p;
1712 char c;
1713 char *end;
1715 if (value == NULL || pattern == NULL || *pattern == '\0')
1716 break;
1718 end = value + strlen (value);
1720 switch (action)
1722 case ACT_RP_SHORT_LEFT:
1723 for (p = value; p <= end; ++p)
1725 c = *p;
1726 *p = '\0';
1727 if (fnmatch (pattern, value, 0) != FNM_NOMATCH)
1729 *p = c;
1730 if (free_value)
1732 char *newval = __strdup (p);
1733 if (newval == NULL)
1735 free (value);
1736 goto no_space;
1738 free (value);
1739 value = newval;
1741 else
1742 value = p;
1743 break;
1745 *p = c;
1748 break;
1750 case ACT_RP_LONG_LEFT:
1751 for (p = end; p >= value; --p)
1753 c = *p;
1754 *p = '\0';
1755 if (fnmatch (pattern, value, 0) != FNM_NOMATCH)
1757 *p = c;
1758 if (free_value)
1760 char *newval = __strdup (p);
1761 if (newval == NULL)
1763 free (value);
1764 goto no_space;
1766 free (value);
1767 value = newval;
1769 else
1770 value = p;
1771 break;
1773 *p = c;
1776 break;
1778 case ACT_RP_SHORT_RIGHT:
1779 for (p = end; p >= value; --p)
1781 if (fnmatch (pattern, p, 0) != FNM_NOMATCH)
1783 char *newval;
1784 newval = malloc (p - value + 1);
1786 if (newval == NULL)
1788 if (free_value)
1789 free (value);
1790 goto no_space;
1793 *(char *) __mempcpy (newval, value, p - value) = '\0';
1794 if (free_value)
1795 free (value);
1796 value = newval;
1797 free_value = 1;
1798 break;
1802 break;
1804 case ACT_RP_LONG_RIGHT:
1805 for (p = value; p <= end; ++p)
1807 if (fnmatch (pattern, p, 0) != FNM_NOMATCH)
1809 char *newval;
1810 newval = malloc (p - value + 1);
1812 if (newval == NULL)
1814 if (free_value)
1815 free (value);
1816 goto no_space;
1819 *(char *) __mempcpy (newval, value, p - value) = '\0';
1820 if (free_value)
1821 free (value);
1822 value = newval;
1823 free_value = 1;
1824 break;
1828 break;
1830 default:
1831 break;
1834 break;
1837 case ACT_NULL_ERROR:
1838 if (value && *value)
1839 /* Substitute parameter */
1840 break;
1842 error = 0;
1843 if (!colon_seen && value)
1844 /* Substitute NULL */
1846 else
1848 const char *str = pattern;
1850 if (str[0] == '\0')
1851 str = _("parameter null or not set");
1853 __fxprintf (NULL, "%s: %s\n", env, str);
1856 if (free_value)
1857 free (value);
1858 goto do_error;
1860 case ACT_NULL_SUBST:
1861 if (value && *value)
1862 /* Substitute parameter */
1863 break;
1865 if (free_value)
1866 free (value);
1868 if (!colon_seen && value)
1869 /* Substitute NULL */
1870 goto success;
1872 value = pattern ? __strdup (pattern) : pattern;
1873 free_value = 1;
1875 if (pattern && !value)
1876 goto no_space;
1878 break;
1880 case ACT_NONNULL_SUBST:
1881 if (value && (*value || !colon_seen))
1883 if (free_value)
1884 free (value);
1886 value = pattern ? __strdup (pattern) : pattern;
1887 free_value = 1;
1889 if (pattern && !value)
1890 goto no_space;
1892 break;
1895 /* Substitute NULL */
1896 if (free_value)
1897 free (value);
1898 goto success;
1900 case ACT_NULL_ASSIGN:
1901 if (value && *value)
1902 /* Substitute parameter */
1903 break;
1905 if (!colon_seen && value)
1907 /* Substitute NULL */
1908 if (free_value)
1909 free (value);
1910 goto success;
1913 if (free_value)
1914 free (value);
1916 value = pattern ? __strdup (pattern) : pattern;
1917 free_value = 1;
1919 if (pattern && !value)
1920 goto no_space;
1922 __setenv (env, value ?: "", 1);
1923 break;
1925 default:
1926 assert (! "Unrecognised action!");
1930 free (env);
1931 env = NULL;
1932 free (pattern);
1933 pattern = NULL;
1935 if (seen_hash)
1937 char param_length[21];
1938 param_length[20] = '\0';
1939 *word = w_addstr (*word, word_length, max_length,
1940 _itoa_word (value ? strlen (value) : 0,
1941 &param_length[20], 10, 0));
1942 if (free_value)
1944 assert (value != NULL);
1945 free (value);
1948 return *word ? 0 : WRDE_NOSPACE;
1951 if (value == NULL)
1952 return 0;
1954 if (quoted || !pwordexp)
1956 /* Quoted - no field split */
1957 *word = w_addstr (*word, word_length, max_length, value);
1958 if (free_value)
1959 free (value);
1961 return *word ? 0 : WRDE_NOSPACE;
1963 else
1965 /* Need to field-split */
1966 char *value_copy = __strdup (value); /* Don't modify value */
1967 char *field_begin = value_copy;
1968 int seen_nonws_ifs = 0;
1970 if (free_value)
1971 free (value);
1973 if (value_copy == NULL)
1974 goto no_space;
1978 char *field_end = field_begin;
1979 char *next_field;
1981 /* If this isn't the first field, start a new word */
1982 if (field_begin != value_copy)
1984 if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
1986 free (value_copy);
1987 goto no_space;
1990 *word = w_newword (word_length, max_length);
1993 /* Skip IFS whitespace before the field */
1994 field_begin += strspn (field_begin, ifs_white);
1996 if (!seen_nonws_ifs && *field_begin == 0)
1997 /* Nothing but whitespace */
1998 break;
2000 /* Search for the end of the field */
2001 field_end = field_begin + strcspn (field_begin, ifs);
2003 /* Set up pointer to the character after end of field and
2004 skip whitespace IFS after it. */
2005 next_field = field_end + strspn (field_end, ifs_white);
2007 /* Skip at most one non-whitespace IFS character after the field */
2008 seen_nonws_ifs = 0;
2009 if (*next_field && strchr (ifs, *next_field))
2011 seen_nonws_ifs = 1;
2012 next_field++;
2015 /* Null-terminate it */
2016 *field_end = 0;
2018 /* Tag a copy onto the current word */
2019 *word = w_addstr (*word, word_length, max_length, field_begin);
2021 if (*word == NULL && *field_begin != '\0')
2023 free (value_copy);
2024 goto no_space;
2027 field_begin = next_field;
2029 while (seen_nonws_ifs || *field_begin);
2031 free (value_copy);
2034 return 0;
2036 success:
2037 error = 0;
2038 goto do_error;
2040 no_space:
2041 error = WRDE_NOSPACE;
2042 goto do_error;
2044 syntax:
2045 error = WRDE_SYNTAX;
2047 do_error:
2048 free (env);
2050 free (pattern);
2052 return error;
2055 #undef CHAR_IN_SET
2057 static int
2058 internal_function
2059 parse_dollars (char **word, size_t *word_length, size_t *max_length,
2060 const char *words, size_t *offset, int flags,
2061 wordexp_t *pwordexp, const char *ifs, const char *ifs_white,
2062 int quoted)
2064 /* We are poised _at_ "$" */
2065 switch (words[1 + *offset])
2067 case '"':
2068 case '\'':
2069 case 0:
2070 *word = w_addchar (*word, word_length, max_length, '$');
2071 return *word ? 0 : WRDE_NOSPACE;
2073 case '(':
2074 if (words[2 + *offset] == '(')
2076 /* Differentiate between $((1+3)) and $((echo);(ls)) */
2077 int i = 3 + *offset;
2078 int depth = 0;
2079 while (words[i] && !(depth == 0 && words[i] == ')'))
2081 if (words[i] == '(')
2082 ++depth;
2083 else if (words[i] == ')')
2084 --depth;
2086 ++i;
2089 if (words[i] == ')' && words[i + 1] == ')')
2091 (*offset) += 3;
2092 /* Call parse_arith -- 0 is for "no brackets" */
2093 return parse_arith (word, word_length, max_length, words, offset,
2094 flags, 0);
2098 (*offset) += 2;
2099 return parse_comm (word, word_length, max_length, words, offset, flags,
2100 quoted? NULL : pwordexp, ifs, ifs_white);
2102 case '[':
2103 (*offset) += 2;
2104 /* Call parse_arith -- 1 is for "brackets" */
2105 return parse_arith (word, word_length, max_length, words, offset, flags,
2108 case '{':
2109 default:
2110 ++(*offset); /* parse_param needs to know if "{" is there */
2111 return parse_param (word, word_length, max_length, words, offset, flags,
2112 pwordexp, ifs, ifs_white, quoted);
2116 static int
2117 internal_function
2118 parse_backtick (char **word, size_t *word_length, size_t *max_length,
2119 const char *words, size_t *offset, int flags,
2120 wordexp_t *pwordexp, const char *ifs, const char *ifs_white)
2122 /* We are poised just after "`" */
2123 int error;
2124 int squoting = 0;
2125 size_t comm_length;
2126 size_t comm_maxlen;
2127 char *comm = w_newword (&comm_length, &comm_maxlen);
2129 for (; words[*offset]; ++(*offset))
2131 switch (words[*offset])
2133 case '`':
2134 /* Go -- give the script to the shell */
2135 error = exec_comm (comm, word, word_length, max_length, flags,
2136 pwordexp, ifs, ifs_white);
2137 free (comm);
2138 return error;
2140 case '\\':
2141 if (squoting)
2143 error = parse_qtd_backslash (&comm, &comm_length, &comm_maxlen,
2144 words, offset);
2146 if (error)
2148 free (comm);
2149 return error;
2152 break;
2155 error = parse_backslash (&comm, &comm_length, &comm_maxlen, words,
2156 offset);
2158 if (error)
2160 free (comm);
2161 return error;
2164 break;
2166 case '\'':
2167 squoting = 1 - squoting;
2168 default:
2169 comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]);
2170 if (comm == NULL)
2171 return WRDE_NOSPACE;
2175 /* Premature end */
2176 free (comm);
2177 return WRDE_SYNTAX;
2180 static int
2181 internal_function
2182 parse_dquote (char **word, size_t *word_length, size_t *max_length,
2183 const char *words, size_t *offset, int flags,
2184 wordexp_t *pwordexp, const char * ifs, const char * ifs_white)
2186 /* We are poised just after a double-quote */
2187 int error;
2189 for (; words[*offset]; ++(*offset))
2191 switch (words[*offset])
2193 case '"':
2194 return 0;
2196 case '$':
2197 error = parse_dollars (word, word_length, max_length, words, offset,
2198 flags, pwordexp, ifs, ifs_white, 1);
2199 /* The ``1'' here is to tell parse_dollars not to
2200 * split the fields. It may need to, however ("$@").
2202 if (error)
2203 return error;
2205 break;
2207 case '`':
2208 ++(*offset);
2209 error = parse_backtick (word, word_length, max_length, words,
2210 offset, flags, NULL, NULL, NULL);
2211 /* The first NULL here is to tell parse_backtick not to
2212 * split the fields.
2214 if (error)
2215 return error;
2217 break;
2219 case '\\':
2220 error = parse_qtd_backslash (word, word_length, max_length, words,
2221 offset);
2223 if (error)
2224 return error;
2226 break;
2228 default:
2229 *word = w_addchar (*word, word_length, max_length, words[*offset]);
2230 if (*word == NULL)
2231 return WRDE_NOSPACE;
2235 /* Unterminated string */
2236 return WRDE_SYNTAX;
2240 * wordfree() is to be called after pwordexp is finished with.
2243 void
2244 wordfree (wordexp_t *pwordexp)
2247 /* wordexp can set pwordexp to NULL */
2248 if (pwordexp && pwordexp->we_wordv)
2250 char **wordv = pwordexp->we_wordv;
2252 for (wordv += pwordexp->we_offs; *wordv; ++wordv)
2253 free (*wordv);
2255 free (pwordexp->we_wordv);
2256 pwordexp->we_wordv = NULL;
2259 libc_hidden_def (wordfree)
2262 * wordexp()
2266 wordexp (const char *words, wordexp_t *pwordexp, int flags)
2268 size_t words_offset;
2269 size_t word_length;
2270 size_t max_length;
2271 char *word = w_newword (&word_length, &max_length);
2272 int error;
2273 char *ifs;
2274 char ifs_white[4];
2275 wordexp_t old_word = *pwordexp;
2277 if (flags & WRDE_REUSE)
2279 /* Minimal implementation of WRDE_REUSE for now */
2280 wordfree (pwordexp);
2281 old_word.we_wordv = NULL;
2284 if ((flags & WRDE_APPEND) == 0)
2286 pwordexp->we_wordc = 0;
2288 if (flags & WRDE_DOOFFS)
2290 pwordexp->we_wordv = calloc (1 + pwordexp->we_offs, sizeof (char *));
2291 if (pwordexp->we_wordv == NULL)
2293 error = WRDE_NOSPACE;
2294 goto do_error;
2297 else
2299 pwordexp->we_wordv = calloc (1, sizeof (char *));
2300 if (pwordexp->we_wordv == NULL)
2302 error = WRDE_NOSPACE;
2303 goto do_error;
2306 pwordexp->we_offs = 0;
2310 /* Find out what the field separators are.
2311 * There are two types: whitespace and non-whitespace.
2313 ifs = getenv ("IFS");
2315 if (ifs == NULL)
2316 /* IFS unset - use <space><tab><newline>. */
2317 ifs = strcpy (ifs_white, " \t\n");
2318 else
2320 char *ifsch = ifs;
2321 char *whch = ifs_white;
2323 while (*ifsch != '\0')
2325 if (*ifsch == ' ' || *ifsch == '\t' || *ifsch == '\n')
2327 /* Whitespace IFS. See first whether it is already in our
2328 collection. */
2329 char *runp = ifs_white;
2331 while (runp < whch && *runp != *ifsch)
2332 ++runp;
2334 if (runp == whch)
2335 *whch++ = *ifsch;
2338 ++ifsch;
2340 *whch = '\0';
2343 for (words_offset = 0 ; words[words_offset] ; ++words_offset)
2344 switch (words[words_offset])
2346 case '\\':
2347 error = parse_backslash (&word, &word_length, &max_length, words,
2348 &words_offset);
2350 if (error)
2351 goto do_error;
2353 break;
2355 case '$':
2356 error = parse_dollars (&word, &word_length, &max_length, words,
2357 &words_offset, flags, pwordexp, ifs, ifs_white,
2360 if (error)
2361 goto do_error;
2363 break;
2365 case '`':
2366 ++words_offset;
2367 error = parse_backtick (&word, &word_length, &max_length, words,
2368 &words_offset, flags, pwordexp, ifs,
2369 ifs_white);
2371 if (error)
2372 goto do_error;
2374 break;
2376 case '"':
2377 ++words_offset;
2378 error = parse_dquote (&word, &word_length, &max_length, words,
2379 &words_offset, flags, pwordexp, ifs, ifs_white);
2381 if (error)
2382 goto do_error;
2384 if (!word_length)
2386 error = w_addword (pwordexp, NULL);
2388 if (error)
2389 return error;
2392 break;
2394 case '\'':
2395 ++words_offset;
2396 error = parse_squote (&word, &word_length, &max_length, words,
2397 &words_offset);
2399 if (error)
2400 goto do_error;
2402 if (!word_length)
2404 error = w_addword (pwordexp, NULL);
2406 if (error)
2407 return error;
2410 break;
2412 case '~':
2413 error = parse_tilde (&word, &word_length, &max_length, words,
2414 &words_offset, pwordexp->we_wordc);
2416 if (error)
2417 goto do_error;
2419 break;
2421 case '*':
2422 case '[':
2423 case '?':
2424 error = parse_glob (&word, &word_length, &max_length, words,
2425 &words_offset, flags, pwordexp, ifs, ifs_white);
2427 if (error)
2428 goto do_error;
2430 break;
2432 default:
2433 /* Is it a word separator? */
2434 if (strchr (" \t", words[words_offset]) == NULL)
2436 char ch = words[words_offset];
2438 /* Not a word separator -- but is it a valid word char? */
2439 if (strchr ("\n|&;<>(){}", ch))
2441 /* Fail */
2442 error = WRDE_BADCHAR;
2443 goto do_error;
2446 /* "Ordinary" character -- add it to word */
2447 word = w_addchar (word, &word_length, &max_length,
2448 ch);
2449 if (word == NULL)
2451 error = WRDE_NOSPACE;
2452 goto do_error;
2455 break;
2458 /* If a word has been delimited, add it to the list. */
2459 if (word != NULL)
2461 error = w_addword (pwordexp, word);
2462 if (error)
2463 goto do_error;
2466 word = w_newword (&word_length, &max_length);
2469 /* End of string */
2471 /* There was a word separator at the end */
2472 if (word == NULL) /* i.e. w_newword */
2473 return 0;
2475 /* There was no field separator at the end */
2476 return w_addword (pwordexp, word);
2478 do_error:
2479 /* Error:
2480 * free memory used (unless error is WRDE_NOSPACE), and
2481 * set pwordexp members back to what they were.
2484 free (word);
2486 if (error == WRDE_NOSPACE)
2487 return WRDE_NOSPACE;
2489 if ((flags & WRDE_APPEND) == 0)
2490 wordfree (pwordexp);
2492 *pwordexp = old_word;
2493 return error;