Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file common.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 4 | * @author Adam Piecek <piecek@cesnet.cz> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 5 | * @brief libyang's yanglint tool - common functions and definitions for both interactive and non-interactive mode. |
| 6 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 7 | * Copyright (c) 2023 CESNET, z.s.p.o. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #ifndef COMMON_H_ |
| 17 | #define COMMON_H_ |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #include "libyang.h" |
| 23 | |
| 24 | #define PROMPT "> " |
| 25 | |
| 26 | /** |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 27 | * @brief Default context creation options. |
| 28 | */ |
| 29 | #define YL_DEFAULT_CTX_OPTIONS LY_CTX_NO_YANGLIBRARY |
| 30 | |
| 31 | /** |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 32 | * @brief Default data parsing flags. |
| 33 | */ |
| 34 | #define YL_DEFAULT_DATA_PARSE_OPTIONS LYD_PARSE_STRICT |
| 35 | |
| 36 | /** |
Michal Vasko | 05eaf83 | 2023-02-10 09:21:46 +0100 | [diff] [blame] | 37 | * @brief Default data validation flags. |
| 38 | */ |
| 39 | #define YL_DEFAULT_DATA_VALIDATE_OPTIONS LYD_VALIDATE_MULTI_ERROR |
| 40 | |
| 41 | /** |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 42 | * @brief log error message |
| 43 | */ |
Michal Vasko | 151ae6c | 2021-09-23 08:23:51 +0200 | [diff] [blame] | 44 | #define YLMSG_E(...) \ |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame^] | 45 | yl_log(1, __VA_ARGS__); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * @brief log warning message |
| 49 | */ |
Michal Vasko | 151ae6c | 2021-09-23 08:23:51 +0200 | [diff] [blame] | 50 | #define YLMSG_W(...) \ |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame^] | 51 | yl_log(0, __VA_ARGS__); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 52 | |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 53 | #ifndef _WIN32 |
| 54 | # define PATH_SEPARATOR ":" |
| 55 | #else |
| 56 | # define PATH_SEPARATOR ";" |
| 57 | #endif |
| 58 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 59 | struct cmdline_file; |
| 60 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 61 | /** |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame^] | 62 | * @brief Log a yanglint message. |
| 63 | * |
| 64 | * @param[in] err Whether the message is an error or a warning. |
| 65 | * @param[in] format Message format. |
| 66 | * @param[in] ... Format arguments. |
| 67 | */ |
| 68 | void yl_log(ly_bool err, const char *format, ...); |
| 69 | |
| 70 | /** |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 71 | * @brief Parse path of a schema module file into the directory and module name. |
| 72 | * |
| 73 | * @param[in] path Schema module file path to be parsed. |
| 74 | * @param[out] dir Pointer to the directory path where the file resides. Caller is expected to free the returned string. |
| 75 | * @param[out] module Pointer to the name of the module (without file suffixes or revision information) specified by the |
aPiecek | 27337da | 2023-06-19 14:49:29 +0200 | [diff] [blame] | 76 | * @p path. Caller is expected to free the returned string. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 77 | * @return 0 on success |
| 78 | * @return -1 on error |
| 79 | */ |
| 80 | int parse_schema_path(const char *path, char **dir, char **module); |
| 81 | |
| 82 | /** |
| 83 | * @brief Get input handler for the specified path. |
| 84 | * |
| 85 | * Using the @p format_schema and @p format_data the type of the file can be limited (by providing NULL) or it can be |
| 86 | * got known if both types are possible. |
| 87 | * |
| 88 | * @param[in] filepath Path of the file to open. |
| 89 | * @param[out] format_schema Format of the schema detected from the file name. If NULL specified, the schema formats are |
| 90 | * prohibited and such files are refused. |
| 91 | * @param[out] format_data Format of the data detected from the file name. If NULL specified, the data formats are |
| 92 | * prohibited and such files are refused. |
aPiecek | 2457b5e | 2023-06-12 11:40:14 +0200 | [diff] [blame] | 93 | * @param[out] in Created input handler referring the file behind the @p filepath. Can be NULL. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 94 | * @return 0 on success. |
| 95 | * @return -1 on failure. |
| 96 | */ |
| 97 | int get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in); |
| 98 | |
| 99 | /** |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 100 | * @brief Get schema format of the @p filename's content according to the @p filename's suffix. |
| 101 | * |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 102 | * @param[in] filename Name of the file to examine. |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 103 | * @return Detected schema input format. |
| 104 | */ |
| 105 | LYS_INFORMAT get_schema_format(const char *filename); |
| 106 | |
| 107 | /** |
| 108 | * @brief Get data format of the @p filename's content according to the @p filename's suffix. |
| 109 | * |
| 110 | * @param[in] filename Name of the file to examine. |
| 111 | * @return Detected data input format. |
| 112 | */ |
| 113 | LYD_FORMAT get_data_format(const char *filename); |
| 114 | |
| 115 | /** |
| 116 | * @brief Get format of the @p filename's content according to the @p filename's suffix. |
| 117 | * |
| 118 | * Either the @p schema or @p data parameter is set. |
| 119 | * |
aPiecek | 27337da | 2023-06-19 14:49:29 +0200 | [diff] [blame] | 120 | * @param[in] filepath Name of the file to examine. |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 121 | * @param[out] schema_form Pointer to a variable to store the input schema format. |
| 122 | * @param[out] data_form Pointer to a variable to store the expected input data format. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 123 | * @return zero in case a format was successfully detected. |
| 124 | * @return nonzero in case it is not possible to get valid format from the @p filename. |
| 125 | */ |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 126 | int get_format(const char *filepath, LYS_INFORMAT *schema_form, LYD_FORMAT *data_form); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 127 | |
| 128 | /** |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 129 | * @brief Get the node specified by the path. |
| 130 | * |
| 131 | * @param[in] ctx libyang context with schema. |
| 132 | * @param[in] schema_path Path to the wanted node. |
| 133 | * @return Pointer to the schema node specified by the path on success, NULL otherwise. |
| 134 | */ |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 135 | const struct lysc_node *find_schema_path(const struct ly_ctx *ctx, const char *schema_path); |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 136 | |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 137 | /** |
| 138 | * @brief General callback providing run-time extension instance data. |
| 139 | * |
| 140 | * @param[in] ext Compiled extension instance. |
| 141 | * @param[in] user_data User-supplied callback data. |
| 142 | * @param[out] ext_data Provided extension instance data. |
| 143 | * @param[out] ext_data_free Whether the extension instance should free @p ext_data or not. |
| 144 | * @return LY_ERR value. |
| 145 | */ |
| 146 | LY_ERR ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free); |
| 147 | |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 148 | /** |
| 149 | * @brief Concatenation of paths into one string. |
| 150 | * |
| 151 | * @param[in,out] searchpaths Collection of paths in the single string. Paths are delimited by colon ":" |
| 152 | * (on Windows, used semicolon ";" instead). |
| 153 | * @param[in] path Path to add. |
| 154 | * @return LY_ERR value. |
| 155 | */ |
| 156 | LY_ERR searchpath_strcat(char **searchpaths, const char *path); |
| 157 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 158 | #endif /* COMMON_H_ */ |