From de1c0cf419fc4db4d616f4e660304e8e9a7e246f Mon Sep 17 00:00:00 2001 From: rillig Date: Sun, 24 Mar 2019 11:29:19 +0000 Subject: mk/tools: correctly quote arguments in the tool wrapper log Before, the tool arguments were written to the log as plain strings. Now the arguments are properly quoted, which makes it possible to replay the commands by copying them from the .work.log file. This only affects tools that are shell builtins (echo, true, false), get additional arguments (mkdir -p) or define a custom TOOLS_SCRIPT (pkg-config, to set an environment variable; or autotools). Tools that are symlinked to the real tool are not affected. The calls to the compiler are already properly logged since cwrappers takes care of that. This commit therefore makes the log entries for the compilers and the other tools more similar. --- mk/tools/create.mk | 19 +++++++++++-------- mk/tools/shquote.sh | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 mk/tools/shquote.sh (limited to 'mk/tools') diff --git a/mk/tools/create.mk b/mk/tools/create.mk index db2974f1ed8..719bce97a34 100644 --- a/mk/tools/create.mk +++ b/mk/tools/create.mk @@ -1,4 +1,4 @@ -# $NetBSD: create.mk,v 1.10 2019/03/24 08:40:07 rillig Exp $ +# $NetBSD: create.mk,v 1.11 2019/03/24 11:29:19 rillig Exp $ # # Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. # All rights reserved. @@ -149,7 +149,7 @@ ${TOOLS_CMD.${_t_}}: if ${TEST} -n ${TOOLS_SCRIPT.${_t_}:Q}""; then \ create=wrapper; \ script=${TOOLS_SCRIPT.${_t_}:Q}; \ - logprefix='"set args "$$@"; shift; "'; \ + logprefix='"set args$$shquoted_args; shift; "'; \ logmain=${TOOLS_SCRIPT.${_t_}:Q:Q}; \ logsuffix=''; \ elif ${TEST} -n ${TOOLS_PATH.${_t_}:Q}""; then \ @@ -158,7 +158,7 @@ ${TOOLS_CMD.${_t_}}: script=${TOOLS_SCRIPT_DFLT.${_t_}:Q}; \ logprefix=''; \ logmain=${TOOLS_PATH.${_t_}:Q:Q}\"\ \"${TOOLS_ARGS.${_t_}:Q:Q}; \ - logsuffix=' "$$*"'; \ + logsuffix='$$shquoted_args'; \ else \ case ${TOOLS_PATH.${_t_}:Q}"" in \ /*) create=symlink ;; \ @@ -166,7 +166,7 @@ ${TOOLS_CMD.${_t_}}: script=${TOOLS_SCRIPT_DFLT.${_t_}:Q}; \ logprefix=''; \ logmain=${TOOLS_PATH.${_t_}:Q:Q}\"\ \"; \ - logsuffix=' "$$*"'; \ + logsuffix='$$shquoted_args'; \ esac; \ fi; \ else \ @@ -175,11 +175,14 @@ ${TOOLS_CMD.${_t_}}: case "$$create" in \ wrapper) \ { ${ECHO} '#!'${TOOLS_SHELL:Q}; \ + ${ECHO} 'tools_wrapper_sed='${SED:Q:Q}; \ + ${SED} -e '/^$$/d' -e '/^\#/d' ${PKGSRCDIR}/mk/tools/shquote.sh; \ ${ECHO} 'wrapperlog="$${TOOLS_WRAPPER_LOG-'${_TOOLS_WRAP_LOG:Q}'}"'; \ - ${ECHO} '${ECHO} "[*] "'${.TARGET:Q}'" $$*" >> $$wrapperlog'; \ - ${ECHO} 'logprefix='$$logprefix; \ - ${ECHO} 'logmain='$$logmain; \ - ${ECHO} "${ECHO} '<.>' \"\$$logprefix\$$logmain\"$$logsuffix >> \$$wrapperlog"; \ + ${ECHO} 'shquote_args "$$@"'; \ + ${ECHO} '${ECHO} "[*] "'${.TARGET:Q}'"$$shquoted_args" >> $$wrapperlog'; \ + ${ECHO} 'logprefix='$$logprefix; \ + ${ECHO} 'logmain='$$logmain; \ + ${ECHO} "${ECHO} '<.>' \"\$$logprefix\$$logmain$$logsuffix\" >> \$$wrapperlog"; \ ${ECHO} "$$script"; \ } > ${.TARGET:Q}; \ ${CHMOD} +x ${.TARGET:Q}; \ diff --git a/mk/tools/shquote.sh b/mk/tools/shquote.sh new file mode 100644 index 00000000000..39755c4deb9 --- /dev/null +++ b/mk/tools/shquote.sh @@ -0,0 +1,27 @@ +#! /bin/sh +# $NetBSD: shquote.sh,v 1.1 2019/03/24 11:29:19 rillig Exp $ + +# Quotes all shell meta characters from $1 and writes the result to $shquoted. +shquote() +{ + shquoted=$1 + case $shquoted in + *\'*) + shquoted=`$tools_wrapper_sed -e 's,'\'','\''\\\\'\'''\'',g' <