Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_feature.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 4 | * @author Adam Piecek <piecek@cesnet.cz> |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 5 | * @brief 'feature' command of the libyang's yanglint tool. |
| 6 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015-2023 CESNET, z.s.p.o. |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 8 | * |
| 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" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 27 | #include "yl_opt.h" |
aPiecek | d8f002f | 2023-06-21 10:40:41 +0200 | [diff] [blame] | 28 | #include "yl_schema_features.h" |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 29 | |
| 30 | void |
| 31 | cmd_feature_help(void) |
| 32 | { |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 33 | 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 Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 41 | } |
| 42 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 43 | int |
| 44 | cmd_feature_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 45 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 46 | int rc = 0, argc = 0; |
| 47 | int opt, opt_index; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 48 | struct option options[] = { |
| 49 | {"help", no_argument, NULL, 'h'}, |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 50 | {"all", no_argument, NULL, 'a'}, |
| 51 | {"feature-param", no_argument, NULL, 'f'}, |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 52 | {NULL, 0, NULL, 0} |
| 53 | }; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 54 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 55 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 56 | return rc; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 57 | } |
| 58 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 59 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_FEATURE].optstring, options, &opt_index)) != -1) { |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 60 | switch (opt) { |
| 61 | case 'h': |
| 62 | cmd_feature_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 63 | return 1; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 64 | case 'a': |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 65 | yo->feature_print_all = 1; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 66 | break; |
| 67 | case 'f': |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 68 | yo->feature_param_format = 1; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 69 | break; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 70 | default: |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 71 | YLMSG_E("Unknown option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 72 | return 1; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 76 | *posv = &yo->argv[optind]; |
| 77 | *posc = argc - optind; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int |
| 83 | cmd_feature_dep(struct yl_opt *yo, int posc) |
| 84 | { |
| 85 | if (ly_out_new_file(stdout, &yo->out)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 86 | YLMSG_E("Unable to print to the standard output."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 87 | return 1; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 88 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 89 | yo->out_stdout = 1; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 90 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 91 | if (yo->interactive && !yo->feature_print_all && !posc) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 92 | YLMSG_E("Missing modules to print."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 93 | return 1; |
| 94 | } |
| 95 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | cmd_feature_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
| 101 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 102 | const struct lys_module *mod; |
| 103 | |
| 104 | if (yo->feature_print_all) { |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 105 | print_all_features(yo->out, *ctx, yo->feature_param_format); |
| 106 | return 0; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 107 | } |
| 108 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 109 | mod = ly_ctx_get_module_latest(*ctx, posv); |
| 110 | if (!mod) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 111 | YLMSG_E("Module \"%s\" not found.", posv); |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 112 | return 1; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 113 | } |
| 114 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 115 | if (yo->feature_param_format) { |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 116 | print_feature_param(yo->out, mod); |
| 117 | } else { |
| 118 | print_features(yo->out, mod); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 119 | } |
| 120 | |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 121 | return 0; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | int |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 125 | cmd_feature_fin(struct ly_ctx *ctx, struct yl_opt *yo) |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 126 | { |
| 127 | (void) ctx; |
| 128 | |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 129 | ly_print(yo->out, "\n"); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 130 | return 0; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 131 | } |