summaryrefslogtreecommitdiffstats
path: root/template.c
diff options
context:
space:
mode:
authorMiloslav Trmač <mitr@redhat.com>2012-08-11 09:19:09 +0200
committerMiloslav Trmač <mitr@redhat.com>2012-08-28 10:26:42 +0200
commitf2068e080718f28b98034233ea08b50f3d2ab220 (patch)
tree0c0b875dfcbf09fe963b26054bf5d11d959e2edf /template.c
parentee8e701cddaac8e786f388beff89b57f84a1a346 (diff)
downloadrsyslog-f2068e080718f28b98034233ea08b50f3d2ab220.zip
rsyslog-f2068e080718f28b98034233ea08b50f3d2ab220.tar.gz
rsyslog-f2068e080718f28b98034233ea08b50f3d2ab220.tar.xz
Handle $!all-json in field templates
Let msg.c handle appending fields, which also allows it to append more than one field per replaced property. The result allows supplementing CEE data with other (non-constant) properties, but will currently result in duplicates if $!all-json and the template contain fields with the same names. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Diffstat (limited to 'template.c')
-rw-r--r--template.c44
1 files changed, 16 insertions, 28 deletions
diff --git a/template.c b/template.c
index d422f8e..d9ca071 100644
--- a/template.c
+++ b/template.c
@@ -280,50 +280,38 @@ rsRetVal tplToFields(struct template *pTpl, msg_t *pMsg,
{
DEFiRet;
struct templateEntry *pTpe;
- struct templateField *fields;
- size_t i, propLen;
- unsigned short bMustBeFreed;
- uchar *pVal;
+ struct fieldBuildingState state;
+ size_t i;
assert(pTpl != NULL);
assert(pMsg != NULL);
assert(pFields != NULL);
- /* loop through the template. We obtain one value, create a
- * private copy (if necessary), add it to the string array
- * and then on to the next until we have processed everything.
- */
-
- /* The zeroization implicitly terminates the output array. */
- CHKmalloc(fields = calloc(pTpl->tpenElements + 1, sizeof(*fields)));
+ state.nextField = 0;
+ state.allocatedFields = pTpl->tpenElements + 1;
+ CHKmalloc(state.fields = malloc(state.allocatedFields
+ * sizeof(*state.fields)));
- i = 0;
for(pTpe = pTpl->pEntryRoot; pTpe != NULL; pTpe = pTpe->pNext) {
if(pTpe->eEntryType == CONSTANT) {
/* Completely ingore this - we don't know a field name
to use anyway. */
} else if(pTpe->eEntryType == FIELD) {
- fields[i].fieldName = pTpe->data.field.fieldName;
- pVal = MsgGetProp(pMsg, pTpe, pTpe->data.field.propid,
- pTpe->data.field.propName, &propLen,
- &bMustBeFreed);
- if(bMustBeFreed) { /* if it must be freed, it is our own private copy... */
- fields[i].value = pVal; /* ... so we can use it! */
- } else {
- CHKmalloc(fields[i].value = (uchar*)strdup((char*) pVal));
- }
- i++;
+ CHKiRet(MsgAppendFields(pMsg, pTpe, &state));
}
}
+ CHKiRet(FBSensureSpace(&state));
+ /* Terminate the array */
+ state.fields[state.nextField].fieldName = NULL;
finalize_it:
- if(iRet != RS_RET_OK && fields != NULL) {
- for(i = 0; fields[i].fieldName != NULL; i++)
- free(fields[i].value);
- free(fields);
- fields = NULL;
+ if(iRet != RS_RET_OK && state.fields != NULL) {
+ for(i = 0; i < state.nextField; i++)
+ free(state.fields[i].value);
+ free(state.fields);
+ state.fields = NULL;
}
- *pFields = fields;
+ *pFields = state.fields;
RETiRet;
}