1 /* Copyright (c) 2009, 2010 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include "ovsdb-data.h"
26 #include "dynamic-string.h"
28 #include "ovsdb-error.h"
35 wrap_json(const char *name, struct json *wrapped)
37 return json_array_create_2(json_string_create(name), wrapped);
40 /* Initializes 'atom' with the default value of the given 'type'.
42 * The default value for an atom is as defined in ovsdb/SPECS:
44 * - "integer" or "real": 0
48 * - "string": "" (the empty string)
50 * - "uuid": 00000000-0000-0000-0000-000000000000
52 * The caller must eventually arrange for 'atom' to be destroyed (with
53 * ovsdb_atom_destroy()). */
55 ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
61 case OVSDB_TYPE_INTEGER:
69 case OVSDB_TYPE_BOOLEAN:
70 atom->boolean = false;
73 case OVSDB_TYPE_STRING:
74 atom->string = xmemdup("", 1);
78 uuid_zero(&atom->uuid);
87 /* Returns a read-only atom of the given 'type' that has the default value for
88 * 'type'. The caller must not modify or free the returned atom.
90 * See ovsdb_atom_init_default() for an explanation of the default value of an
92 const union ovsdb_atom *
93 ovsdb_atom_default(enum ovsdb_atomic_type type)
95 static union ovsdb_atom default_atoms[OVSDB_N_TYPES];
101 for (i = 0; i < OVSDB_N_TYPES; i++) {
102 if (i != OVSDB_TYPE_VOID) {
103 ovsdb_atom_init_default(&default_atoms[i], i);
109 assert(ovsdb_atomic_type_is_valid(type));
110 return &default_atoms[type];
113 /* Returns true if 'atom', which must have the given 'type', has the default
114 * value for that type.
116 * See ovsdb_atom_init_default() for an explanation of the default value of an
119 ovsdb_atom_is_default(const union ovsdb_atom *atom,
120 enum ovsdb_atomic_type type)
123 case OVSDB_TYPE_VOID:
126 case OVSDB_TYPE_INTEGER:
127 return atom->integer == 0;
129 case OVSDB_TYPE_REAL:
130 return atom->real == 0.0;
132 case OVSDB_TYPE_BOOLEAN:
133 return atom->boolean == false;
135 case OVSDB_TYPE_STRING:
136 return atom->string[0] == '\0';
138 case OVSDB_TYPE_UUID:
139 return uuid_is_zero(&atom->uuid);
147 /* Initializes 'new' as a copy of 'old', with the given 'type'.
149 * The caller must eventually arrange for 'new' to be destroyed (with
150 * ovsdb_atom_destroy()). */
152 ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old,
153 enum ovsdb_atomic_type type)
156 case OVSDB_TYPE_VOID:
159 case OVSDB_TYPE_INTEGER:
160 new->integer = old->integer;
163 case OVSDB_TYPE_REAL:
164 new->real = old->real;
167 case OVSDB_TYPE_BOOLEAN:
168 new->boolean = old->boolean;
171 case OVSDB_TYPE_STRING:
172 new->string = xstrdup(old->string);
175 case OVSDB_TYPE_UUID:
176 new->uuid = old->uuid;
185 /* Swaps the contents of 'a' and 'b', which need not have the same type. */
187 ovsdb_atom_swap(union ovsdb_atom *a, union ovsdb_atom *b)
189 union ovsdb_atom tmp = *a;
194 /* Returns a hash value for 'atom', which has the specified 'type', folding
195 * 'basis' into the calculation. */
197 ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
201 case OVSDB_TYPE_VOID:
204 case OVSDB_TYPE_INTEGER:
205 return hash_int(atom->integer, basis);
207 case OVSDB_TYPE_REAL:
208 return hash_double(atom->real, basis);
210 case OVSDB_TYPE_BOOLEAN:
211 return hash_boolean(atom->boolean, basis);
213 case OVSDB_TYPE_STRING:
214 return hash_string(atom->string, basis);
216 case OVSDB_TYPE_UUID:
217 return hash_int(uuid_hash(&atom->uuid), basis);
225 /* Compares 'a' and 'b', which both have type 'type', and returns a
226 * strcmp()-like result. */
228 ovsdb_atom_compare_3way(const union ovsdb_atom *a,
229 const union ovsdb_atom *b,
230 enum ovsdb_atomic_type type)
233 case OVSDB_TYPE_VOID:
236 case OVSDB_TYPE_INTEGER:
237 return a->integer < b->integer ? -1 : a->integer > b->integer;
239 case OVSDB_TYPE_REAL:
240 return a->real < b->real ? -1 : a->real > b->real;
242 case OVSDB_TYPE_BOOLEAN:
243 return a->boolean - b->boolean;
245 case OVSDB_TYPE_STRING:
246 return strcmp(a->string, b->string);
248 case OVSDB_TYPE_UUID:
249 return uuid_compare_3way(&a->uuid, &b->uuid);
257 static struct ovsdb_error *
258 unwrap_json(const struct json *json, const char *name,
259 enum json_type value_type, const struct json **value)
261 if (json->type != JSON_ARRAY
262 || json->u.array.n != 2
263 || json->u.array.elems[0]->type != JSON_STRING
264 || (name && strcmp(json->u.array.elems[0]->u.string, name))
265 || json->u.array.elems[1]->type != value_type)
268 return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name,
269 json_type_to_string(value_type));
271 *value = json->u.array.elems[1];
275 static struct ovsdb_error *
276 parse_json_pair(const struct json *json,
277 const struct json **elem0, const struct json **elem1)
279 if (json->type != JSON_ARRAY || json->u.array.n != 2) {
280 return ovsdb_syntax_error(json, NULL, "expected 2-element array");
282 *elem0 = json->u.array.elems[0];
283 *elem1 = json->u.array.elems[1];
287 static struct ovsdb_error * WARN_UNUSED_RESULT
288 ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
289 struct ovsdb_symbol_table *symtab)
291 struct ovsdb_error *error0;
292 const struct json *value;
294 error0 = unwrap_json(json, "uuid", JSON_STRING, &value);
296 const char *uuid_string = json_string(value);
297 if (!uuid_from_string(uuid, uuid_string)) {
298 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
302 struct ovsdb_error *error1;
304 error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
306 const char *name = json_string(value);
308 ovsdb_error_destroy(error0);
309 *uuid = ovsdb_symbol_table_insert(symtab, name)->uuid;
312 ovsdb_error_destroy(error1);
318 static struct ovsdb_error * WARN_UNUSED_RESULT
319 ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
320 const struct json *json,
321 struct ovsdb_symbol_table *symtab)
324 case OVSDB_TYPE_VOID:
327 case OVSDB_TYPE_INTEGER:
328 if (json->type == JSON_INTEGER) {
329 atom->integer = json->u.integer;
334 case OVSDB_TYPE_REAL:
335 if (json->type == JSON_INTEGER) {
336 atom->real = json->u.integer;
338 } else if (json->type == JSON_REAL) {
339 atom->real = json->u.real;
344 case OVSDB_TYPE_BOOLEAN:
345 if (json->type == JSON_TRUE) {
346 atom->boolean = true;
348 } else if (json->type == JSON_FALSE) {
349 atom->boolean = false;
354 case OVSDB_TYPE_STRING:
355 if (json->type == JSON_STRING) {
356 atom->string = xstrdup(json->u.string);
361 case OVSDB_TYPE_UUID:
362 return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab);
369 return ovsdb_syntax_error(json, NULL, "expected %s",
370 ovsdb_atomic_type_to_string(type));
373 /* Parses 'json' as an atom of the type described by 'base'. If successful,
374 * returns NULL and initializes 'atom' with the parsed atom. On failure,
375 * returns an error and the contents of 'atom' are indeterminate. The caller
376 * is responsible for freeing the error or the atom that is returned.
378 * Violations of constraints expressed by 'base' are treated as errors.
380 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
381 * ovsdb/SPECS for information about this, and for the syntax that this
382 * function accepts. */
384 ovsdb_atom_from_json(union ovsdb_atom *atom,
385 const struct ovsdb_base_type *base,
386 const struct json *json,
387 struct ovsdb_symbol_table *symtab)
389 struct ovsdb_error *error;
391 error = ovsdb_atom_from_json__(atom, base->type, json, symtab);
396 error = ovsdb_atom_check_constraints(atom, base);
398 ovsdb_atom_destroy(atom, base->type);
403 /* Converts 'atom', of the specified 'type', to JSON format, and returns the
404 * JSON. The caller is responsible for freeing the returned JSON.
406 * Refer to ovsdb/SPECS for the format of the JSON that this function
409 ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
412 case OVSDB_TYPE_VOID:
415 case OVSDB_TYPE_INTEGER:
416 return json_integer_create(atom->integer);
418 case OVSDB_TYPE_REAL:
419 return json_real_create(atom->real);
421 case OVSDB_TYPE_BOOLEAN:
422 return json_boolean_create(atom->boolean);
424 case OVSDB_TYPE_STRING:
425 return json_string_create(atom->string);
427 case OVSDB_TYPE_UUID:
428 return wrap_json("uuid", json_string_create_nocopy(
429 xasprintf(UUID_FMT, UUID_ARGS(&atom->uuid))));
438 ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
439 const char *s, struct ovsdb_symbol_table *symtab)
442 case OVSDB_TYPE_VOID:
445 case OVSDB_TYPE_INTEGER: {
446 long long int integer;
447 if (!str_to_llong(s, 10, &integer)) {
448 return xasprintf("\"%s\" is not a valid integer", s);
450 atom->integer = integer;
454 case OVSDB_TYPE_REAL:
455 if (!str_to_double(s, &atom->real)) {
456 return xasprintf("\"%s\" is not a valid real number", s);
458 /* Our JSON input routines map negative zero to zero, so do that here
459 * too for consistency. */
460 if (atom->real == 0.0) {
465 case OVSDB_TYPE_BOOLEAN:
466 if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on")
467 || !strcmp(s, "1")) {
468 atom->boolean = true;
469 } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off")
470 || !strcmp(s, "0")) {
471 atom->boolean = false;
473 return xasprintf("\"%s\" is not a valid boolean "
474 "(use \"true\" or \"false\")", s);
478 case OVSDB_TYPE_STRING:
480 return xstrdup("An empty string is not valid as input; "
481 "use \"\" to represent the empty string");
482 } else if (*s == '"') {
483 size_t s_len = strlen(s);
485 if (s_len < 2 || s[s_len - 1] != '"') {
486 return xasprintf("%s: missing quote at end of "
488 } else if (!json_string_unescape(s + 1, s_len - 2,
490 char *error = xasprintf("%s: %s", s, atom->string);
495 atom->string = xstrdup(s);
499 case OVSDB_TYPE_UUID:
501 atom->uuid = ovsdb_symbol_table_insert(symtab, s)->uuid;
502 } else if (!uuid_from_string(&atom->uuid, s)) {
503 return xasprintf("\"%s\" is not a valid UUID", s);
515 /* Initializes 'atom' to a value of type 'base' parsed from 's', which takes
516 * one of the following forms:
518 * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign.
520 * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by
523 * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false",
524 * "no", "off", or "0" for false.
526 * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
527 * an arbitrary string.
529 * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format. If 'symtab' is nonnull,
530 * then an identifier beginning with '@' is also acceptable. If the
531 * named identifier is already in 'symtab', then the associated UUID is
532 * used; otherwise, a new, random UUID is used and added to the symbol
535 * Returns a null pointer if successful, otherwise an error message describing
536 * the problem. On failure, the contents of 'atom' are indeterminate. The
537 * caller is responsible for freeing the atom or the error.
540 ovsdb_atom_from_string(union ovsdb_atom *atom,
541 const struct ovsdb_base_type *base, const char *s,
542 struct ovsdb_symbol_table *symtab)
544 struct ovsdb_error *error;
547 msg = ovsdb_atom_from_string__(atom, base->type, s, symtab);
552 error = ovsdb_atom_check_constraints(atom, base);
554 msg = ovsdb_error_to_string(error);
555 ovsdb_error_destroy(error);
561 string_needs_quotes(const char *s)
567 if (!isalpha(c) && c != '_') {
571 while ((c = *p++) != '\0') {
572 if (!isalpha(c) && c != '_' && c != '-' && c != '.') {
577 if (!strcmp(s, "true") || !strcmp(s, "false")) {
584 /* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable
585 * to ovsdb_atom_from_string(). */
587 ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
591 case OVSDB_TYPE_VOID:
594 case OVSDB_TYPE_INTEGER:
595 ds_put_format(out, "%"PRId64, atom->integer);
598 case OVSDB_TYPE_REAL:
599 ds_put_format(out, "%.*g", DBL_DIG, atom->real);
602 case OVSDB_TYPE_BOOLEAN:
603 ds_put_cstr(out, atom->boolean ? "true" : "false");
606 case OVSDB_TYPE_STRING:
607 if (string_needs_quotes(atom->string)) {
610 json.type = JSON_STRING;
611 json.u.string = atom->string;
612 json_to_ds(&json, 0, out);
614 ds_put_cstr(out, atom->string);
618 case OVSDB_TYPE_UUID:
619 ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid));
628 static struct ovsdb_error *
629 check_string_constraints(const char *s,
630 const struct ovsdb_string_constraints *c)
635 msg = utf8_validate(s, &n_chars);
637 struct ovsdb_error *error;
639 error = ovsdb_error("constraint violation",
640 "\"%s\" is not a valid UTF-8 string: %s",
646 if (n_chars < c->minLen) {
648 "constraint violation",
649 "\"%s\" length %zu is less than minimum allowed "
650 "length %u", s, n_chars, c->minLen);
651 } else if (n_chars > c->maxLen) {
653 "constraint violation",
654 "\"%s\" length %zu is greater than maximum allowed "
655 "length %u", s, n_chars, c->maxLen);
661 /* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
662 * (base->type must specify 'atom''s type.) Returns a null pointer if the
663 * constraints are met, otherwise an error that explains the violation.
665 * Checking UUID constraints is deferred to transaction commit time, so this
666 * function does nothing for UUID constraints. */
668 ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
669 const struct ovsdb_base_type *base)
672 && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) {
673 struct ovsdb_error *error;
674 struct ds actual = DS_EMPTY_INITIALIZER;
675 struct ds valid = DS_EMPTY_INITIALIZER;
677 ovsdb_atom_to_string(atom, base->type, &actual);
678 ovsdb_datum_to_string(base->enum_,
679 ovsdb_base_type_get_enum_type(base->type),
681 error = ovsdb_error("constraint violation",
682 "%s is not one of the allowed values (%s)",
683 ds_cstr(&actual), ds_cstr(&valid));
690 switch (base->type) {
691 case OVSDB_TYPE_VOID:
694 case OVSDB_TYPE_INTEGER:
695 if (atom->integer >= base->u.integer.min
696 && atom->integer <= base->u.integer.max) {
698 } else if (base->u.integer.min != INT64_MIN) {
699 if (base->u.integer.max != INT64_MAX) {
700 return ovsdb_error("constraint violation",
701 "%"PRId64" is not in the valid range "
702 "%"PRId64" to %"PRId64" (inclusive)",
704 base->u.integer.min, base->u.integer.max);
706 return ovsdb_error("constraint violation",
707 "%"PRId64" is less than minimum allowed "
709 atom->integer, base->u.integer.min);
712 return ovsdb_error("constraint violation",
713 "%"PRId64" is greater than maximum allowed "
715 atom->integer, base->u.integer.max);
719 case OVSDB_TYPE_REAL:
720 if (atom->real >= base->u.real.min && atom->real <= base->u.real.max) {
722 } else if (base->u.real.min != -DBL_MAX) {
723 if (base->u.real.max != DBL_MAX) {
724 return ovsdb_error("constraint violation",
725 "%.*g is not in the valid range "
726 "%.*g to %.*g (inclusive)",
728 DBL_DIG, base->u.real.min,
729 DBL_DIG, base->u.real.max);
731 return ovsdb_error("constraint violation",
732 "%.*g is less than minimum allowed "
735 DBL_DIG, base->u.real.min);
738 return ovsdb_error("constraint violation",
739 "%.*g is greater than maximum allowed "
742 DBL_DIG, base->u.real.max);
746 case OVSDB_TYPE_BOOLEAN:
749 case OVSDB_TYPE_STRING:
750 return check_string_constraints(atom->string, &base->u.string);
752 case OVSDB_TYPE_UUID:
761 static union ovsdb_atom *
762 alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
764 if (type != OVSDB_TYPE_VOID && n) {
765 union ovsdb_atom *atoms;
768 atoms = xmalloc(n * sizeof *atoms);
769 for (i = 0; i < n; i++) {
770 ovsdb_atom_init_default(&atoms[i], type);
774 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
775 * treated as xmalloc(1). */
780 /* Initializes 'datum' as an empty datum. (An empty datum can be treated as
783 ovsdb_datum_init_empty(struct ovsdb_datum *datum)
787 datum->values = NULL;
790 /* Initializes 'datum' as a datum that has the default value for 'type'.
792 * The default value for a particular type is as defined in ovsdb/SPECS:
794 * - If n_min is 0, then the default value is the empty set (or map).
796 * - If n_min is 1, the default value is a single value or a single
797 * key-value pair, whose key and value are the defaults for their
798 * atomic types. (See ovsdb_atom_init_default() for details.)
800 * - n_min > 1 is invalid. See ovsdb_type_is_valid().
803 ovsdb_datum_init_default(struct ovsdb_datum *datum,
804 const struct ovsdb_type *type)
806 datum->n = type->n_min;
807 datum->keys = alloc_default_atoms(type->key.type, datum->n);
808 datum->values = alloc_default_atoms(type->value.type, datum->n);
811 /* Returns a read-only datum of the given 'type' that has the default value for
812 * 'type'. The caller must not modify or free the returned datum.
814 * See ovsdb_datum_init_default() for an explanation of the default value of a
816 const struct ovsdb_datum *
817 ovsdb_datum_default(const struct ovsdb_type *type)
819 if (type->n_min == 0) {
820 static const struct ovsdb_datum empty;
822 } else if (type->n_min == 1) {
823 static struct ovsdb_datum default_data[OVSDB_N_TYPES][OVSDB_N_TYPES];
824 struct ovsdb_datum *d;
825 int kt = type->key.type;
826 int vt = type->value.type;
828 assert(ovsdb_type_is_valid(type));
830 d = &default_data[kt][vt];
833 d->keys = (union ovsdb_atom *) ovsdb_atom_default(kt);
834 if (vt != OVSDB_TYPE_VOID) {
835 d->values = (union ovsdb_atom *) ovsdb_atom_default(vt);
844 /* Returns true if 'datum', which must have the given 'type', has the default
845 * value for that type.
847 * See ovsdb_datum_init_default() for an explanation of the default value of a
850 ovsdb_datum_is_default(const struct ovsdb_datum *datum,
851 const struct ovsdb_type *type)
855 if (datum->n != type->n_min) {
858 for (i = 0; i < datum->n; i++) {
859 if (!ovsdb_atom_is_default(&datum->keys[i], type->key.type)) {
862 if (type->value.type != OVSDB_TYPE_VOID
863 && !ovsdb_atom_is_default(&datum->values[i], type->value.type)) {
871 static union ovsdb_atom *
872 clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n)
874 if (type != OVSDB_TYPE_VOID && n) {
875 union ovsdb_atom *new;
878 new = xmalloc(n * sizeof *new);
879 for (i = 0; i < n; i++) {
880 ovsdb_atom_clone(&new[i], &old[i], type);
884 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
885 * treated as xmalloc(1). */
890 /* Initializes 'new' as a copy of 'old', with the given 'type'.
892 * The caller must eventually arrange for 'new' to be destroyed (with
893 * ovsdb_datum_destroy()). */
895 ovsdb_datum_clone(struct ovsdb_datum *new, const struct ovsdb_datum *old,
896 const struct ovsdb_type *type)
898 unsigned int n = old->n;
900 new->keys = clone_atoms(old->keys, type->key.type, n);
901 new->values = clone_atoms(old->values, type->value.type, n);
905 free_data(enum ovsdb_atomic_type type,
906 union ovsdb_atom *atoms, size_t n_atoms)
908 if (ovsdb_atom_needs_destruction(type)) {
910 for (i = 0; i < n_atoms; i++) {
911 ovsdb_atom_destroy(&atoms[i], type);
917 /* Frees the data owned by 'datum', which must have the given 'type'.
919 * This does not actually call free(datum). If necessary, the caller must be
920 * responsible for that. */
922 ovsdb_datum_destroy(struct ovsdb_datum *datum, const struct ovsdb_type *type)
924 free_data(type->key.type, datum->keys, datum->n);
925 free_data(type->value.type, datum->values, datum->n);
928 /* Swaps the contents of 'a' and 'b', which need not have the same type. */
930 ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b)
932 struct ovsdb_datum tmp = *a;
937 struct ovsdb_datum_sort_cbdata {
938 enum ovsdb_atomic_type key_type;
939 enum ovsdb_atomic_type value_type;
940 struct ovsdb_datum *datum;
944 ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_)
946 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
949 retval = ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
950 &cbdata->datum->keys[b],
952 if (retval || cbdata->value_type == OVSDB_TYPE_VOID) {
956 return ovsdb_atom_compare_3way(&cbdata->datum->values[a],
957 &cbdata->datum->values[b],
962 ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_)
964 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
966 ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
967 if (cbdata->datum->values) {
968 ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
973 ovsdb_datum_sort__(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type,
974 enum ovsdb_atomic_type value_type)
976 struct ovsdb_datum_sort_cbdata cbdata;
978 cbdata.key_type = key_type;
979 cbdata.value_type = value_type;
980 cbdata.datum = datum;
981 sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
985 /* The keys in an ovsdb_datum must be unique and in sorted order. Most
986 * functions that modify an ovsdb_datum maintain these invariants. For those
987 * that don't, this function checks and restores these invariants for 'datum',
988 * whose keys are of type 'key_type'.
990 * This function returns NULL if successful, otherwise an error message. The
991 * caller must free the returned error when it is no longer needed. On error,
992 * 'datum' is sorted but not unique. */
994 ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type)
1002 ovsdb_datum_sort__(datum, key_type, OVSDB_TYPE_VOID);
1004 for (i = 0; i < datum->n - 1; i++) {
1005 if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
1007 if (datum->values) {
1008 return ovsdb_error(NULL, "map contains duplicate key");
1010 return ovsdb_error(NULL, "set contains duplicate");
1017 /* This function is the same as ovsdb_datum_sort(), except that the caller
1018 * knows that 'datum' is unique. The operation therefore "cannot fail", so
1019 * this function assert-fails if it actually does. */
1021 ovsdb_datum_sort_assert(struct ovsdb_datum *datum,
1022 enum ovsdb_atomic_type key_type)
1024 struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type);
1030 /* This is similar to ovsdb_datum_sort(), except that it drops duplicate keys
1031 * instead of reporting an error. In a map type, the smallest value among a
1032 * group of duplicate pairs is retained and the others are dropped.
1034 * Returns the number of keys (or pairs) that were dropped. */
1036 ovsdb_datum_sort_unique(struct ovsdb_datum *datum,
1037 enum ovsdb_atomic_type key_type,
1038 enum ovsdb_atomic_type value_type)
1046 ovsdb_datum_sort__(datum, key_type, value_type);
1049 for (src = 1; src < datum->n; src++) {
1050 if (ovsdb_atom_equals(&datum->keys[src], &datum->keys[dst - 1],
1052 ovsdb_atom_destroy(&datum->keys[src], key_type);
1053 if (value_type != OVSDB_TYPE_VOID) {
1054 ovsdb_atom_destroy(&datum->values[src], value_type);
1058 datum->keys[dst] = datum->keys[src];
1059 if (value_type != OVSDB_TYPE_VOID) {
1060 datum->values[dst] = datum->values[src];
1067 return datum->n - src;
1070 /* Checks that each of the atoms in 'datum' conforms to the constraints
1071 * specified by its 'type'. Returns an error if a constraint is violated,
1072 * otherwise a null pointer.
1074 * This function is not commonly useful because the most ordinary way to obtain
1075 * a datum is ultimately via ovsdb_atom_from_string() or
1076 * ovsdb_atom_from_json(), which check constraints themselves. */
1077 struct ovsdb_error *
1078 ovsdb_datum_check_constraints(const struct ovsdb_datum *datum,
1079 const struct ovsdb_type *type)
1081 struct ovsdb_error *error;
1084 for (i = 0; i < datum->n; i++) {
1085 error = ovsdb_atom_check_constraints(&datum->keys[i], &type->key);
1091 if (type->value.type != OVSDB_TYPE_VOID) {
1092 for (i = 0; i < datum->n; i++) {
1093 error = ovsdb_atom_check_constraints(&datum->values[i],
1104 static struct ovsdb_error *
1105 ovsdb_datum_from_json__(struct ovsdb_datum *datum,
1106 const struct ovsdb_type *type,
1107 const struct json *json,
1108 struct ovsdb_symbol_table *symtab)
1110 struct ovsdb_error *error;
1112 if (ovsdb_type_is_map(type)
1113 || (json->type == JSON_ARRAY
1114 && json->u.array.n > 0
1115 && json->u.array.elems[0]->type == JSON_STRING
1116 && !strcmp(json->u.array.elems[0]->u.string, "set"))) {
1117 bool is_map = ovsdb_type_is_map(type);
1118 const char *class = is_map ? "map" : "set";
1119 const struct json *inner;
1123 error = unwrap_json(json, class, JSON_ARRAY, &inner);
1128 n = inner->u.array.n;
1129 if (n < type->n_min || n > type->n_max) {
1130 return ovsdb_syntax_error(json, NULL, "%s must have %u to "
1131 "%u members but %zu are present",
1132 class, type->n_min, type->n_max, n);
1136 datum->keys = xmalloc(n * sizeof *datum->keys);
1137 datum->values = is_map ? xmalloc(n * sizeof *datum->values) : NULL;
1138 for (i = 0; i < n; i++) {
1139 const struct json *element = inner->u.array.elems[i];
1140 const struct json *key = NULL;
1141 const struct json *value = NULL;
1146 error = parse_json_pair(element, &key, &value);
1152 error = ovsdb_atom_from_json(&datum->keys[i], &type->key,
1159 error = ovsdb_atom_from_json(&datum->values[i],
1160 &type->value, value, symtab);
1162 ovsdb_atom_destroy(&datum->keys[i], type->key.type);
1172 ovsdb_datum_destroy(datum, type);
1176 datum->keys = xmalloc(sizeof *datum->keys);
1177 datum->values = NULL;
1179 error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
1188 /* Parses 'json' as a datum of the type described by 'type'. If successful,
1189 * returns NULL and initializes 'datum' with the parsed datum. On failure,
1190 * returns an error and the contents of 'datum' are indeterminate. The caller
1191 * is responsible for freeing the error or the datum that is returned.
1193 * Violations of constraints expressed by 'type' are treated as errors.
1195 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
1196 * ovsdb/SPECS for information about this, and for the syntax that this
1197 * function accepts. */
1198 struct ovsdb_error *
1199 ovsdb_datum_from_json(struct ovsdb_datum *datum,
1200 const struct ovsdb_type *type,
1201 const struct json *json,
1202 struct ovsdb_symbol_table *symtab)
1204 struct ovsdb_error *error;
1206 error = ovsdb_datum_from_json__(datum, type, json, symtab);
1211 error = ovsdb_datum_sort(datum, type->key.type);
1213 ovsdb_datum_destroy(datum, type);
1218 /* Converts 'datum', of the specified 'type', to JSON format, and returns the
1219 * JSON. The caller is responsible for freeing the returned JSON.
1221 * 'type' constraints on datum->n are ignored.
1223 * Refer to ovsdb/SPECS for the format of the JSON that this function
1226 ovsdb_datum_to_json(const struct ovsdb_datum *datum,
1227 const struct ovsdb_type *type)
1229 if (datum->n == 1 && !ovsdb_type_is_map(type)) {
1230 return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
1231 } else if (type->value.type == OVSDB_TYPE_VOID) {
1232 struct json **elems;
1235 elems = xmalloc(datum->n * sizeof *elems);
1236 for (i = 0; i < datum->n; i++) {
1237 elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
1240 return wrap_json("set", json_array_create(elems, datum->n));
1242 struct json **elems;
1245 elems = xmalloc(datum->n * sizeof *elems);
1246 for (i = 0; i < datum->n; i++) {
1247 elems[i] = json_array_create_2(
1248 ovsdb_atom_to_json(&datum->keys[i], type->key.type),
1249 ovsdb_atom_to_json(&datum->values[i], type->value.type));
1252 return wrap_json("map", json_array_create(elems, datum->n));
1257 skip_spaces(const char *p)
1259 while (isspace((unsigned char) *p)) {
1266 parse_atom_token(const char **s, const struct ovsdb_base_type *base,
1267 union ovsdb_atom *atom, struct ovsdb_symbol_table *symtab)
1269 char *token, *error;
1271 error = ovsdb_token_parse(s, &token);
1273 error = ovsdb_atom_from_string(atom, base, token, symtab);
1280 parse_key_value(const char **s, const struct ovsdb_type *type,
1281 union ovsdb_atom *key, union ovsdb_atom *value,
1282 struct ovsdb_symbol_table *symtab)
1284 const char *start = *s;
1287 error = parse_atom_token(s, &type->key, key, symtab);
1288 if (!error && type->value.type != OVSDB_TYPE_VOID) {
1289 *s = skip_spaces(*s);
1292 *s = skip_spaces(*s);
1293 error = parse_atom_token(s, &type->value, value, symtab);
1295 error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
1299 ovsdb_atom_destroy(key, type->key.type);
1306 free_key_value(const struct ovsdb_type *type,
1307 union ovsdb_atom *key, union ovsdb_atom *value)
1309 ovsdb_atom_destroy(key, type->key.type);
1310 if (type->value.type != OVSDB_TYPE_VOID) {
1311 ovsdb_atom_destroy(value, type->value.type);
1315 /* Initializes 'datum' as a datum of the given 'type', parsing its contents
1316 * from 's'. The format of 's' is a series of space or comma separated atoms
1317 * or, for a map, '='-delimited pairs of atoms. Each atom must in a format
1318 * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed
1319 * in "[]" or a map in "{}"; for an empty set or map these punctuators are
1322 * Optionally, a symbol table may be supplied as 'symtab'. It is passed to
1323 * ovsdb_atom_to_string(). */
1325 ovsdb_datum_from_string(struct ovsdb_datum *datum,
1326 const struct ovsdb_type *type, const char *s,
1327 struct ovsdb_symbol_table *symtab)
1329 bool is_map = ovsdb_type_is_map(type);
1330 struct ovsdb_error *dberror;
1335 ovsdb_datum_init_empty(datum);
1337 /* Swallow a leading delimiter if there is one. */
1339 if (*p == (is_map ? '{' : '[')) {
1340 end_delim = is_map ? '}' : ']';
1341 p = skip_spaces(p + 1);
1344 return xstrdup("use \"{}\" to specify the empty map");
1346 return xstrdup("use \"[]\" to specify the empty set");
1352 while (*p && *p != end_delim) {
1353 union ovsdb_atom key, value;
1355 if (ovsdb_token_is_delim(*p)) {
1356 error = xasprintf("%s: unexpected \"%c\" parsing %s",
1357 s, *p, ovsdb_type_to_english(type));
1362 error = parse_key_value(&p, type, &key, &value, symtab);
1366 ovsdb_datum_add_unsafe(datum, &key, &value, type);
1367 free_key_value(type, &key, &value);
1369 /* Skip optional white space and comma. */
1372 p = skip_spaces(p + 1);
1376 if (*p != end_delim) {
1377 error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim);
1381 p = skip_spaces(p + 1);
1383 error = xasprintf("%s: trailing garbage after \"%c\"",
1389 if (datum->n < type->n_min) {
1390 error = xasprintf("%s: %u %s specified but the minimum number is %u",
1391 s, datum->n, is_map ? "pair(s)" : "value(s)",
1394 } else if (datum->n > type->n_max) {
1395 error = xasprintf("%s: %u %s specified but the maximum number is %u",
1396 s, datum->n, is_map ? "pair(s)" : "value(s)",
1401 dberror = ovsdb_datum_sort(datum, type->key.type);
1403 ovsdb_error_destroy(dberror);
1404 if (ovsdb_type_is_map(type)) {
1405 error = xasprintf("%s: map contains duplicate key", s);
1407 error = xasprintf("%s: set contains duplicate value", s);
1415 ovsdb_datum_destroy(datum, type);
1416 ovsdb_datum_init_empty(datum);
1420 /* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable
1421 * to ovsdb_datum_from_string(). */
1423 ovsdb_datum_to_string(const struct ovsdb_datum *datum,
1424 const struct ovsdb_type *type, struct ds *out)
1426 bool is_map = ovsdb_type_is_map(type);
1429 if (type->n_max > 1 || !datum->n) {
1430 ds_put_char(out, is_map ? '{' : '[');
1432 for (i = 0; i < datum->n; i++) {
1434 ds_put_cstr(out, ", ");
1437 ovsdb_atom_to_string(&datum->keys[i], type->key.type, out);
1439 ds_put_char(out, '=');
1440 ovsdb_atom_to_string(&datum->values[i], type->value.type, out);
1443 if (type->n_max > 1 || !datum->n) {
1444 ds_put_char(out, is_map ? '}' : ']');
1448 /* Initializes 'datum' as a string-to-string map whose contents are taken from
1449 * 'sh'. Destroys 'sh'. */
1451 ovsdb_datum_from_shash(struct ovsdb_datum *datum, struct shash *sh)
1453 struct shash_node *node, *next;
1456 datum->n = shash_count(sh);
1457 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
1458 datum->values = xmalloc(datum->n * sizeof *datum->values);
1461 SHASH_FOR_EACH_SAFE (node, next, sh) {
1462 datum->keys[i].string = node->name;
1463 datum->values[i].string = node->data;
1464 shash_steal(sh, node);
1467 assert(i == datum->n);
1470 ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
1474 hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms,
1475 unsigned int n, uint32_t basis)
1477 if (type != OVSDB_TYPE_VOID) {
1480 for (i = 0; i < n; i++) {
1481 basis = ovsdb_atom_hash(&atoms[i], type, basis);
1488 ovsdb_datum_hash(const struct ovsdb_datum *datum,
1489 const struct ovsdb_type *type, uint32_t basis)
1491 basis = hash_atoms(type->key.type, datum->keys, datum->n, basis);
1492 basis ^= (type->key.type << 24) | (type->value.type << 16) | datum->n;
1493 basis = hash_atoms(type->value.type, datum->values, datum->n, basis);
1498 atom_arrays_compare_3way(const union ovsdb_atom *a,
1499 const union ovsdb_atom *b,
1500 enum ovsdb_atomic_type type,
1505 for (i = 0; i < n; i++) {
1506 int cmp = ovsdb_atom_compare_3way(&a[i], &b[i], type);
1516 ovsdb_datum_equals(const struct ovsdb_datum *a,
1517 const struct ovsdb_datum *b,
1518 const struct ovsdb_type *type)
1520 return !ovsdb_datum_compare_3way(a, b, type);
1524 ovsdb_datum_compare_3way(const struct ovsdb_datum *a,
1525 const struct ovsdb_datum *b,
1526 const struct ovsdb_type *type)
1531 return a->n < b->n ? -1 : 1;
1534 cmp = atom_arrays_compare_3way(a->keys, b->keys, type->key.type, a->n);
1539 return (type->value.type == OVSDB_TYPE_VOID ? 0
1540 : atom_arrays_compare_3way(a->values, b->values, type->value.type,
1544 /* If 'key' is one of the keys in 'datum', returns its index within 'datum',
1545 * otherwise UINT_MAX. 'key.type' must be the type of the atoms stored in the
1546 * 'keys' array in 'datum'.
1549 ovsdb_datum_find_key(const struct ovsdb_datum *datum,
1550 const union ovsdb_atom *key,
1551 enum ovsdb_atomic_type key_type)
1553 unsigned int low = 0;
1554 unsigned int high = datum->n;
1555 while (low < high) {
1556 unsigned int idx = (low + high) / 2;
1557 int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type);
1560 } else if (cmp > 0) {
1569 /* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its
1570 * index within 'datum', otherwise UINT_MAX. 'key.type' must be the type of
1571 * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the
1572 * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys.
1575 ovsdb_datum_find_key_value(const struct ovsdb_datum *datum,
1576 const union ovsdb_atom *key,
1577 enum ovsdb_atomic_type key_type,
1578 const union ovsdb_atom *value,
1579 enum ovsdb_atomic_type value_type)
1581 unsigned int idx = ovsdb_datum_find_key(datum, key, key_type);
1583 && value_type != OVSDB_TYPE_VOID
1584 && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) {
1590 /* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise
1591 * UINT_MAX. 'type' must be the type of 'a' and 'b', except that
1592 * type->value.type may be set to OVSDB_TYPE_VOID to compare keys but not
1595 ovsdb_datum_find(const struct ovsdb_datum *a, int i,
1596 const struct ovsdb_datum *b,
1597 const struct ovsdb_type *type)
1599 return ovsdb_datum_find_key_value(b,
1600 &a->keys[i], type->key.type,
1601 a->values ? &a->values[i] : NULL,
1605 /* Returns true if every element in 'a' is also in 'b', false otherwise. */
1607 ovsdb_datum_includes_all(const struct ovsdb_datum *a,
1608 const struct ovsdb_datum *b,
1609 const struct ovsdb_type *type)
1613 for (i = 0; i < a->n; i++) {
1614 if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) {
1621 /* Returns true if no element in 'a' is also in 'b', false otherwise. */
1623 ovsdb_datum_excludes_all(const struct ovsdb_datum *a,
1624 const struct ovsdb_datum *b,
1625 const struct ovsdb_type *type)
1629 for (i = 0; i < a->n; i++) {
1630 if (ovsdb_datum_find(a, i, b, type) != UINT_MAX) {
1638 ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type,
1639 unsigned int capacity)
1641 a->keys = xrealloc(a->keys, capacity * sizeof *a->keys);
1642 if (type->value.type != OVSDB_TYPE_VOID) {
1643 a->values = xrealloc(a->values, capacity * sizeof *a->values);
1647 /* Removes the element with index 'idx' from 'datum', which has type 'type'.
1648 * If 'idx' is not the last element in 'datum', then the removed element is
1649 * replaced by the (former) last element.
1651 * This function does not maintain ovsdb_datum invariants. Use
1652 * ovsdb_datum_sort() to check and restore these invariants. */
1654 ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx,
1655 const struct ovsdb_type *type)
1657 ovsdb_atom_destroy(&datum->keys[idx], type->key.type);
1658 datum->keys[idx] = datum->keys[datum->n - 1];
1659 if (type->value.type != OVSDB_TYPE_VOID) {
1660 ovsdb_atom_destroy(&datum->values[idx], type->value.type);
1661 datum->values[idx] = datum->values[datum->n - 1];
1666 /* Adds the element with the given 'key' and 'value' to 'datum', which must
1667 * have the specified 'type'.
1669 * This function always allocates memory, so it is not an efficient way to add
1670 * a number of elements to a datum.
1672 * This function does not maintain ovsdb_datum invariants. Use
1673 * ovsdb_datum_sort() to check and restore these invariants. (But a datum with
1674 * 0 or 1 elements cannot violate the invariants anyhow.) */
1676 ovsdb_datum_add_unsafe(struct ovsdb_datum *datum,
1677 const union ovsdb_atom *key,
1678 const union ovsdb_atom *value,
1679 const struct ovsdb_type *type)
1681 size_t idx = datum->n++;
1682 datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys);
1683 ovsdb_atom_clone(&datum->keys[idx], key, type->key.type);
1684 if (type->value.type != OVSDB_TYPE_VOID) {
1685 datum->values = xrealloc(datum->values,
1686 datum->n * sizeof *datum->values);
1687 ovsdb_atom_clone(&datum->values[idx], value, type->value.type);
1692 ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b,
1693 const struct ovsdb_type *type, bool replace)
1699 for (bi = 0; bi < b->n; bi++) {
1702 ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key.type);
1703 if (ai == UINT_MAX) {
1705 ovsdb_datum_reallocate(a, type, a->n + (b->n - bi));
1707 ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key.type);
1708 if (type->value.type != OVSDB_TYPE_VOID) {
1709 ovsdb_atom_clone(&a->values[n], &b->values[bi],
1713 } else if (replace && type->value.type != OVSDB_TYPE_VOID) {
1714 ovsdb_atom_destroy(&a->values[ai], type->value.type);
1715 ovsdb_atom_clone(&a->values[ai], &b->values[bi],
1720 struct ovsdb_error *error;
1722 error = ovsdb_datum_sort(a, type->key.type);
1728 ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type,
1729 const struct ovsdb_datum *b,
1730 const struct ovsdb_type *b_type)
1732 bool changed = false;
1735 assert(a_type->key.type == b_type->key.type);
1736 assert(a_type->value.type == b_type->value.type
1737 || b_type->value.type == OVSDB_TYPE_VOID);
1739 /* XXX The big-O of this could easily be improved. */
1740 for (i = 0; i < a->n; ) {
1741 unsigned int idx = ovsdb_datum_find(a, i, b, b_type);
1742 if (idx != UINT_MAX) {
1744 ovsdb_datum_remove_unsafe(a, i, a_type);
1750 ovsdb_datum_sort_assert(a, a_type->key.type);
1754 struct ovsdb_symbol_table {
1758 struct ovsdb_symbol_table *
1759 ovsdb_symbol_table_create(void)
1761 struct ovsdb_symbol_table *symtab = xmalloc(sizeof *symtab);
1762 shash_init(&symtab->sh);
1767 ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *symtab)
1770 shash_destroy_free_data(&symtab->sh);
1775 struct ovsdb_symbol *
1776 ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab,
1779 return shash_find_data(&symtab->sh, name);
1782 struct ovsdb_symbol *
1783 ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
1784 const struct uuid *uuid, bool used)
1786 struct ovsdb_symbol *symbol;
1788 assert(!ovsdb_symbol_table_get(symtab, name));
1789 symbol = xmalloc(sizeof *symbol);
1790 symbol->uuid = *uuid;
1791 symbol->used = used;
1792 shash_add(&symtab->sh, name, symbol);
1796 struct ovsdb_symbol *
1797 ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab,
1800 struct ovsdb_symbol *symbol;
1802 symbol = ovsdb_symbol_table_get(symtab, name);
1806 uuid_generate(&uuid);
1807 symbol = ovsdb_symbol_table_put(symtab, name, &uuid, false);
1813 ovsdb_symbol_table_find_unused(const struct ovsdb_symbol_table *symtab)
1815 struct shash_node *node;
1817 SHASH_FOR_EACH (node, &symtab->sh) {
1818 struct ovsdb_symbol *symbol = node->data;
1819 if (!symbol->used) {
1827 /* Extracts a token from the beginning of 's' and returns a pointer just after
1828 * the token. Stores the token itself into '*outp', which the caller is
1829 * responsible for freeing (with free()).
1831 * If 's[0]' is a delimiter, the returned token is the empty string.
1833 * A token extends from 's' to the first delimiter, as defined by
1834 * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be
1835 * escaped with a backslash, in which case the backslash does not appear in the
1836 * output. Double quotes also cause delimiters to be ignored, but the double
1837 * quotes are retained in the output. (Backslashes inside double quotes are
1838 * not removed, either.)
1841 ovsdb_token_parse(const char **s, char **outp)
1850 for (p = *s; *p != '\0'; ) {
1854 ds_put_char(&out, '\\');
1857 error = xasprintf("%s: backslash at end of argument", *s);
1860 ds_put_char(&out, *p++);
1861 } else if (!in_quotes && ovsdb_token_is_delim(c)) {
1865 ds_put_char(&out, c);
1867 in_quotes = !in_quotes;
1872 error = xasprintf("%s: quoted string extends past end of argument",
1876 *outp = ds_cstr(&out);
1886 /* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */
1888 ovsdb_token_is_delim(unsigned char c)
1890 return strchr(":=, []{}!<>", c) != NULL;