]> git.ipfire.org Git - pakfire.git/commitdiff
parser: Add function to list all namespaces
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 23 May 2021 15:08:53 +0000 (15:08 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 23 May 2021 15:08:53 +0000 (15:08 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/parser.h
src/libpakfire/libpakfire.sym
src/libpakfire/parser.c

index f7313b472755d694aeef4fd82468c3cd3e9f894d..d572a1f879ff0d72a134b6cbf548e1dd18e1f587 100644 (file)
@@ -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);
 
index dea1a6a57832ceca006f0339c20fd58405bc2a24..48d51b9f3694954a9c830eaca64378aa23e405bd 100644 (file)
@@ -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;
index e03eca14a6aa0cb7061c72258d634f8b254dc202..e6b6714f28ecc13c0fd3961005068336399f139f 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <fnmatch.h>
 #include <linux/limits.h>
 #include <regex.h>
 #include <stdlib.h>
@@ -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];