blob: bc0fd8f3a569cb3b10c38e5985089030d5e97e72 [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
64#ifndef DST_BUFFER_SIZE
65#define DST_BUFFER_SIZE (32 * 1024)
66#endif
67#ifndef SRC_BUFFER_SIZE
68#define SRC_BUFFER_SIZE (32 * 1024)
69#endif
70#ifndef TOKEN_BUFFER_SIZE
71#define TOKEN_BUFFER_SIZE (4 * 1024)
72#endif
73
74uint8_t dst_buffer[DST_BUFFER_SIZE];
75uint8_t src_buffer[SRC_BUFFER_SIZE];
76wuffs_base__token tok_buffer[TOKEN_BUFFER_SIZE];
77
78wuffs_base__io_buffer dst;
79wuffs_base__io_buffer src;
80wuffs_base__token_buffer tok;
81
82wuffs_json__decoder dec;
83wuffs_base__status dec_status;
84
85// dec_current_token_end_src_index is the src.data.ptr index of the end of the
86// current token. An invariant is that (dec_current_token_end_src_index <=
87// src.meta.ri).
88size_t dec_current_token_end_src_index;
89
90#define MAX_INDENT 8
91#define INDENT_STRING " "
92size_t indent;
93
94#define TRY(error_msg) \
95 do { \
96 const char* z = error_msg; \
97 if (z) { \
98 return z; \
99 } \
100 } while (false)
101
102// ----
103
Nigel Tao2914bae2020-02-26 09:40:30 +1100104const char* //
105read_src() {
Nigel Taoa8406922020-02-19 12:22:00 +1100106 if (src.meta.closed) {
Nigel Tao9cc2c252020-02-23 17:05:49 +1100107 return "main: internal error: read requested on a closed source";
Nigel Taoa8406922020-02-19 12:22:00 +1100108 }
Nigel Tao1b073492020-02-16 22:11:36 +1100109 src.compact();
110 if (src.meta.wi >= src.data.len) {
111 return "main: src buffer is full";
112 }
113 size_t n = fread(src.data.ptr + src.meta.wi, sizeof(uint8_t),
114 src.data.len - src.meta.wi, stdin);
115 src.meta.wi += n;
Nigel Tao67306562020-02-19 14:04:49 +1100116 src.meta.closed = feof(stdin);
117 if ((n == 0) && !src.meta.closed) {
Nigel Taoa8406922020-02-19 12:22:00 +1100118 return "main: read error";
Nigel Tao1b073492020-02-16 22:11:36 +1100119 }
120 return nullptr;
121}
122
Nigel Tao2914bae2020-02-26 09:40:30 +1100123const char* //
124flush_dst() {
Nigel Tao1b073492020-02-16 22:11:36 +1100125 size_t n = dst.meta.wi - dst.meta.ri;
126 if (n > 0) {
127 size_t i = fwrite(dst.data.ptr + dst.meta.ri, sizeof(uint8_t), n, stdout);
128 dst.meta.ri += i;
129 if (i != n) {
130 return "main: write error";
131 }
132 dst.compact();
133 }
134 return nullptr;
135}
136
Nigel Tao2914bae2020-02-26 09:40:30 +1100137const char* //
138write_dst(const void* s, size_t n) {
Nigel Tao1b073492020-02-16 22:11:36 +1100139 const uint8_t* p = static_cast<const uint8_t*>(s);
140 while (n > 0) {
141 size_t i = dst.writer_available();
142 if (i == 0) {
143 const char* z = flush_dst();
144 if (z) {
145 return z;
146 }
147 i = dst.writer_available();
148 if (i == 0) {
149 return "main: dst buffer is full";
150 }
151 }
152
153 if (i > n) {
154 i = n;
155 }
156 memcpy(dst.data.ptr + dst.meta.wi, p, i);
157 dst.meta.wi += i;
158 p += i;
159 n -= i;
160 }
161 return nullptr;
162}
163
164// ----
165
166enum class context {
167 none,
168 in_list_after_bracket,
169 in_list_after_value,
170 in_dict_after_brace,
171 in_dict_after_key,
172 in_dict_after_value,
173};
174
175// parsed_token is a result type, combining a wuffs_base_token and an error.
176// For the parsed_token returned by make_parsed_token, it also contains the src
177// data bytes for the token. This slice is just a view into the src_buffer
178// array, and its contents may change on the next call to parse_next_token.
179//
180// An invariant is that (token.length() == data.len).
181typedef struct {
182 const char* error_msg;
183 wuffs_base__token token;
184 wuffs_base__slice_u8 data;
185} parsed_token;
186
Nigel Tao2914bae2020-02-26 09:40:30 +1100187parsed_token //
188make_pt_error(const char* error_msg) {
Nigel Tao1b073492020-02-16 22:11:36 +1100189 parsed_token p;
190 p.error_msg = error_msg;
191 p.token = wuffs_base__make_token(0);
192 p.data = wuffs_base__make_slice_u8(nullptr, 0);
193 return p;
194}
195
Nigel Tao2914bae2020-02-26 09:40:30 +1100196parsed_token //
197make_pt_token(uint64_t token_repr, uint8_t* data_ptr, size_t data_len) {
Nigel Tao1b073492020-02-16 22:11:36 +1100198 parsed_token p;
199 p.error_msg = nullptr;
200 p.token = wuffs_base__make_token(token_repr);
201 p.data = wuffs_base__make_slice_u8(data_ptr, data_len);
202 return p;
203}
204
Nigel Tao2914bae2020-02-26 09:40:30 +1100205parsed_token //
206parse_next_token() {
Nigel Tao1b073492020-02-16 22:11:36 +1100207 while (true) {
208 // Return a previously produced token, if one exists.
209 //
210 // We do this before checking dec_status. This is analogous to Go's
211 // io.Reader's documented idiom, when processing io.Reader.Read's returned
212 // (n int, err error), to "process the n > 0 bytes returned before
213 // considering the error err. Doing so correctly handles I/O errors that
214 // happen after reading some bytes".
215 if (tok.meta.ri < tok.meta.wi) {
216 wuffs_base__token t = tok.data.ptr[tok.meta.ri++];
217
218 uint64_t n = t.length();
219 if ((src.meta.ri - dec_current_token_end_src_index) < n) {
220 return make_pt_error("main: internal error: inconsistent src indexes");
221 }
222 dec_current_token_end_src_index += n;
223
224 // Filter out any filler tokens (e.g. whitespace).
Nigel Tao6b161af2020-02-24 11:01:48 +1100225 if (t.value_base_category() == WUFFS_BASE__TOKEN__VBC__FILLER) {
Nigel Tao1b073492020-02-16 22:11:36 +1100226 continue;
227 }
228
229 return make_pt_token(
230 t.repr, src.data.ptr + dec_current_token_end_src_index - n, n);
231 }
232
233 // Now consider dec_status.
234 if (dec_status.repr == nullptr) {
235 return make_pt_error("main: internal error: parser stopped");
236
237 } else if (dec_status.repr == wuffs_base__suspension__short_read) {
238 if (dec_current_token_end_src_index != src.meta.ri) {
239 return make_pt_error("main: internal error: inconsistent src indexes");
240 }
241 const char* z = read_src();
242 if (z) {
243 return make_pt_error(z);
244 }
245 dec_current_token_end_src_index = src.meta.ri;
246
247 } else if (dec_status.repr == wuffs_base__suspension__short_write) {
248 tok.compact();
249
250 } else {
251 return make_pt_error(dec_status.message());
252 }
253
254 // Retry a "short read" or "short write" suspension.
255 dec_status = dec.decode_tokens(&tok, &src);
256 }
257}
258
259// ----
260
Nigel Tao2914bae2020-02-26 09:40:30 +1100261uint8_t //
262hex_digit(uint8_t nibble) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100263 nibble &= 0x0F;
264 if (nibble <= 9) {
265 return '0' + nibble;
266 }
267 return ('A' - 10) + nibble;
268}
269
Nigel Tao2914bae2020-02-26 09:40:30 +1100270const char* //
271handle_string(parsed_token pt) {
Nigel Tao0711f232020-02-17 13:17:06 +1100272 TRY(write_dst("\"", 1));
Nigel Tao1b073492020-02-16 22:11:36 +1100273 while (true) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100274 uint64_t vbc = pt.token.value_base_category();
275 uint64_t vbd = pt.token.value_base_detail();
276
Nigel Tao9f7a2502020-02-23 09:42:02 +1100277 if (vbc == WUFFS_BASE__TOKEN__VBC__STRING) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100278 TRY(write_dst(pt.data.ptr, pt.data.len));
Nigel Tao9f7a2502020-02-23 09:42:02 +1100279 if ((vbd & WUFFS_BASE__TOKEN__VBD__STRING__INCOMPLETE) == 0) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100280 break;
281 }
282
Nigel Tao9f7a2502020-02-23 09:42:02 +1100283 } else if (vbc != WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT) {
284 return "main: unexpected token";
285
Nigel Taob5461bd2020-02-21 14:13:37 +1100286 } else if (vbd < 0x0020) {
287 switch (vbd) {
288 case '\b':
289 TRY(write_dst("\\b", 2));
290 break;
291 case '\f':
292 TRY(write_dst("\\f", 2));
293 break;
294 case '\n':
295 TRY(write_dst("\\n", 2));
296 break;
297 case '\r':
298 TRY(write_dst("\\r", 2));
299 break;
300 case '\t':
301 TRY(write_dst("\\t", 2));
302 break;
303 default: {
304 // Other bytes less than 0x0020 are valid UTF-8 but not valid in a
305 // JSON string. They need to remain escaped.
306 uint8_t esc6[6];
307 esc6[0] = '\\';
308 esc6[1] = 'u';
309 esc6[2] = '0';
310 esc6[3] = '0';
311 esc6[4] = hex_digit(vbd >> 4);
312 esc6[5] = hex_digit(vbd >> 0);
313 TRY(write_dst(&esc6[0], 6));
314 break;
315 }
316 }
317
318 } else if (vbd <= 0x007F) {
319 switch (vbd) {
320 case '\"':
321 TRY(write_dst("\\\"", 2));
322 break;
323 case '\\':
324 TRY(write_dst("\\\\", 2));
325 break;
326 default: {
327 // The UTF-8 encoding takes 1 byte.
328 uint8_t esc0 = (uint8_t)(vbd);
329 TRY(write_dst(&esc0, 1));
330 break;
331 }
332 }
333
334 } else if (vbd <= 0x07FF) {
335 // The UTF-8 encoding takes 2 bytes.
Nigel Tao16b0c462020-02-24 23:12:39 +1100336 uint8_t esc2[2];
Nigel Taob5461bd2020-02-21 14:13:37 +1100337 esc2[0] = 0xC0 | (uint8_t)((vbd >> 6));
338 esc2[1] = 0x80 | (uint8_t)((vbd >> 0) & 0x3F);
339 TRY(write_dst(&esc2[0], 2));
340
341 } else if (vbd <= 0xFFFF) {
Nigel Tao16b0c462020-02-24 23:12:39 +1100342 if ((0xD800 <= vbd) && (vbd <= 0xDFFF)) {
343 return "main: unexpected Unicode surrogate";
344 }
Nigel Taob5461bd2020-02-21 14:13:37 +1100345 // The UTF-8 encoding takes 3 bytes.
Nigel Tao16b0c462020-02-24 23:12:39 +1100346 uint8_t esc3[3];
Nigel Taob5461bd2020-02-21 14:13:37 +1100347 esc3[0] = 0xE0 | (uint8_t)((vbd >> 12));
348 esc3[1] = 0x80 | (uint8_t)((vbd >> 6) & 0x3F);
349 esc3[2] = 0x80 | (uint8_t)((vbd >> 0) & 0x3F);
350 TRY(write_dst(&esc3[0], 3));
351
Nigel Tao16b0c462020-02-24 23:12:39 +1100352 } else if (vbd <= 0x10FFFF) {
353 // The UTF-8 encoding takes 4 bytes.
354 uint8_t esc4[4];
355 esc4[0] = 0xF0 | (uint8_t)((vbd >> 18));
356 esc4[1] = 0x80 | (uint8_t)((vbd >> 12) & 0x3F);
357 esc4[2] = 0x80 | (uint8_t)((vbd >> 6) & 0x3F);
358 esc4[3] = 0x80 | (uint8_t)((vbd >> 0) & 0x3F);
359 TRY(write_dst(&esc4[0], 4));
360
Nigel Taob5461bd2020-02-21 14:13:37 +1100361 } else {
362 return "main: unexpected Unicode code point";
Nigel Tao1b073492020-02-16 22:11:36 +1100363 }
Nigel Taob5461bd2020-02-21 14:13:37 +1100364
Nigel Tao1b073492020-02-16 22:11:36 +1100365 pt = parse_next_token();
366 if (pt.error_msg) {
367 return pt.error_msg;
368 }
369 }
370 TRY(write_dst("\"", 1));
371 return nullptr;
372}
373
Nigel Tao2914bae2020-02-26 09:40:30 +1100374const char* //
375main2() {
Nigel Tao1b073492020-02-16 22:11:36 +1100376 dec_status = dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
377 if (!dec_status.is_ok()) {
378 return dec_status.message();
379 }
380 dec_status = dec.decode_tokens(&tok, &src);
381 dec_current_token_end_src_index = 0;
382
383 uint64_t depth = 0;
384 context ctx = context::none;
385
386continue_loop:
387 while (true) {
388 parsed_token pt = parse_next_token();
389 if (pt.error_msg) {
390 return pt.error_msg;
391 }
392 uint64_t vbc = pt.token.value_base_category();
393 uint64_t vbd = pt.token.value_base_detail();
394
395 // Handle ']' or '}'.
Nigel Tao9f7a2502020-02-23 09:42:02 +1100396 if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) &&
397 ((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP) != 0)) {
Nigel Tao1b073492020-02-16 22:11:36 +1100398 if (depth <= 0) {
399 return "main: internal error: inconsistent depth";
400 }
401 depth--;
402
403 // Write preceding whitespace.
404 if ((ctx != context::in_list_after_bracket) &&
405 (ctx != context::in_dict_after_brace)) {
406 TRY(write_dst("\n", 1));
407 for (size_t i = 0; i < depth; i++) {
408 TRY(write_dst(INDENT_STRING, indent));
409 }
410 }
411
Nigel Tao9f7a2502020-02-23 09:42:02 +1100412 TRY(write_dst(
413 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", 1));
414 ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
415 ? context::in_list_after_value
416 : context::in_dict_after_key;
Nigel Tao1b073492020-02-16 22:11:36 +1100417 goto after_value;
418 }
419
420 // Write preceding whitespace and punctuation, if it wasn't ']' or '}'.
421 if (ctx == context::in_dict_after_key) {
422 TRY(write_dst(": ", 2));
423 } else if (ctx != context::none) {
424 if ((ctx != context::in_list_after_bracket) &&
425 (ctx != context::in_dict_after_brace)) {
426 TRY(write_dst(",", 1));
427 }
428 TRY(write_dst("\n", 1));
429 for (size_t i = 0; i < depth; i++) {
430 TRY(write_dst(INDENT_STRING, indent));
431 }
432 }
433
434 // Handle the token itself: either a container ('[' or '{') or a simple
435 // value (number, string or literal).
436 switch (vbc) {
Nigel Tao9f7a2502020-02-23 09:42:02 +1100437 case WUFFS_BASE__TOKEN__VBC__STRUCTURE:
438 TRY(write_dst(
439 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", 1));
Nigel Tao1b073492020-02-16 22:11:36 +1100440 depth++;
Nigel Tao9f7a2502020-02-23 09:42:02 +1100441 ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
442 ? context::in_list_after_bracket
443 : context::in_dict_after_brace;
Nigel Tao1b073492020-02-16 22:11:36 +1100444 goto continue_loop;
445
Nigel Tao9f7a2502020-02-23 09:42:02 +1100446 case WUFFS_BASE__TOKEN__VBC__NUMBER:
Nigel Tao8850d382020-02-19 12:25:00 +1100447 TRY(write_dst(pt.data.ptr, pt.data.len));
448 goto after_value;
449
Nigel Tao9f7a2502020-02-23 09:42:02 +1100450 case WUFFS_BASE__TOKEN__VBC__STRING:
Nigel Tao1b073492020-02-16 22:11:36 +1100451 TRY(handle_string(pt));
452 goto after_value;
453 }
454
455 // Return an error if we didn't match the (vbc, vbd) pair.
456 return "main: unexpected token";
457
458 // Book-keeping after completing a value (whether a container value or a
459 // simple value). Empty parent containers are no longer empty. If the
460 // parent container is a "{...}" object, toggle between keys and values.
461 after_value:
462 if (depth <= 0) {
Nigel Tao6b161af2020-02-24 11:01:48 +1100463 goto break_loop;
Nigel Tao1b073492020-02-16 22:11:36 +1100464 }
465 switch (ctx) {
466 case context::in_list_after_bracket:
467 ctx = context::in_list_after_value;
468 break;
469 case context::in_dict_after_brace:
470 ctx = context::in_dict_after_key;
471 break;
472 case context::in_dict_after_key:
473 ctx = context::in_dict_after_value;
474 break;
475 case context::in_dict_after_value:
476 ctx = context::in_dict_after_key;
477 break;
478 }
479 }
Nigel Tao6b161af2020-02-24 11:01:48 +1100480
481break_loop:
482 // Consume an optional whitespace trailer. This isn't part of the JSON spec,
483 // but it works better with line oriented Unix tools (such as "echo 123 |
484 // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which
485 // can accidentally contain trailing whitespace.
486 //
487 // A whitespace trailer is zero or more ' ' and then zero or one '\n'.
488 while (true) {
489 if (src.meta.ri < src.meta.wi) {
490 uint8_t c = src.data.ptr[src.meta.ri];
491 if (c == ' ') {
492 src.meta.ri++;
493 continue;
494 } else if (c == '\n') {
495 src.meta.ri++;
496 break;
497 }
498 // The "exhausted the input" check below will fail.
499 break;
500 } else if (src.meta.closed) {
501 break;
502 }
503 TRY(read_src());
504 }
505
506 // Check that we've exhausted the input.
507 if ((src.meta.ri < src.meta.wi) || !src.meta.closed) {
508 return "main: valid JSON followed by further (unexpected) data";
509 }
510
511 // Check that we've used all of the decoded tokens, other than trailing
512 // filler tokens. For example, a bare `"foo"` string is valid JSON, but even
513 // without a trailing '\n', the Wuffs JSON parser emits a filler token for
514 // the final '\"'.
515 for (; tok.meta.ri < tok.meta.wi; tok.meta.ri++) {
516 if (tok.data.ptr[tok.meta.ri].value_base_category() !=
517 WUFFS_BASE__TOKEN__VBC__FILLER) {
518 return "main: internal error: decoded OK but unprocessed tokens remain";
519 }
520 }
521
522 return nullptr;
Nigel Tao1b073492020-02-16 22:11:36 +1100523}
524
Nigel Tao2914bae2020-02-26 09:40:30 +1100525const char* //
526main1(int argc, char** argv) {
Nigel Tao1b073492020-02-16 22:11:36 +1100527 dst = wuffs_base__make_io_buffer(
528 wuffs_base__make_slice_u8(dst_buffer, DST_BUFFER_SIZE),
529 wuffs_base__empty_io_buffer_meta());
530
531 src = wuffs_base__make_io_buffer(
532 wuffs_base__make_slice_u8(src_buffer, SRC_BUFFER_SIZE),
533 wuffs_base__empty_io_buffer_meta());
534
535 tok = wuffs_base__make_token_buffer(
536 wuffs_base__make_slice_token(tok_buffer, TOKEN_BUFFER_SIZE),
537 wuffs_base__empty_token_buffer_meta());
538
539 indent = 4;
540
541 TRY(main2());
542 TRY(write_dst("\n", 1));
543 return nullptr;
544}
545
Nigel Tao2914bae2020-02-26 09:40:30 +1100546int //
547compute_exit_code(const char* status_msg) {
Nigel Tao9cc2c252020-02-23 17:05:49 +1100548 if (!status_msg) {
549 return 0;
550 }
551 size_t n = strnlen(status_msg, 2047);
552 if (n >= 2047) {
553 status_msg = "main: internal error: error message is too long";
554 n = strnlen(status_msg, 2047);
555 }
556 fprintf(stderr, "%s\n", status_msg);
557 // Return an exit code of 1 for regular (forseen) errors, e.g. badly
558 // formatted or unsupported input.
559 //
560 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
561 // run-time checks found that an internal invariant did not hold.
562 //
563 // Automated testing, including badly formatted inputs, can therefore
564 // discriminate between expected failure (exit code 1) and unexpected failure
565 // (other non-zero exit codes). Specifically, exit code 2 for internal
566 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
567 // linux) for a segmentation fault (e.g. null pointer dereference).
568 return strstr(status_msg, "internal error:") ? 2 : 1;
569}
570
Nigel Tao2914bae2020-02-26 09:40:30 +1100571int //
572main(int argc, char** argv) {
Nigel Tao1b073492020-02-16 22:11:36 +1100573 const char* z0 = main1(argc, argv);
574 const char* z1 = flush_dst();
Nigel Tao9cc2c252020-02-23 17:05:49 +1100575 int exit_code = compute_exit_code(z0 ? z0 : z1);
576 return exit_code;
Nigel Tao1b073492020-02-16 22:11:36 +1100577}