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 StrBuf *Source, int FormatTypeIndex)
486 StrEscAppend(Target, Source, NULL, 0, 0);
489 StrBufAppendBuf(Target, Source, 0);
495 * \brief Print the value of a variable
496 * \param keyname get a key to print
498 void print_value_of(StrBuf *Target, WCTemplateToken *Token, void *Context, int ContextType) {
499 struct wcsession *WCC = WC;
503 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
504 /// TODO: debricated!
505 if (Token->pName[0] == '=') {
506 DoTemplate(Token->pName+1, Token->NameEnd - 1, NULL, NULL, 0);
509 //////TODO: if param[1] == "U" -> urlescape
511 /** Page-local variables */
512 if ((WCC->vars!= NULL) && GetHash(WCC->vars, Token->pName, Token->NameEnd, &vVar)) {
513 ptr = (wcsubst*) vVar;
514 switch(ptr->wcs_type) {
516 StrBufAppendBuf(Target, ptr->wcs_value, 0);
519 pvo_do_cmd(Target, ptr->wcs_value);
522 (*ptr->wcs_function) (Target, Token->nParameters, Token, Context, ContextType);
526 StrBufAppendBuf(Target, ptr->wcs_value, 0);
529 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
532 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n", Token->pName);
533 StrBufAppendPrintf(Target, "<pre>WARNING: \ninvalid value in SV-Hash at %s!\n</pre>", Token->pName);
537 lprintf(1, "didn't find Handler [%s] (in '%s' line %ld); "
540 ChrPtr(Token->FileName),
542 ChrPtr(Token->FlatToken));
547 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
549 struct wcsession *WCC = WC;
553 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
554 ParamToLookup->len, &vVar)) {
555 ptr = (wcsubst*) vVar;
556 switch(ptr->wcs_type) {
560 if (ParamToCompare->Type == TYPE_STR)
561 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
562 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
564 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
572 if (ParamToCompare->Type == TYPE_STR)
575 return ParamToCompare->lvalue == ptr->lvalue;
578 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
579 ParamToLookup->Start);
585 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
587 struct wcsession *WCC = WC;
591 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
592 ParamToLookup->len, &vVar)) {
593 ptr = (wcsubst*) vVar;
594 switch(ptr->wcs_type) {
598 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
599 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
606 return StrTol(Compare) == ptr->lvalue;
608 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
609 ParamToLookup->Start);
616 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
618 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
619 if (Template->TokenSpace <= 0) {
620 Template->Tokens = (WCTemplateToken**)malloc(
621 sizeof(WCTemplateToken*) * 10);
622 Template->TokenSpace = 10;
625 WCTemplateToken **NewTokens;
626 NewTokens= (WCTemplateToken**)malloc(
627 sizeof(WCTemplateToken*) *
628 Template->TokenSpace * 2);
629 memcpy(NewTokens, Template->Tokens,
630 sizeof(WCTemplateToken*) * Template->nTokensUsed);
631 free(Template->Tokens);
632 Template->TokenSpace *= 2;
633 Template->Tokens = NewTokens;
636 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
639 TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, WCTemplateToken *Token, WCTemplate *pTmpl)
641 const char *pch = *pCh;
642 const char *pchs, *pche;
643 TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
646 /* Skip leading whitespaces */
647 while ((*pch == ' ' )||
650 (*pch == '\n')) pch ++;
653 else if (*pch == '\'')
658 Parm->Type = TYPE_STR;
661 ( (pch > pchs) && (*(pch - 1) == '\\'))
667 lprintf(1, "Error (in '%s' line %ld); "
668 "evaluating template param [%s] in Token [%s]\n",
669 ChrPtr(pTmpl->FileName),
671 ChrPtr(Token->FlatToken),
678 StrBufPeek(Buf, pch, -1, '\0');
679 if (LoadTemplates > 1) {
680 lprintf(1, "DBG: got param [%s] %ld %ld\n",
681 pchs, pche - pchs, strlen(pchs));
684 Parm->len = pche - pchs;
685 pch ++; /* move after trailing quote */
689 Parm->Type = TYPE_LONG;
691 while ((pch <= pe) &&
698 StrBufPeek(Buf, pch, -1, '\0');
699 Parm->lvalue = atol(pchs);
705 lprintf(1, "Error (in '%s' line %ld); "
706 "evaluating long template param [%s] in Token [%s]\n",
707 ChrPtr(pTmpl->FileName),
709 ChrPtr(Token->FlatToken),
715 while ((*pch == ' ' )||
719 (*pch == '\n')) pch ++;
725 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
727 const char *pTmplStart,
728 const char *pTmplEnd,
734 TemplateParam *Param;
735 WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
737 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
739 NewToken->Line = Line + 1;
740 NewToken->pTokenStart = pTmplStart;
741 NewToken->TokenStart = pTmplStart - pStart;
742 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
743 NewToken->pTokenEnd = pTmplEnd;
744 NewToken->NameEnd = NewToken->TokenEnd - 2;
745 NewToken->PreEval = NULL;
746 NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
748 StrBufPeek(Buf, pTmplStart, + 1, '\0');
749 StrBufPeek(Buf, pTmplEnd, -1, '\0');
750 pch = NewToken->pName = pTmplStart + 2;
752 NewToken->HaveParameters = 0;;
753 NewToken->nParameters = 0;
755 while (pch < pTmplEnd - 1) {
757 StrBufPeek(Buf, pch, -1, '\0');
758 NewToken->NameEnd = pch - NewToken->pName;
760 if (*(pTmplEnd - 1) != ')') {
761 lprintf(1, "Warning, Non welformed Token; missing right parenthesis (in '%s' line %ld); "
763 ChrPtr(pTmpl->FileName),
765 ChrPtr(NewToken->FlatToken));
767 while (pch < pTmplEnd - 1) {
768 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
770 NewToken->HaveParameters = 1;
771 if (NewToken->nParameters > MAXPARAM) {
772 lprintf(1, "Error (in '%s' line %ld); "
773 "only [%ld] Params allowed in Tokens [%s]\n",
774 ChrPtr(pTmpl->FileName),
777 ChrPtr(NewToken->FlatToken));
779 FreeToken(&NewToken);
782 NewToken->Params[NewToken->nParameters++] = Param;
786 if((NewToken->NameEnd == 1) &&
787 (NewToken->HaveParameters == 1))
790 if (*(NewToken->pName) == '_')
791 NewToken->Flags = SV_GETTEXT;
792 else if (*(NewToken->pName) == '=')
793 NewToken->Flags = SV_SUBTEMPL;
794 else if (*(NewToken->pName) == '%')
795 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
796 else if (*(NewToken->pName) == '?')
797 NewToken->Flags = SV_CONDITIONAL;
798 else if (*(NewToken->pName) == '!')
799 NewToken->Flags = SV_NEG_CONDITIONAL;
805 switch (NewToken->Flags) {
807 /* If we're able to find out more about the token, do it now while its fresh. */
808 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
809 HashHandler *Handler;
810 Handler = (HashHandler*) vVar;
811 if ((NewToken->nParameters < Handler->nMinArgs) ||
812 (NewToken->nParameters > Handler->nMaxArgs)) {
813 lprintf(1, "Handler [%s] (in '%s' line %ld); "
814 "doesn't work with %ld params [%s]\n",
816 ChrPtr(pTmpl->FileName),
818 NewToken->nParameters,
819 ChrPtr(NewToken->FlatToken));
822 NewToken->PreEval = Handler;
823 NewToken->Flags = SV_PREEVALUATED;
828 if (NewToken->nParameters !=1) {
829 lprintf(1, "Gettext (in '%s' line %ld); "
830 "requires exactly 1 parameter, you gave %ld params [%s]\n",
831 ChrPtr(pTmpl->FileName),
833 NewToken->nParameters,
834 ChrPtr(NewToken->FlatToken));
840 if (NewToken->nParameters != 1) {
841 lprintf(1, "Subtemplates (in '%s' line %ld); "
842 "require exactly 1 parameter, you gave %ld params [%s]\n",
843 ChrPtr(pTmpl->FileName),
845 NewToken->nParameters,
846 ChrPtr(NewToken->FlatToken));
850 case SV_CUST_STR_CONDITIONAL:
852 case SV_NEG_CONDITIONAL:
853 if (NewToken->nParameters <2) {
854 lprintf(1, "Conditional (in '%s' line %ld); "
855 "require at least 2 parameters, you gave %ld params [%s]\n",
856 ChrPtr(pTmpl->FileName),
858 NewToken->nParameters,
859 ChrPtr(NewToken->FlatToken));
863 if (!GetHash(Conditionals,
864 NewToken->Params[0]->Start,
865 NewToken->Params[0]->len,
868 if ((NewToken->Params[0]->len == 1) &&
869 (NewToken->Params[0]->Start[0] == 'X'))
871 lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n",
872 NewToken->Params[0]->Start,
873 ChrPtr(pTmpl->FileName),
875 ChrPtr(NewToken->FlatToken));
877 NewToken->Error = NewStrBuf();
880 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
881 NewToken->Params[0]->Start,
882 ChrPtr(pTmpl->FileName),
884 ChrPtr(NewToken->FlatToken));
888 NewToken->PreEval = vVar;
897 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType)
899 ConditionalStruct *Cond;
901 if ((Token->Params[0]->len == 1) &&
902 (Token->Params[0]->Start[0] == 'X'))
903 return (state != 0)?Token->Params[1]->lvalue:0;
905 Cond = (ConditionalStruct *) Token->PreEval;
907 if (Token->nParameters < Cond->nParams) {
908 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n",
909 Token->Params[0]->Start,
910 ChrPtr(pTmpl->FileName),
913 ChrPtr(Token->FlatToken));
916 "<pre>\nConditional [%s] (in '%s' line %ld); needs %ld Params!\n[%s]\n</pre>\n",
917 Token->Params[0]->Start,
918 ChrPtr(pTmpl->FileName),
921 ChrPtr(Token->FlatToken));
924 if (Cond->CondF(Token, Context, ContextType) == Neg)
925 return Token->Params[1]->lvalue;
930 * \brief executes one token
931 * \param Target buffer to append to
932 * \param Token da to process.
933 * \param Template we're iterating
934 * \param Context Contextpoointer to pass in
935 * \param state are we in conditional state?
936 * \param ContextType what type of information does context giv us?
938 int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int state, int ContextType)
940 HashHandler *Handler;
942 // much output, since pName is not terminated...
943 // lprintf(1,"Doing token: %s\n",Token->pName);
945 switch (Token->Flags) {
947 TmplGettext(Target, Token->nParameters, Token);
949 case SV_CONDITIONAL: /** Forward conditional evaluation */
950 return EvaluateConditional(Target, Token, pTmpl, Context, 1, state, ContextType);
952 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
953 return EvaluateConditional(Target, Token, pTmpl, Context, 0, state, ContextType);
955 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
956 if (Token->nParameters >= 6) {
957 if (EvaluateConditional(Target, Token, pTmpl, Context, 0, state, ContextType))
958 StrBufAppendBufPlain(Target,
959 Token->Params[5]->Start,
960 Token->Params[5]->len,
963 StrBufAppendBufPlain(Target,
964 Token->Params[4]->Start,
965 Token->Params[4]->len,
970 if (Token->nParameters == 1)
971 DoTemplate(Token->Params[0]->Start, Token->Params[0]->len, NULL, NULL, ContextType);
973 case SV_PREEVALUATED:
974 Handler = (HashHandler*) Token->PreEval;
975 if ((Handler->ContextRequired != CTX_NONE) &&
976 (Handler->ContextRequired != ContextType)) {
977 lprintf(1, "Handler [%s] (in '%s' line %ld); "
978 "requires context of type %ld, have %ld [%s]\n",
980 ChrPtr(pTmpl->FileName),
982 Handler->ContextRequired,
984 ChrPtr(Token->FlatToken));
987 "<pre>\nHandler [%s] (in '%s' line %ld);"
988 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
990 ChrPtr(pTmpl->FileName),
992 Handler->ContextRequired,
994 ChrPtr(Token->FlatToken));
998 Handler->HandlerFunc(Target,
1005 if (GetHash(GlobalNS, Token->pName, Token->NameEnd, &vVar)) {
1006 Handler = (HashHandler*) vVar;
1007 if ((Handler->ContextRequired != CTX_NONE) &&
1008 (Handler->ContextRequired != ContextType)) {
1009 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1010 "requires context of type %ld, have %ld [%s]\n",
1012 ChrPtr(pTmpl->FileName),
1014 Handler->ContextRequired,
1016 ChrPtr(Token->FlatToken));
1019 "<pre>\nHandler [%s] (in '%s' line %ld);"
1020 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1022 ChrPtr(pTmpl->FileName),
1024 Handler->ContextRequired,
1026 ChrPtr(Token->FlatToken));
1029 else if ((Token->nParameters < Handler->nMinArgs) ||
1030 (Token->nParameters > Handler->nMaxArgs)) {
1031 lprintf(1, "Handler [%s] (in '%s' line %ld); "
1032 "doesn't work with %ld params [%s]\n",
1034 ChrPtr(pTmpl->FileName),
1037 ChrPtr(Token->FlatToken));
1040 "<pre>\nHandler [%s] (in '%s' line %ld);"
1041 " doesn't work with %ld params!\n[%s]\n</pre>\n",
1043 ChrPtr(pTmpl->FileName),
1046 ChrPtr(Token->FlatToken));
1049 Handler->HandlerFunc(Target,
1053 ContextType); /*TODO: subset of that */
1058 print_value_of(Target, Token, Context, ContextType);
1064 void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context, int ContextType)
1066 WCTemplate *pTmpl = Tmpl;
1069 const char *pData, *pS;
1072 if (LoadTemplates != 0) {
1073 if (LoadTemplates > 1)
1074 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1075 ChrPtr(Tmpl->FileName));
1077 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
1081 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1082 ChrPtr(Tmpl->FileName));
1088 pS = pData = ChrPtr(pTmpl->Data);
1089 len = StrLength(pTmpl->Data);
1093 if (i >= pTmpl->nTokensUsed) {
1094 StrBufAppendBufPlain(Target,
1096 len - (pData - pS), 0);
1100 StrBufAppendBufPlain(
1102 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1103 state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state, ContextType);
1104 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1105 /* condition told us to skip till its end condition */
1107 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1108 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1109 if (state == EvaluateConditional(
1114 pTmpl->Tokens[i]->Flags,
1120 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1121 if (i > pTmpl->nTokensUsed)
1125 if (LoadTemplates != 0) {
1126 FreeWCTemplate(pTmpl);
1132 * \brief Display a variable-substituted template
1133 * \param templatename template file to load
1135 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1137 WCTemplate *NewTemplate;
1138 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1139 NewTemplate->Data = NULL;
1140 NewTemplate->FileName = NewStrBufDup(filename);
1141 NewTemplate->nTokensUsed = 0;
1142 NewTemplate->TokenSpace = 0;
1143 NewTemplate->Tokens = NULL;
1145 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1150 * \brief Display a variable-substituted template
1151 * \param templatename template file to load
1153 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1156 struct stat statbuf;
1157 const char *pS, *pE, *pch, *Err;
1160 WCTemplate *NewTemplate;
1162 fd = open(ChrPtr(filename), O_RDONLY);
1164 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1165 ChrPtr(filename), strerror(errno));
1169 if (fstat(fd, &statbuf) == -1) {
1170 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1171 ChrPtr(filename), strerror(errno));
1175 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1176 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1177 NewTemplate->FileName = NewStrBufDup(filename);
1178 NewTemplate->nTokensUsed = 0;
1179 NewTemplate->TokenSpace = 0;
1180 NewTemplate->Tokens = NULL;
1181 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1183 FreeWCTemplate(NewTemplate);
1184 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1185 ChrPtr(filename), strerror(errno));
1191 pS = pch = ChrPtr(NewTemplate->Data);
1192 pE = pS + StrLength(NewTemplate->Data);
1194 const char *pts, *pte;
1196 int InDoubleQuotes = 0;
1198 /** Find one <? > */
1200 for (; pch < pE; pch ++) {
1201 if ((*pch=='<')&&(*(pch + 1)=='?'))
1203 if (*pch=='\n') Line ++;
1209 /** Found one? parse it. */
1210 for (; pch < pE - 1; pch ++) {
1212 InDoubleQuotes = ! InDoubleQuotes;
1213 else if (*pch == '\'')
1214 InQuotes = ! InQuotes;
1215 else if ((!InQuotes && !InDoubleQuotes) &&
1216 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1224 PutNewToken(NewTemplate,
1225 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1228 if (LoadTemplates == 0)
1229 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1234 ///void PrintTemplate(const char *Key, void *vSubst, int odd)
1235 const char* PrintTemplate(void *vSubst)
1237 WCTemplate *Tmpl = vSubst;
1239 return ChrPtr(Tmpl->FileName);
1245 * \brief Display a variable-substituted template
1246 * \param templatename template file to load
1248 void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Context, int ContextType)
1251 HashList *StaticLocal;
1256 if (WC->is_mobile) {
1257 Static = WirelessTemplateCache;
1258 StaticLocal = WirelessLocalTemplateCache;
1261 Static = TemplateCache;
1262 StaticLocal = LocalTemplateCache;
1267 lprintf (1, "Can't to load a template with empty name!\n");
1268 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1272 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1273 !GetHash(Static, templatename, len, &vTmpl)) {
1274 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1275 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1277 (long)strlen(templatename));
1278 /// dbg_PrintHash(Static, PrintTemplate, NULL);
1279 // PrintHash(Static, VarPrintTransition, PrintTemplate);
1284 ProcessTemplate(vTmpl, Target, Context, ContextType);
1287 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
1292 DIR *filedir = NULL;
1293 struct dirent *filedir_entry;
1299 StrBufPrintf(Dir, "%s/t", DirName);
1300 filedir = opendir (ChrPtr(Dir));
1301 if (filedir == NULL) {
1306 FileName = NewStrBuf();
1308 while ((filedir_entry = readdir(filedir)))
1312 #ifdef _DIRENT_HAVE_D_NAMELEN
1313 d_namelen = filedir_entry->d_namelen;
1315 d_namelen = strlen(filedir_entry->d_name);
1317 d_without_ext = d_namelen;
1318 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1320 if ((d_without_ext == 0) || (d_namelen < 3))
1322 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1323 continue; /* Ignore backup files... */
1325 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1326 PStart = filedir_entry->d_name;
1327 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1328 MinorPtr = strchr(filedir_entry->d_name, '.');
1329 if (MinorPtr != NULL)
1331 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1333 if (LoadTemplates > 1)
1334 lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1335 if (LoadTemplates == 0)
1336 load_template(FileName, Tag, (IsMobile)?wireless:big);
1338 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1341 FreeStrBuf(&FileName);
1347 void InitTemplateCache(void)
1349 LoadTemplateDir(static_dirs[0],
1350 WirelessTemplateCache,
1352 LoadTemplateDir(static_dirs[1],
1353 WirelessLocalTemplateCache,
1354 LocalTemplateCache);
1359 typedef struct _HashIterator {
1360 HashList *StaticList;
1361 int AdditionalParams;
1363 int XPectContextType;
1364 RetrieveHashlistFunc GetHash;
1365 HashDestructorFunc Destructor;
1366 SubTemplFunc DoSubTemplate;
1369 void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1381 if (!GetHash(Iterators,
1382 Tokens->Params[0]->Start,
1383 Tokens->Params[0]->len,
1385 lprintf(1, "unknown Iterator [%s] (in '%s' line %ld); "
1387 Tokens->Params[0]->Start,
1388 ChrPtr(Tokens->FileName),
1390 ChrPtr(Tokens->FlatToken));
1393 "<pre>\nunknown Iterator [%s] (in '%s' line %ld); \n"
1395 Tokens->Params[0]->Start,
1396 ChrPtr(Tokens->FileName),
1398 ChrPtr(Tokens->FlatToken));
1402 It = (HashIterator*) vIt;
1404 if (Tokens->nParameters < It->AdditionalParams + 2) {
1405 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1406 "doesn't work with %ld params [%s]\n",
1407 Tokens->Params[0]->Start,
1408 ChrPtr(Tokens->FileName),
1410 Tokens->nParameters,
1411 ChrPtr(Tokens->FlatToken));
1414 "<pre>Iterator [%s] \n(in '%s' line %ld);\n"
1415 "doesn't work with %ld params \n[%s]\n</pre>",
1416 Tokens->Params[0]->Start,
1417 ChrPtr(Tokens->FileName),
1419 Tokens->nParameters,
1420 ChrPtr(Tokens->FlatToken));
1424 if ((It->XPectContextType != CTX_NONE) &&
1425 (It->XPectContextType != ContextType)) {
1426 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1427 "requires context of type %ld, have %ld [%s]\n",
1429 ChrPtr(Tokens->FileName),
1431 It->XPectContextType,
1433 ChrPtr(Tokens->FlatToken));
1436 "<pre>\nIterator [%s] (in '%s' line %ld);"
1437 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1439 ChrPtr(Tokens->FileName),
1441 It->XPectContextType,
1443 ChrPtr(Tokens->FlatToken));
1451 if (It->StaticList == NULL)
1452 List = It->GetHash(Target, nArgs, Tokens, Context, ContextType);
1454 List = It->StaticList;
1456 SubBuf = NewStrBuf();
1457 it = GetNewHashPos();
1458 while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
1459 svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s",
1460 (oddeven) ? "odd" : "even");
1461 svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
1463 if (It->DoSubTemplate != NULL)
1464 It->DoSubTemplate(SubBuf, vContext, Tokens);
1465 DoTemplate(Tokens->Params[1]->Start,
1466 Tokens->Params[1]->len,
1470 StrBufAppendBuf(Target, SubBuf, 0);
1471 FlushStrBuf(SubBuf);
1472 oddeven = ~ oddeven;
1474 FreeStrBuf(&SubBuf);
1476 if (It->Destructor != NULL)
1477 It->Destructor(&List);
1480 int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
1485 if (!GetHash(WC->vars,
1486 Tokens->Params[2]->Start,
1487 Tokens->Params[2]->len,
1490 subst = (wcsubst*) vsubst;
1491 if ((subst->ContextRequired != CTX_NONE) &&
1492 (subst->ContextRequired != ContextType)) {
1493 lprintf(1," WARNING: Conditional requires Context: [%ld]!\n", Tokens->Params[2]->Start);
1497 switch(subst->wcs_type) {
1499 return (subst->wcs_function!=NULL);
1501 lprintf(1, " -> Server [%s]\n", subst->wcs_value);////todo
1505 case WCS_STRBUF_REF:
1506 if (Tokens->nParameters < 4)
1508 return (strcmp(Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
1510 if (Tokens->nParameters < 4)
1511 return (subst->lvalue != 0);
1512 return (subst->lvalue == Tokens->Params[3]->lvalue);
1514 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
1520 void RegisterITERATOR(const char *Name, long len,
1521 int AdditionalParams,
1522 HashList *StaticList,
1523 RetrieveHashlistFunc GetHash,
1524 SubTemplFunc DoSubTempl,
1525 HashDestructorFunc Destructor,
1527 int XPectContextType)
1529 HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
1530 It->StaticList = StaticList;
1531 It->AdditionalParams = AdditionalParams;
1532 It->GetHash = GetHash;
1533 It->DoSubTemplate = DoSubTempl;
1534 It->Destructor = Destructor;
1535 It->ContextType = ContextType;
1536 It->XPectContextType = XPectContextType;
1537 Put(Iterators, Name, len, It, NULL);
1540 void RegisterConditional(const char *Name, long len,
1542 WCConditionalFunc CondF,
1543 int ContextRequired)
1545 ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
1546 Cond->nParams = nParams;
1547 Cond->CondF = CondF;
1548 Cond->ContextRequired = ContextRequired;
1549 Put(Conditionals, Name, len, Cond, NULL);
1552 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1555 StrBuf *Headline = NewStrBuf();
1556 DoTemplate(Tokens->Params[1]->Start,
1557 Tokens->Params[1]->len,
1561 SVPutBuf("BOXTITLE", Headline, 0);
1564 DoTemplate(HKEY("beginbox"), Target, Context, ContextType);
1565 DoTemplate(Tokens->Params[0]->Start,
1566 Tokens->Params[0]->len,
1570 DoTemplate(HKEY("endbox"), Target, Context, ContextType);
1573 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1576 int i, ntabs, nTabs;
1578 nTabs = ntabs = Tokens->nParameters / 2;
1579 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
1581 for (i = 0; i < ntabs; i++) {
1582 TabNames[i] = NewStrBuf();
1583 if (Tokens->Params[i * 2]->len > 0) {
1584 DoTemplate(Tokens->Params[i * 2]->Start,
1585 Tokens->Params[i * 2]->len,
1591 /** A Tab without subject? we can't count that, add it as silent */
1596 StrTabbedDialog(Target, nTabs, TabNames);
1597 for (i = 0; i < ntabs; i++) {
1598 StrBeginTab(Target, i, nTabs);
1600 DoTemplate(Tokens->Params[i * 2 + 1]->Start,
1601 Tokens->Params[i * 2 + 1]->len,
1605 StrEndTab(Target, i, nTabs);
1613 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, CTX_NONE);
1614 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, CTX_NONE);
1615 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, CTX_NONE);
1616 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);