blob: 3904c7abb4b4d311b3f77b781c63b23d74fa402d [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 }
aPieceka83b8e02023-06-07 15:25:16 +020078 if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIR_CWD) {
79 yo->ctx_options &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
80 yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIRS;
Radek Krejcie9f13b12020-11-09 17:42:04 +010081 } else {
aPieceka83b8e02023-06-07 15:25:16 +020082 yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD;
Radek Krejcie9f13b12020-11-09 17:42:04 +010083 }
84 break;
85
86 case 'F': /* --features */
aPieceka83b8e02023-06-07 15:25:16 +020087 if (parse_features(optarg, &yo->schema_features)) {
88 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010089 }
90 break;
91
92 case 'h':
93 cmd_add_help();
aPieceka83b8e02023-06-07 15:25:16 +020094 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010095
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020096 case 'i': /* --make-implemented */
aPieceka83b8e02023-06-07 15:25:16 +020097 if (yo->ctx_options & LY_CTX_REF_IMPLEMENTED) {
98 yo->ctx_options &= ~LY_CTX_REF_IMPLEMENTED;
99 yo->ctx_options |= LY_CTX_ALL_IMPLEMENTED;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100100 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200101 yo->ctx_options |= LY_CTX_REF_IMPLEMENTED;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100102 }
103 break;
104
aPiecek88030e52023-06-02 11:03:57 +0200105 case 'X': /* --extended-leafref */
aPieceka83b8e02023-06-07 15:25:16 +0200106 yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
aPiecek88030e52023-06-02 11:03:57 +0200107 break;
108
Radek Krejcie9f13b12020-11-09 17:42:04 +0100109 default:
110 YLMSG_E("Unknown option.\n");
aPieceka83b8e02023-06-07 15:25:16 +0200111 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100112 }
113 }
114
aPieceka83b8e02023-06-07 15:25:16 +0200115 *posv = &yo->argv[optind];
116 *posc = argc - optind;
117
118 return 0;
119}
120
121int
122cmd_add_dep(struct yl_opt *yo, int posc)
123{
124 if (yo->interactive && !posc) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100125 /* no argument */
126 cmd_add_help();
aPieceka83b8e02023-06-07 15:25:16 +0200127 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100128 }
aPieceka83b8e02023-06-07 15:25:16 +0200129 if (!yo->schema_features.count) {
Michal Vasko703370c2021-10-19 14:07:16 +0200130 /* no features, enable all of them */
aPieceka83b8e02023-06-07 15:25:16 +0200131 yo->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES;
Michal Vasko703370c2021-10-19 14:07:16 +0200132 }
133
aPieceka83b8e02023-06-07 15:25:16 +0200134 return 0;
135}
136
137int
138cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
139{
140 const char *all_features[] = {"*", NULL};
141 LY_ERR ret;
142 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
143 char *dir, *module;
144 const char **features = NULL;
145 struct ly_in *in = NULL;
146
147 assert(posv);
148
149 if (yo->ctx_options) {
150 ly_ctx_set_options(*ctx, yo->ctx_options);
151 yo->ctx_options = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100152 }
153
aPieceka83b8e02023-06-07 15:25:16 +0200154 if (parse_schema_path(posv, &dir, &module)) {
155 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100156 }
157
aPieceka83b8e02023-06-07 15:25:16 +0200158 /* add temporarily also the path of the module itself */
159 if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) {
160 path_unset = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100161 }
aPieceka83b8e02023-06-07 15:25:16 +0200162
163 /* get features list for this module */
164 if (!yo->schema_features.count) {
165 features = all_features;
166 } else {
167 get_features(&yo->schema_features, module, &features);
168 }
169
170 /* temporary cleanup */
171 free(dir);
172 free(module);
173
174 /* prepare input handler */
175 ret = ly_in_new_filepath(posv, 0, &in);
176 if (ret) {
177 return 1;
178 }
179
180 /* parse the file */
181 ret = lys_parse(*ctx, in, LYS_IN_UNKNOWN, features, NULL);
182 ly_in_free(in, 1);
183 ly_ctx_unset_searchdir_last(*ctx, path_unset);
184
185 return ret;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100186}