blob: ccf6bdd77742e47b4f131f14fd0a436d84db5ac9 [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
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
28void
29cmd_clear_help(void)
30{
Michal Vaskoc431a0a2021-01-25 14:31:58 +010031 printf("Usage: clear [--yang-library]\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010032 " Replace the current context with an empty one, searchpaths\n"
33 " are not kept.\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +010034 " -y, --yang-library\n"
35 " Load and implement internal \"ietf-yang-library\" YANG module.\n"
36 " Note that this module includes definitions of mandatory state\n"
37 " data that can result in unexpected data validation errors.\n");
38#if 0
Radek Krejci00a3e8a2021-01-27 08:24:49 +010039 " If <yang-library-data> path specified, load the modules\n"
40 " according to the provided yang library data.\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +010041#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +010042}
43
44void
45cmd_clear(struct ly_ctx **ctx, const char *cmdline)
46{
47 int argc = 0;
48 char **argv = NULL;
49 int opt, opt_index;
50 struct option options[] = {
Michal Vaskoc431a0a2021-01-25 14:31:58 +010051 {"yang-library", no_argument, NULL, 'y'},
52 {"help", no_argument, NULL, 'h'},
Radek Krejcie9f13b12020-11-09 17:42:04 +010053 {NULL, 0, NULL, 0}
54 };
Michal Vaskoc431a0a2021-01-25 14:31:58 +010055 uint16_t options_ctx = LY_CTX_NO_YANGLIBRARY;
Radek Krejcie9f13b12020-11-09 17:42:04 +010056 struct ly_ctx *ctx_new;
57
58 if (parse_cmdline(cmdline, &argc, &argv)) {
59 goto cleanup;
60 }
61
Michal Vaskoc431a0a2021-01-25 14:31:58 +010062 while ((opt = getopt_long(argc, argv, "yh", options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010063 switch (opt) {
Michal Vaskoc431a0a2021-01-25 14:31:58 +010064 case 'y':
65 options_ctx &= ~LY_CTX_NO_YANGLIBRARY;
Radek Krejcie9f13b12020-11-09 17:42:04 +010066 break;
67 case 'h':
68 cmd_clear_help();
69 goto cleanup;
70 default:
71 YLMSG_E("Unknown option.\n");
72 goto cleanup;
73 }
74 }
75
Radek Krejcie9f13b12020-11-09 17:42:04 +010076 if (ly_ctx_new(NULL, options_ctx, &ctx_new)) {
77 YLMSG_W("Failed to create context.\n");
78 goto cleanup;
79 }
80
Radek Krejci90ed21e2021-04-12 14:47:46 +020081 ly_ctx_destroy(*ctx);
Radek Krejcie9f13b12020-11-09 17:42:04 +010082 *ctx = ctx_new;
83
84cleanup:
85 free_cmdline(argv);
86}