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);
153 case ERR_NAME: /* the main token name... */
154 Err = (TP->Tokens!= NULL)? TP->Tokens->pName:"";
157 Err = ((TP->Tokens!= NULL) &&
158 (TP->Tokens->nParameters > 0))?
159 TP->Tokens->Params[0]->Start : "";
162 Err = ((TP->Tokens!= NULL) &&
163 (TP->Tokens->nParameters > 1))?
164 TP->Tokens->Params[1]->Start : "";
167 if (TP->Tokens != NULL)
169 lprintf(1, "%s [%s] (in '%s' line %ld); %s; [%s]\n",
172 ChrPtr(TP->Tokens->FileName),
175 ChrPtr(TP->Tokens->FlatToken));
179 lprintf(1, "%s: %s;\n",
194 if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
195 if (TP->Tokens != NULL)
200 "<pre>\n%s [%s] (in '%s' line %ld); %s\n[%s]\n</pre>\n",
203 ChrPtr(TP->Tokens->FileName),
206 ChrPtr(TP->Tokens->FlatToken));
208 StrBufPrintf(Info, "%s [%s] %s; [%s]",
212 ChrPtr(TP->Tokens->FlatToken));
215 SerializeJson(WCC->WFBuf, WildFireException(SKEY(TP->Tokens->FileName),
220 SerializeJson(Header, WildFireMessage(SKEY(TP->Tokens->FileName),
232 "<pre>\n%s: %s\n</pre>\n",
237 StrBufAppendBufPlain(Info, Type, -1, 0);
238 StrBufAppendBufPlain(Info, HKEY(" ["), 0);
239 StrBufAppendBufPlain(Info, Err, -1, 0);
240 StrBufAppendBufPlain(Info, HKEY("] "), 0);
241 StrBufAppendBuf(Info, Error, 0);
242 StrBufAppendBufPlain(Info, HKEY("; ["), 0);
243 if (TP->Tokens != NULL)
244 StrBufAppendBuf(Info, TP->Tokens->FlatToken, 0);
245 StrBufAppendBufPlain(Info, HKEY("]"), 0);
246 SerializeJson(WCC->WFBuf, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
251 if (dbg_bactrace_template_errors)
259 void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
269 va_start(arg_ptr, Format);
270 StrBufVAppendPrintf(Error, Format, arg_ptr);
273 lprintf(1, ChrPtr(Error));
276 if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
278 SerializeJson(WCC->WFBuf, WildFireException(Type, strlen(Type),
286 if (dbg_bactrace_template_errors)
292 void RegisterNS(const char *NSName,
296 WCHandlerFunc HandlerFunc,
297 WCPreevalFunc PreevalFunc,
300 HashHandler *NewHandler;
302 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
303 memset(NewHandler, 0, sizeof(HashHandler));
304 NewHandler->Filter.nMinArgs = nMinArgs;
305 NewHandler->Filter.nMaxArgs = nMaxArgs;
306 NewHandler->Filter.ContextType = ContextRequired;
307 NewHandler->Filter.ControlContextType = CTX_NONE;
309 NewHandler->PreEvalFunc = PreevalFunc;
310 NewHandler->HandlerFunc = HandlerFunc;
311 Put(GlobalNS, NSName, len, NewHandler, NULL);
314 void RegisterControlNS(const char *NSName,
318 WCHandlerFunc HandlerFunc,
319 int ControlContextRequired)
321 HashHandler *NewHandler;
323 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
324 memset(NewHandler, 0, sizeof(HashHandler));
325 NewHandler->Filter.nMinArgs = nMinArgs;
326 NewHandler->Filter.nMaxArgs = nMaxArgs;
327 NewHandler->Filter.ContextType = CTX_NONE;
328 NewHandler->Filter.ControlContextType = ControlContextRequired;
329 NewHandler->HandlerFunc = HandlerFunc;
330 Put(GlobalNS, NSName, len, NewHandler, NULL);
335 int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, const char *ErrType)
337 if ((Need->ContextType != CTX_NONE) &&
338 (Need->ContextType != TP->Filter.ContextType)) {
340 Target, ErrType, ERR_PARM1, TP,
341 " WARNING: requires Context: [%s], have [%s]!",
342 ContextName(Need->ContextType),
343 ContextName(TP->Filter.ContextType));
347 if ((Need->ControlContextType != CTX_NONE) &&
348 (Need->ControlContextType != TP->Filter.ControlContextType)) {
350 Target, ErrType, ERR_PARM1, TP,
351 " WARNING: requires Control Context: [%s], have [%s]!",
352 ContextName(Need->ControlContextType),
353 ContextName(TP->Filter.ControlContextType));
357 if (TP->Tokens->nParameters < Need->nMinArgs) {
358 LogTemplateError(Target, ErrType, ERR_NAME, TP,
359 "needs at least %ld params, have %ld",
361 TP->Tokens->nParameters);
365 else if (TP->Tokens->nParameters > Need->nMaxArgs) {
366 LogTemplateError(Target, ErrType, ERR_NAME, TP,
367 "just needs %ld params, you gave %ld",
369 TP->Tokens->nParameters);
377 void FreeToken(WCTemplateToken **Token)
380 FreeStrBuf(&(*Token)->FlatToken);
381 if ((*Token)->HaveParameters)
382 for (i = 0; i < (*Token)->nParameters; i++)
383 free((*Token)->Params[i]);
390 void FreeWCTemplate(void *vFreeMe)
393 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
395 if (FreeMe->TokenSpace > 0) {
396 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
397 FreeToken(&FreeMe->Tokens[i]);
399 free(FreeMe->Tokens);
401 FreeStrBuf(&FreeMe->FileName);
402 FreeStrBuf(&FreeMe->Data);
403 FreeStrBuf(&FreeMe->MimeType);
409 * debugging function to print array to log
411 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
414 * debugging function to print array to log
416 void VarPrintEntry(const char *Key, void *vSubst, int odd)
419 lprintf(1,"Subst[%s] : ", Key);
420 ptr = (wcsubst*) vSubst;
422 switch(ptr->wcs_type) {
424 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
427 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
430 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
437 * Clear out the list of substitution variables local to this session
439 void clear_substs(wcsession *wc) {
441 if (wc->vars != NULL) {
442 DeleteHash(&wc->vars);
447 * Clear out the list of substitution variables local to this session
449 void clear_local_substs(void) {
453 int NeedNewBuf(int type)
467 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
469 int NeedNew = NeedNewBuf(type);
470 switch(ptr->wcs_type) {
473 if (reusestrbuf && NeedNew) {
474 FlushStrBuf(ptr->wcs_value);
478 FreeStrBuf(&ptr->wcs_value);
479 ptr->wcs_value = NULL;
483 ptr->wcs_function = NULL;
484 if (reusestrbuf && NeedNew)
485 ptr->wcs_value = NewStrBuf();
488 ptr->wcs_value = NULL;
489 if (reusestrbuf && NeedNew)
490 ptr->wcs_value = NewStrBuf();
494 if (reusestrbuf && NeedNew)
495 ptr->wcs_value = NewStrBuf();
498 if (reusestrbuf && NeedNew)
499 ptr->wcs_value = NewStrBuf();
506 * destructor; kill one entry.
508 void deletevar(void *data)
510 wcsubst *ptr = (wcsubst*)data;
511 FlushPayload(ptr, 0, ptr->wcs_type);
516 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
521 ptr = (wcsubst *) malloc(sizeof(wcsubst));
522 memset(ptr, 0, sizeof(wcsubst));
524 ptr->wcs_type = type;
525 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
526 Put(WCC->vars, keyname, keylen, ptr, deletevar);
528 switch(ptr->wcs_type) {
542 * Add a substitution variable (local to this session) (strlen version...)
543 * keyname the replacementstring to substitute
544 * keytype the kind of the key
545 * format the format string ala printf
546 * ... the arguments to substitute in the formatstring
548 void SVPRINTF(char *keyname, int keytype, const char *format,...)
556 keylen = strlen(keyname);
558 * First look if we're doing a replacement of
561 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
562 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
563 ptr = (wcsubst*)vPtr;
564 FlushPayload(ptr, keytype, keytype);
565 ptr->wcs_type = keytype;
567 else /** Otherwise allocate a new one */
569 ptr = NewSubstVar(keyname, keylen, keytype);
572 /* Format the string */
573 va_start(arg_ptr, format);
574 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
579 * Add a substitution variable (local to this session)
580 * keyname the replacementstring to substitute
581 * keytype the kind of the key
582 * format the format string ala printf
583 * ... the arguments to substitute in the formatstring
585 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
593 * First look if we're doing a replacement of
596 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
597 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
598 ptr = (wcsubst*)vPtr;
599 FlushPayload(ptr, 1, keytype);
600 ptr->wcs_type = keytype;
602 else /** Otherwise allocate a new one */
604 ptr = NewSubstVar(keyname, keylen, keytype);
607 /** Format the string and save it */
608 va_start(arg_ptr, format);
609 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
614 * Add a substitution variable (local to this session)
615 * keyname the replacementstring to substitute
616 * keytype the kind of the key
617 * format the format string ala printf
618 * ... the arguments to substitute in the formatstring
620 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
628 * First look if we're doing a replacement of
631 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
632 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
633 ptr = (wcsubst*)vPtr;
634 FlushPayload(ptr, 1, keytype);
635 ptr->wcs_type = keytype;
637 else /** Otherwise allocate a new one */
639 ptr = NewSubstVar(keyname, keylen, keytype);
641 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
645 * \brief Add a substitution variable (local to this session)
646 * \param keyname the replacementstring to substitute
647 * \param keytype the kind of the key
648 * \param format the format string ala printf
649 * \param ... the arguments to substitute in the formatstring
651 void SVPutLong(char *keyname, size_t keylen, long Data)
659 * First look if we're doing a replacement of
662 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
663 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
664 ptr = (wcsubst*)vPtr;
665 FlushPayload(ptr, 1, WCS_LONG);
666 ptr->wcs_type = WCS_LONG;
668 else /** Otherwise allocate a new one */
670 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
676 * \brief Add a substitution variable (local to this session) that does a callback
677 * \param keyname the keystring to substitute
678 * \param fcn_ptr the function callback to give the substitution string
680 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
687 * First look if we're doing a replacement of
690 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
691 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
692 ptr = (wcsubst*)vPtr;
693 FlushPayload(ptr, 1, WCS_FUNCTION);
694 ptr->wcs_type = WCS_FUNCTION;
696 else /** Otherwise allocate a new one */
698 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
701 ptr->wcs_function = fcn_ptr;
703 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
705 SVCallback(keyname, strlen(keyname), fcn_ptr);
710 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
717 * First look if we're doing a replacement of
720 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
721 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
722 ptr = (wcsubst*)vPtr;
723 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
724 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
726 else /** Otherwise allocate a new one */
728 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
730 ptr->wcs_value = (StrBuf*)Buf;
733 int HaveTemplateTokenString(StrBuf *Target,
734 WCTemplputParams *TP,
739 if (N >= TP->Tokens->nParameters) {
743 switch (TP->Tokens->Params[N]->Type) {
749 case TYPE_SUBTEMPLATE:
758 void GetTemplateTokenString(StrBuf *Target,
759 WCTemplputParams *TP,
765 WCTemplputParams SubTP;
767 if (N >= TP->Tokens->nParameters) {
768 LogTemplateError(Target,
769 "TokenParameter", N, TP,
770 "invalid token %d. this shouldn't have come till here.\n", N);
776 switch (TP->Tokens->Params[N]->Type) {
780 *Value = TP->Tokens->Params[N]->Start;
781 *len = TP->Tokens->Params[N]->len;
784 if (TP->Tokens->Params[N]->len == 0) {
785 LogTemplateError(Target,
786 "TokenParameter", N, TP,
787 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
792 Buf = (StrBuf*) SBstr(TKEY(N));
793 *Value = ChrPtr(Buf);
794 *len = StrLength(Buf);
797 if (TP->Tokens->Params[N]->len == 0) {
798 LogTemplateError(Target,
799 "TokenParameter", N, TP,
800 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
805 get_PREFERENCE(TKEY(N), &Buf);
806 *Value = ChrPtr(Buf);
807 *len = StrLength(Buf);
810 LogTemplateError(Target,
811 "TokenParameter", N, TP,
812 "Requesting parameter %d; of type LONG, want string.", N);
815 LogTemplateError(Target,
816 "TokenParameter", N, TP,
817 "Requesting parameter %d; of type PREFINT, want string.", N);
820 *Value = _(TP->Tokens->Params[N]->Start);
821 *len = strlen(*Value);
823 case TYPE_SUBTEMPLATE:
824 if (TP->Tokens->Params[N]->len == 0) {
825 LogTemplateError(Target,
826 "TokenParameter", N, TP,
827 "Requesting parameter %d; of type SUBTEMPLATE, empty lookup string not admitted.", N);
833 memset(&SubTP, 0, sizeof(WCTemplputParams *));
834 SubTP.Context = TP->Context;
835 SubTP.Filter.ContextType = TP->Filter.ContextType;
837 DoTemplate(TKEY(N), Buf, &SubTP);
838 *Value = ChrPtr(Buf);
839 *len = StrLength(Buf);
840 /* we can't free it here, so we put it into the subst so its discarded later on. */
841 SVPUTBuf(TKEY(N), Buf, 0);
845 LogTemplateError(Target,
846 "TokenParameter", N, TP,
847 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
852 long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
855 if (N >= TP->Tokens->nParameters) {
856 LogTemplateError(Target,
857 "TokenParameter", N, TP,
858 "invalid token %d. this shouldn't have come till here.\n", N);
863 switch (TP->Tokens->Params[N]->Type) {
866 return atol(TP->Tokens->Params[N]->Start);
869 if (TP->Tokens->Params[N]->len == 0) {
870 LogTemplateError(Target,
871 "TokenParameter", N, TP,
872 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
875 return LBstr(TKEY(N));
878 LogTemplateError(Target,
879 "TokenParameter", N, TP,
880 "requesting a prefstring in param %d want a number", N);
881 if (TP->Tokens->Params[N]->len == 0) {
882 LogTemplateError(Target,
883 "TokenParameter", N, TP,
884 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
887 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
892 return TP->Tokens->Params[N]->lvalue;
894 if (TP->Tokens->Params[N]->len == 0) {
895 LogTemplateError(Target,
896 "TokenParameter", N, TP,
897 "Requesting parameter %d; of type PREFINT, empty lookup string not admitted.", N);
900 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
904 LogTemplateError(Target,
905 "TokenParameter", N, TP,
906 "requesting a I18N string in param %d; want a number", N);
908 case TYPE_SUBTEMPLATE:
909 LogTemplateError(Target,
910 "TokenParameter", N, TP,
911 "requesting a subtemplate in param %d; not supported for numbers", N);
914 LogTemplateError(Target,
915 "TokenParameter", N, TP,
916 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
924 * \brief Print the value of a variable
925 * \param keyname get a key to print
927 void print_value_of(StrBuf *Target, WCTemplputParams *TP)
934 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
935 /* TODO: depricated! */
936 if (TP->Tokens->pName[0] == '=') {
937 DoTemplate(TP->Tokens->pName+1, TP->Tokens->NameEnd - 1, NULL, &NoCtx);
939 /*/////TODO: if param[1] == "U" -> urlescape
941 /** Page-local variables */
942 if ((WCC->vars!= NULL) && GetHash(WCC->vars, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
943 ptr = (wcsubst*) vVar;
944 switch(ptr->wcs_type) {
946 StrBufAppendBuf(Target, ptr->wcs_value, 0);
949 (*ptr->wcs_function) (Target, TP);
953 StrBufAppendBuf(Target, ptr->wcs_value, 0);
956 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
960 Target, "Subst", ERR_NAME, TP,
961 "WARNING: invalid value in SV-Hash at %s!", TP->Tokens->pName);
966 Target, "Token", ERR_NAME, TP,
967 "didn't find Handler \"%s\"", TP->Tokens->pName);
972 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
978 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
979 ParamToLookup->len, &vVar)) {
980 ptr = (wcsubst*) vVar;
981 switch(ptr->wcs_type) {
985 if (ParamToCompare->Type == TYPE_STR)
986 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
987 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
989 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
994 if (ParamToCompare->Type == TYPE_STR)
997 return ParamToCompare->lvalue == ptr->lvalue;
1000 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1001 ParamToLookup->Start);
1007 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
1009 wcsession *WCC = WC;
1013 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1014 ParamToLookup->len, &vVar)) {
1015 ptr = (wcsubst*) vVar;
1016 switch(ptr->wcs_type) {
1019 case WCS_STRBUF_REF:
1020 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
1021 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
1025 return StrTol(Compare) == ptr->lvalue;
1027 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1028 ParamToLookup->Start);
1037 * \brief puts string into the template and computes which escape methon we should use
1038 * \param Source the string we should put into the template
1039 * \param FormatTypeIndex where should we look for escape types if?
1041 void StrBufAppendTemplate(StrBuf *Target,
1042 WCTemplputParams *TP,
1043 const StrBuf *Source, int FormatTypeIndex)
1045 char EscapeAs = ' ';
1047 if ((FormatTypeIndex < TP->Tokens->nParameters) &&
1048 (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
1049 (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
1050 EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
1056 StrEscAppend(Target, Source, NULL, 0, 2);
1059 StrEscAppend(Target, Source, NULL, 0, 0);
1062 StrECMAEscAppend(Target, Source, NULL);
1065 StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 0);
1068 StrBufUrlescAppend(Target, Source, NULL);
1071 StrBufAppendBuf(Target, Source, 0);
1076 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
1078 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
1079 if (Template->TokenSpace <= 0) {
1080 Template->Tokens = (WCTemplateToken**)malloc(
1081 sizeof(WCTemplateToken*) * 10);
1082 memset(Template->Tokens, 0, sizeof(WCTemplateToken*) * 10);
1083 Template->TokenSpace = 10;
1086 WCTemplateToken **NewTokens;
1088 NewTokens= (WCTemplateToken**) malloc(
1089 sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1092 0, sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1096 sizeof(WCTemplateToken*) * Template->nTokensUsed);
1098 free(Template->Tokens);
1099 Template->TokenSpace *= 2;
1100 Template->Tokens = NewTokens;
1103 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
1106 TemplateParam *GetNextParameter(StrBuf *Buf,
1109 WCTemplateToken *Tokens,
1111 WCTemplputParams *TP)
1113 const char *pch = *pCh;
1114 const char *pchs, *pche;
1115 TemplateParam *Parm;
1119 Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
1120 memset(Parm, 0, sizeof(TemplateParam));
1121 Parm->Type = TYPE_STR;
1123 /* Skip leading whitespaces */
1124 while ((*pch == ' ' )||
1127 (*pch == '\n')) pch ++;
1130 Parm->Type = TYPE_PREFSTR;
1137 else if (*pch == ';') {
1138 Parm->Type = TYPE_PREFINT;
1145 else if (*pch == '#') {
1146 Parm->Type = TYPE_INTDEFINE;
1149 else if (*pch == '_') {
1150 Parm->Type = TYPE_GETTEXT;
1157 else if (*pch == 'B') {
1158 Parm->Type = TYPE_BSTR;
1165 else if (*pch == '=') {
1166 Parm->Type = TYPE_SUBTEMPLATE;
1177 else if (*pch == '\'')
1179 if (quote != '\0') {
1184 ( (pch > pchs) && (*(pch - 1) == '\\'))
1189 if (*pch != quote) {
1190 lprintf(1, "Error (in '%s' line %ld); "
1191 "evaluating template param [%s] in Token [%s]\n",
1192 ChrPtr(pTmpl->FileName),
1194 ChrPtr(Tokens->FlatToken),
1201 StrBufPeek(Buf, pch, -1, '\0');
1202 if (LoadTemplates > 1) {
1203 lprintf(1, "DBG: got param [%s] %ld %ld\n",
1204 pchs, pche - pchs, strlen(pchs));
1207 Parm->len = pche - pchs;
1208 pch ++; /* move after trailing quote */
1209 if (ParamBrace && (*pch == ')')) {
1216 Parm->Type = TYPE_LONG;
1218 while ((pch <= pe) &&
1224 if (pch - pchs > 1){
1225 StrBufPeek(Buf, pch, -1, '\0');
1226 Parm->lvalue = atol(pchs);
1233 lprintf(1, "Error (in '%s' line %ld); "
1234 "evaluating long template param [%s] in Token [%s]\n",
1235 ChrPtr(pTmpl->FileName),
1237 ChrPtr(Tokens->FlatToken),
1244 while ((*pch == ' ' )||
1248 (*pch == '\n')) pch ++;
1253 if (DumpTemplateI18NStrings) {
1254 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
1257 case TYPE_INTDEFINE: {
1260 if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
1264 PVal = (long*) vPVal;
1266 Parm->lvalue = *PVal;
1270 LogTemplateError(NULL, "Define", ERR_PARM1, TP,
1275 case TYPE_SUBTEMPLATE:{
1277 /* well, we don't check the mobile stuff here... */
1278 if (!GetHash(LocalTemplateCache, Parm->Start, Parm->len, &vTmpl) &&
1279 !GetHash(TemplateCache, Parm->Start, Parm->len, &vTmpl)) {
1281 NULL, "SubTemplate", ERR_PARM1, TP,
1282 "referenced here doesn't exist");
1290 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
1292 const char *pTokenStart,
1293 const char *pTokenEnd,
1299 TemplateParam *Param;
1300 WCTemplateToken *NewToken;
1301 WCTemplputParams TP;
1303 NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
1304 memset(NewToken, 0, sizeof(WCTemplateToken));
1305 TP.Tokens = NewToken;
1306 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
1307 NewToken->Flags = 0;
1308 NewToken->Line = Line + 1;
1309 NewToken->pTokenStart = pTokenStart;
1310 NewToken->TokenStart = pTokenStart - pStart;
1311 NewToken->TokenEnd = (pTokenEnd - pStart) - NewToken->TokenStart;
1312 NewToken->pTokenEnd = pTokenEnd;
1313 NewToken->NameEnd = NewToken->TokenEnd - 2;
1314 NewToken->PreEval = NULL;
1315 NewToken->FlatToken = NewStrBufPlain(pTokenStart + 2, pTokenEnd - pTokenStart - 2);
1316 StrBufShrinkToFit(NewToken->FlatToken, 1);
1318 StrBufPeek(Buf, pTokenStart, + 1, '\0');
1319 StrBufPeek(Buf, pTokenEnd, -1, '\0');
1320 pch = NewToken->pName = pTokenStart + 2;
1322 NewToken->HaveParameters = 0;;
1323 NewToken->nParameters = 0;
1325 while (pch < pTokenEnd - 1) {
1327 StrBufPeek(Buf, pch, -1, '\0');
1328 NewToken->NameEnd = pch - NewToken->pName;
1330 if (*(pTokenEnd - 1) != ')') {
1332 NULL, "Parseerror", ERR_NAME, &TP,
1333 "Warning, Non welformed Token; missing right parenthesis");
1335 while (pch < pTokenEnd - 1) {
1336 Param = GetNextParameter(Buf, &pch, pTokenEnd - 1, NewToken, pTmpl, &TP);
1337 if (Param != NULL) {
1338 NewToken->HaveParameters = 1;
1339 if (NewToken->nParameters > MAXPARAM) {
1341 NULL, "Parseerror", ERR_NAME, &TP,
1342 "only [%d] Params allowed in Tokens",
1346 FreeToken(&NewToken);
1349 NewToken->Params[NewToken->nParameters++] = Param;
1353 if((NewToken->NameEnd == 1) &&
1354 (NewToken->HaveParameters == 1))
1357 if (*(NewToken->pName) == '_')
1358 NewToken->Flags = SV_GETTEXT;
1359 else if (*(NewToken->pName) == '=')
1360 NewToken->Flags = SV_SUBTEMPL;
1361 else if (*(NewToken->pName) == '%')
1362 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
1363 else if (*(NewToken->pName) == '?')
1364 NewToken->Flags = SV_CONDITIONAL;
1365 else if (*(NewToken->pName) == '!')
1366 NewToken->Flags = SV_NEG_CONDITIONAL;
1372 switch (NewToken->Flags) {
1374 /* If we're able to find out more about the token, do it now while its fresh. */
1375 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1376 HashHandler *Handler;
1377 Handler = (HashHandler*) vVar;
1378 if ((NewToken->nParameters < Handler->Filter.nMinArgs) ||
1379 (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
1381 NULL, "Token", ERR_NAME, &TP,
1382 "doesn't work with %d params",
1383 NewToken->nParameters);
1387 NewToken->PreEval = Handler;
1388 NewToken->Flags = SV_PREEVALUATED;
1389 if (Handler->PreEvalFunc != NULL)
1390 Handler->PreEvalFunc(NewToken);
1395 if (NewToken->nParameters !=1) {
1397 NULL, "Gettext", ERR_NAME, &TP,
1398 "requires exactly 1 parameter, you gave %d params",
1399 NewToken->nParameters);
1400 NewToken->Flags = 0;
1403 if (DumpTemplateI18NStrings) {
1404 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
1408 if (NewToken->nParameters != 1) {
1410 NULL, "Subtemplates", ERR_NAME, &TP,
1411 "require exactly 1 parameter, you gave %d params",
1412 NewToken->nParameters);
1417 /* well, we don't check the mobile stuff here... */
1418 if (!GetHash(LocalTemplateCache,
1419 NewToken->Params[0]->Start,
1420 NewToken->Params[0]->len,
1422 !GetHash(TemplateCache,
1423 NewToken->Params[0]->Start,
1424 NewToken->Params[0]->len,
1427 NULL, "SubTemplate", ERR_PARM1, &TP,
1432 case SV_CUST_STR_CONDITIONAL:
1433 case SV_CONDITIONAL:
1434 case SV_NEG_CONDITIONAL:
1435 if (NewToken->nParameters <2) {
1437 NULL, "Conditional", ERR_PARM1, &TP,
1438 "require at least 2 parameters, you gave %d params",
1439 NewToken->nParameters);
1440 NewToken->Flags = 0;
1443 if (NewToken->Params[1]->lvalue == 0) {
1445 NULL, "Conditional", ERR_PARM1, &TP,
1446 "Conditional ID (Parameter 1) mustn't be 0!");
1447 NewToken->Flags = 0;
1450 if (!GetHash(Conditionals,
1451 NewToken->Params[0]->Start,
1452 NewToken->Params[0]->len,
1455 if ((NewToken->Params[0]->len == 1) &&
1456 (NewToken->Params[0]->Start[0] == 'X'))
1459 NULL, "Conditional", ERR_PARM1, &TP,
1462 NewToken->Error = NewStrBuf();
1465 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1466 NewToken->Params[0]->Start,
1467 ChrPtr(pTmpl->FileName),
1469 ChrPtr(NewToken->FlatToken));
1473 NewToken->PreEval = vVar;
1485 * \brief Display a variable-substituted template
1486 * \param templatename template file to load
1488 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1490 WCTemplate *NewTemplate;
1492 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1493 memset(NewTemplate, 0, sizeof(WCTemplate));
1494 NewTemplate->Data = NULL;
1495 NewTemplate->FileName = NewStrBufDup(filename);
1496 StrBufShrinkToFit(NewTemplate->FileName, 1);
1497 NewTemplate->nTokensUsed = 0;
1498 NewTemplate->TokenSpace = 0;
1499 NewTemplate->Tokens = NULL;
1500 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1501 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1502 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1505 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1506 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1509 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1514 * \brief Display a variable-substituted template
1515 * \param templatename template file to load
1517 void *duplicate_template(WCTemplate *OldTemplate)
1519 WCTemplate *NewTemplate;
1521 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1522 memset(NewTemplate, 0, sizeof(WCTemplate));
1523 NewTemplate->Data = NULL;
1524 NewTemplate->FileName = NewStrBufDup(OldTemplate->FileName);
1525 StrBufShrinkToFit(NewTemplate->FileName, 1);
1526 NewTemplate->nTokensUsed = 0;
1527 NewTemplate->TokenSpace = 0;
1528 NewTemplate->Tokens = NULL;
1529 NewTemplate->MimeType = NewStrBufDup(OldTemplate->MimeType);
1534 * \brief Display a variable-substituted template
1535 * \param templatename template file to load
1537 void *load_template(WCTemplate *NewTemplate)
1540 struct stat statbuf;
1541 const char *pS, *pE, *pch, *Err;
1544 fd = open(ChrPtr(NewTemplate->FileName), O_RDONLY);
1546 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1547 ChrPtr(NewTemplate->FileName), strerror(errno));
1551 if (fstat(fd, &statbuf) == -1) {
1552 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1553 ChrPtr(NewTemplate->FileName), strerror(errno));
1557 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size + 1);
1558 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1560 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1561 ChrPtr(NewTemplate->FileName), strerror(errno));
1562 //FreeWCTemplate(NewTemplate);/////tODO
1568 StrBufShrinkToFit(NewTemplate->Data, 1);
1569 StrBufShrinkToFit(NewTemplate->MimeType, 1);
1570 pS = pch = ChrPtr(NewTemplate->Data);
1571 pE = pS + StrLength(NewTemplate->Data);
1573 const char *pts, *pte;
1575 int InDoubleQuotes = 0;
1577 /** Find one <? > */
1578 for (; pch < pE; pch ++) {
1579 if ((*pch=='<')&&(*(pch + 1)=='?') &&
1580 !((pch == pS) && /* we must ommit a <?xml */
1581 (*(pch + 2) == 'x') &&
1582 (*(pch + 3) == 'm') &&
1583 (*(pch + 4) == 'l')))
1585 if (*pch=='\n') Line ++;
1591 /** Found one? parse it. */
1592 for (; pch <= pE - 1; pch ++) {
1594 InDoubleQuotes = ! InDoubleQuotes;
1595 else if (*pch == '\'')
1596 InQuotes = ! InQuotes;
1597 else if ((!InQuotes && !InDoubleQuotes) &&
1598 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1606 PutNewToken(NewTemplate,
1607 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1614 const char* PrintTemplate(void *vSubst)
1616 WCTemplate *Tmpl = vSubst;
1618 return ChrPtr(Tmpl->FileName);
1622 int LoadTemplateDir(const StrBuf *DirName, HashList *wireless, HashList *big, const StrBuf *BaseKey)
1628 StrBuf *SubDirectory;
1629 DIR *filedir = NULL;
1630 struct dirent *filedir_entry;
1637 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
1642 filedir = opendir (ChrPtr(DirName));
1643 if (filedir == NULL) {
1648 Toplevel = StrLength(BaseKey) == 0;
1649 SubDirectory = NewStrBuf();
1650 SubKey = NewStrBuf();
1651 FileName = NewStrBufPlain(NULL, PATH_MAX);
1653 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
1654 (filedir_entry != NULL))
1657 #ifdef _DIRENT_HAVE_D_NAMELEN
1658 d_namelen = filedir_entry->d_namelen;
1659 d_type = filedir_entry->d_type;
1663 #define DT_UNKNOWN 0
1668 #define IFTODT(mode) (((mode) & 0170000) >> 12)
1669 #define DTTOIF(dirtype) ((dirtype) << 12)
1671 d_namelen = strlen(filedir_entry->d_name);
1672 d_type = DT_UNKNOWN;
1674 d_without_ext = d_namelen;
1676 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1677 continue; /* Ignore backup files... */
1679 if ((d_namelen == 1) &&
1680 (filedir_entry->d_name[0] == '.'))
1683 if ((d_namelen == 2) &&
1684 (filedir_entry->d_name[0] == '.') &&
1685 (filedir_entry->d_name[1] == '.'))
1688 if (d_type == DT_UNKNOWN) {
1690 char path[PATH_MAX];
1691 snprintf(path, PATH_MAX, "%s/%s",
1692 ChrPtr(DirName), filedir_entry->d_name);
1693 if (stat(path, &s) == 0) {
1694 d_type = IFTODT(s.st_mode);
1700 /* Skip directories we are not interested in... */
1701 if (strcmp(filedir_entry->d_name, ".svn") == 0)
1704 FlushStrBuf(SubKey);
1706 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1707 StrBufAppendBuf(SubKey, BaseKey, 0);
1708 StrBufAppendBufPlain(SubKey, HKEY("_"), 0);
1710 StrBufAppendBufPlain(SubKey, filedir_entry->d_name, d_namelen, 0);
1712 FlushStrBuf(SubDirectory);
1713 StrBufAppendBuf(SubDirectory, DirName, 0);
1714 if (ChrPtr(SubDirectory)[StrLength(SubDirectory) - 1] != '/')
1715 StrBufAppendBufPlain(SubDirectory, HKEY("/"), 0);
1716 StrBufAppendBufPlain(SubDirectory, filedir_entry->d_name, d_namelen, 0);
1718 LoadTemplateDir(SubDirectory, wireless, big, SubKey);
1721 case DT_LNK: /* TODO: check whether its a file or a directory */
1725 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1727 if ((d_without_ext == 0) || (d_namelen < 3))
1729 if (((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~') ||
1730 (strcmp(&filedir_entry->d_name[d_without_ext], ".orig") == 0) ||
1731 (strcmp(&filedir_entry->d_name[d_without_ext], ".swp") == 0))
1732 continue; /* Ignore backup files... */
1733 /* .m.xxx is for mobile useragents! */
1735 if (d_without_ext > 2)
1736 IsMobile = (filedir_entry->d_name[d_without_ext - 1] == 'm') &&
1737 (filedir_entry->d_name[d_without_ext - 2] == '.');
1738 StrBufPrintf(FileName, "%s/%s", ChrPtr(DirName), filedir_entry->d_name);
1739 MinorPtr = strchr(filedir_entry->d_name, '.');
1740 if (MinorPtr != NULL)
1744 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1745 StrBufAppendBuf(Key, BaseKey, 0);
1746 StrBufAppendBufPlain(Key, HKEY("_"), 0);
1748 StrBufAppendBufPlain(Key, filedir_entry->d_name, MinorPtr - filedir_entry->d_name, 0);
1750 if (LoadTemplates >= 1)
1751 lprintf(1, "%s %d %s\n", ChrPtr(FileName), IsMobile, ChrPtr(Key));
1752 prepare_template(FileName, Key, (IsMobile)?wireless:big);
1759 FreeStrBuf(&FileName);
1761 FreeStrBuf(&SubDirectory);
1762 FreeStrBuf(&SubKey);
1766 void InitTemplateCache(void)
1771 HashList *Templates[4];
1776 /* Primary Template set... */
1777 StrBufPrintf(Dir, "%s/t", static_dirs[0]);
1778 LoadTemplateDir(Dir,
1779 WirelessTemplateCache,
1783 /* User local Template set */
1784 StrBufPrintf(Dir, "%s/t", static_dirs[1]);
1785 LoadTemplateDir(Dir,
1786 WirelessLocalTemplateCache,
1790 /* Debug Templates, just to be loaded while debugging. */
1792 StrBufPrintf(Dir, "%s/dbg", static_dirs[0]);
1793 LoadTemplateDir(Dir,
1794 WirelessTemplateCache,
1797 Templates[0] = WirelessTemplateCache;
1798 Templates[1] = TemplateCache;
1799 Templates[2] = WirelessLocalTemplateCache;
1800 Templates[3] = LocalTemplateCache;
1803 if (LoadTemplates == 0)
1804 for (i=0; i < 4; i++) {
1810 At = GetNewHashPos(Templates[i], 0);
1811 while (GetNextHashPos(Templates[i],
1816 (vTemplate != NULL))
1818 load_template((WCTemplate *)vTemplate);
1830 /*-----------------------------------------------------------------------------
1831 * Filling & processing Templates
1834 * \brief executes one token
1835 * \param Target buffer to append to
1836 * \param Token da to process.
1837 * \param Template we're iterating
1838 * \param Context Contextpoointer to pass in
1839 * \param state are we in conditional state?
1840 * \param ContextType what type of information does context giv us?
1842 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1844 const char *AppendMe;
1846 HashHandler *Handler;
1849 /* much output, since pName is not terminated...
1850 lprintf(1,"Doing token: %s\n",Token->pName);
1853 switch (TP->Tokens->Flags) {
1855 TmplGettext(Target, TP);
1857 case SV_CONDITIONAL: /** Forward conditional evaluation */
1858 return EvaluateConditional(Target, 1, state, TP);
1860 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1861 return EvaluateConditional(Target, 0, state, TP);
1863 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1864 if (TP->Tokens->nParameters >= 6) {
1865 if (EvaluateConditional(Target, 0, state, TP)) {
1866 GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
1867 StrBufAppendBufPlain(Target,
1873 GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
1874 StrBufAppendBufPlain(Target,
1882 Target, "Conditional", ERR_NAME, TP,
1883 "needs at least 6 Params!");
1887 if (TP->Tokens->nParameters == 1)
1888 DoTemplate(TKEY(0), Target, TP);
1890 case SV_PREEVALUATED:
1891 Handler = (HashHandler*) TP->Tokens->PreEval;
1892 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1895 Handler->HandlerFunc(Target, TP);
1898 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
1899 Handler = (HashHandler*) vVar;
1900 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1904 Handler->HandlerFunc(Target, TP);
1908 print_value_of(Target, TP);
1916 const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
1918 WCTemplate *pTmpl = Tmpl;
1921 const char *pData, *pS;
1923 WCTemplputParams TP;
1925 memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
1927 TP.Context = CallingTP->Context;
1928 TP.ControlContext = CallingTP->ControlContext;
1930 if (LoadTemplates != 0) {
1931 if (LoadTemplates > 1)
1932 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1933 ChrPtr(Tmpl->FileName));
1934 pTmpl = duplicate_template(Tmpl);
1935 if(load_template(pTmpl) == NULL) {
1938 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1939 ChrPtr(Tmpl->FileName));
1940 FreeWCTemplate(pTmpl);
1946 pS = pData = ChrPtr(pTmpl->Data);
1947 len = StrLength(pTmpl->Data);
1951 if (i >= pTmpl->nTokensUsed) {
1952 StrBufAppendBufPlain(Target,
1954 len - (pData - pS), 0);
1958 StrBufAppendBufPlain(
1960 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1961 TP.Tokens = pTmpl->Tokens[i];
1962 TP.nArgs = pTmpl->Tokens[i]->nParameters;
1963 state = EvaluateToken(Target, state, &TP);
1965 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1966 /* condition told us to skip till its end condition */
1968 TP.Tokens = pTmpl->Tokens[i];
1969 TP.nArgs = pTmpl->Tokens[i]->nParameters;
1970 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1971 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1972 if (state == EvaluateConditional(
1974 pTmpl->Tokens[i]->Flags,
1980 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1981 if (i > pTmpl->nTokensUsed)
1985 if (LoadTemplates != 0) {
1986 FreeWCTemplate(pTmpl);
1988 return Tmpl->MimeType;
1993 * \brief Display a variable-substituted template
1994 * \param templatename template file to load
1995 * \returns the mimetype of the template its doing
1997 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
1999 WCTemplputParams LocalTP;
2001 HashList *StaticLocal;
2007 memset(&LocalTP, 0, sizeof(WCTemplputParams));
2011 if (WC->is_mobile > 0) {
2012 Static = WirelessTemplateCache;
2013 StaticLocal = WirelessLocalTemplateCache;
2016 Static = TemplateCache;
2017 StaticLocal = LocalTemplateCache;
2022 lprintf (1, "Can't to load a template with empty name!\n");
2023 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
2027 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
2028 !GetHash(Static, templatename, len, &vTmpl)) {
2029 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
2030 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
2032 (long)strlen(templatename));
2034 dbg_PrintHash(Static, PrintTemplate, NULL);
2035 PrintHash(Static, VarPrintTransition, PrintTemplate);
2041 return ProcessTemplate(vTmpl, Target, TP);
2045 /*-----------------------------------------------------------------------------
2048 typedef struct _HashIterator {
2049 HashList *StaticList;
2050 int AdditionalParams;
2052 int XPectContextType;
2054 RetrieveHashlistFunc GetHash;
2055 HashDestructorFunc Destructor;
2056 SubTemplFunc DoSubTemplate;
2059 void RegisterITERATOR(const char *Name, long len,
2060 int AdditionalParams,
2061 HashList *StaticList,
2062 RetrieveHashlistFunc GetHash,
2063 SubTemplFunc DoSubTempl,
2064 HashDestructorFunc Destructor,
2066 int XPectContextType,
2071 It = (HashIterator*)malloc(sizeof(HashIterator));
2072 memset(It, 0, sizeof(HashIterator));
2073 It->StaticList = StaticList;
2074 It->AdditionalParams = AdditionalParams;
2075 It->GetHash = GetHash;
2076 It->DoSubTemplate = DoSubTempl;
2077 It->Destructor = Destructor;
2078 It->ContextType = ContextType;
2079 It->XPectContextType = XPectContextType;
2081 Put(Iterators, Name, len, It, NULL);
2084 typedef struct _iteratestruct {
2093 int preeval_iterate(WCTemplateToken *Token)
2095 WCTemplputParams TPP;
2096 WCTemplputParams *TP;
2100 memset(&TPP, 0, sizeof(WCTemplputParams));
2103 if (!GetHash(Iterators, TKEY(0), &vIt)) {
2105 NULL, "Iterator", ERR_PARM1, TP,
2109 if (TP->Tokens->Params[1]->Type != TYPE_SUBTEMPLATE) {
2110 LogTemplateError(NULL, "Iterator", ERR_PARM1, TP,
2111 "Need token with type Subtemplate as param 1, have %s",
2112 TP->Tokens->Params[1]->Start);
2115 /* well, we don't check the mobile stuff here... */
2116 if (!GetHash(LocalTemplateCache, TKEY(1), &vTmpl) &&
2117 !GetHash(TemplateCache, TKEY(1), &vTmpl)) {
2118 LogTemplateError(NULL, "SubTemplate", ERR_PARM1, TP,
2119 "referenced here doesn't exist");
2121 Token->Preeval2 = vIt;
2125 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
2130 SortStruct *SortBy = NULL;
2132 int DetectGroupChange = 0;
2135 void *vLastContext = NULL;
2137 WCTemplputParams SubTP;
2138 IterateStruct Status;
2144 memset(&Status, 0, sizeof(IterateStruct));
2145 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2147 It = (HashIterator*) TP->Tokens->Preeval2;
2150 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
2154 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2156 Target, "Iterator", ERR_PARM1, TP,
2157 "doesn't work with %d params",
2158 TP->Tokens->nParameters);
2162 if ((It->XPectContextType != CTX_NONE) &&
2163 (It->XPectContextType != TP->Filter.ContextType)) {
2165 Target, "Iterator", ERR_PARM1, TP,
2166 "requires context of type %s, have %s",
2167 ContextName(It->XPectContextType),
2168 ContextName(TP->Filter.ContextType));
2173 if (It->StaticList == NULL)
2174 List = It->GetHash(Target, TP);
2176 List = It->StaticList;
2178 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2179 if (DetectGroupChange) {
2180 const StrBuf *BSort;
2181 DetectGroupChange = 0;
2182 if (havebstr("SortBy")) {
2183 BSort = sbstr("SortBy");
2184 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2185 (vSortBy != NULL)) {
2186 SortBy = (SortStruct*)vSortBy;
2187 /* first check whether its intended for us... */
2188 if ((SortBy->ContextType == It->ContextType)&&
2189 /** Ok, its us, lets see in which direction we should sort... */
2190 (havebstr("SortOrder"))) {
2192 SortOrder = LBSTR("SortOrder");
2194 DetectGroupChange = 1;
2199 nMembersUsed = GetCount(List);
2200 SubBuf = NewStrBuf();
2201 SubTP.Filter.ContextType = It->ContextType;
2202 SubTP.Filter.ControlContextType = CTX_ITERATE;
2203 SubTP.ControlContext = &Status;
2205 if (HAVE_PARAM(2)) {
2206 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2208 if (HAVE_PARAM(3)) {
2209 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2211 if (HAVE_PARAM(4)) {
2212 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2214 it = GetNewHashPos(List, StepWidth);
2216 StopAt = GetCount(List);
2218 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2219 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2220 if (DetectGroupChange && Status.n > 0) {
2221 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2223 Status.LastN = (Status.n + 1) == nMembersUsed;
2224 SubTP.Context = vContext;
2225 if (It->DoSubTemplate != NULL)
2226 It->DoSubTemplate(SubBuf, &SubTP);
2227 DoTemplate(TKEY(1), SubBuf, &SubTP);
2229 StrBufAppendBuf(Target, SubBuf, 0);
2230 FlushStrBuf(SubBuf);
2231 Status.oddeven = ! Status.oddeven;
2232 vLastContext = vContext;
2236 FreeStrBuf(&SubBuf);
2238 if (It->Destructor != NULL)
2239 It->Destructor(&List);
2243 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2245 IterateStruct *Ctx = CCTX;
2246 if (TP->Tokens->nParameters < 3)
2247 return Ctx->GroupChange;
2249 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2252 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2254 IterateStruct *Ctx = CCTX;
2256 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2258 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2262 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2264 IterateStruct *Ctx = CCTX;
2266 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2270 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2272 IterateStruct *Ctx = CCTX;
2273 StrBufAppendPrintf(Target, "%d", Ctx->n);
2276 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2278 IterateStruct *Ctx = CCTX;
2282 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2284 IterateStruct *Ctx = CCTX;
2290 /*-----------------------------------------------------------------------------
2293 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2295 ConditionalStruct *Cond;
2297 if ((TP->Tokens->Params[0]->len == 1) &&
2298 (TP->Tokens->Params[0]->Start[0] == 'X'))
2299 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2301 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2304 Target, "Conditional", ERR_PARM1, TP,
2309 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2313 if (Cond->CondF(Target, TP) == Neg)
2314 return TP->Tokens->Params[1]->lvalue;
2318 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2323 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2325 subst = (wcsubst*) vsubst;
2327 switch(subst->wcs_type) {
2329 return (subst->wcs_function!=NULL);
2332 case WCS_STRBUF_REF:
2333 if (TP->Tokens->nParameters < 4)
2335 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2337 if (TP->Tokens->nParameters < 4)
2338 return (subst->lvalue != 0);
2339 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2341 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2347 void RegisterConditional(const char *Name, long len,
2349 WCConditionalFunc CondF,
2350 int ContextRequired)
2352 ConditionalStruct *Cond;
2354 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2355 memset(Cond, 0, sizeof(ConditionalStruct));
2356 Cond->PlainName = Name;
2357 Cond->Filter.nMaxArgs = nParams;
2358 Cond->Filter.nMinArgs = nParams;
2359 Cond->CondF = CondF;
2360 Cond->Filter.ContextType = ContextRequired;
2361 Cond->Filter.ControlContextType = CTX_NONE;
2362 Put(Conditionals, Name, len, Cond, NULL);
2365 void RegisterControlConditional(const char *Name, long len,
2367 WCConditionalFunc CondF,
2368 int ControlContextRequired)
2370 ConditionalStruct *Cond;
2372 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2373 memset(Cond, 0, sizeof(ConditionalStruct));
2374 Cond->PlainName = Name;
2375 Cond->Filter.nMaxArgs = nParams;
2376 Cond->Filter.nMinArgs = nParams;
2377 Cond->CondF = CondF;
2378 Cond->Filter.ContextType = CTX_NONE;
2379 Cond->Filter.ControlContextType = ControlContextRequired;
2380 Put(Conditionals, Name, len, Cond, NULL);
2383 void RegisterTokenParamDefine(const char *Name, long len,
2388 PVal = (long*)malloc(sizeof(long));
2390 Put(Defines, Name, len, PVal, NULL);
2393 long GetTokenDefine(const char *Name, long len,
2398 if (GetHash(Defines, Name, len, &vPVal) &&
2401 return *(long*) vPVal;
2409 void tmplput_DefStr(StrBuf *Target, WCTemplputParams *TP)
2413 GetTemplateTokenString(Target, TP, 2, &Str, &len);
2415 StrBufAppendBufPlain(Target, Str, len, 0);
2418 void tmplput_DefVal(StrBuf *Target, WCTemplputParams *TP)
2422 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2423 StrBufAppendPrintf(Target, "%d", val);
2428 /*-----------------------------------------------------------------------------
2431 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2433 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2435 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2437 StrBuf *TokenText = (StrBuf*) CTX;
2438 const char *CompareToken;
2441 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2442 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2445 void tmplput_ContextStringArray(StrBuf *Target, WCTemplputParams *TP)
2447 HashList *Arr = (HashList*) CTX;
2451 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2452 if (GetHash(Arr, IKEY(val), &pV) &&
2454 StrBufAppendTemplate(Target, TP, (StrBuf*)pV, 1);
2457 int ConditionalContextStrinArray(StrBuf *Target, WCTemplputParams *TP)
2459 HashList *Arr = (HashList*) CTX;
2462 const char *CompareToken;
2465 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2466 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2467 if (GetHash(Arr, IKEY(val), &pV) &&
2469 return strcmp(ChrPtr((StrBuf*)pV), CompareToken) == 0;
2475 /*-----------------------------------------------------------------------------
2479 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2481 WCTemplputParams SubTP;
2483 StrBuf *Headline = NULL;
2484 if (TP->Tokens->nParameters == 2) {
2485 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2486 Headline = NewStrBuf();
2487 DoTemplate(TKEY(1), Headline, TP);
2492 GetTemplateTokenString(Target,
2497 Headline = NewStrBufPlain(Ch, len);
2500 /* else TODO error? logging? */
2501 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2502 SubTP.Context = Headline;
2503 SubTP.Filter.ContextType = CTX_STRBUF;
2504 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2505 DoTemplate(TKEY(0), Target, TP);
2506 DoTemplate(HKEY("endbox"), Target, TP);
2507 FreeStrBuf(&Headline);
2510 /*-----------------------------------------------------------------------------
2513 int preeval_do_tabbed(WCTemplateToken *Token)
2515 WCTemplputParams TPP;
2516 WCTemplputParams *TP;
2522 memset(&TPP, 0, sizeof(WCTemplputParams));
2525 nTabs = TP->Tokens->nParameters / 2 - 1;
2526 if (TP->Tokens->nParameters % 2 != 0)
2528 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2529 "need even number of arguments");
2533 else for (i = 0; i < nTabs; i++) {
2534 if (!HaveTemplateTokenString(NULL,
2539 (TP->Tokens->Params[i * 2]->len == 0))
2541 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2542 "Tab-Subject %d needs to be able to produce a string, have %s",
2543 i, TP->Tokens->Params[i * 2]->Start);
2546 if (!HaveTemplateTokenString(NULL,
2551 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2553 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2554 "Tab-Content %d needs to be able to produce a string, have %s",
2555 i, TP->Tokens->Params[i * 2 + 1]->Start);
2560 if (!HaveTemplateTokenString(NULL,
2565 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2567 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2568 "Tab-Content %d needs to be able to produce a string, have %s",
2569 i, TP->Tokens->Params[i * 2 + 1]->Start);
2576 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2579 int i, ntabs, nTabs;
2581 nTabs = ntabs = TP->Tokens->nParameters / 2;
2582 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2583 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2585 for (i = 0; i < ntabs; i++) {
2586 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2587 (TP->Tokens->Params[i * 2]->len > 0)) {
2588 TabNames[i] = NewStrBuf();
2589 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2591 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2594 GetTemplateTokenString(Target,
2599 TabNames[i] = NewStrBufPlain(Ch, -1);
2602 /** A Tab without subject? we can't count that, add it as silent */
2607 StrTabbedDialog(Target, nTabs, TabNames);
2608 for (i = 0; i < ntabs; i++) {
2609 StrBeginTab(Target, i, nTabs);
2610 DoTemplate(TKEY(i * 2 + 1), Target, TP);
2611 StrEndTab(Target, i, nTabs);
2613 for (i = 0; i < ntabs; i++)
2614 FreeStrBuf(&TabNames[i]);
2618 /*-----------------------------------------------------------------------------
2623 void RegisterSortFunc(const char *name, long len,
2624 const char *prepend, long preplen,
2625 CompareFunc Forward,
2626 CompareFunc Reverse,
2627 CompareFunc GroupChange,
2630 SortStruct *NewSort;
2632 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2633 memset(NewSort, 0, sizeof(SortStruct));
2634 NewSort->Name = NewStrBufPlain(name, len);
2635 if (prepend != NULL)
2636 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2638 NewSort->PrefPrepend = NULL;
2639 NewSort->Forward = Forward;
2640 NewSort->Reverse = Reverse;
2641 NewSort->GroupChange = GroupChange;
2642 NewSort->ContextType = ContextType;
2643 if (ContextType == CTX_NONE) {
2644 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2648 Put(SortHash, name, len, NewSort, DestroySortStruct);
2651 CompareFunc RetrieveSort(WCTemplputParams *TP,
2652 const char *OtherPrefix, long OtherPrefixLen,
2653 const char *Default, long ldefault, long DefaultDirection)
2655 const StrBuf *BSort = NULL;
2658 long SortOrder = -1;
2660 if (havebstr("SortBy")) {
2661 BSort = sbstr("SortBy");
2662 if (OtherPrefix == NULL) {
2663 set_room_pref("sort", NewStrBufDup(BSort), 0);
2666 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2669 else { /** Try to fallback to our remembered values... */
2670 if (OtherPrefix == NULL) {
2671 BSort = get_room_pref("sort");
2674 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2677 putbstr("SortBy", NewStrBufDup(BSort));
2681 BSort = Buf = NewStrBufPlain(Default, ldefault);
2682 putbstr("SortBy", Buf);
2686 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2687 (vSortBy == NULL)) {
2688 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2689 (vSortBy == NULL)) {
2691 NULL, "Sorting", ERR_PARM1, TP,
2692 "Illegal default sort: [%s]", Default);
2696 SortBy = (SortStruct*)vSortBy;
2698 if (SortBy->ContextType != TP->Filter.ContextType)
2701 /** Ok, its us, lets see in which direction we should sort... */
2702 if (havebstr("SortOrder")) {
2703 SortOrder = LBSTR("SortOrder");
2705 else { /** Try to fallback to our remembered values... */
2707 if (SortBy->PrefPrepend == NULL) {
2708 Buf = get_room_pref("SortOrder");
2709 SortOrder = StrTol(Buf);
2712 /* TODO: this is never used??? */
2713 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2717 SortOrder = DefaultDirection;
2719 Buf = NewStrBufPlain(NULL, 64);
2720 StrBufPrintf(Buf, "%ld", SortOrder);
2721 putbstr("SortOrder", Buf);
2723 switch (SortOrder) {
2728 return SortBy->Forward;
2730 return SortBy->Reverse;
2742 ConstStr SortIcons[] = {
2743 {HKEY("static/sort_none.gif")},
2744 {HKEY("static/up_pointer.gif")},
2745 {HKEY("static/down_pointer.gif")},
2748 ConstStr SortNextOrder[] = {
2755 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2757 int bSortError = eNOT_SPECIFIED;
2758 const StrBuf *BSort;
2763 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2765 return eNO_SUCH_SORT;
2766 *Param = (SortStruct*) vSort;
2769 if (havebstr("SortBy")) {
2770 BSort = sbstr("SortBy");
2771 bSortError = eINVALID_PARAM;
2772 if ((*Param)->PrefPrepend == NULL) {
2773 set_room_pref("sort", NewStrBufDup(BSort), 0);
2776 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2779 else { /** Try to fallback to our remembered values... */
2780 if ((*Param)->PrefPrepend == NULL) {
2781 BSort = get_room_pref("sort");
2784 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2788 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2792 *Next = (SortStruct*) vSort;
2794 /** Ok, its us, lets see in which direction we should sort... */
2795 if (havebstr("SortOrder")) {
2796 *SortOrder = LBSTR("SortOrder");
2798 else { /** Try to fallback to our remembered values... */
2799 if ((*Param)->PrefPrepend == NULL) {
2800 *SortOrder = StrTol(get_room_pref("SortOrder"));
2803 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2813 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2818 const ConstStr *SortIcon;
2820 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2823 Target, "Sorter", ERR_PARM1, TP,
2824 " Sorter [%s] unknown!",
2825 TP->Tokens->Params[0]->Start);
2827 case eINVALID_PARAM:
2828 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2829 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2831 case eNOT_SPECIFIED:
2833 if (Next == Param) {
2834 SortIcon = &SortIcons[SortOrder];
2836 else { /** Not Us... */
2837 SortIcon = &SortIcons[0];
2839 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
2843 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
2849 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2852 Target, "Sorter", ERR_PARM1, TP,
2853 " Sorter [%s] unknown!",
2854 TP->Tokens->Params[0]->Start);
2856 case eINVALID_PARAM:
2858 NULL, "Sorter", ERR_PARM1, TP,
2859 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2861 case eNOT_SPECIFIED:
2863 StrBufAppendBuf(Target, Param->Name, 0);
2868 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
2871 const ConstStr *SortOrderStr;
2875 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2878 Target, "Sorter", ERR_PARM1, TP,
2879 " Sorter [%s] unknown!",
2880 TP->Tokens->Params[0]->Start);
2882 case eINVALID_PARAM:
2884 NULL, "Sorter", ERR_PARM1, TP,
2885 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2887 case eNOT_SPECIFIED:
2889 if (Next == Param) {
2890 SortOrderStr = &SortNextOrder[SortOrder];
2892 else { /** Not Us... */
2893 SortOrderStr = &SortNextOrder[0];
2895 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2900 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
2902 long *LongVector = (long*) CTX;
2904 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
2905 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
2907 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
2911 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
2913 Target, "Longvector", ERR_NAME, TP,
2914 "needs a numerical Parameter!");
2918 Target, "LongVector", ERR_PARM1, TP,
2919 "doesn't have %ld Parameters, its just the size of %ld!",
2920 TP->Tokens->Params[0]->lvalue,
2926 void dbg_print_longvector(long *LongVector)
2928 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
2929 int nItems = LongVector[0];
2932 for (i = 0; i < nItems; i++) {
2934 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
2936 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
2939 lprintf(1, ChrPtr(Buf));
2943 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
2945 long *LongVector = (long*) CTX;
2947 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
2948 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
2949 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
2950 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
2952 return LongVector[TP->Tokens->Params[2]->lvalue] ==
2953 LongVector[TP->Tokens->Params[3]->lvalue];
2957 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
2958 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
2960 Target, "ConditionalLongvector", ERR_PARM1, TP,
2961 "needs two long Parameter!");
2965 Target, "Longvector", ERR_PARM1, TP,
2966 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
2967 TP->Tokens->Params[2]->lvalue,
2968 TP->Tokens->Params[3]->lvalue,
2976 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
2978 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
2985 memset(&NoCtx, 0, sizeof(WCTemplputParams));
2986 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
2987 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
2988 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
2989 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
2990 RegisterNamespace("CONTEXTSTRARR", 1, 2, tmplput_ContextStringArray, NULL, CTX_STRBUF);
2991 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
2992 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
2993 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, preeval_do_tabbed, CTX_NONE);
2994 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
2997 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
2998 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
2999 RegisterConditional(HKEY("COND:CONTEXTSTRARR"), 4, ConditionalContextStrinArray, CTX_STRBUFARR);
3000 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
3002 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
3003 conditional_ITERATE_ISGROUPCHANGE,
3005 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
3006 conditional_ITERATE_LASTN,
3008 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
3009 conditional_ITERATE_FIRSTN,
3012 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
3013 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
3014 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
3015 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
3016 RegisterNamespace("DEF:STR", 1, 1, tmplput_DefStr, NULL, CTX_NONE);
3017 RegisterNamespace("DEF:VAL", 1, 1, tmplput_DefVal, NULL, CTX_NONE);
3025 ServerStartModule_SUBST
3028 WirelessTemplateCache = NewHash(1, NULL);
3029 WirelessLocalTemplateCache = NewHash(1, NULL);
3030 LocalTemplateCache = NewHash(1, NULL);
3031 TemplateCache = NewHash(1, NULL);
3033 GlobalNS = NewHash(1, NULL);
3034 Iterators = NewHash(1, NULL);
3035 Conditionals = NewHash(1, NULL);
3036 SortHash = NewHash(1, NULL);
3037 Defines = NewHash(1, NULL);
3041 FinalizeModule_SUBST
3048 ServerShutdownModule_SUBST
3051 DeleteHash(&WirelessTemplateCache);
3052 DeleteHash(&WirelessLocalTemplateCache);
3053 DeleteHash(&TemplateCache);
3054 DeleteHash(&LocalTemplateCache);
3056 DeleteHash(&GlobalNS);
3057 DeleteHash(&Iterators);
3058 DeleteHash(&Conditionals);
3059 DeleteHash(&SortHash);
3060 DeleteHash(&Defines);
3065 SessionNewModule_SUBST
3072 SessionAttachModule_SUBST
3075 sess->vars = NewHash(1,NULL);
3079 SessionDetachModule_SUBST
3082 DeleteHash(&sess->vars);
3083 FreeStrBuf(&sess->WFBuf);
3087 SessionDestroyModule_SUBST