blob: a6aeacff77139fd3eafa9b172c39b6469f0ce6c4 [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file cmd_searchpath.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 'searchpath' 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 <stdint.h>
23#include <stdio.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_searchpath_help(void)
32{
33 printf("Usage: searchpath [--clear] [<modules-dir-path> ...]\n"
aPiecek4f306da2023-03-27 09:06:07 +020034 " Set paths of directories where to search for imports and includes\n"
35 " of the schema modules. Subdirectories are also searched. The current\n"
36 " working directory and the path of the module being added is used implicitly.\n"
37 " The 'load' command uses these paths to search even for the schema modules\n"
38 " to be loaded.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010039}
40
aPieceka83b8e02023-06-07 15:25:16 +020041int
42cmd_searchpath_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
Radek Krejcie9f13b12020-11-09 17:42:04 +010043{
aPieceka83b8e02023-06-07 15:25:16 +020044 int rc = 0, argc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +010045 int opt, opt_index;
46 struct option options[] = {
47 {"clear", no_argument, NULL, 'c'},
48 {"help", no_argument, NULL, 'h'},
49 {NULL, 0, NULL, 0}
50 };
Radek Krejcie9f13b12020-11-09 17:42:04 +010051
aPieceka83b8e02023-06-07 15:25:16 +020052 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
53 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +010054 }
55
aPieceka83b8e02023-06-07 15:25:16 +020056 while ((opt = getopt_long(argc, yo->argv, commands[CMD_SEARCHPATH].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010057 switch (opt) {
58 case 'c':
aPieceka83b8e02023-06-07 15:25:16 +020059 yo->searchdir_unset = 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010060 break;
61 case 'h':
62 cmd_searchpath_help();
aPieceka83b8e02023-06-07 15:25:16 +020063 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010064 default:
Michal Vasko1407d7d2023-08-21 09:57:54 +020065 YLMSG_E("Unknown option.");
aPieceka83b8e02023-06-07 15:25:16 +020066 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +010067 }
68 }
69
aPieceka83b8e02023-06-07 15:25:16 +020070 *posv = &yo->argv[optind];
71 *posc = argc - optind;
72
73 return 0;
74}
75
76int
77cmd_searchpath_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
78{
79 int rc = 0;
80
81 if (yo->searchdir_unset) {
82 ly_ctx_unset_searchdir(*ctx, NULL);
83 } else if (!yo->searchdir_unset && !posv) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010084 /* no argument - print the paths */
85 const char * const *dirs = ly_ctx_get_searchdirs(*ctx);
86
87 printf("List of the searchpaths:\n");
Radek Krejci91b349d2021-04-07 11:50:45 +020088 for (uint32_t i = 0; dirs[i]; ++i) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010089 printf(" %s\n", dirs[i]);
90 }
aPieceka83b8e02023-06-07 15:25:16 +020091 } else {
92 rc = ly_ctx_set_searchdir(*ctx, posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +010093 }
94
aPieceka83b8e02023-06-07 15:25:16 +020095 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +010096}