ovs-vsctl: Drop redundant {port,iface}-{set,get}-external-ids commands.
[openvswitch] / lib / ovsdb-data.c
index df445d775c12d5eb91302286ef4506354b123189..b89aa57a6fd742055568ff2ed270f9aa87dfc298 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009 Nicira Networks
+/* Copyright (c) 2009, 2010 Nicira Networks
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "ovsdb-data.h"
 
 #include <assert.h>
+#include <ctype.h>
+#include <float.h>
+#include <inttypes.h>
+#include <limits.h>
 
+#include "dynamic-string.h"
 #include "hash.h"
 #include "ovsdb-error.h"
 #include "json.h"
@@ -64,6 +69,35 @@ ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
     }
 }
 
+bool
+ovsdb_atom_is_default(const union ovsdb_atom *atom,
+                      enum ovsdb_atomic_type type)
+{
+    switch (type) {
+    case OVSDB_TYPE_VOID:
+        NOT_REACHED();
+
+    case OVSDB_TYPE_INTEGER:
+        return atom->integer == 0;
+
+    case OVSDB_TYPE_REAL:
+        return atom->real == 0.0;
+
+    case OVSDB_TYPE_BOOLEAN:
+        return atom->boolean == false;
+
+    case OVSDB_TYPE_STRING:
+        return atom->string[0] == '\0';
+
+    case OVSDB_TYPE_UUID:
+        return uuid_is_zero(&atom->uuid);
+
+    case OVSDB_N_TYPES:
+    default:
+        NOT_REACHED();
+    }
+}
+
 void
 ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old,
                  enum ovsdb_atomic_type type)
@@ -321,6 +355,164 @@ ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
         NOT_REACHED();
     }
 }
+
+/* Initializes 'atom' to a value of the given 'type' parsed from 's', which
+ * takes one of the following forms:
+ *
+ *      - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign.
+ *
+ *      - OVSDB_TYPE_REAL: A floating-point number in the format accepted by
+ *        strtod().
+ *
+ *      - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false",
+ *        "no", "off", or "0" for false.
+ *
+ *      - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
+ *        an arbitrary string.
+ *
+ *      - OVSDB_TYPE_UUID: A UUID in RFC 4122 format.
+ *
+ * Returns a null pointer if successful, otherwise an error message describing
+ * the problem.  The caller is responsible for freeing the error.
+ */
+char *
+ovsdb_atom_from_string(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
+                       const char *s)
+{
+    switch (type) {
+    case OVSDB_TYPE_VOID:
+        NOT_REACHED();
+
+    case OVSDB_TYPE_INTEGER: {
+        long long int integer;
+        if (!str_to_llong(s, 10, &integer)) {
+            return xasprintf("\"%s\" is not a valid integer", s);
+        }
+        atom->integer = integer;
+    }
+        break;
+
+    case OVSDB_TYPE_REAL:
+        if (!str_to_double(s, &atom->real)) {
+            return xasprintf("\"%s\" is not a valid real number", s);
+        }
+        break;
+
+    case OVSDB_TYPE_BOOLEAN:
+        if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on")
+            || !strcmp(s, "1")) {
+            atom->boolean = true;
+        } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off")
+                   || !strcmp(s, "0")) {
+            atom->boolean = false;
+        } else {
+            return xasprintf("\"%s\" is not a valid boolean "
+                             "(use \"true\" or \"false\")", s);
+        }
+        break;
+
+    case OVSDB_TYPE_STRING:
+        if (*s == '\0') {
+            return xstrdup("An empty string is not valid as input; "
+                           "use \"\" to represent the empty string");
+        } else if (*s == '"') {
+            size_t s_len = strlen(s);
+
+            if (s_len < 2 || s[s_len - 1] != '"') {
+                return xasprintf("%s: missing quote at end of "
+                                 "quoted string", s);
+            } else if (!json_string_unescape(s + 1, s_len - 2,
+                                             &atom->string)) {
+                char *error = xasprintf("%s: %s", s, atom->string);
+                free(atom->string);
+                return error;
+            }
+        } else {
+            atom->string = xstrdup(s);
+        }
+        break;
+
+    case OVSDB_TYPE_UUID:
+        if (!uuid_from_string(&atom->uuid, s)) {
+            return xasprintf("\"%s\" is not a valid UUID", s);
+        }
+        break;
+
+    case OVSDB_N_TYPES:
+    default:
+        NOT_REACHED();
+    }
+
+    return NULL;
+}
+
+static bool
+string_needs_quotes(const char *s)
+{
+    const char *p = s;
+    unsigned char c;
+
+    c = *p++;
+    if (!isalpha(c) && c != '_') {
+        return true;
+    }
+
+    while ((c = *p++) != '\0') {
+        if (!isalpha(c) && c != '_' && c != '-' && c != '.') {
+            return true;
+        }
+    }
+
+    if (!strcmp(s, "true") || !strcmp(s, "false")) {
+        return true;
+    }
+
+    return false;
+}
+
+/* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable
+ * to ovsdb_atom_from_string().  */
+void
+ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
+                     struct ds *out)
+{
+    switch (type) {
+    case OVSDB_TYPE_VOID:
+        NOT_REACHED();
+
+    case OVSDB_TYPE_INTEGER:
+        ds_put_format(out, "%"PRId64, atom->integer);
+        break;
+
+    case OVSDB_TYPE_REAL:
+        ds_put_format(out, "%.*g", DBL_DIG, atom->real);
+        break;
+
+    case OVSDB_TYPE_BOOLEAN:
+        ds_put_cstr(out, atom->boolean ? "true" : "false");
+        break;
+
+    case OVSDB_TYPE_STRING:
+        if (string_needs_quotes(atom->string)) {
+            struct json json;
+
+            json.type = JSON_STRING;
+            json.u.string = atom->string;
+            json_to_ds(&json, 0, out);
+        } else {
+            ds_put_cstr(out, atom->string);
+        }
+        break;
+
+    case OVSDB_TYPE_UUID:
+        ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid));
+        break;
+
+    case OVSDB_N_TYPES:
+    default:
+        NOT_REACHED();
+    }
+}
 \f
 static union ovsdb_atom *
 alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
