Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 15 | #include "printer_schema.h" |
| 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 19 | #include "compat.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include "log.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 21 | #include "ly_common.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 22 | #include "out_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include "printer_internal.h" |
| 24 | #include "tree_schema.h" |
| 25 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 26 | LIBYANG_API_DEF LY_ERR |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 27 | lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, size_t line_length, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 28 | uint32_t options) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 29 | { |
| 30 | LY_ERR ret; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 31 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 32 | LY_CHECK_ARG_RET(NULL, out, module, LY_EINVAL); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 33 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 34 | /* reset number of printed bytes */ |
| 35 | out->func_printed = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 36 | |
| 37 | switch (format) { |
| 38 | case LYS_OUT_YANG: |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 39 | 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 Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 45 | ret = yang_print_parsed_module(out, module->parsed, options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 46 | break; |
| 47 | case LYS_OUT_YANG_COMPILED: |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 48 | 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 Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 54 | ret = yang_print_compiled(out, module, options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 55 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 56 | case LYS_OUT_YIN: |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 57 | 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 Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 63 | ret = yin_print_parsed_module(out, module->parsed, options); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 64 | break; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 65 | case LYS_OUT_TREE: |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 66 | if (!module->parsed) { |
| 67 | LOGERR(module->ctx, LY_EINVAL, "Module \"%s\" parsed module missing.", module->name); |
| 68 | ret = LY_EINVAL; |
| 69 | break; |
| 70 | } |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 71 | ret = tree_print_module(out, module, options, line_length); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 72 | break; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 73 | 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át | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 82 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 83 | lys_print_submodule(struct ly_out *out, const struct lysp_submodule *submodule, LYS_OUTFORMAT format, |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 84 | size_t line_length, uint32_t options) |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 85 | { |
| 86 | LY_ERR ret; |
| 87 | |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 88 | LY_CHECK_ARG_RET(NULL, out, submodule, LY_EINVAL); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 89 | |
| 90 | /* reset number of printed bytes */ |
| 91 | out->func_printed = 0; |
| 92 | |
| 93 | switch (format) { |
| 94 | case LYS_OUT_YANG: |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 95 | ret = yang_print_parsed_submodule(out, submodule, options); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 96 | break; |
| 97 | case LYS_OUT_YIN: |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 98 | ret = yin_print_parsed_submodule(out, submodule, options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 99 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 100 | case LYS_OUT_TREE: |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 101 | ret = tree_print_parsed_submodule(out, submodule, options, line_length); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 102 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 103 | default: |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 104 | LOGERR(submodule->mod->ctx, LY_EINVAL, "Unsupported output format."); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 105 | ret = LY_EINVAL; |
| 106 | break; |
| 107 | } |
| 108 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 109 | return ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 112 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 113 | lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 114 | { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 115 | LY_ERR ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 116 | |
| 117 | LY_CHECK_ARG_RET(NULL, out, LY_EINVAL); |
| 118 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 119 | ret = lys_print_module(out, module, format, 0, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 120 | |
| 121 | ly_out_free(out, NULL, 0); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 122 | return ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 125 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 126 | lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 127 | { |
| 128 | struct ly_out *out; |
| 129 | |
| 130 | LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL); |
| 131 | |
| 132 | /* init */ |
| 133 | *strp = NULL; |
| 134 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 135 | LY_CHECK_RET(ly_out_new_memory(strp, 0, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 136 | return lys_print_(out, module, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 139 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 140 | lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 141 | { |
| 142 | struct ly_out *out; |
| 143 | |
| 144 | LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL); |
| 145 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 146 | LY_CHECK_RET(ly_out_new_fd(fd, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 147 | return lys_print_(out, module, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 150 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 151 | lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 152 | { |
| 153 | struct ly_out *out; |
| 154 | |
| 155 | LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL); |
| 156 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 157 | LY_CHECK_RET(ly_out_new_file(f, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 158 | return lys_print_(out, module, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 161 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 162 | lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 163 | { |
| 164 | struct ly_out *out; |
| 165 | |
| 166 | LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL); |
| 167 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 168 | LY_CHECK_RET(ly_out_new_filepath(path, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 169 | return lys_print_(out, module, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 172 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 173 | lys_print_clb(ly_write_clb writeclb, void *user_data, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 174 | { |
| 175 | struct ly_out *out; |
| 176 | |
| 177 | LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL); |
| 178 | |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 179 | LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 180 | return lys_print_(out, module, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 183 | LIBYANG_API_DEF LY_ERR |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 184 | lys_print_node(struct ly_out *out, const struct lysc_node *node, LYS_OUTFORMAT format, size_t line_length, uint32_t options) |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 185 | { |
| 186 | LY_ERR ret; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 187 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 188 | LY_CHECK_ARG_RET(NULL, out, node, LY_EINVAL); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 189 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 190 | /* reset number of printed bytes */ |
| 191 | out->func_printed = 0; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 192 | |
| 193 | switch (format) { |
| 194 | case LYS_OUT_YANG_COMPILED: |
| 195 | ret = yang_print_compiled_node(out, node, options); |
| 196 | break; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 197 | case LYS_OUT_TREE: |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 198 | ret = tree_print_compiled_node(out, node, options, line_length); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 199 | break; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 200 | default: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 201 | LOGERR(NULL, LY_EINVAL, "Unsupported output format."); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 202 | ret = LY_EINVAL; |
| 203 | break; |
| 204 | } |
| 205 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 206 | return ret; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 207 | } |