strings.subr (247280) | strings.subr (249751) |
---|---|
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: --- 10 unchanged lines hidden (view full) --- 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# | 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: --- 10 unchanged lines hidden (view full) --- 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 $ | 27# $FreeBSD: head/usr.sbin/bsdconfig/share/strings.subr 249751 2013-04-22 05:52:06Z dteske $ 28# 29############################################################ GLOBALS |
28 | 30 |
31# 32# Valid characters that can appear in an sh(1) variable name 33# 34# Please note that the character ranges A-Z and a-z should be avoided because 35# these can include accent characters (which are not valid in a variable name). 36# For example, A-Z matches any character that sorts after A but before Z, 37# including A and Z. Although ASCII order would make more sense, that is not 38# how it works. 39# 40VALID_VARNAME_CHARS="0-9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" 41 42############################################################ FUNCTIONS 43 |
|
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) }" --- 125 unchanged lines hidden (view full) --- 162{ 163 if [ $# -gt 0 ]; then 164 echo "$1" | awk "$f_uridecode_awk" 165 else 166 awk "$f_uridecode_awk" 167 fi 168} 169 | 44# f_substr "$string" $start [ $length ] 45# 46# Simple wrapper to awk(1)'s `substr' function. 47# 48f_substr() 49{ 50 local string="$1" start="${2:-0}" len="${3:-0}" 51 echo "$string" | awk "{ print substr(\$0, $start, $len) }" --- 125 unchanged lines hidden (view full) --- 177{ 178 if [ $# -gt 0 ]; then 179 echo "$1" | awk "$f_uridecode_awk" 180 else 181 awk "$f_uridecode_awk" 182 fi 183} 184 |
185# f_replaceall $string $find $replace [$var_to_set] 186# 187# Replace all occurrences of $find in $sting with $replace. If $var_to_set is 188# either missing or NULL, the variable name is produced on standard out for 189# capturing in a sub-shell (which is less recommended due to performance 190# degradation). 191# 192f_replaceall() 193{ 194 local __left="" __right="$1" 195 local __find="$2" __replace="$3" __var_to_set="$4" 196 while :; do 197 case "$__right" in *$__find*) 198 __left="$__left${__right%%$__find*}$__replace" 199 __right="${__right#*$__find}" 200 continue 201 esac 202 break 203 done 204 __left="$__left${__right#*$__find}" 205 if [ "$__var_to_set" ]; then 206 setvar "$__var_to_set" "$__left" 207 else 208 echo "$__left" 209 fi 210} 211 212# f_str2varname $string [$var_to_set] 213# 214# Convert a string into a suitable value to be used as a variable name 215# by converting unsuitable characters into the underscrore [_]. If $var_to_set 216# is either missing or NULL, the variable name is produced on standard out for 217# capturing in a sub-shell (which is less recommended due to performance 218# degradation). 219# 220f_str2varname() 221{ 222 local __string="$1" __var_to_set="$2" 223 f_replaceall "$__string" "[!$VALID_VARNAME_CHARS]" "_" "$__var_to_set" 224} 225 226# f_shell_escape $string [$var_to_set] 227# 228# Escape $string for shell eval statement(s) by replacing all single-quotes 229# with a special sequence that creates a compound string when interpolated 230# by eval with surrounding single-quotes. 231# 232# For example: 233# 234# foo="abc'123" 235# f_shell_escape "$foo" bar # bar=[abc'\''123] 236# eval echo \'$foo\' # produces abc'123 237# 238# This is helpful when processing an argument list that has to retain its 239# escaped structure for later evaluations. 240# 241# WARNING: Surrounding single-quotes are not added; this is the responsibility 242# of the code passing the escaped values to eval (which also aids readability). 243# 244f_shell_escape() 245{ 246 local __string="$1" __var_to_set="$2" 247 f_replaceall "$__string" "'" "'\\''" "$__var_to_set" 248} 249 250# f_shell_unescape $string [$var_to_set] 251# 252# The antithesis of f_shell_escape(), this function takes an escaped $string 253# and expands it. 254# 255# For example: 256# 257# foo="abc'123" 258# f_shell_escape "$foo" bar # bar=[abc'\''123] 259# f_shell_unescape "$bar" # produces abc'123 260# 261f_shell_unescape() 262{ 263 local __string="$1" __var_to_set="$2" 264 f_replaceall "$__string" "'\\''" "'" "$__var_to_set" 265} 266 267############################################################ MAIN 268 |
|
170f_dprintf "%s: Successfully loaded." strings.subr 171 172fi # ! $_STRINGS_SUBR | 269f_dprintf "%s: Successfully loaded." strings.subr 270 271fi # ! $_STRINGS_SUBR |