19 #include <json/json.h>
37 #define MAX_VARLEN 256
56 json_object_object_foreach(obj,
key, val) {
70 for (i = 0, len = array_list_length(a); i < len; i++) {
71 snprintf(seq,
sizeof(seq),
"%d", i);
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);
102 putc(isalnum(*
key) ? *
key :
'_', stdout);
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");
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);
197 return var ? var->
val : NULL;
206 snprintf(keys, len,
"%sK_%s",
var_prefix, prefix);
212 char *tmpname, *varname;
215 tmpname = alloca(len);
229 static json_object *
jshn_add_objects(json_object *obj,
const char *prefix,
bool array);
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);
268 char *keys, *key, *brk;
274 for (key = strtok_r(keys,
" ", &brk); key;
275 key = strtok_r(NULL,
" ", &brk)) {
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);
314 static int usage(
const char *progname)
316 fprintf(stderr,
"Usage: %s [-n] [-i] -r <message>|-R <file>|-o <file>|-p <prefix>|-w\n", progname);
326 while (*s1 && *s1 == *s2) {
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);
384 fp = fopen(path,
"w");
386 fprintf(stderr,
"Error opening %s\n", path);
396 int main(
int argc,
char **argv)
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)
#define avl_find_element(tree, key, element, node_element)
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)
static int avl_strcmp_var(const void *k1, const void *k2, void *ptr)
static int jshn_parse(const char *str)
static int add_json_element(const char *key, json_object *obj)
static void get_var(const char *prefix, const char **name, char **var, char **type)
int main(int argc, char **argv)
static int usage(const char *progname)
static int add_json_array(struct array_list *a)
static int jshn_format_file(const char *path, bool no_newline, bool indent)
static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
static int jshn_parse_file(const char *path)
static int var_prefix_len
static char * getenv_avl(const char *key)
static json_object * jshn_add_objects(json_object *obj, const char *prefix, bool array)
static void write_key_string(const char *key)
static const char * var_prefix
static void add_json_string(const char *str)
static char * get_keys(const char *prefix)
static struct avl_tree env_vars
static int jshn_format(bool no_newline, bool indent, FILE *stream)
static int add_json_object(json_object *obj)
FILE(GLOB test_cases "test-*.c") MACRO(ADD_FUZZER_TEST name) ADD_EXECUTABLE($