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 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include "common.h" |
| 21 | #include "config.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 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 27 | API ssize_t |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 28 | lys_print(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, int UNUSED(line_length), int 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 | size_t printed_prev; |
| 32 | |
| 33 | LY_CHECK_ARG_RET(NULL, out, module, -LY_EINVAL); |
| 34 | |
| 35 | printed_prev = out->printed; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 36 | |
| 37 | switch (format) { |
| 38 | case LYS_OUT_YANG: |
| 39 | ret = yang_print_parsed(out, module); |
| 40 | break; |
| 41 | case LYS_OUT_YANG_COMPILED: |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 42 | ret = yang_print_compiled(out, module, options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 43 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 44 | case LYS_OUT_YIN: |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 45 | ret = yin_print_parsed(out, module); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 46 | break; |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 47 | /* TODO not yet implemented |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 48 | 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 Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 54 | */ |
| 55 | default: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 56 | LOGERR(module->ctx, LY_EINVAL, "Unsupported output format."); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 57 | ret = LY_EINVAL; |
| 58 | break; |
| 59 | } |
| 60 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 61 | if (ret) { |
| 62 | /* error */ |
Michal Vasko | b130f33 | 2020-06-29 11:48:35 +0200 | [diff] [blame] | 63 | return (-1) * (signed)ret; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 64 | } else { |
| 65 | /* success */ |
| 66 | return (ssize_t)(out->printed - printed_prev); |
| 67 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 70 | static LY_ERR |
| 71 | lys_print_(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options) |
| 72 | { |
| 73 | ssize_t result; |
| 74 | |
| 75 | LY_CHECK_ARG_RET(NULL, out, LY_EINVAL); |
| 76 | |
| 77 | result = lys_print(out, module, format, line_length, options); |
| 78 | |
| 79 | ly_out_free(out, NULL, 0); |
| 80 | |
| 81 | if (result < 0) { |
| 82 | return (-1) * result; |
| 83 | } else { |
| 84 | return LY_SUCCESS; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | API LY_ERR |
| 89 | lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options) |
| 90 | { |
| 91 | struct ly_out *out; |
| 92 | |
| 93 | LY_CHECK_ARG_RET(NULL, strp, module, LY_EINVAL); |
| 94 | |
| 95 | /* init */ |
| 96 | *strp = NULL; |
| 97 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 98 | LY_CHECK_RET(ly_out_new_memory(strp, 0, &out)); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 99 | return lys_print_(out, module, format, line_length, options); |
| 100 | } |
| 101 | |
| 102 | API LY_ERR |
| 103 | lys_print_fd(int fd, 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, fd != -1, module, LY_EINVAL); |
| 108 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 109 | LY_CHECK_RET(ly_out_new_fd(fd, &out)); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 110 | return lys_print_(out, module, format, line_length, options); |
| 111 | } |
| 112 | |
| 113 | API LY_ERR |
| 114 | lys_print_file(FILE *f, 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, f, module, LY_EINVAL); |
| 119 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 120 | LY_CHECK_RET(ly_out_new_file(f, &out)); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 121 | return lys_print_(out, module, format, line_length, options); |
| 122 | } |
| 123 | |
| 124 | API LY_ERR |
| 125 | lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options) |
| 126 | { |
| 127 | struct ly_out *out; |
| 128 | |
| 129 | LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL); |
| 130 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 131 | LY_CHECK_RET(ly_out_new_filepath(path, &out)); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 132 | return lys_print_(out, module, format, line_length, options); |
| 133 | } |
| 134 | |
| 135 | API LY_ERR |
| 136 | lys_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, |
| 137 | const struct lys_module *module, LYS_OUTFORMAT format, int line_length, int options) |
| 138 | { |
| 139 | struct ly_out *out; |
| 140 | |
| 141 | LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL); |
| 142 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 143 | LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &out)); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 144 | return lys_print_(out, module, format, line_length, options); |
| 145 | } |
| 146 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 147 | API ssize_t |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 148 | 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] | 149 | { |
| 150 | LY_ERR ret; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 151 | size_t printed_prev; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 152 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 153 | LY_CHECK_ARG_RET(NULL, out, node, -LY_EINVAL); |
| 154 | |
| 155 | printed_prev = out->printed; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 156 | |
| 157 | switch (format) { |
| 158 | case LYS_OUT_YANG_COMPILED: |
| 159 | ret = yang_print_compiled_node(out, node, options); |
| 160 | break; |
| 161 | /* TODO not yet implemented |
| 162 | case LYS_OUT_YIN: |
| 163 | ret = yin_print_parsed(out, module); |
| 164 | break; |
| 165 | case LYS_OUT_TREE: |
| 166 | ret = tree_print_model(out, module, target_node, line_length, options); |
| 167 | break; |
| 168 | */ |
| 169 | default: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 170 | LOGERR(NULL, LY_EINVAL, "Unsupported output format."); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 171 | ret = LY_EINVAL; |
| 172 | break; |
| 173 | } |
| 174 | |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 175 | if (ret) { |
| 176 | /* error */ |
Michal Vasko | b130f33 | 2020-06-29 11:48:35 +0200 | [diff] [blame] | 177 | return (-1) * (signed)ret; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 178 | } else { |
| 179 | /* success */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 180 | return (ssize_t)(out->printed - printed_prev); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |