aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file yl_schema_features.c |
| 3 | * @author Adam Piecek <piecek@cesnet.cz> |
| 4 | * @brief Control features for the schema. |
| 5 | * |
| 6 | * Copyright (c) 2023 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 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <stdlib.h> /* calloc */ |
| 19 | #include <string.h> /* strcmp */ |
| 20 | |
| 21 | #include "compat.h" /* strndup */ |
| 22 | #include "set.h" /* ly_set */ |
| 23 | |
| 24 | #include "common.h" |
| 25 | #include "yl_schema_features.h" |
| 26 | |
| 27 | void |
| 28 | yl_schema_features_free(void *flist) |
| 29 | { |
aPiecek | ceae633 | 2023-06-21 10:46:08 +0200 | [diff] [blame] | 30 | struct yl_schema_features *rec = (struct yl_schema_features *)flist; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 31 | |
| 32 | if (rec) { |
| 33 | free(rec->mod_name); |
| 34 | if (rec->features) { |
| 35 | for (uint32_t u = 0; rec->features[u]; ++u) { |
| 36 | free(rec->features[u]); |
| 37 | } |
| 38 | free(rec->features); |
| 39 | } |
| 40 | free(rec); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | get_features(const struct ly_set *fset, const char *module, const char ***features) |
| 46 | { |
| 47 | /* get features list for this module */ |
| 48 | for (uint32_t u = 0; u < fset->count; ++u) { |
aPiecek | ceae633 | 2023-06-21 10:46:08 +0200 | [diff] [blame] | 49 | struct yl_schema_features *sf = (struct yl_schema_features *)fset->objs[u]; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 50 | |
| 51 | if (!strcmp(module, sf->mod_name)) { |
| 52 | /* matched module - explicitly set features */ |
| 53 | *features = (const char **)sf->features; |
| 54 | sf->applied = 1; |
| 55 | return; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /* features not set so disable all */ |
| 60 | *features = NULL; |
| 61 | } |
| 62 | |
| 63 | int |
| 64 | parse_features(const char *fstring, struct ly_set *fset) |
| 65 | { |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 66 | struct yl_schema_features *rec = NULL; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 67 | uint32_t count; |
| 68 | char *p, **fp; |
| 69 | |
| 70 | rec = calloc(1, sizeof *rec); |
| 71 | if (!rec) { |
| 72 | YLMSG_E("Unable to allocate features information record (%s).\n", strerror(errno)); |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 73 | goto error; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* fill the record */ |
| 77 | p = strchr(fstring, ':'); |
| 78 | if (!p) { |
| 79 | YLMSG_E("Invalid format of the features specification (%s).\n", fstring); |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 80 | goto error; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 81 | } |
| 82 | rec->mod_name = strndup(fstring, p - fstring); |
| 83 | |
| 84 | count = 0; |
| 85 | while (p) { |
| 86 | size_t len = 0; |
| 87 | char *token = p + 1; |
| 88 | |
| 89 | p = strchr(token, ','); |
| 90 | if (!p) { |
| 91 | /* the last item, if any */ |
| 92 | len = strlen(token); |
| 93 | } else { |
| 94 | len = p - token; |
| 95 | } |
| 96 | |
| 97 | if (len) { |
| 98 | fp = realloc(rec->features, (count + 1) * sizeof *rec->features); |
| 99 | if (!fp) { |
| 100 | YLMSG_E("Unable to store features list information (%s).\n", strerror(errno)); |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 101 | goto error; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 102 | } |
| 103 | rec->features = fp; |
| 104 | fp = &rec->features[count++]; /* array item to set */ |
| 105 | (*fp) = strndup(token, len); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /* terminating NULL */ |
| 110 | fp = realloc(rec->features, (count + 1) * sizeof *rec->features); |
| 111 | if (!fp) { |
| 112 | YLMSG_E("Unable to store features list information (%s).\n", strerror(errno)); |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 113 | goto error; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 114 | } |
| 115 | rec->features = fp; |
| 116 | rec->features[count++] = NULL; |
| 117 | |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 118 | /* Store record to the output set. */ |
| 119 | if (ly_set_add(fset, rec, 1, NULL)) { |
| 120 | YLMSG_E("Unable to store features information (%s).\n", strerror(errno)); |
| 121 | goto error; |
| 122 | } |
| 123 | rec = NULL; |
| 124 | |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 125 | return 0; |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 126 | |
| 127 | error: |
| 128 | yl_schema_features_free(rec); |
| 129 | return -1; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 130 | } |
| 131 | |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 132 | void |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 133 | print_features(struct ly_out *out, const struct lys_module *mod) |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 134 | { |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 135 | struct lysp_feature *f; |
| 136 | uint32_t idx; |
| 137 | size_t max_len, len; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 138 | |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 139 | ly_print(out, "%s:\n", mod->name); |
| 140 | |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 141 | /* get max len, so the statuses of all the features will be aligned */ |
| 142 | max_len = 0, idx = 0, f = NULL; |
| 143 | while ((f = lysp_feature_next(f, mod->parsed, &idx))) { |
| 144 | len = strlen(f->name); |
| 145 | max_len = (max_len > len) ? max_len : len; |
| 146 | } |
| 147 | if (!max_len) { |
| 148 | ly_print(out, "\t(none)\n"); |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 152 | /* print features */ |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 153 | idx = 0, f = NULL; |
| 154 | while ((f = lysp_feature_next(f, mod->parsed, &idx))) { |
| 155 | ly_print(out, "\t%-*s (%s)\n", (int)max_len, f->name, lys_feature_value(mod, f->name) ? "off" : "on"); |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 156 | } |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 157 | } |
| 158 | |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 159 | void |
| 160 | print_feature_param(struct ly_out *out, const struct lys_module *mod) |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 161 | { |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 162 | struct lysp_feature *f = NULL; |
| 163 | uint32_t idx = 0; |
| 164 | uint8_t first = 1; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 165 | |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 166 | ly_print(out, " -F %s:", mod->name); |
| 167 | while ((f = lysp_feature_next(f, mod->parsed, &idx))) { |
| 168 | if (first) { |
| 169 | ly_print(out, "%s", f->name); |
| 170 | first = 0; |
| 171 | } else { |
| 172 | ly_print(out, ",%s", f->name); |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 173 | } |
| 174 | } |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 175 | } |
| 176 | |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 177 | void |
| 178 | print_all_features(struct ly_out *out, const struct ly_ctx *ctx, uint8_t feature_param) |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 179 | { |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 180 | uint32_t i; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 181 | struct lys_module *mod; |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 182 | uint8_t first; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 183 | |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 184 | /* Print features for all implemented modules. */ |
| 185 | first = 1; |
| 186 | i = 0; |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 187 | while ((mod = ly_ctx_get_module_iter(ctx, &i)) != NULL) { |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 188 | if (!mod->implemented) { |
| 189 | continue; |
| 190 | } |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 191 | if (first) { |
| 192 | print_features(out, mod); |
| 193 | first = 0; |
| 194 | } else { |
| 195 | ly_print(out, "\n"); |
| 196 | print_features(out, mod); |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 197 | } |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 198 | } |
| 199 | |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 200 | if (!feature_param) { |
| 201 | return; |
| 202 | } |
| 203 | ly_print(out, "\n"); |
| 204 | |
| 205 | /* Print features for all implemented modules in 'feature-param' format. */ |
| 206 | i = 0; |
| 207 | while ((mod = ly_ctx_get_module_iter(ctx, &i)) != NULL) { |
| 208 | if (mod->implemented) { |
| 209 | print_feature_param(out, mod); |
| 210 | } |
| 211 | } |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 212 | } |