* --pedantic cleanup.
[citadel.git] / webcit / subst.c
index 0463c6f8f0d70ad0c525e5277fcd14a2d1e5b631..ac0dd07874475cebb21101103975a623f39bb818 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,29 +485,25 @@ 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;
        case TYPE_LONG:
        case TYPE_PREFINT:
-               break; ///todo: string to text?
+               break; /* todo: string to text? */
        case TYPE_GETTEXT:
                *Value = _(Tokens->Params[N]->Start);
                *len = strlen(*Value);
                break;
        default:
                break;
-//todo log error
+/*/todo log error */
        }
 }
 
@@ -518,18 +514,18 @@ 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;
 
        /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
-       /// TODO: debricated!
+       /* TODO: depricated! */
        if (Tokens->pName[0] == '=') {
                DoTemplate(Tokens->pName+1, Tokens->NameEnd - 1, NULL, NULL, 0);
        }
 
-//////TODO: if param[1] == "U" -> urlescape
-/// X -> escputs
+/*/////TODO: if param[1] == "U" -> urlescape
+/// X -> escputs */
        /** Page-local variables */
        if ((WCC->vars!= NULL) && GetHash(WCC->vars, Tokens->pName, Tokens->NameEnd, &vVar)) {
                ptr = (wcsubst*) 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, 
@@ -1094,7 +1090,6 @@ void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
 }
 
 
-///void PrintTemplate(const char *Key, void *vSubst, int odd)
 const char* PrintTemplate(void *vSubst)
 {
        WCTemplate *Tmpl = vSubst;
@@ -1189,10 +1184,13 @@ 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...
-//     lprintf(1,"Doing token: %s\n",Token->pName);
+/* much output, since pName is not terminated...
+       lprintf(1,"Doing token: %s\n",Token->pName);
+*/
 
        switch (Tokens->Flags) {
        case SV_GETTEXT:
@@ -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;
@@ -1429,8 +1431,10 @@ void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Contex
                StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>", 
                                   templatename, len, 
                                   (long)strlen(templatename));
-///            dbg_PrintHash(Static, PrintTemplate, NULL);
-//             PrintHash(Static, VarPrintTransition, PrintTemplate);
+#if 0
+               dbg_PrintHash(Static, PrintTemplate, NULL);
+               PrintHash(Static, VarPrintTransition, PrintTemplate);
+#endif
                return;
        }
        if (vTmpl == NULL) 
@@ -1485,10 +1489,7 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
        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,
@@ -1569,10 +1570,7 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
 
                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);
@@ -1634,10 +1632,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) &&
@@ -1650,7 +1645,7 @@ int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
        case WCS_FUNCTION:
                return (subst->wcs_function!=NULL);
        case WCS_SERVCMD:
-               lprintf(1, "  -> Server [%s]\n", subst->wcs_value);////todo
+               lprintf(1, "  -> Server [%s]\n", subst->wcs_value);/* TODO */
                return 1;
        case WCS_STRING:
        case WCS_STRBUF:
@@ -1710,11 +1705,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;
@@ -1728,11 +1719,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);
 }
@@ -1752,11 +1739,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 */
@@ -1768,11 +1751,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);
        }
 }
@@ -1824,7 +1803,7 @@ CompareFunc RetrieveSort(long ContextType, const char *OtherPrefix,
        const StrBuf *BSort;
        SortStruct *SortBy;
        void *vSortBy;
-       long SortOrder;
+       long SortOrder = -1;
        
        if (havebstr("SortBy")) {
                BSort = sbstr("SortBy");
@@ -1834,7 +1813,15 @@ CompareFunc RetrieveSort(long ContextType, const char *OtherPrefix,
                        BSort = get_room_pref("sort");
                }
                else {
-                       ////todo: nail prefprepend to sort, and lookup this!
+                       /*TODO: nail prefprepend to sort, and lookup this! */
+               }
+               if (BSort != NULL)
+                       putbstr("SortBy", NewStrBufDup(BSort));
+               else {
+                       StrBuf *Buf;
+
+                       BSort = Buf = NewStrBufPlain(Default, ldefault);
+                       putbstr("SortBy", Buf);
                }
        }
 
@@ -1854,12 +1841,21 @@ CompareFunc RetrieveSort(long ContextType, const char *OtherPrefix,
                SortOrder = LBSTR("SortOrder");
        }
        else { /** Try to fallback to our remembered values... */
+               StrBuf *Buf;
                if (SortBy->PrefPrepend == NULL) {
-                       SortOrder = StrTol(get_room_pref("SortOrder"));
+                       Buf = get_room_pref("SortOrder");
+                       SortOrder = StrTol(Buf);
                }
                else {
-                       ////todo: nail prefprepend to sort, and lookup this!
+                       /* TODO: nail prefprepend to sort, and lookup this! */
                }
+
+               if (Buf == NULL)
+                       SortOrder = DefaultDirection;
+
+               Buf = NewStrBufPlain(NULL, 64);
+               StrBufPrintf(Buf, "%ld", SortOrder);
+               putbstr("SortOrder", Buf);
        }
        switch (SortOrder) {
        default:
@@ -1901,7 +1897,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;
@@ -1916,7 +1912,7 @@ int GetSortMetric(WCTemplateToken *Tokens, SortStruct **Next, SortStruct **Param
                        BSort = get_room_pref("sort");
                }
                else {
-                       ////todo: nail prefprepend to sort, and lookup this!
+                       /* TODO: nail prefprepend to sort, and lookup this! */
                }
        }
 
@@ -1935,7 +1931,7 @@ int GetSortMetric(WCTemplateToken *Tokens, SortStruct **Next, SortStruct **Param
                        *SortOrder = StrTol(get_room_pref("SortOrder"));
                }
                else {
-                       ////todo: nail prefprepend to sort, and lookup this!
+                       /* TODO: nail prefprepend to sort, and lookup this! */
                }
        }
        if (*SortOrder > 2)
@@ -2128,7 +2124,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)
 {