blob: 47a74955c2f0ae4309b7f4eb680c1c64a16873e6 [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>
aPieceka83b8e02023-06-07 15:25:16 +02005 * @author Adam Piecek <piecek@cesnet.cz>
Radek Krejcie9f13b12020-11-09 17:42:04 +01006 * @brief 'list' command of the libyang's yanglint tool.
7 *
aPieceka83b8e02023-06-07 15:25:16 +02008 * Copyright (c) 2015-2023 CESNET, z.s.p.o.
Radek Krejcie9f13b12020-11-09 17:42:04 +01009 *
10 * This source code is licensed under BSD 3-Clause License (the "License").
11 * You may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * https://opensource.org/licenses/BSD-3-Clause
15 */
16
17#define _GNU_SOURCE
18
19#include "cmd.h"
20
21#include <getopt.h>
22#include <stdio.h>
23#include <strings.h>
24
25#include "libyang.h"
26
27#include "common.h"
aPieceka83b8e02023-06-07 15:25:16 +020028#include "yl_opt.h"
Radek Krejcie9f13b12020-11-09 17:42:04 +010029
30void
31cmd_list_help(void)
32{
33 printf("Usage: list [-f (xml | json)]\n"
34 " Print the list of modules in the current context\n\n"
35 " -f FORMAT, --format=FORMAT\n"
36 " Print the list as ietf-yang-library data in the specified\n"
37 " data FORMAT. If format not specified, a simple list is\n"
38 " printed with an indication of imported (i) / implemented (I)\n"
39 " modules.\n");
40}
41
aPieceka83b8e02023-06-07 15:25:16 +020042int
43cmd_list_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
Radek Krejcie9f13b12020-11-09 17:42:04 +010044{
aPieceka83b8e02023-06-07 15:25:16 +020045 int rc = 0, argc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +010046 int opt, opt_index;
47 struct option options[] = {
48 {"format", required_argument, NULL, 'f'},
49 {"help", no_argument, NULL, 'h'},
50 {NULL, 0, NULL, 0}
51 };
Radek Krejcie9f13b12020-11-09 17:42:04 +010052
aPieceka83b8e02023-06-07 15:25:16 +020053 yo->data_out_format = LYD_UNKNOWN;
54
55 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
56 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +010057 }
58
aPieceka83b8e02023-06-07 15:25:16 +020059 while ((opt = getopt_long(argc, yo->argv, commands[CMD_LIST].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010060 switch (opt) {
61 case 'f': /* --format */
62 if (!strcasecmp(optarg, "xml")) {
aPieceka83b8e02023-06-07 15:25:16 +020063 yo->data_out_format = LYD_XML;
Radek Krejcie9f13b12020-11-09 17:42:04 +010064 } else if (!strcasecmp(optarg, "json")) {
aPieceka83b8e02023-06-07 15:25:16 +020065 yo->data_out_format = LYD_JSON;
Radek Krejcie9f13b12020-11-09 17:42:04 +010066 } else {
67 YLMSG_E("Unknown output format %s\n", optarg);
68 cmd_list_help();
aPieceka83b8e02023-06-07 15:25:16 +020069 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010070 }
71 break;
72 case 'h':
73 cmd_list_help();
aPieceka83b8e02023-06-07 15:25:16 +020074 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010075 default:
76 YLMSG_E("Unknown option.\n");
aPieceka83b8e02023-06-07 15:25:16 +020077 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010078 }
79 }
80
aPieceka83b8e02023-06-07 15:25:16 +020081 *posv = &yo->argv[optind];
82 *posc = argc - optind;
Radek Krejcie9f13b12020-11-09 17:42:04 +010083
aPieceka83b8e02023-06-07 15:25:16 +020084 return 0;
85}
86
87int
88cmd_list_dep(struct yl_opt *yo, int posc)
89{
90 if (posc) {
91 YLMSG_E("No positional arguments are allowed.\n");
92 return 1;
93 }
94 if (ly_out_new_file(stdout, &yo->out)) {
95 YLMSG_E("Unable to print to the standard output.\n");
96 return 1;
97 }
98 yo->out_stdout = 1;
99
100 return 0;
101}
102
103int
104cmd_list_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
105{
106 (void) posv;
107 return print_list(yo->out, *ctx, yo->data_out_format);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100108}