From 91edcf0d46438a932a267bc0f94ad32a6da64e4d Mon Sep 17 00:00:00 2001 From: Mike Gilbert Date: Sat, 4 Jul 2020 12:12:37 -0400 Subject: [PATCH] functions.sh: refactor printf calls Introduce new helper "eprintf". Pass the message argument via a string argument to printf to ensure percent signs get output correctly Closes: https://bugs.gentoo.org/730432 Signed-off-by: Mike Gilbert --- functions.sh | 55 ++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/functions.sh b/functions.sh index 3ea9daa..e6a6243 100644 --- a/functions.sh +++ b/functions.sh @@ -84,6 +84,17 @@ esyslog() return 0 } +eprintf() +{ + local fmt=$1 + local color=$2 + shift 2 + if ! yesno "${RC_ENDCOL}" && [ "${LAST_E_CMD}" = "ebegin" ]; then + printf "\n" + fi + printf "${fmt}" "${color}*${NORMAL} ${RC_INDENTATION}$*" +} + # # show an informative message (without a newline) # @@ -92,10 +103,7 @@ einfon() if yesno "${EINFO_QUIET}"; then return 0 fi - if ! yesno "${RC_ENDCOL}" && [ "${LAST_E_CMD}" = "ebegin" ]; then - printf "\n" - fi - printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*" + eprintf "%s" "${GOOD}" "$@" LAST_E_CMD="einfon" return 0 } @@ -105,7 +113,10 @@ einfon() # einfo() { - einfon "$*\n" + if yesno "${EINFO_QUIET}"; then + return 0 + fi + eprintf "%s\n" "${GOOD}" "$@" LAST_E_CMD="einfo" return 0 } @@ -117,13 +128,10 @@ ewarnn() { if yesno "${EINFO_QUIET}"; then return 0 - else - if ! yesno "${RC_ENDCOL}" && [ "${LAST_E_CMD}" = "ebegin" ]; then - printf "\n" >&2 - fi - printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*" >&2 fi + eprintf "%s" "${WARN}" >&2 + local name="${0##*/}" # Log warnings to system log esyslog "daemon.warning" "${name}" "$*" @@ -139,13 +147,10 @@ ewarn() { if yesno "${EINFO_QUIET}"; then return 0 - else - if ! yesno "${RC_ENDCOL}" && [ "${LAST_E_CMD}" = "ebegin" ]; then - printf "\n" >&2 - fi - printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n" >&2 fi + eprintf "%s\n" "${WARN}" "$@" >&2 + local name="${0##*/}" # Log warnings to system log esyslog "daemon.warning" "${name}" "$*" @@ -161,13 +166,10 @@ eerrorn() { if yesno "${EERROR_QUIET}"; then return 1 - else - if ! yesno "${RC_ENDCOL}" && [ "${LAST_E_CMD}" = "ebegin" ]; then - printf "\n" >&2 - fi - printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*" >&2 fi + eprintf "%s" "${BAD}" "$@" >&2 + local name="${0##*/}" # Log errors to system log esyslog "daemon.err" "rc-scripts" "$*" @@ -183,13 +185,10 @@ eerror() { if yesno "${EERROR_QUIET}"; then return 1 - else - if ! yesno "${RC_ENDCOL}" && [ "${LAST_E_CMD}" = "ebegin" ]; then - printf "\n" >&2 - fi - printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n" >&2 fi + eprintf "%s\n" "${BAD}" "$@" >&2 + local name="${0##*/}" # Log errors to system log esyslog "daemon.err" "rc-scripts" "$*" @@ -247,7 +246,7 @@ _eend() fi if yesno "${RC_ENDCOL}"; then - printf "${ENDCOL} ${msg}\n" + printf "${ENDCOL} %s\n" "${msg}" else [ "${LAST_E_CMD}" = ebegin ] || LAST_E_LEN=0 printf "%$(( COLS - LAST_E_LEN - 6 ))s%b\n" '' "${msg}" @@ -345,7 +344,7 @@ get_libdir() elif command -v portageq > /dev/null 2>&1; then CONF_LIBDIR="$(portageq envvar CONF_LIBDIR)" fi - printf "${CONF_LIBDIR:=lib}\n" + printf "%s\n" "${CONF_LIBDIR:=lib}" } # @@ -440,7 +439,7 @@ done # Setup COLS and ENDCOL so eend can line up the [ ok ] COLS="${COLUMNS:-0}" # bash's internal COLUMNS variable [ "$COLS" -eq 0 ] && \ - COLS="$(set -- $(stty size 2>/dev/null) ; printf "$2\n")" + COLS="$(set -- $(stty size 2>/dev/null) ; printf "%s\n" "$2")" [ -z "$COLS" ] && COLS=80 [ "$COLS" -gt 0 ] || COLS=80 # width of [ ok ] == 7 -- 2.27.0