Archived
1
0
Disbranĉigi 0
Ĉi tiu deponejo arĥiviĝis je 2024-01-28. Vi povas vidi kaj elŝuti dosierojn, sed ne povas puŝi nek raporti problemojn nek tirpeti.
json.sh/json.sh

154 lines
2.7 KiB
Bash
Raw Normal View History

2017-03-25 13:58:46 -05:00
#!/bin/sh -ex
# WIP JSON parser
2017-03-25 13:11:04 -05:00
2017-03-25 13:58:46 -05:00
JGROUPS=''
GROUPORVAR=0
NEWWORD=0
2017-03-25 13:11:04 -05:00
2017-03-25 13:58:46 -05:00
endword() {
NEWWORD=0
if [ $GROUPORVAR -eq 1 ]
then
echo "${JGROUPS}/$LASTWORD = $CURWORD"
GROUPORVAR=0
else
echo "${JGROUPS}/$CURWORD"
fi
LASTWORD=$CURWORD
CURWORD=''
TYPE=''
}
2017-03-25 13:11:04 -05:00
2017-03-25 13:58:46 -05:00
startword() {
NEWWORD=1
2017-03-25 13:11:04 -05:00
}
2017-03-25 13:58:46 -05:00
addword() {
CURWORD="${CURWORD}${1}"
2017-03-25 13:11:04 -05:00
}
2017-03-25 13:58:46 -05:00
json() {
2017-03-25 13:11:04 -05:00
for char in $(cat $1 | sed 's/\(.\)/\1 /g')
do
2017-03-25 13:58:46 -05:00
case "$char" in
' ')
if [ $NEWWORD -eq 1 ] && [ $TYPE == "string" ]
then
addword $char
fi
;;
# Works in bash, not in sh.
# [[:alpha:]])
[ABCDEFGHIJKLMNOPQRSTUVWXYZ])
addword $char
;;
[abcdefghijklmnopqrstuvwxyz])
addword $char
;;
# [[:digit:]])
[0123456789])
if [ $NEWWORD -eq 1 ]
then
addword $char
else
TYPE="number"
startword
addword $char
fi
;;
[,])
if [ $NEWWORD -eq 1 ] && [ $TYPE == "string" ]
then
addword $char
elif [ $NEWWORD -eq 1 ] && [ $TYPE == "number" ]
then
endword
fi
;;
"\"")
2017-03-25 13:58:46 -05:00
if [ $NEWWORD -eq 1 ]
then
endword
else
TYPE="string"
startword
fi
;;
':')
GROUPORVAR=1
;;
'{')
if [ $GROUPORVAR -eq 1 ]
then
CURGROUP="${LASTWORD}"
JGROUPS="${JGROUPS}/${CURGROUP}"
GROUPORVAR=0
fi
;;
'}')
if [ $NEWWORD -eq 1 ] && [ $TYPE == "number" ]
then
endword
fi
JGROUPS=$(echo $JGROUPS | sed 's@/'$CURGROUP'$@@')
CURGROUP=$(echo $JGROUPS | grep -oE '[^/]+$')
;;
esac
done < "$1"
}
varmatch=0
objmatch=0
silmatch=0
valmatch=0
match=''
arguments=''
na=0
for argument in $@
do
case $argument in
-v)
na=1
varmatch=1
;;
-V)
na=1
valmatch=1
;;
-s)
na=1
silmatch=1
;;
-o)
na=1
objmatch=1
;;
*)
if [ $na -eq 1 ] && [ $objmatch -eq 1 ]
then
match="$argument\$"
elif [ $na -eq 1 ] && [ $varmatch -eq 1 ]
then
match="$argument\ ="
elif [ $na -eq 1 ] && [ $silmatch -eq 1 ]
then
match="$argument\ ="
elif [ $na -eq 1 ] && [ $valmatch -eq 1 ]
then
match="=\ $argument\$"
else
file="$argument"
fi
na=0
;;
esac
done
if [ $silmatch -eq 1 ]
then
json $file | grep "$match" | sed 's/.*= //'
else
json $file | grep "$match"
fi