string-input-visitor.c 8.8 KB
Newer Older
P
Paolo Bonzini 已提交
1 2 3
/*
 * String parsing visitor
 *
4
 * Copyright Red Hat, Inc. 2012-2016
P
Paolo Bonzini 已提交
5 6 7 8 9 10 11 12
 *
 * Author: Paolo Bonzini <pbonzini@redhat.com>
 *
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
 * See the COPYING.LIB file in the top-level directory.
 *
 */

P
Peter Maydell 已提交
13
#include "qemu/osdep.h"
14
#include "qapi/error.h"
P
Paolo Bonzini 已提交
15
#include "qemu-common.h"
16 17 18
#include "qapi/string-input-visitor.h"
#include "qapi/visitor-impl.h"
#include "qapi/qmp/qerror.h"
19
#include "qemu/option.h"
20 21 22
#include "qemu/queue.h"
#include "qemu/range.h"

P
Paolo Bonzini 已提交
23 24 25 26

struct StringInputVisitor
{
    Visitor visitor;
27 28 29 30 31

    GList *ranges;
    GList *cur_range;
    int64_t cur;

P
Paolo Bonzini 已提交
32
    const char *string;
E
Eric Blake 已提交
33
    void *list; /* Only needed for sanity checking the caller */
P
Paolo Bonzini 已提交
34 35
};

36 37 38 39 40
static StringInputVisitor *to_siv(Visitor *v)
{
    return container_of(v, StringInputVisitor, visitor);
}

41 42 43 44 45
static void free_range(void *range, void *dummy)
{
    g_free(range);
}

46
static int parse_str(StringInputVisitor *siv, const char *name, Error **errp)
47 48 49 50 51 52 53
{
    char *str = (char *) siv->string;
    long long start, end;
    Range *cur;
    char *endptr;

    if (siv->ranges) {
54
        return 0;
55 56 57
    }

    do {
58
        errno = 0;
59
        start = strtoll(str, &endptr, 0);
60
        if (errno == 0 && endptr > str) {
61 62
            if (*endptr == '\0') {
                cur = g_malloc0(sizeof(*cur));
63
                range_set_bounds(cur, start, start);
E
Eric Blake 已提交
64
                siv->ranges = range_list_insert(siv->ranges, cur);
65 66 67 68
                cur = NULL;
                str = NULL;
            } else if (*endptr == '-') {
                str = endptr + 1;
69
                errno = 0;
70
                end = strtoll(str, &endptr, 0);
71
                if (errno == 0 && endptr > str && start <= end &&
72 73 74 75
                    (start > INT64_MAX - 65536 ||
                     end < start + 65536)) {
                    if (*endptr == '\0') {
                        cur = g_malloc0(sizeof(*cur));
76
                        range_set_bounds(cur, start, end);
E
Eric Blake 已提交
77
                        siv->ranges = range_list_insert(siv->ranges, cur);
78 79 80 81 82
                        cur = NULL;
                        str = NULL;
                    } else if (*endptr == ',') {
                        str = endptr + 1;
                        cur = g_malloc0(sizeof(*cur));
83
                        range_set_bounds(cur, start, end);
E
Eric Blake 已提交
84
                        siv->ranges = range_list_insert(siv->ranges, cur);
85 86 87 88 89 90 91 92 93 94
                        cur = NULL;
                    } else {
                        goto error;
                    }
                } else {
                    goto error;
                }
            } else if (*endptr == ',') {
                str = endptr + 1;
                cur = g_malloc0(sizeof(*cur));
95
                range_set_bounds(cur, start, start);
E
Eric Blake 已提交
96
                siv->ranges = range_list_insert(siv->ranges, cur);
97 98 99 100 101 102 103 104 105
                cur = NULL;
            } else {
                goto error;
            }
        } else {
            goto error;
        }
    } while (str);

106
    return 0;
107
error:
108 109 110
    g_list_foreach(siv->ranges, free_range, NULL);
    g_list_free(siv->ranges);
    siv->ranges = NULL;
111 112 113
    error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
               "an int64 value or range");
    return -1;
114 115 116
}

static void
117 118
start_list(Visitor *v, const char *name, GenericList **list, size_t size,
           Error **errp)
119
{
120
    StringInputVisitor *siv = to_siv(v);
121

122 123
    /* We don't support visits without a list */
    assert(list);
E
Eric Blake 已提交
124
    siv->list = list;
125

126
    if (parse_str(siv, name, errp) < 0) {
127
        *list = NULL;
128 129
        return;
    }
130 131 132 133 134

    siv->cur_range = g_list_first(siv->ranges);
    if (siv->cur_range) {
        Range *r = siv->cur_range->data;
        if (r) {
135
            siv->cur = range_lob(r);
136
        }
137 138 139
        *list = g_malloc0(size);
    } else {
        *list = NULL;
140 141 142
    }
}

143
static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
144
{
145
    StringInputVisitor *siv = to_siv(v);
146 147 148 149 150 151 152 153 154 155 156
    Range *r;

    if (!siv->ranges || !siv->cur_range) {
        return NULL;
    }

    r = siv->cur_range->data;
    if (!r) {
        return NULL;
    }

157
    if (!range_contains(r, siv->cur)) {
158 159 160 161 162 163 164 165
        siv->cur_range = g_list_next(siv->cur_range);
        if (!siv->cur_range) {
            return NULL;
        }
        r = siv->cur_range->data;
        if (!r) {
            return NULL;
        }
166
        siv->cur = range_lob(r);
167 168
    }

169 170
    tail->next = g_malloc0(size);
    return tail->next;
171 172
}

