* add support for defines
authorWilfried Göesgens <willi@citadel.org>
Thu, 26 Nov 2009 23:33:40 +0000 (23:33 +0000)
committerWilfried Göesgens <willi@citadel.org>
Thu, 26 Nov 2009 23:33:40 +0000 (23:33 +0000)
Syntax;
 #"ib_logoff"
will be resolved to the long value of ib_logoff at parse time

webcit/subst.c
webcit/subst.h

index 90243de86e93009ce1589a33efd3dbcd0b95561a..4b85aa1066a7fe639db69ad3e1f4d5cd96e49fce 100644 (file)
@@ -25,6 +25,7 @@ HashList *GlobalNS;
 HashList *Iterators;
 HashList *Conditionals;
 HashList *SortHash;
+HashList *Defines;
 
 int DumpTemplateI18NStrings = 0;
 int LoadTemplates = 0;
@@ -780,6 +781,7 @@ void GetTemplateTokenString(StrBuf *Target,
 
        switch (TP->Tokens->Params[N]->Type) {
 
+       case TYPE_INTDEFINE:
        case TYPE_STR:
                *Value = TP->Tokens->Params[N]->Start;
                *len = TP->Tokens->Params[N]->len;
@@ -890,7 +892,8 @@ long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long df
                }
                if (get_PREF_LONG(TKEY(N), &Ret, dflt))
                        return Ret;
-               return 0;               
+               return 0;
+       case TYPE_INTDEFINE:
        case TYPE_LONG:
                return TP->Tokens->Params[N]->lvalue;
        case TYPE_PREFINT:
@@ -1111,7 +1114,12 @@ 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)
+TemplateParam *GetNextParameter(StrBuf *Buf, 
+                               const char **pCh, 
+                               const char *pe, 
+                               WCTemplateToken *Tokens, 
+                               WCTemplate *pTmpl, 
+                               WCTemplputParams *TP)
 {
        const char *pch = *pCh;
        const char *pchs, *pche;
@@ -1145,6 +1153,10 @@ TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, W
                        ParamBrace = 1;
                }
        }
+       else if (*pch == '#') {
+               Parm->Type = TYPE_INTDEFINE;
+               pch ++;
+       }
        else if (*pch == '_') {
                Parm->Type = TYPE_GETTEXT;
                pch ++;
@@ -1249,6 +1261,25 @@ TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, W
        if (DumpTemplateI18NStrings && (Parm->Type == TYPE_GETTEXT)) {
                StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
        }
+       if (Parm->Type == TYPE_INTDEFINE)
+       {
+               void *vPVal;
+
+               if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
+                   (vPVal != NULL))
+               {
+                       long *PVal;
+                       PVal = (long*) vPVal;
+               
+                       Parm->lvalue = *PVal;
+               }
+               else 
+               {
+                       LogTemplateError(NULL, "Define", ERR_PARM1, TP,
+                                        "%s isn't known!!",
+                                        Parm->Start);
+               }
+       }
        *pCh = pch;
        return Parm;
 }
@@ -1299,7 +1330,7 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                                        "Warning, Non welformed Token; missing right parenthesis");
                        }
                        while (pch < pTmplEnd - 1) {
-                               Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
+                               Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl, &TP);
                                if (Param != NULL) {
                                        NewToken->HaveParameters = 1;
                                        if (NewToken->nParameters > MAXPARAM) {
@@ -2174,6 +2205,18 @@ void RegisterControlConditional(const char *Name, long len,
        Put(Conditionals, Name, len, Cond, NULL);
 }
 
+void RegisterTokenParamDefine(const char *Name, long len, 
+                             long Value)
+{
+       long *PVal;
+
+       PVal = (long*)malloc(sizeof(long));
+       *PVal = Value;
+       Put(Defines, Name, len, PVal, NULL);
+}
+
+HashList *Defines;
+
 /*-----------------------------------------------------------------------------
  *                      Context Strings
  */
@@ -2681,6 +2724,7 @@ ServerStartModule_SUBST
        Iterators = NewHash(1, NULL);
        Conditionals = NewHash(1, NULL);
        SortHash = NewHash(1, NULL);
+       Defines = NewHash(1, NULL);
 }
 
 void
@@ -2703,7 +2747,7 @@ ServerShutdownModule_SUBST
        DeleteHash(&Iterators);
        DeleteHash(&Conditionals);
        DeleteHash(&SortHash);
-
+       DeleteHash(&Defines);
 }
 
 
index d1157d87ab90f1515e7097acd639fad108855f35..ba27b21c3cbdcc8692c47a80e3ad9b722f85b2a5 100644 (file)
@@ -15,6 +15,7 @@ extern HashList *LocalTemplateCache;
 #define TYPE_GETTEXT 5
 #define TYPE_BSTR 6
 #define TYPE_SUBTEMPLATE 7
+#define TYPE_INTDEFINE 8
 #define MAXPARAM  20
 
 
@@ -282,7 +283,17 @@ void RegisterConditional(const char *Name, long len,
                         WCConditionalFunc CondF, 
                         int ContextRequired);
 
-
+/**
+ * @brief register a string that will represent a long value
+ * this will allow to resolve <?...(#"Name")> to Value; that way 
+ * plain strings can be used an lexed in templates without having the 
+ * lookup overhead at runtime.
+ * @param Name The name of the define
+ * @param len length of Name
+ * @param Value the value to associate with Name
+ */
+void RegisterTokenParamDefine(const char *Name, long len, 
+                             long Value);
 
 #define IT_NOFLAG 0
 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)