1if [ ! "$_COMMON_SUBR" ]; then _COMMON_SUBR=1 2# 3# Copyright (c) 2012 Ron McDowell 4# Copyright (c) 2012-2014 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 9# are met: 10# 1. Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 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-2014 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 9# are met: 10# 1. Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 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#
|
157 f_dprintf "f_debug_init: ARGV=[%s] GETOPTS_STDARGS=[%s]" \ 158 "$ARGV" "$GETOPTS_STDARGS" 159 while getopts "$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" flag \ 160 > /dev/null; do 161 case "$flag" in 162 d) debug=1 ;; 163 D) debugFile="$OPTARG" ;; 164 esac 165 done 166 shift $(( $OPTIND - 1 )) 167 f_dprintf "f_debug_init: debug=[%s] debugFile=[%s]" \ 168 "$debug" "$debugFile" 169 170 # 171 # Automagically enable debugging if debugFile is set (and non-NULL) 172 # 173 [ "$debugFile" ] && { [ "${debug+set}" ] || debug=1; } 174 175 # 176 # Make debugging persistant if set 177 # 178 [ "$debug" ] && export debug 179 [ "$debugFile" ] && export debugFile 180 181 # 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). 186 # 187 local _debug_file="${debugFile#+}" 188 if [ "$_debug_file" -a "$DEBUG_INITIALIZE_FILE" ]; then 189 if ( umask 022 && :> "$_debug_file" ); then 190 f_dprintf "Successfully initialized debugFile \`%s'" \ 191 "$_debug_file" 192 f_isset debug || debug=1 # turn debugging on if not set 193 else 194 unset debugFile 195 f_dprintf "Unable to initialize debugFile \`%s'" \ 196 "$_debug_file" 197 fi 198 fi 199} 200 201# f_err $format [$arguments ...] 202# 203# Print a message to stderr (fd=2). 204# 205f_err() 206{ 207 printf "$@" >&2 208} 209 210# f_quietly $command [$arguments ...] 211# 212# Run a command quietly (quell any output to stdout or stderr) 213# 214f_quietly() 215{ 216 "$@" > /dev/null 2>&1 217} 218 219# f_have $anything ... 220# 221# A wrapper to the `type' built-in. Returns true if argument is a valid shell 222# built-in, keyword, or externally-tracked binary, otherwise false. 223# 224f_have() 225{ 226 f_quietly type "$@" 227} 228 229# f_which $anything [$var_to_set] 230# 231# A fast built-in replacement for syntaxes such as foo=$( which bar ). In a 232# comparison of 10,000 runs of this function versus which, this function 233# completed in under 3 seconds, while `which' took almost a full minute. 234# 235# If $var_to_set is missing or NULL, output is (like which) to standard out. 236# Returns success if a match was found, failure otherwise. 237# 238f_which() 239{ 240 local __name="$1" __var_to_set="$2" 241 case "$__name" in */*|'') return $FAILURE; esac 242 local __p IFS=":" __found= 243 for __p in $PATH; do 244 local __exec="$__p/$__name" 245 [ -f "$__exec" -a -x "$__exec" ] && __found=1 && break 246 done 247 if [ "$__found" ]; then 248 if [ "$__var_to_set" ]; then 249 setvar "$__var_to_set" "$__exec" 250 else 251 echo "$__exec" 252 fi 253 return $SUCCESS 254 fi 255 return $FAILURE 256} 257 258# f_getvar $var_to_get [$var_to_set] 259# 260# Utility function designed to go along with the already-builtin setvar. 261# Allows clean variable name indirection without forking or sub-shells. 262# 263# Returns error status if the requested variable ($var_to_get) is not set. 264# 265# If $var_to_set is missing or NULL, the value of $var_to_get is printed to 266# standard output for capturing in a sub-shell (which is less-recommended 267# because of performance degredation; for example, when called in a loop). 268# 269f_getvar() 270{ 271 local __var_to_get="$1" __var_to_set="$2" 272 [ "$__var_to_set" ] || local value 273 eval [ \"\${$__var_to_get+set}\" ] 274 local __retval=$? 275 eval ${__var_to_set:-value}=\"\${$__var_to_get}\" 276 eval f_dprintf '"f_getvar: var=[%s] value=[%s] r=%u"' \ 277 \"\$__var_to_get\" \"\$${__var_to_set:-value}\" \$__retval 278 [ "$__var_to_set" ] || { [ "$value" ] && echo "$value"; } 279 return $__retval 280} 281 282# f_isset $var 283# 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 292# f_die [$status [$format [$arguments ...]]] 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 301 # If there is at least one argument, take it as the status 302 if [ $# -gt 0 ]; then 303 status=$1 304 shift 1 # status 305 fi 306 307 # If there are still arguments left, pass them to f_show_msg 308 [ $# -gt 0 ] && f_show_msg "$@" 309 310 # Optionally call f_clean_up() function if it exists 311 f_have f_clean_up && f_clean_up 312 313 exit $status 314} 315 316# f_interrupt 317# 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 326# f_show_info $format [$arguments ...] 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 335 # 336 # Use f_dialog_infobox from dialog.subr if possible, otherwise fall 337 # back to dialog(1) (without options, making it obvious when using 338 # un-aided system dialog). 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 347# f_show_msg $format [$arguments ...] 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 356 # 357 # Use f_dialog_msgbox from dialog.subr if possible, otherwise fall 358 # back to dialog(1) (without options, making it obvious when using 359 # un-aided system dialog). 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. 372# 373f_show_err() 374{ 375 local msg 376 msg=$( printf "$@" ) 377 378 : ${msg:=${msg_an_unknown_error_occurred:-An unknown error occurred}} 379 380 if [ "$_DIALOG_SUBR" ]; then 381 f_dialog_title "${msg_error:-Error}" 382 f_dialog_msgbox "$msg" 383 f_dialog_title_restore 384 else 385 dialog --title "${msg_error:-Error}" --msgbox "$msg" 0 0 386 fi 387 return $SUCCESS 388} 389 390# f_yesno $format [$arguments ...] 391# 392# Display a message in a dialog yes/no box using printf(1) syntax. 393# 394f_yesno() 395{ 396 local msg 397 msg=$( printf "$@" ) 398 399 # 400 # Use f_dialog_yesno from dialog.subr if possible, otherwise fall 401 # back to dialog(1) (without options, making it obvious when using 402 # un-aided system dialog). 403 # 404 if f_have f_dialog_yesno; then 405 f_dialog_yesno "$msg" 406 else 407 dialog --yesno "$msg" 0 0 408 fi 409} 410 411# f_noyes $format [$arguments ...] 412# 413# Display a message in a dialog yes/no box using printf(1) syntax. 414# NOTE: THis is just like the f_yesno function except "No" is default. 415# 416f_noyes() 417{ 418 local msg 419 msg=$( printf "$@" ) 420 421 # 422 # Use f_dialog_noyes from dialog.subr if possible, otherwise fall 423 # back to dialog(1) (without options, making it obvious when using 424 # un-aided system dialog). 425 # 426 if f_have f_dialog_noyes; then 427 f_dialog_noyes "$msg" 428 else 429 dialog --defaultno --yesno "$msg" 0 0 430 fi 431} 432 433# f_show_help $file 434# 435# Display a language help-file. Automatically takes $LANG and $LC_ALL into 436# consideration when displaying $file (suffix ".$LC_ALL" or ".$LANG" will 437# automatically be added prior to loading the language help-file). 438# 439# If a language has been requested by setting either $LANG or $LC_ALL in the 440# environment and the language-specific help-file does not exist we will fall 441# back to $file without-suffix. 442# 443# If the language help-file does not exist, an error is displayed instead. 444# 445f_show_help() 446{ 447 local file="$1" 448 local lang="${LANG:-$LC_ALL}" 449 450 [ -f "$file.$lang" ] && file="$file.$lang" 451 452 # 453 # Use f_dialog_textbox from dialog.subr if possible, otherwise fall 454 # back to dialog(1) (without options, making it obvious when using 455 # un-aided system dialog). 456 # 457 if f_have f_dialog_textbox; then 458 f_dialog_textbox "$file" 459 else 460 dialog --msgbox "$( cat "$file" 2>&1 )" 0 0 461 fi 462} 463 464# f_include $file 465# 466# Include a shell subroutine file. 467# 468# If the subroutine file exists but returns error status during loading, exit 469# is called and execution is prematurely terminated with the same error status. 470# 471f_include() 472{ 473 local file="$1" 474 f_dprintf "f_include: file=[%s]" "$file" 475 . "$file" || exit $? 476} 477 478# f_include_lang $file 479# 480# Include a language file. Automatically takes $LANG and $LC_ALL into 481# consideration when including $file (suffix ".$LC_ALL" or ".$LANG" will 482# automatically by added prior to loading the language file). 483# 484# No error is produced if (a) a language has been requested (by setting either 485# $LANG or $LC_ALL in the environment) and (b) the language file does not 486# exist -- in which case we will fall back to loading $file without-suffix. 487# 488# If the language file exists but returns error status during loading, exit 489# is called and execution is prematurely terminated with the same error status. 490# 491f_include_lang() 492{ 493 local file="$1" 494 local lang="${LANG:-$LC_ALL}" 495 496 f_dprintf "f_include_lang: file=[%s] lang=[%s]" "$file" "$lang" 497 if [ -f "$file.$lang" ]; then 498 . "$file.$lang" || exit $? 499 else 500 . "$file" || exit $? 501 fi 502} 503 504# f_usage $file [$key1 $value1 ...] 505# 506# Display USAGE file with optional pre-processor macro definitions. The first 507# argument is the template file containing the usage text to be displayed. If 508# $LANG or $LC_ALL (in order of preference, respectively) is set, ".encoding" 509# will automatically be appended as a suffix to the provided $file pathname. 510# 511# When processing $file, output begins at the first line containing that is 512# (a) not a comment, (b) not empty, and (c) is not pure-whitespace. All lines 513# appearing after this first-line are output, including (a) comments (b) empty 514# lines, and (c) lines that are purely whitespace-only. 515# 516# If additional arguments appear after $file, substitutions are made while 517# printing the contents of the USAGE file. The pre-processor macro syntax is in 518# the style of autoconf(1), for example: 519# 520# f_usage $file "FOO" "BAR" 521# 522# Will cause instances of "@FOO@" appearing in $file to be replaced with the 523# text "BAR" before being printed to the screen. 524# 525# This function is a two-parter. Below is the awk(1) portion of the function, 526# afterward is the sh(1) function which utilizes the below awk script. 527# 528f_usage_awk=' 529BEGIN { found = 0 } 530{ 531 if ( !found && $0 ~ /^[[:space:]]*($|#)/ ) next 532 found = 1 533 print 534} 535' 536f_usage() 537{ 538 local file="$1" 539 local lang="${LANG:-$LC_ALL}" 540 541 f_dprintf "f_usage: file=[%s] lang=[%s]" "$file" "$lang" 542 543 shift 1 # file 544 545 local usage 546 if [ -f "$file.$lang" ]; then 547 usage=$( awk "$f_usage_awk" "$file.$lang" ) || exit $FAILURE 548 else 549 usage=$( awk "$f_usage_awk" "$file" ) || exit $FAILURE 550 fi 551 552 while [ $# -gt 0 ]; do 553 local key="$1" 554 export value="$2" 555 usage=$( echo "$usage" | awk \ 556 "{ gsub(/@$key@/, ENVIRON[\"value\"]); print }" ) 557 shift 2 558 done 559 560 f_err "%s\n" "$usage" 561 562 exit $FAILURE 563} 564 565# f_index_file $keyword [$var_to_set] 566# 567# Process all INDEX files known to bsdconfig and return the path to first file 568# containing a menu_selection line with a keyword portion matching $keyword. 569# 570# If $LANG or $LC_ALL (in order of preference, respectively) is set, 571# "INDEX.encoding" files will be searched first. 572# 573# If no file is found, error status is returned along with the NULL string. 574# 575# If $var_to_set is NULL or missing, output is printed to stdout (which is less 576# recommended due to performance degradation; in a loop for example). 577# 578# This function is a two-parter. Below is the awk(1) portion of the function, 579# afterward is the sh(1) function which utilizes the below awk script. 580# 581f_index_file_awk=' 582# Variables that should be defined on the invocation line: 583# -v keyword="keyword" 584BEGIN { found = 0 } 585( $0 ~ "^menu_selection=\"" keyword "\\|" ) { 586 print FILENAME 587 found++ 588 exit 589} 590END { exit ! found } 591' 592f_index_file() 593{ 594 local __keyword="$1" __var_to_set="$2" 595 local __lang="${LANG:-$LC_ALL}" 596 local __indexes="$BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX" 597 598 f_dprintf "f_index_file: keyword=[%s] lang=[%s]" "$__keyword" "$__lang" 599 600 if [ "$__lang" ]; then 601 if [ "$__var_to_set" ]; then 602 eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ 603 "$f_index_file_awk" $__indexes.$__lang 604 )"' && return $SUCCESS 605 else 606 awk -v keyword="$__keyword" "$f_index_file_awk" \ 607 $__indexes.$__lang && return $SUCCESS 608 fi 609 # No match, fall-thru to non-i18n sources 610 fi 611 if [ "$__var_to_set" ]; then 612 eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ 613 "$f_index_file_awk" $__indexes )"' && return $SUCCESS 614 else 615 awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes && 616 return $SUCCESS 617 fi 618 619 # No match? Fall-thru to `local' libexec sources (add-on modules) 620 621 [ "$BSDCFG_LOCAL_LIBE" ] || return $FAILURE 622 __indexes="$BSDCFG_LOCAL_LIBE/*/INDEX" 623 if [ "$__lang" ]; then 624 if [ "$__var_to_set" ]; then 625 eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ 626 "$f_index_file_awk" $__indexes.$__lang 627 )"' && return $SUCCESS 628 else 629 awk -v keyword="$__keyword" "$f_index_file_awk" \ 630 $__indexes.$__lang && return $SUCCESS 631 fi 632 # No match, fall-thru to non-i18n sources 633 fi 634 if [ "$__var_to_set" ]; then 635 eval "$__var_to_set"='$( awk -v keyword="$__keyword" \ 636 "$f_index_file_awk" $__indexes )"' 637 else 638 awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes 639 fi 640} 641 642# f_index_menusel_keyword $indexfile $pgm [$var_to_set] 643# 644# Process $indexfile and return only the keyword portion of the menu_selection 645# line with a command portion matching $pgm. 646# 647# This function is for internationalization (i18n) mapping of the on-disk 648# scriptname ($pgm) into the localized language (given language-specific 649# $indexfile). If $LANG or $LC_ALL (in orderder of preference, respectively) is 650# set, ".encoding" will automatically be appended as a suffix to the provided 651# $indexfile pathname. 652# 653# If, within $indexfile, multiple $menu_selection values map to $pgm, only the 654# first one will be returned. If no mapping can be made, the NULL string is 655# returned. 656# 657# If $indexfile does not exist, error status is returned with NULL. 658# 659# If $var_to_set is NULL or missing, output is printed to stdout (which is less 660# recommended due to performance degradation; in a loop for example). 661# 662# This function is a two-parter. Below is the awk(1) portion of the function, 663# afterward is the sh(1) function which utilizes the below awk script. 664# 665f_index_menusel_keyword_awk=' 666# Variables that should be defined on the invocation line: 667# -v pgm="program_name" 668# 669BEGIN { 670 prefix = "menu_selection=\"" 671 plen = length(prefix) 672 found = 0 673} 674{ 675 if (!match($0, "^" prefix ".*\\|.*\"")) next 676 677 keyword = command = substr($0, plen + 1, RLENGTH - plen - 1) 678 sub(/^.*\|/, "", command) 679 sub(/\|.*$/, "", keyword) 680 681 if ( command == pgm ) 682 { 683 print keyword 684 found++ 685 exit 686 } 687} 688END { exit ! found } 689' 690f_index_menusel_keyword() 691{ 692 local __indexfile="$1" __pgm="$2" __var_to_set="$3" 693 local __lang="${LANG:-$LC_ALL}" __file="$__indexfile" 694 695 [ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang" 696 f_dprintf "f_index_menusel_keyword: index=[%s] pgm=[%s] lang=[%s]" \ 697 "$__file" "$__pgm" "$__lang" 698 699 if [ "$__var_to_set" ]; then 700 setvar "$__var_to_set" "$( awk \ 701 -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file" 702 )" 703 else 704 awk -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file" 705 fi 706} 707 708# f_index_menusel_command $indexfile $keyword [$var_to_set] 709# 710# Process $indexfile and return only the command portion of the menu_selection 711# line with a keyword portion matching $keyword. 712# 713# This function is for mapping [possibly international] keywords into the 714# command to be executed. If $LANG or $LC_ALL (order of preference) is set, 715# ".encoding" will automatically be appended as a suffix to the provided 716# $indexfile pathname. 717# 718# If, within $indexfile, multiple $menu_selection values map to $keyword, only 719# the first one will be returned. If no mapping can be made, the NULL string is 720# returned. 721# 722# If $indexfile doesn't exist, error status is returned with NULL. 723# 724# If $var_to_set is NULL or missing, output is printed to stdout (which is less 725# recommended due to performance degradation; in a loop for example). 726# 727# This function is a two-parter. Below is the awk(1) portion of the function, 728# afterward is the sh(1) function which utilizes the below awk script. 729# 730f_index_menusel_command_awk=' 731# Variables that should be defined on the invocation line: 732# -v key="keyword" 733# 734BEGIN { 735 prefix = "menu_selection=\"" 736 plen = length(prefix) 737 found = 0 738} 739{ 740 if (!match($0, "^" prefix ".*\\|.*\"")) next 741 742 keyword = command = substr($0, plen + 1, RLENGTH - plen - 1) 743 sub(/^.*\|/, "", command) 744 sub(/\|.*$/, "", keyword) 745 746 if ( keyword == key ) 747 { 748 print command 749 found++ 750 exit 751 } 752} 753END { exit ! found } 754' 755f_index_menusel_command() 756{ 757 local __indexfile="$1" __keyword="$2" __var_to_set="$3" __command 758 local __lang="${LANG:-$LC_ALL}" __file="$__indexfile" 759 760 [ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang" 761 f_dprintf "f_index_menusel_command: index=[%s] key=[%s] lang=[%s]" \ 762 "$__file" "$__keyword" "$__lang" 763 764 [ -f "$__file" ] || return $FAILURE 765 __command=$( awk -v key="$__keyword" \ 766 "$f_index_menusel_command_awk" "$__file" ) || return $FAILURE 767 768 # 769 # If the command pathname is not fully qualified fix-up/force to be 770 # relative to the $indexfile directory. 771 # 772 case "$__command" in 773 /*) : already fully qualified ;; 774 *) 775 local __indexdir="${__indexfile%/*}" 776 [ "$__indexdir" != "$__indexfile" ] || __indexdir="." 777 __command="$__indexdir/$__command" 778 esac 779 780 if [ "$__var_to_set" ]; then 781 setvar "$__var_to_set" "$__command" 782 else 783 echo "$__command" 784 fi 785} 786 787# f_running_as_init 788# 789# Returns true if running as init(1). 790# 791f_running_as_init() 792{ 793 # 794 # When a custom init(8) performs an exec(3) to invoke a shell script, 795 # PID 1 becomes sh(1) and $PPID is set to 1 in the executed script. 796 # 797 [ ${PPID:-0} -eq 1 ] # Return status 798} 799 800# f_mounted $local_directory
| 157 f_dprintf "f_debug_init: ARGV=[%s] GETOPTS_STDARGS=[%s]" \ 158 "$ARGV" "$GETOPTS_STDARGS" 159 while getopts "$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" flag \ 160 > /dev/null; do 161 case "$flag" in 162 d) debug=1 ;; 163 D) debugFile="$OPTARG" ;; 164 esac 165 done 166 shift $(( $OPTIND - 1 )) 167 f_dprintf "f_debug_init: debug=[%s] debugFile=[%s]" \ 168 "$debug" "$debugFile" 169 170 # 171 # Automagically enable debugging if debugFile is set (and non-NULL) 172 # 173 [ "$debugFile" ] && { [ "${debug+set}" ] || debug=1; } 174 175 # 176 # Make debugging persistant if set 177 # 178 [ "$debug" ] && export debug 179 [ "$debugFile" ] && export debugFile 180 181 # 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). 186 # 187 local _debug_file="${debugFile#+}" 188 if [ "$_debug_file" -a "$DEBUG_INITIALIZE_FILE" ]; then 189 if ( umask 022 && :> "$_debug_file" ); then 190 f_dprintf "Successfully initialized debugFile \`%s'" \ 191 "$_debug_file" 192 f_isset debug || debug=1 # turn debugging on if not set 193 else 194 unset debugFile 195 f_dprintf "Unable to initialize debugFile \`%s'" \ 196 "$_debug_file" 197 fi 198 fi 199} 200 201# f_err $format [$arguments ...] 202# 203# Print a message to stderr (fd=2). 204# 205f_err() 206{ 207 printf "$@" >&2 208} 209 210# f_quietly $command [$arguments ...] 211# 212# Run a command quietly (quell any output to stdout or stderr) 213# 214f_quietly() 215{ 216 "$@" > /dev/null 2>&1 217} 218 219# f_have $anything ... 220# 221# A wrapper to the `type' built-in. Returns true if argument is a valid shell 222# built-in, keyword, or externally-tracked binary, otherwise false. 223# 224f_have() 225{ 226 f_quietly type "$@" 227} 228 229# f_which $anything [$var_to_set] 230# 231# A fast built-in replacement for syntaxes such as foo=$( which bar ). In a 232# comparison of 10,000 runs of this function versus which, this function 233# completed in under 3 seconds, while `which' took almost a full minute. 234# 235# If $var_to_set is missing or NULL, output is (like which) to standard out. 236# Returns success if a match was found, failure otherwise. 237# 238f_which() 239{ 240 local __name="$1" __var_to_set="$2" 241 case "$__name" in */*|'') return $FAILURE; esac 242 local __p IFS=":" __found= 243 for __p in $PATH; do 244 local __exec="$__p/$__name" 245 [ -f "$__exec" -a -x "$__exec" ] && __found=1 && break 246 done 247 if [ "$__found" ]; then 248 if [ "$__var_to_set" ]; then 249 setvar "$__var_to_set" "$__exec" 250 else 251 echo "$__exec" 252 fi 253 return $SUCCESS 254 fi 255 return $FAILURE 256} 257 258# f_getvar $var_to_get [$var_to_set] 259# 260# Utility function designed to go along with the already-builtin setvar. 261# Allows clean variable name indirection without forking or sub-shells. 262# 263# Returns error status if the requested variable ($var_to_get) is not set. 264# 265# If $var_to_set is missing or NULL, the value of $var_to_get is printed to 266# standard output for capturing in a sub-shell (which is less-recommended 267# because of performance degredation; for example, when called in a loop). 268# 269f_getvar() 270{ 271 local __var_to_get="$1" __var_to_set="$2" 272 [ "$__var_to_set" ] || local value 273 eval [ \"\${$__var_to_get+set}\" ] 274 local __retval=$? 275 eval ${__var_to_set:-value}=\"\${$__var_to_get}\" 276 eval f_dprintf '"f_getvar: var=[%s] value=[%s] r=%u"' \ 277 \"\$__var_to_get\" \"\$${__var_to_set:-value}\" \$__retval 278 [ "$__var_to_set" ] || { [ "$value" ] && echo "$value"; } 279 return $__retval 280} 281 282# f_isset $var 283# 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 292# f_die [$status [$format [$arguments ...]]] 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 301 # If there is at least one argument, take it as the status 302 if [ $# -gt 0 ]; then 303 status=$1 304 shift 1 # status 305 fi 306 307 # If there are still arguments left, pass them to f_show_msg 308 [ $# -gt 0 ] && f_show_msg "$@" 309 310 # Optionally call f_clean_up() function if it exists 311 f_have f_clean_up && f_clean_up 312 313 exit $status 314} 315 316# f_interrupt 317# 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 326# f_show_info $format [$arguments ...] 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 335 # 336 # Use f_dialog_infobox from dialog.subr if possible, otherwise fall 337 # back to dialog(1) (without options, making it obvious when using 338 # un-aided system dialog). 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 347# f_show_msg $format [$arguments ...] 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 356 # 357 # Use f_dialog_msgbox from dialog.subr if possible, otherwise fall 358 # back to dialog(1) (without options, making it obvious when using 359 # un-aided system dialog). 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. 372# 373f_show_err() 374{ 375 local msg 376 msg=$( printf "$@" ) 377 378 : ${msg:=${msg_an_unknown_error_occurred:-An unknown error occurred}} 379 380 if [ "$_DIALOG_SUBR" ]; then 381 f_dialog_title "${msg_error:-Error}" 382 f_dialog_msgbox "$msg" 383 f_dialog_title_restore 384 else 385 dialog --title "${msg_error:-Error}" --msgbox "$msg" 0 0 386 fi 387 return $SUCCESS 388} 389 390# f_yesno $format [$arguments ...] 391# 392# Display a message in a dialog yes/no box using printf(1) syntax. 393# 394f_yesno() 395{ 396 local msg 397 msg=$( printf "$@" ) 398 399 # 400 # Use f_dialog_yesno from dialog.subr if possible, otherwise fall 401 # back to dialog(1) (without options, making it obvious when using 402 # un-aided system dialog). 403 # 404 if f_have f_dialog_yesno; then 405 f_dialog_yesno "$msg" 406 else 407 dialog --yesno "$msg" 0 0 408 fi 409} 410 411# f_noyes $format [$arguments ...] 412# 413# Display a message in a dialog yes/no box using printf(1) syntax. 414# NOTE: THis is just like the f_yesno function except "No" is default. 415# 416f_noyes() 417{ 418 local msg 419 msg=$( printf "$@" ) 420 421 # 422 # Use f_dialog_noyes from dialog.subr if possible, otherwise fall 423 # back to dialog(1) (without options, making it obvious when using 424 # un-aided system dialog). 425 # 426 if f_have f_dialog_noyes; then 427 f_dialog_noyes "$msg" 428 else 429 dialog --defaultno --yesno "$msg" 0 0 430 fi 431} 432 433# f_show_help $file 434# 435# Display a language help-file. Automatically takes $LANG and $LC_ALL into 436# consideration when displaying $file (suffix ".$LC_ALL" or ".$LANG" will 437# automatically be added prior to loading the language help-file). 438# 439# If a language has been requested by setting either $LANG or $LC_ALL in the 440# environment and the language-specific help-file does not exist we will fall 441# back to $file without-suffix. 442# 443# If the language help-file does not exist, an error is displayed instead. 444# 445f_show_help() 446{ 447 local file="$1" 448 local lang="${LANG:-$LC_ALL}" 449 450 [ -f "$file.$lang" ] && file="$file.$lang" 451 452 # 453 # Use f_dialog_textbox from dialog.subr if possible, otherwise fall 454 # back to dialog(1) (without options, making it obvious when using 455 # un-aided system dialog). 456 # 457 if f_have f_dialog_textbox; then 458 f_dialog_textbox "$file" 459 else 460 dialog --msgbox "$( cat "$file" 2>&1 )" 0 0 461 fi 462} 463 464# f_include $file 465# 466# Include a shell subroutine file. 467# 468# If the subroutine file exists but returns error status during loading, exit 469# is called and execution is prematurely terminated with the same error status. 470# 471f_include() 472{ 473 local file="$1" 474 f_dprintf "f_include: file=[%s]" "$file" 475 . "$file" || exit $? 476} 477 478# f_include_lang $file 479# 480# Include a language file. Automatically takes $LANG and $LC_ALL into 481# consideration when including $file (suffix ".$LC_ALL" or ".$LANG" will 482# automatically by added prior to loading the language file). 483# 484# No error is produced if (a) a language has been requested (by setting either 485# $LANG or $LC_ALL in the environment) and (b) the language file does not 486# exist -- in which case we will fall back to loading $file without-suffix. 487# 488# If the language file exists but returns error status during loading, exit 489# is called and execution is prematurely terminated with the same error status. 490# 491f_include_lang() 492{ 493 local file="$1" 494 local lang="${LANG:-$LC_ALL}" 495 496 f_dprintf "f_include_lang: file=[%s] lang=[%s]" "$file" "$lang" 497 if [ -f "$file.$lang" ]; then 498 . "$file.$lang" || exit $? 499 else 500 . "$file" || exit $? 501 fi 502} 503 504# f_usage $file [$key1 $value1 ...] 505# 506# Display USAGE file with optional pre-processor macro definitions. The first 507# argument is the template file containing the usage text to be displayed. If 508# $LANG or $LC_ALL (in order of preference, respectively) is set, ".encoding" 509# will automatically be appended as a suffix to the provided $file pathname. 510# 511# When processing $file, output begins at the first line containing that is 512# (a) not a comment, (b) not empty, and (c) is not pure-whitespace. All lines 513# appearing after this first-line are output, including (a) comments (b) empty 514# lines, and (c) lines that are purely whitespace-only. 515# 516# If additional arguments appear after $file, substitutions are made while 517# printing the contents of the USAGE file. The pre-processor macro syntax is in 518# the style of autoconf(1), for example: 519# 520# f_usage $file "FOO" "BAR" 521# 522# Will cause instances of "@FOO@" appearing in $file to be replaced with the 523# text "BAR" before being printed to the screen. 524# 525# This function is a two-parter. Below is the awk(1) portion of the function, 526# afterward is the sh(1) function which utilizes the below awk script. 527# 528f_usage_awk=' 529BEGIN { found = 0 } 530{ 531 if ( !found && $0 ~ /^[[:space:]]*($|#)/ ) next 532 found = 1 533 print 534} 535' 536f_usage() 537{ 538 local file="$1" 539 local lang="${LANG:-$LC_ALL}" 540 541 f_dprintf "f_usage: file=[%s] lang=[%s]" "$file" "$lang" 542 543 shift 1 # file 544 545 local usage 546 if [ -f "$file.$lang" ]; then 547 usage=$( awk "$f_usage_awk" "$file.$lang" ) || exit $FAILURE 548 else 549 usage=$( awk "$f_usage_awk" "$file" ) || exit $FAILURE 550 fi 551 552 while [ $# -gt 0 ]; do 553 local key="$1" 554 export value="$2" 555 usage=$( echo "$usage" | awk \ 556 "{ gsub(/@$key@/, ENVIRON[\"value\"]); print }" ) 557 shift 2 558 done 559 560 f_err "%s\n" "$usage" 561 562 exit $FAILURE 563} 564 565# f_index_file $keyword [$var_to_set] 566# 567# Process all INDEX files known to bsdconfig and return the path to first file 568# containing a menu_selection line with a keyword portion matching $keyword. 569# 570# If $LANG or $LC_ALL (in order of preference, respectively) is set, 571# "INDEX.encoding" files will be searched first. 572# 573# If no file is found, error status is returned along with the NULL string. 574# 575# If $var_to_set is NULL or missing, output is printed to stdout (which is less 576# recommended due to performance degradation; in a loop for example). 577# 578# This function is a two-parter. Below is the awk(1) portion of the function, 579# afterward is the sh(1) function which utilizes the below awk script. 580# 581f_index_file_awk=' 582# Variables that should be defined on the invocation line: 583# -v keyword="keyword" 584BEGIN { found = 0 } 585( $0 ~ "^menu_selection=\"" keyword "\\|" ) { 586 print FILENAME 587 found++ 588 exit 589} 590END { exit ! found } 591' 592f_index_file() 593{ 594 local __keyword="$1" __var_to_set="$2" 595 local __lang="${LANG:-$LC_ALL}" 596 local __indexes="$BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX" 597 598 f_dprintf "f_index_file: keyword=[%s] lang=[%s]" "$__keyword" "$__lang" 599 600 if [ "$__lang" ]; then 601 if [ "$__var_to_set" ]; then 602 eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ 603 "$f_index_file_awk" $__indexes.$__lang 604 )"' && return $SUCCESS 605 else 606 awk -v keyword="$__keyword" "$f_index_file_awk" \ 607 $__indexes.$__lang && return $SUCCESS 608 fi 609 # No match, fall-thru to non-i18n sources 610 fi 611 if [ "$__var_to_set" ]; then 612 eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ 613 "$f_index_file_awk" $__indexes )"' && return $SUCCESS 614 else 615 awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes && 616 return $SUCCESS 617 fi 618 619 # No match? Fall-thru to `local' libexec sources (add-on modules) 620 621 [ "$BSDCFG_LOCAL_LIBE" ] || return $FAILURE 622 __indexes="$BSDCFG_LOCAL_LIBE/*/INDEX" 623 if [ "$__lang" ]; then 624 if [ "$__var_to_set" ]; then 625 eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ 626 "$f_index_file_awk" $__indexes.$__lang 627 )"' && return $SUCCESS 628 else 629 awk -v keyword="$__keyword" "$f_index_file_awk" \ 630 $__indexes.$__lang && return $SUCCESS 631 fi 632 # No match, fall-thru to non-i18n sources 633 fi 634 if [ "$__var_to_set" ]; then 635 eval "$__var_to_set"='$( awk -v keyword="$__keyword" \ 636 "$f_index_file_awk" $__indexes )"' 637 else 638 awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes 639 fi 640} 641 642# f_index_menusel_keyword $indexfile $pgm [$var_to_set] 643# 644# Process $indexfile and return only the keyword portion of the menu_selection 645# line with a command portion matching $pgm. 646# 647# This function is for internationalization (i18n) mapping of the on-disk 648# scriptname ($pgm) into the localized language (given language-specific 649# $indexfile). If $LANG or $LC_ALL (in orderder of preference, respectively) is 650# set, ".encoding" will automatically be appended as a suffix to the provided 651# $indexfile pathname. 652# 653# If, within $indexfile, multiple $menu_selection values map to $pgm, only the 654# first one will be returned. If no mapping can be made, the NULL string is 655# returned. 656# 657# If $indexfile does not exist, error status is returned with NULL. 658# 659# If $var_to_set is NULL or missing, output is printed to stdout (which is less 660# recommended due to performance degradation; in a loop for example). 661# 662# This function is a two-parter. Below is the awk(1) portion of the function, 663# afterward is the sh(1) function which utilizes the below awk script. 664# 665f_index_menusel_keyword_awk=' 666# Variables that should be defined on the invocation line: 667# -v pgm="program_name" 668# 669BEGIN { 670 prefix = "menu_selection=\"" 671 plen = length(prefix) 672 found = 0 673} 674{ 675 if (!match($0, "^" prefix ".*\\|.*\"")) next 676 677 keyword = command = substr($0, plen + 1, RLENGTH - plen - 1) 678 sub(/^.*\|/, "", command) 679 sub(/\|.*$/, "", keyword) 680 681 if ( command == pgm ) 682 { 683 print keyword 684 found++ 685 exit 686 } 687} 688END { exit ! found } 689' 690f_index_menusel_keyword() 691{ 692 local __indexfile="$1" __pgm="$2" __var_to_set="$3" 693 local __lang="${LANG:-$LC_ALL}" __file="$__indexfile" 694 695 [ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang" 696 f_dprintf "f_index_menusel_keyword: index=[%s] pgm=[%s] lang=[%s]" \ 697 "$__file" "$__pgm" "$__lang" 698 699 if [ "$__var_to_set" ]; then 700 setvar "$__var_to_set" "$( awk \ 701 -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file" 702 )" 703 else 704 awk -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file" 705 fi 706} 707 708# f_index_menusel_command $indexfile $keyword [$var_to_set] 709# 710# Process $indexfile and return only the command portion of the menu_selection 711# line with a keyword portion matching $keyword. 712# 713# This function is for mapping [possibly international] keywords into the 714# command to be executed. If $LANG or $LC_ALL (order of preference) is set, 715# ".encoding" will automatically be appended as a suffix to the provided 716# $indexfile pathname. 717# 718# If, within $indexfile, multiple $menu_selection values map to $keyword, only 719# the first one will be returned. If no mapping can be made, the NULL string is 720# returned. 721# 722# If $indexfile doesn't exist, error status is returned with NULL. 723# 724# If $var_to_set is NULL or missing, output is printed to stdout (which is less 725# recommended due to performance degradation; in a loop for example). 726# 727# This function is a two-parter. Below is the awk(1) portion of the function, 728# afterward is the sh(1) function which utilizes the below awk script. 729# 730f_index_menusel_command_awk=' 731# Variables that should be defined on the invocation line: 732# -v key="keyword" 733# 734BEGIN { 735 prefix = "menu_selection=\"" 736 plen = length(prefix) 737 found = 0 738} 739{ 740 if (!match($0, "^" prefix ".*\\|.*\"")) next 741 742 keyword = command = substr($0, plen + 1, RLENGTH - plen - 1) 743 sub(/^.*\|/, "", command) 744 sub(/\|.*$/, "", keyword) 745 746 if ( keyword == key ) 747 { 748 print command 749 found++ 750 exit 751 } 752} 753END { exit ! found } 754' 755f_index_menusel_command() 756{ 757 local __indexfile="$1" __keyword="$2" __var_to_set="$3" __command 758 local __lang="${LANG:-$LC_ALL}" __file="$__indexfile" 759 760 [ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang" 761 f_dprintf "f_index_menusel_command: index=[%s] key=[%s] lang=[%s]" \ 762 "$__file" "$__keyword" "$__lang" 763 764 [ -f "$__file" ] || return $FAILURE 765 __command=$( awk -v key="$__keyword" \ 766 "$f_index_menusel_command_awk" "$__file" ) || return $FAILURE 767 768 # 769 # If the command pathname is not fully qualified fix-up/force to be 770 # relative to the $indexfile directory. 771 # 772 case "$__command" in 773 /*) : already fully qualified ;; 774 *) 775 local __indexdir="${__indexfile%/*}" 776 [ "$__indexdir" != "$__indexfile" ] || __indexdir="." 777 __command="$__indexdir/$__command" 778 esac 779 780 if [ "$__var_to_set" ]; then 781 setvar "$__var_to_set" "$__command" 782 else 783 echo "$__command" 784 fi 785} 786 787# f_running_as_init 788# 789# Returns true if running as init(1). 790# 791f_running_as_init() 792{ 793 # 794 # When a custom init(8) performs an exec(3) to invoke a shell script, 795 # PID 1 becomes sh(1) and $PPID is set to 1 in the executed script. 796 # 797 [ ${PPID:-0} -eq 1 ] # Return status 798} 799 800# f_mounted $local_directory
|