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