xref: /freebsd/usr.sbin/bsdconfig/share/dialog.subr (revision c36b3dbc99d1e6dfc658e9c20382abc75749af82)
1ab2043b8SDevin Teskeif [ ! "$_DIALOG_SUBR" ]; then _DIALOG_SUBR=1
2ab2043b8SDevin Teske#
335994c42SDevin Teske# Copyright (c) 2006-2015 Devin Teske
4f8ea072aSDevin Teske# All rights reserved.
5ab2043b8SDevin Teske#
6ab2043b8SDevin Teske# Redistribution and use in source and binary forms, with or without
7ab2043b8SDevin Teske# modification, are permitted provided that the following conditions
8ab2043b8SDevin Teske# are met:
9ab2043b8SDevin Teske# 1. Redistributions of source code must retain the above copyright
10ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer.
11ab2043b8SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
12ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer in the
13ab2043b8SDevin Teske#    documentation and/or other materials provided with the distribution.
14ab2043b8SDevin Teske#
15ab2043b8SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
168e37a7c8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17ab2043b8SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ab2043b8SDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ab2043b8SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ab2043b8SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ab2043b8SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ab2043b8SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ab2043b8SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ab2043b8SDevin Teske# SUCH DAMAGE.
26ab2043b8SDevin Teske#
27ab2043b8SDevin Teske#
28ab2043b8SDevin Teske############################################################ INCLUDES
29ab2043b8SDevin Teske
30ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
31ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3256961fd7SDevin Teskef_dprintf "%s: loading includes..." dialog.subr
33ab2043b8SDevin Teskef_include $BSDCFG_SHARE/strings.subr
343da75d2eSDevin Teskef_include $BSDCFG_SHARE/variable.subr
35ab2043b8SDevin Teske
36ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig"
37ab2043b8SDevin Teskef_include_lang $BSDCFG_LIBE/include/messages.subr
38ab2043b8SDevin Teske
39ab2043b8SDevin Teske############################################################ CONFIGURATION
40ab2043b8SDevin Teske
41ab2043b8SDevin Teske#
4289498fdfSDevin Teske# Default file descriptor to link to stdout for dialog(1) passthru allowing
4389498fdfSDevin Teske# execution of dialog from within a sub-shell (so-long as its standard output
4489498fdfSDevin Teske# is explicitly redirected to this file descriptor).
45ab2043b8SDevin Teske#
46526e1dc1SDevin Teske: ${DIALOG_TERMINAL_PASSTHRU_FD:=${TERMINAL_STDOUT_PASSTHRU:-3}}
47ab2043b8SDevin Teske
48ab2043b8SDevin Teske############################################################ GLOBALS
49ab2043b8SDevin Teske
50ab2043b8SDevin Teske#
51ab2043b8SDevin Teske# Default name of dialog(1) utility
52ab2043b8SDevin Teske# NOTE: This is changed to "Xdialog" by the optional `-X' argument
53ab2043b8SDevin Teske#
54*c36b3dbcSAlfonso S. SicilianoDIALOG="bsddialog"
55ab2043b8SDevin Teske
56ab2043b8SDevin Teske#
57ab2043b8SDevin Teske# Default dialog(1) title and backtitle text
58ab2043b8SDevin Teske#
59ab2043b8SDevin TeskeDIALOG_TITLE="$pgm"
60ab2043b8SDevin TeskeDIALOG_BACKTITLE="bsdconfig"
61ab2043b8SDevin Teske
62ab2043b8SDevin Teske#
63ab2043b8SDevin Teske# Settings used while interacting with dialog(1)
64ab2043b8SDevin Teske#
65ab2043b8SDevin TeskeDIALOG_MENU_TAGS="123456789ABCDEFGHIJKLMNOPQRSTUVWYZabcdefghijklmnopqrstuvwxyz"
66ab2043b8SDevin Teske
67ab2043b8SDevin Teske#
68ab2043b8SDevin Teske# Declare that we are fully-compliant with Xdialog(1) by unset'ing all
69ab2043b8SDevin Teske# compatibility settings.
70ab2043b8SDevin Teske#
71ab2043b8SDevin Teskeunset XDIALOG_HIGH_DIALOG_COMPAT
72ab2043b8SDevin Teskeunset XDIALOG_FORCE_AUTOSIZE
73ab2043b8SDevin Teskeunset XDIALOG_INFOBOX_TIMEOUT
74ab2043b8SDevin Teske
75ab2043b8SDevin Teske#
76f677a9e2SDevin Teske# Exit codes for [X]dialog(1)
77f677a9e2SDevin Teske#
78f677a9e2SDevin TeskeDIALOG_OK=${SUCCESS:-0}
79f677a9e2SDevin TeskeDIALOG_CANCEL=${FAILURE:-1}
80f677a9e2SDevin TeskeDIALOG_HELP=2
81f677a9e2SDevin TeskeDIALOG_EXTRA=3
82f677a9e2SDevin TeskeDIALOG_ITEM_HELP=4
83f677a9e2SDevin Teskeexport DIALOG_ERROR=254 # sh(1) can't handle the default of `-1'
84f677a9e2SDevin TeskeDIALOG_ESC=255
85f677a9e2SDevin Teske
86f677a9e2SDevin Teske#
87*c36b3dbcSAlfonso S. Siciliano# Set bsddialog(1) compatibility with dialog(1): exit codes and use_shadow
88*c36b3dbcSAlfonso S. Siciliano#
89*c36b3dbcSAlfonso S. Sicilianoexport BSDDIALOG_TIMEOUT=0
90*c36b3dbcSAlfonso S. Sicilianoexport BSDDIALOG_ITEM_HELP=4
91*c36b3dbcSAlfonso S. Sicilianoexport BSDDIALOG_ERROR=254
92*c36b3dbcSAlfonso S. Sicilianoexport BSDDIALOG_ESC=255
93*c36b3dbcSAlfonso S. Sicilianoexport BSDDIALOG_COMPATRC=".dialogrc"
94*c36b3dbcSAlfonso S. Siciliano
95*c36b3dbcSAlfonso S. Siciliano#
9679ec1cd5SDevin Teske# Default behavior is to call f_dialog_init() automatically when loaded.
97ab2043b8SDevin Teske#
98ab2043b8SDevin Teske: ${DIALOG_SELF_INITIALIZE=1}
99ab2043b8SDevin Teske
10030a8a39eSDevin Teske#
10130a8a39eSDevin Teske# Default terminal size (used if/when running without a controlling terminal)
10230a8a39eSDevin Teske#
10330a8a39eSDevin Teske: ${DEFAULT_TERMINAL_SIZE:=24 80}
10430a8a39eSDevin Teske
10574036c4dSDevin Teske#
10674036c4dSDevin Teske# Minimum width(s) for various dialog(1) implementations (sensible global
10774036c4dSDevin Teske# default(s) for all widgets of a given variant)
10874036c4dSDevin Teske#
10974036c4dSDevin Teske: ${DIALOG_MIN_WIDTH:=24}
11074036c4dSDevin Teske: ${XDIALOG_MIN_WIDTH:=35}
11174036c4dSDevin Teske
11274036c4dSDevin Teske#
11374036c4dSDevin Teske# When manually sizing Xdialog(1) widgets such as calendar and timebox, you'll
11474036c4dSDevin Teske# need to know the size of the embedded GUI objects because the height passed
115ec65e4f8SPedro F. Giffuni# to Xdialog(1) for these widgets has to be tall enough to accommodate them.
11674036c4dSDevin Teske#
11774036c4dSDevin Teske# These values are helpful when manually sizing with dialog(1) too, but in a
118ec65e4f8SPedro F. Giffuni# different way. dialog(1) does not make you accommodate the custom items in the
11974036c4dSDevin Teske# height (but does for width) -- a height of 3 will display three lines and a
12074036c4dSDevin Teske# full calendar, for example (whereas Xdialog will truncate the calendar if
12174036c4dSDevin Teske# given a height of 3). For dialog(1), use these values for making sure that
12274036c4dSDevin Teske# the height does not exceed max_height (obtained by f_dialog_max_size()).
12374036c4dSDevin Teske#
12474036c4dSDevin TeskeDIALOG_CALENDAR_HEIGHT=15
12574036c4dSDevin TeskeDIALOG_TIMEBOX_HEIGHT=6
12674036c4dSDevin Teske
127ab2043b8SDevin Teske############################################################ GENERIC FUNCTIONS
128ab2043b8SDevin Teske
129fd962ac6SDevin Teske# f_dialog_data_sanitize $var_to_edit ...
130fd962ac6SDevin Teske#
131fd962ac6SDevin Teske# When using dialog(1) or Xdialog(1) sometimes unintended warnings or errors
132fd962ac6SDevin Teske# are generated from underlying libraries. For example, if $LANG is set to an
133fd962ac6SDevin Teske# invalid or unknown locale, the warnings from the Xdialog(1) libraries will
134fd962ac6SDevin Teske# clutter the output. This function helps by providing a centralied function
135fd962ac6SDevin Teske# that removes spurious warnings from the dialog(1) (or Xdialog(1)) response.
136fd962ac6SDevin Teske#
137fd962ac6SDevin Teske# Simply pass the name of one or more variables that need to be sanitized.
138fd962ac6SDevin Teske# After execution, the variables will hold their newly-sanitized data.
139fd962ac6SDevin Teske#
140fd962ac6SDevin Teskef_dialog_data_sanitize()
141fd962ac6SDevin Teske{
142fd962ac6SDevin Teske	if [ "$#" -eq 0 ]; then
143fd962ac6SDevin Teske		f_dprintf "%s: called with zero arguments" \
144fd962ac6SDevin Teske		          f_dialog_response_sanitize
145fd962ac6SDevin Teske		return $FAILURE
146fd962ac6SDevin Teske	fi
147fd962ac6SDevin Teske
148fd962ac6SDevin Teske	local __var_to_edit
149fd962ac6SDevin Teske	for __var_to_edit in $*; do
150fd962ac6SDevin Teske		# Skip warnings and trim leading/trailing whitespace
151fd962ac6SDevin Teske		setvar $__var_to_edit "$( f_getvar $__var_to_edit | awk '
152fd962ac6SDevin Teske			BEGIN { data = 0 }
153fd962ac6SDevin Teske			{
154fd962ac6SDevin Teske				if ( ! data )
155fd962ac6SDevin Teske				{
156fd962ac6SDevin Teske					if ( $0 ~ /^$/ ) next
157fd962ac6SDevin Teske					if ( $0 ~ /^Gdk-WARNING \*\*:/ ) next
158fd962ac6SDevin Teske					data = 1
159fd962ac6SDevin Teske				}
160fd962ac6SDevin Teske				print
161fd962ac6SDevin Teske			}
162fd962ac6SDevin Teske		' )"
163fd962ac6SDevin Teske	done
164fd962ac6SDevin Teske}
165fd962ac6SDevin Teske
166ec7120b5SDevin Teske# f_dialog_line_sanitize $var_to_edit ...
167ec7120b5SDevin Teske#
168ec7120b5SDevin Teske# When using dialog(1) or Xdialog(1) sometimes unintended warnings or errors
169ec7120b5SDevin Teske# are generated from underlying libraries. For example, if $LANG is set to an
170ec7120b5SDevin Teske# invalid or unknown locale, the warnings from the Xdialog(1) libraries will
171ec7120b5SDevin Teske# clutter the output. This function helps by providing a centralied function
172ec7120b5SDevin Teske# that removes spurious warnings from the dialog(1) (or Xdialog(1)) response.
173ec7120b5SDevin Teske#
174ec7120b5SDevin Teske# Simply pass the name of one or more variables that need to be sanitized.
175ec7120b5SDevin Teske# After execution, the variables will hold their newly-sanitized data.
176ec7120b5SDevin Teske#
177ec7120b5SDevin Teske# This function, unlike f_dialog_data_sanitize(), also removes leading/trailing
178ec7120b5SDevin Teske# whitespace from each line.
179ec7120b5SDevin Teske#
180ec7120b5SDevin Teskef_dialog_line_sanitize()
181ec7120b5SDevin Teske{
182ec7120b5SDevin Teske	if [ "$#" -eq 0 ]; then
183ec7120b5SDevin Teske		f_dprintf "%s: called with zero arguments" \
184ec7120b5SDevin Teske		          f_dialog_response_sanitize
185ec7120b5SDevin Teske		return $FAILURE
186ec7120b5SDevin Teske	fi
187ec7120b5SDevin Teske
188ec7120b5SDevin Teske	local __var_to_edit
189ec7120b5SDevin Teske	for __var_to_edit in $*; do
190ec7120b5SDevin Teske		# Skip warnings and trim leading/trailing whitespace
191ec7120b5SDevin Teske		setvar $__var_to_edit "$( f_getvar $__var_to_edit | awk '
192ec7120b5SDevin Teske			BEGIN { data = 0 }
193ec7120b5SDevin Teske			{
194ec7120b5SDevin Teske				if ( ! data )
195ec7120b5SDevin Teske				{
196ec7120b5SDevin Teske					if ( $0 ~ /^$/ ) next
197ec7120b5SDevin Teske					if ( $0 ~ /^Gdk-WARNING \*\*:/ ) next
198ec7120b5SDevin Teske					data = 1
199ec7120b5SDevin Teske				}
200ec7120b5SDevin Teske				sub(/^[[:space:]]*/, "")
201ec7120b5SDevin Teske				sub(/[[:space:]]*$/, "")
202ec7120b5SDevin Teske				print
203ec7120b5SDevin Teske			}
204ec7120b5SDevin Teske		' )"
205ec7120b5SDevin Teske	done
206ec7120b5SDevin Teske}
207ec7120b5SDevin Teske
208fd962ac6SDevin Teske############################################################ TITLE FUNCTIONS
209fd962ac6SDevin Teske
210ab2043b8SDevin Teske# f_dialog_title [$new_title]
211ab2043b8SDevin Teske#
212ab2043b8SDevin Teske# Set the title of future dialog(1) ($DIALOG_TITLE) or backtitle of Xdialog(1)
213ab2043b8SDevin Teske# ($DIALOG_BACKTITLE) invocations. If no arguments are given or the first
214ab2043b8SDevin Teske# argument is NULL, the current title is returned.
215ab2043b8SDevin Teske#
216ab2043b8SDevin Teske# Each time this function is called, a backup of the current values is made
2178a86b3cbSDevin Teske# allowing a one-time (single-level) restoration of the previous title using
2188a86b3cbSDevin Teske# the f_dialog_title_restore() function (below).
219ab2043b8SDevin Teske#
220ab2043b8SDevin Teskef_dialog_title()
221ab2043b8SDevin Teske{
222ab2043b8SDevin Teske	local new_title="$1"
223ab2043b8SDevin Teske
224540bebd7SDevin Teske	if [ "${1+set}" ]; then
225ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
226ab2043b8SDevin Teske			_DIALOG_BACKTITLE="$DIALOG_BACKTITLE"
227ab2043b8SDevin Teske			DIALOG_BACKTITLE="$new_title"
228ab2043b8SDevin Teske		else
229ab2043b8SDevin Teske			_DIALOG_TITLE="$DIALOG_TITLE"
230ab2043b8SDevin Teske			DIALOG_TITLE="$new_title"
231ab2043b8SDevin Teske		fi
232ab2043b8SDevin Teske	else
233ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
234ab2043b8SDevin Teske			echo "$DIALOG_BACKTITLE"
235ab2043b8SDevin Teske		else
236ab2043b8SDevin Teske			echo "$DIALOG_TITLE"
237ab2043b8SDevin Teske		fi
238ab2043b8SDevin Teske	fi
239ab2043b8SDevin Teske}
240ab2043b8SDevin Teske
241ab2043b8SDevin Teske# f_dialog_title_restore
242ab2043b8SDevin Teske#
243ab2043b8SDevin Teske# Restore the previous title set by the last call to f_dialog_title().
244ab2043b8SDevin Teske# Restoration is non-recursive and only works to restore the most-recent title.
245ab2043b8SDevin Teske#
246ab2043b8SDevin Teskef_dialog_title_restore()
247ab2043b8SDevin Teske{
248ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
249ab2043b8SDevin Teske		DIALOG_BACKTITLE="$_DIALOG_BACKTITLE"
250ab2043b8SDevin Teske	else
251ab2043b8SDevin Teske		DIALOG_TITLE="$_DIALOG_TITLE"
252ab2043b8SDevin Teske	fi
253ab2043b8SDevin Teske}
254ab2043b8SDevin Teske
255ab2043b8SDevin Teske# f_dialog_backtitle [$new_backtitle]
256ab2043b8SDevin Teske#
257ab2043b8SDevin Teske# Set the backtitle of future dialog(1) ($DIALOG_BACKTITLE) or title of
258ab2043b8SDevin Teske# Xdialog(1) ($DIALOG_TITLE) invocations. If no arguments are given or the
259ab2043b8SDevin Teske# first argument is NULL, the current backtitle is returned.
260ab2043b8SDevin Teske#
261ab2043b8SDevin Teskef_dialog_backtitle()
262ab2043b8SDevin Teske{
263ab2043b8SDevin Teske	local new_backtitle="$1"
264ab2043b8SDevin Teske
265540bebd7SDevin Teske	if [ "${1+set}" ]; then
266ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
267ab2043b8SDevin Teske			_DIALOG_TITLE="$DIALOG_TITLE"
268ab2043b8SDevin Teske			DIALOG_TITLE="$new_backtitle"
269ab2043b8SDevin Teske		else
270ab2043b8SDevin Teske			_DIALOG_BACKTITLE="$DIALOG_BACKTITLE"
271ab2043b8SDevin Teske			DIALOG_BACKTITLE="$new_backtitle"
272ab2043b8SDevin Teske		fi
273ab2043b8SDevin Teske	else
274ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
275ab2043b8SDevin Teske			echo "$DIALOG_TITLE"
276ab2043b8SDevin Teske		else
277ab2043b8SDevin Teske			echo "$DIALOG_BACKTITLE"
278ab2043b8SDevin Teske		fi
279ab2043b8SDevin Teske	fi
280ab2043b8SDevin Teske}
281ab2043b8SDevin Teske
282ab2043b8SDevin Teske# f_dialog_backtitle_restore
283ab2043b8SDevin Teske#
284ab2043b8SDevin Teske# Restore the previous backtitle set by the last call to f_dialog_backtitle().
285ab2043b8SDevin Teske# Restoration is non-recursive and only works to restore the most-recent
286ab2043b8SDevin Teske# backtitle.
287ab2043b8SDevin Teske#
288ab2043b8SDevin Teskef_dialog_backtitle_restore()
289ab2043b8SDevin Teske{
290ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
291ab2043b8SDevin Teske		DIALOG_TITLE="$_DIALOG_TITLE"
292ab2043b8SDevin Teske	else
293ab2043b8SDevin Teske		DIALOG_BACKTITLE="$_DIALOG_BACKTITLE"
294ab2043b8SDevin Teske	fi
295ab2043b8SDevin Teske}
296ab2043b8SDevin Teske
297ab2043b8SDevin Teske############################################################ SIZE FUNCTIONS
298ab2043b8SDevin Teske
29974036c4dSDevin Teske# f_dialog_max_size $var_height $var_width
30074036c4dSDevin Teske#
30174036c4dSDevin Teske# Get the maximum height and width for a dialog widget and store the values in
30274036c4dSDevin Teske# $var_height and $var_width (respectively).
30374036c4dSDevin Teske#
30474036c4dSDevin Teskef_dialog_max_size()
30574036c4dSDevin Teske{
306d4ae33f0SDevin Teske	local funcname=f_dialog_max_size
30774036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __max_size
30874036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
30974036c4dSDevin Teske	if [ "$USE_XDIALOG" ]; then
31074036c4dSDevin Teske		__max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION
31174036c4dSDevin Teske	else
31291d20ba0SDevin Teske		if __max_size=$( $DIALOG --print-maxsize \
31391d20ba0SDevin Teske			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
31491d20ba0SDevin Teske		then
315d4ae33f0SDevin Teske			f_dprintf "$funcname: %s --print-maxsize = [%s]" \
316d4ae33f0SDevin Teske			          "$DIALOG" "$__max_size"
31791d20ba0SDevin Teske			# usually "MaxSize: 24, 80"
31891d20ba0SDevin Teske			__max_size="${__max_size#*: }"
31991d20ba0SDevin Teske			f_replaceall "$__max_size" "," "" __max_size
32091d20ba0SDevin Teske		else
321d4ae33f0SDevin Teske			f_eval_catch -dk __max_size $funcname stty \
322d4ae33f0SDevin Teske				'stty size' || __max_size=
32391d20ba0SDevin Teske			# usually "24 80"
32491d20ba0SDevin Teske		fi
32574036c4dSDevin Teske		: ${__max_size:=$DEFAULT_TERMINAL_SIZE}
32674036c4dSDevin Teske	fi
3277d3a5a3bSDevin Teske	if [ "$__var_height" ]; then
3287d3a5a3bSDevin Teske		local __height="${__max_size%%[$IFS]*}"
3297d3a5a3bSDevin Teske		#
3307d3a5a3bSDevin Teske		# If we're not using Xdialog(1), we should assume that $DIALOG
3317d3a5a3bSDevin Teske		# will render --backtitle behind the widget. In such a case, we
3328a86b3cbSDevin Teske		# should prevent a widget from obscuring the backtitle (unless
3337d3a5a3bSDevin Teske		# $NO_BACKTITLE is set and non-NULL, allowing a trap-door).
3347d3a5a3bSDevin Teske		#
3357d3a5a3bSDevin Teske		if [ ! "$USE_XDIALOG" ] && [ ! "$NO_BACKTITLE" ]; then
3367d3a5a3bSDevin Teske			#
3377d3a5a3bSDevin Teske			# If use_shadow (in ~/.dialogrc) is OFF, we need to
3387d3a5a3bSDevin Teske			# subtract 4, otherwise 5. However, don't check this
3397d3a5a3bSDevin Teske			# every time, rely on an initialization variable set
3407d3a5a3bSDevin Teske			# by f_dialog_init().
3417d3a5a3bSDevin Teske			#
3427d3a5a3bSDevin Teske			local __adjust=5
3437d3a5a3bSDevin Teske			[ "$NO_SHADOW" ] && __adjust=4
3447d3a5a3bSDevin Teske
3458a86b3cbSDevin Teske			# Don't adjust height if already too small (allowing
3467d3a5a3bSDevin Teske			# obscured backtitle for small values of __height).
3477d3a5a3bSDevin Teske			[ ${__height:-0} -gt 11 ] &&
3487d3a5a3bSDevin Teske				__height=$(( $__height - $__adjust ))
3497d3a5a3bSDevin Teske		fi
3507d3a5a3bSDevin Teske		setvar "$__var_height" "$__height"
3517d3a5a3bSDevin Teske	fi
35274036c4dSDevin Teske	[ "$__var_width" ] && setvar "$__var_width" "${__max_size##*[$IFS]}"
35374036c4dSDevin Teske}
35474036c4dSDevin Teske
35574036c4dSDevin Teske# f_dialog_size_constrain $var_height $var_width [$min_height [$min_width]]
35674036c4dSDevin Teske#
35774036c4dSDevin Teske# Modify $var_height to be no-less-than $min_height (if given; zero otherwise)
35874036c4dSDevin Teske# and no-greater-than terminal height (or screen height if $USE_XDIALOG is
35974036c4dSDevin Teske# set).
36074036c4dSDevin Teske#
36174036c4dSDevin Teske# Also modify $var_width to be no-less-than $XDIALOG_MIN_WIDTH (or
36274036c4dSDevin Teske# $XDIALOG_MIN_WIDTH if $_USE_XDIALOG is set) and no-greater-than terminal
36374036c4dSDevin Teske# or screen width. The use of $[X]DIALOG_MIN_WIDTH can be overridden by
36474036c4dSDevin Teske# passing $min_width.
36574036c4dSDevin Teske#
36674036c4dSDevin Teske# Return status is success unless one of the passed arguments is invalid
36774036c4dSDevin Teske# or all of the $var_* arguments are either NULL or missing.
36874036c4dSDevin Teske#
36974036c4dSDevin Teskef_dialog_size_constrain()
37074036c4dSDevin Teske{
37174036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
37274036c4dSDevin Teske	local __min_height="$3" __min_width="$4"
37374036c4dSDevin Teske	local __retval=$SUCCESS
37474036c4dSDevin Teske
37574036c4dSDevin Teske	# Return failure unless at least one var_* argument is passed
37674036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
37774036c4dSDevin Teske
37874036c4dSDevin Teske	#
37974036c4dSDevin Teske	# Print debug warnings if any given (non-NULL) argument are invalid
38074036c4dSDevin Teske	# NOTE: Don't change the name of $__{var,min,}{height,width}
38174036c4dSDevin Teske	#
38274036c4dSDevin Teske	local __height __width
38374036c4dSDevin Teske	local __arg __cp __fname=f_dialog_size_constrain
38474036c4dSDevin Teske	for __arg in height width; do
38574036c4dSDevin Teske		debug= f_getvar __var_$__arg __cp
38674036c4dSDevin Teske		[ "$__cp" ] || continue
387790062d2SDevin Teske		if ! debug= f_getvar "$__cp" __$__arg; then
38874036c4dSDevin Teske			f_dprintf "%s: var_%s variable \`%s' not set" \
38974036c4dSDevin Teske			          $__fname $__arg "$__cp"
39074036c4dSDevin Teske			__retval=$FAILURE
39174036c4dSDevin Teske		elif ! eval f_isinteger \$__$__arg; then
39274036c4dSDevin Teske			f_dprintf "%s: var_%s variable value not a number" \
39374036c4dSDevin Teske			          $__fname $__arg
39474036c4dSDevin Teske			__retval=$FAILURE
39574036c4dSDevin Teske		fi
39674036c4dSDevin Teske	done
39774036c4dSDevin Teske	for __arg in height width; do
39874036c4dSDevin Teske		debug= f_getvar __min_$__arg __cp
39974036c4dSDevin Teske		[ "$__cp" ] || continue
40074036c4dSDevin Teske		f_isinteger "$__cp" && continue
40174036c4dSDevin Teske		f_dprintf "%s: min_%s value not a number" $__fname $__arg
40274036c4dSDevin Teske		__retval=$FAILURE
40374036c4dSDevin Teske		setvar __min_$__arg ""
40474036c4dSDevin Teske	done
40574036c4dSDevin Teske
40674036c4dSDevin Teske	# Obtain maximum height and width values
40774036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
40874036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
40974036c4dSDevin Teske	local __max_height_size_constain __max_width_size_constrain
41074036c4dSDevin Teske	f_dialog_max_size \
41174036c4dSDevin Teske		__max_height_size_constrain __max_width_size_constrain
41274036c4dSDevin Teske
41374036c4dSDevin Teske	# Adjust height if desired
41474036c4dSDevin Teske	if [ "$__var_height" ]; then
41574036c4dSDevin Teske		if [ $__height -lt ${__min_height:-0} ]; then
41674036c4dSDevin Teske			setvar "$__var_height" $__min_height
41774036c4dSDevin Teske		elif [ $__height -gt $__max_height_size_constrain ]; then
41874036c4dSDevin Teske			setvar "$__var_height" $__max_height_size_constrain
41974036c4dSDevin Teske		fi
42074036c4dSDevin Teske	fi
42174036c4dSDevin Teske
42274036c4dSDevin Teske	# Adjust width if desired
42374036c4dSDevin Teske	if [ "$__var_width" ]; then
42474036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
42574036c4dSDevin Teske			: ${__min_width:=${XDIALOG_MIN_WIDTH:-35}}
42674036c4dSDevin Teske		else
42774036c4dSDevin Teske			: ${__min_width:=${DIALOG_MIN_WIDTH:-24}}
42874036c4dSDevin Teske		fi
42974036c4dSDevin Teske		if [ $__width -lt $__min_width ]; then
43074036c4dSDevin Teske			setvar "$__var_width" $__min_width
43174036c4dSDevin Teske		elif [ $__width -gt $__max_width_size_constrain ]; then
43274036c4dSDevin Teske			setvar "$__var_width" $__max_width_size_constrain
43374036c4dSDevin Teske		fi
43474036c4dSDevin Teske	fi
43574036c4dSDevin Teske
436790062d2SDevin Teske	if [ "$debug" ]; then
4375b3821c0SDevin Teske		# Print final constrained values to debugging
438efa13f7eSDevin Teske		[ "$__var_height" ] && f_quietly f_getvar "$__var_height"
439efa13f7eSDevin Teske		[ "$__var_width"  ] && f_quietly f_getvar "$__var_width"
440790062d2SDevin Teske	fi
441790062d2SDevin Teske
44274036c4dSDevin Teske	return $__retval # success if no debug warnings were printed
44374036c4dSDevin Teske}
44474036c4dSDevin Teske
44574036c4dSDevin Teske# f_dialog_menu_constrain $var_height $var_width $var_rows "$prompt" \
44674036c4dSDevin Teske#                         [$min_height [$min_width [$min_rows]]]
44774036c4dSDevin Teske#
44874036c4dSDevin Teske# Modify $var_height to be no-less-than $min_height (if given; zero otherwise)
44974036c4dSDevin Teske# and no-greater-than terminal height (or screen height if $USE_XDIALOG is
45074036c4dSDevin Teske# set).
45174036c4dSDevin Teske#
45274036c4dSDevin Teske# Also modify $var_width to be no-less-than $XDIALOG_MIN_WIDTH (or
45374036c4dSDevin Teske# $XDIALOG_MIN_WIDTH if $_USE_XDIALOG is set) and no-greater-than terminal
45474036c4dSDevin Teske# or screen width. The use of $[X]DIALOG_MIN_WIDTH can be overridden by
45574036c4dSDevin Teske# passing $min_width.
45674036c4dSDevin Teske#
45774036c4dSDevin Teske# Last, modify $var_rows to be no-less-than $min_rows (if specified; zero
45874036c4dSDevin Teske# otherwise) and no-greater-than (max_height - 8) where max_height is the
45974036c4dSDevin Teske# terminal height (or screen height if $USE_XDIALOG is set). If $prompt is NULL
46074036c4dSDevin Teske# or missing, dialog(1) allows $var_rows to be (max_height - 7), maximizing the
46174036c4dSDevin Teske# number of visible rows.
46274036c4dSDevin Teske#
46374036c4dSDevin Teske# Return status is success unless one of the passed arguments is invalid
46474036c4dSDevin Teske# or all of the $var_* arguments are either NULL or missing.
46574036c4dSDevin Teske#
46674036c4dSDevin Teskef_dialog_menu_constrain()
46774036c4dSDevin Teske{
46874036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __var_rows="$3" __prompt="$4"
46974036c4dSDevin Teske	local __min_height="$5" __min_width="$6" __min_rows="$7"
47074036c4dSDevin Teske
47174036c4dSDevin Teske	# Return failure unless at least one var_* argument is passed
47274036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
47374036c4dSDevin Teske		return $FAILURE
47474036c4dSDevin Teske
47574036c4dSDevin Teske	#
47674036c4dSDevin Teske	# Print debug warnings if any given (non-NULL) argument are invalid
47774036c4dSDevin Teske	# NOTE: Don't change the name of $__{var,min,}{height,width,rows}
47874036c4dSDevin Teske	#
479ad40259cSDevin Teske	local __height_menu_constrain __width_menu_constrain
480ad40259cSDevin Teske	local __rows_menu_constrain
48174036c4dSDevin Teske	local __arg __cp __fname=f_dialog_menu_constrain
48274036c4dSDevin Teske	for __arg in height width rows; do
48374036c4dSDevin Teske		debug= f_getvar __var_$__arg __cp
48474036c4dSDevin Teske		[ "$__cp" ] || continue
485ad40259cSDevin Teske		if ! debug= f_getvar "$__cp" __${__arg}_menu_constrain; then
48674036c4dSDevin Teske			f_dprintf "%s: var_%s variable \`%s' not set" \
48774036c4dSDevin Teske			          $__fname $__arg "$__cp"
48874036c4dSDevin Teske			__retval=$FAILURE
489ad40259cSDevin Teske		elif ! eval f_isinteger \$__${__arg}_menu_constrain; then
49074036c4dSDevin Teske			f_dprintf "%s: var_%s variable value not a number" \
49174036c4dSDevin Teske			          $__fname $__arg
49274036c4dSDevin Teske			__retval=$FAILURE
49374036c4dSDevin Teske		fi
49474036c4dSDevin Teske	done
49574036c4dSDevin Teske	for __arg in height width rows; do
49674036c4dSDevin Teske		debug= f_getvar __min_$__arg __cp
49774036c4dSDevin Teske		[ "$__cp" ] || continue
49874036c4dSDevin Teske		f_isinteger "$__cp" && continue
49974036c4dSDevin Teske		f_dprintf "%s: min_%s value not a number" $__fname $__arg
50074036c4dSDevin Teske		__retval=$FAILURE
50174036c4dSDevin Teske		setvar __min_$__arg ""
50274036c4dSDevin Teske	done
50374036c4dSDevin Teske
50474036c4dSDevin Teske	# Obtain maximum height and width values
50574036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
50674036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
50774036c4dSDevin Teske	local __max_height_menu_constrain __max_width_menu_constrain
50874036c4dSDevin Teske	f_dialog_max_size \
50974036c4dSDevin Teske		__max_height_menu_constrain __max_width_menu_constrain
51074036c4dSDevin Teske
51174036c4dSDevin Teske	# Adjust height if desired
51274036c4dSDevin Teske	if [ "$__var_height" ]; then
513ad40259cSDevin Teske		if [ $__height_menu_constrain -lt ${__min_height:-0} ]; then
51474036c4dSDevin Teske			setvar "$__var_height" $__min_height
515ad40259cSDevin Teske		elif [ $__height_menu_constrain -gt \
516ad40259cSDevin Teske		       $__max_height_menu_constrain ]
517ad40259cSDevin Teske		then
51874036c4dSDevin Teske			setvar "$__var_height" $__max_height_menu_constrain
51974036c4dSDevin Teske		fi
52074036c4dSDevin Teske	fi
52174036c4dSDevin Teske
52274036c4dSDevin Teske	# Adjust width if desired
52374036c4dSDevin Teske	if [ "$__var_width" ]; then
52474036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
52574036c4dSDevin Teske			: ${__min_width:=${XDIALOG_MIN_WIDTH:-35}}
52674036c4dSDevin Teske		else
52774036c4dSDevin Teske			: ${__min_width:=${DIALOG_MIN_WIDTH:-24}}
52874036c4dSDevin Teske		fi
529ad40259cSDevin Teske		if [ $__width_menu_constrain -lt $__min_width ]; then
53074036c4dSDevin Teske			setvar "$__var_width" $__min_width
531ad40259cSDevin Teske		elif [ $__width_menu_constrain -gt \
532ad40259cSDevin Teske		       $__max_width_menu_constrain ]
533ad40259cSDevin Teske		then
53474036c4dSDevin Teske			setvar "$__var_width" $__max_width_menu_constrain
53574036c4dSDevin Teske		fi
53674036c4dSDevin Teske	fi
53774036c4dSDevin Teske
53874036c4dSDevin Teske	# Adjust rows if desired
53974036c4dSDevin Teske	if [ "$__var_rows" ]; then
54074036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
54174036c4dSDevin Teske			: ${__min_rows:=1}
54274036c4dSDevin Teske		else
54374036c4dSDevin Teske			: ${__min_rows:=0}
54474036c4dSDevin Teske		fi
54574036c4dSDevin Teske
546ad40259cSDevin Teske		local __max_rows_menu_constrain=$((
547ad40259cSDevin Teske			$__max_height_menu_constrain - 7
548ad40259cSDevin Teske		))
54974036c4dSDevin Teske		# If prompt_len is zero (no prompt), bump the max-rows by 1
55074036c4dSDevin Teske		# Default assumption is (if no argument) that there's no prompt
551ad40259cSDevin Teske		[ ${__prompt_len:-0} -gt 0 ] || __max_rows_menu_constrain=$((
552ad40259cSDevin Teske			$__max_rows_menu_constrain + 1
553ad40259cSDevin Teske		))
55474036c4dSDevin Teske
555ad40259cSDevin Teske		if [ $__rows_menu_constrain -lt $__min_rows ]; then
55674036c4dSDevin Teske			setvar "$__var_rows" $__min_rows
557ad40259cSDevin Teske		elif [ $__rows_menu_constrain -gt $__max_rows_menu_constrain ]
558ad40259cSDevin Teske		then
559ad40259cSDevin Teske			setvar "$__var_rows" $__max_rows_menu_constrain
56074036c4dSDevin Teske		fi
56174036c4dSDevin Teske	fi
56274036c4dSDevin Teske
563790062d2SDevin Teske	if [ "$debug" ]; then
564efa13f7eSDevin Teske		# Print final constrained values to debugging
565efa13f7eSDevin Teske		[ "$__var_height" ] && f_quietly f_getvar "$__var_height"
566efa13f7eSDevin Teske		[ "$__var_width"  ] && f_quietly f_getvar "$__var_width"
567efa13f7eSDevin Teske		[ "$__var_rows"   ] && f_quietly f_getvar "$__var_rows"
568790062d2SDevin Teske	fi
569790062d2SDevin Teske
57074036c4dSDevin Teske	return $__retval # success if no debug warnings were printed
57174036c4dSDevin Teske}
57274036c4dSDevin Teske
57374036c4dSDevin Teske# f_dialog_infobox_size [-n] $var_height $var_width \
57474036c4dSDevin Teske#                       $title $backtitle $prompt [$hline]
575ab2043b8SDevin Teske#
576ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
577ab2043b8SDevin Teske# `--infobox' boxes sensibly.
578ab2043b8SDevin Teske#
57974036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
58074036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
58174036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
58274036c4dSDevin Teske# title, backtitle, prompt, and [optionally] hline. The optimal height and
58374036c4dSDevin Teske# width for the described widget (not exceeding the actual terminal height or
58474036c4dSDevin Teske# width) is stored in $var_height and $var_width (respectively).
58574036c4dSDevin Teske#
58674036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
58774036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
588ab2043b8SDevin Teske#
589ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
590ab2043b8SDevin Teske# dialog(1).
591ab2043b8SDevin Teske#
592ab2043b8SDevin Teskef_dialog_infobox_size()
593ab2043b8SDevin Teske{
59474036c4dSDevin Teske	local __constrain=1
59574036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
59674036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
59774036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
598ab2043b8SDevin Teske
59974036c4dSDevin Teske	# Return unless at least one size aspect has been requested
60074036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
601ab2043b8SDevin Teske
60274036c4dSDevin Teske	# Default height/width of zero for auto-sizing
60374036c4dSDevin Teske	local __height=0 __width=0 __n
604ab2043b8SDevin Teske
60574036c4dSDevin Teske	# Adjust height if desired
60674036c4dSDevin Teske	if [ "$__var_height" ]; then
607ab2043b8SDevin Teske		#
608ab2043b8SDevin Teske		# Set height based on number of rows in prompt
609ab2043b8SDevin Teske		#
61074036c4dSDevin Teske		__n=$( echo -n "$__prompt" | f_number_of_lines )
61174036c4dSDevin Teske		__n=$(( $__n + 2 ))
61274036c4dSDevin Teske		[ $__n -gt $__height ] && __height=$__n
613ab2043b8SDevin Teske
614ab2043b8SDevin Teske		#
61574036c4dSDevin Teske		# For Xdialog(1) bump height if backtitle is enabled (displayed
61674036c4dSDevin Teske		# in the X11 window with a separator line between the backtitle
61774036c4dSDevin Teske		# and msg text).
618ab2043b8SDevin Teske		#
61974036c4dSDevin Teske		if [ "$USE_XDIALOG" -a "$__btitle" ]; then
62074036c4dSDevin Teske			__n=$( echo "$__btitle" | f_number_of_lines )
62174036c4dSDevin Teske			__height=$(( $__height + $__n + 2 ))
622ab2043b8SDevin Teske		fi
623ab2043b8SDevin Teske
62474036c4dSDevin Teske		setvar "$__var_height" $__height
62574036c4dSDevin Teske	fi
626ab2043b8SDevin Teske
62774036c4dSDevin Teske	# Adjust width if desired
62874036c4dSDevin Teske	if [ "$__var_width" ]; then
62974036c4dSDevin Teske		#
63074036c4dSDevin Teske		# Bump width for long titles
63174036c4dSDevin Teske		#
63274036c4dSDevin Teske		__n=$(( ${#__title} + 4 ))
63374036c4dSDevin Teske		[ $__n -gt $__width ] && __width=$__n
63474036c4dSDevin Teske
63574036c4dSDevin Teske		#
63674036c4dSDevin Teske		# If using Xdialog(1), bump width for long backtitles (which
63774036c4dSDevin Teske		# appear within the window).
63874036c4dSDevin Teske		#
63974036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
64074036c4dSDevin Teske			__n=$(( ${#__btitle} + 4 ))
64174036c4dSDevin Teske			[ $__n -gt $__width ] && __width=$__n
64274036c4dSDevin Teske		fi
64374036c4dSDevin Teske
64474036c4dSDevin Teske		#
64574036c4dSDevin Teske		# Bump width for long prompts
64674036c4dSDevin Teske		#
64774036c4dSDevin Teske		__n=$( echo "$__prompt" | f_longest_line_length )
64874036c4dSDevin Teske		__n=$(( $__n + 4 )) # add width for border
64974036c4dSDevin Teske		[ $__n -gt $__width ] && __width=$__n
65074036c4dSDevin Teske
65174036c4dSDevin Teske		#
65274036c4dSDevin Teske		# Bump width for long hlines. Xdialog(1) supports `--hline' but
65374036c4dSDevin Teske		# it's currently not used (so don't do anything here if using
65474036c4dSDevin Teske		# Xdialog(1)).
65574036c4dSDevin Teske		#
65674036c4dSDevin Teske		if [ ! "$USE_XDIALOG" ]; then
65797820530SDevin Teske			__n=$(( ${#__hline} + 12 ))
65874036c4dSDevin Teske			[ $__n -gt $__width ] && __width=$__n
65974036c4dSDevin Teske		fi
66074036c4dSDevin Teske
66174036c4dSDevin Teske		# Bump width by 16.6% if using Xdialog(1)
66274036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __width=$(( $__width + $__width / 6 ))
66374036c4dSDevin Teske
66474036c4dSDevin Teske		setvar "$__var_width" $__width
66574036c4dSDevin Teske	fi
66674036c4dSDevin Teske
66774036c4dSDevin Teske	# Constrain values to sensible minimums/maximums unless `-n' was passed
66874036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
66974036c4dSDevin Teske	[ ! "$__constrain" ] ||
67074036c4dSDevin Teske		f_dialog_size_constrain "$__var_height" "$__var_width"
671ab2043b8SDevin Teske}
672ab2043b8SDevin Teske
67374036c4dSDevin Teske# f_dialog_buttonbox_size [-n] $var_height $var_width \
67474036c4dSDevin Teske#                         $title $backtitle $prompt [$hline]
675ab2043b8SDevin Teske#
676ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
677ab2043b8SDevin Teske# `--msgbox' and `--yesno' boxes sensibly.
678ab2043b8SDevin Teske#
67974036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
68074036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
68174036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
68274036c4dSDevin Teske# title, backtitle, prompt, and [optionally] hline. The optimal height and
68374036c4dSDevin Teske# width for the described widget (not exceeding the actual terminal height or
68474036c4dSDevin Teske# width) is stored in $var_height and $var_width (respectively).
68574036c4dSDevin Teske#
68674036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
68774036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
688ab2043b8SDevin Teske#
689ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
690ab2043b8SDevin Teske# dialog(1).
691ab2043b8SDevin Teske#
692ab2043b8SDevin Teskef_dialog_buttonbox_size()
693ab2043b8SDevin Teske{
69474036c4dSDevin Teske	local __constrain=1
69574036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
69674036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
69774036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
698ab2043b8SDevin Teske
69974036c4dSDevin Teske	# Return unless at least one size aspect has been requested
70074036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
70174036c4dSDevin Teske
70274036c4dSDevin Teske	# Calculate height/width of infobox (adjusted/constrained below)
70374036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
70474036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
70574036c4dSDevin Teske	local __height_bbox_size __width_bbox_size
70674036c4dSDevin Teske	f_dialog_infobox_size -n \
70774036c4dSDevin Teske		"${__var_height:+__height_bbox_size}" \
70874036c4dSDevin Teske		"${__var_width:+__width_bbox_size}" \
70974036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
71074036c4dSDevin Teske
71174036c4dSDevin Teske	# Adjust height if desired
71274036c4dSDevin Teske	if [ "$__var_height" ]; then
713ec65e4f8SPedro F. Giffuni		# Add height to accommodate the buttons
71474036c4dSDevin Teske		__height_bbox_size=$(( $__height_bbox_size + 2 ))
715ab2043b8SDevin Teske
716ab2043b8SDevin Teske		# Adjust for clipping with Xdialog(1) on Linux/GTK2
71774036c4dSDevin Teske		[ "$USE_XDIALOG" ] &&
71874036c4dSDevin Teske			__height_bbox_size=$(( $__height_bbox_size + 3 ))
719ab2043b8SDevin Teske
72074036c4dSDevin Teske		setvar "$__var_height" $__height_bbox_size
721ab2043b8SDevin Teske	fi
722ab2043b8SDevin Teske
72374036c4dSDevin Teske	# No adjustemnts to width, just pass-thru the infobox width
72474036c4dSDevin Teske	if [ "$__var_width" ]; then
72574036c4dSDevin Teske		setvar "$__var_width" $__width_bbox_size
72674036c4dSDevin Teske	fi
72774036c4dSDevin Teske
72874036c4dSDevin Teske	# Constrain values to sensible minimums/maximums unless `-n' was passed
72974036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
73074036c4dSDevin Teske	[ ! "$__constrain" ] ||
73174036c4dSDevin Teske		f_dialog_size_constrain "$__var_height" "$__var_width"
732ab2043b8SDevin Teske}
733ab2043b8SDevin Teske
73474036c4dSDevin Teske# f_dialog_inputbox_size [-n] $var_height $var_width \
73574036c4dSDevin Teske#                        $title $backtitle $prompt $init [$hline]
736ab2043b8SDevin Teske#
737ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
738ab2043b8SDevin Teske# `--inputbox' boxes sensibly.
739ab2043b8SDevin Teske#
74074036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
74174036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
74274036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
74374036c4dSDevin Teske# title, backtitle, prompt, and [optionally] hline. The optimal height and
74474036c4dSDevin Teske# width for the described widget (not exceeding the actual terminal height or
74574036c4dSDevin Teske# width) is stored in $var_height and $var_width (respectively).
74674036c4dSDevin Teske#
74774036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
74874036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
749ab2043b8SDevin Teske#
750ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
751ab2043b8SDevin Teske# dialog(1).
752ab2043b8SDevin Teske#
753ab2043b8SDevin Teskef_dialog_inputbox_size()
754ab2043b8SDevin Teske{
75574036c4dSDevin Teske	local __constrain=1
75674036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
75774036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
75874036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5" __init="$6" __hline="$7"
759ab2043b8SDevin Teske
76074036c4dSDevin Teske	# Return unless at least one size aspect has been requested
76174036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
762ab2043b8SDevin Teske
76374036c4dSDevin Teske	# Calculate height/width of buttonbox (adjusted/constrained below)
76474036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
76574036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
76674036c4dSDevin Teske	local __height_ibox_size __width_ibox_size
76774036c4dSDevin Teske	f_dialog_buttonbox_size -n \
76874036c4dSDevin Teske		"${__var_height:+__height_ibox_size}" \
76974036c4dSDevin Teske		"${__var_width:+__width_ibox_size}" \
77074036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
771ab2043b8SDevin Teske
77274036c4dSDevin Teske	# Adjust height if desired
77374036c4dSDevin Teske	if [ "$__var_height" ]; then
77474036c4dSDevin Teske		# Add height for input box (not needed for Xdialog(1))
77574036c4dSDevin Teske		[ ! "$USE_XDIALOG" ] &&
77674036c4dSDevin Teske			__height_ibox_size=$(( $__height_ibox_size + 3 ))
777ab2043b8SDevin Teske
77874036c4dSDevin Teske		setvar "$__var_height" $__height_ibox_size
779ab2043b8SDevin Teske	fi
780ab2043b8SDevin Teske
78174036c4dSDevin Teske	# Adjust width if desired
78274036c4dSDevin Teske	if [ "$__var_width" ]; then
78374036c4dSDevin Teske		# Bump width for initial text (something neither dialog(1) nor
78474036c4dSDevin Teske		# Xdialog(1) do, but worth it!; add 16.6% if using Xdialog(1))
78574036c4dSDevin Teske		local __n=$(( ${#__init} + 7 ))
78674036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 ))
78774036c4dSDevin Teske		[ $__n -gt $__width_ibox_size ] && __width_ibox_size=$__n
78874036c4dSDevin Teske
78974036c4dSDevin Teske		setvar "$__var_width" $__width_ibox_size
79074036c4dSDevin Teske	fi
79174036c4dSDevin Teske
79274036c4dSDevin Teske	# Constrain values to sensible minimums/maximums unless `-n' was passed
79374036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
79474036c4dSDevin Teske	[ ! "$__constrain" ] ||
79574036c4dSDevin Teske		f_dialog_size_constrain "$__var_height" "$__var_width"
796ab2043b8SDevin Teske}
797ab2043b8SDevin Teske
79874036c4dSDevin Teske# f_xdialog_2inputsbox_size [-n] $var_height $var_width \
79974036c4dSDevin Teske#                           $title $backtitle $prompt \
800ab2043b8SDevin Teske#                           $label1 $init1 $label2 $init2
801ab2043b8SDevin Teske#
802ab2043b8SDevin Teske# Xdialog(1) does not perform auto-sizing of the width and height of
803ab2043b8SDevin Teske# `--2inputsbox' boxes sensibly.
804ab2043b8SDevin Teske#
80574036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
80674036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
80774036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
80874036c4dSDevin Teske# title, backtitle, prompt, label for the first field, initial text for said
80974036c4dSDevin Teske# field, label for the second field, and initial text for said field. The
81074036c4dSDevin Teske# optimal height and width for the described widget (not exceeding the actual
81174036c4dSDevin Teske# terminal height or width) is stored in $var_height and $var_width
81274036c4dSDevin Teske# (respectively).
81374036c4dSDevin Teske#
81474036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
81574036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
816ab2043b8SDevin Teske#
817ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
818ab2043b8SDevin Teske# Xdialog(1).
819ab2043b8SDevin Teske#
820ab2043b8SDevin Teskef_xdialog_2inputsbox_size()
821ab2043b8SDevin Teske{
82274036c4dSDevin Teske	local __constrain=1
82374036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
82474036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
82574036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5"
82674036c4dSDevin Teske	local __label1="$6" __init1="$7" __label2="$8" __init2="$9"
827ab2043b8SDevin Teske
82874036c4dSDevin Teske	# Return unless at least one size aspect has been requested
82974036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
830ab2043b8SDevin Teske
83174036c4dSDevin Teske	# Calculate height/width of inputbox (adjusted/constrained below)
83274036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
83374036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
83474036c4dSDevin Teske	local __height_2ibox_size __width_2ibox_size
83574036c4dSDevin Teske	f_dialog_inputbox_size -n \
83674036c4dSDevin Teske		"${__var_height:+__height_2ibox_size}" \
83774036c4dSDevin Teske		"${__var_width:+__width_2ibox_size}" \
83874036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline" "$__init1"
839ab2043b8SDevin Teske
84074036c4dSDevin Teske	# Adjust height if desired
84174036c4dSDevin Teske	if [ "$__var_height" ]; then
84274036c4dSDevin Teske		# Add height for 1st label, 2nd label, and 2nd input box
84374036c4dSDevin Teske		__height_2ibox_size=$(( $__height_2ibox_size + 2 + 2 + 2  ))
84474036c4dSDevin Teske		setvar "$__var_height" $__height_2ibox_size
845ab2043b8SDevin Teske	fi
846ab2043b8SDevin Teske
84774036c4dSDevin Teske	# Adjust width if desired
84874036c4dSDevin Teske	if [ "$__var_width" ]; then
84974036c4dSDevin Teske		local __n
850ab2043b8SDevin Teske
85174036c4dSDevin Teske		# Bump width for first label text (+16.6% since Xdialog(1))
85274036c4dSDevin Teske		__n=$(( ${#__label1} + 7 ))
85374036c4dSDevin Teske		__n=$(( $__n + $__n / 6 ))
85474036c4dSDevin Teske		[ $__n -gt $__width_2ibox_size ] && __width_2ibox_size=$__n
855ab2043b8SDevin Teske
85674036c4dSDevin Teske		# Bump width for second label text (+16.6% since Xdialog(1))
85774036c4dSDevin Teske		__n=$(( ${#__label2} + 7 ))
85874036c4dSDevin Teske		__n=$(( $__n + $__n / 6 ))
85974036c4dSDevin Teske		[ $__n -gt $__width_2ibox_size ] && __width_2ibox_size=$__n
860ab2043b8SDevin Teske
86174036c4dSDevin Teske		# Bump width for 2nd initial text (something neither dialog(1)
86274036c4dSDevin Teske		# nor Xdialog(1) do, but worth it!; +16.6% since Xdialog(1))
86374036c4dSDevin Teske		__n=$(( ${#__init2} + 7 ))
86474036c4dSDevin Teske		__n=$(( $__n + $__n / 6 ))
86574036c4dSDevin Teske		[ $__n -gt $__width_2ibox_size ] && __width_2ibox_size=$__n
86674036c4dSDevin Teske
86774036c4dSDevin Teske		setvar "$__var_width" $__width_2ibox_size
868ab2043b8SDevin Teske	fi
869ab2043b8SDevin Teske
87074036c4dSDevin Teske	# Constrain values to sensible minimums/maximums unless `-n' was passed
87174036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
87274036c4dSDevin Teske	[ ! "$__constrain" ] ||
87374036c4dSDevin Teske		f_dialog_size_constrain "$__var_height" "$__var_width"
874ab2043b8SDevin Teske}
875ab2043b8SDevin Teske
87674036c4dSDevin Teske# f_dialog_menu_size [-n] $var_height $var_width $var_rows \
87774036c4dSDevin Teske#                    $title $backtitle $prompt $hline \
878ab2043b8SDevin Teske#                    $tag1 $item1 $tag2 $item2 ...
879ab2043b8SDevin Teske#
880ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
881ab2043b8SDevin Teske# `--menu' boxes sensibly.
882ab2043b8SDevin Teske#
88374036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
88474036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
88574036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
88674036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
88774036c4dSDevin Teske# arguments are the menu list itself (comprised of tag/item couplets). The
88874036c4dSDevin Teske# optimal height, width, and rows for the described widget (not exceeding the
88974036c4dSDevin Teske# actual terminal height or width) is stored in $var_height, $var_width, and
89074036c4dSDevin Teske# $var_rows (respectively).
891ab2043b8SDevin Teske#
89274036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
89374036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
894ab2043b8SDevin Teske#
895ab2043b8SDevin Teskef_dialog_menu_size()
896ab2043b8SDevin Teske{
89774036c4dSDevin Teske	local __constrain=1
89874036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
89974036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __var_rows="$3"
90074036c4dSDevin Teske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
90174036c4dSDevin Teske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
902ab2043b8SDevin Teske
90374036c4dSDevin Teske	# Return unless at least one size aspect has been requested
90474036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
90574036c4dSDevin Teske		return $FAILURE
906ab2043b8SDevin Teske
90774036c4dSDevin Teske	# Calculate height/width of infobox (adjusted/constrained below)
90874036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
90974036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
91074036c4dSDevin Teske	local __height_menu_size __width_menu_size
91174036c4dSDevin Teske	f_dialog_infobox_size -n \
91274036c4dSDevin Teske		"${__var_height:+__height_menu_size}" \
91374036c4dSDevin Teske		"${__var_width:+__width_menu_size}" \
91474036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
915ab2043b8SDevin Teske
916ab2043b8SDevin Teske	#
91774036c4dSDevin Teske	# Always process the menu-item arguments to get the longest tag-length,
91874036c4dSDevin Teske	# longest item-length (both used to bump the width), and the number of
91974036c4dSDevin Teske	# rows (used to bump the height).
920ab2043b8SDevin Teske	#
92174036c4dSDevin Teske	local __longest_tag=0 __longest_item=0 __rows=0
922ab2043b8SDevin Teske	while [ $# -ge 2 ]; do
92374036c4dSDevin Teske		local __tag="$1" __item="$2"
924ab2043b8SDevin Teske		shift 2 # tag/item
92574036c4dSDevin Teske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
92674036c4dSDevin Teske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
92774036c4dSDevin Teske		__rows=$(( $__rows + 1 ))
928ab2043b8SDevin Teske	done
929ab2043b8SDevin Teske
93074036c4dSDevin Teske	# Adjust rows early (for up-comning height calculation)
93174036c4dSDevin Teske	if [ "$__var_height" -o "$__var_rows" ]; then
93274036c4dSDevin Teske		# Add a row for visual aid if using Xdialog(1)
93374036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __rows=$(( $__rows + 1 ))
934ab2043b8SDevin Teske	fi
935ab2043b8SDevin Teske
93674036c4dSDevin Teske	# Adjust height if desired
93774036c4dSDevin Teske	if [ "$__var_height" ]; then
93874036c4dSDevin Teske		# Add rows to height
939ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
94074036c4dSDevin Teske			__height_menu_size=$((
94174036c4dSDevin Teske				$__height_menu_size + $__rows + 7 ))
942ab2043b8SDevin Teske		else
94374036c4dSDevin Teske			__height_menu_size=$((
94474036c4dSDevin Teske				$__height_menu_size + $__rows + 4 ))
945ab2043b8SDevin Teske		fi
94674036c4dSDevin Teske		setvar "$__var_height" $__height_menu_size
94774036c4dSDevin Teske	fi
948ab2043b8SDevin Teske
94974036c4dSDevin Teske	# Adjust width if desired
95074036c4dSDevin Teske	if [ "$__var_width" ]; then
95174036c4dSDevin Teske		# The sum total between the longest tag-length and the
95274036c4dSDevin Teske		# longest item-length should be used to bump menu width
95374036c4dSDevin Teske		local __n=$(( $__longest_tag + $__longest_item + 10 ))
95474036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
95574036c4dSDevin Teske		[ $__n -gt $__width_menu_size ] && __width_menu_size=$__n
95674036c4dSDevin Teske
95774036c4dSDevin Teske		setvar "$__var_width" $__width_menu_size
95874036c4dSDevin Teske	fi
95974036c4dSDevin Teske
96074036c4dSDevin Teske	# Store adjusted rows if desired
96174036c4dSDevin Teske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows
96274036c4dSDevin Teske
96374036c4dSDevin Teske	# Constrain height, width, and rows to sensible minimum/maximum values
96474036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
96574036c4dSDevin Teske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
96674036c4dSDevin Teske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
967ab2043b8SDevin Teske}
968ab2043b8SDevin Teske
96974036c4dSDevin Teske# f_dialog_menu_with_help_size [-n] $var_height $var_width $var_rows \
97074036c4dSDevin Teske#                              $title $backtitle $prompt $hline \
971ab2043b8SDevin Teske#                              $tag1 $item1 $help1 $tag2 $item2 $help2 ...
972ab2043b8SDevin Teske#
973ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
974ab2043b8SDevin Teske# `--menu' boxes sensibly.
975ab2043b8SDevin Teske#
97674036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
97774036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
97874036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
97974036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
98074036c4dSDevin Teske# arguments are the menu list itself (comprised of tag/item/help triplets). The
98174036c4dSDevin Teske# optimal height, width, and rows for the described widget (not exceeding the
98274036c4dSDevin Teske# actual terminal height or width) is stored in $var_height, $var_width, and
98374036c4dSDevin Teske# $var_rows (respectively).
984ab2043b8SDevin Teske#
98574036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
98674036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
987ab2043b8SDevin Teske#
988ab2043b8SDevin Teskef_dialog_menu_with_help_size()
989ab2043b8SDevin Teske{
99074036c4dSDevin Teske	local __constrain=1
99174036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
99274036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __var_rows="$3"
99374036c4dSDevin Teske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
99474036c4dSDevin Teske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
995ab2043b8SDevin Teske
99674036c4dSDevin Teske	# Return unless at least one size aspect has been requested
99774036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
99874036c4dSDevin Teske		return $FAILURE
999ab2043b8SDevin Teske
100074036c4dSDevin Teske	# Calculate height/width of infobox (adjusted/constrained below)
100174036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
100274036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
100374036c4dSDevin Teske	local __height_menu_with_help_size __width_menu_with_help_size
100474036c4dSDevin Teske	f_dialog_infobox_size -n \
100574036c4dSDevin Teske		"${__var_height:+__height_menu_with_help_size}" \
100674036c4dSDevin Teske		"${__var_width:+__width_menu_with_help_size}" \
100774036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
1008ab2043b8SDevin Teske
1009ab2043b8SDevin Teske	#
101074036c4dSDevin Teske	# Always process the menu-item arguments to get the longest tag-length,
101174036c4dSDevin Teske	# longest item-length, longest help-length (help-length only considered
101274036c4dSDevin Teske	# if using Xdialog(1), as it places the help string in the widget) --
101374036c4dSDevin Teske	# all used to bump the width -- and the number of rows (used to bump
101474036c4dSDevin Teske	# the height).
1015ab2043b8SDevin Teske	#
101674036c4dSDevin Teske	local __longest_tag=0 __longest_item=0 __longest_help=0 __rows=0
1017ab2043b8SDevin Teske	while [ $# -ge 3 ]; do
101874036c4dSDevin Teske		local __tag="$1" __item="$2" __help="$3"
1019ab2043b8SDevin Teske		shift 3 # tag/item/help
102074036c4dSDevin Teske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
102174036c4dSDevin Teske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
102274036c4dSDevin Teske		[ ${#__help} -gt $__longest_help ] && __longest_help=${#__help}
102374036c4dSDevin Teske		__rows=$(( $__rows + 1 ))
1024ab2043b8SDevin Teske	done
1025ab2043b8SDevin Teske
102674036c4dSDevin Teske	# Adjust rows early (for up-coming height calculation)
102774036c4dSDevin Teske	if [ "$__var_height" -o "$__var_rows" ]; then
102874036c4dSDevin Teske		# Add a row for visual aid if using Xdialog(1)
102974036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __rows=$(( $__rows + 1 ))
103074036c4dSDevin Teske	fi
103174036c4dSDevin Teske
103274036c4dSDevin Teske	# Adjust height if desired
103374036c4dSDevin Teske	if [ "$__var_height" ]; then
103474036c4dSDevin Teske		# Add rows to height
103574036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
103674036c4dSDevin Teske			__height_menu_with_help_size=$((
103774036c4dSDevin Teske				$__height_menu_with_help_size + $__rows + 8 ))
1038ab2043b8SDevin Teske		else
103974036c4dSDevin Teske			__height_menu_with_help_size=$((
104074036c4dSDevin Teske				$__height_menu_with_help_size + $__rows + 4 ))
1041ab2043b8SDevin Teske		fi
104274036c4dSDevin Teske		setvar "$__var_height" $__height_menu_with_help_size
1043ab2043b8SDevin Teske	fi
1044ab2043b8SDevin Teske
104574036c4dSDevin Teske	# Adjust width if desired
104674036c4dSDevin Teske	if [ "$__var_width" ]; then
104774036c4dSDevin Teske		# The sum total between the longest tag-length and the
104874036c4dSDevin Teske		# longest item-length should be used to bump menu width
104974036c4dSDevin Teske		local __n=$(( $__longest_tag + $__longest_item + 10 ))
105074036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
105174036c4dSDevin Teske		[ $__n -gt $__width_menu_with_help_size ] &&
105274036c4dSDevin Teske			__width_menu_with_help_size=$__n
105374036c4dSDevin Teske
1054ab2043b8SDevin Teske		# Update width for help text if using Xdialog(1)
1055ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
105674036c4dSDevin Teske			__n=$(( $__longest_help + 10 ))
105774036c4dSDevin Teske			__n=$(( $__n + $__n / 6 )) # plus 16.6%
105874036c4dSDevin Teske			[ $__n -gt $__width_menu_with_help_size ] &&
105974036c4dSDevin Teske				__width_menu_with_help_size=$__n
1060ab2043b8SDevin Teske		fi
1061ab2043b8SDevin Teske
106274036c4dSDevin Teske		setvar "$__var_width" $__width_menu_with_help_size
1063ab2043b8SDevin Teske	fi
1064ab2043b8SDevin Teske
106574036c4dSDevin Teske	# Store adjusted rows if desired
106674036c4dSDevin Teske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows
106774036c4dSDevin Teske
106874036c4dSDevin Teske	# Constrain height, width, and rows to sensible minimum/maximum values
106974036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
107074036c4dSDevin Teske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
107174036c4dSDevin Teske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
1072ab2043b8SDevin Teske}
1073ab2043b8SDevin Teske
107474036c4dSDevin Teske# f_dialog_radiolist_size [-n] $var_height $var_width $var_rows \
107574036c4dSDevin Teske#                         $title $backtitle $prompt $hline \
1076ab2043b8SDevin Teske#                         $tag1 $item1 $status1 $tag2 $item2 $status2 ...
1077ab2043b8SDevin Teske#
1078ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1079ab2043b8SDevin Teske# `--radiolist' boxes sensibly.
1080ab2043b8SDevin Teske#
108174036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
108274036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
108374036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
108474036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
108574036c4dSDevin Teske# arguments are the radio list itself (comprised of tag/item/status triplets).
108674036c4dSDevin Teske# The optimal height, width, and rows for the described widget (not exceeding
108774036c4dSDevin Teske# the actual terminal height or width) is stored in $var_height, $var_width,
108874036c4dSDevin Teske# and $var_rows (respectively).
1089ab2043b8SDevin Teske#
109074036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
109174036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
1092ab2043b8SDevin Teske#
1093ab2043b8SDevin Teskef_dialog_radiolist_size()
1094ab2043b8SDevin Teske{
109574036c4dSDevin Teske	local __constrain=1
109674036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
109774036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __var_rows="$3"
109874036c4dSDevin Teske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
109974036c4dSDevin Teske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
1100ab2043b8SDevin Teske
110174036c4dSDevin Teske	# Return unless at least one size aspect has been requested
110274036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
110374036c4dSDevin Teske		return $FAILURE
1104ab2043b8SDevin Teske
110574036c4dSDevin Teske	# Calculate height/width of infobox (adjusted/constrained below)
110674036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
110774036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
110874036c4dSDevin Teske	local __height_rlist_size __width_rlist_size
110974036c4dSDevin Teske	f_dialog_infobox_size -n \
111074036c4dSDevin Teske		"${__var_height:+__height_rlist_size}" \
111174036c4dSDevin Teske		"${__var_width:+__width_rlist_size}" \
111274036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
1113ab2043b8SDevin Teske
1114ab2043b8SDevin Teske	#
111574036c4dSDevin Teske	# Always process the menu-item arguments to get the longest tag-length,
111674036c4dSDevin Teske	# longest item-length (both used to bump the width), and the number of
111774036c4dSDevin Teske	# rows (used to bump the height).
1118ab2043b8SDevin Teske	#
1119ad40259cSDevin Teske	local __longest_tag=0 __longest_item=0 __rows_rlist_size=0
1120efc0f5e2SDevin Teske	while [ $# -ge 3 ]; do
112174036c4dSDevin Teske		local __tag="$1" __item="$2"
1122efc0f5e2SDevin Teske		shift 3 # tag/item/status
112374036c4dSDevin Teske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
112474036c4dSDevin Teske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
1125ad40259cSDevin Teske		__rows_rlist_size=$(( $__rows_rlist_size + 1 ))
1126ab2043b8SDevin Teske	done
1127ab2043b8SDevin Teske
112874036c4dSDevin Teske	# Adjust rows early (for up-coming height calculation)
112974036c4dSDevin Teske	if [ "$__var_height" -o "$__var_rows" ]; then
113074036c4dSDevin Teske		# Add a row for visual aid if using Xdialog(1)
1131ad40259cSDevin Teske		[ "$USE_XDIALOG" ] &&
1132ad40259cSDevin Teske			__rows_rlist_size=$(( $__rows_rlist_size + 1 ))
1133ab2043b8SDevin Teske	fi
1134ab2043b8SDevin Teske
113574036c4dSDevin Teske	# Adjust height if desired
113674036c4dSDevin Teske	if [ "$__var_height" ]; then
113774036c4dSDevin Teske		# Add rows to height
1138ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
113974036c4dSDevin Teske			__height_rlist_size=$((
1140ad40259cSDevin Teske				$__height_rlist_size + $__rows_rlist_size + 7
1141ad40259cSDevin Teske			))
1142ab2043b8SDevin Teske		else
114374036c4dSDevin Teske			__height_rlist_size=$((
1144ad40259cSDevin Teske				$__height_rlist_size + $__rows_rlist_size + 4
1145ad40259cSDevin Teske			))
1146ab2043b8SDevin Teske		fi
114774036c4dSDevin Teske		setvar "$__var_height" $__height_rlist_size
114874036c4dSDevin Teske	fi
1149ab2043b8SDevin Teske
115074036c4dSDevin Teske	# Adjust width if desired
115174036c4dSDevin Teske	if [ "$__var_width" ]; then
115274036c4dSDevin Teske		# Sum total between longest tag-length, longest item-length,
115374036c4dSDevin Teske		# and radio-button width should be used to bump menu width
115474036c4dSDevin Teske		local __n=$(( $__longest_tag + $__longest_item + 13 ))
115574036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
115674036c4dSDevin Teske		[ $__n -gt $__width_rlist_size ] && __width_rlist_size=$__n
115774036c4dSDevin Teske
115874036c4dSDevin Teske		setvar "$__var_width" $__width_rlist_size
115974036c4dSDevin Teske	fi
116074036c4dSDevin Teske
116174036c4dSDevin Teske	# Store adjusted rows if desired
1162ad40259cSDevin Teske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows_rlist_size
116374036c4dSDevin Teske
116474036c4dSDevin Teske	# Constrain height, width, and rows to sensible minimum/maximum values
116574036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
116674036c4dSDevin Teske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
116774036c4dSDevin Teske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
1168ab2043b8SDevin Teske}
1169ab2043b8SDevin Teske
117074036c4dSDevin Teske# f_dialog_checklist_size [-n] $var_height $var_width $var_rows \
117174036c4dSDevin Teske#                         $title $backtitle $prompt $hline \
1172dbc6a6e1SDevin Teske#                         $tag1 $item1 $status1 $tag2 $item2 $status2 ...
1173dbc6a6e1SDevin Teske#
1174dbc6a6e1SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1175dbc6a6e1SDevin Teske# `--checklist' boxes sensibly.
1176dbc6a6e1SDevin Teske#
117774036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
117874036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
117974036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
118074036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
118174036c4dSDevin Teske# arguments are the check list itself (comprised of tag/item/status triplets).
118274036c4dSDevin Teske# The optimal height, width, and rows for the described widget (not exceeding
118374036c4dSDevin Teske# the actual terminal height or width) is stored in $var_height, $var_width,
118474036c4dSDevin Teske# and $var_rows (respectively).
1185dbc6a6e1SDevin Teske#
118674036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
118774036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
1188dbc6a6e1SDevin Teske#
1189dbc6a6e1SDevin Teskef_dialog_checklist_size()
1190dbc6a6e1SDevin Teske{
1191dbc6a6e1SDevin Teske	f_dialog_radiolist_size "$@"
1192dbc6a6e1SDevin Teske}
1193dbc6a6e1SDevin Teske
119474036c4dSDevin Teske# f_dialog_radiolist_with_help_size [-n] $var_height $var_width $var_rows \
119574036c4dSDevin Teske#                                   $title $backtitle $prompt $hline \
1196dbc6a6e1SDevin Teske#                                   $tag1 $item1 $status1 $help1 \
1197dbc6a6e1SDevin Teske#                                   $tag2 $item2 $status2 $help2 ...
1198dbc6a6e1SDevin Teske#
1199dbc6a6e1SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1200dbc6a6e1SDevin Teske# `--radiolist' boxes sensibly.
1201dbc6a6e1SDevin Teske#
120274036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
120374036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
120474036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
120574036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
120674036c4dSDevin Teske# arguments are the radio list itself (comprised of tag/item/status/help
120774036c4dSDevin Teske# quadruplets). The optimal height, width, and rows for the described widget
120874036c4dSDevin Teske# (not exceeding the actual terminal height or width) is stored in $var_height,
120974036c4dSDevin Teske# $var_width, and $var_rows (respectively).
1210dbc6a6e1SDevin Teske#
121174036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
121274036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
1213dbc6a6e1SDevin Teske#
1214dbc6a6e1SDevin Teskef_dialog_radiolist_with_help_size()
1215dbc6a6e1SDevin Teske{
121674036c4dSDevin Teske	local __constrain=1
121774036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
121874036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __var_rows="$3"
121974036c4dSDevin Teske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
122074036c4dSDevin Teske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
1221dbc6a6e1SDevin Teske
122274036c4dSDevin Teske	# Return unless at least one size aspect has been requested
122374036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
122474036c4dSDevin Teske		return $FAILURE
1225dbc6a6e1SDevin Teske
122674036c4dSDevin Teske	# Calculate height/width of infobox (adjusted/constrained below)
122774036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
122874036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
122974036c4dSDevin Teske	local __height_rlist_with_help_size __width_rlist_with_help_size
123074036c4dSDevin Teske	f_dialog_infobox_size -n \
123174036c4dSDevin Teske		"${__var_height:+__height_rlist_with_help_size}" \
123274036c4dSDevin Teske		"${__var_width:+__width_rlist_with_help_size}" \
123374036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
1234dbc6a6e1SDevin Teske
1235dbc6a6e1SDevin Teske	#
123674036c4dSDevin Teske	# Always process the menu-item arguments to get the longest tag-length,
123774036c4dSDevin Teske	# longest item-length, longest help-length (help-length only considered
123874036c4dSDevin Teske	# if using Xdialog(1), as it places the help string in the widget) --
123974036c4dSDevin Teske	# all used to bump the width -- and the number of rows (used to bump
124074036c4dSDevin Teske	# the height).
1241dbc6a6e1SDevin Teske	#
1242ad40259cSDevin Teske	local __longest_tag=0 __longest_item=0 __longest_help=0
1243ad40259cSDevin Teske	local __rows_rlist_with_help_size=0
1244dbc6a6e1SDevin Teske	while [ $# -ge 4 ]; do
124574036c4dSDevin Teske		local __tag="$1" __item="$2" __status="$3" __help="$4"
1246dbc6a6e1SDevin Teske		shift 4 # tag/item/status/help
124774036c4dSDevin Teske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
124874036c4dSDevin Teske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
124974036c4dSDevin Teske		[ ${#__help} -gt $__longest_help ] && __longest_help=${#__help}
1250ad40259cSDevin Teske		__rows_rlist_with_help_size=$((
1251ad40259cSDevin Teske			$__rows_rlist_with_help_size + 1
1252ad40259cSDevin Teske		))
1253dbc6a6e1SDevin Teske	done
1254dbc6a6e1SDevin Teske
125574036c4dSDevin Teske	# Adjust rows early (for up-coming height calculation)
125674036c4dSDevin Teske	if [ "$__var_height" -o "$__var_rows" ]; then
125774036c4dSDevin Teske		# Add a row for visual aid if using Xdialog(1)
1258ad40259cSDevin Teske		[ "$USE_XDIALOG" ] &&
1259ad40259cSDevin Teske			__rows_rlist_with_help_size=$((
1260ad40259cSDevin Teske				$__rows_rlist_with_help_size + 1
1261ad40259cSDevin Teske			))
126274036c4dSDevin Teske	fi
126374036c4dSDevin Teske
126474036c4dSDevin Teske	# Adjust height if desired
126574036c4dSDevin Teske	if [ "$__var_height" ]; then
126674036c4dSDevin Teske		# Add rows to height
126774036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
126874036c4dSDevin Teske			__height_rlist_with_help_size=$((
1269ad40259cSDevin Teske				$__height_rlist_with_help_size +
1270ad40259cSDevin Teske				$__rows_rlist_with_help_size + 7
1271ad40259cSDevin Teske			))
1272dbc6a6e1SDevin Teske		else
127374036c4dSDevin Teske			__height_rlist_with_help_size=$((
1274ad40259cSDevin Teske				$__height_rlist_with_help_size +
1275ad40259cSDevin Teske				$__rows_rlist_with_help_size + 4
1276ad40259cSDevin Teske			))
1277dbc6a6e1SDevin Teske		fi
127874036c4dSDevin Teske		setvar "$__var_height" $__height
1279dbc6a6e1SDevin Teske	fi
1280dbc6a6e1SDevin Teske
128174036c4dSDevin Teske	# Adjust width if desired
128274036c4dSDevin Teske	if [ "$__var_width" ]; then
128374036c4dSDevin Teske		# Sum total between longest tag-length, longest item-length,
128474036c4dSDevin Teske		# and radio-button width should be used to bump menu width
128574036c4dSDevin Teske		local __n=$(( $__longest_tag + $__longest_item + 13 ))
128674036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
128774036c4dSDevin Teske		[ $__n -gt $__width_rlist_with_help_size ] &&
128874036c4dSDevin Teske			__width_rlist_with_help_size=$__n
128974036c4dSDevin Teske
1290dbc6a6e1SDevin Teske		# Update width for help text if using Xdialog(1)
1291dbc6a6e1SDevin Teske		if [ "$USE_XDIALOG" ]; then
129274036c4dSDevin Teske			__n=$(( $__longest_help + 10 ))
129374036c4dSDevin Teske			__n=$(( $__n + $__n / 6 )) # plus 16.6%
129474036c4dSDevin Teske			[ $__n -gt $__width_rlist_with_help_size ] &&
129574036c4dSDevin Teske				__width_rlist_with_help_size=$__n
1296dbc6a6e1SDevin Teske		fi
1297dbc6a6e1SDevin Teske
129874036c4dSDevin Teske		setvar "$__var_width" $__width_rlist_with_help_size
1299dbc6a6e1SDevin Teske	fi
1300dbc6a6e1SDevin Teske
130174036c4dSDevin Teske	# Store adjusted rows if desired
1302ad40259cSDevin Teske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows_rlist_with_help_size
130374036c4dSDevin Teske
130474036c4dSDevin Teske	# Constrain height, width, and rows to sensible minimum/maximum values
130574036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
130674036c4dSDevin Teske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
130774036c4dSDevin Teske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
1308dbc6a6e1SDevin Teske}
1309dbc6a6e1SDevin Teske
131074036c4dSDevin Teske# f_dialog_checklist_with_help_size [-n] $var_height $var_width $var_rows \
131174036c4dSDevin Teske#                                   $title $backtitle $prompt $hline \
1312dbc6a6e1SDevin Teske#                                   $tag1 $item1 $status1 $help1 \
1313dbc6a6e1SDevin Teske#                                   $tag2 $item2 $status2 $help2 ...
1314dbc6a6e1SDevin Teske#
1315dbc6a6e1SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1316dbc6a6e1SDevin Teske# `--checklist' boxes sensibly.
1317dbc6a6e1SDevin Teske#
131874036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
131974036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
132074036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
132174036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
132274036c4dSDevin Teske# arguments are the check list itself (comprised of tag/item/status/help
132374036c4dSDevin Teske# quadruplets). The optimal height, width, and rows for the described widget
132474036c4dSDevin Teske# (not exceeding the actual terminal height or width) is stored in $var_height,
132574036c4dSDevin Teske# $var_width, and $var_rows (respectively).
1326dbc6a6e1SDevin Teske#
132774036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
132874036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
1329dbc6a6e1SDevin Teske#
1330dbc6a6e1SDevin Teskef_dialog_checklist_with_help_size()
1331dbc6a6e1SDevin Teske{
1332dbc6a6e1SDevin Teske	f_dialog_radiolist_with_help_size "$@"
1333dbc6a6e1SDevin Teske}
1334dbc6a6e1SDevin Teske
133574036c4dSDevin Teske# f_dialog_calendar_size [-n] $var_height $var_width \
133674036c4dSDevin Teske#                        $title $backtitle $prompt [$hline]
1337ab2043b8SDevin Teske#
1338ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1339ab2043b8SDevin Teske# `--calendar' boxes sensibly.
1340ab2043b8SDevin Teske#
134174036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
134274036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
134374036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
134474036c4dSDevin Teske# title, backtitle, prompt, and [optionally] hline. The optimal height and
134574036c4dSDevin Teske# width for the described widget (not exceeding the actual terminal height or
134674036c4dSDevin Teske# width) is stored in $var_height and $var_width (respectively).
134774036c4dSDevin Teske#
134874036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
134974036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
1350ab2043b8SDevin Teske#
1351ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
1352ab2043b8SDevin Teske# dialog(1).
1353ab2043b8SDevin Teske#
1354ab2043b8SDevin Teskef_dialog_calendar_size()
1355ab2043b8SDevin Teske{
135674036c4dSDevin Teske	local __constrain=1
135774036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
135874036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
135974036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
1360ab2043b8SDevin Teske
136174036c4dSDevin Teske	# Return unless at least one size aspect has been requested
136274036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
136374036c4dSDevin Teske
136474036c4dSDevin Teske	#
136574036c4dSDevin Teske	# Obtain/Adjust minimum and maximum thresholds
136674036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
136774036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
136874036c4dSDevin Teske	#
136974036c4dSDevin Teske	local __max_height_cal_size __max_width_cal_size
137074036c4dSDevin Teske	f_dialog_max_size __max_height_cal_size __max_width_cal_size
137174036c4dSDevin Teske	__max_width_cal_size=$(( $__max_width_cal_size - 2 ))
137274036c4dSDevin Teske		# the calendar box will refuse to display if too wide
137374036c4dSDevin Teske	local __min_width
1374ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
137574036c4dSDevin Teske		__min_width=55
1376ab2043b8SDevin Teske	else
137774036c4dSDevin Teske		__min_width=40
137874036c4dSDevin Teske		__max_height_cal_size=$((
137974036c4dSDevin Teske			$__max_height_cal_size - $DIALOG_CALENDAR_HEIGHT ))
138074036c4dSDevin Teske		# When using dialog(1), we can't predict whether the user has
138174036c4dSDevin Teske		# disabled shadow's in their `$HOME/.dialogrc' file, so we'll
138274036c4dSDevin Teske		# subtract one for the potential shadow around the widget
138374036c4dSDevin Teske		__max_height_cal_size=$(( $__max_height_cal_size - 1 ))
1384ab2043b8SDevin Teske	fi
1385ab2043b8SDevin Teske
138674036c4dSDevin Teske	# Calculate height if desired
138774036c4dSDevin Teske	if [ "$__var_height" ]; then
138874036c4dSDevin Teske		local __height
138974036c4dSDevin Teske		__height=$( echo "$__prompt" | f_number_of_lines )
1390ab2043b8SDevin Teske
1391ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
1392ec65e4f8SPedro F. Giffuni			# Add height to accommodate for embedded calendar widget
139374036c4dSDevin Teske			__height=$(( $__height + $DIALOG_CALENDAR_HEIGHT - 1 ))
1394ab2043b8SDevin Teske
1395ab2043b8SDevin Teske			# Also, bump height if backtitle is enabled
139674036c4dSDevin Teske			if [ "$__btitle" ]; then
139774036c4dSDevin Teske				local __n
139874036c4dSDevin Teske				__n=$( echo "$__btitle" | f_number_of_lines )
139974036c4dSDevin Teske				__height=$(( $__height + $__n + 2 ))
1400ab2043b8SDevin Teske			fi
1401ab2043b8SDevin Teske		else
140274036c4dSDevin Teske			[ "$__prompt" ] && __height=$(( $__height + 1 ))
1403ab2043b8SDevin Teske		fi
1404ab2043b8SDevin Teske
140574036c4dSDevin Teske		# Enforce maximum height, unless `-n' was passed
140674036c4dSDevin Teske		[ "$__constrain" -a $__height -gt $__max_height_cal_size ] &&
140774036c4dSDevin Teske			__height=$__max_height_cal_size
1408ab2043b8SDevin Teske
140974036c4dSDevin Teske		setvar "$__var_height" $__height
141074036c4dSDevin Teske	fi
141174036c4dSDevin Teske
141274036c4dSDevin Teske	# Calculate width if desired
141374036c4dSDevin Teske	if [ "$__var_width" ]; then
141474036c4dSDevin Teske		# NOTE: Function name appended to prevent __var_{height,width}
141574036c4dSDevin Teske		#       values from becoming local (and thus preventing setvar
141674036c4dSDevin Teske		#       from working).
141774036c4dSDevin Teske		local __width_cal_size
141874036c4dSDevin Teske		f_dialog_infobox_size -n "" __width_cal_size \
141974036c4dSDevin Teske			"$__title" "$__btitle" "$__prompt" "$__hline"
142074036c4dSDevin Teske
142174036c4dSDevin Teske		# Enforce minimum/maximum width, unless `-n' was passed
142274036c4dSDevin Teske		if [ "$__constrain" ]; then
142374036c4dSDevin Teske			if [ $__width_cal_size -lt $__min_width ]; then
142474036c4dSDevin Teske				__width_cal_size=$__min_width
142574036c4dSDevin Teske			elif [ $__width_cal_size -gt $__max_width_cal_size ]
142674036c4dSDevin Teske			then
142774036c4dSDevin Teske				__width_cal_size=$__max_width_size
142874036c4dSDevin Teske			fi
142974036c4dSDevin Teske		fi
143074036c4dSDevin Teske
143174036c4dSDevin Teske		setvar "$__var_width" $__width_cal_size
143274036c4dSDevin Teske	fi
143374036c4dSDevin Teske
143474036c4dSDevin Teske	return $SUCCESS
1435ab2043b8SDevin Teske}
1436ab2043b8SDevin Teske
143774036c4dSDevin Teske# f_dialog_timebox_size [-n] $var_height $var_width \
143874036c4dSDevin Teske#                       $title $backtitle $prompt [$hline]
1439ab2043b8SDevin Teske#
1440ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1441ab2043b8SDevin Teske# `--timebox' boxes sensibly.
1442ab2043b8SDevin Teske#
144374036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
144474036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
144574036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
144674036c4dSDevin Teske# title, backtitle, prompt, and [optionally] hline. The optional height and
144774036c4dSDevin Teske# width for the described widget (not exceeding the actual terminal height or
144874036c4dSDevin Teske# width) is stored in $var_height and $var_width (respectively).
144974036c4dSDevin Teske#
145074036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
145174036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
1452ab2043b8SDevin Teske#
1453ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
1454ab2043b8SDevin Teske# dialog(1).
1455ab2043b8SDevin Teske#
1456ab2043b8SDevin Teskef_dialog_timebox_size()
1457ab2043b8SDevin Teske{
145874036c4dSDevin Teske	local __constrain=1
145974036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
146074036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
146174036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
1462ab2043b8SDevin Teske
146374036c4dSDevin Teske	# Return unless at least one size aspect has been requested
146474036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
1465ab2043b8SDevin Teske
1466ab2043b8SDevin Teske	#
146774036c4dSDevin Teske	# Obtain/Adjust minimum and maximum thresholds
146874036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
146974036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
147074036c4dSDevin Teske	#
147174036c4dSDevin Teske	local __max_height_tbox_size __max_width_tbox_size
147274036c4dSDevin Teske	f_dialog_max_size __max_height_tbox_size __max_width_tbox_size
147374036c4dSDevin Teske	__max_width_tbox_size=$(( $__max_width_tbox_size - 2 ))
147474036c4dSDevin Teske		# the timebox widget refuses to display if too wide
147574036c4dSDevin Teske	local __min_width
147674036c4dSDevin Teske	if [ "$USE_XDIALOG" ]; then
147774036c4dSDevin Teske		__min_width=40
147874036c4dSDevin Teske	else
147974036c4dSDevin Teske		__min_width=20
148074036c4dSDevin Teske		__max_height_tbox_size=$(( \
148174036c4dSDevin Teske			$__max_height_tbox_size - $DIALOG_TIMEBOX_HEIGHT ))
148274036c4dSDevin Teske		# When using dialog(1), we can't predict whether the user has
148374036c4dSDevin Teske		# disabled shadow's in their `$HOME/.dialogrc' file, so we'll
148474036c4dSDevin Teske		# subtract one for the potential shadow around the widget
148574036c4dSDevin Teske		__max_height_tbox_size=$(( $__max_height_tbox_size - 1 ))
148674036c4dSDevin Teske	fi
148774036c4dSDevin Teske
148874036c4dSDevin Teske	# Calculate height if desired
148974036c4dSDevin Teske	if [ "$__var_height" -a "$USE_XDIALOG" ]; then
149074036c4dSDevin Teske		# When using Xdialog(1), the height seems to have
149174036c4dSDevin Teske		# no effect. All values provide the same results.
149274036c4dSDevin Teske		setvar "$__var_height" 0 # autosize
149374036c4dSDevin Teske	elif [ "$__var_height" ]; then
149474036c4dSDevin Teske		local __height
149574036c4dSDevin Teske		__height=$( echo "$__prompt" | f_number_of_lines )
149674036c4dSDevin Teske		__height=$(( $__height ${__prompt:++1} + 1 ))
149774036c4dSDevin Teske
149874036c4dSDevin Teske		# Enforce maximum height, unless `-n' was passed
149974036c4dSDevin Teske		[ "$__constrain" -a $__height -gt $__max_height_tbox_size ] &&
150074036c4dSDevin Teske			__height=$__max_height_tbox_size
150174036c4dSDevin Teske
150274036c4dSDevin Teske		setvar "$__var_height" $__height
150374036c4dSDevin Teske	fi
150474036c4dSDevin Teske
150574036c4dSDevin Teske	# Calculate width if desired
150674036c4dSDevin Teske	if [ "$__var_width" ]; then
150774036c4dSDevin Teske		# NOTE: Function name appended to prevent __var_{height,width}
150874036c4dSDevin Teske		#       values from becoming local (and thus preventing setvar
150974036c4dSDevin Teske		#       from working).
151074036c4dSDevin Teske		local __width_tbox_size
151174036c4dSDevin Teske		f_dialog_infobox_size -n "" __width_tbox_size \
151274036c4dSDevin Teske			"$__title" "$__btitle" "$__prompt" "$__hline"
151374036c4dSDevin Teske
1514ab2043b8SDevin Teske		# Enforce the minimum width for displaying the timebox
151574036c4dSDevin Teske		if [ "$__constrain" ]; then
151674036c4dSDevin Teske			if [ $__width_tbox_size -lt $__min_width ]; then
151774036c4dSDevin Teske				__width_tbox_size=$__min_width
151874036c4dSDevin Teske			elif [ $__width_tbox_size -ge $__max_width_tbox_size ]
151974036c4dSDevin Teske			then
152074036c4dSDevin Teske				__width_tbox_size=$__max_width_tbox_size
152174036c4dSDevin Teske			fi
1522ab2043b8SDevin Teske		fi
1523ab2043b8SDevin Teske
152474036c4dSDevin Teske		setvar "$__var_width" $__width_tbox_size
152574036c4dSDevin Teske	fi
1526ab2043b8SDevin Teske
152774036c4dSDevin Teske	return $SUCCESS
1528ab2043b8SDevin Teske}
1529ab2043b8SDevin Teske
1530ab2043b8SDevin Teske############################################################ CLEAR FUNCTIONS
1531ab2043b8SDevin Teske
1532ab2043b8SDevin Teske# f_dialog_clear
1533ab2043b8SDevin Teske#
1534ab2043b8SDevin Teske# Clears any/all previous dialog(1) displays.
1535ab2043b8SDevin Teske#
1536ab2043b8SDevin Teskef_dialog_clear()
1537ab2043b8SDevin Teske{
1538ab2043b8SDevin Teske	$DIALOG --clear
1539ab2043b8SDevin Teske}
1540ab2043b8SDevin Teske
1541ab2043b8SDevin Teske############################################################ INFO FUNCTIONS
1542ab2043b8SDevin Teske
1543ab2043b8SDevin Teske# f_dialog_info $info_text ...
1544ab2043b8SDevin Teske#
1545ab2043b8SDevin Teske# Throw up a dialog(1) infobox. The infobox remains until another dialog is
15465ffea47aSDevin Teske# displayed or `dialog --clear' (or f_dialog_clear) is called.
1547ab2043b8SDevin Teske#
1548ab2043b8SDevin Teskef_dialog_info()
1549ab2043b8SDevin Teske{
155074036c4dSDevin Teske	local info_text="$*" height width
155174036c4dSDevin Teske	f_dialog_infobox_size height width \
155274036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$info_text"
155374036c4dSDevin Teske	$DIALOG \
155474036c4dSDevin Teske		--title "$DIALOG_TITLE"         \
155574036c4dSDevin Teske		--backtitle "$DIALOG_BACKTITLE" \
1556ab2043b8SDevin Teske		${USE_XDIALOG:+--ignore-eof}    \
1557ab2043b8SDevin Teske		${USE_XDIALOG:+--no-buttons}    \
155874036c4dSDevin Teske		--infobox "$info_text" $height $width
1559ab2043b8SDevin Teske}
1560ab2043b8SDevin Teske
1561ab2043b8SDevin Teske# f_xdialog_info $info_text ...
1562ab2043b8SDevin Teske#
1563ab2043b8SDevin Teske# Throw up an Xdialog(1) infobox and do not dismiss it until stdin produces
1564ab2043b8SDevin Teske# EOF. This implies that you must execute this either as an rvalue to a pipe,
1565ab2043b8SDevin Teske# lvalue to indirection or in a sub-shell that provides data on stdin.
1566ab2043b8SDevin Teske#
1567341e5ca3SDevin Teske# To open an Xdialog(1) infobox that does not disappear until expeclitly dis-
1568341e5ca3SDevin Teske# missed, use the following:
1569341e5ca3SDevin Teske#
1570341e5ca3SDevin Teske# 	f_xdialog_info "$info_text" < /dev/tty &
1571341e5ca3SDevin Teske# 	pid=$!
1572341e5ca3SDevin Teske# 	# Perform some lengthy actions
1573341e5ca3SDevin Teske# 	kill $pid
1574341e5ca3SDevin Teske#
1575341e5ca3SDevin Teske# NB: Check $USE_XDIALOG if you need to support both dialog(1) and Xdialog(1).
1576341e5ca3SDevin Teske#
1577ab2043b8SDevin Teskef_xdialog_info()
1578ab2043b8SDevin Teske{
157974036c4dSDevin Teske	local info_text="$*" height width
158074036c4dSDevin Teske	f_dialog_infobox_size height width \
158174036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$info_text"
1582341e5ca3SDevin Teske	exec $DIALOG \
158374036c4dSDevin Teske		--title "$DIALOG_TITLE"               \
158474036c4dSDevin Teske		--backtitle "$DIALOG_BACKTITLE"       \
1585ab2043b8SDevin Teske		--no-close --no-buttons               \
158674036c4dSDevin Teske		--infobox "$info_text" $height $width \
1587ab2043b8SDevin Teske		-1 # timeout of -1 means abort when EOF on stdin
1588ab2043b8SDevin Teske}
1589ab2043b8SDevin Teske
15903c63cc68SDevin Teske############################################################ PAUSE FUNCTIONS
15913c63cc68SDevin Teske
15923c63cc68SDevin Teske# f_dialog_pause $msg_text $duration [$hline]
15933c63cc68SDevin Teske#
15943c63cc68SDevin Teske# Display a message in a widget with a progress bar that runs backward for
15953c63cc68SDevin Teske# $duration seconds.
15963c63cc68SDevin Teske#
15973c63cc68SDevin Teskef_dialog_pause()
15983c63cc68SDevin Teske{
15993c63cc68SDevin Teske	local pause_text="$1" duration="$2" hline="$3" height width
16003c63cc68SDevin Teske	f_isinteger "$duration" || return $FAILURE
16013c63cc68SDevin Teske	f_dialog_buttonbox_size height width \
16023c63cc68SDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$pause_text" "$hline"
16033c63cc68SDevin Teske	if [ "$USE_XDIALOG" ]; then
16043c63cc68SDevin Teske		$DIALOG \
16053c63cc68SDevin Teske			--title "$DIALOG_TITLE"         \
16063c63cc68SDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
16073c63cc68SDevin Teske			--ok-label "$msg_skip"          \
16083c63cc68SDevin Teske			--cancel-label "$msg_cancel"    \
16093c63cc68SDevin Teske			${noCancel:+--no-cancel}        \
16103c63cc68SDevin Teske			--timeout "$duration"           \
16113c63cc68SDevin Teske			--yesno "$pause_text"           \
16123c63cc68SDevin Teske			$height $width
16133c63cc68SDevin Teske	else
16143c63cc68SDevin Teske		[ $duration -gt 0 ] && duration=$(( $duration - 1 ))
16153c63cc68SDevin Teske		height=$(( $height + 3 )) # Add height for progress bar
16163c63cc68SDevin Teske		$DIALOG \
16173c63cc68SDevin Teske			--title "$DIALOG_TITLE"         \
16183c63cc68SDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
16193c63cc68SDevin Teske			--hline "$hline"                \
16203c63cc68SDevin Teske			--ok-label "$msg_skip"          \
16213c63cc68SDevin Teske			--cancel-label "$msg_cancel"    \
16223c63cc68SDevin Teske			${noCancel:+--no-cancel}        \
16233c63cc68SDevin Teske			--pause "$pause_text"           \
16243c63cc68SDevin Teske			$height $width "$duration"
16253c63cc68SDevin Teske	fi
16263c63cc68SDevin Teske}
16273c63cc68SDevin Teske
16283c63cc68SDevin Teske# f_dialog_pause_no_cancel $msg_text $duration [$hline]
16293c63cc68SDevin Teske#
16303c63cc68SDevin Teske# Display a message in a widget with a progress bar that runs backward for
16313c63cc68SDevin Teske# $duration seconds. No cancel button is provided. Always returns success.
16323c63cc68SDevin Teske#
16333c63cc68SDevin Teskef_dialog_pause_no_cancel()
16343c63cc68SDevin Teske{
16353c63cc68SDevin Teske	noCancel=1 f_dialog_pause "$@"
16363c63cc68SDevin Teske	return $SUCCESS
16373c63cc68SDevin Teske}
16383c63cc68SDevin Teske
1639ab2043b8SDevin Teske############################################################ MSGBOX FUNCTIONS
1640ab2043b8SDevin Teske
16415a97d360SDevin Teske# f_dialog_msgbox $msg_text [$hline]
1642ab2043b8SDevin Teske#
1643ab2043b8SDevin Teske# Throw up a dialog(1) msgbox. The msgbox remains until the user presses ENTER
1644ab2043b8SDevin Teske# or ESC, acknowledging the modal dialog.
1645ab2043b8SDevin Teske#
1646ab2043b8SDevin Teske# If the user presses ENTER, the exit status is zero (success), otherwise if
1647ab2043b8SDevin Teske# the user presses ESC the exit status is 255.
1648ab2043b8SDevin Teske#
1649ab2043b8SDevin Teskef_dialog_msgbox()
1650ab2043b8SDevin Teske{
16515a97d360SDevin Teske	local msg_text="$1" hline="$2" height width
165274036c4dSDevin Teske	f_dialog_buttonbox_size height width \
16535a97d360SDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$msg_text" "$hline"
165474036c4dSDevin Teske	$DIALOG \
165574036c4dSDevin Teske		--title "$DIALOG_TITLE"         \
165674036c4dSDevin Teske		--backtitle "$DIALOG_BACKTITLE" \
16575a97d360SDevin Teske		--hline "$hline"                \
165874036c4dSDevin Teske		--ok-label "$msg_ok"            \
165974036c4dSDevin Teske		--msgbox "$msg_text" $height $width
1660ab2043b8SDevin Teske}
1661ab2043b8SDevin Teske
166240dfc82dSDevin Teske############################################################ TEXTBOX FUNCTIONS
166340dfc82dSDevin Teske
166440dfc82dSDevin Teske# f_dialog_textbox $file
166540dfc82dSDevin Teske#
166640dfc82dSDevin Teske# Display the contents of $file (or an error if $file does not exist, etc.) in
166740dfc82dSDevin Teske# a dialog(1) textbox (which has a scrollable region for the text). The textbox
166840dfc82dSDevin Teske# remains until the user presses ENTER or ESC, acknowledging the modal dialog.
166940dfc82dSDevin Teske#
167040dfc82dSDevin Teske# If the user presses ENTER, the exit status is zero (success), otherwise if
167140dfc82dSDevin Teske# the user presses ESC the exit status is 255.
167240dfc82dSDevin Teske#
167340dfc82dSDevin Teskef_dialog_textbox()
167440dfc82dSDevin Teske{
167540dfc82dSDevin Teske	local file="$1"
167674036c4dSDevin Teske	local contents height width retval
167740dfc82dSDevin Teske
167840dfc82dSDevin Teske	contents=$( cat "$file" 2>&1 )
167940dfc82dSDevin Teske	retval=$?
168040dfc82dSDevin Teske
168174036c4dSDevin Teske	f_dialog_buttonbox_size height width \
168274036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$contents"
168340dfc82dSDevin Teske
168440dfc82dSDevin Teske	if [ $retval -eq $SUCCESS ]; then
168574036c4dSDevin Teske		$DIALOG \
168674036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
168774036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
168874036c4dSDevin Teske			--exit-label "$msg_ok"          \
168940dfc82dSDevin Teske			--no-cancel                     \
169074036c4dSDevin Teske			--textbox "$file" $height $width
169140dfc82dSDevin Teske	else
169274036c4dSDevin Teske		$DIALOG \
169374036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
169474036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
169574036c4dSDevin Teske			--ok-label "$msg_ok"            \
169674036c4dSDevin Teske			--msgbox "$contents" $height $width
169740dfc82dSDevin Teske	fi
169840dfc82dSDevin Teske}
169940dfc82dSDevin Teske
1700ab2043b8SDevin Teske############################################################ YESNO FUNCTIONS
1701ab2043b8SDevin Teske
170268d5c9afSDevin Teske# f_dialog_yesno $msg_text [$hline]
1703ab2043b8SDevin Teske#
1704ab2043b8SDevin Teske# Display a dialog(1) Yes/No prompt to allow the user to make some decision.
1705ab2043b8SDevin Teske# The yesno prompt remains until the user presses ENTER or ESC, acknowledging
1706ab2043b8SDevin Teske# the modal dialog.
1707ab2043b8SDevin Teske#
1708ab2043b8SDevin Teske# If the user chooses YES the exit status is zero, or chooses NO the exit
1709ab2043b8SDevin Teske# status is one, or presses ESC the exit status is 255.
1710ab2043b8SDevin Teske#
1711ab2043b8SDevin Teskef_dialog_yesno()
1712ab2043b8SDevin Teske{
171368d5c9afSDevin Teske	local msg_text="$1" height width
171468d5c9afSDevin Teske	local hline="${2-$hline_arrows_tab_enter}"
17153da75d2eSDevin Teske
17163da75d2eSDevin Teske	f_interactive || return 0 # If non-interactive, return YES all the time
17173da75d2eSDevin Teske
171874036c4dSDevin Teske	f_dialog_buttonbox_size height width \
171974036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$msg_text" "$hline"
1720ab2043b8SDevin Teske
1721ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
172274036c4dSDevin Teske		$DIALOG \
172374036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
172474036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
172574036c4dSDevin Teske			--hline "$hline"                \
172674036c4dSDevin Teske			--ok-label "$msg_yes"           \
172774036c4dSDevin Teske			--cancel-label "$msg_no"        \
172874036c4dSDevin Teske			--yesno "$msg_text" $height $width
1729ab2043b8SDevin Teske	else
173074036c4dSDevin Teske		$DIALOG \
173174036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
173274036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
173374036c4dSDevin Teske			--hline "$hline"                \
173474036c4dSDevin Teske			--yes-label "$msg_yes"          \
173574036c4dSDevin Teske			--no-label "$msg_no"            \
173674036c4dSDevin Teske			--yesno "$msg_text" $height $width
1737ab2043b8SDevin Teske	fi
1738ab2043b8SDevin Teske}
1739ab2043b8SDevin Teske
174068d5c9afSDevin Teske# f_dialog_noyes $msg_text [$hline]
1741ab2043b8SDevin Teske#
1742ab2043b8SDevin Teske# Display a dialog(1) No/Yes prompt to allow the user to make some decision.
1743ab2043b8SDevin Teske# The noyes prompt remains until the user presses ENTER or ESC, acknowledging
1744ab2043b8SDevin Teske# the modal dialog.
1745ab2043b8SDevin Teske#
1746ab2043b8SDevin Teske# If the user chooses YES the exit status is zero, or chooses NO the exit
1747ab2043b8SDevin Teske# status is one, or presses ESC the exit status is 255.
1748ab2043b8SDevin Teske#
1749ab2043b8SDevin Teske# NOTE: This is just like the f_dialog_yesno function except "No" is default.
1750ab2043b8SDevin Teske#
1751ab2043b8SDevin Teskef_dialog_noyes()
1752ab2043b8SDevin Teske{
175368d5c9afSDevin Teske	local msg_text="$1" height width
175468d5c9afSDevin Teske	local hline="${2-$hline_arrows_tab_enter}"
17553da75d2eSDevin Teske
17563da75d2eSDevin Teske	f_interactive || return 1 # If non-interactive, return NO all the time
17573da75d2eSDevin Teske
175874036c4dSDevin Teske	f_dialog_buttonbox_size height width \
175974036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$msg_text" "$hline"
1760ab2043b8SDevin Teske
1761ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
176274036c4dSDevin Teske		$DIALOG \
176374036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
176474036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
176574036c4dSDevin Teske			--hline "$hline"                \
1766ab2043b8SDevin Teske			--default-no                    \
176774036c4dSDevin Teske			--ok-label "$msg_yes"           \
176874036c4dSDevin Teske			--cancel-label "$msg_no"        \
176974036c4dSDevin Teske			--yesno "$msg_text" $height $width
1770ab2043b8SDevin Teske	else
177174036c4dSDevin Teske		$DIALOG \
177274036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
177374036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
177474036c4dSDevin Teske			--hline "$hline"                \
1775ab2043b8SDevin Teske			--defaultno                     \
177674036c4dSDevin Teske			--yes-label "$msg_yes"          \
177774036c4dSDevin Teske			--no-label "$msg_no"            \
177874036c4dSDevin Teske			--yesno "$msg_text" $height $width
1779ab2043b8SDevin Teske	fi
1780ab2043b8SDevin Teske}
1781ab2043b8SDevin Teske
1782ab2043b8SDevin Teske############################################################ INPUT FUNCTIONS
1783ab2043b8SDevin Teske
1784ec7120b5SDevin Teske# f_dialog_inputstr_store [-s] $text
1785ec7120b5SDevin Teske#
1786ec7120b5SDevin Teske# Store some text from a dialog(1) inputbox to be retrieved later by
1787ec7120b5SDevin Teske# f_dialog_inputstr_fetch(). If the first argument is `-s', the text is
1788ec7120b5SDevin Teske# sanitized before being stored.
1789ec7120b5SDevin Teske#
1790ec7120b5SDevin Teskef_dialog_inputstr_store()
1791ec7120b5SDevin Teske{
1792ec7120b5SDevin Teske	local sanitize=
1793ec7120b5SDevin Teske	[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
1794ec7120b5SDevin Teske	local text="$1"
1795ec7120b5SDevin Teske
1796ec7120b5SDevin Teske	# Sanitize the line before storing it if desired
1797ec7120b5SDevin Teske	[ "$sanitize" ] && f_dialog_line_sanitize text
1798ec7120b5SDevin Teske
1799ec7120b5SDevin Teske	setvar DIALOG_INPUTBOX_$$ "$text"
1800ec7120b5SDevin Teske}
1801ec7120b5SDevin Teske
1802ec7120b5SDevin Teske# f_dialog_inputstr_fetch [$var_to_set]
1803ab2043b8SDevin Teske#
1804ab2043b8SDevin Teske# Obtain the inputstr entered by the user from the most recently displayed
1805ec7120b5SDevin Teske# dialog(1) inputbox (previously stored with f_dialog_inputstr_store() above).
1806ec7120b5SDevin Teske# If $var_to_set is NULL or missing, output is printed to stdout (which is less
1807ec7120b5SDevin Teske# recommended due to performance degradation; in a loop for example).
1808ab2043b8SDevin Teske#
1809ec7120b5SDevin Teskef_dialog_inputstr_fetch()
1810ab2043b8SDevin Teske{
1811ec7120b5SDevin Teske	local __var_to_set="$1" __cp
1812ec7120b5SDevin Teske
1813ec7120b5SDevin Teske	debug= f_getvar DIALOG_INPUTBOX_$$ "${__var_to_set:-__cp}" # get data
181489498fdfSDevin Teske	setvar DIALOG_INPUTBOX_$$ "" # scrub memory in case data was sensitive
1815ec7120b5SDevin Teske
1816ec7120b5SDevin Teske	# Return the line on standard-out if desired
1817ec7120b5SDevin Teske	[ "$__var_to_set" ] || echo "$__cp"
1818ec7120b5SDevin Teske
1819ab2043b8SDevin Teske	return $SUCCESS
1820ab2043b8SDevin Teske}
1821ab2043b8SDevin Teske
1822ec7120b5SDevin Teske# f_dialog_input $var_to_set $prompt [$init [$hline]]
1823298cf604SDevin Teske#
1824298cf604SDevin Teske# Prompt the user with a dialog(1) inputbox to enter some value. The inputbox
1825cef367e6SEitan Adler# remains until the user presses ENTER or ESC, or otherwise ends the
1826f4844f65SDevin Teske# editing session (by selecting `Cancel' for example).
1827298cf604SDevin Teske#
1828298cf604SDevin Teske# If the user presses ENTER, the exit status is zero (success), otherwise if
1829298cf604SDevin Teske# the user presses ESC the exit status is 255, or if the user chose Cancel, the
1830298cf604SDevin Teske# exit status is instead 1.
1831298cf604SDevin Teske#
1832298cf604SDevin Teske# NOTE: The hline should correspond to the type of data you want from the user.
1833298cf604SDevin Teske# NOTE: Should not be used to edit multiline values.
1834298cf604SDevin Teske#
1835298cf604SDevin Teskef_dialog_input()
1836298cf604SDevin Teske{
1837ec7120b5SDevin Teske	local __var_to_set="$1" __prompt="$2" __init="$3" __hline="$4"
1838ec7120b5SDevin Teske
1839ec7120b5SDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
1840ec7120b5SDevin Teske	#       from becoming local (and thus preventing setvar from working).
1841ec7120b5SDevin Teske	local __height_input __width_input
1842ec7120b5SDevin Teske	f_dialog_inputbox_size __height_input __width_input \
184374036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" \
1844ec7120b5SDevin Teske		"$__prompt" "$__init" "$__hline"
1845298cf604SDevin Teske
1846ec7120b5SDevin Teske	local __opterm="--"
1847ec7120b5SDevin Teske	[ "$USE_XDIALOG" ] && __opterm=
1848298cf604SDevin Teske
1849ec7120b5SDevin Teske	local __dialog_input
1850ec7120b5SDevin Teske	__dialog_input=$(
185174036c4dSDevin Teske		$DIALOG \
185274036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
185374036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
1854ec7120b5SDevin Teske			--hline "$__hline"              \
185574036c4dSDevin Teske			--ok-label "$msg_ok"            \
185674036c4dSDevin Teske			--cancel-label "$msg_cancel"    \
1857ec7120b5SDevin Teske			--inputbox "$__prompt"          \
1858ec7120b5SDevin Teske			$__height_input $__width_input  \
1859ec7120b5SDevin Teske			$__opterm "$__init"             \
1860298cf604SDevin Teske			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1861298cf604SDevin Teske	)
1862ec7120b5SDevin Teske	local __retval=$?
1863298cf604SDevin Teske
1864ec7120b5SDevin Teske	# Remove warnings and leading/trailing whitespace from user input
1865ec7120b5SDevin Teske	f_dialog_line_sanitize __dialog_input
1866298cf604SDevin Teske
1867ec7120b5SDevin Teske	setvar "$__var_to_set" "$__dialog_input"
1868ec7120b5SDevin Teske	return $__retval
1869298cf604SDevin Teske}
1870298cf604SDevin Teske
1871ab2043b8SDevin Teske############################################################ MENU FUNCTIONS
1872ab2043b8SDevin Teske
1873fd962ac6SDevin Teske# f_dialog_menutag_store [-s] $text
1874fd962ac6SDevin Teske#
1875fd962ac6SDevin Teske# Store some text from a dialog(1) menu to be retrieved later by
1876fd962ac6SDevin Teske# f_dialog_menutag_fetch(). If the first argument is `-s', the text is
1877fd962ac6SDevin Teske# sanitized before being stored.
1878fd962ac6SDevin Teske#
1879fd962ac6SDevin Teskef_dialog_menutag_store()
1880fd962ac6SDevin Teske{
1881fd962ac6SDevin Teske	local sanitize=
1882fd962ac6SDevin Teske	[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
1883fd962ac6SDevin Teske	local text="$1"
1884fd962ac6SDevin Teske
1885fd962ac6SDevin Teske	# Sanitize the menutag before storing it if desired
1886fd962ac6SDevin Teske	[ "$sanitize" ] && f_dialog_data_sanitize text
1887fd962ac6SDevin Teske
1888fd962ac6SDevin Teske	setvar DIALOG_MENU_$$ "$text"
1889fd962ac6SDevin Teske}
1890fd962ac6SDevin Teske
1891fd962ac6SDevin Teske# f_dialog_menutag_fetch [$var_to_set]
1892ab2043b8SDevin Teske#
1893ab2043b8SDevin Teske# Obtain the menutag chosen by the user from the most recently displayed
1894fd962ac6SDevin Teske# dialog(1) menu (previously stored with f_dialog_menutag_store() above). If
1895fd962ac6SDevin Teske# $var_to_set is NULL or missing, output is printed to stdout (which is less
1896fd962ac6SDevin Teske# recommended due to performance degradation; in a loop for example).
1897ab2043b8SDevin Teske#
1898fd962ac6SDevin Teskef_dialog_menutag_fetch()
1899ab2043b8SDevin Teske{
1900fd962ac6SDevin Teske	local __var_to_set="$1" __cp
1901fd962ac6SDevin Teske
1902fd962ac6SDevin Teske	debug= f_getvar DIALOG_MENU_$$ "${__var_to_set:-__cp}" # get the data
190389498fdfSDevin Teske	setvar DIALOG_MENU_$$ "" # scrub memory in case data was sensitive
1904fd962ac6SDevin Teske
1905fd962ac6SDevin Teske	# Return the data on standard-out if desired
1906fd962ac6SDevin Teske	[ "$__var_to_set" ] || echo "$__cp"
1907fd962ac6SDevin Teske
1908ab2043b8SDevin Teske	return $SUCCESS
1909ab2043b8SDevin Teske}
1910ab2043b8SDevin Teske
1911b5bd0bacSDevin Teske# f_dialog_menuitem_store [-s] $text
1912b5bd0bacSDevin Teske#
1913b5bd0bacSDevin Teske# Store the item from a dialog(1) menu (see f_dialog_menutag2item()) to be
1914b5bd0bacSDevin Teske# retrieved later by f_dialog_menuitem_fetch(). If the first argument is `-s',
1915b5bd0bacSDevin Teske# the text is sanitized before being stored.
1916b5bd0bacSDevin Teske#
1917b5bd0bacSDevin Teskef_dialog_menuitem_store()
1918b5bd0bacSDevin Teske{
1919b5bd0bacSDevin Teske	local sanitize=
1920b5bd0bacSDevin Teske	[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
1921b5bd0bacSDevin Teske	local text="$1"
1922b5bd0bacSDevin Teske
1923b5bd0bacSDevin Teske	# Sanitize the menuitem before storing it if desired
1924b5bd0bacSDevin Teske	[ "$sanitize" ] && f_dialog_data_sanitize text
1925b5bd0bacSDevin Teske
1926b5bd0bacSDevin Teske	setvar DIALOG_MENUITEM_$$ "$text"
1927b5bd0bacSDevin Teske}
1928b5bd0bacSDevin Teske
1929b5bd0bacSDevin Teske# f_dialog_menuitem_fetch [$var_to_set]
1930b5bd0bacSDevin Teske#
1931b5bd0bacSDevin Teske# Obtain the menuitem chosen by the user from the most recently displayed
1932b5bd0bacSDevin Teske# dialog(1) menu (previously stored with f_dialog_menuitem_store() above). If
1933b5bd0bacSDevin Teske# $var_to_set is NULL or missing, output is printed to stdout (which is less
1934b5bd0bacSDevin Teske# recommended due to performance degradation; in a loop for example).
1935b5bd0bacSDevin Teske#
1936b5bd0bacSDevin Teskef_dialog_menuitem_fetch()
1937b5bd0bacSDevin Teske{
1938b5bd0bacSDevin Teske	local __var_to_set="$1" __cp
1939b5bd0bacSDevin Teske
1940b5bd0bacSDevin Teske	debug= f_getvar DIALOG_MENUITEM_$$ "${__var_to_set:-__cp}" # get data
1941b5bd0bacSDevin Teske	setvar DIALOG_MENUITEM_$$ "" # scrub memory in case data was sensitive
1942b5bd0bacSDevin Teske
1943b5bd0bacSDevin Teske	# Return the data on standard-out if desired
1944b5bd0bacSDevin Teske	[ "$__var_to_set" ] || echo "$__cp"
1945b5bd0bacSDevin Teske
1946b5bd0bacSDevin Teske	return $SUCCESS
1947b5bd0bacSDevin Teske}
1948b5bd0bacSDevin Teske
194977599778SDevin Teske# f_dialog_default_store [-s] $text
195077599778SDevin Teske#
195177599778SDevin Teske# Store some text to be used later as the --default-item argument to dialog(1)
195277599778SDevin Teske# (or Xdialog(1)) for --menu, --checklist, and --radiolist widgets. Retrieve
195377599778SDevin Teske# the text later with f_dialog_menutag_fetch(). If the first argument is `-s',
195477599778SDevin Teske# the text is sanitized before being stored.
195577599778SDevin Teske#
195677599778SDevin Teskef_dialog_default_store()
195777599778SDevin Teske{
195877599778SDevin Teske	local sanitize=
195977599778SDevin Teske	[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
196077599778SDevin Teske	local text="$1"
196177599778SDevin Teske
196277599778SDevin Teske	# Sanitize the defaulitem before storing it if desired
196377599778SDevin Teske	[ "$sanitize" ] && f_dialog_data_sanitize text
196477599778SDevin Teske
196577599778SDevin Teske	setvar DEFAULTITEM_$$ "$text"
196677599778SDevin Teske}
196777599778SDevin Teske
196877599778SDevin Teske# f_dialog_default_fetch [$var_to_set]
196977599778SDevin Teske#
197077599778SDevin Teske# Obtain text to be used with the --default-item argument of dialog(1) (or
197177599778SDevin Teske# Xdialog(1)) (previously stored with f_dialog_default_store() above). If
197277599778SDevin Teske# $var_to_set is NULL or missing, output is printed to stdout (which is less
197377599778SDevin Teske# recommended due to performance degradation; in a loop for example).
197477599778SDevin Teske#
197577599778SDevin Teskef_dialog_default_fetch()
197677599778SDevin Teske{
197777599778SDevin Teske	local __var_to_set="$1" __cp
197877599778SDevin Teske
197977599778SDevin Teske	debug= f_getvar DEFAULTITEM_$$ "${__var_to_set:-__cp}" # get the data
198077599778SDevin Teske	setvar DEFAULTITEM_$$ "" # scrub memory in case data was sensitive
198177599778SDevin Teske
198277599778SDevin Teske	# Return the data on standard-out if desired
198377599778SDevin Teske	[ "$__var_to_set" ] || echo "$__cp"
198477599778SDevin Teske
198577599778SDevin Teske	return $SUCCESS
198677599778SDevin Teske}
198777599778SDevin Teske
1988ab2043b8SDevin Teske# f_dialog_menutag2item $tag_chosen $tag1 $item1 $tag2 $item2 ...
1989ab2043b8SDevin Teske#
1990ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) you must pass an ordered list of
1991ab2043b8SDevin Teske# tag/item pairs on the command-line. When the user selects a menu option the
1992ab2043b8SDevin Teske# tag for that item is printed to stderr.
1993ab2043b8SDevin Teske#
1994ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into
1995ab2043b8SDevin Teske# the item associated with said tag.
1996ab2043b8SDevin Teske#
1997ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the
1998ab2043b8SDevin Teske# ordered list of tag/item pairs (HINT: use the same tag/item list as was
1999ab2043b8SDevin Teske# passed to dialog(1) for consistency).
2000ab2043b8SDevin Teske#
2001ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned.
2002ab2043b8SDevin Teske#
2003ab2043b8SDevin Teskef_dialog_menutag2item()
2004ab2043b8SDevin Teske{
2005ab2043b8SDevin Teske	local tag="$1" tagn item
2006ab2043b8SDevin Teske	shift 1 # tag
2007ab2043b8SDevin Teske
2008ab2043b8SDevin Teske	while [ $# -gt 0 ]; do
2009ab2043b8SDevin Teske		tagn="$1"
2010ab2043b8SDevin Teske		item="$2"
2011ab2043b8SDevin Teske		shift 2 # tagn/item
2012ab2043b8SDevin Teske
2013ab2043b8SDevin Teske		if [ "$tag" = "$tagn" ]; then
2014ab2043b8SDevin Teske			echo "$item"
2015ab2043b8SDevin Teske			return $SUCCESS
2016ab2043b8SDevin Teske		fi
2017ab2043b8SDevin Teske	done
2018ab2043b8SDevin Teske	return $FAILURE
2019ab2043b8SDevin Teske}
2020ab2043b8SDevin Teske
2021ab2043b8SDevin Teske# f_dialog_menutag2item_with_help $tag_chosen $tag1 $item1 $help1 \
2022ab2043b8SDevin Teske#                                             $tag2 $item2 $help2 ...
2023ab2043b8SDevin Teske#
2024ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) with the `--item-help' option, you
2025ab2043b8SDevin Teske# must pass an ordered list of tag/item/help triplets on the command-line. When
2026ab2043b8SDevin Teske# the user selects a menu option the tag for that item is printed to stderr.
2027ab2043b8SDevin Teske#
2028ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into
2029ab2043b8SDevin Teske# the item associated with said tag (help is discarded/ignored).
2030ab2043b8SDevin Teske#
2031ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the
2032ab2043b8SDevin Teske# ordered list of tag/item/help triplets (HINT: use the same tag/item/help list
2033ab2043b8SDevin Teske# as was passed to dialog(1) for consistency).
2034ab2043b8SDevin Teske#
2035ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned.
2036ab2043b8SDevin Teske#
2037ab2043b8SDevin Teskef_dialog_menutag2item_with_help()
2038ab2043b8SDevin Teske{
2039ab2043b8SDevin Teske	local tag="$1" tagn item
2040ab2043b8SDevin Teske	shift 1 # tag
2041ab2043b8SDevin Teske
2042ab2043b8SDevin Teske	while [ $# -gt 0 ]; do
2043ab2043b8SDevin Teske		tagn="$1"
2044ab2043b8SDevin Teske		item="$2"
2045ab2043b8SDevin Teske		shift 3 # tagn/item/help
2046ab2043b8SDevin Teske
2047ab2043b8SDevin Teske		if [ "$tag" = "$tagn" ]; then
2048ab2043b8SDevin Teske			echo "$item"
2049ab2043b8SDevin Teske			return $SUCCESS
2050ab2043b8SDevin Teske		fi
2051ab2043b8SDevin Teske	done
2052ab2043b8SDevin Teske	return $FAILURE
2053ab2043b8SDevin Teske}
2054ab2043b8SDevin Teske
2055ab2043b8SDevin Teske# f_dialog_menutag2index $tag_chosen $tag1 $item1 $tag2 $item2 ...
2056ab2043b8SDevin Teske#
2057ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) you must pass an ordered list of
2058ab2043b8SDevin Teske# tag/item pairs on the command-line. When the user selects a menu option the
2059ab2043b8SDevin Teske# tag for that item is printed to stderr.
2060ab2043b8SDevin Teske#
2061ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into
2062ab2043b8SDevin Teske# the index associated with said tag. The index is the one-based tag/item pair
2063ab2043b8SDevin Teske# array position within the ordered list of tag/item pairs passed to dialog(1).
2064ab2043b8SDevin Teske#
2065ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the
2066ab2043b8SDevin Teske# ordered list of tag/item pairs (HINT: use the same tag/item list as was
2067ab2043b8SDevin Teske# passed to dialog(1) for consistency).
2068ab2043b8SDevin Teske#
2069ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned.
2070ab2043b8SDevin Teske#
2071ab2043b8SDevin Teskef_dialog_menutag2index()
2072ab2043b8SDevin Teske{
2073ab2043b8SDevin Teske	local tag="$1" tagn n=1
2074ab2043b8SDevin Teske	shift 1 # tag
2075ab2043b8SDevin Teske
2076ab2043b8SDevin Teske	while [ $# -gt 0 ]; do
2077ab2043b8SDevin Teske		tagn="$1"
2078ab2043b8SDevin Teske		shift 2 # tagn/item
2079ab2043b8SDevin Teske
2080ab2043b8SDevin Teske		if [ "$tag" = "$tagn" ]; then
2081ab2043b8SDevin Teske			echo $n
2082ab2043b8SDevin Teske			return $SUCCESS
2083ab2043b8SDevin Teske		fi
2084ab2043b8SDevin Teske		n=$(( $n + 1 ))
2085ab2043b8SDevin Teske	done
2086ab2043b8SDevin Teske	return $FAILURE
2087ab2043b8SDevin Teske}
2088ab2043b8SDevin Teske
2089ab2043b8SDevin Teske# f_dialog_menutag2index_with_help $tag_chosen $tag1 $item1 $help1 \
2090ab2043b8SDevin Teske#                                              $tag2 $item2 $help2 ...
2091ab2043b8SDevin Teske#
2092ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) with the `--item-help' option, you
2093ab2043b8SDevin Teske# must pass an ordered list of tag/item/help triplets on the command-line. When
2094ab2043b8SDevin Teske# the user selects a menu option the tag for that item is printed to stderr.
2095ab2043b8SDevin Teske#
2096ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into
2097ab2043b8SDevin Teske# the index associated with said tag. The index is the one-based tag/item/help
2098ab2043b8SDevin Teske# triplet array position within the ordered list of tag/item/help triplets
2099ab2043b8SDevin Teske# passed to dialog(1).
2100ab2043b8SDevin Teske#
2101ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the
2102ab2043b8SDevin Teske# ordered list of tag/item/help triplets (HINT: use the same tag/item/help list
2103ab2043b8SDevin Teske# as was passed to dialog(1) for consistency).
2104ab2043b8SDevin Teske#
2105ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned.
2106ab2043b8SDevin Teske#
2107ab2043b8SDevin Teskef_dialog_menutag2index_with_help()
2108ab2043b8SDevin Teske{
2109ab2043b8SDevin Teske	local tag="$1" tagn n=1
2110ab2043b8SDevin Teske	shift 1 # tag
2111ab2043b8SDevin Teske
2112ab2043b8SDevin Teske	while [ $# -gt 0 ]; do
2113ab2043b8SDevin Teske		tagn="$1"
2114ab2043b8SDevin Teske		shift 3 # tagn/item/help
2115ab2043b8SDevin Teske
2116ab2043b8SDevin Teske		if [ "$tag" = "$tagn" ]; then
2117ab2043b8SDevin Teske			echo $n
2118ab2043b8SDevin Teske			return $SUCCESS
2119ab2043b8SDevin Teske		fi
2120ab2043b8SDevin Teske		n=$(( $n + 1 ))
2121ab2043b8SDevin Teske	done
2122ab2043b8SDevin Teske	return $FAILURE
2123ab2043b8SDevin Teske}
2124ab2043b8SDevin Teske
2125b39eff33SDevin Teske# f_dialog_menutag2help $tag_chosen $tag1 $item1 $help1 $tag2 $item2 $help2 ...
2126b39eff33SDevin Teske#
2127b39eff33SDevin Teske# To use the `--menu' option of dialog(1) with the `--item-help' option, you
2128b39eff33SDevin Teske# must pass an ordered list of tag/item/help triplets on the command-line. When
2129b39eff33SDevin Teske# the user selects a menu option the tag for that item is printed to stderr.
2130b39eff33SDevin Teske#
2131b39eff33SDevin Teske# This function allows you to dereference the tag chosen by the user back into
2132b39eff33SDevin Teske# the help associated with said tag (item is discarded/ignored).
2133b39eff33SDevin Teske#
2134b39eff33SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the
2135b39eff33SDevin Teske# ordered list of tag/item/help triplets (HINT: use the same tag/item/help list
2136b39eff33SDevin Teske# as was passed to dialog(1) for consistency).
2137b39eff33SDevin Teske#
2138b39eff33SDevin Teske# If the tag cannot be found, NULL is returned.
2139b39eff33SDevin Teske#
2140b39eff33SDevin Teskef_dialog_menutag2help()
2141b39eff33SDevin Teske{
2142b39eff33SDevin Teske	local tag="$1" tagn help
2143b39eff33SDevin Teske	shift 1 # tag
2144b39eff33SDevin Teske
2145b39eff33SDevin Teske	while [ $# -gt 0 ]; do
2146b39eff33SDevin Teske		tagn="$1"
2147b39eff33SDevin Teske		help="$3"
2148b39eff33SDevin Teske		shift 3 # tagn/item/help
2149b39eff33SDevin Teske
2150b39eff33SDevin Teske		if [ "$tag" = "$tagn" ]; then
2151b39eff33SDevin Teske			echo "$help"
2152b39eff33SDevin Teske			return $SUCCESS
2153b39eff33SDevin Teske		fi
2154b39eff33SDevin Teske	done
2155b39eff33SDevin Teske	return $FAILURE
2156b39eff33SDevin Teske}
2157b39eff33SDevin Teske
2158ab2043b8SDevin Teske############################################################ INIT FUNCTIONS
2159ab2043b8SDevin Teske
2160ab2043b8SDevin Teske# f_dialog_init
2161ab2043b8SDevin Teske#
2162ab2043b8SDevin Teske# Initialize (or re-initialize) the dialog module after setting/changing any
2163ab2043b8SDevin Teske# of the following environment variables:
2164ab2043b8SDevin Teske#
2165ab2043b8SDevin Teske# 	USE_XDIALOG   Either NULL or Non-NULL. If given a value will indicate
2166ab2043b8SDevin Teske# 	              that Xdialog(1) should be used instead of dialog(1).
2167ab2043b8SDevin Teske#
2168ab2043b8SDevin Teske# 	SECURE        Either NULL or Non-NULL. If given a value will indicate
2169ab2043b8SDevin Teske# 	              that (while running as root) sudo(8) authentication is
2170ab2043b8SDevin Teske# 	              required to proceed.
2171ab2043b8SDevin Teske#
21727d3a5a3bSDevin Teske# Also reads ~/.dialogrc for the following information:
21737d3a5a3bSDevin Teske#
21747d3a5a3bSDevin Teske# 	NO_SHADOW     Either NULL or Non-NULL. If use_shadow is OFF (case-
21757d3a5a3bSDevin Teske# 	              insensitive) in ~/.dialogrc this is set to "1" (otherwise
21767d3a5a3bSDevin Teske# 	              unset).
21777d3a5a3bSDevin Teske#
2178ab2043b8SDevin Teskef_dialog_init()
2179ab2043b8SDevin Teske{
2180d4ae33f0SDevin Teske	local funcname=f_dialog_init
2181d4ae33f0SDevin Teske
2182ab2043b8SDevin Teske	DIALOG_SELF_INITIALIZE=
21835bd5e0c3SDevin Teske	USE_DIALOG=1
2184ab2043b8SDevin Teske
2185ab2043b8SDevin Teske	#
218689498fdfSDevin Teske	# Clone terminal stdout so we can redirect to it from within sub-shells
218789498fdfSDevin Teske	#
218889498fdfSDevin Teske	eval exec $DIALOG_TERMINAL_PASSTHRU_FD\>\&1
218989498fdfSDevin Teske
219089498fdfSDevin Teske	#
2191c3755aa3SDevin Teske	# Add `-S' and `-X' to the list of standard arguments supported by all
2192c3755aa3SDevin Teske	#
2193c3755aa3SDevin Teske	case "$GETOPTS_STDARGS" in
2194c3755aa3SDevin Teske	*SX*) : good ;; # already present
2195c3755aa3SDevin Teske	   *) GETOPTS_STDARGS="${GETOPTS_STDARGS}SX"
2196c3755aa3SDevin Teske	esac
2197c3755aa3SDevin Teske
2198c3755aa3SDevin Teske	#
2199ab2043b8SDevin Teske	# Process stored command-line arguments
2200ab2043b8SDevin Teske	#
220113243ba9SDevin Teske	# NB: Using backticks instead of $(...) for portability since Linux
220213243ba9SDevin Teske	#     bash(1) balks at the right parentheses encountered in the case-
220313243ba9SDevin Teske	#     statement (incorrectly interpreting it as the close of $(...)).
220413243ba9SDevin Teske	#
2205c3755aa3SDevin Teske	f_dprintf "f_dialog_init: ARGV=[%s] GETOPTS_STDARGS=[%s]" \
2206c3755aa3SDevin Teske	          "$ARGV" "$GETOPTS_STDARGS"
220713243ba9SDevin Teske	SECURE=`set -- $ARGV
2208b74ae45bSDevin Teske		OPTIND=1
22095b4765c3SDevin Teske		while getopts \
22105b4765c3SDevin Teske			"$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" \
22115b4765c3SDevin Teske		flag > /dev/null; do
2212ab2043b8SDevin Teske			case "$flag" in
2213ab2043b8SDevin Teske			S) echo 1 ;;
2214ab2043b8SDevin Teske			esac
2215ab2043b8SDevin Teske		done
221613243ba9SDevin Teske	` # END-BACKTICK
221713243ba9SDevin Teske	USE_XDIALOG=`set -- $ARGV
2218b74ae45bSDevin Teske		OPTIND=1
22195b4765c3SDevin Teske		while getopts \
22205b4765c3SDevin Teske			"$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" \
22215b4765c3SDevin Teske		flag > /dev/null; do
2222ab2043b8SDevin Teske			case "$flag" in
2223ab2043b8SDevin Teske			S|X) echo 1 ;;
2224ab2043b8SDevin Teske			esac
2225ab2043b8SDevin Teske		done
222613243ba9SDevin Teske	` # END-BACKTICK
2227c3755aa3SDevin Teske	f_dprintf "f_dialog_init: SECURE=[%s] USE_XDIALOG=[%s]" \
2228c3755aa3SDevin Teske	          "$SECURE" "$USE_XDIALOG"
2229ab2043b8SDevin Teske
2230ab2043b8SDevin Teske	#
2231ab2043b8SDevin Teske	# Process `-X' command-line option
2232ab2043b8SDevin Teske	#
22330f8b2ba6SAlfonso S. Siciliano	[ "$USE_XDIALOG" ] && DIALOG=Xdialog USE_DIALOG=
2234ab2043b8SDevin Teske
2235ab2043b8SDevin Teske	#
2236ab2043b8SDevin Teske	# Sanity check, or die gracefully
2237ab2043b8SDevin Teske	#
2238ab2043b8SDevin Teske	if ! f_have $DIALOG; then
2239ab2043b8SDevin Teske		unset USE_XDIALOG
224042ade6a9SDevin Teske		local failed_dialog="$DIALOG"
2241*c36b3dbcSAlfonso S. Siciliano		DIALOG=bsddialog
2242ab2043b8SDevin Teske		f_die 1 "$msg_no_such_file_or_directory" "$pgm" "$failed_dialog"
2243ab2043b8SDevin Teske	fi
2244ab2043b8SDevin Teske
2245ab2043b8SDevin Teske	#
22467d3a5a3bSDevin Teske	# Read ~/.dialogrc (unless using Xdialog(1)) for properties
22477d3a5a3bSDevin Teske	#
22484f8a4fa5SDevin Teske	if [ -f ~/.dialogrc -a ! "$USE_XDIALOG" ]; then
22497d3a5a3bSDevin Teske		eval "$(
22507d3a5a3bSDevin Teske			awk -v param=use_shadow -v expect=OFF \
22517d3a5a3bSDevin Teske			    -v set="NO_SHADOW=1" '
22527d3a5a3bSDevin Teske			!/^[[:space:]]*(#|$)/ && \
22537d3a5a3bSDevin Teske			tolower($1) ~ "^"param"(=|$)" && \
22547d3a5a3bSDevin Teske			/[^#]*=/ {
22557d3a5a3bSDevin Teske				sub(/^[^=]*=[[:space:]]*/, "")
22567d3a5a3bSDevin Teske				if ( toupper($1) == expect ) print set";"
22577d3a5a3bSDevin Teske			}' ~/.dialogrc
22587d3a5a3bSDevin Teske		)"
22597d3a5a3bSDevin Teske	fi
22607d3a5a3bSDevin Teske
22617d3a5a3bSDevin Teske	#
2262ab2043b8SDevin Teske	# If we're already running as root but we got there by way of sudo(8)
2263ab2043b8SDevin Teske	# and we have X11, we should merge the xauth(1) credentials from our
2264ab2043b8SDevin Teske	# original user.
2265ab2043b8SDevin Teske	#
2266ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ] &&
2267ab2043b8SDevin Teske	   [ "$( id -u )" = "0" ] &&
2268ab2043b8SDevin Teske	   [ "$SUDO_USER" -a "$DISPLAY" ]
2269ab2043b8SDevin Teske	then
2270ab2043b8SDevin Teske		if ! f_have xauth; then
2271ab2043b8SDevin Teske			# Die gracefully, as we [likely] can't use Xdialog(1)
2272ab2043b8SDevin Teske			unset USE_XDIALOG
2273*c36b3dbcSAlfonso S. Siciliano			DIALOG=bsddialog
2274ab2043b8SDevin Teske			f_die 1 "$msg_no_such_file_or_directory" "$pgm" "xauth"
2275ab2043b8SDevin Teske		fi
2276ab2043b8SDevin Teske		HOSTNAME=$( hostname )
227742ade6a9SDevin Teske		local displaynum="${DISPLAY#*:}"
2278ab2043b8SDevin Teske		eval xauth -if \~$SUDO_USER/.Xauthority extract - \
2279ab2043b8SDevin Teske			\"\$HOSTNAME/unix:\$displaynum\" \
2280ab2043b8SDevin Teske			\"\$HOSTNAME:\$displaynum\" | sudo sh -c 'xauth -ivf \
2281fb7d723eSDevin Teske			~root/.Xauthority merge - > /dev/null 2>&1'
2282ab2043b8SDevin Teske	fi
2283ab2043b8SDevin Teske
2284ab2043b8SDevin Teske	#
2285ab2043b8SDevin Teske	# Probe Xdialog(1) for maximum height/width constraints, or die
2286ab2043b8SDevin Teske	# gracefully
2287ab2043b8SDevin Teske	#
2288ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
228942ade6a9SDevin Teske		local maxsize
2290d4ae33f0SDevin Teske		if ! f_eval_catch -dk maxsize $funcname "$DIALOG" \
2291d4ae33f0SDevin Teske			'LANG= LC_ALL= %s --print-maxsize' "$DIALOG"
2292ab2043b8SDevin Teske		then
2293ab2043b8SDevin Teske			# Xdialog(1) failed, fall back to dialog(1)
2294ab2043b8SDevin Teske			unset USE_XDIALOG
229574036c4dSDevin Teske
229674036c4dSDevin Teske			# Display the error message produced by Xdialog(1)
229774036c4dSDevin Teske			local height width
229874036c4dSDevin Teske			f_dialog_buttonbox_size height width \
229974036c4dSDevin Teske				"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$maxsize"
2300*c36b3dbcSAlfonso S. Siciliano			bsddialog \
230174036c4dSDevin Teske				--title "$DIALOG_TITLE"         \
230274036c4dSDevin Teske				--backtitle "$DIALOG_BACKTITLE" \
230374036c4dSDevin Teske				--ok-label "$msg_ok"            \
230474036c4dSDevin Teske				--msgbox "$maxsize" $height $width
2305ab2043b8SDevin Teske			exit $FAILURE
2306ab2043b8SDevin Teske		fi
2307ab2043b8SDevin Teske
2308ab2043b8SDevin Teske		XDIALOG_MAXSIZE=$(
2309ab2043b8SDevin Teske			set -- ${maxsize##*:}
2310ab2043b8SDevin Teske
2311ab2043b8SDevin Teske			height=${1%,}
2312ab2043b8SDevin Teske			width=$2
2313ab2043b8SDevin Teske
2314ab2043b8SDevin Teske			echo $height $width
2315ab2043b8SDevin Teske		)
2316ab2043b8SDevin Teske	fi
2317ab2043b8SDevin Teske
2318ab2043b8SDevin Teske	#
2319ab2043b8SDevin Teske	# If using Xdialog(1), swap DIALOG_TITLE with DIALOG_BACKTITLE.
2320ab2043b8SDevin Teske	# The reason for this is because many dialog(1) applications use
2321ab2043b8SDevin Teske	# --backtitle for the program name (which is better suited as
2322ab2043b8SDevin Teske	# --title with Xdialog(1)).
2323ab2043b8SDevin Teske	#
2324ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
232542ade6a9SDevin Teske		local _DIALOG_TITLE="$DIALOG_TITLE"
2326ab2043b8SDevin Teske		DIALOG_TITLE="$DIALOG_BACKTITLE"
2327ab2043b8SDevin Teske		DIALOG_BACKTITLE="$_DIALOG_TITLE"
2328ab2043b8SDevin Teske	fi
232956961fd7SDevin Teske
233056961fd7SDevin Teske	f_dprintf "f_dialog_init: dialog(1) API initialized."
2331ab2043b8SDevin Teske}
2332ab2043b8SDevin Teske
233379ec1cd5SDevin Teske############################################################ MAIN
233479ec1cd5SDevin Teske
23355ffea47aSDevin Teske#
23365ffea47aSDevin Teske# Self-initialize unless requested otherwise
23375ffea47aSDevin Teske#
233856961fd7SDevin Teskef_dprintf "%s: DIALOG_SELF_INITIALIZE=[%s]" \
233956961fd7SDevin Teske          dialog.subr "$DIALOG_SELF_INITIALIZE"
2340d3c2b8b6SDevin Teskecase "$DIALOG_SELF_INITIALIZE" in
2341d3c2b8b6SDevin Teske""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
2342d3c2b8b6SDevin Teske*) f_dialog_init
2343d3c2b8b6SDevin Teskeesac
234479ec1cd5SDevin Teske
234556961fd7SDevin Teskef_dprintf "%s: Successfully loaded." dialog.subr
234656961fd7SDevin Teske
2347ab2043b8SDevin Teskefi # ! $_DIALOG_SUBR
2348