aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_extdata.c |
| 3 | * @author Adam Piecek <piecek@cesnet.cz> |
| 4 | * @brief 'extdata' command of the libyang's yanglint tool. |
| 5 | * |
| 6 | * Copyright (c) 2015-2023 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
| 16 | #define _POSIX_C_SOURCE 200809L /* strdup */ |
| 17 | |
| 18 | #include "cmd.h" |
| 19 | |
| 20 | #include <getopt.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include "libyang.h" |
| 25 | |
| 26 | #include "common.h" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 27 | #include "yl_opt.h" |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 28 | |
| 29 | char *filename; |
| 30 | |
| 31 | void |
| 32 | cmd_extdata_free(void) |
| 33 | { |
| 34 | free(filename); |
| 35 | filename = NULL; |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | cmd_extdata_help(void) |
| 40 | { |
| 41 | printf("Usage: extdata [--clear] [<extdata-file-path>]\n" |
| 42 | " File containing the specific data required by an extension. Required by\n" |
| 43 | " the schema-mount extension, for example, when the operational data are\n" |
| 44 | " expected in the file. File format is guessed.\n"); |
| 45 | } |
| 46 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 47 | int |
| 48 | cmd_extdata_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 49 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 50 | int rc = 0, argc = 0; |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 51 | int opt, opt_index; |
| 52 | struct option options[] = { |
| 53 | {"clear", no_argument, NULL, 'c'}, |
| 54 | {"help", no_argument, NULL, 'h'}, |
| 55 | {NULL, 0, NULL, 0} |
| 56 | }; |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 57 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 58 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 59 | return rc; |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 60 | } |
| 61 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 62 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_EXTDATA].optstring, options, &opt_index)) != -1) { |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 63 | switch (opt) { |
| 64 | case 'c': |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 65 | yo->extdata_unset = 1; |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 66 | free(filename); |
| 67 | filename = NULL; |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 68 | break; |
| 69 | case 'h': |
| 70 | cmd_extdata_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 71 | return 1; |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 72 | default: |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 73 | YLMSG_E("Unknown option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 74 | return 1; |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 78 | *posv = &yo->argv[optind]; |
| 79 | *posc = argc - optind; |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 80 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | int |
| 85 | cmd_extdata_dep(struct yl_opt *yo, int posc) |
| 86 | { |
| 87 | if (!yo->extdata_unset && (posc > 1)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 88 | YLMSG_E("Only one file must be entered."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | int |
| 96 | cmd_extdata_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
| 97 | { |
| 98 | if (yo->extdata_unset) { |
| 99 | ly_ctx_set_ext_data_clb(*ctx, NULL, NULL); |
| 100 | } else if (!yo->extdata_unset && !posv) { |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 101 | /* no argument - print the current file */ |
| 102 | printf("%s\n", filename ? filename : "No file set."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 103 | } else if (posv) { |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 104 | /* set callback providing run-time extension instance data */ |
| 105 | free(filename); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 106 | filename = strdup(posv); |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 107 | if (!filename) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 108 | YLMSG_E("Memory allocation error."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 109 | return 1; |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 110 | } |
| 111 | ly_ctx_set_ext_data_clb(*ctx, ext_data_clb, filename); |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 112 | } |
| 113 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 114 | return 0; |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 115 | } |