xref: /freebsd/usr.sbin/bsdconfig/share/dialog.subr (revision ec7120b5b2d110c16e43de0e66e7f971efd52717)
1ab2043b8SDevin Teskeif [ ! "$_DIALOG_SUBR" ]; then _DIALOG_SUBR=1
2ab2043b8SDevin Teske#
31d70f5f4SDevin Teske# Copyright (c) 2006-2013 Devin Teske
4ab2043b8SDevin 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
16ab2043b8SDevin 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
20ab2043b8SDevin Teske# DAMAGES (INLUDING, 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# $FreeBSD$
28ab2043b8SDevin Teske#
29ab2043b8SDevin Teske############################################################ INCLUDES
30ab2043b8SDevin Teske
31ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
32ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3356961fd7SDevin Teskef_dprintf "%s: loading includes..." dialog.subr
34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/strings.subr
353da75d2eSDevin Teskef_include $BSDCFG_SHARE/variable.subr
36ab2043b8SDevin Teske
37ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig"
38ab2043b8SDevin Teskef_include_lang $BSDCFG_LIBE/include/messages.subr
39ab2043b8SDevin Teske
40ab2043b8SDevin Teske############################################################ CONFIGURATION
41ab2043b8SDevin Teske
42ab2043b8SDevin Teske#
4389498fdfSDevin Teske# Default file descriptor to link to stdout for dialog(1) passthru allowing
4489498fdfSDevin Teske# execution of dialog from within a sub-shell (so-long as its standard output
4589498fdfSDevin Teske# is explicitly redirected to this file descriptor).
46ab2043b8SDevin Teske#
47526e1dc1SDevin Teske: ${DIALOG_TERMINAL_PASSTHRU_FD:=${TERMINAL_STDOUT_PASSTHRU:-3}}
48ab2043b8SDevin Teske
49ab2043b8SDevin Teske############################################################ GLOBALS
50ab2043b8SDevin Teske
51ab2043b8SDevin Teske#
52ab2043b8SDevin Teske# Default name of dialog(1) utility
53ab2043b8SDevin Teske# NOTE: This is changed to "Xdialog" by the optional `-X' argument
54ab2043b8SDevin Teske#
55ab2043b8SDevin TeskeDIALOG="dialog"
56ab2043b8SDevin Teske
57ab2043b8SDevin Teske#
58ab2043b8SDevin Teske# Default dialog(1) title and backtitle text
59ab2043b8SDevin Teske#
60ab2043b8SDevin TeskeDIALOG_TITLE="$pgm"
61ab2043b8SDevin TeskeDIALOG_BACKTITLE="bsdconfig"
62ab2043b8SDevin Teske
63ab2043b8SDevin Teske#
64ab2043b8SDevin Teske# Settings used while interacting with dialog(1)
65ab2043b8SDevin Teske#
66ab2043b8SDevin TeskeDIALOG_MENU_TAGS="123456789ABCDEFGHIJKLMNOPQRSTUVWYZabcdefghijklmnopqrstuvwxyz"
67ab2043b8SDevin Teske
68ab2043b8SDevin Teske#
69ab2043b8SDevin Teske# Declare that we are fully-compliant with Xdialog(1) by unset'ing all
70ab2043b8SDevin Teske# compatibility settings.
71ab2043b8SDevin Teske#
72ab2043b8SDevin Teskeunset XDIALOG_HIGH_DIALOG_COMPAT
73ab2043b8SDevin Teskeunset XDIALOG_FORCE_AUTOSIZE
74ab2043b8SDevin Teskeunset XDIALOG_INFOBOX_TIMEOUT
75ab2043b8SDevin Teske
76ab2043b8SDevin Teske#
7779ec1cd5SDevin Teske# Default behavior is to call f_dialog_init() automatically when loaded.
78ab2043b8SDevin Teske#
79ab2043b8SDevin Teske: ${DIALOG_SELF_INITIALIZE=1}
80ab2043b8SDevin Teske
8130a8a39eSDevin Teske#
8230a8a39eSDevin Teske# Default terminal size (used if/when running without a controlling terminal)
8330a8a39eSDevin Teske#
8430a8a39eSDevin Teske: ${DEFAULT_TERMINAL_SIZE:=24 80}
8530a8a39eSDevin Teske
8674036c4dSDevin Teske#
8774036c4dSDevin Teske# Minimum width(s) for various dialog(1) implementations (sensible global
8874036c4dSDevin Teske# default(s) for all widgets of a given variant)
8974036c4dSDevin Teske#
9074036c4dSDevin Teske: ${DIALOG_MIN_WIDTH:=24}
9174036c4dSDevin Teske: ${XDIALOG_MIN_WIDTH:=35}
9274036c4dSDevin Teske
9374036c4dSDevin Teske#
9474036c4dSDevin Teske# When manually sizing Xdialog(1) widgets such as calendar and timebox, you'll
9574036c4dSDevin Teske# need to know the size of the embedded GUI objects because the height passed
9674036c4dSDevin Teske# to Xdialog(1) for these widgets has to be tall enough to accomodate them.
9774036c4dSDevin Teske#
9874036c4dSDevin Teske# These values are helpful when manually sizing with dialog(1) too, but in a
9974036c4dSDevin Teske# different way. dialog(1) does not make you accomodate the custom items in the
10074036c4dSDevin Teske# height (but does for width) -- a height of 3 will display three lines and a
10174036c4dSDevin Teske# full calendar, for example (whereas Xdialog will truncate the calendar if
10274036c4dSDevin Teske# given a height of 3). For dialog(1), use these values for making sure that
10374036c4dSDevin Teske# the height does not exceed max_height (obtained by f_dialog_max_size()).
10474036c4dSDevin Teske#
10574036c4dSDevin TeskeDIALOG_CALENDAR_HEIGHT=15
10674036c4dSDevin TeskeDIALOG_TIMEBOX_HEIGHT=6
10774036c4dSDevin Teske
108ab2043b8SDevin Teske############################################################ GENERIC FUNCTIONS
109ab2043b8SDevin Teske
110fd962ac6SDevin Teske# f_dialog_data_sanitize $var_to_edit ...
111fd962ac6SDevin Teske#
112fd962ac6SDevin Teske# When using dialog(1) or Xdialog(1) sometimes unintended warnings or errors
113fd962ac6SDevin Teske# are generated from underlying libraries. For example, if $LANG is set to an
114fd962ac6SDevin Teske# invalid or unknown locale, the warnings from the Xdialog(1) libraries will
115fd962ac6SDevin Teske# clutter the output. This function helps by providing a centralied function
116fd962ac6SDevin Teske# that removes spurious warnings from the dialog(1) (or Xdialog(1)) response.
117fd962ac6SDevin Teske#
118fd962ac6SDevin Teske# Simply pass the name of one or more variables that need to be sanitized.
119fd962ac6SDevin Teske# After execution, the variables will hold their newly-sanitized data.
120fd962ac6SDevin Teske#
121fd962ac6SDevin Teskef_dialog_data_sanitize()
122fd962ac6SDevin Teske{
123fd962ac6SDevin Teske	if [ "$#" -eq 0 ]; then
124fd962ac6SDevin Teske		f_dprintf "%s: called with zero arguments" \
125fd962ac6SDevin Teske		          f_dialog_response_sanitize
126fd962ac6SDevin Teske		return $FAILURE
127fd962ac6SDevin Teske	fi
128fd962ac6SDevin Teske
129fd962ac6SDevin Teske	local __var_to_edit
130fd962ac6SDevin Teske	for __var_to_edit in $*; do
131fd962ac6SDevin Teske		# Skip warnings and trim leading/trailing whitespace
132fd962ac6SDevin Teske		setvar $__var_to_edit "$( f_getvar $__var_to_edit | awk '
133fd962ac6SDevin Teske			BEGIN { data = 0 }
134fd962ac6SDevin Teske			{
135fd962ac6SDevin Teske				if ( ! data )
136fd962ac6SDevin Teske				{
137fd962ac6SDevin Teske					if ( $0 ~ /^$/ ) next
138fd962ac6SDevin Teske					if ( $0 ~ /^Gdk-WARNING \*\*:/ ) next
139fd962ac6SDevin Teske					data = 1
140fd962ac6SDevin Teske				}
141fd962ac6SDevin Teske				print
142fd962ac6SDevin Teske			}
143fd962ac6SDevin Teske		' )"
144fd962ac6SDevin Teske	done
145fd962ac6SDevin Teske}
146fd962ac6SDevin Teske
147*ec7120b5SDevin Teske# f_dialog_line_sanitize $var_to_edit ...
148*ec7120b5SDevin Teske#
149*ec7120b5SDevin Teske# When using dialog(1) or Xdialog(1) sometimes unintended warnings or errors
150*ec7120b5SDevin Teske# are generated from underlying libraries. For example, if $LANG is set to an
151*ec7120b5SDevin Teske# invalid or unknown locale, the warnings from the Xdialog(1) libraries will
152*ec7120b5SDevin Teske# clutter the output. This function helps by providing a centralied function
153*ec7120b5SDevin Teske# that removes spurious warnings from the dialog(1) (or Xdialog(1)) response.
154*ec7120b5SDevin Teske#
155*ec7120b5SDevin Teske# Simply pass the name of one or more variables that need to be sanitized.
156*ec7120b5SDevin Teske# After execution, the variables will hold their newly-sanitized data.
157*ec7120b5SDevin Teske#
158*ec7120b5SDevin Teske# This function, unlike f_dialog_data_sanitize(), also removes leading/trailing
159*ec7120b5SDevin Teske# whitespace from each line.
160*ec7120b5SDevin Teske#
161*ec7120b5SDevin Teskef_dialog_line_sanitize()
162*ec7120b5SDevin Teske{
163*ec7120b5SDevin Teske	if [ "$#" -eq 0 ]; then
164*ec7120b5SDevin Teske		f_dprintf "%s: called with zero arguments" \
165*ec7120b5SDevin Teske		          f_dialog_response_sanitize
166*ec7120b5SDevin Teske		return $FAILURE
167*ec7120b5SDevin Teske	fi
168*ec7120b5SDevin Teske
169*ec7120b5SDevin Teske	local __var_to_edit
170*ec7120b5SDevin Teske	for __var_to_edit in $*; do
171*ec7120b5SDevin Teske		# Skip warnings and trim leading/trailing whitespace
172*ec7120b5SDevin Teske		setvar $__var_to_edit "$( f_getvar $__var_to_edit | awk '
173*ec7120b5SDevin Teske			BEGIN { data = 0 }
174*ec7120b5SDevin Teske			{
175*ec7120b5SDevin Teske				if ( ! data )
176*ec7120b5SDevin Teske				{
177*ec7120b5SDevin Teske					if ( $0 ~ /^$/ ) next
178*ec7120b5SDevin Teske					if ( $0 ~ /^Gdk-WARNING \*\*:/ ) next
179*ec7120b5SDevin Teske					data = 1
180*ec7120b5SDevin Teske				}
181*ec7120b5SDevin Teske				sub(/^[[:space:]]*/, "")
182*ec7120b5SDevin Teske				sub(/[[:space:]]*$/, "")
183*ec7120b5SDevin Teske				print
184*ec7120b5SDevin Teske			}
185*ec7120b5SDevin Teske		' )"
186*ec7120b5SDevin Teske	done
187*ec7120b5SDevin Teske}
188*ec7120b5SDevin Teske
189fd962ac6SDevin Teske############################################################ TITLE FUNCTIONS
190fd962ac6SDevin Teske
191ab2043b8SDevin Teske# f_dialog_title [$new_title]
192ab2043b8SDevin Teske#
193ab2043b8SDevin Teske# Set the title of future dialog(1) ($DIALOG_TITLE) or backtitle of Xdialog(1)
194ab2043b8SDevin Teske# ($DIALOG_BACKTITLE) invocations. If no arguments are given or the first
195ab2043b8SDevin Teske# argument is NULL, the current title is returned.
196ab2043b8SDevin Teske#
197ab2043b8SDevin Teske# Each time this function is called, a backup of the current values is made
198ab2043b8SDevin Teske# allowing a one-time (single-level) restoration of the previous title using the
199ab2043b8SDevin Teske# f_dialog_title_restore() function (below).
200ab2043b8SDevin Teske#
201ab2043b8SDevin Teskef_dialog_title()
202ab2043b8SDevin Teske{
203ab2043b8SDevin Teske	local new_title="$1"
204ab2043b8SDevin Teske
205540bebd7SDevin Teske	if [ "${1+set}" ]; then
206ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
207ab2043b8SDevin Teske			_DIALOG_BACKTITLE="$DIALOG_BACKTITLE"
208ab2043b8SDevin Teske			DIALOG_BACKTITLE="$new_title"
209ab2043b8SDevin Teske		else
210ab2043b8SDevin Teske			_DIALOG_TITLE="$DIALOG_TITLE"
211ab2043b8SDevin Teske			DIALOG_TITLE="$new_title"
212ab2043b8SDevin Teske		fi
213ab2043b8SDevin Teske	else
214ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
215ab2043b8SDevin Teske			echo "$DIALOG_BACKTITLE"
216ab2043b8SDevin Teske		else
217ab2043b8SDevin Teske			echo "$DIALOG_TITLE"
218ab2043b8SDevin Teske		fi
219ab2043b8SDevin Teske	fi
220ab2043b8SDevin Teske}
221ab2043b8SDevin Teske
222ab2043b8SDevin Teske# f_dialog_title_restore
223ab2043b8SDevin Teske#
224ab2043b8SDevin Teske# Restore the previous title set by the last call to f_dialog_title().
225ab2043b8SDevin Teske# Restoration is non-recursive and only works to restore the most-recent title.
226ab2043b8SDevin Teske#
227ab2043b8SDevin Teskef_dialog_title_restore()
228ab2043b8SDevin Teske{
229ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
230ab2043b8SDevin Teske		DIALOG_BACKTITLE="$_DIALOG_BACKTITLE"
231ab2043b8SDevin Teske	else
232ab2043b8SDevin Teske		DIALOG_TITLE="$_DIALOG_TITLE"
233ab2043b8SDevin Teske	fi
234ab2043b8SDevin Teske}
235ab2043b8SDevin Teske
236ab2043b8SDevin Teske# f_dialog_backtitle [$new_backtitle]
237ab2043b8SDevin Teske#
238ab2043b8SDevin Teske# Set the backtitle of future dialog(1) ($DIALOG_BACKTITLE) or title of
239ab2043b8SDevin Teske# Xdialog(1) ($DIALOG_TITLE) invocations. If no arguments are given or the
240ab2043b8SDevin Teske# first argument is NULL, the current backtitle is returned.
241ab2043b8SDevin Teske#
242ab2043b8SDevin Teskef_dialog_backtitle()
243ab2043b8SDevin Teske{
244ab2043b8SDevin Teske	local new_backtitle="$1"
245ab2043b8SDevin Teske
246540bebd7SDevin Teske	if [ "${1+set}" ]; then
247ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
248ab2043b8SDevin Teske			_DIALOG_TITLE="$DIALOG_TITLE"
249ab2043b8SDevin Teske			DIALOG_TITLE="$new_backtitle"
250ab2043b8SDevin Teske		else
251ab2043b8SDevin Teske			_DIALOG_BACKTITLE="$DIALOG_BACKTITLE"
252ab2043b8SDevin Teske			DIALOG_BACKTITLE="$new_backtitle"
253ab2043b8SDevin Teske		fi
254ab2043b8SDevin Teske	else
255ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
256ab2043b8SDevin Teske			echo "$DIALOG_TITLE"
257ab2043b8SDevin Teske		else
258ab2043b8SDevin Teske			echo "$DIALOG_BACKTITLE"
259ab2043b8SDevin Teske		fi
260ab2043b8SDevin Teske	fi
261ab2043b8SDevin Teske}
262ab2043b8SDevin Teske
263ab2043b8SDevin Teske# f_dialog_backtitle_restore
264ab2043b8SDevin Teske#
265ab2043b8SDevin Teske# Restore the previous backtitle set by the last call to f_dialog_backtitle().
266ab2043b8SDevin Teske# Restoration is non-recursive and only works to restore the most-recent
267ab2043b8SDevin Teske# backtitle.
268ab2043b8SDevin Teske#
269ab2043b8SDevin Teskef_dialog_backtitle_restore()
270ab2043b8SDevin Teske{
271ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
272ab2043b8SDevin Teske		DIALOG_TITLE="$_DIALOG_TITLE"
273ab2043b8SDevin Teske	else
274ab2043b8SDevin Teske		DIALOG_BACKTITLE="$_DIALOG_BACKTITLE"
275ab2043b8SDevin Teske	fi
276ab2043b8SDevin Teske}
277ab2043b8SDevin Teske
278ab2043b8SDevin Teske############################################################ SIZE FUNCTIONS
279ab2043b8SDevin Teske
28074036c4dSDevin Teske# f_dialog_max_size $var_height $var_width
28174036c4dSDevin Teske#
28274036c4dSDevin Teske# Get the maximum height and width for a dialog widget and store the values in
28374036c4dSDevin Teske# $var_height and $var_width (respectively).
28474036c4dSDevin Teske#
28574036c4dSDevin Teskef_dialog_max_size()
28674036c4dSDevin Teske{
28774036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __max_size
28874036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
28974036c4dSDevin Teske	if [ "$USE_XDIALOG" ]; then
29074036c4dSDevin Teske		__max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION
29174036c4dSDevin Teske	else
29274036c4dSDevin Teske		__max_size=$( stty size 2> /dev/null ) # usually "24 80"
29374036c4dSDevin Teske		: ${__max_size:=$DEFAULT_TERMINAL_SIZE}
29474036c4dSDevin Teske	fi
29574036c4dSDevin Teske	[ "$__var_height" ] && setvar "$__var_height" "${__max_size%%[$IFS]*}"
29674036c4dSDevin Teske	[ "$__var_width" ] && setvar "$__var_width" "${__max_size##*[$IFS]}"
29774036c4dSDevin Teske}
29874036c4dSDevin Teske
29974036c4dSDevin Teske# f_dialog_size_constrain $var_height $var_width [$min_height [$min_width]]
30074036c4dSDevin Teske#
30174036c4dSDevin Teske# Modify $var_height to be no-less-than $min_height (if given; zero otherwise)
30274036c4dSDevin Teske# and no-greater-than terminal height (or screen height if $USE_XDIALOG is
30374036c4dSDevin Teske# set).
30474036c4dSDevin Teske#
30574036c4dSDevin Teske# Also modify $var_width to be no-less-than $XDIALOG_MIN_WIDTH (or
30674036c4dSDevin Teske# $XDIALOG_MIN_WIDTH if $_USE_XDIALOG is set) and no-greater-than terminal
30774036c4dSDevin Teske# or screen width. The use of $[X]DIALOG_MIN_WIDTH can be overridden by
30874036c4dSDevin Teske# passing $min_width.
30974036c4dSDevin Teske#
31074036c4dSDevin Teske# Return status is success unless one of the passed arguments is invalid
31174036c4dSDevin Teske# or all of the $var_* arguments are either NULL or missing.
31274036c4dSDevin Teske#
31374036c4dSDevin Teskef_dialog_size_constrain()
31474036c4dSDevin Teske{
31574036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
31674036c4dSDevin Teske	local __min_height="$3" __min_width="$4"
31774036c4dSDevin Teske	local __retval=$SUCCESS
31874036c4dSDevin Teske
31974036c4dSDevin Teske	# Return failure unless at least one var_* argument is passed
32074036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
32174036c4dSDevin Teske
32274036c4dSDevin Teske	#
32374036c4dSDevin Teske	# Print debug warnings if any given (non-NULL) argument are invalid
32474036c4dSDevin Teske	# NOTE: Don't change the name of $__{var,min,}{height,width}
32574036c4dSDevin Teske	#
32674036c4dSDevin Teske	local __height __width
32774036c4dSDevin Teske	local __arg __cp __fname=f_dialog_size_constrain
32874036c4dSDevin Teske	for __arg in height width; do
32974036c4dSDevin Teske		debug= f_getvar __var_$__arg __cp
33074036c4dSDevin Teske		[ "$__cp" ] || continue
33174036c4dSDevin Teske		if ! f_getvar "$__cp" __$__arg; then
33274036c4dSDevin Teske			f_dprintf "%s: var_%s variable \`%s' not set" \
33374036c4dSDevin Teske			          $__fname $__arg "$__cp"
33474036c4dSDevin Teske			__retval=$FAILURE
33574036c4dSDevin Teske		elif ! eval f_isinteger \$__$__arg; then
33674036c4dSDevin Teske			f_dprintf "%s: var_%s variable value not a number" \
33774036c4dSDevin Teske			          $__fname $__arg
33874036c4dSDevin Teske			__retval=$FAILURE
33974036c4dSDevin Teske		fi
34074036c4dSDevin Teske	done
34174036c4dSDevin Teske	for __arg in height width; do
34274036c4dSDevin Teske		debug= f_getvar __min_$__arg __cp
34374036c4dSDevin Teske		[ "$__cp" ] || continue
34474036c4dSDevin Teske		f_isinteger "$__cp" && continue
34574036c4dSDevin Teske		f_dprintf "%s: min_%s value not a number" $__fname $__arg
34674036c4dSDevin Teske		__retval=$FAILURE
34774036c4dSDevin Teske		setvar __min_$__arg ""
34874036c4dSDevin Teske	done
34974036c4dSDevin Teske
35074036c4dSDevin Teske	# Obtain maximum height and width values
35174036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
35274036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
35374036c4dSDevin Teske	local __max_height_size_constain __max_width_size_constrain
35474036c4dSDevin Teske	f_dialog_max_size \
35574036c4dSDevin Teske		__max_height_size_constrain __max_width_size_constrain
35674036c4dSDevin Teske
35774036c4dSDevin Teske	# Adjust height if desired
35874036c4dSDevin Teske	if [ "$__var_height" ]; then
35974036c4dSDevin Teske		if [ $__height -lt ${__min_height:-0} ]; then
36074036c4dSDevin Teske			setvar "$__var_height" $__min_height
36174036c4dSDevin Teske		elif [ $__height -gt $__max_height_size_constrain ]; then
36274036c4dSDevin Teske			setvar "$__var_height" $__max_height_size_constrain
36374036c4dSDevin Teske		fi
36474036c4dSDevin Teske	fi
36574036c4dSDevin Teske
36674036c4dSDevin Teske	# Adjust width if desired
36774036c4dSDevin Teske	if [ "$__var_width" ]; then
36874036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
36974036c4dSDevin Teske			: ${__min_width:=${XDIALOG_MIN_WIDTH:-35}}
37074036c4dSDevin Teske		else
37174036c4dSDevin Teske			: ${__min_width:=${DIALOG_MIN_WIDTH:-24}}
37274036c4dSDevin Teske		fi
37374036c4dSDevin Teske		if [ $__width -lt $__min_width ]; then
37474036c4dSDevin Teske			setvar "$__var_width" $__min_width
37574036c4dSDevin Teske		elif [ $__width -gt $__max_width_size_constrain ]; then
37674036c4dSDevin Teske			setvar "$__var_width" $__max_width_size_constrain
37774036c4dSDevin Teske		fi
37874036c4dSDevin Teske	fi
37974036c4dSDevin Teske
38074036c4dSDevin Teske	return $__retval # success if no debug warnings were printed
38174036c4dSDevin Teske}
38274036c4dSDevin Teske
38374036c4dSDevin Teske# f_dialog_menu_constrain $var_height $var_width $var_rows "$prompt" \
38474036c4dSDevin Teske#                         [$min_height [$min_width [$min_rows]]]
38574036c4dSDevin Teske#
38674036c4dSDevin Teske# Modify $var_height to be no-less-than $min_height (if given; zero otherwise)
38774036c4dSDevin Teske# and no-greater-than terminal height (or screen height if $USE_XDIALOG is
38874036c4dSDevin Teske# set).
38974036c4dSDevin Teske#
39074036c4dSDevin Teske# Also modify $var_width to be no-less-than $XDIALOG_MIN_WIDTH (or
39174036c4dSDevin Teske# $XDIALOG_MIN_WIDTH if $_USE_XDIALOG is set) and no-greater-than terminal
39274036c4dSDevin Teske# or screen width. The use of $[X]DIALOG_MIN_WIDTH can be overridden by
39374036c4dSDevin Teske# passing $min_width.
39474036c4dSDevin Teske#
39574036c4dSDevin Teske# Last, modify $var_rows to be no-less-than $min_rows (if specified; zero
39674036c4dSDevin Teske# otherwise) and no-greater-than (max_height - 8) where max_height is the
39774036c4dSDevin Teske# terminal height (or screen height if $USE_XDIALOG is set). If $prompt is NULL
39874036c4dSDevin Teske# or missing, dialog(1) allows $var_rows to be (max_height - 7), maximizing the
39974036c4dSDevin Teske# number of visible rows.
40074036c4dSDevin Teske#
40174036c4dSDevin Teske# Return status is success unless one of the passed arguments is invalid
40274036c4dSDevin Teske# or all of the $var_* arguments are either NULL or missing.
40374036c4dSDevin Teske#
40474036c4dSDevin Teskef_dialog_menu_constrain()
40574036c4dSDevin Teske{
40674036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __var_rows="$3" __prompt="$4"
40774036c4dSDevin Teske	local __min_height="$5" __min_width="$6" __min_rows="$7"
40874036c4dSDevin Teske
40974036c4dSDevin Teske	# Return failure unless at least one var_* argument is passed
41074036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
41174036c4dSDevin Teske		return $FAILURE
41274036c4dSDevin Teske
41374036c4dSDevin Teske	#
41474036c4dSDevin Teske	# Print debug warnings if any given (non-NULL) argument are invalid
41574036c4dSDevin Teske	# NOTE: Don't change the name of $__{var,min,}{height,width,rows}
41674036c4dSDevin Teske	#
41774036c4dSDevin Teske	local __height __width __rows
41874036c4dSDevin Teske	local __arg __cp __fname=f_dialog_menu_constrain
41974036c4dSDevin Teske	for __arg in height width rows; do
42074036c4dSDevin Teske		debug= f_getvar __var_$__arg __cp
42174036c4dSDevin Teske		[ "$__cp" ] || continue
42274036c4dSDevin Teske		if ! f_getvar "$__cp" __$__arg; then
42374036c4dSDevin Teske			f_dprintf "%s: var_%s variable \`%s' not set" \
42474036c4dSDevin Teske			          $__fname $__arg "$__cp"
42574036c4dSDevin Teske			__retval=$FAILURE
42674036c4dSDevin Teske		elif ! eval f_isinteger \$__$__arg; then
42774036c4dSDevin Teske			f_dprintf "%s: var_%s variable value not a number" \
42874036c4dSDevin Teske			          $__fname $__arg
42974036c4dSDevin Teske			__retval=$FAILURE
43074036c4dSDevin Teske		fi
43174036c4dSDevin Teske	done
43274036c4dSDevin Teske	for __arg in height width rows; do
43374036c4dSDevin Teske		debug= f_getvar __min_$__arg __cp
43474036c4dSDevin Teske		[ "$__cp" ] || continue
43574036c4dSDevin Teske		f_isinteger "$__cp" && continue
43674036c4dSDevin Teske		f_dprintf "%s: min_%s value not a number" $__fname $__arg
43774036c4dSDevin Teske		__retval=$FAILURE
43874036c4dSDevin Teske		setvar __min_$__arg ""
43974036c4dSDevin Teske	done
44074036c4dSDevin Teske
44174036c4dSDevin Teske	# Obtain maximum height and width values
44274036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
44374036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
44474036c4dSDevin Teske	local __max_height_menu_constrain __max_width_menu_constrain
44574036c4dSDevin Teske	f_dialog_max_size \
44674036c4dSDevin Teske		__max_height_menu_constrain __max_width_menu_constrain
44774036c4dSDevin Teske
44874036c4dSDevin Teske	# Adjust height if desired
44974036c4dSDevin Teske	if [ "$__var_height" ]; then
45074036c4dSDevin Teske		if [ $__height -lt ${__min_height:-0} ]; then
45174036c4dSDevin Teske			setvar "$__var_height" $__min_height
45274036c4dSDevin Teske		elif [ $__height -gt $__max_height_menu_constrain ]; then
45374036c4dSDevin Teske			setvar "$__var_height" $__max_height_menu_constrain
45474036c4dSDevin Teske		fi
45574036c4dSDevin Teske	fi
45674036c4dSDevin Teske
45774036c4dSDevin Teske	# Adjust width if desired
45874036c4dSDevin Teske	if [ "$__var_width" ]; then
45974036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
46074036c4dSDevin Teske			: ${__min_width:=${XDIALOG_MIN_WIDTH:-35}}
46174036c4dSDevin Teske		else
46274036c4dSDevin Teske			: ${__min_width:=${DIALOG_MIN_WIDTH:-24}}
46374036c4dSDevin Teske		fi
46474036c4dSDevin Teske		if [ $__width -lt $__min_width ]; then
46574036c4dSDevin Teske			setvar "$__var_width" $__min_width
46674036c4dSDevin Teske		elif [ $__width -gt $__max_width_menu_constrain ]; then
46774036c4dSDevin Teske			setvar "$__var_width" $__max_width_menu_constrain
46874036c4dSDevin Teske		fi
46974036c4dSDevin Teske	fi
47074036c4dSDevin Teske
47174036c4dSDevin Teske	# Adjust rows if desired
47274036c4dSDevin Teske	if [ "$__var_rows" ]; then
47374036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
47474036c4dSDevin Teske			: ${__min_rows:=1}
47574036c4dSDevin Teske		else
47674036c4dSDevin Teske			: ${__min_rows:=0}
47774036c4dSDevin Teske		fi
47874036c4dSDevin Teske
47974036c4dSDevin Teske		local __max_rows=$(( $__max_height_menu_constrain - 8 ))
48074036c4dSDevin Teske		# If prompt_len is zero (no prompt), bump the max-rows by 1
48174036c4dSDevin Teske		# Default assumption is (if no argument) that there's no prompt
48274036c4dSDevin Teske		[ ${__prompt_len:-0} -gt 0 ] ||
48374036c4dSDevin Teske			__max_rows=$(( $__max_rows + 1 ))
48474036c4dSDevin Teske
48574036c4dSDevin Teske		if [ $__rows -lt $__min_rows ]; then
48674036c4dSDevin Teske			setvar "$__var_rows" $__min_rows
48774036c4dSDevin Teske		elif [ $__rows -gt $__max_rows ]; then
48874036c4dSDevin Teske			setvar "$__var_rows" $__max_rows
48974036c4dSDevin Teske		fi
49074036c4dSDevin Teske	fi
49174036c4dSDevin Teske
49274036c4dSDevin Teske	return $__retval # success if no debug warnings were printed
49374036c4dSDevin Teske}
49474036c4dSDevin Teske
49574036c4dSDevin Teske# f_dialog_infobox_size [-n] $var_height $var_width \
49674036c4dSDevin Teske#                       $title $backtitle $prompt [$hline]
497ab2043b8SDevin Teske#
498ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
499ab2043b8SDevin Teske# `--infobox' boxes sensibly.
500ab2043b8SDevin Teske#
50174036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
50274036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
50374036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
50474036c4dSDevin Teske# title, backtitle, prompt, and [optionally] hline. The optimal height and
50574036c4dSDevin Teske# width for the described widget (not exceeding the actual terminal height or
50674036c4dSDevin Teske# width) is stored in $var_height and $var_width (respectively).
50774036c4dSDevin Teske#
50874036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
50974036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
510ab2043b8SDevin Teske#
511ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
512ab2043b8SDevin Teske# dialog(1).
513ab2043b8SDevin Teske#
514ab2043b8SDevin Teskef_dialog_infobox_size()
515ab2043b8SDevin Teske{
51674036c4dSDevin Teske	local __constrain=1
51774036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
51874036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
51974036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
520ab2043b8SDevin Teske
52174036c4dSDevin Teske	# Return unless at least one size aspect has been requested
52274036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
523ab2043b8SDevin Teske
52474036c4dSDevin Teske	# Default height/width of zero for auto-sizing
52574036c4dSDevin Teske	local __height=0 __width=0 __n
526ab2043b8SDevin Teske
52774036c4dSDevin Teske	# Adjust height if desired
52874036c4dSDevin Teske	if [ "$__var_height" ]; then
529ab2043b8SDevin Teske		#
530ab2043b8SDevin Teske		# Set height based on number of rows in prompt
531ab2043b8SDevin Teske		#
53274036c4dSDevin Teske		__n=$( echo -n "$__prompt" | f_number_of_lines )
53374036c4dSDevin Teske		__n=$(( $__n + 2 ))
53474036c4dSDevin Teske		[ $__n -gt $__height ] && __height=$__n
535ab2043b8SDevin Teske
536ab2043b8SDevin Teske		#
53774036c4dSDevin Teske		# For Xdialog(1) bump height if backtitle is enabled (displayed
53874036c4dSDevin Teske		# in the X11 window with a separator line between the backtitle
53974036c4dSDevin Teske		# and msg text).
540ab2043b8SDevin Teske		#
54174036c4dSDevin Teske		if [ "$USE_XDIALOG" -a "$__btitle" ]; then
54274036c4dSDevin Teske			__n=$( echo "$__btitle" | f_number_of_lines )
54374036c4dSDevin Teske			__height=$(( $__height + $__n + 2 ))
544ab2043b8SDevin Teske		fi
545ab2043b8SDevin Teske
54674036c4dSDevin Teske		setvar "$__var_height" $__height
54774036c4dSDevin Teske	fi
548ab2043b8SDevin Teske
54974036c4dSDevin Teske	# Adjust width if desired
55074036c4dSDevin Teske	if [ "$__var_width" ]; then
55174036c4dSDevin Teske		#
55274036c4dSDevin Teske		# Bump width for long titles
55374036c4dSDevin Teske		#
55474036c4dSDevin Teske		__n=$(( ${#__title} + 4 ))
55574036c4dSDevin Teske		[ $__n -gt $__width ] && __width=$__n
55674036c4dSDevin Teske
55774036c4dSDevin Teske		#
55874036c4dSDevin Teske		# If using Xdialog(1), bump width for long backtitles (which
55974036c4dSDevin Teske		# appear within the window).
56074036c4dSDevin Teske		#
56174036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
56274036c4dSDevin Teske			__n=$(( ${#__btitle} + 4 ))
56374036c4dSDevin Teske			[ $__n -gt $__width ] && __width=$__n
56474036c4dSDevin Teske		fi
56574036c4dSDevin Teske
56674036c4dSDevin Teske		#
56774036c4dSDevin Teske		# Bump width for long prompts
56874036c4dSDevin Teske		#
56974036c4dSDevin Teske		__n=$( echo "$__prompt" | f_longest_line_length )
57074036c4dSDevin Teske		__n=$(( $__n + 4 )) # add width for border
57174036c4dSDevin Teske		[ $__n -gt $__width ] && __width=$__n
57274036c4dSDevin Teske
57374036c4dSDevin Teske		#
57474036c4dSDevin Teske		# Bump width for long hlines. Xdialog(1) supports `--hline' but
57574036c4dSDevin Teske		# it's currently not used (so don't do anything here if using
57674036c4dSDevin Teske		# Xdialog(1)).
57774036c4dSDevin Teske		#
57874036c4dSDevin Teske		if [ ! "$USE_XDIALOG" ]; then
57974036c4dSDevin Teske			__n=$(( ${#__hline} + 10 ))
58074036c4dSDevin Teske			[ $__n -gt $__width ] && __width=$__n
58174036c4dSDevin Teske		fi
58274036c4dSDevin Teske
58374036c4dSDevin Teske		# Bump width by 16.6% if using Xdialog(1)
58474036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __width=$(( $__width + $__width / 6 ))
58574036c4dSDevin Teske
58674036c4dSDevin Teske		setvar "$__var_width" $__width
58774036c4dSDevin Teske	fi
58874036c4dSDevin Teske
58974036c4dSDevin Teske	# Constrain values to sensible minimums/maximums unless `-n' was passed
59074036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
59174036c4dSDevin Teske	[ ! "$__constrain" ] ||
59274036c4dSDevin Teske		f_dialog_size_constrain "$__var_height" "$__var_width"
593ab2043b8SDevin Teske}
594ab2043b8SDevin Teske
59574036c4dSDevin Teske# f_dialog_buttonbox_size [-n] $var_height $var_width \
59674036c4dSDevin Teske#                         $title $backtitle $prompt [$hline]
597ab2043b8SDevin Teske#
598ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
599ab2043b8SDevin Teske# `--msgbox' and `--yesno' boxes sensibly.
600ab2043b8SDevin Teske#
60174036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
60274036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
60374036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
60474036c4dSDevin Teske# title, backtitle, prompt, and [optionally] hline. The optimal height and
60574036c4dSDevin Teske# width for the described widget (not exceeding the actual terminal height or
60674036c4dSDevin Teske# width) is stored in $var_height and $var_width (respectively).
60774036c4dSDevin Teske#
60874036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
60974036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
610ab2043b8SDevin Teske#
611ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
612ab2043b8SDevin Teske# dialog(1).
613ab2043b8SDevin Teske#
614ab2043b8SDevin Teskef_dialog_buttonbox_size()
615ab2043b8SDevin Teske{
61674036c4dSDevin Teske	local __constrain=1
61774036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
61874036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
61974036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
620ab2043b8SDevin Teske
62174036c4dSDevin Teske	# Return unless at least one size aspect has been requested
62274036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
62374036c4dSDevin Teske
62474036c4dSDevin Teske	# Calculate height/width of infobox (adjusted/constrained below)
62574036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
62674036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
62774036c4dSDevin Teske	local __height_bbox_size __width_bbox_size
62874036c4dSDevin Teske	f_dialog_infobox_size -n \
62974036c4dSDevin Teske		"${__var_height:+__height_bbox_size}" \
63074036c4dSDevin Teske		"${__var_width:+__width_bbox_size}" \
63174036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
63274036c4dSDevin Teske
63374036c4dSDevin Teske	# Adjust height if desired
63474036c4dSDevin Teske	if [ "$__var_height" ]; then
635ab2043b8SDevin Teske		# Add height to accomodate the buttons
63674036c4dSDevin Teske		__height_bbox_size=$(( $__height_bbox_size + 2 ))
637ab2043b8SDevin Teske
638ab2043b8SDevin Teske		# Adjust for clipping with Xdialog(1) on Linux/GTK2
63974036c4dSDevin Teske		[ "$USE_XDIALOG" ] &&
64074036c4dSDevin Teske			__height_bbox_size=$(( $__height_bbox_size + 3 ))
641ab2043b8SDevin Teske
64274036c4dSDevin Teske		setvar "$__var_height" $__height_bbox_size
643ab2043b8SDevin Teske	fi
644ab2043b8SDevin Teske
64574036c4dSDevin Teske	# No adjustemnts to width, just pass-thru the infobox width
64674036c4dSDevin Teske	if [ "$__var_width" ]; then
64774036c4dSDevin Teske		setvar "$__var_width" $__width_bbox_size
64874036c4dSDevin Teske	fi
64974036c4dSDevin Teske
65074036c4dSDevin Teske	# Constrain values to sensible minimums/maximums unless `-n' was passed
65174036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
65274036c4dSDevin Teske	[ ! "$__constrain" ] ||
65374036c4dSDevin Teske		f_dialog_size_constrain "$__var_height" "$__var_width"
654ab2043b8SDevin Teske}
655ab2043b8SDevin Teske
65674036c4dSDevin Teske# f_dialog_inputbox_size [-n] $var_height $var_width \
65774036c4dSDevin Teske#                        $title $backtitle $prompt $init [$hline]
658ab2043b8SDevin Teske#
659ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
660ab2043b8SDevin Teske# `--inputbox' boxes sensibly.
661ab2043b8SDevin Teske#
66274036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
66374036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
66474036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
66574036c4dSDevin Teske# title, backtitle, prompt, and [optionally] hline. The optimal height and
66674036c4dSDevin Teske# width for the described widget (not exceeding the actual terminal height or
66774036c4dSDevin Teske# width) is stored in $var_height and $var_width (respectively).
66874036c4dSDevin Teske#
66974036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
67074036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
671ab2043b8SDevin Teske#
672ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
673ab2043b8SDevin Teske# dialog(1).
674ab2043b8SDevin Teske#
675ab2043b8SDevin Teskef_dialog_inputbox_size()
676ab2043b8SDevin Teske{
67774036c4dSDevin Teske	local __constrain=1
67874036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
67974036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
68074036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5" __init="$6" __hline="$7"
681ab2043b8SDevin Teske
68274036c4dSDevin Teske	# Return unless at least one size aspect has been requested
68374036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
684ab2043b8SDevin Teske
68574036c4dSDevin Teske	# Calculate height/width of buttonbox (adjusted/constrained below)
68674036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
68774036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
68874036c4dSDevin Teske	local __height_ibox_size __width_ibox_size
68974036c4dSDevin Teske	f_dialog_buttonbox_size -n \
69074036c4dSDevin Teske		"${__var_height:+__height_ibox_size}" \
69174036c4dSDevin Teske		"${__var_width:+__width_ibox_size}" \
69274036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
693ab2043b8SDevin Teske
69474036c4dSDevin Teske	# Adjust height if desired
69574036c4dSDevin Teske	if [ "$__var_height" ]; then
69674036c4dSDevin Teske		# Add height for input box (not needed for Xdialog(1))
69774036c4dSDevin Teske		[ ! "$USE_XDIALOG" ] &&
69874036c4dSDevin Teske			__height_ibox_size=$(( $__height_ibox_size + 3 ))
699ab2043b8SDevin Teske
70074036c4dSDevin Teske		setvar "$__var_height" $__height_ibox_size
701ab2043b8SDevin Teske	fi
702ab2043b8SDevin Teske
70374036c4dSDevin Teske	# Adjust width if desired
70474036c4dSDevin Teske	if [ "$__var_width" ]; then
70574036c4dSDevin Teske		# Bump width for initial text (something neither dialog(1) nor
70674036c4dSDevin Teske		# Xdialog(1) do, but worth it!; add 16.6% if using Xdialog(1))
70774036c4dSDevin Teske		local __n=$(( ${#__init} + 7 ))
70874036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 ))
70974036c4dSDevin Teske		[ $__n -gt $__width_ibox_size ] && __width_ibox_size=$__n
71074036c4dSDevin Teske
71174036c4dSDevin Teske		setvar "$__var_width" $__width_ibox_size
71274036c4dSDevin Teske	fi
71374036c4dSDevin Teske
71474036c4dSDevin Teske	# Constrain values to sensible minimums/maximums unless `-n' was passed
71574036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
71674036c4dSDevin Teske	[ ! "$__constrain" ] ||
71774036c4dSDevin Teske		f_dialog_size_constrain "$__var_height" "$__var_width"
718ab2043b8SDevin Teske}
719ab2043b8SDevin Teske
72074036c4dSDevin Teske# f_xdialog_2inputsbox_size [-n] $var_height $var_width \
72174036c4dSDevin Teske#                           $title $backtitle $prompt \
722ab2043b8SDevin Teske#                           $label1 $init1 $label2 $init2
723ab2043b8SDevin Teske#
724ab2043b8SDevin Teske# Xdialog(1) does not perform auto-sizing of the width and height of
725ab2043b8SDevin Teske# `--2inputsbox' boxes sensibly.
726ab2043b8SDevin Teske#
72774036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
72874036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
72974036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
73074036c4dSDevin Teske# title, backtitle, prompt, label for the first field, initial text for said
73174036c4dSDevin Teske# field, label for the second field, and initial text for said field. The
73274036c4dSDevin Teske# optimal height and width for the described widget (not exceeding the actual
73374036c4dSDevin Teske# terminal height or width) is stored in $var_height and $var_width
73474036c4dSDevin Teske# (respectively).
73574036c4dSDevin Teske#
73674036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
73774036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
738ab2043b8SDevin Teske#
739ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
740ab2043b8SDevin Teske# Xdialog(1).
741ab2043b8SDevin Teske#
742ab2043b8SDevin Teskef_xdialog_2inputsbox_size()
743ab2043b8SDevin Teske{
74474036c4dSDevin Teske	local __constrain=1
74574036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
74674036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
74774036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5"
74874036c4dSDevin Teske	local __label1="$6" __init1="$7" __label2="$8" __init2="$9"
749ab2043b8SDevin Teske
75074036c4dSDevin Teske	# Return unless at least one size aspect has been requested
75174036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
752ab2043b8SDevin Teske
75374036c4dSDevin Teske	# Calculate height/width of inputbox (adjusted/constrained below)
75474036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
75574036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
75674036c4dSDevin Teske	local __height_2ibox_size __width_2ibox_size
75774036c4dSDevin Teske	f_dialog_inputbox_size -n \
75874036c4dSDevin Teske		"${__var_height:+__height_2ibox_size}" \
75974036c4dSDevin Teske		"${__var_width:+__width_2ibox_size}" \
76074036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline" "$__init1"
761ab2043b8SDevin Teske
76274036c4dSDevin Teske	# Adjust height if desired
76374036c4dSDevin Teske	if [ "$__var_height" ]; then
76474036c4dSDevin Teske		# Add height for 1st label, 2nd label, and 2nd input box
76574036c4dSDevin Teske		__height_2ibox_size=$(( $__height_2ibox_size + 2 + 2 + 2  ))
76674036c4dSDevin Teske		setvar "$__var_height" $__height_2ibox_size
767ab2043b8SDevin Teske	fi
768ab2043b8SDevin Teske
76974036c4dSDevin Teske	# Adjust width if desired
77074036c4dSDevin Teske	if [ "$__var_width" ]; then
77174036c4dSDevin Teske		local __n
772ab2043b8SDevin Teske
77374036c4dSDevin Teske		# Bump width for first label text (+16.6% since Xdialog(1))
77474036c4dSDevin Teske		__n=$(( ${#__label1} + 7 ))
77574036c4dSDevin Teske		__n=$(( $__n + $__n / 6 ))
77674036c4dSDevin Teske		[ $__n -gt $__width_2ibox_size ] && __width_2ibox_size=$__n
777ab2043b8SDevin Teske
77874036c4dSDevin Teske		# Bump width for second label text (+16.6% since Xdialog(1))
77974036c4dSDevin Teske		__n=$(( ${#__label2} + 7 ))
78074036c4dSDevin Teske		__n=$(( $__n + $__n / 6 ))
78174036c4dSDevin Teske		[ $__n -gt $__width_2ibox_size ] && __width_2ibox_size=$__n
782ab2043b8SDevin Teske
78374036c4dSDevin Teske		# Bump width for 2nd initial text (something neither dialog(1)
78474036c4dSDevin Teske		# nor Xdialog(1) do, but worth it!; +16.6% since Xdialog(1))
78574036c4dSDevin Teske		__n=$(( ${#__init2} + 7 ))
78674036c4dSDevin Teske		__n=$(( $__n + $__n / 6 ))
78774036c4dSDevin Teske		[ $__n -gt $__width_2ibox_size ] && __width_2ibox_size=$__n
78874036c4dSDevin Teske
78974036c4dSDevin Teske		setvar "$__var_width" $__width_2ibox_size
790ab2043b8SDevin Teske	fi
791ab2043b8SDevin 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_dialog_menu_size [-n] $var_height $var_width $var_rows \
79974036c4dSDevin Teske#                    $title $backtitle $prompt $hline \
800ab2043b8SDevin Teske#                    $tag1 $item1 $tag2 $item2 ...
801ab2043b8SDevin Teske#
802ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
803ab2043b8SDevin Teske# `--menu' boxes sensibly.
804ab2043b8SDevin Teske#
80574036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
80674036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
80774036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
80874036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
80974036c4dSDevin Teske# arguments are the menu list itself (comprised of tag/item couplets). The
81074036c4dSDevin Teske# optimal height, width, and rows for the described widget (not exceeding the
81174036c4dSDevin Teske# actual terminal height or width) is stored in $var_height, $var_width, and
81274036c4dSDevin Teske# $var_rows (respectively).
813ab2043b8SDevin Teske#
81474036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
81574036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
816ab2043b8SDevin Teske#
817ab2043b8SDevin Teskef_dialog_menu_size()
818ab2043b8SDevin Teske{
81974036c4dSDevin Teske	local __constrain=1
82074036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
82174036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __var_rows="$3"
82274036c4dSDevin Teske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
82374036c4dSDevin Teske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
824ab2043b8SDevin Teske
82574036c4dSDevin Teske	# Return unless at least one size aspect has been requested
82674036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
82774036c4dSDevin Teske		return $FAILURE
828ab2043b8SDevin Teske
82974036c4dSDevin Teske	# Calculate height/width of infobox (adjusted/constrained below)
83074036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
83174036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
83274036c4dSDevin Teske	local __height_menu_size __width_menu_size
83374036c4dSDevin Teske	f_dialog_infobox_size -n \
83474036c4dSDevin Teske		"${__var_height:+__height_menu_size}" \
83574036c4dSDevin Teske		"${__var_width:+__width_menu_size}" \
83674036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
837ab2043b8SDevin Teske
838ab2043b8SDevin Teske	#
83974036c4dSDevin Teske	# Always process the menu-item arguments to get the longest tag-length,
84074036c4dSDevin Teske	# longest item-length (both used to bump the width), and the number of
84174036c4dSDevin Teske	# rows (used to bump the height).
842ab2043b8SDevin Teske	#
84374036c4dSDevin Teske	local __longest_tag=0 __longest_item=0 __rows=0
844ab2043b8SDevin Teske	while [ $# -ge 2 ]; do
84574036c4dSDevin Teske		local __tag="$1" __item="$2"
846ab2043b8SDevin Teske		shift 2 # tag/item
84774036c4dSDevin Teske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
84874036c4dSDevin Teske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
84974036c4dSDevin Teske		__rows=$(( $__rows + 1 ))
850ab2043b8SDevin Teske	done
851ab2043b8SDevin Teske
85274036c4dSDevin Teske	# Adjust rows early (for up-comning height calculation)
85374036c4dSDevin Teske	if [ "$__var_height" -o "$__var_rows" ]; then
85474036c4dSDevin Teske		# Add a row for visual aid if using Xdialog(1)
85574036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __rows=$(( $__rows + 1 ))
856ab2043b8SDevin Teske	fi
857ab2043b8SDevin Teske
85874036c4dSDevin Teske	# Adjust height if desired
85974036c4dSDevin Teske	if [ "$__var_height" ]; then
86074036c4dSDevin Teske		# Add rows to height
861ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
86274036c4dSDevin Teske			__height_menu_size=$((
86374036c4dSDevin Teske				$__height_menu_size + $__rows + 7 ))
864ab2043b8SDevin Teske		else
86574036c4dSDevin Teske			__height_menu_size=$((
86674036c4dSDevin Teske				$__height_menu_size + $__rows + 4 ))
867ab2043b8SDevin Teske		fi
86874036c4dSDevin Teske		setvar "$__var_height" $__height_menu_size
86974036c4dSDevin Teske	fi
870ab2043b8SDevin Teske
87174036c4dSDevin Teske	# Adjust width if desired
87274036c4dSDevin Teske	if [ "$__var_width" ]; then
87374036c4dSDevin Teske		# The sum total between the longest tag-length and the
87474036c4dSDevin Teske		# longest item-length should be used to bump menu width
87574036c4dSDevin Teske		local __n=$(( $__longest_tag + $__longest_item + 10 ))
87674036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
87774036c4dSDevin Teske		[ $__n -gt $__width_menu_size ] && __width_menu_size=$__n
87874036c4dSDevin Teske
87974036c4dSDevin Teske		setvar "$__var_width" $__width_menu_size
88074036c4dSDevin Teske	fi
88174036c4dSDevin Teske
88274036c4dSDevin Teske	# Store adjusted rows if desired
88374036c4dSDevin Teske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows
88474036c4dSDevin Teske
88574036c4dSDevin Teske	# Constrain height, width, and rows to sensible minimum/maximum values
88674036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
88774036c4dSDevin Teske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
88874036c4dSDevin Teske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
889ab2043b8SDevin Teske}
890ab2043b8SDevin Teske
89174036c4dSDevin Teske# f_dialog_menu_with_help_size [-n] $var_height $var_width $var_rows \
89274036c4dSDevin Teske#                              $title $backtitle $prompt $hline \
893ab2043b8SDevin Teske#                              $tag1 $item1 $help1 $tag2 $item2 $help2 ...
894ab2043b8SDevin Teske#
895ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
896ab2043b8SDevin Teske# `--menu' boxes sensibly.
897ab2043b8SDevin Teske#
89874036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
89974036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
90074036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
90174036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
90274036c4dSDevin Teske# arguments are the menu list itself (comprised of tag/item/help triplets). The
90374036c4dSDevin Teske# optimal height, width, and rows for the described widget (not exceeding the
90474036c4dSDevin Teske# actual terminal height or width) is stored in $var_height, $var_width, and
90574036c4dSDevin Teske# $var_rows (respectively).
906ab2043b8SDevin Teske#
90774036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
90874036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
909ab2043b8SDevin Teske#
910ab2043b8SDevin Teskef_dialog_menu_with_help_size()
911ab2043b8SDevin Teske{
91274036c4dSDevin Teske	local __constrain=1
91374036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
91474036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __var_rows="$3"
91574036c4dSDevin Teske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
91674036c4dSDevin Teske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
917ab2043b8SDevin Teske
91874036c4dSDevin Teske	# Return unless at least one size aspect has been requested
91974036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
92074036c4dSDevin Teske		return $FAILURE
921ab2043b8SDevin Teske
92274036c4dSDevin Teske	# Calculate height/width of infobox (adjusted/constrained below)
92374036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
92474036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
92574036c4dSDevin Teske	local __height_menu_with_help_size __width_menu_with_help_size
92674036c4dSDevin Teske	f_dialog_infobox_size -n \
92774036c4dSDevin Teske		"${__var_height:+__height_menu_with_help_size}" \
92874036c4dSDevin Teske		"${__var_width:+__width_menu_with_help_size}" \
92974036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
930ab2043b8SDevin Teske
931ab2043b8SDevin Teske	#
93274036c4dSDevin Teske	# Always process the menu-item arguments to get the longest tag-length,
93374036c4dSDevin Teske	# longest item-length, longest help-length (help-length only considered
93474036c4dSDevin Teske	# if using Xdialog(1), as it places the help string in the widget) --
93574036c4dSDevin Teske	# all used to bump the width -- and the number of rows (used to bump
93674036c4dSDevin Teske	# the height).
937ab2043b8SDevin Teske	#
93874036c4dSDevin Teske	local __longest_tag=0 __longest_item=0 __longest_help=0 __rows=0
939ab2043b8SDevin Teske	while [ $# -ge 3 ]; do
94074036c4dSDevin Teske		local __tag="$1" __item="$2" __help="$3"
941ab2043b8SDevin Teske		shift 3 # tag/item/help
94274036c4dSDevin Teske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
94374036c4dSDevin Teske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
94474036c4dSDevin Teske		[ ${#__help} -gt $__longest_help ] && __longest_help=${#__help}
94574036c4dSDevin Teske		__rows=$(( $__rows + 1 ))
946ab2043b8SDevin Teske	done
947ab2043b8SDevin Teske
94874036c4dSDevin Teske	# Adjust rows early (for up-coming height calculation)
94974036c4dSDevin Teske	if [ "$__var_height" -o "$__var_rows" ]; then
95074036c4dSDevin Teske		# Add a row for visual aid if using Xdialog(1)
95174036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __rows=$(( $__rows + 1 ))
95274036c4dSDevin Teske	fi
95374036c4dSDevin Teske
95474036c4dSDevin Teske	# Adjust height if desired
95574036c4dSDevin Teske	if [ "$__var_height" ]; then
95674036c4dSDevin Teske		# Add rows to height
95774036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
95874036c4dSDevin Teske			__height_menu_with_help_size=$((
95974036c4dSDevin Teske				$__height_menu_with_help_size + $__rows + 8 ))
960ab2043b8SDevin Teske		else
96174036c4dSDevin Teske			__height_menu_with_help_size=$((
96274036c4dSDevin Teske				$__height_menu_with_help_size + $__rows + 4 ))
963ab2043b8SDevin Teske		fi
96474036c4dSDevin Teske		setvar "$__var_height" $__height_menu_with_help_size
965ab2043b8SDevin Teske	fi
966ab2043b8SDevin Teske
96774036c4dSDevin Teske	# Adjust width if desired
96874036c4dSDevin Teske	if [ "$__var_width" ]; then
96974036c4dSDevin Teske		# The sum total between the longest tag-length and the
97074036c4dSDevin Teske		# longest item-length should be used to bump menu width
97174036c4dSDevin Teske		local __n=$(( $__longest_tag + $__longest_item + 10 ))
97274036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
97374036c4dSDevin Teske		[ $__n -gt $__width_menu_with_help_size ] &&
97474036c4dSDevin Teske			__width_menu_with_help_size=$__n
97574036c4dSDevin Teske
976ab2043b8SDevin Teske		# Update width for help text if using Xdialog(1)
977ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
97874036c4dSDevin Teske			__n=$(( $__longest_help + 10 ))
97974036c4dSDevin Teske			__n=$(( $__n + $__n / 6 )) # plus 16.6%
98074036c4dSDevin Teske			[ $__n -gt $__width_menu_with_help_size ] &&
98174036c4dSDevin Teske				__width_menu_with_help_size=$__n
982ab2043b8SDevin Teske		fi
983ab2043b8SDevin Teske
98474036c4dSDevin Teske		setvar "$__var_width" $__width_menu_with_help_size
985ab2043b8SDevin Teske	fi
986ab2043b8SDevin Teske
98774036c4dSDevin Teske	# Store adjusted rows if desired
98874036c4dSDevin Teske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows
98974036c4dSDevin Teske
99074036c4dSDevin Teske	# Constrain height, width, and rows to sensible minimum/maximum values
99174036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
99274036c4dSDevin Teske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
99374036c4dSDevin Teske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
994ab2043b8SDevin Teske}
995ab2043b8SDevin Teske
99674036c4dSDevin Teske# f_dialog_radiolist_size [-n] $var_height $var_width $var_rows \
99774036c4dSDevin Teske#                         $title $backtitle $prompt $hline \
998ab2043b8SDevin Teske#                         $tag1 $item1 $status1 $tag2 $item2 $status2 ...
999ab2043b8SDevin Teske#
1000ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1001ab2043b8SDevin Teske# `--radiolist' boxes sensibly.
1002ab2043b8SDevin Teske#
100374036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
100474036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
100574036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
100674036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
100774036c4dSDevin Teske# arguments are the radio list itself (comprised of tag/item/status triplets).
100874036c4dSDevin Teske# The optimal height, width, and rows for the described widget (not exceeding
100974036c4dSDevin Teske# the actual terminal height or width) is stored in $var_height, $var_width,
101074036c4dSDevin Teske# and $var_rows (respectively).
1011ab2043b8SDevin Teske#
101274036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
101374036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
1014ab2043b8SDevin Teske#
1015ab2043b8SDevin Teskef_dialog_radiolist_size()
1016ab2043b8SDevin Teske{
101774036c4dSDevin Teske	local __constrain=1
101874036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
101974036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __var_rows="$3"
102074036c4dSDevin Teske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
102174036c4dSDevin Teske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
1022ab2043b8SDevin Teske
102374036c4dSDevin Teske	# Return unless at least one size aspect has been requested
102474036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
102574036c4dSDevin Teske		return $FAILURE
1026ab2043b8SDevin Teske
102774036c4dSDevin Teske	# Calculate height/width of infobox (adjusted/constrained below)
102874036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
102974036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
103074036c4dSDevin Teske	local __height_rlist_size __width_rlist_size
103174036c4dSDevin Teske	f_dialog_infobox_size -n \
103274036c4dSDevin Teske		"${__var_height:+__height_rlist_size}" \
103374036c4dSDevin Teske		"${__var_width:+__width_rlist_size}" \
103474036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
1035ab2043b8SDevin Teske
1036ab2043b8SDevin Teske	#
103774036c4dSDevin Teske	# Always process the menu-item arguments to get the longest tag-length,
103874036c4dSDevin Teske	# longest item-length (both used to bump the width), and the number of
103974036c4dSDevin Teske	# rows (used to bump the height).
1040ab2043b8SDevin Teske	#
104174036c4dSDevin Teske	local __longest_tag=0 __longest_item=0 __rows=0
1042efc0f5e2SDevin Teske	while [ $# -ge 3 ]; do
104374036c4dSDevin Teske		local __tag="$1" __item="$2"
1044efc0f5e2SDevin Teske		shift 3 # tag/item/status
104574036c4dSDevin Teske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
104674036c4dSDevin Teske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
104774036c4dSDevin Teske		__rows=$(( $__rows + 1 ))
1048ab2043b8SDevin Teske	done
1049ab2043b8SDevin Teske
105074036c4dSDevin Teske	# Adjust rows early (for up-coming height calculation)
105174036c4dSDevin Teske	if [ "$__var_height" -o "$__var_rows" ]; then
105274036c4dSDevin Teske		# Add a row for visual aid if using Xdialog(1)
105374036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __rows=$(( $__rows + 1 ))
1054ab2043b8SDevin Teske	fi
1055ab2043b8SDevin Teske
105674036c4dSDevin Teske	# Adjust height if desired
105774036c4dSDevin Teske	if [ "$__var_height" ]; then
105874036c4dSDevin Teske		# Add rows to height
1059ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
106074036c4dSDevin Teske			__height_rlist_size=$((
106174036c4dSDevin Teske				$__height_rlist_size + $__rows + 7 ))
1062ab2043b8SDevin Teske		else
106374036c4dSDevin Teske			__height_rlist_size=$((
106474036c4dSDevin Teske				$__height_rlist_size + $__rows + 4 ))
1065ab2043b8SDevin Teske		fi
106674036c4dSDevin Teske		setvar "$__var_height" $__height_rlist_size
106774036c4dSDevin Teske	fi
1068ab2043b8SDevin Teske
106974036c4dSDevin Teske	# Adjust width if desired
107074036c4dSDevin Teske	if [ "$__var_width" ]; then
107174036c4dSDevin Teske		# Sum total between longest tag-length, longest item-length,
107274036c4dSDevin Teske		# and radio-button width should be used to bump menu width
107374036c4dSDevin Teske		local __n=$(( $__longest_tag + $__longest_item + 13 ))
107474036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
107574036c4dSDevin Teske		[ $__n -gt $__width_rlist_size ] && __width_rlist_size=$__n
107674036c4dSDevin Teske
107774036c4dSDevin Teske		setvar "$__var_width" $__width_rlist_size
107874036c4dSDevin Teske	fi
107974036c4dSDevin Teske
108074036c4dSDevin Teske	# Store adjusted rows if desired
108174036c4dSDevin Teske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows
108274036c4dSDevin Teske
108374036c4dSDevin Teske	# Constrain height, width, and rows to sensible minimum/maximum values
108474036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
108574036c4dSDevin Teske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
108674036c4dSDevin Teske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
1087ab2043b8SDevin Teske}
1088ab2043b8SDevin Teske
108974036c4dSDevin Teske# f_dialog_checklist_size [-n] $var_height $var_width $var_rows \
109074036c4dSDevin Teske#                         $title $backtitle $prompt $hline \
1091dbc6a6e1SDevin Teske#                         $tag1 $item1 $status1 $tag2 $item2 $status2 ...
1092dbc6a6e1SDevin Teske#
1093dbc6a6e1SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1094dbc6a6e1SDevin Teske# `--checklist' boxes sensibly.
1095dbc6a6e1SDevin Teske#
109674036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
109774036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
109874036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
109974036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
110074036c4dSDevin Teske# arguments are the check list itself (comprised of tag/item/status triplets).
110174036c4dSDevin Teske# The optimal height, width, and rows for the described widget (not exceeding
110274036c4dSDevin Teske# the actual terminal height or width) is stored in $var_height, $var_width,
110374036c4dSDevin Teske# and $var_rows (respectively).
1104dbc6a6e1SDevin Teske#
110574036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
110674036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
1107dbc6a6e1SDevin Teske#
1108dbc6a6e1SDevin Teskef_dialog_checklist_size()
1109dbc6a6e1SDevin Teske{
1110dbc6a6e1SDevin Teske	f_dialog_radiolist_size "$@"
1111dbc6a6e1SDevin Teske}
1112dbc6a6e1SDevin Teske
111374036c4dSDevin Teske# f_dialog_radiolist_with_help_size [-n] $var_height $var_width $var_rows \
111474036c4dSDevin Teske#                                   $title $backtitle $prompt $hline \
1115dbc6a6e1SDevin Teske#                                   $tag1 $item1 $status1 $help1 \
1116dbc6a6e1SDevin Teske#                                   $tag2 $item2 $status2 $help2 ...
1117dbc6a6e1SDevin Teske#
1118dbc6a6e1SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1119dbc6a6e1SDevin Teske# `--radiolist' boxes sensibly.
1120dbc6a6e1SDevin Teske#
112174036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
112274036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
112374036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
112474036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
112574036c4dSDevin Teske# arguments are the radio list itself (comprised of tag/item/status/help
112674036c4dSDevin Teske# quadruplets). The optimal height, width, and rows for the described widget
112774036c4dSDevin Teske# (not exceeding the actual terminal height or width) is stored in $var_height,
112874036c4dSDevin Teske# $var_width, and $var_rows (respectively).
1129dbc6a6e1SDevin Teske#
113074036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
113174036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
1132dbc6a6e1SDevin Teske#
1133dbc6a6e1SDevin Teskef_dialog_radiolist_with_help_size()
1134dbc6a6e1SDevin Teske{
113574036c4dSDevin Teske	local __constrain=1
113674036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
113774036c4dSDevin Teske	local __var_height="$1" __var_width="$2" __var_rows="$3"
113874036c4dSDevin Teske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
113974036c4dSDevin Teske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
1140dbc6a6e1SDevin Teske
114174036c4dSDevin Teske	# Return unless at least one size aspect has been requested
114274036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
114374036c4dSDevin Teske		return $FAILURE
1144dbc6a6e1SDevin Teske
114574036c4dSDevin Teske	# Calculate height/width of infobox (adjusted/constrained below)
114674036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
114774036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
114874036c4dSDevin Teske	local __height_rlist_with_help_size __width_rlist_with_help_size
114974036c4dSDevin Teske	f_dialog_infobox_size -n \
115074036c4dSDevin Teske		"${__var_height:+__height_rlist_with_help_size}" \
115174036c4dSDevin Teske		"${__var_width:+__width_rlist_with_help_size}" \
115274036c4dSDevin Teske		"$__title" "$__btitle" "$__prompt" "$__hline"
1153dbc6a6e1SDevin Teske
1154dbc6a6e1SDevin Teske	#
115574036c4dSDevin Teske	# Always process the menu-item arguments to get the longest tag-length,
115674036c4dSDevin Teske	# longest item-length, longest help-length (help-length only considered
115774036c4dSDevin Teske	# if using Xdialog(1), as it places the help string in the widget) --
115874036c4dSDevin Teske	# all used to bump the width -- and the number of rows (used to bump
115974036c4dSDevin Teske	# the height).
1160dbc6a6e1SDevin Teske	#
116174036c4dSDevin Teske	local __longest_tag=0 __longest_item=0 __longest_help=0 __rows=0
1162dbc6a6e1SDevin Teske	while [ $# -ge 4 ]; do
116374036c4dSDevin Teske		local __tag="$1" __item="$2" __status="$3" __help="$4"
1164dbc6a6e1SDevin Teske		shift 4 # tag/item/status/help
116574036c4dSDevin Teske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
116674036c4dSDevin Teske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
116774036c4dSDevin Teske		[ ${#__help} -gt $__longest_help ] && __longest_help=${#__help}
116874036c4dSDevin Teske		__rows=$(( $__rows + 1 ))
1169dbc6a6e1SDevin Teske	done
1170dbc6a6e1SDevin Teske
117174036c4dSDevin Teske	# Adjust rows early (for up-coming height calculation)
117274036c4dSDevin Teske	if [ "$__var_height" -o "$__var_rows" ]; then
117374036c4dSDevin Teske		# Add a row for visual aid if using Xdialog(1)
117474036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __rows=$(( $__rows + 1 ))
117574036c4dSDevin Teske	fi
117674036c4dSDevin Teske
117774036c4dSDevin Teske	# Adjust height if desired
117874036c4dSDevin Teske	if [ "$__var_height" ]; then
117974036c4dSDevin Teske		# Add rows to height
118074036c4dSDevin Teske		if [ "$USE_XDIALOG" ]; then
118174036c4dSDevin Teske			__height_rlist_with_help_size=$((
118274036c4dSDevin Teske				$__height_rlist_with_help_size + $__rows + 7 ))
1183dbc6a6e1SDevin Teske		else
118474036c4dSDevin Teske			__height_rlist_with_help_size=$((
118574036c4dSDevin Teske				$__height_rlist_with_help_size + $__rows + 4 ))
1186dbc6a6e1SDevin Teske		fi
118774036c4dSDevin Teske		setvar "$__var_height" $__height
1188dbc6a6e1SDevin Teske	fi
1189dbc6a6e1SDevin Teske
119074036c4dSDevin Teske	# Adjust width if desired
119174036c4dSDevin Teske	if [ "$__var_width" ]; then
119274036c4dSDevin Teske		# Sum total between longest tag-length, longest item-length,
119374036c4dSDevin Teske		# and radio-button width should be used to bump menu width
119474036c4dSDevin Teske		local __n=$(( $__longest_tag + $__longest_item + 13 ))
119574036c4dSDevin Teske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
119674036c4dSDevin Teske		[ $__n -gt $__width_rlist_with_help_size ] &&
119774036c4dSDevin Teske			__width_rlist_with_help_size=$__n
119874036c4dSDevin Teske
1199dbc6a6e1SDevin Teske		# Update width for help text if using Xdialog(1)
1200dbc6a6e1SDevin Teske		if [ "$USE_XDIALOG" ]; then
120174036c4dSDevin Teske			__n=$(( $__longest_help + 10 ))
120274036c4dSDevin Teske			__n=$(( $__n + $__n / 6 )) # plus 16.6%
120374036c4dSDevin Teske			[ $__n -gt $__width_rlist_with_help_size ] &&
120474036c4dSDevin Teske				__width_rlist_with_help_size=$__n
1205dbc6a6e1SDevin Teske		fi
1206dbc6a6e1SDevin Teske
120774036c4dSDevin Teske		setvar "$__var_width" $__width_rlist_with_help_size
1208dbc6a6e1SDevin Teske	fi
1209dbc6a6e1SDevin Teske
121074036c4dSDevin Teske	# Store adjusted rows if desired
121174036c4dSDevin Teske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows
121274036c4dSDevin Teske
121374036c4dSDevin Teske	# Constrain height, width, and rows to sensible minimum/maximum values
121474036c4dSDevin Teske	# Return success if no-constrain, else return status from constrain
121574036c4dSDevin Teske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
121674036c4dSDevin Teske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
1217dbc6a6e1SDevin Teske}
1218dbc6a6e1SDevin Teske
121974036c4dSDevin Teske# f_dialog_checklist_with_help_size [-n] $var_height $var_width $var_rows \
122074036c4dSDevin Teske#                                   $title $backtitle $prompt $hline \
1221dbc6a6e1SDevin Teske#                                   $tag1 $item1 $status1 $help1 \
1222dbc6a6e1SDevin Teske#                                   $tag2 $item2 $status2 $help2 ...
1223dbc6a6e1SDevin Teske#
1224dbc6a6e1SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1225dbc6a6e1SDevin Teske# `--checklist' boxes sensibly.
1226dbc6a6e1SDevin Teske#
122774036c4dSDevin Teske# This function helps solve this issue by taking three sets of sequential
122874036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
122974036c4dSDevin Teske# storing the calculated height, width, and rows. The second set of arguments
123074036c4dSDevin Teske# are the title, backtitle, prompt, and hline. The [optional] third set of
123174036c4dSDevin Teske# arguments are the check list itself (comprised of tag/item/status/help
123274036c4dSDevin Teske# quadruplets). The optimal height, width, and rows for the described widget
123374036c4dSDevin Teske# (not exceeding the actual terminal height or width) is stored in $var_height,
123474036c4dSDevin Teske# $var_width, and $var_rows (respectively).
1235dbc6a6e1SDevin Teske#
123674036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
123774036c4dSDevin Teske# and $var_rows) are not constrained to minimum/maximum values.
1238dbc6a6e1SDevin Teske#
1239dbc6a6e1SDevin Teskef_dialog_checklist_with_help_size()
1240dbc6a6e1SDevin Teske{
1241dbc6a6e1SDevin Teske	f_dialog_radiolist_with_help_size "$@"
1242dbc6a6e1SDevin Teske}
1243dbc6a6e1SDevin Teske
124474036c4dSDevin Teske# f_dialog_calendar_size [-n] $var_height $var_width \
124574036c4dSDevin Teske#                        $title $backtitle $prompt [$hline]
1246ab2043b8SDevin Teske#
1247ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1248ab2043b8SDevin Teske# `--calendar' boxes sensibly.
1249ab2043b8SDevin Teske#
125074036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
125174036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
125274036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
125374036c4dSDevin Teske# title, backtitle, prompt, and [optionally] hline. The optimal height and
125474036c4dSDevin Teske# width for the described widget (not exceeding the actual terminal height or
125574036c4dSDevin Teske# width) is stored in $var_height and $var_width (respectively).
125674036c4dSDevin Teske#
125774036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
125874036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
1259ab2043b8SDevin Teske#
1260ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
1261ab2043b8SDevin Teske# dialog(1).
1262ab2043b8SDevin Teske#
1263ab2043b8SDevin Teskef_dialog_calendar_size()
1264ab2043b8SDevin Teske{
126574036c4dSDevin Teske	local __constrain=1
126674036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
126774036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
126874036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
1269ab2043b8SDevin Teske
127074036c4dSDevin Teske	# Return unless at least one size aspect has been requested
127174036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
127274036c4dSDevin Teske
127374036c4dSDevin Teske	#
127474036c4dSDevin Teske	# Obtain/Adjust minimum and maximum thresholds
127574036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
127674036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
127774036c4dSDevin Teske	#
127874036c4dSDevin Teske	local __max_height_cal_size __max_width_cal_size
127974036c4dSDevin Teske	f_dialog_max_size __max_height_cal_size __max_width_cal_size
128074036c4dSDevin Teske	__max_width_cal_size=$(( $__max_width_cal_size - 2 ))
128174036c4dSDevin Teske		# the calendar box will refuse to display if too wide
128274036c4dSDevin Teske	local __min_width
1283ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
128474036c4dSDevin Teske		__min_width=55
1285ab2043b8SDevin Teske	else
128674036c4dSDevin Teske		__min_width=40
128774036c4dSDevin Teske		__max_height_cal_size=$((
128874036c4dSDevin Teske			$__max_height_cal_size - $DIALOG_CALENDAR_HEIGHT ))
128974036c4dSDevin Teske		# When using dialog(1), we can't predict whether the user has
129074036c4dSDevin Teske		# disabled shadow's in their `$HOME/.dialogrc' file, so we'll
129174036c4dSDevin Teske		# subtract one for the potential shadow around the widget
129274036c4dSDevin Teske		__max_height_cal_size=$(( $__max_height_cal_size - 1 ))
1293ab2043b8SDevin Teske	fi
1294ab2043b8SDevin Teske
129574036c4dSDevin Teske	# Calculate height if desired
129674036c4dSDevin Teske	if [ "$__var_height" ]; then
129774036c4dSDevin Teske		local __height
129874036c4dSDevin Teske		__height=$( echo "$__prompt" | f_number_of_lines )
1299ab2043b8SDevin Teske
1300ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
130174036c4dSDevin Teske			# Add height to accomodate for embedded calendar widget
130274036c4dSDevin Teske			__height=$(( $__height + $DIALOG_CALENDAR_HEIGHT - 1 ))
1303ab2043b8SDevin Teske
1304ab2043b8SDevin Teske			# Also, bump height if backtitle is enabled
130574036c4dSDevin Teske			if [ "$__btitle" ]; then
130674036c4dSDevin Teske				local __n
130774036c4dSDevin Teske				__n=$( echo "$__btitle" | f_number_of_lines )
130874036c4dSDevin Teske				__height=$(( $__height + $__n + 2 ))
1309ab2043b8SDevin Teske			fi
1310ab2043b8SDevin Teske		else
131174036c4dSDevin Teske			[ "$__prompt" ] && __height=$(( $__height + 1 ))
1312ab2043b8SDevin Teske		fi
1313ab2043b8SDevin Teske
131474036c4dSDevin Teske		# Enforce maximum height, unless `-n' was passed
131574036c4dSDevin Teske		[ "$__constrain" -a $__height -gt $__max_height_cal_size ] &&
131674036c4dSDevin Teske			__height=$__max_height_cal_size
1317ab2043b8SDevin Teske
131874036c4dSDevin Teske		setvar "$__var_height" $__height
131974036c4dSDevin Teske	fi
132074036c4dSDevin Teske
132174036c4dSDevin Teske	# Calculate width if desired
132274036c4dSDevin Teske	if [ "$__var_width" ]; then
132374036c4dSDevin Teske		# NOTE: Function name appended to prevent __var_{height,width}
132474036c4dSDevin Teske		#       values from becoming local (and thus preventing setvar
132574036c4dSDevin Teske		#       from working).
132674036c4dSDevin Teske		local __width_cal_size
132774036c4dSDevin Teske		f_dialog_infobox_size -n "" __width_cal_size \
132874036c4dSDevin Teske			"$__title" "$__btitle" "$__prompt" "$__hline"
132974036c4dSDevin Teske
133074036c4dSDevin Teske		# Enforce minimum/maximum width, unless `-n' was passed
133174036c4dSDevin Teske		if [ "$__constrain" ]; then
133274036c4dSDevin Teske			if [ $__width_cal_size -lt $__min_width ]; then
133374036c4dSDevin Teske				__width_cal_size=$__min_width
133474036c4dSDevin Teske			elif [ $__width_cal_size -gt $__max_width_cal_size ]
133574036c4dSDevin Teske			then
133674036c4dSDevin Teske				__width_cal_size=$__max_width_size
133774036c4dSDevin Teske			fi
133874036c4dSDevin Teske		fi
133974036c4dSDevin Teske
134074036c4dSDevin Teske		setvar "$__var_width" $__width_cal_size
134174036c4dSDevin Teske	fi
134274036c4dSDevin Teske
134374036c4dSDevin Teske	return $SUCCESS
1344ab2043b8SDevin Teske}
1345ab2043b8SDevin Teske
134674036c4dSDevin Teske# f_dialog_timebox_size [-n] $var_height $var_width \
134774036c4dSDevin Teske#                       $title $backtitle $prompt [$hline]
1348ab2043b8SDevin Teske#
1349ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1350ab2043b8SDevin Teske# `--timebox' boxes sensibly.
1351ab2043b8SDevin Teske#
135274036c4dSDevin Teske# This function helps solve this issue by taking two sets of sequential
135374036c4dSDevin Teske# arguments. The first set of arguments are the variable names to use when
135474036c4dSDevin Teske# storing the calculated height and width. The second set of arguments are the
135574036c4dSDevin Teske# title, backtitle, prompt, and [optionally] hline. The optional height and
135674036c4dSDevin Teske# width for the described widget (not exceeding the actual terminal height or
135774036c4dSDevin Teske# width) is stored in $var_height and $var_width (respectively).
135874036c4dSDevin Teske#
135974036c4dSDevin Teske# If the first argument is `-n', the calculated sizes ($var_height and
136074036c4dSDevin Teske# $var_width) are not constrained to minimum/maximum values.
1361ab2043b8SDevin Teske#
1362ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
1363ab2043b8SDevin Teske# dialog(1).
1364ab2043b8SDevin Teske#
1365ab2043b8SDevin Teskef_dialog_timebox_size()
1366ab2043b8SDevin Teske{
136774036c4dSDevin Teske	local __constrain=1
136874036c4dSDevin Teske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
136974036c4dSDevin Teske	local __var_height="$1" __var_width="$2"
137074036c4dSDevin Teske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
1371ab2043b8SDevin Teske
137274036c4dSDevin Teske	# Return unless at least one size aspect has been requested
137374036c4dSDevin Teske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
1374ab2043b8SDevin Teske
1375ab2043b8SDevin Teske	#
137674036c4dSDevin Teske	# Obtain/Adjust minimum and maximum thresholds
137774036c4dSDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
137874036c4dSDevin Teske	#       from becoming local (and thus preventing setvar from working).
137974036c4dSDevin Teske	#
138074036c4dSDevin Teske	local __max_height_tbox_size __max_width_tbox_size
138174036c4dSDevin Teske	f_dialog_max_size __max_height_tbox_size __max_width_tbox_size
138274036c4dSDevin Teske	__max_width_tbox_size=$(( $__max_width_tbox_size - 2 ))
138374036c4dSDevin Teske		# the timebox widget refuses to display if too wide
138474036c4dSDevin Teske	local __min_width
138574036c4dSDevin Teske	if [ "$USE_XDIALOG" ]; then
138674036c4dSDevin Teske		__min_width=40
138774036c4dSDevin Teske	else
138874036c4dSDevin Teske		__min_width=20
138974036c4dSDevin Teske		__max_height_tbox_size=$(( \
139074036c4dSDevin Teske			$__max_height_tbox_size - $DIALOG_TIMEBOX_HEIGHT ))
139174036c4dSDevin Teske		# When using dialog(1), we can't predict whether the user has
139274036c4dSDevin Teske		# disabled shadow's in their `$HOME/.dialogrc' file, so we'll
139374036c4dSDevin Teske		# subtract one for the potential shadow around the widget
139474036c4dSDevin Teske		__max_height_tbox_size=$(( $__max_height_tbox_size - 1 ))
139574036c4dSDevin Teske	fi
139674036c4dSDevin Teske
139774036c4dSDevin Teske	# Calculate height if desired
139874036c4dSDevin Teske	if [ "$__var_height" -a "$USE_XDIALOG" ]; then
139974036c4dSDevin Teske		# When using Xdialog(1), the height seems to have
140074036c4dSDevin Teske		# no effect. All values provide the same results.
140174036c4dSDevin Teske		setvar "$__var_height" 0 # autosize
140274036c4dSDevin Teske	elif [ "$__var_height" ]; then
140374036c4dSDevin Teske		local __height
140474036c4dSDevin Teske		__height=$( echo "$__prompt" | f_number_of_lines )
140574036c4dSDevin Teske		__height=$(( $__height ${__prompt:++1} + 1 ))
140674036c4dSDevin Teske
140774036c4dSDevin Teske		# Enforce maximum height, unless `-n' was passed
140874036c4dSDevin Teske		[ "$__constrain" -a $__height -gt $__max_height_tbox_size ] &&
140974036c4dSDevin Teske			__height=$__max_height_tbox_size
141074036c4dSDevin Teske
141174036c4dSDevin Teske		setvar "$__var_height" $__height
141274036c4dSDevin Teske	fi
141374036c4dSDevin Teske
141474036c4dSDevin Teske	# Calculate width if desired
141574036c4dSDevin Teske	if [ "$__var_width" ]; then
141674036c4dSDevin Teske		# NOTE: Function name appended to prevent __var_{height,width}
141774036c4dSDevin Teske		#       values from becoming local (and thus preventing setvar
141874036c4dSDevin Teske		#       from working).
141974036c4dSDevin Teske		local __width_tbox_size
142074036c4dSDevin Teske		f_dialog_infobox_size -n "" __width_tbox_size \
142174036c4dSDevin Teske			"$__title" "$__btitle" "$__prompt" "$__hline"
142274036c4dSDevin Teske
1423ab2043b8SDevin Teske		# Enforce the minimum width for displaying the timebox
142474036c4dSDevin Teske		if [ "$__constrain" ]; then
142574036c4dSDevin Teske			if [ $__width_tbox_size -lt $__min_width ]; then
142674036c4dSDevin Teske				__width_tbox_size=$__min_width
142774036c4dSDevin Teske			elif [ $__width_tbox_size -ge $__max_width_tbox_size ]
142874036c4dSDevin Teske			then
142974036c4dSDevin Teske				__width_tbox_size=$__max_width_tbox_size
143074036c4dSDevin Teske			fi
1431ab2043b8SDevin Teske		fi
1432ab2043b8SDevin Teske
143374036c4dSDevin Teske		setvar "$__var_width" $__width_tbox_size
143474036c4dSDevin Teske	fi
1435ab2043b8SDevin Teske
143674036c4dSDevin Teske	return $SUCCESS
1437ab2043b8SDevin Teske}
1438ab2043b8SDevin Teske
1439ab2043b8SDevin Teske############################################################ CLEAR FUNCTIONS
1440ab2043b8SDevin Teske
1441ab2043b8SDevin Teske# f_dialog_clear
1442ab2043b8SDevin Teske#
1443ab2043b8SDevin Teske# Clears any/all previous dialog(1) displays.
1444ab2043b8SDevin Teske#
1445ab2043b8SDevin Teskef_dialog_clear()
1446ab2043b8SDevin Teske{
1447ab2043b8SDevin Teske	$DIALOG --clear
1448ab2043b8SDevin Teske}
1449ab2043b8SDevin Teske
1450ab2043b8SDevin Teske############################################################ INFO FUNCTIONS
1451ab2043b8SDevin Teske
1452ab2043b8SDevin Teske# f_dialog_info $info_text ...
1453ab2043b8SDevin Teske#
1454ab2043b8SDevin Teske# Throw up a dialog(1) infobox. The infobox remains until another dialog is
14555ffea47aSDevin Teske# displayed or `dialog --clear' (or f_dialog_clear) is called.
1456ab2043b8SDevin Teske#
1457ab2043b8SDevin Teskef_dialog_info()
1458ab2043b8SDevin Teske{
145974036c4dSDevin Teske	local info_text="$*" height width
146074036c4dSDevin Teske	f_dialog_infobox_size height width \
146174036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$info_text"
146274036c4dSDevin Teske	$DIALOG \
146374036c4dSDevin Teske		--title "$DIALOG_TITLE"         \
146474036c4dSDevin Teske		--backtitle "$DIALOG_BACKTITLE" \
1465ab2043b8SDevin Teske		${USE_XDIALOG:+--ignore-eof}    \
1466ab2043b8SDevin Teske		${USE_XDIALOG:+--no-buttons}    \
146774036c4dSDevin Teske		--infobox "$info_text" $height $width
1468ab2043b8SDevin Teske}
1469ab2043b8SDevin Teske
1470ab2043b8SDevin Teske# f_xdialog_info $info_text ...
1471ab2043b8SDevin Teske#
1472ab2043b8SDevin Teske# Throw up an Xdialog(1) infobox and do not dismiss it until stdin produces
1473ab2043b8SDevin Teske# EOF. This implies that you must execute this either as an rvalue to a pipe,
1474ab2043b8SDevin Teske# lvalue to indirection or in a sub-shell that provides data on stdin.
1475ab2043b8SDevin Teske#
1476ab2043b8SDevin Teskef_xdialog_info()
1477ab2043b8SDevin Teske{
147874036c4dSDevin Teske	local info_text="$*" height width
147974036c4dSDevin Teske	f_dialog_infobox_size height width \
148074036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$info_text"
148174036c4dSDevin Teske	$DIALOG \
148274036c4dSDevin Teske		--title "$DIALOG_TITLE"               \
148374036c4dSDevin Teske		--backtitle "$DIALOG_BACKTITLE"       \
1484ab2043b8SDevin Teske		--no-close --no-buttons               \
148574036c4dSDevin Teske		--infobox "$info_text" $height $width \
1486ab2043b8SDevin Teske		-1 # timeout of -1 means abort when EOF on stdin
1487ab2043b8SDevin Teske}
1488ab2043b8SDevin Teske
1489ab2043b8SDevin Teske############################################################ MSGBOX FUNCTIONS
1490ab2043b8SDevin Teske
1491ab2043b8SDevin Teske# f_dialog_msgbox $msg_text ...
1492ab2043b8SDevin Teske#
1493ab2043b8SDevin Teske# Throw up a dialog(1) msgbox. The msgbox remains until the user presses ENTER
1494ab2043b8SDevin Teske# or ESC, acknowledging the modal dialog.
1495ab2043b8SDevin Teske#
1496ab2043b8SDevin Teske# If the user presses ENTER, the exit status is zero (success), otherwise if
1497ab2043b8SDevin Teske# the user presses ESC the exit status is 255.
1498ab2043b8SDevin Teske#
1499ab2043b8SDevin Teskef_dialog_msgbox()
1500ab2043b8SDevin Teske{
150174036c4dSDevin Teske	local msg_text="$*" height width
150274036c4dSDevin Teske	f_dialog_buttonbox_size height width \
150374036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$msg_text"
150474036c4dSDevin Teske	$DIALOG \
150574036c4dSDevin Teske		--title "$DIALOG_TITLE"         \
150674036c4dSDevin Teske		--backtitle "$DIALOG_BACKTITLE" \
150774036c4dSDevin Teske		--ok-label "$msg_ok"            \
150874036c4dSDevin Teske		--msgbox "$msg_text" $height $width
1509ab2043b8SDevin Teske}
1510ab2043b8SDevin Teske
151140dfc82dSDevin Teske############################################################ TEXTBOX FUNCTIONS
151240dfc82dSDevin Teske
151340dfc82dSDevin Teske# f_dialog_textbox $file
151440dfc82dSDevin Teske#
151540dfc82dSDevin Teske# Display the contents of $file (or an error if $file does not exist, etc.) in
151640dfc82dSDevin Teske# a dialog(1) textbox (which has a scrollable region for the text). The textbox
151740dfc82dSDevin Teske# remains until the user presses ENTER or ESC, acknowledging the modal dialog.
151840dfc82dSDevin Teske#
151940dfc82dSDevin Teske# If the user presses ENTER, the exit status is zero (success), otherwise if
152040dfc82dSDevin Teske# the user presses ESC the exit status is 255.
152140dfc82dSDevin Teske#
152240dfc82dSDevin Teskef_dialog_textbox()
152340dfc82dSDevin Teske{
152440dfc82dSDevin Teske	local file="$1"
152574036c4dSDevin Teske	local contents height width retval
152640dfc82dSDevin Teske
152740dfc82dSDevin Teske	contents=$( cat "$file" 2>&1 )
152840dfc82dSDevin Teske	retval=$?
152940dfc82dSDevin Teske
153074036c4dSDevin Teske	f_dialog_buttonbox_size height width \
153174036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$contents"
153240dfc82dSDevin Teske
153340dfc82dSDevin Teske	if [ $retval -eq $SUCCESS ]; then
153474036c4dSDevin Teske		$DIALOG \
153574036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
153674036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
153774036c4dSDevin Teske			--exit-label "$msg_ok"          \
153840dfc82dSDevin Teske			--no-cancel                     \
153974036c4dSDevin Teske			--textbox "$file" $height $width
154040dfc82dSDevin Teske	else
154174036c4dSDevin Teske		$DIALOG \
154274036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
154374036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
154474036c4dSDevin Teske			--ok-label "$msg_ok"            \
154574036c4dSDevin Teske			--msgbox "$contents" $height $width
154640dfc82dSDevin Teske	fi
154740dfc82dSDevin Teske}
154840dfc82dSDevin Teske
1549ab2043b8SDevin Teske############################################################ YESNO FUNCTIONS
1550ab2043b8SDevin Teske
1551ab2043b8SDevin Teske# f_dialog_yesno $msg_text ...
1552ab2043b8SDevin Teske#
1553ab2043b8SDevin Teske# Display a dialog(1) Yes/No prompt to allow the user to make some decision.
1554ab2043b8SDevin Teske# The yesno prompt remains until the user presses ENTER or ESC, acknowledging
1555ab2043b8SDevin Teske# the modal dialog.
1556ab2043b8SDevin Teske#
1557ab2043b8SDevin Teske# If the user chooses YES the exit status is zero, or chooses NO the exit
1558ab2043b8SDevin Teske# status is one, or presses ESC the exit status is 255.
1559ab2043b8SDevin Teske#
1560ab2043b8SDevin Teskef_dialog_yesno()
1561ab2043b8SDevin Teske{
156274036c4dSDevin Teske	local msg_text="$*" height width
1563ab2043b8SDevin Teske	local hline="$hline_arrows_tab_enter"
15643da75d2eSDevin Teske
15653da75d2eSDevin Teske	f_interactive || return 0 # If non-interactive, return YES all the time
15663da75d2eSDevin Teske
156774036c4dSDevin Teske	f_dialog_buttonbox_size height width \
156874036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$msg_text" "$hline"
1569ab2043b8SDevin Teske
1570ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
157174036c4dSDevin Teske		$DIALOG \
157274036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
157374036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
157474036c4dSDevin Teske			--hline "$hline"                \
157574036c4dSDevin Teske			--ok-label "$msg_yes"           \
157674036c4dSDevin Teske			--cancel-label "$msg_no"        \
157774036c4dSDevin Teske			--yesno "$msg_text" $height $width
1578ab2043b8SDevin Teske	else
157974036c4dSDevin Teske		$DIALOG \
158074036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
158174036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
158274036c4dSDevin Teske			--hline "$hline"                \
158374036c4dSDevin Teske			--yes-label "$msg_yes"          \
158474036c4dSDevin Teske			--no-label "$msg_no"            \
158574036c4dSDevin Teske			--yesno "$msg_text" $height $width
1586ab2043b8SDevin Teske	fi
1587ab2043b8SDevin Teske}
1588ab2043b8SDevin Teske
1589ab2043b8SDevin Teske# f_dialog_noyes $msg_text ...
1590ab2043b8SDevin Teske#
1591ab2043b8SDevin Teske# Display a dialog(1) No/Yes prompt to allow the user to make some decision.
1592ab2043b8SDevin Teske# The noyes prompt remains until the user presses ENTER or ESC, acknowledging
1593ab2043b8SDevin Teske# the modal dialog.
1594ab2043b8SDevin Teske#
1595ab2043b8SDevin Teske# If the user chooses YES the exit status is zero, or chooses NO the exit
1596ab2043b8SDevin Teske# status is one, or presses ESC the exit status is 255.
1597ab2043b8SDevin Teske#
1598ab2043b8SDevin Teske# NOTE: This is just like the f_dialog_yesno function except "No" is default.
1599ab2043b8SDevin Teske#
1600ab2043b8SDevin Teskef_dialog_noyes()
1601ab2043b8SDevin Teske{
160274036c4dSDevin Teske	local msg_text="$*" height width
1603ab2043b8SDevin Teske	local hline="$hline_arrows_tab_enter"
16043da75d2eSDevin Teske
16053da75d2eSDevin Teske	f_interactive || return 1 # If non-interactive, return NO all the time
16063da75d2eSDevin Teske
160774036c4dSDevin Teske	f_dialog_buttonbox_size height width \
160874036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$msg_text" "$hline"
1609ab2043b8SDevin Teske
1610ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
161174036c4dSDevin Teske		$DIALOG \
161274036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
161374036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
161474036c4dSDevin Teske			--hline "$hline"                \
1615ab2043b8SDevin Teske			--default-no                    \
161674036c4dSDevin Teske			--ok-label "$msg_yes"           \
161774036c4dSDevin Teske			--cancel-label "$msg_no"        \
161874036c4dSDevin Teske			--yesno "$msg_text" $height $width
1619ab2043b8SDevin Teske	else
162074036c4dSDevin Teske		$DIALOG \
162174036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
162274036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
162374036c4dSDevin Teske			--hline "$hline"                \
1624ab2043b8SDevin Teske			--defaultno                     \
162574036c4dSDevin Teske			--yes-label "$msg_yes"          \
162674036c4dSDevin Teske			--no-label "$msg_no"            \
162774036c4dSDevin Teske			--yesno "$msg_text" $height $width
1628ab2043b8SDevin Teske	fi
1629ab2043b8SDevin Teske}
1630ab2043b8SDevin Teske
1631ab2043b8SDevin Teske############################################################ INPUT FUNCTIONS
1632ab2043b8SDevin Teske
1633*ec7120b5SDevin Teske# f_dialog_inputstr_store [-s] $text
1634*ec7120b5SDevin Teske#
1635*ec7120b5SDevin Teske# Store some text from a dialog(1) inputbox to be retrieved later by
1636*ec7120b5SDevin Teske# f_dialog_inputstr_fetch(). If the first argument is `-s', the text is
1637*ec7120b5SDevin Teske# sanitized before being stored.
1638*ec7120b5SDevin Teske#
1639*ec7120b5SDevin Teskef_dialog_inputstr_store()
1640*ec7120b5SDevin Teske{
1641*ec7120b5SDevin Teske	local sanitize=
1642*ec7120b5SDevin Teske	[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
1643*ec7120b5SDevin Teske	local text="$1"
1644*ec7120b5SDevin Teske
1645*ec7120b5SDevin Teske	# Sanitize the line before storing it if desired
1646*ec7120b5SDevin Teske	[ "$sanitize" ] && f_dialog_line_sanitize text
1647*ec7120b5SDevin Teske
1648*ec7120b5SDevin Teske	setvar DIALOG_INPUTBOX_$$ "$text"
1649*ec7120b5SDevin Teske}
1650*ec7120b5SDevin Teske
1651*ec7120b5SDevin Teske# f_dialog_inputstr_fetch [$var_to_set]
1652ab2043b8SDevin Teske#
1653ab2043b8SDevin Teske# Obtain the inputstr entered by the user from the most recently displayed
1654*ec7120b5SDevin Teske# dialog(1) inputbox (previously stored with f_dialog_inputstr_store() above).
1655*ec7120b5SDevin Teske# If $var_to_set is NULL or missing, output is printed to stdout (which is less
1656*ec7120b5SDevin Teske# recommended due to performance degradation; in a loop for example).
1657ab2043b8SDevin Teske#
1658*ec7120b5SDevin Teskef_dialog_inputstr_fetch()
1659ab2043b8SDevin Teske{
1660*ec7120b5SDevin Teske	local __var_to_set="$1" __cp
1661*ec7120b5SDevin Teske
1662*ec7120b5SDevin Teske	debug= f_getvar DIALOG_INPUTBOX_$$ "${__var_to_set:-__cp}" # get data
166389498fdfSDevin Teske	setvar DIALOG_INPUTBOX_$$ "" # scrub memory in case data was sensitive
1664*ec7120b5SDevin Teske
1665*ec7120b5SDevin Teske	# Return the line on standard-out if desired
1666*ec7120b5SDevin Teske	[ "$__var_to_set" ] || echo "$__cp"
1667*ec7120b5SDevin Teske
1668ab2043b8SDevin Teske	return $SUCCESS
1669ab2043b8SDevin Teske}
1670ab2043b8SDevin Teske
1671*ec7120b5SDevin Teske# f_dialog_input $var_to_set $prompt [$init [$hline]]
1672298cf604SDevin Teske#
1673298cf604SDevin Teske# Prompt the user with a dialog(1) inputbox to enter some value. The inputbox
1674298cf604SDevin Teske# remains until the the user presses ENTER or ESC, or otherwise ends the
1675298cf604SDevin Teske# editing session, by selecting `Cancel' for example.
1676298cf604SDevin Teske#
1677298cf604SDevin Teske# If the user presses ENTER, the exit status is zero (success), otherwise if
1678298cf604SDevin Teske# the user presses ESC the exit status is 255, or if the user chose Cancel, the
1679298cf604SDevin Teske# exit status is instead 1.
1680298cf604SDevin Teske#
1681298cf604SDevin Teske# NOTE: The hline should correspond to the type of data you want from the user.
1682298cf604SDevin Teske# NOTE: Should not be used to edit multiline values.
1683298cf604SDevin Teske#
1684298cf604SDevin Teskef_dialog_input()
1685298cf604SDevin Teske{
1686*ec7120b5SDevin Teske	local __var_to_set="$1" __prompt="$2" __init="$3" __hline="$4"
1687*ec7120b5SDevin Teske
1688*ec7120b5SDevin Teske	# NOTE: Function name appended to prevent __var_{height,width} values
1689*ec7120b5SDevin Teske	#       from becoming local (and thus preventing setvar from working).
1690*ec7120b5SDevin Teske	local __height_input __width_input
1691*ec7120b5SDevin Teske	f_dialog_inputbox_size __height_input __width_input \
169274036c4dSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" \
1693*ec7120b5SDevin Teske		"$__prompt" "$__init" "$__hline"
1694298cf604SDevin Teske
1695*ec7120b5SDevin Teske	local __opterm="--"
1696*ec7120b5SDevin Teske	[ "$USE_XDIALOG" ] && __opterm=
1697298cf604SDevin Teske
1698*ec7120b5SDevin Teske	local __dialog_input
1699*ec7120b5SDevin Teske	__dialog_input=$(
170074036c4dSDevin Teske		$DIALOG \
170174036c4dSDevin Teske			--title "$DIALOG_TITLE"         \
170274036c4dSDevin Teske			--backtitle "$DIALOG_BACKTITLE" \
1703*ec7120b5SDevin Teske			--hline "$__hline"              \
170474036c4dSDevin Teske			--ok-label "$msg_ok"            \
170574036c4dSDevin Teske			--cancel-label "$msg_cancel"    \
1706*ec7120b5SDevin Teske			--inputbox "$__prompt"          \
1707*ec7120b5SDevin Teske			$__height_input $__width_input  \
1708*ec7120b5SDevin Teske			$__opterm "$__init"             \
1709298cf604SDevin Teske			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1710298cf604SDevin Teske	)
1711*ec7120b5SDevin Teske	local __retval=$?
1712298cf604SDevin Teske
1713*ec7120b5SDevin Teske	# Remove warnings and leading/trailing whitespace from user input
1714*ec7120b5SDevin Teske	f_dialog_line_sanitize __dialog_input
1715298cf604SDevin Teske
1716*ec7120b5SDevin Teske	setvar "$__var_to_set" "$__dialog_input"
1717*ec7120b5SDevin Teske	return $__retval
1718298cf604SDevin Teske}
1719298cf604SDevin Teske
1720ab2043b8SDevin Teske############################################################ MENU FUNCTIONS
1721ab2043b8SDevin Teske
1722fd962ac6SDevin Teske# f_dialog_menutag_store [-s] $text
1723fd962ac6SDevin Teske#
1724fd962ac6SDevin Teske# Store some text from a dialog(1) menu to be retrieved later by
1725fd962ac6SDevin Teske# f_dialog_menutag_fetch(). If the first argument is `-s', the text is
1726fd962ac6SDevin Teske# sanitized before being stored.
1727fd962ac6SDevin Teske#
1728fd962ac6SDevin Teskef_dialog_menutag_store()
1729fd962ac6SDevin Teske{
1730fd962ac6SDevin Teske	local sanitize=
1731fd962ac6SDevin Teske	[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
1732fd962ac6SDevin Teske	local text="$1"
1733fd962ac6SDevin Teske
1734fd962ac6SDevin Teske	# Sanitize the menutag before storing it if desired
1735fd962ac6SDevin Teske	[ "$sanitize" ] && f_dialog_data_sanitize text
1736fd962ac6SDevin Teske
1737fd962ac6SDevin Teske	setvar DIALOG_MENU_$$ "$text"
1738fd962ac6SDevin Teske}
1739fd962ac6SDevin Teske
1740fd962ac6SDevin Teske# f_dialog_menutag_fetch [$var_to_set]
1741ab2043b8SDevin Teske#
1742ab2043b8SDevin Teske# Obtain the menutag chosen by the user from the most recently displayed
1743fd962ac6SDevin Teske# dialog(1) menu (previously stored with f_dialog_menutag_store() above). If
1744fd962ac6SDevin Teske# $var_to_set is NULL or missing, output is printed to stdout (which is less
1745fd962ac6SDevin Teske# recommended due to performance degradation; in a loop for example).
1746ab2043b8SDevin Teske#
1747fd962ac6SDevin Teskef_dialog_menutag_fetch()
1748ab2043b8SDevin Teske{
1749fd962ac6SDevin Teske	local __var_to_set="$1" __cp
1750fd962ac6SDevin Teske
1751fd962ac6SDevin Teske	debug= f_getvar DIALOG_MENU_$$ "${__var_to_set:-__cp}" # get the data
175289498fdfSDevin Teske	setvar DIALOG_MENU_$$ "" # scrub memory in case data was sensitive
1753fd962ac6SDevin Teske
1754fd962ac6SDevin Teske	# Return the data on standard-out if desired
1755fd962ac6SDevin Teske	[ "$__var_to_set" ] || echo "$__cp"
1756fd962ac6SDevin Teske
1757ab2043b8SDevin Teske	return $SUCCESS
1758ab2043b8SDevin Teske}
1759ab2043b8SDevin Teske
1760ab2043b8SDevin Teske# f_dialog_menutag2item $tag_chosen $tag1 $item1 $tag2 $item2 ...
1761ab2043b8SDevin Teske#
1762ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) you must pass an ordered list of
1763ab2043b8SDevin Teske# tag/item pairs on the command-line. When the user selects a menu option the
1764ab2043b8SDevin Teske# tag for that item is printed to stderr.
1765ab2043b8SDevin Teske#
1766ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into
1767ab2043b8SDevin Teske# the item associated with said tag.
1768ab2043b8SDevin Teske#
1769ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the
1770ab2043b8SDevin Teske# ordered list of tag/item pairs (HINT: use the same tag/item list as was
1771ab2043b8SDevin Teske# passed to dialog(1) for consistency).
1772ab2043b8SDevin Teske#
1773ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned.
1774ab2043b8SDevin Teske#
1775ab2043b8SDevin Teskef_dialog_menutag2item()
1776ab2043b8SDevin Teske{
1777ab2043b8SDevin Teske	local tag="$1" tagn item
1778ab2043b8SDevin Teske	shift 1 # tag
1779ab2043b8SDevin Teske
1780ab2043b8SDevin Teske	while [ $# -gt 0 ]; do
1781ab2043b8SDevin Teske		tagn="$1"
1782ab2043b8SDevin Teske		item="$2"
1783ab2043b8SDevin Teske		shift 2 # tagn/item
1784ab2043b8SDevin Teske
1785ab2043b8SDevin Teske		if [ "$tag" = "$tagn" ]; then
1786ab2043b8SDevin Teske			echo "$item"
1787ab2043b8SDevin Teske			return $SUCCESS
1788ab2043b8SDevin Teske		fi
1789ab2043b8SDevin Teske	done
1790ab2043b8SDevin Teske	return $FAILURE
1791ab2043b8SDevin Teske}
1792ab2043b8SDevin Teske
1793ab2043b8SDevin Teske# f_dialog_menutag2item_with_help $tag_chosen $tag1 $item1 $help1 \
1794ab2043b8SDevin Teske#                                             $tag2 $item2 $help2 ...
1795ab2043b8SDevin Teske#
1796ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) with the `--item-help' option, you
1797ab2043b8SDevin Teske# must pass an ordered list of tag/item/help triplets on the command-line. When
1798ab2043b8SDevin Teske# the user selects a menu option the tag for that item is printed to stderr.
1799ab2043b8SDevin Teske#
1800ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into
1801ab2043b8SDevin Teske# the item associated with said tag (help is discarded/ignored).
1802ab2043b8SDevin Teske#
1803ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the
1804ab2043b8SDevin Teske# ordered list of tag/item/help triplets (HINT: use the same tag/item/help list
1805ab2043b8SDevin Teske# as was passed to dialog(1) for consistency).
1806ab2043b8SDevin Teske#
1807ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned.
1808ab2043b8SDevin Teske#
1809ab2043b8SDevin Teskef_dialog_menutag2item_with_help()
1810ab2043b8SDevin Teske{
1811ab2043b8SDevin Teske	local tag="$1" tagn item
1812ab2043b8SDevin Teske	shift 1 # tag
1813ab2043b8SDevin Teske
1814ab2043b8SDevin Teske	while [ $# -gt 0 ]; do
1815ab2043b8SDevin Teske		tagn="$1"
1816ab2043b8SDevin Teske		item="$2"
1817ab2043b8SDevin Teske		shift 3 # tagn/item/help
1818ab2043b8SDevin Teske
1819ab2043b8SDevin Teske		if [ "$tag" = "$tagn" ]; then
1820ab2043b8SDevin Teske			echo "$item"
1821ab2043b8SDevin Teske			return $SUCCESS
1822ab2043b8SDevin Teske		fi
1823ab2043b8SDevin Teske	done
1824ab2043b8SDevin Teske	return $FAILURE
1825ab2043b8SDevin Teske}
1826ab2043b8SDevin Teske
1827ab2043b8SDevin Teske# f_dialog_menutag2index $tag_chosen $tag1 $item1 $tag2 $item2 ...
1828ab2043b8SDevin Teske#
1829ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) you must pass an ordered list of
1830ab2043b8SDevin Teske# tag/item pairs on the command-line. When the user selects a menu option the
1831ab2043b8SDevin Teske# tag for that item is printed to stderr.
1832ab2043b8SDevin Teske#
1833ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into
1834ab2043b8SDevin Teske# the index associated with said tag. The index is the one-based tag/item pair
1835ab2043b8SDevin Teske# array position within the ordered list of tag/item pairs passed to dialog(1).
1836ab2043b8SDevin Teske#
1837ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the
1838ab2043b8SDevin Teske# ordered list of tag/item pairs (HINT: use the same tag/item list as was
1839ab2043b8SDevin Teske# passed to dialog(1) for consistency).
1840ab2043b8SDevin Teske#
1841ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned.
1842ab2043b8SDevin Teske#
1843ab2043b8SDevin Teskef_dialog_menutag2index()
1844ab2043b8SDevin Teske{
1845ab2043b8SDevin Teske	local tag="$1" tagn n=1
1846ab2043b8SDevin Teske	shift 1 # tag
1847ab2043b8SDevin Teske
1848ab2043b8SDevin Teske	while [ $# -gt 0 ]; do
1849ab2043b8SDevin Teske		tagn="$1"
1850ab2043b8SDevin Teske		shift 2 # tagn/item
1851ab2043b8SDevin Teske
1852ab2043b8SDevin Teske		if [ "$tag" = "$tagn" ]; then
1853ab2043b8SDevin Teske			echo $n
1854ab2043b8SDevin Teske			return $SUCCESS
1855ab2043b8SDevin Teske		fi
1856ab2043b8SDevin Teske		n=$(( $n + 1 ))
1857ab2043b8SDevin Teske	done
1858ab2043b8SDevin Teske	return $FAILURE
1859ab2043b8SDevin Teske}
1860ab2043b8SDevin Teske
1861ab2043b8SDevin Teske# f_dialog_menutag2index_with_help $tag_chosen $tag1 $item1 $help1 \
1862ab2043b8SDevin Teske#                                              $tag2 $item2 $help2 ...
1863ab2043b8SDevin Teske#
1864ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) with the `--item-help' option, you
1865ab2043b8SDevin Teske# must pass an ordered list of tag/item/help triplets on the command-line. When
1866ab2043b8SDevin Teske# the user selects a menu option the tag for that item is printed to stderr.
1867ab2043b8SDevin Teske#
1868ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into
1869ab2043b8SDevin Teske# the index associated with said tag. The index is the one-based tag/item/help
1870ab2043b8SDevin Teske# triplet array position within the ordered list of tag/item/help triplets
1871ab2043b8SDevin Teske# passed to dialog(1).
1872ab2043b8SDevin Teske#
1873ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the
1874ab2043b8SDevin Teske# ordered list of tag/item/help triplets (HINT: use the same tag/item/help list
1875ab2043b8SDevin Teske# as was passed to dialog(1) for consistency).
1876ab2043b8SDevin Teske#
1877ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned.
1878ab2043b8SDevin Teske#
1879ab2043b8SDevin Teskef_dialog_menutag2index_with_help()
1880ab2043b8SDevin Teske{
1881ab2043b8SDevin Teske	local tag="$1" tagn n=1
1882ab2043b8SDevin Teske	shift 1 # tag
1883ab2043b8SDevin Teske
1884ab2043b8SDevin Teske	while [ $# -gt 0 ]; do
1885ab2043b8SDevin Teske		tagn="$1"
1886ab2043b8SDevin Teske		shift 3 # tagn/item/help
1887ab2043b8SDevin Teske
1888ab2043b8SDevin Teske		if [ "$tag" = "$tagn" ]; then
1889ab2043b8SDevin Teske			echo $n
1890ab2043b8SDevin Teske			return $SUCCESS
1891ab2043b8SDevin Teske		fi
1892ab2043b8SDevin Teske		n=$(( $n + 1 ))
1893ab2043b8SDevin Teske	done
1894ab2043b8SDevin Teske	return $FAILURE
1895ab2043b8SDevin Teske}
1896ab2043b8SDevin Teske
1897ab2043b8SDevin Teske############################################################ INIT FUNCTIONS
1898ab2043b8SDevin Teske
1899ab2043b8SDevin Teske# f_dialog_init
1900ab2043b8SDevin Teske#
1901ab2043b8SDevin Teske# Initialize (or re-initialize) the dialog module after setting/changing any
1902ab2043b8SDevin Teske# of the following environment variables:
1903ab2043b8SDevin Teske#
1904ab2043b8SDevin Teske# 	USE_XDIALOG   Either NULL or Non-NULL. If given a value will indicate
1905ab2043b8SDevin Teske# 	              that Xdialog(1) should be used instead of dialog(1).
1906ab2043b8SDevin Teske#
1907ab2043b8SDevin Teske# 	SECURE        Either NULL or Non-NULL. If given a value will indicate
1908ab2043b8SDevin Teske# 	              that (while running as root) sudo(8) authentication is
1909ab2043b8SDevin Teske# 	              required to proceed.
1910ab2043b8SDevin Teske#
1911ab2043b8SDevin Teskef_dialog_init()
1912ab2043b8SDevin Teske{
1913ab2043b8SDevin Teske	DIALOG_SELF_INITIALIZE=
1914ab2043b8SDevin Teske
1915ab2043b8SDevin Teske	#
191689498fdfSDevin Teske	# Clone terminal stdout so we can redirect to it from within sub-shells
191789498fdfSDevin Teske	#
191889498fdfSDevin Teske	eval exec $DIALOG_TERMINAL_PASSTHRU_FD\>\&1
191989498fdfSDevin Teske
192089498fdfSDevin Teske	#
1921c3755aa3SDevin Teske	# Add `-S' and `-X' to the list of standard arguments supported by all
1922c3755aa3SDevin Teske	#
1923c3755aa3SDevin Teske	case "$GETOPTS_STDARGS" in
1924c3755aa3SDevin Teske	*SX*) : good ;; # already present
1925c3755aa3SDevin Teske	   *) GETOPTS_STDARGS="${GETOPTS_STDARGS}SX"
1926c3755aa3SDevin Teske	esac
1927c3755aa3SDevin Teske
1928c3755aa3SDevin Teske	#
1929ab2043b8SDevin Teske	# Process stored command-line arguments
1930ab2043b8SDevin Teske	#
1931c3755aa3SDevin Teske	f_dprintf "f_dialog_init: ARGV=[%s] GETOPTS_STDARGS=[%s]" \
1932c3755aa3SDevin Teske	          "$ARGV" "$GETOPTS_STDARGS"
1933c3755aa3SDevin Teske	SECURE=$( set -- $ARGV
1934c3755aa3SDevin Teske		while getopts "$GETOPTS_STDARGS" flag > /dev/null; do
1935ab2043b8SDevin Teske			case "$flag" in
1936ab2043b8SDevin Teske			S) echo 1;;
1937ab2043b8SDevin Teske			\?) continue;;
1938ab2043b8SDevin Teske			esac
1939ab2043b8SDevin Teske		done
1940ab2043b8SDevin Teske	)
1941c3755aa3SDevin Teske	USE_XDIALOG=$( set -- $ARGV
1942c3755aa3SDevin Teske		while getopts $GETOPTS_STDARGS flag > /dev/null; do
1943ab2043b8SDevin Teske			case "$flag" in
1944ab2043b8SDevin Teske			S|X) echo 1;;
1945ab2043b8SDevin Teske			\?) continue;;
1946ab2043b8SDevin Teske			esac
1947ab2043b8SDevin Teske		done
1948ab2043b8SDevin Teske	)
1949c3755aa3SDevin Teske	f_dprintf "f_dialog_init: SECURE=[%s] USE_XDIALOG=[%s]" \
1950c3755aa3SDevin Teske	          "$SECURE" "$USE_XDIALOG"
1951ab2043b8SDevin Teske
1952ab2043b8SDevin Teske	#
1953ab2043b8SDevin Teske	# Process `-X' command-line option
1954ab2043b8SDevin Teske	#
1955ab2043b8SDevin Teske	[ "$USE_XDIALOG" ] && DIALOG=Xdialog
1956ab2043b8SDevin Teske
1957ab2043b8SDevin Teske	#
1958ab2043b8SDevin Teske	# Sanity check, or die gracefully
1959ab2043b8SDevin Teske	#
1960ab2043b8SDevin Teske	if ! f_have $DIALOG; then
1961ab2043b8SDevin Teske		unset USE_XDIALOG
1962ab2043b8SDevin Teske		failed_dialog="$DIALOG"
1963ab2043b8SDevin Teske		DIALOG=dialog
1964ab2043b8SDevin Teske		f_die 1 "$msg_no_such_file_or_directory" "$pgm" "$failed_dialog"
1965ab2043b8SDevin Teske	fi
1966ab2043b8SDevin Teske
1967ab2043b8SDevin Teske	#
1968ab2043b8SDevin Teske	# If we're already running as root but we got there by way of sudo(8)
1969ab2043b8SDevin Teske	# and we have X11, we should merge the xauth(1) credentials from our
1970ab2043b8SDevin Teske	# original user.
1971ab2043b8SDevin Teske	#
1972ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ] &&
1973ab2043b8SDevin Teske	   [ "$( id -u )" = "0" ] &&
1974ab2043b8SDevin Teske	   [ "$SUDO_USER" -a "$DISPLAY" ]
1975ab2043b8SDevin Teske	then
1976ab2043b8SDevin Teske		if ! f_have xauth; then
1977ab2043b8SDevin Teske			# Die gracefully, as we [likely] can't use Xdialog(1)
1978ab2043b8SDevin Teske			unset USE_XDIALOG
1979ab2043b8SDevin Teske			DIALOG=dialog
1980ab2043b8SDevin Teske			f_die 1 "$msg_no_such_file_or_directory" "$pgm" "xauth"
1981ab2043b8SDevin Teske		fi
1982ab2043b8SDevin Teske		HOSTNAME=$(hostname)
1983ab2043b8SDevin Teske		displaynum="${DISPLAY#*:}"
1984ab2043b8SDevin Teske		eval xauth -if \~$SUDO_USER/.Xauthority extract - \
1985ab2043b8SDevin Teske			\"\$HOSTNAME/unix:\$displaynum\" \
1986ab2043b8SDevin Teske			\"\$HOSTNAME:\$displaynum\" | sudo sh -c 'xauth -ivf \
1987fb7d723eSDevin Teske			~root/.Xauthority merge - > /dev/null 2>&1'
1988ab2043b8SDevin Teske	fi
1989ab2043b8SDevin Teske
1990ab2043b8SDevin Teske	#
1991ab2043b8SDevin Teske	# Probe Xdialog(1) for maximum height/width constraints, or die
1992ab2043b8SDevin Teske	# gracefully
1993ab2043b8SDevin Teske	#
1994ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
1995ab2043b8SDevin Teske		if ! maxsize=$( LANG= LC_ALL= $DIALOG --print-maxsize 2>&1 )
1996ab2043b8SDevin Teske		then
1997ab2043b8SDevin Teske			# Xdialog(1) failed, fall back to dialog(1)
1998ab2043b8SDevin Teske			unset USE_XDIALOG
199974036c4dSDevin Teske
200074036c4dSDevin Teske			# Display the error message produced by Xdialog(1)
200174036c4dSDevin Teske			local height width
200274036c4dSDevin Teske			f_dialog_buttonbox_size height width \
200374036c4dSDevin Teske				"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$maxsize"
200474036c4dSDevin Teske			dialog \
200574036c4dSDevin Teske				--title "$DIALOG_TITLE"         \
200674036c4dSDevin Teske				--backtitle "$DIALOG_BACKTITLE" \
200774036c4dSDevin Teske				--ok-label "$msg_ok"            \
200874036c4dSDevin Teske				--msgbox "$maxsize" $height $width
2009ab2043b8SDevin Teske			exit $FAILURE
2010ab2043b8SDevin Teske		fi
2011ab2043b8SDevin Teske
2012ab2043b8SDevin Teske		XDIALOG_MAXSIZE=$(
2013ab2043b8SDevin Teske			set -- ${maxsize##*:}
2014ab2043b8SDevin Teske
2015ab2043b8SDevin Teske			height=${1%,}
2016ab2043b8SDevin Teske			width=$2
2017ab2043b8SDevin Teske
2018ab2043b8SDevin Teske			echo $height $width
2019ab2043b8SDevin Teske		)
2020ab2043b8SDevin Teske		unset maxsize
2021ab2043b8SDevin Teske	fi
2022ab2043b8SDevin Teske
2023ab2043b8SDevin Teske	#
2024ab2043b8SDevin Teske	# If using Xdialog(1), swap DIALOG_TITLE with DIALOG_BACKTITLE.
2025ab2043b8SDevin Teske	# The reason for this is because many dialog(1) applications use
2026ab2043b8SDevin Teske	# --backtitle for the program name (which is better suited as
2027ab2043b8SDevin Teske	# --title with Xdialog(1)).
2028ab2043b8SDevin Teske	#
2029ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
2030ab2043b8SDevin Teske		_DIALOG_TITLE="$DIALOG_TITLE"
2031ab2043b8SDevin Teske		DIALOG_TITLE="$DIALOG_BACKTITLE"
2032ab2043b8SDevin Teske		DIALOG_BACKTITLE="$_DIALOG_TITLE"
2033ab2043b8SDevin Teske		unset _DIALOG_TITLE
2034ab2043b8SDevin Teske	fi
203556961fd7SDevin Teske
203656961fd7SDevin Teske	f_dprintf "f_dialog_init: dialog(1) API initialized."
2037ab2043b8SDevin Teske}
2038ab2043b8SDevin Teske
203979ec1cd5SDevin Teske############################################################ MAIN
204079ec1cd5SDevin Teske
20415ffea47aSDevin Teske#
20425ffea47aSDevin Teske# Self-initialize unless requested otherwise
20435ffea47aSDevin Teske#
204456961fd7SDevin Teskef_dprintf "%s: DIALOG_SELF_INITIALIZE=[%s]" \
204556961fd7SDevin Teske          dialog.subr "$DIALOG_SELF_INITIALIZE"
2046d3c2b8b6SDevin Teskecase "$DIALOG_SELF_INITIALIZE" in
2047d3c2b8b6SDevin Teske""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
2048d3c2b8b6SDevin Teske*) f_dialog_init
2049d3c2b8b6SDevin Teskeesac
205079ec1cd5SDevin Teske
205156961fd7SDevin Teskef_dprintf "%s: Successfully loaded." dialog.subr
205256961fd7SDevin Teske
2053ab2043b8SDevin Teskefi # ! $_DIALOG_SUBR
2054