aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file yl_opt.h |
| 3 | * @author Adam Piecek <piecek@cesnet.cz> |
| 4 | * @brief Settings options for the libyang context. |
| 5 | * |
| 6 | * Copyright (c) 2020 - 2023 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 YL_OPT_H_ |
| 16 | #define YL_OPT_H_ |
| 17 | |
| 18 | #include "parser_data.h" /* enum lyd_type */ |
| 19 | #include "printer_schema.h" /* LYS_OUTFORMAT */ |
| 20 | #include "set.h" /* ly_set */ |
| 21 | |
| 22 | /** |
| 23 | * @brief Data connected with a file provided on a command line as a file path. |
| 24 | */ |
| 25 | struct cmdline_file { |
| 26 | struct ly_in *in; |
| 27 | const char *path; |
| 28 | LYD_FORMAT format; |
| 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * @brief Create and fill the command line file data (struct cmdline_file *). |
| 33 | * @param[in] set Optional parameter in case the record is supposed to be added into a set. |
| 34 | * @param[in] in Input file handler. |
| 35 | * @param[in] path Filepath of the file. |
| 36 | * @param[in] format Format of the data file. |
| 37 | * @return The created command line file structure. |
| 38 | * @return NULL on failure |
| 39 | */ |
| 40 | struct cmdline_file *fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format); |
| 41 | |
| 42 | /** |
| 43 | * @brief Free the command line file data items. |
| 44 | * @param[in,out] rec record to free. |
| 45 | */ |
| 46 | void free_cmdline_file_items(struct cmdline_file *rec); |
| 47 | |
| 48 | /** |
| 49 | * @brief Free the command line file data (struct cmdline_file *). |
| 50 | * @param[in,out] cmdline_file The (struct cmdline_file *) to free. |
| 51 | */ |
| 52 | void free_cmdline_file(void *cmdline_file); |
| 53 | |
| 54 | /** |
| 55 | * @brief Context structure to hold and pass variables in a structured form. |
| 56 | */ |
| 57 | struct yl_opt { |
| 58 | /* Set to 1 if yanglint running in the interactive mode */ |
| 59 | ly_bool interactive; |
| 60 | ly_bool last_one; |
| 61 | |
| 62 | /* libyang context for the run */ |
| 63 | char *yang_lib_file; |
| 64 | uint16_t ctx_options; |
| 65 | |
| 66 | /* prepared output (--output option or stdout by default) */ |
| 67 | ly_bool out_stdout; |
| 68 | struct ly_out *out; |
| 69 | |
| 70 | char *searchpaths; |
| 71 | ly_bool searchdir_unset; |
| 72 | |
| 73 | /* options flags */ |
| 74 | uint8_t list; /* -l option to print list of schemas */ |
| 75 | |
| 76 | /* line length for 'tree' format */ |
| 77 | size_t line_length; /* --tree-line-length */ |
| 78 | |
| 79 | uint32_t dbg_groups; |
| 80 | |
| 81 | /* |
| 82 | * schema |
| 83 | */ |
| 84 | /* set schema modules' features via --features option (struct schema_features *) */ |
| 85 | struct ly_set schema_features; |
| 86 | |
| 87 | /* set of loaded schema modules (struct lys_module *) */ |
| 88 | struct ly_set schema_modules; |
| 89 | |
| 90 | /* options to parse and print schema modules */ |
| 91 | uint32_t schema_parse_options; |
| 92 | uint32_t schema_print_options; |
| 93 | |
| 94 | /* specification of printing schema node subtree, option --schema-node */ |
| 95 | char *schema_node_path; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 96 | char *submodule; |
| 97 | |
| 98 | /* name of file containing explicit context passed to callback |
| 99 | * for schema-mount extension. This also causes a callback to |
| 100 | * be registered. |
| 101 | */ |
| 102 | char *schema_context_filename; |
| 103 | ly_bool extdata_unset; |
| 104 | |
| 105 | /* value of --format in case of schema format */ |
| 106 | LYS_OUTFORMAT schema_out_format; |
| 107 | ly_bool feature_param_format; |
| 108 | ly_bool feature_print_all; |
| 109 | char *features_output; |
| 110 | |
| 111 | /* |
| 112 | * data |
| 113 | */ |
| 114 | /* various options based on --type option */ |
| 115 | enum lyd_type data_type; |
| 116 | uint32_t data_parse_options; |
| 117 | uint32_t data_validate_options; |
| 118 | uint32_t data_print_options; |
| 119 | |
| 120 | /* flag for --merge option */ |
| 121 | uint8_t data_merge; |
| 122 | |
| 123 | /* value of --format in case of data format */ |
| 124 | LYD_FORMAT data_out_format; |
| 125 | |
| 126 | /* value of --in-format in case of data format */ |
| 127 | LYD_FORMAT data_in_format; |
| 128 | |
| 129 | /* input data files (struct cmdline_file *) */ |
| 130 | struct ly_set data_inputs; |
| 131 | |
| 132 | /* storage for --operational */ |
| 133 | struct cmdline_file data_operational; |
| 134 | |
| 135 | /* storage for --reply-rpc */ |
| 136 | struct cmdline_file reply_rpc; |
| 137 | |
| 138 | /* storage for --data-xpath */ |
| 139 | struct ly_set data_xpath; |
| 140 | |
| 141 | char **argv; |
| 142 | }; |
| 143 | |
| 144 | /** |
| 145 | * @brief Erase all values in @p opt. |
| 146 | * |
| 147 | * The yl_opt.interactive item is not deleted. |
| 148 | * |
| 149 | * @param[in,out] yo Option context to erase. |
| 150 | */ |
| 151 | void yl_opt_erase(struct yl_opt *yo); |
| 152 | |
| 153 | /** |
aPiecek | 113e0f0 | 2023-06-09 08:47:48 +0200 | [diff] [blame] | 154 | * @brief Update @p yo according to the @p arg of the schema --format parameter. |
| 155 | * |
| 156 | * @param[in] arg Format parameter argument (for example yang, yin, ...). |
| 157 | * @param[out] yo yanglint options used to update. |
| 158 | * @return 0 on success. |
| 159 | */ |
| 160 | int yl_opt_update_schema_out_format(const char *arg, struct yl_opt *yo); |
| 161 | |
| 162 | /** |
| 163 | * @brief Update @p yo according to the @p arg of the data --format parameter. |
| 164 | * |
| 165 | * @param[in] arg Format parameter argument (for example xml, json, ...). |
| 166 | * @param[out] yo yanglint options used to update. |
| 167 | * @return 0 on success. |
| 168 | */ |
| 169 | int yl_opt_update_data_out_format(const char *arg, struct yl_opt *yo); |
| 170 | |
| 171 | /** |
| 172 | * @brief Update @p yo according to the @p arg of the general --format parameter. |
| 173 | * |
| 174 | * @param[in] arg Format parameter argument (for example yang, xml, ...). |
| 175 | * @param[out] yo yanglint options used to update. |
| 176 | * @return 0 on success. |
| 177 | */ |
| 178 | int yl_opt_update_out_format(const char *arg, struct yl_opt *yo); |
| 179 | |
| 180 | /** |
aPiecek | 3167f38 | 2023-06-09 09:23:10 +0200 | [diff] [blame] | 181 | * @brief Update @p yo according to the @p arg of the data --type parameter. |
| 182 | * |
| 183 | * @param[in] arg Format parameter argument (for example config, rpc, ...). |
| 184 | * @param[out] yo yanglint options used to update. |
| 185 | * @return 0 on success. |
| 186 | */ |
| 187 | int yl_opt_update_data_type(const char *arg, struct yl_opt *yo); |
| 188 | |
| 189 | /** |
aPiecek | 20cc2fe | 2023-06-09 09:32:28 +0200 | [diff] [blame] | 190 | * @brief Update @p yo according to the @p arg of the data --default parameter. |
| 191 | * |
| 192 | * @param[in] arg Format parameter argument (for example all, trim, ...). |
| 193 | * @param[out] yo yanglint options used to update. |
| 194 | * @return 0 on success. |
| 195 | */ |
| 196 | int yo_opt_update_data_default(const char *arg, struct yl_opt *yo); |
| 197 | |
| 198 | /** |
aPiecek | b5dff49 | 2023-06-09 09:38:08 +0200 | [diff] [blame] | 199 | * @brief Update @p yo according to the @p arg of the data --in-format parameter. |
| 200 | * |
| 201 | * @param[in] arg Format parameter argument (for example xml, json, ...). |
| 202 | * @param[out] yo yanglint options used to update. |
| 203 | * @return 0 on success. |
| 204 | */ |
| 205 | int yo_opt_update_data_in_format(const char *arg, struct yl_opt *yo); |
| 206 | |
| 207 | /** |
aPiecek | 7f22c10 | 2023-06-09 09:48:44 +0200 | [diff] [blame] | 208 | * @brief Update @p yo according to the --make-implemented parameter. |
| 209 | * |
| 210 | * @param[in,out] yo yanglint options used to update. |
| 211 | */ |
| 212 | void yo_opt_update_make_implemented(struct yl_opt *yo); |
| 213 | |
| 214 | /** |
aPiecek | 88ae15e | 2023-06-09 10:20:17 +0200 | [diff] [blame] | 215 | * @brief Update @p yo according to the --disable-searchdir parameter. |
| 216 | * |
| 217 | * @param[in,out] yo yanglint options used to update. |
| 218 | */ |
| 219 | void yo_opt_update_disable_searchdir(struct yl_opt *yo); |
| 220 | |
| 221 | /** |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 222 | * @brief Helper function to prepare argc, argv pair from a command line string. |
| 223 | * |
| 224 | * @param[in] cmdline Complete command line string. |
| 225 | * @param[out] argc_p Pointer to store argc value. |
| 226 | * @param[out] argv_p Pointer to store argv vector. |
| 227 | * @return 0 on success, non-zero on failure. |
| 228 | */ |
| 229 | int parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[]); |
| 230 | |
| 231 | /** |
| 232 | * @brief Destructor for the argument vector prepared by ::parse_cmdline(). |
| 233 | * |
| 234 | * @param[in,out] argv Argument vector to destroy. |
| 235 | */ |
| 236 | void free_cmdline(char *argv[]); |
| 237 | |
| 238 | #endif /* YL_OPT_H_ */ |