index.subr revision 250410
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 250410 2013-05-09 16:09:39Z 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" 73250323Sdteske eval "$__var_to_set"='$( f_device_get media "$__path" | sort )' 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 80250323Sdteske 81250323Sdteske f_show_info "$msg_located_index_now_reading_package_data_from_it" 82250323Sdteske if ! f_index_read "$__var_to_set"; then 83250323Sdteske f_show_msg "$msg_io_or_format_error_on_index_file" "$__path" 84250323Sdteske return $FAILURE 85250323Sdteske fi 86250323Sdteske 87250323Sdteske _INDEX_INITTED=1 88250323Sdteske return $SUCCESS 89250323Sdteske} 90250323Sdteske 91250323Sdteske# f_index_read [$var_to_get] 92250323Sdteske# 93250323Sdteske# Process the INDEX file (contents contained in $var_to_get) and... 94250323Sdteske# 95250323Sdteske# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts 96250323Sdteske# 2. For convenience, create $_npkgs holding the total number of all packages 97250323Sdteske# 3. extract associative categories for each package into $_categories_$varpkg 98250323Sdteske# 4. extract runtime dependencies for each package into $_rundeps_$varpkg 99250323Sdteske# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES 100250323Sdteske# 6. create $_npkgs_$varcat holding the total number of packages in category 101250323Sdteske# 102250323Sdteske# NOTE: $varpkg is the product of f_str2varname $package varpkg 103250323Sdteske# NOTE: $package is the name as it appears in the INDEX (no archive suffix) 104250323Sdteske# NOTE: We only show categories for which there are at least one package. 105250323Sdteske# NOTE: $varcat is the product of f_str2varname $category varcat 106250323Sdteske# 107250323Sdteskef_index_read() 108250323Sdteske{ 109250323Sdteske local var_to_get="${1:-PACKAGE_INDEX}" 110250323Sdteske 111250323Sdteske # Export variables required by awk(1) below 112250323Sdteske export msg_no_description_provided 113250323Sdteske export msg_all msg_all_desc 114250323Sdteske export VALID_VARNAME_CHARS 115250323Sdteske 116250323Sdteske eval "$( debug= f_getvar "$var_to_get" | awk -F'|' ' 117250323Sdteske function asorti(src, dest) 118250323Sdteske { 119250323Sdteske # Copy src indices to dest and calculate array length 120250323Sdteske nitems = 0; for (i in src) dest[++nitems] = i 121250323Sdteske 122250323Sdteske # Sort the array of indices (dest) using insertion sort method 123250323Sdteske for (i = 1; i <= nitems; k = i++) 124250323Sdteske { 125250323Sdteske idx = dest[i] 126250323Sdteske while ((k > 0) && (dest[k] > idx)) 127250323Sdteske { 128250323Sdteske dest[k+1] = dest[k] 129250323Sdteske k-- 130250323Sdteske } 131250323Sdteske dest[k+1] = idx 132250323Sdteske } 133250323Sdteske 134250323Sdteske return nitems 135250323Sdteske } 136250323Sdteske function print_category(category, npkgs, desc) 137250323Sdteske { 138250323Sdteske cat = category 139250323Sdteske # Accent the category if the first page has been 140250323Sdteske # cached (also acting as a visitation indicator) 141250323Sdteske if ( ENVIRON["_index_page_" varcat "_1"] ) 142250323Sdteske cat = cat "*" 143250323Sdteske printf "'\''%s'\'' '\''%s packages'\'' '\''%s'\''\n", 144250323Sdteske cat, npkgs, desc 145250323Sdteske } 146250323Sdteske BEGIN { 147250323Sdteske valid_chars = ENVIRON["VALID_VARNAME_CHARS"] 148250323Sdteske default_desc = ENVIRON["msg_no_description_provided"] 149250410Sdteske tpkgs = 0 150250323Sdteske prefix = "" 151250323Sdteske } 152250323Sdteske { 153250410Sdteske tpkgs++ 154250323Sdteske varpkg = $1 155250323Sdteske gsub("[^" valid_chars "]", "_", varpkg) 156250323Sdteske print "_categories_" varpkg "=\"" $7 "\"" 157250323Sdteske split($7, pkg_categories, /[[:space:]]+/) 158250323Sdteske for (pkg_category in pkg_categories) 159250323Sdteske categories[pkg_categories[pkg_category]]++ 160250323Sdteske print "_rundeps_" varpkg "=\"" $9 "\"" 161250323Sdteske } 162250323Sdteske END { 163250410Sdteske print "_npkgs=" tpkgs # For convenience, total package count 164250323Sdteske 165250323Sdteske n = asorti(categories, categories_sorted) 166250323Sdteske 167250323Sdteske # Produce package counts for each category 168250323Sdteske for (i = 1; i <= n; i++) 169250323Sdteske { 170250323Sdteske cat = varcat = categories_sorted[i] 171250323Sdteske npkgs = categories[cat] 172250323Sdteske gsub("[^" valid_chars "]", "_", varcat) 173250323Sdteske print "_npkgs_" varcat "=\"" npkgs "\"" 174250323Sdteske } 175250323Sdteske 176250323Sdteske # Create menu list and generate list of categories at same time 177250323Sdteske print "CATEGORY_MENU_LIST=\"" 178250410Sdteske print_category(ENVIRON["msg_all"], tpkgs, 179250323Sdteske ENVIRON["msg_all_desc"]) 180250323Sdteske category_list = "" 181250323Sdteske for (i = 1; i <= n; i++) 182250323Sdteske { 183250323Sdteske cat = varcat = categories_sorted[i] 184250323Sdteske npkgs = categories[cat] 185250323Sdteske cur_prefix = tolower(substr(cat, 1, 1)) 186250323Sdteske if ( prefix != cur_prefix ) 187250323Sdteske prefix = cur_prefix 188250323Sdteske else 189250323Sdteske cat = " " cat 190250323Sdteske gsub("[^" valid_chars "]", "_", varcat) 191250323Sdteske desc = ENVIRON["_category_" varcat] 192250323Sdteske if ( ! desc ) desc = default_desc 193250323Sdteske print_category(cat, npkgs, desc) 194250323Sdteske category_list = category_list " " cat 195250323Sdteske } 196250323Sdteske print "\"" 197250323Sdteske 198250323Sdteske # Produce the list of categories (calculated in above block) 199250323Sdteske sub(/^ /, "", category_list) 200250323Sdteske print "PACKAGE_CATEGORIES=\"" category_list "\"" 201250323Sdteske 202250323Sdteske }' )" # End-Quote 203250323Sdteske} 204250323Sdteske 205250323Sdteske# f_index_extract_pages $var_to_get $var_basename $pagesize [$category] 206250323Sdteske# 207250323Sdteske# Extracts the package INDEX into a series of sequential variables 208250323Sdteske# corresponding to "pages" containing up to $pagesize packages. The package 209250323Sdteske# INDEX data must be contained in the variable $var_to_get. The extracted pages 210250323Sdteske# are stored in variables ${var_basename}_# -- where "#" is a the page number. 211250323Sdteske# If $category is set, only packages for that category are extracted. 212250323Sdteske# Otherwise, if $category is "All", missing, or NULL, all packages are 213250323Sdteske# extracted and no filtering is done. 214250323Sdteske# 215250323Sdteskef_index_extract_pages() 216250323Sdteske{ 217250323Sdteske local var_to_get="$1" var_basename="$2" pagesize="$3" 218250323Sdteske local category="$4" # Optional 219250323Sdteske 220250323Sdteske eval "$( 221250323Sdteske debug= f_getvar "$var_to_get" | awk -F'|' \ 222250323Sdteske -v cat="$category" \ 223250323Sdteske -v pagesize="$pagesize" \ 224250323Sdteske -v var_basename="$var_basename" ' 225250323Sdteske BEGIN { n = page = 0 } 226250323Sdteske /'\''/{ gsub(/'\''/, "'\''\\'\'\''") } 227250323Sdteske { 228250323Sdteske if ( cat !~ /(^$|^All$)/ && $7 !~ \ 229250323Sdteske "(^|[[:space:]])" cat "([[:space:]]|$)" ) next 230250323Sdteske starting_new_page = (n++ == (pagesize * page)) 231250323Sdteske if ( starting_new_page ) 232250323Sdteske printf "%s%s", ( n > 1 ? "'\''\n" : "" ), 233250323Sdteske var_basename "_" ++page "='\''" 234250323Sdteske printf "%s%s", ( starting_new_page ? "" : "\n" ), $0 235250323Sdteske } 236250323Sdteske END { if ( n > 0 ) print "'\''" }' 237250323Sdteske )" 238250323Sdteske} 239250323Sdteske 240250323Sdteske############################################################ MAIN 241250323Sdteske 242250323Sdteskef_dprintf "%s: Successfully loaded." packages/index.subr 243250323Sdteske 244250323Sdteskefi # ! $_PACKAGES_INDEX_SUBR 245