* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <json/json.h>
+#ifdef JSONC
+ #include <json.h>
+#else
+ #include <json/json.h>
+#endif
+
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include "list.h"
+#include "blob.h"
+#include "blobmsg_json.h"
+
#define MAX_VARLEN 256
+static struct blob_buf b = { 0 };
+
static const char *var_prefix = "";
static int var_prefix_len = 0;
case json_type_int:
type = "int";
break;
+ case json_type_double:
+ type = "double";
+ break;
default:
return -1;
}
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;
}
json_object *obj;
obj = json_tokener_parse(str);
- if (is_error(obj) || json_object_get_type(obj) != json_type_object) {
+ if (!obj || json_object_get_type(obj) != json_type_object) {
fprintf(stderr, "Failed to parse message data\n");
return 1;
}
{
char *keys;
- keys = alloca(var_prefix_len + strlen(prefix) + sizeof("KEYS_") + 1);
- sprintf(keys, "%sKEYS_%s", var_prefix, prefix);
+ keys = alloca(var_prefix_len + strlen(prefix) + sizeof("K_") + 1);
+ sprintf(keys, "%sK_%s", var_prefix, prefix);
return getenv(keys);
}
{
char *tmpname, *varname;
- tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_"));
+ tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("T_"));
sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name);
*var = getenv(tmpname);
- sprintf(tmpname, "%sTYPE_%s_%s", var_prefix, prefix, *name);
+ sprintf(tmpname, "%sT_%s_%s", var_prefix, prefix, *name);
*type = getenv(tmpname);
- sprintf(tmpname, "%sNAME_%s_%s", var_prefix, prefix, *name);
+ sprintf(tmpname, "%sN_%s_%s", var_prefix, prefix, *name);
varname = getenv(tmpname);
if (varname)
*name = varname;
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 {
return obj;
}
-static int jshn_format(bool no_newline)
+static int jshn_format(bool no_newline, bool indent)
{
json_object *obj;
+ const char *output;
+ char *blobmsg_output = NULL;
+ int ret = -1;
+
+ if (!(obj = json_object_new_object()))
+ return -1;
- obj = json_object_new_object();
- jshn_add_objects(obj, "JSON_VAR", false);
- fprintf(stdout, "%s%s", json_object_to_json_string(obj),
- no_newline ? "" : "\n");
+ jshn_add_objects(obj, "J_V", false);
+ if (!(output = json_object_to_json_string(obj)))
+ goto out;
+
+ if (indent) {
+ blob_buf_init(&b, 0);
+ if (!blobmsg_add_json_from_string(&b, output))
+ goto out;
+ if (!(blobmsg_output = blobmsg_format_json_indent(b.head, 1, 0)))
+ goto out;
+ output = blobmsg_output;
+ }
+ fprintf(stdout, "%s%s", output, no_newline ? "" : "\n");
+ free(blobmsg_output);
+ ret = 0;
+
+out:
json_object_put(obj);
- return 0;
+ return ret;
}
static int usage(const char *progname)
{
- fprintf(stderr, "Usage: %s [-n] -r <message>|-w\n", progname);
+ fprintf(stderr, "Usage: %s [-n] [-i] -r <message>|-w\n", progname);
return 2;
}
int main(int argc, char **argv)
{
bool no_newline = false;
+ bool indent = false;
int ch;
- while ((ch = getopt(argc, argv, "p:nr:w")) != -1) {
+ while ((ch = getopt(argc, argv, "p:nir:w")) != -1) {
switch(ch) {
case 'p':
var_prefix = optarg;
case 'r':
return jshn_parse(optarg);
case 'w':
- return jshn_format(no_newline);
+ return jshn_format(no_newline, indent);
case 'n':
no_newline = true;
break;
+ case 'i':
+ indent = true;
+ break;
default:
return usage(argv[0]);
}