Commit 67b1355b74525fbe2bb38d80639148b1ee912acd

Authored by Gerd Hoffmann
Committed by Anthony Liguori
1 parent 62c5802e

qemu-option: factor out parse_option_bool

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing 1 changed file with 22 additions and 12 deletions
qemu-option.c
... ... @@ -165,6 +165,23 @@ QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
165 165 return NULL;
166 166 }
167 167  
  168 +static int parse_option_bool(const char *name, const char *value, int *ret)
  169 +{
  170 + if (value != NULL) {
  171 + if (!strcmp(value, "on")) {
  172 + *ret = 1;
  173 + } else if (!strcmp(value, "off")) {
  174 + *ret = 0;
  175 + } else {
  176 + fprintf(stderr, "Option '%s': Use 'on' or 'off'\n", name);
  177 + return -1;
  178 + }
  179 + } else {
  180 + *ret = 1;
  181 + }
  182 + return 0;
  183 +}
  184 +
168 185 /*
169 186 * Sets the value of a parameter in a given option list. The parsing of the
170 187 * value depends on the type of option:
... ... @@ -185,6 +202,8 @@ QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
185 202 int set_option_parameter(QEMUOptionParameter *list, const char *name,
186 203 const char *value)
187 204 {
  205 + int flag;
  206 +
188 207 // Find a matching parameter
189 208 list = get_option_parameter(list, name);
190 209 if (list == NULL) {
... ... @@ -195,18 +214,9 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name,
195 214 // Process parameter
196 215 switch (list->type) {
197 216 case OPT_FLAG:
198   - if (value != NULL) {
199   - if (!strcmp(value, "on")) {
200   - list->value.n = 1;
201   - } else if (!strcmp(value, "off")) {
202   - list->value.n = 0;
203   - } else {
204   - fprintf(stderr, "Option '%s': Use 'on' or 'off'\n", name);
205   - return -1;
206   - }
207   - } else {
208   - list->value.n = 1;
209   - }
  217 + if (-1 == parse_option_bool(name, value, &flag))
  218 + return -1;
  219 + list->value.n = flag;
210 220 break;
211 221  
212 222 case OPT_STRING:
... ...