#include "namespace.h"
#include <sys/cdefs.h>
#include <sys/types.h>
#include <assert.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wordexp.h>
#include "extern.h"
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
__FBSDID("$FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/wordexp.c,v 1.5 2004/04/09 11:32:32 tjr Exp $");
#else
__RCSID("$NetBSD: wordexp.c,v 1.4 2024/01/20 14:52:47 christos Exp $");
#endif
#endif
static int we_askshell(const char *, wordexp_t *, int);
static int we_check(const char *, int);
int
wordexp(const char * __restrict words, wordexp_t * __restrict we, int flags)
{
int error;
_DIAGASSERT(we != NULL);
_DIAGASSERT(words != NULL);
if (flags & WRDE_REUSE)
wordfree(we);
if ((flags & WRDE_APPEND) == 0) {
we->we_wordc = 0;
we->we_wordv = NULL;
we->we_strings = NULL;
we->we_nbytes = 0;
}
if ((error = we_check(words, flags)) != 0) {
wordfree(we);
return (error);
}
if ((error = we_askshell(words, we, flags)) != 0) {
wordfree(we);
return (error);
}
return (0);
}
static int
we_askshell(const char *words, wordexp_t *we, int flags)
{
int pdes[2];
size_t nwords, nbytes;
int i;
unsigned int ui;
size_t sofs;
size_t vofs;
pid_t pid;
int status;
const char *ifs;
char *np, *p;
char *nstrings;
char **nwv;
FILE *fp;
char *cmd;
if ((ifs = getenv("IFS")) == NULL)
ifs = " \t\n";
if (asprintf(&cmd, "wordexp%c%s\n", *ifs, words) < 0)
return (WRDE_NOSPACE);
if (pipe(pdes) < 0) {
free(cmd);
return (WRDE_ERRNO);
}
if ((fp = fdopen(pdes[0], "r")) == NULL) {
free(cmd);
return (WRDE_ERRNO);
}
if ((pid = fork()) < 0) {
free(cmd);
fclose(fp);
close(pdes[1]);
return (WRDE_ERRNO);
}
else if (pid == 0) {
int devnull;
close(pdes[0]);
if (pdes[1] != STDOUT_FILENO) {
if (dup2(pdes[1], STDOUT_FILENO) < 0)
_exit(1);
close(pdes[1]);
}
if ((flags & WRDE_SHOWERR) == 0) {
if ((devnull = open(_PATH_DEVNULL, O_RDWR, 0666)) < 0)
_exit(1);
if (dup2(devnull, STDERR_FILENO) < 0)
_exit(1);
close(devnull);
}
execle(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u",
"-c", cmd, (char *)NULL, environ);
_exit(1);
}
free(cmd);
close(pdes[1]);
nwords = 0;
while ((i = getc(fp)) != EOF) {
if (i == '\0')
break;
nwords *= 10;
nwords += (i - '0');
}
nbytes = 0;
while ((i = getc(fp)) != EOF) {
if (i == '\0')
break;
nbytes *= 10;
nbytes += (i - '0');
}
if (i == EOF) {
fclose(fp);
waitpid(pid, &status, 0);
return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX);
}
nbytes += nwords;
sofs = we->we_nbytes;
vofs = we->we_wordc;
if ((flags & (WRDE_DOOFFS|WRDE_APPEND)) == (WRDE_DOOFFS|WRDE_APPEND))
vofs += we->we_offs;
we->we_wordc += nwords;
we->we_nbytes += nbytes;
if ((nwv = realloc(we->we_wordv, (we->we_wordc + 1 +
(flags & WRDE_DOOFFS ? we->we_offs : 0)) *
sizeof(char *))) == NULL) {
fclose(fp);
waitpid(pid, &status, 0);
return (WRDE_NOSPACE);
}
we->we_wordv = nwv;
if ((nstrings = realloc(we->we_strings, we->we_nbytes)) == NULL) {
fclose(fp);
waitpid(pid, &status, 0);
return (WRDE_NOSPACE);
}
for (ui = 0; ui < vofs; ui++)
if (we->we_wordv[ui] != NULL)
we->we_wordv[ui] += nstrings - we->we_strings;
we->we_strings = nstrings;
if (fread(we->we_strings + sofs, sizeof(char), nbytes, fp) != nbytes) {
fclose(fp);
waitpid(pid, &status, 0);
return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX);
}
if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) ||
WEXITSTATUS(status) != 0) {
fclose(fp);
return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX);
}
fclose(fp);
if (vofs == 0 && flags & WRDE_DOOFFS)
while (vofs < we->we_offs)
we->we_wordv[vofs++] = NULL;
p = we->we_strings + sofs;
while (nwords-- != 0) {
we->we_wordv[vofs++] = p;
if ((np = memchr(p, '\0', nbytes)) == NULL)
return (WRDE_NOSPACE);
nbytes -= np - p + 1;
p = np + 1;
}
we->we_wordv[vofs] = NULL;
return (0);
}
static int
we_check(const char *words, int flags)
{
char c;
int dquote, level, quote, squote;
quote = squote = dquote = 0;
while ((c = *words++) != '\0') {
switch (c) {
case '\\':
quote ^= 1;
continue;
case '\'':
if (quote + dquote == 0)
squote ^= 1;
break;
case '"':
if (quote + squote == 0)
dquote ^= 1;
break;
case '`':
if (quote + squote == 0 && flags & WRDE_NOCMD)
return (WRDE_CMDSUB);
while ((c = *words++) != '\0' && c != '`')
if (c == '\\' && (c = *words++) == '\0')
break;
if (c == '\0')
return (WRDE_SYNTAX);
break;
case '|': case '&': case ';': case '<': case '>':
case '{': case '}': case '(': case ')': case '\n':
if (quote + squote + dquote == 0)
return (WRDE_BADCHAR);
break;
case '$':
if ((c = *words++) == '\0')
break;
else if (quote + squote == 0 && c == '(') {
if (flags & WRDE_NOCMD && *words != '(')
return (WRDE_CMDSUB);
level = 1;
while ((c = *words++) != '\0') {
if (c == '\\') {
if ((c = *words++) == '\0')
break;
} else if (c == '(')
level++;
else if (c == ')' && --level == 0)
break;
}
if (c == '\0' || level != 0)
return (WRDE_SYNTAX);
} else if (quote + squote == 0 && c == '{') {
level = 1;
while ((c = *words++) != '\0') {
if (c == '\\') {
if ((c = *words++) == '\0')
break;
} else if (c == '{')
level++;
else if (c == '}' && --level == 0)
break;
}
if (c == '\0' || level != 0)
return (WRDE_SYNTAX);
} else
c = *--words;
break;
default:
break;
}
quote = 0;
}
if (quote + squote + dquote != 0)
return (WRDE_SYNTAX);
return (0);
}
void
wordfree(wordexp_t *we)
{
_DIAGASSERT(we != NULL);
free(we->we_wordv);
free(we->we_strings);
we->we_wordv = NULL;
we->we_strings = NULL;
we->we_nbytes = 0;
we->we_wordc = 0;
}