blob: 488293eb5999874ca1b37fa7ed78519f2f5cd2f1 [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"
aPiecek41955272023-05-10 16:10:17 +020034 " them in searchpaths. If the <revision> of the module not\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010035 " specified, the latest revision available is loaded.\n\n"
36 " -F FEATURES, --features=FEATURES\n"
Michal Vasko703370c2021-10-19 14:07:16 +020037 " Features to support, default all in all implemented modules.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010038 " <modname>:[<feature>,]*\n"
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020039 " -i, --make-implemented\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010040 " 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'},
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020054 {"make-implemented", no_argument, NULL, 'i'},
Radek Krejcie9f13b12020-11-09 17:42:04 +010055 {NULL, 0, NULL, 0}
56 };
57 uint16_t options_ctx = 0;
Michal Vasko584b0c42022-09-14 10:20:52 +020058 const char *all_features[] = {"*", NULL};
Radek Krejcie9f13b12020-11-09 17:42:04 +010059 struct ly_set fset = {0};
60
61 if (parse_cmdline(cmdline, &argc, &argv)) {
62 goto cleanup;
63 }
64
aPiecek15cc4cf2023-04-17 15:23:21 +020065 while ((opt = getopt_long(argc, argv, commands[CMD_LOAD].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010066 switch (opt) {
67 case 'F': /* --features */
68 if (parse_features(optarg, &fset)) {
69 goto cleanup;
70 }
71 break;
72
73 case 'h':
74 cmd_load_help();
75 goto cleanup;
76
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020077 case 'i': /* --make-implemented */
Radek Krejcie9f13b12020-11-09 17:42:04 +010078 if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
79 options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
80 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
81 } else {
82 options_ctx |= LY_CTX_REF_IMPLEMENTED;
83 }
84 break;
85
86 default:
87 YLMSG_E("Unknown option.\n");
88 goto cleanup;
89 }
90 }
91
92 if (argc == optind) {
93 /* no argument */
94 cmd_add_help();
95 goto cleanup;
96 }
97
Michal Vasko703370c2021-10-19 14:07:16 +020098 if (!fset.count) {
99 /* no features, enable all of them */
100 options_ctx |= LY_CTX_ENABLE_IMP_FEATURES;
101 }
102
Radek Krejcie9f13b12020-11-09 17:42:04 +0100103 if (options_ctx) {
104 ly_ctx_set_options(*ctx, options_ctx);
105 }
106
107 for (int i = 0; i < argc - optind; i++) {
108 /* process the schema module files */
109 char *revision;
110 const char **features = NULL;
111
112 /* get revision */
113 revision = strchr(argv[optind + i], '@');
114 if (revision) {
115 revision[0] = '\0';
116 ++revision;
117 }
118
119 /* get features list for this module */
Michal Vasko584b0c42022-09-14 10:20:52 +0200120 if (!fset.count) {
121 features = all_features;
122 } else {
123 get_features(&fset, argv[optind + i], &features);
124 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100125
126 /* load the module */
127 if (!ly_ctx_load_module(*ctx, argv[optind + i], revision, features)) {
128 /* libyang printed the error messages */
129 goto cleanup;
130 }
131 }
132
133cleanup:
134 if (options_ctx) {
135 ly_ctx_unset_options(*ctx, options_ctx);
136 }
137 ly_set_erase(&fset, free_features);
138 free_cmdline(argv);
139}