4 # /lib/lsb/init-functions for Debian -*- shell-script -*-
6 # Copyright (c) 2002-03 Chris Lawrence
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 # 3. Neither the name of the author nor the names of other contributors
18 # may be used to endorse or promote products derived from this software
19 # without specific prior written permission.
21 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 # log_*() functions {{{
34 TPUT="${TPUT:-"/usr/bin/tput"}"
37 [ -x "$TPUT" ] && "$TPUT" hpa 60 >/dev/null 2>&1 \
48 RED="$("$TPUT" setaf 1)"
49 #NORMAL="$("$TPUT" op)"
50 printf " ${RED}*${NORMAL} $@\n"
58 YELLOW="$("$TPUT" setaf 3)"
59 #NORMAL="$("$TPUT" op")"
60 # printf " *${NORMAL} $@\n"
61 printf " ${BLUE}*${NORMAL} $@\n"
67 log_warning_msg_nn() {
69 YELLOW="$("$TPUT" setaf 3)"
70 printf " ${BLUE}*${NORMAL} $@"
76 # int log_begin_message (char *message)
78 if [ "$#" -eq 0 ]; then
81 printf " ${GREEN}*${NORMAL} $@\n"
85 if [ "$#" -eq 0 ]; then
88 printf " ${GREEN}*${NORMAL} $@"
92 SUBMSG=" ${GREEN}-${NORMAL} "
94 # int log_end_message (int exitstatus)
97 # If no arguments were passed, return
98 [ "$#" -eq 0 ] && return 1
100 # Only do the fancy stuff if we have an appropriate terminal
101 # and if /usr is already mounted
103 COLS="$("$TPUT" cols)"
104 if [ -n "$COLS" ]; then
110 END="$("$TPUT" hpa "$COL")"
111 START="$("$TPUT" hpa 0)"
112 #RED="$("$TPUT" setaf 1)"
113 #NORMAL="$("$TPUT" op)"
114 if [ "$1" -eq 0 ]; then
115 printf "${UP}${END}${BLUE}[ ${GREEN}ok ${BLUE}]${NORMAL}\n"
117 printf "${UP}${START} ${RED}*${NORMAL}${END}[${RED}fail${NORMAL}]\n"
120 if [ "$1" -eq 0 ]; then
130 # e*() output functions {{{
131 # heavily based on gentoo's functions.sh; stripped down and modified
132 # to match our needs.
147 # copyright 1999-2005 gentoo foundation
148 # distributed under the terms of the gnu general public license v2
149 # $header: /var/cvsroot/gentoo-src/rc-scripts/sbin/functions.sh,v 1.81.2.6 2005/05/15 20:00:31 vapier exp $
154 # Don't output to stdout?
157 # Default values for e-message indentation and dots
162 # don't output to stdout?
165 # default values for e-message indentation and dots
171 # should we use color?
172 if [ -r /proc/cmdline ] ; then
173 grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
175 [ -n "$NOCOLORS" ] && RC_NOCOLOR='yes'
176 RC_NOCOLOR="${RC_NOCOLOR:-no}"
177 if [ "$RC_NOCOLOR" = "no" ] ; then
178 if [ -r /etc/grml_colors ] ; then
183 # Can the terminal handle endcols?
184 if [ "${RC_NOCOLOR}" = "yes" ]; then
190 # Setup COLS and ENDCOL so eend can line up the [ ok ]
191 # width of [ ok ] == 7
192 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
193 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
197 if [ "${RC_ENDCOL}" = "yes" ]; then
198 ENDCOL="
\e[A
\e[$(( ${COLS} - 8 ))G"
203 # Setup the colors so our messages all look pretty
204 if [ "${RC_NOCOLOR}" = "yes" ]; then
205 unset GOOD WARN BAD NORMAL HILITE BRACKET
216 # void esyslog(char* priority, char* tag, char* message)
218 # use the system logger to log a message
224 [ "$#" -le 2 ] && return 0
225 if [ -x /usr/bin/logger ] ; then
230 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$@"
236 # void eindent(int num)
238 # increase the indent used for e-commands.
242 [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
243 esetdent $(( ${#RC_INDENTATION} + $i ))
246 # void eoutdent(int num)
248 # decrease the indent used for e-commands.
252 [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
253 esetdent $(( ${#RC_INDENTATION} - $i ))
256 # void esetdent(int num)
258 # hard set the indent used for e-commands.
263 [ "$i" -lt 0 ] && i=0
264 RC_INDENTATION="$(printf "%${i}s" '')"
267 # void einfo(char* message)
269 # show an informative message (with a newline)
277 # void einfon(char* message)
279 # show an informative message (without a newline)
282 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
283 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
284 printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
289 # void ewarn(char* message)
291 # show a warning message + log it
294 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
297 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
298 printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
301 # Log warnings to system log
302 esyslog "daemon.warning" "rc-scripts" "$@"
308 # void eerror(char* message)
310 # show an error message + log it
313 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
316 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
317 printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
320 # Log errors to system log
321 esyslog "daemon.err" "rc-scripts" "$@"
327 # void ebegin(char* message)
329 # show a message indicating the start of a process
332 local msg="$@" dots spaces
333 spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
334 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
336 if [ -n "${RC_DOT_PATTERN}" ]; then
337 dots="$(printf "%$(( $COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')"
338 while [ "${dots#${spaces}}" != "${dots}" ] ; do
339 dots="${dots#${spaces}}${RC_DOT_PATTERN}"
346 [ "${RC_ENDCOL}" = "yes" ] && echo
348 LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
353 # void _eend(int error, char *efunc, char* errstr)
355 # indicate the completion of process, called from eend/ewend
356 # if error, show errstr via efunc
358 # This function is private to functions.sh. Do not call it from a
362 local retval="${1:-0}" efunc="${2:-eerror}" msg
365 if [ "${retval}" -eq 0 ]; then
366 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
367 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
369 if [ "$#" -gt 0 ] ; then
372 msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
375 if [ "${RC_ENDCOL}" = "yes" ]; then
376 printf "${ENDCOL} ${msg}\n"
378 [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
379 printf "%$(( ${COLS} - ${LAST_E_LEN} - 6 ))s%b\n" '' "${msg}"
385 # void eend(int error, char* errstr)
387 # indicate the completion of process
388 # if error, show errstr via eerror
391 local retval="${1:-0}"
394 _eend "${retval}" eerror "$@"
400 # void ewend(int error, char* errstr)
402 # indicate the completion of process
403 # if error, show errstr via ewarn
406 local retval="${1:-0}"
409 _eend "${retval}" ewarn "$@"
416 # if we're using systemd then redefine functions
417 # for output in systemd style
418 if [ "$(cat /proc/1/comm 2>/dev/null)" = "systemd" ] ; then
420 printf "[ ${GREEN}OK${NORMAL} ] %s\n" "$*"
424 printf "[ ${YELLOW}WARN${NORMAL} ] %s\n" "$*"
428 printf "[ ${RED}FAIL${NORMAL} ] %s\n" "$*"
436 # vim: ft=sh tw=80 ts=4 foldmethod=marker