libubox
C utility functions for OpenWrt.
test-json-script.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <json.h>
#include "blobmsg.h"
#include "blobmsg_json.h"
#include "json_script.h"

Go to the source code of this file.

Functions

static void handle_command (struct json_script_ctx *ctx, const char *name, struct blob_attr *data, struct blob_attr *vars)
 
static struct json_script_filehandle_file (struct json_script_ctx *ctx, const char *filename)
 
static void usage (const char *prog, int exit_code)
 
int main (int argc, char *argv[])
 

Variables

struct json_script_ctx jctx
 
struct blob_buf b_vars
 
struct blob_buf b_script
 

Function Documentation

◆ handle_command()

static void handle_command ( struct json_script_ctx ctx,
const char *  name,
struct blob_attr data,
struct blob_attr vars 
)
static

Definition at line 13 of file test-json-script.c.

15 {
16  struct blob_attr *cur;
17  size_t rem;
18 
19  fprintf(stdout, "%s", name);
20  blobmsg_for_each_attr(cur, data, rem)
21  fprintf(stdout, " %s", (char *) blobmsg_data(cur));
22  fprintf(stdout, "\n");
23 }
char data[]
Definition: blob.h:1
#define blobmsg_for_each_attr(pos, attr, rem)
Definition: blobmsg.h:364
uint8_t name[]
Definition: blobmsg.h:1
static void * blobmsg_data(const struct blob_attr *attr)
Definition: blobmsg.h:78
Definition: blob.h:52
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_file()

static struct json_script_file* handle_file ( struct json_script_ctx ctx,
const char *  filename 
)
static

Definition at line 26 of file test-json-script.c.

27 {
28  json_object *obj;
29 
30  obj = json_object_from_file(filename);
31  if (!obj) {
32  fprintf(stderr, "load JSON data from %s failed.\n", filename);
33  return NULL;
34  }
35 
38  json_object_put(obj);
39 
40  return json_script_file_from_blobmsg(filename,
42 }
int blob_buf_init(struct blob_buf *buf, int id)
Definition: blob.c:91
static size_t blob_len(const struct blob_attr *attr)
Definition: blob.h:100
static void * blob_data(const struct blob_attr *attr)
Definition: blob.h:75
bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object *obj)
Definition: blobmsg_json.c:47
struct json_script_file * json_script_file_from_blobmsg(const char *name, void *data, int len)
Definition: json_script.c:38
struct blob_attr * head
Definition: blob.h:65
struct blob_buf b_script
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 50 of file test-json-script.c.

51 {
52  int i;
53  char *file = NULL;
54  const char *prog = argv[0];
55 
58 
62 
63  for (i = 1; i < argc; i++) {
64  char *sep = strchr(argv[i], '=');
65  if (sep) {
66  *sep = '\0';
67  blobmsg_add_string(&b_vars, argv[i], sep + 1);
68  } else if (!file) {
69  file = argv[i];
70  } else {
71  usage(prog, -1);
72  }
73  }
74  if (i < argc || !file)
75  usage(prog, -2);
76 
78 
82 
83  return 0;
84 }
void blob_buf_free(struct blob_buf *buf)
Definition: blob.c:104
static int blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
Definition: blobmsg.h:235
static int blobmsg_buf_init(struct blob_buf *buf)
Definition: blobmsg.h:273
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
void json_script_run(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
Definition: json_script.c:626
void(* handle_command)(struct json_script_ctx *ctx, const char *name, struct blob_attr *cmd, struct blob_attr *vars)
Definition: json_script.h:40
struct json_script_file *(* handle_file)(struct json_script_ctx *ctx, const char *name)
Definition: json_script.h:67
static void handle_command(struct json_script_ctx *ctx, const char *name, struct blob_attr *data, struct blob_attr *vars)
struct blob_buf b_vars
static struct json_script_file * handle_file(struct json_script_ctx *ctx, const char *filename)
struct json_script_ctx jctx
static void usage(const char *prog, int exit_code)
Here is the call graph for this function:

◆ usage()

static void usage ( const char *  prog,
int  exit_code 
)
static

Definition at line 44 of file test-json-script.c.

45 {
46  fprintf(stderr, "Usage: %s [VARNAME=value] <filename_json_script>\n", prog);
47  exit(exit_code);
48 }
Here is the caller graph for this function:

Variable Documentation

◆ b_script

struct blob_buf b_script

Definition at line 1 of file test-json-script.c.

◆ b_vars

struct blob_buf b_vars

Definition at line 1 of file test-json-script.c.

◆ jctx

struct json_script_ctx jctx

Definition at line 1 of file test-json-script.c.