2 * Copyright (c) 2002 Tim J. Robbins.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include "namespace.h"
28 #include <sys/cdefs.h>
29 #include <sys/types.h>
40 #include "un-namespace.h"
42 __FBSDID("$FreeBSD$");
44 static int we_askshell(const char *, wordexp_t *, int);
45 static int we_check(const char *, int);
49 * Perform shell word expansion on `words' and place the resulting list
50 * of words in `we'. See wordexp(3).
52 * Specified by IEEE Std. 1003.1-2001.
55 wordexp(const char * __restrict words, wordexp_t * __restrict we, int flags)
59 if (flags & WRDE_REUSE)
61 if ((flags & WRDE_APPEND) == 0) {
64 we->we_strings = NULL;
67 if ((error = we_check(words, flags)) != 0) {
71 if ((error = we_askshell(words, we, flags)) != 0) {
79 we_read_fully(int fd, char *buffer, size_t len)
86 nread = _read(fd, buffer + done, len - done);
87 if (nread == -1 && errno == EINTR)
92 } while (done != len);
98 * Use the `wordexp' /bin/sh builtin function to do most of the work
99 * in expanding the word string. This function is complicated by
103 we_askshell(const char *words, wordexp_t *we, int flags)
105 int pdes[2]; /* Pipe to child */
106 char bbuf[9]; /* Buffer for byte count */
107 char wbuf[9]; /* Buffer for word count */
108 long nwords, nbytes; /* Number of words, bytes from child */
109 long i; /* Handy integer */
110 size_t sofs; /* Offset into we->we_strings */
111 size_t vofs; /* Offset into we->we_wordv */
112 pid_t pid; /* Process ID of child */
113 pid_t wpid; /* waitpid return value */
114 int status; /* Child exit status */
115 int error; /* Our return value */
116 int serrno; /* errno to return */
117 char *ifs; /* IFS env. var. */
118 char *np, *p; /* Handy pointers */
119 char *nstrings; /* Temporary for realloc() */
120 char **nwv; /* Temporary for realloc() */
121 sigset_t newsigblock, oldsigblock;
124 if ((ifs = getenv("IFS")) == NULL)
128 return (WRDE_NOSPACE); /* XXX */
129 (void)sigemptyset(&newsigblock);
130 (void)sigaddset(&newsigblock, SIGCHLD);
131 (void)_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
132 if ((pid = fork()) < 0) {
136 (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
138 return (WRDE_NOSPACE); /* XXX */
142 * We are the child; just get /bin/sh to run the wordexp
143 * builtin on `words'.
148 (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
150 if (_dup2(pdes[1], STDOUT_FILENO) < 0)
153 if (asprintf(&cmd, "wordexp%c%s\n", *ifs, words) < 0)
155 if ((flags & WRDE_SHOWERR) == 0) {
156 if ((devnull = _open(_PATH_DEVNULL, O_RDWR, 0666)) < 0)
158 if (_dup2(devnull, STDERR_FILENO) < 0)
162 execl(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u",
163 "-c", cmd, (char *)NULL);
168 * We are the parent; read the output of the shell wordexp function,
169 * which is a 32-bit hexadecimal word count, a 32-bit hexadecimal
170 * byte count (not including terminating null bytes), followed by
171 * the expanded words separated by nulls.
174 if (we_read_fully(pdes[0], wbuf, 8) != 8 ||
175 we_read_fully(pdes[0], bbuf, 8) != 8) {
176 error = flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX;
180 wbuf[8] = bbuf[8] = '\0';
181 nwords = strtol(wbuf, NULL, 16);
182 nbytes = strtol(bbuf, NULL, 16) + nwords;
185 * Allocate or reallocate (when flags & WRDE_APPEND) the word vector
186 * and string storage buffers for the expanded words we're about to
187 * read from the child.
189 sofs = we->we_nbytes;
191 if ((flags & (WRDE_DOOFFS|WRDE_APPEND)) == (WRDE_DOOFFS|WRDE_APPEND))
193 we->we_wordc += nwords;
194 we->we_nbytes += nbytes;
195 if ((nwv = realloc(we->we_wordv, (we->we_wordc + 1 +
196 (flags & WRDE_DOOFFS ? we->we_offs : 0)) *
197 sizeof(char *))) == NULL) {
198 error = WRDE_NOSPACE;
202 if ((nstrings = realloc(we->we_strings, we->we_nbytes)) == NULL) {
203 error = WRDE_NOSPACE;
206 for (i = 0; i < vofs; i++)
207 if (we->we_wordv[i] != NULL)
208 we->we_wordv[i] += nstrings - we->we_strings;
209 we->we_strings = nstrings;
211 if (we_read_fully(pdes[0], we->we_strings + sofs, nbytes) != nbytes) {
212 error = flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX;
221 wpid = _waitpid(pid, &status, 0);
222 while (wpid < 0 && errno == EINTR);
223 (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
228 if (wpid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
229 return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX);
232 * Break the null-terminated expanded word strings out into
235 if (vofs == 0 && flags & WRDE_DOOFFS)
236 while (vofs < we->we_offs)
237 we->we_wordv[vofs++] = NULL;
238 p = we->we_strings + sofs;
239 while (nwords-- != 0) {
240 we->we_wordv[vofs++] = p;
241 if ((np = memchr(p, '\0', nbytes)) == NULL)
242 return (WRDE_NOSPACE); /* XXX */
243 nbytes -= np - p + 1;
246 we->we_wordv[vofs] = NULL;
253 * Check that the string contains none of the following unquoted
254 * special characters: <newline> |&;<>(){}
255 * or command substitutions when WRDE_NOCMD is set in flags.
258 we_check(const char *words, int flags)
261 int dquote, level, quote, squote;
263 quote = squote = dquote = 0;
264 while ((c = *words++) != '\0') {
270 if (quote + dquote == 0)
274 if (quote + squote == 0)
278 if (quote + squote == 0 && flags & WRDE_NOCMD)
279 return (WRDE_CMDSUB);
280 while ((c = *words++) != '\0' && c != '`')
281 if (c == '\\' && (c = *words++) == '\0')
284 return (WRDE_SYNTAX);
286 case '|': case '&': case ';': case '<': case '>':
287 case '{': case '}': case '(': case ')': case '\n':
288 if (quote + squote + dquote == 0)
289 return (WRDE_BADCHAR);
292 if ((c = *words++) == '\0')
294 else if (quote + squote == 0 && c == '(') {
295 if (flags & WRDE_NOCMD && *words != '(')
296 return (WRDE_CMDSUB);
298 while ((c = *words++) != '\0') {
300 if ((c = *words++) == '\0')
304 else if (c == ')' && --level == 0)
307 if (c == '\0' || level != 0)
308 return (WRDE_SYNTAX);
309 } else if (quote + squote == 0 && c == '{') {
311 while ((c = *words++) != '\0') {
313 if ((c = *words++) == '\0')
317 else if (c == '}' && --level == 0)
320 if (c == '\0' || level != 0)
321 return (WRDE_SYNTAX);
330 if (quote + squote + dquote != 0)
331 return (WRDE_SYNTAX);
338 * Free the result of wordexp(). See wordexp(3).
340 * Specified by IEEE Std. 1003.1-2001.
343 wordfree(wordexp_t *we)
349 free(we->we_strings);
351 we->we_strings = NULL;