Skip to content

Commit 833f6f2

Browse files
committed
bin/xbps-create: use process_dict() for alternatives too
1 parent d5c18ce commit 833f6f2

File tree

1 file changed

+48
-69
lines changed

1 file changed

+48
-69
lines changed

bin/xbps-create/main.c

Lines changed: 48 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,53 @@ 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+
xbps_dictionary_t d;
241+
xbps_array_t a;
242+
char *key, *valstr;
243+
bool alloc = false;
244+
245+
if ((d = xbps_dictionary_get(pkg_propsd, prop)) == NULL) {
246+
d = xbps_dictionary_create();
247+
if (d == NULL)
248+
die("xbps_dictionary_create");
249+
alloc = true;
250+
}
251+
252+
key = strdup(keyval);
253+
if (key == NULL)
254+
die("strdup");
255+
valstr = strchr(key, delim);
256+
*valstr = '\0';
257+
valstr = valstr + 1;
258+
assert(valstr);
259+
260+
if (validate_key && !validate_key(key)) {
261+
diex("%s: invalid key: %s", prop, key);
262+
}
263+
264+
if ((a = xbps_dictionary_get(d, key)) == NULL) {
265+
a = xbps_array_create();
266+
if (a == NULL)
267+
die("xbps_array_create");
268+
}
269+
270+
if (validate_val && !validate_val(valstr)) {
271+
diex("%s: invalid value: %s", prop, valstr);
272+
}
273+
274+
xbps_array_add_cstring(a, valstr);
275+
xbps_dictionary_set(d, key, a);
276+
xbps_dictionary_set(pkg_propsd, prop, d);
277+
if (alloc) {
278+
xbps_object_release(a);
279+
xbps_object_release(d);
280+
}
281+
}
282+
236283
static void
237284
process_keyval_uint64(const char *prop, const char *keyval, const char delim,
238285
bool (*validate_key)(const char *),
@@ -309,74 +356,6 @@ process_dict(const char *key, const char *val, const char delim,
309356
free(args);
310357
}
311358

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

11641143
/* save cwd */
11651144
memset(&cwd, 0, sizeof(cwd));

0 commit comments

Comments
 (0)