1if [ ! "$_PACKAGES_PACKAGES_SUBR" ]; then _PACKAGES_PACKAGES_SUBR=1 2# 3# Copyright (c) 2013 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 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 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$ 28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33f_dprintf "%s: loading includes..." "$0" 34f_include $BSDCFG_SHARE/dialog.subr 35f_include $BSDCFG_SHARE/device.subr 36f_include $BSDCFG_SHARE/media/common.subr 37f_include $BSDCFG_SHARE/packages/categories.subr 38f_include $BSDCFG_SHARE/packages/index.subr 39f_include $BSDCFG_SHARE/strings.subr 40 41BSDCFG_LIBE="/usr/libexec/bsdconfig" 42f_include_lang $BSDCFG_LIBE/include/messages.subr 43 44############################################################ CONFIGURATION 45 46# 47# How many packages to display (maximum) per dialog menubox. 48# 49: ${PACKAGE_MENU_PAGESIZE:=2000} 50 51############################################################ GLOBALS 52 53# 54# Package extensions to try 55# 56PACKAGE_EXTENSIONS=".txz .tbz .tbz2 .tgz" 57 58# 59# Variables used to track runtime states 60# 61PACKAGES_DETECTED= # Boolean (NULL/non-NULL); detected installed packages? 62PACKAGE_CATEGORIES= # List of package categories parsed from INDEX 63SELECTED_PACKAGES= # Packages selected by user in [X]dialog(1) interface 64 65# 66# Options 67# 68[ "${SHOW_DESC+set}" ] || SHOW_DESC=1 69 70############################################################ FUNCTIONS 71 72# eval f_package_accent_category_menu $var_to_set $CATEGORY_MENU_LIST 73# 74# Accent the CATEGORY_MENU_LIST produced by f_index_read() (see 75# packages/index.subr). Accented information includes adding an asterisk to the 76# category name if its index has been cached, adding the number of installed 77# packages for each category, and adding the number _selected_ packages for 78# each category. 79# 80# NOTE: The reason `eval' is recommended/shown for the syntax above is because 81# the $CATEGORY_MENU_LIST generated by f_index_read() is meant to be expanded 82# prior to execution (it contains a series of pre-quoted strings which act as 83# the interpolated command arguments). 84# 85f_package_accent_category_menu() 86{ 87 local var_to_set="$1" category cat desc help varcat menu_buf n 88 shift 1 # var_to_set 89 while [ $# -gt 0 ]; do 90 category="${1%\*}" desc="${2%%; *}" help="$3" 91 shift 3 # cat/desc/help 92 93 cat="${category# }" # Trim lead space inserted by sort-method 94 f_str2varname "$cat" varcat 95 96 # Add number of installed packages for this category (if any) 97 n=0 98 case "$cat" in 99 "$msg_all") debug= f_getvar "_All_ninstalled" n ;; 100 *) debug= f_getvar "_${varcat}_ninstalled" n ;; 101 esac && 102 [ $n -ge 1 ] && desc="$desc; $n $msg_installed_lc" 103 104 # Add number of selected packages for this category (if any) 105 n=0 106 case "$cat" in 107 "$msg_all") debug= f_getvar "_All_nselected" n ;; 108 *) debug= f_getvar "_${varcat}_nselected" n ;; 109 esac && 110 [ $n -ge 1 ] && desc="$desc; $n $msg_selected" 111 112 # Re-Add asterisk to the category if its index has been cached 113 f_isset _index_page_${varcat}_1 && category="$category*" 114 115 # Update buffer with modified elements 116 menu_buf="$menu_buf 117 '$category' '$desc' '$help'" # End-Quote 118 done 119 setvar "$var_to_set" "$menu_buf" # return our buffer 120} 121 122# f_package_select $package ... 123# 124# Add $package to the list of tracked/selected packages. If $package is already 125# being tracked (already apears in $SELECTED_PACKAGES), this function amounts 126# to having no effect. 127# 128f_package_select() 129{ 130 local package pkgsel 131 while [ $# -gt 0 ]; do 132 package="$1" 133 shift 1 # package 134 for pkgsel in $SELECTED_PACKAGES; do 135 [ "$package" = "$pkgsel" ] && return $SUCCESS 136 done 137 SELECTED_PACKAGES="$SELECTED_PACKAGES $package" 138 f_dprintf "Added %s to selection list" "$package" 139 done 140 SELECTED_PACKAGES="${SELECTED_PACKAGES# }" # Trim leading space 141} 142 143# f_package_deselect $package ... 144# 145# Remove $package from teh list of tracked/selected packages. If $package is 146# not being tracked (doesn't appear in $SELECTED_PACKAGES), this function 147# amounts to having no effet. 148# 149f_package_deselect() 150{ 151 local package pkgsel 152 while [ $# -gt 1 ]; do 153 local new_list="" 154 package="$1" 155 shift 1 # package 156 for pkgsel in $SELECTED_PACKAGES; do 157 [ "$pkgsel" = "$package" ] && continue 158 new_list="$new_list${new_list:+ }$pkgsel" 159 done 160 SELECTED_PACKAGES="$new_list" 161 f_dprintf "Removed %s from selection list" "$package" 162 done 163} 164 165# f_package_detect_installed 166# 167# Detect installed packages. Currently this uses pkg-query(8) for querying 168# entries and marks each entry as an installed/selected package. 169# 170f_package_detect_installed() 171{ 172 local package varpkg 173 for package in $( pkg query "%n-%v" ); do 174 f_str2varname $package varpkg 175 export _mark_$varpkg=X # exported for awk(1) ENVIRON[] 176 f_package_select $package 177 done 178} 179 180# f_package_calculate_totals 181# 182# Calculate number of installed/selected packages for each category listed in 183# $PACKAGE_CATEGORIES (the number of installed packages for $category is stored 184# as $_${varcat}_ninstalled -- where $varcat is the product of `f_str2varname 185# $category varcat' -- and number selected packages as $_${varcat}_nselected). 186# Also calculates the total number of installed/selected packages stored as 187# $_All_ninstalled and $_All_nselected. 188# 189# Calculations are peformed by checking "marks". A "mark" is stored as 190# $_mark_$varpkg -- where $varpkg is the product of `f_str2varname $package 191# varpkg'. A mark can be "X" for an installed package, `I' for a package that 192# is marked for installation, "R" for a package that is marked for re-install, 193# and "U" for a package that is marked for uninstallation. If a package mark is 194# NULL or a single space (e.g., " "), the package is considered to be NOT 195# selected (and therefore does not increment the counts calculated herein). 196# 197f_package_calculate_totals() 198{ 199 local pkg varpkg mark cat varcat pkgcat n tselected=0 tinstalled=0 200 for cat in $PACKAGE_CATEGORIES; do 201 f_str2varname $cat varcat 202 setvar _${varcat}_ninstalled=0 203 setvar _${varcat}_nselected=0 204 done 205 for pkg in $SELECTED_PACKAGES; do 206 f_str2varname $pkg varpkg 207 mark= 208 f_getvar _mark_$varpkg mark 209 case "$mark" in 210 ""|" ") : ;; 211 X) tinstalled=$(( $tinstalled + 1 )) ;; 212 *) tselected=$(( $tselected + 1 )) 213 esac 214 f_getvar _categories_$varpkg pkgcat 215 for cat in $pkgcat; do 216 f_str2varname $cat varcat 217 case "$mark" in 218 ""|" ") : ;; 219 X) debug= f_getvar _${varcat}_ninstalled n 220 setvar _${varcat}_ninstalled $(( $n + 1 )) ;; 221 *) debug= f_getvar _${varcat}_nselected n 222 setvar _${varcat}_nselected $(( $n + 1 )) 223 esac 224 done 225 done 226 _All_nselected=$tselected 227 _All_ninstalled=$tinstalled 228} 229 230# f_package_calculate_rundeps 231# 232# Update package dependencies by first unmarking all dependencies and then 233# re-marking all dependencies of packages marked for either install ("I") or 234# re-install ("R"). 235# 236f_package_calculate_rundeps() 237{ 238 local pkg varpkg mark rundeps dep vardep 239 240 # 241 # First unmark all the existing run-dependencies 242 # 243 f_dprintf "Unselecting package run-dependencies..." 244 for pkg in $SELECTED_PACKAGES; do 245 f_str2varname $pkg varpkg 246 mark= 247 debug= f_getvar _mark_$varpkg mark 248 # Only unmark if it's marked as a Dependency 249 if [ "$mark" = "D" ]; then 250 f_dprintf "%s unselected" $pkg 251 unset _mark_$varpkg 252 f_package_deselect $pkg 253 fi 254 done 255 256 # 257 # Processes selected packages, adding dependencies 258 # 259 f_dprintf "Re-selecting package run-dependencies..." 260 for pkg in $SELECTED_PACKAGES; do 261 f_str2varname $pkg varpkg 262 mark= 263 debug= f_getvar _mark_$varpkg mark 264 # Skip pkg unless marked for [Re-]Install 265 [ "$mark" = "I" -o "$mark" = "R" ] || continue 266 f_getvar _rundeps_$varpkg rundeps 267 for dep in $rundeps; do 268 f_str2varname $dep vardep 269 mark= 270 debug= f_getvar _mark_$vardep mark 271 # Skip dep if already marked 272 [ "${mark:- }" = " " ] || continue 273 export _mark_$vardep="D" 274 f_package_select $dep 275 done 276 done 277 278 f_dprintf "Finished recalculating dependencies." 279} 280 281# f_package_menu_categories $var_to_set $defaultitem 282# 283# Dislay the menu of package categories, complete with package counts for each 284# category, accents, and other miscellany. If $defaultitem is non-NULL and 285# matches one of the existing menu-items, it will be pre-highlighted in the 286# menu dialog (HINT: Use f_dialog_menutag_fetch() to populate a local variable 287# that is passed as $defaultitem to highlight the user's last selection). 288# 289f_package_menu_categories() 290{ 291 local var_to_get="$1" defaultitem="$2" 292 local prompt="$msg_please_select_a_category_to_display" 293 local menu_list=" 294 '> $msg_review' '$msg_review_desc' '$msg_review_help' 295 " # End-Quote 296 local hline= 297 298 f_package_calculate_rundeps 299 # updates package mark variables and SELECTED_PACKAGES 300 f_package_calculate_totals 301 # creates _{varcat}_ninstalled and _{varcat}_nselected 302 303 local category_list 304 debug= f_getvar "$var_to_get" category_list || return $DIALOG_CANCEL 305 306 # Accent the category menu list with ninstalled/nselected 307 eval f_package_accent_category_menu category_list $category_list 308 309 # Add list of categories to menu list 310 menu_list="$menu_list $category_list" 311 312 local height width rows 313 eval f_dialog_menu_with_help_size height width rows \ 314 \"\$DIALOG_TITLE\" \ 315 \"\$DIALOG_BACKTITLE\" \ 316 \"\$prompt\" \ 317 \"\$hline\" \ 318 $menu_list 319 local menu_choice 320 menu_choice=$( eval $DIALOG \ 321 --title \"\$DIALOG_TITLE\" \ 322 --backtitle \"\$DIALOG_BACKTITLE\" \ 323 --hline \"\$hline\" \ 324 --item-help \ 325 --default-item \"\$defaultitem\" \ 326 --ok-label \"$msg_select\" \ 327 --cancel-label \"$msg_cancel\" \ 328 --menu \"\$prompt\" \ 329 $height $width $rows \ 330 $menu_list \ 331 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 332 ) 333 local retval=$? 334 f_dialog_menutag_store -s "$menu_choice" 335 return $retval 336} 337 338# f_package_index_get_page $category $page [$var_to_set [$var_to_get]] 339# 340# Obtain a [potentially cached] page of the INDEX file for a given $category. 341# If $page is 1 and the cache has not yet been generated, the cache-generating 342# function f_index_extract_pages() (above) is called to generate all pages 343# (not just the requested page) in cache before returning the requested page. 344# If $page is not 1 and there is no cached page, failure status is returned. 345# 346f_package_index_get_page() 347{ 348 local category="$1" page="$2" var_to_set="$3" var_to_get="$4" varcat 349 f_str2varname "$category" varcat 350 if ! debug= f_getvar "_index_page_${varcat}_$page" $var_to_set && 351 [ "$page" = "1" ] 352 then 353 f_show_info "$msg_building_package_menus" 354 local pagesize="$PACKAGE_MENU_PAGESIZE" 355 f_index_extract_pages "${var_to_get:-PACKAGE_INDEX}" \ 356 _index_page_${varcat} "$pagesize" "$category" 357 debug= f_getvar _index_page_${varcat}_$page $var_to_set 358 359 # Update category default-item because now we're cached 360 [ $page -eq 1 ] && 361 category_defaultitem="${category_defaultitem%\*}*" 362 else 363 return $FAILURE 364 fi 365} 366 367# f_package_menu_select $category [$page [$defaultitem]] 368# 369# Display list of packages for $category, optionally $page N and with a default 370# item selected. If $page is omitted, the first page is displayed (but this 371# only matters if there are multiple pages; which is determined by the global 372# maximum $PACKAGE_MENU_PAGESIZE). 373# 374# On success, if the user doesn't press ESC or choose Cancel, use 375# f_dialog_menuitem_fetch() to populate a local variable with the item (not 376# tag) corresponding to the user's selection. The tag portion of the user's 377# selection is available through f_dialog_menutag_fetch(). 378# 379f_package_menu_select() 380{ 381 local category="$1" page="${2:-1}" 382 local prompt= # Calculated below 383 local menu_list # Calculated below 384 local defaultitem="$3" 385 local hline="$hline_arrows_tab_punc_enter" 386 387 f_isinteger "$page" || return $DIALOG_CANCEL 388 389 local varcat 390 f_str2varname "$category" varcat 391 392 # Get number of packages for this category 393 local npkgs=0 394 case "$category" in 395 "$msg_all"|"") npkgs="${_npkgs:-0}" ;; 396 *) f_getvar _npkgs_$varcat npkgs 397 esac 398 399 # Calculate number of pages 400 local npages=$(( ${npkgs:=0} / $PACKAGE_MENU_PAGESIZE )) 401 402 # Add a page to the pagecount if not evenly divisible 403 [ $(( $npages * $PACKAGE_MENU_PAGESIZE )) -lt $npkgs ] && 404 npages=$(( $npages + 1 )) 405 406 # Print some debugging information 407 f_dprintf "f_package_menu_select: category=[%s] npkgs=%u npages=%u" \ 408 "$category" "$npkgs" "$npages" 409 410 local add_prev="" add_next="" 411 local previous_page="$msg_previous_page" next_page="$msg_next_page" 412 if [ $page -gt 1 ]; then 413 add_prev=1 414 # Accent the `Previous Page' item with an asterisk 415 # if the page-before-previous is loaded/cached 416 f_isset _index_page_${varcat}_$(( $page - 1 )) && 417 previous_page="$previous_page*" 418 fi 419 if [ $page -lt $npages ]; then 420 add_next=1 421 # Accent the `Next Page' item with an asterisk 422 # if the page-after-next is loaded/cached 423 f_isset _index_page_${varcat}_$(( $page + 1 )) && 424 next_page="$next_page*" 425 fi 426 427 local index_page 428 f_package_index_get_page "$category" $page index_page 429 430 menu_list=" 431 ${add_prev:+'> $previous_page' '' ${SHOW_DESC:+''}} 432 ${add_next:+'> $next_page' '' ${SHOW_DESC:+''}} 433 $( 434 export SHOW_DESC 435 export VALID_VARNAME_CHARS 436 echo "$index_page" | awk -F'|' -v view="port" ' 437 BEGIN { 438 valid_chars = ENVIRON["VALID_VARNAME_CHARS"] 439 prefix = "" 440 } 441 { 442 cur_prefix = tolower(substr($1, 1, 1)) 443 printf "'\''" 444 if ( prefix != cur_prefix ) 445 prefix = cur_prefix 446 else 447 printf " " 448 package = $1 449 if ( view == "port" ) 450 desc = $2 451 varpkg = package 452 gsub("[^" valid_chars "]", "_", varpkg) 453 mark = ENVIRON["_mark_" varpkg] 454 if ( ! mark ) mark = " " 455 printf "%s'\'' '\''[%c] %s'\''", 456 package, mark, desc 457 if ( ENVIRON["SHOW_DESC"] ) { 458 help = $4 459 gsub(/'\''/, "'\''\\'\'\''", help) 460 printf " '\''%s'\''", help 461 } 462 printf "\n" 463 }' 464 ) 465 ${add_prev:+'> $previous_page' '' ${SHOW_DESC:+''}} 466 ${add_next:+'> $next_page' '' ${SHOW_DESC:+''}} 467 " # End-Quote 468 469 # Accept/Translate i18n "All" but other category names must 470 # match tree definitions from INDEX, ports, FTP, etc. 471 case "$category" in 472 "$msg_all"|"") f_category_desc_get "All" prompt ;; 473 *) f_category_desc_get "$category" prompt ;; 474 esac 475 f_sprintf prompt "%s $msg_page_of_npages" "$prompt" "$page" "$npages" 476 477 local mheight mwidth mrows 478 eval f_dialog_menu${SHOW_DESC:+_with_help}_size mheight mwidth mrows \ 479 \"\$DIALOG_TITLE\" \"\$DIALOG_BACKTITLE\" \ 480 \"\$prompt\" \"\$hline\" $menu_list 481 local iheight iwidth 482 f_dialog_infobox_size iheight iwidth \ 483 "$DIALOG_TITLE" "$DIALOG_BACKTITLE" \ 484 "$msg_processing_selection" 485 486 local menu_choice 487 menu_choice=$( eval $DIALOG \ 488 --title \"\$DIALOG_TITLE\" \ 489 --backtitle \"\$DIALOG_BACKTITLE\" \ 490 --hline \"\$hline\" \ 491 --keep-tite \ 492 --ok-label \"$msg_select\" \ 493 --cancel-label \"$msg_back\" \ 494 ${SHOW_DESC:+--item-help} \ 495 --default-item \"\$defaultitem\" \ 496 --menu \"\$prompt\" \ 497 $mheight $mwidth $mrows \ 498 $menu_list \ 499 --and-widget \ 500 ${USE_XDIALOG:+--no-buttons} \ 501 --infobox \"\$msg_processing_selection\" \ 502 $iheight $iwidth \ 503 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 504 ) 505 local retval=$? 506 f_dialog_data_sanitize menu_choice 507 f_dialog_menutag_store "$menu_choice" 508 509 if [ $retval -eq $DIALOG_OK ]; then 510 local item 511 item=$( eval f_dialog_menutag2item${SHOW_DESC:+_with_help} \ 512 \"\$menu_choice\" $menu_list ) 513 f_dialog_menuitem_store "$item" 514 fi 515 516 return $retval 517} 518 519# f_package_menu_deselect $package 520# 521# Display a menu, asking the user what they would like to do with $package 522# with regard to "deselecting" an already installed package. Choices include 523# uninstall, re-install, or cancel (leave $package marked as installed). 524# Returns success if the user does not press ESC or choose Cnacel. Use the 525# f_dialog_menutag_fetch() function upon success to retrieve the user's choice. 526# 527f_package_menu_deselect() 528{ 529 local package="$1" 530 local prompt # Calculated below 531 local menu_list=" 532 'X $msg_installed' '$msg_installed_desc' 533 'R $msg_reinstall' '$msg_reinstall_desc' 534 'U $msg_uninstall' '$msg_uninstall_desc' 535 " # End-Quote 536 local hline="$hline_alnum_arrows_punc_tab_enter" 537 538 f_sprintf prompt "$msg_what_would_you_like_to_do_with" "$package" 539 540 local height width rows 541 eval f_dialog_menu_size height width rows \ 542 \"\$DIALOG_TITLE\" \ 543 \"\$DIALOG_BACKTITLE\" \ 544 \"\$prompt\" \ 545 \"\$hline\" \ 546 $menu_list 547 local menu_choice 548 menu_choice=$( eval $DIALOG \ 549 --title \"\$DIALOG_TITLE\" \ 550 --backtitle \"\$DIALOG_BACKTITLE\" \ 551 --hline \"\$hline\" \ 552 --ok-label \"$msg_select\" \ 553 --cancel-label \"$msg_cancel\" \ 554 --menu \"\$prompt\" \ 555 $height $width $rows \ 556 $menu_list \ 557 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 558 ) 559 local retval=$? 560 f_dialog_menutag_store -s "$menu_choice" 561 return $retval 562} 563 564# f_package_review 565# 566# Display a review screen, showing selected packages and what they are marked 567# for, before proceeding (if the user does not press ESC or choose Cancel) to 568# operate on each selection. Returns error if no packages have been selected, 569# or the user has pressed ESC, or if they have chosen Cancel. 570# 571f_package_review() 572{ 573 local funcname=f_package_review 574 local prompt # Calculated below 575 local menu_list # Calculated below 576 local hline="$hline_alnum_arrows_punc_tab_enter" 577 578 f_dprintf "$funcname: SELECTED_PACKAGES=[%s]" "$SELECTED_PACKAGES" 579 580 f_sprintf prompt "$msg_reviewing_selected_packages" "$_All_nselected" 581 582 local package varpkg mark 583 for package in $SELECTED_PACKAGES; do 584 mark= 585 f_str2varname "$package" varpkg 586 f_getvar _mark_$varpkg mark 587 [ "$mark" -a ! "${mark#[IRUD]}" ] || continue 588 menu_list="$menu_list 589 '$mark' '$package' 590 " # End-Quote 591 done 592 if [ ! "$menu_list" ]; then 593 f_show_msg "$msg_no_packages_were_selected_for_extraction" 594 return $DIALOG_CANCEL # Might have selected this by accident 595 fi 596 menu_list=$( echo "$menu_list" | sort ) 597 598 local height width rows 599 eval f_dialog_menu_size height width rows \ 600 \"\$DIALOG_TITLE\" \ 601 \"\$DIALOG_BACKTITLE\" \ 602 \"\$prompt\" \ 603 \"\$hline\" \ 604 $menu_list 605 606 # Show the review menu (ignore menu choice) 607 eval $DIALOG \ 608 --title \"\$DIALOG_TITLE\" \ 609 --backtitle \"\$DIALOG_BACKTITLE\" \ 610 --hline \"\$hline\" \ 611 --ok-label \"\$msg_proceed\" \ 612 --cancel-label \"\$msg_cancel\" \ 613 --menu \"\$prompt\" \ 614 $height $width $rows \ 615 $menu_list \ 616 2> /dev/null || return $? 617 # Return if the user pressed ESC or chose Cancel/No 618 619 # 620 # Process each of the selected packages: 621 # + First, process packages marked for Install. 622 # + Second, process packages marked for Re-install. 623 # + Finally, process packages marked for Uninstall. 624 # 625 for package in $SELECTED_PACKAGES; do 626 mark= 627 f_str2varname "$package" varpkg 628 debug= f_getvar _mark_$varpkg mark 629 [ "$mark" = "I" ] || continue 630 f_dprintf "$funcname: Installing %s package" "$package" 631 f_package_add "$package" 632 done 633 for package in $SELECTED_PACKAGES; do 634 mark= 635 f_str2varname "$package" varpkg 636 debug= f_getvar _mark_$varpkg mark 637 [ "$mark" = "R" ] || continue 638 f_dprintf "$funcname: Reinstalling %s package" "$package" 639 f_package_reinstall "$package" 640 done 641 for package in $SELECTED_PACKAGES; do 642 mark= 643 f_str2varname "$package" varpkg 644 debug= f_getvar _mark_$varpkg mark 645 [ "$mark" = "U" ] || continue 646 f_dprintf "$funcname: Uninstalling %s package" "$package" 647 f_package_delete "$package" || continue 648 f_package_deselect "$package" 649 done 650 651 return $DIALOG_OK 652} 653 654# f_package_config 655# 656# Allow the user to configure packages and install them. Initially, a list of 657# package categories is loaded/displayed. When the user selects a category, 658# the menus for that category are built (unlike sysinstall which built all 659# category menus up-front -- which also took forever, despite the fact that 660# few people visit more than a couple of categories each time). 661# 662f_package_config() 663{ 664 # Did we get an INDEX? 665 f_index_initialize packages/INDEX || return $FAILURE 666 # Creates following variables (indirectly via f_index_read()) 667 # CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg} 668 # PACKAGE_CATEGORIES _npkgs 669 670 # Detect installed packages (updates marks/SELECTED_PACKAGES) 671 f_package_detect_installed 672 export PACKAGES_DETECTED=1 # exported for awk(1) ENVIRON[] 673 674 local retval category varcat defaultitem category_defaultitem="" 675 while :; do 676 # Display the list of package categories 677 f_package_menu_categories \ 678 CATEGORY_MENU_LIST "$category_defaultitem" 679 retval=$? 680 f_dialog_menutag_fetch category 681 f_dprintf "retval=%u mtag=[%s]" $retval "$category" 682 category_defaultitem="$category" 683 684 [ $retval -eq $DIALOG_OK ] || break 685 686 # Maybe the user chose an action (like `Review') 687 case "$category" in 688 "> $msg_review") 689 f_package_review && break 690 continue ;; 691 "> "*) 692 continue 693 esac 694 695 # Anything else is a package category 696 697 category=${category# } # Trim leading space if present 698 category=${category%\*} # Trim trailing asterisk if present 699 700 f_str2varname "$category" varcat 701 702 local page package varpkg mark menu_choice 703 while :; do 704 # Display the list of packages for selected category 705 page=1 defaultitem="" 706 f_getvar _defaultitem_$varcat defaultitem 707 f_getvar _defaultpage_$varcat page 708 f_package_menu_select \ 709 "$category" "${page:=1}" "$defaultitem" 710 retval=$? 711 f_dialog_menutag_fetch menu_choice 712 f_dprintf "retval=%u mtag=[%s]" $retval "$menu_choice" 713 714 # NOTE: When --and-widget is used only ESC will cause 715 # dialog(1) to return without going to the next widget. 716 # This is alright in our case as we can still detect 717 # the Cancel button because stdout will be NULL. 718 # Alternatively, Xdialog(1) will terminate with 1 719 # if/when Cancel is chosen on any widget. 720 if [ $retval -eq $DIALOG_ESC -o ! "$menu_choice" ] 721 then 722 break 723 elif [ $retval -eq $DIALOG_CANCEL ]; then 724 # Using X11, Xdialog(1) returned 1 for Cancel 725 f_show_msg "%s" "$menu_choice" 726 break 727 elif [ $retval -ne $DIALOG_OK ]; then 728 # X11-related error occurred using Xdialog(1) 729 f_show_msg "%s" "$menu_choice" 730 break 731 fi 732 733 defaultitem="$menu_choice" 734 735 # NOTE: f_package_menu_select() does not show the 736 # `Previous Page' or `Next Page' items unless needed 737 case "$menu_choice" in 738 "> $msg_previous_page"|"> $msg_previous_page*") 739 page=$(( $page - 1 )) 740 setvar _defaultpage_$varcat $page 741 # Update default-item to match accent that will 742 # be applied by f_package_menu_select(); if the 743 # page-before-prev is cached, add an asterisk. 744 if f_isset \ 745 _index_page_${varcat}_$(( $page - 1 )) 746 then 747 defaultitem="${defaultitem%\*}*" 748 else 749 defaultitem="${defaultitem%\*}" 750 fi 751 setvar _defaultitem_$varcat "$defaultitem" 752 continue ;; 753 "> $msg_next_page"|"> $msg_next_page*") 754 page=$(( $page + 1 )) 755 setvar _defaultpage_$varcat $page 756 # Update default-item to match accent that will 757 # be applied by f_package_menu_select(); if the 758 # page-after-next is cached, add an asterisk. 759 if f_isset \ 760 _index_page_${varcat}_$(( $page + 1 )) 761 then 762 defaultitem="${defaultitem%\*}*" 763 else 764 defaultitem="${defaultitem%\*}" 765 fi 766 setvar _defaultitem_$varcat "$defaultitem" 767 continue ;; 768 "> "*) # Unknown navigation/action item 769 setvar _defaultpage_$varcat $page 770 continue ;; # Do not treat as a package 771 *) 772 setvar _defaultitem_$varcat "$defaultitem" 773 esac 774 775 # Treat any other selection as a package 776 package="${menu_choice# }" # Trim leading space 777 f_str2varname $package varpkg 778 f_dialog_menuitem_fetch mark 779 mark="${mark#?}" 780 mark="${mark%%\] *}" 781 case "$mark" in 782 "I") 783 mark=" " 784 f_package_deselect $package 785 ;; 786 " "|"D") 787 mark="I" 788 f_package_select $package 789 ;; 790 "X"|"R"|"U") 791 f_package_menu_deselect $package || continue 792 f_dialog_menutag_fetch menu_choice 793 case "$menu_choice" in 794 "X $msg_installed") 795 f_package_deselect "$package" 796 mark="X" 797 ;; 798 "R $msg_reinstall") 799 f_package_select "$package" 800 mark="R" 801 ;; 802 "U $msg_uninstall") 803 f_package_select "$package" 804 mark="U" 805 ;; 806 esac 807 ;; 808 esac 809 export _mark_$varpkg="$mark" 810 # NOTE: exported for awk(1) ENVIRON[] 811 done 812 done 813} 814 815# f_package_add $package_name [$depended] 816# 817# Like f_package_extract(), but assumes current media device and chases deps. 818# Note that $package_name should not contain the archive suffix (e.g., `.tbz'). 819# If $depended is present and non-NULL, the package is treated as a dependency 820# (in this function, dependencies are not handled any differently, but the 821# f_package_extract() function is passed this value and it displays a different 822# message when installing a dependency versus non-dependency). 823# 824f_package_add() 825{ 826 local name="$1" depended="$2" status=$SUCCESS retval 827 828 local alert=f_show_msg no_confirm= 829 f_getvar $VAR_NO_CONFIRM no_confirm 830 [ "$no_confirm" ] && alert=f_show_info 831 832 if ! { [ "$name" ] || { f_getvar $VAR_PACKAGE name && [ "$name" ]; }; } 833 then 834 f_dprintf "packageAdd: %s" \ 835 "$msg_no_package_name_passed_in_package_variable" 836 return $FAILURE 837 fi 838 839 { # Verify and initialize device media if-defined 840 f_media_verify && 841 f_device_init device_media && 842 f_index_initialize packages/INDEX 843 } || return $FAILURE 844 845 # Now we have (indirectly via f_index_read()): 846 # CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg} 847 # PACKAGE_CATEGORIES _npkgs 848 849 local varpkg 850 f_str2varname "$name" varpkg 851 852 # Just as-in the user-interface (opposed to scripted-use), only allow 853 # packages with at least one category to be recognized. 854 # 855 local pkgcat= 856 if ! f_getvar _categories_$varpkg pkgcat || [ ! "$pkgcat" ]; then 857 # $pkg may be a partial name, search the index (this is slow) 858 f_index_search PACKAGE_INDEX $name name 859 if [ ! "$name" ]; then 860 f_show_msg \ 861 "$msg_sorry_package_was_not_found_in_the_index" \ 862 "$name" 863 return $FAILURE 864 fi 865 f_str2varname "$name" varpkg 866 fi 867 868 # If invoked through the scripted interface, we likely have not yet 869 # detected the installed packages -- something we should do only once. 870 # 871 if [ ! "$PACKAGES_DETECTED" ]; then 872 f_dprintf "f_package_add: Detecting installed packages" 873 f_package_detect_installed 874 export PACKAGES_DETECTED=1 # exported for awk(1) ENVIRON[] 875 fi 876 # Now we have: _mark_{varpkg}=X for all installed packages 877 878 # 879 # Since we're maintaining data structures for installed packages, 880 # short-circuit the package dependency checks if the package is already 881 # installed. This prevents wasted cycles, minor delays between package 882 # extractions, and worst-case an infinite loop with a certain faulty 883 # INDEX file. 884 # 885 local mark= 886 f_getvar _mark_$varpkg mark && [ "$mark" = "X" ] && return $SUCCESS 887 888 local dep vardep rundeps= 889 f_getvar _rundeps_$varpkg rundeps 890 for dep in $rundeps; do 891 f_str2varname "$dep" vardep 892 893 # Skip dependency if already installed 894 mark= 895 f_getvar _mark_$vardep mark && [ "$mark" = "X" ] && continue 896 897 # Just as-in the user-interface (opposed to scripted-use), only 898 # allow packages with at least one category to be recognized. 899 # 900 local depcat= 901 if ! f_getvar _categories_$vardep depcat || [ ! "$depcat" ] 902 then 903 $alert "$msg_required_package_not_found" "$dep" 904 [ "$no_confirm" ] && sleep 2 905 fi 906 907 f_package_add "$dep" 908 retval=$? 909 if [ $retval -ne $SUCCESS ]; then 910 status=$(( $status | $retval )) 911 912 # XXX package could be on a future disc volume 913 # XXX (not supporting multiple disc volumes yet) 914 915 $alert "$msg_loading_of_dependent_package_failed" \ 916 "$dep" 917 [ "$no_confirm" ] && sleep 2 918 fi 919 done 920 [ $status -eq $SUCCESS ] || return $status 921 922 # 923 # Done with the deps? Try to load the real m'coy. 924 # 925 926 f_package_extract device_media "$name" "$depended" 927 retval=$? 928 if [ $retval -ne $SUCCESS ]; then 929 status=$(( $status | $retval )) 930 else 931 setvar _mark_$varpkg X 932 fi 933 934 return $status 935} 936 937# f_package_extract $device $name [$depended] 938# 939# Extract a package based on a namespec and media device. If $depended is 940# present and non-NULL, the notification displayed while installing the package 941# has "as a dependency" appended. 942# 943f_package_extract() 944{ 945 local funcname=f_package_extract 946 local device="$1" name="$2" depended="$3" 947 local devname= 948 949 $device get name devname 950 f_dprintf "$funcname: device=[%s] name=[%s] depended=[%s]" \ 951 "$devname" "$name" "$depended" 952 953 # Check to make sure it's not already there 954 local varpkg mark= 955 f_str2varname "$name" varpkg 956 f_getvar _mark_$varpkg mark 957 [ "$mark" = "X" ] && return $SUCCESS 958 959 if ! f_device_init $device; then 960 f_show_msg \ 961 "$msg_unable_to_initialize_media_type_for_package_extract" 962 return $FAILURE 963 fi 964 965 # If necessary, initialize the ldconfig hints 966 [ -f "/var/run/ld-elf.so.hints" ] || 967 f_quietly ldconfig /usr/lib /usr/lib/compat /usr/local/lib 968 969 # Make a couple paranoid locations for temp 970 # files to live if user specified none 971 local tmpdir 972 f_getvar $VAR_PKG_TMPDIR:-/var/tmp tmpdir 973 f_quietly mkdir -p -m 1777 "$tmpdir" 974 975 local path device_type 976 $device get type device_type 977 case "$name" in 978 */*) path="$name" ;; 979 *) 980 if [ "$device_type" = "$DEVICE_TYPE_HTTP" ]; then 981 path="$PKG_ABI/latest/All/$name" 982 else 983 path="packages/$PKG_ABI/All/$name" 984 fi 985 esac 986 987 # We have a path, call the device strategy routine to check the file 988 local pkg_ext found= 989 for pkg_ext in "" $PACKAGE_EXTENSIONS; do 990 if f_device_get $device "$path$pkg_ext" $PROBE_EXIST; then 991 path="$path$pkg_ext" 992 found=1 993 break 994 elif [ "$device_type" = "$DEVICE_TYPE_HTTP" ] && 995 f_device_get $device \ 996 "packages/$PKG_ABI/All/$name$pkg_ext" $PROBE_EXIST 997 then 998 # Mirroring physical media over HTTP 999 path="packages/$PKG_ABI/All/$name$pkg_ext" 1000 found=1 1001 break 1002 fi 1003 done 1004 [ "$found" ] && f_dprintf "$funcname: found path=[%s] dev=[%s]" \ 1005 "$path" "$devname" 1006 1007 local alert=f_show_msg no_confirm= 1008 f_getvar $VAR_NO_CONFIRM no_confirm 1009 [ "$no_confirm" ] && alert=f_show_info 1010 1011 if [ ! "$found" ]; then 1012 f_dprintf "$funcname: No such %s file on %s device" \ 1013 "$path" "$devname" 1014 $alert "$msg_unable_to_fetch_package_from_selected_media" \ 1015 "$name" 1016 [ "$no_confirm" ] && sleep 2 1017 return $FAILURE 1018 fi 1019 1020 if [ "$depended" ]; then 1021 f_show_info "$msg_adding_package_as_a_dependency_from_media" \ 1022 "$name" "$devname" 1023 else 1024 f_show_info "$msg_adding_package_from_media" "$name" "$devname" 1025 fi 1026 1027 # Request the package be added via pkg-install(8) 1028 if f_debugging; then 1029 f_eval_catch $funcname pkg 'pkg -d install -y "%s"' "$name" 1030 else 1031 f_eval_catch $funcname pkg 'pkg install -y "%s"' "$name" 1032 fi 1033 if [ $? -ne $SUCCESS ]; then 1034 $alert "$msg_pkg_install_apparently_did_not_like_the_package" \ 1035 "$name" 1036 [ "$no_confirm" ] && sleep 2 1037 else 1038 f_show_info "$msg_package_was_added_successfully" "$name" 1039 sleep 1 1040 fi 1041 1042 return $SUCCESS 1043} 1044 1045# f_package_delete $name 1046# 1047# Delete package by full $name (lacks archive suffix; e.g., `.tbz'). 1048# 1049f_package_delete() 1050{ 1051 local funcname=f_package_delete 1052 local name="$1" 1053 1054 if ! { [ "$name" ] || { f_getvar $VAR_PACKAGE name && [ "$name" ]; }; } 1055 then 1056 f_dprintf "packageDelete: %s" \ 1057 "$msg_no_package_name_passed_in_package_variable" 1058 return $FAILURE 1059 fi 1060 1061 f_dprintf "$funcname: name=[%s]" "$name" 1062 1063 [ "$name" ] || return $FAILURE 1064 1065 { # Verify and initialize device media if-defined 1066 f_media_verify && 1067 f_device_init device_media && 1068 f_index_initialize packages/INDEX 1069 } || return $FAILURE 1070 1071 # Now we have (indirectly via f_index_read()): 1072 # CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg} 1073 # PACKAGE_CATEGORIES _npkgs 1074 1075 local varpkg 1076 f_str2varname "$name" varpkg 1077 1078 # Just as-in the user-interface (opposed to scripted-use), only allow 1079 # packages with at least one category to be recognized. 1080 # 1081 local pkgcat= 1082 if ! f_getvar _categories_$varpkg pkgcat || [ ! "$pkgcat" ]; then 1083 # $pkg may be a partial name, search the index (this is slow) 1084 f_index_search PACKAGE_INDEX "$name" name 1085 if [ ! "$name" ]; then 1086 f_show_msg \ 1087 "$msg_sorry_package_was_not_found_in_the_index" \ 1088 "$name" 1089 return $FAILURE 1090 fi 1091 f_str2varname "$name" varpkg 1092 fi 1093 1094 # If invoked through the scripted interface, we likely have not yet 1095 # detected the installed packages -- something we should do only once. 1096 # 1097 if [ ! "$PACKAGES_DETECTED" ]; then 1098 f_dprintf "$funcname: Detecting installed packages" 1099 f_package_detect_installed 1100 export PACKAGES_DETECTED=1 # exported for awk(1) ENVIRON[] 1101 fi 1102 # Now we have: _mark_{varpkg}=X for all installed packages 1103 1104 # 1105 # Return failure if the package is not already installed. 1106 # 1107 local pkgmark= 1108 f_getvar _mark_$varpkg pkgmark 1109 if ! [ "$pkgmark" -a ! "${pkgmark#[XUR]}" ]; then 1110 f_show_msg "$msg_package_not_installed_cannot_delete" "$name" 1111 return $FAILURE 1112 fi 1113 1114 # 1115 # Check for dependencies 1116 # 1117 local pkgsel depc=0 udeps= 1118 for pkgsel in $SELECTED_PACKAGES; do 1119 local mark= 1120 f_str2varname $pkgsel varpkg 1121 debug= f_getvar _mark_$varpkg mark 1122 [ "$mark" -a ! "${mark#[XUR]}" ] || continue 1123 local dep rundeps= 1124 debug= f_getvar _rundeps_$varpkg rundeps 1125 for dep in $rundeps; do 1126 if [ "$dep" = "$name" ]; then 1127 # Maybe this package is marked for deletion too 1128 if [ "$mark" = "U" ]; then 1129 udeps="$udeps $pkgsel" 1130 else 1131 depc=$(( $depc + 1 )) 1132 fi 1133 break 1134 fi 1135 done 1136 done 1137 if [ $depc -gt 0 ]; then 1138 local grammatical_s= 1139 [ $depc -gt 1 ] && grammatical_s=s 1140 f_show_msg \ 1141 "$msg_package_is_needed_by_other_installed_packages" \ 1142 "$name" "$depc" "$grammatical_s" 1143 return $FAILURE 1144 fi 1145 1146 # 1147 # Chase dependencies that are marked for uninstallation 1148 # 1149 for pkgsel in $udeps; do 1150 f_dprintf "$funcname: Uninstalling dependency %s (%s)" \ 1151 "$pkgsel" "marked for delete" 1152 f_package_delete "$pkgsel" 1153 done 1154 1155 # 1156 # OK to perform the delete (no other packages depend on it)... 1157 # 1158 f_show_info "$msg_uninstalling_package_waiting_for_pkg_delete" "$name" 1159 if f_debugging; then 1160 f_eval_catch $funcname pkg 'pkg -d delete -y "%s"' "$name" 1161 else 1162 f_eval_catch $funcname pkg 'pkg delete -y "%s"' "$name" 1163 fi 1164 if [ $? -ne $SUCCESS ]; then 1165 f_show_msg "$msg_pkg_delete_failed" "$name" 1166 return $FAILURE 1167 else 1168 f_dprintf "$funcname: pkg-delete(8) of %s successful" "$name" 1169 f_str2varname "$name" varpkg 1170 setvar _mark_$varpkg "" 1171 fi 1172} 1173 1174# f_package_reinstall $name 1175# 1176# A simple wrapper to f_package_delete() + f_package_add() 1177# 1178f_package_reinstall() 1179{ 1180 f_package_delete "$1" && f_package_add "$1" 1181} 1182 1183############################################################ MAIN 1184 1185f_dprintf "%s: Successfully loaded." packages/packages.subr 1186 1187fi # ! $_PACKAGES_PACKAGES_SUBR 1188