From: Elena Ufimtseva <elena.ufimts...@oracle.com> Refactor command-line parser code so that it could be used by other processes as well.
Signed-off-by: Elena Ufimtseva <elena.ufimts...@oracle.com> Signed-off-by: Jagannathan Raman <jag.ra...@oracle.com> Signed-off-by: John G Johnson <john.g.john...@oracle.com> --- MAINTAINERS | 2 + Makefile.objs | 2 + include/qemu-parse.h | 42 ++++++++++++++++++++ qemu-parse.c | 93 ++++++++++++++++++++++++++++++++++++++++++++ softmmu/vl.c | 84 +-------------------------------------- 5 files changed, 140 insertions(+), 83 deletions(-) create mode 100644 include/qemu-parse.h create mode 100644 qemu-parse.c diff --git a/MAINTAINERS b/MAINTAINERS index 04b19ac56c..2e700e6e64 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2032,6 +2032,8 @@ F: tests/test-keyval.c F: tests/test-qemu-opts.c F: util/keyval.c F: util/qemu-option.c +F: include/qemu-parse.h +F: qemu-parse.c Coverity model M: Markus Armbruster <arm...@redhat.com> diff --git a/Makefile.objs b/Makefile.objs index bfb9271862..f29c60c59d 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -78,6 +78,8 @@ qemu-seccomp.o-libs := $(SECCOMP_LIBS) common-obj-$(CONFIG_FDT) += device_tree.o +common-obj-y += qemu-parse.o + common-obj-y += qapi/ common-obj-y += util/machine-notify.o diff --git a/include/qemu-parse.h b/include/qemu-parse.h new file mode 100644 index 0000000000..156b238db6 --- /dev/null +++ b/include/qemu-parse.h @@ -0,0 +1,42 @@ +/* + * Copyright © 2018, 2020 Oracle and/or its affiliates. + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#ifndef VL_H +#define VL_H + +#include "qemu/osdep.h" +#include "qemu-common.h" + +/***********************************************************/ +/* QEMU Block devices */ + +#define HD_OPTS "media=disk" +#define CDROM_OPTS "media=cdrom" +#define FD_OPTS "" +#define PFLASH_OPTS "" +#define MTD_OPTS "" +#define SD_OPTS "" + +#define HAS_ARG 0x0001 + +typedef struct QEMUOption { + const char *name; + int flags; + int index; + uint32_t arch_mask; +} QEMUOption; + +const QEMUOption *lookup_opt(int argc, char **argv, const char **poptarg, + int *poptind); + +int drive_init_func(void *opaque, QemuOpts *opts, Error **errp); + +int device_init_func(void *opaque, QemuOpts *opts, Error **errp); + +#endif /* VL_H */ + diff --git a/qemu-parse.c b/qemu-parse.c new file mode 100644 index 0000000000..2535374fdd --- /dev/null +++ b/qemu-parse.c @@ -0,0 +1,93 @@ +/* + * Copyright © 2018, 2020 Oracle and/or its affiliates. + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "qemu-common.h" + +#include "sysemu/blockdev.h" +#include "sysemu/arch_init.h" +#include "qemu/option.h" +#include "qemu-options.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "monitor/qdev.h" +#include "qom/object.h" +#include "qemu-parse.h" + +/***********************************************************/ +/* QEMU Block devices */ + +static const QEMUOption qemu_options[] = { + { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL }, +#define QEMU_OPTIONS_GENERATE_OPTIONS +#include "qemu-options-wrapper.h" + { NULL }, +}; + +const QEMUOption *lookup_opt(int argc, char **argv, const char **poptarg, + int *poptind) +{ + const QEMUOption *popt; + int optind = *poptind; + char *r = argv[optind]; + const char *optarg; + + loc_set_cmdline(argv, optind, 1); + optind++; + /* Treat --foo the same as -foo. */ + if (r[1] == '-') { + r++; + } + popt = qemu_options; + for (;;) { + if (!popt->name) { + error_report("invalid option"); + exit(1); + } + if (!strcmp(popt->name, r + 1)) { + break; + } + popt++; + } + if (popt->flags & HAS_ARG) { + if (optind >= argc) { + error_report("requires an argument"); + exit(1); + } + optarg = argv[optind++]; + loc_set_cmdline(argv, optind - 2, 2); + } else { + optarg = NULL; + } + + *poptarg = optarg; + *poptind = optind; + + return popt; +} + +int drive_init_func(void *opaque, QemuOpts *opts, Error **errp) +{ + BlockInterfaceType *block_default_type = opaque; + + return drive_new(opts, *block_default_type, errp) == NULL; +} + +int device_init_func(void *opaque, QemuOpts *opts, Error **errp) +{ + DeviceState *dev; + + dev = qdev_device_add(opts, errp); + if (!dev && *errp) { + error_report_err(*errp); + return -1; + } else if (dev) { + object_unref(OBJECT(dev)); + } + return 0; +} diff --git a/softmmu/vl.c b/softmmu/vl.c index 39cbb6b50d..d1b32a33a2 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -36,6 +36,7 @@ #include "sysemu/runstate.h" #include "sysemu/seccomp.h" #include "sysemu/tcg.h" +#include "qemu-parse.h" #include "qemu/error-report.h" #include "qemu/sockets.h" @@ -969,20 +970,6 @@ static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp) /***********************************************************/ /* QEMU Block devices */ -#define HD_OPTS "media=disk" -#define CDROM_OPTS "media=cdrom" -#define FD_OPTS "" -#define PFLASH_OPTS "" -#define MTD_OPTS "" -#define SD_OPTS "" - -static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp) -{ - BlockInterfaceType *block_default_type = opaque; - - return drive_new(opts, *block_default_type, errp) == NULL; -} - static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp) { if (qemu_opt_get(opts, "snapshot") == NULL) { @@ -1690,21 +1677,6 @@ static void help(int exitcode) exit(exitcode); } -#define HAS_ARG 0x0001 - -typedef struct QEMUOption { - const char *name; - int flags; - int index; - uint32_t arch_mask; -} QEMUOption; - -static const QEMUOption qemu_options[] = { - { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL }, -#define QEMU_OPTIONS_GENERATE_OPTIONS -#include "qemu-options-wrapper.h" - { NULL }, -}; typedef struct VGAInterfaceInfo { const char *opt_name; /* option name */ @@ -2066,20 +2038,6 @@ static int device_help_func(void *opaque, QemuOpts *opts, Error **errp) return qdev_device_help(opts); } -static int device_init_func(void *opaque, QemuOpts *opts, Error **errp) -{ - DeviceState *dev; - - dev = qdev_device_add(opts, errp); - if (!dev && *errp) { - error_report_err(*errp); - return -1; - } else if (dev) { - object_unref(OBJECT(dev)); - } - return 0; -} - static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp) { Error *local_err = NULL; @@ -2329,46 +2287,6 @@ static void qemu_unlink_pidfile(Notifier *n, void *data) } } -static const QEMUOption *lookup_opt(int argc, char **argv, - const char **poptarg, int *poptind) -{ - const QEMUOption *popt; - int optind = *poptind; - char *r = argv[optind]; - const char *optarg; - - loc_set_cmdline(argv, optind, 1); - optind++; - /* Treat --foo the same as -foo. */ - if (r[1] == '-') - r++; - popt = qemu_options; - for(;;) { - if (!popt->name) { - error_report("invalid option"); - exit(1); - } - if (!strcmp(popt->name, r + 1)) - break; - popt++; - } - if (popt->flags & HAS_ARG) { - if (optind >= argc) { - error_report("requires an argument"); - exit(1); - } - optarg = argv[optind++]; - loc_set_cmdline(argv, optind - 2, 2); - } else { - optarg = NULL; - } - - *poptarg = optarg; - *poptind = optind; - - return popt; -} - static MachineClass *select_machine(void) { GSList *machines = object_class_get_list(TYPE_MACHINE, false); -- 2.25.GIT