X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fhwdb%2Fhwdb.c;h=446de3a2fc330f4f10d52fec816e5d376ee719ea;hb=13d28f0bcf497b912ec0bcc922902dce7d5d7582;hp=9f8cbf6c795447bb0b826cdd95af2c2fe74f72d5;hpb=65eb4378c3e1de25383d8cd606909e64c71edc80;p=elogind.git diff --git a/src/hwdb/hwdb.c b/src/hwdb/hwdb.c index 9f8cbf6c7..446de3a2f 100644 --- a/src/hwdb/hwdb.c +++ b/src/hwdb/hwdb.c @@ -18,7 +18,6 @@ ***/ #include -#include #include #include #include @@ -28,7 +27,8 @@ #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" @@ -419,7 +419,7 @@ static int trie_store(struct trie *trie, const char *filename) { } 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); @@ -567,16 +567,16 @@ static int import_file(struct trie *trie, const char *filename) { 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) @@ -588,7 +588,7 @@ static int hwdb_query(char **args, unsigned n) { 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; @@ -650,25 +650,28 @@ static int hwdb_update(char **args, unsigned n) { 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' }, {} }; @@ -679,17 +682,27 @@ static int parse_argv(int argc, char *argv[]) { 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"); } @@ -699,75 +712,13 @@ static int parse_argv(int argc, char *argv[]) { } 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[]) {