* document function calls and structures
[citadel.git] / webcit / subst.h
1
2 extern HashList *Conditionals;
3 extern HashList *GlobalNS;
4 extern HashList *Iterators;
5 extern HashList *WirelessTemplateCache;
6 extern HashList *WirelessLocalTemplateCache;
7 extern HashList *TemplateCache;
8 extern HashList *LocalTemplateCache;
9
10
11 #define TYPE_STR   1
12 #define TYPE_LONG  2
13 #define TYPE_PREFSTR 3
14 #define TYPE_PREFINT 4
15 #define TYPE_GETTEXT 5
16 #define TYPE_BSTR 6
17 #define TYPE_SUBTEMPLATE 7
18 #define MAXPARAM  20
19
20
21 /*
22  * \brief Values for wcs_type
23  */
24 enum {
25         WCS_STRING,       /* its a string */
26         WCS_FUNCTION,     /* its a function callback */
27         WCS_SERVCMD,      /* its a command to send to the citadel server */
28         WCS_STRBUF,       /* its a strbuf we own */
29         WCS_STRBUF_REF,   /* its a strbuf we mustn't free */
30         WCS_LONG          /* its an integer */
31 };
32
33 #define CTX TP->Context
34 #define CCTX TP->ControlContext
35
36 #define CTX_NONE 0
37 #define CTX_SITECFG 1
38 #define CTX_SESSION 2
39 #define CTX_INETCFG 3
40 #define CTX_VNOTE 4
41 #define CTX_WHO 5
42 #define CTX_PREF 6
43 #define CTX_NODECONF 7
44 #define CTX_USERLIST 8
45 #define CTX_MAILSUM 9
46 #define CTX_MIME_ATACH 10
47 #define CTX_FILELIST 11
48 #define CTX_STRBUF 12
49 #define CTX_LONGVECTOR 13
50 #define CTX_ROOMS 14
51 #define CTX_FLOORS 15
52 #define CTX_ITERATE 16
53
54
55 /**
56  * ContextFilter resembles our RTTI information. With this structure
57  * we can make shure a tmplput function can live with the environment
58  * we call it in.
59  * if not, we will log/print an error and refuse to call it.
60  */
61 typedef struct _contexts {
62         int ContextType;                /* do we require a User Context ? */
63         int ControlContextType;         /* are we inside of a control structure? */
64         int nMinArgs;                   /* How many arguments do we need at least? */
65         int nMaxArgs;                   /* up to how many arguments can we handle? */
66 } ContextFilter;
67
68
69 /* Forward declarations... */
70 typedef struct WCTemplateToken WCTemplateToken;
71 typedef struct WCTemplputParams WCTemplputParams;
72
73 /* this is the signature of a tmplput function */
74 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
75
76 /* make a template token a lookup key: */
77 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
78
79 /* TODO: wcsubst should be private! */
80
81 /*
82  * \brief Dynamic content for variable substitution in templates
83  */
84 typedef struct _wcsubst {
85         ContextFilter Filter;
86         int wcs_type;                       /* which type of Substitution are we */
87         char wcs_key[32];                   /* copy of our hashkey for debugging */
88         StrBuf *wcs_value;                  /* if we're a string, keep it here */
89         long lvalue;                        /* type long? keep data here */
90         WCHandlerFunc wcs_function; /* funcion hook ???*/
91 } wcsubst;
92
93
94 /**
95  * this is the signature of a conditional function 
96  * Note: Target is just passed in for error messages; don't write onto it in regular cases.
97  */
98 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
99
100
101 typedef struct _TemplateParam {
102         /* are we a string or a number? */
103         int Type;
104         /* string data: */
105         const char *Start;
106         long len;
107         /* if we're a number: */
108         long lvalue;
109 } TemplateParam;
110
111
112 /**
113  * Representation of a token; everything thats inbetween <? and >
114  */ 
115 struct WCTemplateToken {
116         /* Reference to the filename we're in to print error messages; not to be freed */
117         const StrBuf *FileName; 
118         /* Raw copy of our original token; for error printing */
119         StrBuf *FlatToken;
120         /* Which line did the template parser pick us up in? For error printing */
121         long Line;
122
123         /* our position in the template cache buffer */
124         const char *pTokenStart;
125         /* our token length */
126         size_t TokenStart;
127         size_t TokenEnd;
128         /* point after us */
129         const char *pTokenEnd;
130         /* just our token name: */
131         const char *pName;
132         size_t NameEnd;
133
134         /* stuff the pre-evaluater finds out: */
135         int Flags;
136         /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
137         void *PreEval;
138
139         /* if we have parameters here we go: */
140         /* do we have parameters or not? */
141         int HaveParameters;
142         /* How many of them? */
143         int nParameters;
144         /* the parameters */
145         TemplateParam *Params[MAXPARAM];
146 };
147
148
149
150 struct WCTemplputParams {
151         ContextFilter Filter;
152         void *Context;
153         void *ControlContext;
154         int nArgs;
155         WCTemplateToken *Tokens;
156 };
157
158
159
160 typedef struct _ConditionalStruct {
161         ContextFilter Filter;
162         const char *PlainName;
163         WCConditionalFunc CondF;
164 } ConditionalStruct;
165
166
167
168
169 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
170 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
171 typedef void (*HashDestructorFunc) (HashList **KillMe);
172
173
174 extern WCTemplputParams NoCtx;
175
176
177
178
179 #define ERR_NAME 0
180 #define ERR_PARM1 1
181 #define ERR_PARM2 2
182 /**
183  * \Brief log an error while evaluating a token; print it to the actual template 
184  * \param Target your Target Buffer to print the error message next to the log
185  * \param Type What sort of thing are we talking about? Tokens? Conditionals?
186  * \param TP grab our set of default information here
187  * \param Format for the custom error message
188  */ 
189 void LogTemplateError (StrBuf *Target, 
190                        const char *Type, 
191                        int ErrorPos, 
192                        WCTemplputParams *TP, 
193                        const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
194
195 /**
196  * \Brief get the actual value of a token parameter
197  * in your tmplputs or conditionals use this function to access parameters that can also be 
198  * retrieved from dynamic facilities:
199  *  _ -> Gettext; retrieve this token from the i18n facilities
200  *  : -> lookup a setting of that name
201  *  B -> bstr; an URL-Parameter
202  *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
203  * 
204  * \param N which token do you want to lookup?
205  * \param Value reference to the string of the token; don't free me.
206  * \param len the length of Value
207  */
208 void GetTemplateTokenString(WCTemplputParams *TP,
209                             int N,
210                             const char **Value, 
211                             long *len);
212
213 /**
214  * \Brief put a token value into the template
215  * use this function to append your strings into a Template. 
216  * it can escape your string according to the token at FormattypeIndex:
217  *  H: de-QP and utf8-ify
218  *  X: escapize for HTML
219  *  J: JSON Escapize
220  * \param Target the destination buffer
221  * \param TP the template token information
222  * \param Source string to append
223  * \param FormatTypeIndex which parameter contains the escaping functionality?
224  *        if this token doesn't have as much parameters, plain append is done.
225  */
226 void StrBufAppendTemplate(StrBuf *Target, 
227                           WCTemplputParams *TP,
228                           const StrBuf *Source, 
229                           int FormatTypeIndex);
230
231
232 #define RegisterNamespace(a, b, c, d, e) RegisterNS(a, sizeof(a)-1, b, c, d, e)
233 void RegisterNS(const char *NSName, long len, 
234                 int nMinArgs, 
235                 int nMaxArgs, 
236                 WCHandlerFunc HandlerFunc,
237                 int ContextRequired);
238
239 void RegisterConditional(const char *Name, long len, 
240                          int nParams,
241                          WCConditionalFunc CondF, 
242                          int ContextRequired);
243
244
245
246 #define IT_NOFLAG 0
247 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
248 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
249 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
250                       int AdditionalParams,       /* doe we use more parameters? */
251                       HashList *StaticList,       /* pointer to webcit lifetime hashlists */
252                       RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
253                       SubTemplFunc DoSubTempl,       /* call this function on each iteration for svput & friends */
254                       HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
255                       int ContextType,               /* which context do we provide to the subtemplate? */
256                       int XPectContextType,          /* which context do we expct to be called in? */
257                       int Flags);
258
259
260
261
262
263
264 CompareFunc RetrieveSort(WCTemplputParams *TP, 
265                          const char *OtherPrefix, long OtherPrefixLen,  
266                          const char *Default, long ldefault, 
267                          long DefaultDirection);
268 void RegisterSortFunc(const char *name, long len, 
269                       const char *prepend, long preplen,
270                       CompareFunc Forward, 
271                       CompareFunc Reverse, 
272                       CompareFunc GroupChange, 
273                       long ContextType);
274
275
276
277
278 void dbg_print_longvector(long *LongVector);
279
280
281
282
283
284 #define do_template(a, b) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx);
285 void DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
286 void url_do_template(void);
287
288
289
290
291
292 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
293 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);
294
295 void SVPut(char *keyname, size_t keylen, int keytype, char *Data);
296 #define svput(a, b, c) SVPut(a, sizeof(a) - 1, b, c)
297 void SVPutLong(char *keyname, size_t keylen, long Data);
298 #define svputlong(a, b) SVPutLong(a, sizeof(a) - 1, b)
299 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...) __attribute__((__format__(__printf__,4,5)));
300 void SVPRINTF(char *keyname, int keytype, const char *format,...) __attribute__((__format__(__printf__,3,4)));
301 void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr);
302 void SVCallback(char *keyname, size_t keylen,  WCHandlerFunc fcn_ptr);
303 #define svcallback(a, b) SVCallback(a, sizeof(a) - 1, b)
304
305 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref);
306 #define SVPutBuf(a, b, c); SVPUTBuf(a, sizeof(a) - 1, b, c)