]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* add support for defines
[citadel.git] / webcit / subst.c
index 71b8e3ff3aeabc520e2e4fed59c6bb3ff011e77f..4b85aa1066a7fe639db69ad3e1f4d5cd96e49fce 100644 (file)
@@ -25,6 +25,7 @@ HashList *GlobalNS;
 HashList *Iterators;
 HashList *Conditionals;
 HashList *SortHash;
+HashList *Defines;
 
 int DumpTemplateI18NStrings = 0;
 int LoadTemplates = 0;
@@ -769,7 +770,7 @@ void GetTemplateTokenString(StrBuf *Target,
        StrBuf *Buf;
        WCTemplputParams SubTP;
 
-       if (TP->Tokens->nParameters < N) {
+       if (N >= TP->Tokens->nParameters) {
                LogTemplateError(Target, 
                                 "TokenParameter", N, TP, 
                                 "invalid token %d. this shouldn't have come till here.\n", N);
@@ -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;
@@ -856,7 +858,7 @@ void GetTemplateTokenString(StrBuf *Target,
 long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
 {
        long Ret;
-       if (TP->Tokens->nParameters < N) {
+       if (N >= TP->Tokens->nParameters) {
                LogTemplateError(Target, 
                                 "TokenParameter", N, TP, 
                                 "invalid token %d. this shouldn't have come till here.\n", N);
@@ -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
  */
@@ -2294,6 +2337,11 @@ void RegisterSortFunc(const char *name, long len,
        NewSort->Reverse = Reverse;
        NewSort->GroupChange = GroupChange;
        NewSort->ContextType = ContextType;
+       if (ContextType == CTX_NONE) {
+               lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
+               exit(1);
+       }
+               
        Put(SortHash, name, len, NewSort, DestroySortStruct);
 }
 
@@ -2346,6 +2394,9 @@ CompareFunc RetrieveSort(WCTemplputParams *TP,
        }
        SortBy = (SortStruct*)vSortBy;
 
+       if (SortBy->ContextType != TP->Filter.ContextType)
+               return NULL;
+
        /** Ok, its us, lets see in which direction we should sort... */
        if (havebstr("SortOrder")) {
                SortOrder = LBSTR("SortOrder");
@@ -2619,6 +2670,12 @@ int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
        return 0;
 }
 
+
+void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
+{
+       StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
+}
+
 void 
 InitModule_SUBST
 (void)
@@ -2649,6 +2706,9 @@ InitModule_SUBST
        RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
        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);
+
+
 }
 
 void
@@ -2664,6 +2724,7 @@ ServerStartModule_SUBST
        Iterators = NewHash(1, NULL);
        Conditionals = NewHash(1, NULL);
        SortHash = NewHash(1, NULL);
+       Defines = NewHash(1, NULL);
 }
 
 void
@@ -2686,7 +2747,7 @@ ServerShutdownModule_SUBST
        DeleteHash(&Iterators);
        DeleteHash(&Conditionals);
        DeleteHash(&SortHash);
-
+       DeleteHash(&Defines);
 }