blob: 5cece3ad82f94a7b07e78bfd24d9f40a72b6f29c [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{
31 printf("Usage: clear [<yang-library-data> | --external]\n"
32 " Replace the current context with an empty one, searchpaths\n"
33 " are not kept.\n"
34 " If <yang-library-data> path specified, load the modules\n"
35 " according to the provided yang library data.\n"
36 " -e, --external-yl\n"
37 " When creating the new context, do not load the internal\n"
38 " ietf-yang-library and let user to load ietf-yang-library from\n"
39 " file. Note, that until a compatible ietf-yang-library loaded,\n"
40 " the 'list' command does not work.\n");
41}
42
43void
44cmd_clear(struct ly_ctx **ctx, const char *cmdline)
45{
46 int argc = 0;
47 char **argv = NULL;
48 int opt, opt_index;
49 struct option options[] = {
50 {"external-yl", no_argument, NULL, 'e'},
51 {"help", no_argument, NULL, 'h'},
52 {NULL, 0, NULL, 0}
53 };
54 uint16_t options_ctx = 0;
55 struct ly_ctx *ctx_new;
56
57 if (parse_cmdline(cmdline, &argc, &argv)) {
58 goto cleanup;
59 }
60
61 while ((opt = getopt_long(argc, argv, "eh", options, &opt_index)) != -1) {
62 switch (opt) {
63 case 'e':
64 options_ctx |= LY_CTX_NO_YANGLIBRARY;
65 break;
66 case 'h':
67 cmd_clear_help();
68 goto cleanup;
69 default:
70 YLMSG_E("Unknown option.\n");
71 goto cleanup;
72 }
73 }
74
75 /* TODO ietf-yang-library support */
76 if (argc != optind) {
77 YLMSG_E("Creating context following the ietf-yang-library data is not yet supported.\n");
78 goto cleanup;
79 }
80
81 if (ly_ctx_new(NULL, options_ctx, &ctx_new)) {
82 YLMSG_W("Failed to create context.\n");
83 goto cleanup;
84 }
85
86 ly_ctx_destroy(*ctx, NULL);
87 *ctx = ctx_new;
88
89cleanup:
90 free_cmdline(argv);
91}