blob: fe14f4a009a4a98b98015dac57a1747fa9c3737c [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"
aPieceka83b8e02023-06-07 15:25:16 +020027#include "yl_opt.h"
aPiecek647f62e2023-05-18 10:55:58 +020028
29char *filename;
30
31void
32cmd_extdata_free(void)
33{
34 free(filename);
35 filename = NULL;
36}
37
38void
39cmd_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
aPieceka83b8e02023-06-07 15:25:16 +020047int
48cmd_extdata_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
aPiecek647f62e2023-05-18 10:55:58 +020049{
aPieceka83b8e02023-06-07 15:25:16 +020050 int rc = 0, argc = 0;
aPiecek647f62e2023-05-18 10:55:58 +020051 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 };
aPiecek647f62e2023-05-18 10:55:58 +020057
aPieceka83b8e02023-06-07 15:25:16 +020058 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
59 return rc;
aPiecek647f62e2023-05-18 10:55:58 +020060 }
61
aPieceka83b8e02023-06-07 15:25:16 +020062 while ((opt = getopt_long(argc, yo->argv, commands[CMD_EXTDATA].optstring, options, &opt_index)) != -1) {
aPiecek647f62e2023-05-18 10:55:58 +020063 switch (opt) {
64 case 'c':
aPieceka83b8e02023-06-07 15:25:16 +020065 yo->extdata_unset = 1;
aPiecek647f62e2023-05-18 10:55:58 +020066 free(filename);
67 filename = NULL;
aPiecek647f62e2023-05-18 10:55:58 +020068 break;
69 case 'h':
70 cmd_extdata_help();
aPieceka83b8e02023-06-07 15:25:16 +020071 return 1;
aPiecek647f62e2023-05-18 10:55:58 +020072 default:
73 YLMSG_E("Unknown option.\n");
aPieceka83b8e02023-06-07 15:25:16 +020074 return 1;
aPiecek647f62e2023-05-18 10:55:58 +020075 }
76 }
77
aPieceka83b8e02023-06-07 15:25:16 +020078 *posv = &yo->argv[optind];
79 *posc = argc - optind;
aPiecek647f62e2023-05-18 10:55:58 +020080
aPieceka83b8e02023-06-07 15:25:16 +020081 return 0;
82}
83
84int
85cmd_extdata_dep(struct yl_opt *yo, int posc)
86{
87 if (!yo->extdata_unset && (posc > 1)) {
88 YLMSG_E("Only one file must be entered.\n");
89 return 1;
90 }
91
92 return 0;
93}
94
95int
96cmd_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) {
aPiecek647f62e2023-05-18 10:55:58 +0200101 /* no argument - print the current file */
102 printf("%s\n", filename ? filename : "No file set.");
aPieceka83b8e02023-06-07 15:25:16 +0200103 } else if (posv) {
aPiecek647f62e2023-05-18 10:55:58 +0200104 /* set callback providing run-time extension instance data */
105 free(filename);
aPieceka83b8e02023-06-07 15:25:16 +0200106 filename = strdup(posv);
aPiecek647f62e2023-05-18 10:55:58 +0200107 if (!filename) {
108 YLMSG_E("Memory allocation error.\n");
aPieceka83b8e02023-06-07 15:25:16 +0200109 return 1;
aPiecek647f62e2023-05-18 10:55:58 +0200110 }
111 ly_ctx_set_ext_data_clb(*ctx, ext_data_clb, filename);
aPiecek647f62e2023-05-18 10:55:58 +0200112 }
113
aPieceka83b8e02023-06-07 15:25:16 +0200114 return 0;
aPiecek647f62e2023-05-18 10:55:58 +0200115}