slitaz-base-files view rootfs/lib/libtaz.sh @ rev 286

/sbin/mktazdevs.sh: fix /dev/shm permission
author Aleksej Bobylev <al.bobylev@gmail.com>
date Thu Oct 08 00:32:28 2015 +0300 (2015-10-08)
parents 3a790a182103
children efc8f188db6e
line source
1 #!/bin/sh
2 #
3 # SliTaz Base functions.
4 # Documentation: `man libtaz` or /usr/share/doc/slitaz/libtaz.txt
5 #
6 # Copyright (C) 2012-2015 SliTaz GNU/Linux - BSD License
7 #
9 . /usr/bin/gettext.sh
10 alias sed="busybox sed"
12 # Internal
13 lgettext() { gettext -d 'slitaz-base' "$@"; }
14 translate_query() {
15 case $1 in
16 y) lgettext "y";;
17 Y) lgettext "Y";;
18 n) lgettext "n";;
19 N) lgettext "N";;
20 # Support other cases but keep them untranslated.
21 *) echo "$1" ;;
22 esac
23 }
24 okmsg="$(lgettext 'Done')"
25 ermsg="$(lgettext 'Failed')"
26 : ${okcolor=32}
27 : ${ercolor=31}
28 : ${decolor=36}
30 # Parse cmdline options and store values in a variable.
31 for opt in "$@"; do
32 opt_name="${opt%%=*}"; opt_name="$(echo -n "${opt_name#--}" | tr -c 'a-zA-Z0-9' '_')"
33 case "$opt" in
34 --[0-9]*=*) export _$opt_name="${opt#*=}" ;;
35 --[0-9]*) export _$opt_name=yes ;;
36 --*=*) export $opt_name="${opt#*=}" ;;
37 --*) export $opt_name=yes ;;
38 esac
39 done
40 [ "$HTTP_REFERER" ] && output='html'
45 # i18n functions
46 _() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; }
47 _n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; }
48 _p() { local S="$1" P="$2" N="$3"; shift 3; printf "$(ngettext "$S" "$P" "$N")" "$@"; }
50 # Get terminal columns
51 get_cols() { stty size 2>/dev/null | busybox cut -d' ' -f2; }
53 # Last command status
54 status() {
55 local check=$?
56 case $output in
57 raw|gtk)
58 done=" $okmsg"
59 error=" $ermsg";;
60 html)
61 done=" <span class=\"float-right color$okcolor\">$okmsg</span>"
62 error=" <span class=\"float-right color$ercolor\">$ermsg</span>";;
63 *)
64 local cols=$(get_cols)
65 local scol=$((${cols:-80} - 10))
66 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
67 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]";;
68 esac
69 case $check in
70 0) echo -e "$done";;
71 *) echo -e "$error";;
72 esac
73 }
75 # Line separator
76 separator() {
77 case $output in
78 raw|gtk) echo '--------';;
79 html) echo -n '<hr/>';;
80 *)
81 local cols=$(get_cols)
82 printf "%${cols:-80}s\n" | tr ' ' "${1:-=}";;
83 esac
84 }
86 # New line
87 newline() { echo; }
89 # Display a bold message
90 boldify() {
91 case $output in
92 raw) echo "$@" ;;
93 gtk) echo "<b>$@</b>" ;;
94 html) echo "<strong>$@</strong>" ;;
95 *) echo -e "\\033[1m$@\\033[0m" ;;
96 esac
97 }
99 # Colorize message
100 colorize() {
101 : ${color=$1}
102 shift
103 case $output in
104 raw|gtk) echo "$@";;
105 html) echo -n "<span class=\"color$color\">$@</span>";;
106 *) case $color in
107 0*) echo -e "\\033[${color:-38}m$@\\033[39m";;
108 *) echo -e "\\033[1;${color:-38}m$@\\033[0;39m";;
109 esac;;
110 esac
111 unset color
112 }
114 # Indent text
115 indent() {
116 local in="$1"
117 shift
118 echo -e "\033["$in"G $@";
119 }
121 # Extended MeSsaGe output
122 emsg() {
123 local sep="\n--------\n"
124 case $output in
125 raw)
126 echo "$@" | sed -e 's|<b>||g; s|</b>||g; s|<c [0-9]*>||g; \
127 s|</c>||g; s|<->|'$sep'|g; s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
128 gtk)
129 echo "$@" | sed -e 's|<c [0-9]*>||g; s|</c>||g; s|<->|'$sep'|g; \
130 s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
131 html)
132 echo "$@" | sed -e 's|<b>|<strong>|g; s|</b>|</strong>|g; \
133 s|<c \([0-9]*\)>|<span class="color\1">|g; s|</c>|</span>|g; \
134 s|<n>|<br/>|g; s|<->|<hr/>|g; s|<i [0-9]*>| |g' ;;
135 *)
136 local sep="\n"
137 local cols=$(get_cols)
138 [ "$cols" ] || cols=80
139 for c in $(seq 1 $cols); do
140 sep="${sep}="
141 done
142 echo -en "$(echo "$@" | sed -e 's|<b>|\\033[1m|g; s|</b>|\\033[0m|g;
143 s|<c 0\([0-9]*\)>|\\033[\1m|g; s|<c \([1-9][0-9]*\)>|\\033[1;\1m|g;
144 s|</c>|\\033[0;39m|g; s|<n>|\n|g;
145 s|<->|'$sep'|g; s|<i \([0-9]*\)>|\\033[\1G|g')"
146 [ "$1" != "-n" ] && echo
147 ;;
148 esac
149 }
151 # Check if user is logged as root
152 check_root() {
153 if [ $(id -u) != 0 ]; then
154 lgettext "You must be root to execute:"; echo " $(basename $0) $@"
155 exit 1
156 fi
157 }
159 # Display debug info when --debug is used.
160 debug() {
161 [ -n "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1"
162 }
164 # Confirmation
165 confirm() {
166 if [ -n "$yes" ]; then
167 true
168 else
169 if [ -n "$1" ]; then
170 echo -n "$1 "
171 else
172 echo -n " ($(translate_query y)/$(translate_query N)) ? "
173 fi
174 read answer
175 [ "$answer" == "$(translate_query y)" ]
176 fi
177 }
179 # Log activities
180 log() {
181 echo "$(date '+%F %R') : $@" >> ${activity:-/var/log/slitaz/libtaz.log}
182 }
184 # Print two-column list of options with descriptions
185 optlist() {
186 local in cols col1=1 line
187 in="$(echo "$1" | sed 's| *| |g')"
188 cols=$(get_cols); [ "$cols" ] || cols=80
189 IFS=$'\n'
190 for line in $in; do
191 col=$(echo -n "$line" | cut -f1 | wc -m)
192 [ $col -gt $col1 ] && col1=$col
193 done
194 echo "$in" | sed 's|\t|&\n|' | fold -sw$((cols - col1 - 4)) | \
195 sed "/\t/!{s|^.*$|[$((col1 + 4))G&|g}" | sed "/\t$/{N;s|.*| &|;s|\t\n||}"
196 }
198 # Wrap words in long terminal message
199 longline() {
200 cols=$(get_cols)
201 echo -e "$@" | fold -sw${cols:-80}
202 }
204 # Print localized title
205 title() {
206 case $output in
207 html) echo "<section><header>$(_ "$@")</header><pre class=\"scroll\">";;
208 *) newline; boldify "$(_ "$@")"; separator;;
209 esac
210 }
212 # Print footer
213 footer() {
214 case $output in
215 html) echo "</pre><footer>$1</footer></section>";;
216 *) separator; echo "$1"; [ -n "$1" ] && newline;;
217 esac
218 }
220 # Print current action
221 action() {
222 case $output in
223 raw|gtk|html) _n "$@";;
224 *) echo -ne "\033[0;33m$(_ "$@")\033[0m";;
225 esac
226 }
228 # Print long line as list item
229 itemize() {
230 case $output in
231 gtk) echo "$@";;
232 *)
233 local inp="$@" cols=$(get_cols) first offset
234 cols="${cols:-80}"
235 first="$(echo -e "$inp" | fold -sw$cols | head -n1)"
236 echo "$first"
237 cols1="$(echo "${first:1}" | wc -c)"
238 offset=$(echo "$first" | sed -n 's|^\([^:\*-]*[:\*-]\).*$|\1|p' | wc -m)
239 echo "${inp:$cols1}" | fold -sw$((cols - offset)) | awk \
240 '($0){printf "%'$offset's%s\n","",$0}'
241 ;;
242 esac
243 }