Add #defines needed by OpenSolaris, fix breakage by the #defines
[qemu] / qemu-option.c
index 7164ee8..88298e4 100644 (file)
@@ -274,7 +274,7 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name,
 
     case OPT_STRING:
         if (value != NULL) {
-            list->value.s = strdup(value);
+            list->value.s = qemu_strdup(value);
         } else {
             fprintf(stderr, "Option '%s' needs a parameter\n", name);
             return -1;
@@ -334,12 +334,12 @@ void free_option_parameters(QEMUOptionParameter *list)
 
     while (cur && cur->name) {
         if (cur->type == OPT_STRING) {
-            free(cur->value.s);
+            qemu_free(cur->value.s);
         }
         cur++;
     }
 
-    free(list);
+    qemu_free(list);
 }
 
 /*
@@ -477,21 +477,21 @@ struct QemuOpt {
     } value;
 
     QemuOpts     *opts;
-    TAILQ_ENTRY(QemuOpt) next;
+    QTAILQ_ENTRY(QemuOpt) next;
 };
 
 struct QemuOpts {
     const char *id;
     QemuOptsList *list;
-    TAILQ_HEAD(, QemuOpt) head;
-    TAILQ_ENTRY(QemuOpts) next;
+    QTAILQ_HEAD(, QemuOpt) head;
+    QTAILQ_ENTRY(QemuOpts) next;
 };
 
 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
 {
     QemuOpt *opt;
 
-    TAILQ_FOREACH(opt, &opts->head, next) {
+    QTAILQ_FOREACH(opt, &opts->head, next) {
         if (strcmp(opt->name, name) != 0)
             continue;
         return opt;
@@ -556,7 +556,7 @@ static int qemu_opt_parse(QemuOpt *opt)
 
 static void qemu_opt_del(QemuOpt *opt)
 {
-    TAILQ_REMOVE(&opt->opts->head, opt, next);
+    QTAILQ_REMOVE(&opt->opts->head, opt, next);
     qemu_free((/* !const */ char*)opt->name);
     qemu_free((/* !const */ char*)opt->str);
     qemu_free(opt);
@@ -588,7 +588,7 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
         opt = qemu_mallocz(sizeof(*opt));
         opt->name = qemu_strdup(name);
         opt->opts = opts;
-        TAILQ_INSERT_TAIL(&opts->head, opt, next);
+        QTAILQ_INSERT_TAIL(&opts->head, opt, next);
         if (desc[i].name != NULL) {
             opt->desc = desc+i;
         }
@@ -613,7 +613,7 @@ int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
     QemuOpt *opt;
     int rc = 0;
 
-    TAILQ_FOREACH(opt, &opts->head, next) {
+    QTAILQ_FOREACH(opt, &opts->head, next) {
         rc = func(opt->name, opt->str, opaque);
         if (abort_on_failure  &&  rc != 0)
             break;
@@ -625,7 +625,7 @@ QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
 {
     QemuOpts *opts;
 
-    TAILQ_FOREACH(opts, &list->head, next) {
+    QTAILQ_FOREACH(opts, &list->head, next) {
         if (!opts->id) {
             continue;
         }
@@ -658,8 +658,8 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exist
         opts->id = qemu_strdup(id);
     }
     opts->list = list;
-    TAILQ_INIT(&opts->head);
-    TAILQ_INSERT_TAIL(&list->head, opts, next);
+    QTAILQ_INIT(&opts->head);
+    QTAILQ_INSERT_TAIL(&list->head, opts, next);
     return opts;
 }
 
@@ -687,12 +687,12 @@ void qemu_opts_del(QemuOpts *opts)
     QemuOpt *opt;
 
     for (;;) {
-        opt = TAILQ_FIRST(&opts->head);
+        opt = QTAILQ_FIRST(&opts->head);
         if (opt == NULL)
             break;
         qemu_opt_del(opt);
     }
-    TAILQ_REMOVE(&opts->list->head, opts, next);
+    QTAILQ_REMOVE(&opts->list->head, opts, next);
     qemu_free(opts);
 }
 
@@ -702,25 +702,18 @@ int qemu_opts_print(QemuOpts *opts, void *dummy)
 
     fprintf(stderr, "%s: %s:", opts->list->name,
             opts->id ? opts->id : "<noid>");
-    TAILQ_FOREACH(opt, &opts->head, next) {
+    QTAILQ_FOREACH(opt, &opts->head, next) {
         fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
     }
     fprintf(stderr, "\n");
     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;
-    QemuOpts *opts;
+    char option[128], value[128];
     const char *p,*pe,*pc;
 
-    if (get_param_value(value, sizeof(value), "id", params))
-        id = qemu_strdup(value);
-    opts = qemu_opts_create(list, id, 1);
-    if (opts == NULL)
-        return NULL;
-
     p = params;
     for(;;) {
         pe = strchr(p, '=');
@@ -734,7 +727,7 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi
             } else {
                 /* option without value, probably a flag */
                 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);
                     pstrcpy(value, sizeof(value), "off");
                 } else {
@@ -753,8 +746,7 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi
         if (strcmp(option, "id") != 0) {
             /* store and parse */
             if (-1 == qemu_opt_set(opts, option, value)) {
-                qemu_opts_del(opts);
-                return NULL;
+                return -1;
             }
         }
         if (*p != ',') {
@@ -762,6 +754,31 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi
         }
         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;
 }
 
@@ -771,7 +788,7 @@ int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
     QemuOpts *opts;
     int rc = 0;
 
-    TAILQ_FOREACH(opts, &list->head, next) {
+    QTAILQ_FOREACH(opts, &list->head, next) {
         rc = func(opts, opaque);
         if (abort_on_failure  &&  rc != 0)
             break;