From d19e4b599ed7e44fb34f79149d719169f3c1e70a Mon Sep 17 00:00:00 2001 From: Joey Schulze Date: Tue, 27 Nov 2007 18:01:27 +0000 Subject: [PATCH] Restructurisation: moved debug code into general routine, and into aux file --- aux.c | 32 ++++++++++++++++++++++++++++++++ aux.h | 25 +++++++++++++++++++++++++ cgi.c | 48 ++++++++++++------------------------------------ cookies.c | 19 ++++--------------- 4 files changed, 73 insertions(+), 51 deletions(-) create mode 100644 aux.h diff --git a/aux.c b/aux.c index e54895c..5a8c72f 100644 --- a/aux.c +++ b/aux.c @@ -19,6 +19,12 @@ #include #include +#include +#include +#include + +int cgiDebugLevel = 0; +int cgiDebugType = 1; char *cgiEscape (char *string) { @@ -78,6 +84,32 @@ char *cgiEscape (char *string) return buf; } +void cgiDebugOutput (int level, char *format, ...) +{ + va_list args; + + if (level <= cgiDebugLevel) { + + va_start (args, format); + + switch (cgiDebugType) { + case 0: + printf ("
\n");
+	  vprintf (format, args);
+	  printf ("\n
\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 diff --git a/aux.h b/aux.h new file mode 100644 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 + + 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 --- a/cgi.c +++ b/cgi.c @@ -23,9 +23,8 @@ #include #include #include +#include "aux.h" -int cgiDebugLevel = 0; -int cgiDebugStderr = 1; char *cgiHeaderString = NULL; char *cgiType = NULL; @@ -100,10 +99,12 @@ void cgiDebug (int level, int where) 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) @@ -200,12 +201,7 @@ s_var **cgiReadVariables () * and look like foo=bar&foobar=barfoo&foofoo= */ - if (cgiDebugLevel > 0) { - if (cgiDebugStderr) - fprintf (stderr, "Received cgi input: %s\n", line); - else - printf ("Received cgi input
\n
\n--\n%s\n--\n
\n\n", line); - } + cgiDebugOutput (1, "Received cgi input: %s\n", line); for (cp=line; *cp; cp++) if (*cp == '+') @@ -216,12 +212,7 @@ s_var **cgiReadVariables () 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.
\n", numargs); - } + cgiDebugOutput (1, "%d cgi variables found.\n", numargs); 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); - if (cgiDebugLevel) { - if (cgiDebugStderr) - fprintf (stderr, "%s: %s\n", result[i]->name, result[i]->value); - else - printf ("

Variable %s

\n
\n%s\n
\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; @@ -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)) { - if (cgiDebugLevel > 0) { - if (cgiDebugStderr) - fprintf (stderr, "%s found as %s\n", name, parms->vars[i]->value); - else - printf ("%s found as %s
\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 (cgiDebugLevel) { - if (cgiDebugStderr) - fprintf (stderr, "%s not found\n", name); - else - printf ("%s not found
\n", name); - } + cgiDebugOutput (1, "%s not found\n", name); return NULL; } diff --git a/cookies.c b/cookies.c index e588cda..62d2559 100644 --- a/cookies.c +++ b/cookies.c @@ -1,6 +1,6 @@ /* cookies.c - Cookie support for CGI library - Copyright (C) 1999 by Martin Schulze + Copyright (C) 1999,2007 by Martin Schulze 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 #include #include - -extern int cgiDebugLevel, cgiDebugStderr; +#include "aux.h" 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)) { - if (cgiDebugLevel > 0) { - if (cgiDebugStderr) - fprintf (stderr, "%s found as %s\n", name, parms->cookies[i]->value); - else - printf ("%s found as %s
\n", name, parms->cookies[i]->value); - } + cgiDebugOutput (1, "%s found as %s\n", name, parms->cookies[i]->value); return parms->cookies[i]; } - if (cgiDebugLevel) { - if (cgiDebugStderr) - fprintf (stderr, "%s not found\n", name); - else - printf ("%s not found
\n", name); - } + cgiDebugOutput (1, "%s not found\n", name); return NULL; } -- 2.20.1