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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
# Functions for parsing and validating package metadata
#
# Copyright (C) 2012, 2014, 2018 Patrick McDermott
#
# This file is part of opkbuild.
#
# opkbuild is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# opkbuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with opkbuild. If not, see <http://www.gnu.org/licenses/>.
_ob_metadata_do()
{
local func=
func="${1}"
shift 1
"_ob_${func}" "${@}"
return ${?}
}
## @brief Validate a source package name
## @details \fBob_validate_source_name\fP validates a source package name
## against the rules of the metadata plugin selected at libopkbuild's
## build time.
## @operand name req The source package name to validate.
## @return Returns 0 if valid, 1 if invalid, or 125 if given an incorrect number
## of arguments.
## @pure yes This function has no side effects.
ob_validate_source_name()
{
local name=
if [ ${#} -eq 1 ]; then
name="${1}"
else
return 125
fi
_ob_metadata_do 'validate_source_name' "${name}"
return ${?}
}
## @brief Validate a binary package name
## @details \fBob_validate_binary_name\fP validates a binary package name
## against the rules of the metadata plugin selected at libopkbuild's
## build time.
## @operand name req The binary package name to validate.
## @return Returns 0 if valid, 1 if invalid, or 125 if given an incorrect number
## of arguments.
## @pure yes This function has no side effects.
ob_validate_binary_name()
{
local name=
if [ ${#} -eq 1 ]; then
name="${1}"
else
return 125
fi
_ob_metadata_do 'validate_binary_name' "${name}"
return ${?}
}
## @brief Parse a source package version
## @details \fBob_parse_version\fP validates and parses a source package version
## identifier using the metadata plugin selected at libopkbuild's build
## time. Parsing is limited to extracting the entire upstream version
## and distribution revision.
## @option -u upstreamver_var The name of the variable in which to store the
## upstream version.
## @option -d distrev_var The name of the variable in which to store the
## distribution revision.
## @operand version req The version to validate and parse.
## @return Returns 0 on success; 1 on invalid \fIversion\fP; or 125 if given an
## invalid variable name as an argument to \fB-u\fP or \fB-d\fP, an
## invalid option, or an incorrect number of operands.
## @pure maybe This function has side effects when used with either or both of
## the \fB-u\fP and \fB-d\fP options. Without these options, this
## function only validates a version identifier. The purpose of
## this function is mainly to extract version components using these
## options, so in practice this function is generally not
## subshell-safe.
ob_parse_version()
{
local opt=
local upstreamver_var=
local distrev_var=
local version=
OPTIND=1
while getopts 'u:d:' opt; do
case "${opt}" in
u)
upstreamver_var="${OPTARG}"
if ! _ob_validate_var_name "${upstreamver_var}"; then
return 125
fi
;;
d)
distrev_var="${OPTARG}"
if ! _ob_validate_var_name "${distrev_var}"; then
return 125
fi
;;
?)
return 125
;;
esac
done
shift $(($OPTIND - 1))
if [ ${#} -eq 1 ]; then
version="${1}"
else
return 125
fi
if ! _ob_metadata_do 'validate_version' "${version}"; then
return 1
fi
if [ -n "${upstreamver_var}" ]; then
eval ${upstreamver_var}="$(_ob_metadata_do 'get_upstreamver' \
"${version}")"
fi
if [ -n "${distrev_var}" ]; then
eval ${distrev_var}="$(_ob_metadata_do 'get_distrev' \
"${version}")"
fi
return 0
}
## @brief Get the running system's architecture
## @details \fBob_get_system_arch\fP gets the architecture of the build
## (running) system using the metadata plugin selected at libopkbuild's
## build time.
## @return Returns 0 on success, 1 if the system's architecture cannot be
## determined, or 125 if given any arguments.
## @stdout Prints the system's architecture.
## @pure yes This function has no side effects.
ob_get_system_arch()
{
if [ ${#} -gt 0 ]; then
return 125
fi
_ob_metadata_do 'get_system_arch'
return ${?}
}
## @brief Get the running system's platform
## @details \fBob_get_system_arch\fP gets the platform of the build (running)
## system using the metadata plugin selected at libopkbuild's build
## time.
## @return Returns 0 on success, 1 if the system's platform cannot be
## determined, or 125 if given any arguments.
## @stdout Prints the system's platform.
## @pure yes This function has no side effects.
ob_get_system_plat()
{
if [ ${#} -gt 0 ]; then
return 125
fi
_ob_metadata_do 'get_system_plat'
return ${?}
}
ob_match_arch()
{
local arch=
local arch_field=
local a=
local a_ere=
if [ ${#} -eq 2 ]; then
arch="${1}"
arch_field="${2}"
else
return 125
fi
for a in ${arch_field}; do
# "all" == "all"
if [ "${arch}" = 'all' ]; then
if [ "${a}" = 'all' ]; then
return 0
else
continue
fi
fi
# "foo-bar-baz" == "any"
if [ "${a}" = 'any' ]; then
return 0
fi
# Generate an ERE to match hyphenated architectures.
a_ere="$(echo "${a}" | \
sed 's/^any-/[^-]+-/;s/-any-/-[^-]+-/g;s/-any$/-[^-]+/g')"
# "foo-bar-baz" == "foo-any-any"
if echo "${arch}" | grep -E "${a_ere}" >/dev/null 2>&1; then
return 0
fi
done
# Nothing matched.
return 1
}
ob_match_plat()
{
local plat=
local plat_field=
local p=
if [ ${#} -eq 2 ]; then
plat="${1}"
plat_field="${2}"
else
return 125
fi
for p in ${plat_field}; do
if [ "${plat}" = 'all' ]; then
if [ "${p}" = 'all' ]; then
return 0
else
continue
fi
fi
if [ "${p}" = 'any' ]; then
return 0
fi
if [ "${p}" = "${plat}" ]; then
return 0
fi
done
return 1
}
ob_arch_is_concerned()
{
local host_arch=
local arches=
local arch=
local not_arch=
local seen_arch=
if [ ${#} -eq 2 ]; then
host_arch="${1}"
arches="${2}"
else
return 125
fi
if [ "x${arches}" = 'x' ]; then
return 0
elif [ "x${host_arch}" = 'xall' ]; then
if [ "x${arches}" = 'xall' ]; then
return 0
else
return 1
fi
else
seen_arch=1
for arch in ${arches}; do
not_arch="${arch#!}"
if [ "${not_arch}" != "${arch}" ]; then
if ob_match_arch "${host_arch}" "${not_arch}"
then
seen_arch=1
break
else
seen_arch=0
fi
elif ob_match_arch "${host_arch}" "${arch}"; then
seen_arch=0
break
fi
done
return ${seen_arch}
fi
}
ob_plat_is_concerned()
{
local host_plat=
local plats=
local plat=
local not_plat=
local seen_plat=
if [ ${#} -eq 2 ]; then
host_plat="${1}"
plats="${2}"
else
return 125
fi
if [ "x${plats}" = 'x' ]; then
return 0
elif [ "x${host_plat}" = 'xall' ]; then
if [ "x${plats}" = 'xall' ]; then
return 0
else
return 1
fi
else
seen_plat=1
for plat in ${plats}; do
not_plat="${plat#!}"
if [ "${not_plat}" != "${plat}" ]; then
if ob_match_plat "${host_plat}" "${not_plat}"
then
seen_plat=1
break
else
seen_plat=0
fi
elif ob_match_plat "${host_plat}" "${plat}"; then
seen_plat=0
break
fi
done
return ${seen_plat}
fi
}
## @brief Get a system path
## @details \fBob_get_system_path\fP gets a system path consisting of \fIargs\fP
## formatted, by the metadata plugin selected at libopkbuild's build
## time, according to a path format identified by \fIpath_id\fP. The
## arguments for a \fIpath_id\fP of either \fBpackage-source\fP or
## \fBpackage-docs\fP are \fIsource\fP and \fIversion\fP. For
## \fBbuildflags\fP the argument is \fI arch\fP. For \fBplatconf\fP
## they are \fIsource\fP, \fIversion\fP, and \fIplat\fP.
## @operand path_id req One of \fBpackage-source\fP, \fBpackage-docs\fP,
## \fBbuildflags\fP, or \fBplatconf\fP.
## @operand args req Additional arguments specific to each \fIpath_id\fP.
## @return Returns 0 on success, or 125 if given an incorrect number of
## arguments.
## @stdout Prints the requested path with \fIargs\fP.
## @pure yes This function has no side effects.
ob_get_system_path()
{
local path_id=
if [ ${#} -ge 1 ]; then
path_id="${1}"
shift 1
else
return 125
fi
_ob_metadata_do 'get_system_path' "${path_id}" "${@}"
return ${?}
}
|