X-Git-Url: http://git.openwrt.org/?p=project%2Flibubox.git;a=blobdiff_plain;f=jshn.c;h=f71e6e685ddcd43117238b40714b4f7140f03cb7;hp=f7a46dee85b0cbc5450fc0d7838c07035a20d41b;hb=f59f33f2c723b3c52f221234656d0e522a4dae00;hpb=a1a97eb11e89c420b84a659a88a4e72c7f04367d diff --git a/jshn.c b/jshn.c index f7a46de..f71e6e6 100644 --- a/jshn.c +++ b/jshn.c @@ -1,4 +1,24 @@ -#include +/* + * 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. + */ +#ifdef JSONC + #include +#else + #include +#endif + #include #include #include @@ -9,6 +29,9 @@ #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) @@ -89,6 +112,9 @@ static int add_json_element(const char *key, json_object *obj) case json_type_int: type = "int"; break; + case json_type_double: + type = "double"; + break; default: return -1; } @@ -118,6 +144,9 @@ static int add_json_element(const char *key, json_object *obj) case json_type_int: fprintf(stdout, "' %d;\n", json_object_get_int(obj)); break; + case json_type_double: + fprintf(stdout, "' %lf;\n", json_object_get_double(obj)); + break; default: return -1; } @@ -145,8 +174,8 @@ 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); } @@ -154,12 +183,15 @@ static void get_var(const char *prefix, const char **name, char **var, char **ty { char *tmpname, *varname; - tmpname = alloca(strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_")); - sprintf(tmpname, "TYPE_%s_%s", prefix, *name); - *var = getenv(tmpname + 5); + 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); + + sprintf(tmpname, "%sTYPE_%s_%s", var_prefix, prefix, *name); *type = getenv(tmpname); - memcpy(tmpname, "NAME", 4); + sprintf(tmpname, "%sNAME_%s_%s", var_prefix, prefix, *name); varname = getenv(tmpname); if (varname) *name = varname; @@ -186,6 +218,8 @@ static void jshn_add_object_var(json_object *obj, bool array, const char *prefix new = json_object_new_string(var); } else if (!strcmp(type, "int")) { new = json_object_new_int(atoi(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 { @@ -238,8 +272,12 @@ int main(int argc, char **argv) bool no_newline = false; int ch; - while ((ch = getopt(argc, argv, "nr: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':