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