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