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 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include "common.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 20 | #include "compat.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include "log.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 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 26 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 27 | lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, size_t UNUSED(line_length), |
| 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 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 45 | ret = yang_print_parsed_module(out, module, 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 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 63 | ret = yin_print_parsed_module(out, module, module->parsed, options); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 64 | break; |
| 65 | /* TODO not yet implemented |
| 66 | case LYS_OUT_TREE: |
| 67 | ret = tree_print_model(out, module, target_node, line_length, options); |
| 68 | break; |
| 69 | case LYS_OUT_INFO: |
| 70 | ret = info_print_model(out, module, target_node); |
| 71 | break; |
| 72 | */ |
| 73 | default: |
| 74 | LOGERR(module->ctx, LY_EINVAL, "Unsupported output format."); |
| 75 | ret = LY_EINVAL; |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | API LY_ERR |
| 83 | lys_print_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodule, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 84 | LYS_OUTFORMAT format, size_t UNUSED(line_length), uint32_t options) |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 85 | { |
| 86 | LY_ERR ret; |
| 87 | |
| 88 | LY_CHECK_ARG_RET(NULL, out, module, submodule, LY_EINVAL); |
| 89 | |
| 90 | /* reset number of printed bytes */ |
| 91 | out->func_printed = 0; |
| 92 | |
| 93 | switch (format) { |
| 94 | case LYS_OUT_YANG: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 95 | ret = yang_print_parsed_submodule(out, module, submodule, options); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 96 | break; |
| 97 | case LYS_OUT_YIN: |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 98 | ret = yin_print_parsed_submodule(out, module, submodule, options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 99 | break; |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 100 | /* TODO not yet implemented |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 101 | case LYS_OUT_TREE: |
| 102 | ret = tree_print_model(out, module, target_node, line_length, options); |
| 103 | break; |
| 104 | case LYS_OUT_INFO: |
| 105 | ret = info_print_model(out, module, target_node); |
| 106 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 107 | */ |
| 108 | default: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 109 | LOGERR(module->ctx, LY_EINVAL, "Unsupported output format."); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 110 | ret = LY_EINVAL; |
| 111 | break; |
| 112 | } |
| 113 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 114 | return ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 117 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 118 | 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] | 119 | { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 120 | LY_ERR ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 121 | |
| 122 | LY_CHECK_ARG_RET(NULL, out, LY_EINVAL); |
| 123 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 124 | ret = lys_print_module(out, module, format, 0, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 125 | |
| 126 | ly_out_free(out, NULL, 0); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 127 | return ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 131 | 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] | 132 | { |
| 133 | struct ly_out *out; |
| 134 | |
| 135 | LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL); |
| 136 | |
| 137 | /* init */ |
| 138 | *strp = NULL; |
| 139 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 140 | LY_CHECK_RET(ly_out_new_memory(strp, 0, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 141 | return lys_print_(out, module, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 145 | 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] | 146 | { |
| 147 | struct ly_out *out; |
| 148 | |
| 149 | LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL); |
| 150 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 151 | LY_CHECK_RET(ly_out_new_fd(fd, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 152 | return lys_print_(out, module, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 156 | 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] | 157 | { |
| 158 | struct ly_out *out; |
| 159 | |
| 160 | LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL); |
| 161 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 162 | LY_CHECK_RET(ly_out_new_file(f, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 163 | return lys_print_(out, module, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 167 | 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] | 168 | { |
| 169 | struct ly_out *out; |
| 170 | |
| 171 | LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL); |
| 172 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 173 | LY_CHECK_RET(ly_out_new_filepath(path, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 174 | return lys_print_(out, module, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 178 | 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] | 179 | { |
| 180 | struct ly_out *out; |
| 181 | |
| 182 | LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL); |
| 183 | |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 184 | LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 185 | return lys_print_(out, module, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 188 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 189 | lys_print_node(struct ly_out *out, const struct lysc_node *node, LYS_OUTFORMAT format, size_t UNUSED(line_length), uint32_t options) |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 190 | { |
| 191 | LY_ERR ret; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 192 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 193 | LY_CHECK_ARG_RET(NULL, out, node, LY_EINVAL); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 194 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 195 | /* reset number of printed bytes */ |
| 196 | out->func_printed = 0; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 197 | |
| 198 | switch (format) { |
| 199 | case LYS_OUT_YANG_COMPILED: |
| 200 | ret = yang_print_compiled_node(out, node, options); |
| 201 | break; |
| 202 | /* TODO not yet implemented |
| 203 | case LYS_OUT_YIN: |
| 204 | ret = yin_print_parsed(out, module); |
| 205 | break; |
| 206 | case LYS_OUT_TREE: |
| 207 | ret = tree_print_model(out, module, target_node, line_length, options); |
| 208 | break; |
| 209 | */ |
| 210 | default: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 211 | LOGERR(NULL, LY_EINVAL, "Unsupported output format."); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 212 | ret = LY_EINVAL; |
| 213 | break; |
| 214 | } |
| 215 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 216 | return ret; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 217 | } |