blob: aba842e2e30c6572b641eddabcfc9c33521f7f81 [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 Krejcie7b95092019-05-15 11:03:07 +020015#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <unistd.h>
17
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include "common.h"
19#include "config.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include "log.h"
21#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "tree_schema.h"
24
Radek Krejcia5bba312020-01-09 15:41:20 +010025API ssize_t
Radek Krejci241f6b52020-05-21 18:13:49 +020026lys_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 +020027{
28 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010029 size_t printed_prev;
30
31 LY_CHECK_ARG_RET(NULL, out, module, -LY_EINVAL);
32
33 printed_prev = out->printed;
Radek Krejcie7b95092019-05-15 11:03:07 +020034
35 switch (format) {
36 case LYS_OUT_YANG:
37 ret = yang_print_parsed(out, module);
38 break;
39 case LYS_OUT_YANG_COMPILED:
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080040 ret = yang_print_compiled(out, module, options);
Radek Krejcie7b95092019-05-15 11:03:07 +020041 break;
Radek Krejcie7b95092019-05-15 11:03:07 +020042 case LYS_OUT_YIN:
FredGand944bdc2019-11-05 21:57:07 +080043 ret = yin_print_parsed(out, module);
Radek Krejcie7b95092019-05-15 11:03:07 +020044 break;
FredGand944bdc2019-11-05 21:57:07 +080045 /* TODO not yet implemented
Radek Krejcie7b95092019-05-15 11:03:07 +020046 case LYS_OUT_TREE:
47 ret = tree_print_model(out, module, target_node, line_length, options);
48 break;
49 case LYS_OUT_INFO:
50 ret = info_print_model(out, module, target_node);
51 break;
Radek Krejcie7b95092019-05-15 11:03:07 +020052 */
53 default:
Radek Krejcia5bba312020-01-09 15:41:20 +010054 LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +020055 ret = LY_EINVAL;
56 break;
57 }
58
Radek Krejcia5bba312020-01-09 15:41:20 +010059 if (ret) {
60 /* error */
61 return (-1) * ret;
62 } else {
63 /* success */
64 return (ssize_t)(out->printed - printed_prev);
65 }
Radek Krejcie7b95092019-05-15 11:03:07 +020066}
67
Radek Krejci26da5222020-06-09 17:43:17 +020068static LY_ERR
69lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
70{
71 ssize_t result;
72
73 LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);
74
75 result = lys_print(out, module, format, line_length, options);
76
77 ly_out_free(out, NULL, 0);
78
79 if (result < 0) {
80 return (-1) * result;
81 } else {
82 return LY_SUCCESS;
83 }
84}
85
86API LY_ERR
87lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
88{
89 struct ly_out *out;
90
91 LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL);
92
93 /* init */
94 *strp = NULL;
95
96 out = ly_out_new_memory(strp, 0);
97 return lys_print_(out, module, format, line_length, options);
98}
99
100API LY_ERR
101lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
102{
103 struct ly_out *out;
104
105 LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL);
106
107 out = ly_out_new_fd(fd);
108 return lys_print_(out, module, format, line_length, options);
109}
110
111API LY_ERR
112lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
113{
114 struct ly_out *out;
115
116 LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL);
117
118 out = ly_out_new_file(f);
119 return lys_print_(out, module, format, line_length, options);
120}
121
122API LY_ERR
123lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
124{
125 struct ly_out *out;
126
127 LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL);
128
129 out = ly_out_new_filepath(path);
130 return lys_print_(out, module, format, line_length, options);
131}
132
133API LY_ERR
134lys_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
135 const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
136{
137 struct ly_out *out;
138
139 LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);
140
141 out = ly_out_new_clb(writeclb, arg);
142 return lys_print_(out, module, format, line_length, options);
143}
144
Radek Krejcia5bba312020-01-09 15:41:20 +0100145API ssize_t
Radek Krejci241f6b52020-05-21 18:13:49 +0200146lys_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 +0800147{
148 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +0100149 size_t printed_prev;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800150
Radek Krejcia5bba312020-01-09 15:41:20 +0100151 LY_CHECK_ARG_RET(NULL, out, node, -LY_EINVAL);
152
153 printed_prev = out->printed;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800154
155 switch (format) {
156 case LYS_OUT_YANG_COMPILED:
157 ret = yang_print_compiled_node(out, node, options);
158 break;
159 /* TODO not yet implemented
160 case LYS_OUT_YIN:
161 ret = yin_print_parsed(out, module);
162 break;
163 case LYS_OUT_TREE:
164 ret = tree_print_model(out, module, target_node, line_length, options);
165 break;
166 */
167 default:
Radek Krejcia5bba312020-01-09 15:41:20 +0100168 LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800169 ret = LY_EINVAL;
170 break;
171 }
172
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800173 if (ret) {
174 /* error */
175 return (-1) * ret;
176 } else {
177 /* success */
Radek Krejcia5bba312020-01-09 15:41:20 +0100178 return (ssize_t)(out->printed - printed_prev);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800179 }
180}
181