Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema_helpers.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4 | * @brief Parsing and validation helper functions for schema trees |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 - 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 Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 14 | |
| 15 | #define _GNU_SOURCE |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 20 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include <string.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 22 | #include <time.h> |
| 23 | |
Michal Vasko | c5a2283 | 2020-08-20 13:21:33 +0200 | [diff] [blame] | 24 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "common.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | #include "hash_table.h" |
| 28 | #include "log.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 29 | #include "parser.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 30 | #include "parser_schema.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 31 | #include "parser_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 32 | #include "set.h" |
| 33 | #include "tree.h" |
| 34 | #include "tree_schema.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 35 | #include "tree_schema_internal.h" |
| 36 | |
Radek Krejci | 8574795 | 2019-06-07 16:43:43 +0200 | [diff] [blame] | 37 | LY_ERR |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 38 | lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *context_node, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 39 | const struct lys_module *context_module, uint16_t nodetype, uint8_t implement, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 40 | const struct lysc_node **target, uint16_t *result_flag) |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 41 | { |
| 42 | LY_ERR ret = LY_EVALID; |
| 43 | const char *name, *prefix, *id; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 44 | size_t name_len, prefix_len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 45 | const struct lys_module *mod; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 46 | const char *nodeid_type; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 47 | uint32_t getnext_extra_flag = 0; |
| 48 | uint16_t current_nodetype = 0; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 49 | |
| 50 | assert(nodeid); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 51 | assert(target); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 52 | assert(result_flag); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 53 | *target = NULL; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 54 | *result_flag = 0; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 55 | |
| 56 | id = nodeid; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 57 | |
| 58 | if (context_node) { |
| 59 | /* descendant-schema-nodeid */ |
| 60 | nodeid_type = "descendant"; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 61 | |
| 62 | if (*id == '/') { |
| 63 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 64 | "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.", |
| 65 | nodeid_len ? nodeid_len : strlen(nodeid), nodeid); |
| 66 | return LY_EVALID; |
| 67 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 68 | } else { |
| 69 | /* absolute-schema-nodeid */ |
| 70 | nodeid_type = "absolute"; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 71 | |
| 72 | if (*id != '/') { |
| 73 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 74 | "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".", |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 75 | nodeid_len ? nodeid_len : strlen(nodeid), nodeid); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 76 | return LY_EVALID; |
| 77 | } |
| 78 | ++id; |
| 79 | } |
| 80 | |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 81 | while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 82 | if (prefix) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 83 | mod = lys_module_find_prefix(context_module, prefix, prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 84 | if (!mod) { |
| 85 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 86 | "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
| 87 | nodeid_type, id - nodeid, nodeid, prefix_len, prefix, context_module->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 88 | return LY_ENOTFOUND; |
| 89 | } |
| 90 | } else { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 91 | mod = context_module; |
| 92 | } |
| 93 | if (implement && !mod->implemented) { |
| 94 | /* make the module implemented */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 95 | ret = lys_set_implemented_internal((struct lys_module *)mod, ctx->ctx->module_set_id); |
Michal Vasko | 294b76d | 2019-12-16 10:37:54 +0100 | [diff] [blame] | 96 | LY_CHECK_RET(ret); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 97 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 98 | if (context_node && (context_node->nodetype & (LYS_RPC | LYS_ACTION))) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 99 | /* move through input/output manually */ |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 100 | if (!ly_strncmp("input", name, name_len)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 101 | (*result_flag) |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 102 | } else if (!ly_strncmp("output", name, name_len)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 103 | (*result_flag) |= LYSC_OPT_RPC_OUTPUT; |
| 104 | getnext_extra_flag = LYS_GETNEXT_OUTPUT; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 105 | } else { |
| 106 | goto getnext; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 107 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 108 | current_nodetype = LYS_INOUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 109 | } else { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 110 | getnext: |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 111 | context_node = lys_find_child(context_node, mod, name, name_len, 0, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 112 | getnext_extra_flag | LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE); |
| 113 | if (!context_node) { |
| 114 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 115 | "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid); |
| 116 | return LY_ENOTFOUND; |
| 117 | } |
| 118 | getnext_extra_flag = 0; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 119 | current_nodetype = context_node->nodetype; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 120 | |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 121 | if (current_nodetype == LYS_NOTIF) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 122 | (*result_flag) |= LYSC_OPT_NOTIFICATION; |
| 123 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 124 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 125 | if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 126 | break; |
| 127 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 128 | if (*id != '/') { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 129 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 130 | "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.", |
| 131 | nodeid_type, id - nodeid + 1, nodeid); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 132 | return LY_EVALID; |
| 133 | } |
| 134 | ++id; |
| 135 | } |
| 136 | |
| 137 | if (ret == LY_SUCCESS) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 138 | *target = context_node; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 139 | if (nodetype & LYS_INOUT) { |
| 140 | /* instead of input/output nodes, the RPC/action node is actually returned */ |
| 141 | } |
| 142 | if (nodetype && !(current_nodetype & nodetype)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 143 | return LY_EDENIED; |
| 144 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 145 | } else { |
| 146 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 147 | "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.", |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 148 | nodeid_type, nodeid_len ? nodeid_len : strlen(nodeid), nodeid); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 155 | lysp_check_prefix(struct lys_parser_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 156 | { |
| 157 | struct lysp_import *i; |
| 158 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 159 | if (module_prefix && &module_prefix != value && !strcmp(module_prefix, *value)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 160 | LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 161 | return LY_EEXIST; |
| 162 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 163 | LY_ARRAY_FOR(imports, struct lysp_import, i) { |
| 164 | if (i->prefix && &i->prefix != value && !strcmp(i->prefix, *value)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 165 | LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 166 | return LY_EEXIST; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | return LY_SUCCESS; |
| 170 | } |
| 171 | |
| 172 | LY_ERR |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 173 | lysc_check_status(struct lysc_ctx *ctx, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 174 | uint16_t flags1, void *mod1, const char *name1, |
| 175 | uint16_t flags2, void *mod2, const char *name2) |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 176 | { |
| 177 | uint16_t flg1, flg2; |
| 178 | |
| 179 | flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR; |
| 180 | flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR; |
| 181 | |
| 182 | if ((flg1 < flg2) && (mod1 == mod2)) { |
| 183 | if (ctx) { |
| 184 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 185 | "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".", |
| 186 | flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1, |
| 187 | flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2); |
| 188 | } |
| 189 | return LY_EVALID; |
| 190 | } |
| 191 | |
| 192 | return LY_SUCCESS; |
| 193 | } |
| 194 | |
| 195 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 196 | lysp_check_date(struct lys_parser_ctx *ctx, const char *date, uint8_t date_len, const char *stmt) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 197 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 198 | struct tm tm, tm_; |
| 199 | char *r; |
| 200 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 201 | LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL); |
| 202 | LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 203 | |
| 204 | /* check format */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 205 | for (uint8_t i = 0; i < date_len; i++) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 206 | if (i == 4 || i == 7) { |
| 207 | if (date[i] != '-') { |
| 208 | goto error; |
| 209 | } |
| 210 | } else if (!isdigit(date[i])) { |
| 211 | goto error; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /* check content, e.g. 2018-02-31 */ |
| 216 | memset(&tm, 0, sizeof tm); |
| 217 | r = strptime(date, "%Y-%m-%d", &tm); |
| 218 | if (!r || r != &date[LY_REV_SIZE - 1]) { |
| 219 | goto error; |
| 220 | } |
| 221 | memcpy(&tm_, &tm, sizeof tm); |
| 222 | mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */ |
| 223 | if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */ |
| 224 | /* checking days is enough, since other errors |
| 225 | * have been checked by strptime() */ |
| 226 | goto error; |
| 227 | } |
| 228 | |
| 229 | return LY_SUCCESS; |
| 230 | |
| 231 | error: |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 232 | if (stmt) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 233 | if (ctx) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 234 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 235 | } else { |
| 236 | LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt); |
| 237 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 238 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 239 | return LY_EINVAL; |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | lysp_sort_revisions(struct lysp_revision *revs) |
| 244 | { |
| 245 | uint8_t i, r; |
| 246 | struct lysp_revision rev; |
| 247 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 248 | for (i = 1, r = 0; revs && i < LY_ARRAY_COUNT(revs); i++) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 249 | if (strcmp(revs[i].date, revs[r].date) > 0) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 250 | r = i; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (r) { |
| 255 | /* the newest revision is not on position 0, switch them */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 256 | memcpy(&rev, &revs[0], sizeof rev); |
| 257 | memcpy(&revs[0], &revs[r], sizeof rev); |
| 258 | memcpy(&revs[r], &rev, sizeof rev); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 259 | } |
| 260 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 261 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 262 | static const struct lysp_tpdf * |
| 263 | lysp_type_match(const char *name, struct lysp_node *node) |
| 264 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 265 | const struct lysp_tpdf *typedefs; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 266 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 267 | |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 268 | typedefs = lysp_node_typedefs(node); |
| 269 | LY_ARRAY_FOR(typedefs, u) { |
| 270 | if (!strcmp(name, typedefs[u].name)) { |
| 271 | /* match */ |
| 272 | return &typedefs[u]; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | return NULL; |
| 277 | } |
| 278 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 279 | static LY_DATA_TYPE |
| 280 | lysp_type_str2builtin(const char *name, size_t len) |
| 281 | { |
| 282 | if (len >= 4) { /* otherwise it does not match any built-in type */ |
| 283 | if (name[0] == 'b') { |
| 284 | if (name[1] == 'i') { |
| 285 | if (len == 6 && !strncmp(&name[2], "nary", 4)) { |
| 286 | return LY_TYPE_BINARY; |
| 287 | } else if (len == 4 && !strncmp(&name[2], "ts", 2)) { |
| 288 | return LY_TYPE_BITS; |
| 289 | } |
| 290 | } else if (len == 7 && !strncmp(&name[1], "oolean", 6)) { |
| 291 | return LY_TYPE_BOOL; |
| 292 | } |
| 293 | } else if (name[0] == 'd') { |
| 294 | if (len == 9 && !strncmp(&name[1], "ecimal64", 8)) { |
| 295 | return LY_TYPE_DEC64; |
| 296 | } |
| 297 | } else if (name[0] == 'e') { |
| 298 | if (len == 5 && !strncmp(&name[1], "mpty", 4)) { |
| 299 | return LY_TYPE_EMPTY; |
| 300 | } else if (len == 11 && !strncmp(&name[1], "numeration", 10)) { |
| 301 | return LY_TYPE_ENUM; |
| 302 | } |
| 303 | } else if (name[0] == 'i') { |
| 304 | if (name[1] == 'n') { |
| 305 | if (len == 4 && !strncmp(&name[2], "t8", 2)) { |
| 306 | return LY_TYPE_INT8; |
| 307 | } else if (len == 5) { |
| 308 | if (!strncmp(&name[2], "t16", 3)) { |
| 309 | return LY_TYPE_INT16; |
| 310 | } else if (!strncmp(&name[2], "t32", 3)) { |
| 311 | return LY_TYPE_INT32; |
| 312 | } else if (!strncmp(&name[2], "t64", 3)) { |
| 313 | return LY_TYPE_INT64; |
| 314 | } |
| 315 | } else if (len == 19 && !strncmp(&name[2], "stance-identifier", 17)) { |
| 316 | return LY_TYPE_INST; |
| 317 | } |
| 318 | } else if (len == 11 && !strncmp(&name[1], "dentityref", 10)) { |
| 319 | return LY_TYPE_IDENT; |
| 320 | } |
| 321 | } else if (name[0] == 'l') { |
| 322 | if (len == 7 && !strncmp(&name[1], "eafref", 6)) { |
| 323 | return LY_TYPE_LEAFREF; |
| 324 | } |
| 325 | } else if (name[0] == 's') { |
| 326 | if (len == 6 && !strncmp(&name[1], "tring", 5)) { |
| 327 | return LY_TYPE_STRING; |
| 328 | } |
| 329 | } else if (name[0] == 'u') { |
| 330 | if (name[1] == 'n') { |
| 331 | if (len == 5 && !strncmp(&name[2], "ion", 3)) { |
| 332 | return LY_TYPE_UNION; |
| 333 | } |
| 334 | } else if (name[1] == 'i' && name[2] == 'n' && name[3] == 't') { |
| 335 | if (len == 5 && name[4] == '8') { |
| 336 | return LY_TYPE_UINT8; |
| 337 | } else if (len == 6) { |
| 338 | if (!strncmp(&name[4], "16", 2)) { |
| 339 | return LY_TYPE_UINT16; |
| 340 | } else if (!strncmp(&name[4], "32", 2)) { |
| 341 | return LY_TYPE_UINT32; |
| 342 | } else if (!strncmp(&name[4], "64", 2)) { |
| 343 | return LY_TYPE_UINT64; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return LY_TYPE_UNKNOWN; |
| 351 | } |
| 352 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 353 | LY_ERR |
| 354 | lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 355 | LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 356 | { |
| 357 | const char *str, *name; |
| 358 | struct lysp_tpdf *typedefs; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 359 | struct lys_module *mod; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 360 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 361 | |
| 362 | assert(id); |
| 363 | assert(start_module); |
| 364 | assert(tpdf); |
| 365 | assert(node); |
| 366 | assert(module); |
| 367 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 368 | *node = NULL; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 369 | str = strchr(id, ':'); |
| 370 | if (str) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 371 | mod = lys_module_find_prefix(start_module->mod, id, str - id); |
| 372 | *module = mod ? mod->parsed : NULL; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 373 | name = str + 1; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 374 | *type = LY_TYPE_UNKNOWN; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 375 | } else { |
| 376 | *module = start_module; |
| 377 | name = id; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 378 | |
| 379 | /* check for built-in types */ |
| 380 | *type = lysp_type_str2builtin(name, strlen(name)); |
| 381 | if (*type) { |
| 382 | *tpdf = NULL; |
| 383 | return LY_SUCCESS; |
| 384 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 385 | } |
| 386 | LY_CHECK_RET(!(*module), LY_ENOTFOUND); |
| 387 | |
| 388 | if (start_node && *module == start_module) { |
| 389 | /* search typedefs in parent's nodes */ |
| 390 | *node = start_node; |
| 391 | while (*node) { |
| 392 | *tpdf = lysp_type_match(name, *node); |
| 393 | if (*tpdf) { |
| 394 | /* match */ |
| 395 | return LY_SUCCESS; |
| 396 | } |
| 397 | *node = (*node)->parent; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* search in top-level typedefs */ |
| 402 | if ((*module)->typedefs) { |
| 403 | LY_ARRAY_FOR((*module)->typedefs, u) { |
| 404 | if (!strcmp(name, (*module)->typedefs[u].name)) { |
| 405 | /* match */ |
| 406 | *tpdf = &(*module)->typedefs[u]; |
| 407 | return LY_SUCCESS; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* search in submodules' typedefs */ |
| 413 | LY_ARRAY_FOR((*module)->includes, u) { |
| 414 | typedefs = (*module)->includes[u].submodule->typedefs; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 415 | LY_ARRAY_FOR(typedefs, v) { |
| 416 | if (!strcmp(name, typedefs[v].name)) { |
| 417 | /* match */ |
| 418 | *tpdf = &typedefs[v]; |
| 419 | return LY_SUCCESS; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return LY_ENOTFOUND; |
| 425 | } |
| 426 | |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 427 | LY_ERR |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 428 | lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len) |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 429 | { |
| 430 | if (!name_len) { |
| 431 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length."); |
| 432 | return LY_EVALID; |
| 433 | } else if (isspace(name[0]) || isspace(name[name_len - 1])) { |
| 434 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").", |
| 435 | name_len, name); |
| 436 | return LY_EVALID; |
| 437 | } else { |
| 438 | for (size_t u = 0; u < name_len; ++u) { |
| 439 | if (iscntrl(name[u])) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 440 | LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).", |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 441 | name_len, name, u + 1); |
| 442 | break; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | return LY_SUCCESS; |
| 448 | } |
| 449 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 450 | /** |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 451 | * @brief Check name of a new type to avoid name collisions. |
| 452 | * |
| 453 | * @param[in] ctx Parser context, module where the type is being defined is taken from here. |
| 454 | * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef. |
| 455 | * @param[in] tpdf Typedef definition to check. |
| 456 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
| 457 | * typedefs are checked, caller is supposed to free the table. |
| 458 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
| 459 | * typedefs are checked, caller is supposed to free the table. |
| 460 | * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise. |
| 461 | */ |
| 462 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 463 | lysp_check_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 464 | struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 465 | { |
| 466 | struct lysp_node *parent; |
| 467 | uint32_t hash; |
| 468 | size_t name_len; |
| 469 | const char *name; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 470 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 471 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 472 | |
| 473 | assert(ctx); |
| 474 | assert(tpdf); |
| 475 | |
| 476 | name = tpdf->name; |
| 477 | name_len = strlen(name); |
| 478 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 479 | if (lysp_type_str2builtin(name, name_len)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 480 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with a built-in type.", name); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 481 | return LY_EEXIST; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* check locally scoped typedefs (avoid name shadowing) */ |
| 485 | if (node) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 486 | typedefs = lysp_node_typedefs(node); |
| 487 | LY_ARRAY_FOR(typedefs, u) { |
| 488 | if (&typedefs[u] == tpdf) { |
| 489 | break; |
| 490 | } |
| 491 | if (!strcmp(name, typedefs[u].name)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 492 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name); |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 493 | return LY_EEXIST; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | /* search typedefs in parent's nodes */ |
Radek Krejci | 87e78ca | 2019-05-02 09:51:29 +0200 | [diff] [blame] | 497 | for (parent = node->parent; parent; parent = parent->parent) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 498 | if (lysp_type_match(name, parent)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 499 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with another scoped type.", name); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 500 | return LY_EEXIST; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /* check collision with the top-level typedefs */ |
| 506 | hash = dict_hash(name, name_len); |
| 507 | if (node) { |
| 508 | lyht_insert(tpdfs_scoped, &name, hash, NULL); |
| 509 | if (!lyht_find(tpdfs_global, &name, hash, NULL)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 510 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 511 | return LY_EEXIST; |
| 512 | } |
| 513 | } else { |
| 514 | if (lyht_insert(tpdfs_global, &name, hash, NULL)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 515 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with another top-level type.", name); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 516 | return LY_EEXIST; |
| 517 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 518 | /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the |
| 519 | * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision |
| 520 | * is detected in the first branch few lines above */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | return LY_SUCCESS; |
| 524 | } |
| 525 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 526 | static uint8_t |
| 527 | lysp_id_cmp(void *val1, void *val2, uint8_t UNUSED(mod), void *UNUSED(cb_data)) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 528 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 529 | return strcmp(val1, val2) == 0 ? 1 : 0; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | LY_ERR |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 533 | lysp_parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 534 | struct lysp_action *actions, struct lysp_notif *notifs) |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 535 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 536 | LY_ARRAY_COUNT_TYPE u, v; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 537 | struct lysp_node *child; |
| 538 | |
| 539 | /* finalize parent pointers to the reallocated items */ |
| 540 | |
| 541 | /* gropings */ |
| 542 | LY_ARRAY_FOR(groupings, u) { |
| 543 | LY_LIST_FOR(groupings[u].data, child) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 544 | child->parent = (struct lysp_node *)&groupings[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 545 | } |
| 546 | LY_ARRAY_FOR(groupings[u].actions, v) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 547 | groupings[u].actions[v].parent = (struct lysp_node *)&groupings[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 548 | } |
| 549 | LY_ARRAY_FOR(groupings[u].notifs, v) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 550 | groupings[u].notifs[v].parent = (struct lysp_node *)&groupings[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 551 | } |
| 552 | LY_ARRAY_FOR(groupings[u].groupings, v) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 553 | groupings[u].groupings[v].parent = (struct lysp_node *)&groupings[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 554 | } |
| 555 | if (groupings[u].typedefs) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame^] | 556 | LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0, NULL)); |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
| 560 | /* augments */ |
| 561 | LY_ARRAY_FOR(augments, u) { |
| 562 | LY_LIST_FOR(augments[u].child, child) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 563 | child->parent = (struct lysp_node *)&augments[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 564 | } |
| 565 | LY_ARRAY_FOR(augments[u].actions, v) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 566 | augments[u].actions[v].parent = (struct lysp_node *)&augments[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 567 | } |
| 568 | LY_ARRAY_FOR(augments[u].notifs, v) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 569 | augments[u].notifs[v].parent = (struct lysp_node *)&augments[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | /* actions */ |
| 574 | LY_ARRAY_FOR(actions, u) { |
| 575 | if (actions[u].input.parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 576 | actions[u].input.parent = (struct lysp_node *)&actions[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 577 | LY_LIST_FOR(actions[u].input.data, child) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 578 | child->parent = (struct lysp_node *)&actions[u].input; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 579 | } |
| 580 | LY_ARRAY_FOR(actions[u].input.groupings, v) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 581 | actions[u].input.groupings[v].parent = (struct lysp_node *)&actions[u].input; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 582 | } |
| 583 | if (actions[u].input.typedefs) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame^] | 584 | LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0, NULL)); |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | if (actions[u].output.parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 588 | actions[u].output.parent = (struct lysp_node *)&actions[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 589 | LY_LIST_FOR(actions[u].output.data, child) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 590 | child->parent = (struct lysp_node *)&actions[u].output; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 591 | } |
| 592 | LY_ARRAY_FOR(actions[u].output.groupings, v) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 593 | actions[u].output.groupings[v].parent = (struct lysp_node *)&actions[u].output; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 594 | } |
| 595 | if (actions[u].output.typedefs) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame^] | 596 | LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0, NULL)); |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | LY_ARRAY_FOR(actions[u].groupings, v) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 600 | actions[u].groupings[v].parent = (struct lysp_node *)&actions[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 601 | } |
| 602 | if (actions[u].typedefs) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame^] | 603 | LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0, NULL)); |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | /* notifications */ |
| 608 | LY_ARRAY_FOR(notifs, u) { |
| 609 | LY_LIST_FOR(notifs[u].data, child) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 610 | child->parent = (struct lysp_node *)¬ifs[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 611 | } |
| 612 | LY_ARRAY_FOR(notifs[u].groupings, v) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 613 | notifs[u].groupings[v].parent = (struct lysp_node *)¬ifs[u]; |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 614 | } |
| 615 | if (notifs[u].typedefs) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame^] | 616 | LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, ¬ifs[u], 0, NULL)); |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
| 620 | return LY_SUCCESS; |
| 621 | } |
| 622 | |
| 623 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 624 | lysp_check_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 625 | { |
| 626 | struct hash_table *ids_global; |
| 627 | struct hash_table *ids_scoped; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 628 | const struct lysp_tpdf *typedefs; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 629 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 630 | uint32_t i; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 631 | LY_ERR ret = LY_EVALID; |
| 632 | |
| 633 | /* check name collisions - typedefs and groupings */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 634 | ids_global = lyht_new(8, sizeof(char *), lysp_id_cmp, NULL, 1); |
| 635 | ids_scoped = lyht_new(8, sizeof(char *), lysp_id_cmp, NULL, 1); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 636 | LY_ARRAY_FOR(mod->typedefs, v) { |
| 637 | if (lysp_check_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 638 | goto cleanup; |
| 639 | } |
| 640 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 641 | LY_ARRAY_FOR(mod->includes, v) { |
| 642 | LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) { |
| 643 | if (lysp_check_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped)) { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 644 | goto cleanup; |
| 645 | } |
| 646 | } |
| 647 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 648 | for (i = 0; i < ctx->tpdfs_nodes.count; ++i) { |
| 649 | typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]); |
| 650 | LY_ARRAY_FOR(typedefs, u) { |
| 651 | if (lysp_check_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global, ids_scoped)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 652 | goto cleanup; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | ret = LY_SUCCESS; |
| 657 | cleanup: |
| 658 | lyht_free(ids_global); |
| 659 | lyht_free(ids_scoped); |
| 660 | ly_set_erase(&ctx->tpdfs_nodes, NULL); |
| 661 | |
| 662 | return ret; |
| 663 | } |
| 664 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 665 | struct lysp_load_module_check_data { |
| 666 | const char *name; |
| 667 | const char *revision; |
| 668 | const char *path; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 669 | const char *submoduleof; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 670 | }; |
| 671 | |
| 672 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 673 | lysp_load_module_check(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data) |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 674 | { |
| 675 | struct lysp_load_module_check_data *info = data; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 676 | const char *filename, *dot, *rev, *name; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 677 | uint8_t latest_revision; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 678 | size_t len; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 679 | struct lysp_revision *revs; |
| 680 | |
| 681 | name = mod ? mod->mod->name : submod->name; |
| 682 | revs = mod ? mod->revs : submod->revs; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 683 | latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 684 | |
| 685 | if (info->name) { |
| 686 | /* check name of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 687 | if (strcmp(info->name, name)) { |
| 688 | LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 689 | return LY_EINVAL; |
| 690 | } |
| 691 | } |
| 692 | if (info->revision) { |
| 693 | /* check revision of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 694 | if (!revs || strcmp(info->revision, revs[0].date)) { |
| 695 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name, |
Radek Krejci | b07b5c9 | 2019-04-08 10:56:37 +0200 | [diff] [blame] | 696 | revs ? revs[0].date : "none", info->revision); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 697 | return LY_EINVAL; |
| 698 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 699 | } else if (!latest_revision) { |
| 700 | /* do not log, we just need to drop the schema and use the latest revision from the context */ |
| 701 | return LY_EEXIST; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 702 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 703 | if (submod) { |
| 704 | assert(info->submoduleof); |
| 705 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 706 | /* check that the submodule belongs-to our module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 707 | if (strcmp(info->submoduleof, submod->belongsto)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 708 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 709 | submod->name, info->submoduleof, submod->belongsto); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 710 | return LY_EVALID; |
| 711 | } |
| 712 | /* check circular dependency */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 713 | if (submod->parsing) { |
| 714 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 715 | return LY_EVALID; |
| 716 | } |
| 717 | } |
| 718 | if (info->path) { |
| 719 | /* check that name and revision match filename */ |
| 720 | filename = strrchr(info->path, '/'); |
| 721 | if (!filename) { |
| 722 | filename = info->path; |
| 723 | } else { |
| 724 | filename++; |
| 725 | } |
| 726 | /* name */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 727 | len = strlen(name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 728 | rev = strchr(filename, '@'); |
| 729 | dot = strrchr(info->path, '.'); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 730 | if (strncmp(filename, name, len) || |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 731 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 732 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 733 | } |
| 734 | /* revision */ |
| 735 | if (rev) { |
| 736 | len = dot - ++rev; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 737 | if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 738 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 739 | revs ? revs[0].date : "none"); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | } |
| 743 | return LY_SUCCESS; |
| 744 | } |
| 745 | |
| 746 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 747 | lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, uint8_t implement, |
| 748 | struct lys_parser_ctx *main_ctx, const char *main_name, uint8_t required, void **result) |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 749 | { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 750 | struct ly_in *in; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 751 | char *filepath = NULL; |
| 752 | LYS_INFORMAT format; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 753 | void *mod = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 754 | LY_ERR ret = LY_SUCCESS; |
| 755 | struct lysp_load_module_check_data check_data = {0}; |
| 756 | |
| 757 | LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision, |
| 758 | &filepath, &format)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 759 | if (!filepath) { |
| 760 | if (required) { |
| 761 | LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "", |
| 762 | revision ? revision : ""); |
| 763 | } |
| 764 | return LY_ENOTFOUND; |
| 765 | } |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 766 | |
| 767 | LOGVRB("Loading schema from \"%s\" file.", filepath); |
| 768 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 769 | /* get the (sub)module */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 770 | LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in), |
| 771 | LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 772 | check_data.name = name; |
| 773 | check_data.revision = revision; |
| 774 | check_data.path = filepath; |
fredgan | cd485b8 | 2019-10-18 15:00:17 +0800 | [diff] [blame] | 775 | check_data.submoduleof = main_name; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 776 | if (main_ctx) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 777 | ret = lys_parse_mem_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 778 | (struct lysp_submodule **)&mod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 779 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 780 | ret = lys_parse_mem_module(ctx, in, format, implement, lysp_load_module_check, &check_data, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 781 | (struct lys_module **)&mod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 782 | |
| 783 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 784 | LY_CHECK_ERR_GOTO(ret, ly_in_free(in, 1), cleanup); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 785 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 786 | if (main_ctx) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 787 | lys_parser_fill_filepath(ctx, in, &((struct lysp_submodule *)mod)->filepath); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 788 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 789 | lys_parser_fill_filepath(ctx, in, &((struct lys_module *)mod)->filepath); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 790 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 791 | ly_in_free(in, 1); |
| 792 | |
| 793 | if (mod && implement) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 794 | lys_compile((struct lys_module **)&mod, 0); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | *result = mod; |
| 798 | |
| 799 | /* success */ |
| 800 | cleanup: |
| 801 | free(filepath); |
| 802 | return ret; |
| 803 | } |
| 804 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 805 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 806 | lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, uint8_t implement, uint8_t require_parsed, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 807 | struct lys_module **mod) |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 808 | { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 809 | const char *module_data = NULL; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 810 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 811 | void (*module_data_free)(void *module_data, void *user_data) = NULL; |
| 812 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 813 | struct lys_module *m = NULL; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 814 | struct ly_in *in; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 815 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 816 | assert(mod); |
| 817 | |
Radek Krejci | a53d7c9 | 2020-08-21 11:30:56 +0200 | [diff] [blame] | 818 | if (ctx->flags & LY_CTX_ALLIMPLEMENTED) { |
| 819 | implement = 1; |
| 820 | } |
| 821 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 822 | if (!*mod) { |
| 823 | /* try to get the module from the context */ |
| 824 | if (revision) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 825 | /* get the specific revision */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 826 | *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 827 | } else if (implement) { |
| 828 | /* prefer the implemented module instead of the latest one */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 829 | *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 830 | if (!*mod) { |
| 831 | /* there is no implemented module in the context, try to get the latest revision module */ |
| 832 | goto latest_in_the_context; |
| 833 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 834 | } else { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 835 | /* get the requested module of the latest revision in the context */ |
| 836 | latest_in_the_context: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 837 | *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 838 | if (*mod && (*mod)->latest_revision == 1) { |
| 839 | /* let us now search with callback and searchpaths to check if there is newer revision outside the context */ |
| 840 | m = *mod; |
| 841 | *mod = NULL; |
| 842 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 843 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 846 | if (!(*mod) || (require_parsed && !(*mod)->parsed)) { |
| 847 | (*mod) = NULL; |
| 848 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 849 | /* check collision with other implemented revision */ |
| 850 | if (implement && ly_ctx_get_module_implemented(ctx, name)) { |
| 851 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 852 | "Module \"%s\" is already present in other implemented revision.", name); |
| 853 | return LY_EDENIED; |
| 854 | } |
| 855 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 856 | /* module not present in the context, get the input data and parse it */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 857 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 858 | search_clb: |
| 859 | if (ctx->imp_clb) { |
| 860 | if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 861 | &format, &module_data, &module_data_free) == LY_SUCCESS) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 862 | LY_CHECK_RET(ly_in_new_memory(module_data, &in)); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 863 | check_data.name = name; |
| 864 | check_data.revision = revision; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 865 | lys_parse_mem_module(ctx, in, format, implement, lysp_load_module_check, &check_data, mod); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 866 | ly_in_free(in, 0); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 867 | if (module_data_free) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 868 | module_data_free((void *)module_data, ctx->imp_clb_data); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 869 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 870 | if (*mod && implement) { |
| 871 | lys_compile(mod, 0); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 872 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 876 | goto search_file; |
| 877 | } |
| 878 | } else { |
| 879 | search_file: |
| 880 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
| 881 | /* module was not received from the callback or there is no callback set */ |
Radek Krejci | 78f0682 | 2019-10-30 12:54:05 +0100 | [diff] [blame] | 882 | lys_module_localfile(ctx, name, revision, implement, NULL, NULL, m ? 0 : 1, (void **)mod); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 883 | } |
| 884 | if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 885 | goto search_clb; |
| 886 | } |
| 887 | } |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 888 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 889 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 890 | * consider that even the callback provides correct latest revision */ |
| 891 | if (!(*mod) && m) { |
Radek Krejci | 78f0682 | 2019-10-30 12:54:05 +0100 | [diff] [blame] | 892 | LOGVRB("Newer revision than %s-%s not found, using this as the latest revision.", m->name, m->revision); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 893 | m->latest_revision = 2; |
| 894 | *mod = m; |
| 895 | } else if ((*mod) && !revision && ((*mod)->latest_revision == 1)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 896 | (*mod)->latest_revision = 2; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 897 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 898 | } else { |
| 899 | /* we have module from the current context */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 900 | if (implement) { |
| 901 | m = ly_ctx_get_module_implemented(ctx, name); |
| 902 | if (m && m != *mod) { |
| 903 | /* check collision with other implemented revision */ |
| 904 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 905 | "Module \"%s\" is already present in other implemented revision.", name); |
| 906 | *mod = NULL; |
| 907 | return LY_EDENIED; |
| 908 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /* circular check */ |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 912 | if ((*mod)->parsed && (*mod)->parsed->parsing) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 913 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name); |
| 914 | *mod = NULL; |
| 915 | return LY_EVALID; |
| 916 | } |
| 917 | } |
| 918 | if (!(*mod)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 919 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 920 | return LY_EVALID; |
| 921 | } |
| 922 | |
| 923 | if (implement) { |
| 924 | /* mark the module implemented, check for collision was already done */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 925 | (*mod)->implemented = 1; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 926 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 927 | |
| 928 | return LY_SUCCESS; |
| 929 | } |
| 930 | |
| 931 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 932 | lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c) |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 933 | { |
| 934 | if (!is_yangutf8char(c)) { |
| 935 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c); |
| 936 | return LY_EVALID; |
| 937 | } |
| 938 | return LY_SUCCESS; |
| 939 | } |
| 940 | |
| 941 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 942 | lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, uint8_t first, uint8_t *prefix) |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 943 | { |
| 944 | if (first || (prefix && (*prefix) == 1)) { |
| 945 | if (!is_yangidentstartchar(c)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 946 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c); |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 947 | return LY_EVALID; |
| 948 | } |
| 949 | if (prefix) { |
| 950 | if (first) { |
| 951 | (*prefix) = 0; |
| 952 | } else { |
| 953 | (*prefix) = 2; |
| 954 | } |
| 955 | } |
| 956 | } else if (c == ':' && prefix && (*prefix) == 0) { |
| 957 | (*prefix) = 1; |
| 958 | } else if (!is_yangidentchar(c)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 959 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c); |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 960 | return LY_EVALID; |
| 961 | } |
| 962 | |
| 963 | return LY_SUCCESS; |
| 964 | } |
| 965 | |
| 966 | LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 967 | lysp_load_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 968 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 969 | struct ly_ctx *ctx = (struct ly_ctx *)(PARSER_CTX(pctx)); |
Radek Krejci | 3eb299d | 2019-04-08 15:07:44 +0200 | [diff] [blame] | 970 | struct lysp_submodule *submod = NULL; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 971 | const char *submodule_data = NULL; |
| 972 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
| 973 | void (*submodule_data_free)(void *module_data, void *user_data) = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 974 | struct lysp_load_module_check_data check_data = {0}; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 975 | struct ly_in *in; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 976 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 977 | /* submodule not present in the context, get the input data and parse it */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 978 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 979 | search_clb: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 980 | if (ctx->imp_clb) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 981 | if (ctx->imp_clb(pctx->main_mod->name, NULL, inc->name, inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data, |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 982 | &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 983 | LY_CHECK_RET(ly_in_new_memory(submodule_data, &in)); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 984 | check_data.name = inc->name; |
| 985 | check_data.revision = inc->rev[0] ? inc->rev : NULL; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 986 | check_data.submoduleof = pctx->main_mod->name; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 987 | lys_parse_mem_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 988 | ly_in_free(in, 0); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 989 | if (submodule_data_free) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 990 | submodule_data_free((void *)submodule_data, ctx->imp_clb_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 991 | } |
| 992 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 993 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 994 | if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 995 | goto search_file; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 996 | } |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 997 | } else { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 998 | search_file: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 999 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1000 | /* submodule was not received from the callback or there is no callback set */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1001 | lys_module_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, 0, pctx, pctx->main_mod->name, 1, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1002 | (void **)&submod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1003 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1004 | if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1005 | goto search_clb; |
| 1006 | } |
| 1007 | } |
| 1008 | if (submod) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1009 | if (!inc->rev[0] && (submod->latest_revision == 1)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1010 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 1011 | * consider that even the callback provides correct latest revision */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1012 | submod->latest_revision = 2; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1015 | inc->submodule = submod; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1016 | } |
| 1017 | if (!inc->submodule) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1018 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1019 | inc->name, pctx->main_mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1020 | return LY_EVALID; |
| 1021 | } |
| 1022 | |
| 1023 | return LY_SUCCESS; |
| 1024 | } |
| 1025 | |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 1026 | struct lys_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1027 | lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len) |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 1028 | { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1029 | struct lys_module *m = NULL; |
| 1030 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1031 | |
Radek Krejci | 873ab43 | 2020-08-13 13:47:27 +0200 | [diff] [blame] | 1032 | if (!len || !ly_strncmp(mod->prefix, prefix, len)) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1033 | /* it is the prefix of the module itself */ |
| 1034 | m = (struct lys_module *)mod; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 1035 | } |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1036 | |
| 1037 | /* search in imports */ |
| 1038 | if (!m && mod->parsed) { |
| 1039 | LY_ARRAY_FOR(mod->parsed->imports, u) { |
| 1040 | if (!ly_strncmp(mod->parsed->imports[u].prefix, prefix, len)) { |
| 1041 | m = mod->parsed->imports[u].module; |
| 1042 | break; |
| 1043 | } |
| 1044 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1045 | } |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1046 | |
| 1047 | return m; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1048 | } |
| 1049 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1050 | const char * |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1051 | lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import) |
| 1052 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1053 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1054 | |
| 1055 | if (import == mod) { |
| 1056 | return mod->prefix; |
| 1057 | } |
| 1058 | |
| 1059 | if (mod->parsed) { |
| 1060 | LY_ARRAY_FOR(mod->parsed->imports, u) { |
| 1061 | if (mod->parsed->imports[u].module == import) { |
| 1062 | return mod->parsed->imports[u].prefix; |
| 1063 | } |
| 1064 | } |
| 1065 | } else { |
| 1066 | /* we don't have original information about the import's prefix, |
| 1067 | * so the prefix of the import module itself is returned instead */ |
| 1068 | return import->prefix; |
| 1069 | } |
| 1070 | |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1074 | API const char * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1075 | lys_nodetype2str(uint16_t nodetype) |
| 1076 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1077 | switch (nodetype) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1078 | case LYS_CONTAINER: |
| 1079 | return "container"; |
| 1080 | case LYS_CHOICE: |
| 1081 | return "choice"; |
| 1082 | case LYS_LEAF: |
| 1083 | return "leaf"; |
| 1084 | case LYS_LEAFLIST: |
| 1085 | return "leaf-list"; |
| 1086 | case LYS_LIST: |
| 1087 | return "list"; |
| 1088 | case LYS_ANYXML: |
| 1089 | return "anyxml"; |
| 1090 | case LYS_ANYDATA: |
| 1091 | return "anydata"; |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 1092 | case LYS_CASE: |
| 1093 | return "case"; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1094 | case LYS_RPC: |
| 1095 | return "RPC"; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1096 | case LYS_ACTION: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1097 | return "action"; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1098 | case LYS_NOTIF: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1099 | return "notification"; |
Radek Krejci | fc81ea8 | 2019-04-18 13:27:22 +0200 | [diff] [blame] | 1100 | case LYS_USES: |
| 1101 | return "uses"; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1102 | default: |
| 1103 | return "unknown"; |
| 1104 | } |
| 1105 | } |
| 1106 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1107 | const char * |
| 1108 | lys_datatype2str(LY_DATA_TYPE basetype) |
| 1109 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1110 | switch (basetype) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1111 | case LY_TYPE_BINARY: |
| 1112 | return "binary"; |
| 1113 | case LY_TYPE_UINT8: |
| 1114 | return "uint8"; |
| 1115 | case LY_TYPE_UINT16: |
| 1116 | return "uint16"; |
| 1117 | case LY_TYPE_UINT32: |
| 1118 | return "uint32"; |
| 1119 | case LY_TYPE_UINT64: |
| 1120 | return "uint64"; |
| 1121 | case LY_TYPE_STRING: |
| 1122 | return "string"; |
| 1123 | case LY_TYPE_BITS: |
| 1124 | return "bits"; |
| 1125 | case LY_TYPE_BOOL: |
| 1126 | return "boolean"; |
| 1127 | case LY_TYPE_DEC64: |
| 1128 | return "decimal64"; |
| 1129 | case LY_TYPE_EMPTY: |
| 1130 | return "empty"; |
| 1131 | case LY_TYPE_ENUM: |
| 1132 | return "enumeration"; |
| 1133 | case LY_TYPE_IDENT: |
| 1134 | return "identityref"; |
| 1135 | case LY_TYPE_INST: |
| 1136 | return "instance-identifier"; |
| 1137 | case LY_TYPE_LEAFREF: |
| 1138 | return "leafref"; |
| 1139 | case LY_TYPE_UNION: |
| 1140 | return "union"; |
| 1141 | case LY_TYPE_INT8: |
| 1142 | return "int8"; |
| 1143 | case LY_TYPE_INT16: |
| 1144 | return "int16"; |
| 1145 | case LY_TYPE_INT32: |
| 1146 | return "int32"; |
| 1147 | case LY_TYPE_INT64: |
| 1148 | return "int64"; |
| 1149 | default: |
| 1150 | return "unknown"; |
| 1151 | } |
| 1152 | } |
| 1153 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1154 | API const struct lysp_tpdf * |
| 1155 | lysp_node_typedefs(const struct lysp_node *node) |
| 1156 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1157 | switch (node->nodetype) { |
| 1158 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1159 | return ((struct lysp_node_container *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1160 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1161 | return ((struct lysp_node_list *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1162 | case LYS_GROUPING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1163 | return ((struct lysp_grp *)node)->typedefs; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1164 | case LYS_RPC: |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1165 | case LYS_ACTION: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1166 | return ((struct lysp_action *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1167 | case LYS_INOUT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1168 | return ((struct lysp_action_inout *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1169 | case LYS_NOTIF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1170 | return ((struct lysp_notif *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1171 | default: |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1172 | return NULL; |
| 1173 | } |
| 1174 | } |
| 1175 | |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1176 | API const struct lysp_grp * |
| 1177 | lysp_node_groupings(const struct lysp_node *node) |
| 1178 | { |
| 1179 | switch (node->nodetype) { |
| 1180 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1181 | return ((struct lysp_node_container *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1182 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1183 | return ((struct lysp_node_list *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1184 | case LYS_GROUPING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1185 | return ((struct lysp_grp *)node)->groupings; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1186 | case LYS_RPC: |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1187 | case LYS_ACTION: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1188 | return ((struct lysp_action *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1189 | case LYS_INOUT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1190 | return ((struct lysp_action_inout *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1191 | case LYS_NOTIF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1192 | return ((struct lysp_notif *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1193 | default: |
| 1194 | return NULL; |
| 1195 | } |
| 1196 | } |
| 1197 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1198 | struct lysp_action ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1199 | lysp_node_actions_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1200 | { |
| 1201 | assert(node); |
| 1202 | switch (node->nodetype) { |
| 1203 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1204 | return &((struct lysp_node_container *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1205 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1206 | return &((struct lysp_node_list *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1207 | case LYS_GROUPING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1208 | return &((struct lysp_grp *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1209 | case LYS_AUGMENT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1210 | return &((struct lysp_augment *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1211 | default: |
| 1212 | return NULL; |
| 1213 | } |
| 1214 | } |
| 1215 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1216 | API const struct lysp_action * |
| 1217 | lysp_node_actions(const struct lysp_node *node) |
| 1218 | { |
| 1219 | struct lysp_action **actions; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1220 | actions = lysp_node_actions_p((struct lysp_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1221 | if (actions) { |
| 1222 | return *actions; |
| 1223 | } else { |
| 1224 | return NULL; |
| 1225 | } |
| 1226 | } |
| 1227 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1228 | struct lysp_notif ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1229 | lysp_node_notifs_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1230 | { |
| 1231 | assert(node); |
| 1232 | switch (node->nodetype) { |
| 1233 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1234 | return &((struct lysp_node_container *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1235 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1236 | return &((struct lysp_node_list *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1237 | case LYS_GROUPING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1238 | return &((struct lysp_grp *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1239 | case LYS_AUGMENT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1240 | return &((struct lysp_augment *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1241 | default: |
| 1242 | return NULL; |
| 1243 | } |
| 1244 | } |
| 1245 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1246 | API const struct lysp_notif * |
| 1247 | lysp_node_notifs(const struct lysp_node *node) |
| 1248 | { |
| 1249 | struct lysp_notif **notifs; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1250 | notifs = lysp_node_notifs_p((struct lysp_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1251 | if (notifs) { |
| 1252 | return *notifs; |
| 1253 | } else { |
| 1254 | return NULL; |
| 1255 | } |
| 1256 | } |
| 1257 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1258 | struct lysp_node ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1259 | lysp_node_children_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1260 | { |
| 1261 | assert(node); |
| 1262 | switch (node->nodetype) { |
| 1263 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1264 | return &((struct lysp_node_container *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1265 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1266 | return &((struct lysp_node_choice *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1267 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1268 | return &((struct lysp_node_list *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1269 | case LYS_CASE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1270 | return &((struct lysp_node_case *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1271 | case LYS_GROUPING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1272 | return &((struct lysp_grp *)node)->data; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1273 | case LYS_AUGMENT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1274 | return &((struct lysp_augment *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1275 | case LYS_INOUT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1276 | return &((struct lysp_action_inout *)node)->data; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1277 | case LYS_NOTIF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1278 | return &((struct lysp_notif *)node)->data; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1279 | default: |
| 1280 | return NULL; |
| 1281 | } |
| 1282 | } |
| 1283 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1284 | API const struct lysp_node * |
| 1285 | lysp_node_children(const struct lysp_node *node) |
| 1286 | { |
| 1287 | struct lysp_node **children; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1288 | |
| 1289 | if (!node) { |
| 1290 | return NULL; |
| 1291 | } |
| 1292 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1293 | children = lysp_node_children_p((struct lysp_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1294 | if (children) { |
| 1295 | return *children; |
| 1296 | } else { |
| 1297 | return NULL; |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | struct lysc_action ** |
| 1302 | lysc_node_actions_p(struct lysc_node *node) |
| 1303 | { |
| 1304 | assert(node); |
| 1305 | switch (node->nodetype) { |
| 1306 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1307 | return &((struct lysc_node_container *)node)->actions; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1308 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1309 | return &((struct lysc_node_list *)node)->actions; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1310 | default: |
| 1311 | return NULL; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | API const struct lysc_action * |
| 1316 | lysc_node_actions(const struct lysc_node *node) |
| 1317 | { |
| 1318 | struct lysc_action **actions; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1319 | actions = lysc_node_actions_p((struct lysc_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1320 | if (actions) { |
| 1321 | return *actions; |
| 1322 | } else { |
| 1323 | return NULL; |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | struct lysc_notif ** |
| 1328 | lysc_node_notifs_p(struct lysc_node *node) |
| 1329 | { |
| 1330 | assert(node); |
| 1331 | switch (node->nodetype) { |
| 1332 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1333 | return &((struct lysc_node_container *)node)->notifs; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1334 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1335 | return &((struct lysc_node_list *)node)->notifs; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1336 | default: |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | API const struct lysc_notif * |
| 1342 | lysc_node_notifs(const struct lysc_node *node) |
| 1343 | { |
| 1344 | struct lysc_notif **notifs; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1345 | notifs = lysc_node_notifs_p((struct lysc_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1346 | if (notifs) { |
| 1347 | return *notifs; |
| 1348 | } else { |
| 1349 | return NULL; |
| 1350 | } |
| 1351 | } |
| 1352 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1353 | struct lysc_node ** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1354 | lysc_node_children_p(const struct lysc_node *node, uint16_t flags) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1355 | { |
| 1356 | assert(node); |
| 1357 | switch (node->nodetype) { |
| 1358 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1359 | return &((struct lysc_node_container *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1360 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1361 | if (((struct lysc_node_choice *)node)->cases) { |
| 1362 | return &((struct lysc_node_choice *)node)->cases->child; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1363 | } else { |
| 1364 | return NULL; |
| 1365 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 1366 | case LYS_CASE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1367 | return &((struct lysc_node_case *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1368 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1369 | return &((struct lysc_node_list *)node)->child; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1370 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1371 | case LYS_ACTION: |
| 1372 | if (flags & LYS_CONFIG_R) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1373 | return &((struct lysc_action *)node)->output.data; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1374 | } else { |
| 1375 | /* LYS_CONFIG_W, but also the default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1376 | return &((struct lysc_action *)node)->input.data; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1377 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1378 | case LYS_NOTIF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1379 | return &((struct lysc_notif *)node)->data; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1380 | default: |
| 1381 | return NULL; |
| 1382 | } |
| 1383 | } |
| 1384 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1385 | API const struct lysc_node * |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1386 | lysc_node_children(const struct lysc_node *node, uint16_t flags) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1387 | { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1388 | struct lysc_node **children; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1389 | |
| 1390 | if (!node) { |
| 1391 | return NULL; |
| 1392 | } |
| 1393 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1394 | children = lysc_node_children_p((struct lysc_node *)node, flags); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1395 | if (children) { |
| 1396 | return *children; |
| 1397 | } else { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1398 | return NULL; |
| 1399 | } |
| 1400 | } |
| 1401 | |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1402 | struct lys_module * |
| 1403 | lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod) |
| 1404 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1405 | for (uint32_t u = 0; u < ctx->list.count; ++u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1406 | if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) { |
| 1407 | return ((struct lys_module *)ctx->list.objs[u]); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | return NULL; |
| 1411 | } |
| 1412 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1413 | enum ly_stmt |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1414 | lysp_match_kw(struct lys_yang_parser_ctx *ctx, struct ly_in *in) |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 1415 | { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1416 | /** |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1417 | * @brief Move the INPUT by COUNT items. Also updates the indent value in yang parser context |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1418 | * @param[in] CTX yang parser context to update its indent value. |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1419 | * @param[in,out] IN input to move |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1420 | * @param[in] COUNT number of items for which the DATA pointer is supposed to move on. |
Michal Vasko | 64246d8 | 2020-08-19 12:35:00 +0200 | [diff] [blame] | 1421 | * |
| 1422 | * *INDENT-OFF* |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1423 | */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1424 | #define MOVE_IN(CTX, IN, COUNT) ly_in_skip(IN, COUNT);if(CTX){(CTX)->indent+=COUNT;} |
| 1425 | #define IF_KW(STR, LEN, STMT) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN);*kw=STMT;} |
| 1426 | #define IF_KW_PREFIX(STR, LEN) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1427 | #define IF_KW_PREFIX_END } |
David Sedlák | 572e7ab | 2019-06-04 16:01:58 +0200 | [diff] [blame] | 1428 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1429 | const char *start = in->current; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1430 | enum ly_stmt result = LY_STMT_NONE; |
| 1431 | enum ly_stmt *kw = &result; |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1432 | /* read the keyword itself */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1433 | switch (in->current[0]) { |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1434 | case 'a': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1435 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1436 | IF_KW("rgument", 7, LY_STMT_ARGUMENT) |
| 1437 | else IF_KW("ugment", 6, LY_STMT_AUGMENT) |
| 1438 | else IF_KW("ction", 5, LY_STMT_ACTION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1439 | else IF_KW_PREFIX("ny", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1440 | IF_KW("data", 4, LY_STMT_ANYDATA) |
| 1441 | else IF_KW("xml", 3, LY_STMT_ANYXML) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1442 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1443 | break; |
| 1444 | case 'b': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1445 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1446 | IF_KW("ase", 3, LY_STMT_BASE) |
| 1447 | else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO) |
| 1448 | else IF_KW("it", 2, LY_STMT_BIT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1449 | break; |
| 1450 | case 'c': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1451 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1452 | IF_KW("ase", 3, LY_STMT_CASE) |
| 1453 | else IF_KW("hoice", 5, LY_STMT_CHOICE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1454 | else IF_KW_PREFIX("on", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1455 | IF_KW("fig", 3, LY_STMT_CONFIG) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1456 | else IF_KW_PREFIX("ta", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1457 | IF_KW("ct", 2, LY_STMT_CONTACT) |
| 1458 | else IF_KW("iner", 4, LY_STMT_CONTAINER) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1459 | IF_KW_PREFIX_END |
| 1460 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1461 | break; |
| 1462 | case 'd': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1463 | MOVE_IN(ctx, in, 1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1464 | IF_KW_PREFIX("e", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1465 | IF_KW("fault", 5, LY_STMT_DEFAULT) |
| 1466 | else IF_KW("scription", 9, LY_STMT_DESCRIPTION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1467 | else IF_KW_PREFIX("viat", 4) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1468 | IF_KW("e", 1, LY_STMT_DEVIATE) |
| 1469 | else IF_KW("ion", 3, LY_STMT_DEVIATION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1470 | IF_KW_PREFIX_END |
| 1471 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1472 | break; |
| 1473 | case 'e': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1474 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1475 | IF_KW("num", 3, LY_STMT_ENUM) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1476 | else IF_KW_PREFIX("rror-", 5) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1477 | IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG) |
| 1478 | else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1479 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1480 | else IF_KW("xtension", 8, LY_STMT_EXTENSION) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1481 | break; |
| 1482 | case 'f': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1483 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1484 | IF_KW("eature", 6, LY_STMT_FEATURE) |
| 1485 | else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1486 | break; |
| 1487 | case 'g': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1488 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1489 | IF_KW("rouping", 7, LY_STMT_GROUPING) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1490 | break; |
| 1491 | case 'i': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1492 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1493 | IF_KW("dentity", 7, LY_STMT_IDENTITY) |
| 1494 | else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE) |
| 1495 | else IF_KW("mport", 5, LY_STMT_IMPORT) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1496 | else IF_KW_PREFIX("n", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1497 | IF_KW("clude", 5, LY_STMT_INCLUDE) |
| 1498 | else IF_KW("put", 3, LY_STMT_INPUT) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1499 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1500 | break; |
| 1501 | case 'k': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1502 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1503 | IF_KW("ey", 2, LY_STMT_KEY) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1504 | break; |
| 1505 | case 'l': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1506 | MOVE_IN(ctx, in, 1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1507 | IF_KW_PREFIX("e", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1508 | IF_KW("af-list", 7, LY_STMT_LEAF_LIST) |
| 1509 | else IF_KW("af", 2, LY_STMT_LEAF) |
| 1510 | else IF_KW("ngth", 4, LY_STMT_LENGTH) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1511 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1512 | else IF_KW("ist", 3, LY_STMT_LIST) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1513 | break; |
| 1514 | case 'm': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1515 | MOVE_IN(ctx, in, 1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1516 | IF_KW_PREFIX("a", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1517 | IF_KW("ndatory", 7, LY_STMT_MANDATORY) |
| 1518 | else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1519 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1520 | else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS) |
| 1521 | else IF_KW("ust", 3, LY_STMT_MUST) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1522 | else IF_KW_PREFIX("od", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1523 | IF_KW("ule", 3, LY_STMT_MODULE) |
| 1524 | else IF_KW("ifier", 5, LY_STMT_MODIFIER) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1525 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1526 | break; |
| 1527 | case 'n': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1528 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1529 | IF_KW("amespace", 8, LY_STMT_NAMESPACE) |
| 1530 | else IF_KW("otification", 11, LY_STMT_NOTIFICATION) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1531 | break; |
| 1532 | case 'o': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1533 | MOVE_IN(ctx, in, 1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1534 | IF_KW_PREFIX("r", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1535 | IF_KW("dered-by", 8, LY_STMT_ORDERED_BY) |
| 1536 | else IF_KW("ganization", 10, LY_STMT_ORGANIZATION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1537 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1538 | else IF_KW("utput", 5, LY_STMT_OUTPUT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1539 | break; |
| 1540 | case 'p': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1541 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1542 | IF_KW("ath", 3, LY_STMT_PATH) |
| 1543 | else IF_KW("attern", 6, LY_STMT_PATTERN) |
| 1544 | else IF_KW("osition", 7, LY_STMT_POSITION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1545 | else IF_KW_PREFIX("re", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1546 | IF_KW("fix", 3, LY_STMT_PREFIX) |
| 1547 | else IF_KW("sence", 5, LY_STMT_PRESENCE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1548 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1549 | break; |
| 1550 | case 'r': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1551 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1552 | IF_KW("ange", 4, LY_STMT_RANGE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1553 | else IF_KW_PREFIX("e", 1) |
| 1554 | IF_KW_PREFIX("f", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1555 | IF_KW("erence", 6, LY_STMT_REFERENCE) |
| 1556 | else IF_KW("ine", 3, LY_STMT_REFINE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1557 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1558 | else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE) |
| 1559 | else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE) |
| 1560 | else IF_KW("vision", 6, LY_STMT_REVISION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1561 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1562 | else IF_KW("pc", 2, LY_STMT_RPC) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1563 | break; |
| 1564 | case 's': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1565 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1566 | IF_KW("tatus", 5, LY_STMT_STATUS) |
| 1567 | else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1568 | break; |
| 1569 | case 't': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1570 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1571 | IF_KW("ypedef", 6, LY_STMT_TYPEDEF) |
| 1572 | else IF_KW("ype", 3, LY_STMT_TYPE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1573 | break; |
| 1574 | case 'u': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1575 | MOVE_IN(ctx, in, 1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1576 | IF_KW_PREFIX("ni", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1577 | IF_KW("que", 3, LY_STMT_UNIQUE) |
| 1578 | else IF_KW("ts", 2, LY_STMT_UNITS) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1579 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1580 | else IF_KW("ses", 3, LY_STMT_USES) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1581 | break; |
| 1582 | case 'v': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1583 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1584 | IF_KW("alue", 4, LY_STMT_VALUE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1585 | break; |
| 1586 | case 'w': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1587 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1588 | IF_KW("hen", 3, LY_STMT_WHEN) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1589 | break; |
| 1590 | case 'y': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1591 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1592 | IF_KW("ang-version", 11, LY_STMT_YANG_VERSION) |
| 1593 | else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1594 | break; |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1595 | default: |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1596 | /* if context is not NULL we are matching keyword from YANG data*/ |
| 1597 | if (ctx) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1598 | if (in->current[0] == ';') { |
| 1599 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1600 | *kw = LY_STMT_SYNTAX_SEMICOLON; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1601 | } else if (in->current[0] == '{') { |
| 1602 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1603 | *kw = LY_STMT_SYNTAX_LEFT_BRACE; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1604 | } else if (in->current[0] == '}') { |
| 1605 | MOVE_IN(ctx, in, 1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1606 | *kw = LY_STMT_SYNTAX_RIGHT_BRACE; |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1607 | } |
| 1608 | } |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1609 | break; |
| 1610 | } |
| 1611 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1612 | if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) { |
Radek Krejci | 6e546bf | 2020-05-19 16:16:19 +0200 | [diff] [blame] | 1613 | /* the keyword is not terminated */ |
| 1614 | *kw = LY_STMT_NONE; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1615 | in->current = start; |
Radek Krejci | 6e546bf | 2020-05-19 16:16:19 +0200 | [diff] [blame] | 1616 | } |
| 1617 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1618 | #undef IF_KW |
| 1619 | #undef IF_KW_PREFIX |
| 1620 | #undef IF_KW_PREFIX_END |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 1621 | #undef MOVE_IN |
Michal Vasko | 64246d8 | 2020-08-19 12:35:00 +0200 | [diff] [blame] | 1622 | /* *INDENT-ON* */ |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 1623 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1624 | return result; |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1625 | } |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 1626 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1627 | LY_ARRAY_COUNT_TYPE |
| 1628 | lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1629 | { |
| 1630 | LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL); |
| 1631 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1632 | for ( ; index < LY_ARRAY_COUNT(ext); index++) { |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1633 | if (ext[index].insubstmt == substmt) { |
| 1634 | return index; |
| 1635 | } |
| 1636 | } |
| 1637 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1638 | return LY_ARRAY_COUNT(ext); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1639 | } |
| 1640 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1641 | const struct lysc_node * |
| 1642 | lysc_data_parent(const struct lysc_node *schema) |
| 1643 | { |
| 1644 | const struct lysc_node *parent; |
| 1645 | |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1646 | for (parent = schema->parent; parent && (parent->nodetype & (LYS_CHOICE | LYS_CASE)); parent = parent->parent) {} |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1647 | |
| 1648 | return parent; |
| 1649 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1650 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1651 | uint8_t |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1652 | lysc_is_output(const struct lysc_node *schema) |
| 1653 | { |
| 1654 | const struct lysc_node *parent; |
| 1655 | |
| 1656 | assert(schema); |
| 1657 | |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1658 | for (parent = schema->parent; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION)); parent = parent->parent) {} |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1659 | if (parent && (schema->flags & LYS_CONFIG_R)) { |
| 1660 | return 1; |
| 1661 | } |
| 1662 | return 0; |
| 1663 | } |
Michal Vasko | d975f86 | 2020-06-23 13:29:28 +0200 | [diff] [blame] | 1664 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1665 | API uint8_t |
Michal Vasko | d975f86 | 2020-06-23 13:29:28 +0200 | [diff] [blame] | 1666 | lysc_is_userordered(const struct lysc_node *schema) |
| 1667 | { |
| 1668 | if (!schema || !(schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) || !(schema->flags & LYS_ORDBY_USER)) { |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | return 1; |
| 1673 | } |
Michal Vasko | 2fd91b9 | 2020-07-17 16:39:02 +0200 | [diff] [blame] | 1674 | |
| 1675 | API LY_ERR |
| 1676 | lysc_set_private(const struct lysc_node *node, void *priv, void **prev) |
| 1677 | { |
| 1678 | struct lysc_action *act; |
| 1679 | struct lysc_notif *notif; |
| 1680 | |
| 1681 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
| 1682 | |
| 1683 | switch (node->nodetype) { |
| 1684 | case LYS_CONTAINER: |
| 1685 | case LYS_CHOICE: |
| 1686 | case LYS_CASE: |
| 1687 | case LYS_LEAF: |
| 1688 | case LYS_LEAFLIST: |
| 1689 | case LYS_LIST: |
| 1690 | case LYS_ANYXML: |
| 1691 | case LYS_ANYDATA: |
| 1692 | if (prev) { |
| 1693 | *prev = node->priv; |
| 1694 | } |
| 1695 | ((struct lysc_node *)node)->priv = priv; |
| 1696 | break; |
| 1697 | case LYS_RPC: |
| 1698 | case LYS_ACTION: |
| 1699 | act = (struct lysc_action *)node; |
| 1700 | if (prev) { |
| 1701 | *prev = act->priv; |
| 1702 | } |
| 1703 | act->priv = priv; |
| 1704 | break; |
| 1705 | case LYS_NOTIF: |
| 1706 | notif = (struct lysc_notif *)node; |
| 1707 | if (prev) { |
| 1708 | *prev = notif->priv; |
| 1709 | } |
| 1710 | notif->priv = priv; |
| 1711 | break; |
| 1712 | default: |
| 1713 | return LY_EINVAL; |
| 1714 | } |
| 1715 | |
| 1716 | return LY_SUCCESS; |
| 1717 | } |