1 /* vi: set sw=4 ts=4: */
2 /* POSIX.2 wordexp implementation.
3 Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Tim Waugh <tim@cyberelk.demon.co.uk>.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <sys/types.h>
38 /* Experimentally off - libc_hidden_proto(mempcpy) */
39 /* Experimentally off - libc_hidden_proto(stpcpy) */
40 /* Experimentally off - libc_hidden_proto(strchr) */
41 /* Experimentally off - libc_hidden_proto(strcpy) */
42 /* Experimentally off - libc_hidden_proto(strdup) */
43 /* Experimentally off - libc_hidden_proto(strlen) */
44 /* Experimentally off - libc_hidden_proto(strndup) */
45 /* Experimentally off - libc_hidden_proto(strspn) */
46 /* Experimentally off - libc_hidden_proto(strcspn) */
47 /* libc_hidden_proto(setenv) */
48 /* libc_hidden_proto(unsetenv) */
49 /* libc_hidden_proto(waitpid) */
50 /* libc_hidden_proto(kill) */
51 /* libc_hidden_proto(getuid) */
52 /* libc_hidden_proto(getpwnam_r) */
53 /* libc_hidden_proto(getpwuid_r) */
54 /* libc_hidden_proto(execve) */
55 /* libc_hidden_proto(dup2) */
56 /* libc_hidden_proto(atoi) */
57 /* libc_hidden_proto(fnmatch) */
58 /* libc_hidden_proto(pipe) */
59 /* libc_hidden_proto(fork) */
60 /* libc_hidden_proto(open) */
61 /* libc_hidden_proto(close) */
62 /* libc_hidden_proto(read) */
63 /* libc_hidden_proto(getenv) */
64 /* libc_hidden_proto(getpid) */
65 /* libc_hidden_proto(sprintf) */
66 /* libc_hidden_proto(fprintf) */
67 /* libc_hidden_proto(abort) */
68 /* libc_hidden_proto(glob) */
69 /* libc_hidden_proto(globfree) */
70 /* libc_hidden_proto(wordfree) */
71 #ifdef __UCLIBC_HAS_XLOCALE__
72 /* libc_hidden_proto(__ctype_b_loc) */
73 #elif defined __UCLIBC_HAS_CTYPE_TABLES__
74 /* libc_hidden_proto(__ctype_b) */
77 #define __WORDEXP_FULL
78 //#undef __WORDEXP_FULL
81 * This is a recursive-descent-style word expansion routine.
84 /* These variables are defined and initialized in the startup code. */
85 //extern int __libc_argc;
86 //extern char **__libc_argv;
89 int attribute_hidden __libc_argc;
90 char attribute_hidden **__libc_argv;
92 /* Some forward declarations */
93 static int parse_dollars(char **word, size_t * word_length,
94 size_t * max_length, const char *words,
95 size_t * offset, int flags, wordexp_t * pwordexp,
96 const char *ifs, const char *ifs_white,
98 static int parse_backtick(char **word, size_t * word_length,
99 size_t * max_length, const char *words,
100 size_t * offset, int flags, wordexp_t * pwordexp,
101 const char *ifs, const char *ifs_white);
102 static int parse_dquote(char **word, size_t * word_length,
103 size_t * max_length, const char *words,
104 size_t * offset, int flags, wordexp_t * pwordexp,
105 const char *ifs, const char *ifs_white);
109 /* The w_*() functions manipulate word lists. */
110 #define W_CHUNK (100)
112 /* Result of w_newword will be ignored if it's the last word. */
113 static __inline__ char *w_newword(size_t * actlen, size_t * maxlen)
115 *actlen = *maxlen = 0;
119 /* Add a character to the buffer, allocating room for it if needed. */
120 static __inline__ char *w_addchar(char *buffer, size_t * actlen,
121 size_t * maxlen, char ch)
122 /* (lengths exclude trailing zero) */
125 if (*actlen == *maxlen) {
126 char *old_buffer = buffer;
127 assert(buffer == NULL || *maxlen != 0);
129 buffer = realloc(buffer, 1 + *maxlen);
134 if (buffer != NULL) {
135 buffer[*actlen] = ch;
136 buffer[++(*actlen)] = '\0';
142 #define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
143 static char *w_addmem(char *buffer, size_t * actlen, size_t * maxlen,
144 const char *str, size_t len)
146 /* Add a string to the buffer, allocating room for it if needed.
148 if (*actlen + len > *maxlen) {
149 char *old_buffer = buffer;
150 assert(buffer == NULL || *maxlen != 0);
151 *maxlen += MAX(2 * len, W_CHUNK);
152 buffer = realloc(old_buffer, 1 + *maxlen);
157 if (buffer != NULL) {
158 *((char *) mempcpy(&buffer[*actlen], str, len)) = '\0';
164 /* Add a string to the buffer, allocating room for it if needed. */
165 static char *w_addstr(char *buffer, size_t * actlen, size_t * maxlen,
167 /* (lengths exclude trailing zero) */
170 assert(str != NULL); /* w_addstr only called from this file */
173 return w_addmem(buffer, actlen, maxlen, str, len);
176 /* Add a word to the wordlist */
177 static int w_addword(wordexp_t * pwordexp, char *word)
182 /* Internally, NULL acts like "". Convert NULLs to "" before
183 * the caller sees them.
191 num_p = 2 + pwordexp->we_wordc + pwordexp->we_offs;
192 new_wordv = realloc(pwordexp->we_wordv, sizeof(char *) * num_p);
193 if (new_wordv != NULL) {
194 pwordexp->we_wordv = new_wordv;
195 pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc++] = word;
196 pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc] = NULL;
204 /* The parse_*() functions should leave *offset being the offset in 'words'
205 * to the last character processed.
208 parse_backslash(char **word, size_t * word_length, size_t * max_length,
209 const char *words, size_t * offset)
211 /* We are poised _at_ a backslash, not in quotes */
213 switch (words[1 + *offset]) {
215 /* Backslash is last character of input words */
223 *word = w_addchar(*word, word_length, max_length, words[1 + *offset]);
235 parse_qtd_backslash(char **word, size_t * word_length, size_t * max_length,
236 const char *words, size_t * offset)
238 /* We are poised _at_ a backslash, inside quotes */
240 switch (words[1 + *offset]) {
242 /* Backslash is last character of input words */
254 w_addchar(*word, word_length, max_length, words[1 + *offset]);
262 *word = w_addchar(*word, word_length, max_length, words[*offset]);
265 w_addchar(*word, word_length, max_length,
279 parse_tilde(char **word, size_t * word_length, size_t * max_length,
280 const char *words, size_t * offset, size_t wordc)
282 /* We are poised _at_ a tilde */
285 if (*word_length != 0) {
286 if (!((*word)[*word_length - 1] == '=' && wordc == 0)) {
287 if (!((*word)[*word_length - 1] == ':'
288 && 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++) {
296 if (words[i] == ':' || words[i] == '/' || words[i] == ' ' ||
297 words[i] == '\t' || words[i] == 0)
300 if (words[i] == '\\') {
301 *word = w_addchar(*word, word_length, max_length, '~');
302 return *word ? 0 : WRDE_NOSPACE;
306 if (i == 1 + *offset) {
307 /* Tilde appears on its own */
309 struct passwd pwd, *tpwd;
315 /* POSIX.2 says ~ expands to $HOME and if HOME is unset the
316 results are unspecified. We do a lookup on the uid if
319 home = getenv("HOME");
321 *word = w_addstr(*word, word_length, max_length, home);
326 buffer = alloca(buflen);
328 while ((result = getpwuid_r(uid, &pwd, buffer, buflen, &tpwd))
329 != 0 && errno == ERANGE)
332 buffer = alloca(buflen);
335 if (result == 0 && tpwd != NULL && pwd.pw_dir != NULL) {
336 *word = w_addstr(*word, word_length, max_length, pwd.pw_dir);
340 *word = w_addchar(*word, word_length, max_length, '~');
346 /* Look up user name in database to get home directory */
347 char *user = strndup(&words[1 + *offset], i - (1 + *offset));
348 struct passwd pwd, *tpwd;
350 char *buffer = alloca(buflen);
353 while ((result = getpwnam_r(user, &pwd, buffer, buflen, &tpwd)) != 0
354 && errno == ERANGE) {
356 buffer = alloca(buflen);
359 if (result == 0 && tpwd != NULL && pwd.pw_dir)
360 *word = w_addstr(*word, word_length, max_length, pwd.pw_dir);
362 /* (invalid login name) */
363 *word = w_addchar(*word, word_length, max_length, '~');
365 *word = w_addstr(*word, word_length, max_length, user);
370 return *word ? 0 : WRDE_NOSPACE;
375 do_parse_glob(const char *glob_word, char **word, size_t * word_length,
376 size_t * max_length, wordexp_t * pwordexp, const char *ifs,
377 const char *ifs_white)
383 error = glob(glob_word, GLOB_NOCHECK, NULL, &globbuf);
386 /* We can only run into memory problems. */
387 assert(error == GLOB_NOSPACE);
392 /* No field splitting allowed. */
393 assert(globbuf.gl_pathv[0] != NULL);
394 *word = w_addstr(*word, word_length, max_length, globbuf.gl_pathv[0]);
395 for (match = 1; match < globbuf.gl_pathc && *word != NULL; ++match) {
396 *word = w_addchar(*word, word_length, max_length, ' ');
398 *word = w_addstr(*word, word_length, max_length,
399 globbuf.gl_pathv[match]);
403 return *word ? 0 : WRDE_NOSPACE;
406 assert(ifs == NULL || *ifs != '\0');
409 *word = w_newword(word_length, max_length);
412 for (match = 0; match < globbuf.gl_pathc; ++match) {
413 char *matching_word = strdup(globbuf.gl_pathv[match]);
415 if (matching_word == NULL || w_addword(pwordexp, matching_word)) {
426 parse_glob(char **word, size_t * word_length, size_t * max_length,
427 const char *words, size_t * offset, int flags,
428 wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
430 /* We are poised just after a '*', a '[' or a '?'. */
431 int error = WRDE_NOSPACE;
432 int quoted = 0; /* 1 if singly-quoted, 2 if doubly */
434 wordexp_t glob_list; /* List of words to glob */
436 glob_list.we_wordc = 0;
437 glob_list.we_wordv = NULL;
438 glob_list.we_offs = 0;
439 for (; words[*offset] != '\0'; ++*offset) {
440 if ((ifs && strchr(ifs, words[*offset])) ||
441 (!ifs && strchr(" \t\n", words[*offset])))
445 /* Sort out quoting */
446 if (words[*offset] == '\'') {
450 } else if (quoted == 1) {
454 } else if (words[*offset] == '"') {
458 } else if (quoted == 2) {
464 /* Sort out other special characters */
465 if (quoted != 1 && words[*offset] == '$') {
466 error = parse_dollars(word, word_length, max_length, words,
467 offset, flags, &glob_list, ifs,
468 ifs_white, quoted == 2);
473 } else if (words[*offset] == '\\') {
475 error = parse_qtd_backslash(word, word_length, max_length,
478 error = parse_backslash(word, word_length, max_length,
487 *word = w_addchar(*word, word_length, max_length, words[*offset]);
492 /* Don't forget to re-parse the character we stopped at. */
496 error = w_addword(&glob_list, *word);
497 *word = w_newword(word_length, max_length);
498 for (i = 0; error == 0 && i < glob_list.we_wordc; i++)
499 error = do_parse_glob(glob_list.we_wordv[i], word, word_length,
500 max_length, pwordexp, ifs, ifs_white);
504 wordfree(&glob_list);
509 parse_squote(char **word, size_t * word_length, size_t * max_length,
510 const char *words, size_t * offset)
512 /* We are poised just after a single quote */
513 for (; words[*offset]; ++(*offset)) {
514 if (words[*offset] != '\'') {
515 *word = w_addchar(*word, word_length, max_length, words[*offset]);
522 /* Unterminated string */
526 #ifdef __WORDEXP_FULL
527 static int eval_expr(char *expr, long int *result);
529 static char *_itoa(unsigned long long int value, char *buflim)
531 sprintf(buflim, "%llu", value);
535 /* Functions to evaluate an arithmetic expression */
536 static int eval_expr_val(char **expr, long int *result)
541 /* Skip white space */
542 for (digit = *expr; digit && *digit && isspace(*digit); ++digit);
547 /* Scan for closing paren */
548 for (++digit; **expr && **expr != ')'; ++(*expr));
556 if (eval_expr(digit, result))
561 case '+': /* Positive value */
565 case '-': /* Negative value */
571 if (!isdigit(*digit))
576 for (; *digit && isdigit(*digit); ++digit)
577 *result = (*result * 10) + (*digit - '0');
584 static int eval_expr_multdiv(char **expr, long int *result)
589 if (eval_expr_val(expr, result) != 0)
593 /* Skip white space */
594 for (; *expr && **expr && isspace(**expr); ++(*expr));
598 if (eval_expr_val(expr, &arg) != 0)
602 } else if (**expr == '/') {
604 if (eval_expr_val(expr, &arg) != 0)
615 static int eval_expr(char *expr, long int *result)
620 if (eval_expr_multdiv(&expr, result) != 0)
624 /* Skip white space */
625 for (; expr && *expr && isspace(*expr); ++expr);
629 if (eval_expr_multdiv(&expr, &arg) != 0)
633 } else if (*expr == '-') {
635 if (eval_expr_multdiv(&expr, &arg) != 0)
647 parse_arith(char **word, size_t * word_length, size_t * max_length,
648 const char *words, size_t * offset, int flags, int bracket)
650 /* We are poised just after "$((" or "$[" */
657 expr = w_newword(&expr_length, &expr_maxlen);
658 for (; words[*offset]; ++(*offset)) {
659 switch (words[*offset]) {
661 error = parse_dollars(&expr, &expr_length, &expr_maxlen,
662 words, offset, flags, NULL, NULL, NULL,
664 /* The ``1'' here is to tell parse_dollars not to
675 error = parse_backtick(&expr, &expr_length, &expr_maxlen,
676 words, offset, flags, NULL, NULL, NULL);
677 /* The first NULL here is to tell parse_backtick not to
687 error = parse_qtd_backslash(&expr, &expr_length, &expr_maxlen,
693 /* I think that a backslash within an
694 * arithmetic expansion is bound to
695 * cause an error sooner or later anyway though.
700 if (--paren_depth == 0) {
701 char result[21]; /* 21 = ceil(log10(2^64)) + 1 */
702 long int numresult = 0;
703 long long int convertme;
705 if (bracket || words[1 + *offset] != ')') {
713 if (*expr && eval_expr(expr, &numresult) != 0) {
719 convertme = -numresult;
720 *word = w_addchar(*word, word_length, max_length, '-');
726 convertme = numresult;
729 *word = w_addstr(*word, word_length, max_length,
730 _itoa(convertme, &result[20]));
732 return *word ? 0 : WRDE_NOSPACE;
735 w_addchar(expr, &expr_length, &expr_maxlen,
743 if (bracket && paren_depth == 1) {
744 char result[21]; /* 21 = ceil(log10(2^64)) + 1 */
745 long int numresult = 0;
748 if (*expr && eval_expr(expr, &numresult) != 0) {
754 *word = w_addstr(*word, word_length, max_length,
755 _itoa(numresult, &result[20]));
757 return *word ? 0 : WRDE_NOSPACE;
774 w_addchar(expr, &expr_length, &expr_maxlen,
786 /* Function called by child process in exec_comm() */
787 static void attribute_noreturn
788 exec_comm_child(char *comm, int *fildes, int showerr, int noexec)
790 const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
792 /* Execute the command, or just check syntax? */
796 /* Redirect output. */
800 /* Redirect stderr to /dev/null if we have to. */
805 fd = open(_PATH_DEVNULL, O_WRONLY);
806 if (fd >= 0 && fd != 2) {
812 /* Make sure the subshell doesn't field-split on our behalf. */
816 execve(_PATH_BSHELL, (char *const *) args, __environ);
822 /* Function to execute a command and retrieve the results */
823 /* pwordexp contains NULL if field-splitting is forbidden */
825 exec_comm(char *comm, char **word, size_t * word_length,
826 size_t * max_length, int flags, wordexp_t * pwordexp,
827 const char *ifs, const char *ifs_white)
834 size_t maxnewlines = 0;
838 /* Don't fork() unless necessary */
846 if ((pid = fork()) < 0) {
854 exec_comm_child(comm, fildes, (flags & WRDE_SHOWERR), 0);
859 buffer = alloca(bufsize);
862 /* Quoted - no field splitting */
865 if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
866 if (waitpid(pid, &status, WNOHANG) == 0)
868 if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
872 maxnewlines += buflen;
874 *word = w_addmem(*word, word_length, max_length, buffer, buflen);
879 /* Not quoted - split fields */
884 * 0 when searching for first character in a field not IFS white space
885 * 1 when copying the text of a field
886 * 2 when searching for possible non-whitespace IFS
887 * 3 when searching for non-newline after copying field
891 if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
892 if (waitpid(pid, &status, WNOHANG) == 0)
894 if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
898 for (i = 0; i < buflen; ++i) {
899 if (strchr(ifs, buffer[i]) != NULL) {
900 /* Current character is IFS */
901 if (strchr(ifs_white, buffer[i]) == NULL) {
902 /* Current character is IFS but not whitespace */
907 * eg: text<space><comma><space>moretext
909 * So, strip whitespace IFS (like at the start)
916 /* fall through and delimit field.. */
918 if (buffer[i] == '\n') {
919 /* Current character is (IFS) newline */
921 /* If copying a field, this is the end of it,
922 but maybe all that's left is trailing newlines.
923 So start searching for a non-newline. */
929 /* Current character is IFS white space, but
932 /* If not either copying a field or searching
933 for non-newline after a field, ignore it */
934 if (copying != 1 && copying != 3)
937 /* End of field (search for non-ws IFS afterwards) */
942 /* First IFS white space (non-newline), or IFS non-whitespace.
943 * Delimit the field. Nulls are converted by w_addword. */
944 if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
947 *word = w_newword(word_length, max_length);
950 /* fall back round the loop.. */
952 /* Not IFS character */
955 /* Nothing but (IFS) newlines since the last field,
956 so delimit it here before starting new word */
957 if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
960 *word = w_newword(word_length, max_length);
965 if (buffer[i] == '\n') /* happens if newline not in IFS */
970 *word = w_addchar(*word, word_length, max_length,
979 /* Chop off trailing newlines (required by POSIX.2) */
980 /* Ensure we don't go back further than the beginning of the
981 substitution (i.e. remove maxnewlines bytes at most) */
982 while (maxnewlines-- != 0 &&
983 *word_length > 0 && (*word)[*word_length - 1] == '\n') {
984 (*word)[--*word_length] = '\0';
986 /* If the last word was entirely newlines, turn it into a new word
987 * which can be ignored if there's nothing following it. */
988 if (*word_length == 0) {
990 *word = w_newword(word_length, max_length);
997 /* Check for syntax error (re-execute but with "-n" flag) */
998 if (buflen < 1 && status != 0) {
999 if ((pid = fork()) < 0) {
1001 return WRDE_NOSPACE;
1005 fildes[0] = fildes[1] = -1;
1006 exec_comm_child(comm, fildes, 0, 1);
1009 if (waitpid(pid, &status, 0) == pid && status != 0)
1017 waitpid(pid, NULL, 0);
1019 return WRDE_NOSPACE;
1023 parse_comm(char **word, size_t * word_length, size_t * max_length,
1024 const char *words, size_t * offset, int flags,
1025 wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
1027 /* We are poised just after "$(" */
1028 int paren_depth = 1;
1030 int quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1033 char *comm = w_newword(&comm_length, &comm_maxlen);
1035 for (; words[*offset]; ++(*offset)) {
1036 switch (words[*offset]) {
1040 else if (quoted == 1)
1048 else if (quoted == 2)
1054 if (!quoted && --paren_depth == 0) {
1055 /* Go -- give script to the shell */
1057 error = exec_comm(comm, word, word_length, max_length,
1058 flags, pwordexp, ifs, ifs_white);
1065 /* This is just part of the script */
1073 comm = w_addchar(comm, &comm_length, &comm_maxlen, words[*offset]);
1075 return WRDE_NOSPACE;
1085 parse_backtick(char **word, size_t * word_length, size_t * max_length,
1086 const char *words, size_t * offset, int flags,
1087 wordexp_t * pwordexp, const char *ifs,
1088 const char *ifs_white)
1090 /* We are poised just after "`" */
1095 char *comm = w_newword(&comm_length, &comm_maxlen);
1097 for (; words[*offset]; ++(*offset)) {
1098 switch (words[*offset]) {
1100 /* Go -- give the script to the shell */
1101 error = exec_comm(comm, word, word_length, max_length, flags,
1102 pwordexp, ifs, ifs_white);
1108 error = parse_qtd_backslash(&comm, &comm_length, &comm_maxlen,
1120 error = parse_backslash(&comm, &comm_length, &comm_maxlen, words,
1131 squoting = 1 - squoting;
1133 comm = w_addchar(comm, &comm_length, &comm_maxlen,
1136 return WRDE_NOSPACE;
1146 parse_param(char **word, size_t * word_length, size_t * max_length,
1147 const char *words, size_t * offset, int flags,
1148 wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
1151 /* We are poised just after "$" */
1154 ACT_RP_SHORT_LEFT = '#',
1155 ACT_RP_LONG_LEFT = 'L',
1156 ACT_RP_SHORT_RIGHT = '%',
1157 ACT_RP_LONG_RIGHT = 'R',
1158 ACT_NULL_ERROR = '?',
1159 ACT_NULL_SUBST = '-',
1160 ACT_NONNULL_SUBST = '+',
1161 ACT_NULL_ASSIGN = '='
1167 size_t start = *offset;
1171 enum action action = ACT_NONE;
1176 int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1180 int brace = words[*offset] == '{';
1182 env = w_newword(&env_length, &env_maxlen);
1183 pattern = w_newword(&pat_length, &pat_maxlen);
1188 /* First collect the parameter name. */
1190 if (words[*offset] == '#') {
1197 if (isalpha(words[*offset]) || words[*offset] == '_') {
1198 /* Normal parameter name. */
1200 env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
1204 while (isalnum(words[++*offset]) || words[*offset] == '_');
1205 } else if (isdigit(words[*offset])) {
1206 /* Numeric parameter name. */
1209 env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
1215 while (isdigit(words[++*offset]));
1216 } else if (strchr("*@$", words[*offset]) != NULL) {
1217 /* Special parameter. */
1219 env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
1229 /* Check for special action to be applied to the value. */
1230 switch (words[*offset]) {
1236 action = ACT_RP_SHORT_LEFT;
1237 if (words[1 + *offset] == '#') {
1239 action = ACT_RP_LONG_LEFT;
1244 action = ACT_RP_SHORT_RIGHT;
1245 if (words[1 + *offset] == '%') {
1247 action = ACT_RP_LONG_RIGHT;
1252 if (strchr("-=?+", words[1 + *offset]) == NULL)
1256 action = words[++*offset];
1263 action = words[*offset];
1270 /* Now collect the pattern, but don't expand it yet. */
1272 for (; words[*offset]; ++(*offset)) {
1273 switch (words[*offset]) {
1275 if (!pattern_is_quoted)
1280 if (!pattern_is_quoted) {
1288 if (pattern_is_quoted)
1289 /* Quoted; treat as normal character. */
1292 /* Otherwise, it's an escape: next character is literal. */
1293 if (words[++*offset] == '\0')
1296 pattern = w_addchar(pattern, &pat_length, &pat_maxlen, '\\');
1297 if (pattern == NULL)
1303 if (pattern_is_quoted == 0)
1304 pattern_is_quoted = 1;
1305 else if (pattern_is_quoted == 1)
1306 pattern_is_quoted = 0;
1311 if (pattern_is_quoted == 0)
1312 pattern_is_quoted = 2;
1313 else if (pattern_is_quoted == 2)
1314 pattern_is_quoted = 0;
1319 pattern = w_addchar(pattern, &pat_length, &pat_maxlen,
1321 if (pattern == NULL)
1326 /* End of input string -- remember to reparse the character that we
1331 if (words[start] == '{' && words[*offset] != '}')
1336 /* $# expands to the number of positional parameters */
1338 value = _itoa(__libc_argc - 1, &buffer[20]);
1341 /* Just $ on its own */
1342 *offset = start - 1;
1343 *word = w_addchar(*word, word_length, max_length, '$');
1344 return *word ? 0 : WRDE_NOSPACE;
1347 /* Is it a numeric parameter? */
1348 else if (isdigit(env[0])) {
1351 if (n >= __libc_argc)
1352 /* Substitute NULL. */
1355 /* Replace with appropriate positional parameter. */
1356 value = __libc_argv[n];
1358 /* Is it a special parameter? */
1363 value = _itoa(getpid(), &buffer[20]);
1365 /* Is it `${#*}' or `${#@}'? */
1366 else if ((*env == '*' || *env == '@') && seen_hash) {
1368 value = _itoa(__libc_argc > 0 ? __libc_argc - 1 : 0,
1370 *word = w_addstr(*word, word_length, max_length, value);
1373 return *word ? 0 : WRDE_NOSPACE;
1375 /* Is it `$*' or `$@' (unquoted) ? */
1376 else if (*env == '*' || (*env == '@' && !quoted)) {
1377 size_t plist_len = 0;
1381 /* Build up value parameter by parameter (copy them) */
1382 for (p = 1; __libc_argv[p]; ++p)
1383 plist_len += strlen(__libc_argv[p]) + 1; /* for space */
1384 value = malloc(plist_len);
1389 for (p = 1; __libc_argv[p]; ++p) {
1392 end = stpcpy(end, __libc_argv[p]);
1397 /* Must be a quoted `$@' */
1398 assert(*env == '@' && quoted);
1400 /* Each parameter is a separate word ("$@") */
1401 if (__libc_argc == 2)
1402 value = __libc_argv[1];
1403 else if (__libc_argc > 2) {
1406 /* Append first parameter to current word. */
1407 value = w_addstr(*word, word_length, max_length,
1409 if (value == NULL || w_addword(pwordexp, value))
1412 for (p = 2; __libc_argv[p + 1]; p++) {
1413 char *newword = strdup(__libc_argv[p]);
1415 if (newword == NULL || w_addword(pwordexp, newword))
1419 /* Start a new word with the last parameter. */
1420 *word = w_newword(word_length, max_length);
1421 value = __libc_argv[p];
1429 value = getenv(env);
1431 if (value == NULL && (flags & WRDE_UNDEF)) {
1432 /* Variable not defined. */
1433 error = WRDE_BADVAL;
1437 if (action != ACT_NONE) {
1438 int expand_pattern = 0;
1440 /* First, find out if we need to expand pattern (i.e. if we will
1443 case ACT_RP_SHORT_LEFT:
1444 case ACT_RP_LONG_LEFT:
1445 case ACT_RP_SHORT_RIGHT:
1446 case ACT_RP_LONG_RIGHT:
1447 /* Always expand for these. */
1451 case ACT_NULL_ERROR:
1452 case ACT_NULL_SUBST:
1453 case ACT_NULL_ASSIGN:
1454 if (!value || (!*value && colon_seen))
1455 /* If param is unset, or set but null and a colon has been seen,
1456 the expansion of the pattern will be needed. */
1461 case ACT_NONNULL_SUBST:
1462 /* Expansion of word will be needed if parameter is set and not null,
1463 or set null but no colon has been seen. */
1464 if (value && (*value || !colon_seen))
1470 assert(!"Unrecognised action!");
1473 if (expand_pattern) {
1474 /* We need to perform tilde expansion, parameter expansion,
1475 command substitution, and arithmetic expansion. We also
1476 have to be a bit careful with wildcard characters, as
1477 pattern might be given to fnmatch soon. To do this, we
1478 convert quotes to escapes. */
1484 int quoted = 0; /* 1: single quotes; 2: double */
1486 expanded = w_newword(&exp_len, &exp_maxl);
1487 for (p = pattern; p && *p; p++) {
1494 else if (quoted == 0)
1504 else if (quoted == 0)
1514 /* Convert quoted wildchar to escaped wildchar. */
1515 expanded = w_addchar(expanded, &exp_len,
1518 if (expanded == NULL)
1525 error = parse_dollars(&expanded, &exp_len, &exp_maxl, p,
1526 &offset, flags, NULL, NULL, NULL, 1);
1540 if (quoted || exp_len)
1544 error = parse_tilde(&expanded, &exp_len, &exp_maxl, p,
1559 expanded = w_addchar(expanded, &exp_len, &exp_maxl, '\\');
1561 assert(*p); /* checked when extracted initially */
1562 if (expanded == NULL)
1566 expanded = w_addchar(expanded, &exp_len, &exp_maxl, *p);
1568 if (expanded == NULL)
1578 case ACT_RP_SHORT_LEFT:
1579 case ACT_RP_LONG_LEFT:
1580 case ACT_RP_SHORT_RIGHT:
1581 case ACT_RP_LONG_RIGHT:
1587 if (value == NULL || pattern == NULL || *pattern == '\0')
1590 end = value + strlen(value);
1593 case ACT_RP_SHORT_LEFT:
1594 for (p = value; p <= end; ++p) {
1597 if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
1600 char *newval = strdup(p);
1602 if (newval == NULL) {
1617 case ACT_RP_LONG_LEFT:
1618 for (p = end; p >= value; --p) {
1621 if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
1624 char *newval = strdup(p);
1626 if (newval == NULL) {
1641 case ACT_RP_SHORT_RIGHT:
1642 for (p = end; p >= value; --p) {
1643 if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
1646 newval = malloc(p - value + 1);
1648 if (newval == NULL) {
1654 *(char *) mempcpy(newval, value, p - value) = '\0';
1665 case ACT_RP_LONG_RIGHT:
1666 for (p = value; p <= end; ++p) {
1667 if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
1670 newval = malloc(p - value + 1);
1672 if (newval == NULL) {
1678 *(char *) mempcpy(newval, value, p - value) = '\0';
1696 case ACT_NULL_ERROR:
1697 if (value && *value)
1698 /* Substitute parameter */
1702 if (!colon_seen && value)
1703 /* Substitute NULL */
1706 fprintf(stderr, "%s: %s\n", env, pattern);
1708 fprintf(stderr, "%s: parameter null or not set\n", env);
1709 error = WRDE_BADVAL;
1716 case ACT_NULL_SUBST:
1717 if (value && *value)
1718 /* Substitute parameter */
1724 if (!colon_seen && value)
1725 /* Substitute NULL */
1728 value = pattern ? strdup(pattern) : pattern;
1731 if (pattern && !value)
1736 case ACT_NONNULL_SUBST:
1737 if (value && (*value || !colon_seen)) {
1741 value = pattern ? strdup(pattern) : pattern;
1744 if (pattern && !value)
1750 /* Substitute NULL */
1755 case ACT_NULL_ASSIGN:
1756 if (value && *value)
1757 /* Substitute parameter */
1760 if (!colon_seen && value) {
1761 /* Substitute NULL */
1770 value = pattern ? strdup(pattern) : pattern;
1773 if (pattern && !value)
1776 setenv(env, value, 1);
1780 assert(!"Unrecognised action!");
1790 char param_length[21];
1792 param_length[20] = '\0';
1793 *word = w_addstr(*word, word_length, max_length,
1794 _itoa(value ? strlen(value) : 0,
1795 ¶m_length[20]));
1797 assert(value != NULL);
1801 return *word ? 0 : WRDE_NOSPACE;
1807 if (quoted || !pwordexp) {
1808 /* Quoted - no field split */
1809 *word = w_addstr(*word, word_length, max_length, value);
1813 return *word ? 0 : WRDE_NOSPACE;
1815 /* Need to field-split */
1816 char *value_copy = strdup(value); /* Don't modify value */
1817 char *field_begin = value_copy;
1818 int seen_nonws_ifs = 0;
1823 if (value_copy == NULL)
1827 char *field_end = field_begin;
1830 /* If this isn't the first field, start a new word */
1831 if (field_begin != value_copy) {
1832 if (w_addword(pwordexp, *word) == WRDE_NOSPACE) {
1837 *word = w_newword(word_length, max_length);
1840 /* Skip IFS whitespace before the field */
1841 field_begin += strspn(field_begin, ifs_white);
1843 if (!seen_nonws_ifs && *field_begin == 0)
1844 /* Nothing but whitespace */
1847 /* Search for the end of the field */
1848 field_end = field_begin + strcspn(field_begin, ifs);
1850 /* Set up pointer to the character after end of field and
1851 skip whitespace IFS after it. */
1852 next_field = field_end + strspn(field_end, ifs_white);
1854 /* Skip at most one non-whitespace IFS character after the field */
1856 if (*next_field && strchr(ifs, *next_field)) {
1861 /* Null-terminate it */
1864 /* Tag a copy onto the current word */
1865 *word = w_addstr(*word, word_length, max_length, field_begin);
1867 if (*word == NULL && *field_begin != '\0') {
1872 field_begin = next_field;
1874 while (seen_nonws_ifs || *field_begin);
1886 error = WRDE_NOSPACE;
1890 error = WRDE_SYNTAX;
1900 static __inline__ int
1901 parse_backtick(char **word, size_t * word_length, size_t * max_length,
1902 const char *words, size_t * offset, int flags,
1903 wordexp_t * pwordexp, const char *ifs,
1904 const char *ifs_white)
1911 parse_dollars(char **word, size_t * word_length, size_t * max_length,
1912 const char *words, size_t * offset, int flags,
1913 wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
1916 /* We are poised _at_ "$" */
1917 switch (words[1 + *offset]) {
1921 *word = w_addchar(*word, word_length, max_length, '$');
1922 return *word ? 0 : WRDE_NOSPACE;
1924 #ifdef __WORDEXP_FULL
1926 if (words[2 + *offset] == '(') {
1927 /* Differentiate between $((1+3)) and $((echo);(ls)) */
1928 int i = 3 + *offset;
1931 while (words[i] && !(depth == 0 && words[i] == ')')) {
1932 if (words[i] == '(')
1934 else if (words[i] == ')')
1940 if (words[i] == ')' && words[i + 1] == ')') {
1942 /* Call parse_arith -- 0 is for "no brackets" */
1943 return parse_arith(word, word_length, max_length, words,
1948 if (flags & WRDE_NOCMD)
1952 return parse_comm(word, word_length, max_length, words, offset,
1953 flags, quoted ? NULL : pwordexp, ifs, ifs_white);
1957 /* Call parse_arith -- 1 is for "brackets" */
1958 return parse_arith(word, word_length, max_length, words, offset,
1963 ++(*offset); /* parse_param needs to know if "{" is there */
1964 return parse_param(word, word_length, max_length, words, offset,
1965 flags, pwordexp, ifs, ifs_white, quoted);
1968 ++(*offset); /* parse_param needs to know if "{" is there */
1975 parse_dquote(char **word, size_t * word_length, size_t * max_length,
1976 const char *words, size_t * offset, int flags,
1977 wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
1979 /* We are poised just after a double-quote */
1982 for (; words[*offset]; ++(*offset)) {
1983 switch (words[*offset]) {
1988 error = parse_dollars(word, word_length, max_length, words, offset,
1989 flags, pwordexp, ifs, ifs_white, 1);
1990 /* The ``1'' here is to tell parse_dollars not to
1991 * split the fields. It may need to, however ("$@").
1999 if (flags & WRDE_NOCMD)
2003 error = parse_backtick(word, word_length, max_length, words,
2004 offset, flags, NULL, NULL, NULL);
2005 /* The first NULL here is to tell parse_backtick not to
2014 error = parse_qtd_backslash(word, word_length, max_length, words,
2023 *word = w_addchar(*word, word_length, max_length, words[*offset]);
2025 return WRDE_NOSPACE;
2029 /* Unterminated string */
2034 * wordfree() is to be called after pwordexp is finished with.
2037 void wordfree(wordexp_t * pwordexp)
2040 /* wordexp can set pwordexp to NULL */
2041 if (pwordexp && pwordexp->we_wordv) {
2042 char **wordv = pwordexp->we_wordv;
2044 for (wordv += pwordexp->we_offs; *wordv; ++wordv)
2047 free(pwordexp->we_wordv);
2048 pwordexp->we_wordv = NULL;
2051 libc_hidden_def(wordfree)
2057 int wordexp(const char *words, wordexp_t * we, int flags)
2059 size_t words_offset;
2062 char *word = w_newword(&word_length, &max_length);
2066 wordexp_t old_word = *we;
2068 if (flags & WRDE_REUSE) {
2069 /* Minimal implementation of WRDE_REUSE for now */
2071 old_word.we_wordv = NULL;
2074 if ((flags & WRDE_APPEND) == 0) {
2077 if (flags & WRDE_DOOFFS) {
2078 we->we_wordv = calloc(1 + we->we_offs, sizeof(char *));
2079 if (we->we_wordv == NULL) {
2080 error = WRDE_NOSPACE;
2084 we->we_wordv = calloc(1, sizeof(char *));
2085 if (we->we_wordv == NULL) {
2086 error = WRDE_NOSPACE;
2094 /* Find out what the field separators are.
2095 * There are two types: whitespace and non-whitespace.
2097 ifs = getenv("IFS");
2100 /* IFS unset - use <space><tab><newline>. */
2101 ifs = strcpy(ifs_white, " \t\n");
2104 char *whch = ifs_white;
2106 /* Start off with no whitespace IFS characters */
2107 ifs_white[0] = '\0';
2109 while (*ifsch != '\0') {
2110 if ((*ifsch == ' ') || (*ifsch == '\t') || (*ifsch == '\n')) {
2111 /* Whitespace IFS. See first whether it is already in our
2113 char *runp = ifs_white;
2115 while (runp < whch && *runp != '\0' && *runp != *ifsch)
2127 for (words_offset = 0; words[words_offset]; ++words_offset)
2128 switch (words[words_offset]) {
2130 error = parse_backslash(&word, &word_length, &max_length, words,
2139 error = parse_dollars(&word, &word_length, &max_length, words,
2140 &words_offset, flags, we, ifs, ifs_white,
2149 if (flags & WRDE_NOCMD) {
2150 error = WRDE_CMDSUB;
2155 error = parse_backtick(&word, &word_length, &max_length, words,
2156 &words_offset, flags, we, ifs,
2166 error = parse_dquote(&word, &word_length, &max_length, words,
2167 &words_offset, flags, we, ifs, ifs_white);
2173 error = w_addword(we, NULL);
2183 error = parse_squote(&word, &word_length, &max_length, words,
2190 error = w_addword(we, NULL);
2199 error = parse_tilde(&word, &word_length, &max_length, words,
2200 &words_offset, we->we_wordc);
2210 error = parse_glob(&word, &word_length, &max_length, words,
2211 &words_offset, flags, we, ifs, ifs_white);
2219 /* Is it a word separator? */
2220 if (strchr(" \t", words[words_offset]) == NULL) {
2221 char ch = words[words_offset];
2223 /* Not a word separator -- but is it a valid word char? */
2224 if (strchr("\n|&;<>(){}", ch)) {
2226 error = WRDE_BADCHAR;
2230 /* "Ordinary" character -- add it to word */
2231 word = w_addchar(word, &word_length, &max_length, ch);
2233 error = WRDE_NOSPACE;
2240 /* If a word has been delimited, add it to the list. */
2242 error = w_addword(we, word);
2247 word = w_newword(&word_length, &max_length);
2252 /* There was a word separator at the end */
2253 if (word == NULL) /* i.e. w_newword */
2256 /* There was no field separator at the end */
2257 return w_addword(we, word);
2261 * free memory used (unless error is WRDE_NOSPACE), and
2262 * set we members back to what they were.
2267 if (error == WRDE_NOSPACE)
2268 return WRDE_NOSPACE;
2270 if ((flags & WRDE_APPEND) == 0)