blob: abbb2f2426e263cb771403eab4e46d588911db62 (
plain)
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
|
# opkbuild
# lib/control
# Functions for parsing control files.
#
# Copyright (C) 2012 Patrick "P. J." McDermott
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
[ -n "${_OB_CONTROL_SM}" ] && return 0
_OB_CONTROL_SM='true'
ob_use messages
ob_use locale
_OB_SUBSTVARS_MAX_DEPTH=50
OB_CONTROL_NAME=
OB_CONTROL_VALUE=
ob_parse_control()
{
_ob_local _obpco_file _obpco_field_cb _obpco_req_fields _obpco_opt_fields \
_obpco_all_fields _obpco_got_fields \
_obpco_line_nr _obpco_line _obpco_name _obpco_value
if [ ${#} -eq 2 ]; then
_obpco_file="${1}"
_obpco_field_cb="${2}"
_obpco_check_fields='false'
elif [ ${#} -eq 4 ]; then
_obpco_file="${1}"
_obpco_field_cb="${2}"
_obpco_req_fields="${3}"
_obpco_opt_fields="${4}"
_obpco_check_fields='true'
_obpco_all_fields=" ${_obpco_req_fields} ${_obpco_opt_fields} "
else
_ob_return 125
return ${?}
fi
_obpco_got_fields=' '
_obpco_line_nr=0
while IFS= read -r _obpco_line; do
_obpco_line_nr=$(($_obpco_line_nr + 1))
if [ -z "$(echo ${_obpco_line})" ]; then
_ob_parse_control_error "${_obpco_file}" "${_obpco_line_nr}" \
'control_empty_line'
elif [ "${_obpco_line# }" = "${_obpco_line}" ]; then
# "Name: Value" line.
if [ -n "${_obpco_name}" ]; then
OB_CONTROL_NAME="${_obpco_name}"
OB_CONTROL_VALUE="${_obpco_value}"
"${_obpco_field_cb}"
if [ ${?} -ne 0 ]; then
_ob_return 0
return ${?}
fi
fi
_obpco_name="${_obpco_line%%:*}"
_obpco_value="${_obpco_line#*:}"
_obpco_value="${_obpco_value# }"
if [ -z "${_obpco_name}" \
-o "${_obpco_name}" = "${_obpco_line}" ]; then
# Badly formatted control field.
_ob_parse_control_error "${_obpco_file}" "${_obpco_line_nr}" \
'control_bad_nv'
continue
fi
if ${_obpco_check_fields}; then
if [ "${_obpco_all_fields% ${_obpco_name} *}" = \
"${_obpco_all_fields}" ]; then
# Unknown field.
_ob_parse_control_error \
"${_obpco_file}" "${_obpco_line_nr}" \
'control_unknown_field' "${_obpco_name}"
else
# Remove field from list of required fields.
_obpco_req_fields="$(echo "${_obpco_req_fields}" | \
sed "s/${_obpco_name}//")"
fi
fi
if [ "${_obpco_got_fields% ${_obpco_name} *}" != \
"${_obpco_got_fields}" ]; then
# Duplicate field.
_ob_parse_control_error "${_obpco_file}" "${_obpco_line_nr}" \
'control_duplicate_field' "${_obpco_name}"
else
_obpco_got_fields="${_obpco_got_fields}${_obpco_name} "
fi
else
# Continuation line.
if [ -z "${_obpco_name}" ]; then
# Expecting a "Name: Value" line.
_ob_parse_control_error "${_obpco_file}" "${_obpco_line_nr}" \
'control_found_continuation'
continue
fi
_obpco_value="${_obpco_value}
${_obpco_line# }"
fi
done <"${_obpco_file}"
if [ -n "${_obpco_name}" ]; then
OB_CONTROL_NAME="${_obpco_name}"
OB_CONTROL_VALUE="${_obpco_value}"
"${_obpco_field_cb}"
if [ ${?} -ne 0 ]; then
_ob_return 0
return ${?}
fi
fi
if ${_obpco_check_fields}; then
_obpco_req_fields="${_obpco_req_fields## }"
_obpco_req_fields="${_obpco_req_fields%% }"
if [ -n "${_obpco_req_fields}" ]; then
# Missing required control fields.
_obpco_req_fields="$(echo "${_obpco_req_fields}" | \
sed 's/ / /g' | \
sed "s/ /$(ob_get_msg 'list_item_separator')/g")"
_ob_parse_control_error "${_obpco_file}" '0' \
'control_missing_fields' \
"${_obpco_req_fields}"
fi
fi
_ob_return 0
return ${?}
}
ob_set_substvar()
{
_ob_local _obssv_name _obssv_value
if [ ${#} -eq 2 ]; then
_obssv_name="${1}"
_obssv_value="${2}"
else
_ob_return 125
return ${?}
fi
# Trim leading and trailing whitespace from value.
_obssv_value="$(echo "${_obssv_value}" | sed -n '
H; # Store each input line in the hold space.
${ # At the last line of input, …
g; # … restore the hold space into the pattern space, …
s/^[ \t\n]*//; # … remove leading whitespace, …
s/[ \t\n]*$//; # … remove trailing whitespace, and …
p; # … print the results.
};
')"
_obssv_name="$(echo "${_obssv_name}" | tr 'a-z-' 'A-Z_')"
eval "_OB_SUBSTVAR_${_obssv_name}='${_obssv_value}'"
_ob_return 125
return ${?}
}
ob_substvars()
{
_ob_local _obsv_string _obsv_depth \
_obsv_lhs _obsv_name _obsv_rhs _obsv_old_rhs _obsv_value
if [ ${#} -eq 1 ]; then
_obsv_string="${1}"
else
_ob_return 125
return ${?}
fi
# Logic inspired by that of dpkg's Dpkg::Substvars::substvars() subroutine.
_obsv_depth=0
while true; do
_obsv_lhs="${_obsv_string%%\$\{*}"
if [ ${#_obsv_lhs} -eq ${#_obsv_string} ]; then
# No "${" was found.
break
fi
_obsv_string="${_obsv_string#*\$\{}"
_obsv_name="${_obsv_string%%\}*}"
_obsv_rhs="${_obsv_string#*\}}"
if [ ${#_obsv_rhs} -lt ${#_obsv_old_rhs} ]; then
# Reset the nesting counter if we've advanced the right side of the
# matched space.
_obsv_depth=0
fi
if [ ${_obsv_depth} -ge ${_OB_SUBSTVARS_MAX_DEPTH} ]; then
# Warn of possible recursion.
ob_warn "$(ob_get_msg 'substvar_deep_nesting')"
_ob_return 1
return ${?}
fi
_obsv_old_rhs="${_obsv_rhs}"
# Perform the substitution.
_obsv_name="$(echo "${_obsv_name}" | tr 'a-z-' 'A-Z_')"
_obsv_value="$(eval echo \"\$\{"_OB_SUBSTVAR_${_obsv_name}"\}\")"
_obsv_string="${_obsv_lhs}${_obsv_value}${_obsv_rhs}"
_obsv_depth=$(($_obsv_depth + 1))
done
printf '%s\n' "${_obsv_string}"
_ob_return 0
return ${?}
}
_ob_parse_control_error()
{
_ob_local _obpcoe_file _obpcoe_line_nr _obpcoe_msg_id \
_obpcoe_file_info _obpcoe_orig_text_domain
_obpcoe_file="${1}"
_obpcoe_line_nr="${2}"
_obpcoe_msg_id="${3}"
shift 3
if [ "${_obpcoe_line_nr}" -eq 0 ]; then
_obpcoe_file_info="$(printf '%20s:' "${_obpcoe_file}")"
else
_obpcoe_file_info="$(printf '%20s(l%d):' "${_obpcoe_file}" \
"${_obpcoe_line_nr}")"
fi
_obpcoe_orig_text_domain="$(ob_get_text_domain)"
ob_set_text_domain "${_OB_INTERNAL_TEXT_DOMAIN}"
ob_warn "${_obpcoe_file_info} $(ob_get_msg "${_obpcoe_msg_id}")" "${@}"
ob_set_text_domain "${_obpcoe_orig_text_domain}"
_ob_return 0
return ${?}
}
|