libubox
C utility functions for OpenWrt.
json_script.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 #ifndef __JSON_SCRIPT_H
17 #define __JSON_SCRIPT_H
18 
19 #include "avl.h"
20 #include "blob.h"
21 #include "blobmsg.h"
22 #include "utils.h"
23 
24 struct json_script_file;
25 
27  struct avl_tree files;
28  struct blob_buf buf;
29 
30  uint32_t run_seq;
31  bool abort;
32 
33  /*
34  * handle_command: handle a command that was not recognized by the
35  * json_script core (required)
36  *
37  * @cmd: blobmsg container of the processed command
38  * @vars: blobmsg container of current run variables
39  */
40  void (*handle_command)(struct json_script_ctx *ctx, const char *name,
41  struct blob_attr *cmd, struct blob_attr *vars);
42 
43  /*
44  * handle_expr: handle an expression that was not recognized by the
45  * json_script core (optional)
46  *
47  * @expr: blobmsg container of the processed expression
48  * @vars: blobmsg container of current runtime variables
49  */
50  int (*handle_expr)(struct json_script_ctx *ctx, const char *name,
51  struct blob_attr *expr, struct blob_attr *vars);
52 
53  /*
54  * handle_var - look up a variable that's not part of the runtime
55  * variable set (optional)
56  */
57  const char *(*handle_var)(struct json_script_ctx *ctx, const char *name,
58  struct blob_attr *vars);
59 
60  /*
61  * handle_file - load a file by filename (optional)
62  *
63  * in case of wildcards, it can return a chain of json_script files
64  * linked via the ::next pointer. Only the first json_script file is
65  * added to the tree.
66  */
67  struct json_script_file *(*handle_file)(struct json_script_ctx *ctx,
68  const char *name);
69 
70  /*
71  * handle_error - handle a processing error in a command or expression
72  * (optional)
73  *
74  * @msg: error message
75  * @context: source file context of the error (blobmsg container)
76  */
77  void (*handle_error)(struct json_script_ctx *ctx, const char *msg,
78  struct blob_attr *context);
79 };
80 
82  struct avl_node avl;
84 
85  unsigned int seq;
86  struct blob_attr data[];
87 };
88 
89 void json_script_init(struct json_script_ctx *ctx);
90 void json_script_free(struct json_script_ctx *ctx);
91 
92 /*
93  * json_script_run - run a json script with a set of runtime variables
94  *
95  * @filename: initial filename to run
96  * @vars: blobmsg container of the current runtime variables
97  */
98 void json_script_run(struct json_script_ctx *ctx, const char *filename,
99  struct blob_attr *vars);
100 
101 void json_script_run_file(struct json_script_ctx *ctx, struct json_script_file *file,
102  struct blob_attr *vars);
103 
104 /*
105  * json_script_abort - abort current json script run
106  *
107  * to be called from a script context callback
108  */
109 static inline void
111 {
112  ctx->abort = true;
113 }
114 
115 /*
116  * json_script_eval_string - evaluate a string and store the result
117  *
118  * Can be used to process variable references outside of a script
119  * in a same way that they would be interpreted in the script context.
120  */
121 int json_script_eval_string(struct json_script_ctx *ctx, struct blob_attr *vars,
122  struct blob_buf *buf, const char *name,
123  const char *pattern);
124 
125 struct json_script_file *
126 json_script_file_from_blobmsg(const char *name, void *data, int len);
127 
128 /*
129  * json_script_find_var - helper function to find a runtime variable from
130  * the list passed by json_script user.
131  * It is intended to be used by the .handle_var callback
132  */
133 const char *json_script_find_var(struct json_script_ctx *ctx, struct blob_attr *vars,
134  const char *name);
135 
136 #endif
char data[]
Definition: blob.h:1
uint8_t name[]
Definition: blobmsg.h:1
static const struct json_handler cmd[]
Definition: json_script.c:207
static const struct json_handler expr[]
Definition: json_script.c:377
void json_script_init(struct json_script_ctx *ctx)
Definition: json_script.c:689
void json_script_free(struct json_script_ctx *ctx)
Definition: json_script.c:652
struct json_script_file * json_script_file_from_blobmsg(const char *name, void *data, int len)
Definition: json_script.c:38
void json_script_run(struct json_script_ctx *ctx, const char *filename, struct blob_attr *vars)
Definition: json_script.c:626
static void json_script_abort(struct json_script_ctx *ctx)
Definition: json_script.h:110
int json_script_eval_string(struct json_script_ctx *ctx, struct blob_attr *vars, struct blob_buf *buf, const char *name, const char *pattern)
Definition: json_script.c:502
void json_script_run_file(struct json_script_ctx *ctx, struct json_script_file *file, struct blob_attr *vars)
Definition: json_script.c:607
const char * json_script_find_var(struct json_script_ctx *ctx, struct blob_attr *vars, const char *name)
Definition: json_script.c:94
Definition: avl.h:56
Definition: avl.h:110
Definition: blob.h:52
Definition: blob.h:64
int(* handle_expr)(struct json_script_ctx *ctx, const char *name, struct blob_attr *expr, struct blob_attr *vars)
Definition: json_script.h:50
void(* handle_error)(struct json_script_ctx *ctx, const char *msg, struct blob_attr *context)
Definition: json_script.h:77
struct blob_buf buf
Definition: json_script.h:28
void(* handle_command)(struct json_script_ctx *ctx, const char *name, struct blob_attr *cmd, struct blob_attr *vars)
Definition: json_script.h:40
uint32_t run_seq
Definition: json_script.h:30
struct avl_tree files
Definition: json_script.h:27
unsigned int seq
Definition: json_script.h:85
struct avl_node avl
Definition: json_script.h:82
struct json_script_file * next
Definition: json_script.h:83
struct blob_attr data[]
Definition: json_script.h:86
struct udebug_client_msg * msg
Definition: udebug-priv.h:29