blob: 2e25c092d6b40b9c57913e11278bbc55fa874c2e [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file common.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang's yanglint tool - common functions and definitions for both interactive and non-interactive mode.
5 *
6 * Copyright (c) 2020 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 COMMON_H_
16#define COMMON_H_
17
18#include <stdint.h>
19#include <stdio.h>
20
21#include "libyang.h"
22
23#define PROMPT "> "
24
25/**
Michal Vaskoc431a0a2021-01-25 14:31:58 +010026 * @brief Default context creation options.
27 */
28#define YL_DEFAULT_CTX_OPTIONS LY_CTX_NO_YANGLIBRARY
29
30/**
Michal Vasko667ce6b2021-01-25 15:00:27 +010031 * @brief Default data parsing flags.
32 */
33#define YL_DEFAULT_DATA_PARSE_OPTIONS LYD_PARSE_STRICT
34
35/**
Radek Krejcie9f13b12020-11-09 17:42:04 +010036 * @brief log error message
37 */
Michal Vasko151ae6c2021-09-23 08:23:51 +020038#define YLMSG_E(...) \
39 fprintf(stderr, "YANGLINT[E]: " __VA_ARGS__)
Radek Krejcie9f13b12020-11-09 17:42:04 +010040
41/**
42 * @brief log warning message
43 */
Michal Vasko151ae6c2021-09-23 08:23:51 +020044#define YLMSG_W(...) \
45 fprintf(stderr, "YANGLINT[W]: " __VA_ARGS__)
Radek Krejcie9f13b12020-11-09 17:42:04 +010046
47/**
48 * @brief Storage for the list of the features (their names) in a specific YANG module.
49 */
50struct schema_features {
51 char *module;
52 char **features;
53};
54
55/**
56 * @brief Data connected with a file provided on a command line as a file path.
57 */
58struct cmdline_file {
59 struct ly_in *in;
60 const char *path;
61 LYD_FORMAT format;
62};
63
64/**
65 * @brief Free the schema features list (struct schema_features *)
66 * @param[in,out] flist The (struct schema_features *) to free.
67 */
68void free_features(void *flist);
69
70/**
71 * @brief Get the list of features connected with the specific YANG module.
72 *
73 * @param[in] fset The set of features information (struct schema_features *).
74 * @param[in] module Name of the YANG module which features should be found.
75 * @param[out] features Pointer to the list of features being returned.
76 */
77void get_features(struct ly_set *fset, const char *module, const char ***features);
78
79/**
80 * @brief Parse features being specified for the specific YANG module.
81 *
82 * Format of the input @p fstring is as follows: <module_name>:[<feature>,]*
83 *
84 * @param[in] fstring Input string to be parsed.
85 * @param[in, out] fset Features information set (of struct schema_features *). The set is being filled.
86 */
87int parse_features(const char *fstring, struct ly_set *fset);
88
89/**
90 * @brief Parse path of a schema module file into the directory and module name.
91 *
92 * @param[in] path Schema module file path to be parsed.
93 * @param[out] dir Pointer to the directory path where the file resides. Caller is expected to free the returned string.
94 * @param[out] module Pointer to the name of the module (without file suffixes or revision information) specified by the
95 * @path. Caller is expected to free the returned string.
96 * @return 0 on success
97 * @return -1 on error
98 */
99int parse_schema_path(const char *path, char **dir, char **module);
100
101/**
102 * @brief Get input handler for the specified path.
103 *
104 * Using the @p format_schema and @p format_data the type of the file can be limited (by providing NULL) or it can be
105 * got known if both types are possible.
106 *
107 * @param[in] filepath Path of the file to open.
108 * @param[out] format_schema Format of the schema detected from the file name. If NULL specified, the schema formats are
109 * prohibited and such files are refused.
110 * @param[out] format_data Format of the data detected from the file name. If NULL specified, the data formats are
111 * prohibited and such files are refused.
112 * @param[out] in Created input handler referring the file behind the @p filepath.
113 * @return 0 on success.
114 * @return -1 on failure.
115 */
116int get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in);
117
118/**
119 * @brief Free the command line file data (struct cmdline_file *)
120 * @param[in,out] cmdline_file The (struct cmdline_file *) to free.
121 */
122void free_cmdline_file(void *cmdline_file);
123
124/**
125 * @brief Create and fill the command line file data (struct cmdline_file *).
126 * @param[in] set Optional parameter in case the record is supposed to be added into a set.
127 * @param[in] in Input file handler.
128 * @param[in] path Filepath of the file.
129 * @param[in] format Format of the data file.
130 * @return The created command line file structure.
131 * @return NULL on failure
132 */
133struct cmdline_file *fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format);
134
135/**
136 * @brief Helper function to prepare argc, argv pair from a command line string.
137 *
138 * @param[in] cmdline Complete command line string.
139 * @param[out] argc_p Pointer to store argc value.
140 * @param[out] argv_p Pointer to store argv vector.
141 * @return 0 on success, non-zero on failure.
142 */
143int parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[]);
144
145/**
146 * @brief Destructor for the argument vector prepared by ::parse_cmdline().
147 *
148 * @param[in,out] argv Argument vector to destroy.
149 */
150void free_cmdline(char *argv[]);
151
152/**
153 * @brief Get expected format of the @p filename's content according to the @p filename's suffix.
154 * @param[in] filename Name of the file to examine.
155 * @param[out] schema Pointer to a variable to store the expected input schema format. Do not provide the pointer in case a
156 * schema format is not expected.
157 * @param[out] data Pointer to a variable to store the expected input data format. Do not provide the pointer in case a data
158 * format is not expected.
159 * @return zero in case a format was successfully detected.
160 * @return nonzero in case it is not possible to get valid format from the @p filename.
161 */
162int get_format(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data);
163
164/**
165 * @brief Print list of schemas in the context.
166 *
167 * @param[in] out Output handler where to print.
168 * @param[in] ctx Context to print.
169 * @param[in] outformat Optional output format. If not specified (:LYD_UNKNOWN), a simple list with single module per line
170 * is printed. Otherwise, the ietf-yang-library data are printed in the specified format.
171 * @return zero in case the data successfully printed.
172 * @return nonzero in case of error.
173 */
174int print_list(struct ly_out *out, struct ly_ctx *ctx, LYD_FORMAT outformat);
175
176/**
Radek Krejcie9f13b12020-11-09 17:42:04 +0100177 * @brief Process the input data files - parse, validate and print according to provided options.
178 *
179 * @param[in] ctx libyang context with schema.
Michal Vaskoe0665742021-02-11 11:08:44 +0100180 * @param[in] data_type The type of data in the input files.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100181 * @param[in] merge Flag if the data should be merged before validation.
182 * @param[in] format Data format for printing.
183 * @param[in] out The output handler for printing.
184 * @param[in] options_parse Parser options.
185 * @param[in] options_validate Validation options.
186 * @param[in] options_print Printer options.
187 * @param[in] operational_f Optional operational datastore file information for the case of an extended validation of
188 * operation(s).
189 * @param[in] inputs Set of file informations of input data files.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100190 * @param[in] xpath The set of XPaths to be evaluated on the processed data tree, basic information about the resulting set
191 * is printed. Alternative to data printing.
192 * return LY_ERR value.
193 */
Michal Vaskoe0665742021-02-11 11:08:44 +0100194LY_ERR process_data(struct ly_ctx *ctx, enum lyd_type data_type, uint8_t merge, LYD_FORMAT format, struct ly_out *out,
Radek Krejcie9f13b12020-11-09 17:42:04 +0100195 uint32_t options_parse, uint32_t options_validate, uint32_t options_print,
Radek Krejci6784a4e2020-12-09 14:23:05 +0100196 struct cmdline_file *operational_f, struct ly_set *inputs, struct ly_set *xpaths);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100197
198#endif /* COMMON_H_ */