strings.subr revision 247280
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 (INLUDING, 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 247280 2013-02-25 19:55:32Z dteske $
28
29# f_substr "$string" $start [ $length ]
30#
31# Simple wrapper to awk(1)'s `substr' function.
32#
33f_substr()
34{
35	local string="$1" start="${2:-0}" len="${3:-0}"
36	echo "$string" | awk "{ print substr(\$0, $start, $len) }"
37}
38
39# f_longest_line_length
40#
41# Simple wrapper to an awk(1) script to print the length of the longest line of
42# input (read from stdin). Supports the newline escape-sequence `\n' for
43# splitting a single line into multiple lines.
44#
45f_longest_line_length_awk='
46BEGIN { longest = 0 }
47{
48	if (split($0, lines, /\\n/) > 1)
49	{
50		for (n in lines)
51		{
52			len = length(lines[n])
53			longest = ( len > longest ? len : longest )
54		}
55	}
56	else
57	{
58		len = length($0)
59		longest = ( len > longest ? len : longest )
60	}
61}
62END { print longest }
63'
64f_longest_line_length()
65{
66	awk "$f_longest_line_length_awk"
67}
68
69# f_number_of_lines
70#
71# Simple wrapper to an awk(1) script to print the number of lines read from
72# stdin. Supports newline escape-sequence `\n' for splitting a single line into
73# multiple lines.
74#
75f_number_of_lines_awk='
76BEGIN { num_lines = 0 }
77{
78	num_lines += split(" "$0, unused, /\\n/)
79}
80END { print num_lines }
81'
82f_number_of_lines()
83{
84	awk "$f_number_of_lines_awk"
85}
86
87# f_isinteger $arg
88#
89# Returns true if argument is a positive/negative whole integer.
90#
91f_isinteger()
92{
93	local arg="$1"
94
95	# Prevent division-by-zero
96	[ "$arg" = "0" ] && return $SUCCESS
97
98	# Attempt to perform arithmetic divison (an operation which will exit
99	# with error unless arg is a valid positive/negative whole integer).
100	#
101	( : $((0/$arg)) ) > /dev/null 2>&1
102}
103
104# f_uriencode [$text]
105#
106# Encode $text for the purpose of embedding safely into a URL. Non-alphanumeric
107# characters are converted to `%XX' sequence where XX represents the hexa-
108# decimal ordinal of the non-alphanumeric character. If $text is missing, data
109# is instead read from standard input.
110#
111f_uriencode_awk='
112BEGIN {
113	output = ""
114	for (n = 0; n < 256; n++) pack[sprintf("%c", n)] = sprintf("%%%02x", n)
115}
116{
117	sline = ""
118	slen = length($0)
119	for (n = 1; n <= slen; n++) {
120		char = substr($0, n, 1)
121		if ( char !~ /^[[:alnum:]_]$/ ) char = pack[char]
122		sline = sline char
123	}
124	output = output ( output ? "%0a" : "" ) sline
125}
126END { print output }
127'
128f_uriencode()
129{
130	if [ $# -gt 0 ]; then
131		echo "$1" | awk "$f_uriencode_awk"
132	else
133		awk "$f_uriencode_awk"
134	fi
135}
136
137# f_uridecode [$text]
138#
139# Decode $text from a URI. Encoded characters are converted from their `%XX'
140# sequence into original unencoded ASCII sequences. If $text is missing, data
141# is instead read from standard input.
142#
143f_uridecode_awk='
144BEGIN { for (n = 0; n < 256; n++) chr[n] = sprintf("%c", n) }
145{
146	sline = ""
147	slen = length($0)
148	for (n = 1; n <= slen; n++)
149	{
150		seq = substr($0, n, 3)
151		if ( seq ~ /^%[[:xdigit:]][[:xdigit:]]$/ ) {
152			hex = substr(seq, 2, 2)
153			sline = sline chr[sprintf("%u", "0x"hex)]
154			n += 2
155		} else
156			sline = sline substr(seq, 1, 1)
157	}
158	print sline
159}
160'
161f_uridecode()
162{
163	if [ $# -gt 0 ]; then
164		echo "$1" | awk "$f_uridecode_awk"
165	else
166		awk "$f_uridecode_awk"
167	fi
168}
169
170f_dprintf "%s: Successfully loaded." strings.subr
171
172fi # ! $_STRINGS_SUBR
173