update comments and vars

This commit is contained in:
AJ ONeal 2019-06-02 02:44:32 -06:00
parent bca5333171
commit 0a52353444
1 changed files with 11 additions and 10 deletions

View File

@ -1,17 +1,18 @@
set -u set -u
set -e set -e
# ParseArgs will parse a string that contains quoted strings the same as bash does # ParseArgs will parse a string that contains quoted strings the same
# (same as most other *nix shells do). This is secure in the sense that it doesn't do any # as bash does (same as most other *nix shells do). This is secure in
# executing or interpeting. However, it also doesn't do any escaping, so you shouldn't pass # the sense that it doesn't do any executing or interpreting. However,
# these strings to shells without escaping them. # it also doesn't do any escaping, so you shouldn't pass these strings
# to shells without escaping them.
parseargs() { parseargs() {
notquote="-" notquote="-"
str=$1 str=$1
declare -a m=() declare -a args=()
s="" s=""
# Strip leading space, then trailing space, then add a terminating space. # Strip leading space, then trailing space, then end with space.
str="${str## }" str="${str## }"
str="${str%% }" str="${str%% }"
str+=" " str+=" "
@ -19,7 +20,7 @@ parseargs() {
last_quote="${notquote}" last_quote="${notquote}"
is_space="" is_space=""
n=$(( ${#str} - 1 )) n=$(( ${#str} - 1 ))
i=-1
for ((i=0;i<=$n;i+=1)); do for ((i=0;i<=$n;i+=1)); do
c="${str:$i:1}" c="${str:$i:1}"
@ -50,7 +51,7 @@ parseargs() {
continue continue
fi fi
is_space="true" is_space="true"
m+=("$s") args+=("$s")
s="" s=""
continue continue
fi fi
@ -60,11 +61,11 @@ parseargs() {
done done
if [ "$last_quote" != "$notquote" ]; then if [ "$last_quote" != "$notquote" ]; then
>&2 echo "error" >&2 echo "error: quote not terminated"
return 1 return 1
fi fi
for arg in "${m[@]}"; do for arg in "${args[@]}"; do
echo "$arg" echo "$arg"
done done
return 0 return 0