summaryrefslogtreecommitdiffstats
path: root/var.c
blob: 1f4ed1cb82af6f5500ca932f9b5c9be52995acc6 (plain)
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
/* var.c - a typeless variable class
 *
 * This class is used to represent variable values, which may have any type.
 * Among others, it will be used inside rsyslog's expression system, but
 * also internally at any place where a typeless variable is needed.
 *
 * Module begun 2008-02-20 by Rainer Gerhards, with some code taken
 * from the obj.c/.h files.
 *
 * Copyright 2007, 2008 Rainer Gerhards and Adiscon GmbH.
 *
 * This file is part of rsyslog.
 *
 * Rsyslog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rsyslog 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with Rsyslog.  If not, see <http://www.gnu.org/licenses/>.
 *
 * A copy of the GPL can be found in the file "COPYING" in this distribution.
 */

#include "config.h"
#include <stdlib.h>
#include <assert.h>

#include "rsyslog.h"
#include "obj.h"
#include "var.h"

/* static data */
DEFobjStaticHelpers


/* Standard-Constructor
 */
BEGINobjConstruct(var) /* be sure to specify the object type also in END macro! */
ENDobjConstruct(var)


/* ConstructionFinalizer
 * rgerhards, 2008-01-09
 */
rsRetVal varConstructFinalize(var_t __attribute__((unused)) *pThis)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, var);

	RETiRet;
}


/* destructor for the var object */
BEGINobjDestruct(var) /* be sure to specify the object type also in END and CODESTART macros! */
CODESTARTobjDestruct(var)
	if(pThis->pcsName != NULL)
		d_free(pThis->pcsName);
	if(pThis->varType == VARTYPE_STR) {
		if(pThis->val.pStr != NULL)
			d_free(pThis->val.pStr);
	}

ENDobjDestruct(var)


/* DebugPrint support for the var object */
BEGINobjDebugPrint(var) /* be sure to specify the object type also in END and CODESTART macros! */
CODESTARTobjDebugPrint(var)
	switch(pThis->varType) {
		case VARTYPE_STR:
			dbgoprint((obj_t*) pThis, "type: cstr, val '%s'\n", rsCStrGetSzStr(pThis->val.pStr));
			break;
		case VARTYPE_NUMBER:
			dbgoprint((obj_t*) pThis, "type: int64, val %lld\n", pThis->val.num);
			break;
		default:
			dbgoprint((obj_t*) pThis, "type %d currently not suppored in debug output\n", pThis->varType);
			break;
	}
ENDobjDebugPrint(var)


/* free the current values (destructs objects if necessary)
 */
static rsRetVal
varUnsetValues(var_t *pThis)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, var);
	if(pThis->varType == VARTYPE_STR)
		rsCStrDestruct(&pThis->val.pStr);

	pThis->varType = VARTYPE_NONE;

	RETiRet;
}


/* set a string value 
 * The caller hands over the string and must n longer use it after this method
 * has been called.
 */
static rsRetVal
varSetString(var_t *pThis, cstr_t *pStr)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, var);

	CHKiRet(varUnsetValues(pThis));
	pThis->varType = VARTYPE_STR;
	pThis->val.pStr = pStr;

finalize_it:
	RETiRet;
}


/* set an int64 value */
static rsRetVal
varSetNumber(var_t *pThis, number_t iVal)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, var);

	CHKiRet(varUnsetValues(pThis));
	pThis->varType = VARTYPE_NUMBER;
	pThis->val.num = iVal;

finalize_it:
	RETiRet;
}


/* check if the provided object can be converted to a number. Uses
 * non-standard calling conventions because it makes an awful lot of sense.
 * Returns 1 if conversion is possibe and 0 if not. If 1 is returned, a
 * conversion request on the unchanged object is guaranteed to succeed.
 * rgerhards, 2008-02-22
 */
int canConvToNumber(var_t *pThis)
{
	int ret = 0;

	BEGINfunc

	if(pThis->varType == VARTYPE_NUMBER) {
		ret = 1;
	} else if(pThis->varType == VARTYPE_STR) {
		ret = rsCStrCanConvertToNumber(pThis->val.pStr); // TODO: implement the same method in str_t object, then call that */
	}

	ENDfunc
	return ret;
}


/* This function is used to prepare two var_t objects for a common operation,
 * e.g before they are added, multiplied or compared. The function looks at
 * the data types of both operands and finds the best data type suitable for
 * the operation (in respect to current types). Then, it converts those
 * operands that need conversion. Please note that the passed-in var objects
 * *are* modified and returned as new type. So do call this function only if
 * you actually need the conversion.
 *
 * This is how the common data type is selected. Note that op1 and op2 are
 * just the two operands, their order is irrelevant (this would just take up
 * more table space - so string/number is the same thing as number/string).
 *
 * Common Types:
 * op1		op2	operation data type
 * string	string	string
 * string	number	number if op1 can be converted to number, string else
 * date		string  date if op1 can be converted to date, string else
 * number	number  number
 * date		number	string (maybe we can do better?)
 * date		date	date
 * none         n/a     error
 *
 * If a boolean value is required, we need to have a number inside the
 * operand. If it is not, conversion rules to number apply. Once we
 * have a number, things get easy: 0 is false, anything else is true.
 * Please note that due to this conversion rules, "0" becomes false
 * while "-4712" becomes true. Using a date as boolen is not a good
 * idea. Depending on the ultimate conversion rules, it may always
 * become true or false. As such, using dates as booleans is
 * prohibited and the result defined to be undefined.
 *
 * rgerhards, 2008-02-22
 */
static rsRetVal
ConvForOperation(var_t *pThis, var_t *pOther)
{
	DEFiRet;
	varType_t commonType;

	if(pThis->varType == VARTYPE_NONE || pOther->varType == VARTYPE_NONE)
		ABORT_FINALIZE(RS_RET_INVALID_VAR);

	switch(pThis->varType) {
		case VARTYPE_NONE:
			ABORT_FINALIZE(RS_RET_INVALID_VAR);
			break;
		case VARTYPE_STR:
			switch(pOther->varType) {
				case VARTYPE_NONE:
					ABORT_FINALIZE(RS_RET_INVALID_VAR);
					break;
				case VARTYPE_STR:
					commonType = VARTYPE_STR;
					break;
				case VARTYPE_NUMBER:
					/* check if we can convert pThis to a number, if so use number format. */
					commonType = canConvToNumber(pThis) ? VARTYPE_NUMBER : VARTYPE_STR;
					break;
				case VARTYPE_SYSLOGTIME:
					ABORT_FINALIZE(RS_RET_NOT_IMPLEMENTED);
					break;
			}
			break;
		case VARTYPE_NUMBER:
			switch(pOther->varType) {
				case VARTYPE_NONE:
					ABORT_FINALIZE(RS_RET_INVALID_VAR);
					break;
				case VARTYPE_STR:
					/* check if we can convert pOther to a number, if so use number format. */
					commonType = canConvToNumber(pOther) ? VARTYPE_NUMBER : VARTYPE_STR;
					break;
				case VARTYPE_NUMBER:
					commonType = VARTYPE_NUMBER;
					break;
				case VARTYPE_SYSLOGTIME:
					ABORT_FINALIZE(RS_RET_NOT_IMPLEMENTED);
					break;
			}
			break;
		case VARTYPE_SYSLOGTIME:
			ABORT_FINALIZE(RS_RET_NOT_IMPLEMENTED);
			break;
	}

finalize_it:
	RETiRet;
}


/* queryInterface function
 * rgerhards, 2008-02-21
 */
BEGINobjQueryInterface(var)
CODESTARTobjQueryInterface(var)
	if(pIf->ifVersion != varCURR_IF_VERSION) { /* check for current version, increment on each change */
		ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
	}

	/* ok, we have the right interface, so let's fill it
	 * Please note that we may also do some backwards-compatibility
	 * work here (if we can support an older interface version - that,
	 * of course, also affects the "if" above).
	 */
	pIf->oID = OBJvar;

	pIf->Construct = varConstruct;
	pIf->ConstructFinalize = varConstructFinalize;
	pIf->Destruct = varDestruct;
	pIf->DebugPrint = varDebugPrint;
	pIf->SetNumber = varSetNumber;
	pIf->SetString = varSetString;
	pIf->ConvForOperation = ConvForOperation;
finalize_it:
ENDobjQueryInterface(var)


/* Initialize the var class. Must be called as the very first method
 * before anything else is called inside this class.
 * rgerhards, 2008-02-19
 */
BEGINObjClassInit(var, 1) /* class, version */
	OBJSetMethodHandler(objMethod_DEBUGPRINT, varDebugPrint);
	OBJSetMethodHandler(objMethod_CONSTRUCTION_FINALIZER, varConstructFinalize);
ENDObjClassInit(var)

/* vi:set ai:
 */