From: Michael Tremer Date: Sun, 23 May 2021 15:08:53 +0000 (+0000) Subject: parser: Add function to list all namespaces X-Git-Tag: 0.9.28~1285^2~94 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fa3cff8db6d18a10893dcac4c15778cc0942c099;p=pakfire.git parser: Add function to list all namespaces Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/parser.h b/src/libpakfire/include/pakfire/parser.h index f7313b472..d572a1f87 100644 --- a/src/libpakfire/include/pakfire/parser.h +++ b/src/libpakfire/include/pakfire/parser.h @@ -49,6 +49,7 @@ char* pakfire_parser_expand(PakfireParser parser, const char* namespace, const c char* pakfire_parser_get(PakfireParser parser, const char* namespace, const char* name); char** pakfire_parser_get_split(PakfireParser parser, const char* namespace, const char* name, char delim); +char** pakfire_parser_list_namespaces(PakfireParser parser, const char* filter); int pakfire_parser_merge(PakfireParser parser1, PakfireParser parser2); diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index dea1a6a57..48d51b9f3 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -239,6 +239,7 @@ global: pakfire_parser_get_namespace; pakfire_parser_get_parent; pakfire_parser_get_split; + pakfire_parser_list_namespaces; pakfire_parser_merge; pakfire_parser_parse; pakfire_parser_read; diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index e03eca14a..e6b6714f2 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -613,6 +614,62 @@ PAKFIRE_EXPORT char** pakfire_parser_get_split(PakfireParser parser, return list; } +PAKFIRE_EXPORT char** pakfire_parser_list_namespaces(PakfireParser parser, + const char* filter) { + char** namespaces = NULL; + unsigned int counter = 0; + + for (unsigned int i = 0; i < parser->num_declarations; i++) { + struct pakfire_parser_declaration* d = parser->declarations[i]; + + if (filter) { + int r = fnmatch(filter, d->namespace, 0); + + // Skip declaration if it does not match + if (r == FNM_NOMATCH) + continue; + + // Check for any errors + else if (r > 0) { + ERROR(parser->pakfire, "fnmatch failed: %s\n", strerror(errno)); + if (namespaces) + free(namespaces); + + return NULL; + } + } + + // Does this already exist on the list? + if (namespaces) { + int found = 0; + + for (const char** namespace = namespaces; *namespace; namespace++) { + if (strcmp(d->namespace, *namespace) == 0) { + found = 1; + break; + } + } + + // Skip adding if it already exists + if (found) + continue; + } + + namespaces = reallocarray(namespaces, counter + 2, sizeof(*namespaces)); + if (!namespaces) + return NULL; + + // Add namespace + namespaces[counter++] = d->namespace; + } + + // Terminate array + if (namespaces) + namespaces[counter] = NULL; + + return namespaces; +} + PAKFIRE_EXPORT int pakfire_parser_merge(PakfireParser parser1, PakfireParser parser2) { DEBUG(parser1->pakfire, "Merging parsers %p and %p\n", parser1, parser2); char namespace[NAME_MAX*2+1];