Skip to content

Commit

Permalink
using esceval for shell-escaped command strings
Browse files Browse the repository at this point in the history
errors have been discussed in more detail in #37 and should be fixed for
most use cases with this patch.
  • Loading branch information
jonashoechst committed Nov 2, 2021
1 parent 12e8ae4 commit 5e5afea
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
66 changes: 66 additions & 0 deletions modules/esceval.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# SPDX-License-Identifier: 0BSD
# Copyright 2014 Alexander Kozhevnikov <mentalisttraceur@gmail.com>

# On 2019-04-25, this script was compatible with Bourne and POSIX shells.
# EXCEPT for the following exceptions:
# Function declarations (First appeared in SVR2 Bourne shells in 1984).
# command built-in (not supported in really old Bourne shells)

if command -v esceval 1>/dev/null 2>&1
then
:
else
if (eval 'echo ${A%%a} ${A#a}' 1>/dev/null 2>&1)
then
eval 'esceval()
{
case $# in 0) return 0; esac
(
while :
do
escaped=\'\''
unescaped=$1
while :
do
case $unescaped in
*\'\''*)
escaped=$escaped${unescaped%%\'\''*}"'"'\''"'"
unescaped=${unescaped#*\'\''}
;;
*)
break
esac
done
escaped=$escaped$unescaped\'\''
shift
case $# in 0) break; esac
printf "%s " "$escaped" || return $?
done
printf "%s\n" "$escaped"
)
}'
else
esceval()
{
case $# in 0) return 0; esac
(
b='\\'
while :
do
escaped=`
printf '%s\n' "$1" \
| sed "
s/'/'$b''/g
1 s/^/'/
$ s/$/'/
"
` || return $?
shift
case $# in 0) break; esac
printf '%s ' "$escaped" || return $?
done
printf '%s\n' "$escaped"
)
}
fi
fi
1 change: 1 addition & 0 deletions pimod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pushd "$(dirname "$0")" > /dev/null

. ./modules/chroot.sh
. ./modules/error.sh
. ./modules/esceval.sh
. ./modules/from_remote.sh
. ./modules/mount.sh
. ./modules/path.sh
Expand Down
2 changes: 1 addition & 1 deletion stages/30-chroot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ WORKDIR() {
RUN() {
echo -e "\033[0;32m### RUN ${*}\033[0m"
PATH=${GUEST_PATH} chroot "${CHROOT_MOUNT}" \
/bin/sh -c "cd ${WORKDIR_PATH}; $(printf ' %q' "$@")"
/bin/sh -c "cd ${WORKDIR_PATH}; $(esceval "$@")"
}

# HOST executed a command on the local host and can be used to prepare files,
Expand Down

0 comments on commit 5e5afea

Please sign in to comment.