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 | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame^] | 248 | ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &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 | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 262 | ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 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 | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame^] | 293 | ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &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 | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 307 | ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 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 */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 558 | ret = ly_set_add(changed, f, 1, NULL); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 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 */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 621 | ret = ly_set_add(changed, *df, 1, NULL); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 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 */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 805 | LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL)); |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 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: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame^] | 961 | lysp_module_free((struct lysp_module *)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 | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame^] | 976 | struct lysp_submodule *submod; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 977 | LY_ERR ret; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 978 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 979 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 980 | struct lys_yin_parser_ctx *yinctx = NULL; |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 981 | struct lys_parser_ctx *pctx = NULL; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 982 | char *filename, *rev, *dot; |
| 983 | size_t len; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 984 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 985 | LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 986 | if (module) { |
| 987 | *module = NULL; |
| 988 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 989 | |
| 990 | mod = calloc(1, sizeof *mod); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 991 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 992 | mod->ctx = ctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 993 | |
| 994 | switch (format) { |
| 995 | case LYS_IN_YIN: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 996 | ret = yin_parse_module(&yinctx, in, mod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 997 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 998 | break; |
| 999 | case LYS_IN_YANG: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1000 | ret = yang_parse_module(&yangctx, in, mod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1001 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1002 | break; |
| 1003 | default: |
| 1004 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1005 | ret = LY_EINVAL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1006 | break; |
| 1007 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 1008 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1009 | |
| 1010 | /* make sure that the newest revision is at position 0 */ |
| 1011 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1012 | if (mod->parsed->revs) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1013 | 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] | 1014 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1015 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1016 | /* decide the latest revision */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1017 | latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1018 | if (latest) { |
| 1019 | if (mod->revision) { |
| 1020 | if (!latest->revision) { |
| 1021 | /* latest has no revision, so mod is anyway newer */ |
| 1022 | mod->latest_revision = latest->latest_revision; |
| 1023 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1024 | } else if (strcmp(mod->revision, latest->revision) > 0) { |
| 1025 | mod->latest_revision = latest->latest_revision; |
| 1026 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1027 | } else { |
| 1028 | latest = NULL; |
| 1029 | } |
| 1030 | } else { |
| 1031 | latest = NULL; |
| 1032 | } |
| 1033 | } else { |
| 1034 | mod->latest_revision = 1; |
| 1035 | } |
| 1036 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1037 | if (custom_check) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1038 | 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] | 1039 | } |
| 1040 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1041 | if (implement) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1042 | /* mark the loaded module implemented */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1043 | if (ly_ctx_get_module_implemented(ctx, mod->name)) { |
| 1044 | 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] | 1045 | ret = LY_EDENIED; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1046 | goto error; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 1047 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1050 | /* check for duplicity in the context */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1051 | 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] | 1052 | if (mod_dup) { |
| 1053 | if (mod_dup->parsed) { |
| 1054 | /* error */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1055 | if (mod->parsed->revs) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1056 | 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] | 1057 | mod->name, mod->parsed->revs[0].date); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1058 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1059 | 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] | 1060 | mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1061 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1062 | ret = LY_EEXIST; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1063 | goto error; |
| 1064 | } else { |
| 1065 | /* add the parsed data to the currently compiled-only module in the context */ |
| 1066 | mod_dup->parsed = mod->parsed; |
| 1067 | mod_dup->parsed->mod = mod_dup; |
| 1068 | mod->parsed = NULL; |
| 1069 | lys_module_free(mod, NULL); |
| 1070 | mod = mod_dup; |
| 1071 | goto finish_parsing; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1072 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1073 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1074 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1075 | switch (in->type) { |
| 1076 | case LY_IN_FILEPATH: |
| 1077 | /* check that name and revision match filename */ |
| 1078 | filename = strrchr(in->method.fpath.filepath, '/'); |
| 1079 | if (!filename) { |
| 1080 | filename = in->method.fpath.filepath; |
| 1081 | } else { |
| 1082 | filename++; |
| 1083 | } |
| 1084 | rev = strchr(filename, '@'); |
| 1085 | dot = strrchr(filename, '.'); |
| 1086 | |
| 1087 | /* name */ |
| 1088 | len = strlen(mod->name); |
| 1089 | if (strncmp(filename, mod->name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1090 | ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1091 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name); |
| 1092 | } |
| 1093 | if (rev) { |
| 1094 | len = dot - ++rev; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1095 | 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] | 1096 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1097 | mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | break; |
| 1102 | case LY_IN_FD: |
| 1103 | case LY_IN_FILE: |
| 1104 | case LY_IN_MEMORY: |
| 1105 | /* nothing special to do */ |
| 1106 | break; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1107 | case LY_IN_ERROR: |
| 1108 | LOGINT(ctx); |
| 1109 | ret = LY_EINT; |
| 1110 | goto error; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1111 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1112 | |
| 1113 | lys_parser_fill_filepath(ctx, in, &mod->filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1114 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1115 | if (!implement) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1116 | /* pre-compile features and identities of the module */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame^] | 1117 | LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod->parsed, mod->parsed->features, &mod->features), error); |
| 1118 | LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | if (latest) { |
| 1122 | latest->latest_revision = 0; |
| 1123 | } |
| 1124 | |
| 1125 | /* add into context */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1126 | ret = ly_set_add(&ctx->list, mod, 1, NULL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1127 | LY_CHECK_GOTO(ret, error); |
| 1128 | ctx->module_set_id++; |
| 1129 | |
| 1130 | finish_parsing: |
| 1131 | /* resolve imports and includes */ |
| 1132 | LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx); |
| 1133 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1134 | if (!implement) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1135 | /* pre-compile features and identities of any submodules */ |
| 1136 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame^] | 1137 | submod = mod->parsed->includes[u].submodule; |
| 1138 | ret = lys_feature_precompile(NULL, ctx, (struct lysp_module *)submod, submod->features, &mod->features); |
| 1139 | LY_CHECK_GOTO(ret, error); |
| 1140 | ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities); |
| 1141 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | /* check name collisions - typedefs and TODO groupings */ |
| 1146 | LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx); |
| 1147 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1148 | if (implement) { |
| 1149 | /* implement (compile) */ |
| 1150 | LY_CHECK_GOTO(ret = lys_set_implemented(mod), error_ctx); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | if (format == LYS_IN_YANG) { |
| 1154 | yang_parser_ctx_free(yangctx); |
| 1155 | } else { |
| 1156 | yin_parser_ctx_free(yinctx); |
| 1157 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1158 | if (module) { |
| 1159 | *module = mod; |
| 1160 | } |
| 1161 | return LY_SUCCESS; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1162 | |
| 1163 | error_ctx: |
| 1164 | ly_set_rm(&ctx->list, mod, NULL); |
| 1165 | error: |
| 1166 | lys_module_free(mod, NULL); |
| 1167 | if (pctx) { |
| 1168 | ly_set_erase(&pctx->tpdfs_nodes, NULL); |
| 1169 | } |
| 1170 | if (format == LYS_IN_YANG) { |
| 1171 | yang_parser_ctx_free(yangctx); |
| 1172 | } else { |
| 1173 | yin_parser_ctx_free(yinctx); |
| 1174 | } |
| 1175 | |
| 1176 | return ret; |
| 1177 | } |
| 1178 | |
| 1179 | API LY_ERR |
| 1180 | lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module) |
| 1181 | { |
| 1182 | if (module) { |
| 1183 | *module = NULL; |
| 1184 | } |
| 1185 | LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL); |
| 1186 | |
| 1187 | /* remember input position */ |
| 1188 | in->func_start = in->current; |
| 1189 | |
| 1190 | 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] | 1191 | } |
| 1192 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1193 | API LY_ERR |
| 1194 | 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] | 1195 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1196 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1197 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1198 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1199 | LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | 65639b9 | 2018-11-27 10:51:37 +0100 | [diff] [blame] | 1200 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1201 | 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] | 1202 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1203 | ret = lys_parse(ctx, in, format, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1204 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1205 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1206 | return ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1207 | } |
| 1208 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1209 | API LY_ERR |
| 1210 | 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] | 1211 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1212 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1213 | struct ly_in *in = NULL; |
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_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL); |
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 | 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] | 1218 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1219 | ret = lys_parse(ctx, in, format, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1220 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1221 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1222 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1223 | } |
| 1224 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1225 | API LY_ERR |
| 1226 | 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] | 1227 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1228 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1229 | struct ly_in *in = NULL; |
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_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1232 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1233 | LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1234 | 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] | 1235 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1236 | ret = lys_parse(ctx, in, format, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1237 | ly_in_free(in, 0); |
| 1238 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1239 | return ret; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | API LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1243 | 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] | 1244 | char **localfile, LYS_INFORMAT *format) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1245 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1246 | LY_ERR ret = LY_EMEM; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1247 | size_t len, flen, match_len = 0, dir_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1248 | ly_bool implicit_cwd = 0; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1249 | char *wd, *wn = NULL; |
| 1250 | DIR *dir = NULL; |
| 1251 | struct dirent *file; |
| 1252 | char *match_name = NULL; |
| 1253 | LYS_INFORMAT format_aux, match_format = 0; |
| 1254 | struct ly_set *dirs; |
| 1255 | struct stat st; |
| 1256 | |
| 1257 | LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| 1258 | |
| 1259 | /* start to fill the dir fifo with the context's search path (if set) |
| 1260 | * and the current working directory */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1261 | LY_CHECK_RET(ly_set_new(&dirs)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1262 | |
| 1263 | len = strlen(name); |
| 1264 | if (cwd) { |
| 1265 | wd = get_current_dir_name(); |
| 1266 | if (!wd) { |
| 1267 | LOGMEM(NULL); |
| 1268 | goto cleanup; |
| 1269 | } else { |
| 1270 | /* add implicit current working directory (./) to be searched, |
| 1271 | * this directory is not searched recursively */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1272 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 1273 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1274 | implicit_cwd = 1; |
| 1275 | } |
| 1276 | } |
| 1277 | if (searchpaths) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1278 | for (uint64_t i = 0; searchpaths[i]; i++) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1279 | /* check for duplicities with the implicit current working directory */ |
| 1280 | if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| 1281 | implicit_cwd = 0; |
| 1282 | continue; |
| 1283 | } |
| 1284 | wd = strdup(searchpaths[i]); |
| 1285 | if (!wd) { |
| 1286 | LOGMEM(NULL); |
| 1287 | goto cleanup; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1288 | } else { |
| 1289 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 1290 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | wd = NULL; |
| 1295 | |
| 1296 | /* start searching */ |
| 1297 | while (dirs->count) { |
| 1298 | free(wd); |
| 1299 | free(wn); wn = NULL; |
| 1300 | |
| 1301 | dirs->count--; |
| 1302 | wd = (char *)dirs->objs[dirs->count]; |
| 1303 | dirs->objs[dirs->count] = NULL; |
| 1304 | LOGVRB("Searching for \"%s\" in %s.", name, wd); |
| 1305 | |
| 1306 | if (dir) { |
| 1307 | closedir(dir); |
| 1308 | } |
| 1309 | dir = opendir(wd); |
| 1310 | dir_len = strlen(wd); |
| 1311 | if (!dir) { |
| 1312 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 1313 | } else { |
| 1314 | while ((file = readdir(dir))) { |
| 1315 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 1316 | /* skip . and .. */ |
| 1317 | continue; |
| 1318 | } |
| 1319 | free(wn); |
| 1320 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 1321 | LOGMEM(NULL); |
| 1322 | goto cleanup; |
| 1323 | } |
| 1324 | if (stat(wn, &st) == -1) { |
| 1325 | 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] | 1326 | file->d_name, wd, strerror(errno)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1327 | continue; |
| 1328 | } |
| 1329 | if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) { |
| 1330 | /* we have another subdirectory in searchpath to explore, |
| 1331 | * 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] | 1332 | ret = ly_set_add(dirs, wn, 0, NULL); |
| 1333 | LY_CHECK_GOTO(ret, cleanup); |
| 1334 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1335 | /* continue with the next item in current directory */ |
| 1336 | wn = NULL; |
| 1337 | continue; |
| 1338 | } else if (!S_ISREG(st.st_mode)) { |
| 1339 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 1340 | continue; |
| 1341 | } |
| 1342 | |
| 1343 | /* here we know that the item is a file which can contain a module */ |
| 1344 | if (strncmp(name, file->d_name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1345 | ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1346 | /* different filename than the module we search for */ |
| 1347 | continue; |
| 1348 | } |
| 1349 | |
| 1350 | /* get type according to filename suffix */ |
| 1351 | flen = strlen(file->d_name); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1352 | if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1353 | format_aux = LYS_IN_YANG; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1354 | /* TODO YIN parser |
| 1355 | } else if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| 1356 | format_aux = LYS_IN_YIN; |
| 1357 | */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1358 | } else { |
| 1359 | /* not supportde suffix/file format */ |
| 1360 | continue; |
| 1361 | } |
| 1362 | |
| 1363 | if (revision) { |
| 1364 | /* we look for the specific revision, try to get it from the filename */ |
| 1365 | if (file->d_name[len] == '@') { |
| 1366 | /* check revision from the filename */ |
| 1367 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 1368 | /* another revision */ |
| 1369 | continue; |
| 1370 | } else { |
| 1371 | /* exact revision */ |
| 1372 | free(match_name); |
| 1373 | match_name = wn; |
| 1374 | wn = NULL; |
| 1375 | match_len = dir_len + 1 + len; |
| 1376 | match_format = format_aux; |
| 1377 | goto success; |
| 1378 | } |
| 1379 | } else { |
| 1380 | /* continue trying to find exact revision match, use this only if not found */ |
| 1381 | free(match_name); |
| 1382 | match_name = wn; |
| 1383 | wn = NULL; |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1384 | match_len = dir_len + 1 + len; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1385 | match_format = format_aux; |
| 1386 | continue; |
| 1387 | } |
| 1388 | } else { |
| 1389 | /* remember the revision and try to find the newest one */ |
| 1390 | if (match_name) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1391 | if ((file->d_name[len] != '@') || |
| 1392 | 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] | 1393 | continue; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1394 | } else if ((match_name[match_len] == '@') && |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1395 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 1396 | continue; |
| 1397 | } |
| 1398 | free(match_name); |
| 1399 | } |
| 1400 | |
| 1401 | match_name = wn; |
| 1402 | wn = NULL; |
| 1403 | match_len = dir_len + 1 + len; |
| 1404 | match_format = format_aux; |
| 1405 | continue; |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | success: |
| 1412 | (*localfile) = match_name; |
| 1413 | match_name = NULL; |
| 1414 | if (format) { |
| 1415 | (*format) = match_format; |
| 1416 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1417 | ret = LY_SUCCESS; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1418 | |
| 1419 | cleanup: |
| 1420 | free(wn); |
| 1421 | free(wd); |
| 1422 | if (dir) { |
| 1423 | closedir(dir); |
| 1424 | } |
| 1425 | free(match_name); |
| 1426 | ly_set_free(dirs, free); |
| 1427 | |
| 1428 | return ret; |
| 1429 | } |