File: subst.h

package info (click to toggle)
webcit 917-dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,164 kB
  • sloc: ansic: 34,213; sh: 4,455; makefile: 346; xml: 91; sed: 9
file content (417 lines) | stat: -rw-r--r-- 14,303 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * Copyright (c) 1996-2013 by the citadel.org team
 *
 * This program is open source software.  You can redistribute it and/or
 * modify it under the terms of the GNU General Public License, version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * subst template processing functions
 */

extern HashList *Conditionals;
extern HashList *GlobalNS;
extern HashList *Iterators;
extern HashList *WirelessTemplateCache;
extern HashList *WirelessLocalTemplateCache;
extern HashList *TemplateCache;
extern HashList *LocalTemplateCache;


#define TYPE_STR   1
#define TYPE_LONG  2
#define TYPE_PREFSTR 3
#define TYPE_ROOMPREFSTR 4
#define TYPE_PREFINT 5
#define TYPE_GETTEXT 6
#define TYPE_BSTR 7
#define TYPE_SUBTEMPLATE 8
#define TYPE_INTDEFINE 9
#define MAXPARAM  25

#define IS_NUMBER(a) ((a == TYPE_LONG) || (a == TYPE_PREFINT) || (a == TYPE_INTDEFINE))

/*
 * \brief Values for wcs_type
 */
enum {
	WCS_STRING,       /* its a string */
	WCS_FUNCTION,     /* its a function callback */
	WCS_SERVCMD,      /* its a command to send to the citadel server */
	WCS_STRBUF,       /* its a strbuf we own */
	WCS_STRBUF_REF,   /* its a strbuf we mustn't free */
	WCS_LONG          /* its an integer */
};

#define CTX_NONE 0

typedef int CtxType;
typedef struct __CtxTypeStruct {
	CtxType Type;
	StrBuf *Name;
} CtxTypeStruct;

CtxTypeStruct *GetContextType(CtxType Type);
void RegisterContextType(const char *name, long len, CtxType *TheCtx);
#define RegisterCTX(a) RegisterContextType(#a, sizeof(#a) - 1, &a)

extern CtxType CTX_STRBUF;
extern CtxType CTX_STRBUFARR;
extern CtxType CTX_LONGVECTOR;

/**
 * @ingroup subst
 * ContextFilter resembles our RTTI information. With this structure
 * we can make shure a tmplput function can live with the environment
 * we call it in.
 * if not, we will log/print an error and refuse to call it.
 */
typedef struct _contexts {
	CtxType ContextType;                /* do we require a User Context ? */
	int nMinArgs;                   /* How many arguments do we need at least? */
	int nMaxArgs;                   /* up to how many arguments can we handle? */
} ContextFilter;


/* Forward declarations... */
typedef struct WCTemplateToken WCTemplateToken;
typedef struct WCTemplputParams WCTemplputParams;

/* this is the signature of a tmplput function */
typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);

/* if you want to pre-evaluate parts of your token, or do additional syntax, use this. */ 
typedef int (*WCPreevalFunc)(WCTemplateToken *Token);

/* make a template token a lookup key: */
#define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len

void *GetContextPayload(WCTemplputParams *TP, CtxType ContextType);
#define CTX(a) GetContextPayload(TP, a)

/**
 * @ingroup subst
 * this is the signature of a conditional function 
 * Note: Target is just passed in for error messages; don't write onto it in regular cases.
 */
typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);

typedef enum _eBitMask {
	eNO = 0,
	eOR,
	eAND
}eBitMask;

typedef struct _TemplateParam {
        /* are we a string or a number? */
	int Type;
	/* string data: */
	const char *Start;
	long len;
	/* if we're a number: */
	long lvalue;
	eBitMask MaskBy;
} TemplateParam;


/**
 * @ingroup subst
 * Representation of a token; everything thats inbetween <? and >
 */ 
struct WCTemplateToken {
        /* Reference to the filename we're in to print error messages; not to be freed */
	const StrBuf *FileName; 
	/* Raw copy of our original token; for error printing */
	StrBuf *FlatToken;
	/* Which line did the template parser pick us up in? For error printing */
	long Line;

	/* our position in the template cache buffer */
	const char *pTokenStart;
	/* our token length */
	size_t TokenStart;
	size_t TokenEnd;
	/* point after us */
	const char *pTokenEnd;
	/* just our token name: */
	const char *pName;
	size_t NameEnd;

