libubox
C utility functions for OpenWrt.
jshn.c File Reference
#include <json/json.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <getopt.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
#include "list.h"
#include "avl.h"
#include "blob.h"
#include "blobmsg_json.h"

Go to the source code of this file.

Data Structures

struct  env_var
 

Macros

#define MAX_VARLEN   256
 

Functions

static int add_json_element (const char *key, json_object *obj)
 
static int add_json_object (json_object *obj)
 
static int add_json_array (struct array_list *a)
 
static void add_json_string (const char *str)
 
static void write_key_string (const char *key)
 
static int jshn_parse (const char *str)
 
static char * getenv_avl (const char *key)
 
static char * get_keys (const char *prefix)
 
static void get_var (const char *prefix, const char **name, char **var, char **type)
 
static json_object * jshn_add_objects (json_object *obj, const char *prefix, bool array)
 
static void jshn_add_object_var (json_object *obj, bool array, const char *prefix, const char *name)
 
static int jshn_format (bool no_newline, bool indent, FILE *stream)
 
static int usage (const char *progname)
 
static int avl_strcmp_var (const void *k1, const void *k2, void *ptr)
 
static int jshn_parse_file (const char *path)
 
static int jshn_format_file (const char *path, bool no_newline, bool indent)
 
int main (int argc, char **argv)
 

Variables

static struct avl_tree env_vars
 
static struct blob_buf b = { 0 }
 
static const char * var_prefix = ""
 
static int var_prefix_len = 0
 

Macro Definition Documentation

◆ MAX_VARLEN

#define MAX_VARLEN   256

Definition at line 37 of file jshn.c.

Function Documentation

◆ add_json_array()

static int add_json_array ( struct array_list *  a)
static

Definition at line 64 of file jshn.c.

65 {
66  char seq[12];
67  int i, len;
68  int ret;
69 
70  for (i = 0, len = array_list_length(a); i < len; i++) {
71  snprintf(seq, sizeof(seq), "%d", i);
72  ret = add_json_element(seq, array_list_get_idx(a, i));
73  if (ret)
74  return ret;
75  }
76 
77  return 0;
78 }
static int add_json_element(const char *key, json_object *obj)
Definition: jshn.c:107
Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_json_element()

static int add_json_element ( const char *  key,
json_object *  obj 
)
static

Definition at line 107 of file jshn.c.

108 {
109  char *type;
110 
111  switch (json_object_get_type(obj)) {
112  case json_type_object:
113  type = "object";
114  break;
115  case json_type_array:
116  type = "array";
117  break;
118  case json_type_string:
119  type = "string";
120  break;
121  case json_type_boolean:
122  type = "boolean";
123  break;
124  case json_type_int:
125  type = "int";
126  break;
127  case json_type_double:
128  type = "double";
129  break;
130  case json_type_null:
131  type = "null";
132  break;
133  default:
134  return -1;
135  }
136 
137  fprintf(stdout, "json_add_%s '", type);
138  write_key_string(key);
139 
140  switch (json_object_get_type(obj)) {
141  case json_type_object:
142  fprintf(stdout, "';\n");
143  add_json_object(obj);
144  fprintf(stdout, "json_close_object;\n");
145  break;
146  case json_type_array:
147  fprintf(stdout, "';\n");
148  add_json_array(json_object_get_array(obj));
149  fprintf(stdout, "json_close_array;\n");
150  break;
151  case json_type_string:
152  fprintf(stdout, "' '");
153  add_json_string(json_object_get_string(obj));
154  fprintf(stdout, "';\n");
155  break;
156  case json_type_boolean:
157  fprintf(stdout, "' %d;\n", json_object_get_boolean(obj));
158  break;
159  case json_type_int:
160  fprintf(stdout, "' %"PRId64";\n", json_object_get_int64(obj));
161  break;
162  case json_type_double:
163  fprintf(stdout, "' %lf;\n", json_object_get_double(obj));
164  break;
165  case json_type_null:
166  fprintf(stdout, "';\n");
167  break;
168  default:
169  return -1;
170  }
171 
172  return 0;
173 }
static int add_json_array(struct array_list *a)
Definition: jshn.c:64
static void write_key_string(const char *key)
Definition: jshn.c:99
static void add_json_string(const char *str)
Definition: jshn.c:80
static int add_json_object(json_object *obj)
Definition: jshn.c:52
uint8_t type
Definition: udebug-proto.h:0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_json_object()

