1 /* 2 * Commandline option parsing functions 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com> 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 26 #ifndef QEMU_OPTION_H 27 #define QEMU_OPTION_H 28 29 #include "qemu/queue.h" 30 31 /** 32 * get_opt_value 33 * @p: a pointer to the option name, delimited by commas 34 * @value: a non-NULL pointer that will received the delimited options 35 * 36 * The @value char pointer will be allocated and filled with 37 * the delimited options. 38 * 39 * Returns the position of the comma delimiter/zero byte after the 40 * option name in @p. 41 * The memory pointer in @value must be released with a call to g_free() 42 * when no longer required. 43 */ 44 const char *get_opt_value(const char *p, char **value); 45 46 bool parse_option_size(const char *name, const char *value, 47 uint64_t *ret, Error **errp); 48 bool has_help_option(const char *param); 49 50 enum QemuOptType { 51 QEMU_OPT_STRING = 0, /* no parsing (use string as-is) */ 52 QEMU_OPT_BOOL, /* on/off */ 53 QEMU_OPT_NUMBER, /* simple number */ 54 QEMU_OPT_SIZE, /* size, accepts (K)ilo, (M)ega, (G)iga, (T)era postfix */ 55 }; 56 57 typedef struct QemuOpt QemuOpt; 58 59 typedef struct QemuOptDesc { 60 const char *name; 61 enum QemuOptType type; 62 const char *help; 63 const char *def_value_str; 64 } QemuOptDesc; 65 66 struct QemuOptsList { 67 const char *name; 68 const char *implied_opt_name; 69 bool merge_lists; /* Merge multiple uses of option into a single list? */ 70 QTAILQ_HEAD(, QemuOpts) head; 71 QemuOptDesc desc[]; 72 }; 73 74 const char *qemu_opt_get(QemuOpts *opts, const char *name); 75 char *qemu_opt_get_del(QemuOpts *opts, const char *name); 76 /** 77 * qemu_opt_has_help_opt: 78 * @opts: options to search for a help request 79 * 80 * Check whether the options specified by @opts include one of the 81 * standard strings which indicate that the user is asking for a 82 * list of the valid values for a command line option (as defined 83 * by is_help_option()). 84 * 85 * Returns: true if @opts includes 'help' or equivalent. 86 */ 87 bool qemu_opt_has_help_opt(QemuOpts *opts); 88 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name); 89 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval); 90 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval); 91 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval); 92 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval); 93 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name, 94 uint64_t defval); 95 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name, 96 uint64_t defval); 97 int qemu_opt_unset(QemuOpts *opts, const char *name); 98 bool qemu_opt_set(QemuOpts *opts, const char *name, const char *value, 99 Error **errp); 100 bool qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val, 101 Error **errp); 102 bool qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val, 103 Error **errp); 104 typedef int (*qemu_opt_loopfunc)(void *opaque, 105 const char *name, const char *value, 106 Error **errp); 107 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, 108 Error **errp); 109 110 typedef struct { 111 QemuOpts *opts; 112 QemuOpt *opt; 113 const char *name; 114 } QemuOptsIter; 115 116 void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name); 117 const char *qemu_opt_iter_next(QemuOptsIter *iter); 118 119 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id); 120 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, 121 int fail_if_exists, Error **errp); 122 void qemu_opts_reset(QemuOptsList *list); 123 void qemu_opts_loc_restore(QemuOpts *opts); 124 const char *qemu_opts_id(QemuOpts *opts); 125 void qemu_opts_set_id(QemuOpts *opts, char *id); 126 void qemu_opts_del(QemuOpts *opts); 127 bool qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp); 128 bool qemu_opts_do_parse(QemuOpts *opts, const char *params, 129 const char *firstname, Error **errp); 130 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, 131 bool permit_abbrev); 132 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, 133 bool permit_abbrev, Error **errp); 134 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict, 135 Error **errp); 136 QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict, 137 QemuOptsList *list, bool del); 138 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict); 139 bool qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp); 140 141 typedef int (*qemu_opts_loopfunc)(void *opaque, QemuOpts *opts, Error **errp); 142 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, 143 void *opaque, Error **errp); 144 void qemu_opts_print(QemuOpts *opts, const char *sep); 145 void qemu_opts_print_help(QemuOptsList *list, bool print_caption); 146 void qemu_opts_free(QemuOptsList *list); 147 QemuOptsList *qemu_opts_append(QemuOptsList *dst, QemuOptsList *list); 148 149 G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuOpts, qemu_opts_del) 150 151 #endif 152