blob: bcd23d4fb8d1ac09e04fe6be8992740750c7e6e0 [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file cmd_load.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 'load' 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>
23#include <stdint.h>
24#include <stdio.h>
25#include <string.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_load_help(void)
34{
35 printf("Usage: load [-i] <module-name1>[@<revision>] [<module-name2>[@revision] ...]\n"
36 " Add a new module of the specified name, yanglint will find\n"
aPiecek41955272023-05-10 16:10:17 +020037 " them in searchpaths. If the <revision> of the module not\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010038 " specified, the latest revision available is loaded.\n\n"
39 " -F FEATURES, --features=FEATURES\n"
Michal Vasko703370c2021-10-19 14:07:16 +020040 " Features to support, default all in all implemented modules.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010041 " <modname>:[<feature>,]*\n"
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020042 " -i, --make-implemented\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010043 " Make the imported modules \"referenced\" from any loaded\n"
44 " <schema> module also implemented. If specified a second time,\n"
aPiecek88030e52023-06-02 11:03:57 +020045 " all the modules are set implemented.\n"
46 " -X, --extended-leafref\n"
47 " Allow usage of deref() XPath function within leafref.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010048}
49
aPieceka83b8e02023-06-07 15:25:16 +020050int
51cmd_load_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
Radek Krejcie9f13b12020-11-09 17:42:04 +010052{
aPieceka83b8e02023-06-07 15:25:16 +020053 int rc, argc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +010054 int opt, opt_index;
55 struct option options[] = {
56 {"features", required_argument, NULL, 'F'},
57 {"help", no_argument, NULL, 'h'},
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020058 {"make-implemented", no_argument, NULL, 'i'},
aPiecek88030e52023-06-02 11:03:57 +020059 {"extended-leafref", no_argument, NULL, 'X'},
Radek Krejcie9f13b12020-11-09 17:42:04 +010060 {NULL, 0, NULL, 0}
61 };
Radek Krejcie9f13b12020-11-09 17:42:04 +010062
aPieceka83b8e02023-06-07 15:25:16 +020063 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
64 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +010065 }
66
aPieceka83b8e02023-06-07 15:25:16 +020067 while ((opt = getopt_long(argc, yo->argv, commands[CMD_LOAD].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010068 switch (opt) {
69 case 'F': /* --features */
aPieceka83b8e02023-06-07 15:25:16 +020070 if (parse_features(optarg, &yo->schema_features)) {
71 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010072 }
73 break;
74
75 case 'h':
76 cmd_load_help();
aPieceka83b8e02023-06-07 15:25:16 +020077 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010078
Michal Vasko8d6d0ad2021-10-19 12:32:27 +020079 case 'i': /* --make-implemented */
aPieceka83b8e02023-06-07 15:25:16 +020080 if (yo->ctx_options & LY_CTX_REF_IMPLEMENTED) {
81 yo->ctx_options &= ~LY_CTX_REF_IMPLEMENTED;
82 yo->ctx_options |= LY_CTX_ALL_IMPLEMENTED;
Radek Krejcie9f13b12020-11-09 17:42:04 +010083 } else {
aPieceka83b8e02023-06-07 15:25:16 +020084 yo->ctx_options |= LY_CTX_REF_IMPLEMENTED;
Radek Krejcie9f13b12020-11-09 17:42:04 +010085 }
86 break;
87
aPiecek88030e52023-06-02 11:03:57 +020088 case 'X': /* --extended-leafref */
aPieceka83b8e02023-06-07 15:25:16 +020089 yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
aPiecek88030e52023-06-02 11:03:57 +020090 break;
91
Radek Krejcie9f13b12020-11-09 17:42:04 +010092 default:
93 YLMSG_E("Unknown option.\n");
aPieceka83b8e02023-06-07 15:25:16 +020094 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010095 }
96 }
97
aPieceka83b8e02023-06-07 15:25:16 +020098 *posv = &yo->argv[optind];
99 *posc = argc - optind;
100
101 return 0;
102}
103
104int
105cmd_load_dep(struct yl_opt *yo, int posc)
106{
107 if (yo->interactive && !posc) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100108 /* no argument */
aPieceka83b8e02023-06-07 15:25:16 +0200109 cmd_load_help();
110 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100111 }
112
aPieceka83b8e02023-06-07 15:25:16 +0200113 if (!yo->schema_features.count) {
Michal Vasko703370c2021-10-19 14:07:16 +0200114 /* no features, enable all of them */
aPieceka83b8e02023-06-07 15:25:16 +0200115 yo->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES;
Michal Vasko703370c2021-10-19 14:07:16 +0200116 }
117
aPieceka83b8e02023-06-07 15:25:16 +0200118 return 0;
119}
120
121int
122cmd_load_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
123{
124 const char *all_features[] = {"*", NULL};
125 char *revision;
126 const char **features = NULL;
127
128 assert(posv);
129
130 if (yo->ctx_options) {
131 ly_ctx_set_options(*ctx, yo->ctx_options);
132 yo->ctx_options = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100133 }
134
aPieceka83b8e02023-06-07 15:25:16 +0200135 /* get revision */
136 revision = strchr(posv, '@');
137 if (revision) {
138 revision[0] = '\0';
139 ++revision;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100140 }
141
aPieceka83b8e02023-06-07 15:25:16 +0200142 /* get features list for this module */
143 if (!yo->schema_features.count) {
144 features = all_features;
145 } else {
146 get_features(&yo->schema_features, posv, &features);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100147 }
aPieceka83b8e02023-06-07 15:25:16 +0200148
149 /* load the module */
150 if (!ly_ctx_load_module(*ctx, posv, revision, features)) {
151 /* libyang printed the error messages */
152 return 1;
153 }
154
155 return 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100156}