blob: 6b18dad05d61417182d32e4f90e3a8f76ec27899 [file] [log] [blame]
Nigel Tao1b073492020-02-16 22:11:36 +11001// Copyright 2020 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// ----------------
16
17/*
18jsonptr is a JSON formatter (pretty-printer).
19
Nigel Taoc5b3a9e2020-02-24 11:54:35 +110020As of 2020-02-24, this program passes all 318 "test_parsing" cases from the
21JSON test suite (https://github.com/nst/JSONTestSuite), an appendix to the
22"Parsing JSON is a Minefield" article (http://seriot.ch/parsing_json.php) that
23was first published on 2016-10-26 and updated on 2018-03-30.
24
Nigel Tao1b073492020-02-16 22:11:36 +110025This example program differs from most other example Wuffs programs in that it
26is written in C++, not C.
27
28$CXX jsonptr.cc && ./a.out < ../../test/data/github-tags.json; rm -f a.out
29
30for a C++ compiler $CXX, such as clang++ or g++.
Nigel Tao569a2942020-02-23 23:13:51 +110031
32After modifying this program, run "build-example.sh example/jsonptr/" and then
33"script/run-json-test-suite.sh" to catch correctness regressions.
Nigel Tao1b073492020-02-16 22:11:36 +110034*/
35
36#include <inttypes.h>
37#include <stdio.h>
Nigel Tao9cc2c252020-02-23 17:05:49 +110038#include <string.h>
Nigel Tao1b073492020-02-16 22:11:36 +110039
40// Wuffs ships as a "single file C library" or "header file library" as per
41// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
42//
43// To use that single file as a "foo.c"-like implementation, instead of a
44// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
45// compiling it.
46#define WUFFS_IMPLEMENTATION
47
48// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
49// release/c/etc.c whitelist which parts of Wuffs to build. That file contains
50// the entire Wuffs standard library, implementing a variety of codecs and file
51// formats. Without this macro definition, an optimizing compiler or linker may
52// very well discard Wuffs code for unused codecs, but listing the Wuffs
53// modules we use makes that process explicit. Preprocessing means that such
54// code simply isn't compiled.
55#define WUFFS_CONFIG__MODULES
56#define WUFFS_CONFIG__MODULE__BASE
57#define WUFFS_CONFIG__MODULE__JSON
58
59// If building this program in an environment that doesn't easily accommodate
60// relative includes, you can use the script/inline-c-relative-includes.go
61// program to generate a stand-alone C++ file.
62#include "../../release/c/wuffs-unsupported-snapshot.c"
63
Nigel Tao2cf76db2020-02-27 22:42:01 +110064#define TRY(error_msg) \
65 do { \
66 const char* z = error_msg; \
67 if (z) { \
68 return z; \
69 } \
70 } while (false)
71
72static const char* eod = "main: end of data";
73
74// ----
75
76#define MAX_INDENT 8
Nigel Tao107f0ef2020-03-01 21:35:02 +110077#define INDENT_SPACES_STRING " "
78#define INDENT_TABS_STRING "\t\t\t\t\t\t\t\t"
79
80bool flag_compact;
81size_t flag_indent;
82bool flag_tabs;
Nigel Tao2cf76db2020-02-27 22:42:01 +110083
Nigel Tao1b073492020-02-16 22:11:36 +110084#ifndef DST_BUFFER_SIZE
85#define DST_BUFFER_SIZE (32 * 1024)
86#endif
87#ifndef SRC_BUFFER_SIZE
88#define SRC_BUFFER_SIZE (32 * 1024)
89#endif
90#ifndef TOKEN_BUFFER_SIZE
91#define TOKEN_BUFFER_SIZE (4 * 1024)
92#endif
93
Nigel Tao2cf76db2020-02-27 22:42:01 +110094uint8_t dst_array[DST_BUFFER_SIZE];
95uint8_t src_array[SRC_BUFFER_SIZE];
96wuffs_base__token tok_array[TOKEN_BUFFER_SIZE];
Nigel Tao1b073492020-02-16 22:11:36 +110097
98wuffs_base__io_buffer dst;
99wuffs_base__io_buffer src;
100wuffs_base__token_buffer tok;
101
Nigel Tao2cf76db2020-02-27 22:42:01 +1100102// curr_token_end_src_index is the src.data.ptr index of the end of the current
103// token. An invariant is that (curr_token_end_src_index <= src.meta.ri).
104size_t curr_token_end_src_index;
105
Nigel Tao2cf76db2020-02-27 22:42:01 +1100106uint64_t depth;
107
108enum class context {
109 none,
110 in_list_after_bracket,
111 in_list_after_value,
112 in_dict_after_brace,
113 in_dict_after_key,
114 in_dict_after_value,
115} ctx;
116
Nigel Tao1b073492020-02-16 22:11:36 +1100117wuffs_json__decoder dec;
Nigel Tao1b073492020-02-16 22:11:36 +1100118
Nigel Tao2cf76db2020-02-27 22:42:01 +1100119const char* //
120initialize_globals(int argc, char** argv) {
Nigel Tao107f0ef2020-03-01 21:35:02 +1100121 flag_compact = false;
122 flag_indent = 4;
123 flag_tabs = false;
Nigel Tao1b073492020-02-16 22:11:36 +1100124
Nigel Tao2cf76db2020-02-27 22:42:01 +1100125 dst = wuffs_base__make_io_buffer(
126 wuffs_base__make_slice_u8(dst_array, DST_BUFFER_SIZE),
127 wuffs_base__empty_io_buffer_meta());
Nigel Tao1b073492020-02-16 22:11:36 +1100128
Nigel Tao2cf76db2020-02-27 22:42:01 +1100129 src = wuffs_base__make_io_buffer(
130 wuffs_base__make_slice_u8(src_array, SRC_BUFFER_SIZE),
131 wuffs_base__empty_io_buffer_meta());
132
133 tok = wuffs_base__make_token_buffer(
134 wuffs_base__make_slice_token(tok_array, TOKEN_BUFFER_SIZE),
135 wuffs_base__empty_token_buffer_meta());
136
137 curr_token_end_src_index = 0;
138
Nigel Tao2cf76db2020-02-27 22:42:01 +1100139 depth = 0;
140
141 ctx = context::none;
142
Nigel Tao107f0ef2020-03-01 21:35:02 +1100143 int i;
144 for (i = 1; i < argc; i++) {
145 if (argv[i][0] != '-') {
146 return "main: bad argument: use \"jsonptr < foo.json\", not \"jsonptr "
147 "foo.json\"";
148 }
149
150 if ((strcmp(argv[i], "-c") == 0) || //
151 (strcmp(argv[i], "--compact") == 0)) {
152 flag_compact = true;
153 continue;
154
155 } else if ((strncmp(argv[i], "-i=", 3) == 0) || //
156 (strncmp(argv[i], "--indent=", 9) == 0)) {
157 // Set p to point just after the '='.
158 char* p = argv[i];
159 for (; *p != '='; p++) {
160 }
161 p++;
162
163 if (('0' <= p[0]) && (p[0] <= '8') && (p[1] == '\x00')) {
164 flag_indent = p[0] - '0';
165 continue;
166 }
167
168 } else if ((strcmp(argv[i], "-t") == 0) || //
169 (strcmp(argv[i], "--tabs") == 0)) {
170 flag_tabs = true;
171 continue;
172 }
173
174 return "main: bad argument";
175 }
176
Nigel Tao2cf76db2020-02-27 22:42:01 +1100177 return dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0)
178 .message();
179}
Nigel Tao1b073492020-02-16 22:11:36 +1100180
181// ----
182
Nigel Tao2914bae2020-02-26 09:40:30 +1100183const char* //
184read_src() {
Nigel Taoa8406922020-02-19 12:22:00 +1100185 if (src.meta.closed) {
Nigel Tao9cc2c252020-02-23 17:05:49 +1100186 return "main: internal error: read requested on a closed source";
Nigel Taoa8406922020-02-19 12:22:00 +1100187 }
Nigel Tao1b073492020-02-16 22:11:36 +1100188 src.compact();
189 if (src.meta.wi >= src.data.len) {
190 return "main: src buffer is full";
191 }
192 size_t n = fread(src.data.ptr + src.meta.wi, sizeof(uint8_t),
193 src.data.len - src.meta.wi, stdin);
194 src.meta.wi += n;
Nigel Tao67306562020-02-19 14:04:49 +1100195 src.meta.closed = feof(stdin);
196 if ((n == 0) && !src.meta.closed) {
Nigel Taoa8406922020-02-19 12:22:00 +1100197 return "main: read error";
Nigel Tao1b073492020-02-16 22:11:36 +1100198 }
199 return nullptr;
200}
201
Nigel Tao2914bae2020-02-26 09:40:30 +1100202const char* //
203flush_dst() {
Nigel Tao1b073492020-02-16 22:11:36 +1100204 size_t n = dst.meta.wi - dst.meta.ri;
205 if (n > 0) {
206 size_t i = fwrite(dst.data.ptr + dst.meta.ri, sizeof(uint8_t), n, stdout);
207 dst.meta.ri += i;
208 if (i != n) {
209 return "main: write error";
210 }
211 dst.compact();
212 }
213 return nullptr;
214}
215
Nigel Tao2914bae2020-02-26 09:40:30 +1100216const char* //
217write_dst(const void* s, size_t n) {
Nigel Tao1b073492020-02-16 22:11:36 +1100218 const uint8_t* p = static_cast<const uint8_t*>(s);
219 while (n > 0) {
220 size_t i = dst.writer_available();
221 if (i == 0) {
222 const char* z = flush_dst();
223 if (z) {
224 return z;
225 }
226 i = dst.writer_available();
227 if (i == 0) {
228 return "main: dst buffer is full";
229 }
230 }
231
232 if (i > n) {
233 i = n;
234 }
235 memcpy(dst.data.ptr + dst.meta.wi, p, i);
236 dst.meta.wi += i;
237 p += i;
238 n -= i;
239 }
240 return nullptr;
241}
242
243// ----
244
Nigel Tao2914bae2020-02-26 09:40:30 +1100245uint8_t //
246hex_digit(uint8_t nibble) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100247 nibble &= 0x0F;
248 if (nibble <= 9) {
249 return '0' + nibble;
250 }
251 return ('A' - 10) + nibble;
252}
253
Nigel Tao2914bae2020-02-26 09:40:30 +1100254const char* //
Nigel Tao3b486982020-02-27 15:05:59 +1100255handle_unicode_code_point(uint32_t ucp) {
256 if (ucp < 0x0020) {
257 switch (ucp) {
258 case '\b':
259 return write_dst("\\b", 2);
260 case '\f':
261 return write_dst("\\f", 2);
262 case '\n':
263 return write_dst("\\n", 2);
264 case '\r':
265 return write_dst("\\r", 2);
266 case '\t':
267 return write_dst("\\t", 2);
268 default: {
269 // Other bytes less than 0x0020 are valid UTF-8 but not valid in a
270 // JSON string. They need to remain escaped.
271 uint8_t esc6[6];
272 esc6[0] = '\\';
273 esc6[1] = 'u';
274 esc6[2] = '0';
275 esc6[3] = '0';
276 esc6[4] = hex_digit(ucp >> 4);
277 esc6[5] = hex_digit(ucp >> 0);
278 return write_dst(&esc6[0], 6);
279 }
280 }
281
282 } else if (ucp <= 0x007F) {
283 switch (ucp) {
284 case '\"':
285 return write_dst("\\\"", 2);
286 case '\\':
287 return write_dst("\\\\", 2);
288 default: {
289 // The UTF-8 encoding takes 1 byte.
290 uint8_t esc0 = (uint8_t)(ucp);
291 return write_dst(&esc0, 1);
292 }
293 }
294
295 } else if (ucp <= 0x07FF) {
296 // The UTF-8 encoding takes 2 bytes.
297 uint8_t esc2[2];
298 esc2[0] = 0xC0 | (uint8_t)((ucp >> 6));
299 esc2[1] = 0x80 | (uint8_t)((ucp >> 0) & 0x3F);
300 return write_dst(&esc2[0], 2);
301
302 } else if (ucp <= 0xFFFF) {
303 if ((0xD800 <= ucp) && (ucp <= 0xDFFF)) {
Nigel Tao2cf76db2020-02-27 22:42:01 +1100304 return "main: internal error: unexpected Unicode surrogate";
Nigel Tao3b486982020-02-27 15:05:59 +1100305 }
306 // The UTF-8 encoding takes 3 bytes.
307 uint8_t esc3[3];
308 esc3[0] = 0xE0 | (uint8_t)((ucp >> 12));
309 esc3[1] = 0x80 | (uint8_t)((ucp >> 6) & 0x3F);
310 esc3[2] = 0x80 | (uint8_t)((ucp >> 0) & 0x3F);
311 return write_dst(&esc3[0], 3);
312
313 } else if (ucp <= 0x10FFFF) {
314 // The UTF-8 encoding takes 4 bytes.
315 uint8_t esc4[4];
316 esc4[0] = 0xF0 | (uint8_t)((ucp >> 18));
317 esc4[1] = 0x80 | (uint8_t)((ucp >> 12) & 0x3F);
318 esc4[2] = 0x80 | (uint8_t)((ucp >> 6) & 0x3F);
319 esc4[3] = 0x80 | (uint8_t)((ucp >> 0) & 0x3F);
320 return write_dst(&esc4[0], 4);
321 }
322
Nigel Tao2cf76db2020-02-27 22:42:01 +1100323 return "main: internal error: unexpected Unicode code point";
Nigel Tao3b486982020-02-27 15:05:59 +1100324}
325
326const char* //
Nigel Tao2cf76db2020-02-27 22:42:01 +1100327handle_token(wuffs_base__token t) {
328 do {
329 uint64_t vbc = t.value_base_category();
330 uint64_t vbd = t.value_base_detail();
331 uint64_t len = t.length();
Nigel Tao1b073492020-02-16 22:11:36 +1100332
333 // Handle ']' or '}'.
Nigel Tao9f7a2502020-02-23 09:42:02 +1100334 if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) &&
Nigel Tao2cf76db2020-02-27 22:42:01 +1100335 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) {
Nigel Tao1b073492020-02-16 22:11:36 +1100336 if (depth <= 0) {
337 return "main: internal error: inconsistent depth";
338 }
339 depth--;
340
341 // Write preceding whitespace.
342 if ((ctx != context::in_list_after_bracket) &&
Nigel Tao107f0ef2020-03-01 21:35:02 +1100343 (ctx != context::in_dict_after_brace) && !flag_compact) {
Nigel Tao1b073492020-02-16 22:11:36 +1100344 TRY(write_dst("\n", 1));
345 for (size_t i = 0; i < depth; i++) {
Nigel Tao107f0ef2020-03-01 21:35:02 +1100346 TRY(write_dst(flag_tabs ? INDENT_TABS_STRING : INDENT_SPACES_STRING,
347 flag_indent));
Nigel Tao1b073492020-02-16 22:11:36 +1100348 }
349 }
350
Nigel Tao9f7a2502020-02-23 09:42:02 +1100351 TRY(write_dst(
352 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", 1));
353 ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
354 ? context::in_list_after_value
355 : context::in_dict_after_key;
Nigel Tao1b073492020-02-16 22:11:36 +1100356 goto after_value;
357 }
358
Nigel Taod1c928a2020-02-28 12:43:53 +1100359 // Write preceding whitespace and punctuation, if it wasn't ']', '}' or a
360 // continuation of a multi-token chain.
361 if (t.link_prev()) {
362 // No-op.
363 } else if (ctx == context::in_dict_after_key) {
Nigel Tao107f0ef2020-03-01 21:35:02 +1100364 TRY(write_dst(": ", flag_compact ? 1 : 2));
Nigel Taod1c928a2020-02-28 12:43:53 +1100365 } else if (ctx != context::none) {
366 if ((ctx != context::in_list_after_bracket) &&
367 (ctx != context::in_dict_after_brace)) {
368 TRY(write_dst(",", 1));
369 }
Nigel Tao107f0ef2020-03-01 21:35:02 +1100370 if (!flag_compact) {
371 TRY(write_dst("\n", 1));
372 for (size_t i = 0; i < depth; i++) {
373 TRY(write_dst(flag_tabs ? INDENT_TABS_STRING : INDENT_SPACES_STRING,
374 flag_indent));
375 }
Nigel Tao1b073492020-02-16 22:11:36 +1100376 }
377 }
378
379 // Handle the token itself: either a container ('[' or '{') or a simple
Nigel Tao85fba7f2020-02-29 16:28:06 +1100380 // value: string (a chain of raw or escaped parts), literal or number.
Nigel Tao1b073492020-02-16 22:11:36 +1100381 switch (vbc) {
Nigel Tao85fba7f2020-02-29 16:28:06 +1100382 case WUFFS_BASE__TOKEN__VBC__STRUCTURE:
383 TRY(write_dst(
384 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", 1));
385 depth++;
386 ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
387 ? context::in_list_after_bracket
388 : context::in_dict_after_brace;
389 return nullptr;
390
Nigel Tao2cf76db2020-02-27 22:42:01 +1100391 case WUFFS_BASE__TOKEN__VBC__STRING:
Nigel Taod1c928a2020-02-28 12:43:53 +1100392 if (!t.link_prev()) {
Nigel Tao2cf76db2020-02-27 22:42:01 +1100393 TRY(write_dst("\"", 1));
394 }
Nigel Taocb37a562020-02-28 09:56:24 +1100395
396 if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) {
397 // No-op.
398 } else if (vbd &
399 WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) {
400 TRY(write_dst(src.data.ptr + curr_token_end_src_index - len, len));
401 } else {
402 return "main: internal error: unexpected string-token conversion";
403 }
404
Nigel Taod1c928a2020-02-28 12:43:53 +1100405 if (t.link_next()) {
Nigel Tao2cf76db2020-02-27 22:42:01 +1100406 return nullptr;
407 }
408 TRY(write_dst("\"", 1));
409 goto after_value;
410
411 case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT:
412 return handle_unicode_code_point(vbd);
413
Nigel Tao85fba7f2020-02-29 16:28:06 +1100414 case WUFFS_BASE__TOKEN__VBC__LITERAL:
Nigel Tao2cf76db2020-02-27 22:42:01 +1100415 case WUFFS_BASE__TOKEN__VBC__NUMBER:
416 TRY(write_dst(src.data.ptr + curr_token_end_src_index - len, len));
417 goto after_value;
Nigel Tao1b073492020-02-16 22:11:36 +1100418 }
419
420 // Return an error if we didn't match the (vbc, vbd) pair.
Nigel Tao2cf76db2020-02-27 22:42:01 +1100421 return "main: internal error: unexpected token";
422 } while (0);
Nigel Tao1b073492020-02-16 22:11:36 +1100423
Nigel Tao2cf76db2020-02-27 22:42:01 +1100424 // Book-keeping after completing a value (whether a container value or a
425 // simple value). Empty parent containers are no longer empty. If the parent
426 // container is a "{...}" object, toggle between keys and values.
427after_value:
428 if (depth == 0) {
429 return eod;
430 }
431 switch (ctx) {
432 case context::in_list_after_bracket:
433 ctx = context::in_list_after_value;
434 break;
435 case context::in_dict_after_brace:
436 ctx = context::in_dict_after_key;
437 break;
438 case context::in_dict_after_key:
439 ctx = context::in_dict_after_value;
440 break;
441 case context::in_dict_after_value:
442 ctx = context::in_dict_after_key;
443 break;
444 }
445 return nullptr;
446}
447
448const char* //
449main1(int argc, char** argv) {
450 TRY(initialize_globals(argc, argv));
451
452 while (true) {
453 wuffs_base__status status = dec.decode_tokens(&tok, &src);
454
455 while (tok.meta.ri < tok.meta.wi) {
456 wuffs_base__token t = tok.data.ptr[tok.meta.ri++];
457 uint64_t n = t.length();
458 if ((src.meta.ri - curr_token_end_src_index) < n) {
459 return "main: internal error: inconsistent src indexes";
460 }
461 curr_token_end_src_index += n;
462
463 if (t.value() == 0) {
464 continue;
465 }
466
467 const char* z = handle_token(t);
468 if (z == nullptr) {
469 continue;
470 } else if (z == eod) {
471 break;
472 }
473 return z;
Nigel Tao1b073492020-02-16 22:11:36 +1100474 }
Nigel Tao2cf76db2020-02-27 22:42:01 +1100475
476 if (status.repr == nullptr) {
477 break;
478 } else if (status.repr == wuffs_base__suspension__short_read) {
479 if (curr_token_end_src_index != src.meta.ri) {
480 return "main: internal error: inconsistent src indexes";
481 }
482 TRY(read_src());
483 curr_token_end_src_index = src.meta.ri;
484 } else if (status.repr == wuffs_base__suspension__short_write) {
485 tok.compact();
486 } else {
487 return status.message();
Nigel Tao1b073492020-02-16 22:11:36 +1100488 }
489 }
Nigel Tao6b161af2020-02-24 11:01:48 +1100490
Nigel Tao6b161af2020-02-24 11:01:48 +1100491 // Consume an optional whitespace trailer. This isn't part of the JSON spec,
492 // but it works better with line oriented Unix tools (such as "echo 123 |
493 // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which
494 // can accidentally contain trailing whitespace.
495 //
496 // A whitespace trailer is zero or more ' ' and then zero or one '\n'.
497 while (true) {
498 if (src.meta.ri < src.meta.wi) {
499 uint8_t c = src.data.ptr[src.meta.ri];
500 if (c == ' ') {
501 src.meta.ri++;
502 continue;
503 } else if (c == '\n') {
504 src.meta.ri++;
505 break;
506 }
507 // The "exhausted the input" check below will fail.
508 break;
509 } else if (src.meta.closed) {
510 break;
511 }
512 TRY(read_src());
513 }
514
515 // Check that we've exhausted the input.
516 if ((src.meta.ri < src.meta.wi) || !src.meta.closed) {
517 return "main: valid JSON followed by further (unexpected) data";
518 }
519
520 // Check that we've used all of the decoded tokens, other than trailing
521 // filler tokens. For example, a bare `"foo"` string is valid JSON, but even
522 // without a trailing '\n', the Wuffs JSON parser emits a filler token for
523 // the final '\"'.
524 for (; tok.meta.ri < tok.meta.wi; tok.meta.ri++) {
525 if (tok.data.ptr[tok.meta.ri].value_base_category() !=
526 WUFFS_BASE__TOKEN__VBC__FILLER) {
527 return "main: internal error: decoded OK but unprocessed tokens remain";
528 }
529 }
530
531 return nullptr;
Nigel Tao1b073492020-02-16 22:11:36 +1100532}
533
Nigel Tao2914bae2020-02-26 09:40:30 +1100534int //
535compute_exit_code(const char* status_msg) {
Nigel Tao9cc2c252020-02-23 17:05:49 +1100536 if (!status_msg) {
537 return 0;
538 }
539 size_t n = strnlen(status_msg, 2047);
540 if (n >= 2047) {
541 status_msg = "main: internal error: error message is too long";
542 n = strnlen(status_msg, 2047);
543 }
544 fprintf(stderr, "%s\n", status_msg);
545 // Return an exit code of 1 for regular (forseen) errors, e.g. badly
546 // formatted or unsupported input.
547 //
548 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
549 // run-time checks found that an internal invariant did not hold.
550 //
551 // Automated testing, including badly formatted inputs, can therefore
552 // discriminate between expected failure (exit code 1) and unexpected failure
553 // (other non-zero exit codes). Specifically, exit code 2 for internal
554 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
555 // linux) for a segmentation fault (e.g. null pointer dereference).
556 return strstr(status_msg, "internal error:") ? 2 : 1;
557}
558
Nigel Tao2914bae2020-02-26 09:40:30 +1100559int //
560main(int argc, char** argv) {
Nigel Tao1b073492020-02-16 22:11:36 +1100561 const char* z0 = main1(argc, argv);
Nigel Tao2cf76db2020-02-27 22:42:01 +1100562 const char* z1 = write_dst("\n", 1);
563 const char* z2 = flush_dst();
564 int exit_code = compute_exit_code(z0 ? z0 : (z1 ? z1 : z2));
Nigel Tao9cc2c252020-02-23 17:05:49 +1100565 return exit_code;
Nigel Tao1b073492020-02-16 22:11:36 +1100566}