-
Notifications
You must be signed in to change notification settings - Fork 3
/
jparse_main.c
231 lines (209 loc) · 6.09 KB
/
jparse_main.c
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
/*
* jparse_main - main function for the JSON parser stand-alone tool
*
* "Because specs w/o version numbers are forced to commit to their original design flaws." :-)
*
* This JSON parser was co-developed in 2022 by:
*
* @xexyl
* https://xexyl.net Cody Boone Ferguson
* https://ioccc.xexyl.net
* and:
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* "Because sometimes even the IOCCC Judges need some help." :-)
*
* "Share and Enjoy!"
* -- Sirius Cybernetics Corporation Complaints Division, JSON spec department. :-)
*/
/* special comments for the seqcexit tool */
/* exit code out of numerical order - ignore in sequencing - ooo */
/* exit code change of order - use new value in sequencing - coo */
#include <stdio.h>
#include <unistd.h>
/*
* jparse_main - tool that parses a block of JSON input
*/
#include "jparse_main.h"
/*
* definitions
*/
#define REQUIRED_ARGS (1) /* number of required arguments on the command line */
/*
* usage message
*/
static const char * const usage_msg =
"usage: %s [-h] [-v level] [-J level] [-q] [-V] [-s] arg\n"
"\n"
"\t-h\t\tprint help message and exit\n"
"\t-v level\tset verbosity level (def level: %d)\n"
"\t-J level\tset JSON verbosity level (def level: %d)\n"
"\t-q\t\tquiet mode: silence msg(), warn(), warnp() if -v 0 (def: loud :-) )\n"
"\t-V\t\tprint version string and exit\n"
"\t-s\t\targ is a string (def: arg is a filename)\n"
"\n"
"\targ\t\tparse JSON for string (if -s), file (w/o -s), or stdin (if arg is -)\n"
"\n"
"Exit codes:\n"
" 0\tJSON is valid\n"
" 1\tJSON is invalid\n"
" 2\t-h and help string printed or -V and version string printed\n"
" 3\tcommand line error\n"
" >=4\tinternal error\n"
"\n"
"%s version: %s\n"
"jparse UTF-8 version: %s\n"
"jparse library version: %s";
/*
* static functions
*/
static void usage(int exitcode, char const *prog, char const *str) __attribute__((noreturn));
int
main(int argc, char **argv)
{
char const *program = NULL; /* our name */
extern char *optarg; /* option argument */
extern int optind; /* argv index of the next arg */
bool string_flag_used = false; /* true ==> -S string was used */
bool valid_json = false; /* true ==> JSON parse was valid */
struct json *tree = NULL; /* JSON parse tree or NULL */
int arg_count = 0; /* number of args to process */
int i;
/*
* parse args
*/
program = argv[0];
while ((i = getopt(argc, argv, ":hv:qVsJ:")) != -1) {
switch (i) {
case 'h': /* -h - print help to stderr and exit 0 */
usage(2, program, ""); /*ooo*/
not_reached();
break;
case 'v': /* -v verbosity */
/*
* parse verbosity
*/
verbosity_level = parse_verbosity(optarg);
if (verbosity_level < 0) {
usage(3, program, "invalid -v verbosity"); /*ooo*/
not_reached();
}
break;
case 'J': /* -J json_verbosity_level */
/*
* parse json verbosity level
*/
json_verbosity_level = parse_verbosity(optarg);
if (verbosity_level < 0) {
usage(3, program, "invalid -J json_verbosity"); /*ooo*/
not_reached();
}
break;
case 'q':
msg_warn_silent = true;
break;
case 'V': /* -V - print version and exit */
print("%s version: %s\n", JPARSE_BASENAME, JPARSE_VERSION);
print("jparse UTF-8 version: %s\n", JPARSE_UTF8_VERSION);
print("jparse library version: %s\n", JPARSE_LIBRARY_VERSION);
exit(2); /*ooo*/
not_reached();
break;
case 's':
string_flag_used = true;
break;
case ':': /* option requires an argument */
case '?': /* illegal option */
default: /* anything else but should not actually happen */
check_invalid_option(program, i, optopt);
usage(3, program, ""); /*ooo*/
not_reached();
break;
}
}
arg_count = argc - optind;
if (arg_count != REQUIRED_ARGS) {
usage(3, program, "wrong number of arguments"); /*ooo*/
not_reached();
}
/*
* case: process -s arg
*/
if (string_flag_used == true) {
/* parse arg as a block of json input */
dbg(DBG_HIGH, "Calling parse_json_str(\"%s\", %ju, &valid_json):",
argv[argc-1], (uintmax_t)strlen(argv[argc-1]));
tree = parse_json_str(argv[argc-1], strlen(argv[argc-1]), &valid_json);
/*
* case: process file arg
*/
} else {
/* parse arg as a json filename */
dbg(DBG_HIGH, "Calling parse_json_file(\"%s\", &valid_json):", argv[argc-1]);
tree = parse_json_file(argv[argc-1], &valid_json);
}
if (tree == NULL) {
warn(program, "JSON parse tree is NULL");
}
/*
* free the JSON parse tree
*/
else {
json_tree_free(tree, JSON_INFINITE_DEPTH);
free(tree);
tree = NULL;
}
/*
* firewall - JSON parser must have returned a valid JSON parse tree
*/
if (!valid_json) {
err(1, program, "invalid JSON"); /*ooo*/
not_reached();
}
if (verbosity_level > 0) {
msg("valid JSON");
}
exit(0); /*ooo*/
}
/*
* usage - print usage to stderr
*
* Example:
* usage(3, program, "wrong number of arguments");
*
* given:
* exitcode value to exit with
* program our program name
* str top level usage message
*
* NOTE: We warn with extra newlines to help internal fault messages stand out.
* Normally one should NOT include newlines in warn messages.
*
* This function does not return.
*
*/
static void
usage(int exitcode, char const *prog, char const *str)
{
/*
* firewall
*/
if (str == NULL) {
str = "((NULL str))";
warn(__func__, "\nin usage(): program was NULL, forcing it to be: %s\n", str);
}
if (prog == NULL) {
prog = JPARSE_BASENAME;
warn(__func__, "\nin usage(): program was NULL, forcing it to be: %s\n", prog);
}
/*
* print the formatted usage stream
*/
if (*str != '\0') {
fprintf_usage(DO_NOT_EXIT, stderr, "%s\n", str);
}
fprintf_usage(exitcode, stderr, usage_msg, prog,
DBG_DEFAULT, JSON_DBG_DEFAULT, JPARSE_BASENAME, JPARSE_VERSION, JPARSE_UTF8_VERSION, JPARSE_LIBRARY_VERSION);
exit(exitcode); /*ooo*/
not_reached();
}