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 | 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; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 127 | } |
| 128 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 129 | /* collect features of the module */ |
| 130 | if (collect_features(mod, &set)) { |
| 131 | rc = 1; |
| 132 | goto cleanup; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 133 | } |
| 134 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 135 | 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 | |
aPiecek | f8ad2a0 | 2023-06-15 09:10:39 +0200 | [diff] [blame] | 144 | if (yo->interactive) { |
| 145 | print_features(yo->out, mod, &set); |
| 146 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 147 | |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 148 | cleanup: |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 149 | ly_set_erase(&set, NULL); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 150 | |
| 151 | return rc; |
| 152 | } |
| 153 | |
| 154 | int |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 155 | cmd_feature_print_fparam(struct ly_ctx *ctx, struct yl_opt *yo) |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 156 | { |
| 157 | (void) ctx; |
| 158 | |
| 159 | if (yo->feature_param_format) { |
| 160 | printf("%s\n", yo->features_output); |
| 161 | } |
| 162 | |
| 163 | return 0; |
Michal Vasko | 2e79f26 | 2021-10-19 14:29:07 +0200 | [diff] [blame] | 164 | } |