5 * \defgroup Subst Variable substitution type stuff
6 * \ingroup CitadelConfig
12 #include <sys/types.h>
18 #define SHOW_ME_VAPPEND_PRINTF
21 #include "webserver.h"
23 extern char *static_dirs[PATH_MAX]; /**< Disk representation */
25 HashList *WirelessTemplateCache;
26 HashList *WirelessLocalTemplateCache;
27 HashList *TemplateCache;
28 HashList *LocalTemplateCache;
32 HashList *Conditionals;
35 int LoadTemplates = 0;
38 #define SV_CONDITIONAL 2
39 #define SV_NEG_CONDITIONAL 3
40 #define SV_CUST_STR_CONDITIONAL 4
42 #define SV_PREEVALUATED 6
44 typedef struct _WCTemplate {
49 WCTemplateToken **Tokens;
52 typedef struct _HashHandler {
56 WCHandlerFunc HandlerFunc;
59 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere);
60 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType);
62 void RegisterNS(const char *NSName,
66 WCHandlerFunc HandlerFunc,
69 HashHandler *NewHandler;
71 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
72 NewHandler->nMinArgs = nMinArgs;
73 NewHandler->nMaxArgs = nMaxArgs;
74 NewHandler->HandlerFunc = HandlerFunc;
75 NewHandler->ContextRequired = ContextRequired;
76 Put(GlobalNS, NSName, len, NewHandler, NULL);
79 void FreeToken(WCTemplateToken **Token)
82 FreeStrBuf(&(*Token)->FlatToken);
83 if ((*Token)->HaveParameters)
84 for (i = 0; i < (*Token)->nParameters; i++)
85 free((*Token)->Params[i]);
92 void FreeWCTemplate(void *vFreeMe)
95 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
97 if (FreeMe->TokenSpace > 0) {
98 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
99 FreeToken(&FreeMe->Tokens[i]);
101 free(FreeMe->Tokens);
103 FreeStrBuf(&FreeMe->FileName);
104 FreeStrBuf(&FreeMe->Data);
110 * \brief debugging function to print array to log
112 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
114 * \brief debugging function to print array to log
116 void VarPrintEntry(const char *Key, void *vSubst, int odd)
119 lprintf(1,"Subst[%s] : ", Key);
120 ptr = (wcsubst*) vSubst;
122 switch(ptr->wcs_type) {
124 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
127 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
130 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
133 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
140 * \brief Clear out the list of substitution variables local to this session
142 void clear_substs(struct wcsession *wc) {
144 if (wc->vars != NULL) {
145 DeleteHash(&wc->vars);
150 * \brief Clear out the list of substitution variables local to this session
152 void clear_local_substs(void) {
171 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
173 int NeedNew = NeedNewBuf(type);
174 switch(ptr->wcs_type) {
178 if (reusestrbuf && NeedNew) {
179 FlushStrBuf(ptr->wcs_value);
183 FreeStrBuf(&ptr->wcs_value);
184 ptr->wcs_value = NULL;
188 ptr->wcs_function = NULL;
189 if (reusestrbuf && NeedNew)
190 ptr->wcs_value = NewStrBuf();
193 ptr->wcs_value = NULL;
194 if (reusestrbuf && NeedNew)
195 ptr->wcs_value = NewStrBuf();
199 if (reusestrbuf && NeedNew)
200 ptr->wcs_value = NewStrBuf();
203 if (reusestrbuf && NeedNew)
204 ptr->wcs_value = NewStrBuf();
211 * \brief destructor; kill one entry.
213 void deletevar(void *data)
215 wcsubst *ptr = (wcsubst*)data;
216 FlushPayload(ptr, 0, ptr->wcs_type);
221 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
224 struct wcsession *WCC = WC;
226 ptr = (wcsubst *) malloc(sizeof(wcsubst));
227 memset(ptr, 0, sizeof(wcsubst));
229 ptr->wcs_type = type;
230 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
231 Put(WCC->vars, keyname, keylen, ptr, deletevar);
233 switch(ptr->wcs_type) {
236 ptr->wcs_value = NewStrBuf();
250 * \brief Add a substitution variable (local to this session) (strlen version...)
251 * \param keyname the replacementstring to substitute
252 * \param keytype the kind of the key
253 * \param format the format string ala printf
254 * \param ... the arguments to substitute in the formatstring
256 void SVPRINTF(char *keyname, int keytype, const char *format,...)
262 struct wcsession *WCC = WC;
264 keylen = strlen(keyname);
266 * First look if we're doing a replacement of
269 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
270 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
271 ptr = (wcsubst*)vPtr;
272 FlushPayload(ptr, keytype, keytype);
273 ptr->wcs_type = keytype;
275 else /** Otherwise allocate a new one */
277 ptr = NewSubstVar(keyname, keylen, keytype);
280 /** Format the string */
281 va_start(arg_ptr, format);
282 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
287 * \brief Add a substitution variable (local to this session)
288 * \param keyname the replacementstring to substitute
289 * \param keytype the kind of the key
290 * \param format the format string ala printf
291 * \param ... the arguments to substitute in the formatstring
293 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
298 struct wcsession *WCC = WC;
301 * First look if we're doing a replacement of
304 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
305 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
306 ptr = (wcsubst*)vPtr;
307 FlushPayload(ptr, 1, keytype);
308 ptr->wcs_type = keytype;
310 else /** Otherwise allocate a new one */
312 ptr = NewSubstVar(keyname, keylen, keytype);
315 /** Format the string and save it */
316 va_start(arg_ptr, format);
317 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
322 * \brief Add a substitution variable (local to this session)
323 * \param keyname the replacementstring to substitute
324 * \param keytype the kind of the key
325 * \param format the format string ala printf
326 * \param ... the arguments to substitute in the formatstring
328 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
332 struct wcsession *WCC = WC;
336 * First look if we're doing a replacement of
339 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
340 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
341 ptr = (wcsubst*)vPtr;
342 FlushPayload(ptr, 1, keytype);
343 ptr->wcs_type = keytype;
345 else /** Otherwise allocate a new one */
347 ptr = NewSubstVar(keyname, keylen, keytype);
349 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
353 * \brief Add a substitution variable (local to this session)
354 * \param keyname the replacementstring to substitute
355 * \param keytype the kind of the key
356 * \param format the format string ala printf
357 * \param ... the arguments to substitute in the formatstring
359 void SVPutLong(char *keyname, size_t keylen, long Data)
363 struct wcsession *WCC = WC;
367 * First look if we're doing a replacement of
370 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
371 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
372 ptr = (wcsubst*)vPtr;
373 FlushPayload(ptr, 1, WCS_LONG);
374 ptr->wcs_type = WCS_LONG;
376 else /** Otherwise allocate a new one */
378 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
384 * \brief Add a substitution variable (local to this session) that does a callback
385 * \param keyname the keystring to substitute
386 * \param fcn_ptr the function callback to give the substitution string
388 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
392 struct wcsession *WCC = WC;
395 * First look if we're doing a replacement of
398 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
399 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
400 ptr = (wcsubst*)vPtr;
401 FlushPayload(ptr, 1, WCS_FUNCTION);
402 ptr->wcs_type = WCS_FUNCTION;
404 else /** Otherwise allocate a new one */
406 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
409 ptr->wcs_function = fcn_ptr;
411 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
413 SVCallback(keyname, strlen(keyname), fcn_ptr);
418 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
422 struct wcsession *WCC = WC;
425 * First look if we're doing a replacement of
428 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
429 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
430 ptr = (wcsubst*)vPtr;
431 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
432 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
434 else /** Otherwise allocate a new one */
436 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
438 ptr->wcs_value = (StrBuf*)Buf;
442 * \brief back end for print_value_of() ... does a server command
443 * \param servcmd server command to execute on the citadel server
445 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
449 serv_puts(ChrPtr(servcmd));
450 len = serv_getln(buf, sizeof buf);
456 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
459 _fmout(Target, "CENTER");
462 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
468 void GetTemplateTokenString(WCTemplateToken *Tokens,
475 if (Tokens->nParameters < N) {
481 switch (Tokens->Params[N]->Type) {
484 *Value = Tokens->Params[N]->Start;
485 *len = Tokens->Params[N]->len;
488 Buf = (StrBuf*) SBstr(Tokens->Params[N]->Start,
489 Tokens->Params[N]->len);
490 *Value = ChrPtr(Buf);
491 *len = StrLength(Buf);
495 Tokens->Params[N]->Start,
496 Tokens->Params[N]->len,
498 *Value = ChrPtr(Buf);
499 *len = StrLength(Buf);
503 break; ///todo: string to text?
505 *Value = _(Tokens->Params[N]->Start);
506 *len = strlen(*Value);
517 * \brief Print the value of a variable
518 * \param keyname get a key to print
520 void print_value_of(StrBuf *Target, WCTemplateToken *Tokens, void *Context, int ContextType) {
521 struct wcsession *WCC = WC;
525 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
526 /// TODO: debricated!
527 if (Tokens->pName[0] == '=') {
528 DoTemplate(Tokens->pName+1, Tokens->NameEnd - 1, NULL, NULL, 0);
531 //////TODO: if param[1] == "U" -> urlescape
533 /** Page-local variables */
534 if ((WCC->vars!= NULL) && GetHash(WCC->vars, Tokens->pName, Tokens->NameEnd, &vVar)) {
535 ptr = (wcsubst*) vVar;
536 switch(ptr->wcs_type) {
538 StrBufAppendBuf(Target, ptr->wcs_value, 0);
541 pvo_do_cmd(Target, ptr->wcs_value);
544 (*ptr->wcs_function) (Target, Tokens->nParameters, Tokens, Context, ContextType);
548 StrBufAppendBuf(Target, ptr->wcs_value, 0);
551 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
554 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n", Tokens->pName);
555 StrBufAppendPrintf(Target, "<pre>WARNING: \ninvalid value in SV-Hash at %s!\n</pre>", Tokens->pName);
559 lprintf(1, "didn't find Handler [%s] (in '%s' line %ld); "
562 ChrPtr(Tokens->FileName),
564 ChrPtr(Tokens->FlatToken));
569 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
571 struct wcsession *WCC = WC;
575 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
576 ParamToLookup->len, &vVar)) {
577 ptr = (wcsubst*) vVar;
578 switch(ptr->wcs_type) {
582 if (ParamToCompare->Type == TYPE_STR)
583 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
584 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
586 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
594 if (ParamToCompare->Type == TYPE_STR)
597 return ParamToCompare->lvalue == ptr->lvalue;
600 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
601 ParamToLookup->Start);
607 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
609 struct wcsession *WCC = WC;
613 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
614 ParamToLookup->len, &vVar)) {
615 ptr = (wcsubst*) vVar;
616 switch(ptr->wcs_type) {
620 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
621 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
628 return StrTol(Compare) == ptr->lvalue;
630 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
631 ParamToLookup->Start);
640 * \brief puts string into the template and computes which escape methon we should use
641 * \param Source the string we should put into the template
642 * \param FormatTypeIndex where should we look for escape types if?
644 void StrBufAppendTemplate(StrBuf *Target,
646 WCTemplateToken *Tokens,
647 void *Context, int ContextType,
648 const StrBuf *Source, int FormatTypeIndex)
650 struct wcsession *WCC;
654 if ((FormatTypeIndex < Tokens->nParameters) &&
655 (Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
656 (Tokens->Params[FormatTypeIndex]->len == 1)) {
657 EscapeAs = *Tokens->Params[FormatTypeIndex]->Start;
664 Buf = NewStrBufPlain(NULL, StrLength(Source));
665 StrBuf_RFC822_to_Utf8(Buf,
667 (WCC!=NULL)? WCC->DefaultCharset : NULL,
669 StrEscAppend(Target, Buf, NULL, 0, 0);
673 StrEscAppend(Target, Source, NULL, 0, 0);
676 StrBufAppendBuf(Target, Source, 0);
681 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
683 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
684 if (Template->TokenSpace <= 0) {
685 Template->Tokens = (WCTemplateToken**)malloc(
686 sizeof(WCTemplateToken*) * 10);
687 Template->TokenSpace = 10;
690 WCTemplateToken **NewTokens;
691 NewTokens= (WCTemplateToken**)malloc(
692 sizeof(WCTemplateToken*) *
693 Template->TokenSpace * 2);
694 memcpy(NewTokens, Template->Tokens,
695 sizeof(WCTemplateToken*) * Template->nTokensUsed);
696 free(Template->Tokens);
697 Template->TokenSpace *= 2;
698 Template->Tokens = NewTokens;
701 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
704 TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, WCTemplateToken *Tokens, WCTemplate *pTmpl)
706 const char *pch = *pCh;
707 const char *pchs, *pche;
708 TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
712 Parm->Type = TYPE_STR;
714 /* Skip leading whitespaces */
715 while ((*pch == ' ' )||
718 (*pch == '\n')) pch ++;
721 Parm->Type = TYPE_PREFSTR;
724 else if (*pch == ';') {
725 Parm->Type = TYPE_PREFINT;
728 else if (*pch == '_') {
729 Parm->Type = TYPE_GETTEXT;
732 else if (*pch == 'B') {
733 Parm->Type = TYPE_BSTR;
740 else if (*pch == '\'')
747 ( (pch > pchs) && (*(pch - 1) == '\\'))
753 lprintf(1, "Error (in '%s' line %ld); "
754 "evaluating template param [%s] in Token [%s]\n",
755 ChrPtr(pTmpl->FileName),
757 ChrPtr(Tokens->FlatToken),
764 StrBufPeek(Buf, pch, -1, '\0');
765 if (LoadTemplates > 1) {
766 lprintf(1, "DBG: got param [%s] %ld %ld\n",
767 pchs, pche - pchs, strlen(pchs));
770 Parm->len = pche - pchs;
771 pch ++; /* move after trailing quote */
775 Parm->Type = TYPE_LONG;
777 while ((pch <= pe) &&
784 StrBufPeek(Buf, pch, -1, '\0');
785 Parm->lvalue = atol(pchs);
791 lprintf(1, "Error (in '%s' line %ld); "
792 "evaluating long template param [%s] in Token [%s]\n",
793 ChrPtr(pTmpl->FileName),
795 ChrPtr(Tokens->FlatToken),
801 while ((*pch == ' ' )||
805 (*pch == '\n')) pch ++;
811 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
813 const char *pTmplStart,
814 const char *pTmplEnd,
820 TemplateParam *Param;
821 WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
823 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
825 NewToken->Line = Line + 1;
826 NewToken->pTokenStart = pTmplStart;
827 NewToken->TokenStart = pTmplStart - pStart;
828 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
829 NewToken->pTokenEnd = pTmplEnd;
830 NewToken->NameEnd = NewToken->TokenEnd - 2;
831 NewToken->PreEval = NULL;
832 NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
834 StrBufPeek(Buf, pTmplStart, + 1, '\0');
835 StrBufPeek(Buf, pTmplEnd, -1, '\0');
836 pch = NewToken->pName = pTmplStart + 2;
838 NewToken->HaveParameters = 0;;
839 NewToken->nParameters = 0;
841 while (pch < pTmplEnd - 1) {
843 StrBufPeek(Buf, pch, -1, '\0');
844 NewToken->NameEnd = pch - NewToken->pName;
846 if (*(pTmplEnd - 1) != ')') {
847 lprintf(1, "Warning, Non welformed Token; missing right parenthesis (in '%s' line %ld); "
849 ChrPtr(pTmpl->FileName),
851 ChrPtr(NewToken->FlatToken));
853 while (pch < pTmplEnd - 1) {
854 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
856 NewToken->HaveParameters = 1;
857 if (NewToken->nParameters > MAXPARAM) {
858 lprintf(1, "Error (in '%s' line %ld); "
859 "only [%ld] Params allowed in Tokens [%s]\n",
860 ChrPtr(pTmpl->FileName),
863 ChrPtr(NewToken->FlatToken));
865 FreeToken(&NewToken);
868 NewToken->Params[NewToken->nParameters++] = Param;
872 if((NewToken->NameEnd == 1) &&
873 (NewToken->HaveParameters == 1))
876 if (*(NewToken->pName) == '_')
877 NewToken->Flags = SV_GETTEXT;
878 else if (*(NewToken->pName) == '=')
879 NewToken->Flags = SV_SUBTEMPL;
880 else if (*(NewToken->pName) == '%')
881 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
882 else if (*(NewToken->pName) == '?')
883 NewToken->Flags = SV_CONDITIONAL;
884 else if (*(NewToken->pName) == '!')
885 NewToken->Flags = SV_NEG_CONDITIONAL;
891 switch (NewToken->Flags) {
893 /* If we're able to find out more about the token, do it now while its fresh. */
894 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
895 HashHandler *Handler;
896 Handler = (HashHandler*) vVar;
897 if ((NewToken->nParameters < Handler->nMinArgs) ||
898 (NewToken->nParameters > Handler->nMaxArgs)) {
899 lprintf(1, "Handler [%s] (in '%s' line %ld); "
900 "doesn't work with %ld params [%s]\n",
902 ChrPtr(pTmpl->FileName),
904 NewToken->nParameters,
905 ChrPtr(NewToken->FlatToken));
908 NewToken->PreEval = Handler;
909 NewToken->Flags = SV_PREEVALUATED;
914 if (NewToken->nParameters !=1) {
915 lprintf(1, "Gettext (in '%s' line %ld); "
916 "requires exactly 1 parameter, you gave %ld params [%s]\n",
917 ChrPtr(pTmpl->FileName),
919 NewToken->nParameters,
920 ChrPtr(NewToken->FlatToken));
926 if (NewToken->nParameters != 1) {
927 lprintf(1, "Subtemplates (in '%s' line %ld); "
928 "require exactly 1 parameter, you gave %ld params [%s]\n",
929 ChrPtr(pTmpl->FileName),
931 NewToken->nParameters,
932 ChrPtr(NewToken->FlatToken));
936 case SV_CUST_STR_CONDITIONAL:
938 case SV_NEG_CONDITIONAL:
939 if (NewToken->Params[1]->lvalue == 0) {
940 lprintf(1, "Conditional (in '%s' line %ld); "
941 "Conditional ID mustn't be 0! [%s]\n",
942 ChrPtr(pTmpl->FileName),
944 ChrPtr(NewToken->FlatToken));
948 if (NewToken->nParameters <2) {
949 lprintf(1, "Conditional (in '%s' line %ld); "
950 "require at least 2 parameters, you gave %ld params [%s]\n",
951 ChrPtr(pTmpl->FileName),
953 NewToken->nParameters,
954 ChrPtr(NewToken->FlatToken));
958 if (!GetHash(Conditionals,
959 NewToken->Params[0]->Start,
960 NewToken->Params[0]->len,
963 if ((NewToken->Params[0]->len == 1) &&
964 (NewToken->Params[0]->Start[0] == 'X'))
966 lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n",
967 NewToken->Params[0]->Start,
968 ChrPtr(pTmpl->FileName),
970 ChrPtr(NewToken->FlatToken));
972 NewToken->Error = NewStrBuf();
975 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
976 NewToken->Params[0]->Start,
977 ChrPtr(pTmpl->FileName),
979 ChrPtr(NewToken->FlatToken));
983 NewToken->PreEval = vVar;
995 * \brief Display a variable-substituted template
996 * \param templatename template file to load
998 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1000 WCTemplate *NewTemplate;
1001 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1002 NewTemplate->Data = NULL;
1003 NewTemplate->FileName = NewStrBufDup(filename);
1004 NewTemplate->nTokensUsed = 0;
1005 NewTemplate->TokenSpace = 0;
1006 NewTemplate->Tokens = NULL;
1008 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1013 * \brief Display a variable-substituted template
1014 * \param templatename template file to load
1016 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1019 struct stat statbuf;
1020 const char *pS, *pE, *pch, *Err;
1023 WCTemplate *NewTemplate;
1025 fd = open(ChrPtr(filename), O_RDONLY);
1027 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1028 ChrPtr(filename), strerror(errno));
1032 if (fstat(fd, &statbuf) == -1) {
1033 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1034 ChrPtr(filename), strerror(errno));
1038 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1039 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1040 NewTemplate->FileName = NewStrBufDup(filename);
1041 NewTemplate->nTokensUsed = 0;
1042 NewTemplate->TokenSpace = 0;
1043 NewTemplate->Tokens = NULL;
1044 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1046 FreeWCTemplate(NewTemplate);
1047 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1048 ChrPtr(filename), strerror(errno));
1054 pS = pch = ChrPtr(NewTemplate->Data);
1055 pE = pS + StrLength(NewTemplate->Data);
1057 const char *pts, *pte;
1059 int InDoubleQuotes = 0;
1061 /** Find one <? > */
1063 for (; pch < pE; pch ++) {
1064 if ((*pch=='<')&&(*(pch + 1)=='?'))
1066 if (*pch=='\n') Line ++;
1072 /** Found one? parse it. */
1073 for (; pch < pE - 1; pch ++) {
1075 InDoubleQuotes = ! InDoubleQuotes;
1076 else if (*pch == '\'')
1077 InQuotes = ! InQuotes;
1078 else if ((!InQuotes && !InDoubleQuotes) &&
1079 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1087 PutNewToken(NewTemplate,
1088 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1091 if (LoadTemplates == 0)
1092 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1097 ///void PrintTemplate(const char *Key, void *vSubst, int odd)
1098 const char* PrintTemplate(void *vSubst)
1100 WCTemplate *Tmpl = vSubst;
1102 return ChrPtr(Tmpl->FileName);
1106 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
1111 DIR *filedir = NULL;
1112 struct dirent *filedir_entry;
1118 StrBufPrintf(Dir, "%s/t", DirName);
1119 filedir = opendir (ChrPtr(Dir));
1120 if (filedir == NULL) {
1125 FileName = NewStrBuf();
1127 while ((filedir_entry = readdir(filedir)))
1131 #ifdef _DIRENT_HAVE_D_NAMELEN
1132 d_namelen = filedir_entry->d_namelen;
1134 d_namelen = strlen(filedir_entry->d_name);
1136 d_without_ext = d_namelen;
1137 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1139 if ((d_without_ext == 0) || (d_namelen < 3))
1141 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1142 continue; /* Ignore backup files... */
1144 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1145 PStart = filedir_entry->d_name;
1146 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1147 MinorPtr = strchr(filedir_entry->d_name, '.');
1148 if (MinorPtr != NULL)
1150 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1152 if (LoadTemplates > 1)
1153 lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1154 if (LoadTemplates == 0)
1155 load_template(FileName, Tag, (IsMobile)?wireless:big);
1157 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1160 FreeStrBuf(&FileName);
1166 void InitTemplateCache(void)
1168 LoadTemplateDir(static_dirs[0],
1169 WirelessTemplateCache,
1171 LoadTemplateDir(static_dirs[1],
1172 WirelessLocalTemplateCache,
1173 LocalTemplateCache);
1178 /*-----------------------------------------------------------------------------
1179 * Filling & processing Templates
1182 * \brief executes one token
1183 * \param Target buffer to append to
1184 * \param Token da to process.
1185 * \param Template we're iterating
1186 * \param Context Contextpoointer to pass in
1187 * \param state are we in conditional state?
1188 * \param ContextType what type of information does context giv us?
1190 int EvaluateToken(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, void *Context, int state, int ContextType)
1192 HashHandler *Handler;
1194 // much output, since pName is not terminated...
1195 // lprintf(1,"Doing token: %s\n",Token->pName);
1197 switch (Tokens->Flags) {
1199 TmplGettext(Target, Tokens->nParameters, Tokens);
1201 case SV_CONDITIONAL: /** Forward conditional evaluation */
1202 return EvaluateConditional(Target, Tokens, pTmpl, Context, 1, state, ContextType);
1204 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1205 return EvaluateConditional(Target, Tokens, pTmpl, Context, 0, state, ContextType);
1207 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1208 if (Tokens->nParameters >= 6) {
1209 if (EvaluateConditional(Target, Tokens, pTmpl, Context, 0, state, ContextType))
1210 StrBufAppendBufPlain(Target,
1211 Tokens->Params[5]->Start,
1212 Tokens->Params[5]->len,
1215 StrBufAppendBufPlain(Target,
1216 Tokens->Params[4]->Start,
1217 Tokens->Params[4]->len,
1221 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs at least 6 Params![%s]\n",
1222 Tokens->Params[0]->Start,
1223 ChrPtr(pTmpl->FileName),
1225 ChrPtr(Tokens->FlatToken));
1228 "<pre>\nConditional [%s] (in '%s' line %ld); needs 6 Params!\n[%s]\n</pre>\n",
1229 Tokens->Params[0]->Start,
1230 ChrPtr(pTmpl->FileName),
1232 ChrPtr(Tokens->FlatToken));
1236 if (Tokens->nParameters == 1)
1237 DoTemplate(Tokens->Params[0]->Start, Tokens->Params[0]->len, NULL, NULL, ContextType);
1239 case SV_PREEVALUATED:
1240 Handler = (HashHandler*) Tokens->PreEval;
1241 if ((Handler->ContextRequired != CTX_NONE) &&
1242 (Handler->ContextRequired != ContextType)) {
1243 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1244 "requires context of type %ld, have %ld [%s]\n",
1246 ChrPtr(pTmpl->FileName),
1248 Handler->ContextRequired,
1250 ChrPtr(Tokens->FlatToken));
1253 "<pre>\nHandler [%s] (in '%s' line %ld);"
1254 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1256 ChrPtr(pTmpl->FileName),
1258 Handler->ContextRequired,
1260 ChrPtr(Tokens->FlatToken));
1264 Handler->HandlerFunc(Target,
1265 Tokens->nParameters,
1271 if (GetHash(GlobalNS, Tokens->pName, Tokens->NameEnd, &vVar)) {
1272 Handler = (HashHandler*) vVar;
1273 if ((Handler->ContextRequired != CTX_NONE) &&
1274 (Handler->ContextRequired != ContextType)) {
1275 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1276 "requires context of type %ld, have %ld [%s]\n",
1278 ChrPtr(pTmpl->FileName),
1280 Handler->ContextRequired,
1282 ChrPtr(Tokens->FlatToken));
1285 "<pre>\nHandler [%s] (in '%s' line %ld);"
1286 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1288 ChrPtr(pTmpl->FileName),
1290 Handler->ContextRequired,
1292 ChrPtr(Tokens->FlatToken));
1295 else if ((Tokens->nParameters < Handler->nMinArgs) ||
1296 (Tokens->nParameters > Handler->nMaxArgs)) {
1297 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1298 "doesn't work with %ld params [%s]\n",
1300 ChrPtr(pTmpl->FileName),
1302 Tokens->nParameters,
1303 ChrPtr(Tokens->FlatToken));
1306 "<pre>\nHandler [%s] (in '%s' line %ld);"
1307 " doesn't work with %ld params!\n[%s]\n</pre>\n",
1309 ChrPtr(pTmpl->FileName),
1311 Tokens->nParameters,
1312 ChrPtr(Tokens->FlatToken));
1315 Handler->HandlerFunc(Target,
1316 Tokens->nParameters,
1319 ContextType); /*TODO: subset of that */
1324 print_value_of(Target, Tokens, Context, ContextType);
1332 void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context, int ContextType)
1334 WCTemplate *pTmpl = Tmpl;
1337 const char *pData, *pS;
1340 if (LoadTemplates != 0) {
1341 if (LoadTemplates > 1)
1342 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1343 ChrPtr(Tmpl->FileName));
1345 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
1349 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1350 ChrPtr(Tmpl->FileName));
1356 pS = pData = ChrPtr(pTmpl->Data);
1357 len = StrLength(pTmpl->Data);
1361 if (i >= pTmpl->nTokensUsed) {
1362 StrBufAppendBufPlain(Target,
1364 len - (pData - pS), 0);
1368 StrBufAppendBufPlain(
1370 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1371 state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state, ContextType);
1372 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1373 /* condition told us to skip till its end condition */
1375 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1376 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1377 if (state == EvaluateConditional(
1382 pTmpl->Tokens[i]->Flags,
1388 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1389 if (i > pTmpl->nTokensUsed)
1393 if (LoadTemplates != 0) {
1394 FreeWCTemplate(pTmpl);
1399 * \brief Display a variable-substituted template
1400 * \param templatename template file to load
1402 void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Context, int ContextType)
1405 HashList *StaticLocal;
1410 if (WC->is_mobile) {
1411 Static = WirelessTemplateCache;
1412 StaticLocal = WirelessLocalTemplateCache;
1415 Static = TemplateCache;
1416 StaticLocal = LocalTemplateCache;
1421 lprintf (1, "Can't to load a template with empty name!\n");
1422 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1426 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1427 !GetHash(Static, templatename, len, &vTmpl)) {
1428 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1429 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1431 (long)strlen(templatename));
1432 /// dbg_PrintHash(Static, PrintTemplate, NULL);
1433 // PrintHash(Static, VarPrintTransition, PrintTemplate);
1438 ProcessTemplate(vTmpl, Target, Context, ContextType);
1441 /*-----------------------------------------------------------------------------
1444 typedef struct _HashIterator {
1445 HashList *StaticList;
1446 int AdditionalParams;
1448 int XPectContextType;
1449 RetrieveHashlistFunc GetHash;
1450 HashDestructorFunc Destructor;
1451 SubTemplFunc DoSubTemplate;
1454 void RegisterITERATOR(const char *Name, long len,
1455 int AdditionalParams,
1456 HashList *StaticList,
1457 RetrieveHashlistFunc GetHash,
1458 SubTemplFunc DoSubTempl,
1459 HashDestructorFunc Destructor,
1461 int XPectContextType)
1463 HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
1464 It->StaticList = StaticList;
1465 It->AdditionalParams = AdditionalParams;
1466 It->GetHash = GetHash;
1467 It->DoSubTemplate = DoSubTempl;
1468 It->Destructor = Destructor;
1469 It->ContextType = ContextType;
1470 It->XPectContextType = XPectContextType;
1471 Put(Iterators, Name, len, It, NULL);
1474 void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1486 if (!GetHash(Iterators,
1487 Tokens->Params[0]->Start,
1488 Tokens->Params[0]->len,
1490 lprintf(1, "unknown Iterator [%s] (in '%s' line %ld); "
1492 Tokens->Params[0]->Start,
1493 ChrPtr(Tokens->FileName),
1495 ChrPtr(Tokens->FlatToken));
1498 "<pre>\nunknown Iterator [%s] (in '%s' line %ld); \n"
1500 Tokens->Params[0]->Start,
1501 ChrPtr(Tokens->FileName),
1503 ChrPtr(Tokens->FlatToken));
1507 It = (HashIterator*) vIt;
1509 if (Tokens->nParameters < It->AdditionalParams + 2) {
1510 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1511 "doesn't work with %ld params [%s]\n",
1512 Tokens->Params[0]->Start,
1513 ChrPtr(Tokens->FileName),
1515 Tokens->nParameters,
1516 ChrPtr(Tokens->FlatToken));
1519 "<pre>Iterator [%s] \n(in '%s' line %ld);\n"
1520 "doesn't work with %ld params \n[%s]\n</pre>",
1521 Tokens->Params[0]->Start,
1522 ChrPtr(Tokens->FileName),
1524 Tokens->nParameters,
1525 ChrPtr(Tokens->FlatToken));
1529 if ((It->XPectContextType != CTX_NONE) &&
1530 (It->XPectContextType != ContextType)) {
1531 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1532 "requires context of type %ld, have %ld [%s]\n",
1534 ChrPtr(Tokens->FileName),
1536 It->XPectContextType,
1538 ChrPtr(Tokens->FlatToken));
1541 "<pre>\nIterator [%s] (in '%s' line %ld);"
1542 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1544 ChrPtr(Tokens->FileName),
1546 It->XPectContextType,
1548 ChrPtr(Tokens->FlatToken));
1553 if (It->StaticList == NULL)
1554 List = It->GetHash(Target, nArgs, Tokens, Context, ContextType);
1556 List = It->StaticList;
1558 SubBuf = NewStrBuf();
1559 it = GetNewHashPos(List, 0);
1560 while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
1561 svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s",
1562 (oddeven) ? "odd" : "even");
1563 svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
1565 if (It->DoSubTemplate != NULL)
1566 It->DoSubTemplate(SubBuf, vContext, Tokens);
1567 DoTemplate(Tokens->Params[1]->Start,
1568 Tokens->Params[1]->len,
1572 StrBufAppendBuf(Target, SubBuf, 0);
1573 FlushStrBuf(SubBuf);
1574 oddeven = ~ oddeven;
1576 FreeStrBuf(&SubBuf);
1578 if (It->Destructor != NULL)
1579 It->Destructor(&List);
1584 /*-----------------------------------------------------------------------------
1587 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType)
1589 ConditionalStruct *Cond;
1591 if ((Tokens->Params[0]->len == 1) &&
1592 (Tokens->Params[0]->Start[0] == 'X'))
1593 return (state != 0)?Tokens->Params[1]->lvalue:0;
1595 Cond = (ConditionalStruct *) Tokens->PreEval;
1597 lprintf(1, "Conditional [%s] (in '%s' line %ld); unknown![%s]\n",
1598 Tokens->Params[0]->Start,
1599 ChrPtr(pTmpl->FileName),
1601 ChrPtr(Tokens->FlatToken));
1605 if (Tokens->nParameters < Cond->nParams) {
1606 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n",
1607 Tokens->Params[0]->Start,
1608 ChrPtr(pTmpl->FileName),
1611 ChrPtr(Tokens->FlatToken));
1614 "<pre>\nConditional [%s] (in '%s' line %ld); needs %ld Params!\n[%s]\n</pre>\n",
1615 Tokens->Params[0]->Start,
1616 ChrPtr(pTmpl->FileName),
1619 ChrPtr(Tokens->FlatToken));
1622 if (Cond->CondF(Tokens, Context, ContextType) == Neg)
1623 return Tokens->Params[1]->lvalue;
1627 int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
1632 if (!GetHash(WC->vars,
1633 Tokens->Params[2]->Start,
1634 Tokens->Params[2]->len,
1637 subst = (wcsubst*) vsubst;
1638 if ((subst->ContextRequired != CTX_NONE) &&
1639 (subst->ContextRequired != ContextType)) {
1640 lprintf(1," WARNING: Conditional requires Context: [%ld]!\n", Tokens->Params[2]->Start);
1644 switch(subst->wcs_type) {
1646 return (subst->wcs_function!=NULL);
1648 lprintf(1, " -> Server [%s]\n", subst->wcs_value);////todo
1652 case WCS_STRBUF_REF:
1653 if (Tokens->nParameters < 4)
1655 return (strcmp(Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
1657 if (Tokens->nParameters < 4)
1658 return (subst->lvalue != 0);
1659 return (subst->lvalue == Tokens->Params[3]->lvalue);
1661 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
1668 void RegisterConditional(const char *Name, long len,
1670 WCConditionalFunc CondF,
1671 int ContextRequired)
1673 ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
1674 Cond->PlainName = Name;
1675 Cond->nParams = nParams;
1676 Cond->CondF = CondF;
1677 Cond->ContextRequired = ContextRequired;
1678 Put(Conditionals, Name, len, Cond, NULL);
1681 /*-----------------------------------------------------------------------------
1684 void tmplput_ContextString(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1686 StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, (StrBuf*)Context, 0);
1688 int ConditionalContextStr(WCTemplateToken *Tokens, void *Context, int ContextType)
1690 StrBuf *TokenText = (StrBuf*) Context;
1691 const char *CompareToken;
1694 GetTemplateTokenString(Tokens, 2, &CompareToken, &len);
1695 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
1698 /*-----------------------------------------------------------------------------
1702 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1706 if (Tokens->Params[1]->Type == TYPE_STR) {
1707 Headline = NewStrBuf();
1708 DoTemplate(Tokens->Params[1]->Start,
1709 Tokens->Params[1]->len,
1717 GetTemplateTokenString(Tokens,
1721 Headline = NewStrBufPlain(Ch, len);
1725 DoTemplate(HKEY("beginbox"), Target, Headline, CTX_STRBUF);
1726 DoTemplate(Tokens->Params[0]->Start,
1727 Tokens->Params[0]->len,
1731 DoTemplate(HKEY("endbox"), Target, Context, ContextType);
1732 FreeStrBuf(&Headline);
1735 /*-----------------------------------------------------------------------------
1739 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1742 int i, ntabs, nTabs;
1744 nTabs = ntabs = Tokens->nParameters / 2;
1745 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
1747 for (i = 0; i < ntabs; i++) {
1748 TabNames[i] = NewStrBuf();
1749 if (Tokens->Params[i * 2]->len > 0) {
1750 DoTemplate(Tokens->Params[i * 2]->Start,
1751 Tokens->Params[i * 2]->len,
1757 /** A Tab without subject? we can't count that, add it as silent */
1762 StrTabbedDialog(Target, nTabs, TabNames);
1763 for (i = 0; i < ntabs; i++) {
1764 StrBeginTab(Target, i, nTabs);
1766 DoTemplate(Tokens->Params[i * 2 + 1]->Start,
1767 Tokens->Params[i * 2 + 1]->len,
1771 StrEndTab(Target, i, nTabs);
1776 /*-----------------------------------------------------------------------------
1780 typedef struct _SortStruct {
1782 StrBuf *PrefPrepend;
1783 CompareFunc Forward;
1784 CompareFunc Reverse;
1789 void DestroySortStruct(void *vSort)
1791 SortStruct *Sort = (SortStruct*) vSort;
1792 FreeStrBuf(&Sort->Name);
1793 FreeStrBuf(&Sort->PrefPrepend);
1797 void RegisterSortFunc(const char *name, long len,
1798 const char *prepend, long preplen,
1799 CompareFunc Forward,
1800 CompareFunc Reverse,
1803 SortStruct *NewSort = (SortStruct*) malloc(sizeof(SortStruct));
1804 NewSort->Name = NewStrBufPlain(name, len);
1805 if (prepend != NULL)
1806 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
1808 NewSort->PrefPrepend = NULL;
1809 NewSort->Forward = Forward;
1810 NewSort->Reverse = Reverse;
1811 NewSort->ContextType = ContextType;
1812 Put(SortHash, name, len, NewSort, DestroySortStruct);
1815 CompareFunc RetrieveSort(long ContextType, const char *OtherPrefix,
1816 const char *Default, long ldefault, long DefaultDirection)
1819 const StrBuf *BSort;
1824 if (havebstr("SortBy")) {
1825 BSort = sbstr("SortBy");
1827 else { /** Try to fallback to our remembered values... */
1828 if (OtherPrefix == NULL) {
1829 BSort = get_room_pref("sort");
1832 ////todo: nail prefprepend to sort, and lookup this!
1836 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
1837 (vSortBy == NULL)) {
1839 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
1840 (vSortBy == NULL)) {
1841 lprintf(1, "Illegal default sort: [%s]\n", Default);
1845 SortBy = (SortStruct*)vSortBy;
1847 /** Ok, its us, lets see in which direction we should sort... */
1848 if (havebstr("SortOrder")) {
1849 SortOrder = LBSTR("SortOrder");
1851 else { /** Try to fallback to our remembered values... */
1852 if (SortBy->PrefPrepend == NULL) {
1853 SortOrder = StrTol(get_room_pref("SortOrder"));
1856 ////todo: nail prefprepend to sort, and lookup this!
1859 switch (SortOrder) {
1864 return SortBy->Forward;
1866 return SortBy->Reverse;
1878 ConstStr SortIcons[] = {
1879 {HKEY("static/sort_none.gif")},
1880 {HKEY("static/up_pointer.gif")},
1881 {HKEY("static/down_pointer.gif")},
1884 ConstStr SortNextOrder[] = {
1891 int GetSortMetric(WCTemplateToken *Tokens, SortStruct **Next, SortStruct **Param, long *SortOrder)
1893 int bSortError = eNOT_SPECIFIED;
1894 const StrBuf *BSort;
1899 if (!GetHash(SortHash, Tokens->Params[0]->Start, Tokens->Params[0]->len, &vSort) ||
1901 return eNO_SUCH_SORT;
1902 *Param = (SortStruct*) vSort;
1905 if (havebstr("SortBy")) {
1906 BSort = sbstr("SortBy");
1907 bSortError = eINVALID_PARAM;
1909 else { /** Try to fallback to our remembered values... */
1910 if ((*Param)->PrefPrepend == NULL) {
1911 BSort = get_room_pref("sort");
1914 ////todo: nail prefprepend to sort, and lookup this!
1918 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
1922 *Next = (SortStruct*) vSort;
1924 /** Ok, its us, lets see in which direction we should sort... */
1925 if (havebstr("SortOrder")) {
1926 *SortOrder = LBSTR("SortOrder");
1928 else { /** Try to fallback to our remembered values... */
1929 if ((*Param)->PrefPrepend == NULL) {
1930 *SortOrder = StrTol(get_room_pref("SortOrder"));
1933 ////todo: nail prefprepend to sort, and lookup this!
1943 void tmplput_SORT_ICON(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1948 const ConstStr *SortIcon;
1950 switch (GetSortMetric(Tokens, &Next, &Param, &SortOrder)){
1952 lprintf(1, "[%s] (in '%s' line %ld); "
1953 " Sorter [%s] unknown! [%s]\n",
1955 ChrPtr(Tokens->FileName),
1957 Tokens->Params[0]->Start,
1958 ChrPtr(Tokens->FlatToken));
1961 "<pre>\n [%s] (in '%s' line %ld);"
1962 " Sorter [%s] unknown!\n[%s]\n</pre>\n",
1964 ChrPtr(Tokens->FileName),
1966 Tokens->Params[0]->Start,
1967 ChrPtr(Tokens->FlatToken));
1969 case eINVALID_PARAM:
1970 lprintf(1, "[%s] (in '%s' line %ld); "
1971 " Sorter specified by BSTR 'SortBy' [%s] unknown! [%s]\n",
1973 ChrPtr(Tokens->FileName),
1976 ChrPtr(Tokens->FlatToken));
1977 case eNOT_SPECIFIED:
1979 if (Next == Param) {
1980 SortIcon = &SortIcons[SortOrder];
1982 else { /** Not Us... */
1983 SortIcon = &SortIcons[0];
1985 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
1989 void tmplput_SORT_NEXT(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1995 switch (GetSortMetric(Tokens, &Next, &Param, &SortOrder)){
1997 lprintf(1, "[%s] (in '%s' line %ld); "
1998 " Sorter [%s] unknown! [%s]\n",
2000 ChrPtr(Tokens->FileName),
2002 Tokens->Params[0]->Start,
2003 ChrPtr(Tokens->FlatToken));
2006 "<pre>\n [%s] (in '%s' line %ld);"
2007 " Sorter [%s] unknown!\n[%s]\n</pre>\n",
2009 ChrPtr(Tokens->FileName),
2011 Tokens->Params[0]->Start,
2012 ChrPtr(Tokens->FlatToken));
2014 case eINVALID_PARAM:
2015 lprintf(1, "[%s] (in '%s' line %ld); "
2016 " Sorter specified by BSTR 'SortBy' [%s] unknown! [%s]\n",
2018 ChrPtr(Tokens->FileName),
2021 ChrPtr(Tokens->FlatToken));
2023 case eNOT_SPECIFIED:
2025 StrBufAppendBuf(Target, Param->Name, 0);
2030 void tmplput_SORT_ORDER(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
2033 const ConstStr *SortOrderStr;
2037 switch (GetSortMetric(Tokens, &Next, &Param, &SortOrder)){
2039 lprintf(1, "[%s] (in '%s' line %ld); "
2040 " Sorter [%s] unknown! [%s]\n",
2042 ChrPtr(Tokens->FileName),
2044 Tokens->Params[0]->Start,
2045 ChrPtr(Tokens->FlatToken));
2048 "<pre>\n [%s] (in '%s' line %ld);"
2049 " Sorter [%s] unknown!\n[%s]\n</pre>\n",
2051 ChrPtr(Tokens->FileName),
2053 Tokens->Params[0]->Start,
2054 ChrPtr(Tokens->FlatToken));
2056 case eINVALID_PARAM:
2057 lprintf(1, "[%s] (in '%s' line %ld); "
2058 " Sorter specified by BSTR 'SortBy' [%s] unknown! [%s]\n",
2060 ChrPtr(Tokens->FileName),
2063 ChrPtr(Tokens->FlatToken));
2065 case eNOT_SPECIFIED:
2067 if (Next == Param) {
2068 SortOrderStr = &SortNextOrder[SortOrder];
2070 else { /** Not Us... */
2071 SortOrderStr = &SortNextOrder[0];
2073 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2078 void tmplput_long_vector(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
2080 long *LongVector = (long*) Context;
2082 if ((Tokens->Params[0]->Type == TYPE_LONG) &&
2083 (Tokens->Params[0]->lvalue <= LongVector[0]))
2085 StrBufAppendPrintf(Target, "%ld", LongVector[Tokens->Params[0]->lvalue]);
2089 if (Tokens->Params[0]->Type == TYPE_LONG) {
2090 lprintf(1, "longvector [%s] (in '%s' line %ld); needs a long Parameter![%s]\n",
2091 Tokens->Params[0]->Start,
2092 ChrPtr(Tokens->FileName),
2094 ChrPtr(Tokens->FlatToken));
2097 "<pre>\nlongvector [%s] (in '%s' line %ld); needs a numerical Parameter!\n[%s]\n</pre>\n",
2098 Tokens->Params[0]->Start,
2099 ChrPtr(Tokens->FileName),
2101 ChrPtr(Tokens->FlatToken));
2104 lprintf(1, "longvector [%s] (in '%s' line %ld); doesn't have %ld Parameters,"
2105 " its just the size of %ld![%s]\n",
2106 Tokens->Params[0]->Start,
2107 ChrPtr(Tokens->FileName),
2109 Tokens->Params[0]->lvalue,
2111 ChrPtr(Tokens->FlatToken));
2114 "<pre>\nlongvector [%s] (in '%s' line %ld); doesn't have %ld Parameters,"
2115 " its just the size of %ld!\n[%s]\n</pre>\n",
2116 Tokens->Params[0]->Start,
2117 ChrPtr(Tokens->FileName),
2119 Tokens->Params[0]->lvalue,
2121 ChrPtr(Tokens->FlatToken));
2128 int ConditionalLongVector(WCTemplateToken *Tokens, void *Context, int ContextType)
2130 long *LongVector = (long*) Context;
2132 if ((Tokens->Params[2]->Type == TYPE_LONG) &&
2133 (Tokens->Params[2]->lvalue <= LongVector[0])&&
2134 (Tokens->Params[3]->Type == TYPE_LONG) &&
2135 (Tokens->Params[3]->lvalue <= LongVector[0]))
2137 return LongVector[Tokens->Params[2]->lvalue] == LongVector[Tokens->Params[3]->lvalue];
2141 if ((Tokens->Params[2]->Type == TYPE_LONG) ||
2142 (Tokens->Params[2]->Type == TYPE_LONG)) {
2143 lprintf(1, "ConditionalLongVector [%s] (in '%s' line %ld); needs two long Parameter![%s]\n",
2144 Tokens->Params[0]->Start,
2145 ChrPtr(Tokens->FileName),
2147 ChrPtr(Tokens->FlatToken));
2150 lprintf(1, "longvector [%s] (in '%s' line %ld); doesn't have %ld / %ld Parameters,"
2151 " its just the size of %ld![%s]\n",
2152 Tokens->Params[0]->Start,
2153 ChrPtr(Tokens->FileName),
2155 Tokens->Params[2]->lvalue,
2156 Tokens->Params[3]->lvalue,
2158 ChrPtr(Tokens->FlatToken));
2168 RegisterNamespace("SORT:ICON", 1, 1, tmplput_SORT_ICON, CTX_NONE);
2169 RegisterNamespace("SORT:ORDER", 1, 1, tmplput_SORT_ORDER, CTX_NONE);
2170 RegisterNamespace("SORT:NEXT", 1, 1, tmplput_SORT_NEXT, CTX_NONE);
2171 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, CTX_STRBUF);
2172 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, CTX_NONE);
2173 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, CTX_NONE);
2174 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, CTX_NONE);
2175 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, CTX_LONGVECTOR);
2176 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
2177 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
2178 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);