75 lines
1.1 KiB
Bash
75 lines
1.1 KiB
Bash
get_cmd() {
|
|
if (( $# )); then
|
|
IFS= read -ep "$(build_prompt)" "$1"
|
|
fi
|
|
}
|
|
|
|
parse_cmd() {
|
|
local cmd=( ) arg
|
|
declare -n serialized=$1
|
|
|
|
route_cmd "$2"
|
|
|
|
serialized=*${#cmd[@]}$'\r\n'
|
|
|
|
for arg in "${cmd[@]}"; do
|
|
serialized+=$arg
|
|
done
|
|
}
|
|
|
|
route_cmd() {
|
|
case $1 in
|
|
[[:space:]]*)
|
|
parse_ws "$1"
|
|
;;
|
|
"")
|
|
return 0
|
|
;;
|
|
*)
|
|
parse_word "$1"
|
|
esac
|
|
}
|
|
|
|
parse_ws() {
|
|
local line=$1
|
|
|
|
while (( ${#line} )) && [[ $line = [[:space:]]* ]]; do
|
|
line=${line:1}
|
|
done
|
|
|
|
route_cmd "$line"
|
|
}
|
|
|
|
parse_word() {
|
|
local arg line=$1 instr=0
|
|
|
|
while (( ${#line} )); do
|
|
local next=${line:0:1}
|
|
|
|
if ! (( instr )) && [[ $next = \" ]]; then
|
|
instr=1
|
|
line=${line:1}
|
|
elif (( instr )) && [[ $next = \" ]]; then
|
|
instr=0
|
|
line=${line:1}
|
|
elif (( instr )); then
|
|
arg+=$next
|
|
line=${line:1}
|
|
elif [[ $next != [[:space:]] ]]; then
|
|
arg+=$next
|
|
line=${line:1}
|
|
else
|
|
break
|
|
fi
|
|
done
|
|
|
|
if ! (( ${#cmd[@]} )); then
|
|
redis_cmd=( "${arg^^}" )
|
|
cmd=( "\$${#arg}"$'\r\n'"${arg^^}"$'\r\n' )
|
|
else
|
|
redis_cmd+=( "$arg" )
|
|
cmd+=( "\$${#arg}"$'\r\n'"$arg"$'\r\n' )
|
|
fi
|
|
|
|
route_cmd "$line"
|
|
}
|