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