blob: 61a6c2745af4f0d085b4d586bae1799b61c7fdb4 [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
Radek Krejcia5bba312020-01-09 15:41:20 +010027API ssize_t
Radek Krejci241f6b52020-05-21 18:13:49 +020028lys_print(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, int UNUSED(line_length), int options)
Radek Krejcie7b95092019-05-15 11:03:07 +020029{
30 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010031 size_t printed_prev;
32
33 LY_CHECK_ARG_RET(NULL, out, module, -LY_EINVAL);
34
35 printed_prev = out->printed;
Radek Krejcie7b95092019-05-15 11:03:07 +020036
37 switch (format) {
38 case LYS_OUT_YANG:
39 ret = yang_print_parsed(out, module);
40 break;
41 case LYS_OUT_YANG_COMPILED:
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080042 ret = yang_print_compiled(out, module, options);
Radek Krejcie7b95092019-05-15 11:03:07 +020043 break;
Radek Krejcie7b95092019-05-15 11:03:07 +020044 case LYS_OUT_YIN:
FredGand944bdc2019-11-05 21:57:07 +080045 ret = yin_print_parsed(out, module);
Radek Krejcie7b95092019-05-15 11:03:07 +020046 break;
FredGand944bdc2019-11-05 21:57:07 +080047 /* TODO not yet implemented
Radek Krejcie7b95092019-05-15 11:03:07 +020048 case LYS_OUT_TREE:
49 ret = tree_print_model(out, module, target_node, line_length, options);
50 break;
51 case LYS_OUT_INFO:
52 ret = info_print_model(out, module, target_node);
53 break;
Radek Krejcie7b95092019-05-15 11:03:07 +020054 */
55 default:
Radek Krejcia5bba312020-01-09 15:41:20 +010056 LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +020057 ret = LY_EINVAL;
58 break;
59 }
60
Radek Krejcia5bba312020-01-09 15:41:20 +010061 if (ret) {
62 /* error */
Michal Vaskob130f332020-06-29 11:48:35 +020063 return (-1) * (signed)ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010064 } else {
65 /* success */
66 return (ssize_t)(out->printed - printed_prev);
67 }
Radek Krejcie7b95092019-05-15 11:03:07 +020068}
69
Radek Krejci26da5222020-06-09 17:43:17 +020070static LY_ERR
71lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
72{
73 ssize_t result;
74
75 LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);
76
77 result = lys_print(out, module, format, line_length, options);
78
79 ly_out_free(out, NULL, 0);
80
81 if (result < 0) {
82 return (-1) * result;
83 } else {
84 return LY_SUCCESS;
85 }
86}
87
88API LY_ERR
89lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
90{
91 struct ly_out *out;
92
93 LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL);
94
95 /* init */
96 *strp = NULL;
97
Radek Krejci84ce7b12020-06-11 17:28:25 +020098 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Radek Krejci26da5222020-06-09 17:43:17 +020099 return lys_print_(out, module, format, line_length, options);
100}
101
102API LY_ERR
103lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
104{
105 struct ly_out *out;
106
107 LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL);
108
Radek Krejci84ce7b12020-06-11 17:28:25 +0200109 LY_CHECK_RET(ly_out_new_fd(fd, &out));
Radek Krejci26da5222020-06-09 17:43:17 +0200110 return lys_print_(out, module, format, line_length, options);
111}
112
113API LY_ERR
114lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
115{
116 struct ly_out *out;
117
118 LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL);
119
Radek Krejci84ce7b12020-06-11 17:28:25 +0200120 LY_CHECK_RET(ly_out_new_file(f, &out));
Radek Krejci26da5222020-06-09 17:43:17 +0200121 return lys_print_(out, module, format, line_length, options);
122}
123
124API LY_ERR
125lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
126{
127 struct ly_out *out;
128
129 LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL);
130
Radek Krejci84ce7b12020-06-11 17:28:25 +0200131 LY_CHECK_RET(ly_out_new_filepath(path, &out));
Radek Krejci26da5222020-06-09 17:43:17 +0200132 return lys_print_(out, module, format, line_length, options);
133}
134
135API LY_ERR
136lys_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
137 const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
138{
139 struct ly_out *out;
140
141 LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);
142
Radek Krejci84ce7b12020-06-11 17:28:25 +0200143 LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &out));
Radek Krejci26da5222020-06-09 17:43:17 +0200144 return lys_print_(out, module, format, line_length, options);
145}
146
Radek Krejcia5bba312020-01-09 15:41:20 +0100147API ssize_t
Radek Krejci241f6b52020-05-21 18:13:49 +0200148lys_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 +0800149{
150 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +0100151 size_t printed_prev;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800152
Radek Krejcia5bba312020-01-09 15:41:20 +0100153 LY_CHECK_ARG_RET(NULL, out, node, -LY_EINVAL);
154
155 printed_prev = out->printed;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800156
157 switch (format) {
158 case LYS_OUT_YANG_COMPILED:
159 ret = yang_print_compiled_node(out, node, options);
160 break;
161 /* TODO not yet implemented
162 case LYS_OUT_YIN:
163 ret = yin_print_parsed(out, module);
164 break;
165 case LYS_OUT_TREE:
166 ret = tree_print_model(out, module, target_node, line_length, options);
167 break;
168 */
169 default:
Radek Krejcia5bba312020-01-09 15:41:20 +0100170 LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800171 ret = LY_EINVAL;
172 break;
173 }
174
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800175 if (ret) {
176 /* error */
Michal Vaskob130f332020-06-29 11:48:35 +0200177 return (-1) * (signed)ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800178 } else {
179 /* success */
Radek Krejcia5bba312020-01-09 15:41:20 +0100180 return (ssize_t)(out->printed - printed_prev);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800181 }
182}
183