# Reader of JSON texts (RFC 8259) for the scripts of the CI, in plain POSIX awk (mawk, gawk, busybox awk): no jq, no # Node. Every file named on the command line is one JSON text. The program that includes this file (awk -f json.awk # -f .awk) defines five hooks, called while a file is read: # # json_begin(file) before the first value of the file # json_open(kind) a container opens: kind "a" (array) or "o" (object); json_depth is now its level (the # root is level 1) and json_stack[d] the kind of the container at level d # json_close(kind) the container at level json_depth closes (json_depth drops after the call) # json_scalar(kind, value) a value that is not a container: kind "s" string (decoded), "n" number (its text), "t" # true, "f" false, "z" null # json_end(file, error) after the file: error is "" for a valid JSON text, otherwise the reason (with the line) # # Where a value is: inside the container at level d it is at json_at[d], its key (object, decoded) or its index from 0 # (array). A scalar inside the container at level json_depth is at json_at[json_depth]; at the root json_depth is 0. # After an error the rest of that file is skipped and only json_end follows; an empty file is an error, as for every # JSON parser. The hooks must not call exit: the program records what it needs and decides in its own END, which runs # after the END of this file (it comes first on the command line). # # Run with LC_ALL=C: the text is read as bytes, a leading BOM is skipped and a string may hold any byte from 0x20 on # (UTF-8 is not checked). \uXXXX escapes below 0x80 are decoded; the others stay as they are written, which is enough # for the names, versions, dates and addresses the CI compares. Tokens never span lines in JSON (a string cannot hold a # raw line break), so each line is cut once at the quotes and once at the punctuation: the work grows with the size of # the text, never with its square. BEGIN { json_file = "" json_control = "[" sprintf("%c", 1) "-" sprintf("%c", 31) "]" json_escape = "\\\\([\"\\\\/bfnrt]|u[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])" json_number = "^-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][-+]?[0-9]+)?$" } FNR == 1 { if (json_file != "") json_finish() json_start(FILENAME) if (substr($0, 1, 3) == "\357\273\277") $0 = substr($0, 4) } { if (json_error == "") json_line($0) } END { if (json_file != "") json_finish() for (json_i = 1; json_i < ARGC; json_i++) { json_f = ARGV[json_i] if (json_f == "" || (json_f in json_seen) || json_f ~ /^[A-Za-z_][A-Za-z0-9_]*=/) continue json_start(json_f) json_fail("file vuoto") json_finish() } } function json_start(file) { json_file = file json_seen[file] = 1 json_depth = 0 json_state = "V" json_error = "" json_instring = 0 json_buffer = "" json_lineno = 0 json_begin(file) } function json_finish() { if (json_error == "") { if (json_instring) json_fail("stringa non chiusa") else if (json_state != "E") json_fail(json_depth > 0 || json_state != "V" ? "testo interrotto" : "file vuoto") } json_end(json_file, json_error) json_file = "" } function json_fail(message) { if (json_error == "") json_error = (json_lineno > 0 ? "riga " json_lineno ": " : "") message return 0 } # One line: the parts between quotes alternate between text outside and inside strings, except where a quote is # escaped (an odd number of backslashes before it), which stays in the string. function json_line(line, n, parts, i) { json_lineno = FNR n = split(line, parts, "\"") for (i = 1; i <= n; i++) { if (json_instring) { json_buffer = json_buffer parts[i] if (i == n) return json_fail("stringa non chiusa a fine riga") if (json_escaped(json_buffer)) { json_buffer = json_buffer "\"" continue } json_instring = 0 json_string(json_buffer) } else { json_plain(parts[i]) if (i < n && json_error == "") { json_instring = 1 json_buffer = "" } } if (json_error != "") return 0 } return 1 } function json_escaped(text) { return match(text, /[\\]+$/) && RLENGTH % 2 == 1 } # Text outside strings: punctuation, numbers, true, false, null and the four blanks of JSON. function json_plain(text, n, tokens, i) { gsub(/\{/, " { ", text) gsub(/\}/, " } ", text) gsub(/\[/, " [ ", text) gsub(/\]/, " ] ", text) gsub(/,/, " , ", text) gsub(/:/, " : ", text) gsub(/\r/, " ", text) n = split(text, tokens, " ") for (i = 1; i <= n; i++) { json_token(tokens[i]) if (json_error != "") return 0 } return 1 } function json_token(token, kind) { if (token == "{" || token == "[") { if (json_state != "V" && json_state != "VA") return json_fail("«" token "» inatteso") json_enter() json_depth++ json_stack[json_depth] = token == "{" ? "o" : "a" json_index[json_depth] = 0 json_at[json_depth] = "" json_state = token == "{" ? "KO" : "VA" json_open(json_stack[json_depth]) return 1 } if (token == "}" || token == "]") { kind = token == "}" ? "o" : "a" if (json_depth == 0 || json_stack[json_depth] != kind) return json_fail("«" token "» inatteso") if (json_state != "N" && !(kind == "o" && json_state == "KO") && !(kind == "a" && json_state == "VA")) return json_fail("«" token "» inatteso") json_close(kind) json_depth-- json_state = json_depth > 0 ? "N" : "E" return 1 } if (token == ",") { if (json_state != "N") return json_fail("«,» inattesa") if (json_stack[json_depth] == "o") { json_state = "K" } else { json_index[json_depth]++ json_state = "V" } return 1 } if (token == ":") { if (json_state != ":") return json_fail("«:» inattesi") json_state = "V" return 1 } if (token == "true") kind = "t" else if (token == "false") kind = "f" else if (token == "null") kind = "z" else if (token ~ json_number) kind = "n" else return json_fail("«" substr(token, 1, 20) "» non è un valore JSON") if (json_state != "V" && json_state != "VA") return json_fail("«" substr(token, 1, 20) "» inatteso") json_enter() json_scalar(kind, token) json_state = json_depth > 0 ? "N" : "E" return 1 } # A whole string, without its quotes: a key or a value. function json_string(raw, rest, value) { if (raw ~ json_control) return json_fail("carattere di controllo in una stringa") rest = raw gsub(json_escape, "", rest) if (index(rest, "\\") > 0) return json_fail("sequenza «\\» non valida in una stringa") value = json_unescape(raw) if (json_state == "K" || json_state == "KO") { json_at[json_depth] = value json_state = ":" return 1 } if (json_state != "V" && json_state != "VA") return json_fail("stringa inattesa") json_enter() json_scalar("s", value) json_state = json_depth > 0 ? "N" : "E" return 1 } # A value starts inside the current container: in an array its place is the running index. function json_enter() { if (json_depth > 0 && json_stack[json_depth] == "a") json_at[json_depth] = json_index[json_depth] } function json_unescape(text, out, i, c, code) { if (index(text, "\\") == 0) return text out = "" while ((i = index(text, "\\")) > 0) { out = out substr(text, 1, i - 1) c = substr(text, i + 1, 1) if (c == "u") { code = json_hex(substr(text, i + 2, 4)) out = out (code > 0 && code < 128 ? sprintf("%c", code) : "\\u" substr(text, i + 2, 4)) text = substr(text, i + 6) } else { if (c == "b") c = "\b" else if (c == "f") c = "\f" else if (c == "n") c = "\n" else if (c == "r") c = "\r" else if (c == "t") c = "\t" out = out c text = substr(text, i + 2) } } return out text } function json_hex(digits, value, i) { value = 0 for (i = 1; i <= length(digits); i++) value = value * 16 + index("0123456789abcdef", tolower(substr(digits, i, 1))) - 1 return value }