blob: 80eb2da1110447575d79d7684ce4090f2e9dff18 [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
aPiecek61d062b2020-11-02 11:05:09 +010027lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, size_t line_length,
Radek Krejci1deb5be2020-08-26 16:43:36 +020028 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
Michal Vasko7997d5a2021-02-22 10:55:56 +010045 ret = yang_print_parsed_module(out, 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
Michal Vasko7997d5a2021-02-22 10:55:56 +010063 ret = yin_print_parsed_module(out, module->parsed, options);
Michal Vasko7c8439f2020-08-05 13:25:19 +020064 break;
Michal Vasko7c8439f2020-08-05 13:25:19 +020065 case LYS_OUT_TREE:
aPiecek61d062b2020-11-02 11:05:09 +010066 if (!module->parsed) {
67 LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name);
68 ret = LY_EINVAL;
69 break;
70 }
aPiecek3f247652021-04-19 13:40:25 +020071 ret = tree_print_module(out, module, options, line_length);
Michal Vasko7c8439f2020-08-05 13:25:19 +020072 break;
aPiecek61d062b2020-11-02 11:05:09 +010073 /* TODO not yet implemented
Michal Vasko7c8439f2020-08-05 13:25:19 +020074 case LYS_OUT_INFO:
75 ret = info_print_model(out, module, target_node);
76 break;
77 */
78 default:
79 LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
80 ret = LY_EINVAL;
81 break;
82 }
83
84 return ret;
85}
86
87API LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +010088lys_print_submodule(struct ly_out *out, const struct lysp_submodule *submodule, LYS_OUTFORMAT format,
aPiecek9f792e52021-04-21 08:33:56 +020089 size_t line_length, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +020090{
91 LY_ERR ret;
92
Michal Vasko7997d5a2021-02-22 10:55:56 +010093 LY_CHECK_ARG_RET(NULL, out, submodule, LY_EINVAL);
Michal Vasko7c8439f2020-08-05 13:25:19 +020094
95 /* reset number of printed bytes */
96 out->func_printed = 0;
97
98 switch (format) {
99 case LYS_OUT_YANG:
Michal Vasko7997d5a2021-02-22 10:55:56 +0100100 ret = yang_print_parsed_submodule(out, submodule, options);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200101 break;
102 case LYS_OUT_YIN:
Michal Vasko7997d5a2021-02-22 10:55:56 +0100103 ret = yin_print_parsed_submodule(out, submodule, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200104 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200105 case LYS_OUT_TREE:
aPiecek9f792e52021-04-21 08:33:56 +0200106 ret = tree_print_parsed_submodule(out, submodule, options, line_length);
Radek Krejcie7b95092019-05-15 11:03:07 +0200107 break;
aPiecek9f792e52021-04-21 08:33:56 +0200108 /* TODO not yet implemented
Radek Krejcie7b95092019-05-15 11:03:07 +0200109 case LYS_OUT_INFO:
110 ret = info_print_model(out, module, target_node);
111 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200112 */
113 default:
Michal Vasko7997d5a2021-02-22 10:55:56 +0100114 LOGERR(submodule->mod->ctx, LY_EINVAL, "Unsupported output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +0200115 ret = LY_EINVAL;
116 break;
117 }
118
Michal Vasko63f3d842020-07-08 10:10:14 +0200119 return ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200120}
121
Radek Krejci26da5222020-06-09 17:43:17 +0200122static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200123lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200124{
Michal Vasko63f3d842020-07-08 10:10:14 +0200125 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200126
127 LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);
128
Michal Vasko7c8439f2020-08-05 13:25:19 +0200129 ret = lys_print_module(out, module, format, 0, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200130
131 ly_out_free(out, NULL, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200132 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200133}
134
135API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200136lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200137{
138 struct ly_out *out;
139
140 LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL);
141
142 /* init */
143 *strp = NULL;
144
Radek Krejci84ce7b12020-06-11 17:28:25 +0200145 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200146 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200147}
148
149API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200150lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200151{
152 struct ly_out *out;
153
154 LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL);
155
Radek Krejci84ce7b12020-06-11 17:28:25 +0200156 LY_CHECK_RET(ly_out_new_fd(fd, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200157 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200158}
159
160API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200161lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200162{
163 struct ly_out *out;
164
165 LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL);
166
Radek Krejci84ce7b12020-06-11 17:28:25 +0200167 LY_CHECK_RET(ly_out_new_file(f, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200168 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200169}
170
171API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200172lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200173{
174 struct ly_out *out;
175
176 LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL);
177
Radek Krejci84ce7b12020-06-11 17:28:25 +0200178 LY_CHECK_RET(ly_out_new_filepath(path, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200179 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200180}
181
182API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200183lys_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 +0200184{
185 struct ly_out *out;
186
187 LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);
188
Michal Vaskoce2b8742020-08-24 13:20:25 +0200189 LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200190 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200191}
192
Michal Vasko63f3d842020-07-08 10:10:14 +0200193API LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +0200194lys_print_node(struct ly_out *out, const struct lysc_node *node, LYS_OUTFORMAT format, size_t line_length, uint32_t options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800195{
196 LY_ERR ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800197
Michal Vasko63f3d842020-07-08 10:10:14 +0200198 LY_CHECK_ARG_RET(NULL, out, node, LY_EINVAL);
Radek Krejcia5bba312020-01-09 15:41:20 +0100199
Michal Vasko63f3d842020-07-08 10:10:14 +0200200 /* reset number of printed bytes */
201 out->func_printed = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800202
203 switch (format) {
204 case LYS_OUT_YANG_COMPILED:
205 ret = yang_print_compiled_node(out, node, options);
206 break;
207 /* TODO not yet implemented
208 case LYS_OUT_YIN:
209 ret = yin_print_parsed(out, module);
210 break;
aPiecek153b00f2021-04-20 13:52:57 +0200211 */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800212 case LYS_OUT_TREE:
aPiecek61d062b2020-11-02 11:05:09 +0100213 ret = tree_print_compiled_node(out, node, options, line_length);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800214 break;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800215 default:
Radek Krejcia5bba312020-01-09 15:41:20 +0100216 LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800217 ret = LY_EINVAL;
218 break;
219 }
220
Michal Vasko63f3d842020-07-08 10:10:14 +0200221 return ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800222}