Restructurisation: moved debug code into general routine, and into aux file
authorJoey Schulze <joey@infodrom.org>
Tue, 27 Nov 2007 18:01:27 +0000 (18:01 +0000)
committerJoey Schulze <joey@infodrom.org>
Tue, 27 Nov 2007 18:01:27 +0000 (18:01 +0000)
aux.c
aux.h [new file with mode: 0644]
cgi.c
cookies.c

diff --git a/aux.c b/aux.c
index e54895c..5a8c72f 100644 (file)
--- a/aux.c
+++ b/aux.c
 
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <syslog.h>
+
+int cgiDebugLevel = 0;
+int cgiDebugType = 1;
 
 char *cgiEscape (char *string)
 {
 
 char *cgiEscape (char *string)
 {
@@ -78,6 +84,32 @@ char *cgiEscape (char *string)
     return buf;
 }
 
     return buf;
 }
 
+void cgiDebugOutput (int level, char *format, ...)
+{
+  va_list args;
+
+  if (level <= cgiDebugLevel) {
+
+      va_start (args, format);
+
+      switch (cgiDebugType) {
+      case 0:
+         printf ("<pre>\n");
+         vprintf (format, args);
+         printf ("\n</pre>\n");
+         break;
+      case 1:
+         vfprintf (stderr, format, args);
+         break;
+      case 2:
+         vsyslog (LOG_DEBUG, format, args);
+         break;
+      }
+
+      va_end (args);
+  }
+}
+
 /*
  * Local variables:
  *  c-indent-level: 4
 /*
  * Local variables:
  *  c-indent-level: 4
diff --git a/aux.h b/aux.h
new file mode 100644 (file)
index 0000000..1b54759
--- /dev/null
+++ b/aux.h
@@ -0,0 +1,25 @@
+/*
+    aux.h - Auxilliary code for CGI library
+    Copyright (C) 2007 by Martin Schulze <joey@infodrom.org>
+     
+    This program 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 this program; if not, write to the Free Software 
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+*/
+
+extern int cgiDebugLevel;
+extern int cgiDebugType;
+
+char *cgiEscape (char *string);
+
+void cgiDebugOutput (int level, char *format, ...);
diff --git a/cgi.c b/cgi.c
index 2dbfe7c..8d96656 100644 (file)
--- a/cgi.c
+++ b/cgi.c
@@ -23,9 +23,8 @@
 #include <string.h>
 #include <ctype.h>
 #include <cgi.h>
 #include <string.h>
 #include <ctype.h>
 #include <cgi.h>
+#include "aux.h"
 
 
-int cgiDebugLevel = 0;
-int cgiDebugStderr = 1;
 char *cgiHeaderString = NULL;
 char *cgiType = NULL;
 
 char *cgiHeaderString = NULL;
 char *cgiType = NULL;
 
@@ -100,10 +99,12 @@ void cgiDebug (int level, int where)
        cgiDebugLevel = level;
     else
        cgiDebugLevel = 0;
        cgiDebugLevel = level;
     else
        cgiDebugLevel = 0;
-    if (where)
-       cgiDebugStderr = 0;
-    else
-       cgiDebugStderr = 1;
+    if (where > 0) {
+       if (where < 3)
+           cgiDebugType = where;
+       else
+           cgiDebugType = 0;
+    }
 }
 
 char *cgiDecodeString (char *text)
 }
 
 char *cgiDecodeString (char *text)
@@ -200,12 +201,7 @@ s_var **cgiReadVariables ()
      *  and look like  foo=bar&foobar=barfoo&foofoo=
      */
 
      *  and look like  foo=bar&foobar=barfoo&foofoo=
      */
 
-    if (cgiDebugLevel > 0) {
-       if (cgiDebugStderr)
-           fprintf (stderr, "Received cgi input: %s\n", line);
-       else
-           printf ("<b>Received cgi input</b><br>\n<pre>\n--\n%s\n--\n</pre>\n\n", line);
-    }
+    cgiDebugOutput (1, "Received cgi input: %s\n", line);
 
     for (cp=line; *cp; cp++)
        if (*cp == '+')
 
     for (cp=line; *cp; cp++)
        if (*cp == '+')
@@ -216,12 +212,7 @@ s_var **cgiReadVariables ()
            if (*cp == '&' || *cp == ';' ) numargs++;
     } else
        numargs = 0;
            if (*cp == '&' || *cp == ';' ) numargs++;
     } else
        numargs = 0;
