1if [ ! "$_COMMON_SUBR" ]; then _COMMON_SUBR=1 2# 3# Copyright (c) 2012 Ron McDowell 4# Copyright (c) 2012-2016 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# 28# $FreeBSD$ 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} 37: ${TERMINAL_STDERR_PASSTHRU:=4} 38 39# 40# Default OSNAME shown in the installer 41# 42: ${OSNAME:=FreeBSD} 43: ${EFI_LABEL_NAME:=FreeBSD} 44 45############################################################ GLOBALS 46 47# 48# Program name 49# 50pgm="${0##*/}" 51 52# 53# Program arguments 54# 55ARGC="$#" 56ARGV="$@" 57 58# 59# Global exit status variables 60# 61SUCCESS=0 62FAILURE=1 63 64# 65# Operating environment details 66# 67export UNAME_S="$( uname -s )" # Operating System (i.e. FreeBSD) 68export UNAME_P="$( uname -p )" # Processor Architecture (i.e. i386) 69export UNAME_M="$( uname -m )" # Machine platform (i.e. i386) 70export UNAME_R="$( uname -r )" # Release Level (i.e. X.Y-RELEASE) 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# 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:" 94 95# 96# The getopts builtin will return 1 either when the end of "$@" or the first 97# invalid flag is reached. This makes it impossible to determine if you've 98# processed all the arguments or simply have hit an invalid flag. In the cases 99# where we want to tolerate invalid flags (f_debug_init() for example), the 100# following variable can be appended to your optstring argument to getopts, 101# preventing it from prematurely returning 1 before the end of the arguments. 102# 103# NOTE: This assumes that all unknown flags are argument-less. 104# 105GETOPTS_ALLFLAGS="abcdefghijklmnopqrstuvwxyz" 106GETOPTS_ALLFLAGS="${GETOPTS_ALLFLAGS}ABCDEFGHIJKLMNOPQRSTUVWXYZ" 107GETOPTS_ALLFLAGS="${GETOPTS_ALLFLAGS}0123456789" 108 109# 110# When we get included, f_debug_init() will fire (unless $DEBUG_SELF_INITIALIZE 111# is set to disable automatic initialization) and process "$@" for a few global 112# options such as `-d' and/or `-D file'. However, if your program takes custom 113# flags that take arguments, this automatic processing may fail unexpectedly. 114# 115# The solution to this problem is to pre-define (before including this file) 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 124# f_dprintf $format [$arguments ...] 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 (`+') 133# 134f_dprintf() 135{ 136 [ "$debug" ] || return $SUCCESS 137 local fmt="$1"; shift 138 case "$debugFile" in ""|+*) 139 printf "DEBUG: $fmt${fmt:+\n}" "$@" >&${TERMINAL_STDOUT_PASSTHRU:-1} 140 esac 141 [ "${debugFile#+}" ] && 142 printf "DEBUG: $fmt${fmt:+\n}" "$@" >> "${debugFile#+}" 143 return $SUCCESS 144} 145 146# f_debug_init 147# 148# Initialize debugging. Truncates $debugFile to zero bytes if set. 149# 150f_debug_init() 151{ 152 # 153 # Process stored command-line arguments 154 # 155 set -- $ARGV 156 local OPTIND OPTARG flag 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 persistent 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 # persistent 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# setvar $var_to_set [$value] 230# 231# Implement setvar for shells unlike FreeBSD sh(1). 232# 233if ! f_have setvar; then 234setvar() 235{ 236 [ $# -gt 0 ] || return $SUCCESS 237 local __setvar_var_to_set="$1" __setvar_right="$2" __setvar_left= 238 case $# in 239 1) unset "$__setvar_var_to_set" 240 return $? ;; 241 2) : fall through ;; 242 *) f_err "setvar: too many arguments\n" 243 return $FAILURE 244 esac 245 case "$__setvar_var_to_set" in *[!0-9A-Za-z_]*) 246 f_err "setvar: %s: bad variable name\n" "$__setvar_var_to_set" 247 return 2 248 esac 249 while case "$__setvar_r" in *\'*) : ;; *) false ; esac 250 do 251 __setvar_left="$__setvar_left${__setvar_right%%\'*}'\\''" 252 __setvar_right="${__setvar_right#*\'}" 253 done 254 __setvar_left="$__setvar_left${__setvar_right#*\'}" 255 eval "$__setvar_var_to_set='$__setvar_left'" 256} 257fi 258 259# f_which $anything [$var_to_set] 260# 261# A fast built-in replacement for syntaxes such as foo=$( which bar ). In a 262# comparison of 10,000 runs of this function versus which, this function 263# completed in under 3 seconds, while `which' took almost a full minute. 264# 265# If $var_to_set is missing or NULL, output is (like which) to standard out. 266# Returns success if a match was found, failure otherwise. 267# 268f_which() 269{ 270 local __name="$1" __var_to_set="$2" 271 case "$__name" in */*|'') return $FAILURE; esac 272 local __p __exec IFS=":" __found= 273 for __p in $PATH; do 274 __exec="$__p/$__name" 275 [ -f "$__exec" -a -x "$__exec" ] && __found=1 break 276 done 277 if [ "$__found" ]; then 278 if [ "$__var_to_set" ]; then 279 setvar "$__var_to_set" "$__exec" 280 else 281 echo "$__exec" 282 fi 283 return $SUCCESS 284 fi 285 return $FAILURE 286} 287 288# f_getvar $var_to_get [$var_to_set] 289# 290# Utility function designed to go along with the already-builtin setvar. 291# Allows clean variable name indirection without forking or sub-shells. 292# 293# Returns error status if the requested variable ($var_to_get) is not set. 294# 295# If $var_to_set is missing or NULL, the value of $var_to_get is printed to 296# standard output for capturing in a sub-shell (which is less-recommended 297# because of performance degredation; for example, when called in a loop). 298# 299f_getvar() 300{ 301 local __var_to_get="$1" __var_to_set="$2" 302 [ "$__var_to_set" ] || local value 303 eval [ \"\${$__var_to_get+set}\" ] 304 local __retval=$? 305 eval ${__var_to_set:-value}=\"\${$__var_to_get}\" 306 eval f_dprintf '"f_getvar: var=[%s] value=[%s] r=%u"' \ 307 \"\$__var_to_get\" \"\$${__var_to_set:-value}\" \$__retval 308 [ "$__var_to_set" ] || { [ "$value" ] && echo "$value"; } 309 return $__retval 310} 311 312# f_isset $var 313# 314# Check if variable $var is set. Returns success if variable is set, otherwise 315# returns failure. 316# 317f_isset() 318{ 319 eval [ \"\${${1%%[$IFS]*}+set}\" ] 320} 321 322# f_die [$status [$format [$arguments ...]]] 323# 324# Abruptly terminate due to an error optionally displaying a message in a 325# dialog box using printf(1) syntax. 326# 327f_die() 328{ 329 local status=$FAILURE 330 331 # If there is at least one argument, take it as the status 332 if [ $# -gt 0 ]; then 333 status=$1 334 shift 1 # status 335 fi 336 337 # If there are still arguments left, pass them to f_show_msg 338 [ $# -gt 0 ] && f_show_msg "$@" 339 340 # Optionally call f_clean_up() function if it exists 341 f_have f_clean_up && f_clean_up 342 343 exit $status 344} 345 346# f_interrupt 347# 348# Interrupt handler. 349# 350f_interrupt() 351{ 352 exec 2>&1 # fix sh(1) bug where stderr gets lost within async-trap 353 f_die 354} 355 356# f_show_info $format [$arguments ...] 357# 358# Display a message in a dialog infobox using printf(1) syntax. 359# 360f_show_info() 361{ 362 local msg 363 msg=$( printf "$@" ) 364 365 # 366 # Use f_dialog_infobox from dialog.subr if possible, otherwise fall 367 # back to dialog(1) (without options, making it obvious when using 368 # un-aided system dialog). 369 # 370 if f_have f_dialog_info; then 371 f_dialog_info "$msg" 372 else 373 dialog --infobox "$msg" 0 0 374 fi 375} 376 377# f_show_msg $format [$arguments ...] 378# 379# Display a message in a dialog box using printf(1) syntax. 380# 381f_show_msg() 382{ 383 local msg 384 msg=$( printf "$@" ) 385 386 # 387 # Use f_dialog_msgbox from dialog.subr if possible, otherwise fall 388 # back to dialog(1) (without options, making it obvious when using 389 # un-aided system dialog). 390 # 391 if f_have f_dialog_msgbox; then 392 f_dialog_msgbox "$msg" 393 else 394 dialog --msgbox "$msg" 0 0 395 fi 396} 397 398# f_show_err $format [$arguments ...] 399# 400# Display a message in a dialog box with ``Error'' i18n title (overridden by 401# setting msg_error) using printf(1) syntax. 402# 403f_show_err() 404{ 405 local msg 406 msg=$( printf "$@" ) 407 408 : ${msg:=${msg_an_unknown_error_occurred:-An unknown error occurred}} 409 410 if [ "$_DIALOG_SUBR" ]; then 411 f_dialog_title "${msg_error:-Error}" 412 f_dialog_msgbox "$msg" 413 f_dialog_title_restore 414 else 415 dialog --title "${msg_error:-Error}" --msgbox "$msg" 0 0 416 fi 417 return $SUCCESS 418} 419 420# f_yesno $format [$arguments ...] 421# 422# Display a message in a dialog yes/no box using printf(1) syntax. 423# 424f_yesno() 425{ 426 local msg 427 msg=$( printf "$@" ) 428 429 # 430 # Use f_dialog_yesno from dialog.subr if possible, otherwise fall 431 # back to dialog(1) (without options, making it obvious when using 432 # un-aided system dialog). 433 # 434 if f_have f_dialog_yesno; then 435 f_dialog_yesno "$msg" 436 else 437 dialog --yesno "$msg" 0 0 438 fi 439} 440 441# f_noyes $format [$arguments ...] 442# 443# Display a message in a dialog yes/no box using printf(1) syntax. 444# NOTE: THis is just like the f_yesno function except "No" is default. 445# 446f_noyes() 447{ 448 local msg 449 msg=$( printf "$@" ) 450 451 # 452 # Use f_dialog_noyes from dialog.subr if possible, otherwise fall 453 # back to dialog(1) (without options, making it obvious when using 454 # un-aided system dialog). 455 # 456 if f_have f_dialog_noyes; then 457 f_dialog_noyes "$msg" 458 else 459 dialog --defaultno --yesno "$msg" 0 0 460 fi 461} 462 463# f_show_help $file 464# 465# Display a language help-file. Automatically takes $LANG and $LC_ALL into 466# consideration when displaying $file (suffix ".$LC_ALL" or ".$LANG" will 467# automatically be added prior to loading the language help-file). 468# 469# If a language has been requested by setting either $LANG or $LC_ALL in the 470# environment and the language-specific help-file does not exist we will fall 471# back to $file without-suffix. 472# 473# If the language help-file does not exist, an error is displayed instead. 474# 475f_show_help() 476{ 477 local file="$1" 478 local lang="${LANG:-$LC_ALL}" 479 480 [ -f "$file.$lang" ] && file="$file.$lang" 481 482 # 483 # Use f_dialog_textbox from dialog.subr if possible, otherwise fall 484 # back to dialog(1) (without options, making it obvious when using 485 # un-aided system dialog). 486 # 487 if f_have f_dialog_textbox; then 488 f_dialog_textbox "$file" 489 else 490 dialog --msgbox "$( cat "$file" 2>&1 )" 0 0 491 fi 492} 493 494# f_include $file 495# 496# Include a shell subroutine file. 497# 498# If the subroutine file exists but returns error status during loading, exit 499# is called and execution is prematurely terminated with the same error status. 500# 501f_include() 502{ 503 local file="$1" 504 f_dprintf "f_include: file=[%s]" "$file" 505 . "$file" || exit $? 506} 507 508# f_include_lang $file 509# 510# Include a language file. Automatically takes $LANG and $LC_ALL into 511# consideration when including $file (suffix ".$LC_ALL" or ".$LANG" will 512# automatically by added prior to loading the language file). 513# 514# No error is produced if (a) a language has been requested (by setting either 515# $LANG or $LC_ALL in the environment) and (b) the language file does not 516# exist -- in which case we will fall back to loading $file without-suffix. 517# 518# If the language file exists but returns error status during loading, exit 519# is called and execution is prematurely terminated with the same error status. 520# 521f_include_lang() 522{ 523 local file="$1" 524 local lang="${LANG:-$LC_ALL}" 525 526 f_dprintf "f_include_lang: file=[%s] lang=[%s]" "$file" "$lang" 527 if [ -f "$file.$lang" ]; then 528 . "$file.$lang" || exit $? 529 else 530 . "$file" || exit $? 531 fi 532} 533 534# f_usage $file [$key1 $value1 ...] 535# 536# Display USAGE file with optional pre-processor macro definitions. The first 537# argument is the template file containing the usage text to be displayed. If 538# $LANG or $LC_ALL (in order of preference, respectively) is set, ".encoding" 539# will automatically be appended as a suffix to the provided $file pathname. 540# 541# When processing $file, output begins at the first line containing that is 542# (a) not a comment, (b) not empty, and (c) is not pure-whitespace. All lines 543# appearing after this first-line are output, including (a) comments (b) empty 544# lines, and (c) lines that are purely whitespace-only. 545# 546# If additional arguments appear after $file, substitutions are made while 547# printing the contents of the USAGE file. The pre-processor macro syntax is in 548# the style of autoconf(1), for example: 549# 550# f_usage $file "FOO" "BAR" 551# 552# Will cause instances of "@FOO@" appearing in $file to be replaced with the 553# text "BAR" before being printed to the screen. 554# 555# This function is a two-parter. Below is the awk(1) portion of the function, 556# afterward is the sh(1) function which utilizes the below awk script. 557# 558f_usage_awk=' 559BEGIN { found = 0 } 560{ 561 if ( !found && $0 ~ /^[[:space:]]*($|#)/ ) next 562 found = 1 563 print 564} 565' 566f_usage() 567{ 568 local file="$1" 569 local lang="${LANG:-$LC_ALL}" 570 571 f_dprintf "f_usage: file=[%s] lang=[%s]" "$file" "$lang" 572 573 shift 1 # file 574 575 local usage 576 if [ -f "$file.$lang" ]; then 577 usage=$( awk "$f_usage_awk" "$file.$lang" ) || exit $FAILURE 578 else 579 usage=$( awk "$f_usage_awk" "$file" ) || exit $FAILURE 580 fi 581 582 while [ $# -gt 0 ]; do 583 local key="$1" 584 export value="$2" 585 usage=$( echo "$usage" | awk \ 586 "{ gsub(/@$key@/, ENVIRON[\"value\"]); print }" ) 587 shift 2 588 done 589 590 f_err "%s\n" "$usage" 591 592 exit $FAILURE 593} 594 595# f_index_file $keyword [$var_to_set] 596# 597# Process all INDEX files known to bsdconfig and return the path to first file 598# containing a menu_selection line with a keyword portion matching $keyword. 599# 600# If $LANG or $LC_ALL (in order of preference, respectively) is set, 601# "INDEX.encoding" files will be searched first. 602# 603# If no file is found, error status is returned along with the NULL string. 604# 605# If $var_to_set is NULL or missing, output is printed to stdout (which is less 606# recommended due to performance degradation; in a loop for example). 607# 608# This function is a two-parter. Below is the awk(1) portion of the function, 609# afterward is the sh(1) function which utilizes the below awk script. 610# 611f_index_file_awk=' 612# Variables that should be defined on the invocation line: 613# -v keyword="keyword" 614BEGIN { found = 0 } 615( $0 ~ "^menu_selection=\"" keyword "\\|" ) { 616 print FILENAME 617 found++ 618 exit 619} 620END { exit ! found } 621' 622f_index_file() 623{ 624 local __keyword="$1" __var_to_set="$2" 625 local __lang="${LANG:-$LC_ALL}" 626 local __indexes="$BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX" 627 628 f_dprintf "f_index_file: keyword=[%s] lang=[%s]" "$__keyword" "$__lang" 629 630 if [ "$__lang" ]; then 631 if [ "$__var_to_set" ]; then 632 eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ 633 "$f_index_file_awk" $__indexes.$__lang 634 )"' && return $SUCCESS 635 else 636 awk -v keyword="$__keyword" "$f_index_file_awk" \ 637 $__indexes.$__lang && return $SUCCESS 638 fi 639 # No match, fall-thru to non-i18n sources 640 fi 641 if [ "$__var_to_set" ]; then 642 eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ 643 "$f_index_file_awk" $__indexes )"' && return $SUCCESS 644 else 645 awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes && 646 return $SUCCESS 647 fi 648 649 # No match? Fall-thru to `local' libexec sources (add-on modules) 650 651 [ "$BSDCFG_LOCAL_LIBE" ] || return $FAILURE 652 __indexes="$BSDCFG_LOCAL_LIBE/*/INDEX" 653 if [ "$__lang" ]; then 654 if [ "$__var_to_set" ]; then 655 eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \ 656 "$f_index_file_awk" $__indexes.$__lang 657 )"' && return $SUCCESS 658 else 659 awk -v keyword="$__keyword" "$f_index_file_awk" \ 660 $__indexes.$__lang && return $SUCCESS 661 fi 662 # No match, fall-thru to non-i18n sources 663 fi 664 if [ "$__var_to_set" ]; then 665 eval "$__var_to_set"='$( awk -v keyword="$__keyword" \ 666 "$f_index_file_awk" $__indexes )"' 667 else 668 awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes 669 fi 670} 671 672# f_index_menusel_keyword $indexfile $pgm [$var_to_set] 673# 674# Process $indexfile and return only the keyword portion of the menu_selection 675# line with a command portion matching $pgm. 676# 677# This function is for internationalization (i18n) mapping of the on-disk 678# scriptname ($pgm) into the localized language (given language-specific 679# $indexfile). If $LANG or $LC_ALL (in orderder of preference, respectively) is 680# set, ".encoding" will automatically be appended as a suffix to the provided 681# $indexfile pathname. 682# 683# If, within $indexfile, multiple $menu_selection values map to $pgm, only the 684# first one will be returned. If no mapping can be made, the NULL string is 685# returned. 686# 687# If $indexfile does not exist, error status is returned with NULL. 688# 689# If $var_to_set is NULL or missing, output is printed to stdout (which is less 690# recommended due to performance degradation; in a loop for example). 691# 692# This function is a two-parter. Below is the awk(1) portion of the function, 693# afterward is the sh(1) function which utilizes the below awk script. 694# 695f_index_menusel_keyword_awk=' 696# Variables that should be defined on the invocation line: 697# -v pgm="program_name" 698# 699BEGIN { 700 prefix = "menu_selection=\"" 701 plen = length(prefix) 702 found = 0 703} 704{ 705 if (!match($0, "^" prefix ".*\\|.*\"")) next 706 707 keyword = command = substr($0, plen + 1, RLENGTH - plen - 1) 708 sub(/^.*\|/, "", command) 709 sub(/\|.*$/, "", keyword) 710 711 if ( command == pgm ) 712 { 713 print keyword 714 found++ 715 exit 716 } 717} 718END { exit ! found } 719' 720f_index_menusel_keyword() 721{ 722 local __indexfile="$1" __pgm="$2" __var_to_set="$3" 723 local __lang="${LANG:-$LC_ALL}" __file="$__indexfile" 724 725 [ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang" 726 f_dprintf "f_index_menusel_keyword: index=[%s] pgm=[%s] lang=[%s]" \ 727 "$__file" "$__pgm" "$__lang" 728 729 if [ "$__var_to_set" ]; then 730 setvar "$__var_to_set" "$( awk \ 731 -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file" 732 )" 733 else 734 awk -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file" 735 fi 736} 737 738# f_index_menusel_command $indexfile $keyword [$var_to_set] 739# 740# Process $indexfile and return only the command portion of the menu_selection 741# line with a keyword portion matching $keyword. 742# 743# This function is for mapping [possibly international] keywords into the 744# command to be executed. If $LANG or $LC_ALL (order of preference) is set, 745# ".encoding" will automatically be appended as a suffix to the provided 746# $indexfile pathname. 747# 748# If, within $indexfile, multiple $menu_selection values map to $keyword, only 749# the first one will be returned. If no mapping can be made, the NULL string is 750# returned. 751# 752# If $indexfile doesn't exist, error status is returned with NULL. 753# 754# If $var_to_set is NULL or missing, output is printed to stdout (which is less 755# recommended due to performance degradation; in a loop for example). 756# 757# This function is a two-parter. Below is the awk(1) portion of the function, 758# afterward is the sh(1) function which utilizes the below awk script. 759# 760f_index_menusel_command_awk=' 761# Variables that should be defined on the invocation line: 762# -v key="keyword" 763# 764BEGIN { 765 prefix = "menu_selection=\"" 766 plen = length(prefix) 767 found = 0 768} 769{ 770 if (!match($0, "^" prefix ".*\\|.*\"")) next 771 772 keyword = command = substr($0, plen + 1, RLENGTH - plen - 1) 773 sub(/^.*\|/, "", command) 774 sub(/\|.*$/, "", keyword) 775 776 if ( keyword == key ) 777 { 778 print command 779 found++ 780 exit 781 } 782} 783END { exit ! found } 784' 785f_index_menusel_command() 786{ 787 local __indexfile="$1" __keyword="$2" __var_to_set="$3" __command 788 local __lang="${LANG:-$LC_ALL}" __file="$__indexfile" 789 790 [ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang" 791 f_dprintf "f_index_menusel_command: index=[%s] key=[%s] lang=[%s]" \ 792 "$__file" "$__keyword" "$__lang" 793 794 [ -f "$__file" ] || return $FAILURE 795 __command=$( awk -v key="$__keyword" \ 796 "$f_index_menusel_command_awk" "$__file" ) || return $FAILURE 797 798 # 799 # If the command pathname is not fully qualified fix-up/force to be 800 # relative to the $indexfile directory. 801 # 802 case "$__command" in 803 /*) : already fully qualified ;; 804 *) 805 local __indexdir="${__indexfile%/*}" 806 [ "$__indexdir" != "$__indexfile" ] || __indexdir="." 807 __command="$__indexdir/$__command" 808 esac 809 810 if [ "$__var_to_set" ]; then 811 setvar "$__var_to_set" "$__command" 812 else 813 echo "$__command" 814 fi 815} 816 817# f_running_as_init 818# 819# Returns true if running as init(1). 820# 821f_running_as_init() 822{ 823 # 824 # When a custom init(8) performs an exec(3) to invoke a shell script, 825 # PID 1 becomes sh(1) and $PPID is set to 1 in the executed script. 826 # 827 [ ${PPID:-0} -eq 1 ] # Return status 828} 829 830# f_mounted $local_directory 831# f_mounted -b $device 832# 833# Return success if a filesystem is mounted on a particular directory. If `-b' 834# is present, instead check that the block device (or a partition thereof) is 835# mounted. 836# 837f_mounted() 838{ 839 local OPTIND OPTARG flag use_device= 840 while getopts b flag; do 841 case "$flag" in 842 b) use_device=1 ;; 843 esac 844 done 845 shift $(( $OPTIND - 1 )) 846 if [ "$use_device" ]; then 847 local device="$1" 848 mount | grep -Eq \ 849 "^$device([[:space:]]|p[0-9]|s[0-9]|\.nop|\.eli)" 850 else 851 [ -d "$dir" ] || return $FAILURE 852 mount | grep -Eq " on $dir \([^)]+\)$" 853 fi 854 # Return status is that of last grep(1) 855} 856 857# f_eval_catch [-de] [-k $var_to_set] $funcname $utility \ 858# $format [$arguments ...] 859# 860# Silently evaluate a command in a sub-shell and test for error. If debugging 861# is enabled a copy of the command and its output is sent to debug (either 862# stdout or file depending on environment). If an error occurs, output of the 863# command is displayed in a dialog(1) msgbox using the [above] f_show_err() 864# function (unless optional `-d' flag is given, then no dialog). 865# 866# The $funcname argument is sent to debugging while the $utility argument is 867# used in the title of the dialog box. The command that is executed as well as 868# sent to debugging with $funcname is the product of the printf(1) syntax 869# produced by $format with optional $arguments. 870# 871# The following options are supported: 872# 873# -d Do not use dialog(1). 874# -e Produce error text from failed command on stderr. 875# -k var Save output from the command in var. 876# 877# Example 1: 878# 879# debug=1 880# f_eval_catch myfunc echo 'echo "%s"' "Hello, World!" 881# 882# Produces the following debug output: 883# 884# DEBUG: myfunc: echo "Hello, World!" 885# DEBUG: myfunc: retval=0 <output below> 886# Hello, World! 887# 888# Example 2: 889# 890# debug=1 891# f_eval_catch -k contents myfunc cat 'cat "%s"' /some/file 892# # dialog(1) Error ``cat: /some/file: No such file or directory'' 893# # contents=[cat: /some/file: No such file or directory] 894# 895# Produces the following debug output: 896# 897# DEBUG: myfunc: cat "/some/file" 898# DEBUG: myfunc: retval=1 <output below> 899# cat: /some/file: No such file or directory 900# 901# Example 3: 902# 903# debug=1 904# echo 123 | f_eval_catch myfunc rev rev 905# 906# Produces the following debug output: 907# 908# DEBUG: myfunc: rev 909# DEBUG: myfunc: retval=0 <output below> 910# 321 911# 912# Example 4: 913# 914# debug=1 915# f_eval_catch myfunc true true 916# 917# Produces the following debug output: 918# 919# DEBUG: myfunc: true 920# DEBUG: myfunc: retval=0 <no output> 921# 922# Example 5: 923# 924# f_eval_catch -de myfunc ls 'ls "%s"' /some/dir 925# # Output on stderr ``ls: /some/dir: No such file or directory'' 926# 927# Example 6: 928# 929# f_eval_catch -dek contents myfunc ls 'ls "%s"' /etc 930# # Output from `ls' sent to stderr and also saved in $contents 931# 932f_eval_catch() 933{ 934 local __no_dialog= __show_err= __var_to_set= 935 936 # 937 # Process local function arguments 938 # 939 local OPTIND OPTARG __flag 940 while getopts "dek:" __flag > /dev/null; do 941 case "$__flag" in 942 d) __no_dialog=1 ;; 943 e) __show_err=1 ;; 944 k) __var_to_set="$OPTARG" ;; 945 esac 946 done 947 shift $(( $OPTIND - 1 )) 948 949 local __funcname="$1" __utility="$2"; shift 2 950 local __cmd __output __retval 951 952 __cmd=$( printf -- "$@" ) 953 f_dprintf "%s: %s" "$__funcname" "$__cmd" # Log command *before* eval 954 __output=$( exec 2>&1; eval "$__cmd" ) 955 __retval=$? 956 if [ "$__output" ]; then 957 [ "$__show_err" ] && echo "$__output" >&2 958 f_dprintf "%s: retval=%i <output below>\n%s" "$__funcname" \ 959 $__retval "$__output" 960 else 961 f_dprintf "%s: retval=%i <no output>" "$__funcname" $__retval 962 fi 963 964 ! [ "$__no_dialog" -o "$nonInteractive" -o $__retval -eq $SUCCESS ] && 965 msg_error="${msg_error:-Error}${__utility:+: $__utility}" \ 966 f_show_err "%s" "$__output" 967 # NB: f_show_err will handle NULL output appropriately 968 969 [ "$__var_to_set" ] && setvar "$__var_to_set" "$__output" 970 971 return $__retval 972} 973 974# f_count $var_to_set arguments ... 975# 976# Sets $var_to_set to the number of arguments minus one (the effective number 977# of arguments following $var_to_set). 978# 979# Example: 980# f_count count dog house # count=[2] 981# 982f_count() 983{ 984 setvar "$1" $(( $# - 1 )) 985} 986 987# f_count_ifs $var_to_set string ... 988# 989# Sets $var_to_set to the number of words (split by the internal field 990# separator, IFS) following $var_to_set. 991# 992# Example 1: 993# 994# string="word1 word2 word3" 995# f_count_ifs count "$string" # count=[3] 996# f_count_ifs count $string # count=[3] 997# 998# Example 2: 999# 1000# IFS=. f_count_ifs count www.freebsd.org # count=[3] 1001# 1002# NB: Make sure to use double-quotes if you are using a custom value for IFS 1003# and you don't want the current value to effect the result. See example 3. 1004# 1005# Example 3: 1006# 1007# string="a-b c-d" 1008# IFS=- f_count_ifs count "$string" # count=[3] 1009# IFS=- f_count_ifs count $string # count=[4] 1010# 1011f_count_ifs() 1012{ 1013 local __var_to_set="$1" 1014 shift 1 1015 set -- $* 1016 setvar "$__var_to_set" $# 1017} 1018 1019############################################################ MAIN 1020 1021# 1022# Trap signals so we can recover gracefully 1023# 1024trap 'f_interrupt' INT 1025trap 'f_die' TERM PIPE XCPU XFSZ FPE TRAP ABRT SEGV 1026trap '' ALRM PROF USR1 USR2 HUP VTALRM 1027 1028# 1029# Clone terminal stdout/stderr so we can redirect to it from within sub-shells 1030# 1031eval exec $TERMINAL_STDOUT_PASSTHRU\>\&1 1032eval exec $TERMINAL_STDERR_PASSTHRU\>\&2 1033 1034# 1035# Self-initialize unless requested otherwise 1036# 1037f_dprintf "%s: DEBUG_SELF_INITIALIZE=[%s]" \ 1038 dialog.subr "$DEBUG_SELF_INITIALIZE" 1039case "$DEBUG_SELF_INITIALIZE" in 1040""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;; 1041*) f_debug_init 1042esac 1043 1044# 1045# Log our operating environment for debugging purposes 1046# 1047f_dprintf "UNAME_S=[%s] UNAME_P=[%s] UNAME_R=[%s]" \ 1048 "$UNAME_S" "$UNAME_P" "$UNAME_R" 1049 1050f_dprintf "%s: Successfully loaded." common.subr 1051 1052fi # ! $_COMMON_SUBR 1053