Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_list.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 5 | * @author Adam Piecek <piecek@cesnet.cz> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 6 | * @brief 'list' command of the libyang's yanglint tool. |
| 7 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 8 | * Copyright (c) 2015-2023 CESNET, z.s.p.o. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 9 | * |
| 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" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 28 | #include "yl_opt.h" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 29 | |
| 30 | void |
| 31 | cmd_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 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 42 | int |
| 43 | cmd_list_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 44 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 45 | int rc = 0, argc = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 46 | 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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 52 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 53 | yo->data_out_format = LYD_UNKNOWN; |
| 54 | |
| 55 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 56 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 57 | } |
| 58 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 59 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_LIST].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 60 | switch (opt) { |
| 61 | case 'f': /* --format */ |
| 62 | if (!strcasecmp(optarg, "xml")) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 63 | yo->data_out_format = LYD_XML; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 64 | } else if (!strcasecmp(optarg, "json")) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 65 | yo->data_out_format = LYD_JSON; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 66 | } else { |
| 67 | YLMSG_E("Unknown output format %s\n", optarg); |
| 68 | cmd_list_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 69 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 70 | } |
| 71 | break; |
| 72 | case 'h': |
| 73 | cmd_list_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 74 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 75 | default: |
| 76 | YLMSG_E("Unknown option.\n"); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 77 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 81 | *posv = &yo->argv[optind]; |
| 82 | *posc = argc - optind; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 83 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int |
| 88 | cmd_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 | |
| 103 | int |
| 104 | cmd_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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 108 | } |