-    if (cgiDebugLevel > 0) {
-       if (cgiDebugStderr)
-           fprintf (stderr, "%d cgi variables found.\n", numargs);
-       else
-           printf ("%d cgi variables found.<br>\n", numargs);
-    }
+    cgiDebugOutput (1, "%d cgi variables found.\n", numargs);
 
     len = (numargs+1) * sizeof(s_var *);
     if ((result = (s_var **)malloc (len)) == NULL)
 
     len = (numargs+1) * sizeof(s_var *);
     if ((result = (s_var **)malloc (len)) == NULL)
@@ -266,12 +257,7 @@ s_var **cgiReadVariables ()
                memset (result[i]->value, 0, ip-esp+1);
                strncpy(result[i]->value, cp, ip-esp);
                result[i]->value = cgiDecodeString(result[i]->value);
                memset (result[i]->value, 0, ip-esp+1);
                strncpy(result[i]->value, cp, ip-esp);
                result[i]->value = cgiDecodeString(result[i]->value);
-               if (cgiDebugLevel) {
-                   if (cgiDebugStderr)
-                       fprintf (stderr, "%s: %s\n", result[i]->name, result[i]->value);
-                   else
-                       printf ("<h3>Variable %s</h3>\n<pre>\n%s\n</pre>\n\n", result[i]->name, result[i]->value);
-               }
+               cgiDebugOutput (1, "%s: %s\n", result[i]->name, result[i]->value);
                i++;
            } else {    /* There is already such a name, suppose a mutiple field */
                cp = ++esp;
                i++;
            } else {    /* There is already such a name, suppose a mutiple field */
                cp = ++esp;
@@ -323,23 +309,13 @@ char *cgiGetValue (s_cgi *parms, const char *name)
        return NULL;
     for (i=0;parms->vars[i]; i++)
        if (!strcmp(name,parms->vars[i]->name)) {
        return NULL;
     for (i=0;parms->vars[i]; i++)
        if (!strcmp(name,parms->vars[i]->name)) {
-           if (cgiDebugLevel > 0) {
-               if (cgiDebugStderr)
-                   fprintf (stderr, "%s found as %s\n", name, parms->vars[i]->value);
-               else
-                   printf ("%s found as %s<br>\n", name, parms->vars[i]->value);
-           }
+           cgiDebugOutput (1, "%s found as %s\n", name, parms->vars[i]->value);
            if (strlen(parms->vars[i]->value) > 0)
                return parms->vars[i]->value;
            else
                return NULL;
        }
            if (strlen(parms->vars[i]->value) > 0)
                return parms->vars[i]->value;
            else
                return NULL;
        }
-    if (cgiDebugLevel) {
-       if (cgiDebugStderr)
-           fprintf (stderr, "%s not found\n", name);
-       else
-           printf ("%s not found<br>\n", name);
-    }
+    cgiDebugOutput (1, "%s not found\n", name);
     return NULL;
 }
 
     return NULL;
 }
 
index e588cda..62d2559 100644 (file)
--- a/cookies.c
+++ b/cookies.c
@@ -1,6 +1,6 @@
 /*
     cookies.c - Cookie support for CGI library
 /*
     cookies.c - Cookie support for CGI library
-    Copyright (C) 1999 by Martin Schulze <joey@infodrom.org>
+    Copyright (C) 1999,2007 by Martin Schulze <joey@infodrom.org>
      
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
      
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -21,8 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <cgi.h>
 #include <stdlib.h>
 #include <string.h>
 #include <cgi.h>
-
-extern int cgiDebugLevel, cgiDebugStderr;
+#include "aux.h"
 
 s_cookie **cgiReadCookies()
 {
 
 s_cookie **cgiReadCookies()
 {
@@ -129,20 +128,10 @@ s_cookie *cgiGetCookie (s_cgi *parms, const char *name)
        return NULL;
     for (i=0;parms->cookies[i]; i++)
        if (parms->cookies[i]->name && parms->cookies[i]->value && !strcmp(name,parms->cookies[i]->name)) {
        return NULL;
     for (i=0;parms->cookies[i]; i++)
        if (parms->cookies[i]->name && parms->cookies[i]->value && !strcmp(name,parms->cookies[i]->name)) {
-           if (cgiDebugLevel > 0) {
-               if (cgiDebugStderr)
-                   fprintf (stderr, "%s found as %s\n", name, parms->cookies[i]->value);
-               else
-                   printf ("%s found as %s<br>\n", name, parms->cookies[i]->value);
-           }
+           cgiDebugOutput (1, "%s found as %s\n", name, parms->cookies[i]->value);
            return parms->cookies[i];
        }
            return parms->cookies[i];
        }
-    if (cgiDebugLevel) {
-       if (cgiDebugStderr)
-           fprintf (stderr, "%s not found\n", name);
-       else
-           printf ("%s not found<br>\n", name);
-    }
+    cgiDebugOutput (1, "%s not found\n", name);
     return NULL;
 }
 
     return NULL;
 }