* add 'control context'; which will carry information from control elements into...
[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 typedef struct _contexts {
34         int ContextType;                /* do we require a context type? */
35         int ControlContextType;
36         int nMinArgs;
37         int nMaxArgs;
38 } ContextFilter;
39
40
41 typedef struct WCTemplateToken WCTemplateToken;
42 typedef struct WCTemplputParams WCTemplputParams;
43 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
44
45 typedef struct _TemplateParam {
46         const char *Start;
47         int Type;
48         long len;
49         long lvalue;
50 } TemplateParam;
51
52 /* make a template token a lookup key: */
53 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
54 struct WCTemplateToken {
55         const StrBuf *FileName; /* Reference to print error messages; not to be freed */
56         StrBuf *FlatToken;
57         long Line;
58         const char *pTokenStart;
59         size_t TokenStart;
60         size_t TokenEnd;
61         const char *pTokenEnd;
62         int Flags;
63         void *PreEval;
64
65         const char *pName;
66         size_t NameEnd;
67
68         int HaveParameters;
69         int nParameters;
70         TemplateParam *Params[MAXPARAM];
71 };
72
73
74 /*
75  * \brief Dynamic content for variable substitution in templates
76  */
77 typedef struct _wcsubst {
78         ContextFilter Filter;
79         int wcs_type;                       /* which type of Substitution are we */
80         char wcs_key[32];                   /* copy of our hashkey for debugging */
81         StrBuf *wcs_value;                  /* if we're a string, keep it here */
82         long lvalue;                        /* type long? keep data here */
83         WCHandlerFunc wcs_function; /* funcion hook ???*/
84 } wcsubst;
85
86 struct WCTemplputParams {
87         ContextFilter Filter;
88         void *Context;
89         void *ControlContext;
90         int nArgs;
91         WCTemplateToken *Tokens;
92 };
93
94
95 extern WCTemplputParams NoCtx;
96
97 #define CTX TP->Context
98 #define CCTX TP->ControlContext
99
100
101
102 #define CTX_NONE 0
103 #define CTX_SITECFG 1
104 #define CTX_SESSION 2
105 #define CTX_INETCFG 3
106 #define CTX_VNOTE 4
107 #define CTX_WHO 5
108 #define CTX_PREF 6
109 #define CTX_NODECONF 7
110 #define CTX_USERLIST 8
111 #define CTX_MAILSUM 9
112 #define CTX_MIME_ATACH 10
113 #define CTX_FILELIST 11
114 #define CTX_STRBUF 12
115 #define CTX_LONGVECTOR 13
116 #define CTX_ROOMS 14
117 #define CTX_FLOORS 15
118 #define CTX_ITERATE 16
119
120 void RegisterNS(const char *NSName, long len, 
121                 int nMinArgs, 
122                 int nMaxArgs, 
123                 WCHandlerFunc HandlerFunc,
124                 int ContextRequired);
125 #define RegisterNamespace(a, b, c, d, e) RegisterNS(a, sizeof(a)-1, b, c, d, e)
126
127 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
128 typedef struct _ConditionalStruct {
129         ContextFilter Filter;
130         const char *PlainName;
131         int nParams;
132         WCConditionalFunc CondF;
133 } ConditionalStruct;
134 void RegisterConditional(const char *Name, long len, 
135                          int nParams,
136                          WCConditionalFunc CondF, 
137                          int ContextRequired);
138
139
140
141 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
142 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
143 typedef void (*HashDestructorFunc) (HashList **KillMe);
144 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
145                       int AdditionalParams,       /* doe we use more parameters? */
146                       HashList *StaticList,       /* pointer to webcit lifetime hashlists */
147                       RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
148                       SubTemplFunc DoSubTempl,       /* call this function on each iteration for svput & friends */
149                       HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
150                       int ContextType,               /* which context do we provide to the subtemplate? */
151                       int XPectContextType,          /* which context do we expct to be called in? */
152                       int Flags);
153
154 #define IT_NOFLAG 0
155 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
156
157 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
158
159 void GetTemplateTokenString(WCTemplputParams *TP,
160                             int N,
161                             const char **Value, 
162                             long *len);
163
164
165 void SVPut(char *keyname, size_t keylen, int keytype, char *Data);
166 #define svput(a, b, c) SVPut(a, sizeof(a) - 1, b, c)
167 void SVPutLong(char *keyname, size_t keylen, long Data);
168 #define svputlong(a, b) SVPutLong(a, sizeof(a) - 1, b)
169 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...) __attribute__((__format__(__printf__,4,5)));
170 void SVPRINTF(char *keyname, int keytype, const char *format,...) __attribute__((__format__(__printf__,3,4)));
171 void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr);
172 void SVCallback(char *keyname, size_t keylen,  WCHandlerFunc fcn_ptr);
173 #define svcallback(a, b) SVCallback(a, sizeof(a) - 1, b)
174
175 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref);
176 #define SVPutBuf(a, b, c); SVPUTBuf(a, sizeof(a) - 1, b, c)
177
178 void DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
179 #define do_template(a, b) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx);
180 void url_do_template(void);
181
182 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
183 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);
184
185 void StrBufAppendTemplate(StrBuf *Target, 
186                           WCTemplputParams *TP,
187                           const StrBuf *Source, 
188                           int FormatTypeIndex);
189 CompareFunc RetrieveSort(WCTemplputParams *TP, 
190                          const char *OtherPrefix, long OtherPrefixLen,  
191                          const char *Default, long ldefault, 
192                          long DefaultDirection);
193 void RegisterSortFunc(const char *name, long len, 
194                       const char *prepend, long preplen,
195                       CompareFunc Forward, 
196                       CompareFunc Reverse, 
197                       CompareFunc GroupChange, 
198                       long ContextType);
199
200 void dbg_print_longvector(long *LongVector);
201
202
203 #define ERR_NAME 0
204 #define ERR_PARM1 1
205 #define ERR_PARM2 2
206 void LogTemplateError (StrBuf *Target, 
207                        const char *Type, 
208                        int ErrorPos, 
209                        WCTemplputParams *TP, 
210                        const char *Format, ...);