1 /* POSIX.2 wordexp implementation.
2 Copyright (C) 1997-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
30 #include <sys/param.h>
35 #include <scratch_buffer.h>
40 * This is a recursive-descent-style word expansion routine.
43 /* These variables are defined and initialized in the startup code. */
44 extern int __libc_argc attribute_hidden
;
45 extern char **__libc_argv attribute_hidden
;
47 /* Some forward declarations */
48 static int parse_dollars (char **word
, size_t *word_length
, size_t *max_length
,
49 const char *words
, size_t *offset
, int flags
,
50 wordexp_t
*pwordexp
, const char *ifs
,
51 const char *ifs_white
, int quoted
);
52 static int parse_backtick (char **word
, size_t *word_length
,
53 size_t *max_length
, const char *words
,
54 size_t *offset
, int flags
, wordexp_t
*pwordexp
,
55 const char *ifs
, const char *ifs_white
);
56 static int parse_dquote (char **word
, size_t *word_length
, size_t *max_length
,
57 const char *words
, size_t *offset
, int flags
,
58 wordexp_t
*pwordexp
, const char *ifs
,
59 const char *ifs_white
);
60 static int eval_expr (char *expr
, long int *result
);
62 /* The w_*() functions manipulate word lists. */
66 /* Result of w_newword will be ignored if it's the last word. */
68 w_newword (size_t *actlen
, size_t *maxlen
)
70 *actlen
= *maxlen
= 0;
75 w_addchar (char *buffer
, size_t *actlen
, size_t *maxlen
, char ch
)
76 /* (lengths exclude trailing zero) */
78 /* Add a character to the buffer, allocating room for it if needed. */
80 if (*actlen
== *maxlen
)
82 char *old_buffer
= buffer
;
83 assert (buffer
== NULL
|| *maxlen
!= 0);
85 buffer
= (char *) realloc (buffer
, 1 + *maxlen
);
94 buffer
[++(*actlen
)] = '\0';
101 w_addmem (char *buffer
, size_t *actlen
, size_t *maxlen
, const char *str
,
104 /* Add a string to the buffer, allocating room for it if needed.
106 if (*actlen
+ len
> *maxlen
)
108 char *old_buffer
= buffer
;
109 assert (buffer
== NULL
|| *maxlen
!= 0);
110 *maxlen
+= MAX (2 * len
, W_CHUNK
);
111 buffer
= realloc (old_buffer
, 1 + *maxlen
);
119 *((char *) __mempcpy (&buffer
[*actlen
], str
, len
)) = '\0';
127 w_addstr (char *buffer
, size_t *actlen
, size_t *maxlen
, const char *str
)
128 /* (lengths exclude trailing zero) */
130 /* Add a string to the buffer, allocating room for it if needed.
134 assert (str
!= NULL
); /* w_addstr only called from this file */
137 return w_addmem (buffer
, actlen
, maxlen
, str
, len
);
141 w_addword (wordexp_t
*pwordexp
, char *word
)
143 /* Add a word to the wordlist */
146 bool allocated
= false;
148 /* Internally, NULL acts like "". Convert NULLs to "" before
149 * the caller sees them.
153 word
= __strdup ("");
159 num_p
= 2 + pwordexp
->we_wordc
+ pwordexp
->we_offs
;
160 new_wordv
= realloc (pwordexp
->we_wordv
, sizeof (char *) * num_p
);
161 if (new_wordv
!= NULL
)
163 pwordexp
->we_wordv
= new_wordv
;
164 pwordexp
->we_wordv
[pwordexp
->we_offs
+ pwordexp
->we_wordc
++] = word
;
165 pwordexp
->we_wordv
[pwordexp
->we_offs
+ pwordexp
->we_wordc
] = NULL
;
176 /* The parse_*() functions should leave *offset being the offset in 'words'
177 * to the last character processed.
181 parse_backslash (char **word
, size_t *word_length
, size_t *max_length
,
182 const char *words
, size_t *offset
)
184 /* We are poised _at_ a backslash, not in quotes */
186 switch (words
[1 + *offset
])
189 /* Backslash is last character of input words */
197 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
209 parse_qtd_backslash (char **word
, size_t *word_length
, size_t *max_length
,
210 const char *words
, size_t *offset
)
212 /* We are poised _at_ a backslash, inside quotes */
214 switch (words
[1 + *offset
])
217 /* Backslash is last character of input words */
228 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
236 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
238 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
251 parse_tilde (char **word
, size_t *word_length
, size_t *max_length
,
252 const char *words
, size_t *offset
, size_t wordc
)
254 /* We are poised _at_ a tilde */
257 if (*word_length
!= 0)
259 if (!((*word
)[*word_length
- 1] == '=' && wordc
== 0))
261 if (!((*word
)[*word_length
- 1] == ':'
262 && strchr (*word
, '=') && wordc
== 0))
264 *word
= w_addchar (*word
, word_length
, max_length
, '~');
265 return *word
? 0 : WRDE_NOSPACE
;
270 for (i
= 1 + *offset
; words
[i
]; i
++)
272 if (words
[i
] == ':' || words
[i
] == '/' || words
[i
] == ' '
273 || words
[i
] == '\t' || words
[i
] == 0 )
276 if (words
[i
] == '\\')
278 *word
= w_addchar (*word
, word_length
, max_length
, '~');
279 return *word
? 0 : WRDE_NOSPACE
;
283 if (i
== 1 + *offset
)
285 /* Tilde appears on its own */
288 /* POSIX.2 says ~ expands to $HOME and if HOME is unset the
289 results are unspecified. We do a lookup on the uid if
292 home
= getenv ("HOME");
295 *word
= w_addstr (*word
, word_length
, max_length
, home
);
301 struct passwd pwd
, *tpwd
;
302 uid_t uid
= __getuid ();
304 struct scratch_buffer tmpbuf
;
305 scratch_buffer_init (&tmpbuf
);
307 while ((result
= __getpwuid_r (uid
, &pwd
,
308 tmpbuf
.data
, tmpbuf
.length
,
311 if (!scratch_buffer_grow (&tmpbuf
))
314 if (result
== 0 && tpwd
!= NULL
&& pwd
.pw_dir
!= NULL
)
316 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
319 scratch_buffer_free (&tmpbuf
);
325 *word
= w_addchar (*word
, word_length
, max_length
, '~');
328 scratch_buffer_free (&tmpbuf
);
332 scratch_buffer_free (&tmpbuf
);
337 /* Look up user name in database to get home directory */
338 char *user
= strndupa (&words
[1 + *offset
], i
- (1 + *offset
));
339 struct passwd pwd
, *tpwd
;
341 struct scratch_buffer tmpbuf
;
342 scratch_buffer_init (&tmpbuf
);
344 while ((result
= __getpwnam_r (user
, &pwd
, tmpbuf
.data
, tmpbuf
.length
,
347 if (!scratch_buffer_grow (&tmpbuf
))
350 if (result
== 0 && tpwd
!= NULL
&& pwd
.pw_dir
)
351 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
354 /* (invalid login name) */
355 *word
= w_addchar (*word
, word_length
, max_length
, '~');
357 *word
= w_addstr (*word
, word_length
, max_length
, user
);
360 scratch_buffer_free (&tmpbuf
);
364 return *word
? 0 : WRDE_NOSPACE
;
369 do_parse_glob (const char *glob_word
, char **word
, size_t *word_length
,
370 size_t *max_length
, wordexp_t
*pwordexp
, const char *ifs
,
371 const char *ifs_white
)
377 error
= glob (glob_word
, GLOB_NOCHECK
, NULL
, &globbuf
);
381 /* We can only run into memory problems. */
382 assert (error
== GLOB_NOSPACE
);
388 /* No field splitting allowed. */
389 assert (globbuf
.gl_pathv
[0] != NULL
);
390 *word
= w_addstr (*word
, word_length
, max_length
, globbuf
.gl_pathv
[0]);
391 for (match
= 1; match
< globbuf
.gl_pathc
&& *word
!= NULL
; ++match
)
393 *word
= w_addchar (*word
, word_length
, max_length
, ' ');
395 *word
= w_addstr (*word
, word_length
, max_length
,
396 globbuf
.gl_pathv
[match
]);
400 return *word
? 0 : WRDE_NOSPACE
;
403 assert (ifs
== NULL
|| *ifs
!= '\0');
407 *word
= w_newword (word_length
, max_length
);
410 for (match
= 0; match
< globbuf
.gl_pathc
; ++match
)
412 char *matching_word
= __strdup (globbuf
.gl_pathv
[match
]);
413 if (matching_word
== NULL
|| w_addword (pwordexp
, matching_word
))
425 parse_glob (char **word
, size_t *word_length
, size_t *max_length
,
426 const char *words
, size_t *offset
, int flags
,
427 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
429 /* We are poised just after a '*', a '[' or a '?'. */
430 int error
= WRDE_NOSPACE
;
431 int quoted
= 0; /* 1 if singly-quoted, 2 if doubly */
433 wordexp_t glob_list
; /* List of words to glob */
435 glob_list
.we_wordc
= 0;
436 glob_list
.we_wordv
= NULL
;
437 glob_list
.we_offs
= 0;
438 for (; words
[*offset
] != '\0'; ++*offset
)
440 if (strchr (ifs
, words
[*offset
]) != NULL
)
444 /* Sort out quoting */
445 if (words
[*offset
] == '\'')
452 else if (quoted
== 1)
458 else if (words
[*offset
] == '"')
465 else if (quoted
== 2)
472 /* Sort out other special characters */
473 if (quoted
!= 1 && words
[*offset
] == '$')
475 error
= parse_dollars (word
, word_length
, max_length
, words
,
476 offset
, flags
, &glob_list
, ifs
, ifs_white
,
483 else if (words
[*offset
] == '\\')
486 error
= parse_qtd_backslash (word
, word_length
, max_length
,
489 error
= parse_backslash (word
, word_length
, max_length
,
498 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
503 /* Don't forget to re-parse the character we stopped at. */
507 error
= w_addword (&glob_list
, *word
);
508 *word
= w_newword (word_length
, max_length
);
509 for (i
= 0; error
== 0 && i
< glob_list
.we_wordc
; i
++)
510 error
= do_parse_glob (glob_list
.we_wordv
[i
], word
, word_length
,
511 max_length
, pwordexp
, ifs
, ifs_white
);
515 wordfree (&glob_list
);
520 parse_squote (char **word
, size_t *word_length
, size_t *max_length
,
521 const char *words
, size_t *offset
)
523 /* We are poised just after a single quote */
524 for (; words
[*offset
]; ++(*offset
))
526 if (words
[*offset
] != '\'')
528 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
535 /* Unterminated string */
539 /* Functions to evaluate an arithmetic expression */
541 eval_expr_val (char **expr
, long int *result
)
545 /* Skip white space */
546 for (digit
= *expr
; digit
&& *digit
&& isspace (*digit
); ++digit
);
550 /* Scan for closing paren */
551 for (++digit
; **expr
&& **expr
!= ')'; ++(*expr
));
559 if (eval_expr (digit
, result
))
565 /* POSIX requires that decimal, octal, and hexadecimal constants are
566 recognized. Therefore we pass 0 as the third parameter to strtol. */
567 *result
= strtol (digit
, expr
, 0);
575 eval_expr_multdiv (char **expr
, long int *result
)
580 if (eval_expr_val (expr
, result
) != 0)
585 /* Skip white space */
586 for (; *expr
&& **expr
&& isspace (**expr
); ++(*expr
));
591 if (eval_expr_val (expr
, &arg
) != 0)
596 else if (**expr
== '/')
599 if (eval_expr_val (expr
, &arg
) != 0)
602 /* Division by zero or integer overflow. */
603 if (arg
== 0 || (arg
== -1 && *result
== LONG_MIN
))
615 eval_expr (char *expr
, long int *result
)
620 if (eval_expr_multdiv (&expr
, result
) != 0)
625 /* Skip white space */
626 for (; expr
&& *expr
&& isspace (*expr
); ++expr
);
631 if (eval_expr_multdiv (&expr
, &arg
) != 0)
636 else if (*expr
== '-')
639 if (eval_expr_multdiv (&expr
, &arg
) != 0)
651 parse_arith (char **word
, size_t *word_length
, size_t *max_length
,
652 const char *words
, size_t *offset
, int flags
, int bracket
)
654 /* We are poised just after "$((" or "$[" */
661 expr
= w_newword (&expr_length
, &expr_maxlen
);
662 for (; words
[*offset
]; ++(*offset
))
664 switch (words
[*offset
])
667 error
= parse_dollars (&expr
, &expr_length
, &expr_maxlen
,
668 words
, offset
, flags
, NULL
, NULL
, NULL
, 1);
669 /* The ``1'' here is to tell parse_dollars not to
681 error
= parse_backtick (&expr
, &expr_length
, &expr_maxlen
,
682 words
, offset
, flags
, NULL
, NULL
, NULL
);
683 /* The first NULL here is to tell parse_backtick not to
694 error
= parse_qtd_backslash (&expr
, &expr_length
, &expr_maxlen
,
701 /* I think that a backslash within an
702 * arithmetic expansion is bound to
703 * cause an error sooner or later anyway though.
708 if (--paren_depth
== 0)
710 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
711 long int numresult
= 0;
712 long long int convertme
;
714 if (bracket
|| words
[1 + *offset
] != ')')
723 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
731 convertme
= -numresult
;
732 *word
= w_addchar (*word
, word_length
, max_length
, '-');
740 convertme
= numresult
;
743 *word
= w_addstr (*word
, word_length
, max_length
,
744 _itoa (convertme
, &result
[20], 10, 0));
746 return *word
? 0 : WRDE_NOSPACE
;
748 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
755 if (bracket
&& paren_depth
== 1)
757 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
758 long int numresult
= 0;
761 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
768 *word
= w_addstr (*word
, word_length
, max_length
,
769 _itoa_word (numresult
, &result
[20], 10, 0));
771 return *word
? 0 : WRDE_NOSPACE
;
788 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
799 #define DYNARRAY_STRUCT strlist
800 #define DYNARRAY_ELEMENT char *
801 #define DYNARRAY_PREFIX strlist_
802 /* Allocates about 512/1024 (32/64 bit) on stack. */
803 #define DYNARRAY_INITIAL_SIZE 128
804 #include <malloc/dynarray-skeleton.c>
806 /* Function called by child process in exec_comm() */
808 exec_comm_child (char *comm
, int *fildes
, bool showerr
, bool noexec
)
812 /* Execute the command, or just check syntax? */
813 const char *args
[] = { _PATH_BSHELL
, noexec
? "-nc" : "-c", comm
, NULL
};
815 posix_spawn_file_actions_t fa
;
816 /* posix_spawn_file_actions_init does not fail. */
817 __posix_spawn_file_actions_init (&fa
);
819 /* Redirect output. For check syntax only (noexec being true), exec_comm
820 explicits sets fildes[1] to -1, so check its value to avoid a failure in
821 __posix_spawn_file_actions_adddup2. */
824 if (__glibc_likely (fildes
[1] != STDOUT_FILENO
))
826 if (__posix_spawn_file_actions_adddup2 (&fa
, fildes
[1],
828 || __posix_spawn_file_actions_addclose (&fa
, fildes
[1]) != 0)
832 /* Reset the close-on-exec flag (if necessary). */
833 if (__posix_spawn_file_actions_adddup2 (&fa
, fildes
[1], fildes
[1])
838 /* Redirect stderr to /dev/null if we have to. */
840 if (__posix_spawn_file_actions_addopen (&fa
, STDERR_FILENO
, _PATH_DEVNULL
,
844 struct strlist newenv
;
845 strlist_init (&newenv
);
847 bool recreate_env
= getenv ("IFS") != NULL
;
850 for (char **ep
= __environ
; *ep
!= NULL
; ep
++)
851 if (strncmp (*ep
, "IFS=", strlen ("IFS=")) != 0)
852 strlist_add (&newenv
, *ep
);
853 strlist_add (&newenv
, NULL
);
854 if (strlist_has_failed (&newenv
))
858 /* pid is not set if posix_spawn fails, so it keep the original value
860 __posix_spawn (&pid
, _PATH_BSHELL
, &fa
, NULL
, (char *const *) args
,
861 recreate_env
? strlist_begin (&newenv
) : __environ
);
863 strlist_free (&newenv
);
866 __posix_spawn_file_actions_destroy (&fa
);
871 /* Function to execute a command and retrieve the results */
872 /* pwordexp contains NULL if field-splitting is forbidden */
874 exec_comm (char *comm
, char **word
, size_t *word_length
, size_t *max_length
,
875 int flags
, wordexp_t
*pwordexp
, const char *ifs
,
876 const char *ifs_white
)
883 size_t maxnewlines
= 0;
884 char buffer
[bufsize
];
888 /* Do nothing if command substitution should not succeed. */
889 if (flags
& WRDE_NOCMD
)
892 /* Don't posix_spawn unless necessary */
896 if (__pipe2 (fildes
, O_CLOEXEC
) < 0)
900 pid
= exec_comm_child (comm
, fildes
, noexec
? false : flags
& WRDE_SHOWERR
,
909 /* If we are just testing the syntax, only wait. */
911 return (TEMP_FAILURE_RETRY (__waitpid (pid
, &status
, 0)) == pid
912 && status
!= 0) ? WRDE_SYNTAX
: 0;
918 /* Quoted - no field splitting */
922 if ((buflen
= TEMP_FAILURE_RETRY (__read (fildes
[0], buffer
,
925 /* If read returned 0 then the process has closed its
926 stdout. Don't use WNOHANG in that case to avoid busy
927 looping until the process eventually exits. */
928 if (TEMP_FAILURE_RETRY (__waitpid (pid
, &status
,
929 buflen
== 0 ? 0 : WNOHANG
))
932 if ((buflen
= TEMP_FAILURE_RETRY (__read (fildes
[0], buffer
,
937 maxnewlines
+= buflen
;
939 *word
= w_addmem (*word
, word_length
, max_length
, buffer
, buflen
);
945 /* Not quoted - split fields */
949 * 0 when searching for first character in a field not IFS white space
950 * 1 when copying the text of a field
951 * 2 when searching for possible non-whitespace IFS
952 * 3 when searching for non-newline after copying field
957 if ((buflen
= TEMP_FAILURE_RETRY (__read (fildes
[0], buffer
,
960 /* If read returned 0 then the process has closed its
961 stdout. Don't use WNOHANG in that case to avoid busy
962 looping until the process eventually exits. */
963 if (TEMP_FAILURE_RETRY (__waitpid (pid
, &status
,
964 buflen
== 0 ? 0 : WNOHANG
))
967 if ((buflen
= TEMP_FAILURE_RETRY (__read (fildes
[0], buffer
,
972 for (i
= 0; i
< buflen
; ++i
)
974 if (strchr (ifs
, buffer
[i
]) != NULL
)
976 /* Current character is IFS */
977 if (strchr (ifs_white
, buffer
[i
]) == NULL
)
979 /* Current character is IFS but not whitespace */
985 * eg: text<space><comma><space>moretext
987 * So, strip whitespace IFS (like at the start)
994 /* fall through and delimit field.. */
998 if (buffer
[i
] == '\n')
1000 /* Current character is (IFS) newline */
1002 /* If copying a field, this is the end of it,
1003 but maybe all that's left is trailing newlines.
1004 So start searching for a non-newline. */
1012 /* Current character is IFS white space, but
1015 /* If not either copying a field or searching
1016 for non-newline after a field, ignore it */
1017 if (copying
!= 1 && copying
!= 3)
1020 /* End of field (search for non-ws IFS afterwards) */
1025 /* First IFS white space (non-newline), or IFS non-whitespace.
1026 * Delimit the field. Nulls are converted by w_addword. */
1027 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1030 *word
= w_newword (word_length
, max_length
);
1033 /* fall back round the loop.. */
1037 /* Not IFS character */
1041 /* Nothing but (IFS) newlines since the last field,
1042 so delimit it here before starting new word */
1043 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1046 *word
= w_newword (word_length
, max_length
);
1051 if (buffer
[i
] == '\n') /* happens if newline not in IFS */
1056 *word
= w_addchar (*word
, word_length
, max_length
,
1065 /* Chop off trailing newlines (required by POSIX.2) */
1066 /* Ensure we don't go back further than the beginning of the
1067 substitution (i.e. remove maxnewlines bytes at most) */
1068 while (maxnewlines
-- != 0
1069 && *word_length
> 0 && (*word
)[*word_length
- 1] == '\n')
1071 (*word
)[--*word_length
] = '\0';
1073 /* If the last word was entirely newlines, turn it into a new word
1074 * which can be ignored if there's nothing following it. */
1075 if (*word_length
== 0)
1078 *word
= w_newword (word_length
, max_length
);
1083 __close (fildes
[0]);
1086 /* Check for syntax error (re-execute but with "-n" flag) */
1087 if (buflen
< 1 && status
!= 0)
1096 __kill (pid
, SIGKILL
);
1097 TEMP_FAILURE_RETRY (__waitpid (pid
, NULL
, 0));
1098 __close (fildes
[0]);
1099 return WRDE_NOSPACE
;
1103 parse_comm (char **word
, size_t *word_length
, size_t *max_length
,
1104 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1105 const char *ifs
, const char *ifs_white
)
1107 /* We are poised just after "$(" */
1108 int paren_depth
= 1;
1110 int quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1113 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
1115 for (; words
[*offset
]; ++(*offset
))
1117 switch (words
[*offset
])
1122 else if (quoted
== 1)
1130 else if (quoted
== 2)
1136 if (!quoted
&& --paren_depth
== 0)
1138 /* Go -- give script to the shell */
1141 /* posix_spawn already handles thread cancellation. */
1142 error
= exec_comm (comm
, word
, word_length
, max_length
,
1143 flags
, pwordexp
, ifs
, ifs_white
);
1150 /* This is just part of the script */
1158 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
1160 return WRDE_NOSPACE
;
1163 /* Premature end. */
1169 #define CHAR_IN_SET(ch, char_set) \
1170 (memchr (char_set "", ch, sizeof (char_set) - 1) != NULL)
1173 parse_param (char **word
, size_t *word_length
, size_t *max_length
,
1174 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1175 const char *ifs
, const char *ifs_white
, int quoted
)
1177 /* We are poised just after "$" */
1181 ACT_RP_SHORT_LEFT
= '#',
1182 ACT_RP_LONG_LEFT
= 'L',
1183 ACT_RP_SHORT_RIGHT
= '%',
1184 ACT_RP_LONG_RIGHT
= 'R',
1185 ACT_NULL_ERROR
= '?',
1186 ACT_NULL_SUBST
= '-',
1187 ACT_NONNULL_SUBST
= '+',
1188 ACT_NULL_ASSIGN
= '='
1194 size_t start
= *offset
;
1198 enum action action
= ACT_NONE
;
1203 int pattern_is_quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1207 int brace
= words
[*offset
] == '{';
1209 env
= w_newword (&env_length
, &env_maxlen
);
1210 pattern
= w_newword (&pat_length
, &pat_maxlen
);
1215 /* First collect the parameter name. */
1217 if (words
[*offset
] == '#')
1225 if (isalpha (words
[*offset
]) || words
[*offset
] == '_')
1227 /* Normal parameter name. */
1230 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1235 while (isalnum (words
[++*offset
]) || words
[*offset
] == '_');
1237 else if (isdigit (words
[*offset
]))
1239 /* Numeric parameter name. */
1243 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1250 while (isdigit(words
[++*offset
]));
1252 else if (CHAR_IN_SET (words
[*offset
], "*@$"))
1254 /* Special parameter. */
1256 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1270 /* Check for special action to be applied to the value. */
1271 switch (words
[*offset
])
1278 action
= ACT_RP_SHORT_LEFT
;
1279 if (words
[1 + *offset
] == '#')
1282 action
= ACT_RP_LONG_LEFT
;
1287 action
= ACT_RP_SHORT_RIGHT
;
1288 if (words
[1 + *offset
] == '%')
1291 action
= ACT_RP_LONG_RIGHT
;
1296 if (!CHAR_IN_SET (words
[1 + *offset
], "-=?+"))
1300 action
= words
[++*offset
];
1307 action
= words
[*offset
];
1314 /* Now collect the pattern, but don't expand it yet. */
1316 for (; words
[*offset
]; ++(*offset
))
1318 switch (words
[*offset
])
1321 if (!pattern_is_quoted
)
1326 if (!pattern_is_quoted
)
1335 if (pattern_is_quoted
)
1336 /* Quoted; treat as normal character. */
1339 /* Otherwise, it's an escape: next character is literal. */
1340 if (words
[++*offset
] == '\0')
1343 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
, '\\');
1344 if (pattern
== NULL
)
1350 if (pattern_is_quoted
== 0)
1351 pattern_is_quoted
= 1;
1352 else if (pattern_is_quoted
== 1)
1353 pattern_is_quoted
= 0;
1358 if (pattern_is_quoted
== 0)
1359 pattern_is_quoted
= 2;
1360 else if (pattern_is_quoted
== 2)
1361 pattern_is_quoted
= 0;
1366 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
,
1368 if (pattern
== NULL
)
1373 /* End of input string -- remember to reparse the character that we
1378 if (words
[start
] == '{' && words
[*offset
] != '}')
1385 /* $# expands to the number of positional parameters */
1387 value
= _itoa_word (__libc_argc
- 1, &buffer
[20], 10, 0);
1392 /* Just $ on its own */
1393 *offset
= start
- 1;
1394 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1395 return *word
? 0 : WRDE_NOSPACE
;
1398 /* Is it a numeric parameter? */
1399 else if (isdigit (env
[0]))
1401 unsigned long n
= strtoul (env
, NULL
, 10);
1403 if (n
>= __libc_argc
)
1404 /* Substitute NULL. */
1407 /* Replace with appropriate positional parameter. */
1408 value
= __libc_argv
[n
];
1410 /* Is it a special parameter? */
1417 value
= _itoa_word (__getpid (), &buffer
[20], 10, 0);
1419 /* Is it `${#*}' or `${#@}'? */
1420 else if ((*env
== '*' || *env
== '@') && seen_hash
)
1423 value
= _itoa_word (__libc_argc
> 0 ? __libc_argc
- 1 : 0,
1424 &buffer
[20], 10, 0);
1425 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1428 return *word
? 0 : WRDE_NOSPACE
;
1430 /* Is it `$*' or `$@' (unquoted) ? */
1431 else if (*env
== '*' || (*env
== '@' && !quoted
))
1433 size_t plist_len
= 0;
1437 /* Build up value parameter by parameter (copy them) */
1438 for (p
= 1; __libc_argv
[p
]; ++p
)
1439 plist_len
+= strlen (__libc_argv
[p
]) + 1; /* for space */
1440 value
= malloc (plist_len
);
1445 for (p
= 1; __libc_argv
[p
]; ++p
)
1449 end
= __stpcpy (end
, __libc_argv
[p
]);
1456 /* Must be a quoted `$@' */
1457 assert (*env
== '@' && quoted
);
1459 /* Each parameter is a separate word ("$@") */
1460 if (__libc_argc
== 2)
1461 value
= __libc_argv
[1];
1462 else if (__libc_argc
> 2)
1466 /* Append first parameter to current word. */
1467 value
= w_addstr (*word
, word_length
, max_length
,
1469 if (value
== NULL
|| w_addword (pwordexp
, value
))
1472 for (p
= 2; __libc_argv
[p
+ 1]; p
++)
1474 char *newword
= __strdup (__libc_argv
[p
]);
1475 if (newword
== NULL
|| w_addword (pwordexp
, newword
))
1479 /* Start a new word with the last parameter. */
1480 *word
= w_newword (word_length
, max_length
);
1481 value
= __libc_argv
[p
];
1492 value
= getenv (env
);
1494 if (value
== NULL
&& (flags
& WRDE_UNDEF
))
1496 /* Variable not defined. */
1497 error
= WRDE_BADVAL
;
1501 if (action
!= ACT_NONE
)
1503 int expand_pattern
= 0;
1505 /* First, find out if we need to expand pattern (i.e. if we will
1509 case ACT_RP_SHORT_LEFT
:
1510 case ACT_RP_LONG_LEFT
:
1511 case ACT_RP_SHORT_RIGHT
:
1512 case ACT_RP_LONG_RIGHT
:
1513 /* Always expand for these. */
1517 case ACT_NULL_ERROR
:
1518 case ACT_NULL_SUBST
:
1519 case ACT_NULL_ASSIGN
:
1520 if (!value
|| (!*value
&& colon_seen
))
1521 /* If param is unset, or set but null and a colon has been seen,
1522 the expansion of the pattern will be needed. */
1527 case ACT_NONNULL_SUBST
:
1528 /* Expansion of word will be needed if parameter is set and not null,
1529 or set null but no colon has been seen. */
1530 if (value
&& (*value
|| !colon_seen
))
1536 assert (! "Unrecognised action!");
1541 /* We need to perform tilde expansion, parameter expansion,
1542 command substitution, and arithmetic expansion. We also
1543 have to be a bit careful with wildcard characters, as
1544 pattern might be given to fnmatch soon. To do this, we
1545 convert quotes to escapes. */
1551 int quoted
= 0; /* 1: single quotes; 2: double */
1553 expanded
= w_newword (&exp_len
, &exp_maxl
);
1554 for (p
= pattern
; p
&& *p
; p
++)
1563 else if (quoted
== 0)
1572 else if (quoted
== 0)
1582 /* Convert quoted wildchar to escaped wildchar. */
1583 expanded
= w_addchar (expanded
, &exp_len
,
1586 if (expanded
== NULL
)
1593 error
= parse_dollars (&expanded
, &exp_len
, &exp_maxl
, p
,
1594 &offset
, flags
, NULL
, NULL
, NULL
, 1);
1609 if (quoted
|| exp_len
)
1613 error
= parse_tilde (&expanded
, &exp_len
, &exp_maxl
, p
,
1629 expanded
= w_addchar (expanded
, &exp_len
, &exp_maxl
, '\\');
1631 assert (*p
); /* checked when extracted initially */
1632 if (expanded
== NULL
)
1636 expanded
= w_addchar (expanded
, &exp_len
, &exp_maxl
, *p
);
1638 if (expanded
== NULL
)
1649 case ACT_RP_SHORT_LEFT
:
1650 case ACT_RP_LONG_LEFT
:
1651 case ACT_RP_SHORT_RIGHT
:
1652 case ACT_RP_LONG_RIGHT
:
1658 if (value
== NULL
|| pattern
== NULL
|| *pattern
== '\0')
1661 end
= value
+ strlen (value
);
1665 case ACT_RP_SHORT_LEFT
:
1666 for (p
= value
; p
<= end
; ++p
)
1670 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1675 char *newval
= __strdup (p
);
1693 case ACT_RP_LONG_LEFT
:
1694 for (p
= end
; p
>= value
; --p
)
1698 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1703 char *newval
= __strdup (p
);
1721 case ACT_RP_SHORT_RIGHT
:
1722 for (p
= end
; p
>= value
; --p
)
1724 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1727 newval
= malloc (p
- value
+ 1);
1736 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1747 case ACT_RP_LONG_RIGHT
:
1748 for (p
= value
; p
<= end
; ++p
)
1750 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1753 newval
= malloc (p
- value
+ 1);
1762 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1780 case ACT_NULL_ERROR
:
1781 if (value
&& *value
)
1782 /* Substitute parameter */
1786 if (!colon_seen
&& value
)
1787 /* Substitute NULL */
1791 const char *str
= pattern
;
1794 str
= _("parameter null or not set");
1796 __fxprintf (NULL
, "%s: %s\n", env
, str
);
1803 case ACT_NULL_SUBST
:
1804 if (value
&& *value
)
1805 /* Substitute parameter */
1811 if (!colon_seen
&& value
)
1812 /* Substitute NULL */
1815 value
= pattern
? __strdup (pattern
) : pattern
;
1818 if (pattern
&& !value
)
1823 case ACT_NONNULL_SUBST
:
1824 if (value
&& (*value
|| !colon_seen
))
1829 value
= pattern
? __strdup (pattern
) : pattern
;
1832 if (pattern
&& !value
)
1838 /* Substitute NULL */
1843 case ACT_NULL_ASSIGN
:
1844 if (value
&& *value
)
1845 /* Substitute parameter */
1848 if (!colon_seen
&& value
)
1850 /* Substitute NULL */
1859 value
= pattern
? __strdup (pattern
) : pattern
;
1862 if (pattern
&& !value
)
1865 __setenv (env
, value
?: "", 1);
1869 assert (! "Unrecognised action!");
1880 char param_length
[21];
1881 param_length
[20] = '\0';
1882 *word
= w_addstr (*word
, word_length
, max_length
,
1883 _itoa_word (value
? strlen (value
) : 0,
1884 ¶m_length
[20], 10, 0));
1887 assert (value
!= NULL
);
1891 return *word
? 0 : WRDE_NOSPACE
;
1897 if (quoted
|| !pwordexp
)
1899 /* Quoted - no field split */
1900 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1904 return *word
? 0 : WRDE_NOSPACE
;
1908 /* Need to field-split */
1909 char *value_copy
= __strdup (value
); /* Don't modify value */
1910 char *field_begin
= value_copy
;
1911 int seen_nonws_ifs
= 0;
1916 if (value_copy
== NULL
)
1921 char *field_end
= field_begin
;
1924 /* If this isn't the first field, start a new word */
1925 if (field_begin
!= value_copy
)
1927 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1933 *word
= w_newword (word_length
, max_length
);
1936 /* Skip IFS whitespace before the field */
1937 field_begin
+= strspn (field_begin
, ifs_white
);
1939 if (!seen_nonws_ifs
&& *field_begin
== 0)
1940 /* Nothing but whitespace */
1943 /* Search for the end of the field */
1944 field_end
= field_begin
+ strcspn (field_begin
, ifs
);
1946 /* Set up pointer to the character after end of field and
1947 skip whitespace IFS after it. */
1948 next_field
= field_end
+ strspn (field_end
, ifs_white
);
1950 /* Skip at most one non-whitespace IFS character after the field */
1952 if (*next_field
&& strchr (ifs
, *next_field
))
1958 /* Null-terminate it */
1961 /* Tag a copy onto the current word */
1962 *word
= w_addstr (*word
, word_length
, max_length
, field_begin
);
1964 if (*word
== NULL
&& *field_begin
!= '\0')
1970 field_begin
= next_field
;
1972 while (seen_nonws_ifs
|| *field_begin
);
1984 error
= WRDE_NOSPACE
;
1988 error
= WRDE_SYNTAX
;
2001 parse_dollars (char **word
, size_t *word_length
, size_t *max_length
,
2002 const char *words
, size_t *offset
, int flags
,
2003 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
,
2006 /* We are poised _at_ "$" */
2007 switch (words
[1 + *offset
])
2012 *word
= w_addchar (*word
, word_length
, max_length
, '$');
2013 return *word
? 0 : WRDE_NOSPACE
;
2016 if (words
[2 + *offset
] == '(')
2018 /* Differentiate between $((1+3)) and $((echo);(ls)) */
2019 int i
= 3 + *offset
;
2021 while (words
[i
] && !(depth
== 0 && words
[i
] == ')'))
2023 if (words
[i
] == '(')
2025 else if (words
[i
] == ')')
2031 if (words
[i
] == ')' && words
[i
+ 1] == ')')
2034 /* Call parse_arith -- 0 is for "no brackets" */
2035 return parse_arith (word
, word_length
, max_length
, words
, offset
,
2041 return parse_comm (word
, word_length
, max_length
, words
, offset
, flags
,
2042 quoted
? NULL
: pwordexp
, ifs
, ifs_white
);
2046 /* Call parse_arith -- 1 is for "brackets" */
2047 return parse_arith (word
, word_length
, max_length
, words
, offset
, flags
,
2052 ++(*offset
); /* parse_param needs to know if "{" is there */
2053 return parse_param (word
, word_length
, max_length
, words
, offset
, flags
,
2054 pwordexp
, ifs
, ifs_white
, quoted
);
2059 parse_backtick (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
)
2063 /* We are poised just after "`" */
2068 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
2070 for (; words
[*offset
]; ++(*offset
))
2072 switch (words
[*offset
])
2075 /* Go -- give the script to the shell */
2076 error
= exec_comm (comm
, word
, word_length
, max_length
, flags
,
2077 pwordexp
, ifs
, ifs_white
);
2084 error
= parse_qtd_backslash (&comm
, &comm_length
, &comm_maxlen
,
2096 error
= parse_backslash (&comm
, &comm_length
, &comm_maxlen
, words
,
2108 squoting
= 1 - squoting
;
2111 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
2113 return WRDE_NOSPACE
;
2123 parse_dquote (char **word
, size_t *word_length
, size_t *max_length
,
2124 const char *words
, size_t *offset
, int flags
,
2125 wordexp_t
*pwordexp
, const char * ifs
, const char * ifs_white
)
2127 /* We are poised just after a double-quote */
2130 for (; words
[*offset
]; ++(*offset
))
2132 switch (words
[*offset
])
2138 error
= parse_dollars (word
, word_length
, max_length
, words
, offset
,
2139 flags
, pwordexp
, ifs
, ifs_white
, 1);
2140 /* The ``1'' here is to tell parse_dollars not to
2141 * split the fields. It may need to, however ("$@").
2150 error
= parse_backtick (word
, word_length
, max_length
, words
,
2151 offset
, flags
, NULL
, NULL
, NULL
);
2152 /* The first NULL here is to tell parse_backtick not to
2161 error
= parse_qtd_backslash (word
, word_length
, max_length
, words
,
2170 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
2172 return WRDE_NOSPACE
;
2176 /* Unterminated string */
2181 * wordfree() is to be called after pwordexp is finished with.
2185 wordfree (wordexp_t
*pwordexp
)
2188 /* wordexp can set pwordexp to NULL */
2189 if (pwordexp
&& pwordexp
->we_wordv
)
2191 char **wordv
= pwordexp
->we_wordv
;
2193 for (wordv
+= pwordexp
->we_offs
; *wordv
; ++wordv
)
2196 free (pwordexp
->we_wordv
);
2197 pwordexp
->we_wordv
= NULL
;
2200 libc_hidden_def (wordfree
)
2207 wordexp (const char *words
, wordexp_t
*pwordexp
, int flags
)
2209 size_t words_offset
;
2212 char *word
= w_newword (&word_length
, &max_length
);
2216 wordexp_t old_word
= *pwordexp
;
2218 if (flags
& WRDE_REUSE
)
2220 /* Minimal implementation of WRDE_REUSE for now */
2221 wordfree (pwordexp
);
2222 old_word
.we_wordv
= NULL
;
2225 if ((flags
& WRDE_APPEND
) == 0)
2227 pwordexp
->we_wordc
= 0;
2229 if (flags
& WRDE_DOOFFS
)
2231 pwordexp
->we_wordv
= calloc (1 + pwordexp
->we_offs
, sizeof (char *));
2232 if (pwordexp
->we_wordv
== NULL
)
2234 error
= WRDE_NOSPACE
;
2240 pwordexp
->we_wordv
= calloc (1, sizeof (char *));
2241 if (pwordexp
->we_wordv
== NULL
)
2243 error
= WRDE_NOSPACE
;
2247 pwordexp
->we_offs
= 0;
2251 /* Find out what the field separators are.
2252 * There are two types: whitespace and non-whitespace.
2254 ifs
= getenv ("IFS");
2257 /* IFS unset - use <space><tab><newline>. */
2258 ifs
= strcpy (ifs_white
, " \t\n");
2262 char *whch
= ifs_white
;
2264 while (*ifsch
!= '\0')
2266 if (*ifsch
== ' ' || *ifsch
== '\t' || *ifsch
== '\n')
2268 /* Whitespace IFS. See first whether it is already in our
2270 char *runp
= ifs_white
;
2272 while (runp
< whch
&& *runp
!= *ifsch
)
2284 for (words_offset
= 0 ; words
[words_offset
] ; ++words_offset
)
2285 switch (words
[words_offset
])
2288 error
= parse_backslash (&word
, &word_length
, &max_length
, words
,
2297 error
= parse_dollars (&word
, &word_length
, &max_length
, words
,
2298 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
,
2308 error
= parse_backtick (&word
, &word_length
, &max_length
, words
,
2309 &words_offset
, flags
, pwordexp
, ifs
,
2319 error
= parse_dquote (&word
, &word_length
, &max_length
, words
,
2320 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2327 error
= w_addword (pwordexp
, NULL
);
2337 error
= parse_squote (&word
, &word_length
, &max_length
, words
,
2345 error
= w_addword (pwordexp
, NULL
);
2354 error
= parse_tilde (&word
, &word_length
, &max_length
, words
,
2355 &words_offset
, pwordexp
->we_wordc
);
2365 error
= parse_glob (&word
, &word_length
, &max_length
, words
,
2366 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2374 /* Is it a word separator? */
2375 if (strchr (" \t", words
[words_offset
]) == NULL
)
2377 char ch
= words
[words_offset
];
2379 /* Not a word separator -- but is it a valid word char? */
2380 if (strchr ("\n|&;<>(){}", ch
))
2383 error
= WRDE_BADCHAR
;
2387 /* "Ordinary" character -- add it to word */
2388 word
= w_addchar (word
, &word_length
, &max_length
,
2392 error
= WRDE_NOSPACE
;
2399 /* If a word has been delimited, add it to the list. */
2402 error
= w_addword (pwordexp
, word
);
2407 word
= w_newword (&word_length
, &max_length
);
2412 /* There was a word separator at the end */
2413 if (word
== NULL
) /* i.e. w_newword */
2416 /* There was no field separator at the end */
2417 return w_addword (pwordexp
, word
);
2421 * free memory used (unless error is WRDE_NOSPACE), and
2422 * set pwordexp members back to what they were.
2427 if (error
== WRDE_NOSPACE
)
2428 return WRDE_NOSPACE
;
2430 if ((flags
& WRDE_APPEND
) == 0)
2431 wordfree (pwordexp
);
2433 *pwordexp
= old_word
;