E
Eric Blake 已提交
173
static void end_list(Visitor *v, void **obj)
174
{
E
Eric Blake 已提交
175 176 177
    StringInputVisitor *siv = to_siv(v);

    assert(siv->list == obj);
178 179
}

180
static void parse_type_int64(Visitor *v, const char *name, int64_t *obj,
181
                             Error **errp)
P
Paolo Bonzini 已提交
182
{
183
    StringInputVisitor *siv = to_siv(v);
P
Paolo Bonzini 已提交
184

185
    if (!siv->string) {
186 187
        error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                   "integer");
P
Paolo Bonzini 已提交
188 189 190
        return;
    }

191 192 193
    if (parse_str(siv, name, errp) < 0) {
        return;
    }
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

    if (!siv->ranges) {
        goto error;
    }

    if (!siv->cur_range) {
        Range *r;

        siv->cur_range = g_list_first(siv->ranges);
        if (!siv->cur_range) {
            goto error;
        }

        r = siv->cur_range->data;
        if (!r) {
            goto error;
        }

212
        siv->cur = range_lob(r);
213 214 215 216 217 218 219
    }

    *obj = siv->cur;
    siv->cur++;
    return;

error:
220
    error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
221
               "an int64 value or range");
P
Paolo Bonzini 已提交
222 223
}

224
static void parse_type_uint64(Visitor *v, const char *name, uint64_t *obj,
225 226 227 228 229
                              Error **errp)
{
    /* FIXME: parse_type_int64 mishandles values over INT64_MAX */
    int64_t i;
    Error *err = NULL;
230
    parse_type_int64(v, name, &i, &err);
231 232 233 234 235 236 237
    if (err) {
        error_propagate(errp, err);
    } else {
        *obj = i;
    }
}

238
static void parse_type_size(Visitor *v, const char *name, uint64_t *obj,
239 240
                            Error **errp)
{
241
    StringInputVisitor *siv = to_siv(v);
242 243 244 245 246 247
    Error *err = NULL;
    uint64_t val;

    if (siv->string) {
        parse_option_size(name, siv->string, &val, &err);
    } else {
248 249
        error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                   "size");
250 251 252 253 254 255 256 257 258 259
        return;
    }
    if (err) {
        error_propagate(errp, err);
        return;
    }

    *obj = val;
}

260
static void parse_type_bool(Visitor *v, const char *name, bool *obj,
P
Paolo Bonzini 已提交
261 262
                            Error **errp)
{
263
    StringInputVisitor *siv = to_siv(v);
P
Paolo Bonzini 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

    if (siv->string) {
        if (!strcasecmp(siv->string, "on") ||
            !strcasecmp(siv->string, "yes") ||
            !strcasecmp(siv->string, "true")) {
            *obj = true;
            return;
        }
        if (!strcasecmp(siv->string, "off") ||
            !strcasecmp(siv->string, "no") ||
            !strcasecmp(siv->string, "false")) {
            *obj = false;
            return;
        }
    }

280 281
    error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
               "boolean");
P
Paolo Bonzini 已提交
282 283
}

284
static void parse_type_str(Visitor *v, const char *name, char **obj,
P
Paolo Bonzini 已提交
285 286
                           Error **errp)
{
287
    StringInputVisitor *siv = to_siv(v);
P
Paolo Bonzini 已提交
288 289 290
    if (siv->string) {
        *obj = g_strdup(siv->string);
    } else {
291
        *obj = NULL;
292 293
        error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                   "string");
P
Paolo Bonzini 已提交
294 295 296
    }
}

297
static void parse_type_number(Visitor *v, const char *name, double *obj,
P
Paolo Bonzini 已提交
298 299
                              Error **errp)
{
300
    StringInputVisitor *siv = to_siv(v);
P
Paolo Bonzini 已提交
301 302 303 304 305 306 307 308
    char *endp = (char *) siv->string;
    double val;

    errno = 0;
    if (siv->string) {
        val = strtod(siv->string, &endp);
    }
    if (!siv->string || errno || endp == siv->string || *endp) {
309 310
        error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                   "number");
P
Paolo Bonzini 已提交
311 312 313 314 315 316
        return;
    }

    *obj = val;
}

317
static void parse_optional(Visitor *v, const char *name, bool *present)
P
Paolo Bonzini 已提交
318
{
319
    StringInputVisitor *siv = to_siv(v);
P
Paolo Bonzini 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

    if (!siv->string) {
        *present = false;
        return;
    }

    *present = true;
}

Visitor *string_input_get_visitor(StringInputVisitor *v)
{
    return &v->visitor;
}

void string_input_visitor_cleanup(StringInputVisitor *v)
{
336 337
    g_list_foreach(v->ranges, free_range, NULL);
    g_list_free(v->ranges);
P
Paolo Bonzini 已提交
338 339 340 341 342 343 344 345 346
    g_free(v);
}

StringInputVisitor *string_input_visitor_new(const char *str)
{
    StringInputVisitor *v;

    v = g_malloc0(sizeof(*v));

347
    v->visitor.type = VISITOR_INPUT;
348
    v->visitor.type_int64 = parse_type_int64;
349
    v->visitor.type_uint64 = parse_type_uint64;
350
    v->visitor.type_size = parse_type_size;
P
Paolo Bonzini 已提交
351 352 353
    v->visitor.type_bool = parse_type_bool;
    v->visitor.type_str = parse_type_str;
    v->visitor.type_number = parse_type_number;
354 355 356
    v->visitor.start_list = start_list;
    v->visitor.next_list = next_list;
    v->visitor.end_list = end_list;
357
    v->visitor.optional = parse_optional;
P
Paolo Bonzini 已提交
358 359 360 361

    v->string = str;
    return v;
}