Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_searchpath.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 5 | * @author Adam Piecek <piecek@cesnet.cz> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 6 | * @brief 'searchpath' command of the libyang's yanglint tool. |
| 7 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 8 | * Copyright (c) 2015-2023 CESNET, z.s.p.o. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 9 | * |
| 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" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 28 | #include "yl_opt.h" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 29 | |
| 30 | void |
| 31 | cmd_searchpath_help(void) |
| 32 | { |
| 33 | printf("Usage: searchpath [--clear] [<modules-dir-path> ...]\n" |
aPiecek | 4f306da | 2023-03-27 09:06:07 +0200 | [diff] [blame] | 34 | " 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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 39 | } |
| 40 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 41 | int |
| 42 | cmd_searchpath_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 43 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 44 | int rc = 0, argc = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 45 | 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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 51 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 52 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 53 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 54 | } |
| 55 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 56 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_SEARCHPATH].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 57 | switch (opt) { |
| 58 | case 'c': |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 59 | yo->searchdir_unset = 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 60 | break; |
| 61 | case 'h': |
| 62 | cmd_searchpath_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 63 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 64 | default: |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 65 | YLMSG_E("Unknown option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 66 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 70 | *posv = &yo->argv[optind]; |
| 71 | *posc = argc - optind; |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | cmd_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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 84 | /* no argument - print the paths */ |
| 85 | const char * const *dirs = ly_ctx_get_searchdirs(*ctx); |
| 86 | |
| 87 | printf("List of the searchpaths:\n"); |
Radek Krejci | 91b349d | 2021-04-07 11:50:45 +0200 | [diff] [blame] | 88 | for (uint32_t i = 0; dirs[i]; ++i) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 89 | printf(" %s\n", dirs[i]); |
| 90 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 91 | } else { |
| 92 | rc = ly_ctx_set_searchdir(*ctx, posv); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 93 | } |
| 94 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 95 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 96 | } |