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",
236 StrBufPrintf(Info, "%s [%s] %s; [%s]",
240 ChrPtr(TP->Tokens->FlatToken));
241 SerializeJson(WCC->WFBuf, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
246 if (dbg_bactrace_template_errors)
254 void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
264 va_start(arg_ptr, Format);
265 StrBufVAppendPrintf(Error, Format, arg_ptr);
268 lprintf(1, ChrPtr(Error));
271 if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
273 SerializeJson(WCC->WFBuf, WildFireException(Type, strlen(Type),
281 if (dbg_bactrace_template_errors)
287 void RegisterNS(const char *NSName,
291 WCHandlerFunc HandlerFunc,
292 WCPreevalFunc PreevalFunc,
295 HashHandler *NewHandler;
297 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
298 memset(NewHandler, 0, sizeof(HashHandler));
299 NewHandler->Filter.nMinArgs = nMinArgs;
300 NewHandler->Filter.nMaxArgs = nMaxArgs;
301 NewHandler->Filter.ContextType = ContextRequired;
302 NewHandler->Filter.ControlContextType = CTX_NONE;
304 NewHandler->PreEvalFunc = PreevalFunc;
305 NewHandler->HandlerFunc = HandlerFunc;
306 Put(GlobalNS, NSName, len, NewHandler, NULL);
309 void RegisterControlNS(const char *NSName,
313 WCHandlerFunc HandlerFunc,
314 int ControlContextRequired)
316 HashHandler *NewHandler;
318 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
319 memset(NewHandler, 0, sizeof(HashHandler));
320 NewHandler->Filter.nMinArgs = nMinArgs;
321 NewHandler->Filter.nMaxArgs = nMaxArgs;
322 NewHandler->Filter.ContextType = CTX_NONE;
323 NewHandler->Filter.ControlContextType = ControlContextRequired;
324 NewHandler->HandlerFunc = HandlerFunc;
325 Put(GlobalNS, NSName, len, NewHandler, NULL);
330 int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, const char *ErrType)
332 if ((Need->ContextType != CTX_NONE) &&
333 (Need->ContextType != TP->Filter.ContextType)) {
335 Target, ErrType, ERR_PARM1, TP,
336 " WARNING: requires Context: [%s], have [%s]!",
337 ContextName(Need->ContextType),
338 ContextName(TP->Filter.ContextType));
342 if ((Need->ControlContextType != CTX_NONE) &&
343 (Need->ControlContextType != TP->Filter.ControlContextType)) {
345 Target, ErrType, ERR_PARM1, TP,
346 " WARNING: requires Control Context: [%s], have [%s]!",
347 ContextName(Need->ControlContextType),
348 ContextName(TP->Filter.ControlContextType));
352 if (TP->Tokens->nParameters < Need->nMinArgs) {
353 LogTemplateError(Target, ErrType, ERR_NAME, TP,
354 "needs at least %ld params, have %ld",
356 TP->Tokens->nParameters);
360 else if (TP->Tokens->nParameters > Need->nMaxArgs) {
361 LogTemplateError(Target, ErrType, ERR_NAME, TP,
362 "just needs %ld params, you gave %ld",
364 TP->Tokens->nParameters);
372 void FreeToken(WCTemplateToken **Token)
375 FreeStrBuf(&(*Token)->FlatToken);
376 if ((*Token)->HaveParameters)
377 for (i = 0; i < (*Token)->nParameters; i++)
378 free((*Token)->Params[i]);
385 void FreeWCTemplate(void *vFreeMe)
388 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
390 if (FreeMe->TokenSpace > 0) {
391 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
392 FreeToken(&FreeMe->Tokens[i]);
394 free(FreeMe->Tokens);
396 FreeStrBuf(&FreeMe->FileName);
397 FreeStrBuf(&FreeMe->Data);
398 FreeStrBuf(&FreeMe->MimeType);
404 * debugging function to print array to log
406 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
409 * debugging function to print array to log
411 void VarPrintEntry(const char *Key, void *vSubst, int odd)
414 lprintf(1,"Subst[%s] : ", Key);
415 ptr = (wcsubst*) vSubst;
417 switch(ptr->wcs_type) {
419 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
422 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
425 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
428 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
435 * Clear out the list of substitution variables local to this session
437 void clear_substs(wcsession *wc) {
439 if (wc->vars != NULL) {
440 DeleteHash(&wc->vars);
445 * Clear out the list of substitution variables local to this session
447 void clear_local_substs(void) {
466 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
468 int NeedNew = NeedNewBuf(type);
469 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) {
531 ptr->wcs_value = NewStrBuf();
545 * Add a substitution variable (local to this session) (strlen version...)
546 * keyname the replacementstring to substitute
547 * keytype the kind of the key
548 * format the format string ala printf
549 * ... the arguments to substitute in the formatstring
551 void SVPRINTF(char *keyname, int keytype, const char *format,...)
559 keylen = strlen(keyname);
561 * First look if we're doing a replacement of
564 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
565 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
566 ptr = (wcsubst*)vPtr;
567 FlushPayload(ptr, keytype, keytype);
568 ptr->wcs_type = keytype;
570 else /** Otherwise allocate a new one */
572 ptr = NewSubstVar(keyname, keylen, keytype);
575 /* Format the string */
576 va_start(arg_ptr, format);
577 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
582 * Add a substitution variable (local to this session)
583 * keyname the replacementstring to substitute
584 * keytype the kind of the key
585 * format the format string ala printf
586 * ... the arguments to substitute in the formatstring
588 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
596 * First look if we're doing a replacement of
599 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
600 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
601 ptr = (wcsubst*)vPtr;
602 FlushPayload(ptr, 1, keytype);
603 ptr->wcs_type = keytype;
605 else /** Otherwise allocate a new one */
607 ptr = NewSubstVar(keyname, keylen, keytype);
610 /** Format the string and save it */
611 va_start(arg_ptr, format);
612 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
617 * Add a substitution variable (local to this session)
618 * keyname the replacementstring to substitute
619 * keytype the kind of the key
620 * format the format string ala printf
621 * ... the arguments to substitute in the formatstring
623 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
631 * First look if we're doing a replacement of
634 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
635 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
636 ptr = (wcsubst*)vPtr;
637 FlushPayload(ptr, 1, keytype);
638 ptr->wcs_type = keytype;
640 else /** Otherwise allocate a new one */
642 ptr = NewSubstVar(keyname, keylen, keytype);
644 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
648 * \brief Add a substitution variable (local to this session)
649 * \param keyname the replacementstring to substitute
650 * \param keytype the kind of the key
651 * \param format the format string ala printf
652 * \param ... the arguments to substitute in the formatstring
654 void SVPutLong(char *keyname, size_t keylen, long Data)
662 * First look if we're doing a replacement of
665 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
666 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
667 ptr = (wcsubst*)vPtr;
668 FlushPayload(ptr, 1, WCS_LONG);
669 ptr->wcs_type = WCS_LONG;
671 else /** Otherwise allocate a new one */
673 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
679 * \brief Add a substitution variable (local to this session) that does a callback
680 * \param keyname the keystring to substitute
681 * \param fcn_ptr the function callback to give the substitution string
683 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
690 * First look if we're doing a replacement of
693 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
694 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
695 ptr = (wcsubst*)vPtr;
696 FlushPayload(ptr, 1, WCS_FUNCTION);
697 ptr->wcs_type = WCS_FUNCTION;
699 else /** Otherwise allocate a new one */
701 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
704 ptr->wcs_function = fcn_ptr;
706 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
708 SVCallback(keyname, strlen(keyname), fcn_ptr);
713 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
720 * First look if we're doing a replacement of
723 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
724 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
725 ptr = (wcsubst*)vPtr;
726 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
727 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
729 else /** Otherwise allocate a new one */
731 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
733 ptr->wcs_value = (StrBuf*)Buf;
737 * \brief back end for print_value_of() ... does a server command
738 * \param servcmd server command to execute on the citadel server
740 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
744 serv_puts(ChrPtr(servcmd));
745 len = serv_getln(buf, sizeof buf);
751 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
754 _fmout(Target, "CENTER");
757 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
763 int HaveTemplateTokenString(StrBuf *Target,
764 WCTemplputParams *TP,
769 if (N >= TP->Tokens->nParameters) {
773 switch (TP->Tokens->Params[N]->Type) {
779 case TYPE_SUBTEMPLATE:
788 void GetTemplateTokenString(StrBuf *Target,
789 WCTemplputParams *TP,
795 WCTemplputParams SubTP;
797 if (N >= TP->Tokens->nParameters) {
798 LogTemplateError(Target,
799 "TokenParameter", N, TP,
800 "invalid token %d. this shouldn't have come till here.\n", N);
806 switch (TP->Tokens->Params[N]->Type) {
810 *Value = TP->Tokens->Params[N]->Start;
811 *len = TP->Tokens->Params[N]->len;
814 if (TP->Tokens->Params[N]->len == 0) {
815 LogTemplateError(Target,
816 "TokenParameter", N, TP,
817 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
822 Buf = (StrBuf*) SBstr(TKEY(N));
823 *Value = ChrPtr(Buf);
824 *len = StrLength(Buf);
827 if (TP->Tokens->Params[N]->len == 0) {
828 LogTemplateError(Target,
829 "TokenParameter", N, TP,
830 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
835 get_PREFERENCE(TKEY(N), &Buf);
836 *Value = ChrPtr(Buf);
837 *len = StrLength(Buf);
840 LogTemplateError(Target,
841 "TokenParameter", N, TP,
842 "Requesting parameter %d; of type LONG, want string.", N);
845 LogTemplateError(Target,
846 "TokenParameter", N, TP,
847 "Requesting parameter %d; of type PREFINT, want string.", N);
850 *Value = _(TP->Tokens->Params[N]->Start);
851 *len = strlen(*Value);
853 case TYPE_SUBTEMPLATE:
854 if (TP->Tokens->Params[N]->len == 0) {
855 LogTemplateError(Target,
856 "TokenParameter", N, TP,
857 "Requesting parameter %d; of type SUBTEMPLATE, empty lookup string not admitted.", N);
863 memset(&SubTP, 0, sizeof(WCTemplputParams *));
864 SubTP.Context = TP->Context;
865 SubTP.Filter.ContextType = TP->Filter.ContextType;
867 DoTemplate(TKEY(N), Buf, &SubTP);
868 *Value = ChrPtr(Buf);
869 *len = StrLength(Buf);
870 /* we can't free it here, so we put it into the subst so its discarded later on. */
871 SVPUTBuf(TKEY(N), Buf, 0);
875 LogTemplateError(Target,
876 "TokenParameter", N, TP,
877 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
882 long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
885 if (N >= TP->Tokens->nParameters) {
886 LogTemplateError(Target,
887 "TokenParameter", N, TP,
888 "invalid token %d. this shouldn't have come till here.\n", N);
893 switch (TP->Tokens->Params[N]->Type) {
896 return atol(TP->Tokens->Params[N]->Start);
899 if (TP->Tokens->Params[N]->len == 0) {
900 LogTemplateError(Target,
901 "TokenParameter", N, TP,
902 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
905 return LBstr(TKEY(N));
908 LogTemplateError(Target,
909 "TokenParameter", N, TP,
910 "requesting a prefstring in param %d want a number", N);
911 if (TP->Tokens->Params[N]->len == 0) {
912 LogTemplateError(Target,
913 "TokenParameter", N, TP,
914 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
917 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
922 return TP->Tokens->Params[N]->lvalue;
924 if (TP->Tokens->Params[N]->len == 0) {
925 LogTemplateError(Target,
926 "TokenParameter", N, TP,
927 "Requesting parameter %d; of type PREFINT, empty lookup string not admitted.", N);
930 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
934 LogTemplateError(Target,
935 "TokenParameter", N, TP,
936 "requesting a I18N string in param %d; want a number", N);
938 case TYPE_SUBTEMPLATE:
939 LogTemplateError(Target,
940 "TokenParameter", N, TP,
941 "requesting a subtemplate in param %d; not supported for numbers", N);
944 LogTemplateError(Target,
945 "TokenParameter", N, TP,
946 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
954 * \brief Print the value of a variable
955 * \param keyname get a key to print
957 void print_value_of(StrBuf *Target, WCTemplputParams *TP)
964 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
965 /* TODO: depricated! */
966 if (TP->Tokens->pName[0] == '=') {
967 DoTemplate(TP->Tokens->pName+1, TP->Tokens->NameEnd - 1, NULL, &NoCtx);
969 /*/////TODO: if param[1] == "U" -> urlescape
971 /** Page-local variables */
972 if ((WCC->vars!= NULL) && GetHash(WCC->vars, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
973 ptr = (wcsubst*) vVar;
974 switch(ptr->wcs_type) {
976 StrBufAppendBuf(Target, ptr->wcs_value, 0);
979 pvo_do_cmd(Target, ptr->wcs_value);
982 (*ptr->wcs_function) (Target, TP);
986 StrBufAppendBuf(Target, ptr->wcs_value, 0);
989 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
993 Target, "Subst", ERR_NAME, TP,
994 "WARNING: invalid value in SV-Hash at %s!", TP->Tokens->pName);
999 Target, "Token", ERR_NAME, TP,
1000 "didn't find Handler \"%s\"", TP->Tokens->pName);
1005 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
1007 wcsession *WCC = WC;
1011 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1012 ParamToLookup->len, &vVar)) {
1013 ptr = (wcsubst*) vVar;
1014 switch(ptr->wcs_type) {
1017 case WCS_STRBUF_REF:
1018 if (ParamToCompare->Type == TYPE_STR)
1019 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
1020 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
1022 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
1030 if (ParamToCompare->Type == TYPE_STR)
1033 return ParamToCompare->lvalue == ptr->lvalue;
1036 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1037 ParamToLookup->Start);
1043 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
1045 wcsession *WCC = WC;
1049 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1050 ParamToLookup->len, &vVar)) {
1051 ptr = (wcsubst*) vVar;
1052 switch(ptr->wcs_type) {
1055 case WCS_STRBUF_REF:
1056 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
1057 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
1064 return StrTol(Compare) == ptr->lvalue;
1066 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1067 ParamToLookup->Start);
1076 * \brief puts string into the template and computes which escape methon we should use
1077 * \param Source the string we should put into the template
1078 * \param FormatTypeIndex where should we look for escape types if?
1080 void StrBufAppendTemplate(StrBuf *Target,
1081 WCTemplputParams *TP,
1082 const StrBuf *Source, int FormatTypeIndex)
1085 char EscapeAs = ' ';
1087 if ((FormatTypeIndex < TP->Tokens->nParameters) &&
1088 (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
1089 (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
1090 EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
1097 StrEscAppend(Target, Source, NULL, 0, 2);
1100 StrEscAppend(Target, Source, NULL, 0, 0);
1103 StrECMAEscAppend(Target, Source, NULL);
1106 StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 0);
1109 StrBufUrlescAppend(Target, Source, NULL);
1112 StrBufAppendBuf(Target, Source, 0);
1117 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
1119 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
1120 if (Template->TokenSpace <= 0) {
1121 Template->Tokens = (WCTemplateToken**)malloc(
1122 sizeof(WCTemplateToken*) * 10);
1123 memset(Template->Tokens, 0, sizeof(WCTemplateToken*) * 10);
1124 Template->TokenSpace = 10;
1127 WCTemplateToken **NewTokens;
1129 NewTokens= (WCTemplateToken**) malloc(
1130 sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1133 0, sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1137 sizeof(WCTemplateToken*) * Template->nTokensUsed);
1139 free(Template->Tokens);
1140 Template->TokenSpace *= 2;
1141 Template->Tokens = NewTokens;
1144 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
1147 TemplateParam *GetNextParameter(StrBuf *Buf,
1150 WCTemplateToken *Tokens,
1152 WCTemplputParams *TP)
1154 const char *pch = *pCh;
1155 const char *pchs, *pche;
1156 TemplateParam *Parm;
1160 Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
1161 memset(Parm, 0, sizeof(TemplateParam));
1162 Parm->Type = TYPE_STR;
1164 /* Skip leading whitespaces */
1165 while ((*pch == ' ' )||
1168 (*pch == '\n')) pch ++;
1171 Parm->Type = TYPE_PREFSTR;
1178 else if (*pch == ';') {
1179 Parm->Type = TYPE_PREFINT;
1186 else if (*pch == '#') {
1187 Parm->Type = TYPE_INTDEFINE;
1190 else if (*pch == '_') {
1191 Parm->Type = TYPE_GETTEXT;
1198 else if (*pch == 'B') {
1199 Parm->Type = TYPE_BSTR;
1206 else if (*pch == '=') {
1207 Parm->Type = TYPE_SUBTEMPLATE;
1218 else if (*pch == '\'')
1220 if (quote != '\0') {
1225 ( (pch > pchs) && (*(pch - 1) == '\\'))
1230 if (*pch != quote) {
1231 lprintf(1, "Error (in '%s' line %ld); "
1232 "evaluating template param [%s] in Token [%s]\n",
1233 ChrPtr(pTmpl->FileName),
1235 ChrPtr(Tokens->FlatToken),
1242 StrBufPeek(Buf, pch, -1, '\0');
1243 if (LoadTemplates > 1) {
1244 lprintf(1, "DBG: got param [%s] %ld %ld\n",
1245 pchs, pche - pchs, strlen(pchs));
1248 Parm->len = pche - pchs;
1249 pch ++; /* move after trailing quote */
1250 if (ParamBrace && (*pch == ')')) {
1257 Parm->Type = TYPE_LONG;
1259 while ((pch <= pe) &&
1265 if (pch - pchs > 1){
1266 StrBufPeek(Buf, pch, -1, '\0');
1267 Parm->lvalue = atol(pchs);
1274 lprintf(1, "Error (in '%s' line %ld); "
1275 "evaluating long template param [%s] in Token [%s]\n",
1276 ChrPtr(pTmpl->FileName),
1278 ChrPtr(Tokens->FlatToken),
1285 while ((*pch == ' ' )||
1289 (*pch == '\n')) pch ++;
1294 if (DumpTemplateI18NStrings) {
1295 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
1298 case TYPE_INTDEFINE: {
1301 if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
1305 PVal = (long*) vPVal;
1307 Parm->lvalue = *PVal;
1311 LogTemplateError(NULL, "Define", ERR_PARM1, TP,
1316 case TYPE_SUBTEMPLATE:{
1318 /* well, we don't check the mobile stuff here... */
1319 if (!GetHash(LocalTemplateCache, Parm->Start, Parm->len, &vTmpl) &&
1320 !GetHash(TemplateCache, Parm->Start, Parm->len, &vTmpl)) {
1322 NULL, "SubTemplate", ERR_PARM1, TP,
1323 "referenced here doesn't exist");
1331 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
1333 const char *pTokenStart,
1334 const char *pTokenEnd,
1340 TemplateParam *Param;
1341 WCTemplateToken *NewToken;
1342 WCTemplputParams TP;
1344 NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
1345 memset(NewToken, 0, sizeof(WCTemplateToken));
1346 TP.Tokens = NewToken;
1347 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
1348 NewToken->Flags = 0;
1349 NewToken->Line = Line + 1;
1350 NewToken->pTokenStart = pTokenStart;
1351 NewToken->TokenStart = pTokenStart - pStart;
1352 NewToken->TokenEnd = (pTokenEnd - pStart) - NewToken->TokenStart;
1353 NewToken->pTokenEnd = pTokenEnd;
1354 NewToken->NameEnd = NewToken->TokenEnd - 2;
1355 NewToken->PreEval = NULL;
1356 NewToken->FlatToken = NewStrBufPlain(pTokenStart + 2, pTokenEnd - pTokenStart - 2);
1357 StrBufShrinkToFit(NewToken->FlatToken, 1);
1359 StrBufPeek(Buf, pTokenStart, + 1, '\0');
1360 StrBufPeek(Buf, pTokenEnd, -1, '\0');
1361 pch = NewToken->pName = pTokenStart + 2;
1363 NewToken->HaveParameters = 0;;
1364 NewToken->nParameters = 0;
1366 while (pch < pTokenEnd - 1) {
1368 StrBufPeek(Buf, pch, -1, '\0');
1369 NewToken->NameEnd = pch - NewToken->pName;
1371 if (*(pTokenEnd - 1) != ')') {
1373 NULL, "Parseerror", ERR_NAME, &TP,
1374 "Warning, Non welformed Token; missing right parenthesis");
1376 while (pch < pTokenEnd - 1) {
1377 Param = GetNextParameter(Buf, &pch, pTokenEnd - 1, NewToken, pTmpl, &TP);
1378 if (Param != NULL) {
1379 NewToken->HaveParameters = 1;
1380 if (NewToken->nParameters > MAXPARAM) {
1382 NULL, "Parseerror", ERR_NAME, &TP,
1383 "only [%d] Params allowed in Tokens",
1387 FreeToken(&NewToken);
1390 NewToken->Params[NewToken->nParameters++] = Param;
1394 if((NewToken->NameEnd == 1) &&
1395 (NewToken->HaveParameters == 1))
1398 if (*(NewToken->pName) == '_')
1399 NewToken->Flags = SV_GETTEXT;
1400 else if (*(NewToken->pName) == '=')
1401 NewToken->Flags = SV_SUBTEMPL;
1402 else if (*(NewToken->pName) == '%')
1403 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
1404 else if (*(NewToken->pName) == '?')
1405 NewToken->Flags = SV_CONDITIONAL;
1406 else if (*(NewToken->pName) == '!')
1407 NewToken->Flags = SV_NEG_CONDITIONAL;
1413 switch (NewToken->Flags) {
1415 /* If we're able to find out more about the token, do it now while its fresh. */
1416 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1417 HashHandler *Handler;
1418 Handler = (HashHandler*) vVar;
1419 if ((NewToken->nParameters < Handler->Filter.nMinArgs) ||
1420 (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
1422 NULL, "Token", ERR_NAME, &TP,
1423 "doesn't work with %d params",
1424 NewToken->nParameters);
1428 NewToken->PreEval = Handler;
1429 NewToken->Flags = SV_PREEVALUATED;
1430 if (Handler->PreEvalFunc != NULL)
1431 Handler->PreEvalFunc(NewToken);
1436 if (NewToken->nParameters !=1) {
1438 NULL, "Gettext", ERR_NAME, &TP,
1439 "requires exactly 1 parameter, you gave %d params",
1440 NewToken->nParameters);
1441 NewToken->Flags = 0;
1444 if (DumpTemplateI18NStrings) {
1445 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
1449 if (NewToken->nParameters != 1) {
1451 NULL, "Subtemplates", ERR_NAME, &TP,
1452 "require exactly 1 parameter, you gave %d params",
1453 NewToken->nParameters);
1458 /* well, we don't check the mobile stuff here... */
1459 if (!GetHash(LocalTemplateCache,
1460 NewToken->Params[0]->Start,
1461 NewToken->Params[0]->len,
1463 !GetHash(TemplateCache,
1464 NewToken->Params[0]->Start,
1465 NewToken->Params[0]->len,
1468 NULL, "SubTemplate", ERR_PARM1, &TP,
1473 case SV_CUST_STR_CONDITIONAL:
1474 case SV_CONDITIONAL:
1475 case SV_NEG_CONDITIONAL:
1476 if (NewToken->nParameters <2) {
1478 NULL, "Conditional", ERR_PARM1, &TP,
1479 "require at least 2 parameters, you gave %d params",
1480 NewToken->nParameters);
1481 NewToken->Flags = 0;
1484 if (NewToken->Params[1]->lvalue == 0) {
1486 NULL, "Conditional", ERR_PARM1, &TP,
1487 "Conditional ID (Parameter 1) mustn't be 0!");
1488 NewToken->Flags = 0;
1491 if (!GetHash(Conditionals,
1492 NewToken->Params[0]->Start,
1493 NewToken->Params[0]->len,
1496 if ((NewToken->Params[0]->len == 1) &&
1497 (NewToken->Params[0]->Start[0] == 'X'))
1500 NULL, "Conditional", ERR_PARM1, &TP,
1503 NewToken->Error = NewStrBuf();
1506 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1507 NewToken->Params[0]->Start,
1508 ChrPtr(pTmpl->FileName),
1510 ChrPtr(NewToken->FlatToken));
1514 NewToken->PreEval = vVar;
1526 * \brief Display a variable-substituted template
1527 * \param templatename template file to load
1529 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1531 WCTemplate *NewTemplate;
1533 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1534 memset(NewTemplate, 0, sizeof(WCTemplate));
1535 NewTemplate->Data = NULL;
1536 NewTemplate->FileName = NewStrBufDup(filename);
1537 StrBufShrinkToFit(NewTemplate->FileName, 1);
1538 NewTemplate->nTokensUsed = 0;
1539 NewTemplate->TokenSpace = 0;
1540 NewTemplate->Tokens = NULL;
1541 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1542 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1543 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1546 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1547 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1550 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1555 * \brief Display a variable-substituted template
1556 * \param templatename template file to load
1558 void *duplicate_template(WCTemplate *OldTemplate)
1560 WCTemplate *NewTemplate;
1562 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1563 memset(NewTemplate, 0, sizeof(WCTemplate));
1564 NewTemplate->Data = NULL;
1565 NewTemplate->FileName = NewStrBufDup(OldTemplate->FileName);
1566 StrBufShrinkToFit(NewTemplate->FileName, 1);
1567 NewTemplate->nTokensUsed = 0;
1568 NewTemplate->TokenSpace = 0;
1569 NewTemplate->Tokens = NULL;
1570 NewTemplate->MimeType = NewStrBufDup(OldTemplate->MimeType);
1575 * \brief Display a variable-substituted template
1576 * \param templatename template file to load
1578 void *load_template(WCTemplate *NewTemplate)
1581 struct stat statbuf;
1582 const char *pS, *pE, *pch, *Err;
1586 fd = open(ChrPtr(NewTemplate->FileName), O_RDONLY);
1588 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1589 ChrPtr(NewTemplate->FileName), strerror(errno));
1593 if (fstat(fd, &statbuf) == -1) {
1594 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1595 ChrPtr(NewTemplate->FileName), strerror(errno));
1599 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size + 1);
1600 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1602 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1603 ChrPtr(NewTemplate->FileName), strerror(errno));
1604 //FreeWCTemplate(NewTemplate);/////tODO
1610 StrBufShrinkToFit(NewTemplate->Data, 1);
1611 StrBufShrinkToFit(NewTemplate->MimeType, 1);
1612 pS = pch = ChrPtr(NewTemplate->Data);
1613 pE = pS + StrLength(NewTemplate->Data);
1615 const char *pts, *pte;
1617 int InDoubleQuotes = 0;
1619 /** Find one <? > */
1621 for (; pch < pE; pch ++) {
1622 if ((*pch=='<')&&(*(pch + 1)=='?') &&
1623 !((pch == pS) && /* we must ommit a <?xml */
1624 (*(pch + 2) == 'x') &&
1625 (*(pch + 3) == 'm') &&
1626 (*(pch + 4) == 'l')))
1628 if (*pch=='\n') Line ++;
1634 /** Found one? parse it. */
1635 for (; pch <= pE - 1; pch ++) {
1637 InDoubleQuotes = ! InDoubleQuotes;
1638 else if (*pch == '\'')
1639 InQuotes = ! InQuotes;
1640 else if ((!InQuotes && !InDoubleQuotes) &&
1641 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1649 PutNewToken(NewTemplate,
1650 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1657 const char* PrintTemplate(void *vSubst)
1659 WCTemplate *Tmpl = vSubst;
1661 return ChrPtr(Tmpl->FileName);
1665 int LoadTemplateDir(const StrBuf *DirName, HashList *wireless, HashList *big, const StrBuf *BaseKey)
1671 StrBuf *SubDirectory;
1672 DIR *filedir = NULL;
1673 struct dirent *filedir_entry;
1680 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
1685 filedir = opendir (ChrPtr(DirName));
1686 if (filedir == NULL) {
1691 Toplevel = StrLength(BaseKey) == 0;
1692 SubDirectory = NewStrBuf();
1693 SubKey = NewStrBuf();
1694 FileName = NewStrBufPlain(NULL, PATH_MAX);
1696 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
1697 (filedir_entry != NULL))
1701 #ifdef _DIRENT_HAVE_D_NAMELEN
1702 d_namelen = filedir_entry->d_namelen;
1703 d_type = filedir_entry->d_type;
1707 #define DT_UNKNOWN 0
1712 #define IFTODT(mode) (((mode) & 0170000) >> 12)
1713 #define DTTOIF(dirtype) ((dirtype) << 12)
1715 d_namelen = strlen(filedir_entry->d_name);
1716 d_type = DT_UNKNOWN;
1718 d_without_ext = d_namelen;
1720 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1721 continue; /* Ignore backup files... */
1723 if ((d_namelen == 1) &&
1724 (filedir_entry->d_name[0] == '.'))
1727 if ((d_namelen == 2) &&
1728 (filedir_entry->d_name[0] == '.') &&
1729 (filedir_entry->d_name[1] == '.'))
1732 if (d_type == DT_UNKNOWN) {
1734 char path[PATH_MAX];
1735 snprintf(path, PATH_MAX, "%s/%s",
1736 ChrPtr(DirName), filedir_entry->d_name);
1737 if (stat(path, &s) == 0) {
1738 d_type = IFTODT(s.st_mode);
1744 /* Skip directories we are not interested in... */
1745 if (strcmp(filedir_entry->d_name, ".svn") == 0)
1748 FlushStrBuf(SubKey);
1750 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1751 StrBufAppendBuf(SubKey, BaseKey, 0);
1752 StrBufAppendBufPlain(SubKey, HKEY("_"), 0);
1754 StrBufAppendBufPlain(SubKey, filedir_entry->d_name, d_namelen, 0);
1756 FlushStrBuf(SubDirectory);
1757 StrBufAppendBuf(SubDirectory, DirName, 0);
1758 if (ChrPtr(SubDirectory)[StrLength(SubDirectory) - 1] != '/')
1759 StrBufAppendBufPlain(SubDirectory, HKEY("/"), 0);
1760 StrBufAppendBufPlain(SubDirectory, filedir_entry->d_name, d_namelen, 0);
1762 LoadTemplateDir(SubDirectory, wireless, big, SubKey);
1765 case DT_LNK: /* TODO: check whether its a file or a directory */
1769 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1771 if ((d_without_ext == 0) || (d_namelen < 3))
1773 if (((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~') ||
1774 (strcmp(&filedir_entry->d_name[d_without_ext], ".orig") == 0) ||
1775 (strcmp(&filedir_entry->d_name[d_without_ext], ".swp") == 0))
1776 continue; /* Ignore backup files... */
1777 /* .m.xxx is for mobile useragents! */
1779 if (d_without_ext > 2)
1780 IsMobile = (filedir_entry->d_name[d_without_ext - 1] == 'm') &&
1781 (filedir_entry->d_name[d_without_ext - 2] == '.');
1782 PStart = filedir_entry->d_name;
1783 StrBufPrintf(FileName, "%s/%s", ChrPtr(DirName), filedir_entry->d_name);
1784 MinorPtr = strchr(filedir_entry->d_name, '.');
1785 if (MinorPtr != NULL)
1789 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1790 StrBufAppendBuf(Key, BaseKey, 0);
1791 StrBufAppendBufPlain(Key, HKEY("_"), 0);
1793 StrBufAppendBufPlain(Key, filedir_entry->d_name, MinorPtr - filedir_entry->d_name, 0);
1795 if (LoadTemplates >= 1)
1796 lprintf(1, "%s %d %s\n", ChrPtr(FileName), IsMobile, ChrPtr(Key));
1797 prepare_template(FileName, Key, (IsMobile)?wireless:big);
1804 FreeStrBuf(&FileName);
1806 FreeStrBuf(&SubDirectory);
1807 FreeStrBuf(&SubKey);
1811 void InitTemplateCache(void)
1816 HashList *Templates[4];
1821 /* Primary Template set... */
1822 StrBufPrintf(Dir, "%s/t", static_dirs[0]);
1823 LoadTemplateDir(Dir,
1824 WirelessTemplateCache,
1828 /* User local Template set */
1829 StrBufPrintf(Dir, "%s/t", static_dirs[1]);
1830 LoadTemplateDir(Dir,
1831 WirelessLocalTemplateCache,
1835 /* Debug Templates, just to be loaded while debugging. */
1837 StrBufPrintf(Dir, "%s/dbg", static_dirs[0]);
1838 LoadTemplateDir(Dir,
1839 WirelessTemplateCache,
1842 Templates[0] = WirelessTemplateCache;
1843 Templates[1] = TemplateCache;
1844 Templates[2] = WirelessLocalTemplateCache;
1845 Templates[3] = LocalTemplateCache;
1848 if (LoadTemplates == 0)
1849 for (i=0; i < 4; i++) {
1855 At = GetNewHashPos(Templates[i], 0);
1856 while (GetNextHashPos(Templates[i],
1861 (vTemplate != NULL))
1863 load_template((WCTemplate *)vTemplate);
1875 /*-----------------------------------------------------------------------------
1876 * Filling & processing Templates
1879 * \brief executes one token
1880 * \param Target buffer to append to
1881 * \param Token da to process.
1882 * \param Template we're iterating
1883 * \param Context Contextpoointer to pass in
1884 * \param state are we in conditional state?
1885 * \param ContextType what type of information does context giv us?
1887 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1889 const char *AppendMe;
1891 HashHandler *Handler;
1894 /* much output, since pName is not terminated...
1895 lprintf(1,"Doing token: %s\n",Token->pName);
1898 switch (TP->Tokens->Flags) {
1900 TmplGettext(Target, TP);
1902 case SV_CONDITIONAL: /** Forward conditional evaluation */
1903 return EvaluateConditional(Target, 1, state, TP);
1905 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1906 return EvaluateConditional(Target, 0, state, TP);
1908 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1909 if (TP->Tokens->nParameters >= 6) {
1910 if (EvaluateConditional(Target, 0, state, TP)) {
1911 GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
1912 StrBufAppendBufPlain(Target,
1918 GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
1919 StrBufAppendBufPlain(Target,
1927 Target, "Conditional", ERR_NAME, TP,
1928 "needs at least 6 Params!");
1932 if (TP->Tokens->nParameters == 1)
1933 DoTemplate(TKEY(0), Target, TP);
1935 case SV_PREEVALUATED:
1936 Handler = (HashHandler*) TP->Tokens->PreEval;
1937 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1940 Handler->HandlerFunc(Target, TP);
1943 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
1944 Handler = (HashHandler*) vVar;
1945 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1949 Handler->HandlerFunc(Target, TP);
1953 print_value_of(Target, TP);
1961 const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
1963 WCTemplate *pTmpl = Tmpl;
1966 const char *pData, *pS;
1968 WCTemplputParams TP;
1970 memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
1972 TP.Context = CallingTP->Context;
1973 TP.ControlContext = CallingTP->ControlContext;
1975 if (LoadTemplates != 0) {
1976 if (LoadTemplates > 1)
1977 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1978 ChrPtr(Tmpl->FileName));
1979 pTmpl = duplicate_template(Tmpl);
1980 if(load_template(pTmpl) == NULL) {
1983 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1984 ChrPtr(Tmpl->FileName));
1985 FreeWCTemplate(pTmpl);
1991 pS = pData = ChrPtr(pTmpl->Data);
1992 len = StrLength(pTmpl->Data);
1996 if (i >= pTmpl->nTokensUsed) {
1997 StrBufAppendBufPlain(Target,
1999 len - (pData - pS), 0);
2003 StrBufAppendBufPlain(
2005 pTmpl->Tokens[i]->pTokenStart - pData, 0);
2006 TP.Tokens = pTmpl->Tokens[i];
2007 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2008 state = EvaluateToken(Target, state, &TP);
2010 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
2011 /* condition told us to skip till its end condition */
2013 TP.Tokens = pTmpl->Tokens[i];
2014 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2015 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
2016 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
2017 if (state == EvaluateConditional(
2019 pTmpl->Tokens[i]->Flags,
2025 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
2026 if (i > pTmpl->nTokensUsed)
2030 if (LoadTemplates != 0) {
2031 FreeWCTemplate(pTmpl);
2033 return Tmpl->MimeType;
2038 * \brief Display a variable-substituted template
2039 * \param templatename template file to load
2040 * \returns the mimetype of the template its doing
2042 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
2044 WCTemplputParams LocalTP;
2046 HashList *StaticLocal;
2052 memset(&LocalTP, 0, sizeof(WCTemplputParams));
2056 if (WC->is_mobile > 0) {
2057 Static = WirelessTemplateCache;
2058 StaticLocal = WirelessLocalTemplateCache;
2061 Static = TemplateCache;
2062 StaticLocal = LocalTemplateCache;
2067 lprintf (1, "Can't to load a template with empty name!\n");
2068 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
2072 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
2073 !GetHash(Static, templatename, len, &vTmpl)) {
2074 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
2075 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
2077 (long)strlen(templatename));
2079 dbg_PrintHash(Static, PrintTemplate, NULL);
2080 PrintHash(Static, VarPrintTransition, PrintTemplate);
2086 return ProcessTemplate(vTmpl, Target, TP);
2090 /*-----------------------------------------------------------------------------
2093 typedef struct _HashIterator {
2094 HashList *StaticList;
2095 int AdditionalParams;
2097 int XPectContextType;
2099 RetrieveHashlistFunc GetHash;
2100 HashDestructorFunc Destructor;
2101 SubTemplFunc DoSubTemplate;
2104 void RegisterITERATOR(const char *Name, long len,
2105 int AdditionalParams,
2106 HashList *StaticList,
2107 RetrieveHashlistFunc GetHash,
2108 SubTemplFunc DoSubTempl,
2109 HashDestructorFunc Destructor,
2111 int XPectContextType,
2116 It = (HashIterator*)malloc(sizeof(HashIterator));
2117 memset(It, 0, sizeof(HashIterator));
2118 It->StaticList = StaticList;
2119 It->AdditionalParams = AdditionalParams;
2120 It->GetHash = GetHash;
2121 It->DoSubTemplate = DoSubTempl;
2122 It->Destructor = Destructor;
2123 It->ContextType = ContextType;
2124 It->XPectContextType = XPectContextType;
2126 Put(Iterators, Name, len, It, NULL);
2129 typedef struct _iteratestruct {
2138 int preeval_iterate(WCTemplateToken *Token)
2140 WCTemplputParams TPP;
2141 WCTemplputParams *TP;
2145 memset(&TPP, 0, sizeof(WCTemplputParams));
2148 if (!GetHash(Iterators, TKEY(0), &vIt)) {
2150 NULL, "Iterator", ERR_PARM1, TP,
2154 if (TP->Tokens->Params[1]->Type != TYPE_SUBTEMPLATE) {
2155 LogTemplateError(NULL, "Iterator", ERR_PARM1, TP,
2156 "Need token with type Subtemplate as param 1, have %s",
2157 TP->Tokens->Params[1]->Start);
2160 /* well, we don't check the mobile stuff here... */
2161 if (!GetHash(LocalTemplateCache, TKEY(1), &vTmpl) &&
2162 !GetHash(TemplateCache, TKEY(1), &vTmpl)) {
2163 LogTemplateError(NULL, "SubTemplate", ERR_PARM1, TP,
2164 "referenced here doesn't exist");
2166 Token->Preeval2 = vIt;
2170 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
2175 SortStruct *SortBy = NULL;
2177 int DetectGroupChange = 0;
2180 void *vLastContext = NULL;
2182 WCTemplputParams SubTP;
2183 IterateStruct Status;
2189 memset(&Status, 0, sizeof(IterateStruct));
2190 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2192 It = (HashIterator*) TP->Tokens->Preeval2;
2195 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
2199 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2201 Target, "Iterator", ERR_PARM1, TP,
2202 "doesn't work with %d params",
2203 TP->Tokens->nParameters);
2207 if ((It->XPectContextType != CTX_NONE) &&
2208 (It->XPectContextType != TP->Filter.ContextType)) {
2210 Target, "Iterator", ERR_PARM1, TP,
2211 "requires context of type %d, have %d",
2212 It->XPectContextType,
2213 TP->Filter.ContextType);
2218 if (It->StaticList == NULL)
2219 List = It->GetHash(Target, TP);
2221 List = It->StaticList;
2223 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2224 if (DetectGroupChange) {
2225 const StrBuf *BSort;
2226 DetectGroupChange = 0;
2227 if (havebstr("SortBy")) {
2228 BSort = sbstr("SortBy");
2229 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2230 (vSortBy != NULL)) {
2231 SortBy = (SortStruct*)vSortBy;
2232 /** Ok, its us, lets see in which direction we should sort... */
2233 if (havebstr("SortOrder")) {
2235 SortOrder = LBSTR("SortOrder");
2237 DetectGroupChange = 1;
2242 nMembersUsed = GetCount(List);
2243 SubBuf = NewStrBuf();
2244 SubTP.Filter.ContextType = It->ContextType;
2245 SubTP.Filter.ControlContextType = CTX_ITERATE;
2246 SubTP.ControlContext = &Status;
2248 if (HAVE_PARAM(2)) {
2249 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2251 if (HAVE_PARAM(3)) {
2252 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2254 if (HAVE_PARAM(4)) {
2255 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2257 it = GetNewHashPos(List, StepWidth);
2259 StopAt = GetCount(List);
2261 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2262 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2263 if (DetectGroupChange && Status.n > 0) {
2264 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2266 Status.LastN = (Status.n + 1) == nMembersUsed;
2267 SubTP.Context = vContext;
2268 if (It->DoSubTemplate != NULL)
2269 It->DoSubTemplate(SubBuf, &SubTP);
2270 DoTemplate(TKEY(1), SubBuf, &SubTP);
2272 StrBufAppendBuf(Target, SubBuf, 0);
2273 FlushStrBuf(SubBuf);
2274 Status.oddeven = ! Status.oddeven;
2275 vLastContext = vContext;
2279 FreeStrBuf(&SubBuf);
2281 if (It->Destructor != NULL)
2282 It->Destructor(&List);
2286 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2288 IterateStruct *Ctx = CCTX;
2289 if (TP->Tokens->nParameters < 3)
2290 return Ctx->GroupChange;
2292 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2295 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2297 IterateStruct *Ctx = CCTX;
2299 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2301 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2305 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2307 IterateStruct *Ctx = CCTX;
2309 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2313 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2315 IterateStruct *Ctx = CCTX;
2316 StrBufAppendPrintf(Target, "%d", Ctx->n);
2319 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2321 IterateStruct *Ctx = CCTX;
2325 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2327 IterateStruct *Ctx = CCTX;
2333 /*-----------------------------------------------------------------------------
2336 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2338 ConditionalStruct *Cond;
2340 if ((TP->Tokens->Params[0]->len == 1) &&
2341 (TP->Tokens->Params[0]->Start[0] == 'X'))
2342 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2344 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2347 Target, "Conditional", ERR_PARM1, TP,
2352 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2356 if (Cond->CondF(Target, TP) == Neg)
2357 return TP->Tokens->Params[1]->lvalue;
2361 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2366 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2368 subst = (wcsubst*) vsubst;
2370 switch(subst->wcs_type) {
2372 return (subst->wcs_function!=NULL);
2374 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
2378 case WCS_STRBUF_REF:
2379 if (TP->Tokens->nParameters < 4)
2381 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2383 if (TP->Tokens->nParameters < 4)
2384 return (subst->lvalue != 0);
2385 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2387 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2393 void RegisterConditional(const char *Name, long len,
2395 WCConditionalFunc CondF,
2396 int ContextRequired)
2398 ConditionalStruct *Cond;
2400 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2401 memset(Cond, 0, sizeof(ConditionalStruct));
2402 Cond->PlainName = Name;
2403 Cond->Filter.nMaxArgs = nParams;
2404 Cond->Filter.nMinArgs = nParams;
2405 Cond->CondF = CondF;
2406 Cond->Filter.ContextType = ContextRequired;
2407 Cond->Filter.ControlContextType = CTX_NONE;
2408 Put(Conditionals, Name, len, Cond, NULL);
2411 void RegisterControlConditional(const char *Name, long len,
2413 WCConditionalFunc CondF,
2414 int ControlContextRequired)
2416 ConditionalStruct *Cond;
2418 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2419 memset(Cond, 0, sizeof(ConditionalStruct));
2420 Cond->PlainName = Name;
2421 Cond->Filter.nMaxArgs = nParams;
2422 Cond->Filter.nMinArgs = nParams;
2423 Cond->CondF = CondF;
2424 Cond->Filter.ContextType = CTX_NONE;
2425 Cond->Filter.ControlContextType = ControlContextRequired;
2426 Put(Conditionals, Name, len, Cond, NULL);
2429 void RegisterTokenParamDefine(const char *Name, long len,
2434 PVal = (long*)malloc(sizeof(long));
2436 Put(Defines, Name, len, PVal, NULL);
2439 long GetTokenDefine(const char *Name, long len,
2444 if (GetHash(Defines, Name, len, &vPVal) &&
2447 return *(long*) vPVal;
2455 void tmplput_DefStr(StrBuf *Target, WCTemplputParams *TP)
2459 GetTemplateTokenString(Target, TP, 2, &Str, &len);
2461 StrBufAppendBufPlain(Target, Str, len, 0);
2464 void tmplput_DefVal(StrBuf *Target, WCTemplputParams *TP)
2468 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2469 StrBufAppendPrintf(Target, "%d", val);
2474 /*-----------------------------------------------------------------------------
2477 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2479 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2481 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2483 StrBuf *TokenText = (StrBuf*) CTX;
2484 const char *CompareToken;
2487 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2488 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2491 void tmplput_ContextStringArray(StrBuf *Target, WCTemplputParams *TP)
2493 HashList *Arr = (HashList*) CTX;
2497 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2498 if (GetHash(Arr, IKEY(val), &pV) &&
2500 StrBufAppendTemplate(Target, TP, (StrBuf*)pV, 1);
2503 int ConditionalContextStrinArray(StrBuf *Target, WCTemplputParams *TP)
2505 HashList *Arr = (HashList*) CTX;
2508 const char *CompareToken;
2511 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2512 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2513 if (GetHash(Arr, IKEY(val), &pV) &&
2515 return strcmp(ChrPtr((StrBuf*)pV), CompareToken) == 0;
2521 /*-----------------------------------------------------------------------------
2525 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2527 WCTemplputParams SubTP;
2529 StrBuf *Headline = NULL;
2530 if (TP->Tokens->nParameters == 2) {
2531 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2532 Headline = NewStrBuf();
2533 DoTemplate(TKEY(1), Headline, TP);
2538 GetTemplateTokenString(Target,
2543 Headline = NewStrBufPlain(Ch, len);
2546 /* else TODO error? logging? */
2547 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2548 SubTP.Context = Headline;
2549 SubTP.Filter.ContextType = CTX_STRBUF;
2550 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2551 DoTemplate(TKEY(0), Target, TP);
2552 DoTemplate(HKEY("endbox"), Target, TP);
2553 FreeStrBuf(&Headline);
2556 /*-----------------------------------------------------------------------------
2559 int preeval_do_tabbed(WCTemplateToken *Token)
2561 WCTemplputParams TPP;
2562 WCTemplputParams *TP;
2568 memset(&TPP, 0, sizeof(WCTemplputParams));
2571 nTabs = TP->Tokens->nParameters / 2 - 1;
2572 if (TP->Tokens->nParameters % 2 != 0)
2574 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2575 "need even number of arguments");
2579 else for (i = 0; i < nTabs; i++) {
2580 if (!HaveTemplateTokenString(NULL,
2585 (TP->Tokens->Params[i * 2]->len == 0))
2587 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2588 "Tab-Subject %d needs to be able to produce a string, have %s",
2589 i, TP->Tokens->Params[i * 2]->Start);
2592 if (!HaveTemplateTokenString(NULL,
2597 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2599 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2600 "Tab-Content %d needs to be able to produce a string, have %s",
2601 i, TP->Tokens->Params[i * 2 + 1]->Start);
2606 if (!HaveTemplateTokenString(NULL,
2611 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2613 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2614 "Tab-Content %d needs to be able to produce a string, have %s",
2615 i, TP->Tokens->Params[i * 2 + 1]->Start);
2622 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2625 int i, ntabs, nTabs;
2627 nTabs = ntabs = TP->Tokens->nParameters / 2;
2628 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2629 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2631 for (i = 0; i < ntabs; i++) {
2632 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2633 (TP->Tokens->Params[i * 2]->len > 0)) {
2634 TabNames[i] = NewStrBuf();
2635 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2637 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2640 GetTemplateTokenString(Target,
2645 TabNames[i] = NewStrBufPlain(Ch, -1);
2648 /** A Tab without subject? we can't count that, add it as silent */
2653 StrTabbedDialog(Target, nTabs, TabNames);
2654 for (i = 0; i < ntabs; i++) {
2655 StrBeginTab(Target, i, nTabs);
2656 DoTemplate(TKEY(i * 2 + 1), Target, TP);
2657 StrEndTab(Target, i, nTabs);
2659 for (i = 0; i < ntabs; i++)
2660 FreeStrBuf(&TabNames[i]);
2664 /*-----------------------------------------------------------------------------
2669 void RegisterSortFunc(const char *name, long len,
2670 const char *prepend, long preplen,
2671 CompareFunc Forward,
2672 CompareFunc Reverse,
2673 CompareFunc GroupChange,
2676 SortStruct *NewSort;
2678 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2679 memset(NewSort, 0, sizeof(SortStruct));
2680 NewSort->Name = NewStrBufPlain(name, len);
2681 if (prepend != NULL)
2682 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2684 NewSort->PrefPrepend = NULL;
2685 NewSort->Forward = Forward;
2686 NewSort->Reverse = Reverse;
2687 NewSort->GroupChange = GroupChange;
2688 NewSort->ContextType = ContextType;
2689 if (ContextType == CTX_NONE) {
2690 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2694 Put(SortHash, name, len, NewSort, DestroySortStruct);
2697 CompareFunc RetrieveSort(WCTemplputParams *TP,
2698 const char *OtherPrefix, long OtherPrefixLen,
2699 const char *Default, long ldefault, long DefaultDirection)
2702 const StrBuf *BSort = NULL;
2705 long SortOrder = -1;
2707 if (havebstr("SortBy")) {
2708 BSort = sbstr("SortBy");
2709 if (OtherPrefix == NULL) {
2710 set_room_pref("sort", NewStrBufDup(BSort), 0);
2713 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2716 else { /** Try to fallback to our remembered values... */
2717 if (OtherPrefix == NULL) {
2718 BSort = get_room_pref("sort");
2721 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2724 putbstr("SortBy", NewStrBufDup(BSort));
2728 BSort = Buf = NewStrBufPlain(Default, ldefault);
2729 putbstr("SortBy", Buf);
2733 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2734 (vSortBy == NULL)) {
2736 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2737 (vSortBy == NULL)) {
2739 NULL, "Sorting", ERR_PARM1, TP,
2740 "Illegal default sort: [%s]", Default);
2744 SortBy = (SortStruct*)vSortBy;
2746 if (SortBy->ContextType != TP->Filter.ContextType)
2749 /** Ok, its us, lets see in which direction we should sort... */
2750 if (havebstr("SortOrder")) {
2751 SortOrder = LBSTR("SortOrder");
2753 else { /** Try to fallback to our remembered values... */
2755 if (SortBy->PrefPrepend == NULL) {
2756 Buf = get_room_pref("SortOrder");
2757 SortOrder = StrTol(Buf);
2760 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2764 SortOrder = DefaultDirection;
2766 Buf = NewStrBufPlain(NULL, 64);
2767 StrBufPrintf(Buf, "%ld", SortOrder);
2768 putbstr("SortOrder", Buf);
2770 switch (SortOrder) {
2775 return SortBy->Forward;
2777 return SortBy->Reverse;
2789 ConstStr SortIcons[] = {
2790 {HKEY("static/sort_none.gif")},
2791 {HKEY("static/up_pointer.gif")},
2792 {HKEY("static/down_pointer.gif")},
2795 ConstStr SortNextOrder[] = {
2802 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2804 int bSortError = eNOT_SPECIFIED;
2805 const StrBuf *BSort;
2810 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2812 return eNO_SUCH_SORT;
2813 *Param = (SortStruct*) vSort;
2816 if (havebstr("SortBy")) {
2817 BSort = sbstr("SortBy");
2818 bSortError = eINVALID_PARAM;
2819 if ((*Param)->PrefPrepend == NULL) {
2820 set_room_pref("sort", NewStrBufDup(BSort), 0);
2823 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2826 else { /** Try to fallback to our remembered values... */
2827 if ((*Param)->PrefPrepend == NULL) {
2828 BSort = get_room_pref("sort");
2831 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2835 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2839 *Next = (SortStruct*) vSort;
2841 /** Ok, its us, lets see in which direction we should sort... */
2842 if (havebstr("SortOrder")) {
2843 *SortOrder = LBSTR("SortOrder");
2845 else { /** Try to fallback to our remembered values... */
2846 if ((*Param)->PrefPrepend == NULL) {
2847 *SortOrder = StrTol(get_room_pref("SortOrder"));
2850 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2860 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2865 const ConstStr *SortIcon;
2867 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2870 Target, "Sorter", ERR_PARM1, TP,
2871 " Sorter [%s] unknown!",
2872 TP->Tokens->Params[0]->Start);
2874 case eINVALID_PARAM:
2875 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2876 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2878 case eNOT_SPECIFIED:
2880 if (Next == Param) {
2881 SortIcon = &SortIcons[SortOrder];
2883 else { /** Not Us... */
2884 SortIcon = &SortIcons[0];
2886 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
2890 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
2896 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2899 Target, "Sorter", ERR_PARM1, TP,
2900 " Sorter [%s] unknown!",
2901 TP->Tokens->Params[0]->Start);
2903 case eINVALID_PARAM:
2905 NULL, "Sorter", ERR_PARM1, TP,
2906 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2908 case eNOT_SPECIFIED:
2910 StrBufAppendBuf(Target, Param->Name, 0);
2915 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
2918 const ConstStr *SortOrderStr;
2922 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2925 Target, "Sorter", ERR_PARM1, TP,
2926 " Sorter [%s] unknown!",
2927 TP->Tokens->Params[0]->Start);
2929 case eINVALID_PARAM:
2931 NULL, "Sorter", ERR_PARM1, TP,
2932 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2934 case eNOT_SPECIFIED:
2936 if (Next == Param) {
2937 SortOrderStr = &SortNextOrder[SortOrder];
2939 else { /** Not Us... */
2940 SortOrderStr = &SortNextOrder[0];
2942 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2947 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
2949 long *LongVector = (long*) CTX;
2951 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
2952 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
2954 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
2958 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
2960 Target, "Longvector", ERR_NAME, TP,
2961 "needs a numerical Parameter!");
2965 Target, "LongVector", ERR_PARM1, TP,
2966 "doesn't have %ld Parameters, its just the size of %ld!",
2967 TP->Tokens->Params[0]->lvalue,
2973 void dbg_print_longvector(long *LongVector)
2975 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
2976 int nItems = LongVector[0];
2979 for (i = 0; i < nItems; i++) {
2981 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
2983 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
2986 lprintf(1, ChrPtr(Buf));
2990 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
2992 long *LongVector = (long*) CTX;
2994 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
2995 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
2996 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
2997 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
2999 return LongVector[TP->Tokens->Params[2]->lvalue] ==
3000 LongVector[TP->Tokens->Params[3]->lvalue];
3004 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
3005 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
3007 Target, "ConditionalLongvector", ERR_PARM1, TP,
3008 "needs two long Parameter!");
3012 Target, "Longvector", ERR_PARM1, TP,
3013 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
3014 TP->Tokens->Params[2]->lvalue,
3015 TP->Tokens->Params[3]->lvalue,
3023 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
3025 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
3032 memset(&NoCtx, 0, sizeof(WCTemplputParams));
3033 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
3034 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
3035 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
3036 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
3037 RegisterNamespace("CONTEXTSTRARR", 1, 2, tmplput_ContextStringArray, NULL, CTX_STRBUF);
3038 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
3039 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
3040 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, preeval_do_tabbed, CTX_NONE);
3041 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
3044 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
3045 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
3046 RegisterConditional(HKEY("COND:CONTEXTSTRARR"), 4, ConditionalContextStrinArray, CTX_STRBUFARR);
3047 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
3049 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
3050 conditional_ITERATE_ISGROUPCHANGE,
3052 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
3053 conditional_ITERATE_LASTN,
3055 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
3056 conditional_ITERATE_FIRSTN,
3059 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
3060 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
3061 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
3062 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
3063 RegisterNamespace("DEF:STR", 0, 0, tmplput_DefStr, NULL, CTX_NONE);
3064 RegisterNamespace("DEF:VAL", 0, 0, tmplput_DefVal, NULL, CTX_NONE);
3072 ServerStartModule_SUBST
3075 WirelessTemplateCache = NewHash(1, NULL);
3076 WirelessLocalTemplateCache = NewHash(1, NULL);
3077 LocalTemplateCache = NewHash(1, NULL);
3078 TemplateCache = NewHash(1, NULL);
3080 GlobalNS = NewHash(1, NULL);
3081 Iterators = NewHash(1, NULL);
3082 Conditionals = NewHash(1, NULL);
3083 SortHash = NewHash(1, NULL);
3084 Defines = NewHash(1, NULL);
3088 FinalizeModule_SUBST
3095 ServerShutdownModule_SUBST
3098 DeleteHash(&WirelessTemplateCache);
3099 DeleteHash(&WirelessLocalTemplateCache);
3100 DeleteHash(&TemplateCache);
3101 DeleteHash(&LocalTemplateCache);
3103 DeleteHash(&GlobalNS);
3104 DeleteHash(&Iterators);
3105 DeleteHash(&Conditionals);
3106 DeleteHash(&SortHash);
3107 DeleteHash(&Defines);
3112 SessionNewModule_SUBST
3119 SessionAttachModule_SUBST
3122 sess->vars = NewHash(1,NULL);
3126 SessionDetachModule_SUBST
3129 DeleteHash(&sess->vars);
3133 SessionDestroyModule_SUBST