5 * \defgroup Subst Variable substitution type stuff
6 * \ingroup CitadelConfig
12 #include <sys/types.h>
18 #define SHOW_ME_VAPPEND_PRINTF
21 #include "webserver.h"
23 extern char *static_dirs[PATH_MAX]; /**< Disk representation */
25 HashList *WirelessTemplateCache;
26 HashList *WirelessLocalTemplateCache;
27 HashList *TemplateCache;
28 HashList *LocalTemplateCache;
32 HashList *Conditionals;
34 int LoadTemplates = 0;
37 #define SV_CONDITIONAL 2
38 #define SV_NEG_CONDITIONAL 3
39 #define SV_CUST_STR_CONDITIONAL 4
41 #define SV_PREEVALUATED 6
43 typedef struct _WCTemplate {
48 WCTemplateToken **Tokens;
51 typedef struct _HashHandler {
55 WCHandlerFunc HandlerFunc;
58 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere);
60 void RegisterNS(const char *NSName,
64 WCHandlerFunc HandlerFunc,
67 HashHandler *NewHandler;
69 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
70 NewHandler->nMinArgs = nMinArgs;
71 NewHandler->nMaxArgs = nMaxArgs;
72 NewHandler->HandlerFunc = HandlerFunc;
73 NewHandler->ContextRequired = ContextRequired;
74 Put(GlobalNS, NSName, len, NewHandler, NULL);
77 void FreeToken(WCTemplateToken **Token)
80 FreeStrBuf(&(*Token)->FlatToken);
81 if ((*Token)->HaveParameters)
82 for (i = 0; i < (*Token)->nParameters; i++)
83 free((*Token)->Params[i]);
90 void FreeWCTemplate(void *vFreeMe)
93 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
95 if (FreeMe->TokenSpace > 0) {
96 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
97 FreeToken(&FreeMe->Tokens[i]);
101 FreeStrBuf(&FreeMe->FileName);
102 FreeStrBuf(&FreeMe->Data);
108 * \brief debugging function to print array to log
110 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
112 * \brief debugging function to print array to log
114 void VarPrintEntry(const char *Key, void *vSubst, int odd)
117 lprintf(1,"Subst[%s] : ", Key);
118 ptr = (wcsubst*) vSubst;
120 switch(ptr->wcs_type) {
122 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
125 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
128 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
131 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
138 * \brief Clear out the list of substitution variables local to this session
140 void clear_substs(struct wcsession *wc) {
142 if (wc->vars != NULL) {
143 DeleteHash(&wc->vars);
148 * \brief Clear out the list of substitution variables local to this session
150 void clear_local_substs(void) {
169 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
171 int NeedNew = NeedNewBuf(type);
172 switch(ptr->wcs_type) {
176 if (reusestrbuf && NeedNew) {
177 FlushStrBuf(ptr->wcs_value);
181 FreeStrBuf(&ptr->wcs_value);
182 ptr->wcs_value = NULL;
186 ptr->wcs_function = NULL;
187 if (reusestrbuf && NeedNew)
188 ptr->wcs_value = NewStrBuf();
191 ptr->wcs_value = NULL;
192 if (reusestrbuf && NeedNew)
193 ptr->wcs_value = NewStrBuf();
197 if (reusestrbuf && NeedNew)
198 ptr->wcs_value = NewStrBuf();
201 if (reusestrbuf && NeedNew)
202 ptr->wcs_value = NewStrBuf();
209 * \brief destructor; kill one entry.
211 void deletevar(void *data)
213 wcsubst *ptr = (wcsubst*)data;
214 FlushPayload(ptr, 0, ptr->wcs_type);
219 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
222 struct wcsession *WCC = WC;
224 ptr = (wcsubst *) malloc(sizeof(wcsubst));
225 memset(ptr, 0, sizeof(wcsubst));
227 ptr->wcs_type = type;
228 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
229 Put(WCC->vars, keyname, keylen, ptr, deletevar);
231 switch(ptr->wcs_type) {
234 ptr->wcs_value = NewStrBuf();
248 * \brief Add a substitution variable (local to this session) (strlen version...)
249 * \param keyname the replacementstring to substitute
250 * \param keytype the kind of the key
251 * \param format the format string ala printf
252 * \param ... the arguments to substitute in the formatstring
254 void SVPRINTF(char *keyname, int keytype, const char *format,...)
260 struct wcsession *WCC = WC;
262 keylen = strlen(keyname);
264 * First look if we're doing a replacement of
267 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
268 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
269 ptr = (wcsubst*)vPtr;
270 FlushPayload(ptr, keytype, keytype);
271 ptr->wcs_type = keytype;
273 else /** Otherwise allocate a new one */
275 ptr = NewSubstVar(keyname, keylen, keytype);
278 /** Format the string */
279 va_start(arg_ptr, format);
280 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
285 * \brief Add a substitution variable (local to this session)
286 * \param keyname the replacementstring to substitute
287 * \param keytype the kind of the key
288 * \param format the format string ala printf
289 * \param ... the arguments to substitute in the formatstring
291 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
296 struct wcsession *WCC = WC;
299 * First look if we're doing a replacement of
302 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
303 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
304 ptr = (wcsubst*)vPtr;
305 FlushPayload(ptr, 1, keytype);
306 ptr->wcs_type = keytype;
308 else /** Otherwise allocate a new one */
310 ptr = NewSubstVar(keyname, keylen, keytype);
313 /** Format the string and save it */
314 va_start(arg_ptr, format);
315 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
320 * \brief Add a substitution variable (local to this session)
321 * \param keyname the replacementstring to substitute
322 * \param keytype the kind of the key
323 * \param format the format string ala printf
324 * \param ... the arguments to substitute in the formatstring
326 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
330 struct wcsession *WCC = WC;
334 * First look if we're doing a replacement of
337 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
338 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
339 ptr = (wcsubst*)vPtr;
340 FlushPayload(ptr, 1, keytype);
341 ptr->wcs_type = keytype;
343 else /** Otherwise allocate a new one */
345 ptr = NewSubstVar(keyname, keylen, keytype);
347 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
351 * \brief Add a substitution variable (local to this session)
352 * \param keyname the replacementstring to substitute
353 * \param keytype the kind of the key
354 * \param format the format string ala printf
355 * \param ... the arguments to substitute in the formatstring
357 void SVPutLong(char *keyname, size_t keylen, long Data)
361 struct wcsession *WCC = WC;
365 * First look if we're doing a replacement of
368 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
369 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
370 ptr = (wcsubst*)vPtr;
371 FlushPayload(ptr, 1, WCS_LONG);
372 ptr->wcs_type = WCS_LONG;
374 else /** Otherwise allocate a new one */
376 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
382 * \brief Add a substitution variable (local to this session) that does a callback
383 * \param keyname the keystring to substitute
384 * \param fcn_ptr the function callback to give the substitution string
386 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
390 struct wcsession *WCC = WC;
393 * First look if we're doing a replacement of
396 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
397 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
398 ptr = (wcsubst*)vPtr;
399 FlushPayload(ptr, 1, WCS_FUNCTION);
400 ptr->wcs_type = WCS_FUNCTION;
402 else /** Otherwise allocate a new one */
404 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
407 ptr->wcs_function = fcn_ptr;
409 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
411 SVCallback(keyname, strlen(keyname), fcn_ptr);
416 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
420 struct wcsession *WCC = WC;
423 * First look if we're doing a replacement of
426 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
427 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
428 ptr = (wcsubst*)vPtr;
429 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
430 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
432 else /** Otherwise allocate a new one */
434 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
436 ptr->wcs_value = (StrBuf*)Buf;
440 * \brief back end for print_value_of() ... does a server command
441 * \param servcmd server command to execute on the citadel server
443 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
447 serv_puts(ChrPtr(servcmd));
448 len = serv_getln(buf, sizeof buf);
454 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
457 _fmout(Target, "CENTER");
460 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
468 * \brief puts string into the template and computes which escape methon we should use
469 * \param Source the string we should put into the template
470 * \param FormatTypeIndex where should we look for escape types if?
472 void StrBufAppendTemplate(StrBuf *Target,
474 WCTemplateToken *Tokens,
475 void *Context, int ContextType,
476 const StrBuf *Source, int FormatTypeIndex)
478 struct wcsession *WCC;
482 if ((FormatTypeIndex < Tokens->nParameters) &&
483 (Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
484 (Tokens->Params[FormatTypeIndex]->len == 1)) {
485 EscapeAs = *Tokens->Params[FormatTypeIndex]->Start;
492 Buf = NewStrBufPlain(NULL, StrLength(Buf));
493 StrBuf_RFC822_to_Utf8(Buf,
495 (WCC!=NULL)? WCC->DefaultCharset : NULL,
497 StrEscAppend(Target, Buf, NULL, 0, 0);
501 StrEscAppend(Target, Source, NULL, 0, 0);
504 StrBufAppendBuf(Target, Source, 0);
509 void GetTemplateTokenString(WCTemplateToken *Tokens,
516 if (Tokens->HaveParameters < N) {
522 switch (Tokens->Params[N]->Type) {
525 *Value = Tokens->Params[N]->Start;
526 *len = Tokens->Params[N]->len;
529 Buf = (StrBuf*) SBstr(Tokens->Params[N]->Start,
530 Tokens->Params[N]->len);
531 *Value = ChrPtr(Buf);
532 *len = StrLength(Buf);
536 Tokens->Params[N]->Start,
537 Tokens->Params[N]->len,
539 *Value = ChrPtr(Buf);
540 *len = StrLength(Buf);
544 break; ///todo: string to text?
546 *Value = _(Tokens->Params[N]->Start);
547 *len = strlen(*Value);
558 * \brief Print the value of a variable
559 * \param keyname get a key to print
561 void print_value_of(StrBuf *Target, WCTemplateToken *Token, void *Context, int ContextType) {
562 struct wcsession *WCC = WC;
566 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
567 /// TODO: debricated!
568 if (Token->pName[0] == '=') {
569 DoTemplate(Token->pName+1, Token->NameEnd - 1, NULL, NULL, 0);
572 //////TODO: if param[1] == "U" -> urlescape
574 /** Page-local variables */
575 if ((WCC->vars!= NULL) && GetHash(WCC->vars, Token->pName, Token->NameEnd, &vVar)) {
576 ptr = (wcsubst*) vVar;
577 switch(ptr->wcs_type) {
579 StrBufAppendBuf(Target, ptr->wcs_value, 0);
582 pvo_do_cmd(Target, ptr->wcs_value);
585 (*ptr->wcs_function) (Target, Token->nParameters, Token, Context, ContextType);
589 StrBufAppendBuf(Target, ptr->wcs_value, 0);
592 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
595 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n", Token->pName);
596 StrBufAppendPrintf(Target, "<pre>WARNING: \ninvalid value in SV-Hash at %s!\n</pre>", Token->pName);
600 lprintf(1, "didn't find Handler [%s] (in '%s' line %ld); "
603 ChrPtr(Token->FileName),
605 ChrPtr(Token->FlatToken));
610 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
612 struct wcsession *WCC = WC;
616 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
617 ParamToLookup->len, &vVar)) {
618 ptr = (wcsubst*) vVar;
619 switch(ptr->wcs_type) {
623 if (ParamToCompare->Type == TYPE_STR)
624 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
625 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
627 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
635 if (ParamToCompare->Type == TYPE_STR)
638 return ParamToCompare->lvalue == ptr->lvalue;
641 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
642 ParamToLookup->Start);
648 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
650 struct wcsession *WCC = WC;
654 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
655 ParamToLookup->len, &vVar)) {
656 ptr = (wcsubst*) vVar;
657 switch(ptr->wcs_type) {
661 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
662 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
669 return StrTol(Compare) == ptr->lvalue;
671 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
672 ParamToLookup->Start);
679 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
681 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
682 if (Template->TokenSpace <= 0) {
683 Template->Tokens = (WCTemplateToken**)malloc(
684 sizeof(WCTemplateToken*) * 10);
685 Template->TokenSpace = 10;
688 WCTemplateToken **NewTokens;
689 NewTokens= (WCTemplateToken**)malloc(
690 sizeof(WCTemplateToken*) *
691 Template->TokenSpace * 2);
692 memcpy(NewTokens, Template->Tokens,
693 sizeof(WCTemplateToken*) * Template->nTokensUsed);
694 free(Template->Tokens);
695 Template->TokenSpace *= 2;
696 Template->Tokens = NewTokens;
699 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
702 TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, WCTemplateToken *Token, WCTemplate *pTmpl)
704 const char *pch = *pCh;
705 const char *pchs, *pche;
706 TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
710 Parm->Type = TYPE_STR;
712 /* Skip leading whitespaces */
713 while ((*pch == ' ' )||
716 (*pch == '\n')) pch ++;
719 Parm->Type = TYPE_PREFSTR;
722 else if (*pch == ';') {
723 Parm->Type = TYPE_PREFINT;
726 else if (*pch == '_') {
727 Parm->Type = TYPE_GETTEXT;
730 else if (*pch == 'B') {
731 Parm->Type = TYPE_BSTR;
738 else if (*pch == '\'')
745 ( (pch > pchs) && (*(pch - 1) == '\\'))
751 lprintf(1, "Error (in '%s' line %ld); "
752 "evaluating template param [%s] in Token [%s]\n",
753 ChrPtr(pTmpl->FileName),
755 ChrPtr(Token->FlatToken),
762 StrBufPeek(Buf, pch, -1, '\0');
763 if (LoadTemplates > 1) {
764 lprintf(1, "DBG: got param [%s] %ld %ld\n",
765 pchs, pche - pchs, strlen(pchs));
768 Parm->len = pche - pchs;
769 pch ++; /* move after trailing quote */
773 Parm->Type = TYPE_LONG;
775 while ((pch <= pe) &&
782 StrBufPeek(Buf, pch, -1, '\0');
783 Parm->lvalue = atol(pchs);
789 lprintf(1, "Error (in '%s' line %ld); "
790 "evaluating long template param [%s] in Token [%s]\n",
791 ChrPtr(pTmpl->FileName),
793 ChrPtr(Token->FlatToken),
799 while ((*pch == ' ' )||
803 (*pch == '\n')) pch ++;
809 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
811 const char *pTmplStart,
812 const char *pTmplEnd,
818 TemplateParam *Param;
819 WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
821 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
823 NewToken->Line = Line + 1;
824 NewToken->pTokenStart = pTmplStart;
825 NewToken->TokenStart = pTmplStart - pStart;
826 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
827 NewToken->pTokenEnd = pTmplEnd;
828 NewToken->NameEnd = NewToken->TokenEnd - 2;
829 NewToken->PreEval = NULL;
830 NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
832 StrBufPeek(Buf, pTmplStart, + 1, '\0');
833 StrBufPeek(Buf, pTmplEnd, -1, '\0');
834 pch = NewToken->pName = pTmplStart + 2;
836 NewToken->HaveParameters = 0;;
837 NewToken->nParameters = 0;
839 while (pch < pTmplEnd - 1) {
841 StrBufPeek(Buf, pch, -1, '\0');
842 NewToken->NameEnd = pch - NewToken->pName;
844 if (*(pTmplEnd - 1) != ')') {
845 lprintf(1, "Warning, Non welformed Token; missing right parenthesis (in '%s' line %ld); "
847 ChrPtr(pTmpl->FileName),
849 ChrPtr(NewToken->FlatToken));
851 while (pch < pTmplEnd - 1) {
852 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
854 NewToken->HaveParameters = 1;
855 if (NewToken->nParameters > MAXPARAM) {
856 lprintf(1, "Error (in '%s' line %ld); "
857 "only [%ld] Params allowed in Tokens [%s]\n",
858 ChrPtr(pTmpl->FileName),
861 ChrPtr(NewToken->FlatToken));
863 FreeToken(&NewToken);
866 NewToken->Params[NewToken->nParameters++] = Param;
870 if((NewToken->NameEnd == 1) &&
871 (NewToken->HaveParameters == 1))
874 if (*(NewToken->pName) == '_')
875 NewToken->Flags = SV_GETTEXT;
876 else if (*(NewToken->pName) == '=')
877 NewToken->Flags = SV_SUBTEMPL;
878 else if (*(NewToken->pName) == '%')
879 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
880 else if (*(NewToken->pName) == '?')
881 NewToken->Flags = SV_CONDITIONAL;
882 else if (*(NewToken->pName) == '!')
883 NewToken->Flags = SV_NEG_CONDITIONAL;
889 switch (NewToken->Flags) {
891 /* If we're able to find out more about the token, do it now while its fresh. */
892 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
893 HashHandler *Handler;
894 Handler = (HashHandler*) vVar;
895 if ((NewToken->nParameters < Handler->nMinArgs) ||
896 (NewToken->nParameters > Handler->nMaxArgs)) {
897 lprintf(1, "Handler [%s] (in '%s' line %ld); "
898 "doesn't work with %ld params [%s]\n",
900 ChrPtr(pTmpl->FileName),
902 NewToken->nParameters,
903 ChrPtr(NewToken->FlatToken));
906 NewToken->PreEval = Handler;
907 NewToken->Flags = SV_PREEVALUATED;
912 if (NewToken->nParameters !=1) {
913 lprintf(1, "Gettext (in '%s' line %ld); "
914 "requires exactly 1 parameter, you gave %ld params [%s]\n",
915 ChrPtr(pTmpl->FileName),
917 NewToken->nParameters,
918 ChrPtr(NewToken->FlatToken));
924 if (NewToken->nParameters != 1) {
925 lprintf(1, "Subtemplates (in '%s' line %ld); "
926 "require exactly 1 parameter, you gave %ld params [%s]\n",
927 ChrPtr(pTmpl->FileName),
929 NewToken->nParameters,
930 ChrPtr(NewToken->FlatToken));
934 case SV_CUST_STR_CONDITIONAL:
936 case SV_NEG_CONDITIONAL:
937 if (NewToken->nParameters <2) {
938 lprintf(1, "Conditional (in '%s' line %ld); "
939 "require at least 2 parameters, you gave %ld params [%s]\n",
940 ChrPtr(pTmpl->FileName),
942 NewToken->nParameters,
943 ChrPtr(NewToken->FlatToken));
947 if (!GetHash(Conditionals,
948 NewToken->Params[0]->Start,
949 NewToken->Params[0]->len,
952 if ((NewToken->Params[0]->len == 1) &&
953 (NewToken->Params[0]->Start[0] == 'X'))
955 lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n",
956 NewToken->Params[0]->Start,
957 ChrPtr(pTmpl->FileName),
959 ChrPtr(NewToken->FlatToken));
961 NewToken->Error = NewStrBuf();
964 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
965 NewToken->Params[0]->Start,
966 ChrPtr(pTmpl->FileName),
968 ChrPtr(NewToken->FlatToken));
972 NewToken->PreEval = vVar;
981 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType)
983 ConditionalStruct *Cond;
985 if ((Token->Params[0]->len == 1) &&
986 (Token->Params[0]->Start[0] == 'X'))
987 return (state != 0)?Token->Params[1]->lvalue:0;
989 Cond = (ConditionalStruct *) Token->PreEval;
991 lprintf(1, "Conditional [%s] (in '%s' line %ld); unknown![%s]\n",
992 Token->Params[0]->Start,
993 ChrPtr(pTmpl->FileName),
995 ChrPtr(Token->FlatToken));
999 if (Token->nParameters < Cond->nParams) {
1000 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n",
1001 Token->Params[0]->Start,
1002 ChrPtr(pTmpl->FileName),
1005 ChrPtr(Token->FlatToken));
1008 "<pre>\nConditional [%s] (in '%s' line %ld); needs %ld Params!\n[%s]\n</pre>\n",
1009 Token->Params[0]->Start,
1010 ChrPtr(pTmpl->FileName),
1013 ChrPtr(Token->FlatToken));
1016 if (Cond->CondF(Token, Context, ContextType) == Neg)
1017 return Token->Params[1]->lvalue;
1022 * \brief executes one token
1023 * \param Target buffer to append to
1024 * \param Token da to process.
1025 * \param Template we're iterating
1026 * \param Context Contextpoointer to pass in
1027 * \param state are we in conditional state?
1028 * \param ContextType what type of information does context giv us?
1030 int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int state, int ContextType)
1032 HashHandler *Handler;
1034 // much output, since pName is not terminated...
1035 // lprintf(1,"Doing token: %s\n",Token->pName);
1037 switch (Token->Flags) {
1039 TmplGettext(Target, Token->nParameters, Token);
1041 case SV_CONDITIONAL: /** Forward conditional evaluation */
1042 return EvaluateConditional(Target, Token, pTmpl, Context, 1, state, ContextType);
1044 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1045 return EvaluateConditional(Target, Token, pTmpl, Context, 0, state, ContextType);
1047 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1048 if (Token->nParameters >= 6) {
1049 if (EvaluateConditional(Target, Token, pTmpl, Context, 0, state, ContextType))
1050 StrBufAppendBufPlain(Target,
1051 Token->Params[5]->Start,
1052 Token->Params[5]->len,
1055 StrBufAppendBufPlain(Target,
1056 Token->Params[4]->Start,
1057 Token->Params[4]->len,
1062 if (Token->nParameters == 1)
1063 DoTemplate(Token->Params[0]->Start, Token->Params[0]->len, NULL, NULL, ContextType);
1065 case SV_PREEVALUATED:
1066 Handler = (HashHandler*) Token->PreEval;
1067 if ((Handler->ContextRequired != CTX_NONE) &&
1068 (Handler->ContextRequired != ContextType)) {
1069 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1070 "requires context of type %ld, have %ld [%s]\n",
1072 ChrPtr(pTmpl->FileName),
1074 Handler->ContextRequired,
1076 ChrPtr(Token->FlatToken));
1079 "<pre>\nHandler [%s] (in '%s' line %ld);"
1080 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1082 ChrPtr(pTmpl->FileName),
1084 Handler->ContextRequired,
1086 ChrPtr(Token->FlatToken));
1090 Handler->HandlerFunc(Target,
1097 if (GetHash(GlobalNS, Token->pName, Token->NameEnd, &vVar)) {
1098 Handler = (HashHandler*) vVar;
1099 if ((Handler->ContextRequired != CTX_NONE) &&
1100 (Handler->ContextRequired != ContextType)) {
1101 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1102 "requires context of type %ld, have %ld [%s]\n",
1104 ChrPtr(pTmpl->FileName),
1106 Handler->ContextRequired,
1108 ChrPtr(Token->FlatToken));
1111 "<pre>\nHandler [%s] (in '%s' line %ld);"
1112 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1114 ChrPtr(pTmpl->FileName),
1116 Handler->ContextRequired,
1118 ChrPtr(Token->FlatToken));
1121 else if ((Token->nParameters < Handler->nMinArgs) ||
1122 (Token->nParameters > Handler->nMaxArgs)) {
1123 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1124 "doesn't work with %ld params [%s]\n",
1126 ChrPtr(pTmpl->FileName),
1129 ChrPtr(Token->FlatToken));
1132 "<pre>\nHandler [%s] (in '%s' line %ld);"
1133 " doesn't work with %ld params!\n[%s]\n</pre>\n",
1135 ChrPtr(pTmpl->FileName),
1138 ChrPtr(Token->FlatToken));
1141 Handler->HandlerFunc(Target,
1145 ContextType); /*TODO: subset of that */
1150 print_value_of(Target, Token, Context, ContextType);
1156 void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context, int ContextType)
1158 WCTemplate *pTmpl = Tmpl;
1161 const char *pData, *pS;
1164 if (LoadTemplates != 0) {
1165 if (LoadTemplates > 1)
1166 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1167 ChrPtr(Tmpl->FileName));
1169 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
1173 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1174 ChrPtr(Tmpl->FileName));
1180 pS = pData = ChrPtr(pTmpl->Data);
1181 len = StrLength(pTmpl->Data);
1185 if (i >= pTmpl->nTokensUsed) {
1186 StrBufAppendBufPlain(Target,
1188 len - (pData - pS), 0);
1192 StrBufAppendBufPlain(
1194 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1195 state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state, ContextType);
1196 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1197 /* condition told us to skip till its end condition */
1199 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1200 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1201 if (state == EvaluateConditional(
1206 pTmpl->Tokens[i]->Flags,
1212 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1213 if (i > pTmpl->nTokensUsed)
1217 if (LoadTemplates != 0) {
1218 FreeWCTemplate(pTmpl);
1224 * \brief Display a variable-substituted template
1225 * \param templatename template file to load
1227 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1229 WCTemplate *NewTemplate;
1230 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1231 NewTemplate->Data = NULL;
1232 NewTemplate->FileName = NewStrBufDup(filename);
1233 NewTemplate->nTokensUsed = 0;
1234 NewTemplate->TokenSpace = 0;
1235 NewTemplate->Tokens = NULL;
1237 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1242 * \brief Display a variable-substituted template
1243 * \param templatename template file to load
1245 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1248 struct stat statbuf;
1249 const char *pS, *pE, *pch, *Err;
1252 WCTemplate *NewTemplate;
1254 fd = open(ChrPtr(filename), O_RDONLY);
1256 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1257 ChrPtr(filename), strerror(errno));
1261 if (fstat(fd, &statbuf) == -1) {
1262 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1263 ChrPtr(filename), strerror(errno));
1267 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1268 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1269 NewTemplate->FileName = NewStrBufDup(filename);
1270 NewTemplate->nTokensUsed = 0;
1271 NewTemplate->TokenSpace = 0;
1272 NewTemplate->Tokens = NULL;
1273 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1275 FreeWCTemplate(NewTemplate);
1276 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1277 ChrPtr(filename), strerror(errno));
1283 pS = pch = ChrPtr(NewTemplate->Data);
1284 pE = pS + StrLength(NewTemplate->Data);
1286 const char *pts, *pte;
1288 int InDoubleQuotes = 0;
1290 /** Find one <? > */
1292 for (; pch < pE; pch ++) {
1293 if ((*pch=='<')&&(*(pch + 1)=='?'))
1295 if (*pch=='\n') Line ++;
1301 /** Found one? parse it. */
1302 for (; pch < pE - 1; pch ++) {
1304 InDoubleQuotes = ! InDoubleQuotes;
1305 else if (*pch == '\'')
1306 InQuotes = ! InQuotes;
1307 else if ((!InQuotes && !InDoubleQuotes) &&
1308 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1316 PutNewToken(NewTemplate,
1317 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1320 if (LoadTemplates == 0)
1321 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1326 ///void PrintTemplate(const char *Key, void *vSubst, int odd)
1327 const char* PrintTemplate(void *vSubst)
1329 WCTemplate *Tmpl = vSubst;
1331 return ChrPtr(Tmpl->FileName);
1337 * \brief Display a variable-substituted template
1338 * \param templatename template file to load
1340 void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Context, int ContextType)
1343 HashList *StaticLocal;
1348 if (WC->is_mobile) {
1349 Static = WirelessTemplateCache;
1350 StaticLocal = WirelessLocalTemplateCache;
1353 Static = TemplateCache;
1354 StaticLocal = LocalTemplateCache;
1359 lprintf (1, "Can't to load a template with empty name!\n");
1360 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1364 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1365 !GetHash(Static, templatename, len, &vTmpl)) {
1366 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1367 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1369 (long)strlen(templatename));
1370 /// dbg_PrintHash(Static, PrintTemplate, NULL);
1371 // PrintHash(Static, VarPrintTransition, PrintTemplate);
1376 ProcessTemplate(vTmpl, Target, Context, ContextType);
1379 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
1384 DIR *filedir = NULL;
1385 struct dirent *filedir_entry;
1391 StrBufPrintf(Dir, "%s/t", DirName);
1392 filedir = opendir (ChrPtr(Dir));
1393 if (filedir == NULL) {
1398 FileName = NewStrBuf();
1400 while ((filedir_entry = readdir(filedir)))
1404 #ifdef _DIRENT_HAVE_D_NAMELEN
1405 d_namelen = filedir_entry->d_namelen;
1407 d_namelen = strlen(filedir_entry->d_name);
1409 d_without_ext = d_namelen;
1410 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1412 if ((d_without_ext == 0) || (d_namelen < 3))
1414 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1415 continue; /* Ignore backup files... */
1417 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1418 PStart = filedir_entry->d_name;
1419 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1420 MinorPtr = strchr(filedir_entry->d_name, '.');
1421 if (MinorPtr != NULL)
1423 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1425 if (LoadTemplates > 1)
1426 lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1427 if (LoadTemplates == 0)
1428 load_template(FileName, Tag, (IsMobile)?wireless:big);
1430 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1433 FreeStrBuf(&FileName);
1439 void InitTemplateCache(void)
1441 LoadTemplateDir(static_dirs[0],
1442 WirelessTemplateCache,
1444 LoadTemplateDir(static_dirs[1],
1445 WirelessLocalTemplateCache,
1446 LocalTemplateCache);
1451 typedef struct _HashIterator {
1452 HashList *StaticList;
1453 int AdditionalParams;
1455 int XPectContextType;
1456 RetrieveHashlistFunc GetHash;
1457 HashDestructorFunc Destructor;
1458 SubTemplFunc DoSubTemplate;
1461 void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1473 if (!GetHash(Iterators,
1474 Tokens->Params[0]->Start,
1475 Tokens->Params[0]->len,
1477 lprintf(1, "unknown Iterator [%s] (in '%s' line %ld); "
1479 Tokens->Params[0]->Start,
1480 ChrPtr(Tokens->FileName),
1482 ChrPtr(Tokens->FlatToken));
1485 "<pre>\nunknown Iterator [%s] (in '%s' line %ld); \n"
1487 Tokens->Params[0]->Start,
1488 ChrPtr(Tokens->FileName),
1490 ChrPtr(Tokens->FlatToken));
1494 It = (HashIterator*) vIt;
1496 if (Tokens->nParameters < It->AdditionalParams + 2) {
1497 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1498 "doesn't work with %ld params [%s]\n",
1499 Tokens->Params[0]->Start,
1500 ChrPtr(Tokens->FileName),
1502 Tokens->nParameters,
1503 ChrPtr(Tokens->FlatToken));
1506 "<pre>Iterator [%s] \n(in '%s' line %ld);\n"
1507 "doesn't work with %ld params \n[%s]\n</pre>",
1508 Tokens->Params[0]->Start,
1509 ChrPtr(Tokens->FileName),
1511 Tokens->nParameters,
1512 ChrPtr(Tokens->FlatToken));
1516 if ((It->XPectContextType != CTX_NONE) &&
1517 (It->XPectContextType != ContextType)) {
1518 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1519 "requires context of type %ld, have %ld [%s]\n",
1521 ChrPtr(Tokens->FileName),
1523 It->XPectContextType,
1525 ChrPtr(Tokens->FlatToken));
1528 "<pre>\nIterator [%s] (in '%s' line %ld);"
1529 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1531 ChrPtr(Tokens->FileName),
1533 It->XPectContextType,
1535 ChrPtr(Tokens->FlatToken));
1543 if (It->StaticList == NULL)
1544 List = It->GetHash(Target, nArgs, Tokens, Context, ContextType);
1546 List = It->StaticList;
1548 SubBuf = NewStrBuf();
1549 it = GetNewHashPos();
1550 while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
1551 svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s",
1552 (oddeven) ? "odd" : "even");
1553 svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
1555 if (It->DoSubTemplate != NULL)
1556 It->DoSubTemplate(SubBuf, vContext, Tokens);
1557 DoTemplate(Tokens->Params[1]->Start,
1558 Tokens->Params[1]->len,
1562 StrBufAppendBuf(Target, SubBuf, 0);
1563 FlushStrBuf(SubBuf);
1564 oddeven = ~ oddeven;
1566 FreeStrBuf(&SubBuf);
1568 if (It->Destructor != NULL)
1569 It->Destructor(&List);
1572 int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
1577 if (!GetHash(WC->vars,
1578 Tokens->Params[2]->Start,
1579 Tokens->Params[2]->len,
1582 subst = (wcsubst*) vsubst;
1583 if ((subst->ContextRequired != CTX_NONE) &&
1584 (subst->ContextRequired != ContextType)) {
1585 lprintf(1," WARNING: Conditional requires Context: [%ld]!\n", Tokens->Params[2]->Start);
1589 switch(subst->wcs_type) {
1591 return (subst->wcs_function!=NULL);
1593 lprintf(1, " -> Server [%s]\n", subst->wcs_value);////todo
1597 case WCS_STRBUF_REF:
1598 if (Tokens->nParameters < 4)
1600 return (strcmp(Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
1602 if (Tokens->nParameters < 4)
1603 return (subst->lvalue != 0);
1604 return (subst->lvalue == Tokens->Params[3]->lvalue);
1606 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
1612 void RegisterITERATOR(const char *Name, long len,
1613 int AdditionalParams,
1614 HashList *StaticList,
1615 RetrieveHashlistFunc GetHash,
1616 SubTemplFunc DoSubTempl,
1617 HashDestructorFunc Destructor,
1619 int XPectContextType)
1621 HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
1622 It->StaticList = StaticList;
1623 It->AdditionalParams = AdditionalParams;
1624 It->GetHash = GetHash;
1625 It->DoSubTemplate = DoSubTempl;
1626 It->Destructor = Destructor;
1627 It->ContextType = ContextType;
1628 It->XPectContextType = XPectContextType;
1629 Put(Iterators, Name, len, It, NULL);
1632 void RegisterConditional(const char *Name, long len,
1634 WCConditionalFunc CondF,
1635 int ContextRequired)
1637 ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
1638 Cond->nParams = nParams;
1639 Cond->CondF = CondF;
1640 Cond->ContextRequired = ContextRequired;
1641 Put(Conditionals, Name, len, Cond, NULL);
1645 void tmplput_ContextString(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1647 StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, (StrBuf*)Context, 0);
1649 int ConditionalContextStr(WCTemplateToken *Tokens, void *Context, int ContextType)
1651 StrBuf *EmailAddr = (StrBuf*) Context;
1652 const char *CompareToken;
1655 GetTemplateTokenString(Tokens, 3, &CompareToken, &len);
1656 return strcmp(ChrPtr(EmailAddr), CompareToken) == 0;
1660 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1664 if (Tokens->Params[1]->Type == TYPE_STR) {
1665 Headline = NewStrBuf();
1666 DoTemplate(Tokens->Params[1]->Start,
1667 Tokens->Params[1]->len,
1675 GetTemplateTokenString(Tokens,
1679 Headline = NewStrBufPlain(Ch, len);
1683 DoTemplate(HKEY("beginbox"), Target, Headline, CTX_STRBUF);
1684 DoTemplate(Tokens->Params[0]->Start,
1685 Tokens->Params[0]->len,
1689 DoTemplate(HKEY("endbox"), Target, Context, ContextType);
1693 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1696 int i, ntabs, nTabs;
1698 nTabs = ntabs = Tokens->nParameters / 2;
1699 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
1701 for (i = 0; i < ntabs; i++) {
1702 TabNames[i] = NewStrBuf();
1703 if (Tokens->Params[i * 2]->len > 0) {
1704 DoTemplate(Tokens->Params[i * 2]->Start,
1705 Tokens->Params[i * 2]->len,
1711 /** A Tab without subject? we can't count that, add it as silent */
1716 StrTabbedDialog(Target, nTabs, TabNames);
1717 for (i = 0; i < ntabs; i++) {
1718 StrBeginTab(Target, i, nTabs);
1720 DoTemplate(Tokens->Params[i * 2 + 1]->Start,
1721 Tokens->Params[i * 2 + 1]->len,
1725 StrEndTab(Target, i, nTabs);
1733 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, CTX_STRBUF);
1734 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, CTX_NONE);
1735 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, CTX_NONE);
1736 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, CTX_NONE);
1737 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
1738 RegisterConditional(HKEY("COND:CONTEXT"), 3, ConditionalContextStr, CTX_NONE);