QemuOpts: split option parser into two functions.

looking for id= and creating a new QemuOpts instance is splitted from
the actual option parser code now, so the parser can be called from
other contexts too.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Gerd Hoffmann 2009-09-10 10:58:33 +02:00 committed by Anthony Liguori
parent d03f09ccde
commit 96729cbd29
2 changed files with 30 additions and 17 deletions

View File

@ -709,23 +709,11 @@ int qemu_opts_print(QemuOpts *opts, void *dummy)
return 0; return 0;
} }
QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname) int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
{ {
char option[128], value[128], *id = NULL; char option[128], value[128];
QemuOpts *opts;
const char *p,*pe,*pc; const char *p,*pe,*pc;
if (strncmp(params, "id=", 3) == 0) {
get_opt_value(value, sizeof(value), params+3);
id = qemu_strdup(value);
} else if ((p = strstr(params, ",id=")) != NULL) {
get_opt_value(value, sizeof(value), p+4);
id = qemu_strdup(value);
}
opts = qemu_opts_create(list, id, 1);
if (opts == NULL)
return NULL;
p = params; p = params;
for(;;) { for(;;) {
pe = strchr(p, '='); pe = strchr(p, '=');
@ -739,7 +727,7 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi
} else { } else {
/* option without value, probably a flag */ /* option without value, probably a flag */
p = get_opt_name(option, sizeof(option), p, ','); p = get_opt_name(option, sizeof(option), p, ',');
if (strncmp(p, "no", 2) == 0) { if (strncmp(option, "no", 2) == 0) {
memmove(option, option+2, strlen(option+2)+1); memmove(option, option+2, strlen(option+2)+1);
pstrcpy(value, sizeof(value), "off"); pstrcpy(value, sizeof(value), "off");
} else { } else {
@ -758,8 +746,7 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi
if (strcmp(option, "id") != 0) { if (strcmp(option, "id") != 0) {
/* store and parse */ /* store and parse */
if (-1 == qemu_opt_set(opts, option, value)) { if (-1 == qemu_opt_set(opts, option, value)) {
qemu_opts_del(opts); return -1;
return NULL;
} }
} }
if (*p != ',') { if (*p != ',') {
@ -767,6 +754,31 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi
} }
p++; p++;
} }
return 0;
}
QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname)
{
char value[128], *id = NULL;
const char *p;
QemuOpts *opts;
if (strncmp(params, "id=", 3) == 0) {
get_opt_value(value, sizeof(value), params+3);
id = qemu_strdup(value);
} else if ((p = strstr(params, ",id=")) != NULL) {
get_opt_value(value, sizeof(value), p+4);
id = qemu_strdup(value);
}
opts = qemu_opts_create(list, id, 1);
if (opts == NULL)
return NULL;
if (qemu_opts_do_parse(opts, params, firstname) != 0) {
qemu_opts_del(opts);
return NULL;
}
return opts; return opts;
} }

View File

@ -114,6 +114,7 @@ int qemu_opts_set(QemuOptsList *list, const char *id,
const char *name, const char *value); const char *name, const char *value);
const char *qemu_opts_id(QemuOpts *opts); const char *qemu_opts_id(QemuOpts *opts);
void qemu_opts_del(QemuOpts *opts); void qemu_opts_del(QemuOpts *opts);
int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname);
QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname); QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname);
typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque); typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque);