blob: 5c5de8a05f7af7a510056068baabef021f538e60 [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"
21#include "config.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020023#include "printer.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
Michal Vasko7c8439f2020-08-05 13:25:19 +020028lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, int UNUSED(line_length),
29 int 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 Krejci5536d282020-08-04 23:27:44 +020085 LYS_OUTFORMAT format, int UNUSED(line_length), int 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
Michal Vasko7c8439f2020-08-05 13:25:19 +0200119lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, int 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
Michal Vasko7c8439f2020-08-05 13:25:19 +0200132lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, int 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
Michal Vasko7c8439f2020-08-05 13:25:19 +0200146lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, int 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
Michal Vasko7c8439f2020-08-05 13:25:19 +0200157lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, int 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
Michal Vasko7c8439f2020-08-05 13:25:19 +0200168lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, int 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
179lys_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
Michal Vasko7c8439f2020-08-05 13:25:19 +0200180 const struct lys_module *module, LYS_OUTFORMAT format, int options)
Radek Krejci26da5222020-06-09 17:43:17 +0200181{
182 struct ly_out *out;
183
184 LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);
185
Radek Krejci84ce7b12020-06-11 17:28:25 +0200186 LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200187 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200188}
189
Michal Vasko63f3d842020-07-08 10:10:14 +0200190API LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +0200191lys_print_node(struct ly_out *out, const struct lysc_node *node, LYS_OUTFORMAT format, int UNUSED(line_length), int options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800192{
193 LY_ERR ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800194
Michal Vasko63f3d842020-07-08 10:10:14 +0200195 LY_CHECK_ARG_RET(NULL, out, node, LY_EINVAL);
Radek Krejcia5bba312020-01-09 15:41:20 +0100196
Michal Vasko63f3d842020-07-08 10:10:14 +0200197 /* reset number of printed bytes */
198 out->func_printed = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800199
200 switch (format) {
201 case LYS_OUT_YANG_COMPILED:
202 ret = yang_print_compiled_node(out, node, options);
203 break;
204 /* TODO not yet implemented
205 case LYS_OUT_YIN:
206 ret = yin_print_parsed(out, module);
207 break;
208 case LYS_OUT_TREE:
209 ret = tree_print_model(out, module, target_node, line_length, options);
210 break;
211 */
212 default:
Radek Krejcia5bba312020-01-09 15:41:20 +0100213 LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800214 ret = LY_EINVAL;
215 break;
216 }
217
Michal Vasko63f3d842020-07-08 10:10:14 +0200218 return ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800219}
220