]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.5.2/examples/jxpretty/jxpretty.c
buildsys: switch source download to quincy
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.5.2 / examples / jxpretty / jxpretty.c
1 /*
2 * Pretty print JSON from stdin into indented JX.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "duktape.h"
8
9 static duk_ret_t do_jxpretty(duk_context *ctx) {
10 FILE *f = stdin;
11 char buf[4096];
12 size_t ret;
13
14 for (;;) {
15 if (ferror(f)) {
16 duk_error(ctx, DUK_ERR_ERROR, "ferror() on stdin");
17 }
18 if (feof(f)) {
19 break;
20 }
21
22 ret = fread(buf, 1, sizeof(buf), f);
23 #if 0
24 fprintf(stderr, "Read: %ld\n", (long) ret);
25 fflush(stderr);
26 #endif
27 if (ret == 0) {
28 break;
29 }
30
31 duk_require_stack(ctx, 1);
32 duk_push_lstring(ctx, (const char *) buf, ret);
33 }
34
35 duk_concat(ctx, duk_get_top(ctx));
36
37 duk_eval_string(ctx, "(function (v) { print(Duktape.enc('jx', JSON.parse(v), null, 4)); })");
38 duk_insert(ctx, -2);
39 duk_call(ctx, 1);
40
41 return 0;
42 }
43
44 int main(int argc, char *argv[]) {
45 duk_context *ctx;
46 duk_int_t rc;
47
48 /* suppress warnings */
49 (void) argc;
50 (void) argv;
51
52 ctx = duk_create_heap_default();
53
54 rc = duk_safe_call(ctx, do_jxpretty, 0 /*nargs*/, 1 /*nrets*/);
55 if (rc) {
56 fprintf(stderr, "ERROR: %s\n", duk_safe_to_string(ctx, -1));
57 fflush(stderr);
58 }
59
60 duk_destroy_heap(ctx);
61
62 return 0;
63 }