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> |
| 5 | * @brief 'clear' command of the libyang's yanglint tool. |
| 6 | * |
| 7 | * Copyright (c) 2015-2020 CESNET, z.s.p.o. |
| 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 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include "cmd.h" |
| 19 | |
| 20 | #include <getopt.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include "libyang.h" |
| 25 | |
| 26 | #include "common.h" |
| 27 | |
| 28 | void |
| 29 | cmd_clear_help(void) |
| 30 | { |
Michal Vasko | 8b28060 | 2021-10-05 13:23:48 +0200 | [diff] [blame] | 31 | printf("Usage: clear [-i] [-y]\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 32 | " Replace the current context with an empty one, searchpaths\n" |
Michal Vasko | 8b28060 | 2021-10-05 13:23:48 +0200 | [diff] [blame] | 33 | " are not kept.\n\n" |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 34 | " -i, --make-implemented\n" |
aPiecek | 6c1e57e | 2023-03-30 14:43:34 +0200 | [diff] [blame] | 35 | " When loading a module into the context, the imported 'referenced'\n" |
| 36 | " modules will also be implemented. If specified a second time,\n" |
| 37 | " all the modules will be implemented.\n" |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 38 | " -y, --yang-library\n" |
| 39 | " Load and implement internal \"ietf-yang-library\" YANG module.\n" |
| 40 | " Note that this module includes definitions of mandatory state\n" |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 41 | " data that can result in unexpected data validation errors.\n" |
| 42 | " -Y FILE, --yang-library-file=FILE\n" |
| 43 | " Parse FILE with \"ietf-yang-library\" data and use them to\n" |
| 44 | " create an exact YANG schema context. Searchpaths defined so far\n" |
| 45 | " are used, but then deleted.\n"); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @brief Convert searchpaths into single string. |
| 50 | * |
| 51 | * @param[in] ctx Context with searchpaths. |
| 52 | * @param[out] searchpaths Collection of paths in the single string. Paths are delimited by colon ":" |
| 53 | * (on Windows, used semicolon ";" instead). |
| 54 | * @return LY_ERR value. |
| 55 | */ |
| 56 | static LY_ERR |
| 57 | searchpaths_to_str(const struct ly_ctx *ctx, char **searchpaths) |
| 58 | { |
| 59 | uint32_t i; |
| 60 | int rc = 0; |
| 61 | const char * const *dirs = ly_ctx_get_searchdirs(ctx); |
| 62 | |
| 63 | for (i = 0; dirs[i]; ++i) { |
| 64 | rc = searchpath_strcat(searchpaths, dirs[i]); |
| 65 | if (!rc) { |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
| 74 | cmd_clear(struct ly_ctx **ctx, const char *cmdline) |
| 75 | { |
| 76 | int argc = 0; |
| 77 | char **argv = NULL; |
| 78 | int opt, opt_index; |
| 79 | struct option options[] = { |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 80 | {"make-implemented", no_argument, NULL, 'i'}, |
| 81 | {"yang-library", no_argument, NULL, 'y'}, |
| 82 | {"yang-library-file", no_argument, NULL, 'Y'}, |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 83 | {"help", no_argument, NULL, 'h'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 84 | {NULL, 0, NULL, 0} |
| 85 | }; |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 86 | uint16_t options_ctx = LY_CTX_NO_YANGLIBRARY; |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 87 | struct ly_ctx *ctx_new = NULL; |
| 88 | char *ylibfile = NULL; |
| 89 | char *searchpaths = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 90 | |
| 91 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 92 | goto cleanup; |
| 93 | } |
| 94 | |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 95 | while ((opt = getopt_long(argc, argv, commands[CMD_CLEAR].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 96 | switch (opt) { |
Michal Vasko | 8b28060 | 2021-10-05 13:23:48 +0200 | [diff] [blame] | 97 | case 'i': |
| 98 | if (options_ctx & LY_CTX_REF_IMPLEMENTED) { |
| 99 | options_ctx &= ~LY_CTX_REF_IMPLEMENTED; |
| 100 | options_ctx |= LY_CTX_ALL_IMPLEMENTED; |
| 101 | } else { |
| 102 | options_ctx |= LY_CTX_REF_IMPLEMENTED; |
| 103 | } |
| 104 | break; |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 105 | case 'y': |
| 106 | options_ctx &= ~LY_CTX_NO_YANGLIBRARY; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 107 | break; |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 108 | case 'Y': |
| 109 | options_ctx &= ~LY_CTX_NO_YANGLIBRARY; |
| 110 | ylibfile = optarg; |
| 111 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 112 | case 'h': |
| 113 | cmd_clear_help(); |
| 114 | goto cleanup; |
| 115 | default: |
| 116 | YLMSG_E("Unknown option.\n"); |
| 117 | goto cleanup; |
| 118 | } |
| 119 | } |
| 120 | |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 121 | if (ylibfile) { |
| 122 | /* Create context according to the provided yang-library data in a file but use already defined searchpaths. */ |
| 123 | if (searchpaths_to_str(*ctx, &searchpaths)) { |
| 124 | YLMSG_E("Storing searchpaths failed.\n"); |
| 125 | goto cleanup; |
| 126 | } |
| 127 | if (ly_ctx_new_ylpath(searchpaths, ylibfile, LYD_UNKNOWN, options_ctx, &ctx_new)) { |
| 128 | YLMSG_E("Unable to create libyang context with yang-library data.\n"); |
| 129 | goto cleanup; |
| 130 | } |
| 131 | } else { |
| 132 | if (ly_ctx_new(NULL, options_ctx, &ctx_new)) { |
| 133 | YLMSG_W("Failed to create context.\n"); |
| 134 | goto cleanup; |
| 135 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 136 | } |
| 137 | |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 138 | /* Global variables in commands are also deleted. */ |
| 139 | cmd_free(); |
| 140 | |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 141 | ly_ctx_destroy(*ctx); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 142 | *ctx = ctx_new; |
| 143 | |
| 144 | cleanup: |
| 145 | free_cmdline(argv); |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 146 | free(searchpaths); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 147 | } |