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",
108 "Context LONGVECTOR",
113 "Context DavNamespace",
119 void DestroySortStruct(void *vSort)
121 SortStruct *Sort = (SortStruct*) vSort;
122 FreeStrBuf(&Sort->Name);
123 FreeStrBuf(&Sort->PrefPrepend);
127 const char *ContextName(int ContextType)
129 if (ContextType < CTX_UNKNOWN)
130 return CtxNames[ContextType];
132 return CtxNames[CTX_UNKNOWN];
135 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 Header = 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(Header, WildFireException(SKEY(TP->Tokens->FileName),
220 SerializeJson(Header, WildFireMessage(SKEY(TP->Tokens->FileName),
225 WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
232 "<pre>\n%s: %s\n</pre>\n",
236 StrBufPrintf(Info, "%s [%s] %s; [%s]",
240 ChrPtr(TP->Tokens->FlatToken));
241 SerializeJson(Header, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
242 WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
248 if (dbg_bactrace_template_errors)
256 void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
267 va_start(arg_ptr, Format);
268 StrBufVAppendPrintf(Error, Format, arg_ptr);
271 lprintf(1, ChrPtr(Error));
274 Header = NewStrBuf();
277 SerializeJson(Header, WildFireException(Type, strlen(Type),
281 WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
287 if (dbg_bactrace_template_errors)
293 void RegisterNS(const char *NSName,
297 WCHandlerFunc HandlerFunc,
298 WCPreevalFunc PreevalFunc,
301 HashHandler *NewHandler;
303 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
304 memset(NewHandler, 0, sizeof(HashHandler));
305 NewHandler->Filter.nMinArgs = nMinArgs;
306 NewHandler->Filter.nMaxArgs = nMaxArgs;
307 NewHandler->Filter.ContextType = ContextRequired;
308 NewHandler->Filter.ControlContextType = CTX_NONE;
310 NewHandler->PreEvalFunc = PreevalFunc;
311 NewHandler->HandlerFunc = HandlerFunc;
312 Put(GlobalNS, NSName, len, NewHandler, NULL);
315 void RegisterControlNS(const char *NSName,
319 WCHandlerFunc HandlerFunc,
320 int ControlContextRequired)
322 HashHandler *NewHandler;
324 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
325 memset(NewHandler, 0, sizeof(HashHandler));
326 NewHandler->Filter.nMinArgs = nMinArgs;
327 NewHandler->Filter.nMaxArgs = nMaxArgs;
328 NewHandler->Filter.ContextType = CTX_NONE;
329 NewHandler->Filter.ControlContextType = ControlContextRequired;
330 NewHandler->HandlerFunc = HandlerFunc;
331 Put(GlobalNS, NSName, len, NewHandler, NULL);
336 int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, const char *ErrType)
338 if ((Need->ContextType != CTX_NONE) &&
339 (Need->ContextType != TP->Filter.ContextType)) {
341 Target, ErrType, ERR_PARM1, TP,
342 " WARNING: requires Context: [%s], have [%s]!",
343 ContextName(Need->ContextType),
344 ContextName(TP->Filter.ContextType));
348 if ((Need->ControlContextType != CTX_NONE) &&
349 (Need->ControlContextType != TP->Filter.ControlContextType)) {
351 Target, ErrType, ERR_PARM1, TP,
352 " WARNING: requires Control Context: [%s], have [%s]!",
353 ContextName(Need->ControlContextType),
354 ContextName(TP->Filter.ControlContextType));
358 if (TP->Tokens->nParameters < Need->nMinArgs) {
359 LogTemplateError(Target, ErrType, ERR_NAME, TP,
360 "needs at least %ld params, have %ld",
362 TP->Tokens->nParameters);
366 else if (TP->Tokens->nParameters > Need->nMaxArgs) {
367 LogTemplateError(Target, ErrType, ERR_NAME, TP,
368 "just needs %ld params, you gave %ld",
370 TP->Tokens->nParameters);
378 void FreeToken(WCTemplateToken **Token)
381 FreeStrBuf(&(*Token)->FlatToken);
382 if ((*Token)->HaveParameters)
383 for (i = 0; i < (*Token)->nParameters; i++)
384 free((*Token)->Params[i]);
391 void FreeWCTemplate(void *vFreeMe)
394 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
396 if (FreeMe->TokenSpace > 0) {
397 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
398 FreeToken(&FreeMe->Tokens[i]);
400 free(FreeMe->Tokens);
402 FreeStrBuf(&FreeMe->FileName);
403 FreeStrBuf(&FreeMe->Data);
404 FreeStrBuf(&FreeMe->MimeType);
410 * debugging function to print array to log
412 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
415 * debugging function to print array to log
417 void VarPrintEntry(const char *Key, void *vSubst, int odd)
420 lprintf(1,"Subst[%s] : ", Key);
421 ptr = (wcsubst*) vSubst;
423 switch(ptr->wcs_type) {
425 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
428 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
431 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
434 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
441 * Clear out the list of substitution variables local to this session
443 void clear_substs(wcsession *wc) {
445 if (wc->vars != NULL) {
446 DeleteHash(&wc->vars);
451 * Clear out the list of substitution variables local to this session
453 void clear_local_substs(void) {
472 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
474 int NeedNew = NeedNewBuf(type);
475 switch(ptr->wcs_type) {
479 if (reusestrbuf && NeedNew) {
480 FlushStrBuf(ptr->wcs_value);
484 FreeStrBuf(&ptr->wcs_value);
485 ptr->wcs_value = NULL;
489 ptr->wcs_function = NULL;
490 if (reusestrbuf && NeedNew)
491 ptr->wcs_value = NewStrBuf();
494 ptr->wcs_value = NULL;
495 if (reusestrbuf && NeedNew)
496 ptr->wcs_value = NewStrBuf();
500 if (reusestrbuf && NeedNew)
501 ptr->wcs_value = NewStrBuf();
504 if (reusestrbuf && NeedNew)
505 ptr->wcs_value = NewStrBuf();
512 * destructor; kill one entry.
514 void deletevar(void *data)
516 wcsubst *ptr = (wcsubst*)data;
517 FlushPayload(ptr, 0, ptr->wcs_type);
522 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
527 ptr = (wcsubst *) malloc(sizeof(wcsubst));
528 memset(ptr, 0, sizeof(wcsubst));
530 ptr->wcs_type = type;
531 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
532 Put(WCC->vars, keyname, keylen, ptr, deletevar);
534 switch(ptr->wcs_type) {
537 ptr->wcs_value = NewStrBuf();
551 * Add a substitution variable (local to this session) (strlen version...)
552 * keyname the replacementstring to substitute
553 * keytype the kind of the key
554 * format the format string ala printf
555 * ... the arguments to substitute in the formatstring
557 void SVPRINTF(char *keyname, int keytype, const char *format,...)
565 keylen = strlen(keyname);
567 * First look if we're doing a replacement of
570 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
571 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
572 ptr = (wcsubst*)vPtr;
573 FlushPayload(ptr, keytype, keytype);
574 ptr->wcs_type = keytype;
576 else /** Otherwise allocate a new one */
578 ptr = NewSubstVar(keyname, keylen, keytype);
581 /* Format the string */
582 va_start(arg_ptr, format);
583 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
588 * Add a substitution variable (local to this session)
589 * keyname the replacementstring to substitute
590 * keytype the kind of the key
591 * format the format string ala printf
592 * ... the arguments to substitute in the formatstring
594 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
602 * First look if we're doing a replacement of
605 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
606 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
607 ptr = (wcsubst*)vPtr;
608 FlushPayload(ptr, 1, keytype);
609 ptr->wcs_type = keytype;
611 else /** Otherwise allocate a new one */
613 ptr = NewSubstVar(keyname, keylen, keytype);
616 /** Format the string and save it */
617 va_start(arg_ptr, format);
618 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
623 * Add a substitution variable (local to this session)
624 * keyname the replacementstring to substitute
625 * keytype the kind of the key
626 * format the format string ala printf
627 * ... the arguments to substitute in the formatstring
629 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
637 * First look if we're doing a replacement of
640 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
641 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
642 ptr = (wcsubst*)vPtr;
643 FlushPayload(ptr, 1, keytype);
644 ptr->wcs_type = keytype;
646 else /** Otherwise allocate a new one */
648 ptr = NewSubstVar(keyname, keylen, keytype);
650 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
654 * \brief Add a substitution variable (local to this session)
655 * \param keyname the replacementstring to substitute
656 * \param keytype the kind of the key
657 * \param format the format string ala printf
658 * \param ... the arguments to substitute in the formatstring
660 void SVPutLong(char *keyname, size_t keylen, long Data)
668 * First look if we're doing a replacement of
671 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
672 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
673 ptr = (wcsubst*)vPtr;
674 FlushPayload(ptr, 1, WCS_LONG);
675 ptr->wcs_type = WCS_LONG;
677 else /** Otherwise allocate a new one */
679 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
685 * \brief Add a substitution variable (local to this session) that does a callback
686 * \param keyname the keystring to substitute
687 * \param fcn_ptr the function callback to give the substitution string
689 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
696 * First look if we're doing a replacement of
699 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
700 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
701 ptr = (wcsubst*)vPtr;
702 FlushPayload(ptr, 1, WCS_FUNCTION);
703 ptr->wcs_type = WCS_FUNCTION;
705 else /** Otherwise allocate a new one */
707 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
710 ptr->wcs_function = fcn_ptr;
712 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
714 SVCallback(keyname, strlen(keyname), fcn_ptr);
719 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
726 * First look if we're doing a replacement of
729 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
730 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
731 ptr = (wcsubst*)vPtr;
732 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
733 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
735 else /** Otherwise allocate a new one */
737 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
739 ptr->wcs_value = (StrBuf*)Buf;
743 * \brief back end for print_value_of() ... does a server command
744 * \param servcmd server command to execute on the citadel server
746 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
750 serv_puts(ChrPtr(servcmd));
751 len = serv_getln(buf, sizeof buf);
757 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
760 _fmout(Target, "CENTER");
763 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
769 int HaveTemplateTokenString(StrBuf *Target,
770 WCTemplputParams *TP,
775 if (N >= TP->Tokens->nParameters) {
779 switch (TP->Tokens->Params[N]->Type) {
785 case TYPE_SUBTEMPLATE:
794 void GetTemplateTokenString(StrBuf *Target,
795 WCTemplputParams *TP,
801 WCTemplputParams SubTP;
803 if (N >= TP->Tokens->nParameters) {
804 LogTemplateError(Target,
805 "TokenParameter", N, TP,
806 "invalid token %d. this shouldn't have come till here.\n", N);
812 switch (TP->Tokens->Params[N]->Type) {
816 *Value = TP->Tokens->Params[N]->Start;
817 *len = TP->Tokens->Params[N]->len;
820 if (TP->Tokens->Params[N]->len == 0) {
821 LogTemplateError(Target,
822 "TokenParameter", N, TP,
823 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
828 Buf = (StrBuf*) SBstr(TKEY(N));
829 *Value = ChrPtr(Buf);
830 *len = StrLength(Buf);
833 if (TP->Tokens->Params[N]->len == 0) {
834 LogTemplateError(Target,
835 "TokenParameter", N, TP,
836 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
841 get_PREFERENCE(TKEY(N), &Buf);
842 *Value = ChrPtr(Buf);
843 *len = StrLength(Buf);
846 LogTemplateError(Target,
847 "TokenParameter", N, TP,
848 "Requesting parameter %d; of type LONG, want string.", N);
851 LogTemplateError(Target,
852 "TokenParameter", N, TP,
853 "Requesting parameter %d; of type PREFINT, want string.", N);
856 *Value = _(TP->Tokens->Params[N]->Start);
857 *len = strlen(*Value);
859 case TYPE_SUBTEMPLATE:
860 if (TP->Tokens->Params[N]->len == 0) {
861 LogTemplateError(Target,
862 "TokenParameter", N, TP,
863 "Requesting parameter %d; of type SUBTEMPLATE, empty lookup string not admitted.", N);
869 memset(&SubTP, 0, sizeof(WCTemplputParams *));
870 SubTP.Context = TP->Context;
871 SubTP.Filter.ContextType = TP->Filter.ContextType;
873 DoTemplate(TKEY(N), Buf, &SubTP);
874 *Value = ChrPtr(Buf);
875 *len = StrLength(Buf);
876 /* we can't free it here, so we put it into the subst so its discarded later on. */
877 SVPUTBuf(TKEY(N), Buf, 0);
881 LogTemplateError(Target,
882 "TokenParameter", N, TP,
883 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
888 long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
891 if (N >= TP->Tokens->nParameters) {
892 LogTemplateError(Target,
893 "TokenParameter", N, TP,
894 "invalid token %d. this shouldn't have come till here.\n", N);
899 switch (TP->Tokens->Params[N]->Type) {
902 return atol(TP->Tokens->Params[N]->Start);
905 if (TP->Tokens->Params[N]->len == 0) {
906 LogTemplateError(Target,
907 "TokenParameter", N, TP,
908 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
911 return LBstr(TKEY(N));
914 LogTemplateError(Target,
915 "TokenParameter", N, TP,
916 "requesting a prefstring in param %d want a number", N);
917 if (TP->Tokens->Params[N]->len == 0) {
918 LogTemplateError(Target,
919 "TokenParameter", N, TP,
920 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
923 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
928 return TP->Tokens->Params[N]->lvalue;
930 if (TP->Tokens->Params[N]->len == 0) {
931 LogTemplateError(Target,
932 "TokenParameter", N, TP,
933 "Requesting parameter %d; of type PREFINT, empty lookup string not admitted.", N);
936 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
940 LogTemplateError(Target,
941 "TokenParameter", N, TP,
942 "requesting a I18N string in param %d; want a number", N);
944 case TYPE_SUBTEMPLATE:
945 LogTemplateError(Target,
946 "TokenParameter", N, TP,
947 "requesting a subtemplate in param %d; not supported for numbers", N);
950 LogTemplateError(Target,
951 "TokenParameter", N, TP,
952 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
960 * \brief Print the value of a variable
961 * \param keyname get a key to print
963 void print_value_of(StrBuf *Target, WCTemplputParams *TP)
970 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
971 /* TODO: depricated! */
972 if (TP->Tokens->pName[0] == '=') {
973 DoTemplate(TP->Tokens->pName+1, TP->Tokens->NameEnd - 1, NULL, &NoCtx);
975 /*/////TODO: if param[1] == "U" -> urlescape
977 /** Page-local variables */
978 if ((WCC->vars!= NULL) && GetHash(WCC->vars, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
979 ptr = (wcsubst*) vVar;
980 switch(ptr->wcs_type) {
982 StrBufAppendBuf(Target, ptr->wcs_value, 0);
985 pvo_do_cmd(Target, ptr->wcs_value);
988 (*ptr->wcs_function) (Target, TP);
992 StrBufAppendBuf(Target, ptr->wcs_value, 0);
995 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
999 Target, "Subst", ERR_NAME, TP,
1000 "WARNING: invalid value in SV-Hash at %s!", TP->Tokens->pName);
1005 Target, "Token", ERR_NAME, TP,
1006 "didn't find Handler \"%s\"", TP->Tokens->pName);
1011 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
1013 wcsession *WCC = WC;
1017 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1018 ParamToLookup->len, &vVar)) {
1019 ptr = (wcsubst*) vVar;
1020 switch(ptr->wcs_type) {
1023 case WCS_STRBUF_REF:
1024 if (ParamToCompare->Type == TYPE_STR)
1025 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
1026 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
1028 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
1036 if (ParamToCompare->Type == TYPE_STR)
1039 return ParamToCompare->lvalue == ptr->lvalue;
1042 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1043 ParamToLookup->Start);
1049 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
1051 wcsession *WCC = WC;
1055 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1056 ParamToLookup->len, &vVar)) {
1057 ptr = (wcsubst*) vVar;
1058 switch(ptr->wcs_type) {
1061 case WCS_STRBUF_REF:
1062 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
1063 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
1070 return StrTol(Compare) == ptr->lvalue;
1072 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1073 ParamToLookup->Start);
1082 * \brief puts string into the template and computes which escape methon we should use
1083 * \param Source the string we should put into the template
1084 * \param FormatTypeIndex where should we look for escape types if?
1086 void StrBufAppendTemplate(StrBuf *Target,
1087 WCTemplputParams *TP,
1088 const StrBuf *Source, int FormatTypeIndex)
1091 char EscapeAs = ' ';
1093 if ((FormatTypeIndex < TP->Tokens->nParameters) &&
1094 (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
1095 (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
1096 EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
1103 StrEscAppend(Target, Source, NULL, 0, 2);
1106 StrEscAppend(Target, Source, NULL, 0, 0);
1109 StrECMAEscAppend(Target, Source, NULL);
1112 StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 0);
1115 StrBufUrlescAppend(Target, Source, NULL);
1118 StrBufAppendBuf(Target, Source, 0);
1123 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
1125 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
1126 if (Template->TokenSpace <= 0) {
1127 Template->Tokens = (WCTemplateToken**)malloc(
1128 sizeof(WCTemplateToken*) * 10);
1129 memset(Template->Tokens, 0, sizeof(WCTemplateToken*) * 10);
1130 Template->TokenSpace = 10;
1133 WCTemplateToken **NewTokens;
1135 NewTokens= (WCTemplateToken**) malloc(
1136 sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1139 0, sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1143 sizeof(WCTemplateToken*) * Template->nTokensUsed);
1145 free(Template->Tokens);
1146 Template->TokenSpace *= 2;
1147 Template->Tokens = NewTokens;
1150 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
1153 TemplateParam *GetNextParameter(StrBuf *Buf,
1156 WCTemplateToken *Tokens,
1158 WCTemplputParams *TP)
1160 const char *pch = *pCh;
1161 const char *pchs, *pche;
1162 TemplateParam *Parm;
1166 Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
1167 memset(Parm, 0, sizeof(TemplateParam));
1168 Parm->Type = TYPE_STR;
1170 /* Skip leading whitespaces */
1171 while ((*pch == ' ' )||
1174 (*pch == '\n')) pch ++;
1177 Parm->Type = TYPE_PREFSTR;
1184 else if (*pch == ';') {
1185 Parm->Type = TYPE_PREFINT;
1192 else if (*pch == '#') {
1193 Parm->Type = TYPE_INTDEFINE;
1196 else if (*pch == '_') {
1197 Parm->Type = TYPE_GETTEXT;
1204 else if (*pch == 'B') {
1205 Parm->Type = TYPE_BSTR;
1212 else if (*pch == '=') {
1213 Parm->Type = TYPE_SUBTEMPLATE;
1224 else if (*pch == '\'')
1226 if (quote != '\0') {
1231 ( (pch > pchs) && (*(pch - 1) == '\\'))
1236 if (*pch != quote) {
1237 lprintf(1, "Error (in '%s' line %ld); "
1238 "evaluating template param [%s] in Token [%s]\n",
1239 ChrPtr(pTmpl->FileName),
1241 ChrPtr(Tokens->FlatToken),
1248 StrBufPeek(Buf, pch, -1, '\0');
1249 if (LoadTemplates > 1) {
1250 lprintf(1, "DBG: got param [%s] %ld %ld\n",
1251 pchs, pche - pchs, strlen(pchs));
1254 Parm->len = pche - pchs;
1255 pch ++; /* move after trailing quote */
1256 if (ParamBrace && (*pch == ')')) {
1263 Parm->Type = TYPE_LONG;
1265 while ((pch <= pe) &&
1271 if (pch - pchs > 1){
1272 StrBufPeek(Buf, pch, -1, '\0');
1273 Parm->lvalue = atol(pchs);
1280 lprintf(1, "Error (in '%s' line %ld); "
1281 "evaluating long template param [%s] in Token [%s]\n",
1282 ChrPtr(pTmpl->FileName),
1284 ChrPtr(Tokens->FlatToken),
1291 while ((*pch == ' ' )||
1295 (*pch == '\n')) pch ++;
1300 if (DumpTemplateI18NStrings) {
1301 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
1304 case TYPE_INTDEFINE: {
1307 if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
1311 PVal = (long*) vPVal;
1313 Parm->lvalue = *PVal;
1317 LogTemplateError(NULL, "Define", ERR_PARM1, TP,
1322 case TYPE_SUBTEMPLATE:{
1324 /* well, we don't check the mobile stuff here... */
1325 if (!GetHash(LocalTemplateCache, Parm->Start, Parm->len, &vTmpl) &&
1326 !GetHash(TemplateCache, Parm->Start, Parm->len, &vTmpl)) {
1328 NULL, "SubTemplate", ERR_PARM1, TP,
1329 "referenced here doesn't exist");
1337 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
1339 const char *pTokenStart,
1340 const char *pTokenEnd,
1346 TemplateParam *Param;
1347 WCTemplateToken *NewToken;
1348 WCTemplputParams TP;
1350 NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
1351 memset(NewToken, 0, sizeof(WCTemplateToken));
1352 TP.Tokens = NewToken;
1353 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
1354 NewToken->Flags = 0;
1355 NewToken->Line = Line + 1;
1356 NewToken->pTokenStart = pTokenStart;
1357 NewToken->TokenStart = pTokenStart - pStart;
1358 NewToken->TokenEnd = (pTokenEnd - pStart) - NewToken->TokenStart;
1359 NewToken->pTokenEnd = pTokenEnd;
1360 NewToken->NameEnd = NewToken->TokenEnd - 2;
1361 NewToken->PreEval = NULL;
1362 NewToken->FlatToken = NewStrBufPlain(pTokenStart + 2, pTokenEnd - pTokenStart - 2);
1363 StrBufShrinkToFit(NewToken->FlatToken, 1);
1365 StrBufPeek(Buf, pTokenStart, + 1, '\0');
1366 StrBufPeek(Buf, pTokenEnd, -1, '\0');
1367 pch = NewToken->pName = pTokenStart + 2;
1369 NewToken->HaveParameters = 0;;
1370 NewToken->nParameters = 0;
1372 while (pch < pTokenEnd - 1) {
1374 StrBufPeek(Buf, pch, -1, '\0');
1375 NewToken->NameEnd = pch - NewToken->pName;
1377 if (*(pTokenEnd - 1) != ')') {
1379 NULL, "Parseerror", ERR_NAME, &TP,
1380 "Warning, Non welformed Token; missing right parenthesis");
1382 while (pch < pTokenEnd - 1) {
1383 Param = GetNextParameter(Buf, &pch, pTokenEnd - 1, NewToken, pTmpl, &TP);
1384 if (Param != NULL) {
1385 NewToken->HaveParameters = 1;
1386 if (NewToken->nParameters > MAXPARAM) {
1388 NULL, "Parseerror", ERR_NAME, &TP,
1389 "only [%d] Params allowed in Tokens",
1393 FreeToken(&NewToken);
1396 NewToken->Params[NewToken->nParameters++] = Param;
1400 if((NewToken->NameEnd == 1) &&
1401 (NewToken->HaveParameters == 1))
1404 if (*(NewToken->pName) == '_')
1405 NewToken->Flags = SV_GETTEXT;
1406 else if (*(NewToken->pName) == '=')
1407 NewToken->Flags = SV_SUBTEMPL;
1408 else if (*(NewToken->pName) == '%')
1409 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
1410 else if (*(NewToken->pName) == '?')
1411 NewToken->Flags = SV_CONDITIONAL;
1412 else if (*(NewToken->pName) == '!')
1413 NewToken->Flags = SV_NEG_CONDITIONAL;
1419 switch (NewToken->Flags) {
1421 /* If we're able to find out more about the token, do it now while its fresh. */
1422 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1423 HashHandler *Handler;
1424 Handler = (HashHandler*) vVar;
1425 if ((NewToken->nParameters < Handler->Filter.nMinArgs) ||
1426 (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
1428 NULL, "Token", ERR_NAME, &TP,
1429 "doesn't work with %d params",
1430 NewToken->nParameters);
1434 NewToken->PreEval = Handler;
1435 NewToken->Flags = SV_PREEVALUATED;
1436 if (Handler->PreEvalFunc != NULL)
1437 Handler->PreEvalFunc(NewToken);
1442 if (NewToken->nParameters !=1) {
1444 NULL, "Gettext", ERR_NAME, &TP,
1445 "requires exactly 1 parameter, you gave %d params",
1446 NewToken->nParameters);
1447 NewToken->Flags = 0;
1450 if (DumpTemplateI18NStrings) {
1451 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
1455 if (NewToken->nParameters != 1) {
1457 NULL, "Subtemplates", ERR_NAME, &TP,
1458 "require exactly 1 parameter, you gave %d params",
1459 NewToken->nParameters);
1464 /* well, we don't check the mobile stuff here... */
1465 if (!GetHash(LocalTemplateCache,
1466 NewToken->Params[0]->Start,
1467 NewToken->Params[0]->len,
1469 !GetHash(TemplateCache,
1470 NewToken->Params[0]->Start,
1471 NewToken->Params[0]->len,
1474 NULL, "SubTemplate", ERR_PARM1, &TP,
1479 case SV_CUST_STR_CONDITIONAL:
1480 case SV_CONDITIONAL:
1481 case SV_NEG_CONDITIONAL:
1482 if (NewToken->nParameters <2) {
1484 NULL, "Conditional", ERR_PARM1, &TP,
1485 "require at least 2 parameters, you gave %d params",
1486 NewToken->nParameters);
1487 NewToken->Flags = 0;
1490 if (NewToken->Params[1]->lvalue == 0) {
1492 NULL, "Conditional", ERR_PARM1, &TP,
1493 "Conditional ID (Parameter 1) mustn't be 0!");
1494 NewToken->Flags = 0;
1497 if (!GetHash(Conditionals,
1498 NewToken->Params[0]->Start,
1499 NewToken->Params[0]->len,
1502 if ((NewToken->Params[0]->len == 1) &&
1503 (NewToken->Params[0]->Start[0] == 'X'))
1506 NULL, "Conditional", ERR_PARM1, &TP,
1509 NewToken->Error = NewStrBuf();
1512 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1513 NewToken->Params[0]->Start,
1514 ChrPtr(pTmpl->FileName),
1516 ChrPtr(NewToken->FlatToken));
1520 NewToken->PreEval = vVar;
1532 * \brief Display a variable-substituted template
1533 * \param templatename template file to load
1535 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1537 WCTemplate *NewTemplate;
1539 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1540 memset(NewTemplate, 0, sizeof(WCTemplate));
1541 NewTemplate->Data = NULL;
1542 NewTemplate->FileName = NewStrBufDup(filename);
1543 StrBufShrinkToFit(NewTemplate->FileName, 1);
1544 NewTemplate->nTokensUsed = 0;
1545 NewTemplate->TokenSpace = 0;
1546 NewTemplate->Tokens = NULL;
1547 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1548 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1549 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1552 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1553 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1556 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1561 * \brief Display a variable-substituted template
1562 * \param templatename template file to load
1564 void *duplicate_template(WCTemplate *OldTemplate)
1566 WCTemplate *NewTemplate;
1568 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1569 memset(NewTemplate, 0, sizeof(WCTemplate));
1570 NewTemplate->Data = NULL;
1571 NewTemplate->FileName = NewStrBufDup(OldTemplate->FileName);
1572 StrBufShrinkToFit(NewTemplate->FileName, 1);
1573 NewTemplate->nTokensUsed = 0;
1574 NewTemplate->TokenSpace = 0;
1575 NewTemplate->Tokens = NULL;
1576 NewTemplate->MimeType = NewStrBufDup(OldTemplate->MimeType);
1581 * \brief Display a variable-substituted template
1582 * \param templatename template file to load
1584 void *load_template(WCTemplate *NewTemplate)
1587 struct stat statbuf;
1588 const char *pS, *pE, *pch, *Err;
1592 fd = open(ChrPtr(NewTemplate->FileName), O_RDONLY);
1594 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1595 ChrPtr(NewTemplate->FileName), strerror(errno));
1599 if (fstat(fd, &statbuf) == -1) {
1600 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1601 ChrPtr(NewTemplate->FileName), strerror(errno));
1605 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size + 1);
1606 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1608 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1609 ChrPtr(NewTemplate->FileName), strerror(errno));
1610 //FreeWCTemplate(NewTemplate);/////tODO
1616 StrBufShrinkToFit(NewTemplate->Data, 1);
1617 StrBufShrinkToFit(NewTemplate->MimeType, 1);
1618 pS = pch = ChrPtr(NewTemplate->Data);
1619 pE = pS + StrLength(NewTemplate->Data);
1621 const char *pts, *pte;
1623 int InDoubleQuotes = 0;
1625 /** Find one <? > */
1627 for (; pch < pE; pch ++) {
1628 if ((*pch=='<')&&(*(pch + 1)=='?') &&
1629 !((pch == pS) && /* we must ommit a <?xml */
1630 (*(pch + 2) == 'x') &&
1631 (*(pch + 3) == 'm') &&
1632 (*(pch + 4) == 'l')))
1634 if (*pch=='\n') Line ++;
1640 /** Found one? parse it. */
1641 for (; pch <= pE - 1; pch ++) {
1643 InDoubleQuotes = ! InDoubleQuotes;
1644 else if (*pch == '\'')
1645 InQuotes = ! InQuotes;
1646 else if ((!InQuotes && !InDoubleQuotes) &&
1647 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1655 PutNewToken(NewTemplate,
1656 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1663 const char* PrintTemplate(void *vSubst)
1665 WCTemplate *Tmpl = vSubst;
1667 return ChrPtr(Tmpl->FileName);
1671 int LoadTemplateDir(const StrBuf *DirName, HashList *wireless, HashList *big, const StrBuf *BaseKey)
1677 StrBuf *SubDirectory;
1678 DIR *filedir = NULL;
1679 struct dirent *filedir_entry;
1686 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
1691 filedir = opendir (ChrPtr(DirName));
1692 if (filedir == NULL) {
1697 Toplevel = StrLength(BaseKey) == 0;
1698 SubDirectory = NewStrBuf();
1699 SubKey = NewStrBuf();
1700 FileName = NewStrBufPlain(NULL, PATH_MAX);
1702 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
1703 (filedir_entry != NULL))
1707 #ifdef _DIRENT_HAVE_D_NAMELEN
1708 d_namelen = filedir_entry->d_namelen;
1709 d_type = filedir_entry->d_type;
1713 #define DT_UNKNOWN 0
1718 #define IFTODT(mode) (((mode) & 0170000) >> 12)
1719 #define DTTOIF(dirtype) ((dirtype) << 12)
1721 d_namelen = strlen(filedir_entry->d_name);
1722 d_type = DT_UNKNOWN;
1724 d_without_ext = d_namelen;
1726 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1727 continue; /* Ignore backup files... */
1729 if ((d_namelen == 1) &&
1730 (filedir_entry->d_name[0] == '.'))
1733 if ((d_namelen == 2) &&
1734 (filedir_entry->d_name[0] == '.') &&
1735 (filedir_entry->d_name[1] == '.'))
1738 if (d_type == DT_UNKNOWN) {
1740 char path[PATH_MAX];
1741 snprintf(path, PATH_MAX, "%s/%s",
1742 ChrPtr(DirName), filedir_entry->d_name);
1743 if (stat(path, &s) == 0) {
1744 d_type = IFTODT(s.st_mode);
1750 /* Skip directories we are not interested in... */
1751 if (strcmp(filedir_entry->d_name, ".svn") == 0)
1754 FlushStrBuf(SubKey);
1756 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1757 StrBufAppendBuf(SubKey, BaseKey, 0);
1758 StrBufAppendBufPlain(SubKey, HKEY("_"), 0);
1760 StrBufAppendBufPlain(SubKey, filedir_entry->d_name, d_namelen, 0);
1762 FlushStrBuf(SubDirectory);
1763 StrBufAppendBuf(SubDirectory, DirName, 0);
1764 if (ChrPtr(SubDirectory)[StrLength(SubDirectory) - 1] != '/')
1765 StrBufAppendBufPlain(SubDirectory, HKEY("/"), 0);
1766 StrBufAppendBufPlain(SubDirectory, filedir_entry->d_name, d_namelen, 0);
1768 LoadTemplateDir(SubDirectory, wireless, big, SubKey);
1771 case DT_LNK: /* TODO: check whether its a file or a directory */
1775 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1777 if ((d_without_ext == 0) || (d_namelen < 3))
1779 if (((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~') ||
1780 (strcmp(&filedir_entry->d_name[d_without_ext], ".orig") == 0) ||
1781 (strcmp(&filedir_entry->d_name[d_without_ext], ".swp") == 0))
1782 continue; /* Ignore backup files... */
1783 /* .m.xxx is for mobile useragents! */
1785 if (d_without_ext > 2)
1786 IsMobile = (filedir_entry->d_name[d_without_ext - 1] == 'm') &&
1787 (filedir_entry->d_name[d_without_ext - 2] == '.');
1788 PStart = filedir_entry->d_name;
1789 StrBufPrintf(FileName, "%s/%s", ChrPtr(DirName), filedir_entry->d_name);
1790 MinorPtr = strchr(filedir_entry->d_name, '.');
1791 if (MinorPtr != NULL)
1795 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1796 StrBufAppendBuf(Key, BaseKey, 0);
1797 StrBufAppendBufPlain(Key, HKEY("_"), 0);
1799 StrBufAppendBufPlain(Key, filedir_entry->d_name, MinorPtr - filedir_entry->d_name, 0);
1801 if (LoadTemplates >= 1)
1802 lprintf(1, "%s %d %s\n", ChrPtr(FileName), IsMobile, ChrPtr(Key));
1803 prepare_template(FileName, Key, (IsMobile)?wireless:big);
1810 FreeStrBuf(&FileName);
1812 FreeStrBuf(&SubDirectory);
1813 FreeStrBuf(&SubKey);
1817 void InitTemplateCache(void)
1822 HashList *Templates[4];
1827 /* Primary Template set... */
1828 StrBufPrintf(Dir, "%s/t", static_dirs[0]);
1829 LoadTemplateDir(Dir,
1830 WirelessTemplateCache,
1834 /* User local Template set */
1835 StrBufPrintf(Dir, "%s/t", static_dirs[1]);
1836 LoadTemplateDir(Dir,
1837 WirelessLocalTemplateCache,
1841 /* Debug Templates, just to be loaded while debugging. */
1843 StrBufPrintf(Dir, "%s/dbg", static_dirs[0]);
1844 LoadTemplateDir(Dir,
1845 WirelessTemplateCache,
1848 Templates[0] = WirelessTemplateCache;
1849 Templates[1] = TemplateCache;
1850 Templates[2] = WirelessLocalTemplateCache;
1851 Templates[3] = LocalTemplateCache;
1854 if (LoadTemplates == 0)
1855 for (i=0; i < 4; i++) {
1861 At = GetNewHashPos(Templates[i], 0);
1862 while (GetNextHashPos(Templates[i],
1867 (vTemplate != NULL))
1869 load_template((WCTemplate *)vTemplate);
1881 /*-----------------------------------------------------------------------------
1882 * Filling & processing Templates
1885 * \brief executes one token
1886 * \param Target buffer to append to
1887 * \param Token da to process.
1888 * \param Template we're iterating
1889 * \param Context Contextpoointer to pass in
1890 * \param state are we in conditional state?
1891 * \param ContextType what type of information does context giv us?
1893 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1895 const char *AppendMe;
1897 HashHandler *Handler;
1900 /* much output, since pName is not terminated...
1901 lprintf(1,"Doing token: %s\n",Token->pName);
1904 switch (TP->Tokens->Flags) {
1906 TmplGettext(Target, TP);
1908 case SV_CONDITIONAL: /** Forward conditional evaluation */
1909 return EvaluateConditional(Target, 1, state, TP);
1911 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1912 return EvaluateConditional(Target, 0, state, TP);
1914 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1915 if (TP->Tokens->nParameters >= 6) {
1916 if (EvaluateConditional(Target, 0, state, TP)) {
1917 GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
1918 StrBufAppendBufPlain(Target,
1924 GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
1925 StrBufAppendBufPlain(Target,
1933 Target, "Conditional", ERR_NAME, TP,
1934 "needs at least 6 Params!");
1938 if (TP->Tokens->nParameters == 1)
1939 DoTemplate(TKEY(0), Target, TP);
1941 case SV_PREEVALUATED:
1942 Handler = (HashHandler*) TP->Tokens->PreEval;
1943 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1946 Handler->HandlerFunc(Target, TP);
1949 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
1950 Handler = (HashHandler*) vVar;
1951 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1955 Handler->HandlerFunc(Target, TP);
1959 print_value_of(Target, TP);
1967 const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
1969 WCTemplate *pTmpl = Tmpl;
1972 const char *pData, *pS;
1974 WCTemplputParams TP;
1976 memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
1978 TP.Context = CallingTP->Context;
1979 TP.ControlContext = CallingTP->ControlContext;
1981 if (LoadTemplates != 0) {
1982 if (LoadTemplates > 1)
1983 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1984 ChrPtr(Tmpl->FileName));
1985 pTmpl = duplicate_template(Tmpl);
1986 if(load_template(pTmpl) == NULL) {
1989 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1990 ChrPtr(Tmpl->FileName));
1991 FreeWCTemplate(pTmpl);
1997 pS = pData = ChrPtr(pTmpl->Data);
1998 len = StrLength(pTmpl->Data);
2002 if (i >= pTmpl->nTokensUsed) {
2003 StrBufAppendBufPlain(Target,
2005 len - (pData - pS), 0);
2009 StrBufAppendBufPlain(
2011 pTmpl->Tokens[i]->pTokenStart - pData, 0);
2012 TP.Tokens = pTmpl->Tokens[i];
2013 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2014 state = EvaluateToken(Target, state, &TP);
2016 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
2017 /* condition told us to skip till its end condition */
2019 TP.Tokens = pTmpl->Tokens[i];
2020 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2021 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
2022 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
2023 if (state == EvaluateConditional(
2025 pTmpl->Tokens[i]->Flags,
2031 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
2032 if (i > pTmpl->nTokensUsed)
2036 if (LoadTemplates != 0) {
2037 FreeWCTemplate(pTmpl);
2039 return Tmpl->MimeType;
2044 * \brief Display a variable-substituted template
2045 * \param templatename template file to load
2046 * \returns the mimetype of the template its doing
2048 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
2050 WCTemplputParams LocalTP;
2052 HashList *StaticLocal;
2058 memset(&LocalTP, 0, sizeof(WCTemplputParams));
2062 if (WC->is_mobile > 0) {
2063 Static = WirelessTemplateCache;
2064 StaticLocal = WirelessLocalTemplateCache;
2067 Static = TemplateCache;
2068 StaticLocal = LocalTemplateCache;
2073 lprintf (1, "Can't to load a template with empty name!\n");
2074 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
2078 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
2079 !GetHash(Static, templatename, len, &vTmpl)) {
2080 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
2081 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
2083 (long)strlen(templatename));
2085 dbg_PrintHash(Static, PrintTemplate, NULL);
2086 PrintHash(Static, VarPrintTransition, PrintTemplate);
2092 return ProcessTemplate(vTmpl, Target, TP);
2096 /*-----------------------------------------------------------------------------
2099 typedef struct _HashIterator {
2100 HashList *StaticList;
2101 int AdditionalParams;
2103 int XPectContextType;
2105 RetrieveHashlistFunc GetHash;
2106 HashDestructorFunc Destructor;
2107 SubTemplFunc DoSubTemplate;
2110 void RegisterITERATOR(const char *Name, long len,
2111 int AdditionalParams,
2112 HashList *StaticList,
2113 RetrieveHashlistFunc GetHash,
2114 SubTemplFunc DoSubTempl,
2115 HashDestructorFunc Destructor,
2117 int XPectContextType,
2122 It = (HashIterator*)malloc(sizeof(HashIterator));
2123 memset(It, 0, sizeof(HashIterator));
2124 It->StaticList = StaticList;
2125 It->AdditionalParams = AdditionalParams;
2126 It->GetHash = GetHash;
2127 It->DoSubTemplate = DoSubTempl;
2128 It->Destructor = Destructor;
2129 It->ContextType = ContextType;
2130 It->XPectContextType = XPectContextType;
2132 Put(Iterators, Name, len, It, NULL);
2135 typedef struct _iteratestruct {
2144 int preeval_iterate(WCTemplateToken *Token)
2146 WCTemplputParams TPP;
2147 WCTemplputParams *TP;
2151 memset(&TPP, 0, sizeof(WCTemplputParams));
2154 if (!GetHash(Iterators, TKEY(0), &vIt)) {
2156 NULL, "Iterator", ERR_PARM1, TP,
2160 if (TP->Tokens->Params[1]->Type != TYPE_SUBTEMPLATE) {
2161 LogTemplateError(NULL, "Iterator", ERR_PARM1, TP,
2162 "Need token with type Subtemplate as param 1, have %s",
2163 TP->Tokens->Params[1]->Start);
2166 /* well, we don't check the mobile stuff here... */
2167 if (!GetHash(LocalTemplateCache, TKEY(1), &vTmpl) &&
2168 !GetHash(TemplateCache, TKEY(1), &vTmpl)) {
2169 LogTemplateError(NULL, "SubTemplate", ERR_PARM1, TP,
2170 "referenced here doesn't exist");
2172 Token->Preeval2 = vIt;
2176 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
2181 SortStruct *SortBy = NULL;
2183 int DetectGroupChange = 0;
2186 void *vLastContext = NULL;
2188 WCTemplputParams SubTP;
2189 IterateStruct Status;
2195 memset(&Status, 0, sizeof(IterateStruct));
2196 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2198 It = (HashIterator*) TP->Tokens->Preeval2;
2201 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
2205 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2207 Target, "Iterator", ERR_PARM1, TP,
2208 "doesn't work with %d params",
2209 TP->Tokens->nParameters);
2213 if ((It->XPectContextType != CTX_NONE) &&
2214 (It->XPectContextType != TP->Filter.ContextType)) {
2216 Target, "Iterator", ERR_PARM1, TP,
2217 "requires context of type %d, have %d",
2218 It->XPectContextType,
2219 TP->Filter.ContextType);
2224 if (It->StaticList == NULL)
2225 List = It->GetHash(Target, TP);
2227 List = It->StaticList;
2229 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2230 if (DetectGroupChange) {
2231 const StrBuf *BSort;
2232 DetectGroupChange = 0;
2233 if (havebstr("SortBy")) {
2234 BSort = sbstr("SortBy");
2235 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2236 (vSortBy != NULL)) {
2237 SortBy = (SortStruct*)vSortBy;
2238 /** Ok, its us, lets see in which direction we should sort... */
2239 if (havebstr("SortOrder")) {
2241 SortOrder = LBSTR("SortOrder");
2243 DetectGroupChange = 1;
2248 nMembersUsed = GetCount(List);
2249 SubBuf = NewStrBuf();
2250 SubTP.Filter.ContextType = It->ContextType;
2251 SubTP.Filter.ControlContextType = CTX_ITERATE;
2252 SubTP.ControlContext = &Status;
2254 if (HAVE_PARAM(2)) {
2255 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2257 if (HAVE_PARAM(3)) {
2258 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2260 if (HAVE_PARAM(4)) {
2261 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2263 it = GetNewHashPos(List, StepWidth);
2265 StopAt = GetCount(List);
2267 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2268 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2269 if (DetectGroupChange && Status.n > 0) {
2270 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2272 Status.LastN = (Status.n + 1) == nMembersUsed;
2273 SubTP.Context = vContext;
2274 if (It->DoSubTemplate != NULL)
2275 It->DoSubTemplate(SubBuf, &SubTP);
2276 DoTemplate(TKEY(1), SubBuf, &SubTP);
2278 StrBufAppendBuf(Target, SubBuf, 0);
2279 FlushStrBuf(SubBuf);
2280 Status.oddeven = ! Status.oddeven;
2281 vLastContext = vContext;
2285 FreeStrBuf(&SubBuf);
2287 if (It->Destructor != NULL)
2288 It->Destructor(&List);
2292 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2294 IterateStruct *Ctx = CCTX;
2295 if (TP->Tokens->nParameters < 3)
2296 return Ctx->GroupChange;
2298 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2301 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2303 IterateStruct *Ctx = CCTX;
2305 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2307 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2311 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2313 IterateStruct *Ctx = CCTX;
2315 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2319 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2321 IterateStruct *Ctx = CCTX;
2322 StrBufAppendPrintf(Target, "%d", Ctx->n);
2325 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2327 IterateStruct *Ctx = CCTX;
2331 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2333 IterateStruct *Ctx = CCTX;
2339 /*-----------------------------------------------------------------------------
2342 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2344 ConditionalStruct *Cond;
2346 if ((TP->Tokens->Params[0]->len == 1) &&
2347 (TP->Tokens->Params[0]->Start[0] == 'X'))
2348 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2350 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2353 Target, "Conditional", ERR_PARM1, TP,
2358 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2362 if (Cond->CondF(Target, TP) == Neg)
2363 return TP->Tokens->Params[1]->lvalue;
2367 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2372 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2374 subst = (wcsubst*) vsubst;
2376 switch(subst->wcs_type) {
2378 return (subst->wcs_function!=NULL);
2380 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
2384 case WCS_STRBUF_REF:
2385 if (TP->Tokens->nParameters < 4)
2387 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2389 if (TP->Tokens->nParameters < 4)
2390 return (subst->lvalue != 0);
2391 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2393 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2399 void RegisterConditional(const char *Name, long len,
2401 WCConditionalFunc CondF,
2402 int ContextRequired)
2404 ConditionalStruct *Cond;
2406 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2407 memset(Cond, 0, sizeof(ConditionalStruct));
2408 Cond->PlainName = Name;
2409 Cond->Filter.nMaxArgs = nParams;
2410 Cond->Filter.nMinArgs = nParams;
2411 Cond->CondF = CondF;
2412 Cond->Filter.ContextType = ContextRequired;
2413 Cond->Filter.ControlContextType = CTX_NONE;
2414 Put(Conditionals, Name, len, Cond, NULL);
2417 void RegisterControlConditional(const char *Name, long len,
2419 WCConditionalFunc CondF,
2420 int ControlContextRequired)
2422 ConditionalStruct *Cond;
2424 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2425 memset(Cond, 0, sizeof(ConditionalStruct));
2426 Cond->PlainName = Name;
2427 Cond->Filter.nMaxArgs = nParams;
2428 Cond->Filter.nMinArgs = nParams;
2429 Cond->CondF = CondF;
2430 Cond->Filter.ContextType = CTX_NONE;
2431 Cond->Filter.ControlContextType = ControlContextRequired;
2432 Put(Conditionals, Name, len, Cond, NULL);
2435 void RegisterTokenParamDefine(const char *Name, long len,
2440 PVal = (long*)malloc(sizeof(long));
2442 Put(Defines, Name, len, PVal, NULL);
2445 long GetTokenDefine(const char *Name, long len,
2450 if (GetHash(Defines, Name, len, &vPVal) &&
2453 return *(long*) vPVal;
2463 /*-----------------------------------------------------------------------------
2466 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2468 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2470 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2472 StrBuf *TokenText = (StrBuf*) CTX;
2473 const char *CompareToken;
2476 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2477 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2480 /*-----------------------------------------------------------------------------
2484 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2486 WCTemplputParams SubTP;
2488 StrBuf *Headline = NULL;
2489 if (TP->Tokens->nParameters == 2) {
2490 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2491 Headline = NewStrBuf();
2492 DoTemplate(TKEY(1), Headline, TP);
2497 GetTemplateTokenString(Target,
2502 Headline = NewStrBufPlain(Ch, len);
2505 /* else TODO error? logging? */
2506 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2507 SubTP.Context = Headline;
2508 SubTP.Filter.ContextType = CTX_STRBUF;
2509 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2510 DoTemplate(TKEY(0), Target, TP);
2511 DoTemplate(HKEY("endbox"), Target, TP);
2512 FreeStrBuf(&Headline);
2515 /*-----------------------------------------------------------------------------
2518 int preeval_do_tabbed(WCTemplateToken *Token)
2520 WCTemplputParams TPP;
2521 WCTemplputParams *TP;
2527 memset(&TPP, 0, sizeof(WCTemplputParams));
2530 nTabs = TP->Tokens->nParameters / 2 - 1;
2531 if (TP->Tokens->nParameters % 2 != 0)
2533 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2534 "need even number of arguments");
2538 else for (i = 0; i < nTabs; i++) {
2539 if (!HaveTemplateTokenString(NULL,
2544 (TP->Tokens->Params[i * 2]->len == 0))
2546 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2547 "Tab-Subject %d needs to be able to produce a string, have %s",
2548 i, TP->Tokens->Params[i * 2]->Start);
2551 if (!HaveTemplateTokenString(NULL,
2556 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2558 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2559 "Tab-Content %d needs to be able to produce a string, have %s",
2560 i, TP->Tokens->Params[i * 2 + 1]->Start);
2565 if (!HaveTemplateTokenString(NULL,
2570 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2572 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2573 "Tab-Content %d needs to be able to produce a string, have %s",
2574 i, TP->Tokens->Params[i * 2 + 1]->Start);
2581 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2584 int i, ntabs, nTabs;
2586 nTabs = ntabs = TP->Tokens->nParameters / 2;
2587 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2588 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2590 for (i = 0; i < ntabs; i++) {
2591 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2592 (TP->Tokens->Params[i * 2]->len > 0)) {
2593 TabNames[i] = NewStrBuf();
2594 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2596 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2599 GetTemplateTokenString(Target,
2604 TabNames[i] = NewStrBufPlain(Ch, -1);
2607 /** A Tab without subject? we can't count that, add it as silent */
2612 StrTabbedDialog(Target, nTabs, TabNames);
2613 for (i = 0; i < ntabs; i++) {
2614 StrBeginTab(Target, i, nTabs);
2615 DoTemplate(TKEY(i * 2 + 1), Target, TP);
2616 StrEndTab(Target, i, nTabs);
2618 for (i = 0; i < ntabs; i++)
2619 FreeStrBuf(&TabNames[i]);
2623 /*-----------------------------------------------------------------------------
2628 void RegisterSortFunc(const char *name, long len,
2629 const char *prepend, long preplen,
2630 CompareFunc Forward,
2631 CompareFunc Reverse,
2632 CompareFunc GroupChange,
2635 SortStruct *NewSort;
2637 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2638 memset(NewSort, 0, sizeof(SortStruct));
2639 NewSort->Name = NewStrBufPlain(name, len);
2640 if (prepend != NULL)
2641 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2643 NewSort->PrefPrepend = NULL;
2644 NewSort->Forward = Forward;
2645 NewSort->Reverse = Reverse;
2646 NewSort->GroupChange = GroupChange;
2647 NewSort->ContextType = ContextType;
2648 if (ContextType == CTX_NONE) {
2649 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2653 Put(SortHash, name, len, NewSort, DestroySortStruct);
2656 CompareFunc RetrieveSort(WCTemplputParams *TP,
2657 const char *OtherPrefix, long OtherPrefixLen,
2658 const char *Default, long ldefault, long DefaultDirection)
2661 const StrBuf *BSort = NULL;
2664 long SortOrder = -1;
2666 if (havebstr("SortBy")) {
2667 BSort = sbstr("SortBy");
2668 if (OtherPrefix == NULL) {
2669 set_room_pref("sort", NewStrBufDup(BSort), 0);
2672 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2675 else { /** Try to fallback to our remembered values... */
2676 if (OtherPrefix == NULL) {
2677 BSort = get_room_pref("sort");
2680 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2683 putbstr("SortBy", NewStrBufDup(BSort));
2687 BSort = Buf = NewStrBufPlain(Default, ldefault);
2688 putbstr("SortBy", Buf);
2692 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2693 (vSortBy == NULL)) {
2695 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2696 (vSortBy == NULL)) {
2698 NULL, "Sorting", ERR_PARM1, TP,
2699 "Illegal default sort: [%s]", Default);
2703 SortBy = (SortStruct*)vSortBy;
2705 if (SortBy->ContextType != TP->Filter.ContextType)
2708 /** Ok, its us, lets see in which direction we should sort... */
2709 if (havebstr("SortOrder")) {
2710 SortOrder = LBSTR("SortOrder");
2712 else { /** Try to fallback to our remembered values... */
2714 if (SortBy->PrefPrepend == NULL) {
2715 Buf = get_room_pref("SortOrder");
2716 SortOrder = StrTol(Buf);
2719 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2723 SortOrder = DefaultDirection;
2725 Buf = NewStrBufPlain(NULL, 64);
2726 StrBufPrintf(Buf, "%ld", SortOrder);
2727 putbstr("SortOrder", Buf);
2729 switch (SortOrder) {
2734 return SortBy->Forward;
2736 return SortBy->Reverse;
2748 ConstStr SortIcons[] = {
2749 {HKEY("static/sort_none.gif")},
2750 {HKEY("static/up_pointer.gif")},
2751 {HKEY("static/down_pointer.gif")},
2754 ConstStr SortNextOrder[] = {
2761 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2763 int bSortError = eNOT_SPECIFIED;
2764 const StrBuf *BSort;
2769 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2771 return eNO_SUCH_SORT;
2772 *Param = (SortStruct*) vSort;
2775 if (havebstr("SortBy")) {
2776 BSort = sbstr("SortBy");
2777 bSortError = eINVALID_PARAM;
2778 if ((*Param)->PrefPrepend == NULL) {
2779 set_room_pref("sort", NewStrBufDup(BSort), 0);
2782 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2785 else { /** Try to fallback to our remembered values... */
2786 if ((*Param)->PrefPrepend == NULL) {
2787 BSort = get_room_pref("sort");
2790 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2794 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2798 *Next = (SortStruct*) vSort;
2800 /** Ok, its us, lets see in which direction we should sort... */
2801 if (havebstr("SortOrder")) {
2802 *SortOrder = LBSTR("SortOrder");
2804 else { /** Try to fallback to our remembered values... */
2805 if ((*Param)->PrefPrepend == NULL) {
2806 *SortOrder = StrTol(get_room_pref("SortOrder"));
2809 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2819 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2824 const ConstStr *SortIcon;
2826 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2829 Target, "Sorter", ERR_PARM1, TP,
2830 " Sorter [%s] unknown!",
2831 TP->Tokens->Params[0]->Start);
2833 case eINVALID_PARAM:
2834 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2835 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2837 case eNOT_SPECIFIED:
2839 if (Next == Param) {
2840 SortIcon = &SortIcons[SortOrder];
2842 else { /** Not Us... */
2843 SortIcon = &SortIcons[0];
2845 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
2849 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
2855 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2858 Target, "Sorter", ERR_PARM1, TP,
2859 " Sorter [%s] unknown!",
2860 TP->Tokens->Params[0]->Start);
2862 case eINVALID_PARAM:
2864 NULL, "Sorter", ERR_PARM1, TP,
2865 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2867 case eNOT_SPECIFIED:
2869 StrBufAppendBuf(Target, Param->Name, 0);
2874 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
2877 const ConstStr *SortOrderStr;
2881 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2884 Target, "Sorter", ERR_PARM1, TP,
2885 " Sorter [%s] unknown!",
2886 TP->Tokens->Params[0]->Start);
2888 case eINVALID_PARAM:
2890 NULL, "Sorter", ERR_PARM1, TP,
2891 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2893 case eNOT_SPECIFIED:
2895 if (Next == Param) {
2896 SortOrderStr = &SortNextOrder[SortOrder];
2898 else { /** Not Us... */
2899 SortOrderStr = &SortNextOrder[0];
2901 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2906 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
2908 long *LongVector = (long*) CTX;
2910 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
2911 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
2913 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
2917 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
2919 Target, "Longvector", ERR_NAME, TP,
2920 "needs a numerical Parameter!");
2924 Target, "LongVector", ERR_PARM1, TP,
2925 "doesn't have %ld Parameters, its just the size of %ld!",
2926 TP->Tokens->Params[0]->lvalue,
2932 void dbg_print_longvector(long *LongVector)
2934 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
2935 int nItems = LongVector[0];
2938 for (i = 0; i < nItems; i++) {
2940 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
2942 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
2945 lprintf(1, ChrPtr(Buf));
2949 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
2951 long *LongVector = (long*) CTX;
2953 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
2954 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
2955 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
2956 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
2958 return LongVector[TP->Tokens->Params[2]->lvalue] ==
2959 LongVector[TP->Tokens->Params[3]->lvalue];
2963 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
2964 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
2966 Target, "ConditionalLongvector", ERR_PARM1, TP,
2967 "needs two long Parameter!");
2971 Target, "Longvector", ERR_PARM1, TP,
2972 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
2973 TP->Tokens->Params[2]->lvalue,
2974 TP->Tokens->Params[3]->lvalue,
2982 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
2984 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
2991 memset(&NoCtx, 0, sizeof(WCTemplputParams));
2992 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
2993 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
2994 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
2995 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
2996 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
2997 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
2998 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, preeval_do_tabbed, CTX_NONE);
2999 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
3000 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
3001 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
3002 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
3004 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
3005 conditional_ITERATE_ISGROUPCHANGE,
3007 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
3008 conditional_ITERATE_LASTN,
3010 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
3011 conditional_ITERATE_FIRSTN,
3014 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
3015 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
3016 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
3017 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
3023 ServerStartModule_SUBST
3026 WirelessTemplateCache = NewHash(1, NULL);
3027 WirelessLocalTemplateCache = NewHash(1, NULL);
3028 LocalTemplateCache = NewHash(1, NULL);
3029 TemplateCache = NewHash(1, NULL);
3031 GlobalNS = NewHash(1, NULL);
3032 Iterators = NewHash(1, NULL);
3033 Conditionals = NewHash(1, NULL);
3034 SortHash = NewHash(1, NULL);
3035 Defines = NewHash(1, NULL);
3039 FinalizeModule_SUBST
3046 ServerShutdownModule_SUBST
3049 DeleteHash(&WirelessTemplateCache);
3050 DeleteHash(&WirelessLocalTemplateCache);
3051 DeleteHash(&TemplateCache);
3052 DeleteHash(&LocalTemplateCache);
3054 DeleteHash(&GlobalNS);
3055 DeleteHash(&Iterators);
3056 DeleteHash(&Conditionals);
3057 DeleteHash(&SortHash);
3058 DeleteHash(&Defines);
3063 SessionNewModule_SUBST
3070 SessionAttachModule_SUBST
3073 sess->vars = NewHash(1,NULL);
3077 SessionDetachModule_SUBST
3080 DeleteHash(&sess->vars);
3084 SessionDestroyModule_SUBST