2014-01-03 06:52:47 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-01-04 08:51:13 -05:00
|
|
|
#include <errno.h>
|
2014-01-03 06:52:47 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
NARGS = 5000
|
|
|
|
};
|
|
|
|
|
|
|
|
static int inputc(void);
|
|
|
|
static void deinputc(int);
|
2014-01-07 09:56:05 -05:00
|
|
|
static void fillargbuf(int);
|
2014-01-06 13:12:06 -05:00
|
|
|
static int eatspace(void);
|
2014-01-03 06:52:47 -05:00
|
|
|
static int parsequote(int);
|
2014-01-06 13:20:26 -05:00
|
|
|
static int parseescape(void);
|
2014-01-03 06:52:47 -05:00
|
|
|
static char *poparg(void);
|
|
|
|
static void pusharg(char *);
|
2014-01-07 09:50:59 -05:00
|
|
|
static void waitchld(void);
|
2014-01-04 08:51:13 -05:00
|
|
|
static void runcmd(void);
|
2014-01-03 06:52:47 -05:00
|
|
|
|
|
|
|
static char **cmd;
|
|
|
|
static char *argb;
|
|
|
|
static size_t argbsz = 1;
|
|
|
|
static size_t argbpos;
|
2014-01-06 13:51:06 -05:00
|
|
|
static int nerrors = 0;
|
2014-01-07 06:53:55 -05:00
|
|
|
static char *eofstr;
|
2014-01-03 06:52:47 -05:00
|
|
|
static int rflag = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-01-07 06:53:55 -05:00
|
|
|
eprintf("usage: %s [-r] [-E eofstr] [cmd [arg...]]\n", argv0);
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
long argsz, argmaxsz;
|
|
|
|
char *arg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
2014-01-07 06:53:55 -05:00
|
|
|
case 'E':
|
|
|
|
eofstr = EARGF(usage());
|
|
|
|
break;
|
2014-01-03 06:52:47 -05:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
argmaxsz = sysconf(_SC_ARG_MAX);
|
|
|
|
if (argmaxsz < 0)
|
|
|
|
eprintf("sysconf:");
|
|
|
|
/* Leave some room for environment variables */
|
|
|
|
argmaxsz -= 4 * 1024;
|
|
|
|
|
|
|
|
cmd = malloc(NARGS * sizeof(*cmd));
|
|
|
|
if (!cmd)
|
|
|
|
eprintf("malloc:");
|
|
|
|
|
|
|
|
argb = malloc(argbsz);
|
|
|
|
if (!argb)
|
|
|
|
eprintf("malloc:");
|
|
|
|
|
|
|
|
do {
|
|
|
|
argsz = 0; i = 0;
|
|
|
|
if (argc > 0) {
|
|
|
|
for (; i < argc; i++) {
|
|
|
|
cmd[i] = strdup(argv[i]);
|
|
|
|
argsz += strlen(cmd[i]) + 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmd[i] = strdup("/bin/echo");
|
|
|
|
argsz += strlen(cmd[i]) + 1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
while ((arg = poparg())) {
|
|
|
|
if (argsz + strlen(arg) + 1 > argmaxsz ||
|
|
|
|
i >= NARGS - 1) {
|
2014-01-08 15:20:04 -05:00
|
|
|
if (strlen(arg) + 1 > argmaxsz)
|
|
|
|
enprintf(EXIT_FAILURE, "insufficient argument space\n");
|
2014-01-03 06:52:47 -05:00
|
|
|
pusharg(arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cmd[i] = strdup(arg);
|
|
|
|
argsz += strlen(cmd[i]) + 1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
cmd[i] = NULL;
|
2014-01-04 08:51:13 -05:00
|
|
|
if (i == 1 && rflag == 1); else runcmd();
|
2014-01-03 06:52:47 -05:00
|
|
|
for (; i >= 0; i--)
|
|
|
|
free(cmd[i]);
|
|
|
|
} while (arg);
|
|
|
|
|
|
|
|
free(argb);
|
|
|
|
free(cmd);
|
2014-01-06 13:51:06 -05:00
|
|
|
|
|
|
|
return nerrors > 0 ? 123 : 0;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
inputc(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
ch = getc(stdin);
|
|
|
|
if (ch == EOF && ferror(stdin))
|
|
|
|
eprintf("stdin: read error:");
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
deinputc(int ch)
|
|
|
|
{
|
|
|
|
ungetc(ch, stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-07 09:56:05 -05:00
|
|
|
fillargbuf(int ch)
|
2014-01-03 06:52:47 -05:00
|
|
|
{
|
|
|
|
if (argbpos >= argbsz) {
|
|
|
|
argbsz *= 2;
|
|
|
|
argb = realloc(argb, argbsz);
|
|
|
|
if (!argb)
|
|
|
|
eprintf("realloc:");
|
|
|
|
}
|
|
|
|
argb[argbpos] = ch;
|
|
|
|
}
|
|
|
|
|
2014-01-06 13:12:06 -05:00
|
|
|
static int
|
2014-01-03 06:52:47 -05:00
|
|
|
eatspace(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = inputc()) != EOF) {
|
|
|
|
switch (ch) {
|
|
|
|
case ' ': case '\t': case '\n':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
deinputc(ch);
|
2014-01-06 13:12:06 -05:00
|
|
|
return ch;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-06 13:12:06 -05:00
|
|
|
return -1;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parsequote(int q)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = inputc()) != EOF) {
|
|
|
|
if (ch == q) {
|
2014-01-07 09:56:05 -05:00
|
|
|
fillargbuf('\0');
|
2014-01-03 06:52:47 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ch != '\n') {
|
2014-01-07 09:56:05 -05:00
|
|
|
fillargbuf(ch);
|
2014-01-03 06:52:47 -05:00
|
|
|
argbpos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-06 13:20:26 -05:00
|
|
|
static int
|
2014-01-03 06:52:47 -05:00
|
|
|
parseescape(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
if ((ch = inputc()) != EOF) {
|
2014-01-07 09:56:05 -05:00
|
|
|
fillargbuf(ch);
|
2014-01-03 06:52:47 -05:00
|
|
|
argbpos++;
|
2014-01-06 13:20:26 -05:00
|
|
|
return ch;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
2014-01-06 13:20:26 -05:00
|
|
|
return -1;
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
poparg(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
argbpos = 0;
|
2014-01-06 13:12:06 -05:00
|
|
|
if (eatspace() == -1)
|
|
|
|
return NULL;
|
2014-01-03 06:52:47 -05:00
|
|
|
while ((ch = inputc()) != EOF) {
|
|
|
|
switch (ch) {
|
|
|
|
case ' ': case '\t': case '\n':
|
|
|
|
deinputc(ch);
|
2014-01-07 06:53:55 -05:00
|
|
|
goto out;
|
2014-01-03 06:52:47 -05:00
|
|
|
case '\'':
|
|
|
|
if (parsequote('\'') == -1)
|
|
|
|
enprintf(EXIT_FAILURE,
|
|
|
|
"unterminated single quote\n");
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
if (parsequote('\"') == -1)
|
|
|
|
enprintf(EXIT_FAILURE,
|
|
|
|
"unterminated double quote\n");
|
|
|
|
break;
|
|
|
|
case '\\':
|
2014-01-06 13:20:26 -05:00
|
|
|
if (parseescape() == -1)
|
|
|
|
enprintf(EXIT_FAILURE, "backslash at EOF\n");
|
2014-01-03 06:52:47 -05:00
|
|
|
break;
|
|
|
|
default:
|
2014-01-07 09:56:05 -05:00
|
|
|
fillargbuf(ch);
|
2014-01-03 06:52:47 -05:00
|
|
|
argbpos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-07 06:53:55 -05:00
|
|
|
out:
|
2014-01-03 06:52:47 -05:00
|
|
|
if (argbpos > 0) {
|
2014-01-07 09:56:05 -05:00
|
|
|
fillargbuf('\0');
|
2014-01-07 06:53:55 -05:00
|
|
|
if (eofstr && strcmp(argb, eofstr) == 0)
|
|
|
|
return NULL;
|
2014-01-03 06:52:47 -05:00
|
|
|
return argb;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pusharg(char *arg)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (p = &arg[strlen(arg) - 1]; p >= arg; p--)
|
|
|
|
deinputc(*p);
|
|
|
|
}
|
|
|
|
|
2014-01-04 08:51:13 -05:00
|
|
|
static void
|
2014-01-07 09:50:59 -05:00
|
|
|
waitchld(void)
|
2014-01-03 06:52:47 -05:00
|
|
|
{
|
2014-01-07 09:50:59 -05:00
|
|
|
int status;
|
2014-01-03 06:52:47 -05:00
|
|
|
|
|
|
|
wait(&status);
|
2014-01-04 09:01:22 -05:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
if (WEXITSTATUS(status) == 255)
|
|
|
|
exit(124);
|
|
|
|
if (WEXITSTATUS(status) == 127 ||
|
|
|
|
WEXITSTATUS(status) == 126)
|
|
|
|
exit(WEXITSTATUS(status));
|
2014-01-06 13:51:06 -05:00
|
|
|
if (status != 0)
|
|
|
|
nerrors++;
|
2014-01-04 09:01:22 -05:00
|
|
|
}
|
2014-01-06 13:03:34 -05:00
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
exit(125);
|
2014-01-03 06:52:47 -05:00
|
|
|
}
|
2014-01-07 09:50:59 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
runcmd(void)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int saved_errno;
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
eprintf("fork:");
|
|
|
|
if (pid == 0) {
|
|
|
|
execvp(*cmd, cmd);
|
|
|
|
saved_errno = errno;
|
|
|
|
weprintf("execvp %s:", *cmd);
|
|
|
|
_exit(saved_errno == ENOENT ? 127 : 126);
|
|
|
|
}
|
|
|
|
waitchld();
|
|
|
|
}
|