1238438Sdteskeif [ ! "$_DIALOG_SUBR" ]; then _DIALOG_SUBR=1
2238438Sdteske#
3279619Sdteske# Copyright (c) 2006-2015 Devin Teske
4252980Sdteske# All rights reserved.
5238438Sdteske#
6238438Sdteske# Redistribution and use in source and binary forms, with or without
7238438Sdteske# modification, are permitted provided that the following conditions
8238438Sdteske# are met:
9238438Sdteske# 1. Redistributions of source code must retain the above copyright
10238438Sdteske#    notice, this list of conditions and the following disclaimer.
11238438Sdteske# 2. Redistributions in binary form must reproduce the above copyright
12238438Sdteske#    notice, this list of conditions and the following disclaimer in the
13238438Sdteske#    documentation and/or other materials provided with the distribution.
14238438Sdteske#
15238438Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16252987Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17238438Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18238438Sdteske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19238438Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20252987Sdteske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21238438Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22238438Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23238438Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24238438Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25238438Sdteske# SUCH DAMAGE.
26238438Sdteske#
27238438Sdteske# $FreeBSD: releng/10.3/usr.sbin/bsdconfig/share/dialog.subr 294898 2016-01-27 06:38:03Z dteske $
28238438Sdteske#
29238438Sdteske############################################################ INCLUDES
30238438Sdteske
31240684SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
32240684Sdteske. $BSDCFG_SHARE/common.subr || exit 1
33244675Sdteskef_dprintf "%s: loading includes..." dialog.subr
34240684Sdteskef_include $BSDCFG_SHARE/strings.subr
35245116Sdteskef_include $BSDCFG_SHARE/variable.subr
36240684Sdteske
37238438SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig"
38238438Sdteskef_include_lang $BSDCFG_LIBE/include/messages.subr
39238438Sdteske
40238438Sdteske############################################################ CONFIGURATION
41238438Sdteske
42238438Sdteske#
43240768Sdteske# Default file descriptor to link to stdout for dialog(1) passthru allowing
44240768Sdteske# execution of dialog from within a sub-shell (so-long as its standard output
45240768Sdteske# is explicitly redirected to this file descriptor).
46238438Sdteske#
47244550Sdteske: ${DIALOG_TERMINAL_PASSTHRU_FD:=${TERMINAL_STDOUT_PASSTHRU:-3}}
48238438Sdteske
49238438Sdteske############################################################ GLOBALS
50238438Sdteske
51238438Sdteske#
52238438Sdteske# Default name of dialog(1) utility
53238438Sdteske# NOTE: This is changed to "Xdialog" by the optional `-X' argument
54238438Sdteske#
55238438SdteskeDIALOG="dialog"
56238438Sdteske
57238438Sdteske#
58238438Sdteske# Default dialog(1) title and backtitle text
59238438Sdteske#
60238438SdteskeDIALOG_TITLE="$pgm"
61238438SdteskeDIALOG_BACKTITLE="bsdconfig"
62238438Sdteske
63238438Sdteske#
64238438Sdteske# Settings used while interacting with dialog(1)
65238438Sdteske#
66238438SdteskeDIALOG_MENU_TAGS="123456789ABCDEFGHIJKLMNOPQRSTUVWYZabcdefghijklmnopqrstuvwxyz"
67238438Sdteske
68238438Sdteske#
69238438Sdteske# Declare that we are fully-compliant with Xdialog(1) by unset'ing all
70238438Sdteske# compatibility settings.
71238438Sdteske#
72238438Sdteskeunset XDIALOG_HIGH_DIALOG_COMPAT
73238438Sdteskeunset XDIALOG_FORCE_AUTOSIZE
74238438Sdteskeunset XDIALOG_INFOBOX_TIMEOUT
75238438Sdteske
76238438Sdteske#
77256181Sdteske# Exit codes for [X]dialog(1)
78256181Sdteske#
79256181SdteskeDIALOG_OK=${SUCCESS:-0}
80256181SdteskeDIALOG_CANCEL=${FAILURE:-1}
81256181SdteskeDIALOG_HELP=2
82256181SdteskeDIALOG_ITEM_HELP=2
83256181SdteskeDIALOG_EXTRA=3
84256181SdteskeDIALOG_ITEM_HELP=4
85256181Sdteskeexport DIALOG_ERROR=254 # sh(1) can't handle the default of `-1'
86256181SdteskeDIALOG_ESC=255
87256181Sdteske
88256181Sdteske#
89243635Sdteske# Default behavior is to call f_dialog_init() automatically when loaded.
90238438Sdteske#
91238438Sdteske: ${DIALOG_SELF_INITIALIZE=1}
92238438Sdteske
93244801Sdteske#
94244801Sdteske# Default terminal size (used if/when running without a controlling terminal)
95244801Sdteske#
96244801Sdteske: ${DEFAULT_TERMINAL_SIZE:=24 80}
97244801Sdteske
98251190Sdteske#
99251190Sdteske# Minimum width(s) for various dialog(1) implementations (sensible global
100251190Sdteske# default(s) for all widgets of a given variant)
101251190Sdteske#
102251190Sdteske: ${DIALOG_MIN_WIDTH:=24}
103251190Sdteske: ${XDIALOG_MIN_WIDTH:=35}
104251190Sdteske
105251190Sdteske#
106251190Sdteske# When manually sizing Xdialog(1) widgets such as calendar and timebox, you'll
107251190Sdteske# need to know the size of the embedded GUI objects because the height passed
108251190Sdteske# to Xdialog(1) for these widgets has to be tall enough to accomodate them.
109251190Sdteske#
110251190Sdteske# These values are helpful when manually sizing with dialog(1) too, but in a
111251190Sdteske# different way. dialog(1) does not make you accomodate the custom items in the
112251190Sdteske# height (but does for width) -- a height of 3 will display three lines and a
113251190Sdteske# full calendar, for example (whereas Xdialog will truncate the calendar if
114251190Sdteske# given a height of 3). For dialog(1), use these values for making sure that
115251190Sdteske# the height does not exceed max_height (obtained by f_dialog_max_size()).
116251190Sdteske#
117251190SdteskeDIALOG_CALENDAR_HEIGHT=15
118251190SdteskeDIALOG_TIMEBOX_HEIGHT=6
119251190Sdteske
120238438Sdteske############################################################ GENERIC FUNCTIONS
121238438Sdteske
122251236Sdteske# f_dialog_data_sanitize $var_to_edit ...
123251236Sdteske#
124251236Sdteske# When using dialog(1) or Xdialog(1) sometimes unintended warnings or errors
125251236Sdteske# are generated from underlying libraries. For example, if $LANG is set to an
126251236Sdteske# invalid or unknown locale, the warnings from the Xdialog(1) libraries will
127251236Sdteske# clutter the output. This function helps by providing a centralied function
128251236Sdteske# that removes spurious warnings from the dialog(1) (or Xdialog(1)) response.
129251236Sdteske#
130251236Sdteske# Simply pass the name of one or more variables that need to be sanitized.
131251236Sdteske# After execution, the variables will hold their newly-sanitized data.
132251236Sdteske#
133251236Sdteskef_dialog_data_sanitize()
134251236Sdteske{
135251236Sdteske	if [ "$#" -eq 0 ]; then
136251236Sdteske		f_dprintf "%s: called with zero arguments" \
137251236Sdteske		          f_dialog_response_sanitize
138251236Sdteske		return $FAILURE
139251236Sdteske	fi
140251236Sdteske
141251236Sdteske	local __var_to_edit
142251236Sdteske	for __var_to_edit in $*; do
143251236Sdteske		# Skip warnings and trim leading/trailing whitespace
144251236Sdteske		setvar $__var_to_edit "$( f_getvar $__var_to_edit | awk '
145251236Sdteske			BEGIN { data = 0 }
146251236Sdteske			{
147251236Sdteske				if ( ! data )
148251236Sdteske				{
149251236Sdteske					if ( $0 ~ /^$/ ) next
150251236Sdteske					if ( $0 ~ /^Gdk-WARNING \*\*:/ ) next
151251236Sdteske					data = 1
152251236Sdteske				}
153251236Sdteske				print
154251236Sdteske			}
155251236Sdteske		' )"
156251236Sdteske	done
157251236Sdteske}
158251236Sdteske
159251242Sdteske# f_dialog_line_sanitize $var_to_edit ...
160251242Sdteske#
161251242Sdteske# When using dialog(1) or Xdialog(1) sometimes unintended warnings or errors
162251242Sdteske# are generated from underlying libraries. For example, if $LANG is set to an
163251242Sdteske# invalid or unknown locale, the warnings from the Xdialog(1) libraries will
164251242Sdteske# clutter the output. This function helps by providing a centralied function
165251242Sdteske# that removes spurious warnings from the dialog(1) (or Xdialog(1)) response.
166251242Sdteske#
167251242Sdteske# Simply pass the name of one or more variables that need to be sanitized.
168251242Sdteske# After execution, the variables will hold their newly-sanitized data.
169251242Sdteske#
170251242Sdteske# This function, unlike f_dialog_data_sanitize(), also removes leading/trailing
171251242Sdteske# whitespace from each line.
172251242Sdteske#
173251242Sdteskef_dialog_line_sanitize()
174251242Sdteske{
175251242Sdteske	if [ "$#" -eq 0 ]; then
176251242Sdteske		f_dprintf "%s: called with zero arguments" \
177251242Sdteske		          f_dialog_response_sanitize
178251242Sdteske		return $FAILURE
179251242Sdteske	fi
180251242Sdteske
181251242Sdteske	local __var_to_edit
182251242Sdteske	for __var_to_edit in $*; do
183251242Sdteske		# Skip warnings and trim leading/trailing whitespace
184251242Sdteske		setvar $__var_to_edit "$( f_getvar $__var_to_edit | awk '
185251242Sdteske			BEGIN { data = 0 }
186251242Sdteske			{
187251242Sdteske				if ( ! data )
188251242Sdteske				{
189251242Sdteske					if ( $0 ~ /^$/ ) next
190251242Sdteske					if ( $0 ~ /^Gdk-WARNING \*\*:/ ) next
191251242Sdteske					data = 1
192251242Sdteske				}
193251242Sdteske				sub(/^[[:space:]]*/, "")
194251242Sdteske				sub(/[[:space:]]*$/, "")
195251242Sdteske				print
196251242Sdteske			}
197251242Sdteske		' )"
198251242Sdteske	done
199251242Sdteske}
200251242Sdteske
201251236Sdteske############################################################ TITLE FUNCTIONS
202251236Sdteske
203238438Sdteske# f_dialog_title [$new_title]
204238438Sdteske#
205238438Sdteske# Set the title of future dialog(1) ($DIALOG_TITLE) or backtitle of Xdialog(1)
206238438Sdteske# ($DIALOG_BACKTITLE) invocations. If no arguments are given or the first
207238438Sdteske# argument is NULL, the current title is returned.
208238438Sdteske#
209238438Sdteske# Each time this function is called, a backup of the current values is made
210253121Sdteske# allowing a one-time (single-level) restoration of the previous title using
211253121Sdteske# the f_dialog_title_restore() function (below).
212238438Sdteske#
213238438Sdteskef_dialog_title()
214238438Sdteske{
215238438Sdteske	local new_title="$1"
216238438Sdteske
217243636Sdteske	if [ "${1+set}" ]; then
218238438Sdteske		if [ "$USE_XDIALOG" ]; then
219238438Sdteske			_DIALOG_BACKTITLE="$DIALOG_BACKTITLE"
220238438Sdteske			DIALOG_BACKTITLE="$new_title"
221238438Sdteske		else
222238438Sdteske			_DIALOG_TITLE="$DIALOG_TITLE"
223238438Sdteske			DIALOG_TITLE="$new_title"
224238438Sdteske		fi
225238438Sdteske	else
226238438Sdteske		if [ "$USE_XDIALOG" ]; then
227238438Sdteske			echo "$DIALOG_BACKTITLE"
228238438Sdteske		else
229238438Sdteske			echo "$DIALOG_TITLE"
230238438Sdteske		fi
231238438Sdteske	fi
232238438Sdteske}
233238438Sdteske
234238438Sdteske# f_dialog_title_restore
235238438Sdteske#
236238438Sdteske# Restore the previous title set by the last call to f_dialog_title().
237238438Sdteske# Restoration is non-recursive and only works to restore the most-recent title.
238238438Sdteske#
239238438Sdteskef_dialog_title_restore()
240238438Sdteske{
241238438Sdteske	if [ "$USE_XDIALOG" ]; then
242238438Sdteske		DIALOG_BACKTITLE="$_DIALOG_BACKTITLE"
243238438Sdteske	else
244238438Sdteske		DIALOG_TITLE="$_DIALOG_TITLE"
245238438Sdteske	fi
246238438Sdteske}
247238438Sdteske
248238438Sdteske# f_dialog_backtitle [$new_backtitle]
249238438Sdteske#
250238438Sdteske# Set the backtitle of future dialog(1) ($DIALOG_BACKTITLE) or title of
251238438Sdteske# Xdialog(1) ($DIALOG_TITLE) invocations. If no arguments are given or the
252238438Sdteske# first argument is NULL, the current backtitle is returned.
253238438Sdteske#
254238438Sdteskef_dialog_backtitle()
255238438Sdteske{
256238438Sdteske	local new_backtitle="$1"
257238438Sdteske
258243636Sdteske	if [ "${1+set}" ]; then
259238438Sdteske		if [ "$USE_XDIALOG" ]; then
260238438Sdteske			_DIALOG_TITLE="$DIALOG_TITLE"
261238438Sdteske			DIALOG_TITLE="$new_backtitle"
262238438Sdteske		else
263238438Sdteske			_DIALOG_BACKTITLE="$DIALOG_BACKTITLE"
264238438Sdteske			DIALOG_BACKTITLE="$new_backtitle"
265238438Sdteske		fi
266238438Sdteske	else
267238438Sdteske		if [ "$USE_XDIALOG" ]; then
268238438Sdteske			echo "$DIALOG_TITLE"
269238438Sdteske		else
270238438Sdteske			echo "$DIALOG_BACKTITLE"
271238438Sdteske		fi
272238438Sdteske	fi
273238438Sdteske}
274238438Sdteske
275238438Sdteske# f_dialog_backtitle_restore
276238438Sdteske#
277238438Sdteske# Restore the previous backtitle set by the last call to f_dialog_backtitle().
278238438Sdteske# Restoration is non-recursive and only works to restore the most-recent
279238438Sdteske# backtitle.
280238438Sdteske#
281238438Sdteskef_dialog_backtitle_restore()
282238438Sdteske{
283238438Sdteske	if [ "$USE_XDIALOG" ]; then
284238438Sdteske		DIALOG_TITLE="$_DIALOG_TITLE"
285238438Sdteske	else
286238438Sdteske		DIALOG_BACKTITLE="$_DIALOG_BACKTITLE"
287238438Sdteske	fi
288238438Sdteske}
289238438Sdteske
290238438Sdteske############################################################ SIZE FUNCTIONS
291238438Sdteske
292251190Sdteske# f_dialog_max_size $var_height $var_width
293238438Sdteske#
294251190Sdteske# Get the maximum height and width for a dialog widget and store the values in
295251190Sdteske# $var_height and $var_width (respectively).
296238438Sdteske#
297251190Sdteskef_dialog_max_size()
298251190Sdteske{
299260678Sdteske	local funcname=f_dialog_max_size
300251190Sdteske	local __var_height="$1" __var_width="$2" __max_size
301251190Sdteske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
302251190Sdteske	if [ "$USE_XDIALOG" ]; then
303251190Sdteske		__max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION
304251190Sdteske	else
305258420Sdteske		if __max_size=$( $DIALOG --print-maxsize \
306258420Sdteske			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
307258420Sdteske		then
308260678Sdteske			f_dprintf "$funcname: %s --print-maxsize = [%s]" \
309260678Sdteske			          "$DIALOG" "$__max_size"
310258420Sdteske			# usually "MaxSize: 24, 80"
311258420Sdteske			__max_size="${__max_size#*: }"
312258420Sdteske			f_replaceall "$__max_size" "," "" __max_size
313258420Sdteske		else
314260678Sdteske			f_eval_catch -dk __max_size $funcname stty \
315260678Sdteske				'stty size' || __max_size=
316258420Sdteske			# usually "24 80"
317258420Sdteske		fi
318251190Sdteske		: ${__max_size:=$DEFAULT_TERMINAL_SIZE}
319251190Sdteske	fi
320253117Sdteske	if [ "$__var_height" ]; then
321253117Sdteske		local __height="${__max_size%%[$IFS]*}"
322253117Sdteske		#
323253117Sdteske		# If we're not using Xdialog(1), we should assume that $DIALOG
324253117Sdteske		# will render --backtitle behind the widget. In such a case, we
325253121Sdteske		# should prevent a widget from obscuring the backtitle (unless
326253117Sdteske		# $NO_BACKTITLE is set and non-NULL, allowing a trap-door).
327253117Sdteske		#
328253117Sdteske		if [ ! "$USE_XDIALOG" ] && [ ! "$NO_BACKTITLE" ]; then
329253117Sdteske			#
330253117Sdteske			# If use_shadow (in ~/.dialogrc) is OFF, we need to
331253117Sdteske			# subtract 4, otherwise 5. However, don't check this
332253117Sdteske			# every time, rely on an initialization variable set
333253117Sdteske			# by f_dialog_init().
334253117Sdteske			#
335253117Sdteske			local __adjust=5
336253117Sdteske			[ "$NO_SHADOW" ] && __adjust=4
337253117Sdteske
338253121Sdteske			# Don't adjust height if already too small (allowing
339253117Sdteske			# obscured backtitle for small values of __height).
340253117Sdteske			[ ${__height:-0} -gt 11 ] &&
341253117Sdteske				__height=$(( $__height - $__adjust ))
342253117Sdteske		fi
343253117Sdteske		setvar "$__var_height" "$__height"
344253117Sdteske	fi
345251190Sdteske	[ "$__var_width" ] && setvar "$__var_width" "${__max_size##*[$IFS]}"
346251190Sdteske}
347251190Sdteske
348251190Sdteske# f_dialog_size_constrain $var_height $var_width [$min_height [$min_width]]
349238438Sdteske#
350251190Sdteske# Modify $var_height to be no-less-than $min_height (if given; zero otherwise)
351251190Sdteske# and no-greater-than terminal height (or screen height if $USE_XDIALOG is
352251190Sdteske# set).
353238438Sdteske#
354251190Sdteske# Also modify $var_width to be no-less-than $XDIALOG_MIN_WIDTH (or
355251190Sdteske# $XDIALOG_MIN_WIDTH if $_USE_XDIALOG is set) and no-greater-than terminal
356251190Sdteske# or screen width. The use of $[X]DIALOG_MIN_WIDTH can be overridden by
357251190Sdteske# passing $min_width.
358238438Sdteske#
359251190Sdteske# Return status is success unless one of the passed arguments is invalid
360251190Sdteske# or all of the $var_* arguments are either NULL or missing.
361251190Sdteske#
362251190Sdteskef_dialog_size_constrain()
363238438Sdteske{
364251190Sdteske	local __var_height="$1" __var_width="$2"
365251190Sdteske	local __min_height="$3" __min_width="$4"
366251190Sdteske	local __retval=$SUCCESS
367238438Sdteske
368251190Sdteske	# Return failure unless at least one var_* argument is passed
369251190Sdteske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
370238438Sdteske
371238438Sdteske	#
372251190Sdteske	# Print debug warnings if any given (non-NULL) argument are invalid
373251190Sdteske	# NOTE: Don't change the name of $__{var,min,}{height,width}
374238438Sdteske	#
375251190Sdteske	local __height __width
376251190Sdteske	local __arg __cp __fname=f_dialog_size_constrain 
377251190Sdteske	for __arg in height width; do
378251190Sdteske		debug= f_getvar __var_$__arg __cp
379251190Sdteske		[ "$__cp" ] || continue
380251353Sdteske		if ! debug= f_getvar "$__cp" __$__arg; then
381251190Sdteske			f_dprintf "%s: var_%s variable \`%s' not set" \
382251190Sdteske			          $__fname $__arg "$__cp"
383251190Sdteske			__retval=$FAILURE
384251190Sdteske		elif ! eval f_isinteger \$__$__arg; then
385251190Sdteske			f_dprintf "%s: var_%s variable value not a number" \
386251190Sdteske			          $__fname $__arg
387251190Sdteske			__retval=$FAILURE
388251190Sdteske		fi
389251190Sdteske	done
390251190Sdteske	for __arg in height width; do
391251190Sdteske		debug= f_getvar __min_$__arg __cp
392251190Sdteske		[ "$__cp" ] || continue
393251190Sdteske		f_isinteger "$__cp" && continue
394251190Sdteske		f_dprintf "%s: min_%s value not a number" $__fname $__arg
395251190Sdteske		__retval=$FAILURE
396251190Sdteske		setvar __min_$__arg ""
397251190Sdteske	done
398238438Sdteske
399251190Sdteske	# Obtain maximum height and width values
400251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
401251190Sdteske	#       from becoming local (and thus preventing setvar from working).
402251190Sdteske	local __max_height_size_constain __max_width_size_constrain
403251190Sdteske	f_dialog_max_size \
404251190Sdteske		__max_height_size_constrain __max_width_size_constrain
405251190Sdteske
406251190Sdteske	# Adjust height if desired
407251190Sdteske	if [ "$__var_height" ]; then
408251190Sdteske		if [ $__height -lt ${__min_height:-0} ]; then
409251190Sdteske			setvar "$__var_height" $__min_height
410251190Sdteske		elif [ $__height -gt $__max_height_size_constrain ]; then
411251190Sdteske			setvar "$__var_height" $__max_height_size_constrain
412251190Sdteske		fi
413251190Sdteske	fi
414251190Sdteske
415251190Sdteske	# Adjust width if desired
416251190Sdteske	if [ "$__var_width" ]; then
417251190Sdteske		if [ "$USE_XDIALOG" ]; then
418251190Sdteske			: ${__min_width:=${XDIALOG_MIN_WIDTH:-35}}
419238438Sdteske		else
420251190Sdteske			: ${__min_width:=${DIALOG_MIN_WIDTH:-24}}
421238438Sdteske		fi
422251190Sdteske		if [ $__width -lt $__min_width ]; then
423251190Sdteske			setvar "$__var_width" $__min_width
424251190Sdteske		elif [ $__width -gt $__max_width_size_constrain ]; then
425251190Sdteske			setvar "$__var_width" $__max_width_size_constrain
426251190Sdteske		fi
427238438Sdteske	fi
428238438Sdteske
429251353Sdteske	if [ "$debug" ]; then
430252075Sdteske		# Print final constrained values to debugging
431256361Sdteske		[ "$__var_height" ] && f_quietly f_getvar "$__var_height"
432256361Sdteske		[ "$__var_width"  ] && f_quietly f_getvar "$__var_width"
433251353Sdteske	fi
434251353Sdteske
435251190Sdteske	return $__retval # success if no debug warnings were printed
436251190Sdteske}
437251190Sdteske
438251190Sdteske# f_dialog_menu_constrain $var_height $var_width $var_rows "$prompt" \
439251190Sdteske#                         [$min_height [$min_width [$min_rows]]]
440251190Sdteske#
441251190Sdteske# Modify $var_height to be no-less-than $min_height (if given; zero otherwise)
442251190Sdteske# and no-greater-than terminal height (or screen height if $USE_XDIALOG is
443251190Sdteske# set).
444251190Sdteske#
445251190Sdteske# Also modify $var_width to be no-less-than $XDIALOG_MIN_WIDTH (or
446251190Sdteske# $XDIALOG_MIN_WIDTH if $_USE_XDIALOG is set) and no-greater-than terminal
447251190Sdteske# or screen width. The use of $[X]DIALOG_MIN_WIDTH can be overridden by
448251190Sdteske# passing $min_width.
449251190Sdteske#
450251190Sdteske# Last, modify $var_rows to be no-less-than $min_rows (if specified; zero
451251190Sdteske# otherwise) and no-greater-than (max_height - 8) where max_height is the
452251190Sdteske# terminal height (or screen height if $USE_XDIALOG is set). If $prompt is NULL
453251190Sdteske# or missing, dialog(1) allows $var_rows to be (max_height - 7), maximizing the
454251190Sdteske# number of visible rows.
455251190Sdteske#
456251190Sdteske# Return status is success unless one of the passed arguments is invalid
457251190Sdteske# or all of the $var_* arguments are either NULL or missing.
458251190Sdteske#
459251190Sdteskef_dialog_menu_constrain()
460251190Sdteske{
461251190Sdteske	local __var_height="$1" __var_width="$2" __var_rows="$3" __prompt="$4"
462251190Sdteske	local __min_height="$5" __min_width="$6" __min_rows="$7"
463251190Sdteske
464251190Sdteske	# Return failure unless at least one var_* argument is passed
465251190Sdteske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
466251190Sdteske		return $FAILURE
467251190Sdteske
468238438Sdteske	#
469251190Sdteske	# Print debug warnings if any given (non-NULL) argument are invalid
470251190Sdteske	# NOTE: Don't change the name of $__{var,min,}{height,width,rows}
471238438Sdteske	#
472263980Sdteske	local __height_menu_constrain __width_menu_constrain
473263980Sdteske	local __rows_menu_constrain
474251190Sdteske	local __arg __cp __fname=f_dialog_menu_constrain 
475251190Sdteske	for __arg in height width rows; do
476251190Sdteske		debug= f_getvar __var_$__arg __cp
477251190Sdteske		[ "$__cp" ] || continue
478263980Sdteske		if ! debug= f_getvar "$__cp" __${__arg}_menu_constrain; then
479251190Sdteske			f_dprintf "%s: var_%s variable \`%s' not set" \
480251190Sdteske			          $__fname $__arg "$__cp"
481251190Sdteske			__retval=$FAILURE
482263980Sdteske		elif ! eval f_isinteger \$__${__arg}_menu_constrain; then
483251190Sdteske			f_dprintf "%s: var_%s variable value not a number" \
484251190Sdteske			          $__fname $__arg
485251190Sdteske			__retval=$FAILURE
486238438Sdteske		fi
487251190Sdteske	done
488251190Sdteske	for __arg in height width rows; do
489251190Sdteske		debug= f_getvar __min_$__arg __cp
490251190Sdteske		[ "$__cp" ] || continue
491251190Sdteske		f_isinteger "$__cp" && continue
492251190Sdteske		f_dprintf "%s: min_%s value not a number" $__fname $__arg
493251190Sdteske		__retval=$FAILURE
494251190Sdteske		setvar __min_$__arg ""
495251190Sdteske	done
496251190Sdteske
497251190Sdteske	# Obtain maximum height and width values
498251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
499251190Sdteske	#       from becoming local (and thus preventing setvar from working).
500251190Sdteske	local __max_height_menu_constrain __max_width_menu_constrain
501251190Sdteske	f_dialog_max_size \
502251190Sdteske		__max_height_menu_constrain __max_width_menu_constrain
503251190Sdteske
504251190Sdteske	# Adjust height if desired
505251190Sdteske	if [ "$__var_height" ]; then
506263980Sdteske		if [ $__height_menu_constrain -lt ${__min_height:-0} ]; then
507251190Sdteske			setvar "$__var_height" $__min_height
508263980Sdteske		elif [ $__height_menu_constrain -gt \
509263980Sdteske		       $__max_height_menu_constrain ]
510263980Sdteske		then
511251190Sdteske			setvar "$__var_height" $__max_height_menu_constrain
512251190Sdteske		fi
513238438Sdteske	fi
514238438Sdteske
515251190Sdteske	# Adjust width if desired
516251190Sdteske	if [ "$__var_width" ]; then
517251190Sdteske		if [ "$USE_XDIALOG" ]; then
518251190Sdteske			: ${__min_width:=${XDIALOG_MIN_WIDTH:-35}}
519251190Sdteske		else
520251190Sdteske			: ${__min_width:=${DIALOG_MIN_WIDTH:-24}}
521251190Sdteske		fi
522263980Sdteske		if [ $__width_menu_constrain -lt $__min_width ]; then
523251190Sdteske			setvar "$__var_width" $__min_width
524263980Sdteske		elif [ $__width_menu_constrain -gt \
525263980Sdteske		       $__max_width_menu_constrain ]
526263980Sdteske		then
527251190Sdteske			setvar "$__var_width" $__max_width_menu_constrain
528251190Sdteske		fi
529251190Sdteske	fi
530238438Sdteske
531251190Sdteske	# Adjust rows if desired
532251190Sdteske	if [ "$__var_rows" ]; then
533251190Sdteske		if [ "$USE_XDIALOG" ]; then
534251190Sdteske			: ${__min_rows:=1}
535251190Sdteske		else
536251190Sdteske			: ${__min_rows:=0}
537251190Sdteske		fi
538238438Sdteske
539263980Sdteske		local __max_rows_menu_constrain=$((
540263980Sdteske			$__max_height_menu_constrain - 7
541263980Sdteske		))
542251190Sdteske		# If prompt_len is zero (no prompt), bump the max-rows by 1
543251190Sdteske		# Default assumption is (if no argument) that there's no prompt
544263980Sdteske		[ ${__prompt_len:-0} -gt 0 ] || __max_rows_menu_constrain=$((
545263980Sdteske			$__max_rows_menu_constrain + 1
546263980Sdteske		))
547251190Sdteske
548263980Sdteske		if [ $__rows_menu_constrain -lt $__min_rows ]; then
549251190Sdteske			setvar "$__var_rows" $__min_rows
550263980Sdteske		elif [ $__rows_menu_constrain -gt $__max_rows_menu_constrain ]
551263980Sdteske		then
552263980Sdteske			setvar "$__var_rows" $__max_rows_menu_constrain
553238438Sdteske		fi
554238438Sdteske	fi
555238438Sdteske
556251353Sdteske	if [ "$debug" ]; then
557256361Sdteske		# Print final constrained values to debugging
558256361Sdteske		[ "$__var_height" ] && f_quietly f_getvar "$__var_height"
559256361Sdteske		[ "$__var_width"  ] && f_quietly f_getvar "$__var_width"
560256361Sdteske		[ "$__var_rows"   ] && f_quietly f_getvar "$__var_rows"
561251353Sdteske	fi
562251353Sdteske
563251190Sdteske	return $__retval # success if no debug warnings were printed
564251190Sdteske}
565251190Sdteske
566251190Sdteske# f_dialog_infobox_size [-n] $var_height $var_width \
567251190Sdteske#                       $title $backtitle $prompt [$hline]
568251190Sdteske#
569251190Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
570251190Sdteske# `--infobox' boxes sensibly.
571251190Sdteske#
572251190Sdteske# This function helps solve this issue by taking two sets of sequential
573251190Sdteske# arguments. The first set of arguments are the variable names to use when
574251190Sdteske# storing the calculated height and width. The second set of arguments are the
575251190Sdteske# title, backtitle, prompt, and [optionally] hline. The optimal height and
576251190Sdteske# width for the described widget (not exceeding the actual terminal height or
577251190Sdteske# width) is stored in $var_height and $var_width (respectively).
578251190Sdteske#
579251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height and
580251190Sdteske# $var_width) are not constrained to minimum/maximum values.
581251190Sdteske#
582251190Sdteske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
583251190Sdteske# dialog(1).
584251190Sdteske#
585251190Sdteskef_dialog_infobox_size()
586251190Sdteske{
587251190Sdteske	local __constrain=1
588251190Sdteske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
589251190Sdteske	local __var_height="$1" __var_width="$2"
590251190Sdteske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
591251190Sdteske
592251190Sdteske	# Return unless at least one size aspect has been requested
593251190Sdteske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
594251190Sdteske
595251190Sdteske	# Default height/width of zero for auto-sizing
596251190Sdteske	local __height=0 __width=0 __n
597251190Sdteske
598251190Sdteske	# Adjust height if desired
599251190Sdteske	if [ "$__var_height" ]; then
600251190Sdteske		#
601251190Sdteske		# Set height based on number of rows in prompt
602251190Sdteske		#
603251190Sdteske		__n=$( echo -n "$__prompt" | f_number_of_lines )
604251190Sdteske		__n=$(( $__n + 2 ))
605251190Sdteske		[ $__n -gt $__height ] && __height=$__n
606251190Sdteske
607251190Sdteske		#
608251190Sdteske		# For Xdialog(1) bump height if backtitle is enabled (displayed
609251190Sdteske		# in the X11 window with a separator line between the backtitle
610251190Sdteske		# and msg text).
611251190Sdteske		#
612251190Sdteske		if [ "$USE_XDIALOG" -a "$__btitle" ]; then
613251190Sdteske			__n=$( echo "$__btitle" | f_number_of_lines )
614251190Sdteske			__height=$(( $__height + $__n + 2 ))
615238438Sdteske		fi
616251190Sdteske
617251190Sdteske		setvar "$__var_height" $__height
618238438Sdteske	fi
619238438Sdteske
620251190Sdteske	# Adjust width if desired
621251190Sdteske	if [ "$__var_width" ]; then
622251190Sdteske		#
623251190Sdteske		# Bump width for long titles
624251190Sdteske		#
625251190Sdteske		__n=$(( ${#__title} + 4 ))
626251190Sdteske		[ $__n -gt $__width ] && __width=$__n
627238438Sdteske
628251190Sdteske		#
629251190Sdteske		# If using Xdialog(1), bump width for long backtitles (which
630251190Sdteske		# appear within the window).
631251190Sdteske		#
632251190Sdteske		if [ "$USE_XDIALOG" ]; then
633251190Sdteske			__n=$(( ${#__btitle} + 4 ))
634251190Sdteske			[ $__n -gt $__width ] && __width=$__n
635251190Sdteske		fi
636251190Sdteske
637251190Sdteske		#
638251190Sdteske		# Bump width for long prompts
639251190Sdteske		#
640251190Sdteske		__n=$( echo "$__prompt" | f_longest_line_length )
641251190Sdteske		__n=$(( $__n + 4 )) # add width for border
642251190Sdteske		[ $__n -gt $__width ] && __width=$__n
643251190Sdteske
644251190Sdteske		#
645251190Sdteske		# Bump width for long hlines. Xdialog(1) supports `--hline' but
646251190Sdteske		# it's currently not used (so don't do anything here if using
647251190Sdteske		# Xdialog(1)).
648251190Sdteske		#
649251190Sdteske		if [ ! "$USE_XDIALOG" ]; then
650251190Sdteske			__n=$(( ${#__hline} + 10 ))
651251190Sdteske			[ $__n -gt $__width ] && __width=$__n
652251190Sdteske		fi
653251190Sdteske
654251190Sdteske		# Bump width by 16.6% if using Xdialog(1)
655251190Sdteske		[ "$USE_XDIALOG" ] && __width=$(( $__width + $__width / 6 ))
656251190Sdteske
657251190Sdteske		setvar "$__var_width" $__width
658238438Sdteske	fi
659243476Sdteske
660251190Sdteske	# Constrain values to sensible minimums/maximums unless `-n' was passed
661251190Sdteske	# Return success if no-constrain, else return status from constrain
662251190Sdteske	[ ! "$__constrain" ] ||
663251190Sdteske		f_dialog_size_constrain "$__var_height" "$__var_width"
664238438Sdteske}
665238438Sdteske
666251190Sdteske# f_dialog_buttonbox_size [-n] $var_height $var_width \
667251190Sdteske#                         $title $backtitle $prompt [$hline]
668238438Sdteske#
669238438Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
670238438Sdteske# `--msgbox' and `--yesno' boxes sensibly.
671238438Sdteske#
672251190Sdteske# This function helps solve this issue by taking two sets of sequential
673251190Sdteske# arguments. The first set of arguments are the variable names to use when
674251190Sdteske# storing the calculated height and width. The second set of arguments are the
675251190Sdteske# title, backtitle, prompt, and [optionally] hline. The optimal height and
676251190Sdteske# width for the described widget (not exceeding the actual terminal height or
677251190Sdteske# width) is stored in $var_height and $var_width (respectively).
678238438Sdteske#
679251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height and
680251190Sdteske# $var_width) are not constrained to minimum/maximum values.
681251190Sdteske#
682238438Sdteske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
683238438Sdteske# dialog(1).
684238438Sdteske#
685238438Sdteskef_dialog_buttonbox_size()
686238438Sdteske{
687251190Sdteske	local __constrain=1
688251190Sdteske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
689251190Sdteske	local __var_height="$1" __var_width="$2"
690251190Sdteske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
691238438Sdteske
692251190Sdteske	# Return unless at least one size aspect has been requested
693251190Sdteske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
694238438Sdteske
695251190Sdteske	# Calculate height/width of infobox (adjusted/constrained below)
696251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
697251190Sdteske	#       from becoming local (and thus preventing setvar from working).
698251190Sdteske	local __height_bbox_size __width_bbox_size
699251190Sdteske	f_dialog_infobox_size -n \
700251190Sdteske		"${__var_height:+__height_bbox_size}" \
701251190Sdteske		"${__var_width:+__width_bbox_size}" \
702251190Sdteske		"$__title" "$__btitle" "$__prompt" "$__hline"
703238438Sdteske
704251190Sdteske	# Adjust height if desired
705251190Sdteske	if [ "$__var_height" ]; then
706251190Sdteske		# Add height to accomodate the buttons
707251190Sdteske		__height_bbox_size=$(( $__height_bbox_size + 2 ))
708251190Sdteske
709251190Sdteske		# Adjust for clipping with Xdialog(1) on Linux/GTK2
710251190Sdteske		[ "$USE_XDIALOG" ] &&
711251190Sdteske			__height_bbox_size=$(( $__height_bbox_size + 3 ))
712251190Sdteske
713251190Sdteske		setvar "$__var_height" $__height_bbox_size
714238438Sdteske	fi
715238438Sdteske
716251190Sdteske	# No adjustemnts to width, just pass-thru the infobox width
717251190Sdteske	if [ "$__var_width" ]; then
718251190Sdteske		setvar "$__var_width" $__width_bbox_size
719251190Sdteske	fi
720251190Sdteske
721251190Sdteske	# Constrain values to sensible minimums/maximums unless `-n' was passed
722251190Sdteske	# Return success if no-constrain, else return status from constrain
723251190Sdteske	[ ! "$__constrain" ] ||
724251190Sdteske		f_dialog_size_constrain "$__var_height" "$__var_width"
725238438Sdteske}
726238438Sdteske
727251190Sdteske# f_dialog_inputbox_size [-n] $var_height $var_width \
728251190Sdteske#                        $title $backtitle $prompt $init [$hline]
729238438Sdteske#
730238438Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
731238438Sdteske# `--inputbox' boxes sensibly.
732238438Sdteske#
733251190Sdteske# This function helps solve this issue by taking two sets of sequential
734251190Sdteske# arguments. The first set of arguments are the variable names to use when
735251190Sdteske# storing the calculated height and width. The second set of arguments are the
736251190Sdteske# title, backtitle, prompt, and [optionally] hline. The optimal height and
737251190Sdteske# width for the described widget (not exceeding the actual terminal height or
738251190Sdteske# width) is stored in $var_height and $var_width (respectively).
739238438Sdteske#
740251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height and
741251190Sdteske# $var_width) are not constrained to minimum/maximum values.
742251190Sdteske#
743238438Sdteske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
744238438Sdteske# dialog(1).
745238438Sdteske#
746238438Sdteskef_dialog_inputbox_size()
747238438Sdteske{
748251190Sdteske	local __constrain=1
749251190Sdteske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
750251190Sdteske	local __var_height="$1" __var_width="$2"
751251190Sdteske	local __title="$3" __btitle="$4" __prompt="$5" __init="$6" __hline="$7"
752238438Sdteske
753251190Sdteske	# Return unless at least one size aspect has been requested
754251190Sdteske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
755238438Sdteske
756251190Sdteske	# Calculate height/width of buttonbox (adjusted/constrained below)
757251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
758251190Sdteske	#       from becoming local (and thus preventing setvar from working).
759251190Sdteske	local __height_ibox_size __width_ibox_size
760251190Sdteske	f_dialog_buttonbox_size -n \
761251190Sdteske		"${__var_height:+__height_ibox_size}" \
762251190Sdteske		"${__var_width:+__width_ibox_size}" \
763251190Sdteske		"$__title" "$__btitle" "$__prompt" "$__hline"
764238438Sdteske
765251190Sdteske	# Adjust height if desired
766251190Sdteske	if [ "$__var_height" ]; then
767251190Sdteske		# Add height for input box (not needed for Xdialog(1))
768251190Sdteske		[ ! "$USE_XDIALOG" ] &&
769251190Sdteske			__height_ibox_size=$(( $__height_ibox_size + 3 ))
770238438Sdteske
771251190Sdteske		setvar "$__var_height" $__height_ibox_size
772251190Sdteske	fi
773238438Sdteske
774251190Sdteske	# Adjust width if desired
775251190Sdteske	if [ "$__var_width" ]; then
776251190Sdteske		# Bump width for initial text (something neither dialog(1) nor
777251190Sdteske		# Xdialog(1) do, but worth it!; add 16.6% if using Xdialog(1))
778251190Sdteske		local __n=$(( ${#__init} + 7 ))
779251190Sdteske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 ))
780251190Sdteske		[ $__n -gt $__width_ibox_size ] && __width_ibox_size=$__n
781251190Sdteske
782251190Sdteske		setvar "$__var_width" $__width_ibox_size
783238438Sdteske	fi
784238438Sdteske
785251190Sdteske	# Constrain values to sensible minimums/maximums unless `-n' was passed
786251190Sdteske	# Return success if no-constrain, else return status from constrain
787251190Sdteske	[ ! "$__constrain" ] ||
788251190Sdteske		f_dialog_size_constrain "$__var_height" "$__var_width"
789238438Sdteske}
790238438Sdteske
791251190Sdteske# f_xdialog_2inputsbox_size [-n] $var_height $var_width \
792251190Sdteske#                           $title $backtitle $prompt \
793238438Sdteske#                           $label1 $init1 $label2 $init2
794238438Sdteske#
795238438Sdteske# Xdialog(1) does not perform auto-sizing of the width and height of
796238438Sdteske# `--2inputsbox' boxes sensibly.
797238438Sdteske#
798251190Sdteske# This function helps solve this issue by taking two sets of sequential
799251190Sdteske# arguments. The first set of arguments are the variable names to use when
800251190Sdteske# storing the calculated height and width. The second set of arguments are the
801251190Sdteske# title, backtitle, prompt, label for the first field, initial text for said
802251190Sdteske# field, label for the second field, and initial text for said field. The
803251190Sdteske# optimal height and width for the described widget (not exceeding the actual
804251190Sdteske# terminal height or width) is stored in $var_height and $var_width
805251190Sdteske# (respectively).
806238438Sdteske#
807251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height and
808251190Sdteske# $var_width) are not constrained to minimum/maximum values.
809251190Sdteske#
810238438Sdteske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
811238438Sdteske# Xdialog(1).
812238438Sdteske#
813238438Sdteskef_xdialog_2inputsbox_size()
814238438Sdteske{
815251190Sdteske	local __constrain=1
816251190Sdteske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
817251190Sdteske	local __var_height="$1" __var_width="$2"
818251190Sdteske	local __title="$3" __btitle="$4" __prompt="$5"
819251190Sdteske	local __label1="$6" __init1="$7" __label2="$8" __init2="$9"
820238438Sdteske
821251190Sdteske	# Return unless at least one size aspect has been requested
822251190Sdteske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
823238438Sdteske
824251190Sdteske	# Calculate height/width of inputbox (adjusted/constrained below)
825251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
826251190Sdteske	#       from becoming local (and thus preventing setvar from working).
827251190Sdteske	local __height_2ibox_size __width_2ibox_size
828251190Sdteske	f_dialog_inputbox_size -n \
829251190Sdteske		"${__var_height:+__height_2ibox_size}" \
830251190Sdteske		"${__var_width:+__width_2ibox_size}" \
831251190Sdteske		"$__title" "$__btitle" "$__prompt" "$__hline" "$__init1"
832251190Sdteske	
833251190Sdteske	# Adjust height if desired
834251190Sdteske	if [ "$__var_height" ]; then
835251190Sdteske		# Add height for 1st label, 2nd label, and 2nd input box
836251190Sdteske		__height_2ibox_size=$(( $__height_2ibox_size + 2 + 2 + 2  ))
837251190Sdteske		setvar "$__var_height" $__height_2ibox_size
838238438Sdteske	fi
839238438Sdteske
840251190Sdteske	# Adjust width if desired
841251190Sdteske	if [ "$__var_width" ]; then
842251190Sdteske		local __n
843238438Sdteske
844251190Sdteske		# Bump width for first label text (+16.6% since Xdialog(1))
845251190Sdteske		__n=$(( ${#__label1} + 7 ))
846251190Sdteske		__n=$(( $__n + $__n / 6 ))
847251190Sdteske		[ $__n -gt $__width_2ibox_size ] && __width_2ibox_size=$__n
848238438Sdteske
849251190Sdteske		# Bump width for second label text (+16.6% since Xdialog(1))
850251190Sdteske		__n=$(( ${#__label2} + 7 ))
851251190Sdteske		__n=$(( $__n + $__n / 6 ))
852251190Sdteske		[ $__n -gt $__width_2ibox_size ] && __width_2ibox_size=$__n
853238438Sdteske
854251190Sdteske		# Bump width for 2nd initial text (something neither dialog(1)
855251190Sdteske		# nor Xdialog(1) do, but worth it!; +16.6% since Xdialog(1))
856251190Sdteske		__n=$(( ${#__init2} + 7 ))
857251190Sdteske		__n=$(( $__n + $__n / 6 ))
858251190Sdteske		[ $__n -gt $__width_2ibox_size ] && __width_2ibox_size=$__n
859238438Sdteske
860251190Sdteske		setvar "$__var_width" $__width_2ibox_size
861238438Sdteske	fi
862238438Sdteske
863251190Sdteske	# Constrain values to sensible minimums/maximums unless `-n' was passed
864251190Sdteske	# Return success if no-constrain, else return status from constrain
865251190Sdteske	[ ! "$__constrain" ] ||
866251190Sdteske		f_dialog_size_constrain "$__var_height" "$__var_width"
867238438Sdteske}
868238438Sdteske
869251190Sdteske# f_dialog_menu_size [-n] $var_height $var_width $var_rows \
870251190Sdteske#                    $title $backtitle $prompt $hline \
871238438Sdteske#                    $tag1 $item1 $tag2 $item2 ...
872238438Sdteske#
873238438Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
874238438Sdteske# `--menu' boxes sensibly.
875238438Sdteske#
876251190Sdteske# This function helps solve this issue by taking three sets of sequential
877251190Sdteske# arguments. The first set of arguments are the variable names to use when
878251190Sdteske# storing the calculated height, width, and rows. The second set of arguments
879251190Sdteske# are the title, backtitle, prompt, and hline. The [optional] third set of
880251190Sdteske# arguments are the menu list itself (comprised of tag/item couplets). The
881251190Sdteske# optimal height, width, and rows for the described widget (not exceeding the
882251190Sdteske# actual terminal height or width) is stored in $var_height, $var_width, and
883251190Sdteske# $var_rows (respectively).
884238438Sdteske#
885251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
886251190Sdteske# and $var_rows) are not constrained to minimum/maximum values.
887238438Sdteske#
888238438Sdteskef_dialog_menu_size()
889238438Sdteske{
890251190Sdteske	local __constrain=1
891251190Sdteske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
892251190Sdteske	local __var_height="$1" __var_width="$2" __var_rows="$3"
893251190Sdteske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
894251190Sdteske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
895238438Sdteske
896251190Sdteske	# Return unless at least one size aspect has been requested
897251190Sdteske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
898251190Sdteske		return $FAILURE
899238438Sdteske
900251190Sdteske	# Calculate height/width of infobox (adjusted/constrained below)
901251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
902251190Sdteske	#       from becoming local (and thus preventing setvar from working).
903251190Sdteske	local __height_menu_size __width_menu_size
904251190Sdteske	f_dialog_infobox_size -n \
905251190Sdteske		"${__var_height:+__height_menu_size}" \
906251190Sdteske		"${__var_width:+__width_menu_size}" \
907251190Sdteske		"$__title" "$__btitle" "$__prompt" "$__hline"
908238438Sdteske
909238438Sdteske	#
910251190Sdteske	# Always process the menu-item arguments to get the longest tag-length,
911251190Sdteske	# longest item-length (both used to bump the width), and the number of
912251190Sdteske	# rows (used to bump the height).
913238438Sdteske	#
914251190Sdteske	local __longest_tag=0 __longest_item=0 __rows=0
915238438Sdteske	while [ $# -ge 2 ]; do
916251190Sdteske		local __tag="$1" __item="$2"
917238438Sdteske		shift 2 # tag/item
918251190Sdteske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
919251190Sdteske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
920251190Sdteske		__rows=$(( $__rows + 1 ))
921238438Sdteske	done
922238438Sdteske
923251190Sdteske	# Adjust rows early (for up-comning height calculation)
924251190Sdteske	if [ "$__var_height" -o "$__var_rows" ]; then
925251190Sdteske		# Add a row for visual aid if using Xdialog(1)
926251190Sdteske		[ "$USE_XDIALOG" ] && __rows=$(( $__rows + 1 ))
927251190Sdteske	fi
928251190Sdteske
929251190Sdteske	# Adjust height if desired
930251190Sdteske	if [ "$__var_height" ]; then
931251190Sdteske		# Add rows to height
932251190Sdteske		if [ "$USE_XDIALOG" ]; then
933251190Sdteske			__height_menu_size=$((
934251190Sdteske				$__height_menu_size + $__rows + 7 ))
935238438Sdteske		else
936251190Sdteske			__height_menu_size=$((
937251190Sdteske				$__height_menu_size + $__rows + 4 ))
938238438Sdteske		fi
939251190Sdteske		setvar "$__var_height" $__height_menu_size
940238438Sdteske	fi
941238438Sdteske
942251190Sdteske	# Adjust width if desired
943251190Sdteske	if [ "$__var_width" ]; then
944251190Sdteske		# The sum total between the longest tag-length and the
945251190Sdteske		# longest item-length should be used to bump menu width
946251190Sdteske		local __n=$(( $__longest_tag + $__longest_item + 10 ))
947251190Sdteske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
948251190Sdteske		[ $__n -gt $__width_menu_size ] && __width_menu_size=$__n
949251190Sdteske
950251190Sdteske		setvar "$__var_width" $__width_menu_size
951238438Sdteske	fi
952238438Sdteske
953251190Sdteske	# Store adjusted rows if desired
954251190Sdteske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows
955251190Sdteske
956251190Sdteske	# Constrain height, width, and rows to sensible minimum/maximum values
957251190Sdteske	# Return success if no-constrain, else return status from constrain
958251190Sdteske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
959251190Sdteske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
960238438Sdteske}
961238438Sdteske
962251190Sdteske# f_dialog_menu_with_help_size [-n] $var_height $var_width $var_rows \
963251190Sdteske#                              $title $backtitle $prompt $hline \
964238438Sdteske#                              $tag1 $item1 $help1 $tag2 $item2 $help2 ...
965238438Sdteske#
966238438Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
967238438Sdteske# `--menu' boxes sensibly.
968238438Sdteske#
969251190Sdteske# This function helps solve this issue by taking three sets of sequential
970251190Sdteske# arguments. The first set of arguments are the variable names to use when
971251190Sdteske# storing the calculated height, width, and rows. The second set of arguments
972251190Sdteske# are the title, backtitle, prompt, and hline. The [optional] third set of
973251190Sdteske# arguments are the menu list itself (comprised of tag/item/help triplets). The
974251190Sdteske# optimal height, width, and rows for the described widget (not exceeding the
975251190Sdteske# actual terminal height or width) is stored in $var_height, $var_width, and
976251190Sdteske# $var_rows (respectively).
977238438Sdteske#
978251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
979251190Sdteske# and $var_rows) are not constrained to minimum/maximum values.
980238438Sdteske#
981238438Sdteskef_dialog_menu_with_help_size()
982238438Sdteske{
983251190Sdteske	local __constrain=1
984251190Sdteske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
985251190Sdteske	local __var_height="$1" __var_width="$2" __var_rows="$3"
986251190Sdteske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
987251190Sdteske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
988238438Sdteske
989251190Sdteske	# Return unless at least one size aspect has been requested
990251190Sdteske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
991251190Sdteske		return $FAILURE
992238438Sdteske
993251190Sdteske	# Calculate height/width of infobox (adjusted/constrained below)
994251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
995251190Sdteske	#       from becoming local (and thus preventing setvar from working).
996251190Sdteske	local __height_menu_with_help_size __width_menu_with_help_size
997251190Sdteske	f_dialog_infobox_size -n \
998251190Sdteske		"${__var_height:+__height_menu_with_help_size}" \
999251190Sdteske		"${__var_width:+__width_menu_with_help_size}" \
1000251190Sdteske		"$__title" "$__btitle" "$__prompt" "$__hline"
1001238438Sdteske
1002238438Sdteske	#
1003251190Sdteske	# Always process the menu-item arguments to get the longest tag-length,
1004251190Sdteske	# longest item-length, longest help-length (help-length only considered
1005251190Sdteske	# if using Xdialog(1), as it places the help string in the widget) --
1006251190Sdteske	# all used to bump the width -- and the number of rows (used to bump
1007251190Sdteske	# the height).
1008238438Sdteske	#
1009251190Sdteske	local __longest_tag=0 __longest_item=0 __longest_help=0 __rows=0
1010238438Sdteske	while [ $# -ge 3 ]; do
1011251190Sdteske		local __tag="$1" __item="$2" __help="$3"
1012238438Sdteske		shift 3 # tag/item/help
1013251190Sdteske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
1014251190Sdteske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
1015251190Sdteske		[ ${#__help} -gt $__longest_help ] && __longest_help=${#__help}
1016251190Sdteske		__rows=$(( $__rows + 1 ))
1017238438Sdteske	done
1018238438Sdteske
1019251190Sdteske	# Adjust rows early (for up-coming height calculation)
1020251190Sdteske	if [ "$__var_height" -o "$__var_rows" ]; then
1021251190Sdteske		# Add a row for visual aid if using Xdialog(1)
1022251190Sdteske		[ "$USE_XDIALOG" ] && __rows=$(( $__rows + 1 ))
1023251190Sdteske	fi
1024251190Sdteske
1025251190Sdteske	# Adjust height if desired
1026251190Sdteske	if [ "$__var_height" ]; then
1027251190Sdteske		# Add rows to height
1028251190Sdteske		if [ "$USE_XDIALOG" ]; then
1029251190Sdteske			__height_menu_with_help_size=$((
1030251190Sdteske				$__height_menu_with_help_size + $__rows + 8 ))
1031238438Sdteske		else
1032251190Sdteske			__height_menu_with_help_size=$((
1033251190Sdteske				$__height_menu_with_help_size + $__rows + 4 ))
1034238438Sdteske		fi
1035251190Sdteske		setvar "$__var_height" $__height_menu_with_help_size
1036238438Sdteske	fi
1037238438Sdteske
1038251190Sdteske	# Adjust width if desired
1039251190Sdteske	if [ "$__var_width" ]; then
1040251190Sdteske		# The sum total between the longest tag-length and the
1041251190Sdteske		# longest item-length should be used to bump menu width
1042251190Sdteske		local __n=$(( $__longest_tag + $__longest_item + 10 ))
1043251190Sdteske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
1044251190Sdteske		[ $__n -gt $__width_menu_with_help_size ] &&
1045251190Sdteske			__width_menu_with_help_size=$__n
1046251190Sdteske
1047251190Sdteske		# Update width for help text if using Xdialog(1)
1048251190Sdteske		if [ "$USE_XDIALOG" ]; then
1049251190Sdteske			__n=$(( $__longest_help + 10 ))
1050251190Sdteske			__n=$(( $__n + $__n / 6 )) # plus 16.6%
1051251190Sdteske			[ $__n -gt $__width_menu_with_help_size ] &&
1052251190Sdteske				__width_menu_with_help_size=$__n
1053238438Sdteske		fi
1054238438Sdteske
1055251190Sdteske		setvar "$__var_width" $__width_menu_with_help_size
1056238438Sdteske	fi
1057238438Sdteske
1058251190Sdteske	# Store adjusted rows if desired
1059251190Sdteske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows
1060251190Sdteske
1061251190Sdteske	# Constrain height, width, and rows to sensible minimum/maximum values
1062251190Sdteske	# Return success if no-constrain, else return status from constrain
1063251190Sdteske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
1064251190Sdteske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
1065238438Sdteske}
1066238438Sdteske
1067251190Sdteske# f_dialog_radiolist_size [-n] $var_height $var_width $var_rows \
1068251190Sdteske#                         $title $backtitle $prompt $hline \
1069238438Sdteske#                         $tag1 $item1 $status1 $tag2 $item2 $status2 ...
1070238438Sdteske#
1071238438Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1072238438Sdteske# `--radiolist' boxes sensibly.
1073238438Sdteske#
1074251190Sdteske# This function helps solve this issue by taking three sets of sequential
1075251190Sdteske# arguments. The first set of arguments are the variable names to use when
1076251190Sdteske# storing the calculated height, width, and rows. The second set of arguments
1077251190Sdteske# are the title, backtitle, prompt, and hline. The [optional] third set of
1078251190Sdteske# arguments are the radio list itself (comprised of tag/item/status triplets).
1079251190Sdteske# The optimal height, width, and rows for the described widget (not exceeding
1080251190Sdteske# the actual terminal height or width) is stored in $var_height, $var_width,
1081251190Sdteske# and $var_rows (respectively).
1082238438Sdteske#
1083251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
1084251190Sdteske# and $var_rows) are not constrained to minimum/maximum values.
1085238438Sdteske#
1086238438Sdteskef_dialog_radiolist_size()
1087238438Sdteske{
1088251190Sdteske	local __constrain=1
1089251190Sdteske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
1090251190Sdteske	local __var_height="$1" __var_width="$2" __var_rows="$3"
1091251190Sdteske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
1092251190Sdteske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
1093238438Sdteske
1094251190Sdteske	# Return unless at least one size aspect has been requested
1095251190Sdteske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
1096251190Sdteske		return $FAILURE
1097238438Sdteske
1098251190Sdteske	# Calculate height/width of infobox (adjusted/constrained below)
1099251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
1100251190Sdteske	#       from becoming local (and thus preventing setvar from working).
1101251190Sdteske	local __height_rlist_size __width_rlist_size
1102251190Sdteske	f_dialog_infobox_size -n \
1103251190Sdteske		"${__var_height:+__height_rlist_size}" \
1104251190Sdteske		"${__var_width:+__width_rlist_size}" \
1105251190Sdteske		"$__title" "$__btitle" "$__prompt" "$__hline"
1106238438Sdteske
1107238438Sdteske	#
1108251190Sdteske	# Always process the menu-item arguments to get the longest tag-length,
1109251190Sdteske	# longest item-length (both used to bump the width), and the number of
1110251190Sdteske	# rows (used to bump the height).
1111238438Sdteske	#
1112263980Sdteske	local __longest_tag=0 __longest_item=0 __rows_rlist_size=0
1113249757Sdteske	while [ $# -ge 3 ]; do
1114251190Sdteske		local __tag="$1" __item="$2"
1115249757Sdteske		shift 3 # tag/item/status
1116251190Sdteske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
1117251190Sdteske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
1118263980Sdteske		__rows_rlist_size=$(( $__rows_rlist_size + 1 ))
1119238438Sdteske	done
1120238438Sdteske
1121251190Sdteske	# Adjust rows early (for up-coming height calculation)
1122251190Sdteske	if [ "$__var_height" -o "$__var_rows" ]; then
1123251190Sdteske		# Add a row for visual aid if using Xdialog(1)
1124263980Sdteske		[ "$USE_XDIALOG" ] &&
1125263980Sdteske			__rows_rlist_size=$(( $__rows_rlist_size + 1 ))
1126251190Sdteske	fi
1127251190Sdteske
1128251190Sdteske	# Adjust height if desired
1129251190Sdteske	if [ "$__var_height" ]; then
1130251190Sdteske		# Add rows to height
1131251190Sdteske		if [ "$USE_XDIALOG" ]; then
1132251190Sdteske			__height_rlist_size=$((
1133263980Sdteske				$__height_rlist_size + $__rows_rlist_size + 7
1134263980Sdteske			))
1135238438Sdteske		else
1136251190Sdteske			__height_rlist_size=$((
1137263980Sdteske				$__height_rlist_size + $__rows_rlist_size + 4
1138263980Sdteske			))
1139238438Sdteske		fi
1140251190Sdteske		setvar "$__var_height" $__height_rlist_size
1141238438Sdteske	fi
1142238438Sdteske
1143251190Sdteske	# Adjust width if desired
1144251190Sdteske	if [ "$__var_width" ]; then
1145251190Sdteske		# Sum total between longest tag-length, longest item-length,
1146251190Sdteske		# and radio-button width should be used to bump menu width
1147251190Sdteske		local __n=$(( $__longest_tag + $__longest_item + 13 ))
1148251190Sdteske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
1149251190Sdteske		[ $__n -gt $__width_rlist_size ] && __width_rlist_size=$__n
1150251190Sdteske
1151251190Sdteske		setvar "$__var_width" $__width_rlist_size
1152238438Sdteske	fi
1153238438Sdteske
1154251190Sdteske	# Store adjusted rows if desired
1155263980Sdteske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows_rlist_size
1156251190Sdteske
1157251190Sdteske	# Constrain height, width, and rows to sensible minimum/maximum values
1158251190Sdteske	# Return success if no-constrain, else return status from constrain
1159251190Sdteske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
1160251190Sdteske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
1161238438Sdteske}
1162238438Sdteske
1163251190Sdteske# f_dialog_checklist_size [-n] $var_height $var_width $var_rows \
1164251190Sdteske#                         $title $backtitle $prompt $hline \
1165249758Sdteske#                         $tag1 $item1 $status1 $tag2 $item2 $status2 ...
1166249758Sdteske#
1167249758Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1168249758Sdteske# `--checklist' boxes sensibly.
1169249758Sdteske#
1170251190Sdteske# This function helps solve this issue by taking three sets of sequential
1171251190Sdteske# arguments. The first set of arguments are the variable names to use when
1172251190Sdteske# storing the calculated height, width, and rows. The second set of arguments
1173251190Sdteske# are the title, backtitle, prompt, and hline. The [optional] third set of
1174251190Sdteske# arguments are the check list itself (comprised of tag/item/status triplets).
1175251190Sdteske# The optimal height, width, and rows for the described widget (not exceeding
1176251190Sdteske# the actual terminal height or width) is stored in $var_height, $var_width,
1177251190Sdteske# and $var_rows (respectively). 
1178249758Sdteske#
1179251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
1180251190Sdteske# and $var_rows) are not constrained to minimum/maximum values.
1181249758Sdteske#
1182249758Sdteskef_dialog_checklist_size()
1183249758Sdteske{
1184249758Sdteske	f_dialog_radiolist_size "$@"
1185249758Sdteske}
1186249758Sdteske
1187251190Sdteske# f_dialog_radiolist_with_help_size [-n] $var_height $var_width $var_rows \
1188251190Sdteske#                                   $title $backtitle $prompt $hline \
1189249758Sdteske#                                   $tag1 $item1 $status1 $help1 \
1190249758Sdteske#                                   $tag2 $item2 $status2 $help2 ...
1191249758Sdteske#
1192249758Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1193249758Sdteske# `--radiolist' boxes sensibly.
1194249758Sdteske#
1195251190Sdteske# This function helps solve this issue by taking three sets of sequential
1196251190Sdteske# arguments. The first set of arguments are the variable names to use when
1197251190Sdteske# storing the calculated height, width, and rows. The second set of arguments
1198251190Sdteske# are the title, backtitle, prompt, and hline. The [optional] third set of
1199251190Sdteske# arguments are the radio list itself (comprised of tag/item/status/help
1200251190Sdteske# quadruplets). The optimal height, width, and rows for the described widget
1201251190Sdteske# (not exceeding the actual terminal height or width) is stored in $var_height,
1202251190Sdteske# $var_width, and $var_rows (respectively).
1203249758Sdteske#
1204251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
1205251190Sdteske# and $var_rows) are not constrained to minimum/maximum values.
1206249758Sdteske#
1207249758Sdteskef_dialog_radiolist_with_help_size()
1208249758Sdteske{
1209251190Sdteske	local __constrain=1
1210251190Sdteske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
1211251190Sdteske	local __var_height="$1" __var_width="$2" __var_rows="$3"
1212251190Sdteske	local __title="$4" __btitle="$5" __prompt="$6" __hline="$7"
1213251190Sdteske	shift 7 # var_height/var_width/var_rows/title/btitle/prompt/hline
1214249758Sdteske
1215251190Sdteske	# Return unless at least one size aspect has been requested
1216251190Sdteske	[ "$__var_height" -o "$__var_width" -o "$__var_rows" ] ||
1217251190Sdteske		return $FAILURE
1218249758Sdteske
1219251190Sdteske	# Calculate height/width of infobox (adjusted/constrained below)
1220251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
1221251190Sdteske	#       from becoming local (and thus preventing setvar from working).
1222251190Sdteske	local __height_rlist_with_help_size __width_rlist_with_help_size
1223251190Sdteske	f_dialog_infobox_size -n \
1224251190Sdteske		"${__var_height:+__height_rlist_with_help_size}" \
1225251190Sdteske		"${__var_width:+__width_rlist_with_help_size}" \
1226251190Sdteske		"$__title" "$__btitle" "$__prompt" "$__hline"
1227249758Sdteske
1228249758Sdteske	#
1229251190Sdteske	# Always process the menu-item arguments to get the longest tag-length,
1230251190Sdteske	# longest item-length, longest help-length (help-length only considered
1231251190Sdteske	# if using Xdialog(1), as it places the help string in the widget) --
1232251190Sdteske	# all used to bump the width -- and the number of rows (used to bump
1233251190Sdteske	# the height).
1234249758Sdteske	#
1235263980Sdteske	local __longest_tag=0 __longest_item=0 __longest_help=0
1236263980Sdteske	local __rows_rlist_with_help_size=0
1237249758Sdteske	while [ $# -ge 4 ]; do
1238251190Sdteske		local __tag="$1" __item="$2" __status="$3" __help="$4"
1239249758Sdteske		shift 4 # tag/item/status/help
1240251190Sdteske		[ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag}
1241251190Sdteske		[ ${#__item} -gt $__longest_item ] && __longest_item=${#__item}
1242251190Sdteske		[ ${#__help} -gt $__longest_help ] && __longest_help=${#__help}
1243263980Sdteske		__rows_rlist_with_help_size=$((
1244263980Sdteske			$__rows_rlist_with_help_size + 1
1245263980Sdteske		))
1246249758Sdteske	done
1247249758Sdteske
1248251190Sdteske	# Adjust rows early (for up-coming height calculation)
1249251190Sdteske	if [ "$__var_height" -o "$__var_rows" ]; then
1250251190Sdteske		# Add a row for visual aid if using Xdialog(1)
1251263980Sdteske		[ "$USE_XDIALOG" ] &&
1252263980Sdteske			__rows_rlist_with_help_size=$((
1253263980Sdteske				$__rows_rlist_with_help_size + 1
1254263980Sdteske			))
1255251190Sdteske	fi
1256251190Sdteske
1257251190Sdteske	# Adjust height if desired
1258251190Sdteske	if [ "$__var_height" ]; then
1259251190Sdteske		# Add rows to height
1260251190Sdteske		if [ "$USE_XDIALOG" ]; then
1261251190Sdteske			__height_rlist_with_help_size=$((
1262263980Sdteske				$__height_rlist_with_help_size +
1263263980Sdteske				$__rows_rlist_with_help_size + 7
1264263980Sdteske			))
1265249758Sdteske		else
1266251190Sdteske			__height_rlist_with_help_size=$((
1267263980Sdteske				$__height_rlist_with_help_size +
1268263980Sdteske				$__rows_rlist_with_help_size + 4
1269263980Sdteske			))
1270249758Sdteske		fi
1271251190Sdteske		setvar "$__var_height" $__height
1272249758Sdteske	fi
1273249758Sdteske
1274251190Sdteske	# Adjust width if desired
1275251190Sdteske	if [ "$__var_width" ]; then
1276251190Sdteske		# Sum total between longest tag-length, longest item-length,
1277251190Sdteske		# and radio-button width should be used to bump menu width
1278251190Sdteske		local __n=$(( $__longest_tag + $__longest_item + 13 ))
1279251190Sdteske		[ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6%
1280251190Sdteske		[ $__n -gt $__width_rlist_with_help_size ] &&
1281251190Sdteske			__width_rlist_with_help_size=$__n
1282251190Sdteske
1283251190Sdteske		# Update width for help text if using Xdialog(1)
1284251190Sdteske		if [ "$USE_XDIALOG" ]; then
1285251190Sdteske			__n=$(( $__longest_help + 10 ))
1286251190Sdteske			__n=$(( $__n + $__n / 6 )) # plus 16.6%
1287251190Sdteske			[ $__n -gt $__width_rlist_with_help_size ] &&
1288251190Sdteske				__width_rlist_with_help_size=$__n
1289249758Sdteske		fi
1290249758Sdteske
1291251190Sdteske		setvar "$__var_width" $__width_rlist_with_help_size
1292249758Sdteske	fi
1293249758Sdteske
1294251190Sdteske	# Store adjusted rows if desired
1295263980Sdteske	[ "$__var_rows" ] && setvar "$__var_rows" $__rows_rlist_with_help_size
1296251190Sdteske
1297251190Sdteske	# Constrain height, width, and rows to sensible minimum/maximum values
1298251190Sdteske	# Return success if no-constrain, else return status from constrain
1299251190Sdteske	[ ! "$__constrain" ] || f_dialog_menu_constrain \
1300251190Sdteske		"$__var_height" "$__var_width" "$__var_rows" "$__prompt"
1301249758Sdteske}
1302249758Sdteske
1303251190Sdteske# f_dialog_checklist_with_help_size [-n] $var_height $var_width $var_rows \
1304251190Sdteske#                                   $title $backtitle $prompt $hline \
1305249758Sdteske#                                   $tag1 $item1 $status1 $help1 \
1306249758Sdteske#                                   $tag2 $item2 $status2 $help2 ...
1307249758Sdteske#
1308249758Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1309249758Sdteske# `--checklist' boxes sensibly.
1310249758Sdteske#
1311251190Sdteske# This function helps solve this issue by taking three sets of sequential
1312251190Sdteske# arguments. The first set of arguments are the variable names to use when
1313251190Sdteske# storing the calculated height, width, and rows. The second set of arguments
1314251190Sdteske# are the title, backtitle, prompt, and hline. The [optional] third set of
1315251190Sdteske# arguments are the check list itself (comprised of tag/item/status/help
1316251190Sdteske# quadruplets). The optimal height, width, and rows for the described widget
1317251190Sdteske# (not exceeding the actual terminal height or width) is stored in $var_height,
1318251190Sdteske# $var_width, and $var_rows (respectively).
1319249758Sdteske#
1320251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height, $var_width,
1321251190Sdteske# and $var_rows) are not constrained to minimum/maximum values.
1322249758Sdteske#
1323249758Sdteskef_dialog_checklist_with_help_size()
1324249758Sdteske{
1325249758Sdteske	f_dialog_radiolist_with_help_size "$@"
1326249758Sdteske}
1327249758Sdteske
1328251190Sdteske# f_dialog_calendar_size [-n] $var_height $var_width \
1329251190Sdteske#                        $title $backtitle $prompt [$hline]
1330238438Sdteske#
1331238438Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1332238438Sdteske# `--calendar' boxes sensibly.
1333238438Sdteske#
1334251190Sdteske# This function helps solve this issue by taking two sets of sequential
1335251190Sdteske# arguments. The first set of arguments are the variable names to use when
1336251190Sdteske# storing the calculated height and width. The second set of arguments are the
1337251190Sdteske# title, backtitle, prompt, and [optionally] hline. The optimal height and
1338251190Sdteske# width for the described widget (not exceeding the actual terminal height or
1339251190Sdteske# width) is stored in $var_height and $var_width (respectively).
1340238438Sdteske#
1341251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height and
1342251190Sdteske# $var_width) are not constrained to minimum/maximum values.
1343251190Sdteske#
1344238438Sdteske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
1345238438Sdteske# dialog(1).
1346238438Sdteske#
1347238438Sdteskef_dialog_calendar_size()
1348238438Sdteske{
1349251190Sdteske	local __constrain=1
1350251190Sdteske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
1351251190Sdteske	local __var_height="$1" __var_width="$2"
1352251190Sdteske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
1353238438Sdteske
1354251190Sdteske	# Return unless at least one size aspect has been requested
1355251190Sdteske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
1356251190Sdteske
1357251190Sdteske	#
1358251190Sdteske	# Obtain/Adjust minimum and maximum thresholds
1359251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
1360251190Sdteske	#       from becoming local (and thus preventing setvar from working).
1361251190Sdteske	#
1362251190Sdteske	local __max_height_cal_size __max_width_cal_size
1363251190Sdteske	f_dialog_max_size __max_height_cal_size __max_width_cal_size
1364251190Sdteske	__max_width_cal_size=$(( $__max_width_cal_size - 2 ))
1365251190Sdteske		# the calendar box will refuse to display if too wide
1366251190Sdteske	local __min_width
1367238438Sdteske	if [ "$USE_XDIALOG" ]; then
1368251190Sdteske		__min_width=55
1369238438Sdteske	else
1370251190Sdteske		__min_width=40
1371251190Sdteske		__max_height_cal_size=$((
1372251190Sdteske			$__max_height_cal_size - $DIALOG_CALENDAR_HEIGHT ))
1373251190Sdteske		# When using dialog(1), we can't predict whether the user has
1374251190Sdteske		# disabled shadow's in their `$HOME/.dialogrc' file, so we'll
1375251190Sdteske		# subtract one for the potential shadow around the widget
1376251190Sdteske		__max_height_cal_size=$(( $__max_height_cal_size - 1 ))
1377238438Sdteske	fi
1378238438Sdteske
1379251190Sdteske	# Calculate height if desired
1380251190Sdteske	if [ "$__var_height" ]; then
1381251190Sdteske		local __height
1382251190Sdteske		__height=$( echo "$__prompt" | f_number_of_lines )
1383238438Sdteske
1384251190Sdteske		if [ "$USE_XDIALOG" ]; then
1385251190Sdteske			# Add height to accomodate for embedded calendar widget
1386251190Sdteske			__height=$(( $__height + $DIALOG_CALENDAR_HEIGHT - 1 ))
1387238438Sdteske
1388251190Sdteske			# Also, bump height if backtitle is enabled
1389251190Sdteske			if [ "$__btitle" ]; then
1390251190Sdteske				local __n
1391251190Sdteske				__n=$( echo "$__btitle" | f_number_of_lines )
1392251190Sdteske				__height=$(( $__height + $__n + 2 ))
1393251190Sdteske			fi
1394251190Sdteske		else
1395251190Sdteske			[ "$__prompt" ] && __height=$(( $__height + 1 ))
1396238438Sdteske		fi
1397251190Sdteske
1398251190Sdteske		# Enforce maximum height, unless `-n' was passed
1399251190Sdteske		[ "$__constrain" -a $__height -gt $__max_height_cal_size ] &&
1400251190Sdteske			__height=$__max_height_cal_size
1401251190Sdteske
1402251190Sdteske		setvar "$__var_height" $__height
1403238438Sdteske	fi
1404238438Sdteske
1405251190Sdteske	# Calculate width if desired
1406251190Sdteske	if [ "$__var_width" ]; then
1407251190Sdteske		# NOTE: Function name appended to prevent __var_{height,width}
1408251190Sdteske		#       values from becoming local (and thus preventing setvar
1409251190Sdteske		#       from working).
1410251190Sdteske		local __width_cal_size
1411251190Sdteske		f_dialog_infobox_size -n "" __width_cal_size \
1412251190Sdteske			"$__title" "$__btitle" "$__prompt" "$__hline"
1413238438Sdteske
1414251190Sdteske		# Enforce minimum/maximum width, unless `-n' was passed
1415251190Sdteske		if [ "$__constrain" ]; then
1416251190Sdteske			if [ $__width_cal_size -lt $__min_width ]; then
1417251190Sdteske				__width_cal_size=$__min_width
1418251190Sdteske			elif [ $__width_cal_size -gt $__max_width_cal_size ]
1419251190Sdteske			then
1420251190Sdteske				__width_cal_size=$__max_width_size
1421251190Sdteske			fi
1422251190Sdteske		fi
1423251190Sdteske
1424251190Sdteske		setvar "$__var_width" $__width_cal_size
1425251190Sdteske	fi
1426251190Sdteske
1427251190Sdteske	return $SUCCESS
1428238438Sdteske}
1429238438Sdteske
1430251190Sdteske# f_dialog_timebox_size [-n] $var_height $var_width \
1431251190Sdteske#                       $title $backtitle $prompt [$hline]
1432238438Sdteske#
1433238438Sdteske# Not all versions of dialog(1) perform auto-sizing of the width and height of
1434238438Sdteske# `--timebox' boxes sensibly.
1435238438Sdteske#
1436251190Sdteske# This function helps solve this issue by taking two sets of sequential
1437251190Sdteske# arguments. The first set of arguments are the variable names to use when
1438251190Sdteske# storing the calculated height and width. The second set of arguments are the
1439251190Sdteske# title, backtitle, prompt, and [optionally] hline. The optional height and
1440251190Sdteske# width for the described widget (not exceeding the actual terminal height or
1441251190Sdteske# width) is stored in $var_height and $var_width (respectively).
1442238438Sdteske#
1443251190Sdteske# If the first argument is `-n', the calculated sizes ($var_height and
1444251190Sdteske# $var_width) are not constrained to minimum/maximum values.
1445251190Sdteske#
1446238438Sdteske# Newline character sequences (``\n'') in $prompt are expanded as-is done by
1447238438Sdteske# dialog(1).
1448238438Sdteske#
1449238438Sdteskef_dialog_timebox_size()
1450238438Sdteske{
1451251190Sdteske	local __constrain=1
1452251190Sdteske	[ "$1" = "-n" ] && __constrain= && shift 1 # -n
1453251190Sdteske	local __var_height="$1" __var_width="$2"
1454251190Sdteske	local __title="$3" __btitle="$4" __prompt="$5" __hline="$6"
1455238438Sdteske
1456251190Sdteske	# Return unless at least one size aspect has been requested
1457251190Sdteske	[ "$__var_height" -o "$__var_width" ] || return $FAILURE
1458238438Sdteske
1459238438Sdteske	#
1460251190Sdteske	# Obtain/Adjust minimum and maximum thresholds
1461251190Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
1462251190Sdteske	#       from becoming local (and thus preventing setvar from working).
1463238438Sdteske	#
1464251190Sdteske	local __max_height_tbox_size __max_width_tbox_size
1465251190Sdteske	f_dialog_max_size __max_height_tbox_size __max_width_tbox_size
1466251190Sdteske	__max_width_tbox_size=$(( $__max_width_tbox_size - 2 ))
1467251190Sdteske		# the timebox widget refuses to display if too wide
1468251190Sdteske	local __min_width
1469238438Sdteske	if [ "$USE_XDIALOG" ]; then
1470251190Sdteske		__min_width=40
1471238438Sdteske	else
1472251190Sdteske		__min_width=20
1473251190Sdteske		__max_height_tbox_size=$(( \
1474251190Sdteske			$__max_height_tbox_size - $DIALOG_TIMEBOX_HEIGHT ))
1475251190Sdteske		# When using dialog(1), we can't predict whether the user has
1476251190Sdteske		# disabled shadow's in their `$HOME/.dialogrc' file, so we'll
1477251190Sdteske		# subtract one for the potential shadow around the widget
1478251190Sdteske		__max_height_tbox_size=$(( $__max_height_tbox_size - 1 ))
1479238438Sdteske	fi
1480238438Sdteske
1481251190Sdteske	# Calculate height if desired
1482251190Sdteske	if [ "$__var_height" -a "$USE_XDIALOG" ]; then
1483251190Sdteske		# When using Xdialog(1), the height seems to have
1484251190Sdteske		# no effect. All values provide the same results.
1485251190Sdteske		setvar "$__var_height" 0 # autosize
1486251190Sdteske	elif [ "$__var_height" ]; then
1487251190Sdteske		local __height
1488251190Sdteske		__height=$( echo "$__prompt" | f_number_of_lines )
1489251190Sdteske		__height=$(( $__height ${__prompt:++1} + 1 ))
1490238438Sdteske
1491251190Sdteske		# Enforce maximum height, unless `-n' was passed
1492251190Sdteske		[ "$__constrain" -a $__height -gt $__max_height_tbox_size ] &&
1493251190Sdteske			__height=$__max_height_tbox_size
1494251190Sdteske
1495251190Sdteske		setvar "$__var_height" $__height
1496251190Sdteske	fi
1497251190Sdteske
1498251190Sdteske	# Calculate width if desired
1499251190Sdteske	if [ "$__var_width" ]; then
1500251190Sdteske		# NOTE: Function name appended to prevent __var_{height,width}
1501251190Sdteske		#       values from becoming local (and thus preventing setvar
1502251190Sdteske		#       from working).
1503251190Sdteske		local __width_tbox_size
1504251190Sdteske		f_dialog_infobox_size -n "" __width_tbox_size \
1505251190Sdteske			"$__title" "$__btitle" "$__prompt" "$__hline"
1506251190Sdteske
1507251190Sdteske		# Enforce the minimum width for displaying the timebox
1508251190Sdteske		if [ "$__constrain" ]; then
1509251190Sdteske			if [ $__width_tbox_size -lt $__min_width ]; then
1510251190Sdteske				__width_tbox_size=$__min_width
1511251190Sdteske			elif [ $__width_tbox_size -ge $__max_width_tbox_size ]
1512251190Sdteske			then
1513251190Sdteske				__width_tbox_size=$__max_width_tbox_size
1514251190Sdteske			fi
1515251190Sdteske		fi
1516251190Sdteske
1517251190Sdteske		setvar "$__var_width" $__width_tbox_size
1518251190Sdteske	fi
1519251190Sdteske
1520251190Sdteske	return $SUCCESS
1521238438Sdteske}
1522238438Sdteske
1523238438Sdteske############################################################ CLEAR FUNCTIONS
1524238438Sdteske
1525238438Sdteske# f_dialog_clear
1526238438Sdteske#
1527238438Sdteske# Clears any/all previous dialog(1) displays.
1528238438Sdteske#
1529238438Sdteskef_dialog_clear()
1530238438Sdteske{
1531238438Sdteske	$DIALOG --clear
1532238438Sdteske}
1533238438Sdteske
1534238438Sdteske############################################################ INFO FUNCTIONS
1535238438Sdteske
1536238438Sdteske# f_dialog_info $info_text ...
1537238438Sdteske#
1538238438Sdteske# Throw up a dialog(1) infobox. The infobox remains until another dialog is
1539244804Sdteske# displayed or `dialog --clear' (or f_dialog_clear) is called.
1540238438Sdteske#
1541238438Sdteskef_dialog_info()
1542238438Sdteske{
1543251190Sdteske	local info_text="$*" height width
1544251190Sdteske	f_dialog_infobox_size height width \
1545251190Sdteske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$info_text"
1546251190Sdteske	$DIALOG \
1547251190Sdteske		--title "$DIALOG_TITLE"         \
1548251190Sdteske		--backtitle "$DIALOG_BACKTITLE" \
1549251190Sdteske		${USE_XDIALOG:+--ignore-eof}    \
1550251190Sdteske		${USE_XDIALOG:+--no-buttons}    \
1551251190Sdteske		--infobox "$info_text" $height $width
1552238438Sdteske}
1553238438Sdteske
1554238438Sdteske# f_xdialog_info $info_text ...
1555238438Sdteske#
1556238438Sdteske# Throw up an Xdialog(1) infobox and do not dismiss it until stdin produces
1557238438Sdteske# EOF. This implies that you must execute this either as an rvalue to a pipe,
1558238438Sdteske# lvalue to indirection or in a sub-shell that provides data on stdin.
1559238438Sdteske#
1560272230Sdteske# To open an Xdialog(1) infobox that does not disappear until expeclitly dis-
1561272230Sdteske# missed, use the following:
1562272230Sdteske#
1563272230Sdteske# 	f_xdialog_info "$info_text" < /dev/tty &
1564272230Sdteske# 	pid=$!
1565272230Sdteske# 	# Perform some lengthy actions
1566272230Sdteske# 	kill $pid
1567272230Sdteske#
1568272230Sdteske# NB: Check $USE_XDIALOG if you need to support both dialog(1) and Xdialog(1).
1569272230Sdteske#
1570238438Sdteskef_xdialog_info()
1571238438Sdteske{
1572251190Sdteske	local info_text="$*" height width
1573251190Sdteske	f_dialog_infobox_size height width \
1574251190Sdteske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$info_text"
1575272230Sdteske	exec $DIALOG \
1576251190Sdteske		--title "$DIALOG_TITLE"               \
1577251190Sdteske		--backtitle "$DIALOG_BACKTITLE"       \
1578251190Sdteske		--no-close --no-buttons               \
1579251190Sdteske		--infobox "$info_text" $height $width \
1580238438Sdteske		-1 # timeout of -1 means abort when EOF on stdin
1581238438Sdteske}
1582238438Sdteske
1583279617Sdteske############################################################ PAUSE FUNCTIONS
1584279617Sdteske
1585279617Sdteske# f_dialog_pause $msg_text $duration [$hline]
1586279617Sdteske#
1587279617Sdteske# Display a message in a widget with a progress bar that runs backward for
1588279617Sdteske# $duration seconds.
1589279617Sdteske#
1590279617Sdteskef_dialog_pause()
1591279617Sdteske{
1592279617Sdteske	local pause_text="$1" duration="$2" hline="$3" height width
1593279617Sdteske	f_isinteger "$duration" || return $FAILURE
1594279617Sdteske	f_dialog_buttonbox_size height width \
1595279617Sdteske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$pause_text" "$hline"
1596279617Sdteske	if [ "$USE_XDIALOG" ]; then
1597279617Sdteske		$DIALOG \
1598279617Sdteske			--title "$DIALOG_TITLE"         \
1599279617Sdteske			--backtitle "$DIALOG_BACKTITLE" \
1600279617Sdteske			--ok-label "$msg_skip"          \
1601279617Sdteske			--cancel-label "$msg_cancel"    \
1602279617Sdteske			${noCancel:+--no-cancel}        \
1603279617Sdteske			--timeout "$duration"           \
1604279617Sdteske			--yesno "$pause_text"           \
1605279617Sdteske			$height $width
1606279617Sdteske	else
1607279617Sdteske		[ $duration -gt 0 ] && duration=$(( $duration - 1 ))
1608279617Sdteske		height=$(( $height + 3 )) # Add height for progress bar
1609279617Sdteske		$DIALOG \
1610279617Sdteske			--title "$DIALOG_TITLE"         \
1611279617Sdteske			--backtitle "$DIALOG_BACKTITLE" \
1612279617Sdteske			--hline "$hline"                \
1613279617Sdteske			--ok-label "$msg_skip"          \
1614279617Sdteske			--cancel-label "$msg_cancel"    \
1615279617Sdteske			${noCancel:+--no-cancel}        \
1616279617Sdteske			--pause "$pause_text"           \
1617279617Sdteske			$height $width "$duration"
1618279617Sdteske	fi
1619279617Sdteske}
1620279617Sdteske
1621279617Sdteske# f_dialog_pause_no_cancel $msg_text $duration [$hline]
1622279617Sdteske#
1623279617Sdteske# Display a message in a widget with a progress bar that runs backward for
1624279617Sdteske# $duration seconds. No cancel button is provided. Always returns success.
1625279617Sdteske#
1626279617Sdteskef_dialog_pause_no_cancel()
1627279617Sdteske{
1628279617Sdteske	noCancel=1 f_dialog_pause "$@"
1629279617Sdteske	return $SUCCESS
1630279617Sdteske}
1631279617Sdteske
1632238438Sdteske############################################################ MSGBOX FUNCTIONS
1633238438Sdteske
1634251912Sdteske# f_dialog_msgbox $msg_text [$hline]
1635238438Sdteske#
1636238438Sdteske# Throw up a dialog(1) msgbox. The msgbox remains until the user presses ENTER
1637238438Sdteske# or ESC, acknowledging the modal dialog.
1638238438Sdteske#
1639238438Sdteske# If the user presses ENTER, the exit status is zero (success), otherwise if
1640238438Sdteske# the user presses ESC the exit status is 255.
1641238438Sdteske#
1642238438Sdteskef_dialog_msgbox()
1643238438Sdteske{
1644251912Sdteske	local msg_text="$1" hline="$2" height width
1645251190Sdteske	f_dialog_buttonbox_size height width \
1646251912Sdteske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$msg_text" "$hline"
1647251190Sdteske	$DIALOG \
1648251190Sdteske		--title "$DIALOG_TITLE"         \
1649251190Sdteske		--backtitle "$DIALOG_BACKTITLE" \
1650251912Sdteske		--hline "$hline"                \
1651251190Sdteske		--ok-label "$msg_ok"            \
1652251190Sdteske		--msgbox "$msg_text" $height $width
1653238438Sdteske}
1654238438Sdteske
1655242107Sdteske############################################################ TEXTBOX FUNCTIONS
1656242107Sdteske
1657242107Sdteske# f_dialog_textbox $file
1658242107Sdteske#
1659242107Sdteske# Display the contents of $file (or an error if $file does not exist, etc.) in
1660242107Sdteske# a dialog(1) textbox (which has a scrollable region for the text). The textbox
1661242107Sdteske# remains until the user presses ENTER or ESC, acknowledging the modal dialog.
1662242107Sdteske#
1663242107Sdteske# If the user presses ENTER, the exit status is zero (success), otherwise if
1664242107Sdteske# the user presses ESC the exit status is 255.
1665242107Sdteske#
1666242107Sdteskef_dialog_textbox()
1667242107Sdteske{
1668242107Sdteske	local file="$1"
1669251190Sdteske	local contents height width retval
1670242107Sdteske
1671242107Sdteske	contents=$( cat "$file" 2>&1 )
1672242107Sdteske	retval=$?
1673242107Sdteske
1674251190Sdteske	f_dialog_buttonbox_size height width \
1675251190Sdteske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$contents"
1676242107Sdteske
1677242107Sdteske	if [ $retval -eq $SUCCESS ]; then
1678251190Sdteske		$DIALOG \
1679251190Sdteske			--title "$DIALOG_TITLE"         \
1680251190Sdteske			--backtitle "$DIALOG_BACKTITLE" \
1681251190Sdteske			--exit-label "$msg_ok"          \
1682251190Sdteske			--no-cancel                     \
1683251190Sdteske			--textbox "$file" $height $width
1684242107Sdteske	else
1685251190Sdteske		$DIALOG \
1686251190Sdteske			--title "$DIALOG_TITLE"         \
1687251190Sdteske			--backtitle "$DIALOG_BACKTITLE" \
1688251190Sdteske			--ok-label "$msg_ok"            \
1689251190Sdteske			--msgbox "$contents" $height $width
1690242107Sdteske	fi
1691242107Sdteske}
1692242107Sdteske
1693238438Sdteske############################################################ YESNO FUNCTIONS
1694238438Sdteske
1695251977Sdteske# f_dialog_yesno $msg_text [$hline]
1696238438Sdteske#
1697238438Sdteske# Display a dialog(1) Yes/No prompt to allow the user to make some decision.
1698238438Sdteske# The yesno prompt remains until the user presses ENTER or ESC, acknowledging
1699238438Sdteske# the modal dialog.
1700238438Sdteske#
1701238438Sdteske# If the user chooses YES the exit status is zero, or chooses NO the exit
1702238438Sdteske# status is one, or presses ESC the exit status is 255.
1703238438Sdteske#
1704238438Sdteskef_dialog_yesno()
1705238438Sdteske{
1706251977Sdteske	local msg_text="$1" height width
1707251977Sdteske	local hline="${2-$hline_arrows_tab_enter}"
1708245116Sdteske
1709245116Sdteske	f_interactive || return 0 # If non-interactive, return YES all the time
1710245116Sdteske
1711251190Sdteske	f_dialog_buttonbox_size height width \
1712251190Sdteske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$msg_text" "$hline"
1713238438Sdteske
1714238438Sdteske	if [ "$USE_XDIALOG" ]; then
1715251190Sdteske		$DIALOG \
1716251190Sdteske			--title "$DIALOG_TITLE"         \
1717251190Sdteske			--backtitle "$DIALOG_BACKTITLE" \
1718251190Sdteske			--hline "$hline"                \
1719251190Sdteske			--ok-label "$msg_yes"           \
1720251190Sdteske			--cancel-label "$msg_no"        \
1721251190Sdteske			--yesno "$msg_text" $height $width
1722238438Sdteske	else
1723251190Sdteske		$DIALOG \
1724251190Sdteske			--title "$DIALOG_TITLE"         \
1725251190Sdteske			--backtitle "$DIALOG_BACKTITLE" \
1726251190Sdteske			--hline "$hline"                \
1727251190Sdteske			--yes-label "$msg_yes"          \
1728251190Sdteske			--no-label "$msg_no"            \
1729251190Sdteske			--yesno "$msg_text" $height $width
1730238438Sdteske	fi
1731238438Sdteske}
1732238438Sdteske
1733251977Sdteske# f_dialog_noyes $msg_text [$hline]
1734238438Sdteske#
1735238438Sdteske# Display a dialog(1) No/Yes prompt to allow the user to make some decision.
1736238438Sdteske# The noyes prompt remains until the user presses ENTER or ESC, acknowledging
1737238438Sdteske# the modal dialog.
1738238438Sdteske#
1739238438Sdteske# If the user chooses YES the exit status is zero, or chooses NO the exit
1740238438Sdteske# status is one, or presses ESC the exit status is 255.
1741238438Sdteske#
1742238438Sdteske# NOTE: This is just like the f_dialog_yesno function except "No" is default.
1743238438Sdteske#
1744238438Sdteskef_dialog_noyes()
1745238438Sdteske{
1746251977Sdteske	local msg_text="$1" height width
1747251977Sdteske	local hline="${2-$hline_arrows_tab_enter}"
1748245116Sdteske
1749245116Sdteske	f_interactive || return 1 # If non-interactive, return NO all the time
1750245116Sdteske
1751251190Sdteske	f_dialog_buttonbox_size height width \
1752251190Sdteske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$msg_text" "$hline"
1753238438Sdteske
1754238438Sdteske	if [ "$USE_XDIALOG" ]; then
1755251190Sdteske		$DIALOG \
1756251190Sdteske			--title "$DIALOG_TITLE"         \
1757251190Sdteske			--backtitle "$DIALOG_BACKTITLE" \
1758251190Sdteske			--hline "$hline"                \
1759251190Sdteske			--default-no                    \
1760251190Sdteske			--ok-label "$msg_yes"           \
1761251190Sdteske			--cancel-label "$msg_no"        \
1762251190Sdteske			--yesno "$msg_text" $height $width
1763238438Sdteske	else
1764251190Sdteske		$DIALOG \
1765251190Sdteske			--title "$DIALOG_TITLE"         \
1766251190Sdteske			--backtitle "$DIALOG_BACKTITLE" \
1767251190Sdteske			--hline "$hline"                \
1768251190Sdteske			--defaultno                     \
1769251190Sdteske			--yes-label "$msg_yes"          \
1770251190Sdteske			--no-label "$msg_no"            \
1771251190Sdteske			--yesno "$msg_text" $height $width
1772238438Sdteske	fi
1773238438Sdteske}
1774238438Sdteske
1775238438Sdteske############################################################ INPUT FUNCTIONS
1776238438Sdteske
1777251242Sdteske# f_dialog_inputstr_store [-s] $text
1778238438Sdteske#
1779251242Sdteske# Store some text from a dialog(1) inputbox to be retrieved later by
1780251242Sdteske# f_dialog_inputstr_fetch(). If the first argument is `-s', the text is
1781251242Sdteske# sanitized before being stored.
1782251242Sdteske#
1783251242Sdteskef_dialog_inputstr_store()
1784251242Sdteske{
1785251242Sdteske	local sanitize=
1786251242Sdteske	[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
1787251242Sdteske	local text="$1"
1788251242Sdteske
1789251242Sdteske	# Sanitize the line before storing it if desired
1790251242Sdteske	[ "$sanitize" ] && f_dialog_line_sanitize text
1791251242Sdteske
1792251242Sdteske	setvar DIALOG_INPUTBOX_$$ "$text"
1793251242Sdteske}
1794251242Sdteske
1795251242Sdteske# f_dialog_inputstr_fetch [$var_to_set]
1796251242Sdteske#
1797238438Sdteske# Obtain the inputstr entered by the user from the most recently displayed
1798251242Sdteske# dialog(1) inputbox (previously stored with f_dialog_inputstr_store() above).
1799251242Sdteske# If $var_to_set is NULL or missing, output is printed to stdout (which is less
1800251242Sdteske# recommended due to performance degradation; in a loop for example).
1801238438Sdteske#
1802251242Sdteskef_dialog_inputstr_fetch()
1803238438Sdteske{
1804251242Sdteske	local __var_to_set="$1" __cp
1805251242Sdteske
1806251242Sdteske	debug= f_getvar DIALOG_INPUTBOX_$$ "${__var_to_set:-__cp}" # get data
1807240768Sdteske	setvar DIALOG_INPUTBOX_$$ "" # scrub memory in case data was sensitive
1808251242Sdteske
1809251242Sdteske	# Return the line on standard-out if desired
1810251242Sdteske	[ "$__var_to_set" ] || echo "$__cp"
1811251242Sdteske
1812238438Sdteske	return $SUCCESS
1813238438Sdteske}
1814238438Sdteske
1815251242Sdteske# f_dialog_input $var_to_set $prompt [$init [$hline]]
1816244548Sdteske#
1817244548Sdteske# Prompt the user with a dialog(1) inputbox to enter some value. The inputbox
1818244548Sdteske# remains until the the user presses ENTER or ESC, or otherwise ends the
1819251266Sdteske# editing session (by selecting `Cancel' for example).
1820244548Sdteske#
1821244548Sdteske# If the user presses ENTER, the exit status is zero (success), otherwise if
1822244548Sdteske# the user presses ESC the exit status is 255, or if the user chose Cancel, the
1823244548Sdteske# exit status is instead 1.
1824244548Sdteske#
1825244548Sdteske# NOTE: The hline should correspond to the type of data you want from the user.
1826244548Sdteske# NOTE: Should not be used to edit multiline values.
1827244548Sdteske#
1828244548Sdteskef_dialog_input()
1829244548Sdteske{
1830251242Sdteske	local __var_to_set="$1" __prompt="$2" __init="$3" __hline="$4"
1831251242Sdteske
1832251242Sdteske	# NOTE: Function name appended to prevent __var_{height,width} values
1833251242Sdteske	#       from becoming local (and thus preventing setvar from working).
1834251242Sdteske	local __height_input __width_input
1835251242Sdteske	f_dialog_inputbox_size __height_input __width_input \
1836251190Sdteske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" \
1837251242Sdteske		"$__prompt" "$__init" "$__hline"
1838244548Sdteske
1839251242Sdteske	local __opterm="--"
1840251242Sdteske	[ "$USE_XDIALOG" ] && __opterm=
1841244548Sdteske
1842251242Sdteske	local __dialog_input
1843251242Sdteske	__dialog_input=$(
1844251190Sdteske		$DIALOG \
1845251190Sdteske			--title "$DIALOG_TITLE"         \
1846251190Sdteske			--backtitle "$DIALOG_BACKTITLE" \
1847251242Sdteske			--hline "$__hline"              \
1848251190Sdteske			--ok-label "$msg_ok"            \
1849251190Sdteske			--cancel-label "$msg_cancel"    \
1850251242Sdteske			--inputbox "$__prompt"          \
1851251242Sdteske			$__height_input $__width_input  \
1852251242Sdteske			$__opterm "$__init"             \
1853244548Sdteske			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1854244548Sdteske	)
1855251242Sdteske	local __retval=$?
1856244548Sdteske
1857251242Sdteske	# Remove warnings and leading/trailing whitespace from user input
1858251242Sdteske	f_dialog_line_sanitize __dialog_input
1859244548Sdteske
1860251242Sdteske	setvar "$__var_to_set" "$__dialog_input"
1861251242Sdteske	return $__retval
1862244548Sdteske}
1863244548Sdteske
1864238438Sdteske############################################################ MENU FUNCTIONS
1865238438Sdteske
1866251236Sdteske# f_dialog_menutag_store [-s] $text
1867238438Sdteske#
1868251236Sdteske# Store some text from a dialog(1) menu to be retrieved later by
1869251236Sdteske# f_dialog_menutag_fetch(). If the first argument is `-s', the text is
1870251236Sdteske# sanitized before being stored.
1871251236Sdteske#
1872251236Sdteskef_dialog_menutag_store()
1873251236Sdteske{
1874251236Sdteske	local sanitize=
1875251236Sdteske	[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
1876251236Sdteske	local text="$1"
1877251236Sdteske
1878251236Sdteske	# Sanitize the menutag before storing it if desired
1879251236Sdteske	[ "$sanitize" ] && f_dialog_data_sanitize text
1880251236Sdteske
1881251236Sdteske	setvar DIALOG_MENU_$$ "$text"
1882251236Sdteske}
1883251236Sdteske
1884251236Sdteske# f_dialog_menutag_fetch [$var_to_set]
1885251236Sdteske#
1886238438Sdteske# Obtain the menutag chosen by the user from the most recently displayed
1887251236Sdteske# dialog(1) menu (previously stored with f_dialog_menutag_store() above). If
1888251236Sdteske# $var_to_set is NULL or missing, output is printed to stdout (which is less
1889251236Sdteske# recommended due to performance degradation; in a loop for example).
1890238438Sdteske#
1891251236Sdteskef_dialog_menutag_fetch()
1892238438Sdteske{
1893251236Sdteske	local __var_to_set="$1" __cp
1894251236Sdteske
1895251236Sdteske	debug= f_getvar DIALOG_MENU_$$ "${__var_to_set:-__cp}" # get the data
1896240768Sdteske	setvar DIALOG_MENU_$$ "" # scrub memory in case data was sensitive
1897251236Sdteske
1898251236Sdteske	# Return the data on standard-out if desired
1899251236Sdteske	[ "$__var_to_set" ] || echo "$__cp"
1900251236Sdteske
1901238438Sdteske	return $SUCCESS
1902238438Sdteske}
1903238438Sdteske
1904251361Sdteske# f_dialog_menuitem_store [-s] $text
1905251361Sdteske#
1906251361Sdteske# Store the item from a dialog(1) menu (see f_dialog_menutag2item()) to be
1907251361Sdteske# retrieved later by f_dialog_menuitem_fetch(). If the first argument is `-s',
1908251361Sdteske# the text is sanitized before being stored.
1909251361Sdteske#
1910251361Sdteskef_dialog_menuitem_store()
1911251361Sdteske{
1912251361Sdteske	local sanitize=
1913251361Sdteske	[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
1914251361Sdteske	local text="$1"
1915251361Sdteske
1916251361Sdteske	# Sanitize the menuitem before storing it if desired
1917251361Sdteske	[ "$sanitize" ] && f_dialog_data_sanitize text
1918251361Sdteske
1919251361Sdteske	setvar DIALOG_MENUITEM_$$ "$text"
1920251361Sdteske}
1921251361Sdteske
1922251361Sdteske# f_dialog_menuitem_fetch [$var_to_set]
1923251361Sdteske#
1924251361Sdteske# Obtain the menuitem chosen by the user from the most recently displayed
1925251361Sdteske# dialog(1) menu (previously stored with f_dialog_menuitem_store() above). If
1926251361Sdteske# $var_to_set is NULL or missing, output is printed to stdout (which is less
1927251361Sdteske# recommended due to performance degradation; in a loop for example).
1928251361Sdteske#
1929251361Sdteskef_dialog_menuitem_fetch()
1930251361Sdteske{
1931251361Sdteske	local __var_to_set="$1" __cp
1932251361Sdteske
1933251361Sdteske	debug= f_getvar DIALOG_MENUITEM_$$ "${__var_to_set:-__cp}" # get data
1934251361Sdteske	setvar DIALOG_MENUITEM_$$ "" # scrub memory in case data was sensitive
1935251361Sdteske
1936251361Sdteske	# Return the data on standard-out if desired
1937251361Sdteske	[ "$__var_to_set" ] || echo "$__cp"
1938251361Sdteske
1939251361Sdteske	return $SUCCESS
1940251361Sdteske}
1941251361Sdteske
1942251244Sdteske# f_dialog_default_store [-s] $text
1943251244Sdteske#
1944251244Sdteske# Store some text to be used later as the --default-item argument to dialog(1)
1945251244Sdteske# (or Xdialog(1)) for --menu, --checklist, and --radiolist widgets. Retrieve
1946251244Sdteske# the text later with f_dialog_menutag_fetch(). If the first argument is `-s',
1947251244Sdteske# the text is sanitized before being stored.
1948251244Sdteske#
1949251244Sdteskef_dialog_default_store()
1950251244Sdteske{
1951251244Sdteske	local sanitize=
1952251244Sdteske	[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
1953251244Sdteske	local text="$1"
1954251244Sdteske
1955251244Sdteske	# Sanitize the defaulitem before storing it if desired
1956251244Sdteske	[ "$sanitize" ] && f_dialog_data_sanitize text
1957251244Sdteske
1958251244Sdteske	setvar DEFAULTITEM_$$ "$text"
1959251244Sdteske}
1960251244Sdteske
1961251244Sdteske# f_dialog_default_fetch [$var_to_set]
1962251244Sdteske#
1963251244Sdteske# Obtain text to be used with the --default-item argument of dialog(1) (or
1964251244Sdteske# Xdialog(1)) (previously stored with f_dialog_default_store() above). If
1965251244Sdteske# $var_to_set is NULL or missing, output is printed to stdout (which is less
1966251244Sdteske# recommended due to performance degradation; in a loop for example).
1967251244Sdteske#
1968251244Sdteskef_dialog_default_fetch()
1969251244Sdteske{
1970251244Sdteske	local __var_to_set="$1" __cp
1971251244Sdteske
1972251244Sdteske	debug= f_getvar DEFAULTITEM_$$ "${__var_to_set:-__cp}" # get the data
1973251244Sdteske	setvar DEFAULTITEM_$$ "" # scrub memory in case data was sensitive
1974251244Sdteske
1975251244Sdteske	# Return the data on standard-out if desired
1976251244Sdteske	[ "$__var_to_set" ] || echo "$__cp"
1977251244Sdteske
1978251244Sdteske	return $SUCCESS
1979251244Sdteske}
1980251244Sdteske
1981238438Sdteske# f_dialog_menutag2item $tag_chosen $tag1 $item1 $tag2 $item2 ...
1982238438Sdteske#
1983238438Sdteske# To use the `--menu' option of dialog(1) you must pass an ordered list of
1984238438Sdteske# tag/item pairs on the command-line. When the user selects a menu option the
1985238438Sdteske# tag for that item is printed to stderr.
1986238438Sdteske#
1987238438Sdteske# This function allows you to dereference the tag chosen by the user back into
1988238438Sdteske# the item associated with said tag.
1989238438Sdteske#
1990238438Sdteske# Pass the tag chosen by the user as the first argument, followed by the
1991238438Sdteske# ordered list of tag/item pairs (HINT: use the same tag/item list as was
1992238438Sdteske# passed to dialog(1) for consistency).
1993238438Sdteske#
1994238438Sdteske# If the tag cannot be found, NULL is returned.
1995238438Sdteske#
1996238438Sdteskef_dialog_menutag2item()
1997238438Sdteske{
1998238438Sdteske	local tag="$1" tagn item
1999238438Sdteske	shift 1 # tag
2000238438Sdteske
2001238438Sdteske	while [ $# -gt 0 ]; do
2002238438Sdteske		tagn="$1"
2003238438Sdteske		item="$2"
2004238438Sdteske		shift 2 # tagn/item
2005238438Sdteske
2006238438Sdteske		if [ "$tag" = "$tagn" ]; then
2007238438Sdteske			echo "$item"
2008238438Sdteske			return $SUCCESS
2009238438Sdteske		fi
2010238438Sdteske	done
2011238438Sdteske	return $FAILURE
2012238438Sdteske}
2013238438Sdteske
2014238438Sdteske# f_dialog_menutag2item_with_help $tag_chosen $tag1 $item1 $help1 \
2015238438Sdteske#                                             $tag2 $item2 $help2 ...
2016238438Sdteske#
2017238438Sdteske# To use the `--menu' option of dialog(1) with the `--item-help' option, you
2018238438Sdteske# must pass an ordered list of tag/item/help triplets on the command-line. When
2019238438Sdteske# the user selects a menu option the tag for that item is printed to stderr.
2020238438Sdteske#
2021238438Sdteske# This function allows you to dereference the tag chosen by the user back into
2022238438Sdteske# the item associated with said tag (help is discarded/ignored).
2023238438Sdteske#
2024238438Sdteske# Pass the tag chosen by the user as the first argument, followed by the
2025238438Sdteske# ordered list of tag/item/help triplets (HINT: use the same tag/item/help list
2026238438Sdteske# as was passed to dialog(1) for consistency).
2027238438Sdteske#
2028238438Sdteske# If the tag cannot be found, NULL is returned.
2029238438Sdteske#
2030238438Sdteskef_dialog_menutag2item_with_help()
2031238438Sdteske{
2032238438Sdteske	local tag="$1" tagn item
2033238438Sdteske	shift 1 # tag
2034238438Sdteske
2035238438Sdteske	while [ $# -gt 0 ]; do
2036238438Sdteske		tagn="$1"
2037238438Sdteske		item="$2"
2038238438Sdteske		shift 3 # tagn/item/help
2039238438Sdteske
2040238438Sdteske		if [ "$tag" = "$tagn" ]; then
2041238438Sdteske			echo "$item"
2042238438Sdteske			return $SUCCESS
2043238438Sdteske		fi
2044238438Sdteske	done
2045238438Sdteske	return $FAILURE
2046238438Sdteske}
2047238438Sdteske
2048238438Sdteske# f_dialog_menutag2index $tag_chosen $tag1 $item1 $tag2 $item2 ...
2049238438Sdteske#
2050238438Sdteske# To use the `--menu' option of dialog(1) you must pass an ordered list of
2051238438Sdteske# tag/item pairs on the command-line. When the user selects a menu option the
2052238438Sdteske# tag for that item is printed to stderr.
2053238438Sdteske#
2054238438Sdteske# This function allows you to dereference the tag chosen by the user back into
2055238438Sdteske# the index associated with said tag. The index is the one-based tag/item pair
2056238438Sdteske# array position within the ordered list of tag/item pairs passed to dialog(1).
2057238438Sdteske#
2058238438Sdteske# Pass the tag chosen by the user as the first argument, followed by the
2059238438Sdteske# ordered list of tag/item pairs (HINT: use the same tag/item list as was
2060238438Sdteske# passed to dialog(1) for consistency).
2061238438Sdteske#
2062238438Sdteske# If the tag cannot be found, NULL is returned.
2063238438Sdteske#
2064238438Sdteskef_dialog_menutag2index()
2065238438Sdteske{
2066238438Sdteske	local tag="$1" tagn n=1
2067238438Sdteske	shift 1 # tag
2068238438Sdteske
2069238438Sdteske	while [ $# -gt 0 ]; do
2070238438Sdteske		tagn="$1"
2071238438Sdteske		shift 2 # tagn/item
2072238438Sdteske
2073238438Sdteske		if [ "$tag" = "$tagn" ]; then
2074238438Sdteske			echo $n
2075238438Sdteske			return $SUCCESS
2076238438Sdteske		fi
2077238438Sdteske		n=$(( $n + 1 ))
2078238438Sdteske	done
2079238438Sdteske	return $FAILURE
2080238438Sdteske}
2081238438Sdteske
2082238438Sdteske# f_dialog_menutag2index_with_help $tag_chosen $tag1 $item1 $help1 \
2083238438Sdteske#                                              $tag2 $item2 $help2 ...
2084238438Sdteske#
2085238438Sdteske# To use the `--menu' option of dialog(1) with the `--item-help' option, you
2086238438Sdteske# must pass an ordered list of tag/item/help triplets on the command-line. When
2087238438Sdteske# the user selects a menu option the tag for that item is printed to stderr.
2088238438Sdteske#
2089238438Sdteske# This function allows you to dereference the tag chosen by the user back into
2090238438Sdteske# the index associated with said tag. The index is the one-based tag/item/help
2091238438Sdteske# triplet array position within the ordered list of tag/item/help triplets
2092238438Sdteske# passed to dialog(1).
2093238438Sdteske#
2094238438Sdteske# Pass the tag chosen by the user as the first argument, followed by the
2095238438Sdteske# ordered list of tag/item/help triplets (HINT: use the same tag/item/help list
2096238438Sdteske# as was passed to dialog(1) for consistency).
2097238438Sdteske#
2098238438Sdteske# If the tag cannot be found, NULL is returned.
2099238438Sdteske#
2100238438Sdteskef_dialog_menutag2index_with_help()
2101238438Sdteske{
2102238438Sdteske	local tag="$1" tagn n=1
2103238438Sdteske	shift 1 # tag
2104238438Sdteske
2105238438Sdteske	while [ $# -gt 0 ]; do
2106238438Sdteske		tagn="$1"
2107238438Sdteske		shift 3 # tagn/item/help
2108238438Sdteske
2109238438Sdteske		if [ "$tag" = "$tagn" ]; then
2110238438Sdteske			echo $n
2111238438Sdteske			return $SUCCESS
2112238438Sdteske		fi
2113238438Sdteske		n=$(( $n + 1 ))
2114238438Sdteske	done
2115238438Sdteske	return $FAILURE
2116238438Sdteske}
2117238438Sdteske
2118279618Sdteske# f_dialog_menutag2help $tag_chosen $tag1 $item1 $help1 $tag2 $item2 $help2 ...
2119279618Sdteske#
2120279618Sdteske# To use the `--menu' option of dialog(1) with the `--item-help' option, you
2121279618Sdteske# must pass an ordered list of tag/item/help triplets on the command-line. When
2122279618Sdteske# the user selects a menu option the tag for that item is printed to stderr.
2123279618Sdteske#
2124279618Sdteske# This function allows you to dereference the tag chosen by the user back into
2125279618Sdteske# the help associated with said tag (item is discarded/ignored).
2126279618Sdteske#
2127279618Sdteske# Pass the tag chosen by the user as the first argument, followed by the
2128279618Sdteske# ordered list of tag/item/help triplets (HINT: use the same tag/item/help list
2129279618Sdteske# as was passed to dialog(1) for consistency).
2130279618Sdteske#
2131279618Sdteske# If the tag cannot be found, NULL is returned.
2132279618Sdteske#
2133279618Sdteskef_dialog_menutag2help()
2134279618Sdteske{
2135279618Sdteske	local tag="$1" tagn help
2136279618Sdteske	shift 1 # tag
2137279618Sdteske
2138279618Sdteske	while [ $# -gt 0 ]; do
2139279618Sdteske		tagn="$1"
2140279618Sdteske		help="$3"
2141279618Sdteske		shift 3 # tagn/item/help
2142279618Sdteske
2143279618Sdteske		if [ "$tag" = "$tagn" ]; then
2144279618Sdteske			echo "$help"
2145279618Sdteske			return $SUCCESS
2146279618Sdteske		fi
2147279618Sdteske	done
2148279618Sdteske	return $FAILURE
2149279618Sdteske}
2150279618Sdteske
2151238438Sdteske############################################################ INIT FUNCTIONS
2152238438Sdteske
2153238438Sdteske# f_dialog_init
2154238438Sdteske#
2155238438Sdteske# Initialize (or re-initialize) the dialog module after setting/changing any
2156238438Sdteske# of the following environment variables:
2157238438Sdteske#
2158238438Sdteske# 	USE_XDIALOG   Either NULL or Non-NULL. If given a value will indicate
2159238438Sdteske# 	              that Xdialog(1) should be used instead of dialog(1).
2160238438Sdteske#
2161238438Sdteske# 	SECURE        Either NULL or Non-NULL. If given a value will indicate
2162238438Sdteske# 	              that (while running as root) sudo(8) authentication is
2163238438Sdteske# 	              required to proceed.
2164238438Sdteske#
2165253117Sdteske# Also reads ~/.dialogrc for the following information:
2166253117Sdteske#
2167253117Sdteske# 	NO_SHADOW     Either NULL or Non-NULL. If use_shadow is OFF (case-
2168253117Sdteske# 	              insensitive) in ~/.dialogrc this is set to "1" (otherwise
2169253117Sdteske# 	              unset).
2170253117Sdteske#
2171238438Sdteskef_dialog_init()
2172238438Sdteske{
2173260678Sdteske	local funcname=f_dialog_init
2174260678Sdteske
2175238438Sdteske	DIALOG_SELF_INITIALIZE=
2176256361Sdteske	USE_DIALOG=1
2177238438Sdteske
2178238438Sdteske	#
2179240768Sdteske	# Clone terminal stdout so we can redirect to it from within sub-shells
2180240768Sdteske	#
2181240768Sdteske	eval exec $DIALOG_TERMINAL_PASSTHRU_FD\>\&1
2182240768Sdteske
2183240768Sdteske	#
2184250633Sdteske	# Add `-S' and `-X' to the list of standard arguments supported by all
2185250633Sdteske	#
2186250633Sdteske	case "$GETOPTS_STDARGS" in
2187250633Sdteske	*SX*) : good ;; # already present
2188250633Sdteske	   *) GETOPTS_STDARGS="${GETOPTS_STDARGS}SX"
2189250633Sdteske	esac
2190250633Sdteske
2191250633Sdteske	#
2192238438Sdteske	# Process stored command-line arguments
2193238438Sdteske	#
2194272230Sdteske	# NB: Using backticks instead of $(...) for portability since Linux
2195272230Sdteske	#     bash(1) balks at the right parentheses encountered in the case-
2196272230Sdteske	#     statement (incorrectly interpreting it as the close of $(...)).
2197272230Sdteske	#
2198250633Sdteske	f_dprintf "f_dialog_init: ARGV=[%s] GETOPTS_STDARGS=[%s]" \
2199250633Sdteske	          "$ARGV" "$GETOPTS_STDARGS"
2200272230Sdteske	SECURE=`set -- $ARGV
2201272230Sdteske		OPTIND=1
2202251265Sdteske		while getopts \
2203251265Sdteske			"$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" \
2204251265Sdteske		flag > /dev/null; do
2205238438Sdteske			case "$flag" in
2206252178Sdteske			S) echo 1 ;;
2207238438Sdteske			esac
2208238438Sdteske		done
2209272230Sdteske	` # END-BACKTICK
2210272230Sdteske	USE_XDIALOG=`set -- $ARGV
2211272230Sdteske		OPTIND=1
2212251265Sdteske		while getopts \
2213251265Sdteske			"$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" \
2214251265Sdteske		flag > /dev/null; do
2215238438Sdteske			case "$flag" in
2216252178Sdteske			S|X) echo 1 ;;
2217238438Sdteske			esac
2218238438Sdteske		done
2219272230Sdteske	` # END-BACKTICK
2220250633Sdteske	f_dprintf "f_dialog_init: SECURE=[%s] USE_XDIALOG=[%s]" \
2221250633Sdteske	          "$SECURE" "$USE_XDIALOG"
2222238438Sdteske
2223238438Sdteske	#
2224238438Sdteske	# Process `-X' command-line option
2225238438Sdteske	#
2226256361Sdteske	[ "$USE_XDIALOG" ] && DIALOG=Xdialog USE_DIALOG=
2227238438Sdteske
2228238438Sdteske	#
2229238438Sdteske	# Sanity check, or die gracefully
2230238438Sdteske	#
2231238438Sdteske	if ! f_have $DIALOG; then
2232238438Sdteske		unset USE_XDIALOG
2233251267Sdteske		local failed_dialog="$DIALOG"
2234238438Sdteske		DIALOG=dialog
2235238438Sdteske		f_die 1 "$msg_no_such_file_or_directory" "$pgm" "$failed_dialog"
2236238438Sdteske	fi
2237238438Sdteske
2238238438Sdteske	#
2239253117Sdteske	# Read ~/.dialogrc (unless using Xdialog(1)) for properties
2240253117Sdteske	#
2241253118Sdteske	if [ -f ~/.dialogrc -a ! "$USE_XDIALOG" ]; then
2242253117Sdteske		eval "$(
2243253117Sdteske			awk -v param=use_shadow -v expect=OFF \
2244253117Sdteske			    -v set="NO_SHADOW=1" '
2245253117Sdteske			!/^[[:space:]]*(#|$)/ && \
2246253117Sdteske			tolower($1) ~ "^"param"(=|$)" && \
2247253117Sdteske			/[^#]*=/ {
2248253117Sdteske				sub(/^[^=]*=[[:space:]]*/, "")
2249253117Sdteske				if ( toupper($1) == expect ) print set";"
2250253117Sdteske			}' ~/.dialogrc
2251253117Sdteske		)"
2252253117Sdteske	fi
2253253117Sdteske
2254253117Sdteske	#
2255238438Sdteske	# If we're already running as root but we got there by way of sudo(8)
2256238438Sdteske	# and we have X11, we should merge the xauth(1) credentials from our
2257238438Sdteske	# original user.
2258238438Sdteske	#
2259238438Sdteske	if [ "$USE_XDIALOG" ] &&
2260238438Sdteske	   [ "$( id -u )" = "0" ] &&
2261238438Sdteske	   [ "$SUDO_USER" -a "$DISPLAY" ]
2262238438Sdteske	then
2263238438Sdteske		if ! f_have xauth; then
2264238438Sdteske			# Die gracefully, as we [likely] can't use Xdialog(1)
2265238438Sdteske			unset USE_XDIALOG
2266238438Sdteske			DIALOG=dialog
2267238438Sdteske			f_die 1 "$msg_no_such_file_or_directory" "$pgm" "xauth"
2268238438Sdteske		fi
2269260678Sdteske		HOSTNAME=$( hostname )
2270251267Sdteske		local displaynum="${DISPLAY#*:}"
2271238438Sdteske		eval xauth -if \~$SUDO_USER/.Xauthority extract - \
2272238438Sdteske			\"\$HOSTNAME/unix:\$displaynum\" \
2273238438Sdteske			\"\$HOSTNAME:\$displaynum\" | sudo sh -c 'xauth -ivf \
2274240783Sdteske			~root/.Xauthority merge - > /dev/null 2>&1'
2275238438Sdteske	fi
2276238438Sdteske
2277238438Sdteske	#
2278238438Sdteske	# Probe Xdialog(1) for maximum height/width constraints, or die
2279238438Sdteske	# gracefully
2280238438Sdteske	#
2281238438Sdteske	if [ "$USE_XDIALOG" ]; then
2282251267Sdteske		local maxsize
2283260678Sdteske		if ! f_eval_catch -dk maxsize $funcname "$DIALOG" \
2284260678Sdteske			'LANG= LC_ALL= %s --print-maxsize' "$DIALOG"
2285238438Sdteske		then
2286238438Sdteske			# Xdialog(1) failed, fall back to dialog(1)
2287238438Sdteske			unset USE_XDIALOG
2288251190Sdteske
2289251190Sdteske			# Display the error message produced by Xdialog(1)
2290251190Sdteske			local height width
2291251190Sdteske			f_dialog_buttonbox_size height width \
2292251190Sdteske				"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$maxsize"
2293251190Sdteske			dialog \
2294251190Sdteske				--title "$DIALOG_TITLE"         \
2295251190Sdteske				--backtitle "$DIALOG_BACKTITLE" \
2296251190Sdteske				--ok-label "$msg_ok"            \
2297251190Sdteske				--msgbox "$maxsize" $height $width
2298238438Sdteske			exit $FAILURE
2299238438Sdteske		fi
2300238438Sdteske
2301238438Sdteske		XDIALOG_MAXSIZE=$(
2302238438Sdteske			set -- ${maxsize##*:}
2303238438Sdteske
2304238438Sdteske			height=${1%,}
2305238438Sdteske			width=$2
2306238438Sdteske
2307238438Sdteske			echo $height $width
2308238438Sdteske		)
2309238438Sdteske	fi
2310238438Sdteske
2311238438Sdteske	#
2312238438Sdteske	# If using Xdialog(1), swap DIALOG_TITLE with DIALOG_BACKTITLE.
2313238438Sdteske	# The reason for this is because many dialog(1) applications use
2314238438Sdteske	# --backtitle for the program name (which is better suited as
2315238438Sdteske	# --title with Xdialog(1)).
2316238438Sdteske	#
2317238438Sdteske	if [ "$USE_XDIALOG" ]; then
2318251267Sdteske		local _DIALOG_TITLE="$DIALOG_TITLE"
2319238438Sdteske		DIALOG_TITLE="$DIALOG_BACKTITLE"
2320238438Sdteske		DIALOG_BACKTITLE="$_DIALOG_TITLE"
2321238438Sdteske	fi
2322244675Sdteske
2323244675Sdteske	f_dprintf "f_dialog_init: dialog(1) API initialized."
2324238438Sdteske}
2325238438Sdteske
2326243635Sdteske############################################################ MAIN
2327243635Sdteske
2328244804Sdteske#
2329244804Sdteske# Self-initialize unless requested otherwise
2330244804Sdteske#
2331244675Sdteskef_dprintf "%s: DIALOG_SELF_INITIALIZE=[%s]" \
2332244675Sdteske          dialog.subr "$DIALOG_SELF_INITIALIZE"
2333244558Sdteskecase "$DIALOG_SELF_INITIALIZE" in
2334244558Sdteske""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
2335244558Sdteske*) f_dialog_init
2336244558Sdteskeesac
2337243635Sdteske
2338244675Sdteskef_dprintf "%s: Successfully loaded." dialog.subr
2339244675Sdteske
2340238438Sdteskefi # ! $_DIALOG_SUBR
2341