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 void GetTemplateTokenString(StrBuf *Target,
770 WCTemplputParams *TP,
776 WCTemplputParams SubTP;
778 if (N >= TP->Tokens->nParameters) {
779 LogTemplateError(Target,
780 "TokenParameter", N, TP,
781 "invalid token %d. this shouldn't have come till here.\n", N);
787 switch (TP->Tokens->Params[N]->Type) {
791 *Value = TP->Tokens->Params[N]->Start;
792 *len = TP->Tokens->Params[N]->len;
795 if (TP->Tokens->Params[N]->len == 0) {
796 LogTemplateError(Target,
797 "TokenParameter", N, TP,
798 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
803 Buf = (StrBuf*) SBstr(TKEY(N));
804 *Value = ChrPtr(Buf);
805 *len = StrLength(Buf);
808 if (TP->Tokens->Params[N]->len == 0) {
809 LogTemplateError(Target,
810 "TokenParameter", N, TP,
811 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
816 get_PREFERENCE(TKEY(N), &Buf);
817 *Value = ChrPtr(Buf);
818 *len = StrLength(Buf);
821 LogTemplateError(Target,
822 "TokenParameter", N, TP,
823 "Requesting parameter %d; of type LONG, want string.", N);
826 LogTemplateError(Target,
827 "TokenParameter", N, TP,
828 "Requesting parameter %d; of type PREFINT, want string.", N);
831 *Value = _(TP->Tokens->Params[N]->Start);
832 *len = strlen(*Value);
834 case TYPE_SUBTEMPLATE:
835 if (TP->Tokens->Params[N]->len == 0) {
836 LogTemplateError(Target,
837 "TokenParameter", N, TP,
838 "Requesting parameter %d; of type SUBTEMPLATE, empty lookup string not admitted.", N);
844 memset(&SubTP, 0, sizeof(WCTemplputParams *));
845 SubTP.Context = TP->Context;
846 SubTP.Filter.ContextType = TP->Filter.ContextType;
848 DoTemplate(TKEY(N), Buf, &SubTP);
849 *Value = ChrPtr(Buf);
850 *len = StrLength(Buf);
851 /* we can't free it here, so we put it into the subst so its discarded later on. */
852 SVPUTBuf(TKEY(N), Buf, 0);
856 LogTemplateError(Target,
857 "TokenParameter", N, TP,
858 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
863 long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
866 if (N >= TP->Tokens->nParameters) {
867 LogTemplateError(Target,
868 "TokenParameter", N, TP,
869 "invalid token %d. this shouldn't have come till here.\n", N);
874 switch (TP->Tokens->Params[N]->Type) {
877 return atol(TP->Tokens->Params[N]->Start);
880 if (TP->Tokens->Params[N]->len == 0) {
881 LogTemplateError(Target,
882 "TokenParameter", N, TP,
883 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
886 return LBstr(TKEY(N));
889 LogTemplateError(Target,
890 "TokenParameter", N, TP,
891 "requesting a prefstring in param %d want a number", N);
892 if (TP->Tokens->Params[N]->len == 0) {
893 LogTemplateError(Target,
894 "TokenParameter", N, TP,
895 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
898 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
903 return TP->Tokens->Params[N]->lvalue;
905 if (TP->Tokens->Params[N]->len == 0) {
906 LogTemplateError(Target,
907 "TokenParameter", N, TP,
908 "Requesting parameter %d; of type PREFINT, empty lookup string not admitted.", N);
911 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
915 LogTemplateError(Target,
916 "TokenParameter", N, TP,
917 "requesting a I18N string in param %d; want a number", N);
919 case TYPE_SUBTEMPLATE:
920 LogTemplateError(Target,
921 "TokenParameter", N, TP,
922 "requesting a subtemplate in param %d; not supported for numbers", N);
925 LogTemplateError(Target,
926 "TokenParameter", N, TP,
927 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
935 * \brief Print the value of a variable
936 * \param keyname get a key to print
938 void print_value_of(StrBuf *Target, WCTemplputParams *TP)
945 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
946 /* TODO: depricated! */
947 if (TP->Tokens->pName[0] == '=') {
948 DoTemplate(TP->Tokens->pName+1, TP->Tokens->NameEnd - 1, NULL, &NoCtx);
950 /*/////TODO: if param[1] == "U" -> urlescape
952 /** Page-local variables */
953 if ((WCC->vars!= NULL) && GetHash(WCC->vars, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
954 ptr = (wcsubst*) vVar;
955 switch(ptr->wcs_type) {
957 StrBufAppendBuf(Target, ptr->wcs_value, 0);
960 pvo_do_cmd(Target, ptr->wcs_value);
963 (*ptr->wcs_function) (Target, TP);
967 StrBufAppendBuf(Target, ptr->wcs_value, 0);
970 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
974 Target, "Subst", ERR_NAME, TP,
975 "WARNING: invalid value in SV-Hash at %s!", TP->Tokens->pName);
980 Target, "Token", ERR_NAME, TP,
981 "didn't find Handler \"%s\"", TP->Tokens->pName);
986 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
992 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
993 ParamToLookup->len, &vVar)) {
994 ptr = (wcsubst*) vVar;
995 switch(ptr->wcs_type) {
999 if (ParamToCompare->Type == TYPE_STR)
1000 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
1001 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
1003 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
1011 if (ParamToCompare->Type == TYPE_STR)
1014 return ParamToCompare->lvalue == ptr->lvalue;
1017 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1018 ParamToLookup->Start);
1024 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
1026 wcsession *WCC = WC;
1030 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1031 ParamToLookup->len, &vVar)) {
1032 ptr = (wcsubst*) vVar;
1033 switch(ptr->wcs_type) {
1036 case WCS_STRBUF_REF:
1037 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
1038 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
1045 return StrTol(Compare) == ptr->lvalue;
1047 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1048 ParamToLookup->Start);
1057 * \brief puts string into the template and computes which escape methon we should use
1058 * \param Source the string we should put into the template
1059 * \param FormatTypeIndex where should we look for escape types if?
1061 void StrBufAppendTemplate(StrBuf *Target,
1062 WCTemplputParams *TP,
1063 const StrBuf *Source, int FormatTypeIndex)
1066 char EscapeAs = ' ';
1068 if ((FormatTypeIndex < TP->Tokens->nParameters) &&
1069 (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
1070 (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
1071 EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
1078 StrEscAppend(Target, Source, NULL, 0, 2);
1081 StrEscAppend(Target, Source, NULL, 0, 0);
1084 StrECMAEscAppend(Target, Source, NULL);
1087 StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 0);
1090 StrBufUrlescAppend(Target, Source, NULL);
1093 StrBufAppendBuf(Target, Source, 0);
1098 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
1100 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
1101 if (Template->TokenSpace <= 0) {
1102 Template->Tokens = (WCTemplateToken**)malloc(
1103 sizeof(WCTemplateToken*) * 10);
1104 memset(Template->Tokens, 0, sizeof(WCTemplateToken*) * 10);
1105 Template->TokenSpace = 10;
1108 WCTemplateToken **NewTokens;
1110 NewTokens= (WCTemplateToken**) malloc(
1111 sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1114 0, sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1118 sizeof(WCTemplateToken*) * Template->nTokensUsed);
1120 free(Template->Tokens);
1121 Template->TokenSpace *= 2;
1122 Template->Tokens = NewTokens;
1125 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
1128 TemplateParam *GetNextParameter(StrBuf *Buf,
1131 WCTemplateToken *Tokens,
1133 WCTemplputParams *TP)
1135 const char *pch = *pCh;
1136 const char *pchs, *pche;
1137 TemplateParam *Parm;
1141 Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
1142 memset(Parm, 0, sizeof(TemplateParam));
1143 Parm->Type = TYPE_STR;
1145 /* Skip leading whitespaces */
1146 while ((*pch == ' ' )||
1149 (*pch == '\n')) pch ++;
1152 Parm->Type = TYPE_PREFSTR;
1159 else if (*pch == ';') {
1160 Parm->Type = TYPE_PREFINT;
1167 else if (*pch == '#') {
1168 Parm->Type = TYPE_INTDEFINE;
1171 else if (*pch == '_') {
1172 Parm->Type = TYPE_GETTEXT;
1179 else if (*pch == 'B') {
1180 Parm->Type = TYPE_BSTR;
1187 else if (*pch == '=') {
1188 Parm->Type = TYPE_SUBTEMPLATE;
1199 else if (*pch == '\'')
1201 if (quote != '\0') {
1206 ( (pch > pchs) && (*(pch - 1) == '\\'))
1211 if (*pch != quote) {
1212 lprintf(1, "Error (in '%s' line %ld); "
1213 "evaluating template param [%s] in Token [%s]\n",
1214 ChrPtr(pTmpl->FileName),
1216 ChrPtr(Tokens->FlatToken),
1223 StrBufPeek(Buf, pch, -1, '\0');
1224 if (LoadTemplates > 1) {
1225 lprintf(1, "DBG: got param [%s] %ld %ld\n",
1226 pchs, pche - pchs, strlen(pchs));
1229 Parm->len = pche - pchs;
1230 pch ++; /* move after trailing quote */
1231 if (ParamBrace && (*pch == ')')) {
1238 Parm->Type = TYPE_LONG;
1240 while ((pch <= pe) &&
1246 if (pch - pchs > 1){
1247 StrBufPeek(Buf, pch, -1, '\0');
1248 Parm->lvalue = atol(pchs);
1255 lprintf(1, "Error (in '%s' line %ld); "
1256 "evaluating long template param [%s] in Token [%s]\n",
1257 ChrPtr(pTmpl->FileName),
1259 ChrPtr(Tokens->FlatToken),
1266 while ((*pch == ' ' )||
1270 (*pch == '\n')) pch ++;
1275 if (DumpTemplateI18NStrings) {
1276 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
1279 case TYPE_INTDEFINE: {
1282 if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
1286 PVal = (long*) vPVal;
1288 Parm->lvalue = *PVal;
1292 LogTemplateError(NULL, "Define", ERR_PARM1, TP,
1297 case TYPE_SUBTEMPLATE:{
1299 /* well, we don't check the mobile stuff here... */
1300 if (!GetHash(LocalTemplateCache, Parm->Start, Parm->len, &vTmpl) &&
1301 !GetHash(TemplateCache, Parm->Start, Parm->len, &vTmpl)) {
1303 NULL, "SubTemplate", ERR_PARM1, TP,
1304 "referenced here doesn't exist");
1312 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
1314 const char *pTokenStart,
1315 const char *pTokenEnd,
1321 TemplateParam *Param;
1322 WCTemplateToken *NewToken;
1323 WCTemplputParams TP;
1325 NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
1326 memset(NewToken, 0, sizeof(WCTemplateToken));
1327 TP.Tokens = NewToken;
1328 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
1329 NewToken->Flags = 0;
1330 NewToken->Line = Line + 1;
1331 NewToken->pTokenStart = pTokenStart;
1332 NewToken->TokenStart = pTokenStart - pStart;
1333 NewToken->TokenEnd = (pTokenEnd - pStart) - NewToken->TokenStart;
1334 NewToken->pTokenEnd = pTokenEnd;
1335 NewToken->NameEnd = NewToken->TokenEnd - 2;
1336 NewToken->PreEval = NULL;
1337 NewToken->FlatToken = NewStrBufPlain(pTokenStart + 2, pTokenEnd - pTokenStart - 2);
1338 StrBufShrinkToFit(NewToken->FlatToken, 1);
1340 StrBufPeek(Buf, pTokenStart, + 1, '\0');
1341 StrBufPeek(Buf, pTokenEnd, -1, '\0');
1342 pch = NewToken->pName = pTokenStart + 2;
1344 NewToken->HaveParameters = 0;;
1345 NewToken->nParameters = 0;
1347 while (pch < pTokenEnd - 1) {
1349 StrBufPeek(Buf, pch, -1, '\0');
1350 NewToken->NameEnd = pch - NewToken->pName;
1352 if (*(pTokenEnd - 1) != ')') {
1354 NULL, "Parseerror", ERR_NAME, &TP,
1355 "Warning, Non welformed Token; missing right parenthesis");
1357 while (pch < pTokenEnd - 1) {
1358 Param = GetNextParameter(Buf, &pch, pTokenEnd - 1, NewToken, pTmpl, &TP);
1359 if (Param != NULL) {
1360 NewToken->HaveParameters = 1;
1361 if (NewToken->nParameters > MAXPARAM) {
1363 NULL, "Parseerror", ERR_NAME, &TP,
1364 "only [%d] Params allowed in Tokens",
1368 FreeToken(&NewToken);
1371 NewToken->Params[NewToken->nParameters++] = Param;
1375 if((NewToken->NameEnd == 1) &&
1376 (NewToken->HaveParameters == 1))
1379 if (*(NewToken->pName) == '_')
1380 NewToken->Flags = SV_GETTEXT;
1381 else if (*(NewToken->pName) == '=')
1382 NewToken->Flags = SV_SUBTEMPL;
1383 else if (*(NewToken->pName) == '%')
1384 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
1385 else if (*(NewToken->pName) == '?')
1386 NewToken->Flags = SV_CONDITIONAL;
1387 else if (*(NewToken->pName) == '!')
1388 NewToken->Flags = SV_NEG_CONDITIONAL;
1394 switch (NewToken->Flags) {
1396 /* If we're able to find out more about the token, do it now while its fresh. */
1397 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1398 HashHandler *Handler;
1399 Handler = (HashHandler*) vVar;
1400 if ((NewToken->nParameters < Handler->Filter.nMinArgs) ||
1401 (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
1403 NULL, "Token", ERR_NAME, &TP,
1404 "doesn't work with %d params",
1405 NewToken->nParameters);
1409 NewToken->PreEval = Handler;
1410 NewToken->Flags = SV_PREEVALUATED;
1411 if (Handler->PreEvalFunc != NULL)
1412 Handler->PreEvalFunc(NewToken);
1417 if (NewToken->nParameters !=1) {
1419 NULL, "Gettext", ERR_NAME, &TP,
1420 "requires exactly 1 parameter, you gave %d params",
1421 NewToken->nParameters);
1422 NewToken->Flags = 0;
1425 if (DumpTemplateI18NStrings) {
1426 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
1430 if (NewToken->nParameters != 1) {
1432 NULL, "Subtemplates", ERR_NAME, &TP,
1433 "require exactly 1 parameter, you gave %d params",
1434 NewToken->nParameters);
1439 /* well, we don't check the mobile stuff here... */
1440 if (!GetHash(LocalTemplateCache,
1441 NewToken->Params[0]->Start,
1442 NewToken->Params[0]->len,
1444 !GetHash(TemplateCache,
1445 NewToken->Params[0]->Start,
1446 NewToken->Params[0]->len,
1449 NULL, "SubTemplate", ERR_PARM1, &TP,
1454 case SV_CUST_STR_CONDITIONAL:
1455 case SV_CONDITIONAL:
1456 case SV_NEG_CONDITIONAL:
1457 if (NewToken->nParameters <2) {
1459 NULL, "Conditional", ERR_PARM1, &TP,
1460 "require at least 2 parameters, you gave %d params",
1461 NewToken->nParameters);
1462 NewToken->Flags = 0;
1465 if (NewToken->Params[1]->lvalue == 0) {
1467 NULL, "Conditional", ERR_PARM1, &TP,
1468 "Conditional ID (Parameter 1) mustn't be 0!");
1469 NewToken->Flags = 0;
1472 if (!GetHash(Conditionals,
1473 NewToken->Params[0]->Start,
1474 NewToken->Params[0]->len,
1477 if ((NewToken->Params[0]->len == 1) &&
1478 (NewToken->Params[0]->Start[0] == 'X'))
1481 NULL, "Conditional", ERR_PARM1, &TP,
1484 NewToken->Error = NewStrBuf();
1487 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1488 NewToken->Params[0]->Start,
1489 ChrPtr(pTmpl->FileName),
1491 ChrPtr(NewToken->FlatToken));
1495 NewToken->PreEval = vVar;
1507 * \brief Display a variable-substituted template
1508 * \param templatename template file to load
1510 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1512 WCTemplate *NewTemplate;
1514 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1515 memset(NewTemplate, 0, sizeof(WCTemplate));
1516 NewTemplate->Data = NULL;
1517 NewTemplate->FileName = NewStrBufDup(filename);
1518 StrBufShrinkToFit(NewTemplate->FileName, 1);
1519 NewTemplate->nTokensUsed = 0;
1520 NewTemplate->TokenSpace = 0;
1521 NewTemplate->Tokens = NULL;
1522 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1523 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1524 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1527 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1528 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1531 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1536 * \brief Display a variable-substituted template
1537 * \param templatename template file to load
1539 void *duplicate_template(WCTemplate *OldTemplate)
1541 WCTemplate *NewTemplate;
1543 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1544 memset(NewTemplate, 0, sizeof(WCTemplate));
1545 NewTemplate->Data = NULL;
1546 NewTemplate->FileName = NewStrBufDup(OldTemplate->FileName);
1547 StrBufShrinkToFit(NewTemplate->FileName, 1);
1548 NewTemplate->nTokensUsed = 0;
1549 NewTemplate->TokenSpace = 0;
1550 NewTemplate->Tokens = NULL;
1551 NewTemplate->MimeType = NewStrBufDup(OldTemplate->MimeType);
1556 * \brief Display a variable-substituted template
1557 * \param templatename template file to load
1559 void *load_template(WCTemplate *NewTemplate)
1562 struct stat statbuf;
1563 const char *pS, *pE, *pch, *Err;
1567 fd = open(ChrPtr(NewTemplate->FileName), O_RDONLY);
1569 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1570 ChrPtr(NewTemplate->FileName), strerror(errno));
1574 if (fstat(fd, &statbuf) == -1) {
1575 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1576 ChrPtr(NewTemplate->FileName), strerror(errno));
1580 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size + 1);
1581 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1583 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1584 ChrPtr(NewTemplate->FileName), strerror(errno));
1585 //FreeWCTemplate(NewTemplate);/////tODO
1591 StrBufShrinkToFit(NewTemplate->Data, 1);
1592 StrBufShrinkToFit(NewTemplate->MimeType, 1);
1593 pS = pch = ChrPtr(NewTemplate->Data);
1594 pE = pS + StrLength(NewTemplate->Data);
1596 const char *pts, *pte;
1598 int InDoubleQuotes = 0;
1600 /** Find one <? > */
1602 for (; pch < pE; pch ++) {
1603 if ((*pch=='<')&&(*(pch + 1)=='?') &&
1604 !((pch == pS) && /* we must ommit a <?xml */
1605 (*(pch + 2) == 'x') &&
1606 (*(pch + 3) == 'm') &&
1607 (*(pch + 4) == 'l')))
1609 if (*pch=='\n') Line ++;
1615 /** Found one? parse it. */
1616 for (; pch <= pE - 1; pch ++) {
1618 InDoubleQuotes = ! InDoubleQuotes;
1619 else if (*pch == '\'')
1620 InQuotes = ! InQuotes;
1621 else if ((!InQuotes && !InDoubleQuotes) &&
1622 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1630 PutNewToken(NewTemplate,
1631 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1638 const char* PrintTemplate(void *vSubst)
1640 WCTemplate *Tmpl = vSubst;
1642 return ChrPtr(Tmpl->FileName);
1646 int LoadTemplateDir(const StrBuf *DirName, HashList *wireless, HashList *big, const StrBuf *BaseKey)
1652 StrBuf *SubDirectory;
1653 DIR *filedir = NULL;
1654 struct dirent *filedir_entry;
1661 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
1666 filedir = opendir (ChrPtr(DirName));
1667 if (filedir == NULL) {
1672 Toplevel = StrLength(BaseKey) == 0;
1673 SubDirectory = NewStrBuf();
1674 SubKey = NewStrBuf();
1675 FileName = NewStrBufPlain(NULL, PATH_MAX);
1677 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
1678 (filedir_entry != NULL))
1682 #ifdef _DIRENT_HAVE_D_NAMELEN
1683 d_namelen = filedir_entry->d_namelen;
1684 d_type = filedir_entry->d_type;
1688 #define DT_UNKNOWN 0
1693 #define IFTODT(mode) (((mode) & 0170000) >> 12)
1694 #define DTTOIF(dirtype) ((dirtype) << 12)
1696 d_namelen = strlen(filedir_entry->d_name);
1697 d_type = DT_UNKNOWN;
1699 d_without_ext = d_namelen;
1701 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1702 continue; /* Ignore backup files... */
1704 if ((d_namelen == 1) &&
1705 (filedir_entry->d_name[0] == '.'))
1708 if ((d_namelen == 2) &&
1709 (filedir_entry->d_name[0] == '.') &&
1710 (filedir_entry->d_name[1] == '.'))
1713 if (d_type == DT_UNKNOWN) {
1715 char path[PATH_MAX];
1716 snprintf(path, PATH_MAX, "%s/%s",
1717 ChrPtr(DirName), filedir_entry->d_name);
1718 if (stat(path, &s) == 0) {
1719 d_type = IFTODT(s.st_mode);
1725 /* Skip directories we are not interested in... */
1726 if (strcmp(filedir_entry->d_name, ".svn") == 0)
1729 FlushStrBuf(SubKey);
1731 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1732 StrBufAppendBuf(SubKey, BaseKey, 0);
1733 StrBufAppendBufPlain(SubKey, HKEY("_"), 0);
1735 StrBufAppendBufPlain(SubKey, filedir_entry->d_name, d_namelen, 0);
1737 FlushStrBuf(SubDirectory);
1738 StrBufAppendBuf(SubDirectory, DirName, 0);
1739 if (ChrPtr(SubDirectory)[StrLength(SubDirectory) - 1] != '/')
1740 StrBufAppendBufPlain(SubDirectory, HKEY("/"), 0);
1741 StrBufAppendBufPlain(SubDirectory, filedir_entry->d_name, d_namelen, 0);
1743 LoadTemplateDir(SubDirectory, wireless, big, SubKey);
1746 case DT_LNK: /* TODO: check whether its a file or a directory */
1750 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1752 if ((d_without_ext == 0) || (d_namelen < 3))
1754 if (((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~') ||
1755 (strcmp(&filedir_entry->d_name[d_without_ext], ".orig") == 0) ||
1756 (strcmp(&filedir_entry->d_name[d_without_ext], ".swp") == 0))
1757 continue; /* Ignore backup files... */
1758 /* .m.xxx is for mobile useragents! */
1760 if (d_without_ext > 2)
1761 IsMobile = (filedir_entry->d_name[d_without_ext - 1] == 'm') &&
1762 (filedir_entry->d_name[d_without_ext - 2] == '.');
1763 PStart = filedir_entry->d_name;
1764 StrBufPrintf(FileName, "%s/%s", ChrPtr(DirName), filedir_entry->d_name);
1765 MinorPtr = strchr(filedir_entry->d_name, '.');
1766 if (MinorPtr != NULL)
1770 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1771 StrBufAppendBuf(Key, BaseKey, 0);
1772 StrBufAppendBufPlain(Key, HKEY("_"), 0);
1774 StrBufAppendBufPlain(Key, filedir_entry->d_name, MinorPtr - filedir_entry->d_name, 0);
1776 if (LoadTemplates >= 1)
1777 lprintf(1, "%s %d %s\n", ChrPtr(FileName), IsMobile, ChrPtr(Key));
1778 prepare_template(FileName, Key, (IsMobile)?wireless:big);
1785 FreeStrBuf(&FileName);
1787 FreeStrBuf(&SubDirectory);
1788 FreeStrBuf(&SubKey);
1792 void InitTemplateCache(void)
1797 HashList *Templates[4];
1802 /* Primary Template set... */
1803 StrBufPrintf(Dir, "%s/t", static_dirs[0]);
1804 LoadTemplateDir(Dir,
1805 WirelessTemplateCache,
1809 /* User local Template set */
1810 StrBufPrintf(Dir, "%s/t", static_dirs[1]);
1811 LoadTemplateDir(Dir,
1812 WirelessLocalTemplateCache,
1816 /* Debug Templates, just to be loaded while debugging. */
1818 StrBufPrintf(Dir, "%s/dbg", static_dirs[0]);
1819 LoadTemplateDir(Dir,
1820 WirelessTemplateCache,
1823 Templates[0] = WirelessTemplateCache;
1824 Templates[1] = TemplateCache;
1825 Templates[2] = WirelessLocalTemplateCache;
1826 Templates[3] = LocalTemplateCache;
1829 if (LoadTemplates == 0)
1830 for (i=0; i < 4; i++) {
1836 At = GetNewHashPos(Templates[i], 0);
1837 while (GetNextHashPos(Templates[i],
1842 (vTemplate != NULL))
1844 load_template((WCTemplate *)vTemplate);
1856 /*-----------------------------------------------------------------------------
1857 * Filling & processing Templates
1860 * \brief executes one token
1861 * \param Target buffer to append to
1862 * \param Token da to process.
1863 * \param Template we're iterating
1864 * \param Context Contextpoointer to pass in
1865 * \param state are we in conditional state?
1866 * \param ContextType what type of information does context giv us?
1868 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1870 const char *AppendMe;
1872 HashHandler *Handler;
1875 /* much output, since pName is not terminated...
1876 lprintf(1,"Doing token: %s\n",Token->pName);
1879 switch (TP->Tokens->Flags) {
1881 TmplGettext(Target, TP);
1883 case SV_CONDITIONAL: /** Forward conditional evaluation */
1884 return EvaluateConditional(Target, 1, state, TP);
1886 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1887 return EvaluateConditional(Target, 0, state, TP);
1889 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1890 if (TP->Tokens->nParameters >= 6) {
1891 if (EvaluateConditional(Target, 0, state, TP)) {
1892 GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
1893 StrBufAppendBufPlain(Target,
1899 GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
1900 StrBufAppendBufPlain(Target,
1908 Target, "Conditional", ERR_NAME, TP,
1909 "needs at least 6 Params!");
1913 if (TP->Tokens->nParameters == 1)
1914 DoTemplate(TKEY(0), Target, TP);
1916 case SV_PREEVALUATED:
1917 Handler = (HashHandler*) TP->Tokens->PreEval;
1918 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1921 Handler->HandlerFunc(Target, TP);
1924 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
1925 Handler = (HashHandler*) vVar;
1926 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1930 Handler->HandlerFunc(Target, TP);
1934 print_value_of(Target, TP);
1942 const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
1944 WCTemplate *pTmpl = Tmpl;
1947 const char *pData, *pS;
1949 WCTemplputParams TP;
1951 memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
1953 TP.Context = CallingTP->Context;
1954 TP.ControlContext = CallingTP->ControlContext;
1956 if (LoadTemplates != 0) {
1957 if (LoadTemplates > 1)
1958 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1959 ChrPtr(Tmpl->FileName));
1960 pTmpl = duplicate_template(Tmpl);
1961 if(load_template(pTmpl) == NULL) {
1964 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1965 ChrPtr(Tmpl->FileName));
1966 FreeWCTemplate(pTmpl);
1972 pS = pData = ChrPtr(pTmpl->Data);
1973 len = StrLength(pTmpl->Data);
1977 if (i >= pTmpl->nTokensUsed) {
1978 StrBufAppendBufPlain(Target,
1980 len - (pData - pS), 0);
1984 StrBufAppendBufPlain(
1986 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1987 TP.Tokens = pTmpl->Tokens[i];
1988 TP.nArgs = pTmpl->Tokens[i]->nParameters;
1989 state = EvaluateToken(Target, state, &TP);
1991 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1992 /* condition told us to skip till its end condition */
1994 TP.Tokens = pTmpl->Tokens[i];
1995 TP.nArgs = pTmpl->Tokens[i]->nParameters;
1996 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1997 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1998 if (state == EvaluateConditional(
2000 pTmpl->Tokens[i]->Flags,
2006 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
2007 if (i > pTmpl->nTokensUsed)
2011 if (LoadTemplates != 0) {
2012 FreeWCTemplate(pTmpl);
2014 return Tmpl->MimeType;
2019 * \brief Display a variable-substituted template
2020 * \param templatename template file to load
2021 * \returns the mimetype of the template its doing
2023 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
2025 WCTemplputParams LocalTP;
2027 HashList *StaticLocal;
2033 memset(&LocalTP, 0, sizeof(WCTemplputParams));
2037 if (WC->is_mobile > 0) {
2038 Static = WirelessTemplateCache;
2039 StaticLocal = WirelessLocalTemplateCache;
2042 Static = TemplateCache;
2043 StaticLocal = LocalTemplateCache;
2048 lprintf (1, "Can't to load a template with empty name!\n");
2049 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
2053 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
2054 !GetHash(Static, templatename, len, &vTmpl)) {
2055 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
2056 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
2058 (long)strlen(templatename));
2060 dbg_PrintHash(Static, PrintTemplate, NULL);
2061 PrintHash(Static, VarPrintTransition, PrintTemplate);
2067 return ProcessTemplate(vTmpl, Target, TP);
2071 /*-----------------------------------------------------------------------------
2074 typedef struct _HashIterator {
2075 HashList *StaticList;
2076 int AdditionalParams;
2078 int XPectContextType;
2080 RetrieveHashlistFunc GetHash;
2081 HashDestructorFunc Destructor;
2082 SubTemplFunc DoSubTemplate;
2085 void RegisterITERATOR(const char *Name, long len,
2086 int AdditionalParams,
2087 HashList *StaticList,
2088 RetrieveHashlistFunc GetHash,
2089 SubTemplFunc DoSubTempl,
2090 HashDestructorFunc Destructor,
2092 int XPectContextType,
2097 It = (HashIterator*)malloc(sizeof(HashIterator));
2098 memset(It, 0, sizeof(HashIterator));
2099 It->StaticList = StaticList;
2100 It->AdditionalParams = AdditionalParams;
2101 It->GetHash = GetHash;
2102 It->DoSubTemplate = DoSubTempl;
2103 It->Destructor = Destructor;
2104 It->ContextType = ContextType;
2105 It->XPectContextType = XPectContextType;
2107 Put(Iterators, Name, len, It, NULL);
2110 typedef struct _iteratestruct {
2119 int preeval_iterate(WCTemplateToken *Token)
2121 WCTemplputParams TPP;
2122 WCTemplputParams *TP;
2126 memset(&TPP, 0, sizeof(WCTemplputParams));
2129 if (!GetHash(Iterators, TKEY(0), &vIt)) {
2131 NULL, "Iterator", ERR_PARM1, TP,
2135 if (TP->Tokens->Params[1]->Type != TYPE_SUBTEMPLATE) {
2136 LogTemplateError(NULL, "Iterator", ERR_PARM1, TP,
2137 "Need token with type Subtemplate as param 1, have %s",
2138 TP->Tokens->Params[1]->Start);
2141 /* well, we don't check the mobile stuff here... */
2142 if (!GetHash(LocalTemplateCache, TKEY(1), &vTmpl) &&
2143 !GetHash(TemplateCache, TKEY(1), &vTmpl)) {
2144 LogTemplateError(NULL, "SubTemplate", ERR_PARM1, TP,
2145 "referenced here doesn't exist");
2147 Token->Preeval2 = vIt;
2151 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
2156 SortStruct *SortBy = NULL;
2158 int DetectGroupChange = 0;
2161 void *vLastContext = NULL;
2163 WCTemplputParams SubTP;
2164 IterateStruct Status;
2170 memset(&Status, 0, sizeof(IterateStruct));
2171 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2173 It = (HashIterator*) TP->Tokens->Preeval2;
2176 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
2180 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2182 Target, "Iterator", ERR_PARM1, TP,
2183 "doesn't work with %d params",
2184 TP->Tokens->nParameters);
2188 if ((It->XPectContextType != CTX_NONE) &&
2189 (It->XPectContextType != TP->Filter.ContextType)) {
2191 Target, "Iterator", ERR_PARM1, TP,
2192 "requires context of type %d, have %d",
2193 It->XPectContextType,
2194 TP->Filter.ContextType);
2199 if (It->StaticList == NULL)
2200 List = It->GetHash(Target, TP);
2202 List = It->StaticList;
2204 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2205 if (DetectGroupChange) {
2206 const StrBuf *BSort;
2207 DetectGroupChange = 0;
2208 if (havebstr("SortBy")) {
2209 BSort = sbstr("SortBy");
2210 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2211 (vSortBy != NULL)) {
2212 SortBy = (SortStruct*)vSortBy;
2213 /** Ok, its us, lets see in which direction we should sort... */
2214 if (havebstr("SortOrder")) {
2216 SortOrder = LBSTR("SortOrder");
2218 DetectGroupChange = 1;
2223 nMembersUsed = GetCount(List);
2224 SubBuf = NewStrBuf();
2225 SubTP.Filter.ContextType = It->ContextType;
2226 SubTP.Filter.ControlContextType = CTX_ITERATE;
2227 SubTP.ControlContext = &Status;
2229 if (HAVE_PARAM(2)) {
2230 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2232 if (HAVE_PARAM(3)) {
2233 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2235 if (HAVE_PARAM(4)) {
2236 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2238 it = GetNewHashPos(List, StepWidth);
2240 StopAt = GetCount(List);
2242 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2243 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2244 if (DetectGroupChange && Status.n > 0) {
2245 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2247 Status.LastN = (Status.n + 1) == nMembersUsed;
2248 SubTP.Context = vContext;
2249 if (It->DoSubTemplate != NULL)
2250 It->DoSubTemplate(SubBuf, &SubTP);
2251 DoTemplate(TKEY(1), SubBuf, &SubTP);
2253 StrBufAppendBuf(Target, SubBuf, 0);
2254 FlushStrBuf(SubBuf);
2255 Status.oddeven = ! Status.oddeven;
2256 vLastContext = vContext;
2260 FreeStrBuf(&SubBuf);
2262 if (It->Destructor != NULL)
2263 It->Destructor(&List);
2267 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2269 IterateStruct *Ctx = CCTX;
2270 if (TP->Tokens->nParameters < 3)
2271 return Ctx->GroupChange;
2273 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2276 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2278 IterateStruct *Ctx = CCTX;
2280 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2282 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2286 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2288 IterateStruct *Ctx = CCTX;
2290 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2294 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2296 IterateStruct *Ctx = CCTX;
2297 StrBufAppendPrintf(Target, "%d", Ctx->n);
2300 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2302 IterateStruct *Ctx = CCTX;
2306 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2308 IterateStruct *Ctx = CCTX;
2314 /*-----------------------------------------------------------------------------
2317 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2319 ConditionalStruct *Cond;
2321 if ((TP->Tokens->Params[0]->len == 1) &&
2322 (TP->Tokens->Params[0]->Start[0] == 'X'))
2323 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2325 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2328 Target, "Conditional", ERR_PARM1, TP,
2333 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2337 if (Cond->CondF(Target, TP) == Neg)
2338 return TP->Tokens->Params[1]->lvalue;
2342 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2347 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2349 subst = (wcsubst*) vsubst;
2351 switch(subst->wcs_type) {
2353 return (subst->wcs_function!=NULL);
2355 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
2359 case WCS_STRBUF_REF:
2360 if (TP->Tokens->nParameters < 4)
2362 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2364 if (TP->Tokens->nParameters < 4)
2365 return (subst->lvalue != 0);
2366 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2368 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2374 void RegisterConditional(const char *Name, long len,
2376 WCConditionalFunc CondF,
2377 int ContextRequired)
2379 ConditionalStruct *Cond;
2381 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2382 memset(Cond, 0, sizeof(ConditionalStruct));
2383 Cond->PlainName = Name;
2384 Cond->Filter.nMaxArgs = nParams;
2385 Cond->Filter.nMinArgs = nParams;
2386 Cond->CondF = CondF;
2387 Cond->Filter.ContextType = ContextRequired;
2388 Cond->Filter.ControlContextType = CTX_NONE;
2389 Put(Conditionals, Name, len, Cond, NULL);
2392 void RegisterControlConditional(const char *Name, long len,
2394 WCConditionalFunc CondF,
2395 int ControlContextRequired)
2397 ConditionalStruct *Cond;
2399 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2400 memset(Cond, 0, sizeof(ConditionalStruct));
2401 Cond->PlainName = Name;
2402 Cond->Filter.nMaxArgs = nParams;
2403 Cond->Filter.nMinArgs = nParams;
2404 Cond->CondF = CondF;
2405 Cond->Filter.ContextType = CTX_NONE;
2406 Cond->Filter.ControlContextType = ControlContextRequired;
2407 Put(Conditionals, Name, len, Cond, NULL);
2410 void RegisterTokenParamDefine(const char *Name, long len,
2415 PVal = (long*)malloc(sizeof(long));
2417 Put(Defines, Name, len, PVal, NULL);
2422 /*-----------------------------------------------------------------------------
2425 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2427 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2429 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2431 StrBuf *TokenText = (StrBuf*) CTX;
2432 const char *CompareToken;
2435 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2436 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2439 /*-----------------------------------------------------------------------------
2443 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2445 WCTemplputParams SubTP;
2447 StrBuf *Headline = NULL;
2448 if (TP->Tokens->nParameters == 2) {
2449 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2450 Headline = NewStrBuf();
2451 DoTemplate(TKEY(1), Headline, TP);
2456 GetTemplateTokenString(Target,
2461 Headline = NewStrBufPlain(Ch, len);
2464 /* else TODO error? logging? */
2465 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2466 SubTP.Context = Headline;
2467 SubTP.Filter.ContextType = CTX_STRBUF;
2468 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2469 DoTemplate(TKEY(0), Target, TP);
2470 DoTemplate(HKEY("endbox"), Target, TP);
2471 FreeStrBuf(&Headline);
2474 /*-----------------------------------------------------------------------------
2478 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2481 int i, ntabs, nTabs;
2483 nTabs = ntabs = TP->Tokens->nParameters / 2;
2484 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2485 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2487 for (i = 0; i < ntabs; i++) {
2488 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2489 (TP->Tokens->Params[i * 2]->len > 0)) {
2490 TabNames[i] = NewStrBuf();
2491 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2493 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2496 GetTemplateTokenString(Target,
2501 TabNames[i] = NewStrBufPlain(Ch, -1);
2504 /** A Tab without subject? we can't count that, add it as silent */
2509 StrTabbedDialog(Target, nTabs, TabNames);
2510 for (i = 0; i < ntabs; i++) {
2511 StrBeginTab(Target, i, nTabs);
2512 DoTemplate(TKEY(i * 2 + 1), Target, TP);
2513 StrEndTab(Target, i, nTabs);
2518 /*-----------------------------------------------------------------------------
2523 void RegisterSortFunc(const char *name, long len,
2524 const char *prepend, long preplen,
2525 CompareFunc Forward,
2526 CompareFunc Reverse,
2527 CompareFunc GroupChange,
2530 SortStruct *NewSort;
2532 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2533 memset(NewSort, 0, sizeof(SortStruct));
2534 NewSort->Name = NewStrBufPlain(name, len);
2535 if (prepend != NULL)
2536 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2538 NewSort->PrefPrepend = NULL;
2539 NewSort->Forward = Forward;
2540 NewSort->Reverse = Reverse;
2541 NewSort->GroupChange = GroupChange;
2542 NewSort->ContextType = ContextType;
2543 if (ContextType == CTX_NONE) {
2544 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2548 Put(SortHash, name, len, NewSort, DestroySortStruct);
2551 CompareFunc RetrieveSort(WCTemplputParams *TP,
2552 const char *OtherPrefix, long OtherPrefixLen,
2553 const char *Default, long ldefault, long DefaultDirection)
2556 const StrBuf *BSort = NULL;
2559 long SortOrder = -1;
2561 if (havebstr("SortBy")) {
2562 BSort = sbstr("SortBy");
2563 if (OtherPrefix == NULL) {
2564 set_room_pref("sort", NewStrBufDup(BSort), 0);
2567 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2570 else { /** Try to fallback to our remembered values... */
2571 if (OtherPrefix == NULL) {
2572 BSort = get_room_pref("sort");
2575 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2578 putbstr("SortBy", NewStrBufDup(BSort));
2582 BSort = Buf = NewStrBufPlain(Default, ldefault);
2583 putbstr("SortBy", Buf);
2587 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2588 (vSortBy == NULL)) {
2590 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2591 (vSortBy == NULL)) {
2593 NULL, "Sorting", ERR_PARM1, TP,
2594 "Illegal default sort: [%s]", Default);
2598 SortBy = (SortStruct*)vSortBy;
2600 if (SortBy->ContextType != TP->Filter.ContextType)
2603 /** Ok, its us, lets see in which direction we should sort... */
2604 if (havebstr("SortOrder")) {
2605 SortOrder = LBSTR("SortOrder");
2607 else { /** Try to fallback to our remembered values... */
2609 if (SortBy->PrefPrepend == NULL) {
2610 Buf = get_room_pref("SortOrder");
2611 SortOrder = StrTol(Buf);
2614 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2618 SortOrder = DefaultDirection;
2620 Buf = NewStrBufPlain(NULL, 64);
2621 StrBufPrintf(Buf, "%ld", SortOrder);
2622 putbstr("SortOrder", Buf);
2624 switch (SortOrder) {
2629 return SortBy->Forward;
2631 return SortBy->Reverse;
2643 ConstStr SortIcons[] = {
2644 {HKEY("static/sort_none.gif")},
2645 {HKEY("static/up_pointer.gif")},
2646 {HKEY("static/down_pointer.gif")},
2649 ConstStr SortNextOrder[] = {
2656 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2658 int bSortError = eNOT_SPECIFIED;
2659 const StrBuf *BSort;
2664 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2666 return eNO_SUCH_SORT;
2667 *Param = (SortStruct*) vSort;
2670 if (havebstr("SortBy")) {
2671 BSort = sbstr("SortBy");
2672 bSortError = eINVALID_PARAM;
2673 if ((*Param)->PrefPrepend == NULL) {
2674 set_room_pref("sort", NewStrBufDup(BSort), 0);
2677 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2680 else { /** Try to fallback to our remembered values... */
2681 if ((*Param)->PrefPrepend == NULL) {
2682 BSort = get_room_pref("sort");
2685 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2689 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2693 *Next = (SortStruct*) vSort;
2695 /** Ok, its us, lets see in which direction we should sort... */
2696 if (havebstr("SortOrder")) {
2697 *SortOrder = LBSTR("SortOrder");
2699 else { /** Try to fallback to our remembered values... */
2700 if ((*Param)->PrefPrepend == NULL) {
2701 *SortOrder = StrTol(get_room_pref("SortOrder"));
2704 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2714 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2719 const ConstStr *SortIcon;
2721 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2724 Target, "Sorter", ERR_PARM1, TP,
2725 " Sorter [%s] unknown!",
2726 TP->Tokens->Params[0]->Start);
2728 case eINVALID_PARAM:
2729 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2730 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2732 case eNOT_SPECIFIED:
2734 if (Next == Param) {
2735 SortIcon = &SortIcons[SortOrder];
2737 else { /** Not Us... */
2738 SortIcon = &SortIcons[0];
2740 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
2744 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
2750 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2753 Target, "Sorter", ERR_PARM1, TP,
2754 " Sorter [%s] unknown!",
2755 TP->Tokens->Params[0]->Start);
2757 case eINVALID_PARAM:
2759 NULL, "Sorter", ERR_PARM1, TP,
2760 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2762 case eNOT_SPECIFIED:
2764 StrBufAppendBuf(Target, Param->Name, 0);
2769 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
2772 const ConstStr *SortOrderStr;
2776 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2779 Target, "Sorter", ERR_PARM1, TP,
2780 " Sorter [%s] unknown!",
2781 TP->Tokens->Params[0]->Start);
2783 case eINVALID_PARAM:
2785 NULL, "Sorter", ERR_PARM1, TP,
2786 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2788 case eNOT_SPECIFIED:
2790 if (Next == Param) {
2791 SortOrderStr = &SortNextOrder[SortOrder];
2793 else { /** Not Us... */
2794 SortOrderStr = &SortNextOrder[0];
2796 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2801 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
2803 long *LongVector = (long*) CTX;
2805 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
2806 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
2808 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
2812 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
2814 Target, "Longvector", ERR_NAME, TP,
2815 "needs a numerical Parameter!");
2819 Target, "LongVector", ERR_PARM1, TP,
2820 "doesn't have %ld Parameters, its just the size of %ld!",
2821 TP->Tokens->Params[0]->lvalue,
2827 void dbg_print_longvector(long *LongVector)
2829 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
2830 int nItems = LongVector[0];
2833 for (i = 0; i < nItems; i++) {
2835 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
2837 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
2840 lprintf(1, ChrPtr(Buf));
2844 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
2846 long *LongVector = (long*) CTX;
2848 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
2849 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
2850 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
2851 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
2853 return LongVector[TP->Tokens->Params[2]->lvalue] ==
2854 LongVector[TP->Tokens->Params[3]->lvalue];
2858 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
2859 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
2861 Target, "ConditionalLongvector", ERR_PARM1, TP,
2862 "needs two long Parameter!");
2866 Target, "Longvector", ERR_PARM1, TP,
2867 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
2868 TP->Tokens->Params[2]->lvalue,
2869 TP->Tokens->Params[3]->lvalue,
2877 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
2879 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
2886 memset(&NoCtx, 0, sizeof(WCTemplputParams));
2887 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
2888 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
2889 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
2890 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
2891 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
2892 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
2893 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, NULL, CTX_NONE);
2894 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
2895 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
2896 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
2897 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
2899 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
2900 conditional_ITERATE_ISGROUPCHANGE,
2902 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
2903 conditional_ITERATE_LASTN,
2905 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
2906 conditional_ITERATE_FIRSTN,
2909 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
2910 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
2911 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
2912 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
2918 ServerStartModule_SUBST
2921 WirelessTemplateCache = NewHash(1, NULL);
2922 WirelessLocalTemplateCache = NewHash(1, NULL);
2923 LocalTemplateCache = NewHash(1, NULL);
2924 TemplateCache = NewHash(1, NULL);
2926 GlobalNS = NewHash(1, NULL);
2927 Iterators = NewHash(1, NULL);
2928 Conditionals = NewHash(1, NULL);
2929 SortHash = NewHash(1, NULL);
2930 Defines = NewHash(1, NULL);
2934 FinalizeModule_SUBST
2941 ServerShutdownModule_SUBST
2944 DeleteHash(&WirelessTemplateCache);
2945 DeleteHash(&WirelessLocalTemplateCache);
2946 DeleteHash(&TemplateCache);
2947 DeleteHash(&LocalTemplateCache);
2949 DeleteHash(&GlobalNS);
2950 DeleteHash(&Iterators);
2951 DeleteHash(&Conditionals);
2952 DeleteHash(&SortHash);
2953 DeleteHash(&Defines);
2958 SessionNewModule_SUBST
2965 SessionAttachModule_SUBST
2968 sess->vars = NewHash(1,NULL);
2972 SessionDetachModule_SUBST
2975 DeleteHash(&sess->vars);
2979 SessionDestroyModule_SUBST