1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
if [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1
#
# Copyright (c) 2013 Devin Teske
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
############################################################ INCLUDES
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
f_dprintf "%s: loading includes..." packages/index.subr
f_include $BSDCFG_SHARE/device.subr
f_include $BSDCFG_SHARE/media/common.subr
f_include $BSDCFG_SHARE/strings.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig"
f_include_lang $BSDCFG_LIBE/include/messages.subr
############################################################ GLOBALS
PACKAGE_INDEX=
_INDEX_INITTED=
############################################################ FUNCTIONS
# f_index_initialize $path [$var_to_set]
#
# Read and initialize the global index. $path is to be relative to the chosen
# media (not necessarily the filesystem; e.g. FTP) -- this is usually going to
# be `packages/INDEX'. Returns success unless media cannot be initialized for
# any reason (e.g. user cancels media selection dialog) or an error occurs. The
# index is sorted before being loaded into $var_to_set.
#
# NOTE: The index is processed with f_index_read() [below] after being loaded.
#
f_index_initialize()
{
local __path="$1" __var_to_set="${2:-PACKAGE_INDEX}"
[ "$_INDEX_INITTED" ] && return $SUCCESS
[ "$__path" ] || return $FAILURE
# Got any media?
f_media_verify || return $FAILURE
# Does it move when you kick it?
f_device_init media || return $FAILURE
f_show_info "$msg_attempting_to_fetch_file_from_selected_media" \
"$__path"
eval "$__var_to_set"='$( f_device_get media "$__path" | sort )'
if [ $? -ne $SUCCESS ]; then
f_show_msg "$msg_unable_to_get_file_from_selected_media" \
"$__path"
f_device_shutdown media
return $FAILURE
fi
f_show_info "$msg_located_index_now_reading_package_data_from_it"
if ! f_index_read "$__var_to_set"; then
f_show_msg "$msg_io_or_format_error_on_index_file" "$__path"
return $FAILURE
fi
_INDEX_INITTED=1
return $SUCCESS
}
# f_index_read [$var_to_get]
#
# Process the INDEX file (contents contained in $var_to_get) and...
#
# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts
# 2. For convenience, create $_npkgs holding the total number of all packages
# 3. extract associative categories for each package into $_categories_$varpkg
# 4. extract runtime dependencies for each package into $_rundeps_$varpkg
# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES
# 6. create $_npkgs_$varcat holding the total number of packages in category
#
# NOTE: $varpkg is the product of f_str2varname $package varpkg
# NOTE: $package is the name as it appears in the INDEX (no archive suffix)
# NOTE: We only show categories for which there are at least one package.
# NOTE: $varcat is the product of f_str2varname $category varcat
#
f_index_read()
{
local var_to_get="${1:-PACKAGE_INDEX}"
# Export variables required by awk(1) below
export msg_no_description_provided
export msg_all msg_all_desc
export VALID_VARNAME_CHARS
eval "$( debug= f_getvar "$var_to_get" | awk -F'|' '
function asorti(src, dest)
{
# Copy src indices to dest and calculate array length
nitems = 0; for (i in src) dest[++nitems] = i
# Sort the array of indices (dest) using insertion sort method
for (i = 1; i <= nitems; k = i++)
{
idx = dest[i]
while ((k > 0) && (dest[k] > idx))
{
dest[k+1] = dest[k]
k--
}
dest[k+1] = idx
}
return nitems
}
function print_category(category, npkgs, desc)
{
cat = category
# Accent the category if the first page has been
# cached (also acting as a visitation indicator)
if ( ENVIRON["_index_page_" varcat "_1"] )
cat = cat "*"
printf "'\''%s'\'' '\''%s packages'\'' '\''%s'\''\n",
cat, npkgs, desc
}
BEGIN {
valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
default_desc = ENVIRON["msg_no_description_provided"]
tpkgs = 0
prefix = ""
}
{
tpkgs++
varpkg = $1
gsub("[^" valid_chars "]", "_", varpkg)
print "_categories_" varpkg "=\"" $7 "\""
split($7, pkg_categories, /[[:space:]]+/)
for (pkg_category in pkg_categories)
categories[pkg_categories[pkg_category]]++
print "_rundeps_" varpkg "=\"" $9 "\""
}
END {
print "_npkgs=" tpkgs # For convenience, total package count
n = asorti(categories, categories_sorted)
# Produce package counts for each category
for (i = 1; i <= n; i++)
{
cat = varcat = categories_sorted[i]
npkgs = categories[cat]
gsub("[^" valid_chars "]", "_", varcat)
print "_npkgs_" varcat "=\"" npkgs "\""
}
# Create menu list and generate list of categories at same time
print "CATEGORY_MENU_LIST=\""
print_category(ENVIRON["msg_all"], tpkgs,
ENVIRON["msg_all_desc"])
category_list = ""
for (i = 1; i <= n; i++)
{
cat = varcat = categories_sorted[i]
npkgs = categories[cat]
cur_prefix = tolower(substr(cat, 1, 1))
if ( prefix != cur_prefix )
prefix = cur_prefix
else
cat = " " cat
gsub("[^" valid_chars "]", "_", varcat)
desc = ENVIRON["_category_" varcat]
if ( ! desc ) desc = default_desc
print_category(cat, npkgs, desc)
category_list = category_list " " cat
}
print "\""
# Produce the list of categories (calculated in above block)
sub(/^ /, "", category_list)
print "PACKAGE_CATEGORIES=\"" category_list "\""
}' )" # End-Quote
}
# f_index_extract_pages $var_to_get $var_basename $pagesize [$category]
#
# Extracts the package INDEX into a series of sequential variables
# corresponding to "pages" containing up to $pagesize packages. The package
# INDEX data must be contained in the variable $var_to_get. The extracted pages
# are stored in variables ${var_basename}_# -- where "#" is a the page number.
# If $category is set, only packages for that category are extracted.
# Otherwise, if $category is "All", missing, or NULL, all packages are
# extracted and no filtering is done.
#
f_index_extract_pages()
{
local var_to_get="$1" var_basename="$2" pagesize="$3"
local category="$4" # Optional
eval "$(
debug= f_getvar "$var_to_get" | awk -F'|' \
-v cat="$category" \
-v pagesize="$pagesize" \
-v var_basename="$var_basename" '
BEGIN { n = page = 0 }
/'\''/{ gsub(/'\''/, "'\''\\'\'\''") }
{
if ( cat !~ /(^$|^All$)/ && $7 !~ \
"(^|[[:space:]])" cat "([[:space:]]|$)" ) next
starting_new_page = (n++ == (pagesize * page))
if ( starting_new_page )
printf "%s%s", ( n > 1 ? "'\''\n" : "" ),
var_basename "_" ++page "='\''"
printf "%s%s", ( starting_new_page ? "" : "\n" ), $0
}
END { if ( n > 0 ) print "'\''" }'
)"
}
############################################################ MAIN
f_dprintf "%s: Successfully loaded." packages/index.subr
fi # ! $_PACKAGES_INDEX_SUBR
|