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" |
| 41 | " data that can result in unexpected data validation errors.\n"); |
| 42 | #if 0 |
Radek Krejci | 00a3e8a | 2021-01-27 08:24:49 +0100 | [diff] [blame] | 43 | " If <yang-library-data> path specified, load the modules\n" |
| 44 | " according to the provided yang library data.\n" |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 45 | #endif |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
| 49 | cmd_clear(struct ly_ctx **ctx, const char *cmdline) |
| 50 | { |
| 51 | int argc = 0; |
| 52 | char **argv = NULL; |
| 53 | int opt, opt_index; |
| 54 | struct option options[] = { |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 55 | {"make-implemented", no_argument, NULL, 'i'}, |
| 56 | {"yang-library", no_argument, NULL, 'y'}, |
| 57 | {"help", no_argument, NULL, 'h'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 58 | {NULL, 0, NULL, 0} |
| 59 | }; |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 60 | uint16_t options_ctx = LY_CTX_NO_YANGLIBRARY; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 61 | struct ly_ctx *ctx_new; |
| 62 | |
| 63 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 64 | goto cleanup; |
| 65 | } |
| 66 | |
Michal Vasko | 8b28060 | 2021-10-05 13:23:48 +0200 | [diff] [blame] | 67 | while ((opt = getopt_long(argc, argv, "iyh", options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 68 | switch (opt) { |
Michal Vasko | 8b28060 | 2021-10-05 13:23:48 +0200 | [diff] [blame] | 69 | case 'i': |
| 70 | if (options_ctx & LY_CTX_REF_IMPLEMENTED) { |
| 71 | options_ctx &= ~LY_CTX_REF_IMPLEMENTED; |
| 72 | options_ctx |= LY_CTX_ALL_IMPLEMENTED; |
| 73 | } else { |
| 74 | options_ctx |= LY_CTX_REF_IMPLEMENTED; |
| 75 | } |
| 76 | break; |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 77 | case 'y': |
| 78 | options_ctx &= ~LY_CTX_NO_YANGLIBRARY; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 79 | break; |
| 80 | case 'h': |
| 81 | cmd_clear_help(); |
| 82 | goto cleanup; |
| 83 | default: |
| 84 | YLMSG_E("Unknown option.\n"); |
| 85 | goto cleanup; |
| 86 | } |
| 87 | } |
| 88 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 89 | if (ly_ctx_new(NULL, options_ctx, &ctx_new)) { |
| 90 | YLMSG_W("Failed to create context.\n"); |
| 91 | goto cleanup; |
| 92 | } |
| 93 | |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 94 | ly_ctx_destroy(*ctx); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 95 | *ctx = ctx_new; |
| 96 | |
| 97 | cleanup: |
| 98 | free_cmdline(argv); |
| 99 | } |