index.subr revision 252745
1250323Sdteskeif [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1 2250323Sdteske# 3250323Sdteske# Copyright (c) 2013 Devin Teske 4250323Sdteske# All Rights Reserved. 5250323Sdteske# 6250323Sdteske# Redistribution and use in source and binary forms, with or without 7250323Sdteske# modification, are permitted provided that the following conditions 8250323Sdteske# are met: 9250323Sdteske# 1. Redistributions of source code must retain the above copyright 10250323Sdteske# notice, this list of conditions and the following disclaimer. 11250323Sdteske# 2. Redistributions in binary form must reproduce the above copyright 12250323Sdteske# notice, this list of conditions and the following disclaimer in the 13250323Sdteske# documentation and/or other materials provided with the distribution. 14250323Sdteske# 15250323Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16250323Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17250323Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18250323Sdteske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19250323Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20250323Sdteske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21250323Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22250323Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23250323Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24250323Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25250323Sdteske# SUCH DAMAGE. 26250323Sdteske# 27250323Sdteske# $FreeBSD: head/usr.sbin/bsdconfig/share/packages/index.subr 252745 2013-07-05 01:44:59Z dteske $ 28250323Sdteske# 29250323Sdteske############################################################ INCLUDES 30250323Sdteske 31250323SdteskeBSDCFG_SHARE="/usr/share/bsdconfig" 32250323Sdteske. $BSDCFG_SHARE/common.subr || exit 1 33250323Sdteskef_dprintf "%s: loading includes..." packages/index.subr 34250323Sdteskef_include $BSDCFG_SHARE/device.subr 35250323Sdteskef_include $BSDCFG_SHARE/media/common.subr 36250323Sdteskef_include $BSDCFG_SHARE/strings.subr 37250323Sdteske 38250323SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig" 39250323Sdteskef_include_lang $BSDCFG_LIBE/include/messages.subr 40250323Sdteske 41250323Sdteske############################################################ GLOBALS 42250323Sdteske 43250323SdteskePACKAGE_INDEX= 44250323Sdteske_INDEX_INITTED= 45250323Sdteske 46250323Sdteske############################################################ FUNCTIONS 47250323Sdteske 48250323Sdteske# f_index_initialize $path [$var_to_set] 49250323Sdteske# 50250323Sdteske# Read and initialize the global index. $path is to be relative to the chosen 51250323Sdteske# media (not necessarily the filesystem; e.g. FTP) -- this is usually going to 52250323Sdteske# be `packages/INDEX'. Returns success unless media cannot be initialized for 53250323Sdteske# any reason (e.g. user cancels media selection dialog) or an error occurs. The 54250323Sdteske# index is sorted before being loaded into $var_to_set. 55250323Sdteske# 56250323Sdteske# NOTE: The index is processed with f_index_read() [below] after being loaded. 57250323Sdteske# 58250323Sdteskef_index_initialize() 59250323Sdteske{ 60250323Sdteske local __path="$1" __var_to_set="${2:-PACKAGE_INDEX}" 61250323Sdteske 62250323Sdteske [ "$_INDEX_INITTED" ] && return $SUCCESS 63250323Sdteske [ "$__path" ] || return $FAILURE 64250323Sdteske 65250323Sdteske # Got any media? 66250323Sdteske f_media_verify || return $FAILURE 67250323Sdteske 68250323Sdteske # Does it move when you kick it? 69250323Sdteske f_device_init media || return $FAILURE 70250323Sdteske 71250323Sdteske f_show_info "$msg_attempting_to_fetch_file_from_selected_media" \ 72250323Sdteske "$__path" 73251276Sdteske eval "$__var_to_set"='$( f_device_get media "$__path" )' 74250323Sdteske if [ $? -ne $SUCCESS ]; then 75250323Sdteske f_show_msg "$msg_unable_to_get_file_from_selected_media" \ 76250323Sdteske "$__path" 77250323Sdteske f_device_shutdown media 78250323Sdteske return $FAILURE 79250323Sdteske fi 80251276Sdteske eval "$__var_to_set"='$( debug= f_getvar "$__var_to_set" | sort )' 81250323Sdteske 82250323Sdteske f_show_info "$msg_located_index_now_reading_package_data_from_it" 83250323Sdteske if ! f_index_read "$__var_to_set"; then 84250323Sdteske f_show_msg "$msg_io_or_format_error_on_index_file" "$__path" 85250323Sdteske return $FAILURE 86250323Sdteske fi 87250323Sdteske 88250323Sdteske _INDEX_INITTED=1 89250323Sdteske return $SUCCESS 90250323Sdteske} 91250323Sdteske 92250323Sdteske# f_index_read [$var_to_get] 93250323Sdteske# 94250323Sdteske# Process the INDEX file (contents contained in $var_to_get) and... 95250323Sdteske# 96250323Sdteske# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts 97250323Sdteske# 2. For convenience, create $_npkgs holding the total number of all packages 98250323Sdteske# 3. extract associative categories for each package into $_categories_$varpkg 99250323Sdteske# 4. extract runtime dependencies for each package into $_rundeps_$varpkg 100250323Sdteske# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES 101250323Sdteske# 6. create $_npkgs_$varcat holding the total number of packages in category 102250323Sdteske# 103250323Sdteske# NOTE: $varpkg is the product of f_str2varname $package varpkg 104250323Sdteske# NOTE: $package is the name as it appears in the INDEX (no archive suffix) 105250323Sdteske# NOTE: We only show categories for which there are at least one package. 106250323Sdteske# NOTE: $varcat is the product of f_str2varname $category varcat 107250323Sdteske# 108250323Sdteskef_index_read() 109250323Sdteske{ 110250323Sdteske local var_to_get="${1:-PACKAGE_INDEX}" 111250323Sdteske 112250323Sdteske # Export variables required by awk(1) below 113250323Sdteske export msg_no_description_provided 114250323Sdteske export msg_all msg_all_desc 115250323Sdteske export VALID_VARNAME_CHARS 116250538Sdteske export msg_packages 117250323Sdteske 118250323Sdteske eval "$( debug= f_getvar "$var_to_get" | awk -F'|' ' 119250323Sdteske function asorti(src, dest) 120250323Sdteske { 121250323Sdteske # Copy src indices to dest and calculate array length 122250323Sdteske nitems = 0; for (i in src) dest[++nitems] = i 123250323Sdteske 124250323Sdteske # Sort the array of indices (dest) using insertion sort method 125250323Sdteske for (i = 1; i <= nitems; k = i++) 126250323Sdteske { 127250323Sdteske idx = dest[i] 128250323Sdteske while ((k > 0) && (dest[k] > idx)) 129250323Sdteske { 130250323Sdteske dest[k+1] = dest[k] 131250323Sdteske k-- 132250323Sdteske } 133250323Sdteske dest[k+1] = idx 134250323Sdteske } 135250323Sdteske 136250323Sdteske return nitems 137250323Sdteske } 138250323Sdteske function print_category(category, npkgs, desc) 139250323Sdteske { 140250323Sdteske cat = category 141250323Sdteske # Accent the category if the first page has been 142250323Sdteske # cached (also acting as a visitation indicator) 143250323Sdteske if ( ENVIRON["_index_page_" varcat "_1"] ) 144250323Sdteske cat = cat "*" 145250538Sdteske printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n", 146250323Sdteske cat, npkgs, desc 147250323Sdteske } 148250323Sdteske BEGIN { 149250323Sdteske valid_chars = ENVIRON["VALID_VARNAME_CHARS"] 150250323Sdteske default_desc = ENVIRON["msg_no_description_provided"] 151250538Sdteske packages = ENVIRON["msg_packages"] 152250410Sdteske tpkgs = 0 153250323Sdteske prefix = "" 154250323Sdteske } 155250323Sdteske { 156250410Sdteske tpkgs++ 157250323Sdteske varpkg = $1 158250323Sdteske gsub("[^" valid_chars "]", "_", varpkg) 159250323Sdteske print "_categories_" varpkg "=\"" $7 "\"" 160250323Sdteske split($7, pkg_categories, /[[:space:]]+/) 161250323Sdteske for (pkg_category in pkg_categories) 162250323Sdteske categories[pkg_categories[pkg_category]]++ 163250323Sdteske print "_rundeps_" varpkg "=\"" $9 "\"" 164250323Sdteske } 165250323Sdteske END { 166250410Sdteske print "_npkgs=" tpkgs # For convenience, total package count 167250323Sdteske 168250323Sdteske n = asorti(categories, categories_sorted) 169250323Sdteske 170250323Sdteske # Produce package counts for each category 171250323Sdteske for (i = 1; i <= n; i++) 172250323Sdteske { 173250323Sdteske cat = varcat = categories_sorted[i] 174250323Sdteske npkgs = categories[cat] 175250323Sdteske gsub("[^" valid_chars "]", "_", varcat) 176250323Sdteske print "_npkgs_" varcat "=\"" npkgs "\"" 177250323Sdteske } 178250323Sdteske 179250323Sdteske # Create menu list and generate list of categories at same time 180250323Sdteske print "CATEGORY_MENU_LIST=\"" 181250410Sdteske print_category(ENVIRON["msg_all"], tpkgs, 182250323Sdteske ENVIRON["msg_all_desc"]) 183250323Sdteske category_list = "" 184250323Sdteske for (i = 1; i <= n; i++) 185250323Sdteske { 186250323Sdteske cat = varcat = categories_sorted[i] 187250323Sdteske npkgs = categories[cat] 188250323Sdteske cur_prefix = tolower(substr(cat, 1, 1)) 189250323Sdteske if ( prefix != cur_prefix ) 190250323Sdteske prefix = cur_prefix 191250323Sdteske else 192250323Sdteske cat = " " cat 193250323Sdteske gsub("[^" valid_chars "]", "_", varcat) 194250323Sdteske desc = ENVIRON["_category_" varcat] 195250323Sdteske if ( ! desc ) desc = default_desc 196250323Sdteske print_category(cat, npkgs, desc) 197250323Sdteske category_list = category_list " " cat 198250323Sdteske } 199250323Sdteske print "\"" 200250323Sdteske 201250323Sdteske # Produce the list of categories (calculated in above block) 202250323Sdteske sub(/^ /, "", category_list) 203250323Sdteske print "PACKAGE_CATEGORIES=\"" category_list "\"" 204250323Sdteske 205250323Sdteske }' )" # End-Quote 206250323Sdteske} 207250323Sdteske 208250323Sdteske# f_index_extract_pages $var_to_get $var_basename $pagesize [$category] 209250323Sdteske# 210252738Sdteske# Extracts the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is 211252738Sdteske# NULL; but should not be missing) into a series of sequential variables 212250323Sdteske# corresponding to "pages" containing up to $pagesize packages. The package 213250323Sdteske# INDEX data must be contained in the variable $var_to_get. The extracted pages 214250323Sdteske# are stored in variables ${var_basename}_# -- where "#" is a the page number. 215250323Sdteske# If $category is set, only packages for that category are extracted. 216250323Sdteske# Otherwise, if $category is "All", missing, or NULL, all packages are 217250323Sdteske# extracted and no filtering is done. 218250323Sdteske# 219250323Sdteskef_index_extract_pages() 220250323Sdteske{ 221252738Sdteske local var_to_get="${1:-PACKAGE_INDEX}" var_basename="$2" pagesize="$3" 222250323Sdteske local category="$4" # Optional 223250323Sdteske 224250323Sdteske eval "$( 225250323Sdteske debug= f_getvar "$var_to_get" | awk -F'|' \ 226250323Sdteske -v cat="$category" \ 227250323Sdteske -v pagesize="$pagesize" \ 228250538Sdteske -v var_basename="$var_basename" \ 229250538Sdteske -v i18n_all="$msg_all" ' 230250323Sdteske BEGIN { n = page = 0 } 231250323Sdteske /'\''/{ gsub(/'\''/, "'\''\\'\'\''") } 232250323Sdteske { 233250538Sdteske if ( cat !~ "(^$|^" i18n_all "$)" && $7 !~ \ 234250323Sdteske "(^|[[:space:]])" cat "([[:space:]]|$)" ) next 235250323Sdteske starting_new_page = (n++ == (pagesize * page)) 236250323Sdteske if ( starting_new_page ) 237250323Sdteske printf "%s%s", ( n > 1 ? "'\''\n" : "" ), 238250323Sdteske var_basename "_" ++page "='\''" 239250323Sdteske printf "%s%s", ( starting_new_page ? "" : "\n" ), $0 240250323Sdteske } 241250323Sdteske END { if ( n > 0 ) print "'\''" }' 242250323Sdteske )" 243250323Sdteske} 244250323Sdteske 245252745Sdteske# f_index_search $var_to_get $name [$var_to_set] 246252745Sdteske# 247252745Sdteske# Search the package INDEX ($PACKAGE_INDEX by default if/when $var_to)get is 248252745Sdteske# NULL; but should not be missing) for $name, returning the first match. 249252745Sdteske# Matches are strict (not regular expressions) and must match the beginning 250252745Sdteske# portion of the package name to be considered a match. If $var_to_set is 251252745Sdteske# missing or NULL, output is sent to standard output. If a match is found, 252252745Sdteske# returns success; otherwise failure. 253252745Sdteske# 254252745Sdteskef_index_search() 255252745Sdteske{ 256252745Sdteske local __var_to_get="${1:-PACKAGE_INDEX}" __pkg_basename="$2" 257252745Sdteske local __var_to_set="$3" 258252745Sdteske 259252745Sdteske f_dprintf "f_index_search: Searching package data (in %s) for %s" \ 260252745Sdteske "$__var_to_get" "$__pkg_basename" 261252745Sdteske 262252745Sdteske local __pkg= 263252745Sdteske __pkg=$( debug= f_getvar "$__var_to_get" | 264252745Sdteske awk -F'|' -v basename="$__pkg_basename" ' 265252745Sdteske BEGIN { n = length(basename) } 266252745Sdteske substr($1, 0, n) == basename { print $1; exit } 267252745Sdteske ' ) 268252745Sdteske if [ ! "$__pkg" ]; then 269252745Sdteske f_dprintf "f_index_search: No packages matching %s found" \ 270252745Sdteske "$__pkg_basename" 271252745Sdteske return $FAILURE 272252745Sdteske fi 273252745Sdteske 274252745Sdteske f_dprintf "f_index_search: Found package %s" "$__pkg" 275252745Sdteske if [ "$__var_to_set" ]; then 276252745Sdteske setvar "$__var_to_set" "$__pkg" 277252745Sdteske else 278252745Sdteske echo "$__pkg" 279252745Sdteske fi 280252745Sdteske return $SUCCESS 281252745Sdteske} 282252745Sdteske 283250323Sdteske############################################################ MAIN 284250323Sdteske 285250323Sdteskef_dprintf "%s: Successfully loaded." packages/index.subr 286250323Sdteske 287250323Sdteskefi # ! $_PACKAGES_INDEX_SUBR 288