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