17 #define SHOW_ME_VAPPEND_PRINTF
20 #include "webserver.h"
22 extern char *static_dirs[PATH_MAX]; /* Disk representation */
24 HashList *WirelessTemplateCache;
25 HashList *WirelessLocalTemplateCache;
26 HashList *TemplateCache;
27 HashList *LocalTemplateCache;
31 HashList *Conditionals;
35 int DumpTemplateI18NStrings = 0;
36 int LoadTemplates = 0;
37 int dbg_bactrace_template_errors = 0;
38 WCTemplputParams NoCtx;
39 StrBuf *I18nDump = NULL;
41 const char EmptyStr[]="";
44 #define SV_CONDITIONAL 2
45 #define SV_NEG_CONDITIONAL 3
46 #define SV_CUST_STR_CONDITIONAL 4
48 #define SV_PREEVALUATED 6
52 * Dynamic content for variable substitution in templates
54 typedef struct _wcsubst {
56 int wcs_type; /* which type of Substitution are we */
57 char wcs_key[32]; /* copy of our hashkey for debugging */
58 StrBuf *wcs_value; /* if we're a string, keep it here */
59 long lvalue; /* type long? keep data here */
60 WCHandlerFunc wcs_function; /* funcion hook ???*/
64 typedef struct _WCTemplate {
70 WCTemplateToken **Tokens;
73 typedef struct _HashHandler {
75 WCPreevalFunc PreEvalFunc;
76 WCHandlerFunc HandlerFunc;
79 void *load_template(WCTemplate *NewTemplate);
80 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP);
84 typedef struct _SortStruct {
89 CompareFunc GroupChange;
94 const char *CtxNames[] = {
105 "Context MIME_ATACH",
109 "Context LONGVECTOR",
114 "Context DavNamespace",
120 void DestroySortStruct(void *vSort)
122 SortStruct *Sort = (SortStruct*) vSort;
123 FreeStrBuf(&Sort->Name);
124 FreeStrBuf(&Sort->PrefPrepend);
128 const char *ContextName(int ContextType)
130 if (ContextType < CTX_UNKNOWN)
131 return CtxNames[ContextType];
133 return CtxNames[CTX_UNKNOWN];
136 void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplputParams *TP, const char *Format, ...)
142 const char *Err = NULL;
147 va_start(arg_ptr, Format);
148 StrBufVAppendPrintf(Error, Format, arg_ptr);
152 case ERR_NAME: /* the main token name... */
153 Err = (TP->Tokens!= NULL)? TP->Tokens->pName:"";
156 Err = ((TP->Tokens!= NULL) &&
157 (TP->Tokens->nParameters > ErrorPos - 1))?
158 TP->Tokens->Params[ErrorPos - 1]->Start : "";
161 if (TP->Tokens != NULL)
163 lprintf(1, "%s [%s] (in '%s' line %ld); %s; [%s]\n",
166 ChrPtr(TP->Tokens->FileName),
169 ChrPtr(TP->Tokens->FlatToken));
173 lprintf(1, "%s: %s;\n",
188 if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
189 if (TP->Tokens != NULL)
194 "<pre>\n%s [%s] (in '%s' line %ld); %s\n[%s]\n</pre>\n",
197 ChrPtr(TP->Tokens->FileName),
200 ChrPtr(TP->Tokens->FlatToken));
202 StrBufPrintf(Info, "%s [%s] %s; [%s]",
206 ChrPtr(TP->Tokens->FlatToken));
209 SerializeJson(WCC->WFBuf, WildFireException(SKEY(TP->Tokens->FileName),
214 SerializeJson(Header, WildFireMessage(SKEY(TP->Tokens->FileName),
226 "<pre>\n%s: %s\n</pre>\n",
230 StrBufPrintf(Info, "%s [%s] %s; [%s]",
234 ChrPtr(TP->Tokens->FlatToken));
235 SerializeJson(WCC->WFBuf, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
240 if (dbg_bactrace_template_errors)
248 void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
258 va_start(arg_ptr, Format);
259 StrBufVAppendPrintf(Error, Format, arg_ptr);
262 lprintf(1, ChrPtr(Error));
265 if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
267 SerializeJson(WCC->WFBuf, WildFireException(Type, strlen(Type),
275 if (dbg_bactrace_template_errors)
281 void RegisterNS(const char *NSName,
285 WCHandlerFunc HandlerFunc,
286 WCPreevalFunc PreevalFunc,
289 HashHandler *NewHandler;
291 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
292 memset(NewHandler, 0, sizeof(HashHandler));
293 NewHandler->Filter.nMinArgs = nMinArgs;
294 NewHandler->Filter.nMaxArgs = nMaxArgs;
295 NewHandler->Filter.ContextType = ContextRequired;
296 NewHandler->Filter.ControlContextType = CTX_NONE;
298 NewHandler->PreEvalFunc = PreevalFunc;
299 NewHandler->HandlerFunc = HandlerFunc;
300 Put(GlobalNS, NSName, len, NewHandler, NULL);
303 void RegisterControlNS(const char *NSName,
307 WCHandlerFunc HandlerFunc,
308 int ControlContextRequired)
310 HashHandler *NewHandler;
312 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
313 memset(NewHandler, 0, sizeof(HashHandler));
314 NewHandler->Filter.nMinArgs = nMinArgs;
315 NewHandler->Filter.nMaxArgs = nMaxArgs;
316 NewHandler->Filter.ContextType = CTX_NONE;
317 NewHandler->Filter.ControlContextType = ControlContextRequired;
318 NewHandler->HandlerFunc = HandlerFunc;
319 Put(GlobalNS, NSName, len, NewHandler, NULL);
324 int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, const char *ErrType)
326 if ((Need->ContextType != CTX_NONE) &&
327 (Need->ContextType != TP->Filter.ContextType)) {
329 Target, ErrType, ERR_PARM1, TP,
330 " WARNING: requires Context: [%s], have [%s]!",
331 ContextName(Need->ContextType),
332 ContextName(TP->Filter.ContextType));
336 if ((Need->ControlContextType != CTX_NONE) &&
337 (Need->ControlContextType != TP->Filter.ControlContextType)) {
339 Target, ErrType, ERR_PARM1, TP,
340 " WARNING: requires Control Context: [%s], have [%s]!",
341 ContextName(Need->ControlContextType),
342 ContextName(TP->Filter.ControlContextType));
346 if (TP->Tokens->nParameters < Need->nMinArgs) {
347 LogTemplateError(Target, ErrType, ERR_NAME, TP,
348 "needs at least %ld params, have %ld",
350 TP->Tokens->nParameters);
354 else if (TP->Tokens->nParameters > Need->nMaxArgs) {
355 LogTemplateError(Target, ErrType, ERR_NAME, TP,
356 "just needs %ld params, you gave %ld",
358 TP->Tokens->nParameters);
366 void FreeToken(WCTemplateToken **Token)
369 FreeStrBuf(&(*Token)->FlatToken);
370 if ((*Token)->HaveParameters)
371 for (i = 0; i < (*Token)->nParameters; i++)
372 free((*Token)->Params[i]);
379 void FreeWCTemplate(void *vFreeMe)
382 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
384 if (FreeMe->TokenSpace > 0) {
385 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
386 FreeToken(&FreeMe->Tokens[i]);
388 free(FreeMe->Tokens);
390 FreeStrBuf(&FreeMe->FileName);
391 FreeStrBuf(&FreeMe->Data);
392 FreeStrBuf(&FreeMe->MimeType);
398 * debugging function to print array to log
400 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
403 * debugging function to print array to log
405 void VarPrintEntry(const char *Key, void *vSubst, int odd)
408 lprintf(1,"Subst[%s] : ", Key);
409 ptr = (wcsubst*) vSubst;
411 switch(ptr->wcs_type) {
413 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
416 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
419 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
422 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
429 * Clear out the list of substitution variables local to this session
431 void clear_substs(wcsession *wc) {
433 if (wc->vars != NULL) {
434 DeleteHash(&wc->vars);
439 * Clear out the list of substitution variables local to this session
441 void clear_local_substs(void) {
445 int NeedNewBuf(int type)
460 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
462 int NeedNew = NeedNewBuf(type);
463 switch(ptr->wcs_type) {
467 if (reusestrbuf && NeedNew) {
468 FlushStrBuf(ptr->wcs_value);
472 FreeStrBuf(&ptr->wcs_value);
473 ptr->wcs_value = NULL;
477 ptr->wcs_function = NULL;
478 if (reusestrbuf && NeedNew)
479 ptr->wcs_value = NewStrBuf();
482 ptr->wcs_value = NULL;
483 if (reusestrbuf && NeedNew)
484 ptr->wcs_value = NewStrBuf();
488 if (reusestrbuf && NeedNew)
489 ptr->wcs_value = NewStrBuf();
492 if (reusestrbuf && NeedNew)
493 ptr->wcs_value = NewStrBuf();
500 * destructor; kill one entry.
502 void deletevar(void *data)
504 wcsubst *ptr = (wcsubst*)data;
505 FlushPayload(ptr, 0, ptr->wcs_type);
510 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
515 ptr = (wcsubst *) malloc(sizeof(wcsubst));
516 memset(ptr, 0, sizeof(wcsubst));
518 ptr->wcs_type = type;
519 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
520 Put(WCC->vars, keyname, keylen, ptr, deletevar);
522 switch(ptr->wcs_type) {
525 ptr->wcs_value = NewStrBuf();
539 * Add a substitution variable (local to this session) (strlen version...)
540 * keyname the replacementstring to substitute
541 * keytype the kind of the key
542 * format the format string ala printf
543 * ... the arguments to substitute in the formatstring
545 void SVPRINTF(char *keyname, int keytype, const char *format,...)
553 keylen = strlen(keyname);
555 * First look if we're doing a replacement of
558 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
559 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
560 ptr = (wcsubst*)vPtr;
561 FlushPayload(ptr, keytype, keytype);
562 ptr->wcs_type = keytype;
564 else /** Otherwise allocate a new one */
566 ptr = NewSubstVar(keyname, keylen, keytype);
569 /* Format the string */
570 va_start(arg_ptr, format);
571 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
576 * Add a substitution variable (local to this session)
577 * keyname the replacementstring to substitute
578 * keytype the kind of the key
579 * format the format string ala printf
580 * ... the arguments to substitute in the formatstring
582 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
590 * First look if we're doing a replacement of
593 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
594 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
595 ptr = (wcsubst*)vPtr;
596 FlushPayload(ptr, 1, keytype);
597 ptr->wcs_type = keytype;
599 else /** Otherwise allocate a new one */
601 ptr = NewSubstVar(keyname, keylen, keytype);
604 /** Format the string and save it */
605 va_start(arg_ptr, format);
606 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
611 * Add a substitution variable (local to this session)
612 * keyname the replacementstring to substitute
613 * keytype the kind of the key
614 * format the format string ala printf
615 * ... the arguments to substitute in the formatstring
617 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
625 * First look if we're doing a replacement of
628 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
629 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
630 ptr = (wcsubst*)vPtr;
631 FlushPayload(ptr, 1, keytype);
632 ptr->wcs_type = keytype;
634 else /** Otherwise allocate a new one */
636 ptr = NewSubstVar(keyname, keylen, keytype);
638 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
642 * \brief Add a substitution variable (local to this session)
643 * \param keyname the replacementstring to substitute
644 * \param keytype the kind of the key
645 * \param format the format string ala printf
646 * \param ... the arguments to substitute in the formatstring
648 void SVPutLong(char *keyname, size_t keylen, long Data)
656 * First look if we're doing a replacement of
659 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
660 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
661 ptr = (wcsubst*)vPtr;
662 FlushPayload(ptr, 1, WCS_LONG);
663 ptr->wcs_type = WCS_LONG;
665 else /** Otherwise allocate a new one */
667 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
673 * \brief Add a substitution variable (local to this session) that does a callback
674 * \param keyname the keystring to substitute
675 * \param fcn_ptr the function callback to give the substitution string
677 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
684 * First look if we're doing a replacement of
687 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
688 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
689 ptr = (wcsubst*)vPtr;
690 FlushPayload(ptr, 1, WCS_FUNCTION);
691 ptr->wcs_type = WCS_FUNCTION;
693 else /** Otherwise allocate a new one */
695 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
698 ptr->wcs_function = fcn_ptr;
700 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
702 SVCallback(keyname, strlen(keyname), fcn_ptr);
707 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
714 * First look if we're doing a replacement of
717 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
718 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
719 ptr = (wcsubst*)vPtr;
720 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
721 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
723 else /** Otherwise allocate a new one */
725 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
727 ptr->wcs_value = (StrBuf*)Buf;
731 * \brief back end for print_value_of() ... does a server command
732 * \param servcmd server command to execute on the citadel server
734 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
738 serv_puts(ChrPtr(servcmd));
739 len = serv_getln(buf, sizeof buf);
745 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
748 _fmout(Target, "CENTER");
751 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
757 int HaveTemplateTokenString(StrBuf *Target,
758 WCTemplputParams *TP,
763 if (N >= TP->Tokens->nParameters) {
767 switch (TP->Tokens->Params[N]->Type) {
773 case TYPE_SUBTEMPLATE:
782 void GetTemplateTokenString(StrBuf *Target,
783 WCTemplputParams *TP,
789 WCTemplputParams SubTP;
791 if (N >= TP->Tokens->nParameters) {
792 LogTemplateError(Target,
793 "TokenParameter", N, TP,
794 "invalid token %d. this shouldn't have come till here.\n", N);
800 switch (TP->Tokens->Params[N]->Type) {
804 *Value = TP->Tokens->Params[N]->Start;
805 *len = TP->Tokens->Params[N]->len;
808 if (TP->Tokens->Params[N]->len == 0) {
809 LogTemplateError(Target,
810 "TokenParameter", N, TP,
811 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
816 Buf = (StrBuf*) SBstr(TKEY(N));
817 *Value = ChrPtr(Buf);
818 *len = StrLength(Buf);
821 if (TP->Tokens->Params[N]->len == 0) {
822 LogTemplateError(Target,
823 "TokenParameter", N, TP,
824 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
829 get_PREFERENCE(TKEY(N), &Buf);
830 *Value = ChrPtr(Buf);
831 *len = StrLength(Buf);
834 LogTemplateError(Target,
835 "TokenParameter", N, TP,
836 "Requesting parameter %d; of type LONG, want string.", N);
839 LogTemplateError(Target,
840 "TokenParameter", N, TP,
841 "Requesting parameter %d; of type PREFINT, want string.", N);
844 *Value = _(TP->Tokens->Params[N]->Start);
845 *len = strlen(*Value);
847 case TYPE_SUBTEMPLATE:
848 if (TP->Tokens->Params[N]->len == 0) {
849 LogTemplateError(Target,
850 "TokenParameter", N, TP,
851 "Requesting parameter %d; of type SUBTEMPLATE, empty lookup string not admitted.", N);
857 memset(&SubTP, 0, sizeof(WCTemplputParams *));
858 SubTP.Context = TP->Context;
859 SubTP.Filter.ContextType = TP->Filter.ContextType;
861 DoTemplate(TKEY(N), Buf, &SubTP);
862 *Value = ChrPtr(Buf);
863 *len = StrLength(Buf);
864 /* we can't free it here, so we put it into the subst so its discarded later on. */
865 SVPUTBuf(TKEY(N), Buf, 0);
869 LogTemplateError(Target,
870 "TokenParameter", N, TP,
871 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
876 long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
879 if (N >= TP->Tokens->nParameters) {
880 LogTemplateError(Target,
881 "TokenParameter", N, TP,
882 "invalid token %d. this shouldn't have come till here.\n", N);
887 switch (TP->Tokens->Params[N]->Type) {
890 return atol(TP->Tokens->Params[N]->Start);
893 if (TP->Tokens->Params[N]->len == 0) {
894 LogTemplateError(Target,
895 "TokenParameter", N, TP,
896 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
899 return LBstr(TKEY(N));
902 LogTemplateError(Target,
903 "TokenParameter", N, TP,
904 "requesting a prefstring in param %d want a number", N);
905 if (TP->Tokens->Params[N]->len == 0) {
906 LogTemplateError(Target,
907 "TokenParameter", N, TP,
908 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
911 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
916 return TP->Tokens->Params[N]->lvalue;
918 if (TP->Tokens->Params[N]->len == 0) {
919 LogTemplateError(Target,
920 "TokenParameter", N, TP,
921 "Requesting parameter %d; of type PREFINT, empty lookup string not admitted.", N);
924 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
928 LogTemplateError(Target,
929 "TokenParameter", N, TP,
930 "requesting a I18N string in param %d; want a number", N);
932 case TYPE_SUBTEMPLATE:
933 LogTemplateError(Target,
934 "TokenParameter", N, TP,
935 "requesting a subtemplate in param %d; not supported for numbers", N);
938 LogTemplateError(Target,
939 "TokenParameter", N, TP,
940 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
948 * \brief Print the value of a variable
949 * \param keyname get a key to print
951 void print_value_of(StrBuf *Target, WCTemplputParams *TP)
958 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
959 /* TODO: depricated! */
960 if (TP->Tokens->pName[0] == '=') {
961 DoTemplate(TP->Tokens->pName+1, TP->Tokens->NameEnd - 1, NULL, &NoCtx);
963 /*/////TODO: if param[1] == "U" -> urlescape
965 /** Page-local variables */
966 if ((WCC->vars!= NULL) && GetHash(WCC->vars, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
967 ptr = (wcsubst*) vVar;
968 switch(ptr->wcs_type) {
970 StrBufAppendBuf(Target, ptr->wcs_value, 0);
973 pvo_do_cmd(Target, ptr->wcs_value);
976 (*ptr->wcs_function) (Target, TP);
980 StrBufAppendBuf(Target, ptr->wcs_value, 0);
983 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
987 Target, "Subst", ERR_NAME, TP,
988 "WARNING: invalid value in SV-Hash at %s!", TP->Tokens->pName);
993 Target, "Token", ERR_NAME, TP,
994 "didn't find Handler \"%s\"", TP->Tokens->pName);
999 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
1001 wcsession *WCC = WC;
1005 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1006 ParamToLookup->len, &vVar)) {
1007 ptr = (wcsubst*) vVar;
1008 switch(ptr->wcs_type) {
1011 case WCS_STRBUF_REF:
1012 if (ParamToCompare->Type == TYPE_STR)
1013 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
1014 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
1016 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
1024 if (ParamToCompare->Type == TYPE_STR)
1027 return ParamToCompare->lvalue == ptr->lvalue;
1030 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1031 ParamToLookup->Start);
1037 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
1039 wcsession *WCC = WC;
1043 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1044 ParamToLookup->len, &vVar)) {
1045 ptr = (wcsubst*) vVar;
1046 switch(ptr->wcs_type) {
1049 case WCS_STRBUF_REF:
1050 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
1051 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
1058 return StrTol(Compare) == ptr->lvalue;
1060 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1061 ParamToLookup->Start);
1070 * \brief puts string into the template and computes which escape methon we should use
1071 * \param Source the string we should put into the template
1072 * \param FormatTypeIndex where should we look for escape types if?
1074 void StrBufAppendTemplate(StrBuf *Target,
1075 WCTemplputParams *TP,
1076 const StrBuf *Source, int FormatTypeIndex)
1079 char EscapeAs = ' ';
1081 if ((FormatTypeIndex < TP->Tokens->nParameters) &&
1082 (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
1083 (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
1084 EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
1091 StrEscAppend(Target, Source, NULL, 0, 2);
1094 StrEscAppend(Target, Source, NULL, 0, 0);
1097 StrECMAEscAppend(Target, Source, NULL);
1100 StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 0);
1103 StrBufUrlescAppend(Target, Source, NULL);
1106 StrBufAppendBuf(Target, Source, 0);
1111 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
1113 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
1114 if (Template->TokenSpace <= 0) {
1115 Template->Tokens = (WCTemplateToken**)malloc(
1116 sizeof(WCTemplateToken*) * 10);
1117 memset(Template->Tokens, 0, sizeof(WCTemplateToken*) * 10);
1118 Template->TokenSpace = 10;
1121 WCTemplateToken **NewTokens;
1123 NewTokens= (WCTemplateToken**) malloc(
1124 sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1127 0, sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1131 sizeof(WCTemplateToken*) * Template->nTokensUsed);
1133 free(Template->Tokens);
1134 Template->TokenSpace *= 2;
1135 Template->Tokens = NewTokens;
1138 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
1141 int GetNextParameter(StrBuf *Buf,
1144 WCTemplateToken *Tokens,
1146 WCTemplputParams *TP,
1147 TemplateParam **pParm)
1149 const char *pch = *pCh;
1150 const char *pchs, *pche;
1151 TemplateParam *Parm;
1155 *pParm = Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
1156 memset(Parm, 0, sizeof(TemplateParam));
1157 Parm->Type = TYPE_STR;
1159 /* Skip leading whitespaces */
1160 while ((*pch == ' ' )||
1163 (*pch == '\n')) pch ++;
1166 Parm->Type = TYPE_PREFSTR;
1173 else if (*pch == ';') {
1174 Parm->Type = TYPE_PREFINT;
1181 else if (*pch == '#') {
1182 Parm->Type = TYPE_INTDEFINE;
1185 else if (*pch == '_') {
1186 Parm->Type = TYPE_GETTEXT;
1193 else if (*pch == 'B') {
1194 Parm->Type = TYPE_BSTR;
1201 else if (*pch == '=') {
1202 Parm->Type = TYPE_SUBTEMPLATE;
1213 else if (*pch == '\'')
1215 if (quote != '\0') {
1220 ( (pch > pchs) && (*(pch - 1) == '\\'))
1225 if (*pch != quote) {
1226 lprintf(1, "Error (in '%s' line %ld); "
1227 "evaluating template param [%s] in Token [%s]\n",
1228 ChrPtr(pTmpl->FileName),
1230 ChrPtr(Tokens->FlatToken),
1238 StrBufPeek(Buf, pch, -1, '\0');
1239 if (LoadTemplates > 1) {
1240 lprintf(1, "DBG: got param [%s] %ld %ld\n",
1241 pchs, pche - pchs, strlen(pchs));
1244 Parm->len = pche - pchs;
1245 pch ++; /* move after trailing quote */
1246 if (ParamBrace && (*pch == ')')) {
1253 Parm->Type = TYPE_LONG;
1255 while ((pch <= pe) &&
1261 if (pch - pchs > 1){
1262 StrBufPeek(Buf, pch, -1, '\0');
1263 Parm->lvalue = atol(pchs);
1270 lprintf(1, "Error (in '%s' line %ld); "
1271 "evaluating long template param [%s] in Token [%s]\n",
1272 ChrPtr(pTmpl->FileName),
1274 ChrPtr(Tokens->FlatToken),
1282 while ((*pch == ' ' )||
1286 (*pch == '\n')) pch ++;
1291 if (DumpTemplateI18NStrings) {
1292 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
1295 case TYPE_INTDEFINE: {
1298 if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
1302 PVal = (long*) vPVal;
1304 Parm->lvalue = *PVal;
1306 else if (strchr(Parm->Start, '|') != NULL)
1313 pToken = NewStrBufPlain (Parm->Start, Parm->len);
1314 Match = NewStrBufPlain (NULL, Parm->len);
1315 Pos = ChrPtr(pToken);
1317 while ((Pos != NULL) && (Pos != StrBufNOTNULL))
1319 StrBufExtract_NextToken(Match, pToken, &Pos, '|');
1321 if (StrLength (Match) > 0)
1323 if (GetHash(Defines, SKEY(Match), &vPVal) &&
1327 PVal = (long*) vPVal;
1329 Parm->lvalue |= *PVal;
1332 LogTemplateError(NULL, "Define",
1333 Tokens->nParameters,
1342 else if (strchr(Parm->Start, '&') != NULL)
1348 Parm->MaskBy = eAND;
1349 pToken = NewStrBufPlain (Parm->Start, Parm->len);
1350 Match = NewStrBufPlain (NULL, Parm->len);
1351 Pos = ChrPtr(pToken);
1353 while ((Pos != NULL) && (Pos != StrBufNOTNULL))
1355 StrBufExtract_NextToken(Match, pToken, &Pos, '&');
1357 if (StrLength (Match) > 0)
1359 if (GetHash(Defines, SKEY(Match), &vPVal) &&
1363 PVal = (long*) vPVal;
1365 Parm->lvalue |= *PVal;
1368 LogTemplateError(NULL, "Define",
1369 Tokens->nParameters,
1381 LogTemplateError(NULL, "Define",
1382 Tokens->nParameters,
1388 case TYPE_SUBTEMPLATE:{
1390 /* well, we don't check the mobile stuff here... */
1391 if (!GetHash(LocalTemplateCache, Parm->Start, Parm->len, &vTmpl) &&
1392 !GetHash(TemplateCache, Parm->Start, Parm->len, &vTmpl)) {
1393 LogTemplateError(NULL,
1395 Tokens->nParameters,
1397 "referenced here doesn't exist");
1405 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
1407 const char *pTokenStart,
1408 const char *pTokenEnd,
1414 WCTemplateToken *NewToken;
1415 WCTemplputParams TP;
1417 NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
1418 memset(NewToken, 0, sizeof(WCTemplateToken));
1419 TP.Tokens = NewToken;
1420 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
1421 NewToken->Flags = 0;
1422 NewToken->Line = Line + 1;
1423 NewToken->pTokenStart = pTokenStart;
1424 NewToken->TokenStart = pTokenStart - pStart;
1425 NewToken->TokenEnd = (pTokenEnd - pStart) - NewToken->TokenStart;
1426 NewToken->pTokenEnd = pTokenEnd;
1427 NewToken->NameEnd = NewToken->TokenEnd - 2;
1428 NewToken->PreEval = NULL;
1429 NewToken->FlatToken = NewStrBufPlain(pTokenStart + 2, pTokenEnd - pTokenStart - 2);
1430 StrBufShrinkToFit(NewToken->FlatToken, 1);
1432 StrBufPeek(Buf, pTokenStart, + 1, '\0');
1433 StrBufPeek(Buf, pTokenEnd, -1, '\0');
1434 pch = NewToken->pName = pTokenStart + 2;
1436 NewToken->HaveParameters = 0;;
1437 NewToken->nParameters = 0;
1439 while (pch < pTokenEnd - 1) {
1441 StrBufPeek(Buf, pch, -1, '\0');
1442 NewToken->NameEnd = pch - NewToken->pName;
1444 if (*(pTokenEnd - 1) != ')') {
1446 NULL, "Parseerror", ERR_NAME, &TP,
1447 "Warning, Non welformed Token; missing right parenthesis");
1449 while (pch < pTokenEnd - 1) {
1450 NewToken->nParameters++;
1451 if (GetNextParameter(Buf,
1457 &NewToken->Params[NewToken->nParameters - 1]))
1459 NewToken->HaveParameters = 1;
1460 if (NewToken->nParameters > MAXPARAM) {
1462 NULL, "Parseerror", ERR_NAME, &TP,
1463 "only [%d] Params allowed in Tokens",
1466 FreeToken(&NewToken);
1472 if((NewToken->NameEnd == 1) &&
1473 (NewToken->HaveParameters == 1))
1476 if (*(NewToken->pName) == '_')
1477 NewToken->Flags = SV_GETTEXT;
1478 else if (*(NewToken->pName) == '=')
1479 NewToken->Flags = SV_SUBTEMPL;
1480 else if (*(NewToken->pName) == '%')
1481 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
1482 else if (*(NewToken->pName) == '?')
1483 NewToken->Flags = SV_CONDITIONAL;
1484 else if (*(NewToken->pName) == '!')
1485 NewToken->Flags = SV_NEG_CONDITIONAL;
1491 switch (NewToken->Flags) {
1493 /* If we're able to find out more about the token, do it now while its fresh. */
1494 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1495 HashHandler *Handler;
1496 Handler = (HashHandler*) vVar;
1497 if ((NewToken->nParameters < Handler->Filter.nMinArgs) ||
1498 (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
1500 NULL, "Token", ERR_NAME, &TP,
1501 "doesn't work with %d params",
1502 NewToken->nParameters);
1506 NewToken->PreEval = Handler;
1507 NewToken->Flags = SV_PREEVALUATED;
1508 if (Handler->PreEvalFunc != NULL)
1509 Handler->PreEvalFunc(NewToken);
1514 if (NewToken->nParameters !=1) {
1516 NULL, "Gettext", ERR_NAME, &TP,
1517 "requires exactly 1 parameter, you gave %d params",
1518 NewToken->nParameters);
1519 NewToken->Flags = 0;
1522 if (DumpTemplateI18NStrings) {
1523 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
1527 if (NewToken->nParameters != 1) {
1529 NULL, "Subtemplates", ERR_NAME, &TP,
1530 "require exactly 1 parameter, you gave %d params",
1531 NewToken->nParameters);
1536 /* well, we don't check the mobile stuff here... */
1537 if (!GetHash(LocalTemplateCache,
1538 NewToken->Params[0]->Start,
1539 NewToken->Params[0]->len,
1541 !GetHash(TemplateCache,
1542 NewToken->Params[0]->Start,
1543 NewToken->Params[0]->len,
1546 NULL, "SubTemplate", ERR_PARM1, &TP,
1551 case SV_CUST_STR_CONDITIONAL:
1552 case SV_CONDITIONAL:
1553 case SV_NEG_CONDITIONAL:
1554 if (NewToken->nParameters <2) {
1556 NULL, "Conditional", ERR_PARM1, &TP,
1557 "require at least 2 parameters, you gave %d params",
1558 NewToken->nParameters);
1559 NewToken->Flags = 0;
1562 if (NewToken->Params[1]->lvalue == 0) {
1564 NULL, "Conditional", ERR_PARM1, &TP,
1565 "Conditional ID (Parameter 1) mustn't be 0!");
1566 NewToken->Flags = 0;
1569 if (!GetHash(Conditionals,
1570 NewToken->Params[0]->Start,
1571 NewToken->Params[0]->len,
1574 if ((NewToken->Params[0]->len == 1) &&
1575 (NewToken->Params[0]->Start[0] == 'X'))
1578 NULL, "Conditional", ERR_PARM1, &TP,
1581 NewToken->Error = NewStrBuf();
1584 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1585 NewToken->Params[0]->Start,
1586 ChrPtr(pTmpl->FileName),
1588 ChrPtr(NewToken->FlatToken));
1592 NewToken->PreEval = vVar;
1604 * \brief Display a variable-substituted template
1605 * \param templatename template file to load
1607 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1609 WCTemplate *NewTemplate;
1611 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1612 memset(NewTemplate, 0, sizeof(WCTemplate));
1613 NewTemplate->Data = NULL;
1614 NewTemplate->FileName = NewStrBufDup(filename);
1615 StrBufShrinkToFit(NewTemplate->FileName, 1);
1616 NewTemplate->nTokensUsed = 0;
1617 NewTemplate->TokenSpace = 0;
1618 NewTemplate->Tokens = NULL;
1619 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1620 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1621 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1624 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1625 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1628 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1633 * \brief Display a variable-substituted template
1634 * \param templatename template file to load
1636 void *duplicate_template(WCTemplate *OldTemplate)
1638 WCTemplate *NewTemplate;
1640 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1641 memset(NewTemplate, 0, sizeof(WCTemplate));
1642 NewTemplate->Data = NULL;
1643 NewTemplate->FileName = NewStrBufDup(OldTemplate->FileName);
1644 StrBufShrinkToFit(NewTemplate->FileName, 1);
1645 NewTemplate->nTokensUsed = 0;
1646 NewTemplate->TokenSpace = 0;
1647 NewTemplate->Tokens = NULL;
1648 NewTemplate->MimeType = NewStrBufDup(OldTemplate->MimeType);
1653 * \brief Display a variable-substituted template
1654 * \param templatename template file to load
1656 void *load_template(WCTemplate *NewTemplate)
1659 struct stat statbuf;
1660 const char *pS, *pE, *pch, *Err;
1664 fd = open(ChrPtr(NewTemplate->FileName), O_RDONLY);
1666 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1667 ChrPtr(NewTemplate->FileName), strerror(errno));
1671 if (fstat(fd, &statbuf) == -1) {
1672 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1673 ChrPtr(NewTemplate->FileName), strerror(errno));
1677 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size + 1);
1678 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1680 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1681 ChrPtr(NewTemplate->FileName), strerror(errno));
1682 //FreeWCTemplate(NewTemplate);/////tODO
1688 StrBufShrinkToFit(NewTemplate->Data, 1);
1689 StrBufShrinkToFit(NewTemplate->MimeType, 1);
1690 pS = pch = ChrPtr(NewTemplate->Data);
1691 pE = pS + StrLength(NewTemplate->Data);
1693 const char *pts, *pte;
1695 int InDoubleQuotes = 0;
1697 /** Find one <? > */
1699 for (; pch < pE; pch ++) {
1700 if ((*pch=='<')&&(*(pch + 1)=='?') &&
1701 !((pch == pS) && /* we must ommit a <?xml */
1702 (*(pch + 2) == 'x') &&
1703 (*(pch + 3) == 'm') &&
1704 (*(pch + 4) == 'l')))
1706 if (*pch=='\n') Line ++;
1712 /** Found one? parse it. */
1713 for (; pch <= pE - 1; pch ++) {
1715 InDoubleQuotes = ! InDoubleQuotes;
1716 else if (*pch == '\'')
1717 InQuotes = ! InQuotes;
1718 else if ((!InQuotes && !InDoubleQuotes) &&
1719 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1727 PutNewToken(NewTemplate,
1728 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1735 const char* PrintTemplate(void *vSubst)
1737 WCTemplate *Tmpl = vSubst;
1739 return ChrPtr(Tmpl->FileName);
1743 int LoadTemplateDir(const StrBuf *DirName, HashList *wireless, HashList *big, const StrBuf *BaseKey)
1749 StrBuf *SubDirectory;
1750 DIR *filedir = NULL;
1751 struct dirent *filedir_entry;
1758 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
1763 filedir = opendir (ChrPtr(DirName));
1764 if (filedir == NULL) {
1769 Toplevel = StrLength(BaseKey) == 0;
1770 SubDirectory = NewStrBuf();
1771 SubKey = NewStrBuf();
1772 FileName = NewStrBufPlain(NULL, PATH_MAX);
1774 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
1775 (filedir_entry != NULL))
1779 #ifdef _DIRENT_HAVE_D_NAMELEN
1780 d_namelen = filedir_entry->d_namelen;
1781 d_type = filedir_entry->d_type;
1785 #define DT_UNKNOWN 0
1790 #define IFTODT(mode) (((mode) & 0170000) >> 12)
1791 #define DTTOIF(dirtype) ((dirtype) << 12)
1793 d_namelen = strlen(filedir_entry->d_name);
1794 d_type = DT_UNKNOWN;
1796 d_without_ext = d_namelen;
1798 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1799 continue; /* Ignore backup files... */
1801 if ((d_namelen == 1) &&
1802 (filedir_entry->d_name[0] == '.'))
1805 if ((d_namelen == 2) &&
1806 (filedir_entry->d_name[0] == '.') &&
1807 (filedir_entry->d_name[1] == '.'))
1810 if (d_type == DT_UNKNOWN) {
1812 char path[PATH_MAX];
1813 snprintf(path, PATH_MAX, "%s/%s",
1814 ChrPtr(DirName), filedir_entry->d_name);
1815 if (stat(path, &s) == 0) {
1816 d_type = IFTODT(s.st_mode);
1822 /* Skip directories we are not interested in... */
1823 if (strcmp(filedir_entry->d_name, ".svn") == 0)
1826 FlushStrBuf(SubKey);
1828 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1829 StrBufAppendBuf(SubKey, BaseKey, 0);
1830 StrBufAppendBufPlain(SubKey, HKEY("_"), 0);
1832 StrBufAppendBufPlain(SubKey, filedir_entry->d_name, d_namelen, 0);
1834 FlushStrBuf(SubDirectory);
1835 StrBufAppendBuf(SubDirectory, DirName, 0);
1836 if (ChrPtr(SubDirectory)[StrLength(SubDirectory) - 1] != '/')
1837 StrBufAppendBufPlain(SubDirectory, HKEY("/"), 0);
1838 StrBufAppendBufPlain(SubDirectory, filedir_entry->d_name, d_namelen, 0);
1840 LoadTemplateDir(SubDirectory, wireless, big, SubKey);
1843 case DT_LNK: /* TODO: check whether its a file or a directory */
1847 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1849 if ((d_without_ext == 0) || (d_namelen < 3))
1851 if (((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~') ||
1852 (strcmp(&filedir_entry->d_name[d_without_ext], ".orig") == 0) ||
1853 (strcmp(&filedir_entry->d_name[d_without_ext], ".swp") == 0))
1854 continue; /* Ignore backup files... */
1855 /* .m.xxx is for mobile useragents! */
1857 if (d_without_ext > 2)
1858 IsMobile = (filedir_entry->d_name[d_without_ext - 1] == 'm') &&
1859 (filedir_entry->d_name[d_without_ext - 2] == '.');
1860 PStart = filedir_entry->d_name;
1861 StrBufPrintf(FileName, "%s/%s", ChrPtr(DirName), filedir_entry->d_name);
1862 MinorPtr = strchr(filedir_entry->d_name, '.');
1863 if (MinorPtr != NULL)
1867 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1868 StrBufAppendBuf(Key, BaseKey, 0);
1869 StrBufAppendBufPlain(Key, HKEY("_"), 0);
1871 StrBufAppendBufPlain(Key, filedir_entry->d_name, MinorPtr - filedir_entry->d_name, 0);
1873 if (LoadTemplates >= 1)
1874 lprintf(1, "%s %d %s\n", ChrPtr(FileName), IsMobile, ChrPtr(Key));
1875 prepare_template(FileName, Key, (IsMobile)?wireless:big);
1882 FreeStrBuf(&FileName);
1884 FreeStrBuf(&SubDirectory);
1885 FreeStrBuf(&SubKey);
1889 void InitTemplateCache(void)
1894 HashList *Templates[4];
1899 /* Primary Template set... */
1900 StrBufPrintf(Dir, "%s/t", static_dirs[0]);
1901 LoadTemplateDir(Dir,
1902 WirelessTemplateCache,
1906 /* User local Template set */
1907 StrBufPrintf(Dir, "%s/t", static_dirs[1]);
1908 LoadTemplateDir(Dir,
1909 WirelessLocalTemplateCache,
1913 /* Debug Templates, just to be loaded while debugging. */
1915 StrBufPrintf(Dir, "%s/dbg", static_dirs[0]);
1916 LoadTemplateDir(Dir,
1917 WirelessTemplateCache,
1920 Templates[0] = WirelessTemplateCache;
1921 Templates[1] = TemplateCache;
1922 Templates[2] = WirelessLocalTemplateCache;
1923 Templates[3] = LocalTemplateCache;
1926 if (LoadTemplates == 0)
1927 for (i=0; i < 4; i++) {
1933 At = GetNewHashPos(Templates[i], 0);
1934 while (GetNextHashPos(Templates[i],
1939 (vTemplate != NULL))
1941 load_template((WCTemplate *)vTemplate);
1953 /*-----------------------------------------------------------------------------
1954 * Filling & processing Templates
1957 * \brief executes one token
1958 * \param Target buffer to append to
1959 * \param Token da to process.
1960 * \param Template we're iterating
1961 * \param Context Contextpoointer to pass in
1962 * \param state are we in conditional state?
1963 * \param ContextType what type of information does context giv us?
1965 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1967 const char *AppendMe;
1969 HashHandler *Handler;
1972 /* much output, since pName is not terminated...
1973 lprintf(1,"Doing token: %s\n",Token->pName);
1976 switch (TP->Tokens->Flags) {
1978 TmplGettext(Target, TP);
1980 case SV_CONDITIONAL: /** Forward conditional evaluation */
1981 return EvaluateConditional(Target, 1, state, TP);
1983 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1984 return EvaluateConditional(Target, 0, state, TP);
1986 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1987 if (TP->Tokens->nParameters >= 6) {
1988 if (EvaluateConditional(Target, 0, state, TP)) {
1989 GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
1990 StrBufAppendBufPlain(Target,
1996 GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
1997 StrBufAppendBufPlain(Target,
2005 Target, "Conditional", ERR_NAME, TP,
2006 "needs at least 6 Params!");
2010 if (TP->Tokens->nParameters == 1)
2011 DoTemplate(TKEY(0), Target, TP);
2013 case SV_PREEVALUATED:
2014 Handler = (HashHandler*) TP->Tokens->PreEval;
2015 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
2018 Handler->HandlerFunc(Target, TP);
2021 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
2022 Handler = (HashHandler*) vVar;
2023 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
2027 Handler->HandlerFunc(Target, TP);
2031 print_value_of(Target, TP);
2039 const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
2041 WCTemplate *pTmpl = Tmpl;
2044 const char *pData, *pS;
2046 WCTemplputParams TP;
2048 memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
2050 TP.Context = CallingTP->Context;
2051 TP.ControlContext = CallingTP->ControlContext;
2053 if (LoadTemplates != 0) {
2054 if (LoadTemplates > 1)
2055 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
2056 ChrPtr(Tmpl->FileName));
2057 pTmpl = duplicate_template(Tmpl);
2058 if(load_template(pTmpl) == NULL) {
2061 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
2062 ChrPtr(Tmpl->FileName));
2063 FreeWCTemplate(pTmpl);
2069 pS = pData = ChrPtr(pTmpl->Data);
2070 len = StrLength(pTmpl->Data);
2074 if (i >= pTmpl->nTokensUsed) {
2075 StrBufAppendBufPlain(Target,
2077 len - (pData - pS), 0);
2081 StrBufAppendBufPlain(
2083 pTmpl->Tokens[i]->pTokenStart - pData, 0);
2084 TP.Tokens = pTmpl->Tokens[i];
2085 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2086 state = EvaluateToken(Target, state, &TP);
2088 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
2089 /* condition told us to skip till its end condition */
2091 TP.Tokens = pTmpl->Tokens[i];
2092 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2093 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
2094 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
2095 if (state == EvaluateConditional(
2097 pTmpl->Tokens[i]->Flags,
2103 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
2104 if (i > pTmpl->nTokensUsed)
2108 if (LoadTemplates != 0) {
2109 FreeWCTemplate(pTmpl);
2111 return Tmpl->MimeType;
2116 * \brief Display a variable-substituted template
2117 * \param templatename template file to load
2118 * \returns the mimetype of the template its doing
2120 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
2122 WCTemplputParams LocalTP;
2124 HashList *StaticLocal;
2130 memset(&LocalTP, 0, sizeof(WCTemplputParams));
2134 if (WC->is_mobile > 0) {
2135 Static = WirelessTemplateCache;
2136 StaticLocal = WirelessLocalTemplateCache;
2139 Static = TemplateCache;
2140 StaticLocal = LocalTemplateCache;
2145 lprintf (1, "Can't to load a template with empty name!\n");
2146 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
2150 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
2151 !GetHash(Static, templatename, len, &vTmpl)) {
2152 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
2153 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
2155 (long)strlen(templatename));
2157 dbg_PrintHash(Static, PrintTemplate, NULL);
2158 PrintHash(Static, VarPrintTransition, PrintTemplate);
2164 return ProcessTemplate(vTmpl, Target, TP);
2168 /*-----------------------------------------------------------------------------
2171 typedef struct _HashIterator {
2172 HashList *StaticList;
2173 int AdditionalParams;
2175 int XPectContextType;
2177 RetrieveHashlistFunc GetHash;
2178 HashDestructorFunc Destructor;
2179 SubTemplFunc DoSubTemplate;
2182 void RegisterITERATOR(const char *Name, long len,
2183 int AdditionalParams,
2184 HashList *StaticList,
2185 RetrieveHashlistFunc GetHash,
2186 SubTemplFunc DoSubTempl,
2187 HashDestructorFunc Destructor,
2189 int XPectContextType,
2194 It = (HashIterator*)malloc(sizeof(HashIterator));
2195 memset(It, 0, sizeof(HashIterator));
2196 It->StaticList = StaticList;
2197 It->AdditionalParams = AdditionalParams;
2198 It->GetHash = GetHash;
2199 It->DoSubTemplate = DoSubTempl;
2200 It->Destructor = Destructor;
2201 It->ContextType = ContextType;
2202 It->XPectContextType = XPectContextType;
2204 Put(Iterators, Name, len, It, NULL);
2207 typedef struct _iteratestruct {
2216 int preeval_iterate(WCTemplateToken *Token)
2218 WCTemplputParams TPP;
2219 WCTemplputParams *TP;
2224 memset(&TPP, 0, sizeof(WCTemplputParams));
2227 if (!GetHash(Iterators, TKEY(0), &vIt)) {
2229 NULL, "Iterator", ERR_PARM1, TP,
2233 if (TP->Tokens->Params[1]->Type != TYPE_SUBTEMPLATE) {
2234 LogTemplateError(NULL, "Iterator", ERR_PARM1, TP,
2235 "Need token with type Subtemplate as param 1, have %s",
2236 TP->Tokens->Params[1]->Start);
2239 /* well, we don't check the mobile stuff here... */
2240 if (!GetHash(LocalTemplateCache, TKEY(1), &vTmpl) &&
2241 !GetHash(TemplateCache, TKEY(1), &vTmpl)) {
2242 LogTemplateError(NULL, "SubTemplate", ERR_PARM1, TP,
2243 "referenced here doesn't exist");
2245 Token->Preeval2 = vIt;
2246 It = (HashIterator *) vIt;
2248 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2250 NULL, "Iterator", ERR_PARM1, TP,
2251 "doesn't work with %d params",
2252 TP->Tokens->nParameters);
2259 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
2264 SortStruct *SortBy = NULL;
2266 int DetectGroupChange = 0;
2269 void *vLastContext = NULL;
2271 WCTemplputParams SubTP;
2272 IterateStruct Status;
2278 memset(&Status, 0, sizeof(IterateStruct));
2279 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2281 It = (HashIterator*) TP->Tokens->Preeval2;
2284 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
2288 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2290 Target, "Iterator", ERR_PARM1, TP,
2291 "doesn't work with %d params",
2292 TP->Tokens->nParameters - 1);
2296 if ((It->XPectContextType != CTX_NONE) &&
2297 (It->XPectContextType != TP->Filter.ContextType)) {
2299 Target, "Iterator", ERR_PARM1, TP,
2300 "requires context of type %d, have %d",
2301 It->XPectContextType,
2302 TP->Filter.ContextType);
2307 if (It->StaticList == NULL)
2308 List = It->GetHash(Target, TP);
2310 List = It->StaticList;
2312 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2313 if (DetectGroupChange) {
2314 const StrBuf *BSort;
2315 DetectGroupChange = 0;
2316 if (havebstr("SortBy")) {
2317 BSort = sbstr("SortBy");
2318 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2319 (vSortBy != NULL)) {
2320 SortBy = (SortStruct*)vSortBy;
2321 /** Ok, its us, lets see in which direction we should sort... */
2322 if (havebstr("SortOrder")) {
2324 SortOrder = LBSTR("SortOrder");
2326 DetectGroupChange = 1;
2331 nMembersUsed = GetCount(List);
2332 SubBuf = NewStrBuf();
2333 SubTP.Filter.ContextType = It->ContextType;
2334 SubTP.Filter.ControlContextType = CTX_ITERATE;
2335 SubTP.ControlContext = &Status;
2337 if (HAVE_PARAM(2)) {
2338 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2340 if (HAVE_PARAM(3)) {
2341 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2343 if (HAVE_PARAM(4)) {
2344 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2346 it = GetNewHashPos(List, StepWidth);
2348 StopAt = GetCount(List);
2350 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2351 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2352 if (DetectGroupChange && Status.n > 0) {
2353 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2355 Status.LastN = (Status.n + 1) == nMembersUsed;
2356 SubTP.Context = vContext;
2357 if (It->DoSubTemplate != NULL)
2358 It->DoSubTemplate(SubBuf, &SubTP);
2359 DoTemplate(TKEY(1), SubBuf, &SubTP);
2361 StrBufAppendBuf(Target, SubBuf, 0);
2362 FlushStrBuf(SubBuf);
2363 Status.oddeven = ! Status.oddeven;
2364 vLastContext = vContext;
2368 FreeStrBuf(&SubBuf);
2370 if (It->Destructor != NULL)
2371 It->Destructor(&List);
2375 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2377 IterateStruct *Ctx = CCTX;
2378 if (TP->Tokens->nParameters < 3)
2379 return Ctx->GroupChange;
2381 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2384 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2386 IterateStruct *Ctx = CCTX;
2388 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2390 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2394 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2396 IterateStruct *Ctx = CCTX;
2398 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2402 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2404 IterateStruct *Ctx = CCTX;
2405 StrBufAppendPrintf(Target, "%d", Ctx->n);
2408 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2410 IterateStruct *Ctx = CCTX;
2414 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2416 IterateStruct *Ctx = CCTX;
2422 /*-----------------------------------------------------------------------------
2425 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2427 ConditionalStruct *Cond;
2429 if ((TP->Tokens->Params[0]->len == 1) &&
2430 (TP->Tokens->Params[0]->Start[0] == 'X'))
2431 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2433 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2436 Target, "Conditional", ERR_PARM1, TP,
2441 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2445 if (Cond->CondF(Target, TP) == Neg)
2446 return TP->Tokens->Params[1]->lvalue;
2450 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2455 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2457 subst = (wcsubst*) vsubst;
2459 switch(subst->wcs_type) {
2461 return (subst->wcs_function!=NULL);
2463 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
2467 case WCS_STRBUF_REF:
2468 if (TP->Tokens->nParameters < 4)
2470 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2472 if (TP->Tokens->nParameters < 4)
2473 return (subst->lvalue != 0);
2474 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2476 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2482 void RegisterConditional(const char *Name, long len,
2484 WCConditionalFunc CondF,
2485 int ContextRequired)
2487 ConditionalStruct *Cond;
2489 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2490 memset(Cond, 0, sizeof(ConditionalStruct));
2491 Cond->PlainName = Name;
2492 Cond->Filter.nMaxArgs = nParams;
2493 Cond->Filter.nMinArgs = nParams;
2494 Cond->CondF = CondF;
2495 Cond->Filter.ContextType = ContextRequired;
2496 Cond->Filter.ControlContextType = CTX_NONE;
2497 Put(Conditionals, Name, len, Cond, NULL);
2500 void RegisterControlConditional(const char *Name, long len,
2502 WCConditionalFunc CondF,
2503 int ControlContextRequired)
2505 ConditionalStruct *Cond;
2507 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2508 memset(Cond, 0, sizeof(ConditionalStruct));
2509 Cond->PlainName = Name;
2510 Cond->Filter.nMaxArgs = nParams;
2511 Cond->Filter.nMinArgs = nParams;
2512 Cond->CondF = CondF;
2513 Cond->Filter.ContextType = CTX_NONE;
2514 Cond->Filter.ControlContextType = ControlContextRequired;
2515 Put(Conditionals, Name, len, Cond, NULL);
2518 void RegisterTokenParamDefine(const char *Name, long len,
2523 PVal = (long*)malloc(sizeof(long));
2525 Put(Defines, Name, len, PVal, NULL);
2528 long GetTokenDefine(const char *Name, long len,
2533 if (GetHash(Defines, Name, len, &vPVal) &&
2536 return *(long*) vPVal;
2544 void tmplput_DefStr(StrBuf *Target, WCTemplputParams *TP)
2548 GetTemplateTokenString(Target, TP, 2, &Str, &len);
2550 StrBufAppendBufPlain(Target, Str, len, 0);
2553 void tmplput_DefVal(StrBuf *Target, WCTemplputParams *TP)
2557 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2558 StrBufAppendPrintf(Target, "%d", val);
2563 /*-----------------------------------------------------------------------------
2566 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2568 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2570 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2572 StrBuf *TokenText = (StrBuf*) CTX;
2573 const char *CompareToken;
2576 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2577 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2580 void tmplput_ContextStringArray(StrBuf *Target, WCTemplputParams *TP)
2582 HashList *Arr = (HashList*) CTX;
2586 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2587 if (GetHash(Arr, IKEY(val), &pV) &&
2589 StrBufAppendTemplate(Target, TP, (StrBuf*)pV, 1);
2592 int ConditionalContextStrinArray(StrBuf *Target, WCTemplputParams *TP)
2594 HashList *Arr = (HashList*) CTX;
2597 const char *CompareToken;
2600 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2601 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2602 if (GetHash(Arr, IKEY(val), &pV) &&
2604 return strcmp(ChrPtr((StrBuf*)pV), CompareToken) == 0;
2610 /*-----------------------------------------------------------------------------
2614 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2616 WCTemplputParams SubTP;
2618 StrBuf *Headline = NULL;
2619 if (TP->Tokens->nParameters == 2) {
2620 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2621 Headline = NewStrBuf();
2622 DoTemplate(TKEY(1), Headline, TP);
2627 GetTemplateTokenString(Target,
2632 Headline = NewStrBufPlain(Ch, len);
2635 /* else TODO error? logging? */
2636 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2637 SubTP.Context = Headline;
2638 SubTP.Filter.ContextType = CTX_STRBUF;
2639 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2640 DoTemplate(TKEY(0), Target, TP);
2641 DoTemplate(HKEY("endbox"), Target, TP);
2642 FreeStrBuf(&Headline);
2645 /*-----------------------------------------------------------------------------
2648 int preeval_do_tabbed(WCTemplateToken *Token)
2650 WCTemplputParams TPP;
2651 WCTemplputParams *TP;
2657 memset(&TPP, 0, sizeof(WCTemplputParams));
2660 nTabs = TP->Tokens->nParameters / 2 - 1;
2661 if (TP->Tokens->nParameters % 2 != 0)
2663 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2664 "need even number of arguments");
2668 else for (i = 0; i < nTabs; i++) {
2669 if (!HaveTemplateTokenString(NULL,
2674 (TP->Tokens->Params[i * 2]->len == 0))
2676 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2677 "Tab-Subject %d needs to be able to produce a string, have %s",
2678 i, TP->Tokens->Params[i * 2]->Start);
2681 if (!HaveTemplateTokenString(NULL,
2686 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2688 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2689 "Tab-Content %d needs to be able to produce a string, have %s",
2690 i, TP->Tokens->Params[i * 2 + 1]->Start);
2695 if (!HaveTemplateTokenString(NULL,
2700 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2702 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2703 "Tab-Content %d needs to be able to produce a string, have %s",
2704 i, TP->Tokens->Params[i * 2 + 1]->Start);
2711 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2714 int i, ntabs, nTabs;
2716 nTabs = ntabs = TP->Tokens->nParameters / 2;
2717 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2718 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2720 for (i = 0; i < ntabs; i++) {
2721 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2722 (TP->Tokens->Params[i * 2]->len > 0)) {
2723 TabNames[i] = NewStrBuf();
2724 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2726 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2729 GetTemplateTokenString(Target,
2734 TabNames[i] = NewStrBufPlain(Ch, -1);
2737 /** A Tab without subject? we can't count that, add it as silent */
2742 StrTabbedDialog(Target, nTabs, TabNames);
2743 for (i = 0; i < ntabs; i++) {
2744 StrBeginTab(Target, i, nTabs, TabNames);
2745 DoTemplate(TKEY(i * 2 + 1), Target, TP);
2746 StrEndTab(Target, i, nTabs);
2748 for (i = 0; i < ntabs; i++)
2749 FreeStrBuf(&TabNames[i]);
2753 /*-----------------------------------------------------------------------------
2758 void RegisterSortFunc(const char *name, long len,
2759 const char *prepend, long preplen,
2760 CompareFunc Forward,
2761 CompareFunc Reverse,
2762 CompareFunc GroupChange,
2765 SortStruct *NewSort;
2767 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2768 memset(NewSort, 0, sizeof(SortStruct));
2769 NewSort->Name = NewStrBufPlain(name, len);
2770 if (prepend != NULL)
2771 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2773 NewSort->PrefPrepend = NULL;
2774 NewSort->Forward = Forward;
2775 NewSort->Reverse = Reverse;
2776 NewSort->GroupChange = GroupChange;
2777 NewSort->ContextType = ContextType;
2778 if (ContextType == CTX_NONE) {
2779 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2783 Put(SortHash, name, len, NewSort, DestroySortStruct);
2786 CompareFunc RetrieveSort(WCTemplputParams *TP,
2787 const char *OtherPrefix, long OtherPrefixLen,
2788 const char *Default, long ldefault, long DefaultDirection)
2791 const StrBuf *BSort = NULL;
2794 long SortOrder = -1;
2796 if (havebstr("SortBy")) {
2797 BSort = sbstr("SortBy");
2798 if (OtherPrefix == NULL) {
2799 set_room_pref("sort", NewStrBufDup(BSort), 0);
2802 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2805 else { /** Try to fallback to our remembered values... */
2806 if (OtherPrefix == NULL) {
2807 BSort = get_room_pref("sort");
2810 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2813 putbstr("SortBy", NewStrBufDup(BSort));
2817 BSort = Buf = NewStrBufPlain(Default, ldefault);
2818 putbstr("SortBy", Buf);
2822 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2823 (vSortBy == NULL)) {
2825 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2826 (vSortBy == NULL)) {
2828 NULL, "Sorting", ERR_PARM1, TP,
2829 "Illegal default sort: [%s]", Default);
2833 SortBy = (SortStruct*)vSortBy;
2835 if (SortBy->ContextType != TP->Filter.ContextType)
2838 /** Ok, its us, lets see in which direction we should sort... */
2839 if (havebstr("SortOrder")) {
2840 SortOrder = LBSTR("SortOrder");
2842 else { /** Try to fallback to our remembered values... */
2844 if (SortBy->PrefPrepend == NULL) {
2845 Buf = get_room_pref("SortOrder");
2846 SortOrder = StrTol(Buf);
2849 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2853 SortOrder = DefaultDirection;
2855 Buf = NewStrBufPlain(NULL, 64);
2856 StrBufPrintf(Buf, "%ld", SortOrder);
2857 putbstr("SortOrder", Buf);
2859 switch (SortOrder) {
2864 return SortBy->Forward;
2866 return SortBy->Reverse;
2878 ConstStr SortIcons[] = {
2879 {HKEY("static/sort_none.gif")},
2880 {HKEY("static/up_pointer.gif")},
2881 {HKEY("static/down_pointer.gif")},
2884 ConstStr SortNextOrder[] = {
2891 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2893 int bSortError = eNOT_SPECIFIED;
2894 const StrBuf *BSort;
2899 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2901 return eNO_SUCH_SORT;
2902 *Param = (SortStruct*) vSort;
2905 if (havebstr("SortBy")) {
2906 BSort = sbstr("SortBy");
2907 bSortError = eINVALID_PARAM;
2908 if ((*Param)->PrefPrepend == NULL) {
2909 set_room_pref("sort", NewStrBufDup(BSort), 0);
2912 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2915 else { /** Try to fallback to our remembered values... */
2916 if ((*Param)->PrefPrepend == NULL) {
2917 BSort = get_room_pref("sort");
2920 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2924 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2928 *Next = (SortStruct*) vSort;
2930 /** Ok, its us, lets see in which direction we should sort... */
2931 if (havebstr("SortOrder")) {
2932 *SortOrder = LBSTR("SortOrder");
2934 else { /** Try to fallback to our remembered values... */
2935 if ((*Param)->PrefPrepend == NULL) {
2936 *SortOrder = StrTol(get_room_pref("SortOrder"));
2939 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2949 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2954 const ConstStr *SortIcon;
2956 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2959 Target, "Sorter", ERR_PARM1, TP,
2960 " Sorter [%s] unknown!",
2961 TP->Tokens->Params[0]->Start);
2963 case eINVALID_PARAM:
2964 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2965 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2967 case eNOT_SPECIFIED:
2969 if (Next == Param) {
2970 SortIcon = &SortIcons[SortOrder];
2972 else { /** Not Us... */
2973 SortIcon = &SortIcons[0];
2975 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
2979 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
2985 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2988 Target, "Sorter", ERR_PARM1, TP,
2989 " Sorter [%s] unknown!",
2990 TP->Tokens->Params[0]->Start);
2992 case eINVALID_PARAM:
2994 NULL, "Sorter", ERR_PARM1, TP,
2995 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2997 case eNOT_SPECIFIED:
2999 StrBufAppendBuf(Target, Param->Name, 0);
3004 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
3007 const ConstStr *SortOrderStr;
3011 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
3014 Target, "Sorter", ERR_PARM1, TP,
3015 " Sorter [%s] unknown!",
3016 TP->Tokens->Params[0]->Start);
3018 case eINVALID_PARAM:
3020 NULL, "Sorter", ERR_PARM1, TP,
3021 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
3023 case eNOT_SPECIFIED:
3025 if (Next == Param) {
3026 SortOrderStr = &SortNextOrder[SortOrder];
3028 else { /** Not Us... */
3029 SortOrderStr = &SortNextOrder[0];
3031 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
3036 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
3038 long *LongVector = (long*) CTX;
3040 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
3041 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
3043 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
3047 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
3049 Target, "Longvector", ERR_NAME, TP,
3050 "needs a numerical Parameter!");
3054 Target, "LongVector", ERR_PARM1, TP,
3055 "doesn't have %ld Parameters, its just the size of %ld!",
3056 TP->Tokens->Params[0]->lvalue,
3062 void dbg_print_longvector(long *LongVector)
3064 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
3065 int nItems = LongVector[0];
3068 for (i = 0; i < nItems; i++) {
3070 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
3072 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
3075 lprintf(1, ChrPtr(Buf));
3079 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
3081 long *LongVector = (long*) CTX;
3083 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
3084 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
3085 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
3086 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
3088 return LongVector[TP->Tokens->Params[2]->lvalue] ==
3089 LongVector[TP->Tokens->Params[3]->lvalue];
3093 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
3094 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
3096 Target, "ConditionalLongvector", ERR_PARM1, TP,
3097 "needs two long Parameter!");
3101 Target, "Longvector", ERR_PARM1, TP,
3102 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
3103 TP->Tokens->Params[2]->lvalue,
3104 TP->Tokens->Params[3]->lvalue,
3112 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
3114 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
3121 memset(&NoCtx, 0, sizeof(WCTemplputParams));
3122 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
3123 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
3124 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
3125 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
3126 RegisterNamespace("CONTEXTSTRARR", 1, 2, tmplput_ContextStringArray, NULL, CTX_STRBUFARR);
3127 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
3128 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
3129 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, preeval_do_tabbed, CTX_NONE);
3130 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
3133 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
3134 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
3135 RegisterConditional(HKEY("COND:CONTEXTSTRARR"), 4, ConditionalContextStrinArray, CTX_STRBUFARR);
3136 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
3139 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
3140 conditional_ITERATE_ISGROUPCHANGE,
3142 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
3143 conditional_ITERATE_LASTN,
3145 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
3146 conditional_ITERATE_FIRSTN,
3149 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
3150 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
3151 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
3152 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
3153 RegisterNamespace("DEF:STR", 1, 1, tmplput_DefStr, NULL, CTX_NONE);
3154 RegisterNamespace("DEF:VAL", 1, 1, tmplput_DefVal, NULL, CTX_NONE);
3162 ServerStartModule_SUBST
3165 WirelessTemplateCache = NewHash(1, NULL);
3166 WirelessLocalTemplateCache = NewHash(1, NULL);
3167 LocalTemplateCache = NewHash(1, NULL);
3168 TemplateCache = NewHash(1, NULL);
3170 GlobalNS = NewHash(1, NULL);
3171 Iterators = NewHash(1, NULL);
3172 Conditionals = NewHash(1, NULL);
3173 SortHash = NewHash(1, NULL);
3174 Defines = NewHash(1, NULL);
3178 FinalizeModule_SUBST
3185 ServerShutdownModule_SUBST
3188 DeleteHash(&WirelessTemplateCache);
3189 DeleteHash(&WirelessLocalTemplateCache);
3190 DeleteHash(&TemplateCache);
3191 DeleteHash(&LocalTemplateCache);
3193 DeleteHash(&GlobalNS);
3194 DeleteHash(&Iterators);
3195 DeleteHash(&Conditionals);
3196 DeleteHash(&SortHash);
3197 DeleteHash(&Defines);
3202 SessionNewModule_SUBST
3209 SessionAttachModule_SUBST
3212 sess->vars = NewHash(1,NULL);
3216 SessionDetachModule_SUBST
3219 DeleteHash(&sess->vars);
3220 FreeStrBuf(&sess->WFBuf);
3224 SessionDestroyModule_SUBST