1 if [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1
3 # Copyright (c) 2006-2016 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
34 ############################################################ GLOBALS
37 # A Literal newline (for use with f_replace_all(), or IFS, or whatever)
43 # Valid characters that can appear in an sh(1) variable name
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
51 VALID_VARNAME_CHARS="0-9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"
53 ############################################################ FUNCTIONS
57 # Returns true if argument is a positive/negative whole integer.
62 [ "${arg:-x}" = "${arg%[!0-9]*}" ]
65 # f_substr [-v $var_to_set] $string $start [$length]
67 # Similar to awk(1)'s substr(), return length substring of string that begins
68 # at start position counted from 1.
70 case "$BASH_VERSION" in
76 -v) __var_to_set="$2"; shift 2 ;;
77 -v?*) __var_to_set="${2#-v}"; shift 1 ;;
79 local __tmp="$1" __start="${2:-1}" __len="$3"
80 [ "$__start" -gt 0 ] 2> /dev/null &&
81 __start=$(( $__start - 1 ))
82 if [ ! "$__var_to_set" ]; then
83 eval echo \"\${__tmp:\$__start${__len:+:\$__len}}\"
87 eval $__var_to_set=\"\${__tmp:\$__start:\$__len}\"
89 eval $__var_to_set=\"\${__tmp:\$__start}\"
94 # NB: On FreeBSD, sh(1) runs this faster than bash(1) runs the above
97 local OPTIND=1 OPTARG __flag __var_to_set=
98 while getopts v: __flag; do
100 v) __var_to_set="$OPTARG" ;;
103 shift $(( $OPTIND - 1 ))
105 local __tmp="$1" __start="${2:-1}" __size="$3"
106 local __tbuf __tbuf_len __trim __trimq
108 if [ ! "$__tmp" ]; then
109 [ "$__var_to_set" ] && setvar "$__var_to_set" ""
112 [ "$__start" -ge 1 ] 2> /dev/null || __start=1
113 if ! [ "${__size:-1}" -ge 1 ] 2> /dev/null; then
114 [ "$__var_to_set" ] && setvar "$__var_to_set" ""
118 __trim=$(( $__start - 1 ))
119 while [ $__trim -gt 0 ]; do
122 while [ $__tbuf_len -lt $(( $__trim / $__tbuf_len )) ]
125 __tbuf_len=$(( $__tbuf_len + 1 ))
127 __trimq=$(( $__trim / $__tbuf_len ))
128 __trim=$(( $__trim - $__tbuf_len * $__trimq ))
129 while [ $__trimq -gt 0 ]; do
130 __tmp="${__tmp#$__tbuf}"
131 __trimq=$(( $__trimq - 1 ))
135 local __tmp_size=${#__tmp}
136 local __mask __mask_len
137 __trim=$(( $__tmp_size - ${__size:-$__tmp_size} ))
138 while [ $__trim -gt 0 ]; do
141 if [ $__trim -le $__size ]; then
142 while [ $__tbuf_len -lt $((
143 $__trim / $__tbuf_len
146 __tbuf_len=$(( $__tbuf_len + 1 ))
148 __trimq=$(( $__trim / $__tbuf_len ))
149 __trim=$(( $__trim - $__tbuf_len * $__trimq ))
150 while [ $__trimq -gt 0 ]; do
151 __tmp="${__tmp%$__tbuf}"
152 __trimq=$(( $__trimq - 1 ))
156 while [ $__tbuf_len -lt $((
157 $__size / $__tbuf_len
160 __tbuf_len=$(( $__tbuf_len + 1 ))
162 __trimq=$(( $__size / $__tbuf_len ))
164 $__trimq * $__tbuf_len
167 __tbuf_len=$(( $__tbuf_len + 1 ))
170 $__tmp_size - $__tbuf_len * $__trimq
173 $__tmp_size - $__mask_len - $__size
175 while [ $__trimq -gt 0 ]; do
176 __mask="${__mask#$__tbuf}"
177 __trimq=$(( $__trimq - 1 ))
179 __tmp="${__tmp%"$__mask"}"
183 if [ "$__var_to_set" ]; then
184 setvar "$__var_to_set" "$__tmp"
191 # f_sprintf $var_to_set $format [$arguments ...]
193 # Similar to sprintf(3), write a string into $var_to_set using printf(1) syntax
194 # (`$format [$arguments ...]').
196 case "$BASH_VERSION" in
200 local __var_to_set="$1" __tmp
203 eval "$__var_to_set"=\"\${__tmp%\$NL}\"
207 # NB: On FreeBSD, sh(1) runs this faster than bash(1) runs the above
210 local __var_to_set="$1"
212 eval "$__var_to_set"=\$\( printf -- \"\$@\" \)
216 # f_vsprintf $var_to_set $format $format_args
218 # Similar to vsprintf(3), write a string into $var_to_set using printf(1)
219 # syntax (`$format $format_args').
223 eval f_sprintf \"\$1\" \"\$2\" $3
226 # f_snprintf $var_to_set $size $format [$arguments ...]
228 # Similar to snprintf(3), write at most $size number of bytes into $var_to_set
229 # using printf(1) syntax (`$format [$arguments ...]').
233 local __var_to_set="$1" __size="$2"
234 shift 2 # var_to_set size
236 local __f_snprintf_tmp
237 f_sprintf __f_snprintf_tmp "$@"
238 f_substr "$__var_to_set" "$__f_snprintf_tmp" 1 "$__size"
241 # f_vsnprintf $var_to_set $size $format $format_args
243 # Similar to vsnprintf(3), write at most $size number of bytes into $var_to_set
244 # using printf(1) syntax (`$format $format_args'). The value of $var_to_set is
245 # NULL unless at-least one byte is stored from the output.
249 # limit=7 format="%s"
250 # format_args="'abc 123'" # 3-spaces between abc and 123
251 # f_vsnprintf foo $limit "$format" "$format_args" # foo=[abc 1]
255 # limit=12 format="%s %s"
256 # format_args=" 'doghouse' 'fox' "
257 # # even more spaces added to illustrate escape-method
258 # f_vsnprintf foo $limit "$format" "$format_args" # foo=[doghouse fox]
262 # limit=13 format="%s %s"
263 # f_shell_escape arg1 'aaa"aaa' # arg1=[aaa"aaa] (no change)
264 # f_shell_escape arg2 "aaa'aaa" # arg2=[aaa'\''aaa] (escaped s-quote)
265 # format_args="'$arg1' '$arg2'" # use single-quotes to surround args
266 # f_vsnprintf foo $limit "$format" "$format_args" # foo=[aaa"aaa aaa'a]
268 # In all of the above examples, the call to f_vsnprintf() does not change. Only
269 # the contents of $limit, $format, and $format_args changes in each example.
273 eval f_snprintf \"\$1\" \"\$2\" \"\$3\" $4
276 # f_replaceall $string $find $replace [$var_to_set]
278 # Replace all occurrences of $find in $string with $replace. If $var_to_set is
279 # either missing or NULL, the variable name is produced on standard out for
280 # capturing in a sub-shell (which is less recommended due to performance
283 # To replace newlines or a sequence containing the newline character, use $NL
284 # as `\n' is not supported.
288 local __left="" __right="$1"
289 local __find="$2" __replace="$3" __var_to_set="$4"
291 case "$__right" in *$__find*)
292 __left="$__left${__right%%$__find*}$__replace"
293 __right="${__right#*$__find}"
298 __left="$__left${__right#*$__find}"
299 if [ "$__var_to_set" ]; then
300 setvar "$__var_to_set" "$__left"
306 # f_str2varname $string [$var_to_set]
308 # Convert a string into a suitable value to be used as a variable name
309 # by converting unsuitable characters into the underscrore [_]. If $var_to_set
310 # is either missing or NULL, the variable name is produced on standard out for
311 # capturing in a sub-shell (which is less recommended due to performance
316 local __string="$1" __var_to_set="$2"
317 f_replaceall "$__string" "[!$VALID_VARNAME_CHARS]" "_" "$__var_to_set"
320 # f_shell_escape $string [$var_to_set]
322 # Escape $string for shell eval statement(s) by replacing all single-quotes
323 # with a special sequence that creates a compound string when interpolated
324 # by eval with surrounding single-quotes.
329 # f_shell_escape "$foo" bar # bar=[abc'\''123]
330 # eval echo \'$bar\' # produces abc'123
332 # This is helpful when processing an argument list that has to retain its
333 # escaped structure for later evaluations.
335 # WARNING: Surrounding single-quotes are not added; this is the responsibility
336 # of the code passing the escaped values to eval (which also aids readability).
340 local __string="$1" __var_to_set="$2"
341 f_replaceall "$__string" "'" "'\\''" "$__var_to_set"
344 # f_shell_unescape $string [$var_to_set]
346 # The antithesis of f_shell_escape(), this function takes an escaped $string
352 # f_shell_escape "$foo" bar # bar=[abc'\''123]
353 # f_shell_unescape "$bar" # produces abc'123
357 local __string="$1" __var_to_set="$2"
358 f_replaceall "$__string" "'\\''" "'" "$__var_to_set"
361 # f_expand_number $string [$var_to_set]
363 # Unformat $string into a number, optionally to be stored in $var_to_set. This
364 # function follows the SI power of two convention.
368 # Prefix Description Multiplier
372 # T tera 1099511627776
373 # P peta 1125899906842624
374 # E exa 1152921504606846976
376 # NOTE: Prefixes are case-insensitive.
378 # Upon successful completion, success status is returned; otherwise the number
379 # -1 is produced ($var_to_set set to -1 or if $var_to_set is NULL or missing)
380 # on standard output. In the case of failure, the error status will be one of:
383 # 1 Given $string contains no digits
384 # 2 An unrecognized prefix was given
385 # 3 Result too large to calculate
389 local __string="$1" __var_to_set="$2"
390 local __cp __num __bshift __maxinput
392 # Remove any leading non-digits
393 __string="${__string#${__string%%[0-9]*}}"
395 # Store the numbers (no trailing suffix)
396 __num="${__string%%[!0-9]*}"
398 # Produce `-1' if string didn't contain any digits
399 if [ ! "$__num" ]; then
400 if [ "$__var_to_set" ]; then
401 setvar "$__var_to_set" -1
405 return 1 # 1 = "Given $string contains no digits"
408 # Remove all the leading numbers from the string to get at the prefix
409 __string="${__string#"$__num"}"
412 # Test for invalid prefix (and determine bitshift length)
415 ""|[[:space:]]*) # Shortcut
416 if [ "$__var_to_set" ]; then
417 setvar "$__var_to_set" $__num
422 [Kk]*) __bshift=10 ;;
423 [Mm]*) __bshift=20 ;;
424 [Gg]*) __bshift=30 ;;
425 [Tt]*) __bshift=40 ;;
426 [Pp]*) __bshift=50 ;;
427 [Ee]*) __bshift=60 ;;
430 if [ "$__var_to_set" ]; then
431 setvar "$__var_to_set" -1
435 return 2 # 2 = "An unrecognized prefix was given"
438 # Determine if the wheels fall off
439 __maxinput=$(( 0x7fffffffffffffff >> $__bshift ))
440 if [ $__num -gt $__maxinput ]; then
441 # Input (before expanding) would exceed 64-bit signed int
442 if [ "$__var_to_set" ]; then
443 setvar "$__var_to_set" -1
447 return 3 # 3 = "Result too large to calculate"
450 # Shift the number out and produce it
451 __num=$(( $__num << $__bshift ))
452 if [ "$__var_to_set" ]; then
453 setvar "$__var_to_set" $__num
459 # f_longest_line_length
461 # Simple wrapper to an awk(1) script to print the length of the longest line of
462 # input (read from stdin). Supports the newline escape-sequence `\n' for
463 # splitting a single line into multiple lines.
465 f_longest_line_length_awk='
466 BEGIN { longest = 0 }
468 if (split($0, lines, /\\n/) > 1)
472 len = length(lines[n])
473 longest = ( len > longest ? len : longest )
479 longest = ( len > longest ? len : longest )
482 END { print longest }
484 f_longest_line_length()
486 awk "$f_longest_line_length_awk"
491 # Simple wrapper to an awk(1) script to print the number of lines read from
492 # stdin. Supports newline escape-sequence `\n' for splitting a single line into
495 f_number_of_lines_awk='
496 BEGIN { num_lines = 0 }
498 num_lines += split(" "$0, unused, /\\n/)
500 END { print num_lines }
504 awk "$f_number_of_lines_awk"
507 # f_uriencode [$text]
509 # Encode $text for the purpose of embedding safely into a URL. Non-alphanumeric
510 # characters are converted to `%XX' sequence where XX represents the hexa-
511 # decimal ordinal of the non-alphanumeric character. If $text is missing, data
512 # is instead read from standard input.
517 for (n = 0; n < 256; n++) pack[sprintf("%c", n)] = sprintf("%%%02x", n)
522 for (n = 1; n <= slen; n++) {
523 char = substr($0, n, 1)
524 if ( char !~ /^[[:alnum:]_]$/ ) char = pack[char]
527 output = output ( output ? "%0a" : "" ) sline
533 if [ $# -gt 0 ]; then
534 echo "$1" | awk "$f_uriencode_awk"
536 awk "$f_uriencode_awk"
540 # f_uridecode [$text]
542 # Decode $text from a URI. Encoded characters are converted from their `%XX'
543 # sequence into original unencoded ASCII sequences. If $text is missing, data
544 # is instead read from standard input.
547 BEGIN { for (n = 0; n < 256; n++) chr[n] = sprintf("%c", n) }
551 for (n = 1; n <= slen; n++)
553 seq = substr($0, n, 3)
554 if ( seq ~ /^%[[:xdigit:]][[:xdigit:]]$/ ) {
555 hex = substr(seq, 2, 2)
556 sline = sline chr[sprintf("%u", "0x"hex)]
559 sline = sline substr(seq, 1, 1)
566 if [ $# -gt 0 ]; then
567 echo "$1" | awk "$f_uridecode_awk"
569 awk "$f_uridecode_awk"
573 ############################################################ MAIN
575 f_dprintf "%s: Successfully loaded." strings.subr
577 fi # ! $_STRINGS_SUBR