/[baselayout]/trunk/sbin/functions.sh
Gentoo

Contents of /trunk/sbin/functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2047 - (show annotations) (download) (as text)
Tue May 30 09:58:08 2006 UTC (8 years, 2 months ago) by uberlord
File MIME type: text/x-sh
File size: 19399 byte(s)
Really fix is_xenU_sys, #134571 thanks to Stefan Hellermann.
1 # Copyright 1999-2006 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 RC_GOT_FUNCTIONS="yes"
5
6 # Override defaults with user settings ...
7 [[ -f /etc/conf.d/rc ]] && source /etc/conf.d/rc
8
9 # Check /etc/conf.d/rc for a description of these ...
10 declare -r svclib="/lib/rcscripts"
11 declare -r svcdir="${svcdir:-/var/lib/init.d}"
12 svcmount="${svcmount:-no}"
13 svcfstype="${svcfstype:-tmpfs}"
14 svcsize="${svcsize:-1024}"
15
16 # Different types of dependencies
17 deptypes="need use"
18 # Different types of order deps
19 ordtypes="before after"
20
21 #
22 # Internal variables
23 #
24
25 # Dont output to stdout?
26 RC_QUIET_STDOUT="${RC_QUIET_STDOUT:-no}"
27 RC_VERBOSE="${RC_VERBOSE:-no}"
28
29 # Should we use color?
30 RC_NOCOLOR="${RC_NOCOLOR:-no}"
31 # Can the terminal handle endcols?
32 RC_ENDCOL="yes"
33
34 #
35 # Default values for rc system
36 #
37 RC_TTY_NUMBER="${RC_TTY_NUMBER:-11}"
38 RC_PARALLEL_STARTUP="${RC_PARALLEL_STARTUP:-no}"
39 RC_NET_STRICT_CHECKING="${RC_NET_STRICT_CHECKING:-no}"
40 RC_USE_FSTAB="${RC_USE_FSTAB:-no}"
41 RC_USE_CONFIG_PROFILE="${RC_USE_CONFIG_PROFILE:-yes}"
42 RC_FORCE_AUTO="${RC_FORCE_AUTO:-no}"
43 RC_DEVICES="${RC_DEVICES:-auto}"
44 RC_DOWN_INTERFACE="${RC_DOWN_INTERFACE:-yes}"
45
46 #
47 # Default values for e-message indentation and dots
48 #
49 RC_INDENTATION=''
50 RC_DEFAULT_INDENT=2
51 #RC_DOT_PATTERN=' .'
52 RC_DOT_PATTERN=''
53
54 # Setup i18n variables
55 TEXTDOMAINDIR="${svclib}/locale"
56 TEXTDOMAIN="rc-scripts"
57
58 # void import_addon(char *addon)
59 #
60 # Import code from the specified addon if it exists
61 #
62 import_addon() {
63 local addon="${svclib}/addons/$1"
64 if [[ -r ${addon} ]] ; then
65 source "${addon}"
66 return 0
67 fi
68 return 1
69 }
70
71 # void splash(...)
72 #
73 # Notify bootsplash/splashutils/gensplash/whatever about
74 # important events.
75 #
76 splash() {
77 return 0
78 }
79 # This will override the splash() function...
80 if ! import_addon splash-functions.sh ; then
81 [[ -f /sbin/splash-functions.sh ]] && source /sbin/splash-functions.sh
82 fi
83
84 # void profiling(...)
85 #
86 # Notify bootsplash/whatever about important events.
87 #
88 profiling() {
89 return 0
90 }
91 import_addon profiling-functions.sh
92
93 # void bootlog(...)
94 #
95 # Notify bootlogger about important events.
96 bootlog() {
97 return 0
98 }
99 [[ ${RC_BOOTLOG} == "yes" ]] && import_addon bootlogger.sh
100
101 # void get_bootconfig()
102 #
103 # Get the BOOTLEVEL and SOFTLEVEL by setting
104 # 'bootlevel' and 'softlevel' via kernel
105 # parameters.
106 #
107 get_bootconfig() {
108 local copt=
109 local newbootlevel=
110 local newsoftlevel=
111
112 if [[ -r /proc/cmdline ]] ; then
113 for copt in $(</proc/cmdline) ; do
114 case "${copt%=*}" in
115 bootlevel)
116 newbootlevel="${copt##*=}"
117 ;;
118 softlevel)
119 newsoftlevel="${copt##*=}"
120 ;;
121 esac
122 done
123 fi
124
125 if [[ -n ${newbootlevel} ]] ; then
126 export BOOTLEVEL="${newbootlevel}"
127 else
128 export BOOTLEVEL="boot"
129 fi
130
131 if [[ -n ${newsoftlevel} ]] ; then
132 export DEFAULTLEVEL="${newsoftlevel}"
133 else
134 export DEFAULTLEVEL="default"
135 fi
136
137 return 0
138 }
139
140 setup_defaultlevels() {
141 get_bootconfig
142
143 if get_bootparam "noconfigprofile" ; then
144 export RC_USE_CONFIG_PROFILE="no"
145
146 elif get_bootparam "configprofile" ; then
147 export RC_USE_CONFIG_PROFILE="yes"
148 fi
149
150 if [[ ${RC_USE_CONFIG_PROFILE} == "yes" && -n ${DEFAULTLEVEL} ]] && \
151 [[ -d "/etc/runlevels/${BOOTLEVEL}.${DEFAULTLEVEL}" || \
152 -L "/etc/runlevels/${BOOTLEVEL}.${DEFAULTLEVEL}" ]] ; then
153 export BOOTLEVEL="${BOOTLEVEL}.${DEFAULTLEVEL}"
154 fi
155
156 if [[ -z ${SOFTLEVEL} ]] ; then
157 if [[ -f "${svcdir}/softlevel" ]] ; then
158 export SOFTLEVEL=$(< "${svcdir}/softlevel")
159 else
160 export SOFTLEVEL="${BOOTLEVEL}"
161 fi
162 fi
163
164 return 0
165 }
166
167 # void get_libdir(void)
168 #
169 # prints the current libdir {lib,lib32,lib64}
170 #
171 get_libdir() {
172 if [[ -n ${CONF_LIBDIR_OVERRIDE} ]] ; then
173 CONF_LIBDIR="${CONF_LIBDIR_OVERRIDE}"
174 elif [[ -x /usr/bin/portageq ]] ; then
175 CONF_LIBDIR="$(/usr/bin/portageq envvar CONF_LIBDIR)"
176 fi
177 echo "${CONF_LIBDIR:=lib}"
178 }
179
180 # void esyslog(char* priority, char* tag, char* message)
181 #
182 # use the system logger to log a message
183 #
184 esyslog() {
185 local pri=
186 local tag=
187
188 if [[ -x /usr/bin/logger ]] ; then
189 pri="$1"
190 tag="$2"
191
192 shift 2
193 [[ -z "$*" ]] && return 0
194
195 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
196 fi
197
198 return 0
199 }
200
201 # void eindent(int num)
202 #
203 # increase the indent used for e-commands.
204 #
205 eindent() {
206 local i="$1"
207 (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
208 esetdent $(( ${#RC_INDENTATION} + i ))
209 }
210
211 # void eoutdent(int num)
212 #
213 # decrease the indent used for e-commands.
214 #
215 eoutdent() {
216 local i="$1"
217 (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
218 esetdent $(( ${#RC_INDENTATION} - i ))
219 }
220
221 # void esetdent(int num)
222 #
223 # hard set the indent used for e-commands.
224 # num defaults to 0
225 #
226 esetdent() {
227 local i="$1"
228 (( i < 0 )) && (( i = 0 ))
229 RC_INDENTATION=$(printf "%${i}s" '')
230 }
231
232 # void einfo(char* message)
233 #
234 # show an informative message (with a newline)
235 #
236 einfo() {
237 einfon "$*\n"
238 LAST_E_CMD="einfo"
239 return 0
240 }
241
242 # void einfon(char* message)
243 #
244 # show an informative message (without a newline)
245 #
246 einfon() {
247 [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
248 [[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo
249 echo -ne " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
250 LAST_E_CMD="einfon"
251 return 0
252 }
253
254 # void ewarn(char* message)
255 #
256 # show a warning message + log it
257 #
258 ewarn() {
259 if [[ ${RC_QUIET_STDOUT} == "yes" ]] ; then
260 echo " $*"
261 else
262 [[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo
263 echo -e " ${WARN}*${NORMAL} ${RC_INDENTATION}$*"
264 fi
265
266 local name="rc-scripts"
267 [[ $0 != "/sbin/runscript.sh" ]] && name="${0##*/}"
268 # Log warnings to system log
269 esyslog "daemon.warning" "${name}" "$*"
270
271 LAST_E_CMD="ewarn"
272 return 0
273 }
274
275 # void eerror(char* message)
276 #
277 # show an error message + log it
278 #
279 eerror() {
280 if [[ ${RC_QUIET_STDOUT} == "yes" ]] ; then
281 echo " $*" >/dev/stderr
282 else
283 [[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo
284 echo -e " ${BAD}*${NORMAL} ${RC_INDENTATION}$*"
285 fi
286
287 local name="rc-scripts"
288 [[ $0 != "/sbin/runscript.sh" ]] && name="${0##*/}"
289 # Log errors to system log
290 esyslog "daemon.err" "rc-scripts" "$*"
291
292 LAST_E_CMD="eerror"
293 return 0
294 }
295
296 # void ebegin(char* message)
297 #
298 # show a message indicating the start of a process
299 #
300 ebegin() {
301 local msg="$*" dots spaces="${RC_DOT_PATTERN//?/ }"
302 [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
303
304 if [[ -n ${RC_DOT_PATTERN} ]] ; then
305 dots="$(printf "%$((COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7))s" '')"
306 dots="${dots//${spaces}/${RC_DOT_PATTERN}}"
307 msg="${msg}${dots}"
308 else
309 msg="${msg} ..."
310 fi
311 einfon "${msg}"
312 [[ ${RC_ENDCOL} == "yes" ]] && echo
313
314 LAST_E_LEN="$(( 3 + ${#RC_INDENTATION} + ${#msg} ))"
315 LAST_E_CMD="ebegin"
316 return 0
317 }
318
319 # void _eend(int error, char *efunc, char* errstr)
320 #
321 # indicate the completion of process, called from eend/ewend
322 # if error, show errstr via efunc
323 #
324 # This function is private to functions.sh. Do not call it from a
325 # script.
326 #
327 _eend() {
328 local retval="${1:-0}" efunc="${2:-eerror}" msg
329 shift 2
330
331 if [[ ${retval} == "0" ]] ; then
332 [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
333 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
334 else
335 if [[ -c /dev/null ]] ; then
336 rc_splash "stop" &>/dev/null &
337 else
338 rc_splash "stop" &
339 fi
340 if [[ -n $* ]] ; then
341 ${efunc} "$*"
342 fi
343 msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
344 fi
345
346 if [[ ${RC_ENDCOL} == "yes" ]] ; then
347 echo -e "${ENDCOL} ${msg}"
348 else
349 [[ ${LAST_E_CMD} == ebegin ]] || LAST_E_LEN=0
350 printf "%$(( COLS - LAST_E_LEN - 6 ))s%b\n" '' "${msg}"
351 fi
352
353 return ${retval}
354 }
355
356 # void eend(int error, char* errstr)
357 #
358 # indicate the completion of process
359 # if error, show errstr via eerror
360 #
361 eend() {
362 local retval="${1:-0}"
363 shift
364
365 _eend "${retval}" eerror "$*"
366
367 LAST_E_CMD="eend"
368 return ${retval}
369 }
370
371 # void ewend(int error, char* errstr)
372 #
373 # indicate the completion of process
374 # if error, show errstr via ewarn
375 #
376 ewend() {
377 local retval="${1:-0}"
378 shift
379
380 _eend "${retval}" ewarn "$*"
381
382 LAST_E_CMD="ewend"
383 return ${retval}
384 }
385
386 # v-e-commands honor RC_VERBOSE which defaults to no.
387 # The condition is negated so the return value will be zero.
388 veinfo() { [[ ${RC_VERBOSE} != "yes" ]] || einfo "$@"; }
389 veinfon() { [[ ${RC_VERBOSE} != "yes" ]] || einfon "$@"; }
390 vewarn() { [[ ${RC_VERBOSE} != "yes" ]] || ewarn "$@"; }
391 veerror() { [[ ${RC_VERBOSE} != "yes" ]] || eerror "$@"; }
392 vebegin() { [[ ${RC_VERBOSE} != "yes" ]] || ebegin "$@"; }
393 veend() {
394 [[ ${RC_VERBOSE} == "yes" ]] && { eend "$@"; return $?; }
395 return ${1:-0}
396 }
397 vewend() {
398 [[ ${RC_VERBOSE} == "yes" ]] && { ewend "$@"; return $?; }
399 return ${1:-0}
400 }
401
402 # char *KV_major(string)
403 #
404 # Return the Major (X of X.Y.Z) kernel version
405 #
406 KV_major() {
407 [[ -z $1 ]] && return 1
408
409 local KV="$@"
410 echo "${KV%%.*}"
411 }
412
413 # char *KV_minor(string)
414 #
415 # Return the Minor (Y of X.Y.Z) kernel version
416 #
417 KV_minor() {
418 [[ -z $1 ]] && return 1
419
420 local KV="$@"
421 KV="${KV#*.}"
422 echo "${KV%%.*}"
423 }
424
425 # char *KV_micro(string)
426 #
427 # Return the Micro (Z of X.Y.Z) kernel version.
428 #
429 KV_micro() {
430 [[ -z $1 ]] && return 1
431
432 local KV="$@"
433 KV="${KV#*.*.}"
434 echo "${KV%%[^[:digit:]]*}"
435 }
436
437 # int KV_to_int(string)
438 #
439 # Convert a string type kernel version (2.4.0) to an int (132096)
440 # for easy compairing or versions ...
441 #
442 KV_to_int() {
443 [[ -z $1 ]] && return 1
444
445 local KV_MAJOR="$(KV_major "$1")"
446 local KV_MINOR="$(KV_minor "$1")"
447 local KV_MICRO="$(KV_micro "$1")"
448 local KV_int="$(( KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO ))"
449
450 # We make version 2.2.0 the minimum version we will handle as
451 # a sanity check ... if its less, we fail ...
452 if [[ ${KV_int} -ge 131584 ]] ; then
453 echo "${KV_int}"
454 return 0
455 fi
456
457 return 1
458 }
459
460 # int get_KV()
461 #
462 # Return the kernel version (major, minor and micro concated) as an integer.
463 # Assumes X and Y of X.Y.Z are numbers. Also assumes that some leading
464 # portion of Z is a number.
465 # e.g. 2.4.25, 2.6.10, 2.6.4-rc3, 2.2.40-poop, 2.0.15+foo
466 #
467 _RC_GET_KV_CACHE=""
468 get_KV() {
469 [[ -z ${_RC_GET_KV_CACHE} ]] \
470 && _RC_GET_KV_CACHE="$(uname -r)"
471
472 echo "$(KV_to_int "${_RC_GET_KV_CACHE}")"
473
474 return $?
475 }
476
477 # bool get_bootparam(param)
478 #
479 # return 0 if gentoo=param was passed to the kernel
480 #
481 # EXAMPLE: if get_bootparam "nodevfs" ; then ....
482 #
483 get_bootparam() {
484 local x copt params retval=1
485
486 [[ ! -r /proc/cmdline ]] && return 1
487
488 for copt in $(< /proc/cmdline) ; do
489 if [[ ${copt%=*} == "gentoo" ]] ; then
490 params=$(gawk -v PARAMS="${copt##*=}" '
491 BEGIN {
492 split(PARAMS, nodes, ",")
493 for (x in nodes)
494 print nodes[x]
495 }')
496
497 # Parse gentoo option
498 for x in ${params} ; do
499 if [[ ${x} == "$1" ]] ; then
500 # echo "YES"
501 retval=0
502 fi
503 done
504 fi
505 done
506
507 return ${retval}
508 }
509
510 # Safer way to list the contents of a directory,
511 # as it do not have the "empty dir bug".
512 #
513 # char *dolisting(param)
514 #
515 # print a list of the directory contents
516 #
517 # NOTE: quote the params if they contain globs.
518 # also, error checking is not that extensive ...
519 #
520 dolisting() {
521 local x=
522 local y=
523 local tmpstr=
524 local mylist=
525 local mypath="$*"
526
527 if [[ ${mypath%/\*} != "${mypath}" ]] ; then
528 mypath=${mypath%/\*}
529 fi
530
531 for x in ${mypath} ; do
532 [[ ! -e ${x} ]] && continue
533
534 if [[ ! -d ${x} ]] && [[ -L ${x} || -f ${x} ]] ; then
535 mylist="${mylist} $(ls "${x}" 2> /dev/null)"
536 else
537 [[ ${x%/} != "${x}" ]] && x=${x%/}
538
539 cd "${x}"; tmpstr=$(ls)
540
541 for y in ${tmpstr} ; do
542 mylist="${mylist} ${x}/${y}"
543 done
544 fi
545 done
546
547 echo "${mylist}"
548 }
549
550
551 # char *add_suffix(char * configfile)
552 #
553 # Returns a config file name with the softlevel suffix
554 # appended to it. For use with multi-config services.
555 add_suffix() {
556 if [[ ${RC_USE_CONFIG_PROFILE} != "yes" ]] ; then
557 echo "$1"
558 return 0
559 fi
560
561 local suffix="${SOFTLEVEL}"
562 [[ ${SOFTLEVEL} == "${BOOTLEVEL}" \
563 || ${SOFTLEVEL} == "reboot" \
564 || ${SOFTLEVEL} == "shutdown" \
565 || ${SOFTLEVEL} == "single" ]] \
566 && suffix="${DEFAULTLEVEL}"
567 if [[ -e "$1.${suffix}" ]] ; then
568 echo "$1.${suffix}"
569 else
570 echo "$1"
571 fi
572
573 return 0
574 }
575
576 # char *get_base_ver()
577 #
578 # get the version of baselayout that this system is running
579 #
580 get_base_ver() {
581 [[ ! -r /etc/gentoo-release ]] && return 0
582 local ver="$(</etc/gentoo-release)"
583 echo "${ver##* }"
584 }
585
586 # Network filesystems list for common use in rc-scripts.
587 # This variable is used in is_net_fs and other places such as
588 # localmount.
589 NET_FS_LIST="afs cifs coda davfs fuse gfs ncpfs nfs nfs4 ocfs2 shfs smbfs"
590
591 # bool is_net_fs(path)
592 #
593 # return 0 if path is the mountpoint of a networked filesystem
594 #
595 # EXAMPLE: if is_net_fs / ; then ...
596 #
597 is_net_fs() {
598 local fstype
599 # /proc/mounts is always accurate but may not always be available
600 if [[ -e /proc/mounts ]] ; then
601 fstype="$( sed -n -e '/^rootfs/!s:.* '"$1"' \([^ ]*\).*:\1:p' /proc/mounts )"
602 else
603 fstype="$( mount | sed -n -e 's:.* on '"$1"' type \([^ ]*\).*:\1:p' )"
604 fi
605 [[ " ${NET_FS_LIST} " == *" ${fstype} "* ]]
606 return $?
607 }
608
609 # bool is_net_fs(path)
610 #
611 # return 0 if path is under unionfs control
612 #
613 # EXAMPLE: if is_union_fs / ; then ...
614 #
615 is_union_fs() {
616 [[ ! -x /sbin/unionctl ]] && return 1
617 unionctl "$1" --list &>/dev/null
618 }
619
620 # bool is_uml_sys()
621 #
622 # return 0 if the currently running system is User Mode Linux
623 #
624 # EXAMPLE: if is_uml_sys ; then ...
625 #
626 is_uml_sys() {
627 grep -qs 'UML' /proc/cpuinfo
628 }
629
630 # bool is_vserver_sys()
631 #
632 # return 0 if the currently running system is a Linux VServer
633 #
634 # EXAMPLE: if is_vserver_sys ; then ...
635 #
636 is_vserver_sys() {
637 grep -qs '^s_context:[[:space:]]*[1-9]' /proc/self/status
638 }
639
640 # bool is_xenU_sys()
641 #
642 # return 0 if the currently running system is an unprivileged Xen domain
643 #
644 # EXAMPLE: if is_xenU_sys ; then ...
645 #
646 is_xenU_sys() {
647 [[ ! -d /proc/xen ]] && return 1
648 [[ ! -r /proc/xen/capabilities ]] && return 1
649 grep -q "control_d" /proc/xen/capabilities && return 1
650 return 0
651 }
652
653 # bool get_mount_fstab(path)
654 #
655 # return the parameters to pass to the mount command generated from fstab
656 #
657 # EXAMPLE: cmd=$( get_mount_fstab /proc )
658 # cmd=${cmd:--t proc none /proc}
659 # mount -n ${cmd}
660 #
661 get_mount_fstab() {
662 gawk '$1 ~ "^#" { next }
663 $2 == "'$*'" { stab="-t "$3" -o "$4" "$1" "$2; }
664 END { print stab; }
665 ' /etc/fstab
666 }
667
668 # char *reverse_list(list)
669 #
670 # Returns the reversed order of list
671 #
672 reverse_list() {
673 for (( i = $# ; i > 0 ; --i )) ; do
674 echo -n "${!i} "
675 done
676 }
677
678 # void start_addon(addon)
679 #
680 # Starts addon.
681 #
682 start_addon() {
683 local addon="$1"
684 (import_addon "${addon}-start.sh")
685 return 0
686 }
687
688 # void start_volumes()
689 #
690 # Starts all volumes in RC_VOLUME_ORDER.
691 #
692 start_volumes() {
693 local x=
694
695 for x in ${RC_VOLUME_ORDER} ; do
696 start_addon "${x}"
697 done
698
699 return 0
700 }
701
702 # void stop_addon(addon)
703 #
704 # Stops addon.
705 #
706 stop_addon() {
707 local addon=$1
708 (import_addon "${addon}-stop.sh")
709 return 0
710 }
711
712 # void stop_volumes()
713 #
714 # Stops all volumes in RC_VOLUME_ORDER (reverse order).
715 #
716 stop_volumes() {
717 local x=
718
719 for x in $(reverse_list ${RC_VOLUME_ORDER}) ; do
720 stop_addon "${x}"
721 done
722
723 return 0
724 }
725
726 # bool is_older_than(reference, files/dirs to check)
727 #
728 # return 0 if any of the files/dirs are newer than
729 # the reference file
730 #
731 # EXAMPLE: if is_older_than a.out *.o ; then ...
732 is_older_than() {
733 local x=
734 local ref="$1"
735 shift
736
737 for x in "$@" ; do
738 if [[ -d ${x} ]] ; then
739 is_older_than "${ref}" "${x}"/* && return 0
740 elif [[ ${x} -nt ${ref} ]] ; then
741 return 0
742 fi
743 done
744
745 return 1
746 }
747
748 # char* bash_variable(char *variable)
749 #
750 # Turns the given variable into something that bash can use
751 # Basically replaces anything not a-z,A-Z into a _
752 #
753 bash_variable() {
754 local args="$@"
755 LC_ALL=C echo "${args//[![:word:]]/_}"
756 }
757
758 # void requote()
759 #
760 # Requotes params so they're suitable to be eval'd, just like this would:
761 # set -- 1 2 "3 4"
762 # /usr/bin/getopt -- '' "$@" | sed 's/^ -- //'
763 #
764 requote() {
765 local q=\'
766 set -- "${@//\'/$q\'$q}" # quote inner instances of '
767 set -- "${@/#/$q}" # add ' to start of each param
768 set -- "${@/%/$q}" # add ' to end of each param
769 echo "$*"
770 }
771
772 # char* uniqify(char *arg, ...)
773 #
774 # Ensure that params are unique
775 #
776 uniqify() {
777 local result= x=
778 while [[ -n "$1" ]] ; do
779 [[ " ${result} " != *" $1 "* ]] && result="${result} $1"
780 shift
781 done
782 echo "${result# *}"
783 }
784
785 ##############################################################################
786 # #
787 # This should be the last code in here, please add all functions above!! #
788 # #
789 # *** START LAST CODE *** #
790 # #
791 ##############################################################################
792
793 if [[ -z ${EBUILD} ]] ; then
794 # Setup a basic $PATH. Just add system default to existing.
795 # This should solve both /sbin and /usr/sbin not present when
796 # doing 'su -c foo', or for something like: PATH= rcscript start
797 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
798
799 # Cache the CONSOLETYPE - this is important as backgrounded shells don't
800 # have a TTY. rc unsets it at the end of running so it shouldn't hang
801 # around
802 if [[ -z ${CONSOLETYPE} ]] ; then
803 export CONSOLETYPE="$( /sbin/consoletype 2>/dev/null )"
804 fi
805 if [[ ${CONSOLETYPE} == "serial" ]] ; then
806 RC_NOCOLOR="yes"
807 RC_ENDCOL="no"
808 fi
809
810 for arg in "$@" ; do
811 case "${arg}" in
812 # Lastly check if the user disabled it with --nocolor argument
813 --nocolor|-nc)
814 RC_NOCOLOR="yes"
815 ;;
816 esac
817 done
818
819 setup_defaultlevels
820
821 # If we are not /sbin/rc then ensure that we cannot change level variables
822 if [[ -n ${BASH_SOURCE} \
823 && ${BASH_SOURCE[${#BASH_SOURCE[@]}-1]} != "/sbin/rc" ]] ; then
824 declare -r BOOTLEVEL DEFAULTLEVEL SOFTLEVEL
825 fi
826 else
827 # Should we use colors ?
828 if [[ $* != *depend* ]] ; then
829 # Check user pref in portage
830 RC_NOCOLOR="$(portageq envvar NOCOLOR 2>/dev/null)"
831 [[ ${RC_NOCOLOR} == "true" ]] && RC_NOCOLOR="yes"
832 else
833 # We do not want colors during emerge depend
834 RC_NOCOLOR="yes"
835 # No output is seen during emerge depend, so this is not needed.
836 RC_ENDCOL="no"
837 fi
838 fi
839
840 if [[ -n ${EBUILD} && $* == *depend* ]] ; then
841 # We do not want stty to run during emerge depend
842 COLS=80
843 else
844 # Setup COLS and ENDCOL so eend can line up the [ ok ]
845 COLS="${COLUMNS:-0}" # bash's internal COLUMNS variable
846 (( COLS == 0 )) && COLS="$(set -- `stty size 2>/dev/null` ; echo "$2")"
847 (( COLS > 0 )) || (( COLS = 80 )) # width of [ ok ] == 7
848 fi
849
850 if [[ ${RC_ENDCOL} == "yes" ]] ; then
851 ENDCOL=$'\e[A\e['$(( COLS - 8 ))'C'
852 else
853 ENDCOL=''
854 fi
855
856 # Setup the colors so our messages all look pretty
857 if [[ ${RC_NOCOLOR} == "yes" ]] ; then
858 unset GOOD WARN BAD NORMAL HILITE BRACKET
859 else
860 if [[ ${RC_COLORS} == "pink" ]] ; then
861 GOOD=$'\e[35;01m'
862 else
863 GOOD=$'\e[32;01m'
864 fi
865 WARN=$'\e[33;01m'
866 BAD=$'\e[31;01m'
867 HILITE=$'\e[36;01m'
868 BRACKET=$'\e[34;01m'
869 NORMAL=$'\e[0m'
870 fi
871
872 ##############################################################################
873 # #
874 # *** END LAST CODE *** #
875 # #
876 # This should be the last code in here, please add all functions above!! #
877 # #
878 ##############################################################################
879
880
881 # vim:ts=4

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20