summaryrefslogtreecommitdiffstats
path: root/runtime/vmprg.c
diff options
context:
space:
mode:
authorRainer Gerhards <rgerhards@adiscon.com>2008-07-07 09:41:31 +0200
committerRainer Gerhards <rgerhards@adiscon.com>2008-07-07 09:41:31 +0200
commit37fd063042821dc328c88e1f7366543decdcb76f (patch)
tree5f14f9144f7c4c9ad3a071535ccc8a4b48b40218 /runtime/vmprg.c
parent06001e951f5b5d0a7919c61057bc7a87b9eb8cba (diff)
downloadrsyslog-37fd063042821dc328c88e1f7366543decdcb76f.tar.gz
rsyslog-37fd063042821dc328c88e1f7366543decdcb76f.tar.xz
rsyslog-37fd063042821dc328c88e1f7366543decdcb76f.zip
added capability to create a printable string of a vmprg
This is needed so that we can create simple testbenches which will check the result of a test (a generated program) via a simple strcmp.
Diffstat (limited to 'runtime/vmprg.c')
-rw-r--r--runtime/vmprg.c40
1 files changed, 38 insertions, 2 deletions
diff --git a/runtime/vmprg.c b/runtime/vmprg.c
index a2b744d7..c7354fe4 100644
--- a/runtime/vmprg.c
+++ b/runtime/vmprg.c
@@ -24,12 +24,14 @@
*/
#include "config.h"
+#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "rsyslog.h"
#include "obj.h"
#include "vmprg.h"
+#include "stringbuf.h"
/* static data */
DEFobjStaticHelpers
@@ -79,6 +81,41 @@ CODESTARTobjDebugPrint(vmprg)
ENDobjDebugPrint(vmprg)
+/* This function is similar to DebugPrint, but does not send its output to
+ * the debug log but instead to a caller-provided string. The idea here is that
+ * we can use this string to get a textual representation of a bytecode program.
+ * Among others, this is useful for creating testbenches, our first use case for
+ * it. Here, it enables simple comparison of the resulting program to a
+ * reference program by simple string compare.
+ * Note that the caller must initialize the string object. We always add
+ * data to it. So, it can be easily combined into a chain of methods
+ * to generate the final string.
+ * rgerhards, 2008-07-04
+ */
+static rsRetVal
+Obj2Str(vmprg_t *pThis, cstr_t *pstrPrg)
+{
+ uchar szAddr[12];
+ vmop_t *pOp;
+ int i;
+ int lenAddr;
+ DEFiRet;
+
+ ISOBJ_TYPE_assert(pThis, vmprg);
+ assert(pstrPrg != NULL);
+ CHKiRet(rsCStrAppendStr(pstrPrg, (uchar*)"program contents:\n"));
+ i = 0; /* "program counter" */
+ for(pOp = pThis->vmopRoot ; pOp != NULL ; pOp = pOp->pNext) {
+ lenAddr = snprintf((char*)szAddr, sizeof(szAddr), "%8.8d: ", i++);
+ CHKiRet(rsCStrAppendStrWithLen(pstrPrg, szAddr, lenAddr));
+ vmop.Obj2Str(pOp, pstrPrg);
+ }
+
+finalize_it:
+ RETiRet;
+}
+
+
/* add an operation (instruction) to the end of the current program. This
* function is expected to be called while creating the program, but never
* again after this is done and it is being executed. Results are undefined if
@@ -146,12 +183,11 @@ CODESTARTobjQueryInterface(vmprg)
* work here (if we can support an older interface version - that,
* of course, also affects the "if" above).
*/
- //xxxpIf->oID = OBJvmprg;
-
pIf->Construct = vmprgConstruct;
pIf->ConstructFinalize = vmprgConstructFinalize;
pIf->Destruct = vmprgDestruct;
pIf->DebugPrint = vmprgDebugPrint;
+ pIf->Obj2Str = Obj2Str;
pIf->AddOperation = vmprgAddOperation;
pIf->AddVarOperation = vmprgAddVarOperation;
finalize_it: