diff options
author | Pekka Enberg <penberg@kernel.org> | 2013-03-24 14:03:27 +0200 |
---|---|---|
committer | Pekka Enberg <penberg@kernel.org> | 2013-03-24 14:10:13 +0200 |
commit | 196b35cbe2a183da94ebc28170095c6d21961c99 (patch) | |
tree | d1dfd894fa72a3333a1dd0b4f96123e4547902f9 | |
parent | dfa660b7f27562be2e7aee99437444eefa5f9086 (diff) | |
download | jato-196b35cbe2a183da94ebc28170095c6d21961c99.tar.gz |
lib: Move option parsing to lib
Separate command line option parsing from the VM launcher.
Signed-off-by: Pekka Enberg <penberg@kernel.org>
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | include/lib/options.h | 29 | ||||
-rw-r--r-- | jato.c | 40 | ||||
-rw-r--r-- | lib/options.c | 45 |
4 files changed, 77 insertions, 38 deletions
@@ -103,6 +103,7 @@ LIB_OBJS += lib/compile-lock.o LIB_OBJS += lib/guard-page.o LIB_OBJS += lib/hash-map.o LIB_OBJS += lib/list.o +LIB_OBJS += lib/options.o LIB_OBJS += lib/parse.o LIB_OBJS += lib/pqueue.o LIB_OBJS += lib/radix-tree.o diff --git a/include/lib/options.h b/include/lib/options.h new file mode 100644 index 00000000..caf8b9b5 --- /dev/null +++ b/include/lib/options.h @@ -0,0 +1,29 @@ +#ifndef JATO_LIB_OPTIONS_H +#define JATO_LIB_OPTIONS_H + +#include <stdbool.h> + +struct option { + const char *name; + + bool arg; + bool arg_is_adjacent; + + union { + void (*func)(void); + void (*func_arg)(const char *arg); + } handler; +}; + +#define DEFINE_OPTION(_name, _handler) \ + { .name = _name, .arg = false, .handler.func = _handler } + +#define DEFINE_OPTION_ARG(_name, _handler) \ + { .name = _name, .arg = true, .arg_is_adjacent = false, .handler.func_arg = _handler } + +#define DEFINE_OPTION_ADJACENT_ARG(_name, _handler) \ + { .name = _name, .arg = true, .arg_is_adjacent = true, .handler.func_arg = _handler } + +const struct option *get_option(const struct option *options, unsigned long len, const char *name); + +#endif @@ -62,6 +62,7 @@ #include "jit/debug.h" #include "jit/text.h" +#include "lib/options.h" #include "lib/string.h" #include "lib/parse.h" #include "lib/list.h" @@ -896,27 +897,6 @@ static void handle_print_compilation(void) opt_print_compilation = true; } -struct option { - const char *name; - - bool arg; - bool arg_is_adjacent; - - union { - void (*func)(void); - void (*func_arg)(const char *arg); - } handler; -}; - -#define DEFINE_OPTION(_name, _handler) \ - { .name = _name, .arg = false, .handler.func = _handler } - -#define DEFINE_OPTION_ARG(_name, _handler) \ - { .name = _name, .arg = true, .arg_is_adjacent = false, .handler.func_arg = _handler } - -#define DEFINE_OPTION_ADJACENT_ARG(_name, _handler) \ - { .name = _name, .arg = true, .arg_is_adjacent = true, .handler.func_arg = _handler } - const struct option options[] = { DEFINE_OPTION("version", handle_version), DEFINE_OPTION("h", handle_help), @@ -965,22 +945,6 @@ const struct option options[] = { DEFINE_OPTION("XX:+PrintCompilation", handle_print_compilation), }; -static const struct option *get_option(const char *name) -{ - for (unsigned int i = 0; i < ARRAY_SIZE(options); ++i) { - const struct option *opt = &options[i]; - - if (opt->arg && opt->arg_is_adjacent && - !strncmp(name, opt->name, strlen(opt->name))) - return opt; - - if (!strcmp(name, opt->name)) - return opt; - } - - return NULL; -} - static void parse_options(int argc, char *argv[]) { int optind; @@ -989,7 +953,7 @@ static void parse_options(int argc, char *argv[]) if (argv[optind][0] != '-') break; - const struct option *opt = get_option(argv[optind] + 1); + const struct option *opt = get_option(options, ARRAY_SIZE(options), argv[optind] + 1); if (!opt) { fprintf(stderr, "%s: unrecognized option '%s'\n", program_name, argv[optind]); usage(stderr, EXIT_FAILURE); diff --git a/lib/options.c b/lib/options.c new file mode 100644 index 00000000..2d4b72b4 --- /dev/null +++ b/lib/options.c @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2009 Vegard Nossum + * + * This file is released under the GPL version 2 with the following + * clarification and special exception: + * + * Linking this library statically or dynamically with other modules is + * making a combined work based on this library. Thus, the terms and + * conditions of the GNU General Public License cover the whole + * combination. + * + * As a special exception, the copyright holders of this library give you + * permission to link this library with independent modules to produce an + * executable, regardless of the license terms of these independent + * modules, and to copy and distribute the resulting executable under terms + * of your choice, provided that you also meet, for each linked independent + * module, the terms and conditions of the license of that module. An + * independent module is a module which is not derived from or based on + * this library. If you modify this library, you may extend this exception + * to your version of the library, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from your + * version. + * + * Please refer to the file LICENSE for details. + */ + +#include "lib/options.h" + +#include <string.h> + +const struct option *get_option(const struct option *options, unsigned long len, const char *name) +{ + for (unsigned int i = 0; i < len; ++i) { + const struct option *opt = &options[i]; + + if (opt->arg && opt->arg_is_adjacent && + !strncmp(name, opt->name, strlen(opt->name))) + return opt; + + if (!strcmp(name, opt->name)) + return opt; + } + + return NULL; +} |