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 if [ "$(cat /proc/1/comm 2>/dev/null)" = "systemd" ] ; then
39 # log_*() functions {{{
40 TPUT="${TPUT:-"/usr/bin/tput"}"
43 [ -x "$TPUT" ] && "$TPUT" hpa 60 >/dev/null 2>&1 \
54 RED="$("$TPUT" setaf 1)"
55 #NORMAL="$("$TPUT" op)"
56 printf " ${RED}*${NORMAL} $@\n"
64 YELLOW="$("$TPUT" setaf 3)"
65 #NORMAL="$("$TPUT" op")"
66 # printf " *${NORMAL} $@\n"
67 printf " ${BLUE}*${NORMAL} $@\n"
73 log_warning_msg_nn() {
75 YELLOW="$("$TPUT" setaf 3)"
76 printf " ${BLUE}*${NORMAL} $@"
82 # int log_begin_message (char *message)
84 if [ "$#" -eq 0 ]; then
87 printf " ${GREEN}*${NORMAL} $@\n"
91 if [ "$#" -eq 0 ]; then
94 printf " ${GREEN}*${NORMAL} $@"
98 SUBMSG=" ${GREEN}-${NORMAL} "
100 # int log_end_message (int exitstatus)
103 # If no arguments were passed, return
104 [ "$#" -eq 0 ] && return 1
106 # Only do the fancy stuff if we have an appropriate terminal
107 # and if /usr is already mounted
109 COLS="$("$TPUT" cols)"
110 if [ -n "$COLS" ]; then
116 END="$("$TPUT" hpa "$COL")"
117 START="$("$TPUT" hpa 0)"
118 #RED="$("$TPUT" setaf 1)"
119 #NORMAL="$("$TPUT" op)"
120 if [ "$1" -eq 0 ]; then
121 printf "${UP}${END}${BLUE}[ ${GREEN}ok ${BLUE}]${NORMAL}\n"
123 printf "${UP}${START} ${RED}*${NORMAL}${END}[${RED}fail${NORMAL}]\n"
126 if [ "$1" -eq 0 ]; then
136 # e*() output functions {{{
137 # heavily based on gentoo's functions.sh; stripped down and modified
138 # to match our needs.
153 # copyright 1999-2005 gentoo foundation
154 # distributed under the terms of the gnu general public license v2
155 # $header: /var/cvsroot/gentoo-src/rc-scripts/sbin/functions.sh,v 1.81.2.6 2005/05/15 20:00:31 vapier exp $
160 # Dont output to stdout?
163 # Default values for e-message indentation and dots
168 # dont output to stdout?
171 # default values for e-message indentation and dots
177 # should we use color?
178 if [ -r /proc/cmdline ] ; then
179 grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
181 [ -n "$NOCOLORS" ] && RC_NOCOLOR='yes'
182 RC_NOCOLOR="${RC_NOCOLOR:-no}"
184 # Can the terminal handle endcols?
185 if [ "${RC_NOCOLOR}" = "yes" ]; then
191 # Setup COLS and ENDCOL so eend can line up the [ ok ]
192 # width of [ ok ] == 7
193 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
194 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
198 if [ "${RC_ENDCOL}" = "yes" ]; then
199 ENDCOL="
\e[A
\e[$(( ${COLS} - 8 ))G"
204 # Setup the colors so our messages all look pretty
205 if [ "${RC_NOCOLOR}" = "yes" ]; then
206 unset GOOD WARN BAD NORMAL HILITE BRACKET
217 # void esyslog(char* priority, char* tag, char* message)
219 # use the system logger to log a message
225 [ "$#" -le 2 ] && return 0
226 if [ -x /usr/bin/logger ] ; then
231 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$@"
237 # void eindent(int num)
239 # increase the indent used for e-commands.
243 [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
244 esetdent $(( ${#RC_INDENTATION} + $i ))
247 # void eoutdent(int num)
249 # decrease the indent used for e-commands.
253 [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
254 esetdent $(( ${#RC_INDENTATION} - $i ))
257 # void esetdent(int num)
259 # hard set the indent used for e-commands.
264 [ "$i" -lt 0 ] && i=0
265 RC_INDENTATION="$(printf "%${i}s" '')"
268 # void einfo(char* message)
270 # show an informative message (with a newline)
278 # void einfon(char* message)
280 # show an informative message (without a newline)
283 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
284 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
285 printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
290 # void ewarn(char* message)
292 # show a warning message + log it
295 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
298 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
299 printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
302 # Log warnings to system log
303 esyslog "daemon.warning" "rc-scripts" "$@"
309 # void eerror(char* message)
311 # show an error message + log it
314 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
317 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
318 printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
321 # Log errors to system log
322 esyslog "daemon.err" "rc-scripts" "$@"
328 # void ebegin(char* message)
330 # show a message indicating the start of a process
333 local msg="$@" dots spaces
334 spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
335 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
337 if [ -n "${RC_DOT_PATTERN}" ]; then
338 dots="$(printf "%$(( $COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')"
339 while [ "${dots#${spaces}}" != "${dots}" ] ; do
340 dots="${dots#${spaces}}${RC_DOT_PATTERN}"
347 [ "${RC_ENDCOL}" = "yes" ] && echo
349 LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
354 # void _eend(int error, char *efunc, char* errstr)
356 # indicate the completion of process, called from eend/ewend
357 # if error, show errstr via efunc
359 # This function is private to functions.sh. Do not call it from a
363 local retval="${1:-0}" efunc="${2:-eerror}" msg
366 if [ "${retval}" -eq 0 ]; then
367 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
368 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
370 if [ "$#" -gt 0 ] ; then
373 msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
376 if [ "${RC_ENDCOL}" = "yes" ]; then
377 printf "${ENDCOL} ${msg}\n"
379 [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
380 printf "%$(( ${COLS} - ${LAST_E_LEN} - 6 ))s%b\n" '' "${msg}"
386 # void eend(int error, char* errstr)
388 # indicate the completion of process
389 # if error, show errstr via eerror
392 local retval="${1:-0}"
395 _eend "${retval}" eerror "$@"
401 # void ewend(int error, char* errstr)
403 # indicate the completion of process
404 # if error, show errstr via ewarn
407 local retval="${1:-0}"
410 _eend "${retval}" ewarn "$@"
417 # if we're using systemd then redfine functions for
418 # output in systemd style
421 printf "[ ${GREEN}OK${NORMAL} ] %s\n" "$*"
425 printf "[ ${YELLOW}WARN${NORMAL} ] %s\n" "$*"
429 printf "[ ${RED}FAIL${NORMAL} ] %s\n" "$*"
437 # don't expose unneeded local variables
440 # vim: ft=sh tw=80 ts=4 foldmethod=marker