1 /**
2  * @file printer.h
3  * @author Radek Krejci <rkrejci@cesnet.cz>
4  * @brief Printers for libyang
5  *
6  * Copyright (c) 2015 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 
15 #ifndef LY_PRINTER_H_
16 #define LY_PRINTER_H_
17 
18 #include "libyang.h"
19 #include "tree_schema.h"
20 #include "tree_internal.h"
21 
22 typedef enum LYOUT_TYPE {
23     LYOUT_FD,          /**< file descriptor */
24     LYOUT_STREAM,      /**< FILE stream */
25     LYOUT_MEMORY,      /**< memory */
26     LYOUT_CALLBACK     /**< print via provided callback */
27 } LYOUT_TYPE;
28 
29 struct lyout {
30     LYOUT_TYPE type;
31     union {
32         int fd;
33         FILE *f;
34         struct {
35             char *buf;
36             size_t len;
37             size_t size;
38         } mem;
39         struct {
40             ssize_t (*f)(void *arg, const void *buf, size_t count);
41             void *arg;
42         } clb;
43     } method;
44 
45     /* buffer for holes */
46     char *buffered;
47     size_t buf_len;
48     size_t buf_size;
49 
50     /* hole counter */
51     size_t hole_count;
52 };
53 
54 struct ext_substmt_info_s {
55     const char *name;
56     const char *arg;
57     int flags;
58 #define SUBST_FLAG_YIN 0x1 /**< has YIN element */
59 #define SUBST_FLAG_ID 0x2  /**< the value is identifier -> no quotes */
60 };
61 
62 #define LY_PRINT_SET errno = 0
63 
64 #define LY_PRINT_RET(ctx) if (errno) { LOGERR(ctx, LY_ESYS, "Print error (%s).", strerror(errno)); return EXIT_FAILURE; } else \
65         { return EXIT_SUCCESS; }
66 
67 /* filled in printer.c */
68 extern struct ext_substmt_info_s ext_substmt_info[];
69 
70 /**
71  * @brief Generic printer, replacement for printf() / write() / etc
72  */
73 int ly_print(struct lyout *out, const char *format, ...);
74 void ly_print_flush(struct lyout *out);
75 int ly_write(struct lyout *out, const char *buf, size_t count);
76 int ly_write_skip(struct lyout *out, size_t count, size_t *position);
77 int ly_write_skipped(struct lyout *out, size_t position, const char *buf, size_t count);
78 
79 /* prefix_kind: 0 - print import prefixes for foreign features, 1 - print module names, 2 - print prefixes (tree printer), 3 - print module names including revisions (JSONS printer) */
80 int ly_print_iffeature(struct lyout *out, const struct lys_module *module, struct lys_iffeature *expr, int prefix_kind);
81 
82 int yang_print_model(struct lyout *out, const struct lys_module *module);
83 int yin_print_model(struct lyout *out, const struct lys_module *module);
84 int tree_print_model(struct lyout *out, const struct lys_module *module, const char *target_schema_path, int line_length, int options);
85 int info_print_model(struct lyout *out, const struct lys_module *module, const char *target_schema_path);
86 int jsons_print_model(struct lyout *out, const struct lys_module *module, const char *target_schema_path);
87 
88 int json_print_data(struct lyout *out, const struct lyd_node *root, int options);
89 int xml_print_data(struct lyout *out, const struct lyd_node *root, int options);
90 int xml_print_node(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options);
91 int lyb_print_data(struct lyout *out, const struct lyd_node *root, int options);
92 
93 int lys_print_target(struct lyout *out, const struct lys_module *module, const char *target_schema_path,
94                      void (*clb_print_typedef)(struct lyout*, const struct lys_tpdf*, int*),
95                      void (*clb_print_identity)(struct lyout*, const struct lys_ident*, int*),
96                      void (*clb_print_feature)(struct lyout*, const struct lys_feature*, int*),
97                      void (*clb_print_type)(struct lyout*, const struct lys_type*, int*),
98                      void (*clb_print_grouping)(struct lyout*, const struct lys_node*, int*),
99                      void (*clb_print_container)(struct lyout*, const struct lys_node*, int*),
100                      void (*clb_print_choice)(struct lyout*, const struct lys_node*, int*),
101                      void (*clb_print_leaf)(struct lyout*, const struct lys_node*, int*),
102                      void (*clb_print_leaflist)(struct lyout*, const struct lys_node*, int*),
103                      void (*clb_print_list)(struct lyout*, const struct lys_node*, int*),
104                      void (*clb_print_anydata)(struct lyout*, const struct lys_node*, int*),
105                      void (*clb_print_case)(struct lyout*, const struct lys_node*, int*),
106                      void (*clb_print_notif)(struct lyout*, const struct lys_node*, int*),
107                      void (*clb_print_rpc)(struct lyout*, const struct lys_node*, int*),
108                      void (*clb_print_action)(struct lyout*, const struct lys_node*, int*),
109                      void (*clb_print_input)(struct lyout*, const struct lys_node*, int*),
110                      void (*clb_print_output)(struct lyout*, const struct lys_node*, int*));
111 
112 /* 0 - same, 1 - different */
113 int nscmp(const struct lyd_node *node1, const struct lyd_node *node2);
114 
115 #endif /* LY_PRINTER_H_ */
116