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