Coverage Report

Created: 2025-11-09 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libyang/src/printer_schema.c
Line
Count
Source
1
/**
2
 * @file printer_schema.c
3
 * @author Radek Krejci <rkrejci@cesnet.cz>
4
 * @brief Generic schema printers functions.
5
 *
6
 * Copyright (c) 2015 - 2019 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
#include "printer_schema.h"
16
17
#include <stdio.h>
18
19
#include "common.h"
20
#include "compat.h"
21
#include "log.h"
22
#include "out_internal.h"
23
#include "printer_internal.h"
24
#include "tree_schema.h"
25
26
API LY_ERR
27
lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, size_t line_length,
28
        uint32_t options)
29
0
{
30
0
    LY_ERR ret;
31
32
0
    LY_CHECK_ARG_RET(NULL, out, module, LY_EINVAL);
33
34
    /* reset number of printed bytes */
35
0
    out->func_printed = 0;
36
37
0
    switch (format) {
38
0
    case LYS_OUT_YANG:
39
0
        if (!module->parsed) {
40
0
            LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
41
0
            ret = LY_EINVAL;
42
0
            break;
43
0
        }
44
45
0
        ret = yang_print_parsed_module(out, module->parsed, options);
46
0
        break;
47
0
    case LYS_OUT_YANG_COMPILED:
48
0
        if (!module->compiled) {
49
0
            LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" compiled module missing.", module->name);
50
0
            ret = LY_EINVAL;
51
0
            break;
52
0
        }
53
54
0
        ret = yang_print_compiled(out, module, options);
55
0
        break;
56
0
    case LYS_OUT_YIN:
57
0
        if (!module->parsed) {
58
0
            LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
59
0
            ret = LY_EINVAL;
60
0
            break;
61
0
        }
62
63
0
        ret = yin_print_parsed_module(out, module->parsed, options);
64
0
        break;
65
0
    case LYS_OUT_TREE:
66
0
        if (!module->parsed) {
67
0
            LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
68
0
            ret = LY_EINVAL;
69
0
            break;
70
0
        }
71
0
        ret = tree_print_module(out, module, options, line_length);
72
0
        break;
73
    /* TODO not yet implemented
74
    case LYS_OUT_INFO:
75
        ret = info_print_model(out, module, target_node);
76
        break;
77
    */
78
0
    default:
79
0
        LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
80
0
        ret = LY_EINVAL;
81
0
        break;
82
0
    }
83
84
0
    return ret;
85
0
}
86
87
API LY_ERR
88
lys_print_submodule(struct ly_out *out, const struct lysp_submodule *submodule, LYS_OUTFORMAT format,
89
        size_t line_length, uint32_t options)
90
0
{
91
0
    LY_ERR ret;
92
93
0
    LY_CHECK_ARG_RET(NULL, out, submodule, LY_EINVAL);
94
95
    /* reset number of printed bytes */
96
0
    out->func_printed = 0;
97
98
0
    switch (format) {
99
0
    case LYS_OUT_YANG:
100
0
        ret = yang_print_parsed_submodule(out, submodule, options);
101
0
        break;
102
0
    case LYS_OUT_YIN:
103
0
        ret = yin_print_parsed_submodule(out, submodule, options);
104
0
        break;
105
0
    case LYS_OUT_TREE:
106
0
        ret = tree_print_parsed_submodule(out, submodule, options, line_length);
107
0
        break;
108
    /* TODO not yet implemented
109
    case LYS_OUT_INFO:
110
        ret = info_print_model(out, module, target_node);
111
        break;
112
    */
113
0
    default:
114
0
        LOGERR(submodule->mod->ctx, LY_EINVAL, "Unsupported output format.");
115
0
        ret = LY_EINVAL;
116
0
        break;
117
0
    }
118
119
0
    return ret;
120
0
}
121
122
static LY_ERR
123
lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
124
0
{
125
0
    LY_ERR ret;
126
127
0
    LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);
128
129
0
    ret = lys_print_module(out, module, format, 0, options);
130
131
0
    ly_out_free(out, NULL, 0);
132
0
    return ret;
133
0
}
134
135
API LY_ERR
136
lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
137
0
{
138
0
    struct ly_out *out;
139
140
0
    LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL);
141
142
    /* init */
143
0
    *strp = NULL;
144
145
0
    LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
146
0
    return lys_print_(out, module, format, options);
147
0
}
148
149
API LY_ERR
150
lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
151
0
{
152
0
    struct ly_out *out;
153
154
0
    LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL);
155
156
0
    LY_CHECK_RET(ly_out_new_fd(fd, &out));
157
0
    return lys_print_(out, module, format, options);
158
0
}
159
160
API LY_ERR
161
lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
162
0
{
163
0
    struct ly_out *out;
164
165
0
    LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL);
166
167
0
    LY_CHECK_RET(ly_out_new_file(f, &out));
168
0
    return lys_print_(out, module, format, options);
169
0
}
170
171
API LY_ERR
172
lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
173
0
{
174
0
    struct ly_out *out;
175
176
0
    LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL);
177
178
0
    LY_CHECK_RET(ly_out_new_filepath(path, &out));
179
0
    return lys_print_(out, module, format, options);
180
0
}
181
182
API LY_ERR
183
lys_print_clb(ly_write_clb writeclb, void *user_data, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
184
0
{
185
0
    struct ly_out *out;
186
187
0
    LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);
188
189
0
    LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out));
190
0
    return lys_print_(out, module, format, options);
191
0
}
192
193
API LY_ERR
194
lys_print_node(struct ly_out *out, const struct lysc_node *node, LYS_OUTFORMAT format, size_t line_length, uint32_t options)
195
0
{
196
0
    LY_ERR ret;
197
198
0
    LY_CHECK_ARG_RET(NULL, out, node, LY_EINVAL);
199
200
    /* reset number of printed bytes */
201
0
    out->func_printed = 0;
202
203
0
    switch (format) {
204
0
    case LYS_OUT_YANG_COMPILED:
205
0
        ret = yang_print_compiled_node(out, node, options);
206
0
        break;
207
    /* TODO not yet implemented
208
    case LYS_OUT_YIN:
209
        ret = yin_print_parsed(out, module);
210
        break;
211
    */
212
0
    case LYS_OUT_TREE:
213
0
        ret = tree_print_compiled_node(out, node, options, line_length);
214
0
        break;
215
0
    default:
216
0
        LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
217
0
        ret = LY_EINVAL;
218
0
        break;
219
0
    }
220
221
0
    return ret;
222
0
}