6 #include "blobmsg_json.h"
7 #include "json_script.h"
9 struct json_script_ctx jctx
;
10 struct blob_buf b_vars
;
11 struct blob_buf b_script
;
13 static void handle_command(struct json_script_ctx
*ctx
, const char *name
,
14 struct blob_attr
*data
, struct blob_attr
*vars
)
16 struct blob_attr
*cur
;
19 fprintf(stderr
, "Command: %s", name
);
20 blobmsg_for_each_attr(cur
, data
, rem
)
21 fprintf(stderr
, " %s", (char *) blobmsg_data(cur
));
22 fprintf(stderr
, "\n");
25 static struct json_script_file
*
26 handle_file(struct json_script_ctx
*ctx
, const char *filename
)
30 obj
= json_object_from_file(filename
);
32 fprintf(stderr
, "load JSON data from %s failed.\n", filename
);
36 blob_buf_init(&b_script
, 0);
37 blobmsg_add_json_element(&b_script
, "", obj
);
40 return json_script_file_from_blobmsg(filename
,
41 blob_data(b_script
.head
), blob_len(b_script
.head
));
44 static void usage(const char *prog
, int exit_code
)
46 fprintf(stderr
, "Usage: %s [VARNAME=value] <filename_json_script>\n", prog
);
50 int main(int argc
, char *argv
[])
54 const char *prog
= argv
[0];
56 blobmsg_buf_init(&b_vars
);
57 blobmsg_buf_init(&b_script
);
59 json_script_init(&jctx
);
60 jctx
.handle_command
= handle_command
;
61 jctx
.handle_file
= handle_file
;
63 for (i
= 1; i
< argc
; i
++) {
64 char *sep
= strchr(argv
[i
], '=');
67 blobmsg_add_string(&b_vars
, argv
[i
], sep
+ 1);
74 if (i
< argc
|| !file
)
77 json_script_run(&jctx
, file
, b_vars
.head
);
79 json_script_free(&jctx
);
80 blob_buf_free(&b_script
);
81 blob_buf_free(&b_vars
);