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