• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/libubox/tests/test-json-script.c

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 #include <json.h>
  5 #include "blobmsg.h"
  6 #include "blobmsg_json.h"
  7 #include "json_script.h"
  8 
  9 struct json_script_ctx  jctx;
 10 struct blob_buf         b_vars;
 11 struct blob_buf         b_script;
 12 
 13 static void handle_command(struct json_script_ctx *ctx, const char *name,
 14         struct blob_attr *data, struct blob_attr *vars)
 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 }
 24 
 25 static struct json_script_file *
 26 handle_file(struct json_script_ctx *ctx, const char *filename)
 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 
 36         blob_buf_init(&b_script, 0);
 37         blobmsg_add_json_element(&b_script, "", obj);
 38         json_object_put(obj);
 39 
 40         return json_script_file_from_blobmsg(filename,
 41                 blob_data(b_script.head), blob_len(b_script.head));
 42 }
 43 
 44 static void usage(const char *prog, int exit_code)
 45 {
 46         fprintf(stderr, "Usage: %s [VARNAME=value] <filename_json_script>\n", prog);
 47         exit(exit_code);
 48 }
 49 
 50 int main(int argc, char *argv[])
 51 {
 52         int i;
 53         char *file = NULL;
 54         const char *prog = argv[0];
 55 
 56         blobmsg_buf_init(&b_vars);
 57         blobmsg_buf_init(&b_script);
 58 
 59         json_script_init(&jctx);
 60         jctx.handle_command = handle_command;
 61         jctx.handle_file = handle_file;
 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 
 77         json_script_run(&jctx, file, b_vars.head);
 78 
 79         json_script_free(&jctx);
 80         blob_buf_free(&b_script);
 81         blob_buf_free(&b_vars);
 82 
 83         return 0;
 84 }
 85 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt