chiark / gitweb /
www-cgi/: Add some trivial tracing.
[userv-utils.git] / www-cgi / ucgitarget.c
index 795d804e39c0c6d03a2d4ad60228650e8f934afc..96372353c19cbf441151a942bbe1d5be6d4e4b3b 100644 (file)
@@ -2,6 +2,25 @@
  * Usage: as CGI script, but called by userv
  * environment variables are USERV_U_E_...
  */
+/*
+ * Copyright (C) 1998-1999,2003 Ian Jackson
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with userv-utils; if not, write to the Free Software
+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * $Id$
+ */
 
 #include <stdio.h>
 #include <string.h>
 
 #include "ucgi.h"
 
-static void *xrealloc(void *ptr, size_t sz) {
-  void *r;
-
-  r= realloc(ptr,sz);
-  if (!r) syserror("realloc failed");
-  return r;
-}
-
 int main(int argc, const char **argv) {
   char *uservarn, *scriptpath, *newvar;
   const char *nextslash, *lastslash, *pathi, *ev, *ev2, *en, *scriptdir, *av;
@@ -33,6 +44,7 @@ int main(int argc, const char **argv) {
   ev= getenv("USERV_U_DEBUG");
   if (ev && *ev) debugmode= 1;
   
+  D( if (debugmode) printf(";;; UCGITARGET\n"); )
   if (argc > MAX_ARGS) error("too many arguments");
 
   if (!*++argv) error("no script directory argument");
@@ -59,6 +71,11 @@ int main(int argc, const char **argv) {
   pathi= getenv("PATH_INFO");
   if (!pathi) error("PATH_INFO not found");
   lastslash= pathi;
+  D( if (debugmode) {
+       printf(";; find script name...\n"
+             ";;   PATH_INFO = `%s'\n",
+             pathi);
+  } )
   for (;;) {
     if (*lastslash != '/') error("PATH_INFO expected slash not found");
     if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) error("bad char begin");
@@ -73,11 +90,13 @@ int main(int argc, const char **argv) {
     memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi);
     scriptpath[scriptpathlen]= 0;
     if (scriptpath[scriptpathlen-1]=='~') error("bad char end");
+    D( if (debugmode) printf(";;   try `%s'\n", scriptpath); )
     r= stat(scriptpath,&stab); if (r) syserror("stat script");
     if (S_ISREG(stab.st_mode)) break;
-    if (!S_ISDIR(stab.st_mode)) syserror("script not directory or file");
+    if (!S_ISDIR(stab.st_mode)) error("script not directory or file");
     lastslash= nextslash;
   }
+  D( if (debugmode) printf(";;   found script: tail = `%s'\n", nextslash); )
   if (*nextslash) xsetenv("PATH_INFO",nextslash,1);
   else unsetenv("PATH_INFO");
 
@@ -102,6 +121,19 @@ int main(int argc, const char **argv) {
   while ((av= (*++argv))) arguments[nargs++]= av;
   arguments[nargs++]= 0;
 
+  D( if (debugmode) {
+       int i;
+
+       printf(";; final environment...\n");
+       for (i = 0; environ[i]; i++)
+        printf(";;   %s\n", environ[i]);
+
+       printf(";; final command line...\n");
+       for (i = 0; arguments[i]; i++)
+        printf(";;   %s\n", arguments[i]);
+       fflush(stdout);
+  } )
+
   execvp(scriptpath,(char*const*)arguments);
   syserror("exec script");
   return -1;