summaryrefslogtreecommitdiffstats
path: root/src/printer_schema.c
blob: 075c5199fb4919029583b85622702d2ca6b1a17e (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
213
214
215
216
217
218
219
220
221
222
/**
 * @file printer_schema.c
 * @author Radek Krejci <rkrejci@cesnet.cz>
 * @brief Generic schema printers functions.
 *
 * Copyright (c) 2015 - 2019 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
 */

#include "printer_schema.h"

#include <stdio.h>

#include "common.h"
#include "compat.h"
#include "log.h"
#include "out_internal.h"
#include "printer_internal.h"
#include "tree_schema.h"

LIBYANG_API_DEF LY_ERR
lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, size_t line_length,
        uint32_t options)
{
    LY_ERR ret;

    LY_CHECK_ARG_RET(NULL, out, module, LY_EINVAL);

    /* reset number of printed bytes */
    out->func_printed = 0;

    switch (format) {
    case LYS_OUT_YANG:
        if (!module->parsed) {
            LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
            ret = LY_EINVAL;
            break;
        }

        ret = yang_print_parsed_module(out, module->parsed, options);
        break;
    case LYS_OUT_YANG_COMPILED:
        if (!module->compiled) {
            LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" compiled module missing.", module->name);
            ret = LY_EINVAL;
            break;
        }

        ret = yang_print_compiled(out, module, options);
        break;
    case LYS_OUT_YIN:
        if (!module->parsed) {
            LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
            ret = LY_EINVAL;
            break;
        }

        ret = yin_print_parsed_module(out, module->parsed, options);
        break;
    case LYS_OUT_TREE:
        if (!module->parsed) {
            LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
            ret = LY_EINVAL;
            break;
        }
        ret = tree_print_module(out, module, options, line_length);
        break;
    /* TODO not yet implemented
    case LYS_OUT_INFO:
        ret = info_print_model(out, module, target_node);
        break;
    */
    default:
        LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
        ret = LY_EINVAL;
        break;
    }

    return ret;
}

LIBYANG_API_DEF LY_ERR
lys_print_submodule(struct ly_out *out, const struct lysp_submodule *submodule, LYS_OUTFORMAT format,
        size_t line_length, uint32_t options)
{
    LY_ERR ret;

    LY_CHECK_ARG_RET(NULL, out, submodule, LY_EINVAL);

    /* reset number of printed bytes */
    out->func_printed = 0;

    switch (format) {
    case LYS_OUT_YANG:
        ret = yang_print_parsed_submodule(out, submodule, options);
        break;
    case LYS_OUT_YIN:
        ret = yin_print_parsed_submodule(out, submodule, options);
        break;
    case LYS_OUT_TREE:
        ret = tree_print_parsed_submodule(out, submodule, options, line_length);
        break;
    /* TODO not yet implemented
    case LYS_OUT_INFO:
        ret = info_print_model(out, module, target_node);
        break;
    */
    default:
        LOGERR(submodule->mod->ctx, LY_EINVAL, "Unsupported output format.");
        ret = LY_EINVAL;
        break;
    }

    return ret;
}

static LY_ERR
lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
{
    LY_ERR ret;

    LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);

    ret = lys_print_module(out, module, format, 0, options);

    ly_out_free(out, NULL, 0);
    return ret;
}

LIBYANG_API_DEF LY_ERR
lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
{
    struct ly_out *out;

    LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL);

    /* init */
    *strp = NULL;

    LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
    return lys_print_(out, module, format, options);
}

LIBYANG_API_DEF LY_ERR
lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
{
    struct ly_out *out;

    LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL);

    LY_CHECK_RET(ly_out_new_fd(fd, &out));
    return lys_print_(out, module, format, options);
}

LIBYANG_API_DEF LY_ERR
lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
{
    struct ly_out *out;

    LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL);

    LY_CHECK_RET(ly_out_new_file(f, &out));
    return lys_print_(out, module, format, options);
}

LIBYANG_API_DEF LY_ERR
lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
{
    struct ly_out *out;

    LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL);

    LY_CHECK_RET(ly_out_new_filepath(path, &out));
    return lys_print_(out, module, format, options);
}

LIBYANG_API_DEF LY_ERR
lys_print_clb(ly_write_clb writeclb, void *user_data, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
{
    struct ly_out *out;

    LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);

    LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out));
    return lys_print_(out, module, format, options);
}

LIBYANG_API_DEF LY_ERR
lys_print_node(struct ly_out *out, const struct lysc_node *node, LYS_OUTFORMAT format, size_t line_length, uint32_t options)
{
    LY_ERR ret;

    LY_CHECK_ARG_RET(NULL, out, node, LY_EINVAL);

    /* reset number of printed bytes */
    out->func_printed = 0;

    switch (format) {
    case LYS_OUT_YANG_COMPILED:
        ret = yang_print_compiled_node(out, node, options);
        break;
    /* TODO not yet implemented
    case LYS_OUT_YIN:
        ret = yin_print_parsed(out, module);
        break;
    */
    case LYS_OUT_TREE:
        ret = tree_print_compiled_node(out, node, options, line_length);
        break;
    default:
        LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
        ret = LY_EINVAL;
        break;
    }

    return ret;
}