Skip to content

Commit c874c15

Browse files
committed
bin/xbps-create: use process_dict() for alternatives too
1 parent ad9bcef commit c874c15

File tree

1 file changed

+49
-69
lines changed

1 file changed

+49
-69
lines changed

bin/xbps-create/main.c

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,54 @@ process_array(const char *key, const char *val, bool (*validate)(const char *s))
233233
xbps_object_release(array);
234234
}
235235

236+
static void
237+
process_keyval_array(const char *prop, const char *keyval, const char delim,
238+
bool (*validate_key)(const char *),
239+
bool (*validate_val)(const char *))
240+
{
241+
xbps_dictionary_t d;
242+
xbps_array_t a;
243+
char *key, *valstr;
244+
bool alloc = false;
245+
246+
if ((d = xbps_dictionary_get(pkg_propsd, prop)) == NULL) {
247+
d = xbps_dictionary_create();
248+
if (d == NULL)
249+
die("xbps_dictionary_create");
250+
alloc = true;
251+
}
252+
253+
key = strdup(keyval);
254+
if (key == NULL)
255+
die("strdup");
256+
valstr = strchr(key, delim);
257+
*valstr = '\0';
258+
valstr = valstr + 1;
259+
assert(valstr);
260+
261+
if (validate_key && !validate_key(key)) {
262+
diex("%s: invalid key: %s", prop, key);
263+
}
264+
265+
if ((a = xbps_dictionary_get(d, key)) == NULL) {
266+
a = xbps_array_create();
267+
if (a == NULL)
268+
die("xbps_array_create");
269+
}
270+
271+
if (validate_val && !validate_val(valstr)) {
272+
diex("%s: invalid value for key `%s': %s", prop, key, valstr);
273+
}
274+
275+
xbps_array_add_cstring(a, valstr);
276+
xbps_dictionary_set(d, key, a);
277+
xbps_dictionary_set(pkg_propsd, prop, d);
278+
if (alloc) {
279+
xbps_object_release(a);
280+
xbps_object_release(d);
281+
}
282+
}
283+
236284
static void
237285
process_keyval_uint64(const char *prop, const char *keyval, const char delim,
238286
bool (*validate_key)(const char *),
@@ -314,74 +362,6 @@ process_dict(const char *key, const char *val, const char delim,
314362
free(args);
315363
}
316364

317-
static void
318-
process_one_alternative(const char *altgrname, const char *val)
319-
{
320-
xbps_dictionary_t d;
321-
xbps_array_t a;
322-
char *altfiles;
323-
bool alloc = false;
324-
325-
if ((d = xbps_dictionary_get(pkg_propsd, "alternatives")) == NULL) {
326-
d = xbps_dictionary_create();
327-
if (d == NULL)
328-
die("xbps_dictionary_create");
329-
alloc = true;
330-
}
331-
if ((a = xbps_dictionary_get(d, altgrname)) == NULL) {
332-
a = xbps_array_create();
333-
if (a == NULL)
334-
die("xbps_array_create");
335-
}
336-
altfiles = strchr(val, ':') + 1;
337-
assert(altfiles);
338-
339-
xbps_array_add_cstring(a, altfiles);
340-
xbps_dictionary_set(d, altgrname, a);
341-
xbps_dictionary_set(pkg_propsd, "alternatives", d);
342-
343-
if (alloc) {
344-
xbps_object_release(a);
345-
xbps_object_release(d);
346-
}
347-
}
348-
349-
350-
static void
351-
process_dict_of_arrays(const char *key UNUSED, const char *val)
352-
{
353-
char *altgrname, *args, *p, *saveptr;
354-
355-
assert(key);
356-
357-
if (val == NULL)
358-
return;
359-
360-
args = strdup(val);
361-
assert(args);
362-
363-
if (strchr(args, ' ') == NULL) {
364-
altgrname = strtok(args, ":");
365-
assert(altgrname);
366-
process_one_alternative(altgrname, val);
367-
goto out;
368-
}
369-
370-
for ((p = strtok_r(args, " ", &saveptr)); p;
371-
(p = strtok_r(NULL, " ", &saveptr))) {
372-
char *b;
373-
374-
b = strdup(p);
375-
assert(b);
376-
altgrname = strtok(b, ":");
377-
assert(altgrname);
378-
process_one_alternative(altgrname, p);
379-
free(b);
380-
}
381-
out:
382-
free(args);
383-
}
384-
385365
static void
386366
process_file(const char *file, const char *key)
387367
{
@@ -1164,7 +1144,7 @@ main(int argc, char **argv)
11641144
process_array("reverts", reverts, NULL);
11651145
process_array("shlib-provides", shlib_provides, NULL);
11661146
process_array("shlib-requires", shlib_requires, NULL);
1167-
process_dict_of_arrays("alternatives", alternatives);
1147+
process_dict("alternatives", alternatives, ':', process_keyval_array, NULL, NULL);
11681148

11691149
/* save cwd */
11701150
memset(&cwd, 0, sizeof(cwd));

0 commit comments

Comments
 (0)