File: fmt.c

package info (click to toggle)
jose 10-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,772 kB
  • sloc: ansic: 9,696; sh: 4,816; makefile: 157
file content (695 lines) | stat: -rw-r--r-- 20,637 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
/*
 * Copyright 2017 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "jose.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <ctype.h>

#define SUMMARY "Converts JSON between serialization formats"
#define JAIN json_array_insert_new

#ifdef __MINGW32__
#define sscanf __mingw_sscanf
#endif

static const char *prefix = "jose fmt [OPTIONS]\n\n" SUMMARY;

typedef struct {
    json_t *args;
} jcmd_opt_t;

static size_t
convert_int(const json_t *arr, const char *arg)
{
    ssize_t indx = 0;

    if (sscanf(arg, "%zd", &indx) != 1)
        return SIZE_MAX;

    if (indx < 0)
        indx += json_array_size(arr);

    if (indx < 0)
        return SIZE_MAX;

    return indx;
}

static void
jcmd_opt_cleanup(jcmd_opt_t *opt)
{
    json_decref(opt->args);
}

static bool
cmd_output(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    const int wflags = JSON_ENCODE_ANY | JSON_COMPACT | JSON_SORT_KEYS;
    const char *s = json_string_value(arg);
    FILE *file = NULL;
    bool ret = false;

    if (strcmp(s, "-") == 0)
        file = stdout;
    else
        file = fopen(s, "w");
    if (!file)
        return false;

    if (json_dumpf(cur, file, wflags) < 0)
        goto egress;

    if (isatty(fileno(file)) && fwrite("\n", 1, 1, file) != 1)
        goto egress;

    ret = true;

egress:
    if (strcmp(s, "-") != 0)
        fclose(file);
    return ret;
}

static bool
cmd_foreach(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    const int wflags = JSON_ENCODE_ANY | JSON_COMPACT | JSON_SORT_KEYS;
    const char *s = json_string_value(arg);
    FILE *file = NULL;
    bool ret = false;

    if (!json_is_array(cur) && !json_is_object(cur))
        return false;

    if (strcmp(s, "-") == 0)
        file = stdout;
    else
        file = fopen(s, "w");
    if (!file)
        return false;

    if (json_is_array(cur)) {
        json_t *v = NULL;
        size_t i = 0;

        json_array_foreach(cur, i, v) {
            if (json_dumpf(v, file, wflags) < 0 ||
                fprintf(file, "\n") < 0)
                goto egress;
        }
    } else if (json_is_object(cur)) {
        const char *k = NULL;
        json_t *v = NULL;

        json_object_foreach(cur, k, v) {
            if (fprintf(file, "%s=", k) < 0 ||
                json_dumpf(v, file, wflags) < 0 ||
                fprintf(file, "\n") < 0)
                goto egress;
        }
    }

    ret = true;

egress:
    if (strcmp(s, "-") != 0)
        fclose(file);
    return ret;
}

static bool
cmd_unquote(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    const char *s = json_string_value(arg);
    FILE *file = NULL;
    bool ret = false;

    if (!json_is_string(cur))
        return false;

    if (strcmp(s, "-") == 0)
        return fprintf(stdout, "%s\n", json_string_value(cur)) >= 0;

    file = fopen(s, "w");
    if (!file)
        return false;

    ret = fprintf(file, "%s\n", json_string_value(cur)) >= 0;
    fclose(file);
    return ret;
}

static bool
cmd_move(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    json_int_t i = json_integer_value(arg);

    if (json_array_insert(stk, i + 1, cur) < 0)
        return false;

    if (json_array_remove(stk, 0) < 0)
        return false;

    return true;
}

static bool
cmd_trunc(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    size_t i = json_integer_value(arg);
    size_t s;

    for (s = json_array_size(cur); s > i; s--) {
        if (json_array_remove(cur, s - 1) < 0)
            return false;
    }

    return true;
}

static bool
cmd_insert(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    size_t i = json_integer_value(arg);
    return json_array_insert(lst, i, cur) >= 0;
}

static bool
cmd_append(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    if (json_is_array(lst))
        return json_array_append(lst, cur) >= 0;

    if (json_is_object(lst))
        return json_object_update_missing(lst, cur) >= 0;

    return false;
}

static bool
cmd_extend(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    if (json_is_array(lst))
        return json_array_extend(lst, cur) >= 0;

    if (json_is_object(lst))
        return json_object_update(lst, cur) >= 0;

    return false;
}

static bool
cmd_delete(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    const char *s = json_string_value(arg);

    if (json_is_array(cur)) {
        size_t indx;

        indx = convert_int(cur, s);
        if (indx == SIZE_MAX)
            return false;

        return json_array_remove(cur, indx) >= 0;
    }

    if (json_is_object(cur))
        return json_object_del(cur, s) >= 0;

    return false;
}

static bool
cmd_length(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    size_t count = 0;

    if (json_is_array(cur))
        count = json_array_size(cur);
    else if (json_is_object(cur))
        count = json_object_size(cur);
    else if (json_is_string(cur))
        count = json_string_length(cur);
    else
        return false;

    return json_array_insert_new(stk, 0, json_integer(count)) >= 0;
}

static bool
cmd_empty(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    if (json_is_array(cur))
        return json_array_clear(cur) >= 0;

    if (json_is_object(cur))
        return json_object_clear(cur) >= 0;

    return false;
}

static bool
cmd_get(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    const char *s = json_string_value(arg);
    json_t *v = NULL;

    if (json_is_array(cur)) {
        size_t indx;

        indx = convert_int(cur, s);
        if (indx == SIZE_MAX)
            return false;

        v = json_array_get(cur, indx);
    } else if (json_is_object(cur)) {
        v = json_object_get(cur, s);
    } else {
        return false;
    }

    return json_array_insert(stk, 0, v) >= 0;
}

static bool
cmd_set(const json_t *arg, json_t *stk, json_t *cur, json_t *lst)
{
    const char *s = json_string_value(arg);

    if (json_is_array(lst)) {
        size_t indx;

        indx = convert_int(lst, s);
        if (indx == SIZE_MAX)
            return false;

        return json_array_set(lst, indx, cur) >= 0;
    }

    if (json_is_object(lst))
        return json_object_set(lst, s, cur) >= 0;

    return false;
}

static const jcmd_doc_t doc_not[] = {
    { .doc = "Invert the following assertion" },
    {}
};

static const jcmd_doc_t doc_object[] = {
    { .doc = "Assert TOP to be an object" },
    {}
};

static const jcmd_doc_t doc_array[] = {
    { .doc = "Assert TOP to be an array" },
    {}
};

static const jcmd_doc_t doc_string[] = {
    { .doc = "Assert TOP to be a string" },
    {}
};

static const jcmd_doc_t doc_int[] = {
    { .doc = "Assert TOP to be an integer" },
    {}
};

static const jcmd_doc_t doc_real[] = {
    { .doc = "Assert TOP to be a real" },
    {}
};

static const jcmd_doc_t doc_number[] = {
    { .doc = "Assert TOP to be a number" },
    {}
};

static const jcmd_doc_t doc_true[] = {
    { .doc = "Assert TOP to be true" },
    {}
};

static const jcmd_doc_t doc_false[] = {
    { .doc = "Assert TOP to be false" },
    {}
};

static const jcmd_doc_t doc_bool[] = {
    { .doc = "Assert TOP to be a boolean" },
    {}
};

static const jcmd_doc_t doc_null[] = {
    { .doc = "Assert TOP to be null" },
    {}
};

static const jcmd_doc_t doc_equal[] = {
    { .doc = "Assert TOP to be equal to PREV" },
    {}
};

static const jcmd_doc_t doc_json[] = {
    { .arg = "JSON", .doc = "Parse JSON constant, push onto TOP" },
    { .arg = "FILE", .doc = "Read from FILE, push onto TOP" },
    { .arg = "-",    .doc = "Read from STDIN, push onto TOP" },
    {}
};

static const jcmd_doc_t doc_quote[] = {
    { .arg = "STR",  .doc = "Convert STR to a string, push onto TOP" },
    {}
};

static const jcmd_doc_t doc_output[] = {
    { .arg = "FILE", .doc = "Write TOP to FILE" },
    { .arg = "-",    .doc = "Write TOP to STDOUT" },
    {}
};

static const jcmd_doc_t doc_foreach[] = {
    { .arg = "FILE", .doc = "Write TOP (obj./arr.) to FILE, one line/item" },
    { .arg = "-",    .doc = "Write TOP (obj./arr.) to STDOUT, one line/item" },
    {}
};

static const jcmd_doc_t doc_unquote[] = {
    { .arg = "FILE", .doc = "Write TOP (str.) to FILE without quotes" },
    { .arg = "-",    .doc = "Write TOP (str.) to STDOUT without quotes" },
    {}
};

static const jcmd_doc_t doc_copy[] = {
    { .doc = "Deep copy TOP, push onto TOP" },
    {}
};

static const jcmd_doc_t doc_query[] = {
    { .doc = "Query the stack by deep copying and pushing onto TOP" },
    {}
};

static const jcmd_doc_t doc_move[] = {
    { .arg = "#", .doc = "Move TOP back # places on the stack" },
    {}
};

static const jcmd_doc_t doc_unwind[] = {
    { .doc = "Discard TOP from the stack" },
    {}
};

static const jcmd_doc_t doc_trunc[] = {
    { .arg = "#",  .doc = "Shrink TOP (arr.) to length #" },
    { .arg = "-#", .doc = "Discard last # items from TOP (arr.)" },
    {}
};

static const jcmd_doc_t doc_insert[] = {
    { .arg = "#", .doc = "Insert TOP into PREV (arr.) at #" },
    {}
};

static const jcmd_doc_t doc_append[] = {
    { .doc = "Append TOP to the end of PREV (arr.)" },
    { .doc = "Set missing values from TOP (obj.) into PREV (obj.)" },
    {}
};

static const jcmd_doc_t doc_extend[] = {
    { .doc = "Append items from TOP to the end of PREV (arr.)" },
    { .doc = "Set all values from TOP (obj.) into PREV (obj.)" },
    {}
};

static const jcmd_doc_t doc_delete[] = {
    { .arg = "NAME", .doc = "Delete NAME from TOP (obj.)" },
    { .arg = "#",    .doc = "Delete # from TOP (arr.)" },
    { .arg = "-#",   .doc = "Delete # from the end of TOP (arr.)" },
    {}
};

static const jcmd_doc_t doc_length[] = {
    { .doc = "Push length of TOP (arr./str./obj.) to TOP" },
    {}
};

static const jcmd_doc_t doc_empty[] = {
    { .doc = "Erase all items from TOP (arr./obj.)" },
    {}
};

static const jcmd_doc_t doc_get[] = {
    { .arg = "NAME", .doc = "Get item with NAME from TOP (obj.), push to TOP" },
    { .arg = "#",    .doc = "Get # item from TOP (arr.), push to TOP" },
    { .arg = "-#",   .doc = "Get # item from the end of TOP (arr.), push to TOP" },
    {}
};

static const jcmd_doc_t doc_set[] = {
    { .arg = "NAME", .doc = "Sets TOP into PREV (obj.) with NAME" },
    { .arg = "#",    .doc = "Sets TOP into PREV (obj.) at #" },
    { .arg = "-#",   .doc = "Sets TOP into PREV (obj.) at # from the end" },
    {}
};

static const jcmd_doc_t doc_b64l[] = {
    { .doc = "URL-safe Base64 decode TOP (str.), push onto TOP" },
    {}
};

static const jcmd_doc_t doc_b64d[] = {
    { .doc = "URL-safe Base64 encode TOP, push onto TOP" },
    {}
};

static bool
opt_set_null(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
{
    json_t **x = vopt;
    if (!*x) *x = json_array();
    return json_array_append_new(*x, json_pack("[i,n]", cfg->opt.val)) >= 0;
}


static bool
opt_set_str(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
{
    json_t **x = vopt;
    if (!*x) *x = json_array();
    return json_array_append_new(*x, json_pack("[i,s]", cfg->opt.val, arg)) >= 0;
}

static bool
opt_set_int(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
{
    json_t **x = vopt;
    json_int_t j = 0;
    int i = 0;

    if (sscanf(arg, "%d", &i) != 1)
        return false;

    j = i;
    if (!*x) *x = json_array();
    return json_array_append_new(*x, json_pack("[i,I]", cfg->opt.val, j)) >= 0;
}

static bool
opt_set_uint(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
{
    unsigned int i = 0;
    json_t **x = vopt;
    json_int_t j = 0;

    if (sscanf(arg, "%u", &i) != 1)
        return false;

    j = i;
    if (!*x) *x = json_array();
    return json_array_append_new(*x, json_pack("[i,I]", cfg->opt.val, j)) >= 0;
}

static bool
opt_set_json(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
{
    json_auto_t *j = NULL;
    json_t **x = vopt;

    if (!jcmd_opt_set_json(cfg, &j, arg))
        return false;

    if (!*x) *x = json_array();
    return json_array_append_new(*x, json_pack("[i,O]", cfg->opt.val, j)) >= 0;
}

static const jcmd_cfg_t cfgs[] = {
    { .opt = { "not",      no_argument,       .val = 'X' }, .doc = doc_not,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "object",   no_argument,       .val = 'O' }, .doc = doc_object,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "array",    no_argument,       .val = 'A' }, .doc = doc_array,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "string",   no_argument,       .val = 'S' }, .doc = doc_string,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "integer",  no_argument,       .val = 'I' }, .doc = doc_int,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "real",     no_argument,       .val = 'R' }, .doc = doc_real,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "number",   no_argument,       .val = 'N' }, .doc = doc_number,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "true",     no_argument,       .val = 'T' }, .doc = doc_true,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "false",    no_argument,       .val = 'F' }, .doc = doc_false,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "boolean",  no_argument,       .val = 'B' }, .doc = doc_bool,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "null",     no_argument,       .val = '0' }, .doc = doc_null,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "equal",    no_argument,       .val = 'E' }, .doc = doc_equal,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },

    { .opt = { "query",    no_argument,       .val = 'Q' }, .doc = doc_query,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "move",     required_argument, .val = 'M' }, .doc = doc_move,
      .set = opt_set_uint, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "unwind",   no_argument,       .val = 'U' }, .doc = doc_unwind,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },

    { .opt = { "json",     required_argument, .val = 'j' }, .doc = doc_json,
      .set = opt_set_json, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "copy",     no_argument,       .val = 'c' }, .doc = doc_copy,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "quote",    required_argument, .val = 'q' }, .doc = doc_quote,
      .set = opt_set_str,  .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "output",   required_argument, .val = 'o' }, .doc = doc_output,
      .set = opt_set_str,  .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "foreach",  required_argument, .val = 'f' }, .doc = doc_foreach,
      .set = opt_set_str,  .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "unquote",  required_argument, .val = 'u' }, .doc = doc_unquote,
      .set = opt_set_str,  .off = offsetof(jcmd_opt_t, args) },

    { .opt = { "truncate", required_argument, .val = 't' }, .doc = doc_trunc,
      .set = opt_set_int,  .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "insert",   required_argument, .val = 'i' }, .doc = doc_insert,
      .set = opt_set_uint, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "append",   no_argument,       .val = 'a' }, .doc = doc_append,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "extend",   no_argument,       .val = 'x' }, .doc = doc_extend,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },

    { .opt = { "delete",   required_argument, .val = 'd' }, .doc = doc_delete,
      .set = opt_set_str,  .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "length",   no_argument,       .val = 'l' }, .doc = doc_length,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "empty",    no_argument,       .val = 'e' }, .doc = doc_empty,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "get",      required_argument, .val = 'g' }, .doc = doc_get,
      .set = opt_set_str,  .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "set",      required_argument, .val = 's' }, .doc = doc_set,
      .set = opt_set_str,  .off = offsetof(jcmd_opt_t, args) },

    { .opt = { "b64load",  no_argument,       .val = 'y' }, .doc = doc_b64l,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },
    { .opt = { "b64dump",  no_argument,       .val = 'Y' }, .doc = doc_b64d,
      .set = opt_set_null, .off = offsetof(jcmd_opt_t, args) },

    {}
};

static int
jcmd_fmt(int argc, char *argv[])
{
    json_auto_t *stk = json_array();
    jcmd_opt_auto_t opt = {};
    unsigned char ret = 0;
    bool not = false;

    if (!jcmd_opt_parse(argc, argv, cfgs, &opt, prefix))
        return -1;

    for (size_t i = 0; i < json_array_size(opt.args); i++) {
        json_t *lst = NULL;
        json_t *cur = NULL;
        json_t *p = NULL;
        bool ok = false;
        int o = 0;

        if (json_unpack(json_array_get(opt.args, i), "[i,o!]", &o, &p) < 0)
            return ++ret;

        if (not && !strchr("OASIRNTFB0E", o))
            return ret;

        cur = json_array_get(stk, 0);
        lst = json_array_get(stk, 1);
        ret++;

        switch (o) {
        case 'X': ok = not = true;                                break;
        case 'O': ok = not ^ json_is_object(cur);  not = false;   break;
        case 'A': ok = not ^ json_is_array(cur);   not = false;   break;
        case 'S': ok = not ^ json_is_string(cur);  not = false;   break;
        case 'I': ok = not ^ json_is_integer(cur); not = false;   break;
        case 'R': ok = not ^ json_is_real(cur);    not = false;   break;
        case 'N': ok = not ^ json_is_number(cur);  not = false;   break;
        case 'T': ok = not ^ json_is_true(cur);    not = false;   break;
        case 'F': ok = not ^ json_is_false(cur);   not = false;   break;
        case 'B': ok = not ^ json_is_boolean(cur); not = false;   break;
        case '0': ok = not ^ json_is_null(cur);    not = false;   break;
        case 'E': ok = not ^ json_equal(cur, lst); not = false;   break;
        case 'Q': ok = JAIN(stk, 0, json_deep_copy(stk)) >= 0;    break;
        case 'M': ok = cmd_move(p, stk, cur, lst);                break;
        case 'U': ok = json_array_remove(stk, 0) >= 0;            break;
        case 'j': ok = json_array_insert(stk, 0, p) >= 0;         break;
        case 'c': ok = JAIN(stk, 0, json_deep_copy(cur)) >= 0;    break;
        case 'q': ok = json_array_insert(stk, 0, p) >= 0;         break;
        case 'o': ok = cmd_output(p, stk, cur, lst);              break;
        case 'f': ok = cmd_foreach(p, stk, cur, lst);             break;
        case 'u': ok = cmd_unquote(p, stk, cur, lst);             break;
        case 't': ok = cmd_trunc(p, stk, cur, lst);               break;
        case 'i': ok = cmd_insert(p, stk, cur, lst);              break;
        case 'a': ok = cmd_append(p, stk, cur, lst);              break;
        case 'x': ok = cmd_extend(p, stk, cur, lst);              break;
        case 'd': ok = cmd_delete(p, stk, cur, lst);              break;
        case 'l': ok = cmd_length(p, stk, cur, lst);              break;
        case 'e': ok = cmd_empty(p, stk, cur, lst);               break;
        case 'g': ok = cmd_get(p, stk, cur, lst);                 break;
        case 's': ok = cmd_set(p, stk, cur, lst);                 break;
        case 'Y': ok = JAIN(stk, 0, jose_b64_enc_dump(cur)) >= 0; break;
        case 'y': ok = JAIN(stk, 0, jose_b64_dec_load(cur)) >= 0; break;
        default:  ok = false;                                     break;
        }

        if (!ok)
            return ret;
    }

    if (not)
        return ret;

    return EXIT_SUCCESS;
}

JCMD_REGISTER(SUMMARY, jcmd_fmt, "fmt")