strings.subr (256392) | strings.subr (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: --- 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 256392 2013-10-12 19:54:12Z dteske $ | 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 --- 5 unchanged lines hidden (view full) --- 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 | 28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33 34############################################################ GLOBALS 35 --- 5 unchanged lines hidden (view full) --- 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 ] | 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 | 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 ... | 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 | 60# 61# Similar to snprintf(3), write at most $size number of bytes into $var_to_set |
62# using printf(1) syntax (`$format ...'). The value of $var_to_set is NULL 63# unless at-least one byte is stored from the output. | 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" | 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 \"\$@\" \| awk -v max=\"\$__size\" \'' | 68 shift 2 # var_to_set size 69 eval "$__var_to_set"=\$\( printf -- \"\$@\" \| \ 70 awk -v max=\"\$__size\" \'' |
70 { 71 len = length($0) 72 max -= len 73 print substr($0,0,(max > 0 ? len : max + len)) 74 if ( max < 0 ) exit 75 max-- 76 }'\' \) 77} 78 | 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 |
|
79# f_vsnprintf $var_to_set $size $format $format_args 80# 81# Similar to vsnprintf(3), write at most $size number of bytes into $var_to_set 82# using printf(1) syntax (`$format $format_args'). The value of $var_to_set is 83# NULL unless at-least one byte is stored from the output. 84# 85# Example 1: 86# --- 19 unchanged lines hidden (view full) --- 106# In all of the above examples, the call to f_vsnprintf() does not change. Only 107# the contents of $limit, $format, and $format_args changes in each example. 108# 109f_vsnprintf() 110{ 111 eval f_snprintf \"\$1\" \"\$2\" \"\$3\" $4 112} 113 | 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# --- 19 unchanged lines hidden (view full) --- 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 |
|
114# f_longest_line_length 115# 116# Simple wrapper to an awk(1) script to print the length of the longest line of 117# input (read from stdin). Supports the newline escape-sequence `\n' for 118# splitting a single line into multiple lines. 119# 120f_longest_line_length_awk=' 121BEGIN { longest = 0 } --- 326 unchanged lines hidden --- | 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 } --- 326 unchanged lines hidden --- |