***/
#include <stdlib.h>
-#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <ctype.h>
#include "conf-files.h"
#include "strv.h"
#include "mkdir.h"
-#include "fileio.h"
+#include "verbs.h"
+#include "build.h"
#include "hwdb-internal.h"
#include "hwdb-util.h"
}
log_debug("=== trie on-disk ===");
- log_debug("size: %8"PRIu64" bytes", size);
+ log_debug("size: %8"PRIi64" bytes", size);
log_debug("header: %8zu bytes", sizeof(struct trie_header_f));
log_debug("nodes: %8"PRIu64" bytes (%8"PRIu64")",
t.nodes_count * sizeof(struct trie_node_f), t.nodes_count);
return 0;
}
-static int hwdb_query(char **args, unsigned n) {
+static int hwdb_query(int argc, char *argv[], void *userdata) {
_cleanup_hwdb_unref_ sd_hwdb *hwdb = NULL;
const char *key, *value;
const char *modalias;
int r;
- assert(args);
- assert(n == 2);
+ assert(argc >= 2);
+ assert(argv);
- modalias = args[1];
+ modalias = argv[1];
r = sd_hwdb_new(&hwdb);
if (r < 0)
return 0;
}
-static int hwdb_update(char **args, unsigned n) {
+static int hwdb_update(int argc, char *argv[], void *userdata) {
_cleanup_free_ char *hwdb_bin = NULL;
_cleanup_(trie_freep) struct trie *trie = NULL;
char **files, **f;
static void help(void) {
printf("Usage: %s OPTIONS COMMAND\n\n"
- "Options:\n"
- " --usr generate in " UDEVLIBEXECDIR " instead of /etc/udev\n"
- " -r,--root=PATH alternative root path in the filesystem\n"
- " -h,--help\n\n"
+ "Update or query the hardware database.\n\n"
+ " -h --help Show this help\n"
+ " --version Show package version\n"
+ " --usr Generate in " UDEVLIBEXECDIR " instead of /etc/udev\n"
+ " -r --root=PATH Alternative root path in the filesystem\n\n"
"Commands:\n"
- " update update the hwdb database\n"
- " query MODALIAS query database and print result\n\n",
+ " update Update the hwdb database\n"
+ " query MODALIAS Query database and print result\n",
program_invocation_short_name);
}
static int parse_argv(int argc, char *argv[]) {
enum {
- ARG_USR = 0x100,
+ ARG_VERSION = 0x100,
+ ARG_USR,
};
static const struct option options[] = {
- { "usr", no_argument, NULL, ARG_USR },
- { "root", required_argument, NULL, 'r' },
- { "help", no_argument, NULL, 'h' },
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, ARG_VERSION },
+ { "usr", no_argument, NULL, ARG_USR },
+ { "root", required_argument, NULL, 'r' },
{}
};
while ((c = getopt_long(argc, argv, "ut:r:h", options, NULL)) >= 0) {
switch(c) {
+
+ case 'h':
+ help();
+ return 0;
+
+ case ARG_VERSION:
+ puts(PACKAGE_STRING);
+ puts(SYSTEMD_FEATURES);
+ return 0;
+
case ARG_USR:
arg_hwdb_bin_dir = UDEVLIBEXECDIR;
break;
+
case 'r':
arg_root = optarg;
break;
- case 'h':
- help();
- return 0;
+
case '?':
return -EINVAL;
+
default:
assert_not_reached("Unknown option");
}
}
static int hwdb_main(int argc, char *argv[]) {
- static const struct {
- const char *verb;
- const enum {
- MORE,
- LESS,
- EQUAL,
- } argc_cmp;
- const int argc;
- int (*const dispatch)(char **args, unsigned n);
- } verbs[] = {
- { "update", EQUAL, 1, hwdb_update },
- { "query", EQUAL, 2, hwdb_query },
+ const Verb verbs[] = {
+ { "update", 1, 1, 0, hwdb_update },
+ { "query", 2, 2, 0, hwdb_query },
+ {},
};
- int left;
- unsigned i;
-
- assert(argc >= 0);
- assert(argv);
-
- left = argc - optind;
-
- if (left <= 0) {
- log_error("Missing command.");
- help();
- return -EINVAL;
- }
-
- for (i = 0; i < ELEMENTSOF(verbs); i++)
- if (streq(argv[optind], verbs[i].verb))
- break;
-
- if (i >= ELEMENTSOF(verbs)) {
- log_error("Unknown command %s.", argv[optind]);
- help();
- return -EINVAL;
- }
-
- switch (verbs[i].argc_cmp) {
- case EQUAL:
- if (left != verbs[i].argc) {
- log_error("Invalid number of arguments.");
- help();
- return -EINVAL;
- }
-
- break;
-
- case MORE:
- if (left < verbs[i].argc) {
- log_error("Too few arguments.");
- help();
- return -EINVAL;
- }
-
- break;
- case LESS:
- if (left > verbs[i].argc) {
- log_error("Too many arguments.");
- help();
- return -EINVAL;
- }
-
- break;
- default:
- assert_not_reached("Unknown comparison operator.");
- }
-
- return verbs[i].dispatch(argv + optind, left);
+ return dispatch_verb(argc, argv, verbs, NULL);
}
int main (int argc, char *argv[]) {