]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/bsdconfig/share/strings.subr
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.sbin / bsdconfig / share / strings.subr
1 if [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1
2 #
3 # Copyright (c) 2006-2013 Devin Teske
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE.
26 #
27 # $FreeBSD$
28 #
29 ############################################################ INCLUDES
30
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33
34 ############################################################ GLOBALS
35
36 #
37 # A Literal newline (for use with f_replace_all(), or IFS, or whatever)
38 #
39 NL="
40 " # END-QUOTE
41
42 #
43 # Valid characters that can appear in an sh(1) variable name
44 #
45 # Please note that the character ranges A-Z and a-z should be avoided because
46 # these can include accent characters (which are not valid in a variable name).
47 # For example, A-Z matches any character that sorts after A but before Z,
48 # including A and Z. Although ASCII order would make more sense, that is not
49 # how it works.
50 #
51 VALID_VARNAME_CHARS="0-9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"
52
53 ############################################################ FUNCTIONS
54
55 # f_substr "$string" $start [$length]
56 #
57 # Simple wrapper to awk(1)'s `substr' function.
58 #
59 f_substr()
60 {
61         local string="$1" start="${2:-0}" len="${3:-0}"
62         echo "$string" | awk "{ print substr(\$0, $start, $len) }"
63 }
64
65 # f_snprintf $var_to_set $size $format [$arguments ...]
66 #
67 # Similar to snprintf(3), write at most $size number of bytes into $var_to_set
68 # using printf(1) syntax (`$format [$arguments ...]'). The value of $var_to_set
69 # is NULL unless at-least one byte is stored from the output.
70 #
71 f_snprintf()
72 {
73         local __var_to_set="$1" __size="$2"
74         shift 2 # var_to_set size
75         eval "$__var_to_set"=\$\( printf -- \"\$@\" \| \
76                 awk -v max=\"\$__size\" \''
77         {
78                 len = length($0)
79                 max -= len
80                 print substr($0,0,(max > 0 ? len : max + len))
81                 if ( max < 0 ) exit
82                 max--
83         }'\' \)
84 }
85
86 # f_sprintf $var_to_set $format [$arguments ...]
87 #
88 # Similar to sprintf(3), write a string into $var_to_set using printf(1) syntax
89 # (`$format [$arguments ...]').
90 #
91 f_sprintf()
92 {
93         local __var_to_set="$1"
94         shift 1 # var_to_set
95         eval "$__var_to_set"=\$\( printf -- \"\$@\" \)
96 }
97
98 # f_vsnprintf $var_to_set $size $format $format_args
99 #
100 # Similar to vsnprintf(3), write at most $size number of bytes into $var_to_set
101 # using printf(1) syntax (`$format $format_args'). The value of $var_to_set is
102 # NULL unless at-least one byte is stored from the output.
103 #
104 # Example 1:
105 #
106 #       limit=7 format="%s"
107 #       format_args="'abc   123'" # 3-spaces between abc and 123
108 #       f_vsnprintf foo $limit "$format" "$format_args" # foo=[abc   1]
109 #
110 # Example 2:
111 #
112 #       limit=12 format="%s %s"
113 #       format_args="   'doghouse'      'foxhound'   "
114 #               # even more spaces added to illustrate escape-method
115 #       f_vsnprintf foo $limit "$format" "$format_args" # foo=[doghouse fox]
116 #
117 # Example 3:
118 #
119 #       limit=13 format="%s %s"
120 #       f_shell_escape arg1 'aaa"aaa' # arg1=[aaa"aaa] (no change)
121 #       f_shell_escape arg2 "aaa'aaa" # arg2=[aaa'\''aaa] (escaped s-quote)
122 #       format_args="'$arg1' '$arg2'" # use single-quotes to surround args
123 #       f_vsnprintf foo $limit "$format" "$format_args" # foo=[aaa"aaa aaa'a]
124 #
125 # In all of the above examples, the call to f_vsnprintf() does not change. Only
126 # the contents of $limit, $format, and $format_args changes in each example.
127 #
128 f_vsnprintf()
129 {
130         eval f_snprintf \"\$1\" \"\$2\" \"\$3\" $4
131 }
132
133 # f_vsprintf $var_to_set $format $format_args
134 #
135 # Similar to vsprintf(3), write a string into $var_to_set using printf(1)
136 # syntax (`$format $format_args').
137 #
138 f_vsprintf()
139 {
140         eval f_sprintf \"\$1\" \"\$2\" $3
141 }
142
143 # f_longest_line_length
144 #
145 # Simple wrapper to an awk(1) script to print the length of the longest line of
146 # input (read from stdin). Supports the newline escape-sequence `\n' for
147 # splitting a single line into multiple lines.
148 #
149 f_longest_line_length_awk='
150 BEGIN { longest = 0 }
151 {
152         if (split($0, lines, /\\n/) > 1)
153         {
154                 for (n in lines)
155                 {
156                         len = length(lines[n])
157                         longest = ( len > longest ? len : longest )
158                 }
159         }
160         else
161         {
162                 len = length($0)
163                 longest = ( len > longest ? len : longest )
164         }
165 }
166 END { print longest }
167 '
168 f_longest_line_length()
169 {
170         awk "$f_longest_line_length_awk"
171 }
172
173 # f_number_of_lines
174 #
175 # Simple wrapper to an awk(1) script to print the number of lines read from
176 # stdin. Supports newline escape-sequence `\n' for splitting a single line into
177 # multiple lines.
178 #
179 f_number_of_lines_awk='
180 BEGIN { num_lines = 0 }
181 {
182         num_lines += split(" "$0, unused, /\\n/)
183 }
184 END { print num_lines }
185 '
186 f_number_of_lines()
187 {
188         awk "$f_number_of_lines_awk"
189 }
190
191 # f_isinteger $arg
192 #
193 # Returns true if argument is a positive/negative whole integer.
194 #
195 f_isinteger()
196 {
197         local arg="${1#-}"
198         [ "${arg:-x}" = "${arg%[!0-9]*}" ]
199 }
200
201 # f_uriencode [$text]
202 #
203 # Encode $text for the purpose of embedding safely into a URL. Non-alphanumeric
204 # characters are converted to `%XX' sequence where XX represents the hexa-
205 # decimal ordinal of the non-alphanumeric character. If $text is missing, data
206 # is instead read from standard input.
207 #
208 f_uriencode_awk='
209 BEGIN {
210         output = ""
211         for (n = 0; n < 256; n++) pack[sprintf("%c", n)] = sprintf("%%%02x", n)
212 }
213 {
214         sline = ""
215         slen = length($0)
216         for (n = 1; n <= slen; n++) {
217                 char = substr($0, n, 1)
218                 if ( char !~ /^[[:alnum:]_]$/ ) char = pack[char]
219                 sline = sline char
220         }
221         output = output ( output ? "%0a" : "" ) sline
222 }
223 END { print output }
224 '
225 f_uriencode()
226 {
227         if [ $# -gt 0 ]; then
228                 echo "$1" | awk "$f_uriencode_awk"
229         else
230                 awk "$f_uriencode_awk"
231         fi
232 }
233
234 # f_uridecode [$text]
235 #
236 # Decode $text from a URI. Encoded characters are converted from their `%XX'
237 # sequence into original unencoded ASCII sequences. If $text is missing, data
238 # is instead read from standard input.
239 #
240 f_uridecode_awk='
241 BEGIN { for (n = 0; n < 256; n++) chr[n] = sprintf("%c", n) }
242 {
243         sline = ""
244         slen = length($0)
245         for (n = 1; n <= slen; n++)
246         {
247                 seq = substr($0, n, 3)
248                 if ( seq ~ /^%[[:xdigit:]][[:xdigit:]]$/ ) {
249                         hex = substr(seq, 2, 2)
250                         sline = sline chr[sprintf("%u", "0x"hex)]
251                         n += 2
252                 } else
253                         sline = sline substr(seq, 1, 1)
254         }
255         print sline
256 }
257 '
258 f_uridecode()
259 {
260         if [ $# -gt 0 ]; then
261                 echo "$1" | awk "$f_uridecode_awk"
262         else
263                 awk "$f_uridecode_awk"
264         fi
265 }
266
267 # f_replaceall $string $find $replace [$var_to_set]
268 #
269 # Replace all occurrences of $find in $string with $replace. If $var_to_set is
270 # either missing or NULL, the variable name is produced on standard out for
271 # capturing in a sub-shell (which is less recommended due to performance
272 # degradation).
273 #
274 # To replace newlines or a sequence containing the newline character, use $NL
275 # as `\n' is not supported.
276 #
277 f_replaceall()
278 {
279         local __left="" __right="$1"
280         local __find="$2" __replace="$3" __var_to_set="$4"
281         while :; do
282                 case "$__right" in *$__find*)
283                         __left="$__left${__right%%$__find*}$__replace"
284                         __right="${__right#*$__find}"
285                         continue
286                 esac
287                 break
288         done
289         __left="$__left${__right#*$__find}"
290         if [ "$__var_to_set" ]; then
291                 setvar "$__var_to_set" "$__left"
292         else
293                 echo "$__left"
294         fi
295 }
296
297 # f_str2varname $string [$var_to_set]
298 #
299 # Convert a string into a suitable value to be used as a variable name
300 # by converting unsuitable characters into the underscrore [_]. If $var_to_set
301 # is either missing or NULL, the variable name is produced on standard out for
302 # capturing in a sub-shell (which is less recommended due to performance
303 # degradation).
304 #
305 f_str2varname()
306 {
307         local __string="$1" __var_to_set="$2"
308         f_replaceall "$__string" "[!$VALID_VARNAME_CHARS]" "_" "$__var_to_set"
309 }
310
311 # f_shell_escape $string [$var_to_set]
312 #
313 # Escape $string for shell eval statement(s) by replacing all single-quotes
314 # with a special sequence that creates a compound string when interpolated
315 # by eval with surrounding single-quotes.
316 #
317 # For example:
318 #
319 #       foo="abc'123"
320 #       f_shell_escape "$foo" bar # bar=[abc'\''123]
321 #       eval echo \'$bar\' # produces abc'123
322 #
323 # This is helpful when processing an argument list that has to retain its
324 # escaped structure for later evaluations.
325 #
326 # WARNING: Surrounding single-quotes are not added; this is the responsibility
327 # of the code passing the escaped values to eval (which also aids readability).
328 #
329 f_shell_escape()
330 {
331         local __string="$1" __var_to_set="$2"
332         f_replaceall "$__string" "'" "'\\''" "$__var_to_set"
333 }
334
335 # f_shell_unescape $string [$var_to_set]
336 #
337 # The antithesis of f_shell_escape(), this function takes an escaped $string
338 # and expands it.
339 #
340 # For example:
341 #
342 #       foo="abc'123"
343 #       f_shell_escape "$foo" bar # bar=[abc'\''123]
344 #       f_shell_unescape "$bar" # produces abc'123
345 #
346 f_shell_unescape()
347 {
348         local __string="$1" __var_to_set="$2"
349         f_replaceall "$__string" "'\\''" "'" "$__var_to_set"
350 }
351
352 # f_expand_number $string [$var_to_set]
353 #
354 # Unformat $string into a number, optionally to be stored in $var_to_set. This
355 # function follows the SI power of two convention.
356 #
357 # The prefixes are:
358 #
359 #       Prefix  Description     Multiplier
360 #       k       kilo            1024
361 #       M       mega            1048576
362 #       G       giga            1073741824
363 #       T       tera            1099511627776
364 #       P       peta            1125899906842624
365 #       E       exa             1152921504606846976
366 #
367 # NOTE: Prefixes are case-insensitive.
368 #
369 # Upon successful completion, success status is returned; otherwise the number
370 # -1 is produced ($var_to_set set to -1 or if $var_to_set is NULL or missing)
371 # on standard output. In the case of failure, the error status will be one of:
372 #
373 #       Status  Reason
374 #       1       Given $string contains no digits
375 #       2       An unrecognized prefix was given
376 #       3       Result too large to calculate
377 #
378 f_expand_number()
379 {
380         local __string="$1" __var_to_set="$2"
381         local __cp __num __bshift __maxinput
382
383         # Remove any leading non-digits
384         __string="${__string#${__string%%[0-9]*}}"
385
386         # Store the numbers (no trailing suffix)
387         __num="${__string%%[!0-9]*}"
388
389         # Produce `-1' if string didn't contain any digits
390         if [ ! "$__num" ]; then
391                 if [ "$__var_to_set" ]; then
392                         setvar "$__var_to_set" -1
393                 else
394                         echo -1
395                 fi
396                 return 1 # 1 = "Given $string contains no digits"
397         fi
398
399         # Remove all the leading numbers from the string to get at the prefix
400         __string="${__string#"$__num"}"
401
402         #
403         # Test for invalid prefix (and determine bitshift length)
404         #
405         case "$__string" in
406         ""|[[:space:]]*) # Shortcut
407                 if [ "$__var_to_set" ]; then
408                         setvar "$__var_to_set" $__num
409                 else
410                         echo $__num
411                 fi
412                 return $SUCCESS ;;
413         [Kk]*) __bshift=10 ;;
414         [Mm]*) __bshift=20 ;;
415         [Gg]*) __bshift=30 ;;
416         [Tt]*) __bshift=40 ;;
417         [Pp]*) __bshift=50 ;;
418         [Ee]*) __bshift=60 ;;
419         *)
420                 # Unknown prefix
421                 if [ "$__var_to_set" ]; then
422                         setvar "$__var_to_set" -1
423                 else
424                         echo -1
425                 fi
426                 return 2 # 2 = "An unrecognized prefix was given"
427         esac
428
429         # Determine if the wheels fall off
430         __maxinput=$(( 0x7fffffffffffffff >> $__bshift ))
431         if [ $__num -gt $__maxinput ]; then
432                 # Input (before expanding) would exceed 64-bit signed int
433                 if [ "$__var_to_set" ]; then
434                         setvar "$__var_to_set" -1
435                 else
436                         echo -1
437                 fi
438                 return 3 # 3 = "Result too large to calculate"
439         fi
440
441         # Shift the number out and produce it
442         __num=$(( $__num << $__bshift ))
443         if [ "$__var_to_set" ]; then
444                 setvar "$__var_to_set" $__num
445         else
446                 echo $__num
447         fi
448 }
449
450 ############################################################ MAIN
451
452 f_dprintf "%s: Successfully loaded." strings.subr
453
454 fi # ! $_STRINGS_SUBR