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