strings.subr (256361) | strings.subr (256392) |
---|---|
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 (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# | 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 (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 256361 2013-10-11 23:12:05Z dteske $ | 27# $FreeBSD: stable/10/usr.sbin/bsdconfig/share/strings.subr 256392 2013-10-12 19:54:12Z dteske $ |
28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33 34############################################################ GLOBALS 35 --- 300 unchanged lines hidden (view full) --- 336# M mega 1048576 337# G giga 1073741824 338# T tera 1099511627776 339# P peta 1125899906842624 340# E exa 1152921504606846976 341# 342# NOTE: Prefixes are case-insensitive. 343# | 28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33 34############################################################ GLOBALS 35 --- 300 unchanged lines hidden (view full) --- 336# M mega 1048576 337# G giga 1073741824 338# T tera 1099511627776 339# P peta 1125899906842624 340# E exa 1152921504606846976 341# 342# NOTE: Prefixes are case-insensitive. 343# |
344# Upon successful completion, the value 0 is returned (or stored to 345# $var_to_set); otherwise -1. Reasons for a -1 return include: | 344# Upon successful completion, success status is returned; otherwise the number 345# -1 is produced ($var_to_set set to -1 or if $var_to_set is NULL or missing) 346# on standard output. In the case of failure, the error status will be one of: |
346# | 347# |
347# Given $string contains no digits. 348# An unrecognized prefix was given. 349# Result too large to calculate. | 348# Status Reason 349# 1 Given $string contains no digits 350# 2 An unrecognized prefix was given 351# 3 Result too large to calculate |
350# 351f_expand_number() 352{ 353 local __string="$1" __var_to_set="$2" | 352# 353f_expand_number() 354{ 355 local __string="$1" __var_to_set="$2" |
354 local __cp __num | 356 local __cp __num __bshift __maxinput |
355 356 # Remove any leading non-digits 357 while :; do 358 __cp="$__string" 359 __string="${__cp#[!0-9]}" 360 [ "$__string" = "$__cp" ] && break 361 done 362 | 357 358 # Remove any leading non-digits 359 while :; do 360 __cp="$__string" 361 __string="${__cp#[!0-9]}" 362 [ "$__string" = "$__cp" ] && break 363 done 364 |
363 # Return `-1' if string didn't contain any digits | 365 # Produce `-1' if string didn't contain any digits |
364 if [ ! "$__string" ]; then 365 if [ "$__var_to_set" ]; then 366 setvar "$__var_to_set" -1 367 else 368 echo -1 369 fi | 366 if [ ! "$__string" ]; then 367 if [ "$__var_to_set" ]; then 368 setvar "$__var_to_set" -1 369 else 370 echo -1 371 fi |
370 return $FAILURE | 372 return 1 # 1 = "Given $string contains no digits" |
371 fi 372 373 # Store the numbers 374 __num="${__string%%[!0-9]*}" 375 376 # Shortcut 377 if [ $__num -eq 0 ]; then 378 if [ "$__var_to_set" ]; then --- 6 unchanged lines hidden (view full) --- 385 386 # Remove all the leading numbers from the string to get at the prefix 387 while :; do 388 __cp="$__string" 389 __string="${__cp#[0-9]}" 390 [ "$__string" = "$__cp" ] && break 391 done 392 | 373 fi 374 375 # Store the numbers 376 __num="${__string%%[!0-9]*}" 377 378 # Shortcut 379 if [ $__num -eq 0 ]; then 380 if [ "$__var_to_set" ]; then --- 6 unchanged lines hidden (view full) --- 387 388 # Remove all the leading numbers from the string to get at the prefix 389 while :; do 390 __cp="$__string" 391 __string="${__cp#[0-9]}" 392 [ "$__string" = "$__cp" ] && break 393 done 394 |
393 # Test for invalid prefix | 395 # 396 # Test for invalid prefix (and determine bitshift length) 397 # |
394 case "$__string" in | 398 case "$__string" in |
395 ""|[KkMmGgTtPpEe]*) : known prefix ;; | 399 ""|[[:space:]]*) # Shortcut 400 if [ "$__var_to_set" ]; then 401 setvar "$__var_to_set" $__num 402 else 403 echo $__num 404 fi 405 return $SUCCESS ;; 406 [Kk]*) __bshift=10 ;; 407 [Mm]*) __bshift=20 ;; 408 [Gg]*) __bshift=30 ;; 409 [Tt]*) __bshift=40 ;; 410 [Pp]*) __bshift=50 ;; 411 [Ee]*) __bshift=60 ;; |
396 *) 397 # Unknown prefix 398 if [ "$__var_to_set" ]; then 399 setvar "$__var_to_set" -1 400 else 401 echo -1 402 fi | 412 *) 413 # Unknown prefix 414 if [ "$__var_to_set" ]; then 415 setvar "$__var_to_set" -1 416 else 417 echo -1 418 fi |
403 return $FAILURE | 419 return 2 # 2 = "An unrecognized prefix was given" |
404 esac 405 | 420 esac 421 |
406 # Multiply the number out 407 case "$__string" in 408 [Kk]) __num=$(( $__num * 1024 )) ;; 409 [Mm]) __num=$(( $__num * 1048576 )) ;; 410 [Gg]) __num=$(( $__num * 1073741824 )) ;; 411 [Tt]) __num=$(( $__num * 1099511627776 )) ;; 412 [Pp]) __num=$(( $__num * 1125899906842624 )) ;; 413 [Ee]) __num=$(( $__num * 1152921504606846976 )) ;; 414 esac 415 if [ $__num -le 0 ]; then 416 # Arithmetic overflow | 422 # Determine if the wheels fall off 423 __maxinput=$(( 0x7fffffffffffffff >> $__bshift )) 424 if [ $__num -gt $__maxinput ]; then 425 # Input (before expanding) would exceed 64-bit signed int |
417 if [ "$__var_to_set" ]; then 418 setvar "$__var_to_set" -1 419 else 420 echo -1 421 fi | 426 if [ "$__var_to_set" ]; then 427 setvar "$__var_to_set" -1 428 else 429 echo -1 430 fi |
422 return $FAILURE | 431 return 3 # 3 = "Result too large to calculate" |
423 fi 424 | 432 fi 433 |
425 # Return the number | 434 # Shift the number out and produce it 435 __num=$(( $__num << $__bshift )) |
426 if [ "$__var_to_set" ]; then 427 setvar "$__var_to_set" $__num 428 else 429 echo $__num 430 fi 431} 432 433############################################################ MAIN 434 435f_dprintf "%s: Successfully loaded." strings.subr 436 437fi # ! $_STRINGS_SUBR | 436 if [ "$__var_to_set" ]; then 437 setvar "$__var_to_set" $__num 438 else 439 echo $__num 440 fi 441} 442 443############################################################ MAIN 444 445f_dprintf "%s: Successfully loaded." strings.subr 446 447fi # ! $_STRINGS_SUBR |