blob: b4524963fdc13d8041269306a1307548c7a46c42 [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
aPiecekd8f002f2023-06-21 10:40:41 +0200126void
aPiecek6fde6d02023-06-21 15:06:45 +0200127print_features(struct ly_out *out, const struct lys_module *mod)
aPiecekd8f002f2023-06-21 10:40:41 +0200128{
aPiecek6fde6d02023-06-21 15:06:45 +0200129 struct lysp_feature *f;
130 uint32_t idx;
131 size_t max_len, len;
aPiecekd8f002f2023-06-21 10:40:41 +0200132
aPiecekd8f002f2023-06-21 10:40:41 +0200133 ly_print(out, "%s:\n", mod->name);
134
aPiecek6fde6d02023-06-21 15:06:45 +0200135 /* get max len, so the statuses of all the features will be aligned */
136 max_len = 0, idx = 0, f = NULL;
137 while ((f = lysp_feature_next(f, mod->parsed, &idx))) {
138 len = strlen(f->name);
139 max_len = (max_len > len) ? max_len : len;
140 }
141 if (!max_len) {
142 ly_print(out, "\t(none)\n");
aPiecekd8f002f2023-06-21 10:40:41 +0200143 return;
144 }
145
aPiecekd8f002f2023-06-21 10:40:41 +0200146 /* print features */
aPiecek6fde6d02023-06-21 15:06:45 +0200147 idx = 0, f = NULL;
148 while ((f = lysp_feature_next(f, mod->parsed, &idx))) {
149 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 +0200150 }
aPiecekd8f002f2023-06-21 10:40:41 +0200151}
152
aPiecek6fde6d02023-06-21 15:06:45 +0200153void
154print_feature_param(struct ly_out *out, const struct lys_module *mod)
aPiecekd8f002f2023-06-21 10:40:41 +0200155{
aPiecek6fde6d02023-06-21 15:06:45 +0200156 struct lysp_feature *f = NULL;
157 uint32_t idx = 0;
158 uint8_t first = 1;
aPiecekd8f002f2023-06-21 10:40:41 +0200159
aPiecek6fde6d02023-06-21 15:06:45 +0200160 ly_print(out, " -F %s:", mod->name);
161 while ((f = lysp_feature_next(f, mod->parsed, &idx))) {
162 if (first) {
163 ly_print(out, "%s", f->name);
164 first = 0;
165 } else {
166 ly_print(out, ",%s", f->name);
aPiecekd8f002f2023-06-21 10:40:41 +0200167 }
168 }
aPiecekd8f002f2023-06-21 10:40:41 +0200169}
170
aPiecek6fde6d02023-06-21 15:06:45 +0200171void
172print_all_features(struct ly_out *out, const struct ly_ctx *ctx, uint8_t feature_param)
aPiecekd8f002f2023-06-21 10:40:41 +0200173{
aPiecek6fde6d02023-06-21 15:06:45 +0200174 uint32_t i;
aPiecekd8f002f2023-06-21 10:40:41 +0200175 struct lys_module *mod;
aPiecek6fde6d02023-06-21 15:06:45 +0200176 uint8_t first;
aPiecekd8f002f2023-06-21 10:40:41 +0200177
aPiecek6fde6d02023-06-21 15:06:45 +0200178 /* Print features for all implemented modules. */
179 first = 1;
180 i = 0;
aPiecekd8f002f2023-06-21 10:40:41 +0200181 while ((mod = ly_ctx_get_module_iter(ctx, &i)) != NULL) {
aPiecekd8f002f2023-06-21 10:40:41 +0200182 if (!mod->implemented) {
183 continue;
184 }
aPiecek6fde6d02023-06-21 15:06:45 +0200185 if (first) {
186 print_features(out, mod);
187 first = 0;
188 } else {
189 ly_print(out, "\n");
190 print_features(out, mod);
aPiecekd8f002f2023-06-21 10:40:41 +0200191 }
aPiecekd8f002f2023-06-21 10:40:41 +0200192 }
193
aPiecek6fde6d02023-06-21 15:06:45 +0200194 if (!feature_param) {
195 return;
196 }
197 ly_print(out, "\n");
198
199 /* Print features for all implemented modules in 'feature-param' format. */
200 i = 0;
201 while ((mod = ly_ctx_get_module_iter(ctx, &i)) != NULL) {
202 if (mod->implemented) {
203 print_feature_param(out, mod);
204 }
205 }
aPiecekd8f002f2023-06-21 10:40:41 +0200206}