]> git.ipfire.org Git - pakfire.git/commitdiff
cli: provides: Upgrade to the new parser
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 12:22:04 +0000 (12:22 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 12:22:04 +0000 (12:22 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/provides.c
src/cli/lib/provides.h
src/cli/pakfire-builder.c

index 0552be37fe5e85cb94cf2588ca8c50fe93794e10..0e625b20e747b20f460ac0e0f0a2cf0e768ab31e 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <argp.h>
+
 #include <pakfire/pakfire.h>
 
+#include "command.h"
 #include "dump.h"
+#include "pakfire.h"
 #include "provides.h"
 
-int cli_provides(struct pakfire* pakfire, int argc, char* argv[]) {
+static const char* args_doc = "PATTERNS...";
+
+static const char* doc = "Search for packages that provide a certain pattern";
+
+#define MAX_PATTERNS 256
+
+struct config {
+       char* patterns[MAX_PATTERNS];
+       unsigned int num_patterns;
+};
+
+static error_t parse(int key, char* arg, void* data) {
+       struct config* config = data;
+
+       switch (key) {
+               case ARGP_KEY_ARG:
+                       if (config->num_patterns >= MAX_PATTERNS)
+                               return -ENOBUFS;
+
+                       config->patterns[config->num_patterns++] = arg;
+                       break;
+
+               default:
+                       return ARGP_ERR_UNKNOWN;
+       }
+
+       return 0;
+}
+
+int cli_provides(void* data, int argc, char* argv[]) {
+       struct pakfire* pakfire = NULL;
        struct pakfire_packagelist* list = NULL;
        int r;
 
+       struct cli_config* cli_config = data;
+
+       struct config config = {};
+
+       // Parse the command line
+       r = cli_parse(NULL, NULL, args_doc, doc, parse, argc, argv, &config);
+       if (r)
+               goto ERROR;
+
+       // Setup pakfire
+       r = cli_setup_pakfire(&pakfire, cli_config);
+       if (r)
+               goto ERROR;
+
        // Allocate a new packagelist
        r = pakfire_packagelist_create(&list, pakfire);
        if (r)
                goto ERROR;
 
        // Perform search
-       for (int i = 0; i < argc; i++) {
-               r = pakfire_whatprovides(pakfire, argv[i], 0, list);
+       for (unsigned int i = 0; i < config.num_patterns; i++) {
+               r = pakfire_whatprovides(pakfire, config.patterns[i], 0, list);
                if (r)
                        goto ERROR;
        }
@@ -45,6 +93,8 @@ int cli_provides(struct pakfire* pakfire, int argc, char* argv[]) {
 ERROR:
        if (list)
                pakfire_packagelist_unref(list);
+       if (pakfire)
+               pakfire_unref(pakfire);
 
        return r;
 }
index 2d21feb4e34bc0b234e40d0bb7873065455e4863..8d2736ae628be42f5a74344ee9b55089ba40a4b1 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CLI_PROVIDES_H
 #define PAKFIRE_CLI_PROVIDES_H
 
-#include <pakfire/pakfire.h>
-
-int cli_provides(struct pakfire* pakfire, int argc, char* argv[]);
+int cli_provides(void* data, int argc, char* argv[]);
 
 #endif /* PAKFIRE_CLI_PROVIDES_H */
index a7b8f7df42640cd42fbcf7723c745248ae69e7a1..d95636a26c3df842c80a2ea1311763794e4c11d7 100644 (file)
@@ -64,14 +64,14 @@ static struct argp_option options[] = {
 };
 
 static const struct command commands[] = {
-       { "build",    cli_build,  1, -1, 0 },
-       { "clean",    cli_clean,  0,  0, 0 },
-       { "dist",     cli_dist,   1, -1, 0 },
-       { "image",    cli_image, -1, -1, 0 },
-       { "info",     cli_info,   1, -1, 0 },
-       { "search",   cli_search, 1, -1, 0 },
+       { "build",    cli_build,    1, -1, 0 },
+       { "clean",    cli_clean,    0,  0, 0 },
+       { "dist",     cli_dist,     1, -1, 0 },
+       { "image",    cli_image,   -1, -1, 0 },
+       { "info",     cli_info,     1, -1, 0 },
+       { "provides", cli_provides, 1, -1, 0 },
+       { "search",   cli_search,   1, -1, 0 },
 #if 0
-       { "provides", 0, cli_provides },
        { "repo",     0, cli_repo },
        { "repolist", 0, cli_repolist },
        { "requires", 0, cli_requires },