	/* stuff the pre-evaluater finds out: */
	int Flags;
	/* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
	void *PreEval;
	void *Preeval2;

	/* if we have parameters here we go: */
	/* do we have parameters or not? */
	int HaveParameters;
	/* How many of them? */
	int nParameters;
	/* the parameters */
	TemplateParam *Params[MAXPARAM];
};



struct WCTemplputParams {
	ContextFilter Filter;
	void *Context;
	int nArgs;
	WCTemplateToken *Tokens;
	WCTemplputParams *Sub, *Super;
	WCConditionalFunc ExitCtx;
        long ExitCTXID;
};



typedef struct _ConditionalStruct {
	ContextFilter Filter;
	const char *PlainName;
	WCConditionalFunc CondF;
	WCConditionalFunc CondExitCtx;
} ConditionalStruct;


typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
typedef void (*HashDestructorFunc) (HashList **KillMe);
typedef int (*FilterByParamFunc)(const char* key, long len, void *Context, StrBuf *Target, WCTemplputParams *TP);

extern WCTemplputParams NoCtx;

#define HAVE_PARAM(a) (TP->Tokens->nParameters > a)


#define ERR_NAME 0
#define ERR_PARM1 1
#define ERR_PARM2 2
/**
 * @ingroup subst
 * @brief log an error while evaluating a token; print it to the actual template 
 * @param Target your Target Buffer to print the error message next to the log
 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
 * @param TP grab our set of default information here
 * @param Format for the custom error message
 */ 
void LogTemplateError (StrBuf *Target, 
		       const char *Type, 
		       int ErrorPos, 
		       WCTemplputParams *TP, 
		       const char *Format, ...)__attribute__((__format__(__printf__,5,6)));


/**
 * @ingroup subst
 * @brief log an error while in global context; print it to Wildfire / Target
 * @param Target your Target Buffer to print the error message next to the log
 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
 * @param Format for the custom error message
 */ 
void LogError (StrBuf *Target, const char *Type, const char *Format, ...);

/**
 * @ingroup subst
 * @brief get the actual value of a token parameter
 * in your tmplputs or conditionals use this function to access parameters that can also be 
 * retrieved from dynamic facilities:
 *  _ -> Gettext; retrieve this token from the i18n facilities
 *  : -> lookup a setting of that name
 *  B -> bstr; an URL-Parameter
 *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
 * 
 * @param N which token do you want to lookup?
 * @param Value reference to the string of the token; don't free me.
 * @param len the length of Value
 */
void GetTemplateTokenString(StrBuf *Target, 
			    WCTemplputParams *TP,
			    int N,
			    const char **Value, 
			    long *len);
/**
 * @ingroup subst
 * @return whether @ref GetTemplateTokenString would be able to give you a string
 */
int HaveTemplateTokenString(StrBuf *Target, 
			    WCTemplputParams *TP,
			    int N,
			    const char **Value, 
			    long *len);



/**
 * @ingroup subst
 * @brief get the actual integer value of a token parameter
 * in your tmplputs or conditionals use this function to access parameters that can also be 
 * retrieved from dynamic facilities:
 *  _ -> Gettext; retrieve this token from the i18n facilities
 *  : -> lookup a setting of that name
 *  B -> bstr; an URL-Parameter
 *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
 * 
 * @param N which token do you want to lookup?
 * @param dflt default value to be retrieved if not found in preferences
 * \returns the long value
 */
long GetTemplateTokenNumber(StrBuf *Target, 
			    WCTemplputParams *TP, 
			    int N, long dflt);

/**
 * @brief put a token value into the template
 * use this function to append your strings into a Template. 
 * it can escape your string according to the token at FormattypeIndex:
 *  H: de-QP and utf8-ify
 *  X: escapize for HTML
 *  J: JSON Escapize
 * @param Target the destination buffer
 * @param TP the template token information
 * @param Source string to append
 * @param FormatTypeIndex which parameter contains the escaping functionality?
 *        if this token doesn't have as much parameters, plain append is done.
 */
void StrBufAppendTemplate(StrBuf *Target, 
			  WCTemplputParams *TP,
			  const StrBuf *Source, 
			  int FormatTypeIndex);

void StrBufAppendTemplateStr(StrBuf *Target, 
			     WCTemplputParams *TP,
			     const char *Source, 
			     int FormatTypeIndex);


