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

Contents of /trunk/sh/functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2650 - (show annotations) (download) (as text)
Tue Apr 24 11:29:19 2007 UTC (7 years, 7 months ago) by uberlord
File MIME type: text/x-sh
File size: 3067 byte(s)
We now buffer stdout and stderr to a file and flush that when running in parallel. RC_PARALLEL_STARTUP has been renamed to RC_PARALLEL.
1 # Copyright 1999-2007 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # Please keep this useable by every shell in portage
5
6 RC_GOT_FUNCTIONS="yes"
7
8 eindent() {
9 RC_EINDENT=$((${RC_EINDENT:-0} + 2))
10 [ "${RC_EINDENT}" -gt 40 ] && RC_EINDENT=40
11 export RC_EINDENT
12 }
13
14 eoutdent() {
15 RC_EINDENT=$((${RC_EINDENT:-0} - 2))
16 [ "${RC_EINDENT}" -lt 0 ] && RC_EINDENT=0
17 return 0
18 }
19
20 # void esyslog(char* priority, char* tag, char* message)
21 #
22 # use the system logger to log a message
23 #
24 esyslog() {
25 local pri= tag=
26
27 if [ -x /usr/bin/logger ] ; then
28 pri="$1"
29 tag="$2"
30
31 shift 2
32 [ -z "$*" ] && return 0
33
34 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
35 fi
36
37 return 0
38 }
39
40 # Safer way to list the contents of a directory,
41 # as it do not have the "empty dir bug".
42 #
43 # char *dolisting(param)
44 #
45 # print a list of the directory contents
46 #
47 # NOTE: quote the params if they contain globs.
48 # also, error checking is not that extensive ...
49 #
50 dolisting() {
51 local x= y= mylist= mypath="$*"
52
53 # Here we use file globbing instead of ls to save on forking
54 for x in ${mypath} ; do
55 [ ! -e "${x}" ] && continue
56
57 if [ -L "${x}" -o -f "${x}" ] ; then
58 mylist="${mylist} "${x}
59 elif [ -d "${x}" ] ; then
60 [ "${x%/}" != "${x}" ] && x=${x%/}
61
62 for y in "${x}"/* ; do
63 [ -e "${y}" ] && mylist="${mylist} ${y}"
64 done
65 fi
66 done
67
68 echo "${mylist# *}"
69 }
70
71 # bool is_older_than(reference, files/dirs to check)
72 #
73 # return 0 if any of the files/dirs are newer than
74 # the reference file
75 #
76 # EXAMPLE: if is_older_than a.out *.o ; then ...
77 is_older_than() {
78 local x= ref="$1"
79 shift
80
81 for x in "$@" ; do
82 [ -e "${x}" ] || continue
83 # We need to check the mtime if it's a directory too as the
84 # contents may have changed.
85 [ "${x}" -nt "${ref}" ] && return 0
86 [ -d "${x}" ] && is_older_than "${ref}" "${x}"/* && return 0
87 done
88
89 return 1
90 }
91
92 uniqify() {
93 local result=
94 while [ -n "$1" ] ; do
95 case " ${result} " in
96 *" $1 "*) ;;
97 *) result="${result} $1" ;;
98 esac
99 shift
100 done
101 echo "${result# *}"
102 }
103
104 KV_to_int() {
105 [ -z $1 ] && return 1
106
107 local KV_MAJOR=${1%%.*}
108 local x=${1#*.}
109 local KV_MINOR=${x%%.*}
110 x=${1#*.*.}
111 local KV_MICRO=${x%%-*}
112 local KV_int=$((${KV_MAJOR} * 65536 + ${KV_MINOR} * 256 + ${KV_MICRO} ))
113
114 # We make version 2.2.0 the minimum version we will handle as
115 # a sanity check ... if its less, we fail ...
116 [ "${KV_int}" -lt 131584 ] && return 1
117
118 echo "${KV_int}"
119 }
120
121 # Setup a basic $PATH. Just add system default to existing.
122 # This should solve both /sbin and /usr/sbin not present when
123 # doing 'su -c foo', or for something like: PATH= rcscript start
124 case "${PATH}" in
125 /lib/rcscripts/bin:/bin:/sbin:/usr/bin:/usr/sbin) ;;
126 /lib/rcscripts/bin:/bin:/sbin:/usr/bin:/usr/sbin:*) ;;
127 *) export PATH="/lib/rcscripts/bin:/bin:/sbin:/usr/bin:/usr/sbin:${PATH}" ;;
128 esac
129
130 for arg in "$@" ; do
131 case "${arg}" in
132 --nocolor|--nocolour)
133 export RC_NOCOLOR="yes"
134 ;;
135 esac
136 done
137
138 if [ "${RC_NOCOLOR}" != "yes" -a -z "${GOOD}" ] ; then
139 eval $(eval_ecolors)
140 fi
141
142 # vim: set ts=4 :

  ViewVC Help
Powered by ViewVC 1.1.20