blob: 96d55c1d50e6e28ada42cb8c82f154cadcb91f78 [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"
aPiecekd8f002f2023-06-21 10:40:41 +020028#include "yl_schema_features.h"
Michal Vasko2e79f262021-10-19 14:29:07 +020029
30void
31cmd_feature_help(void)
32{
roman300b8782022-08-11 12:49:21 +020033 printf("Usage: feature [-f] <module> [<module>]*\n"
34 " feature -a [-f]\n"
35 " Print features of all the modules with state of each one.\n\n"
36 " -f <module1, module2, ...>, --feature-param <module1, module2, ...>\n"
37 " Generate features parameter for the command \"add\" \n"
38 " in the form of -F <module-name>:<features>\n"
39 " -a, --all \n"
40 " Print features of all implemented modules.\n");
Michal Vasko2e79f262021-10-19 14:29:07 +020041}
42
aPieceka83b8e02023-06-07 15:25:16 +020043int
44cmd_feature_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
Michal Vasko2e79f262021-10-19 14:29:07 +020045{
aPieceka83b8e02023-06-07 15:25:16 +020046 int rc = 0, argc = 0;
47 int opt, opt_index;
Michal Vasko2e79f262021-10-19 14:29:07 +020048 struct option options[] = {
49 {"help", no_argument, NULL, 'h'},
roman300b8782022-08-11 12:49:21 +020050 {"all", no_argument, NULL, 'a'},
51 {"feature-param", no_argument, NULL, 'f'},
Michal Vasko2e79f262021-10-19 14:29:07 +020052 {NULL, 0, NULL, 0}
53 };
Michal Vasko2e79f262021-10-19 14:29:07 +020054
aPieceka83b8e02023-06-07 15:25:16 +020055 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
56 return rc;
Michal Vasko2e79f262021-10-19 14:29:07 +020057 }
58
aPieceka83b8e02023-06-07 15:25:16 +020059 while ((opt = getopt_long(argc, yo->argv, commands[CMD_FEATURE].optstring, options, &opt_index)) != -1) {
Michal Vasko2e79f262021-10-19 14:29:07 +020060 switch (opt) {
61 case 'h':
62 cmd_feature_help();
aPieceka83b8e02023-06-07 15:25:16 +020063 return 1;
roman300b8782022-08-11 12:49:21 +020064 case 'a':
aPieceka83b8e02023-06-07 15:25:16 +020065 yo->feature_print_all = 1;
roman300b8782022-08-11 12:49:21 +020066 break;
67 case 'f':
aPieceka83b8e02023-06-07 15:25:16 +020068 yo->feature_param_format = 1;
roman300b8782022-08-11 12:49:21 +020069 break;
Michal Vasko2e79f262021-10-19 14:29:07 +020070 default:
Michal Vasko1407d7d2023-08-21 09:57:54 +020071 YLMSG_E("Unknown option.");
aPieceka83b8e02023-06-07 15:25:16 +020072 return 1;
Michal Vasko2e79f262021-10-19 14:29:07 +020073 }
74 }
75
aPieceka83b8e02023-06-07 15:25:16 +020076 *posv = &yo->argv[optind];
77 *posc = argc - optind;
78
79 return 0;
80}
81
82int
83cmd_feature_dep(struct yl_opt *yo, int posc)
84{
85 if (ly_out_new_file(stdout, &yo->out)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020086 YLMSG_E("Unable to print to the standard output.");
aPieceka83b8e02023-06-07 15:25:16 +020087 return 1;
roman300b8782022-08-11 12:49:21 +020088 }
aPieceka83b8e02023-06-07 15:25:16 +020089 yo->out_stdout = 1;
roman300b8782022-08-11 12:49:21 +020090
aPieceka83b8e02023-06-07 15:25:16 +020091 if (yo->interactive && !yo->feature_print_all && !posc) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020092 YLMSG_E("Missing modules to print.");
aPieceka83b8e02023-06-07 15:25:16 +020093 return 1;
94 }
95
aPieceka83b8e02023-06-07 15:25:16 +020096 return 0;
97}
98
99int
100cmd_feature_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
101{
aPieceka83b8e02023-06-07 15:25:16 +0200102 const struct lys_module *mod;
103
104 if (yo->feature_print_all) {
aPiecek6fde6d02023-06-21 15:06:45 +0200105 print_all_features(yo->out, *ctx, yo->feature_param_format);
106 return 0;
Michal Vasko2e79f262021-10-19 14:29:07 +0200107 }
108
aPieceka83b8e02023-06-07 15:25:16 +0200109 mod = ly_ctx_get_module_latest(*ctx, posv);
110 if (!mod) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200111 YLMSG_E("Module \"%s\" not found.", posv);
aPiecek6fde6d02023-06-21 15:06:45 +0200112 return 1;
Michal Vasko2e79f262021-10-19 14:29:07 +0200113 }
114
aPieceka83b8e02023-06-07 15:25:16 +0200115 if (yo->feature_param_format) {
aPiecek6fde6d02023-06-21 15:06:45 +0200116 print_feature_param(yo->out, mod);
117 } else {
118 print_features(yo->out, mod);
aPieceka83b8e02023-06-07 15:25:16 +0200119 }
120
aPiecek6fde6d02023-06-21 15:06:45 +0200121 return 0;
aPieceka83b8e02023-06-07 15:25:16 +0200122}
123
124int
aPiecek6fde6d02023-06-21 15:06:45 +0200125cmd_feature_fin(struct ly_ctx *ctx, struct yl_opt *yo)
aPieceka83b8e02023-06-07 15:25:16 +0200126{
127 (void) ctx;
128
aPiecek6fde6d02023-06-21 15:06:45 +0200129 ly_print(yo->out, "\n");
aPieceka83b8e02023-06-07 15:25:16 +0200130 return 0;
Michal Vasko2e79f262021-10-19 14:29:07 +0200131}