index.subr revision 252745
126213Swpaulif [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1
226213Swpaul#
326213Swpaul# Copyright (c) 2013 Devin Teske
426213Swpaul# All Rights Reserved.
526213Swpaul#
626213Swpaul# Redistribution and use in source and binary forms, with or without
726213Swpaul# modification, are permitted provided that the following conditions
826213Swpaul# are met:
926213Swpaul# 1. Redistributions of source code must retain the above copyright
1026213Swpaul#    notice, this list of conditions and the following disclaimer.
1126213Swpaul# 2. Redistributions in binary form must reproduce the above copyright
1226213Swpaul#    notice, this list of conditions and the following disclaimer in the
1326213Swpaul#    documentation and/or other materials provided with the distribution.
1426213Swpaul#
1526213Swpaul# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1626213Swpaul# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
1726213Swpaul# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1826213Swpaul# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1926213Swpaul# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2026213Swpaul# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2126213Swpaul# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2226213Swpaul# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2326213Swpaul# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2426213Swpaul# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2526213Swpaul# SUCH DAMAGE.
2626213Swpaul#
2726213Swpaul# $FreeBSD: head/usr.sbin/bsdconfig/share/packages/index.subr 252745 2013-07-05 01:44:59Z dteske $
2826213Swpaul#
2926213Swpaul############################################################ INCLUDES
3026213Swpaul
3126213SwpaulBSDCFG_SHARE="/usr/share/bsdconfig"
3226213Swpaul. $BSDCFG_SHARE/common.subr || exit 1
3326213Swpaulf_dprintf "%s: loading includes..." packages/index.subr
3426213Swpaulf_include $BSDCFG_SHARE/device.subr
3526213Swpaulf_include $BSDCFG_SHARE/media/common.subr
3626213Swpaulf_include $BSDCFG_SHARE/strings.subr
3726213Swpaul
3826213SwpaulBSDCFG_LIBE="/usr/libexec/bsdconfig"
3984220Sdillonf_include_lang $BSDCFG_LIBE/include/messages.subr
4084220Sdillon
4184220Sdillon############################################################ GLOBALS
4226213Swpaul
4326213SwpaulPACKAGE_INDEX=
4426213Swpaul_INDEX_INITTED=
4526213Swpaul
4626213Swpaul############################################################ FUNCTIONS
4726213Swpaul
4826213Swpaul# f_index_initialize $path [$var_to_set]
4926213Swpaul#
5026213Swpaul# Read and initialize the global index. $path is to be relative to the chosen
5126213Swpaul# media (not necessarily the filesystem; e.g. FTP) -- this is usually going to
5226213Swpaul# be `packages/INDEX'. Returns success unless media cannot be initialized for
5326213Swpaul# any reason (e.g. user cancels media selection dialog) or an error occurs. The
5426213Swpaul# index is sorted before being loaded into $var_to_set.
5526213Swpaul#
5626213Swpaul# NOTE: The index is processed with f_index_read() [below] after being loaded.
5726213Swpaul#
5890298Sdesf_index_initialize()
5990298Sdes{
6026213Swpaul	local __path="$1" __var_to_set="${2:-PACKAGE_INDEX}"
6126213Swpaul
6226213Swpaul	[ "$_INDEX_INITTED" ] && return $SUCCESS
6326213Swpaul	[ "$__path" ] || return $FAILURE
6426213Swpaul
6526213Swpaul	# Got any media?
6626213Swpaul	f_media_verify || return $FAILURE
6726213Swpaul
6826213Swpaul	# Does it move when you kick it?
6926213Swpaul	f_device_init media || return $FAILURE
7026213Swpaul
7126213Swpaul	f_show_info "$msg_attempting_to_fetch_file_from_selected_media" \
7226213Swpaul	            "$__path"
7326213Swpaul	eval "$__var_to_set"='$( f_device_get media "$__path" )'
7426213Swpaul	if [ $? -ne $SUCCESS ]; then
7526213Swpaul		f_show_msg "$msg_unable_to_get_file_from_selected_media" \
7626213Swpaul		           "$__path"
7726213Swpaul		f_device_shutdown media
7826213Swpaul		return $FAILURE
7926213Swpaul	fi
8026213Swpaul	eval "$__var_to_set"='$( debug= f_getvar "$__var_to_set" | sort )'
8126213Swpaul
8226213Swpaul	f_show_info "$msg_located_index_now_reading_package_data_from_it"
8326213Swpaul	if ! f_index_read "$__var_to_set"; then
8490297Sdes		f_show_msg "$msg_io_or_format_error_on_index_file" "$__path"
8526213Swpaul		return $FAILURE
8626213Swpaul	fi
8726213Swpaul
8826213Swpaul	_INDEX_INITTED=1
8926213Swpaul	return $SUCCESS
9026213Swpaul}
9126213Swpaul
9226213Swpaul# f_index_read [$var_to_get]
9326213Swpaul#
9426213Swpaul# Process the INDEX file (contents contained in $var_to_get) and...
9526213Swpaul#
9626213Swpaul# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts
9726213Swpaul# 2. For convenience, create $_npkgs holding the total number of all packages
9826213Swpaul# 3. extract associative categories for each package into $_categories_$varpkg
9926213Swpaul# 4. extract runtime dependencies for each package into $_rundeps_$varpkg
10026213Swpaul# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES
10126213Swpaul# 6. create $_npkgs_$varcat holding the total number of packages in category
10226213Swpaul#
10326213Swpaul# NOTE: $varpkg is the product of f_str2varname $package varpkg
10426213Swpaul# NOTE: $package is the name as it appears in the INDEX (no archive suffix)
10526213Swpaul# NOTE: We only show categories for which there are at least one package.
10626213Swpaul# NOTE: $varcat is the product of f_str2varname $category varcat
10726213Swpaul#
10826213Swpaulf_index_read()
10926213Swpaul{
11026213Swpaul	local var_to_get="${1:-PACKAGE_INDEX}"
11126213Swpaul
11226213Swpaul	# Export variables required by awk(1) below
11326213Swpaul	export msg_no_description_provided
11426213Swpaul	export msg_all msg_all_desc
11526213Swpaul	export VALID_VARNAME_CHARS
11626213Swpaul	export msg_packages
11726213Swpaul
11826213Swpaul	eval "$( debug= f_getvar "$var_to_get" | awk -F'|' '
11926213Swpaul	function asorti(src, dest)
12026213Swpaul	{
12190297Sdes		# Copy src indices to dest and calculate array length
12226213Swpaul		nitems = 0; for (i in src) dest[++nitems] = i
12326213Swpaul
12426213Swpaul		# Sort the array of indices (dest) using insertion sort method
12526213Swpaul		for (i = 1; i <= nitems; k = i++)
12626213Swpaul		{
12726213Swpaul			idx = dest[i]
12826213Swpaul			while ((k > 0) && (dest[k] > idx))
129121529Speter			{
130121529Speter				dest[k+1] = dest[k]
131121529Speter				k--
13226213Swpaul			}
13326213Swpaul			dest[k+1] = idx
13426213Swpaul		}
13526213Swpaul
13626213Swpaul		return nitems
13726213Swpaul	}
13826213Swpaul	function print_category(category, npkgs, desc)
13926213Swpaul	{
14026213Swpaul		cat = category
14126213Swpaul		# Accent the category if the first page has been
14226213Swpaul		# cached (also acting as a visitation indicator)
14326213Swpaul		if ( ENVIRON["_index_page_" varcat "_1"] )
14426213Swpaul			cat = cat "*"
14526213Swpaul		printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n",
146121529Speter		       cat, npkgs, desc
147121529Speter	}
148121529Speter	BEGIN {
14926213Swpaul		valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
15026213Swpaul		default_desc = ENVIRON["msg_no_description_provided"]
15126213Swpaul		packages = ENVIRON["msg_packages"]
15226213Swpaul		tpkgs = 0
15326213Swpaul		prefix = ""
15426213Swpaul	}
15526213Swpaul	{
15626213Swpaul		tpkgs++
15726213Swpaul		varpkg = $1
15826213Swpaul		gsub("[^" valid_chars "]", "_", varpkg)
15926213Swpaul		print "_categories_" varpkg "=\"" $7 "\""
16026213Swpaul		split($7, pkg_categories, /[[:space:]]+/)
161121529Speter		for (pkg_category in pkg_categories)
162121529Speter			categories[pkg_categories[pkg_category]]++
163121529Speter		print "_rundeps_" varpkg "=\"" $9 "\""
16426213Swpaul	}
16526213Swpaul	END {
16626213Swpaul		print "_npkgs=" tpkgs # For convenience, total package count
16726213Swpaul
16826213Swpaul		n = asorti(categories, categories_sorted)
16926213Swpaul
17026213Swpaul		# Produce package counts for each category
17126213Swpaul		for (i = 1; i <= n; i++)
17226213Swpaul		{
17326213Swpaul			cat = varcat = categories_sorted[i]
17426213Swpaul			npkgs = categories[cat]
17526213Swpaul			gsub("[^" valid_chars "]", "_", varcat)
17626213Swpaul			print "_npkgs_" varcat "=\"" npkgs "\""
17726213Swpaul		}
178121529Speter
179121529Speter		# Create menu list and generate list of categories at same time
180121529Speter		print "CATEGORY_MENU_LIST=\""
18126213Swpaul		print_category(ENVIRON["msg_all"], tpkgs,
18226213Swpaul		               ENVIRON["msg_all_desc"])
18326213Swpaul		category_list = ""
18426213Swpaul		for (i = 1; i <= n; i++)
18526213Swpaul		{
18626213Swpaul			cat = varcat = categories_sorted[i]
18726213Swpaul			npkgs = categories[cat]
18826213Swpaul			cur_prefix = tolower(substr(cat, 1, 1))
18926213Swpaul			if ( prefix != cur_prefix )
19026213Swpaul				prefix = cur_prefix
19126213Swpaul			else
19226213Swpaul				cat = " " cat
19326213Swpaul			gsub("[^" valid_chars "]", "_", varcat)
19426213Swpaul			desc = ENVIRON["_category_" varcat]
19526213Swpaul			if ( ! desc ) desc = default_desc
19626213Swpaul			print_category(cat, npkgs, desc)
19726213Swpaul			category_list = category_list " " cat
19826213Swpaul		}
19926213Swpaul		print "\""
200
201		# Produce the list of categories (calculated in above block)
202		sub(/^ /, "", category_list)
203		print "PACKAGE_CATEGORIES=\"" category_list "\""
204
205	}' )" # End-Quote
206}
207
208# f_index_extract_pages $var_to_get $var_basename $pagesize [$category]
209#
210# Extracts the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is
211# NULL; but should not be missing) into a series of sequential variables
212# corresponding to "pages" containing up to $pagesize packages. The package
213# INDEX data must be contained in the variable $var_to_get. The extracted pages
214# are stored in variables ${var_basename}_# -- where "#" is a the page number.
215# If $category is set, only packages for that category are extracted.
216# Otherwise, if $category is "All", missing, or NULL, all packages are
217# extracted and no filtering is done.
218#
219f_index_extract_pages()
220{
221	local var_to_get="${1:-PACKAGE_INDEX}" var_basename="$2" pagesize="$3"
222	local category="$4" # Optional
223
224	eval "$(
225		debug= f_getvar "$var_to_get" | awk -F'|' \
226			-v cat="$category" \
227			-v pagesize="$pagesize" \
228			-v var_basename="$var_basename" \
229			-v i18n_all="$msg_all" '
230		BEGIN { n = page = 0 }
231		/'\''/{ gsub(/'\''/, "'\''\\'\'\''") }
232		{
233			if ( cat !~ "(^$|^" i18n_all "$)" && $7 !~ \
234			     "(^|[[:space:]])" cat "([[:space:]]|$)" ) next
235			starting_new_page = (n++ == (pagesize * page))
236			if ( starting_new_page )
237				printf "%s%s", ( n > 1 ? "'\''\n" : "" ),
238				       var_basename "_" ++page "='\''"
239			printf "%s%s", ( starting_new_page ? "" : "\n" ), $0
240		}
241		END { if ( n > 0 ) print "'\''" }'
242	)"
243}
244
245# f_index_search $var_to_get $name [$var_to_set]
246#
247# Search the package INDEX ($PACKAGE_INDEX by default if/when $var_to)get is
248# NULL; but should not be missing) for $name, returning the first match.
249# Matches are strict (not regular expressions) and must match the beginning
250# portion of the package name to be considered a match. If $var_to_set is
251# missing or NULL, output is sent to standard output. If a match is found,
252# returns success; otherwise failure.
253#
254f_index_search()
255{
256	local __var_to_get="${1:-PACKAGE_INDEX}" __pkg_basename="$2"
257	local __var_to_set="$3"
258
259	f_dprintf "f_index_search: Searching package data (in %s) for %s" \
260	          "$__var_to_get" "$__pkg_basename"
261
262	local __pkg=
263	__pkg=$( debug= f_getvar "$__var_to_get" |
264			awk -F'|' -v basename="$__pkg_basename" '
265		BEGIN { n = length(basename) }
266		substr($1, 0, n) == basename { print $1; exit }
267	' )
268	if [ ! "$__pkg" ]; then
269		f_dprintf "f_index_search: No packages matching %s found" \
270		          "$__pkg_basename"
271		return $FAILURE
272	fi
273
274	f_dprintf "f_index_search: Found package %s" "$__pkg"
275	if [ "$__var_to_set" ]; then
276		setvar "$__var_to_set" "$__pkg"
277	else
278		echo "$__pkg"
279	fi
280	return $SUCCESS
281}
282
283############################################################ MAIN
284
285f_dprintf "%s: Successfully loaded." packages/index.subr
286
287fi # ! $_PACKAGES_INDEX_SUBR
288