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