common.subr (256281) | common.subr (258420) |
---|---|
1if [ ! "$_COMMON_SUBR" ]; then _COMMON_SUBR=1 2# 3# Copyright (c) 2012 Ron McDowell 4# Copyright (c) 2012-2013 Devin Teske 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions --- 11 unchanged lines hidden (view full) --- 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# | 1if [ ! "$_COMMON_SUBR" ]; then _COMMON_SUBR=1 2# 3# Copyright (c) 2012 Ron McDowell 4# Copyright (c) 2012-2013 Devin Teske 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions --- 11 unchanged lines hidden (view full) --- 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# |
28# $FreeBSD: stable/10/usr.sbin/bsdconfig/share/common.subr 256181 2013-10-09 08:12:26Z dteske $ | 28# $FreeBSD: stable/10/usr.sbin/bsdconfig/share/common.subr 258420 2013-11-21 03:38:47Z dteske $ |
29# 30############################################################ CONFIGURATION 31 32# 33# Default file descriptors to link to stdout/stderr for passthru allowing 34# redirection within a sub-shell to bypass directly to the terminal. 35# 36: ${TERMINAL_STDOUT_PASSTHRU:=3}} --- 18 unchanged lines hidden (view full) --- 55SUCCESS=0 56FAILURE=1 57 58# 59# Operating environment details 60# 61export UNAME_S="$(uname -s)" # Operating System (i.e. FreeBSD) 62export UNAME_P="$(uname -p)" # Processor Architecture (i.e. i386) | 29# 30############################################################ CONFIGURATION 31 32# 33# Default file descriptors to link to stdout/stderr for passthru allowing 34# redirection within a sub-shell to bypass directly to the terminal. 35# 36: ${TERMINAL_STDOUT_PASSTHRU:=3}} --- 18 unchanged lines hidden (view full) --- 55SUCCESS=0 56FAILURE=1 57 58# 59# Operating environment details 60# 61export UNAME_S="$(uname -s)" # Operating System (i.e. FreeBSD) 62export UNAME_P="$(uname -p)" # Processor Architecture (i.e. i386) |
63export UNAME_M="$(uname -m)" # Machine platform (i.e. i386) |
|
63export UNAME_R="$(uname -r)" # Release Level (i.e. X.Y-RELEASE) | 64export UNAME_R="$(uname -r)" # Release Level (i.e. X.Y-RELEASE) |
65if [ ! "${PKG_ABI+set}" ]; then 66 export PKG_ABI="$( 67 ASSUME_ALWAYS_YES=1 pkg -vv | 68 awk '$1=="ABI:"{print $2;exit}' 2> /dev/null 69 )" 70fi |
|
64 65# 66# Default behavior is to call f_debug_init() automatically when loaded. 67# 68: ${DEBUG_SELF_INITIALIZE=1} 69 70# | 71 72# 73# Default behavior is to call f_debug_init() automatically when loaded. 74# 75: ${DEBUG_SELF_INITIALIZE=1} 76 77# |
78# Default behavior of f_debug_init() is to truncate $debugFile (set to NULL to 79# disable truncating the debug file when initializing). To get child processes 80# to append to the same log file, export this variarable (with a NULL value) 81# and also export debugFile with the desired value. 82# 83: ${DEBUG_INITIALIZE_FILE=1} 84 85# |
|
71# Define standard optstring arguments that should be supported by all programs 72# using this include (unless DEBUG_SELF_INITIALIZE is set to NULL to prevent 73# f_debug_init() from autamatically processing "$@" for the below arguments): 74# 75# d Sets $debug to 1 76# D: Sets $debugFile to $OPTARG 77# 78GETOPTS_STDARGS="dD:" --- 22 unchanged lines hidden (view full) --- 101# the following variable (which defaults to NULL) to indicate that there are 102# extra flags that should be considered when performing automatic processing of 103# globally persistent flags. 104# 105: ${GETOPTS_EXTRA:=} 106 107############################################################ FUNCTIONS 108 | 86# Define standard optstring arguments that should be supported by all programs 87# using this include (unless DEBUG_SELF_INITIALIZE is set to NULL to prevent 88# f_debug_init() from autamatically processing "$@" for the below arguments): 89# 90# d Sets $debug to 1 91# D: Sets $debugFile to $OPTARG 92# 93GETOPTS_STDARGS="dD:" --- 22 unchanged lines hidden (view full) --- 116# the following variable (which defaults to NULL) to indicate that there are 117# extra flags that should be considered when performing automatic processing of 118# globally persistent flags. 119# 120: ${GETOPTS_EXTRA:=} 121 122############################################################ FUNCTIONS 123 |
109# f_dprintf $fmt [ $opts ... ] | 124# f_dprintf $format [$arguments ...] |
110# 111# Sensible debug function. Override in ~/.bsdconfigrc if desired. 112# See /usr/share/examples/bsdconfig/bsdconfigrc for example. 113# 114# If $debug is set and non-NULL, prints DEBUG info using printf(1) syntax: 115# + To $debugFile, if set and non-NULL 116# + To standard output if $debugFile is either NULL or unset 117# + To both if $debugFile begins with a single plus-sign (`+') --- 41 unchanged lines hidden (view full) --- 159 160 # 161 # Make debugging persistant if set 162 # 163 [ "$debug" ] && export debug 164 [ "$debugFile" ] && export debugFile 165 166 # | 125# 126# Sensible debug function. Override in ~/.bsdconfigrc if desired. 127# See /usr/share/examples/bsdconfig/bsdconfigrc for example. 128# 129# If $debug is set and non-NULL, prints DEBUG info using printf(1) syntax: 130# + To $debugFile, if set and non-NULL 131# + To standard output if $debugFile is either NULL or unset 132# + To both if $debugFile begins with a single plus-sign (`+') --- 41 unchanged lines hidden (view full) --- 174 175 # 176 # Make debugging persistant if set 177 # 178 [ "$debug" ] && export debug 179 [ "$debugFile" ] && export debugFile 180 181 # |
167 # Truncate the debug file upon. Note that we will trim a leading plus 168 # (`+') from the value of debugFile to support persistant meaning that 169 # f_dprintf() should print both to standard output and $debugFile 170 # (minus the leading plus, of course). | 182 # Truncate debug file unless requested otherwise. Note that we will 183 # trim a leading plus (`+') from the value of debugFile to support 184 # persistant meaning that f_dprintf() should print both to standard 185 # output and $debugFile (minus the leading plus, of course). |
171 # 172 local _debug_file="${debugFile#+}" | 186 # 187 local _debug_file="${debugFile#+}" |
173 if [ "$_debug_file" ]; then | 188 if [ "$_debug_file" -a "$DEBUG_INITIALIZE_FILE" ]; then |
174 if ( umask 022 && :> "$_debug_file" ); then 175 f_dprintf "Successfully initialized debugFile \`%s'" \ 176 "$_debug_file" | 189 if ( umask 022 && :> "$_debug_file" ); then 190 f_dprintf "Successfully initialized debugFile \`%s'" \ 191 "$_debug_file" |
177 [ "${debug+set}" ] || 178 debug=1 # turn debugging on if not set | 192 f_isset debug || debug=1 # turn debugging on if not set |
179 else 180 unset debugFile 181 f_dprintf "Unable to initialize debugFile \`%s'" \ 182 "$_debug_file" 183 fi 184 fi 185} 186 | 193 else 194 unset debugFile 195 f_dprintf "Unable to initialize debugFile \`%s'" \ 196 "$_debug_file" 197 fi 198 fi 199} 200 |
187# f_err $fmt [ $opts ... ] | 201# f_err $format [$arguments ...] |
188# 189# Print a message to stderr (fd=2). 190# 191f_err() 192{ 193 printf "$@" >&${TERMINAL_STDERR_PASSTHRU:-2} 194} 195 | 202# 203# Print a message to stderr (fd=2). 204# 205f_err() 206{ 207 printf "$@" >&${TERMINAL_STDERR_PASSTHRU:-2} 208} 209 |
196# f_quietly $command [ $arguments ... ] | 210# f_quietly $command [$arguments ...] |
197# 198# Run a command quietly (quell any output to stdout or stderr) 199# 200f_quietly() 201{ 202 "$@" > /dev/null 2>&1 203} 204 --- 65 unchanged lines hidden (view full) --- 270# Check if variable $var is set. Returns success if variable is set, otherwise 271# returns failure. 272# 273f_isset() 274{ 275 eval [ \"\${${1%%[$IFS]*}+set}\" ] 276} 277 | 211# 212# Run a command quietly (quell any output to stdout or stderr) 213# 214f_quietly() 215{ 216 "$@" > /dev/null 2>&1 217} 218 --- 65 unchanged lines hidden (view full) --- 284# Check if variable $var is set. Returns success if variable is set, otherwise 285# returns failure. 286# 287f_isset() 288{ 289 eval [ \"\${${1%%[$IFS]*}+set}\" ] 290} 291 |
278# f_die [ $status [ $fmt [ $opts ... ]]] | 292# f_die [$status [$format [$arguments ...]]] |
279# 280# Abruptly terminate due to an error optionally displaying a message in a 281# dialog box using printf(1) syntax. 282# 283f_die() 284{ 285 local status=$FAILURE 286 --- 17 unchanged lines hidden (view full) --- 304# Interrupt handler. 305# 306f_interrupt() 307{ 308 exec 2>&1 # fix sh(1) bug where stderr gets lost within async-trap 309 f_die 310} 311 | 293# 294# Abruptly terminate due to an error optionally displaying a message in a 295# dialog box using printf(1) syntax. 296# 297f_die() 298{ 299 local status=$FAILURE 300 --- 17 unchanged lines hidden (view full) --- 318# Interrupt handler. 319# 320f_interrupt() 321{ 322 exec 2>&1 # fix sh(1) bug where stderr gets lost within async-trap 323 f_die 324} 325 |
312# f_show_info $fmt [ $opts ... ] | 326# f_show_info $format [$arguments ...] |
313# 314# Display a message in a dialog infobox using printf(1) syntax. 315# 316f_show_info() 317{ 318 local msg 319 msg=$( printf "$@" ) 320 --- 4 unchanged lines hidden (view full) --- 325 # 326 if f_have f_dialog_info; then 327 f_dialog_info "$msg" 328 else 329 dialog --infobox "$msg" 0 0 330 fi 331} 332 | 327# 328# Display a message in a dialog infobox using printf(1) syntax. 329# 330f_show_info() 331{ 332 local msg 333 msg=$( printf "$@" ) 334 --- 4 unchanged lines hidden (view full) --- 339 # 340 if f_have f_dialog_info; then 341 f_dialog_info "$msg" 342 else 343 dialog --infobox "$msg" 0 0 344 fi 345} 346 |
333# f_show_msg $fmt [ $opts ... ] | 347# f_show_msg $format [$arguments ...] |
334# 335# Display a message in a dialog box using printf(1) syntax. 336# 337f_show_msg() 338{ 339 local msg 340 msg=$( printf "$@" ) 341 --- 4 unchanged lines hidden (view full) --- 346 # 347 if f_have f_dialog_msgbox; then 348 f_dialog_msgbox "$msg" 349 else 350 dialog --msgbox "$msg" 0 0 351 fi 352} 353 | 348# 349# Display a message in a dialog box using printf(1) syntax. 350# 351f_show_msg() 352{ 353 local msg 354 msg=$( printf "$@" ) 355 --- 4 unchanged lines hidden (view full) --- 360 # 361 if f_have f_dialog_msgbox; then 362 f_dialog_msgbox "$msg" 363 else 364 dialog --msgbox "$msg" 0 0 365 fi 366} 367 |
368# f_show_err $format [$arguments ...] 369# 370# Display a message in a dialog box with ``Error'' i18n title (overridden by 371# setting msg_error) using printf(1) syntax. If running non-interactively, 372# the process will terminate (using [above] f_die()). 373# 374f_show_err() 375{ 376 [ "$nonInteractive" ] && f_die |
|
354 | 377 |
355# f_yesno $fmt [ $opts ... ] | 378 local msg 379 msg=$( printf "$@" ) 380 381 : ${msg:=${msg_an_unknown_error_occurred:-An unknown error occurred}} 382 383 if [ "$_DIALOG_SUBR" ]; then 384 f_dialog_title "${msg_error:-Error}" 385 f_dialog_msgbox "$msg" 386 f_dialog_title_restore 387 else 388 dialog --title "${msg_error:-Error}" --msgbox "$msg" 0 0 389 fi 390 return $SUCCESS 391} 392 393# f_yesno $format [$arguments ...] |
356# 357# Display a message in a dialog yes/no box using printf(1) syntax. 358# 359f_yesno() 360{ 361 local msg 362 msg=$( printf "$@" ) 363 --- 4 unchanged lines hidden (view full) --- 368 # 369 if f_have f_dialog_yesno; then 370 f_dialog_yesno "$msg" 371 else 372 dialog --yesno "$msg" 0 0 373 fi 374} 375 | 394# 395# Display a message in a dialog yes/no box using printf(1) syntax. 396# 397f_yesno() 398{ 399 local msg 400 msg=$( printf "$@" ) 401 --- 4 unchanged lines hidden (view full) --- 406 # 407 if f_have f_dialog_yesno; then 408 f_dialog_yesno "$msg" 409 else 410 dialog --yesno "$msg" 0 0 411 fi 412} 413 |
376# f_noyes $fmt [ $opts ... ] | 414# f_noyes $format [$arguments ...] |
377# 378# Display a message in a dialog yes/no box using printf(1) syntax. 379# NOTE: THis is just like the f_yesno function except "No" is default. 380# 381f_noyes() 382{ 383 local msg 384 msg=$( printf "$@" ) --- 76 unchanged lines hidden (view full) --- 461 f_dprintf "f_include_lang: file=[%s] lang=[%s]" "$file" "$lang" 462 if [ -f "$file.$lang" ]; then 463 . "$file.$lang" || exit $? 464 else 465 . "$file" || exit $? 466 fi 467} 468 | 415# 416# Display a message in a dialog yes/no box using printf(1) syntax. 417# NOTE: THis is just like the f_yesno function except "No" is default. 418# 419f_noyes() 420{ 421 local msg 422 msg=$( printf "$@" ) --- 76 unchanged lines hidden (view full) --- 499 f_dprintf "f_include_lang: file=[%s] lang=[%s]" "$file" "$lang" 500 if [ -f "$file.$lang" ]; then 501 . "$file.$lang" || exit $? 502 else 503 . "$file" || exit $? 504 fi 505} 506 |
469# f_usage $file [ $key1 $value1 ... ] | 507# f_usage $file [$key1 $value1 ...] |
470# 471# Display USAGE file with optional pre-processor macro definitions. The first 472# argument is the template file containing the usage text to be displayed. If 473# $LANG or $LC_ALL (in order of preference, respectively) is set, ".encoding" 474# will automatically be appended as a suffix to the provided $file pathname. 475# 476# When processing $file, output begins at the first line containing that is 477# (a) not a comment, (b) not empty, and (c) is not pure-whitespace. All lines --- 263 unchanged lines hidden (view full) --- 741# 742f_mounted() 743{ 744 local dir="$1" 745 [ -d "$dir" ] || return $FAILURE 746 mount | grep -Eq " on $dir \([^)]+\)$" 747} 748 | 508# 509# Display USAGE file with optional pre-processor macro definitions. The first 510# argument is the template file containing the usage text to be displayed. If 511# $LANG or $LC_ALL (in order of preference, respectively) is set, ".encoding" 512# will automatically be appended as a suffix to the provided $file pathname. 513# 514# When processing $file, output begins at the first line containing that is 515# (a) not a comment, (b) not empty, and (c) is not pure-whitespace. All lines --- 263 unchanged lines hidden (view full) --- 779# 780f_mounted() 781{ 782 local dir="$1" 783 [ -d "$dir" ] || return $FAILURE 784 mount | grep -Eq " on $dir \([^)]+\)$" 785} 786 |
787# f_eval_catch [-d] $funcname $utility $format [$arguments ...] 788# 789# Silently evaluate a command in a sub-shell and test for error. If debugging 790# is enabled a copy of the command and its output is sent to debug (either 791# stdout or file depending on environment). If an error occurs, output of the 792# command is displayed in a dialog(1) msgbox using the [above] f_show_err() 793# function (unless optional `-d' flag is the first argument, then no dialog). 794# The $funcname argument is sent to debugging while the $utility argument is 795# used in the title of the dialog box. The command that is sent to debugging 796# along with $funcname is the product of the printf(1) syntax produced by 797# $format with optional $arguments. 798# 799# Example 1: 800# 801# debug=1 802# f_eval_catch myfunc cat 'contents=$( cat "%s" )' /some/file 803# # Error displayed ``cat: /some/file: No such file or directory'' 804# 805# Produces the following debug output: 806# 807# DEBUG: myfunc: cat "/some/file" 808# DEBUG: myfunc: retval=1 <output below> 809# cat: /some/file: No such file or directory 810# 811# Example 2: 812# 813# debug=1 814# f_eval_catch myfunc echo 'echo "%s"' "Hello, World!" 815# # No error displayed 816# 817# Produces the following debug output: 818# 819# DEBUG: myfunc: echo "Hello, World!" 820# DEBUG: myfunc: retval=0 <output below> 821# Hello, World! 822# 823# Example 3: 824# 825# debug=1 826# echo 123 | f_eval_catch myfunc rev rev 827# # No error displayed 828# 829# Produces the following debug output: 830# 831# DEBUG: myfunc: rev 832# DEBUG: myfunc: retval=0 <output below> 833# 321 834# 835# Example 4: 836# 837# debug=1 838# f_eval_catch myfunc true true 839# # No error displayed 840# 841# Produces the following debug output: 842# 843# DEBUG: myfunc: true 844# DEBUG: myfunc: retval=0 <no output> 845# 846f_eval_catch() 847{ 848 local no_dialog= 849 [ "$1" = "-d" ] && no_dialog=1 && shift 1 850 local funcname="$1" utility="$2"; shift 2 851 local cmd output retval 852 cmd=$( printf -- "$@" ) 853 f_dprintf "%s: %s" "$funcname" "$cmd" # Log command *before* eval 854 output=$( exec 2>&1; eval "$cmd" ) 855 retval=$? 856 if [ "$output" ]; then 857 f_dprintf "%s: retval=%i <output below>\n%s" "$funcname" \ 858 $retval "$output" 859 else 860 f_dprintf "%s: retval=%i <no output>" "$funcname" $retval 861 fi 862 ! [ "$no_dialog" -o "$nonInteractive" -o $retval -eq $SUCCESS ] && 863 msg_error="${msg_error:-Error}${utility:+: $utility}" \ 864 f_show_err "%s" "$output" 865 # NB: f_show_err will handle NULL output appropriately 866 return $retval 867} 868 |
|
749############################################################ MAIN 750 751# 752# Trap signals so we can recover gracefully 753# 754trap 'f_interrupt' SIGINT 755trap 'f_die' SIGTERM SIGPIPE SIGXCPU SIGXFSZ \ 756 SIGFPE SIGTRAP SIGABRT SIGSEGV --- 27 unchanged lines hidden --- | 869############################################################ MAIN 870 871# 872# Trap signals so we can recover gracefully 873# 874trap 'f_interrupt' SIGINT 875trap 'f_die' SIGTERM SIGPIPE SIGXCPU SIGXFSZ \ 876 SIGFPE SIGTRAP SIGABRT SIGSEGV --- 27 unchanged lines hidden --- |