diff options
Diffstat (limited to 'tools/lint/cmd_feature.c')
-rw-r--r-- | tools/lint/cmd_feature.c | 122 |
1 files changed, 59 insertions, 63 deletions
diff --git a/tools/lint/cmd_feature.c b/tools/lint/cmd_feature.c index 6b332ab..96d55c1 100644 --- a/tools/lint/cmd_feature.c +++ b/tools/lint/cmd_feature.c @@ -1,9 +1,10 @@ /** * @file cmd_feature.c * @author Michal Vasko <mvasko@cesnet.cz> + * @author Adam Piecek <piecek@cesnet.cz> * @brief 'feature' command of the libyang's yanglint tool. * - * Copyright (c) 2015-2021 CESNET, z.s.p.o. + * Copyright (c) 2015-2023 CESNET, z.s.p.o. * * This source code is licensed under BSD 3-Clause License (the "License"). * You may not use this file except in compliance with the License. @@ -23,6 +24,8 @@ #include "libyang.h" #include "common.h" +#include "yl_opt.h" +#include "yl_schema_features.h" void cmd_feature_help(void) @@ -37,17 +40,11 @@ cmd_feature_help(void) " Print features of all implemented modules.\n"); } -void -cmd_feature(struct ly_ctx **ctx, const char *cmdline) +int +cmd_feature_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) { - int argc = 0; - char **argv = NULL; - char *features_output = NULL; - int opt, opt_index, i; - ly_bool generate_features = 0, print_all = 0; - struct ly_set set = {0}; - const struct lys_module *mod; - struct ly_out *out = NULL; + int rc = 0, argc = 0; + int opt, opt_index; struct option options[] = { {"help", no_argument, NULL, 'h'}, {"all", no_argument, NULL, 'a'}, @@ -55,81 +52,80 @@ cmd_feature(struct ly_ctx **ctx, const char *cmdline) {NULL, 0, NULL, 0} }; - if (parse_cmdline(cmdline, &argc, &argv)) { - goto cleanup; + if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { + return rc; } - while ((opt = getopt_long(argc, argv, "haf", options, &opt_index)) != -1) { + while ((opt = getopt_long(argc, yo->argv, commands[CMD_FEATURE].optstring, options, &opt_index)) != -1) { switch (opt) { case 'h': cmd_feature_help(); - goto cleanup; + return 1; case 'a': - print_all = 1; + yo->feature_print_all = 1; break; case 'f': - generate_features = 1; + yo->feature_param_format = 1; break; default: - YLMSG_E("Unknown option.\n"); - goto cleanup; + YLMSG_E("Unknown option."); + return 1; } } - if (ly_out_new_file(stdout, &out)) { - YLMSG_E("Unable to print to the standard output.\n"); - goto cleanup; - } + *posv = &yo->argv[optind]; + *posc = argc - optind; - if (print_all) { - if (print_all_features(out, *ctx, generate_features, &features_output)) { - YLMSG_E("Printing all features failed.\n"); - goto cleanup; - } - if (generate_features) { - printf("%s\n", features_output); - } - goto cleanup; - } + return 0; +} - if (argc == optind) { - YLMSG_E("Missing modules to print.\n"); - goto cleanup; +int +cmd_feature_dep(struct yl_opt *yo, int posc) +{ + if (ly_out_new_file(stdout, &yo->out)) { + YLMSG_E("Unable to print to the standard output."); + return 1; } + yo->out_stdout = 1; - for (i = 0; i < argc - optind; i++) { - /* always erase the set, so the previous module's features don't carry over to the next module's features */ - ly_set_erase(&set, NULL); + if (yo->interactive && !yo->feature_print_all && !posc) { + YLMSG_E("Missing modules to print."); + return 1; + } - mod = ly_ctx_get_module_latest(*ctx, argv[optind + i]); - if (!mod) { - YLMSG_E("Module \"%s\" not found.\n", argv[optind + i]); - goto cleanup; - } + return 0; +} - /* collect features of the module */ - if (collect_features(mod, &set)) { - goto cleanup; - } +int +cmd_feature_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) +{ + const struct lys_module *mod; - if (generate_features) { - if (generate_features_output(mod, &set, &features_output)) { - goto cleanup; - } - /* don't print features and their state of each module if generating features parameter */ - continue; - } + if (yo->feature_print_all) { + print_all_features(yo->out, *ctx, yo->feature_param_format); + return 0; + } - print_features(out, mod, &set); + mod = ly_ctx_get_module_latest(*ctx, posv); + if (!mod) { + YLMSG_E("Module \"%s\" not found.", posv); + return 1; } - if (generate_features) { - printf("%s\n", features_output); + if (yo->feature_param_format) { + print_feature_param(yo->out, mod); + } else { + print_features(yo->out, mod); } -cleanup: - free_cmdline(argv); - ly_out_free(out, NULL, 0); - ly_set_erase(&set, NULL); - free(features_output); + return 0; +} + +int +cmd_feature_fin(struct ly_ctx *ctx, struct yl_opt *yo) +{ + (void) ctx; + + ly_print(yo->out, "\n"); + return 0; } |