blob: 9f107110abe9aa7ab8314dcbdf57e6a9542f7a85 [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"
aPiecekd8f002f2023-06-21 10:40:41 +020031#include "yl_schema_features.h"
Radek Krejcie9f13b12020-11-09 17:42:04 +010032
33void
34cmd_add_help(void)
35{
36 printf("Usage: add [-iD] <schema1> [<schema2> ...]\n"
37 " Add a new module from a specific file.\n\n"
38 " -D, --disable-searchdir\n"
39 " Do not implicitly search in current working directory for\n"
40 " the import schema modules. If specified a second time, do not\n"
41 " even search in the module directory (all modules must be \n"
42 " explicitly specified).\n"
43 " -F FEATURES, --features=FEATURES\n"
Michal Vasko703370c2021-10-19 14:07:16 +020044 " Features to support, default all in all implemented modules.\n"
roman300b8782022-08-11 12:49:21 +020045 " Specify separately for each module.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010046 " <modname>:[<feature>,]*\n"
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020047 " -i, --make-implemented\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010048 " Make the imported modules \"referenced\" from any loaded\n"
49 " <schema> module also implemented. If specified a second time,\n"
aPiecek88030e52023-06-02 11:03:57 +020050 " all the modules are set implemented.\n"
51 " -X, --extended-leafref\n"
52 " Allow usage of deref() XPath function within leafref.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010053}
54
aPieceka83b8e02023-06-07 15:25:16 +020055int
56cmd_add_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
Radek Krejcie9f13b12020-11-09 17:42:04 +010057{
aPieceka83b8e02023-06-07 15:25:16 +020058 int rc = 0, argc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +010059 int opt, opt_index;
60 struct option options[] = {
61 {"disable-searchdir", no_argument, NULL, 'D'},
62 {"features", required_argument, NULL, 'F'},
63 {"help", no_argument, NULL, 'h'},
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020064 {"make-implemented", no_argument, NULL, 'i'},
aPiecek88030e52023-06-02 11:03:57 +020065 {"extended-leafref", no_argument, NULL, 'X'},
Radek Krejcie9f13b12020-11-09 17:42:04 +010066 {NULL, 0, NULL, 0}
67 };
Radek Krejcie9f13b12020-11-09 17:42:04 +010068
aPieceka83b8e02023-06-07 15:25:16 +020069 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
70 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +010071 }
72
aPieceka83b8e02023-06-07 15:25:16 +020073 while ((opt = getopt_long(argc, yo->argv, commands[CMD_ADD].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010074 switch (opt) {
75 case 'D': /* --disable--search */
aPieceka83b8e02023-06-07 15:25:16 +020076 if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020077 YLMSG_W("The -D option specified too many times.");
Radek Krejcie9f13b12020-11-09 17:42:04 +010078 }
aPiecek88ae15e2023-06-09 10:20:17 +020079 yo_opt_update_disable_searchdir(yo);
Radek Krejcie9f13b12020-11-09 17:42:04 +010080 break;
81
82 case 'F': /* --features */
aPieceka83b8e02023-06-07 15:25:16 +020083 if (parse_features(optarg, &yo->schema_features)) {
84 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010085 }
86 break;
87
88 case 'h':
89 cmd_add_help();
aPieceka83b8e02023-06-07 15:25:16 +020090 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010091
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020092 case 'i': /* --make-implemented */
aPiecek7f22c102023-06-09 09:48:44 +020093 yo_opt_update_make_implemented(yo);
Radek Krejcie9f13b12020-11-09 17:42:04 +010094 break;
95
aPiecek88030e52023-06-02 11:03:57 +020096 case 'X': /* --extended-leafref */
aPieceka83b8e02023-06-07 15:25:16 +020097 yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
aPiecek88030e52023-06-02 11:03:57 +020098 break;
99
Radek Krejcie9f13b12020-11-09 17:42:04 +0100100 default:
Michal Vasko1407d7d2023-08-21 09:57:54 +0200101 YLMSG_E("Unknown option.");
aPieceka83b8e02023-06-07 15:25:16 +0200102 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100103 }
104 }
105
aPieceka83b8e02023-06-07 15:25:16 +0200106 *posv = &yo->argv[optind];
107 *posc = argc - optind;
108
109 return 0;
110}
111
112int
113cmd_add_dep(struct yl_opt *yo, int posc)
114{
115 if (yo->interactive && !posc) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100116 /* no argument */
117 cmd_add_help();
aPieceka83b8e02023-06-07 15:25:16 +0200118 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100119 }
aPieceka83b8e02023-06-07 15:25:16 +0200120 if (!yo->schema_features.count) {
Michal Vasko703370c2021-10-19 14:07:16 +0200121 /* no features, enable all of them */
aPieceka83b8e02023-06-07 15:25:16 +0200122 yo->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES;
Michal Vasko703370c2021-10-19 14:07:16 +0200123 }
124
aPieceka83b8e02023-06-07 15:25:16 +0200125 return 0;
126}
127
aPiecek2457b5e2023-06-12 11:40:14 +0200128static int
129store_parsed_module(const char *filepath, struct lys_module *mod, struct yl_opt *yo)
130{
131 assert(!yo->interactive);
132
133 if (yo->schema_out_format || yo->feature_param_format) {
134 if (ly_set_add(&yo->schema_modules, (void *)mod, 1, NULL)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200135 YLMSG_E("Storing parsed schema module (%s) for print failed.", filepath);
aPiecek2457b5e2023-06-12 11:40:14 +0200136 return 1;
137 }
138 }
139
140 return 0;
141}
142
aPieceka83b8e02023-06-07 15:25:16 +0200143int
144cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
145{
146 const char *all_features[] = {"*", NULL};
147 LY_ERR ret;
148 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
aPiecek2457b5e2023-06-12 11:40:14 +0200149 char *dir, *modname = NULL;
aPieceka83b8e02023-06-07 15:25:16 +0200150 const char **features = NULL;
151 struct ly_in *in = NULL;
aPiecek2457b5e2023-06-12 11:40:14 +0200152 struct lys_module *mod;
aPieceka83b8e02023-06-07 15:25:16 +0200153
154 assert(posv);
155
156 if (yo->ctx_options) {
157 ly_ctx_set_options(*ctx, yo->ctx_options);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100158 }
159
aPiecek2457b5e2023-06-12 11:40:14 +0200160 if (parse_schema_path(posv, &dir, &modname)) {
aPieceka83b8e02023-06-07 15:25:16 +0200161 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100162 }
163
aPiecek2457b5e2023-06-12 11:40:14 +0200164 if (!yo->interactive) {
165 /* The module should already be parsed if yang_lib_file was set. */
166 if (yo->yang_lib_file && (mod = ly_ctx_get_module_implemented(*ctx, modname))) {
167 ret = store_parsed_module(posv, mod, yo);
168 goto cleanup;
169 }
170 /* parse module */
171 }
172
aPieceka83b8e02023-06-07 15:25:16 +0200173 /* add temporarily also the path of the module itself */
174 if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) {
175 path_unset = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100176 }
aPieceka83b8e02023-06-07 15:25:16 +0200177
178 /* get features list for this module */
179 if (!yo->schema_features.count) {
180 features = all_features;
181 } else {
aPiecek2457b5e2023-06-12 11:40:14 +0200182 get_features(&yo->schema_features, modname, &features);
aPieceka83b8e02023-06-07 15:25:16 +0200183 }
184
aPieceka83b8e02023-06-07 15:25:16 +0200185 /* prepare input handler */
186 ret = ly_in_new_filepath(posv, 0, &in);
187 if (ret) {
aPiecek2457b5e2023-06-12 11:40:14 +0200188 goto cleanup;
aPieceka83b8e02023-06-07 15:25:16 +0200189 }
190
191 /* parse the file */
aPiecek2457b5e2023-06-12 11:40:14 +0200192 ret = lys_parse(*ctx, in, LYS_IN_UNKNOWN, features, &mod);
aPieceka83b8e02023-06-07 15:25:16 +0200193 ly_in_free(in, 1);
194 ly_ctx_unset_searchdir_last(*ctx, path_unset);
aPiecek2457b5e2023-06-12 11:40:14 +0200195 if (ret) {
196 goto cleanup;
197 }
198
199 if (!yo->interactive) {
200 if ((ret = store_parsed_module(posv, mod, yo))) {
201 goto cleanup;
202 }
203 }
204
205cleanup:
206 free(dir);
207 free(modname);
aPieceka83b8e02023-06-07 15:25:16 +0200208
209 return ret;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100210}