redish/lib/cli.sh

67 lines
932 B
Bash

get_cmd() {
if (( $# )); then
IFS= read -ep "$(build_prompt)" "$1"
fi
}
parse_cmd() {
local cmd=( ) arg
declare -n cmd_sent=$1
declare -n serialized=$2
route_cmd "$3"
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
while (( ${#line} )); do
local next=${line:0:1}
if [[ $next != [[:space:]] ]]; then
arg+=$next
line=${line:1}
else
break
fi
done
if ! (( ${#cmd[@]} )); then
cmd_sent=${arg^^}
cmd+=( "\$${#arg}"$'\r\n'"$cmd_sent"$'\r\n' )
else
cmd+=( "\$${#arg}"$'\r\n'"$arg"$'\r\n' )
fi
route_cmd "$line"
}