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 *Contitionals;
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 {
54 WCHandlerFunc HandlerFunc;
57 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere);
59 void RegisterNS(const char *NSName, long len, int nMinArgs, int nMaxArgs, WCHandlerFunc HandlerFunc)
61 HashHandler *NewHandler;
63 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
64 NewHandler->nMinArgs = nMinArgs;
65 NewHandler->nMaxArgs = nMaxArgs;
66 NewHandler->HandlerFunc = HandlerFunc;
67 Put(GlobalNS, NSName, len, NewHandler, NULL);
70 void FreeToken(WCTemplateToken **Token)
73 FreeStrBuf(&(*Token)->FlatToken);
74 if ((*Token)->HaveParameters)
75 for (i = 0; i < (*Token)->nParameters; i++)
76 free((*Token)->Params[i]);
83 void FreeWCTemplate(void *vFreeMe)
86 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
88 if (FreeMe->TokenSpace > 0) {
89 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
90 FreeToken(&FreeMe->Tokens[i]);
94 FreeStrBuf(&FreeMe->FileName);
95 FreeStrBuf(&FreeMe->Data);
101 * \brief debugging function to print array to log
103 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
105 * \brief debugging function to print array to log
107 void VarPrintEntry(const char *Key, void *vSubst, int odd)
110 lprintf(1,"Subst[%s] : ", Key);
111 ptr = (wcsubst*) vSubst;
113 switch(ptr->wcs_type) {
115 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
118 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
121 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
124 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
131 * \brief Clear out the list of substitution variables local to this session
133 void clear_substs(struct wcsession *wc) {
135 if (wc->vars != NULL) {
136 DeleteHash(&wc->vars);
141 * \brief Clear out the list of substitution variables local to this session
143 void clear_local_substs(void) {
162 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
164 int NeedNew = NeedNewBuf(type);
165 switch(ptr->wcs_type) {
169 if (reusestrbuf && NeedNew) {
170 FlushStrBuf(ptr->wcs_value);
174 FreeStrBuf(&ptr->wcs_value);
175 ptr->wcs_value = NULL;
179 ptr->wcs_function = NULL;
180 if (reusestrbuf && NeedNew)
181 ptr->wcs_value = NewStrBuf();
184 ptr->wcs_value = NULL;
185 if (reusestrbuf && NeedNew)
186 ptr->wcs_value = NewStrBuf();
190 if (reusestrbuf && NeedNew)
191 ptr->wcs_value = NewStrBuf();
194 if (reusestrbuf && NeedNew)
195 ptr->wcs_value = NewStrBuf();
202 * \brief destructor; kill one entry.
204 void deletevar(void *data)
206 wcsubst *ptr = (wcsubst*)data;
207 FlushPayload(ptr, 0, ptr->wcs_type);
212 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
215 struct wcsession *WCC = WC;
217 ptr = (wcsubst *) malloc(sizeof(wcsubst));
218 memset(ptr, 0, sizeof(wcsubst));
220 ptr->wcs_type = type;
221 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
222 Put(WCC->vars, keyname, keylen, ptr, deletevar);
224 switch(ptr->wcs_type) {
227 ptr->wcs_value = NewStrBuf();
241 * \brief Add a substitution variable (local to this session) (strlen version...)
242 * \param keyname the replacementstring to substitute
243 * \param keytype the kind of the key
244 * \param format the format string ala printf
245 * \param ... the arguments to substitute in the formatstring
247 void SVPRINTF(char *keyname, int keytype, const char *format,...)
253 struct wcsession *WCC = WC;
255 keylen = strlen(keyname);
257 * First look if we're doing a replacement of
260 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
261 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
262 ptr = (wcsubst*)vPtr;
263 FlushPayload(ptr, keytype, keytype);
264 ptr->wcs_type = keytype;
266 else /** Otherwise allocate a new one */
268 ptr = NewSubstVar(keyname, keylen, keytype);
271 /** Format the string */
272 va_start(arg_ptr, format);
273 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
278 * \brief Add a substitution variable (local to this session)
279 * \param keyname the replacementstring to substitute
280 * \param keytype the kind of the key
281 * \param format the format string ala printf
282 * \param ... the arguments to substitute in the formatstring
284 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
289 struct wcsession *WCC = WC;
292 * First look if we're doing a replacement of
295 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
296 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
297 ptr = (wcsubst*)vPtr;
298 FlushPayload(ptr, 1, keytype);
299 ptr->wcs_type = keytype;
301 else /** Otherwise allocate a new one */
303 ptr = NewSubstVar(keyname, keylen, keytype);
306 /** Format the string and save it */
307 va_start(arg_ptr, format);
308 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
313 * \brief Add a substitution variable (local to this session)
314 * \param keyname the replacementstring to substitute
315 * \param keytype the kind of the key
316 * \param format the format string ala printf
317 * \param ... the arguments to substitute in the formatstring
319 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
323 struct wcsession *WCC = WC;
327 * First look if we're doing a replacement of
330 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
331 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
332 ptr = (wcsubst*)vPtr;
333 FlushPayload(ptr, 1, keytype);
334 ptr->wcs_type = keytype;
336 else /** Otherwise allocate a new one */
338 ptr = NewSubstVar(keyname, keylen, keytype);
340 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
344 * \brief Add a substitution variable (local to this session)
345 * \param keyname the replacementstring to substitute
346 * \param keytype the kind of the key
347 * \param format the format string ala printf
348 * \param ... the arguments to substitute in the formatstring
350 void SVPutLong(char *keyname, size_t keylen, long Data)
354 struct wcsession *WCC = WC;
358 * First look if we're doing a replacement of
361 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
362 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
363 ptr = (wcsubst*)vPtr;
364 FlushPayload(ptr, 1, WCS_LONG);
365 ptr->wcs_type = WCS_LONG;
367 else /** Otherwise allocate a new one */
369 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
375 * \brief Add a substitution variable (local to this session) that does a callback
376 * \param keyname the keystring to substitute
377 * \param fcn_ptr the function callback to give the substitution string
379 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
383 struct wcsession *WCC = WC;
386 * First look if we're doing a replacement of
389 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
390 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
391 ptr = (wcsubst*)vPtr;
392 FlushPayload(ptr, 1, WCS_FUNCTION);
393 ptr->wcs_type = WCS_FUNCTION;
395 else /** Otherwise allocate a new one */
397 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
400 ptr->wcs_function = fcn_ptr;
402 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
404 SVCallback(keyname, strlen(keyname), fcn_ptr);
409 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
413 struct wcsession *WCC = WC;
416 * First look if we're doing a replacement of
419 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
420 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
421 ptr = (wcsubst*)vPtr;
422 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
423 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
425 else /** Otherwise allocate a new one */
427 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
429 ptr->wcs_value = (StrBuf*)Buf;
433 * \brief back end for print_value_of() ... does a server command
434 * \param servcmd server command to execute on the citadel server
436 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
440 serv_puts(ChrPtr(servcmd));
441 len = serv_getln(buf, sizeof buf);
447 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
450 _fmout(Target, "CENTER");
453 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
460 * \brief Print the value of a variable
461 * \param keyname get a key to print
463 void print_value_of(StrBuf *Target, WCTemplateToken *Token, void *Context) {
464 struct wcsession *WCC = WC;
468 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
469 /// TODO: debricated!
470 if (Token->pName[0] == '=') {
471 DoTemplate(Token->pName+1, Token->NameEnd - 1, NULL, NULL);
474 //////TODO: if param[1] == "U" -> urlescape
476 /** Page-local variables */
477 if ((WCC->vars!= NULL) && GetHash(WCC->vars, Token->pName, Token->NameEnd, &vVar)) {
478 ptr = (wcsubst*) vVar;
479 switch(ptr->wcs_type) {
481 StrBufAppendBuf(Target, ptr->wcs_value, 0);
484 pvo_do_cmd(Target, ptr->wcs_value);
487 (*ptr->wcs_function) (Target, Token->nParameters, Token, Context);
491 StrBufAppendBuf(Target, ptr->wcs_value, 0);
494 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
497 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n", Token->pName);
498 StrBufAppendPrintf(Target, "<pre>WARNING: \ninvalid value in SV-Hash at %s!\n</pre>", Token->pName);
503 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
505 struct wcsession *WCC = WC;
509 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
510 ParamToLookup->len, &vVar)) {
511 ptr = (wcsubst*) vVar;
512 switch(ptr->wcs_type) {
516 if (ParamToCompare->Type == TYPE_STR)
517 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
518 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
520 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
528 if (ParamToCompare->Type == TYPE_STR)
531 return ParamToCompare->lvalue == ptr->lvalue;
534 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
535 ParamToLookup->Start);
541 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
543 struct wcsession *WCC = WC;
547 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
548 ParamToLookup->len, &vVar)) {
549 ptr = (wcsubst*) vVar;
550 switch(ptr->wcs_type) {
554 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
555 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
562 return StrTol(Compare) == ptr->lvalue;
564 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
565 ParamToLookup->Start);
572 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
574 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
575 if (Template->TokenSpace <= 0) {
576 Template->Tokens = (WCTemplateToken**)malloc(
577 sizeof(WCTemplateToken*) * 10);
578 Template->TokenSpace = 10;
581 WCTemplateToken **NewTokens;
582 NewTokens= (WCTemplateToken**)malloc(
583 sizeof(WCTemplateToken*) *
584 Template->TokenSpace * 2);
585 memcpy(NewTokens, Template->Tokens,
586 sizeof(WCTemplateToken*) * Template->nTokensUsed);
587 free(Template->Tokens);
588 Template->TokenSpace *= 2;
589 Template->Tokens = NewTokens;
592 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
595 TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, WCTemplateToken *Token, WCTemplate *pTmpl)
597 const char *pch = *pCh;
598 const char *pchs, *pche;
599 TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
602 /* Skip leading whitespaces */
603 while ((*pch == ' ' )||
606 (*pch == '\n')) pch ++;
609 else if (*pch == '\'')
614 Parm->Type = TYPE_STR;
617 ( (pch > pchs) && (*(pch - 1) == '\\'))
623 lprintf(1, "Error (in '%s' line %ld); "
624 "evaluating template param [%s] in Token [%s]\n",
625 ChrPtr(pTmpl->FileName),
627 ChrPtr(Token->FlatToken),
634 StrBufPeek(Buf, pch, -1, '\0');
635 if (LoadTemplates > 1) {
636 lprintf(1, "DBG: got param [%s] %ld %ld\n",
637 pchs, pche - pchs, strlen(pchs));
640 Parm->len = pche - pchs;
641 pch ++; /* move after trailing quote */
645 Parm->Type = TYPE_LONG;
647 while ((pch <= pe) &&
654 StrBufPeek(Buf, pch, -1, '\0');
655 Parm->lvalue = atol(pchs);
661 lprintf(1, "Error (in '%s' line %ld); "
662 "evaluating long template param [%s] in Token [%s]\n",
663 ChrPtr(pTmpl->FileName),
665 ChrPtr(Token->FlatToken),
671 while ((*pch == ' ' )||
675 (*pch == '\n')) pch ++;
681 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
683 const char *pTmplStart,
684 const char *pTmplEnd,
689 TemplateParam *Param;
690 WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
693 NewToken->Line = Line + 1;
694 NewToken->pTokenStart = pTmplStart;
695 NewToken->TokenStart = pTmplStart - pStart;
696 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
697 NewToken->pTokenEnd = pTmplEnd;
698 NewToken->NameEnd = NewToken->TokenEnd - 2;
699 NewToken->PreEval = NULL;
700 NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
702 StrBufPeek(Buf, pTmplStart, + 1, '\0');
703 StrBufPeek(Buf, pTmplEnd, -1, '\0');
704 pch = NewToken->pName = pTmplStart + 2;
706 NewToken->HaveParameters = 0;;
707 NewToken->nParameters = 0;
709 while (pch < pTmplEnd - 1) {
711 StrBufPeek(Buf, pch, -1, '\0');
712 NewToken->NameEnd = pch - NewToken->pName;
714 while (pch < pTmplEnd - 1) {
715 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
717 NewToken->HaveParameters = 1;
718 if (NewToken->nParameters > MAXPARAM) {
719 lprintf(1, "Error (in '%s' line %ld); "
720 "only [%ld] Params allowed in Tokens [%s]\n",
721 ChrPtr(pTmpl->FileName),
724 ChrPtr(NewToken->FlatToken));
726 FreeToken(&NewToken);
729 NewToken->Params[NewToken->nParameters++] = Param;
733 if((NewToken->NameEnd == 1) &&
734 (NewToken->HaveParameters == 1))
737 if ((NewToken->nParameters == 1) &&
738 (*(NewToken->pName) == '_'))
739 NewToken->Flags = SV_GETTEXT;
740 else if ((NewToken->nParameters == 1) &&
741 (*(NewToken->pName) == '='))
742 NewToken->Flags = SV_SUBTEMPL;
743 else if ((NewToken->nParameters >= 2) &&
744 (*(NewToken->pName) == '%'))
745 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
746 else if ((NewToken->nParameters >= 2) &&
747 (*(NewToken->pName) == '?'))
748 NewToken->Flags = SV_CONDITIONAL;
749 else if ((NewToken->nParameters >=2) &&
750 (*(NewToken->pName) == '!'))
751 NewToken->Flags = SV_NEG_CONDITIONAL;
757 if (NewToken->Flags == 0) {
758 /* If we're able to find out more about the token, do it now while its fresh. */
760 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
761 HashHandler *Handler;
762 Handler = (HashHandler*) vVar;
763 if ((NewToken->nParameters < Handler->nMinArgs) ||
764 (NewToken->nParameters > Handler->nMaxArgs)) {
765 lprintf(1, "Handler [%s] (in '%s' line %ld); "
766 "doesn't work with %ld params [%s]\n",
768 ChrPtr(pTmpl->FileName),
770 NewToken->nParameters,
771 ChrPtr(NewToken->FlatToken));
774 NewToken->PreEval = Handler;
775 NewToken->Flags = SV_PREEVALUATED;
785 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int Neg, int state)
787 void *vConditional = NULL;
788 ConditionalStruct *Cond;
790 if ((Token->Params[0]->len == 1) &&
791 (Token->Params[0]->Start[0] == 'X'))
792 return (state != 0)?Token->Params[1]->lvalue:0;
794 if (!GetHash(Contitionals,
795 Token->Params[0]->Start,
796 Token->Params[0]->len,
798 (vConditional == NULL)) {
799 lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n",
800 Token->Params[0]->Start,
801 ChrPtr(pTmpl->FileName),
803 ChrPtr(Token->FlatToken));
806 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
807 Token->Params[0]->Start,
808 ChrPtr(pTmpl->FileName),
810 ChrPtr(Token->FlatToken));
813 Cond = (ConditionalStruct *) vConditional;
815 if (Token->nParameters < Cond->nParams) {
816 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n",
817 Token->Params[0]->Start,
818 ChrPtr(pTmpl->FileName),
821 ChrPtr(Token->FlatToken));
824 "<pre>\nConditional [%s] (in '%s' line %ld); needs %ld Params!\n[%s]\n</pre>\n",
825 Token->Params[0]->Start,
826 ChrPtr(pTmpl->FileName),
829 ChrPtr(Token->FlatToken));
832 if (Cond->CondF(Token, Context) == Neg)
833 return Token->Params[1]->lvalue;
837 int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int state)
839 HashHandler *Handler;
841 // much output, since pName is not terminated...
842 // lprintf(1,"Doing token: %s\n",Token->pName);
843 switch (Token->Flags) {
845 TmplGettext(Target, Token->nParameters, Token);
847 case SV_CONDITIONAL: /** Forward conditional evaluation */
848 return EvaluateConditional(Target, Token, pTmpl, Context, 1, state);
850 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
851 return EvaluateConditional(Target, Token, pTmpl, Context, 0, state);
853 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
854 if (Token->nParameters >= 6) {
855 if (EvaluateConditional(Target, Token, pTmpl, Context, 0, state))
856 StrBufAppendBufPlain(Target,
857 Token->Params[5]->Start,
858 Token->Params[5]->len,
861 StrBufAppendBufPlain(Target,
862 Token->Params[4]->Start,
863 Token->Params[4]->len,
868 if (Token->nParameters == 1)
869 DoTemplate(Token->Params[0]->Start, Token->Params[0]->len, NULL, NULL);
871 case SV_PREEVALUATED:
872 Handler = (HashHandler*) Token->PreEval;
873 Handler->HandlerFunc(Target,
879 if (GetHash(GlobalNS, Token->pName, Token->NameEnd, &vVar)) {
880 Handler = (HashHandler*) vVar;
881 if ((Token->nParameters < Handler->nMinArgs) ||
882 (Token->nParameters > Handler->nMaxArgs)) {
883 lprintf(1, "Handler [%s] (in '%s' line %ld); "
884 "doesn't work with %ld params [%s]\n",
886 ChrPtr(pTmpl->FileName),
889 ChrPtr(Token->FlatToken));
892 "<pre>\nHandler [%s] (in '%s' line %ld);"
893 " doesn't work with %ld params!\n[%s]\n</pre>\n",
895 ChrPtr(pTmpl->FileName),
898 ChrPtr(Token->FlatToken));
901 Handler->HandlerFunc(Target,
904 Context); /*TODO: subset of that */
909 print_value_of(Target, Token, Context);
915 void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context)
917 WCTemplate *pTmpl = Tmpl;
920 const char *pData, *pS;
923 if (LoadTemplates != 0) {
924 if (LoadTemplates > 1)
925 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
926 ChrPtr(Tmpl->FileName));
927 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
931 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
932 ChrPtr(Tmpl->FileName));
940 pS = pData = ChrPtr(pTmpl->Data);
941 len = StrLength(pTmpl->Data);
945 if (i >= pTmpl->nTokensUsed) {
946 StrBufAppendBufPlain(Target,
948 len - (pData - pS), 0);
952 StrBufAppendBufPlain(
954 pTmpl->Tokens[i]->pTokenStart - pData, 0);
955 state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state);
956 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
957 /* condition told us to skip till its end condition */
959 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
960 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
961 if (state == EvaluateConditional(
966 pTmpl->Tokens[i]->Flags,
971 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
972 if (i > pTmpl->nTokensUsed)
976 if (LoadTemplates != 0) {
977 FreeWCTemplate(pTmpl);
983 * \brief Display a variable-substituted template
984 * \param templatename template file to load
986 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
988 WCTemplate *NewTemplate;
989 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
990 NewTemplate->Data = NULL;
991 NewTemplate->FileName = NewStrBufDup(filename);
992 NewTemplate->nTokensUsed = 0;
993 NewTemplate->TokenSpace = 0;
994 NewTemplate->Tokens = NULL;
996 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1001 * \brief Display a variable-substituted template
1002 * \param templatename template file to load
1004 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1007 struct stat statbuf;
1008 const char *pS, *pE, *pch, *Err;
1011 WCTemplate *NewTemplate;
1013 fd = open(ChrPtr(filename), O_RDONLY);
1015 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1016 ChrPtr(filename), strerror(errno));
1020 if (fstat(fd, &statbuf) == -1) {
1021 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1022 ChrPtr(filename), strerror(errno));
1026 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1027 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1028 NewTemplate->FileName = NewStrBufDup(filename);
1029 NewTemplate->nTokensUsed = 0;
1030 NewTemplate->TokenSpace = 0;
1031 NewTemplate->Tokens = NULL;
1032 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1034 FreeWCTemplate(NewTemplate);
1035 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1036 ChrPtr(filename), strerror(errno));
1042 pS = pch = ChrPtr(NewTemplate->Data);
1043 pE = pS + StrLength(NewTemplate->Data);
1045 const char *pts, *pte;
1047 int InDoubleQuotes = 0;
1049 /** Find one <? > */
1051 for (; pch < pE; pch ++) {
1052 if ((*pch=='<')&&(*(pch + 1)=='?'))
1054 if (*pch=='\n') Line ++;
1060 /** Found one? parse it. */
1061 for (; pch < pE - 1; pch ++) {
1063 InDoubleQuotes = ! InDoubleQuotes;
1064 else if (*pch == '\'')
1065 InQuotes = ! InQuotes;
1066 else if ((!InQuotes && !InDoubleQuotes) &&
1067 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1075 PutNewToken(NewTemplate,
1076 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1079 if (LoadTemplates == 0)
1080 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1085 ///void PrintTemplate(const char *Key, void *vSubst, int odd)
1086 const char* PrintTemplate(void *vSubst)
1088 WCTemplate *Tmpl = vSubst;
1090 return ChrPtr(Tmpl->FileName);
1096 * \brief Display a variable-substituted template
1097 * \param templatename template file to load
1099 void DoTemplate(const char *templatename, long len, void *Context, StrBuf *Target)
1102 HashList *StaticLocal;
1107 if (WC->is_mobile) {
1108 Static = WirelessTemplateCache;
1109 StaticLocal = WirelessLocalTemplateCache;
1112 Static = TemplateCache;
1113 StaticLocal = LocalTemplateCache;
1118 lprintf (1, "Can't to load a template with empty name!\n");
1119 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1123 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1124 !GetHash(Static, templatename, len, &vTmpl)) {
1125 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1126 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1128 (long)strlen(templatename));
1129 /// dbg_PrintHash(Static, PrintTemplate, NULL);
1130 // PrintHash(Static, VarPrintTransition, PrintTemplate);
1135 ProcessTemplate(vTmpl, Target, Context);
1138 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
1143 DIR *filedir = NULL;
1144 struct dirent *filedir_entry;
1150 StrBufPrintf(Dir, "%s/t", DirName);
1151 filedir = opendir (ChrPtr(Dir));
1152 if (filedir == NULL) {
1157 FileName = NewStrBuf();
1159 while ((filedir_entry = readdir(filedir)))
1163 #ifdef _DIRENT_HAVE_D_NAMELEN
1164 d_namelen = filedir_entry->d_namelen;
1166 d_namelen = strlen(filedir_entry->d_name);
1168 d_without_ext = d_namelen;
1169 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1171 if ((d_without_ext == 0) || (d_namelen < 3))
1173 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1174 continue; /* Ignore backup files... */
1176 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1177 PStart = filedir_entry->d_name;
1178 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1179 MinorPtr = strchr(filedir_entry->d_name, '.');
1180 if (MinorPtr != NULL)
1182 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1185 lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1186 if (LoadTemplates == 0)
1187 load_template(FileName, Tag, (IsMobile)?wireless:big);
1189 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1192 FreeStrBuf(&FileName);
1198 void InitTemplateCache(void)
1200 LoadTemplateDir(static_dirs[0],
1201 WirelessTemplateCache,
1203 LoadTemplateDir(static_dirs[1],
1204 WirelessLocalTemplateCache,
1205 LocalTemplateCache);
1208 void tmplput_serv_ip(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1210 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
1213 void tmplput_serv_nodename(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1215 StrEscAppend(Target, NULL, serv_info.serv_nodename, 0, 0);
1218 void tmplput_serv_humannode(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1220 StrEscAppend(Target, NULL, serv_info.serv_humannode, 0, 0);
1223 void tmplput_serv_fqdn(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1225 StrEscAppend(Target, NULL, serv_info.serv_fqdn, 0, 0);
1228 void tmmplput_serv_software(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1230 StrEscAppend(Target, NULL, serv_info.serv_software, 0, 0);
1233 void tmplput_serv_rev_level(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1235 StrBufAppendPrintf(Target, "%d.%02d",
1236 serv_info.serv_rev_level / 100,
1237 serv_info.serv_rev_level % 100);
1240 void tmmplput_serv_bbs_city(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1242 StrEscAppend(Target, NULL, serv_info.serv_bbs_city, 0, 0);
1245 void tmplput_current_user(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1247 StrEscAppend(Target, NULL, WC->wc_fullname, 0, 0);
1250 void tmplput_current_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1252 StrEscAppend(Target, NULL, WC->wc_roomname, 0, 0);
1256 typedef struct _HashIterator {
1257 HashList *StaticList;
1258 int AdditionalParams;
1259 RetrieveHashlistFunc GetHash;
1260 HashDestructorFunc Destructor;
1261 SubTemplFunc DoSubTemplate;
1264 void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1276 if (!GetHash(Iterators,
1277 Tokens->Params[0]->Start,
1278 Tokens->Params[0]->len,
1280 lprintf(1, "unknown Iterator [%s] (in line %ld); "
1282 Tokens->Params[0]->Start,
1284 ChrPtr(Tokens->FlatToken));
1287 "<pre>\nunknown Iterator [%s] (in line %ld); \n"
1289 Tokens->Params[0]->Start,
1291 ChrPtr(Tokens->FlatToken));
1295 It = (HashIterator*) vIt;
1297 if (Tokens->nParameters < It->AdditionalParams + 2) {
1298 lprintf(1, "Iterator [%s] (in line %ld); "
1299 "doesn't work with %ld params [%s]\n",
1300 Tokens->Params[0]->Start,
1302 Tokens->nParameters,
1303 ChrPtr(Tokens->FlatToken));
1306 "<pre>Iterator [%s] \n(in line %ld);\n"
1307 "doesn't work with %ld params \n[%s]\n</pre>",
1308 Tokens->Params[0]->Start,
1310 Tokens->nParameters,
1311 ChrPtr(Tokens->FlatToken));
1315 if (It->StaticList == NULL)
1316 List = It->GetHash(Tokens);
1318 List = It->StaticList;
1320 SubBuf = NewStrBuf();
1321 it = GetNewHashPos();
1322 while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
1323 svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s", (oddeven)?"odd":"even");
1324 svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s",Key);
1325 It->DoSubTemplate(SubBuf, vContext, Tokens);
1326 DoTemplate(Tokens->Params[1]->Start,
1327 Tokens->Params[1]->len,
1330 StrBufAppendBuf(Target, SubBuf, 0);
1331 FlushStrBuf(SubBuf);
1332 oddeven = ~ oddeven;
1334 FreeStrBuf(&SubBuf);
1336 if (It->Destructor != NULL)
1337 It->Destructor(&List);
1340 int ConditionalVar(WCTemplateToken *Tokens, void *Context)
1345 if (!GetHash(WC->vars,
1346 Tokens->Params[2]->Start,
1347 Tokens->Params[2]->len,
1350 subst = (wcsubst*) vsubst;
1351 switch(subst->wcs_type) {
1353 return (subst->wcs_function!=NULL);
1355 lprintf(1, " -> Server [%s]\n", subst->wcs_value);////todo
1359 case WCS_STRBUF_REF:
1360 if (Tokens->nParameters < 4)
1362 return (strcmp(Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
1364 if (Tokens->nParameters < 4)
1365 return (subst->lvalue != 0);
1366 return (subst->lvalue == Tokens->Params[3]->lvalue);
1368 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
1373 void RegisterITERATOR(const char *Name, long len,
1374 int AdditionalParams,
1375 HashList *StaticList,
1376 RetrieveHashlistFunc GetHash,
1377 SubTemplFunc DoSubTempl,
1378 HashDestructorFunc Destructor)
1380 HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
1381 It->StaticList = StaticList;
1382 It->AdditionalParams = AdditionalParams;
1383 It->GetHash = GetHash;
1384 It->DoSubTemplate = DoSubTempl;
1385 It->Destructor = Destructor;
1386 Put(Iterators, Name, len, It, NULL);
1389 void RegisterConditional(const char *Name, long len,
1391 WCConditionalFunc CondF)
1393 ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
1394 Cond->nParams = nParams;
1395 Cond->CondF = CondF;
1396 Put(Contitionals, Name, len, Cond, NULL);
1399 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1402 StrBuf *Headline = NewStrBuf();
1403 DoTemplate(Tokens->Params[1]->Start,
1404 Tokens->Params[1]->len,
1407 SVPutBuf("BOXTITLE", Headline, 0);
1410 DoTemplate(HKEY("beginbox"), Context, Target);
1411 DoTemplate(Tokens->Params[0]->Start,
1412 Tokens->Params[0]->len,
1415 DoTemplate(HKEY("endbox"), Context, Target);
1418 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1421 int i, ntabs, nTabs;
1423 nTabs = ntabs = Tokens->nParameters / 2;
1424 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
1426 for (i = 0; i < ntabs; i++) {
1427 TabNames[i] = NewStrBuf();
1428 if (Tokens->Params[i * 2]->len > 0) {
1429 DoTemplate(Tokens->Params[i * 2]->Start,
1430 Tokens->Params[i * 2]->len,
1435 /** A Tab without subject? we can't count that, add it as silent */
1440 StrTabbedDialog(Target, nTabs, TabNames);
1441 for (i = 0; i < ntabs; i++) {
1442 StrBeginTab(Target, i, nTabs);
1444 DoTemplate(Tokens->Params[i * 2 + 1]->Start,
1445 Tokens->Params[i * 2 + 1]->len,
1448 StrEndTab(Target, i, nTabs);
1456 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip);
1457 RegisterNamespace("SERV:NODENAME", 0, 0, tmplput_serv_nodename);
1458 RegisterNamespace("SERV:HUMANNODE", 0, 0, tmplput_serv_humannode);
1459 RegisterNamespace("SERV:FQDN", 0, 0, tmplput_serv_fqdn);
1460 RegisterNamespace("SERV:SOFTWARE", 0, 0, tmmplput_serv_software);
1461 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level);
1462 RegisterNamespace("SERV:BBS_CITY", 0, 0, tmmplput_serv_bbs_city);
1463 /// RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmmplput_serv_ldap_enabled);
1464 RegisterNamespace("CURRENT_USER", 0, 0, tmplput_current_user);
1465 RegisterNamespace("CURRENT_ROOM", 0, 0, tmplput_current_room);
1466 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl);
1467 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed);
1468 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed);
1469 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar);