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