]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* defines may now be ored or anded
[citadel.git] / webcit / subst.c
index 6f02936d1f8cdf124dfa1e5992b7ddd723a1454e..ee0e324e726bafe5cd84db4083d9f5ff5f72b6b1 100644 (file)
@@ -105,6 +105,7 @@ const char *CtxNames[]  = {
        "Context MIME_ATACH",
        "Context FILELIST",
        "Context STRBUF",
+       "Context STRBUFARR",
        "Context LONGVECTOR",
        "Context ROOMS",
        "Context FLOORS",
@@ -135,7 +136,6 @@ const char *ContextName(int ContextType)
 void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplputParams *TP, const char *Format, ...)
 {
        wcsession *WCC;
-       StrBuf *Header;
        StrBuf *Error;
        StrBuf *Info;
         va_list arg_ptr;
@@ -149,19 +149,13 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
        va_end(arg_ptr);
 
        switch (ErrorPos) {
-       default:
        case ERR_NAME: /* the main token name... */ 
                Err = (TP->Tokens!= NULL)? TP->Tokens->pName:"";
                break;
-       case ERR_PARM1:
-               Err = ((TP->Tokens!= NULL) && 
-                      (TP->Tokens->nParameters > 0))? 
-                       TP->Tokens->Params[0]->Start : "";
-               break;
-       case ERR_PARM2:
+       default:
                Err = ((TP->Tokens!= NULL) && 
-                      (TP->Tokens->nParameters > 1))? 
-                       TP->Tokens->Params[1]->Start : "";
+                      (TP->Tokens->nParameters > ErrorPos - 1))? 
+                       TP->Tokens->Params[ErrorPos - 1]->Start : "";
                break;
        }
        if (TP->Tokens != NULL) 
@@ -191,7 +185,7 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
                return; 
        }
 
-       Header = NewStrBuf();
+       if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
        if (TP->Tokens != NULL) 
        {
                /* deprecated: 
@@ -212,7 +206,7 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
                             ChrPtr(TP->Tokens->FlatToken));
 
 
-               SerializeJson(Header, WildFireException(SKEY(TP->Tokens->FileName),
+               SerializeJson(WCC->WFBuf, WildFireException(SKEY(TP->Tokens->FileName),
                                                        TP->Tokens->Line,
                                                        Info,
                                                        1), 1);
@@ -222,7 +216,7 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
                                                      Error,
                                                      eERROR), 1);
 */
-               WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
+               
        }
        else
        {
@@ -238,10 +232,8 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
                             Err, 
                             ChrPtr(Error), 
                             ChrPtr(TP->Tokens->FlatToken));
