Skip to content

Commit

Permalink
We are now truly metacharacter safe. However, there is an internal
Browse files Browse the repository at this point in the history
function, that fixes embedded "'" inside variables.
  • Loading branch information
Adam Heath committed Mar 6, 2001
1 parent 1478696 commit 23a3cf7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
22 changes: 20 additions & 2 deletions shoop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ _shoopcommand_header_='
_shoopcommand_footer_='
esac
'
_shoop_tr() {
local char="$1" replace="$2" bad rest tmp left; shift 2
bad="$@" rest="$@" out=""
while :; do
: $rest
: $char
tmp="${rest%$char*}"
if [ ${#tmp} -eq ${#rest} ]; then
break
fi
left="${rest#$tmp$char}"
out="$replace$left$out"
rest="$tmp"
done
out="$rest$out"
}
_shoop () {
local cmd=$1; shift
local TRUEOBJ=$1 TRYOBJ=$2 METH=$3 TRUEMETH=$1_$3 TRYMETH=$2_$3 LASTMETH=$METH GETMETH THIS CLASS
Expand Down Expand Up @@ -50,12 +66,14 @@ _shoop () {
fi
if [ "$varmeth" = = ]; then
if [ "$append" ];then set -- "$(eval eval "\$_shoop_$TRUEMETH") $@"; fi
if [ ! "$quiet" ]; then eval 'echo -n '\'"$@"\'; fi
local out
_shoop_tr "'" "'\''" "$@"
if [ ! "$quiet" ]; then eval 'echo -n '\'"$out"\'; fi

if [ $# -eq 0 ]; then
return
fi
eval "_shoop_$TRUEMETH=\"echo -n '\$@'\"
eval "_shoop_$TRUEMETH=\"echo -n '\$out'\"
_shooptype_$TRUEMETH=variable"
else
if [ "$quiet" ]; then echo "Invalid modifier(q) on assignment!($TRUEOBJ.$METH)" >&2; fi
Expand Down
20 changes: 17 additions & 3 deletions t/metachars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@ ok "" 0 'a' OBJECT . var
ok "" 0 '' OBJECT . method : "echo a"
ok "" 0 'a' OBJECT . method

c=4
stuff="a'b'c'"
stuff_fixed="a'\''b'\''c'\''"
ok "_shoop_tr" 0 "$stuff_fixed" eval '_shoop_tr "'\''" "'\''\\'\'\''" "$stuff"; echo "$out"'

stuff="aaaaa'bbbbbccccc"
stuff_fixed="aaaaa'\''bbbbbccccc"
ok "_shoop_tr" 0 "$stuff_fixed" eval '_shoop_tr "'\''" "'\''\\'\'\''" "$stuff"; echo "$out"'

c=8
for meta in \` \~ \! \@ \# \$ \% \^ \& \* \( \) \{ \} \[ \] \| \\ \; \' \" \< \> \? ' '; do
# Let's make sure the testing framework can handle the metacharacters
ok "$meta" 0 "$meta" echo "$meta"

# Now let's test shoop
ok "set-$meta" 0 "a${meta}a" OBJECT . var = "a$(echo "$meta"|sed 's/'"'/'"'\\'"''/g")a"
ok "set-$meta" 0 "a${meta}a" OBJECT . var = "a${meta}a"
ok "get-$meta" 0 "a${meta}a" OBJECT . var

ok "method-$meta" 0 "" OBJECT . method : "echo 'a$(echo "$meta"|sed 's/'"'/'"'\\'"''/g")a'"
_shoop_tr "'" "'\\''" "$meta"
ok "method-$meta" 0 "" OBJECT . method : "echo 'a${out}a'"
ok "call-$meta" 0 "a${meta}a" OBJECT . method

c=$(($c+5))
allmeta="$allmeta$meta"
done

# All metachars at once
ok "set-all" 0 "$allmeta$allmeta$allmeta" OBJECT . var = "$allmeta$allmeta$allmeta"
ok "get-all" 0 "$allmeta$allmeta$allmeta" OBJECT . var

tests $c

0 comments on commit 23a3cf7

Please sign in to comment.