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(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)
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,...)
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,...)
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)
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)
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)
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)
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(TKEY(N));
489 *Value = ChrPtr(Buf);
490 *len = StrLength(Buf);
493 get_PREFERENCE(TKEY(N), &Buf);
494 *Value = ChrPtr(Buf);
495 *len = StrLength(Buf);
499 break; ///todo: string to text?
501 *Value = _(Tokens->Params[N]->Start);
502 *len = strlen(*Value);
513 * \brief Print the value of a variable
514 * \param keyname get a key to print
516 void print_value_of(StrBuf *Target, WCTemplateToken *Tokens, void *Context, int ContextType) {
521 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
522 /// TODO: debricated!
523 if (Tokens->pName[0] == '=') {
524 DoTemplate(Tokens->pName+1, Tokens->NameEnd - 1, NULL, NULL, 0);
527 //////TODO: if param[1] == "U" -> urlescape
529 /** Page-local variables */
530 if ((WCC->vars!= NULL) && GetHash(WCC->vars, Tokens->pName, Tokens->NameEnd, &vVar)) {
531 ptr = (wcsubst*) vVar;
532 switch(ptr->wcs_type) {
534 StrBufAppendBuf(Target, ptr->wcs_value, 0);
537 pvo_do_cmd(Target, ptr->wcs_value);
540 (*ptr->wcs_function) (Target, Tokens->nParameters, Tokens, Context, ContextType);
544 StrBufAppendBuf(Target, ptr->wcs_value, 0);
547 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
550 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n", Tokens->pName);
551 StrBufAppendPrintf(Target, "<pre>WARNING: \ninvalid value in SV-Hash at %s!\n</pre>", Tokens->pName);
555 lprintf(1, "didn't find Handler [%s] (in '%s' line %ld); "
558 ChrPtr(Tokens->FileName),
560 ChrPtr(Tokens->FlatToken));
565 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
571 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
572 ParamToLookup->len, &vVar)) {
573 ptr = (wcsubst*) vVar;
574 switch(ptr->wcs_type) {
578 if (ParamToCompare->Type == TYPE_STR)
579 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
580 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
582 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
590 if (ParamToCompare->Type == TYPE_STR)
593 return ParamToCompare->lvalue == ptr->lvalue;
596 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
597 ParamToLookup->Start);
603 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
609 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
610 ParamToLookup->len, &vVar)) {
611 ptr = (wcsubst*) vVar;
612 switch(ptr->wcs_type) {
616 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
617 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
624 return StrTol(Compare) == ptr->lvalue;
626 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
627 ParamToLookup->Start);
636 * \brief puts string into the template and computes which escape methon we should use
637 * \param Source the string we should put into the template
638 * \param FormatTypeIndex where should we look for escape types if?
640 void StrBufAppendTemplate(StrBuf *Target,
642 WCTemplateToken *Tokens,
643 void *Context, int ContextType,
644 const StrBuf *Source, int FormatTypeIndex)
650 if ((FormatTypeIndex < Tokens->nParameters) &&
651 (Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
652 (Tokens->Params[FormatTypeIndex]->len == 1)) {
653 EscapeAs = *Tokens->Params[FormatTypeIndex]->Start;
660 Buf = NewStrBufPlain(NULL, StrLength(Source));
661 StrBuf_RFC822_to_Utf8(Buf,
663 (WCC!=NULL)? WCC->DefaultCharset : NULL,
665 StrEscAppend(Target, Buf, NULL, 0, 0);
669 StrEscAppend(Target, Source, NULL, 0, 0);
672 StrBufAppendBuf(Target, Source, 0);
677 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
679 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
680 if (Template->TokenSpace <= 0) {
681 Template->Tokens = (WCTemplateToken**)malloc(
682 sizeof(WCTemplateToken*) * 10);
683 Template->TokenSpace = 10;
686 WCTemplateToken **NewTokens;
687 NewTokens= (WCTemplateToken**)malloc(
688 sizeof(WCTemplateToken*) *
689 Template->TokenSpace * 2);
690 memcpy(NewTokens, Template->Tokens,
691 sizeof(WCTemplateToken*) * Template->nTokensUsed);
692 free(Template->Tokens);
693 Template->TokenSpace *= 2;
694 Template->Tokens = NewTokens;
697 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
700 TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, WCTemplateToken *Tokens, WCTemplate *pTmpl)
702 const char *pch = *pCh;
703 const char *pchs, *pche;
704 TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
708 Parm->Type = TYPE_STR;
710 /* Skip leading whitespaces */
711 while ((*pch == ' ' )||
714 (*pch == '\n')) pch ++;
717 Parm->Type = TYPE_PREFSTR;
720 else if (*pch == ';') {
721 Parm->Type = TYPE_PREFINT;
724 else if (*pch == '_') {
725 Parm->Type = TYPE_GETTEXT;
728 else if (*pch == 'B') {
729 Parm->Type = TYPE_BSTR;
736 else if (*pch == '\'')
743 ( (pch > pchs) && (*(pch - 1) == '\\'))
749 lprintf(1, "Error (in '%s' line %ld); "
750 "evaluating template param [%s] in Token [%s]\n",
751 ChrPtr(pTmpl->FileName),
753 ChrPtr(Tokens->FlatToken),
760 StrBufPeek(Buf, pch, -1, '\0');
761 if (LoadTemplates > 1) {
762 lprintf(1, "DBG: got param [%s] %ld %ld\n",
763 pchs, pche - pchs, strlen(pchs));
766 Parm->len = pche - pchs;
767 pch ++; /* move after trailing quote */
771 Parm->Type = TYPE_LONG;
773 while ((pch <= pe) &&
780 StrBufPeek(Buf, pch, -1, '\0');
781 Parm->lvalue = atol(pchs);
787 lprintf(1, "Error (in '%s' line %ld); "
788 "evaluating long template param [%s] in Token [%s]\n",
789 ChrPtr(pTmpl->FileName),
791 ChrPtr(Tokens->FlatToken),
797 while ((*pch == ' ' )||
801 (*pch == '\n')) pch ++;
807 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
809 const char *pTmplStart,
810 const char *pTmplEnd,
816 TemplateParam *Param;
817 WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
819 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
821 NewToken->Line = Line + 1;
822 NewToken->pTokenStart = pTmplStart;
823 NewToken->TokenStart = pTmplStart - pStart;
824 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
825 NewToken->pTokenEnd = pTmplEnd;
826 NewToken->NameEnd = NewToken->TokenEnd - 2;
827 NewToken->PreEval = NULL;
828 NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
830 StrBufPeek(Buf, pTmplStart, + 1, '\0');
831 StrBufPeek(Buf, pTmplEnd, -1, '\0');
832 pch = NewToken->pName = pTmplStart + 2;
834 NewToken->HaveParameters = 0;;
835 NewToken->nParameters = 0;
837 while (pch < pTmplEnd - 1) {
839 StrBufPeek(Buf, pch, -1, '\0');
840 NewToken->NameEnd = pch - NewToken->pName;
842 if (*(pTmplEnd - 1) != ')') {
843 lprintf(1, "Warning, Non welformed Token; missing right parenthesis (in '%s' line %ld); "
845 ChrPtr(pTmpl->FileName),
847 ChrPtr(NewToken->FlatToken));
849 while (pch < pTmplEnd - 1) {
850 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
852 NewToken->HaveParameters = 1;
853 if (NewToken->nParameters > MAXPARAM) {
854 lprintf(1, "Error (in '%s' line %ld); "
855 "only [%ld] Params allowed in Tokens [%s]\n",
856 ChrPtr(pTmpl->FileName),
859 ChrPtr(NewToken->FlatToken));
861 FreeToken(&NewToken);
864 NewToken->Params[NewToken->nParameters++] = Param;
868 if((NewToken->NameEnd == 1) &&
869 (NewToken->HaveParameters == 1))
872 if (*(NewToken->pName) == '_')
873 NewToken->Flags = SV_GETTEXT;
874 else if (*(NewToken->pName) == '=')
875 NewToken->Flags = SV_SUBTEMPL;
876 else if (*(NewToken->pName) == '%')
877 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
878 else if (*(NewToken->pName) == '?')
879 NewToken->Flags = SV_CONDITIONAL;
880 else if (*(NewToken->pName) == '!')
881 NewToken->Flags = SV_NEG_CONDITIONAL;
887 switch (NewToken->Flags) {
889 /* If we're able to find out more about the token, do it now while its fresh. */
890 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
891 HashHandler *Handler;
892 Handler = (HashHandler*) vVar;
893 if ((NewToken->nParameters < Handler->nMinArgs) ||
894 (NewToken->nParameters > Handler->nMaxArgs)) {
895 lprintf(1, "Handler [%s] (in '%s' line %ld); "
896 "doesn't work with %ld params [%s]\n",
898 ChrPtr(pTmpl->FileName),
900 NewToken->nParameters,
901 ChrPtr(NewToken->FlatToken));
904 NewToken->PreEval = Handler;
905 NewToken->Flags = SV_PREEVALUATED;
910 if (NewToken->nParameters !=1) {
911 lprintf(1, "Gettext (in '%s' line %ld); "
912 "requires exactly 1 parameter, you gave %ld params [%s]\n",
913 ChrPtr(pTmpl->FileName),
915 NewToken->nParameters,
916 ChrPtr(NewToken->FlatToken));
922 if (NewToken->nParameters != 1) {
923 lprintf(1, "Subtemplates (in '%s' line %ld); "
924 "require exactly 1 parameter, you gave %ld params [%s]\n",
925 ChrPtr(pTmpl->FileName),
927 NewToken->nParameters,
928 ChrPtr(NewToken->FlatToken));
932 case SV_CUST_STR_CONDITIONAL:
934 case SV_NEG_CONDITIONAL:
935 if (NewToken->nParameters <2) {
936 lprintf(1, "Conditional (in '%s' line %ld); "
937 "require at least 2 parameters, you gave %ld params [%s]\n",
938 ChrPtr(pTmpl->FileName),
940 NewToken->nParameters,
941 ChrPtr(NewToken->FlatToken));
945 if (NewToken->Params[1]->lvalue == 0) {
946 lprintf(1, "Conditional (in '%s' line %ld); "
947 "Conditional ID mustn't be 0! [%s]\n",
948 ChrPtr(pTmpl->FileName),
950 ChrPtr(NewToken->FlatToken));
954 if (!GetHash(Conditionals,
955 NewToken->Params[0]->Start,
956 NewToken->Params[0]->len,
959 if ((NewToken->Params[0]->len == 1) &&
960 (NewToken->Params[0]->Start[0] == 'X'))
962 lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n",
963 NewToken->Params[0]->Start,
964 ChrPtr(pTmpl->FileName),
966 ChrPtr(NewToken->FlatToken));
968 NewToken->Error = NewStrBuf();
971 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
972 NewToken->Params[0]->Start,
973 ChrPtr(pTmpl->FileName),
975 ChrPtr(NewToken->FlatToken));
979 NewToken->PreEval = vVar;
991 * \brief Display a variable-substituted template
992 * \param templatename template file to load
994 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
996 WCTemplate *NewTemplate;
997 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
998 NewTemplate->Data = NULL;
999 NewTemplate->FileName = NewStrBufDup(filename);
1000 NewTemplate->nTokensUsed = 0;
1001 NewTemplate->TokenSpace = 0;
1002 NewTemplate->Tokens = NULL;
1004 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1009 * \brief Display a variable-substituted template
1010 * \param templatename template file to load
1012 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1015 struct stat statbuf;
1016 const char *pS, *pE, *pch, *Err;
1019 WCTemplate *NewTemplate;
1021 fd = open(ChrPtr(filename), O_RDONLY);
1023 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1024 ChrPtr(filename), strerror(errno));
1028 if (fstat(fd, &statbuf) == -1) {
1029 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1030 ChrPtr(filename), strerror(errno));
1034 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1035 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1036 NewTemplate->FileName = NewStrBufDup(filename);
1037 NewTemplate->nTokensUsed = 0;
1038 NewTemplate->TokenSpace = 0;
1039 NewTemplate->Tokens = NULL;
1040 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1042 FreeWCTemplate(NewTemplate);
1043 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1044 ChrPtr(filename), strerror(errno));
1050 pS = pch = ChrPtr(NewTemplate->Data);
1051 pE = pS + StrLength(NewTemplate->Data);
1053 const char *pts, *pte;
1055 int InDoubleQuotes = 0;
1057 /** Find one <? > */
1059 for (; pch < pE; pch ++) {
1060 if ((*pch=='<')&&(*(pch + 1)=='?'))
1062 if (*pch=='\n') Line ++;
1068 /** Found one? parse it. */
1069 for (; pch <= pE - 1; pch ++) {
1071 InDoubleQuotes = ! InDoubleQuotes;
1072 else if (*pch == '\'')
1073 InQuotes = ! InQuotes;
1074 else if ((!InQuotes && !InDoubleQuotes) &&
1075 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1083 PutNewToken(NewTemplate,
1084 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1087 if (LoadTemplates == 0)
1088 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1093 ///void PrintTemplate(const char *Key, void *vSubst, int odd)
1094 const char* PrintTemplate(void *vSubst)
1096 WCTemplate *Tmpl = vSubst;
1098 return ChrPtr(Tmpl->FileName);
1102 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
1107 DIR *filedir = NULL;
1108 struct dirent *filedir_entry;
1114 StrBufPrintf(Dir, "%s/t", DirName);
1115 filedir = opendir (ChrPtr(Dir));
1116 if (filedir == NULL) {
1121 FileName = NewStrBuf();
1123 while ((filedir_entry = readdir(filedir)))
1127 #ifdef _DIRENT_HAVE_D_NAMELEN
1128 d_namelen = filedir_entry->d_namelen;
1130 d_namelen = strlen(filedir_entry->d_name);
1132 d_without_ext = d_namelen;
1133 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1135 if ((d_without_ext == 0) || (d_namelen < 3))
1137 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1138 continue; /* Ignore backup files... */
1140 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1141 PStart = filedir_entry->d_name;
1142 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1143 MinorPtr = strchr(filedir_entry->d_name, '.');
1144 if (MinorPtr != NULL)
1146 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1148 if (LoadTemplates > 1)
1149 lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1150 if (LoadTemplates == 0)
1151 load_template(FileName, Tag, (IsMobile)?wireless:big);
1153 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1156 FreeStrBuf(&FileName);
1162 void InitTemplateCache(void)
1164 LoadTemplateDir(static_dirs[0],
1165 WirelessTemplateCache,
1167 LoadTemplateDir(static_dirs[1],
1168 WirelessLocalTemplateCache,
1169 LocalTemplateCache);
1174 /*-----------------------------------------------------------------------------
1175 * Filling & processing Templates
1178 * \brief executes one token
1179 * \param Target buffer to append to
1180 * \param Token da to process.
1181 * \param Template we're iterating
1182 * \param Context Contextpoointer to pass in
1183 * \param state are we in conditional state?
1184 * \param ContextType what type of information does context giv us?
1186 int EvaluateToken(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, void *Context, int state, int ContextType)
1188 const char *AppendMe;
1190 HashHandler *Handler;
1192 // much output, since pName is not terminated...
1193 // lprintf(1,"Doing token: %s\n",Token->pName);
1195 switch (Tokens->Flags) {
1197 TmplGettext(Target, Tokens->nParameters, Tokens);
1199 case SV_CONDITIONAL: /** Forward conditional evaluation */
1200 return EvaluateConditional(Target, Tokens, pTmpl, Context, 1, state, ContextType);
1202 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1203 return EvaluateConditional(Target, Tokens, pTmpl, Context, 0, state, ContextType);
1205 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1206 if (Tokens->nParameters >= 6) {
1207 if (EvaluateConditional(Target, Tokens, pTmpl, Context, 0, state, ContextType)) {
1208 GetTemplateTokenString(Tokens, 5, &AppendMe, &AppendMeLen);
1209 StrBufAppendBufPlain(Target,
1215 GetTemplateTokenString(Tokens, 4, &AppendMe, &AppendMeLen);
1216 StrBufAppendBufPlain(Target,
1223 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs at least 6 Params![%s]\n",
1224 Tokens->Params[0]->Start,
1225 ChrPtr(pTmpl->FileName),
1227 ChrPtr(Tokens->FlatToken));
1230 "<pre>\nConditional [%s] (in '%s' line %ld); needs 6 Params!\n[%s]\n</pre>\n",
1231 Tokens->Params[0]->Start,
1232 ChrPtr(pTmpl->FileName),
1234 ChrPtr(Tokens->FlatToken));
1238 if (Tokens->nParameters == 1)
1239 DoTemplate(TKEY(0), NULL, NULL, ContextType);
1241 case SV_PREEVALUATED:
1242 Handler = (HashHandler*) Tokens->PreEval;
1243 if ((Handler->ContextRequired != CTX_NONE) &&
1244 (Handler->ContextRequired != ContextType)) {
1245 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1246 "requires context of type %ld, have %ld [%s]\n",
1248 ChrPtr(pTmpl->FileName),
1250 Handler->ContextRequired,
1252 ChrPtr(Tokens->FlatToken));
1255 "<pre>\nHandler [%s] (in '%s' line %ld);"
1256 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1258 ChrPtr(pTmpl->FileName),
1260 Handler->ContextRequired,
1262 ChrPtr(Tokens->FlatToken));
1266 Handler->HandlerFunc(Target,
1267 Tokens->nParameters,
1273 if (GetHash(GlobalNS, Tokens->pName, Tokens->NameEnd, &vVar)) {
1274 Handler = (HashHandler*) vVar;
1275 if ((Handler->ContextRequired != CTX_NONE) &&
1276 (Handler->ContextRequired != ContextType)) {
1277 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1278 "requires context of type %ld, have %ld [%s]\n",
1280 ChrPtr(pTmpl->FileName),
1282 Handler->ContextRequired,
1284 ChrPtr(Tokens->FlatToken));
1287 "<pre>\nHandler [%s] (in '%s' line %ld);"
1288 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1290 ChrPtr(pTmpl->FileName),
1292 Handler->ContextRequired,
1294 ChrPtr(Tokens->FlatToken));
1297 else if ((Tokens->nParameters < Handler->nMinArgs) ||
1298 (Tokens->nParameters > Handler->nMaxArgs)) {
1299 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1300 "doesn't work with %ld params [%s]\n",
1302 ChrPtr(pTmpl->FileName),
1304 Tokens->nParameters,
1305 ChrPtr(Tokens->FlatToken));
1308 "<pre>\nHandler [%s] (in '%s' line %ld);"
1309 " doesn't work with %ld params!\n[%s]\n</pre>\n",
1311 ChrPtr(pTmpl->FileName),
1313 Tokens->nParameters,
1314 ChrPtr(Tokens->FlatToken));
1317 Handler->HandlerFunc(Target,
1318 Tokens->nParameters,
1321 ContextType); /*TODO: subset of that */
1326 print_value_of(Target, Tokens, Context, ContextType);
1334 void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context, int ContextType)
1336 WCTemplate *pTmpl = Tmpl;
1339 const char *pData, *pS;
1342 if (LoadTemplates != 0) {
1343 if (LoadTemplates > 1)
1344 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1345 ChrPtr(Tmpl->FileName));
1347 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
1351 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1352 ChrPtr(Tmpl->FileName));
1358 pS = pData = ChrPtr(pTmpl->Data);
1359 len = StrLength(pTmpl->Data);
1363 if (i >= pTmpl->nTokensUsed) {
1364 StrBufAppendBufPlain(Target,
1366 len - (pData - pS), 0);
1370 StrBufAppendBufPlain(
1372 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1373 state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state, ContextType);
1374 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1375 /* condition told us to skip till its end condition */
1377 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1378 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1379 if (state == EvaluateConditional(
1384 pTmpl->Tokens[i]->Flags,
1390 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1391 if (i > pTmpl->nTokensUsed)
1395 if (LoadTemplates != 0) {
1396 FreeWCTemplate(pTmpl);
1401 * \brief Display a variable-substituted template
1402 * \param templatename template file to load
1404 void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Context, int ContextType)
1407 HashList *StaticLocal;
1412 if (WC->is_mobile) {
1413 Static = WirelessTemplateCache;
1414 StaticLocal = WirelessLocalTemplateCache;
1417 Static = TemplateCache;
1418 StaticLocal = LocalTemplateCache;
1423 lprintf (1, "Can't to load a template with empty name!\n");
1424 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1428 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1429 !GetHash(Static, templatename, len, &vTmpl)) {
1430 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1431 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1433 (long)strlen(templatename));
1434 /// dbg_PrintHash(Static, PrintTemplate, NULL);
1435 // PrintHash(Static, VarPrintTransition, PrintTemplate);
1440 ProcessTemplate(vTmpl, Target, Context, ContextType);
1443 /*-----------------------------------------------------------------------------
1446 typedef struct _HashIterator {
1447 HashList *StaticList;
1448 int AdditionalParams;
1450 int XPectContextType;
1451 RetrieveHashlistFunc GetHash;
1452 HashDestructorFunc Destructor;
1453 SubTemplFunc DoSubTemplate;
1456 void RegisterITERATOR(const char *Name, long len,
1457 int AdditionalParams,
1458 HashList *StaticList,
1459 RetrieveHashlistFunc GetHash,
1460 SubTemplFunc DoSubTempl,
1461 HashDestructorFunc Destructor,
1463 int XPectContextType)
1465 HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
1466 It->StaticList = StaticList;
1467 It->AdditionalParams = AdditionalParams;
1468 It->GetHash = GetHash;
1469 It->DoSubTemplate = DoSubTempl;
1470 It->Destructor = Destructor;
1471 It->ContextType = ContextType;
1472 It->XPectContextType = XPectContextType;
1473 Put(Iterators, Name, len, It, NULL);
1476 void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1483 int nMembersCounted = 0;
1490 if (!GetHash(Iterators, TKEY(0), &vIt)) {
1491 lprintf(1, "unknown Iterator [%s] (in '%s' line %ld); "
1493 Tokens->Params[0]->Start,
1494 ChrPtr(Tokens->FileName),
1496 ChrPtr(Tokens->FlatToken));
1499 "<pre>\nunknown Iterator [%s] (in '%s' line %ld); \n"
1501 Tokens->Params[0]->Start,
1502 ChrPtr(Tokens->FileName),
1504 ChrPtr(Tokens->FlatToken));
1508 It = (HashIterator*) vIt;
1510 if (Tokens->nParameters < It->AdditionalParams + 2) {
1511 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1512 "doesn't work with %ld params [%s]\n",
1513 Tokens->Params[0]->Start,
1514 ChrPtr(Tokens->FileName),
1516 Tokens->nParameters,
1517 ChrPtr(Tokens->FlatToken));
1520 "<pre>Iterator [%s] \n(in '%s' line %ld);\n"
1521 "doesn't work with %ld params \n[%s]\n</pre>",
1522 Tokens->Params[0]->Start,
1523 ChrPtr(Tokens->FileName),
1525 Tokens->nParameters,
1526 ChrPtr(Tokens->FlatToken));
1530 if ((It->XPectContextType != CTX_NONE) &&
1531 (It->XPectContextType != ContextType)) {
1532 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1533 "requires context of type %ld, have %ld [%s]\n",
1535 ChrPtr(Tokens->FileName),
1537 It->XPectContextType,
1539 ChrPtr(Tokens->FlatToken));
1542 "<pre>\nIterator [%s] (in '%s' line %ld);"
1543 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1545 ChrPtr(Tokens->FileName),
1547 It->XPectContextType,
1549 ChrPtr(Tokens->FlatToken));
1554 if (It->StaticList == NULL)
1555 List = It->GetHash(Target, nArgs, Tokens, Context, ContextType);
1557 List = It->StaticList;
1559 nMembersUsed = GetCount(List);
1560 SubBuf = NewStrBuf();
1561 it = GetNewHashPos(List, 0);
1562 while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
1563 svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s",
1564 (oddeven) ? "odd" : "even");
1565 svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
1566 svputlong("ITERATE:N", nMembersCounted);
1567 svputlong("ITERATE:LASTN", ++nMembersCounted == nMembersUsed);
1569 if (It->DoSubTemplate != NULL)
1570 It->DoSubTemplate(SubBuf, vContext, Tokens);
1571 DoTemplate(TKEY(1), SubBuf, vContext, It->ContextType);
1573 StrBufAppendBuf(Target, SubBuf, 0);
1574 FlushStrBuf(SubBuf);
1575 oddeven = ~ oddeven;
1577 FreeStrBuf(&SubBuf);
1579 if (It->Destructor != NULL)
1580 It->Destructor(&List);
1585 /*-----------------------------------------------------------------------------
1588 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType)
1590 ConditionalStruct *Cond;
1592 if ((Tokens->Params[0]->len == 1) &&
1593 (Tokens->Params[0]->Start[0] == 'X'))
1594 return (state != 0)?Tokens->Params[1]->lvalue:0;
1596 Cond = (ConditionalStruct *) Tokens->PreEval;
1598 lprintf(1, "Conditional [%s] (in '%s' line %ld); unknown![%s]\n",
1599 Tokens->Params[0]->Start,
1600 ChrPtr(pTmpl->FileName),
1602 ChrPtr(Tokens->FlatToken));
1606 if (Tokens->nParameters < Cond->nParams) {
1607 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n",
1608 Tokens->Params[0]->Start,
1609 ChrPtr(pTmpl->FileName),
1612 ChrPtr(Tokens->FlatToken));
1615 "<pre>\nConditional [%s] (in '%s' line %ld); needs %ld Params!\n[%s]\n</pre>\n",
1616 Tokens->Params[0]->Start,
1617 ChrPtr(pTmpl->FileName),
1620 ChrPtr(Tokens->FlatToken));
1623 if (Cond->CondF(Tokens, Context, ContextType) == Neg)
1624 return Tokens->Params[1]->lvalue;
1628 int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
1633 if (!GetHash(WC->vars, TKEY(2), &vsubst))
1635 subst = (wcsubst*) vsubst;
1636 if ((subst->ContextRequired != CTX_NONE) &&
1637 (subst->ContextRequired != ContextType)) {
1638 lprintf(1," WARNING: Conditional requires Context: [%ld]!\n", Tokens->Params[2]->Start);
1642 switch(subst->wcs_type) {
1644 return (subst->wcs_function!=NULL);
1646 lprintf(1, " -> Server [%s]\n", subst->wcs_value);////todo
1650 case WCS_STRBUF_REF:
1651 if (Tokens->nParameters < 4)
1653 return (strcmp(Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
1655 if (Tokens->nParameters < 4)
1656 return (subst->lvalue != 0);
1657 return (subst->lvalue == Tokens->Params[3]->lvalue);
1659 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
1666 void RegisterConditional(const char *Name, long len,
1668 WCConditionalFunc CondF,
1669 int ContextRequired)
1671 ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
1672 Cond->PlainName = Name;
1673 Cond->nParams = nParams;
1674 Cond->CondF = CondF;
1675 Cond->ContextRequired = ContextRequired;
1676 Put(Conditionals, Name, len, Cond, NULL);
1679 /*-----------------------------------------------------------------------------
1682 void tmplput_ContextString(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1684 StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, (StrBuf*)Context, 0);
1686 int ConditionalContextStr(WCTemplateToken *Tokens, void *Context, int ContextType)
1688 StrBuf *TokenText = (StrBuf*) Context;
1689 const char *CompareToken;
1692 GetTemplateTokenString(Tokens, 2, &CompareToken, &len);
1693 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
1696 /*-----------------------------------------------------------------------------
1700 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1704 if (Tokens->Params[1]->Type == TYPE_STR) {
1705 Headline = NewStrBuf();
1706 DoTemplate(TKEY(1), Headline, Context, ContextType);
1711 GetTemplateTokenString(Tokens,
1715 Headline = NewStrBufPlain(Ch, len);
1719 DoTemplate(HKEY("beginbox"), Target, Headline, CTX_STRBUF);
1720 DoTemplate(TKEY(0), Target, Context, ContextType);
1721 DoTemplate(HKEY("endbox"), Target, Context, ContextType);
1722 FreeStrBuf(&Headline);
1725 /*-----------------------------------------------------------------------------
1729 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1732 int i, ntabs, nTabs;
1734 nTabs = ntabs = Tokens->nParameters / 2;
1735 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
1737 for (i = 0; i < ntabs; i++) {
1738 TabNames[i] = NewStrBuf();
1739 if (Tokens->Params[i * 2]->len > 0) {
1740 DoTemplate(TKEY(i * 2), TabNames[i], Context, ContextType);
1743 /** A Tab without subject? we can't count that, add it as silent */
1748 StrTabbedDialog(Target, nTabs, TabNames);
1749 for (i = 0; i < ntabs; i++) {
1750 StrBeginTab(Target, i, nTabs);
1752 DoTemplate(TKEY(i * 2 + 1), Target, Context, ContextType);
1753 StrEndTab(Target, i, nTabs);
1758 /*-----------------------------------------------------------------------------
1762 typedef struct _SortStruct {
1764 StrBuf *PrefPrepend;
1765 CompareFunc Forward;
1766 CompareFunc Reverse;
1771 void DestroySortStruct(void *vSort)
1773 SortStruct *Sort = (SortStruct*) vSort;
1774 FreeStrBuf(&Sort->Name);
1775 FreeStrBuf(&Sort->PrefPrepend);
1779 void RegisterSortFunc(const char *name, long len,
1780 const char *prepend, long preplen,
1781 CompareFunc Forward,
1782 CompareFunc Reverse,
1785 SortStruct *NewSort = (SortStruct*) malloc(sizeof(SortStruct));
1786 NewSort->Name = NewStrBufPlain(name, len);
1787 if (prepend != NULL)
1788 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
1790 NewSort->PrefPrepend = NULL;
1791 NewSort->Forward = Forward;
1792 NewSort->Reverse = Reverse;
1793 NewSort->ContextType = ContextType;
1794 Put(SortHash, name, len, NewSort, DestroySortStruct);
1797 CompareFunc RetrieveSort(long ContextType, const char *OtherPrefix,
1798 const char *Default, long ldefault, long DefaultDirection)
1801 const StrBuf *BSort;
1806 if (havebstr("SortBy")) {
1807 BSort = sbstr("SortBy");
1809 else { /** Try to fallback to our remembered values... */
1810 if (OtherPrefix == NULL) {
1811 BSort = get_room_pref("sort");
1814 ////todo: nail prefprepend to sort, and lookup this!
1818 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
1819 (vSortBy == NULL)) {
1821 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
1822 (vSortBy == NULL)) {
1823 lprintf(1, "Illegal default sort: [%s]\n", Default);
1827 SortBy = (SortStruct*)vSortBy;
1829 /** Ok, its us, lets see in which direction we should sort... */
1830 if (havebstr("SortOrder")) {
1831 SortOrder = LBSTR("SortOrder");
1833 else { /** Try to fallback to our remembered values... */
1834 if (SortBy->PrefPrepend == NULL) {
1835 SortOrder = StrTol(get_room_pref("SortOrder"));
1838 ////todo: nail prefprepend to sort, and lookup this!
1841 switch (SortOrder) {
1846 return SortBy->Forward;
1848 return SortBy->Reverse;
1860 ConstStr SortIcons[] = {
1861 {HKEY("static/sort_none.gif")},
1862 {HKEY("static/up_pointer.gif")},
1863 {HKEY("static/down_pointer.gif")},
1866 ConstStr SortNextOrder[] = {
1873 int GetSortMetric(WCTemplateToken *Tokens, SortStruct **Next, SortStruct **Param, long *SortOrder)
1875 int bSortError = eNOT_SPECIFIED;
1876 const StrBuf *BSort;
1881 if (!GetHash(SortHash, TKEY(0), &vSort) ||
1883 return eNO_SUCH_SORT;
1884 *Param = (SortStruct*) vSort;
1887 if (havebstr("SortBy")) {
1888 BSort = sbstr("SortBy");
1889 bSortError = eINVALID_PARAM;
1891 else { /** Try to fallback to our remembered values... */
1892 if ((*Param)->PrefPrepend == NULL) {
1893 BSort = get_room_pref("sort");
1896 ////todo: nail prefprepend to sort, and lookup this!
1900 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
1904 *Next = (SortStruct*) vSort;
1906 /** Ok, its us, lets see in which direction we should sort... */
1907 if (havebstr("SortOrder")) {
1908 *SortOrder = LBSTR("SortOrder");
1910 else { /** Try to fallback to our remembered values... */
1911 if ((*Param)->PrefPrepend == NULL) {
1912 *SortOrder = StrTol(get_room_pref("SortOrder"));
1915 ////todo: nail prefprepend to sort, and lookup this!
1925 void tmplput_SORT_ICON(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1930 const ConstStr *SortIcon;
1932 switch (GetSortMetric(Tokens, &Next, &Param, &SortOrder)){
1934 lprintf(1, "[%s] (in '%s' line %ld); "
1935 " Sorter [%s] unknown! [%s]\n",
1937 ChrPtr(Tokens->FileName),
1939 Tokens->Params[0]->Start,
1940 ChrPtr(Tokens->FlatToken));
1943 "<pre>\n [%s] (in '%s' line %ld);"
1944 " Sorter [%s] unknown!\n[%s]\n</pre>\n",
1946 ChrPtr(Tokens->FileName),
1948 Tokens->Params[0]->Start,
1949 ChrPtr(Tokens->FlatToken));
1951 case eINVALID_PARAM:
1952 lprintf(1, "[%s] (in '%s' line %ld); "
1953 " Sorter specified by BSTR 'SortBy' [%s] unknown! [%s]\n",
1955 ChrPtr(Tokens->FileName),
1958 ChrPtr(Tokens->FlatToken));
1959 case eNOT_SPECIFIED:
1961 if (Next == Param) {
1962 SortIcon = &SortIcons[SortOrder];
1964 else { /** Not Us... */
1965 SortIcon = &SortIcons[0];
1967 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
1971 void tmplput_SORT_NEXT(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1977 switch (GetSortMetric(Tokens, &Next, &Param, &SortOrder)){
1979 lprintf(1, "[%s] (in '%s' line %ld); "
1980 " Sorter [%s] unknown! [%s]\n",
1982 ChrPtr(Tokens->FileName),
1984 Tokens->Params[0]->Start,
1985 ChrPtr(Tokens->FlatToken));
1988 "<pre>\n [%s] (in '%s' line %ld);"
1989 " Sorter [%s] unknown!\n[%s]\n</pre>\n",
1991 ChrPtr(Tokens->FileName),
1993 Tokens->Params[0]->Start,
1994 ChrPtr(Tokens->FlatToken));
1996 case eINVALID_PARAM:
1997 lprintf(1, "[%s] (in '%s' line %ld); "
1998 " Sorter specified by BSTR 'SortBy' [%s] unknown! [%s]\n",
2000 ChrPtr(Tokens->FileName),
2003 ChrPtr(Tokens->FlatToken));
2005 case eNOT_SPECIFIED:
2007 StrBufAppendBuf(Target, Param->Name, 0);
2012 void tmplput_SORT_ORDER(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
2015 const ConstStr *SortOrderStr;
2019 switch (GetSortMetric(Tokens, &Next, &Param, &SortOrder)){
2021 lprintf(1, "[%s] (in '%s' line %ld); "
2022 " Sorter [%s] unknown! [%s]\n",
2024 ChrPtr(Tokens->FileName),
2026 Tokens->Params[0]->Start,
2027 ChrPtr(Tokens->FlatToken));
2030 "<pre>\n [%s] (in '%s' line %ld);"
2031 " Sorter [%s] unknown!\n[%s]\n</pre>\n",
2033 ChrPtr(Tokens->FileName),
2035 Tokens->Params[0]->Start,
2036 ChrPtr(Tokens->FlatToken));
2038 case eINVALID_PARAM:
2039 lprintf(1, "[%s] (in '%s' line %ld); "
2040 " Sorter specified by BSTR 'SortBy' [%s] unknown! [%s]\n",
2042 ChrPtr(Tokens->FileName),
2045 ChrPtr(Tokens->FlatToken));
2047 case eNOT_SPECIFIED:
2049 if (Next == Param) {
2050 SortOrderStr = &SortNextOrder[SortOrder];
2052 else { /** Not Us... */
2053 SortOrderStr = &SortNextOrder[0];
2055 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2060 void tmplput_long_vector(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
2062 long *LongVector = (long*) Context;
2064 if ((Tokens->Params[0]->Type == TYPE_LONG) &&
2065 (Tokens->Params[0]->lvalue <= LongVector[0]))
2067 StrBufAppendPrintf(Target, "%ld", LongVector[Tokens->Params[0]->lvalue]);
2071 if (Tokens->Params[0]->Type == TYPE_LONG) {
2072 lprintf(1, "longvector [%s] (in '%s' line %ld); needs a long Parameter![%s]\n",
2073 Tokens->Params[0]->Start,
2074 ChrPtr(Tokens->FileName),
2076 ChrPtr(Tokens->FlatToken));
2079 "<pre>\nlongvector [%s] (in '%s' line %ld); needs a numerical Parameter!\n[%s]\n</pre>\n",
2080 Tokens->Params[0]->Start,
2081 ChrPtr(Tokens->FileName),
2083 ChrPtr(Tokens->FlatToken));
2086 lprintf(1, "longvector [%s] (in '%s' line %ld); doesn't have %ld Parameters,"
2087 " its just the size of %ld![%s]\n",
2088 Tokens->Params[0]->Start,
2089 ChrPtr(Tokens->FileName),
2091 Tokens->Params[0]->lvalue,
2093 ChrPtr(Tokens->FlatToken));
2096 "<pre>\nlongvector [%s] (in '%s' line %ld); doesn't have %ld Parameters,"
2097 " its just the size of %ld!\n[%s]\n</pre>\n",
2098 Tokens->Params[0]->Start,
2099 ChrPtr(Tokens->FileName),
2101 Tokens->Params[0]->lvalue,
2103 ChrPtr(Tokens->FlatToken));
2110 int ConditionalLongVector(WCTemplateToken *Tokens, void *Context, int ContextType)
2112 long *LongVector = (long*) Context;
2114 if ((Tokens->Params[2]->Type == TYPE_LONG) &&
2115 (Tokens->Params[2]->lvalue <= LongVector[0])&&
2116 (Tokens->Params[3]->Type == TYPE_LONG) &&
2117 (Tokens->Params[3]->lvalue <= LongVector[0]))
2119 return LongVector[Tokens->Params[2]->lvalue] == LongVector[Tokens->Params[3]->lvalue];
2123 if ((Tokens->Params[2]->Type == TYPE_LONG) ||
2124 (Tokens->Params[2]->Type == TYPE_LONG)) {
2125 lprintf(1, "ConditionalLongVector [%s] (in '%s' line %ld); needs two long Parameter![%s]\n",
2126 Tokens->Params[0]->Start,
2127 ChrPtr(Tokens->FileName),
2129 ChrPtr(Tokens->FlatToken));
2132 lprintf(1, "longvector [%s] (in '%s' line %ld); doesn't have %ld / %ld Parameters,"
2133 " its just the size of %ld![%s]\n",
2134 Tokens->Params[0]->Start,
2135 ChrPtr(Tokens->FileName),
2137 Tokens->Params[2]->lvalue,
2138 Tokens->Params[3]->lvalue,
2140 ChrPtr(Tokens->FlatToken));
2150 RegisterNamespace("SORT:ICON", 1, 1, tmplput_SORT_ICON, CTX_NONE);
2151 RegisterNamespace("SORT:ORDER", 1, 1, tmplput_SORT_ORDER, CTX_NONE);
2152 RegisterNamespace("SORT:NEXT", 1, 1, tmplput_SORT_NEXT, CTX_NONE);
2153 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, CTX_STRBUF);
2154 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, CTX_NONE);
2155 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, CTX_NONE);
2156 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, CTX_NONE);
2157 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, CTX_LONGVECTOR);
2158 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
2159 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
2160 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);