1 /* Copyright (c) 2009, 2010, 2011 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"
29 #include "ovsdb-parser.h"
36 wrap_json(const char *name, struct json *wrapped)
38 return json_array_create_2(json_string_create(name), wrapped);
41 /* Initializes 'atom' with the default value of the given 'type'.
43 * The default value for an atom is as defined in ovsdb/SPECS:
45 * - "integer" or "real": 0
49 * - "string": "" (the empty string)
51 * - "uuid": 00000000-0000-0000-0000-000000000000
53 * The caller must eventually arrange for 'atom' to be destroyed (with
54 * ovsdb_atom_destroy()). */
56 ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
62 case OVSDB_TYPE_INTEGER:
70 case OVSDB_TYPE_BOOLEAN:
71 atom->boolean = false;
74 case OVSDB_TYPE_STRING:
75 atom->string = xmemdup("", 1);
79 uuid_zero(&atom->uuid);
88 /* Returns a read-only atom of the given 'type' that has the default value for
89 * 'type'. The caller must not modify or free the returned atom.
91 * See ovsdb_atom_init_default() for an explanation of the default value of an
93 const union ovsdb_atom *
94 ovsdb_atom_default(enum ovsdb_atomic_type type)
96 static union ovsdb_atom default_atoms[OVSDB_N_TYPES];
102 for (i = 0; i < OVSDB_N_TYPES; i++) {
103 if (i != OVSDB_TYPE_VOID) {
104 ovsdb_atom_init_default(&default_atoms[i], i);
110 assert(ovsdb_atomic_type_is_valid(type));
111 return &default_atoms[type];
114 /* Returns true if 'atom', which must have the given 'type', has the default
115 * value for that type.
117 * See ovsdb_atom_init_default() for an explanation of the default value of an
120 ovsdb_atom_is_default(const union ovsdb_atom *atom,
121 enum ovsdb_atomic_type type)
124 case OVSDB_TYPE_VOID:
127 case OVSDB_TYPE_INTEGER:
128 return atom->integer == 0;
130 case OVSDB_TYPE_REAL:
131 return atom->real == 0.0;
133 case OVSDB_TYPE_BOOLEAN:
134 return atom->boolean == false;
136 case OVSDB_TYPE_STRING:
137 return atom->string[0] == '\0';
139 case OVSDB_TYPE_UUID:
140 return uuid_is_zero(&atom->uuid);
148 /* Initializes 'new' as a copy of 'old', with the given 'type'.
150 * The caller must eventually arrange for 'new' to be destroyed (with
151 * ovsdb_atom_destroy()). */
153 ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old,
154 enum ovsdb_atomic_type type)
157 case OVSDB_TYPE_VOID:
160 case OVSDB_TYPE_INTEGER:
161 new->integer = old->integer;
164 case OVSDB_TYPE_REAL:
165 new->real = old->real;
168 case OVSDB_TYPE_BOOLEAN:
169 new->boolean = old->boolean;
172 case OVSDB_TYPE_STRING:
173 new->string = xstrdup(old->string);
176 case OVSDB_TYPE_UUID:
177 new->uuid = old->uuid;
186 /* Swaps the contents of 'a' and 'b', which need not have the same type. */
188 ovsdb_atom_swap(union ovsdb_atom *a, union ovsdb_atom *b)
190 union ovsdb_atom tmp = *a;
195 /* Returns a hash value for 'atom', which has the specified 'type', folding
196 * 'basis' into the calculation. */
198 ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
202 case OVSDB_TYPE_VOID:
205 case OVSDB_TYPE_INTEGER:
206 return hash_int(atom->integer, basis);
208 case OVSDB_TYPE_REAL:
209 return hash_double(atom->real, basis);
211 case OVSDB_TYPE_BOOLEAN:
212 return hash_boolean(atom->boolean, basis);
214 case OVSDB_TYPE_STRING:
215 return hash_string(atom->string, basis);
217 case OVSDB_TYPE_UUID:
218 return hash_int(uuid_hash(&atom->uuid), basis);
226 /* Compares 'a' and 'b', which both have type 'type', and returns a
227 * strcmp()-like result. */
229 ovsdb_atom_compare_3way(const union ovsdb_atom *a,
230 const union ovsdb_atom *b,
231 enum ovsdb_atomic_type type)
234 case OVSDB_TYPE_VOID:
237 case OVSDB_TYPE_INTEGER:
238 return a->integer < b->integer ? -1 : a->integer > b->integer;
240 case OVSDB_TYPE_REAL:
241 return a->real < b->real ? -1 : a->real > b->real;
243 case OVSDB_TYPE_BOOLEAN:
244 return a->boolean - b->boolean;
246 case OVSDB_TYPE_STRING:
247 return strcmp(a->string, b->string);
249 case OVSDB_TYPE_UUID:
250 return uuid_compare_3way(&a->uuid, &b->uuid);
258 static struct ovsdb_error *
259 unwrap_json(const struct json *json, const char *name,
260 enum json_type value_type, const struct json **value)
262 if (json->type != JSON_ARRAY
263 || json->u.array.n != 2
264 || json->u.array.elems[0]->type != JSON_STRING
265 || (name && strcmp(json->u.array.elems[0]->u.string, name))
266 || json->u.array.elems[1]->type != value_type)
269 return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name,
270 json_type_to_string(value_type));
272 *value = json->u.array.elems[1];
276 static struct ovsdb_error *
277 parse_json_pair(const struct json *json,
278 const struct json **elem0, const struct json **elem1)
280 if (json->type != JSON_ARRAY || json->u.array.n != 2) {
281 return ovsdb_syntax_error(json, NULL, "expected 2-element array");
283 *elem0 = json->u.array.elems[0];
284 *elem1 = json->u.array.elems[1];
288 static struct ovsdb_error * WARN_UNUSED_RESULT
289 ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
290 struct ovsdb_symbol_table *symtab)
292 struct ovsdb_error *error0;
293 const struct json *value;
295 error0 = unwrap_json(json, "uuid", JSON_STRING, &value);
297 const char *uuid_string = json_string(value);
298 if (!uuid_from_string(uuid, uuid_string)) {
299 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
303 struct ovsdb_error *error1;
305 error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
307 const char *name = json_string(value);
309 ovsdb_error_destroy(error0);
310 *uuid = ovsdb_symbol_table_insert(symtab, name)->uuid;
311 if (!ovsdb_parser_is_id(json_string(value))) {
312 return ovsdb_syntax_error(json, NULL, "named-uuid string is "
317 ovsdb_error_destroy(error1);
323 static struct ovsdb_error * WARN_UNUSED_RESULT
324 ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
325 const struct json *json,
326 struct ovsdb_symbol_table *symtab)
329 case OVSDB_TYPE_VOID:
332 case OVSDB_TYPE_INTEGER:
333 if (json->type == JSON_INTEGER) {
334 atom->integer = json->u.integer;
339 case OVSDB_TYPE_REAL:
340 if (json->type == JSON_INTEGER) {
341 atom->real = json->u.integer;
343 } else if (json->type == JSON_REAL) {
344 atom->real = json->u.real;
349 case OVSDB_TYPE_BOOLEAN:
350 if (json->type == JSON_TRUE) {
351 atom->boolean = true;
353 } else if (json->type == JSON_FALSE) {
354 atom->boolean = false;
359 case OVSDB_TYPE_STRING:
360 if (json->type == JSON_STRING) {
361 atom->string = xstrdup(json->u.string);
366 case OVSDB_TYPE_UUID:
367 return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab);
374 return ovsdb_syntax_error(json, NULL, "expected %s",
375 ovsdb_atomic_type_to_string(type));
378 /* Parses 'json' as an atom of the type described by 'base'. If successful,
379 * returns NULL and initializes 'atom' with the parsed atom. On failure,
380 * returns an error and the contents of 'atom' are indeterminate. The caller
381 * is responsible for freeing the error or the atom that is returned.
383 * Violations of constraints expressed by 'base' are treated as errors.
385 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
386 * ovsdb/SPECS for information about this, and for the syntax that this
387 * function accepts. */
389 ovsdb_atom_from_json(union ovsdb_atom *atom,
390 const struct ovsdb_base_type *base,
391 const struct json *json,
392 struct ovsdb_symbol_table *symtab)
394 struct ovsdb_error *error;
396 error = ovsdb_atom_from_json__(atom, base->type, json, symtab);
401 error = ovsdb_atom_check_constraints(atom, base);
403 ovsdb_atom_destroy(atom, base->type);
408 /* Converts 'atom', of the specified 'type', to JSON format, and returns the
409 * JSON. The caller is responsible for freeing the returned JSON.
411 * Refer to ovsdb/SPECS for the format of the JSON that this function
414 ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
417 case OVSDB_TYPE_VOID:
420 case OVSDB_TYPE_INTEGER:
421 return json_integer_create(atom->integer);
423 case OVSDB_TYPE_REAL:
424 return json_real_create(atom->real);
426 case OVSDB_TYPE_BOOLEAN:
427 return json_boolean_create(atom->boolean);
429 case OVSDB_TYPE_STRING:
430 return json_string_create(atom->string);
432 case OVSDB_TYPE_UUID:
433 return wrap_json("uuid", json_string_create_nocopy(
434 xasprintf(UUID_FMT, UUID_ARGS(&atom->uuid))));
443 ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
444 const char *s, struct ovsdb_symbol_table *symtab)
447 case OVSDB_TYPE_VOID:
450 case OVSDB_TYPE_INTEGER: {
451 long long int integer;
452 if (!str_to_llong(s, 10, &integer)) {
453 return xasprintf("\"%s\" is not a valid integer", s);
455 atom->integer = integer;
459 case OVSDB_TYPE_REAL:
460 if (!str_to_double(s, &atom->real)) {
461 return xasprintf("\"%s\" is not a valid real number", s);
463 /* Our JSON input routines map negative zero to zero, so do that here
464 * too for consistency. */
465 if (atom->real == 0.0) {
470 case OVSDB_TYPE_BOOLEAN:
471 if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on")
472 || !strcmp(s, "1")) {
473 atom->boolean = true;
474 } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off")
475 || !strcmp(s, "0")) {
476 atom->boolean = false;
478 return xasprintf("\"%s\" is not a valid boolean "
479 "(use \"true\" or \"false\")", s);
483 case OVSDB_TYPE_STRING:
485 return xstrdup("An empty string is not valid as input; "
486 "use \"\" to represent the empty string");
487 } else if (*s == '"') {
488 size_t s_len = strlen(s);
490 if (s_len < 2 || s[s_len - 1] != '"') {
491 return xasprintf("%s: missing quote at end of "
493 } else if (!json_string_unescape(s + 1, s_len - 2,
495 char *error = xasprintf("%s: %s", s, atom->string);
500 atom->string = xstrdup(s);
504 case OVSDB_TYPE_UUID:
506 atom->uuid = ovsdb_symbol_table_insert(symtab, s)->uuid;
507 } else if (!uuid_from_string(&atom->uuid, s)) {
508 return xasprintf("\"%s\" is not a valid UUID", s);
520 /* Initializes 'atom' to a value of type 'base' parsed from 's', which takes
521 * one of the following forms:
523 * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign.
525 * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by
528 * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false",
529 * "no", "off", or "0" for false.
531 * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
532 * an arbitrary string.
534 * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format. If 'symtab' is nonnull,
535 * then an identifier beginning with '@' is also acceptable. If the
536 * named identifier is already in 'symtab', then the associated UUID is
537 * used; otherwise, a new, random UUID is used and added to the symbol
540 * Returns a null pointer if successful, otherwise an error message describing
541 * the problem. On failure, the contents of 'atom' are indeterminate. The
542 * caller is responsible for freeing the atom or the error.
545 ovsdb_atom_from_string(union ovsdb_atom *atom,
546 const struct ovsdb_base_type *base, const char *s,
547 struct ovsdb_symbol_table *symtab)
549 struct ovsdb_error *error;
552 msg = ovsdb_atom_from_string__(atom, base->type, s, symtab);
557 error = ovsdb_atom_check_constraints(atom, base);
559 msg = ovsdb_error_to_string(error);
560 ovsdb_error_destroy(error);
566 string_needs_quotes(const char *s)
572 if (!isalpha(c) && c != '_') {
576 while ((c = *p++) != '\0') {
577 if (!isalpha(c) && c != '_' && c != '-' && c != '.') {
582 if (!strcmp(s, "true") || !strcmp(s, "false")) {
589 /* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable
590 * to ovsdb_atom_from_string(). */
592 ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
596 case OVSDB_TYPE_VOID:
599 case OVSDB_TYPE_INTEGER:
600 ds_put_format(out, "%"PRId64, atom->integer);
603 case OVSDB_TYPE_REAL:
604 ds_put_format(out, "%.*g", DBL_DIG, atom->real);
607 case OVSDB_TYPE_BOOLEAN:
608 ds_put_cstr(out, atom->boolean ? "true" : "false");
611 case OVSDB_TYPE_STRING:
612 if (string_needs_quotes(atom->string)) {
615 json.type = JSON_STRING;
616 json.u.string = atom->string;
617 json_to_ds(&json, 0, out);
619 ds_put_cstr(out, atom->string);
623 case OVSDB_TYPE_UUID:
624 ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid));
633 /* Appends 'atom' (which has the given 'type') to 'out', in a bare string
634 * format that cannot be parsed uniformly back into a datum but is easier for
635 * shell scripts, etc., to deal with. */
637 ovsdb_atom_to_bare(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
640 if (type == OVSDB_TYPE_STRING) {
641 ds_put_cstr(out, atom->string);
643 ovsdb_atom_to_string(atom, type, out);
647 static struct ovsdb_error *
648 check_string_constraints(const char *s,
649 const struct ovsdb_string_constraints *c)
654 msg = utf8_validate(s, &n_chars);
656 struct ovsdb_error *error;
658 error = ovsdb_error("constraint violation",
659 "\"%s\" is not a valid UTF-8 string: %s",
665 if (n_chars < c->minLen) {
667 "constraint violation",
668 "\"%s\" length %zu is less than minimum allowed "
669 "length %u", s, n_chars, c->minLen);
670 } else if (n_chars > c->maxLen) {
672 "constraint violation",
673 "\"%s\" length %zu is greater than maximum allowed "
674 "length %u", s, n_chars, c->maxLen);
680 /* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
681 * (base->type must specify 'atom''s type.) Returns a null pointer if the
682 * constraints are met, otherwise an error that explains the violation.
684 * Checking UUID constraints is deferred to transaction commit time, so this
685 * function does nothing for UUID constraints. */
687 ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
688 const struct ovsdb_base_type *base)
691 && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) {
692 struct ovsdb_error *error;
693 struct ds actual = DS_EMPTY_INITIALIZER;
694 struct ds valid = DS_EMPTY_INITIALIZER;
696 ovsdb_atom_to_string(atom, base->type, &actual);
697 ovsdb_datum_to_string(base->enum_,
698 ovsdb_base_type_get_enum_type(base->type),
700 error = ovsdb_error("constraint violation",
701 "%s is not one of the allowed values (%s)",
702 ds_cstr(&actual), ds_cstr(&valid));
709 switch (base->type) {
710 case OVSDB_TYPE_VOID:
713 case OVSDB_TYPE_INTEGER:
714 if (atom->integer >= base->u.integer.min
715 && atom->integer <= base->u.integer.max) {
717 } else if (base->u.integer.min != INT64_MIN) {
718 if (base->u.integer.max != INT64_MAX) {
719 return ovsdb_error("constraint violation",
720 "%"PRId64" is not in the valid range "
721 "%"PRId64" to %"PRId64" (inclusive)",
723 base->u.integer.min, base->u.integer.max);
725 return ovsdb_error("constraint violation",
726 "%"PRId64" is less than minimum allowed "
728 atom->integer, base->u.integer.min);
731 return ovsdb_error("constraint violation",
732 "%"PRId64" is greater than maximum allowed "
734 atom->integer, base->u.integer.max);
738 case OVSDB_TYPE_REAL:
739 if (atom->real >= base->u.real.min && atom->real <= base->u.real.max) {
741 } else if (base->u.real.min != -DBL_MAX) {
742 if (base->u.real.max != DBL_MAX) {
743 return ovsdb_error("constraint violation",
744 "%.*g is not in the valid range "
745 "%.*g to %.*g (inclusive)",
747 DBL_DIG, base->u.real.min,
748 DBL_DIG, base->u.real.max);
750 return ovsdb_error("constraint violation",
751 "%.*g is less than minimum allowed "
754 DBL_DIG, base->u.real.min);
757 return ovsdb_error("constraint violation",
758 "%.*g is greater than maximum allowed "
761 DBL_DIG, base->u.real.max);
765 case OVSDB_TYPE_BOOLEAN:
768 case OVSDB_TYPE_STRING:
769 return check_string_constraints(atom->string, &base->u.string);
771 case OVSDB_TYPE_UUID:
780 static union ovsdb_atom *
781 alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
783 if (type != OVSDB_TYPE_VOID && n) {
784 union ovsdb_atom *atoms;
787 atoms = xmalloc(n * sizeof *atoms);
788 for (i = 0; i < n; i++) {
789 ovsdb_atom_init_default(&atoms[i], type);
793 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
794 * treated as xmalloc(1). */
799 /* Initializes 'datum' as an empty datum. (An empty datum can be treated as
802 ovsdb_datum_init_empty(struct ovsdb_datum *datum)
806 datum->values = NULL;
809 /* Initializes 'datum' as a datum that has the default value for 'type'.
811 * The default value for a particular type is as defined in ovsdb/SPECS:
813 * - If n_min is 0, then the default value is the empty set (or map).
815 * - If n_min is 1, the default value is a single value or a single
816 * key-value pair, whose key and value are the defaults for their
817 * atomic types. (See ovsdb_atom_init_default() for details.)
819 * - n_min > 1 is invalid. See ovsdb_type_is_valid().
822 ovsdb_datum_init_default(struct ovsdb_datum *datum,
823 const struct ovsdb_type *type)
825 datum->n = type->n_min;
826 datum->keys = alloc_default_atoms(type->key.type, datum->n);
827 datum->values = alloc_default_atoms(type->value.type, datum->n);
830 /* Returns a read-only datum of the given 'type' that has the default value for
831 * 'type'. The caller must not modify or free the returned datum.
833 * See ovsdb_datum_init_default() for an explanation of the default value of a
835 const struct ovsdb_datum *
836 ovsdb_datum_default(const struct ovsdb_type *type)
838 if (type->n_min == 0) {
839 static const struct ovsdb_datum empty;
841 } else if (type->n_min == 1) {
842 static struct ovsdb_datum default_data[OVSDB_N_TYPES][OVSDB_N_TYPES];
843 struct ovsdb_datum *d;
844 int kt = type->key.type;
845 int vt = type->value.type;
847 assert(ovsdb_type_is_valid(type));
849 d = &default_data[kt][vt];
852 d->keys = (union ovsdb_atom *) ovsdb_atom_default(kt);
853 if (vt != OVSDB_TYPE_VOID) {
854 d->values = (union ovsdb_atom *) ovsdb_atom_default(vt);
863 /* Returns true if 'datum', which must have the given 'type', has the default
864 * value for that type.
866 * See ovsdb_datum_init_default() for an explanation of the default value of a
869 ovsdb_datum_is_default(const struct ovsdb_datum *datum,
870 const struct ovsdb_type *type)
874 if (datum->n != type->n_min) {
877 for (i = 0; i < datum->n; i++) {
878 if (!ovsdb_atom_is_default(&datum->keys[i], type->key.type)) {
881 if (type->value.type != OVSDB_TYPE_VOID
882 && !ovsdb_atom_is_default(&datum->values[i], type->value.type)) {
890 static union ovsdb_atom *
891 clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n)
893 if (type != OVSDB_TYPE_VOID && n) {
894 union ovsdb_atom *new;
897 new = xmalloc(n * sizeof *new);
898 for (i = 0; i < n; i++) {
899 ovsdb_atom_clone(&new[i], &old[i], type);
903 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
904 * treated as xmalloc(1). */
909 /* Initializes 'new' as a copy of 'old', with the given 'type'.
911 * The caller must eventually arrange for 'new' to be destroyed (with
912 * ovsdb_datum_destroy()). */
914 ovsdb_datum_clone(struct ovsdb_datum *new, const struct ovsdb_datum *old,
915 const struct ovsdb_type *type)
917 unsigned int n = old->n;
919 new->keys = clone_atoms(old->keys, type->key.type, n);
920 new->values = clone_atoms(old->values, type->value.type, n);
924 free_data(enum ovsdb_atomic_type type,
925 union ovsdb_atom *atoms, size_t n_atoms)
927 if (ovsdb_atom_needs_destruction(type)) {
929 for (i = 0; i < n_atoms; i++) {
930 ovsdb_atom_destroy(&atoms[i], type);
936 /* Frees the data owned by 'datum', which must have the given 'type'.
938 * This does not actually call free(datum). If necessary, the caller must be
939 * responsible for that. */
941 ovsdb_datum_destroy(struct ovsdb_datum *datum, const struct ovsdb_type *type)
943 free_data(type->key.type, datum->keys, datum->n);
944 free_data(type->value.type, datum->values, datum->n);
947 /* Swaps the contents of 'a' and 'b', which need not have the same type. */
949 ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b)
951 struct ovsdb_datum tmp = *a;
956 struct ovsdb_datum_sort_cbdata {
957 enum ovsdb_atomic_type key_type;
958 enum ovsdb_atomic_type value_type;
959 struct ovsdb_datum *datum;
963 ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_)
965 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
968 retval = ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
969 &cbdata->datum->keys[b],
971 if (retval || cbdata->value_type == OVSDB_TYPE_VOID) {
975 return ovsdb_atom_compare_3way(&cbdata->datum->values[a],
976 &cbdata->datum->values[b],
981 ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_)
983 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
985 ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
986 if (cbdata->datum->values) {
987 ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
992 ovsdb_datum_sort__(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type,
993 enum ovsdb_atomic_type value_type)
995 struct ovsdb_datum_sort_cbdata cbdata;
997 cbdata.key_type = key_type;
998 cbdata.value_type = value_type;
999 cbdata.datum = datum;
1000 sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
1004 /* The keys in an ovsdb_datum must be unique and in sorted order. Most
1005 * functions that modify an ovsdb_datum maintain these invariants. For those
1006 * that don't, this function checks and restores these invariants for 'datum',
1007 * whose keys are of type 'key_type'.
1009 * This function returns NULL if successful, otherwise an error message. The
1010 * caller must free the returned error when it is no longer needed. On error,
1011 * 'datum' is sorted but not unique. */
1012 struct ovsdb_error *
1013 ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type)
1021 ovsdb_datum_sort__(datum, key_type, OVSDB_TYPE_VOID);
1023 for (i = 0; i < datum->n - 1; i++) {
1024 if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
1026 if (datum->values) {
1027 return ovsdb_error(NULL, "map contains duplicate key");
1029 return ovsdb_error(NULL, "set contains duplicate");
1036 /* This function is the same as ovsdb_datum_sort(), except that the caller
1037 * knows that 'datum' is unique. The operation therefore "cannot fail", so
1038 * this function assert-fails if it actually does. */
1040 ovsdb_datum_sort_assert(struct ovsdb_datum *datum,
1041 enum ovsdb_atomic_type key_type)
1043 struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type);
1049 /* This is similar to ovsdb_datum_sort(), except that it drops duplicate keys
1050 * instead of reporting an error. In a map type, the smallest value among a
1051 * group of duplicate pairs is retained and the others are dropped.
1053 * Returns the number of keys (or pairs) that were dropped. */
1055 ovsdb_datum_sort_unique(struct ovsdb_datum *datum,
1056 enum ovsdb_atomic_type key_type,
1057 enum ovsdb_atomic_type value_type)
1065 ovsdb_datum_sort__(datum, key_type, value_type);
1068 for (src = 1; src < datum->n; src++) {
1069 if (ovsdb_atom_equals(&datum->keys[src], &datum->keys[dst - 1],
1071 ovsdb_atom_destroy(&datum->keys[src], key_type);
1072 if (value_type != OVSDB_TYPE_VOID) {
1073 ovsdb_atom_destroy(&datum->values[src], value_type);
1077 datum->keys[dst] = datum->keys[src];
1078 if (value_type != OVSDB_TYPE_VOID) {
1079 datum->values[dst] = datum->values[src];
1086 return datum->n - src;
1089 /* Checks that each of the atoms in 'datum' conforms to the constraints
1090 * specified by its 'type'. Returns an error if a constraint is violated,
1091 * otherwise a null pointer.
1093 * This function is not commonly useful because the most ordinary way to obtain
1094 * a datum is ultimately via ovsdb_atom_from_string() or
1095 * ovsdb_atom_from_json(), which check constraints themselves. */
1096 struct ovsdb_error *
1097 ovsdb_datum_check_constraints(const struct ovsdb_datum *datum,
1098 const struct ovsdb_type *type)
1100 struct ovsdb_error *error;
1103 for (i = 0; i < datum->n; i++) {
1104 error = ovsdb_atom_check_constraints(&datum->keys[i], &type->key);
1110 if (type->value.type != OVSDB_TYPE_VOID) {
1111 for (i = 0; i < datum->n; i++) {
1112 error = ovsdb_atom_check_constraints(&datum->values[i],
1123 static struct ovsdb_error *
1124 ovsdb_datum_from_json__(struct ovsdb_datum *datum,
1125 const struct ovsdb_type *type,
1126 const struct json *json,
1127 struct ovsdb_symbol_table *symtab)
1129 struct ovsdb_error *error;
1131 if (ovsdb_type_is_map(type)
1132 || (json->type == JSON_ARRAY
1133 && json->u.array.n > 0
1134 && json->u.array.elems[0]->type == JSON_STRING
1135 && !strcmp(json->u.array.elems[0]->u.string, "set"))) {
1136 bool is_map = ovsdb_type_is_map(type);
1137 const char *class = is_map ? "map" : "set";
1138 const struct json *inner;
1142 error = unwrap_json(json, class, JSON_ARRAY, &inner);
1147 n = inner->u.array.n;
1148 if (n < type->n_min || n > type->n_max) {
1149 return ovsdb_syntax_error(json, NULL, "%s must have %u to "
1150 "%u members but %zu are present",
1151 class, type->n_min, type->n_max, n);
1155 datum->keys = xmalloc(n * sizeof *datum->keys);
1156 datum->values = is_map ? xmalloc(n * sizeof *datum->values) : NULL;
1157 for (i = 0; i < n; i++) {
1158 const struct json *element = inner->u.array.elems[i];
1159 const struct json *key = NULL;
1160 const struct json *value = NULL;
1165 error = parse_json_pair(element, &key, &value);
1171 error = ovsdb_atom_from_json(&datum->keys[i], &type->key,
1178 error = ovsdb_atom_from_json(&datum->values[i],
1179 &type->value, value, symtab);
1181 ovsdb_atom_destroy(&datum->keys[i], type->key.type);
1191 ovsdb_datum_destroy(datum, type);
1195 datum->keys = xmalloc(sizeof *datum->keys);
1196 datum->values = NULL;
1198 error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
1207 /* Parses 'json' as a datum of the type described by 'type'. If successful,
1208 * returns NULL and initializes 'datum' with the parsed datum. On failure,
1209 * returns an error and the contents of 'datum' are indeterminate. The caller
1210 * is responsible for freeing the error or the datum that is returned.
1212 * Violations of constraints expressed by 'type' are treated as errors.
1214 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
1215 * ovsdb/SPECS for information about this, and for the syntax that this
1216 * function accepts. */
1217 struct ovsdb_error *
1218 ovsdb_datum_from_json(struct ovsdb_datum *datum,
1219 const struct ovsdb_type *type,
1220 const struct json *json,
1221 struct ovsdb_symbol_table *symtab)
1223 struct ovsdb_error *error;
1225 error = ovsdb_datum_from_json__(datum, type, json, symtab);
1230 error = ovsdb_datum_sort(datum, type->key.type);
1232 ovsdb_datum_destroy(datum, type);
1237 /* Converts 'datum', of the specified 'type', to JSON format, and returns the
1238 * JSON. The caller is responsible for freeing the returned JSON.
1240 * 'type' constraints on datum->n are ignored.
1242 * Refer to ovsdb/SPECS for the format of the JSON that this function
1245 ovsdb_datum_to_json(const struct ovsdb_datum *datum,
1246 const struct ovsdb_type *type)
1248 if (datum->n == 1 && !ovsdb_type_is_map(type)) {
1249 return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
1250 } else if (type->value.type == OVSDB_TYPE_VOID) {
1251 struct json **elems;
1254 elems = xmalloc(datum->n * sizeof *elems);
1255 for (i = 0; i < datum->n; i++) {
1256 elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
1259 return wrap_json("set", json_array_create(elems, datum->n));
1261 struct json **elems;
1264 elems = xmalloc(datum->n * sizeof *elems);
1265 for (i = 0; i < datum->n; i++) {
1266 elems[i] = json_array_create_2(
1267 ovsdb_atom_to_json(&datum->keys[i], type->key.type),
1268 ovsdb_atom_to_json(&datum->values[i], type->value.type));
1271 return wrap_json("map", json_array_create(elems, datum->n));
1276 skip_spaces(const char *p)
1278 while (isspace((unsigned char) *p)) {
1285 parse_atom_token(const char **s, const struct ovsdb_base_type *base,
1286 union ovsdb_atom *atom, struct ovsdb_symbol_table *symtab)
1288 char *token, *error;
1290 error = ovsdb_token_parse(s, &token);
1292 error = ovsdb_atom_from_string(atom, base, token, symtab);
1299 parse_key_value(const char **s, const struct ovsdb_type *type,
1300 union ovsdb_atom *key, union ovsdb_atom *value,
1301 struct ovsdb_symbol_table *symtab)
1303 const char *start = *s;
1306 error = parse_atom_token(s, &type->key, key, symtab);
1307 if (!error && type->value.type != OVSDB_TYPE_VOID) {
1308 *s = skip_spaces(*s);
1311 *s = skip_spaces(*s);
1312 error = parse_atom_token(s, &type->value, value, symtab);
1314 error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
1318 ovsdb_atom_destroy(key, type->key.type);
1325 free_key_value(const struct ovsdb_type *type,
1326 union ovsdb_atom *key, union ovsdb_atom *value)
1328 ovsdb_atom_destroy(key, type->key.type);
1329 if (type->value.type != OVSDB_TYPE_VOID) {
1330 ovsdb_atom_destroy(value, type->value.type);
1334 /* Initializes 'datum' as a datum of the given 'type', parsing its contents
1335 * from 's'. The format of 's' is a series of space or comma separated atoms
1336 * or, for a map, '='-delimited pairs of atoms. Each atom must in a format
1337 * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed
1338 * in "[]" or a map in "{}"; for an empty set or map these punctuators are
1341 * Optionally, a symbol table may be supplied as 'symtab'. It is passed to
1342 * ovsdb_atom_to_string(). */
1344 ovsdb_datum_from_string(struct ovsdb_datum *datum,
1345 const struct ovsdb_type *type, const char *s,
1346 struct ovsdb_symbol_table *symtab)
1348 bool is_map = ovsdb_type_is_map(type);
1349 struct ovsdb_error *dberror;
1354 ovsdb_datum_init_empty(datum);
1356 /* Swallow a leading delimiter if there is one. */
1358 if (*p == (is_map ? '{' : '[')) {
1359 end_delim = is_map ? '}' : ']';
1360 p = skip_spaces(p + 1);
1363 return xstrdup("use \"{}\" to specify the empty map");
1365 return xstrdup("use \"[]\" to specify the empty set");
1371 while (*p && *p != end_delim) {
1372 union ovsdb_atom key, value;
1374 if (ovsdb_token_is_delim(*p)) {
1375 char *type_str = ovsdb_type_to_english(type);
1376 error = xasprintf("%s: unexpected \"%c\" parsing %s",
1383 error = parse_key_value(&p, type, &key, &value, symtab);
1387 ovsdb_datum_add_unsafe(datum, &key, &value, type);
1388 free_key_value(type, &key, &value);
1390 /* Skip optional white space and comma. */
1393 p = skip_spaces(p + 1);
1397 if (*p != end_delim) {
1398 error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim);
1402 p = skip_spaces(p + 1);
1404 error = xasprintf("%s: trailing garbage after \"%c\"",
1410 if (datum->n < type->n_min) {
1411 error = xasprintf("%s: %u %s specified but the minimum number is %u",
1412 s, datum->n, is_map ? "pair(s)" : "value(s)",
1415 } else if (datum->n > type->n_max) {
1416 error = xasprintf("%s: %u %s specified but the maximum number is %u",
1417 s, datum->n, is_map ? "pair(s)" : "value(s)",
1422 dberror = ovsdb_datum_sort(datum, type->key.type);
1424 ovsdb_error_destroy(dberror);
1425 if (ovsdb_type_is_map(type)) {
1426 error = xasprintf("%s: map contains duplicate key", s);
1428 error = xasprintf("%s: set contains duplicate value", s);
1436 ovsdb_datum_destroy(datum, type);
1437 ovsdb_datum_init_empty(datum);
1441 /* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable
1442 * to ovsdb_datum_from_string(). */
1444 ovsdb_datum_to_string(const struct ovsdb_datum *datum,
1445 const struct ovsdb_type *type, struct ds *out)
1447 bool is_map = ovsdb_type_is_map(type);
1450 if (type->n_max > 1 || !datum->n) {
1451 ds_put_char(out, is_map ? '{' : '[');
1453 for (i = 0; i < datum->n; i++) {
1455 ds_put_cstr(out, ", ");
1458 ovsdb_atom_to_string(&datum->keys[i], type->key.type, out);
1460 ds_put_char(out, '=');
1461 ovsdb_atom_to_string(&datum->values[i], type->value.type, out);
1464 if (type->n_max > 1 || !datum->n) {
1465 ds_put_char(out, is_map ? '}' : ']');
1469 /* Appends to 'out' the 'datum' (with the given 'type') in a bare string format
1470 * that cannot be parsed uniformly back into a datum but is easier for shell
1471 * scripts, etc., to deal with. */
1473 ovsdb_datum_to_bare(const struct ovsdb_datum *datum,
1474 const struct ovsdb_type *type, struct ds *out)
1476 bool is_map = ovsdb_type_is_map(type);
1479 for (i = 0; i < datum->n; i++) {
1481 ds_put_cstr(out, " ");
1484 ovsdb_atom_to_bare(&datum->keys[i], type->key.type, out);
1486 ds_put_char(out, '=');
1487 ovsdb_atom_to_bare(&datum->values[i], type->value.type, out);
1492 /* Initializes 'datum' as a string-to-string map whose contents are taken from
1493 * 'sh'. Destroys 'sh'. */
1495 ovsdb_datum_from_shash(struct ovsdb_datum *datum, struct shash *sh)
1497 struct shash_node *node, *next;
1500 datum->n = shash_count(sh);
1501 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
1502 datum->values = xmalloc(datum->n * sizeof *datum->values);
1505 SHASH_FOR_EACH_SAFE (node, next, sh) {
1506 datum->keys[i].string = node->name;
1507 datum->values[i].string = node->data;
1508 shash_steal(sh, node);
1511 assert(i == datum->n);
1514 ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
1518 hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms,
1519 unsigned int n, uint32_t basis)
1521 if (type != OVSDB_TYPE_VOID) {
1524 for (i = 0; i < n; i++) {
1525 basis = ovsdb_atom_hash(&atoms[i], type, basis);
1532 ovsdb_datum_hash(const struct ovsdb_datum *datum,
1533 const struct ovsdb_type *type, uint32_t basis)
1535 basis = hash_atoms(type->key.type, datum->keys, datum->n, basis);
1536 basis ^= (type->key.type << 24) | (type->value.type << 16) | datum->n;
1537 basis = hash_atoms(type->value.type, datum->values, datum->n, basis);
1542 atom_arrays_compare_3way(const union ovsdb_atom *a,
1543 const union ovsdb_atom *b,
1544 enum ovsdb_atomic_type type,
1549 for (i = 0; i < n; i++) {
1550 int cmp = ovsdb_atom_compare_3way(&a[i], &b[i], type);
1560 ovsdb_datum_equals(const struct ovsdb_datum *a,
1561 const struct ovsdb_datum *b,
1562 const struct ovsdb_type *type)
1564 return !ovsdb_datum_compare_3way(a, b, type);
1568 ovsdb_datum_compare_3way(const struct ovsdb_datum *a,
1569 const struct ovsdb_datum *b,
1570 const struct ovsdb_type *type)
1575 return a->n < b->n ? -1 : 1;
1578 cmp = atom_arrays_compare_3way(a->keys, b->keys, type->key.type, a->n);
1583 return (type->value.type == OVSDB_TYPE_VOID ? 0
1584 : atom_arrays_compare_3way(a->values, b->values, type->value.type,
1588 /* If 'key' is one of the keys in 'datum', returns its index within 'datum',
1589 * otherwise UINT_MAX. 'key.type' must be the type of the atoms stored in the
1590 * 'keys' array in 'datum'.
1593 ovsdb_datum_find_key(const struct ovsdb_datum *datum,
1594 const union ovsdb_atom *key,
1595 enum ovsdb_atomic_type key_type)
1597 unsigned int low = 0;
1598 unsigned int high = datum->n;
1599 while (low < high) {
1600 unsigned int idx = (low + high) / 2;
1601 int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type);
1604 } else if (cmp > 0) {
1613 /* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its
1614 * index within 'datum', otherwise UINT_MAX. 'key.type' must be the type of
1615 * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the
1616 * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys.
1619 ovsdb_datum_find_key_value(const struct ovsdb_datum *datum,
1620 const union ovsdb_atom *key,
1621 enum ovsdb_atomic_type key_type,
1622 const union ovsdb_atom *value,
1623 enum ovsdb_atomic_type value_type)
1625 unsigned int idx = ovsdb_datum_find_key(datum, key, key_type);
1627 && value_type != OVSDB_TYPE_VOID
1628 && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) {
1634 /* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise
1635 * UINT_MAX. 'type' must be the type of 'a' and 'b', except that
1636 * type->value.type may be set to OVSDB_TYPE_VOID to compare keys but not
1639 ovsdb_datum_find(const struct ovsdb_datum *a, int i,
1640 const struct ovsdb_datum *b,
1641 const struct ovsdb_type *type)
1643 return ovsdb_datum_find_key_value(b,
1644 &a->keys[i], type->key.type,
1645 a->values ? &a->values[i] : NULL,
1649 /* Returns true if every element in 'a' is also in 'b', false otherwise. */
1651 ovsdb_datum_includes_all(const struct ovsdb_datum *a,
1652 const struct ovsdb_datum *b,
1653 const struct ovsdb_type *type)
1657 for (i = 0; i < a->n; i++) {
1658 if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) {
1665 /* Returns true if no element in 'a' is also in 'b', false otherwise. */
1667 ovsdb_datum_excludes_all(const struct ovsdb_datum *a,
1668 const struct ovsdb_datum *b,
1669 const struct ovsdb_type *type)
1673 for (i = 0; i < a->n; i++) {
1674 if (ovsdb_datum_find(a, i, b, type) != UINT_MAX) {
1682 ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type,
1683 unsigned int capacity)
1685 a->keys = xrealloc(a->keys, capacity * sizeof *a->keys);
1686 if (type->value.type != OVSDB_TYPE_VOID) {
1687 a->values = xrealloc(a->values, capacity * sizeof *a->values);
1691 /* Removes the element with index 'idx' from 'datum', which has type 'type'.
1692 * If 'idx' is not the last element in 'datum', then the removed element is
1693 * replaced by the (former) last element.
1695 * This function does not maintain ovsdb_datum invariants. Use
1696 * ovsdb_datum_sort() to check and restore these invariants. */
1698 ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx,
1699 const struct ovsdb_type *type)
1701 ovsdb_atom_destroy(&datum->keys[idx], type->key.type);
1702 datum->keys[idx] = datum->keys[datum->n - 1];
1703 if (type->value.type != OVSDB_TYPE_VOID) {
1704 ovsdb_atom_destroy(&datum->values[idx], type->value.type);
1705 datum->values[idx] = datum->values[datum->n - 1];
1710 /* Adds the element with the given 'key' and 'value' to 'datum', which must
1711 * have the specified 'type'.
1713 * This function always allocates memory, so it is not an efficient way to add
1714 * a number of elements to a datum.
1716 * This function does not maintain ovsdb_datum invariants. Use
1717 * ovsdb_datum_sort() to check and restore these invariants. (But a datum with
1718 * 0 or 1 elements cannot violate the invariants anyhow.) */
1720 ovsdb_datum_add_unsafe(struct ovsdb_datum *datum,
1721 const union ovsdb_atom *key,
1722 const union ovsdb_atom *value,
1723 const struct ovsdb_type *type)
1725 size_t idx = datum->n++;
1726 datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys);
1727 ovsdb_atom_clone(&datum->keys[idx], key, type->key.type);
1728 if (type->value.type != OVSDB_TYPE_VOID) {
1729 datum->values = xrealloc(datum->values,
1730 datum->n * sizeof *datum->values);
1731 ovsdb_atom_clone(&datum->values[idx], value, type->value.type);
1736 ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b,
1737 const struct ovsdb_type *type, bool replace)
1743 for (bi = 0; bi < b->n; bi++) {
1746 ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key.type);
1747 if (ai == UINT_MAX) {
1749 ovsdb_datum_reallocate(a, type, a->n + (b->n - bi));
1751 ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key.type);
1752 if (type->value.type != OVSDB_TYPE_VOID) {
1753 ovsdb_atom_clone(&a->values[n], &b->values[bi],
1757 } else if (replace && type->value.type != OVSDB_TYPE_VOID) {
1758 ovsdb_atom_destroy(&a->values[ai], type->value.type);
1759 ovsdb_atom_clone(&a->values[ai], &b->values[bi],
1764 struct ovsdb_error *error;
1766 error = ovsdb_datum_sort(a, type->key.type);
1772 ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type,
1773 const struct ovsdb_datum *b,
1774 const struct ovsdb_type *b_type)
1776 bool changed = false;
1779 assert(a_type->key.type == b_type->key.type);
1780 assert(a_type->value.type == b_type->value.type
1781 || b_type->value.type == OVSDB_TYPE_VOID);
1783 /* XXX The big-O of this could easily be improved. */
1784 for (i = 0; i < a->n; ) {
1785 unsigned int idx = ovsdb_datum_find(a, i, b, b_type);
1786 if (idx != UINT_MAX) {
1788 ovsdb_datum_remove_unsafe(a, i, a_type);
1794 ovsdb_datum_sort_assert(a, a_type->key.type);
1798 struct ovsdb_symbol_table *
1799 ovsdb_symbol_table_create(void)
1801 struct ovsdb_symbol_table *symtab = xmalloc(sizeof *symtab);
1802 shash_init(&symtab->sh);
1807 ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *symtab)
1810 shash_destroy_free_data(&symtab->sh);
1815 struct ovsdb_symbol *
1816 ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab,
1819 return shash_find_data(&symtab->sh, name);
1822 struct ovsdb_symbol *
1823 ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
1824 const struct uuid *uuid, bool created)
1826 struct ovsdb_symbol *symbol;
1828 assert(!ovsdb_symbol_table_get(symtab, name));
1829 symbol = xmalloc(sizeof *symbol);
1830 symbol->uuid = *uuid;
1831 symbol->created = created;
1832 shash_add(&symtab->sh, name, symbol);
1836 struct ovsdb_symbol *
1837 ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab,
1840 struct ovsdb_symbol *symbol;
1842 symbol = ovsdb_symbol_table_get(symtab, name);
1846 uuid_generate(&uuid);
1847 symbol = ovsdb_symbol_table_put(symtab, name, &uuid, false);
1852 /* Extracts a token from the beginning of 's' and returns a pointer just after
1853 * the token. Stores the token itself into '*outp', which the caller is
1854 * responsible for freeing (with free()).
1856 * If 's[0]' is a delimiter, the returned token is the empty string.
1858 * A token extends from 's' to the first delimiter, as defined by
1859 * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be
1860 * escaped with a backslash, in which case the backslash does not appear in the
1861 * output. Double quotes also cause delimiters to be ignored, but the double
1862 * quotes are retained in the output. (Backslashes inside double quotes are
1863 * not removed, either.)
1866 ovsdb_token_parse(const char **s, char **outp)
1875 for (p = *s; *p != '\0'; ) {
1879 ds_put_char(&out, '\\');
1882 error = xasprintf("%s: backslash at end of argument", *s);
1885 ds_put_char(&out, *p++);
1886 } else if (!in_quotes && ovsdb_token_is_delim(c)) {
1890 ds_put_char(&out, c);
1892 in_quotes = !in_quotes;
1897 error = xasprintf("%s: quoted string extends past end of argument",
1901 *outp = ds_cstr(&out);
1911 /* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */
1913 ovsdb_token_is_delim(unsigned char c)
1915 return strchr(":=, []{}!<>", c) != NULL;