blob: 39a8d69086f688cfa5832861b25e6fff50f0f665 [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file cmd_list.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @author Radek Krejci <rkrejci@cesnet.cz>
5 * @brief 'list' command of the libyang's yanglint tool.
6 *
7 * Copyright (c) 2015-2020 CESNET, z.s.p.o.
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 <stdio.h>
22#include <strings.h>
23
24#include "libyang.h"
25
26#include "common.h"
27
28void
29cmd_list_help(void)
30{
31 printf("Usage: list [-f (xml | json)]\n"
32 " Print the list of modules in the current context\n\n"
33 " -f FORMAT, --format=FORMAT\n"
34 " Print the list as ietf-yang-library data in the specified\n"
35 " data FORMAT. If format not specified, a simple list is\n"
36 " printed with an indication of imported (i) / implemented (I)\n"
37 " modules.\n");
38}
39
40void
41cmd_list(struct ly_ctx **ctx, const char *cmdline)
42{
43 int argc = 0;
44 char **argv = NULL;
45 int opt, opt_index;
46 struct option options[] = {
47 {"format", required_argument, NULL, 'f'},
48 {"help", no_argument, NULL, 'h'},
49 {NULL, 0, NULL, 0}
50 };
51 LYD_FORMAT format = LYD_UNKNOWN;
52 struct ly_out *out = NULL;
53
54 if (parse_cmdline(cmdline, &argc, &argv)) {
55 goto cleanup;
56 }
57
58 while ((opt = getopt_long(argc, argv, "f:h", options, &opt_index)) != -1) {
59 switch (opt) {
60 case 'f': /* --format */
61 if (!strcasecmp(optarg, "xml")) {
62 format = LYD_XML;
63 } else if (!strcasecmp(optarg, "json")) {
64 format = LYD_JSON;
65 } else {
66 YLMSG_E("Unknown output format %s\n", optarg);
67 cmd_list_help();
68 goto cleanup;
69 }
70 break;
71 case 'h':
72 cmd_list_help();
73 goto cleanup;
74 default:
75 YLMSG_E("Unknown option.\n");
76 goto cleanup;
77 }
78 }
79
80 ly_out_new_file(stdout, &out);
81 print_list(out, *ctx, format);
82 ly_out_free(out, NULL, 0);
83
84cleanup:
85 free_cmdline(argv);
86}