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/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 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.
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 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).
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 # 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 device_media &&
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 device_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"
950 f_musthavepkg_init # Make sure we have a usable pkg(8) with $PKG_ABI
952 $device get name devname
953 f_dprintf "$funcname: device=[%s] name=[%s] depended=[%s]" \
954 "$devname" "$name" "$depended"
956 # Check to make sure it's not already there
958 f_str2varname "$name" varpkg
959 f_getvar _mark_$varpkg mark
960 [ "$mark" = "X" ] && return $SUCCESS
962 if ! f_device_init $device; then
964 "$msg_unable_to_initialize_media_type_for_package_extract"
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
972 # Make a couple paranoid locations for temp
973 # files to live if user specified none
975 f_getvar $VAR_PKG_TMPDIR:-/var/tmp tmpdir
976 f_quietly mkdir -p -m 1777 "$tmpdir"
978 local path device_type
979 $device get type device_type
983 if [ "$device_type" = "$DEVICE_TYPE_HTTP" ]; then
984 path="$PKG_ABI/latest/All/$name"
986 path="packages/$PKG_ABI/All/$name"
990 # We have a path, call the device strategy routine to check the file
992 for pkg_ext in "" $PACKAGE_EXTENSIONS; do
993 if f_device_get $device "$path$pkg_ext" $PROBE_EXIST; then
997 elif [ "$device_type" = "$DEVICE_TYPE_HTTP" ] &&
998 f_device_get $device \
999 "packages/$PKG_ABI/All/$name$pkg_ext" $PROBE_EXIST
1001 # Mirroring physical media over HTTP
1002 path="packages/$PKG_ABI/All/$name$pkg_ext"
1007 [ "$found" ] && f_dprintf "$funcname: found path=[%s] dev=[%s]" \
1010 local alert=f_show_msg no_confirm=
1011 f_getvar $VAR_NO_CONFIRM no_confirm
1012 [ "$no_confirm" ] && alert=f_show_info
1014 if [ ! "$found" ]; then
1015 f_dprintf "$funcname: No such %s file on %s device" \
1017 $alert "$msg_unable_to_fetch_package_from_selected_media" \
1019 [ "$no_confirm" ] && sleep 2
1023 if [ "$depended" ]; then
1024 f_show_info "$msg_adding_package_as_a_dependency_from_media" \
1027 f_show_info "$msg_adding_package_from_media" "$name" "$devname"
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"
1035 f_eval_catch $funcname pkg \
1036 'pkg install -${depended:+A}y "%s"' "$name"
1038 if [ $? -ne $SUCCESS ]; then
1039 $alert "$msg_pkg_install_apparently_did_not_like_the_package" \
1041 [ "$no_confirm" ] && sleep 2
1043 f_show_info "$msg_package_was_added_successfully" "$name"
1050 # f_package_delete $name
1052 # Delete package by full $name (lacks archive suffix; e.g., `.tbz').
1056 local funcname=f_package_delete
1059 if ! { [ "$name" ] || { f_getvar $VAR_PACKAGE name && [ "$name" ]; }; }
1061 f_dprintf "packageDelete: %s" \
1062 "$msg_no_package_name_passed_in_package_variable"
1066 f_dprintf "$funcname: name=[%s]" "$name"
1068 [ "$name" ] || return $FAILURE
1070 { # Verify and initialize device media if-defined
1072 f_device_init device_media &&
1074 } || return $FAILURE
1076 # Now we have (indirectly via f_index_read()):
1077 # CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg}
1078 # PACKAGE_CATEGORIES _npkgs
1081 f_str2varname "$name" varpkg
1083 # Just as-in the user-interface (opposed to scripted-use), only allow
1084 # packages with at least one category to be recognized.
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
1092 "$msg_sorry_package_was_not_found_in_the_index" \
1096 f_str2varname "$name" varpkg
1099 # If invoked through the scripted interface, we likely have not yet
1100 # detected the installed packages -- something we should do only once.
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[]
1107 # Now we have: _mark_{varpkg}=X for all installed packages
1110 # Return failure if the package is not already installed.
1113 f_getvar _mark_$varpkg pkgmark
1114 if ! [ "$pkgmark" -a ! "${pkgmark#[XUR]}" ]; then
1115 f_show_msg "$msg_package_not_installed_cannot_delete" "$name"
1120 # Check for dependencies
1122 local pkgsel depc=0 udeps=
1123 for pkgsel in $SELECTED_PACKAGES; do
1125 f_str2varname $pkgsel varpkg
1126 debug= f_getvar _mark_$varpkg mark
1127 [ "$mark" -a ! "${mark#[XUR]}" ] || continue
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"
1136 depc=$(( $depc + 1 ))
1142 if [ $depc -gt 0 ]; then
1143 local grammatical_s=
1144 [ $depc -gt 1 ] && grammatical_s=s
1146 "$msg_package_is_needed_by_other_installed_packages" \
1147 "$name" "$depc" "$grammatical_s"
1152 # Chase dependencies that are marked for uninstallation
1154 for pkgsel in $udeps; do
1155 f_dprintf "$funcname: Uninstalling dependency %s (%s)" \
1156 "$pkgsel" "marked for delete"
1157 f_package_delete "$pkgsel"
1161 # OK to perform the delete (no other packages depend on it)...
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"
1167 f_eval_catch $funcname pkg 'pkg delete -y "%s"' "$name"
1169 if [ $? -ne $SUCCESS ]; then
1170 f_show_msg "$msg_pkg_delete_failed" "$name"
1173 f_dprintf "$funcname: pkg-delete(8) of %s successful" "$name"
1174 f_str2varname "$name" varpkg
1175 setvar _mark_$varpkg ""
1179 # f_package_reinstall $name
1181 # A simple wrapper to f_package_delete() + f_package_add()
1183 f_package_reinstall()
1185 f_package_delete "$1" && f_package_add "$1"
1188 ############################################################ MAIN
1190 f_dprintf "%s: Successfully loaded." packages/packages.subr
1192 fi # ! $_PACKAGES_PACKAGES_SUBR