Skip to content

Commit ed25ebe

Browse files
committed
bin/xbps-create: use process_dict() for alternatives too
1 parent 56be05c commit ed25ebe

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 for key `%s': %s", prop, key, 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 *),
@@ -312,74 +359,6 @@ process_dict(const char *key, const char *val, const char delim,
312359
free(args);
313360
}
314361

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

11671146
/* save cwd */
11681147
memset(&cwd, 0, sizeof(cwd));

0 commit comments

Comments
 (0)