blob: 8c201079604cc0997f31b7d85b026642dcd5ceb2 [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#include <unistd.h>
19
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020021#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include "log.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020023#include "out_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "printer_internal.h"
25#include "tree_schema.h"
26
Michal Vasko63f3d842020-07-08 10:10:14 +020027API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020028lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, size_t UNUSED(line_length),
29 uint32_t options)
Radek Krejcie7b95092019-05-15 11:03:07 +020030{
31 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010032
Michal Vasko63f3d842020-07-08 10:10:14 +020033 LY_CHECK_ARG_RET(NULL, out, module, LY_EINVAL);
Radek Krejcia5bba312020-01-09 15:41:20 +010034
Michal Vasko63f3d842020-07-08 10:10:14 +020035 /* reset number of printed bytes */
36 out->func_printed = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +020037
38 switch (format) {
39 case LYS_OUT_YANG:
Michal Vasko7c8439f2020-08-05 13:25:19 +020040 if (!module->parsed) {
41 LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
42 ret = LY_EINVAL;
43 break;
44 }
45
Radek Krejci5536d282020-08-04 23:27:44 +020046 ret = yang_print_parsed_module(out, module, module->parsed, options);
Radek Krejcie7b95092019-05-15 11:03:07 +020047 break;
48 case LYS_OUT_YANG_COMPILED:
Michal Vasko7c8439f2020-08-05 13:25:19 +020049 if (!module->compiled) {
50 LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" compiled module missing.", module->name);
51 ret = LY_EINVAL;
52 break;
53 }
54
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080055 ret = yang_print_compiled(out, module, options);
Radek Krejcie7b95092019-05-15 11:03:07 +020056 break;
Radek Krejcie7b95092019-05-15 11:03:07 +020057 case LYS_OUT_YIN:
Michal Vasko7c8439f2020-08-05 13:25:19 +020058 if (!module->parsed) {
59 LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
60 ret = LY_EINVAL;
61 break;
62 }
63
Radek Krejci5536d282020-08-04 23:27:44 +020064 ret = yin_print_parsed_module(out, module, module->parsed, options);
Michal Vasko7c8439f2020-08-05 13:25:19 +020065 break;
66 /* TODO not yet implemented
67 case LYS_OUT_TREE:
68 ret = tree_print_model(out, module, target_node, line_length, options);
69 break;
70 case LYS_OUT_INFO:
71 ret = info_print_model(out, module, target_node);
72 break;
73 */
74 default:
75 LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
76 ret = LY_EINVAL;
77 break;
78 }
79
80 return ret;
81}
82
83API LY_ERR
84lys_print_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodule,
Radek Krejci1deb5be2020-08-26 16:43:36 +020085 LYS_OUTFORMAT format, size_t UNUSED(line_length), uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +020086{
87 LY_ERR ret;
88
89 LY_CHECK_ARG_RET(NULL, out, module, submodule, LY_EINVAL);
90
91 /* reset number of printed bytes */
92 out->func_printed = 0;
93
94 switch (format) {
95 case LYS_OUT_YANG:
Radek Krejci5536d282020-08-04 23:27:44 +020096 ret = yang_print_parsed_submodule(out, module, submodule, options);
Michal Vasko7c8439f2020-08-05 13:25:19 +020097 break;
98 case LYS_OUT_YIN:
Radek Krejci5536d282020-08-04 23:27:44 +020099 ret = yin_print_parsed_submodule(out, module, submodule, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200100 break;
FredGand944bdc2019-11-05 21:57:07 +0800101 /* TODO not yet implemented
Radek Krejcie7b95092019-05-15 11:03:07 +0200102 case LYS_OUT_TREE:
103 ret = tree_print_model(out, module, target_node, line_length, options);
104 break;
105 case LYS_OUT_INFO:
106 ret = info_print_model(out, module, target_node);
107 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200108 */
109 default:
Radek Krejcia5bba312020-01-09 15:41:20 +0100110 LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +0200111 ret = LY_EINVAL;
112 break;
113 }
114
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 return ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200116}
117
Radek Krejci26da5222020-06-09 17:43:17 +0200118static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200119lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200120{
Michal Vasko63f3d842020-07-08 10:10:14 +0200121 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200122
123 LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);
124
Michal Vasko7c8439f2020-08-05 13:25:19 +0200125 ret = lys_print_module(out, module, format, 0, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200126
127 ly_out_free(out, NULL, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200128 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200129}
130
131API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200132lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200133{
134 struct ly_out *out;
135
136 LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL);
137
138 /* init */
139 *strp = NULL;
140
Radek Krejci84ce7b12020-06-11 17:28:25 +0200141 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200142 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200143}
144
145API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200146lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200147{
148 struct ly_out *out;
149
150 LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL);
151
Radek Krejci84ce7b12020-06-11 17:28:25 +0200152 LY_CHECK_RET(ly_out_new_fd(fd, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200153 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200154}
155
156API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200157lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200158{
159 struct ly_out *out;
160
161 LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL);
162
Radek Krejci84ce7b12020-06-11 17:28:25 +0200163 LY_CHECK_RET(ly_out_new_file(f, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200164 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200165}
166
167API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200168lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200169{
170 struct ly_out *out;
171
172 LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL);
173
Radek Krejci84ce7b12020-06-11 17:28:25 +0200174 LY_CHECK_RET(ly_out_new_filepath(path, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200175 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200176}
177
178API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200179lys_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 +0200180{
181 struct ly_out *out;
182
183 LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);
184
Michal Vaskoce2b8742020-08-24 13:20:25 +0200185 LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200186 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200187}
188
Michal Vasko63f3d842020-07-08 10:10:14 +0200189API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200190lys_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 +0800191{
192 LY_ERR ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800193
Michal Vasko63f3d842020-07-08 10:10:14 +0200194 LY_CHECK_ARG_RET(NULL, out, node, LY_EINVAL);
Radek Krejcia5bba312020-01-09 15:41:20 +0100195
Michal Vasko63f3d842020-07-08 10:10:14 +0200196 /* reset number of printed bytes */
197 out->func_printed = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800198
199 switch (format) {
200 case LYS_OUT_YANG_COMPILED:
201 ret = yang_print_compiled_node(out, node, options);
202 break;
203 /* TODO not yet implemented
204 case LYS_OUT_YIN:
205 ret = yin_print_parsed(out, module);
206 break;
207 case LYS_OUT_TREE:
208 ret = tree_print_model(out, module, target_node, line_length, options);
209 break;
210 */
211 default:
Radek Krejcia5bba312020-01-09 15:41:20 +0100212 LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800213 ret = LY_EINVAL;
214 break;
215 }
216
Michal Vasko63f3d842020-07-08 10:10:14 +0200217 return ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800218}