2 * Copyright (c) 1996-2013 by the citadel.org team
4 * This program is open source software. You can redistribute it and/or
5 * modify it under the terms of the GNU General Public License, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * subst template processing functions
15 extern HashList *Conditionals;
16 extern HashList *GlobalNS;
17 extern HashList *Iterators;
18 extern HashList *WirelessTemplateCache;
19 extern HashList *WirelessLocalTemplateCache;
20 extern HashList *TemplateCache;
21 extern HashList *LocalTemplateCache;
26 #define TYPE_PREFSTR 3
27 #define TYPE_ROOMPREFSTR 4
28 #define TYPE_PREFINT 5
29 #define TYPE_GETTEXT 6
31 #define TYPE_SUBTEMPLATE 8
32 #define TYPE_INTDEFINE 9
35 #define IS_NUMBER(a) ((a == TYPE_LONG) || (a == TYPE_PREFINT) || (a == TYPE_INTDEFINE))
38 * \brief Values for wcs_type
41 WCS_STRING, /* its a string */
42 WCS_FUNCTION, /* its a function callback */
43 WCS_SERVCMD, /* its a command to send to the citadel server */
44 WCS_STRBUF, /* its a strbuf we own */
45 WCS_STRBUF_REF, /* its a strbuf we mustn't free */
46 WCS_LONG /* its an integer */
52 typedef struct __CtxTypeStruct {
57 CtxTypeStruct *GetContextType(CtxType Type);
58 void RegisterContextType(const char *name, long len, CtxType *TheCtx);
59 #define RegisterCTX(a) RegisterContextType(#a, sizeof(#a) - 1, &a)
61 extern CtxType CTX_STRBUF;
62 extern CtxType CTX_STRBUFARR;
63 extern CtxType CTX_LONGVECTOR;
67 * ContextFilter resembles our RTTI information. With this structure
68 * we can make shure a tmplput function can live with the environment
70 * if not, we will log/print an error and refuse to call it.
72 typedef struct _contexts {
73 CtxType ContextType; /* do we require a User Context ? */
74 int nMinArgs; /* How many arguments do we need at least? */
75 int nMaxArgs; /* up to how many arguments can we handle? */
79 /* Forward declarations... */
80 typedef struct WCTemplateToken WCTemplateToken;
81 typedef struct WCTemplputParams WCTemplputParams;
83 /* this is the signature of a tmplput function */
84 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
86 /* if you want to pre-evaluate parts of your token, or do additional syntax, use this. */
87 typedef int (*WCPreevalFunc)(WCTemplateToken *Token);
89 /* make a template token a lookup key: */
90 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
92 void *GetContextPayload(WCTemplputParams *TP, CtxType ContextType);
93 #define CTX(a) GetContextPayload(TP, a)
97 * this is the signature of a conditional function
98 * Note: Target is just passed in for error messages; don't write onto it in regular cases.
100 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
102 typedef enum _eBitMask {
108 typedef struct _TemplateParam {
109 /* are we a string or a number? */
114 /* if we're a number: */
122 * Representation of a token; everything thats inbetween <? and >
124 struct WCTemplateToken {
125 /* Reference to the filename we're in to print error messages; not to be freed */
126 const StrBuf *FileName;
127 /* Raw copy of our original token; for error printing */
129 /* Which line did the template parser pick us up in? For error printing */
132 /* our position in the template cache buffer */
133 const char *pTokenStart;
134 /* our token length */
138 const char *pTokenEnd;
139 /* just our token name: */
143 /* stuff the pre-evaluater finds out: */
145 /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
149 /* if we have parameters here we go: */
150 /* do we have parameters or not? */
152 /* How many of them? */
155 TemplateParam *Params[MAXPARAM];
160 struct WCTemplputParams {
161 ContextFilter Filter;
164 WCTemplateToken *Tokens;
165 WCTemplputParams *Sub, *Super;
166 WCConditionalFunc ExitCtx;
172 typedef struct _ConditionalStruct {
173 ContextFilter Filter;
174 const char *PlainName;
175 WCConditionalFunc CondF;
176 WCConditionalFunc CondExitCtx;
180 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
181 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
182 typedef void (*HashDestructorFunc) (HashList **KillMe);
183 typedef int (*FilterByParamFunc)(const char* key, long len, void *Context, StrBuf *Target, WCTemplputParams *TP);
185 extern WCTemplputParams NoCtx;
187 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
195 * @brief log an error while evaluating a token; print it to the actual template
196 * @param Target your Target Buffer to print the error message next to the log
197 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
198 * @param TP grab our set of default information here
199 * @param Format for the custom error message
201 void LogTemplateError (StrBuf *Target,
204 WCTemplputParams *TP,
205 const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
210 * @brief log an error while in global context; print it to Wildfire / Target
211 * @param Target your Target Buffer to print the error message next to the log
212 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
213 * @param Format for the custom error message
215 void LogError (StrBuf *Target, const char *Type, const char *Format, ...);
219 * @brief get the actual value of a token parameter
220 * in your tmplputs or conditionals use this function to access parameters that can also be
221 * retrieved from dynamic facilities:
222 * _ -> Gettext; retrieve this token from the i18n facilities
223 * : -> lookup a setting of that name
224 * B -> bstr; an URL-Parameter
225 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
227 * @param N which token do you want to lookup?
228 * @param Value reference to the string of the token; don't free me.
229 * @param len the length of Value
231 void GetTemplateTokenString(StrBuf *Target,
232 WCTemplputParams *TP,
238 * @return whether @ref GetTemplateTokenString would be able to give you a string
240 int HaveTemplateTokenString(StrBuf *Target,
241 WCTemplputParams *TP,
250 * @brief get the actual integer value of a token parameter
251 * in your tmplputs or conditionals use this function to access parameters that can also be
252 * retrieved from dynamic facilities:
253 * _ -> Gettext; retrieve this token from the i18n facilities
254 * : -> lookup a setting of that name
255 * B -> bstr; an URL-Parameter
256 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
258 * @param N which token do you want to lookup?
259 * @param dflt default value to be retrieved if not found in preferences
260 * \returns the long value
262 long GetTemplateTokenNumber(StrBuf *Target,
263 WCTemplputParams *TP,
267 * @brief put a token value into the template
268 * use this function to append your strings into a Template.
269 * it can escape your string according to the token at FormattypeIndex:
270 * H: de-QP and utf8-ify
271 * X: escapize for HTML
273 * @param Target the destination buffer
274 * @param TP the template token information
275 * @param Source string to append
276 * @param FormatTypeIndex which parameter contains the escaping functionality?
277 * if this token doesn't have as much parameters, plain append is done.
279 void StrBufAppendTemplate(StrBuf *Target,
280 WCTemplputParams *TP,
281 const StrBuf *Source,
282 int FormatTypeIndex);
284 void StrBufAppendTemplateStr(StrBuf *Target,
285 WCTemplputParams *TP,
287 int FormatTypeIndex);
290 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
293 * @brief register a template token handler
294 * call this function in your InitModule_MODULENAME which will be called at the server start
295 * @param nMinArgs how much parameters does your token require at least?
296 * @param nMaxArgs how many parameters does your token accept?
297 * @param HandlerFunc your callback when the template is rendered and your token is there
298 * @param PreEvalFunc is called when the template is parsed; you can do additional
299 * syntax checks here or pre-evaluate stuff for better performance
300 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
302 void RegisterNS(const char *NSName, long len,
305 WCHandlerFunc HandlerFunc,
306 WCPreevalFunc PreEvalFunc,
307 int ContextRequired);
311 * @brief register a conditional token <pair> handler
312 * call this function in your InitModule_MODULENAME which will be called at the server start
313 * conditionals can be ? or ! with a pair or % similar to an implicit if
314 * @param Name whats the name of your conditional? should start with COND:
315 * @param len the token length so we don't have to measure it.
316 * @param nParams how many parameters does your conditional need on top of the default conditional parameters
317 * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
318 * @param ExitCtxCond if non-NULL, will be called after the area of the conditional is left behind.
319 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
321 void RegisterContextConditional(const char *Name, long len,
323 WCConditionalFunc CondF,
324 WCConditionalFunc ExitCtxCond,
325 int ContextRequired);
327 #define RegisterCtxConditional(Name, nParams, CondF, ExitCtxCond, ContextRequired) \
328 RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, ExitCtxCond, ContextRequired)
330 #define RegisterConditional(Name, nParams, CondF, ContextRequired) \
331 RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, NULL, ContextRequired)
336 * @brief register a string that will represent a long value
337 * this will allow to resolve <?...(#"Name")> to Value; that way
338 * plain strings can be used an lexed in templates without having the
339 * lookup overhead at runtime.
340 * @param Name The name of the define
341 * @param len length of Name
342 * @param Value the value to associate with Name
344 void RegisterTokenParamDefine(const char *Name, long len,
347 * teh r0x0r! forward your favourite define from C to the templates with one easy call!
349 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
353 * @brief retrieve the long value of a registered string define
354 * @param Name The name of the define
355 * @param len length of Name
356 * @param Value the value to return if not found
358 long GetTokenDefine(const char *Name,
364 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
365 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, NULL, g, h, i)
366 #define RegisterFilteredIterator(a, b, c, d, e, f, g, h, i, j) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i, j)
367 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
368 int AdditionalParams, /* do we use more parameters? */
369 HashList *StaticList, /* pointer to webcit lifetime hashlists */
370 RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
371 SubTemplFunc DoSubTempl, /* call this function on each iteration for svput & friends */
372 HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
373 FilterByParamFunc Filter, /* use this function if you want to skip items */
374 CtxType ContextType, /* which context do we provide to the subtemplate? */
375 CtxType XPectContextType, /* which context do we expct to be called in? */
380 void StackDynamicContext(WCTemplputParams *Super,
381 WCTemplputParams *Sub,
385 WCTemplateToken *Tokens,
386 WCConditionalFunc ExitCtx,
389 #define StackContext(Super, Sub, Context, ContextType, nArgs, Tokens) \
390 StackDynamicContext(Super, Sub, Context, ContextType, nArgs, Tokens, NULL, 0)
393 void UnStackContext(WCTemplputParams *Sub);
397 CompareFunc RetrieveSort(WCTemplputParams *TP,
398 const char *OtherPrefix, long OtherPrefixLen,
399 const char *Default, long ldefault,
400 long DefaultDirection);
401 void RegisterSortFunc(const char *name, long len,
402 const char *prepend, long preplen,
405 CompareFunc GroupChange,
406 CtxType ContextType);
408 void dbg_print_longvector(long *LongVector);
410 #define do_template(a) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx)
411 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
412 void url_do_template(void);
415 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
416 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);