Restore some RC_* variables
[grml-etc-core.git] / etc / grml / lsb-functions
1 # lsb init-functions
2 #
3 # based on:
4 # /lib/lsb/init-functions for Debian -*- shell-script -*-
5 #
6 # Copyright (c) 2002-03 Chris Lawrence
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
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.
20 #
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
31 # SUCH DAMAGE.
32
33 # log_*() functions {{{
34 TPUT="${TPUT:-"/usr/bin/tput"}"
35
36 _have_tput() {
37     [ -x "$TPUT" ] && "$TPUT" hpa 60 >/dev/null 2>&1 \
38         && return 0 \
39         || return 1
40 }
41
42 log_success_msg() {
43     printf " * $@\n"
44 }
45
46 log_failure_msg() {
47     if _have_tput ; then
48         RED="$("$TPUT" setaf 1)"
49         #NORMAL="$("$TPUT" op)"
50         printf " ${RED}*${NORMAL} $@\n"
51     else
52         printf " * $@\n"
53     fi
54 }
55
56 log_warning_msg() {
57     if _have_tput ; then
58         YELLOW="$("$TPUT" setaf 3)"
59         #NORMAL="$("$TPUT" op")"
60         # printf " *${NORMAL} $@\n"
61         printf " ${BLUE}*${NORMAL} $@\n"
62     else
63         printf " * $@\n"
64     fi
65 }
66
67 log_warning_msg_nn() {
68     if _have_tput ; then
69         YELLOW="$("$TPUT" setaf 3)"
70         printf " ${BLUE}*${NORMAL} $@"
71     else
72         printf " * $@"
73     fi
74 }
75
76 # int log_begin_message (char *message)
77 log_begin_msg() {
78         if [ "$#" -eq 0 ]; then
79                 return 1
80         fi
81         printf " ${GREEN}*${NORMAL} $@\n"
82 }
83
84 log_begin_msg_nn() {
85         if [ "$#" -eq 0 ]; then
86                 return 1
87         fi
88         printf " ${GREEN}*${NORMAL} $@"
89 }
90
91
92 SUBMSG="   ${GREEN}-${NORMAL} "
93
94 # int log_end_message (int exitstatus)
95 log_end_msg() {
96
97     # If no arguments were passed, return
98     [ "$#" -eq 0 ] && return 1
99
100     # Only do the fancy stuff if we have an appropriate terminal
101     # and if /usr is already mounted
102     if _have_tput ; then
103         COLS="$("$TPUT" cols)"
104         if [ -n "$COLS" ]; then
105             COL=$(( $COLS - 7 ))
106         else
107             COL=73
108         fi
109         UP="$("$TPUT" cuu1)"
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"
116         else
117             printf "${UP}${START} ${RED}*${NORMAL}${END}[${RED}fail${NORMAL}]\n"
118         fi
119     else
120         if [ "$1" -eq 0 ]; then
121             printf "   ...done.\n"
122         else
123             printf "   ...fail!\n"
124         fi
125     fi
126     return "$1"
127 }
128 # }}}
129
130 # e*() output functions {{{
131 # heavily based on gentoo's functions.sh; stripped down and modified
132 # to match our needs.
133 #
134 # defined functions:
135 #   ebegin()
136 #   eend()
137 #   eerror()
138 #   eindent()
139 #   einfo()
140 #   einfon()
141 #   eoutdent()
142 #   esetdent()
143 #   esyslog()
144 #   ewarn()
145 #   ewend()
146 #
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 $
150
151 # initialisation {{{
152 # internal variables
153
154 # Dont output to stdout?
155 RC_QUIET_STDOUT="no"
156
157 # Default values for e-message indentation and dots
158 RC_INDENTATION=''
159 RC_DEFAULT_INDENT=2
160 #RC_DOT_PATTERN=' .'
161 RC_DOT_PATTERN=''
162 # dont output to stdout?
163 rc_quiet_stdout="no"
164
165 # default values for e-message indentation and dots
166 rc_indentation=''
167 rc_default_indent=2
168 #rc_dot_pattern=' .'
169 rc_dot_pattern=''
170
171 # should we use color?
172 if [ -r /proc/cmdline ] ; then
173   grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
174 fi
175 [ -n "$NOCOLORS" ] && RC_NOCOLOR='yes'
176 RC_NOCOLOR="${RC_NOCOLOR:-no}"
177
178 # Can the terminal handle endcols?
179 RC_ENDCOL="yes"
180
181 # Setup COLS and ENDCOL so eend can line up the [ ok ]
182 # width of [ ok ] == 7
183 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
184 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
185   COLS=80
186 fi
187
188 if [ "${RC_ENDCOL}" = "yes" ]; then
189   ENDCOL="\e[A\e[$(( ${COLS} - 8 ))G"
190 else
191   ENDCOL=''
192 fi
193
194 # Setup the colors so our messages all look pretty
195 if [ "${RC_NOCOLOR}" = "yes" ]; then
196   unset GOOD WARN BAD NORMAL HILITE BRACKET
197 else
198   GOOD='\e[32;01m'
199   WARN='\e[33;01m'
200   BAD='\e[31;01m'
201   NORMAL='\e[0m'
202   HILITE='\e[36;01m'
203   BRACKET='\e[34;01m'
204 fi
205 #}}}
206
207 # void esyslog(char* priority, char* tag, char* message)
208 #
209 #    use the system logger to log a message
210 #
211 esyslog() {
212   local pri
213   local tag
214
215   [ "$#" -le 2 ] && return 0
216   if [ -x /usr/bin/logger ] ; then
217     pri="$1"
218     tag="$2"
219     shift 2
220
221     /usr/bin/logger -p "${pri}" -t "${tag}" -- "$@"
222   fi
223
224   return 0
225 }
226
227 # void eindent(int num)
228 #
229 #    increase the indent used for e-commands.
230 #
231 eindent() {
232   local i="${1:-0}"
233   [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
234   esetdent $(( ${#RC_INDENTATION} + $i ))
235 }
236
237 # void eoutdent(int num)
238 #
239 #    decrease the indent used for e-commands.
240 #
241 eoutdent() {
242   local i="${1:-0}"
243   [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
244   esetdent $(( ${#RC_INDENTATION} - $i ))
245 }
246
247 # void esetdent(int num)
248 #
249 #    hard set the indent used for e-commands.
250 #    num defaults to 0
251 #
252 esetdent() {
253   local i="${1:-0}"
254   [ "$i" -lt 0 ] && i=0
255   RC_INDENTATION="$(printf "%${i}s" '')"
256 }
257
258 # void einfo(char* message)
259 #
260 #    show an informative message (with a newline)
261 #
262 einfo() {
263   einfon "$*\n"
264   LAST_E_CMD=einfo
265   return 0
266 }
267
268 # void einfon(char* message)
269 #
270 #    show an informative message (without a newline)
271 #
272 einfon() {
273   [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
274   [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
275   printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
276   LAST_E_CMD=einfon
277   return 0
278 }
279
280 # void ewarn(char* message)
281 #
282 #    show a warning message + log it
283 #
284 ewarn() {
285   if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
286       printf " $*\n"
287   else
288     [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
289     printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
290   fi
291
292   # Log warnings to system log
293   esyslog "daemon.warning" "rc-scripts" "$@"
294
295   LAST_E_CMD=ewarn
296   return 0
297 }
298
299 # void eerror(char* message)
300 #
301 #    show an error message + log it
302 #
303 eerror() {
304   if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
305     printf " $*\n" >&2
306   else
307     [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
308     printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
309   fi
310
311   # Log errors to system log
312   esyslog "daemon.err" "rc-scripts" "$@"
313
314   LAST_E_CMD=eerror
315   return 0
316 }
317
318 # void ebegin(char* message)
319 #
320 #    show a message indicating the start of a process
321 #
322 ebegin() {
323   local msg="$@" dots spaces
324   spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
325   [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
326
327   if [ -n "${RC_DOT_PATTERN}" ]; then
328     dots="$(printf "%$(( $COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')"
329     while [ "${dots#${spaces}}" != "${dots}" ] ; do
330         dots="${dots#${spaces}}${RC_DOT_PATTERN}"
331     done
332     msg="${msg}${dots}"
333   else
334     msg="${msg} ..."
335   fi
336   einfon "${msg}"
337   [ "${RC_ENDCOL}" = "yes" ] && echo
338
339   LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
340   LAST_E_CMD=ebegin
341   return 0
342 }
343
344 # void _eend(int error, char *efunc, char* errstr)
345 #
346 #    indicate the completion of process, called from eend/ewend
347 #    if error, show errstr via efunc
348 #
349 #    This function is private to functions.sh.  Do not call it from a
350 #    script.
351 #
352 _eend() {
353   local retval="${1:-0}" efunc="${2:-eerror}" msg
354   shift 2
355
356   if [ "${retval}" -eq 0 ]; then
357     [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
358     msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
359   else
360     if [ "$#" -gt 0 ] ; then
361         "${efunc}" "$@"
362     fi
363     msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
364   fi
365
366   if [ "${RC_ENDCOL}" = "yes" ]; then
367     printf "${ENDCOL}  ${msg}\n"
368   else
369     [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
370     printf "%$(( ${COLS} - ${LAST_E_LEN} - 6 ))s%b\n" '' "${msg}"
371   fi
372
373   return "${retval}"
374 }
375
376 # void eend(int error, char* errstr)
377 #
378 #    indicate the completion of process
379 #    if error, show errstr via eerror
380 #
381 eend() {
382   local retval="${1:-0}"
383   shift
384
385   _eend "${retval}" eerror "$@"
386
387   LAST_E_CMD=eend
388   return "$retval"
389 }
390
391 # void ewend(int error, char* errstr)
392 #
393 #    indicate the completion of process
394 #    if error, show errstr via ewarn
395 #
396 ewend() {
397   local retval="${1:-0}"
398   shift
399
400   _eend "${retval}" ewarn "$@"
401
402   LAST_E_CMD=ewend
403   return "$retval"
404 }
405 #}}}
406
407 # vim: ft=sh tw=80 ts=4 foldmethod=marker