@@ -341,6 +533,14 @@ alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
     }
 }
 
+void
+ovsdb_datum_init_empty(struct ovsdb_datum *datum)
+{
+    datum->n = 0;
+    datum->keys = NULL;
+    datum->values = NULL;
+}
+
 void
 ovsdb_datum_init_default(struct ovsdb_datum *datum,
                          const struct ovsdb_type *type)
@@ -350,6 +550,28 @@ ovsdb_datum_init_default(struct ovsdb_datum *datum,
     datum->values = alloc_default_atoms(type->value_type, datum->n);
 }
 
+bool
+ovsdb_datum_is_default(const struct ovsdb_datum *datum,
+                       const struct ovsdb_type *type)
+{
+    size_t i;
+
+    if (datum->n != type->n_min) {
+        return false;
+    }
+    for (i = 0; i < datum->n; i++) {
+        if (!ovsdb_atom_is_default(&datum->keys[i], type->key_type)) {
+            return false;
+        }
+        if (type->value_type != OVSDB_TYPE_VOID
+            && !ovsdb_atom_is_default(&datum->values[i], type->value_type)) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
 static union ovsdb_atom *
 clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n)
 {
@@ -433,7 +655,7 @@ ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_)
     }
 }
 
-static struct ovsdb_error *
+struct ovsdb_error *
 ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type)
 {
     if (datum->n < 2) {
@@ -583,6 +805,189 @@ ovsdb_datum_to_json(const struct ovsdb_datum *datum,
     }
 }
 
+static const char *
+skip_spaces(const char *p)
+{
+    return p + strspn(p, " ");
+}
+
+static char *
+parse_atom_token(const char **s, enum ovsdb_atomic_type type,
+                 union ovsdb_atom *atom)
+{
+    char *token, *error;
+
+    error = ovsdb_token_parse(s, &token);
+    if (!error) {
+        error = ovsdb_atom_from_string(atom, type, token);
+        free(token);
+    }
+    return error;
+}
+
+
+static char *
+parse_key_value(const char **s, const struct ovsdb_type *type,
+                union ovsdb_atom *key, union ovsdb_atom *value)
+{
+    const char *start = *s;
+    char *error;
+
+    error = parse_atom_token(s, type->key_type, key);
+    if (!error && type->value_type != OVSDB_TYPE_VOID) {
+        if (**s == '=') {
+            (*s)++;
+            error = parse_atom_token(s, type->value_type, value);
+        } else {
+            error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
+                              start, **s);
+        }
+        if (error) {
+            ovsdb_atom_destroy(key, type->key_type);
+        }
+    }
+    return error;
+}
+
+static void
+free_key_value(const struct ovsdb_type *type,
+               union ovsdb_atom *key, union ovsdb_atom *value)
+{
+    ovsdb_atom_destroy(key, type->key_type);
+    if (type->value_type != OVSDB_TYPE_VOID) {
+        ovsdb_atom_destroy(value, type->value_type);
+    }
+}
+
+/* Initializes 'datum' as a datum of the given 'type', parsing its contents
+ * from 's'.  The format of 's' is a series of space or comma separated atoms
+ * or, for a map, '='-delimited pairs of atoms.  Each atom must in a format
+ * acceptable to ovsdb_atom_from_string().  Optionally, a set may be enclosed
+ * in "[]" or a map in "{}"; for an empty set or map these punctuators are
+ * required. */
+char *
+ovsdb_datum_from_string(struct ovsdb_datum *datum,
+                        const struct ovsdb_type *type, const char *s)
+{
+    bool is_map = ovsdb_type_is_map(type);
+    struct ovsdb_error *dberror;
+    const char *p;
+    int end_delim;
+    char *error;
+
+    ovsdb_datum_init_empty(datum);
+
+    /* Swallow a leading delimiter if there is one. */
+    p = skip_spaces(s);
+    if (*p == (is_map ? '{' : '[')) {
+        end_delim = is_map ? '}' : ']';
+        p = skip_spaces(p + 1);
+    } else if (!*p) {
+        if (is_map) {
+            return xstrdup("use \"{}\" to specify the empty map");
+        } else {
+            return xstrdup("use \"[]\" to specify the empty set");
+        }
+    } else {
+        end_delim = 0;
+    }
+
+    while (*p && *p != end_delim) {
+        union ovsdb_atom key, value;
+
+        if (ovsdb_token_is_delim(*p)) {
+            error = xasprintf("%s: unexpected \"%c\" parsing %s",
+                              s, *p, ovsdb_type_to_english(type));
+            goto error;
+        }
+
+        /* Add to datum. */
+        error = parse_key_value(&p, type, &key, &value);
+        if (error) {
+            goto error;
+        }
+        ovsdb_datum_add_unsafe(datum, &key, &value, type);
+        free_key_value(type, &key, &value);
+
+        /* Skip optional white space and comma. */
+        p = skip_spaces(p);
+        if (*p == ',') {
+            p = skip_spaces(p + 1);
+        }
+    }
+
+    if (*p != end_delim) {
+        error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim);
+        goto error;
+    }
+    if (end_delim) {
+        p = skip_spaces(p + 1);
+        if (*p) {
+            error = xasprintf("%s: trailing garbage after \"%c\"",
+                              s, end_delim);
+            goto error;
+        }
+    }
+
+    if (datum->n < type->n_min) {
+        error = xasprintf("%s: %u %s specified but the minimum number is %u",
+                          s, datum->n, is_map ? "pair(s)" : "value(s)",
+                          type->n_min);
+        goto error;
+    } else if (datum->n > type->n_max) {
+        error = xasprintf("%s: %u %s specified but the maximum number is %u",
+                          s, datum->n, is_map ? "pair(s)" : "value(s)",
+            type->n_max);
+        goto error;
+    }
+
+    dberror = ovsdb_datum_sort(datum, type);
+    if (dberror) {
+        ovsdb_error_destroy(dberror);
+        if (ovsdb_type_is_map(type)) {
+            error = xasprintf("%s: map contains duplicate key", s);
+        } else {
+            error = xasprintf("%s: set contains duplicate value", s);
+        }
+        goto error;
+    }
+
+    return NULL;
+
+error:
+    ovsdb_datum_destroy(datum, type);
+    ovsdb_datum_init_empty(datum);
+    return error;
+}
+
+/* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable
+ * to ovsdb_datum_from_string(). */
+void
+ovsdb_datum_to_string(const struct ovsdb_datum *datum,
+                      const struct ovsdb_type *type, struct ds *out)
+{
+    bool is_map = ovsdb_type_is_map(type);
+    size_t i;
+
+    if (type->n_max > 1 || !datum->n) {
+        ds_put_char(out, is_map ? '{' : '[');
+    }
+    for (i = 0; i < datum->n; i++) {
+        if (i > 0) {
+            ds_put_cstr(out, ", ");
+        }
+
+        ovsdb_atom_to_string(&datum->keys[i], type->key_type, out);
+        if (is_map) {
+            ds_put_char(out, '=');
+            ovsdb_atom_to_string(&datum->values[i], type->value_type, out);
+        }
+    }
+    if (type->n_max > 1 || !datum->n) {
+        ds_put_char(out, is_map ? '}' : ']');
+    }
+}
+
 static uint32_t
 hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms,
            unsigned int n, uint32_t basis)
