2012-07-14 03:16:57 +00:00
|
|
|
if [ ! "$_COMMON_SUBR" ]; then _COMMON_SUBR=1
|
|
|
|
#
|
|
|
|
# Copyright (c) 2012 Ron McDowell
|
2015-09-14 21:26:48 +00:00
|
|
|
# Copyright (c) 2012-2015 Devin Teske
|
2012-07-14 03:16:57 +00:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
# SUCH DAMAGE.
|
|
|
|
#
|
|
|
|
# $FreeBSD$
|
|
|
|
#
|
2012-12-21 19:26:17 +00:00
|
|
|
############################################################ CONFIGURATION
|
|
|
|
|
|
|
|
#
|
|
|
|
# Default file descriptors to link to stdout/stderr for passthru allowing
|
|
|
|
# redirection within a sub-shell to bypass directly to the terminal.
|
|
|
|
#
|
2015-09-14 21:26:48 +00:00
|
|
|
: ${TERMINAL_STDOUT_PASSTHRU:=3}
|
|
|
|
: ${TERMINAL_STDERR_PASSTHRU:=4}
|
2012-12-21 19:26:17 +00:00
|
|
|
|
2012-07-14 03:16:57 +00:00
|
|
|
############################################################ GLOBALS
|
|
|
|
|
|
|
|
#
|
|
|
|
# Program name
|
|
|
|
#
|
|
|
|
pgm="${0##*/}"
|
|
|
|
|
|
|
|
#
|
|
|
|
# Program arguments
|
|
|
|
#
|
|
|
|
ARGC="$#"
|
|
|
|
ARGV="$@"
|
|
|
|
|
|
|
|
#
|
|
|
|
# Global exit status variables
|
|
|
|
#
|
|
|
|
SUCCESS=0
|
|
|
|
FAILURE=1
|
|
|
|
|
2012-12-25 10:47:45 +00:00
|
|
|
#
|
|
|
|
# Operating environment details
|
|
|
|
#
|
2013-12-07 00:31:01 +00:00
|
|
|
export UNAME_S="$( uname -s )" # Operating System (i.e. FreeBSD)
|
|
|
|
export UNAME_P="$( uname -p )" # Processor Architecture (i.e. i386)
|
|
|
|
export UNAME_M="$( uname -m )" # Machine platform (i.e. i386)
|
|
|
|
export UNAME_R="$( uname -r )" # Release Level (i.e. X.Y-RELEASE)
|
2012-12-25 10:47:45 +00:00
|
|
|
|
2013-04-22 05:02:34 +00:00
|
|
|
#
|
|
|
|
# Default behavior is to call f_debug_init() automatically when loaded.
|
|
|
|
#
|
|
|
|
: ${DEBUG_SELF_INITIALIZE=1}
|
|
|
|
|
2013-11-07 10:20:19 +00:00
|
|
|
#
|
|
|
|
# Default behavior of f_debug_init() is to truncate $debugFile (set to NULL to
|
|
|
|
# disable truncating the debug file when initializing). To get child processes
|
|
|
|
# to append to the same log file, export this variarable (with a NULL value)
|
|
|
|
# and also export debugFile with the desired value.
|
|
|
|
#
|
|
|
|
: ${DEBUG_INITIALIZE_FILE=1}
|
|
|
|
|
2013-05-14 03:21:13 +00:00
|
|
|
#
|
|
|
|
# Define standard optstring arguments that should be supported by all programs
|
|
|
|
# using this include (unless DEBUG_SELF_INITIALIZE is set to NULL to prevent
|
|
|
|
# f_debug_init() from autamatically processing "$@" for the below arguments):
|
|
|
|
#
|
|
|
|
# d Sets $debug to 1
|
|
|
|
# D: Sets $debugFile to $OPTARG
|
|
|
|
#
|
|
|
|
GETOPTS_STDARGS="dD:"
|
|
|
|
|
2013-06-02 22:04:39 +00:00
|
|
|
#
|
|
|
|
# The getopts builtin will return 1 either when the end of "$@" or the first
|
|
|
|
# invalid flag is reached. This makes it impossible to determine if you've
|
|
|
|
# processed all the arguments or simply have hit an invalid flag. In the cases
|
|
|
|
# where we want to tolerate invalid flags (f_debug_init() for example), the
|
|
|
|
# following variable can be appended to your optstring argument to getopts,
|
|
|
|
# preventing it from prematurely returning 1 before the end of the arguments.
|
|
|
|
#
|
|
|
|
# NOTE: This assumes that all unknown flags are argument-less.
|
|
|
|
#
|
|
|
|
GETOPTS_ALLFLAGS="abcdefghijklmnopqrstuvwxyz"
|
|
|
|
GETOPTS_ALLFLAGS="${GETOPTS_ALLFLAGS}ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
GETOPTS_ALLFLAGS="${GETOPTS_ALLFLAGS}0123456789"
|
|
|
|
|
|
|
|
#
|
|
|
|
# When we get included, f_debug_init() will fire (unless $DEBUG_SELF_INITIALIZE
|
|
|
|
# is set to disable automatic initialization) and process "$@" for a few global
|
|
|
|
# options such as `-d' and/or `-D file'. However, if your program takes custom
|
|
|
|
# flags that take arguments, this automatic processing may fail unexpectedly.
|
|
|
|
#
|
|
|
|
# The solution to this problem is to pre-define (before including this file)
|
|
|
|
# the following variable (which defaults to NULL) to indicate that there are
|
|
|
|
# extra flags that should be considered when performing automatic processing of
|
|
|
|
# globally persistent flags.
|
|
|
|
#
|
|
|
|
: ${GETOPTS_EXTRA:=}
|
|
|
|
|
2012-07-14 03:16:57 +00:00
|
|
|
############################################################ FUNCTIONS
|
|
|
|
|
2013-11-07 10:14:40 +00:00
|
|
|
# f_dprintf $format [$arguments ...]
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
2012-12-25 10:47:45 +00:00
|
|
|
# Sensible debug function. Override in ~/.bsdconfigrc if desired.
|
|
|
|
# See /usr/share/examples/bsdconfig/bsdconfigrc for example.
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
2013-01-03 15:48:00 +00:00
|
|
|
# If $debug is set and non-NULL, prints DEBUG info using printf(1) syntax:
|
|
|
|
# + To $debugFile, if set and non-NULL
|
|
|
|
# + To standard output if $debugFile is either NULL or unset
|
|
|
|
# + To both if $debugFile begins with a single plus-sign (`+')
|
|
|
|
#
|
2012-07-14 03:16:57 +00:00
|
|
|
f_dprintf()
|
|
|
|
{
|
2012-12-25 10:47:45 +00:00
|
|
|
[ "$debug" ] || return $SUCCESS
|
|
|
|
local fmt="$1"; shift
|
2012-12-28 23:49:17 +00:00
|
|
|
case "$debugFile" in ""|+*)
|
2012-12-25 10:47:45 +00:00
|
|
|
printf "DEBUG: $fmt${fmt:+\n}" "$@" >&${TERMINAL_STDOUT_PASSTHRU:-1}
|
2012-12-28 23:49:17 +00:00
|
|
|
esac
|
|
|
|
[ "${debugFile#+}" ] &&
|
|
|
|
printf "DEBUG: $fmt${fmt:+\n}" "$@" >> "${debugFile#+}"
|
|
|
|
return $SUCCESS
|
2012-07-14 03:16:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 05:02:34 +00:00
|
|
|
# f_debug_init
|
|
|
|
#
|
|
|
|
# Initialize debugging. Truncates $debugFile to zero bytes if set.
|
|
|
|
#
|
|
|
|
f_debug_init()
|
|
|
|
{
|
|
|
|
#
|
|
|
|
# Process stored command-line arguments
|
|
|
|
#
|
2013-04-22 21:03:44 +00:00
|
|
|
set -- $ARGV
|
2014-04-23 22:04:04 +00:00
|
|
|
local OPTIND OPTARG flag
|
2013-05-14 03:21:13 +00:00
|
|
|
f_dprintf "f_debug_init: ARGV=[%s] GETOPTS_STDARGS=[%s]" \
|
|
|
|
"$ARGV" "$GETOPTS_STDARGS"
|
2013-06-02 22:04:39 +00:00
|
|
|
while getopts "$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" flag \
|
|
|
|
> /dev/null; do
|
2013-04-22 21:03:44 +00:00
|
|
|
case "$flag" in
|
2013-06-24 20:58:54 +00:00
|
|
|
d) debug=1 ;;
|
|
|
|
D) debugFile="$OPTARG" ;;
|
2013-04-22 21:03:44 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(( $OPTIND - 1 ))
|
2013-05-14 03:21:13 +00:00
|
|
|
f_dprintf "f_debug_init: debug=[%s] debugFile=[%s]" \
|
|
|
|
"$debug" "$debugFile"
|
2013-04-22 21:03:44 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Automagically enable debugging if debugFile is set (and non-NULL)
|
|
|
|
#
|
|
|
|
[ "$debugFile" ] && { [ "${debug+set}" ] || debug=1; }
|
2013-04-22 05:02:34 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Make debugging persistant if set
|
|
|
|
#
|
|
|
|
[ "$debug" ] && export debug
|
2013-04-22 21:19:44 +00:00
|
|
|
[ "$debugFile" ] && export debugFile
|
2013-04-22 05:02:34 +00:00
|
|
|
|
|
|
|
#
|
2013-11-07 10:20:19 +00:00
|
|
|
# Truncate debug file unless requested otherwise. Note that we will
|
|
|
|
# trim a leading plus (`+') from the value of debugFile to support
|
|
|
|
# persistant meaning that f_dprintf() should print both to standard
|
|
|
|
# output and $debugFile (minus the leading plus, of course).
|
2013-04-22 05:02:34 +00:00
|
|
|
#
|
|
|
|
local _debug_file="${debugFile#+}"
|
2013-11-07 10:20:19 +00:00
|
|
|
if [ "$_debug_file" -a "$DEBUG_INITIALIZE_FILE" ]; then
|
2013-04-22 05:02:34 +00:00
|
|
|
if ( umask 022 && :> "$_debug_file" ); then
|
|
|
|
f_dprintf "Successfully initialized debugFile \`%s'" \
|
|
|
|
"$_debug_file"
|
2013-11-07 10:14:40 +00:00
|
|
|
f_isset debug || debug=1 # turn debugging on if not set
|
2013-04-22 05:02:34 +00:00
|
|
|
else
|
|
|
|
unset debugFile
|
|
|
|
f_dprintf "Unable to initialize debugFile \`%s'" \
|
|
|
|
"$_debug_file"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-11-07 10:14:40 +00:00
|
|
|
# f_err $format [$arguments ...]
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
|
|
|
# Print a message to stderr (fd=2).
|
|
|
|
#
|
|
|
|
f_err()
|
|
|
|
{
|
2013-12-07 00:31:01 +00:00
|
|
|
printf "$@" >&2
|
2012-07-14 03:16:57 +00:00
|
|
|
}
|
|
|
|
|
2013-11-07 10:14:40 +00:00
|
|
|
# f_quietly $command [$arguments ...]
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
2012-09-22 03:11:35 +00:00
|
|
|
# Run a command quietly (quell any output to stdout or stderr)
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
|
|
|
f_quietly()
|
|
|
|
{
|
2012-09-21 19:03:25 +00:00
|
|
|
"$@" > /dev/null 2>&1
|
2012-07-14 03:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# f_have $anything ...
|
2012-09-22 03:11:35 +00:00
|
|
|
#
|
2012-07-14 03:16:57 +00:00
|
|
|
# A wrapper to the `type' built-in. Returns true if argument is a valid shell
|
|
|
|
# built-in, keyword, or externally-tracked binary, otherwise false.
|
|
|
|
#
|
|
|
|
f_have()
|
|
|
|
{
|
|
|
|
f_quietly type "$@"
|
|
|
|
}
|
|
|
|
|
2014-07-31 22:00:36 +00:00
|
|
|
# setvar $var_to_set [$value]
|
|
|
|
#
|
2014-07-31 22:13:31 +00:00
|
|
|
# Implement setvar for shells unlike FreeBSD sh(1).
|
2014-07-31 22:00:36 +00:00
|
|
|
#
|
|
|
|
if ! f_have setvar; then
|
|
|
|
setvar()
|
|
|
|
{
|
|
|
|
[ $# -gt 0 ] || return $SUCCESS
|
|
|
|
local __setvar_var_to_set="$1" __setvar_right="$2" __setvar_left=
|
|
|
|
case $# in
|
|
|
|
1) unset "$__setvar_var_to_set"
|
|
|
|
return $? ;;
|
|
|
|
2) : fall through ;;
|
|
|
|
*) f_err "setvar: too many arguments\n"
|
|
|
|
return $FAILURE
|
|
|
|
esac
|
2014-07-31 22:13:31 +00:00
|
|
|
case "$__setvar_var_to_set" in *[!0-9A-Za-z_]*)
|
|
|
|
f_err "setvar: %s: bad variable name\n" "$__setvar_var_to_set"
|
|
|
|
return 2
|
|
|
|
esac
|
2014-07-31 22:00:36 +00:00
|
|
|
while case "$__setvar_r" in *\'*) : ;; *) false ; esac
|
|
|
|
do
|
|
|
|
__setvar_left="$__setvar_left${__setvar_right%%\'*}'\\''"
|
|
|
|
__setvar_right="${__setvar_right#*\'}"
|
|
|
|
done
|
|
|
|
__setvar_left="$__setvar_left${__setvar_right#*\'}"
|
|
|
|
eval "$__setvar_var_to_set='$__setvar_left'"
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
2013-07-10 22:45:07 +00:00
|
|
|
# f_which $anything [$var_to_set]
|
|
|
|
#
|
|
|
|
# A fast built-in replacement for syntaxes such as foo=$( which bar ). In a
|
|
|
|
# comparison of 10,000 runs of this function versus which, this function
|
|
|
|
# completed in under 3 seconds, while `which' took almost a full minute.
|
|
|
|
#
|
|
|
|
# If $var_to_set is missing or NULL, output is (like which) to standard out.
|
|
|
|
# Returns success if a match was found, failure otherwise.
|
|
|
|
#
|
|
|
|
f_which()
|
|
|
|
{
|
|
|
|
local __name="$1" __var_to_set="$2"
|
|
|
|
case "$__name" in */*|'') return $FAILURE; esac
|
2014-08-24 16:40:31 +00:00
|
|
|
local __p __exec IFS=":" __found=
|
2013-07-10 22:45:07 +00:00
|
|
|
for __p in $PATH; do
|
2014-08-24 16:40:31 +00:00
|
|
|
__exec="$__p/$__name"
|
|
|
|
[ -f "$__exec" -a -x "$__exec" ] && __found=1 break
|
2013-07-10 22:45:07 +00:00
|
|
|
done
|
|
|
|
if [ "$__found" ]; then
|
|
|
|
if [ "$__var_to_set" ]; then
|
|
|
|
setvar "$__var_to_set" "$__exec"
|
|
|
|
else
|
|
|
|
echo "$__exec"
|
|
|
|
fi
|
|
|
|
return $SUCCESS
|
|
|
|
fi
|
|
|
|
return $FAILURE
|
|
|
|
}
|
|
|
|
|
2013-01-05 02:08:47 +00:00
|
|
|
# f_getvar $var_to_get [$var_to_set]
|
|
|
|
#
|
|
|
|
# Utility function designed to go along with the already-builtin setvar.
|
|
|
|
# Allows clean variable name indirection without forking or sub-shells.
|
|
|
|
#
|
|
|
|
# Returns error status if the requested variable ($var_to_get) is not set.
|
|
|
|
#
|
|
|
|
# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
|
|
|
|
# standard output for capturing in a sub-shell (which is less-recommended
|
|
|
|
# because of performance degredation; for example, when called in a loop).
|
|
|
|
#
|
|
|
|
f_getvar()
|
|
|
|
{
|
Import media selection/preparation framework (sysinstall inspired). Makes
accessing files from various types of media nice and abstracted away from
the wet-work involved in preparing, validating, and initializing those
types of media. This will be used for the package management system module
and other modules that need access to files and want to allow the user to
decide where those files come from (either in a scripted fashion, prompted
fashion, or any combination thereof).
Heavily inspired by sysinstall and even uses the same reserved words so
that scripts are portable. Coded over months, tested continuously through-
out, and reviewed several times.
Some notes about the changes:
- Move network-setting acquisition/validation routines to media/tcpip.subr
- The options screen from sysinstall has been converted to a dialog menu
- The "UFS" media choice is renamed to "Directory" to reflect how sysinstall
treats the choice and a new [true] "UFS" media choice has been added that
acts on real UFS partitions (such as external disks with disklabels).
- Many more help files have been resurrected from sysinstall (I noticed that
some of the content seems a bit dated; I gave them a once-over but they
could really use an update).
- A total of 10 media choices are presented (via mediaGetType) including:
CD/DVD, FTP, FTP Passive, HTTP Proxy, Directory, NFS, DOS, UFS, Floppy, USB
- Novel struct/device management layer for managing the issue of passing
more information than can comfortably fit in an argument list.
2013-02-25 19:55:32 +00:00
|
|
|
local __var_to_get="$1" __var_to_set="$2"
|
|
|
|
[ "$__var_to_set" ] || local value
|
|
|
|
eval [ \"\${$__var_to_get+set}\" ]
|
|
|
|
local __retval=$?
|
2014-03-07 20:18:39 +00:00
|
|
|
eval ${__var_to_set:-value}=\"\${$__var_to_get}\"
|
2013-01-05 02:08:47 +00:00
|
|
|
eval f_dprintf '"f_getvar: var=[%s] value=[%s] r=%u"' \
|
Import media selection/preparation framework (sysinstall inspired). Makes
accessing files from various types of media nice and abstracted away from
the wet-work involved in preparing, validating, and initializing those
types of media. This will be used for the package management system module
and other modules that need access to files and want to allow the user to
decide where those files come from (either in a scripted fashion, prompted
fashion, or any combination thereof).
Heavily inspired by sysinstall and even uses the same reserved words so
that scripts are portable. Coded over months, tested continuously through-
out, and reviewed several times.
Some notes about the changes:
- Move network-setting acquisition/validation routines to media/tcpip.subr
- The options screen from sysinstall has been converted to a dialog menu
- The "UFS" media choice is renamed to "Directory" to reflect how sysinstall
treats the choice and a new [true] "UFS" media choice has been added that
acts on real UFS partitions (such as external disks with disklabels).
- Many more help files have been resurrected from sysinstall (I noticed that
some of the content seems a bit dated; I gave them a once-over but they
could really use an update).
- A total of 10 media choices are presented (via mediaGetType) including:
CD/DVD, FTP, FTP Passive, HTTP Proxy, Directory, NFS, DOS, UFS, Floppy, USB
- Novel struct/device management layer for managing the issue of passing
more information than can comfortably fit in an argument list.
2013-02-25 19:55:32 +00:00
|
|
|
\"\$__var_to_get\" \"\$${__var_to_set:-value}\" \$__retval
|
|
|
|
[ "$__var_to_set" ] || { [ "$value" ] && echo "$value"; }
|
|
|
|
return $__retval
|
2013-01-05 02:08:47 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 03:57:45 +00:00
|
|
|
# f_isset $var
|
|
|
|
#
|
|
|
|
# Check if variable $var is set. Returns success if variable is set, otherwise
|
|
|
|
# returns failure.
|
|
|
|
#
|
|
|
|
f_isset()
|
|
|
|
{
|
|
|
|
eval [ \"\${${1%%[$IFS]*}+set}\" ]
|
|
|
|
}
|
|
|
|
|
2013-11-07 10:14:40 +00:00
|
|
|
# f_die [$status [$format [$arguments ...]]]
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
|
|
|
# Abruptly terminate due to an error optionally displaying a message in a
|
|
|
|
# dialog box using printf(1) syntax.
|
|
|
|
#
|
|
|
|
f_die()
|
|
|
|
{
|
|
|
|
local status=$FAILURE
|
|
|
|
|
|
|
|
# If there is at least one argument, take it as the status
|
|
|
|
if [ $# -gt 0 ]; then
|
|
|
|
status=$1
|
|
|
|
shift 1 # status
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If there are still arguments left, pass them to f_show_msg
|
|
|
|
[ $# -gt 0 ] && f_show_msg "$@"
|
|
|
|
|
|
|
|
# Optionally call f_clean_up() function if it exists
|
|
|
|
f_have f_clean_up && f_clean_up
|
|
|
|
|
|
|
|
exit $status
|
|
|
|
}
|
|
|
|
|
|
|
|
# f_interrupt
|
|
|
|
#
|
|
|
|
# Interrupt handler.
|
|
|
|
#
|
|
|
|
f_interrupt()
|
|
|
|
{
|
|
|
|
exec 2>&1 # fix sh(1) bug where stderr gets lost within async-trap
|
|
|
|
f_die
|
|
|
|
}
|
|
|
|
|
2013-11-07 10:14:40 +00:00
|
|
|
# f_show_info $format [$arguments ...]
|
2012-12-21 20:29:28 +00:00
|
|
|
#
|
|
|
|
# Display a message in a dialog infobox using printf(1) syntax.
|
|
|
|
#
|
|
|
|
f_show_info()
|
|
|
|
{
|
|
|
|
local msg
|
|
|
|
msg=$( printf "$@" )
|
|
|
|
|
|
|
|
#
|
|
|
|
# Use f_dialog_infobox from dialog.subr if possible, otherwise fall
|
|
|
|
# back to dialog(1) (without options, making it obvious when using
|
|
|
|
# un-aided system dialog).
|
|
|
|
#
|
|
|
|
if f_have f_dialog_info; then
|
|
|
|
f_dialog_info "$msg"
|
|
|
|
else
|
|
|
|
dialog --infobox "$msg" 0 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-11-07 10:14:40 +00:00
|
|
|
# f_show_msg $format [$arguments ...]
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
|
|
|
# Display a message in a dialog box using printf(1) syntax.
|
|
|
|
#
|
|
|
|
f_show_msg()
|
|
|
|
{
|
|
|
|
local msg
|
|
|
|
msg=$( printf "$@" )
|
|
|
|
|
|
|
|
#
|
|
|
|
# Use f_dialog_msgbox from dialog.subr if possible, otherwise fall
|
|
|
|
# back to dialog(1) (without options, making it obvious when using
|
|
|
|
# un-aided system dialog).
|
|
|
|
#
|
|
|
|
if f_have f_dialog_msgbox; then
|
|
|
|
f_dialog_msgbox "$msg"
|
|
|
|
else
|
|
|
|
dialog --msgbox "$msg" 0 0
|
|
|
|
fi
|
2013-01-14 01:15:25 +00:00
|
|
|
}
|
|
|
|
|
2013-11-07 10:23:37 +00:00
|
|
|
# f_show_err $format [$arguments ...]
|
|
|
|
#
|
|
|
|
# Display a message in a dialog box with ``Error'' i18n title (overridden by
|
2014-03-07 20:20:27 +00:00
|
|
|
# setting msg_error) using printf(1) syntax.
|
2013-11-07 10:23:37 +00:00
|
|
|
#
|
|
|
|
f_show_err()
|
|
|
|
{
|
|
|
|
local msg
|
|
|
|
msg=$( printf "$@" )
|
|
|
|
|
|
|
|
: ${msg:=${msg_an_unknown_error_occurred:-An unknown error occurred}}
|
|
|
|
|
|
|
|
if [ "$_DIALOG_SUBR" ]; then
|
|
|
|
f_dialog_title "${msg_error:-Error}"
|
|
|
|
f_dialog_msgbox "$msg"
|
|
|
|
f_dialog_title_restore
|
|
|
|
else
|
|
|
|
dialog --title "${msg_error:-Error}" --msgbox "$msg" 0 0
|
|
|
|
fi
|
|
|
|
return $SUCCESS
|
|
|
|
}
|
2013-01-14 01:15:25 +00:00
|
|
|
|
2013-11-07 10:14:40 +00:00
|
|
|
# f_yesno $format [$arguments ...]
|
2013-01-14 01:15:25 +00:00
|
|
|
#
|
|
|
|
# Display a message in a dialog yes/no box using printf(1) syntax.
|
|
|
|
#
|
|
|
|
f_yesno()
|
|
|
|
{
|
|
|
|
local msg
|
|
|
|
msg=$( printf "$@" )
|
|
|
|
|
|
|
|
#
|
|
|
|
# Use f_dialog_yesno from dialog.subr if possible, otherwise fall
|
|
|
|
# back to dialog(1) (without options, making it obvious when using
|
|
|
|
# un-aided system dialog).
|
|
|
|
#
|
|
|
|
if f_have f_dialog_yesno; then
|
|
|
|
f_dialog_yesno "$msg"
|
|
|
|
else
|
|
|
|
dialog --yesno "$msg" 0 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-11-07 10:14:40 +00:00
|
|
|
# f_noyes $format [$arguments ...]
|
2013-01-14 01:15:25 +00:00
|
|
|
#
|
|
|
|
# Display a message in a dialog yes/no box using printf(1) syntax.
|
|
|
|
# NOTE: THis is just like the f_yesno function except "No" is default.
|
|
|
|
#
|
|
|
|
f_noyes()
|
|
|
|
{
|
|
|
|
local msg
|
|
|
|
msg=$( printf "$@" )
|
|
|
|
|
|
|
|
#
|
|
|
|
# Use f_dialog_noyes from dialog.subr if possible, otherwise fall
|
|
|
|
# back to dialog(1) (without options, making it obvious when using
|
|
|
|
# un-aided system dialog).
|
|
|
|
#
|
|
|
|
if f_have f_dialog_noyes; then
|
|
|
|
f_dialog_noyes "$msg"
|
|
|
|
else
|
|
|
|
dialog --defaultno --yesno "$msg" 0 0
|
|
|
|
fi
|
2012-07-14 03:16:57 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 00:31:25 +00:00
|
|
|
# f_show_help $file
|
|
|
|
#
|
|
|
|
# Display a language help-file. Automatically takes $LANG and $LC_ALL into
|
|
|
|
# consideration when displaying $file (suffix ".$LC_ALL" or ".$LANG" will
|
|
|
|
# automatically be added prior to loading the language help-file).
|
|
|
|
#
|
|
|
|
# If a language has been requested by setting either $LANG or $LC_ALL in the
|
|
|
|
# environment and the language-specific help-file does not exist we will fall
|
|
|
|
# back to $file without-suffix.
|
|
|
|
#
|
|
|
|
# If the language help-file does not exist, an error is displayed instead.
|
|
|
|
#
|
|
|
|
f_show_help()
|
|
|
|
{
|
|
|
|
local file="$1"
|
|
|
|
local lang="${LANG:-$LC_ALL}"
|
|
|
|
|
|
|
|
[ -f "$file.$lang" ] && file="$file.$lang"
|
|
|
|
|
|
|
|
#
|
|
|
|
# Use f_dialog_textbox from dialog.subr if possible, otherwise fall
|
|
|
|
# back to dialog(1) (without options, making it obvious when using
|
|
|
|
# un-aided system dialog).
|
|
|
|
#
|
|
|
|
if f_have f_dialog_textbox; then
|
|
|
|
f_dialog_textbox "$file"
|
|
|
|
else
|
|
|
|
dialog --msgbox "$( cat "$file" 2>&1 )" 0 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2012-07-14 03:16:57 +00:00
|
|
|
# f_include $file
|
|
|
|
#
|
|
|
|
# Include a shell subroutine file.
|
|
|
|
#
|
|
|
|
# If the subroutine file exists but returns error status during loading, exit
|
|
|
|
# is called and execution is prematurely terminated with the same error status.
|
|
|
|
#
|
|
|
|
f_include()
|
|
|
|
{
|
|
|
|
local file="$1"
|
2012-12-21 19:26:17 +00:00
|
|
|
f_dprintf "f_include: file=[%s]" "$file"
|
2012-07-14 03:16:57 +00:00
|
|
|
. "$file" || exit $?
|
|
|
|
}
|
|
|
|
|
|
|
|
# f_include_lang $file
|
|
|
|
#
|
|
|
|
# Include a language file. Automatically takes $LANG and $LC_ALL into
|
2012-10-18 07:55:09 +00:00
|
|
|
# consideration when including $file (suffix ".$LC_ALL" or ".$LANG" will
|
2012-07-14 03:16:57 +00:00
|
|
|
# automatically by added prior to loading the language file).
|
|
|
|
#
|
|
|
|
# No error is produced if (a) a language has been requested (by setting either
|
|
|
|
# $LANG or $LC_ALL in the environment) and (b) the language file does not
|
|
|
|
# exist -- in which case we will fall back to loading $file without-suffix.
|
|
|
|
#
|
|
|
|
# If the language file exists but returns error status during loading, exit
|
|
|
|
# is called and execution is prematurely terminated with the same error status.
|
|
|
|
#
|
|
|
|
f_include_lang()
|
|
|
|
{
|
|
|
|
local file="$1"
|
|
|
|
local lang="${LANG:-$LC_ALL}"
|
|
|
|
|
2012-12-21 19:26:17 +00:00
|
|
|
f_dprintf "f_include_lang: file=[%s] lang=[%s]" "$file" "$lang"
|
2012-07-14 03:16:57 +00:00
|
|
|
if [ -f "$file.$lang" ]; then
|
|
|
|
. "$file.$lang" || exit $?
|
|
|
|
else
|
|
|
|
. "$file" || exit $?
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-11-07 10:14:40 +00:00
|
|
|
# f_usage $file [$key1 $value1 ...]
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
|
|
|
# Display USAGE file with optional pre-processor macro definitions. The first
|
|
|
|
# argument is the template file containing the usage text to be displayed. If
|
|
|
|
# $LANG or $LC_ALL (in order of preference, respectively) is set, ".encoding"
|
|
|
|
# will automatically be appended as a suffix to the provided $file pathname.
|
|
|
|
#
|
|
|
|
# When processing $file, output begins at the first line containing that is
|
|
|
|
# (a) not a comment, (b) not empty, and (c) is not pure-whitespace. All lines
|
|
|
|
# appearing after this first-line are output, including (a) comments (b) empty
|
|
|
|
# lines, and (c) lines that are purely whitespace-only.
|
|
|
|
#
|
|
|
|
# If additional arguments appear after $file, substitutions are made while
|
|
|
|
# printing the contents of the USAGE file. The pre-processor macro syntax is in
|
|
|
|
# the style of autoconf(1), for example:
|
|
|
|
#
|
|
|
|
# f_usage $file "FOO" "BAR"
|
|
|
|
#
|
|
|
|
# Will cause instances of "@FOO@" appearing in $file to be replaced with the
|
2014-03-07 20:15:52 +00:00
|
|
|
# text "BAR" before being printed to the screen.
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
|
|
|
# This function is a two-parter. Below is the awk(1) portion of the function,
|
|
|
|
# afterward is the sh(1) function which utilizes the below awk script.
|
|
|
|
#
|
|
|
|
f_usage_awk='
|
|
|
|
BEGIN { found = 0 }
|
|
|
|
{
|
|
|
|
if ( !found && $0 ~ /^[[:space:]]*($|#)/ ) next
|
|
|
|
found = 1
|
|
|
|
print
|
|
|
|
}
|
|
|
|
'
|
|
|
|
f_usage()
|
|
|
|
{
|
|
|
|
local file="$1"
|
|
|
|
local lang="${LANG:-$LC_ALL}"
|
|
|
|
|
2012-12-21 19:26:17 +00:00
|
|
|
f_dprintf "f_usage: file=[%s] lang=[%s]" "$file" "$lang"
|
2012-07-14 03:16:57 +00:00
|
|
|
|
|
|
|
shift 1 # file
|
|
|
|
|
|
|
|
local usage
|
|
|
|
if [ -f "$file.$lang" ]; then
|
|
|
|
usage=$( awk "$f_usage_awk" "$file.$lang" ) || exit $FAILURE
|
|
|
|
else
|
|
|
|
usage=$( awk "$f_usage_awk" "$file" ) || exit $FAILURE
|
|
|
|
fi
|
|
|
|
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
local key="$1"
|
|
|
|
export value="$2"
|
|
|
|
usage=$( echo "$usage" | awk \
|
|
|
|
"{ gsub(/@$key@/, ENVIRON[\"value\"]); print }" )
|
|
|
|
shift 2
|
|
|
|
done
|
|
|
|
|
|
|
|
f_err "%s\n" "$usage"
|
|
|
|
|
|
|
|
exit $FAILURE
|
|
|
|
}
|
|
|
|
|
2013-12-07 00:31:01 +00:00
|
|
|
# f_index_file $keyword [$var_to_set]
|
2012-11-16 00:59:11 +00:00
|
|
|
#
|
|
|
|
# Process all INDEX files known to bsdconfig and return the path to first file
|
|
|
|
# containing a menu_selection line with a keyword portion matching $keyword.
|
|
|
|
#
|
|
|
|
# If $LANG or $LC_ALL (in order of preference, respectively) is set,
|
|
|
|
# "INDEX.encoding" files will be searched first.
|
|
|
|
#
|
|
|
|
# If no file is found, error status is returned along with the NULL string.
|
|
|
|
#
|
2013-12-07 00:31:01 +00:00
|
|
|
# If $var_to_set is NULL or missing, output is printed to stdout (which is less
|
|
|
|
# recommended due to performance degradation; in a loop for example).
|
|
|
|
#
|
2012-11-16 00:59:11 +00:00
|
|
|
# This function is a two-parter. Below is the awk(1) portion of the function,
|
|
|
|
# afterward is the sh(1) function which utilizes the below awk script.
|
|
|
|
#
|
|
|
|
f_index_file_awk='
|
|
|
|
# Variables that should be defined on the invocation line:
|
|
|
|
# -v keyword="keyword"
|
|
|
|
BEGIN { found = 0 }
|
|
|
|
( $0 ~ "^menu_selection=\"" keyword "\\|" ) {
|
|
|
|
print FILENAME
|
|
|
|
found++
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
END { exit ! found }
|
|
|
|
'
|
|
|
|
f_index_file()
|
|
|
|
{
|
2013-12-07 00:31:01 +00:00
|
|
|
local __keyword="$1" __var_to_set="$2"
|
|
|
|
local __lang="${LANG:-$LC_ALL}"
|
|
|
|
local __indexes="$BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX"
|
2012-11-16 00:59:11 +00:00
|
|
|
|
2013-12-07 00:31:01 +00:00
|
|
|
f_dprintf "f_index_file: keyword=[%s] lang=[%s]" "$__keyword" "$__lang"
|
2012-11-16 00:59:11 +00:00
|
|
|
|
2013-12-07 00:31:01 +00:00
|
|
|
if [ "$__lang" ]; then
|
|
|
|
if [ "$__var_to_set" ]; then
|
|
|
|
eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \
|
|
|
|
"$f_index_file_awk" $__indexes.$__lang
|
|
|
|
)"' && return $SUCCESS
|
|
|
|
else
|
|
|
|
awk -v keyword="$__keyword" "$f_index_file_awk" \
|
|
|
|
$__indexes.$__lang && return $SUCCESS
|
|
|
|
fi
|
2012-11-16 00:59:11 +00:00
|
|
|
# No match, fall-thru to non-i18n sources
|
|
|
|
fi
|
2013-12-07 00:31:01 +00:00
|
|
|
if [ "$__var_to_set" ]; then
|
|
|
|
eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \
|
|
|
|
"$f_index_file_awk" $__indexes )"' && return $SUCCESS
|
|
|
|
else
|
|
|
|
awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes &&
|
|
|
|
return $SUCCESS
|
|
|
|
fi
|
2013-07-05 20:13:00 +00:00
|
|
|
|
|
|
|
# No match? Fall-thru to `local' libexec sources (add-on modules)
|
|
|
|
|
|
|
|
[ "$BSDCFG_LOCAL_LIBE" ] || return $FAILURE
|
2013-12-07 00:31:01 +00:00
|
|
|
__indexes="$BSDCFG_LOCAL_LIBE/*/INDEX"
|
|
|
|
if [ "$__lang" ]; then
|
|
|
|
if [ "$__var_to_set" ]; then
|
|
|
|
eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \
|
|
|
|
"$f_index_file_awk" $__indexes.$__lang
|
|
|
|
)"' && return $SUCCESS
|
|
|
|
else
|
|
|
|
awk -v keyword="$__keyword" "$f_index_file_awk" \
|
|
|
|
$__indexes.$__lang && return $SUCCESS
|
|
|
|
fi
|
2013-07-05 20:13:00 +00:00
|
|
|
# No match, fall-thru to non-i18n sources
|
|
|
|
fi
|
2013-12-07 00:31:01 +00:00
|
|
|
if [ "$__var_to_set" ]; then
|
|
|
|
eval "$__var_to_set"='$( awk -v keyword="$__keyword" \
|
|
|
|
"$f_index_file_awk" $__indexes )"'
|
|
|
|
else
|
|
|
|
awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes
|
|
|
|
fi
|
2012-11-16 00:59:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 00:31:01 +00:00
|
|
|
# f_index_menusel_keyword $indexfile $pgm [$var_to_set]
|
2012-11-16 00:59:11 +00:00
|
|
|
#
|
|
|
|
# Process $indexfile and return only the keyword portion of the menu_selection
|
|
|
|
# line with a command portion matching $pgm.
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
|
|
|
# This function is for internationalization (i18n) mapping of the on-disk
|
|
|
|
# scriptname ($pgm) into the localized language (given language-specific
|
2012-11-16 00:59:11 +00:00
|
|
|
# $indexfile). If $LANG or $LC_ALL (in orderder of preference, respectively) is
|
|
|
|
# set, ".encoding" will automatically be appended as a suffix to the provided
|
|
|
|
# $indexfile pathname.
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
2012-11-16 00:59:11 +00:00
|
|
|
# If, within $indexfile, multiple $menu_selection values map to $pgm, only the
|
|
|
|
# first one will be returned. If no mapping can be made, the NULL string is
|
|
|
|
# returned.
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
2012-11-16 00:59:11 +00:00
|
|
|
# If $indexfile does not exist, error status is returned with NULL.
|
2012-07-14 03:16:57 +00:00
|
|
|
#
|
2013-12-07 00:31:01 +00:00
|
|
|
# If $var_to_set is NULL or missing, output is printed to stdout (which is less
|
|
|
|
# recommended due to performance degradation; in a loop for example).
|
|
|
|
#
|
2012-07-14 03:16:57 +00:00
|
|
|
# This function is a two-parter. Below is the awk(1) portion of the function,
|
|
|
|
# afterward is the sh(1) function which utilizes the below awk script.
|
|
|
|
#
|
2012-11-16 00:59:11 +00:00
|
|
|
f_index_menusel_keyword_awk='
|
2012-07-14 03:16:57 +00:00
|
|
|
# Variables that should be defined on the invocation line:
|
|
|
|
# -v pgm="program_name"
|
|
|
|
#
|
2012-11-16 00:59:11 +00:00
|
|
|
BEGIN {
|
|
|
|
prefix = "menu_selection=\""
|
|
|
|
plen = length(prefix)
|
|
|
|
found = 0
|
2012-07-14 03:16:57 +00:00
|
|
|
}
|
2012-11-16 00:59:11 +00:00
|
|
|
{
|
|
|
|
if (!match($0, "^" prefix ".*\\|.*\"")) next
|
|
|
|
|
|
|
|
keyword = command = substr($0, plen + 1, RLENGTH - plen - 1)
|
|
|
|
sub(/^.*\|/, "", command)
|
|
|
|
sub(/\|.*$/, "", keyword)
|
|
|
|
|
|
|
|
if ( command == pgm )
|
|
|
|
{
|
|
|
|
print keyword
|
|
|
|
found++
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END { exit ! found }
|
2012-07-14 03:16:57 +00:00
|
|
|
'
|
2012-11-16 00:59:11 +00:00
|
|
|
f_index_menusel_keyword()
|
2012-07-14 03:16:57 +00:00
|
|
|
{
|
2013-12-07 00:31:01 +00:00
|
|
|
local __indexfile="$1" __pgm="$2" __var_to_set="$3"
|
|
|
|
local __lang="${LANG:-$LC_ALL}" __file="$__indexfile"
|
2012-07-14 03:16:57 +00:00
|
|
|
|
2013-12-07 00:31:01 +00:00
|
|
|
[ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang"
|
2012-12-21 19:26:17 +00:00
|
|
|
f_dprintf "f_index_menusel_keyword: index=[%s] pgm=[%s] lang=[%s]" \
|
2013-12-07 00:31:01 +00:00
|
|
|
"$__file" "$__pgm" "$__lang"
|
|
|
|
|
|
|
|
if [ "$__var_to_set" ]; then
|
|
|
|
setvar "$__var_to_set" "$( awk \
|
|
|
|
-v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file"
|
|
|
|
)"
|
|
|
|
else
|
|
|
|
awk -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file"
|
2012-11-16 00:59:11 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-12-07 00:31:01 +00:00
|
|
|
# f_index_menusel_command $indexfile $keyword [$var_to_set]
|
2012-11-16 00:59:11 +00:00
|
|
|
#
|
|
|
|
# Process $indexfile and return only the command portion of the menu_selection
|
|
|
|
# line with a keyword portion matching $keyword.
|
|
|
|
#
|
|
|
|
# This function is for mapping [possibly international] keywords into the
|
|
|
|
# command to be executed. If $LANG or $LC_ALL (order of preference) is set,
|
|
|
|
# ".encoding" will automatically be appended as a suffix to the provided
|
|
|
|
# $indexfile pathname.
|
|
|
|
#
|
|
|
|
# If, within $indexfile, multiple $menu_selection values map to $keyword, only
|
|
|
|
# the first one will be returned. If no mapping can be made, the NULL string is
|
|
|
|
# returned.
|
|
|
|
#
|
|
|
|
# If $indexfile doesn't exist, error status is returned with NULL.
|
|
|
|
#
|
2013-12-07 00:31:01 +00:00
|
|
|
# If $var_to_set is NULL or missing, output is printed to stdout (which is less
|
|
|
|
# recommended due to performance degradation; in a loop for example).
|
|
|
|
#
|
2012-11-16 00:59:11 +00:00
|
|
|
# This function is a two-parter. Below is the awk(1) portion of the function,
|
|
|
|
# afterward is the sh(1) function which utilizes the below awk script.
|
|
|
|
#
|
|
|
|
f_index_menusel_command_awk='
|
|
|
|
# Variables that should be defined on the invocation line:
|
|
|
|
# -v key="keyword"
|
|
|
|
#
|
|
|
|
BEGIN {
|
|
|
|
prefix = "menu_selection=\""
|
|
|
|
plen = length(prefix)
|
|
|
|
found = 0
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if (!match($0, "^" prefix ".*\\|.*\"")) next
|
|
|
|
|
|
|
|
keyword = command = substr($0, plen + 1, RLENGTH - plen - 1)
|
|
|
|
sub(/^.*\|/, "", command)
|
|
|
|
sub(/\|.*$/, "", keyword)
|
|
|
|
|
|
|
|
if ( keyword == key )
|
|
|
|
{
|
|
|
|
print command
|
|
|
|
found++
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END { exit ! found }
|
|
|
|
'
|
|
|
|
f_index_menusel_command()
|
|
|
|
{
|
2013-12-07 00:31:01 +00:00
|
|
|
local __indexfile="$1" __keyword="$2" __var_to_set="$3" __command
|
|
|
|
local __lang="${LANG:-$LC_ALL}" __file="$__indexfile"
|
2012-11-16 00:59:11 +00:00
|
|
|
|
2013-12-07 00:31:01 +00:00
|
|
|
[ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang"
|
2012-12-21 19:26:17 +00:00
|
|
|
f_dprintf "f_index_menusel_command: index=[%s] key=[%s] lang=[%s]" \
|
2013-12-07 00:31:01 +00:00
|
|
|
"$__file" "$__keyword" "$__lang"
|
|
|
|
|
|
|
|
[ -f "$__file" ] || return $FAILURE
|
|
|
|
__command=$( awk -v key="$__keyword" \
|
|
|
|
"$f_index_menusel_command_awk" "$__file" ) || return $FAILURE
|
2012-11-16 00:59:11 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# If the command pathname is not fully qualified fix-up/force to be
|
|
|
|
# relative to the $indexfile directory.
|
|
|
|
#
|
2013-12-07 00:31:01 +00:00
|
|
|
case "$__command" in
|
2012-11-16 00:59:11 +00:00
|
|
|
/*) : already fully qualified ;;
|
|
|
|
*)
|
2013-12-07 00:31:01 +00:00
|
|
|
local __indexdir="${__indexfile%/*}"
|
|
|
|
[ "$__indexdir" != "$__indexfile" ] || __indexdir="."
|
|
|
|
__command="$__indexdir/$__command"
|
2012-11-16 00:59:11 +00:00
|
|
|
esac
|
|
|
|
|
2013-12-07 00:31:01 +00:00
|
|
|
if [ "$__var_to_set" ]; then
|
|
|
|
setvar "$__var_to_set" "$__command"
|
|
|
|
else
|
|
|
|
echo "$__command"
|
|
|
|
fi
|
2012-07-14 03:16:57 +00:00
|
|
|
}
|
|
|
|
|
Import media selection/preparation framework (sysinstall inspired). Makes
accessing files from various types of media nice and abstracted away from
the wet-work involved in preparing, validating, and initializing those
types of media. This will be used for the package management system module
and other modules that need access to files and want to allow the user to
decide where those files come from (either in a scripted fashion, prompted
fashion, or any combination thereof).
Heavily inspired by sysinstall and even uses the same reserved words so
that scripts are portable. Coded over months, tested continuously through-
out, and reviewed several times.
Some notes about the changes:
- Move network-setting acquisition/validation routines to media/tcpip.subr
- The options screen from sysinstall has been converted to a dialog menu
- The "UFS" media choice is renamed to "Directory" to reflect how sysinstall
treats the choice and a new [true] "UFS" media choice has been added that
acts on real UFS partitions (such as external disks with disklabels).
- Many more help files have been resurrected from sysinstall (I noticed that
some of the content seems a bit dated; I gave them a once-over but they
could really use an update).
- A total of 10 media choices are presented (via mediaGetType) including:
CD/DVD, FTP, FTP Passive, HTTP Proxy, Directory, NFS, DOS, UFS, Floppy, USB
- Novel struct/device management layer for managing the issue of passing
more information than can comfortably fit in an argument list.
2013-02-25 19:55:32 +00:00
|
|
|
# f_running_as_init
|
|
|
|
#
|
|
|
|
# Returns true if running as init(1).
|
|
|
|
#
|
|
|
|
f_running_as_init()
|
|
|
|
{
|
|
|
|
#
|
|
|
|
# When a custom init(8) performs an exec(3) to invoke a shell script,
|
|
|
|
# PID 1 becomes sh(1) and $PPID is set to 1 in the executed script.
|
|
|
|
#
|
|
|
|
[ ${PPID:-0} -eq 1 ] # Return status
|
|
|
|
}
|
|
|
|
|
|
|
|
# f_mounted $local_directory
|
2014-04-23 22:04:04 +00:00
|
|
|
# f_mounted -b $device
|
Import media selection/preparation framework (sysinstall inspired). Makes
accessing files from various types of media nice and abstracted away from
the wet-work involved in preparing, validating, and initializing those
types of media. This will be used for the package management system module
and other modules that need access to files and want to allow the user to
decide where those files come from (either in a scripted fashion, prompted
fashion, or any combination thereof).
Heavily inspired by sysinstall and even uses the same reserved words so
that scripts are portable. Coded over months, tested continuously through-
out, and reviewed several times.
Some notes about the changes:
- Move network-setting acquisition/validation routines to media/tcpip.subr
- The options screen from sysinstall has been converted to a dialog menu
- The "UFS" media choice is renamed to "Directory" to reflect how sysinstall
treats the choice and a new [true] "UFS" media choice has been added that
acts on real UFS partitions (such as external disks with disklabels).
- Many more help files have been resurrected from sysinstall (I noticed that
some of the content seems a bit dated; I gave them a once-over but they
could really use an update).
- A total of 10 media choices are presented (via mediaGetType) including:
CD/DVD, FTP, FTP Passive, HTTP Proxy, Directory, NFS, DOS, UFS, Floppy, USB
- Novel struct/device management layer for managing the issue of passing
more information than can comfortably fit in an argument list.
2013-02-25 19:55:32 +00:00
|
|
|
#
|
2014-04-23 22:04:04 +00:00
|
|
|
# Return success if a filesystem is mounted on a particular directory. If `-b'
|
|
|
|
# is present, instead check that the block device (or a partition thereof) is
|
|
|
|
# mounted.
|
Import media selection/preparation framework (sysinstall inspired). Makes
accessing files from various types of media nice and abstracted away from
the wet-work involved in preparing, validating, and initializing those
types of media. This will be used for the package management system module
and other modules that need access to files and want to allow the user to
decide where those files come from (either in a scripted fashion, prompted
fashion, or any combination thereof).
Heavily inspired by sysinstall and even uses the same reserved words so
that scripts are portable. Coded over months, tested continuously through-
out, and reviewed several times.
Some notes about the changes:
- Move network-setting acquisition/validation routines to media/tcpip.subr
- The options screen from sysinstall has been converted to a dialog menu
- The "UFS" media choice is renamed to "Directory" to reflect how sysinstall
treats the choice and a new [true] "UFS" media choice has been added that
acts on real UFS partitions (such as external disks with disklabels).
- Many more help files have been resurrected from sysinstall (I noticed that
some of the content seems a bit dated; I gave them a once-over but they
could really use an update).
- A total of 10 media choices are presented (via mediaGetType) including:
CD/DVD, FTP, FTP Passive, HTTP Proxy, Directory, NFS, DOS, UFS, Floppy, USB
- Novel struct/device management layer for managing the issue of passing
more information than can comfortably fit in an argument list.
2013-02-25 19:55:32 +00:00
|
|
|
#
|
|
|
|
f_mounted()
|
|
|
|
{
|
2014-04-23 22:04:04 +00:00
|
|
|
local OPTIND OPTARG flag use_device=
|
|
|
|
while getopts b flag; do
|
|
|
|
case "$flag" in
|
|
|
|
b) use_device=1 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(( $OPTIND - 1 ))
|
|
|
|
if [ "$use_device" ]; then
|
|
|
|
local device="$1"
|
|
|
|
mount | grep -Eq \
|
|
|
|
"^$device([[:space:]]|p[0-9]|s[0-9]|\.nop|\.eli)"
|
|
|
|
else
|
|
|
|
[ -d "$dir" ] || return $FAILURE
|
|
|
|
mount | grep -Eq " on $dir \([^)]+\)$"
|
|
|
|
fi
|
|
|
|
# Return status is that of last grep(1)
|
Import media selection/preparation framework (sysinstall inspired). Makes
accessing files from various types of media nice and abstracted away from
the wet-work involved in preparing, validating, and initializing those
types of media. This will be used for the package management system module
and other modules that need access to files and want to allow the user to
decide where those files come from (either in a scripted fashion, prompted
fashion, or any combination thereof).
Heavily inspired by sysinstall and even uses the same reserved words so
that scripts are portable. Coded over months, tested continuously through-
out, and reviewed several times.
Some notes about the changes:
- Move network-setting acquisition/validation routines to media/tcpip.subr
- The options screen from sysinstall has been converted to a dialog menu
- The "UFS" media choice is renamed to "Directory" to reflect how sysinstall
treats the choice and a new [true] "UFS" media choice has been added that
acts on real UFS partitions (such as external disks with disklabels).
- Many more help files have been resurrected from sysinstall (I noticed that
some of the content seems a bit dated; I gave them a once-over but they
could really use an update).
- A total of 10 media choices are presented (via mediaGetType) including:
CD/DVD, FTP, FTP Passive, HTTP Proxy, Directory, NFS, DOS, UFS, Floppy, USB
- Novel struct/device management layer for managing the issue of passing
more information than can comfortably fit in an argument list.
2013-02-25 19:55:32 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 00:31:01 +00:00
|
|
|
# f_eval_catch [-de] [-k $var_to_set] $funcname $utility \
|
|
|
|
# $format [$arguments ...]
|
2013-11-07 10:28:12 +00:00
|
|
|
#
|
|
|
|
# Silently evaluate a command in a sub-shell and test for error. If debugging
|
|
|
|
# is enabled a copy of the command and its output is sent to debug (either
|
|
|
|
# stdout or file depending on environment). If an error occurs, output of the
|
|
|
|
# command is displayed in a dialog(1) msgbox using the [above] f_show_err()
|
2013-12-07 00:31:01 +00:00
|
|
|
# function (unless optional `-d' flag is given, then no dialog).
|
|
|
|
#
|
2013-11-07 10:28:12 +00:00
|
|
|
# The $funcname argument is sent to debugging while the $utility argument is
|
2013-12-07 00:31:01 +00:00
|
|
|
# used in the title of the dialog box. The command that is executed as well as
|
|
|
|
# sent to debugging with $funcname is the product of the printf(1) syntax
|
|
|
|
# produced by $format with optional $arguments.
|
|
|
|
#
|
|
|
|
# The following options are supported:
|
|
|
|
#
|
|
|
|
# -d Do not use dialog(1).
|
|
|
|
# -e Produce error text from failed command on stderr.
|
|
|
|
# -k var Save output from the command in var.
|
2013-11-07 10:28:12 +00:00
|
|
|
#
|
|
|
|
# Example 1:
|
|
|
|
#
|
|
|
|
# debug=1
|
2013-12-07 00:31:01 +00:00
|
|
|
# f_eval_catch myfunc echo 'echo "%s"' "Hello, World!"
|
2013-11-07 10:28:12 +00:00
|
|
|
#
|
|
|
|
# Produces the following debug output:
|
|
|
|
#
|
2013-12-07 00:31:01 +00:00
|
|
|
# DEBUG: myfunc: echo "Hello, World!"
|
|
|
|
# DEBUG: myfunc: retval=0 <output below>
|
|
|
|
# Hello, World!
|
2013-11-07 10:28:12 +00:00
|
|
|
#
|
|
|
|
# Example 2:
|
|
|
|
#
|
|
|
|
# debug=1
|
2013-12-07 00:31:01 +00:00
|
|
|
# f_eval_catch -k contents myfunc cat 'cat "%s"' /some/file
|
|
|
|
# # dialog(1) Error ``cat: /some/file: No such file or directory''
|
|
|
|
# # contents=[cat: /some/file: No such file or directory]
|
2013-11-07 10:28:12 +00:00
|
|
|
#
|
|
|
|
# Produces the following debug output:
|
|
|
|
#
|
2013-12-07 00:31:01 +00:00
|
|
|
# DEBUG: myfunc: cat "/some/file"
|
|
|
|
# DEBUG: myfunc: retval=1 <output below>
|
|
|
|
# cat: /some/file: No such file or directory
|
2013-11-07 10:28:12 +00:00
|
|
|
#
|
|
|
|
# Example 3:
|
|
|
|
#
|
|
|
|
# debug=1
|
|
|
|
# echo 123 | f_eval_catch myfunc rev rev
|
|
|
|
#
|
|
|
|
# Produces the following debug output:
|
|
|
|
#
|
|
|
|
# DEBUG: myfunc: rev
|
|
|
|
# DEBUG: myfunc: retval=0 <output below>
|
|
|
|
# 321
|
|
|
|
#
|
|
|
|
# Example 4:
|
|
|
|
#
|
|
|
|
# debug=1
|
|
|
|
# f_eval_catch myfunc true true
|
|
|
|
#
|
|
|
|
# Produces the following debug output:
|
|
|
|
#
|
|
|
|
# DEBUG: myfunc: true
|
|
|
|
# DEBUG: myfunc: retval=0 <no output>
|
|
|
|
#
|
2013-12-07 00:31:01 +00:00
|
|
|
# Example 5:
|
|
|
|
#
|
|
|
|
# f_eval_catch -de myfunc ls 'ls "%s"' /some/dir
|
|
|
|
# # Output on stderr ``ls: /some/dir: No such file or directory''
|
|
|
|
#
|
|
|
|
# Example 6:
|
|
|
|
#
|
|
|
|
# f_eval_catch -dek contents myfunc ls 'ls "%s"' /etc
|
|
|
|
# # Output from `ls' sent to stderr and also saved in $contents
|
|
|
|
#
|
2013-11-07 10:28:12 +00:00
|
|
|
f_eval_catch()
|
|
|
|
{
|
2013-12-07 00:31:01 +00:00
|
|
|
local __no_dialog= __show_err= __var_to_set=
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process local function arguments
|
|
|
|
#
|
2014-04-23 22:04:04 +00:00
|
|
|
local OPTIND OPTARG __flag
|
2013-12-07 00:31:01 +00:00
|
|
|
while getopts "dek:" __flag > /dev/null; do
|
|
|
|
case "$__flag" in
|
|
|
|
d) __no_dialog=1 ;;
|
|
|
|
e) __show_err=1 ;;
|
|
|
|
k) __var_to_set="$OPTARG" ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(( $OPTIND - 1 ))
|
|
|
|
|
|
|
|
local __funcname="$1" __utility="$2"; shift 2
|
|
|
|
local __cmd __output __retval
|
|
|
|
|
|
|
|
__cmd=$( printf -- "$@" )
|
|
|
|
f_dprintf "%s: %s" "$__funcname" "$__cmd" # Log command *before* eval
|
|
|
|
__output=$( exec 2>&1; eval "$__cmd" )
|
|
|
|
__retval=$?
|
|
|
|
if [ "$__output" ]; then
|
|
|
|
[ "$__show_err" ] && echo "$__output" >&2
|
|
|
|
f_dprintf "%s: retval=%i <output below>\n%s" "$__funcname" \
|
|
|
|
$__retval "$__output"
|
2013-11-07 10:28:12 +00:00
|
|
|
else
|
2013-12-07 00:31:01 +00:00
|
|
|
f_dprintf "%s: retval=%i <no output>" "$__funcname" $__retval
|
2013-11-07 10:28:12 +00:00
|
|
|
fi
|
2013-12-07 00:31:01 +00:00
|
|
|
|
|
|
|
! [ "$__no_dialog" -o "$nonInteractive" -o $__retval -eq $SUCCESS ] &&
|
|
|
|
msg_error="${msg_error:-Error}${__utility:+: $__utility}" \
|
|
|
|
f_show_err "%s" "$__output"
|
2013-11-07 10:28:12 +00:00
|
|
|
# NB: f_show_err will handle NULL output appropriately
|
2013-12-07 00:31:01 +00:00
|
|
|
|
|
|
|
[ "$__var_to_set" ] && setvar "$__var_to_set" "$__output"
|
|
|
|
|
|
|
|
return $__retval
|
|
|
|
}
|
|
|
|
|
|
|
|
# f_count $var_to_set arguments ...
|
|
|
|
#
|
|
|
|
# Sets $var_to_set to the number of arguments minus one (the effective number
|
|
|
|
# of arguments following $var_to_set).
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# f_count count dog house # count=[2]
|
|
|
|
#
|
|
|
|
f_count()
|
|
|
|
{
|
|
|
|
setvar "$1" $(( $# - 1 ))
|
|
|
|
}
|
|
|
|
|
|
|
|
# f_count_ifs $var_to_set string ...
|
|
|
|
#
|
|
|
|
# Sets $var_to_set to the number of words (split by the internal field
|
|
|
|
# separator, IFS) following $var_to_set.
|
|
|
|
#
|
|
|
|
# Example 1:
|
|
|
|
#
|
|
|
|
# string="word1 word2 word3"
|
|
|
|
# f_count_ifs count "$string" # count=[3]
|
|
|
|
# f_count_ifs count $string # count=[3]
|
|
|
|
#
|
|
|
|
# Example 2:
|
|
|
|
#
|
|
|
|
# IFS=. f_count_ifs count www.freebsd.org # count=[3]
|
|
|
|
#
|
|
|
|
# NB: Make sure to use double-quotes if you are using a custom value for IFS
|
|
|
|
# and you don't want the current value to effect the result. See example 3.
|
|
|
|
#
|
|
|
|
# Example 3:
|
|
|
|
#
|
|
|
|
# string="a-b c-d"
|
|
|
|
# IFS=- f_count_ifs count "$string" # count=[3]
|
|
|
|
# IFS=- f_count_ifs count $string # count=[4]
|
|
|
|
#
|
|
|
|
f_count_ifs()
|
|
|
|
{
|
|
|
|
local __var_to_set="$1"
|
|
|
|
shift 1
|
|
|
|
set -- $*
|
|
|
|
setvar "$__var_to_set" $#
|
2013-11-07 10:28:12 +00:00
|
|
|
}
|
|
|
|
|
2012-07-14 03:16:57 +00:00
|
|
|
############################################################ MAIN
|
|
|
|
|
|
|
|
#
|
|
|
|
# Trap signals so we can recover gracefully
|
|
|
|
#
|
|
|
|
trap 'f_interrupt' SIGINT
|
|
|
|
trap 'f_die' SIGTERM SIGPIPE SIGXCPU SIGXFSZ \
|
|
|
|
SIGFPE SIGTRAP SIGABRT SIGSEGV
|
|
|
|
trap '' SIGALRM SIGPROF SIGUSR1 SIGUSR2 SIGHUP SIGVTALRM
|
|
|
|
|
2012-12-21 19:26:17 +00:00
|
|
|
#
|
|
|
|
# Clone terminal stdout/stderr so we can redirect to it from within sub-shells
|
|
|
|
#
|
|
|
|
eval exec $TERMINAL_STDOUT_PASSTHRU\>\&1
|
|
|
|
eval exec $TERMINAL_STDERR_PASSTHRU\>\&2
|
|
|
|
|
2012-12-25 10:47:45 +00:00
|
|
|
#
|
2013-04-22 05:02:34 +00:00
|
|
|
# Self-initialize unless requested otherwise
|
2012-12-25 10:47:45 +00:00
|
|
|
#
|
2013-04-22 05:02:34 +00:00
|
|
|
f_dprintf "%s: DEBUG_SELF_INITIALIZE=[%s]" \
|
|
|
|
dialog.subr "$DEBUG_SELF_INITIALIZE"
|
|
|
|
case "$DEBUG_SELF_INITIALIZE" in
|
|
|
|
""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
|
|
|
|
*) f_debug_init
|
|
|
|
esac
|
2012-12-28 23:49:17 +00:00
|
|
|
|
2012-12-25 10:47:45 +00:00
|
|
|
#
|
|
|
|
# Log our operating environment for debugging purposes
|
|
|
|
#
|
|
|
|
f_dprintf "UNAME_S=[%s] UNAME_P=[%s] UNAME_R=[%s]" \
|
|
|
|
"$UNAME_S" "$UNAME_P" "$UNAME_R"
|
|
|
|
|
|
|
|
f_dprintf "%s: Successfully loaded." common.subr
|
|
|
|
|
2012-07-14 03:16:57 +00:00
|
|
|
fi # ! $_COMMON_SUBR
|