blob: 23388844d7c101009ff5eaa4695df5ffd716ed39 [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* //
Nigel Tao3b486982020-02-27 15:05:59 +1100271handle_unicode_code_point(uint32_t ucp) {
272 if (ucp < 0x0020) {
273 switch (ucp) {
274 case '\b':
275 return write_dst("\\b", 2);
276 case '\f':
277 return write_dst("\\f", 2);
278 case '\n':
279 return write_dst("\\n", 2);
280 case '\r':
281 return write_dst("\\r", 2);
282 case '\t':
283 return write_dst("\\t", 2);
284 default: {
285 // Other bytes less than 0x0020 are valid UTF-8 but not valid in a
286 // JSON string. They need to remain escaped.
287 uint8_t esc6[6];
288 esc6[0] = '\\';
289 esc6[1] = 'u';
290 esc6[2] = '0';
291 esc6[3] = '0';
292 esc6[4] = hex_digit(ucp >> 4);
293 esc6[5] = hex_digit(ucp >> 0);
294 return write_dst(&esc6[0], 6);
295 }
296 }
297
298 } else if (ucp <= 0x007F) {
299 switch (ucp) {
300 case '\"':
301 return write_dst("\\\"", 2);
302 case '\\':
303 return write_dst("\\\\", 2);
304 default: {
305 // The UTF-8 encoding takes 1 byte.
306 uint8_t esc0 = (uint8_t)(ucp);
307 return write_dst(&esc0, 1);
308 }
309 }
310
311 } else if (ucp <= 0x07FF) {
312 // The UTF-8 encoding takes 2 bytes.
313 uint8_t esc2[2];
314 esc2[0] = 0xC0 | (uint8_t)((ucp >> 6));
315 esc2[1] = 0x80 | (uint8_t)((ucp >> 0) & 0x3F);
316 return write_dst(&esc2[0], 2);
317
318 } else if (ucp <= 0xFFFF) {
319 if ((0xD800 <= ucp) && (ucp <= 0xDFFF)) {
320 return "main: unexpected Unicode surrogate";
321 }
322 // The UTF-8 encoding takes 3 bytes.
323 uint8_t esc3[3];
324 esc3[0] = 0xE0 | (uint8_t)((ucp >> 12));
325 esc3[1] = 0x80 | (uint8_t)((ucp >> 6) & 0x3F);
326 esc3[2] = 0x80 | (uint8_t)((ucp >> 0) & 0x3F);
327 return write_dst(&esc3[0], 3);
328
329 } else if (ucp <= 0x10FFFF) {
330 // The UTF-8 encoding takes 4 bytes.
331 uint8_t esc4[4];
332 esc4[0] = 0xF0 | (uint8_t)((ucp >> 18));
333 esc4[1] = 0x80 | (uint8_t)((ucp >> 12) & 0x3F);
334 esc4[2] = 0x80 | (uint8_t)((ucp >> 6) & 0x3F);
335 esc4[3] = 0x80 | (uint8_t)((ucp >> 0) & 0x3F);
336 return write_dst(&esc4[0], 4);
337 }
338
339 return "main: unexpected Unicode code point";
340}
341
342const char* //
Nigel Tao2914bae2020-02-26 09:40:30 +1100343handle_string(parsed_token pt) {
Nigel Tao0711f232020-02-17 13:17:06 +1100344 TRY(write_dst("\"", 1));
Nigel Tao1b073492020-02-16 22:11:36 +1100345 while (true) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100346 uint64_t vbc = pt.token.value_base_category();
347 uint64_t vbd = pt.token.value_base_detail();
348
Nigel Tao9f7a2502020-02-23 09:42:02 +1100349 if (vbc == WUFFS_BASE__TOKEN__VBC__STRING) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100350 TRY(write_dst(pt.data.ptr, pt.data.len));
Nigel Tao9f7a2502020-02-23 09:42:02 +1100351 if ((vbd & WUFFS_BASE__TOKEN__VBD__STRING__INCOMPLETE) == 0) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100352 break;
353 }
354
Nigel Tao3b486982020-02-27 15:05:59 +1100355 } else if (vbc == WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT) {
356 TRY(handle_unicode_code_point(vbd));
Nigel Tao16b0c462020-02-24 23:12:39 +1100357
Nigel Taob5461bd2020-02-21 14:13:37 +1100358 } else {
Nigel Tao3b486982020-02-27 15:05:59 +1100359 return "main: unexpected token";
Nigel Tao1b073492020-02-16 22:11:36 +1100360 }
Nigel Taob5461bd2020-02-21 14:13:37 +1100361
Nigel Tao1b073492020-02-16 22:11:36 +1100362 pt = parse_next_token();
363 if (pt.error_msg) {
364 return pt.error_msg;
365 }
366 }
367 TRY(write_dst("\"", 1));
368 return nullptr;
369}
370
Nigel Tao2914bae2020-02-26 09:40:30 +1100371const char* //
372main2() {
Nigel Tao1b073492020-02-16 22:11:36 +1100373 dec_status = dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
374 if (!dec_status.is_ok()) {
375 return dec_status.message();
376 }
377 dec_status = dec.decode_tokens(&tok, &src);
378 dec_current_token_end_src_index = 0;
379
380 uint64_t depth = 0;
381 context ctx = context::none;
382
383continue_loop:
384 while (true) {
385 parsed_token pt = parse_next_token();
386 if (pt.error_msg) {
387 return pt.error_msg;
388 }
389 uint64_t vbc = pt.token.value_base_category();
390 uint64_t vbd = pt.token.value_base_detail();
391
392 // Handle ']' or '}'.
Nigel Tao9f7a2502020-02-23 09:42:02 +1100393 if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) &&
394 ((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP) != 0)) {
Nigel Tao1b073492020-02-16 22:11:36 +1100395 if (depth <= 0) {
396 return "main: internal error: inconsistent depth";
397 }
398 depth--;
399
400 // Write preceding whitespace.
401 if ((ctx != context::in_list_after_bracket) &&
402 (ctx != context::in_dict_after_brace)) {
403 TRY(write_dst("\n", 1));
404 for (size_t i = 0; i < depth; i++) {
405 TRY(write_dst(INDENT_STRING, indent));
406 }
407 }
408
Nigel Tao9f7a2502020-02-23 09:42:02 +1100409 TRY(write_dst(
410 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", 1));
411 ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
412 ? context::in_list_after_value
413 : context::in_dict_after_key;
Nigel Tao1b073492020-02-16 22:11:36 +1100414 goto after_value;
415 }
416
417 // Write preceding whitespace and punctuation, if it wasn't ']' or '}'.
418 if (ctx == context::in_dict_after_key) {
419 TRY(write_dst(": ", 2));
420 } else if (ctx != context::none) {
421 if ((ctx != context::in_list_after_bracket) &&
422 (ctx != context::in_dict_after_brace)) {
423 TRY(write_dst(",", 1));
424 }
425 TRY(write_dst("\n", 1));
426 for (size_t i = 0; i < depth; i++) {
427 TRY(write_dst(INDENT_STRING, indent));
428 }
429 }
430
431 // Handle the token itself: either a container ('[' or '{') or a simple
432 // value (number, string or literal).
433 switch (vbc) {
Nigel Tao9f7a2502020-02-23 09:42:02 +1100434 case WUFFS_BASE__TOKEN__VBC__STRUCTURE:
435 TRY(write_dst(
436 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", 1));
Nigel Tao1b073492020-02-16 22:11:36 +1100437 depth++;
Nigel Tao9f7a2502020-02-23 09:42:02 +1100438 ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
439 ? context::in_list_after_bracket
440 : context::in_dict_after_brace;
Nigel Tao1b073492020-02-16 22:11:36 +1100441 goto continue_loop;
442
Nigel Tao9f7a2502020-02-23 09:42:02 +1100443 case WUFFS_BASE__TOKEN__VBC__NUMBER:
Nigel Tao8850d382020-02-19 12:25:00 +1100444 TRY(write_dst(pt.data.ptr, pt.data.len));
445 goto after_value;
446
Nigel Tao9f7a2502020-02-23 09:42:02 +1100447 case WUFFS_BASE__TOKEN__VBC__STRING:
Nigel Tao1b073492020-02-16 22:11:36 +1100448 TRY(handle_string(pt));
449 goto after_value;
450 }
451
452 // Return an error if we didn't match the (vbc, vbd) pair.
453 return "main: unexpected token";
454
455 // Book-keeping after completing a value (whether a container value or a
456 // simple value). Empty parent containers are no longer empty. If the
457 // parent container is a "{...}" object, toggle between keys and values.
458 after_value:
459 if (depth <= 0) {
Nigel Tao6b161af2020-02-24 11:01:48 +1100460 goto break_loop;
Nigel Tao1b073492020-02-16 22:11:36 +1100461 }
462 switch (ctx) {
463 case context::in_list_after_bracket:
464 ctx = context::in_list_after_value;
465 break;
466 case context::in_dict_after_brace:
467 ctx = context::in_dict_after_key;
468 break;
469 case context::in_dict_after_key:
470 ctx = context::in_dict_after_value;
471 break;
472 case context::in_dict_after_value:
473 ctx = context::in_dict_after_key;
474 break;
475 }
476 }
Nigel Tao6b161af2020-02-24 11:01:48 +1100477
478break_loop:
479 // Consume an optional whitespace trailer. This isn't part of the JSON spec,
480 // but it works better with line oriented Unix tools (such as "echo 123 |
481 // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which
482 // can accidentally contain trailing whitespace.
483 //
484 // A whitespace trailer is zero or more ' ' and then zero or one '\n'.
485 while (true) {
486 if (src.meta.ri < src.meta.wi) {
487 uint8_t c = src.data.ptr[src.meta.ri];
488 if (c == ' ') {
489 src.meta.ri++;
490 continue;
491 } else if (c == '\n') {
492 src.meta.ri++;
493 break;
494 }
495 // The "exhausted the input" check below will fail.
496 break;
497 } else if (src.meta.closed) {
498 break;
499 }
500 TRY(read_src());
501 }
502
503 // Check that we've exhausted the input.
504 if ((src.meta.ri < src.meta.wi) || !src.meta.closed) {
505 return "main: valid JSON followed by further (unexpected) data";
506 }
507
508 // Check that we've used all of the decoded tokens, other than trailing
509 // filler tokens. For example, a bare `"foo"` string is valid JSON, but even
510 // without a trailing '\n', the Wuffs JSON parser emits a filler token for
511 // the final '\"'.
512 for (; tok.meta.ri < tok.meta.wi; tok.meta.ri++) {
513 if (tok.data.ptr[tok.meta.ri].value_base_category() !=
514 WUFFS_BASE__TOKEN__VBC__FILLER) {
515 return "main: internal error: decoded OK but unprocessed tokens remain";
516 }
517 }
518
519 return nullptr;
Nigel Tao1b073492020-02-16 22:11:36 +1100520}
521
Nigel Tao2914bae2020-02-26 09:40:30 +1100522const char* //
523main1(int argc, char** argv) {
Nigel Tao1b073492020-02-16 22:11:36 +1100524 dst = wuffs_base__make_io_buffer(
525 wuffs_base__make_slice_u8(dst_buffer, DST_BUFFER_SIZE),
526 wuffs_base__empty_io_buffer_meta());
527
528 src = wuffs_base__make_io_buffer(
529 wuffs_base__make_slice_u8(src_buffer, SRC_BUFFER_SIZE),
530 wuffs_base__empty_io_buffer_meta());
531
532 tok = wuffs_base__make_token_buffer(
533 wuffs_base__make_slice_token(tok_buffer, TOKEN_BUFFER_SIZE),
534 wuffs_base__empty_token_buffer_meta());
535
536 indent = 4;
537
538 TRY(main2());
539 TRY(write_dst("\n", 1));
540 return nullptr;
541}
542
Nigel Tao2914bae2020-02-26 09:40:30 +1100543int //
544compute_exit_code(const char* status_msg) {
Nigel Tao9cc2c252020-02-23 17:05:49 +1100545 if (!status_msg) {
546 return 0;
547 }
548 size_t n = strnlen(status_msg, 2047);
549 if (n >= 2047) {
550 status_msg = "main: internal error: error message is too long";
551 n = strnlen(status_msg, 2047);
552 }
553 fprintf(stderr, "%s\n", status_msg);
554 // Return an exit code of 1 for regular (forseen) errors, e.g. badly
555 // formatted or unsupported input.
556 //
557 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
558 // run-time checks found that an internal invariant did not hold.
559 //
560 // Automated testing, including badly formatted inputs, can therefore
561 // discriminate between expected failure (exit code 1) and unexpected failure
562 // (other non-zero exit codes). Specifically, exit code 2 for internal
563 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
564 // linux) for a segmentation fault (e.g. null pointer dereference).
565 return strstr(status_msg, "internal error:") ? 2 : 1;
566}
567
Nigel Tao2914bae2020-02-26 09:40:30 +1100568int //
569main(int argc, char** argv) {
Nigel Tao1b073492020-02-16 22:11:36 +1100570 const char* z0 = main1(argc, argv);
571 const char* z1 = flush_dst();
Nigel Tao9cc2c252020-02-23 17:05:49 +1100572 int exit_code = compute_exit_code(z0 ? z0 : z1);
573 return exit_code;
Nigel Tao1b073492020-02-16 22:11:36 +1100574}