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