-               SerializeJson(Header, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
-               WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
+               SerializeJson(WCC->WFBuf, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
        }
-       FreeStrBuf(&Header);
        FreeStrBuf(&Info);
        FreeStrBuf(&Error);
 /*
@@ -256,7 +248,6 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
 void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
 {
        wcsession *WCC;
-       StrBuf *Header;
        StrBuf *Error;
        StrBuf *Info;
         va_list arg_ptr;
@@ -271,16 +262,13 @@ void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
        lprintf(1, ChrPtr(Error));
 
        WCC = WC;
-       Header = NewStrBuf();
+       if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
 
+       SerializeJson(WCC->WFBuf, WildFireException(Type, strlen(Type),
+                                                   0,
+                                                   Info,
+                                                   1), 1);
 
-       SerializeJson(Header, WildFireException(Type, strlen(Type),
-                                               0,
-                                               Info,
-                                               1), 1);
-       WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
-       
-       FreeStrBuf(&Header);
        FreeStrBuf(&Info);
        FreeStrBuf(&Error);
 /*
@@ -454,7 +442,7 @@ void clear_local_substs(void) {
        clear_substs (WC);
 }
 
-int NeedNewBuf(type)
+int NeedNewBuf(int type)
 {
        switch(type) {
        case WCS_STRING:
@@ -1150,12 +1138,13 @@ void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
        Template->Tokens[(Template->nTokensUsed)++] = NewToken;
 }
 
-TemplateParam *GetNextParameter(StrBuf *Buf, 
-                               const char **pCh, 
-                               const char *pe, 
-                               WCTemplateToken *Tokens, 
-                               WCTemplate *pTmpl, 
-                               WCTemplputParams *TP)
+int GetNextParameter(StrBuf *Buf, 
+                    const char **pCh, 
+                    const char *pe, 
+                    WCTemplateToken *Tokens, 
+                    WCTemplate *pTmpl, 
+                    WCTemplputParams *TP, 
+                    TemplateParam **pParm)
 {
        const char *pch = *pCh;
        const char *pchs, *pche;
@@ -1163,7 +1152,7 @@ TemplateParam *GetNextParameter(StrBuf *Buf,
        char quote = '\0';
        int ParamBrace = 0;
 
-       Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
+       *pParm = Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
        memset(Parm, 0, sizeof(TemplateParam));
        Parm->Type = TYPE_STR;
 
@@ -1242,7 +1231,8 @@ TemplateParam *GetNextParameter(StrBuf *Buf,
                                *pCh);
                        pch ++;
                        free(Parm);
-                       return NULL;
+                       *pParm = NULL;
+                       return 0;
                }
                else {
                        StrBufPeek(Buf, pch, -1, '\0');         
@@ -1285,7 +1275,8 @@ TemplateParam *GetNextParameter(StrBuf *Buf,
                                *pCh);
                                */
                        free(Parm);
-                       return NULL;
+                       *pParm = NULL;
+                       return 0;
                }
        }
        while ((*pch == ' ' )||
@@ -1312,9 +1303,84 @@ TemplateParam *GetNextParameter(StrBuf *Buf,
                
                        Parm->lvalue = *PVal;
                }
-               else 
+               else if (strchr(Parm->Start, '|') != NULL)
+               {
+                       const char *Pos;
+                       StrBuf *pToken;
+                       StrBuf *Match;
+
+                       Parm->MaskBy = eOR;
+                       pToken = NewStrBufPlain (Parm->Start, Parm->len);
+                       Match = NewStrBufPlain (NULL, Parm->len);
+                       Pos = ChrPtr(pToken);
+                       
+                       while ((Pos != NULL) && (Pos != StrBufNOTNULL))
+                       {
+                               StrBufExtract_NextToken(Match, pToken, &Pos, '|');
+                               StrBufTrim(Match);
+                               if (StrLength (Match) > 0)
+                               {
+                                       if (GetHash(Defines, SKEY(Match), &vPVal) &&
+                                           (vPVal != NULL))
+                                       {
+                                               long *PVal;
+                                               PVal = (long*) vPVal;
+                                               
+                                               Parm->lvalue |= *PVal;
+                                       }
+                                       else {
+                                               LogTemplateError(NULL, "Define", 
+                                                                Tokens->nParameters,
+                                                                TP,
+                                                                "%s isn't known!!",
+                                                                ChrPtr(Match));
+
+                                       }
+                               }
+                       }
+               }
+               else if (strchr(Parm->Start, '&') != NULL)
                {
-                       LogTemplateError(NULL, "Define", ERR_PARM1, TP,
+                       const char *Pos;
+                       StrBuf *pToken;
+                       StrBuf *Match;
+
+                       Parm->MaskBy = eAND;
+                       pToken = NewStrBufPlain (Parm->Start, Parm->len);
+                       Match = NewStrBufPlain (NULL, Parm->len);
+                       Pos = ChrPtr(pToken);
+                       
+                       while ((Pos != NULL) && (Pos != StrBufNOTNULL))
+                       {
+                               StrBufExtract_NextToken(Match, pToken, &Pos, '&');
+                               StrBufTrim(Match);
+                               if (StrLength (Match) > 0)
+                               {
+                                       if (GetHash(Defines, SKEY(Match), &vPVal) &&
+                                           (vPVal != NULL))
+                                       {
+                                               long *PVal;
+                                               PVal = (long*) vPVal;
+                                               
+                                               Parm->lvalue |= *PVal;
+                                       }
+                                       else {
+                                               LogTemplateError(NULL, "Define", 
+                                                                Tokens->nParameters,
+                                                                TP,
+                                                                "%s isn't known!!",
+                                                                ChrPtr(Match));
+
+                                       }
+                               }
+                       }
+               }
+               else {
+
+
+                       LogTemplateError(NULL, "Define", 
+                                        Tokens->nParameters,
+                                        TP,
                                         "%s isn't known!!",
                                         Parm->Start);
                }}
@@ -1324,14 +1390,16 @@ TemplateParam *GetNextParameter(StrBuf *Buf,
                /* well, we don't check the mobile stuff here... */
                if (!GetHash(LocalTemplateCache, Parm->Start, Parm->len, &vTmpl) &&
                    !GetHash(TemplateCache, Parm->Start, Parm->len, &vTmpl)) {
-                       LogTemplateError(
-                               NULL, "SubTemplate", ERR_PARM1, TP,
-                               "referenced here doesn't exist");
+                       LogTemplateError(NULL, 
+                                        "SubTemplate", 
+                                        Tokens->nParameters,
+                                        TP,
+                                        "referenced here doesn't exist");
                }}
                break;
        }
        *pCh = pch;
-       return Parm;
+       return 1;
 }
 
 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf, 