@@ -609,9 +1014,9 @@ ovsdb_datum_hash(const struct ovsdb_datum *datum,
 
 static int
 atom_arrays_compare_3way(const union ovsdb_atom *a,
-                  const union ovsdb_atom *b,
-                  enum ovsdb_atomic_type type,
-                  size_t n)
+                         const union ovsdb_atom *b,
+                         enum ovsdb_atomic_type type,
+                         size_t n)
 {
     unsigned int i;
 
@@ -654,27 +1059,65 @@ ovsdb_datum_compare_3way(const struct ovsdb_datum *a,
                                        a->n));
 }
 
-static bool
-ovsdb_datum_contains(const struct ovsdb_datum *a, int i,
-                     const struct ovsdb_datum *b,
-                     const struct ovsdb_type *type)
+/* If 'key' is one of the keys in 'datum', returns its index within 'datum',
+ * otherwise UINT_MAX.  'key_type' must be the type of the atoms stored in the
+ * 'keys' array in 'datum'.
+ */
+unsigned int
+ovsdb_datum_find_key(const struct ovsdb_datum *datum,
+                     const union ovsdb_atom *key,
+                     enum ovsdb_atomic_type key_type)
 {
-    int low = 0;
-    int high = b->n;
+    unsigned int low = 0;
+    unsigned int high = datum->n;
     while (low < high) {
-        int j = (low + high) / 2;
-        int cmp = ovsdb_atom_compare_3way(&a->keys[i], &b->keys[j], type->key_type);
+        unsigned int idx = (low + high) / 2;
+        int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type);
         if (cmp < 0) {
-            high = j;
+            high = idx;
         } else if (cmp > 0) {
-            low = j + 1;
+            low = idx + 1;
         } else {
-            return (type->value_type == OVSDB_TYPE_VOID
-                    || ovsdb_atom_equals(&a->values[i], &b->values[j],
-                                         type->value_type));
+            return idx;
         }
     }
