blob: 12213bb7237e241169ebfaae12b9960e23afa088 [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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010026LIBYANG_API_DEF 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;
Michal Vasko7c8439f2020-08-05 13:25:19 +020073 default:
74 LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
75 ret = LY_EINVAL;
76 break;
77 }
78
79 return ret;
80}
81
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010082LIBYANG_API_DEF LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +010083lys_print_submodule(struct ly_out *out, const struct lysp_submodule *submodule, LYS_OUTFORMAT format,
aPiecek9f792e52021-04-21 08:33:56 +020084 size_t line_length, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +020085{
86 LY_ERR ret;
87
Michal Vasko7997d5a2021-02-22 10:55:56 +010088 LY_CHECK_ARG_RET(NULL, out, submodule, LY_EINVAL);
Michal Vasko7c8439f2020-08-05 13:25:19 +020089
90 /* reset number of printed bytes */
91 out->func_printed = 0;
92
93 switch (format) {
94 case LYS_OUT_YANG:
Michal Vasko7997d5a2021-02-22 10:55:56 +010095 ret = yang_print_parsed_submodule(out, submodule, options);
Michal Vasko7c8439f2020-08-05 13:25:19 +020096 break;
97 case LYS_OUT_YIN:
Michal Vasko7997d5a2021-02-22 10:55:56 +010098 ret = yin_print_parsed_submodule(out, submodule, options);
Radek Krejcie7b95092019-05-15 11:03:07 +020099 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200100 case LYS_OUT_TREE:
aPiecek9f792e52021-04-21 08:33:56 +0200101 ret = tree_print_parsed_submodule(out, submodule, options, line_length);
Radek Krejcie7b95092019-05-15 11:03:07 +0200102 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200103 default:
Michal Vasko7997d5a2021-02-22 10:55:56 +0100104 LOGERR(submodule->mod->ctx, LY_EINVAL, "Unsupported output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +0200105 ret = LY_EINVAL;
106 break;
107 }
108
Michal Vasko63f3d842020-07-08 10:10:14 +0200109 return ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200110}
111
Radek Krejci26da5222020-06-09 17:43:17 +0200112static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200113lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200114{
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200116
117 LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);
118
Michal Vasko7c8439f2020-08-05 13:25:19 +0200119 ret = lys_print_module(out, module, format, 0, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200120
121 ly_out_free(out, NULL, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200122 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200123}
124
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100125LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200126lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200127{
128 struct ly_out *out;
129
130 LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL);
131
132 /* init */
133 *strp = NULL;
134
Radek Krejci84ce7b12020-06-11 17:28:25 +0200135 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200136 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200137}
138
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100139LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200140lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200141{
142 struct ly_out *out;
143
144 LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL);
145
Radek Krejci84ce7b12020-06-11 17:28:25 +0200146 LY_CHECK_RET(ly_out_new_fd(fd, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200147 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200148}
149
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100150LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200151lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200152{
153 struct ly_out *out;
154
155 LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL);
156
Radek Krejci84ce7b12020-06-11 17:28:25 +0200157 LY_CHECK_RET(ly_out_new_file(f, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200158 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200159}
160
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100161LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200162lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200163{
164 struct ly_out *out;
165
166 LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL);
167
Radek Krejci84ce7b12020-06-11 17:28:25 +0200168 LY_CHECK_RET(ly_out_new_filepath(path, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200169 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200170}
171
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100172LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200173lys_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 +0200174{
175 struct ly_out *out;
176
177 LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);
178
Michal Vaskoce2b8742020-08-24 13:20:25 +0200179 LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200180 return lys_print_(out, module, format, options);
Radek Krejci26da5222020-06-09 17:43:17 +0200181}
182
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100183LIBYANG_API_DEF LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +0200184lys_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 +0800185{
186 LY_ERR ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800187
Michal Vasko63f3d842020-07-08 10:10:14 +0200188 LY_CHECK_ARG_RET(NULL, out, node, LY_EINVAL);
Radek Krejcia5bba312020-01-09 15:41:20 +0100189
Michal Vasko63f3d842020-07-08 10:10:14 +0200190 /* reset number of printed bytes */
191 out->func_printed = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800192
193 switch (format) {
194 case LYS_OUT_YANG_COMPILED:
195 ret = yang_print_compiled_node(out, node, options);
196 break;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800197 case LYS_OUT_TREE:
aPiecek61d062b2020-11-02 11:05:09 +0100198 ret = tree_print_compiled_node(out, node, options, line_length);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800199 break;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800200 default:
Radek Krejcia5bba312020-01-09 15:41:20 +0100201 LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800202 ret = LY_EINVAL;
203 break;
204 }
205
Michal Vasko63f3d842020-07-08 10:10:14 +0200206 return ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800207}