index.subr revision 250538
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 250538 2013-05-12 00:46:18Z 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 115250538Sdteske export msg_packages 116250323Sdteske 117250323Sdteske eval "$( debug= f_getvar "$var_to_get" | awk -F'|' ' 118250323Sdteske function asorti(src, dest) 119250323Sdteske { 120250323Sdteske # Copy src indices to dest and calculate array length 121250323Sdteske nitems = 0; for (i in src) dest[++nitems] = i 122250323Sdteske 123250323Sdteske # Sort the array of indices (dest) using insertion sort method 124250323Sdteske for (i = 1; i <= nitems; k = i++) 125250323Sdteske { 126250323Sdteske idx = dest[i] 127250323Sdteske while ((k > 0) && (dest[k] > idx)) 128250323Sdteske { 129250323Sdteske dest[k+1] = dest[k] 130250323Sdteske k-- 131250323Sdteske } 132250323Sdteske dest[k+1] = idx 133250323Sdteske } 134250323Sdteske 135250323Sdteske return nitems 136250323Sdteske } 137250323Sdteske function print_category(category, npkgs, desc) 138250323Sdteske { 139250323Sdteske cat = category 140250323Sdteske # Accent the category if the first page has been 141250323Sdteske # cached (also acting as a visitation indicator) 142250323Sdteske if ( ENVIRON["_index_page_" varcat "_1"] ) 143250323Sdteske cat = cat "*" 144250538Sdteske printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n", 145250323Sdteske cat, npkgs, desc 146250323Sdteske } 147250323Sdteske BEGIN { 148250323Sdteske valid_chars = ENVIRON["VALID_VARNAME_CHARS"] 149250323Sdteske default_desc = ENVIRON["msg_no_description_provided"] 150250538Sdteske packages = ENVIRON["msg_packages"] 151250410Sdteske tpkgs = 0 152250323Sdteske prefix = "" 153250323Sdteske } 154250323Sdteske { 155250410Sdteske tpkgs++ 156250323Sdteske varpkg = $1 157250323Sdteske gsub("[^" valid_chars "]", "_", varpkg) 158250323Sdteske print "_categories_" varpkg "=\"" $7 "\"" 159250323Sdteske split($7, pkg_categories, /[[:space:]]+/) 160250323Sdteske for (pkg_category in pkg_categories) 161250323Sdteske categories[pkg_categories[pkg_category]]++ 162250323Sdteske print "_rundeps_" varpkg "=\"" $9 "\"" 163250323Sdteske } 164250323Sdteske END { 165250410Sdteske print "_npkgs=" tpkgs # For convenience, total package count 166250323Sdteske 167250323Sdteske n = asorti(categories, categories_sorted) 168250323Sdteske 169250323Sdteske # Produce package counts for each category 170250323Sdteske for (i = 1; i <= n; i++) 171250323Sdteske { 172250323Sdteske cat = varcat = categories_sorted[i] 173250323Sdteske npkgs = categories[cat] 174250323Sdteske gsub("[^" valid_chars "]", "_", varcat) 175250323Sdteske print "_npkgs_" varcat "=\"" npkgs "\"" 176250323Sdteske } 177250323Sdteske 178250323Sdteske # Create menu list and generate list of categories at same time 179250323Sdteske print "CATEGORY_MENU_LIST=\"" 180250410Sdteske print_category(ENVIRON["msg_all"], tpkgs, 181250323Sdteske ENVIRON["msg_all_desc"]) 182250323Sdteske category_list = "" 183250323Sdteske for (i = 1; i <= n; i++) 184250323Sdteske { 185250323Sdteske cat = varcat = categories_sorted[i] 186250323Sdteske npkgs = categories[cat] 187250323Sdteske cur_prefix = tolower(substr(cat, 1, 1)) 188250323Sdteske if ( prefix != cur_prefix ) 189250323Sdteske prefix = cur_prefix 190250323Sdteske else 191250323Sdteske cat = " " cat 192250323Sdteske gsub("[^" valid_chars "]", "_", varcat) 193250323Sdteske desc = ENVIRON["_category_" varcat] 194250323Sdteske if ( ! desc ) desc = default_desc 195250323Sdteske print_category(cat, npkgs, desc) 196250323Sdteske category_list = category_list " " cat 197250323Sdteske } 198250323Sdteske print "\"" 199250323Sdteske 200250323Sdteske # Produce the list of categories (calculated in above block) 201250323Sdteske sub(/^ /, "", category_list) 202250323Sdteske print "PACKAGE_CATEGORIES=\"" category_list "\"" 203250323Sdteske 204250323Sdteske }' )" # End-Quote 205250323Sdteske} 206250323Sdteske 207250323Sdteske# f_index_extract_pages $var_to_get $var_basename $pagesize [$category] 208250323Sdteske# 209250323Sdteske# Extracts the package INDEX into a series of sequential variables 210250323Sdteske# corresponding to "pages" containing up to $pagesize packages. The package 211250323Sdteske# INDEX data must be contained in the variable $var_to_get. The extracted pages 212250323Sdteske# are stored in variables ${var_basename}_# -- where "#" is a the page number. 213250323Sdteske# If $category is set, only packages for that category are extracted. 214250323Sdteske# Otherwise, if $category is "All", missing, or NULL, all packages are 215250323Sdteske# extracted and no filtering is done. 216250323Sdteske# 217250323Sdteskef_index_extract_pages() 218250323Sdteske{ 219250323Sdteske local var_to_get="$1" var_basename="$2" pagesize="$3" 220250323Sdteske local category="$4" # Optional 221250323Sdteske 222250323Sdteske eval "$( 223250323Sdteske debug= f_getvar "$var_to_get" | awk -F'|' \ 224250323Sdteske -v cat="$category" \ 225250323Sdteske -v pagesize="$pagesize" \ 226250538Sdteske -v var_basename="$var_basename" \ 227250538Sdteske -v i18n_all="$msg_all" ' 228250323Sdteske BEGIN { n = page = 0 } 229250323Sdteske /'\''/{ gsub(/'\''/, "'\''\\'\'\''") } 230250323Sdteske { 231250538Sdteske if ( cat !~ "(^$|^" i18n_all "$)" && $7 !~ \ 232250323Sdteske "(^|[[:space:]])" cat "([[:space:]]|$)" ) next 233250323Sdteske starting_new_page = (n++ == (pagesize * page)) 234250323Sdteske if ( starting_new_page ) 235250323Sdteske printf "%s%s", ( n > 1 ? "'\''\n" : "" ), 236250323Sdteske var_basename "_" ++page "='\''" 237250323Sdteske printf "%s%s", ( starting_new_page ? "" : "\n" ), $0 238250323Sdteske } 239250323Sdteske END { if ( n > 0 ) print "'\''" }' 240250323Sdteske )" 241250323Sdteske} 242250323Sdteske 243250323Sdteske############################################################ MAIN 244250323Sdteske 245250323Sdteskef_dprintf "%s: Successfully loaded." packages/index.subr 246250323Sdteske 247250323Sdteskefi # ! $_PACKAGES_INDEX_SUBR 248