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 |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 19 | (RFC 6901) query syntax. It reads UTF-8 JSON from stdin and writes |
| 20 | canonicalized, formatted UTF-8 JSON to stdout. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 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 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 30 | One benefit of simplicity is that this program's JSON and JSON Pointer |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 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 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 36 | The core JSON implementation is also written in the Wuffs programming language |
| 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 | 0291a47 | 2020-08-13 22:40:10 +1000 | [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. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 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 | 50bfab9 | 2020-08-05 11:39:09 +1000 | [diff] [blame] | 89 | To run: |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 90 | |
| 91 | $CXX jsonptr.cc && ./a.out < ../../test/data/github-tags.json; rm -f a.out |
| 92 | |
| 93 | for a C++ compiler $CXX, such as clang++ or g++. |
| 94 | */ |
| 95 | |
Nigel Tao | 721190a | 2020-04-03 22:25:21 +1100 | [diff] [blame] | 96 | #if defined(__cplusplus) && (__cplusplus < 201103L) |
| 97 | #error "This C++ program requires -std=c++11 or later" |
| 98 | #endif |
| 99 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 100 | #include <errno.h> |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 101 | #include <fcntl.h> |
| 102 | #include <stdio.h> |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 103 | #include <string.h> |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 104 | #include <unistd.h> |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 105 | |
| 106 | // Wuffs ships as a "single file C library" or "header file library" as per |
| 107 | // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt |
| 108 | // |
| 109 | // To use that single file as a "foo.c"-like implementation, instead of a |
| 110 | // "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or |
| 111 | // compiling it. |
| 112 | #define WUFFS_IMPLEMENTATION |
| 113 | |
| 114 | // Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of |
| 115 | // release/c/etc.c whitelist which parts of Wuffs to build. That file contains |
| 116 | // the entire Wuffs standard library, implementing a variety of codecs and file |
| 117 | // formats. Without this macro definition, an optimizing compiler or linker may |
| 118 | // very well discard Wuffs code for unused codecs, but listing the Wuffs |
| 119 | // modules we use makes that process explicit. Preprocessing means that such |
| 120 | // code simply isn't compiled. |
| 121 | #define WUFFS_CONFIG__MODULES |
| 122 | #define WUFFS_CONFIG__MODULE__BASE |
| 123 | #define WUFFS_CONFIG__MODULE__JSON |
| 124 | |
| 125 | // If building this program in an environment that doesn't easily accommodate |
| 126 | // relative includes, you can use the script/inline-c-relative-includes.go |
| 127 | // program to generate a stand-alone C++ file. |
| 128 | #include "../../release/c/wuffs-unsupported-snapshot.c" |
| 129 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 130 | #if defined(__linux__) |
| 131 | #include <linux/prctl.h> |
| 132 | #include <linux/seccomp.h> |
| 133 | #include <sys/prctl.h> |
| 134 | #include <sys/syscall.h> |
| 135 | #define WUFFS_EXAMPLE_USE_SECCOMP |
| 136 | #endif |
| 137 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 138 | #define TRY(error_msg) \ |
| 139 | do { \ |
| 140 | const char* z = error_msg; \ |
| 141 | if (z) { \ |
| 142 | return z; \ |
| 143 | } \ |
| 144 | } while (false) |
| 145 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 146 | static const char* g_eod = "main: end of data"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 147 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 148 | static const char* g_usage = |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 149 | "Usage: jsonptr -flags input.json\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 150 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 151 | "Flags:\n" |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 152 | " -c -compact-output\n" |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 153 | " -d=NUM -max-output-depth=NUM\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 154 | " -q=STR -query=STR\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 155 | " -s=NUM -spaces=NUM\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 156 | " -t -tabs\n" |
| 157 | " -fail-if-unsandboxed\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 158 | " -input-allow-comments\n" |
| 159 | " -input-allow-extra-comma\n" |
| 160 | " -input-allow-inf-nan-numbers\n" |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 161 | " -jwcc\n" |
| 162 | " -output-comments\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 163 | " -output-extra-comma\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 164 | " -strict-json-pointer-syntax\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 165 | "\n" |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 166 | "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] | 167 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 168 | "----\n" |
| 169 | "\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 170 | "jsonptr is a JSON formatter (pretty-printer) that supports the JSON\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 171 | "Pointer (RFC 6901) query syntax. It reads UTF-8 JSON from stdin and\n" |
| 172 | "writes canonicalized, formatted UTF-8 JSON to stdout.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 173 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 174 | "Canonicalized means that e.g. \"abc\\u000A\\tx\\u0177z\" is re-written\n" |
| 175 | "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] | 176 | "duplicate keys. Canonicalization does not imply Unicode normalization.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 177 | "\n" |
| 178 | "Formatted means that arrays' and objects' elements are indented, each\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 179 | "on its own line. Configure this with the -c / -compact-output, -s=NUM /\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 180 | "-spaces=NUM (for NUM ranging from 0 to 8) and -t / -tabs flags.\n" |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 181 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 182 | "The -input-allow-comments flag allows \"/*slash-star*/\" and\n" |
| 183 | "\"//slash-slash\" C-style comments within JSON input. Such comments are\n" |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 184 | "stripped from the output unless -output-comments was also set.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 185 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 186 | "The -input-allow-extra-comma flag allows input like \"[1,2,]\", with a\n" |
| 187 | "comma after the final element of a JSON list or dictionary.\n" |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 188 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 189 | "The -input-allow-inf-nan-numbers flag allows non-finite floating point\n" |
| 190 | "numbers (infinities and not-a-numbers) within JSON input.\n" |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 191 | "\n" |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 192 | "The -output-comments flag copies any input comments to the output. It\n" |
| 193 | "has no effect unless -input-allow-comments was also set. Comments look\n" |
| 194 | "better after commas than before them, but a closing \"]\" or \"}\" can\n" |
| 195 | "occur after arbitrarily many comments, so -output-comments also requires\n" |
| 196 | "that one or both of -compact-output and -output-extra-comma be set.\n" |
| 197 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 198 | "The -output-extra-comma flag writes output like \"[1,2,]\", with a comma\n" |
| 199 | "after the final element of a JSON list or dictionary. Such commas are\n" |
| 200 | "non-compliant with the JSON specification but many parsers accept them\n" |
| 201 | "and they can produce simpler line-based diffs. This flag is ignored when\n" |
| 202 | "-compact-output is set.\n" |
Nigel Tao | f8dfc76 | 2020-07-23 23:35:44 +1000 | [diff] [blame] | 203 | "\n" |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 204 | "The -jwcc flag (JSON With Commas and Comments) enables all of:\n" |
| 205 | " -input-allow-comments\n" |
| 206 | " -input-allow-extra-comma\n" |
| 207 | " -output-comments\n" |
| 208 | " -output-extra-comma\n" |
| 209 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 210 | "----\n" |
| 211 | "\n" |
| 212 | "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] | 213 | "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] | 214 | "sample input (https://tools.ietf.org/rfc/rfc6901.txt), this command:\n" |
| 215 | " jsonptr -query=/foo/1 rfc-6901-json-pointer.json\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 216 | "will print:\n" |
| 217 | " \"baz\"\n" |
| 218 | "\n" |
| 219 | "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] | 220 | "entire input (the root value). Unlike a file system, the \"/\" query\n" |
Nigel Tao | d0b16cb | 2020-03-14 10:15:54 +1100 | [diff] [blame] | 221 | "does not identify the root. Instead, \"\" is the root and \"/\" is the\n" |
| 222 | "child (the value in a key-value pair) of the root whose key is the empty\n" |
| 223 | "string. Similarly, \"/xyz\" and \"/xyz/\" are two different nodes.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 224 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 225 | "If the query found a valid JSON value, this program will return a zero\n" |
| 226 | "exit code even if the rest of the input isn't valid JSON. If the query\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 227 | "did not find a value, or found an invalid one, this program returns a\n" |
| 228 | "non-zero exit code, but may still print partial output to stdout.\n" |
| 229 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 230 | "The JSON specification (https://json.org/) permits implementations that\n" |
| 231 | "allow duplicate keys, as this one does. This JSON Pointer implementation\n" |
| 232 | "is also greedy, following the first match for each fragment without\n" |
| 233 | "back-tracking. For example, the \"/foo/bar\" query will fail if the root\n" |
| 234 | "object has multiple \"foo\" children but the first one doesn't have a\n" |
| 235 | "\"bar\" child, even if later ones do.\n" |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 236 | "\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 237 | "The -strict-json-pointer-syntax flag restricts the -query=STR string to\n" |
| 238 | "exactly RFC 6901, with only two escape sequences: \"~0\" and \"~1\" for\n" |
| 239 | "\"~\" and \"/\". Without this flag, this program also lets \"~n\" and\n" |
| 240 | "\"~r\" escape the New Line and Carriage Return ASCII control characters,\n" |
| 241 | "which can work better with line oriented Unix tools that assume exactly\n" |
| 242 | "one value (i.e. one JSON Pointer string) per line.\n" |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 243 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 244 | "----\n" |
| 245 | "\n" |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 246 | "The -d=NUM or -max-output-depth=NUM flag gives the maximum (inclusive)\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 247 | "output depth. JSON containers ([] arrays and {} objects) can hold other\n" |
| 248 | "containers. When this flag is set, containers at depth NUM are replaced\n" |
| 249 | "with \"[…]\" or \"{…}\". A bare -d or -max-output-depth is equivalent to\n" |
| 250 | "-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] | 251 | "\n" |
| 252 | "The -max-output-depth flag only affects the program's output. It doesn't\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 253 | "affect whether or not the input is considered valid JSON. The JSON\n" |
| 254 | "specification permits implementations to set their own maximum input\n" |
| 255 | "depth. This JSON implementation sets it to 1024.\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 256 | "\n" |
| 257 | "Depth is measured in terms of nested containers. It is unaffected by the\n" |
| 258 | "number of spaces or tabs used to indent.\n" |
| 259 | "\n" |
| 260 | "When both -max-output-depth and -query are set, the output depth is\n" |
| 261 | "measured from when the query resolves, not from the input root. The\n" |
| 262 | "input depth (measured from the root) is still limited to 1024.\n" |
| 263 | "\n" |
| 264 | "----\n" |
| 265 | "\n" |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 266 | "The -fail-if-unsandboxed flag causes the program to exit if it does not\n" |
| 267 | "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] | 268 | "sandbox, regardless of whether this flag was set."; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 269 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 270 | // ---- |
| 271 | |
Nigel Tao | 6344181 | 2020-08-21 14:05:48 +1000 | [diff] [blame] | 272 | // ascii_escapes was created by script/print-json-ascii-escapes.go. |
| 273 | const uint8_t ascii_escapes[1024] = { |
| 274 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x30, 0x00, // 0x00: "\\u0000" |
| 275 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x31, 0x00, // 0x01: "\\u0001" |
| 276 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x32, 0x00, // 0x02: "\\u0002" |
| 277 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x33, 0x00, // 0x03: "\\u0003" |
| 278 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x34, 0x00, // 0x04: "\\u0004" |
| 279 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x35, 0x00, // 0x05: "\\u0005" |
| 280 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x36, 0x00, // 0x06: "\\u0006" |
| 281 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x37, 0x00, // 0x07: "\\u0007" |
| 282 | 0x02, 0x5C, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08: "\\b" |
| 283 | 0x02, 0x5C, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x09: "\\t" |
| 284 | 0x02, 0x5C, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0A: "\\n" |
| 285 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x42, 0x00, // 0x0B: "\\u000B" |
| 286 | 0x02, 0x5C, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0C: "\\f" |
| 287 | 0x02, 0x5C, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0D: "\\r" |
| 288 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x45, 0x00, // 0x0E: "\\u000E" |
| 289 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x46, 0x00, // 0x0F: "\\u000F" |
| 290 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x30, 0x00, // 0x10: "\\u0010" |
| 291 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x31, 0x00, // 0x11: "\\u0011" |
| 292 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x32, 0x00, // 0x12: "\\u0012" |
| 293 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x33, 0x00, // 0x13: "\\u0013" |
| 294 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x34, 0x00, // 0x14: "\\u0014" |
| 295 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x35, 0x00, // 0x15: "\\u0015" |
| 296 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x36, 0x00, // 0x16: "\\u0016" |
| 297 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x37, 0x00, // 0x17: "\\u0017" |
| 298 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x38, 0x00, // 0x18: "\\u0018" |
| 299 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x39, 0x00, // 0x19: "\\u0019" |
| 300 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x41, 0x00, // 0x1A: "\\u001A" |
| 301 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x42, 0x00, // 0x1B: "\\u001B" |
| 302 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x43, 0x00, // 0x1C: "\\u001C" |
| 303 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x44, 0x00, // 0x1D: "\\u001D" |
| 304 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x45, 0x00, // 0x1E: "\\u001E" |
| 305 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x46, 0x00, // 0x1F: "\\u001F" |
| 306 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x32, 0x30, 0x00, // 0x20: "\\u0020" |
| 307 | 0x01, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x21: "!" |
| 308 | 0x02, 0x5C, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x22: "\\\"" |
| 309 | 0x01, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x23: "#" |
| 310 | 0x01, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x24: "$" |
| 311 | 0x01, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x25: "%" |
| 312 | 0x01, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x26: "&" |
| 313 | 0x01, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x27: "'" |
| 314 | 0x01, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28: "(" |
| 315 | 0x01, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x29: ")" |
| 316 | 0x01, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2A: "*" |
| 317 | 0x01, 0x2B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2B: "+" |
| 318 | 0x01, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2C: "," |
| 319 | 0x01, 0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2D: "-" |
| 320 | 0x01, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2E: "." |
| 321 | 0x01, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2F: "/" |
| 322 | 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30: "0" |
| 323 | 0x01, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x31: "1" |
| 324 | 0x01, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x32: "2" |
| 325 | 0x01, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x33: "3" |
| 326 | 0x01, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x34: "4" |
| 327 | 0x01, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x35: "5" |
| 328 | 0x01, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x36: "6" |
| 329 | 0x01, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x37: "7" |
| 330 | 0x01, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38: "8" |
| 331 | 0x01, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x39: "9" |
| 332 | 0x01, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3A: ":" |
| 333 | 0x01, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3B: ";" |
| 334 | 0x01, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3C: "<" |
| 335 | 0x01, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3D: "=" |
| 336 | 0x01, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3E: ">" |
| 337 | 0x01, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3F: "?" |
| 338 | 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40: "@" |
| 339 | 0x01, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x41: "A" |
| 340 | 0x01, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x42: "B" |
| 341 | 0x01, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x43: "C" |
| 342 | 0x01, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x44: "D" |
| 343 | 0x01, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x45: "E" |
| 344 | 0x01, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x46: "F" |
| 345 | 0x01, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x47: "G" |
| 346 | 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48: "H" |
| 347 | 0x01, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x49: "I" |
| 348 | 0x01, 0x4A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4A: "J" |
| 349 | 0x01, 0x4B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4B: "K" |
| 350 | 0x01, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4C: "L" |
| 351 | 0x01, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4D: "M" |
| 352 | 0x01, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4E: "N" |
| 353 | 0x01, 0x4F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4F: "O" |
| 354 | 0x01, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50: "P" |
| 355 | 0x01, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x51: "Q" |
| 356 | 0x01, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x52: "R" |
| 357 | 0x01, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x53: "S" |
| 358 | 0x01, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x54: "T" |
| 359 | 0x01, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x55: "U" |
| 360 | 0x01, 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x56: "V" |
| 361 | 0x01, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x57: "W" |
| 362 | 0x01, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58: "X" |
| 363 | 0x01, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x59: "Y" |
| 364 | 0x01, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5A: "Z" |
| 365 | 0x01, 0x5B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5B: "[" |
| 366 | 0x02, 0x5C, 0x5C, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5C: "\\\\" |
| 367 | 0x01, 0x5D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5D: "]" |
| 368 | 0x01, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5E: "^" |
| 369 | 0x01, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5F: "_" |
| 370 | 0x01, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60: "`" |
| 371 | 0x01, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x61: "a" |
| 372 | 0x01, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x62: "b" |
| 373 | 0x01, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x63: "c" |
| 374 | 0x01, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x64: "d" |
| 375 | 0x01, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x65: "e" |
| 376 | 0x01, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x66: "f" |
| 377 | 0x01, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x67: "g" |
| 378 | 0x01, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68: "h" |
| 379 | 0x01, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x69: "i" |
| 380 | 0x01, 0x6A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6A: "j" |
| 381 | 0x01, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6B: "k" |
| 382 | 0x01, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6C: "l" |
| 383 | 0x01, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6D: "m" |
| 384 | 0x01, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6E: "n" |
| 385 | 0x01, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6F: "o" |
| 386 | 0x01, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70: "p" |
| 387 | 0x01, 0x71, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x71: "q" |
| 388 | 0x01, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x72: "r" |
| 389 | 0x01, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x73: "s" |
| 390 | 0x01, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x74: "t" |
| 391 | 0x01, 0x75, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x75: "u" |
| 392 | 0x01, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x76: "v" |
| 393 | 0x01, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x77: "w" |
| 394 | 0x01, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78: "x" |
| 395 | 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79: "y" |
| 396 | 0x01, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7A: "z" |
| 397 | 0x01, 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7B: "{" |
| 398 | 0x01, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7C: "|" |
| 399 | 0x01, 0x7D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7D: "}" |
| 400 | 0x01, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7E: "~" |
| 401 | 0x01, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7F: "<DEL>" |
| 402 | }; |
| 403 | |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 404 | // Wuffs allows either statically or dynamically allocated work buffers. This |
| 405 | // program exercises static allocation. |
| 406 | #define WORK_BUFFER_ARRAY_SIZE \ |
| 407 | WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE |
| 408 | #if WORK_BUFFER_ARRAY_SIZE > 0 |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 409 | uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE]; |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 410 | #else |
| 411 | // Not all C/C++ compilers support 0-length arrays. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 412 | uint8_t g_work_buffer_array[1]; |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 413 | #endif |
| 414 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 415 | bool g_sandboxed = false; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 416 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 417 | int g_input_file_descriptor = 0; // A 0 default means stdin. |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 418 | |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame] | 419 | #define NEW_LINE_THEN_256_SPACES \ |
| 420 | "\n " \ |
| 421 | " " \ |
| 422 | " " \ |
| 423 | " " |
| 424 | #define NEW_LINE_THEN_256_TABS \ |
| 425 | "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \ |
| 426 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \ |
| 427 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \ |
| 428 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \ |
| 429 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \ |
| 430 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \ |
| 431 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" |
| 432 | |
| 433 | const char* g_new_line_then_256_indent_bytes; |
| 434 | uint32_t g_bytes_per_indent_depth; |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 435 | |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 436 | #ifndef DST_BUFFER_ARRAY_SIZE |
| 437 | #define DST_BUFFER_ARRAY_SIZE (32 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 438 | #endif |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 439 | #ifndef SRC_BUFFER_ARRAY_SIZE |
| 440 | #define SRC_BUFFER_ARRAY_SIZE (32 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 441 | #endif |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 442 | #ifndef TOKEN_BUFFER_ARRAY_SIZE |
| 443 | #define TOKEN_BUFFER_ARRAY_SIZE (4 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 444 | #endif |
| 445 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 446 | uint8_t g_dst_array[DST_BUFFER_ARRAY_SIZE]; |
| 447 | uint8_t g_src_array[SRC_BUFFER_ARRAY_SIZE]; |
| 448 | wuffs_base__token g_tok_array[TOKEN_BUFFER_ARRAY_SIZE]; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 449 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 450 | wuffs_base__io_buffer g_dst; |
| 451 | wuffs_base__io_buffer g_src; |
| 452 | wuffs_base__token_buffer g_tok; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 453 | |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 454 | // g_cursor_index is the g_src.data.ptr index between the previous and current |
| 455 | // token. An invariant is that (g_cursor_index <= g_src.meta.ri). |
| 456 | size_t g_cursor_index; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 457 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 458 | uint32_t g_depth; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 459 | |
| 460 | enum class context { |
| 461 | none, |
| 462 | in_list_after_bracket, |
| 463 | in_list_after_value, |
| 464 | in_dict_after_brace, |
| 465 | in_dict_after_key, |
| 466 | in_dict_after_value, |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 467 | } g_ctx; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 468 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 469 | bool // |
| 470 | in_dict_before_key() { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 471 | return (g_ctx == context::in_dict_after_brace) || |
| 472 | (g_ctx == context::in_dict_after_value); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 473 | } |
| 474 | |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 475 | bool g_is_after_comment; |
| 476 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 477 | uint32_t g_suppress_write_dst; |
| 478 | bool g_wrote_to_dst; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 479 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 480 | wuffs_json__decoder g_dec; |
Nigel Tao | ea53245 | 2020-07-27 00:03:00 +1000 | [diff] [blame] | 481 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 482 | // ---- |
| 483 | |
| 484 | // Query is a JSON Pointer query. After initializing with a NUL-terminated C |
| 485 | // string, its multiple fragments are consumed as the program walks the JSON |
| 486 | // data from stdin. For example, letting "$" denote a NUL, suppose that we |
| 487 | // 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] | 488 | // 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] | 489 | // |
| 490 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 491 | // / a p p l e / b a n a n a / 1 2 / d u r i a n $ |
| 492 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 493 | // ^ ^ |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 494 | // m_frag_i m_frag_k |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 495 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 496 | // The two pointers m_frag_i and m_frag_k (abbreviated as mfi and mfk) are the |
| 497 | // start (inclusive) and end (exclusive) of the query fragment. They satisfy |
| 498 | // (mfi <= mfk) and may be equal if the fragment empty (note that "" is a valid |
| 499 | // JSON object key). |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 500 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 501 | // The m_frag_j (mfj) pointer moves between these two, or is nullptr. An |
| 502 | // invariant is that (((mfi <= mfj) && (mfj <= mfk)) || (mfj == nullptr)). |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 503 | // |
| 504 | // Wuffs' JSON tokenizer can portray a single JSON string as multiple Wuffs |
| 505 | // tokens, as backslash-escaped values within that JSON string may each get |
| 506 | // their own token. |
| 507 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 508 | // 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] | 509 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 510 | // While mfj remains non-nullptr, each token's unescaped contents are then |
| 511 | // compared to that part of the fragment from mfj to mfk. If it is a prefix |
| 512 | // (including the case of an exact match), then mfj is advanced by the |
| 513 | // unescaped length. Otherwise, mfj is set to nullptr. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 514 | // |
| 515 | // Comparison accounts for JSON Pointer's escaping notation: "~0" and "~1" in |
| 516 | // the query (not the JSON value) are unescaped to "~" and "/" respectively. |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 517 | // "~n" and "~r" are also unescaped to "\n" and "\r". The program is |
| 518 | // responsible for calling Query::validate (with a strict_json_pointer_syntax |
| 519 | // argument) before otherwise using this class. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 520 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 521 | // The mfj pointer therefore advances from mfi to mfk, or drops out, as we |
| 522 | // incrementally match the object key with the query fragment. For example, if |
| 523 | // we have already matched the "ban" of "banana", then we would accept any of |
| 524 | // an "ana" token, an "a" token or a "\u0061" token, amongst others. They would |
| 525 | // advance mfj by 3, 1 or 1 bytes respectively. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 526 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 527 | // mfj |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 528 | // v |
| 529 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 530 | // / a p p l e / b a n a n a / 1 2 / d u r i a n $ |
| 531 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 532 | // ^ ^ |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 533 | // mfi mfk |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 534 | // |
| 535 | // 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] | 536 | // value), if mfj is non-nullptr and equal to (but not less than) mfk then we |
| 537 | // have a fragment match: the query fragment equals the object key. If there is |
| 538 | // a next fragment (in this example, "12") we move the frag_etc pointers to its |
| 539 | // start and end and increment Query::m_depth. Otherwise, we have matched the |
| 540 | // 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] | 541 | // |
| 542 | // The discussion above centers on object keys. If the query fragment is |
| 543 | // numeric then it can also match as an array index: the string fragment "12" |
| 544 | // will match an array's 13th element (starting counting from zero). See RFC |
| 545 | // 6901 for its precise definition of an "array index" number. |
| 546 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 547 | // 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] | 548 | // whose type (wuffs_base__result_u64) is a result type. An error result means |
| 549 | // that the fragment is not an array index. A value result holds the number of |
| 550 | // list elements remaining. When matching a query fragment in an array (instead |
| 551 | // of in an object), each element ticks this number down towards zero. At zero, |
| 552 | // the upcoming JSON value is the one that matches the query fragment. |
| 553 | class Query { |
| 554 | private: |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 555 | uint8_t* m_frag_i; |
| 556 | uint8_t* m_frag_j; |
| 557 | uint8_t* m_frag_k; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 558 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 559 | uint32_t m_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 560 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 561 | wuffs_base__result_u64 m_array_index; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 562 | |
| 563 | public: |
| 564 | void reset(char* query_c_string) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 565 | m_frag_i = (uint8_t*)query_c_string; |
| 566 | m_frag_j = (uint8_t*)query_c_string; |
| 567 | m_frag_k = (uint8_t*)query_c_string; |
| 568 | m_depth = 0; |
| 569 | m_array_index.status.repr = "#main: not an array index query fragment"; |
| 570 | m_array_index.value = 0; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 571 | } |
| 572 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 573 | 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] | 574 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 575 | bool is_at(uint32_t depth) { return m_depth == depth; } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 576 | |
| 577 | // tick returns whether the fragment is a valid array index whose value is |
| 578 | // zero. If valid but non-zero, it decrements it and returns false. |
| 579 | bool tick() { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 580 | if (m_array_index.status.is_ok()) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 581 | if (m_array_index.value == 0) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 582 | return true; |
| 583 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 584 | m_array_index.value--; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 585 | } |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | // next_fragment moves to the next fragment, returning whether it existed. |
| 590 | bool next_fragment() { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 591 | uint8_t* k = m_frag_k; |
| 592 | uint32_t d = m_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 593 | |
| 594 | this->reset(nullptr); |
| 595 | |
| 596 | if (!k || (*k != '/')) { |
| 597 | return false; |
| 598 | } |
| 599 | k++; |
| 600 | |
| 601 | bool all_digits = true; |
| 602 | uint8_t* i = k; |
| 603 | while ((*k != '\x00') && (*k != '/')) { |
| 604 | all_digits = all_digits && ('0' <= *k) && (*k <= '9'); |
| 605 | k++; |
| 606 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 607 | m_frag_i = i; |
| 608 | m_frag_j = i; |
| 609 | m_frag_k = k; |
| 610 | m_depth = d + 1; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 611 | if (all_digits) { |
| 612 | // wuffs_base__parse_number_u64 rejects leading zeroes, e.g. "00", "07". |
Nigel Tao | 6b7ce30 | 2020-07-07 16:19:46 +1000 | [diff] [blame] | 613 | m_array_index = wuffs_base__parse_number_u64( |
| 614 | wuffs_base__make_slice_u8(i, k - i), |
| 615 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 616 | } |
| 617 | return true; |
| 618 | } |
| 619 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 620 | bool matched_all() { return m_frag_k == nullptr; } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 621 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 622 | 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] | 623 | |
| 624 | void incremental_match_slice(uint8_t* ptr, size_t len) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 625 | if (!m_frag_j) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 626 | return; |
| 627 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 628 | uint8_t* j = m_frag_j; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 629 | while (true) { |
| 630 | if (len == 0) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 631 | m_frag_j = j; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 632 | return; |
| 633 | } |
| 634 | |
| 635 | if (*j == '\x00') { |
| 636 | break; |
| 637 | |
| 638 | } else if (*j == '~') { |
| 639 | j++; |
| 640 | if (*j == '0') { |
| 641 | if (*ptr != '~') { |
| 642 | break; |
| 643 | } |
| 644 | } else if (*j == '1') { |
| 645 | if (*ptr != '/') { |
| 646 | break; |
| 647 | } |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 648 | } else if (*j == 'n') { |
| 649 | if (*ptr != '\n') { |
| 650 | break; |
| 651 | } |
| 652 | } else if (*j == 'r') { |
| 653 | if (*ptr != '\r') { |
| 654 | break; |
| 655 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 656 | } else { |
| 657 | break; |
| 658 | } |
| 659 | |
| 660 | } else if (*j != *ptr) { |
| 661 | break; |
| 662 | } |
| 663 | |
| 664 | j++; |
| 665 | ptr++; |
| 666 | len--; |
| 667 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 668 | m_frag_j = nullptr; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | void incremental_match_code_point(uint32_t code_point) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 672 | if (!m_frag_j) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 673 | return; |
| 674 | } |
| 675 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 676 | size_t n = wuffs_base__utf_8__encode( |
| 677 | wuffs_base__make_slice_u8(&u[0], |
| 678 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 679 | code_point); |
| 680 | if (n > 0) { |
| 681 | this->incremental_match_slice(&u[0], n); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // validate returns whether the (ptr, len) arguments form a valid JSON |
| 686 | // Pointer. In particular, it must be valid UTF-8, and either be empty or |
| 687 | // start with a '/'. Any '~' within must immediately be followed by either |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 688 | // '0' or '1'. If strict_json_pointer_syntax is false, a '~' may also be |
| 689 | // followed by either 'n' or 'r'. |
| 690 | static bool validate(char* query_c_string, |
| 691 | size_t length, |
| 692 | bool strict_json_pointer_syntax) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 693 | if (length <= 0) { |
| 694 | return true; |
| 695 | } |
| 696 | if (query_c_string[0] != '/') { |
| 697 | return false; |
| 698 | } |
| 699 | wuffs_base__slice_u8 s = |
| 700 | wuffs_base__make_slice_u8((uint8_t*)query_c_string, length); |
| 701 | bool previous_was_tilde = false; |
| 702 | while (s.len > 0) { |
Nigel Tao | 702c7b2 | 2020-07-22 15:42:54 +1000 | [diff] [blame] | 703 | wuffs_base__utf_8__next__output o = wuffs_base__utf_8__next(s.ptr, s.len); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 704 | if (!o.is_valid()) { |
| 705 | return false; |
| 706 | } |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 707 | |
| 708 | if (previous_was_tilde) { |
| 709 | switch (o.code_point) { |
| 710 | case '0': |
| 711 | case '1': |
| 712 | break; |
| 713 | case 'n': |
| 714 | case 'r': |
| 715 | if (strict_json_pointer_syntax) { |
| 716 | return false; |
| 717 | } |
| 718 | break; |
| 719 | default: |
| 720 | return false; |
| 721 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 722 | } |
| 723 | previous_was_tilde = o.code_point == '~'; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 724 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 725 | s.ptr += o.byte_length; |
| 726 | s.len -= o.byte_length; |
| 727 | } |
| 728 | return !previous_was_tilde; |
| 729 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 730 | } g_query; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 731 | |
| 732 | // ---- |
| 733 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 734 | struct { |
| 735 | int remaining_argc; |
| 736 | char** remaining_argv; |
| 737 | |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 738 | bool compact_output; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 739 | bool fail_if_unsandboxed; |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 740 | bool input_allow_comments; |
| 741 | bool input_allow_extra_comma; |
| 742 | bool input_allow_inf_nan_numbers; |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 743 | bool output_comments; |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 744 | bool output_extra_comma; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 745 | bool strict_json_pointer_syntax; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 746 | bool tabs; |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame] | 747 | |
| 748 | uint32_t max_output_depth; |
| 749 | uint32_t spaces; |
| 750 | |
| 751 | char* query_c_string; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 752 | } g_flags = {0}; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 753 | |
| 754 | const char* // |
| 755 | parse_flags(int argc, char** argv) { |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 756 | g_flags.spaces = 4; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 757 | g_flags.max_output_depth = 0xFFFFFFFF; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 758 | |
| 759 | int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name. |
| 760 | for (; c < argc; c++) { |
| 761 | char* arg = argv[c]; |
| 762 | if (*arg++ != '-') { |
| 763 | break; |
| 764 | } |
| 765 | |
| 766 | // A double-dash "--foo" is equivalent to a single-dash "-foo". As special |
| 767 | // cases, a bare "-" is not a flag (some programs may interpret it as |
| 768 | // stdin) and a bare "--" means to stop parsing flags. |
| 769 | if (*arg == '\x00') { |
| 770 | break; |
| 771 | } else if (*arg == '-') { |
| 772 | arg++; |
| 773 | if (*arg == '\x00') { |
| 774 | c++; |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 | |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 779 | if (!strcmp(arg, "c") || !strcmp(arg, "compact-output")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 780 | g_flags.compact_output = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 781 | continue; |
| 782 | } |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 783 | if (!strcmp(arg, "d") || !strcmp(arg, "max-output-depth")) { |
| 784 | g_flags.max_output_depth = 1; |
| 785 | continue; |
| 786 | } else if (!strncmp(arg, "d=", 2) || |
| 787 | !strncmp(arg, "max-output-depth=", 16)) { |
| 788 | while (*arg++ != '=') { |
| 789 | } |
| 790 | wuffs_base__result_u64 u = wuffs_base__parse_number_u64( |
Nigel Tao | 6b7ce30 | 2020-07-07 16:19:46 +1000 | [diff] [blame] | 791 | wuffs_base__make_slice_u8((uint8_t*)arg, strlen(arg)), |
| 792 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
Nigel Tao | af75772 | 2020-07-18 17:27:11 +1000 | [diff] [blame] | 793 | if (u.status.is_ok() && (u.value <= 0xFFFFFFFF)) { |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 794 | g_flags.max_output_depth = (uint32_t)(u.value); |
| 795 | continue; |
| 796 | } |
| 797 | return g_usage; |
| 798 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 799 | if (!strcmp(arg, "fail-if-unsandboxed")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 800 | g_flags.fail_if_unsandboxed = true; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 801 | continue; |
| 802 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 803 | if (!strcmp(arg, "input-allow-comments")) { |
| 804 | g_flags.input_allow_comments = true; |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 805 | continue; |
| 806 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 807 | if (!strcmp(arg, "input-allow-extra-comma")) { |
| 808 | g_flags.input_allow_extra_comma = true; |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 809 | continue; |
| 810 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 811 | if (!strcmp(arg, "input-allow-inf-nan-numbers")) { |
| 812 | g_flags.input_allow_inf_nan_numbers = true; |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 813 | continue; |
| 814 | } |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 815 | if (!strcmp(arg, "jwcc")) { |
| 816 | g_flags.input_allow_comments = true; |
| 817 | g_flags.input_allow_extra_comma = true; |
| 818 | g_flags.output_comments = true; |
| 819 | g_flags.output_extra_comma = true; |
| 820 | continue; |
| 821 | } |
| 822 | if (!strcmp(arg, "output-comments")) { |
| 823 | g_flags.output_comments = true; |
| 824 | continue; |
| 825 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 826 | if (!strcmp(arg, "output-extra-comma")) { |
| 827 | g_flags.output_extra_comma = true; |
Nigel Tao | dd11469 | 2020-07-25 21:54:12 +1000 | [diff] [blame] | 828 | continue; |
| 829 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 830 | if (!strncmp(arg, "q=", 2) || !strncmp(arg, "query=", 6)) { |
| 831 | while (*arg++ != '=') { |
| 832 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 833 | g_flags.query_c_string = arg; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 834 | continue; |
| 835 | } |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 836 | if (!strncmp(arg, "s=", 2) || !strncmp(arg, "spaces=", 7)) { |
| 837 | while (*arg++ != '=') { |
| 838 | } |
| 839 | if (('0' <= arg[0]) && (arg[0] <= '8') && (arg[1] == '\x00')) { |
| 840 | g_flags.spaces = arg[0] - '0'; |
| 841 | continue; |
| 842 | } |
| 843 | return g_usage; |
| 844 | } |
| 845 | if (!strcmp(arg, "strict-json-pointer-syntax")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 846 | g_flags.strict_json_pointer_syntax = true; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 847 | continue; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 848 | } |
| 849 | if (!strcmp(arg, "t") || !strcmp(arg, "tabs")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 850 | g_flags.tabs = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 851 | continue; |
| 852 | } |
| 853 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 854 | return g_usage; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 855 | } |
| 856 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 857 | if (g_flags.query_c_string && |
| 858 | !Query::validate(g_flags.query_c_string, strlen(g_flags.query_c_string), |
| 859 | g_flags.strict_json_pointer_syntax)) { |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 860 | return "main: bad JSON Pointer (RFC 6901) syntax for the -query=STR flag"; |
| 861 | } |
| 862 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 863 | g_flags.remaining_argc = argc - c; |
| 864 | g_flags.remaining_argv = argv + c; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 865 | return nullptr; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 866 | } |
| 867 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 868 | const char* // |
| 869 | initialize_globals(int argc, char** argv) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 870 | g_dst = wuffs_base__make_io_buffer( |
| 871 | wuffs_base__make_slice_u8(g_dst_array, DST_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 872 | wuffs_base__empty_io_buffer_meta()); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 873 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 874 | g_src = wuffs_base__make_io_buffer( |
| 875 | wuffs_base__make_slice_u8(g_src_array, SRC_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 876 | wuffs_base__empty_io_buffer_meta()); |
| 877 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 878 | g_tok = wuffs_base__make_token_buffer( |
| 879 | wuffs_base__make_slice_token(g_tok_array, TOKEN_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 880 | wuffs_base__empty_token_buffer_meta()); |
| 881 | |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 882 | g_cursor_index = 0; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 883 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 884 | g_depth = 0; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 885 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 886 | g_ctx = context::none; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 887 | |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 888 | g_is_after_comment = false; |
| 889 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 890 | TRY(parse_flags(argc, argv)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 891 | if (g_flags.fail_if_unsandboxed && !g_sandboxed) { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 892 | return "main: unsandboxed"; |
| 893 | } |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 894 | if (g_flags.output_comments && !g_flags.compact_output && |
| 895 | !g_flags.output_extra_comma) { |
| 896 | return "main: -output-comments requires one or both of -compact-output and " |
| 897 | "-output-extra-comma"; |
| 898 | } |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 899 | const int stdin_fd = 0; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 900 | if (g_flags.remaining_argc > |
| 901 | ((g_input_file_descriptor != stdin_fd) ? 1 : 0)) { |
| 902 | return g_usage; |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 903 | } |
| 904 | |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame] | 905 | g_new_line_then_256_indent_bytes = |
| 906 | g_flags.tabs ? NEW_LINE_THEN_256_TABS : NEW_LINE_THEN_256_SPACES; |
| 907 | g_bytes_per_indent_depth = g_flags.tabs ? 1 : g_flags.spaces; |
| 908 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 909 | g_query.reset(g_flags.query_c_string); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 910 | |
Nigel Tao | c96b31c | 2020-07-27 22:37:23 +1000 | [diff] [blame] | 911 | // If the query is non-empty, suppress writing to stdout until we've |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 912 | // completed the query. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 913 | g_suppress_write_dst = g_query.next_fragment() ? 1 : 0; |
| 914 | g_wrote_to_dst = false; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 915 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 916 | TRY(g_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0) |
| 917 | .message()); |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 918 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 919 | if (g_flags.input_allow_comments) { |
| 920 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, true); |
| 921 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, true); |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 922 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 923 | if (g_flags.input_allow_extra_comma) { |
| 924 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, true); |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 925 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 926 | if (g_flags.input_allow_inf_nan_numbers) { |
| 927 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS, true); |
Nigel Tao | 51a3829 | 2020-07-19 22:43:17 +1000 | [diff] [blame] | 928 | } |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 929 | |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 930 | // Consume an optional whitespace trailer. This isn't part of the JSON spec, |
| 931 | // but it works better with line oriented Unix tools (such as "echo 123 | |
| 932 | // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which |
| 933 | // can accidentally contain trailing whitespace. |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 934 | 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] | 935 | |
| 936 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 937 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 938 | |
| 939 | // ---- |
| 940 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 941 | // ignore_return_value suppresses errors from -Wall -Werror. |
| 942 | static void // |
| 943 | ignore_return_value(int ignored) {} |
| 944 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 945 | const char* // |
| 946 | read_src() { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 947 | if (g_src.meta.closed) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 948 | return "main: internal error: read requested on a closed source"; |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 949 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 950 | g_src.compact(); |
| 951 | if (g_src.meta.wi >= g_src.data.len) { |
| 952 | return "main: g_src buffer is full"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 953 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 954 | while (true) { |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 955 | ssize_t n = read(g_input_file_descriptor, g_src.writer_pointer(), |
| 956 | g_src.writer_length()); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 957 | if (n >= 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 958 | g_src.meta.wi += n; |
| 959 | g_src.meta.closed = n == 0; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 960 | break; |
| 961 | } else if (errno != EINTR) { |
| 962 | return strerror(errno); |
| 963 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 964 | } |
| 965 | return nullptr; |
| 966 | } |
| 967 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 968 | const char* // |
| 969 | flush_dst() { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 970 | while (true) { |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 971 | size_t n = g_dst.reader_length(); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 972 | if (n == 0) { |
| 973 | break; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 974 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 975 | const int stdout_fd = 1; |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 976 | ssize_t i = write(stdout_fd, g_dst.reader_pointer(), n); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 977 | if (i >= 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 978 | g_dst.meta.ri += i; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 979 | } else if (errno != EINTR) { |
| 980 | return strerror(errno); |
| 981 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 982 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 983 | g_dst.compact(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 984 | return nullptr; |
| 985 | } |
| 986 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 987 | const char* // |
Nigel Tao | 6b86cbc | 2020-08-19 11:39:56 +1000 | [diff] [blame] | 988 | write_dst_slow(const void* s, size_t n) { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 989 | const uint8_t* p = static_cast<const uint8_t*>(s); |
| 990 | while (n > 0) { |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 991 | size_t i = g_dst.writer_length(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 992 | if (i == 0) { |
| 993 | const char* z = flush_dst(); |
| 994 | if (z) { |
| 995 | return z; |
| 996 | } |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 997 | i = g_dst.writer_length(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 998 | if (i == 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 999 | return "main: g_dst buffer is full"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | if (i > n) { |
| 1004 | i = n; |
| 1005 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1006 | memcpy(g_dst.data.ptr + g_dst.meta.wi, p, i); |
| 1007 | g_dst.meta.wi += i; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1008 | p += i; |
| 1009 | n -= i; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1010 | g_wrote_to_dst = true; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1011 | } |
| 1012 | return nullptr; |
| 1013 | } |
| 1014 | |
Nigel Tao | 6b86cbc | 2020-08-19 11:39:56 +1000 | [diff] [blame] | 1015 | inline const char* // |
| 1016 | write_dst(const void* s, size_t n) { |
| 1017 | if (g_suppress_write_dst > 0) { |
| 1018 | return nullptr; |
| 1019 | } else if (n <= (DST_BUFFER_ARRAY_SIZE - g_dst.meta.wi)) { |
| 1020 | memcpy(g_dst.data.ptr + g_dst.meta.wi, s, n); |
| 1021 | g_dst.meta.wi += n; |
| 1022 | g_wrote_to_dst = true; |
| 1023 | return nullptr; |
| 1024 | } |
| 1025 | return write_dst_slow(s, n); |
| 1026 | } |
| 1027 | |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1028 | #define TRY_INDENT_WITH_LEADING_NEW_LINE \ |
| 1029 | do { \ |
| 1030 | uint32_t indent = g_depth * g_bytes_per_indent_depth; \ |
| 1031 | TRY(write_dst(g_new_line_then_256_indent_bytes, 1 + (indent & 0xFF))); \ |
| 1032 | for (indent >>= 8; indent > 0; indent--) { \ |
| 1033 | TRY(write_dst(g_new_line_then_256_indent_bytes + 1, 0x100)); \ |
| 1034 | } \ |
| 1035 | } while (false) |
| 1036 | |
| 1037 | // TRY_INDENT_SANS_LEADING_NEW_LINE is used after comments, which print their |
| 1038 | // own "\n". |
| 1039 | #define TRY_INDENT_SANS_LEADING_NEW_LINE \ |
| 1040 | do { \ |
| 1041 | uint32_t indent = g_depth * g_bytes_per_indent_depth; \ |
| 1042 | TRY(write_dst(g_new_line_then_256_indent_bytes + 1, (indent & 0xFF))); \ |
| 1043 | for (indent >>= 8; indent > 0; indent--) { \ |
| 1044 | TRY(write_dst(g_new_line_then_256_indent_bytes + 1, 0x100)); \ |
| 1045 | } \ |
| 1046 | } while (false) |
| 1047 | |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1048 | // ---- |
| 1049 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1050 | const char* // |
Nigel Tao | 7cb7654 | 2020-07-19 22:19:04 +1000 | [diff] [blame] | 1051 | handle_unicode_code_point(uint32_t ucp) { |
Nigel Tao | 6344181 | 2020-08-21 14:05:48 +1000 | [diff] [blame] | 1052 | if (ucp < 0x80) { |
| 1053 | return write_dst(&ascii_escapes[8 * ucp + 1], ascii_escapes[8 * ucp]); |
Nigel Tao | 7cb7654 | 2020-07-19 22:19:04 +1000 | [diff] [blame] | 1054 | } |
Nigel Tao | 7cb7654 | 2020-07-19 22:19:04 +1000 | [diff] [blame] | 1055 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 1056 | size_t n = wuffs_base__utf_8__encode( |
| 1057 | wuffs_base__make_slice_u8(&u[0], |
| 1058 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 1059 | ucp); |
| 1060 | if (n == 0) { |
| 1061 | return "main: internal error: unexpected Unicode code point"; |
| 1062 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1063 | return write_dst(&u[0], n); |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1064 | } |
| 1065 | |
Nigel Tao | d191a3f | 2020-07-19 22:14:54 +1000 | [diff] [blame] | 1066 | // ---- |
| 1067 | |
Nigel Tao | 50db4a4 | 2020-08-20 11:31:28 +1000 | [diff] [blame] | 1068 | inline const char* // |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 1069 | handle_token(wuffs_base__token t, bool start_of_token_chain) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1070 | do { |
Nigel Tao | 462f866 | 2020-04-01 23:01:51 +1100 | [diff] [blame] | 1071 | int64_t vbc = t.value_base_category(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1072 | uint64_t vbd = t.value_base_detail(); |
Nigel Tao | ee6927f | 2020-07-27 12:08:33 +1000 | [diff] [blame] | 1073 | uint64_t token_length = t.length(); |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1074 | // The "- token_length" is because we incremented g_cursor_index before |
| 1075 | // calling handle_token. |
Nigel Tao | ee6927f | 2020-07-27 12:08:33 +1000 | [diff] [blame] | 1076 | wuffs_base__slice_u8 tok = wuffs_base__make_slice_u8( |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1077 | g_src.data.ptr + g_cursor_index - token_length, token_length); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1078 | |
| 1079 | // Handle ']' or '}'. |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 1080 | if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) && |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1081 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1082 | if (g_query.is_at(g_depth)) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1083 | return "main: no match for query"; |
| 1084 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1085 | if (g_depth <= 0) { |
| 1086 | return "main: internal error: inconsistent g_depth"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1087 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1088 | g_depth--; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1089 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1090 | if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) { |
| 1091 | g_suppress_write_dst--; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1092 | // '…' is U+2026 HORIZONTAL ELLIPSIS, which is 3 UTF-8 bytes. |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1093 | TRY(write_dst((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) |
| 1094 | ? "\"[…]\"" |
| 1095 | : "\"{…}\"", |
| 1096 | 7)); |
| 1097 | } else { |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1098 | // Write preceding whitespace. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1099 | if ((g_ctx != context::in_list_after_bracket) && |
| 1100 | (g_ctx != context::in_dict_after_brace) && |
| 1101 | !g_flags.compact_output) { |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1102 | if (g_is_after_comment) { |
| 1103 | TRY_INDENT_SANS_LEADING_NEW_LINE; |
| 1104 | } else { |
| 1105 | if (g_flags.output_extra_comma) { |
| 1106 | TRY(write_dst(",", 1)); |
| 1107 | } |
| 1108 | TRY_INDENT_WITH_LEADING_NEW_LINE; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1109 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1110 | } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1111 | |
| 1112 | TRY(write_dst( |
| 1113 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", |
| 1114 | 1)); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1115 | } |
| 1116 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1117 | g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 1118 | ? context::in_list_after_value |
| 1119 | : context::in_dict_after_key; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1120 | goto after_value; |
| 1121 | } |
| 1122 | |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 1123 | // Write preceding whitespace and punctuation, if it wasn't ']', '}' or a |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1124 | // continuation of a multi-token chain. |
| 1125 | if (start_of_token_chain) { |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1126 | if (g_is_after_comment) { |
| 1127 | TRY_INDENT_SANS_LEADING_NEW_LINE; |
| 1128 | } else if (g_ctx == context::in_dict_after_key) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1129 | TRY(write_dst(": ", g_flags.compact_output ? 1 : 2)); |
| 1130 | } else if (g_ctx != context::none) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1131 | if ((g_ctx != context::in_list_after_bracket) && |
| 1132 | (g_ctx != context::in_dict_after_brace)) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1133 | TRY(write_dst(",", 1)); |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 1134 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1135 | if (!g_flags.compact_output) { |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1136 | TRY_INDENT_WITH_LEADING_NEW_LINE; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1140 | bool query_matched_fragment = false; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1141 | if (g_query.is_at(g_depth)) { |
| 1142 | switch (g_ctx) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1143 | case context::in_list_after_bracket: |
| 1144 | case context::in_list_after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1145 | query_matched_fragment = g_query.tick(); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1146 | break; |
| 1147 | case context::in_dict_after_key: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1148 | query_matched_fragment = g_query.matched_fragment(); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1149 | break; |
Nigel Tao | 18ef5b4 | 2020-03-16 10:37:47 +1100 | [diff] [blame] | 1150 | default: |
| 1151 | break; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1152 | } |
| 1153 | } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1154 | if (!query_matched_fragment) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1155 | // No-op. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1156 | } else if (!g_query.next_fragment()) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1157 | // There is no next fragment. We have matched the complete query, and |
| 1158 | // the upcoming JSON value is the result of that query. |
| 1159 | // |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1160 | // Un-suppress writing to stdout and reset the g_ctx and g_depth as if |
| 1161 | // we were about to decode a top-level value. This makes any subsequent |
| 1162 | // indentation be relative to this point, and we will return g_eod |
| 1163 | // after the upcoming JSON value is complete. |
| 1164 | if (g_suppress_write_dst != 1) { |
| 1165 | return "main: internal error: inconsistent g_suppress_write_dst"; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1166 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1167 | g_suppress_write_dst = 0; |
| 1168 | g_ctx = context::none; |
| 1169 | g_depth = 0; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1170 | } else if ((vbc != WUFFS_BASE__TOKEN__VBC__STRUCTURE) || |
| 1171 | !(vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH)) { |
| 1172 | // The query has moved on to the next fragment but the upcoming JSON |
| 1173 | // value is not a container. |
| 1174 | return "main: no match for query"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | // Handle the token itself: either a container ('[' or '{') or a simple |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1179 | // value: string (a chain of raw or escaped parts), literal or number. |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1180 | switch (vbc) { |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1181 | case WUFFS_BASE__TOKEN__VBC__STRUCTURE: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1182 | if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) { |
| 1183 | g_suppress_write_dst++; |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1184 | } else { |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1185 | TRY(write_dst( |
| 1186 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", |
| 1187 | 1)); |
| 1188 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1189 | g_depth++; |
| 1190 | g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 1191 | ? context::in_list_after_bracket |
| 1192 | : context::in_dict_after_brace; |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1193 | return nullptr; |
| 1194 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1195 | case WUFFS_BASE__TOKEN__VBC__STRING: |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1196 | if (start_of_token_chain) { |
| 1197 | TRY(write_dst("\"", 1)); |
| 1198 | g_query.restart_fragment(in_dict_before_key() && |
| 1199 | g_query.is_at(g_depth)); |
| 1200 | } |
| 1201 | |
| 1202 | if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) { |
| 1203 | // No-op. |
| 1204 | } else if (vbd & |
| 1205 | WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) { |
| 1206 | TRY(write_dst(tok.ptr, tok.len)); |
| 1207 | g_query.incremental_match_slice(tok.ptr, tok.len); |
| 1208 | } else { |
| 1209 | return "main: internal error: unexpected string-token conversion"; |
| 1210 | } |
| 1211 | |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1212 | if (t.continued()) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1213 | return nullptr; |
| 1214 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1215 | TRY(write_dst("\"", 1)); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1216 | goto after_value; |
| 1217 | |
| 1218 | case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1219 | if (!t.continued()) { |
| 1220 | return "main: internal error: unexpected non-continued UCP token"; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1221 | } |
| 1222 | TRY(handle_unicode_code_point(vbd)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1223 | g_query.incremental_match_code_point(vbd); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1224 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1225 | |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1226 | case WUFFS_BASE__TOKEN__VBC__LITERAL: |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1227 | case WUFFS_BASE__TOKEN__VBC__NUMBER: |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1228 | TRY(write_dst(tok.ptr, tok.len)); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1229 | goto after_value; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1230 | } |
| 1231 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1232 | // Return an error if we didn't match the (vbc, vbd) pair. |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1233 | return "main: internal error: unexpected token"; |
| 1234 | } while (0); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1235 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1236 | // Book-keeping after completing a value (whether a container value or a |
| 1237 | // simple value). Empty parent containers are no longer empty. If the parent |
| 1238 | // container is a "{...}" object, toggle between keys and values. |
| 1239 | after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1240 | if (g_depth == 0) { |
| 1241 | return g_eod; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1242 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1243 | switch (g_ctx) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1244 | case context::in_list_after_bracket: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1245 | g_ctx = context::in_list_after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1246 | break; |
| 1247 | case context::in_dict_after_brace: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1248 | g_ctx = context::in_dict_after_key; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1249 | break; |
| 1250 | case context::in_dict_after_key: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1251 | g_ctx = context::in_dict_after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1252 | break; |
| 1253 | case context::in_dict_after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1254 | g_ctx = context::in_dict_after_key; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1255 | break; |
Nigel Tao | 18ef5b4 | 2020-03-16 10:37:47 +1100 | [diff] [blame] | 1256 | default: |
| 1257 | break; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1258 | } |
| 1259 | return nullptr; |
| 1260 | } |
| 1261 | |
| 1262 | const char* // |
| 1263 | main1(int argc, char** argv) { |
| 1264 | TRY(initialize_globals(argc, argv)); |
| 1265 | |
Nigel Tao | cd183f9 | 2020-07-14 12:11:05 +1000 | [diff] [blame] | 1266 | bool start_of_token_chain = true; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1267 | while (true) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1268 | wuffs_base__status status = g_dec.decode_tokens( |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1269 | &g_tok, &g_src, |
| 1270 | 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] | 1271 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1272 | while (g_tok.meta.ri < g_tok.meta.wi) { |
| 1273 | wuffs_base__token t = g_tok.data.ptr[g_tok.meta.ri++]; |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1274 | uint64_t token_length = t.length(); |
| 1275 | if ((g_src.meta.ri - g_cursor_index) < token_length) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1276 | return "main: internal error: inconsistent g_src indexes"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1277 | } |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1278 | g_cursor_index += token_length; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1279 | |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1280 | // Handle filler tokens (e.g. whitespace, punctuation and comments). |
| 1281 | // These are skipped, unless -output-comments is enabled. |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 1282 | if (t.value_base_category() == WUFFS_BASE__TOKEN__VBC__FILLER) { |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1283 | if (g_flags.output_comments && |
| 1284 | (t.value_base_detail() & |
| 1285 | WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_ANY)) { |
| 1286 | if (g_flags.compact_output) { |
| 1287 | TRY(write_dst(g_src.data.ptr + g_cursor_index - token_length, |
| 1288 | token_length)); |
| 1289 | } else { |
| 1290 | if (start_of_token_chain) { |
| 1291 | if (g_is_after_comment) { |
| 1292 | TRY_INDENT_SANS_LEADING_NEW_LINE; |
| 1293 | } else if (g_ctx != context::none) { |
| 1294 | if (g_ctx == context::in_dict_after_key) { |
| 1295 | TRY(write_dst(":", 1)); |
| 1296 | } else if ((g_ctx != context::in_list_after_bracket) && |
| 1297 | (g_ctx != context::in_dict_after_brace)) { |
| 1298 | TRY(write_dst(",", 1)); |
| 1299 | } |
| 1300 | if (!g_flags.compact_output) { |
| 1301 | TRY_INDENT_WITH_LEADING_NEW_LINE; |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | TRY(write_dst(g_src.data.ptr + g_cursor_index - token_length, |
| 1306 | token_length)); |
| 1307 | if (!t.continued() && |
| 1308 | (t.value_base_detail() & |
| 1309 | WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_BLOCK)) { |
| 1310 | TRY(write_dst("\n", 1)); |
| 1311 | } |
| 1312 | g_is_after_comment = true; |
| 1313 | } |
| 1314 | } |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1315 | start_of_token_chain = !t.continued(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1316 | continue; |
| 1317 | } |
| 1318 | |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 1319 | const char* z = handle_token(t, start_of_token_chain); |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1320 | g_is_after_comment = false; |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1321 | start_of_token_chain = !t.continued(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1322 | if (z == nullptr) { |
| 1323 | continue; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1324 | } else if (z == g_eod) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1325 | goto end_of_data; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1326 | } |
| 1327 | return z; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1328 | } |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1329 | |
| 1330 | if (status.repr == nullptr) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1331 | return "main: internal error: unexpected end of token stream"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1332 | } else if (status.repr == wuffs_base__suspension__short_read) { |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1333 | if (g_cursor_index != g_src.meta.ri) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1334 | return "main: internal error: inconsistent g_src indexes"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1335 | } |
| 1336 | TRY(read_src()); |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1337 | g_cursor_index = g_src.meta.ri; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1338 | } else if (status.repr == wuffs_base__suspension__short_write) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1339 | g_tok.compact(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1340 | } else { |
| 1341 | return status.message(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1342 | } |
| 1343 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1344 | end_of_data: |
| 1345 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1346 | // 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] | 1347 | // confirm that we've processed all the tokens. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1348 | if (g_flags.query_c_string && *g_flags.query_c_string) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1349 | return nullptr; |
| 1350 | } |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1351 | |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1352 | // Check that we've exhausted the input. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1353 | 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] | 1354 | TRY(read_src()); |
| 1355 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1356 | if ((g_src.meta.ri < g_src.meta.wi) || !g_src.meta.closed) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1357 | return "main: valid JSON followed by further (unexpected) data"; |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | // 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] | 1361 | // filler tokens. For example, "true\n" is valid JSON (and fully consumed |
| 1362 | // with WUFFS_JSON__QUIRK_ALLOW_TRAILING_NEW_LINE enabled) with a trailing |
| 1363 | // filler token for the "\n". |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1364 | for (; g_tok.meta.ri < g_tok.meta.wi; g_tok.meta.ri++) { |
| 1365 | if (g_tok.data.ptr[g_tok.meta.ri].value_base_category() != |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1366 | WUFFS_BASE__TOKEN__VBC__FILLER) { |
| 1367 | return "main: internal error: decoded OK but unprocessed tokens remain"; |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | return nullptr; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1372 | } |
| 1373 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1374 | int // |
| 1375 | compute_exit_code(const char* status_msg) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1376 | if (!status_msg) { |
| 1377 | return 0; |
| 1378 | } |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1379 | size_t n; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1380 | if (status_msg == g_usage) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1381 | n = strlen(status_msg); |
| 1382 | } else { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1383 | n = strnlen(status_msg, 2047); |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1384 | if (n >= 2047) { |
| 1385 | status_msg = "main: internal error: error message is too long"; |
| 1386 | n = strnlen(status_msg, 2047); |
| 1387 | } |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1388 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1389 | const int stderr_fd = 2; |
| 1390 | ignore_return_value(write(stderr_fd, status_msg, n)); |
| 1391 | ignore_return_value(write(stderr_fd, "\n", 1)); |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1392 | // Return an exit code of 1 for regular (forseen) errors, e.g. badly |
| 1393 | // formatted or unsupported input. |
| 1394 | // |
| 1395 | // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive |
| 1396 | // run-time checks found that an internal invariant did not hold. |
| 1397 | // |
| 1398 | // Automated testing, including badly formatted inputs, can therefore |
| 1399 | // discriminate between expected failure (exit code 1) and unexpected failure |
| 1400 | // (other non-zero exit codes). Specifically, exit code 2 for internal |
| 1401 | // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 |
| 1402 | // linux) for a segmentation fault (e.g. null pointer dereference). |
| 1403 | return strstr(status_msg, "internal error:") ? 2 : 1; |
| 1404 | } |
| 1405 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1406 | int // |
| 1407 | main(int argc, char** argv) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1408 | // Look for an input filename (the first non-flag argument) in argv. If there |
| 1409 | // is one, open it (but do not read from it) before we self-impose a sandbox. |
| 1410 | // |
| 1411 | // Flags start with "-", unless it comes after a bare "--" arg. |
| 1412 | { |
| 1413 | bool dash_dash = false; |
| 1414 | int a; |
| 1415 | for (a = 1; a < argc; a++) { |
| 1416 | char* arg = argv[a]; |
| 1417 | if ((arg[0] == '-') && !dash_dash) { |
| 1418 | dash_dash = (arg[1] == '-') && (arg[2] == '\x00'); |
| 1419 | continue; |
| 1420 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1421 | g_input_file_descriptor = open(arg, O_RDONLY); |
| 1422 | if (g_input_file_descriptor < 0) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1423 | fprintf(stderr, "%s: %s\n", arg, strerror(errno)); |
| 1424 | return 1; |
| 1425 | } |
| 1426 | break; |
| 1427 | } |
| 1428 | } |
| 1429 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1430 | #if defined(WUFFS_EXAMPLE_USE_SECCOMP) |
| 1431 | prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1432 | g_sandboxed = true; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1433 | #endif |
| 1434 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1435 | const char* z = main1(argc, argv); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1436 | if (g_wrote_to_dst) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1437 | const char* z1 = write_dst("\n", 1); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1438 | const char* z2 = flush_dst(); |
| 1439 | z = z ? z : (z1 ? z1 : z2); |
| 1440 | } |
| 1441 | int exit_code = compute_exit_code(z); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1442 | |
| 1443 | #if defined(WUFFS_EXAMPLE_USE_SECCOMP) |
| 1444 | // Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by |
| 1445 | // either calling _exit or returning from main. SECCOMP_MODE_STRICT allows |
| 1446 | // only SYS_exit. |
| 1447 | syscall(SYS_exit, exit_code); |
| 1448 | #endif |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1449 | return exit_code; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1450 | } |