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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2014 Joyent, Inc.
*/
/*
* This program implements a small domain-specific language (DSL) for the
* generation of nvlists, and subsequent printing in JSON-formatted output.
* The test suite uses this tool to drive the JSON formatting routines in
* libnvpair(3LIB) for testing.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <locale.h>
#include <libnvpair.h>
#define MAX_ARGS 100
#define CMD_NAME_LEN 50
/*
* As we are parsing a language that allows the creation of arbitrarily nested
* state, i.e. both nested nvlists and arrays of nested nvlists, we store that
* state in a stack. The top frame in the stack represents the nested nvlist
* (or nvlists, for an array) that we are currently building.
*
* When creating an array, the "next" directive advances lw_pos and allocates a
* new nvlist. The "end" directive commits either the nvlist, or array of
* nvlists, into the parent nvlist. It then pops and frees the stack frame
* before returning control to the parser.
*/
typedef struct list_wrap {
nvlist_t *lw_nvl[MAX_ARGS];
char *lw_name;
int lw_pos;
boolean_t lw_array;
struct list_wrap *lw_next;
} list_wrap_t;
int
list_wrap_depth(list_wrap_t *lw)
{
int d = 0;
while (lw != NULL) {
d++;
lw = lw->lw_next;
}
return (d);
}
list_wrap_t *
list_wrap_alloc(list_wrap_t *next)
{
list_wrap_t *out = calloc(1, sizeof (list_wrap_t));
if (out == NULL)
abort();
out->lw_next = next;
return (out);
}
list_wrap_t *
list_wrap_pop_and_free(list_wrap_t *lw)
{
list_wrap_t *next = lw->lw_next;
free(lw->lw_name);
free(lw);
return (next);
}
/*
* Generic integer and floating point parsing routines:
*/
int
parse_int(char *in, int64_t *val, int64_t min, int64_t max)
{
int64_t t;
char *end = NULL;
errno = 0;
t = strtoll(in, &end, 10);
if (errno != 0 || end == in || *end != '\0') {
if (errno == ERANGE) {
(void) fprintf(stderr, "ERROR: integer %s not in "
"range [%lld,%lld]\n", in, min, max);
return (-1);
}
(void) fprintf(stderr, "ERROR: could not parse \"%s\" as "
"signed integer (%s)\n", in, strerror(errno));
return (-1);
}
if (t < min || t > max) {
(void) fprintf(stderr, "ERROR: integer %lld not in range "
"[%lld,%lld]\n", t, min, max);
return (-1);
}
*val = t;
return (0);
}
int
parse_uint(char *in, uint64_t *val, uint64_t min, uint64_t max)
{
uint64_t t;
char *end = NULL;
errno = 0;
t = strtoull(in, &end, 10);
if (errno != 0 || end == in || *end != '\0') {
if (errno == ERANGE) {
(void) fprintf(stderr, "ERROR: integer %s not in "
"range [%llu,%llu]\n", in, min, max);
return (-1);
}
(void) fprintf(stderr, "ERROR: could not parse \"%s\" as "
"unsigned integer (%s)\n", in, strerror(errno));
return (-1);
}
if (t < min || t > max) {
(void) fprintf(stderr, "ERROR: integer %llu not in range "
"[%llu,%llu]\n", t, min, max);
return (-1);
}
*val = t;
return (0);
}
int
parse_double(char *in, double *val)
{
double t;
char *end = NULL;
errno = 0;
t = strtod(in, &end);
if (errno != 0 || end == in || *end != '\0') {
(void) fprintf(stderr, "ERROR: could not parse \"%s\" as "
"double\n", in);
return (-1);
}
*val = t;
return (0);
}
/*
* Command-specific handlers for directives specified in the DSL input:
*/
typedef int (*command_handler_t)(list_wrap_t **, boolean_t, int,
char **);
static int
ch_add_string(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
if (array) {
if (nvlist_add_string_array(nvl, argv[0], &argv[1],
argc - 1) != 0) {
(void) fprintf(stderr, "fail at "
"nvlist_add_string_array\n");
return (-1);
}
} else {
if (nvlist_add_string(nvl, argv[0], argv[1]) != 0) {
(void) fprintf(stderr, "fail at nvlist_add_string\n");
return (-1);
}
}
return (0);
}
static int
ch_add_boolean(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
if (array)
abort();
if (nvlist_add_boolean(nvl, argv[0]) != 0) {
(void) fprintf(stderr, "fail at nvlist_add_boolean\n");
return (-1);
}
return (0);
}
static int
ch_add_boolean_value(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
int i;
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
boolean_t arrval[MAX_ARGS];
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "true") == 0) {
arrval[i - 1] = B_TRUE;
} else if (strcmp(argv[i], "false") == 0) {
arrval[i - 1] = B_FALSE;
} else {
(void) fprintf(stderr, "invalid boolean value: %s\n",
argv[i]);
return (-1);
}
}
if (array) {
if (nvlist_add_boolean_array(nvl, argv[0], arrval,
argc - 1) != 0) {
(void) fprintf(stderr, "fail at "
"nvlist_add_boolean_array\n");
return (-1);
}
} else {
if (nvlist_add_boolean_value(nvl, argv[0], arrval[0]) != 0) {
(void) fprintf(stderr, "fail at "
"nvlist_add_boolean_value\n");
return (-1);
}
}
return (0);
}
/*
* The confluence of a strongly typed C API for libnvpair(3LIB) and the
* combinatorial explosion of both sizes and signedness is unfortunate. Rather
* than reproduce the same code over and over, this macro parses an integer,
* checks applicable bounds based on size and signedness, and stores the value
* (or array of values).
*/
#define DO_CMD_NUMBER(typ, nam, min, max, ptyp, func) \
ptyp val; \
typ ## _t arrval[MAX_ARGS]; \
int i; \
for (i = 1; i < argc; i++) { \
if (func(argv[i], &val, min, max) != 0) { \
return (-1); \
} \
arrval[i - 1] = (typ ## _t) val; \
} \
if (array) { \
if (nvlist_add_ ## nam ## _array(nvl, argv[0], \
arrval, argc - 1) != 0) { \
(void) fprintf(stderr, "fail at " \
"nvlist_add_" #nam "_array\n"); \
return (-1); \
} \
} else { \
if (nvlist_add_ ## nam(nvl, argv[0], \
arrval[0]) == -1) { \
(void) fprintf(stderr, "fail at " \
"nvlist_add_" #nam "\n"); \
return (-1); \
} \
} \
return (0);
static int
ch_add_byte(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
DO_CMD_NUMBER(uchar, byte, 0, UCHAR_MAX, uint64_t, parse_uint)
}
static int
ch_add_int8(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
DO_CMD_NUMBER(int8, int8, INT8_MIN, INT8_MAX, int64_t, parse_int)
}
static int
ch_add_uint8(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
DO_CMD_NUMBER(uint8, uint8, 0, UINT8_MAX, uint64_t, parse_uint)
}
static int
ch_add_int16(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
DO_CMD_NUMBER(int16, int16, INT16_MIN, INT16_MAX, int64_t, parse_int)
}
static int
ch_add_uint16(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
DO_CMD_NUMBER(uint16, uint16, 0, UINT16_MAX, uint64_t, parse_uint)
}
static int
ch_add_int32(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
DO_CMD_NUMBER(int32, int32, INT32_MIN, INT32_MAX, int64_t, parse_int)
}
static int
ch_add_uint32(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
DO_CMD_NUMBER(uint32, uint32, 0, UINT32_MAX, uint64_t, parse_uint)
}
static int
ch_add_int64(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
DO_CMD_NUMBER(int64, int64, INT64_MIN, INT64_MAX, int64_t, parse_int)
}
static int
ch_add_uint64(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
DO_CMD_NUMBER(uint64, uint64, 0, UINT64_MAX, uint64_t, parse_uint)
}
static int
ch_add_double(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];
double val;
if (array)
abort();
if (parse_double(argv[1], &val) != 0) {
return (-1);
}
if (nvlist_add_double(nvl, argv[0], val) != 0) {
(void) fprintf(stderr, "fail at nvlist_add_double_value\n");
return (-1);
}
return (0);
}
static int
ch_end(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
nvlist_t *parent;
char *name;
if (list_wrap_depth(*lw) < 2) {
(void) fprintf(stderr, "ERROR: not nested, cannot end.\n");
return (-1);
}
parent = (*lw)->lw_next->lw_nvl[(*lw)->lw_next->lw_pos];
name = (*lw)->lw_name;
if ((*lw)->lw_array) {
/*
* This was an array of objects.
*/
nvlist_t **children = (*lw)->lw_nvl;
int nelems = (*lw)->lw_pos + 1;
if (nvlist_add_nvlist_array(parent, name, children,
nelems) != 0) {
(void) fprintf(stderr, "fail at "
"nvlist_add_nvlist_array\n");
return (-1);
}
} else {
/*
* This was a single object.
*/
nvlist_t *child = (*lw)->lw_nvl[0];
if ((*lw)->lw_pos != 0)
abort();
if (nvlist_add_nvlist(parent, name, child) != 0) {
(void) fprintf(stderr, "fail at nvlist_add_nvlist\n");
return (-1);
}
}
*lw = list_wrap_pop_and_free(*lw);
return (0);
}
static int
ch_next(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
if (!(*lw)->lw_array) {
(void) fprintf(stderr, "ERROR: cannot use 'next' outside an "
"object array.\n");
return (-1);
}
if ((*lw)->lw_pos++ >= MAX_ARGS) {
(void) fprintf(stderr, "ERROR: object array too long\n");
return (-1);
}
if (nvlist_alloc(&(*lw)->lw_nvl[(*lw)->lw_pos], NV_UNIQUE_NAME,
0) != 0) {
(void) fprintf(stderr, "ERROR: failed at nvlist_alloc\n");
return (-1);
}
return (0);
}
static int
ch_add_object(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
*lw = list_wrap_alloc(*lw);
(*lw)->lw_name = strdup(argv[0]);
(*lw)->lw_array = array;
if (nvlist_alloc(&(*lw)->lw_nvl[0], NV_UNIQUE_NAME, 0) != 0) {
(void) fprintf(stderr, "fail at nvlist_alloc\n");
return (-1);
}
return (0);
}
typedef struct command {
char cmd_name[CMD_NAME_LEN];
command_handler_t cmd_func;
int cmd_min_args;
int cmd_max_args;
boolean_t cmd_array_mode;
} command_t;
/*
* These are the commands we support in the testing DSL, and their
* handling functions:
*/
command_t command_handlers[] = {
{ "add_boolean", ch_add_boolean, 1, 1, B_FALSE },
{ "add_boolean_value", ch_add_boolean_value, 2, 2, B_FALSE },
{ "add_byte", ch_add_byte, 2, 2, B_FALSE },
{ "add_int8", ch_add_int8, 2, 2, B_FALSE },
{ "add_uint8", ch_add_uint8, 2, 2, B_FALSE },
{ "add_int16", ch_add_int16, 2, 2, B_FALSE },
{ "add_uint16", ch_add_uint16, 2, 2, B_FALSE },
{ "add_int32", ch_add_int32, 2, 2, B_FALSE },
{ "add_uint32", ch_add_uint32, 2, 2, B_FALSE },
{ "add_int64", ch_add_int64, 2, 2, B_FALSE },
{ "add_uint64", ch_add_uint64, 2, 2, B_FALSE },
{ "add_double", ch_add_double, 2, 2, B_FALSE },
{ "add_string", ch_add_string, 2, 2, B_FALSE },
{ "add_object", ch_add_object, 1, 1, B_FALSE },
{ "add_boolean_array", ch_add_boolean_value, 1, MAX_ARGS, B_TRUE },
{ "add_byte_array", ch_add_byte, 1, MAX_ARGS, B_TRUE },
{ "add_int8_array", ch_add_int8, 1, MAX_ARGS, B_TRUE },
{ "add_uint8_array", ch_add_uint8, 1, MAX_ARGS, B_TRUE },
{ "add_int16_array", ch_add_int16, 1, MAX_ARGS, B_TRUE },
{ "add_uint16_array", ch_add_uint16, 1, MAX_ARGS, B_TRUE },
{ "add_int32_array", ch_add_int32, 1, MAX_ARGS, B_TRUE },
{ "add_uint32_array", ch_add_uint32, 1, MAX_ARGS, B_TRUE },
{ "add_int64_array", ch_add_int64, 1, MAX_ARGS, B_TRUE },
{ "add_uint64_array", ch_add_uint64, 1, MAX_ARGS, B_TRUE },
{ "add_string_array", ch_add_string, 1, MAX_ARGS, B_TRUE },
{ "add_object_array", ch_add_object, 1, 1, B_TRUE },
{ "end", ch_end, 0, 0, B_FALSE },
{ "next", ch_next, 0, 0, B_FALSE },
{ 0 }
};
/*
* This function determines which command we are executing, checks argument
* counts, and dispatches to the appropriate handler:
*/
static int
command_call(list_wrap_t **lw, char *command, int argc, char **argv)
{
int ch;
for (ch = 0; command_handlers[ch].cmd_name[0] != '\0'; ch++) {
if (strcmp(command, command_handlers[ch].cmd_name) != 0)
continue;
if (argc > command_handlers[ch].cmd_max_args ||
argc < command_handlers[ch].cmd_min_args) {
(void) fprintf(stderr, "ERROR: command \"%s\""
" expects between %d and %d arguments,"
" but %d were provided.\n", command,
command_handlers[ch].cmd_min_args,
command_handlers[ch].cmd_max_args,
argc);
return (-1);
}
return (command_handlers[ch].cmd_func(lw,
command_handlers[ch].cmd_array_mode, argc, argv));
}
(void) fprintf(stderr, "ERROR: invalid command: \"%s\"\n", command);
return (-1);
}
/*
* The primary state machine for parsing the input DSL is implemented in
* this function:
*/
typedef enum state {
STATE_REST = 1,
STATE_COMMAND,
STATE_ARG_FIND,
STATE_ARG,
STATE_ARG_ESCAPE,
STATE_ARG_ESCAPE_HEX,
STATE_C_COMMENT_0,
STATE_C_COMMENT_1,
STATE_C_COMMENT_2
} state_t;
int
parse(FILE *in, list_wrap_t **lw)
{
char b[8192];
int bp;
state_t st = STATE_REST;
int argc = 0;
char *argv[MAX_ARGS];
int line = 1;
char hex[3];
int nhex = 0;
b[0] = '\0';
bp = 0;
for (;;) {
int c = fgetc(in);
/*
* Signal an error if the file ends part way through a
* construct:
*/
if (st != STATE_REST && c == EOF) {
(void) fprintf(stderr, "ERROR: unexpected end of "
"file\n");
return (-1);
} else if (c == EOF) {
return (0);
}
if (c == '\n')
line++;
switch (st) {
case STATE_REST:
if (isalpha(c) || c == '_') {
argc = 0;
bp = 0;
b[bp++] = c;
b[bp] = '\0';
st = STATE_COMMAND;
continue;
} else if (c == ' ' || c == '\t' || c == '\n') {
/*
* Ignore whitespace.
*/
continue;
} else if (c == '/') {
st = STATE_C_COMMENT_0;
continue;
} else {
goto unexpected;
}
case STATE_C_COMMENT_0:
if (c != '*') {
goto unexpected;
}
st = STATE_C_COMMENT_1;
continue;
case STATE_C_COMMENT_1:
if (c == '*') {
st = STATE_C_COMMENT_2;
}
continue;
case STATE_C_COMMENT_2:
if (c == '/') {
st = STATE_REST;
} else if (c != '*') {
st = STATE_C_COMMENT_1;
}
continue;
case STATE_COMMAND:
if (isalnum(c) || c == '_') {
b[bp++] = c;
b[bp] = '\0';
st = STATE_COMMAND;
continue;
} else if (isspace(c)) {
/*
* Start collecting arguments into 'b'
* after the command.
*/
st = STATE_ARG_FIND;
bp++;
continue;
} else if (c == ';') {
/*
* This line was _just_ a command,
* so break out and process now:
*/
goto execute;
} else {
goto unexpected;
}
case STATE_ARG_FIND:
if (isspace(c)) {
/*
* Whitespace, ignore.
*/
continue;
} else if (c == ';') {
/*
* Break out to process command.
*/
goto execute;
} else if (c == '"') {
st = STATE_ARG;
argv[argc] = &b[++bp];
b[bp] = '\0';
continue;
} else {
goto unexpected;
}
case STATE_ARG:
if (c == '"') {
if (argc++ >= MAX_ARGS) {
(void) fprintf(stderr, "ERROR: too "
"many args\n");
return (-1);
}
st = STATE_ARG_FIND;
continue;
} else if (c == '\n') {
(void) fprintf(stderr, "ERROR: line not "
"finished\n");
return (-1);
} else if (c == '\\') {
st = STATE_ARG_ESCAPE;
continue;
} else {
b[bp++] = c;
b[bp] = '\0';
continue;
}
case STATE_ARG_ESCAPE:
if (c == 'a') {
c = '\a';
} else if (c == 'b') {
c = '\b';
} else if (c == 'f') {
c = '\f';
} else if (c == 'n') {
c = '\n';
} else if (c == 'r') {
c = '\r';
} else if (c == 't') {
c = '\t';
} else if (c == 'v') {
c = '\v';
} else if (c == 'x') {
st = STATE_ARG_ESCAPE_HEX;
hex[0] = hex[1] = hex[2] = '\0';
nhex = 0;
continue;
} else if (c != '\\' && c != '"') {
goto unexpected;
}
b[bp++] = c;
b[bp] = '\0';
st = STATE_ARG;
continue;
case STATE_ARG_ESCAPE_HEX:
if (!isxdigit(c)) {
goto unexpected;
}
hex[nhex] = c;
if (nhex++ >= 1) {
/*
* The hex escape pair is complete, parse
* the integer and insert it as a character:
*/
int x;
errno = 0;
if ((x = strtol(hex, NULL, 16)) == 0 ||
errno != 0) {
goto unexpected;
}
b[bp++] = (char)x;
b[bp] = '\0';
st = STATE_ARG;
}
continue;
}
/*
* We do not ever expect to break out of the switch block
* above. If we do, it's a programmer error.
*/
abort();
execute:
if (command_call(lw, b, argc, argv) == -1)
return (-1);
st = STATE_REST;
continue;
unexpected:
(void) fprintf(stderr, "ERROR: (line %d) unexpected "
"character: %c\n", line, c);
return (-1);
}
}
/*
* Entry point:
*/
int
main(int argc, char **argv)
{
int rc = EXIT_FAILURE;
list_wrap_t *lw;
/*
* Be locale-aware. The JSON output functions will process multibyte
* characters in the current locale, and emit a correct JSON encoding
* for unprintable characters.
*/
if (setlocale(LC_ALL, "") == NULL) {
(void) fprintf(stderr, "Could not set locale: %s\n",
strerror(errno));
goto out;
}
lw = list_wrap_alloc(NULL);
if (nvlist_alloc(&lw->lw_nvl[0], NV_UNIQUE_NAME, 0) != 0)
goto out;
/*
* Generate the list from the commands passed to us on stdin:
*/
if (parse(stdin, &lw) != 0)
goto out;
/*
* Print the resultant list, and a terminating newline:
*/
if (nvlist_print_json(stdout, lw->lw_nvl[0]) < 0 ||
fprintf(stdout, "\n") < 0)
goto out;
rc = EXIT_SUCCESS;
out:
(void) list_wrap_pop_and_free(lw);
return (rc);
}
|