Created
March 4, 2021 15:48
-
-
Save haraldh/a03668ae6438fd40c60488a42de419de to your computer and use it in GitHub Desktop.
getargs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_dogetarg() { | |
local _search_key | |
local _in_quote | |
local _key | |
local _val | |
local _equals | |
local _STATE | |
local _lastchar | |
local _want_value | |
local _multi_args | |
local _final_value | |
local _final_value_unset | |
_multi_args="$2" | |
if [ -n "${1#*=}" -a "${1#*=*}" != "${1}" ]; then | |
_want_value="${1#*=}" | |
fi | |
_search_key="${1%=*}" | |
CMDLINE=${CMDLINE:-$(getcmdline)} | |
_STATE=start | |
echo -n "$CMDLINE" | while read -r -N 1 _a && [ -n "$_a" ] || _a="EOF"; do | |
if [ $_STATE = start ]; then | |
_key="" | |
_val="" | |
_in_quote=0 | |
_equals=0 | |
_STATE=key | |
if [ "$_a" = "\"" ]; then | |
_in_quote=1 | |
continue | |
fi | |
fi | |
if [ "$_STATE" = "key" ]; then | |
if [ "$_a" = "EOF" ]; then | |
if [ "$_search_key" = "$_key" ]; then | |
_final_value_unset=1 | |
fi | |
if [ "$_final_value_unset" = "1" ] && [ -z "$_want_value" ]; then | |
return 0 | |
fi | |
return 1 | |
fi | |
if [ "$_a" = " " ] && [ "$_in_quote" != "1" ]; then | |
if [ "$_search_key" = "$_key" ]; then | |
_final_value="" | |
_final_value_unset=1 | |
fi | |
_STATE=start | |
elif [ "$_a" = "=" ] && [ "$_equals" != "1" ]; then | |
if [ "$_lastchar" = "\"" ] && [ "$_in_quote" != "1" ]; then | |
_key="${_key}\"" | |
fi | |
_equals=1 | |
_STATE=val | |
elif [ "$_a" = "\"" ] && [ "$_in_quote" = "1" ]; then | |
_in_quote=0 | |
else | |
_key="${_key}${_a}" | |
fi | |
elif [ "$_STATE" = "val" ]; then | |
if [ "$_a" = "EOF" ]; then | |
if [ "$_search_key" = "$_key" ]; then | |
_final_value=${_val%"\""} | |
_final_value_unset=0 | |
if [ -n "$_multi_args" ]; then | |
echo "$_final_value" | |
return 0 | |
fi | |
fi | |
if [ -z "$_want_value" ]; then | |
if [ -n "$_final_value" ]; then | |
if [ -z "$_multi_args" ]; then | |
echo "$_final_value" | |
fi | |
return 0 | |
fi | |
if [ "$_final_value_unset" = "1" ]; then | |
return 0 | |
fi | |
return 1 | |
fi | |
if [ "$_final_value_unset" = "1" ]; then | |
return 1 | |
fi | |
if [ "$_final_value" = "$_want_value" ]; then | |
return 0 | |
fi | |
return 1 | |
fi | |
if [ "$_equals" = "1" ]; then | |
_equals=0 | |
if [ "$_a" = "\"" ]; then | |
if [ "$_in_quote" = "1" ]; then | |
_in_quote=0 | |
else | |
_in_quote=1 | |
fi | |
continue | |
fi | |
fi | |
if [ "$_a" = " " ] && [ "$_in_quote" != "1" ]; then | |
if [ "$_search_key" = "$_key" ]; then | |
_final_value=${_val%"\""} | |
_final_value_unset=0 | |
if [ -n "$_multi_args" ]; then | |
echo "$_final_value" | |
fi | |
fi | |
_STATE=start | |
else | |
_val="${_val}${_a}" | |
if [ "$_a" = "\"" ]; then | |
if [ "$_in_quote" = "1" ]; then | |
_in_quote=0 | |
else | |
_in_quote=1 | |
fi | |
fi | |
fi | |
fi | |
_lastchar="$_a" | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment