/etc/grml/lsb-functions: support disabling colors through $NOCOLORS
[grml-etc-core.git] / etc / grml / lsb-functions
1 # lsb init-functions
2 # vim:ft=sh:tw=80
3 # /lib/lsb/init-functions for Debian -*- shell-script -*-
4 #
5 # Copyright (c) 2002-03 Chris Lawrence
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 #    notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 #    notice, this list of conditions and the following disclaimer in the
15 #    documentation and/or other materials provided with the distribution.
16 # 3. Neither the name of the author nor the names of other contributors
17 #    may be used to endorse or promote products derived from this software
18 #    without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # SUCH DAMAGE.
31
32 TPUT="${TPUT:-"/usr/bin/tput"}"
33
34 _have_tput() {
35     [ -x "$TPUT" ] && "$TPUT" hpa 60 >/dev/null 2>&1 \
36         && return 0 \
37         || return 1
38 }
39
40 log_success_msg() {
41     printf " * $@\n"
42 }
43
44 log_failure_msg() {
45     if _have_tput ; then
46         RED="$("$TPUT" setaf 1)"
47         #NORMAL="$("$TPUT" op)"
48         printf " ${RED}*${NORMAL} $@\n"
49     else
50         printf " * $@\n"
51     fi
52 }
53
54 log_warning_msg() {
55     if _have_tput ; then
56         YELLOW="$("$TPUT" setaf 3)"
57         #NORMAL="$("$TPUT" op")"
58         # printf " *${NORMAL} $@\n"
59         printf " ${BLUE}*${NORMAL} $@\n"
60     else
61         printf " * $@\n"
62     fi
63 }
64
65 log_warning_msg_nn() {
66     if _have_tput ; then
67         YELLOW="$("$TPUT" setaf 3)"
68         printf " ${BLUE}*${NORMAL} $@"
69     else
70         printf " * $@"
71     fi
72 }
73
74 # int log_begin_message (char *message)
75 log_begin_msg() {
76         if [ "$#" -eq 0 ]; then
77                 return 1
78         fi
79         printf " ${GREEN}*${NORMAL} $@\n"
80 }
81
82 log_begin_msg_nn() {
83         if [ "$#" -eq 0 ]; then
84                 return 1
85         fi
86         printf " ${GREEN}*${NORMAL} $@"
87 }
88
89
90 SUBMSG="   ${GREEN}-${NORMAL} "
91
92 # int log_end_message (int exitstatus)
93 log_end_msg() {
94
95     # If no arguments were passed, return
96     [ "$#" -eq 0 ] && return 1
97
98     # Only do the fancy stuff if we have an appropriate terminal
99     # and if /usr is already mounted
100     if _have_tput ; then
101         COLS="$("$TPUT" cols)"
102         if [ -n "$COLS" ]; then
103             COL=$(( "$COLS" - 7 ))
104         else
105             COL=73
106         fi
107         UP="$("$TPUT" cuu1)"
108         END="$("$TPUT" hpa "$COL")"
109         START="$("$TPUT" hpa 0)"
110         #RED="$("$TPUT" setaf 1)"
111         #NORMAL="$("$TPUT" op)"
112         if [ "$1" -eq 0 ]; then
113             printf "${UP}${END}${BLUE}[ ${GREEN}ok ${BLUE}]${NORMAL}\n"
114         else
115             printf "${UP}${START} ${RED}*${NORMAL}${END}[${RED}fail${NORMAL}]\n"
116         fi
117     else
118         if [ "$1" -eq 0 ]; then
119             printf "   ...done.\n"
120         else
121             printf "   ...fail!\n"
122         fi
123     fi
124     return "$1"
125 }
126
127 # Copyright 1999-2005 Gentoo Foundation
128 # Distributed under the terms of the GNU General Public License v2
129 # $Header: /var/cvsroot/gentoo-src/rc-scripts/sbin/functions.sh,v 1.81.2.6 2005/05/15 20:00:31 vapier Exp $
130
131 # Internal variables
132
133 # Dont output to stdout?
134 RC_QUIET_STDOUT="no"
135
136 # Default values for e-message indentation and dots
137 RC_INDENTATION=''
138 RC_DEFAULT_INDENT=2
139 #RC_DOT_PATTERN=' .'
140 RC_DOT_PATTERN=''
141
142 # Should we use color?
143 if [ -r /proc/cmdline ] ; then
144     grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
145 fi
146 [ -n "$NOCOLORS" ] && RC_NOCOLOR='yes'
147 RC_NOCOLOR="${RC_NOCOLOR:-no}"
148
149 # Can the terminal handle endcols?
150 RC_ENDCOL="yes"
151
152 # Setup COLS and ENDCOL so eend can line up the [ ok ]
153 # width of [ ok ] == 7
154 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
155 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
156     COLS=80
157 fi
158
159 if [ "${RC_ENDCOL}" = "yes" ]; then
160     ENDCOL="\e[A\e[$(( "${COLS}" - 8 ))G"
161 else
162     ENDCOL=''
163 fi
164
165 # Setup the colors so our messages all look pretty
166 if [ "${RC_NOCOLOR}" = "yes" ]; then
167     unset GOOD WARN BAD NORMAL HILITE BRACKET
168 else
169     GOOD='\e[32;01m'
170     WARN='\e[33;01m'
171     BAD='\e[31;01m'
172     NORMAL='\e[0m'
173     HILITE='\e[36;01m'
174     BRACKET='\e[34;01m'
175 fi
176
177 # void esyslog(char* priority, char* tag, char* message)
178 #
179 #    use the system logger to log a message
180 #
181 esyslog() {
182     local pri
183     local tag
184
185     if [ -x /usr/bin/logger ] ; then
186         pri="$1"
187         tag="$2"
188
189         shift 2
190         [ "$#" -eq 0 ] && return 0
191
192         /usr/bin/logger -p "${pri}" -t "${tag}" -- "$@"
193     fi
194
195     return 0
196 }
197
198 # void eindent(int num)
199 #
200 #    increase the indent used for e-commands.
201 #
202 eindent() {
203     local i="$1"
204     [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
205     esetdent $(( ${#RC_INDENTATION} + "$i" ))
206 }
207
208 # void eoutdent(int num)
209 #
210 #    decrease the indent used for e-commands.
211 #
212 eoutdent() {
213     local i="$1"
214     [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
215     esetdent $(( ${#RC_INDENTATION} - "$i" ))
216 }
217
218 # void esetdent(int num)
219 #
220 #    hard set the indent used for e-commands.
221 #    num defaults to 0
222 #
223 esetdent() {
224     local i="$1"
225     [ "$i" -lt 0 ] && i=0
226     RC_INDENTATION="$(printf "%${i}s" '')"
227 }
228
229 # void einfo(char* message)
230 #
231 #    show an informative message (with a newline)
232 #
233 einfo() {
234     einfon "$*\n"
235     LAST_E_CMD=einfo
236     return 0
237 }
238
239 # void einfon(char* message)
240 #
241 #    show an informative message (without a newline)
242 #
243 einfon() {
244     [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
245     [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
246     printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
247     LAST_E_CMD=einfon
248     return 0
249 }
250
251 # void ewarn(char* message)
252 #
253 #    show a warning message + log it
254 #
255 ewarn() {
256     if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
257         printf " $*\n"
258     else
259         [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
260         printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
261     fi
262
263     # Log warnings to system log
264     esyslog "daemon.warning" "rc-scripts" "$@"
265
266     LAST_E_CMD=ewarn
267     return 0
268 }
269
270 # void eerror(char* message)
271 #
272 #    show an error message + log it
273 #
274 eerror() {
275     if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
276         printf " $*\n" >/dev/stderr
277     else
278         [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
279         printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
280     fi
281
282     # Log errors to system log
283     esyslog "daemon.err" "rc-scripts" "$@"
284
285     LAST_E_CMD=eerror
286     return 0
287 }
288
289 # void ebegin(char* message)
290 #
291 #    show a message indicating the start of a process
292 #
293 ebegin() {
294     local msg="$@" dots spaces
295     spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
296     [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
297
298     if [ -n "${RC_DOT_PATTERN}" ]; then
299         dots="$(printf "%$(( "$COLS" - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')"
300         while [ "${dots#${spaces}}" != "${dots}" ] ; do
301             dots="${dots#${spaces}}${RC_DOT_PATTERN}"
302         done
303         msg="${msg}${dots}"
304     else
305         msg="${msg} ..."
306     fi
307     einfon "${msg}"
308     [ "${RC_ENDCOL}" = "yes" ] && echo
309
310     LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
311     LAST_E_CMD=ebegin
312     return 0
313 }
314
315 # void _eend(int error, char *efunc, char* errstr)
316 #
317 #    indicate the completion of process, called from eend/ewend
318 #    if error, show errstr via efunc
319 #
320 #    This function is private to functions.sh.  Do not call it from a
321 #    script.
322 #
323 _eend() {
324     local retval="${1:-0}" efunc="${2:-eerror}" msg
325     shift 2
326
327     if [ "${retval}" -eq 0 ]; then
328         [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
329         msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
330     else
331         if [ "$#" -gt 0 ] ; then
332             "${efunc}" "$@"
333         fi
334         msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
335     fi
336
337     if [ "${RC_ENDCOL}" = "yes" ]; then
338         printf "${ENDCOL}  ${msg}\n"
339     else
340         [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
341         printf "%$(( "${COLS}" - "${LAST_E_LEN}" - 6 ))s%b\n" '' "${msg}"
342     fi
343
344     return "${retval}"
345 }
346
347 # void eend(int error, char* errstr)
348 #
349 #    indicate the completion of process
350 #    if error, show errstr via eerror
351 #
352 eend() {
353     local retval="${1:-0}"
354     shift
355
356     _eend "${retval}" eerror "$@"
357
358     LAST_E_CMD=eend
359     return "$retval"
360 }
361
362 # void ewend(int error, char* errstr)
363 #
364 #    indicate the completion of process
365 #    if error, show errstr via ewarn
366 #
367 ewend() {
368     local retval="${1:-0}"
369     shift
370
371     _eend "${retval}" ewarn "$@"
372
373     LAST_E_CMD=ewend
374     return "$retval"
375 }
376
377 # vim:ts=4