From: Ken Hornstein Date: Tue, 29 Jan 2013 20:30:36 +0000 (-0500) Subject: The start of the use of argsplit() to process command strings. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/ccafa194418b6f43f6aebda81a9eb854318ff72f?ds=inline;hp=-c The start of the use of argsplit() to process command strings. --- ccafa194418b6f43f6aebda81a9eb854318ff72f diff --git a/h/prototypes.h b/h/prototypes.h index 6db8e169..d4fc6eb0 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -27,6 +27,8 @@ void adios (char *, char *, ...) NORETURN; void admonish (char *, char *, ...); void advertise (char *, char *, char *, va_list); void advise (char *, char *, ...); +char **argsplit (char *, char **, int *); +void arglist_free (char *, char **); void ambigsw (char *, struct swit *); int atooi(char *); char **brkstring (char *, char *, char *); diff --git a/sbr/arglist.c b/sbr/arglist.c index 1f85b7b2..8749a4b9 100644 --- a/sbr/arglist.c +++ b/sbr/arglist.c @@ -25,10 +25,10 @@ * * - If there are no spaces or shell metacharacters in "command", then * take it as-is. - * - If there are spaces in command, space-split command string and - * append an argument list to it. + * - If there are spaces in command, space-split command string. * - If we have shell metacharacters, run the command using - * /bin/sh -c 'command "${@}"'. + * /bin/sh -c 'command "$@"'. In this case, any additional arguments + * appended to the arglist will be expanded by "$@". * * In all cases additional arguments can be added to the argv[] array. */ @@ -123,3 +123,22 @@ argsplit(char *command, char **file, int *argp) return argvarray; } + +/* + * Free our argument array + */ + +void +arglist_free(char *command, char **argvarray) +{ + int i; + + if (command != NULL) + free(command); + + if (argvarray != NULL) { + for (i = 0; argvarray[i] != NULL; i++) + free(argvarray[i]); + free(argvarray); + } +} diff --git a/uip/mhlsbr.c b/uip/mhlsbr.c index 95f8c54d..f87c47d3 100644 --- a/uip/mhlsbr.c +++ b/uip/mhlsbr.c @@ -1648,6 +1648,8 @@ static void m_popen (char *name) { int pd[2]; + char *file; + char **arglist; if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK) adios ("standard output", "unable to dup()"); @@ -1668,7 +1670,8 @@ m_popen (char *name) dup2 (pd[0], fileno (stdin)); close (pd[0]); } - execlp (name, r1bindex (name, '/'), NULL); + arglist = argsplit(name, &file, NULL); + execvp (file, arglist); fprintf (stderr, "unable to exec "); perror (name); _exit (-1); diff --git a/uip/mhshowsbr.c b/uip/mhshowsbr.c index 3b91eebf..866a41bf 100644 --- a/uip/mhshowsbr.c +++ b/uip/mhshowsbr.c @@ -168,24 +168,24 @@ DisplayMsgHeader (CT ct, char *form) { pid_t child_id; int i, vecp; - char *vec[8]; + char **vec; + char *file; - vecp = 0; - vec[vecp++] = r1bindex (mhlproc, '/'); - vec[vecp++] = "-form"; - vec[vecp++] = form; - vec[vecp++] = "-nobody"; - vec[vecp++] = ct->c_file; + vec = argsplit(mhlproc, &file, &vecp); + vec[vecp++] = getcpy("-form"); + vec[vecp++] = getcpy(form); + vec[vecp++] = getcpy("-nobody"); + vec[vecp++] = getcpy(ct->c_file); /* * If we've specified -(no)moreproc, * then just pass that along. */ if (nomore) { - vec[vecp++] = "-nomoreproc"; + vec[vecp++] = getcpy("-nomoreproc"); } else if (progsw) { - vec[vecp++] = "-moreproc"; - vec[vecp++] = progsw; + vec[vecp++] = getcpy("-moreproc"); + vec[vecp++] = getcpy(progsw); } vec[vecp] = NULL; @@ -200,7 +200,7 @@ DisplayMsgHeader (CT ct, char *form) /* NOTREACHED */ case OK: - execvp (mhlproc, vec); + execvp (file, vec); fprintf (stderr, "unable to exec "); perror (mhlproc); _exit (-1); @@ -210,6 +210,8 @@ DisplayMsgHeader (CT ct, char *form) xpid = -child_id; break; } + + arglist_free(file, vec); } diff --git a/uip/replsbr.c b/uip/replsbr.c index d81eb745..b696ae71 100644 --- a/uip/replsbr.c +++ b/uip/replsbr.c @@ -412,7 +412,8 @@ replfilter (FILE *in, FILE *out, char *filter, int fmtproc) int pid; char *mhl; char *errstr; - char *arglist[7]; + char **arglist; + int argnum; if (filter == NULL) return; @@ -420,8 +421,6 @@ replfilter (FILE *in, FILE *out, char *filter, int fmtproc) if (access (filter, R_OK) == NOTOK) adios (filter, "unable to read"); - mhl = r1bindex (mhlproc, '/'); - rewind (in); lseek (fileno(in), (off_t) 0, SEEK_SET); @@ -434,26 +433,29 @@ replfilter (FILE *in, FILE *out, char *filter, int fmtproc) dup2 (fileno (out), fileno (stdout)); closefds (3); - arglist[0] = mhl; - arglist[1] = "-form"; - arglist[2] = filter; - arglist[3] = "-noclear"; + /* + * We're not allocating the memory for the extra arguments, + * because we never call arglist_free(). But if we ever change + * that be sure to use getcpy() for the extra arguments. + */ + arglist = argsplit(mhlproc, &mhl, &argnum); + arglist[argnum++] = "-form"; + arglist[argnum++] = filter; + arglist[argnum++] = "-noclear"; switch (fmtproc) { case 1: - arglist[4] = "-fmtproc"; - arglist[5] = formatproc; - arglist[6] = NULL; + arglist[argnum++] = "-fmtproc"; + arglist[argnum++] = formatproc; break; case 0: - arglist[4] = "-nofmtproc"; - arglist[5] = NULL; + arglist[argnum++] = "-nofmtproc"; break; - default: - arglist[4] = NULL; } - execvp (mhlproc, arglist); + arglist[argnum++] = NULL; + + execvp (mhl, arglist); errstr = strerror(errno); write(2, "unable to exec ", 15); write(2, mhlproc, strlen(mhlproc));