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 | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 14 | #include "common.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 15 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 16 | #include <assert.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 17 | #include <ctype.h> |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 20 | #include <limits.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include <stdint.h> |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 22 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include <string.h> |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 24 | #include <unistd.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 25 | #include <time.h> |
| 26 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | #include "context.h" |
| 28 | #include "dict.h" |
| 29 | #include "extensions.h" |
| 30 | #include "hash_table.h" |
| 31 | #include "log.h" |
| 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 |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 38 | lys_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *context_node, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 39 | const struct lys_module *context_module, int nodetype, int implement, |
| 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 | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 47 | int getnext_extra_flag = 0; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 48 | int 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 */ |
| 95 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 96 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 97 | if (context_node && context_node->nodetype == LYS_ACTION) { |
| 98 | /* move through input/output manually */ |
| 99 | if (!strncmp("input", name, name_len)) { |
| 100 | (*result_flag) |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 101 | } else if (!strncmp("output", name, name_len)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 102 | (*result_flag) |= LYSC_OPT_RPC_OUTPUT; |
| 103 | getnext_extra_flag = LYS_GETNEXT_OUTPUT; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 104 | } else { |
| 105 | goto getnext; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 106 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 107 | current_nodetype = LYS_INOUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 108 | } else { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 109 | getnext: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 110 | context_node = lys_child(context_node, mod, name, name_len, 0, |
| 111 | getnext_extra_flag | LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE); |
| 112 | if (!context_node) { |
| 113 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 114 | "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid); |
| 115 | return LY_ENOTFOUND; |
| 116 | } |
| 117 | getnext_extra_flag = 0; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 118 | current_nodetype = context_node->nodetype; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 119 | |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 120 | if (current_nodetype == LYS_NOTIF) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 121 | (*result_flag) |= LYSC_OPT_NOTIFICATION; |
| 122 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 123 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 124 | if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 125 | break; |
| 126 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 127 | if (*id != '/') { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 128 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 129 | "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.", |
| 130 | nodeid_type, id - nodeid + 1, nodeid); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 131 | return LY_EVALID; |
| 132 | } |
| 133 | ++id; |
| 134 | } |
| 135 | |
| 136 | if (ret == LY_SUCCESS) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 137 | *target = context_node; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 138 | if (nodetype & LYS_INOUT) { |
| 139 | /* instead of input/output nodes, the RPC/action node is actually returned */ |
| 140 | } |
| 141 | if (nodetype && !(current_nodetype & nodetype)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 142 | return LY_EDENIED; |
| 143 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 144 | } else { |
| 145 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 146 | "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.", |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 147 | nodeid_type, nodeid_len ? nodeid_len : strlen(nodeid), nodeid); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 154 | 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] | 155 | { |
| 156 | struct lysp_import *i; |
| 157 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 158 | if (module_prefix && &module_prefix != value && !strcmp(module_prefix, *value)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 159 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, |
| 160 | "Prefix \"%s\" already used as module prefix.", *value); |
| 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)) { |
| 165 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", |
| 166 | *value, i->name); |
| 167 | return LY_EEXIST; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | return LY_SUCCESS; |
| 171 | } |
| 172 | |
| 173 | LY_ERR |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 174 | lysc_check_status(struct lysc_ctx *ctx, |
| 175 | uint16_t flags1, void *mod1, const char *name1, |
| 176 | uint16_t flags2, void *mod2, const char *name2) |
| 177 | { |
| 178 | uint16_t flg1, flg2; |
| 179 | |
| 180 | flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR; |
| 181 | flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR; |
| 182 | |
| 183 | if ((flg1 < flg2) && (mod1 == mod2)) { |
| 184 | if (ctx) { |
| 185 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 186 | "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".", |
| 187 | flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1, |
| 188 | flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2); |
| 189 | } |
| 190 | return LY_EVALID; |
| 191 | } |
| 192 | |
| 193 | return LY_SUCCESS; |
| 194 | } |
| 195 | |
| 196 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 197 | lysp_check_date(struct lys_parser_ctx *ctx, const char *date, int date_len, const char *stmt) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 198 | { |
| 199 | int i; |
| 200 | struct tm tm, tm_; |
| 201 | char *r; |
| 202 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 203 | LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, date, LY_EINVAL); |
| 204 | LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? ctx->ctx : NULL, date_len), LY_EINVAL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 205 | |
| 206 | /* check format */ |
| 207 | for (i = 0; i < date_len; i++) { |
| 208 | if (i == 4 || i == 7) { |
| 209 | if (date[i] != '-') { |
| 210 | goto error; |
| 211 | } |
| 212 | } else if (!isdigit(date[i])) { |
| 213 | goto error; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* check content, e.g. 2018-02-31 */ |
| 218 | memset(&tm, 0, sizeof tm); |
| 219 | r = strptime(date, "%Y-%m-%d", &tm); |
| 220 | if (!r || r != &date[LY_REV_SIZE - 1]) { |
| 221 | goto error; |
| 222 | } |
| 223 | memcpy(&tm_, &tm, sizeof tm); |
| 224 | mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */ |
| 225 | if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */ |
| 226 | /* checking days is enough, since other errors |
| 227 | * have been checked by strptime() */ |
| 228 | goto error; |
| 229 | } |
| 230 | |
| 231 | return LY_SUCCESS; |
| 232 | |
| 233 | error: |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 234 | if (stmt) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 235 | if (ctx) { |
| 236 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LY_VCODE_INVAL, date_len, date, stmt); |
| 237 | } else { |
| 238 | LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt); |
| 239 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 240 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 241 | return LY_EINVAL; |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | lysp_sort_revisions(struct lysp_revision *revs) |
| 246 | { |
| 247 | uint8_t i, r; |
| 248 | struct lysp_revision rev; |
| 249 | |
| 250 | for (i = 1, r = 0; revs && i < LY_ARRAY_SIZE(revs); i++) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 251 | if (strcmp(revs[i].date, revs[r].date) > 0) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 252 | r = i; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (r) { |
| 257 | /* the newest revision is not on position 0, switch them */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 258 | memcpy(&rev, &revs[0], sizeof rev); |
| 259 | memcpy(&revs[0], &revs[r], sizeof rev); |
| 260 | memcpy(&revs[r], &rev, sizeof rev); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 261 | } |
| 262 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 263 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 264 | static const struct lysp_tpdf * |
| 265 | lysp_type_match(const char *name, struct lysp_node *node) |
| 266 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 267 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 268 | unsigned int u; |
| 269 | |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 270 | typedefs = lysp_node_typedefs(node); |
| 271 | LY_ARRAY_FOR(typedefs, u) { |
| 272 | if (!strcmp(name, typedefs[u].name)) { |
| 273 | /* match */ |
| 274 | return &typedefs[u]; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | return NULL; |
| 279 | } |
| 280 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 281 | static LY_DATA_TYPE |
| 282 | lysp_type_str2builtin(const char *name, size_t len) |
| 283 | { |
| 284 | if (len >= 4) { /* otherwise it does not match any built-in type */ |
| 285 | if (name[0] == 'b') { |
| 286 | if (name[1] == 'i') { |
| 287 | if (len == 6 && !strncmp(&name[2], "nary", 4)) { |
| 288 | return LY_TYPE_BINARY; |
| 289 | } else if (len == 4 && !strncmp(&name[2], "ts", 2)) { |
| 290 | return LY_TYPE_BITS; |
| 291 | } |
| 292 | } else if (len == 7 && !strncmp(&name[1], "oolean", 6)) { |
| 293 | return LY_TYPE_BOOL; |
| 294 | } |
| 295 | } else if (name[0] == 'd') { |
| 296 | if (len == 9 && !strncmp(&name[1], "ecimal64", 8)) { |
| 297 | return LY_TYPE_DEC64; |
| 298 | } |
| 299 | } else if (name[0] == 'e') { |
| 300 | if (len == 5 && !strncmp(&name[1], "mpty", 4)) { |
| 301 | return LY_TYPE_EMPTY; |
| 302 | } else if (len == 11 && !strncmp(&name[1], "numeration", 10)) { |
| 303 | return LY_TYPE_ENUM; |
| 304 | } |
| 305 | } else if (name[0] == 'i') { |
| 306 | if (name[1] == 'n') { |
| 307 | if (len == 4 && !strncmp(&name[2], "t8", 2)) { |
| 308 | return LY_TYPE_INT8; |
| 309 | } else if (len == 5) { |
| 310 | if (!strncmp(&name[2], "t16", 3)) { |
| 311 | return LY_TYPE_INT16; |
| 312 | } else if (!strncmp(&name[2], "t32", 3)) { |
| 313 | return LY_TYPE_INT32; |
| 314 | } else if (!strncmp(&name[2], "t64", 3)) { |
| 315 | return LY_TYPE_INT64; |
| 316 | } |
| 317 | } else if (len == 19 && !strncmp(&name[2], "stance-identifier", 17)) { |
| 318 | return LY_TYPE_INST; |
| 319 | } |
| 320 | } else if (len == 11 && !strncmp(&name[1], "dentityref", 10)) { |
| 321 | return LY_TYPE_IDENT; |
| 322 | } |
| 323 | } else if (name[0] == 'l') { |
| 324 | if (len == 7 && !strncmp(&name[1], "eafref", 6)) { |
| 325 | return LY_TYPE_LEAFREF; |
| 326 | } |
| 327 | } else if (name[0] == 's') { |
| 328 | if (len == 6 && !strncmp(&name[1], "tring", 5)) { |
| 329 | return LY_TYPE_STRING; |
| 330 | } |
| 331 | } else if (name[0] == 'u') { |
| 332 | if (name[1] == 'n') { |
| 333 | if (len == 5 && !strncmp(&name[2], "ion", 3)) { |
| 334 | return LY_TYPE_UNION; |
| 335 | } |
| 336 | } else if (name[1] == 'i' && name[2] == 'n' && name[3] == 't') { |
| 337 | if (len == 5 && name[4] == '8') { |
| 338 | return LY_TYPE_UINT8; |
| 339 | } else if (len == 6) { |
| 340 | if (!strncmp(&name[4], "16", 2)) { |
| 341 | return LY_TYPE_UINT16; |
| 342 | } else if (!strncmp(&name[4], "32", 2)) { |
| 343 | return LY_TYPE_UINT32; |
| 344 | } else if (!strncmp(&name[4], "64", 2)) { |
| 345 | return LY_TYPE_UINT64; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return LY_TYPE_UNKNOWN; |
| 353 | } |
| 354 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 355 | LY_ERR |
| 356 | lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module, |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 357 | 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] | 358 | { |
| 359 | const char *str, *name; |
| 360 | struct lysp_tpdf *typedefs; |
| 361 | unsigned int u, v; |
| 362 | |
| 363 | assert(id); |
| 364 | assert(start_module); |
| 365 | assert(tpdf); |
| 366 | assert(node); |
| 367 | assert(module); |
| 368 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 369 | *node = NULL; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 370 | str = strchr(id, ':'); |
| 371 | if (str) { |
| 372 | *module = lysp_module_find_prefix(start_module, id, str - id); |
| 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 | |
| 427 | /* |
| 428 | * @brief Check name of a new type to avoid name collisions. |
| 429 | * |
| 430 | * @param[in] ctx Parser context, module where the type is being defined is taken from here. |
| 431 | * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef. |
| 432 | * @param[in] tpdf Typedef definition to check. |
| 433 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
| 434 | * typedefs are checked, caller is supposed to free the table. |
| 435 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
| 436 | * typedefs are checked, caller is supposed to free the table. |
| 437 | * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise. |
| 438 | */ |
| 439 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 440 | lysp_check_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf, |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 441 | struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped) |
| 442 | { |
| 443 | struct lysp_node *parent; |
| 444 | uint32_t hash; |
| 445 | size_t name_len; |
| 446 | const char *name; |
| 447 | unsigned int u; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 448 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 449 | |
| 450 | assert(ctx); |
| 451 | assert(tpdf); |
| 452 | |
| 453 | name = tpdf->name; |
| 454 | name_len = strlen(name); |
| 455 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 456 | if (lysp_type_str2builtin(name, name_len)) { |
| 457 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 458 | "Invalid name \"%s\" of typedef - name collision with a built-in type.", name); |
| 459 | return LY_EEXIST; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* check locally scoped typedefs (avoid name shadowing) */ |
| 463 | if (node) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 464 | typedefs = lysp_node_typedefs(node); |
| 465 | LY_ARRAY_FOR(typedefs, u) { |
| 466 | if (&typedefs[u] == tpdf) { |
| 467 | break; |
| 468 | } |
| 469 | if (!strcmp(name, typedefs[u].name)) { |
| 470 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 471 | "Invalid name \"%s\" of typedef - name collision with sibling type.", name); |
| 472 | return LY_EEXIST; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | /* search typedefs in parent's nodes */ |
Radek Krejci | 87e78ca | 2019-05-02 09:51:29 +0200 | [diff] [blame] | 476 | for (parent = node->parent; parent; parent = parent->parent) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 477 | if (lysp_type_match(name, parent)) { |
| 478 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 479 | "Invalid name \"%s\" of typedef - name collision with another scoped type.", name); |
| 480 | return LY_EEXIST; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /* check collision with the top-level typedefs */ |
| 486 | hash = dict_hash(name, name_len); |
| 487 | if (node) { |
| 488 | lyht_insert(tpdfs_scoped, &name, hash, NULL); |
| 489 | if (!lyht_find(tpdfs_global, &name, hash, NULL)) { |
| 490 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 491 | "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name); |
| 492 | return LY_EEXIST; |
| 493 | } |
| 494 | } else { |
| 495 | if (lyht_insert(tpdfs_global, &name, hash, NULL)) { |
| 496 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 497 | "Invalid name \"%s\" of typedef - name collision with another top-level type.", name); |
| 498 | return LY_EEXIST; |
| 499 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 500 | /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the |
| 501 | * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision |
| 502 | * is detected in the first branch few lines above */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | return LY_SUCCESS; |
| 506 | } |
| 507 | |
| 508 | static int |
| 509 | lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data)) |
| 510 | { |
| 511 | return !strcmp(val1, val2); |
| 512 | } |
| 513 | |
| 514 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 515 | lysp_check_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 516 | { |
| 517 | struct hash_table *ids_global; |
| 518 | struct hash_table *ids_scoped; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 519 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 520 | unsigned int i, u; |
| 521 | LY_ERR ret = LY_EVALID; |
| 522 | |
| 523 | /* check name collisions - typedefs and groupings */ |
| 524 | ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1); |
| 525 | ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 526 | LY_ARRAY_FOR(mod->typedefs, i) { |
| 527 | if (lysp_check_typedef(ctx, NULL, &mod->typedefs[i], ids_global, ids_scoped)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 528 | goto cleanup; |
| 529 | } |
| 530 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 531 | LY_ARRAY_FOR(mod->includes, i) { |
| 532 | LY_ARRAY_FOR(mod->includes[i].submodule->typedefs, u) { |
| 533 | if (lysp_check_typedef(ctx, NULL, &mod->includes[i].submodule->typedefs[u], ids_global, ids_scoped)) { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 534 | goto cleanup; |
| 535 | } |
| 536 | } |
| 537 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 538 | for (u = 0; u < ctx->tpdfs_nodes.count; ++u) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 539 | typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[u]); |
| 540 | LY_ARRAY_FOR(typedefs, i) { |
| 541 | if (lysp_check_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[u], &typedefs[i], ids_global, ids_scoped)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 542 | goto cleanup; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | ret = LY_SUCCESS; |
| 547 | cleanup: |
| 548 | lyht_free(ids_global); |
| 549 | lyht_free(ids_scoped); |
| 550 | ly_set_erase(&ctx->tpdfs_nodes, NULL); |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 555 | struct lysp_load_module_check_data { |
| 556 | const char *name; |
| 557 | const char *revision; |
| 558 | const char *path; |
| 559 | const char* submoduleof; |
| 560 | }; |
| 561 | |
| 562 | static LY_ERR |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 563 | lysp_load_module_check(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] | 564 | { |
| 565 | struct lysp_load_module_check_data *info = data; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 566 | const char *filename, *dot, *rev, *name; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 567 | size_t len; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 568 | struct lysp_revision *revs; |
| 569 | |
| 570 | name = mod ? mod->mod->name : submod->name; |
| 571 | revs = mod ? mod->revs : submod->revs; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 572 | |
| 573 | if (info->name) { |
| 574 | /* check name of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 575 | if (strcmp(info->name, name)) { |
| 576 | 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] | 577 | return LY_EINVAL; |
| 578 | } |
| 579 | } |
| 580 | if (info->revision) { |
| 581 | /* check revision of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 582 | if (!revs || strcmp(info->revision, revs[0].date)) { |
| 583 | 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] | 584 | revs ? revs[0].date : "none", info->revision); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 585 | return LY_EINVAL; |
| 586 | } |
| 587 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 588 | if (submod) { |
| 589 | assert(info->submoduleof); |
| 590 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 591 | /* check that the submodule belongs-to our module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 592 | if (strcmp(info->submoduleof, submod->belongsto)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 593 | 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] | 594 | submod->name, info->submoduleof, submod->belongsto); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 595 | return LY_EVALID; |
| 596 | } |
| 597 | /* check circular dependency */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 598 | if (submod->parsing) { |
| 599 | 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] | 600 | return LY_EVALID; |
| 601 | } |
| 602 | } |
| 603 | if (info->path) { |
| 604 | /* check that name and revision match filename */ |
| 605 | filename = strrchr(info->path, '/'); |
| 606 | if (!filename) { |
| 607 | filename = info->path; |
| 608 | } else { |
| 609 | filename++; |
| 610 | } |
| 611 | /* name */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 612 | len = strlen(name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 613 | rev = strchr(filename, '@'); |
| 614 | dot = strrchr(info->path, '.'); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 615 | if (strncmp(filename, name, len) || |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 616 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 617 | 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] | 618 | } |
| 619 | /* revision */ |
| 620 | if (rev) { |
| 621 | len = dot - ++rev; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 622 | if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 623 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 624 | revs ? revs[0].date : "none"); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | } |
| 628 | return LY_SUCCESS; |
| 629 | } |
| 630 | |
| 631 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 632 | lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement, struct lys_parser_ctx *main_ctx, |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 633 | void **result) |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 634 | { |
| 635 | int fd; |
| 636 | char *filepath = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 637 | const char **fp; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 638 | LYS_INFORMAT format; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 639 | void *mod = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 640 | LY_ERR ret = LY_SUCCESS; |
| 641 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 642 | char rpath[PATH_MAX]; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 643 | |
| 644 | LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision, |
| 645 | &filepath, &format)); |
| 646 | LY_CHECK_ERR_RET(!filepath, LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", |
| 647 | name, revision ? "@" : "", revision ? revision : ""), LY_ENOTFOUND); |
| 648 | |
| 649 | |
| 650 | LOGVRB("Loading schema from \"%s\" file.", filepath); |
| 651 | |
| 652 | /* open the file */ |
| 653 | fd = open(filepath, O_RDONLY); |
| 654 | LY_CHECK_ERR_GOTO(fd < 0, LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).", |
| 655 | filepath, strerror(errno)); ret = LY_ESYS, cleanup); |
| 656 | |
| 657 | check_data.name = name; |
| 658 | check_data.revision = revision; |
| 659 | check_data.path = filepath; |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 660 | mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 661 | lysp_load_module_check, &check_data); |
| 662 | close(fd); |
| 663 | LY_CHECK_ERR_GOTO(!mod, ly_errcode(ctx), cleanup); |
| 664 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 665 | if (main_ctx) { |
| 666 | fp = &((struct lysp_submodule*)mod)->filepath; |
| 667 | } else { |
| 668 | fp = &((struct lys_module*)mod)->filepath; |
| 669 | } |
| 670 | if (!(*fp)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 671 | if (realpath(filepath, rpath) != NULL) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 672 | (*fp) = lydict_insert(ctx, rpath, 0); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 673 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 674 | (*fp) = lydict_insert(ctx, filepath, 0); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
| 678 | *result = mod; |
| 679 | |
| 680 | /* success */ |
| 681 | cleanup: |
| 682 | free(filepath); |
| 683 | return ret; |
| 684 | } |
| 685 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 686 | LY_ERR |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 687 | lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, int implement, int require_parsed, struct lys_module **mod) |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 688 | { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 689 | const char *module_data = NULL; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 690 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 691 | void (*module_data_free)(void *module_data, void *user_data) = NULL; |
| 692 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 693 | struct lys_module *m; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 694 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 695 | assert(mod); |
| 696 | |
| 697 | if (!*mod) { |
| 698 | /* try to get the module from the context */ |
| 699 | if (revision) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 700 | /* get the specific revision */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 701 | *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 702 | } else if (implement) { |
| 703 | /* prefer the implemented module instead of the latest one */ |
| 704 | *mod = (struct lys_module*)ly_ctx_get_module_implemented(ctx, name); |
| 705 | if (!*mod) { |
| 706 | /* there is no implemented module in the context, try to get the latest revision module */ |
| 707 | goto latest_in_the_context; |
| 708 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 709 | } else { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 710 | /* get the requested module of the latest revision in the context */ |
| 711 | latest_in_the_context: |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 712 | *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name); |
| 713 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 716 | if (!(*mod) || (require_parsed && !(*mod)->parsed)) { |
| 717 | (*mod) = NULL; |
| 718 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 719 | /* check collision with other implemented revision */ |
| 720 | if (implement && ly_ctx_get_module_implemented(ctx, name)) { |
| 721 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 722 | "Module \"%s\" is already present in other implemented revision.", name); |
| 723 | return LY_EDENIED; |
| 724 | } |
| 725 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 726 | /* 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] | 727 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 728 | search_clb: |
| 729 | if (ctx->imp_clb) { |
| 730 | if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 731 | &format, &module_data, &module_data_free) == LY_SUCCESS) { |
| 732 | check_data.name = name; |
| 733 | check_data.revision = revision; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 734 | *mod = lys_parse_mem_module(ctx, module_data, format, implement, |
| 735 | lysp_load_module_check, &check_data); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 736 | if (module_data_free) { |
| 737 | module_data_free((void*)module_data, ctx->imp_clb_data); |
| 738 | } |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 739 | if (*mod && implement && lys_compile(*mod, 0)) { |
| 740 | ly_set_rm(&ctx->list, *mod, NULL); |
| 741 | lys_module_free(*mod, NULL); |
| 742 | *mod = NULL; |
| 743 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 747 | goto search_file; |
| 748 | } |
| 749 | } else { |
| 750 | search_file: |
| 751 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
| 752 | /* module was not received from the callback or there is no callback set */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 753 | lys_module_localfile(ctx, name, revision, implement, NULL, (void **)mod); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 754 | } |
| 755 | if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 756 | goto search_clb; |
| 757 | } |
| 758 | } |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 759 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 760 | if ((*mod) && !revision && ((*mod)->latest_revision == 1)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 761 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 762 | * consider that even the callback provides correct latest revision */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 763 | (*mod)->latest_revision = 2; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 764 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 765 | } else { |
| 766 | /* we have module from the current context */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 767 | if (implement) { |
| 768 | m = ly_ctx_get_module_implemented(ctx, name); |
| 769 | if (m && m != *mod) { |
| 770 | /* check collision with other implemented revision */ |
| 771 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 772 | "Module \"%s\" is already present in other implemented revision.", name); |
| 773 | *mod = NULL; |
| 774 | return LY_EDENIED; |
| 775 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* circular check */ |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 779 | if ((*mod)->parsed && (*mod)->parsed->parsing) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 780 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name); |
| 781 | *mod = NULL; |
| 782 | return LY_EVALID; |
| 783 | } |
| 784 | } |
| 785 | if (!(*mod)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 786 | 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] | 787 | return LY_EVALID; |
| 788 | } |
| 789 | |
| 790 | if (implement) { |
| 791 | /* mark the module implemented, check for collision was already done */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 792 | (*mod)->implemented = 1; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 793 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 794 | |
| 795 | return LY_SUCCESS; |
| 796 | } |
| 797 | |
| 798 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 799 | lysp_load_submodule(struct lys_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 800 | { |
Radek Krejci | 3eb299d | 2019-04-08 15:07:44 +0200 | [diff] [blame] | 801 | struct lysp_submodule *submod = NULL; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 802 | const char *submodule_data = NULL; |
| 803 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
| 804 | void (*submodule_data_free)(void *module_data, void *user_data) = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 805 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 806 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 807 | /* submodule not present in the context, get the input data and parse it */ |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 808 | if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 809 | search_clb: |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 810 | if (ctx->ctx->imp_clb) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 811 | if (ctx->ctx->imp_clb(mod->mod->name, NULL, inc->name, inc->rev[0] ? inc->rev : NULL, ctx->ctx->imp_clb_data, |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 812 | &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) { |
| 813 | check_data.name = inc->name; |
| 814 | check_data.revision = inc->rev[0] ? inc->rev : NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 815 | check_data.submoduleof = mod->mod->name; |
| 816 | submod = lys_parse_mem_submodule(ctx->ctx, submodule_data, format, ctx, |
| 817 | lysp_load_module_check, &check_data); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 818 | if (submodule_data_free) { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 819 | submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 820 | } |
| 821 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 822 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 823 | if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 824 | goto search_file; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 825 | } |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 826 | } else { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 827 | search_file: |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 828 | if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 829 | /* submodule was not received from the callback or there is no callback set */ |
| 830 | lys_module_localfile(ctx->ctx, inc->name, inc->rev[0] ? inc->rev : NULL, 0, ctx, (void**)&submod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 831 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 832 | if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 833 | goto search_clb; |
| 834 | } |
| 835 | } |
| 836 | if (submod) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 837 | if (!inc->rev[0] && (submod->latest_revision == 1)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 838 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 839 | * consider that even the callback provides correct latest revision */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 840 | submod->latest_revision = 2; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 843 | inc->submodule = submod; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 844 | } |
| 845 | if (!inc->submodule) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 846 | LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", |
| 847 | inc->name, mod->mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 848 | return LY_EVALID; |
| 849 | } |
| 850 | |
| 851 | return LY_SUCCESS; |
| 852 | } |
| 853 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 854 | #define FIND_MODULE(TYPE, MOD) \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 855 | TYPE *imp; \ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 856 | if (!strncmp((MOD)->mod->prefix, prefix, len) && (MOD)->mod->prefix[len] == '\0') { \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 857 | /* it is the prefix of the module itself */ \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 858 | m = ly_ctx_get_module((MOD)->mod->ctx, (MOD)->mod->name, (MOD)->mod->revision); \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 859 | } \ |
| 860 | /* search in imports */ \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 861 | if (!m) { \ |
| 862 | LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 863 | if (!strncmp(imp->prefix, prefix, len) && imp->prefix[len] == '\0') { \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 864 | m = imp->module; \ |
| 865 | break; \ |
| 866 | } \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 867 | } \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 868 | } |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 869 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 870 | struct lysc_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 871 | lysc_module_find_prefix(const struct lysc_module *mod, const char *prefix, size_t len) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 872 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 873 | const struct lys_module *m = NULL; |
| 874 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 875 | FIND_MODULE(struct lysc_import, mod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 876 | return m ? m->compiled : NULL; |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 877 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 878 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 879 | struct lysp_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 880 | lysp_module_find_prefix(const struct lysp_module *mod, const char *prefix, size_t len) |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 881 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 882 | const struct lys_module *m = NULL; |
| 883 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 884 | FIND_MODULE(struct lysp_import, mod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 885 | return m ? m->parsed : NULL; |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 886 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 887 | |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 888 | struct lys_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 889 | 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] | 890 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 891 | const struct lys_module *m = NULL; |
| 892 | |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 893 | if (!prefix) { |
| 894 | return (struct lys_module*)mod; |
| 895 | } |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 896 | if (mod->compiled) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 897 | FIND_MODULE(struct lysc_import, mod->compiled); |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 898 | } else { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 899 | FIND_MODULE(struct lysp_import, mod->parsed); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 900 | } |
| 901 | return (struct lys_module*)m; |
| 902 | } |
| 903 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 904 | const char * |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 905 | lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import) |
| 906 | { |
| 907 | unsigned int u; |
| 908 | |
| 909 | if (import == mod) { |
| 910 | return mod->prefix; |
| 911 | } |
| 912 | |
| 913 | if (mod->parsed) { |
| 914 | LY_ARRAY_FOR(mod->parsed->imports, u) { |
| 915 | if (mod->parsed->imports[u].module == import) { |
| 916 | return mod->parsed->imports[u].prefix; |
| 917 | } |
| 918 | } |
| 919 | } else { |
| 920 | /* we don't have original information about the import's prefix, |
| 921 | * so the prefix of the import module itself is returned instead */ |
| 922 | return import->prefix; |
| 923 | } |
| 924 | |
| 925 | return NULL; |
| 926 | } |
| 927 | |
| 928 | const char * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 929 | lys_nodetype2str(uint16_t nodetype) |
| 930 | { |
| 931 | switch(nodetype) { |
| 932 | case LYS_CONTAINER: |
| 933 | return "container"; |
| 934 | case LYS_CHOICE: |
| 935 | return "choice"; |
| 936 | case LYS_LEAF: |
| 937 | return "leaf"; |
| 938 | case LYS_LEAFLIST: |
| 939 | return "leaf-list"; |
| 940 | case LYS_LIST: |
| 941 | return "list"; |
| 942 | case LYS_ANYXML: |
| 943 | return "anyxml"; |
| 944 | case LYS_ANYDATA: |
| 945 | return "anydata"; |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 946 | case LYS_CASE: |
| 947 | return "case"; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 948 | case LYS_ACTION: |
| 949 | return "RPC/action"; |
| 950 | case LYS_NOTIF: |
| 951 | return "Notification"; |
Radek Krejci | fc81ea8 | 2019-04-18 13:27:22 +0200 | [diff] [blame] | 952 | case LYS_USES: |
| 953 | return "uses"; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 954 | default: |
| 955 | return "unknown"; |
| 956 | } |
| 957 | } |
| 958 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 959 | const char * |
| 960 | lys_datatype2str(LY_DATA_TYPE basetype) |
| 961 | { |
| 962 | switch(basetype) { |
| 963 | case LY_TYPE_BINARY: |
| 964 | return "binary"; |
| 965 | case LY_TYPE_UINT8: |
| 966 | return "uint8"; |
| 967 | case LY_TYPE_UINT16: |
| 968 | return "uint16"; |
| 969 | case LY_TYPE_UINT32: |
| 970 | return "uint32"; |
| 971 | case LY_TYPE_UINT64: |
| 972 | return "uint64"; |
| 973 | case LY_TYPE_STRING: |
| 974 | return "string"; |
| 975 | case LY_TYPE_BITS: |
| 976 | return "bits"; |
| 977 | case LY_TYPE_BOOL: |
| 978 | return "boolean"; |
| 979 | case LY_TYPE_DEC64: |
| 980 | return "decimal64"; |
| 981 | case LY_TYPE_EMPTY: |
| 982 | return "empty"; |
| 983 | case LY_TYPE_ENUM: |
| 984 | return "enumeration"; |
| 985 | case LY_TYPE_IDENT: |
| 986 | return "identityref"; |
| 987 | case LY_TYPE_INST: |
| 988 | return "instance-identifier"; |
| 989 | case LY_TYPE_LEAFREF: |
| 990 | return "leafref"; |
| 991 | case LY_TYPE_UNION: |
| 992 | return "union"; |
| 993 | case LY_TYPE_INT8: |
| 994 | return "int8"; |
| 995 | case LY_TYPE_INT16: |
| 996 | return "int16"; |
| 997 | case LY_TYPE_INT32: |
| 998 | return "int32"; |
| 999 | case LY_TYPE_INT64: |
| 1000 | return "int64"; |
| 1001 | default: |
| 1002 | return "unknown"; |
| 1003 | } |
| 1004 | } |
| 1005 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1006 | API const struct lysp_tpdf * |
| 1007 | lysp_node_typedefs(const struct lysp_node *node) |
| 1008 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1009 | switch (node->nodetype) { |
| 1010 | case LYS_CONTAINER: |
| 1011 | return ((struct lysp_node_container*)node)->typedefs; |
| 1012 | case LYS_LIST: |
| 1013 | return ((struct lysp_node_list*)node)->typedefs; |
| 1014 | case LYS_GROUPING: |
| 1015 | return ((struct lysp_grp*)node)->typedefs; |
| 1016 | case LYS_ACTION: |
| 1017 | return ((struct lysp_action*)node)->typedefs; |
| 1018 | case LYS_INOUT: |
| 1019 | return ((struct lysp_action_inout*)node)->typedefs; |
| 1020 | case LYS_NOTIF: |
| 1021 | return ((struct lysp_notif*)node)->typedefs; |
| 1022 | default: |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1023 | return NULL; |
| 1024 | } |
| 1025 | } |
| 1026 | |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1027 | API const struct lysp_grp * |
| 1028 | lysp_node_groupings(const struct lysp_node *node) |
| 1029 | { |
| 1030 | switch (node->nodetype) { |
| 1031 | case LYS_CONTAINER: |
| 1032 | return ((struct lysp_node_container*)node)->groupings; |
| 1033 | case LYS_LIST: |
| 1034 | return ((struct lysp_node_list*)node)->groupings; |
| 1035 | case LYS_GROUPING: |
| 1036 | return ((struct lysp_grp*)node)->groupings; |
| 1037 | case LYS_ACTION: |
| 1038 | return ((struct lysp_action*)node)->groupings; |
| 1039 | case LYS_INOUT: |
| 1040 | return ((struct lysp_action_inout*)node)->groupings; |
| 1041 | case LYS_NOTIF: |
| 1042 | return ((struct lysp_notif*)node)->groupings; |
| 1043 | default: |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | } |
| 1047 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1048 | struct lysp_action ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1049 | lysp_node_actions_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1050 | { |
| 1051 | assert(node); |
| 1052 | switch (node->nodetype) { |
| 1053 | case LYS_CONTAINER: |
| 1054 | return &((struct lysp_node_container*)node)->actions; |
| 1055 | case LYS_LIST: |
| 1056 | return &((struct lysp_node_list*)node)->actions; |
| 1057 | case LYS_GROUPING: |
| 1058 | return &((struct lysp_grp*)node)->actions; |
| 1059 | case LYS_AUGMENT: |
| 1060 | return &((struct lysp_augment*)node)->actions; |
| 1061 | default: |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | } |
| 1065 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1066 | API const struct lysp_action * |
| 1067 | lysp_node_actions(const struct lysp_node *node) |
| 1068 | { |
| 1069 | struct lysp_action **actions; |
| 1070 | actions = lysp_node_actions_p((struct lysp_node*)node); |
| 1071 | if (actions) { |
| 1072 | return *actions; |
| 1073 | } else { |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | } |
| 1077 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1078 | struct lysp_notif ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1079 | lysp_node_notifs_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1080 | { |
| 1081 | assert(node); |
| 1082 | switch (node->nodetype) { |
| 1083 | case LYS_CONTAINER: |
| 1084 | return &((struct lysp_node_container*)node)->notifs; |
| 1085 | case LYS_LIST: |
| 1086 | return &((struct lysp_node_list*)node)->notifs; |
| 1087 | case LYS_GROUPING: |
| 1088 | return &((struct lysp_grp*)node)->notifs; |
| 1089 | case LYS_AUGMENT: |
| 1090 | return &((struct lysp_augment*)node)->notifs; |
| 1091 | default: |
| 1092 | return NULL; |
| 1093 | } |
| 1094 | } |
| 1095 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1096 | API const struct lysp_notif * |
| 1097 | lysp_node_notifs(const struct lysp_node *node) |
| 1098 | { |
| 1099 | struct lysp_notif **notifs; |
| 1100 | notifs = lysp_node_notifs_p((struct lysp_node*)node); |
| 1101 | if (notifs) { |
| 1102 | return *notifs; |
| 1103 | } else { |
| 1104 | return NULL; |
| 1105 | } |
| 1106 | } |
| 1107 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1108 | struct lysp_node ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1109 | lysp_node_children_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1110 | { |
| 1111 | assert(node); |
| 1112 | switch (node->nodetype) { |
| 1113 | case LYS_CONTAINER: |
| 1114 | return &((struct lysp_node_container*)node)->child; |
| 1115 | case LYS_CHOICE: |
| 1116 | return &((struct lysp_node_choice*)node)->child; |
| 1117 | case LYS_LIST: |
| 1118 | return &((struct lysp_node_list*)node)->child; |
| 1119 | case LYS_CASE: |
| 1120 | return &((struct lysp_node_case*)node)->child; |
| 1121 | case LYS_GROUPING: |
| 1122 | return &((struct lysp_grp*)node)->data; |
| 1123 | case LYS_AUGMENT: |
| 1124 | return &((struct lysp_augment*)node)->child; |
| 1125 | case LYS_INOUT: |
| 1126 | return &((struct lysp_action_inout*)node)->data; |
| 1127 | case LYS_NOTIF: |
| 1128 | return &((struct lysp_notif*)node)->data; |
| 1129 | default: |
| 1130 | return NULL; |
| 1131 | } |
| 1132 | } |
| 1133 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1134 | API const struct lysp_node * |
| 1135 | lysp_node_children(const struct lysp_node *node) |
| 1136 | { |
| 1137 | struct lysp_node **children; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1138 | |
| 1139 | if (!node) { |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1143 | children = lysp_node_children_p((struct lysp_node*)node); |
| 1144 | if (children) { |
| 1145 | return *children; |
| 1146 | } else { |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | struct lysc_action ** |
| 1152 | lysc_node_actions_p(struct lysc_node *node) |
| 1153 | { |
| 1154 | assert(node); |
| 1155 | switch (node->nodetype) { |
| 1156 | case LYS_CONTAINER: |
| 1157 | return &((struct lysc_node_container*)node)->actions; |
| 1158 | case LYS_LIST: |
| 1159 | return &((struct lysc_node_list*)node)->actions; |
| 1160 | default: |
| 1161 | return NULL; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | API const struct lysc_action * |
| 1166 | lysc_node_actions(const struct lysc_node *node) |
| 1167 | { |
| 1168 | struct lysc_action **actions; |
| 1169 | actions = lysc_node_actions_p((struct lysc_node*)node); |
| 1170 | if (actions) { |
| 1171 | return *actions; |
| 1172 | } else { |
| 1173 | return NULL; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | struct lysc_notif ** |
| 1178 | lysc_node_notifs_p(struct lysc_node *node) |
| 1179 | { |
| 1180 | assert(node); |
| 1181 | switch (node->nodetype) { |
| 1182 | case LYS_CONTAINER: |
| 1183 | return &((struct lysc_node_container*)node)->notifs; |
| 1184 | case LYS_LIST: |
| 1185 | return &((struct lysc_node_list*)node)->notifs; |
| 1186 | default: |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | API const struct lysc_notif * |
| 1192 | lysc_node_notifs(const struct lysc_node *node) |
| 1193 | { |
| 1194 | struct lysc_notif **notifs; |
| 1195 | notifs = lysc_node_notifs_p((struct lysc_node*)node); |
| 1196 | if (notifs) { |
| 1197 | return *notifs; |
| 1198 | } else { |
| 1199 | return NULL; |
| 1200 | } |
| 1201 | } |
| 1202 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1203 | struct lysc_node ** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1204 | lysc_node_children_p(const struct lysc_node *node, uint16_t flags) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1205 | { |
| 1206 | assert(node); |
| 1207 | switch (node->nodetype) { |
| 1208 | case LYS_CONTAINER: |
| 1209 | return &((struct lysc_node_container*)node)->child; |
| 1210 | case LYS_CHOICE: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1211 | if (((struct lysc_node_choice*)node)->cases) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1212 | return &((struct lysc_node_choice*)node)->cases->child; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1213 | } else { |
| 1214 | return NULL; |
| 1215 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 1216 | case LYS_CASE: |
| 1217 | return &((struct lysc_node_case*)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1218 | case LYS_LIST: |
| 1219 | return &((struct lysc_node_list*)node)->child; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1220 | case LYS_ACTION: |
| 1221 | if (flags & LYS_CONFIG_R) { |
| 1222 | return &((struct lysc_action*)node)->output.data; |
| 1223 | } else { |
| 1224 | /* LYS_CONFIG_W, but also the default case */ |
| 1225 | return &((struct lysc_action*)node)->input.data; |
| 1226 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1227 | case LYS_NOTIF: |
| 1228 | return &((struct lysc_notif*)node)->data; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1229 | default: |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | } |
| 1233 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1234 | API const struct lysc_node * |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1235 | lysc_node_children(const struct lysc_node *node, uint16_t flags) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1236 | { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1237 | struct lysc_node **children; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1238 | |
| 1239 | if (!node) { |
| 1240 | return NULL; |
| 1241 | } |
| 1242 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1243 | children = lysc_node_children_p((struct lysc_node*)node, flags); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1244 | if (children) { |
| 1245 | return *children; |
| 1246 | } else { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1247 | return NULL; |
| 1248 | } |
| 1249 | } |
| 1250 | |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1251 | struct lys_module * |
| 1252 | lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod) |
| 1253 | { |
| 1254 | unsigned int u; |
| 1255 | |
| 1256 | for (u = 0; u < ctx->list.count; ++u) { |
| 1257 | if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) { |
| 1258 | return ((struct lys_module*)ctx->list.objs[u]); |
| 1259 | } |
| 1260 | } |
| 1261 | return NULL; |
| 1262 | } |
| 1263 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1264 | unsigned int |
| 1265 | lysp_ext_instance_iter(struct lysp_ext_instance *ext, unsigned int index, LYEXT_SUBSTMT substmt) |
| 1266 | { |
| 1267 | LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL); |
| 1268 | |
| 1269 | for (; index < LY_ARRAY_SIZE(ext); index++) { |
| 1270 | if (ext[index].insubstmt == substmt) { |
| 1271 | return index; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | return LY_ARRAY_SIZE(ext); |
| 1276 | } |
| 1277 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 1278 | /** |
| 1279 | * @brief Schema mapping of YANG modules to prefixes in values. |
| 1280 | * |
| 1281 | * Implementation of ly_clb_get_prefix. Inverse function to lys_resolve_prefix. |
| 1282 | * |
| 1283 | * In this case the @p mod is searched in the list of imports and the import's prefix |
| 1284 | * (not the module's itself) prefix is returned. |
| 1285 | */ |
| 1286 | const char * |
| 1287 | lys_get_prefix(const struct lys_module *mod, void *private) |
| 1288 | { |
| 1289 | struct lys_module *context_mod = (struct lys_module*)private; |
| 1290 | unsigned int u; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1291 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 1292 | if (context_mod == mod) { |
| 1293 | return context_mod->prefix; |
| 1294 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 1295 | LY_ARRAY_FOR(context_mod->compiled->imports, u) { |
| 1296 | if (context_mod->compiled->imports[u].module == mod) { |
| 1297 | /* match */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 1298 | return context_mod->compiled->imports[u].prefix; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | return NULL; |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * @brief Schema mapping of prefix in values to YANG modules (imports). |
| 1307 | * |
| 1308 | * Implementation of ly_clb_resolve_prefix. Inverse function to lys_get_prefix(). |
| 1309 | * |
| 1310 | * In this case the @p prefix is searched in the list of imports' prefixes (not the prefixes of the imported modules themselves). |
| 1311 | */ |
| 1312 | const struct lys_module * |
| 1313 | lys_resolve_prefix(struct ly_ctx *UNUSED(ctx), const char *prefix, size_t prefix_len, void *private) |
| 1314 | { |
| 1315 | return lys_module_find_prefix((const struct lys_module*)private, prefix, prefix_len); |
| 1316 | } |