* make error logging NULL safe.
authorWilfried Göesgens <willi@citadel.org>
Sat, 17 Jan 2009 10:22:09 +0000 (10:22 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sat, 17 Jan 2009 10:22:09 +0000 (10:22 +0000)
webcit/subst.c
webcit/subst.h

index c32448c7b9de679c2cc169ba5c5c79c3f2fdb49a..249a88699fc391c4ae76dd5e7aeac8bce6d76537 100644 (file)
@@ -88,7 +88,8 @@ const char *CtxNames[]  = {
        "Context LONGVECTOR",
        "Context ROOMS",
        "Context FLOORS",
-       "Context ITERATE"
+       "Context ITERATE",
+       "Context UNKNOWN"
 };
 
 
@@ -101,6 +102,14 @@ void DestroySortStruct(void *vSort)
        free (Sort);
 }
 
+const char *ContextName(int ContextType)
+{
+       if (ContextType < CTX_UNKNOWN)
+               return CtxNames[ContextType];
+       else
+               return CtxNames[CTX_UNKNOWN];
+}
+
 void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplputParams *TP, const char *Format, ...)
 {
        StrBuf *Error;
@@ -118,23 +127,35 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
        switch (ErrorPos) {
        default:
        case ERR_NAME: /* the main token name... */ 
-               Err = TP->Tokens->pName;
+               Err = (TP->Tokens!= NULL)? TP->Tokens->pName:"";
                break;
        case ERR_PARM1:
-               Err = TP->Tokens->Params[0]->Start;
+               Err = (TP->Tokens!= NULL)? TP->Tokens->Params[0]->Start:"";
                break;
        case ERR_PARM2:
-               Err = TP->Tokens->Params[1]->Start;
+               Err = (TP->Tokens!= NULL)? TP->Tokens->Params[1]->Start:"";
                break;
        }
-       lprintf(1, "%s [%s]  (in '%s' line %ld); %s; [%s]\n", 
-               Type, 
-               Err, 
-               ChrPtr(TP->Tokens->FileName),
-               TP->Tokens->Line, 
-               ChrPtr(Error), 
-               ChrPtr(TP->Tokens->FlatToken));
-       if (Target != NULL) {
+       if (TP->Tokens != NULL) 
+       {
+               lprintf(1, "%s [%s]  (in '%s' line %ld); %s; [%s]\n", 
+                       Type, 
+                       Err, 
+                       ChrPtr(TP->Tokens->FileName),
+                       TP->Tokens->Line, 
+                       ChrPtr(Error), 
+                       ChrPtr(TP->Tokens->FlatToken));
+       }
+       else 
+       {
+               lprintf(1, "%s: %s;\n", 
+                       Type, 
+                       ChrPtr(Error));
+       }
+       if (Target == NULL) 
+               return;
+       if (TP->Tokens != NULL) 
+       {
                StrBufAppendPrintf(                                                          
                        Target,                                                              
                        "<pre>\n%s [%s] (in '%s' line %ld); %s\n[%s]\n</pre>\n",
@@ -145,6 +166,14 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
                        ChrPtr(Error),
                        ChrPtr(TP->Tokens->FlatToken));
        }
+       else
+       {
+               StrBufAppendPrintf(                                                          
+                       Target,                                                              
+                       "<pre>\n%s: %s\n</pre>\n",
+                       Type, 
+                       ChrPtr(Error));
+       }
 }
 
 
@@ -194,8 +223,8 @@ int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, cons
                 LogTemplateError(
                         Target, ErrType, ERR_PARM1, TP,
                        "  WARNING: requires Context: [%s], have [%s]!", 
-                       CtxNames[Need->ContextType]
-                       CtxNames[TP->Filter.ContextType]);
+                       ContextName(Need->ContextType)
+                       ContextName(TP->Filter.ContextType));
                return 0;
        }
 
@@ -204,8 +233,8 @@ int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, cons
                 LogTemplateError(
                         Target, ErrType, ERR_PARM1, TP,
                        "  WARNING: requires Control Context: [%s], have [%s]!", 
-                       CtxNames[Need->ControlContextType]
-                       CtxNames[TP->Filter.ControlContextType]);
+                       ContextName(Need->ControlContextType)
+                       ContextName(TP->Filter.ControlContextType));
                return 0;
        }
 /*                     
index 466e6aee2d64b005dd883b7e70eeb6f2ab5bb70d..37ce34fe547f0d219650275242db1bd5bc2f0a7f 100644 (file)
@@ -51,6 +51,8 @@ enum {
 #define CTX_FLOORS 15
 #define CTX_ITERATE 16
 
+#define CTX_UNKNOWN 17
+
 
 /**
  * ContextFilter resembles our RTTI information. With this structure