stew

a monorepo of some sort
git clone git://git.nsmpr.xyz/stew.git
Log | Files | Refs

fmt.c (880B)


      1 /* read json, print pretty json */
      2 #include <u.h>
      3 #include <libc.h>
      4 #include <json.h>
      5 
      6 #define RBUFSIZE 8192
      7 
      8 typedef struct Rbuf Rbuf;
      9 struct Rbuf {
     10 	char buf[RBUFSIZE];
     11 	int n;
     12 	Rbuf *next;
     13 };
     14 
     15 void
     16 usage(void)
     17 {
     18 	fprint(2, "%s takes no arguments\n", argv0);
     19 	exits("usage");
     20 }
     21 
     22 void
     23 main(int argc, char **argv)
     24 {
     25 	argv0 = argv[0];
     26 	if (argc != 1) usage();
     27 
     28 	Rbuf *start = mallocz(sizeof(Rbuf), 1);
     29 	Rbuf *rpt = start;
     30 	int total = 0, n;
     31 	while ((n = read(0, rpt->buf, RBUFSIZE)) > 0) {
     32 		fprint(2, "%d\n", n);
     33 		rpt->n = n;
     34 		total += n;
     35 		rpt->next = mallocz(sizeof(Rbuf), 1);
     36 		rpt=rpt->next;
     37 	}
     38 	fprint(2, "total %d\n", total);
     39 	rpt = start;
     40 	char *buf = mallocz(total+1, 1);
     41 	n = 0;
     42 	while (rpt != nil) {
     43 		memcpy(buf+n, rpt->buf, rpt->n);
     44 		n += rpt->n;
     45 		rpt=rpt->next;
     46 	}
     47 	JSON *j = jsonparse(buf);
     48 	if (j == nil) sysfatal("%r");
     49 	JSONfmtinstall();
     50 	print("%J\n", j);
     51 }