@@ -1343,7 +1411,6 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
 {
        void *vVar;
        const char *pch;
-       TemplateParam *Param;
        WCTemplateToken *NewToken;
        WCTemplputParams TP;
 
@@ -1380,8 +1447,15 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                                        "Warning, Non welformed Token; missing right parenthesis");
                        }
                        while (pch < pTokenEnd - 1) {
-                               Param = GetNextParameter(Buf, &pch, pTokenEnd - 1, NewToken, pTmpl, &TP);
-                               if (Param != NULL) {
+                               NewToken->nParameters++;
+                               if (GetNextParameter(Buf, 
+                                                    &pch, 
+                                                    pTokenEnd - 1, 
+                                                    NewToken, 
+                                                    pTmpl, 
+                                                    &TP, 
+                                                    &NewToken->Params[NewToken->nParameters - 1]))
+                               {
                                        NewToken->HaveParameters = 1;
                                        if (NewToken->nParameters > MAXPARAM) {
                                                LogTemplateError(
@@ -1389,11 +1463,9 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                                                        "only [%d] Params allowed in Tokens",
                                                        MAXPARAM);
 
-                                               free(Param);
                                                FreeToken(&NewToken);
                                                return NULL;
                                        }
-                                       NewToken->Params[NewToken->nParameters++] = Param;
                                }
                                else break;
                        }
@@ -2147,6 +2219,7 @@ int preeval_iterate(WCTemplateToken *Token)
        WCTemplputParams *TP;
        void *vTmpl;
        void *vIt;
+       HashIterator *It;
 
        memset(&TPP, 0, sizeof(WCTemplputParams));
        TP = &TPP;
@@ -2170,6 +2243,16 @@ int preeval_iterate(WCTemplateToken *Token)
                                 "referenced here doesn't exist");
        }
        Token->Preeval2 = vIt;
+       It = (HashIterator *) vIt;
+
+       if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
+               LogTemplateError(                               
+                       NULL, "Iterator", ERR_PARM1, TP,
+                       "doesn't work with %d params", 
+                       TP->Tokens->nParameters);
+       }
+
+
        return 1;
 }
 
@@ -2206,7 +2289,7 @@ void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
                LogTemplateError(                               
                        Target, "Iterator", ERR_PARM1, TP,
                        "doesn't work with %d params", 
-                       TP->Tokens->nParameters);
+                       TP->Tokens->nParameters - 1);
                return;
        }
 
@@ -2456,7 +2539,24 @@ long GetTokenDefine(const char *Name, long len,
         {
                 return DefValue;
         }
- }
+}
+
+void tmplput_DefStr(StrBuf *Target, WCTemplputParams *TP)
+{
+       const char *Str;
+       long len;
+       GetTemplateTokenString(Target, TP, 2, &Str, &len);
+       
+       StrBufAppendBufPlain(Target, Str, len, 0);
+}
+
+void tmplput_DefVal(StrBuf *Target, WCTemplputParams *TP)
+{
+       int val;
+
+       val = GetTemplateTokenNumber(Target, TP, 0, 0);
+       StrBufAppendPrintf(Target, "%d", val);
+}
 
 HashList *Defines;
 
@@ -2477,6 +2577,36 @@ int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
        return strcmp(ChrPtr(TokenText), CompareToken) == 0;
 }
 
+void tmplput_ContextStringArray(StrBuf *Target, WCTemplputParams *TP)
+{
+       HashList *Arr = (HashList*) CTX;
+       void *pV;
+       int val;
+
+       val = GetTemplateTokenNumber(Target, TP, 0, 0);
+       if (GetHash(Arr, IKEY(val), &pV) && 
+           (pV != NULL)) {
+               StrBufAppendTemplate(Target, TP, (StrBuf*)pV, 1);
+       }
+}
+int ConditionalContextStrinArray(StrBuf *Target, WCTemplputParams *TP)
+{
+       HashList *Arr = (HashList*) CTX;
+       void *pV;
+       int val;
+       const char *CompareToken;
+       long len;
+
+       GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
+       val = GetTemplateTokenNumber(Target, TP, 0, 0);
+       if (GetHash(Arr, IKEY(val), &pV) && 
+           (pV != NULL)) {
+               return strcmp(ChrPtr((StrBuf*)pV), CompareToken) == 0;
+       }
+       else
+               return 0;
+}
+
 /*-----------------------------------------------------------------------------
  *                      Boxed-API
  */
@@ -2993,14 +3123,19 @@ InitModule_SUBST
        RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
        RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
        RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
+       RegisterNamespace("CONTEXTSTRARR", 1, 2, tmplput_ContextStringArray, NULL, CTX_STRBUFARR);
        RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
        RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
        RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, preeval_do_tabbed, CTX_NONE);
        RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
+
+
        RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
        RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
+       RegisterConditional(HKEY("COND:CONTEXTSTRARR"), 4, ConditionalContextStrinArray, CTX_STRBUFARR);
        RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
 
+
        RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2, 
                                   conditional_ITERATE_ISGROUPCHANGE, 
                                   CTX_ITERATE);
@@ -3015,6 +3150,10 @@ InitModule_SUBST
        RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
        RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
        RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
+       RegisterNamespace("DEF:STR", 1, 1, tmplput_DefStr, NULL, CTX_NONE);
+       RegisterNamespace("DEF:VAL", 1, 1, tmplput_DefVal, NULL, CTX_NONE);
+
+
 
 
 }
@@ -3078,6 +3217,7 @@ SessionDetachModule_SUBST
 (wcsession *sess)
 {
        DeleteHash(&sess->vars);
+       FreeStrBuf(&sess->WFBuf);
 }
 
 void