blob: fb9a2a7f9991922268de578b26697bcc8726b84c [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file cmd_list.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 'list' 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
21#include <getopt.h>
22#include <stdio.h>
23#include <strings.h>
24
25#include "libyang.h"
26
27#include "common.h"
aPieceka83b8e02023-06-07 15:25:16 +020028#include "yl_opt.h"
Radek Krejcie9f13b12020-11-09 17:42:04 +010029
30void
31cmd_list_help(void)
32{
33 printf("Usage: list [-f (xml | json)]\n"
34 " Print the list of modules in the current context\n\n"
35 " -f FORMAT, --format=FORMAT\n"
36 " Print the list as ietf-yang-library data in the specified\n"
37 " data FORMAT. If format not specified, a simple list is\n"
38 " printed with an indication of imported (i) / implemented (I)\n"
39 " modules.\n");
40}
41
aPieceka83b8e02023-06-07 15:25:16 +020042int
43cmd_list_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
Radek Krejcie9f13b12020-11-09 17:42:04 +010044{
aPieceka83b8e02023-06-07 15:25:16 +020045 int rc = 0, argc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +010046 int opt, opt_index;
47 struct option options[] = {
48 {"format", required_argument, NULL, 'f'},
49 {"help", no_argument, NULL, 'h'},
50 {NULL, 0, NULL, 0}
51 };
Radek Krejcie9f13b12020-11-09 17:42:04 +010052
aPieceka83b8e02023-06-07 15:25:16 +020053 yo->data_out_format = LYD_UNKNOWN;
54
55 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
56 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +010057 }
58
aPieceka83b8e02023-06-07 15:25:16 +020059 while ((opt = getopt_long(argc, yo->argv, commands[CMD_LIST].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010060 switch (opt) {
61 case 'f': /* --format */
62 if (!strcasecmp(optarg, "xml")) {
aPieceka83b8e02023-06-07 15:25:16 +020063 yo->data_out_format = LYD_XML;
Radek Krejcie9f13b12020-11-09 17:42:04 +010064 } else if (!strcasecmp(optarg, "json")) {
aPieceka83b8e02023-06-07 15:25:16 +020065 yo->data_out_format = LYD_JSON;
Radek Krejcie9f13b12020-11-09 17:42:04 +010066 } else {
67 YLMSG_E("Unknown output format %s\n", optarg);
68 cmd_list_help();
aPieceka83b8e02023-06-07 15:25:16 +020069 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010070 }
71 break;
72 case 'h':
73 cmd_list_help();
aPieceka83b8e02023-06-07 15:25:16 +020074 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010075 default:
76 YLMSG_E("Unknown option.\n");
aPieceka83b8e02023-06-07 15:25:16 +020077 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010078 }
79 }
80
aPieceka83b8e02023-06-07 15:25:16 +020081 *posv = &yo->argv[optind];
82 *posc = argc - optind;
Radek Krejcie9f13b12020-11-09 17:42:04 +010083
aPieceka83b8e02023-06-07 15:25:16 +020084 return 0;
85}
86
87int
88cmd_list_dep(struct yl_opt *yo, int posc)
89{
90 if (posc) {
91 YLMSG_E("No positional arguments are allowed.\n");
92 return 1;
93 }
94 if (ly_out_new_file(stdout, &yo->out)) {
95 YLMSG_E("Unable to print to the standard output.\n");
96 return 1;
97 }
98 yo->out_stdout = 1;
99
100 return 0;
101}
102
aPiecek8ec2e832023-06-12 15:35:34 +0200103/**
104 * @brief Print yang library data.
105 *
106 * @param[in] ctx Context for libyang.
107 * @param[in] data_out_format Output format of printed data.
108 * @param[in] out Output handler.
109 * @return 0 on success.
110 */
111static int
112print_yang_lib_data(struct ly_ctx *ctx, LYD_FORMAT data_out_format, struct ly_out *out)
113{
114 struct lyd_node *ylib;
115
116 if (ly_ctx_get_yanglib_data(ctx, &ylib, "%u", ly_ctx_get_change_count(ctx))) {
117 YLMSG_E("Getting context info (ietf-yang-library data) failed. If the YANG module is missing or not implemented, use an option to add it internally.\n");
118 return 1;
119 }
120
121 lyd_print_all(out, ylib, data_out_format, 0);
122 lyd_free_all(ylib);
123
124 return 0;
125}
126
aPieceka83b8e02023-06-07 15:25:16 +0200127int
128cmd_list_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
129{
130 (void) posv;
aPiecek8ec2e832023-06-12 15:35:34 +0200131 uint32_t idx = 0, has_modules = 0;
132 const struct lys_module *mod;
133
134 if (yo->data_out_format != LYD_UNKNOWN) {
135 /* ietf-yang-library data are printed in the specified format */
136 if (print_yang_lib_data(*ctx, yo->data_out_format, yo->out)) {
137 return 1;
138 }
139 return 0;
140 }
141
142 /* iterate schemas in context and provide just the basic info */
143 ly_print(yo->out, "List of the loaded models:\n");
144 while ((mod = ly_ctx_get_module_iter(*ctx, &idx))) {
145 has_modules++;
146
147 /* conformance print */
148 if (mod->implemented) {
149 ly_print(yo->out, " I");
150 } else {
151 ly_print(yo->out, " i");
152 }
153
154 /* module print */
155 ly_print(yo->out, " %s", mod->name);
156 if (mod->revision) {
157 ly_print(yo->out, "@%s", mod->revision);
158 }
159
160 /* submodules print */
161 if (mod->parsed && mod->parsed->includes) {
162 uint64_t u = 0;
163
164 ly_print(yo->out, " (");
165 LY_ARRAY_FOR(mod->parsed->includes, u) {
166 ly_print(yo->out, "%s%s", !u ? "" : ",", mod->parsed->includes[u].name);
167 if (mod->parsed->includes[u].rev[0]) {
168 ly_print(yo->out, "@%s", mod->parsed->includes[u].rev);
169 }
170 }
171 ly_print(yo->out, ")");
172 }
173
174 /* finish the line */
175 ly_print(yo->out, "\n");
176 }
177
178 if (!has_modules) {
179 ly_print(yo->out, "\t(none)\n");
180 }
181
182 ly_print_flush(yo->out);
183
184 return 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100185}