redish/lib/cli.sh

61 lines
818 B
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'
serialized+=${cmd[0]^^}
for arg in "${cmd[@]:1}"; 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
cmd+=( "\$${#arg}"$'\r\n'"$arg"$'\r\n' )
route_cmd "$line"
}