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 | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 68 | API ssize_t |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 69 | 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] | 70 | { |
| 71 | LY_ERR ret; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 72 | size_t printed_prev; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 73 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 74 | LY_CHECK_ARG_RET(NULL, out, node, -LY_EINVAL); |
| 75 | |
| 76 | printed_prev = out->printed; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 77 | |
| 78 | switch (format) { |
| 79 | case LYS_OUT_YANG_COMPILED: |
| 80 | ret = yang_print_compiled_node(out, node, options); |
| 81 | break; |
| 82 | /* TODO not yet implemented |
| 83 | case LYS_OUT_YIN: |
| 84 | ret = yin_print_parsed(out, module); |
| 85 | break; |
| 86 | case LYS_OUT_TREE: |
| 87 | ret = tree_print_model(out, module, target_node, line_length, options); |
| 88 | break; |
| 89 | */ |
| 90 | default: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 91 | LOGERR(NULL, LY_EINVAL, "Unsupported output format."); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 92 | ret = LY_EINVAL; |
| 93 | break; |
| 94 | } |
| 95 | |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 96 | if (ret) { |
| 97 | /* error */ |
| 98 | return (-1) * ret; |
| 99 | } else { |
| 100 | /* success */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 101 | return (ssize_t)(out->printed - printed_prev); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |