]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* make error logging NULL safe.
[citadel.git] / webcit / subst.c
index 6a9c28baa17945cded85972293fe260ea2285a7c..249a88699fc391c4ae76dd5e7aeac8bce6d76537 100644 (file)
@@ -59,6 +59,8 @@ typedef struct _HashHandler {
 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere);
 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP);
 
+
+
 typedef struct _SortStruct {
        StrBuf *Name;
        StrBuf *PrefPrepend;
@@ -86,7 +88,8 @@ const char *CtxNames[]  = {
        "Context LONGVECTOR",
        "Context ROOMS",
        "Context FLOORS",
-       "Context ITERATE"
+       "Context ITERATE",
+       "Context UNKNOWN"
 };
 
 
@@ -99,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;
@@ -116,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",
@@ -143,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));
+       }
 }
 
 
@@ -192,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;
        }
 
@@ -202,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;
        }
 /*                     
@@ -1049,7 +1080,7 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                                        if (NewToken->nParameters > MAXPARAM) {
                                                LogTemplateError(
                                                        NULL, "Parseerror", ERR_NAME, &TP,
-                                                       "only [%ld] Params allowed in Tokens",
+                                                       "only [%d] Params allowed in Tokens",
                                                        MAXPARAM);
 
                                                free(Param);
@@ -1089,7 +1120,7 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                            (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
                                LogTemplateError(
                                        NULL, "Token", ERR_NAME, &TP,
-                                       "doesn't work with %ld params", 
+                                       "doesn't work with %d params", 
                                        NewToken->nParameters);
 
                        }
@@ -1103,7 +1134,7 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                if (NewToken->nParameters !=1) {
                        LogTemplateError(                               
                                NULL, "Gettext", ERR_NAME, &TP,
-                               "requires exactly 1 parameter, you gave %ld params", 
+                               "requires exactly 1 parameter, you gave %d params", 
                                NewToken->nParameters);
                        NewToken->Flags = 0;
                        break;
@@ -1113,7 +1144,7 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                if (NewToken->nParameters != 1) {
                        LogTemplateError(
                                NULL, "Subtemplates", ERR_NAME, &TP,
-                               "require exactly 1 parameter, you gave %ld params", 
+                               "require exactly 1 parameter, you gave %d params", 
                                NewToken->nParameters);
                        break;
                }
@@ -1124,7 +1155,7 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                if (NewToken->nParameters <2) {
                        LogTemplateError(
                                NULL, "Conditional", ERR_NAME, &TP,
-                               "require at least 2 parameters, you gave %ld params", 
+                               "require at least 2 parameters, you gave %d params", 
                                NewToken->nParameters);
                        NewToken->Flags = 0;
                        break;
@@ -1631,7 +1662,7 @@ void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
        if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
                LogTemplateError(                               
                        Target, "Iterator", ERR_PARM1, TP,
-                       "doesn't work with %ld params", 
+                       "doesn't work with %d params", 
                        TP->Tokens->nParameters);
                return;
        }
@@ -1640,7 +1671,7 @@ void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
            (It->XPectContextType != TP->Filter.ContextType)) {
                LogTemplateError(
                        Target, "Iterator", ERR_PARM1, TP,
-                       "requires context of type %ld, have %ld", 
+                       "requires context of type %d, have %d", 
                        It->XPectContextType, 
                        TP->Filter.ContextType);
                return ;