Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_clear.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 5 | * @author Adam Piecek <piecek@cesnet.cz> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 6 | * @brief 'clear' command of the libyang's yanglint tool. |
| 7 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 8 | * Copyright (c) 2015-2023 CESNET, z.s.p.o. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 9 | * |
| 10 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 11 | * You may not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * https://opensource.org/licenses/BSD-3-Clause |
| 15 | */ |
| 16 | |
| 17 | #define _GNU_SOURCE |
| 18 | |
| 19 | #include "cmd.h" |
| 20 | |
| 21 | #include <getopt.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | |
| 25 | #include "libyang.h" |
| 26 | |
| 27 | #include "common.h" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 28 | #include "yl_opt.h" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 29 | |
| 30 | void |
| 31 | cmd_clear_help(void) |
| 32 | { |
Michal Vasko | 8b28060 | 2021-10-05 13:23:48 +0200 | [diff] [blame] | 33 | printf("Usage: clear [-i] [-y]\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 34 | " Replace the current context with an empty one, searchpaths\n" |
Michal Vasko | 8b28060 | 2021-10-05 13:23:48 +0200 | [diff] [blame] | 35 | " are not kept.\n\n" |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 36 | " -i, --make-implemented\n" |
aPiecek | 6c1e57e | 2023-03-30 14:43:34 +0200 | [diff] [blame] | 37 | " When loading a module into the context, the imported 'referenced'\n" |
| 38 | " modules will also be implemented. If specified a second time,\n" |
| 39 | " all the modules will be implemented.\n" |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 40 | " -y, --yang-library\n" |
| 41 | " Load and implement internal \"ietf-yang-library\" YANG module.\n" |
| 42 | " Note that this module includes definitions of mandatory state\n" |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 43 | " data that can result in unexpected data validation errors.\n" |
| 44 | " -Y FILE, --yang-library-file=FILE\n" |
| 45 | " Parse FILE with \"ietf-yang-library\" data and use them to\n" |
| 46 | " create an exact YANG schema context. Searchpaths defined so far\n" |
| 47 | " are used, but then deleted.\n"); |
| 48 | } |
| 49 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 50 | int |
| 51 | cmd_clear_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
| 52 | { |
| 53 | int rc = 0, argc = 0; |
| 54 | int opt, opt_index; |
| 55 | struct option options[] = { |
| 56 | {"make-implemented", no_argument, NULL, 'i'}, |
| 57 | {"yang-library", no_argument, NULL, 'y'}, |
| 58 | {"yang-library-file", no_argument, NULL, 'Y'}, |
| 59 | {"help", no_argument, NULL, 'h'}, |
| 60 | {NULL, 0, NULL, 0} |
| 61 | }; |
| 62 | |
| 63 | yo->ctx_options = LY_CTX_NO_YANGLIBRARY; |
| 64 | |
| 65 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 66 | return rc; |
| 67 | } |
| 68 | |
| 69 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_CLEAR].optstring, options, &opt_index)) != -1) { |
| 70 | switch (opt) { |
| 71 | case 'i': |
| 72 | if (yo->ctx_options & LY_CTX_REF_IMPLEMENTED) { |
| 73 | yo->ctx_options &= ~LY_CTX_REF_IMPLEMENTED; |
| 74 | yo->ctx_options |= LY_CTX_ALL_IMPLEMENTED; |
| 75 | } else { |
| 76 | yo->ctx_options |= LY_CTX_REF_IMPLEMENTED; |
| 77 | } |
| 78 | break; |
| 79 | case 'y': |
| 80 | yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY; |
| 81 | break; |
| 82 | case 'Y': |
| 83 | yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY; |
| 84 | yo->yang_lib_file = optarg; |
| 85 | if (!yo->yang_lib_file) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 86 | YLMSG_E("Memory allocation error."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 87 | return 1; |
| 88 | } |
| 89 | break; |
| 90 | case 'h': |
| 91 | cmd_clear_help(); |
| 92 | return 1; |
| 93 | default: |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 94 | YLMSG_E("Unknown option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | *posv = &yo->argv[optind]; |
| 100 | *posc = argc - optind; |
| 101 | |
| 102 | return rc; |
| 103 | } |
| 104 | |
| 105 | int |
| 106 | cmd_clear_dep(struct yl_opt *yo, int posc) |
| 107 | { |
| 108 | (void) yo; |
| 109 | |
| 110 | if (posc) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 111 | YLMSG_E("No positional arguments are allowed."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 118 | /** |
| 119 | * @brief Convert searchpaths into single string. |
| 120 | * |
| 121 | * @param[in] ctx Context with searchpaths. |
| 122 | * @param[out] searchpaths Collection of paths in the single string. Paths are delimited by colon ":" |
| 123 | * (on Windows, used semicolon ";" instead). |
| 124 | * @return LY_ERR value. |
| 125 | */ |
| 126 | static LY_ERR |
| 127 | searchpaths_to_str(const struct ly_ctx *ctx, char **searchpaths) |
| 128 | { |
| 129 | uint32_t i; |
| 130 | int rc = 0; |
| 131 | const char * const *dirs = ly_ctx_get_searchdirs(ctx); |
| 132 | |
| 133 | for (i = 0; dirs[i]; ++i) { |
| 134 | rc = searchpath_strcat(searchpaths, dirs[i]); |
| 135 | if (!rc) { |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 141 | } |
| 142 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 143 | int |
| 144 | cmd_clear_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 145 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 146 | (void) posv; |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 147 | struct ly_ctx *ctx_new = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 148 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 149 | if (yo->yang_lib_file) { |
| 150 | if (searchpaths_to_str(*ctx, &yo->searchpaths)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 151 | YLMSG_E("Storing searchpaths failed."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 152 | return 1; |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 153 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 154 | if (ly_ctx_new_ylpath(yo->searchpaths, yo->yang_lib_file, LYD_UNKNOWN, yo->ctx_options, &ctx_new)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 155 | YLMSG_E("Unable to create libyang context with yang-library data."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 156 | return 1; |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 157 | } |
| 158 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 159 | if (ly_ctx_new(NULL, yo->ctx_options, &ctx_new)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 160 | YLMSG_W("Failed to create context."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 161 | return 1; |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 162 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 163 | } |
| 164 | |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 165 | /* Global variables in commands are also deleted. */ |
| 166 | cmd_free(); |
| 167 | |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 168 | ly_ctx_destroy(*ctx); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 169 | *ctx = ctx_new; |
| 170 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 171 | return 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 172 | } |