libubox
C utility functions for OpenWrt.
json_script.h File Reference
#include "avl.h"
#include "blob.h"
#include "blobmsg.h"
#include "utils.h"

Go to the source code of this file.

Data Structures

struct  json_script_ctx
 
struct  json_script_file
 

Functions

void json_script_init (struct json_script_ctx *ctx)
 
void json_script_free (struct json_script_ctx *ctx)
 
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)
 
static void json_script_abort (struct json_script_ctx *ctx)
 
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_filejson_script_file_from_blobmsg (const char *name, void *data, int len)
 
const char * json_script_find_var (struct json_script_ctx *ctx, struct blob_attr *vars, const char *name)
 

Function Documentation

◆ json_script_abort()

static void json_script_abort ( struct json_script_ctx ctx)
inlinestatic

Definition at line 110 of file json_script.h.

111 {
112  ctx->abort = true;
113 }

◆ json_script_eval_string()

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 at line 502 of file json_script.c.

505 {
506  struct json_call call = {
507  .ctx = ctx,
508  .vars = vars,
509  };
510 
511  return eval_string(&call, buf, name, pattern);
512 }
uint8_t name[]
Definition: blobmsg.h:1
static int eval_string(struct json_call *call, struct blob_buf *buf, const char *name, const char *pattern)
Definition: json_script.c:427
struct json_script_ctx * ctx
Definition: json_script.c:23
struct blob_attr * vars
Definition: json_script.c:24
Here is the call graph for this function:

◆ json_script_file_from_blobmsg()

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

Definition at line 38 of file json_script.c.

39 {
40  struct json_script_file *f;
41  char *new_name;
42  int name_len = 0;
43 
44  if (name)
45  name_len = strlen(name) + 1;
46 
47  f = calloc_a(sizeof(*f) + len, &new_name, name_len);
48  if (!f)
49  return NULL;
50 
51  memcpy(f->data, data, len);
52  if (name)
53  f->avl.key = strcpy(new_name, name);
54 
55  return f;
56 }
char data[]
Definition: blob.h:1
const void * key
Definition: avl.h:84
struct avl_node avl
Definition: json_script.h:82
struct blob_attr data[]
Definition: json_script.h:86
#define calloc_a(len,...)
Definition: utils.h:39
Here is the caller graph for this function:

◆ json_script_find_var()

const char* json_script_find_var ( struct json_script_ctx ctx,
struct blob_attr vars,
const char *  name 
)

Definition at line 94 of file json_script.c.

96 {
97  struct blob_attr *cur;
98  size_t rem;
99 
100  blobmsg_for_each_attr(cur, vars, rem) {
101  if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
102  continue;
103 
104  if (strcmp(blobmsg_name(cur), name) != 0)
105  continue;
106 
107  return blobmsg_data(cur);
108  }
109 
110  return ctx->handle_var(ctx, name, vars);
111 }
static const char * blobmsg_name(const struct blob_attr *attr)
Definition: blobmsg.h:62
#define blobmsg_for_each_attr(pos, attr, rem)
Definition: blobmsg.h:364
blobmsg_type
Definition: blobmsg.h:25
@ BLOBMSG_TYPE_STRING
Definition: blobmsg.h:29
static void * blobmsg_data(const struct blob_attr *attr)
Definition: blobmsg.h:78
Definition: blob.h:52
const char *(* handle_var)(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
Definition: json_script.h:57
Here is the call graph for this function:
Here is the caller graph for this function:

◆ json_script_free()

void json_script_free ( struct json_script_ctx ctx)

Definition at line 652 of file json_script.c.

653 {
654  struct json_script_file *f, *next;
655 
658 
659  blob_buf_free(&ctx->buf);
660 }
#define avl_remove_all_elements(tree, element, node_member, ptr)
Definition: avl.h:545
void blob_buf_free(struct blob_buf *buf)
Definition: blob.c:104
static void __json_script_file_free(struct json_script_file *f)
Definition: json_script.c:638
struct blob_buf buf
Definition: json_script.h:28
struct avl_tree files
Definition: json_script.h:27
struct json_script_file * next
Definition: json_script.h:83
Here is the call graph for this function:
Here is the caller graph for this function:

◆ json_script_init()

void json_script_init ( struct json_script_ctx ctx)

Definition at line 689 of file json_script.c.

690 {
691  avl_init(&ctx->files, avl_strcmp, false, NULL);
692 
693  if (!ctx->handle_error)
695 
696  if (!ctx->handle_var)
698 
699  if (!ctx->handle_expr)
701 
702  if (!ctx->handle_file)
704 }
int avl_strcmp(const void *k1, const void *k2, void *ptr)
Definition: avl-cmp.c:26
void avl_init(struct avl_tree *tree, avl_tree_comp comp, bool allow_dups, void *ptr)
Definition: avl.c:92
static int __default_handle_expr(struct json_script_ctx *ctx, const char *name, struct blob_attr *expr, struct blob_attr *vars)
Definition: json_script.c:676
static const char * __default_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
Definition: json_script.c:669
static void __default_handle_error(struct json_script_ctx *ctx, const char *msg, struct blob_attr *context)
Definition: json_script.c:663
static struct json_script_file * __default_handle_file(struct json_script_ctx *ctx, const char *name)
Definition: json_script.c:684
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 json_script_file *(* handle_file)(struct json_script_ctx *ctx, const char *name)
Definition: json_script.h:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ json_script_run()

void json_script_run ( struct json_script_ctx ctx,
const char *  filename,
struct blob_attr vars 
)

Definition at line 626 of file json_script.c.

628 {
629  struct json_script_file *file;
630 
631  file = json_script_get_file(ctx, name);
632  if (!file)
633  return;
634 
635  json_script_run_file(ctx, file, vars);
636 }
static struct json_script_file * json_script_get_file(struct json_script_ctx *ctx, const char *filename)
Definition: json_script.c:59
void json_script_run_file(struct json_script_ctx *ctx, struct json_script_file *file, struct blob_attr *vars)
Definition: json_script.c:607
Here is the call graph for this function:
Here is the caller graph for this function:

◆ json_script_run_file()

void json_script_run_file ( struct json_script_ctx ctx,
struct json_script_file file,
struct blob_attr vars 
)

Definition at line 607 of file json_script.c.

609 {
610  static unsigned int _seq = 0;
611  struct json_call call = {
612  .ctx = ctx,
613  .vars = vars,
614  .seq = ++_seq,
615  };
616 
617  /* overflow */
618  if (!call.seq)
619  call.seq = ++_seq;
620 
621  ctx->abort = false;
622 
623  __json_script_run(&call, file, NULL);
624 }
static void __json_script_run(struct json_call *call, struct json_script_file *file, struct blob_attr *context)
Definition: json_script.c:75
unsigned int seq
Definition: json_script.c:25
Here is the call graph for this function:
Here is the caller graph for this function: