diff options
author | dteske <dteske@FreeBSD.org> | 2014-01-15 07:49:17 +0000 |
---|---|---|
committer | dteske <dteske@FreeBSD.org> | 2014-01-15 07:49:17 +0000 |
commit | a35804263ce5ac47cb5d4819f6b5c9933997c9b0 (patch) | |
tree | 5307c06d0753e37d7f6c7040f0ae9896e2701ecd /usr.sbin/bsdconfig/share | |
parent | 44abef95373cf49911f4c9bf27ad329cccbd3a05 (diff) | |
download | FreeBSD-src-a35804263ce5ac47cb5d4819f6b5c9933997c9b0.zip FreeBSD-src-a35804263ce5ac47cb5d4819f6b5c9933997c9b0.tar.gz |
MFC r259054:
Performance and debugging enhancements:
+ Remove UNAME_P=$(...) from startup/misc -- already supplied by common.subr
+ Use f_getvar instead of $(eval echo \$$var) -- f_getvar is sub-shell free
+ Add `-e' and `-k var' options to f_eval_catch -- increasing use-cases
+ Use f_eval_catch to display errors on failure -- reducing duplicated code
+ Use f_eval_catch when we need output from a command -- improving debugging
+ Optimize f_isinter of strings.subr for performance -- now sub-shell free
+ Improve error checking on pidfiles -- using f_eval_catch and f_isinteger
+ Use $var_to_set arg of f_ifconfig_{inet,netmask} -- eliminate sub-shells
+ Use f_sprintf instead of $(printf ...) -- consolidate sub-shells
+ Use $var_to_set arg of f_route_get_default -- eliminate sub-shells
+ Add f_count to replace $(set -- ...;echo $#) -- eliminate sub-shells
+ Add f_count_ifs to replace $(IFS=x;set -- ...;echo $#) -- no sub-shells
+ Replace var="$var${var:+ }..." in loops with var="$var ..." with a follow-
up var="${var# }" to trim leading whitespace -- optimize loops
+ Use $var_to_set arg of f_resolv_conf_nameservers -- eliminate sub-shells
+ Comments for the f_eval_catch function
+ Remove a duplicate `local ... desc ...' in f_device_get_all of device.subr
+ Use $var_to_set arg of f_device_capacity -- eliminate sub-shells
+ Whitespace fixes in f_dialog_init of dialog.subr
+ Optimize f_inet_atoi of media/tcpip.subr for performance -- sub-shell free
+ In several cases, send stderr to /dev/null -- clean up runtime execution
+ Change f_err of common.subr to go to program stderr not terminal stderr,
allowing redirection of output from functions that use f_err
+ Disable debugging when using f_getvar to get variable argument to
f_startup_rcconf_map_expand of startup/rcconf.subr
+ Use f_replace_all instead of $(echo ... | tr | sed) -- performance
+ Add a $var_to_set option to f_index_{file,menusel_{command,keyword}} of
common.subr -- centralize sub-shells
Diffstat (limited to 'usr.sbin/bsdconfig/share')
19 files changed, 385 insertions, 227 deletions
diff --git a/usr.sbin/bsdconfig/share/common.subr b/usr.sbin/bsdconfig/share/common.subr index db28a54..94b5cfd 100644 --- a/usr.sbin/bsdconfig/share/common.subr +++ b/usr.sbin/bsdconfig/share/common.subr @@ -58,10 +58,10 @@ FAILURE=1 # # Operating environment details # -export UNAME_S="$(uname -s)" # Operating System (i.e. FreeBSD) -export UNAME_P="$(uname -p)" # Processor Architecture (i.e. i386) -export UNAME_M="$(uname -m)" # Machine platform (i.e. i386) -export UNAME_R="$(uname -r)" # Release Level (i.e. X.Y-RELEASE) +export UNAME_S="$( uname -s )" # Operating System (i.e. FreeBSD) +export UNAME_P="$( uname -p )" # Processor Architecture (i.e. i386) +export UNAME_M="$( uname -m )" # Machine platform (i.e. i386) +export UNAME_R="$( uname -r )" # Release Level (i.e. X.Y-RELEASE) if [ ! "${PKG_ABI+set}" ]; then export PKG_ABI="$( ASSUME_ALWAYS_YES=1 pkg -vv 2> /dev/null | @@ -204,7 +204,7 @@ f_debug_init() # f_err() { - printf "$@" >&${TERMINAL_STDERR_PASSTHRU:-2} + printf "$@" >&2 } # f_quietly $command [$arguments ...] @@ -565,7 +565,7 @@ f_usage() exit $FAILURE } -# f_index_file $keyword +# f_index_file $keyword [$var_to_set] # # Process all INDEX files known to bsdconfig and return the path to first file # containing a menu_selection line with a keyword portion matching $keyword. @@ -575,6 +575,9 @@ f_usage() # # If no file is found, error status is returned along with the NULL string. # +# If $var_to_set is NULL or missing, output is printed to stdout (which is less +# recommended due to performance degradation; in a loop for example). +# # This function is a two-parter. Below is the awk(1) portion of the function, # afterward is the sh(1) function which utilizes the below awk script. # @@ -591,33 +594,55 @@ END { exit ! found } ' f_index_file() { - local keyword="$1" - local lang="${LANG:-$LC_ALL}" + local __keyword="$1" __var_to_set="$2" + local __lang="${LANG:-$LC_ALL}" + local __indexes="$BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX" - f_dprintf "f_index_file: keyword=[%s] lang=[%s]" "$keyword" "$lang" + f_dprintf "f_index_file: keyword=[%s] lang=[%s]" "$__keyword" "$__lang" - if [ "$lang" ]; then - awk -v keyword="$keyword" "$f_index_file_awk" \ - $BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX.$lang && - return $SUCCESS + if [ "$__lang" ]; then + if [ "$__var_to_set" ]; then + eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ + "$f_index_file_awk" $__indexes.$__lang + )"' && return $SUCCESS + else + awk -v keyword="$__keyword" "$f_index_file_awk" \ + $__indexes.$__lang && return $SUCCESS + fi # No match, fall-thru to non-i18n sources fi - awk -v keyword="$keyword" "$f_index_file_awk" \ - $BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX && return $SUCCESS + if [ "$__var_to_set" ]; then + eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ + "$f_index_file_awk" $__indexes )"' && return $SUCCESS + else + awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes && + return $SUCCESS + fi # No match? Fall-thru to `local' libexec sources (add-on modules) [ "$BSDCFG_LOCAL_LIBE" ] || return $FAILURE - if [ "$lang" ]; then - awk -v keyword="$keyword" "$f_index_file_awk" \ - $BSDCFG_LOCAL_LIBE/*/INDEX.$lang && return $SUCCESS + __indexes="$BSDCFG_LOCAL_LIBE/*/INDEX" + if [ "$__lang" ]; then + if [ "$__var_to_set" ]; then + eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ + "$f_index_file_awk" $__indexes.$__lang + )"' && return $SUCCESS + else + awk -v keyword="$__keyword" "$f_index_file_awk" \ + $__indexes.$__lang && return $SUCCESS + fi # No match, fall-thru to non-i18n sources fi - awk -v keyword="$keyword" "$f_index_file_awk" \ - $BSDCFG_LOCAL_LIBE/*/INDEX + if [ "$__var_to_set" ]; then + eval "$__var_to_set"='$( awk -v keyword="$__keyword" \ + "$f_index_file_awk" $__indexes )"' + else + awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes + fi } -# f_index_menusel_keyword $indexfile $pgm +# f_index_menusel_keyword $indexfile $pgm [$var_to_set] # # Process $indexfile and return only the keyword portion of the menu_selection # line with a command portion matching $pgm. @@ -634,6 +659,9 @@ f_index_file() # # If $indexfile does not exist, error status is returned with NULL. # +# If $var_to_set is NULL or missing, output is printed to stdout (which is less +# recommended due to performance degradation; in a loop for example). +# # This function is a two-parter. Below is the awk(1) portion of the function, # afterward is the sh(1) function which utilizes the below awk script. # @@ -664,24 +692,23 @@ END { exit ! found } ' f_index_menusel_keyword() { - local indexfile="$1" pgm="$2" - local lang="${LANG:-$LC_ALL}" + local __indexfile="$1" __pgm="$2" __var_to_set="$3" + local __lang="${LANG:-$LC_ALL}" __file="$__indexfile" + [ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang" f_dprintf "f_index_menusel_keyword: index=[%s] pgm=[%s] lang=[%s]" \ - "$indexfile" "$pgm" "$lang" - - if [ -f "$indexfile.$lang" ]; then - awk -v pgm="$pgm" \ - "$f_index_menusel_keyword_awk" \ - "$indexfile.$lang" - elif [ -f "$indexfile" ]; then - awk -v pgm="$pgm" \ - "$f_index_menusel_keyword_awk" \ - "$indexfile" + "$__file" "$__pgm" "$__lang" + + if [ "$__var_to_set" ]; then + setvar "$__var_to_set" "$( awk \ + -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file" + )" + else + awk -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file" fi } -# f_index_menusel_command $indexfile $keyword +# f_index_menusel_command $indexfile $keyword [$var_to_set] # # Process $indexfile and return only the command portion of the menu_selection # line with a keyword portion matching $keyword. @@ -697,6 +724,9 @@ f_index_menusel_keyword() # # If $indexfile doesn't exist, error status is returned with NULL. # +# If $var_to_set is NULL or missing, output is printed to stdout (which is less +# recommended due to performance degradation; in a loop for example). +# # This function is a two-parter. Below is the awk(1) portion of the function, # afterward is the sh(1) function which utilizes the below awk script. # @@ -727,37 +757,34 @@ END { exit ! found } ' f_index_menusel_command() { - local indexfile="$1" keyword="$2" command - local lang="${LANG:-$LC_ALL}" + local __indexfile="$1" __keyword="$2" __var_to_set="$3" __command + local __lang="${LANG:-$LC_ALL}" __file="$__indexfile" + [ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang" f_dprintf "f_index_menusel_command: index=[%s] key=[%s] lang=[%s]" \ - "$indexfile" "$keyword" "$lang" - - if [ -f "$indexfile.$lang" ]; then - command=$( awk -v key="$keyword" \ - "$f_index_menusel_command_awk" \ - "$indexfile.$lang" ) || return $FAILURE - elif [ -f "$indexfile" ]; then - command=$( awk -v key="$keyword" \ - "$f_index_menusel_command_awk" \ - "$indexfile" ) || return $FAILURE - else - return $FAILURE - fi + "$__file" "$__keyword" "$__lang" + + [ -f "$__file" ] || return $FAILURE + __command=$( awk -v key="$__keyword" \ + "$f_index_menusel_command_awk" "$__file" ) || return $FAILURE # # If the command pathname is not fully qualified fix-up/force to be # relative to the $indexfile directory. # - case "$command" in + case "$__command" in /*) : already fully qualified ;; *) - local indexdir="${indexfile%/*}" - [ "$indexdir" != "$indexfile" ] || indexdir="." - command="$indexdir/$command" + local __indexdir="${__indexfile%/*}" + [ "$__indexdir" != "$__indexfile" ] || __indexdir="." + __command="$__indexdir/$__command" esac - echo "$command" + if [ "$__var_to_set" ]; then + setvar "$__var_to_set" "$__command" + else + echo "$__command" + fi } # f_running_as_init @@ -784,47 +811,54 @@ f_mounted() mount | grep -Eq " on $dir \([^)]+\)$" } -# f_eval_catch [-d] $funcname $utility $format [$arguments ...] +# f_eval_catch [-de] [-k $var_to_set] $funcname $utility \ +# $format [$arguments ...] # # Silently evaluate a command in a sub-shell and test for error. If debugging # is enabled a copy of the command and its output is sent to debug (either # stdout or file depending on environment). If an error occurs, output of the # command is displayed in a dialog(1) msgbox using the [above] f_show_err() -# function (unless optional `-d' flag is the first argument, then no dialog). +# function (unless optional `-d' flag is given, then no dialog). +# # The $funcname argument is sent to debugging while the $utility argument is -# used in the title of the dialog box. The command that is sent to debugging -# along with $funcname is the product of the printf(1) syntax produced by -# $format with optional $arguments. +# used in the title of the dialog box. The command that is executed as well as +# sent to debugging with $funcname is the product of the printf(1) syntax +# produced by $format with optional $arguments. +# +# The following options are supported: +# +# -d Do not use dialog(1). +# -e Produce error text from failed command on stderr. +# -k var Save output from the command in var. # # Example 1: # # debug=1 -# f_eval_catch myfunc cat 'contents=$( cat "%s" )' /some/file -# # Error displayed ``cat: /some/file: No such file or directory'' +# f_eval_catch myfunc echo 'echo "%s"' "Hello, World!" # # Produces the following debug output: # -# DEBUG: myfunc: cat "/some/file" -# DEBUG: myfunc: retval=1 <output below> -# cat: /some/file: No such file or directory +# DEBUG: myfunc: echo "Hello, World!" +# DEBUG: myfunc: retval=0 <output below> +# Hello, World! # # Example 2: # # debug=1 -# f_eval_catch myfunc echo 'echo "%s"' "Hello, World!" -# # No error displayed +# f_eval_catch -k contents myfunc cat 'cat "%s"' /some/file +# # dialog(1) Error ``cat: /some/file: No such file or directory'' +# # contents=[cat: /some/file: No such file or directory] # # Produces the following debug output: # -# DEBUG: myfunc: echo "Hello, World!" -# DEBUG: myfunc: retval=0 <output below> -# Hello, World! +# DEBUG: myfunc: cat "/some/file" +# DEBUG: myfunc: retval=1 <output below> +# cat: /some/file: No such file or directory # # Example 3: # # debug=1 # echo 123 | f_eval_catch myfunc rev rev -# # No error displayed # # Produces the following debug output: # @@ -836,34 +870,107 @@ f_mounted() # # debug=1 # f_eval_catch myfunc true true -# # No error displayed # # Produces the following debug output: # # DEBUG: myfunc: true # DEBUG: myfunc: retval=0 <no output> # +# Example 5: +# +# f_eval_catch -de myfunc ls 'ls "%s"' /some/dir +# # Output on stderr ``ls: /some/dir: No such file or directory'' +# +# Example 6: +# +# f_eval_catch -dek contents myfunc ls 'ls "%s"' /etc +# # Output from `ls' sent to stderr and also saved in $contents +# f_eval_catch() { - local no_dialog= - [ "$1" = "-d" ] && no_dialog=1 && shift 1 - local funcname="$1" utility="$2"; shift 2 - local cmd output retval - cmd=$( printf -- "$@" ) - f_dprintf "%s: %s" "$funcname" "$cmd" # Log command *before* eval - output=$( exec 2>&1; eval "$cmd" ) - retval=$? - if [ "$output" ]; then - f_dprintf "%s: retval=%i <output below>\n%s" "$funcname" \ - $retval "$output" + local __no_dialog= __show_err= __var_to_set= + + # + # Process local function arguments + # + local OPTIND __flag + while getopts "dek:" __flag > /dev/null; do + case "$__flag" in + d) __no_dialog=1 ;; + e) __show_err=1 ;; + k) __var_to_set="$OPTARG" ;; + esac + done + shift $(( $OPTIND - 1 )) + + local __funcname="$1" __utility="$2"; shift 2 + local __cmd __output __retval + + __cmd=$( printf -- "$@" ) + f_dprintf "%s: %s" "$__funcname" "$__cmd" # Log command *before* eval + __output=$( exec 2>&1; eval "$__cmd" ) + __retval=$? + if [ "$__output" ]; then + [ "$__show_err" ] && echo "$__output" >&2 + f_dprintf "%s: retval=%i <output below>\n%s" "$__funcname" \ + $__retval "$__output" else - f_dprintf "%s: retval=%i <no output>" "$funcname" $retval + f_dprintf "%s: retval=%i <no output>" "$__funcname" $__retval fi - ! [ "$no_dialog" -o "$nonInteractive" -o $retval -eq $SUCCESS ] && - msg_error="${msg_error:-Error}${utility:+: $utility}" \ - f_show_err "%s" "$output" + + ! [ "$__no_dialog" -o "$nonInteractive" -o $__retval -eq $SUCCESS ] && + msg_error="${msg_error:-Error}${__utility:+: $__utility}" \ + f_show_err "%s" "$__output" # NB: f_show_err will handle NULL output appropriately - return $retval + + [ "$__var_to_set" ] && setvar "$__var_to_set" "$__output" + + return $__retval +} + +# f_count $var_to_set arguments ... +# +# Sets $var_to_set to the number of arguments minus one (the effective number +# of arguments following $var_to_set). +# +# Example: +# f_count count dog house # count=[2] +# +f_count() +{ + setvar "$1" $(( $# - 1 )) +} + +# f_count_ifs $var_to_set string ... +# +# Sets $var_to_set to the number of words (split by the internal field +# separator, IFS) following $var_to_set. +# +# Example 1: +# +# string="word1 word2 word3" +# f_count_ifs count "$string" # count=[3] +# f_count_ifs count $string # count=[3] +# +# Example 2: +# +# IFS=. f_count_ifs count www.freebsd.org # count=[3] +# +# NB: Make sure to use double-quotes if you are using a custom value for IFS +# and you don't want the current value to effect the result. See example 3. +# +# Example 3: +# +# string="a-b c-d" +# IFS=- f_count_ifs count "$string" # count=[3] +# IFS=- f_count_ifs count $string # count=[4] +# +f_count_ifs() +{ + local __var_to_set="$1" + shift 1 + set -- $* + setvar "$__var_to_set" $# } ############################################################ MAIN diff --git a/usr.sbin/bsdconfig/share/device.subr b/usr.sbin/bsdconfig/share/device.subr index f5d8ad7..e42751e 100644 --- a/usr.sbin/bsdconfig/share/device.subr +++ b/usr.sbin/bsdconfig/share/device.subr @@ -42,6 +42,7 @@ f_include_lang $BSDCFG_LIBE/include/messages.subr DEVICES= DEVICE_NAMES= +NDEVICES=0 # A "device" from sysinstall's point of view f_struct_define DEVICE \ @@ -100,7 +101,7 @@ f_device_try() { local name="$1" i="$2" var_path="$3" unit if [ "$i" ]; then - unit=$( printf "$name" "$i" ) + f_sprintf unit "$name" "$i" else unit="$name" fi @@ -213,7 +214,7 @@ f_device_reset_network() # f_device_get_all() { - local devname desc + local devname desc capacity f_dprintf "f_device_get_all: Probing devices..." f_dialog_info "$msg_probing_devices_please_wait_this_can_take_a_while" @@ -225,7 +226,7 @@ f_device_get_all() # as a media source for content # - local dev desc type max n=0 + local dev type max n=0 for dev in $DEVICE_NAMES; do n=$(( $n + 1 )) # Get the desc, type, and max (with debugging disabled) @@ -245,32 +246,32 @@ f_device_get_all() case "$type" in $DEVICE_TYPE_CDROM) f_device_try "$dev" "$i" devname || continue + f_device_capacity "$devname" capacity f_device_register "${devname##*/}" "$desc" \ "$devname" $DEVICE_TYPE_CDROM 1 \ f_media_init_cdrom f_media_get_cdrom \ - f_media_shutdown_cdrom "" \ - "$( f_device_capacity "$devname" )" + f_media_shutdown_cdrom "" "$capacity" f_dprintf "Found a CDROM device for %s" \ "$devname" ;; $DEVICE_TYPE_FLOPPY) f_device_try "$dev" "$i" devname || continue + f_device_capacity "$devname" capacity f_device_register "${devname##*/}" "$desc" \ "$devname" $DEVICE_TYPE_FLOPPY 1 \ f_media_init_floppy \ f_media_get_floppy \ - f_media_shutdown_floppy "" \ - "$( f_device_capacity "$devname" )" + f_media_shutdown_floppy "" "$capacity" f_dprintf "Found a floppy device for %s" \ "$devname" ;; $DEVICE_TYPE_USB) f_device_try "$dev" "$i" devname || continue + f_device_capacity "$devname" capacity f_device_register "${devname##*/}" "$desc" \ "$devname" $DEVICE_TYPE_USB 1 \ f_media_init_usb f_media_get_usb \ - f_media_shutdown_usb "" \ - "$( f_device_capacity "$devname" )" + f_media_shutdown_usb "" "$capacity" f_dprintf "Found a USB disk for %s" "$devname" ;; esac @@ -280,11 +281,11 @@ f_device_get_all() # Register ISO9660 providers as CDROM devices for devname in /dev/iso9660/*; do f_device_try "$devname" || continue + f_device_capacity "$devname" capacity f_device_register "${devname##*/}" "ISO9660 file system" \ "$devname" $DEVICE_TYPE_CDROM 1 \ f_media_init_cdrom f_media_get_cdrom \ - f_media_shutdown_cdrom "" \ - "$( f_device_capacity "$devname" )" + f_media_shutdown_cdrom "" "$capacity" f_dprintf "Found a CDROM device for %s" "$devname" done @@ -307,12 +308,12 @@ f_device_get_all() ' ) case "$filename" in *.iso) # Register the device as an ISO9660 provider + f_device_capacity "$devname" capacity f_device_register "${devname##*/}" \ "md(4) vnode file system" \ "$devname" $DEVICE_TYPE_CDROM 1 \ f_media_init_cdrom f_media_get_cdrom \ - f_media_shutdown_cdrom "" \ - "$( f_device_capacity "$devname" )" + f_media_shutdown_cdrom "" "$capacity" f_dprintf "Found a CDROM device for %s" "$devname" ;; esac @@ -347,10 +348,10 @@ f_device_get_all() # Try and find its description f_device_desc "$diskname" $DEVICE_TYPE_DISK desc + f_device_capacity "$diskname" capacity f_device_register "$diskname" "$desc" \ "/dev/$diskname" $DEVICE_TYPE_DISK 0 \ - "" "" "" "" \ - "$( f_device_capacity "$diskname" )" + "" "" "" "" "$capacity" f_dprintf "Found a disk device named %s" "$diskname" # Look for existing partitions to register @@ -360,11 +361,11 @@ f_device_get_all() case "$type" in 0x01|0x04|0x06|0x0b|0x0c|0x0e|0xef) # DOS partitions to add as "DOS media devices" + f_device_capacity "/dev/$slice" capacity f_device_register "$slice" "" \ "/dev/$slice" $DEVICE_TYPE_DOS 1 \ f_media_init_dos f_media_get_dos \ - f_media_shutdown_dos "" \ - "$( f_device_capacity "/dev/$slice" )" + f_media_shutdown_dos "" "$capacity" f_dprintf "Found a DOS partition %s" "$slice" ;; 0xa5) # FreeBSD partition @@ -379,14 +380,15 @@ f_device_get_all() ); do f_quietly dumpfs -m /dev/$part || continue + f_device_capacity \ + "$/dev/$part" capacity f_device_register \ "$part" "" "/dev/$part" \ $DEVICE_TYPE_UFS 1 \ f_media_init_ufs \ f_media_get_ufs \ f_media_shutdown_ufs "" \ - "$( f_device_capacity \ - "$/dev/$part" )" + "$capacity" f_dprintf "Found a UFS partition %s" \ "$part" done # parts @@ -1007,8 +1009,9 @@ f_network "xe" "Xircom/Intel EtherExpress Pro100/16 Ethernet card" f_network "xl" "3COM 3c90x / 3c90xB PCI Ethernet card" f_network "zyd" "ZyDAS ZD1211/ZD1211B USB 802.11 wireless adapter" +f_count NDEVICES $DEVICE_NAMES f_dprintf "%s: Initialized %u known device names/descriptions." device.subr \ - "$( set -- $DEVICE_NAMES; echo $# )" + $NDEVICES # # Scan for the above devices unless requeted otherwise diff --git a/usr.sbin/bsdconfig/share/dialog.subr b/usr.sbin/bsdconfig/share/dialog.subr index 1297711..d05d4fb 100644 --- a/usr.sbin/bsdconfig/share/dialog.subr +++ b/usr.sbin/bsdconfig/share/dialog.subr @@ -296,6 +296,7 @@ f_dialog_backtitle_restore() # f_dialog_max_size() { + local funcname=f_dialog_max_size local __var_height="$1" __var_width="$2" __max_size [ "$__var_height" -o "$__var_width" ] || return $FAILURE if [ "$USE_XDIALOG" ]; then @@ -304,11 +305,14 @@ f_dialog_max_size() if __max_size=$( $DIALOG --print-maxsize \ 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) then + f_dprintf "$funcname: %s --print-maxsize = [%s]" \ + "$DIALOG" "$__max_size" # usually "MaxSize: 24, 80" __max_size="${__max_size#*: }" f_replaceall "$__max_size" "," "" __max_size else - __max_size=$( stty size 2> /dev/null ) + f_eval_catch -dk __max_size $funcname stty \ + 'stty size' || __max_size= # usually "24 80" fi : ${__max_size:=$DEFAULT_TERMINAL_SIZE} @@ -2052,6 +2056,8 @@ f_dialog_menutag2index_with_help() # f_dialog_init() { + local funcname=f_dialog_init + DIALOG_SELF_INITIALIZE= USE_DIALOG=1 @@ -2140,7 +2146,7 @@ f_dialog_init() DIALOG=dialog f_die 1 "$msg_no_such_file_or_directory" "$pgm" "xauth" fi - HOSTNAME=$(hostname) + HOSTNAME=$( hostname ) local displaynum="${DISPLAY#*:}" eval xauth -if \~$SUDO_USER/.Xauthority extract - \ \"\$HOSTNAME/unix:\$displaynum\" \ @@ -2154,7 +2160,8 @@ f_dialog_init() # if [ "$USE_XDIALOG" ]; then local maxsize - if ! maxsize=$( LANG= LC_ALL= $DIALOG --print-maxsize 2>&1 ) + if ! f_eval_catch -dk maxsize $funcname "$DIALOG" \ + 'LANG= LC_ALL= %s --print-maxsize' "$DIALOG" then # Xdialog(1) failed, fall back to dialog(1) unset USE_XDIALOG diff --git a/usr.sbin/bsdconfig/share/keymap.subr b/usr.sbin/bsdconfig/share/keymap.subr index b2e615a..39b6b54 100644 --- a/usr.sbin/bsdconfig/share/keymap.subr +++ b/usr.sbin/bsdconfig/share/keymap.subr @@ -44,6 +44,7 @@ f_include $BSDCFG_SHARE/struct.subr ############################################################ GLOBALS KEYMAPS= +NKEYMAPS=0 # A "keymap" from kbdmap's point of view f_struct_define KEYMAP \ @@ -254,8 +255,8 @@ case "$KEYMAP_SELF_SCAN_ALL" in *) f_keymap_get_all esac -f_dprintf "%s: Found %u keymap file(s)." keymap.subr \ - "$( set -- $KEYMAPS; echo $# )" +f_count NKEYMAPS $KEYMAPS +f_dprintf "%s: Found %u keymap file(s)." keymap.subr $NKEYMAPS f_dprintf "%s: Successfully loaded." keymap.subr diff --git a/usr.sbin/bsdconfig/share/media/cdrom.subr b/usr.sbin/bsdconfig/share/media/cdrom.subr index 7f8b523..f14021e 100644 --- a/usr.sbin/bsdconfig/share/media/cdrom.subr +++ b/usr.sbin/bsdconfig/share/media/cdrom.subr @@ -58,7 +58,7 @@ f_media_set_cdrom() local devs ndevs f_device_find "" $DEVICE_TYPE_CDROM devs - ndevs=$( set -- $devs; echo $# ) + f_count ndevs $devs if [ ${ndevs:=0} -eq 0 ]; then f_interactive && f_show_msg "$msg_no_cd_dvd_devices_found" @@ -95,6 +95,7 @@ f_media_set_cdrom() # f_media_init_cdrom() { + local funcname=f_media_init_cdrom local dev="$1" devname err device_$dev get devname devname || return $FAILURE @@ -106,14 +107,13 @@ f_media_init_cdrom() return $SUCCESS fi - if [ ! -e "$MOUNTPOINT" ] && - ! err=$( mkdir -p "$MOUNTPOINT" 2>&1 ) - then - f_dialog_msgbox "$err" - return $FAILURE + if [ ! -e "$MOUNTPOINT" ]; then + f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" || + return $FAILURE fi - if ! err=$( mount_cd9660 "$devname" "$MOUNTPOINT" 2>&1 ) + if ! f_eval_catch -dk err $funcname mount_cd9660 \ + 'mount_cd9660 "%s" "%s"' "$devname" "$MOUNTPOINT" then err="${err#mount_cd9660: }"; err="${err#$devname: }" case "$err" in @@ -167,6 +167,7 @@ f_media_get_cdrom() # f_media_shutdown_cdrom() { + local funcname=f_media_shutdown_cdrom local dev="$1" err [ "$CDROM_MOUNTED" ] || return $FAILURE @@ -176,7 +177,9 @@ f_media_shutdown_cdrom() return $SUCCESS fi - if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); then + if ! f_eval_catch -dk err $funcname umount \ + 'umount -f "%s"' "$MOUNPOINT" + then err="${err#umount: }"; err="${err#*: }" f_show_msg "$msg_could_not_unmount_the_cdrom_dvd" \ "$MOUNTPOINT" "$err" @@ -191,11 +194,14 @@ f_media_shutdown_cdrom() # f_media_eject_cdrom() { + local funcname=f_media_eject_cdrom local dev="$1" devname err device_$dev get name devname || return $SUCCESS case "$devname" in /dev/iso9660/*) return $SUCCESS; esac f_dprintf "Ejecting CDROM/DVD at %s" "$devname" - if ! err=$( cdcontrol -f "$devname" eject 2>&1 ); then + if ! f_eval_catch -dk err $funcname cdcontrol \ + 'cdcontrol -f "%s" eject' "$devname" + then f_dprintf "Could not eject the CDROM/DVD from %s: %s" \ "$devname" "${err#cdcontrol: }" fi diff --git a/usr.sbin/bsdconfig/share/media/common.subr b/usr.sbin/bsdconfig/share/media/common.subr index 01d50a1..aac2cdd 100644 --- a/usr.sbin/bsdconfig/share/media/common.subr +++ b/usr.sbin/bsdconfig/share/media/common.subr @@ -99,6 +99,7 @@ f_media_verify() # f_media_generic_get() { + local funcname=f_media_generic_get local base="$1" file="$2" probe_type="$3" local fname=f_media_generic_get @@ -117,13 +118,10 @@ f_media_generic_get() f_dprintf "%s: file exists path=[%s]" $fname "$path" if [ "$probe_type" = "$PROBE_SIZE" ]; then local size - if ! size=$( stat -f %z "$path" 2>&1 ); then - f_dprintf "stat: %s" "$size" - echo "-1" - else - f_isinteger "$size" || size=-1 - echo $size - fi + f_eval_catch -dk size $funcname stat \ + 'stat -f %%z "%s"' "$path" || size=-1 + f_isinteger "$size" || size=-1 + echo $size fi [ "$probe_type" ] && return $SUCCESS cat "$path" @@ -136,13 +134,10 @@ f_media_generic_get() f_dprintf "%s: file exists path=[%s]" $fname "$path" if [ "$probe_type" = "$PROBE_SIZE" ]; then local size - if ! size=$( stat -f %z "$path" 2>&1 ); then - f_dprintf "stat: %s" "$size" - echo "-1" - else - f_isinteger "$size" || size=-1 - echo $size - fi + f_eval_catch -dk size $funcname stat \ + 'stat -f %%z "%s"' "$path" || size=-1 + f_isinteger "$size" || size=-1 + echo $size fi [ "$probe_type" ] && return $SUCCESS elif [ "$probe_type" ]; then diff --git a/usr.sbin/bsdconfig/share/media/dos.subr b/usr.sbin/bsdconfig/share/media/dos.subr index be4cfc7..e8e6aa6 100644 --- a/usr.sbin/bsdconfig/share/media/dos.subr +++ b/usr.sbin/bsdconfig/share/media/dos.subr @@ -56,7 +56,7 @@ f_media_set_dos() local devs ndevs f_device_find "" $DEVICE_TYPE_DOS devs - ndevs=$( set -- $devs; echo $# ) + f_count ndevs $devs if [ ${ndevs:=0} -eq 0 ]; then f_show_msg "$msg_no_dos_primary_partitions_found" @@ -93,6 +93,7 @@ f_media_set_dos() # f_media_init_dos() { + local funcname=f_media_init_dos local dev="$1" devname err device_$dev get devname devname || return $FAILURE @@ -104,14 +105,13 @@ f_media_init_dos() return $SUCCESS fi - if [ ! -e "$MOUNTPOINT" ] && - ! err=$( mkdir -p "$MOUNTPOINT" 2>&1 ) - then - f_dialog_msgbox "$err" - return $FAILURE + if [ ! -e "$MOUNTPOINT" ]; then + f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" || + return $FAILURE fi - if ! err=$( mount_msdosfs "$devname" "$MOUNTPOINT" 2>&1 ) + if ! f_eval_catch -dk err $funcname mount_msdosfs \ + 'mount_msdosfs "%s" "%s"' "$devname" "$MOUNTPOINT" then err="${err#mount_msdosfs: }"; err="${err#$devname: }" f_show_msg "$msg_error_mounting_device" \ @@ -146,11 +146,14 @@ f_media_get_dos() # f_media_shutdown_dos() { + local funcname=f_media_shutdown_dos local dev="$1" err [ "$DOS_MOUNTED" ] || return $FAILURE - if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); then + if ! f_eval_catch -dk err $funcname umount \ + 'umount -f "%s"' "$MOUNTPOINT" + then err="${err#umount: }"; err="${err#*: }" f_show_msg "$msg_could_not_unmount_the_dos_partition" \ "$MOUNTPOINT" "$err" diff --git a/usr.sbin/bsdconfig/share/media/floppy.subr b/usr.sbin/bsdconfig/share/media/floppy.subr index 62fe64b..e0e2df4 100644 --- a/usr.sbin/bsdconfig/share/media/floppy.subr +++ b/usr.sbin/bsdconfig/share/media/floppy.subr @@ -57,7 +57,7 @@ f_media_set_floppy() local devs ndevs f_device_find "" $DEVICE_TYPE_FLOPPY devs - ndevs=$( set -- $devs; echo $# ) + f_count ndevs $devs if [ ${ndevs:=0} -eq 0 ]; then f_interactive && f_show_msg "$msg_no_floppy_devices_found" @@ -98,6 +98,7 @@ f_media_set_floppy() # f_media_init_floppy() { + local funcname=f_media_init_floppy local dev="$1" devname err device_$dev get devname devname || return $FAILURE @@ -129,8 +130,11 @@ f_media_init_floppy() fi if ! { - f_quietly mount_msdosfs -o ro -m 0777 -u 0 -g 0 "$devname" "$mp" || - err=$( mount -o ro "$devname" "$mp" 2>&1 ) + f_eval_catch -dk err $funcname mount_msdosfs \ + 'mount_msdosfs -o ro -m 0777 -u 0 -g 0 "%s" "%s"' \ + "$devname" "$mp" || + f_eval_catch -dk err $funcname mount \ + 'mount -o ro "%s" "%s"' "$devname" "$mp" }; then err="${err#mount: }"; err="${err#*: }" local name @@ -153,6 +157,7 @@ f_media_init_floppy() # f_media_get_floppy() { + local funcname=f_media_get_floppy local dev="$1" file="$2" probe_type="$3" f_dprintf "f_media_get_floppy: dev=[%s] file=[%s] probe_type=%s" \ @@ -188,7 +193,7 @@ f_media_get_floppy() # if [ "$probe_type" = "$PROBE_SIZE" ]; then local size - size=$( stat -f %z "$fp" 2>&1 ) || f_dprintf "stat: %s" "$size" + f_eval_catch -dk size $funcname stat 'stat -f %%z "%s"' "$fp" f_isinteger "$size" || size=-1 echo "$size" fi @@ -203,15 +208,15 @@ f_media_get_floppy() # f_media_shutdown_floppy() { + local funcname=f_media_shutdown_floppy local dev="$1" err mp [ "$FLOPPY_MOUNTED" ] || return $FAILURE device_$dev get private mp - if ! err=$( umount -f "${mp:=$MOUNTPOINT}" 2>&1 ); then - err="${err#umount: }"; err="${err#*:}" - f_dprintf "Umount of floppy on %s failed: %s" "$mp" "$err" - else + if f_eval_catch -d $funcname umount \ + 'umount -f "%s"' "${mp:=$MOUNTPOINT}" + then FLOPPY_MOUNTED= if f_interactive && [ "$_systemState" != "fixit" ]; then local desc diff --git a/usr.sbin/bsdconfig/share/media/ftp.subr b/usr.sbin/bsdconfig/share/media/ftp.subr index d7f33bb..7f5256a3 100644 --- a/usr.sbin/bsdconfig/share/media/ftp.subr +++ b/usr.sbin/bsdconfig/share/media/ftp.subr @@ -794,6 +794,7 @@ f_media_init_ftp() # f_media_get_ftp() { + local funcname=f_media_get_ftp local dev="$1" file="$2" probe_type="$3" hosts= f_dprintf "f_media_get_ftp: dev=[%s] file=[%s] probe_type=%s" \ @@ -870,9 +871,10 @@ f_media_get_ftp() if [ "$probe_type" ]; then local url="ftp://$userpass$host$port/$dir/$file" size [ "$use_anon" ] && url="ftp://$host$port/$dir/$file" - if ! size=$( fetch -s "$url" 2>&1 ) || ! f_isinteger "$size" + if ! f_eval_catch -dk size $funcname fetch \ + 'fetch -s "%s"' "$url" || ! f_isinteger "$size" then - f_dprintf "request failed! size response=[%s]" "$size" + f_dprintf "size request failed!" [ "$probe_type" = "$PROBE_SIZE" ] && echo "-1" return $FAILURE fi diff --git a/usr.sbin/bsdconfig/share/media/nfs.subr b/usr.sbin/bsdconfig/share/media/nfs.subr index 8db5a06..33b37c8 100644 --- a/usr.sbin/bsdconfig/share/media/nfs.subr +++ b/usr.sbin/bsdconfig/share/media/nfs.subr @@ -154,6 +154,7 @@ f_media_set_nfs() # f_media_init_nfs() { + local funcname=f_media_init_nfs local dev="$1" name err device_$dev get name name || return $FAILURE @@ -170,11 +171,9 @@ f_media_init_nfs() return $FAILURE fi - if [ ! -e "$MOUNTPOINT" ] && - ! err=$( mkdir -p "$MOUNTPOINT" 2>&1 ) - then - f_dialog_msgbox "$err" - return $FAILURE + if [ ! -e "$MOUNTPOINT" ]; then + f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" || + return $FAILURE fi local cp tcp="" use3="" secure="" readsize=4096 writesize=4096 @@ -191,8 +190,9 @@ f_media_init_nfs() [ "$use3" ] && options="$options,nfsv3" [ "$tcp" ] && options="$options,tcp" - if ! err=$( mount_nfs \ - ${secure:+-P} -o "$options" "$name" "$MOUNTPOINT" 2>&1 ) + if ! f_eval_catch -dk err $funcname mount_nfs \ + 'mount_nfs %s -o "%s" "%s" "%s"' \ + "${secure:+-P}" "$options" "$name" "$MOUNTPOINT" then err="${err#mount_nfs: }" f_show_msg "$msg_error_mounting_device" \ @@ -231,12 +231,15 @@ f_media_get_nfs() # f_media_shutdown_nfs() { + local funcname=f_media_shutdown_nfs local dev="$1" err [ "$NFS_MOUNTED" ] || return $FAILURE f_dprintf "Unmounting NFS partition on %s" "$MOUNTPOINT" - if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); then + if ! f_eval_catch -dk err $funcname umount \ + 'umount -f "%s"' "$MOUNTPOINT" + then err="${err#umount: }"; err="${err#*: }" f_show_msg "$msg_could_not_unmount_the_nfs_partition" \ "$MOUNTPOINT" "$err" diff --git a/usr.sbin/bsdconfig/share/media/tcpip.subr b/usr.sbin/bsdconfig/share/media/tcpip.subr index edbdb7e..ea7350d8 100644 --- a/usr.sbin/bsdconfig/share/media/tcpip.subr +++ b/usr.sbin/bsdconfig/share/media/tcpip.subr @@ -33,6 +33,7 @@ BSDCFG_SHARE="/usr/share/bsdconfig" f_dprintf "%s: loading includes..." media/tcpip.subr f_include $BSDCFG_SHARE/device.subr f_include $BSDCFG_SHARE/dialog.subr +f_include $BSDCFG_SHARE/strings.subr f_include $BSDCFG_SHARE/struct.subr f_include $BSDCFG_SHARE/variable.subr @@ -168,8 +169,9 @@ f_inet_atoi() { local __addr="$1" __var_to_set="$2" __num=0 if f_validate_ipaddr "$__addr"; then - __num=$( IFS=.; set -- $__addr; \ - echo $(( ($1 << 24) + ($2 << 16) + ($3 << 8) + $4 )) ) + IFS=. + set -- $__addr + __num=$(( ($1 << 24) + ($2 << 16) + ($3 << 8) + $4 )) fi if [ "$__var_to_set" ]; then setvar "$__var_to_set" $__num @@ -574,7 +576,7 @@ f_ifconfig_netmask() local __octet __netmask= for __octet in $__octets; do - __netmask="$__netmask.$( printf "%u" "0x$__octet" )" + f_sprintf __netmask "%s.%u" "$__netmask" "0x$__octet" done __netmask="${__netmask#.}" if [ "$__var_to_set" ]; then @@ -1035,10 +1037,11 @@ f_host_lookup() # Calculate wait time as dividend of total time and host(1) invocations local __host_runs __wait + f_count __host_runs $__srvtypes if [ "$__ip6" = "YES" ]; then - __host_runs=$(( 2 + $( set -- $__srvtypes; echo $# ) )) + __host_runs=$(( $__host_runs + 2 )) else - __host_runs=$(( 1 + $( set -- $__srvtypes; echo $# ) )) + __host_runs=$(( $__host_runs + 1 )) fi f_getvar $VAR_MEDIA_TIMEOUT __wait [ "$__wait" ] && __wait="-W $(( $__wait / $__host_runs ))" @@ -1614,7 +1617,7 @@ f_device_select_tcp() esac f_device_find "$dev" $DEVICE_TYPE_NETWORK devs - cnt=$( set -- $devs; echo $# ) + f_count cnt $devs if [ ${cnt:=0} -gt 0 ]; then dev="${devs%%[$IFS]*}" @@ -1632,7 +1635,7 @@ f_device_select_tcp() fi # $network_dev f_device_find "" $DEVICE_TYPE_NETWORK devs - cnt=$( set -- $devs; echo $# ) + f_count cnt $devs dev="${devs%%[$IFS]*}" f_quietly f_getvar NETWORK_CONFIGURED # for debugging info diff --git a/usr.sbin/bsdconfig/share/media/ufs.subr b/usr.sbin/bsdconfig/share/media/ufs.subr index 2de29b3..a16b53d 100644 --- a/usr.sbin/bsdconfig/share/media/ufs.subr +++ b/usr.sbin/bsdconfig/share/media/ufs.subr @@ -67,7 +67,7 @@ f_media_set_ufs() local devs ndevs f_device_find "" $DEVICE_TYPE_UFS devs - ndevs=$( set -- $devs; echo $# ) + f_count ndevs $devs if [ ${ndevs:=0} -eq 0 ]; then f_variable_get_value $VAR_UFS_PATH \ @@ -117,6 +117,7 @@ f_media_set_ufs() # f_media_init_ufs() { + local funcname=f_media_init_ufs local dev="$1" devname err device_$dev get devname devname || return $FAILURE @@ -134,14 +135,13 @@ f_media_init_ufs() return $FAILURE fi - if [ ! -e "$MOUNTPOINT" ] && - ! err=$( mkdir -p "$MOUNTPOINT" 2>&1 ) - then - f_dialog_msgbox "$err" - return $FAILURE + if [ ! -e "$MOUNTPOINT" ]; then + f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" || + return $FAILURE fi - if ! err=$( mount "$devname" "$MOUNTPOINT" 2>&1 ) + if ! f_eval_catch -dk err $funcname mount \ + 'mount "%s" "%s"' "$devname" "$MOUNTPOINT" then err="${err#mount: }"; err="${err#$devname : }" f_show_msg "$msg_error_mounting_device" \ @@ -175,11 +175,14 @@ f_media_get_ufs() # f_media_shutdown_ufs() { + local funcname=f_media_shutdown_ufs local dev="$1" err [ "$UFS_MOUNTED" ] || return $FAILURE - if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); then + if ! f_eval_catch -dk err $funcname umount \ + 'umount -f "%s"' "$MOUNTPOINT" + then err="${err#umount: }"; err="${err#*: }" f_show_msg "$msg_could_not_unmount_the_ufs_partition" \ "$MOUNTPOINT" "$err" diff --git a/usr.sbin/bsdconfig/share/media/usb.subr b/usr.sbin/bsdconfig/share/media/usb.subr index f7afc29..39384fa 100644 --- a/usr.sbin/bsdconfig/share/media/usb.subr +++ b/usr.sbin/bsdconfig/share/media/usb.subr @@ -57,7 +57,7 @@ f_media_set_usb() local devs ndevs f_device_find "" $DEVICE_TYPE_USB devs - ndevs=$( set -- $devs; echo $# ) + f_count ndevs $devs if [ ${ndevs:=0} -eq 0 ]; then f_show_msg "$msg_no_usb_devices_found" @@ -103,6 +103,7 @@ f_media_set_usb() # f_media_init_usb() { + local funcname=f_media_init_usb local dev="$1" devname err device_$dev get devname devname || return $FAILURE @@ -114,14 +115,14 @@ f_media_init_usb() return $SUCCESS fi - if [ ! -e "$MOUNTPOINT" ] && - ! err=$( mkdir -p "$MOUNTPOINT" 2>&1 ) - then - f_dialog_msgbox "$err" - return $FAILURE + if [ ! -e "$MOUNTPOINT" ]; then + f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" || + return $FAILURE fi - if err=$( mount "$devname" "$MOUNTPOINT" 2>&1 ); then + if f_eval_catch -dk err $funcname mount \ + 'mount "%s" "%s"' "$devname" "$MOUNTPOINT" + then USB_MOUNTED=1 return $SUCCESS fi @@ -156,11 +157,14 @@ f_media_get_usb() # f_media_shutdown_usb() { + local funcname=f_media_shutdown_usb local dev="$1" err [ "$USB_MOUNTED" ] || return $FAILURE - if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); then + if ! f_eval_catch -dk err $funcname umount \ + 'umount -f "%s"' "$MOUNTPOINT" + then err="${err#umount: }"; err="${err#*: }" f_show_msg "$msg_could_not_unmount_the_ufs_partition" \ "$MOUNTPOINT" "$err" diff --git a/usr.sbin/bsdconfig/share/mustberoot.subr b/usr.sbin/bsdconfig/share/mustberoot.subr index 67b4c3e..88ff818 100644 --- a/usr.sbin/bsdconfig/share/mustberoot.subr +++ b/usr.sbin/bsdconfig/share/mustberoot.subr @@ -32,6 +32,7 @@ BSDCFG_SHARE="/usr/share/bsdconfig" . $BSDCFG_SHARE/common.subr || exit 1 f_dprintf "%s: loading includes..." mustberoot.subr f_include $BSDCFG_SHARE/dialog.subr +f_include $BSDCFG_SHARE/strings.subr BSDCFG_LIBE="/usr/libexec/bsdconfig" f_include_lang $BSDCFG_LIBE/include/messages.subr @@ -77,6 +78,7 @@ SECURE_DIVULGE_UNKNOWN_USER= # f_become_root_via_sudo() { + local funcname=f_become_root_via_sudo local prompt hline height width rows msg [ "$( id -u )" = "0" ] && return $SUCCESS @@ -90,8 +92,8 @@ f_become_root_via_sudo() # local checkpath="${HOME%/}/.bsdconfig_uses_sudo" if [ ! -e "$checkpath" ]; then - prompt=$( printf "$msg_you_are_not_root_but" bsdconfig ) - msg=$( printf "$msg_always_try_sudo_when_run_as" "$USER" ) + f_sprintf prompt "$msg_you_are_not_root_but" bsdconfig + f_sprintf msg "$msg_always_try_sudo_when_run_as" "$USER" local menu_list=" 'X' '$msg_cancel_exit' '1' '$msg' @@ -124,12 +126,9 @@ f_become_root_via_sudo() X) # Cancel/Exit f_die ;; 1) # Always try sudo(8) when run as $user - local err - if ! err=$( touch "$checkpath" 2>&1 ); then - f_dialog_msgbox "$err" - else + f_eval_catch $funcname touch \ + 'touch "%s"' "$checkpath" && f_show_msg "$msg_created_path" "$checkpath" - fi esac else # @@ -225,7 +224,7 @@ f_become_root_via_sudo() # the security event and exit immediately. # if [ $nfailures -ge $PASSWD_TRIES ]; then - msg=$( printf "$msg_nfailed_attempts" "$nfailures" ) + f_sprintf msg "$msg_nfailed_attempts" "$nfailures" logger -p auth.notice -t sudo " " \ "$USER : $msg" \ "; TTY=$(tty)" \ @@ -387,7 +386,7 @@ f_authenticate_some_user() # the security event and exit immediately. # if [ $nfailures -ge $PASSWD_TRIES ]; then - msg=$( printf "$msg_nfailed_attempts" "$nfailures" ) + f_sprintf msg "$msg_nfailed_attempts" "$nfailures" logger -p auth.notice -t sudo " " \ "${SUDO_USER:-$USER} : $msg" \ "; TTY=$(tty)" \ diff --git a/usr.sbin/bsdconfig/share/packages/categories.subr b/usr.sbin/bsdconfig/share/packages/categories.subr index dd2fa8f..474c41d 100755 --- a/usr.sbin/bsdconfig/share/packages/categories.subr +++ b/usr.sbin/bsdconfig/share/packages/categories.subr @@ -39,6 +39,7 @@ f_include_lang $BSDCFG_LIBE/include/messages.subr ############################################################ GLOBALS CATEGORIES= +NCATEGORIES=0 ############################################################ FUNCTIONS @@ -199,8 +200,9 @@ f_category x11-wm "$msg_x11_wm_desc" f_category xfce "$msg_xfce_desc" f_category zope "$msg_zope_desc" +f_count NCATEGORIES $CATEGORIES f_dprintf "%s: Initialized %u package category descriptions." \ - packages/categories.subr "$( set -- $CATEGORIES; echo $# )" + packages/categories.subr $NCATEGORIES f_dprintf "%s: Successfully loaded." packages/categories.subr diff --git a/usr.sbin/bsdconfig/share/packages/index.subr b/usr.sbin/bsdconfig/share/packages/index.subr index 535d815..f0c1725 100755 --- a/usr.sbin/bsdconfig/share/packages/index.subr +++ b/usr.sbin/bsdconfig/share/packages/index.subr @@ -248,17 +248,21 @@ f_index_initialize() # # Create a new temporary file to write to - local __tmpfile="$( mktemp -t "$pgm" )" - if [ "$__tmpfile" ]; then + local __tmpfile + if f_eval_catch -dk __tmpfile $__funcname mktemp \ + 'mktemp -t "%s"' "$pgm" + then # Write the temporary file contents echo "$__sqlite_digest" > "$__tmpfile" debug= f_getvar "$__var_to_set" >> "$__tmpfile" # Finally, move the temporary file into place case "$PACKAGES_INDEX_CACHEFILE" in - */*) f_quietly mkdir -p "${PACKAGES_INDEX_CACHEFILE%/*}" + */*) f_eval_catch -d $funcname mkdir \ + 'mkdir -p "%s"' "${PACKAGES_INDEX_CACHEFILE%/*}" esac - f_quietly mv -f "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE" + f_eval_catch -d $__funcname mv 'mv -f "%s" "%s"' \ + "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE" fi f_show_info "$msg_located_index_now_reading_package_data_from_it" diff --git a/usr.sbin/bsdconfig/share/packages/packages.subr b/usr.sbin/bsdconfig/share/packages/packages.subr index a4ecdb9..36c1172 100755 --- a/usr.sbin/bsdconfig/share/packages/packages.subr +++ b/usr.sbin/bsdconfig/share/packages/packages.subr @@ -472,8 +472,7 @@ f_package_menu_select() "$msg_all"|"") f_category_desc_get "All" prompt ;; *) f_category_desc_get "$category" prompt ;; esac - prompt="$prompt $( printf "$msg_page_of_npages" \ - "$page" "$npages" )" + f_sprintf prompt "%s $msg_page_of_npages" "$prompt" "$page" "$npages" local mheight mwidth mrows eval f_dialog_menu${SHOW_DESC:+_with_help}_size mheight mwidth mrows \ @@ -536,7 +535,7 @@ f_package_menu_deselect() " # End-Quote local hline="$hline_alnum_arrows_punc_tab_enter" - prompt=$( printf "$msg_what_would_you_like_to_do_with" "$package" ) + f_sprintf prompt "$msg_what_would_you_like_to_do_with" "$package" local height width rows eval f_dialog_menu_size height width rows \ @@ -578,7 +577,7 @@ f_package_review() f_dprintf "$funcname: SELECTED_PACKAGES=[%s]" "$SELECTED_PACKAGES" - prompt=$( printf "$msg_reviewing_selected_packages" "$_All_nselected" ) + f_sprintf prompt "$msg_reviewing_selected_packages" "$_All_nselected" local package varpkg mark for package in $SELECTED_PACKAGES; do diff --git a/usr.sbin/bsdconfig/share/strings.subr b/usr.sbin/bsdconfig/share/strings.subr index 25cceee..9b0014f 100644 --- a/usr.sbin/bsdconfig/share/strings.subr +++ b/usr.sbin/bsdconfig/share/strings.subr @@ -188,15 +188,8 @@ f_number_of_lines() # f_isinteger() { - local arg="$1" - - # Prevent division-by-zero - [ "$arg" = "0" ] && return $SUCCESS - - # Attempt to perform arithmetic divison (an operation which will exit - # with error unless arg is a valid positive/negative whole integer). - # - ( : $((0/$arg)) ) > /dev/null 2>&1 + local arg="${1#-}" + [ "${arg:-x}" = "${arg#[!0-9]*}" ] } # f_uriencode [$text] diff --git a/usr.sbin/bsdconfig/share/sysrc.subr b/usr.sbin/bsdconfig/share/sysrc.subr index c1bd0e2..d863ba0 100644 --- a/usr.sbin/bsdconfig/share/sysrc.subr +++ b/usr.sbin/bsdconfig/share/sysrc.subr @@ -428,6 +428,7 @@ END { exit retval } ' f_sysrc_set() { + local funcname=f_sysrc_set local varname="$1" new_value="$2" # Check arguments @@ -478,8 +479,12 @@ f_sysrc_set() # # Create a new temporary file to write to. # - local tmpfile="$( mktemp -t "$pgm" )" - [ "$tmpfile" ] || return $FAILURE + local tmpfile + if ! f_eval_catch -dk tmpfile $funcname mktemp 'mktemp -t "%s"' "$pgm" + then + echo "$tmpfile" >&2 + return $FAILURE + fi # # Fixup permissions (else we're in for a surprise, as mktemp(1) creates @@ -488,8 +493,9 @@ f_sysrc_set() # permissions from the temporary file). # local mode - mode=$( stat -f '%#Lp' "$file" 2> /dev/null ) - f_quietly chmod "${mode:-0644}" "$tmpfile" + f_eval_catch -dk mode $funcname stat 'stat -f "%%#Lp" "%s"' "$file" || + mode=0644 + f_eval_catch -d $funcname chmod 'chmod "%s" "%s"' "$mode" "$tmpfile" # # Fixup ownership. The destination file _is_ writable (we tested @@ -497,8 +503,9 @@ f_sysrc_set() # permissions (so we throw stderr into the bit-bucket). # local owner - owner=$( stat -f '%u:%g' "$file" 2> /dev/null ) - f_quietly chown "${owner:-root:wheel}" "$tmpfile" + f_eval_catch -dk owner $funcname stat \ + 'stat -f "%%u:%%g" "%s"' "$file" || owner="root:wheel" + f_eval_catch -d $funcname chown 'chown "%s" "%s"' "$owner" "$tmpfile" # # Operate on the matching file, replacing only the last occurrence. @@ -520,7 +527,7 @@ f_sysrc_set() # # Taint-check our results. # - if ! /bin/sh -n "$tmpfile"; then + if ! f_eval_catch -d $funcname sh '/bin/sh -n "%s"' "$tmpfile"; then f_err "$msg_previous_syntax_errors\n" "$pgm" "$file" rm -f "$tmpfile" return $FAILURE @@ -529,7 +536,7 @@ f_sysrc_set() # # Finally, move the temporary file into place. # - mv "$tmpfile" "$file" + f_eval_catch -de $funcname mv 'mv "%s" "%s"' "$tmpfile" "$file" } # f_sysrc_delete $varname @@ -560,6 +567,7 @@ END { exit ! found } ' f_sysrc_delete() { + local funcname=f_sysrc_delete local varname="$1" local file @@ -569,24 +577,33 @@ f_sysrc_delete() # # Operate on each of the specified files # + local tmpfile for file in ${RC_CONFS-$( f_sysrc_get rc_conf_files )}; do [ -e "$file" ] || continue # # Create a new temporary file to write to. # - local tmpfile="$( mktemp -t "$pgm" )" - [ "$tmpfile" ] || return $FAILURE + if ! f_eval_catch -dk tmpfile $funcname mktemp \ + 'mktemp -t "%s"' "$pgm" + then + echo "$tmpfile" >&2 + return $FAILURE + fi # # Fixup permissions and ownership (mktemp(1) defaults to 0600 # permissions) to instead match the destination file. # local mode owner - mode=$( stat -f '%#Lp' "$file" 2> /dev/null ) - owner=$( stat -f '%u:%g' "$file" 2> /dev/null ) - f_quietly chmod "${mode:-0644}" "$tmpfile" - f_quietly chown "${owner:-root:wheel}" "$tmpfile" + f_eval_catch -dk mode $funcname stat \ + 'stat -f "%%#Lp" "%s"' "$file" || mode=0644 + f_eval_catch -dk owner $funcname stat \ + 'stat -f "%%u:%%g" "%s"' "$file" || owner="root:wheel" + f_eval_catch -d $funcname chmod \ + 'chmod "%s" "%s"' "$mode" "$tmpfile" + f_eval_catch -d $funcname chown \ + 'chown "%s" "%s"' "$owner" "$tmpfile" # # Operate on the file, removing all occurrences, saving the @@ -603,7 +620,8 @@ f_sysrc_delete() # # Taint-check our results. # - if ! /bin/sh -n "$tmpfile"; then + if ! f_eval_catch -d $funcname sh '/bin/sh -n "%s"' "$tmpfile" + then f_err "$msg_previous_syntax_errors\n" \ "$pgm" "$file" rm -f "$tmpfile" @@ -622,7 +640,8 @@ f_sysrc_delete() # # Finally, move the temporary file into place. # - mv "$tmpfile" "$file" + f_eval_catch -de $funcname mv \ + 'mv "%s" "%s"' "$tmpfile" "$file" || return $FAILURE done } |