blob: a94a343d86dcb91132cb3940ed7ad00b637d1c58 [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>
5 * @brief 'searchpath' command of the libyang's yanglint tool.
6 *
7 * Copyright (c) 2015-2020 CESNET, z.s.p.o.
8 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#define _GNU_SOURCE
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
28void
29cmd_searchpath_help(void)
30{
31 printf("Usage: searchpath [--clear] [<modules-dir-path> ...]\n"
32 " Set paths of directories where to search for imports and\n"
33 " includes of the schema modules. The current working directory\n"
34 " and the path of the module being added is used implicitly.\n"
35 " The 'load' command uses these paths to search even for the\n"
36 " schema modules to be loaded.\n");
37}
38
39void
40cmd_searchpath(struct ly_ctx **ctx, const char *cmdline)
41{
42 int argc = 0;
43 char **argv = NULL;
44 int opt, opt_index;
45 struct option options[] = {
46 {"clear", no_argument, NULL, 'c'},
47 {"help", no_argument, NULL, 'h'},
48 {NULL, 0, NULL, 0}
49 };
50 int8_t cleared = 0;
51
52 if (parse_cmdline(cmdline, &argc, &argv)) {
53 goto cleanup;
54 }
55
56 while ((opt = getopt_long(argc, argv, "ch", options, &opt_index)) != -1) {
57 switch (opt) {
58 case 'c':
59 ly_ctx_unset_searchdir(*ctx, NULL);
60 cleared = 1;
61 break;
62 case 'h':
63 cmd_searchpath_help();
64 goto cleanup;
65 default:
66 YLMSG_E("Unknown option.\n");
67 goto cleanup;
68 }
69 }
70
71 if (!cleared && (argc == optind)) {
72 /* no argument - print the paths */
73 const char * const *dirs = ly_ctx_get_searchdirs(*ctx);
74
75 printf("List of the searchpaths:\n");
76 for (uint32_t i = 0; dirs[0]; ++i) {
77 printf(" %s\n", dirs[i]);
78 }
79 goto cleanup;
80 }
81
82 for (int i = 0; i < argc - optind; i++) {
83 if (ly_ctx_set_searchdir(*ctx, argv[optind + i])) {
84 goto cleanup;
85 }
86 }
87
88cleanup:
89 free_cmdline(argv);
90}