From: Michael Tremer Date: Sun, 15 Oct 2023 12:52:28 +0000 (+0000) Subject: cli: shell: Upgrade to the new parser X-Git-Tag: 0.9.30~1507 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=15b7db71c92c268f549058f352c932539e811a84;p=pakfire.git cli: shell: Upgrade to the new parser Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/shell.c b/src/cli/lib/shell.c index 982de8e35..94c1e5b22 100644 --- a/src/cli/lib/shell.c +++ b/src/cli/lib/shell.c @@ -18,11 +18,10 @@ # # #############################################################################*/ -#include -#include -#include +#include #include "command.h" +#include "pakfire.h" #include "shell.h" #include @@ -32,79 +31,78 @@ struct config { int flags; + + // Packages const char* packages[MAX_PACKAGES]; unsigned int num_packages; }; -static void help(void) { - printf( - "%s [OPTIONS...] shell [OPTIONS...]\n\n" - "Options:\n" - " --disable-snapshot Do not use the snapshot\n" - " --install PACKAGE Installs the given package\n" - " -h --help Show help\n", - program_invocation_short_name - ); - - exit(0); -} - -static int parse_argv(struct config* config, int argc, char* argv[]) { - enum { - ARG_DISABLE_SNAPSHOT, - ARG_INSTALL, - }; - - static const struct option options[] = { - { "disable-snapshot", no_argument, NULL, ARG_DISABLE_SNAPSHOT }, - { "help", no_argument, NULL, 'h' }, - { "install", required_argument, NULL, ARG_INSTALL }, - { NULL }, - }; - int c; +static const char* doc = + "Creates a build environment and launches an interactive shell in it"; - for (;;) { - c = getopt_long(argc, argv, "h", options, NULL); - if (c < 0) - break; +enum { + OPT_DISABLE_SNAPSHOT = 1, + OPT_INSTALL = 2, +}; - switch (c) { - case 'h': - help(); +static struct argp_option options[] = { + { "disable-snapshot", OPT_DISABLE_SNAPSHOT, NULL, 0, "Do not use the snapshot", 0 }, + { "install", OPT_INSTALL, "PACKAGE", 0, "Install this package", 0 }, + { NULL }, +}; - case ARG_DISABLE_SNAPSHOT: - config->flags |= PAKFIRE_BUILD_DISABLE_SNAPSHOT; - break; +static error_t parse(int key, char* arg, void* data) { + struct config* config = data; - case ARG_INSTALL: - if (config->num_packages >= MAX_PACKAGES) - return -ENOBUFS; + switch (key) { + case OPT_DISABLE_SNAPSHOT: + config->flags |= PAKFIRE_BUILD_DISABLE_SNAPSHOT; + break; - config->packages[config->num_packages++] = optarg; - break; + case ARGP_KEY_ARG: + if (config->num_packages >= MAX_PACKAGES) + return -ENOBUFS; - case '?': - return -EINVAL; + config->packages[config->num_packages++] = arg; + break; - default: - break; - } + default: + return ARGP_ERR_UNKNOWN; } return 0; } -int cli_shell(struct pakfire* pakfire, int argc, char* argv[]) { +int cli_shell(void* data, int argc, char* argv[]) { + struct pakfire* pakfire = NULL; + int r; + + struct cli_config* cli_config = data; + struct config config = { - .packages = { NULL }, + .flags = 0, + .packages = { NULL }, .num_packages = 0, }; - int r; - // Parse commandline - r = parse_argv(&config, argc, argv); + // Parse the command line + r = cli_parse(options, NULL, NULL, doc, parse, argc, argv, &config); if (r) - return r; + goto ERROR; + + // Setup pakfire + r = cli_setup_pakfire(&pakfire, cli_config); + if (r) + goto ERROR; + + // Run the shell + r = pakfire_shell(pakfire, config.packages, config.flags); + if (r) + goto ERROR; + +ERROR: + if (pakfire) + pakfire_unref(pakfire); - return pakfire_shell(pakfire, config.packages, config.flags); + return r; } diff --git a/src/cli/lib/shell.h b/src/cli/lib/shell.h index 1bc6fe735..3f780bdea 100644 --- a/src/cli/lib/shell.h +++ b/src/cli/lib/shell.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_CLI_SHELL_H #define PAKFIRE_CLI_SHELL_H -#include - -int cli_shell(struct pakfire* pakfire, int argc, char* argv[]); +int cli_shell(void* data, int argc, char* argv[]); #endif /* PAKFIRE_CLI_SHELL_H */ diff --git a/src/cli/pakfire-builder.c b/src/cli/pakfire-builder.c index 67d1efd44..7e1a4e253 100644 --- a/src/cli/pakfire-builder.c +++ b/src/cli/pakfire-builder.c @@ -73,11 +73,8 @@ static const struct command commands[] = { { "repo", cli_repo, -1, -1, 0 }, { "repolist", cli_repolist, 0, 0, 0 }, { "requires", cli_requires, 1, -1, 0 }, + { "shell", cli_shell, 0, 0, 0 }, { "search", cli_search, 1, -1, 0 }, - -#if 0 - { "shell", 0, cli_shell }, -#endif { NULL }, };