strings.subr revision 263141
1215976Sjmallettif [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1
2232812Sjmallett#
3215976Sjmallett# Copyright (c) 2006-2013 Devin Teske
4215976Sjmallett# All rights reserved.
5215976Sjmallett#
6215976Sjmallett# Redistribution and use in source and binary forms, with or without
7215976Sjmallett# modification, are permitted provided that the following conditions
8215976Sjmallett# are met:
9215976Sjmallett# 1. Redistributions of source code must retain the above copyright
10215976Sjmallett#    notice, this list of conditions and the following disclaimer.
11215976Sjmallett# 2. Redistributions in binary form must reproduce the above copyright
12215976Sjmallett#    notice, this list of conditions and the following disclaimer in the
13215976Sjmallett#    documentation and/or other materials provided with the distribution.
14215976Sjmallett#
15215976Sjmallett# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16215976Sjmallett# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17215976Sjmallett# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18232812Sjmallett# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19215976Sjmallett# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20215976Sjmallett# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21215976Sjmallett# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22215976Sjmallett# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23215976Sjmallett# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24215976Sjmallett# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25215976Sjmallett# SUCH DAMAGE.
26215976Sjmallett#
27215976Sjmallett# $FreeBSD: head/usr.sbin/bsdconfig/share/strings.subr 263141 2014-03-14 03:00:02Z dteske $
28215976Sjmallett#
29232812Sjmallett############################################################ INCLUDES
30215976Sjmallett
31215976SjmallettBSDCFG_SHARE="/usr/share/bsdconfig"
32215976Sjmallett. $BSDCFG_SHARE/common.subr || exit 1
33215976Sjmallett
34215976Sjmallett############################################################ GLOBALS
35215976Sjmallett
36215976Sjmallett#
37215976Sjmallett# A Literal newline (for use with f_replace_all(), or IFS, or whatever)
38215976Sjmallett#
39215976SjmallettNL="
40215976Sjmallett" # END-QUOTE
41215976Sjmallett
42215976Sjmallett#
43215976Sjmallett# Valid characters that can appear in an sh(1) variable name
44215976Sjmallett#
45215976Sjmallett# Please note that the character ranges A-Z and a-z should be avoided because
46215976Sjmallett# these can include accent characters (which are not valid in a variable name).
47215976Sjmallett# For example, A-Z matches any character that sorts after A but before Z,
48215976Sjmallett# including A and Z. Although ASCII order would make more sense, that is not
49215976Sjmallett# how it works.
50215976Sjmallett#
51215976SjmallettVALID_VARNAME_CHARS="0-9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"
52215976Sjmallett
53215976Sjmallett############################################################ FUNCTIONS
54215976Sjmallett
55232812Sjmallett# f_substr "$string" $start [$length]
56215976Sjmallett#
57215976Sjmallett# Simple wrapper to awk(1)'s `substr' function.
58232812Sjmallett#
59232812Sjmallettf_substr()
60215976Sjmallett{
61232812Sjmallett	local string="$1" start="${2:-0}" len="${3:-0}"
62215976Sjmallett	echo "$string" | awk "{ print substr(\$0, $start, $len) }"
63215976Sjmallett}
64215976Sjmallett
65215976Sjmallett# f_snprintf $var_to_set $size $format [$arguments ...]
66215976Sjmallett#
67215976Sjmallett# Similar to snprintf(3), write at most $size number of bytes into $var_to_set
68215976Sjmallett# using printf(1) syntax (`$format [$arguments ...]'). The value of $var_to_set
69215976Sjmallett# is NULL unless at-least one byte is stored from the output.
70215976Sjmallett#
71215976Sjmallettf_snprintf()
72215976Sjmallett{
73215976Sjmallett	local __var_to_set="$1" __size="$2"
74215976Sjmallett	shift 2 # var_to_set size
75215976Sjmallett	eval "$__var_to_set"=\$\( printf -- \"\$@\" \| \
76215976Sjmallett		awk -v max=\"\$__size\" \''
77215976Sjmallett	{
78215976Sjmallett		len = length($0)
79215976Sjmallett		max -= len
80215976Sjmallett		print substr($0,0,(max > 0 ? len : max + len))
81215976Sjmallett		if ( max < 0 ) exit
82215976Sjmallett		max--
83215976Sjmallett	}'\' \)
84215976Sjmallett}
85215976Sjmallett
86215976Sjmallett# f_sprintf $var_to_set $format [$arguments ...]
87215976Sjmallett#
88215976Sjmallett# Similar to sprintf(3), write a string into $var_to_set using printf(1) syntax
89215976Sjmallett# (`$format [$arguments ...]').
90215976Sjmallett#
91215976Sjmallettf_sprintf()
92215976Sjmallett{
93215976Sjmallett	local __var_to_set="$1"
94215976Sjmallett	shift 1 # var_to_set
95215976Sjmallett	eval "$__var_to_set"=\$\( printf -- \"\$@\" \)
96215976Sjmallett}
97215976Sjmallett
98215976Sjmallett# f_vsnprintf $var_to_set $size $format $format_args
99215976Sjmallett#
100215976Sjmallett# Similar to vsnprintf(3), write at most $size number of bytes into $var_to_set
101232812Sjmallett# using printf(1) syntax (`$format $format_args'). The value of $var_to_set is
102215976Sjmallett# NULL unless at-least one byte is stored from the output.
103215976Sjmallett#
104215976Sjmallett# Example 1:
105215976Sjmallett#
106215976Sjmallett# 	limit=7 format="%s"
107215976Sjmallett# 	format_args="'abc   123'" # 3-spaces between abc and 123
108215976Sjmallett# 	f_vsnprintf foo $limit "$format" "$format_args" # foo=[abc   1]
109215976Sjmallett#
110215976Sjmallett# Example 2:
111215976Sjmallett#
112215976Sjmallett# 	limit=12 format="%s %s"
113215976Sjmallett# 	format_args="   'doghouse'      'foxhound'   "
114215976Sjmallett# 		# even more spaces added to illustrate escape-method
115215976Sjmallett# 	f_vsnprintf foo $limit "$format" "$format_args" # foo=[doghouse fox]
116215976Sjmallett#
117215976Sjmallett# Example 3:
118215976Sjmallett#
119215976Sjmallett# 	limit=13 format="%s %s"
120215976Sjmallett# 	f_shell_escape arg1 'aaa"aaa' # arg1=[aaa"aaa] (no change)
121215976Sjmallett# 	f_shell_escape arg2 "aaa'aaa" # arg2=[aaa'\''aaa] (escaped s-quote)
122215976Sjmallett# 	format_args="'$arg1' '$arg2'" # use single-quotes to surround args
123215976Sjmallett# 	f_vsnprintf foo $limit "$format" "$format_args" # foo=[aaa"aaa aaa'a]
124215976Sjmallett#
125215976Sjmallett# In all of the above examples, the call to f_vsnprintf() does not change. Only
126215976Sjmallett# the contents of $limit, $format, and $format_args changes in each example.
127215976Sjmallett#
128215976Sjmallettf_vsnprintf()
129215976Sjmallett{
130215976Sjmallett	eval f_snprintf \"\$1\" \"\$2\" \"\$3\" $4
131215976Sjmallett}
132215976Sjmallett
133215976Sjmallett# f_vsprintf $var_to_set $format $format_args
134215976Sjmallett#
135215976Sjmallett# Similar to vsprintf(3), write a string into $var_to_set using printf(1)
136215976Sjmallett# syntax (`$format $format_args').
137215976Sjmallett#
138215976Sjmallettf_vsprintf()
139215976Sjmallett{
140215976Sjmallett	eval f_sprintf \"\$1\" \"\$2\" $3
141215976Sjmallett}
142215976Sjmallett
143215976Sjmallett# f_longest_line_length
144215976Sjmallett#
145215976Sjmallett# Simple wrapper to an awk(1) script to print the length of the longest line of
146215976Sjmallett# input (read from stdin). Supports the newline escape-sequence `\n' for
147215976Sjmallett# splitting a single line into multiple lines.
148215976Sjmallett#
149232812Sjmallettf_longest_line_length_awk='
150232812SjmallettBEGIN { longest = 0 }
151232812Sjmallett{
152232812Sjmallett	if (split($0, lines, /\\n/) > 1)
153232812Sjmallett	{
154232812Sjmallett		for (n in lines)
155232812Sjmallett		{
156232812Sjmallett			len = length(lines[n])
157215976Sjmallett			longest = ( len > longest ? len : longest )
158215976Sjmallett		}
159215976Sjmallett	}
160215976Sjmallett	else
161215976Sjmallett	{
162215976Sjmallett		len = length($0)
163215976Sjmallett		longest = ( len > longest ? len : longest )
164215976Sjmallett	}
165215976Sjmallett}
166215976SjmallettEND { print longest }
167215976Sjmallett'
168215976Sjmallettf_longest_line_length()
169215976Sjmallett{
170215976Sjmallett	awk "$f_longest_line_length_awk"
171215976Sjmallett}
172215976Sjmallett
173215976Sjmallett# f_number_of_lines
174215976Sjmallett#
175215976Sjmallett# Simple wrapper to an awk(1) script to print the number of lines read from
176215976Sjmallett# stdin. Supports newline escape-sequence `\n' for splitting a single line into
177215976Sjmallett# multiple lines.
178215976Sjmallett#
179215976Sjmallettf_number_of_lines_awk='
180215976SjmallettBEGIN { num_lines = 0 }
181215976Sjmallett{
182215976Sjmallett	num_lines += split(" "$0, unused, /\\n/)
183215976Sjmallett}
184215976SjmallettEND { print num_lines }
185215976Sjmallett'
186215976Sjmallettf_number_of_lines()
187215976Sjmallett{
188215976Sjmallett	awk "$f_number_of_lines_awk"
189215976Sjmallett}
190215976Sjmallett
191215976Sjmallett# f_isinteger $arg
192215976Sjmallett#
193215976Sjmallett# Returns true if argument is a positive/negative whole integer.
194215976Sjmallett#
195215976Sjmallettf_isinteger()
196215976Sjmallett{
197215976Sjmallett	local arg="${1#-}"
198215976Sjmallett	[ "${arg:-x}" = "${arg%[!0-9]*}" ]
199215976Sjmallett}
200215976Sjmallett
201215976Sjmallett# f_uriencode [$text]
202215976Sjmallett#
203215976Sjmallett# Encode $text for the purpose of embedding safely into a URL. Non-alphanumeric
204215976Sjmallett# characters are converted to `%XX' sequence where XX represents the hexa-
205215976Sjmallett# decimal ordinal of the non-alphanumeric character. If $text is missing, data
206215976Sjmallett# is instead read from standard input.
207215976Sjmallett#
208215976Sjmallettf_uriencode_awk='
209215976SjmallettBEGIN {
210215976Sjmallett	output = ""
211215976Sjmallett	for (n = 0; n < 256; n++) pack[sprintf("%c", n)] = sprintf("%%%02x", n)
212215976Sjmallett}
213215976Sjmallett{
214215976Sjmallett	sline = ""
215215976Sjmallett	slen = length($0)
216215976Sjmallett	for (n = 1; n <= slen; n++) {
217215976Sjmallett		char = substr($0, n, 1)
218215976Sjmallett		if ( char !~ /^[[:alnum:]_]$/ ) char = pack[char]
219215976Sjmallett		sline = sline char
220215976Sjmallett	}
221215976Sjmallett	output = output ( output ? "%0a" : "" ) sline
222215976Sjmallett}
223215976SjmallettEND { print output }
224215976Sjmallett'
225215976Sjmallettf_uriencode()
226215976Sjmallett{
227215976Sjmallett	if [ $# -gt 0 ]; then
228215976Sjmallett		echo "$1" | awk "$f_uriencode_awk"
229215976Sjmallett	else
230215976Sjmallett		awk "$f_uriencode_awk"
231215976Sjmallett	fi
232215976Sjmallett}
233215976Sjmallett
234215976Sjmallett# f_uridecode [$text]
235215976Sjmallett#
236215976Sjmallett# Decode $text from a URI. Encoded characters are converted from their `%XX'
237215976Sjmallett# sequence into original unencoded ASCII sequences. If $text is missing, data
238215976Sjmallett# is instead read from standard input.
239215976Sjmallett#
240215976Sjmallettf_uridecode_awk='
241215976SjmallettBEGIN { for (n = 0; n < 256; n++) chr[n] = sprintf("%c", n) }
242215976Sjmallett{
243215976Sjmallett	sline = ""
244215976Sjmallett	slen = length($0)
245215976Sjmallett	for (n = 1; n <= slen; n++)
246215976Sjmallett	{
247215976Sjmallett		seq = substr($0, n, 3)
248215976Sjmallett		if ( seq ~ /^%[[:xdigit:]][[:xdigit:]]$/ ) {
249232812Sjmallett			hex = substr(seq, 2, 2)
250232812Sjmallett			sline = sline chr[sprintf("%u", "0x"hex)]
251215976Sjmallett			n += 2
252215976Sjmallett		} else
253215976Sjmallett			sline = sline substr(seq, 1, 1)
254215976Sjmallett	}
255215976Sjmallett	print sline
256215976Sjmallett}
257215976Sjmallett'
258215976Sjmallettf_uridecode()
259215976Sjmallett{
260215976Sjmallett	if [ $# -gt 0 ]; then
261215976Sjmallett		echo "$1" | awk "$f_uridecode_awk"
262215976Sjmallett	else
263215976Sjmallett		awk "$f_uridecode_awk"
264215976Sjmallett	fi
265215976Sjmallett}
266215976Sjmallett
267215976Sjmallett# f_replaceall $string $find $replace [$var_to_set]
268215976Sjmallett#
269215976Sjmallett# Replace all occurrences of $find in $string with $replace. If $var_to_set is
270215976Sjmallett# either missing or NULL, the variable name is produced on standard out for
271215976Sjmallett# capturing in a sub-shell (which is less recommended due to performance
272215976Sjmallett# degradation).
273215976Sjmallett#
274215976Sjmallett# To replace newlines or a sequence containing the newline character, use $NL
275215976Sjmallett# as `\n' is not supported.
276215976Sjmallett#
277215976Sjmallettf_replaceall()
278215976Sjmallett{
279	local __left="" __right="$1"
280	local __find="$2" __replace="$3" __var_to_set="$4"
281	while :; do
282		case "$__right" in *$__find*)
283			__left="$__left${__right%%$__find*}$__replace"
284			__right="${__right#*$__find}"
285			continue
286		esac
287		break
288	done
289	__left="$__left${__right#*$__find}"
290	if [ "$__var_to_set" ]; then
291		setvar "$__var_to_set" "$__left"
292	else
293		echo "$__left"
294	fi
295}
296
297# f_str2varname $string [$var_to_set]
298#
299# Convert a string into a suitable value to be used as a variable name
300# by converting unsuitable characters into the underscrore [_]. If $var_to_set
301# is either missing or NULL, the variable name is produced on standard out for
302# capturing in a sub-shell (which is less recommended due to performance
303# degradation).
304#
305f_str2varname()
306{
307	local __string="$1" __var_to_set="$2"
308	f_replaceall "$__string" "[!$VALID_VARNAME_CHARS]" "_" "$__var_to_set"
309}
310
311# f_shell_escape $string [$var_to_set]
312#
313# Escape $string for shell eval statement(s) by replacing all single-quotes
314# with a special sequence that creates a compound string when interpolated
315# by eval with surrounding single-quotes.
316#
317# For example:
318#
319# 	foo="abc'123"
320# 	f_shell_escape "$foo" bar # bar=[abc'\''123]
321# 	eval echo \'$bar\' # produces abc'123
322#
323# This is helpful when processing an argument list that has to retain its
324# escaped structure for later evaluations.
325#
326# WARNING: Surrounding single-quotes are not added; this is the responsibility
327# of the code passing the escaped values to eval (which also aids readability).
328#
329f_shell_escape()
330{
331	local __string="$1" __var_to_set="$2"
332	f_replaceall "$__string" "'" "'\\''" "$__var_to_set"
333}
334
335# f_shell_unescape $string [$var_to_set]
336#
337# The antithesis of f_shell_escape(), this function takes an escaped $string
338# and expands it.
339#
340# For example:
341#
342# 	foo="abc'123"
343# 	f_shell_escape "$foo" bar # bar=[abc'\''123]
344# 	f_shell_unescape "$bar" # produces abc'123
345#
346f_shell_unescape()
347{
348	local __string="$1" __var_to_set="$2"
349	f_replaceall "$__string" "'\\''" "'" "$__var_to_set"
350}
351
352# f_expand_number $string [$var_to_set]
353#
354# Unformat $string into a number, optionally to be stored in $var_to_set. This
355# function follows the SI power of two convention.
356#
357# The prefixes are:
358#
359# 	Prefix	Description	Multiplier
360# 	k	kilo		1024
361# 	M	mega		1048576
362# 	G	giga		1073741824
363# 	T	tera		1099511627776
364# 	P	peta		1125899906842624
365# 	E	exa		1152921504606846976
366#
367# NOTE: Prefixes are case-insensitive.
368#
369# Upon successful completion, success status is returned; otherwise the number
370# -1 is produced ($var_to_set set to -1 or if $var_to_set is NULL or missing)
371# on standard output. In the case of failure, the error status will be one of:
372#
373# 	Status	Reason
374# 	1	Given $string contains no digits
375# 	2	An unrecognized prefix was given
376# 	3	Result too large to calculate
377#
378f_expand_number()
379{
380	local __string="$1" __var_to_set="$2"
381	local __cp __num __bshift __maxinput
382
383	# Remove any leading non-digits
384	__string="${__string#${__string%%[0-9]*}}"
385
386	# Store the numbers (no trailing suffix)
387	__num="${__string%%[!0-9]*}"
388
389	# Produce `-1' if string didn't contain any digits
390	if [ ! "$__num" ]; then
391		if [ "$__var_to_set" ]; then
392			setvar "$__var_to_set" -1
393		else
394			echo -1
395		fi
396		return 1 # 1 = "Given $string contains no digits"
397	fi
398
399	# Remove all the leading numbers from the string to get at the prefix
400	__string="${__string#"$__num"}"
401
402	#
403	# Test for invalid prefix (and determine bitshift length)
404	#
405	case "$__string" in
406	""|[[:space:]]*) # Shortcut
407		if [ "$__var_to_set" ]; then
408			setvar "$__var_to_set" $__num
409		else
410			echo $__num
411		fi
412		return $SUCCESS ;;
413	[Kk]*) __bshift=10 ;;
414	[Mm]*) __bshift=20 ;;
415	[Gg]*) __bshift=30 ;;
416	[Tt]*) __bshift=40 ;;
417	[Pp]*) __bshift=50 ;;
418	[Ee]*) __bshift=60 ;;
419	*)
420		# Unknown prefix
421		if [ "$__var_to_set" ]; then
422			setvar "$__var_to_set" -1
423		else
424			echo -1
425		fi
426		return 2 # 2 = "An unrecognized prefix was given"
427	esac
428
429	# Determine if the wheels fall off
430	__maxinput=$(( 0x7fffffffffffffff >> $__bshift ))
431	if [ $__num -gt $__maxinput ]; then
432		# Input (before expanding) would exceed 64-bit signed int
433		if [ "$__var_to_set" ]; then
434			setvar "$__var_to_set" -1
435		else
436			echo -1
437		fi
438		return 3 # 3 = "Result too large to calculate"
439	fi
440
441	# Shift the number out and produce it
442	__num=$(( $__num << $__bshift ))
443	if [ "$__var_to_set" ]; then
444		setvar "$__var_to_set" $__num
445	else
446		echo $__num
447	fi
448}
449
450############################################################ MAIN
451
452f_dprintf "%s: Successfully loaded." strings.subr
453
454fi # ! $_STRINGS_SUBR
455