diff --git a/lib/net.sh b/lib/net.sh index 9baef9a..4256469 100644 --- a/lib/net.sh +++ b/lib/net.sh @@ -11,3 +11,9 @@ exec {sock}<>/dev/tcp/"$host"/"$port" redis_send() { printf %s "$@" >&"$sock" } + +redis_recv() { + if (( $# )); then + IFS= read -rt 5 "$1" <&"$sock" + fi +} diff --git a/lib/serv.sh b/lib/serv.sh new file mode 100644 index 0000000..e6878cb --- /dev/null +++ b/lib/serv.sh @@ -0,0 +1,68 @@ +parse_res() { + local line=${1%$'\r'} + + case $line in + +*) + parse_simple "$line" + ;; + -*) + parse_err "$line" + ;; + :*) + parse_int "$line" + ;; + \$*) + parse_bulk "$line" + ;; + \**) + parse_arr "$line" + ;; + *) + parse_other "$line" + esac +} + +parse_simple() { + (( lines_left -= 1 )) + printf '%s\n' "${1:1}" +} + +parse_int() { + (( lines_left -= 1 )) + printf '(integer) %d\n' "${1:1}" +} + +parse_err() { + (( lines_left -= 1 )) + printf '(error) %s\n' "${1:1}" +} + +parse_bulk() { + bulk=1 +} + +parse_arr() { + lines_left=${1:1} + arr=1 +} + +parse_other() { + (( lines_left -= 1 )) + + if (( arr )); then + printf '%d) ' "$arr" + (( arr++ )) + fi + + if (( bulk )); then + printf '"' + fi + + printf %s "$1" + + if (( bulk )); then + printf '"' + fi + + printf '\n' +} diff --git a/redish-cli b/redish-cli index 3198d30..6b07ff3 100755 --- a/redish-cli +++ b/redish-cli @@ -30,4 +30,11 @@ fi while get_cmd line; do parse_cmd redis_cmd redis_line "$line" redis_send "$redis_line" + + lines_left=1 + bulk=0 arr=0 + + while (( lines_left )) && redis_recv line; do + parse_res "$line" + done done