blob: a2a5277c362090aed06ba4a69cdd193c655a1eae [file] [log] [blame]
Nigel Tao193527b2020-02-26 21:55:01 +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// print-json-token-debug-format.c parses JSON from stdin and prints the
18// resulting token stream, eliding any non-essential (e.g. whitespace) tokens.
19//
Nigel Taod1c928a2020-02-28 12:43:53 +110020// The output format is only for debugging or regression testing, and certainly
21// not for long term storage. It isn't guaranteed to be stable between versions
22// of this program and of the Wuffs standard library.
Nigel Tao193527b2020-02-26 21:55:01 +110023//
Nigel Taod1c928a2020-02-28 12:43:53 +110024// It prints 16 bytes (128 bits) per token, containing big-endian numbers:
25//
26// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
Nigel Tao496e88b2020-04-09 22:10:08 +100027// | | | | | VALUE_EXTENSION |
28// | POS | LEN | CON |EXT|VALUE_MAJOR| VALUE_MINOR |
29// | | | | | 0 |VBC| VBD |
Nigel Taod1c928a2020-02-28 12:43:53 +110030// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
31//
Nigel Tao3385efa2020-04-02 12:10:10 +110032// - POS (4 bytes) is the position: the sum of all previous tokens' lengths,
33// including elided tokens.
34// - LEN (2 bytes) is the length.
Nigel Tao496e88b2020-04-09 22:10:08 +100035// - CON (2 bytes) is the continued bit
Nigel Tao3385efa2020-04-02 12:10:10 +110036// - EXT (1 bytes) is 1 for extended and 0 for simple tokens.
Nigel Taod1c928a2020-02-28 12:43:53 +110037//
Nigel Tao3385efa2020-04-02 12:10:10 +110038// Extended tokens have a VALUE_EXTENSION (7 bytes).
39//
40// Simple tokens have a VALUE_MAJOR (3 bytes) and then either 4 bytes
41// VALUE_MINOR (when VALUE_MAJOR is non-zero) or (1 + 3) bytes
42// VALUE_BASE_CATEGORY and VALUE_BASE_DETAIL (when VALUE_MAJOR is zero).
43//
44// ----
Nigel Taod1c928a2020-02-28 12:43:53 +110045//
Nigel Tao193527b2020-02-26 21:55:01 +110046// Together with the hexadecimal WUFFS_BASE__TOKEN__ETC constants defined in
47// token-public.h, this format is somewhat human-readable when piped through a
Nigel Taod1c928a2020-02-28 12:43:53 +110048// hex-dump program (such as /usr/bin/hd), printing one token per line.
Nigel Tao6b29f322020-02-29 17:05:24 +110049// Alternatively, pass the -h (--human-readable) flag to this program.
50//
51// Pass -a (--all-tokens) to print all tokens, including whitespace.
Nigel Tao193527b2020-02-26 21:55:01 +110052//
53// If the input or output is larger than the program's buffers (64 MiB and
54// 131072 tokens by default), there may be multiple valid tokenizations of any
55// given input. For example, if a source string "abcde" straddles an I/O
Nigel Tao496e88b2020-04-09 22:10:08 +100056// boundary, it may be tokenized as single (not continued) 5-length string or
57// as a 3-length continued string followed by a 2-length string.
Nigel Tao193527b2020-02-26 21:55:01 +110058//
Nigel Taod1c928a2020-02-28 12:43:53 +110059// A Wuffs token stream, in general, can support inputs more than 0xFFFF_FFFF
Nigel Tao193527b2020-02-26 21:55:01 +110060// bytes long, but this program can not, as it tracks the tokens' cumulative
61// position as a uint32.
62
63#include <inttypes.h>
64#include <stdio.h>
65#include <string.h>
66#include <unistd.h>
67
68// Wuffs ships as a "single file C library" or "header file library" as per
69// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
70//
71// To use that single file as a "foo.c"-like implementation, instead of a
72// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
73// compiling it.
74#define WUFFS_IMPLEMENTATION
75
76// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
77// release/c/etc.c whitelist which parts of Wuffs to build. That file contains
78// the entire Wuffs standard library, implementing a variety of codecs and file
79// formats. Without this macro definition, an optimizing compiler or linker may
80// very well discard Wuffs code for unused codecs, but listing the Wuffs
81// modules we use makes that process explicit. Preprocessing means that such
82// code simply isn't compiled.
83#define WUFFS_CONFIG__MODULES
84#define WUFFS_CONFIG__MODULE__BASE
85#define WUFFS_CONFIG__MODULE__JSON
86
87// If building this program in an environment that doesn't easily accommodate
88// relative includes, you can use the script/inline-c-relative-includes.go
89// program to generate a stand-alone C++ file.
90#include "../release/c/wuffs-unsupported-snapshot.c"
91
Nigel Taof3146c22020-03-26 08:47:42 +110092// Wuffs allows either statically or dynamically allocated work buffers. This
93// program exercises static allocation.
94#define WORK_BUFFER_ARRAY_SIZE \
95 WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
96#if WORK_BUFFER_ARRAY_SIZE > 0
Nigel Taoc0bd9df2020-03-26 21:55:44 +110097uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE];
Nigel Taof3146c22020-03-26 08:47:42 +110098#else
99// Not all C/C++ compilers support 0-length arrays.
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100100uint8_t g_work_buffer_array[1];
Nigel Taof3146c22020-03-26 08:47:42 +1100101#endif
102
Nigel Taofdac24a2020-03-06 21:53:08 +1100103#ifndef SRC_BUFFER_ARRAY_SIZE
104#define SRC_BUFFER_ARRAY_SIZE (64 * 1024 * 1024)
Nigel Tao193527b2020-02-26 21:55:01 +1100105#endif
Nigel Taofdac24a2020-03-06 21:53:08 +1100106#ifndef TOKEN_BUFFER_ARRAY_SIZE
107#define TOKEN_BUFFER_ARRAY_SIZE (128 * 1024)
Nigel Tao193527b2020-02-26 21:55:01 +1100108#endif
109
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100110uint8_t g_src_buffer_array[SRC_BUFFER_ARRAY_SIZE];
111wuffs_base__token g_tok_buffer_array[TOKEN_BUFFER_ARRAY_SIZE];
Nigel Tao193527b2020-02-26 21:55:01 +1100112
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100113wuffs_base__io_buffer g_src;
114wuffs_base__token_buffer g_tok;
Nigel Tao193527b2020-02-26 21:55:01 +1100115
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100116wuffs_json__decoder g_dec;
117wuffs_base__status g_dec_status;
Nigel Tao193527b2020-02-26 21:55:01 +1100118
Nigel Tao193527b2020-02-26 21:55:01 +1100119#define TRY(error_msg) \
120 do { \
121 const char* z = error_msg; \
122 if (z) { \
123 return z; \
124 } \
125 } while (false)
126
Nigel Tao68920952020-03-03 11:25:18 +1100127// ignore_return_value suppresses errors from -Wall -Werror.
128static void //
129ignore_return_value(int ignored) {}
130
Nigel Tao193527b2020-02-26 21:55:01 +1100131const char* //
132read_src() {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100133 if (g_src.meta.closed) {
Nigel Tao193527b2020-02-26 21:55:01 +1100134 return "main: internal error: read requested on a closed source";
135 }
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100136 wuffs_base__io_buffer__compact(&g_src);
137 if (g_src.meta.wi >= g_src.data.len) {
138 return "main: g_src buffer is full";
Nigel Tao193527b2020-02-26 21:55:01 +1100139 }
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100140 size_t n = fread(g_src.data.ptr + g_src.meta.wi, sizeof(uint8_t),
141 g_src.data.len - g_src.meta.wi, stdin);
142 g_src.meta.wi += n;
143 g_src.meta.closed = feof(stdin);
144 if ((n == 0) && !g_src.meta.closed) {
Nigel Tao193527b2020-02-26 21:55:01 +1100145 return "main: read error";
146 }
147 return NULL;
148}
149
150// ----
151
Nigel Tao68920952020-03-03 11:25:18 +1100152struct {
153 int remaining_argc;
154 char** remaining_argv;
155
156 bool all_tokens;
157 bool human_readable;
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100158 bool quirks;
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100159} g_flags = {0};
Nigel Tao68920952020-03-03 11:25:18 +1100160
161const char* //
162parse_flags(int argc, char** argv) {
163 int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name.
164 for (; c < argc; c++) {
165 char* arg = argv[c];
166 if (*arg++ != '-') {
167 break;
168 }
169
170 // A double-dash "--foo" is equivalent to a single-dash "-foo". As special
171 // cases, a bare "-" is not a flag (some programs may interpret it as
172 // stdin) and a bare "--" means to stop parsing flags.
173 if (*arg == '\x00') {
174 break;
175 } else if (*arg == '-') {
176 arg++;
177 if (*arg == '\x00') {
178 c++;
179 break;
180 }
181 }
182
183 if (!strcmp(arg, "a") || !strcmp(arg, "all-tokens")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100184 g_flags.all_tokens = true;
Nigel Tao68920952020-03-03 11:25:18 +1100185 continue;
186 }
187 if (!strcmp(arg, "h") || !strcmp(arg, "human-readable")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100188 g_flags.human_readable = true;
Nigel Tao68920952020-03-03 11:25:18 +1100189 continue;
190 }
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100191 if (!strcmp(arg, "q") || !strcmp(arg, "quirks")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100192 g_flags.quirks = true;
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100193 continue;
194 }
Nigel Tao68920952020-03-03 11:25:18 +1100195
196 return "main: unrecognized flag argument";
197 }
198
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100199 g_flags.remaining_argc = argc - c;
200 g_flags.remaining_argv = argv + c;
Nigel Tao68920952020-03-03 11:25:18 +1100201 return NULL;
202}
203
Nigel Tao496e88b2020-04-09 22:10:08 +1000204const char* g_vbc_names[16] = {
Nigel Tao67f00c12020-02-29 12:48:59 +1100205 "0:Filler..........", //
Nigel Tao85fba7f2020-02-29 16:28:06 +1100206 "1:Structure.......", //
207 "2:String..........", //
208 "3:UnicodeCodePoint", //
209 "4:Literal.........", //
210 "5:Number..........", //
Nigel Tao67f00c12020-02-29 12:48:59 +1100211 "6:Reserved........", //
212 "7:Reserved........", //
Nigel Tao496e88b2020-04-09 22:10:08 +1000213 "8:Reserved........", //
214 "9:Reserved........", //
215 "A:Reserved........", //
216 "B:Reserved........", //
217 "C:Reserved........", //
218 "D:Reserved........", //
219 "E:Reserved........", //
220 "F:Reserved........", //
Nigel Tao67f00c12020-02-29 12:48:59 +1100221};
222
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100223const int g_base38_decode[38] = {
Nigel Tao67f00c12020-02-29 12:48:59 +1100224 ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '?', //
225 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', //
226 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', //
227};
228
Nigel Tao193527b2020-02-26 21:55:01 +1100229const char* //
230main1(int argc, char** argv) {
Nigel Tao68920952020-03-03 11:25:18 +1100231 TRY(parse_flags(argc, argv));
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100232 if (g_flags.remaining_argc > 0) {
Nigel Tao68920952020-03-03 11:25:18 +1100233 return "main: bad argument: use \"program < input\", not \"program input\"";
Nigel Tao67f00c12020-02-29 12:48:59 +1100234 }
235
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100236 g_src = wuffs_base__make_io_buffer(
237 wuffs_base__make_slice_u8(g_src_buffer_array, SRC_BUFFER_ARRAY_SIZE),
Nigel Tao193527b2020-02-26 21:55:01 +1100238 wuffs_base__empty_io_buffer_meta());
239
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100240 g_tok = wuffs_base__make_token_buffer(
241 wuffs_base__make_slice_token(g_tok_buffer_array, TOKEN_BUFFER_ARRAY_SIZE),
Nigel Tao193527b2020-02-26 21:55:01 +1100242 wuffs_base__empty_token_buffer_meta());
243
244 wuffs_base__status init_status = wuffs_json__decoder__initialize(
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100245 &g_dec, sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
Nigel Tao193527b2020-02-26 21:55:01 +1100246 if (!wuffs_base__status__is_ok(&init_status)) {
247 return wuffs_base__status__message(&init_status);
248 }
249
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100250 if (g_flags.quirks) {
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100251 uint32_t quirks[] = {
252 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_A,
253 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_CAPITAL_U,
254 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_E,
255 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_QUESTION_MARK,
256 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_SINGLE_QUOTE,
257 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_V,
258 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X,
259 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_ZERO,
260 WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK,
261 WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE,
262 WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA,
263 WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS,
264 WUFFS_JSON__QUIRK_ALLOW_LEADING_ASCII_RECORD_SEPARATOR,
265 WUFFS_JSON__QUIRK_ALLOW_LEADING_UNICODE_BYTE_ORDER_MARK,
266 WUFFS_JSON__QUIRK_ALLOW_TRAILING_NEW_LINE,
Nigel Tao3bfed6d2020-03-24 17:44:47 +1100267 WUFFS_JSON__QUIRK_REPLACE_INVALID_UNICODE,
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100268 0,
269 };
270 uint32_t i;
271 for (i = 0; quirks[i]; i++) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100272 wuffs_json__decoder__set_quirk_enabled(&g_dec, quirks[i], true);
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100273 }
274 }
275
Nigel Tao193527b2020-02-26 21:55:01 +1100276 uint64_t pos = 0;
277 while (true) {
Nigel Taof3146c22020-03-26 08:47:42 +1100278 wuffs_base__status status = wuffs_json__decoder__decode_tokens(
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100279 &g_dec, &g_tok, &g_src,
280 wuffs_base__make_slice_u8(g_work_buffer_array, WORK_BUFFER_ARRAY_SIZE));
Nigel Tao193527b2020-02-26 21:55:01 +1100281
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100282 while (g_tok.meta.ri < g_tok.meta.wi) {
283 wuffs_base__token* t = &g_tok.data.ptr[g_tok.meta.ri++];
Nigel Taod1c928a2020-02-28 12:43:53 +1100284 uint16_t len = wuffs_base__token__length(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100285
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100286 if (g_flags.all_tokens || (wuffs_base__token__value(t) != 0)) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000287 uint16_t con = wuffs_base__token__continued(t) ? 1 : 0;
Nigel Tao3385efa2020-04-02 12:10:10 +1100288 int32_t vmajor = wuffs_base__token__value_major(t);
Nigel Tao67f00c12020-02-29 12:48:59 +1100289 uint32_t vminor = wuffs_base__token__value_minor(t);
290 uint8_t vbc = wuffs_base__token__value_base_category(t);
291 uint32_t vbd = wuffs_base__token__value_base_detail(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100292
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100293 if (g_flags.human_readable) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000294 printf("pos=0x%08" PRIX32 " len=0x%04" PRIX16 " con=%" PRId16 " ",
295 (uint32_t)(pos), len, con);
Nigel Tao67f00c12020-02-29 12:48:59 +1100296
Nigel Tao3385efa2020-04-02 12:10:10 +1100297 if (vmajor > 0) {
Nigel Taof7ea4622020-04-10 22:44:13 +1000298 char vmajor_name[5];
299 vmajor_name[0] = '*';
300 vmajor_name[1] = '*';
301 vmajor_name[2] = '*';
302 vmajor_name[3] = '*';
303 vmajor_name[4] = '\x00';
Nigel Tao67f00c12020-02-29 12:48:59 +1100304 uint32_t m = vmajor;
Nigel Taof7ea4622020-04-10 22:44:13 +1000305 if (m < 38 * 38 * 38 * 38) {
306 uint32_t m0 = m / (38 * 38 * 38);
307 m -= m0 * (38 * 38 * 38);
308 vmajor_name[0] = g_base38_decode[m0];
Nigel Tao67f00c12020-02-29 12:48:59 +1100309
Nigel Taof7ea4622020-04-10 22:44:13 +1000310 uint32_t m1 = m / (38 * 38);
311 m -= m1 * (38 * 38);
312 vmajor_name[1] = g_base38_decode[m1];
313
314 uint32_t m2 = m / (38);
315 m -= m2 * (38);
316 vmajor_name[2] = g_base38_decode[m2];
317
318 uint32_t m3 = m;
319 vmajor_name[3] = g_base38_decode[m3];
320 }
321
322 printf("vmajor=0x%06" PRIX32 ":%s vminor=0x%06" PRIX32 "\n",
323 vmajor, vmajor_name, vminor);
Nigel Tao3385efa2020-04-02 12:10:10 +1100324 } else if (vmajor == 0) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000325 printf("vbc=%s. vbd=0x%06" PRIX32 "\n", g_vbc_names[vbc & 15],
326 vbd);
Nigel Tao3385efa2020-04-02 12:10:10 +1100327 } else {
328 printf("extended... vextension=0x%012" PRIX64 "\n",
329 wuffs_base__token__value_extension(t));
Nigel Tao67f00c12020-02-29 12:48:59 +1100330 }
331
Nigel Taod1c928a2020-02-28 12:43:53 +1100332 } else {
Nigel Tao67f00c12020-02-29 12:48:59 +1100333 uint8_t buf[16];
334 wuffs_base__store_u32be__no_bounds_check(&buf[0x0], (uint32_t)(pos));
335 wuffs_base__store_u16be__no_bounds_check(&buf[0x4], len);
Nigel Tao496e88b2020-04-09 22:10:08 +1000336 wuffs_base__store_u16be__no_bounds_check(&buf[0x6], con);
Nigel Tao3385efa2020-04-02 12:10:10 +1100337 if (vmajor > 0) {
338 wuffs_base__store_u32be__no_bounds_check(&buf[0x8], vmajor);
Nigel Tao67f00c12020-02-29 12:48:59 +1100339 wuffs_base__store_u32be__no_bounds_check(&buf[0xC], vminor);
Nigel Tao3385efa2020-04-02 12:10:10 +1100340 } else if (vmajor == 0) {
341 wuffs_base__store_u32be__no_bounds_check(&buf[0x8], 0);
Nigel Tao67f00c12020-02-29 12:48:59 +1100342 wuffs_base__store_u8__no_bounds_check(&buf[0x000C], vbc);
343 wuffs_base__store_u24be__no_bounds_check(&buf[0xD], vbd);
Nigel Tao3385efa2020-04-02 12:10:10 +1100344 } else {
345 wuffs_base__store_u8__no_bounds_check(&buf[0x0008], 0x01);
346 wuffs_base__store_u56be__no_bounds_check(
347 &buf[0x9], wuffs_base__token__value_extension(t));
Nigel Tao67f00c12020-02-29 12:48:59 +1100348 }
349 const int stdout_fd = 1;
Nigel Tao68920952020-03-03 11:25:18 +1100350 ignore_return_value(write(stdout_fd, &buf[0], 16));
Nigel Taod1c928a2020-02-28 12:43:53 +1100351 }
Nigel Tao193527b2020-02-26 21:55:01 +1100352 }
353
354 pos += len;
355 if (pos > 0xFFFFFFFF) {
356 return "main: input is too long";
357 }
358 }
359
360 if (status.repr == NULL) {
361 return NULL;
362 } else if (status.repr == wuffs_base__suspension__short_read) {
363 TRY(read_src());
364 } else if (status.repr == wuffs_base__suspension__short_write) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100365 wuffs_base__token_buffer__compact(&g_tok);
Nigel Tao193527b2020-02-26 21:55:01 +1100366 } else {
367 return wuffs_base__status__message(&status);
368 }
369 }
370}
371
372// ----
373
374int //
375compute_exit_code(const char* status_msg) {
376 if (!status_msg) {
377 return 0;
378 }
379 size_t n = strnlen(status_msg, 2047);
380 if (n >= 2047) {
381 status_msg = "main: internal error: error message is too long";
382 n = strnlen(status_msg, 2047);
383 }
384 fprintf(stderr, "%s\n", status_msg);
385 // Return an exit code of 1 for regular (forseen) errors, e.g. badly
386 // formatted or unsupported input.
387 //
388 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
389 // run-time checks found that an internal invariant did not hold.
390 //
391 // Automated testing, including badly formatted inputs, can therefore
392 // discriminate between expected failure (exit code 1) and unexpected failure
393 // (other non-zero exit codes). Specifically, exit code 2 for internal
394 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
395 // linux) for a segmentation fault (e.g. null pointer dereference).
396 return strstr(status_msg, "internal error:") ? 2 : 1;
397}
398
399int //
400main(int argc, char** argv) {
401 const char* z = main1(argc, argv);
402 int exit_code = compute_exit_code(z);
403 return exit_code;
404}