xref: /freebsd/lib/libc/gen/wordexp.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1faea1495STim J. Robbins /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
4faea1495STim J. Robbins  * Copyright (c) 2002 Tim J. Robbins.
5faea1495STim J. Robbins  * All rights reserved.
6faea1495STim J. Robbins  *
7faea1495STim J. Robbins  * Redistribution and use in source and binary forms, with or without
8faea1495STim J. Robbins  * modification, are permitted provided that the following conditions
9faea1495STim J. Robbins  * are met:
10faea1495STim J. Robbins  * 1. Redistributions of source code must retain the above copyright
11faea1495STim J. Robbins  *    notice, this list of conditions and the following disclaimer.
12faea1495STim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
13faea1495STim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
14faea1495STim J. Robbins  *    documentation and/or other materials provided with the distribution.
15faea1495STim J. Robbins  *
16faea1495STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17faea1495STim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18faea1495STim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19faea1495STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20faea1495STim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21faea1495STim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22faea1495STim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23faea1495STim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24faea1495STim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25faea1495STim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26faea1495STim J. Robbins  * SUCH DAMAGE.
27faea1495STim J. Robbins  */
28faea1495STim J. Robbins 
29faea1495STim J. Robbins #include "namespace.h"
30faea1495STim J. Robbins #include <sys/types.h>
31faea1495STim J. Robbins #include <sys/wait.h>
32364e9ccbSJilles Tjoelker #include <errno.h>
33faea1495STim J. Robbins #include <fcntl.h>
34faea1495STim J. Robbins #include <paths.h>
35364e9ccbSJilles Tjoelker #include <signal.h>
36d358fa78SJilles Tjoelker #include <stdbool.h>
37faea1495STim J. Robbins #include <stdio.h>
38faea1495STim J. Robbins #include <stdlib.h>
39faea1495STim J. Robbins #include <string.h>
40faea1495STim J. Robbins #include <unistd.h>
41faea1495STim J. Robbins #include <wordexp.h>
42faea1495STim J. Robbins #include "un-namespace.h"
43bd6060a1SKonstantin Belousov #include "libc_private.h"
44faea1495STim J. Robbins static int	we_askshell(const char *, wordexp_t *, int);
45d358fa78SJilles Tjoelker static int	we_check(const char *);
46faea1495STim J. Robbins 
47faea1495STim J. Robbins /*
48faea1495STim J. Robbins  * wordexp --
49faea1495STim J. Robbins  *	Perform shell word expansion on `words' and place the resulting list
50faea1495STim J. Robbins  *	of words in `we'. See wordexp(3).
51faea1495STim J. Robbins  *
52faea1495STim J. Robbins  *	Specified by IEEE Std. 1003.1-2001.
53faea1495STim J. Robbins  */
54faea1495STim J. Robbins int
wordexp(const char * __restrict words,wordexp_t * __restrict we,int flags)55faea1495STim J. Robbins wordexp(const char * __restrict words, wordexp_t * __restrict we, int flags)
56faea1495STim J. Robbins {
57faea1495STim J. Robbins 	int error;
58faea1495STim J. Robbins 
59faea1495STim J. Robbins 	if (flags & WRDE_REUSE)
60faea1495STim J. Robbins 		wordfree(we);
61faea1495STim J. Robbins 	if ((flags & WRDE_APPEND) == 0) {
62faea1495STim J. Robbins 		we->we_wordc = 0;
63faea1495STim J. Robbins 		we->we_wordv = NULL;
64faea1495STim J. Robbins 		we->we_strings = NULL;
65faea1495STim J. Robbins 		we->we_nbytes = 0;
66faea1495STim J. Robbins 	}
67d358fa78SJilles Tjoelker 	if ((error = we_check(words)) != 0) {
68faea1495STim J. Robbins 		wordfree(we);
69faea1495STim J. Robbins 		return (error);
70faea1495STim J. Robbins 	}
71faea1495STim J. Robbins 	if ((error = we_askshell(words, we, flags)) != 0) {
72faea1495STim J. Robbins 		wordfree(we);
73faea1495STim J. Robbins 		return (error);
74faea1495STim J. Robbins 	}
75faea1495STim J. Robbins 	return (0);
76faea1495STim J. Robbins }
77faea1495STim J. Robbins 
78364e9ccbSJilles Tjoelker static size_t
we_read_fully(int fd,char * buffer,size_t len)79364e9ccbSJilles Tjoelker we_read_fully(int fd, char *buffer, size_t len)
80364e9ccbSJilles Tjoelker {
81364e9ccbSJilles Tjoelker 	size_t done;
82364e9ccbSJilles Tjoelker 	ssize_t nread;
83364e9ccbSJilles Tjoelker 
84364e9ccbSJilles Tjoelker 	done = 0;
85364e9ccbSJilles Tjoelker 	do {
86364e9ccbSJilles Tjoelker 		nread = _read(fd, buffer + done, len - done);
87364e9ccbSJilles Tjoelker 		if (nread == -1 && errno == EINTR)
88364e9ccbSJilles Tjoelker 			continue;
89364e9ccbSJilles Tjoelker 		if (nread <= 0)
90364e9ccbSJilles Tjoelker 			break;
91364e9ccbSJilles Tjoelker 		done += nread;
92364e9ccbSJilles Tjoelker 	} while (done != len);
93364e9ccbSJilles Tjoelker 	return done;
94364e9ccbSJilles Tjoelker }
95364e9ccbSJilles Tjoelker 
96d358fa78SJilles Tjoelker static bool
we_write_fully(int fd,const char * buffer,size_t len)97d358fa78SJilles Tjoelker we_write_fully(int fd, const char *buffer, size_t len)
98d358fa78SJilles Tjoelker {
99d358fa78SJilles Tjoelker 	size_t done;
100d358fa78SJilles Tjoelker 	ssize_t nwritten;
101d358fa78SJilles Tjoelker 
102d358fa78SJilles Tjoelker 	done = 0;
103d358fa78SJilles Tjoelker 	do {
104d358fa78SJilles Tjoelker 		nwritten = _write(fd, buffer + done, len - done);
105d358fa78SJilles Tjoelker 		if (nwritten == -1 && errno == EINTR)
106d358fa78SJilles Tjoelker 			continue;
107d358fa78SJilles Tjoelker 		if (nwritten <= 0)
108d358fa78SJilles Tjoelker 			return (false);
109d358fa78SJilles Tjoelker 		done += nwritten;
110d358fa78SJilles Tjoelker 	} while (done != len);
111d358fa78SJilles Tjoelker 	return (true);
112d358fa78SJilles Tjoelker }
113d358fa78SJilles Tjoelker 
114faea1495STim J. Robbins /*
115faea1495STim J. Robbins  * we_askshell --
116d358fa78SJilles Tjoelker  *	Use the `freebsd_wordexp' /bin/sh builtin function to do most of the
117d358fa78SJilles Tjoelker  *	work in expanding the word string. This function is complicated by
118faea1495STim J. Robbins  *	memory management.
119faea1495STim J. Robbins  */
120faea1495STim J. Robbins static int
we_askshell(const char * words,wordexp_t * we,int flags)121faea1495STim J. Robbins we_askshell(const char *words, wordexp_t *we, int flags)
122faea1495STim J. Robbins {
123d358fa78SJilles Tjoelker 	int pdesw[2];			/* Pipe for writing words */
124d358fa78SJilles Tjoelker 	int pdes[2];			/* Pipe for reading output */
125d358fa78SJilles Tjoelker 	char wfdstr[sizeof(int) * 3 + 1];
126d358fa78SJilles Tjoelker 	char buf[35];			/* Buffer for byte and word count */
127faea1495STim J. Robbins 	long nwords, nbytes;		/* Number of words, bytes from child */
128faea1495STim J. Robbins 	long i;				/* Handy integer */
129faea1495STim J. Robbins 	size_t sofs;			/* Offset into we->we_strings */
130faea1495STim J. Robbins 	size_t vofs;			/* Offset into we->we_wordv */
131faea1495STim J. Robbins 	pid_t pid;			/* Process ID of child */
132364e9ccbSJilles Tjoelker 	pid_t wpid;			/* waitpid return value */
133faea1495STim J. Robbins 	int status;			/* Child exit status */
134364e9ccbSJilles Tjoelker 	int error;			/* Our return value */
135364e9ccbSJilles Tjoelker 	int serrno;			/* errno to return */
136faea1495STim J. Robbins 	char *np, *p;			/* Handy pointers */
137faea1495STim J. Robbins 	char *nstrings;			/* Temporary for realloc() */
138faea1495STim J. Robbins 	char **nwv;			/* Temporary for realloc() */
139364e9ccbSJilles Tjoelker 	sigset_t newsigblock, oldsigblock;
1402f61288cSJilles Tjoelker 	const char *ifs;
141faea1495STim J. Robbins 
142364e9ccbSJilles Tjoelker 	serrno = errno;
1432f61288cSJilles Tjoelker 	ifs = getenv("IFS");
144faea1495STim J. Robbins 
145d358fa78SJilles Tjoelker 	if (pipe2(pdesw, O_CLOEXEC) < 0)
146faea1495STim J. Robbins 		return (WRDE_NOSPACE);	/* XXX */
147d358fa78SJilles Tjoelker 	snprintf(wfdstr, sizeof(wfdstr), "%d", pdesw[0]);
148d358fa78SJilles Tjoelker 	if (pipe2(pdes, O_CLOEXEC) < 0) {
149d358fa78SJilles Tjoelker 		_close(pdesw[0]);
150d358fa78SJilles Tjoelker 		_close(pdesw[1]);
151d358fa78SJilles Tjoelker 		return (WRDE_NOSPACE);	/* XXX */
152d358fa78SJilles Tjoelker 	}
153364e9ccbSJilles Tjoelker 	(void)sigemptyset(&newsigblock);
154364e9ccbSJilles Tjoelker 	(void)sigaddset(&newsigblock, SIGCHLD);
155bd6060a1SKonstantin Belousov 	(void)__libc_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
156faea1495STim J. Robbins 	if ((pid = fork()) < 0) {
157364e9ccbSJilles Tjoelker 		serrno = errno;
158d358fa78SJilles Tjoelker 		_close(pdesw[0]);
159d358fa78SJilles Tjoelker 		_close(pdesw[1]);
1602005f192STim J. Robbins 		_close(pdes[0]);
1612005f192STim J. Robbins 		_close(pdes[1]);
162bd6060a1SKonstantin Belousov 		(void)__libc_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
163364e9ccbSJilles Tjoelker 		errno = serrno;
164faea1495STim J. Robbins 		return (WRDE_NOSPACE);	/* XXX */
165faea1495STim J. Robbins 	}
166faea1495STim J. Robbins 	else if (pid == 0) {
167faea1495STim J. Robbins 		/*
168842ad8acSJilles Tjoelker 		 * We are the child; make /bin/sh expand `words'.
169faea1495STim J. Robbins 		 */
170bd6060a1SKonstantin Belousov 		(void)__libc_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
171f6d7148dSJilles Tjoelker 		if ((pdes[1] != STDOUT_FILENO ?
172f6d7148dSJilles Tjoelker 		    _dup2(pdes[1], STDOUT_FILENO) :
173f6d7148dSJilles Tjoelker 		    _fcntl(pdes[1], F_SETFD, 0)) < 0)
174faea1495STim J. Robbins 			_exit(1);
175d358fa78SJilles Tjoelker 		if (_fcntl(pdesw[0], F_SETFD, 0) < 0)
176d358fa78SJilles Tjoelker 			_exit(1);
177faea1495STim J. Robbins 		execl(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u",
178d358fa78SJilles Tjoelker 		    "-c", "IFS=$1;eval \"$2\";"
179d358fa78SJilles Tjoelker 		    "freebsd_wordexp -f \"$3\" ${4:+\"$4\"}",
180842ad8acSJilles Tjoelker 		    "",
1812f61288cSJilles Tjoelker 		    ifs != NULL ? ifs : " \t\n",
182d358fa78SJilles Tjoelker 		    flags & WRDE_SHOWERR ? "" : "exec 2>/dev/null",
183d358fa78SJilles Tjoelker 		    wfdstr,
184d358fa78SJilles Tjoelker 		    flags & WRDE_NOCMD ? "-p" : "",
185ae4c676cSJilles Tjoelker 		    (char *)NULL);
186faea1495STim J. Robbins 		_exit(1);
187faea1495STim J. Robbins 	}
188faea1495STim J. Robbins 
189faea1495STim J. Robbins 	/*
190d358fa78SJilles Tjoelker 	 * We are the parent; write the words.
191faea1495STim J. Robbins 	 */
1922005f192STim J. Robbins 	_close(pdes[1]);
193d358fa78SJilles Tjoelker 	_close(pdesw[0]);
194d358fa78SJilles Tjoelker 	if (!we_write_fully(pdesw[1], words, strlen(words))) {
195d358fa78SJilles Tjoelker 		_close(pdesw[1]);
196d358fa78SJilles Tjoelker 		error = WRDE_SYNTAX;
197d358fa78SJilles Tjoelker 		goto cleanup;
198d358fa78SJilles Tjoelker 	}
199d358fa78SJilles Tjoelker 	_close(pdesw[1]);
200d358fa78SJilles Tjoelker 	/*
201d358fa78SJilles Tjoelker 	 * Read the output of the shell wordexp function,
202d358fa78SJilles Tjoelker 	 * which is a byte indicating that the words were parsed successfully,
203d358fa78SJilles Tjoelker 	 * a 64-bit hexadecimal word count, a dummy byte, a 64-bit hexadecimal
204d358fa78SJilles Tjoelker 	 * byte count (not including terminating null bytes), followed by the
205d358fa78SJilles Tjoelker 	 * expanded words separated by nulls.
206d358fa78SJilles Tjoelker 	 */
207d358fa78SJilles Tjoelker 	switch (we_read_fully(pdes[0], buf, 34)) {
20889cead33SJilles Tjoelker 	case 1:
209d358fa78SJilles Tjoelker 		error = buf[0] == 'C' ? WRDE_CMDSUB : WRDE_BADVAL;
21089cead33SJilles Tjoelker 		serrno = errno;
21189cead33SJilles Tjoelker 		goto cleanup;
212d358fa78SJilles Tjoelker 	case 34:
21389cead33SJilles Tjoelker 		break;
21489cead33SJilles Tjoelker 	default:
21589cead33SJilles Tjoelker 		error = WRDE_SYNTAX;
216364e9ccbSJilles Tjoelker 		serrno = errno;
217364e9ccbSJilles Tjoelker 		goto cleanup;
218faea1495STim J. Robbins 	}
21989cead33SJilles Tjoelker 	buf[17] = '\0';
220d358fa78SJilles Tjoelker 	nwords = strtol(buf + 1, NULL, 16);
221d358fa78SJilles Tjoelker 	buf[34] = '\0';
222d358fa78SJilles Tjoelker 	nbytes = strtol(buf + 18, NULL, 16) + nwords;
223faea1495STim J. Robbins 
224faea1495STim J. Robbins 	/*
225faea1495STim J. Robbins 	 * Allocate or reallocate (when flags & WRDE_APPEND) the word vector
226faea1495STim J. Robbins 	 * and string storage buffers for the expanded words we're about to
227faea1495STim J. Robbins 	 * read from the child.
228faea1495STim J. Robbins 	 */
229faea1495STim J. Robbins 	sofs = we->we_nbytes;
230faea1495STim J. Robbins 	vofs = we->we_wordc;
231b7114d4aSTim J. Robbins 	if ((flags & (WRDE_DOOFFS|WRDE_APPEND)) == (WRDE_DOOFFS|WRDE_APPEND))
232fe634ca7STim J. Robbins 		vofs += we->we_offs;
233faea1495STim J. Robbins 	we->we_wordc += nwords;
234faea1495STim J. Robbins 	we->we_nbytes += nbytes;
2359f36610fSPedro F. Giffuni 	if ((nwv = reallocarray(we->we_wordv, (we->we_wordc + 1 +
2369f36610fSPedro F. Giffuni 	    (flags & WRDE_DOOFFS ? we->we_offs : 0)),
237faea1495STim J. Robbins 	    sizeof(char *))) == NULL) {
238364e9ccbSJilles Tjoelker 		error = WRDE_NOSPACE;
239364e9ccbSJilles Tjoelker 		goto cleanup;
240faea1495STim J. Robbins 	}
241faea1495STim J. Robbins 	we->we_wordv = nwv;
242faea1495STim J. Robbins 	if ((nstrings = realloc(we->we_strings, we->we_nbytes)) == NULL) {
243364e9ccbSJilles Tjoelker 		error = WRDE_NOSPACE;
244364e9ccbSJilles Tjoelker 		goto cleanup;
245faea1495STim J. Robbins 	}
246faea1495STim J. Robbins 	for (i = 0; i < vofs; i++)
247faea1495STim J. Robbins 		if (we->we_wordv[i] != NULL)
248faea1495STim J. Robbins 			we->we_wordv[i] += nstrings - we->we_strings;
249faea1495STim J. Robbins 	we->we_strings = nstrings;
250faea1495STim J. Robbins 
251364e9ccbSJilles Tjoelker 	if (we_read_fully(pdes[0], we->we_strings + sofs, nbytes) != nbytes) {
25289cead33SJilles Tjoelker 		error = WRDE_NOSPACE; /* abort for unknown reason */
253364e9ccbSJilles Tjoelker 		serrno = errno;
254364e9ccbSJilles Tjoelker 		goto cleanup;
255faea1495STim J. Robbins 	}
256faea1495STim J. Robbins 
257364e9ccbSJilles Tjoelker 	error = 0;
258364e9ccbSJilles Tjoelker cleanup:
2592005f192STim J. Robbins 	_close(pdes[0]);
260364e9ccbSJilles Tjoelker 	do
261364e9ccbSJilles Tjoelker 		wpid = _waitpid(pid, &status, 0);
262364e9ccbSJilles Tjoelker 	while (wpid < 0 && errno == EINTR);
263bd6060a1SKonstantin Belousov 	(void)__libc_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
264364e9ccbSJilles Tjoelker 	if (error != 0) {
265364e9ccbSJilles Tjoelker 		errno = serrno;
266364e9ccbSJilles Tjoelker 		return (error);
267faea1495STim J. Robbins 	}
268364e9ccbSJilles Tjoelker 	if (wpid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
26989cead33SJilles Tjoelker 		return (WRDE_NOSPACE); /* abort for unknown reason */
270faea1495STim J. Robbins 
271faea1495STim J. Robbins 	/*
272faea1495STim J. Robbins 	 * Break the null-terminated expanded word strings out into
273faea1495STim J. Robbins 	 * the vector.
274faea1495STim J. Robbins 	 */
275b7114d4aSTim J. Robbins 	if (vofs == 0 && flags & WRDE_DOOFFS)
276faea1495STim J. Robbins 		while (vofs < we->we_offs)
277faea1495STim J. Robbins 			we->we_wordv[vofs++] = NULL;
278faea1495STim J. Robbins 	p = we->we_strings + sofs;
279faea1495STim J. Robbins 	while (nwords-- != 0) {
280faea1495STim J. Robbins 		we->we_wordv[vofs++] = p;
281faea1495STim J. Robbins 		if ((np = memchr(p, '\0', nbytes)) == NULL)
282faea1495STim J. Robbins 			return (WRDE_NOSPACE);	/* XXX */
283faea1495STim J. Robbins 		nbytes -= np - p + 1;
284faea1495STim J. Robbins 		p = np + 1;
285faea1495STim J. Robbins 	}
286faea1495STim J. Robbins 	we->we_wordv[vofs] = NULL;
287faea1495STim J. Robbins 
288faea1495STim J. Robbins 	return (0);
289faea1495STim J. Robbins }
290faea1495STim J. Robbins 
291faea1495STim J. Robbins /*
292faea1495STim J. Robbins  * we_check --
293faea1495STim J. Robbins  *	Check that the string contains none of the following unquoted
294faea1495STim J. Robbins  *	special characters: <newline> |&;<>(){}
295d358fa78SJilles Tjoelker  *	This mainly serves for {} which are normally legal in sh.
296d358fa78SJilles Tjoelker  *	It deliberately does not attempt to model full sh syntax.
297faea1495STim J. Robbins  */
29897c1c8f8STim J. Robbins static int
we_check(const char * words)299d358fa78SJilles Tjoelker we_check(const char *words)
300faea1495STim J. Robbins {
301faea1495STim J. Robbins 	char c;
302d358fa78SJilles Tjoelker 	/* Saw \ or $, possibly not special: */
303d358fa78SJilles Tjoelker 	bool quote = false, dollar = false;
304d358fa78SJilles Tjoelker 	/* Saw ', ", ${, ` or $(, possibly not special: */
305d358fa78SJilles Tjoelker 	bool have_sq = false, have_dq = false, have_par_begin = false;
306d358fa78SJilles Tjoelker 	bool have_cmd = false;
307d358fa78SJilles Tjoelker 	/* Definitely saw a ', ", ${, ` or $(, need a closing character: */
308d358fa78SJilles Tjoelker 	bool need_sq = false, need_dq = false, need_par_end = false;
309d358fa78SJilles Tjoelker 	bool need_cmd_old = false, need_cmd_new = false;
310faea1495STim J. Robbins 
311faea1495STim J. Robbins 	while ((c = *words++) != '\0') {
312faea1495STim J. Robbins 		switch (c) {
313faea1495STim J. Robbins 		case '\\':
314d358fa78SJilles Tjoelker 			quote = !quote;
315d358fa78SJilles Tjoelker 			continue;
316d358fa78SJilles Tjoelker 		case '$':
317d358fa78SJilles Tjoelker 			if (quote)
318d358fa78SJilles Tjoelker 				quote = false;
319d358fa78SJilles Tjoelker 			else
320d358fa78SJilles Tjoelker 				dollar = !dollar;
321fe634ca7STim J. Robbins 			continue;
322faea1495STim J. Robbins 		case '\'':
323d358fa78SJilles Tjoelker 			if (!quote && !have_sq && !have_dq)
324d358fa78SJilles Tjoelker 				need_sq = true;
325d358fa78SJilles Tjoelker 			else
326d358fa78SJilles Tjoelker 				need_sq = false;
327d358fa78SJilles Tjoelker 			have_sq = true;
328faea1495STim J. Robbins 			break;
329faea1495STim J. Robbins 		case '"':
330d358fa78SJilles Tjoelker 			if (!quote && !have_sq && !have_dq)
331d358fa78SJilles Tjoelker 				need_dq = true;
332d358fa78SJilles Tjoelker 			else
333d358fa78SJilles Tjoelker 				need_dq = false;
334d358fa78SJilles Tjoelker 			have_dq = true;
335faea1495STim J. Robbins 			break;
336faea1495STim J. Robbins 		case '`':
337d358fa78SJilles Tjoelker 			if (!quote && !have_sq && !have_cmd)
338d358fa78SJilles Tjoelker 				need_cmd_old = true;
339d358fa78SJilles Tjoelker 			else
340d358fa78SJilles Tjoelker 				need_cmd_old = false;
341d358fa78SJilles Tjoelker 			have_cmd = true;
342faea1495STim J. Robbins 			break;
343d358fa78SJilles Tjoelker 		case '{':
344d358fa78SJilles Tjoelker 			if (!quote && !dollar && !have_sq && !have_dq &&
345d358fa78SJilles Tjoelker 			    !have_cmd)
346faea1495STim J. Robbins 				return (WRDE_BADCHAR);
347d358fa78SJilles Tjoelker 			if (dollar) {
348d358fa78SJilles Tjoelker 				if (!quote && !have_sq)
349d358fa78SJilles Tjoelker 					need_par_end = true;
350d358fa78SJilles Tjoelker 				have_par_begin = true;
351faea1495STim J. Robbins 			}
352faea1495STim J. Robbins 			break;
353d358fa78SJilles Tjoelker 		case '}':
354d358fa78SJilles Tjoelker 			if (!quote && !have_sq && !have_dq && !have_par_begin &&
355d358fa78SJilles Tjoelker 			    !have_cmd)
356d358fa78SJilles Tjoelker 				return (WRDE_BADCHAR);
357d358fa78SJilles Tjoelker 			need_par_end = false;
358faea1495STim J. Robbins 			break;
359d358fa78SJilles Tjoelker 		case '(':
360d358fa78SJilles Tjoelker 			if (!quote && !dollar && !have_sq && !have_dq &&
361d358fa78SJilles Tjoelker 			    !have_cmd)
362d358fa78SJilles Tjoelker 				return (WRDE_BADCHAR);
363d358fa78SJilles Tjoelker 			if (dollar) {
364d358fa78SJilles Tjoelker 				if (!quote && !have_sq)
365d358fa78SJilles Tjoelker 					need_cmd_new = true;
366d358fa78SJilles Tjoelker 				have_cmd = true;
367faea1495STim J. Robbins 			}
368d358fa78SJilles Tjoelker 			break;
369d358fa78SJilles Tjoelker 		case ')':
370d358fa78SJilles Tjoelker 			if (!quote && !have_sq && !have_dq && !have_cmd)
371d358fa78SJilles Tjoelker 				return (WRDE_BADCHAR);
372d358fa78SJilles Tjoelker 			need_cmd_new = false;
373d358fa78SJilles Tjoelker 			break;
374d358fa78SJilles Tjoelker 		case '|': case '&': case ';': case '<': case '>': case '\n':
375d358fa78SJilles Tjoelker 			if (!quote && !have_sq && !have_dq && !have_cmd)
376d358fa78SJilles Tjoelker 				return (WRDE_BADCHAR);
377faea1495STim J. Robbins 			break;
378faea1495STim J. Robbins 		default:
379faea1495STim J. Robbins 			break;
380faea1495STim J. Robbins 		}
381d358fa78SJilles Tjoelker 		quote = dollar = false;
382faea1495STim J. Robbins 	}
383d358fa78SJilles Tjoelker 	if (quote || dollar || need_sq || need_dq || need_par_end ||
384d358fa78SJilles Tjoelker 	    need_cmd_old || need_cmd_new)
385faea1495STim J. Robbins 		return (WRDE_SYNTAX);
386faea1495STim J. Robbins 
387faea1495STim J. Robbins 	return (0);
388faea1495STim J. Robbins }
389faea1495STim J. Robbins 
390faea1495STim J. Robbins /*
391faea1495STim J. Robbins  * wordfree --
392faea1495STim J. Robbins  *	Free the result of wordexp(). See wordexp(3).
393faea1495STim J. Robbins  *
394faea1495STim J. Robbins  *	Specified by IEEE Std. 1003.1-2001.
395faea1495STim J. Robbins  */
396faea1495STim J. Robbins void
wordfree(wordexp_t * we)397faea1495STim J. Robbins wordfree(wordexp_t *we)
398faea1495STim J. Robbins {
399faea1495STim J. Robbins 
400faea1495STim J. Robbins 	if (we == NULL)
401faea1495STim J. Robbins 		return;
402faea1495STim J. Robbins 	free(we->we_wordv);
403faea1495STim J. Robbins 	free(we->we_strings);
404faea1495STim J. Robbins 	we->we_wordv = NULL;
405faea1495STim J. Robbins 	we->we_strings = NULL;
406faea1495STim J. Robbins 	we->we_nbytes = 0;
407faea1495STim J. Robbins 	we->we_wordc = 0;
408faea1495STim J. Robbins }
409