]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ovsdb-data.c
ovsdb-idl: Fix memory leak writing synthetic rows in ovsdb_idl_txn_write().
[mirror_ovs.git] / lib / ovsdb-data.c
CommitLineData
c6a41252 1/* Copyright (c) 2009, 2010, 2011 Nicira Networks
f85f8ebb
BP
2 *
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <config.h>
17
18#include "ovsdb-data.h"
19
20#include <assert.h>
0194f33a
BP
21#include <ctype.h>
22#include <float.h>
23#include <inttypes.h>
e9f8f936 24#include <limits.h>
f85f8ebb 25
0194f33a 26#include "dynamic-string.h"
f85f8ebb
BP
27#include "hash.h"
28#include "ovsdb-error.h"
d198c4be 29#include "ovsdb-parser.h"
f85f8ebb
BP
30#include "json.h"
31#include "shash.h"
32#include "sort.h"
bd76d25d 33#include "unicode.h"
f85f8ebb
BP
34
35static struct json *
36wrap_json(const char *name, struct json *wrapped)
37{
38 return json_array_create_2(json_string_create(name), wrapped);
39}
40
5413de95
BP
41/* Initializes 'atom' with the default value of the given 'type'.
42 *
43 * The default value for an atom is as defined in ovsdb/SPECS:
44 *
45 * - "integer" or "real": 0
46 *
47 * - "boolean": false
48 *
49 * - "string": "" (the empty string)
50 *
51 * - "uuid": 00000000-0000-0000-0000-000000000000
52 *
53 * The caller must eventually arrange for 'atom' to be destroyed (with
54 * ovsdb_atom_destroy()). */
f85f8ebb
BP
55void
56ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
57{
58 switch (type) {
59 case OVSDB_TYPE_VOID:
60 NOT_REACHED();
61
62 case OVSDB_TYPE_INTEGER:
63 atom->integer = 0;
64 break;
65
66 case OVSDB_TYPE_REAL:
67 atom->real = 0.0;
68 break;
69
70 case OVSDB_TYPE_BOOLEAN:
71 atom->boolean = false;
72 break;
73
74 case OVSDB_TYPE_STRING:
75 atom->string = xmemdup("", 1);
76 break;
77
78 case OVSDB_TYPE_UUID:
79 uuid_zero(&atom->uuid);
80 break;
81
82 case OVSDB_N_TYPES:
83 default:
84 NOT_REACHED();
85 }
86}
87
958ac03a
BP
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.
90 *
91 * See ovsdb_atom_init_default() for an explanation of the default value of an
92 * atom. */
93const union ovsdb_atom *
94ovsdb_atom_default(enum ovsdb_atomic_type type)
95{
96 static union ovsdb_atom default_atoms[OVSDB_N_TYPES];
97 static bool inited;
98
99 if (!inited) {
100 int i;
101
102 for (i = 0; i < OVSDB_N_TYPES; i++) {
103 if (i != OVSDB_TYPE_VOID) {
104 ovsdb_atom_init_default(&default_atoms[i], i);
105 }
106 }
107 inited = true;
108 }
109
110 assert(ovsdb_atomic_type_is_valid(type));
111 return &default_atoms[type];
112}
5413de95
BP
113
114/* Returns true if 'atom', which must have the given 'type', has the default
115 * value for that type.
116 *
117 * See ovsdb_atom_init_default() for an explanation of the default value of an
118 * atom. */
c532bf9d
BP
119bool
120ovsdb_atom_is_default(const union ovsdb_atom *atom,
121 enum ovsdb_atomic_type type)
122{
123 switch (type) {
124 case OVSDB_TYPE_VOID:
125 NOT_REACHED();
126
127 case OVSDB_TYPE_INTEGER:
128 return atom->integer == 0;
129
130 case OVSDB_TYPE_REAL:
131 return atom->real == 0.0;
132
133 case OVSDB_TYPE_BOOLEAN:
134 return atom->boolean == false;
135
136 case OVSDB_TYPE_STRING:
137 return atom->string[0] == '\0';
138
139 case OVSDB_TYPE_UUID:
140 return uuid_is_zero(&atom->uuid);
141
142 case OVSDB_N_TYPES:
143 default:
144 NOT_REACHED();
145 }
146}
147
5413de95
BP
148/* Initializes 'new' as a copy of 'old', with the given 'type'.
149 *
150 * The caller must eventually arrange for 'new' to be destroyed (with
151 * ovsdb_atom_destroy()). */
f85f8ebb
BP
152void
153ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old,
154 enum ovsdb_atomic_type type)
155{
156 switch (type) {
157 case OVSDB_TYPE_VOID:
158 NOT_REACHED();
159
160 case OVSDB_TYPE_INTEGER:
161 new->integer = old->integer;
162 break;
163
164 case OVSDB_TYPE_REAL:
165 new->real = old->real;
166 break;
167
168 case OVSDB_TYPE_BOOLEAN:
169 new->boolean = old->boolean;
170 break;
171
172 case OVSDB_TYPE_STRING:
173 new->string = xstrdup(old->string);
174 break;
175
176 case OVSDB_TYPE_UUID:
177 new->uuid = old->uuid;
178 break;
179
180 case OVSDB_N_TYPES:
181 default:
182 NOT_REACHED();
183 }
184}
185
5413de95 186/* Swaps the contents of 'a' and 'b', which need not have the same type. */
f85f8ebb
BP
187void
188ovsdb_atom_swap(union ovsdb_atom *a, union ovsdb_atom *b)
189{
190 union ovsdb_atom tmp = *a;
191 *a = *b;
192 *b = tmp;
193}
194
5413de95
BP
195/* Returns a hash value for 'atom', which has the specified 'type', folding
196 * 'basis' into the calculation. */
f85f8ebb
BP
197uint32_t
198ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
199 uint32_t basis)
200{
201 switch (type) {
202 case OVSDB_TYPE_VOID:
203 NOT_REACHED();
204
205 case OVSDB_TYPE_INTEGER:
206 return hash_int(atom->integer, basis);
207
208 case OVSDB_TYPE_REAL:
209 return hash_double(atom->real, basis);
210
211 case OVSDB_TYPE_BOOLEAN:
212 return hash_boolean(atom->boolean, basis);
213
214 case OVSDB_TYPE_STRING:
215 return hash_string(atom->string, basis);
216
217 case OVSDB_TYPE_UUID:
218 return hash_int(uuid_hash(&atom->uuid), basis);
219
220 case OVSDB_N_TYPES:
221 default:
222 NOT_REACHED();
223 }
224}
225
5413de95
BP
226/* Compares 'a' and 'b', which both have type 'type', and returns a
227 * strcmp()-like result. */
f85f8ebb
BP
228int
229ovsdb_atom_compare_3way(const union ovsdb_atom *a,
230 const union ovsdb_atom *b,
231 enum ovsdb_atomic_type type)
232{
233 switch (type) {
234 case OVSDB_TYPE_VOID:
235 NOT_REACHED();
236
237 case OVSDB_TYPE_INTEGER:
238 return a->integer < b->integer ? -1 : a->integer > b->integer;
239
240 case OVSDB_TYPE_REAL:
241 return a->real < b->real ? -1 : a->real > b->real;
242
243 case OVSDB_TYPE_BOOLEAN:
244 return a->boolean - b->boolean;
245
246 case OVSDB_TYPE_STRING:
247 return strcmp(a->string, b->string);
248
249 case OVSDB_TYPE_UUID:
250 return uuid_compare_3way(&a->uuid, &b->uuid);
251
252 case OVSDB_N_TYPES:
253 default:
254 NOT_REACHED();
255 }
256}
257
258static struct ovsdb_error *
259unwrap_json(const struct json *json, const char *name,
260 enum json_type value_type, const struct json **value)
261{
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)
267 {
829d41bd 268 *value = NULL;
f85f8ebb
BP
269 return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name,
270 json_type_to_string(value_type));
271 }
272 *value = json->u.array.elems[1];
273 return NULL;
274}
275
276static struct ovsdb_error *
277parse_json_pair(const struct json *json,
278 const struct json **elem0, const struct json **elem1)
279{
280 if (json->type != JSON_ARRAY || json->u.array.n != 2) {
281 return ovsdb_syntax_error(json, NULL, "expected 2-element array");
282 }
283 *elem0 = json->u.array.elems[0];
284 *elem1 = json->u.array.elems[1];
285 return NULL;
286}
287
c5f341ab
BP
288static void
289ovsdb_symbol_referenced(struct ovsdb_symbol *symbol,
290 const struct ovsdb_base_type *base)
291{
292 assert(base->type == OVSDB_TYPE_UUID);
293
294 if (base->u.uuid.refTableName) {
295 switch (base->u.uuid.refType) {
296 case OVSDB_REF_STRONG:
297 symbol->strong_ref = true;
298 break;
299 case OVSDB_REF_WEAK:
300 symbol->weak_ref = true;
301 break;
302 }
303 }
304}
305
fbf925e4 306static struct ovsdb_error * WARN_UNUSED_RESULT
f85f8ebb 307ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
c5f341ab
BP
308 struct ovsdb_symbol_table *symtab,
309 const struct ovsdb_base_type *base)
f85f8ebb
BP
310{
311 struct ovsdb_error *error0;
312 const struct json *value;
313
314 error0 = unwrap_json(json, "uuid", JSON_STRING, &value);
315 if (!error0) {
316 const char *uuid_string = json_string(value);
317 if (!uuid_from_string(uuid, uuid_string)) {
318 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
319 uuid_string);
320 }
321 } else if (symtab) {
322 struct ovsdb_error *error1;
323
324 error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
325 if (!error1) {
c5f341ab 326 struct ovsdb_symbol *symbol;
f85f8ebb
BP
327
328 ovsdb_error_destroy(error0);
d198c4be
BP
329 if (!ovsdb_parser_is_id(json_string(value))) {
330 return ovsdb_syntax_error(json, NULL, "named-uuid string is "
331 "not a valid <id>");
332 }
c5f341ab
BP
333
334 symbol = ovsdb_symbol_table_insert(symtab, json_string(value));
335 *uuid = symbol->uuid;
336 ovsdb_symbol_referenced(symbol, base);
fbf925e4 337 return NULL;
f85f8ebb
BP
338 }
339 ovsdb_error_destroy(error1);
340 }
341
342 return error0;
343}
344
bd76d25d 345static struct ovsdb_error * WARN_UNUSED_RESULT
c5f341ab
BP
346ovsdb_atom_from_json__(union ovsdb_atom *atom,
347 const struct ovsdb_base_type *base,
bd76d25d 348 const struct json *json,
fbf925e4 349 struct ovsdb_symbol_table *symtab)
f85f8ebb 350{
c5f341ab
BP
351 enum ovsdb_atomic_type type = base->type;
352
f85f8ebb
BP
353 switch (type) {
354 case OVSDB_TYPE_VOID:
355 NOT_REACHED();
356
357 case OVSDB_TYPE_INTEGER:
358 if (json->type == JSON_INTEGER) {
359 atom->integer = json->u.integer;
360 return NULL;
361 }
362 break;
363
364 case OVSDB_TYPE_REAL:
365 if (json->type == JSON_INTEGER) {
366 atom->real = json->u.integer;
367 return NULL;
368 } else if (json->type == JSON_REAL) {
369 atom->real = json->u.real;
370 return NULL;
371 }
372 break;
373
374 case OVSDB_TYPE_BOOLEAN:
375 if (json->type == JSON_TRUE) {
376 atom->boolean = true;
377 return NULL;
378 } else if (json->type == JSON_FALSE) {
379 atom->boolean = false;
380 return NULL;
381 }
382 break;
383
384 case OVSDB_TYPE_STRING:
385 if (json->type == JSON_STRING) {
386 atom->string = xstrdup(json->u.string);
387 return NULL;
388 }
389 break;
390
391 case OVSDB_TYPE_UUID:
c5f341ab 392 return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab, base);
f85f8ebb
BP
393
394 case OVSDB_N_TYPES:
395 default:
396 NOT_REACHED();
397 }
398
399 return ovsdb_syntax_error(json, NULL, "expected %s",
400 ovsdb_atomic_type_to_string(type));
401}
402
5413de95
BP
403/* Parses 'json' as an atom of the type described by 'base'. If successful,
404 * returns NULL and initializes 'atom' with the parsed atom. On failure,
405 * returns an error and the contents of 'atom' are indeterminate. The caller
406 * is responsible for freeing the error or the atom that is returned.
407 *
958ac03a
BP
408 * Violations of constraints expressed by 'base' are treated as errors.
409 *
5413de95 410 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
958ac03a 411 * ovsdb/SPECS for information about this, and for the syntax that this
c5f341ab
BP
412 * function accepts. If 'base' is a reference and a symbol is parsed, then the
413 * symbol's 'strong_ref' or 'weak_ref' member is set to true, as
414 * appropriate. */
bd76d25d
BP
415struct ovsdb_error *
416ovsdb_atom_from_json(union ovsdb_atom *atom,
417 const struct ovsdb_base_type *base,
418 const struct json *json,
fbf925e4 419 struct ovsdb_symbol_table *symtab)
bd76d25d
BP
420{
421 struct ovsdb_error *error;
422
c5f341ab 423 error = ovsdb_atom_from_json__(atom, base, json, symtab);
bd76d25d
BP
424 if (error) {
425 return error;
426 }
427
428 error = ovsdb_atom_check_constraints(atom, base);
429 if (error) {
430 ovsdb_atom_destroy(atom, base->type);
431 }
432 return error;
433}
434
5413de95 435/* Converts 'atom', of the specified 'type', to JSON format, and returns the
958ac03a
BP
436 * JSON. The caller is responsible for freeing the returned JSON.
437 *
438 * Refer to ovsdb/SPECS for the format of the JSON that this function
439 * produces. */
f85f8ebb
BP
440struct json *
441ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
442{
443 switch (type) {
444 case OVSDB_TYPE_VOID:
445 NOT_REACHED();
446
447 case OVSDB_TYPE_INTEGER:
448 return json_integer_create(atom->integer);
449
450 case OVSDB_TYPE_REAL:
451 return json_real_create(atom->real);
452
453 case OVSDB_TYPE_BOOLEAN:
454 return json_boolean_create(atom->boolean);
455
456 case OVSDB_TYPE_STRING:
457 return json_string_create(atom->string);
458
459 case OVSDB_TYPE_UUID:
460 return wrap_json("uuid", json_string_create_nocopy(
461 xasprintf(UUID_FMT, UUID_ARGS(&atom->uuid))));
462
463 case OVSDB_N_TYPES:
464 default:
465 NOT_REACHED();
466 }
467}
0194f33a 468
bd76d25d 469static char *
c5f341ab
BP
470ovsdb_atom_from_string__(union ovsdb_atom *atom,
471 const struct ovsdb_base_type *base, const char *s,
472 struct ovsdb_symbol_table *symtab)
0194f33a 473{
c5f341ab
BP
474 enum ovsdb_atomic_type type = base->type;
475
0194f33a
BP
476 switch (type) {
477 case OVSDB_TYPE_VOID:
478 NOT_REACHED();
479
480 case OVSDB_TYPE_INTEGER: {
481 long long int integer;
482 if (!str_to_llong(s, 10, &integer)) {
1bc6ff29 483 return xasprintf("\"%s\" is not a valid integer", s);
0194f33a
BP
484 }
485 atom->integer = integer;
486 }
487 break;
488
489 case OVSDB_TYPE_REAL:
490 if (!str_to_double(s, &atom->real)) {
1bc6ff29 491 return xasprintf("\"%s\" is not a valid real number", s);
0194f33a 492 }
54a687fd
BP
493 /* Our JSON input routines map negative zero to zero, so do that here
494 * too for consistency. */
495 if (atom->real == 0.0) {
496 atom->real = 0.0;
497 }
0194f33a
BP
498 break;
499
500 case OVSDB_TYPE_BOOLEAN:
501 if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on")
502 || !strcmp(s, "1")) {
503 atom->boolean = true;
504 } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off")
505 || !strcmp(s, "0")) {
506 atom->boolean = false;
507 } else {
1bc6ff29
BP
508 return xasprintf("\"%s\" is not a valid boolean "
509 "(use \"true\" or \"false\")", s);
0194f33a
BP
510 }
511 break;
512
513 case OVSDB_TYPE_STRING:
514 if (*s == '\0') {
1bc6ff29
BP
515 return xstrdup("An empty string is not valid as input; "
516 "use \"\" to represent the empty string");
0194f33a
BP
517 } else if (*s == '"') {
518 size_t s_len = strlen(s);
519
520 if (s_len < 2 || s[s_len - 1] != '"') {
1bc6ff29
BP
521 return xasprintf("%s: missing quote at end of "
522 "quoted string", s);
0194f33a
BP
523 } else if (!json_string_unescape(s + 1, s_len - 2,
524 &atom->string)) {
1bc6ff29
BP
525 char *error = xasprintf("%s: %s", s, atom->string);
526 free(atom->string);
527 return error;
0194f33a
BP
528 }
529 } else {
530 atom->string = xstrdup(s);
531 }
532 break;
533
534 case OVSDB_TYPE_UUID:
ce5a3e38 535 if (*s == '@') {
c5f341ab
BP
536 struct ovsdb_symbol *symbol = ovsdb_symbol_table_insert(symtab, s);
537 atom->uuid = symbol->uuid;
538 ovsdb_symbol_referenced(symbol, base);
ce5a3e38 539 } else if (!uuid_from_string(&atom->uuid, s)) {
1bc6ff29 540 return xasprintf("\"%s\" is not a valid UUID", s);
0194f33a
BP
541 }
542 break;
543
544 case OVSDB_N_TYPES:
545 default:
546 NOT_REACHED();
547 }
1bc6ff29
BP
548
549 return NULL;
0194f33a
BP
550}
551
bd76d25d
BP
552/* Initializes 'atom' to a value of type 'base' parsed from 's', which takes
553 * one of the following forms:
554 *
555 * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign.
556 *
557 * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by
558 * strtod().
559 *
560 * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false",
561 * "no", "off", or "0" for false.
562 *
563 * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
564 * an arbitrary string.
565 *
ce5a3e38
BP
566 * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format. If 'symtab' is nonnull,
567 * then an identifier beginning with '@' is also acceptable. If the
568 * named identifier is already in 'symtab', then the associated UUID is
569 * used; otherwise, a new, random UUID is used and added to the symbol
c5f341ab
BP
570 * table. If 'base' is a reference and a symbol is parsed, then the
571 * symbol's 'strong_ref' or 'weak_ref' member is set to true, as
572 * appropriate.
bd76d25d
BP
573 *
574 * Returns a null pointer if successful, otherwise an error message describing
5413de95
BP
575 * the problem. On failure, the contents of 'atom' are indeterminate. The
576 * caller is responsible for freeing the atom or the error.
bd76d25d
BP
577 */
578char *
579ovsdb_atom_from_string(union ovsdb_atom *atom,
ce5a3e38
BP
580 const struct ovsdb_base_type *base, const char *s,
581 struct ovsdb_symbol_table *symtab)
bd76d25d
BP
582{
583 struct ovsdb_error *error;
584 char *msg;
585
c5f341ab 586 msg = ovsdb_atom_from_string__(atom, base, s, symtab);
bd76d25d
BP
587 if (msg) {
588 return msg;
589 }
590
591 error = ovsdb_atom_check_constraints(atom, base);
592 if (error) {
593 msg = ovsdb_error_to_string(error);
594 ovsdb_error_destroy(error);
595 }
596 return msg;
597}
598
0194f33a
BP
599static bool
600string_needs_quotes(const char *s)
601{
602 const char *p = s;
603 unsigned char c;
604
605 c = *p++;
606 if (!isalpha(c) && c != '_') {
607 return true;
608 }
609
610 while ((c = *p++) != '\0') {
611 if (!isalpha(c) && c != '_' && c != '-' && c != '.') {
612 return true;
613 }
614 }
615
616 if (!strcmp(s, "true") || !strcmp(s, "false")) {
617 return true;
618 }
619
620 return false;
621}
622
623/* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable
624 * to ovsdb_atom_from_string(). */
625void
626ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
627 struct ds *out)
628{
629 switch (type) {
630 case OVSDB_TYPE_VOID:
631 NOT_REACHED();
632
633 case OVSDB_TYPE_INTEGER:
634 ds_put_format(out, "%"PRId64, atom->integer);
635 break;
636
637 case OVSDB_TYPE_REAL:
638 ds_put_format(out, "%.*g", DBL_DIG, atom->real);
639 break;
640
641 case OVSDB_TYPE_BOOLEAN:
642 ds_put_cstr(out, atom->boolean ? "true" : "false");
643 break;
644
645 case OVSDB_TYPE_STRING:
646 if (string_needs_quotes(atom->string)) {
647 struct json json;
648
649 json.type = JSON_STRING;
650 json.u.string = atom->string;
651 json_to_ds(&json, 0, out);
652 } else {
653 ds_put_cstr(out, atom->string);
654 }
655 break;
656
657 case OVSDB_TYPE_UUID:
658 ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid));
659 break;
660
661 case OVSDB_N_TYPES:
662 default:
663 NOT_REACHED();
664 }
665}
bd76d25d 666
c6a41252
BP
667/* Appends 'atom' (which has the given 'type') to 'out', in a bare string
668 * format that cannot be parsed uniformly back into a datum but is easier for
669 * shell scripts, etc., to deal with. */
670void
671ovsdb_atom_to_bare(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
672 struct ds *out)
673{
674 if (type == OVSDB_TYPE_STRING) {
675 ds_put_cstr(out, atom->string);
676 } else {
677 ovsdb_atom_to_string(atom, type, out);
678 }
679}
680
bd76d25d
BP
681static struct ovsdb_error *
682check_string_constraints(const char *s,
683 const struct ovsdb_string_constraints *c)
684{
685 size_t n_chars;
686 char *msg;
687
688 msg = utf8_validate(s, &n_chars);
689 if (msg) {
690 struct ovsdb_error *error;
691
692 error = ovsdb_error("constraint violation",
be44585c 693 "not a valid UTF-8 string: %s", msg);
bd76d25d
BP
694 free(msg);
695 return error;
696 }
697
698 if (n_chars < c->minLen) {
699 return ovsdb_error(
700 "constraint violation",
701 "\"%s\" length %zu is less than minimum allowed "
702 "length %u", s, n_chars, c->minLen);
703 } else if (n_chars > c->maxLen) {
704 return ovsdb_error(
705 "constraint violation",
706 "\"%s\" length %zu is greater than maximum allowed "
707 "length %u", s, n_chars, c->maxLen);
708 }
709
bd76d25d
BP
710 return NULL;
711}
712
713/* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
714 * (base->type must specify 'atom''s type.) Returns a null pointer if the
0d0f05b9
BP
715 * constraints are met, otherwise an error that explains the violation.
716 *
717 * Checking UUID constraints is deferred to transaction commit time, so this
718 * function does nothing for UUID constraints. */
bd76d25d
BP
719struct ovsdb_error *
720ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
721 const struct ovsdb_base_type *base)
722{
bfc96d9b
BP
723 if (base->enum_
724 && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) {
725 struct ovsdb_error *error;
726 struct ds actual = DS_EMPTY_INITIALIZER;
727 struct ds valid = DS_EMPTY_INITIALIZER;
728
729 ovsdb_atom_to_string(atom, base->type, &actual);
730 ovsdb_datum_to_string(base->enum_,
731 ovsdb_base_type_get_enum_type(base->type),
732 &valid);
733 error = ovsdb_error("constraint violation",
734 "%s is not one of the allowed values (%s)",
735 ds_cstr(&actual), ds_cstr(&valid));
736 ds_destroy(&actual);
737 ds_destroy(&valid);
738
739 return error;
740 }
741
bd76d25d
BP
742 switch (base->type) {
743 case OVSDB_TYPE_VOID:
744 NOT_REACHED();
745
746 case OVSDB_TYPE_INTEGER:
747 if (atom->integer >= base->u.integer.min
748 && atom->integer <= base->u.integer.max) {
749 return NULL;
750 } else if (base->u.integer.min != INT64_MIN) {
751 if (base->u.integer.max != INT64_MAX) {
752 return ovsdb_error("constraint violation",
753 "%"PRId64" is not in the valid range "
754 "%"PRId64" to %"PRId64" (inclusive)",
755 atom->integer,
756 base->u.integer.min, base->u.integer.max);
757 } else {
758 return ovsdb_error("constraint violation",
759 "%"PRId64" is less than minimum allowed "
760 "value %"PRId64,
761 atom->integer, base->u.integer.min);
762 }
763 } else {
764 return ovsdb_error("constraint violation",
765 "%"PRId64" is greater than maximum allowed "
766 "value %"PRId64,
767 atom->integer, base->u.integer.max);
768 }
769 NOT_REACHED();
770
771 case OVSDB_TYPE_REAL:
772 if (atom->real >= base->u.real.min && atom->real <= base->u.real.max) {
773 return NULL;
774 } else if (base->u.real.min != -DBL_MAX) {
775 if (base->u.real.max != DBL_MAX) {
776 return ovsdb_error("constraint violation",
777 "%.*g is not in the valid range "
778 "%.*g to %.*g (inclusive)",
779 DBL_DIG, atom->real,
780 DBL_DIG, base->u.real.min,
781 DBL_DIG, base->u.real.max);
782 } else {
783 return ovsdb_error("constraint violation",
784 "%.*g is less than minimum allowed "
785 "value %.*g",
786 DBL_DIG, atom->real,
787 DBL_DIG, base->u.real.min);
788 }
789 } else {
790 return ovsdb_error("constraint violation",
791 "%.*g is greater than maximum allowed "
792 "value %.*g",
793 DBL_DIG, atom->real,
794 DBL_DIG, base->u.real.max);
795 }
796 NOT_REACHED();
797
798 case OVSDB_TYPE_BOOLEAN:
799 return NULL;
800
801 case OVSDB_TYPE_STRING:
802 return check_string_constraints(atom->string, &base->u.string);
803
804 case OVSDB_TYPE_UUID:
805 return NULL;
806
807 case OVSDB_N_TYPES:
808 default:
809 NOT_REACHED();
810 }
811}
f85f8ebb
BP
812\f
813static union ovsdb_atom *
814alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
815{
816 if (type != OVSDB_TYPE_VOID && n) {
817 union ovsdb_atom *atoms;
818 unsigned int i;
819
820 atoms = xmalloc(n * sizeof *atoms);
821 for (i = 0; i < n; i++) {
822 ovsdb_atom_init_default(&atoms[i], type);
823 }
824 return atoms;
825 } else {
826 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
827 * treated as xmalloc(1). */
828 return NULL;
829 }
830}
831
5413de95
BP
832/* Initializes 'datum' as an empty datum. (An empty datum can be treated as
833 * any type.) */
2f47998b
BP
834void
835ovsdb_datum_init_empty(struct ovsdb_datum *datum)
836{
837 datum->n = 0;
838 datum->keys = NULL;
839 datum->values = NULL;
840}
841
5413de95
BP
842/* Initializes 'datum' as a datum that has the default value for 'type'.
843 *
844 * The default value for a particular type is as defined in ovsdb/SPECS:
845 *
846 * - If n_min is 0, then the default value is the empty set (or map).
847 *
848 * - If n_min is 1, the default value is a single value or a single
849 * key-value pair, whose key and value are the defaults for their
850 * atomic types. (See ovsdb_atom_init_default() for details.)
851 *
852 * - n_min > 1 is invalid. See ovsdb_type_is_valid().
853 */
f85f8ebb
BP
854void
855ovsdb_datum_init_default(struct ovsdb_datum *datum,
856 const struct ovsdb_type *type)
857{
858 datum->n = type->n_min;
bd76d25d
BP
859 datum->keys = alloc_default_atoms(type->key.type, datum->n);
860 datum->values = alloc_default_atoms(type->value.type, datum->n);
f85f8ebb
BP
861}
862
958ac03a
BP
863/* Returns a read-only datum of the given 'type' that has the default value for
864 * 'type'. The caller must not modify or free the returned datum.
865 *
866 * See ovsdb_datum_init_default() for an explanation of the default value of a
867 * datum. */
868const struct ovsdb_datum *
869ovsdb_datum_default(const struct ovsdb_type *type)
870{
871 if (type->n_min == 0) {
872 static const struct ovsdb_datum empty;
873 return &empty;
874 } else if (type->n_min == 1) {
875 static struct ovsdb_datum default_data[OVSDB_N_TYPES][OVSDB_N_TYPES];
876 struct ovsdb_datum *d;
877 int kt = type->key.type;
878 int vt = type->value.type;
879
880 assert(ovsdb_type_is_valid(type));
881
882 d = &default_data[kt][vt];
883 if (!d->n) {
884 d->n = 1;
885 d->keys = (union ovsdb_atom *) ovsdb_atom_default(kt);
886 if (vt != OVSDB_TYPE_VOID) {
887 d->values = (union ovsdb_atom *) ovsdb_atom_default(vt);
888 }
889 }
890 return d;
891 } else {
892 NOT_REACHED();
893 }
894}
895
5413de95
BP
896/* Returns true if 'datum', which must have the given 'type', has the default
897 * value for that type.
898 *
899 * See ovsdb_datum_init_default() for an explanation of the default value of a
900 * datum. */
c532bf9d
BP
901bool
902ovsdb_datum_is_default(const struct ovsdb_datum *datum,
903 const struct ovsdb_type *type)
904{
905 size_t i;
906
907 if (datum->n != type->n_min) {
908 return false;
909 }
910 for (i = 0; i < datum->n; i++) {
bd76d25d 911 if (!ovsdb_atom_is_default(&datum->keys[i], type->key.type)) {
c532bf9d
BP
912 return false;
913 }
bd76d25d
BP
914 if (type->value.type != OVSDB_TYPE_VOID
915 && !ovsdb_atom_is_default(&datum->values[i], type->value.type)) {
c532bf9d
BP
916 return false;
917 }
918 }
919
920 return true;
921}
922
f85f8ebb
BP
923static union ovsdb_atom *
924clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n)
925{
926 if (type != OVSDB_TYPE_VOID && n) {
927 union ovsdb_atom *new;
928 unsigned int i;
929
930 new = xmalloc(n * sizeof *new);
931 for (i = 0; i < n; i++) {
932 ovsdb_atom_clone(&new[i], &old[i], type);
933 }
934 return new;
935 } else {
936 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
937 * treated as xmalloc(1). */
938 return NULL;
939 }
940}
941
5413de95
BP
942/* Initializes 'new' as a copy of 'old', with the given 'type'.
943 *
944 * The caller must eventually arrange for 'new' to be destroyed (with
945 * ovsdb_datum_destroy()). */
f85f8ebb
BP
946void
947ovsdb_datum_clone(struct ovsdb_datum *new, const struct ovsdb_datum *old,
948 const struct ovsdb_type *type)
949{
950 unsigned int n = old->n;
951 new->n = n;
bd76d25d
BP
952 new->keys = clone_atoms(old->keys, type->key.type, n);
953 new->values = clone_atoms(old->values, type->value.type, n);
f85f8ebb
BP
954}
955
956static void
957free_data(enum ovsdb_atomic_type type,
958 union ovsdb_atom *atoms, size_t n_atoms)
959{
960 if (ovsdb_atom_needs_destruction(type)) {
961 unsigned int i;
962 for (i = 0; i < n_atoms; i++) {
963 ovsdb_atom_destroy(&atoms[i], type);
964 }
965 }
966 free(atoms);
967}
968
5413de95
BP
969/* Frees the data owned by 'datum', which must have the given 'type'.
970 *
971 * This does not actually call free(datum). If necessary, the caller must be
972 * responsible for that. */
f85f8ebb
BP
973void
974ovsdb_datum_destroy(struct ovsdb_datum *datum, const struct ovsdb_type *type)
975{
bd76d25d
BP
976 free_data(type->key.type, datum->keys, datum->n);
977 free_data(type->value.type, datum->values, datum->n);
f85f8ebb
BP
978}
979
5413de95 980/* Swaps the contents of 'a' and 'b', which need not have the same type. */
f85f8ebb
BP
981void
982ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b)
983{
984 struct ovsdb_datum tmp = *a;
985 *a = *b;
986 *b = tmp;
987}
988
989struct ovsdb_datum_sort_cbdata {
bfc96d9b 990 enum ovsdb_atomic_type key_type;
2b66469b 991 enum ovsdb_atomic_type value_type;
f85f8ebb
BP
992 struct ovsdb_datum *datum;
993};
994
995static int
996ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_)
997{
998 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
2b66469b 999 int retval;
f85f8ebb 1000
2b66469b
BP
1001 retval = ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
1002 &cbdata->datum->keys[b],
1003 cbdata->key_type);
1004 if (retval || cbdata->value_type == OVSDB_TYPE_VOID) {
1005 return retval;
1006 }
1007
1008 return ovsdb_atom_compare_3way(&cbdata->datum->values[a],
1009 &cbdata->datum->values[b],
1010 cbdata->value_type);
f85f8ebb
BP
1011}
1012
1013static void
1014ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_)
1015{
1016 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
1017
1018 ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
bfc96d9b 1019 if (cbdata->datum->values) {
f85f8ebb
BP
1020 ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
1021 }
1022}
1023
2b66469b
BP
1024static void
1025ovsdb_datum_sort__(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type,
1026 enum ovsdb_atomic_type value_type)
1027{
1028 struct ovsdb_datum_sort_cbdata cbdata;
1029
1030 cbdata.key_type = key_type;
1031 cbdata.value_type = value_type;
1032 cbdata.datum = datum;
1033 sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
1034 &cbdata);
1035}
1036
5413de95
BP
1037/* The keys in an ovsdb_datum must be unique and in sorted order. Most
1038 * functions that modify an ovsdb_datum maintain these invariants. For those
1039 * that don't, this function checks and restores these invariants for 'datum',
1040 * whose keys are of type 'key_type'.
1041 *
1042 * This function returns NULL if successful, otherwise an error message. The
1043 * caller must free the returned error when it is no longer needed. On error,
1044 * 'datum' is sorted but not unique. */
e9f8f936 1045struct ovsdb_error *
bfc96d9b 1046ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type)
f85f8ebb 1047{
2b66469b
BP
1048 size_t i;
1049
f85f8ebb
BP
1050 if (datum->n < 2) {
1051 return NULL;
2b66469b
BP
1052 }
1053
1054 ovsdb_datum_sort__(datum, key_type, OVSDB_TYPE_VOID);
1055
1056 for (i = 0; i < datum->n - 1; i++) {
1057 if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
1058 key_type)) {
1059 if (datum->values) {
1060 return ovsdb_error(NULL, "map contains duplicate key");
1061 } else {
1062 return ovsdb_error(NULL, "set contains duplicate");
f85f8ebb
BP
1063 }
1064 }
f85f8ebb 1065 }
2b66469b 1066 return NULL;
f85f8ebb
BP
1067}
1068
5413de95
BP
1069/* This function is the same as ovsdb_datum_sort(), except that the caller
1070 * knows that 'datum' is unique. The operation therefore "cannot fail", so
1071 * this function assert-fails if it actually does. */
bfc96d9b
BP
1072void
1073ovsdb_datum_sort_assert(struct ovsdb_datum *datum,
1074 enum ovsdb_atomic_type key_type)
1075{
1076 struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type);
1077 if (error) {
1078 NOT_REACHED();
1079 }
1080}
1081
2b66469b
BP
1082/* This is similar to ovsdb_datum_sort(), except that it drops duplicate keys
1083 * instead of reporting an error. In a map type, the smallest value among a
1084 * group of duplicate pairs is retained and the others are dropped.
1085 *
1086 * Returns the number of keys (or pairs) that were dropped. */
1087size_t
1088ovsdb_datum_sort_unique(struct ovsdb_datum *datum,
1089 enum ovsdb_atomic_type key_type,
1090 enum ovsdb_atomic_type value_type)
1091{
1092 size_t src, dst;
1093
1094 if (datum->n < 2) {
1095 return 0;
1096 }
1097
1098 ovsdb_datum_sort__(datum, key_type, value_type);
1099
1100 dst = 1;
1101 for (src = 1; src < datum->n; src++) {
1102 if (ovsdb_atom_equals(&datum->keys[src], &datum->keys[dst - 1],
1103 key_type)) {
1104 ovsdb_atom_destroy(&datum->keys[src], key_type);
1105 if (value_type != OVSDB_TYPE_VOID) {
1106 ovsdb_atom_destroy(&datum->values[src], value_type);
1107 }
1108 } else {
1109 if (src != dst) {
1110 datum->keys[dst] = datum->keys[src];
1111 if (value_type != OVSDB_TYPE_VOID) {
1112 datum->values[dst] = datum->values[src];
1113 }
1114 }
1115 dst++;
1116 }
1117 }
1118 datum->n = dst;
1119 return datum->n - src;
1120}
1121
bd76d25d
BP
1122/* Checks that each of the atoms in 'datum' conforms to the constraints
1123 * specified by its 'type'. Returns an error if a constraint is violated,
1124 * otherwise a null pointer.
1125 *
1126 * This function is not commonly useful because the most ordinary way to obtain
1127 * a datum is ultimately via ovsdb_atom_from_string() or
1128 * ovsdb_atom_from_json(), which check constraints themselves. */
1129struct ovsdb_error *
1130ovsdb_datum_check_constraints(const struct ovsdb_datum *datum,
1131 const struct ovsdb_type *type)
1132{
1133 struct ovsdb_error *error;
1134 unsigned int i;
1135
1136 for (i = 0; i < datum->n; i++) {
1137 error = ovsdb_atom_check_constraints(&datum->keys[i], &type->key);
1138 if (error) {
1139 return error;
1140 }
1141 }
1142
1143 if (type->value.type != OVSDB_TYPE_VOID) {
1144 for (i = 0; i < datum->n; i++) {
1145 error = ovsdb_atom_check_constraints(&datum->values[i],
1146 &type->value);
1147 if (error) {
1148 return error;
1149 }
1150 }
1151 }
1152
1153 return NULL;
1154}
1155
2b66469b
BP
1156static struct ovsdb_error *
1157ovsdb_datum_from_json__(struct ovsdb_datum *datum,
1158 const struct ovsdb_type *type,
1159 const struct json *json,
1160 struct ovsdb_symbol_table *symtab)
f85f8ebb
BP
1161{
1162 struct ovsdb_error *error;
1163
ae8f13e2
BP
1164 if (ovsdb_type_is_map(type)
1165 || (json->type == JSON_ARRAY
1166 && json->u.array.n > 0
1167 && json->u.array.elems[0]->type == JSON_STRING
1168 && !strcmp(json->u.array.elems[0]->u.string, "set"))) {
f85f8ebb
BP
1169 bool is_map = ovsdb_type_is_map(type);
1170 const char *class = is_map ? "map" : "set";
1171 const struct json *inner;
1172 unsigned int i;
1173 size_t n;
1174
f85f8ebb
BP
1175 error = unwrap_json(json, class, JSON_ARRAY, &inner);
1176 if (error) {
1177 return error;
1178 }
1179
1180 n = inner->u.array.n;
1181 if (n < type->n_min || n > type->n_max) {
1182 return ovsdb_syntax_error(json, NULL, "%s must have %u to "
1183 "%u members but %zu are present",
1184 class, type->n_min, type->n_max, n);
1185 }
1186
1187 datum->n = 0;
1188 datum->keys = xmalloc(n * sizeof *datum->keys);
1189 datum->values = is_map ? xmalloc(n * sizeof *datum->values) : NULL;
1190 for (i = 0; i < n; i++) {
1191 const struct json *element = inner->u.array.elems[i];
1192 const struct json *key = NULL;
1193 const struct json *value = NULL;
1194
1195 if (!is_map) {
1196 key = element;
1197 } else {
1198 error = parse_json_pair(element, &key, &value);
1199 if (error) {
1200 goto error;
1201 }
1202 }
1203
bd76d25d 1204 error = ovsdb_atom_from_json(&datum->keys[i], &type->key,
f85f8ebb
BP
1205 key, symtab);
1206 if (error) {
1207 goto error;
1208 }
1209
1210 if (is_map) {
1211 error = ovsdb_atom_from_json(&datum->values[i],
bd76d25d 1212 &type->value, value, symtab);
f85f8ebb 1213 if (error) {
bd76d25d 1214 ovsdb_atom_destroy(&datum->keys[i], type->key.type);
f85f8ebb
BP
1215 goto error;
1216 }
1217 }
1218
1219 datum->n++;
1220 }
f85f8ebb
BP
1221 return NULL;
1222
1223 error:
1224 ovsdb_datum_destroy(datum, type);
1225 return error;
ae8f13e2
BP
1226 } else {
1227 datum->n = 1;
1228 datum->keys = xmalloc(sizeof *datum->keys);
1229 datum->values = NULL;
1230
1231 error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
1232 json, symtab);
1233 if (error) {
1234 free(datum->keys);
1235 }
1236 return error;
f85f8ebb
BP
1237 }
1238}
1239
2b66469b
BP
1240/* Parses 'json' as a datum of the type described by 'type'. If successful,
1241 * returns NULL and initializes 'datum' with the parsed datum. On failure,
1242 * returns an error and the contents of 'datum' are indeterminate. The caller
1243 * is responsible for freeing the error or the datum that is returned.
1244 *
1245 * Violations of constraints expressed by 'type' are treated as errors.
1246 *
1247 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
1248 * ovsdb/SPECS for information about this, and for the syntax that this
1249 * function accepts. */
1250struct ovsdb_error *
1251ovsdb_datum_from_json(struct ovsdb_datum *datum,
1252 const struct ovsdb_type *type,
1253 const struct json *json,
1254 struct ovsdb_symbol_table *symtab)
1255{
1256 struct ovsdb_error *error;
1257
1258 error = ovsdb_datum_from_json__(datum, type, json, symtab);
1259 if (error) {
1260 return error;
1261 }
1262
1263 error = ovsdb_datum_sort(datum, type->key.type);
1264 if (error) {
1265 ovsdb_datum_destroy(datum, type);
1266 }
1267 return error;
1268}
1269
958ac03a
BP
1270/* Converts 'datum', of the specified 'type', to JSON format, and returns the
1271 * JSON. The caller is responsible for freeing the returned JSON.
1272 *
1273 * 'type' constraints on datum->n are ignored.
1274 *
1275 * Refer to ovsdb/SPECS for the format of the JSON that this function
1276 * produces. */
f85f8ebb
BP
1277struct json *
1278ovsdb_datum_to_json(const struct ovsdb_datum *datum,
1279 const struct ovsdb_type *type)
1280{
6a6f8d16 1281 if (ovsdb_type_is_map(type)) {
f85f8ebb
BP
1282 struct json **elems;
1283 size_t i;
1284
1285 elems = xmalloc(datum->n * sizeof *elems);
1286 for (i = 0; i < datum->n; i++) {
6a6f8d16
BP
1287 elems[i] = json_array_create_2(
1288 ovsdb_atom_to_json(&datum->keys[i], type->key.type),
1289 ovsdb_atom_to_json(&datum->values[i], type->value.type));
f85f8ebb
BP
1290 }
1291
6a6f8d16
BP
1292 return wrap_json("map", json_array_create(elems, datum->n));
1293 } else if (datum->n == 1) {
1294 return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
f85f8ebb
BP
1295 } else {
1296 struct json **elems;
1297 size_t i;
1298
1299 elems = xmalloc(datum->n * sizeof *elems);
1300 for (i = 0; i < datum->n; i++) {
6a6f8d16 1301 elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
f85f8ebb
BP
1302 }
1303
6a6f8d16 1304 return wrap_json("set", json_array_create(elems, datum->n));
f85f8ebb
BP
1305 }
1306}
1307
0194f33a
BP
1308static const char *
1309skip_spaces(const char *p)
1310{
cb456217
BP
1311 while (isspace((unsigned char) *p)) {
1312 p++;
1313 }
1314 return p;
0194f33a
BP
1315}
1316
1bc6ff29 1317static char *
bd76d25d 1318parse_atom_token(const char **s, const struct ovsdb_base_type *base,
ce5a3e38 1319 union ovsdb_atom *atom, struct ovsdb_symbol_table *symtab)
0194f33a 1320{
1bc6ff29 1321 char *token, *error;
0194f33a 1322
1bc6ff29
BP
1323 error = ovsdb_token_parse(s, &token);
1324 if (!error) {
ce5a3e38 1325 error = ovsdb_atom_from_string(atom, base, token, symtab);
1bc6ff29
BP
1326 free(token);
1327 }
1328 return error;
1329}
0194f33a 1330
1bc6ff29
BP
1331static char *
1332parse_key_value(const char **s, const struct ovsdb_type *type,
ce5a3e38
BP
1333 union ovsdb_atom *key, union ovsdb_atom *value,
1334 struct ovsdb_symbol_table *symtab)
1bc6ff29
BP
1335{
1336 const char *start = *s;
1337 char *error;
1338
ce5a3e38 1339 error = parse_atom_token(s, &type->key, key, symtab);
bd76d25d 1340 if (!error && type->value.type != OVSDB_TYPE_VOID) {
9397a13f 1341 *s = skip_spaces(*s);
1bc6ff29
BP
1342 if (**s == '=') {
1343 (*s)++;
9397a13f 1344 *s = skip_spaces(*s);
ce5a3e38 1345 error = parse_atom_token(s, &type->value, value, symtab);
1bc6ff29
BP
1346 } else {
1347 error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
1348 start, **s);
1349 }
1350 if (error) {
bd76d25d 1351 ovsdb_atom_destroy(key, type->key.type);
0194f33a 1352 }
0194f33a 1353 }
1bc6ff29 1354 return error;
0194f33a
BP
1355}
1356
1357static void
1358free_key_value(const struct ovsdb_type *type,
1359 union ovsdb_atom *key, union ovsdb_atom *value)
1360{
bd76d25d
BP
1361 ovsdb_atom_destroy(key, type->key.type);
1362 if (type->value.type != OVSDB_TYPE_VOID) {
1363 ovsdb_atom_destroy(value, type->value.type);
0194f33a
BP
1364 }
1365}
1366
1367/* Initializes 'datum' as a datum of the given 'type', parsing its contents
1368 * from 's'. The format of 's' is a series of space or comma separated atoms
1369 * or, for a map, '='-delimited pairs of atoms. Each atom must in a format
1370 * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed
1371 * in "[]" or a map in "{}"; for an empty set or map these punctuators are
ce5a3e38
BP
1372 * required.
1373 *
1374 * Optionally, a symbol table may be supplied as 'symtab'. It is passed to
1375 * ovsdb_atom_to_string(). */
1bc6ff29 1376char *
0194f33a 1377ovsdb_datum_from_string(struct ovsdb_datum *datum,
ce5a3e38
BP
1378 const struct ovsdb_type *type, const char *s,
1379 struct ovsdb_symbol_table *symtab)
0194f33a
BP
1380{
1381 bool is_map = ovsdb_type_is_map(type);
1bc6ff29 1382 struct ovsdb_error *dberror;
0194f33a
BP
1383 const char *p;
1384 int end_delim;
1bc6ff29 1385 char *error;
0194f33a
BP
1386
1387 ovsdb_datum_init_empty(datum);
1388
1389 /* Swallow a leading delimiter if there is one. */
1390 p = skip_spaces(s);
1391 if (*p == (is_map ? '{' : '[')) {
1392 end_delim = is_map ? '}' : ']';
1393 p = skip_spaces(p + 1);
1394 } else if (!*p) {
1395 if (is_map) {
1bc6ff29 1396 return xstrdup("use \"{}\" to specify the empty map");
0194f33a 1397 } else {
1bc6ff29 1398 return xstrdup("use \"[]\" to specify the empty set");
0194f33a
BP
1399 }
1400 } else {
1401 end_delim = 0;
1402 }
1403
1404 while (*p && *p != end_delim) {
1405 union ovsdb_atom key, value;
1406
1407 if (ovsdb_token_is_delim(*p)) {
9466d7d4 1408 char *type_str = ovsdb_type_to_english(type);
1bc6ff29 1409 error = xasprintf("%s: unexpected \"%c\" parsing %s",
9466d7d4
JP
1410 s, *p, type_str);
1411 free(type_str);
1bc6ff29 1412 goto error;
0194f33a
BP
1413 }
1414
1415 /* Add to datum. */
ce5a3e38 1416 error = parse_key_value(&p, type, &key, &value, symtab);
1bc6ff29
BP
1417 if (error) {
1418 goto error;
1419 }
0194f33a
BP
1420 ovsdb_datum_add_unsafe(datum, &key, &value, type);
1421 free_key_value(type, &key, &value);
1422
1423 /* Skip optional white space and comma. */
1424 p = skip_spaces(p);
1425 if (*p == ',') {
1426 p = skip_spaces(p + 1);
1427 }
1428 }
1429
1430 if (*p != end_delim) {
1bc6ff29
BP
1431 error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim);
1432 goto error;
0194f33a
BP
1433 }
1434 if (end_delim) {
1435 p = skip_spaces(p + 1);
1436 if (*p) {
1bc6ff29
BP
1437 error = xasprintf("%s: trailing garbage after \"%c\"",
1438 s, end_delim);
1439 goto error;
0194f33a
BP
1440 }
1441 }
1442
1443 if (datum->n < type->n_min) {
1bc6ff29
BP
1444 error = xasprintf("%s: %u %s specified but the minimum number is %u",
1445 s, datum->n, is_map ? "pair(s)" : "value(s)",
1446 type->n_min);
1447 goto error;
0194f33a 1448 } else if (datum->n > type->n_max) {
1bc6ff29
BP
1449 error = xasprintf("%s: %u %s specified but the maximum number is %u",
1450 s, datum->n, is_map ? "pair(s)" : "value(s)",
1451 type->n_max);
1452 goto error;
0194f33a
BP
1453 }
1454
bfc96d9b 1455 dberror = ovsdb_datum_sort(datum, type->key.type);
1bc6ff29
BP
1456 if (dberror) {
1457 ovsdb_error_destroy(dberror);
0194f33a 1458 if (ovsdb_type_is_map(type)) {
1bc6ff29 1459 error = xasprintf("%s: map contains duplicate key", s);
0194f33a 1460 } else {
1bc6ff29 1461 error = xasprintf("%s: set contains duplicate value", s);
0194f33a 1462 }
1bc6ff29 1463 goto error;
0194f33a 1464 }
1bc6ff29
BP
1465
1466 return NULL;
1467
1468error:
1469 ovsdb_datum_destroy(datum, type);
1470 ovsdb_datum_init_empty(datum);
1471 return error;
0194f33a
BP
1472}
1473
1474/* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable
1475 * to ovsdb_datum_from_string(). */
1476void
1477ovsdb_datum_to_string(const struct ovsdb_datum *datum,
1478 const struct ovsdb_type *type, struct ds *out)
1479{
1480 bool is_map = ovsdb_type_is_map(type);
1481 size_t i;
1482
1483 if (type->n_max > 1 || !datum->n) {
1484 ds_put_char(out, is_map ? '{' : '[');
1485 }
1486 for (i = 0; i < datum->n; i++) {
1487 if (i > 0) {
1488 ds_put_cstr(out, ", ");
1489 }
1490
bd76d25d 1491 ovsdb_atom_to_string(&datum->keys[i], type->key.type, out);
0194f33a
BP
1492 if (is_map) {
1493 ds_put_char(out, '=');
bd76d25d 1494 ovsdb_atom_to_string(&datum->values[i], type->value.type, out);
0194f33a
BP
1495 }
1496 }
1497 if (type->n_max > 1 || !datum->n) {
1498 ds_put_char(out, is_map ? '}' : ']');
1499 }
1500}
1501
c6a41252
BP
1502/* Appends to 'out' the 'datum' (with the given 'type') in a bare string format
1503 * that cannot be parsed uniformly back into a datum but is easier for shell
1504 * scripts, etc., to deal with. */
1505void
1506ovsdb_datum_to_bare(const struct ovsdb_datum *datum,
1507 const struct ovsdb_type *type, struct ds *out)
1508{
1509 bool is_map = ovsdb_type_is_map(type);
1510 size_t i;
1511
1512 for (i = 0; i < datum->n; i++) {
1513 if (i > 0) {
1514 ds_put_cstr(out, " ");
1515 }
1516
1517 ovsdb_atom_to_bare(&datum->keys[i], type->key.type, out);
1518 if (is_map) {
1519 ds_put_char(out, '=');
1520 ovsdb_atom_to_bare(&datum->values[i], type->value.type, out);
1521 }
1522 }
1523}
1524
e4af5615
BP
1525/* Initializes 'datum' as a string-to-string map whose contents are taken from
1526 * 'sh'. Destroys 'sh'. */
1527void
1528ovsdb_datum_from_shash(struct ovsdb_datum *datum, struct shash *sh)
1529{
1530 struct shash_node *node, *next;
1531 size_t i;
1532
1533 datum->n = shash_count(sh);
1534 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
1535 datum->values = xmalloc(datum->n * sizeof *datum->values);
1536
1537 i = 0;
1538 SHASH_FOR_EACH_SAFE (node, next, sh) {
1539 datum->keys[i].string = node->name;
1540 datum->values[i].string = node->data;
1541 shash_steal(sh, node);
1542 i++;
1543 }
1544 assert(i == datum->n);
1545
1546 shash_destroy(sh);
1547 ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
1548}
1549
f85f8ebb
BP
1550static uint32_t
1551hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms,
1552 unsigned int n, uint32_t basis)
1553{
1554 if (type != OVSDB_TYPE_VOID) {
1555 unsigned int i;
1556
1557 for (i = 0; i < n; i++) {
1558 basis = ovsdb_atom_hash(&atoms[i], type, basis);
1559 }
1560 }
1561 return basis;
1562}
1563
1564uint32_t
1565ovsdb_datum_hash(const struct ovsdb_datum *datum,
1566 const struct ovsdb_type *type, uint32_t basis)
1567{
bd76d25d
BP
1568 basis = hash_atoms(type->key.type, datum->keys, datum->n, basis);
1569 basis ^= (type->key.type << 24) | (type->value.type << 16) | datum->n;
1570 basis = hash_atoms(type->value.type, datum->values, datum->n, basis);
f85f8ebb
BP
1571 return basis;
1572}
1573
1574static int
1575atom_arrays_compare_3way(const union ovsdb_atom *a,
2f47998b
BP
1576 const union ovsdb_atom *b,
1577 enum ovsdb_atomic_type type,
1578 size_t n)
f85f8ebb
BP
1579{
1580 unsigned int i;
1581
1582 for (i = 0; i < n; i++) {
1583 int cmp = ovsdb_atom_compare_3way(&a[i], &b[i], type);
1584 if (cmp) {
1585 return cmp;
1586 }
1587 }
1588
1589 return 0;
1590}
1591
1592bool
1593ovsdb_datum_equals(const struct ovsdb_datum *a,
1594 const struct ovsdb_datum *b,
1595 const struct ovsdb_type *type)
1596{
1597 return !ovsdb_datum_compare_3way(a, b, type);
1598}
1599
1600int
1601ovsdb_datum_compare_3way(const struct ovsdb_datum *a,
1602 const struct ovsdb_datum *b,
1603 const struct ovsdb_type *type)
1604{
1605 int cmp;
1606
1607 if (a->n != b->n) {
1608 return a->n < b->n ? -1 : 1;
1609 }
1610
bd76d25d 1611 cmp = atom_arrays_compare_3way(a->keys, b->keys, type->key.type, a->n);
f85f8ebb
BP
1612 if (cmp) {
1613 return cmp;
1614 }
1615
bd76d25d
BP
1616 return (type->value.type == OVSDB_TYPE_VOID ? 0
1617 : atom_arrays_compare_3way(a->values, b->values, type->value.type,
f85f8ebb
BP
1618 a->n));
1619}
1620
2f47998b 1621/* If 'key' is one of the keys in 'datum', returns its index within 'datum',
bd76d25d 1622 * otherwise UINT_MAX. 'key.type' must be the type of the atoms stored in the
2f47998b
BP
1623 * 'keys' array in 'datum'.
1624 */
1625unsigned int
1626ovsdb_datum_find_key(const struct ovsdb_datum *datum,
1627 const union ovsdb_atom *key,
1628 enum ovsdb_atomic_type key_type)
1629{
1630 unsigned int low = 0;
1631 unsigned int high = datum->n;
1632 while (low < high) {
1633 unsigned int idx = (low + high) / 2;
1634 int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type);
1635 if (cmp < 0) {
1636 high = idx;
1637 } else if (cmp > 0) {
1638 low = idx + 1;
1639 } else {
1640 return idx;
1641 }
1642 }
1643 return UINT_MAX;
1644}
1645
1646/* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its
bd76d25d 1647 * index within 'datum', otherwise UINT_MAX. 'key.type' must be the type of
2f47998b
BP
1648 * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the
1649 * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys.
1650 */
1651unsigned int
1652ovsdb_datum_find_key_value(const struct ovsdb_datum *datum,
1653 const union ovsdb_atom *key,
1654 enum ovsdb_atomic_type key_type,
1655 const union ovsdb_atom *value,
1656 enum ovsdb_atomic_type value_type)
1657{
1658 unsigned int idx = ovsdb_datum_find_key(datum, key, key_type);
1659 if (idx != UINT_MAX
1660 && value_type != OVSDB_TYPE_VOID
1661 && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) {
1662 idx = UINT_MAX;
1663 }
1664 return idx;
1665}
1666
e9f8f936
BP
1667/* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise
1668 * UINT_MAX. 'type' must be the type of 'a' and 'b', except that
bd76d25d 1669 * type->value.type may be set to OVSDB_TYPE_VOID to compare keys but not
e9f8f936
BP
1670 * values. */
1671static unsigned int
1672ovsdb_datum_find(const struct ovsdb_datum *a, int i,
1673 const struct ovsdb_datum *b,
1674 const struct ovsdb_type *type)
f85f8ebb 1675{
2f47998b 1676 return ovsdb_datum_find_key_value(b,
bd76d25d 1677 &a->keys[i], type->key.type,
2f47998b 1678 a->values ? &a->values[i] : NULL,
bd76d25d 1679 type->value.type);
f85f8ebb
BP
1680}
1681
1682/* Returns true if every element in 'a' is also in 'b', false otherwise. */
1683bool
1684ovsdb_datum_includes_all(const struct ovsdb_datum *a,
1685 const struct ovsdb_datum *b,
1686 const struct ovsdb_type *type)
1687{
1688 size_t i;
1689
ca261b65
BP
1690 if (a->n > b->n) {
1691 return false;
1692 }
f85f8ebb 1693 for (i = 0; i < a->n; i++) {
e9f8f936 1694 if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) {
f85f8ebb
BP
1695 return false;
1696 }
1697 }
1698 return true;
1699}
1700
1701/* Returns true if no element in 'a' is also in 'b', false otherwise. */
1702bool
1703ovsdb_datum_excludes_all(const struct ovsdb_datum *a,
1704 const struct ovsdb_datum *b,
1705 const struct ovsdb_type *type)
1706{
1707 size_t i;
1708
1709 for (i = 0; i < a->n; i++) {
e9f8f936 1710 if (ovsdb_datum_find(a, i, b, type) != UINT_MAX) {
f85f8ebb
BP
1711 return false;
1712 }
1713 }
1714 return true;
1715}
e9f8f936
BP
1716
1717static void
1718ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type,
1719 unsigned int capacity)
1720{
1721 a->keys = xrealloc(a->keys, capacity * sizeof *a->keys);
bd76d25d 1722 if (type->value.type != OVSDB_TYPE_VOID) {
e9f8f936
BP
1723 a->values = xrealloc(a->values, capacity * sizeof *a->values);
1724 }
1725}
1726
2f47998b
BP
1727/* Removes the element with index 'idx' from 'datum', which has type 'type'.
1728 * If 'idx' is not the last element in 'datum', then the removed element is
1729 * replaced by the (former) last element.
1730 *
1731 * This function does not maintain ovsdb_datum invariants. Use
1732 * ovsdb_datum_sort() to check and restore these invariants. */
1733void
1734ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx,
1735 const struct ovsdb_type *type)
e9f8f936 1736{
bd76d25d 1737 ovsdb_atom_destroy(&datum->keys[idx], type->key.type);
2f47998b 1738 datum->keys[idx] = datum->keys[datum->n - 1];
bd76d25d
BP
1739 if (type->value.type != OVSDB_TYPE_VOID) {
1740 ovsdb_atom_destroy(&datum->values[idx], type->value.type);
2f47998b
BP
1741 datum->values[idx] = datum->values[datum->n - 1];
1742 }
1743 datum->n--;
1744}
1745
1746/* Adds the element with the given 'key' and 'value' to 'datum', which must
1747 * have the specified 'type'.
1748 *
1749 * This function always allocates memory, so it is not an efficient way to add
1750 * a number of elements to a datum.
1751 *
1752 * This function does not maintain ovsdb_datum invariants. Use
1753 * ovsdb_datum_sort() to check and restore these invariants. (But a datum with
1754 * 0 or 1 elements cannot violate the invariants anyhow.) */
1755void
1756ovsdb_datum_add_unsafe(struct ovsdb_datum *datum,
1757 const union ovsdb_atom *key,
1758 const union ovsdb_atom *value,
1759 const struct ovsdb_type *type)
1760{
1761 size_t idx = datum->n++;
1762 datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys);
bd76d25d
BP
1763 ovsdb_atom_clone(&datum->keys[idx], key, type->key.type);
1764 if (type->value.type != OVSDB_TYPE_VOID) {
2f47998b
BP
1765 datum->values = xrealloc(datum->values,
1766 datum->n * sizeof *datum->values);
bd76d25d 1767 ovsdb_atom_clone(&datum->values[idx], value, type->value.type);
e9f8f936 1768 }
e9f8f936
BP
1769}
1770
1771void
2f47998b
BP
1772ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b,
1773 const struct ovsdb_type *type, bool replace)
e9f8f936 1774{
e9f8f936 1775 unsigned int n;
2f47998b 1776 size_t bi;
e9f8f936 1777
e9f8f936 1778 n = a->n;
2f47998b
BP
1779 for (bi = 0; bi < b->n; bi++) {
1780 unsigned int ai;
1781
bd76d25d 1782 ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key.type);
2f47998b 1783 if (ai == UINT_MAX) {
e9f8f936 1784 if (n == a->n) {
2f47998b 1785 ovsdb_datum_reallocate(a, type, a->n + (b->n - bi));
e9f8f936 1786 }
bd76d25d
BP
1787 ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key.type);
1788 if (type->value.type != OVSDB_TYPE_VOID) {
2f47998b 1789 ovsdb_atom_clone(&a->values[n], &b->values[bi],
bd76d25d 1790 type->value.type);
e9f8f936
BP
1791 }
1792 n++;
bd76d25d
BP
1793 } else if (replace && type->value.type != OVSDB_TYPE_VOID) {
1794 ovsdb_atom_destroy(&a->values[ai], type->value.type);
2f47998b 1795 ovsdb_atom_clone(&a->values[ai], &b->values[bi],
bd76d25d 1796 type->value.type);
e9f8f936
BP
1797 }
1798 }
1799 if (n != a->n) {
1800 struct ovsdb_error *error;
1801 a->n = n;
bfc96d9b 1802 error = ovsdb_datum_sort(a, type->key.type);
e9f8f936
BP
1803 assert(!error);
1804 }
1805}
1806
1807void
1808ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type,
1809 const struct ovsdb_datum *b,
1810 const struct ovsdb_type *b_type)
1811{
1812 bool changed = false;
1813 size_t i;
1814
bd76d25d
BP
1815 assert(a_type->key.type == b_type->key.type);
1816 assert(a_type->value.type == b_type->value.type
1817 || b_type->value.type == OVSDB_TYPE_VOID);
e9f8f936
BP
1818
1819 /* XXX The big-O of this could easily be improved. */
1820 for (i = 0; i < a->n; ) {
1821 unsigned int idx = ovsdb_datum_find(a, i, b, b_type);
1822 if (idx != UINT_MAX) {
1823 changed = true;
2f47998b 1824 ovsdb_datum_remove_unsafe(a, i, a_type);
e9f8f936
BP
1825 } else {
1826 i++;
1827 }
1828 }
1829 if (changed) {
bfc96d9b 1830 ovsdb_datum_sort_assert(a, a_type->key.type);
e9f8f936
BP
1831 }
1832}
f85f8ebb 1833\f
f85f8ebb
BP
1834struct ovsdb_symbol_table *
1835ovsdb_symbol_table_create(void)
1836{
1837 struct ovsdb_symbol_table *symtab = xmalloc(sizeof *symtab);
1838 shash_init(&symtab->sh);
1839 return symtab;
1840}
1841
1842void
1843ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *symtab)
1844{
1845 if (symtab) {
506051fc 1846 shash_destroy_free_data(&symtab->sh);
f85f8ebb
BP
1847 free(symtab);
1848 }
1849}
1850
2d2d6d4a 1851struct ovsdb_symbol *
f85f8ebb
BP
1852ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab,
1853 const char *name)
1854{
1855 return shash_find_data(&symtab->sh, name);
1856}
1857
fbf925e4 1858struct ovsdb_symbol *
f85f8ebb 1859ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
e9387de4 1860 const struct uuid *uuid, bool created)
f85f8ebb 1861{
2d2d6d4a
BP
1862 struct ovsdb_symbol *symbol;
1863
1864 assert(!ovsdb_symbol_table_get(symtab, name));
1865 symbol = xmalloc(sizeof *symbol);
1866 symbol->uuid = *uuid;
e9387de4 1867 symbol->created = created;
c5f341ab
BP
1868 symbol->strong_ref = false;
1869 symbol->weak_ref = false;
2d2d6d4a 1870 shash_add(&symtab->sh, name, symbol);
fbf925e4
BP
1871 return symbol;
1872}
1873
1874struct ovsdb_symbol *
1875ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab,
1876 const char *name)
1877{
1878 struct ovsdb_symbol *symbol;
1879
1880 symbol = ovsdb_symbol_table_get(symtab, name);
1881 if (!symbol) {
1882 struct uuid uuid;
1883
1884 uuid_generate(&uuid);
1885 symbol = ovsdb_symbol_table_put(symtab, name, &uuid, false);
1886 }
1887 return symbol;
f85f8ebb 1888}
0194f33a
BP
1889\f
1890/* Extracts a token from the beginning of 's' and returns a pointer just after
1891 * the token. Stores the token itself into '*outp', which the caller is
1892 * responsible for freeing (with free()).
1893 *
1894 * If 's[0]' is a delimiter, the returned token is the empty string.
1895 *
1896 * A token extends from 's' to the first delimiter, as defined by
1897 * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be
1898 * escaped with a backslash, in which case the backslash does not appear in the
1899 * output. Double quotes also cause delimiters to be ignored, but the double
1900 * quotes are retained in the output. (Backslashes inside double quotes are
1901 * not removed, either.)
1902 */
1bc6ff29
BP
1903char *
1904ovsdb_token_parse(const char **s, char **outp)
0194f33a
BP
1905{
1906 const char *p;
1907 struct ds out;
1908 bool in_quotes;
1bc6ff29 1909 char *error;
0194f33a
BP
1910
1911 ds_init(&out);
1912 in_quotes = false;
1bc6ff29 1913 for (p = *s; *p != '\0'; ) {
0194f33a
BP
1914 int c = *p++;
1915 if (c == '\\') {
1916 if (in_quotes) {
1917 ds_put_char(&out, '\\');
1918 }
1919 if (!*p) {
1bc6ff29
BP
1920 error = xasprintf("%s: backslash at end of argument", *s);
1921 goto error;
0194f33a
BP
1922 }
1923 ds_put_char(&out, *p++);
1924 } else if (!in_quotes && ovsdb_token_is_delim(c)) {
1925 p--;
1926 break;
1927 } else {
1928 ds_put_char(&out, c);
1929 if (c == '"') {
1930 in_quotes = !in_quotes;
1931 }
1932 }
1933 }
1934 if (in_quotes) {
1bc6ff29
BP
1935 error = xasprintf("%s: quoted string extends past end of argument",
1936 *s);
1937 goto error;
0194f33a
BP
1938 }
1939 *outp = ds_cstr(&out);
1bc6ff29
BP
1940 *s = p;
1941 return NULL;
1942
1943error:
1944 ds_destroy(&out);
1945 *outp = NULL;
1946 return error;
0194f33a
BP
1947}
1948
1949/* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */
1950bool
1951ovsdb_token_is_delim(unsigned char c)
1952{
e89e5374 1953 return strchr(":=, []{}!<>", c) != NULL;
0194f33a 1954}