fix: Interpret most escape sequence within JSON strings

master
Edwin Kofler 10 months ago committed by Joseph Werle
parent 91ef3ff1c8
commit 9c34a8f0d8

@ -164,7 +164,7 @@ bpkg_install () {
did_fail=0 did_fail=0
continue continue
fi fi
## Check each remote in order ## Check each remote in order
local i=0 local i=0
for remote in "${BPKG_REMOTES[@]}"; do for remote in "${BPKG_REMOTES[@]}"; do
@ -381,8 +381,9 @@ bpkg_install_from_remote () {
if (( 1 == needs_global )); then if (( 1 == needs_global )); then
if (( has_pkg_json > 0 )); then if (( has_pkg_json > 0 )); then
## install bin if needed ## install bin if needed
build="$(echo -n "$json" | bpkg-json -b | grep '\["install"\]' | awk '{$1=""; print $0 }' | tr -d '\"')" build="$(echo -n "$json" | bpkg-json -b -f='"install"')"
build="$(echo -n "$build" | sed -e 's/^ *//' -e 's/ *$//')" build=${build#\"}
build=${build%\"}
fi fi
if [[ -z "$build" ]]; then if [[ -z "$build" ]]; then

@ -8,18 +8,36 @@ throw () {
BRIEF=0 BRIEF=0
LEAFONLY=0 LEAFONLY=0
PRUNE=0 PRUNE=0
FILTER=
usage() { usage() {
echo echo
echo "Usage: JSON.sh [-b] [-l] [-p] [-h]" echo "Usage: JSON.sh [-b] [-l] [-p] [-f=<filter>] [-h]"
echo echo
echo "-p - Prune empty. Exclude fields with empty values." echo "-p - Prune empty. Exclude fields with empty values."
echo "-l - Leaf only. Only show leaf nodes, which stops data duplication." echo "-l - Leaf only. Only show leaf nodes, which stops data duplication."
echo "-b - Brief. Combines 'Leaf only' and 'Prune empty' options." echo "-b - Brief. Combines 'Leaf only' and 'Prune empty' options."
echo "-f - Filter. Only print the values for the keys that match the specified filter"
echo "-h - This help text." echo "-h - This help text."
echo echo
} }
escape_string() {
local str=$1
str=${str//\\\"/\"}
str=${str//\\\\/\\}
str=${str//\\\//\/}
str=${str//\\b/$'\b'}
str=${str//\\f/$'\f'}
str=${str//\\n/$'\n'}
str=${str//\\r/$'\r'}
str=${str//\\t/$'\t'}
# TODO: unicode escaping
REPLY=$str
}
parse_options() { parse_options() {
set -- "$@" set -- "$@"
local ARGN=$# local ARGN=$#
@ -37,6 +55,8 @@ parse_options() {
;; ;;
-p) PRUNE=1 -p) PRUNE=1
;; ;;
-f=*) FILTER=${1#-f=}
;;
?*) echo "ERROR: Unknown option." ?*) echo "ERROR: Unknown option."
usage usage
exit 0 exit 0
@ -126,7 +146,7 @@ parse_object () {
while : while :
do do
case "$token" in case "$token" in
'"'*'"') key=$token ;; '"'*'"') escape_string "$token"; key=$REPLY ;;
*) throw "EXPECTED string GOT ${token:-EOF}" ;; *) throw "EXPECTED string GOT ${token:-EOF}" ;;
esac esac
read -r token read -r token
@ -158,7 +178,7 @@ parse_value () {
'[') parse_array "$jpath" ;; '[') parse_array "$jpath" ;;
# At this point, the only valid single-character tokens are digits. # At this point, the only valid single-character tokens are digits.
''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;; ''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
*) value=$token *) escape_string "$token"; value=$REPLY
isleaf=1 isleaf=1
[ "$value" = '""' ] && isempty=1 [ "$value" = '""' ] && isempty=1
;; ;;
@ -169,8 +189,17 @@ parse_value () {
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 1 ] && [ "$isempty" -eq 0 ] && print=1 [ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 1 ] && [ "$isempty" -eq 0 ] && print=1
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && \ [ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && \
[ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1 [ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1
[ "$print" -eq 1 ] && printf "[%s]\t%s\n" "$jpath" "$value"
: if [ "$print" -eq 1 ]; then
# FILTER
if [ -n "$FILTER" ]; then
if [ "$FILTER" = "$jpath" ]; then
printf '%s\n' "$value"
fi
else
printf "[%s]\t%s\n" "$jpath" "$value"
fi
fi
} }
parse () { parse () {

Loading…
Cancel
Save