]> diplodocus.org Git - nmh/commitdiff
The start of the use of argsplit() to process command strings.
authorKen Hornstein <kenh@pobox.com>
Tue, 29 Jan 2013 20:30:36 +0000 (15:30 -0500)
committerKen Hornstein <kenh@pobox.com>
Tue, 29 Jan 2013 20:30:36 +0000 (15:30 -0500)
h/prototypes.h
sbr/arglist.c
uip/mhlsbr.c
uip/mhshowsbr.c
uip/replsbr.c

index 6db8e16900cb396e1f711ca8425c2d22ee6badda..d4fc6eb0ac16e775cdafef095f4dab57d5984198 100644 (file)
@@ -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 *);
index 1f85b7b29558acb592d1a5bbd585a041867d4228..8749a4b9c4f8595b5924aec22e2aae05957d9f96 100644 (file)
  *
  * - 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);
+    }
+}
index 95f8c54d03d3c4bf83e012f1f2dc154e64a9fb38..f87c47d35c0f19ab1ea04647ad258e7732fd8203 100644 (file)
@@ -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);
index 3b91eebf78ccb74ff46e044cea46e94c96189d51..866a41bfbcbca9f684c754c62877101c0757f29b 100644 (file)
@@ -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);
 }
 
 
index d81eb745505ec32e10ea6fcdef9149136a19aa39..b696ae71eaaf1ac26fea246e827eb8ce6597f43a 100644 (file)
@@ -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));