blob: 4e34078fa8be002bc29a58b116dc49220ac5219e [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
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 Krejcie7b95092019-05-15 11:03:07 +020015#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <unistd.h>
17
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include "common.h"
19#include "config.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include "log.h"
21#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "tree_schema.h"
24
Radek Krejcia5bba312020-01-09 15:41:20 +010025API ssize_t
Radek Krejci241f6b52020-05-21 18:13:49 +020026lys_print(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, int UNUSED(line_length), int options)
Radek Krejcie7b95092019-05-15 11:03:07 +020027{
28 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010029 size_t printed_prev;
30
31 LY_CHECK_ARG_RET(NULL, out, module, -LY_EINVAL);
32
33 printed_prev = out->printed;
Radek Krejcie7b95092019-05-15 11:03:07 +020034
35 switch (format) {
36 case LYS_OUT_YANG:
37 ret = yang_print_parsed(out, module);
38 break;
39 case LYS_OUT_YANG_COMPILED:
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080040 ret = yang_print_compiled(out, module, options);
Radek Krejcie7b95092019-05-15 11:03:07 +020041 break;
Radek Krejcie7b95092019-05-15 11:03:07 +020042 case LYS_OUT_YIN:
FredGand944bdc2019-11-05 21:57:07 +080043 ret = yin_print_parsed(out, module);
Radek Krejcie7b95092019-05-15 11:03:07 +020044 break;
FredGand944bdc2019-11-05 21:57:07 +080045 /* TODO not yet implemented
Radek Krejcie7b95092019-05-15 11:03:07 +020046 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 Krejcie7b95092019-05-15 11:03:07 +020052 */
53 default:
Radek Krejcia5bba312020-01-09 15:41:20 +010054 LOGERR(module->ctx, LY_EINVAL, "Unsupported output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +020055 ret = LY_EINVAL;
56 break;
57 }
58
Radek Krejcia5bba312020-01-09 15:41:20 +010059 if (ret) {
60 /* error */
61 return (-1) * ret;
62 } else {
63 /* success */
64 return (ssize_t)(out->printed - printed_prev);
65 }
Radek Krejcie7b95092019-05-15 11:03:07 +020066}
67
Radek Krejcia5bba312020-01-09 15:41:20 +010068API ssize_t
Radek Krejci241f6b52020-05-21 18:13:49 +020069lys_print_node(struct ly_out *out, const struct lysc_node *node, LYS_OUTFORMAT format, int UNUSED(line_length), int options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080070{
71 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010072 size_t printed_prev;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080073
Radek Krejcia5bba312020-01-09 15:41:20 +010074 LY_CHECK_ARG_RET(NULL, out, node, -LY_EINVAL);
75
76 printed_prev = out->printed;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080077
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 Krejcia5bba312020-01-09 15:41:20 +010091 LOGERR(NULL, LY_EINVAL, "Unsupported output format.");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080092 ret = LY_EINVAL;
93 break;
94 }
95
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080096 if (ret) {
97 /* error */
98 return (-1) * ret;
99 } else {
100 /* success */
Radek Krejcia5bba312020-01-09 15:41:20 +0100101 return (ssize_t)(out->printed - printed_prev);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800102 }
103}
104