1252995Sdteskeif [ ! "$_COMMON_SUBR" ]; then _COMMON_SUBR=1
2252995Sdteske#
3252995Sdteske# Copyright (c) 2012 Ron McDowell
4263791Sdteske# Copyright (c) 2012-2014 Devin Teske
5252995Sdteske# All rights reserved.
6252995Sdteske#
7252995Sdteske# Redistribution and use in source and binary forms, with or without
8252995Sdteske# modification, are permitted provided that the following conditions
9252995Sdteske# are met:
10252995Sdteske# 1. Redistributions of source code must retain the above copyright
11252995Sdteske#    notice, this list of conditions and the following disclaimer.
12252995Sdteske# 2. Redistributions in binary form must reproduce the above copyright
13252995Sdteske#    notice, this list of conditions and the following disclaimer in the
14252995Sdteske#    documentation and/or other materials provided with the distribution.
15252995Sdteske#
16252995Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17252995Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18252995Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19252995Sdteske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20252995Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21252995Sdteske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22252995Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23252995Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24252995Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25252995Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26252995Sdteske# SUCH DAMAGE.
27252995Sdteske#
28252995Sdteske# $FreeBSD$
29252995Sdteske#
30252995Sdteske############################################################ CONFIGURATION
31252995Sdteske
32252995Sdteske#
33252995Sdteske# Default file descriptors to link to stdout/stderr for passthru allowing
34252995Sdteske# redirection within a sub-shell to bypass directly to the terminal.
35252995Sdteske#
36252995Sdteske: ${TERMINAL_STDOUT_PASSTHRU:=3}}
37252995Sdteske: ${TERMINAL_STDERR_PASSTHRU:=4}}
38252995Sdteske
39252995Sdteske############################################################ GLOBALS
40252995Sdteske
41252995Sdteske#
42252995Sdteske# Program name
43252995Sdteske#
44252995Sdteskepgm="${0##*/}"
45252995Sdteske
46252995Sdteske#
47252995Sdteske# Program arguments
48252995Sdteske#
49252995SdteskeARGC="$#"
50252995SdteskeARGV="$@"
51252995Sdteske
52252995Sdteske#
53252995Sdteske# Global exit status variables
54252995Sdteske#
55252995SdteskeSUCCESS=0
56252995SdteskeFAILURE=1
57252995Sdteske
58252995Sdteske#
59252995Sdteske# Operating environment details
60252995Sdteske#
61263791Sdteskeexport UNAME_S="$( uname -s )" # Operating System (i.e. FreeBSD)
62263791Sdteskeexport UNAME_P="$( uname -p )" # Processor Architecture (i.e. i386)
63267892Sgjbexport UNAME_M="$( uname -m )" # Machine platform (i.e. i386)
64263791Sdteskeexport UNAME_R="$( uname -r )" # Release Level (i.e. X.Y-RELEASE)
65267892Sgjbif [ ! "${PKG_ABI+set}" ]; then
66267892Sgjb	export PKG_ABI="$(
67267892Sgjb		ASSUME_ALWAYS_YES=1 pkg -vv 2> /dev/null |
68267892Sgjb			awk '$1=="ABI"{print $3;exit}'
69267892Sgjb	)"
70267892Sgjbfi
71252995Sdteske
72252995Sdteske#
73252995Sdteske# Default behavior is to call f_debug_init() automatically when loaded.
74252995Sdteske#
75252995Sdteske: ${DEBUG_SELF_INITIALIZE=1}
76252995Sdteske
77252995Sdteske#
78263791Sdteske# Default behavior of f_debug_init() is to truncate $debugFile (set to NULL to
79263791Sdteske# disable truncating the debug file when initializing). To get child processes
80263791Sdteske# to append to the same log file, export this variarable (with a NULL value)
81263791Sdteske# and also export debugFile with the desired value.
82263791Sdteske# 
83263791Sdteske: ${DEBUG_INITIALIZE_FILE=1}
84263791Sdteske
85263791Sdteske#
86252995Sdteske# Define standard optstring arguments that should be supported by all programs
87252995Sdteske# using this include (unless DEBUG_SELF_INITIALIZE is set to NULL to prevent
88252995Sdteske# f_debug_init() from autamatically processing "$@" for the below arguments):
89252995Sdteske#
90252995Sdteske# 	d	Sets $debug to 1
91252995Sdteske# 	D:	Sets $debugFile to $OPTARG
92252995Sdteske#
93252995SdteskeGETOPTS_STDARGS="dD:"
94252995Sdteske
95252995Sdteske#
96252995Sdteske# The getopts builtin will return 1 either when the end of "$@" or the first
97252995Sdteske# invalid flag is reached. This makes it impossible to determine if you've
98252995Sdteske# processed all the arguments or simply have hit an invalid flag. In the cases
99252995Sdteske# where we want to tolerate invalid flags (f_debug_init() for example), the
100252995Sdteske# following variable can be appended to your optstring argument to getopts,
101252995Sdteske# preventing it from prematurely returning 1 before the end of the arguments.
102252995Sdteske#
103252995Sdteske# NOTE: This assumes that all unknown flags are argument-less.
104252995Sdteske#
105252995SdteskeGETOPTS_ALLFLAGS="abcdefghijklmnopqrstuvwxyz"
106252995SdteskeGETOPTS_ALLFLAGS="${GETOPTS_ALLFLAGS}ABCDEFGHIJKLMNOPQRSTUVWXYZ"
107252995SdteskeGETOPTS_ALLFLAGS="${GETOPTS_ALLFLAGS}0123456789"
108252995Sdteske
109252995Sdteske#
110252995Sdteske# When we get included, f_debug_init() will fire (unless $DEBUG_SELF_INITIALIZE
111252995Sdteske# is set to disable automatic initialization) and process "$@" for a few global
112252995Sdteske# options such as `-d' and/or `-D file'. However, if your program takes custom
113252995Sdteske# flags that take arguments, this automatic processing may fail unexpectedly.
114252995Sdteske#
115252995Sdteske# The solution to this problem is to pre-define (before including this file)
116252995Sdteske# the following variable (which defaults to NULL) to indicate that there are
117252995Sdteske# extra flags that should be considered when performing automatic processing of
118252995Sdteske# globally persistent flags.
119252995Sdteske#
120252995Sdteske: ${GETOPTS_EXTRA:=}
121252995Sdteske
122252995Sdteske############################################################ FUNCTIONS
123252995Sdteske
124263791Sdteske# f_dprintf $format [$arguments ...]
125252995Sdteske#
126252995Sdteske# Sensible debug function. Override in ~/.bsdconfigrc if desired.
127252995Sdteske# See /usr/share/examples/bsdconfig/bsdconfigrc for example.
128252995Sdteske#
129252995Sdteske# If $debug is set and non-NULL, prints DEBUG info using printf(1) syntax:
130252995Sdteske# 	+ To $debugFile, if set and non-NULL
131252995Sdteske# 	+ To standard output if $debugFile is either NULL or unset
132252995Sdteske# 	+ To both if $debugFile begins with a single plus-sign (`+')
133252995Sdteske#
134252995Sdteskef_dprintf()
135252995Sdteske{
136252995Sdteske	[ "$debug" ] || return $SUCCESS
137252995Sdteske	local fmt="$1"; shift
138252995Sdteske	case "$debugFile" in ""|+*)
139252995Sdteske	printf "DEBUG: $fmt${fmt:+\n}" "$@" >&${TERMINAL_STDOUT_PASSTHRU:-1}
140252995Sdteske	esac
141252995Sdteske	[ "${debugFile#+}" ] &&
142252995Sdteske		printf "DEBUG: $fmt${fmt:+\n}" "$@" >> "${debugFile#+}"
143252995Sdteske	return $SUCCESS
144252995Sdteske}
145252995Sdteske
146252995Sdteske# f_debug_init
147252995Sdteske#
148252995Sdteske# Initialize debugging. Truncates $debugFile to zero bytes if set.
149252995Sdteske#
150252995Sdteskef_debug_init()
151252995Sdteske{
152252995Sdteske	#
153252995Sdteske	# Process stored command-line arguments
154252995Sdteske	#
155252995Sdteske	set -- $ARGV
156266292Sdteske	local OPTIND OPTARG flag
157252995Sdteske	f_dprintf "f_debug_init: ARGV=[%s] GETOPTS_STDARGS=[%s]" \
158252995Sdteske	          "$ARGV" "$GETOPTS_STDARGS"
159252995Sdteske	while getopts "$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" flag \
160252995Sdteske	> /dev/null; do
161252995Sdteske		case "$flag" in
162252995Sdteske		d) debug=1 ;;
163252995Sdteske		D) debugFile="$OPTARG" ;;
164252995Sdteske		esac
165252995Sdteske	done
166252995Sdteske	shift $(( $OPTIND - 1 ))
167252995Sdteske	f_dprintf "f_debug_init: debug=[%s] debugFile=[%s]" \
168252995Sdteske	          "$debug" "$debugFile"
169252995Sdteske
170252995Sdteske	#
171252995Sdteske	# Automagically enable debugging if debugFile is set (and non-NULL)
172252995Sdteske	#
173252995Sdteske	[ "$debugFile" ] && { [ "${debug+set}" ] || debug=1; }
174252995Sdteske
175252995Sdteske	#
176252995Sdteske	# Make debugging persistant if set
177252995Sdteske	#
178252995Sdteske	[ "$debug" ] && export debug
179252995Sdteske	[ "$debugFile" ] && export debugFile
180252995Sdteske
181252995Sdteske	#
182263791Sdteske	# Truncate debug file unless requested otherwise. Note that we will
183263791Sdteske	# trim a leading plus (`+') from the value of debugFile to support
184263791Sdteske	# persistant meaning that f_dprintf() should print both to standard
185263791Sdteske	# output and $debugFile (minus the leading plus, of course).
186252995Sdteske	#
187252995Sdteske	local _debug_file="${debugFile#+}"
188263791Sdteske	if [ "$_debug_file" -a "$DEBUG_INITIALIZE_FILE" ]; then
189252995Sdteske		if ( umask 022 && :> "$_debug_file" ); then
190252995Sdteske			f_dprintf "Successfully initialized debugFile \`%s'" \
191252995Sdteske			          "$_debug_file"
192263791Sdteske			f_isset debug || debug=1 # turn debugging on if not set
193252995Sdteske		else
194252995Sdteske			unset debugFile
195252995Sdteske			f_dprintf "Unable to initialize debugFile \`%s'" \
196252995Sdteske			          "$_debug_file"
197252995Sdteske		fi
198252995Sdteske	fi
199252995Sdteske}
200252995Sdteske
201263791Sdteske# f_err $format [$arguments ...]
202252995Sdteske#
203252995Sdteske# Print a message to stderr (fd=2).
204252995Sdteske#
205252995Sdteskef_err()
206252995Sdteske{
207263791Sdteske	printf "$@" >&2
208252995Sdteske}
209252995Sdteske
210263791Sdteske# f_quietly $command [$arguments ...]
211252995Sdteske#
212252995Sdteske# Run a command quietly (quell any output to stdout or stderr)
213252995Sdteske#
214252995Sdteskef_quietly()
215252995Sdteske{
216252995Sdteske	"$@" > /dev/null 2>&1
217252995Sdteske}
218252995Sdteske
219252995Sdteske# f_have $anything ...
220252995Sdteske#
221252995Sdteske# A wrapper to the `type' built-in. Returns true if argument is a valid shell
222252995Sdteske# built-in, keyword, or externally-tracked binary, otherwise false.
223252995Sdteske#
224252995Sdteskef_have()
225252995Sdteske{
226252995Sdteske	f_quietly type "$@"
227252995Sdteske}
228252995Sdteske
229252995Sdteske# f_getvar $var_to_get [$var_to_set]
230252995Sdteske#
231252995Sdteske# Utility function designed to go along with the already-builtin setvar.
232252995Sdteske# Allows clean variable name indirection without forking or sub-shells.
233252995Sdteske#
234252995Sdteske# Returns error status if the requested variable ($var_to_get) is not set.
235252995Sdteske#
236252995Sdteske# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
237252995Sdteske# standard output for capturing in a sub-shell (which is less-recommended
238252995Sdteske# because of performance degredation; for example, when called in a loop).
239252995Sdteske#
240252995Sdteskef_getvar()
241252995Sdteske{
242252995Sdteske	local __var_to_get="$1" __var_to_set="$2"
243252995Sdteske	[ "$__var_to_set" ] || local value
244252995Sdteske	eval [ \"\${$__var_to_get+set}\" ]
245252995Sdteske	local __retval=$?
246263791Sdteske	eval ${__var_to_set:-value}=\"\${$__var_to_get}\"
247252995Sdteske	eval f_dprintf '"f_getvar: var=[%s] value=[%s] r=%u"' \
248252995Sdteske		\"\$__var_to_get\" \"\$${__var_to_set:-value}\" \$__retval
249252995Sdteske	[ "$__var_to_set" ] || { [ "$value" ] && echo "$value"; }
250252995Sdteske	return $__retval
251252995Sdteske}
252252995Sdteske
253252995Sdteske# f_isset $var
254252995Sdteske#
255252995Sdteske# Check if variable $var is set. Returns success if variable is set, otherwise
256252995Sdteske# returns failure.
257252995Sdteske#
258252995Sdteskef_isset()
259252995Sdteske{
260252995Sdteske	eval [ \"\${${1%%[$IFS]*}+set}\" ]
261252995Sdteske}
262252995Sdteske
263263791Sdteske# f_die [$status [$format [$arguments ...]]]
264252995Sdteske#
265252995Sdteske# Abruptly terminate due to an error optionally displaying a message in a
266252995Sdteske# dialog box using printf(1) syntax.
267252995Sdteske#
268252995Sdteskef_die()
269252995Sdteske{
270252995Sdteske	local status=$FAILURE
271252995Sdteske
272252995Sdteske	# If there is at least one argument, take it as the status
273252995Sdteske	if [ $# -gt 0 ]; then
274252995Sdteske		status=$1
275252995Sdteske		shift 1 # status
276252995Sdteske	fi
277252995Sdteske
278252995Sdteske	# If there are still arguments left, pass them to f_show_msg
279252995Sdteske	[ $# -gt 0 ] && f_show_msg "$@"
280252995Sdteske
281252995Sdteske	# Optionally call f_clean_up() function if it exists
282252995Sdteske	f_have f_clean_up && f_clean_up
283252995Sdteske
284252995Sdteske	exit $status
285252995Sdteske}
286252995Sdteske
287252995Sdteske# f_interrupt
288252995Sdteske#
289252995Sdteske# Interrupt handler.
290252995Sdteske#
291252995Sdteskef_interrupt()
292252995Sdteske{
293252995Sdteske	exec 2>&1 # fix sh(1) bug where stderr gets lost within async-trap
294252995Sdteske	f_die
295252995Sdteske}
296252995Sdteske
297263791Sdteske# f_show_info $format [$arguments ...]
298252995Sdteske#
299252995Sdteske# Display a message in a dialog infobox using printf(1) syntax.
300252995Sdteske#
301252995Sdteskef_show_info()
302252995Sdteske{
303252995Sdteske	local msg
304252995Sdteske	msg=$( printf "$@" )
305252995Sdteske
306252995Sdteske	#
307252995Sdteske	# Use f_dialog_infobox from dialog.subr if possible, otherwise fall
308252995Sdteske	# back to dialog(1) (without options, making it obvious when using
309252995Sdteske	# un-aided system dialog).
310252995Sdteske	#
311252995Sdteske	if f_have f_dialog_info; then
312252995Sdteske		f_dialog_info "$msg"
313252995Sdteske	else
314252995Sdteske		dialog --infobox "$msg" 0 0
315252995Sdteske	fi
316252995Sdteske}
317252995Sdteske
318263791Sdteske# f_show_msg $format [$arguments ...]
319252995Sdteske#
320252995Sdteske# Display a message in a dialog box using printf(1) syntax.
321252995Sdteske#
322252995Sdteskef_show_msg()
323252995Sdteske{
324252995Sdteske	local msg
325252995Sdteske	msg=$( printf "$@" )
326252995Sdteske
327252995Sdteske	#
328252995Sdteske	# Use f_dialog_msgbox from dialog.subr if possible, otherwise fall
329252995Sdteske	# back to dialog(1) (without options, making it obvious when using
330252995Sdteske	# un-aided system dialog).
331252995Sdteske	#
332252995Sdteske	if f_have f_dialog_msgbox; then
333252995Sdteske		f_dialog_msgbox "$msg"
334252995Sdteske	else
335252995Sdteske		dialog --msgbox "$msg" 0 0
336252995Sdteske	fi
337252995Sdteske}
338252995Sdteske
339263791Sdteske# f_show_err $format [$arguments ...]
340263791Sdteske#
341263791Sdteske# Display a message in a dialog box with ``Error'' i18n title (overridden by
342263791Sdteske# setting msg_error) using printf(1) syntax.
343263791Sdteske#
344263791Sdteskef_show_err()
345263791Sdteske{
346263791Sdteske	local msg
347263791Sdteske	msg=$( printf "$@" )
348252995Sdteske
349263791Sdteske	: ${msg:=${msg_an_unknown_error_occurred:-An unknown error occurred}}
350263791Sdteske
351263791Sdteske	if [ "$_DIALOG_SUBR" ]; then
352263791Sdteske		f_dialog_title "${msg_error:-Error}"
353263791Sdteske		f_dialog_msgbox "$msg"
354263791Sdteske		f_dialog_title_restore
355263791Sdteske	else
356263791Sdteske		dialog --title "${msg_error:-Error}" --msgbox "$msg" 0 0
357263791Sdteske	fi
358263791Sdteske	return $SUCCESS
359263791Sdteske}
360263791Sdteske
361263791Sdteske# f_yesno $format [$arguments ...]
362252995Sdteske#
363252995Sdteske# Display a message in a dialog yes/no box using printf(1) syntax.
364252995Sdteske#
365252995Sdteskef_yesno()
366252995Sdteske{
367252995Sdteske	local msg
368252995Sdteske	msg=$( printf "$@" )
369252995Sdteske
370252995Sdteske	#
371252995Sdteske	# Use f_dialog_yesno from dialog.subr if possible, otherwise fall
372252995Sdteske	# back to dialog(1) (without options, making it obvious when using
373252995Sdteske	# un-aided system dialog).
374252995Sdteske	#
375252995Sdteske	if f_have f_dialog_yesno; then
376252995Sdteske		f_dialog_yesno "$msg"
377252995Sdteske	else
378252995Sdteske		dialog --yesno "$msg" 0 0
379252995Sdteske	fi
380252995Sdteske}
381252995Sdteske
382263791Sdteske# f_noyes $format [$arguments ...]
383252995Sdteske#
384252995Sdteske# Display a message in a dialog yes/no box using printf(1) syntax.
385252995Sdteske# NOTE: THis is just like the f_yesno function except "No" is default.
386252995Sdteske#
387252995Sdteskef_noyes()
388252995Sdteske{
389252995Sdteske	local msg
390252995Sdteske	msg=$( printf "$@" )
391252995Sdteske
392252995Sdteske	#
393252995Sdteske	# Use f_dialog_noyes from dialog.subr if possible, otherwise fall
394252995Sdteske	# back to dialog(1) (without options, making it obvious when using
395252995Sdteske	# un-aided system dialog).
396252995Sdteske	#
397252995Sdteske	if f_have f_dialog_noyes; then
398252995Sdteske		f_dialog_noyes "$msg"
399252995Sdteske	else
400252995Sdteske		dialog --defaultno --yesno "$msg" 0 0
401252995Sdteske	fi
402252995Sdteske}
403252995Sdteske
404252995Sdteske# f_show_help $file
405252995Sdteske#
406252995Sdteske# Display a language help-file. Automatically takes $LANG and $LC_ALL into
407252995Sdteske# consideration when displaying $file (suffix ".$LC_ALL" or ".$LANG" will
408252995Sdteske# automatically be added prior to loading the language help-file).
409252995Sdteske#
410252995Sdteske# If a language has been requested by setting either $LANG or $LC_ALL in the
411252995Sdteske# environment and the language-specific help-file does not exist we will fall
412252995Sdteske# back to $file without-suffix.
413252995Sdteske#
414252995Sdteske# If the language help-file does not exist, an error is displayed instead.
415252995Sdteske#
416252995Sdteskef_show_help()
417252995Sdteske{
418252995Sdteske	local file="$1"
419252995Sdteske	local lang="${LANG:-$LC_ALL}"
420252995Sdteske
421252995Sdteske	[ -f "$file.$lang" ] && file="$file.$lang"
422252995Sdteske
423252995Sdteske	#
424252995Sdteske	# Use f_dialog_textbox from dialog.subr if possible, otherwise fall
425252995Sdteske	# back to dialog(1) (without options, making it obvious when using
426252995Sdteske	# un-aided system dialog).
427252995Sdteske	#
428252995Sdteske	if f_have f_dialog_textbox; then
429252995Sdteske		f_dialog_textbox "$file"
430252995Sdteske	else
431252995Sdteske		dialog --msgbox "$( cat "$file" 2>&1 )" 0 0
432252995Sdteske	fi
433252995Sdteske}
434252995Sdteske
435252995Sdteske# f_include $file
436252995Sdteske#
437252995Sdteske# Include a shell subroutine file.
438252995Sdteske#
439252995Sdteske# If the subroutine file exists but returns error status during loading, exit
440252995Sdteske# is called and execution is prematurely terminated with the same error status.
441252995Sdteske#
442252995Sdteskef_include()
443252995Sdteske{
444252995Sdteske	local file="$1"
445252995Sdteske	f_dprintf "f_include: file=[%s]" "$file"
446252995Sdteske	. "$file" || exit $?
447252995Sdteske}
448252995Sdteske
449252995Sdteske# f_include_lang $file
450252995Sdteske#
451252995Sdteske# Include a language file. Automatically takes $LANG and $LC_ALL into
452252995Sdteske# consideration when including $file (suffix ".$LC_ALL" or ".$LANG" will
453252995Sdteske# automatically by added prior to loading the language file).
454252995Sdteske#
455252995Sdteske# No error is produced if (a) a language has been requested (by setting either
456252995Sdteske# $LANG or $LC_ALL in the environment) and (b) the language file does not
457252995Sdteske# exist -- in which case we will fall back to loading $file without-suffix.
458252995Sdteske#
459252995Sdteske# If the language file exists but returns error status during loading, exit
460252995Sdteske# is called and execution is prematurely terminated with the same error status.
461252995Sdteske#
462252995Sdteskef_include_lang()
463252995Sdteske{
464252995Sdteske	local file="$1"
465252995Sdteske	local lang="${LANG:-$LC_ALL}"
466252995Sdteske
467252995Sdteske	f_dprintf "f_include_lang: file=[%s] lang=[%s]" "$file" "$lang"
468252995Sdteske	if [ -f "$file.$lang" ]; then
469252995Sdteske		. "$file.$lang" || exit $?
470252995Sdteske	else
471252995Sdteske		. "$file" || exit $?
472252995Sdteske	fi
473252995Sdteske}
474252995Sdteske
475263791Sdteske# f_usage $file [$key1 $value1 ...]
476252995Sdteske#
477252995Sdteske# Display USAGE file with optional pre-processor macro definitions. The first
478252995Sdteske# argument is the template file containing the usage text to be displayed. If
479252995Sdteske# $LANG or $LC_ALL (in order of preference, respectively) is set, ".encoding"
480252995Sdteske# will automatically be appended as a suffix to the provided $file pathname.
481252995Sdteske#
482252995Sdteske# When processing $file, output begins at the first line containing that is
483252995Sdteske# (a) not a comment, (b) not empty, and (c) is not pure-whitespace. All lines
484252995Sdteske# appearing after this first-line are output, including (a) comments (b) empty
485252995Sdteske# lines, and (c) lines that are purely whitespace-only.
486252995Sdteske#
487252995Sdteske# If additional arguments appear after $file, substitutions are made while
488252995Sdteske# printing the contents of the USAGE file. The pre-processor macro syntax is in
489252995Sdteske# the style of autoconf(1), for example:
490252995Sdteske#
491252995Sdteske# 	f_usage $file "FOO" "BAR"
492252995Sdteske#
493252995Sdteske# Will cause instances of "@FOO@" appearing in $file to be replaced with the
494263791Sdteske# text "BAR" before being printed to the screen.
495252995Sdteske#
496252995Sdteske# This function is a two-parter. Below is the awk(1) portion of the function,
497252995Sdteske# afterward is the sh(1) function which utilizes the below awk script.
498252995Sdteske#
499252995Sdteskef_usage_awk='
500252995SdteskeBEGIN { found = 0 }
501252995Sdteske{
502252995Sdteske	if ( !found && $0 ~ /^[[:space:]]*($|#)/ ) next
503252995Sdteske	found = 1
504252995Sdteske	print
505252995Sdteske}
506252995Sdteske'
507252995Sdteskef_usage()
508252995Sdteske{
509252995Sdteske	local file="$1"
510252995Sdteske	local lang="${LANG:-$LC_ALL}"
511252995Sdteske
512252995Sdteske	f_dprintf "f_usage: file=[%s] lang=[%s]" "$file" "$lang"
513252995Sdteske
514252995Sdteske	shift 1 # file
515252995Sdteske
516252995Sdteske	local usage
517252995Sdteske	if [ -f "$file.$lang" ]; then
518252995Sdteske		usage=$( awk "$f_usage_awk" "$file.$lang" ) || exit $FAILURE
519252995Sdteske	else
520252995Sdteske		usage=$( awk "$f_usage_awk" "$file" ) || exit $FAILURE
521252995Sdteske	fi
522252995Sdteske
523252995Sdteske	while [ $# -gt 0 ]; do
524252995Sdteske		local key="$1"
525252995Sdteske		export value="$2"
526252995Sdteske		usage=$( echo "$usage" | awk \
527252995Sdteske			"{ gsub(/@$key@/, ENVIRON[\"value\"]); print }" )
528252995Sdteske		shift 2
529252995Sdteske	done
530252995Sdteske
531252995Sdteske	f_err "%s\n" "$usage"
532252995Sdteske
533252995Sdteske	exit $FAILURE
534252995Sdteske}
535252995Sdteske
536263791Sdteske# f_index_file $keyword [$var_to_set]
537252995Sdteske#
538252995Sdteske# Process all INDEX files known to bsdconfig and return the path to first file
539252995Sdteske# containing a menu_selection line with a keyword portion matching $keyword.
540252995Sdteske#
541252995Sdteske# If $LANG or $LC_ALL (in order of preference, respectively) is set,
542252995Sdteske# "INDEX.encoding" files will be searched first.
543252995Sdteske#
544252995Sdteske# If no file is found, error status is returned along with the NULL string.
545252995Sdteske#
546263791Sdteske# If $var_to_set is NULL or missing, output is printed to stdout (which is less
547263791Sdteske# recommended due to performance degradation; in a loop for example).
548263791Sdteske#
549252995Sdteske# This function is a two-parter. Below is the awk(1) portion of the function,
550252995Sdteske# afterward is the sh(1) function which utilizes the below awk script.
551252995Sdteske#
552252995Sdteskef_index_file_awk='
553252995Sdteske# Variables that should be defined on the invocation line:
554252995Sdteske# 	-v keyword="keyword"
555252995SdteskeBEGIN { found = 0 }
556252995Sdteske( $0 ~ "^menu_selection=\"" keyword "\\|" ) {
557252995Sdteske	print FILENAME
558252995Sdteske	found++
559252995Sdteske	exit
560252995Sdteske}
561252995SdteskeEND { exit ! found }
562252995Sdteske'
563252995Sdteskef_index_file()
564252995Sdteske{
565263791Sdteske	local __keyword="$1" __var_to_set="$2"
566263791Sdteske	local __lang="${LANG:-$LC_ALL}"
567263791Sdteske	local __indexes="$BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX"
568252995Sdteske
569263791Sdteske	f_dprintf "f_index_file: keyword=[%s] lang=[%s]" "$__keyword" "$__lang"
570252995Sdteske
571263791Sdteske	if [ "$__lang" ]; then
572263791Sdteske		if [ "$__var_to_set" ]; then
573263791Sdteske			eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \
574263791Sdteske				"$f_index_file_awk" $__indexes.$__lang
575263791Sdteske			)"' && return $SUCCESS
576263791Sdteske		else
577263791Sdteske			awk -v keyword="$__keyword" "$f_index_file_awk" \
578263791Sdteske				$__indexes.$__lang && return $SUCCESS
579263791Sdteske		fi
580252995Sdteske		# No match, fall-thru to non-i18n sources
581252995Sdteske	fi
582263791Sdteske	if [ "$__var_to_set" ]; then
583263791Sdteske		eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \
584263791Sdteske			"$f_index_file_awk" $__indexes )"' && return $SUCCESS
585263791Sdteske	else
586263791Sdteske		awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes &&
587263791Sdteske			return $SUCCESS
588263791Sdteske	fi
589252995Sdteske
590252995Sdteske	# No match? Fall-thru to `local' libexec sources (add-on modules)
591252995Sdteske
592252995Sdteske	[ "$BSDCFG_LOCAL_LIBE" ] || return $FAILURE
593263791Sdteske	__indexes="$BSDCFG_LOCAL_LIBE/*/INDEX"
594263791Sdteske	if [ "$__lang" ]; then
595263791Sdteske		if [ "$__var_to_set" ]; then
596263791Sdteske			eval "$__var_to_set"='"$( awk -v keyword="$__keyword" \
597263791Sdteske				"$f_index_file_awk" $__indexes.$__lang
598263791Sdteske			)"' && return $SUCCESS
599263791Sdteske		else
600263791Sdteske			awk -v keyword="$__keyword" "$f_index_file_awk" \
601263791Sdteske				$__indexes.$__lang && return $SUCCESS
602263791Sdteske		fi
603252995Sdteske		# No match, fall-thru to non-i18n sources
604252995Sdteske	fi
605263791Sdteske	if [ "$__var_to_set" ]; then
606263791Sdteske		eval "$__var_to_set"='$( awk -v keyword="$__keyword" \
607263791Sdteske			"$f_index_file_awk" $__indexes )"'
608263791Sdteske	else
609263791Sdteske		awk -v keyword="$__keyword" "$f_index_file_awk" $__indexes
610263791Sdteske	fi
611252995Sdteske}
612252995Sdteske
613263791Sdteske# f_index_menusel_keyword $indexfile $pgm [$var_to_set]
614252995Sdteske#
615252995Sdteske# Process $indexfile and return only the keyword portion of the menu_selection
616252995Sdteske# line with a command portion matching $pgm.
617252995Sdteske#
618252995Sdteske# This function is for internationalization (i18n) mapping of the on-disk
619252995Sdteske# scriptname ($pgm) into the localized language (given language-specific
620252995Sdteske# $indexfile). If $LANG or $LC_ALL (in orderder of preference, respectively) is
621252995Sdteske# set, ".encoding" will automatically be appended as a suffix to the provided
622252995Sdteske# $indexfile pathname.
623252995Sdteske#
624252995Sdteske# If, within $indexfile, multiple $menu_selection values map to $pgm, only the
625252995Sdteske# first one will be returned. If no mapping can be made, the NULL string is
626252995Sdteske# returned.
627252995Sdteske#
628252995Sdteske# If $indexfile does not exist, error status is returned with NULL.
629252995Sdteske#
630263791Sdteske# If $var_to_set is NULL or missing, output is printed to stdout (which is less
631263791Sdteske# recommended due to performance degradation; in a loop for example).
632263791Sdteske#
633252995Sdteske# This function is a two-parter. Below is the awk(1) portion of the function,
634252995Sdteske# afterward is the sh(1) function which utilizes the below awk script.
635252995Sdteske#
636252995Sdteskef_index_menusel_keyword_awk='
637252995Sdteske# Variables that should be defined on the invocation line:
638252995Sdteske# 	-v pgm="program_name"
639252995Sdteske#
640252995SdteskeBEGIN {
641252995Sdteske	prefix = "menu_selection=\""
642252995Sdteske	plen = length(prefix)
643252995Sdteske	found = 0
644252995Sdteske}
645252995Sdteske{
646252995Sdteske	if (!match($0, "^" prefix ".*\\|.*\"")) next
647252995Sdteske
648252995Sdteske	keyword = command = substr($0, plen + 1, RLENGTH - plen - 1)
649252995Sdteske	sub(/^.*\|/, "", command)
650252995Sdteske	sub(/\|.*$/, "", keyword)
651252995Sdteske
652252995Sdteske	if ( command == pgm )
653252995Sdteske	{
654252995Sdteske		print keyword
655252995Sdteske		found++
656252995Sdteske		exit
657252995Sdteske	}
658252995Sdteske}
659252995SdteskeEND { exit ! found }
660252995Sdteske'
661252995Sdteskef_index_menusel_keyword()
662252995Sdteske{
663263791Sdteske	local __indexfile="$1" __pgm="$2" __var_to_set="$3"
664263791Sdteske	local __lang="${LANG:-$LC_ALL}" __file="$__indexfile"
665252995Sdteske
666263791Sdteske	[ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang"
667252995Sdteske	f_dprintf "f_index_menusel_keyword: index=[%s] pgm=[%s] lang=[%s]" \
668263791Sdteske	          "$__file" "$__pgm" "$__lang"
669252995Sdteske
670263791Sdteske	if [ "$__var_to_set" ]; then
671263791Sdteske		setvar "$__var_to_set" "$( awk \
672263791Sdteske		    -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file"
673263791Sdteske		)"
674263791Sdteske	else
675263791Sdteske		awk -v pgm="$__pgm" "$f_index_menusel_keyword_awk" "$__file"
676252995Sdteske	fi
677252995Sdteske}
678252995Sdteske
679263791Sdteske# f_index_menusel_command $indexfile $keyword [$var_to_set]
680252995Sdteske#
681252995Sdteske# Process $indexfile and return only the command portion of the menu_selection
682252995Sdteske# line with a keyword portion matching $keyword.
683252995Sdteske#
684252995Sdteske# This function is for mapping [possibly international] keywords into the
685252995Sdteske# command to be executed. If $LANG or $LC_ALL (order of preference) is set,
686252995Sdteske# ".encoding" will automatically be appended as a suffix to the provided
687252995Sdteske# $indexfile pathname.
688252995Sdteske#
689252995Sdteske# If, within $indexfile, multiple $menu_selection values map to $keyword, only
690252995Sdteske# the first one will be returned. If no mapping can be made, the NULL string is
691252995Sdteske# returned.
692252995Sdteske#
693252995Sdteske# If $indexfile doesn't exist, error status is returned with NULL.
694252995Sdteske#
695263791Sdteske# If $var_to_set is NULL or missing, output is printed to stdout (which is less
696263791Sdteske# recommended due to performance degradation; in a loop for example).
697263791Sdteske#
698252995Sdteske# This function is a two-parter. Below is the awk(1) portion of the function,
699252995Sdteske# afterward is the sh(1) function which utilizes the below awk script.
700252995Sdteske#
701252995Sdteskef_index_menusel_command_awk='
702252995Sdteske# Variables that should be defined on the invocation line:
703252995Sdteske# 	-v key="keyword"
704252995Sdteske#
705252995SdteskeBEGIN {
706252995Sdteske	prefix = "menu_selection=\""
707252995Sdteske	plen = length(prefix)
708252995Sdteske	found = 0
709252995Sdteske}
710252995Sdteske{
711252995Sdteske	if (!match($0, "^" prefix ".*\\|.*\"")) next
712252995Sdteske
713252995Sdteske	keyword = command = substr($0, plen + 1, RLENGTH - plen - 1)
714252995Sdteske	sub(/^.*\|/, "", command)
715252995Sdteske	sub(/\|.*$/, "", keyword)
716252995Sdteske
717252995Sdteske	if ( keyword == key )
718252995Sdteske	{
719252995Sdteske		print command
720252995Sdteske		found++
721252995Sdteske		exit
722252995Sdteske	}
723252995Sdteske}
724252995SdteskeEND { exit ! found }
725252995Sdteske'
726252995Sdteskef_index_menusel_command()
727252995Sdteske{
728263791Sdteske	local __indexfile="$1" __keyword="$2" __var_to_set="$3" __command
729263791Sdteske	local __lang="${LANG:-$LC_ALL}" __file="$__indexfile"
730252995Sdteske
731263791Sdteske	[ -f "$__indexfile.$__lang" ] && __file="$__indexfile.$__lang"
732252995Sdteske	f_dprintf "f_index_menusel_command: index=[%s] key=[%s] lang=[%s]" \
733263791Sdteske	          "$__file" "$__keyword" "$__lang"
734252995Sdteske
735263791Sdteske	[ -f "$__file" ] || return $FAILURE
736263791Sdteske	__command=$( awk -v key="$__keyword" \
737263791Sdteske		"$f_index_menusel_command_awk" "$__file" ) || return $FAILURE
738252995Sdteske
739252995Sdteske	#
740252995Sdteske	# If the command pathname is not fully qualified fix-up/force to be
741252995Sdteske	# relative to the $indexfile directory.
742252995Sdteske	#
743263791Sdteske	case "$__command" in
744252995Sdteske	/*) : already fully qualified ;;
745252995Sdteske	*)
746263791Sdteske		local __indexdir="${__indexfile%/*}"
747263791Sdteske		[ "$__indexdir" != "$__indexfile" ] || __indexdir="."
748263791Sdteske		__command="$__indexdir/$__command"
749252995Sdteske	esac
750252995Sdteske
751263791Sdteske	if [ "$__var_to_set" ]; then
752263791Sdteske		setvar "$__var_to_set" "$__command"
753263791Sdteske	else
754263791Sdteske		echo "$__command"
755263791Sdteske	fi
756252995Sdteske}
757252995Sdteske
758252995Sdteske# f_running_as_init
759252995Sdteske#
760252995Sdteske# Returns true if running as init(1).
761252995Sdteske#
762252995Sdteskef_running_as_init()
763252995Sdteske{
764252995Sdteske	#
765252995Sdteske	# When a custom init(8) performs an exec(3) to invoke a shell script,
766252995Sdteske	# PID 1 becomes sh(1) and $PPID is set to 1 in the executed script.
767252995Sdteske	#
768252995Sdteske	[ ${PPID:-0} -eq 1 ] # Return status
769252995Sdteske}
770252995Sdteske
771252995Sdteske# f_mounted $local_directory
772266292Sdteske# f_mounted -b $device
773252995Sdteske#
774266292Sdteske# Return success if a filesystem is mounted on a particular directory. If `-b'
775266292Sdteske# is present, instead check that the block device (or a partition thereof) is
776266292Sdteske# mounted.
777252995Sdteske#
778252995Sdteskef_mounted()
779252995Sdteske{
780266292Sdteske	local OPTIND OPTARG flag use_device=
781266292Sdteske	while getopts b flag; do
782266292Sdteske		case "$flag" in
783266292Sdteske		b) use_device=1 ;;
784266292Sdteske		esac
785266292Sdteske	done
786266292Sdteske	shift $(( $OPTIND - 1 ))
787266292Sdteske	if [ "$use_device" ]; then
788266292Sdteske		local device="$1"
789266292Sdteske		mount | grep -Eq \
790266292Sdteske			"^$device([[:space:]]|p[0-9]|s[0-9]|\.nop|\.eli)"
791266292Sdteske	else
792266292Sdteske		[ -d "$dir" ] || return $FAILURE
793266292Sdteske		mount | grep -Eq " on $dir \([^)]+\)$"
794266292Sdteske	fi
795266292Sdteske	# Return status is that of last grep(1)
796252995Sdteske}
797252995Sdteske
798263791Sdteske# f_eval_catch [-de] [-k $var_to_set] $funcname $utility \
799263791Sdteske#              $format [$arguments ...]
800263791Sdteske#
801263791Sdteske# Silently evaluate a command in a sub-shell and test for error. If debugging
802263791Sdteske# is enabled a copy of the command and its output is sent to debug (either
803263791Sdteske# stdout or file depending on environment). If an error occurs, output of the
804263791Sdteske# command is displayed in a dialog(1) msgbox using the [above] f_show_err()
805263791Sdteske# function (unless optional `-d' flag is given, then no dialog).
806263791Sdteske#
807263791Sdteske# The $funcname argument is sent to debugging while the $utility argument is
808263791Sdteske# used in the title of the dialog box. The command that is executed as well as
809263791Sdteske# sent to debugging with $funcname is the product of the printf(1) syntax
810263791Sdteske# produced by $format with optional $arguments.
811263791Sdteske#
812263791Sdteske# The following options are supported:
813263791Sdteske#
814263791Sdteske# 	-d	Do not use dialog(1).
815263791Sdteske# 	-e	Produce error text from failed command on stderr.
816263791Sdteske# 	-k var	Save output from the command in var.
817263791Sdteske#
818263791Sdteske# Example 1:
819263791Sdteske#
820263791Sdteske# 	debug=1
821263791Sdteske# 	f_eval_catch myfunc echo 'echo "%s"' "Hello, World!"
822263791Sdteske#
823263791Sdteske# 	Produces the following debug output:
824263791Sdteske#
825263791Sdteske# 		DEBUG: myfunc: echo "Hello, World!"
826263791Sdteske# 		DEBUG: myfunc: retval=0 <output below>
827263791Sdteske# 		Hello, World!
828263791Sdteske#
829263791Sdteske# Example 2:
830263791Sdteske#
831263791Sdteske# 	debug=1
832263791Sdteske# 	f_eval_catch -k contents myfunc cat 'cat "%s"' /some/file
833263791Sdteske# 	# dialog(1) Error ``cat: /some/file: No such file or directory''
834263791Sdteske# 	# contents=[cat: /some/file: No such file or directory]
835263791Sdteske#
836263791Sdteske# 	Produces the following debug output:
837263791Sdteske#
838263791Sdteske# 		DEBUG: myfunc: cat "/some/file"
839263791Sdteske# 		DEBUG: myfunc: retval=1 <output below>
840263791Sdteske# 		cat: /some/file: No such file or directory
841263791Sdteske#
842263791Sdteske# Example 3:
843263791Sdteske#
844263791Sdteske# 	debug=1
845263791Sdteske# 	echo 123 | f_eval_catch myfunc rev rev
846263791Sdteske#
847263791Sdteske# 	Produces the following debug output:
848263791Sdteske#
849263791Sdteske# 		DEBUG: myfunc: rev
850263791Sdteske# 		DEBUG: myfunc: retval=0 <output below>
851263791Sdteske# 		321
852263791Sdteske#
853263791Sdteske# Example 4:
854263791Sdteske#
855263791Sdteske# 	debug=1
856263791Sdteske# 	f_eval_catch myfunc true true
857263791Sdteske#
858263791Sdteske# 	Produces the following debug output:
859263791Sdteske#
860263791Sdteske# 		DEBUG: myfunc: true
861263791Sdteske# 		DEBUG: myfunc: retval=0 <no output>
862263791Sdteske#
863263791Sdteske# Example 5:
864263791Sdteske#
865263791Sdteske# 	f_eval_catch -de myfunc ls 'ls "%s"' /some/dir
866263791Sdteske# 	# Output on stderr ``ls: /some/dir: No such file or directory''
867263791Sdteske#
868263791Sdteske# Example 6:
869263791Sdteske#
870263791Sdteske# 	f_eval_catch -dek contents myfunc ls 'ls "%s"' /etc
871263791Sdteske# 	# Output from `ls' sent to stderr and also saved in $contents
872263791Sdteske#
873263791Sdteskef_eval_catch()
874263791Sdteske{
875263791Sdteske	local __no_dialog= __show_err= __var_to_set=
876263791Sdteske
877263791Sdteske	#
878263791Sdteske	# Process local function arguments
879263791Sdteske	#
880266292Sdteske	local OPTIND OPTARG __flag
881263791Sdteske	while getopts "dek:" __flag > /dev/null; do
882263791Sdteske		case "$__flag" in
883263791Sdteske		d) __no_dialog=1 ;;
884263791Sdteske		e) __show_err=1 ;;
885263791Sdteske		k) __var_to_set="$OPTARG" ;;
886263791Sdteske		esac
887263791Sdteske	done
888263791Sdteske	shift $(( $OPTIND - 1 ))
889263791Sdteske
890263791Sdteske	local __funcname="$1" __utility="$2"; shift 2
891263791Sdteske	local __cmd __output __retval
892263791Sdteske
893263791Sdteske	__cmd=$( printf -- "$@" )
894263791Sdteske	f_dprintf "%s: %s" "$__funcname" "$__cmd" # Log command *before* eval
895263791Sdteske	__output=$( exec 2>&1; eval "$__cmd" )
896263791Sdteske	__retval=$?
897263791Sdteske	if [ "$__output" ]; then
898263791Sdteske		[ "$__show_err" ] && echo "$__output" >&2
899263791Sdteske		f_dprintf "%s: retval=%i <output below>\n%s" "$__funcname" \
900263791Sdteske		          $__retval "$__output"
901263791Sdteske	else
902263791Sdteske		f_dprintf "%s: retval=%i <no output>" "$__funcname" $__retval
903263791Sdteske	fi
904263791Sdteske
905263791Sdteske	! [ "$__no_dialog" -o "$nonInteractive" -o $__retval -eq $SUCCESS ] &&
906263791Sdteske		msg_error="${msg_error:-Error}${__utility:+: $__utility}" \
907263791Sdteske			f_show_err "%s" "$__output"
908263791Sdteske		# NB: f_show_err will handle NULL output appropriately
909263791Sdteske
910263791Sdteske	[ "$__var_to_set" ] && setvar "$__var_to_set" "$__output"
911263791Sdteske
912263791Sdteske	return $__retval
913263791Sdteske}
914263791Sdteske
915263791Sdteske# f_count $var_to_set arguments ...
916263791Sdteske#
917263791Sdteske# Sets $var_to_set to the number of arguments minus one (the effective number
918263791Sdteske# of arguments following $var_to_set).
919263791Sdteske#
920263791Sdteske# Example:
921263791Sdteske# 	f_count count dog house # count=[2]
922263791Sdteske#
923263791Sdteskef_count()
924263791Sdteske{
925263791Sdteske	setvar "$1" $(( $# - 1 ))
926263791Sdteske}
927263791Sdteske
928263791Sdteske# f_count_ifs $var_to_set string ...
929263791Sdteske#
930263791Sdteske# Sets $var_to_set to the number of words (split by the internal field
931263791Sdteske# separator, IFS) following $var_to_set.
932263791Sdteske#
933263791Sdteske# Example 1:
934263791Sdteske#
935263791Sdteske# 	string="word1   word2   word3"
936263791Sdteske# 	f_count_ifs count "$string" # count=[3]
937263791Sdteske# 	f_count_ifs count $string # count=[3]
938263791Sdteske#
939263791Sdteske# Example 2:
940263791Sdteske#
941263791Sdteske# 	IFS=. f_count_ifs count www.freebsd.org # count=[3]
942263791Sdteske#
943263791Sdteske# NB: Make sure to use double-quotes if you are using a custom value for IFS
944263791Sdteske# and you don't want the current value to effect the result. See example 3.
945263791Sdteske#
946263791Sdteske# Example 3:
947263791Sdteske#
948263791Sdteske# 	string="a-b c-d"
949263791Sdteske# 	IFS=- f_count_ifs count "$string" # count=[3]
950263791Sdteske# 	IFS=- f_count_ifs count $string # count=[4]
951263791Sdteske#
952263791Sdteskef_count_ifs()
953263791Sdteske{
954263791Sdteske	local __var_to_set="$1"
955263791Sdteske	shift 1
956263791Sdteske	set -- $*
957263791Sdteske	setvar "$__var_to_set" $#
958263791Sdteske}
959263791Sdteske
960252995Sdteske############################################################ MAIN
961252995Sdteske
962252995Sdteske#
963252995Sdteske# Trap signals so we can recover gracefully
964252995Sdteske#
965252995Sdtesketrap 'f_interrupt' SIGINT
966252995Sdtesketrap 'f_die' SIGTERM SIGPIPE SIGXCPU SIGXFSZ \
967252995Sdteske             SIGFPE SIGTRAP SIGABRT SIGSEGV
968252995Sdtesketrap '' SIGALRM SIGPROF SIGUSR1 SIGUSR2 SIGHUP SIGVTALRM
969252995Sdteske
970252995Sdteske#
971252995Sdteske# Clone terminal stdout/stderr so we can redirect to it from within sub-shells
972252995Sdteske#
973252995Sdteskeeval exec $TERMINAL_STDOUT_PASSTHRU\>\&1
974252995Sdteskeeval exec $TERMINAL_STDERR_PASSTHRU\>\&2
975252995Sdteske
976252995Sdteske#
977252995Sdteske# Self-initialize unless requested otherwise
978252995Sdteske#
979252995Sdteskef_dprintf "%s: DEBUG_SELF_INITIALIZE=[%s]" \
980252995Sdteske          dialog.subr "$DEBUG_SELF_INITIALIZE"
981252995Sdteskecase "$DEBUG_SELF_INITIALIZE" in
982252995Sdteske""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
983252995Sdteske*) f_debug_init
984252995Sdteskeesac
985252995Sdteske
986252995Sdteske#
987252995Sdteske# Log our operating environment for debugging purposes
988252995Sdteske#
989252995Sdteskef_dprintf "UNAME_S=[%s] UNAME_P=[%s] UNAME_R=[%s]" \
990252995Sdteske          "$UNAME_S" "$UNAME_P" "$UNAME_R"
991252995Sdteske
992252995Sdteskef_dprintf "%s: Successfully loaded." common.subr
993252995Sdteske
994252995Sdteskefi # ! $_COMMON_SUBR
995