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