#!/bin/sh -efu
### This file is covered by the GNU General Public License,
### which should be included with libshell as the file LICENSE.
### All copyright information are listed in the COPYING.
###
### This source is largely based on the implementation of the json
### parser written by Richard Crowley.
###
### Original source: https://github.com/rcrowley/json.sh

if [ -z "${__included_shell_json-}" ]; then
__included_shell_json=1

. shell-error
. shell-string

# Probe read once; its behavior is fixed for the current shell.
__json_read_opt_n=''
__json_read_newline=1
# shellcheck disable=SC2169,SC3045
if { printf 1 | (IFS= read -n 1 -r __json_probe); } 2>/dev/null; then
	__json_read_opt_n=1
	# bash stops at newlines with -n; ksh includes them in the chunk.
	# shellcheck disable=SC2169,SC3045
	printf 'a\nb' | (IFS= read -n 3 -r __json_probe; [ "$__json_probe" = a ]) ||
		__json_read_newline=
fi

JSON_FOREACH_ABORT=100

JSON_DEBUG=
__json_debug()
{
	[ -z "$JSON_DEBUG" ] ||
		printf >&2 'json: %s\n' "$*"
}

__json_push_name()
{
	eval "__json_name_$__json_name=\"\$1\""
	__json_name=$(( $__json_name + 1 ))
}

__json_pop_name()
{
	__json_name=$(( $__json_name - 1 ))
	eval "unset __json_name_$__json_name"
}

json_get_last_name()
{
	eval "$1=\"\${__json_name_$(( $__json_name - 1 ))-}\""
}

json_get_full_name()
{
	local __i=0 __v=''
	while [ "$__i" -lt "$__json_name" ]; do
		eval "__v=\"\$__v${2-/}\${__json_name_$__i-}\""
		__i=$(( $__i + 1 ))
	done
	eval "$1=\"\$__v\""
}

__json_get_stack()
{
	local i=0
	stack=''
	while [ "$i" -lt "$__json_states" ]; do
		eval "stack=\"\${stack:+\$stack -> }\${__json_states_$i-}\""
		i=$(( $i + 1 ))
	done
	stack="${stack:-none}"
}

__json_push_state()
{
	if [ -n "${JSON_DEBUG-}" ]; then
		local stack
		__json_get_stack
		__json_debug "state: append: $stack -> { $1 }"
	fi

	__json_state="$1"

	eval "__json_states_$__json_states=\"\$1\""
	__json_states=$(( $__json_states + 1 ))
}

__json_pop_state()
{
	local prev="$__json_state"

	__json_states=$(( $__json_states - 1 ))
	eval "unset __json_states_$__json_states"
	eval "__json_state=\"\${__json_states_$(( $__json_states - 1 ))-}\""

	if [ -n "${JSON_DEBUG-}" ]; then
		local stack
		__json_get_stack
		__json_debug "state: remove: $stack // { $prev }"
	fi
}

json_foreach_handler()
{
	local name=''
	json_get_full_name name
	printf '%s %s %s\n' "${name:-/}" "$1" "$2"
}

__json_handle()
{
	if [ -n "$__json_handle" ]; then
		local IFS="$SAVE_IFS"
		json_foreach_handler "$1" "$2"
	fi
}

# Consume standard input one character at a time to parse JSON.
json_foreach()
{
	local __json_l __json_c __json_escape __json_eof __json_buffer __json_state
	local __json_name=0 __json_states=0
	local __json_handle=1 __json_unicode=0
	local __json_controls __json_cr __json_lf='
'
	__json_controls="$(printf '\001-\037')"
	__json_cr="$(printf '\r')"

	local SAVE_IFS="$IFS"
	local IFS=''

	__json_buffer='' __json_eof='' __json_escape=''

	# Accept exactly one top-level value.
	__json_push_state 'done'
	__json_push_state 'value'

	while [ -z "$__json_eof" ]; do
		read ${__json_read_opt_n:+-n 9} -r __json_l || __json_eof=1

		# read removes the newline delimiter, but it still separates tokens.
		if [ -z "$__json_eof" ] && [ -n "$__json_read_newline" ] &&
		   { [ -z "$__json_read_opt_n" ] || [ "${#__json_l}" -lt 9 ]; }; then
			__json_l="$__json_l$__json_lf"
		fi

		__shell_string_foreach_prepare __json_l

		while __shell_string_foreach_continue __json_l; do
			__shell_string_foreach_char __json_c __json_l
			__shell_string_foreach_iter __json_l

			__json_char || [ $? -ne $JSON_FOREACH_ABORT ] ||
				return 0
		done
	done
	__json_c=''
	__json_char || [ $? -ne $JSON_FOREACH_ABORT ] ||
		return 0
	if [ "$__json_state" != 'done' ]; then
		local stack
		__json_get_stack
		fatal "unfinished json: $stack"
	fi
}

