#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.
|
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) |
|
◆ MAX_VARLEN
◆ add_json_array()
static int add_json_array |
( |
struct array_list * |
a | ) |
|
|
static |
Definition at line 64 of file jshn.c.
70 for (i = 0, len = array_list_length(a); i < len; i++) {
71 snprintf(seq,
sizeof(seq),
"%d", i);
static int add_json_element(const char *key, json_object *obj)
◆ add_json_element()
static int add_json_element |
( |
const char * |
key, |
|
|
json_object * |
obj |
|
) |
| |
|
static |
Definition at line 107 of file jshn.c.
111 switch (json_object_get_type(obj)) {
112 case json_type_object:
115 case json_type_array:
118 case json_type_string:
121 case json_type_boolean:
127 case json_type_double:
137 fprintf(stdout,
"json_add_%s '",
type);
140 switch (json_object_get_type(obj)) {
141 case json_type_object:
142 fprintf(stdout,
"';\n");
144 fprintf(stdout,
"json_close_object;\n");
146 case json_type_array:
147 fprintf(stdout,
"';\n");
149 fprintf(stdout,
"json_close_array;\n");
151 case json_type_string:
152 fprintf(stdout,
"' '");
154 fprintf(stdout,
"';\n");
156 case json_type_boolean:
157 fprintf(stdout,
"' %d;\n", json_object_get_boolean(obj));
160 fprintf(stdout,
"' %"PRId64
";\n", json_object_get_int64(obj));
162 case json_type_double:
163 fprintf(stdout,
"' %lf;\n", json_object_get_double(obj));
166 fprintf(stdout,
"';\n");
static int add_json_array(struct array_list *a)
static void write_key_string(const char *key)
static void add_json_string(const char *str)
static int add_json_object(json_object *obj)
◆ add_json_object()
static int add_json_object |
( |
json_object * |
obj | ) |
|
|
static |
Definition at line 52 of file jshn.c.
56 json_object_object_foreach(obj, key, val) {
◆ add_json_string()
static void add_json_string |
( |
const char * |
str | ) |
|
|
static |
Definition at line 80 of file jshn.c.
82 char *ptr = (
char *) str;
86 while ((c = strchr(ptr,
'\'')) != NULL) {
89 fwrite(ptr, len, 1, stdout);
92 fwrite(c, strlen(c), 1, stdout);
96 fwrite(ptr, len, 1, stdout);
◆ 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.
326 while (*s1 && *s1 == *s2) {
◆ get_keys()
static char* get_keys |
( |
const char * |
prefix | ) |
|
|
static |
Definition at line 200 of file jshn.c.
206 snprintf(keys, len,
"%sK_%s",
var_prefix, prefix);
static int var_prefix_len
static char * getenv_avl(const char *key)
static const char * var_prefix
◆ 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.
212 char *tmpname, *varname;
215 tmpname = alloca(len);
◆ getenv_avl()
static char* getenv_avl |
( |
const char * |
key | ) |
|
|
static |
Definition at line 194 of file jshn.c.
197 return var ? var->
val : NULL;
#define avl_find_element(tree, key, element, node_element)
static struct avl_tree env_vars
◆ 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.
240 if (!strcmp(
type,
"array")) {
241 new = json_object_new_array();
243 }
else if (!strcmp(
type,
"object")) {
244 new = json_object_new_object();
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")) {
261 json_object_array_add(obj,
new);
263 json_object_object_add(obj,
name,
new);
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)
◆ 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.
268 char *keys, *key, *brk;
274 for (key = strtok_r(keys,
" ", &brk); key;
275 key = strtok_r(NULL,
" ", &brk)) {
static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
static char * get_keys(const char *prefix)
◆ jshn_format()
static int jshn_format |
( |
bool |
no_newline, |
|
|
bool |
indent, |
|
|
FILE * |
stream |
|
) |
| |
|
static |
Definition at line 283 of file jshn.c.
287 char *blobmsg_output = NULL;
290 if (!(obj = json_object_new_object()))
294 if (!(output = json_object_to_json_string(obj)))
303 output = blobmsg_output;
305 fprintf(stream,
"%s%s", output, no_newline ?
"" :
"\n");
306 free(blobmsg_output);
310 json_object_put(obj);
int blob_buf_init(struct blob_buf *buf, int id)
bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
static char * blobmsg_format_json_indent(struct blob_attr *attr, bool list, int indent)
◆ 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.
384 fp = fopen(path,
"w");
386 fprintf(stderr,
"Error opening %s\n", path);
static int jshn_format(bool no_newline, bool indent, FILE *stream)
FILE(GLOB test_cases "test-*.c") MACRO(ADD_FUZZER_TEST name) ADD_EXECUTABLE($
◆ jshn_parse()
static int jshn_parse |
( |
const char * |
str | ) |
|
|
static |
Definition at line 175 of file jshn.c.
179 obj = json_tokener_parse(str);
180 if (!obj || json_object_get_type(obj) != json_type_object) {
182 json_object_put(obj);
183 fprintf(stderr,
"Failed to parse message data\n");
186 fprintf(stdout,
"json_init;\n");
189 json_object_put(obj);
◆ jshn_parse_file()
static int jshn_parse_file |
( |
const char * |
path | ) |
|
|
static |
Definition at line 341 of file jshn.c.
348 if ((
fd = open(path, O_RDONLY)) == -1) {
349 fprintf(stderr,
"Error opening %s\n", path);
353 if (fstat(
fd, &sb) == -1) {
354 fprintf(stderr,
"Error getting size of %s\n", path);
359 if (!(fbuf = calloc(1, sb.st_size+1))) {
360 fprintf(stderr,
"Error allocating memory for %s\n", path);
365 if (read(
fd, fbuf, sb.st_size) != sb.st_size) {
366 fprintf(stderr,
"Error reading %s\n", path);
static int jshn_parse(const char *str)
◆ main()
int main |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
Definition at line 396 of file jshn.c.
398 extern char **environ;
399 bool no_newline =
false;
407 for (i = 0; environ[i]; i++);
409 vars = calloc(i,
sizeof(*vars));
411 fprintf(stderr,
"%m\n");
414 for (i = 0; environ[i]; i++) {
417 vars[i].
avl.
key = environ[i];
418 c = strchr(environ[i],
'=');
426 while ((ch = getopt(argc, argv,
"p:nir:R:o:w")) != -1) {
452 return usage(argv[0]);
457 return usage(argv[0]);
int avl_insert(struct avl_tree *tree, struct avl_node *new)
void avl_init(struct avl_tree *tree, avl_tree_comp comp, bool allow_dups, void *ptr)
static int avl_strcmp_var(const void *k1, const void *k2, void *ptr)
static int usage(const char *progname)
static int jshn_format_file(const char *path, bool no_newline, bool indent)
static int jshn_parse_file(const char *path)
◆ usage()
static int usage |
( |
const char * |
progname | ) |
|
|
static |
Definition at line 314 of file jshn.c.
316 fprintf(stderr,
"Usage: %s [-n] [-i] -r <message>|-R <file>|-o <file>|-p <prefix>|-w\n", progname);
◆ write_key_string()
static void write_key_string |
( |
const char * |
key | ) |
|
|
static |
Definition at line 99 of file jshn.c.
102 putc(isalnum(*key) ? *key :
'_', stdout);
Definition at line 1 of file jshn.c.
◆ env_vars
Definition at line 1 of file jshn.c.
◆ var_prefix
const char* var_prefix = "" |
|
static |
◆ var_prefix_len