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