X-Git-Url: http://git.archive.openwrt.org/?p=project%2Flibubox.git;a=blobdiff_plain;f=jshn.c;h=9cbdb3194489d180406c8f656fa98d5c014c82aa;hp=a2d711f38e818aeff92c6b8af5d987cdb99a87ea;hb=d01922625d21f97fcab757d0299586178333f1e0;hpb=fda6079b30a4f47daf95c8b93b9c86afee008f45 diff --git a/jshn.c b/jshn.c index a2d711f..9cbdb31 100644 --- a/jshn.c +++ b/jshn.c @@ -1,14 +1,32 @@ +/* + * Copyright (C) 2011-2013 Felix Fietkau + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ #include -#include #include #include #include #include #include #include +#include "list.h" #define MAX_VARLEN 256 +static const char *var_prefix = ""; +static int var_prefix_len = 0; + static int add_json_element(const char *key, json_object *obj); static int add_json_object(json_object *obj) @@ -145,19 +163,27 @@ static char *get_keys(const char *prefix) { char *keys; - keys = alloca(strlen(prefix) + sizeof("KEYS_") + 1); - sprintf(keys, "KEYS_%s", prefix); + keys = alloca(var_prefix_len + strlen(prefix) + sizeof("KEYS_") + 1); + sprintf(keys, "%sKEYS_%s", var_prefix, prefix); return getenv(keys); } -static void get_var(const char *prefix, const char *name, char **var, char **type) +static void get_var(const char *prefix, const char **name, char **var, char **type) { - char *tmpname; + char *tmpname, *varname; + + tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_")); + + sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name); + *var = getenv(tmpname); - tmpname = alloca(strlen(prefix) + 1 + strlen(name) + 1 + sizeof("TYPE_")); - sprintf(tmpname, "TYPE_%s_%s", prefix, name); - *var = getenv(tmpname + 5); + sprintf(tmpname, "%sTYPE_%s_%s", var_prefix, prefix, *name); *type = getenv(tmpname); + + sprintf(tmpname, "%sNAME_%s_%s", var_prefix, prefix, *name); + varname = getenv(tmpname); + if (varname) + *name = varname; } static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array); @@ -167,7 +193,7 @@ static void jshn_add_object_var(json_object *obj, bool array, const char *prefix json_object *new; char *var, *type; - get_var(prefix, name, &var, &type); + get_var(prefix, &name, &var, &type); if (!var || !type) return; @@ -210,33 +236,42 @@ out: return obj; } -static int jshn_format(void) +static int jshn_format(bool no_newline) { json_object *obj; obj = json_object_new_object(); jshn_add_objects(obj, "JSON_VAR", false); - fprintf(stdout, "%s\n", json_object_to_json_string(obj)); + fprintf(stdout, "%s%s", json_object_to_json_string(obj), + no_newline ? "" : "\n"); json_object_put(obj); return 0; } static int usage(const char *progname) { - fprintf(stderr, "Usage: %s -r |-w\n", progname); + fprintf(stderr, "Usage: %s [-n] -r |-w\n", progname); return 2; } int main(int argc, char **argv) { + bool no_newline = false; int ch; - while ((ch = getopt(argc, argv, "r:w")) != -1) { + while ((ch = getopt(argc, argv, "p:nr:w")) != -1) { switch(ch) { + case 'p': + var_prefix = optarg; + var_prefix_len = strlen(var_prefix); + break; case 'r': return jshn_parse(optarg); case 'w': - return jshn_format(); + return jshn_format(no_newline); + case 'n': + no_newline = true; + break; default: return usage(argv[0]); }