projects
/
project
/
libubox.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
utils: move ARRAY_SIZE from uloop to utils.h
[project/libubox.git]
/
jshn.c
diff --git
a/jshn.c
b/jshn.c
index
e2f0b83
..
dc002c1
100644
(file)
--- a/
jshn.c
+++ b/
jshn.c
@@
-1,14
+1,17
@@
#include <json/json.h>
#include <json/json.h>
-#include <libubox/list.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <getopt.h>
+#include "list.h"
#define MAX_VARLEN 256
#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)
static int add_json_element(const char *key, json_object *obj);
static int add_json_object(json_object *obj)
@@
-145,19
+148,27
@@
static char *get_keys(const char *prefix)
{
char *keys;
{
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);
}
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);
*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);
}
static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
@@
-167,7
+178,7
@@
static void jshn_add_object_var(json_object *obj, bool array, const char *prefix
json_object *new;
char *var, *type;
json_object *new;
char *var, *type;
- get_var(prefix, name, &var, &type);
+ get_var(prefix,
&
name, &var, &type);
if (!var || !type)
return;
if (!var || !type)
return;
@@
-233,8
+244,12
@@
int main(int argc, char **argv)
bool no_newline = false;
int ch;
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) {
switch(ch) {
+ case 'p':
+ var_prefix = optarg;
+ var_prefix_len = strlen(var_prefix);
+ break;
case 'r':
return jshn_parse(optarg);
case 'w':
case 'r':
return jshn_parse(optarg);
case 'w':