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