blob: b1bd1cc23277c8d194b6131d0823c19a76fd0fb9 [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
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
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:
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
Michal Vasko63f3d842020-07-08 10:10:14 +020061 return ret;
Radek Krejcie7b95092019-05-15 11:03:07 +020062}
63
Radek Krejci26da5222020-06-09 17:43:17 +020064static LY_ERR
65lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
66{
Michal Vasko63f3d842020-07-08 10:10:14 +020067 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +020068
69 LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);
70
Michal Vasko63f3d842020-07-08 10:10:14 +020071 ret = lys_print(out, module, format, line_length, options);
Radek Krejci26da5222020-06-09 17:43:17 +020072
73 ly_out_free(out, NULL, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +020074 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +020075}
76
77API LY_ERR
78lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
79{
80 struct ly_out *out;
81
82 LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL);
83
84 /* init */
85 *strp = NULL;
86
Radek Krejci84ce7b12020-06-11 17:28:25 +020087 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Radek Krejci26da5222020-06-09 17:43:17 +020088 return lys_print_(out, module, format, line_length, options);
89}
90
91API LY_ERR
92lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
93{
94 struct ly_out *out;
95
96 LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL);
97
Radek Krejci84ce7b12020-06-11 17:28:25 +020098 LY_CHECK_RET(ly_out_new_fd(fd, &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_file(FILE *f, 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, f, module, LY_EINVAL);
108
Radek Krejci84ce7b12020-06-11 17:28:25 +0200109 LY_CHECK_RET(ly_out_new_file(f, &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_path(const char *path, 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, path, module, LY_EINVAL);
119
Radek Krejci84ce7b12020-06-11 17:28:25 +0200120 LY_CHECK_RET(ly_out_new_filepath(path, &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_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
126 const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options)
127{
128 struct ly_out *out;
129
130 LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);
131
Radek Krejci84ce7b12020-06-11 17:28:25 +0200132 LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &out));
Radek Krejci26da5222020-06-09 17:43:17 +0200133 return lys_print_(out, module, format, line_length, options);
134}
135
Michal Vasko63f3d842020-07-08 10:10:14 +0200136API LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +0200137lys_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 +0800138{
139 LY_ERR ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800140
Michal Vasko63f3d842020-07-08 10:10:14 +0200141 LY_CHECK_ARG_RET(NULL, out, node, LY_EINVAL);
Radek Krejcia5bba312020-01-09 15:41:20 +0100142
Michal Vasko63f3d842020-07-08 10:10:14 +0200143 /* reset number of printed bytes */
144 out->func_printed = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800145
146 switch (format) {
147 case LYS_OUT_YANG_COMPILED:
148 ret = yang_print_compiled_node(out, node, options);
149 break;
150 /* TODO not yet implemented
151 case LYS_OUT_YIN:
152 ret = yin_print_parsed(out, module);
153 break;
154 case LYS_OUT_TREE:
155 ret = tree_print_model(out, module, target_node, line_length, options);
156 break;
157 */
158 default:
Radek Krejcia5bba312020-01-09 15:41:20 +0100159 LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800160 ret = LY_EINVAL;
161 break;
162 }
163
Michal Vasko63f3d842020-07-08 10:10:14 +0200164 return ret;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800165}
166