Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/jshn.c
diff options
context:
space:
mode:
authorJohn Crispin <blogic@openwrt.org>2014-08-04 21:26:18 +0400
committerJohn Crispin <blogic@openwrt.org>2014-08-04 22:01:16 +0400
commitdffbc09baf71b294185a36048166d00066d433b5 (patch)
tree23dd63522bc5ed9e9ed48f39fa55dc5cbaa73dcd /jshn.c
parent042f1a2546660e8aadde2ee65f5fe7a9cc9b1378 (diff)
jshn: pretty print indented output with jshn -i -w
Signed-off-by: John Crispin <blogic@openwrt.org>
Diffstat (limited to 'jshn.c')
-rw-r--r--jshn.c27
1 files changed, 21 insertions, 6 deletions
diff --git a/jshn.c b/jshn.c
index f71e6e6..cec48a0 100644
--- a/jshn.c
+++ b/jshn.c
@@ -27,8 +27,13 @@
#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;
@@ -249,30 +254,37 @@ out:
return obj;
}
-static int jshn_format(bool no_newline)
+static int jshn_format(bool no_newline, bool indent)
{
json_object *obj;
+ const char *output;
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");
+ output = json_object_to_json_string(obj);
+ if (indent) {
+ blob_buf_init(&b, 0);
+ blobmsg_add_json_from_string(&b, output);
+ output = blobmsg_format_json_indent(b.head, 1, 0);
+ }
+ fprintf(stdout, "%s%s", output, no_newline ? "" : "\n");
json_object_put(obj);
return 0;
}
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;
@@ -281,10 +293,13 @@ int main(int argc, char **argv)
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]);
}