X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=jshn.c;h=0aa120c925dd673d090a4586812b3d81108ffc6c;hb=eb30a03048f8;hp=e2d90220335c6bbc505dd4de4bd26477abb0e752;hpb=dfe446e2a981eaa83cb41df3840ca7c649dc7527;p=oweals%2Flibubox.git diff --git a/jshn.c b/jshn.c index e2d9022..0aa120c 100644 --- a/jshn.c +++ b/jshn.c @@ -25,6 +25,9 @@ #include #include #include +#include +#include +#include #include "list.h" #include "avl.h" @@ -105,9 +108,6 @@ static int add_json_element(const char *key, json_object *obj) { char *type; - if (!obj) - return -1; - switch (json_object_get_type(obj)) { case json_type_object: type = "object"; @@ -127,6 +127,9 @@ static int add_json_element(const char *key, json_object *obj) case json_type_double: type = "double"; break; + case json_type_null: + type = "null"; + break; default: return -1; } @@ -154,11 +157,14 @@ static int add_json_element(const char *key, json_object *obj) fprintf(stdout, "' %d;\n", json_object_get_boolean(obj)); break; case json_type_int: - fprintf(stdout, "' %d;\n", json_object_get_int(obj)); + fprintf(stdout, "' %"PRId64";\n", json_object_get_int64(obj)); break; case json_type_double: fprintf(stdout, "' %lf;\n", json_object_get_double(obj)); break; + case json_type_null: + fprintf(stdout, "';\n"); + break; default: return -1; } @@ -235,11 +241,13 @@ static void jshn_add_object_var(json_object *obj, bool array, const char *prefix } else if (!strcmp(type, "string")) { new = json_object_new_string(var); } else if (!strcmp(type, "int")) { - new = json_object_new_int(atoi(var)); + new = json_object_new_int64(atoll(var)); } else if (!strcmp(type, "double")) { new = json_object_new_double(strtod(var, NULL)); } else if (!strcmp(type, "boolean")) { new = json_object_new_boolean(!!atoi(var)); + } else if (!strcmp(type, "null")) { + new = NULL; } else { return; } @@ -267,7 +275,7 @@ out: return obj; } -static int jshn_format(bool no_newline, bool indent) +static int jshn_format(bool no_newline, bool indent, FILE *stream) { json_object *obj; const char *output; @@ -289,7 +297,7 @@ static int jshn_format(bool no_newline, bool indent) goto out; output = blobmsg_output; } - fprintf(stdout, "%s%s", output, no_newline ? "" : "\n"); + fprintf(stream, "%s%s", output, no_newline ? "" : "\n"); free(blobmsg_output); ret = 0; @@ -300,7 +308,7 @@ out: static int usage(const char *progname) { - fprintf(stderr, "Usage: %s [-n] [-i] -r |-w\n", progname); + fprintf(stderr, "Usage: %s [-n] [-i] -r |-R |-w\n", progname); return 2; } @@ -333,11 +341,20 @@ int main(int argc, char **argv) struct env_var *vars; int i; int ch; + int fd; + FILE *fp = NULL; + struct stat sb; + char *fbuf; + int ret; avl_init(&env_vars, avl_strcmp_var, false, NULL); for (i = 0; environ[i]; i++); vars = calloc(i, sizeof(*vars)); + if (!vars) { + fprintf(stderr, "%m\n"); + return -1; + } for (i = 0; environ[i]; i++) { char *c; @@ -350,7 +367,7 @@ int main(int argc, char **argv) avl_insert(&env_vars, &vars[i].avl); } - while ((ch = getopt(argc, argv, "p:nir:w")) != -1) { + while ((ch = getopt(argc, argv, "p:nir:R:o:w")) != -1) { switch(ch) { case 'p': var_prefix = optarg; @@ -358,8 +375,42 @@ int main(int argc, char **argv) break; case 'r': return jshn_parse(optarg); + case 'R': + if ((fd = open(optarg, O_RDONLY)) == -1) { + fprintf(stderr, "Error opening %s\n", optarg); + return 3; + } + if (fstat(fd, &sb) == -1) { + fprintf(stderr, "Error getting size of %s\n", optarg); + close(fd); + return 3; + } + if (!(fbuf = malloc(sb.st_size))) { + fprintf(stderr, "Error allocating memory for %s\n", optarg); + close(fd); + return 3; + } + if (read(fd, fbuf, sb.st_size) != sb.st_size) { + fprintf(stderr, "Error reading %s\n", optarg); + free(fbuf); + close(fd); + return 3; + } + ret = jshn_parse(fbuf); + free(fbuf); + close(fd); + return ret; case 'w': - return jshn_format(no_newline, indent); + return jshn_format(no_newline, indent, stdout); + case 'o': + fp = fopen(optarg, "w"); + if (!fp) { + fprintf(stderr, "Error opening %s\n", optarg); + return 3; + } + jshn_format(no_newline, indent, fp); + fclose(fp); + return 0; case 'n': no_newline = true; break;