Question on calling v1.0.0 librosie using C
Created by: yausteve-ibm
Hi, Jamie, I am trying to call v1.0.0 librosie with the following C Program:
#include <stdlib.h> #include <stdio.h> #include <string.h> #include "librosie.h" #define ROSIE_HOME "/Users/syau/dev/log-analytics/rosie-pattern-language"
str new_rstr(char s){ str *p; p=(str *)malloc(sizeof(str)); p->len=strlen(s); p->ptr=(byte_ptr)s; return p; }
void free_rstr(str p){ / zero memory first before freeing */ memset(p->ptr,0,p->len); free(p); }
int main() { int rc; str errors; errors=new_rstr("ok"); / initialize Rosie engine / void engine=rosie_new(errors); printf("* Instantiate\n"); printf("Instantiate Rosie engine error:%s\n",errors->ptr); /* int rosie_import(void *L, int *ok, str *pkgname, str *as, str *errors); int rosie_compile(void *L, str *expression, int *pat, str *errors); int rosie_match(void *L, int pat, int start, char *encoder, str *input, match *match); / / import package / str pkgname; pkgname=new_rstr("all"); str as; as=new_rstr(""); int ok; rc=rosie_import(engine, &ok, pkgname, as, errors); printf(" Import\n"); printf("Import RPL error:%s\n",errors->ptr);
str *expression;
expression=new_rstr("\"all.things11\"");
int pat=0;
errors=new_rstr("ok2");
rc=rosie_compile(engine, expression, &pat, errors);
printf("*** Compile\n");
printf("Compile pattern error:%s\n",errors->ptr);
printf("pat:%i\n",pat);
str *input;
input=new_rstr("\"1234\"");
match r;
rc=rosie_match(engine, pat, 1, "json", input, &r);
printf("*** Match\n");
printf("match rc:%i\n",rc);
printf("leftover:%i\n",r.leftover);
printf("ttotal:%i\n",r.ttotal);
printf("tmatch:%i\n",r.tmatch);
printf("data length:%i\n",r.data.len);
printf("data%c\n",*(r.data.ptr));
}
I compile with the following: /Applications/Xcode.app/Contents/Developer/usr/bin/make librosie.so SYSCFLAGS="-DLUA_USE_MACOSX" SYSLDFLAGS="-dynamiclib" CC=clang clang -o librosie.o -c librosie.c -O2 -Wall -Wextra -DLUA_COMPAT_5_2 -DLUA_USE_MACOSX -I/Users/syau/dev/log-analytics/rosie-pattern-language/submodules/lua/include clang -o crosie crosie.c -O2 -Wall -Wextra -DLUA_COMPAT_5_2 -DLUA_USE_MACOSX -std=gnu99 -I${ROSIE_HOME}/submodules/lua/include -L. -lrosie
When I run the program, I got: Kwans-MBP:librosie syau$ ./crosie *** Instantiate Instantiate Rosie engine error:ok *** Import Import RPL error:{} *** Compile Compile pattern error:{} pat:1 *** Match match rc:0 leftover:6 ttotal:3 tmatch:3 data length:0 Segmentation fault: 11
It looks like all but the rosie_match() call had no error. I don't know what am I missing or done wrong here. Appreciate your advice. Many thanks. -Steve PS- I am just referring to the ffi.cdef in rosie.py and test.py to arrive at the above C program.