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;
35 int LoadTemplates = 0;
36 WCTemplputParams NoCtx;
39 #define SV_CONDITIONAL 2
40 #define SV_NEG_CONDITIONAL 3
41 #define SV_CUST_STR_CONDITIONAL 4
43 #define SV_PREEVALUATED 6
45 typedef struct _WCTemplate {
50 WCTemplateToken **Tokens;
53 typedef struct _HashHandler {
57 WCHandlerFunc HandlerFunc;
60 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere);
61 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP);
63 typedef struct _SortStruct {
68 CompareFunc GroupChange;
73 void DestroySortStruct(void *vSort)
75 SortStruct *Sort = (SortStruct*) vSort;
76 FreeStrBuf(&Sort->Name);
77 FreeStrBuf(&Sort->PrefPrepend);
81 void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplputParams *TP, const char *Format, ...)
86 const char *Err = NULL;
91 va_start(arg_ptr, Format);
92 StrBufVAppendPrintf(Error, Format, arg_ptr);
97 case ERR_NAME: /* the main token name... */
98 Err = TP->Tokens->pName;
101 Err = TP->Tokens->Params[0]->Start;
104 Err = TP->Tokens->Params[1]->Start;
107 lprintf(1, "%s [%s] (in '%s' line %ld); %s; [%s]\n",
110 ChrPtr(TP->Tokens->FileName),
113 ChrPtr(TP->Tokens->FlatToken));
114 if (Target != NULL) {
117 "<pre>\n%s [%s] (in '%s' line %ld); %s\n[%s]\n</pre>\n",
120 ChrPtr(TP->Tokens->FileName),
123 ChrPtr(TP->Tokens->FlatToken));
128 void RegisterNS(const char *NSName,
132 WCHandlerFunc HandlerFunc,
135 HashHandler *NewHandler;
137 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
138 NewHandler->nMinArgs = nMinArgs;
139 NewHandler->nMaxArgs = nMaxArgs;
140 NewHandler->HandlerFunc = HandlerFunc;
141 NewHandler->ContextRequired = ContextRequired;
142 Put(GlobalNS, NSName, len, NewHandler, NULL);
145 void FreeToken(WCTemplateToken **Token)
148 FreeStrBuf(&(*Token)->FlatToken);
149 if ((*Token)->HaveParameters)
150 for (i = 0; i < (*Token)->nParameters; i++)
151 free((*Token)->Params[i]);
158 void FreeWCTemplate(void *vFreeMe)
161 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
163 if (FreeMe->TokenSpace > 0) {
164 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
165 FreeToken(&FreeMe->Tokens[i]);
167 free(FreeMe->Tokens);
169 FreeStrBuf(&FreeMe->FileName);
170 FreeStrBuf(&FreeMe->Data);
176 * \brief debugging function to print array to log
178 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
180 * \brief debugging function to print array to log
182 void VarPrintEntry(const char *Key, void *vSubst, int odd)
185 lprintf(1,"Subst[%s] : ", Key);
186 ptr = (wcsubst*) vSubst;
188 switch(ptr->wcs_type) {
190 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
193 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
196 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
199 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
206 * \brief Clear out the list of substitution variables local to this session
208 void clear_substs(wcsession *wc) {
210 if (wc->vars != NULL) {
211 DeleteHash(&wc->vars);
216 * \brief Clear out the list of substitution variables local to this session
218 void clear_local_substs(void) {
237 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
239 int NeedNew = NeedNewBuf(type);
240 switch(ptr->wcs_type) {
244 if (reusestrbuf && NeedNew) {
245 FlushStrBuf(ptr->wcs_value);
249 FreeStrBuf(&ptr->wcs_value);
250 ptr->wcs_value = NULL;
254 ptr->wcs_function = NULL;
255 if (reusestrbuf && NeedNew)
256 ptr->wcs_value = NewStrBuf();
259 ptr->wcs_value = NULL;
260 if (reusestrbuf && NeedNew)
261 ptr->wcs_value = NewStrBuf();
265 if (reusestrbuf && NeedNew)
266 ptr->wcs_value = NewStrBuf();
269 if (reusestrbuf && NeedNew)
270 ptr->wcs_value = NewStrBuf();
277 * \brief destructor; kill one entry.
279 void deletevar(void *data)
281 wcsubst *ptr = (wcsubst*)data;
282 FlushPayload(ptr, 0, ptr->wcs_type);
287 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
292 ptr = (wcsubst *) malloc(sizeof(wcsubst));
293 memset(ptr, 0, sizeof(wcsubst));
295 ptr->wcs_type = type;
296 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
297 Put(WCC->vars, keyname, keylen, ptr, deletevar);
299 switch(ptr->wcs_type) {
302 ptr->wcs_value = NewStrBuf();
316 * \brief Add a substitution variable (local to this session) (strlen version...)
317 * \param keyname the replacementstring to substitute
318 * \param keytype the kind of the key
319 * \param format the format string ala printf
320 * \param ... the arguments to substitute in the formatstring
322 void SVPRINTF(char *keyname, int keytype, const char *format,...)
330 keylen = strlen(keyname);
332 * First look if we're doing a replacement of
335 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
336 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
337 ptr = (wcsubst*)vPtr;
338 FlushPayload(ptr, keytype, keytype);
339 ptr->wcs_type = keytype;
341 else /** Otherwise allocate a new one */
343 ptr = NewSubstVar(keyname, keylen, keytype);
346 /** Format the string */
347 va_start(arg_ptr, format);
348 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
353 * \brief Add a substitution variable (local to this session)
354 * \param keyname the replacementstring to substitute
355 * \param keytype the kind of the key
356 * \param format the format string ala printf
357 * \param ... the arguments to substitute in the formatstring
359 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
367 * First look if we're doing a replacement of
370 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
371 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
372 ptr = (wcsubst*)vPtr;
373 FlushPayload(ptr, 1, keytype);
374 ptr->wcs_type = keytype;
376 else /** Otherwise allocate a new one */
378 ptr = NewSubstVar(keyname, keylen, keytype);
381 /** Format the string and save it */
382 va_start(arg_ptr, format);
383 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
388 * \brief Add a substitution variable (local to this session)
389 * \param keyname the replacementstring to substitute
390 * \param keytype the kind of the key
391 * \param format the format string ala printf
392 * \param ... the arguments to substitute in the formatstring
394 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
402 * First look if we're doing a replacement of
405 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
406 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
407 ptr = (wcsubst*)vPtr;
408 FlushPayload(ptr, 1, keytype);
409 ptr->wcs_type = keytype;
411 else /** Otherwise allocate a new one */
413 ptr = NewSubstVar(keyname, keylen, keytype);
415 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
419 * \brief Add a substitution variable (local to this session)
420 * \param keyname the replacementstring to substitute
421 * \param keytype the kind of the key
422 * \param format the format string ala printf
423 * \param ... the arguments to substitute in the formatstring
425 void SVPutLong(char *keyname, size_t keylen, long Data)
433 * First look if we're doing a replacement of
436 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
437 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
438 ptr = (wcsubst*)vPtr;
439 FlushPayload(ptr, 1, WCS_LONG);
440 ptr->wcs_type = WCS_LONG;
442 else /** Otherwise allocate a new one */
444 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
450 * \brief Add a substitution variable (local to this session) that does a callback
451 * \param keyname the keystring to substitute
452 * \param fcn_ptr the function callback to give the substitution string
454 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
461 * First look if we're doing a replacement of
464 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
465 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
466 ptr = (wcsubst*)vPtr;
467 FlushPayload(ptr, 1, WCS_FUNCTION);
468 ptr->wcs_type = WCS_FUNCTION;
470 else /** Otherwise allocate a new one */
472 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
475 ptr->wcs_function = fcn_ptr;
477 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
479 SVCallback(keyname, strlen(keyname), fcn_ptr);
484 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
491 * First look if we're doing a replacement of
494 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
495 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
496 ptr = (wcsubst*)vPtr;
497 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
498 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
500 else /** Otherwise allocate a new one */
502 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
504 ptr->wcs_value = (StrBuf*)Buf;
508 * \brief back end for print_value_of() ... does a server command
509 * \param servcmd server command to execute on the citadel server
511 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
515 serv_puts(ChrPtr(servcmd));
516 len = serv_getln(buf, sizeof buf);
522 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
525 _fmout(Target, "CENTER");
528 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
534 void GetTemplateTokenString(WCTemplputParams *TP,
540 WCTemplputParams SubTP;
542 if (TP->Tokens->nParameters < N) {
543 lprintf(1, "invalid token. this shouldn't have come till here.\n");
550 switch (TP->Tokens->Params[N]->Type) {
553 *Value = TP->Tokens->Params[N]->Start;
554 *len = TP->Tokens->Params[N]->len;
557 Buf = (StrBuf*) SBstr(TKEY(N));
558 *Value = ChrPtr(Buf);
559 *len = StrLength(Buf);
562 get_PREFERENCE(TKEY(N), &Buf);
563 *Value = ChrPtr(Buf);
564 *len = StrLength(Buf);
568 break; /* todo: string to text? */
570 *Value = _(TP->Tokens->Params[N]->Start);
571 *len = strlen(*Value);
573 case TYPE_SUBTEMPLATE:
574 memset(&SubTP, 0, sizeof(WCTemplputParams *));
575 SubTP.Context = TP->Context;
576 SubTP.ContextType = TP->ContextType;
578 DoTemplate(TKEY(N), Buf, &SubTP);
579 *Value = ChrPtr(Buf);
580 *len = StrLength(Buf);
581 /* we can't free it here, so we put it into the subst so its discarded later on. */
582 SVPUTBuf(TKEY(N), Buf, 0);
594 * \brief Print the value of a variable
595 * \param keyname get a key to print
597 void print_value_of(StrBuf *Target, WCTemplputParams *TP)
604 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
605 /* TODO: depricated! */
606 if (TP->Tokens->pName[0] == '=') {
607 DoTemplate(TP->Tokens->pName+1, TP->Tokens->NameEnd - 1, NULL, &NoCtx);
609 /*/////TODO: if param[1] == "U" -> urlescape
611 /** Page-local variables */
612 if ((WCC->vars!= NULL) && GetHash(WCC->vars, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
613 ptr = (wcsubst*) vVar;
614 switch(ptr->wcs_type) {
616 StrBufAppendBuf(Target, ptr->wcs_value, 0);
619 pvo_do_cmd(Target, ptr->wcs_value);
622 (*ptr->wcs_function) (Target, TP);
626 StrBufAppendBuf(Target, ptr->wcs_value, 0);
629 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
633 Target, "Subst", ERR_NAME, TP,
634 "WARNING: invalid value in SV-Hash at %s!", TP->Tokens->pName);
639 Target, "Token", ERR_NAME, TP,
640 "didn't find Handler");
645 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
651 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
652 ParamToLookup->len, &vVar)) {
653 ptr = (wcsubst*) vVar;
654 switch(ptr->wcs_type) {
658 if (ParamToCompare->Type == TYPE_STR)
659 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
660 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
662 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
670 if (ParamToCompare->Type == TYPE_STR)
673 return ParamToCompare->lvalue == ptr->lvalue;
676 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
677 ParamToLookup->Start);
683 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
689 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
690 ParamToLookup->len, &vVar)) {
691 ptr = (wcsubst*) vVar;
692 switch(ptr->wcs_type) {
696 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
697 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
704 return StrTol(Compare) == ptr->lvalue;
706 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
707 ParamToLookup->Start);
716 * \brief puts string into the template and computes which escape methon we should use
717 * \param Source the string we should put into the template
718 * \param FormatTypeIndex where should we look for escape types if?
720 void StrBufAppendTemplate(StrBuf *Target,
721 WCTemplputParams *TP,
722 const StrBuf *Source, int FormatTypeIndex)
728 if ((FormatTypeIndex < TP->Tokens->nParameters) &&
729 (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
730 (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
731 EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
738 Buf = NewStrBufPlain(NULL, StrLength(Source));
739 StrBuf_RFC822_to_Utf8(Buf,
741 (WCC!=NULL)? WCC->DefaultCharset : NULL,
743 StrEscAppend(Target, Buf, NULL, 0, 0);
747 StrEscAppend(Target, Source, NULL, 0, 0);
750 StrECMAEscAppend(Target, Source, NULL);
753 StrBufAppendBuf(Target, Source, 0);
758 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
760 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
761 if (Template->TokenSpace <= 0) {
762 Template->Tokens = (WCTemplateToken**)malloc(
763 sizeof(WCTemplateToken*) * 10);
764 Template->TokenSpace = 10;
767 WCTemplateToken **NewTokens;
768 NewTokens= (WCTemplateToken**)malloc(
769 sizeof(WCTemplateToken*) *
770 Template->TokenSpace * 2);
771 memcpy(NewTokens, Template->Tokens,
772 sizeof(WCTemplateToken*) * Template->nTokensUsed);
773 free(Template->Tokens);
774 Template->TokenSpace *= 2;
775 Template->Tokens = NewTokens;
778 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
781 TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, WCTemplateToken *Tokens, WCTemplate *pTmpl)
783 const char *pch = *pCh;
784 const char *pchs, *pche;
785 TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
789 Parm->Type = TYPE_STR;
791 /* Skip leading whitespaces */
792 while ((*pch == ' ' )||
795 (*pch == '\n')) pch ++;
798 Parm->Type = TYPE_PREFSTR;
805 else if (*pch == ';') {
806 Parm->Type = TYPE_PREFINT;
813 else if (*pch == '_') {
814 Parm->Type = TYPE_GETTEXT;
821 else if (*pch == 'B') {
822 Parm->Type = TYPE_BSTR;
829 else if (*pch == '=') {
830 Parm->Type = TYPE_SUBTEMPLATE;
841 else if (*pch == '\'')
848 ( (pch > pchs) && (*(pch - 1) == '\\'))
854 lprintf(1, "Error (in '%s' line %ld); "
855 "evaluating template param [%s] in Token [%s]\n",
856 ChrPtr(pTmpl->FileName),
858 ChrPtr(Tokens->FlatToken),
865 StrBufPeek(Buf, pch, -1, '\0');
866 if (LoadTemplates > 1) {
867 lprintf(1, "DBG: got param [%s] %ld %ld\n",
868 pchs, pche - pchs, strlen(pchs));
871 Parm->len = pche - pchs;
872 pch ++; /* move after trailing quote */
873 if (ParamBrace && (*pch == ')')) {
880 Parm->Type = TYPE_LONG;
882 while ((pch <= pe) &&
889 StrBufPeek(Buf, pch, -1, '\0');
890 Parm->lvalue = atol(pchs);
897 lprintf(1, "Error (in '%s' line %ld); "
898 "evaluating long template param [%s] in Token [%s]\n",
899 ChrPtr(pTmpl->FileName),
901 ChrPtr(Tokens->FlatToken),
908 while ((*pch == ' ' )||
912 (*pch == '\n')) pch ++;
918 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
920 const char *pTmplStart,
921 const char *pTmplEnd,
927 TemplateParam *Param;
928 WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
931 TP.Tokens = NewToken;
932 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
934 NewToken->Line = Line + 1;
935 NewToken->pTokenStart = pTmplStart;
936 NewToken->TokenStart = pTmplStart - pStart;
937 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
938 NewToken->pTokenEnd = pTmplEnd;
939 NewToken->NameEnd = NewToken->TokenEnd - 2;
940 NewToken->PreEval = NULL;
941 NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
943 StrBufPeek(Buf, pTmplStart, + 1, '\0');
944 StrBufPeek(Buf, pTmplEnd, -1, '\0');
945 pch = NewToken->pName = pTmplStart + 2;
947 NewToken->HaveParameters = 0;;
948 NewToken->nParameters = 0;
950 while (pch < pTmplEnd - 1) {
952 StrBufPeek(Buf, pch, -1, '\0');
953 NewToken->NameEnd = pch - NewToken->pName;
955 if (*(pTmplEnd - 1) != ')') {
957 NULL, "Parseerror", ERR_NAME, &TP,
958 "Warning, Non welformed Token; missing right parenthesis");
960 while (pch < pTmplEnd - 1) {
961 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
963 NewToken->HaveParameters = 1;
964 if (NewToken->nParameters > MAXPARAM) {
966 NULL, "Parseerror", ERR_NAME, &TP,
967 "only [%ld] Params allowed in Tokens",
971 FreeToken(&NewToken);
974 NewToken->Params[NewToken->nParameters++] = Param;
978 if((NewToken->NameEnd == 1) &&
979 (NewToken->HaveParameters == 1))
982 if (*(NewToken->pName) == '_')
983 NewToken->Flags = SV_GETTEXT;
984 else if (*(NewToken->pName) == '=')
985 NewToken->Flags = SV_SUBTEMPL;
986 else if (*(NewToken->pName) == '%')
987 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
988 else if (*(NewToken->pName) == '?')
989 NewToken->Flags = SV_CONDITIONAL;
990 else if (*(NewToken->pName) == '!')
991 NewToken->Flags = SV_NEG_CONDITIONAL;
997 switch (NewToken->Flags) {
999 /* If we're able to find out more about the token, do it now while its fresh. */
1000 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1001 HashHandler *Handler;
1002 Handler = (HashHandler*) vVar;
1003 if ((NewToken->nParameters < Handler->nMinArgs) ||
1004 (NewToken->nParameters > Handler->nMaxArgs)) {
1006 NULL, "Token", ERR_NAME, &TP,
1007 "doesn't work with %ld params",
1008 NewToken->nParameters);
1012 NewToken->PreEval = Handler;
1013 NewToken->Flags = SV_PREEVALUATED;
1018 if (NewToken->nParameters !=1) {
1020 NULL, "Gettext", ERR_NAME, &TP,
1021 "requires exactly 1 parameter, you gave %ld params",
1022 NewToken->nParameters);
1023 NewToken->Flags = 0;
1028 if (NewToken->nParameters != 1) {
1030 NULL, "Subtemplates", ERR_NAME, &TP,
1031 "require exactly 1 parameter, you gave %ld params",
1032 NewToken->nParameters);
1036 case SV_CUST_STR_CONDITIONAL:
1037 case SV_CONDITIONAL:
1038 case SV_NEG_CONDITIONAL:
1039 if (NewToken->nParameters <2) {
1041 NULL, "Conditional", ERR_NAME, &TP,
1042 "require at least 2 parameters, you gave %ld params",
1043 NewToken->nParameters);
1044 NewToken->Flags = 0;
1047 if (NewToken->Params[1]->lvalue == 0) {
1049 NULL, "Conditional", ERR_NAME, &TP,
1050 "Conditional ID (Parameter 1) mustn't be 0!");
1051 NewToken->Flags = 0;
1054 if (!GetHash(Conditionals,
1055 NewToken->Params[0]->Start,
1056 NewToken->Params[0]->len,
1059 if ((NewToken->Params[0]->len == 1) &&
1060 (NewToken->Params[0]->Start[0] == 'X'))
1063 NULL, "Conditional", ERR_NAME, &TP,
1066 NewToken->Error = NewStrBuf();
1069 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1070 NewToken->Params[0]->Start,
1071 ChrPtr(pTmpl->FileName),
1073 ChrPtr(NewToken->FlatToken));
1077 NewToken->PreEval = vVar;
1089 * \brief Display a variable-substituted template
1090 * \param templatename template file to load
1092 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1094 WCTemplate *NewTemplate;
1095 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1096 NewTemplate->Data = NULL;
1097 NewTemplate->FileName = NewStrBufDup(filename);
1098 NewTemplate->nTokensUsed = 0;
1099 NewTemplate->TokenSpace = 0;
1100 NewTemplate->Tokens = NULL;
1102 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1107 * \brief Display a variable-substituted template
1108 * \param templatename template file to load
1110 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1113 struct stat statbuf;
1114 const char *pS, *pE, *pch, *Err;
1117 WCTemplate *NewTemplate;
1119 fd = open(ChrPtr(filename), O_RDONLY);
1121 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1122 ChrPtr(filename), strerror(errno));
1126 if (fstat(fd, &statbuf) == -1) {
1127 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1128 ChrPtr(filename), strerror(errno));
1132 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1133 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1134 NewTemplate->FileName = NewStrBufDup(filename);
1135 NewTemplate->nTokensUsed = 0;
1136 NewTemplate->TokenSpace = 0;
1137 NewTemplate->Tokens = NULL;
1138 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1140 FreeWCTemplate(NewTemplate);
1141 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1142 ChrPtr(filename), strerror(errno));
1148 pS = pch = ChrPtr(NewTemplate->Data);
1149 pE = pS + StrLength(NewTemplate->Data);
1151 const char *pts, *pte;
1153 int InDoubleQuotes = 0;
1155 /** Find one <? > */
1157 for (; pch < pE; pch ++) {
1158 if ((*pch=='<')&&(*(pch + 1)=='?'))
1160 if (*pch=='\n') Line ++;
1166 /** Found one? parse it. */
1167 for (; pch <= pE - 1; pch ++) {
1169 InDoubleQuotes = ! InDoubleQuotes;
1170 else if (*pch == '\'')
1171 InQuotes = ! InQuotes;
1172 else if ((!InQuotes && !InDoubleQuotes) &&
1173 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1181 PutNewToken(NewTemplate,
1182 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1185 if (LoadTemplates == 0)
1186 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1191 const char* PrintTemplate(void *vSubst)
1193 WCTemplate *Tmpl = vSubst;
1195 return ChrPtr(Tmpl->FileName);
1199 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
1204 DIR *filedir = NULL;
1205 struct dirent *filedir_entry;
1211 StrBufPrintf(Dir, "%s/t", DirName);
1212 filedir = opendir (ChrPtr(Dir));
1213 if (filedir == NULL) {
1218 FileName = NewStrBuf();
1220 while ((filedir_entry = readdir(filedir)))
1224 #ifdef _DIRENT_HAVE_D_NAMELEN
1225 d_namelen = filedir_entry->d_namelen;
1227 d_namelen = strlen(filedir_entry->d_name);
1229 d_without_ext = d_namelen;
1230 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1232 if ((d_without_ext == 0) || (d_namelen < 3))
1234 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1235 continue; /* Ignore backup files... */
1237 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1238 PStart = filedir_entry->d_name;
1239 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1240 MinorPtr = strchr(filedir_entry->d_name, '.');
1241 if (MinorPtr != NULL)
1243 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1245 if (LoadTemplates > 1)
1246 lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1247 if (LoadTemplates == 0)
1248 load_template(FileName, Tag, (IsMobile)?wireless:big);
1250 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1253 FreeStrBuf(&FileName);
1259 void InitTemplateCache(void)
1261 LoadTemplateDir(static_dirs[0],
1262 WirelessTemplateCache,
1264 LoadTemplateDir(static_dirs[1],
1265 WirelessLocalTemplateCache,
1266 LocalTemplateCache);
1271 /*-----------------------------------------------------------------------------
1272 * Filling & processing Templates
1275 * \brief executes one token
1276 * \param Target buffer to append to
1277 * \param Token da to process.
1278 * \param Template we're iterating
1279 * \param Context Contextpoointer to pass in
1280 * \param state are we in conditional state?
1281 * \param ContextType what type of information does context giv us?
1283 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1285 const char *AppendMe;
1287 HashHandler *Handler;
1289 /* much output, since pName is not terminated...
1290 lprintf(1,"Doing token: %s\n",Token->pName);
1293 switch (TP->Tokens->Flags) {
1295 TmplGettext(Target, TP);
1297 case SV_CONDITIONAL: /** Forward conditional evaluation */
1298 return EvaluateConditional(Target, 1, state, TP);
1300 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1301 return EvaluateConditional(Target, 0, state, TP);
1303 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1304 if (TP->Tokens->nParameters >= 6) {
1305 if (EvaluateConditional(Target, 0, state, TP)) {
1306 GetTemplateTokenString(TP, 5, &AppendMe, &AppendMeLen);
1307 StrBufAppendBufPlain(Target,
1313 GetTemplateTokenString(TP, 4, &AppendMe, &AppendMeLen);
1314 StrBufAppendBufPlain(Target,
1322 Target, "Conditional", ERR_NAME, TP,
1323 "needs at least 6 Params!");
1327 if (TP->Tokens->nParameters == 1)
1328 DoTemplate(TKEY(0), NULL, TP);
1330 case SV_PREEVALUATED:
1331 Handler = (HashHandler*) TP->Tokens->PreEval;
1332 if ((Handler->ContextRequired != CTX_NONE) &&
1333 (Handler->ContextRequired != TP->ContextType)) {
1335 Target, "Token", ERR_NAME, TP,
1336 "requires context of type %ld, have %ld",
1337 Handler->ContextRequired,
1342 Handler->HandlerFunc(Target, TP);
1345 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
1346 Handler = (HashHandler*) vVar;
1347 if ((Handler->ContextRequired != CTX_NONE) &&
1348 (Handler->ContextRequired != TP->ContextType)) {
1350 Target, "Token", ERR_NAME, TP,
1351 "requires context of type %ld, have %ld",
1352 Handler->ContextRequired,
1356 else if ((TP->Tokens->nParameters < Handler->nMinArgs) ||
1357 (TP->Tokens->nParameters > Handler->nMaxArgs)) {
1359 Target, "Token", ERR_NAME, TP,
1360 "doesn't work with %ld params need > %ld < %ld",
1361 TP->Tokens->nParameters,
1366 Handler->HandlerFunc(Target, TP);
1370 print_value_of(Target, TP);
1378 void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context, int ContextType)
1380 WCTemplate *pTmpl = Tmpl;
1383 const char *pData, *pS;
1385 WCTemplputParams TP;
1387 TP.Context = Context;
1388 TP.ContextType = ContextType;
1390 if (LoadTemplates != 0) {
1391 if (LoadTemplates > 1)
1392 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1393 ChrPtr(Tmpl->FileName));
1395 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
1399 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1400 ChrPtr(Tmpl->FileName));
1406 pS = pData = ChrPtr(pTmpl->Data);
1407 len = StrLength(pTmpl->Data);
1411 if (i >= pTmpl->nTokensUsed) {
1412 StrBufAppendBufPlain(Target,
1414 len - (pData - pS), 0);
1418 StrBufAppendBufPlain(
1420 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1421 TP.Tokens = pTmpl->Tokens[i];
1422 TP.nArgs = pTmpl->Tokens[i]->nParameters;
1423 state = EvaluateToken(Target, state, &TP);
1425 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1426 /* condition told us to skip till its end condition */
1428 TP.Tokens = pTmpl->Tokens[i];
1429 TP.nArgs = pTmpl->Tokens[i]->nParameters;
1430 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1431 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1432 if (state == EvaluateConditional(
1434 pTmpl->Tokens[i]->Flags,
1440 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1441 if (i > pTmpl->nTokensUsed)
1445 if (LoadTemplates != 0) {
1446 FreeWCTemplate(pTmpl);
1451 * \brief Display a variable-substituted template
1452 * \param templatename template file to load
1454 void DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
1457 HashList *StaticLocal;
1462 if (WC->is_mobile) {
1463 Static = WirelessTemplateCache;
1464 StaticLocal = WirelessLocalTemplateCache;
1467 Static = TemplateCache;
1468 StaticLocal = LocalTemplateCache;
1473 lprintf (1, "Can't to load a template with empty name!\n");
1474 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1478 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1479 !GetHash(Static, templatename, len, &vTmpl)) {
1480 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1481 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1483 (long)strlen(templatename));
1485 dbg_PrintHash(Static, PrintTemplate, NULL);
1486 PrintHash(Static, VarPrintTransition, PrintTemplate);
1492 ProcessTemplate(vTmpl, Target, TP->Context, TP->ContextType);
1495 /*-----------------------------------------------------------------------------
1498 typedef struct _HashIterator {
1499 HashList *StaticList;
1500 int AdditionalParams;
1502 int XPectContextType;
1504 RetrieveHashlistFunc GetHash;
1505 HashDestructorFunc Destructor;
1506 SubTemplFunc DoSubTemplate;
1509 void RegisterITERATOR(const char *Name, long len,
1510 int AdditionalParams,
1511 HashList *StaticList,
1512 RetrieveHashlistFunc GetHash,
1513 SubTemplFunc DoSubTempl,
1514 HashDestructorFunc Destructor,
1516 int XPectContextType,
1519 HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
1520 It->StaticList = StaticList;
1521 It->AdditionalParams = AdditionalParams;
1522 It->GetHash = GetHash;
1523 It->DoSubTemplate = DoSubTempl;
1524 It->Destructor = Destructor;
1525 It->ContextType = ContextType;
1526 It->XPectContextType = XPectContextType;
1528 Put(Iterators, Name, len, It, NULL);
1531 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
1537 SortStruct *SortBy = NULL;
1539 int DetectGroupChange = 0;
1541 int nMembersCounted = 0;
1545 void *vLastContext = NULL;
1548 WCTemplputParams SubTP;
1550 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
1552 if (!GetHash(Iterators, TKEY(0), &vIt)) {
1554 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
1558 It = (HashIterator*) vIt;
1560 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
1562 Target, "Iterator", ERR_PARM1, TP,
1563 "doesn't work with %ld params",
1564 TP->Tokens->nParameters);
1568 if ((It->XPectContextType != CTX_NONE) &&
1569 (It->XPectContextType != TP->ContextType)) {
1571 Target, "Iterator", ERR_PARM1, TP,
1572 "requires context of type %ld, have %ld",
1573 It->XPectContextType,
1579 if (It->StaticList == NULL)
1580 List = It->GetHash(Target, TP);
1582 List = It->StaticList;
1584 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
1585 if (DetectGroupChange) {
1586 const StrBuf *BSort;
1587 DetectGroupChange = 0;
1588 if (havebstr("SortBy")) {
1589 BSort = sbstr("SortBy");
1590 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
1591 (vSortBy != NULL)) {
1592 SortBy = (SortStruct*)vSortBy;
1593 /** Ok, its us, lets see in which direction we should sort... */
1594 if (havebstr("SortOrder")) {
1596 SortOrder = LBSTR("SortOrder");
1598 DetectGroupChange = 1;
1603 nMembersUsed = GetCount(List);
1604 SubBuf = NewStrBuf();
1605 SubTP.ContextType = It->ContextType;
1606 it = GetNewHashPos(List, 0);
1607 while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
1608 if (DetectGroupChange && nMembersCounted > 0) {
1609 if (SortBy->GroupChange(vContext, vLastContext))
1610 svputlong("ITERATE:ISGROUPCHANGE", 1);
1612 svputlong("ITERATE:ISGROUPCHANGE", 0);
1614 svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s",
1615 (oddeven) ? "odd" : "even");
1616 svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
1617 svputlong("ITERATE:N", nMembersCounted);
1618 svputlong("ITERATE:LASTN", ++nMembersCounted == nMembersUsed);
1619 SubTP.Context = vContext;
1620 if (It->DoSubTemplate != NULL)
1621 It->DoSubTemplate(SubBuf, &SubTP);
1622 DoTemplate(TKEY(1), SubBuf, &SubTP);
1624 StrBufAppendBuf(Target, SubBuf, 0);
1625 FlushStrBuf(SubBuf);
1626 oddeven = ~ oddeven;
1627 vLastContext = vContext;
1629 FreeStrBuf(&SubBuf);
1631 if (It->Destructor != NULL)
1632 It->Destructor(&List);
1637 /*-----------------------------------------------------------------------------
1640 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
1642 ConditionalStruct *Cond;
1644 if ((TP->Tokens->Params[0]->len == 1) &&
1645 (TP->Tokens->Params[0]->Start[0] == 'X'))
1646 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
1648 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
1651 Target, "Conditional", ERR_PARM1, TP,
1656 if (TP->Tokens->nParameters < Cond->nParams) {
1658 Target, "Conditional", ERR_PARM1, TP,
1659 "needs %ld Params, have %ld!",
1661 TP->Tokens->nParameters);
1664 if (Cond->CondF(Target, TP) == Neg)
1665 return TP->Tokens->Params[1]->lvalue;
1669 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
1674 if (!GetHash(WC->vars, TKEY(2), &vsubst))
1676 subst = (wcsubst*) vsubst;
1677 if ((subst->ContextRequired != CTX_NONE) &&
1678 (subst->ContextRequired != TP->ContextType)) {
1680 Target, "ConditionalVar", ERR_PARM1, TP,
1681 " WARNING: Conditional requires Context: [%ld], have [%ld]!",
1682 subst->ContextRequired, CTX);
1686 switch(subst->wcs_type) {
1688 return (subst->wcs_function!=NULL);
1690 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
1694 case WCS_STRBUF_REF:
1695 if (TP->Tokens->nParameters < 4)
1697 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
1699 if (TP->Tokens->nParameters < 4)
1700 return (subst->lvalue != 0);
1701 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
1703 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
1710 void RegisterConditional(const char *Name, long len,
1712 WCConditionalFunc CondF,
1713 int ContextRequired)
1715 ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
1716 Cond->PlainName = Name;
1717 Cond->nParams = nParams;
1718 Cond->CondF = CondF;
1719 Cond->ContextRequired = ContextRequired;
1720 Put(Conditionals, Name, len, Cond, NULL);
1723 /*-----------------------------------------------------------------------------
1726 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
1728 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
1730 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
1732 StrBuf *TokenText = (StrBuf*) CTX;
1733 const char *CompareToken;
1736 GetTemplateTokenString(TP, 2, &CompareToken, &len);
1737 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
1740 /*-----------------------------------------------------------------------------
1744 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
1746 WCTemplputParams SubTP;
1749 if (TP->Tokens->nParameters == 2) {
1750 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
1751 Headline = NewStrBuf();
1752 DoTemplate(TKEY(1), Headline, TP);
1757 GetTemplateTokenString(TP,
1761 Headline = NewStrBufPlain(Ch, len);
1764 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
1765 SubTP.Context = Headline;
1766 SubTP.ContextType = CTX_STRBUF;
1767 DoTemplate(HKEY("beginbox"), Target, &SubTP);
1768 DoTemplate(TKEY(0), Target, TP);
1769 DoTemplate(HKEY("endbox"), Target, TP);
1770 FreeStrBuf(&Headline);
1773 /*-----------------------------------------------------------------------------
1777 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
1780 int i, ntabs, nTabs;
1782 nTabs = ntabs = TP->Tokens->nParameters / 2;
1783 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
1785 for (i = 0; i < ntabs; i++) {
1786 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
1787 (TP->Tokens->Params[i * 2]->len > 0)) {
1788 TabNames[i] = NewStrBuf();
1789 DoTemplate(TKEY(i * 2), TabNames[i], TP);
1791 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
1794 GetTemplateTokenString(TP,
1798 TabNames[i] = NewStrBufPlain(Ch, -1);
1801 /** A Tab without subject? we can't count that, add it as silent */
1806 StrTabbedDialog(Target, nTabs, TabNames);
1807 for (i = 0; i < ntabs; i++) {
1808 StrBeginTab(Target, i, nTabs);
1809 DoTemplate(TKEY(i * 2 + 1), Target, TP);
1810 StrEndTab(Target, i, nTabs);
1815 /*-----------------------------------------------------------------------------
1820 void RegisterSortFunc(const char *name, long len,
1821 const char *prepend, long preplen,
1822 CompareFunc Forward,
1823 CompareFunc Reverse,
1824 CompareFunc GroupChange,
1827 SortStruct *NewSort = (SortStruct*) malloc(sizeof(SortStruct));
1828 NewSort->Name = NewStrBufPlain(name, len);
1829 if (prepend != NULL)
1830 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
1832 NewSort->PrefPrepend = NULL;
1833 NewSort->Forward = Forward;
1834 NewSort->Reverse = Reverse;
1835 NewSort->GroupChange = GroupChange;
1836 NewSort->ContextType = ContextType;
1837 Put(SortHash, name, len, NewSort, DestroySortStruct);
1840 CompareFunc RetrieveSort(WCTemplputParams *TP,
1841 const char *OtherPrefix, long OtherPrefixLen,
1842 const char *Default, long ldefault, long DefaultDirection)
1845 const StrBuf *BSort = NULL;
1848 long SortOrder = -1;
1850 if (havebstr("SortBy")) {
1851 BSort = sbstr("SortBy");
1852 if (OtherPrefix == NULL) {
1853 set_room_pref("sort", NewStrBufDup(BSort), 0);
1856 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
1859 else { /** Try to fallback to our remembered values... */
1860 if (OtherPrefix == NULL) {
1861 BSort = get_room_pref("sort");
1864 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
1867 putbstr("SortBy", NewStrBufDup(BSort));
1871 BSort = Buf = NewStrBufPlain(Default, ldefault);
1872 putbstr("SortBy", Buf);
1876 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
1877 (vSortBy == NULL)) {
1879 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
1880 (vSortBy == NULL)) {
1882 NULL, "Sorting", ERR_PARM1, TP,
1883 "Illegal default sort: [%s]", Default);
1887 SortBy = (SortStruct*)vSortBy;
1889 /** Ok, its us, lets see in which direction we should sort... */
1890 if (havebstr("SortOrder")) {
1891 SortOrder = LBSTR("SortOrder");
1893 else { /** Try to fallback to our remembered values... */
1895 if (SortBy->PrefPrepend == NULL) {
1896 Buf = get_room_pref("SortOrder");
1897 SortOrder = StrTol(Buf);
1900 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
1904 SortOrder = DefaultDirection;
1906 Buf = NewStrBufPlain(NULL, 64);
1907 StrBufPrintf(Buf, "%ld", SortOrder);
1908 putbstr("SortOrder", Buf);
1910 switch (SortOrder) {
1915 return SortBy->Forward;
1917 return SortBy->Reverse;
1929 ConstStr SortIcons[] = {
1930 {HKEY("static/sort_none.gif")},
1931 {HKEY("static/up_pointer.gif")},
1932 {HKEY("static/down_pointer.gif")},
1935 ConstStr SortNextOrder[] = {
1942 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
1944 int bSortError = eNOT_SPECIFIED;
1945 const StrBuf *BSort;
1950 if (!GetHash(SortHash, TKEY(0), &vSort) ||
1952 return eNO_SUCH_SORT;
1953 *Param = (SortStruct*) vSort;
1956 if (havebstr("SortBy")) {
1957 BSort = sbstr("SortBy");
1958 bSortError = eINVALID_PARAM;
1959 if ((*Param)->PrefPrepend == NULL) {
1960 set_room_pref("sort", NewStrBufDup(BSort), 0);
1963 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
1966 else { /** Try to fallback to our remembered values... */
1967 if ((*Param)->PrefPrepend == NULL) {
1968 BSort = get_room_pref("sort");
1971 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
1975 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
1979 *Next = (SortStruct*) vSort;
1981 /** Ok, its us, lets see in which direction we should sort... */
1982 if (havebstr("SortOrder")) {
1983 *SortOrder = LBSTR("SortOrder");
1985 else { /** Try to fallback to our remembered values... */
1986 if ((*Param)->PrefPrepend == NULL) {
1987 *SortOrder = StrTol(get_room_pref("SortOrder"));
1990 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2000 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2005 const ConstStr *SortIcon;
2007 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2010 Target, "Sorter", ERR_PARM1, TP,
2011 " Sorter [%s] unknown!",
2012 TP->Tokens->Params[0]->Start);
2014 case eINVALID_PARAM:
2015 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2016 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2018 case eNOT_SPECIFIED:
2020 if (Next == Param) {
2021 SortIcon = &SortIcons[SortOrder];
2023 else { /** Not Us... */
2024 SortIcon = &SortIcons[0];
2026 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
2030 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
2036 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2039 Target, "Sorter", ERR_PARM1, TP,
2040 " Sorter [%s] unknown!",
2041 TP->Tokens->Params[0]->Start);
2043 case eINVALID_PARAM:
2045 NULL, "Sorter", ERR_PARM1, TP,
2046 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2048 case eNOT_SPECIFIED:
2050 StrBufAppendBuf(Target, Param->Name, 0);
2055 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
2058 const ConstStr *SortOrderStr;
2062 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2065 Target, "Sorter", ERR_PARM1, TP,
2066 " Sorter [%s] unknown!",
2067 TP->Tokens->Params[0]->Start);
2069 case eINVALID_PARAM:
2071 NULL, "Sorter", ERR_PARM1, TP,
2072 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2074 case eNOT_SPECIFIED:
2076 if (Next == Param) {
2077 SortOrderStr = &SortNextOrder[SortOrder];
2079 else { /** Not Us... */
2080 SortOrderStr = &SortNextOrder[0];
2082 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2087 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
2089 long *LongVector = (long*) CTX;
2091 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
2092 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
2094 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
2098 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
2100 Target, "Longvector", ERR_NAME, TP,
2101 "needs a numerical Parameter!");
2105 Target, "LongVector", ERR_PARM1, TP,
2106 "doesn't have %ld Parameters, its just the size of %ld!",
2107 TP->Tokens->Params[0]->lvalue,
2113 void dbg_print_longvector(long *LongVector)
2115 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
2116 int nItems = LongVector[0];
2119 for (i = 0; i < nItems; i++) {
2121 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
2123 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
2126 lprintf(1, ChrPtr(Buf));
2130 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
2132 long *LongVector = (long*) CTX;
2134 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
2135 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
2136 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
2137 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
2139 return LongVector[TP->Tokens->Params[2]->lvalue] ==
2140 LongVector[TP->Tokens->Params[3]->lvalue];
2144 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
2145 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
2147 Target, "ConditionalLongvector", ERR_PARM1, TP,
2148 "needs two long Parameter!");
2152 Target, "Longvector", ERR_PARM1, TP,
2153 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
2154 TP->Tokens->Params[2]->lvalue,
2155 TP->Tokens->Params[3]->lvalue,
2166 memset(&NoCtx, 0, sizeof(WCTemplputParams));
2167 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, CTX_NONE);
2168 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, CTX_NONE);
2169 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, CTX_NONE);
2170 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, CTX_STRBUF);
2171 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, CTX_NONE);
2172 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, CTX_NONE);
2173 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, CTX_NONE);
2174 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, CTX_LONGVECTOR);
2175 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
2176 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
2177 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);