Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1 | // 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 | /* |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 18 | jsonptr is a JSON formatter (pretty-printer) that supports the JSON Pointer |
| 19 | (RFC 6901) query syntax. It reads UTF-8 JSON from stdin and writes |
| 20 | canonicalized, formatted UTF-8 JSON to stdout. |
| 21 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 22 | See the "const char* g_usage" string below for details. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 23 | |
| 24 | ---- |
| 25 | |
| 26 | JSON Pointer (and this program's implementation) is one of many JSON query |
| 27 | languages and JSON tools, such as jq, jql and JMESPath. This one is relatively |
| 28 | simple and fewer-featured compared to those others. |
| 29 | |
| 30 | One benefit of simplicity is that this program's JSON and JSON Pointer |
| 31 | implementations do not dynamically allocate or free memory (yet it does not |
| 32 | require that the entire input fits in memory at once). They are therefore |
| 33 | trivially protected against certain bug classes: memory leaks, double-frees and |
| 34 | use-after-frees. |
| 35 | |
| 36 | The core JSON implementation is also written in the Wuffs programming language |
Nigel Tao | f2eb701 | 2020-03-16 21:10:20 +1100 | [diff] [blame] | 37 | (and then transpiled to C/C++), which is memory-safe (e.g. array indexing is |
| 38 | bounds-checked) but also guards against integer arithmetic overflows. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 39 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 40 | For defense in depth, on Linux, this program also self-imposes a |
| 41 | SECCOMP_MODE_STRICT sandbox before reading (or otherwise processing) its input |
| 42 | or writing its output. Under this sandbox, the only permitted system calls are |
| 43 | read, write, exit and sigreturn. |
| 44 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 45 | All together, this program aims to safely handle untrusted JSON files without |
| 46 | fear of security bugs such as remote code execution. |
| 47 | |
| 48 | ---- |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 49 | |
Nigel Tao | c5b3a9e | 2020-02-24 11:54:35 +1100 | [diff] [blame] | 50 | As of 2020-02-24, this program passes all 318 "test_parsing" cases from the |
| 51 | JSON test suite (https://github.com/nst/JSONTestSuite), an appendix to the |
| 52 | "Parsing JSON is a Minefield" article (http://seriot.ch/parsing_json.php) that |
| 53 | was first published on 2016-10-26 and updated on 2018-03-30. |
| 54 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 55 | After modifying this program, run "build-example.sh example/jsonptr/" and then |
| 56 | "script/run-json-test-suite.sh" to catch correctness regressions. |
| 57 | |
| 58 | ---- |
| 59 | |
Nigel Tao | d0b16cb | 2020-03-14 10:15:54 +1100 | [diff] [blame] | 60 | This program uses Wuffs' JSON decoder at a relatively low level, processing the |
| 61 | decoder's token-stream output individually. The core loop, in pseudo-code, is |
| 62 | "for_each_token { handle_token(etc); }", where the handle_token function |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 63 | changes global state (e.g. the `g_depth` and `g_ctx` variables) and prints |
Nigel Tao | d0b16cb | 2020-03-14 10:15:54 +1100 | [diff] [blame] | 64 | output text based on that state and the token's source text. Notably, |
| 65 | handle_token is not recursive, even though JSON values can nest. |
| 66 | |
| 67 | This approach is centered around JSON tokens. Each JSON 'thing' (e.g. number, |
| 68 | string, object) comprises one or more JSON tokens. |
| 69 | |
| 70 | An alternative, higher-level approach is in the sibling example/jsonfindptrs |
| 71 | program. Neither approach is better or worse per se, but when studying this |
| 72 | program, be aware that there are multiple ways to use Wuffs' JSON decoder. |
| 73 | |
| 74 | The two programs, jsonfindptrs and jsonptr, also demonstrate different |
| 75 | trade-offs with regard to JSON object duplicate keys. The JSON spec permits |
| 76 | different implementations to allow or reject duplicate keys. It is not always |
| 77 | clear which approach is safer. Rejecting them is certainly unambiguous, and |
| 78 | security bugs can lurk in ambiguous corners of a file format, if two different |
| 79 | implementations both silently accept a file but differ on how to interpret it. |
| 80 | On the other hand, in the worst case, detecting duplicate keys requires O(N) |
| 81 | memory, where N is the size of the (potentially untrusted) input. |
| 82 | |
| 83 | This program (jsonptr) allows duplicate keys and requires only O(1) memory. As |
| 84 | mentioned above, it doesn't dynamically allocate memory at all, and on Linux, |
| 85 | it runs in a SECCOMP_MODE_STRICT sandbox. |
| 86 | |
| 87 | ---- |
| 88 | |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 89 | This example program differs from most other example Wuffs programs in that it |
| 90 | is written in C++, not C. |
| 91 | |
| 92 | $CXX jsonptr.cc && ./a.out < ../../test/data/github-tags.json; rm -f a.out |
| 93 | |
| 94 | for a C++ compiler $CXX, such as clang++ or g++. |
| 95 | */ |
| 96 | |
Nigel Tao | 721190a | 2020-04-03 22:25:21 +1100 | [diff] [blame] | 97 | #if defined(__cplusplus) && (__cplusplus < 201103L) |
| 98 | #error "This C++ program requires -std=c++11 or later" |
| 99 | #endif |
| 100 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 101 | #include <errno.h> |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 102 | #include <fcntl.h> |
| 103 | #include <stdio.h> |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 104 | #include <string.h> |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 105 | #include <unistd.h> |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 106 | |
| 107 | // Wuffs ships as a "single file C library" or "header file library" as per |
| 108 | // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt |
| 109 | // |
| 110 | // To use that single file as a "foo.c"-like implementation, instead of a |
| 111 | // "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or |
| 112 | // compiling it. |
| 113 | #define WUFFS_IMPLEMENTATION |
| 114 | |
| 115 | // Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of |
| 116 | // release/c/etc.c whitelist which parts of Wuffs to build. That file contains |
| 117 | // the entire Wuffs standard library, implementing a variety of codecs and file |
| 118 | // formats. Without this macro definition, an optimizing compiler or linker may |
| 119 | // very well discard Wuffs code for unused codecs, but listing the Wuffs |
| 120 | // modules we use makes that process explicit. Preprocessing means that such |
| 121 | // code simply isn't compiled. |
| 122 | #define WUFFS_CONFIG__MODULES |
| 123 | #define WUFFS_CONFIG__MODULE__BASE |
| 124 | #define WUFFS_CONFIG__MODULE__JSON |
| 125 | |
| 126 | // If building this program in an environment that doesn't easily accommodate |
| 127 | // relative includes, you can use the script/inline-c-relative-includes.go |
| 128 | // program to generate a stand-alone C++ file. |
| 129 | #include "../../release/c/wuffs-unsupported-snapshot.c" |
| 130 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 131 | #if defined(__linux__) |
| 132 | #include <linux/prctl.h> |
| 133 | #include <linux/seccomp.h> |
| 134 | #include <sys/prctl.h> |
| 135 | #include <sys/syscall.h> |
| 136 | #define WUFFS_EXAMPLE_USE_SECCOMP |
| 137 | #endif |
| 138 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 139 | #define TRY(error_msg) \ |
| 140 | do { \ |
| 141 | const char* z = error_msg; \ |
| 142 | if (z) { \ |
| 143 | return z; \ |
| 144 | } \ |
| 145 | } while (false) |
| 146 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 147 | static const char* g_eod = "main: end of data"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 148 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 149 | static const char* g_usage = |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 150 | "Usage: jsonptr -flags input.json\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 151 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 152 | "Flags:\n" |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 153 | " -c -compact-output\n" |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 154 | " -d=NUM -max-output-depth=NUM\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 155 | " -i=NUM -indent=NUM\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 156 | " -q=STR -query=STR\n" |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 157 | " -s -strict-json-pointer-syntax\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 158 | " -t -tabs\n" |
| 159 | " -fail-if-unsandboxed\n" |
| 160 | "\n" |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 161 | "The input.json filename is optional. If absent, it reads from stdin.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 162 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 163 | "----\n" |
| 164 | "\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 165 | "jsonptr is a JSON formatter (pretty-printer) that supports the JSON\n" |
| 166 | "Pointer (RFC 6901) query syntax. It reads UTF-8 JSON from stdin and\n" |
| 167 | "writes canonicalized, formatted UTF-8 JSON to stdout.\n" |
| 168 | "\n" |
| 169 | "Canonicalized means that e.g. \"abc\\u000A\\tx\\u0177z\" is re-written\n" |
| 170 | "as \"abc\\n\\txÅ·z\". It does not sort object keys, nor does it reject\n" |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 171 | "duplicate keys. Canonicalization does not imply Unicode normalization.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 172 | "\n" |
| 173 | "Formatted means that arrays' and objects' elements are indented, each\n" |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 174 | "on its own line. Configure this with the -c / -compact-output, -i=NUM /\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 175 | "-indent=NUM (for NUM ranging from 0 to 8) and -t / -tabs flags.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 176 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 177 | "----\n" |
| 178 | "\n" |
| 179 | "The -q=STR or -query=STR flag gives an optional JSON Pointer query, to\n" |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 180 | "print a subset of the input. For example, given RFC 6901 section 5's\n" |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 181 | "sample input (https://tools.ietf.org/rfc/rfc6901.txt), this command:\n" |
| 182 | " jsonptr -query=/foo/1 rfc-6901-json-pointer.json\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 183 | "will print:\n" |
| 184 | " \"baz\"\n" |
| 185 | "\n" |
| 186 | "An absent query is equivalent to the empty query, which identifies the\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 187 | "entire input (the root value). Unlike a file system, the \"/\" query\n" |
Nigel Tao | d0b16cb | 2020-03-14 10:15:54 +1100 | [diff] [blame] | 188 | "does not identify the root. Instead, \"\" is the root and \"/\" is the\n" |
| 189 | "child (the value in a key-value pair) of the root whose key is the empty\n" |
| 190 | "string. Similarly, \"/xyz\" and \"/xyz/\" are two different nodes.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 191 | "\n" |
| 192 | "If the query found a valid JSON value, this program will return a zero\n" |
| 193 | "exit code even if the rest of the input isn't valid JSON. If the query\n" |
| 194 | "did not find a value, or found an invalid one, this program returns a\n" |
| 195 | "non-zero exit code, but may still print partial output to stdout.\n" |
| 196 | "\n" |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 197 | "The JSON specification (https://json.org/) permits implementations that\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 198 | "allow duplicate keys, as this one does. This JSON Pointer implementation\n" |
| 199 | "is also greedy, following the first match for each fragment without\n" |
| 200 | "back-tracking. For example, the \"/foo/bar\" query will fail if the root\n" |
| 201 | "object has multiple \"foo\" children but the first one doesn't have a\n" |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 202 | "\"bar\" child, even if later ones do.\n" |
| 203 | "\n" |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 204 | "The -s or -strict-json-pointer-syntax flag restricts the -query=STR\n" |
| 205 | "string to exactly RFC 6901, with only two escape sequences: \"~0\" and\n" |
| 206 | "\"~1\" for \"~\" and \"/\". Without this flag, this program also lets\n" |
| 207 | "\"~n\" and \"~r\" escape the New Line and Carriage Return ASCII control\n" |
| 208 | "characters, which can work better with line oriented Unix tools that\n" |
| 209 | "assume exactly one value (i.e. one JSON Pointer string) per line.\n" |
| 210 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 211 | "----\n" |
| 212 | "\n" |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 213 | "The -d=NUM or -max-output-depth=NUM flag gives the maximum (inclusive)\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 214 | "output depth. JSON containers ([] arrays and {} objects) can hold other\n" |
| 215 | "containers. When this flag is set, containers at depth NUM are replaced\n" |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 216 | "with \"[…]\" or \"{…}\". A bare -d or -max-output-depth is equivalent to\n" |
| 217 | "-d=1. The flag's absence is equivalent to an unlimited output depth.\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 218 | "\n" |
| 219 | "The -max-output-depth flag only affects the program's output. It doesn't\n" |
| 220 | "affect whether or not the input is considered valid JSON. The JSON\n" |
| 221 | "specification permits implementations to set their own maximum input\n" |
| 222 | "depth. This JSON implementation sets it to 1024.\n" |
| 223 | "\n" |
| 224 | "Depth is measured in terms of nested containers. It is unaffected by the\n" |
| 225 | "number of spaces or tabs used to indent.\n" |
| 226 | "\n" |
| 227 | "When both -max-output-depth and -query are set, the output depth is\n" |
| 228 | "measured from when the query resolves, not from the input root. The\n" |
| 229 | "input depth (measured from the root) is still limited to 1024.\n" |
| 230 | "\n" |
| 231 | "----\n" |
| 232 | "\n" |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 233 | "The -fail-if-unsandboxed flag causes the program to exit if it does not\n" |
| 234 | "self-impose a sandbox. On Linux, it self-imposes a SECCOMP_MODE_STRICT\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 235 | "sandbox, regardless of whether this flag was set."; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 236 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 237 | // ---- |
| 238 | |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 239 | // Wuffs allows either statically or dynamically allocated work buffers. This |
| 240 | // program exercises static allocation. |
| 241 | #define WORK_BUFFER_ARRAY_SIZE \ |
| 242 | WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE |
| 243 | #if WORK_BUFFER_ARRAY_SIZE > 0 |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 244 | uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE]; |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 245 | #else |
| 246 | // Not all C/C++ compilers support 0-length arrays. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 247 | uint8_t g_work_buffer_array[1]; |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 248 | #endif |
| 249 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 250 | bool g_sandboxed = false; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 251 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 252 | int g_input_file_descriptor = 0; // A 0 default means stdin. |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 253 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 254 | #define MAX_INDENT 8 |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 255 | #define INDENT_SPACES_STRING " " |
Nigel Tao | 6e7d141 | 2020-03-06 09:21:35 +1100 | [diff] [blame] | 256 | #define INDENT_TAB_STRING "\t" |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 257 | |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 258 | #ifndef DST_BUFFER_ARRAY_SIZE |
| 259 | #define DST_BUFFER_ARRAY_SIZE (32 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 260 | #endif |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 261 | #ifndef SRC_BUFFER_ARRAY_SIZE |
| 262 | #define SRC_BUFFER_ARRAY_SIZE (32 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 263 | #endif |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 264 | #ifndef TOKEN_BUFFER_ARRAY_SIZE |
| 265 | #define TOKEN_BUFFER_ARRAY_SIZE (4 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 266 | #endif |
| 267 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 268 | uint8_t g_dst_array[DST_BUFFER_ARRAY_SIZE]; |
| 269 | uint8_t g_src_array[SRC_BUFFER_ARRAY_SIZE]; |
| 270 | wuffs_base__token g_tok_array[TOKEN_BUFFER_ARRAY_SIZE]; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 271 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 272 | wuffs_base__io_buffer g_dst; |
| 273 | wuffs_base__io_buffer g_src; |
| 274 | wuffs_base__token_buffer g_tok; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 275 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 276 | // g_curr_token_end_src_index is the g_src.data.ptr index of the end of the |
| 277 | // current token. An invariant is that (g_curr_token_end_src_index <= |
| 278 | // g_src.meta.ri). |
| 279 | size_t g_curr_token_end_src_index; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 280 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 281 | uint32_t g_depth; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 282 | |
| 283 | enum class context { |
| 284 | none, |
| 285 | in_list_after_bracket, |
| 286 | in_list_after_value, |
| 287 | in_dict_after_brace, |
| 288 | in_dict_after_key, |
| 289 | in_dict_after_value, |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 290 | } g_ctx; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 291 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 292 | bool // |
| 293 | in_dict_before_key() { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 294 | return (g_ctx == context::in_dict_after_brace) || |
| 295 | (g_ctx == context::in_dict_after_value); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 296 | } |
| 297 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 298 | uint32_t g_suppress_write_dst; |
| 299 | bool g_wrote_to_dst; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 300 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 301 | wuffs_json__decoder g_dec; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 302 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 303 | // ---- |
| 304 | |
| 305 | // Query is a JSON Pointer query. After initializing with a NUL-terminated C |
| 306 | // string, its multiple fragments are consumed as the program walks the JSON |
| 307 | // data from stdin. For example, letting "$" denote a NUL, suppose that we |
| 308 | // started with a query string of "/apple/banana/12/durian" and are currently |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 309 | // trying to match the second fragment, "banana", so that Query::m_depth is 2: |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 310 | // |
| 311 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 312 | // / a p p l e / b a n a n a / 1 2 / d u r i a n $ |
| 313 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 314 | // ^ ^ |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 315 | // m_frag_i m_frag_k |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 316 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 317 | // The two pointers m_frag_i and m_frag_k (abbreviated as mfi and mfk) are the |
| 318 | // start (inclusive) and end (exclusive) of the query fragment. They satisfy |
| 319 | // (mfi <= mfk) and may be equal if the fragment empty (note that "" is a valid |
| 320 | // JSON object key). |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 321 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 322 | // The m_frag_j (mfj) pointer moves between these two, or is nullptr. An |
| 323 | // invariant is that (((mfi <= mfj) && (mfj <= mfk)) || (mfj == nullptr)). |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 324 | // |
| 325 | // Wuffs' JSON tokenizer can portray a single JSON string as multiple Wuffs |
| 326 | // tokens, as backslash-escaped values within that JSON string may each get |
| 327 | // their own token. |
| 328 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 329 | // At the start of each object key (a JSON string), mfj is set to mfi. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 330 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 331 | // While mfj remains non-nullptr, each token's unescaped contents are then |
| 332 | // compared to that part of the fragment from mfj to mfk. If it is a prefix |
| 333 | // (including the case of an exact match), then mfj is advanced by the |
| 334 | // unescaped length. Otherwise, mfj is set to nullptr. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 335 | // |
| 336 | // Comparison accounts for JSON Pointer's escaping notation: "~0" and "~1" in |
| 337 | // the query (not the JSON value) are unescaped to "~" and "/" respectively. |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 338 | // "~n" and "~r" are also unescaped to "\n" and "\r". The program is |
| 339 | // responsible for calling Query::validate (with a strict_json_pointer_syntax |
| 340 | // argument) before otherwise using this class. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 341 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 342 | // The mfj pointer therefore advances from mfi to mfk, or drops out, as we |
| 343 | // incrementally match the object key with the query fragment. For example, if |
| 344 | // we have already matched the "ban" of "banana", then we would accept any of |
| 345 | // an "ana" token, an "a" token or a "\u0061" token, amongst others. They would |
| 346 | // advance mfj by 3, 1 or 1 bytes respectively. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 347 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 348 | // mfj |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 349 | // v |
| 350 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 351 | // / a p p l e / b a n a n a / 1 2 / d u r i a n $ |
| 352 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 353 | // ^ ^ |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 354 | // mfi mfk |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 355 | // |
| 356 | // At the end of each object key (or equivalently, at the start of each object |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 357 | // value), if mfj is non-nullptr and equal to (but not less than) mfk then we |
| 358 | // have a fragment match: the query fragment equals the object key. If there is |
| 359 | // a next fragment (in this example, "12") we move the frag_etc pointers to its |
| 360 | // start and end and increment Query::m_depth. Otherwise, we have matched the |
| 361 | // complete query, and the upcoming JSON value is the result of that query. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 362 | // |
| 363 | // The discussion above centers on object keys. If the query fragment is |
| 364 | // numeric then it can also match as an array index: the string fragment "12" |
| 365 | // will match an array's 13th element (starting counting from zero). See RFC |
| 366 | // 6901 for its precise definition of an "array index" number. |
| 367 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 368 | // Array index fragment match is represented by the Query::m_array_index field, |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 369 | // whose type (wuffs_base__result_u64) is a result type. An error result means |
| 370 | // that the fragment is not an array index. A value result holds the number of |
| 371 | // list elements remaining. When matching a query fragment in an array (instead |
| 372 | // of in an object), each element ticks this number down towards zero. At zero, |
| 373 | // the upcoming JSON value is the one that matches the query fragment. |
| 374 | class Query { |
| 375 | private: |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 376 | uint8_t* m_frag_i; |
| 377 | uint8_t* m_frag_j; |
| 378 | uint8_t* m_frag_k; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 379 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 380 | uint32_t m_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 381 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 382 | wuffs_base__result_u64 m_array_index; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 383 | |
| 384 | public: |
| 385 | void reset(char* query_c_string) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 386 | m_frag_i = (uint8_t*)query_c_string; |
| 387 | m_frag_j = (uint8_t*)query_c_string; |
| 388 | m_frag_k = (uint8_t*)query_c_string; |
| 389 | m_depth = 0; |
| 390 | m_array_index.status.repr = "#main: not an array index query fragment"; |
| 391 | m_array_index.value = 0; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 392 | } |
| 393 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 394 | void restart_fragment(bool enable) { m_frag_j = enable ? m_frag_i : nullptr; } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 395 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 396 | bool is_at(uint32_t depth) { return m_depth == depth; } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 397 | |
| 398 | // tick returns whether the fragment is a valid array index whose value is |
| 399 | // zero. If valid but non-zero, it decrements it and returns false. |
| 400 | bool tick() { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 401 | if (m_array_index.status.is_ok()) { |
| 402 | if (m_array_index.value == 0) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 403 | return true; |
| 404 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 405 | m_array_index.value--; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 406 | } |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | // next_fragment moves to the next fragment, returning whether it existed. |
| 411 | bool next_fragment() { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 412 | uint8_t* k = m_frag_k; |
| 413 | uint32_t d = m_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 414 | |
| 415 | this->reset(nullptr); |
| 416 | |
| 417 | if (!k || (*k != '/')) { |
| 418 | return false; |
| 419 | } |
| 420 | k++; |
| 421 | |
| 422 | bool all_digits = true; |
| 423 | uint8_t* i = k; |
| 424 | while ((*k != '\x00') && (*k != '/')) { |
| 425 | all_digits = all_digits && ('0' <= *k) && (*k <= '9'); |
| 426 | k++; |
| 427 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 428 | m_frag_i = i; |
| 429 | m_frag_j = i; |
| 430 | m_frag_k = k; |
| 431 | m_depth = d + 1; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 432 | if (all_digits) { |
| 433 | // wuffs_base__parse_number_u64 rejects leading zeroes, e.g. "00", "07". |
Nigel Tao | 6b7ce30 | 2020-07-07 16:19:46 +1000 | [diff] [blame] | 434 | m_array_index = wuffs_base__parse_number_u64( |
| 435 | wuffs_base__make_slice_u8(i, k - i), |
| 436 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 437 | } |
| 438 | return true; |
| 439 | } |
| 440 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 441 | bool matched_all() { return m_frag_k == nullptr; } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 442 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 443 | bool matched_fragment() { return m_frag_j && (m_frag_j == m_frag_k); } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 444 | |
| 445 | void incremental_match_slice(uint8_t* ptr, size_t len) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 446 | if (!m_frag_j) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 447 | return; |
| 448 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 449 | uint8_t* j = m_frag_j; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 450 | while (true) { |
| 451 | if (len == 0) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 452 | m_frag_j = j; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
| 456 | if (*j == '\x00') { |
| 457 | break; |
| 458 | |
| 459 | } else if (*j == '~') { |
| 460 | j++; |
| 461 | if (*j == '0') { |
| 462 | if (*ptr != '~') { |
| 463 | break; |
| 464 | } |
| 465 | } else if (*j == '1') { |
| 466 | if (*ptr != '/') { |
| 467 | break; |
| 468 | } |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 469 | } else if (*j == 'n') { |
| 470 | if (*ptr != '\n') { |
| 471 | break; |
| 472 | } |
| 473 | } else if (*j == 'r') { |
| 474 | if (*ptr != '\r') { |
| 475 | break; |
| 476 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 477 | } else { |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | } else if (*j != *ptr) { |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | j++; |
| 486 | ptr++; |
| 487 | len--; |
| 488 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 489 | m_frag_j = nullptr; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void incremental_match_code_point(uint32_t code_point) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 493 | if (!m_frag_j) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 497 | size_t n = wuffs_base__utf_8__encode( |
| 498 | wuffs_base__make_slice_u8(&u[0], |
| 499 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 500 | code_point); |
| 501 | if (n > 0) { |
| 502 | this->incremental_match_slice(&u[0], n); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // validate returns whether the (ptr, len) arguments form a valid JSON |
| 507 | // Pointer. In particular, it must be valid UTF-8, and either be empty or |
| 508 | // start with a '/'. Any '~' within must immediately be followed by either |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 509 | // '0' or '1'. If strict_json_pointer_syntax is false, a '~' may also be |
| 510 | // followed by either 'n' or 'r'. |
| 511 | static bool validate(char* query_c_string, |
| 512 | size_t length, |
| 513 | bool strict_json_pointer_syntax) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 514 | if (length <= 0) { |
| 515 | return true; |
| 516 | } |
| 517 | if (query_c_string[0] != '/') { |
| 518 | return false; |
| 519 | } |
| 520 | wuffs_base__slice_u8 s = |
| 521 | wuffs_base__make_slice_u8((uint8_t*)query_c_string, length); |
| 522 | bool previous_was_tilde = false; |
| 523 | while (s.len > 0) { |
| 524 | wuffs_base__utf_8__next__output o = wuffs_base__utf_8__next(s); |
| 525 | if (!o.is_valid()) { |
| 526 | return false; |
| 527 | } |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 528 | |
| 529 | if (previous_was_tilde) { |
| 530 | switch (o.code_point) { |
| 531 | case '0': |
| 532 | case '1': |
| 533 | break; |
| 534 | case 'n': |
| 535 | case 'r': |
| 536 | if (strict_json_pointer_syntax) { |
| 537 | return false; |
| 538 | } |
| 539 | break; |
| 540 | default: |
| 541 | return false; |
| 542 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 543 | } |
| 544 | previous_was_tilde = o.code_point == '~'; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 545 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 546 | s.ptr += o.byte_length; |
| 547 | s.len -= o.byte_length; |
| 548 | } |
| 549 | return !previous_was_tilde; |
| 550 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 551 | } g_query; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 552 | |
| 553 | // ---- |
| 554 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 555 | struct { |
| 556 | int remaining_argc; |
| 557 | char** remaining_argv; |
| 558 | |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 559 | bool compact_output; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 560 | bool fail_if_unsandboxed; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 561 | size_t indent; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 562 | uint32_t max_output_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 563 | char* query_c_string; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 564 | bool strict_json_pointer_syntax; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 565 | bool tabs; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 566 | } g_flags = {0}; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 567 | |
| 568 | const char* // |
| 569 | parse_flags(int argc, char** argv) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 570 | g_flags.indent = 4; |
| 571 | g_flags.max_output_depth = 0xFFFFFFFF; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 572 | |
| 573 | int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name. |
| 574 | for (; c < argc; c++) { |
| 575 | char* arg = argv[c]; |
| 576 | if (*arg++ != '-') { |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | // A double-dash "--foo" is equivalent to a single-dash "-foo". As special |
| 581 | // cases, a bare "-" is not a flag (some programs may interpret it as |
| 582 | // stdin) and a bare "--" means to stop parsing flags. |
| 583 | if (*arg == '\x00') { |
| 584 | break; |
| 585 | } else if (*arg == '-') { |
| 586 | arg++; |
| 587 | if (*arg == '\x00') { |
| 588 | c++; |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 593 | if (!strcmp(arg, "c") || !strcmp(arg, "compact-output")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 594 | g_flags.compact_output = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 595 | continue; |
| 596 | } |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 597 | if (!strcmp(arg, "d") || !strcmp(arg, "max-output-depth")) { |
| 598 | g_flags.max_output_depth = 1; |
| 599 | continue; |
| 600 | } else if (!strncmp(arg, "d=", 2) || |
| 601 | !strncmp(arg, "max-output-depth=", 16)) { |
| 602 | while (*arg++ != '=') { |
| 603 | } |
| 604 | wuffs_base__result_u64 u = wuffs_base__parse_number_u64( |
Nigel Tao | 6b7ce30 | 2020-07-07 16:19:46 +1000 | [diff] [blame] | 605 | wuffs_base__make_slice_u8((uint8_t*)arg, strlen(arg)), |
| 606 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 607 | if (wuffs_base__status__is_ok(&u.status) && (u.value <= 0xFFFFFFFF)) { |
| 608 | g_flags.max_output_depth = (uint32_t)(u.value); |
| 609 | continue; |
| 610 | } |
| 611 | return g_usage; |
| 612 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 613 | if (!strcmp(arg, "fail-if-unsandboxed")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 614 | g_flags.fail_if_unsandboxed = true; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 615 | continue; |
| 616 | } |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 617 | if (!strncmp(arg, "i=", 2) || !strncmp(arg, "indent=", 7)) { |
| 618 | while (*arg++ != '=') { |
| 619 | } |
| 620 | if (('0' <= arg[0]) && (arg[0] <= '8') && (arg[1] == '\x00')) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 621 | g_flags.indent = arg[0] - '0'; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 622 | continue; |
| 623 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 624 | return g_usage; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 625 | } |
| 626 | if (!strncmp(arg, "q=", 2) || !strncmp(arg, "query=", 6)) { |
| 627 | while (*arg++ != '=') { |
| 628 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 629 | g_flags.query_c_string = arg; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 630 | continue; |
| 631 | } |
| 632 | if (!strcmp(arg, "s") || !strcmp(arg, "strict-json-pointer-syntax")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 633 | g_flags.strict_json_pointer_syntax = true; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 634 | continue; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 635 | } |
| 636 | if (!strcmp(arg, "t") || !strcmp(arg, "tabs")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 637 | g_flags.tabs = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 638 | continue; |
| 639 | } |
| 640 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 641 | return g_usage; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 642 | } |
| 643 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 644 | if (g_flags.query_c_string && |
| 645 | !Query::validate(g_flags.query_c_string, strlen(g_flags.query_c_string), |
| 646 | g_flags.strict_json_pointer_syntax)) { |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 647 | return "main: bad JSON Pointer (RFC 6901) syntax for the -query=STR flag"; |
| 648 | } |
| 649 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 650 | g_flags.remaining_argc = argc - c; |
| 651 | g_flags.remaining_argv = argv + c; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 652 | return nullptr; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 653 | } |
| 654 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 655 | const char* // |
| 656 | initialize_globals(int argc, char** argv) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 657 | g_dst = wuffs_base__make_io_buffer( |
| 658 | wuffs_base__make_slice_u8(g_dst_array, DST_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 659 | wuffs_base__empty_io_buffer_meta()); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 660 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 661 | g_src = wuffs_base__make_io_buffer( |
| 662 | wuffs_base__make_slice_u8(g_src_array, SRC_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 663 | wuffs_base__empty_io_buffer_meta()); |
| 664 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 665 | g_tok = wuffs_base__make_token_buffer( |
| 666 | wuffs_base__make_slice_token(g_tok_array, TOKEN_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 667 | wuffs_base__empty_token_buffer_meta()); |
| 668 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 669 | g_curr_token_end_src_index = 0; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 670 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 671 | g_depth = 0; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 672 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 673 | g_ctx = context::none; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 674 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 675 | TRY(parse_flags(argc, argv)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 676 | if (g_flags.fail_if_unsandboxed && !g_sandboxed) { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 677 | return "main: unsandboxed"; |
| 678 | } |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 679 | const int stdin_fd = 0; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 680 | if (g_flags.remaining_argc > |
| 681 | ((g_input_file_descriptor != stdin_fd) ? 1 : 0)) { |
| 682 | return g_usage; |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 683 | } |
| 684 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 685 | g_query.reset(g_flags.query_c_string); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 686 | |
| 687 | // If the query is non-empty, suprress writing to stdout until we've |
| 688 | // completed the query. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 689 | g_suppress_write_dst = g_query.next_fragment() ? 1 : 0; |
| 690 | g_wrote_to_dst = false; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 691 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 692 | TRY(g_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0) |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 693 | .message()); |
| 694 | |
| 695 | // Consume an optional whitespace trailer. This isn't part of the JSON spec, |
| 696 | // but it works better with line oriented Unix tools (such as "echo 123 | |
| 697 | // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which |
| 698 | // can accidentally contain trailing whitespace. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 699 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_TRAILING_NEW_LINE, true); |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 700 | |
| 701 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 702 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 703 | |
| 704 | // ---- |
| 705 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 706 | // ignore_return_value suppresses errors from -Wall -Werror. |
| 707 | static void // |
| 708 | ignore_return_value(int ignored) {} |
| 709 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 710 | const char* // |
| 711 | read_src() { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 712 | if (g_src.meta.closed) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 713 | return "main: internal error: read requested on a closed source"; |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 714 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 715 | g_src.compact(); |
| 716 | if (g_src.meta.wi >= g_src.data.len) { |
| 717 | return "main: g_src buffer is full"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 718 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 719 | while (true) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 720 | ssize_t n = read(g_input_file_descriptor, g_src.data.ptr + g_src.meta.wi, |
| 721 | g_src.data.len - g_src.meta.wi); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 722 | if (n >= 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 723 | g_src.meta.wi += n; |
| 724 | g_src.meta.closed = n == 0; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 725 | break; |
| 726 | } else if (errno != EINTR) { |
| 727 | return strerror(errno); |
| 728 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 729 | } |
| 730 | return nullptr; |
| 731 | } |
| 732 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 733 | const char* // |
| 734 | flush_dst() { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 735 | while (true) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 736 | size_t n = g_dst.meta.wi - g_dst.meta.ri; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 737 | if (n == 0) { |
| 738 | break; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 739 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 740 | const int stdout_fd = 1; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 741 | ssize_t i = write(stdout_fd, g_dst.data.ptr + g_dst.meta.ri, n); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 742 | if (i >= 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 743 | g_dst.meta.ri += i; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 744 | } else if (errno != EINTR) { |
| 745 | return strerror(errno); |
| 746 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 747 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 748 | g_dst.compact(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 749 | return nullptr; |
| 750 | } |
| 751 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 752 | const char* // |
| 753 | write_dst(const void* s, size_t n) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 754 | if (g_suppress_write_dst > 0) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 755 | return nullptr; |
| 756 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 757 | const uint8_t* p = static_cast<const uint8_t*>(s); |
| 758 | while (n > 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 759 | size_t i = g_dst.writer_available(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 760 | if (i == 0) { |
| 761 | const char* z = flush_dst(); |
| 762 | if (z) { |
| 763 | return z; |
| 764 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 765 | i = g_dst.writer_available(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 766 | if (i == 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 767 | return "main: g_dst buffer is full"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
| 771 | if (i > n) { |
| 772 | i = n; |
| 773 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 774 | memcpy(g_dst.data.ptr + g_dst.meta.wi, p, i); |
| 775 | g_dst.meta.wi += i; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 776 | p += i; |
| 777 | n -= i; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 778 | g_wrote_to_dst = true; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 779 | } |
| 780 | return nullptr; |
| 781 | } |
| 782 | |
| 783 | // ---- |
| 784 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 785 | uint8_t // |
| 786 | hex_digit(uint8_t nibble) { |
Nigel Tao | b5461bd | 2020-02-21 14:13:37 +1100 | [diff] [blame] | 787 | nibble &= 0x0F; |
| 788 | if (nibble <= 9) { |
| 789 | return '0' + nibble; |
| 790 | } |
| 791 | return ('A' - 10) + nibble; |
| 792 | } |
| 793 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 794 | const char* // |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 795 | handle_unicode_code_point(uint32_t ucp) { |
| 796 | if (ucp < 0x0020) { |
| 797 | switch (ucp) { |
| 798 | case '\b': |
| 799 | return write_dst("\\b", 2); |
| 800 | case '\f': |
| 801 | return write_dst("\\f", 2); |
| 802 | case '\n': |
| 803 | return write_dst("\\n", 2); |
| 804 | case '\r': |
| 805 | return write_dst("\\r", 2); |
| 806 | case '\t': |
| 807 | return write_dst("\\t", 2); |
| 808 | default: { |
| 809 | // Other bytes less than 0x0020 are valid UTF-8 but not valid in a |
| 810 | // JSON string. They need to remain escaped. |
| 811 | uint8_t esc6[6]; |
| 812 | esc6[0] = '\\'; |
| 813 | esc6[1] = 'u'; |
| 814 | esc6[2] = '0'; |
| 815 | esc6[3] = '0'; |
| 816 | esc6[4] = hex_digit(ucp >> 4); |
| 817 | esc6[5] = hex_digit(ucp >> 0); |
| 818 | return write_dst(&esc6[0], 6); |
| 819 | } |
| 820 | } |
| 821 | |
Nigel Tao | b9ad34f | 2020-03-03 12:44:01 +1100 | [diff] [blame] | 822 | } else if (ucp == '\"') { |
| 823 | return write_dst("\\\"", 2); |
| 824 | |
| 825 | } else if (ucp == '\\') { |
| 826 | return write_dst("\\\\", 2); |
| 827 | |
| 828 | } else { |
| 829 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 830 | size_t n = wuffs_base__utf_8__encode( |
| 831 | wuffs_base__make_slice_u8(&u[0], |
| 832 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 833 | ucp); |
| 834 | if (n > 0) { |
| 835 | return write_dst(&u[0], n); |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 836 | } |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 837 | } |
| 838 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 839 | return "main: internal error: unexpected Unicode code point"; |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | const char* // |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 843 | handle_token(wuffs_base__token t, bool start_of_token_chain) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 844 | do { |
Nigel Tao | 462f866 | 2020-04-01 23:01:51 +1100 | [diff] [blame] | 845 | int64_t vbc = t.value_base_category(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 846 | uint64_t vbd = t.value_base_detail(); |
| 847 | uint64_t len = t.length(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 848 | |
| 849 | // Handle ']' or '}'. |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 850 | if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) && |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 851 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 852 | if (g_query.is_at(g_depth)) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 853 | return "main: no match for query"; |
| 854 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 855 | if (g_depth <= 0) { |
| 856 | return "main: internal error: inconsistent g_depth"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 857 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 858 | g_depth--; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 859 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 860 | if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) { |
| 861 | g_suppress_write_dst--; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 862 | // '…' is U+2026 HORIZONTAL ELLIPSIS, which is 3 UTF-8 bytes. |
| 863 | TRY(write_dst((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) |
| 864 | ? "\"[…]\"" |
| 865 | : "\"{…}\"", |
| 866 | 7)); |
| 867 | } else { |
| 868 | // Write preceding whitespace. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 869 | if ((g_ctx != context::in_list_after_bracket) && |
| 870 | (g_ctx != context::in_dict_after_brace) && |
| 871 | !g_flags.compact_output) { |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 872 | TRY(write_dst("\n", 1)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 873 | for (uint32_t i = 0; i < g_depth; i++) { |
| 874 | TRY(write_dst( |
| 875 | g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING, |
| 876 | g_flags.tabs ? 1 : g_flags.indent)); |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 877 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 878 | } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 879 | |
| 880 | TRY(write_dst( |
| 881 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", |
| 882 | 1)); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 883 | } |
| 884 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 885 | g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 886 | ? context::in_list_after_value |
| 887 | : context::in_dict_after_key; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 888 | goto after_value; |
| 889 | } |
| 890 | |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 891 | // Write preceding whitespace and punctuation, if it wasn't ']', '}' or a |
| 892 | // continuation of a multi-token chain. |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 893 | if (start_of_token_chain) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 894 | if (g_ctx == context::in_dict_after_key) { |
| 895 | TRY(write_dst(": ", g_flags.compact_output ? 1 : 2)); |
| 896 | } else if (g_ctx != context::none) { |
| 897 | if ((g_ctx != context::in_list_after_bracket) && |
| 898 | (g_ctx != context::in_dict_after_brace)) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 899 | TRY(write_dst(",", 1)); |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 900 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 901 | if (!g_flags.compact_output) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 902 | TRY(write_dst("\n", 1)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 903 | for (size_t i = 0; i < g_depth; i++) { |
| 904 | TRY(write_dst( |
| 905 | g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING, |
| 906 | g_flags.tabs ? 1 : g_flags.indent)); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 911 | bool query_matched_fragment = false; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 912 | if (g_query.is_at(g_depth)) { |
| 913 | switch (g_ctx) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 914 | case context::in_list_after_bracket: |
| 915 | case context::in_list_after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 916 | query_matched_fragment = g_query.tick(); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 917 | break; |
| 918 | case context::in_dict_after_key: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 919 | query_matched_fragment = g_query.matched_fragment(); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 920 | break; |
Nigel Tao | 18ef5b4 | 2020-03-16 10:37:47 +1100 | [diff] [blame] | 921 | default: |
| 922 | break; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 923 | } |
| 924 | } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 925 | if (!query_matched_fragment) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 926 | // No-op. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 927 | } else if (!g_query.next_fragment()) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 928 | // There is no next fragment. We have matched the complete query, and |
| 929 | // the upcoming JSON value is the result of that query. |
| 930 | // |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 931 | // Un-suppress writing to stdout and reset the g_ctx and g_depth as if |
| 932 | // we were about to decode a top-level value. This makes any subsequent |
| 933 | // indentation be relative to this point, and we will return g_eod |
| 934 | // after the upcoming JSON value is complete. |
| 935 | if (g_suppress_write_dst != 1) { |
| 936 | return "main: internal error: inconsistent g_suppress_write_dst"; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 937 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 938 | g_suppress_write_dst = 0; |
| 939 | g_ctx = context::none; |
| 940 | g_depth = 0; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 941 | } else if ((vbc != WUFFS_BASE__TOKEN__VBC__STRUCTURE) || |
| 942 | !(vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH)) { |
| 943 | // The query has moved on to the next fragment but the upcoming JSON |
| 944 | // value is not a container. |
| 945 | return "main: no match for query"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
| 949 | // Handle the token itself: either a container ('[' or '{') or a simple |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 950 | // value: string (a chain of raw or escaped parts), literal or number. |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 951 | switch (vbc) { |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 952 | case WUFFS_BASE__TOKEN__VBC__STRUCTURE: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 953 | if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) { |
| 954 | g_suppress_write_dst++; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 955 | } else { |
| 956 | TRY(write_dst( |
| 957 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", |
| 958 | 1)); |
| 959 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 960 | g_depth++; |
| 961 | g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 962 | ? context::in_list_after_bracket |
| 963 | : context::in_dict_after_brace; |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 964 | return nullptr; |
| 965 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 966 | case WUFFS_BASE__TOKEN__VBC__STRING: |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 967 | if (start_of_token_chain) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 968 | TRY(write_dst("\"", 1)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 969 | g_query.restart_fragment(in_dict_before_key() && |
| 970 | g_query.is_at(g_depth)); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 971 | } |
Nigel Tao | cb37a56 | 2020-02-28 09:56:24 +1100 | [diff] [blame] | 972 | |
| 973 | if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) { |
| 974 | // No-op. |
| 975 | } else if (vbd & |
| 976 | WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 977 | uint8_t* ptr = g_src.data.ptr + g_curr_token_end_src_index - len; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 978 | TRY(write_dst(ptr, len)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 979 | g_query.incremental_match_slice(ptr, len); |
Nigel Tao | cb37a56 | 2020-02-28 09:56:24 +1100 | [diff] [blame] | 980 | } else { |
| 981 | return "main: internal error: unexpected string-token conversion"; |
| 982 | } |
| 983 | |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 984 | if (t.continued()) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 985 | return nullptr; |
| 986 | } |
| 987 | TRY(write_dst("\"", 1)); |
| 988 | goto after_value; |
| 989 | |
| 990 | case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 991 | if (!t.continued()) { |
| 992 | return "main: internal error: unexpected non-continued UCP token"; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 993 | } |
| 994 | TRY(handle_unicode_code_point(vbd)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 995 | g_query.incremental_match_code_point(vbd); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 996 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 997 | |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 998 | case WUFFS_BASE__TOKEN__VBC__LITERAL: |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 999 | case WUFFS_BASE__TOKEN__VBC__NUMBER: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1000 | TRY(write_dst(g_src.data.ptr + g_curr_token_end_src_index - len, len)); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1001 | goto after_value; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | // Return an error if we didn't match the (vbc, vbd) pair. |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1005 | return "main: internal error: unexpected token"; |
| 1006 | } while (0); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1007 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1008 | // Book-keeping after completing a value (whether a container value or a |
| 1009 | // simple value). Empty parent containers are no longer empty. If the parent |
| 1010 | // container is a "{...}" object, toggle between keys and values. |
| 1011 | after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1012 | if (g_depth == 0) { |
| 1013 | return g_eod; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1014 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1015 | switch (g_ctx) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1016 | case context::in_list_after_bracket: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1017 | g_ctx = context::in_list_after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1018 | break; |
| 1019 | case context::in_dict_after_brace: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1020 | g_ctx = context::in_dict_after_key; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1021 | break; |
| 1022 | case context::in_dict_after_key: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1023 | g_ctx = context::in_dict_after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1024 | break; |
| 1025 | case context::in_dict_after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1026 | g_ctx = context::in_dict_after_key; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1027 | break; |
Nigel Tao | 18ef5b4 | 2020-03-16 10:37:47 +1100 | [diff] [blame] | 1028 | default: |
| 1029 | break; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1030 | } |
| 1031 | return nullptr; |
| 1032 | } |
| 1033 | |
| 1034 | const char* // |
| 1035 | main1(int argc, char** argv) { |
| 1036 | TRY(initialize_globals(argc, argv)); |
| 1037 | |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 1038 | bool start_of_token_chain = false; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1039 | while (true) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1040 | wuffs_base__status status = g_dec.decode_tokens( |
| 1041 | &g_tok, &g_src, |
| 1042 | wuffs_base__make_slice_u8(g_work_buffer_array, WORK_BUFFER_ARRAY_SIZE)); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1043 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1044 | while (g_tok.meta.ri < g_tok.meta.wi) { |
| 1045 | wuffs_base__token t = g_tok.data.ptr[g_tok.meta.ri++]; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1046 | uint64_t n = t.length(); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1047 | if ((g_src.meta.ri - g_curr_token_end_src_index) < n) { |
| 1048 | return "main: internal error: inconsistent g_src indexes"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1049 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1050 | g_curr_token_end_src_index += n; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1051 | |
Nigel Tao | d0b16cb | 2020-03-14 10:15:54 +1100 | [diff] [blame] | 1052 | // Skip filler tokens (e.g. whitespace). |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1053 | if (t.value() == 0) { |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1054 | start_of_token_chain = !t.continued(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1055 | continue; |
| 1056 | } |
| 1057 | |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 1058 | const char* z = handle_token(t, start_of_token_chain); |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1059 | start_of_token_chain = !t.continued(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1060 | if (z == nullptr) { |
| 1061 | continue; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1062 | } else if (z == g_eod) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1063 | goto end_of_data; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1064 | } |
| 1065 | return z; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1066 | } |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1067 | |
| 1068 | if (status.repr == nullptr) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1069 | return "main: internal error: unexpected end of token stream"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1070 | } else if (status.repr == wuffs_base__suspension__short_read) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1071 | if (g_curr_token_end_src_index != g_src.meta.ri) { |
| 1072 | return "main: internal error: inconsistent g_src indexes"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1073 | } |
| 1074 | TRY(read_src()); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1075 | g_curr_token_end_src_index = g_src.meta.ri; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1076 | } else if (status.repr == wuffs_base__suspension__short_write) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1077 | g_tok.compact(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1078 | } else { |
| 1079 | return status.message(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1080 | } |
| 1081 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1082 | end_of_data: |
| 1083 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1084 | // With a non-empty g_query, don't try to consume trailing whitespace or |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1085 | // confirm that we've processed all the tokens. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1086 | if (g_flags.query_c_string && *g_flags.query_c_string) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1087 | return nullptr; |
| 1088 | } |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1089 | |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1090 | // Check that we've exhausted the input. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1091 | if ((g_src.meta.ri == g_src.meta.wi) && !g_src.meta.closed) { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1092 | TRY(read_src()); |
| 1093 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1094 | if ((g_src.meta.ri < g_src.meta.wi) || !g_src.meta.closed) { |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1095 | return "main: valid JSON followed by further (unexpected) data"; |
| 1096 | } |
| 1097 | |
| 1098 | // Check that we've used all of the decoded tokens, other than trailing |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 1099 | // filler tokens. For example, "true\n" is valid JSON (and fully consumed |
| 1100 | // with WUFFS_JSON__QUIRK_ALLOW_TRAILING_NEW_LINE enabled) with a trailing |
| 1101 | // filler token for the "\n". |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1102 | for (; g_tok.meta.ri < g_tok.meta.wi; g_tok.meta.ri++) { |
| 1103 | if (g_tok.data.ptr[g_tok.meta.ri].value_base_category() != |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1104 | WUFFS_BASE__TOKEN__VBC__FILLER) { |
| 1105 | return "main: internal error: decoded OK but unprocessed tokens remain"; |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | return nullptr; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1110 | } |
| 1111 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1112 | int // |
| 1113 | compute_exit_code(const char* status_msg) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1114 | if (!status_msg) { |
| 1115 | return 0; |
| 1116 | } |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1117 | size_t n; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1118 | if (status_msg == g_usage) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1119 | n = strlen(status_msg); |
| 1120 | } else { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1121 | n = strnlen(status_msg, 2047); |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1122 | if (n >= 2047) { |
| 1123 | status_msg = "main: internal error: error message is too long"; |
| 1124 | n = strnlen(status_msg, 2047); |
| 1125 | } |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1126 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1127 | const int stderr_fd = 2; |
| 1128 | ignore_return_value(write(stderr_fd, status_msg, n)); |
| 1129 | ignore_return_value(write(stderr_fd, "\n", 1)); |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1130 | // Return an exit code of 1 for regular (forseen) errors, e.g. badly |
| 1131 | // formatted or unsupported input. |
| 1132 | // |
| 1133 | // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive |
| 1134 | // run-time checks found that an internal invariant did not hold. |
| 1135 | // |
| 1136 | // Automated testing, including badly formatted inputs, can therefore |
| 1137 | // discriminate between expected failure (exit code 1) and unexpected failure |
| 1138 | // (other non-zero exit codes). Specifically, exit code 2 for internal |
| 1139 | // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 |
| 1140 | // linux) for a segmentation fault (e.g. null pointer dereference). |
| 1141 | return strstr(status_msg, "internal error:") ? 2 : 1; |
| 1142 | } |
| 1143 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1144 | int // |
| 1145 | main(int argc, char** argv) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1146 | // Look for an input filename (the first non-flag argument) in argv. If there |
| 1147 | // is one, open it (but do not read from it) before we self-impose a sandbox. |
| 1148 | // |
| 1149 | // Flags start with "-", unless it comes after a bare "--" arg. |
| 1150 | { |
| 1151 | bool dash_dash = false; |
| 1152 | int a; |
| 1153 | for (a = 1; a < argc; a++) { |
| 1154 | char* arg = argv[a]; |
| 1155 | if ((arg[0] == '-') && !dash_dash) { |
| 1156 | dash_dash = (arg[1] == '-') && (arg[2] == '\x00'); |
| 1157 | continue; |
| 1158 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1159 | g_input_file_descriptor = open(arg, O_RDONLY); |
| 1160 | if (g_input_file_descriptor < 0) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1161 | fprintf(stderr, "%s: %s\n", arg, strerror(errno)); |
| 1162 | return 1; |
| 1163 | } |
| 1164 | break; |
| 1165 | } |
| 1166 | } |
| 1167 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1168 | #if defined(WUFFS_EXAMPLE_USE_SECCOMP) |
| 1169 | prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1170 | g_sandboxed = true; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1171 | #endif |
| 1172 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1173 | const char* z = main1(argc, argv); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1174 | if (g_wrote_to_dst) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1175 | const char* z1 = write_dst("\n", 1); |
| 1176 | const char* z2 = flush_dst(); |
| 1177 | z = z ? z : (z1 ? z1 : z2); |
| 1178 | } |
| 1179 | int exit_code = compute_exit_code(z); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1180 | |
| 1181 | #if defined(WUFFS_EXAMPLE_USE_SECCOMP) |
| 1182 | // Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by |
| 1183 | // either calling _exit or returning from main. SECCOMP_MODE_STRICT allows |
| 1184 | // only SYS_exit. |
| 1185 | syscall(SYS_exit, exit_code); |
| 1186 | #endif |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1187 | return exit_code; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1188 | } |