From cd07912073c951b4bbb871ed2653af1be2cfc714 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 11:55:11 +0200 Subject: Adding upstream version 2.1.30. Signed-off-by: Daniel Baumann --- tools/lint/cmd_list.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tools/lint/cmd_list.c (limited to 'tools/lint/cmd_list.c') diff --git a/tools/lint/cmd_list.c b/tools/lint/cmd_list.c new file mode 100644 index 0000000..ec7a021 --- /dev/null +++ b/tools/lint/cmd_list.c @@ -0,0 +1,89 @@ +/** + * @file cmd_list.c + * @author Michal Vasko + * @author Radek Krejci + * @brief 'list' command of the libyang's yanglint tool. + * + * Copyright (c) 2015-2020 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. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#define _GNU_SOURCE + +#include "cmd.h" + +#include +#include +#include + +#include "libyang.h" + +#include "common.h" + +void +cmd_list_help(void) +{ + printf("Usage: list [-f (xml | json)]\n" + " Print the list of modules in the current context\n\n" + " -f FORMAT, --format=FORMAT\n" + " Print the list as ietf-yang-library data in the specified\n" + " data FORMAT. If format not specified, a simple list is\n" + " printed with an indication of imported (i) / implemented (I)\n" + " modules.\n"); +} + +void +cmd_list(struct ly_ctx **ctx, const char *cmdline) +{ + int argc = 0; + char **argv = NULL; + int opt, opt_index; + struct option options[] = { + {"format", required_argument, NULL, 'f'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0} + }; + LYD_FORMAT format = LYD_UNKNOWN; + struct ly_out *out = NULL; + + if (parse_cmdline(cmdline, &argc, &argv)) { + goto cleanup; + } + + while ((opt = getopt_long(argc, argv, "f:h", options, &opt_index)) != -1) { + switch (opt) { + case 'f': /* --format */ + if (!strcasecmp(optarg, "xml")) { + format = LYD_XML; + } else if (!strcasecmp(optarg, "json")) { + format = LYD_JSON; + } else { + YLMSG_E("Unknown output format %s\n", optarg); + cmd_list_help(); + goto cleanup; + } + break; + case 'h': + cmd_list_help(); + goto cleanup; + default: + YLMSG_E("Unknown option.\n"); + goto cleanup; + } + } + + if (!ly_out_new_file(stdout, &out)) { + print_list(out, *ctx, format); + ly_out_free(out, NULL, 0); + } else { + YLMSG_E("Unable to print to the standard output.\n"); + } + +cleanup: + free_cmdline(argv); +} -- cgit v1.2.3