__json_char()
{
	while :; do
		case "$__json_state" in
			'value')
				case "$__json_c" in
					'	'|' '|"$__json_lf"|"$__json_cr"|'')
						break
						;;
				esac
				__json_pop_state 'value'
				case "$__json_c" in
					'[')
						__json_push_name 0
						__json_push_state 'array'
						;;
					'{')
						__json_push_state 'object'
						;;
					'"')
						__json_push_state 'string'
						__json_buffer=
						;;
					0)
						__json_push_state 'number-leading-zero'
						__json_buffer="$__json_c"
						;;
					[1-9])
						__json_push_state 'number-leading-nonzero'
						__json_buffer="$__json_c"
						;;
					'-')
						__json_push_state 'number-negative'
						__json_buffer="$__json_c"
						;;
					't'|'f')
						__json_push_state 'boolean'
						__json_buffer=
						continue
						;;
					'n')
						__json_push_state 'null'
						__json_buffer=
						continue
						;;
					*)
						fatal "expected value, got '$__json_c'"
						;;
				esac
				;;
			'array')
				case "$__json_c" in
					'	'|' '|"$__json_lf"|"$__json_cr")
						;;
					']')
						__json_pop_name
						__json_pop_state 'array'
						;;
					*)
						__json_push_state 'array-more'
						__json_push_state 'value'
						continue
						;;
				esac
				;;
			'array-more')
				case "$__json_c" in
					'	'|' '|"$__json_lf"|"$__json_cr")
						;;
					']')
						__json_pop_name
						__json_pop_state 'array-more'
						__json_pop_state 'array'
						;;
					',')
						local last

						json_get_last_name last
						__json_pop_name
						__json_push_name $(( $last + 1 ))

						__json_push_state 'value'
						;;
					*)
						fatal "$__json_state unexpected '$__json_c'"
						;;
				esac
				;;
			'object')
				case "$__json_c" in
					'	'|' '|"$__json_lf"|"$__json_cr")
						;;
					'"')
						__json_handle=
						__json_push_state 'object-more'
						__json_push_state 'object-name'
						continue
						;;
					'}')
						__json_pop_state 'object'
						;;
					*)
						fatal "$__json_state unexpected '$__json_c'"
						;;
				esac
				;;
			'object-name')
				case "$__json_c" in
					'	'|' '|"$__json_lf"|"$__json_cr")
						;;
					'"')
						__json_pop_state 'object-name'
						__json_push_state 'object-colon'
						__json_push_state 'string'
						__json_buffer=
						;;
					*)
						fatal "$__json_state unexpected '$__json_c'"
						;;
				esac
				;;
			'object-colon')
				case "$__json_c" in
					'	'|' '|"$__json_lf"|"$__json_cr")
						;;
					':')
						__json_handle=1
						__json_push_name "$__json_buffer"
						__json_pop_state 'object-colon'
						__json_push_state 'value'
						;;
					*)
						fatal "$__json_state unexpected '$__json_c'"
						;;
				esac
				;;
			'object-more')
				case "$__json_c" in
					'	'|' '|"$__json_lf"|"$__json_cr")
						;;
					'}')
						__json_pop_name
						__json_pop_state 'object-more'
						__json_pop_state 'object'
						;;
					',')
						__json_pop_name
						__json_handle=
						__json_push_state 'object-name'
						;;
					*)
						fatal "$__json_state unexpected '$__json_c'"
						;;
				esac
				;;
			'boolean')
				# Boolean values are multicharacter literals but they're unique
				# from their first character.  This means the eventual value is
				# already known when the "boolean" state is entered so we can
				# raise syntax errors as soon as the input goes south.
				case "$__json_buffer$__json_c" in
					't'|'tr'|'tru'|'f'|'fa'|'fal'|'fals')
						__json_buffer="$__json_buffer$__json_c"
						;;
					'true'|'false')
						__json_pop_state 'boolean'
						__json_handle boolean "$__json_buffer$__json_c" || [ $? -ne $JSON_FOREACH_ABORT ] ||
							return $JSON_FOREACH_ABORT
						;;
					*)
						fatal "$__json_state unexpected '$__json_c'"
						;;
				esac
				;;
			'null')
				# Null values work exactly like boolean values.  See above.
				case "$__json_buffer$__json_c" in
					'n'|'nu'|'nul')
						__json_buffer="$__json_buffer$__json_c"
						;;
					'null')
						__json_pop_state 'null'
						__json_handle null "$__json_buffer$__json_c" || [ $? -ne $JSON_FOREACH_ABORT ] ||
							return $JSON_FOREACH_ABORT
						;;
					*)
						fatal "$__json_state unexpected '$__json_c'"
						;;
				esac
				;;
			'string')
				case "$__json_c" in
					[$__json_controls]) fatal "unescaped control character in string" ;;
				esac
				# A consumed escape must not escape the next character,
				# even when the escaped character is a backslash.
				# shellcheck disable=SC1003
				case "$__json_escape$__json_c" in
					'\"'|'\/'|'\\')
						__json_buffer="$__json_buffer$__json_c"
						__json_escape=
						;;
					'\b'|'\f'|'\n'|'\r'|'\t')
						__json_buffer="$__json_buffer\\$__json_c"
						__json_escape=
						;;
					'\u')
						__json_buffer="$__json_buffer\\u"
						__json_escape=
						__json_unicode=4
						__json_push_state 'string-unicode'
						;;
					'\'?*)
						fatal "invalid string escape: '$__json_c'"
						;;
					*'"')
						__json_pop_state 'string'
						__json_handle string "$__json_buffer" || [ $? -ne $JSON_FOREACH_ABORT ] ||
							return $JSON_FOREACH_ABORT
						;;
					*'\')
						__json_escape='\'
						;;
					*)
						__json_buffer="$__json_buffer$__json_c"
						__json_escape=
						;;
				esac
				;;
			'string-unicode')
				case "$__json_c" in
					[0-9a-fA-F])
						__json_buffer="$__json_buffer$__json_c"
						__json_unicode=$(( __json_unicode - 1 ))
						[ "$__json_unicode" -ne 0 ] || __json_pop_state 'string-unicode'
						;;
					*)
						fatal "expected hexadecimal digit, got '$__json_c'"
						;;
				esac
				;;
			'number-negative')
				# This is an entrypoint into parsing a number, used when
				# the first character consumed is a '-'.  From here, a number
				# may progress to the "number-leading-nonzero" or
				# "number-leading-zero" states.
				__json_pop_state 'number-negative'
				case "$__json_c" in
					'0')
						__json_push_state 'number-leading-zero'
						__json_buffer="$__json_buffer$__json_c"
						;;
					[1-9])
						__json_push_state 'number-leading-nonzero'
						__json_buffer="$__json_buffer$__json_c"
						;;
					*)
						fatal "expected digit after minus, got '$__json_c'"
						;;
				esac
				;;
			'number-leading-zero')
				# This is an entrypoint into parsing a number, used when
				# the first digit consumed is zero.  From here, a number
				# may remain zero, become a floating-point number by
				# consuming a '.', or become scientific-notation by consuming
				# an 'E' or 'e'.
				__json_pop_state 'number-leading-zero'
				case "$__json_c" in
					'.')
						__json_push_state 'number-fraction'
						__json_buffer="$__json_buffer$__json_c"
						;;
					[eE])
						__json_push_state 'number-sci'
						__json_buffer="$__json_buffer$__json_c"
						;;
					[0-9])
						fatal "$__json_state unexpected '$__json_buffer$__json_c'"
						;;
					*)
						__json_handle number "$__json_buffer" || [ $? -ne $JSON_FOREACH_ABORT ] ||
							return $JSON_FOREACH_ABORT
						__json_buffer=
						continue
						;;
				esac
				;;
			'number-leading-nonzero')
				# This is an entrypoint into parsing a number, used when
				# the first digit consumed is non-zero.  From here, a number
				# may continue on a positive integer, become a floating-point
				# number by consuming a '.', or become scientific-notation by
				# consuming an 'E' or 'e'.
				case "$__json_c" in
					'.')
						__json_pop_state 'number-leading-nonzero'
						__json_push_state 'number-fraction'
						__json_buffer="$__json_buffer$__json_c"
						;;
					[eE])
						__json_pop_state 'number-leading-nonzero'
						__json_push_state 'number-sci'
						__json_buffer="$__json_buffer$__json_c"
						;;
					[0-9])
						__json_buffer="$__json_buffer$__json_c"
						;;
					*)
						__json_pop_state 'number-leading-nonzero'
						__json_handle number "$__json_buffer" || [ $? -ne $JSON_FOREACH_ABORT ] ||
							return $JSON_FOREACH_ABORT
						__json_buffer=
						continue
						;;
				esac
				;;
			'number-fraction'|'number-sci-first')
				case "$__json_c" in
					[0-9])
						local next='number-float'
						[ "$__json_state" != number-sci-first ] || next='number-sci-digits'
						__json_pop_state
						__json_push_state "$next"
						__json_buffer="$__json_buffer$__json_c"
						;;
					*)
						fatal "$__json_state expected digit, got '$__json_c'"
						;;
				esac
				;;
			'number-float')
				# Numbers that encounter a '.' become floating point and may
				# continue consuming digits forever or may become
				# scientific-notation.
				case "$__json_c" in
					[eE])
						__json_pop_state 'number-float'
						__json_push_state 'number-sci'
						__json_buffer="$__json_buffer$__json_c"
						;;
					[0-9])
						__json_buffer="$__json_buffer$__json_c"
						;;
					*)
						__json_pop_state 'number-float'
						__json_handle number "$__json_buffer" || [ $? -ne $JSON_FOREACH_ABORT ] ||
							return $JSON_FOREACH_ABORT
						__json_buffer=
						continue
						;;
				esac
				;;
			'number-sci')
				# Numbers that encounter an 'E' or 'e' become
				# scientific-notation and consume digits, optionally prefixed
				# by a '+' or '-', forever.  The actual consumption is
				# delegated to the "number-sci-first" and "number-sci-digits"
				# states.  Any other character immediately following the 'E'
				# or 'e' is a syntax error.
				__json_pop_state 'number-sci'
				case "$__json_c" in
					'+'|'-')
						__json_push_state 'number-sci-first'
						__json_buffer="$__json_buffer$__json_c"
						;;
					[0-9])
						__json_push_state 'number-sci-digits'
						__json_buffer="$__json_buffer$__json_c"
						;;
					*)
						fatal "$__json_state unexpected '$__json_buffer$__json_c'"
						;;
				esac
				;;
			'number-sci-digits')
				# Once in these states, numbers may consume digits forever.
				case "$__json_c" in
					[0-9])
						__json_buffer="$__json_buffer$__json_c"
						;;
					*)
						__json_pop_state 'number-sci-digits'
						__json_handle number "$__json_buffer" || [ $? -ne $JSON_FOREACH_ABORT ] ||
							return $JSON_FOREACH_ABORT
						__json_buffer=
						continue
						;;
				esac
				;;
			'done')
				case "$__json_c" in
					'	'|' '|"$__json_lf"|"$__json_cr"|'') ;;
					*) fatal "unexpected data after json: '$__json_c'" ;;
				esac
				;;
		esac

		__json_debug "c='$__json_c'"
		break
	done
}

fi #__included_shell_json
# vim: tw=200
