blob: 744ea80839bd9241fcca5b6017032ed7e93c8a81 [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 */
aPiecek7f22c102023-06-09 09:48:44 +020097 yo_opt_update_make_implemented(yo);
Radek Krejcie9f13b12020-11-09 17:42:04 +010098 break;
99
aPiecek88030e52023-06-02 11:03:57 +0200100 case 'X': /* --extended-leafref */
aPieceka83b8e02023-06-07 15:25:16 +0200101 yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
aPiecek88030e52023-06-02 11:03:57 +0200102 break;
103
Radek Krejcie9f13b12020-11-09 17:42:04 +0100104 default:
105 YLMSG_E("Unknown option.\n");
aPieceka83b8e02023-06-07 15:25:16 +0200106 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100107 }
108 }
109
aPieceka83b8e02023-06-07 15:25:16 +0200110 *posv = &yo->argv[optind];
111 *posc = argc - optind;
112
113 return 0;
114}
115
116int
117cmd_add_dep(struct yl_opt *yo, int posc)
118{
119 if (yo->interactive && !posc) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100120 /* no argument */
121 cmd_add_help();
aPieceka83b8e02023-06-07 15:25:16 +0200122 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100123 }
aPieceka83b8e02023-06-07 15:25:16 +0200124 if (!yo->schema_features.count) {
Michal Vasko703370c2021-10-19 14:07:16 +0200125 /* no features, enable all of them */
aPieceka83b8e02023-06-07 15:25:16 +0200126 yo->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES;
Michal Vasko703370c2021-10-19 14:07:16 +0200127 }
128
aPieceka83b8e02023-06-07 15:25:16 +0200129 return 0;
130}
131
132int
133cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
134{
135 const char *all_features[] = {"*", NULL};
136 LY_ERR ret;
137 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
138 char *dir, *module;
139 const char **features = NULL;
140 struct ly_in *in = NULL;
141
142 assert(posv);
143
144 if (yo->ctx_options) {
145 ly_ctx_set_options(*ctx, yo->ctx_options);
146 yo->ctx_options = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100147 }
148
aPieceka83b8e02023-06-07 15:25:16 +0200149 if (parse_schema_path(posv, &dir, &module)) {
150 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100151 }
152
aPieceka83b8e02023-06-07 15:25:16 +0200153 /* add temporarily also the path of the module itself */
154 if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) {
155 path_unset = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100156 }
aPieceka83b8e02023-06-07 15:25:16 +0200157
158 /* get features list for this module */
159 if (!yo->schema_features.count) {
160 features = all_features;
161 } else {
162 get_features(&yo->schema_features, module, &features);
163 }
164
165 /* temporary cleanup */
166 free(dir);
167 free(module);
168
169 /* prepare input handler */
170 ret = ly_in_new_filepath(posv, 0, &in);
171 if (ret) {
172 return 1;
173 }
174
175 /* parse the file */
176 ret = lys_parse(*ctx, in, LYS_IN_UNKNOWN, features, NULL);
177 ly_in_free(in, 1);
178 ly_ctx_unset_searchdir_last(*ctx, path_unset);
179
180 return ret;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100181}