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