packages.subr revision 251236
1if [ ! "$_PACKAGES_PACKAGES_SUBR" ]; then _PACKAGES_PACKAGES_SUBR=1
2#
3# Copyright (c) 2013 Devin Teske
4# All Rights Reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INLUDING, 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: head/usr.sbin/bsdconfig/share/packages/packages.subr 251236 2013-06-01 23:58:44Z dteske $
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/strings.subr
36f_include $BSDCFG_SHARE/packages/categories.subr
37f_include $BSDCFG_SHARE/packages/index.subr
38
39BSDCFG_LIBE="/usr/libexec/bsdconfig"
40f_include_lang $BSDCFG_LIBE/include/messages.subr
41
42############################################################ CONFIGURATION
43
44#
45# How many packages to display (maximum) per dialog menubox.
46#
47: ${PACKAGE_MENU_PAGESIZE:=2000}
48
49############################################################ GLOBALS
50
51PACKAGE_CATEGORIES=
52SELECTED_PACKAGES=
53
54#
55# Options
56#
57[ "${SHOW_DESC+set}" ] || SHOW_DESC=1
58
59############################################################ FUNCTIONS
60
61# eval f_package_accent_category_menu $var_to_set $CATEGORY_MENU_LIST
62#
63# Accent the CATEGORY_MENU_LIST produced by f_index_read() (see
64# packages/index.subr). Accented information includes adding an asterisk to the
65# category name if its index has been cached, adding the number of installed
66# packages for each category, and adding the number _selected_ packages for
67# each category.
68#
69# NOTE: The reason `eval' is recommended/shown for the syntax above is because
70# the $CATEGORY_MENU_LIST generated by f_index_read() is meant to be expanded
71# prior to execution (it contains a series of pre-quoted strings which act as
72# the interpolated command arguments).
73#
74f_package_accent_category_menu()
75{
76	local var_to_set="$1" cat desc help varcat menu_buf n
77	shift 1 # var_to_set
78	while [ $# -gt 0 ]; do
79		cat="${1%\*}" desc="${2%%; *}" help="$3"
80		shift 3 # cat/desc/help
81
82		cat="${cat# }" # Trim leading space inserted by sort-method
83		f_str2varname "$cat" varcat
84
85		# Add number of installed packages for this category (if any)
86		n=0
87		case "$cat" in
88		"$msg_all") debug= f_getvar "_All_ninstalled" n ;;
89		         *) debug= f_getvar "_${varcat}_ninstalled" n ;;
90		esac &&
91			[ $n -ge 1 ] && desc="$desc; $n $msg_installed_lc"
92
93		# Add number of selected packages for this category (if any)
94		n=0
95		case "$cat" in
96		"$msg_all") debug= f_getvar "_All_nselected" n ;;
97		         *) debug= f_getvar "_${varcat}_nselected" n ;;
98		esac &&
99			[ $n -ge 1 ] && desc="$desc; $n $msg_selected"
100
101		# Add an asterisk to the category if its index has been cached
102		f_isset _index_page_${varcat}_1 && cat="$cat*"
103
104		# Update buffer with modified elements
105		menu_buf="$menu_buf
106		'$cat' '$desc' '$help'" # End-Quote
107	done
108	setvar "$var_to_set" "$menu_buf" # return our buffer
109}
110
111# f_package_select $package ...
112#
113# Add $package to the list of tracked/selected packages. If $package is already
114# being tracked (already apears in $SELECTED_PACKAGES), this function amounts
115# to having no effect.
116#
117f_package_select()
118{
119	local package pkgsel
120	while [ $# -gt 0 ]; do
121		package="$1"
122		shift 1 # package
123		for pkgsel in $SELECTED_PACKAGES; do
124			[ "$package" = "$pkgsel" ] && return
125		done
126		SELECTED_PACKAGES="$SELECTED_PACKAGES $package"
127	done
128	SELECTED_PACKAGES="${SELECTED_PACKAGES# }" # Trim leading space
129}
130
131# f_package_deselect $package ...
132#
133# Remove $package from teh list of tracked/selected packages. If $package is
134# not being tracked (doesn't appear in $SELECTED_PACKAGES), this function
135# amounts to having no effet.
136#
137f_package_deselect()
138{
139	local package pkgsel
140	while [ $# -gt 1 ]; do
141		local new_list=""
142		package="$1"
143		shift 1 # package
144		for pkgsel in $SELECTED_PACKAGES; do
145			[ "$pkgsel" = "$package" ] && continue
146			new_list="$new_list${new_list:+ }$pkgsel"
147		done
148		SELECTED_PACKAGES="$new_list"
149	done
150}
151
152# f_package_detect_installed
153#
154# Detect installed packages. Currently this searches /var/db/pkg for directory
155# entries and marks each entry as an installed/selected package.
156#
157f_package_detect_installed()
158{
159	local installed package varpkg
160	installed=$( find -s /var/db/pkg -mindepth 1 -maxdepth 1 -type d |
161			sed -e 's:/var/db/pkg/::' )
162	for package in $installed; do
163		f_str2varname $package varpkg
164		export _mark_$varpkg=X # exported for awk(1) ENVIRON[]
165		f_package_select $package
166	done
167}
168
169# f_package_calculate_totals
170#
171# Calculate number of installed/selected packages for each category listed in
172# $PACKAGE_CATEGORIES (the number of installed packages for $category is stored
173# as $_${varcat}_ninstalled -- where $varcat is the product of `f_str2varname
174# $category varcat' -- and number selected packages as $_${varcat}_nselected).
175# Also calculates the total number of installed/selected packages stored as
176# $_All_ninstalled and $_All_nselected.
177#
178# Calculations are peformed by checking "marks". A "mark" is stored as
179# $_mark_$varpkg -- where $varpkg is the product of `f_str2varname $package
180# varpkg'. A mark can be "X" for an installed package, `I' for a package that
181# is marked for installation, "R" for a package that is marked for re-install,
182# and "U" for a package that is marked for uninstallation. If a package mark is
183# NULL or a single space (e.g., " "), the package is considered to be NOT
184# selected (and therefore does not increment the counts calculated herein).
185#
186f_package_calculate_totals()
187{
188	local pkg varpkg mark cat varcat pkgcat n tselected=0 tinstalled=0
189	for cat in $PACKAGE_CATEGORIES; do
190		f_str2varname $cat varcat
191		setvar _${varcat}_ninstalled=0
192		setvar _${varcat}_nselected=0
193	done
194	for pkg in $SELECTED_PACKAGES; do
195		f_str2varname $pkg varpkg
196		mark=
197		f_getvar _mark_$varpkg mark
198		case "$mark" in
199		""|" ") : ;;
200		X) tinstalled=$(( $tinstalled + 1 ));;
201		*) tselected=$(( $tselected + 1 ))
202		esac
203		f_getvar _categories_$varpkg pkgcat
204		for cat in $pkgcat; do
205			f_str2varname $cat varcat
206			case "$mark" in
207			""|" ") : ;;
208			X) debug= f_getvar _${varcat}_ninstalled n
209			   setvar _${varcat}_ninstalled $(( $n + 1 ));;
210			*) debug= f_getvar _${varcat}_nselected n
211			   setvar _${varcat}_nselected $(( $n + 1 ))
212			esac
213		done
214	done
215	_All_nselected=$tselected
216	_All_ninstalled=$tinstalled
217}
218
219# f_package_calculate_rundeps
220#
221# Update package dependencies by first unmarking all dependencies and then
222# re-marking all dependencies of packages marked for either install ("I") or
223# re-install ("R").
224#
225f_package_calculate_rundeps()
226{
227	local pkg varpkg mark rundeps dep vardep
228
229	#
230	# First unmark all the existing run-dependencies
231	#
232	f_dprintf "Unselecting package run-dependencies..."
233	for pkg in $SELECTED_PACKAGES; do
234		f_str2varname $pkg varpkg
235		mark=
236		debug= f_getvar _mark_$varpkg mark
237		# Only unmark if it's marked as a Dependency
238		if [ "$mark" = "D" ]; then
239			f_dprintf "%s unselected" $pkg
240			unset _mark_$varpkg
241			f_package_deselect $pkg
242		fi
243	done
244
245	#
246	# Processes selected packages, adding dependencies
247	#
248	f_dprintf "Re-selecting package run-dependencies..."
249	for pkg in $SELECTED_PACKAGES; do
250		f_str2varname $pkg varpkg
251		mark=
252		debug= f_getvar _mark_$varpkg mark
253		# Skip pkg unless marked for [Re-]Install
254		[ "$mark" = "I" -o "$mark" = "R" ] || continue
255		f_getvar _rundeps_$varpkg rundeps
256		for dep in $rundeps; do
257			f_str2varname $dep vardep
258			mark=
259			debug= f_getvar _mark_$vardep mark
260			# Skip dep if already marked
261			[ "${mark:- }" = " " ] || continue
262			export _mark_$vardep="D"
263			f_package_select $dep
264		done
265	done
266
267	f_dprintf "Finished recalculating dependencies."
268}
269
270# f_package_menu_categories $var_to_set $defaultitem
271#
272# Dislay the menu of package categories, complete with package counts for each
273# category, accents, and other miscellany. If $defaultitem is non-NULL and
274# matches one of the existing menu-items, it will be pre-highlighted in the
275# menu dialog (HINT: Use f_dialog_menutag_fetch() to populate a local variable
276# that is passed as $defaultitem to highlight the user's last selection).
277#
278f_package_menu_categories()
279{
280	local var_to_get="$1" defaultitem="$2" category_list menu_list
281
282	f_package_calculate_rundeps
283		# updates package mark variables and SELECTED_PACKAGES
284	f_package_calculate_totals
285		# creates _{varcat}_ninstalled and _{varcat}_nselected
286
287	debug= f_getvar "$var_to_get" category_list || return $FAILURE
288
289	# Accent the category menu list with ninstalled/nselected
290	eval f_package_accent_category_menu category_list $category_list
291
292	local prompt="$msg_please_select_a_category_to_display"
293	local hline=""
294
295	menu_list="
296		'> $msg_review' '$msg_review_desc' '$msg_review_help'
297		$category_list
298	" # End-Quote
299
300	local height width rows
301	eval f_dialog_menu_with_help_size height width rows \
302	                                  \"\$DIALOG_TITLE\"     \
303	                                  \"\$DIALOG_BACKTITLE\" \
304	                                  \"\$prompt\"           \
305	                                  \"\$hline\"            \
306	                                  $menu_list
307	local menu_choice
308	menu_choice=$( eval $DIALOG \
309			--title \"\$DIALOG_TITLE\"         \
310			--backtitle \"\$DIALOG_BACKTITLE\" \
311			--hline \"\$hline\"                \
312			--item-help                        \
313			--default-item \"\$defaultitem\"   \
314			--ok-label \"$msg_select\"         \
315			--cancel-label \"$msg_cancel\"     \
316			--menu \"\$prompt\"                \
317			$height $width $rows               \
318			$menu_list                         \
319			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
320		)
321	local retval=$?
322	f_dialog_menutag_store -s "$menu_choice"
323	return $retval
324}
325
326# f_package_index_get_page $category $page [$var_to_set [$var_to_get]]
327#
328# Obtain a [potentially cached] page of the INDEX file for a given $category.
329# If $page is 1 and the cache has not yet been generated, the cache-generating
330# function f_index_extract_pages() (above) is called to generate all pages
331# (not just the requested page) in cache before returning the requested page.
332# If $page is not 1 and there is no cached page, failure status is returned.
333#
334f_package_index_get_page()
335{
336	local category="$1" page="$2" var_to_set="$3" var_to_get="$4" varcat
337	f_str2varname "$category" varcat
338	if ! debug= f_getvar "_index_page_${varcat}_$page" $var_to_set &&
339	   [ "$page" = "1" ]
340	then
341		f_show_info "$msg_building_package_menus"
342		local pagesize="$PACKAGE_MENU_PAGESIZE"
343		f_index_extract_pages "${var_to_get:-PACKAGE_INDEX}" \
344			_index_page_${varcat} "$pagesize" "$category"
345		debug= f_getvar _index_page_${varcat}_$page $var_to_set
346
347		# Update category default-item because now we're cached
348		[ $page -eq 1 ] &&
349		    category_defaultitem="${category_defaultitem%\*}*"
350	else
351		return $FAILURE
352	fi
353}
354
355# f_package_menu_select $category [ $page [ $defaultitem ] ]
356#
357# Display list of packages for $category, optionally $page N and with a default
358# item selected. If $page is omitted, the first page is displayed (but this
359# only matters if there are multiple pages; which is determined by the global
360# maximum $PACKAGE_MENU_PAGESIZE).
361#
362# On success, if the user doesn't press ESC or choose Cancel, the environment
363# variable $DIALOG_MENUITEM_$$ will hold the item associated with the chosen
364# tag (accessible through f_dialog_menutag_fetch()).
365#
366f_package_menu_select()
367{
368	local category="$1" page="${2:-1}" defaultitem="$3"
369	local varcat npkgs=0 npages menu_list
370
371	f_isinteger "$page" || return $FAILURE
372
373	f_str2varname "$category" varcat
374	f_package_index_get_page "$category" $page index_page
375
376	# Get number of packages for this category
377	case "$category" in
378	"$msg_all"|"") npkgs="${_npkgs:-0}";;
379	*) f_getvar _npkgs_$varcat npkgs
380	esac
381
382	# Calculate number of pages
383	npages=$(( ${npkgs:=0} / $PACKAGE_MENU_PAGESIZE ))
384
385	# Add a page to the pagecount if not evenly divisible
386	[ $(( $npages * $PACKAGE_MENU_PAGESIZE )) -lt $npkgs ] &&
387		npages=$(( $npages + 1 ))
388
389	# Print some debugging information
390	f_dprintf "f_package_menu_select: category=[%s] npkgs=%u npages=%u" \
391	          "$category" "$npkgs" "$npages"
392
393	local add_prev="" add_next=""
394	local previous_page="$msg_previous_page" next_page="$msg_next_page"
395	if [ $page -gt 1 ]; then
396		add_prev=1
397		# Accent the `Previous Page' item with an asterisk
398		# if the page-before-previous is loaded/cached
399		f_isset _index_page_${varcat}_$(( $page - 1 )) &&
400			previous_page="$previous_page*"
401	fi
402	if [ $page -lt $npages ]; then
403		add_next=1
404		# Accent the `Next Page' item with an asterisk
405		# if the page-after-next is loaded/cached
406		f_isset _index_page_${varcat}_$(( $page + 1 )) &&
407			next_page="$next_page*"
408	fi
409
410	menu_list="
411		${add_prev:+'> $previous_page' '' ${SHOW_DESC:+''}}
412		${add_next:+'> $next_page' '' ${SHOW_DESC:+''}}
413	$(
414		export SHOW_DESC
415		export VALID_VARNAME_CHARS
416		echo "$index_page" | awk -F'|' -v view="port" '
417		BEGIN {
418			valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
419			prefix = ""
420		}
421		{
422			cur_prefix = tolower(substr($1, 1, 1))
423			printf "'\''"
424			if ( prefix != cur_prefix )
425				prefix = cur_prefix
426			else
427				printf " "
428			package = $1
429			if ( view == "port" )
430				desc = $2
431			varpkg = package
432			gsub("[^" valid_chars "]", "_", varpkg)
433			mark = ENVIRON["_mark_" varpkg]
434			if ( ! mark ) mark = " "
435			printf "%s'\'' '\''[%c] %s'\''",
436			       package, mark, desc
437			if ( ENVIRON["SHOW_DESC"] ) {
438				help = $4
439				gsub(/'\''/, "'\''\\'\'\''", help)
440				printf " '\''%s'\''", help
441			}
442			printf "\n"
443		}'
444	)
445		${add_prev:+'> $previous_page' '' ${SHOW_DESC:+''}}
446		${add_next:+'> $next_page' '' ${SHOW_DESC:+''}}
447	" # End-Quote
448
449	local prompt=""
450	# Accept/Translate i18n "All" but other category names must
451	# match tree definitions from INDEX, ports, FTP, etc.
452	case "$category" in
453	"$msg_all"|"") f_category_desc_get "All" prompt ;;
454	            *) f_category_desc_get "$category" prompt ;;
455	esac
456	prompt="$prompt $( printf "$msg_page_of_npages" \
457	                          "$page" "$npages" )"
458
459	local hline="$hline_arrows_tab_punc_enter"
460	local mheight mwidth mrows
461	f_dialog_menu${SHOW_DESC:+_with_help}_size mheight mwidth mrows \
462	        	\"\$DIALOG_TITLE\" \"\$DIALOG_BACKTITLE\" \
463	        	\"\$prompt\" \"\$hline\" $menu_list
464	local iheight iwidth
465	f_dialog_infobox_size iheight iwidth \
466	         	"$DIALOG_TITLE" "$DIALOG_BACKTITLE" \
467	         	"$msg_processing_selection"
468
469	local menu_choice
470	menu_choice=$( eval $DIALOG \
471		--title \"\$DIALOG_TITLE\"         \
472		--backtitle \"\$DIALOG_BACKTITLE\" \
473		--hline \"\$hline\"                \
474		--keep-tite                        \
475		--ok-label \"$msg_select\"         \
476		--cancel-label \"$msg_back\"       \
477		${SHOW_DESC:+--item-help}          \
478		--default-item \"\$defaultitem\"   \
479		--menu \"\$prompt\"                \
480		$mheight $mwidth $mrows            \
481		$menu_list                         \
482		--and-widget                       \
483		${USE_XDIALOG:+--no-buttons}       \
484		--infobox \"\$msg_processing_selection\" \
485		$iheight $iwidth                   \
486		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
487	)
488	local retval=$?
489	f_dialog_data_sanitize menu_choice
490	f_dialog_menutag_store "$menu_choice"
491
492	if [ $retval -eq $SUCCESS ]; then
493		local item
494		item=$( eval f_dialog_menutag2item${SHOW_DESC:+_with_help} \
495		             	\"\$menu_choice\" $menu_list )
496		setvar DIALOG_MENUITEM_$$ "$item"
497	fi
498
499	return $retval
500}
501
502# f_package_menu_deselect $package
503#
504# Display a menu, asking the user what they would like to do with $package
505# with regard to "deselecting" an already installed package. Choices include
506# uninstall, re-install, or cancel (leave $package marked as installed).
507# Returns success if the user does not press ESC or choose Cnacel. Use the
508# f_dialog_menutag_fetch() function upon success to retrieve the user's choice.
509#
510f_package_menu_deselect()
511{
512	local package="$1" prompt menu_list
513	prompt=$( printf "$msg_what_would_you_like_to_do_with" "$package" )
514	local hline="$hline_alnum_arrows_punc_tab_enter"
515	menu_list="
516		'X $msg_installed' '$msg_installed_desc'
517		'R $msg_reinstall' '$msg_reinstall_desc'
518		'U $msg_uninstall' '$msg_uninstall_desc'
519	" # End-Quote
520
521	local height width rows
522	eval f_dialog_menu_size height width rows \
523	                        \"\$DIALOG_TITLE\"     \
524	                        \"\$DIALOG_BACKTITLE\" \
525	                        \"\$prompt\"           \
526	                        \"\$hline\"            \
527	                        $menu_list
528	local menu_choice
529	menu_choice=$( eval $DIALOG \
530		--title \"\$DIALOG_TITLE\"         \
531		--backtitle \"\$DIALOG_BACKTITLE\" \
532		--hline \"\$hline\"                \
533		--ok-label \"$msg_select\"         \
534		--cancel-label \"$msg_cancel\"     \
535		--menu \"\$prompt\"                \
536		$height $width $rows               \
537		$menu_list                         \
538		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
539	)
540	local retval=$?
541	f_dialog_menutag_store -s "$menu_choice"
542	return $retval
543}
544
545# f_package_review
546#
547# Display a review screen, showing selected packages and what they are marked
548# for, before proceeding (if the user does not press ESC or choose Cancel) to
549# operate on each selection. Returns error if no packages have been selected,
550# or the user has pressed ESC, or if they have chosen Cancel.
551#
552f_package_review()
553{
554	local prompt package varpkg mark menu_list=
555	prompt=$( printf "$msg_reviewing_selected_packages" \
556	                 "$_All_nselected" )
557	local hline="$hline_alnum_arrows_punc_tab_enter"
558	f_dprintf "f_package_review: SELECTED_PACKAGES=[%s]" \
559	          "$SELECTED_PACKAGES"
560	for package in $SELECTED_PACKAGES; do
561		mark=
562		f_str2varname "$package" varpkg
563		f_getvar _mark_$varpkg mark
564		[ "$mark" -a ! "${mark#[IRUD]}" ] || continue
565		menu_list="$menu_list
566			'$mark' '$package'
567		" # End-Quote
568	done
569	if [ ! "$menu_list" ]; then
570		f_show_msg "$msg_no_packages_were_selected_for_extraction"
571		return $FAILURE # They might have selected this by accident
572	fi
573	menu_list=$( echo "$menu_list" | sort )
574
575	local height width rows
576	eval f_dialog_menu_size height width rows \
577	                        \"\$DIALOG_TITLE\"     \
578	                        \"\$DIALOG_BACKTITLE\" \
579	                        \"\$prompt\"           \
580	                        \"\$hline\"            \
581	                        $menu_list
582
583	# Show the review menu (ignore menu choice)
584	eval $DIALOG \
585		--title \"\$DIALOG_TITLE\"         \
586		--backtitle \"\$DIALOG_BACKTITLE\" \
587		--hline \"\$hline\"                \
588		--ok-label \"\$msg_proceed\"       \
589		--cancel-label \"\$msg_cancel\"    \
590		--menu \"\$prompt\"                \
591		$height $width $rows               \
592		$menu_list                         \
593		2> /dev/null
594	local retval=$?
595
596	#
597	# XXX
598	#
599	f_show_msg "Coming soon..."
600	#
601	# XXX
602	#
603
604	return $retval
605}
606
607# f_package_config
608#
609# Allow the user to configure packages and install them. Initially, a list of
610# package categories is loaded/displayed. When the user selects a category,
611# the menus for that category are built (unlike sysinstall which built all
612# category menus up-front -- which also took forever, despite the fact that
613# few people visit more than a couple of categories each time).
614#
615f_package_config()
616{
617	# Did we get an INDEX?
618	f_index_initialize packages/INDEX || return $FAILURE
619		# Creates following variables (indirectly via f_index_read())
620		#   CATEGORY_MENU_LIST _categories_{varpkg} _rundeps_{varpkg}
621		#   PACKAGE_CATEGORIES _npkgs
622
623	# Detect installed packages (updates marks/SELECTED_PACKAGES)
624	f_package_detect_installed
625
626	local retval category varcat defaultitem category_defaultitem=""
627	while :; do
628		# Display the list of package categories
629		f_package_menu_categories \
630			CATEGORY_MENU_LIST "$category_defaultitem"
631		retval=$?
632		f_dialog_menutag_fetch category
633		f_dprintf "retval=%u mtag=[%s]" $retval "$category"
634		category_defaultitem="$category"
635
636		[ $retval -eq $SUCCESS ] || break
637
638		# Maybe the user chose an action (like `Review')
639		case "$category" in
640		"> $msg_review")
641			f_package_review && break
642			continue ;;
643		"> "*)
644			continue
645		esac
646
647		# Anything else is a package category
648
649		category=${category# } # Trim leading space if present
650		category=${category%\*} # Trim trailing asterisk if present
651
652		f_str2varname "$category" varcat
653
654		local page package varpkg mark menu_choice
655		while :; do
656			# Display the list of packages for selected category
657			page=1 defaultitem=""
658			f_getvar _defaultitem_$varcat defaultitem
659			f_getvar _defaultpage_$varcat page
660			f_package_menu_select \
661				"$category" "${page:=1}" "$defaultitem"
662			retval=$?
663			f_dialog_menutag_fetch menu_choice
664			f_dprintf "retval=%u mtag=[%s]" $retval "$menu_choice"
665
666			# NOTE: When --and-widget is used only ESC will cause
667			# dialog(1) to return without going to the next widget.
668			# This is alright in our case as we can still detect
669			# the Cancel button because stdout will be NULL.
670			# Alternatively, Xdialog(1) will terminate with 1
671			# if/when Cancel is chosen on any widget.
672			if [ $retval -eq 255 -o ! "$menu_choice" ]; then
673				# User pressed ESC or chose Cancel
674				break
675			elif [ $retval -eq 1 ]; then
676				# Using X11, Xdialog(1) returned 1 for Cancel
677				f_show_msg "%s" "$menu_choice"
678				break
679			elif [ $retval -ne $SUCCESS ]; then
680				# X11-related error occurred using Xdialog(1)
681				f_show_msg "%s" "$menu_choice"
682				break
683			fi
684
685			defaultitem="$menu_choice"
686
687			# NOTE: f_package_menu_select() does not show the
688			# `Previous Page' or `Next Page' items unless needed
689			case "$menu_choice" in
690			"> $msg_previous_page"|"> $msg_previous_page*")
691				page=$(( $page - 1 ))
692				setvar _defaultpage_$varcat $page
693				# Update default-item to match accent that will
694				# be applied by f_package_menu_select(); if the
695				# page-before-prev is cached, add an asterisk.
696				if f_isset \
697					_index_page_${varcat}_$(( $page - 1 ))
698				then
699					defaultitem="${defaultitem%\*}*"
700				else
701					defaultitem="${defaultitem%\*}"
702				fi
703				setvar _defaultitem_$varcat "$defaultitem"
704				continue ;;
705			"> $msg_next_page"|"> $msg_next_page*")
706				page=$(( $page + 1 ))
707				setvar _defaultpage_$varcat $page
708				# Update default-item to match accent that will
709				# be applied by f_package_menu_select(); if the
710				# page-after-next is cached, add an asterisk.
711				if f_isset \
712					_index_page_${varcat}_$(( $page + 1 ))
713				then
714					defaultitem="${defaultitem%\*}*"
715				else
716					defaultitem="${defaultitem%\*}"
717				fi
718				setvar _defaultitem_$varcat "$defaultitem"
719				continue ;;
720			"> "*) # Unknown navigation/action item
721				setvar _defaultpage_$varcat $page
722				continue ;; # Do not treat as a package
723			*)
724				setvar _defaultitem_$varcat "$defaultitem"
725			esac
726
727			# Treat any other selection as a package
728			package="${menu_choice# }" # Trim leading space
729			f_str2varname $package varpkg
730			f_getvar DIALOG_MENUITEM_$$ mark
731			mark="${mark#?}"
732			mark="${mark%%\] *}"
733			case "$mark" in
734			"I")
735				mark=" "
736				f_package_deselect $package
737				;;
738			" "|"D")
739				mark="I"
740				f_package_select $package
741				;;
742			"X"|"R"|"U")
743				f_package_menu_deselect $package || continue
744				f_dialog_menutag_fetch menu_choice
745				case "$menu_choice" in
746				"X $msg_installed")
747					f_package_deselect "$package"
748					mark="X"
749					;;
750				"R $msg_reinstall")
751					f_package_select "$package"
752					mark="R"
753					;;
754				"U $msg_uninstall")
755					f_package_select "$package"
756					mark="U"
757					;;
758				esac
759				;;
760			esac
761			export _mark_$varpkg="$mark"
762				# NOTE: exported for awk(1) ENVIRON[]
763		done
764	done
765}
766
767############################################################ MAIN
768
769f_dprintf "%s: Successfully loaded." packages/packages.subr
770
771fi # ! $_PACKAGES_PACKAGES_SUBR
772