jshn: add functionality to read big JSON
authorChristian Beier <dontmind@freeshell.org>
Thu, 18 Jan 2018 20:21:09 +0000 (21:21 +0100)
committerJohn Crispin <john@phrozen.org>
Mon, 22 Jan 2018 08:21:03 +0000 (09:21 +0100)
The existing read functionality feeds the complete JSON to jshn as a
cmdline argument, leading to `-ash: jshn: Argument list too long`
errors for JSONs bigger than ca. 100KB.

This commit adds the ability to read the JSON directly from a file if
wanted, removing this shell-imposed size limit.

Tested on x86-64 and ar71xx. An mmap()-based solution was also evaluated,
but found to make no performance difference on either platform.

Signed-off-by: Christian Beier <dontmind@freeshell.org>
jshn.c
sh/jshn.sh

diff --git a/jshn.c b/jshn.c
index 3188af56cc38e46b849d7324e4580ab125a96b54..7f67746be559cfea5a10d7c8b81809f20f7d8fdc 100644 (file)
--- a/jshn.c
+++ b/jshn.c
@@ -25,6 +25,8 @@
 #include <stdbool.h>
 #include <ctype.h>
 #include <getopt.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include "list.h"
 
 #include "avl.h"
@@ -305,7 +307,7 @@ out:
 
 static int usage(const char *progname)
 {
-       fprintf(stderr, "Usage: %s [-n] [-i] -r <message>|-w\n", progname);
+       fprintf(stderr, "Usage: %s [-n] [-i] -r <message>|-R <file>|-w\n", progname);
        return 2;
 }
 
@@ -338,6 +340,10 @@ int main(int argc, char **argv)
        struct env_var *vars;
        int i;
        int ch;
+       int fd;
+       struct stat sb;
+       char *fbuf;
+       int ret;
 
        avl_init(&env_vars, avl_strcmp_var, false, NULL);
        for (i = 0; environ[i]; i++);
@@ -359,7 +365,7 @@ int main(int argc, char **argv)
                avl_insert(&env_vars, &vars[i].avl);
        }
 
-       while ((ch = getopt(argc, argv, "p:nir:w")) != -1) {
+       while ((ch = getopt(argc, argv, "p:nir:R:w")) != -1) {
                switch(ch) {
                case 'p':
                        var_prefix = optarg;
@@ -367,6 +373,31 @@ int main(int argc, char **argv)
                        break;
                case 'r':
                        return jshn_parse(optarg);
+               case 'R':
+                       if ((fd = open(optarg, O_RDONLY)) == -1) {
+                               fprintf(stderr, "Error opening %s\n", optarg);
+                               return 3;
+                       }
+                       if (fstat(fd, &sb) == -1) {
+                               fprintf(stderr, "Error getting size of %s\n", optarg);
+                               close(fd);
+                               return 3;
+                       }
+                       if (!(fbuf = malloc(sb.st_size))) {
+                               fprintf(stderr, "Error allocating memory for %s\n", optarg);
+                               close(fd);
+                               return 3;
+                       }
+                       if (read(fd, fbuf, sb.st_size) != sb.st_size) {
+                               fprintf(stderr, "Error reading %s\n", optarg);
+                               free(fbuf);
+                               close(fd);
+                               return 3;
+                       }
+                       ret = jshn_parse(fbuf);
+                       free(fbuf);
+                       close(fd);
+                       return ret;
                case 'w':
                        return jshn_format(no_newline, indent);
                case 'n':
index 1090814bc8fb2558053cb43cd26030536f4883c2..66baccbcaacff184ec25371fcdaf8159f1223dd0 100644 (file)
@@ -180,6 +180,10 @@ json_load() {
        eval "`jshn -r "$1"`"
 }
 
+json_load_file() {
+       eval "`jshn -R "$1"`"
+}
+
 json_dump() {
        jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} -w 
 }