blob: 1ae2c1ec2b242521b4c9e2f6d524792684406ada [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file cmd_load.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @author Radek Krejci <rkrejci@cesnet.cz>
5 * @brief 'load' 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#include <string.h>
24
25#include "libyang.h"
26
27#include "common.h"
28
29void
30cmd_load_help(void)
31{
32 printf("Usage: load [-i] <module-name1>[@<revision>] [<module-name2>[@revision] ...]\n"
33 " Add a new module of the specified name, yanglint will find\n"
34 " them in searchpaths. if the <revision> of the module not\n"
35 " specified, the latest revision available is loaded.\n\n"
36 " -F FEATURES, --features=FEATURES\n"
37 " Features to support, default all.\n"
38 " <modname>:[<feature>,]*\n"
39 " -i, --makeimplemented\n"
40 " Make the imported modules \"referenced\" from any loaded\n"
41 " <schema> module also implemented. If specified a second time,\n"
42 " all the modules are set implemented.\n");
43}
44
45void
46cmd_load(struct ly_ctx **ctx, const char *cmdline)
47{
48 int argc = 0;
49 char **argv = NULL;
50 int opt, opt_index;
51 struct option options[] = {
52 {"features", required_argument, NULL, 'F'},
53 {"help", no_argument, NULL, 'h'},
54 {"makeimplemented", no_argument, NULL, 'i'},
55 {NULL, 0, NULL, 0}
56 };
57 uint16_t options_ctx = 0;
58 struct ly_set fset = {0};
59
60 if (parse_cmdline(cmdline, &argc, &argv)) {
61 goto cleanup;
62 }
63
64 while ((opt = getopt_long(argc, argv, "F:hi", options, &opt_index)) != -1) {
65 switch (opt) {
66 case 'F': /* --features */
67 if (parse_features(optarg, &fset)) {
68 goto cleanup;
69 }
70 break;
71
72 case 'h':
73 cmd_load_help();
74 goto cleanup;
75
76 case 'i': /* --makeimplemented */
77 if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
78 options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
79 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
80 } else {
81 options_ctx |= LY_CTX_REF_IMPLEMENTED;
82 }
83 break;
84
85 default:
86 YLMSG_E("Unknown option.\n");
87 goto cleanup;
88 }
89 }
90
91 if (argc == optind) {
92 /* no argument */
93 cmd_add_help();
94 goto cleanup;
95 }
96
97 if (options_ctx) {
98 ly_ctx_set_options(*ctx, options_ctx);
99 }
100
101 for (int i = 0; i < argc - optind; i++) {
102 /* process the schema module files */
103 char *revision;
104 const char **features = NULL;
105
106 /* get revision */
107 revision = strchr(argv[optind + i], '@');
108 if (revision) {
109 revision[0] = '\0';
110 ++revision;
111 }
112
113 /* get features list for this module */
114 get_features(&fset, argv[optind + i], &features);
115
116 /* load the module */
117 if (!ly_ctx_load_module(*ctx, argv[optind + i], revision, features)) {
118 /* libyang printed the error messages */
119 goto cleanup;
120 }
121 }
122
123cleanup:
124 if (options_ctx) {
125 ly_ctx_unset_options(*ctx, options_ctx);
126 }
127 ly_set_erase(&fset, free_features);
128 free_cmdline(argv);
129}