記錄libubox example code
更新記錄
item | note |
---|---|
20160907 | 第一版 |
目錄
libubox
json_script_ctx
json_script_ctx
json_script_run
json當成command來執行
會呼叫handle_file及handle_command來做相對的處理json_script_run
json_script_run
json_script_get_file
json_script_run_file1
2
3void json_script_run(struct json_script_ctx *ctx, const char *name,struct blob_attr *vars)
static struct json_script_file *json_script_get_file(struct json_script_ctx *ctx, const char *filename)
void json_script_run_file(struct json_script_ctx *ctx, struct json_script_file *file,struct blob_attr *vars)
example
- json_script-ex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86$ cat json_script-ex.c
#include <stdio.h>
#include <stdlib.h>
#include <json.h>
#include "blobmsg.h"
#include "blobmsg_json.h"
#include "json_script.h"
struct json_script_ctx jctx;
struct blob_buf b_vars;
struct blob_buf b_script;
static void handle_command(struct json_script_ctx *ctx, const char *name,
struct blob_attr *data, struct blob_attr *vars)
{
struct blob_attr *cur;
int rem;
fprintf(stderr, "Command: %s", name);
fprintf(stderr,"##");
blobmsg_for_each_attr(cur, data, rem)
fprintf(stderr, " %s", (char *) blobmsg_data(cur));
fprintf(stderr, "\n");
}
static struct json_script_file *
handle_file(struct json_script_ctx *ctx, const char *filename)
{
json_object *obj;
obj = json_object_from_file(filename);
if (!obj) {
fprintf(stderr, "load JSON data from %s failed.\n", filename);
return NULL;
}
blob_buf_init(&b_script, 0);
blobmsg_add_json_element(&b_script, "", obj);
json_object_put(obj);
return json_script_file_from_blobmsg(filename,
blob_data(b_script.head), blob_len(b_script.head));
}
static void usage(const char *prog, int exit_code)
{
fprintf(stderr, "Usage: %s [VARNAME=value] <filename_json_script>\n", prog);
exit(exit_code);
}
int main(int argc, char *argv[])
{
int i;
char *file = NULL;
const char *prog = argv[0];
blobmsg_buf_init(&b_vars);
blobmsg_buf_init(&b_script);
json_script_init(&jctx);
jctx.handle_command = handle_command;
jctx.handle_file = handle_file;
for (i = 1; i < argc; i++) {
char *sep = strchr(argv[i], '=');
if (sep) {
*sep = '\0';
blobmsg_add_string(&b_vars, argv[i], sep + 1);
} else if (!file) {
file = argv[i];
} else {
usage(prog, -1);
}
}
if (i < argc || !file)
usage(prog, -2);
printf("test11\n");
json_script_run(&jctx, file, b_vars.head);
json_script_free(&jctx);
blob_buf_free(&b_script);
blob_buf_free(&b_vars);
return 0;
}
使用json來執行command
不過還是看懂如何使用