blob: a3b1cfd4b9e8474a3fde7b5021fbefaf31799401 [file] [log] [blame]
aPiecek647f62e2023-05-18 10:55:58 +02001/**
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"
27
28char *filename;
29
30void
31cmd_extdata_free(void)
32{
33 free(filename);
34 filename = NULL;
35}
36
37void
38cmd_extdata_help(void)
39{
40 printf("Usage: extdata [--clear] [<extdata-file-path>]\n"
41 " File containing the specific data required by an extension. Required by\n"
42 " the schema-mount extension, for example, when the operational data are\n"
43 " expected in the file. File format is guessed.\n");
44}
45
46void
47cmd_extdata(struct ly_ctx **ctx, const char *cmdline)
48{
49 int argc = 0;
50 char **argv = NULL;
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 };
57 int8_t cleared = 0;
58 int8_t file_count = 0;
59
60 if (parse_cmdline(cmdline, &argc, &argv)) {
61 goto cleanup;
62 }
63
64 while ((opt = getopt_long(argc, argv, commands[CMD_EXTDATA].optstring, options, &opt_index)) != -1) {
65 switch (opt) {
66 case 'c':
67 ly_ctx_set_ext_data_clb(*ctx, NULL, NULL);
68 free(filename);
69 filename = NULL;
70 cleared = 1;
71 break;
72 case 'h':
73 cmd_extdata_help();
74 goto cleanup;
75 default:
76 YLMSG_E("Unknown option.\n");
77 goto cleanup;
78 }
79 }
80
81 file_count = argc - 1 - cleared;
82
83 if (!cleared && (file_count == 0)) {
84 /* no argument - print the current file */
85 printf("%s\n", filename ? filename : "No file set.");
86 } else if (file_count == 1) {
87 /* set callback providing run-time extension instance data */
88 free(filename);
89 filename = strdup(argv[optind]);
90 if (!filename) {
91 YLMSG_E("Memory allocation error.\n");
92 goto cleanup;
93 }
94 ly_ctx_set_ext_data_clb(*ctx, ext_data_clb, filename);
95 } else if (!cleared) {
96 YLMSG_E("Only one file must be entered.\n");
97 }
98
99cleanup:
100 free_cmdline(argv);
101}