-    return false;
+    return UINT_MAX;
+}
+
+/* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its
+ * index within 'datum', otherwise UINT_MAX.  'key_type' must be the type of
+ * the atoms stored in the 'keys' array in 'datum'.  'value_type' may be the
+ * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys.
+ */
+unsigned int
+ovsdb_datum_find_key_value(const struct ovsdb_datum *datum,
+                           const union ovsdb_atom *key,
+                           enum ovsdb_atomic_type key_type,
+                           const union ovsdb_atom *value,
+                           enum ovsdb_atomic_type value_type)
+{
+    unsigned int idx = ovsdb_datum_find_key(datum, key, key_type);
+    if (idx != UINT_MAX
+        && value_type != OVSDB_TYPE_VOID
+        && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) {
+        idx = UINT_MAX;
+    }
+    return idx;
+}
+
+/* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise
+ * UINT_MAX.  'type' must be the type of 'a' and 'b', except that
+ * type->value_type may be set to OVSDB_TYPE_VOID to compare keys but not
+ * values. */
+static unsigned int
+ovsdb_datum_find(const struct ovsdb_datum *a, int i,
+                 const struct ovsdb_datum *b,
+                 const struct ovsdb_type *type)
+{
+    return ovsdb_datum_find_key_value(b,
+                                      &a->keys[i], type->key_type,
+                                      a->values ? &a->values[i] : NULL,
+                                      type->value_type);
 }
 
 /* Returns true if every element in 'a' is also in 'b', false otherwise. */
