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}"
183 if [ "$RC_NOCOLOR" = "no" ] ; then
184 if [ -r /etc/grml_colors ] ; then
189 # Can the terminal handle endcols?
190 if [ "${RC_NOCOLOR}" = "yes" ]; then
196 # Setup COLS and ENDCOL so eend can line up the [ ok ]
197 # width of [ ok ] == 7
198 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
199 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
203 if [ "${RC_ENDCOL}" = "yes" ]; then
204 ENDCOL="
\e[A
\e[$(( ${COLS} - 8 ))G"
209 # Setup the colors so our messages all look pretty
210 if [ "${RC_NOCOLOR}" = "yes" ]; then
211 unset GOOD WARN BAD NORMAL HILITE BRACKET
222 # void esyslog(char* priority, char* tag, char* message)
224 # use the system logger to log a message
230 [ "$#" -le 2 ] && return 0
231 if [ -x /usr/bin/logger ] ; then
236 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$@"
242 # void eindent(int num)
244 # increase the indent used for e-commands.
248 [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
249 esetdent $(( ${#RC_INDENTATION} + $i ))
252 # void eoutdent(int num)
254 # decrease the indent used for e-commands.
258 [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
259 esetdent $(( ${#RC_INDENTATION} - $i ))
262 # void esetdent(int num)
264 # hard set the indent used for e-commands.
269 [ "$i" -lt 0 ] && i=0
270 RC_INDENTATION="$(printf "%${i}s" '')"
273 # void einfo(char* message)
275 # show an informative message (with a newline)
283 # void einfon(char* message)
285 # show an informative message (without a newline)
288 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
289 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
290 printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
295 # void ewarn(char* message)
297 # show a warning message + log it
300 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
303 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
304 printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
307 # Log warnings to system log
308 esyslog "daemon.warning" "rc-scripts" "$@"
314 # void eerror(char* message)
316 # show an error message + log it
319 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
322 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
323 printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
326 # Log errors to system log
327 esyslog "daemon.err" "rc-scripts" "$@"
333 # void ebegin(char* message)
335 # show a message indicating the start of a process
338 local msg="$@" dots spaces
339 spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
340 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
342 if [ -n "${RC_DOT_PATTERN}" ]; then
343 dots="$(printf "%$(( $COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')"
344 while [ "${dots#${spaces}}" != "${dots}" ] ; do
345 dots="${dots#${spaces}}${RC_DOT_PATTERN}"
352 [ "${RC_ENDCOL}" = "yes" ] && echo
354 LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
359 # void _eend(int error, char *efunc, char* errstr)
361 # indicate the completion of process, called from eend/ewend
362 # if error, show errstr via efunc
364 # This function is private to functions.sh. Do not call it from a
368 local retval="${1:-0}" efunc="${2:-eerror}" msg
371 if [ "${retval}" -eq 0 ]; then
372 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
373 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
375 if [ "$#" -gt 0 ] ; then
378 msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
381 if [ "${RC_ENDCOL}" = "yes" ]; then
382 printf "${ENDCOL} ${msg}\n"
384 [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
385 printf "%$(( ${COLS} - ${LAST_E_LEN} - 6 ))s%b\n" '' "${msg}"
391 # void eend(int error, char* errstr)
393 # indicate the completion of process
394 # if error, show errstr via eerror
397 local retval="${1:-0}"
400 _eend "${retval}" eerror "$@"
406 # void ewend(int error, char* errstr)
408 # indicate the completion of process
409 # if error, show errstr via ewarn
412 local retval="${1:-0}"
415 _eend "${retval}" ewarn "$@"
422 # if we're using systemd then redfine functions for
423 # output in systemd style
426 printf "[ ${GREEN}OK${NORMAL} ] %s\n" "$*"
430 printf "[ ${YELLOW}WARN${NORMAL} ] %s\n" "$*"
434 printf "[ ${RED}FAIL${NORMAL} ] %s\n" "$*"
442 # don't expose unneeded local variables
445 # vim: ft=sh tw=80 ts=4 foldmethod=marker