xref: /freebsd/usr.sbin/bsdconfig/share/strings.subr (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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# $FreeBSD$
28#
29############################################################ INCLUDES
30
31BSDCFG_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#
39NL="
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#
51VALID_VARNAME_CHARS="0-9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"
52
53############################################################ FUNCTIONS
54
55# f_isinteger $arg
56#
57# Returns true if argument is a positive/negative whole integer.
58#
59f_isinteger()
60{
61	local arg="${1#-}"
62	[ "${arg:-x}" = "${arg%[!0-9]*}" ]
63}
64
65# f_substr [-v $var_to_set] $string $start [$length]
66#
67# Similar to awk(1)'s substr(), return length substring of string that begins
68# at start position counted from 1.
69#
70case "$BASH_VERSION" in
71*?*)
72	f_substr()
73	{
74		local __var_to_set=
75		case "$1" in
76		-v) __var_to_set="$2"; shift 2 ;;
77		-v?*) __var_to_set="${2#-v}"; shift 1 ;;
78		esac
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}}\"
84			return $?
85		fi
86		if [ "$__len" ]; then
87			eval $__var_to_set=\"\${__tmp:\$__start:\$__len}\"
88		else
89			eval $__var_to_set=\"\${__tmp:\$__start}\"
90		fi
91	}
92	;;
93*)
94	# NB: On FreeBSD, sh(1) runs this faster than bash(1) runs the above
95	f_substr()
96	{
97		local OPTIND=1 OPTARG __flag __var_to_set=
98		while getopts v: __flag; do
99			case "$__flag" in
100			v) __var_to_set="$OPTARG" ;;
101			esac
102		done
103		shift $(( $OPTIND - 1 ))
104
105		local __tmp="$1" __start="${2:-1}" __size="$3"
106		local __tbuf __tbuf_len __trim __trimq
107
108		if [ ! "$__tmp" ]; then
109			[ "$__var_to_set" ] && setvar "$__var_to_set" ""
110			return ${SUCCESS:-0}
111		fi
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" ""
115			return ${FAILURE:-1}
116		fi
117
118		__trim=$(( $__start - 1 ))
119		while [ $__trim -gt 0 ]; do
120			__tbuf="?"
121			__tbuf_len=1
122			while [ $__tbuf_len -lt $(( $__trim / $__tbuf_len )) ]
123			do
124				__tbuf="$__tbuf?"
125				__tbuf_len=$(( $__tbuf_len + 1 ))
126			done
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 ))
132			done
133		done
134
135		local __tmp_size=${#__tmp}
136		local __mask __mask_len
137		__trim=$(( $__tmp_size - ${__size:-$__tmp_size} ))
138		while [ $__trim -gt 0 ]; do
139			__tbuf="?"
140			__tbuf_len=1
141			if [ $__trim -le $__size ]; then
142				while [ $__tbuf_len -lt $((
143					$__trim / $__tbuf_len
144				)) ]; do
145					__tbuf="$__tbuf?"
146					__tbuf_len=$(( $__tbuf_len + 1 ))
147				done
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 ))
153				done
154			else
155				__mask="$__tmp"
156				while [ $__tbuf_len -lt $((
157					$__size / $__tbuf_len
158				)) ]; do
159					__tbuf="$__tbuf?"
160					__tbuf_len=$(( $__tbuf_len + 1 ))
161				done
162				__trimq=$(( $__size / $__tbuf_len ))
163				if [ $__size -ne $((
164					$__trimq * $__tbuf_len
165				)) ]; then
166					__tbuf="$__tbuf?"
167					__tbuf_len=$(( $__tbuf_len + 1 ))
168				fi
169				__mask_len=$((
170					$__tmp_size - $__tbuf_len * $__trimq
171				))
172				__trim=$((
173					$__tmp_size - $__mask_len - $__size
174				))
175				while [ $__trimq -gt 0 ]; do
176					__mask="${__mask#$__tbuf}"
177					__trimq=$(( $__trimq - 1 ))
178				done
179				__tmp="${__tmp%"$__mask"}"
180			fi
181		done
182
183		if [ "$__var_to_set" ]; then
184			setvar "$__var_to_set" "$__tmp"
185		else
186			echo "$__tmp"
187		fi
188	}
189esac
190
191# f_sprintf $var_to_set $format [$arguments ...]
192#
193# Similar to sprintf(3), write a string into $var_to_set using printf(1) syntax
194# (`$format [$arguments ...]').
195#
196case "$BASH_VERSION" in
1973.1*|4.*)
198	f_sprintf()
199	{
200		local __var_to_set="$1" __tmp
201		shift 1 # var_to_set
202		printf -v __tmp "$@"
203		eval "$__var_to_set"=\"\${__tmp%\$NL}\"
204	}
205	;;
206*)
207	# NB: On FreeBSD, sh(1) runs this faster than bash(1) runs the above
208	f_sprintf()
209	{
210		local __var_to_set="$1"
211		shift 1 # var_to_set
212		eval "$__var_to_set"=\$\( printf -- \"\$@\" \)
213	}
214esac
215
216# f_vsprintf $var_to_set $format $format_args
217#
218# Similar to vsprintf(3), write a string into $var_to_set using printf(1)
219# syntax (`$format $format_args').
220#
221f_vsprintf()
222{
223	eval f_sprintf \"\$1\" \"\$2\" $3
224}
225
226# f_snprintf $var_to_set $size $format [$arguments ...]
227#
228# Similar to snprintf(3), write at most $size number of bytes into $var_to_set
229# using printf(1) syntax (`$format [$arguments ...]').
230#
231f_snprintf()
232{
233	local __var_to_set="$1" __size="$2"
234	shift 2 # var_to_set size
235
236	local __f_snprintf_tmp
237	f_sprintf __f_snprintf_tmp "$@"
238	f_substr "$__var_to_set" "$__f_snprintf_tmp" 1 "$__size"
239}
240
241# f_vsnprintf $var_to_set $size $format $format_args
242#
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.
246#
247# Example 1:
248#
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]
252#
253# Example 2:
254#
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]
259#
260# Example 3:
261#
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]
267#
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.
270#
271f_vsnprintf()
272{
273	eval f_snprintf \"\$1\" \"\$2\" \"\$3\" $4
274}
275
276# f_replaceall $string $find $replace [$var_to_set]
277#
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
281# degradation).
282#
283# To replace newlines or a sequence containing the newline character, use $NL
284# as `\n' is not supported.
285#
286f_replaceall()
287{
288	local __left="" __right="$1"
289	local __find="$2" __replace="$3" __var_to_set="$4"
290	while :; do
291		case "$__right" in *$__find*)
292			__left="$__left${__right%%$__find*}$__replace"
293			__right="${__right#*$__find}"
294			continue
295		esac
296		break
297	done
298	__left="$__left${__right#*$__find}"
299	if [ "$__var_to_set" ]; then
300		setvar "$__var_to_set" "$__left"
301	else
302		echo "$__left"
303	fi
304}
305
306# f_str2varname $string [$var_to_set]
307#
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
312# degradation).
313#
314f_str2varname()
315{
316	local __string="$1" __var_to_set="$2"
317	f_replaceall "$__string" "[!$VALID_VARNAME_CHARS]" "_" "$__var_to_set"
318}
319
320# f_shell_escape $string [$var_to_set]
321#
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.
325#
326# For example:
327#
328# 	foo="abc'123"
329# 	f_shell_escape "$foo" bar # bar=[abc'\''123]
330# 	eval echo \'$bar\' # produces abc'123
331#
332# This is helpful when processing an argument list that has to retain its
333# escaped structure for later evaluations.
334#
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).
337#
338f_shell_escape()
339{
340	local __string="$1" __var_to_set="$2"
341	f_replaceall "$__string" "'" "'\\''" "$__var_to_set"
342}
343
344# f_shell_unescape $string [$var_to_set]
345#
346# The antithesis of f_shell_escape(), this function takes an escaped $string
347# and expands it.
348#
349# For example:
350#
351# 	foo="abc'123"
352# 	f_shell_escape "$foo" bar # bar=[abc'\''123]
353# 	f_shell_unescape "$bar" # produces abc'123
354#
355f_shell_unescape()
356{
357	local __string="$1" __var_to_set="$2"
358	f_replaceall "$__string" "'\\''" "'" "$__var_to_set"
359}
360
361# f_expand_number $string [$var_to_set]
362#
363# Unformat $string into a number, optionally to be stored in $var_to_set. This
364# function follows the SI power of two convention.
365#
366# The prefixes are:
367#
368# 	Prefix	Description	Multiplier
369# 	k	kilo		1024
370# 	M	mega		1048576
371# 	G	giga		1073741824
372# 	T	tera		1099511627776
373# 	P	peta		1125899906842624
374# 	E	exa		1152921504606846976
375#
376# NOTE: Prefixes are case-insensitive.
377#
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:
381#
382# 	Status	Reason
383# 	1	Given $string contains no digits
384# 	2	An unrecognized prefix was given
385# 	3	Result too large to calculate
386#
387f_expand_number()
388{
389	local __string="$1" __var_to_set="$2"
390	local __cp __num __bshift __maxinput
391
392	# Remove any leading non-digits
393	__string="${__string#${__string%%[0-9]*}}"
394
395	# Store the numbers (no trailing suffix)
396	__num="${__string%%[!0-9]*}"
397
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
402		else
403			echo -1
404		fi
405		return 1 # 1 = "Given $string contains no digits"
406	fi
407
408	# Remove all the leading numbers from the string to get at the prefix
409	__string="${__string#"$__num"}"
410
411	#
412	# Test for invalid prefix (and determine bitshift length)
413	#
414	case "$__string" in
415	""|[[:space:]]*) # Shortcut
416		if [ "$__var_to_set" ]; then
417			setvar "$__var_to_set" $__num
418		else
419			echo $__num
420		fi
421		return $SUCCESS ;;
422	[Kk]*) __bshift=10 ;;
423	[Mm]*) __bshift=20 ;;
424	[Gg]*) __bshift=30 ;;
425	[Tt]*) __bshift=40 ;;
426	[Pp]*) __bshift=50 ;;
427	[Ee]*) __bshift=60 ;;
428	*)
429		# Unknown prefix
430		if [ "$__var_to_set" ]; then
431			setvar "$__var_to_set" -1
432		else
433			echo -1
434		fi
435		return 2 # 2 = "An unrecognized prefix was given"
436	esac
437
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
444		else
445			echo -1
446		fi
447		return 3 # 3 = "Result too large to calculate"
448	fi
449
450	# Shift the number out and produce it
451	__num=$(( $__num << $__bshift ))
452	if [ "$__var_to_set" ]; then
453		setvar "$__var_to_set" $__num
454	else
455		echo $__num
456	fi
457}
458
459# f_longest_line_length
460#
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.
464#
465f_longest_line_length_awk='
466BEGIN { longest = 0 }
467{
468	if (split($0, lines, /\\n/) > 1)
469	{
470		for (n in lines)
471		{
472			len = length(lines[n])
473			longest = ( len > longest ? len : longest )
474		}
475	}
476	else
477	{
478		len = length($0)
479		longest = ( len > longest ? len : longest )
480	}
481}
482END { print longest }
483'
484f_longest_line_length()
485{
486	awk "$f_longest_line_length_awk"
487}
488
489# f_number_of_lines
490#
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
493# multiple lines.
494#
495f_number_of_lines_awk='
496BEGIN { num_lines = 0 }
497{
498	num_lines += split(" "$0, unused, /\\n/)
499}
500END { print num_lines }
501'
502f_number_of_lines()
503{
504	awk "$f_number_of_lines_awk"
505}
506
507# f_uriencode [$text]
508#
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.
513#
514f_uriencode_awk='
515BEGIN {
516	output = ""
517	for (n = 0; n < 256; n++) pack[sprintf("%c", n)] = sprintf("%%%02x", n)
518}
519{
520	sline = ""
521	slen = length($0)
522	for (n = 1; n <= slen; n++) {
523		char = substr($0, n, 1)
524		if ( char !~ /^[[:alnum:]_]$/ ) char = pack[char]
525		sline = sline char
526	}
527	output = output ( output ? "%0a" : "" ) sline
528}
529END { print output }
530'
531f_uriencode()
532{
533	if [ $# -gt 0 ]; then
534		echo "$1" | awk "$f_uriencode_awk"
535	else
536		awk "$f_uriencode_awk"
537	fi
538}
539
540# f_uridecode [$text]
541#
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.
545#
546f_uridecode_awk='
547BEGIN { for (n = 0; n < 256; n++) chr[n] = sprintf("%c", n) }
548{
549	sline = ""
550	slen = length($0)
551	for (n = 1; n <= slen; n++)
552	{
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)]
557			n += 2
558		} else
559			sline = sline substr(seq, 1, 1)
560	}
561	print sline
562}
563'
564f_uridecode()
565{
566	if [ $# -gt 0 ]; then
567		echo "$1" | awk "$f_uridecode_awk"
568	else
569		awk "$f_uridecode_awk"
570	fi
571}
572
573############################################################ MAIN
574
575f_dprintf "%s: Successfully loaded." strings.subr
576
577fi # ! $_STRINGS_SUBR
578