summaryrefslogtreecommitdiffstats
path: root/tools/lint/yl_schema_features.c
blob: 74f88b96d499933a4b519b13185e7a434dcf222d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
 * @file yl_schema_features.c
 * @author Adam Piecek <piecek@cesnet.cz>
 * @brief Control features for the schema.
 *
 * Copyright (c) 2023 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

#define _GNU_SOURCE

#include <errno.h>
#include <stdlib.h> /* calloc */
#include <string.h> /* strcmp */

#include "compat.h" /* strndup */
#include "set.h" /* ly_set */

#include "common.h"
#include "yl_schema_features.h"

void
yl_schema_features_free(void *flist)
{
    struct yl_schema_features *rec = (struct yl_schema_features *)flist;

    if (rec) {
        free(rec->mod_name);
        if (rec->features) {
            for (uint32_t u = 0; rec->features[u]; ++u) {
                free(rec->features[u]);
            }
            free(rec->features);
        }
        free(rec);
    }
}

void
get_features(const struct ly_set *fset, const char *module, const char ***features)
{
    /* get features list for this module */
    for (uint32_t u = 0; u < fset->count; ++u) {
        struct yl_schema_features *sf = (struct yl_schema_features *)fset->objs[u];

        if (!strcmp(module, sf->mod_name)) {
            /* matched module - explicitly set features */
            *features = (const char **)sf->features;
            sf->applied = 1;
            return;
        }
    }

    /* features not set so disable all */
    *features = NULL;
}

int
parse_features(const char *fstring, struct ly_set *fset)
{
    struct yl_schema_features *rec = NULL;
    uint32_t count;
    char *p, **fp;

    rec = calloc(1, sizeof *rec);
    if (!rec) {
        YLMSG_E("Unable to allocate features information record (%s).", strerror(errno));
        goto error;
    }

    /* fill the record */
    p = strchr(fstring, ':');
    if (!p) {
        YLMSG_E("Invalid format of the features specification (%s).", fstring);
        goto error;
    }
    rec->mod_name = strndup(fstring, p - fstring);

    count = 0;
    while (p) {
        size_t len = 0;
        char *token = p + 1;

        p = strchr(token, ',');
        if (!p) {
            /* the last item, if any */
            len = strlen(token);
        } else {
            len = p - token;
        }

        if (len) {
            fp = realloc(rec->features, (count + 1) * sizeof *rec->features);
            if (!fp) {
                YLMSG_E("Unable to store features list information (%s).", strerror(errno));
                goto error;
            }
            rec->features = fp;
            fp = &rec->features[count++]; /* array item to set */
            (*fp) = strndup(token, len);
        }
    }

    /* terminating NULL */
    fp = realloc(rec->features, (count + 1) * sizeof *rec->features);
    if (!fp) {
        YLMSG_E("Unable to store features list information (%s).", strerror(errno));
        goto error;
    }
    rec->features = fp;
    rec->features[count++] = NULL;

    /* Store record to the output set. */
    if (ly_set_add(fset, rec, 1, NULL)) {
        YLMSG_E("Unable to store features information (%s).", strerror(errno));
        goto error;
    }
    rec = NULL;

    return 0;

error:
    yl_schema_features_free(rec);
    return -1;
}

void
print_features(struct ly_out *out, const struct lys_module *mod)
{
    struct lysp_feature *f;
    uint32_t idx;
    size_t max_len, len;

    ly_print(out, "%s:\n", mod->name);

    /* get max len, so the statuses of all the features will be aligned */
    max_len = 0, idx = 0, f = NULL;
    while ((f = lysp_feature_next(f, mod->parsed, &idx))) {
        len = strlen(f->name);
        max_len = (max_len > len) ? max_len : len;
    }
    if (!max_len) {
        ly_print(out, "\t(none)\n");
        return;
    }

    /* print features */
    idx = 0, f = NULL;
    while ((f = lysp_feature_next(f, mod->parsed, &idx))) {
        ly_print(out, "\t%-*s (%s)\n", (int)max_len, f->name, lys_feature_value(mod, f->name) ? "off" : "on");
    }
}

void
print_feature_param(struct ly_out *out, const struct lys_module *mod)
{
    struct lysp_feature *f = NULL;
    uint32_t idx = 0;
    uint8_t first = 1;

    ly_print(out, " -F %s:", mod->name);
    while ((f = lysp_feature_next(f, mod->parsed, &idx))) {
        if (first) {
            ly_print(out, "%s", f->name);
            first = 0;
        } else {
            ly_print(out, ",%s", f->name);
        }
    }
}

void
print_all_features(struct ly_out *out, const struct ly_ctx *ctx, uint8_t feature_param)
{
    uint32_t i;
    struct lys_module *mod;
    uint8_t first;

    /* Print features for all implemented modules. */
    first = 1;
    i = 0;
    while ((mod = ly_ctx_get_module_iter(ctx, &i)) != NULL) {
        if (!mod->implemented) {
            continue;
        }
        if (first) {
            print_features(out, mod);
            first = 0;
        } else {
            ly_print(out, "\n");
            print_features(out, mod);
        }
    }

    if (!feature_param) {
        return;
    }
    ly_print(out, "\n");

    /* Print features for all implemented modules in 'feature-param' format. */
    i = 0;
    while ((mod = ly_ctx_get_module_iter(ctx, &i)) != NULL) {
        if (mod->implemented) {
            print_feature_param(out, mod);
        }
    }
}