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
|
/* This is the template processing code of rsyslog.
* Please see syslogd.c for license information.
* This code is placed under the GPL.
* begun 2004-11-17 rgerhards
*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "liblogging-stub.h"
#include "stringbuf.h"
#include "template.h"
#include "syslogd.h"
static struct template *tplRoot = NULL; /* the root of the template list */
static struct template *tplLast = NULL; /* points to the last element of the template list */
/* Constructs a template entry object. Returns pointer to it
* or NULL (if it fails). Pointer to associated template list entry
* must be provided.
*/
struct templateEntry* tpeConstruct(struct template *pTpl)
{
assert(pTpl != NULL);
struct templateEntry *pTpe;
if((pTpe = calloc(1, sizeof(struct templateEntry))) == NULL)
return NULL;
/* basic initialisaion is done via calloc() - need to
* initialize only values != 0. */
if(pTpl->pEntryLast == NULL)
{ /* we are the first element! */
pTpl->pEntryRoot = pTpl->pEntryLast = pTpe;
}
else
{
pTpl->pEntryLast->pNext = pTpe;
pTpl->pEntryLast = pTpe;
}
pTpl->tpenElements++;
return(pTpe);
}
/* Constructs a template list object. Returns pointer to it
* or NULL (if it fails).
*/
struct template* tplConstruct(void)
{
struct template *pTpl;
if((pTpl = calloc(1, sizeof(struct template))) == NULL)
return NULL;
/* basic initialisaion is done via calloc() - need to
* initialize only values != 0. */
if(tplLast == NULL)
{ /* we are the first element! */
tplRoot = tplLast = pTpl;
}
else
{
tplLast->pNext = pTpl;
tplLast = pTpl;
}
return(pTpl);
}
/* helper to tplAddLine. Parses a constant and generates
* the necessary structure.
* returns: 0 - ok, 1 - failure
*/
static int do_Constant(char **pp, struct template *pTpl)
{
register char *p;
sbStrBObj *pStrB;
struct templateEntry *pTpe;
int i;
assert(pp != NULL);
assert(*pp != NULL);
assert(pTpl != NULL);
p = *pp;
if((pStrB = sbStrBConstruct()) == NULL)
return 1;
sbStrBSetAllocIncrement(pStrB, 32);
/* TODO: implement escape characters! */
while(*p && *p != '%' && *p != '\"') {
if(*p == '\\') {
switch(*++p) {
case '\0':
/* the best we can do - it's invalid anyhow... */
sbStrBAppendChar(pStrB, *p);
break;
case 'n':
sbStrBAppendChar(pStrB, '\n');
++p;
break;
case 'r':
sbStrBAppendChar(pStrB, '\r');
++p;
break;
case '\\':
sbStrBAppendChar(pStrB, '\\');
++p;
break;
case '%':
sbStrBAppendChar(pStrB, '%');
++p;
break;
case '0': /* numerical escape sequence */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
i = 0;
while(*p && isdigit(*p)) {
i = i * 10 + *p++ - '0';
}
sbStrBAppendChar(pStrB, i);
break;
default:
sbStrBAppendChar(pStrB, *p++);
break;
}
}
else
sbStrBAppendChar(pStrB, *p++);
}
if((pTpe = tpeConstruct(pTpl)) == NULL) {
/* TODO: add handler */
return 1;
}
pTpe->eEntryType = CONSTANT;
pTpe->data.constant.pConstant = sbStrBFinish(pStrB);
/* we need to call strlen() below because of the escape sequneces.
* due to them p -*pp is NOT the rigt size!
*/
pTpe->data.constant.iLenConstant = strlen(pTpe->data.constant.pConstant);
*pp = p;
return 0;
}
/* helper to tplAddLine. Parses a parameter and generates
* the necessary structure.
* returns: 0 - ok, 1 - failure
*/
static int do_Parameter(char **pp, struct template *pTpl)
{
register char *p;
sbStrBObj *pStrB;
struct templateEntry *pTpe;
assert(pp != NULL);
assert(*pp != NULL);
assert(pTpl != NULL);
p = *pp;
if((pStrB = sbStrBConstruct()) == NULL)
return 1;
while(*p && *p != '%') {
sbStrBAppendChar(pStrB, *p++);
}
if(*p) ++p; /* eat '%' */
/* now store param */
if((pTpe = tpeConstruct(pTpl)) == NULL) {
/* TODO: add handler */
dprintf("Could not allocate memory for template parameter!\n");
return 1;
}
pTpe->eEntryType = FIELD;
pTpe->data.field.pPropRepl = sbStrBFinish(pStrB);
*pp = p;
return 0;
}
/* Add a new template line
* returns pointer to new object if it succeeds, NULL otherwise.
*/
struct template *tplAddLine(char* pName, char** ppRestOfConfLine)
{
struct template *pTpl;
char *p;
int bDone;
assert(pName != NULL);
assert(ppRestOfConfLine != NULL);
if((pTpl = tplConstruct()) == NULL)
return NULL;
pTpl->iLenName = strlen(pName);
pTpl->pszName = (char*) malloc(sizeof(char) * pTpl->iLenName);
if(pTpl->pszName == NULL) {
dprintf("tplAddLine could not alloc memory for template name!");
pTpl->iLenName = 0;
return NULL;
/* I know - we create a memory leak here - but I deem
* it acceptable as it is a) a very small leak b) very
* unlikely to happen. rgerhards 2004-11-17
*/
}
memcpy(pTpl->pszName, pName, pTpl->iLenName + 1);
#if 0
pTpl->iLenTemplate = strlen(pName);
pTpl->pszTemplate = (char*) malloc(sizeof(char) * pTpl->iLenTemplate);
if(pTpl->pszTemplate == NULL) {
/rintf("could not alloc memory Template"); /* TODO: change to dprintf()! */
free(pTpl->pszName);
pTpl->pszName = NULL;
pTpl->iLenName = 0;
pTpl->iLenTemplate = 0;
return NULL;
/* see note on leak above */
}
#endif
p = *ppRestOfConfLine;
assert(p != NULL);
/* skip whitespace */
while(isspace(*p))
++p;
if(*p != '"') {
dprintf("Template invalid, does not start with '\"'!\n");
/* TODO: Free mem! */
return NULL;
}
++p;
/* we finally got to the actual template string - so let's have some fun... */
bDone = *p ? 0 : 1;
while(!bDone) {
switch(*p) {
case '\0':
bDone = 1;
break;
case '%': /* parameter */
++p; /* eat '%' */
do_Parameter(&p, pTpl);
break;
default: /* constant */
do_Constant(&p, pTpl);
break;
}
if(*p == '\"') {/* end of template string? */
++p; /* eat it! */
bDone = 1;
}
}
*ppRestOfConfLine = p;
return(pTpl);
}
/* Find a template object based on name. Search
* currently is case-senstive (should we change?).
* returns pointer to template object if found and
* NULL otherwise.
* rgerhards 2004-11-17
*/
struct template *tplFind(char *pName, int iLenName)
{
struct template *pTpl;
assert(pName != NULL);
pTpl = tplRoot;
while(pTpl != NULL &&
!(pTpl->iLenName == iLenName &&
!strcmp(pTpl->pszName, pName)
))
{
pTpl = pTpl->pNext;
}
return(pTpl);
}
/* Print the template structure. This is more or less a
* debug or test aid, but anyhow I think it's worth it...
*/
void tplPrintList(void)
{
struct template *pTpl;
struct templateEntry *pTpe;
pTpl = tplRoot;
while(pTpl != NULL) {
dprintf("Template: Name='%s'\n", pTpl->pszName == NULL? "NULL" : pTpl->pszName);
pTpe = pTpl->pEntryRoot;
while(pTpe != NULL) {
dprintf("\tEntry(%x): type %d, ", (unsigned) pTpe, pTpe->eEntryType);
switch(pTpe->eEntryType) {
case UNDEFINED:
dprintf("(UNDEFINED)\n");
break;
case CONSTANT:
dprintf("(CONSTANT), value: '%s'\n",
pTpe->data.constant.pConstant);
break;
case FIELD:
dprintf("(FIELD), value: '%s'\n", pTpe->data.field.pPropRepl);
break;
}
pTpe = pTpe->pNext;
}
pTpl = pTpl->pNext; /* done, go next */
}
}
int tplGetEntryCount(struct template *pTpl)
{
assert(pTpl != NULL);
return(pTpl->tpenElements);
}
/*
* vi:set ai:
*/
|