Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 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 | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 17 | #include "tree_schema.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 18 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 20 | #include <dirent.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 21 | #include <errno.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 23 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 26 | #include <sys/stat.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 27 | #include <unistd.h> |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 28 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 29 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 30 | #include "compat.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 31 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 32 | #include "dict.h" |
| 33 | #include "log.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 34 | #include "parser.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 35 | #include "parser_internal.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 36 | #include "parser_schema.h" |
| 37 | #include "set.h" |
| 38 | #include "tree.h" |
| 39 | #include "tree_schema_internal.h" |
| 40 | #include "xpath.h" |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 41 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 42 | API const struct lysc_node * |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 43 | lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 44 | { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 45 | const struct lysc_node *next = NULL; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 46 | struct lysc_node **snode; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 47 | ly_bool action_flag = 0, notif_flag = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 48 | const struct lysc_action *actions; |
| 49 | const struct lysc_notif *notifs; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 50 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 51 | |
| 52 | LY_CHECK_ARG_RET(NULL, parent || module, NULL); |
| 53 | |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 54 | next: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 55 | if (!last) { |
| 56 | /* first call */ |
| 57 | |
| 58 | /* get know where to start */ |
| 59 | if (parent) { |
| 60 | /* schema subtree */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 61 | if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 62 | if (((struct lysc_node_choice *)parent)->cases) { |
| 63 | next = last = (const struct lysc_node *)&((struct lysc_node_choice *)parent)->cases[0]; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 64 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 65 | } else { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 66 | snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 67 | /* do not return anything if the node does not have any children */ |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 68 | if (snode && *snode) { |
| 69 | next = last = *snode; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 70 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 71 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 72 | } else { |
| 73 | /* top level data */ |
| 74 | next = last = module->data; |
| 75 | } |
| 76 | if (!next) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 77 | /* try to get action or notification */ |
| 78 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 79 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 80 | /* test if the next can be returned */ |
| 81 | goto check; |
| 82 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 83 | } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 84 | action_flag = 1; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 85 | if (last->parent) { |
| 86 | actions = lysc_node_actions(last->parent); |
| 87 | } else { |
| 88 | actions = module->rpcs; |
| 89 | } |
| 90 | LY_ARRAY_FOR(actions, u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 91 | if (&actions[u] == (struct lysc_action *)last) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 92 | break; |
| 93 | } |
| 94 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 95 | if (u + 1 < LY_ARRAY_COUNT(actions)) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 96 | next = (struct lysc_node *)(&actions[u + 1]); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 97 | } |
| 98 | goto repeat; |
| 99 | } else if (last->nodetype == LYS_NOTIF) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 100 | action_flag = notif_flag = 1; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 101 | if (last->parent) { |
| 102 | notifs = lysc_node_notifs(last->parent); |
| 103 | } else { |
| 104 | notifs = module->notifs; |
| 105 | } |
| 106 | LY_ARRAY_FOR(notifs, u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 107 | if (¬ifs[u] == (struct lysc_notif *)last) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 111 | if (u + 1 < LY_ARRAY_COUNT(notifs)) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 112 | next = (struct lysc_node *)(¬ifs[u + 1]); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 113 | } |
| 114 | goto repeat; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 115 | } else { |
| 116 | next = last->next; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 119 | repeat: |
| 120 | if (!next) { |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 121 | /* possibly go back to parent */ |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 122 | if (last && last->parent != parent) { |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 123 | last = last->parent; |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 124 | goto next; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 125 | } else if (!action_flag) { |
| 126 | action_flag = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 127 | next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 128 | } else if (!notif_flag) { |
| 129 | notif_flag = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 130 | next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 131 | } else { |
| 132 | return NULL; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 133 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 134 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 135 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 136 | check: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 137 | switch (next->nodetype) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 138 | case LYS_RPC: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 139 | case LYS_ACTION: |
| 140 | case LYS_NOTIF: |
| 141 | case LYS_LEAF: |
| 142 | case LYS_ANYXML: |
| 143 | case LYS_ANYDATA: |
| 144 | case LYS_LIST: |
| 145 | case LYS_LEAFLIST: |
| 146 | break; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 147 | case LYS_CASE: |
| 148 | if (options & LYS_GETNEXT_WITHCASE) { |
| 149 | break; |
| 150 | } else { |
| 151 | /* go into */ |
| 152 | next = ((struct lysc_node_case *)next)->child; |
| 153 | } |
| 154 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 155 | case LYS_CONTAINER: |
| 156 | if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) { |
| 157 | if (((struct lysc_node_container *)next)->child) { |
| 158 | /* go into */ |
| 159 | next = ((struct lysc_node_container *)next)->child; |
| 160 | } else { |
| 161 | next = next->next; |
| 162 | } |
| 163 | goto repeat; |
| 164 | } |
| 165 | break; |
| 166 | case LYS_CHOICE: |
| 167 | if (options & LYS_GETNEXT_WITHCHOICE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 168 | break; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 169 | } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) { |
| 170 | next = next->next; |
| 171 | } else { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 172 | /* go into */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 173 | if (options & LYS_GETNEXT_WITHCASE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 174 | next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 175 | } else { |
| 176 | next = ((struct lysc_node_choice *)next)->cases->child; |
| 177 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 178 | } |
| 179 | goto repeat; |
| 180 | default: |
| 181 | /* we should not be here */ |
Radek Krejci | b07b5c9 | 2019-04-08 10:56:37 +0200 | [diff] [blame] | 182 | LOGINT(module ? module->mod->ctx : parent->module->ctx); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | if (!(options & LYS_GETNEXT_NOSTATECHECK)) { |
| 187 | /* check if the node is disabled by if-feature */ |
Radek Krejci | fab954b | 2019-09-11 11:25:14 +0200 | [diff] [blame] | 188 | if (lysc_node_is_disabled(next, 0)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 189 | next = next->next; |
| 190 | goto repeat; |
| 191 | } |
| 192 | } |
| 193 | return next; |
| 194 | } |
| 195 | |
| 196 | API const struct lysc_node * |
Radek Krejci | 09e8d0a | 2019-11-17 12:14:15 +0800 | [diff] [blame] | 197 | lys_find_node(struct ly_ctx *ctx, const struct lysc_node *context_node, const char *qpath) |
| 198 | { |
| 199 | const char *id = qpath; |
| 200 | const char *prefix, *name; |
| 201 | size_t prefix_len, name_len; |
Radek Krejci | 09e8d0a | 2019-11-17 12:14:15 +0800 | [diff] [blame] | 202 | const struct lysc_node *node = context_node; |
| 203 | struct lys_module *mod = NULL; |
| 204 | |
| 205 | LY_CHECK_ARG_RET(ctx, qpath, NULL); |
| 206 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 207 | while (*id) { |
Radek Krejci | 09e8d0a | 2019-11-17 12:14:15 +0800 | [diff] [blame] | 208 | if (id[0] == '/') { |
| 209 | ++id; |
| 210 | } |
| 211 | /* precess ".." in relative paths */ |
| 212 | while (!strncmp("../", id, 3)) { |
| 213 | id += 3; |
| 214 | if (!node) { |
| 215 | LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - too many \"..\" in the path.", qpath); |
| 216 | return NULL; |
| 217 | } |
| 218 | node = node->parent; |
| 219 | } |
| 220 | |
| 221 | if (ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len) != LY_SUCCESS) { |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 222 | LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - invalid nodeid \"%.*s\".", qpath, id - qpath, qpath); |
Radek Krejci | 09e8d0a | 2019-11-17 12:14:15 +0800 | [diff] [blame] | 223 | return NULL; |
| 224 | } |
| 225 | if (prefix) { |
| 226 | if (context_node) { |
| 227 | mod = lys_module_find_prefix(context_node->module, prefix, prefix_len); |
| 228 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 229 | for (uint32_t u = 0; u < ctx->list.count; ++u) { |
Radek Krejci | 09e8d0a | 2019-11-17 12:14:15 +0800 | [diff] [blame] | 230 | if (!ly_strncmp(((struct lys_module *)ctx->list.objs[u])->name, prefix, prefix_len)) { |
| 231 | struct lys_module *m = (struct lys_module *)ctx->list.objs[u]; |
| 232 | if (mod) { |
| 233 | if (m->implemented) { |
| 234 | mod = m; |
| 235 | break; |
| 236 | } else if (m->latest_revision) { |
| 237 | mod = m; |
| 238 | } |
| 239 | } else { |
| 240 | mod = m; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | if (!mod) { |
| 247 | LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find module connected with the prefix of the node \"%.*s\".", |
| 248 | id - qpath, qpath); |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | node = lys_find_child(node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 253 | if (!node) { |
| 254 | LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find \"%.*s\".", id - qpath, qpath); |
| 255 | return NULL; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return node; |
| 260 | } |
| 261 | |
| 262 | API const struct lysc_node * |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 263 | lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 264 | uint16_t nodetype, uint32_t options) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 265 | { |
| 266 | const struct lysc_node *node = NULL; |
| 267 | |
| 268 | LY_CHECK_ARG_RET(NULL, module, name, NULL); |
| 269 | if (!nodetype) { |
| 270 | nodetype = 0xffff; |
| 271 | } |
| 272 | |
| 273 | while ((node = lys_getnext(node, parent, module->compiled, options))) { |
| 274 | if (!(node->nodetype & nodetype)) { |
| 275 | continue; |
| 276 | } |
| 277 | if (node->module != module) { |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | if (name_len) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 282 | if (!ly_strncmp(node->name, name, name_len)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 283 | return node; |
| 284 | } |
| 285 | } else { |
| 286 | if (!strcmp(node->name, name)) { |
| 287 | return node; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | return NULL; |
| 292 | } |
| 293 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 294 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 295 | lys_atomize_xpath(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set) |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 296 | { |
| 297 | LY_ERR ret = LY_SUCCESS; |
| 298 | struct lyxp_set xp_set; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 299 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 300 | uint32_t i; |
| 301 | |
| 302 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 303 | if (!(options & LYXP_SCNODE_ALL)) { |
| 304 | options = LYXP_SCNODE; |
| 305 | } |
| 306 | |
| 307 | memset(&xp_set, 0, sizeof xp_set); |
| 308 | |
| 309 | /* compile expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 310 | ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp); |
| 311 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 312 | |
| 313 | /* atomize expression */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 314 | ret = lyxp_atomize(exp, LY_PREF_JSON, ctx_node->module, ctx_node, LYXP_NODE_ELEM, &xp_set, options); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 315 | LY_CHECK_GOTO(ret, cleanup); |
| 316 | |
| 317 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 318 | ret = ly_set_new(set); |
| 319 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 320 | |
| 321 | /* transform into ly_set */ |
| 322 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
| 323 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup); |
| 324 | (*set)->size = xp_set.used; |
| 325 | |
| 326 | for (i = 0; i < xp_set.used; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 327 | if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 328 | ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, LY_SET_OPT_USEASLIST, NULL); |
| 329 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | cleanup: |
| 334 | lyxp_set_free_content(&xp_set); |
| 335 | lyxp_expr_free(ctx_node->module->ctx, exp); |
| 336 | return ret; |
| 337 | } |
| 338 | |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 339 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 340 | lys_find_xpath(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set) |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 341 | { |
| 342 | LY_ERR ret = LY_SUCCESS; |
| 343 | struct lyxp_set xp_set; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 344 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 345 | uint32_t i; |
| 346 | |
| 347 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 348 | if (!(options & LYXP_SCNODE_ALL)) { |
| 349 | options = LYXP_SCNODE; |
| 350 | } |
| 351 | |
| 352 | memset(&xp_set, 0, sizeof xp_set); |
| 353 | |
| 354 | /* compile expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 355 | ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp); |
| 356 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 357 | |
| 358 | /* atomize expression */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 359 | ret = lyxp_atomize(exp, LY_PREF_JSON, ctx_node->module, ctx_node, LYXP_NODE_ELEM, &xp_set, options); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 360 | LY_CHECK_GOTO(ret, cleanup); |
| 361 | |
| 362 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 363 | ret = ly_set_new(set); |
| 364 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 365 | |
| 366 | /* transform into ly_set */ |
| 367 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
| 368 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup); |
| 369 | (*set)->size = xp_set.used; |
| 370 | |
| 371 | for (i = 0; i < xp_set.used; ++i) { |
| 372 | if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 373 | ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, LY_SET_OPT_USEASLIST, NULL); |
| 374 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | cleanup: |
| 379 | lyxp_set_free_content(&xp_set); |
| 380 | lyxp_expr_free(ctx_node->module->ctx, exp); |
| 381 | return ret; |
| 382 | } |
| 383 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 384 | char * |
| 385 | lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 386 | size_t buflen) |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 387 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 388 | const struct lysc_node *iter; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 389 | char *path = NULL; |
| 390 | int len = 0; |
| 391 | |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 392 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 393 | if (buffer) { |
| 394 | LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL); |
| 395 | } |
| 396 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 397 | switch (pathtype) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 398 | case LYSC_PATH_LOG: |
Michal Vasko | 65de040 | 2020-08-03 16:34:19 +0200 | [diff] [blame] | 399 | case LYSC_PATH_DATA: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 400 | for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) { |
Michal Vasko | 11deea1 | 2020-08-05 13:54:50 +0200 | [diff] [blame] | 401 | char *s, *id; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 402 | const char *slash; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 403 | |
Michal Vasko | 65de040 | 2020-08-03 16:34:19 +0200 | [diff] [blame] | 404 | if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 405 | /* schema-only node */ |
| 406 | continue; |
| 407 | } |
| 408 | |
Michal Vasko | 11deea1 | 2020-08-05 13:54:50 +0200 | [diff] [blame] | 409 | s = buffer ? strdup(buffer) : path; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 410 | id = strdup(iter->name); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 411 | if (parent && (iter->parent == parent)) { |
| 412 | slash = ""; |
| 413 | } else { |
| 414 | slash = "/"; |
| 415 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 416 | if (!iter->parent || iter->parent->module != iter->module) { |
| 417 | /* print prefix */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 418 | if (buffer) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 419 | len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 420 | } else { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 421 | len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 422 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 423 | } else { |
| 424 | /* prefix is the same as in parent */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 425 | if (buffer) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 426 | len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 427 | } else { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 428 | len = asprintf(&path, "%s%s%s", slash, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 429 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 430 | } |
| 431 | free(s); |
| 432 | free(id); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 433 | |
| 434 | if (buffer && buflen <= (size_t)len) { |
| 435 | /* not enough space in buffer */ |
| 436 | break; |
| 437 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | if (len < 0) { |
| 441 | free(path); |
| 442 | path = NULL; |
| 443 | } else if (len == 0) { |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 444 | if (buffer) { |
| 445 | strcpy(buffer, "/"); |
| 446 | } else { |
| 447 | path = strdup("/"); |
| 448 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 449 | } |
| 450 | break; |
| 451 | } |
| 452 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 453 | if (buffer) { |
| 454 | return buffer; |
| 455 | } else { |
| 456 | return path; |
| 457 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 460 | API char * |
| 461 | lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 462 | { |
| 463 | return lysc_path_until(node, NULL, pathtype, buffer, buflen); |
| 464 | } |
| 465 | |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 466 | API LY_ERR |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 467 | lysc_feature_value(const struct lysc_feature *feature) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 468 | { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 469 | LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL); |
| 470 | return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 473 | uint8_t |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 474 | lysc_iff_getop(uint8_t *list, size_t pos) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 475 | { |
| 476 | uint8_t *item; |
| 477 | uint8_t mask = 3, result; |
| 478 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 479 | item = &list[pos / 4]; |
| 480 | result = (*item) & (mask << 2 * (pos % 4)); |
| 481 | return result >> 2 * (pos % 4); |
| 482 | } |
| 483 | |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 484 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 485 | lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 486 | { |
| 487 | uint8_t op; |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 488 | LY_ERR a, b; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 489 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 490 | op = lysc_iff_getop(iff->expr, *index_e); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 491 | (*index_e)++; |
| 492 | |
| 493 | switch (op) { |
| 494 | case LYS_IFF_F: |
| 495 | /* resolve feature */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 496 | return lysc_feature_value(iff->features[(*index_f)++]); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 497 | case LYS_IFF_NOT: |
| 498 | /* invert result */ |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 499 | return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 500 | case LYS_IFF_AND: |
| 501 | case LYS_IFF_OR: |
| 502 | a = lysc_iffeature_value_(iff, index_e, index_f); |
| 503 | b = lysc_iffeature_value_(iff, index_e, index_f); |
| 504 | if (op == LYS_IFF_AND) { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 505 | if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) { |
| 506 | return LY_SUCCESS; |
| 507 | } else { |
| 508 | return LY_ENOT; |
| 509 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 510 | } else { /* LYS_IFF_OR */ |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 511 | if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) { |
| 512 | return LY_SUCCESS; |
| 513 | } else { |
| 514 | return LY_ENOT; |
| 515 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 522 | API LY_ERR |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 523 | lysc_iffeature_value(const struct lysc_iffeature *iff) |
| 524 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 525 | size_t index_e = 0, index_f = 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 526 | |
| 527 | LY_CHECK_ARG_RET(NULL, iff, -1); |
| 528 | |
| 529 | if (iff->expr) { |
| 530 | return lysc_iffeature_value_(iff, &index_e, &index_f); |
| 531 | } |
| 532 | return 0; |
| 533 | } |
| 534 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 535 | /** |
| 536 | * @brief Enable/Disable the specified feature in the module. |
| 537 | * |
| 538 | * If the feature is already set to the desired value, LY_SUCCESS is returned. |
| 539 | * By changing the feature, also all the feature which depends on it via their |
| 540 | * if-feature statements are again evaluated (disabled if a if-feature statemen |
| 541 | * evaluates to false). |
| 542 | * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 543 | * @param[in] mod Module where to set (search for) the feature. |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 544 | * @param[in] name Name of the feature to set. Asterisk ('*') can be used to |
| 545 | * set all the features in the module. |
| 546 | * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable). |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 547 | * @param[in] skip_checks Flag to skip checking of if-features and just set @p value of the feature. |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 548 | * @return LY_ERR value. |
| 549 | */ |
| 550 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 551 | lys_feature_change(const struct lys_module *mod, const char *name, ly_bool value, ly_bool skip_checks) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 552 | { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 553 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 554 | ly_bool all = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 555 | LY_ARRAY_COUNT_TYPE u, disabled_count; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 556 | uint32_t changed_count; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 557 | struct lysc_feature *f, **df; |
| 558 | struct lysc_iffeature *iff; |
| 559 | struct ly_set *changed; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 560 | struct ly_ctx *ctx = mod->ctx; /* shortcut */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 561 | |
Radek Krejci | 6e67c40 | 2019-05-02 09:55:39 +0200 | [diff] [blame] | 562 | if (!strcmp(name, "*")) { |
| 563 | /* enable all */ |
| 564 | all = 1; |
| 565 | } |
| 566 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 567 | if (!mod->compiled) { |
| 568 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.", |
| 569 | mod->name); |
| 570 | return LY_EINVAL; |
| 571 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 572 | if (!mod->features) { |
Radek Krejci | 6e67c40 | 2019-05-02 09:55:39 +0200 | [diff] [blame] | 573 | if (all) { |
| 574 | /* no feature to enable */ |
| 575 | return LY_SUCCESS; |
| 576 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 577 | LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name); |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 578 | return LY_ENOTFOUND; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 581 | LY_CHECK_RET(ly_set_new(&changed)); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 582 | changed_count = 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 583 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 584 | run: |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 585 | for (disabled_count = u = 0; u < LY_ARRAY_COUNT(mod->features); ++u) { |
| 586 | f = &mod->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 587 | if (all || !strcmp(f->name, name)) { |
| 588 | if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) { |
| 589 | if (all) { |
| 590 | /* skip already set features */ |
| 591 | continue; |
| 592 | } else { |
| 593 | /* feature already set correctly */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 594 | goto cleanup; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | if (value) { /* enable */ |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 599 | if (!skip_checks) { |
| 600 | /* check referenced features if they are enabled */ |
| 601 | LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) { |
| 602 | if (lysc_iffeature_value(iff) == LY_ENOT) { |
| 603 | if (all) { |
| 604 | ++disabled_count; |
| 605 | goto next; |
| 606 | } else { |
| 607 | LOGERR(ctx, LY_EDENIED, |
| 608 | "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
| 609 | f->name); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 610 | ret = LY_EDENIED; |
| 611 | goto cleanup; |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 612 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | } |
| 616 | /* enable the feature */ |
| 617 | f->flags |= LYS_FENABLED; |
| 618 | } else { /* disable */ |
| 619 | /* disable the feature */ |
| 620 | f->flags &= ~LYS_FENABLED; |
| 621 | } |
| 622 | |
| 623 | /* remember the changed feature */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 624 | ret = ly_set_add(changed, f, LY_SET_OPT_USEASLIST, NULL); |
| 625 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 626 | |
| 627 | if (!all) { |
| 628 | /* stop in case changing a single feature */ |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | next: |
| 633 | ; |
| 634 | } |
| 635 | |
| 636 | if (!all && !changed->count) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 637 | LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 638 | ret = LY_ENOTFOUND; |
| 639 | goto cleanup; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 640 | } |
| 641 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 642 | if (value && all && disabled_count) { |
| 643 | if (changed_count == changed->count) { |
| 644 | /* no change in last run -> not able to enable all ... */ |
| 645 | /* ... print errors */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 646 | for (u = 0; disabled_count && u < LY_ARRAY_COUNT(mod->features); ++u) { |
| 647 | if (!(mod->features[u].flags & LYS_FENABLED)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 648 | LOGERR(ctx, LY_EDENIED, |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 649 | "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 650 | mod->features[u].name); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 651 | --disabled_count; |
| 652 | } |
| 653 | } |
| 654 | /* ... restore the original state */ |
| 655 | for (u = 0; u < changed->count; ++u) { |
| 656 | f = changed->objs[u]; |
| 657 | /* re-disable the feature */ |
| 658 | f->flags &= ~LYS_FENABLED; |
| 659 | } |
| 660 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 661 | ret = LY_EDENIED; |
| 662 | goto cleanup; |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 663 | } else { |
| 664 | /* we did some change in last run, try it again */ |
| 665 | changed_count = changed->count; |
| 666 | goto run; |
| 667 | } |
| 668 | } |
| 669 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 670 | /* reflect change(s) in the dependent features */ |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 671 | for (u = 0; !skip_checks && (u < changed->count); ++u) { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 672 | /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of |
| 673 | * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled |
| 674 | * is not done - by default, features are disabled and must be explicitely enabled. */ |
| 675 | f = changed->objs[u]; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 676 | LY_ARRAY_FOR(f->depfeatures, struct lysc_feature *, df) { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 677 | if (!((*df)->flags & LYS_FENABLED)) { |
| 678 | /* not enabled, nothing to do */ |
| 679 | continue; |
| 680 | } |
| 681 | /* check the feature's if-features which could change by the previous change of our feature */ |
| 682 | LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 683 | if (lysc_iffeature_value(iff) == LY_ENOT) { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 684 | /* the feature must be disabled now */ |
| 685 | (*df)->flags &= ~LYS_FENABLED; |
| 686 | /* add the feature into the list of changed features */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 687 | ret = ly_set_add(changed, *df, LY_SET_OPT_USEASLIST, NULL); |
| 688 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 689 | break; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
Michal Vasko | 87a97f4 | 2020-10-06 10:30:28 +0200 | [diff] [blame] | 695 | /* success */ |
| 696 | ++mod->ctx->module_set_id; |
| 697 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 698 | cleanup: |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 699 | ly_set_free(changed, NULL); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 700 | return ret; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 704 | lys_feature_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 705 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 706 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 707 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 708 | return lys_feature_change((struct lys_module *)module, feature, 1, 0); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 712 | lys_feature_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 713 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 714 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 715 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 716 | return lys_feature_change((struct lys_module *)module, feature, 0, 0); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 719 | API LY_ERR |
| 720 | lys_feature_enable_force(const struct lys_module *module, const char *feature) |
| 721 | { |
| 722 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
| 723 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 724 | return lys_feature_change((struct lys_module *)module, feature, 1, 1); |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | API LY_ERR |
| 728 | lys_feature_disable_force(const struct lys_module *module, const char *feature) |
| 729 | { |
| 730 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
| 731 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 732 | return lys_feature_change((struct lys_module *)module, feature, 0, 1); |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | API LY_ERR |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 736 | lys_feature_value(const struct lys_module *module, const char *feature) |
| 737 | { |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 738 | struct lysc_feature *f = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 739 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 740 | |
| 741 | LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 742 | |
| 743 | /* search for the specified feature */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 744 | LY_ARRAY_FOR(module->features, u) { |
| 745 | f = &module->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 746 | if (!strcmp(f->name, feature)) { |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 747 | break; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | /* feature definition not found */ |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 752 | if (!f) { |
| 753 | return LY_ENOTFOUND; |
| 754 | } |
| 755 | |
| 756 | /* feature disabled */ |
| 757 | if (!(f->flags & LYS_FENABLED)) { |
| 758 | return LY_ENOT; |
| 759 | } |
| 760 | |
| 761 | /* check referenced features if they are enabled */ |
| 762 | LY_ARRAY_FOR(f->iffeatures, u) { |
| 763 | if (lysc_iffeature_value(&f->iffeatures[u]) == LY_ENOT) { |
| 764 | /* if-feature disabled */ |
| 765 | return LY_ENOT; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /* feature enabled */ |
| 770 | return LY_SUCCESS; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 771 | } |
| 772 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 773 | API const struct lysc_node * |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 774 | lysc_node_is_disabled(const struct lysc_node *node, ly_bool recursive) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 775 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 776 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 777 | |
| 778 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 779 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 780 | do { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 781 | if (node->iffeatures) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 782 | /* check local if-features */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 783 | LY_ARRAY_FOR(node->iffeatures, u) { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 784 | if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 785 | return node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | if (!recursive) { |
| 791 | return NULL; |
| 792 | } |
| 793 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 794 | /* go through schema-only parents */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 795 | node = node->parent; |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 796 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 797 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 798 | return NULL; |
| 799 | } |
| 800 | |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 801 | API LY_ERR |
Radek Krejci | af9cd80 | 2020-10-06 21:59:47 +0200 | [diff] [blame^] | 802 | lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p) |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 803 | { |
Radek Krejci | af9cd80 | 2020-10-06 21:59:47 +0200 | [diff] [blame^] | 804 | struct lysc_action *act; |
| 805 | struct lysc_notif *notif; |
| 806 | |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 807 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
| 808 | |
Radek Krejci | af9cd80 | 2020-10-06 21:59:47 +0200 | [diff] [blame^] | 809 | switch (node->nodetype) { |
| 810 | case LYS_CONTAINER: |
| 811 | case LYS_CHOICE: |
| 812 | case LYS_CASE: |
| 813 | case LYS_LEAF: |
| 814 | case LYS_LEAFLIST: |
| 815 | case LYS_LIST: |
| 816 | case LYS_ANYXML: |
| 817 | case LYS_ANYDATA: |
| 818 | if (prev_priv_p) { |
| 819 | *prev_priv_p = node->priv; |
| 820 | } |
| 821 | ((struct lysc_node *)node)->priv = priv; |
| 822 | break; |
| 823 | case LYS_RPC: |
| 824 | case LYS_ACTION: |
| 825 | act = (struct lysc_action *)node; |
| 826 | if (prev_priv_p) { |
| 827 | *prev_priv_p = act->priv; |
| 828 | } |
| 829 | act->priv = priv; |
| 830 | break; |
| 831 | case LYS_NOTIF: |
| 832 | notif = (struct lysc_notif *)node; |
| 833 | if (prev_priv_p) { |
| 834 | *prev_priv_p = notif->priv; |
| 835 | } |
| 836 | notif->priv = priv; |
| 837 | break; |
| 838 | default: |
| 839 | return LY_EINVAL; |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 840 | } |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 841 | |
| 842 | return LY_SUCCESS; |
| 843 | } |
| 844 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 845 | API LY_ERR |
| 846 | lys_set_implemented(struct lys_module *mod) |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 847 | { |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 848 | LY_ERR ret = LY_SUCCESS, r; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 849 | struct lys_module *m; |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 850 | uint32_t i, idx; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 851 | |
| 852 | LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL); |
| 853 | |
| 854 | if (mod->implemented) { |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 855 | /* mod is already implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 856 | return LY_SUCCESS; |
| 857 | } |
| 858 | |
| 859 | /* we have module from the current context */ |
| 860 | m = ly_ctx_get_module_implemented(mod->ctx, mod->name); |
| 861 | if (m) { |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 862 | assert(m != mod); |
| 863 | |
| 864 | /* check collision with other implemented revision */ |
| 865 | LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).", |
| 866 | mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none"); |
| 867 | return LY_EDENIED; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 870 | /* add the module into newly implemented module set */ |
| 871 | LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, LY_SET_OPT_USEASLIST, NULL)); |
| 872 | |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 873 | /* mark the module implemented, check for collision was already done */ |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 874 | mod->implemented = 1; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 875 | |
| 876 | /* compile the schema */ |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 877 | ret = lys_compile(mod, 0); |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 878 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 879 | if (mod == mod->ctx->implementing.objs[0]) { |
| 880 | /* the first module being implemented, consolidate the set */ |
| 881 | if (ret) { |
| 882 | /* failure, full compile revert */ |
| 883 | for (i = 0; i < mod->ctx->list.count; ++i) { |
| 884 | m = mod->ctx->list.objs[i]; |
| 885 | if (ly_set_contains(&mod->ctx->implementing, m, &idx)) { |
| 886 | assert(m->implemented); |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 887 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 888 | /* make the module correctly non-implemented again */ |
| 889 | m->implemented = 0; |
| 890 | ly_set_rm_index(&mod->ctx->implementing, idx, NULL); |
| 891 | lys_precompile_augments_deviations_revert(mod->ctx, m); |
| 892 | } |
| 893 | |
| 894 | /* free the compiled version of the module, if any */ |
| 895 | lysc_module_free(m->compiled, NULL); |
| 896 | m->compiled = NULL; |
| 897 | |
| 898 | if (m->implemented) { |
| 899 | /* recompile, must succeed because it was already compiled; hide messages because any |
| 900 | * warnings were already printed, are not really relevant, and would hide the real error */ |
| 901 | uint32_t prev_lo = ly_log_options(0); |
| 902 | r = lys_compile(m, 0); |
| 903 | ly_log_options(prev_lo); |
| 904 | if (r) { |
| 905 | LOGERR(mod->ctx, r, "Recompilation of module \"%s\" failed.", m->name); |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | ly_set_erase(&mod->ctx->implementing, NULL); |
| 912 | } |
| 913 | return ret; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 914 | } |
| 915 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 916 | static LY_ERR |
| 917 | lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *modp) |
| 918 | { |
| 919 | struct lysp_import *imp; |
| 920 | struct lysp_include *inc; |
| 921 | LY_ARRAY_COUNT_TYPE u, v; |
| 922 | |
| 923 | modp->parsing = 1; |
| 924 | LY_ARRAY_FOR(modp->imports, u) { |
| 925 | imp = &modp->imports[u]; |
| 926 | if (!imp->module) { |
| 927 | LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)); |
| 928 | } |
| 929 | /* check for importing the same module twice */ |
| 930 | for (v = 0; v < u; ++v) { |
| 931 | if (imp->module == modp->imports[v].module) { |
| 932 | LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name); |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | LY_ARRAY_FOR(modp->includes, u) { |
| 937 | inc = &modp->includes[u]; |
| 938 | if (!inc->submodule) { |
| 939 | LY_CHECK_RET(lysp_load_submodule(pctx, inc)); |
| 940 | } |
| 941 | } |
| 942 | modp->parsing = 0; |
| 943 | |
| 944 | return LY_SUCCESS; |
| 945 | } |
| 946 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 947 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 948 | lys_parse_submodule(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 949 | LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *), |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 950 | void *check_data, struct lysp_submodule **submodule) |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 951 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 952 | LY_ERR ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 953 | struct lysp_submodule *submod = NULL, *latest_sp; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 954 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 955 | struct lys_yin_parser_ctx *yinctx = NULL; |
| 956 | struct lys_parser_ctx *pctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 957 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 958 | LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 959 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 960 | switch (format) { |
| 961 | case LYS_IN_YIN: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 962 | ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 963 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 964 | break; |
| 965 | case LYS_IN_YANG: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 966 | ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 967 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 968 | break; |
| 969 | default: |
David Sedlák | 4f2f5ba | 2019-08-15 13:18:48 +0200 | [diff] [blame] | 970 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 971 | ret = LY_EINVAL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 972 | break; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 973 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 974 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | f027df7 | 2020-09-15 13:00:28 +0200 | [diff] [blame] | 975 | assert(submod); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 976 | |
| 977 | /* make sure that the newest revision is at position 0 */ |
| 978 | lysp_sort_revisions(submod->revs); |
| 979 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 980 | /* decide the latest revision */ |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 981 | latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 982 | if (latest_sp) { |
| 983 | if (submod->revs) { |
| 984 | if (!latest_sp->revs) { |
| 985 | /* latest has no revision, so mod is anyway newer */ |
| 986 | submod->latest_revision = latest_sp->latest_revision; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 987 | /* the latest_sp is zeroed later when the new module is being inserted into the context */ |
| 988 | } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) { |
| 989 | submod->latest_revision = latest_sp->latest_revision; |
| 990 | /* the latest_sp is zeroed later when the new module is being inserted into the context */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 991 | } else { |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 992 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 993 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 994 | } else { |
| 995 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 996 | } |
| 997 | } else { |
| 998 | submod->latest_revision = 1; |
| 999 | } |
| 1000 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1001 | if (custom_check) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1002 | LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | if (latest_sp) { |
| 1006 | latest_sp->latest_revision = 0; |
| 1007 | } |
| 1008 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1009 | lys_parser_fill_filepath(ctx, in, &submod->filepath); |
| 1010 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1011 | /* resolve imports and includes */ |
| 1012 | LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error); |
| 1013 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1014 | /* remap possibly changed and reallocated typedefs and groupings list back to the main context */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1015 | memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| 1016 | memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1017 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1018 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1019 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1020 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1021 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1022 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1023 | *submodule = submod; |
| 1024 | return LY_SUCCESS; |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1025 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1026 | error: |
| 1027 | lysp_submodule_free(ctx, submod); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1028 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1029 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1030 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1031 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1032 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1033 | return ret; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1036 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1037 | lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1038 | LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data), |
| 1039 | void *check_data, struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1040 | { |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 1041 | struct lys_module *mod = NULL, *latest, *mod_dup; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1042 | LY_ERR ret; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1043 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1044 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 1045 | struct lys_yin_parser_ctx *yinctx = NULL; |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 1046 | struct lys_parser_ctx *pctx = NULL; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1047 | char *filename, *rev, *dot; |
| 1048 | size_t len; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1049 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1050 | LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1051 | if (module) { |
| 1052 | *module = NULL; |
| 1053 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1054 | |
| 1055 | mod = calloc(1, sizeof *mod); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1056 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1057 | mod->ctx = ctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1058 | |
| 1059 | switch (format) { |
| 1060 | case LYS_IN_YIN: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1061 | ret = yin_parse_module(&yinctx, in, mod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1062 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1063 | break; |
| 1064 | case LYS_IN_YANG: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1065 | ret = yang_parse_module(&yangctx, in, mod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1066 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1067 | break; |
| 1068 | default: |
| 1069 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1070 | ret = LY_EINVAL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1071 | break; |
| 1072 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 1073 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1074 | |
| 1075 | /* make sure that the newest revision is at position 0 */ |
| 1076 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1077 | if (mod->parsed->revs) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1078 | LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1079 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1080 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1081 | /* decide the latest revision */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1082 | latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1083 | if (latest) { |
| 1084 | if (mod->revision) { |
| 1085 | if (!latest->revision) { |
| 1086 | /* latest has no revision, so mod is anyway newer */ |
| 1087 | mod->latest_revision = latest->latest_revision; |
| 1088 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1089 | } else if (strcmp(mod->revision, latest->revision) > 0) { |
| 1090 | mod->latest_revision = latest->latest_revision; |
| 1091 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1092 | } else { |
| 1093 | latest = NULL; |
| 1094 | } |
| 1095 | } else { |
| 1096 | latest = NULL; |
| 1097 | } |
| 1098 | } else { |
| 1099 | mod->latest_revision = 1; |
| 1100 | } |
| 1101 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1102 | if (custom_check) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1103 | LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1106 | if (implement) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1107 | /* mark the loaded module implemented */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1108 | if (ly_ctx_get_module_implemented(ctx, mod->name)) { |
| 1109 | LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1110 | ret = LY_EDENIED; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1111 | goto error; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 1112 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1115 | /* check for duplicity in the context */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1116 | mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1117 | if (mod_dup) { |
| 1118 | if (mod_dup->parsed) { |
| 1119 | /* error */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1120 | if (mod->parsed->revs) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1121 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.", |
| 1122 | mod->name, mod->parsed->revs[0].date); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1123 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1124 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.", |
| 1125 | mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1126 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1127 | ret = LY_EEXIST; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1128 | goto error; |
| 1129 | } else { |
| 1130 | /* add the parsed data to the currently compiled-only module in the context */ |
| 1131 | mod_dup->parsed = mod->parsed; |
| 1132 | mod_dup->parsed->mod = mod_dup; |
| 1133 | mod->parsed = NULL; |
| 1134 | lys_module_free(mod, NULL); |
| 1135 | mod = mod_dup; |
| 1136 | goto finish_parsing; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1137 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1138 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1139 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1140 | switch (in->type) { |
| 1141 | case LY_IN_FILEPATH: |
| 1142 | /* check that name and revision match filename */ |
| 1143 | filename = strrchr(in->method.fpath.filepath, '/'); |
| 1144 | if (!filename) { |
| 1145 | filename = in->method.fpath.filepath; |
| 1146 | } else { |
| 1147 | filename++; |
| 1148 | } |
| 1149 | rev = strchr(filename, '@'); |
| 1150 | dot = strrchr(filename, '.'); |
| 1151 | |
| 1152 | /* name */ |
| 1153 | len = strlen(mod->name); |
| 1154 | if (strncmp(filename, mod->name, len) || |
| 1155 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
| 1156 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name); |
| 1157 | } |
| 1158 | if (rev) { |
| 1159 | len = dot - ++rev; |
| 1160 | if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) { |
| 1161 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
| 1162 | mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | break; |
| 1167 | case LY_IN_FD: |
| 1168 | case LY_IN_FILE: |
| 1169 | case LY_IN_MEMORY: |
| 1170 | /* nothing special to do */ |
| 1171 | break; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1172 | case LY_IN_ERROR: |
| 1173 | LOGINT(ctx); |
| 1174 | ret = LY_EINT; |
| 1175 | goto error; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1176 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1177 | |
| 1178 | lys_parser_fill_filepath(ctx, in, &mod->filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1179 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1180 | if (!implement) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1181 | /* pre-compile features and identities of the module */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1182 | LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->features), error); |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1183 | LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->identities, &mod->identities), error); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | if (latest) { |
| 1187 | latest->latest_revision = 0; |
| 1188 | } |
| 1189 | |
| 1190 | /* add into context */ |
| 1191 | ret = ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST, NULL); |
| 1192 | LY_CHECK_GOTO(ret, error); |
| 1193 | ctx->module_set_id++; |
| 1194 | |
| 1195 | finish_parsing: |
| 1196 | /* resolve imports and includes */ |
| 1197 | LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx); |
| 1198 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1199 | if (!implement) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1200 | /* pre-compile features and identities of any submodules */ |
| 1201 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 1202 | LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->includes[u].submodule->features, |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1203 | &mod->features), error); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1204 | LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->includes[u].submodule->identities, |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1205 | &mod->identities), error); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | /* check name collisions - typedefs and TODO groupings */ |
| 1210 | LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx); |
| 1211 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1212 | if (implement) { |
| 1213 | /* implement (compile) */ |
| 1214 | LY_CHECK_GOTO(ret = lys_set_implemented(mod), error_ctx); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | if (format == LYS_IN_YANG) { |
| 1218 | yang_parser_ctx_free(yangctx); |
| 1219 | } else { |
| 1220 | yin_parser_ctx_free(yinctx); |
| 1221 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1222 | if (module) { |
| 1223 | *module = mod; |
| 1224 | } |
| 1225 | return LY_SUCCESS; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1226 | |
| 1227 | error_ctx: |
| 1228 | ly_set_rm(&ctx->list, mod, NULL); |
| 1229 | error: |
| 1230 | lys_module_free(mod, NULL); |
| 1231 | if (pctx) { |
| 1232 | ly_set_erase(&pctx->tpdfs_nodes, NULL); |
| 1233 | } |
| 1234 | if (format == LYS_IN_YANG) { |
| 1235 | yang_parser_ctx_free(yangctx); |
| 1236 | } else { |
| 1237 | yin_parser_ctx_free(yinctx); |
| 1238 | } |
| 1239 | |
| 1240 | return ret; |
| 1241 | } |
| 1242 | |
| 1243 | API LY_ERR |
| 1244 | lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module) |
| 1245 | { |
| 1246 | if (module) { |
| 1247 | *module = NULL; |
| 1248 | } |
| 1249 | LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL); |
| 1250 | |
| 1251 | /* remember input position */ |
| 1252 | in->func_start = in->current; |
| 1253 | |
| 1254 | return lys_create_module(ctx, in, format, 1, NULL, NULL, (struct lys_module **)module); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1257 | API LY_ERR |
| 1258 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1259 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1260 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1261 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1262 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1263 | LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | 65639b9 | 2018-11-27 10:51:37 +0100 | [diff] [blame] | 1264 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1265 | LY_CHECK_ERR_RET(ret = ly_in_new_memory(data, &in), LOGERR(ctx, ret, "Unable to create input handler."), ret); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1266 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1267 | ret = lys_parse(ctx, in, format, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1268 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1269 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1270 | return ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1271 | } |
| 1272 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1273 | API LY_ERR |
| 1274 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1275 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1276 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1277 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1278 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1279 | LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1280 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1281 | LY_CHECK_ERR_RET(ret = ly_in_new_fd(fd, &in), LOGERR(ctx, ret, "Unable to create input handler."), ret); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1282 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1283 | ret = lys_parse(ctx, in, format, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1284 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1285 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1286 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1289 | API LY_ERR |
| 1290 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1291 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1292 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1293 | struct ly_in *in = NULL; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1294 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1295 | LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1296 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1297 | LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in), |
| 1298 | LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1299 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1300 | ret = lys_parse(ctx, in, format, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1301 | ly_in_free(in, 0); |
| 1302 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1303 | return ret; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | API LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1307 | lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1308 | char **localfile, LYS_INFORMAT *format) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1309 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1310 | LY_ERR ret = LY_EMEM; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1311 | size_t len, flen, match_len = 0, dir_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1312 | ly_bool implicit_cwd = 0; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1313 | char *wd, *wn = NULL; |
| 1314 | DIR *dir = NULL; |
| 1315 | struct dirent *file; |
| 1316 | char *match_name = NULL; |
| 1317 | LYS_INFORMAT format_aux, match_format = 0; |
| 1318 | struct ly_set *dirs; |
| 1319 | struct stat st; |
| 1320 | |
| 1321 | LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| 1322 | |
| 1323 | /* start to fill the dir fifo with the context's search path (if set) |
| 1324 | * and the current working directory */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1325 | LY_CHECK_RET(ly_set_new(&dirs)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1326 | |
| 1327 | len = strlen(name); |
| 1328 | if (cwd) { |
| 1329 | wd = get_current_dir_name(); |
| 1330 | if (!wd) { |
| 1331 | LOGMEM(NULL); |
| 1332 | goto cleanup; |
| 1333 | } else { |
| 1334 | /* add implicit current working directory (./) to be searched, |
| 1335 | * this directory is not searched recursively */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1336 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 1337 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1338 | implicit_cwd = 1; |
| 1339 | } |
| 1340 | } |
| 1341 | if (searchpaths) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1342 | for (uint64_t i = 0; searchpaths[i]; i++) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1343 | /* check for duplicities with the implicit current working directory */ |
| 1344 | if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| 1345 | implicit_cwd = 0; |
| 1346 | continue; |
| 1347 | } |
| 1348 | wd = strdup(searchpaths[i]); |
| 1349 | if (!wd) { |
| 1350 | LOGMEM(NULL); |
| 1351 | goto cleanup; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1352 | } else { |
| 1353 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 1354 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | wd = NULL; |
| 1359 | |
| 1360 | /* start searching */ |
| 1361 | while (dirs->count) { |
| 1362 | free(wd); |
| 1363 | free(wn); wn = NULL; |
| 1364 | |
| 1365 | dirs->count--; |
| 1366 | wd = (char *)dirs->objs[dirs->count]; |
| 1367 | dirs->objs[dirs->count] = NULL; |
| 1368 | LOGVRB("Searching for \"%s\" in %s.", name, wd); |
| 1369 | |
| 1370 | if (dir) { |
| 1371 | closedir(dir); |
| 1372 | } |
| 1373 | dir = opendir(wd); |
| 1374 | dir_len = strlen(wd); |
| 1375 | if (!dir) { |
| 1376 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 1377 | } else { |
| 1378 | while ((file = readdir(dir))) { |
| 1379 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 1380 | /* skip . and .. */ |
| 1381 | continue; |
| 1382 | } |
| 1383 | free(wn); |
| 1384 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 1385 | LOGMEM(NULL); |
| 1386 | goto cleanup; |
| 1387 | } |
| 1388 | if (stat(wn, &st) == -1) { |
| 1389 | LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)", |
| 1390 | file->d_name, wd, strerror(errno)); |
| 1391 | continue; |
| 1392 | } |
| 1393 | if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) { |
| 1394 | /* we have another subdirectory in searchpath to explore, |
| 1395 | * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1396 | ret = ly_set_add(dirs, wn, 0, NULL); |
| 1397 | LY_CHECK_GOTO(ret, cleanup); |
| 1398 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1399 | /* continue with the next item in current directory */ |
| 1400 | wn = NULL; |
| 1401 | continue; |
| 1402 | } else if (!S_ISREG(st.st_mode)) { |
| 1403 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 1404 | continue; |
| 1405 | } |
| 1406 | |
| 1407 | /* here we know that the item is a file which can contain a module */ |
| 1408 | if (strncmp(name, file->d_name, len) || |
| 1409 | (file->d_name[len] != '.' && file->d_name[len] != '@')) { |
| 1410 | /* different filename than the module we search for */ |
| 1411 | continue; |
| 1412 | } |
| 1413 | |
| 1414 | /* get type according to filename suffix */ |
| 1415 | flen = strlen(file->d_name); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1416 | if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1417 | format_aux = LYS_IN_YANG; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1418 | /* TODO YIN parser |
| 1419 | } else if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| 1420 | format_aux = LYS_IN_YIN; |
| 1421 | */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1422 | } else { |
| 1423 | /* not supportde suffix/file format */ |
| 1424 | continue; |
| 1425 | } |
| 1426 | |
| 1427 | if (revision) { |
| 1428 | /* we look for the specific revision, try to get it from the filename */ |
| 1429 | if (file->d_name[len] == '@') { |
| 1430 | /* check revision from the filename */ |
| 1431 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 1432 | /* another revision */ |
| 1433 | continue; |
| 1434 | } else { |
| 1435 | /* exact revision */ |
| 1436 | free(match_name); |
| 1437 | match_name = wn; |
| 1438 | wn = NULL; |
| 1439 | match_len = dir_len + 1 + len; |
| 1440 | match_format = format_aux; |
| 1441 | goto success; |
| 1442 | } |
| 1443 | } else { |
| 1444 | /* continue trying to find exact revision match, use this only if not found */ |
| 1445 | free(match_name); |
| 1446 | match_name = wn; |
| 1447 | wn = NULL; |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1448 | match_len = dir_len + 1 + len; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1449 | match_format = format_aux; |
| 1450 | continue; |
| 1451 | } |
| 1452 | } else { |
| 1453 | /* remember the revision and try to find the newest one */ |
| 1454 | if (match_name) { |
| 1455 | if (file->d_name[len] != '@' || |
| 1456 | lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) { |
| 1457 | continue; |
| 1458 | } else if (match_name[match_len] == '@' && |
| 1459 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 1460 | continue; |
| 1461 | } |
| 1462 | free(match_name); |
| 1463 | } |
| 1464 | |
| 1465 | match_name = wn; |
| 1466 | wn = NULL; |
| 1467 | match_len = dir_len + 1 + len; |
| 1468 | match_format = format_aux; |
| 1469 | continue; |
| 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | success: |
| 1476 | (*localfile) = match_name; |
| 1477 | match_name = NULL; |
| 1478 | if (format) { |
| 1479 | (*format) = match_format; |
| 1480 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1481 | ret = LY_SUCCESS; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1482 | |
| 1483 | cleanup: |
| 1484 | free(wn); |
| 1485 | free(wd); |
| 1486 | if (dir) { |
| 1487 | closedir(dir); |
| 1488 | } |
| 1489 | free(match_name); |
| 1490 | ly_set_free(dirs, free); |
| 1491 | |
| 1492 | return ret; |
| 1493 | } |