index.subr revision 259427
1250323Sdteskeif [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1 2250323Sdteske# 3250323Sdteske# Copyright (c) 2013 Devin Teske 4252980Sdteske# 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 16252987Sdteske# 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 20252987Sdteske# DAMAGES (INCLUDING, 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 259427 2013-12-15 20:47:27Z gjb $ 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 46257795Sdteske# 47257795Sdteske# Default path to pkg(8) repo-packagesite.sqlite database 48257795Sdteske# 49257795SdteskeSQLITE_REPO="/var/db/pkg/repo-packagesite.sqlite" 50257795Sdteske 51257795Sdteske# 52257795Sdteske# Default path to on-disk cache INDEX file 53257795Sdteske# 54257795SdteskePACKAGES_INDEX_CACHEFILE="/var/run/bsdconfig/packages_INDEX.cache" 55257795Sdteske 56257795Sdteske# 57257795Sdteske# INDEX format for FreeBSD-6.0 or higher: 58257795Sdteske# 59257795Sdteske# package|port-origin|install-prefix|comment|port-desc-file|maintainer| 60257795Sdteske# categories|build-deps|run-deps|www-site|reserve|reserve|reserve|disc 61257795Sdteske# 62257795SdteskeINDEX_FORMAT="%n-%v" # package 63257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|/usr/ports/%o" # port-origin 64257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|%p" # install-prefix 65257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|%c" # comment 66257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|/usr/ports/%o/pkg-descr" # port-desc-file 67257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|%m" # maintainer 68257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|@CATEGORIES@" # place-holder 69257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|" # build-deps 70257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|@RUNDEPS@" # place-holder 71257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|%w" # www-site 72257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|" # reserved 73257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|" # reserved 74257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|" # reserved 75257795SdteskeINDEX_FORMAT="$INDEX_FORMAT|" # disc 76257795Sdteske 77250323Sdteske############################################################ FUNCTIONS 78250323Sdteske 79257795Sdteske# f_index_initialize [$var_to_set] 80250323Sdteske# 81257795Sdteske# Read and initialize the global index. Returns success unless media cannot be 82257795Sdteske# initialized for any reason (e.g. user cancels media selection dialog or an 83257795Sdteske# error occurs). The index is sorted before being loaded into $var_to_set. 84250323Sdteske# 85250323Sdteske# NOTE: The index is processed with f_index_read() [below] after being loaded. 86250323Sdteske# 87250323Sdteskef_index_initialize() 88250323Sdteske{ 89258265Sdteske local __funcname=f_index_initialize 90257795Sdteske local __var_to_set="${2:-PACKAGE_INDEX}" 91250323Sdteske 92250323Sdteske [ "$_INDEX_INITTED" ] && return $SUCCESS 93250323Sdteske 94250323Sdteske # Got any media? 95250323Sdteske f_media_verify || return $FAILURE 96250323Sdteske 97250323Sdteske # Does it move when you kick it? 98250323Sdteske f_device_init media || return $FAILURE 99250323Sdteske 100257795Sdteske f_show_info "$msg_attempting_to_update_repository_catalogue" 101257795Sdteske 102257795Sdteske # 103257795Sdteske # Generate $PACKAGESITE variable for pkg(8) based on media type 104257795Sdteske # 105257795Sdteske local __type __data __site 106257795Sdteske device_media get type __type 107257795Sdteske device_media get private __data 108257795Sdteske case "$__type" in 109258267Sdteske $DEVICE_TYPE_DIRECTORY) 110258267Sdteske __site="file://$__data/packages/$PKG_ABI" ;; 111258267Sdteske $DEVICE_TYPE_FLOPPY) 112258267Sdteske __site="file://${__data:-$MOUNTPOINT}/packages/$PKG_ABI" ;; 113258267Sdteske $DEVICE_TYPE_FTP) 114258267Sdteske f_getvar $VAR_FTP_PATH __site 115258267Sdteske __site="$__site/packages/$PKG_ABI" ;; 116258267Sdteske $DEVICE_TYPE_HTTP) 117258267Sdteske f_getvar $VAR_HTTP_PATH __site 118258267Sdteske __site="$__site/$PKG_ABI/latest" ;; 119258267Sdteske $DEVICE_TYPE_HTTP_PROXY) 120258267Sdteske f_getvar $VAR_HTTP_PROXY_PATH __site 121258267Sdteske __site="$__site/packages/$PKG_ABI" ;; 122259427Sgjb $DEVICE_TYPE_CDROM) 123259427Sgjb __site="file://$MOUNTPOINT/packages/$PKG_ABI" 124259427Sgjb export REPOS_DIR="$MOUNTPOINT/packages/repos" ;; 125258267Sdteske *) # UFS, DISK, CDROM, USB, DOS, NFS, etc. 126258267Sdteske __site="file://$MOUNTPOINT/packages/$PKG_ABI" 127257795Sdteske esac 128257795Sdteske 129258265Sdteske export PACKAGESITE="$__site" 130258265Sdteske f_dprintf "PACKAGESITE=[%s]" "$PACKAGESITE" 131258265Sdteske if ! f_eval_catch $__funcname pkg "pkg update"; then 132257795Sdteske f_show_err "$msg_unable_to_update_pkg_from_selected_media" 133250323Sdteske f_device_shutdown media 134250323Sdteske return $FAILURE 135250323Sdteske fi 136257795Sdteske 137257795Sdteske # 138257795Sdteske # Try to get contents from validated on-disk cache 139257795Sdteske # 140257795Sdteske 141257795Sdteske # 142257795Sdteske # Calculate digest used to determine if the on-disk persistant cache 143257795Sdteske # INDEX (containing this digest on the first line) is valid and can be 144257795Sdteske # used to quickly populate the environment. 145257795Sdteske # 146257795Sdteske local __sqlite_digest 147257795Sdteske if ! __sqlite_digest=$( md5 < "$SQLITE_REPO" 2> /dev/null ); then 148257795Sdteske f_show_err "$msg_no_pkg_database_found" 149257795Sdteske f_device_shutdown media 150257795Sdteske return $FAILURE 151257795Sdteske fi 152257795Sdteske 153257795Sdteske # 154257795Sdteske # Check to see if the persistant cache INDEX file exists 155257795Sdteske # 156257795Sdteske if [ -f "$PACKAGES_INDEX_CACHEFILE" ]; then 157257795Sdteske # 158257795Sdteske # Attempt to populate the environment with the (soon to be) 159257795Sdteske # validated on-disk cache. If validation fails, fall-back to 160257795Sdteske # generating a fresh cache. 161257795Sdteske # 162257795Sdteske if eval $__var_to_set='$( 163257795Sdteske ( # Get digest as the first word on first line 164257795Sdteske read digest rest_ignored 165257795Sdteske 166257795Sdteske # 167257795Sdteske # If the stored digest matches the calculated- 168257795Sdteske # one populate the environment from the on-disk 169257795Sdteske # cache and provide success exit status. 170257795Sdteske # 171257795Sdteske if [ "$digest" = "$__sqlite_digest" ]; then 172257795Sdteske cat 173257795Sdteske exit $SUCCESS 174257795Sdteske else 175257795Sdteske # Otherwise, return the current value 176257795Sdteske eval echo \"\$__var_to_set\" 177257795Sdteske exit $FAILURE 178257795Sdteske fi 179257795Sdteske ) < "$PACKAGES_INDEX_CACHEFILE" 2> /dev/null 180257795Sdteske )'; then 181257795Sdteske f_show_info \ 182257795Sdteske "$msg_located_index_now_reading_package_data_from_it" 183257795Sdteske if ! f_index_read "$__var_to_set"; then 184257795Sdteske f_show_err \ 185257795Sdteske "$msg_io_or_format_error_on_index_file" 186257795Sdteske return $FAILURE 187257795Sdteske fi 188257795Sdteske _INDEX_INITTED=1 189257795Sdteske return $SUCCESS 190257795Sdteske fi 191257795Sdteske # Otherwise, fall-thru to create a fresh cache from scratch 192257795Sdteske fi 193257795Sdteske 194257795Sdteske # 195257795Sdteske # If we reach this point, we need to generate the data from scratch 196257795Sdteske # 197257795Sdteske 198257795Sdteske f_show_info "$msg_getting_package_categories_via_pkg_rquery" 199257795Sdteske if ! eval "$( pkg rquery "%n-%v %C" | awk ' 200257795Sdteske { categories[$1] = categories[$1] " " $2 } 201257795Sdteske END { 202257795Sdteske for (package in categories) 203257795Sdteske { 204257795Sdteske cats = categories[package] 205257795Sdteske sub(/^ /, "", cats) 206257795Sdteske gsub(/[^[:alnum:]_]/, "_", package) 207257795Sdteske printf "local _%s_categories=\"%s\";\n", package, cats 208257795Sdteske } 209257795Sdteske }' )"; then 210257817Sdteske f_show_err "$msg_unable_to_pkg_rquery_package_dependencies" 211257795Sdteske f_device_shutdown media 212257795Sdteske return $FAILURE 213257795Sdteske fi 214257795Sdteske 215257795Sdteske f_show_info "$msg_getting_package_dependencies_via_pkg_rquery" 216257795Sdteske if ! eval "$( pkg rquery "%n-%v %dn-%dv" | awk ' 217257795Sdteske { rundeps[$1] = rundeps[$1] " " $2 } 218257795Sdteske END { 219257795Sdteske for (package in rundeps) 220257795Sdteske { 221257795Sdteske deps = rundeps[package] 222257795Sdteske sub(/^ /, "", deps) 223257795Sdteske gsub(/[^[:alnum:]_]/, "_", package) 224257795Sdteske printf "local _%s_rundeps=\"%s\";\n", package, deps 225257795Sdteske } 226257795Sdteske }' )"; then 227257817Sdteske f_show_err "$msg_unable_to_pkg_rquery_package_dependencies" 228257795Sdteske f_device_shutdown media 229257795Sdteske return $FAILURE 230257795Sdteske fi 231257795Sdteske 232257795Sdteske f_show_info "$msg_generating_index_from_pkg_database" 233257795Sdteske eval "$__var_to_set"='$( pkg rquery "$INDEX_FORMAT" | 234257795Sdteske while read LINE; do 235257795Sdteske package="${LINE%%|*}"; 236257795Sdteske f_str2varname "$package" varpkg; 237257795Sdteske eval f_replaceall \"\$LINE\" \"\|@CATEGORIES@\|\" \ 238257795Sdteske \"\|\$_${varpkg}_categories\|\" LINE 239257795Sdteske eval f_replaceall \"\$LINE\" \"\|@RUNDEPS@\|\" \ 240257795Sdteske \"\|\$_${varpkg}_rundeps\|\" LINE 241257795Sdteske echo "$LINE" 242257795Sdteske done 243257795Sdteske )' # always returns true (status of last item in pipe-chain) 244251276Sdteske eval "$__var_to_set"='$( debug= f_getvar "$__var_to_set" | sort )' 245250323Sdteske 246257795Sdteske # 247257795Sdteske # Attempt to create the persistant on-disk cache 248257795Sdteske # 249257795Sdteske 250257795Sdteske # Create a new temporary file to write to 251259054Sdteske local __tmpfile 252259054Sdteske if f_eval_catch -dk __tmpfile $__funcname mktemp \ 253259054Sdteske 'mktemp -t "%s"' "$pgm" 254259054Sdteske then 255257795Sdteske # Write the temporary file contents 256257795Sdteske echo "$__sqlite_digest" > "$__tmpfile" 257257795Sdteske debug= f_getvar "$__var_to_set" >> "$__tmpfile" 258257795Sdteske 259257795Sdteske # Finally, move the temporary file into place 260257795Sdteske case "$PACKAGES_INDEX_CACHEFILE" in 261259054Sdteske */*) f_eval_catch -d $funcname mkdir \ 262259054Sdteske 'mkdir -p "%s"' "${PACKAGES_INDEX_CACHEFILE%/*}" 263257795Sdteske esac 264259054Sdteske f_eval_catch -d $__funcname mv 'mv -f "%s" "%s"' \ 265259054Sdteske "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE" 266257795Sdteske fi 267257795Sdteske 268250323Sdteske f_show_info "$msg_located_index_now_reading_package_data_from_it" 269250323Sdteske if ! f_index_read "$__var_to_set"; then 270257795Sdteske f_show_err "$msg_io_or_format_error_on_index_file" 271250323Sdteske return $FAILURE 272250323Sdteske fi 273250323Sdteske 274250323Sdteske _INDEX_INITTED=1 275250323Sdteske return $SUCCESS 276250323Sdteske} 277250323Sdteske 278250323Sdteske# f_index_read [$var_to_get] 279250323Sdteske# 280250323Sdteske# Process the INDEX file (contents contained in $var_to_get) and... 281250323Sdteske# 282250323Sdteske# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts 283250323Sdteske# 2. For convenience, create $_npkgs holding the total number of all packages 284250323Sdteske# 3. extract associative categories for each package into $_categories_$varpkg 285250323Sdteske# 4. extract runtime dependencies for each package into $_rundeps_$varpkg 286250323Sdteske# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES 287250323Sdteske# 6. create $_npkgs_$varcat holding the total number of packages in category 288250323Sdteske# 289250323Sdteske# NOTE: $varpkg is the product of f_str2varname $package varpkg 290250323Sdteske# NOTE: $package is the name as it appears in the INDEX (no archive suffix) 291250323Sdteske# NOTE: We only show categories for which there are at least one package. 292250323Sdteske# NOTE: $varcat is the product of f_str2varname $category varcat 293250323Sdteske# 294250323Sdteskef_index_read() 295250323Sdteske{ 296250323Sdteske local var_to_get="${1:-PACKAGE_INDEX}" 297250323Sdteske 298250323Sdteske # Export variables required by awk(1) below 299250323Sdteske export msg_no_description_provided 300250323Sdteske export msg_all msg_all_desc 301250323Sdteske export VALID_VARNAME_CHARS 302250538Sdteske export msg_packages 303250323Sdteske 304250323Sdteske eval "$( debug= f_getvar "$var_to_get" | awk -F'|' ' 305250323Sdteske function asorti(src, dest) 306250323Sdteske { 307250323Sdteske # Copy src indices to dest and calculate array length 308250323Sdteske nitems = 0; for (i in src) dest[++nitems] = i 309250323Sdteske 310250323Sdteske # Sort the array of indices (dest) using insertion sort method 311250323Sdteske for (i = 1; i <= nitems; k = i++) 312250323Sdteske { 313250323Sdteske idx = dest[i] 314250323Sdteske while ((k > 0) && (dest[k] > idx)) 315250323Sdteske { 316250323Sdteske dest[k+1] = dest[k] 317250323Sdteske k-- 318250323Sdteske } 319250323Sdteske dest[k+1] = idx 320250323Sdteske } 321250323Sdteske 322250323Sdteske return nitems 323250323Sdteske } 324250323Sdteske function print_category(category, npkgs, desc) 325250323Sdteske { 326250323Sdteske cat = category 327250323Sdteske # Accent the category if the first page has been 328250323Sdteske # cached (also acting as a visitation indicator) 329250323Sdteske if ( ENVIRON["_index_page_" varcat "_1"] ) 330250323Sdteske cat = cat "*" 331250538Sdteske printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n", 332250323Sdteske cat, npkgs, desc 333250323Sdteske } 334250323Sdteske BEGIN { 335250323Sdteske valid_chars = ENVIRON["VALID_VARNAME_CHARS"] 336250323Sdteske default_desc = ENVIRON["msg_no_description_provided"] 337250538Sdteske packages = ENVIRON["msg_packages"] 338250410Sdteske tpkgs = 0 339250323Sdteske prefix = "" 340250323Sdteske } 341250323Sdteske { 342250410Sdteske tpkgs++ 343250323Sdteske varpkg = $1 344250323Sdteske gsub("[^" valid_chars "]", "_", varpkg) 345250323Sdteske print "_categories_" varpkg "=\"" $7 "\"" 346250323Sdteske split($7, pkg_categories, /[[:space:]]+/) 347250323Sdteske for (pkg_category in pkg_categories) 348250323Sdteske categories[pkg_categories[pkg_category]]++ 349250323Sdteske print "_rundeps_" varpkg "=\"" $9 "\"" 350250323Sdteske } 351250323Sdteske END { 352250410Sdteske print "_npkgs=" tpkgs # For convenience, total package count 353250323Sdteske 354250323Sdteske n = asorti(categories, categories_sorted) 355250323Sdteske 356250323Sdteske # Produce package counts for each category 357250323Sdteske for (i = 1; i <= n; i++) 358250323Sdteske { 359250323Sdteske cat = varcat = categories_sorted[i] 360250323Sdteske npkgs = categories[cat] 361250323Sdteske gsub("[^" valid_chars "]", "_", varcat) 362250323Sdteske print "_npkgs_" varcat "=\"" npkgs "\"" 363250323Sdteske } 364250323Sdteske 365250323Sdteske # Create menu list and generate list of categories at same time 366250323Sdteske print "CATEGORY_MENU_LIST=\"" 367250410Sdteske print_category(ENVIRON["msg_all"], tpkgs, 368250323Sdteske ENVIRON["msg_all_desc"]) 369250323Sdteske category_list = "" 370250323Sdteske for (i = 1; i <= n; i++) 371250323Sdteske { 372250323Sdteske cat = varcat = categories_sorted[i] 373250323Sdteske npkgs = categories[cat] 374250323Sdteske cur_prefix = tolower(substr(cat, 1, 1)) 375250323Sdteske if ( prefix != cur_prefix ) 376250323Sdteske prefix = cur_prefix 377250323Sdteske else 378250323Sdteske cat = " " cat 379250323Sdteske gsub("[^" valid_chars "]", "_", varcat) 380250323Sdteske desc = ENVIRON["_category_" varcat] 381250323Sdteske if ( ! desc ) desc = default_desc 382250323Sdteske print_category(cat, npkgs, desc) 383250323Sdteske category_list = category_list " " cat 384250323Sdteske } 385250323Sdteske print "\"" 386250323Sdteske 387250323Sdteske # Produce the list of categories (calculated in above block) 388250323Sdteske sub(/^ /, "", category_list) 389250323Sdteske print "PACKAGE_CATEGORIES=\"" category_list "\"" 390250323Sdteske 391250323Sdteske }' )" # End-Quote 392250323Sdteske} 393250323Sdteske 394250323Sdteske# f_index_extract_pages $var_to_get $var_basename $pagesize [$category] 395250323Sdteske# 396252738Sdteske# Extracts the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is 397252738Sdteske# NULL; but should not be missing) into a series of sequential variables 398250323Sdteske# corresponding to "pages" containing up to $pagesize packages. The package 399250323Sdteske# INDEX data must be contained in the variable $var_to_get. The extracted pages 400250323Sdteske# are stored in variables ${var_basename}_# -- where "#" is a the page number. 401250323Sdteske# If $category is set, only packages for that category are extracted. 402250323Sdteske# Otherwise, if $category is "All", missing, or NULL, all packages are 403250323Sdteske# extracted and no filtering is done. 404250323Sdteske# 405250323Sdteskef_index_extract_pages() 406250323Sdteske{ 407252738Sdteske local var_to_get="${1:-PACKAGE_INDEX}" var_basename="$2" pagesize="$3" 408250323Sdteske local category="$4" # Optional 409250323Sdteske 410250323Sdteske eval "$( 411250323Sdteske debug= f_getvar "$var_to_get" | awk -F'|' \ 412250323Sdteske -v cat="$category" \ 413250323Sdteske -v pagesize="$pagesize" \ 414250538Sdteske -v var_basename="$var_basename" \ 415250538Sdteske -v i18n_all="$msg_all" ' 416250323Sdteske BEGIN { n = page = 0 } 417250323Sdteske /'\''/{ gsub(/'\''/, "'\''\\'\'\''") } 418250323Sdteske { 419250538Sdteske if ( cat !~ "(^$|^" i18n_all "$)" && $7 !~ \ 420250323Sdteske "(^|[[:space:]])" cat "([[:space:]]|$)" ) next 421250323Sdteske starting_new_page = (n++ == (pagesize * page)) 422250323Sdteske if ( starting_new_page ) 423250323Sdteske printf "%s%s", ( n > 1 ? "'\''\n" : "" ), 424250323Sdteske var_basename "_" ++page "='\''" 425250323Sdteske printf "%s%s", ( starting_new_page ? "" : "\n" ), $0 426250323Sdteske } 427250323Sdteske END { if ( n > 0 ) print "'\''" }' 428250323Sdteske )" 429250323Sdteske} 430250323Sdteske 431252745Sdteske# f_index_search $var_to_get $name [$var_to_set] 432252745Sdteske# 433252753Sdteske# Search the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is 434252745Sdteske# NULL; but should not be missing) for $name, returning the first match. 435252745Sdteske# Matches are strict (not regular expressions) and must match the beginning 436252745Sdteske# portion of the package name to be considered a match. If $var_to_set is 437252745Sdteske# missing or NULL, output is sent to standard output. If a match is found, 438252745Sdteske# returns success; otherwise failure. 439252745Sdteske# 440252745Sdteskef_index_search() 441252745Sdteske{ 442252745Sdteske local __var_to_get="${1:-PACKAGE_INDEX}" __pkg_basename="$2" 443252745Sdteske local __var_to_set="$3" 444252745Sdteske 445252745Sdteske f_dprintf "f_index_search: Searching package data (in %s) for %s" \ 446252745Sdteske "$__var_to_get" "$__pkg_basename" 447252745Sdteske 448252745Sdteske local __pkg= 449252745Sdteske __pkg=$( debug= f_getvar "$__var_to_get" | 450252745Sdteske awk -F'|' -v basename="$__pkg_basename" ' 451252745Sdteske BEGIN { n = length(basename) } 452252745Sdteske substr($1, 0, n) == basename { print $1; exit } 453252745Sdteske ' ) 454252745Sdteske if [ ! "$__pkg" ]; then 455252745Sdteske f_dprintf "f_index_search: No packages matching %s found" \ 456252745Sdteske "$__pkg_basename" 457252745Sdteske return $FAILURE 458252745Sdteske fi 459252745Sdteske 460252745Sdteske f_dprintf "f_index_search: Found package %s" "$__pkg" 461252745Sdteske if [ "$__var_to_set" ]; then 462252745Sdteske setvar "$__var_to_set" "$__pkg" 463252745Sdteske else 464252745Sdteske echo "$__pkg" 465252745Sdteske fi 466252745Sdteske return $SUCCESS 467252745Sdteske} 468252745Sdteske 469250323Sdteske############################################################ MAIN 470250323Sdteske 471250323Sdteskef_dprintf "%s: Successfully loaded." packages/index.subr 472250323Sdteske 473250323Sdteskefi # ! $_PACKAGES_INDEX_SUBR 474