Welcome to mirror list, hosted at ThFree Co, Russian Federation.

json_script.h - git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9475baa59507f8284048386fe8fd152192d6146d (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#ifndef __JSON_SCRIPT_H
#define __JSON_SCRIPT_H

#include "avl.h"
#include "blob.h"
#include "blobmsg.h"
#include "utils.h"

struct json_script_file;

struct json_script_ctx {
	struct avl_tree files;
	struct blob_buf buf;

	uint32_t run_seq;

	/*
	 * handle_command: handle a command that was not recognized by the
	 * json_script core (required)
	 *
	 * @cmd: blobmsg container of the processed command
	 * @vars: blobmsg container of current run variables
	 */
	void (*handle_command)(struct json_script_ctx *ctx, const char *name,
			       struct blob_attr *cmd, struct blob_attr *vars);

	/*
	 * handle_expr: handle an expression that was not recognized by the
	 * json_script core (optional)
	 *
	 * @expr: blobmsg container of the processed expression
	 * @vars: blobmsg container of current runtime variables
	 */
	int (*handle_expr)(struct json_script_ctx *ctx, const char *name,
			   struct blob_attr *expr, struct blob_attr *vars);

	/*
	 * handle_var - look up a variable that's not part of the runtime
	 * variable set (optional)
	 */
	const char *(*handle_var)(struct json_script_ctx *ctx, const char *name,
				  struct blob_attr *vars);

	/*
	 * handle_file - load a file by filename (optional)
	 *
	 * in case of wildcards, it can return a chain of json_script files
	 * linked via the ::next pointer. Only the first json_script file is
	 * added to the tree.
	 */
	struct json_script_file *(*handle_file)(struct json_script_ctx *ctx,
						const char *name);

	/*
	 * handle_error - handle a processing error in a command or expression
	 * (optional)
	 * 
	 * @msg: error message
	 * @context: source file context of the error (blobmsg container)
	 */
	void (*handle_error)(struct json_script_ctx *ctx, const char *msg,
			     struct blob_attr *context);
};

struct json_script_file {
	struct avl_node avl;
	struct json_script_file *next;

	unsigned int seq;
	struct blob_attr data[];
};

void json_script_init(struct json_script_ctx *ctx);
void json_script_free(struct json_script_ctx *ctx);

/*
 * json_script_run - run a json script with a set of runtime variables
 *
 * @filename: initial filename to run
 * @vars: blobmsg container of the current runtime variables
 */
void json_script_run(struct json_script_ctx *ctx, const char *filename,
		     struct blob_attr *vars);

void json_script_run_file(struct json_script_ctx *ctx, struct json_script_file *file,
			  struct blob_attr *vars);
/*
 * json_script_eval_string - evaluate a string and store the result
 *
 * Can be used to process variable references outside of a script
 * in a same way that they would be interpreted in the script context.
 */
int json_script_eval_string(struct json_script_ctx *ctx, struct blob_attr *vars,
			    struct blob_buf *buf, const char *name,
			    const char *pattern);

struct json_script_file *
json_script_file_from_blobmsg(const char *name, void *data, int len);

/*
 * json_script_find_var - helper function to find a runtime variable from
 * the list passed by json_script user.
 * It is intended to be used by the .handle_var callback
 */
const char *json_script_find_var(struct json_script_ctx *ctx, struct blob_attr *vars,
				 const char *name);

#endif