blob: 4938325aea69d9f8df63243740689888c48dc8fa [file] [log] [blame]
Michal Vasko1324b6c2018-09-07 11:16:23 +02001/**
2 * @file common.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief common internal definitions for libyang
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejcib7db73a2018-10-24 14:18:40 +020014
15#include "common.h"
Michal Vasko1324b6c2018-09-07 11:16:23 +020016
Radek Krejci86d106e2018-10-18 09:53:19 +020017#include <assert.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020018#include <ctype.h>
Radek Krejci4546aa62019-07-15 16:53:32 +020019#include <errno.h>
20#include <stdarg.h>
21#include <stdlib.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020022#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <sys/mman.h>
24#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020025#include <unistd.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020026
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "extensions.h"
Michal Vasko841d1a92018-09-07 15:40:31 +020028#include "tree_schema.h"
Radek Krejcib4a4a272019-06-10 12:44:52 +020029#include "tree_schema_internal.h"
Michal Vasko1324b6c2018-09-07 11:16:23 +020030
Radek Krejcic59bc972018-09-17 16:13:06 +020031const char *const ly_stmt_list[] = {
32 [YANG_ACTION] = "action",
33 [YANG_ANYDATA] = "anydata",
34 [YANG_ANYXML] = "anyxml",
35 [YANG_ARGUMENT] = "argument",
36 [YANG_AUGMENT] = "augment",
37 [YANG_BASE] = "base",
38 [YANG_BELONGS_TO] = "belongs-to",
39 [YANG_BIT] = "bit",
40 [YANG_CASE] = "case",
41 [YANG_CHOICE] = "choice",
42 [YANG_CONFIG] = "config",
43 [YANG_CONTACT] = "contact",
44 [YANG_CONTAINER] = "container",
45 [YANG_CUSTOM] = "<extension-instance>",
46 [YANG_DEFAULT] = "default",
47 [YANG_DESCRIPTION] = "description",
48 [YANG_DEVIATE] = "deviate",
49 [YANG_DEVIATION] = "deviation",
50 [YANG_ENUM] = "enum",
51 [YANG_ERROR_APP_TAG] = "error-app-tag",
52 [YANG_ERROR_MESSAGE] = "error-message",
53 [YANG_EXTENSION] = "extension",
54 [YANG_FEATURE] = "feature",
55 [YANG_FRACTION_DIGITS] = "fraction-digits",
56 [YANG_GROUPING] = "grouping",
57 [YANG_IDENTITY] = "identitiy",
58 [YANG_IF_FEATURE] = "if-feature",
59 [YANG_IMPORT] = "import",
60 [YANG_INCLUDE] = "include",
61 [YANG_INPUT] = "input",
62 [YANG_KEY] = "key",
63 [YANG_LEAF] = "leaf",
64 [YANG_LEAF_LIST] = "leaf-list",
65 [YANG_LENGTH] = "length",
66 [YANG_LIST] = "list",
67 [YANG_MANDATORY] = "mandatory",
68 [YANG_MAX_ELEMENTS] = "max-elements",
69 [YANG_MIN_ELEMENTS] = "min-elements",
70 [YANG_MODIFIER] = "modifier",
71 [YANG_MODULE] = "module",
72 [YANG_MUST] = "must",
73 [YANG_NAMESPACE] = "namespace",
74 [YANG_NOTIFICATION] = "notification",
75 [YANG_ORDERED_BY] = "ordered-by",
76 [YANG_ORGANIZATION] = "organization",
77 [YANG_OUTPUT] = "output",
78 [YANG_PATH] = "path",
79 [YANG_PATTERN] = "pattern",
80 [YANG_POSITION] = "position",
81 [YANG_PREFIX] = "prefix",
82 [YANG_PRESENCE] = "presence",
83 [YANG_RANGE] = "range",
84 [YANG_REFERENCE] = "reference",
85 [YANG_REFINE] = "refine",
86 [YANG_REQUIRE_INSTANCE] = "require-instance",
87 [YANG_REVISION] = "revision",
88 [YANG_REVISION_DATE] = "revision-date",
89 [YANG_RPC] = "rpc",
90 [YANG_STATUS] = "status",
91 [YANG_SUBMODULE] = "submodule",
92 [YANG_TYPE] = "type",
93 [YANG_TYPEDEF] = "typedef",
94 [YANG_UNIQUE] = "unique",
95 [YANG_UNITS] = "units",
96 [YANG_USES] = "uses",
97 [YANG_VALUE] = "value",
98 [YANG_WHEN] = "when",
99 [YANG_YANG_VERSION] = "yang-version",
100 [YANG_YIN_ELEMENT] = "yin-element",
101 [YANG_SEMICOLON] = ";",
102 [YANG_LEFT_BRACE] = "{",
103 [YANG_RIGHT_BRACE] = "}",
104};
105
106const char *const lyext_substmt_list[] = {
107 [LYEXT_SUBSTMT_ARGUMENT] = "argument",
108 [LYEXT_SUBSTMT_BASE] = "base",
109 [LYEXT_SUBSTMT_BELONGSTO] = "belongs-to",
110 [LYEXT_SUBSTMT_CONTACT] = "contact",
111 [LYEXT_SUBSTMT_DEFAULT] = "default",
112 [LYEXT_SUBSTMT_DESCRIPTION] = "description",
113 [LYEXT_SUBSTMT_ERRTAG] = "error-app-tag",
114 [LYEXT_SUBSTMT_ERRMSG] = "error-message",
115 [LYEXT_SUBSTMT_KEY] = "key",
116 [LYEXT_SUBSTMT_NAMESPACE] = "namespace",
117 [LYEXT_SUBSTMT_ORGANIZATION] = "organization",
118 [LYEXT_SUBSTMT_PATH] = "path",
119 [LYEXT_SUBSTMT_PREFIX] = "prefix",
120 [LYEXT_SUBSTMT_PRESENCE] = "presence",
121 [LYEXT_SUBSTMT_REFERENCE] = "reference",
122 [LYEXT_SUBSTMT_REVISIONDATE] = "revision-date",
123 [LYEXT_SUBSTMT_UNITS] = "units",
124 [LYEXT_SUBSTMT_VALUE] = "value",
125 [LYEXT_SUBSTMT_VERSION] = "yang-version",
126 [LYEXT_SUBSTMT_MODIFIER] = "modifier",
127 [LYEXT_SUBSTMT_REQINSTANCE] = "require-instance",
128 [LYEXT_SUBSTMT_YINELEM] = "yin-element",
129 [LYEXT_SUBSTMT_CONFIG] = "config",
130 [LYEXT_SUBSTMT_MANDATORY] = "mandatory",
131 [LYEXT_SUBSTMT_ORDEREDBY] = "ordered-by",
132 [LYEXT_SUBSTMT_STATUS] = "status",
133 [LYEXT_SUBSTMT_FRACDIGITS] = "fraction-digits",
134 [LYEXT_SUBSTMT_MAX] = "max-elements",
135 [LYEXT_SUBSTMT_MIN] = "min-elements",
136 [LYEXT_SUBSTMT_POSITION] = "position",
137 [LYEXT_SUBSTMT_UNIQUE] = "unique",
138 [LYEXT_SUBSTMT_IFFEATURE] = "if-feature",
139};
140
141const char *const ly_devmod_list[] = {
142 [LYS_DEV_NOT_SUPPORTED] = "not-supported",
143 [LYS_DEV_ADD] = "add",
144 [LYS_DEV_DELETE] = "delete",
145 [LYS_DEV_REPLACE] = "replace",
146};
147
Michal Vasko1324b6c2018-09-07 11:16:23 +0200148void *
149ly_realloc(void *ptr, size_t size)
150{
151 void *new_mem;
152
153 new_mem = realloc(ptr, size);
154 if (!new_mem) {
155 free(ptr);
156 }
157
158 return new_mem;
159}
Michal Vasko841d1a92018-09-07 15:40:31 +0200160
Radek Krejcib416be62018-10-01 14:51:45 +0200161LY_ERR
162ly_getutf8(const char **input, unsigned int *utf8_char, size_t *bytes_read)
163{
164 unsigned int c, len;
165 int aux;
166 int i;
167
Radek Krejcicc6a45c2019-05-13 10:16:14 +0200168 if (bytes_read) {
169 (*bytes_read) = 0;
170 }
171
Radek Krejcib416be62018-10-01 14:51:45 +0200172 c = (*input)[0];
173 LY_CHECK_RET(!c, LY_EINVAL);
174
175 if (!(c & 0x80)) {
176 /* one byte character */
177 len = 1;
178
179 if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) {
180 return LY_EINVAL;
181 }
182 } else if ((c & 0xe0) == 0xc0) {
183 /* two bytes character */
184 len = 2;
185
186 aux = (*input)[1];
187 if ((aux & 0xc0) != 0x80) {
188 return LY_EINVAL;
189 }
190 c = ((c & 0x1f) << 6) | (aux & 0x3f);
191
192 if (c < 0x80) {
193 return LY_EINVAL;
194 }
195 } else if ((c & 0xf0) == 0xe0) {
196 /* three bytes character */
197 len = 3;
198
199 c &= 0x0f;
200 for (i = 1; i <= 2; i++) {
201 aux = (*input)[i];
202 if ((aux & 0xc0) != 0x80) {
203 return LY_EINVAL;
204 }
205
206 c = (c << 6) | (aux & 0x3f);
207 }
208
209 if (c < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) {
210 return LY_EINVAL;
211 }
212 } else if ((c & 0xf8) == 0xf0) {
213 /* four bytes character */
214 len = 4;
215
216 c &= 0x07;
217 for (i = 1; i <= 3; i++) {
218 aux = (*input)[i];
219 if ((aux & 0xc0) != 0x80) {
220 return LY_EINVAL;
221 }
222
223 c = (c << 6) | (aux & 0x3f);
224 }
225
226 if (c < 0x1000 || c > 0x10ffff) {
227 return LY_EINVAL;
228 }
229 } else {
230 return LY_EINVAL;
231 }
232
233 (*utf8_char) = c;
234 (*input) += len;
235 if (bytes_read) {
236 (*bytes_read) = len;
237 }
238 return LY_SUCCESS;
239}
240
Radek Krejcid972c252018-09-25 13:23:39 +0200241size_t
242LY_VCODE_INSTREXP_len(const char *str)
243{
244 size_t len = 0;
245 if (!str) {
246 return len;
247 } else if (!str[0]) {
248 return 1;
249 }
250 for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len);
251 return len;
252}
253
Radek Krejcif345c012018-09-19 11:12:59 +0200254LY_ERR
Radek Krejci86d106e2018-10-18 09:53:19 +0200255ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr)
Michal Vasko841d1a92018-09-07 15:40:31 +0200256{
Radek Krejci86d106e2018-10-18 09:53:19 +0200257 struct stat sb;
258 long pagesize;
259 size_t m;
Michal Vasko841d1a92018-09-07 15:40:31 +0200260
Radek Krejci86d106e2018-10-18 09:53:19 +0200261 assert(length);
262 assert(addr);
263 assert(fd >= 0);
Michal Vasko841d1a92018-09-07 15:40:31 +0200264
Radek Krejci86d106e2018-10-18 09:53:19 +0200265 if (fstat(fd, &sb) == -1) {
266 LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno));
267 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200268 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200269 if (!S_ISREG(sb.st_mode)) {
270 LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file.");
271 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200272 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200273 if (!sb.st_size) {
274 *addr = NULL;
275 return LY_SUCCESS;
276 }
277 pagesize = sysconf(_SC_PAGESIZE);
278
279 m = sb.st_size % pagesize;
280 if (m && pagesize - m >= 1) {
281 /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */
282 *length = sb.st_size + 1;
283 *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0);
284 } else {
285 /* there will not be enough bytes after the file content mapping for the additional bytes and some of them
286 * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS.
287 * Therefore we have to do the following hack with double mapping. First, the required number of bytes
288 * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more
289 * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address
290 * where the anonymous mapping starts. */
291 *length = sb.st_size + pagesize;
292 *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
293 *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0);
294 }
295 if (*addr == MAP_FAILED) {
296 LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno));
297 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200298 }
299
Radek Krejcif345c012018-09-19 11:12:59 +0200300 return LY_SUCCESS;
Radek Krejci86d106e2018-10-18 09:53:19 +0200301}
Michal Vasko841d1a92018-09-07 15:40:31 +0200302
Radek Krejci86d106e2018-10-18 09:53:19 +0200303LY_ERR
304ly_munmap(void *addr, size_t length)
305{
306 if (munmap(addr, length)) {
307 return LY_ESYS;
308 }
309 return LY_SUCCESS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200310}
Radek Krejci4f28eda2018-11-12 11:46:16 +0100311
312LY_ERR
Radek Krejci4546aa62019-07-15 16:53:32 +0200313ly_strcat(char **dest, const char *format, ...)
314{
315 va_list fp;
316 char *addition = NULL;
317 size_t len;
318
319 va_start(fp, format);
320 len = vasprintf(&addition, format, fp);
321 len += (*dest ? strlen(*dest) : 0) + 1;
322
323 if (*dest) {
324 *dest = ly_realloc(*dest, len);
325 if (!*dest) {
326 return LY_EMEM;
327 }
328 *dest = strcat(*dest, addition);
329 free(addition);
330 } else {
331 *dest = addition;
332 }
333
334 va_end(fp);
335 return LY_SUCCESS;
336}
337
338LY_ERR
Radek Krejci249973a2019-06-10 10:50:54 +0200339ly_parse_int(const char *val_str, size_t val_len, int64_t min, int64_t max, int base, int64_t *ret)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100340{
341 char *strptr;
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200342 int64_t i;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100343
Radek Krejci249973a2019-06-10 10:50:54 +0200344 LY_CHECK_ARG_RET(NULL, val_str, val_str[0], val_len, LY_EINVAL);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100345
346 /* convert to 64-bit integer, all the redundant characters are handled */
347 errno = 0;
348 strptr = NULL;
349
350 /* parse the value */
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200351 i = strtoll(val_str, &strptr, base);
Radek Krejci249973a2019-06-10 10:50:54 +0200352 if (errno || strptr == val_str) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100353 return LY_EVALID;
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200354 } else if ((i < min) || (i > max)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100355 return LY_EDENIED;
356 } else if (strptr && *strptr) {
357 while (isspace(*strptr)) {
358 ++strptr;
359 }
Radek Krejci249973a2019-06-10 10:50:54 +0200360 if (*strptr && strptr < val_str + val_len) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100361 return LY_EVALID;
362 }
363 }
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200364
365 *ret = i;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100366 return LY_SUCCESS;
367}
368
369LY_ERR
Radek Krejci249973a2019-06-10 10:50:54 +0200370ly_parse_uint(const char *val_str, size_t val_len, uint64_t max, int base, uint64_t *ret)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100371{
372 char *strptr;
373 uint64_t u;
374
375 LY_CHECK_ARG_RET(NULL, val_str, val_str[0], LY_EINVAL);
376
377 errno = 0;
378 strptr = NULL;
379 u = strtoull(val_str, &strptr, base);
Radek Krejci249973a2019-06-10 10:50:54 +0200380 if (errno || strptr == val_str) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100381 return LY_EVALID;
382 } else if ((u > max) || (u && val_str[0] == '-')) {
383 return LY_EDENIED;
384 } else if (strptr && *strptr) {
385 while (isspace(*strptr)) {
386 ++strptr;
387 }
Radek Krejci249973a2019-06-10 10:50:54 +0200388 if (*strptr && strptr < val_str + val_len) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100389 return LY_EVALID;
390 }
391 }
392
393 *ret = u;
394 return LY_SUCCESS;
395}
Radek Krejcib4a4a272019-06-10 12:44:52 +0200396
397/**
398 * @brief Parse an identifier.
399 *
400 * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l'))
401 * identifier = (ALPHA / "_")
402 * *(ALPHA / DIGIT / "_" / "-" / ".")
403 *
404 * @param[in,out] id Identifier to parse. When returned, it points to the first character which is not part of the identifier.
405 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid starting character.
406 */
407static LY_ERR
408lys_parse_id(const char **id)
409{
410 assert(id && *id);
411
412 if (!is_yangidentstartchar(**id)) {
413 return LY_EINVAL;
414 }
415 ++(*id);
416
417 while (is_yangidentchar(**id)) {
418 ++(*id);
419 }
420 return LY_SUCCESS;
421}
422
423LY_ERR
424ly_parse_nodeid(const char **id, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
425{
426 assert(id && *id);
427 assert(prefix && prefix_len);
428 assert(name && name_len);
429
430 *prefix = *id;
431 *prefix_len = 0;
432 *name = NULL;
433 *name_len = 0;
434
435 LY_CHECK_RET(lys_parse_id(id));
436 if (**id == ':') {
437 /* there is prefix */
438 *prefix_len = *id - *prefix;
439 ++(*id);
440 *name = *id;
441
442 LY_CHECK_RET(lys_parse_id(id));
443 *name_len = *id - *name;
444 } else {
445 /* there is no prefix, so what we have as prefix now is actually the name */
446 *name = *prefix;
447 *name_len = *id - *name;
448 *prefix = NULL;
449 }
450
451 return LY_SUCCESS;
452}
453
454LY_ERR
Radek Krejci084289f2019-07-09 17:35:30 +0200455ly_parse_instance_predicate(const char **pred, size_t limit, LYD_FORMAT format,
456 const char **prefix, size_t *prefix_len, const char **id, size_t *id_len, const char **value, size_t *value_len,
457 const char **errmsg)
Radek Krejcib4a4a272019-06-10 12:44:52 +0200458{
459 LY_ERR ret = LY_EVALID;
460 const char *in = *pred;
461 size_t offset = 1;
462 int expr = 0;
463 char quot;
464
465 assert(in[0] == '\[');
466
467 *prefix = *id = *value = NULL;
468 *prefix_len = *id_len = *value_len = 0;
469
470 /* leading *WSP */
471 for (; isspace(in[offset]); offset++);
472
473 if (isdigit(in[offset])) {
474 /* pos: "[" *WSP positive-integer-value *WSP "]" */
475 if (in[offset] == '0') {
476 /* zero */
477 *errmsg = "The position predicate cannot be zero.";
478 goto error;
479 }
480
481 /* positive-integer-value */
Radek Krejci10bfdf82019-06-10 14:08:13 +0200482 *value = &in[offset++];
Radek Krejcib4a4a272019-06-10 12:44:52 +0200483 for (; isdigit(in[offset]); offset++);
Radek Krejci10bfdf82019-06-10 14:08:13 +0200484 *value_len = &in[offset] - *value;
Radek Krejcib4a4a272019-06-10 12:44:52 +0200485
486 } else if (in[offset] == '.') {
487 /* leaf-list-predicate: "[" *WSP "." *WSP "=" *WSP quoted-string *WSP "]" */
488 *id = &in[offset];
489 *id_len = 1;
490 offset++;
491 expr = 1;
Radek Krejci10bfdf82019-06-10 14:08:13 +0200492 } else if (in[offset] == '-') {
493 /* typically negative value */
494 *errmsg = "Invalid instance predicate format (negative position or invalid node-identifier).";
495 goto error;
Radek Krejcib4a4a272019-06-10 12:44:52 +0200496 } else {
497 /* key-predicate: "[" *WSP node-identifier *WSP "=" *WSP quoted-string *WSP "]" */
498 in = &in[offset];
499 if (ly_parse_nodeid(&in, prefix, prefix_len, id, id_len)) {
500 *errmsg = "Invalid node-identifier.";
501 goto error;
502 }
Radek Krejci084289f2019-07-09 17:35:30 +0200503 if (format == LYD_XML && !(*prefix)) {
504 /* all node names MUST be qualified with explicit namespace prefix */
505 *errmsg = "Missing prefix of a node name.";
506 goto error;
507 }
Radek Krejcib4a4a272019-06-10 12:44:52 +0200508 offset = in - *pred;
509 in = *pred;
Radek Krejci10bfdf82019-06-10 14:08:13 +0200510 expr = 2;
Radek Krejcib4a4a272019-06-10 12:44:52 +0200511 }
512
513 if (expr) {
514 /* *WSP "=" *WSP quoted-string *WSP "]" */
515 for (; isspace(in[offset]); offset++);
516
517 if (in[offset] != '=') {
Radek Krejci10bfdf82019-06-10 14:08:13 +0200518 if (expr == 1) {
519 *errmsg = "Unexpected character instead of \'=\' in leaf-list-predicate.";
520 } else { /* 2 */
521 *errmsg = "Unexpected character instead of \'=\' in key-predicate.";
522 }
Radek Krejcib4a4a272019-06-10 12:44:52 +0200523 goto error;
524 }
525 offset++;
526 for (; isspace(in[offset]); offset++);
527
528 /* quoted-string */
529 quot = in[offset++];
530 if (quot != '\'' && quot != '\"') {
531 *errmsg = "String value is not quoted.";
532 goto error;
533 }
534 *value = &in[offset];
Radek Krejci084289f2019-07-09 17:35:30 +0200535 for (;offset < limit && (in[offset] != quot || (offset && in[offset - 1] == '\\')); offset++);
Radek Krejci10bfdf82019-06-10 14:08:13 +0200536 if (in[offset] == quot) {
537 *value_len = &in[offset] - *value;
538 offset++;
539 } else {
540 *errmsg = "Value is not terminated quoted-string.";
541 goto error;
542 }
Radek Krejcib4a4a272019-06-10 12:44:52 +0200543 }
544
545 /* *WSP "]" */
546 for(; isspace(in[offset]); offset++);
547 if (in[offset] != ']') {
Radek Krejci10bfdf82019-06-10 14:08:13 +0200548 if (expr == 0) {
549 *errmsg = "Predicate (pos) is not terminated by \']\' character.";
550 } else if (expr == 1) {
551 *errmsg = "Predicate (leaf-list-predicate) is not terminated by \']\' character.";
552 } else { /* 2 */
553 *errmsg = "Predicate (key-predicate) is not terminated by \']\' character.";
554 }
Radek Krejcib4a4a272019-06-10 12:44:52 +0200555 goto error;
556 }
Radek Krejci10bfdf82019-06-10 14:08:13 +0200557 offset++;
Radek Krejcib4a4a272019-06-10 12:44:52 +0200558
Radek Krejci10bfdf82019-06-10 14:08:13 +0200559 if (offset <= limit) {
560 *pred = &in[offset];
Radek Krejcib4a4a272019-06-10 12:44:52 +0200561 return LY_SUCCESS;
562 }
563
564 /* we read after the limit */
565 *errmsg = "Predicate is incomplete.";
566 *prefix = *id = *value = NULL;
567 *prefix_len = *id_len = *value_len = 0;
568 offset = limit;
569 ret = LY_EINVAL;
570
571error:
572 *pred = &in[offset];
573 return ret;
574}