summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2013-12-29 18:03:10 +0000
committerJo-Philipp Wich <jow@openwrt.org>2013-12-29 18:03:10 +0000
commitbb372e308e2332766d038cef7bcdf74df7b0e31c (patch)
tree3193b449d204cd4467fdc5d9fbbd595a127a94ad /main.c
parent054eb236c524a237f7cd156e8e71151b1b4e0ace (diff)
downloadjsonpath-bb372e308e2332766d038cef7bcdf74df7b0e31c.tar.gz
eliminate global variables and use a private parser/lexer state
Diffstat (limited to 'main.c')
-rw-r--r--main.c24
1 files changed, 14 insertions, 10 deletions
diff --git a/main.c b/main.c
index 7a19091..05e445c 100644
--- a/main.c
+++ b/main.c
@@ -76,23 +76,25 @@ static void
export_json(struct json_object *jsobj, char *expr)
{
bool first;
- struct jp_opcode *tree;
+ struct jp_state *state;
struct json_object *res;
- const char *error, *prefix;
+ const char *prefix;
- tree = jp_parse(expr, &error);
+ state = jp_parse(expr);
- if (error)
+ if (!state || state->error)
{
- fprintf(stderr, "In expression '%s': %s\n", expr, error);
- return;
+ fprintf(stderr, "In expression '%s': %s\n",
+ expr, state ? state->error : "Out of memory");
+
+ goto out;
}
- res = jp_match(tree, jsobj);
+ res = jp_match(state->path, jsobj);
- if (tree->type == T_LABEL)
+ if (state->path->type == T_LABEL)
{
- prefix = tree->str;
+ prefix = state->path->str;
switch (json_object_get_type(res))
{
@@ -163,7 +165,9 @@ export_json(struct json_object *jsobj, char *expr)
printf("%s\n", json_object_to_json_string(res));
}
- jp_free();
+out:
+ if (state)
+ jp_free(state);
}
int main(int argc, char **argv)