1 if [ ! "$_PACKAGES_PACKAGES_SUBR" ]; then _PACKAGES_PACKAGES_SUBR=1
3 # Copyright (c) 2013-2016 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/packages/musthavepkg.subr
40 f_include $BSDCFG_SHARE/strings.subr
42 BSDCFG_LIBE="/usr/libexec/bsdconfig"
43 f_include_lang $BSDCFG_LIBE/include/messages.subr
45 ############################################################ CONFIGURATION
48 # How many packages to display (maximum) per dialog menubox.
50 : ${PACKAGE_MENU_PAGESIZE:=2000}
52 ############################################################ GLOBALS
55 # Package extensions to try
57 PACKAGE_EXTENSIONS=".txz .tbz .tbz2 .tgz"
60 # Variables used to track runtime states
62 PACKAGES_DETECTED= # Boolean (NULL/non-NULL); detected installed packages?
63 PACKAGE_CATEGORIES= # List of package categories parsed from INDEX
64 SELECTED_PACKAGES= # Packages selected by user in [X]dialog(1) interface
69 [ "${SHOW_DESC+set}" ] || SHOW_DESC=1
71 ############################################################ FUNCTIONS
73 # eval f_package_accent_category_menu $var_to_set $CATEGORY_MENU_LIST
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
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).
86 f_package_accent_category_menu()
88 local var_to_set="$1" category cat desc help varcat menu_buf n
90 while [ $# -gt 0 ]; do
91 category="${1%\*}" desc="${2%%; *}" help="$3"
92 shift 3 # cat/desc/help
94 cat="${category# }" # Trim lead space inserted by sort-method
95 f_str2varname "$cat" varcat
97 # Add number of installed packages for this category (if any)
100 "$msg_all") debug= f_getvar "_All_ninstalled" n ;;
101 *) debug= f_getvar "_${varcat}_ninstalled" n ;;
103 [ $n -ge 1 ] && desc="$desc; $n $msg_installed_lc"
105 # Add number of selected packages for this category (if any)
108 "$msg_all") debug= f_getvar "_All_nselected" n ;;
109 *) debug= f_getvar "_${varcat}_nselected" n ;;
111 [ $n -ge 1 ] && desc="$desc; $n $msg_selected"
113 # Re-Add asterisk to the category if its index has been cached
114 f_isset _index_page_${varcat}_1 && category="$category*"
116 # Update buffer with modified elements
118 '$category' '$desc' '$help'" # End-Quote
120 setvar "$var_to_set" "$menu_buf" # return our buffer
123 # f_package_select $package ...
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.
132 while [ $# -gt 0 ]; do
135 for pkgsel in $SELECTED_PACKAGES; do
136 [ "$package" = "$pkgsel" ] && return $SUCCESS
138 SELECTED_PACKAGES="$SELECTED_PACKAGES $package"
139 f_dprintf "Added %s to selection list" "$package"
141 SELECTED_PACKAGES="${SELECTED_PACKAGES# }" # Trim leading space
144 # f_package_deselect $package ...
146 # Remove $package from the 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.
153 while [ $# -gt 1 ]; do
157 for pkgsel in $SELECTED_PACKAGES; do
158 [ "$pkgsel" = "$package" ] && continue
159 new_list="$new_list${new_list:+ }$pkgsel"
161 SELECTED_PACKAGES="$new_list"
162 f_dprintf "Removed %s from selection list" "$package"
166 # f_package_detect_installed
168 # Detect installed packages. Currently this uses pkg-query(8) for querying
169 # entries and marks each entry as an installed/selected package.
171 f_package_detect_installed()
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
181 # f_package_calculate_totals
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.
190 # Calculations are performed 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).
198 f_package_calculate_totals()
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
206 for pkg in $SELECTED_PACKAGES; do
207 f_str2varname $pkg varpkg
209 f_getvar _mark_$varpkg mark
212 X) tinstalled=$(( $tinstalled + 1 )) ;;
213 *) tselected=$(( $tselected + 1 ))
215 f_getvar _categories_$varpkg pkgcat
216 for cat in $pkgcat; do
217 f_str2varname $cat varcat
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 ))
227 _All_nselected=$tselected
228 _All_ninstalled=$tinstalled
231 # f_package_calculate_rundeps
233 # Update package dependencies by first unmarking all dependencies and then
234 # re-marking all dependencies of packages marked for either install ("I") or
237 f_package_calculate_rundeps()
239 local pkg varpkg mark rundeps dep vardep
242 # First unmark all the existing run-dependencies
244 f_dprintf "Unselecting package run-dependencies..."
245 for pkg in $SELECTED_PACKAGES; do
246 f_str2varname $pkg varpkg
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
253 f_package_deselect $pkg
258 # Processes selected packages, adding dependencies
260 f_dprintf "Re-selecting package run-dependencies..."
261 for pkg in $SELECTED_PACKAGES; do
262 f_str2varname $pkg varpkg
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
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
279 f_dprintf "Finished recalculating dependencies."
282 # f_package_menu_categories $var_to_set $defaultitem
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).
290 f_package_menu_categories()
292 local var_to_get="$1" defaultitem="$2"
293 local prompt="$msg_please_select_a_category_to_display"
295 '> $msg_review' '$msg_review_desc' '$msg_review_help'
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
305 debug= f_getvar "$var_to_get" category_list || return $DIALOG_CANCEL
307 # Accent the category menu list with ninstalled/nselected
308 eval f_package_accent_category_menu category_list $category_list
310 # Add list of categories to menu list
311 menu_list="$menu_list $category_list"
313 local height width rows
314 eval f_dialog_menu_with_help_size height width rows \
316 \"\$DIALOG_BACKTITLE\" \
321 menu_choice=$( eval $DIALOG \
322 --title \"\$DIALOG_TITLE\" \
323 --backtitle \"\$DIALOG_BACKTITLE\" \
324 --hline \"\$hline\" \
326 --default-item \"\$defaultitem\" \
327 --ok-label \"$msg_select\" \
328 --cancel-label \"$msg_cancel\" \
329 --menu \"\$prompt\" \
330 $height $width $rows \
332 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
335 f_dialog_menutag_store -s "$menu_choice"
339 # f_package_index_get_page $category $page [$var_to_set [$var_to_get]]
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.
347 f_package_index_get_page()
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 &&
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
360 # Update category default-item because now we're cached
362 category_defaultitem="${category_defaultitem%\*}*"
368 # f_package_menu_select $category [$page [$defaultitem]]
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).
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().
380 f_package_menu_select()
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"
388 f_isinteger "$page" || return $DIALOG_CANCEL
391 f_str2varname "$category" varcat
393 # Get number of packages for this category
396 "$msg_all"|"") npkgs="${_npkgs:-0}" ;;
397 *) f_getvar _npkgs_$varcat npkgs
400 # Calculate number of pages
401 local npages=$(( ${npkgs:=0} / $PACKAGE_MENU_PAGESIZE ))
403 # Add a page to the pagecount if not evenly divisible
404 [ $(( $npages * $PACKAGE_MENU_PAGESIZE )) -lt $npkgs ] &&
405 npages=$(( $npages + 1 ))
407 # Print some debugging information
408 f_dprintf "f_package_menu_select: category=[%s] npkgs=%u npages=%u" \
409 "$category" "$npkgs" "$npages"
411 local add_prev="" add_next=""
412 local previous_page="$msg_previous_page" next_page="$msg_next_page"
413 if [ $page -gt 1 ]; then
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*"
420 if [ $page -lt $npages ]; then
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*"
429 f_package_index_get_page "$category" $page index_page
432 ${add_prev:+'> $previous_page' '' ${SHOW_DESC:+''}}
433 ${add_next:+'> $next_page' '' ${SHOW_DESC:+''}}
436 export VALID_VARNAME_CHARS
437 echo "$index_page" | awk -F'|' -v view="port" '
439 valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
443 cur_prefix = tolower(substr($1, 1, 1))
445 if ( prefix != cur_prefix )
450 if ( view == "port" )
453 gsub("[^" valid_chars "]", "_", varpkg)
454 mark = ENVIRON["_mark_" varpkg]
455 if ( ! mark ) mark = " "
456 printf "%s'\'' '\''[%c] %s'\''",
458 if ( ENVIRON["SHOW_DESC"] ) {
460 gsub(/'\''/, "'\''\\'\'\''", help)
461 printf " '\''%s'\''", help
466 ${add_prev:+'> $previous_page' '' ${SHOW_DESC:+''}}
467 ${add_next:+'> $next_page' '' ${SHOW_DESC:+''}}
470 # Accept/Translate i18n "All" but other category names must
471 # match tree definitions from INDEX, ports, FTP, etc.
473 "$msg_all"|"") f_category_desc_get "All" prompt ;;
474 *) f_category_desc_get "$category" prompt ;;
476 f_sprintf prompt "%s $msg_page_of_npages" "$prompt" "$page" "$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 f_sprintf prompt "$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 f_sprintf prompt "$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 || return $FAILURE
667 # Creates following variables (indirectly via f_index_read())
668 # CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg}
669 # PACKAGE_CATEGORIES _npkgs
671 f_show_info "$msg_building_package_main_menu"
673 # Detect installed packages (updates marks/SELECTED_PACKAGES)
674 f_package_detect_installed
675 export PACKAGES_DETECTED=1 # exported for awk(1) ENVIRON[]
677 local retval category varcat defaultitem category_defaultitem=""
679 # Display the list of package categories
680 f_package_menu_categories \
681 CATEGORY_MENU_LIST "$category_defaultitem"
683 f_dialog_menutag_fetch category
684 f_dprintf "retval=%u mtag=[%s]" $retval "$category"
685 category_defaultitem="$category"
687 [ $retval -eq $DIALOG_OK ] || break
689 # Maybe the user chose an action (like `Review')
692 f_package_review && break
698 # Anything else is a package category
700 category=${category# } # Trim leading space if present
701 category=${category%\*} # Trim trailing asterisk if present
703 f_str2varname "$category" varcat
705 local page package varpkg mark menu_choice
707 # Display the list of packages for selected category
708 page=1 defaultitem=""
709 f_getvar _defaultitem_$varcat defaultitem
710 f_getvar _defaultpage_$varcat page
711 f_package_menu_select \
712 "$category" "${page:=1}" "$defaultitem"
714 f_dialog_menutag_fetch menu_choice
715 f_dprintf "retval=%u mtag=[%s]" $retval "$menu_choice"
717 # NOTE: When --and-widget is used only ESC will cause
718 # dialog(1) to return without going to the next widget.
719 # This is alright in our case as we can still detect
720 # the Cancel button because stdout will be NULL.
721 # Alternatively, Xdialog(1) will terminate with 1
722 # if/when Cancel is chosen on any widget.
723 if [ $retval -eq $DIALOG_ESC -o ! "$menu_choice" ]
726 elif [ $retval -eq $DIALOG_CANCEL ]; then
727 # Using X11, Xdialog(1) returned 1 for Cancel
728 f_show_msg "%s" "$menu_choice"
730 elif [ $retval -ne $DIALOG_OK ]; then
731 # X11-related error occurred using Xdialog(1)
732 f_show_msg "%s" "$menu_choice"
736 defaultitem="$menu_choice"
738 # NOTE: f_package_menu_select() does not show the
739 # `Previous Page' or `Next Page' items unless needed
740 case "$menu_choice" in
741 "> $msg_previous_page"|"> $msg_previous_page*")
742 page=$(( $page - 1 ))
743 setvar _defaultpage_$varcat $page
744 # Update default-item to match accent that will
745 # be applied by f_package_menu_select(); if the
746 # page-before-prev is cached, add an asterisk.
748 _index_page_${varcat}_$(( $page - 1 ))
750 defaultitem="${defaultitem%\*}*"
752 defaultitem="${defaultitem%\*}"
754 setvar _defaultitem_$varcat "$defaultitem"
756 "> $msg_next_page"|"> $msg_next_page*")
757 page=$(( $page + 1 ))
758 setvar _defaultpage_$varcat $page
759 # Update default-item to match accent that will
760 # be applied by f_package_menu_select(); if the
761 # page-after-next is cached, add an asterisk.
763 _index_page_${varcat}_$(( $page + 1 ))
765 defaultitem="${defaultitem%\*}*"
767 defaultitem="${defaultitem%\*}"
769 setvar _defaultitem_$varcat "$defaultitem"
771 "> "*) # Unknown navigation/action item
772 setvar _defaultpage_$varcat $page
773 continue ;; # Do not treat as a package
775 setvar _defaultitem_$varcat "$defaultitem"
778 # Treat any other selection as a package
779 package="${menu_choice# }" # Trim leading space
780 f_str2varname $package varpkg
781 f_dialog_menuitem_fetch mark
787 f_package_deselect $package
791 f_package_select $package
794 f_package_menu_deselect $package || continue
795 f_dialog_menutag_fetch menu_choice
796 case "$menu_choice" in
798 f_package_deselect "$package"
802 f_package_select "$package"
806 f_package_select "$package"
812 export _mark_$varpkg="$mark"
813 # NOTE: exported for awk(1) ENVIRON[]
818 # f_package_add $package_name [$depended]
820 # Like f_package_extract(), but assumes current media device and chases deps.
821 # Note that $package_name should not contain the archive suffix (e.g., `.tbz').
822 # If $depended is present and non-NULL, the package is treated as a dependency
823 # (in this function, dependencies are not handled any differently, but the
824 # f_package_extract() function is passed this value and it displays a different
825 # message when installing a dependency versus non-dependency).
829 local name="$1" depended="$2" status=$SUCCESS retval
831 local alert=f_show_msg no_confirm=
832 f_getvar $VAR_NO_CONFIRM no_confirm
833 [ "$no_confirm" ] && alert=f_show_info
835 if ! { [ "$name" ] || { f_getvar $VAR_PACKAGE name && [ "$name" ]; }; }
837 f_dprintf "packageAdd: %s" \
838 "$msg_no_package_name_passed_in_package_variable"
842 { # Verify and initialize device media if-defined
844 f_device_init device_media &&
848 # Now we have (indirectly via f_index_read()):
849 # CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg}
850 # PACKAGE_CATEGORIES _npkgs
853 f_str2varname "$name" varpkg
855 # Just as-in the user-interface (opposed to scripted-use), only allow
856 # packages with at least one category to be recognized.
859 if ! f_getvar _categories_$varpkg pkgcat || [ ! "$pkgcat" ]; then
860 # $pkg may be a partial name, search the index (this is slow)
861 f_index_search PACKAGE_INDEX $name name
862 if [ ! "$name" ]; then
864 "$msg_sorry_package_was_not_found_in_the_index" \
868 f_str2varname "$name" varpkg
871 # If invoked through the scripted interface, we likely have not yet
872 # detected the installed packages -- something we should do only once.
874 if [ ! "$PACKAGES_DETECTED" ]; then
875 f_dprintf "f_package_add: Detecting installed packages"
876 f_package_detect_installed
877 export PACKAGES_DETECTED=1 # exported for awk(1) ENVIRON[]
879 # Now we have: _mark_{varpkg}=X for all installed packages
882 # Since we're maintaining data structures for installed packages,
883 # short-circuit the package dependency checks if the package is already
884 # installed. This prevents wasted cycles, minor delays between package
885 # extractions, and worst-case an infinite loop with a certain faulty
889 f_getvar _mark_$varpkg mark && [ "$mark" = "X" ] && return $SUCCESS
891 local dep vardep rundeps=
892 f_getvar _rundeps_$varpkg rundeps
893 for dep in $rundeps; do
894 f_str2varname "$dep" vardep
896 # Skip dependency if already installed
898 f_getvar _mark_$vardep mark && [ "$mark" = "X" ] && continue
900 # Just as-in the user-interface (opposed to scripted-use), only
901 # allow packages with at least one category to be recognized.
904 if ! f_getvar _categories_$vardep depcat || [ ! "$depcat" ]
906 $alert "$msg_required_package_not_found" "$dep"
907 [ "$no_confirm" ] && sleep 2
912 if [ $retval -ne $SUCCESS ]; then
913 status=$(( $status | $retval ))
915 # XXX package could be on a future disc volume
916 # XXX (not supporting multiple disc volumes yet)
918 $alert "$msg_loading_of_dependent_package_failed" \
920 [ "$no_confirm" ] && sleep 2
923 [ $status -eq $SUCCESS ] || return $status
926 # Done with the deps? Try to load the real m'coy.
929 f_package_extract device_media "$name" "$depended"
931 if [ $retval -ne $SUCCESS ]; then
932 status=$(( $status | $retval ))
934 setvar _mark_$varpkg X
940 # f_package_extract $device $name [$depended]
942 # Extract a package based on a namespec and media device. If $depended is
943 # present and non-NULL, the notification displayed while installing the package
944 # has "as a dependency" appended.
948 local funcname=f_package_extract
949 local device="$1" name="$2" depended="$3"
952 f_musthavepkg_init # Make sure we have a usable pkg(8) with $PKG_ABI
954 $device get name devname
955 f_dprintf "$funcname: device=[%s] name=[%s] depended=[%s]" \
956 "$devname" "$name" "$depended"
958 # Check to make sure it's not already there
960 f_str2varname "$name" varpkg
961 f_getvar _mark_$varpkg mark
962 [ "$mark" = "X" ] && return $SUCCESS
964 if ! f_device_init $device; then
966 "$msg_unable_to_initialize_media_type_for_package_extract"
970 # If necessary, initialize the ldconfig hints
971 [ -f "/var/run/ld-elf.so.hints" ] ||
972 f_quietly ldconfig /usr/lib /usr/lib/compat /usr/local/lib
974 # Make a couple paranoid locations for temp
975 # files to live if user specified none
977 f_getvar $VAR_PKG_TMPDIR:-/var/tmp tmpdir
978 f_quietly mkdir -p -m 1777 "$tmpdir"
980 local path device_type
981 $device get type device_type
985 if [ "$device_type" = "$DEVICE_TYPE_HTTP" ]; then
986 path="$PKG_ABI/latest/All/$name"
988 path="packages/$PKG_ABI/All/$name"
992 # We have a path, call the device strategy routine to check the file
994 for pkg_ext in "" $PACKAGE_EXTENSIONS; do
995 if f_device_get $device "$path$pkg_ext" $PROBE_EXIST; then
999 elif [ "$device_type" = "$DEVICE_TYPE_HTTP" ] &&
1000 f_device_get $device \
1001 "packages/$PKG_ABI/All/$name$pkg_ext" $PROBE_EXIST
1003 # Mirroring physical media over HTTP
1004 path="packages/$PKG_ABI/All/$name$pkg_ext"
1009 [ "$found" ] && f_dprintf "$funcname: found path=[%s] dev=[%s]" \
1012 local alert=f_show_msg no_confirm=
1013 f_getvar $VAR_NO_CONFIRM no_confirm
1014 [ "$no_confirm" ] && alert=f_show_info
1016 if [ ! "$found" ]; then
1017 f_dprintf "$funcname: No such %s file on %s device" \
1019 $alert "$msg_unable_to_fetch_package_from_selected_media" \
1021 [ "$no_confirm" ] && sleep 2
1025 if [ "$depended" ]; then
1026 f_show_info "$msg_adding_package_as_a_dependency_from_media" \
1029 f_show_info "$msg_adding_package_from_media" "$name" "$devname"
1032 # Request the package be added via pkg-install(8)
1033 if f_debugging; then
1034 f_eval_catch $funcname pkg \
1035 'pkg -d install -${depended:+A}y "%s"' "$name"
1037 f_eval_catch $funcname pkg \
1038 'pkg install -${depended:+A}y "%s"' "$name"
1040 if [ $? -ne $SUCCESS ]; then
1041 $alert "$msg_pkg_install_apparently_did_not_like_the_package" \
1043 [ "$no_confirm" ] && sleep 2
1045 f_show_info "$msg_package_was_added_successfully" "$name"
1052 # f_package_delete $name
1054 # Delete package by full $name (lacks archive suffix; e.g., `.tbz').
1058 local funcname=f_package_delete
1061 if ! { [ "$name" ] || { f_getvar $VAR_PACKAGE name && [ "$name" ]; }; }
1063 f_dprintf "packageDelete: %s" \
1064 "$msg_no_package_name_passed_in_package_variable"
1068 f_dprintf "$funcname: name=[%s]" "$name"
1070 [ "$name" ] || return $FAILURE
1072 { # Verify and initialize device media if-defined
1074 f_device_init device_media &&
1076 } || return $FAILURE
1078 # Now we have (indirectly via f_index_read()):
1079 # CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg}
1080 # PACKAGE_CATEGORIES _npkgs
1083 f_str2varname "$name" varpkg
1085 # Just as-in the user-interface (opposed to scripted-use), only allow
1086 # packages with at least one category to be recognized.
1089 if ! f_getvar _categories_$varpkg pkgcat || [ ! "$pkgcat" ]; then
1090 # $pkg may be a partial name, search the index (this is slow)
1091 f_index_search PACKAGE_INDEX "$name" name
1092 if [ ! "$name" ]; then
1094 "$msg_sorry_package_was_not_found_in_the_index" \
1098 f_str2varname "$name" varpkg
1101 # If invoked through the scripted interface, we likely have not yet
1102 # detected the installed packages -- something we should do only once.
1104 if [ ! "$PACKAGES_DETECTED" ]; then
1105 f_dprintf "$funcname: Detecting installed packages"
1106 f_package_detect_installed
1107 export PACKAGES_DETECTED=1 # exported for awk(1) ENVIRON[]
1109 # Now we have: _mark_{varpkg}=X for all installed packages
1112 # Return failure if the package is not already installed.
1115 f_getvar _mark_$varpkg pkgmark
1116 if ! [ "$pkgmark" -a ! "${pkgmark#[XUR]}" ]; then
1117 f_show_msg "$msg_package_not_installed_cannot_delete" "$name"
1122 # Check for dependencies
1124 local pkgsel depc=0 udeps=
1125 for pkgsel in $SELECTED_PACKAGES; do
1127 f_str2varname $pkgsel varpkg
1128 debug= f_getvar _mark_$varpkg mark
1129 [ "$mark" -a ! "${mark#[XUR]}" ] || continue
1131 debug= f_getvar _rundeps_$varpkg rundeps
1132 for dep in $rundeps; do
1133 if [ "$dep" = "$name" ]; then
1134 # Maybe this package is marked for deletion too
1135 if [ "$mark" = "U" ]; then
1136 udeps="$udeps $pkgsel"
1138 depc=$(( $depc + 1 ))
1144 if [ $depc -gt 0 ]; then
1145 local grammatical_s=
1146 [ $depc -gt 1 ] && grammatical_s=s
1148 "$msg_package_is_needed_by_other_installed_packages" \
1149 "$name" "$depc" "$grammatical_s"
1154 # Chase dependencies that are marked for uninstallation
1156 for pkgsel in $udeps; do
1157 f_dprintf "$funcname: Uninstalling dependency %s (%s)" \
1158 "$pkgsel" "marked for delete"
1159 f_package_delete "$pkgsel"
1163 # OK to perform the delete (no other packages depend on it)...
1165 f_show_info "$msg_uninstalling_package_waiting_for_pkg_delete" "$name"
1166 if f_debugging; then
1167 f_eval_catch $funcname pkg 'pkg -d delete -y "%s"' "$name"
1169 f_eval_catch $funcname pkg 'pkg delete -y "%s"' "$name"
1171 if [ $? -ne $SUCCESS ]; then
1172 f_show_msg "$msg_pkg_delete_failed" "$name"
1175 f_dprintf "$funcname: pkg-delete(8) of %s successful" "$name"
1176 f_str2varname "$name" varpkg
1177 setvar _mark_$varpkg ""
1181 # f_package_reinstall $name
1183 # A simple wrapper to f_package_delete() + f_package_add()
1185 f_package_reinstall()
1187 f_package_delete "$1" && f_package_add "$1"
1190 ############################################################ MAIN
1192 f_dprintf "%s: Successfully loaded." packages/packages.subr
1194 fi # ! $_PACKAGES_PACKAGES_SUBR