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(StrBuf *filename, StrBuf *Key, HashList *PutThere);
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*));
1105 Template->TokenSpace = 10;
1108 WCTemplateToken **NewTokens;
1109 NewTokens= (WCTemplateToken**)malloc(
1110 sizeof(WCTemplateToken*) *
1111 Template->TokenSpace * 2);
1112 memcpy(NewTokens, Template->Tokens,
1113 sizeof(WCTemplateToken*) * Template->nTokensUsed);
1114 free(Template->Tokens);
1115 Template->TokenSpace *= 2;
1116 Template->Tokens = NewTokens;
1119 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
1122 TemplateParam *GetNextParameter(StrBuf *Buf,
1125 WCTemplateToken *Tokens,
1127 WCTemplputParams *TP)
1129 const char *pch = *pCh;
1130 const char *pchs, *pche;
1131 TemplateParam *Parm;
1135 Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
1136 memset(Parm, 0, sizeof(TemplateParam));
1137 Parm->Type = TYPE_STR;
1139 /* Skip leading whitespaces */
1140 while ((*pch == ' ' )||
1143 (*pch == '\n')) pch ++;
1146 Parm->Type = TYPE_PREFSTR;
1153 else if (*pch == ';') {
1154 Parm->Type = TYPE_PREFINT;
1161 else if (*pch == '#') {
1162 Parm->Type = TYPE_INTDEFINE;
1165 else if (*pch == '_') {
1166 Parm->Type = TYPE_GETTEXT;
1173 else if (*pch == 'B') {
1174 Parm->Type = TYPE_BSTR;
1181 else if (*pch == '=') {
1182 Parm->Type = TYPE_SUBTEMPLATE;
1193 else if (*pch == '\'')
1195 if (quote != '\0') {
1200 ( (pch > pchs) && (*(pch - 1) == '\\'))
1205 if (*pch != quote) {
1206 lprintf(1, "Error (in '%s' line %ld); "
1207 "evaluating template param [%s] in Token [%s]\n",
1208 ChrPtr(pTmpl->FileName),
1210 ChrPtr(Tokens->FlatToken),
1217 StrBufPeek(Buf, pch, -1, '\0');
1218 if (LoadTemplates > 1) {
1219 lprintf(1, "DBG: got param [%s] %ld %ld\n",
1220 pchs, pche - pchs, strlen(pchs));
1223 Parm->len = pche - pchs;
1224 pch ++; /* move after trailing quote */
1225 if (ParamBrace && (*pch == ')')) {
1232 Parm->Type = TYPE_LONG;
1234 while ((pch <= pe) &&
1240 if (pch - pchs > 1){
1241 StrBufPeek(Buf, pch, -1, '\0');
1242 Parm->lvalue = atol(pchs);
1249 lprintf(1, "Error (in '%s' line %ld); "
1250 "evaluating long template param [%s] in Token [%s]\n",
1251 ChrPtr(pTmpl->FileName),
1253 ChrPtr(Tokens->FlatToken),
1260 while ((*pch == ' ' )||
1264 (*pch == '\n')) pch ++;
1266 if (DumpTemplateI18NStrings && (Parm->Type == TYPE_GETTEXT)) {
1267 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
1269 if (Parm->Type == TYPE_INTDEFINE)
1273 if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
1277 PVal = (long*) vPVal;
1279 Parm->lvalue = *PVal;
1283 LogTemplateError(NULL, "Define", ERR_PARM1, TP,
1292 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
1294 const char *pTmplStart,
1295 const char *pTmplEnd,
1301 TemplateParam *Param;
1302 WCTemplateToken *NewToken;
1303 WCTemplputParams TP;
1305 NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
1306 memset(NewToken, 0, sizeof(WCTemplateToken));
1307 TP.Tokens = NewToken;
1308 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
1309 NewToken->Flags = 0;
1310 NewToken->Line = Line + 1;
1311 NewToken->pTokenStart = pTmplStart;
1312 NewToken->TokenStart = pTmplStart - pStart;
1313 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
1314 NewToken->pTokenEnd = pTmplEnd;
1315 NewToken->NameEnd = NewToken->TokenEnd - 2;
1316 NewToken->PreEval = NULL;
1317 NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
1318 StrBufShrinkToFit(NewToken->FlatToken, 1);
1320 StrBufPeek(Buf, pTmplStart, + 1, '\0');
1321 StrBufPeek(Buf, pTmplEnd, -1, '\0');
1322 pch = NewToken->pName = pTmplStart + 2;
1324 NewToken->HaveParameters = 0;;
1325 NewToken->nParameters = 0;
1327 while (pch < pTmplEnd - 1) {
1329 StrBufPeek(Buf, pch, -1, '\0');
1330 NewToken->NameEnd = pch - NewToken->pName;
1332 if (*(pTmplEnd - 1) != ')') {
1334 NULL, "Parseerror", ERR_NAME, &TP,
1335 "Warning, Non welformed Token; missing right parenthesis");
1337 while (pch < pTmplEnd - 1) {
1338 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl, &TP);
1339 if (Param != NULL) {
1340 NewToken->HaveParameters = 1;
1341 if (NewToken->nParameters > MAXPARAM) {
1343 NULL, "Parseerror", ERR_NAME, &TP,
1344 "only [%d] Params allowed in Tokens",
1348 FreeToken(&NewToken);
1351 NewToken->Params[NewToken->nParameters++] = Param;
1355 if((NewToken->NameEnd == 1) &&
1356 (NewToken->HaveParameters == 1))
1359 if (*(NewToken->pName) == '_')
1360 NewToken->Flags = SV_GETTEXT;
1361 else if (*(NewToken->pName) == '=')
1362 NewToken->Flags = SV_SUBTEMPL;
1363 else if (*(NewToken->pName) == '%')
1364 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
1365 else if (*(NewToken->pName) == '?')
1366 NewToken->Flags = SV_CONDITIONAL;
1367 else if (*(NewToken->pName) == '!')
1368 NewToken->Flags = SV_NEG_CONDITIONAL;
1374 switch (NewToken->Flags) {
1376 /* If we're able to find out more about the token, do it now while its fresh. */
1377 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1378 HashHandler *Handler;
1379 Handler = (HashHandler*) vVar;
1380 if ((NewToken->nParameters < Handler->Filter.nMinArgs) ||
1381 (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
1383 NULL, "Token", ERR_NAME, &TP,
1384 "doesn't work with %d params",
1385 NewToken->nParameters);
1389 NewToken->PreEval = Handler;
1390 NewToken->Flags = SV_PREEVALUATED;
1391 if (Handler->PreEvalFunc != NULL)
1392 Handler->PreEvalFunc(NewToken);
1397 if (NewToken->nParameters !=1) {
1399 NULL, "Gettext", ERR_NAME, &TP,
1400 "requires exactly 1 parameter, you gave %d params",
1401 NewToken->nParameters);
1402 NewToken->Flags = 0;
1405 if (DumpTemplateI18NStrings) {
1406 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
1410 if (NewToken->nParameters != 1) {
1412 NULL, "Subtemplates", ERR_NAME, &TP,
1413 "require exactly 1 parameter, you gave %d params",
1414 NewToken->nParameters);
1418 case SV_CUST_STR_CONDITIONAL:
1419 case SV_CONDITIONAL:
1420 case SV_NEG_CONDITIONAL:
1421 if (NewToken->nParameters <2) {
1423 NULL, "Conditional", ERR_PARM1, &TP,
1424 "require at least 2 parameters, you gave %d params",
1425 NewToken->nParameters);
1426 NewToken->Flags = 0;
1429 if (NewToken->Params[1]->lvalue == 0) {
1431 NULL, "Conditional", ERR_PARM1, &TP,
1432 "Conditional ID (Parameter 1) mustn't be 0!");
1433 NewToken->Flags = 0;
1436 if (!GetHash(Conditionals,
1437 NewToken->Params[0]->Start,
1438 NewToken->Params[0]->len,
1441 if ((NewToken->Params[0]->len == 1) &&
1442 (NewToken->Params[0]->Start[0] == 'X'))
1445 NULL, "Conditional", ERR_PARM1, &TP,
1448 NewToken->Error = NewStrBuf();
1451 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1452 NewToken->Params[0]->Start,
1453 ChrPtr(pTmpl->FileName),
1455 ChrPtr(NewToken->FlatToken));
1459 NewToken->PreEval = vVar;
1471 * \brief Display a variable-substituted template
1472 * \param templatename template file to load
1474 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1476 WCTemplate *NewTemplate;
1478 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1479 memset(NewTemplate, 0, sizeof(WCTemplate));
1480 NewTemplate->Data = NULL;
1481 NewTemplate->FileName = NewStrBufDup(filename);
1482 StrBufShrinkToFit(NewTemplate->FileName, 1);
1483 NewTemplate->nTokensUsed = 0;
1484 NewTemplate->TokenSpace = 0;
1485 NewTemplate->Tokens = NULL;
1486 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1487 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1488 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1491 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1496 * \brief Display a variable-substituted template
1497 * \param templatename template file to load
1499 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1502 struct stat statbuf;
1503 const char *pS, *pE, *pch, *Err;
1506 WCTemplate *NewTemplate;
1508 fd = open(ChrPtr(filename), O_RDONLY);
1510 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1511 ChrPtr(filename), strerror(errno));
1515 if (fstat(fd, &statbuf) == -1) {
1516 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1517 ChrPtr(filename), strerror(errno));
1521 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1522 memset(NewTemplate, 0, sizeof(WCTemplate));
1523 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1524 NewTemplate->FileName = NewStrBufDup(filename);
1525 NewTemplate->nTokensUsed = 0;
1526 NewTemplate->TokenSpace = 0;
1527 NewTemplate->Tokens = NULL;
1528 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1529 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1530 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1533 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1535 FreeWCTemplate(NewTemplate);
1536 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1537 ChrPtr(filename), strerror(errno));
1543 StrBufShrinkToFit(NewTemplate->Data, 1);
1544 StrBufShrinkToFit(NewTemplate->MimeType, 1);
1545 pS = pch = ChrPtr(NewTemplate->Data);
1546 pE = pS + StrLength(NewTemplate->Data);
1548 const char *pts, *pte;
1550 int InDoubleQuotes = 0;
1552 /** Find one <? > */
1554 for (; pch < pE; pch ++) {
1555 if ((*pch=='<')&&(*(pch + 1)=='?') &&
1556 !((pch == pS) && /* we must ommit a <?xml */
1557 (*(pch + 2) == 'x') &&
1558 (*(pch + 3) == 'm') &&
1559 (*(pch + 4) == 'l')))
1561 if (*pch=='\n') Line ++;
1567 /** Found one? parse it. */
1568 for (; pch <= pE - 1; pch ++) {
1570 InDoubleQuotes = ! InDoubleQuotes;
1571 else if (*pch == '\'')
1572 InQuotes = ! InQuotes;
1573 else if ((!InQuotes && !InDoubleQuotes) &&
1574 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1582 PutNewToken(NewTemplate,
1583 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1586 if (LoadTemplates == 0)
1587 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1592 const char* PrintTemplate(void *vSubst)
1594 WCTemplate *Tmpl = vSubst;
1596 return ChrPtr(Tmpl->FileName);
1600 int LoadTemplateDir(const StrBuf *DirName, HashList *wireless, HashList *big, const StrBuf *BaseKey)
1606 StrBuf *SubDirectory;
1607 DIR *filedir = NULL;
1608 struct dirent *filedir_entry;
1615 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
1620 filedir = opendir (ChrPtr(DirName));
1621 if (filedir == NULL) {
1626 Toplevel = StrLength(BaseKey) == 0;
1627 SubDirectory = NewStrBuf();
1628 SubKey = NewStrBuf();
1629 FileName = NewStrBufPlain(NULL, PATH_MAX);
1631 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
1632 (filedir_entry != NULL))
1636 #ifdef _DIRENT_HAVE_D_NAMELEN
1637 d_namelen = filedir_entry->d_namelen;
1638 d_type = filedir_entry->d_type;
1642 #define DT_UNKNOWN 0
1647 #define IFTODT(mode) (((mode) & 0170000) >> 12)
1648 #define DTTOIF(dirtype) ((dirtype) << 12)
1650 d_namelen = strlen(filedir_entry->d_name);
1651 d_type = DT_UNKNOWN;
1653 d_without_ext = d_namelen;
1655 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1656 continue; /* Ignore backup files... */
1658 if ((d_namelen == 1) &&
1659 (filedir_entry->d_name[0] == '.'))
1662 if ((d_namelen == 2) &&
1663 (filedir_entry->d_name[0] == '.') &&
1664 (filedir_entry->d_name[1] == '.'))
1667 if (d_type == DT_UNKNOWN) {
1669 char path[PATH_MAX];
1670 snprintf(path, PATH_MAX, "%s/%s",
1671 ChrPtr(DirName), filedir_entry->d_name);
1672 if (stat(path, &s) == 0) {
1673 d_type = IFTODT(s.st_mode);
1679 /* Skip directories we are not interested in... */
1680 if (strcmp(filedir_entry->d_name, ".svn") == 0)
1683 FlushStrBuf(SubKey);
1685 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1686 StrBufAppendBuf(SubKey, BaseKey, 0);
1687 StrBufAppendBufPlain(SubKey, HKEY("_"), 0);
1689 StrBufAppendBufPlain(SubKey, filedir_entry->d_name, d_namelen, 0);
1691 FlushStrBuf(SubDirectory);
1692 StrBufAppendBuf(SubDirectory, DirName, 0);
1693 if (ChrPtr(SubDirectory)[StrLength(SubDirectory) - 1] != '/')
1694 StrBufAppendBufPlain(SubDirectory, HKEY("/"), 0);
1695 StrBufAppendBufPlain(SubDirectory, filedir_entry->d_name, d_namelen, 0);
1697 LoadTemplateDir(SubDirectory, wireless, big, SubKey);
1700 case DT_LNK: /* TODO: check whether its a file or a directory */
1704 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1706 if ((d_without_ext == 0) || (d_namelen < 3))
1708 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1709 continue; /* Ignore backup files... */
1710 /* .m.xxx is for mobile useragents! */
1711 if (d_without_ext > 2)
1712 IsMobile = (filedir_entry->d_name[d_without_ext - 1] == 'm') &&
1713 (filedir_entry->d_name[d_without_ext - 2] == '.');
1714 PStart = filedir_entry->d_name;
1715 StrBufPrintf(FileName, "%s/%s", ChrPtr(DirName), filedir_entry->d_name);
1716 MinorPtr = strchr(filedir_entry->d_name, '.');
1717 if (MinorPtr != NULL)
1721 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1722 StrBufAppendBuf(Key, BaseKey, 0);
1723 StrBufAppendBufPlain(Key, HKEY("_"), 0);
1725 StrBufAppendBufPlain(Key, filedir_entry->d_name, MinorPtr - filedir_entry->d_name, 0);
1727 if (LoadTemplates >= 1)
1728 lprintf(1, "%s %d %s\n", ChrPtr(FileName), IsMobile, ChrPtr(Key));
1729 if (LoadTemplates == 0)
1730 load_template(FileName, Key, (IsMobile)?wireless:big);
1732 prepare_template(FileName, Key, (IsMobile)?wireless:big);
1739 FreeStrBuf(&FileName);
1741 FreeStrBuf(&SubDirectory);
1742 FreeStrBuf(&SubKey);
1746 void InitTemplateCache(void)
1754 /* Primary Template set... */
1755 StrBufPrintf(Dir, "%s/t", static_dirs[0]);
1756 LoadTemplateDir(Dir,
1757 WirelessTemplateCache,
1761 /* User local Template set */
1762 StrBufPrintf(Dir, "%s/t", static_dirs[1]);
1763 LoadTemplateDir(Dir,
1764 WirelessLocalTemplateCache,
1768 /* Debug Templates, just to be loaded while debugging. */
1770 StrBufPrintf(Dir, "%s/dbg", static_dirs[0]);
1771 LoadTemplateDir(Dir,
1772 WirelessTemplateCache,
1783 /*-----------------------------------------------------------------------------
1784 * Filling & processing Templates
1787 * \brief executes one token
1788 * \param Target buffer to append to
1789 * \param Token da to process.
1790 * \param Template we're iterating
1791 * \param Context Contextpoointer to pass in
1792 * \param state are we in conditional state?
1793 * \param ContextType what type of information does context giv us?
1795 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1797 const char *AppendMe;
1799 HashHandler *Handler;
1802 /* much output, since pName is not terminated...
1803 lprintf(1,"Doing token: %s\n",Token->pName);
1806 switch (TP->Tokens->Flags) {
1808 TmplGettext(Target, TP);
1810 case SV_CONDITIONAL: /** Forward conditional evaluation */
1811 return EvaluateConditional(Target, 1, state, TP);
1813 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1814 return EvaluateConditional(Target, 0, state, TP);
1816 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1817 if (TP->Tokens->nParameters >= 6) {
1818 if (EvaluateConditional(Target, 0, state, TP)) {
1819 GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
1820 StrBufAppendBufPlain(Target,
1826 GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
1827 StrBufAppendBufPlain(Target,
1835 Target, "Conditional", ERR_NAME, TP,
1836 "needs at least 6 Params!");
1840 if (TP->Tokens->nParameters == 1)
1841 DoTemplate(TKEY(0), Target, TP);
1843 case SV_PREEVALUATED:
1844 Handler = (HashHandler*) TP->Tokens->PreEval;
1845 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1848 Handler->HandlerFunc(Target, TP);
1851 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
1852 Handler = (HashHandler*) vVar;
1853 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1857 Handler->HandlerFunc(Target, TP);
1861 print_value_of(Target, TP);
1869 const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
1871 WCTemplate *pTmpl = Tmpl;
1874 const char *pData, *pS;
1876 WCTemplputParams TP;
1878 memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
1880 TP.Context = CallingTP->Context;
1881 TP.ControlContext = CallingTP->ControlContext;
1883 if (LoadTemplates != 0) {
1884 if (LoadTemplates > 1)
1885 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1886 ChrPtr(Tmpl->FileName));
1888 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
1892 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1893 ChrPtr(Tmpl->FileName));
1899 pS = pData = ChrPtr(pTmpl->Data);
1900 len = StrLength(pTmpl->Data);
1904 if (i >= pTmpl->nTokensUsed) {
1905 StrBufAppendBufPlain(Target,
1907 len - (pData - pS), 0);
1911 StrBufAppendBufPlain(
1913 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1914 TP.Tokens = pTmpl->Tokens[i];
1915 TP.nArgs = pTmpl->Tokens[i]->nParameters;
1916 state = EvaluateToken(Target, state, &TP);
1918 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1919 /* condition told us to skip till its end condition */
1921 TP.Tokens = pTmpl->Tokens[i];
1922 TP.nArgs = pTmpl->Tokens[i]->nParameters;
1923 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1924 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1925 if (state == EvaluateConditional(
1927 pTmpl->Tokens[i]->Flags,
1933 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1934 if (i > pTmpl->nTokensUsed)
1938 if (LoadTemplates != 0) {
1939 FreeWCTemplate(pTmpl);
1941 return Tmpl->MimeType;
1946 * \brief Display a variable-substituted template
1947 * \param templatename template file to load
1948 * \returns the mimetype of the template its doing
1950 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
1952 WCTemplputParams LocalTP;
1954 HashList *StaticLocal;
1960 memset(&LocalTP, 0, sizeof(WCTemplputParams));
1964 if (WC->is_mobile > 0) {
1965 Static = WirelessTemplateCache;
1966 StaticLocal = WirelessLocalTemplateCache;
1969 Static = TemplateCache;
1970 StaticLocal = LocalTemplateCache;
1975 lprintf (1, "Can't to load a template with empty name!\n");
1976 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1980 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1981 !GetHash(Static, templatename, len, &vTmpl)) {
1982 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1983 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1985 (long)strlen(templatename));
1987 dbg_PrintHash(Static, PrintTemplate, NULL);
1988 PrintHash(Static, VarPrintTransition, PrintTemplate);
1994 return ProcessTemplate(vTmpl, Target, TP);
1998 /*-----------------------------------------------------------------------------
2001 typedef struct _HashIterator {
2002 HashList *StaticList;
2003 int AdditionalParams;
2005 int XPectContextType;
2007 RetrieveHashlistFunc GetHash;
2008 HashDestructorFunc Destructor;
2009 SubTemplFunc DoSubTemplate;
2012 void RegisterITERATOR(const char *Name, long len,
2013 int AdditionalParams,
2014 HashList *StaticList,
2015 RetrieveHashlistFunc GetHash,
2016 SubTemplFunc DoSubTempl,
2017 HashDestructorFunc Destructor,
2019 int XPectContextType,
2024 It = (HashIterator*)malloc(sizeof(HashIterator));
2025 memset(It, 0, sizeof(HashIterator));
2026 It->StaticList = StaticList;
2027 It->AdditionalParams = AdditionalParams;
2028 It->GetHash = GetHash;
2029 It->DoSubTemplate = DoSubTempl;
2030 It->Destructor = Destructor;
2031 It->ContextType = ContextType;
2032 It->XPectContextType = XPectContextType;
2034 Put(Iterators, Name, len, It, NULL);
2037 typedef struct _iteratestruct {
2046 int preeval_iterate(WCTemplateToken *Token)
2048 WCTemplputParams TPP;
2049 WCTemplputParams *TP;
2052 memset(&TPP, 0, sizeof(WCTemplputParams));
2055 if (!GetHash(Iterators, TKEY(0), &vIt)) {
2057 NULL, "Iterator", ERR_PARM1, TP,
2061 Token->Preeval2 = vIt;
2065 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
2070 SortStruct *SortBy = NULL;
2072 int DetectGroupChange = 0;
2075 void *vLastContext = NULL;
2077 WCTemplputParams SubTP;
2078 IterateStruct Status;
2084 memset(&Status, 0, sizeof(IterateStruct));
2085 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2087 It = (HashIterator*) TP->Tokens->Preeval2;
2090 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
2094 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2096 Target, "Iterator", ERR_PARM1, TP,
2097 "doesn't work with %d params",
2098 TP->Tokens->nParameters);
2102 if ((It->XPectContextType != CTX_NONE) &&
2103 (It->XPectContextType != TP->Filter.ContextType)) {
2105 Target, "Iterator", ERR_PARM1, TP,
2106 "requires context of type %d, have %d",
2107 It->XPectContextType,
2108 TP->Filter.ContextType);
2113 if (It->StaticList == NULL)
2114 List = It->GetHash(Target, TP);
2116 List = It->StaticList;
2118 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2119 if (DetectGroupChange) {
2120 const StrBuf *BSort;
2121 DetectGroupChange = 0;
2122 if (havebstr("SortBy")) {
2123 BSort = sbstr("SortBy");
2124 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2125 (vSortBy != NULL)) {
2126 SortBy = (SortStruct*)vSortBy;
2127 /** Ok, its us, lets see in which direction we should sort... */
2128 if (havebstr("SortOrder")) {
2130 SortOrder = LBSTR("SortOrder");
2132 DetectGroupChange = 1;
2137 nMembersUsed = GetCount(List);
2138 SubBuf = NewStrBuf();
2139 SubTP.Filter.ContextType = It->ContextType;
2140 SubTP.Filter.ControlContextType = CTX_ITERATE;
2141 SubTP.ControlContext = &Status;
2143 if (HAVE_PARAM(2)) {
2144 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2146 if (HAVE_PARAM(3)) {
2147 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2149 if (HAVE_PARAM(4)) {
2150 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2152 it = GetNewHashPos(List, StepWidth);
2154 StopAt = GetCount(List);
2156 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2157 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2158 if (DetectGroupChange && Status.n > 0) {
2159 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2161 Status.LastN = (Status.n + 1) == nMembersUsed;
2162 SubTP.Context = vContext;
2163 if (It->DoSubTemplate != NULL)
2164 It->DoSubTemplate(SubBuf, &SubTP);
2165 DoTemplate(TKEY(1), SubBuf, &SubTP);
2167 StrBufAppendBuf(Target, SubBuf, 0);
2168 FlushStrBuf(SubBuf);
2169 Status.oddeven = ! Status.oddeven;
2170 vLastContext = vContext;
2174 FreeStrBuf(&SubBuf);
2176 if (It->Destructor != NULL)
2177 It->Destructor(&List);
2181 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2183 IterateStruct *Ctx = CCTX;
2184 if (TP->Tokens->nParameters < 3)
2185 return Ctx->GroupChange;
2187 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2190 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2192 IterateStruct *Ctx = CCTX;
2194 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2196 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2200 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2202 IterateStruct *Ctx = CCTX;
2204 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2208 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2210 IterateStruct *Ctx = CCTX;
2211 StrBufAppendPrintf(Target, "%d", Ctx->n);
2214 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2216 IterateStruct *Ctx = CCTX;
2220 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2222 IterateStruct *Ctx = CCTX;
2228 /*-----------------------------------------------------------------------------
2231 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2233 ConditionalStruct *Cond;
2235 if ((TP->Tokens->Params[0]->len == 1) &&
2236 (TP->Tokens->Params[0]->Start[0] == 'X'))
2237 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2239 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2242 Target, "Conditional", ERR_PARM1, TP,
2247 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2251 if (Cond->CondF(Target, TP) == Neg)
2252 return TP->Tokens->Params[1]->lvalue;
2256 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2261 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2263 subst = (wcsubst*) vsubst;
2265 switch(subst->wcs_type) {
2267 return (subst->wcs_function!=NULL);
2269 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
2273 case WCS_STRBUF_REF:
2274 if (TP->Tokens->nParameters < 4)
2276 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2278 if (TP->Tokens->nParameters < 4)
2279 return (subst->lvalue != 0);
2280 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2282 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2288 void RegisterConditional(const char *Name, long len,
2290 WCConditionalFunc CondF,
2291 int ContextRequired)
2293 ConditionalStruct *Cond;
2295 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2296 memset(Cond, 0, sizeof(ConditionalStruct));
2297 Cond->PlainName = Name;
2298 Cond->Filter.nMaxArgs = nParams;
2299 Cond->Filter.nMinArgs = nParams;
2300 Cond->CondF = CondF;
2301 Cond->Filter.ContextType = ContextRequired;
2302 Cond->Filter.ControlContextType = CTX_NONE;
2303 Put(Conditionals, Name, len, Cond, NULL);
2306 void RegisterControlConditional(const char *Name, long len,
2308 WCConditionalFunc CondF,
2309 int ControlContextRequired)
2311 ConditionalStruct *Cond;
2313 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2314 memset(Cond, 0, sizeof(ConditionalStruct));
2315 Cond->PlainName = Name;
2316 Cond->Filter.nMaxArgs = nParams;
2317 Cond->Filter.nMinArgs = nParams;
2318 Cond->CondF = CondF;
2319 Cond->Filter.ContextType = CTX_NONE;
2320 Cond->Filter.ControlContextType = ControlContextRequired;
2321 Put(Conditionals, Name, len, Cond, NULL);
2324 void RegisterTokenParamDefine(const char *Name, long len,
2329 PVal = (long*)malloc(sizeof(long));
2331 Put(Defines, Name, len, PVal, NULL);
2336 /*-----------------------------------------------------------------------------
2339 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2341 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2343 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2345 StrBuf *TokenText = (StrBuf*) CTX;
2346 const char *CompareToken;
2349 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2350 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2353 /*-----------------------------------------------------------------------------
2357 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2359 WCTemplputParams SubTP;
2361 StrBuf *Headline = NULL;
2362 if (TP->Tokens->nParameters == 2) {
2363 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2364 Headline = NewStrBuf();
2365 DoTemplate(TKEY(1), Headline, TP);
2370 GetTemplateTokenString(Target,
2375 Headline = NewStrBufPlain(Ch, len);
2378 /* else TODO error? logging? */
2379 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2380 SubTP.Context = Headline;
2381 SubTP.Filter.ContextType = CTX_STRBUF;
2382 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2383 DoTemplate(TKEY(0), Target, TP);
2384 DoTemplate(HKEY("endbox"), Target, TP);
2385 FreeStrBuf(&Headline);
2388 /*-----------------------------------------------------------------------------
2392 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2395 int i, ntabs, nTabs;
2397 nTabs = ntabs = TP->Tokens->nParameters / 2;
2398 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2399 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2401 for (i = 0; i < ntabs; i++) {
2402 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2403 (TP->Tokens->Params[i * 2]->len > 0)) {
2404 TabNames[i] = NewStrBuf();
2405 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2407 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2410 GetTemplateTokenString(Target,
2415 TabNames[i] = NewStrBufPlain(Ch, -1);
2418 /** A Tab without subject? we can't count that, add it as silent */
2423 StrTabbedDialog(Target, nTabs, TabNames);
2424 for (i = 0; i < ntabs; i++) {
2425 StrBeginTab(Target, i, nTabs);
2426 DoTemplate(TKEY(i * 2 + 1), Target, TP);
2427 StrEndTab(Target, i, nTabs);
2432 /*-----------------------------------------------------------------------------
2437 void RegisterSortFunc(const char *name, long len,
2438 const char *prepend, long preplen,
2439 CompareFunc Forward,
2440 CompareFunc Reverse,
2441 CompareFunc GroupChange,
2444 SortStruct *NewSort;
2446 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2447 memset(NewSort, 0, sizeof(SortStruct));
2448 NewSort->Name = NewStrBufPlain(name, len);
2449 if (prepend != NULL)
2450 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2452 NewSort->PrefPrepend = NULL;
2453 NewSort->Forward = Forward;
2454 NewSort->Reverse = Reverse;
2455 NewSort->GroupChange = GroupChange;
2456 NewSort->ContextType = ContextType;
2457 if (ContextType == CTX_NONE) {
2458 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2462 Put(SortHash, name, len, NewSort, DestroySortStruct);
2465 CompareFunc RetrieveSort(WCTemplputParams *TP,
2466 const char *OtherPrefix, long OtherPrefixLen,
2467 const char *Default, long ldefault, long DefaultDirection)
2470 const StrBuf *BSort = NULL;
2473 long SortOrder = -1;
2475 if (havebstr("SortBy")) {
2476 BSort = sbstr("SortBy");
2477 if (OtherPrefix == NULL) {
2478 set_room_pref("sort", NewStrBufDup(BSort), 0);
2481 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2484 else { /** Try to fallback to our remembered values... */
2485 if (OtherPrefix == NULL) {
2486 BSort = get_room_pref("sort");
2489 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2492 putbstr("SortBy", NewStrBufDup(BSort));
2496 BSort = Buf = NewStrBufPlain(Default, ldefault);
2497 putbstr("SortBy", Buf);
2501 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2502 (vSortBy == NULL)) {
2504 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2505 (vSortBy == NULL)) {
2507 NULL, "Sorting", ERR_PARM1, TP,
2508 "Illegal default sort: [%s]", Default);
2512 SortBy = (SortStruct*)vSortBy;
2514 if (SortBy->ContextType != TP->Filter.ContextType)
2517 /** Ok, its us, lets see in which direction we should sort... */
2518 if (havebstr("SortOrder")) {
2519 SortOrder = LBSTR("SortOrder");
2521 else { /** Try to fallback to our remembered values... */
2523 if (SortBy->PrefPrepend == NULL) {
2524 Buf = get_room_pref("SortOrder");
2525 SortOrder = StrTol(Buf);
2528 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2532 SortOrder = DefaultDirection;
2534 Buf = NewStrBufPlain(NULL, 64);
2535 StrBufPrintf(Buf, "%ld", SortOrder);
2536 putbstr("SortOrder", Buf);
2538 switch (SortOrder) {
2543 return SortBy->Forward;
2545 return SortBy->Reverse;
2557 ConstStr SortIcons[] = {
2558 {HKEY("static/sort_none.gif")},
2559 {HKEY("static/up_pointer.gif")},
2560 {HKEY("static/down_pointer.gif")},
2563 ConstStr SortNextOrder[] = {
2570 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2572 int bSortError = eNOT_SPECIFIED;
2573 const StrBuf *BSort;
2578 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2580 return eNO_SUCH_SORT;
2581 *Param = (SortStruct*) vSort;
2584 if (havebstr("SortBy")) {
2585 BSort = sbstr("SortBy");
2586 bSortError = eINVALID_PARAM;
2587 if ((*Param)->PrefPrepend == NULL) {
2588 set_room_pref("sort", NewStrBufDup(BSort), 0);
2591 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2594 else { /** Try to fallback to our remembered values... */
2595 if ((*Param)->PrefPrepend == NULL) {
2596 BSort = get_room_pref("sort");
2599 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2603 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2607 *Next = (SortStruct*) vSort;
2609 /** Ok, its us, lets see in which direction we should sort... */
2610 if (havebstr("SortOrder")) {
2611 *SortOrder = LBSTR("SortOrder");
2613 else { /** Try to fallback to our remembered values... */
2614 if ((*Param)->PrefPrepend == NULL) {
2615 *SortOrder = StrTol(get_room_pref("SortOrder"));
2618 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2628 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2633 const ConstStr *SortIcon;
2635 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2638 Target, "Sorter", ERR_PARM1, TP,
2639 " Sorter [%s] unknown!",
2640 TP->Tokens->Params[0]->Start);
2642 case eINVALID_PARAM:
2643 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2644 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2646 case eNOT_SPECIFIED:
2648 if (Next == Param) {
2649 SortIcon = &SortIcons[SortOrder];
2651 else { /** Not Us... */
2652 SortIcon = &SortIcons[0];
2654 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
2658 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
2664 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2667 Target, "Sorter", ERR_PARM1, TP,
2668 " Sorter [%s] unknown!",
2669 TP->Tokens->Params[0]->Start);
2671 case eINVALID_PARAM:
2673 NULL, "Sorter", ERR_PARM1, TP,
2674 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2676 case eNOT_SPECIFIED:
2678 StrBufAppendBuf(Target, Param->Name, 0);
2683 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
2686 const ConstStr *SortOrderStr;
2690 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2693 Target, "Sorter", ERR_PARM1, TP,
2694 " Sorter [%s] unknown!",
2695 TP->Tokens->Params[0]->Start);
2697 case eINVALID_PARAM:
2699 NULL, "Sorter", ERR_PARM1, TP,
2700 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2702 case eNOT_SPECIFIED:
2704 if (Next == Param) {
2705 SortOrderStr = &SortNextOrder[SortOrder];
2707 else { /** Not Us... */
2708 SortOrderStr = &SortNextOrder[0];
2710 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2715 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
2717 long *LongVector = (long*) CTX;
2719 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
2720 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
2722 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
2726 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
2728 Target, "Longvector", ERR_NAME, TP,
2729 "needs a numerical Parameter!");
2733 Target, "LongVector", ERR_PARM1, TP,
2734 "doesn't have %ld Parameters, its just the size of %ld!",
2735 TP->Tokens->Params[0]->lvalue,
2741 void dbg_print_longvector(long *LongVector)
2743 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
2744 int nItems = LongVector[0];
2747 for (i = 0; i < nItems; i++) {
2749 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
2751 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
2754 lprintf(1, ChrPtr(Buf));
2758 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
2760 long *LongVector = (long*) CTX;
2762 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
2763 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
2764 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
2765 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
2767 return LongVector[TP->Tokens->Params[2]->lvalue] ==
2768 LongVector[TP->Tokens->Params[3]->lvalue];
2772 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
2773 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
2775 Target, "ConditionalLongvector", ERR_PARM1, TP,
2776 "needs two long Parameter!");
2780 Target, "Longvector", ERR_PARM1, TP,
2781 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
2782 TP->Tokens->Params[2]->lvalue,
2783 TP->Tokens->Params[3]->lvalue,
2791 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
2793 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
2800 memset(&NoCtx, 0, sizeof(WCTemplputParams));
2801 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
2802 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
2803 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
2804 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
2805 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
2806 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
2807 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, NULL, CTX_NONE);
2808 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
2809 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
2810 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
2811 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
2813 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
2814 conditional_ITERATE_ISGROUPCHANGE,
2816 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
2817 conditional_ITERATE_LASTN,
2819 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
2820 conditional_ITERATE_FIRSTN,
2823 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
2824 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
2825 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
2826 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
2832 ServerStartModule_SUBST
2835 WirelessTemplateCache = NewHash(1, NULL);
2836 WirelessLocalTemplateCache = NewHash(1, NULL);
2837 LocalTemplateCache = NewHash(1, NULL);
2838 TemplateCache = NewHash(1, NULL);
2840 GlobalNS = NewHash(1, NULL);
2841 Iterators = NewHash(1, NULL);
2842 Conditionals = NewHash(1, NULL);
2843 SortHash = NewHash(1, NULL);
2844 Defines = NewHash(1, NULL);
2848 FinalizeModule_SUBST
2855 ServerShutdownModule_SUBST
2858 DeleteHash(&WirelessTemplateCache);
2859 DeleteHash(&WirelessLocalTemplateCache);
2860 DeleteHash(&TemplateCache);
2861 DeleteHash(&LocalTemplateCache);
2863 DeleteHash(&GlobalNS);
2864 DeleteHash(&Iterators);
2865 DeleteHash(&Conditionals);
2866 DeleteHash(&SortHash);
2867 DeleteHash(&Defines);
2872 SessionNewModule_SUBST
2879 SessionAttachModule_SUBST
2882 sess->vars = NewHash(1,NULL);
2886 SessionDetachModule_SUBST
2889 DeleteHash(&sess->vars);
2893 SessionDestroyModule_SUBST