]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* fix dropdown
[citadel.git] / webcit / subst.c
index 2593732906661027b9f5d6b782de491d6d8a3ada..de4c18ee1d78bd7f9118712ced9084877e289988 100644 (file)
@@ -139,7 +139,7 @@ void VarPrintEntry(const char *Key, void *vSubst, int odd)
 /**
  * \brief Clear out the list of substitution variables local to this session
  */
-void clear_substs(struct wcsession *wc) {
+void clear_substs(wcsession *wc) {
 
        if (wc->vars != NULL) {
                DeleteHash(&wc->vars);
@@ -221,7 +221,7 @@ void deletevar(void *data)
 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
 {
        wcsubst* ptr;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
 
        ptr = (wcsubst *) malloc(sizeof(wcsubst));
        memset(ptr, 0, sizeof(wcsubst));
@@ -259,7 +259,7 @@ void SVPRINTF(char *keyname, int keytype, const char *format,...)
        void *vPtr;
        wcsubst *ptr = NULL;
        size_t keylen;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        
        keylen = strlen(keyname);
        /**
@@ -295,7 +295,7 @@ void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
        va_list arg_ptr;
        void *vPtr;
        wcsubst *ptr = NULL;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
                
        /**
         * First look if we're doing a replacement of
@@ -329,7 +329,7 @@ void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
 {
        void *vPtr;
        wcsubst *ptr = NULL;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
 
        
        /**
@@ -360,7 +360,7 @@ void SVPutLong(char *keyname, size_t keylen, long Data)
 {
        void *vPtr;
        wcsubst *ptr = NULL;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
 
        
        /**
@@ -389,7 +389,7 @@ void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
 {
        wcsubst *ptr;
        void *vPtr;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
 
        /**
         * First look if we're doing a replacement of
@@ -419,7 +419,7 @@ void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
 {
        wcsubst *ptr;
        void *vPtr;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
 
        /**
         * First look if we're doing a replacement of
@@ -485,16 +485,12 @@ void GetTemplateTokenString(WCTemplateToken *Tokens,
                *len = Tokens->Params[N]->len;
                break;
        case TYPE_BSTR:
-               Buf = (StrBuf*) SBstr(Tokens->Params[N]->Start, 
-                                     Tokens->Params[N]->len);
+               Buf = (StrBuf*) SBstr(TKEY(N));
                *Value = ChrPtr(Buf);
                *len = StrLength(Buf);
                break;
        case TYPE_PREFSTR:
-               get_PREFERENCE(
-                       Tokens->Params[N]->Start, 
-                       Tokens->Params[N]->len, 
-                       &Buf);
+               get_PREFERENCE(TKEY(N), &Buf);
                *Value = ChrPtr(Buf);
                *len = StrLength(Buf);
                break;
@@ -518,7 +514,7 @@ void GetTemplateTokenString(WCTemplateToken *Tokens,
  * \param keyname get a key to print
  */
 void print_value_of(StrBuf *Target, WCTemplateToken *Tokens, void *Context, int ContextType) {
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        wcsubst *ptr;
        void *vVar;
 
@@ -568,7 +564,7 @@ void print_value_of(StrBuf *Target, WCTemplateToken *Tokens, void *Context, int
 
 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
 {
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        wcsubst *ptr;
        void *vVar;
 
@@ -606,7 +602,7 @@ int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLoo
 
 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
 {
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        wcsubst *ptr;
        void *vVar;
 
@@ -647,7 +643,7 @@ void StrBufAppendTemplate(StrBuf *Target,
                          void *Context, int ContextType,
                          const StrBuf *Source, int FormatTypeIndex)
 {
-        struct wcsession *WCC;
+        wcsession *WCC;
        StrBuf *Buf;
        char EscapeAs = ' ';
 
@@ -936,28 +932,28 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
        case SV_CUST_STR_CONDITIONAL:
        case SV_CONDITIONAL:
        case SV_NEG_CONDITIONAL:
-               if (NewToken->Params[1]->lvalue == 0) {
+               if (NewToken->nParameters <2) {
                        lprintf(1, "Conditional (in '%s' line %ld); "
-                               "Conditional ID mustn't be 0! [%s]\n", 
+                               "require at least 2 parameters, you gave %ld params [%s]\n", 
                                ChrPtr(pTmpl->FileName),
                                NewToken->Line,
+                               NewToken->nParameters, 
                                ChrPtr(NewToken->FlatToken));
                        NewToken->Flags = 0;
                        break;
                }
-               if (NewToken->nParameters <2) {
+               if (NewToken->Params[1]->lvalue == 0) {
                        lprintf(1, "Conditional (in '%s' line %ld); "
-                               "require at least 2 parameters, you gave %ld params [%s]\n", 
+                               "Conditional ID mustn't be 0! [%s]\n", 
                                ChrPtr(pTmpl->FileName),
                                NewToken->Line,
-                               NewToken->nParameters, 
                                ChrPtr(NewToken->FlatToken));
                        NewToken->Flags = 0;
                        break;
                }
                if (!GetHash(Conditionals, 
-                            NewToken->Params[0]->Start,
-                            NewToken->Params[0]->len,
+                            NewToken->Params[0]->Start, 
+                            NewToken->Params[0]->len, 
                             &vVar) || 
                    (vVar == NULL)) {
                        if ((NewToken->Params[0]->len == 1) &&
@@ -1070,7 +1066,7 @@ void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
                pts = pch;
 
                /** Found one? parse it. */
-               for (; pch < pE - 1; pch ++) {
+               for (; pch <= pE - 1; pch ++) {
                        if (*pch == '"')
                                InDoubleQuotes = ! InDoubleQuotes;
                        else if (*pch == '\'')
@@ -1081,7 +1077,7 @@ void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
                                break;
                        }
                }
-               if (pch + 1 >= pE)
+               if (pch + 1 > pE)
                        continue;
                pte = pch;
                PutNewToken(NewTemplate, 
@@ -1189,6 +1185,8 @@ void InitTemplateCache(void)
  */
 int EvaluateToken(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, void *Context, int state, int ContextType)
 {
+       const char *AppendMe;
+       long AppendMeLen;
        HashHandler *Handler;
        void *vVar;
 // much output, since pName is not terminated...
@@ -1206,16 +1204,20 @@ int EvaluateToken(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, vo
                break;
        case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
                if (Tokens->nParameters >= 6) {
-                       if (EvaluateConditional(Target, Tokens, pTmpl, Context, 0, state, ContextType))
+                       if (EvaluateConditional(Target, Tokens, pTmpl, Context, 0, state, ContextType)) {
+                               GetTemplateTokenString(Tokens, 5, &AppendMe, &AppendMeLen);
                                StrBufAppendBufPlain(Target, 
-                                                    Tokens->Params[5]->Start,
-                                                    Tokens->Params[5]->len,
+                                                    AppendMe, 
+                                                    AppendMeLen,
                                                     0);
-                       else
+                       }
+                       else{
+                               GetTemplateTokenString(Tokens, 4, &AppendMe, &AppendMeLen);
                                StrBufAppendBufPlain(Target, 
-                                                    Tokens->Params[4]->Start,
-                                                    Tokens->Params[4]->len,
+                                                    AppendMe, 
+                                                    AppendMeLen,
                                                     0);
+                       }
                }
                else  {
                        lprintf(1, "Conditional [%s] (in '%s' line %ld); needs at least 6 Params![%s]\n", 
@@ -1234,7 +1236,7 @@ int EvaluateToken(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, vo
                break;
        case SV_SUBTEMPL:
                if (Tokens->nParameters == 1)
-                       DoTemplate(Tokens->Params[0]->Start, Tokens->Params[0]->len, NULL, NULL, ContextType);
+                       DoTemplate(TKEY(0), NULL, NULL, ContextType);
                break;
        case SV_PREEVALUATED:
                Handler = (HashHandler*) Tokens->PreEval;
@@ -1477,16 +1479,15 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
        HashIterator *It;
        HashList *List;
        HashPos  *it;
+       int nMembersUsed;
+       int nMembersCounted = 0;
        long len; 
        const char *Key;
        void *vContext;
        StrBuf *SubBuf;
        int oddeven = 0;
        
-       if (!GetHash(Iterators, 
-                    Tokens->Params[0]->Start,
-                    Tokens->Params[0]->len,
-                    &vIt)) {
+       if (!GetHash(Iterators, TKEY(0), &vIt)) {
                lprintf(1, "unknown Iterator [%s] (in '%s' line %ld); "
                        " [%s]\n", 
                        Tokens->Params[0]->Start,
@@ -1555,19 +1556,19 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
        else
                List = It->StaticList;
 
+       nMembersUsed = GetCount(List);
        SubBuf = NewStrBuf();
        it = GetNewHashPos(List, 0);
        while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
                svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s", 
                         (oddeven) ? "odd" : "even");
                svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
+               svputlong("ITERATE:N", nMembersCounted);
+               svputlong("ITERATE:LASTN", ++nMembersCounted == nMembersUsed);
 
                if (It->DoSubTemplate != NULL)
                        It->DoSubTemplate(SubBuf, vContext, Tokens);
-               DoTemplate(Tokens->Params[1]->Start,
-                          Tokens->Params[1]->len,
-                          SubBuf, vContext,
-                          It->ContextType);
+               DoTemplate(TKEY(1), SubBuf, vContext, It->ContextType);
                        
                StrBufAppendBuf(Target, SubBuf, 0);
                FlushStrBuf(SubBuf);
@@ -1629,10 +1630,7 @@ int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
        void *vsubst;
        wcsubst *subst;
        
-       if (!GetHash(WC->vars, 
-                    Tokens->Params[2]->Start,
-                    Tokens->Params[2]->len,
-                    &vsubst))
+       if (!GetHash(WC->vars, TKEY(2), &vsubst))
                return 0;
        subst = (wcsubst*) vsubst;
        if ((subst->ContextRequired != CTX_NONE) &&
@@ -1705,11 +1703,7 @@ void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Con
        if (nArgs == 2) {
                if (Tokens->Params[1]->Type == TYPE_STR) {
                        Headline = NewStrBuf();
-                       DoTemplate(Tokens->Params[1]->Start, 
-                                  Tokens->Params[1]->len,
-                                  Headline, 
-                                  Context, 
-                                  ContextType);
+                       DoTemplate(TKEY(1), Headline, Context, ContextType);
                }
                else {
                        const char *Ch;
@@ -1723,11 +1717,7 @@ void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Con
        }
        
        DoTemplate(HKEY("beginbox"), Target, Headline, CTX_STRBUF);
-       DoTemplate(Tokens->Params[0]->Start, 
-                  Tokens->Params[0]->len,
-                  Target, 
-                  Context, 
-                  ContextType);
+       DoTemplate(TKEY(0), Target, Context, ContextType);
        DoTemplate(HKEY("endbox"), Target, Context, ContextType);
        FreeStrBuf(&Headline);
 }
@@ -1747,11 +1737,7 @@ void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Co
        for (i = 0; i < ntabs; i++) {
                TabNames[i] = NewStrBuf();
                if (Tokens->Params[i * 2]->len > 0) {
-                       DoTemplate(Tokens->Params[i * 2]->Start, 
-                                  Tokens->Params[i * 2]->len,
-                                  TabNames[i],
-                                  Context,
-                                  ContextType);
+                       DoTemplate(TKEY(i * 2), TabNames[i], Context, ContextType);
                }
                else { 
                        /** A Tab without subject? we can't count that, add it as silent */
@@ -1763,11 +1749,7 @@ void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Co
        for (i = 0; i < ntabs; i++) {
                StrBeginTab(Target, i, nTabs);
 
-               DoTemplate(Tokens->Params[i * 2 + 1]->Start, 
-                          Tokens->Params[i * 2 + 1]->len,
-                          Target,
-                          Context, 
-                          ContextType);
+               DoTemplate(TKEY(i * 2 + 1), Target, Context, ContextType);
                StrEndTab(Target, i, nTabs);
        }
 }
@@ -1896,7 +1878,7 @@ int GetSortMetric(WCTemplateToken *Tokens, SortStruct **Next, SortStruct **Param
        
        *SortOrder = 0;
        *Next = NULL;
-       if (!GetHash(SortHash, Tokens->Params[0]->Start, Tokens->Params[0]->len, &vSort) || 
+       if (!GetHash(SortHash, TKEY(0), &vSort) || 
            (vSort == NULL))
                return eNO_SUCH_SORT;
        *Param = (SortStruct*) vSort;
@@ -2123,7 +2105,22 @@ void tmplput_long_vector(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, voi
        }
 }
 
+void dbg_print_longvector(long *LongVector)
+{
+       StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
+       int nItems = LongVector[0];
+       int i;
 
+       for (i = 0; i < nItems; i++) {
+               if (i + 1 < nItems)
+                       StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
+               else
+                       StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
+
+       }
+       lprintf(1, ChrPtr(Buf));
+       FreeStrBuf(&Buf);
+}
 
 int ConditionalLongVector(WCTemplateToken *Tokens, void *Context, int ContextType)
 {