blob: 09f8a295be02e99743e84acde27cd725923f982b [file] [log] [blame]
Michal Vasko2e79f262021-10-19 14:29:07 +02001/**
2 * @file cmd_feature.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
aPieceka83b8e02023-06-07 15:25:16 +02004 * @author Adam Piecek <piecek@cesnet.cz>
Michal Vasko2e79f262021-10-19 14:29:07 +02005 * @brief 'feature' command of the libyang's yanglint tool.
6 *
aPieceka83b8e02023-06-07 15:25:16 +02007 * Copyright (c) 2015-2023 CESNET, z.s.p.o.
Michal Vasko2e79f262021-10-19 14:29:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#define _GNU_SOURCE
17
18#include "cmd.h"
19
20#include <getopt.h>
21#include <stdint.h>
22#include <stdio.h>
23
24#include "libyang.h"
25
26#include "common.h"
aPieceka83b8e02023-06-07 15:25:16 +020027#include "yl_opt.h"
Michal Vasko2e79f262021-10-19 14:29:07 +020028
29void
30cmd_feature_help(void)
31{
roman300b8782022-08-11 12:49:21 +020032 printf("Usage: feature [-f] <module> [<module>]*\n"
33 " feature -a [-f]\n"
34 " Print features of all the modules with state of each one.\n\n"
35 " -f <module1, module2, ...>, --feature-param <module1, module2, ...>\n"
36 " Generate features parameter for the command \"add\" \n"
37 " in the form of -F <module-name>:<features>\n"
38 " -a, --all \n"
39 " Print features of all implemented modules.\n");
Michal Vasko2e79f262021-10-19 14:29:07 +020040}
41
aPieceka83b8e02023-06-07 15:25:16 +020042int
43cmd_feature_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
Michal Vasko2e79f262021-10-19 14:29:07 +020044{
aPieceka83b8e02023-06-07 15:25:16 +020045 int rc = 0, argc = 0;
46 int opt, opt_index;
Michal Vasko2e79f262021-10-19 14:29:07 +020047 struct option options[] = {
48 {"help", no_argument, NULL, 'h'},
roman300b8782022-08-11 12:49:21 +020049 {"all", no_argument, NULL, 'a'},
50 {"feature-param", no_argument, NULL, 'f'},
Michal Vasko2e79f262021-10-19 14:29:07 +020051 {NULL, 0, NULL, 0}
52 };
Michal Vasko2e79f262021-10-19 14:29:07 +020053
aPieceka83b8e02023-06-07 15:25:16 +020054 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
55 return rc;
Michal Vasko2e79f262021-10-19 14:29:07 +020056 }
57
aPieceka83b8e02023-06-07 15:25:16 +020058 while ((opt = getopt_long(argc, yo->argv, commands[CMD_FEATURE].optstring, options, &opt_index)) != -1) {
Michal Vasko2e79f262021-10-19 14:29:07 +020059 switch (opt) {
60 case 'h':
61 cmd_feature_help();
aPieceka83b8e02023-06-07 15:25:16 +020062 return 1;
roman300b8782022-08-11 12:49:21 +020063 case 'a':
aPieceka83b8e02023-06-07 15:25:16 +020064 yo->feature_print_all = 1;
roman300b8782022-08-11 12:49:21 +020065 break;
66 case 'f':
aPieceka83b8e02023-06-07 15:25:16 +020067 yo->feature_param_format = 1;
roman300b8782022-08-11 12:49:21 +020068 break;
Michal Vasko2e79f262021-10-19 14:29:07 +020069 default:
70 YLMSG_E("Unknown option.\n");
aPieceka83b8e02023-06-07 15:25:16 +020071 return 1;
Michal Vasko2e79f262021-10-19 14:29:07 +020072 }
73 }
74
aPieceka83b8e02023-06-07 15:25:16 +020075 *posv = &yo->argv[optind];
76 *posc = argc - optind;
77
78 return 0;
79}
80
81int
82cmd_feature_dep(struct yl_opt *yo, int posc)
83{
84 if (ly_out_new_file(stdout, &yo->out)) {
roman300b8782022-08-11 12:49:21 +020085 YLMSG_E("Unable to print to the standard output.\n");
aPieceka83b8e02023-06-07 15:25:16 +020086 return 1;
roman300b8782022-08-11 12:49:21 +020087 }
aPieceka83b8e02023-06-07 15:25:16 +020088 yo->out_stdout = 1;
roman300b8782022-08-11 12:49:21 +020089
aPieceka83b8e02023-06-07 15:25:16 +020090 if (yo->interactive && !yo->feature_print_all && !posc) {
Michal Vasko2e79f262021-10-19 14:29:07 +020091 YLMSG_E("Missing modules to print.\n");
aPieceka83b8e02023-06-07 15:25:16 +020092 return 1;
93 }
94
95 if (yo->feature_print_all && posc) {
96 YLMSG_E("No positional arguments are allowed.\n");
97 return 1;
98 }
99
100 return 0;
101}
102
103int
104cmd_feature_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
105{
106 int rc = 0;
107 struct ly_set set = {0};
108 const struct lys_module *mod;
109
110 if (yo->feature_print_all) {
111 if (print_all_features(yo->out, *ctx, yo->feature_param_format, &yo->features_output)) {
112 YLMSG_E("Printing all features failed.\n");
113 rc = 1;
114 goto cleanup;
115 }
116 if (yo->feature_param_format) {
117 printf("%s\n", yo->features_output);
118 }
Michal Vasko2e79f262021-10-19 14:29:07 +0200119 goto cleanup;
120 }
121
aPieceka83b8e02023-06-07 15:25:16 +0200122 mod = ly_ctx_get_module_latest(*ctx, posv);
123 if (!mod) {
124 YLMSG_E("Module \"%s\" not found.\n", posv);
125 rc = 1;
126 goto cleanup;
roman300b8782022-08-11 12:49:21 +0200127 }
128
aPieceka83b8e02023-06-07 15:25:16 +0200129 /* collect features of the module */
130 if (collect_features(mod, &set)) {
131 rc = 1;
132 goto cleanup;
Michal Vasko2e79f262021-10-19 14:29:07 +0200133 }
134
aPieceka83b8e02023-06-07 15:25:16 +0200135 if (yo->feature_param_format) {
136 if (generate_features_output(mod, &set, &yo->features_output)) {
137 rc = 1;
138 goto cleanup;
139 }
140 /* don't print features and their state of each module if generating features parameter */
141 goto cleanup;
142 }
143
aPiecekf8ad2a02023-06-15 09:10:39 +0200144 if (yo->interactive) {
145 print_features(yo->out, mod, &set);
146 }
aPieceka83b8e02023-06-07 15:25:16 +0200147
Michal Vasko2e79f262021-10-19 14:29:07 +0200148cleanup:
roman300b8782022-08-11 12:49:21 +0200149 ly_set_erase(&set, NULL);
aPieceka83b8e02023-06-07 15:25:16 +0200150
151 return rc;
152}
153
154int
aPiecekcf9e7682023-06-19 14:03:50 +0200155cmd_feature_print_fparam(struct ly_ctx *ctx, struct yl_opt *yo)
aPieceka83b8e02023-06-07 15:25:16 +0200156{
157 (void) ctx;
158
159 if (yo->feature_param_format) {
160 printf("%s\n", yo->features_output);
161 }
162
163 return 0;
Michal Vasko2e79f262021-10-19 14:29:07 +0200164}