indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / subst.h
1
2 /*
3  * Copyright (c) 1996-2013 by the citadel.org team
4  *
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.
7  *
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.
12  *
13  * subst template processing functions
14  */
15
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;
23
24
25 #define TYPE_STR   1
26 #define TYPE_LONG  2
27 #define TYPE_PREFSTR 3
28 #define TYPE_ROOMPREFSTR 4
29 #define TYPE_PREFINT 5
30 #define TYPE_GETTEXT 6
31 #define TYPE_BSTR 7
32 #define TYPE_SUBTEMPLATE 8
33 #define TYPE_INTDEFINE 9
34 #define MAXPARAM  25
35
36 #define IS_NUMBER(a) ((a == TYPE_LONG) || (a == TYPE_PREFINT) || (a == TYPE_INTDEFINE))
37
38 /*
39  * \brief Values for wcs_type
40  */
41 enum {
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 */
48 };
49
50 #define CTX_NONE 0
51
52 typedef int CtxType;
53 typedef struct __CtxTypeStruct {
54         CtxType Type;
55         StrBuf *Name;
56 } CtxTypeStruct;
57
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)
61
62 extern CtxType CTX_STRBUF;
63 extern CtxType CTX_STRBUFARR;
64 extern CtxType CTX_LONGVECTOR;
65
66 /**
67  * @ingroup subst
68  * ContextFilter resembles our RTTI information. With this structure
69  * we can make shure a tmplput function can live with the environment
70  * we call it in.
71  * if not, we will log/print an error and refuse to call it.
72  */
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? */
77 } ContextFilter;
78
79
80 /* Forward declarations... */
81 typedef struct WCTemplateToken WCTemplateToken;
82 typedef struct WCTemplputParams WCTemplputParams;
83
84 /* this is the signature of a tmplput function */
85 typedef void (*WCHandlerFunc)(StrBuf * Target, WCTemplputParams * TP);
86
87 /* if you want to pre-evaluate parts of your token, or do additional syntax, use this. */
88 typedef int (*WCPreevalFunc)(WCTemplateToken * Token);
89
90 /* make a template token a lookup key: */
91 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
92
93 void *GetContextPayload(WCTemplputParams * TP, CtxType ContextType);
94 #define CTX(a) GetContextPayload(TP, a)
95
96 /**
97  * @ingroup subst
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.
100  */
101 typedef int (*WCConditionalFunc)(StrBuf * Target, WCTemplputParams * TP);
102
103 typedef enum _eBitMask {
104         eNO = 0,
105         eOR,
106         eAND
107 } eBitMask;
108
109 typedef struct _TemplateParam {
110         /* are we a string or a number? */
111         int Type;
112         /* string data: */
113         const char *Start;
114         long len;
115         /* if we're a number: */
116         long lvalue;
117         eBitMask MaskBy;
118 } TemplateParam;
119
120
121 /**
122  * @ingroup subst
123  * Representation of a token; everything thats inbetween <? and >
124  */
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 */
129         StrBuf *FlatToken;
130         /* Which line did the template parser pick us up in? For error printing */
131         long Line;
132
133         /* our position in the template cache buffer */
134         const char *pTokenStart;
135         /* our token length */
136         size_t TokenStart;
137         size_t TokenEnd;
138         /* point after us */
139         const char *pTokenEnd;
140         /* just our token name: */
141         const char *pName;
142         size_t NameEnd;
143
144         /* stuff the pre-evaluater finds out: */
145         int Flags;
146         /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
147         void *PreEval;
148         void *Preeval2;
149
150         /* if we have parameters here we go: */
151         /* do we have parameters or not? */
152         int HaveParameters;
153         /* How many of them? */
154         int nParameters;
155         /* the parameters */
156         TemplateParam *Params[MAXPARAM];
157 };
158
159
160
161 struct WCTemplputParams {
162         ContextFilter Filter;
163         void *Context;
164         int nArgs;
165         WCTemplateToken *Tokens;
166         WCTemplputParams *Sub, *Super;
167         WCConditionalFunc ExitCtx;
168         long ExitCTXID;
169 };
170
171
172
173 typedef struct _ConditionalStruct {
174         ContextFilter Filter;
175         const char *PlainName;
176         WCConditionalFunc CondF;
177         WCConditionalFunc CondExitCtx;
178 } ConditionalStruct;
179
180
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);
185
186 extern WCTemplputParams NoCtx;
187
188 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
189
190
191 #define ERR_NAME 0
192 #define ERR_PARM1 1
193 #define ERR_PARM2 2
194
195 /**
196  * @ingroup subst
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
202  */
203 void LogTemplateError(StrBuf * Target,
204                       const char *Type,
205                       int ErrorPos, WCTemplputParams * TP, const char *Format, ...) __attribute__((__format__(__printf__, 5, 6)));
206
207
208 /**
209  * @ingroup subst
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
214  */
215 void LogError(StrBuf * Target, const char *Type, const char *Format, ...);
216
217 /**
218  * @ingroup subst
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 
226  * 
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
230  */
231 void GetTemplateTokenString(StrBuf * Target, WCTemplputParams * TP, int N, const char **Value, long *len);
232
233 /**
234  * @ingroup subst
235  * @return whether @ref GetTemplateTokenString would be able to give you a string
236  */
237 int HaveTemplateTokenString(StrBuf * Target, WCTemplputParams * TP, int N, const char **Value, long *len);
238
239
240
241 /**
242  * @ingroup subst
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 
250  * 
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
254  */
255 long GetTemplateTokenNumber(StrBuf * Target, WCTemplputParams * TP, int N, long dflt);
256
257 /**
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
263  *  J: JSON Escapize
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.
269  */
270 void StrBufAppendTemplate(StrBuf * Target, WCTemplputParams * TP, const StrBuf * Source, int FormatTypeIndex);
271
272 void StrBufAppendTemplateStr(StrBuf * Target, WCTemplputParams * TP, const char *Source, int FormatTypeIndex);
273
274
275 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
276
277 /**
278  * @ingroup subst
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.
287  */
288 void RegisterNS(const char *NSName, long len,
289                 int nMinArgs, int nMaxArgs, WCHandlerFunc HandlerFunc, WCPreevalFunc PreEvalFunc, int ContextRequired);
290
291 /**
292  * @ingroup subst
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.
302  */
303 void RegisterContextConditional(const char *Name, long len,
304                                 int nParams, WCConditionalFunc CondF, WCConditionalFunc ExitCtxCond, int ContextRequired);
305
306 #define RegisterCtxConditional(Name, nParams, CondF, ExitCtxCond, ContextRequired) \
307         RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, ExitCtxCond, ContextRequired)
308
309 #define RegisterConditional(Name, nParams, CondF, ContextRequired) \
310         RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, NULL, ContextRequired)
311
312
313 /**
314  * @ingroup subst
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
322  */
323 void RegisterTokenParamDefine(const char *Name, long len, long Value);
324
325 /**
326  * teh r0x0r! forward your favourite define from C to the templates with one easy call!
327  */
328 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
329
330 /**
331  * @ingroup subst
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
336  */
337 long GetTokenDefine(const char *Name, long len, long DefValue);
338
339
340 #define IT_NOFLAG 0
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? */
354                       int Flags);
355
356
357
358 void StackDynamicContext(WCTemplputParams * Super,
359                          WCTemplputParams * Sub,
360                          void *Context,
361                          CtxType ContextType, int nArgs, WCTemplateToken * Tokens, WCConditionalFunc ExitCtx, long ExitCTXID);
362
363 #define StackContext(Super, Sub, Context, ContextType, nArgs, Tokens) \
364         StackDynamicContext(Super, Sub, Context, ContextType, nArgs, Tokens, NULL, 0)
365
366
367 void UnStackContext(WCTemplputParams * Sub);
368
369
370
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);
376
377 void dbg_print_longvector(long *LongVector);
378
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);
382
383
384 int CompareSubstToToken(TemplateParam * ParamToCompare, TemplateParam * ParamToLookup);
385 int CompareSubstToStrBuf(StrBuf * Compare, TemplateParam * ParamToLookup);