chiark / gitweb /
www-cgi/ucgi.c: A bit more machinery for building the command line.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 30 Jan 2013 00:47:45 +0000 (00:47 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 2 Feb 2013 13:31:03 +0000 (13:31 +0000)
Move the state for building the command line into a structure, and
introduce a function for adding an argument.  We'll want this later.

www-cgi/ucgi.c

index 374fea5c0f1f03eef014f12f2ca649337ba23506..431d280c9cd0e1a4304e06c420b63e6f0e949e28 100644 (file)
 
 #include "ucgi.h"
 
 
 #include "ucgi.h"
 
+struct buildargs {
+  const char **v;
+  int n, max;
+};
+
+static void addarg(struct buildargs *args, const char *a) {
+  if (args->n > args->max) error("too many arguments");
+  args->v[args->n++]= a;
+}
+
+static void add_userv_var(const char *en, const char *ev,
+                         struct buildargs *args) {
+  size_t l;
+  char *a;
+
+  l= strlen(ev); if (l > MAX_ENVVAR_VALUE) error("environment variable too long");
+  a= xmalloc(strlen(en)+l+6);
+  sprintf(a,"-DE_%s=%s",en,ev);
+  addarg(args, a);
+}
+
 int main(int argc, const char **argv) {
 int main(int argc, const char **argv) {
-  char *defarg, *username;
+  char *username;
   const char *slash2, *pathi, *ev, *en, *av;
   const char *const *ep;
   const char *slash2, *pathi, *ev, *en, *av;
   const char *const *ep;
-  const char **arguments;
   size_t usernamelen, l;
   size_t usernamelen, l;
+  struct buildargs args;
   pid_t child, rchild;
   pid_t child, rchild;
-  int nargs, status;
+  int status;
 
   l= strlen(argv[0]);
   if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1;
 
   l= strlen(argv[0]);
   if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1;
@@ -68,25 +89,22 @@ int main(int argc, const char **argv) {
        printf(";;   user = `%s'; tail = `%s'\n", username, slash2); )
   if (!isalpha(username[0])) error("username 1st character is not alphabetic");
   xsetenv("PATH_INFO",slash2,1);
        printf(";;   user = `%s'; tail = `%s'\n", username, slash2); )
   if (!isalpha(username[0])) error("username 1st character is not alphabetic");
   xsetenv("PATH_INFO",slash2,1);
+
+  args.n= 0; args.max= argc + nenvok + 10;
+  args.v= xmalloc(args.max * sizeof(*args.v));
   
   
-  arguments= xmalloc(sizeof(const char*)*(nenvok+argc+10));
-  nargs= 0;
-  
-  arguments[nargs++]= "userv";
-  if (debugmode) arguments[nargs++]= "-DDEBUG=1";
-  
+  addarg(&args, "userv");
+  if (debugmode) addarg(&args, "-DDEBUG=1");
+
   for (ep= envok; (en= *ep); ep++) {
     ev= getenv(en); if (!ev) continue;
   for (ep= envok; (en= *ep); ep++) {
     ev= getenv(en); if (!ev) continue;
-    l= strlen(ev); if (l > MAX_ENVVAR_VALUE) error("environment variable too long");
-    defarg= xmalloc(strlen(en)+l+6);
-    sprintf(defarg,"-DE_%s=%s",en,ev);
-    arguments[nargs++]= defarg;
+    add_userv_var(en, ev, &args);
   }
 
   }
 
-  arguments[nargs++]= username;
-  arguments[nargs++]= "www-cgi";
-  while ((av= (*++argv))) arguments[nargs++]= av;
-  arguments[nargs++]= 0;
+  addarg(&args, username);
+  addarg(&args, "www-cgi");
+  while ((av= (*++argv))) addarg(&args, av);
+  addarg(&args, 0);
 
   if (debugmode) {
     D( fflush(stdout); )
 
   if (debugmode) {
     D( fflush(stdout); )
@@ -103,12 +121,12 @@ int main(int argc, const char **argv) {
        int i;
 
        printf(";; final command line...\n");
        int i;
 
        printf(";; final command line...\n");
-       for (i = 0; arguments[i]; i++)
-        printf(";;   %s\n", arguments[i]);
+       for (i = 0; args.v[i]; i++)
+        printf(";;   %s\n", args.v[i]);
        fflush(stdout);
   } )
 
        fflush(stdout);
   } )
 
-  execvp("userv",(char*const*)arguments);
+  execvp("userv",(char*const*)args.v);
   syserror("exec userv");
   return -1;
 }
   syserror("exec userv");
   return -1;
 }