17 #define SHOW_ME_VAPPEND_PRINTF
20 #include "webserver.h"
22 extern char *static_dirs[PATH_MAX]; /* Disk representation */
24 HashList *WirelessTemplateCache;
25 HashList *WirelessLocalTemplateCache;
26 HashList *TemplateCache;
27 HashList *LocalTemplateCache;
31 HashList *Conditionals;
35 int DumpTemplateI18NStrings = 0;
36 int LoadTemplates = 0;
37 int dbg_bactrace_template_errors = 0;
38 WCTemplputParams NoCtx;
39 StrBuf *I18nDump = NULL;
41 const char EmptyStr[]="";
44 #define SV_CONDITIONAL 2
45 #define SV_NEG_CONDITIONAL 3
46 #define SV_CUST_STR_CONDITIONAL 4
48 #define SV_PREEVALUATED 6
52 * Dynamic content for variable substitution in templates
54 typedef struct _wcsubst {
56 int wcs_type; /* which type of Substitution are we */
57 char wcs_key[32]; /* copy of our hashkey for debugging */
58 StrBuf *wcs_value; /* if we're a string, keep it here */
59 long lvalue; /* type long? keep data here */
60 WCHandlerFunc wcs_function; /* funcion hook ???*/
64 typedef struct _WCTemplate {
70 WCTemplateToken **Tokens;
73 typedef struct _HashHandler {
75 WCPreevalFunc PreEvalFunc;
76 WCHandlerFunc HandlerFunc;
79 void *load_template(WCTemplate *NewTemplate);
80 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP);
84 typedef struct _SortStruct {
89 CompareFunc GroupChange;
94 const char *CtxNames[] = {
105 "Context MIME_ATACH",
109 "Context LONGVECTOR",
114 "Context DavNamespace",
120 void DestroySortStruct(void *vSort)
122 SortStruct *Sort = (SortStruct*) vSort;
123 FreeStrBuf(&Sort->Name);
124 FreeStrBuf(&Sort->PrefPrepend);
128 const char *ContextName(int ContextType)
130 if (ContextType < CTX_UNKNOWN)
131 return CtxNames[ContextType];
133 return CtxNames[CTX_UNKNOWN];
136 void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplputParams *TP, const char *Format, ...)
142 const char *Err = NULL;
147 va_start(arg_ptr, Format);
148 StrBufVAppendPrintf(Error, Format, arg_ptr);
152 case ERR_NAME: /* the main token name... */
153 Err = (TP->Tokens!= NULL)? TP->Tokens->pName:"";
156 Err = ((TP->Tokens!= NULL) &&
157 (TP->Tokens->nParameters > ErrorPos - 1))?
158 TP->Tokens->Params[ErrorPos - 1]->Start : "";
161 if (TP->Tokens != NULL)
163 lprintf(1, "%s [%s] (in '%s' line %ld); %s; [%s]\n",
166 ChrPtr(TP->Tokens->FileName),
169 ChrPtr(TP->Tokens->FlatToken));
173 lprintf(1, "%s: %s;\n",
188 if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
189 if (TP->Tokens != NULL)
194 "<pre>\n%s [%s] (in '%s' line %ld); %s\n[%s]\n</pre>\n",
197 ChrPtr(TP->Tokens->FileName),
200 ChrPtr(TP->Tokens->FlatToken));
202 StrBufPrintf(Info, "%s [%s] %s; [%s]",
206 ChrPtr(TP->Tokens->FlatToken));
209 SerializeJson(WCC->WFBuf, WildFireException(SKEY(TP->Tokens->FileName),
214 SerializeJson(Header, WildFireMessage(SKEY(TP->Tokens->FileName),
226 "<pre>\n%s: %s\n</pre>\n",
230 StrBufPrintf(Info, "%s [%s] %s; [%s]",
234 ChrPtr(TP->Tokens->FlatToken));
235 SerializeJson(WCC->WFBuf, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
240 if (dbg_bactrace_template_errors)
248 void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
258 va_start(arg_ptr, Format);
259 StrBufVAppendPrintf(Error, Format, arg_ptr);
262 lprintf(1, ChrPtr(Error));
265 if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
267 SerializeJson(WCC->WFBuf, WildFireException(Type, strlen(Type),
275 if (dbg_bactrace_template_errors)
281 void RegisterNS(const char *NSName,
285 WCHandlerFunc HandlerFunc,
286 WCPreevalFunc PreevalFunc,
289 HashHandler *NewHandler;
291 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
292 memset(NewHandler, 0, sizeof(HashHandler));
293 NewHandler->Filter.nMinArgs = nMinArgs;
294 NewHandler->Filter.nMaxArgs = nMaxArgs;
295 NewHandler->Filter.ContextType = ContextRequired;
296 NewHandler->Filter.ControlContextType = CTX_NONE;
298 NewHandler->PreEvalFunc = PreevalFunc;
299 NewHandler->HandlerFunc = HandlerFunc;
300 Put(GlobalNS, NSName, len, NewHandler, NULL);
303 void RegisterControlNS(const char *NSName,
307 WCHandlerFunc HandlerFunc,
308 int ControlContextRequired)
310 HashHandler *NewHandler;
312 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
313 memset(NewHandler, 0, sizeof(HashHandler));
314 NewHandler->Filter.nMinArgs = nMinArgs;
315 NewHandler->Filter.nMaxArgs = nMaxArgs;
316 NewHandler->Filter.ContextType = CTX_NONE;
317 NewHandler->Filter.ControlContextType = ControlContextRequired;
318 NewHandler->HandlerFunc = HandlerFunc;
319 Put(GlobalNS, NSName, len, NewHandler, NULL);
324 int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, const char *ErrType)
326 if ((Need->ContextType != CTX_NONE) &&
327 (Need->ContextType != TP->Filter.ContextType)) {
329 Target, ErrType, ERR_PARM1, TP,
330 " WARNING: requires Context: [%s], have [%s]!",
331 ContextName(Need->ContextType),
332 ContextName(TP->Filter.ContextType));
336 if ((Need->ControlContextType != CTX_NONE) &&
337 (Need->ControlContextType != TP->Filter.ControlContextType)) {
339 Target, ErrType, ERR_PARM1, TP,
340 " WARNING: requires Control Context: [%s], have [%s]!",
341 ContextName(Need->ControlContextType),
342 ContextName(TP->Filter.ControlContextType));
346 if (TP->Tokens->nParameters < Need->nMinArgs) {
347 LogTemplateError(Target, ErrType, ERR_NAME, TP,
348 "needs at least %ld params, have %ld",
350 TP->Tokens->nParameters);
354 else if (TP->Tokens->nParameters > Need->nMaxArgs) {
355 LogTemplateError(Target, ErrType, ERR_NAME, TP,
356 "just needs %ld params, you gave %ld",
358 TP->Tokens->nParameters);
366 void FreeToken(WCTemplateToken **Token)
369 FreeStrBuf(&(*Token)->FlatToken);
370 if ((*Token)->HaveParameters)
371 for (i = 0; i < (*Token)->nParameters; i++)
372 free((*Token)->Params[i]);
379 void FreeWCTemplate(void *vFreeMe)
382 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
384 if (FreeMe->TokenSpace > 0) {
385 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
386 FreeToken(&FreeMe->Tokens[i]);
388 free(FreeMe->Tokens);
390 FreeStrBuf(&FreeMe->FileName);
391 FreeStrBuf(&FreeMe->Data);
392 FreeStrBuf(&FreeMe->MimeType);
398 * debugging function to print array to log
400 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
403 * debugging function to print array to log
405 void VarPrintEntry(const char *Key, void *vSubst, int odd)
408 lprintf(1,"Subst[%s] : ", Key);
409 ptr = (wcsubst*) vSubst;
411 switch(ptr->wcs_type) {
413 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
416 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
419 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
422 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
429 * Clear out the list of substitution variables local to this session
431 void clear_substs(wcsession *wc) {
433 if (wc->vars != NULL) {
434 DeleteHash(&wc->vars);
439 * Clear out the list of substitution variables local to this session
441 void clear_local_substs(void) {
445 int NeedNewBuf(int type)
460 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
462 int NeedNew = NeedNewBuf(type);
463 switch(ptr->wcs_type) {
467 if (reusestrbuf && NeedNew) {
468 FlushStrBuf(ptr->wcs_value);
472 FreeStrBuf(&ptr->wcs_value);
473 ptr->wcs_value = NULL;
477 ptr->wcs_function = NULL;
478 if (reusestrbuf && NeedNew)
479 ptr->wcs_value = NewStrBuf();
482 ptr->wcs_value = NULL;
483 if (reusestrbuf && NeedNew)
484 ptr->wcs_value = NewStrBuf();
488 if (reusestrbuf && NeedNew)
489 ptr->wcs_value = NewStrBuf();
492 if (reusestrbuf && NeedNew)
493 ptr->wcs_value = NewStrBuf();
500 * destructor; kill one entry.
502 void deletevar(void *data)
504 wcsubst *ptr = (wcsubst*)data;
505 FlushPayload(ptr, 0, ptr->wcs_type);
510 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
515 ptr = (wcsubst *) malloc(sizeof(wcsubst));
516 memset(ptr, 0, sizeof(wcsubst));
518 ptr->wcs_type = type;
519 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
520 Put(WCC->vars, keyname, keylen, ptr, deletevar);
522 switch(ptr->wcs_type) {
525 ptr->wcs_value = NewStrBuf();
539 * Add a substitution variable (local to this session) (strlen version...)
540 * keyname the replacementstring to substitute
541 * keytype the kind of the key
542 * format the format string ala printf
543 * ... the arguments to substitute in the formatstring
545 void SVPRINTF(char *keyname, int keytype, const char *format,...)
553 keylen = strlen(keyname);
555 * First look if we're doing a replacement of
558 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
559 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
560 ptr = (wcsubst*)vPtr;
561 FlushPayload(ptr, keytype, keytype);
562 ptr->wcs_type = keytype;
564 else /** Otherwise allocate a new one */
566 ptr = NewSubstVar(keyname, keylen, keytype);
569 /* Format the string */
570 va_start(arg_ptr, format);
571 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
576 * Add a substitution variable (local to this session)
577 * keyname the replacementstring to substitute
578 * keytype the kind of the key
579 * format the format string ala printf
580 * ... the arguments to substitute in the formatstring
582 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
590 * First look if we're doing a replacement of
593 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
594 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
595 ptr = (wcsubst*)vPtr;
596 FlushPayload(ptr, 1, keytype);
597 ptr->wcs_type = keytype;
599 else /** Otherwise allocate a new one */
601 ptr = NewSubstVar(keyname, keylen, keytype);
604 /** Format the string and save it */
605 va_start(arg_ptr, format);
606 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
611 * Add a substitution variable (local to this session)
612 * keyname the replacementstring to substitute
613 * keytype the kind of the key
614 * format the format string ala printf
615 * ... the arguments to substitute in the formatstring
617 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
625 * First look if we're doing a replacement of
628 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
629 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
630 ptr = (wcsubst*)vPtr;
631 FlushPayload(ptr, 1, keytype);
632 ptr->wcs_type = keytype;
634 else /** Otherwise allocate a new one */
636 ptr = NewSubstVar(keyname, keylen, keytype);
638 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
642 * \brief Add a substitution variable (local to this session)
643 * \param keyname the replacementstring to substitute
644 * \param keytype the kind of the key
645 * \param format the format string ala printf
646 * \param ... the arguments to substitute in the formatstring
648 void SVPutLong(char *keyname, size_t keylen, long Data)
656 * First look if we're doing a replacement of
659 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
660 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
661 ptr = (wcsubst*)vPtr;
662 FlushPayload(ptr, 1, WCS_LONG);
663 ptr->wcs_type = WCS_LONG;
665 else /** Otherwise allocate a new one */
667 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
673 * \brief Add a substitution variable (local to this session) that does a callback
674 * \param keyname the keystring to substitute
675 * \param fcn_ptr the function callback to give the substitution string
677 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
684 * First look if we're doing a replacement of
687 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
688 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
689 ptr = (wcsubst*)vPtr;
690 FlushPayload(ptr, 1, WCS_FUNCTION);
691 ptr->wcs_type = WCS_FUNCTION;
693 else /** Otherwise allocate a new one */
695 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
698 ptr->wcs_function = fcn_ptr;
700 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
702 SVCallback(keyname, strlen(keyname), fcn_ptr);
707 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
714 * First look if we're doing a replacement of
717 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
718 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
719 ptr = (wcsubst*)vPtr;
720 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
721 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
723 else /** Otherwise allocate a new one */
725 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
727 ptr->wcs_value = (StrBuf*)Buf;
731 * \brief back end for print_value_of() ... does a server command
732 * \param servcmd server command to execute on the citadel server
734 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
738 serv_puts(ChrPtr(servcmd));
739 len = serv_getln(buf, sizeof buf);
745 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
748 _fmout(Target, "CENTER");
751 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
757 int HaveTemplateTokenString(StrBuf *Target,
758 WCTemplputParams *TP,
763 if (N >= TP->Tokens->nParameters) {
767 switch (TP->Tokens->Params[N]->Type) {
773 case TYPE_SUBTEMPLATE:
782 void GetTemplateTokenString(StrBuf *Target,
783 WCTemplputParams *TP,
789 WCTemplputParams SubTP;
791 if (N >= TP->Tokens->nParameters) {
792 LogTemplateError(Target,
793 "TokenParameter", N, TP,
794 "invalid token %d. this shouldn't have come till here.\n", N);
800 switch (TP->Tokens->Params[N]->Type) {
804 *Value = TP->Tokens->Params[N]->Start;
805 *len = TP->Tokens->Params[N]->len;
808 if (TP->Tokens->Params[N]->len == 0) {
809 LogTemplateError(Target,
810 "TokenParameter", N, TP,
811 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
816 Buf = (StrBuf*) SBstr(TKEY(N));
817 *Value = ChrPtr(Buf);
818 *len = StrLength(Buf);
821 if (TP->Tokens->Params[N]->len == 0) {
822 LogTemplateError(Target,
823 "TokenParameter", N, TP,
824 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
829 get_PREFERENCE(TKEY(N), &Buf);
830 *Value = ChrPtr(Buf);
831 *len = StrLength(Buf);
834 LogTemplateError(Target,
835 "TokenParameter", N, TP,
836 "Requesting parameter %d; of type LONG, want string.", N);
839 LogTemplateError(Target,
840 "TokenParameter", N, TP,
841 "Requesting parameter %d; of type PREFINT, want string.", N);
844 *Value = _(TP->Tokens->Params[N]->Start);
845 *len = strlen(*Value);
847 case TYPE_SUBTEMPLATE:
848 if (TP->Tokens->Params[N]->len == 0) {
849 LogTemplateError(Target,
850 "TokenParameter", N, TP,
851 "Requesting parameter %d; of type SUBTEMPLATE, empty lookup string not admitted.", N);
857 memset(&SubTP, 0, sizeof(WCTemplputParams *));
858 SubTP.Context = TP->Context;
859 SubTP.Filter.ContextType = TP->Filter.ContextType;
861 DoTemplate(TKEY(N), Buf, &SubTP);
862 *Value = ChrPtr(Buf);
863 *len = StrLength(Buf);
864 /* we can't free it here, so we put it into the subst so its discarded later on. */
865 SVPUTBuf(TKEY(N), Buf, 0);
869 LogTemplateError(Target,
870 "TokenParameter", N, TP,
871 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
876 long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
879 if (N >= TP->Tokens->nParameters) {
880 LogTemplateError(Target,
881 "TokenParameter", N, TP,
882 "invalid token %d. this shouldn't have come till here.\n", N);
887 switch (TP->Tokens->Params[N]->Type) {
890 return atol(TP->Tokens->Params[N]->Start);
893 if (TP->Tokens->Params[N]->len == 0) {
894 LogTemplateError(Target,
895 "TokenParameter", N, TP,
896 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
899 return LBstr(TKEY(N));
902 LogTemplateError(Target,
903 "TokenParameter", N, TP,
904 "requesting a prefstring in param %d want a number", N);
905 if (TP->Tokens->Params[N]->len == 0) {
906 LogTemplateError(Target,
907 "TokenParameter", N, TP,
908 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
911 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
916 return TP->Tokens->Params[N]->lvalue;
918 if (TP->Tokens->Params[N]->len == 0) {
919 LogTemplateError(Target,
920 "TokenParameter", N, TP,
921 "Requesting parameter %d; of type PREFINT, empty lookup string not admitted.", N);
924 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
928 LogTemplateError(Target,
929 "TokenParameter", N, TP,
930 "requesting a I18N string in param %d; want a number", N);
932 case TYPE_SUBTEMPLATE:
933 LogTemplateError(Target,
934 "TokenParameter", N, TP,
935 "requesting a subtemplate in param %d; not supported for numbers", N);
938 LogTemplateError(Target,
939 "TokenParameter", N, TP,
940 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
948 * \brief Print the value of a variable
949 * \param keyname get a key to print
951 void print_value_of(StrBuf *Target, WCTemplputParams *TP)
958 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
959 /* TODO: depricated! */
960 if (TP->Tokens->pName[0] == '=') {
961 DoTemplate(TP->Tokens->pName+1, TP->Tokens->NameEnd - 1, NULL, &NoCtx);
963 /*/////TODO: if param[1] == "U" -> urlescape
965 /** Page-local variables */
966 if ((WCC->vars!= NULL) && GetHash(WCC->vars, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
967 ptr = (wcsubst*) vVar;
968 switch(ptr->wcs_type) {
970 StrBufAppendBuf(Target, ptr->wcs_value, 0);
973 pvo_do_cmd(Target, ptr->wcs_value);
976 (*ptr->wcs_function) (Target, TP);
980 StrBufAppendBuf(Target, ptr->wcs_value, 0);
983 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
987 Target, "Subst", ERR_NAME, TP,
988 "WARNING: invalid value in SV-Hash at %s!", TP->Tokens->pName);
993 Target, "Token", ERR_NAME, TP,
994 "didn't find Handler \"%s\"", TP->Tokens->pName);
999 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
1001 wcsession *WCC = WC;
1005 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1006 ParamToLookup->len, &vVar)) {
1007 ptr = (wcsubst*) vVar;
1008 switch(ptr->wcs_type) {
1011 case WCS_STRBUF_REF:
1012 if (ParamToCompare->Type == TYPE_STR)
1013 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
1014 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
1016 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
1024 if (ParamToCompare->Type == TYPE_STR)
1027 return ParamToCompare->lvalue == ptr->lvalue;
1030 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1031 ParamToLookup->Start);
1037 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
1039 wcsession *WCC = WC;
1043 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1044 ParamToLookup->len, &vVar)) {
1045 ptr = (wcsubst*) vVar;
1046 switch(ptr->wcs_type) {
1049 case WCS_STRBUF_REF:
1050 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
1051 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
1058 return StrTol(Compare) == ptr->lvalue;
1060 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1061 ParamToLookup->Start);
1070 * \brief puts string into the template and computes which escape methon we should use
1071 * \param Source the string we should put into the template
1072 * \param FormatTypeIndex where should we look for escape types if?
1074 void StrBufAppendTemplate(StrBuf *Target,
1075 WCTemplputParams *TP,
1076 const StrBuf *Source, int FormatTypeIndex)
1079 char EscapeAs = ' ';
1081 if ((FormatTypeIndex < TP->Tokens->nParameters) &&
1082 (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
1083 (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
1084 EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
1091 StrEscAppend(Target, Source, NULL, 0, 2);
1094 StrEscAppend(Target, Source, NULL, 0, 0);
1097 StrECMAEscAppend(Target, Source, NULL);
1100 StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 0);
1103 StrBufUrlescAppend(Target, Source, NULL);
1106 StrBufAppendBuf(Target, Source, 0);
1111 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
1113 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
1114 if (Template->TokenSpace <= 0) {
1115 Template->Tokens = (WCTemplateToken**)malloc(
1116 sizeof(WCTemplateToken*) * 10);
1117 memset(Template->Tokens, 0, sizeof(WCTemplateToken*) * 10);
1118 Template->TokenSpace = 10;
1121 WCTemplateToken **NewTokens;
1123 NewTokens= (WCTemplateToken**) malloc(
1124 sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1127 0, sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1131 sizeof(WCTemplateToken*) * Template->nTokensUsed);
1133 free(Template->Tokens);
1134 Template->TokenSpace *= 2;
1135 Template->Tokens = NewTokens;
1138 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
1141 int GetNextParameter(StrBuf *Buf,
1144 WCTemplateToken *Tokens,
1146 WCTemplputParams *TP,
1147 TemplateParam **pParm)
1149 const char *pch = *pCh;
1150 const char *pchs, *pche;
1151 TemplateParam *Parm;
1155 *pParm = Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
1156 memset(Parm, 0, sizeof(TemplateParam));
1157 Parm->Type = TYPE_STR;
1159 /* Skip leading whitespaces */
1160 while ((*pch == ' ' )||
1163 (*pch == '\n')) pch ++;
1166 Parm->Type = TYPE_PREFSTR;
1173 else if (*pch == ';') {
1174 Parm->Type = TYPE_PREFINT;
1181 else if (*pch == '#') {
1182 Parm->Type = TYPE_INTDEFINE;
1185 else if (*pch == '_') {
1186 Parm->Type = TYPE_GETTEXT;
1193 else if (*pch == 'B') {
1194 Parm->Type = TYPE_BSTR;
1201 else if (*pch == '=') {
1202 Parm->Type = TYPE_SUBTEMPLATE;
1213 else if (*pch == '\'')
1215 if (quote != '\0') {
1220 ( (pch > pchs) && (*(pch - 1) == '\\'))
1225 if (*pch != quote) {
1226 lprintf(1, "Error (in '%s' line %ld); "
1227 "evaluating template param [%s] in Token [%s]\n",
1228 ChrPtr(pTmpl->FileName),
1230 ChrPtr(Tokens->FlatToken),
1238 StrBufPeek(Buf, pch, -1, '\0');
1239 if (LoadTemplates > 1) {
1240 lprintf(1, "DBG: got param [%s] %ld %ld\n",
1241 pchs, pche - pchs, strlen(pchs));
1244 Parm->len = pche - pchs;
1245 pch ++; /* move after trailing quote */
1246 if (ParamBrace && (*pch == ')')) {
1253 Parm->Type = TYPE_LONG;
1255 while ((pch <= pe) &&
1261 if (pch - pchs > 1){
1262 StrBufPeek(Buf, pch, -1, '\0');
1263 Parm->lvalue = atol(pchs);
1270 lprintf(1, "Error (in '%s' line %ld); "
1271 "evaluating long template param [%s] in Token [%s]\n",
1272 ChrPtr(pTmpl->FileName),
1274 ChrPtr(Tokens->FlatToken),
1282 while ((*pch == ' ' )||
1286 (*pch == '\n')) pch ++;
1291 if (DumpTemplateI18NStrings) {
1292 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
1295 case TYPE_INTDEFINE: {
1298 if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
1302 PVal = (long*) vPVal;
1304 Parm->lvalue = *PVal;
1308 LogTemplateError(NULL, "Define",
1309 Tokens->nParameters,
1315 case TYPE_SUBTEMPLATE:{
1317 /* well, we don't check the mobile stuff here... */
1318 if (!GetHash(LocalTemplateCache, Parm->Start, Parm->len, &vTmpl) &&
1319 !GetHash(TemplateCache, Parm->Start, Parm->len, &vTmpl)) {
1320 LogTemplateError(NULL,
1322 Tokens->nParameters,
1324 "referenced here doesn't exist");
1332 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
1334 const char *pTokenStart,
1335 const char *pTokenEnd,
1341 WCTemplateToken *NewToken;
1342 WCTemplputParams TP;
1344 NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
1345 memset(NewToken, 0, sizeof(WCTemplateToken));
1346 TP.Tokens = NewToken;
1347 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
1348 NewToken->Flags = 0;
1349 NewToken->Line = Line + 1;
1350 NewToken->pTokenStart = pTokenStart;
1351 NewToken->TokenStart = pTokenStart - pStart;
1352 NewToken->TokenEnd = (pTokenEnd - pStart) - NewToken->TokenStart;
1353 NewToken->pTokenEnd = pTokenEnd;
1354 NewToken->NameEnd = NewToken->TokenEnd - 2;
1355 NewToken->PreEval = NULL;
1356 NewToken->FlatToken = NewStrBufPlain(pTokenStart + 2, pTokenEnd - pTokenStart - 2);
1357 StrBufShrinkToFit(NewToken->FlatToken, 1);
1359 StrBufPeek(Buf, pTokenStart, + 1, '\0');
1360 StrBufPeek(Buf, pTokenEnd, -1, '\0');
1361 pch = NewToken->pName = pTokenStart + 2;
1363 NewToken->HaveParameters = 0;;
1364 NewToken->nParameters = 0;
1366 while (pch < pTokenEnd - 1) {
1368 StrBufPeek(Buf, pch, -1, '\0');
1369 NewToken->NameEnd = pch - NewToken->pName;
1371 if (*(pTokenEnd - 1) != ')') {
1373 NULL, "Parseerror", ERR_NAME, &TP,
1374 "Warning, Non welformed Token; missing right parenthesis");
1376 while (pch < pTokenEnd - 1) {
1377 NewToken->nParameters++;
1378 if (GetNextParameter(Buf,
1384 &NewToken->Params[NewToken->nParameters - 1]))
1386 NewToken->HaveParameters = 1;
1387 if (NewToken->nParameters > MAXPARAM) {
1389 NULL, "Parseerror", ERR_NAME, &TP,
1390 "only [%d] Params allowed in Tokens",
1393 FreeToken(&NewToken);
1399 if((NewToken->NameEnd == 1) &&
1400 (NewToken->HaveParameters == 1))
1403 if (*(NewToken->pName) == '_')
1404 NewToken->Flags = SV_GETTEXT;
1405 else if (*(NewToken->pName) == '=')
1406 NewToken->Flags = SV_SUBTEMPL;
1407 else if (*(NewToken->pName) == '%')
1408 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
1409 else if (*(NewToken->pName) == '?')
1410 NewToken->Flags = SV_CONDITIONAL;
1411 else if (*(NewToken->pName) == '!')
1412 NewToken->Flags = SV_NEG_CONDITIONAL;
1418 switch (NewToken->Flags) {
1420 /* If we're able to find out more about the token, do it now while its fresh. */
1421 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1422 HashHandler *Handler;
1423 Handler = (HashHandler*) vVar;
1424 if ((NewToken->nParameters < Handler->Filter.nMinArgs) ||
1425 (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
1427 NULL, "Token", ERR_NAME, &TP,
1428 "doesn't work with %d params",
1429 NewToken->nParameters);
1433 NewToken->PreEval = Handler;
1434 NewToken->Flags = SV_PREEVALUATED;
1435 if (Handler->PreEvalFunc != NULL)
1436 Handler->PreEvalFunc(NewToken);
1441 if (NewToken->nParameters !=1) {
1443 NULL, "Gettext", ERR_NAME, &TP,
1444 "requires exactly 1 parameter, you gave %d params",
1445 NewToken->nParameters);
1446 NewToken->Flags = 0;
1449 if (DumpTemplateI18NStrings) {
1450 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
1454 if (NewToken->nParameters != 1) {
1456 NULL, "Subtemplates", ERR_NAME, &TP,
1457 "require exactly 1 parameter, you gave %d params",
1458 NewToken->nParameters);
1463 /* well, we don't check the mobile stuff here... */
1464 if (!GetHash(LocalTemplateCache,
1465 NewToken->Params[0]->Start,
1466 NewToken->Params[0]->len,
1468 !GetHash(TemplateCache,
1469 NewToken->Params[0]->Start,
1470 NewToken->Params[0]->len,
1473 NULL, "SubTemplate", ERR_PARM1, &TP,
1478 case SV_CUST_STR_CONDITIONAL:
1479 case SV_CONDITIONAL:
1480 case SV_NEG_CONDITIONAL:
1481 if (NewToken->nParameters <2) {
1483 NULL, "Conditional", ERR_PARM1, &TP,
1484 "require at least 2 parameters, you gave %d params",
1485 NewToken->nParameters);
1486 NewToken->Flags = 0;
1489 if (NewToken->Params[1]->lvalue == 0) {
1491 NULL, "Conditional", ERR_PARM1, &TP,
1492 "Conditional ID (Parameter 1) mustn't be 0!");
1493 NewToken->Flags = 0;
1496 if (!GetHash(Conditionals,
1497 NewToken->Params[0]->Start,
1498 NewToken->Params[0]->len,
1501 if ((NewToken->Params[0]->len == 1) &&
1502 (NewToken->Params[0]->Start[0] == 'X'))
1505 NULL, "Conditional", ERR_PARM1, &TP,
1508 NewToken->Error = NewStrBuf();
1511 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1512 NewToken->Params[0]->Start,
1513 ChrPtr(pTmpl->FileName),
1515 ChrPtr(NewToken->FlatToken));
1519 NewToken->PreEval = vVar;
1531 * \brief Display a variable-substituted template
1532 * \param templatename template file to load
1534 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1536 WCTemplate *NewTemplate;
1538 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1539 memset(NewTemplate, 0, sizeof(WCTemplate));
1540 NewTemplate->Data = NULL;
1541 NewTemplate->FileName = NewStrBufDup(filename);
1542 StrBufShrinkToFit(NewTemplate->FileName, 1);
1543 NewTemplate->nTokensUsed = 0;
1544 NewTemplate->TokenSpace = 0;
1545 NewTemplate->Tokens = NULL;
1546 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1547 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1548 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1551 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1552 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1555 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1560 * \brief Display a variable-substituted template
1561 * \param templatename template file to load
1563 void *duplicate_template(WCTemplate *OldTemplate)
1565 WCTemplate *NewTemplate;
1567 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1568 memset(NewTemplate, 0, sizeof(WCTemplate));
1569 NewTemplate->Data = NULL;
1570 NewTemplate->FileName = NewStrBufDup(OldTemplate->FileName);
1571 StrBufShrinkToFit(NewTemplate->FileName, 1);
1572 NewTemplate->nTokensUsed = 0;
1573 NewTemplate->TokenSpace = 0;
1574 NewTemplate->Tokens = NULL;
1575 NewTemplate->MimeType = NewStrBufDup(OldTemplate->MimeType);
1580 * \brief Display a variable-substituted template
1581 * \param templatename template file to load
1583 void *load_template(WCTemplate *NewTemplate)
1586 struct stat statbuf;
1587 const char *pS, *pE, *pch, *Err;
1591 fd = open(ChrPtr(NewTemplate->FileName), O_RDONLY);
1593 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1594 ChrPtr(NewTemplate->FileName), strerror(errno));
1598 if (fstat(fd, &statbuf) == -1) {
1599 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1600 ChrPtr(NewTemplate->FileName), strerror(errno));
1604 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size + 1);
1605 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1607 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1608 ChrPtr(NewTemplate->FileName), strerror(errno));
1609 //FreeWCTemplate(NewTemplate);/////tODO
1615 StrBufShrinkToFit(NewTemplate->Data, 1);
1616 StrBufShrinkToFit(NewTemplate->MimeType, 1);
1617 pS = pch = ChrPtr(NewTemplate->Data);
1618 pE = pS + StrLength(NewTemplate->Data);
1620 const char *pts, *pte;
1622 int InDoubleQuotes = 0;
1624 /** Find one <? > */
1626 for (; pch < pE; pch ++) {
1627 if ((*pch=='<')&&(*(pch + 1)=='?') &&
1628 !((pch == pS) && /* we must ommit a <?xml */
1629 (*(pch + 2) == 'x') &&
1630 (*(pch + 3) == 'm') &&
1631 (*(pch + 4) == 'l')))
1633 if (*pch=='\n') Line ++;
1639 /** Found one? parse it. */
1640 for (; pch <= pE - 1; pch ++) {
1642 InDoubleQuotes = ! InDoubleQuotes;
1643 else if (*pch == '\'')
1644 InQuotes = ! InQuotes;
1645 else if ((!InQuotes && !InDoubleQuotes) &&
1646 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1654 PutNewToken(NewTemplate,
1655 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1662 const char* PrintTemplate(void *vSubst)
1664 WCTemplate *Tmpl = vSubst;
1666 return ChrPtr(Tmpl->FileName);
1670 int LoadTemplateDir(const StrBuf *DirName, HashList *wireless, HashList *big, const StrBuf *BaseKey)
1676 StrBuf *SubDirectory;
1677 DIR *filedir = NULL;
1678 struct dirent *filedir_entry;
1685 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
1690 filedir = opendir (ChrPtr(DirName));
1691 if (filedir == NULL) {
1696 Toplevel = StrLength(BaseKey) == 0;
1697 SubDirectory = NewStrBuf();
1698 SubKey = NewStrBuf();
1699 FileName = NewStrBufPlain(NULL, PATH_MAX);
1701 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
1702 (filedir_entry != NULL))
1706 #ifdef _DIRENT_HAVE_D_NAMELEN
1707 d_namelen = filedir_entry->d_namelen;
1708 d_type = filedir_entry->d_type;
1712 #define DT_UNKNOWN 0
1717 #define IFTODT(mode) (((mode) & 0170000) >> 12)
1718 #define DTTOIF(dirtype) ((dirtype) << 12)
1720 d_namelen = strlen(filedir_entry->d_name);
1721 d_type = DT_UNKNOWN;
1723 d_without_ext = d_namelen;
1725 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1726 continue; /* Ignore backup files... */
1728 if ((d_namelen == 1) &&
1729 (filedir_entry->d_name[0] == '.'))
1732 if ((d_namelen == 2) &&
1733 (filedir_entry->d_name[0] == '.') &&
1734 (filedir_entry->d_name[1] == '.'))
1737 if (d_type == DT_UNKNOWN) {
1739 char path[PATH_MAX];
1740 snprintf(path, PATH_MAX, "%s/%s",
1741 ChrPtr(DirName), filedir_entry->d_name);
1742 if (stat(path, &s) == 0) {
1743 d_type = IFTODT(s.st_mode);
1749 /* Skip directories we are not interested in... */
1750 if (strcmp(filedir_entry->d_name, ".svn") == 0)
1753 FlushStrBuf(SubKey);
1755 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1756 StrBufAppendBuf(SubKey, BaseKey, 0);
1757 StrBufAppendBufPlain(SubKey, HKEY("_"), 0);
1759 StrBufAppendBufPlain(SubKey, filedir_entry->d_name, d_namelen, 0);
1761 FlushStrBuf(SubDirectory);
1762 StrBufAppendBuf(SubDirectory, DirName, 0);
1763 if (ChrPtr(SubDirectory)[StrLength(SubDirectory) - 1] != '/')
1764 StrBufAppendBufPlain(SubDirectory, HKEY("/"), 0);
1765 StrBufAppendBufPlain(SubDirectory, filedir_entry->d_name, d_namelen, 0);
1767 LoadTemplateDir(SubDirectory, wireless, big, SubKey);
1770 case DT_LNK: /* TODO: check whether its a file or a directory */
1774 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1776 if ((d_without_ext == 0) || (d_namelen < 3))
1778 if (((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~') ||
1779 (strcmp(&filedir_entry->d_name[d_without_ext], ".orig") == 0) ||
1780 (strcmp(&filedir_entry->d_name[d_without_ext], ".swp") == 0))
1781 continue; /* Ignore backup files... */
1782 /* .m.xxx is for mobile useragents! */
1784 if (d_without_ext > 2)
1785 IsMobile = (filedir_entry->d_name[d_without_ext - 1] == 'm') &&
1786 (filedir_entry->d_name[d_without_ext - 2] == '.');
1787 PStart = filedir_entry->d_name;
1788 StrBufPrintf(FileName, "%s/%s", ChrPtr(DirName), filedir_entry->d_name);
1789 MinorPtr = strchr(filedir_entry->d_name, '.');
1790 if (MinorPtr != NULL)
1794 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1795 StrBufAppendBuf(Key, BaseKey, 0);
1796 StrBufAppendBufPlain(Key, HKEY("_"), 0);
1798 StrBufAppendBufPlain(Key, filedir_entry->d_name, MinorPtr - filedir_entry->d_name, 0);
1800 if (LoadTemplates >= 1)
1801 lprintf(1, "%s %d %s\n", ChrPtr(FileName), IsMobile, ChrPtr(Key));
1802 prepare_template(FileName, Key, (IsMobile)?wireless:big);
1809 FreeStrBuf(&FileName);
1811 FreeStrBuf(&SubDirectory);
1812 FreeStrBuf(&SubKey);
1816 void InitTemplateCache(void)
1821 HashList *Templates[4];
1826 /* Primary Template set... */
1827 StrBufPrintf(Dir, "%s/t", static_dirs[0]);
1828 LoadTemplateDir(Dir,
1829 WirelessTemplateCache,
1833 /* User local Template set */
1834 StrBufPrintf(Dir, "%s/t", static_dirs[1]);
1835 LoadTemplateDir(Dir,
1836 WirelessLocalTemplateCache,
1840 /* Debug Templates, just to be loaded while debugging. */
1842 StrBufPrintf(Dir, "%s/dbg", static_dirs[0]);
1843 LoadTemplateDir(Dir,
1844 WirelessTemplateCache,
1847 Templates[0] = WirelessTemplateCache;
1848 Templates[1] = TemplateCache;
1849 Templates[2] = WirelessLocalTemplateCache;
1850 Templates[3] = LocalTemplateCache;
1853 if (LoadTemplates == 0)
1854 for (i=0; i < 4; i++) {
1860 At = GetNewHashPos(Templates[i], 0);
1861 while (GetNextHashPos(Templates[i],
1866 (vTemplate != NULL))
1868 load_template((WCTemplate *)vTemplate);
1880 /*-----------------------------------------------------------------------------
1881 * Filling & processing Templates
1884 * \brief executes one token
1885 * \param Target buffer to append to
1886 * \param Token da to process.
1887 * \param Template we're iterating
1888 * \param Context Contextpoointer to pass in
1889 * \param state are we in conditional state?
1890 * \param ContextType what type of information does context giv us?
1892 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1894 const char *AppendMe;
1896 HashHandler *Handler;
1899 /* much output, since pName is not terminated...
1900 lprintf(1,"Doing token: %s\n",Token->pName);
1903 switch (TP->Tokens->Flags) {
1905 TmplGettext(Target, TP);
1907 case SV_CONDITIONAL: /** Forward conditional evaluation */
1908 return EvaluateConditional(Target, 1, state, TP);
1910 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1911 return EvaluateConditional(Target, 0, state, TP);
1913 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1914 if (TP->Tokens->nParameters >= 6) {
1915 if (EvaluateConditional(Target, 0, state, TP)) {
1916 GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
1917 StrBufAppendBufPlain(Target,
1923 GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
1924 StrBufAppendBufPlain(Target,
1932 Target, "Conditional", ERR_NAME, TP,
1933 "needs at least 6 Params!");
1937 if (TP->Tokens->nParameters == 1)
1938 DoTemplate(TKEY(0), Target, TP);
1940 case SV_PREEVALUATED:
1941 Handler = (HashHandler*) TP->Tokens->PreEval;
1942 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1945 Handler->HandlerFunc(Target, TP);
1948 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
1949 Handler = (HashHandler*) vVar;
1950 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1954 Handler->HandlerFunc(Target, TP);
1958 print_value_of(Target, TP);
1966 const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
1968 WCTemplate *pTmpl = Tmpl;
1971 const char *pData, *pS;
1973 WCTemplputParams TP;
1975 memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
1977 TP.Context = CallingTP->Context;
1978 TP.ControlContext = CallingTP->ControlContext;
1980 if (LoadTemplates != 0) {
1981 if (LoadTemplates > 1)
1982 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1983 ChrPtr(Tmpl->FileName));
1984 pTmpl = duplicate_template(Tmpl);
1985 if(load_template(pTmpl) == NULL) {
1988 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1989 ChrPtr(Tmpl->FileName));
1990 FreeWCTemplate(pTmpl);
1996 pS = pData = ChrPtr(pTmpl->Data);
1997 len = StrLength(pTmpl->Data);
2001 if (i >= pTmpl->nTokensUsed) {
2002 StrBufAppendBufPlain(Target,
2004 len - (pData - pS), 0);
2008 StrBufAppendBufPlain(
2010 pTmpl->Tokens[i]->pTokenStart - pData, 0);
2011 TP.Tokens = pTmpl->Tokens[i];
2012 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2013 state = EvaluateToken(Target, state, &TP);
2015 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
2016 /* condition told us to skip till its end condition */
2018 TP.Tokens = pTmpl->Tokens[i];
2019 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2020 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
2021 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
2022 if (state == EvaluateConditional(
2024 pTmpl->Tokens[i]->Flags,
2030 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
2031 if (i > pTmpl->nTokensUsed)
2035 if (LoadTemplates != 0) {
2036 FreeWCTemplate(pTmpl);
2038 return Tmpl->MimeType;
2043 * \brief Display a variable-substituted template
2044 * \param templatename template file to load
2045 * \returns the mimetype of the template its doing
2047 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
2049 WCTemplputParams LocalTP;
2051 HashList *StaticLocal;
2057 memset(&LocalTP, 0, sizeof(WCTemplputParams));
2061 if (WC->is_mobile > 0) {
2062 Static = WirelessTemplateCache;
2063 StaticLocal = WirelessLocalTemplateCache;
2066 Static = TemplateCache;
2067 StaticLocal = LocalTemplateCache;
2072 lprintf (1, "Can't to load a template with empty name!\n");
2073 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
2077 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
2078 !GetHash(Static, templatename, len, &vTmpl)) {
2079 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
2080 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
2082 (long)strlen(templatename));
2084 dbg_PrintHash(Static, PrintTemplate, NULL);
2085 PrintHash(Static, VarPrintTransition, PrintTemplate);
2091 return ProcessTemplate(vTmpl, Target, TP);
2095 /*-----------------------------------------------------------------------------
2098 typedef struct _HashIterator {
2099 HashList *StaticList;
2100 int AdditionalParams;
2102 int XPectContextType;
2104 RetrieveHashlistFunc GetHash;
2105 HashDestructorFunc Destructor;
2106 SubTemplFunc DoSubTemplate;
2109 void RegisterITERATOR(const char *Name, long len,
2110 int AdditionalParams,
2111 HashList *StaticList,
2112 RetrieveHashlistFunc GetHash,
2113 SubTemplFunc DoSubTempl,
2114 HashDestructorFunc Destructor,
2116 int XPectContextType,
2121 It = (HashIterator*)malloc(sizeof(HashIterator));
2122 memset(It, 0, sizeof(HashIterator));
2123 It->StaticList = StaticList;
2124 It->AdditionalParams = AdditionalParams;
2125 It->GetHash = GetHash;
2126 It->DoSubTemplate = DoSubTempl;
2127 It->Destructor = Destructor;
2128 It->ContextType = ContextType;
2129 It->XPectContextType = XPectContextType;
2131 Put(Iterators, Name, len, It, NULL);
2134 typedef struct _iteratestruct {
2143 int preeval_iterate(WCTemplateToken *Token)
2145 WCTemplputParams TPP;
2146 WCTemplputParams *TP;
2150 memset(&TPP, 0, sizeof(WCTemplputParams));
2153 if (!GetHash(Iterators, TKEY(0), &vIt)) {
2155 NULL, "Iterator", ERR_PARM1, TP,
2159 if (TP->Tokens->Params[1]->Type != TYPE_SUBTEMPLATE) {
2160 LogTemplateError(NULL, "Iterator", ERR_PARM1, TP,
2161 "Need token with type Subtemplate as param 1, have %s",
2162 TP->Tokens->Params[1]->Start);
2165 /* well, we don't check the mobile stuff here... */
2166 if (!GetHash(LocalTemplateCache, TKEY(1), &vTmpl) &&
2167 !GetHash(TemplateCache, TKEY(1), &vTmpl)) {
2168 LogTemplateError(NULL, "SubTemplate", ERR_PARM1, TP,
2169 "referenced here doesn't exist");
2171 Token->Preeval2 = vIt;
2175 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
2180 SortStruct *SortBy = NULL;
2182 int DetectGroupChange = 0;
2185 void *vLastContext = NULL;
2187 WCTemplputParams SubTP;
2188 IterateStruct Status;
2194 memset(&Status, 0, sizeof(IterateStruct));
2195 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2197 It = (HashIterator*) TP->Tokens->Preeval2;
2200 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
2204 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2206 Target, "Iterator", ERR_PARM1, TP,
2207 "doesn't work with %d params",
2208 TP->Tokens->nParameters);
2212 if ((It->XPectContextType != CTX_NONE) &&
2213 (It->XPectContextType != TP->Filter.ContextType)) {
2215 Target, "Iterator", ERR_PARM1, TP,
2216 "requires context of type %d, have %d",
2217 It->XPectContextType,
2218 TP->Filter.ContextType);
2223 if (It->StaticList == NULL)
2224 List = It->GetHash(Target, TP);
2226 List = It->StaticList;
2228 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2229 if (DetectGroupChange) {
2230 const StrBuf *BSort;
2231 DetectGroupChange = 0;
2232 if (havebstr("SortBy")) {
2233 BSort = sbstr("SortBy");
2234 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2235 (vSortBy != NULL)) {
2236 SortBy = (SortStruct*)vSortBy;
2237 /** Ok, its us, lets see in which direction we should sort... */
2238 if (havebstr("SortOrder")) {
2240 SortOrder = LBSTR("SortOrder");
2242 DetectGroupChange = 1;
2247 nMembersUsed = GetCount(List);
2248 SubBuf = NewStrBuf();
2249 SubTP.Filter.ContextType = It->ContextType;
2250 SubTP.Filter.ControlContextType = CTX_ITERATE;
2251 SubTP.ControlContext = &Status;
2253 if (HAVE_PARAM(2)) {
2254 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2256 if (HAVE_PARAM(3)) {
2257 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2259 if (HAVE_PARAM(4)) {
2260 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2262 it = GetNewHashPos(List, StepWidth);
2264 StopAt = GetCount(List);
2266 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2267 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2268 if (DetectGroupChange && Status.n > 0) {
2269 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2271 Status.LastN = (Status.n + 1) == nMembersUsed;
2272 SubTP.Context = vContext;
2273 if (It->DoSubTemplate != NULL)
2274 It->DoSubTemplate(SubBuf, &SubTP);
2275 DoTemplate(TKEY(1), SubBuf, &SubTP);
2277 StrBufAppendBuf(Target, SubBuf, 0);
2278 FlushStrBuf(SubBuf);
2279 Status.oddeven = ! Status.oddeven;
2280 vLastContext = vContext;
2284 FreeStrBuf(&SubBuf);
2286 if (It->Destructor != NULL)
2287 It->Destructor(&List);
2291 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2293 IterateStruct *Ctx = CCTX;
2294 if (TP->Tokens->nParameters < 3)
2295 return Ctx->GroupChange;
2297 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2300 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2302 IterateStruct *Ctx = CCTX;
2304 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2306 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2310 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2312 IterateStruct *Ctx = CCTX;
2314 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2318 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2320 IterateStruct *Ctx = CCTX;
2321 StrBufAppendPrintf(Target, "%d", Ctx->n);
2324 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2326 IterateStruct *Ctx = CCTX;
2330 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2332 IterateStruct *Ctx = CCTX;
2338 /*-----------------------------------------------------------------------------
2341 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2343 ConditionalStruct *Cond;
2345 if ((TP->Tokens->Params[0]->len == 1) &&
2346 (TP->Tokens->Params[0]->Start[0] == 'X'))
2347 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2349 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2352 Target, "Conditional", ERR_PARM1, TP,
2357 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2361 if (Cond->CondF(Target, TP) == Neg)
2362 return TP->Tokens->Params[1]->lvalue;
2366 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2371 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2373 subst = (wcsubst*) vsubst;
2375 switch(subst->wcs_type) {
2377 return (subst->wcs_function!=NULL);
2379 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
2383 case WCS_STRBUF_REF:
2384 if (TP->Tokens->nParameters < 4)
2386 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2388 if (TP->Tokens->nParameters < 4)
2389 return (subst->lvalue != 0);
2390 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2392 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2398 void RegisterConditional(const char *Name, long len,
2400 WCConditionalFunc CondF,
2401 int ContextRequired)
2403 ConditionalStruct *Cond;
2405 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2406 memset(Cond, 0, sizeof(ConditionalStruct));
2407 Cond->PlainName = Name;
2408 Cond->Filter.nMaxArgs = nParams;
2409 Cond->Filter.nMinArgs = nParams;
2410 Cond->CondF = CondF;
2411 Cond->Filter.ContextType = ContextRequired;
2412 Cond->Filter.ControlContextType = CTX_NONE;
2413 Put(Conditionals, Name, len, Cond, NULL);
2416 void RegisterControlConditional(const char *Name, long len,
2418 WCConditionalFunc CondF,
2419 int ControlContextRequired)
2421 ConditionalStruct *Cond;
2423 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2424 memset(Cond, 0, sizeof(ConditionalStruct));
2425 Cond->PlainName = Name;
2426 Cond->Filter.nMaxArgs = nParams;
2427 Cond->Filter.nMinArgs = nParams;
2428 Cond->CondF = CondF;
2429 Cond->Filter.ContextType = CTX_NONE;
2430 Cond->Filter.ControlContextType = ControlContextRequired;
2431 Put(Conditionals, Name, len, Cond, NULL);
2434 void RegisterTokenParamDefine(const char *Name, long len,
2439 PVal = (long*)malloc(sizeof(long));
2441 Put(Defines, Name, len, PVal, NULL);
2444 long GetTokenDefine(const char *Name, long len,
2449 if (GetHash(Defines, Name, len, &vPVal) &&
2452 return *(long*) vPVal;
2460 void tmplput_DefStr(StrBuf *Target, WCTemplputParams *TP)
2464 GetTemplateTokenString(Target, TP, 2, &Str, &len);
2466 StrBufAppendBufPlain(Target, Str, len, 0);
2469 void tmplput_DefVal(StrBuf *Target, WCTemplputParams *TP)
2473 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2474 StrBufAppendPrintf(Target, "%d", val);
2479 /*-----------------------------------------------------------------------------
2482 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2484 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2486 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2488 StrBuf *TokenText = (StrBuf*) CTX;
2489 const char *CompareToken;
2492 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2493 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2496 void tmplput_ContextStringArray(StrBuf *Target, WCTemplputParams *TP)
2498 HashList *Arr = (HashList*) CTX;
2502 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2503 if (GetHash(Arr, IKEY(val), &pV) &&
2505 StrBufAppendTemplate(Target, TP, (StrBuf*)pV, 1);
2508 int ConditionalContextStrinArray(StrBuf *Target, WCTemplputParams *TP)
2510 HashList *Arr = (HashList*) CTX;
2513 const char *CompareToken;
2516 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2517 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2518 if (GetHash(Arr, IKEY(val), &pV) &&
2520 return strcmp(ChrPtr((StrBuf*)pV), CompareToken) == 0;
2526 /*-----------------------------------------------------------------------------
2530 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2532 WCTemplputParams SubTP;
2534 StrBuf *Headline = NULL;
2535 if (TP->Tokens->nParameters == 2) {
2536 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2537 Headline = NewStrBuf();
2538 DoTemplate(TKEY(1), Headline, TP);
2543 GetTemplateTokenString(Target,
2548 Headline = NewStrBufPlain(Ch, len);
2551 /* else TODO error? logging? */
2552 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2553 SubTP.Context = Headline;
2554 SubTP.Filter.ContextType = CTX_STRBUF;
2555 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2556 DoTemplate(TKEY(0), Target, TP);
2557 DoTemplate(HKEY("endbox"), Target, TP);
2558 FreeStrBuf(&Headline);
2561 /*-----------------------------------------------------------------------------
2564 int preeval_do_tabbed(WCTemplateToken *Token)
2566 WCTemplputParams TPP;
2567 WCTemplputParams *TP;
2573 memset(&TPP, 0, sizeof(WCTemplputParams));
2576 nTabs = TP->Tokens->nParameters / 2 - 1;
2577 if (TP->Tokens->nParameters % 2 != 0)
2579 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2580 "need even number of arguments");
2584 else for (i = 0; i < nTabs; i++) {
2585 if (!HaveTemplateTokenString(NULL,
2590 (TP->Tokens->Params[i * 2]->len == 0))
2592 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2593 "Tab-Subject %d needs to be able to produce a string, have %s",
2594 i, TP->Tokens->Params[i * 2]->Start);
2597 if (!HaveTemplateTokenString(NULL,
2602 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2604 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2605 "Tab-Content %d needs to be able to produce a string, have %s",
2606 i, TP->Tokens->Params[i * 2 + 1]->Start);
2611 if (!HaveTemplateTokenString(NULL,
2616 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2618 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2619 "Tab-Content %d needs to be able to produce a string, have %s",
2620 i, TP->Tokens->Params[i * 2 + 1]->Start);
2627 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2630 int i, ntabs, nTabs;
2632 nTabs = ntabs = TP->Tokens->nParameters / 2;
2633 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2634 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2636 for (i = 0; i < ntabs; i++) {
2637 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2638 (TP->Tokens->Params[i * 2]->len > 0)) {
2639 TabNames[i] = NewStrBuf();
2640 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2642 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2645 GetTemplateTokenString(Target,
2650 TabNames[i] = NewStrBufPlain(Ch, -1);
2653 /** A Tab without subject? we can't count that, add it as silent */
2658 StrTabbedDialog(Target, nTabs, TabNames);
2659 for (i = 0; i < ntabs; i++) {
2660 StrBeginTab(Target, i, nTabs);
2661 DoTemplate(TKEY(i * 2 + 1), Target, TP);
2662 StrEndTab(Target, i, nTabs);
2664 for (i = 0; i < ntabs; i++)
2665 FreeStrBuf(&TabNames[i]);
2669 /*-----------------------------------------------------------------------------
2674 void RegisterSortFunc(const char *name, long len,
2675 const char *prepend, long preplen,
2676 CompareFunc Forward,
2677 CompareFunc Reverse,
2678 CompareFunc GroupChange,
2681 SortStruct *NewSort;
2683 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2684 memset(NewSort, 0, sizeof(SortStruct));
2685 NewSort->Name = NewStrBufPlain(name, len);
2686 if (prepend != NULL)
2687 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2689 NewSort->PrefPrepend = NULL;
2690 NewSort->Forward = Forward;
2691 NewSort->Reverse = Reverse;
2692 NewSort->GroupChange = GroupChange;
2693 NewSort->ContextType = ContextType;
2694 if (ContextType == CTX_NONE) {
2695 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2699 Put(SortHash, name, len, NewSort, DestroySortStruct);
2702 CompareFunc RetrieveSort(WCTemplputParams *TP,
2703 const char *OtherPrefix, long OtherPrefixLen,
2704 const char *Default, long ldefault, long DefaultDirection)
2707 const StrBuf *BSort = NULL;
2710 long SortOrder = -1;
2712 if (havebstr("SortBy")) {
2713 BSort = sbstr("SortBy");
2714 if (OtherPrefix == NULL) {
2715 set_room_pref("sort", NewStrBufDup(BSort), 0);
2718 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2721 else { /** Try to fallback to our remembered values... */
2722 if (OtherPrefix == NULL) {
2723 BSort = get_room_pref("sort");
2726 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2729 putbstr("SortBy", NewStrBufDup(BSort));
2733 BSort = Buf = NewStrBufPlain(Default, ldefault);
2734 putbstr("SortBy", Buf);
2738 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2739 (vSortBy == NULL)) {
2741 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2742 (vSortBy == NULL)) {
2744 NULL, "Sorting", ERR_PARM1, TP,
2745 "Illegal default sort: [%s]", Default);
2749 SortBy = (SortStruct*)vSortBy;
2751 if (SortBy->ContextType != TP->Filter.ContextType)
2754 /** Ok, its us, lets see in which direction we should sort... */
2755 if (havebstr("SortOrder")) {
2756 SortOrder = LBSTR("SortOrder");
2758 else { /** Try to fallback to our remembered values... */
2760 if (SortBy->PrefPrepend == NULL) {
2761 Buf = get_room_pref("SortOrder");
2762 SortOrder = StrTol(Buf);
2765 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2769 SortOrder = DefaultDirection;
2771 Buf = NewStrBufPlain(NULL, 64);
2772 StrBufPrintf(Buf, "%ld", SortOrder);
2773 putbstr("SortOrder", Buf);
2775 switch (SortOrder) {
2780 return SortBy->Forward;
2782 return SortBy->Reverse;
2794 ConstStr SortIcons[] = {
2795 {HKEY("static/sort_none.gif")},
2796 {HKEY("static/up_pointer.gif")},
2797 {HKEY("static/down_pointer.gif")},
2800 ConstStr SortNextOrder[] = {
2807 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2809 int bSortError = eNOT_SPECIFIED;
2810 const StrBuf *BSort;
2815 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2817 return eNO_SUCH_SORT;
2818 *Param = (SortStruct*) vSort;
2821 if (havebstr("SortBy")) {
2822 BSort = sbstr("SortBy");
2823 bSortError = eINVALID_PARAM;
2824 if ((*Param)->PrefPrepend == NULL) {
2825 set_room_pref("sort", NewStrBufDup(BSort), 0);
2828 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2831 else { /** Try to fallback to our remembered values... */
2832 if ((*Param)->PrefPrepend == NULL) {
2833 BSort = get_room_pref("sort");
2836 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2840 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2844 *Next = (SortStruct*) vSort;
2846 /** Ok, its us, lets see in which direction we should sort... */
2847 if (havebstr("SortOrder")) {
2848 *SortOrder = LBSTR("SortOrder");
2850 else { /** Try to fallback to our remembered values... */
2851 if ((*Param)->PrefPrepend == NULL) {
2852 *SortOrder = StrTol(get_room_pref("SortOrder"));
2855 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2865 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2870 const ConstStr *SortIcon;
2872 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2875 Target, "Sorter", ERR_PARM1, TP,
2876 " Sorter [%s] unknown!",
2877 TP->Tokens->Params[0]->Start);
2879 case eINVALID_PARAM:
2880 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2881 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2883 case eNOT_SPECIFIED:
2885 if (Next == Param) {
2886 SortIcon = &SortIcons[SortOrder];
2888 else { /** Not Us... */
2889 SortIcon = &SortIcons[0];
2891 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
2895 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
2901 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2904 Target, "Sorter", ERR_PARM1, TP,
2905 " Sorter [%s] unknown!",
2906 TP->Tokens->Params[0]->Start);
2908 case eINVALID_PARAM:
2910 NULL, "Sorter", ERR_PARM1, TP,
2911 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2913 case eNOT_SPECIFIED:
2915 StrBufAppendBuf(Target, Param->Name, 0);
2920 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
2923 const ConstStr *SortOrderStr;
2927 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2930 Target, "Sorter", ERR_PARM1, TP,
2931 " Sorter [%s] unknown!",
2932 TP->Tokens->Params[0]->Start);
2934 case eINVALID_PARAM:
2936 NULL, "Sorter", ERR_PARM1, TP,
2937 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2939 case eNOT_SPECIFIED:
2941 if (Next == Param) {
2942 SortOrderStr = &SortNextOrder[SortOrder];
2944 else { /** Not Us... */
2945 SortOrderStr = &SortNextOrder[0];
2947 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2952 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
2954 long *LongVector = (long*) CTX;
2956 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
2957 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
2959 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
2963 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
2965 Target, "Longvector", ERR_NAME, TP,
2966 "needs a numerical Parameter!");
2970 Target, "LongVector", ERR_PARM1, TP,
2971 "doesn't have %ld Parameters, its just the size of %ld!",
2972 TP->Tokens->Params[0]->lvalue,
2978 void dbg_print_longvector(long *LongVector)
2980 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
2981 int nItems = LongVector[0];
2984 for (i = 0; i < nItems; i++) {
2986 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
2988 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
2991 lprintf(1, ChrPtr(Buf));
2995 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
2997 long *LongVector = (long*) CTX;
2999 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
3000 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
3001 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
3002 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
3004 return LongVector[TP->Tokens->Params[2]->lvalue] ==
3005 LongVector[TP->Tokens->Params[3]->lvalue];
3009 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
3010 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
3012 Target, "ConditionalLongvector", ERR_PARM1, TP,
3013 "needs two long Parameter!");
3017 Target, "Longvector", ERR_PARM1, TP,
3018 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
3019 TP->Tokens->Params[2]->lvalue,
3020 TP->Tokens->Params[3]->lvalue,
3028 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
3030 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
3037 memset(&NoCtx, 0, sizeof(WCTemplputParams));
3038 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
3039 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
3040 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
3041 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
3042 RegisterNamespace("CONTEXTSTRARR", 1, 2, tmplput_ContextStringArray, NULL, CTX_STRBUF);
3043 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
3044 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
3045 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, preeval_do_tabbed, CTX_NONE);
3046 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
3049 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
3050 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
3051 RegisterConditional(HKEY("COND:CONTEXTSTRARR"), 4, ConditionalContextStrinArray, CTX_STRBUFARR);
3052 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
3054 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
3055 conditional_ITERATE_ISGROUPCHANGE,
3057 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
3058 conditional_ITERATE_LASTN,
3060 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
3061 conditional_ITERATE_FIRSTN,
3064 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
3065 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
3066 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
3067 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
3068 RegisterNamespace("DEF:STR", 1, 1, tmplput_DefStr, NULL, CTX_NONE);
3069 RegisterNamespace("DEF:VAL", 1, 1, tmplput_DefVal, NULL, CTX_NONE);
3077 ServerStartModule_SUBST
3080 WirelessTemplateCache = NewHash(1, NULL);
3081 WirelessLocalTemplateCache = NewHash(1, NULL);
3082 LocalTemplateCache = NewHash(1, NULL);
3083 TemplateCache = NewHash(1, NULL);
3085 GlobalNS = NewHash(1, NULL);
3086 Iterators = NewHash(1, NULL);
3087 Conditionals = NewHash(1, NULL);
3088 SortHash = NewHash(1, NULL);
3089 Defines = NewHash(1, NULL);
3093 FinalizeModule_SUBST
3100 ServerShutdownModule_SUBST
3103 DeleteHash(&WirelessTemplateCache);
3104 DeleteHash(&WirelessLocalTemplateCache);
3105 DeleteHash(&TemplateCache);
3106 DeleteHash(&LocalTemplateCache);
3108 DeleteHash(&GlobalNS);
3109 DeleteHash(&Iterators);
3110 DeleteHash(&Conditionals);
3111 DeleteHash(&SortHash);
3112 DeleteHash(&Defines);
3117 SessionNewModule_SUBST
3124 SessionAttachModule_SUBST
3127 sess->vars = NewHash(1,NULL);
3131 SessionDetachModule_SUBST
3134 DeleteHash(&sess->vars);
3135 FreeStrBuf(&sess->WFBuf);
3139 SessionDestroyModule_SUBST