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" |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 28 | |
| 29 | void |
| 30 | cmd_feature_help(void) |
| 31 | { |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 32 | 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 Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 40 | } |
| 41 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 42 | int |
| 43 | 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] | 44 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 45 | int rc = 0, argc = 0; |
| 46 | int opt, opt_index; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 47 | struct option options[] = { |
| 48 | {"help", no_argument, NULL, 'h'}, |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 49 | {"all", no_argument, NULL, 'a'}, |
| 50 | {"feature-param", no_argument, NULL, 'f'}, |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 51 | {NULL, 0, NULL, 0} |
| 52 | }; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 53 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 54 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 55 | return rc; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 56 | } |
| 57 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 58 | 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] | 59 | switch (opt) { |
| 60 | case 'h': |
| 61 | cmd_feature_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 62 | return 1; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 63 | case 'a': |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 64 | yo->feature_print_all = 1; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 65 | break; |
| 66 | case 'f': |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 67 | yo->feature_param_format = 1; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 68 | break; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 69 | default: |
| 70 | YLMSG_E("Unknown option.\n"); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 71 | return 1; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 75 | *posv = &yo->argv[optind]; |
| 76 | *posc = argc - optind; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | cmd_feature_dep(struct yl_opt *yo, int posc) |
| 83 | { |
| 84 | if (ly_out_new_file(stdout, &yo->out)) { |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 85 | YLMSG_E("Unable to print to the standard output.\n"); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 86 | return 1; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 87 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 88 | yo->out_stdout = 1; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 89 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 90 | if (yo->interactive && !yo->feature_print_all && !posc) { |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 91 | YLMSG_E("Missing modules to print.\n"); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 92 | 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 | |
| 103 | int |
| 104 | cmd_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 Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 119 | goto cleanup; |
| 120 | } |
| 121 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 122 | /* always erase the set, so the previous module's features don't carry over to the next module's features */ |
| 123 | ly_set_erase(&set, NULL); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 124 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 125 | mod = ly_ctx_get_module_latest(*ctx, posv); |
| 126 | if (!mod) { |
| 127 | YLMSG_E("Module \"%s\" not found.\n", posv); |
| 128 | rc = 1; |
| 129 | goto cleanup; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 130 | } |
| 131 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 132 | /* collect features of the module */ |
| 133 | if (collect_features(mod, &set)) { |
| 134 | rc = 1; |
| 135 | goto cleanup; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 136 | } |
| 137 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 138 | if (yo->feature_param_format) { |
| 139 | if (generate_features_output(mod, &set, &yo->features_output)) { |
| 140 | rc = 1; |
| 141 | goto cleanup; |
| 142 | } |
| 143 | /* don't print features and their state of each module if generating features parameter */ |
| 144 | goto cleanup; |
| 145 | } |
| 146 | |
| 147 | print_features(yo->out, mod, &set); |
| 148 | |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 149 | cleanup: |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 150 | ly_set_erase(&set, NULL); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 151 | |
| 152 | return rc; |
| 153 | } |
| 154 | |
| 155 | int |
| 156 | cmd_feature_fin(struct ly_ctx *ctx, struct yl_opt *yo) |
| 157 | { |
| 158 | (void) ctx; |
| 159 | |
| 160 | if (yo->feature_param_format) { |
| 161 | printf("%s\n", yo->features_output); |
| 162 | } |
| 163 | |
| 164 | return 0; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 165 | } |