1 if [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_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..." packages/index.subr
34 f_include $BSDCFG_SHARE/device.subr
35 f_include $BSDCFG_SHARE/media/common.subr
36 f_include $BSDCFG_SHARE/strings.subr
38 BSDCFG_LIBE="/usr/libexec/bsdconfig"
39 f_include_lang $BSDCFG_LIBE/include/messages.subr
41 ############################################################ GLOBALS
46 ############################################################ FUNCTIONS
48 # f_index_initialize $path [$var_to_set]
50 # Read and initialize the global index. $path is to be relative to the chosen
51 # media (not necessarily the filesystem; e.g. FTP) -- this is usually going to
52 # be `packages/INDEX'. Returns success unless media cannot be initialized for
53 # any reason (e.g. user cancels media selection dialog) or an error occurs. The
54 # index is sorted before being loaded into $var_to_set.
56 # NOTE: The index is processed with f_index_read() [below] after being loaded.
60 local __path="$1" __var_to_set="${2:-PACKAGE_INDEX}"
62 [ "$_INDEX_INITTED" ] && return $SUCCESS
63 [ "$__path" ] || return $FAILURE
66 f_media_verify || return $FAILURE
68 # Does it move when you kick it?
69 f_device_init device_media || return $FAILURE
71 f_show_info "$msg_attempting_to_fetch_file_from_selected_media" \
73 eval "$__var_to_set"='$( f_device_get device_media "$__path" )'
74 if [ $? -ne $SUCCESS ]; then
75 f_show_msg "$msg_unable_to_get_file_from_selected_media" \
77 f_device_shutdown device_media
80 eval "$__var_to_set"='$( debug= f_getvar "$__var_to_set" | sort )'
82 f_show_info "$msg_located_index_now_reading_package_data_from_it"
83 if ! f_index_read "$__var_to_set"; then
84 f_show_msg "$msg_io_or_format_error_on_index_file" "$__path"
92 # f_index_read [$var_to_get]
94 # Process the INDEX file (contents contained in $var_to_get) and...
96 # 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts
97 # 2. For convenience, create $_npkgs holding the total number of all packages
98 # 3. extract associative categories for each package into $_categories_$varpkg
99 # 4. extract runtime dependencies for each package into $_rundeps_$varpkg
100 # 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES
101 # 6. create $_npkgs_$varcat holding the total number of packages in category
103 # NOTE: $varpkg is the product of f_str2varname $package varpkg
104 # NOTE: $package is the name as it appears in the INDEX (no archive suffix)
105 # NOTE: We only show categories for which there are at least one package.
106 # NOTE: $varcat is the product of f_str2varname $category varcat
110 local var_to_get="${1:-PACKAGE_INDEX}"
112 # Export variables required by awk(1) below
113 export msg_no_description_provided
114 export msg_all msg_all_desc
115 export VALID_VARNAME_CHARS
118 eval "$( debug= f_getvar "$var_to_get" | awk -F'|' '
119 function asorti(src, dest)
121 # Copy src indices to dest and calculate array length
122 nitems = 0; for (i in src) dest[++nitems] = i
124 # Sort the array of indices (dest) using insertion sort method
125 for (i = 1; i <= nitems; k = i++)
128 while ((k > 0) && (dest[k] > idx))
138 function print_category(category, npkgs, desc)
141 # Accent the category if the first page has been
142 # cached (also acting as a visitation indicator)
143 if ( ENVIRON["_index_page_" varcat "_1"] )
145 printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n",
149 valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
150 default_desc = ENVIRON["msg_no_description_provided"]
151 packages = ENVIRON["msg_packages"]
158 gsub("[^" valid_chars "]", "_", varpkg)
159 print "_categories_" varpkg "=\"" $7 "\""
160 split($7, pkg_categories, /[[:space:]]+/)
161 for (pkg_category in pkg_categories)
162 categories[pkg_categories[pkg_category]]++
163 print "_rundeps_" varpkg "=\"" $9 "\""
166 print "_npkgs=" tpkgs # For convenience, total package count
168 n = asorti(categories, categories_sorted)
170 # Produce package counts for each category
171 for (i = 1; i <= n; i++)
173 cat = varcat = categories_sorted[i]
174 npkgs = categories[cat]
175 gsub("[^" valid_chars "]", "_", varcat)
176 print "_npkgs_" varcat "=\"" npkgs "\""
179 # Create menu list and generate list of categories at same time
180 print "CATEGORY_MENU_LIST=\""
181 print_category(ENVIRON["msg_all"], tpkgs,
182 ENVIRON["msg_all_desc"])
184 for (i = 1; i <= n; i++)
186 cat = varcat = categories_sorted[i]
187 npkgs = categories[cat]
188 cur_prefix = tolower(substr(cat, 1, 1))
189 if ( prefix != cur_prefix )
193 gsub("[^" valid_chars "]", "_", varcat)
194 desc = ENVIRON["_category_" varcat]
195 if ( ! desc ) desc = default_desc
196 print_category(cat, npkgs, desc)
197 category_list = category_list " " cat
201 # Produce the list of categories (calculated in above block)
202 sub(/^ /, "", category_list)
203 print "PACKAGE_CATEGORIES=\"" category_list "\""
208 # f_index_extract_pages $var_to_get $var_basename $pagesize [$category]
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.
219 f_index_extract_pages()
221 local var_to_get="${1:-PACKAGE_INDEX}" var_basename="$2" pagesize="$3"
222 local category="$4" # Optional
225 debug= f_getvar "$var_to_get" | awk -F'|' \
227 -v pagesize="$pagesize" \
228 -v var_basename="$var_basename" \
229 -v i18n_all="$msg_all" '
230 BEGIN { n = page = 0 }
231 /'\''/{ gsub(/'\''/, "'\''\\'\'\''") }
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
241 END { if ( n > 0 ) print "'\''" }'
245 # f_index_search $var_to_get $name [$var_to_set]
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.
256 local __var_to_get="${1:-PACKAGE_INDEX}" __pkg_basename="$2"
257 local __var_to_set="$3"
259 f_dprintf "f_index_search: Searching package data (in %s) for %s" \
260 "$__var_to_get" "$__pkg_basename"
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 }
268 if [ ! "$__pkg" ]; then
269 f_dprintf "f_index_search: No packages matching %s found" \
274 f_dprintf "f_index_search: Found package %s" "$__pkg"
275 if [ "$__var_to_set" ]; then
276 setvar "$__var_to_set" "$__pkg"
283 ############################################################ MAIN
285 f_dprintf "%s: Successfully loaded." packages/index.subr
287 fi # ! $_PACKAGES_INDEX_SUBR