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(Source));
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->nParameters < 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 *Tokens, void *Context, int ContextType) {
562 struct wcsession *WCC = WC;
566 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
567 /// TODO: debricated!
568 if (Tokens->pName[0] == '=') {
569 DoTemplate(Tokens->pName+1, Tokens->NameEnd - 1, NULL, NULL, 0);
572 //////TODO: if param[1] == "U" -> urlescape
574 /** Page-local variables */
575 if ((WCC->vars!= NULL) && GetHash(WCC->vars, Tokens->pName, Tokens->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, Tokens->nParameters, Tokens, 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", Tokens->pName);
596 StrBufAppendPrintf(Target, "<pre>WARNING: \ninvalid value in SV-Hash at %s!\n</pre>", Tokens->pName);
600 lprintf(1, "didn't find Handler [%s] (in '%s' line %ld); "
603 ChrPtr(Tokens->FileName),
605 ChrPtr(Tokens->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 *Tokens, 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(Tokens->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(Tokens->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->Params[1]->lvalue == 0) {
938 lprintf(1, "Conditional (in '%s' line %ld); "
939 "Conditional ID mustn't be 0! [%s]\n",
940 ChrPtr(pTmpl->FileName),
942 ChrPtr(NewToken->FlatToken));
946 if (NewToken->nParameters <2) {
947 lprintf(1, "Conditional (in '%s' line %ld); "
948 "require at least 2 parameters, you gave %ld params [%s]\n",
949 ChrPtr(pTmpl->FileName),
951 NewToken->nParameters,
952 ChrPtr(NewToken->FlatToken));
956 if (!GetHash(Conditionals,
957 NewToken->Params[0]->Start,
958 NewToken->Params[0]->len,
961 if ((NewToken->Params[0]->len == 1) &&
962 (NewToken->Params[0]->Start[0] == 'X'))
964 lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n",
965 NewToken->Params[0]->Start,
966 ChrPtr(pTmpl->FileName),
968 ChrPtr(NewToken->FlatToken));
970 NewToken->Error = NewStrBuf();
973 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
974 NewToken->Params[0]->Start,
975 ChrPtr(pTmpl->FileName),
977 ChrPtr(NewToken->FlatToken));
981 NewToken->PreEval = vVar;
990 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType)
992 ConditionalStruct *Cond;
994 if ((Tokens->Params[0]->len == 1) &&
995 (Tokens->Params[0]->Start[0] == 'X'))
996 return (state != 0)?Tokens->Params[1]->lvalue:0;
998 Cond = (ConditionalStruct *) Tokens->PreEval;
1000 lprintf(1, "Conditional [%s] (in '%s' line %ld); unknown![%s]\n",
1001 Tokens->Params[0]->Start,
1002 ChrPtr(pTmpl->FileName),
1004 ChrPtr(Tokens->FlatToken));
1008 if (Tokens->nParameters < Cond->nParams) {
1009 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n",
1010 Tokens->Params[0]->Start,
1011 ChrPtr(pTmpl->FileName),
1014 ChrPtr(Tokens->FlatToken));
1017 "<pre>\nConditional [%s] (in '%s' line %ld); needs %ld Params!\n[%s]\n</pre>\n",
1018 Tokens->Params[0]->Start,
1019 ChrPtr(pTmpl->FileName),
1022 ChrPtr(Tokens->FlatToken));
1025 if (Cond->CondF(Tokens, Context, ContextType) == Neg)
1026 return Tokens->Params[1]->lvalue;
1031 * \brief executes one token
1032 * \param Target buffer to append to
1033 * \param Token da to process.
1034 * \param Template we're iterating
1035 * \param Context Contextpoointer to pass in
1036 * \param state are we in conditional state?
1037 * \param ContextType what type of information does context giv us?
1039 int EvaluateToken(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, void *Context, int state, int ContextType)
1041 HashHandler *Handler;
1043 // much output, since pName is not terminated...
1044 // lprintf(1,"Doing token: %s\n",Token->pName);
1046 switch (Tokens->Flags) {
1048 TmplGettext(Target, Tokens->nParameters, Tokens);
1050 case SV_CONDITIONAL: /** Forward conditional evaluation */
1051 return EvaluateConditional(Target, Tokens, pTmpl, Context, 1, state, ContextType);
1053 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1054 return EvaluateConditional(Target, Tokens, pTmpl, Context, 0, state, ContextType);
1056 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1057 if (Tokens->nParameters >= 6) {
1058 if (EvaluateConditional(Target, Tokens, pTmpl, Context, 0, state, ContextType))
1059 StrBufAppendBufPlain(Target,
1060 Tokens->Params[5]->Start,
1061 Tokens->Params[5]->len,
1064 StrBufAppendBufPlain(Target,
1065 Tokens->Params[4]->Start,
1066 Tokens->Params[4]->len,
1070 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs at least 6 Params![%s]\n",
1071 Tokens->Params[0]->Start,
1072 ChrPtr(pTmpl->FileName),
1074 ChrPtr(Tokens->FlatToken));
1077 "<pre>\nConditional [%s] (in '%s' line %ld); needs 6 Params!\n[%s]\n</pre>\n",
1078 Tokens->Params[0]->Start,
1079 ChrPtr(pTmpl->FileName),
1081 ChrPtr(Tokens->FlatToken));
1085 if (Tokens->nParameters == 1)
1086 DoTemplate(Tokens->Params[0]->Start, Tokens->Params[0]->len, NULL, NULL, ContextType);
1088 case SV_PREEVALUATED:
1089 Handler = (HashHandler*) Tokens->PreEval;
1090 if ((Handler->ContextRequired != CTX_NONE) &&
1091 (Handler->ContextRequired != ContextType)) {
1092 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1093 "requires context of type %ld, have %ld [%s]\n",
1095 ChrPtr(pTmpl->FileName),
1097 Handler->ContextRequired,
1099 ChrPtr(Tokens->FlatToken));
1102 "<pre>\nHandler [%s] (in '%s' line %ld);"
1103 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1105 ChrPtr(pTmpl->FileName),
1107 Handler->ContextRequired,
1109 ChrPtr(Tokens->FlatToken));
1113 Handler->HandlerFunc(Target,
1114 Tokens->nParameters,
1120 if (GetHash(GlobalNS, Tokens->pName, Tokens->NameEnd, &vVar)) {
1121 Handler = (HashHandler*) vVar;
1122 if ((Handler->ContextRequired != CTX_NONE) &&
1123 (Handler->ContextRequired != ContextType)) {
1124 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1125 "requires context of type %ld, have %ld [%s]\n",
1127 ChrPtr(pTmpl->FileName),
1129 Handler->ContextRequired,
1131 ChrPtr(Tokens->FlatToken));
1134 "<pre>\nHandler [%s] (in '%s' line %ld);"
1135 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1137 ChrPtr(pTmpl->FileName),
1139 Handler->ContextRequired,
1141 ChrPtr(Tokens->FlatToken));
1144 else if ((Tokens->nParameters < Handler->nMinArgs) ||
1145 (Tokens->nParameters > Handler->nMaxArgs)) {
1146 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1147 "doesn't work with %ld params [%s]\n",
1149 ChrPtr(pTmpl->FileName),
1151 Tokens->nParameters,
1152 ChrPtr(Tokens->FlatToken));
1155 "<pre>\nHandler [%s] (in '%s' line %ld);"
1156 " doesn't work with %ld params!\n[%s]\n</pre>\n",
1158 ChrPtr(pTmpl->FileName),
1160 Tokens->nParameters,
1161 ChrPtr(Tokens->FlatToken));
1164 Handler->HandlerFunc(Target,
1165 Tokens->nParameters,
1168 ContextType); /*TODO: subset of that */
1173 print_value_of(Target, Tokens, Context, ContextType);
1179 void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context, int ContextType)
1181 WCTemplate *pTmpl = Tmpl;
1184 const char *pData, *pS;
1187 if (LoadTemplates != 0) {
1188 if (LoadTemplates > 1)
1189 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1190 ChrPtr(Tmpl->FileName));
1192 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
1196 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1197 ChrPtr(Tmpl->FileName));
1203 pS = pData = ChrPtr(pTmpl->Data);
1204 len = StrLength(pTmpl->Data);
1208 if (i >= pTmpl->nTokensUsed) {
1209 StrBufAppendBufPlain(Target,
1211 len - (pData - pS), 0);
1215 StrBufAppendBufPlain(
1217 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1218 state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state, ContextType);
1219 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1220 /* condition told us to skip till its end condition */
1222 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1223 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1224 if (state == EvaluateConditional(
1229 pTmpl->Tokens[i]->Flags,
1235 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1236 if (i > pTmpl->nTokensUsed)
1240 if (LoadTemplates != 0) {
1241 FreeWCTemplate(pTmpl);
1247 * \brief Display a variable-substituted template
1248 * \param templatename template file to load
1250 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1252 WCTemplate *NewTemplate;
1253 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1254 NewTemplate->Data = NULL;
1255 NewTemplate->FileName = NewStrBufDup(filename);
1256 NewTemplate->nTokensUsed = 0;
1257 NewTemplate->TokenSpace = 0;
1258 NewTemplate->Tokens = NULL;
1260 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1265 * \brief Display a variable-substituted template
1266 * \param templatename template file to load
1268 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1271 struct stat statbuf;
1272 const char *pS, *pE, *pch, *Err;
1275 WCTemplate *NewTemplate;
1277 fd = open(ChrPtr(filename), O_RDONLY);
1279 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1280 ChrPtr(filename), strerror(errno));
1284 if (fstat(fd, &statbuf) == -1) {
1285 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1286 ChrPtr(filename), strerror(errno));
1290 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1291 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1292 NewTemplate->FileName = NewStrBufDup(filename);
1293 NewTemplate->nTokensUsed = 0;
1294 NewTemplate->TokenSpace = 0;
1295 NewTemplate->Tokens = NULL;
1296 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1298 FreeWCTemplate(NewTemplate);
1299 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1300 ChrPtr(filename), strerror(errno));
1306 pS = pch = ChrPtr(NewTemplate->Data);
1307 pE = pS + StrLength(NewTemplate->Data);
1309 const char *pts, *pte;
1311 int InDoubleQuotes = 0;
1313 /** Find one <? > */
1315 for (; pch < pE; pch ++) {
1316 if ((*pch=='<')&&(*(pch + 1)=='?'))
1318 if (*pch=='\n') Line ++;
1324 /** Found one? parse it. */
1325 for (; pch < pE - 1; pch ++) {
1327 InDoubleQuotes = ! InDoubleQuotes;
1328 else if (*pch == '\'')
1329 InQuotes = ! InQuotes;
1330 else if ((!InQuotes && !InDoubleQuotes) &&
1331 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1339 PutNewToken(NewTemplate,
1340 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1343 if (LoadTemplates == 0)
1344 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1349 ///void PrintTemplate(const char *Key, void *vSubst, int odd)
1350 const char* PrintTemplate(void *vSubst)
1352 WCTemplate *Tmpl = vSubst;
1354 return ChrPtr(Tmpl->FileName);
1360 * \brief Display a variable-substituted template
1361 * \param templatename template file to load
1363 void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Context, int ContextType)
1366 HashList *StaticLocal;
1371 if (WC->is_mobile) {
1372 Static = WirelessTemplateCache;
1373 StaticLocal = WirelessLocalTemplateCache;
1376 Static = TemplateCache;
1377 StaticLocal = LocalTemplateCache;
1382 lprintf (1, "Can't to load a template with empty name!\n");
1383 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1387 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1388 !GetHash(Static, templatename, len, &vTmpl)) {
1389 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1390 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1392 (long)strlen(templatename));
1393 /// dbg_PrintHash(Static, PrintTemplate, NULL);
1394 // PrintHash(Static, VarPrintTransition, PrintTemplate);
1399 ProcessTemplate(vTmpl, Target, Context, ContextType);
1402 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
1407 DIR *filedir = NULL;
1408 struct dirent *filedir_entry;
1414 StrBufPrintf(Dir, "%s/t", DirName);
1415 filedir = opendir (ChrPtr(Dir));
1416 if (filedir == NULL) {
1421 FileName = NewStrBuf();
1423 while ((filedir_entry = readdir(filedir)))
1427 #ifdef _DIRENT_HAVE_D_NAMELEN
1428 d_namelen = filedir_entry->d_namelen;
1430 d_namelen = strlen(filedir_entry->d_name);
1432 d_without_ext = d_namelen;
1433 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1435 if ((d_without_ext == 0) || (d_namelen < 3))
1437 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1438 continue; /* Ignore backup files... */
1440 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1441 PStart = filedir_entry->d_name;
1442 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1443 MinorPtr = strchr(filedir_entry->d_name, '.');
1444 if (MinorPtr != NULL)
1446 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1448 if (LoadTemplates > 1)
1449 lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1450 if (LoadTemplates == 0)
1451 load_template(FileName, Tag, (IsMobile)?wireless:big);
1453 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1456 FreeStrBuf(&FileName);
1462 void InitTemplateCache(void)
1464 LoadTemplateDir(static_dirs[0],
1465 WirelessTemplateCache,
1467 LoadTemplateDir(static_dirs[1],
1468 WirelessLocalTemplateCache,
1469 LocalTemplateCache);
1474 typedef struct _HashIterator {
1475 HashList *StaticList;
1476 int AdditionalParams;
1478 int XPectContextType;
1479 RetrieveHashlistFunc GetHash;
1480 HashDestructorFunc Destructor;
1481 SubTemplFunc DoSubTemplate;
1484 void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1496 if (!GetHash(Iterators,
1497 Tokens->Params[0]->Start,
1498 Tokens->Params[0]->len,
1500 lprintf(1, "unknown Iterator [%s] (in '%s' line %ld); "
1502 Tokens->Params[0]->Start,
1503 ChrPtr(Tokens->FileName),
1505 ChrPtr(Tokens->FlatToken));
1508 "<pre>\nunknown Iterator [%s] (in '%s' line %ld); \n"
1510 Tokens->Params[0]->Start,
1511 ChrPtr(Tokens->FileName),
1513 ChrPtr(Tokens->FlatToken));
1517 It = (HashIterator*) vIt;
1519 if (Tokens->nParameters < It->AdditionalParams + 2) {
1520 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1521 "doesn't work with %ld params [%s]\n",
1522 Tokens->Params[0]->Start,
1523 ChrPtr(Tokens->FileName),
1525 Tokens->nParameters,
1526 ChrPtr(Tokens->FlatToken));
1529 "<pre>Iterator [%s] \n(in '%s' line %ld);\n"
1530 "doesn't work with %ld params \n[%s]\n</pre>",
1531 Tokens->Params[0]->Start,
1532 ChrPtr(Tokens->FileName),
1534 Tokens->nParameters,
1535 ChrPtr(Tokens->FlatToken));
1539 if ((It->XPectContextType != CTX_NONE) &&
1540 (It->XPectContextType != ContextType)) {
1541 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1542 "requires context of type %ld, have %ld [%s]\n",
1544 ChrPtr(Tokens->FileName),
1546 It->XPectContextType,
1548 ChrPtr(Tokens->FlatToken));
1551 "<pre>\nIterator [%s] (in '%s' line %ld);"
1552 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1554 ChrPtr(Tokens->FileName),
1556 It->XPectContextType,
1558 ChrPtr(Tokens->FlatToken));
1566 if (It->StaticList == NULL)
1567 List = It->GetHash(Target, nArgs, Tokens, Context, ContextType);
1569 List = It->StaticList;
1571 SubBuf = NewStrBuf();
1572 it = GetNewHashPos();
1573 while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
1574 svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s",
1575 (oddeven) ? "odd" : "even");
1576 svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
1578 if (It->DoSubTemplate != NULL)
1579 It->DoSubTemplate(SubBuf, vContext, Tokens);
1580 DoTemplate(Tokens->Params[1]->Start,
1581 Tokens->Params[1]->len,
1585 StrBufAppendBuf(Target, SubBuf, 0);
1586 FlushStrBuf(SubBuf);
1587 oddeven = ~ oddeven;
1589 FreeStrBuf(&SubBuf);
1591 if (It->Destructor != NULL)
1592 It->Destructor(&List);
1595 int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
1600 if (!GetHash(WC->vars,
1601 Tokens->Params[2]->Start,
1602 Tokens->Params[2]->len,
1605 subst = (wcsubst*) vsubst;
1606 if ((subst->ContextRequired != CTX_NONE) &&
1607 (subst->ContextRequired != ContextType)) {
1608 lprintf(1," WARNING: Conditional requires Context: [%ld]!\n", Tokens->Params[2]->Start);
1612 switch(subst->wcs_type) {
1614 return (subst->wcs_function!=NULL);
1616 lprintf(1, " -> Server [%s]\n", subst->wcs_value);////todo
1620 case WCS_STRBUF_REF:
1621 if (Tokens->nParameters < 4)
1623 return (strcmp(Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
1625 if (Tokens->nParameters < 4)
1626 return (subst->lvalue != 0);
1627 return (subst->lvalue == Tokens->Params[3]->lvalue);
1629 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
1635 void RegisterITERATOR(const char *Name, long len,
1636 int AdditionalParams,
1637 HashList *StaticList,
1638 RetrieveHashlistFunc GetHash,
1639 SubTemplFunc DoSubTempl,
1640 HashDestructorFunc Destructor,
1642 int XPectContextType)
1644 HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
1645 It->StaticList = StaticList;
1646 It->AdditionalParams = AdditionalParams;
1647 It->GetHash = GetHash;
1648 It->DoSubTemplate = DoSubTempl;
1649 It->Destructor = Destructor;
1650 It->ContextType = ContextType;
1651 It->XPectContextType = XPectContextType;
1652 Put(Iterators, Name, len, It, NULL);
1655 void RegisterConditional(const char *Name, long len,
1657 WCConditionalFunc CondF,
1658 int ContextRequired)
1660 ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
1661 Cond->PlainName = Name;
1662 Cond->nParams = nParams;
1663 Cond->CondF = CondF;
1664 Cond->ContextRequired = ContextRequired;
1665 Put(Conditionals, Name, len, Cond, NULL);
1669 void tmplput_ContextString(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1671 StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, (StrBuf*)Context, 0);
1673 int ConditionalContextStr(WCTemplateToken *Tokens, void *Context, int ContextType)
1675 StrBuf *TokenText = (StrBuf*) Context;
1676 const char *CompareToken;
1679 GetTemplateTokenString(Tokens, 2, &CompareToken, &len);
1680 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
1684 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1688 if (Tokens->Params[1]->Type == TYPE_STR) {
1689 Headline = NewStrBuf();
1690 DoTemplate(Tokens->Params[1]->Start,
1691 Tokens->Params[1]->len,
1699 GetTemplateTokenString(Tokens,
1703 Headline = NewStrBufPlain(Ch, len);
1707 DoTemplate(HKEY("beginbox"), Target, Headline, CTX_STRBUF);
1708 DoTemplate(Tokens->Params[0]->Start,
1709 Tokens->Params[0]->len,
1713 DoTemplate(HKEY("endbox"), Target, Context, ContextType);
1714 FreeStrBuf(Headline);
1718 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1721 int i, ntabs, nTabs;
1723 nTabs = ntabs = Tokens->nParameters / 2;
1724 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
1726 for (i = 0; i < ntabs; i++) {
1727 TabNames[i] = NewStrBuf();
1728 if (Tokens->Params[i * 2]->len > 0) {
1729 DoTemplate(Tokens->Params[i * 2]->Start,
1730 Tokens->Params[i * 2]->len,
1736 /** A Tab without subject? we can't count that, add it as silent */
1741 StrTabbedDialog(Target, nTabs, TabNames);
1742 for (i = 0; i < ntabs; i++) {
1743 StrBeginTab(Target, i, nTabs);
1745 DoTemplate(Tokens->Params[i * 2 + 1]->Start,
1746 Tokens->Params[i * 2 + 1]->len,
1750 StrEndTab(Target, i, nTabs);
1758 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, CTX_STRBUF);
1759 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, CTX_NONE);
1760 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, CTX_NONE);
1761 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, CTX_NONE);
1762 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
1763 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);