1if [ ! "$_DIALOG_SUBR" ]; then _DIALOG_SUBR=1 2# |
3# Copyright (c) 2006-2014 Devin Teske |
4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. Redistributions in binary form must reproduce the above copyright --- 7 unchanged lines hidden (view full) --- 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26# |
27# $FreeBSD: stable/9/usr.sbin/bsdconfig/share/dialog.subr 263791 2014-03-27 03:20:47Z dteske $ |
28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33f_dprintf "%s: loading includes..." dialog.subr 34f_include $BSDCFG_SHARE/strings.subr 35f_include $BSDCFG_SHARE/variable.subr --- 33 unchanged lines hidden (view full) --- 69# Declare that we are fully-compliant with Xdialog(1) by unset'ing all 70# compatibility settings. 71# 72unset XDIALOG_HIGH_DIALOG_COMPAT 73unset XDIALOG_FORCE_AUTOSIZE 74unset XDIALOG_INFOBOX_TIMEOUT 75 76# |
77# Exit codes for [X]dialog(1) 78# 79DIALOG_OK=${SUCCESS:-0} 80DIALOG_CANCEL=${FAILURE:-1} 81DIALOG_HELP=2 82DIALOG_ITEM_HELP=2 83DIALOG_EXTRA=3 84DIALOG_ITEM_HELP=4 85export DIALOG_ERROR=254 # sh(1) can't handle the default of `-1' 86DIALOG_ESC=255 87 88# |
89# Default behavior is to call f_dialog_init() automatically when loaded. 90# 91: ${DIALOG_SELF_INITIALIZE=1} 92 93# 94# Default terminal size (used if/when running without a controlling terminal) 95# 96: ${DEFAULT_TERMINAL_SIZE:=24 80} --- 194 unchanged lines hidden (view full) --- 291 292# f_dialog_max_size $var_height $var_width 293# 294# Get the maximum height and width for a dialog widget and store the values in 295# $var_height and $var_width (respectively). 296# 297f_dialog_max_size() 298{ |
299 local funcname=f_dialog_max_size |
300 local __var_height="$1" __var_width="$2" __max_size 301 [ "$__var_height" -o "$__var_width" ] || return $FAILURE 302 if [ "$USE_XDIALOG" ]; then 303 __max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION 304 else |
305 if __max_size=$( $DIALOG --print-maxsize \ 306 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) 307 then 308 f_dprintf "$funcname: %s --print-maxsize = [%s]" \ 309 "$DIALOG" "$__max_size" 310 # usually "MaxSize: 24, 80" 311 __max_size="${__max_size#*: }" 312 f_replaceall "$__max_size" "," "" __max_size 313 else 314 f_eval_catch -dk __max_size $funcname stty \ 315 'stty size' || __max_size= 316 # usually "24 80" 317 fi |
318 : ${__max_size:=$DEFAULT_TERMINAL_SIZE} 319 fi 320 if [ "$__var_height" ]; then 321 local __height="${__max_size%%[$IFS]*}" 322 # 323 # If we're not using Xdialog(1), we should assume that $DIALOG 324 # will render --backtitle behind the widget. In such a case, we 325 # should prevent a widget from obscuring the backtitle (unless --- 97 unchanged lines hidden (view full) --- 423 setvar "$__var_width" $__min_width 424 elif [ $__width -gt $__max_width_size_constrain ]; then 425 setvar "$__var_width" $__max_width_size_constrain 426 fi 427 fi 428 429 if [ "$debug" ]; then 430 # Print final constrained values to debugging |
431 [ "$__var_height" ] && f_quietly f_getvar "$__var_height" 432 [ "$__var_width" ] && f_quietly f_getvar "$__var_width" |
433 fi 434 435 return $__retval # success if no debug warnings were printed 436} 437 438# f_dialog_menu_constrain $var_height $var_width $var_rows "$prompt" \ 439# [$min_height [$min_width [$min_rows]]] 440# --- 23 unchanged lines hidden (view full) --- 464 # Return failure unless at least one var_* argument is passed 465 [ "$__var_height" -o "$__var_width" -o "$__var_rows" ] || 466 return $FAILURE 467 468 # 469 # Print debug warnings if any given (non-NULL) argument are invalid 470 # NOTE: Don't change the name of $__{var,min,}{height,width,rows} 471 # |
472 local __height_menu_constrain __width_menu_constrain 473 local __rows_menu_constrain |
474 local __arg __cp __fname=f_dialog_menu_constrain 475 for __arg in height width rows; do 476 debug= f_getvar __var_$__arg __cp 477 [ "$__cp" ] || continue |
478 if ! debug= f_getvar "$__cp" __${__arg}_menu_constrain; then |
479 f_dprintf "%s: var_%s variable \`%s' not set" \ 480 $__fname $__arg "$__cp" 481 __retval=$FAILURE |
482 elif ! eval f_isinteger \$__${__arg}_menu_constrain; then |
483 f_dprintf "%s: var_%s variable value not a number" \ 484 $__fname $__arg 485 __retval=$FAILURE 486 fi 487 done 488 for __arg in height width rows; do 489 debug= f_getvar __min_$__arg __cp 490 [ "$__cp" ] || continue --- 7 unchanged lines hidden (view full) --- 498 # NOTE: Function name appended to prevent __var_{height,width} values 499 # from becoming local (and thus preventing setvar from working). 500 local __max_height_menu_constrain __max_width_menu_constrain 501 f_dialog_max_size \ 502 __max_height_menu_constrain __max_width_menu_constrain 503 504 # Adjust height if desired 505 if [ "$__var_height" ]; then |
506 if [ $__height_menu_constrain -lt ${__min_height:-0} ]; then |
507 setvar "$__var_height" $__min_height |
508 elif [ $__height_menu_constrain -gt \ 509 $__max_height_menu_constrain ] 510 then |
511 setvar "$__var_height" $__max_height_menu_constrain 512 fi 513 fi 514 515 # Adjust width if desired 516 if [ "$__var_width" ]; then 517 if [ "$USE_XDIALOG" ]; then 518 : ${__min_width:=${XDIALOG_MIN_WIDTH:-35}} 519 else 520 : ${__min_width:=${DIALOG_MIN_WIDTH:-24}} 521 fi |
522 if [ $__width_menu_constrain -lt $__min_width ]; then |
523 setvar "$__var_width" $__min_width |
524 elif [ $__width_menu_constrain -gt \ 525 $__max_width_menu_constrain ] 526 then |
527 setvar "$__var_width" $__max_width_menu_constrain 528 fi 529 fi 530 531 # Adjust rows if desired 532 if [ "$__var_rows" ]; then 533 if [ "$USE_XDIALOG" ]; then 534 : ${__min_rows:=1} 535 else 536 : ${__min_rows:=0} 537 fi 538 |
539 local __max_rows_menu_constrain=$(( 540 $__max_height_menu_constrain - 7 541 )) |
542 # If prompt_len is zero (no prompt), bump the max-rows by 1 543 # Default assumption is (if no argument) that there's no prompt |
544 [ ${__prompt_len:-0} -gt 0 ] || __max_rows_menu_constrain=$(( 545 $__max_rows_menu_constrain + 1 546 )) |
547 |
548 if [ $__rows_menu_constrain -lt $__min_rows ]; then |
549 setvar "$__var_rows" $__min_rows |
550 elif [ $__rows_menu_constrain -gt $__max_rows_menu_constrain ] 551 then 552 setvar "$__var_rows" $__max_rows_menu_constrain |
553 fi 554 fi 555 556 if [ "$debug" ]; then |
557 # Print final constrained values to debugging 558 [ "$__var_height" ] && f_quietly f_getvar "$__var_height" 559 [ "$__var_width" ] && f_quietly f_getvar "$__var_width" 560 [ "$__var_rows" ] && f_quietly f_getvar "$__var_rows" |
561 fi 562 563 return $__retval # success if no debug warnings were printed 564} 565 566# f_dialog_infobox_size [-n] $var_height $var_width \ 567# $title $backtitle $prompt [$hline] 568# --- 535 unchanged lines hidden (view full) --- 1104 "${__var_width:+__width_rlist_size}" \ 1105 "$__title" "$__btitle" "$__prompt" "$__hline" 1106 1107 # 1108 # Always process the menu-item arguments to get the longest tag-length, 1109 # longest item-length (both used to bump the width), and the number of 1110 # rows (used to bump the height). 1111 # |
1112 local __longest_tag=0 __longest_item=0 __rows_rlist_size=0 |
1113 while [ $# -ge 3 ]; do 1114 local __tag="$1" __item="$2" 1115 shift 3 # tag/item/status 1116 [ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag} 1117 [ ${#__item} -gt $__longest_item ] && __longest_item=${#__item} |
1118 __rows_rlist_size=$(( $__rows_rlist_size + 1 )) |
1119 done 1120 1121 # Adjust rows early (for up-coming height calculation) 1122 if [ "$__var_height" -o "$__var_rows" ]; then 1123 # Add a row for visual aid if using Xdialog(1) |
1124 [ "$USE_XDIALOG" ] && 1125 __rows_rlist_size=$(( $__rows_rlist_size + 1 )) |
1126 fi 1127 1128 # Adjust height if desired 1129 if [ "$__var_height" ]; then 1130 # Add rows to height 1131 if [ "$USE_XDIALOG" ]; then 1132 __height_rlist_size=$(( |
1133 $__height_rlist_size + $__rows_rlist_size + 7 1134 )) |
1135 else 1136 __height_rlist_size=$(( |
1137 $__height_rlist_size + $__rows_rlist_size + 4 1138 )) |
1139 fi 1140 setvar "$__var_height" $__height_rlist_size 1141 fi 1142 1143 # Adjust width if desired 1144 if [ "$__var_width" ]; then 1145 # Sum total between longest tag-length, longest item-length, 1146 # and radio-button width should be used to bump menu width 1147 local __n=$(( $__longest_tag + $__longest_item + 13 )) 1148 [ "$USE_XDIALOG" ] && __n=$(( $__n + $__n / 6 )) # plus 16.6% 1149 [ $__n -gt $__width_rlist_size ] && __width_rlist_size=$__n 1150 1151 setvar "$__var_width" $__width_rlist_size 1152 fi 1153 1154 # Store adjusted rows if desired |
1155 [ "$__var_rows" ] && setvar "$__var_rows" $__rows_rlist_size |
1156 1157 # Constrain height, width, and rows to sensible minimum/maximum values 1158 # Return success if no-constrain, else return status from constrain 1159 [ ! "$__constrain" ] || f_dialog_menu_constrain \ 1160 "$__var_height" "$__var_width" "$__var_rows" "$__prompt" 1161} 1162 1163# f_dialog_checklist_size [-n] $var_height $var_width $var_rows \ --- 63 unchanged lines hidden (view full) --- 1227 1228 # 1229 # Always process the menu-item arguments to get the longest tag-length, 1230 # longest item-length, longest help-length (help-length only considered 1231 # if using Xdialog(1), as it places the help string in the widget) -- 1232 # all used to bump the width -- and the number of rows (used to bump 1233 # the height). 1234 # |
1235 local __longest_tag=0 __longest_item=0 __longest_help=0 1236 local __rows_rlist_with_help_size=0 |
1237 while [ $# -ge 4 ]; do 1238 local __tag="$1" __item="$2" __status="$3" __help="$4" 1239 shift 4 # tag/item/status/help 1240 [ ${#__tag} -gt $__longest_tag ] && __longest_tag=${#__tag} 1241 [ ${#__item} -gt $__longest_item ] && __longest_item=${#__item} 1242 [ ${#__help} -gt $__longest_help ] && __longest_help=${#__help} |
1243 __rows_rlist_with_help_size=$(( 1244 $__rows_rlist_with_help_size + 1 1245 )) |
1246 done 1247 1248 # Adjust rows early (for up-coming height calculation) 1249 if [ "$__var_height" -o "$__var_rows" ]; then 1250 # Add a row for visual aid if using Xdialog(1) |
1251 [ "$USE_XDIALOG" ] && 1252 __rows_rlist_with_help_size=$(( 1253 $__rows_rlist_with_help_size + 1 1254 )) |
1255 fi 1256 1257 # Adjust height if desired 1258 if [ "$__var_height" ]; then 1259 # Add rows to height 1260 if [ "$USE_XDIALOG" ]; then 1261 __height_rlist_with_help_size=$(( |
1262 $__height_rlist_with_help_size + 1263 $__rows_rlist_with_help_size + 7 1264 )) |
1265 else 1266 __height_rlist_with_help_size=$(( |
1267 $__height_rlist_with_help_size + 1268 $__rows_rlist_with_help_size + 4 1269 )) |
1270 fi 1271 setvar "$__var_height" $__height 1272 fi 1273 1274 # Adjust width if desired 1275 if [ "$__var_width" ]; then 1276 # Sum total between longest tag-length, longest item-length, 1277 # and radio-button width should be used to bump menu width --- 9 unchanged lines hidden (view full) --- 1287 [ $__n -gt $__width_rlist_with_help_size ] && 1288 __width_rlist_with_help_size=$__n 1289 fi 1290 1291 setvar "$__var_width" $__width_rlist_with_help_size 1292 fi 1293 1294 # Store adjusted rows if desired |
1295 [ "$__var_rows" ] && setvar "$__var_rows" $__rows_rlist_with_help_size |
1296 1297 # Constrain height, width, and rows to sensible minimum/maximum values 1298 # Return success if no-constrain, else return status from constrain 1299 [ ! "$__constrain" ] || f_dialog_menu_constrain \ 1300 "$__var_height" "$__var_width" "$__var_rows" "$__prompt" 1301} 1302 1303# f_dialog_checklist_with_help_size [-n] $var_height $var_width $var_rows \ --- 769 unchanged lines hidden (view full) --- 2073# Also reads ~/.dialogrc for the following information: 2074# 2075# NO_SHADOW Either NULL or Non-NULL. If use_shadow is OFF (case- 2076# insensitive) in ~/.dialogrc this is set to "1" (otherwise 2077# unset). 2078# 2079f_dialog_init() 2080{ |
2081 local funcname=f_dialog_init 2082 |
2083 DIALOG_SELF_INITIALIZE= |
2084 USE_DIALOG=1 |
2085 2086 # 2087 # Clone terminal stdout so we can redirect to it from within sub-shells 2088 # 2089 eval exec $DIALOG_TERMINAL_PASSTHRU_FD\>\&1 2090 2091 # 2092 # Add `-S' and `-X' to the list of standard arguments supported by all --- 27 unchanged lines hidden (view full) --- 2120 done 2121 ) 2122 f_dprintf "f_dialog_init: SECURE=[%s] USE_XDIALOG=[%s]" \ 2123 "$SECURE" "$USE_XDIALOG" 2124 2125 # 2126 # Process `-X' command-line option 2127 # |
2128 [ "$USE_XDIALOG" ] && DIALOG=Xdialog USE_DIALOG= |
2129 2130 # 2131 # Sanity check, or die gracefully 2132 # 2133 if ! f_have $DIALOG; then 2134 unset USE_XDIALOG 2135 local failed_dialog="$DIALOG" 2136 DIALOG=dialog --- 26 unchanged lines hidden (view full) --- 2163 [ "$SUDO_USER" -a "$DISPLAY" ] 2164 then 2165 if ! f_have xauth; then 2166 # Die gracefully, as we [likely] can't use Xdialog(1) 2167 unset USE_XDIALOG 2168 DIALOG=dialog 2169 f_die 1 "$msg_no_such_file_or_directory" "$pgm" "xauth" 2170 fi |
2171 HOSTNAME=$( hostname ) |
2172 local displaynum="${DISPLAY#*:}" 2173 eval xauth -if \~$SUDO_USER/.Xauthority extract - \ 2174 \"\$HOSTNAME/unix:\$displaynum\" \ 2175 \"\$HOSTNAME:\$displaynum\" | sudo sh -c 'xauth -ivf \ 2176 ~root/.Xauthority merge - > /dev/null 2>&1' 2177 fi 2178 2179 # 2180 # Probe Xdialog(1) for maximum height/width constraints, or die 2181 # gracefully 2182 # 2183 if [ "$USE_XDIALOG" ]; then 2184 local maxsize |
2185 if ! f_eval_catch -dk maxsize $funcname "$DIALOG" \ 2186 'LANG= LC_ALL= %s --print-maxsize' "$DIALOG" |
2187 then 2188 # Xdialog(1) failed, fall back to dialog(1) 2189 unset USE_XDIALOG 2190 2191 # Display the error message produced by Xdialog(1) 2192 local height width 2193 f_dialog_buttonbox_size height width \ 2194 "$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$maxsize" --- 48 unchanged lines hidden --- |