@@ -686,7 +1129,7 @@ ovsdb_datum_includes_all(const struct ovsdb_datum *a,
     size_t i;
 
     for (i = 0; i < a->n; i++) {
-        if (!ovsdb_datum_contains(a, i, b, type)) {
+        if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) {
             return false;
         }
     }
@@ -702,12 +1145,130 @@ ovsdb_datum_excludes_all(const struct ovsdb_datum *a,
     size_t i;
 
     for (i = 0; i < a->n; i++) {
-        if (ovsdb_datum_contains(a, i, b, type)) {
+        if (ovsdb_datum_find(a, i, b, type) != UINT_MAX) {
             return false;
         }
     }
     return true;
 }
+
+static void
+ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type,
+                       unsigned int capacity)
+{
+    a->keys = xrealloc(a->keys, capacity * sizeof *a->keys);
+    if (type->value_type != OVSDB_TYPE_VOID) {
+        a->values = xrealloc(a->values, capacity * sizeof *a->values);
+    }
+}
+
+/* Removes the element with index 'idx' from 'datum', which has type 'type'.
+ * If 'idx' is not the last element in 'datum', then the removed element is
+ * replaced by the (former) last element.
+ *
+ * This function does not maintain ovsdb_datum invariants.  Use
+ * ovsdb_datum_sort() to check and restore these invariants. */
+void
+ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx,
+                          const struct ovsdb_type *type)
+{
+    ovsdb_atom_destroy(&datum->keys[idx], type->key_type);
+    datum->keys[idx] = datum->keys[datum->n - 1];
+    if (type->value_type != OVSDB_TYPE_VOID) {
+        ovsdb_atom_destroy(&datum->values[idx], type->value_type);
+        datum->values[idx] = datum->values[datum->n - 1];
+    }
+    datum->n--;
+}
+
+/* Adds the element with the given 'key' and 'value' to 'datum', which must
+ * have the specified 'type'.
+ *
+ * This function always allocates memory, so it is not an efficient way to add
+ * a number of elements to a datum.
+ *
+ * This function does not maintain ovsdb_datum invariants.  Use
+ * ovsdb_datum_sort() to check and restore these invariants.  (But a datum with
+ * 0 or 1 elements cannot violate the invariants anyhow.) */
+void
+ovsdb_datum_add_unsafe(struct ovsdb_datum *datum,
+                       const union ovsdb_atom *key,
+                       const union ovsdb_atom *value,
+                       const struct ovsdb_type *type)
+{
+    size_t idx = datum->n++;
+    datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys);
+    ovsdb_atom_clone(&datum->keys[idx], key, type->key_type);
+    if (type->value_type != OVSDB_TYPE_VOID) {
+        datum->values = xrealloc(datum->values,
+                                 datum->n * sizeof *datum->values);
+        ovsdb_atom_clone(&datum->values[idx], value, type->value_type);
+    }
+}
+
+void
+ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b,
+                  const struct ovsdb_type *type, bool replace)
+{
+    unsigned int n;
+    size_t bi;
+
+    n = a->n;
+    for (bi = 0; bi < b->n; bi++) {
+        unsigned int ai;
+
+        ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key_type);
+        if (ai == UINT_MAX) {
+            if (n == a->n) {
+                ovsdb_datum_reallocate(a, type, a->n + (b->n - bi));
+            }
+            ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key_type);
+            if (type->value_type != OVSDB_TYPE_VOID) {
+                ovsdb_atom_clone(&a->values[n], &b->values[bi],
+                                 type->value_type);
+            }
+            n++;
+        } else if (replace && type->value_type != OVSDB_TYPE_VOID) {
+            ovsdb_atom_destroy(&a->values[ai], type->value_type);
+            ovsdb_atom_clone(&a->values[ai], &b->values[bi],
+                             type->value_type);
+        }
+    }
+    if (n != a->n) {
+        struct ovsdb_error *error;
+        a->n = n;
+        error = ovsdb_datum_sort(a, type);
+        assert(!error);
+    }
+}
+
+void
+ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type,
+                     const struct ovsdb_datum *b,
+                     const struct ovsdb_type *b_type)
+{
+    bool changed = false;
+    size_t i;
+
+    assert(a_type->key_type == b_type->key_type);
+    assert(a_type->value_type == b_type->value_type
+           || b_type->value_type == OVSDB_TYPE_VOID);
+
+    /* XXX The big-O of this could easily be improved. */
+    for (i = 0; i < a->n; ) {
+        unsigned int idx = ovsdb_datum_find(a, i, b, b_type);
+        if (idx != UINT_MAX) {
+            changed = true;
+            ovsdb_datum_remove_unsafe(a, i, a_type);
+        } else {
+            i++;
+        }
+    }
+    if (changed) {
+        struct ovsdb_error *error = ovsdb_datum_sort(a, a_type);
+        assert(!error);
+    }
+}
 \f
 struct ovsdb_symbol_table {
     struct shash sh;
@@ -756,3 +1317,69 @@ ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
     symbol->used = used;
     shash_add(&symtab->sh, name, symbol);
 }