static int add_json_object ( json_object *  obj)
static

Definition at line 52 of file jshn.c.

53 {
54  int ret = 0;
55 
56  json_object_object_foreach(obj, key, val) {
57  ret = add_json_element(key, val);
58  if (ret)
59  break;
60  }
61  return ret;
62 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_json_string()

static void add_json_string ( const char *  str)
static

Definition at line 80 of file jshn.c.

81 {
82  char *ptr = (char *) str;
83  int len;
84  char *c;
85 
86  while ((c = strchr(ptr, '\'')) != NULL) {
87  len = c - ptr;
88  if (len > 0)
89  fwrite(ptr, len, 1, stdout);
90  ptr = c + 1;
91  c = "'\\''";
92  fwrite(c, strlen(c), 1, stdout);
93  }
94  len = strlen(ptr);
95  if (len > 0)
96  fwrite(ptr, len, 1, stdout);
97 }
Here is the caller graph for this function:

◆ avl_strcmp_var()

static int avl_strcmp_var ( const void *  k1,
const void *  k2,
void *  ptr 
)
static

Definition at line 320 of file jshn.c.

321 {
322  const char *s1 = k1;
323  const char *s2 = k2;
324  char c1, c2;
325 
326  while (*s1 && *s1 == *s2) {
327  s1++;
328  s2++;
329  }
330 
331  c1 = *s1;
332  c2 = *s2;
333  if (c1 == '=')
334  c1 = 0;
335  if (c2 == '=')
336  c2 = 0;
337 
338  return c1 - c2;
339 }

◆ get_keys()

static char* get_keys ( const char *  prefix)
static

Definition at line 200 of file jshn.c.

201 {
202  char *keys;
203  size_t len = var_prefix_len + strlen(prefix) + sizeof("K_") + 1;
204 
205  keys = alloca(len);
206  snprintf(keys, len, "%sK_%s", var_prefix, prefix);
207  return getenv_avl(keys);
208 }
static int var_prefix_len
Definition: jshn.c:43
static char * getenv_avl(const char *key)
Definition: jshn.c:194
static const char * var_prefix
Definition: jshn.c:42
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_var()

static void get_var ( const char *  prefix,
const char **  name,
char **  var,
char **  type 
)
static

Definition at line 210 of file jshn.c.

211 {
212  char *tmpname, *varname;
213  size_t len = var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("T_");
214 
215  tmpname = alloca(len);
216 
217  snprintf(tmpname, len, "%s%s_%s", var_prefix, prefix, *name);
218  *var = getenv_avl(tmpname);
219 
220  snprintf(tmpname, len, "%sT_%s_%s", var_prefix, prefix, *name);
221  *type = getenv_avl(tmpname);
222 
223  snprintf(tmpname, len, "%sN_%s_%s", var_prefix, prefix, *name);
224  varname = getenv_avl(tmpname);
225  if (varname)
226  *name = varname;
227 }
uint8_t name[]
Definition: blobmsg.h:1
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getenv_avl()

static char* getenv_avl ( const char *  key)
static

Definition at line 194 of file jshn.c.

195 {
196  struct env_var *var = avl_find_element(&env_vars, key, var, avl);
197  return var ? var->val : NULL;
198 }
#define avl_find_element(tree, key, element, node_element)
Definition: avl.h:240
static struct avl_tree env_vars
Definition: jshn.c:39
Definition: jshn.c:47
char * val
Definition: jshn.c:49
struct avl_node avl
Definition: jshn.c:48
Here is the caller graph for this function:

◆ jshn_add_object_var()

static void jshn_add_object_var ( json_object *  obj,
bool  array,
const char *  prefix,
const char *  name 
)
static

Definition at line 231 of file jshn.c.

232 {
233  json_object *new;
234  char *var, *type;
235 
236  get_var(prefix, &name, &var, &type);
237  if (!var || !type)
238  return;
239 
240  if (!strcmp(type, "array")) {
241  new = json_object_new_array();
242  jshn_add_objects(new, var, true);
243  } else if (!strcmp(type, "object")) {
244  new = json_object_new_object();
245  jshn_add_objects(new, var, false);
246  } else if (!strcmp(type, "string")) {
247  new = json_object_new_string(var);
248  } else if (!strcmp(type, "int")) {
249  new = json_object_new_int64(atoll(var));
250  } else if (!strcmp(type, "double")) {
251  new = json_object_new_double(strtod(var, NULL));
252  } else if (!strcmp(type, "boolean")) {
253  new = json_object_new_boolean(!!atoi(var));
254  } else if (!strcmp(type, "null")) {
255  new = NULL;
256  } else {
257  return;
258  }
259 
260  if (array)
261  json_object_array_add(obj, new);
262  else
263  json_object_object_add(obj, name, new);
264 }
static void get_var(const char *prefix, const char **name, char **var, char **type)
Definition: jshn.c:210
static json_object * jshn_add_objects(json_object *obj, const char *prefix, bool array)
Definition: jshn.c:266
Here is the call graph for this function:
Here is the caller graph for this function:

◆ jshn_add_objects()

static json_object * jshn_add_objects ( json_object *  obj,
const char *  prefix,
bool  array 
)
static

Definition at line 266 of file jshn.c.

267 {
268  char *keys, *key, *brk;
269 
270  keys = get_keys(prefix);
271  if (!keys || !obj)
272  goto out;
273 
274  for (key = strtok_r(keys, " ", &brk); key;
275  key = strtok_r(NULL, " ", &brk)) {
276  jshn_add_object_var(obj, array, prefix, key);
277  }
278 
279 out:
280  return obj;
281 }
static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
Definition: jshn.c:231
static char * get_keys(const char *prefix)
Definition: jshn.c:200
Here is the call graph for this function:
Here is the caller graph for this function:

◆ jshn_format()

static int jshn_format ( bool  no_newline,
bool  indent,
FILE stream 
)
static

Definition at line 283 of file jshn.c.

284 {
285  json_object *obj;
286  const char *output;
287  char *blobmsg_output = NULL;
288  int ret = -1;
289 
290  if (!(obj = json_object_new_object()))
291  return -1;
292 
293  jshn_add_objects(obj, "J_V", false);
294  if (!(output = json_object_to_json_string(obj)))
295  goto out;
296 
297  if (indent) {
298  blob_buf_init(&b, 0);
299  if (!blobmsg_add_json_from_string(&b, output))
300  goto out;
301  if (!(blobmsg_output = blobmsg_format_json_indent(b.head, 1, 0)))
302  goto out;
303  output = blobmsg_output;
304  }
305  fprintf(stream, "%s%s", output, no_newline ? "" : "\n");
306  free(blobmsg_output);
307  ret = 0;
308 
309 out:
310  json_object_put(obj);
311  return ret;
312 }
int blob_buf_init(struct blob_buf *buf, int id)
Definition: blob.c:91
bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
Definition: blobmsg_json.c:112
static char * blobmsg_format_json_indent(struct blob_attr *attr, bool list, int indent)
Definition: blobmsg_json.h:40
static struct blob_buf b
Definition: jshn.c:40
struct blob_attr * head
Definition: blob.h:65
Here is the call graph for this function:
Here is the caller graph for this function:

◆ jshn_format_file()

static int jshn_format_file ( const char *  path,
bool  no_newline,
bool  indent 
)
static

Definition at line 379 of file jshn.c.

380 {
381  FILE *fp = NULL;
382  int ret = 0;
383 
384  fp = fopen(path, "w");
385  if (!fp) {
386  fprintf(stderr, "Error opening %s\n", path);
387  return 3;
388  }
389 
390  ret = jshn_format(no_newline, indent, fp);
391  fclose(fp);
392 
393  return ret;
394 }
static int jshn_format(bool no_newline, bool indent, FILE *stream)
Definition: jshn.c:283
FILE(GLOB test_cases "test-*.c") MACRO(ADD_FUZZER_TEST name) ADD_EXECUTABLE($
Definition: CMakeLists.txt:1
Here is the call graph for this function:

◆ jshn_parse()

static int jshn_parse ( const char *  str)
static

Definition at line 175 of file jshn.c.

176 {
177  json_object *obj;
178 
179  obj = json_tokener_parse(str);
180  if (!obj || json_object_get_type(obj) != json_type_object) {
181  if (obj)
182  json_object_put(obj);
183  fprintf(stderr, "Failed to parse message data\n");
184  return 1;
185  }
186  fprintf(stdout, "json_init;\n");
187  add_json_object(obj);
188  fflush(stdout);
189  json_object_put(obj);
190 
191  return 0;
192 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ jshn_parse_file()

static int jshn_parse_file ( const char *  path)
static

Definition at line 341 of file jshn.c.

342 {
343  struct stat sb;
344  int ret = 0;
345  char *fbuf;
346  int fd;
347 
348  if ((fd = open(path, O_RDONLY)) == -1) {
349  fprintf(stderr, "Error opening %s\n", path);
350  return 3;
351  }
352 
353  if (fstat(fd, &sb) == -1) {
354  fprintf(stderr, "Error getting size of %s\n", path);
355  close(fd);
356  return 3;
357  }
358 
359  if (!(fbuf = calloc(1, sb.st_size+1))) {
360  fprintf(stderr, "Error allocating memory for %s\n", path);
361  close(fd);
362  return 3;
363  }
364 
365  if (read(fd, fbuf, sb.st_size) != sb.st_size) {
366  fprintf(stderr, "Error reading %s\n", path);
367  free(fbuf);
368  close(fd);
369  return 3;
370  }
371 
372  ret = jshn_parse(fbuf);
373  free(fbuf);
374  close(fd);
375 
376  return ret;
377 }
static int jshn_parse(const char *str)
Definition: jshn.c:175
int fd
Definition: udebug-priv.h:27
Here is the call graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 396 of file jshn.c.

397 {
398  extern char **environ;
399  bool no_newline = false;
400  bool indent = false;
401  struct env_var *vars;
402  int i;
403  int ret = 0;
404  int ch;
405 
406  avl_init(&env_vars, avl_strcmp_var, false, NULL);
407  for (i = 0; environ[i]; i++);
408 
409  vars = calloc(i, sizeof(*vars));
410  if (!vars) {
411  fprintf(stderr, "%m\n");
412  return -1;
413  }
414  for (i = 0; environ[i]; i++) {
415  char *c;
416 
417  vars[i].avl.key = environ[i];
418  c = strchr(environ[i], '=');
419  if (!c)
420  continue;
421 
422  vars[i].val = c + 1;
423  avl_insert(&env_vars, &vars[i].avl);
424  }
425 
426  while ((ch = getopt(argc, argv, "p:nir:R:o:w")) != -1) {
427  switch(ch) {
428  case 'p':
429  var_prefix = optarg;
430  var_prefix_len = strlen(var_prefix);
431  break;
432  case 'r':
433  ret = jshn_parse(optarg);
434  goto exit;
435  case 'R':
436  ret = jshn_parse_file(optarg);
437  goto exit;
438  case 'w':
439  ret = jshn_format(no_newline, indent, stdout);
440  goto exit;
441  case 'o':
442  ret = jshn_format_file(optarg, no_newline, indent);
443  goto exit;
444  case 'n':
445  no_newline = true;
446  break;
447  case 'i':
448  indent = true;
449  break;
450  default:
451  free(vars);
452  return usage(argv[0]);
453  }
454  }
455 
456  free(vars);
457  return usage(argv[0]);
458 
459 exit:
460  free(vars);
461  return ret;
462 }
int avl_insert(struct avl_tree *tree, struct avl_node *new)
Definition: avl.c:220
void avl_init(struct avl_tree *tree, avl_tree_comp comp, bool allow_dups, void *ptr)
Definition: avl.c:92
static int avl_strcmp_var(const void *k1, const void *k2, void *ptr)
Definition: jshn.c:320
static int usage(const char *progname)
Definition: jshn.c:314
static int jshn_format_file(const char *path, bool no_newline, bool indent)
Definition: jshn.c:379
static int jshn_parse_file(const char *path)
Definition: jshn.c:341
const void * key
Definition: avl.h:84

◆ usage()

static int usage ( const char *  progname)
static

Definition at line 314 of file jshn.c.

315 {
316  fprintf(stderr, "Usage: %s [-n] [-i] -r <message>|-R <file>|-o <file>|-p <prefix>|-w\n", progname);
317  return 2;
318 }

◆ write_key_string()

static void write_key_string ( const char *  key)
static

Definition at line 99 of file jshn.c.

100 {
101  while (*key) {
102  putc(isalnum(*key) ? *key : '_', stdout);
103  key++;
104  }
105 }
Here is the caller graph for this function:

Variable Documentation

◆ b

struct blob_buf b = { 0 }
static

Definition at line 1 of file jshn.c.

◆ env_vars

struct avl_tree env_vars
static

Definition at line 1 of file jshn.c.

◆ var_prefix

const char* var_prefix = ""
static

Definition at line 42 of file jshn.c.

◆ var_prefix_len

int var_prefix_len = 0
static

Definition at line 43 of file jshn.c.