blob: 166fbfa352e1eeec994a4942ec748bbcebd749e5 [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 {
Michal Vasko1407d7d2023-08-21 09:57:54 +020067 YLMSG_E("Unknown output format %s.", optarg);
Radek Krejcie9f13b12020-11-09 17:42:04 +010068 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:
Michal Vasko1407d7d2023-08-21 09:57:54 +020076 YLMSG_E("Unknown option.");
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) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020091 YLMSG_E("No positional arguments are allowed.");
aPieceka83b8e02023-06-07 15:25:16 +020092 return 1;
93 }
94 if (ly_out_new_file(stdout, &yo->out)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020095 YLMSG_E("Unable to print to the standard output.");
aPieceka83b8e02023-06-07 15:25:16 +020096 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))) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200117 YLMSG_E("Getting context info (ietf-yang-library data) failed. If the YANG module is missing or not implemented, "
118 "use an option to add it internally.");
aPiecek8ec2e832023-06-12 15:35:34 +0200119 return 1;
120 }
121
122 lyd_print_all(out, ylib, data_out_format, 0);
123 lyd_free_all(ylib);
124
125 return 0;
126}
127
aPieceka83b8e02023-06-07 15:25:16 +0200128int
129cmd_list_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
130{
131 (void) posv;
aPiecek8ec2e832023-06-12 15:35:34 +0200132 uint32_t idx = 0, has_modules = 0;
133 const struct lys_module *mod;
134
135 if (yo->data_out_format != LYD_UNKNOWN) {
136 /* ietf-yang-library data are printed in the specified format */
137 if (print_yang_lib_data(*ctx, yo->data_out_format, yo->out)) {
138 return 1;
139 }
140 return 0;
141 }
142
143 /* iterate schemas in context and provide just the basic info */
144 ly_print(yo->out, "List of the loaded models:\n");
145 while ((mod = ly_ctx_get_module_iter(*ctx, &idx))) {
146 has_modules++;
147
148 /* conformance print */
149 if (mod->implemented) {
150 ly_print(yo->out, " I");
151 } else {
152 ly_print(yo->out, " i");
153 }
154
155 /* module print */
156 ly_print(yo->out, " %s", mod->name);
157 if (mod->revision) {
158 ly_print(yo->out, "@%s", mod->revision);
159 }
160
161 /* submodules print */
162 if (mod->parsed && mod->parsed->includes) {
163 uint64_t u = 0;
164
165 ly_print(yo->out, " (");
166 LY_ARRAY_FOR(mod->parsed->includes, u) {
167 ly_print(yo->out, "%s%s", !u ? "" : ",", mod->parsed->includes[u].name);
168 if (mod->parsed->includes[u].rev[0]) {
169 ly_print(yo->out, "@%s", mod->parsed->includes[u].rev);
170 }
171 }
172 ly_print(yo->out, ")");
173 }
174
175 /* finish the line */
176 ly_print(yo->out, "\n");
177 }
178
179 if (!has_modules) {
180 ly_print(yo->out, "\t(none)\n");
181 }
182
183 ly_print_flush(yo->out);
184
185 return 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100186}