+\f
+/* Extracts a token from the beginning of 's' and returns a pointer just after
+ * the token.  Stores the token itself into '*outp', which the caller is
+ * responsible for freeing (with free()).
+ *
+ * If 's[0]' is a delimiter, the returned token is the empty string.
+ *
+ * A token extends from 's' to the first delimiter, as defined by
+ * ovsdb_token_is_delim(), or until the end of the string.  A delimiter can be
+ * escaped with a backslash, in which case the backslash does not appear in the
+ * output.  Double quotes also cause delimiters to be ignored, but the double
+ * quotes are retained in the output.  (Backslashes inside double quotes are
+ * not removed, either.)
+ */
+char *
+ovsdb_token_parse(const char **s, char **outp)
+{
+    const char *p;
+    struct ds out;
+    bool in_quotes;
+    char *error;
+
+    ds_init(&out);
+    in_quotes = false;
+    for (p = *s; *p != '\0'; ) {
+        int c = *p++;
+        if (c == '\\') {
+            if (in_quotes) {
+                ds_put_char(&out, '\\');
+            }
+            if (!*p) {
+                error = xasprintf("%s: backslash at end of argument", *s);
+                goto error;
+            }
+            ds_put_char(&out, *p++);
+        } else if (!in_quotes && ovsdb_token_is_delim(c)) {
+            p--;
+            break;
+        } else {
+            ds_put_char(&out, c);
+            if (c == '"') {
+                in_quotes = !in_quotes;
+            }
+        }
+    }
+    if (in_quotes) {
+        error = xasprintf("%s: quoted string extends past end of argument",
+                          *s);
+        goto error;
+    }
+    *outp = ds_cstr(&out);
+    *s = p;
+    return NULL;
+
+error:
+    ds_destroy(&out);
+    *outp = NULL;
+    return error;
+}
+
+/* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */
+bool
+ovsdb_token_is_delim(unsigned char c)
+{
+    return strchr(":=, []{}", c) != NULL;
+}