blob: 74f88b96d499933a4b519b13185e7a434dcf222d [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{
aPiecekb2b51362023-06-22 09:14:59 +020066 struct yl_schema_features *rec = NULL;
aPiecekd8f002f2023-06-21 10:40:41 +020067 uint32_t count;
68 char *p, **fp;
69
70 rec = calloc(1, sizeof *rec);
71 if (!rec) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020072 YLMSG_E("Unable to allocate features information record (%s).", strerror(errno));
aPiecekb2b51362023-06-22 09:14:59 +020073 goto error;
aPiecekd8f002f2023-06-21 10:40:41 +020074 }
75
76 /* fill the record */
77 p = strchr(fstring, ':');
78 if (!p) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020079 YLMSG_E("Invalid format of the features specification (%s).", fstring);
aPiecekb2b51362023-06-22 09:14:59 +020080 goto error;
aPiecekd8f002f2023-06-21 10:40:41 +020081 }
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) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200100 YLMSG_E("Unable to store features list information (%s).", strerror(errno));
aPiecekb2b51362023-06-22 09:14:59 +0200101 goto error;
aPiecekd8f002f2023-06-21 10:40:41 +0200102 }
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) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200112 YLMSG_E("Unable to store features list information (%s).", strerror(errno));
aPiecekb2b51362023-06-22 09:14:59 +0200113 goto error;
aPiecekd8f002f2023-06-21 10:40:41 +0200114 }
115 rec->features = fp;
116 rec->features[count++] = NULL;
117
aPiecekb2b51362023-06-22 09:14:59 +0200118 /* Store record to the output set. */
119 if (ly_set_add(fset, rec, 1, NULL)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200120 YLMSG_E("Unable to store features information (%s).", strerror(errno));
aPiecekb2b51362023-06-22 09:14:59 +0200121 goto error;
122 }
123 rec = NULL;
124
aPiecekd8f002f2023-06-21 10:40:41 +0200125 return 0;
aPiecekb2b51362023-06-22 09:14:59 +0200126
127error:
128 yl_schema_features_free(rec);
129 return -1;
aPiecekd8f002f2023-06-21 10:40:41 +0200130}
131
aPiecekd8f002f2023-06-21 10:40:41 +0200132void
aPiecek6fde6d02023-06-21 15:06:45 +0200133print_features(struct ly_out *out, const struct lys_module *mod)
aPiecekd8f002f2023-06-21 10:40:41 +0200134{
aPiecek6fde6d02023-06-21 15:06:45 +0200135 struct lysp_feature *f;
136 uint32_t idx;
137 size_t max_len, len;
aPiecekd8f002f2023-06-21 10:40:41 +0200138
aPiecekd8f002f2023-06-21 10:40:41 +0200139 ly_print(out, "%s:\n", mod->name);
140
aPiecek6fde6d02023-06-21 15:06:45 +0200141 /* 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");
aPiecekd8f002f2023-06-21 10:40:41 +0200149 return;
150 }
151
aPiecekd8f002f2023-06-21 10:40:41 +0200152 /* print features */
aPiecek6fde6d02023-06-21 15:06:45 +0200153 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");
aPiecekd8f002f2023-06-21 10:40:41 +0200156 }
aPiecekd8f002f2023-06-21 10:40:41 +0200157}
158
aPiecek6fde6d02023-06-21 15:06:45 +0200159void
160print_feature_param(struct ly_out *out, const struct lys_module *mod)
aPiecekd8f002f2023-06-21 10:40:41 +0200161{
aPiecek6fde6d02023-06-21 15:06:45 +0200162 struct lysp_feature *f = NULL;
163 uint32_t idx = 0;
164 uint8_t first = 1;
aPiecekd8f002f2023-06-21 10:40:41 +0200165
aPiecek6fde6d02023-06-21 15:06:45 +0200166 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);
aPiecekd8f002f2023-06-21 10:40:41 +0200173 }
174 }
aPiecekd8f002f2023-06-21 10:40:41 +0200175}
176
aPiecek6fde6d02023-06-21 15:06:45 +0200177void
178print_all_features(struct ly_out *out, const struct ly_ctx *ctx, uint8_t feature_param)
aPiecekd8f002f2023-06-21 10:40:41 +0200179{
aPiecek6fde6d02023-06-21 15:06:45 +0200180 uint32_t i;
aPiecekd8f002f2023-06-21 10:40:41 +0200181 struct lys_module *mod;
aPiecek6fde6d02023-06-21 15:06:45 +0200182 uint8_t first;
aPiecekd8f002f2023-06-21 10:40:41 +0200183
aPiecek6fde6d02023-06-21 15:06:45 +0200184 /* Print features for all implemented modules. */
185 first = 1;
186 i = 0;
aPiecekd8f002f2023-06-21 10:40:41 +0200187 while ((mod = ly_ctx_get_module_iter(ctx, &i)) != NULL) {
aPiecekd8f002f2023-06-21 10:40:41 +0200188 if (!mod->implemented) {
189 continue;
190 }
aPiecek6fde6d02023-06-21 15:06:45 +0200191 if (first) {
192 print_features(out, mod);
193 first = 0;
194 } else {
195 ly_print(out, "\n");
196 print_features(out, mod);
aPiecekd8f002f2023-06-21 10:40:41 +0200197 }
aPiecekd8f002f2023-06-21 10:40:41 +0200198 }
199
aPiecek6fde6d02023-06-21 15:06:45 +0200200 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 }
aPiecekd8f002f2023-06-21 10:40:41 +0200212}