From 8c65a6637a2efc3a34f3782470d382bfd0315f21 Mon Sep 17 00:00:00 2001 From: Neil Roza Date: Fri, 13 Sep 2019 21:33:14 +0000 Subject: [PATCH] script & args -> startup command, fix #171 * edit `makeself-header`: * change every `script` & `scriptargs` to `startup_command` * use `quote` (implemented in `makeself.sh`) to assign `startup_command` * change diagnostic references of "script" to "command" * edit `makeself.sh`: * add Rich Felker's `quote` and `save` * move first instructions to follow last function definition * use `save` to assign `MS_COMMAND` * use `save` to assign `STARTUP_COMMAND` * add `test/startupcommandtest` to test weird characters in files and startup commands --- makeself-header.sh | 27 +++++++++-------- makeself.sh | 36 ++++++++++++----------- test/startupcommandtest | 65 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 31 deletions(-) create mode 100644 test/startupcommandtest diff --git a/makeself-header.sh b/makeself-header.sh index 296733f0..748d17eb 100755 --- a/makeself-header.sh +++ b/makeself-header.sh @@ -15,8 +15,7 @@ TMPROOT=\${TMPDIR:=/tmp} USER_PWD="\$PWD"; export USER_PWD label="$LABEL" -script="$SCRIPT" -scriptargs="$SCRIPTARGS" +startup_command=$(quote "$STARTUP_COMMAND") licensetxt="$LICENSE" helpheader='$HELPHEADER' targetdir="$archdirname" @@ -311,10 +310,10 @@ do fi echo Date of packaging: $DATE echo Built with Makeself version $MS_VERSION on $OSTYPE - echo Build command was: "$MS_COMMAND" - if test x"\$script" != x; then - echo Script run after extraction: - echo " " \$script \$scriptargs + echo Build command was: $MS_COMMAND + if test x"\$startup_command" != x; then + echo Command run after extraction: + echo " \$(echo \$startup_command | xargs)" fi if test x"$copy" = xcopy; then echo "Archive will copy itself to a temporary location" @@ -331,8 +330,7 @@ do ;; --dumpconf) echo LABEL=\"\$label\" - echo SCRIPT=\"\$script\" - echo SCRIPTARGS=\"\$scriptargs\" + echo STARTUP_COMMAND=\"\$startup_command\" echo archdirname=\"$archdirname\" echo KEEP=$KEEP echo NOOVERWRITE=$NOOVERWRITE @@ -590,12 +588,11 @@ fi cd "\$tmpdir" res=0 -if test x"\$script" != x; then +if test x"\$startup_command" != x; then if test x"\$export_conf" = x"y"; then MS_BUNDLE="\$0" MS_LABEL="\$label" - MS_SCRIPT="\$script" - MS_SCRIPTARGS="\$scriptargs" + MS_STARTUP_COMMAND="\$startup_command" MS_ARCHDIRNAME="\$archdirname" MS_KEEP="\$KEEP" MS_NOOVERWRITE="\$NOOVERWRITE" @@ -608,13 +605,15 @@ if test x"\$script" != x; then MS_Printf "OK to execute: \$script \$scriptargs \$* ? [Y/n] " read yn if test x"\$yn" = x -o x"\$yn" = xy -o x"\$yn" = xY; then - eval "\"\$script\" \$scriptargs \"\\\$@\""; res=\$?; + eval "\$startup_command \"\\\$@\""; res=\$? fi else - eval "\"\$script\" \$scriptargs \"\\\$@\""; res=\$? + eval "\$startup_command \"\\\$@\""; res=\$? fi if test "\$res" -ne 0; then - test x"\$verbose" = xy && echo "The program '\$script' returned an error code (\$res)" >&2 + if test x"\$verbose" = xy; then + echo "The command '\$startup_command' returned an error code (\$res)" >&2 + fi fi fi if test x"\$keep" = xn; then diff --git a/makeself.sh b/makeself.sh index 6d8309c9..ab7c5b96 100755 --- a/makeself.sh +++ b/makeself.sh @@ -76,22 +76,16 @@ # Self-extracting archives created with this script are explictly NOT released under the term of the GPL # -MS_VERSION=2.4.0 -MS_COMMAND="$0" -unset CDPATH +# Procedures -for f in "${1+"$@"}"; do - MS_COMMAND="$MS_COMMAND \\\\ - \\\"$f\\\"" -done +# https://www.etalabs.net/sh_tricks.html, "Shell-quoting arbitrary strings" +quote() { printf %s\\n "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/" ; } -# For Solaris systems -if test -d /usr/xpg4/bin; then - PATH=/usr/xpg4/bin:$PATH - export PATH -fi - -# Procedures +# https://www.etalabs.net/sh_tricks.html, "Working with arrays" +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} MS_Usage() { @@ -163,6 +157,16 @@ MS_Usage() exit 1 } +MS_VERSION=2.4.0 +MS_COMMAND="$(save $0 $@)" +unset CDPATH + +# For Solaris systems +if test -d /usr/xpg4/bin; then + PATH=/usr/xpg4/bin:$PATH + export PATH +fi + # Default settings if type gzip 2>&1 > /dev/null; then COMPRESS=gzip @@ -459,10 +463,8 @@ else fi LABEL="$3" - SCRIPT="$4" - test "x$SCRIPT" = x || shift 1 shift 3 - SCRIPTARGS="$*" + STARTUP_COMMAND="$(save $@)" fi if test "$KEEP" = n -a "$CURRENT" = y; then diff --git a/test/startupcommandtest b/test/startupcommandtest new file mode 100644 index 00000000..35bcdb08 --- /dev/null +++ b/test/startupcommandtest @@ -0,0 +1,65 @@ +#!/bin/bash + +THIS="$(readlink -f "$0")" +HERE="$(dirname "${THIS}")" +SUT="$(dirname "${HERE}")/makeself.sh" + +testStartupCommand() { + ( + export TMPDIR="$(mktemp -d "${PWD}/tmpdir.XXXXXX")" + + local weirdname_list="$(mktemp -t weirdname_list.XXXXXX)" + cat >"${weirdname_list}" <<'EOF' +_!_exclamation_mark +_"_quotation_mark +_#_octothorp +_$_dollar +_%_percent +_&_ampersand +_'_apostrophe +_(_left_parenthesis +_)_right_parenthesis +_*_asterisk +_+_plus +_,_comma +_-_hyphen +_._full_stop +_:_colon +_;_semicolon +_<_less_than +_=_equals +_>_greater_than +_?_interrogative +_@_ampersat +_[_left_square_bracket +_\_reverse_solidus +_]_right_square_bracket +_^_caret +_{_left_bracket +_|_vertical_bar +_}_right_bracket +EOF + + set -eu + while read weirdname; do + local archive_dir="$(mktemp -dt archive_dir.XXXXXX)" + echo "${weirdname}" | tee "${archive_dir}/${weirdname}.txt" + local file_name="$(mktemp -ut "file_name.XXXXXX")" + "${SUT}" \ + --nocomp --nox11 \ + "${archive_dir}" "${file_name}" "The Label" "ls -lah ${weirdname}.txt" + assertEqual $? 0 + "${file_name}" --list + assertEqual $? 0 + "${file_name}" --info + assertEqual $? 0 + "${file_name}" --dumpconf + assertEqual $? 0 + "${file_name}" + assertEqual $? 0 + done <"${weirdname_list}" + rm -rf "${TMPDIR}" + ) +} + +source "${HERE}/bashunit/bashunit.bash"