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