3 * Copyright (c) 1996-2013 by the citadel.org team
5 * This program is open source software. You can redistribute it and/or
6 * modify it under the terms of the GNU General Public License, version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * subst template processing functions
16 extern HashList *Conditionals;
17 extern HashList *GlobalNS;
18 extern HashList *Iterators;
19 extern HashList *WirelessTemplateCache;
20 extern HashList *WirelessLocalTemplateCache;
21 extern HashList *TemplateCache;
22 extern HashList *LocalTemplateCache;
27 #define TYPE_PREFSTR 3
28 #define TYPE_ROOMPREFSTR 4
29 #define TYPE_PREFINT 5
30 #define TYPE_GETTEXT 6
32 #define TYPE_SUBTEMPLATE 8
33 #define TYPE_INTDEFINE 9
36 #define IS_NUMBER(a) ((a == TYPE_LONG) || (a == TYPE_PREFINT) || (a == TYPE_INTDEFINE))
39 * \brief Values for wcs_type
42 WCS_STRING, /* its a string */
43 WCS_FUNCTION, /* its a function callback */
44 WCS_SERVCMD, /* its a command to send to the citadel server */
45 WCS_STRBUF, /* its a strbuf we own */
46 WCS_STRBUF_REF, /* its a strbuf we mustn't free */
47 WCS_LONG /* its an integer */
53 typedef struct __CtxTypeStruct {
58 CtxTypeStruct *GetContextType(CtxType Type);
59 void RegisterContextType(const char *name, long len, CtxType * TheCtx);
60 #define RegisterCTX(a) RegisterContextType(#a, sizeof(#a) - 1, &a)
62 extern CtxType CTX_STRBUF;
63 extern CtxType CTX_STRBUFARR;
64 extern CtxType CTX_LONGVECTOR;
68 * ContextFilter resembles our RTTI information. With this structure
69 * we can make shure a tmplput function can live with the environment
71 * if not, we will log/print an error and refuse to call it.
73 typedef struct _contexts {
74 CtxType ContextType; /* do we require a User Context ? */
75 int nMinArgs; /* How many arguments do we need at least? */
76 int nMaxArgs; /* up to how many arguments can we handle? */
80 /* Forward declarations... */
81 typedef struct WCTemplateToken WCTemplateToken;
82 typedef struct WCTemplputParams WCTemplputParams;
84 /* this is the signature of a tmplput function */
85 typedef void (*WCHandlerFunc)(StrBuf * Target, WCTemplputParams * TP);
87 /* if you want to pre-evaluate parts of your token, or do additional syntax, use this. */
88 typedef int (*WCPreevalFunc)(WCTemplateToken * Token);
90 /* make a template token a lookup key: */
91 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
93 void *GetContextPayload(WCTemplputParams * TP, CtxType ContextType);
94 #define CTX(a) GetContextPayload(TP, a)
98 * this is the signature of a conditional function
99 * Note: Target is just passed in for error messages; don't write onto it in regular cases.
101 typedef int (*WCConditionalFunc)(StrBuf * Target, WCTemplputParams * TP);
103 typedef enum _eBitMask {
109 typedef struct _TemplateParam {
110 /* are we a string or a number? */
115 /* if we're a number: */
123 * Representation of a token; everything thats inbetween <? and >
125 struct WCTemplateToken {
126 /* Reference to the filename we're in to print error messages; not to be freed */
127 const StrBuf *FileName;
128 /* Raw copy of our original token; for error printing */
130 /* Which line did the template parser pick us up in? For error printing */
133 /* our position in the template cache buffer */
134 const char *pTokenStart;
135 /* our token length */
139 const char *pTokenEnd;
140 /* just our token name: */
144 /* stuff the pre-evaluater finds out: */
146 /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
150 /* if we have parameters here we go: */
151 /* do we have parameters or not? */
153 /* How many of them? */
156 TemplateParam *Params[MAXPARAM];
161 struct WCTemplputParams {
162 ContextFilter Filter;
165 WCTemplateToken *Tokens;
166 WCTemplputParams *Sub, *Super;
167 WCConditionalFunc ExitCtx;
173 typedef struct _ConditionalStruct {
174 ContextFilter Filter;
175 const char *PlainName;
176 WCConditionalFunc CondF;
177 WCConditionalFunc CondExitCtx;
181 typedef void (*SubTemplFunc)(StrBuf * TemplBuffer, WCTemplputParams * TP);
182 typedef HashList *(*RetrieveHashlistFunc)(StrBuf * Target, WCTemplputParams * TP);
183 typedef void (*HashDestructorFunc)(HashList ** KillMe);
184 typedef int (*FilterByParamFunc)(const char *key, long len, void *Context, StrBuf * Target, WCTemplputParams * TP);
186 extern WCTemplputParams NoCtx;
188 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
197 * @brief log an error while evaluating a token; print it to the actual template
198 * @param Target your Target Buffer to print the error message next to the log
199 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
200 * @param TP grab our set of default information here
201 * @param Format for the custom error message
203 void LogTemplateError(StrBuf * Target,
205 int ErrorPos, WCTemplputParams * TP, 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, WCTemplputParams * TP, int N, const char **Value, long *len);
235 * @return whether @ref GetTemplateTokenString would be able to give you a string
237 int HaveTemplateTokenString(StrBuf * Target, WCTemplputParams * TP, int N, const char **Value, long *len);
243 * @brief get the actual integer value of a token parameter
244 * in your tmplputs or conditionals use this function to access parameters that can also be
245 * retrieved from dynamic facilities:
246 * _ -> Gettext; retrieve this token from the i18n facilities
247 * : -> lookup a setting of that name
248 * B -> bstr; an URL-Parameter
249 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
251 * @param N which token do you want to lookup?
252 * @param dflt default value to be retrieved if not found in preferences
253 * \returns the long value
255 long GetTemplateTokenNumber(StrBuf * Target, WCTemplputParams * TP, int N, long dflt);
258 * @brief put a token value into the template
259 * use this function to append your strings into a Template.
260 * it can escape your string according to the token at FormattypeIndex:
261 * H: de-QP and utf8-ify
262 * X: escapize for HTML
264 * @param Target the destination buffer
265 * @param TP the template token information
266 * @param Source string to append
267 * @param FormatTypeIndex which parameter contains the escaping functionality?
268 * if this token doesn't have as much parameters, plain append is done.
270 void StrBufAppendTemplate(StrBuf * Target, WCTemplputParams * TP, const StrBuf * Source, int FormatTypeIndex);
272 void StrBufAppendTemplateStr(StrBuf * Target, WCTemplputParams * TP, const char *Source, int FormatTypeIndex);
275 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
279 * @brief register a template token handler
280 * call this function in your InitModule_MODULENAME which will be called at the server start
281 * @param nMinArgs how much parameters does your token require at least?
282 * @param nMaxArgs how many parameters does your token accept?
283 * @param HandlerFunc your callback when the template is rendered and your token is there
284 * @param PreEvalFunc is called when the template is parsed; you can do additional
285 * syntax checks here or pre-evaluate stuff for better performance
286 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
288 void RegisterNS(const char *NSName, long len,
289 int nMinArgs, int nMaxArgs, WCHandlerFunc HandlerFunc, WCPreevalFunc PreEvalFunc, int ContextRequired);
293 * @brief register a conditional token <pair> handler
294 * call this function in your InitModule_MODULENAME which will be called at the server start
295 * conditionals can be ? or ! with a pair or % similar to an implicit if
296 * @param Name whats the name of your conditional? should start with COND:
297 * @param len the token length so we don't have to measure it.
298 * @param nParams how many parameters does your conditional need on top of the default conditional parameters
299 * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
300 * @param ExitCtxCond if non-NULL, will be called after the area of the conditional is left behind.
301 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
303 void RegisterContextConditional(const char *Name, long len,
304 int nParams, WCConditionalFunc CondF, WCConditionalFunc ExitCtxCond, int ContextRequired);
306 #define RegisterCtxConditional(Name, nParams, CondF, ExitCtxCond, ContextRequired) \
307 RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, ExitCtxCond, ContextRequired)
309 #define RegisterConditional(Name, nParams, CondF, ContextRequired) \
310 RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, NULL, ContextRequired)
315 * @brief register a string that will represent a long value
316 * this will allow to resolve <?...(#"Name")> to Value; that way
317 * plain strings can be used an lexed in templates without having the
318 * lookup overhead at runtime.
319 * @param Name The name of the define
320 * @param len length of Name
321 * @param Value the value to associate with Name
323 void RegisterTokenParamDefine(const char *Name, long len, long Value);
326 * teh r0x0r! forward your favourite define from C to the templates with one easy call!
328 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
332 * @brief retrieve the long value of a registered string define
333 * @param Name The name of the define
334 * @param len length of Name
335 * @param Value the value to return if not found
337 long GetTokenDefine(const char *Name, long len, long DefValue);
341 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
342 #define IT_ADDT_PARAM(n) 5 + n /* If you have AdditionalParams, use this macro to fetch them. */
343 #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)
344 #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)
345 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
346 int AdditionalParams, /* do we use more parameters? */
347 HashList * StaticList, /* pointer to webcit lifetime hashlists */
348 RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
349 SubTemplFunc DoSubTempl, /* call this function on each iteration for svput & friends */
350 HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
351 FilterByParamFunc Filter, /* use this function if you want to skip items */
352 CtxType ContextType, /* which context do we provide to the subtemplate? */
353 CtxType XPectContextType, /* which context do we expct to be called in? */
358 void StackDynamicContext(WCTemplputParams * Super,
359 WCTemplputParams * Sub,
361 CtxType ContextType, int nArgs, WCTemplateToken * Tokens, WCConditionalFunc ExitCtx, long ExitCTXID);
363 #define StackContext(Super, Sub, Context, ContextType, nArgs, Tokens) \
364 StackDynamicContext(Super, Sub, Context, ContextType, nArgs, Tokens, NULL, 0)
367 void UnStackContext(WCTemplputParams * Sub);
371 CompareFunc RetrieveSort(WCTemplputParams * TP,
372 const char *OtherPrefix, long OtherPrefixLen, const char *Default, long ldefault, long DefaultDirection);
373 void RegisterSortFunc(const char *name, long len,
374 const char *prepend, long preplen,
375 CompareFunc Forward, CompareFunc Reverse, CompareFunc GroupChange, CtxType ContextType);
377 void dbg_print_longvector(long *LongVector);
379 #define do_template(a) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx)
380 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf * Target, WCTemplputParams * TP);
381 void url_do_template(void);
384 int CompareSubstToToken(TemplateParam * ParamToCompare, TemplateParam * ParamToLookup);
385 int CompareSubstToStrBuf(StrBuf * Compare, TemplateParam * ParamToLookup);