index.subr revision 250410
1235251Seadlerif [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1 2244164Seadler# 3244164Seadler# Copyright (c) 2013 Devin Teske 4235251Seadler# All Rights Reserved. 5235251Seadler# 6235251Seadler# Redistribution and use in source and binary forms, with or without 7235251Seadler# modification, are permitted provided that the following conditions 8235251Seadler# are met: 9235251Seadler# 1. Redistributions of source code must retain the above copyright 10235251Seadler# notice, this list of conditions and the following disclaimer. 11235251Seadler# 2. Redistributions in binary form must reproduce the above copyright 12235251Seadler# notice, this list of conditions and the following disclaimer in the 13235251Seadler# documentation and/or other materials provided with the distribution. 14235251Seadler# 15235251Seadler# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16235251Seadler# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17235251Seadler# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18235251Seadler# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19235251Seadler# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20235251Seadler# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21235251Seadler# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22235251Seadler# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23235251Seadler# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24235251Seadler# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25235251Seadler# SUCH DAMAGE. 26235251Seadler# 27235251Seadler# $FreeBSD: head/usr.sbin/bsdconfig/share/packages/index.subr 250410 2013-05-09 16:09:39Z dteske $ 28235251Seadler# 29235251Seadler############################################################ INCLUDES 30235251Seadler 31235251SeadlerBSDCFG_SHARE="/usr/share/bsdconfig" 32235251Seadler. $BSDCFG_SHARE/common.subr || exit 1 33235251Seadlerf_dprintf "%s: loading includes..." packages/index.subr 34235251Seadlerf_include $BSDCFG_SHARE/device.subr 35235251Seadlerf_include $BSDCFG_SHARE/media/common.subr 36235251Seadlerf_include $BSDCFG_SHARE/strings.subr 37235251Seadler 38235251SeadlerBSDCFG_LIBE="/usr/libexec/bsdconfig" 39235251Seadlerf_include_lang $BSDCFG_LIBE/include/messages.subr 40235251Seadler 41235251Seadler############################################################ GLOBALS 42235251Seadler 43235251SeadlerPACKAGE_INDEX= 44235251Seadler_INDEX_INITTED= 45235251Seadler 46235251Seadler############################################################ FUNCTIONS 47235251Seadler 48235251Seadler# f_index_initialize $path [$var_to_set] 49235251Seadler# 50235251Seadler# Read and initialize the global index. $path is to be relative to the chosen 51235251Seadler# media (not necessarily the filesystem; e.g. FTP) -- this is usually going to 52235251Seadler# be `packages/INDEX'. Returns success unless media cannot be initialized for 53235251Seadler# any reason (e.g. user cancels media selection dialog) or an error occurs. The 54235251Seadler# index is sorted before being loaded into $var_to_set. 55235251Seadler# 56235251Seadler# NOTE: The index is processed with f_index_read() [below] after being loaded. 57235251Seadler# 58235251Seadlerf_index_initialize() 59235251Seadler{ 60235251Seadler local __path="$1" __var_to_set="${2:-PACKAGE_INDEX}" 61235251Seadler 62235251Seadler [ "$_INDEX_INITTED" ] && return $SUCCESS 63235251Seadler [ "$__path" ] || return $FAILURE 64235251Seadler 65235251Seadler # Got any media? 66235251Seadler f_media_verify || return $FAILURE 67235251Seadler 68235251Seadler # Does it move when you kick it? 69235251Seadler f_device_init media || return $FAILURE 70235251Seadler 71235251Seadler f_show_info "$msg_attempting_to_fetch_file_from_selected_media" \ 72235251Seadler "$__path" 73235251Seadler eval "$__var_to_set"='$( f_device_get media "$__path" | sort )' 74235251Seadler if [ $? -ne $SUCCESS ]; then 75235251Seadler f_show_msg "$msg_unable_to_get_file_from_selected_media" \ 76235251Seadler "$__path" 77235251Seadler f_device_shutdown media 78235251Seadler return $FAILURE 79235251Seadler fi 80235251Seadler 81235251Seadler f_show_info "$msg_located_index_now_reading_package_data_from_it" 82235251Seadler if ! f_index_read "$__var_to_set"; then 83235251Seadler f_show_msg "$msg_io_or_format_error_on_index_file" "$__path" 84235251Seadler return $FAILURE 85235251Seadler fi 86235251Seadler 87235251Seadler _INDEX_INITTED=1 88235251Seadler return $SUCCESS 89235251Seadler} 90235251Seadler 91235251Seadler# f_index_read [$var_to_get] 92235251Seadler# 93235251Seadler# Process the INDEX file (contents contained in $var_to_get) and... 94235251Seadler# 95235251Seadler# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts 96235251Seadler# 2. For convenience, create $_npkgs holding the total number of all packages 97235251Seadler# 3. extract associative categories for each package into $_categories_$varpkg 98235251Seadler# 4. extract runtime dependencies for each package into $_rundeps_$varpkg 99235251Seadler# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES 100235251Seadler# 6. create $_npkgs_$varcat holding the total number of packages in category 101235251Seadler# 102235251Seadler# NOTE: $varpkg is the product of f_str2varname $package varpkg 103235251Seadler# NOTE: $package is the name as it appears in the INDEX (no archive suffix) 104235251Seadler# NOTE: We only show categories for which there are at least one package. 105235251Seadler# NOTE: $varcat is the product of f_str2varname $category varcat 106235251Seadler# 107235251Seadlerf_index_read() 108235251Seadler{ 109235251Seadler local var_to_get="${1:-PACKAGE_INDEX}" 110235251Seadler 111235251Seadler # Export variables required by awk(1) below 112235251Seadler export msg_no_description_provided 113235251Seadler export msg_all msg_all_desc 114235251Seadler export VALID_VARNAME_CHARS 115235251Seadler 116235251Seadler eval "$( debug= f_getvar "$var_to_get" | awk -F'|' ' 117235251Seadler function asorti(src, dest) 118235251Seadler { 119235251Seadler # Copy src indices to dest and calculate array length 120235251Seadler nitems = 0; for (i in src) dest[++nitems] = i 121235251Seadler 122235251Seadler # Sort the array of indices (dest) using insertion sort method 123235251Seadler for (i = 1; i <= nitems; k = i++) 124235251Seadler { 125235251Seadler idx = dest[i] 126235251Seadler while ((k > 0) && (dest[k] > idx)) 127235251Seadler { 128235251Seadler dest[k+1] = dest[k] 129235251Seadler k-- 130235251Seadler } 131235251Seadler dest[k+1] = idx 132235251Seadler } 133235251Seadler 134235251Seadler return nitems 135235251Seadler } 136235251Seadler function print_category(category, npkgs, desc) 137235251Seadler { 138235251Seadler cat = category 139235251Seadler # Accent the category if the first page has been 140235251Seadler # cached (also acting as a visitation indicator) 141235251Seadler if ( ENVIRON["_index_page_" varcat "_1"] ) 142 cat = cat "*" 143 printf "'\''%s'\'' '\''%s packages'\'' '\''%s'\''\n", 144 cat, npkgs, desc 145 } 146 BEGIN { 147 valid_chars = ENVIRON["VALID_VARNAME_CHARS"] 148 default_desc = ENVIRON["msg_no_description_provided"] 149 tpkgs = 0 150 prefix = "" 151 } 152 { 153 tpkgs++ 154 varpkg = $1 155 gsub("[^" valid_chars "]", "_", varpkg) 156 print "_categories_" varpkg "=\"" $7 "\"" 157 split($7, pkg_categories, /[[:space:]]+/) 158 for (pkg_category in pkg_categories) 159 categories[pkg_categories[pkg_category]]++ 160 print "_rundeps_" varpkg "=\"" $9 "\"" 161 } 162 END { 163 print "_npkgs=" tpkgs # For convenience, total package count 164 165 n = asorti(categories, categories_sorted) 166 167 # Produce package counts for each category 168 for (i = 1; i <= n; i++) 169 { 170 cat = varcat = categories_sorted[i] 171 npkgs = categories[cat] 172 gsub("[^" valid_chars "]", "_", varcat) 173 print "_npkgs_" varcat "=\"" npkgs "\"" 174 } 175 176 # Create menu list and generate list of categories at same time 177 print "CATEGORY_MENU_LIST=\"" 178 print_category(ENVIRON["msg_all"], tpkgs, 179 ENVIRON["msg_all_desc"]) 180 category_list = "" 181 for (i = 1; i <= n; i++) 182 { 183 cat = varcat = categories_sorted[i] 184 npkgs = categories[cat] 185 cur_prefix = tolower(substr(cat, 1, 1)) 186 if ( prefix != cur_prefix ) 187 prefix = cur_prefix 188 else 189 cat = " " cat 190 gsub("[^" valid_chars "]", "_", varcat) 191 desc = ENVIRON["_category_" varcat] 192 if ( ! desc ) desc = default_desc 193 print_category(cat, npkgs, desc) 194 category_list = category_list " " cat 195 } 196 print "\"" 197 198 # Produce the list of categories (calculated in above block) 199 sub(/^ /, "", category_list) 200 print "PACKAGE_CATEGORIES=\"" category_list "\"" 201 202 }' )" # End-Quote 203} 204 205# f_index_extract_pages $var_to_get $var_basename $pagesize [$category] 206# 207# Extracts the package INDEX into a series of sequential variables 208# corresponding to "pages" containing up to $pagesize packages. The package 209# INDEX data must be contained in the variable $var_to_get. The extracted pages 210# are stored in variables ${var_basename}_# -- where "#" is a the page number. 211# If $category is set, only packages for that category are extracted. 212# Otherwise, if $category is "All", missing, or NULL, all packages are 213# extracted and no filtering is done. 214# 215f_index_extract_pages() 216{ 217 local var_to_get="$1" var_basename="$2" pagesize="$3" 218 local category="$4" # Optional 219 220 eval "$( 221 debug= f_getvar "$var_to_get" | awk -F'|' \ 222 -v cat="$category" \ 223 -v pagesize="$pagesize" \ 224 -v var_basename="$var_basename" ' 225 BEGIN { n = page = 0 } 226 /'\''/{ gsub(/'\''/, "'\''\\'\'\''") } 227 { 228 if ( cat !~ /(^$|^All$)/ && $7 !~ \ 229 "(^|[[:space:]])" cat "([[:space:]]|$)" ) next 230 starting_new_page = (n++ == (pagesize * page)) 231 if ( starting_new_page ) 232 printf "%s%s", ( n > 1 ? "'\''\n" : "" ), 233 var_basename "_" ++page "='\''" 234 printf "%s%s", ( starting_new_page ? "" : "\n" ), $0 235 } 236 END { if ( n > 0 ) print "'\''" }' 237 )" 238} 239 240############################################################ MAIN 241 242f_dprintf "%s: Successfully loaded." packages/index.subr 243 244fi # ! $_PACKAGES_INDEX_SUBR 245