enhanced rainerscript to allow chained dereference of json-arrays + fixed a bug json-array dereference handling + added a test for rainerscript using chained json-array dereference

Conflicts:
	tests/Makefile.am
This commit is contained in:
Janmejay Singh 2014-11-11 17:01:08 +05:30 committed by Rainer Gerhards
parent a0cac770c4
commit 2e3266b097
8 changed files with 16 additions and 27 deletions

View File

@ -199,7 +199,6 @@ expr: expr AND expr { $$ = cnfexprNew(AND, $1, $3); }
| NUMBER { $$ = (struct cnfexpr*) cnfnumvalNew($1); }
| STRING { $$ = (struct cnfexpr*) cnfstringvalNew($1); }
| VAR { $$ = (struct cnfexpr*) cnfvarNew($1); }
| VAR '[' NUMBER ']' { $$ = (struct cnfexpr*) cnfvarNewIndexed($1, $3); }
| array { $$ = (struct cnfexpr*) $1; }
fparams: expr { $$ = cnffparamlstNew($1, NULL); }
| expr ',' fparams { $$ = cnffparamlstNew($1, $3); }

View File

@ -138,7 +138,7 @@ int fileno(FILE *stream);
<EXPR>0[0-7]+ | /* octal number */
<EXPR>0x[0-7a-f] | /* hex number, following rule is dec; strtoll handles all! */
<EXPR>([1-9][0-9]*|0) { yylval.n = strtoll(yytext, NULL, 0); return NUMBER; }
<EXPR>\$[$!./]{0,1}[@a-z][!@a-z0-9\-_\.]* { yylval.s = strdup(yytext+1); return VAR; }
<EXPR>\$[$!./]{0,1}[@a-z][!@a-z0-9\-_\.\[\]]* { yylval.s = strdup(yytext+1); return VAR; }
<EXPR>\'([^'\\]|\\['"\\$bntr]|\\x[0-9a-f][0-9a-f]|\\[0-7][0-7][0-7])*\' {
yytext[yyleng-1] = '\0';
unescapeStr((uchar*)yytext+1, yyleng-2);

View File

@ -2823,22 +2823,6 @@ cnfvarNew(char *name)
return var;
}
struct cnfvar*
cnfvarNewIndexed(char *name, int subscript)
{
char name_buff[MAX_VARIABLE_NAME_LEN];
int len = strlen(name);
memcpy(name_buff, name, len);
free(name);
name_buff[len++] = '[';
len += snprintf(name_buff + len, MAX_VARIABLE_NAME_LEN - len - 2, "%d", subscript); //2 is for ]\0
name_buff[len++] = ']';
name_buff[len] = '\0';
char *indexed_name = strdup(name_buff);
return indexed_name == NULL ? NULL : cnfvarNew(indexed_name);
}
struct cnfstmt *
cnfstmtNew(unsigned s_type)
{

View File

@ -340,7 +340,6 @@ void cnfexprDestruct(struct cnfexpr *expr);
struct cnfnumval* cnfnumvalNew(long long val);
struct cnfstringval* cnfstringvalNew(es_str_t *estr);
struct cnfvar* cnfvarNew(char *name);
struct cnfvar* cnfvarNewIndexed(char *name, int subscript);
struct cnffunc * cnffuncNew(es_str_t *fname, struct cnffparamlst* paramlst);
struct cnffparamlst * cnffparamlstNew(struct cnfexpr *expr, struct cnffparamlst *next);
int cnfDoInclude(char *name);

View File

@ -4194,7 +4194,7 @@ static json_bool jsonVarExtract(struct json_object* root, const char *key, struc
char *array_idx_start = strstr(key, "[");
char *array_idx_end = NULL;
char *array_idx_num_end_discovered = NULL;
struct json_object **arr = NULL;
struct json_object *arr = NULL;
if (array_idx_start != NULL) {
array_idx_end = strstr(array_idx_start, "]");
}
@ -4204,11 +4204,11 @@ static json_bool jsonVarExtract(struct json_object* root, const char *key, struc
if (errno == 0 && array_idx_num_end_discovered == array_idx_end) {
memcpy(namebuf, key, array_idx_start - key);
namebuf[array_idx_start - key] = '\0';
json_bool found_obj = RS_json_object_object_get_ex(root, namebuf, arr);
if (found_obj && json_object_is_type(*arr, json_type_array)) {
int len = json_object_array_length(*arr);
json_bool found_obj = RS_json_object_object_get_ex(root, namebuf, &arr);
if (found_obj && json_object_is_type(arr, json_type_array)) {
int len = json_object_array_length(arr);
if (len > idx) {
*value = json_object_array_get_idx(*arr, idx);
*value = json_object_array_get_idx(arr, idx);
if (*value != NULL) return TRUE;
}
return FALSE;

View File

@ -186,7 +186,8 @@ endif
if ENABLE_MMJSONPARSE
TESTS += \
mmjsonparse_simple.sh \
mmjsonparse_cim.sh
mmjsonparse_cim.sh \
json_array_subscripting.sh
endif
if ENABLE_GNUTLS
@ -705,6 +706,9 @@ EXTRA_DIST= 1.rstest 2.rstest 3.rstest err1.rstest \
testsuites/rscript_wrap3.conf \
testsuites/wrap3_input\
testsuites/gethostname.conf \
json_array_subscripting.sh \
testsuites/json_array_subscripting.conf \
testsuites/json_array_input \
cfg.sh
# TODO: re-enable

View File

@ -9,5 +9,5 @@ echo doing shutdown
source $srcdir/diag.sh shutdown-when-empty
echo wait on shutdown
source $srcdir/diag.sh wait-shutdown
source $srcdir/diag.sh content-check "msg: def1 | important_msg | other_msg"
source $srcdir/diag.sh content-check 'msg: def1 | ghi2 | important_msg | { "baz": "other_msg" } | other_msg'
source $srcdir/diag.sh exit

View File

@ -1,9 +1,12 @@
$IncludeConfig diag-common.conf
template(name="outfmt" type="string" string="msg: %$!foo[1]% | %$!foo[3]!bar[0]!baz% | %$!foo[3]!bar[1]!baz%\n")
template(name="outfmt" type="string" string="msg: %$!foo[1]% | %$.quux% | %$.corge% | %$.grault% | %$!foo[3]!bar[1]!baz%\n")
module(load="../plugins/mmjsonparse/.libs/mmjsonparse")
module(load="../plugins/imptcp/.libs/imptcp")
input(type="imptcp" port="13514")
action(type="mmjsonparse")
set $.quux = $!foo[2];
set $.corge = $!foo[3]!bar[0]!baz;
set $.grault = $!foo[3]!bar[1];
action(type="omfile" file="./rsyslog.out.log" template="outfmt")