blob: 3b2acd426ecd199bb7b0106e071485ad5fe32744 [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file cmd_add.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @author Radek Krejci <rkrejci@cesnet.cz>
aPieceka83b8e02023-06-07 15:25:16 +02005 * @author Adam Piecek <piecek@cesnet.cz>
Radek Krejcie9f13b12020-11-09 17:42:04 +01006 * @brief 'add' command of the libyang's yanglint tool.
7 *
aPieceka83b8e02023-06-07 15:25:16 +02008 * Copyright (c) 2015-2023 CESNET, z.s.p.o.
Radek Krejcie9f13b12020-11-09 17:42:04 +01009 *
10 * This source code is licensed under BSD 3-Clause License (the "License").
11 * You may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * https://opensource.org/licenses/BSD-3-Clause
15 */
16
17#define _GNU_SOURCE
18
19#include "cmd.h"
20
aPieceka83b8e02023-06-07 15:25:16 +020021#include <assert.h>
Radek Krejcie9f13b12020-11-09 17:42:04 +010022#include <getopt.h>
Radek Krejcie9f13b12020-11-09 17:42:04 +010023#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26
27#include "libyang.h"
28
29#include "common.h"
aPieceka83b8e02023-06-07 15:25:16 +020030#include "yl_opt.h"
Radek Krejcie9f13b12020-11-09 17:42:04 +010031
32void
33cmd_add_help(void)
34{
35 printf("Usage: add [-iD] <schema1> [<schema2> ...]\n"
36 " Add a new module from a specific file.\n\n"
37 " -D, --disable-searchdir\n"
38 " Do not implicitly search in current working directory for\n"
39 " the import schema modules. If specified a second time, do not\n"
40 " even search in the module directory (all modules must be \n"
41 " explicitly specified).\n"
42 " -F FEATURES, --features=FEATURES\n"
Michal Vasko703370c2021-10-19 14:07:16 +020043 " Features to support, default all in all implemented modules.\n"
roman300b8782022-08-11 12:49:21 +020044 " Specify separately for each module.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010045 " <modname>:[<feature>,]*\n"
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020046 " -i, --make-implemented\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010047 " Make the imported modules \"referenced\" from any loaded\n"
48 " <schema> module also implemented. If specified a second time,\n"
aPiecek88030e52023-06-02 11:03:57 +020049 " all the modules are set implemented.\n"
50 " -X, --extended-leafref\n"
51 " Allow usage of deref() XPath function within leafref.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010052}
53
aPieceka83b8e02023-06-07 15:25:16 +020054int
55cmd_add_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
Radek Krejcie9f13b12020-11-09 17:42:04 +010056{
aPieceka83b8e02023-06-07 15:25:16 +020057 int rc = 0, argc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +010058 int opt, opt_index;
59 struct option options[] = {
60 {"disable-searchdir", no_argument, NULL, 'D'},
61 {"features", required_argument, NULL, 'F'},
62 {"help", no_argument, NULL, 'h'},
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020063 {"make-implemented", no_argument, NULL, 'i'},
aPiecek88030e52023-06-02 11:03:57 +020064 {"extended-leafref", no_argument, NULL, 'X'},
Radek Krejcie9f13b12020-11-09 17:42:04 +010065 {NULL, 0, NULL, 0}
66 };
Radek Krejcie9f13b12020-11-09 17:42:04 +010067
aPieceka83b8e02023-06-07 15:25:16 +020068 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
69 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +010070 }
71
aPieceka83b8e02023-06-07 15:25:16 +020072 while ((opt = getopt_long(argc, yo->argv, commands[CMD_ADD].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010073 switch (opt) {
74 case 'D': /* --disable--search */
aPieceka83b8e02023-06-07 15:25:16 +020075 if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010076 YLMSG_W("The -D option specified too many times.\n");
77 }
aPiecek88ae15e2023-06-09 10:20:17 +020078 yo_opt_update_disable_searchdir(yo);
Radek Krejcie9f13b12020-11-09 17:42:04 +010079 break;
80
81 case 'F': /* --features */
aPieceka83b8e02023-06-07 15:25:16 +020082 if (parse_features(optarg, &yo->schema_features)) {
83 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010084 }
85 break;
86
87 case 'h':
88 cmd_add_help();
aPieceka83b8e02023-06-07 15:25:16 +020089 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010090
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020091 case 'i': /* --make-implemented */
aPiecek7f22c102023-06-09 09:48:44 +020092 yo_opt_update_make_implemented(yo);
Radek Krejcie9f13b12020-11-09 17:42:04 +010093 break;
94
aPiecek88030e52023-06-02 11:03:57 +020095 case 'X': /* --extended-leafref */
aPieceka83b8e02023-06-07 15:25:16 +020096 yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
aPiecek88030e52023-06-02 11:03:57 +020097 break;
98
Radek Krejcie9f13b12020-11-09 17:42:04 +010099 default:
100 YLMSG_E("Unknown option.\n");
aPieceka83b8e02023-06-07 15:25:16 +0200101 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100102 }
103 }
104
aPieceka83b8e02023-06-07 15:25:16 +0200105 *posv = &yo->argv[optind];
106 *posc = argc - optind;
107
108 return 0;
109}
110
111int
112cmd_add_dep(struct yl_opt *yo, int posc)
113{
114 if (yo->interactive && !posc) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100115 /* no argument */
116 cmd_add_help();
aPieceka83b8e02023-06-07 15:25:16 +0200117 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100118 }
aPieceka83b8e02023-06-07 15:25:16 +0200119 if (!yo->schema_features.count) {
Michal Vasko703370c2021-10-19 14:07:16 +0200120 /* no features, enable all of them */
aPieceka83b8e02023-06-07 15:25:16 +0200121 yo->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES;
Michal Vasko703370c2021-10-19 14:07:16 +0200122 }
123
aPieceka83b8e02023-06-07 15:25:16 +0200124 return 0;
125}
126
aPiecek2457b5e2023-06-12 11:40:14 +0200127static int
128store_parsed_module(const char *filepath, struct lys_module *mod, struct yl_opt *yo)
129{
130 assert(!yo->interactive);
131
132 if (yo->schema_out_format || yo->feature_param_format) {
133 if (ly_set_add(&yo->schema_modules, (void *)mod, 1, NULL)) {
134 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", filepath);
135 return 1;
136 }
137 }
138
139 return 0;
140}
141
aPieceka83b8e02023-06-07 15:25:16 +0200142int
143cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
144{
145 const char *all_features[] = {"*", NULL};
146 LY_ERR ret;
147 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
aPiecek2457b5e2023-06-12 11:40:14 +0200148 char *dir, *modname = NULL;
aPieceka83b8e02023-06-07 15:25:16 +0200149 const char **features = NULL;
150 struct ly_in *in = NULL;
aPiecek2457b5e2023-06-12 11:40:14 +0200151 struct lys_module *mod;
aPieceka83b8e02023-06-07 15:25:16 +0200152
153 assert(posv);
154
155 if (yo->ctx_options) {
156 ly_ctx_set_options(*ctx, yo->ctx_options);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100157 }
158
aPiecek2457b5e2023-06-12 11:40:14 +0200159 if (parse_schema_path(posv, &dir, &modname)) {
aPieceka83b8e02023-06-07 15:25:16 +0200160 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100161 }
162
aPiecek2457b5e2023-06-12 11:40:14 +0200163 if (!yo->interactive) {
164 /* The module should already be parsed if yang_lib_file was set. */
165 if (yo->yang_lib_file && (mod = ly_ctx_get_module_implemented(*ctx, modname))) {
166 ret = store_parsed_module(posv, mod, yo);
167 goto cleanup;
168 }
169 /* parse module */
170 }
171
aPieceka83b8e02023-06-07 15:25:16 +0200172 /* add temporarily also the path of the module itself */
173 if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) {
174 path_unset = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100175 }
aPieceka83b8e02023-06-07 15:25:16 +0200176
177 /* get features list for this module */
178 if (!yo->schema_features.count) {
179 features = all_features;
180 } else {
aPiecek2457b5e2023-06-12 11:40:14 +0200181 get_features(&yo->schema_features, modname, &features);
aPieceka83b8e02023-06-07 15:25:16 +0200182 }
183
aPieceka83b8e02023-06-07 15:25:16 +0200184 /* prepare input handler */
185 ret = ly_in_new_filepath(posv, 0, &in);
186 if (ret) {
aPiecek2457b5e2023-06-12 11:40:14 +0200187 goto cleanup;
aPieceka83b8e02023-06-07 15:25:16 +0200188 }
189
190 /* parse the file */
aPiecek2457b5e2023-06-12 11:40:14 +0200191 ret = lys_parse(*ctx, in, LYS_IN_UNKNOWN, features, &mod);
aPieceka83b8e02023-06-07 15:25:16 +0200192 ly_in_free(in, 1);
193 ly_ctx_unset_searchdir_last(*ctx, path_unset);
aPiecek2457b5e2023-06-12 11:40:14 +0200194 if (ret) {
195 goto cleanup;
196 }
197
198 if (!yo->interactive) {
199 if ((ret = store_parsed_module(posv, mod, yo))) {
200 goto cleanup;
201 }
202 }
203
204cleanup:
205 free(dir);
206 free(modname);
aPieceka83b8e02023-06-07 15:25:16 +0200207
208 return ret;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100209}