Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

lib: Move option parsing to lib

Separate command line option parsing from the VM launcher.

Signed-off-by: Pekka Enberg <penberg@kernel.org>
  • Loading branch information...
commit 196b35cbe2a183da94ebc28170095c6d21961c99 1 parent dfa660b
@penberg authored
Showing with 77 additions and 38 deletions.
  1. +1 −0  Makefile
  2. +29 −0 include/lib/options.h
  3. +2 −38 jato.c
  4. +45 −0 lib/options.c
View
1  Makefile
@@ -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
View
29 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
View
40 jato.c
@@ -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);
View
45 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;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.