blob: 24f772f7f02aa19ad38f9dcabbfb8af849db7e2e [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
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
Radek Krejcica376bd2020-06-11 16:04:06 +020015#include "printer_schema.h"
16
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020020#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "log.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020022#include "out_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "printer_internal.h"
24#include "tree_schema.h"
25
Michal Vasko63f3d842020-07-08 10:10:14 +020026API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020027lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, size_t UNUSED(line_length),
28 uint32_t options)
Radek Krejcie7b95092019-05-15 11:03:07 +020029{
30 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010031
Michal Vasko63f3d842020-07-08 10:10:14 +020032 LY_CHECK_ARG_RET(NULL, out, module, LY_EINVAL);
Radek Krejcia5bba312020-01-09 15:41:20 +010033
Michal Vasko63f3d842020-07-08 10:10:14 +020034 /* reset number of printed bytes */
35 out->func_printed = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +020036
37 switch (format) {
38 case LYS_OUT_YANG:
Michal Vasko7c8439f2020-08-05 13:25:19 +020039 if (!module->parsed) {
40 LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
41 ret = LY_EINVAL;
42 break;
43 }
44
Radek Krejci5536d282020-08-04 23:27:44 +020045 ret = yang_print_parsed_module(out, module, module->parsed, options);
Radek Krejcie7b95092019-05-15 11:03:07 +020046 break;
47 case LYS_OUT_YANG_COMPILED:
Michal Vasko7c8439f2020-08-05 13:25:19 +020048 if (!module->compiled) {
49 LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" compiled module missing.", module->name);
50 ret = LY_EINVAL;
51 break;
52 }
53
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080054 ret = yang_print_compiled(out, module, options);
Radek Krejcie7b95092019-05-15 11:03:07 +020055 break;
Radek Krejcie7b95092019-05-15 11:03:07 +020056 case LYS_OUT_YIN:
Michal Vasko7c8439f2020-08-05 13:25:19 +020057 if (!module->parsed) {
58 LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
59 ret = LY_EINVAL;
60 break;
61 }
62
Radek Krejci5536d282020-08-04 23:27:44 +020063 ret = yin_print_parsed_module(out, module, module->parsed, options);
Michal Vasko7c8439f2020-08-05 13:25:19 +020064 break;
65 /* TODO not yet implemented
66 case LYS_OUT_TREE:
67 ret = tree_print_model(out, module, target_node, line_length, options);
68 break;
69 case LYS_OUT_INFO:
70 ret = info_print_model(out, module, target_node);
71 break;
72 */
73 default:
74 LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
75 ret = LY_EINVAL;
76 break;
77 }
78
79 return ret;
80}
81
82API LY_ERR
83lys_print_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodule,
Radek Krejci1deb5be2020-08-26 16:43:36 +020084 LYS_OUTFORMAT format, size_t UNUSED(line_length), uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +020085{
86 LY_ERR ret;
87
88 LY_CHECK_ARG_RET(NULL, out, module, submodule, LY_EINVAL);
89
90 /* reset number of printed bytes */
91 out->func_printed = 0;
92
93 switch (format) {
94 case LYS_OUT_YANG:
Radek Krejci5536d282020-08-04 23:27:44 +020095 ret = yang_print_parsed_submodule(out, module, submodule, options);
Michal Vasko7c8439f2020-08-05 13:25:19 +020096 break;
97 case LYS_OUT_YIN:
Radek Krejci5536d282020-08-04 23:27:44 +020098 ret = yin_print_parsed_submodule(out, module, submodule, options);
Radek Krejcie7b95092019-05-15 11:03:07 +020099 break;
FredGand944bdc2019-11-05 21:57:07 +0800100 /* TODO not yet implemented
Radek Krejcie7b95092019-05-15 11:03:07 +0200101 case LYS_OUT_TREE:
102 ret = tree_print_model(out, module, target_node, line_length, options);
103 break;
104 case LYS_OUT_INFO:
105 ret = info_print_model(out, module, target_node);
106 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200107 */
108 default:
Radek Krejcia5bba312020-01-09 15:41:20 +0100109 LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +0200110 ret = LY_EINVAL;
111 break;
112 }
113
Michal Vasko63f3d842020-07-08 10:10:14 +0200114 return ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200115}
116
Radek Krejci26da5222020-06-09 17:43:17 +0200117static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200118lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200119{
Michal Vasko63f3d842020-07-08 10:10:14 +0200120 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200121
122 LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);
123
Michal Vasko7c8439f2020-08-05 13:25:19 +0200124 ret = lys_print_module(out, module, format, 0, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200125
126 ly_out_free(out, NULL, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200127 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200128}
129
130API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200131lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200132{
133 struct ly_out *out;
134
135 LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL);
136
137 /* init */
138 *strp = NULL;
139
Radek Krejci84ce7b12020-06-11 17:28:25 +0200140 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200141 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200142}
143
144API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200145lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200146{
147 struct ly_out *out;
148
149 LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL);
150
Radek Krejci84ce7b12020-06-11 17:28:25 +0200151 LY_CHECK_RET(ly_out_new_fd(fd, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200152 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200153}
154
155API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200156lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200157{
158 struct ly_out *out;
159
160 LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL);
161
Radek Krejci84ce7b12020-06-11 17:28:25 +0200162 LY_CHECK_RET(ly_out_new_file(f, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200163 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200164}
165
166API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200167lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200168{
169 struct ly_out *out;
170
171 LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL);
172
Radek Krejci84ce7b12020-06-11 17:28:25 +0200173 LY_CHECK_RET(ly_out_new_filepath(path, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200174 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200175}
176
177API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200178lys_print_clb(ly_write_clb writeclb, void *user_data, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200179{
180 struct ly_out *out;
181
182 LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);
183
Michal Vaskoce2b8742020-08-24 13:20:25 +0200184 LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200185 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200186}
187
Michal Vasko63f3d842020-07-08 10:10:14 +0200188API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200189lys_print_node(struct ly_out *out, const struct lysc_node *node, LYS_OUTFORMAT format, size_t UNUSED(line_length), uint32_t options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800190{
191 LY_ERR ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800192
Michal Vasko63f3d842020-07-08 10:10:14 +0200193 LY_CHECK_ARG_RET(NULL, out, node, LY_EINVAL);
Radek Krejcia5bba312020-01-09 15:41:20 +0100194
Michal Vasko63f3d842020-07-08 10:10:14 +0200195 /* reset number of printed bytes */
196 out->func_printed = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800197
198 switch (format) {
199 case LYS_OUT_YANG_COMPILED:
200 ret = yang_print_compiled_node(out, node, options);
201 break;
202 /* TODO not yet implemented
203 case LYS_OUT_YIN:
204 ret = yin_print_parsed(out, module);
205 break;
206 case LYS_OUT_TREE:
207 ret = tree_print_model(out, module, target_node, line_length, options);
208 break;
209 */
210 default:
Radek Krejcia5bba312020-01-09 15:41:20 +0100211 LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800212 ret = LY_EINVAL;
213 break;
214 }
215
Michal Vasko63f3d842020-07-08 10:10:14 +0200216 return ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800217}