Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_add.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 'add' 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 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 21 | #include <assert.h> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 22 | #include <getopt.h> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #include "libyang.h" |
| 28 | |
| 29 | #include "common.h" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 30 | #include "yl_opt.h" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 31 | |
| 32 | void |
| 33 | cmd_add_help(void) |
| 34 | { |
| 35 | printf("Usage: add [-iD] <schema1> [<schema2> ...]\n" |
| 36 | " Add a new module from a specific file.\n\n" |
| 37 | " -D, --disable-searchdir\n" |
| 38 | " Do not implicitly search in current working directory for\n" |
| 39 | " the import schema modules. If specified a second time, do not\n" |
| 40 | " even search in the module directory (all modules must be \n" |
| 41 | " explicitly specified).\n" |
| 42 | " -F FEATURES, --features=FEATURES\n" |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 43 | " Features to support, default all in all implemented modules.\n" |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 44 | " Specify separately for each module.\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 45 | " <modname>:[<feature>,]*\n" |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 46 | " -i, --make-implemented\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 47 | " Make the imported modules \"referenced\" from any loaded\n" |
| 48 | " <schema> module also implemented. If specified a second time,\n" |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 49 | " all the modules are set implemented.\n" |
| 50 | " -X, --extended-leafref\n" |
| 51 | " Allow usage of deref() XPath function within leafref.\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 52 | } |
| 53 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 54 | int |
| 55 | cmd_add_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 56 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 57 | int rc = 0, argc = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 58 | int opt, opt_index; |
| 59 | struct option options[] = { |
| 60 | {"disable-searchdir", no_argument, NULL, 'D'}, |
| 61 | {"features", required_argument, NULL, 'F'}, |
| 62 | {"help", no_argument, NULL, 'h'}, |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 63 | {"make-implemented", no_argument, NULL, 'i'}, |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 64 | {"extended-leafref", no_argument, NULL, 'X'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 65 | {NULL, 0, NULL, 0} |
| 66 | }; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 67 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 68 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 69 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 70 | } |
| 71 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 72 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_ADD].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 73 | switch (opt) { |
| 74 | case 'D': /* --disable--search */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 75 | if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 76 | YLMSG_W("The -D option specified too many times.\n"); |
| 77 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 78 | if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIR_CWD) { |
| 79 | yo->ctx_options &= ~LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 80 | yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIRS; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 81 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 82 | yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 83 | } |
| 84 | break; |
| 85 | |
| 86 | case 'F': /* --features */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 87 | if (parse_features(optarg, &yo->schema_features)) { |
| 88 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 89 | } |
| 90 | break; |
| 91 | |
| 92 | case 'h': |
| 93 | cmd_add_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 94 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 95 | |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 96 | case 'i': /* --make-implemented */ |
aPiecek | 7f22c10 | 2023-06-09 09:48:44 +0200 | [diff] [blame^] | 97 | yo_opt_update_make_implemented(yo); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 98 | break; |
| 99 | |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 100 | case 'X': /* --extended-leafref */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 101 | yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED; |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 102 | break; |
| 103 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 104 | default: |
| 105 | YLMSG_E("Unknown option.\n"); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 106 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 110 | *posv = &yo->argv[optind]; |
| 111 | *posc = argc - optind; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int |
| 117 | cmd_add_dep(struct yl_opt *yo, int posc) |
| 118 | { |
| 119 | if (yo->interactive && !posc) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 120 | /* no argument */ |
| 121 | cmd_add_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 122 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 123 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 124 | if (!yo->schema_features.count) { |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 125 | /* no features, enable all of them */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 126 | yo->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES; |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 127 | } |
| 128 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | int |
| 133 | cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
| 134 | { |
| 135 | const char *all_features[] = {"*", NULL}; |
| 136 | LY_ERR ret; |
| 137 | uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */ |
| 138 | char *dir, *module; |
| 139 | const char **features = NULL; |
| 140 | struct ly_in *in = NULL; |
| 141 | |
| 142 | assert(posv); |
| 143 | |
| 144 | if (yo->ctx_options) { |
| 145 | ly_ctx_set_options(*ctx, yo->ctx_options); |
| 146 | yo->ctx_options = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 147 | } |
| 148 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 149 | if (parse_schema_path(posv, &dir, &module)) { |
| 150 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 151 | } |
| 152 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 153 | /* add temporarily also the path of the module itself */ |
| 154 | if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) { |
| 155 | path_unset = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 156 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 157 | |
| 158 | /* get features list for this module */ |
| 159 | if (!yo->schema_features.count) { |
| 160 | features = all_features; |
| 161 | } else { |
| 162 | get_features(&yo->schema_features, module, &features); |
| 163 | } |
| 164 | |
| 165 | /* temporary cleanup */ |
| 166 | free(dir); |
| 167 | free(module); |
| 168 | |
| 169 | /* prepare input handler */ |
| 170 | ret = ly_in_new_filepath(posv, 0, &in); |
| 171 | if (ret) { |
| 172 | return 1; |
| 173 | } |
| 174 | |
| 175 | /* parse the file */ |
| 176 | ret = lys_parse(*ctx, in, LYS_IN_UNKNOWN, features, NULL); |
| 177 | ly_in_free(in, 1); |
| 178 | ly_ctx_unset_searchdir_last(*ctx, path_unset); |
| 179 | |
| 180 | return ret; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 181 | } |