#define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
/**
 * @ingroup subst
 * @brief register a template token handler
 * call this function in your InitModule_MODULENAME which will be called at the server start
 * @param nMinArgs how much parameters does your token require at least?
 * @param nMaxArgs how many parameters does your token accept?
 * @param HandlerFunc your callback when the template is rendered and your token is there 
 * @param PreEvalFunc is called when the template is parsed; you can do additional 
 *        syntax checks here or pre-evaluate stuff for better performance
 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
 */
void RegisterNS(const char *NSName, long len, 
		int nMinArgs, 
		int nMaxArgs, 
		WCHandlerFunc HandlerFunc,
		WCPreevalFunc PreEvalFunc,
		int ContextRequired);

/**
 * @ingroup subst
 * @brief register a conditional token <pair> handler
 * call this function in your InitModule_MODULENAME which will be called at the server start
 * conditionals can be ? or ! with a pair or % similar to an implicit if
 * @param Name whats the name of your conditional? should start with COND:
 * @param len the token length so we don't have to measure it.
 * @param nParams how many parameters does your conditional need on top of the default conditional parameters
 * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
 * @param ExitCtxCond if non-NULL, will be called after the area of the conditional is left behind.
 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
 */
void RegisterContextConditional(const char *Name, long len, 
				int nParams,
				WCConditionalFunc CondF, 
				WCConditionalFunc ExitCtxCond,
				int ContextRequired);

#define RegisterCtxConditional(Name, nParams, CondF, ExitCtxCond, ContextRequired) \
	RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, ExitCtxCond, ContextRequired)

#define RegisterConditional(Name, nParams, CondF, ContextRequired) \
	RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, NULL, ContextRequired)


/**
 * @ingroup subst
 * @brief register a string that will represent a long value
 * this will allow to resolve <?...(#"Name")> to Value; that way 
 * plain strings can be used an lexed in templates without having the 
 * lookup overhead at runtime.
 * @param Name The name of the define
 * @param len length of Name
 * @param Value the value to associate with Name
 */
void RegisterTokenParamDefine(const char *Name, long len, 
			      long Value);
/**
 * teh r0x0r! forward your favourite define from C to the templates with one easy call!
 */
#define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);

/**
 * @ingroup subst
 * @brief retrieve the long value of a registered string define
 * @param Name The name of the define
 * @param len length of Name
 * @param Value the value to return if not found
 */
long GetTokenDefine(const char *Name, 
		    long len, 
		    long DefValue);


#define IT_NOFLAG 0
#define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
#define IT_ADDT_PARAM(n) 5 + n /* If you have AdditionalParams, use this macro to fetch them. */
#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)
#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)
void RegisterITERATOR(const char *Name, long len, /* Our identifier */
		      int AdditionalParams,       /* do we use more parameters? */
		      HashList *StaticList,       /* pointer to webcit lifetime hashlists */
		      RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
		      SubTemplFunc DoSubTempl,       /* call this function on each iteration for svput & friends */
		      HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
		      FilterByParamFunc Filter,      /* use this function if you want to skip items */
		      CtxType ContextType,               /* which context do we provide to the subtemplate? */
		      CtxType XPectContextType,          /* which context do we expct to be called in? */
		      int Flags);



void StackDynamicContext(WCTemplputParams *Super, 
			 WCTemplputParams *Sub, 
			 void *Context,
			 CtxType ContextType,
			 int nArgs,
			 WCTemplateToken *Tokens, 
			 WCConditionalFunc ExitCtx,
			 long ExitCTXID);

#define StackContext(Super, Sub, Context, ContextType, nArgs, Tokens) \
	StackDynamicContext(Super, Sub, Context, ContextType, nArgs, Tokens, NULL, 0)


void UnStackContext(WCTemplputParams *Sub);



CompareFunc RetrieveSort(WCTemplputParams *TP, 
			 const char *OtherPrefix, long OtherPrefixLen,  
			 const char *Default, long ldefault, 
			 long DefaultDirection);
void RegisterSortFunc(const char *name, long len, 
		      const char *prepend, long preplen,
		      CompareFunc Forward, 
		      CompareFunc Reverse, 
		      CompareFunc GroupChange, 
		      CtxType ContextType);

void dbg_print_longvector(long *LongVector);

#define do_template(a) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx)
const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
void url_do_template(void);


int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);