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 | |
| 15 | #include "common.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 18 | #include <dirent.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include <limits.h> |
| 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 | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 29 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | #include "dict.h" |
| 31 | #include "log.h" |
| 32 | #include "set.h" |
| 33 | #include "tree.h" |
| 34 | #include "tree_schema.h" |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 35 | #include "tree_schema_internal.h" |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 36 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 37 | API const struct lysc_node * |
| 38 | lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options) |
| 39 | { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 40 | const struct lysc_node *next = NULL; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 41 | struct lysc_node **snode; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 42 | int action_flag = 0, notif_flag = 0; |
| 43 | const struct lysc_action *actions; |
| 44 | const struct lysc_notif *notifs; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame^] | 45 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 46 | |
| 47 | LY_CHECK_ARG_RET(NULL, parent || module, NULL); |
| 48 | |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 49 | next: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 50 | if (!last) { |
| 51 | /* first call */ |
| 52 | |
| 53 | /* get know where to start */ |
| 54 | if (parent) { |
| 55 | /* schema subtree */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 56 | if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) { |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 57 | if (((struct lysc_node_choice*)parent)->cases) { |
| 58 | 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] | 59 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 60 | } else { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 61 | 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] | 62 | /* do not return anything if the node does not have any children */ |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 63 | if (snode && *snode) { |
| 64 | next = last = *snode; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 65 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 66 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 67 | } else { |
| 68 | /* top level data */ |
| 69 | next = last = module->data; |
| 70 | } |
| 71 | if (!next) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 72 | /* try to get action or notification */ |
| 73 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 74 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 75 | /* test if the next can be returned */ |
| 76 | goto check; |
| 77 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 78 | } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 79 | action_flag = 1; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 80 | if (last->parent) { |
| 81 | actions = lysc_node_actions(last->parent); |
| 82 | } else { |
| 83 | actions = module->rpcs; |
| 84 | } |
| 85 | LY_ARRAY_FOR(actions, u) { |
| 86 | if (&actions[u] == (struct lysc_action*)last) { |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | if (u + 1 < LY_ARRAY_SIZE(actions)) { |
| 91 | next = (struct lysc_node*)(&actions[u + 1]); |
| 92 | } |
| 93 | goto repeat; |
| 94 | } else if (last->nodetype == LYS_NOTIF) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 95 | action_flag = notif_flag = 1; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 96 | if (last->parent) { |
| 97 | notifs = lysc_node_notifs(last->parent); |
| 98 | } else { |
| 99 | notifs = module->notifs; |
| 100 | } |
| 101 | LY_ARRAY_FOR(notifs, u) { |
| 102 | if (¬ifs[u] == (struct lysc_notif*)last) { |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | if (u + 1 < LY_ARRAY_SIZE(notifs)) { |
| 107 | next = (struct lysc_node*)(¬ifs[u + 1]); |
| 108 | } |
| 109 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | next = last->next; |
| 113 | repeat: |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 114 | if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) { |
| 115 | /* inside case (as an explicit parent, not when diving into it from choice), |
| 116 | * limit the list of children only to the specific case */ |
| 117 | next = NULL; |
| 118 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 119 | if (!next) { |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 120 | /* possibly go back to parent */ |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 121 | if (last && last->parent != parent) { |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 122 | last = last->parent; |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 123 | goto next; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 124 | } else if (!action_flag) { |
| 125 | action_flag = 1; |
| 126 | next = parent ? (struct lysc_node*)lysc_node_actions(parent) : (struct lysc_node*)module->rpcs; |
| 127 | } else if (!notif_flag) { |
| 128 | notif_flag = 1; |
| 129 | next = parent ? (struct lysc_node*)lysc_node_notifs(parent) : (struct lysc_node*)module->notifs; |
| 130 | } else { |
| 131 | return NULL; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 132 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 133 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 134 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 135 | check: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 136 | switch (next->nodetype) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 137 | case LYS_RPC: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 138 | case LYS_ACTION: |
| 139 | case LYS_NOTIF: |
| 140 | case LYS_LEAF: |
| 141 | case LYS_ANYXML: |
| 142 | case LYS_ANYDATA: |
| 143 | case LYS_LIST: |
| 144 | case LYS_LEAFLIST: |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 145 | case LYS_CASE: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 146 | break; |
| 147 | case LYS_CONTAINER: |
| 148 | if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) { |
| 149 | if (((struct lysc_node_container *)next)->child) { |
| 150 | /* go into */ |
| 151 | next = ((struct lysc_node_container *)next)->child; |
| 152 | } else { |
| 153 | next = next->next; |
| 154 | } |
| 155 | goto repeat; |
| 156 | } |
| 157 | break; |
| 158 | case LYS_CHOICE: |
| 159 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 160 | return next; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 161 | } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) { |
| 162 | next = next->next; |
| 163 | } else { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 164 | /* go into */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 165 | if (options & LYS_GETNEXT_WITHCASE) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 166 | next = (struct lysc_node*)((struct lysc_node_choice *)next)->cases; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 167 | } else { |
| 168 | next = ((struct lysc_node_choice *)next)->cases->child; |
| 169 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 170 | } |
| 171 | goto repeat; |
| 172 | default: |
| 173 | /* we should not be here */ |
Radek Krejci | b07b5c9 | 2019-04-08 10:56:37 +0200 | [diff] [blame] | 174 | LOGINT(module ? module->mod->ctx : parent->module->ctx); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | if (!(options & LYS_GETNEXT_NOSTATECHECK)) { |
| 179 | /* check if the node is disabled by if-feature */ |
Radek Krejci | fab954b | 2019-09-11 11:25:14 +0200 | [diff] [blame] | 180 | if (lysc_node_is_disabled(next, 0)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 181 | next = next->next; |
| 182 | goto repeat; |
| 183 | } |
| 184 | } |
| 185 | return next; |
| 186 | } |
| 187 | |
| 188 | API const struct lysc_node * |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 189 | lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len, |
| 190 | uint16_t nodetype, int options) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 191 | { |
| 192 | const struct lysc_node *node = NULL; |
| 193 | |
| 194 | LY_CHECK_ARG_RET(NULL, module, name, NULL); |
| 195 | if (!nodetype) { |
| 196 | nodetype = 0xffff; |
| 197 | } |
| 198 | |
| 199 | while ((node = lys_getnext(node, parent, module->compiled, options))) { |
| 200 | if (!(node->nodetype & nodetype)) { |
| 201 | continue; |
| 202 | } |
| 203 | if (node->module != module) { |
| 204 | continue; |
| 205 | } |
| 206 | |
| 207 | if (name_len) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 208 | if (!ly_strncmp(node->name, name, name_len)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 209 | return node; |
| 210 | } |
| 211 | } else { |
| 212 | if (!strcmp(node->name, name)) { |
| 213 | return node; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | return NULL; |
| 218 | } |
| 219 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 220 | char * |
| 221 | lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer, |
| 222 | size_t buflen) |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 223 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 224 | const struct lysc_node *iter; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 225 | char *path = NULL; |
| 226 | int len = 0; |
| 227 | |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 228 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 229 | if (buffer) { |
| 230 | LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL); |
| 231 | } |
| 232 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 233 | switch (pathtype) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 234 | case LYSC_PATH_LOG: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 235 | for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 236 | char *s = buffer ? strdup(buffer) : path; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 237 | char *id; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 238 | const char *slash; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 239 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 240 | id = strdup(iter->name); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 241 | if (parent && (iter->parent == parent)) { |
| 242 | slash = ""; |
| 243 | } else { |
| 244 | slash = "/"; |
| 245 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 246 | if (!iter->parent || iter->parent->module != iter->module) { |
| 247 | /* print prefix */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 248 | if (buffer) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 249 | 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] | 250 | } else { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 251 | 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] | 252 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 253 | } else { |
| 254 | /* prefix is the same as in parent */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 255 | if (buffer) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 256 | len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 257 | } else { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 258 | len = asprintf(&path, "%s%s%s", slash, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 259 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 260 | } |
| 261 | free(s); |
| 262 | free(id); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 263 | |
| 264 | if (buffer && buflen <= (size_t)len) { |
| 265 | /* not enough space in buffer */ |
| 266 | break; |
| 267 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | if (len < 0) { |
| 271 | free(path); |
| 272 | path = NULL; |
| 273 | } else if (len == 0) { |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 274 | if (buffer) { |
| 275 | strcpy(buffer, "/"); |
| 276 | } else { |
| 277 | path = strdup("/"); |
| 278 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 279 | } |
| 280 | break; |
| 281 | } |
| 282 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 283 | if (buffer) { |
| 284 | return buffer; |
| 285 | } else { |
| 286 | return path; |
| 287 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 290 | API char * |
| 291 | lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 292 | { |
| 293 | return lysc_path_until(node, NULL, pathtype, buffer, buflen); |
| 294 | } |
| 295 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 296 | API int |
| 297 | lysc_feature_value(const struct lysc_feature *feature) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 298 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 299 | LY_CHECK_ARG_RET(NULL, feature, -1); |
| 300 | return feature->flags & LYS_FENABLED ? 1 : 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 303 | uint8_t |
| 304 | lysc_iff_getop(uint8_t *list, int pos) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 305 | { |
| 306 | uint8_t *item; |
| 307 | uint8_t mask = 3, result; |
| 308 | |
| 309 | assert(pos >= 0); |
| 310 | |
| 311 | item = &list[pos / 4]; |
| 312 | result = (*item) & (mask << 2 * (pos % 4)); |
| 313 | return result >> 2 * (pos % 4); |
| 314 | } |
| 315 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 316 | static int |
| 317 | lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f) |
| 318 | { |
| 319 | uint8_t op; |
| 320 | int a, b; |
| 321 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 322 | op = lysc_iff_getop(iff->expr, *index_e); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 323 | (*index_e)++; |
| 324 | |
| 325 | switch (op) { |
| 326 | case LYS_IFF_F: |
| 327 | /* resolve feature */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 328 | return lysc_feature_value(iff->features[(*index_f)++]); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 329 | case LYS_IFF_NOT: |
| 330 | /* invert result */ |
| 331 | return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1; |
| 332 | case LYS_IFF_AND: |
| 333 | case LYS_IFF_OR: |
| 334 | a = lysc_iffeature_value_(iff, index_e, index_f); |
| 335 | b = lysc_iffeature_value_(iff, index_e, index_f); |
| 336 | if (op == LYS_IFF_AND) { |
| 337 | return a && b; |
| 338 | } else { /* LYS_IFF_OR */ |
| 339 | return a || b; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | API int |
| 347 | lysc_iffeature_value(const struct lysc_iffeature *iff) |
| 348 | { |
| 349 | int index_e = 0, index_f = 0; |
| 350 | |
| 351 | LY_CHECK_ARG_RET(NULL, iff, -1); |
| 352 | |
| 353 | if (iff->expr) { |
| 354 | return lysc_iffeature_value_(iff, &index_e, &index_f); |
| 355 | } |
| 356 | return 0; |
| 357 | } |
| 358 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 359 | /** |
| 360 | * @brief Enable/Disable the specified feature in the module. |
| 361 | * |
| 362 | * If the feature is already set to the desired value, LY_SUCCESS is returned. |
| 363 | * By changing the feature, also all the feature which depends on it via their |
| 364 | * if-feature statements are again evaluated (disabled if a if-feature statemen |
| 365 | * evaluates to false). |
| 366 | * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 367 | * @param[in] mod Module where to set (search for) the feature. |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 368 | * @param[in] name Name of the feature to set. Asterisk ('*') can be used to |
| 369 | * set all the features in the module. |
| 370 | * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable). |
| 371 | * @return LY_ERR value. |
| 372 | */ |
| 373 | static LY_ERR |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 374 | lys_feature_change(const struct lys_module *mod, const char *name, int value) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 375 | { |
| 376 | int all = 0; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame^] | 377 | LY_ARRAY_SIZE_TYPE u, disabled_count; |
| 378 | uint32_t changed_count; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 379 | struct lysc_feature *f, **df; |
| 380 | struct lysc_iffeature *iff; |
| 381 | struct ly_set *changed; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 382 | struct ly_ctx *ctx = mod->ctx; /* shortcut */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 383 | |
Radek Krejci | 6e67c40 | 2019-05-02 09:55:39 +0200 | [diff] [blame] | 384 | if (!strcmp(name, "*")) { |
| 385 | /* enable all */ |
| 386 | all = 1; |
| 387 | } |
| 388 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 389 | if (!mod->compiled) { |
| 390 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.", |
| 391 | mod->name); |
| 392 | return LY_EINVAL; |
| 393 | } |
| 394 | if (!mod->compiled->features) { |
Radek Krejci | 6e67c40 | 2019-05-02 09:55:39 +0200 | [diff] [blame] | 395 | if (all) { |
| 396 | /* no feature to enable */ |
| 397 | return LY_SUCCESS; |
| 398 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 399 | LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 400 | return LY_EINVAL; |
| 401 | } |
| 402 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 403 | changed = ly_set_new(); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 404 | changed_count = 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 405 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 406 | run: |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 407 | for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) { |
| 408 | f = &mod->compiled->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 409 | if (all || !strcmp(f->name, name)) { |
| 410 | if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) { |
| 411 | if (all) { |
| 412 | /* skip already set features */ |
| 413 | continue; |
| 414 | } else { |
| 415 | /* feature already set correctly */ |
| 416 | ly_set_free(changed, NULL); |
| 417 | return LY_SUCCESS; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (value) { /* enable */ |
| 422 | /* check referenced features if they are enabled */ |
| 423 | LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) { |
| 424 | if (!lysc_iffeature_value(iff)) { |
| 425 | if (all) { |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 426 | ++disabled_count; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 427 | goto next; |
| 428 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 429 | LOGERR(ctx, LY_EDENIED, |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 430 | "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
| 431 | f->name); |
| 432 | ly_set_free(changed, NULL); |
| 433 | return LY_EDENIED; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | /* enable the feature */ |
| 438 | f->flags |= LYS_FENABLED; |
| 439 | } else { /* disable */ |
| 440 | /* disable the feature */ |
| 441 | f->flags &= ~LYS_FENABLED; |
| 442 | } |
| 443 | |
| 444 | /* remember the changed feature */ |
| 445 | ly_set_add(changed, f, LY_SET_OPT_USEASLIST); |
| 446 | |
| 447 | if (!all) { |
| 448 | /* stop in case changing a single feature */ |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | next: |
| 453 | ; |
| 454 | } |
| 455 | |
| 456 | if (!all && !changed->count) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 457 | LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 458 | ly_set_free(changed, NULL); |
| 459 | return LY_EINVAL; |
| 460 | } |
| 461 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 462 | if (value && all && disabled_count) { |
| 463 | if (changed_count == changed->count) { |
| 464 | /* no change in last run -> not able to enable all ... */ |
| 465 | /* ... print errors */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 466 | for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) { |
| 467 | if (!(mod->compiled->features[u].flags & LYS_FENABLED)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 468 | LOGERR(ctx, LY_EDENIED, |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 469 | "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 470 | mod->compiled->features[u].name); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 471 | --disabled_count; |
| 472 | } |
| 473 | } |
| 474 | /* ... restore the original state */ |
| 475 | for (u = 0; u < changed->count; ++u) { |
| 476 | f = changed->objs[u]; |
| 477 | /* re-disable the feature */ |
| 478 | f->flags &= ~LYS_FENABLED; |
| 479 | } |
| 480 | |
| 481 | ly_set_free(changed, NULL); |
| 482 | return LY_EDENIED; |
| 483 | } else { |
| 484 | /* we did some change in last run, try it again */ |
| 485 | changed_count = changed->count; |
| 486 | goto run; |
| 487 | } |
| 488 | } |
| 489 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 490 | /* reflect change(s) in the dependent features */ |
| 491 | for (u = 0; u < changed->count; ++u) { |
| 492 | /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of |
| 493 | * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled |
| 494 | * is not done - by default, features are disabled and must be explicitely enabled. */ |
| 495 | f = changed->objs[u]; |
| 496 | LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) { |
| 497 | if (!((*df)->flags & LYS_FENABLED)) { |
| 498 | /* not enabled, nothing to do */ |
| 499 | continue; |
| 500 | } |
| 501 | /* check the feature's if-features which could change by the previous change of our feature */ |
| 502 | LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) { |
| 503 | if (!lysc_iffeature_value(iff)) { |
| 504 | /* the feature must be disabled now */ |
| 505 | (*df)->flags &= ~LYS_FENABLED; |
| 506 | /* add the feature into the list of changed features */ |
| 507 | ly_set_add(changed, *df, LY_SET_OPT_USEASLIST); |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | ly_set_free(changed, NULL); |
| 515 | return LY_SUCCESS; |
| 516 | } |
| 517 | |
| 518 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 519 | lys_feature_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 520 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 521 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 522 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 523 | return lys_feature_change((struct lys_module*)module, feature, 1); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 527 | lys_feature_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 528 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 529 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 530 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 531 | return lys_feature_change((struct lys_module*)module, feature, 0); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | API int |
| 535 | lys_feature_value(const struct lys_module *module, const char *feature) |
| 536 | { |
| 537 | struct lysc_feature *f; |
| 538 | struct lysc_module *mod; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame^] | 539 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 540 | |
| 541 | LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1); |
| 542 | mod = module->compiled; |
| 543 | |
| 544 | /* search for the specified feature */ |
| 545 | for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 546 | f = &mod->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 547 | if (!strcmp(f->name, feature)) { |
| 548 | if (f->flags & LYS_FENABLED) { |
| 549 | return 1; |
| 550 | } else { |
| 551 | return 0; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /* feature definition not found */ |
| 557 | return -1; |
| 558 | } |
| 559 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 560 | API const struct lysc_node * |
Radek Krejci | fab954b | 2019-09-11 11:25:14 +0200 | [diff] [blame] | 561 | lysc_node_is_disabled(const struct lysc_node *node, int recursive) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 562 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame^] | 563 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 564 | |
| 565 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 566 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 567 | do { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 568 | if (node->iffeatures) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 569 | /* check local if-features */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 570 | LY_ARRAY_FOR(node->iffeatures, u) { |
| 571 | if (!lysc_iffeature_value(&node->iffeatures[u])) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 572 | return node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (!recursive) { |
| 578 | return NULL; |
| 579 | } |
| 580 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 581 | /* go through schema-only parents */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 582 | node = node->parent; |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 583 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 584 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 585 | return NULL; |
| 586 | } |
| 587 | |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 588 | LY_ERR |
| 589 | lys_set_implemented_internal(struct lys_module *mod, uint8_t value) |
| 590 | { |
| 591 | struct lys_module *m; |
| 592 | |
| 593 | LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL); |
| 594 | |
| 595 | if (mod->implemented) { |
| 596 | return LY_SUCCESS; |
| 597 | } |
| 598 | |
| 599 | /* we have module from the current context */ |
| 600 | m = ly_ctx_get_module_implemented(mod->ctx, mod->name); |
| 601 | if (m) { |
| 602 | if (m != mod) { |
| 603 | /* check collision with other implemented revision */ |
| 604 | LOGERR(mod->ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).", |
| 605 | mod->name, mod->revision ? mod->revision : "module without revision"); |
| 606 | return LY_EDENIED; |
| 607 | } else { |
| 608 | /* mod is already implemented */ |
| 609 | return LY_SUCCESS; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* mark the module implemented, check for collision was already done */ |
| 614 | mod->implemented = value; |
| 615 | |
| 616 | /* compile the schema */ |
| 617 | LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL)); |
| 618 | |
| 619 | return LY_SUCCESS; |
| 620 | } |
| 621 | |
| 622 | API LY_ERR |
| 623 | lys_set_implemented(struct lys_module *mod) |
| 624 | { |
| 625 | return lys_set_implemented_internal(mod, 1); |
| 626 | } |
| 627 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 628 | struct lysp_submodule * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 629 | lys_parse_mem_submodule(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx, |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 630 | LY_ERR (*custom_check)(const struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data) |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 631 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 632 | LY_ERR ret = LY_EINVAL; |
| 633 | struct lysp_submodule *submod = NULL, *latest_sp; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 634 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 635 | struct lys_yin_parser_ctx *yinctx = NULL; |
| 636 | struct lys_parser_ctx *pctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 637 | |
| 638 | LY_CHECK_ARG_RET(ctx, ctx, data, NULL); |
| 639 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 640 | switch (format) { |
| 641 | case LYS_IN_YIN: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 642 | ret = yin_parse_submodule(&yinctx, ctx, main_ctx, data, &submod); |
| 643 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 644 | break; |
| 645 | case LYS_IN_YANG: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 646 | ret = yang_parse_submodule(&yangctx, ctx, main_ctx, data, &submod); |
| 647 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 648 | break; |
| 649 | default: |
David Sedlák | 4f2f5ba | 2019-08-15 13:18:48 +0200 | [diff] [blame] | 650 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 651 | break; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 652 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 653 | LY_CHECK_RET(ret, NULL); |
| 654 | |
| 655 | /* make sure that the newest revision is at position 0 */ |
| 656 | lysp_sort_revisions(submod->revs); |
| 657 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 658 | /* decide the latest revision */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 659 | latest_sp = ly_ctx_get_submodule(PARSER_CTX(pctx), submod->belongsto, submod->name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 660 | if (latest_sp) { |
| 661 | if (submod->revs) { |
| 662 | if (!latest_sp->revs) { |
| 663 | /* latest has no revision, so mod is anyway newer */ |
| 664 | submod->latest_revision = latest_sp->latest_revision; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 665 | /* the latest_sp is zeroed later when the new module is being inserted into the context */ |
| 666 | } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) { |
| 667 | submod->latest_revision = latest_sp->latest_revision; |
| 668 | /* 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] | 669 | } else { |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 670 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 671 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 672 | } else { |
| 673 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 674 | } |
| 675 | } else { |
| 676 | submod->latest_revision = 1; |
| 677 | } |
| 678 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 679 | if (custom_check) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 680 | LY_CHECK_GOTO(custom_check(PARSER_CTX(pctx), NULL, submod, check_data), error); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | if (latest_sp) { |
| 684 | latest_sp->latest_revision = 0; |
| 685 | } |
| 686 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 687 | /* 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] | 688 | memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| 689 | 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] | 690 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 691 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 692 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 693 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 694 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 695 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 696 | return submod; |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 697 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 698 | error: |
| 699 | lysp_submodule_free(ctx, submod); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 700 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 701 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 702 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 703 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 704 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 705 | return NULL; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 706 | } |
| 707 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 708 | struct lys_module * |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 709 | lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement, |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 710 | LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data), |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 711 | void *check_data) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 712 | { |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 713 | struct lys_module *mod = NULL, *latest, *mod_dup; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 714 | struct lysp_import *imp; |
| 715 | struct lysp_include *inc; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 716 | LY_ERR ret = LY_EINVAL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame^] | 717 | LY_ARRAY_SIZE_TYPE u, v; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 718 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 719 | struct lys_yin_parser_ctx *yinctx = NULL; |
| 720 | struct lys_parser_ctx *pctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 721 | |
| 722 | LY_CHECK_ARG_RET(ctx, ctx, data, NULL); |
| 723 | |
| 724 | mod = calloc(1, sizeof *mod); |
| 725 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 726 | mod->ctx = ctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 727 | |
| 728 | switch (format) { |
| 729 | case LYS_IN_YIN: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 730 | ret = yin_parse_module(&yinctx, data, mod); |
| 731 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 732 | break; |
| 733 | case LYS_IN_YANG: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 734 | ret = yang_parse_module(&yangctx, data, mod); |
| 735 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 736 | break; |
| 737 | default: |
| 738 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
| 739 | break; |
| 740 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 741 | LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 742 | |
| 743 | /* make sure that the newest revision is at position 0 */ |
| 744 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 745 | if (mod->parsed->revs) { |
| 746 | mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0); |
| 747 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 748 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 749 | /* decide the latest revision */ |
| 750 | latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name); |
| 751 | if (latest) { |
| 752 | if (mod->revision) { |
| 753 | if (!latest->revision) { |
| 754 | /* latest has no revision, so mod is anyway newer */ |
| 755 | mod->latest_revision = latest->latest_revision; |
| 756 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 757 | } else if (strcmp(mod->revision, latest->revision) > 0) { |
| 758 | mod->latest_revision = latest->latest_revision; |
| 759 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 760 | } else { |
| 761 | latest = NULL; |
| 762 | } |
| 763 | } else { |
| 764 | latest = NULL; |
| 765 | } |
| 766 | } else { |
| 767 | mod->latest_revision = 1; |
| 768 | } |
| 769 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 770 | if (custom_check) { |
| 771 | LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error); |
| 772 | } |
| 773 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 774 | if (implement) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 775 | /* mark the loaded module implemented */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 776 | if (ly_ctx_get_module_implemented(ctx, mod->name)) { |
| 777 | LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 778 | goto error; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 779 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 780 | mod->implemented = 1; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 781 | } |
| 782 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 783 | /* check for duplicity in the context */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 784 | 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] | 785 | if (mod_dup) { |
| 786 | if (mod_dup->parsed) { |
| 787 | /* error */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 788 | if (mod->parsed->revs) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 789 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.", |
| 790 | mod->name, mod->parsed->revs[0].date); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 791 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 792 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.", |
| 793 | mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 794 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 795 | goto error; |
| 796 | } else { |
| 797 | /* add the parsed data to the currently compiled-only module in the context */ |
| 798 | mod_dup->parsed = mod->parsed; |
| 799 | mod_dup->parsed->mod = mod_dup; |
| 800 | mod->parsed = NULL; |
| 801 | lys_module_free(mod, NULL); |
| 802 | mod = mod_dup; |
| 803 | goto finish_parsing; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 804 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 805 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 806 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 807 | if (!mod->implemented) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 808 | /* pre-compile features of the module */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 809 | LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->off_features), error); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 810 | } |
| 811 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 812 | if (latest) { |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 813 | latest->latest_revision = 0; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 814 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 815 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 816 | /* add into context */ |
| 817 | ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 818 | |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 819 | finish_parsing: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 820 | /* resolve imports */ |
| 821 | mod->parsed->parsing = 1; |
| 822 | LY_ARRAY_FOR(mod->parsed->imports, u) { |
| 823 | imp = &mod->parsed->imports[u]; |
| 824 | if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) { |
| 825 | goto error_ctx; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 826 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 827 | /* check for importing the same module twice */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame^] | 828 | for (v = 0; v < u; ++v) { |
| 829 | if (imp->module == mod->parsed->imports[v].module) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 830 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Single revision of the module \"%s\" referred twice.", imp->name); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 831 | goto error_ctx; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 832 | } |
| 833 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 834 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 835 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 836 | inc = &mod->parsed->includes[u]; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 837 | if (!inc->submodule && lysp_load_submodule(pctx, mod->parsed, inc)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 838 | goto error_ctx; |
| 839 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 840 | if (!mod->implemented) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 841 | /* pre-compile features of the submodule */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 842 | LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, inc->submodule->features, &mod->off_features), error); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 843 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 844 | } |
| 845 | mod->parsed->parsing = 0; |
| 846 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 847 | /* check name collisions - typedefs and TODO groupings */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 848 | LY_CHECK_GOTO(lysp_check_typedefs(pctx, mod->parsed), error_ctx); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 849 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 850 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 851 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 852 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 853 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 854 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 855 | return mod; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 856 | |
| 857 | error_ctx: |
| 858 | ly_set_rm(&ctx->list, mod, NULL); |
| 859 | error: |
| 860 | lys_module_free(mod, NULL); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 861 | ly_set_erase(&pctx->tpdfs_nodes, NULL); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 862 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 863 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 864 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 865 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 866 | } |
| 867 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 868 | return NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 869 | } |
| 870 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 871 | API struct lys_module * |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 872 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 873 | { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 874 | struct lys_module *mod; |
| 875 | |
| 876 | mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL); |
| 877 | LY_CHECK_RET(!mod, NULL); |
| 878 | |
| 879 | if (lys_compile(mod, 0)) { |
| 880 | ly_set_rm(&ctx->list, mod, NULL); |
| 881 | lys_module_free(mod, NULL); |
| 882 | return NULL; |
| 883 | } |
| 884 | return mod; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static void |
| 888 | lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd) |
| 889 | { |
Radek Krejci | 65639b9 | 2018-11-27 10:51:37 +0100 | [diff] [blame] | 890 | char path[PATH_MAX]; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 891 | |
| 892 | #ifdef __APPLE__ |
| 893 | if (fcntl(fd, F_GETPATH, path) != -1) { |
| 894 | *filename = lydict_insert(ctx, path, 0); |
| 895 | } |
| 896 | #else |
Radek Krejci | 65639b9 | 2018-11-27 10:51:37 +0100 | [diff] [blame] | 897 | int len; |
| 898 | char proc_path[32]; |
| 899 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 900 | /* get URI if there is /proc */ |
| 901 | sprintf(proc_path, "/proc/self/fd/%d", fd); |
| 902 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
| 903 | *filename = lydict_insert(ctx, path, len); |
| 904 | } |
| 905 | #endif |
| 906 | } |
| 907 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 908 | void * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 909 | lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct lys_parser_ctx *main_ctx, |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 910 | lys_custom_check custom_check, void *check_data) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 911 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 912 | void *result; |
| 913 | struct lys_module *mod = NULL; |
| 914 | struct lysp_submodule *submod = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 915 | size_t length; |
| 916 | char *addr; |
| 917 | |
| 918 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 919 | if (fd < 0) { |
| 920 | LOGARG(ctx, fd); |
| 921 | return NULL; |
| 922 | } |
| 923 | |
| 924 | LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL); |
| 925 | if (!addr) { |
| 926 | LOGERR(ctx, LY_EINVAL, "Empty schema file."); |
| 927 | return NULL; |
| 928 | } |
| 929 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 930 | if (main_ctx) { |
| 931 | result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data); |
| 932 | } else { |
| 933 | result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 934 | if (mod && implement && lys_compile(mod, 0)) { |
| 935 | ly_set_rm(&ctx->list, mod, NULL); |
| 936 | lys_module_free(mod, NULL); |
| 937 | result = mod = NULL; |
| 938 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 939 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 940 | ly_munmap(addr, length); |
| 941 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 942 | if (mod && !mod->filepath) { |
| 943 | lys_parse_set_filename(ctx, &mod->filepath, fd); |
| 944 | } else if (submod && !submod->filepath) { |
| 945 | lys_parse_set_filename(ctx, &submod->filepath, fd); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 946 | } |
| 947 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 948 | return result; |
| 949 | } |
| 950 | |
| 951 | struct lys_module * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 952 | lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, lys_custom_check custom_check, |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 953 | void *check_data) |
| 954 | { |
| 955 | return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data); |
| 956 | } |
| 957 | |
| 958 | struct lysp_submodule * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 959 | lys_parse_fd_submodule(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx, |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 960 | lys_custom_check custom_check, void *check_data) |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 961 | { |
| 962 | assert(main_ctx); |
| 963 | return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 966 | API struct lys_module * |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 967 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
| 968 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 969 | return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 970 | } |
| 971 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 972 | struct lys_module * |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 973 | lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement, |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 974 | lys_custom_check custom_check, void *check_data) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 975 | { |
| 976 | int fd; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 977 | struct lys_module *mod; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 978 | const char *rev, *dot, *filename; |
| 979 | size_t len; |
| 980 | |
| 981 | LY_CHECK_ARG_RET(ctx, ctx, path, NULL); |
| 982 | |
| 983 | fd = open(path, O_RDONLY); |
| 984 | LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL); |
| 985 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 986 | mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 987 | close(fd); |
| 988 | LY_CHECK_RET(!mod, NULL); |
| 989 | |
| 990 | /* check that name and revision match filename */ |
| 991 | filename = strrchr(path, '/'); |
| 992 | if (!filename) { |
| 993 | filename = path; |
| 994 | } else { |
| 995 | filename++; |
| 996 | } |
| 997 | rev = strchr(filename, '@'); |
| 998 | dot = strrchr(filename, '.'); |
| 999 | |
| 1000 | /* name */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1001 | len = strlen(mod->name); |
| 1002 | if (strncmp(filename, mod->name, len) || |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1003 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1004 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1005 | } |
| 1006 | if (rev) { |
| 1007 | len = dot - ++rev; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 1008 | if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1009 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 1010 | mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1014 | if (!mod->filepath) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1015 | /* store URI */ |
| 1016 | char rpath[PATH_MAX]; |
| 1017 | if (realpath(path, rpath) != NULL) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1018 | mod->filepath = lydict_insert(ctx, rpath, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1019 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1020 | mod->filepath = lydict_insert(ctx, path, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | return mod; |
| 1025 | } |
| 1026 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 1027 | API struct lys_module * |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1028 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 1029 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1030 | return lys_parse_path_(ctx, path, format, 1, NULL, NULL); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | API LY_ERR |
| 1034 | lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision, |
| 1035 | char **localfile, LYS_INFORMAT *format) |
| 1036 | { |
| 1037 | size_t len, flen, match_len = 0, dir_len; |
| 1038 | int i, implicit_cwd = 0, ret = EXIT_FAILURE; |
| 1039 | char *wd, *wn = NULL; |
| 1040 | DIR *dir = NULL; |
| 1041 | struct dirent *file; |
| 1042 | char *match_name = NULL; |
| 1043 | LYS_INFORMAT format_aux, match_format = 0; |
| 1044 | struct ly_set *dirs; |
| 1045 | struct stat st; |
| 1046 | |
| 1047 | LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| 1048 | |
| 1049 | /* start to fill the dir fifo with the context's search path (if set) |
| 1050 | * and the current working directory */ |
| 1051 | dirs = ly_set_new(); |
| 1052 | if (!dirs) { |
| 1053 | LOGMEM(NULL); |
| 1054 | return EXIT_FAILURE; |
| 1055 | } |
| 1056 | |
| 1057 | len = strlen(name); |
| 1058 | if (cwd) { |
| 1059 | wd = get_current_dir_name(); |
| 1060 | if (!wd) { |
| 1061 | LOGMEM(NULL); |
| 1062 | goto cleanup; |
| 1063 | } else { |
| 1064 | /* add implicit current working directory (./) to be searched, |
| 1065 | * this directory is not searched recursively */ |
| 1066 | if (ly_set_add(dirs, wd, 0) == -1) { |
| 1067 | goto cleanup; |
| 1068 | } |
| 1069 | implicit_cwd = 1; |
| 1070 | } |
| 1071 | } |
| 1072 | if (searchpaths) { |
| 1073 | for (i = 0; searchpaths[i]; i++) { |
| 1074 | /* check for duplicities with the implicit current working directory */ |
| 1075 | if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| 1076 | implicit_cwd = 0; |
| 1077 | continue; |
| 1078 | } |
| 1079 | wd = strdup(searchpaths[i]); |
| 1080 | if (!wd) { |
| 1081 | LOGMEM(NULL); |
| 1082 | goto cleanup; |
| 1083 | } else if (ly_set_add(dirs, wd, 0) == -1) { |
| 1084 | goto cleanup; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | wd = NULL; |
| 1089 | |
| 1090 | /* start searching */ |
| 1091 | while (dirs->count) { |
| 1092 | free(wd); |
| 1093 | free(wn); wn = NULL; |
| 1094 | |
| 1095 | dirs->count--; |
| 1096 | wd = (char *)dirs->objs[dirs->count]; |
| 1097 | dirs->objs[dirs->count] = NULL; |
| 1098 | LOGVRB("Searching for \"%s\" in %s.", name, wd); |
| 1099 | |
| 1100 | if (dir) { |
| 1101 | closedir(dir); |
| 1102 | } |
| 1103 | dir = opendir(wd); |
| 1104 | dir_len = strlen(wd); |
| 1105 | if (!dir) { |
| 1106 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 1107 | } else { |
| 1108 | while ((file = readdir(dir))) { |
| 1109 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 1110 | /* skip . and .. */ |
| 1111 | continue; |
| 1112 | } |
| 1113 | free(wn); |
| 1114 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 1115 | LOGMEM(NULL); |
| 1116 | goto cleanup; |
| 1117 | } |
| 1118 | if (stat(wn, &st) == -1) { |
| 1119 | LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)", |
| 1120 | file->d_name, wd, strerror(errno)); |
| 1121 | continue; |
| 1122 | } |
| 1123 | if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) { |
| 1124 | /* we have another subdirectory in searchpath to explore, |
| 1125 | * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */ |
| 1126 | if (ly_set_add(dirs, wn, 0) == -1) { |
| 1127 | goto cleanup; |
| 1128 | } |
| 1129 | /* continue with the next item in current directory */ |
| 1130 | wn = NULL; |
| 1131 | continue; |
| 1132 | } else if (!S_ISREG(st.st_mode)) { |
| 1133 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 1134 | continue; |
| 1135 | } |
| 1136 | |
| 1137 | /* here we know that the item is a file which can contain a module */ |
| 1138 | if (strncmp(name, file->d_name, len) || |
| 1139 | (file->d_name[len] != '.' && file->d_name[len] != '@')) { |
| 1140 | /* different filename than the module we search for */ |
| 1141 | continue; |
| 1142 | } |
| 1143 | |
| 1144 | /* get type according to filename suffix */ |
| 1145 | flen = strlen(file->d_name); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1146 | if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1147 | format_aux = LYS_IN_YANG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1148 | /* TODO YIN parser |
| 1149 | } else if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| 1150 | format_aux = LYS_IN_YIN; |
| 1151 | */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1152 | } else { |
| 1153 | /* not supportde suffix/file format */ |
| 1154 | continue; |
| 1155 | } |
| 1156 | |
| 1157 | if (revision) { |
| 1158 | /* we look for the specific revision, try to get it from the filename */ |
| 1159 | if (file->d_name[len] == '@') { |
| 1160 | /* check revision from the filename */ |
| 1161 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 1162 | /* another revision */ |
| 1163 | continue; |
| 1164 | } else { |
| 1165 | /* exact revision */ |
| 1166 | free(match_name); |
| 1167 | match_name = wn; |
| 1168 | wn = NULL; |
| 1169 | match_len = dir_len + 1 + len; |
| 1170 | match_format = format_aux; |
| 1171 | goto success; |
| 1172 | } |
| 1173 | } else { |
| 1174 | /* continue trying to find exact revision match, use this only if not found */ |
| 1175 | free(match_name); |
| 1176 | match_name = wn; |
| 1177 | wn = NULL; |
| 1178 | match_len = dir_len + 1 +len; |
| 1179 | match_format = format_aux; |
| 1180 | continue; |
| 1181 | } |
| 1182 | } else { |
| 1183 | /* remember the revision and try to find the newest one */ |
| 1184 | if (match_name) { |
| 1185 | if (file->d_name[len] != '@' || |
| 1186 | lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) { |
| 1187 | continue; |
| 1188 | } else if (match_name[match_len] == '@' && |
| 1189 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 1190 | continue; |
| 1191 | } |
| 1192 | free(match_name); |
| 1193 | } |
| 1194 | |
| 1195 | match_name = wn; |
| 1196 | wn = NULL; |
| 1197 | match_len = dir_len + 1 + len; |
| 1198 | match_format = format_aux; |
| 1199 | continue; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | success: |
| 1206 | (*localfile) = match_name; |
| 1207 | match_name = NULL; |
| 1208 | if (format) { |
| 1209 | (*format) = match_format; |
| 1210 | } |
| 1211 | ret = EXIT_SUCCESS; |
| 1212 | |
| 1213 | cleanup: |
| 1214 | free(wn); |
| 1215 | free(wd); |
| 1216 | if (dir) { |
| 1217 | closedir(dir); |
| 1218 | } |
| 1219 | free(match_name); |
| 1220 | ly_set_free(dirs, free); |
| 1221 | |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |