1 if [ ! "$_PACKAGES_PACKAGES_SUBR" ]; then _PACKAGES_PACKAGES_SUBR=1
3 # Copyright (c) 2013 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." "$0"
34 f_include $BSDCFG_SHARE/dialog.subr
35 f_include $BSDCFG_SHARE/device.subr
36 f_include $BSDCFG_SHARE/media/common.subr
37 f_include $BSDCFG_SHARE/packages/categories.subr
38 f_include $BSDCFG_SHARE/packages/index.subr
39 f_include $BSDCFG_SHARE/strings.subr
41 BSDCFG_LIBE="/usr/libexec/bsdconfig"
42 f_include_lang $BSDCFG_LIBE/include/messages.subr
44 ############################################################ CONFIGURATION
47 # How many packages to display (maximum) per dialog menubox.
49 : ${PACKAGE_MENU_PAGESIZE:=2000}
51 ############################################################ GLOBALS
54 # Package extensions to try
56 PACKAGE_EXTENSIONS=".txz .tbz .tbz2 .tgz"
59 # Variables used to track runtime states
61 PACKAGES_DETECTED= # Boolean (NULL/non-NULL); detected installed packages?
62 PACKAGE_CATEGORIES= # List of package categories parsed from INDEX
63 SELECTED_PACKAGES= # Packages selected by user in [X]dialog(1) interface
68 [ "${SHOW_DESC+set}" ] || SHOW_DESC=1
70 ############################################################ FUNCTIONS
72 # eval f_package_accent_category_menu $var_to_set $CATEGORY_MENU_LIST
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
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).
85 f_package_accent_category_menu()
87 local var_to_set="$1" category cat desc help varcat menu_buf n
89 while [ $# -gt 0 ]; do
90 category="${1%\*}" desc="${2%%; *}" help="$3"
91 shift 3 # cat/desc/help
93 cat="${category# }" # Trim lead space inserted by sort-method
94 f_str2varname "$cat" varcat
96 # Add number of installed packages for this category (if any)
99 "$msg_all") debug= f_getvar "_All_ninstalled" n ;;
100 *) debug= f_getvar "_${varcat}_ninstalled" n ;;
102 [ $n -ge 1 ] && desc="$desc; $n $msg_installed_lc"
104 # Add number of selected packages for this category (if any)
107 "$msg_all") debug= f_getvar "_All_nselected" n ;;
108 *) debug= f_getvar "_${varcat}_nselected" n ;;
110 [ $n -ge 1 ] && desc="$desc; $n $msg_selected"
112 # Re-Add asterisk to the category if its index has been cached
113 f_isset _index_page_${varcat}_1 && category="$category*"
115 # Update buffer with modified elements
117 '$category' '$desc' '$help'" # End-Quote
119 setvar "$var_to_set" "$menu_buf" # return our buffer
122 # f_package_select $package ...
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.
131 while [ $# -gt 0 ]; do
134 for pkgsel in $SELECTED_PACKAGES; do
135 [ "$package" = "$pkgsel" ] && return $SUCCESS
137 SELECTED_PACKAGES="$SELECTED_PACKAGES $package"
138 f_dprintf "Added %s to selection list" "$package"
140 SELECTED_PACKAGES="${SELECTED_PACKAGES# }" # Trim leading space
143 # f_package_deselect $package ...
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.
152 while [ $# -gt 1 ]; do
156 for pkgsel in $SELECTED_PACKAGES; do
157 [ "$pkgsel" = "$package" ] && continue
158 new_list="$new_list${new_list:+ }$pkgsel"
160 SELECTED_PACKAGES="$new_list"
161 f_dprintf "Removed %s from selection list" "$package"
165 # f_package_detect_installed
167 # Detect installed packages. Currently this uses pkg-query(8) for querying
168 # entries and marks each entry as an installed/selected package.
170 f_package_detect_installed()
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
180 # f_package_calculate_totals
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.
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).
197 f_package_calculate_totals()
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
205 for pkg in $SELECTED_PACKAGES; do
206 f_str2varname $pkg varpkg
208 f_getvar _mark_$varpkg mark
211 X) tinstalled=$(( $tinstalled + 1 )) ;;
212 *) tselected=$(( $tselected + 1 ))
214 f_getvar _categories_$varpkg pkgcat
215 for cat in $pkgcat; do
216 f_str2varname $cat varcat
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 ))
226 _All_nselected=$tselected
227 _All_ninstalled=$tinstalled
230 # f_package_calculate_rundeps
232 # Update package dependencies by first unmarking all dependencies and then
233 # re-marking all dependencies of packages marked for either install ("I") or
236 f_package_calculate_rundeps()
238 local pkg varpkg mark rundeps dep vardep
241 # First unmark all the existing run-dependencies
243 f_dprintf "Unselecting package run-dependencies..."
244 for pkg in $SELECTED_PACKAGES; do
245 f_str2varname $pkg varpkg
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
252 f_package_deselect $pkg
257 # Processes selected packages, adding dependencies
259 f_dprintf "Re-selecting package run-dependencies..."
260 for pkg in $SELECTED_PACKAGES; do
261 f_str2varname $pkg varpkg
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
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
278 f_dprintf "Finished recalculating dependencies."
281 # f_package_menu_categories $var_to_set $defaultitem
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).
289 f_package_menu_categories()
291 local var_to_get="$1" defaultitem="$2"
292 local prompt="$msg_please_select_a_category_to_display"
294 '> $msg_review' '$msg_review_desc' '$msg_review_help'
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
304 debug= f_getvar "$var_to_get" category_list || return $DIALOG_CANCEL
306 # Accent the category menu list with ninstalled/nselected
307 eval f_package_accent_category_menu category_list $category_list
309 # Add list of categories to menu list
310 menu_list="$menu_list $category_list"
312 local height width rows
313 eval f_dialog_menu_with_help_size height width rows \
315 \"\$DIALOG_BACKTITLE\" \
320 menu_choice=$( eval $DIALOG \
321 --title \"\$DIALOG_TITLE\" \
322 --backtitle \"\$DIALOG_BACKTITLE\" \
323 --hline \"\$hline\" \
325 --default-item \"\$defaultitem\" \
326 --ok-label \"$msg_select\" \
327 --cancel-label \"$msg_cancel\" \
328 --menu \"\$prompt\" \
329 $height $width $rows \
331 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
334 f_dialog_menutag_store -s "$menu_choice"
338 # f_package_index_get_page $category $page [$var_to_set [$var_to_get]]
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.
346 f_package_index_get_page()
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 &&
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
359 # Update category default-item because now we're cached
361 category_defaultitem="${category_defaultitem%\*}*"
367 # f_package_menu_select $category [$page [$defaultitem]]
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).
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().
379 f_package_menu_select()
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"
387 f_isinteger "$page" || return $DIALOG_CANCEL
390 f_str2varname "$category" varcat
392 # Get number of packages for this category
395 "$msg_all"|"") npkgs="${_npkgs:-0}" ;;
396 *) f_getvar _npkgs_$varcat npkgs
399 # Calculate number of pages
400 local npages=$(( ${npkgs:=0} / $PACKAGE_MENU_PAGESIZE ))
402 # Add a page to the pagecount if not evenly divisible
403 [ $(( $npages * $PACKAGE_MENU_PAGESIZE )) -lt $npkgs ] &&
404 npages=$(( $npages + 1 ))
406 # Print some debugging information
407 f_dprintf "f_package_menu_select: category=[%s] npkgs=%u npages=%u" \
408 "$category" "$npkgs" "$npages"
410 local add_prev="" add_next=""
411 local previous_page="$msg_previous_page" next_page="$msg_next_page"
412 if [ $page -gt 1 ]; then
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*"
419 if [ $page -lt $npages ]; then
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*"
428 f_package_index_get_page "$category" $page index_page
431 ${add_prev:+'> $previous_page' '' ${SHOW_DESC:+''}}
432 ${add_next:+'> $next_page' '' ${SHOW_DESC:+''}}
435 export VALID_VARNAME_CHARS
436 echo "$index_page" | awk -F'|' -v view="port" '
438 valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
442 cur_prefix = tolower(substr($1, 1, 1))
444 if ( prefix != cur_prefix )
449 if ( view == "port" )
452 gsub("[^" valid_chars "]", "_", varpkg)
453 mark = ENVIRON["_mark_" varpkg]
454 if ( ! mark ) mark = " "
455 printf "%s'\'' '\''[%c] %s'\''",
457 if ( ENVIRON["SHOW_DESC"] ) {
459 gsub(/'\''/, "'\''\\'\'\''", help)
460 printf " '\''%s'\''", help
465 ${add_prev:+'> $previous_page' '' ${SHOW_DESC:+''}}
466 ${add_next:+'> $next_page' '' ${SHOW_DESC:+''}}
469 # Accept/Translate i18n "All" but other category names must
470 # match tree definitions from INDEX, ports, FTP, etc.
472 "$msg_all"|"") f_category_desc_get "All" prompt ;;
473 *) f_category_desc_get "$category" prompt ;;
475 prompt="$prompt $( printf "$msg_page_of_npages" \
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
483 f_dialog_infobox_size iheight iwidth \
484 "$DIALOG_TITLE" "$DIALOG_BACKTITLE" \
485 "$msg_processing_selection"
488 menu_choice=$( eval $DIALOG \
489 --title \"\$DIALOG_TITLE\" \
490 --backtitle \"\$DIALOG_BACKTITLE\" \
491 --hline \"\$hline\" \
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 \
501 ${USE_XDIALOG:+--no-buttons} \
502 --infobox \"\$msg_processing_selection\" \
504 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
507 f_dialog_data_sanitize menu_choice
508 f_dialog_menutag_store "$menu_choice"
510 if [ $retval -eq $DIALOG_OK ]; then
512 item=$( eval f_dialog_menutag2item${SHOW_DESC:+_with_help} \
513 \"\$menu_choice\" $menu_list )
514 f_dialog_menuitem_store "$item"
520 # f_package_menu_deselect $package
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.
528 f_package_menu_deselect()
531 local prompt # Calculated below
533 'X $msg_installed' '$msg_installed_desc'
534 'R $msg_reinstall' '$msg_reinstall_desc'
535 'U $msg_uninstall' '$msg_uninstall_desc'
537 local hline="$hline_alnum_arrows_punc_tab_enter"
539 prompt=$( printf "$msg_what_would_you_like_to_do_with" "$package" )
541 local height width rows
542 eval f_dialog_menu_size height width rows \
544 \"\$DIALOG_BACKTITLE\" \
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 \
558 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
561 f_dialog_menutag_store -s "$menu_choice"
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.
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"
579 f_dprintf "$funcname: SELECTED_PACKAGES=[%s]" "$SELECTED_PACKAGES"
581 prompt=$( printf "$msg_reviewing_selected_packages" "$_All_nselected" )
583 local package varpkg mark
584 for package in $SELECTED_PACKAGES; do
586 f_str2varname "$package" varpkg
587 f_getvar _mark_$varpkg mark
588 [ "$mark" -a ! "${mark#[IRUD]}" ] || continue
589 menu_list="$menu_list
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
597 menu_list=$( echo "$menu_list" | sort )
599 local height width rows
600 eval f_dialog_menu_size height width rows \
602 \"\$DIALOG_BACKTITLE\" \
607 # Show the review menu (ignore menu choice)
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 \
617 2> /dev/null || return $?
618 # Return if the user pressed ESC or chose Cancel/No
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.
626 for package in $SELECTED_PACKAGES; do
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"
634 for package in $SELECTED_PACKAGES; do
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"
642 for package in $SELECTED_PACKAGES; do
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"
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).
665 # Did we get an INDEX?
666 f_index_initialize packages/INDEX || return $FAILURE
667 # Creates following variables (indirectly via f_index_read())
668 # CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg}
669 # PACKAGE_CATEGORIES _npkgs
671 # Detect installed packages (updates marks/SELECTED_PACKAGES)
672 f_package_detect_installed
673 export PACKAGES_DETECTED=1 # exported for awk(1) ENVIRON[]
675 local retval category varcat defaultitem category_defaultitem=""
677 # Display the list of package categories
678 f_package_menu_categories \
679 CATEGORY_MENU_LIST "$category_defaultitem"
681 f_dialog_menutag_fetch category
682 f_dprintf "retval=%u mtag=[%s]" $retval "$category"
683 category_defaultitem="$category"
685 [ $retval -eq $DIALOG_OK ] || break
687 # Maybe the user chose an action (like `Review')
690 f_package_review && break
696 # Anything else is a package category
698 category=${category# } # Trim leading space if present
699 category=${category%\*} # Trim trailing asterisk if present
701 f_str2varname "$category" varcat
703 local page package varpkg mark menu_choice
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"
712 f_dialog_menutag_fetch menu_choice
713 f_dprintf "retval=%u mtag=[%s]" $retval "$menu_choice"
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" ]
724 elif [ $retval -eq $DIALOG_CANCEL ]; then
725 # Using X11, Xdialog(1) returned 1 for Cancel
726 f_show_msg "%s" "$menu_choice"
728 elif [ $retval -ne $DIALOG_OK ]; then
729 # X11-related error occurred using Xdialog(1)
730 f_show_msg "%s" "$menu_choice"
734 defaultitem="$menu_choice"
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.
746 _index_page_${varcat}_$(( $page - 1 ))
748 defaultitem="${defaultitem%\*}*"
750 defaultitem="${defaultitem%\*}"
752 setvar _defaultitem_$varcat "$defaultitem"
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.
761 _index_page_${varcat}_$(( $page + 1 ))
763 defaultitem="${defaultitem%\*}*"
765 defaultitem="${defaultitem%\*}"
767 setvar _defaultitem_$varcat "$defaultitem"
769 "> "*) # Unknown navigation/action item
770 setvar _defaultpage_$varcat $page
771 continue ;; # Do not treat as a package
773 setvar _defaultitem_$varcat "$defaultitem"
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
785 f_package_deselect $package
789 f_package_select $package
792 f_package_menu_deselect $package || continue
793 f_dialog_menutag_fetch menu_choice
794 case "$menu_choice" in
796 f_package_deselect "$package"
800 f_package_select "$package"
804 f_package_select "$package"
810 export _mark_$varpkg="$mark"
811 # NOTE: exported for awk(1) ENVIRON[]
816 # f_package_add $package_name [$depended]
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).
827 local name="$1" depended="$2" status=$SUCCESS retval
829 local alert=f_show_msg no_confirm=
830 f_getvar $VAR_NO_CONFIRM no_confirm
831 [ "$no_confirm" ] && alert=f_show_info
833 if ! { [ "$name" ] || { f_getvar $VAR_PACKAGE name && [ "$name" ]; }; }
835 f_dprintf "packageAdd: %s" \
836 "$msg_no_package_name_passed_in_package_variable"
840 { # Verify and initialize device media if-defined
842 f_device_init media &&
843 f_index_initialize packages/INDEX
846 # Now we have (indirectly via f_index_read()):
847 # CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg}
848 # PACKAGE_CATEGORIES _npkgs
851 f_str2varname "$name" varpkg
853 # Just as-in the user-interface (opposed to scripted-use), only allow
854 # packages with at least one category to be recognized.
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
862 "$msg_sorry_package_was_not_found_in_the_index" \
866 f_str2varname "$name" varpkg
869 # If invoked through the scripted interface, we likely have not yet
870 # detected the installed packages -- something we should do only once.
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[]
877 # Now we have: _mark_{varpkg}=X for all installed packages
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
887 f_getvar _mark_$varpkg mark && [ "$mark" = "X" ] && return $SUCCESS
889 local dep vardep rundeps=
890 f_getvar _rundeps_$varpkg rundeps
891 for dep in $rundeps; do
892 f_str2varname "$dep" vardep
894 # Skip dependency if already installed
896 f_getvar _mark_$vardep mark && [ "$mark" = "X" ] && continue
898 # Just as-in the user-interface (opposed to scripted-use), only
899 # allow packages with at least one category to be recognized.
902 if ! f_getvar _categories_$vardep depcat || [ ! "$depcat" ]
904 $alert "$msg_required_package_not_found" "$dep"
905 [ "$no_confirm" ] && sleep 2
910 if [ $retval -ne $SUCCESS ]; then
911 status=$(( $status | $retval ))
913 # XXX package could be on a future disc volume
914 # XXX (not supporting multiple disc volumes yet)
916 $alert "$msg_loading_of_dependent_package_failed" \
918 [ "$no_confirm" ] && sleep 2
921 [ $status -eq $SUCCESS ] || return $status
924 # Done with the deps? Try to load the real m'coy.
927 f_package_extract media "$name" "$depended"
929 if [ $retval -ne $SUCCESS ]; then
930 status=$(( $status | $retval ))
932 setvar _mark_$varpkg X
938 # f_package_extract $device $name [$depended]
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.
946 local funcname=f_package_extract
947 local device="$1" name="$2" depended="$3"
949 f_dprintf "$funcname: device=[%s] name=[%s] depended=[%s]" \
950 "$device" "$name" "$depended"
952 # Check to make sure it's not already there
954 f_str2varname "$name" varpkg
955 f_getvar _mark_$varpkg mark
956 [ "$mark" = "X" ] && return $SUCCESS
958 if ! f_device_init $device; then
960 "$msg_unable_to_initialize_media_type_for_package_extract"
964 # If necessary, initialize the ldconfig hints
965 [ -f "/var/run/ld-elf.so.hints" ] ||
966 f_quietly ldconfig /usr/lib /usr/lib/compat /usr/local/lib
968 # Make a couple paranoid locations for temp
969 # files to live if user specified none
971 f_getvar $VAR_PKG_TMPDIR:-/var/tmp tmpdir
972 f_quietly mkdir -p -m 1777 "$tmpdir"
974 local path device_type
975 device_$device get type device_type
979 if [ "$device_type" = "$DEVICE_TYPE_HTTP" ]; then
980 path="$PKG_ABI/latest/All/$name"
982 path="packages/$PKG_ABI/All/$name"
986 # We have a path, call the device strategy routine to check the file
988 for pkg_ext in "" $PACKAGE_EXTENSIONS; do
989 if f_device_get $device "$path$pkg_ext" $PROBE_EXIST; then
993 elif [ "$device_type" = "$DEVICE_TYPE_HTTP" ] &&
994 f_device_get $device \
995 "packages/$PKG_ABI/All/$name$pkg_ext" $PROBE_EXIST
997 # Mirroring physical media over HTTP
998 path="packages/$PKG_ABI/All/$name$pkg_ext"
1003 [ "$found" ] && f_dprintf "$funcname: found path=[%s] dev=[%s]" \
1006 local alert=f_show_msg no_confirm=
1007 f_getvar $VAR_NO_CONFIRM no_confirm
1008 [ "$no_confirm" ] && alert=f_show_info
1010 if [ ! "$found" ]; then
1011 f_dprintf "$funcname: No such %s file on %s device" \
1013 $alert "$msg_unable_to_fetch_package_from_selected_media" \
1015 [ "$no_confirm" ] && sleep 2
1020 f_struct device_$device get name devname
1021 if [ "$depended" ]; then
1022 f_show_info "$msg_adding_package_as_a_dependency_from_media" \
1025 f_show_info "$msg_adding_package_from_media" "$name" "$devname"
1028 # Request the package be added via pkg-install(8)
1029 if f_debugging; then
1030 f_eval_catch $funcname pkg 'pkg -d install -y "%s"' "$name"
1032 f_eval_catch $funcname pkg 'pkg install -y "%s"' "$name"
1034 if [ $? -ne $SUCCESS ]; then
1035 $alert "$msg_pkg_install_apparently_did_not_like_the_package" \
1037 [ "$no_confirm" ] && sleep 2
1039 f_show_info "$msg_package_was_added_successfully" "$name"
1046 # f_package_delete $name
1048 # Delete package by full $name (lacks archive suffix; e.g., `.tbz').
1052 local funcname=f_package_delete
1055 if ! { [ "$name" ] || { f_getvar $VAR_PACKAGE name && [ "$name" ]; }; }
1057 f_dprintf "packageDelete: %s" \
1058 "$msg_no_package_name_passed_in_package_variable"
1062 f_dprintf "$funcname: name=[%s]" "$name"
1064 [ "$name" ] || return $FAILURE
1066 { # Verify and initialize device media if-defined
1068 f_device_init media &&
1069 f_index_initialize packages/INDEX
1070 } || return $FAILURE
1072 # Now we have (indirectly via f_index_read()):
1073 # CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg}
1074 # PACKAGE_CATEGORIES _npkgs
1077 f_str2varname "$name" varpkg
1079 # Just as-in the user-interface (opposed to scripted-use), only allow
1080 # packages with at least one category to be recognized.
1083 if ! f_getvar _categories_$varpkg pkgcat || [ ! "$pkgcat" ]; then
1084 # $pkg may be a partial name, search the index (this is slow)
1085 f_index_search PACKAGE_INDEX "$name" name
1086 if [ ! "$name" ]; then
1088 "$msg_sorry_package_was_not_found_in_the_index" \
1092 f_str2varname "$name" varpkg
1095 # If invoked through the scripted interface, we likely have not yet
1096 # detected the installed packages -- something we should do only once.
1098 if [ ! "$PACKAGES_DETECTED" ]; then
1099 f_dprintf "$funcname: Detecting installed packages"
1100 f_package_detect_installed
1101 export PACKAGES_DETECTED=1 # exported for awk(1) ENVIRON[]
1103 # Now we have: _mark_{varpkg}=X for all installed packages
1106 # Return failure if the package is not already installed.
1109 f_getvar _mark_$varpkg pkgmark
1110 if ! [ "$pkgmark" -a ! "${pkgmark#[XUR]}" ]; then
1111 f_show_msg "$msg_package_not_installed_cannot_delete" "$name"
1116 # Check for dependencies
1118 local pkgsel depc=0 udeps=
1119 for pkgsel in $SELECTED_PACKAGES; do
1121 f_str2varname $pkgsel varpkg
1122 debug= f_getvar _mark_$varpkg mark
1123 [ "$mark" -a ! "${mark#[XUR]}" ] || continue
1125 debug= f_getvar _rundeps_$varpkg rundeps
1126 for dep in $rundeps; do
1127 if [ "$dep" = "$name" ]; then
1128 # Maybe this package is marked for deletion too
1129 if [ "$mark" = "U" ]; then
1130 udeps="$udeps $pkgsel"
1132 depc=$(( $depc + 1 ))
1138 if [ $depc -gt 0 ]; then
1139 local grammatical_s=
1140 [ $depc -gt 1 ] && grammatical_s=s
1142 "$msg_package_is_needed_by_other_installed_packages" \
1143 "$name" "$depc" "$grammatical_s"
1148 # Chase dependencies that are marked for uninstallation
1150 for pkgsel in $udeps; do
1151 f_dprintf "$funcname: Uninstalling dependency %s (%s)" \
1152 "$pkgsel" "marked for delete"
1153 f_package_delete "$pkgsel"
1157 # OK to perform the delete (no other packages depend on it)...
1159 f_show_info "$msg_uninstalling_package_waiting_for_pkg_delete" "$name"
1160 if f_debugging; then
1161 f_eval_catch $funcname pkg 'pkg -d delete -y "%s"' "$name"
1163 f_eval_catch $funcname pkg 'pkg delete -y "%s"' "$name"
1165 if [ $? -ne $SUCCESS ]; then
1166 f_show_msg "$msg_pkg_delete_failed" "$name"
1169 f_dprintf "$funcname: pkg-delete(8) of %s successful" "$name"
1170 f_str2varname "$name" varpkg
1171 setvar _mark_$varpkg ""
1175 # f_package_reinstall $name
1177 # A simple wrapper to f_package_delete() + f_package_add()
1179 f_package_reinstall()
1181 f_package_delete "$1" && f_package_add "$1"
1184 ############################################################ MAIN
1186 f_dprintf "%s: Successfully loaded." packages/packages.subr
1188 fi # ! $_PACKAGES_PACKAGES_SUBR