Home | History | Annotate | Line # | Download | only in gen
wordexp.c revision 1.2
      1 /*	$NetBSD: wordexp.c,v 1.2 2005/11/29 03:11:59 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 Tim J. Robbins.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include "namespace.h"
     30 #include <sys/cdefs.h>
     31 #include <sys/types.h>
     32 #include <assert.h>
     33 #include <sys/wait.h>
     34 #include <fcntl.h>
     35 #include <paths.h>
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <string.h>
     39 #include <unistd.h>
     40 #include <wordexp.h>
     41 
     42 #if defined(LIBC_SCCS) && !defined(lint)
     43 #if 0
     44 __FBSDID("$FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/wordexp.c,v 1.5 2004/04/09 11:32:32 tjr Exp $");
     45 #else
     46 __RCSID("$NetBSD: wordexp.c,v 1.2 2005/11/29 03:11:59 christos Exp $");
     47 #endif
     48 #endif /* LIBC_SCCS and not lint */
     49 
     50 static int	we_askshell(const char *, wordexp_t *, int);
     51 static int	we_check(const char *, int);
     52 
     53 /*
     54  * wordexp --
     55  *	Perform shell word expansion on `words' and place the resulting list
     56  *	of words in `we'. See wordexp(3).
     57  *
     58  */
     59 int
     60 wordexp(const char * __restrict words, wordexp_t * __restrict we, int flags)
     61 {
     62 	int error;
     63 
     64 	_DIAGASSERT(we != NULL);
     65 	_DIAGASSERT(words != NULL);
     66 	if (flags & WRDE_REUSE)
     67 		wordfree(we);
     68 	if ((flags & WRDE_APPEND) == 0) {
     69 		we->we_wordc = 0;
     70 		we->we_wordv = NULL;
     71 		we->we_strings = NULL;
     72 		we->we_nbytes = 0;
     73 	}
     74 	if ((error = we_check(words, flags)) != 0) {
     75 		wordfree(we);
     76 		return (error);
     77 	}
     78 	if ((error = we_askshell(words, we, flags)) != 0) {
     79 		wordfree(we);
     80 		return (error);
     81 	}
     82 	return (0);
     83 }
     84 
     85 /*
     86  * we_askshell --
     87  *	Use the `wordexp' /bin/sh builtin function to do most of the work
     88  *	in expanding the word string. This function is complicated by
     89  *	memory management.
     90  */
     91 static int
     92 we_askshell(const char *words, wordexp_t *we, int flags)
     93 {
     94 	int pdes[2];			/* Pipe to child */
     95 	size_t nwords, nbytes;		/* Number of words, bytes from child */
     96 	int i;				/* Handy integer */
     97 	size_t sofs;			/* Offset into we->we_strings */
     98 	size_t vofs;			/* Offset into we->we_wordv */
     99 	pid_t pid;			/* Process ID of child */
    100 	int status;			/* Child exit status */
    101 	const char *ifs;		/* IFS env. var. */
    102 	char *np, *p;			/* Handy pointers */
    103 	char *nstrings;			/* Temporary for realloc() */
    104 	char **nwv;			/* Temporary for realloc() */
    105 	FILE *fp;			/* Stream to read pipe */
    106 	extern char **environ;
    107 	char *cmd;
    108 
    109 	if ((ifs = getenv("IFS")) == NULL)
    110 		ifs = " \t\n";
    111 	if (asprintf(&cmd, "wordexp%c%s\n", *ifs, words) < 0)
    112 		return (WRDE_NOSPACE);
    113 	if (pipe(pdes) < 0) {
    114 		free(cmd);
    115 		return (WRDE_ERRNO);
    116 	}
    117 	if ((fp = fdopen(pdes[0], "r")) == NULL) {
    118 		free(cmd);
    119 		return (WRDE_ERRNO);
    120 	}
    121 	if ((pid = fork()) < 0) {
    122 		free(cmd);
    123 		fclose(fp);
    124 		close(pdes[1]);
    125 		return (WRDE_ERRNO);
    126 	}
    127 	else if (pid == 0) {
    128 		/*
    129 		 * We are the child; just get /bin/sh to run the wordexp
    130 		 * builtin on `words'.
    131 		 */
    132 		int devnull;
    133 
    134 		close(pdes[0]);
    135 		if (pdes[1] != STDOUT_FILENO) {
    136 			if (dup2(pdes[1], STDOUT_FILENO) < 0)
    137 				_exit(1);
    138 			close(pdes[1]);
    139 		}
    140 		if ((flags & WRDE_SHOWERR) == 0) {
    141 			if ((devnull = open(_PATH_DEVNULL, O_RDWR, 0666)) < 0)
    142 				_exit(1);
    143 			if (dup2(devnull, STDERR_FILENO) < 0)
    144 				_exit(1);
    145 			close(devnull);
    146 		}
    147 		execle(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u",
    148 		    "-c", cmd, (char *)NULL, environ);
    149 		_exit(1);
    150 	}
    151 
    152 	/*
    153 	 * We are the parent; read the output of the shell wordexp function,
    154 	 * which is a decimal word count, an null, a decimal byte count,
    155 	 * (not including terminating null bytes), a null and then followed
    156 	 * by the expanded words separated by nulls.
    157 	 */
    158 	free(cmd);
    159 	close(pdes[1]);
    160 	/* read the word count */
    161 	nwords = 0;
    162 	while ((i = getc(fp)) != EOF) {
    163 		if (i == '\0')
    164 			break;
    165 		nwords *= 10;
    166 		nwords += (i - '0');
    167 	}
    168 	/* read the byte count */
    169 	nbytes = 0;
    170 	while ((i = getc(fp)) != EOF) {
    171 		if (i == '\0')
    172 			break;
    173 		nbytes *= 10;
    174 		nbytes += (i - '0');
    175 	}
    176 	if (i == EOF) {
    177 		fclose(fp);
    178 		waitpid(pid, &status, 0);
    179 		return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX);
    180 	}
    181 	nbytes += nwords;
    182 
    183 	/*
    184 	 * Allocate or reallocate (when flags & WRDE_APPEND) the word vector
    185 	 * and string storage buffers for the expanded words we're about to
    186 	 * read from the child.
    187 	 */
    188 	sofs = we->we_nbytes;
    189 	vofs = we->we_wordc;
    190 	if ((flags & (WRDE_DOOFFS|WRDE_APPEND)) == (WRDE_DOOFFS|WRDE_APPEND))
    191 		vofs += we->we_offs;
    192 	we->we_wordc += nwords;
    193 	we->we_nbytes += nbytes;
    194 	if ((nwv = realloc(we->we_wordv, (we->we_wordc + 1 +
    195 	    (flags & WRDE_DOOFFS ?  we->we_offs : 0)) *
    196 	    sizeof(char *))) == NULL) {
    197 		fclose(fp);
    198 		waitpid(pid, &status, 0);
    199 		return (WRDE_NOSPACE);
    200 	}
    201 	we->we_wordv = nwv;
    202 	if ((nstrings = realloc(we->we_strings, we->we_nbytes)) == NULL) {
    203 		fclose(fp);
    204 		waitpid(pid, &status, 0);
    205 		return (WRDE_NOSPACE);
    206 	}
    207 	for (i = 0; i < vofs; i++)
    208 		if (we->we_wordv[i] != NULL)
    209 			we->we_wordv[i] += nstrings - we->we_strings;
    210 	we->we_strings = nstrings;
    211 
    212 	if (fread(we->we_strings + sofs, sizeof(char), nbytes, fp) != nbytes) {
    213 		fclose(fp);
    214 		waitpid(pid, &status, 0);
    215 		return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX);
    216 	}
    217 
    218 	if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) ||
    219 	    WEXITSTATUS(status) != 0) {
    220 		fclose(fp);
    221 		return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX);
    222 	}
    223 	fclose(fp);
    224 
    225 	/*
    226 	 * Break the null-terminated expanded word strings out into
    227 	 * the vector.
    228 	 */
    229 	if (vofs == 0 && flags & WRDE_DOOFFS)
    230 		while (vofs < we->we_offs)
    231 			we->we_wordv[vofs++] = NULL;
    232 	p = we->we_strings + sofs;
    233 	while (nwords-- != 0) {
    234 		we->we_wordv[vofs++] = p;
    235 		if ((np = memchr(p, '\0', nbytes)) == NULL)
    236 			return (WRDE_NOSPACE);	/* XXX */
    237 		nbytes -= np - p + 1;
    238 		p = np + 1;
    239 	}
    240 	we->we_wordv[vofs] = NULL;
    241 
    242 	return (0);
    243 }
    244 
    245 /*
    246  * we_check --
    247  *	Check that the string contains none of the following unquoted
    248  *	special characters: <newline> |&;<>(){}
    249  *	or command substitutions when WRDE_NOCMD is set in flags.
    250  */
    251 static int
    252 we_check(const char *words, int flags)
    253 {
    254 	char c;
    255 	int dquote, level, quote, squote;
    256 
    257 	quote = squote = dquote = 0;
    258 	while ((c = *words++) != '\0') {
    259 		switch (c) {
    260 		case '\\':
    261 			quote ^= 1;
    262 			continue;
    263 		case '\'':
    264 			if (quote + dquote == 0)
    265 				squote ^= 1;
    266 			break;
    267 		case '"':
    268 			if (quote + squote == 0)
    269 				dquote ^= 1;
    270 			break;
    271 		case '`':
    272 			if (quote + squote == 0 && flags & WRDE_NOCMD)
    273 				return (WRDE_CMDSUB);
    274 			while ((c = *words++) != '\0' && c != '`')
    275 				if (c == '\\' && (c = *words++) == '\0')
    276 					break;
    277 			if (c == '\0')
    278 				return (WRDE_SYNTAX);
    279 			break;
    280 		case '|': case '&': case ';': case '<': case '>':
    281 		case '{': case '}': case '(': case ')': case '\n':
    282 			if (quote + squote + dquote == 0)
    283 				return (WRDE_BADCHAR);
    284 			break;
    285 		case '$':
    286 			if ((c = *words++) == '\0')
    287 				break;
    288 			else if (quote + squote == 0 && c == '(') {
    289 				if (flags & WRDE_NOCMD && *words != '(')
    290 					return (WRDE_CMDSUB);
    291 				level = 1;
    292 				while ((c = *words++) != '\0') {
    293 					if (c == '\\') {
    294 						if ((c = *words++) == '\0')
    295 							break;
    296 					} else if (c == '(')
    297 						level++;
    298 					else if (c == ')' && --level == 0)
    299 						break;
    300 				}
    301 				if (c == '\0' || level != 0)
    302 					return (WRDE_SYNTAX);
    303 			} else if (quote + squote == 0 && c == '{') {
    304 				level = 1;
    305 				while ((c = *words++) != '\0') {
    306 					if (c == '\\') {
    307 						if ((c = *words++) == '\0')
    308 							break;
    309 					} else if (c == '{')
    310 						level++;
    311 					else if (c == '}' && --level == 0)
    312 						break;
    313 				}
    314 				if (c == '\0' || level != 0)
    315 					return (WRDE_SYNTAX);
    316 			} else
    317 				c = *--words;
    318 			break;
    319 		default:
    320 			break;
    321 		}
    322 		quote = 0;
    323 	}
    324 	if (quote + squote + dquote != 0)
    325 		return (WRDE_SYNTAX);
    326 
    327 	return (0);
    328 }
    329 
    330 /*
    331  * wordfree --
    332  *	Free the result of wordexp(). See wordexp(3).
    333  *
    334  */
    335 void
    336 wordfree(wordexp_t *we)
    337 {
    338 	_DIAGASSERT(we != NULL);
    339 	free(we->we_wordv);
    340 	free(we->we_strings);
    341 	we->we_wordv = NULL;
    342 	we->we_strings = NULL;
    343 	we->we_nbytes = 0;
    344 	we->we_wordc = 0;
    345 }
    346