xref: /freebsd/usr.sbin/bsdconfig/share/strings.subr (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1if [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1
2#
3# Copyright (c) 2006-2016 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#
28############################################################ INCLUDES
29
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32
33############################################################ GLOBALS
34
35#
36# A Literal newline (for use with f_replace_all(), or IFS, or whatever)
37#
38NL="
39" # END-QUOTE
40
41#
42# Valid characters that can appear in an sh(1) variable name
43#
44# Please note that the character ranges A-Z and a-z should be avoided because
45# these can include accent characters (which are not valid in a variable name).
46# For example, A-Z matches any character that sorts after A but before Z,
47# including A and Z. Although ASCII order would make more sense, that is not
48# how it works.
49#
50VALID_VARNAME_CHARS="0-9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"
51
52############################################################ FUNCTIONS
53
54# f_isinteger $arg
55#
56# Returns true if argument is a positive/negative whole integer.
57#
58f_isinteger()
59{
60	local arg="${1#-}"
61	[ "${arg:-x}" = "${arg%[!0-9]*}" ]
62}
63
64# f_substr [-v $var_to_set] $string $start [$length]
65#
66# Similar to awk(1)'s substr(), return length substring of string that begins
67# at start position counted from 1.
68#
69case "$BASH_VERSION" in
70*?*)
71	f_substr()
72	{
73		local __var_to_set=
74		case "$1" in
75		-v) __var_to_set="$2"; shift 2 ;;
76		-v?*) __var_to_set="${2#-v}"; shift 1 ;;
77		esac
78		local __tmp="$1" __start="${2:-1}"  __len="$3"
79		[ "$__start" -gt 0 ] 2> /dev/null &&
80			__start=$(( $__start - 1 ))
81		if [ ! "$__var_to_set" ]; then
82			eval echo \"\${__tmp:\$__start${__len:+:\$__len}}\"
83			return $?
84		fi
85		if [ "$__len" ]; then
86			eval $__var_to_set=\"\${__tmp:\$__start:\$__len}\"
87		else
88			eval $__var_to_set=\"\${__tmp:\$__start}\"
89		fi
90	}
91	;;
92*)
93	# NB: On FreeBSD, sh(1) runs this faster than bash(1) runs the above
94	f_substr()
95	{
96		local OPTIND=1 OPTARG __flag __var_to_set=
97		while getopts v: __flag; do
98			case "$__flag" in
99			v) __var_to_set="$OPTARG" ;;
100			esac
101		done
102		shift $(( $OPTIND - 1 ))
103
104		local __tmp="$1" __start="${2:-1}" __size="$3"
105		local __tbuf __tbuf_len __trim __trimq
106
107		if [ ! "$__tmp" ]; then
108			[ "$__var_to_set" ] && setvar "$__var_to_set" ""
109			return ${SUCCESS:-0}
110		fi
111		[ "$__start" -ge 1 ] 2> /dev/null || __start=1
112		if ! [ "${__size:-1}" -ge 1 ] 2> /dev/null; then
113			[ "$__var_to_set" ] && setvar "$__var_to_set" ""
114			return ${FAILURE:-1}
115		fi
116
117		__trim=$(( $__start - 1 ))
118		while [ $__trim -gt 0 ]; do
119			__tbuf="?"
120			__tbuf_len=1
121			while [ $__tbuf_len -lt $(( $__trim / $__tbuf_len )) ]
122			do
123				__tbuf="$__tbuf?"
124				__tbuf_len=$(( $__tbuf_len + 1 ))
125			done
126			__trimq=$(( $__trim / $__tbuf_len ))
127			__trim=$(( $__trim - $__tbuf_len * $__trimq ))
128			while [ $__trimq -gt 0 ]; do
129				__tmp="${__tmp#$__tbuf}"
130				__trimq=$(( $__trimq - 1 ))
131			done
132		done
133
134		local __tmp_size=${#__tmp}
135		local __mask __mask_len
136		__trim=$(( $__tmp_size - ${__size:-$__tmp_size} ))
137		while [ $__trim -gt 0 ]; do
138			__tbuf="?"
139			__tbuf_len=1
140			if [ $__trim -le $__size ]; then
141				while [ $__tbuf_len -lt $((
142					$__trim / $__tbuf_len
143				)) ]; do
144					__tbuf="$__tbuf?"
145					__tbuf_len=$(( $__tbuf_len + 1 ))
146				done
147				__trimq=$(( $__trim / $__tbuf_len ))
148				__trim=$(( $__trim - $__tbuf_len * $__trimq ))
149				while [ $__trimq -gt 0 ]; do
150					__tmp="${__tmp%$__tbuf}"
151					__trimq=$(( $__trimq - 1 ))
152				done
153			else
154				__mask="$__tmp"
155				while [ $__tbuf_len -lt $((
156					$__size / $__tbuf_len
157				)) ]; do
158					__tbuf="$__tbuf?"
159					__tbuf_len=$(( $__tbuf_len + 1 ))
160				done
161				__trimq=$(( $__size / $__tbuf_len ))
162				if [ $__size -ne $((
163					$__trimq * $__tbuf_len
164				)) ]; then
165					__tbuf="$__tbuf?"
166					__tbuf_len=$(( $__tbuf_len + 1 ))
167				fi
168				__mask_len=$((
169					$__tmp_size - $__tbuf_len * $__trimq
170				))
171				__trim=$((
172					$__tmp_size - $__mask_len - $__size
173				))
174				while [ $__trimq -gt 0 ]; do
175					__mask="${__mask#$__tbuf}"
176					__trimq=$(( $__trimq - 1 ))
177				done
178				__tmp="${__tmp%"$__mask"}"
179			fi
180		done
181
182		if [ "$__var_to_set" ]; then
183			setvar "$__var_to_set" "$__tmp"
184		else
185			echo "$__tmp"
186		fi
187	}
188esac
189
190# f_sprintf $var_to_set $format [$arguments ...]
191#
192# Similar to sprintf(3), write a string into $var_to_set using printf(1) syntax
193# (`$format [$arguments ...]').
194#
195case "$BASH_VERSION" in
1963.1*|4.*)
197	f_sprintf()
198	{
199		local __var_to_set="$1" __tmp
200		shift 1 # var_to_set
201		printf -v __tmp "$@"
202		eval "$__var_to_set"=\"\${__tmp%\$NL}\"
203	}
204	;;
205*)
206	# NB: On FreeBSD, sh(1) runs this faster than bash(1) runs the above
207	f_sprintf()
208	{
209		local __var_to_set="$1"
210		shift 1 # var_to_set
211		eval "$__var_to_set"=\$\( printf -- \"\$@\" \)
212	}
213esac
214
215# f_vsprintf $var_to_set $format $format_args
216#
217# Similar to vsprintf(3), write a string into $var_to_set using printf(1)
218# syntax (`$format $format_args').
219#
220f_vsprintf()
221{
222	eval f_sprintf \"\$1\" \"\$2\" $3
223}
224
225# f_snprintf $var_to_set $size $format [$arguments ...]
226#
227# Similar to snprintf(3), write at most $size number of bytes into $var_to_set
228# using printf(1) syntax (`$format [$arguments ...]').
229#
230f_snprintf()
231{
232	local __var_to_set="$1" __size="$2"
233	shift 2 # var_to_set size
234
235	local __f_snprintf_tmp
236	f_sprintf __f_snprintf_tmp "$@"
237	f_substr "$__var_to_set" "$__f_snprintf_tmp" 1 "$__size"
238}
239
240# f_vsnprintf $var_to_set $size $format $format_args
241#
242# Similar to vsnprintf(3), write at most $size number of bytes into $var_to_set
243# using printf(1) syntax (`$format $format_args'). The value of $var_to_set is
244# NULL unless at-least one byte is stored from the output.
245#
246# Example 1:
247#
248# 	limit=7 format="%s"
249# 	format_args="'abc   123'" # 3-spaces between abc and 123
250# 	f_vsnprintf foo $limit "$format" "$format_args" # foo=[abc   1]
251#
252# Example 2:
253#
254# 	limit=12 format="%s %s"
255# 	format_args="   'doghouse'      'fox'   "
256# 		# even more spaces added to illustrate escape-method
257# 	f_vsnprintf foo $limit "$format" "$format_args" # foo=[doghouse fox]
258#
259# Example 3:
260#
261# 	limit=13 format="%s %s"
262# 	f_shell_escape arg1 'aaa"aaa' # arg1=[aaa"aaa] (no change)
263# 	f_shell_escape arg2 "aaa'aaa" # arg2=[aaa'\''aaa] (escaped s-quote)
264# 	format_args="'$arg1' '$arg2'" # use single-quotes to surround args
265# 	f_vsnprintf foo $limit "$format" "$format_args" # foo=[aaa"aaa aaa'a]
266#
267# In all of the above examples, the call to f_vsnprintf() does not change. Only
268# the contents of $limit, $format, and $format_args changes in each example.
269#
270f_vsnprintf()
271{
272	eval f_snprintf \"\$1\" \"\$2\" \"\$3\" $4
273}
274
275# f_replaceall $string $find $replace [$var_to_set]
276#
277# Replace all occurrences of $find in $string with $replace. If $var_to_set is
278# either missing or NULL, the variable name is produced on standard out for
279# capturing in a sub-shell (which is less recommended due to performance
280# degradation).
281#
282# To replace newlines or a sequence containing the newline character, use $NL
283# as `\n' is not supported.
284#
285f_replaceall()
286{
287	local __left="" __right="$1"
288	local __find="$2" __replace="$3" __var_to_set="$4"
289	while :; do
290		case "$__right" in *$__find*)
291			__left="$__left${__right%%$__find*}$__replace"
292			__right="${__right#*$__find}"
293			continue
294		esac
295		break
296	done
297	__left="$__left${__right#*$__find}"
298	if [ "$__var_to_set" ]; then
299		setvar "$__var_to_set" "$__left"
300	else
301		echo "$__left"
302	fi
303}
304
305# f_str2varname $string [$var_to_set]
306#
307# Convert a string into a suitable value to be used as a variable name
308# by converting unsuitable characters into the underscrore [_]. If $var_to_set
309# is either missing or NULL, the variable name is produced on standard out for
310# capturing in a sub-shell (which is less recommended due to performance
311# degradation).
312#
313f_str2varname()
314{
315	local __string="$1" __var_to_set="$2"
316	f_replaceall "$__string" "[!$VALID_VARNAME_CHARS]" "_" "$__var_to_set"
317}
318
319# f_shell_escape $string [$var_to_set]
320#
321# Escape $string for shell eval statement(s) by replacing all single-quotes
322# with a special sequence that creates a compound string when interpolated
323# by eval with surrounding single-quotes.
324#
325# For example:
326#
327# 	foo="abc'123"
328# 	f_shell_escape "$foo" bar # bar=[abc'\''123]
329# 	eval echo \'$bar\' # produces abc'123
330#
331# This is helpful when processing an argument list that has to retain its
332# escaped structure for later evaluations.
333#
334# WARNING: Surrounding single-quotes are not added; this is the responsibility
335# of the code passing the escaped values to eval (which also aids readability).
336#
337f_shell_escape()
338{
339	local __string="$1" __var_to_set="$2"
340	f_replaceall "$__string" "'" "'\\''" "$__var_to_set"
341}
342
343# f_shell_unescape $string [$var_to_set]
344#
345# The antithesis of f_shell_escape(), this function takes an escaped $string
346# and expands it.
347#
348# For example:
349#
350# 	foo="abc'123"
351# 	f_shell_escape "$foo" bar # bar=[abc'\''123]
352# 	f_shell_unescape "$bar" # produces abc'123
353#
354f_shell_unescape()
355{
356	local __string="$1" __var_to_set="$2"
357	f_replaceall "$__string" "'\\''" "'" "$__var_to_set"
358}
359
360# f_expand_number $string [$var_to_set]
361#
362# Unformat $string into a number, optionally to be stored in $var_to_set. This
363# function follows the SI power of two convention.
364#
365# The prefixes are:
366#
367# 	Prefix	Description	Multiplier
368# 	k	kilo		1024
369# 	M	mega		1048576
370# 	G	giga		1073741824
371# 	T	tera		1099511627776
372# 	P	peta		1125899906842624
373# 	E	exa		1152921504606846976
374#
375# NOTE: Prefixes are case-insensitive.
376#
377# Upon successful completion, success status is returned; otherwise the number
378# -1 is produced ($var_to_set set to -1 or if $var_to_set is NULL or missing)
379# on standard output. In the case of failure, the error status will be one of:
380#
381# 	Status	Reason
382# 	1	Given $string contains no digits
383# 	2	An unrecognized prefix was given
384# 	3	Result too large to calculate
385#
386f_expand_number()
387{
388	local __string="$1" __var_to_set="$2"
389	local __cp __num __bshift __maxinput
390
391	# Remove any leading non-digits
392	__string="${__string#${__string%%[0-9]*}}"
393
394	# Store the numbers (no trailing suffix)
395	__num="${__string%%[!0-9]*}"
396
397	# Produce `-1' if string didn't contain any digits
398	if [ ! "$__num" ]; then
399		if [ "$__var_to_set" ]; then
400			setvar "$__var_to_set" -1
401		else
402			echo -1
403		fi
404		return 1 # 1 = "Given $string contains no digits"
405	fi
406
407	# Remove all the leading numbers from the string to get at the prefix
408	__string="${__string#"$__num"}"
409
410	#
411	# Test for invalid prefix (and determine bitshift length)
412	#
413	case "$__string" in
414	""|[[:space:]]*) # Shortcut
415		if [ "$__var_to_set" ]; then
416			setvar "$__var_to_set" $__num
417		else
418			echo $__num
419		fi
420		return $SUCCESS ;;
421	[Kk]*) __bshift=10 ;;
422	[Mm]*) __bshift=20 ;;
423	[Gg]*) __bshift=30 ;;
424	[Tt]*) __bshift=40 ;;
425	[Pp]*) __bshift=50 ;;
426	[Ee]*) __bshift=60 ;;
427	*)
428		# Unknown prefix
429		if [ "$__var_to_set" ]; then
430			setvar "$__var_to_set" -1
431		else
432			echo -1
433		fi
434		return 2 # 2 = "An unrecognized prefix was given"
435	esac
436
437	# Determine if the wheels fall off
438	__maxinput=$(( 0x7fffffffffffffff >> $__bshift ))
439	if [ $__num -gt $__maxinput ]; then
440		# Input (before expanding) would exceed 64-bit signed int
441		if [ "$__var_to_set" ]; then
442			setvar "$__var_to_set" -1
443		else
444			echo -1
445		fi
446		return 3 # 3 = "Result too large to calculate"
447	fi
448
449	# Shift the number out and produce it
450	__num=$(( $__num << $__bshift ))
451	if [ "$__var_to_set" ]; then
452		setvar "$__var_to_set" $__num
453	else
454		echo $__num
455	fi
456}
457
458# f_longest_line_length
459#
460# Simple wrapper to an awk(1) script to print the length of the longest line of
461# input (read from stdin). Supports the newline escape-sequence `\n' for
462# splitting a single line into multiple lines.
463#
464f_longest_line_length_awk='
465BEGIN { longest = 0 }
466{
467	if (split($0, lines, /\\n/) > 1)
468	{
469		for (n in lines)
470		{
471			len = length(lines[n])
472			longest = ( len > longest ? len : longest )
473		}
474	}
475	else
476	{
477		len = length($0)
478		longest = ( len > longest ? len : longest )
479	}
480}
481END { print longest }
482'
483f_longest_line_length()
484{
485	awk "$f_longest_line_length_awk"
486}
487
488# f_number_of_lines
489#
490# Simple wrapper to an awk(1) script to print the number of lines read from
491# stdin. Supports newline escape-sequence `\n' for splitting a single line into
492# multiple lines.
493#
494f_number_of_lines_awk='
495BEGIN { num_lines = 0 }
496{
497	num_lines += split(" "$0, unused, /\\n/)
498}
499END { print num_lines }
500'
501f_number_of_lines()
502{
503	awk "$f_number_of_lines_awk"
504}
505
506# f_uriencode [$text]
507#
508# Encode $text for the purpose of embedding safely into a URL. Non-alphanumeric
509# characters are converted to `%XX' sequence where XX represents the hexa-
510# decimal ordinal of the non-alphanumeric character. If $text is missing, data
511# is instead read from standard input.
512#
513f_uriencode_awk='
514BEGIN {
515	output = ""
516	for (n = 0; n < 256; n++) pack[sprintf("%c", n)] = sprintf("%%%02x", n)
517}
518{
519	sline = ""
520	slen = length($0)
521	for (n = 1; n <= slen; n++) {
522		char = substr($0, n, 1)
523		if ( char !~ /^[[:alnum:]_]$/ ) char = pack[char]
524		sline = sline char
525	}
526	output = output ( output ? "%0a" : "" ) sline
527}
528END { print output }
529'
530f_uriencode()
531{
532	if [ $# -gt 0 ]; then
533		echo "$1" | awk "$f_uriencode_awk"
534	else
535		awk "$f_uriencode_awk"
536	fi
537}
538
539# f_uridecode [$text]
540#
541# Decode $text from a URI. Encoded characters are converted from their `%XX'
542# sequence into original unencoded ASCII sequences. If $text is missing, data
543# is instead read from standard input.
544#
545f_uridecode_awk='
546BEGIN { for (n = 0; n < 256; n++) chr[n] = sprintf("%c", n) }
547{
548	sline = ""
549	slen = length($0)
550	for (n = 1; n <= slen; n++)
551	{
552		seq = substr($0, n, 3)
553		if ( seq ~ /^%[[:xdigit:]][[:xdigit:]]$/ ) {
554			hex = substr(seq, 2, 2)
555			sline = sline chr[sprintf("%u", "0x"hex)]
556			n += 2
557		} else
558			sline = sline substr(seq, 1, 1)
559	}
560	print sline
561}
562'
563f_uridecode()
564{
565	if [ $# -gt 0 ]; then
566		echo "$1" | awk "$f_uridecode_awk"
567	else
568		awk "$f_uridecode_awk"
569	fi
570}
571
572############################################################ MAIN
573
574f_dprintf "%s: Successfully loaded." strings.subr
575
576fi # ! $_STRINGS_SUBR
577