1ab2043b8SDevin Teskeif [ ! "$_DIALOG_SUBR" ]; then _DIALOG_SUBR=1 2ab2043b8SDevin Teske# 3ab2043b8SDevin Teske# Copyright (c) 2006-2012 Devin Teske 4ab2043b8SDevin Teske# All Rights Reserved. 5ab2043b8SDevin Teske# 6ab2043b8SDevin Teske# Redistribution and use in source and binary forms, with or without 7ab2043b8SDevin Teske# modification, are permitted provided that the following conditions 8ab2043b8SDevin Teske# are met: 9ab2043b8SDevin Teske# 1. Redistributions of source code must retain the above copyright 10ab2043b8SDevin Teske# notice, this list of conditions and the following disclaimer. 11ab2043b8SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright 12ab2043b8SDevin Teske# notice, this list of conditions and the following disclaimer in the 13ab2043b8SDevin Teske# documentation and/or other materials provided with the distribution. 14ab2043b8SDevin Teske# 15ab2043b8SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16ab2043b8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17ab2043b8SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18ab2043b8SDevin Teske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19ab2043b8SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20ab2043b8SDevin Teske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21ab2043b8SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22ab2043b8SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23ab2043b8SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24ab2043b8SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25ab2043b8SDevin Teske# SUCH DAMAGE. 26ab2043b8SDevin Teske# 27ab2043b8SDevin Teske# $FreeBSD$ 28ab2043b8SDevin Teske# 29ab2043b8SDevin Teske############################################################ INCLUDES 30ab2043b8SDevin Teske 31ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig" 32ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1 33*56961fd7SDevin Teskef_dprintf "%s: loading includes..." dialog.subr 34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/strings.subr 35ab2043b8SDevin Teske 36ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" 37ab2043b8SDevin Teskef_include_lang $BSDCFG_LIBE/include/messages.subr 38ab2043b8SDevin Teske 39ab2043b8SDevin Teske############################################################ CONFIGURATION 40ab2043b8SDevin Teske 41ab2043b8SDevin Teske# 4289498fdfSDevin Teske# Default file descriptor to link to stdout for dialog(1) passthru allowing 4389498fdfSDevin Teske# execution of dialog from within a sub-shell (so-long as its standard output 4489498fdfSDevin Teske# is explicitly redirected to this file descriptor). 45ab2043b8SDevin Teske# 46526e1dc1SDevin Teske: ${DIALOG_TERMINAL_PASSTHRU_FD:=${TERMINAL_STDOUT_PASSTHRU:-3}} 47ab2043b8SDevin Teske 48ab2043b8SDevin Teske############################################################ GLOBALS 49ab2043b8SDevin Teske 50ab2043b8SDevin Teske# 51ab2043b8SDevin Teske# Default name of dialog(1) utility 52ab2043b8SDevin Teske# NOTE: This is changed to "Xdialog" by the optional `-X' argument 53ab2043b8SDevin Teske# 54ab2043b8SDevin TeskeDIALOG="dialog" 55ab2043b8SDevin Teske 56ab2043b8SDevin Teske# 57ab2043b8SDevin Teske# Default dialog(1) title and backtitle text 58ab2043b8SDevin Teske# 59ab2043b8SDevin TeskeDIALOG_TITLE="$pgm" 60ab2043b8SDevin TeskeDIALOG_BACKTITLE="bsdconfig" 61ab2043b8SDevin Teske 62ab2043b8SDevin Teske# 63ab2043b8SDevin Teske# Settings used while interacting with dialog(1) 64ab2043b8SDevin Teske# 65ab2043b8SDevin TeskeDIALOG_MENU_TAGS="123456789ABCDEFGHIJKLMNOPQRSTUVWYZabcdefghijklmnopqrstuvwxyz" 66ab2043b8SDevin Teske 67ab2043b8SDevin Teske# 68ab2043b8SDevin Teske# Declare that we are fully-compliant with Xdialog(1) by unset'ing all 69ab2043b8SDevin Teske# compatibility settings. 70ab2043b8SDevin Teske# 71ab2043b8SDevin Teskeunset XDIALOG_HIGH_DIALOG_COMPAT 72ab2043b8SDevin Teskeunset XDIALOG_FORCE_AUTOSIZE 73ab2043b8SDevin Teskeunset XDIALOG_INFOBOX_TIMEOUT 74ab2043b8SDevin Teske 75ab2043b8SDevin Teske# 7679ec1cd5SDevin Teske# Default behavior is to call f_dialog_init() automatically when loaded. 77ab2043b8SDevin Teske# 78ab2043b8SDevin Teske: ${DIALOG_SELF_INITIALIZE=1} 79ab2043b8SDevin Teske 80ab2043b8SDevin Teske############################################################ GENERIC FUNCTIONS 81ab2043b8SDevin Teske 82ab2043b8SDevin Teske# f_dialog_title [$new_title] 83ab2043b8SDevin Teske# 84ab2043b8SDevin Teske# Set the title of future dialog(1) ($DIALOG_TITLE) or backtitle of Xdialog(1) 85ab2043b8SDevin Teske# ($DIALOG_BACKTITLE) invocations. If no arguments are given or the first 86ab2043b8SDevin Teske# argument is NULL, the current title is returned. 87ab2043b8SDevin Teske# 88ab2043b8SDevin Teske# Each time this function is called, a backup of the current values is made 89ab2043b8SDevin Teske# allowing a one-time (single-level) restoration of the previous title using the 90ab2043b8SDevin Teske# f_dialog_title_restore() function (below). 91ab2043b8SDevin Teske# 92ab2043b8SDevin Teskef_dialog_title() 93ab2043b8SDevin Teske{ 94ab2043b8SDevin Teske local new_title="$1" 95ab2043b8SDevin Teske 96540bebd7SDevin Teske if [ "${1+set}" ]; then 97ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 98ab2043b8SDevin Teske _DIALOG_BACKTITLE="$DIALOG_BACKTITLE" 99ab2043b8SDevin Teske DIALOG_BACKTITLE="$new_title" 100ab2043b8SDevin Teske else 101ab2043b8SDevin Teske _DIALOG_TITLE="$DIALOG_TITLE" 102ab2043b8SDevin Teske DIALOG_TITLE="$new_title" 103ab2043b8SDevin Teske fi 104ab2043b8SDevin Teske else 105ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 106ab2043b8SDevin Teske echo "$DIALOG_BACKTITLE" 107ab2043b8SDevin Teske else 108ab2043b8SDevin Teske echo "$DIALOG_TITLE" 109ab2043b8SDevin Teske fi 110ab2043b8SDevin Teske fi 111ab2043b8SDevin Teske} 112ab2043b8SDevin Teske 113ab2043b8SDevin Teske# f_dialog_title_restore 114ab2043b8SDevin Teske# 115ab2043b8SDevin Teske# Restore the previous title set by the last call to f_dialog_title(). 116ab2043b8SDevin Teske# Restoration is non-recursive and only works to restore the most-recent title. 117ab2043b8SDevin Teske# 118ab2043b8SDevin Teskef_dialog_title_restore() 119ab2043b8SDevin Teske{ 120ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 121ab2043b8SDevin Teske DIALOG_BACKTITLE="$_DIALOG_BACKTITLE" 122ab2043b8SDevin Teske else 123ab2043b8SDevin Teske DIALOG_TITLE="$_DIALOG_TITLE" 124ab2043b8SDevin Teske fi 125ab2043b8SDevin Teske} 126ab2043b8SDevin Teske 127ab2043b8SDevin Teske# f_dialog_backtitle [$new_backtitle] 128ab2043b8SDevin Teske# 129ab2043b8SDevin Teske# Set the backtitle of future dialog(1) ($DIALOG_BACKTITLE) or title of 130ab2043b8SDevin Teske# Xdialog(1) ($DIALOG_TITLE) invocations. If no arguments are given or the 131ab2043b8SDevin Teske# first argument is NULL, the current backtitle is returned. 132ab2043b8SDevin Teske# 133ab2043b8SDevin Teskef_dialog_backtitle() 134ab2043b8SDevin Teske{ 135ab2043b8SDevin Teske local new_backtitle="$1" 136ab2043b8SDevin Teske 137540bebd7SDevin Teske if [ "${1+set}" ]; then 138ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 139ab2043b8SDevin Teske _DIALOG_TITLE="$DIALOG_TITLE" 140ab2043b8SDevin Teske DIALOG_TITLE="$new_backtitle" 141ab2043b8SDevin Teske else 142ab2043b8SDevin Teske _DIALOG_BACKTITLE="$DIALOG_BACKTITLE" 143ab2043b8SDevin Teske DIALOG_BACKTITLE="$new_backtitle" 144ab2043b8SDevin Teske fi 145ab2043b8SDevin Teske else 146ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 147ab2043b8SDevin Teske echo "$DIALOG_TITLE" 148ab2043b8SDevin Teske else 149ab2043b8SDevin Teske echo "$DIALOG_BACKTITLE" 150ab2043b8SDevin Teske fi 151ab2043b8SDevin Teske fi 152ab2043b8SDevin Teske} 153ab2043b8SDevin Teske 154ab2043b8SDevin Teske# f_dialog_backtitle_restore 155ab2043b8SDevin Teske# 156ab2043b8SDevin Teske# Restore the previous backtitle set by the last call to f_dialog_backtitle(). 157ab2043b8SDevin Teske# Restoration is non-recursive and only works to restore the most-recent 158ab2043b8SDevin Teske# backtitle. 159ab2043b8SDevin Teske# 160ab2043b8SDevin Teskef_dialog_backtitle_restore() 161ab2043b8SDevin Teske{ 162ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 163ab2043b8SDevin Teske DIALOG_TITLE="$_DIALOG_TITLE" 164ab2043b8SDevin Teske else 165ab2043b8SDevin Teske DIALOG_BACKTITLE="$_DIALOG_BACKTITLE" 166ab2043b8SDevin Teske fi 167ab2043b8SDevin Teske} 168ab2043b8SDevin Teske 169ab2043b8SDevin Teske############################################################ SIZE FUNCTIONS 170ab2043b8SDevin Teske 171ab2043b8SDevin Teske# f_dialog_infobox_size $title $backtitle $prompt [$hline] 172ab2043b8SDevin Teske# 173ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of 174ab2043b8SDevin Teske# `--infobox' boxes sensibly. 175ab2043b8SDevin Teske# 176ab2043b8SDevin Teske# This function helps solve this issue by taking as arguments (in order of 177ab2043b8SDevin Teske# appearance) the title, backtitle, prompt, and [optionally] hline returning 178ab2043b8SDevin Teske# the optimal width and height for the box (not exceeding the actual terminal 179ab2043b8SDevin Teske# width or height). 180ab2043b8SDevin Teske# 181ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by 182ab2043b8SDevin Teske# dialog(1). 183ab2043b8SDevin Teske# 184ab2043b8SDevin Teske# Output is in the format of "height width". 185ab2043b8SDevin Teske# 186ab2043b8SDevin Teskef_dialog_infobox_size() 187ab2043b8SDevin Teske{ 188ab2043b8SDevin Teske local title="$1" btitle="$2" prompt="$3" hline="$4" n=0 189ab2043b8SDevin Teske local min_width max_size 190ab2043b8SDevin Teske 191ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 192ab2043b8SDevin Teske min_width=35 193ab2043b8SDevin Teske max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION 194ab2043b8SDevin Teske else 195ab2043b8SDevin Teske min_width=24 196ab2043b8SDevin Teske max_size=$( stty size ) # usually "24 80" 197ab2043b8SDevin Teske fi 198ab2043b8SDevin Teske 199ab2043b8SDevin Teske local max_height="${max_size%%[$IFS]*}" 200ab2043b8SDevin Teske local max_width="${max_size##*[$IFS]}" 201ab2043b8SDevin Teske local height width=$min_width 202ab2043b8SDevin Teske 203ab2043b8SDevin Teske # 204ab2043b8SDevin Teske # Bump width for long titles (but don't exceed terminal width). 205ab2043b8SDevin Teske # 206ab2043b8SDevin Teske n=$(( ${#title} + 4 )) 207ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 208ab2043b8SDevin Teske # Add 16.6% width for Xdialog(1) 209ab2043b8SDevin Teske [ "$USE_XDIALOG" ] && n=$(( $n + $n / 6 )) 210ab2043b8SDevin Teske 211ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 212ab2043b8SDevin Teske width=$n 213ab2043b8SDevin Teske else 214ab2043b8SDevin Teske width=$max_width 215ab2043b8SDevin Teske fi 216ab2043b8SDevin Teske fi 217ab2043b8SDevin Teske 218ab2043b8SDevin Teske # 219ab2043b8SDevin Teske # For Xdialog(1), bump width for long backtitles (which appear within 220ab2043b8SDevin Teske # the window; don't exceed maximum width). 221ab2043b8SDevin Teske # 222ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 223ab2043b8SDevin Teske n=$(( ${#btitle} + 4 )) 224ab2043b8SDevin Teske n=$(( $n + $n / 6 )) 225ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 226ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 227ab2043b8SDevin Teske width=$n 228ab2043b8SDevin Teske else 229ab2043b8SDevin Teske width=$max_width 230ab2043b8SDevin Teske fi 231ab2043b8SDevin Teske fi 232ab2043b8SDevin Teske fi 233ab2043b8SDevin Teske 234ab2043b8SDevin Teske # 235ab2043b8SDevin Teske # Bump width for long prompts (if not already at maximum width). 236ab2043b8SDevin Teske # 237ab2043b8SDevin Teske if [ $width -lt $max_width ]; then 238ab2043b8SDevin Teske n=$( echo "$prompt" | f_longest_line_length ) 239ab2043b8SDevin Teske n=$(( $n + 4 )) 240ab2043b8SDevin Teske 241ab2043b8SDevin Teske # Add 16.6% width for Xdialog(1) 242ab2043b8SDevin Teske [ "$USE_XDIALOG" ] && n=$(( $n + $n / 6 )) 243ab2043b8SDevin Teske 244ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 245ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 246ab2043b8SDevin Teske width=$n 247ab2043b8SDevin Teske else 248ab2043b8SDevin Teske width=$max_width 249ab2043b8SDevin Teske fi 250ab2043b8SDevin Teske fi 251ab2043b8SDevin Teske fi 252ab2043b8SDevin Teske 253ab2043b8SDevin Teske # 254ab2043b8SDevin Teske # Bump width for long hlines (if not already at maximum width). 255ab2043b8SDevin Teske # NOTE: Though Xdialog(1) supports `--hline', it's not currently used. 256ab2043b8SDevin Teske # 257ab2043b8SDevin Teske if [ ! "$USE_XDIALOG" ]; then 258ab2043b8SDevin Teske if [ $width -lt $max_width ]; then 259ab2043b8SDevin Teske n=$(( ${#hline} + 10 )) 260ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 261ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 262ab2043b8SDevin Teske width=$n 263ab2043b8SDevin Teske else 264ab2043b8SDevin Teske width=$max_width 265ab2043b8SDevin Teske fi 266ab2043b8SDevin Teske fi 267ab2043b8SDevin Teske fi 268ab2043b8SDevin Teske fi 269ab2043b8SDevin Teske 270ab2043b8SDevin Teske # 271ab2043b8SDevin Teske # Set height based on number of rows in prompt 272ab2043b8SDevin Teske # 273de8fd13bSDevin Teske height=$( echo -n "$prompt" | f_number_of_lines ) 274ab2043b8SDevin Teske height=$(( $height + 2 )) 275ab2043b8SDevin Teske 276ab2043b8SDevin Teske # 277ab2043b8SDevin Teske # For Xdialog(1) bump height if backtitle is enabled (displayed in the 278ab2043b8SDevin Teske # X11 window with a separator line between the backtitle and msg text) 279ab2043b8SDevin Teske # 280ab2043b8SDevin Teske if [ "$USE_XDIALOG" -a "$btitle" ]; then 281ab2043b8SDevin Teske n=$( echo "$btitle" | f_number_of_lines ) 282ab2043b8SDevin Teske height=$(( $height + $n + 2 )) 283ab2043b8SDevin Teske fi 284ab2043b8SDevin Teske 285ab2043b8SDevin Teske # Make sure height is less than maximum screen size 286ab2043b8SDevin Teske [ $height -le $max_height ] || height=$max_height 287ab2043b8SDevin Teske 288ab2043b8SDevin Teske # Return both 289ab2043b8SDevin Teske echo "$height $width" 290ab2043b8SDevin Teske} 291ab2043b8SDevin Teske 292ab2043b8SDevin Teske# f_dialog_buttonbox_size $title $backtitle $prompt [$hline] 293ab2043b8SDevin Teske# 294ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of 295ab2043b8SDevin Teske# `--msgbox' and `--yesno' boxes sensibly. 296ab2043b8SDevin Teske# 297ab2043b8SDevin Teske# This function helps solve this issue by taking as arguments (in order of 298ab2043b8SDevin Teske# appearance) the title, backtitle, prompt, and [optionally] hline returning 299ab2043b8SDevin Teske# the optimal width and height for the box (not exceeding the actual terminal 300ab2043b8SDevin Teske# width or height). 301ab2043b8SDevin Teske# 302ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by 303ab2043b8SDevin Teske# dialog(1). 304ab2043b8SDevin Teske# 305ab2043b8SDevin Teske# Output is in the format of "height width". 306ab2043b8SDevin Teske# 307ab2043b8SDevin Teskef_dialog_buttonbox_size() 308ab2043b8SDevin Teske{ 309ab2043b8SDevin Teske local title="$1" btitle="$2" prompt="$3" hline="$4" 310ab2043b8SDevin Teske local size="$( f_dialog_infobox_size \ 311ab2043b8SDevin Teske "$title" "$btitle" "$prompt" "$hline" )" 312ab2043b8SDevin Teske local height="${size%%[$IFS]*}" 313ab2043b8SDevin Teske local width="${size##*[$IFS]}" 314ab2043b8SDevin Teske 315ab2043b8SDevin Teske # Add height to accomodate the buttons 316ab2043b8SDevin Teske height=$(( $height + 2 )) 317ab2043b8SDevin Teske 318ab2043b8SDevin Teske # Adjust for clipping with Xdialog(1) on Linux/GTK2 319ab2043b8SDevin Teske [ "$USE_XDIALOG" ] && height=$(( $height + 3 )) 320ab2043b8SDevin Teske 321ab2043b8SDevin Teske # 322ab2043b8SDevin Teske # Enforce maximum height regardless 323ab2043b8SDevin Teske # 324ab2043b8SDevin Teske local max_size 325ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 326ab2043b8SDevin Teske max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION 327ab2043b8SDevin Teske else 328ab2043b8SDevin Teske max_size=$( stty size ) # usually "24 80" 329ab2043b8SDevin Teske fi 330ab2043b8SDevin Teske local max_height="${max_size%%[$IFS]*}" 331ab2043b8SDevin Teske [ $height -le $max_height ] || height=$max_height 332ab2043b8SDevin Teske 333ab2043b8SDevin Teske # Return both 334ab2043b8SDevin Teske echo "$height $width" 335ab2043b8SDevin Teske} 336ab2043b8SDevin Teske 337ab2043b8SDevin Teske# f_dialog_inputbox_size $title $backtitle $prompt $init [$hline] 338ab2043b8SDevin Teske# 339ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of 340ab2043b8SDevin Teske# `--inputbox' boxes sensibly. 341ab2043b8SDevin Teske# 342ab2043b8SDevin Teske# This function helps solve this issue by taking as arguments (in order of 343ab2043b8SDevin Teske# appearance) the title, backtitle, prompt, initial text, and [optionally] 344ab2043b8SDevin Teske# hline returning the optimal width and height for the box (not exceeding the 345ab2043b8SDevin Teske# actual terminal width and height). 346ab2043b8SDevin Teske# 347ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by 348ab2043b8SDevin Teske# dialog(1). 349ab2043b8SDevin Teske# 350ab2043b8SDevin Teske# Output is in the format of "height width". 351ab2043b8SDevin Teske# 352ab2043b8SDevin Teskef_dialog_inputbox_size() 353ab2043b8SDevin Teske{ 354ab2043b8SDevin Teske local title="$1" btitle="$2" prompt="$3" init="$4" hline="$5" n 355ab2043b8SDevin Teske local size="$( f_dialog_buttonbox_size \ 356ab2043b8SDevin Teske "$title" "$btitle" "$prompt" "$hline" )" 357ab2043b8SDevin Teske local height="${size%%[$IFS]*}" 358ab2043b8SDevin Teske local width="${size##*[$IFS]}" 359ab2043b8SDevin Teske 360ab2043b8SDevin Teske local min_width max_size 361ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 362ab2043b8SDevin Teske min_width=35 363ab2043b8SDevin Teske max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION 364ab2043b8SDevin Teske else 365ab2043b8SDevin Teske min_width=24 366ab2043b8SDevin Teske max_size=$( stty size ) # usually "24 80" 367ab2043b8SDevin Teske fi 368ab2043b8SDevin Teske local max_height="${max_size%%[$IFS]*}" 369ab2043b8SDevin Teske local max_width="${max_size##*[$IFS]}" 370ab2043b8SDevin Teske 371ab2043b8SDevin Teske # 372ab2043b8SDevin Teske # Add height to accomodate the input box 373ab2043b8SDevin Teske # 374ab2043b8SDevin Teske [ ! "$USE_XDIALOG" ] && height=$(( $height + 3 )) 375ab2043b8SDevin Teske [ $height -le $max_height ] || height=$max_height 376ab2043b8SDevin Teske 377ab2043b8SDevin Teske # 378ab2043b8SDevin Teske # Bump width for initial text (if not already at maximum width). 379ab2043b8SDevin Teske # NOTE: Something neither dialog(1)/Xdialog(1) do, but worth it! 380ab2043b8SDevin Teske # 381ab2043b8SDevin Teske if [ $width -lt $max_width ]; then 382ab2043b8SDevin Teske n=$(( ${#init} + 7 )) 383ab2043b8SDevin Teske 384ab2043b8SDevin Teske # Add 16.6% width for Xdialog(1) 385ab2043b8SDevin Teske [ "$USE_XDIALOG" ] && n=$(( $n + $n / 6 )) 386ab2043b8SDevin Teske 387ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 388ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 389ab2043b8SDevin Teske width=$n 390ab2043b8SDevin Teske else 391ab2043b8SDevin Teske width=$max_width 392ab2043b8SDevin Teske fi 393ab2043b8SDevin Teske fi 394ab2043b8SDevin Teske fi 395ab2043b8SDevin Teske 396ab2043b8SDevin Teske # Return both 397ab2043b8SDevin Teske echo "$height $width" 398ab2043b8SDevin Teske} 399ab2043b8SDevin Teske 400ab2043b8SDevin Teske# f_xdialog_2inputsbox_size $title $backtitle $prompt \ 401ab2043b8SDevin Teske# $label1 $init1 $label2 $init2 402ab2043b8SDevin Teske# 403ab2043b8SDevin Teske# Xdialog(1) does not perform auto-sizing of the width and height of 404ab2043b8SDevin Teske# `--2inputsbox' boxes sensibly. 405ab2043b8SDevin Teske# 406ab2043b8SDevin Teske# This function helps solve this issue by taking as arguments (in order of 407ab2043b8SDevin Teske# appearance) the title, backtitle, prompt, label for the first field, initial 408ab2043b8SDevin Teske# text for said field, label for the second field, and initial text for said 409ab2043b8SDevin Teske# field returning the optimal width and height for the box (not exceeding the 410ab2043b8SDevin Teske# actual terminal width and height). 411ab2043b8SDevin Teske# 412ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by 413ab2043b8SDevin Teske# Xdialog(1). 414ab2043b8SDevin Teske# 415ab2043b8SDevin Teske# Output is in the format of "height width". 416ab2043b8SDevin Teske# 417ab2043b8SDevin Teskef_xdialog_2inputsbox_size() 418ab2043b8SDevin Teske{ 419ab2043b8SDevin Teske local title="$1" btitle="$2" prompt="$3" 420ab2043b8SDevin Teske local label1="$4" init1="$5" label2="$6" init2="$7" n 421ab2043b8SDevin Teske local size="$( f_dialog_inputbox_size \ 422ab2043b8SDevin Teske "$title" "$btitle" "$prompt" "$init1" )" 423ab2043b8SDevin Teske local height="${size%%[$IFS]*}" 424ab2043b8SDevin Teske local width="${size##*[$IFS]}" 425ab2043b8SDevin Teske 426ab2043b8SDevin Teske local min_width=35 427ab2043b8SDevin Teske local max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION 428ab2043b8SDevin Teske local max_height="${max_size%%[$IFS]*}" 429ab2043b8SDevin Teske local max_width="${max_size##*[$IFS]}" 430ab2043b8SDevin Teske 431ab2043b8SDevin Teske # Add height for first label 432ab2043b8SDevin Teske height=$(( $height + 2 )) 433ab2043b8SDevin Teske 434ab2043b8SDevin Teske # 435ab2043b8SDevin Teske # Bump width for first label text (if not already at maximum width). 436ab2043b8SDevin Teske # 437ab2043b8SDevin Teske if [ $width -lt $max_width ]; then 438ab2043b8SDevin Teske n=$(( ${#label1} + 7 )) 439ab2043b8SDevin Teske 440ab2043b8SDevin Teske # Add 16.6% width for Xdialog(1) 441ab2043b8SDevin Teske n=$(( $n + $n / 6 )) 442ab2043b8SDevin Teske 443ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 444ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 445ab2043b8SDevin Teske width=$n 446ab2043b8SDevin Teske else 447ab2043b8SDevin Teske width=$max_width 448ab2043b8SDevin Teske fi 449ab2043b8SDevin Teske fi 450ab2043b8SDevin Teske fi 451ab2043b8SDevin Teske 452ab2043b8SDevin Teske # Add height for second label 453ab2043b8SDevin Teske height=$(( $height + 2 )) 454ab2043b8SDevin Teske 455ab2043b8SDevin Teske # 456ab2043b8SDevin Teske # Bump width for second label text (if not already at maximum width). 457ab2043b8SDevin Teske # 458ab2043b8SDevin Teske if [ $width -lt $max_width ]; then 459ab2043b8SDevin Teske n=$(( ${#label2} + 7 )) 460ab2043b8SDevin Teske 461ab2043b8SDevin Teske # Add 16.6% width for Xdialog(1) 462ab2043b8SDevin Teske n=$(( $n + $n / 6 )) 463ab2043b8SDevin Teske 464ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 465ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 466ab2043b8SDevin Teske width=$n 467ab2043b8SDevin Teske else 468ab2043b8SDevin Teske width=$max_width 469ab2043b8SDevin Teske fi 470ab2043b8SDevin Teske fi 471ab2043b8SDevin Teske fi 472ab2043b8SDevin Teske 473ab2043b8SDevin Teske # Add height for a second inputbox 474ab2043b8SDevin Teske height=$(( $height + 2 )) 475ab2043b8SDevin Teske 476ab2043b8SDevin Teske # 477ab2043b8SDevin Teske # Bump width for second initial text (if not already at maximum width). 478ab2043b8SDevin Teske # NOTE: Something neither dialog(1)/Xdialog(1) do, but worth it! 479ab2043b8SDevin Teske # 480ab2043b8SDevin Teske if [ $width -lt $max_width ]; then 481ab2043b8SDevin Teske n=$(( ${#init2} + 7 )) 482ab2043b8SDevin Teske 483ab2043b8SDevin Teske # Add 16.6% width for Xdialog(1) 484ab2043b8SDevin Teske n=$(( $n + $n / 6 )) 485ab2043b8SDevin Teske 486ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 487ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 488ab2043b8SDevin Teske width=$n 489ab2043b8SDevin Teske else 490ab2043b8SDevin Teske width=$max_width 491ab2043b8SDevin Teske fi 492ab2043b8SDevin Teske fi 493ab2043b8SDevin Teske fi 494ab2043b8SDevin Teske 495ab2043b8SDevin Teske # Return both 496ab2043b8SDevin Teske echo "$height $width" 497ab2043b8SDevin Teske} 498ab2043b8SDevin Teske 499ab2043b8SDevin Teske# f_dialog_menu_size $title $backtitle $prompt $hline \ 500ab2043b8SDevin Teske# $tag1 $item1 $tag2 $item2 ... 501ab2043b8SDevin Teske# 502ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of 503ab2043b8SDevin Teske# `--menu' boxes sensibly. 504ab2043b8SDevin Teske# 505ab2043b8SDevin Teske# This function helps solve this issue by taking as arguments (in order of 506ab2043b8SDevin Teske# appearance) the title, backtitle, prompt, hline and list of tag/item pairs, 507ab2043b8SDevin Teske# returning the optimal width and height for the menu (not exceeding the actual 508ab2043b8SDevin Teske# terminal width or height). 509ab2043b8SDevin Teske# 510ab2043b8SDevin Teske# Output is in the format of "height width rows". 511ab2043b8SDevin Teske# 512ab2043b8SDevin Teskef_dialog_menu_size() 513ab2043b8SDevin Teske{ 514ab2043b8SDevin Teske local title="$1" btitle="$2" prompt="$3" hline="$4" n=0 515ab2043b8SDevin Teske local min_width min_rows max_size 516ab2043b8SDevin Teske 517ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 518ab2043b8SDevin Teske min_width=35 519ab2043b8SDevin Teske min_rows=1 520ab2043b8SDevin Teske max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION 521ab2043b8SDevin Teske else 522ab2043b8SDevin Teske min_width=24 523ab2043b8SDevin Teske min_rows=0 524ab2043b8SDevin Teske max_size=$( stty size ) # usually "24 80" 525ab2043b8SDevin Teske fi 526ab2043b8SDevin Teske 527ab2043b8SDevin Teske local max_width="${max_size##*[$IFS]}" 528ab2043b8SDevin Teske local max_height="${max_size%%[$IFS]*}" 529ab2043b8SDevin Teske local box_size="$( f_dialog_infobox_size \ 530ab2043b8SDevin Teske "$title" "$btitle" "$prompt" "$hline" )" 531ab2043b8SDevin Teske local box_height="${box_size%%[$IFS]*}" 532ab2043b8SDevin Teske local box_width="${box_size##*[$IFS]}" 533ab2043b8SDevin Teske local max_rows=$(( $max_height - 8 )) 534ab2043b8SDevin Teske local height width=$box_width rows=$min_rows 535ab2043b8SDevin Teske 536ab2043b8SDevin Teske shift 4 # title/btitle/prompt/hline 537ab2043b8SDevin Teske 538ab2043b8SDevin Teske # If there's no prompt, bump the max-rows by 1 539ab2043b8SDevin Teske [ "$prompt" ] || max_rows=$(( $max_rows + 1 )) 540ab2043b8SDevin Teske 541ab2043b8SDevin Teske # 542ab2043b8SDevin Teske # The sum total between the longest tag-length and longest item-length 543ab2043b8SDevin Teske # should be used for the menu width (not to exceed terminal width). 544ab2043b8SDevin Teske # 545ab2043b8SDevin Teske # Also, calculate the number of rows (not to exceed terminal height). 546ab2043b8SDevin Teske # 547ab2043b8SDevin Teske local longest_tag=0 longest_item=0 548ab2043b8SDevin Teske while [ $# -ge 2 ]; do 549ab2043b8SDevin Teske local tag="$1" item="$2" 550ab2043b8SDevin Teske shift 2 # tag/item 551ab2043b8SDevin Teske 552ab2043b8SDevin Teske [ ${#tag} -gt $longest_tag ] && longest_tag=${#tag} 553ab2043b8SDevin Teske [ ${#item} -gt $longest_item ] && longest_item=${#item} 554ab2043b8SDevin Teske [ $rows -lt $max_rows ] && rows=$(( $rows + 1 )) 555ab2043b8SDevin Teske done 556ab2043b8SDevin Teske 557ab2043b8SDevin Teske # Update width 558ab2043b8SDevin Teske n=$(( $longest_tag + $longest_item + 10 )) 559ab2043b8SDevin Teske [ "$USE_XDIALOG" ] && n=$(( $n + $n / 6 )) # Add 16.6% for Xdialog(1) 560ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 561ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 562ab2043b8SDevin Teske width=$n 563ab2043b8SDevin Teske else 564ab2043b8SDevin Teske width=$max_width 565ab2043b8SDevin Teske fi 566ab2043b8SDevin Teske fi 567ab2043b8SDevin Teske 568ab2043b8SDevin Teske # Fix rows and set height 569ab2043b8SDevin Teske [ $rows -gt 0 ] || rows=1 570ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 571ab2043b8SDevin Teske height=$(( $rows + $box_height + 7 )) 572ab2043b8SDevin Teske else 573ab2043b8SDevin Teske height=$(( $rows + $box_height + 4 )) 574ab2043b8SDevin Teske fi 575ab2043b8SDevin Teske [ $height -le $max_height ] || height=$max_height 576ab2043b8SDevin Teske 577ab2043b8SDevin Teske # Return all three 578ab2043b8SDevin Teske echo "$height $width $rows" 579ab2043b8SDevin Teske} 580ab2043b8SDevin Teske 581ab2043b8SDevin Teske# f_dialog_menu_with_help_size $title $backtitle $prompt $hline \ 582ab2043b8SDevin Teske# $tag1 $item1 $help1 $tag2 $item2 $help2 ... 583ab2043b8SDevin Teske# 584ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of 585ab2043b8SDevin Teske# `--menu' boxes sensibly. 586ab2043b8SDevin Teske# 587ab2043b8SDevin Teske# This function helps solve this issue by taking as arguments (in order of 588ab2043b8SDevin Teske# appearance) the title, backtitle, prompt, hline and list of tag/item/help 589ab2043b8SDevin Teske# triplets, returning the optimal width and height for the menu (not exceeding 590ab2043b8SDevin Teske# the actual terminal width or height). 591ab2043b8SDevin Teske# 592ab2043b8SDevin Teske# Output is in the format of "height width rows". 593ab2043b8SDevin Teske# 594ab2043b8SDevin Teskef_dialog_menu_with_help_size() 595ab2043b8SDevin Teske{ 596ab2043b8SDevin Teske local title="$1" btitle="$2" prompt="$3" hline="$4" n=0 597ab2043b8SDevin Teske local min_width min_rows max_size 598ab2043b8SDevin Teske 599ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 600ab2043b8SDevin Teske min_width=35 601ab2043b8SDevin Teske min_rows=1 602ab2043b8SDevin Teske max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION 603ab2043b8SDevin Teske else 604ab2043b8SDevin Teske min_width=24 605ab2043b8SDevin Teske min_rows=0 606ab2043b8SDevin Teske max_size=$( stty size ) # usually "24 80" 607ab2043b8SDevin Teske fi 608ab2043b8SDevin Teske 609ab2043b8SDevin Teske local max_width="${max_size##*[$IFS]}" 610ab2043b8SDevin Teske local max_height="${max_size%%[$IFS]*}" 611ab2043b8SDevin Teske local box_size="$( f_dialog_infobox_size \ 612ab2043b8SDevin Teske "$title" "$btitle" "$prompt" "$hline" )" 613ab2043b8SDevin Teske local box_height="${box_size%%[$IFS]*}" 614ab2043b8SDevin Teske local box_width="${box_size##*[$IFS]}" 615ab2043b8SDevin Teske local max_rows=$(( $max_height - 8 )) 616ab2043b8SDevin Teske local height width=$box_width rows=$min_rows 617ab2043b8SDevin Teske 618ab2043b8SDevin Teske shift 4 # title/btitle/prompt/hline 619ab2043b8SDevin Teske 620ab2043b8SDevin Teske # If there's no prompt, bump the max-rows by 1 621ab2043b8SDevin Teske [ "$prompt" ] || max_rows=$(( $max_rows + 1 )) 622ab2043b8SDevin Teske 623ab2043b8SDevin Teske # 624ab2043b8SDevin Teske # The sum total between the longest tag-length and longest item-length 625ab2043b8SDevin Teske # should be used for the menu width (not to exceed terminal width). 626ab2043b8SDevin Teske # 627ab2043b8SDevin Teske # Also, calculate the number of rows (not to exceed terminal height). 628ab2043b8SDevin Teske # 629ab2043b8SDevin Teske # Also, calculate the longest help while we're here. This will be used 630ab2043b8SDevin Teske # to influence the width of the menu if (and only-if) using Xdialog(1). 631ab2043b8SDevin Teske # 632ab2043b8SDevin Teske local longest_tag=0 longest_item=0 longest_help=0 633ab2043b8SDevin Teske while [ $# -ge 3 ]; do 634ab2043b8SDevin Teske local tag="$1" item="$2" help="$3" 635ab2043b8SDevin Teske shift 3 # tag/item/help 636ab2043b8SDevin Teske 637ab2043b8SDevin Teske [ ${#tag} -gt $longest_tag ] && longest_tag=${#tag} 638ab2043b8SDevin Teske [ ${#item} -gt $longest_item ] && longest_item=${#item} 639ab2043b8SDevin Teske [ ${#help} -gt $longest_help ] && longest_help=${#help} 640ab2043b8SDevin Teske [ $rows -lt $max_rows ] && rows=$(( $rows + 1 )) 641ab2043b8SDevin Teske done 642ab2043b8SDevin Teske 643ab2043b8SDevin Teske # Update width 644ab2043b8SDevin Teske n=$(( $longest_tag + $longest_item + 10 )) 645ab2043b8SDevin Teske [ "$USE_XDIALOG" ] && n=$(( $n + $n / 6 )) # Add 16.6% for Xdialog(1) 646ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 647ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 648ab2043b8SDevin Teske width=$n 649ab2043b8SDevin Teske else 650ab2043b8SDevin Teske width=$max_width 651ab2043b8SDevin Teske fi 652ab2043b8SDevin Teske fi 653ab2043b8SDevin Teske 654ab2043b8SDevin Teske # Update width for help text if using Xdialog(1) 655ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 656ab2043b8SDevin Teske n=$(( $longest_help + 10 )) 657ab2043b8SDevin Teske n=$(( $n + $n / 6 )) # +16.6% 658ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 659ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 660ab2043b8SDevin Teske width=$n 661ab2043b8SDevin Teske else 662ab2043b8SDevin Teske width=$max_width 663ab2043b8SDevin Teske fi 664ab2043b8SDevin Teske fi 665ab2043b8SDevin Teske fi 666ab2043b8SDevin Teske 667ab2043b8SDevin Teske # Fix rows and set height 668ab2043b8SDevin Teske [ $rows -gt 0 ] || rows=1 669ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 670ab2043b8SDevin Teske height=$(( $rows + $box_height + 8 )) 671ab2043b8SDevin Teske else 672ab2043b8SDevin Teske height=$(( $rows + $box_height + 4 )) 673ab2043b8SDevin Teske fi 674ab2043b8SDevin Teske [ $height -le $max_height ] || height=$max_height 675ab2043b8SDevin Teske 676ab2043b8SDevin Teske # Return all three 677ab2043b8SDevin Teske echo "$height $width $rows" 678ab2043b8SDevin Teske} 679ab2043b8SDevin Teske 680ab2043b8SDevin Teske# f_dialog_radiolist_size $title $backtitle $prompt $hline \ 681ab2043b8SDevin Teske# $tag1 $item1 $status1 $tag2 $item2 $status2 ... 682ab2043b8SDevin Teske# 683ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of 684ab2043b8SDevin Teske# `--radiolist' boxes sensibly. 685ab2043b8SDevin Teske# 686ab2043b8SDevin Teske# This function helps solve this issue by taking as arguments (in order of 687ab2043b8SDevin Teske# appearance) the title, backtitle, prompt, hline and list of tag/item/status 688ab2043b8SDevin Teske# triplets, returning the optimal width and height for the radiolist (not 689ab2043b8SDevin Teske# exceeding the actual terminal width or height). 690ab2043b8SDevin Teske# 691ab2043b8SDevin Teske# Output is in the format of "height width rows". 692ab2043b8SDevin Teske# 693ab2043b8SDevin Teskef_dialog_radiolist_size() 694ab2043b8SDevin Teske{ 695ab2043b8SDevin Teske local title="$1" btitle="$2" prompt="$3" hline="$4" n=0 696ab2043b8SDevin Teske local min_width min_rows max_size 697ab2043b8SDevin Teske 698ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 699ab2043b8SDevin Teske min_width=35 700ab2043b8SDevin Teske min_rows=1 701ab2043b8SDevin Teske max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION 702ab2043b8SDevin Teske else 703ab2043b8SDevin Teske min_width=24 704ab2043b8SDevin Teske min_rows=0 705ab2043b8SDevin Teske max_size=$( stty size ) # usually "24 80" 706ab2043b8SDevin Teske fi 707ab2043b8SDevin Teske 708ab2043b8SDevin Teske local max_width="${max_size##*[$IFS]}" 709ab2043b8SDevin Teske local max_height="${max_size%%[$IFS]*}" 710ab2043b8SDevin Teske local box_size="$( f_dialog_infobox_size \ 711ab2043b8SDevin Teske "$title" "$btitle" "$prompt" "$hline" )" 712ab2043b8SDevin Teske local box_height="${box_size%%[$IFS]*}" 713ab2043b8SDevin Teske local box_width="${box_size##*[$IFS]}" 714ab2043b8SDevin Teske local max_rows=$(( $max_height - 8 )) 715ab2043b8SDevin Teske local height width=$box_width rows=$min_rows 716ab2043b8SDevin Teske 717ab2043b8SDevin Teske shift 4 # title/btitle/prompt/hline 718ab2043b8SDevin Teske 719ab2043b8SDevin Teske # 720ab2043b8SDevin Teske # The sum total between the longest tag-length, longest item-length, 721ab2043b8SDevin Teske # and radio-button width should be used for the menu width (not to 722ab2043b8SDevin Teske # exceed terminal width). 723ab2043b8SDevin Teske # 724ab2043b8SDevin Teske # Also, calculate the number of rows (not to exceed terminal height). 725ab2043b8SDevin Teske # 726ab2043b8SDevin Teske local longest_tag=0 longest_item=0 727ab2043b8SDevin Teske while [ $# -ge 2 ]; do 728ab2043b8SDevin Teske local tag="$1" item="$2" help="$3" 729ab2043b8SDevin Teske shift 3 # tag/item/status 730ab2043b8SDevin Teske 731ab2043b8SDevin Teske [ ${#tag} -gt $longest_tag ] && longest_tag=${#tag} 732ab2043b8SDevin Teske [ ${#item} -gt $longest_item ] && longest_item=${#item} 733ab2043b8SDevin Teske [ $rows -lt $max_rows ] && rows=$(( $rows + 1 )) 734ab2043b8SDevin Teske done 735ab2043b8SDevin Teske 736ab2043b8SDevin Teske # Update width 737ab2043b8SDevin Teske n=$(( $longest_tag + $longest_item + 13 )) 738ab2043b8SDevin Teske [ "$USE_XDIALOG" ] && n=$(( $n + $n / 6 )) # Add 16.6% for Xdialog(1) 739ab2043b8SDevin Teske if [ $n -gt $width -a $n -gt $min_width ]; then 740ab2043b8SDevin Teske if [ $n -lt $max_width ]; then 741ab2043b8SDevin Teske width=$n 742ab2043b8SDevin Teske else 743ab2043b8SDevin Teske width=$max_width 744ab2043b8SDevin Teske fi 745ab2043b8SDevin Teske fi 746ab2043b8SDevin Teske 747ab2043b8SDevin Teske # Fix rows and set height 748ab2043b8SDevin Teske [ $rows -gt 0 ] || rows=1 749ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 750ab2043b8SDevin Teske height=$(( $rows + $box_height + 7 )) 751ab2043b8SDevin Teske else 752ab2043b8SDevin Teske height=$(( $rows + $box_height + 4 )) 753ab2043b8SDevin Teske fi 754ab2043b8SDevin Teske [ $height -le $max_height ] || height=$max_height 755ab2043b8SDevin Teske 756ab2043b8SDevin Teske # Return all three 757ab2043b8SDevin Teske echo "$height $width $rows" 758ab2043b8SDevin Teske} 759ab2043b8SDevin Teske 760ab2043b8SDevin Teske# f_dialog_calendar_size $title $backtitle $prompt [$hline] 761ab2043b8SDevin Teske# 762ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of 763ab2043b8SDevin Teske# `--calendar' boxes sensibly. 764ab2043b8SDevin Teske# 765ab2043b8SDevin Teske# This function helps solve this issue by taking as arguments (in order of 766ab2043b8SDevin Teske# appearance) the title, backtitle, prompt, and [optionally] hline returning 767ab2043b8SDevin Teske# the optimal width and height for the box (not exceeding the actual terminal 768ab2043b8SDevin Teske# width and height). 769ab2043b8SDevin Teske# 770ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by 771ab2043b8SDevin Teske# dialog(1). 772ab2043b8SDevin Teske# 773ab2043b8SDevin Teske# Output is in the format of "height width". 774ab2043b8SDevin Teske# 775ab2043b8SDevin Teskef_dialog_calendar_size() 776ab2043b8SDevin Teske{ 777ab2043b8SDevin Teske local title="$1" btitle="$2" prompt="$3" hline="$4" n 778ab2043b8SDevin Teske local size="$( f_dialog_infobox_size \ 779ab2043b8SDevin Teske "$title" "$btitle" "$prompt" "$hline" )" 780ab2043b8SDevin Teske local height="${size%%[$IFS]*}" 781ab2043b8SDevin Teske local width="${size##*[$IFS]}" 782ab2043b8SDevin Teske 783ab2043b8SDevin Teske local min_width min_height max_size 784ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 785ab2043b8SDevin Teske min_height=15 786ab2043b8SDevin Teske min_width=55 787ab2043b8SDevin Teske max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION 788ab2043b8SDevin Teske else 789ab2043b8SDevin Teske min_height=0 790ab2043b8SDevin Teske min_width=40 791ab2043b8SDevin Teske max_size=$( stty size ) # usually "24 80" 792ab2043b8SDevin Teske fi 793ab2043b8SDevin Teske local max_height="${max_size%%[$IFS]*}" 794ab2043b8SDevin Teske local max_width="${max_size##*[$IFS]}" 795ab2043b8SDevin Teske 796ab2043b8SDevin Teske # 797ab2043b8SDevin Teske # Enforce the minimum width for displaying the calendar 798ab2043b8SDevin Teske # 799ab2043b8SDevin Teske [ $width -ge $min_width ] || width=$min_width 800ab2043b8SDevin Teske 801ab2043b8SDevin Teske # 802ab2043b8SDevin Teske # When using dialog(1), the calendar box is unique from other dialog(1) 803ab2043b8SDevin Teske # boxes in-that the height passed should not accomodate the 15-lines 804ab2043b8SDevin Teske # required to display the calendar. This does not apply to Xdialog(1). 805ab2043b8SDevin Teske # 806ab2043b8SDevin Teske # When using Xdialog(1), the height must accomodate the 15-lines 807ab2043b8SDevin Teske # required to display the calendar. 808ab2043b8SDevin Teske # 809ab2043b8SDevin Teske # NOTE: Also under dialog(1), because we can't predict whether the user 810ab2043b8SDevin Teske # has disabled shadow's in their `$HOME/.dialogrc' file, we'll subtract 811ab2043b8SDevin Teske # 16 rather than 15. This does not apply to Xdialog(1). 812ab2043b8SDevin Teske # 813ab2043b8SDevin Teske max_height=$(( $max_height - 16 )) 814ab2043b8SDevin Teske height=$( echo "$prompt" | f_number_of_lines ) 815ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 816ab2043b8SDevin Teske # Add height to accomodate for the embedded calendar widget 817ab2043b8SDevin Teske height=$(( $height + $min_height - 1 )) 818ab2043b8SDevin Teske 819ab2043b8SDevin Teske # Also, bump height if backtitle is enabled 820ab2043b8SDevin Teske if [ "$btitle" ]; then 821ab2043b8SDevin Teske local n="$( echo "$btitle" | f_number_of_lines )" 822ab2043b8SDevin Teske height=$(( $height + $n + 2 )) 823ab2043b8SDevin Teske fi 824ab2043b8SDevin Teske else 825ab2043b8SDevin Teske [ "$prompt" ] && height=$(( $height + 1 )) 826ab2043b8SDevin Teske fi 827ab2043b8SDevin Teske [ $height -le $max_height ] || height=$max_height 828ab2043b8SDevin Teske 829ab2043b8SDevin Teske # 830ab2043b8SDevin Teske # The calendar box refuses to display if too large. 831ab2043b8SDevin Teske # 832ab2043b8SDevin Teske max_width=$(( $max_width - 2 )) 833ab2043b8SDevin Teske [ $width -le $max_width ] || width=$max_width 834ab2043b8SDevin Teske 835ab2043b8SDevin Teske # Return both 836ab2043b8SDevin Teske echo "$height $width" 837ab2043b8SDevin Teske} 838ab2043b8SDevin Teske 839ab2043b8SDevin Teske# f_dialog_timebox_size $title $backtitle $prompt [$hline] 840ab2043b8SDevin Teske# 841ab2043b8SDevin Teske# Not all versions of dialog(1) perform auto-sizing of the width and height of 842ab2043b8SDevin Teske# `--timebox' boxes sensibly. 843ab2043b8SDevin Teske# 844ab2043b8SDevin Teske# This function helps solve this issue by taking as arguments (in order of 845ab2043b8SDevin Teske# appearance) the title, backtitle, prompt, and [optionally] hline returning 846ab2043b8SDevin Teske# the optimal width and height for the box (not exceeding the actual terminal 847ab2043b8SDevin Teske# width and height). 848ab2043b8SDevin Teske# 849ab2043b8SDevin Teske# Newline character sequences (``\n'') in $prompt are expanded as-is done by 850ab2043b8SDevin Teske# dialog(1). 851ab2043b8SDevin Teske# 852ab2043b8SDevin Teske# Output is in the format of "height width". 853ab2043b8SDevin Teske# 854ab2043b8SDevin Teskef_dialog_timebox_size() 855ab2043b8SDevin Teske{ 856ab2043b8SDevin Teske local title="$1" btitle="$2" prompt="$3" hline="$4" n 857ab2043b8SDevin Teske local size="$( f_dialog_infobox_size \ 858ab2043b8SDevin Teske "$title" "$btitle" "$prompt" "$hline" )" 859ab2043b8SDevin Teske local height="${size%%[$IFS]*}" 860ab2043b8SDevin Teske local width="${size##*[$IFS]}" 861ab2043b8SDevin Teske 862ab2043b8SDevin Teske local min_width min_height max_size 863ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 864ab2043b8SDevin Teske min_width=40 865ab2043b8SDevin Teske max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION 866ab2043b8SDevin Teske else 867ab2043b8SDevin Teske min_height=0 868ab2043b8SDevin Teske min_width=20 869ab2043b8SDevin Teske max_size=$( stty size ) # usually "24 80" 870ab2043b8SDevin Teske fi 871ab2043b8SDevin Teske local max_height="${max_size%%[$IFS]*}" 872ab2043b8SDevin Teske local max_width="${max_size##*[$IFS]}" 873ab2043b8SDevin Teske 874ab2043b8SDevin Teske # 875ab2043b8SDevin Teske # Enforce the minimum width for displaying the timebox 876ab2043b8SDevin Teske # 877ab2043b8SDevin Teske [ $width -ge $min_width ] || width=$min_width 878ab2043b8SDevin Teske 879ab2043b8SDevin Teske # 880ab2043b8SDevin Teske # When using dialog(1), the timebox box is unique from other dialog(1) 881ab2043b8SDevin Teske # boxes in-that the height passed should not accomodate the 6-lines 882ab2043b8SDevin Teske # required to display the timebox. This does not apply to Xdialog(1). 883ab2043b8SDevin Teske # 884ab2043b8SDevin Teske # When using Xdialog(1), the height seems to have no effect. All values 885ab2043b8SDevin Teske # provide the same results. 886ab2043b8SDevin Teske # 887ab2043b8SDevin Teske # NOTE: Also under dialog(1), because we can't predict whether the user 888ab2043b8SDevin Teske # has disabled shadow's in their `$HOME/.dialogrc' file, we'll subtract 889ab2043b8SDevin Teske # 7 rather than 6. This does not apply to Xdialog(1). 890ab2043b8SDevin Teske # 891ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 892ab2043b8SDevin Teske height=0 # Autosize; all values produce same results 893ab2043b8SDevin Teske else 894ab2043b8SDevin Teske max_height=$(( $max_height - 7 )) 895ab2043b8SDevin Teske height=$( echo "$prompt" | f_number_of_lines ) 896ab2043b8SDevin Teske height=$(( $height + 1 )) 897ab2043b8SDevin Teske [ $height -le $max_height ] || height=$max_height 898ab2043b8SDevin Teske [ "$prompt" ] && height=$(( $height + 1 )) 899ab2043b8SDevin Teske fi 900ab2043b8SDevin Teske 901ab2043b8SDevin Teske # 902ab2043b8SDevin Teske # The timebox box refuses to display if too large. 903ab2043b8SDevin Teske # 904ab2043b8SDevin Teske max_width=$(( $max_width - 2 )) 905ab2043b8SDevin Teske [ $width -le $max_width ] || width=$max_width 906ab2043b8SDevin Teske 907ab2043b8SDevin Teske # Return both 908ab2043b8SDevin Teske echo "$height $width" 909ab2043b8SDevin Teske} 910ab2043b8SDevin Teske 911ab2043b8SDevin Teske############################################################ CLEAR FUNCTIONS 912ab2043b8SDevin Teske 913ab2043b8SDevin Teske# f_dialog_clear 914ab2043b8SDevin Teske# 915ab2043b8SDevin Teske# Clears any/all previous dialog(1) displays. 916ab2043b8SDevin Teske# 917ab2043b8SDevin Teskef_dialog_clear() 918ab2043b8SDevin Teske{ 919ab2043b8SDevin Teske $DIALOG --clear 920ab2043b8SDevin Teske} 921ab2043b8SDevin Teske 922ab2043b8SDevin Teske############################################################ INFO FUNCTIONS 923ab2043b8SDevin Teske 924ab2043b8SDevin Teske# f_dialog_info $info_text ... 925ab2043b8SDevin Teske# 926ab2043b8SDevin Teske# Throw up a dialog(1) infobox. The infobox remains until another dialog is 927ab2043b8SDevin Teske# displayed or `dialog --clear' (or dialog_clear) is called. 928ab2043b8SDevin Teske# 929ab2043b8SDevin Teskef_dialog_info() 930ab2043b8SDevin Teske{ 931ab2043b8SDevin Teske local info_text="$*" 932ab2043b8SDevin Teske local size="$( f_dialog_infobox_size \ 933ab2043b8SDevin Teske "$DIALOG_TITLE" \ 934ab2043b8SDevin Teske "$DIALOG_BACKTITLE" \ 935ab2043b8SDevin Teske "$info_text" )" 936ab2043b8SDevin Teske 937ab2043b8SDevin Teske eval $DIALOG \ 938ab2043b8SDevin Teske --title \"\$DIALOG_TITLE\" \ 939ab2043b8SDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 940ab2043b8SDevin Teske ${USE_XDIALOG:+--ignore-eof} \ 941ab2043b8SDevin Teske ${USE_XDIALOG:+--no-buttons} \ 942ab2043b8SDevin Teske --infobox \"\$info_text\" $size 943ab2043b8SDevin Teske} 944ab2043b8SDevin Teske 945ab2043b8SDevin Teske# f_xdialog_info $info_text ... 946ab2043b8SDevin Teske# 947ab2043b8SDevin Teske# Throw up an Xdialog(1) infobox and do not dismiss it until stdin produces 948ab2043b8SDevin Teske# EOF. This implies that you must execute this either as an rvalue to a pipe, 949ab2043b8SDevin Teske# lvalue to indirection or in a sub-shell that provides data on stdin. 950ab2043b8SDevin Teske# 951ab2043b8SDevin Teskef_xdialog_info() 952ab2043b8SDevin Teske{ 953ab2043b8SDevin Teske local info_text="$*" 954ab2043b8SDevin Teske local size="$( f_dialog_infobox_size \ 955ab2043b8SDevin Teske "$DIALOG_TITLE" \ 956ab2043b8SDevin Teske "$DIALOG_BACKTITLE" \ 957ab2043b8SDevin Teske "$info_text" )" 958ab2043b8SDevin Teske 959ab2043b8SDevin Teske eval $DIALOG \ 960ab2043b8SDevin Teske --title \"\$DIALOG_TITLE\" \ 961ab2043b8SDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 962ab2043b8SDevin Teske --no-close --no-buttons \ 963ab2043b8SDevin Teske --infobox \"\$info_text\" $size \ 964ab2043b8SDevin Teske -1 # timeout of -1 means abort when EOF on stdin 965ab2043b8SDevin Teske} 966ab2043b8SDevin Teske 967ab2043b8SDevin Teske############################################################ MSGBOX FUNCTIONS 968ab2043b8SDevin Teske 969ab2043b8SDevin Teske# f_dialog_msgbox $msg_text ... 970ab2043b8SDevin Teske# 971ab2043b8SDevin Teske# Throw up a dialog(1) msgbox. The msgbox remains until the user presses ENTER 972ab2043b8SDevin Teske# or ESC, acknowledging the modal dialog. 973ab2043b8SDevin Teske# 974ab2043b8SDevin Teske# If the user presses ENTER, the exit status is zero (success), otherwise if 975ab2043b8SDevin Teske# the user presses ESC the exit status is 255. 976ab2043b8SDevin Teske# 977ab2043b8SDevin Teskef_dialog_msgbox() 978ab2043b8SDevin Teske{ 979ab2043b8SDevin Teske local msg_text="$*" 980ab2043b8SDevin Teske local size="$( f_dialog_buttonbox_size \ 981ab2043b8SDevin Teske "$DIALOG_TITLE" \ 982ab2043b8SDevin Teske "$DIALOG_BACKTITLE" \ 983ab2043b8SDevin Teske "$msg_text" )" 984ab2043b8SDevin Teske 985ab2043b8SDevin Teske eval $DIALOG \ 986ab2043b8SDevin Teske --title \"\$DIALOG_TITLE\" \ 987ab2043b8SDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 988ab2043b8SDevin Teske --ok-label \"\$msg_ok\" \ 989ab2043b8SDevin Teske --msgbox \"\$msg_text\" $size 990ab2043b8SDevin Teske} 991ab2043b8SDevin Teske 99240dfc82dSDevin Teske############################################################ TEXTBOX FUNCTIONS 99340dfc82dSDevin Teske 99440dfc82dSDevin Teske# f_dialog_textbox $file 99540dfc82dSDevin Teske# 99640dfc82dSDevin Teske# Display the contents of $file (or an error if $file does not exist, etc.) in 99740dfc82dSDevin Teske# a dialog(1) textbox (which has a scrollable region for the text). The textbox 99840dfc82dSDevin Teske# remains until the user presses ENTER or ESC, acknowledging the modal dialog. 99940dfc82dSDevin Teske# 100040dfc82dSDevin Teske# If the user presses ENTER, the exit status is zero (success), otherwise if 100140dfc82dSDevin Teske# the user presses ESC the exit status is 255. 100240dfc82dSDevin Teske# 100340dfc82dSDevin Teskef_dialog_textbox() 100440dfc82dSDevin Teske{ 100540dfc82dSDevin Teske local file="$1" 100640dfc82dSDevin Teske local contents retval size 100740dfc82dSDevin Teske 100840dfc82dSDevin Teske contents=$( cat "$file" 2>&1 ) 100940dfc82dSDevin Teske retval=$? 101040dfc82dSDevin Teske 101140dfc82dSDevin Teske size=$( f_dialog_buttonbox_size \ 101240dfc82dSDevin Teske "$DIALOG_TITLE" \ 101340dfc82dSDevin Teske "$DIALOG_BACKTITLE" \ 101440dfc82dSDevin Teske "$contents" ) 101540dfc82dSDevin Teske 101640dfc82dSDevin Teske if [ $retval -eq $SUCCESS ]; then 101740dfc82dSDevin Teske eval $DIALOG \ 101840dfc82dSDevin Teske --title \"\$DIALOG_TITLE\" \ 101940dfc82dSDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 102040dfc82dSDevin Teske --exit-label \"\$msg_ok\" \ 102140dfc82dSDevin Teske --no-cancel \ 102240dfc82dSDevin Teske --textbox \"\$file\" $size 102340dfc82dSDevin Teske else 102440dfc82dSDevin Teske eval $DIALOG \ 102540dfc82dSDevin Teske --title \"\$DIALOG_TITLE\" \ 102640dfc82dSDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 102740dfc82dSDevin Teske --ok-label \"\$msg_ok\" \ 1028254917d4SDevin Teske --msgbox \"\$contents\" $size 102940dfc82dSDevin Teske fi 103040dfc82dSDevin Teske} 103140dfc82dSDevin Teske 1032ab2043b8SDevin Teske############################################################ YESNO FUNCTIONS 1033ab2043b8SDevin Teske 1034ab2043b8SDevin Teske# f_dialog_yesno $msg_text ... 1035ab2043b8SDevin Teske# 1036ab2043b8SDevin Teske# Display a dialog(1) Yes/No prompt to allow the user to make some decision. 1037ab2043b8SDevin Teske# The yesno prompt remains until the user presses ENTER or ESC, acknowledging 1038ab2043b8SDevin Teske# the modal dialog. 1039ab2043b8SDevin Teske# 1040ab2043b8SDevin Teske# If the user chooses YES the exit status is zero, or chooses NO the exit 1041ab2043b8SDevin Teske# status is one, or presses ESC the exit status is 255. 1042ab2043b8SDevin Teske# 1043ab2043b8SDevin Teskef_dialog_yesno() 1044ab2043b8SDevin Teske{ 1045ab2043b8SDevin Teske local msg_text="$*" 1046ab2043b8SDevin Teske local hline="$hline_arrows_tab_enter" 1047ab2043b8SDevin Teske local size="$( f_dialog_buttonbox_size \ 1048ab2043b8SDevin Teske "$DIALOG_TITLE" \ 1049ab2043b8SDevin Teske "$DIALOG_BACKTITLE" \ 1050ab2043b8SDevin Teske "$msg_text" \ 1051ab2043b8SDevin Teske "$hline" )" 1052ab2043b8SDevin Teske 1053ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 1054ab2043b8SDevin Teske eval $DIALOG \ 1055ab2043b8SDevin Teske --title \"\$DIALOG_TITLE\" \ 1056ab2043b8SDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 1057ab2043b8SDevin Teske --hline \"\$hline\" \ 1058ab2043b8SDevin Teske --ok-label \"\$msg_yes\" \ 1059ab2043b8SDevin Teske --cancel-label \"\$msg_no\" \ 1060ab2043b8SDevin Teske --yesno \"\$msg_text\" $size 1061ab2043b8SDevin Teske else 1062ab2043b8SDevin Teske eval $DIALOG \ 1063ab2043b8SDevin Teske --title \"\$DIALOG_TITLE\" \ 1064ab2043b8SDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 1065ab2043b8SDevin Teske --hline \"\$hline\" \ 1066ab2043b8SDevin Teske --yes-label \"\$msg_yes\" \ 1067ab2043b8SDevin Teske --no-label \"\$msg_no\" \ 1068ab2043b8SDevin Teske --yesno \"\$msg_text\" $size 1069ab2043b8SDevin Teske fi 1070ab2043b8SDevin Teske} 1071ab2043b8SDevin Teske 1072ab2043b8SDevin Teske# f_dialog_noyes $msg_text ... 1073ab2043b8SDevin Teske# 1074ab2043b8SDevin Teske# Display a dialog(1) No/Yes prompt to allow the user to make some decision. 1075ab2043b8SDevin Teske# The noyes prompt remains until the user presses ENTER or ESC, acknowledging 1076ab2043b8SDevin Teske# the modal dialog. 1077ab2043b8SDevin Teske# 1078ab2043b8SDevin Teske# If the user chooses YES the exit status is zero, or chooses NO the exit 1079ab2043b8SDevin Teske# status is one, or presses ESC the exit status is 255. 1080ab2043b8SDevin Teske# 1081ab2043b8SDevin Teske# NOTE: This is just like the f_dialog_yesno function except "No" is default. 1082ab2043b8SDevin Teske# 1083ab2043b8SDevin Teskef_dialog_noyes() 1084ab2043b8SDevin Teske{ 1085ab2043b8SDevin Teske local msg_text="$*" 1086ab2043b8SDevin Teske local hline="$hline_arrows_tab_enter" 1087ab2043b8SDevin Teske local size="$( f_dialog_buttonbox_size \ 1088ab2043b8SDevin Teske "$DIALOG_TITLE" \ 1089ab2043b8SDevin Teske "$DIALOG_BACKTITLE" \ 1090ab2043b8SDevin Teske "$msg_text" \ 1091ab2043b8SDevin Teske "$hline" )" 1092ab2043b8SDevin Teske 1093ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 1094ab2043b8SDevin Teske eval $DIALOG \ 1095ab2043b8SDevin Teske --title \"\$DIALOG_TITLE\" \ 1096ab2043b8SDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 1097ab2043b8SDevin Teske --hline \"\$hline\" \ 1098ab2043b8SDevin Teske --default-no \ 1099ab2043b8SDevin Teske --ok-label \"\$msg_yes\" \ 1100ab2043b8SDevin Teske --cancel-label \"\$msg_no\" \ 1101ab2043b8SDevin Teske --yesno \"\$msg_text\" $size 1102ab2043b8SDevin Teske else 1103ab2043b8SDevin Teske eval $DIALOG \ 1104ab2043b8SDevin Teske --title \"\$DIALOG_TITLE\" \ 1105ab2043b8SDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 1106ab2043b8SDevin Teske --hline \"\$hline\" \ 1107ab2043b8SDevin Teske --defaultno \ 1108ab2043b8SDevin Teske --yes-label \"\$msg_yes\" \ 1109ab2043b8SDevin Teske --no-label \"\$msg_no\" \ 1110ab2043b8SDevin Teske --yesno \"\$msg_text\" $size 1111ab2043b8SDevin Teske fi 1112ab2043b8SDevin Teske} 1113ab2043b8SDevin Teske 1114ab2043b8SDevin Teske############################################################ INPUT FUNCTIONS 1115ab2043b8SDevin Teske 1116ab2043b8SDevin Teske# f_dialog_inputstr 1117ab2043b8SDevin Teske# 1118ab2043b8SDevin Teske# Obtain the inputstr entered by the user from the most recently displayed 1119298cf604SDevin Teske# dialog(1) inputbox and clean up any temporary files/variables. 1120ab2043b8SDevin Teske# 1121ab2043b8SDevin Teskef_dialog_inputstr() 1122ab2043b8SDevin Teske{ 1123ab2043b8SDevin Teske # Skip warnings and trim leading/trailing whitespace from user input 112489498fdfSDevin Teske eval echo \"\$DIALOG_INPUTBOX_$$\" | awk ' 1125ab2043b8SDevin Teske BEGIN { found = 0 } 1126ab2043b8SDevin Teske { 1127ab2043b8SDevin Teske if ( ! found ) 1128ab2043b8SDevin Teske { 1129ab2043b8SDevin Teske if ( $0 ~ /^$/ ) next 1130ab2043b8SDevin Teske if ( $0 ~ /^Gdk-WARNING \*\*:/ ) next 1131ab2043b8SDevin Teske found = 1 1132ab2043b8SDevin Teske } 1133ab2043b8SDevin Teske sub(/^[[:space:]]*/, "") 1134ab2043b8SDevin Teske sub(/[[:space:]]*$/, "") 1135ab2043b8SDevin Teske print 1136ab2043b8SDevin Teske } 113789498fdfSDevin Teske ' 113889498fdfSDevin Teske setvar DIALOG_INPUTBOX_$$ "" # scrub memory in case data was sensitive 1139ab2043b8SDevin Teske return $SUCCESS 1140ab2043b8SDevin Teske} 1141ab2043b8SDevin Teske 1142298cf604SDevin Teske# f_dialog_input $prompt [$init [$hline]] 1143298cf604SDevin Teske# 1144298cf604SDevin Teske# Prompt the user with a dialog(1) inputbox to enter some value. The inputbox 1145298cf604SDevin Teske# remains until the the user presses ENTER or ESC, or otherwise ends the 1146298cf604SDevin Teske# editing session, by selecting `Cancel' for example. 1147298cf604SDevin Teske# 1148298cf604SDevin Teske# If the user presses ENTER, the exit status is zero (success), otherwise if 1149298cf604SDevin Teske# the user presses ESC the exit status is 255, or if the user chose Cancel, the 1150298cf604SDevin Teske# exit status is instead 1. 1151298cf604SDevin Teske# 1152298cf604SDevin Teske# NOTE: The hline should correspond to the type of data you want from the user. 1153298cf604SDevin Teske# NOTE: Should not be used to edit multiline values. 1154298cf604SDevin Teske# 1155298cf604SDevin Teskef_dialog_input() 1156298cf604SDevin Teske{ 1157298cf604SDevin Teske local prompt="$1" init="$2" hline="$3" 1158298cf604SDevin Teske local size="$( f_dialog_inputbox_size \ 1159298cf604SDevin Teske "$DIALOG_TITLE" \ 1160298cf604SDevin Teske "$DIALOG_BACKTITLE" \ 1161298cf604SDevin Teske "$prompt" \ 1162298cf604SDevin Teske "$init" \ 1163298cf604SDevin Teske "$hline" )" 1164298cf604SDevin Teske 1165298cf604SDevin Teske local opterm="--" 1166298cf604SDevin Teske [ "$USE_XDIALOG" ] && opterm= 1167298cf604SDevin Teske 1168298cf604SDevin Teske local dialog_input 1169298cf604SDevin Teske dialog_input=$( 1170298cf604SDevin Teske eval $DIALOG \ 1171298cf604SDevin Teske --title \"\$DIALOG_TITLE\" \ 1172298cf604SDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 1173298cf604SDevin Teske --hline \"\$hline\" \ 1174298cf604SDevin Teske --ok-label \"\$msg_ok\" \ 1175298cf604SDevin Teske --cancel-label \"\$msg_cancel\" \ 1176298cf604SDevin Teske --inputbox \"\$prompt\" $size \ 1177298cf604SDevin Teske $opterm \"\$init\" \ 1178298cf604SDevin Teske 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 1179298cf604SDevin Teske ) 1180298cf604SDevin Teske local retval=$? 1181298cf604SDevin Teske 1182298cf604SDevin Teske setvar DIALOG_INPUTBOX_$$ "$dialog_input" 1183298cf604SDevin Teske f_dialog_inputstr 1184298cf604SDevin Teske 1185298cf604SDevin Teske return $retval 1186298cf604SDevin Teske} 1187298cf604SDevin Teske 1188ab2043b8SDevin Teske############################################################ MENU FUNCTIONS 1189ab2043b8SDevin Teske 1190ab2043b8SDevin Teske# f_dialog_menutag 1191ab2043b8SDevin Teske# 1192ab2043b8SDevin Teske# Obtain the menutag chosen by the user from the most recently displayed 1193298cf604SDevin Teske# dialog(1) menu and clean up any temporary files/variables. 1194ab2043b8SDevin Teske# 1195ab2043b8SDevin Teskef_dialog_menutag() 1196ab2043b8SDevin Teske{ 119789498fdfSDevin Teske # Skip warnings 119889498fdfSDevin Teske eval echo \"\$DIALOG_MENU_$$\" | awk ' 1199ab2043b8SDevin Teske BEGIN { found = 0 } 1200ab2043b8SDevin Teske { 1201ab2043b8SDevin Teske if ( found ) # ... just spew 1202ab2043b8SDevin Teske { 1203ab2043b8SDevin Teske print 1204ab2043b8SDevin Teske next 1205ab2043b8SDevin Teske } 1206ab2043b8SDevin Teske if ( $0 ~ /^$/ ) next 1207ab2043b8SDevin Teske if ( $0 ~ /^Gdk-WARNING \*\*:/ ) next 1208ab2043b8SDevin Teske found = 1 1209ab2043b8SDevin Teske print 1210ab2043b8SDevin Teske } 121189498fdfSDevin Teske ' 121289498fdfSDevin Teske setvar DIALOG_MENU_$$ "" # scrub memory in case data was sensitive 1213ab2043b8SDevin Teske return $SUCCESS 1214ab2043b8SDevin Teske} 1215ab2043b8SDevin Teske 1216ab2043b8SDevin Teske# f_dialog_menutag2item $tag_chosen $tag1 $item1 $tag2 $item2 ... 1217ab2043b8SDevin Teske# 1218ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) you must pass an ordered list of 1219ab2043b8SDevin Teske# tag/item pairs on the command-line. When the user selects a menu option the 1220ab2043b8SDevin Teske# tag for that item is printed to stderr. 1221ab2043b8SDevin Teske# 1222ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into 1223ab2043b8SDevin Teske# the item associated with said tag. 1224ab2043b8SDevin Teske# 1225ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the 1226ab2043b8SDevin Teske# ordered list of tag/item pairs (HINT: use the same tag/item list as was 1227ab2043b8SDevin Teske# passed to dialog(1) for consistency). 1228ab2043b8SDevin Teske# 1229ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned. 1230ab2043b8SDevin Teske# 1231ab2043b8SDevin Teskef_dialog_menutag2item() 1232ab2043b8SDevin Teske{ 1233ab2043b8SDevin Teske local tag="$1" tagn item 1234ab2043b8SDevin Teske shift 1 # tag 1235ab2043b8SDevin Teske 1236ab2043b8SDevin Teske while [ $# -gt 0 ]; do 1237ab2043b8SDevin Teske tagn="$1" 1238ab2043b8SDevin Teske item="$2" 1239ab2043b8SDevin Teske shift 2 # tagn/item 1240ab2043b8SDevin Teske 1241ab2043b8SDevin Teske if [ "$tag" = "$tagn" ]; then 1242ab2043b8SDevin Teske echo "$item" 1243ab2043b8SDevin Teske return $SUCCESS 1244ab2043b8SDevin Teske fi 1245ab2043b8SDevin Teske done 1246ab2043b8SDevin Teske return $FAILURE 1247ab2043b8SDevin Teske} 1248ab2043b8SDevin Teske 1249ab2043b8SDevin Teske# f_dialog_menutag2item_with_help $tag_chosen $tag1 $item1 $help1 \ 1250ab2043b8SDevin Teske# $tag2 $item2 $help2 ... 1251ab2043b8SDevin Teske# 1252ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) with the `--item-help' option, you 1253ab2043b8SDevin Teske# must pass an ordered list of tag/item/help triplets on the command-line. When 1254ab2043b8SDevin Teske# the user selects a menu option the tag for that item is printed to stderr. 1255ab2043b8SDevin Teske# 1256ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into 1257ab2043b8SDevin Teske# the item associated with said tag (help is discarded/ignored). 1258ab2043b8SDevin Teske# 1259ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the 1260ab2043b8SDevin Teske# ordered list of tag/item/help triplets (HINT: use the same tag/item/help list 1261ab2043b8SDevin Teske# as was passed to dialog(1) for consistency). 1262ab2043b8SDevin Teske# 1263ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned. 1264ab2043b8SDevin Teske# 1265ab2043b8SDevin Teskef_dialog_menutag2item_with_help() 1266ab2043b8SDevin Teske{ 1267ab2043b8SDevin Teske local tag="$1" tagn item 1268ab2043b8SDevin Teske shift 1 # tag 1269ab2043b8SDevin Teske 1270ab2043b8SDevin Teske while [ $# -gt 0 ]; do 1271ab2043b8SDevin Teske tagn="$1" 1272ab2043b8SDevin Teske item="$2" 1273ab2043b8SDevin Teske shift 3 # tagn/item/help 1274ab2043b8SDevin Teske 1275ab2043b8SDevin Teske if [ "$tag" = "$tagn" ]; then 1276ab2043b8SDevin Teske echo "$item" 1277ab2043b8SDevin Teske return $SUCCESS 1278ab2043b8SDevin Teske fi 1279ab2043b8SDevin Teske done 1280ab2043b8SDevin Teske return $FAILURE 1281ab2043b8SDevin Teske} 1282ab2043b8SDevin Teske 1283ab2043b8SDevin Teske# f_dialog_menutag2index $tag_chosen $tag1 $item1 $tag2 $item2 ... 1284ab2043b8SDevin Teske# 1285ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) you must pass an ordered list of 1286ab2043b8SDevin Teske# tag/item pairs on the command-line. When the user selects a menu option the 1287ab2043b8SDevin Teske# tag for that item is printed to stderr. 1288ab2043b8SDevin Teske# 1289ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into 1290ab2043b8SDevin Teske# the index associated with said tag. The index is the one-based tag/item pair 1291ab2043b8SDevin Teske# array position within the ordered list of tag/item pairs passed to dialog(1). 1292ab2043b8SDevin Teske# 1293ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the 1294ab2043b8SDevin Teske# ordered list of tag/item pairs (HINT: use the same tag/item list as was 1295ab2043b8SDevin Teske# passed to dialog(1) for consistency). 1296ab2043b8SDevin Teske# 1297ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned. 1298ab2043b8SDevin Teske# 1299ab2043b8SDevin Teskef_dialog_menutag2index() 1300ab2043b8SDevin Teske{ 1301ab2043b8SDevin Teske local tag="$1" tagn n=1 1302ab2043b8SDevin Teske shift 1 # tag 1303ab2043b8SDevin Teske 1304ab2043b8SDevin Teske while [ $# -gt 0 ]; do 1305ab2043b8SDevin Teske tagn="$1" 1306ab2043b8SDevin Teske shift 2 # tagn/item 1307ab2043b8SDevin Teske 1308ab2043b8SDevin Teske if [ "$tag" = "$tagn" ]; then 1309ab2043b8SDevin Teske echo $n 1310ab2043b8SDevin Teske return $SUCCESS 1311ab2043b8SDevin Teske fi 1312ab2043b8SDevin Teske n=$(( $n + 1 )) 1313ab2043b8SDevin Teske done 1314ab2043b8SDevin Teske return $FAILURE 1315ab2043b8SDevin Teske} 1316ab2043b8SDevin Teske 1317ab2043b8SDevin Teske# f_dialog_menutag2index_with_help $tag_chosen $tag1 $item1 $help1 \ 1318ab2043b8SDevin Teske# $tag2 $item2 $help2 ... 1319ab2043b8SDevin Teske# 1320ab2043b8SDevin Teske# To use the `--menu' option of dialog(1) with the `--item-help' option, you 1321ab2043b8SDevin Teske# must pass an ordered list of tag/item/help triplets on the command-line. When 1322ab2043b8SDevin Teske# the user selects a menu option the tag for that item is printed to stderr. 1323ab2043b8SDevin Teske# 1324ab2043b8SDevin Teske# This function allows you to dereference the tag chosen by the user back into 1325ab2043b8SDevin Teske# the index associated with said tag. The index is the one-based tag/item/help 1326ab2043b8SDevin Teske# triplet array position within the ordered list of tag/item/help triplets 1327ab2043b8SDevin Teske# passed to dialog(1). 1328ab2043b8SDevin Teske# 1329ab2043b8SDevin Teske# Pass the tag chosen by the user as the first argument, followed by the 1330ab2043b8SDevin Teske# ordered list of tag/item/help triplets (HINT: use the same tag/item/help list 1331ab2043b8SDevin Teske# as was passed to dialog(1) for consistency). 1332ab2043b8SDevin Teske# 1333ab2043b8SDevin Teske# If the tag cannot be found, NULL is returned. 1334ab2043b8SDevin Teske# 1335ab2043b8SDevin Teskef_dialog_menutag2index_with_help() 1336ab2043b8SDevin Teske{ 1337ab2043b8SDevin Teske local tag="$1" tagn n=1 1338ab2043b8SDevin Teske shift 1 # tag 1339ab2043b8SDevin Teske 1340ab2043b8SDevin Teske while [ $# -gt 0 ]; do 1341ab2043b8SDevin Teske tagn="$1" 1342ab2043b8SDevin Teske shift 3 # tagn/item/help 1343ab2043b8SDevin Teske 1344ab2043b8SDevin Teske if [ "$tag" = "$tagn" ]; then 1345ab2043b8SDevin Teske echo $n 1346ab2043b8SDevin Teske return $SUCCESS 1347ab2043b8SDevin Teske fi 1348ab2043b8SDevin Teske n=$(( $n + 1 )) 1349ab2043b8SDevin Teske done 1350ab2043b8SDevin Teske return $FAILURE 1351ab2043b8SDevin Teske} 1352ab2043b8SDevin Teske 1353ab2043b8SDevin Teske############################################################ INIT FUNCTIONS 1354ab2043b8SDevin Teske 1355ab2043b8SDevin Teske# f_dialog_init 1356ab2043b8SDevin Teske# 1357ab2043b8SDevin Teske# Initialize (or re-initialize) the dialog module after setting/changing any 1358ab2043b8SDevin Teske# of the following environment variables: 1359ab2043b8SDevin Teske# 1360ab2043b8SDevin Teske# USE_XDIALOG Either NULL or Non-NULL. If given a value will indicate 1361ab2043b8SDevin Teske# that Xdialog(1) should be used instead of dialog(1). 1362ab2043b8SDevin Teske# 1363ab2043b8SDevin Teske# SECURE Either NULL or Non-NULL. If given a value will indicate 1364ab2043b8SDevin Teske# that (while running as root) sudo(8) authentication is 1365ab2043b8SDevin Teske# required to proceed. 1366ab2043b8SDevin Teske# 1367ab2043b8SDevin Teskef_dialog_init() 1368ab2043b8SDevin Teske{ 1369ab2043b8SDevin Teske DIALOG_SELF_INITIALIZE= 1370ab2043b8SDevin Teske 1371ab2043b8SDevin Teske # 137289498fdfSDevin Teske # Clone terminal stdout so we can redirect to it from within sub-shells 137389498fdfSDevin Teske # 137489498fdfSDevin Teske eval exec $DIALOG_TERMINAL_PASSTHRU_FD\>\&1 137589498fdfSDevin Teske 137689498fdfSDevin Teske # 1377ab2043b8SDevin Teske # Process stored command-line arguments 1378ab2043b8SDevin Teske # 1379ab2043b8SDevin Teske SECURE=$( set -- "$ARGV" 1380fb7d723eSDevin Teske while getopts S flag > /dev/null; do 1381ab2043b8SDevin Teske case "$flag" in 1382ab2043b8SDevin Teske S) echo 1;; 1383ab2043b8SDevin Teske \?) continue;; 1384ab2043b8SDevin Teske esac 1385ab2043b8SDevin Teske done 1386ab2043b8SDevin Teske ) 1387ab2043b8SDevin Teske USE_XDIALOG=$( set -- "$ARGV" 1388fb7d723eSDevin Teske while getopts SX flag > /dev/null; do 1389ab2043b8SDevin Teske case "$flag" in 1390ab2043b8SDevin Teske S|X) echo 1;; 1391ab2043b8SDevin Teske \?) continue;; 1392ab2043b8SDevin Teske esac 1393ab2043b8SDevin Teske done 1394ab2043b8SDevin Teske ) 1395ab2043b8SDevin Teske 1396ab2043b8SDevin Teske # 1397ab2043b8SDevin Teske # Process `-X' command-line option 1398ab2043b8SDevin Teske # 1399ab2043b8SDevin Teske [ "$USE_XDIALOG" ] && DIALOG=Xdialog 1400ab2043b8SDevin Teske 1401ab2043b8SDevin Teske # 1402ab2043b8SDevin Teske # Sanity check, or die gracefully 1403ab2043b8SDevin Teske # 1404ab2043b8SDevin Teske if ! f_have $DIALOG; then 1405ab2043b8SDevin Teske unset USE_XDIALOG 1406ab2043b8SDevin Teske failed_dialog="$DIALOG" 1407ab2043b8SDevin Teske DIALOG=dialog 1408ab2043b8SDevin Teske f_die 1 "$msg_no_such_file_or_directory" "$pgm" "$failed_dialog" 1409ab2043b8SDevin Teske fi 1410ab2043b8SDevin Teske 1411ab2043b8SDevin Teske # 1412ab2043b8SDevin Teske # If we're already running as root but we got there by way of sudo(8) 1413ab2043b8SDevin Teske # and we have X11, we should merge the xauth(1) credentials from our 1414ab2043b8SDevin Teske # original user. 1415ab2043b8SDevin Teske # 1416ab2043b8SDevin Teske if [ "$USE_XDIALOG" ] && 1417ab2043b8SDevin Teske [ "$( id -u )" = "0" ] && 1418ab2043b8SDevin Teske [ "$SUDO_USER" -a "$DISPLAY" ] 1419ab2043b8SDevin Teske then 1420ab2043b8SDevin Teske if ! f_have xauth; then 1421ab2043b8SDevin Teske # Die gracefully, as we [likely] can't use Xdialog(1) 1422ab2043b8SDevin Teske unset USE_XDIALOG 1423ab2043b8SDevin Teske DIALOG=dialog 1424ab2043b8SDevin Teske f_die 1 "$msg_no_such_file_or_directory" "$pgm" "xauth" 1425ab2043b8SDevin Teske fi 1426ab2043b8SDevin Teske HOSTNAME=$(hostname) 1427ab2043b8SDevin Teske displaynum="${DISPLAY#*:}" 1428ab2043b8SDevin Teske eval xauth -if \~$SUDO_USER/.Xauthority extract - \ 1429ab2043b8SDevin Teske \"\$HOSTNAME/unix:\$displaynum\" \ 1430ab2043b8SDevin Teske \"\$HOSTNAME:\$displaynum\" | sudo sh -c 'xauth -ivf \ 1431fb7d723eSDevin Teske ~root/.Xauthority merge - > /dev/null 2>&1' 1432ab2043b8SDevin Teske fi 1433ab2043b8SDevin Teske 1434ab2043b8SDevin Teske # 1435ab2043b8SDevin Teske # Probe Xdialog(1) for maximum height/width constraints, or die 1436ab2043b8SDevin Teske # gracefully 1437ab2043b8SDevin Teske # 1438ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 1439ab2043b8SDevin Teske if ! maxsize=$( LANG= LC_ALL= $DIALOG --print-maxsize 2>&1 ) 1440ab2043b8SDevin Teske then 1441ab2043b8SDevin Teske # Xdialog(1) failed, fall back to dialog(1) 1442ab2043b8SDevin Teske unset USE_XDIALOG 1443ab2043b8SDevin Teske size=$( f_dialog_buttonbox_size "$DIALOG_TITLE" \ 1444ab2043b8SDevin Teske "$DIALOG_BACKTITLE" \ 1445ab2043b8SDevin Teske "$maxsize" "" ) 1446ab2043b8SDevin Teske eval dialog \ 1447ab2043b8SDevin Teske --title \"\$DIALOG_TITLE\" \ 1448ab2043b8SDevin Teske --backtitle \"\$DIALOG_BACKTITLE\" \ 1449ab2043b8SDevin Teske --ok-label \"\$msg_ok\" \ 1450ab2043b8SDevin Teske --msgbox \"\$maxsize\" $size 1451ab2043b8SDevin Teske exit $FAILURE 1452ab2043b8SDevin Teske fi 1453ab2043b8SDevin Teske 1454ab2043b8SDevin Teske XDIALOG_MAXSIZE=$( 1455ab2043b8SDevin Teske set -- ${maxsize##*:} 1456ab2043b8SDevin Teske 1457ab2043b8SDevin Teske height=${1%,} 1458ab2043b8SDevin Teske width=$2 1459ab2043b8SDevin Teske 1460ab2043b8SDevin Teske echo $height $width 1461ab2043b8SDevin Teske ) 1462ab2043b8SDevin Teske unset maxsize 1463ab2043b8SDevin Teske fi 1464ab2043b8SDevin Teske 1465ab2043b8SDevin Teske # 1466ab2043b8SDevin Teske # If using Xdialog(1), swap DIALOG_TITLE with DIALOG_BACKTITLE. 1467ab2043b8SDevin Teske # The reason for this is because many dialog(1) applications use 1468ab2043b8SDevin Teske # --backtitle for the program name (which is better suited as 1469ab2043b8SDevin Teske # --title with Xdialog(1)). 1470ab2043b8SDevin Teske # 1471ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 1472ab2043b8SDevin Teske _DIALOG_TITLE="$DIALOG_TITLE" 1473ab2043b8SDevin Teske DIALOG_TITLE="$DIALOG_BACKTITLE" 1474ab2043b8SDevin Teske DIALOG_BACKTITLE="$_DIALOG_TITLE" 1475ab2043b8SDevin Teske unset _DIALOG_TITLE 1476ab2043b8SDevin Teske fi 1477*56961fd7SDevin Teske 1478*56961fd7SDevin Teske f_dprintf "f_dialog_init: dialog(1) API initialized." 1479ab2043b8SDevin Teske} 1480ab2043b8SDevin Teske 148179ec1cd5SDevin Teske############################################################ MAIN 148279ec1cd5SDevin Teske 1483*56961fd7SDevin Teskef_dprintf "%s: DIALOG_SELF_INITIALIZE=[%s]" \ 1484*56961fd7SDevin Teske dialog.subr "$DIALOG_SELF_INITIALIZE" 1485d3c2b8b6SDevin Teskecase "$DIALOG_SELF_INITIALIZE" in 1486d3c2b8b6SDevin Teske""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;; 1487d3c2b8b6SDevin Teske*) f_dialog_init 1488d3c2b8b6SDevin Teskeesac 148979ec1cd5SDevin Teske 1490*56961fd7SDevin Teskef_dprintf "%s: Successfully loaded." dialog.subr 1491*56961fd7SDevin Teske 1492ab2043b8SDevin Teskefi # ! $_DIALOG_SUBR 1493