stew

a monorepo of some sort
Log | Files | Refs

commit 08ee99629d7750bf3811d2e7f9f8907be149ac4a
parent 3dfb21a9b25123a0f525c4bfb81630980717fff0
Author: rpa <rpa@grass>
Date:   Sat, 18 Nov 2023 20:50:22 +0000

src/json: a formatter

Diffstat:
Asrc/json/fmt.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/json/mkfile | 11+++++++++++
2 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/src/json/fmt.c b/src/json/fmt.c @@ -0,0 +1,51 @@ +/* read json, print pretty json */ +#include <u.h> +#include <libc.h> +#include <json.h> + +#define RBUFSIZE 8192 + +typedef struct Rbuf Rbuf; +struct Rbuf { + char buf[RBUFSIZE]; + int n; + Rbuf *next; +}; + +void +usage(void) +{ + fprint(2, "%s takes no arguments\n", argv0); + exits("usage"); +} + +void +main(int argc, char **argv) +{ + argv0 = argv[0]; + if (argc != 1) usage(); + + Rbuf *start = mallocz(sizeof(Rbuf), 1); + Rbuf *rpt = start; + int total = 0, n; + while ((n = read(0, rpt->buf, RBUFSIZE)) > 0) { + fprint(2, "%d\n", n); + rpt->n = n; + total += n; + rpt->next = mallocz(sizeof(Rbuf), 1); + rpt=rpt->next; + } + fprint(2, "total %d\n", total); + rpt = start; + char *buf = mallocz(total+1, 1); + n = 0; + while (rpt != nil) { + memcpy(buf+n, rpt->buf, rpt->n); + n += rpt->n; + rpt=rpt->next; + } + JSON *j = jsonparse(buf); + if (j == nil) sysfatal("%r"); + JSONfmtinstall(); + print("%J\n", j); +} diff --git a/src/json/mkfile b/src/json/mkfile @@ -0,0 +1,11 @@ +</$objtype/mkfile +BIN=/$objtype/bin/json +TARG=fmt +OFILES=fmt.$O + +install:V: installdir + +installdir:V: + mkdir -p $BIN + +</sys/src/cmd/mkone