# #
#############################################################################*/
-#include <errno.h>
-#include <getopt.h>
-#include <stdlib.h>
+#include <argp.h>
#include "command.h"
+#include "pakfire.h"
#include "shell.h"
#include <pakfire/build.h>
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;
}
{ "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 },
};