blob: 0caac174470a1ad8df7f7ddb43a3c8d676c7c55a [file] [log] [blame]
aPiecekd8f002f2023-06-21 10:40:41 +02001/**
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
27void
28yl_schema_features_free(void *flist)
29{
aPiecekceae6332023-06-21 10:46:08 +020030 struct yl_schema_features *rec = (struct yl_schema_features *)flist;
aPiecekd8f002f2023-06-21 10:40:41 +020031
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
44void
45get_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) {
aPiecekceae6332023-06-21 10:46:08 +020049 struct yl_schema_features *sf = (struct yl_schema_features *)fset->objs[u];
aPiecekd8f002f2023-06-21 10:40:41 +020050
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
63int
64parse_features(const char *fstring, struct ly_set *fset)
65{
aPiecekceae6332023-06-21 10:46:08 +020066 struct yl_schema_features *rec;
aPiecekd8f002f2023-06-21 10:40:41 +020067 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));
73 return -1;
74 }
75 if (ly_set_add(fset, rec, 1, NULL)) {
76 YLMSG_E("Unable to store features information (%s).\n", strerror(errno));
77 free(rec);
78 return -1;
79 }
80
81 /* fill the record */
82 p = strchr(fstring, ':');
83 if (!p) {
84 YLMSG_E("Invalid format of the features specification (%s).\n", fstring);
85 return -1;
86 }
87 rec->mod_name = strndup(fstring, p - fstring);
88
89 count = 0;
90 while (p) {
91 size_t len = 0;
92 char *token = p + 1;
93
94 p = strchr(token, ',');
95 if (!p) {
96 /* the last item, if any */
97 len = strlen(token);
98 } else {
99 len = p - token;
100 }
101
102 if (len) {
103 fp = realloc(rec->features, (count + 1) * sizeof *rec->features);
104 if (!fp) {
105 YLMSG_E("Unable to store features list information (%s).\n", strerror(errno));
106 return -1;
107 }
108 rec->features = fp;
109 fp = &rec->features[count++]; /* array item to set */
110 (*fp) = strndup(token, len);
111 }
112 }
113
114 /* terminating NULL */
115 fp = realloc(rec->features, (count + 1) * sizeof *rec->features);
116 if (!fp) {
117 YLMSG_E("Unable to store features list information (%s).\n", strerror(errno));
118 return -1;
119 }
120 rec->features = fp;
121 rec->features[count++] = NULL;
122
123 return 0;
124}
125
126int
127collect_features(const struct lys_module *mod, struct ly_set *set)
128{
129 struct lysp_feature *f = NULL;
130 uint32_t idx = 0;
131
132 while ((f = lysp_feature_next(f, mod->parsed, &idx))) {
133 if (ly_set_add(set, (void *)f->name, 1, NULL)) {
134 YLMSG_E("Memory allocation failed.\n");
135 ly_set_erase(set, NULL);
136 return 1;
137 }
138 }
139
140 return 0;
141}
142
143void
144print_features(struct ly_out *out, const struct lys_module *mod, const struct ly_set *set)
145{
146 size_t max_len;
147 uint32_t j;
148 const char *name;
149
150 /* header */
151 ly_print(out, "%s:\n", mod->name);
152
153 /* no features */
154 if (!set->count) {
155 ly_print(out, "\t(none)\n\n");
156 return;
157 }
158
159 /* get max len, so the statuses of all the features will be aligned */
160 max_len = 0;
161 for (j = 0; j < set->count; ++j) {
162 name = set->objs[j];
163 if (strlen(name) > max_len) {
164 max_len = strlen(name);
165 }
166 }
167
168 /* print features */
169 for (j = 0; j < set->count; ++j) {
170 name = set->objs[j];
171 ly_print(out, "\t%-*s (%s)\n", (int)max_len, name, lys_feature_value(mod, name) ? "off" : "on");
172 }
173
174 ly_print(out, "\n");
175}
176
177int
178generate_features_output(const struct lys_module *mod, const struct ly_set *set, char **features_param)
179{
180 uint32_t j;
181 /*
182 * features_len - length of all the features in the current module
183 * added_len - length of a string to be added, = features_len + extra necessary length
184 * param_len - length of the parameter before appending new string
185 */
186 size_t features_len, added_len, param_len;
187 char *tmp;
188
189 features_len = 0;
190 for (j = 0; j < set->count; j++) {
191 features_len += strlen(set->objs[j]);
192 }
193
194 if (j == 0) {
195 /* no features */
196 added_len = strlen("-F ") + strlen(mod->name) + strlen(":");
197 } else {
198 /* j = comma count, -1 because of trailing comma */
199 added_len = strlen("-F ") + strlen(mod->name) + strlen(":") + features_len + j - 1;
200 }
201
202 /* to avoid strlen(NULL) if this is the first call */
203 param_len = 0;
204 if (*features_param) {
205 param_len = strlen(*features_param);
206 }
207
208 /* +1 because of white space at the beginning */
209 tmp = realloc(*features_param, param_len + added_len + 1 + 1);
210 if (!tmp) {
211 goto error;
212 } else {
213 *features_param = tmp;
214 }
215 sprintf(*features_param + param_len, " -F %s:", mod->name);
216
217 for (j = 0; j < set->count; j++) {
218 strcat(*features_param, set->objs[j]);
219 /* no trailing comma */
220 if (j != (set->count - 1)) {
221 strcat(*features_param, ",");
222 }
223 }
224
225 return 0;
226
227error:
228 YLMSG_E("Memory allocation failed (%s:%d, %s).\n", __FILE__, __LINE__, strerror(errno));
229 return 1;
230}
231
232int
233print_all_features(struct ly_out *out, const struct ly_ctx *ctx, uint8_t generate_features, char **features_param)
234{
235 int ret = 0;
236 uint32_t i = 0;
237 struct lys_module *mod;
238 struct ly_set set = {0};
239
240 while ((mod = ly_ctx_get_module_iter(ctx, &i)) != NULL) {
241 /* only care about implemented modules */
242 if (!mod->implemented) {
243 continue;
244 }
245
246 /* always erase the set, so the previous module's features don't carry over to the next module's features */
247 ly_set_erase(&set, NULL);
248
249 if (collect_features(mod, &set)) {
250 ret = 1;
251 goto cleanup;
252 }
253
254 if (generate_features && generate_features_output(mod, &set, features_param)) {
255 ret = 1;
256 goto cleanup;
257 }
258 print_features(out, mod, &set);
259 }
260
261cleanup:
262 ly_set_erase(&set, NULL);
263 return ret;
264}