blob: 97ef94c7e1c99f659e0fca49b814ce938b309fca [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
127int
128cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
129{
130 const char *all_features[] = {"*", NULL};
131 LY_ERR ret;
132 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
133 char *dir, *module;
134 const char **features = NULL;
135 struct ly_in *in = NULL;
136
137 assert(posv);
138
139 if (yo->ctx_options) {
140 ly_ctx_set_options(*ctx, yo->ctx_options);
141 yo->ctx_options = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100142 }
143
aPieceka83b8e02023-06-07 15:25:16 +0200144 if (parse_schema_path(posv, &dir, &module)) {
145 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100146 }
147
aPieceka83b8e02023-06-07 15:25:16 +0200148 /* add temporarily also the path of the module itself */
149 if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) {
150 path_unset = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100151 }
aPieceka83b8e02023-06-07 15:25:16 +0200152
153 /* get features list for this module */
154 if (!yo->schema_features.count) {
155 features = all_features;
156 } else {
157 get_features(&yo->schema_features, module, &features);
158 }
159
160 /* temporary cleanup */
161 free(dir);
162 free(module);
163
164 /* prepare input handler */
165 ret = ly_in_new_filepath(posv, 0, &in);
166 if (ret) {
167 return 1;
168 }
169
170 /* parse the file */
171 ret = lys_parse(*ctx, in, LYS_IN_UNKNOWN, features, NULL);
172 ly_in_free(in, 1);
173 ly_ctx_unset_searchdir_last(*ctx, path_unset);
174
175 return ret;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100176}