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 | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 97 | if (yo->ctx_options & LY_CTX_REF_IMPLEMENTED) { |
| 98 | yo->ctx_options &= ~LY_CTX_REF_IMPLEMENTED; |
| 99 | yo->ctx_options |= LY_CTX_ALL_IMPLEMENTED; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 100 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 101 | yo->ctx_options |= LY_CTX_REF_IMPLEMENTED; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 102 | } |
| 103 | break; |
| 104 | |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 105 | case 'X': /* --extended-leafref */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 106 | yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED; |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 107 | break; |
| 108 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 109 | default: |
| 110 | YLMSG_E("Unknown option.\n"); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 111 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 115 | *posv = &yo->argv[optind]; |
| 116 | *posc = argc - optind; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int |
| 122 | cmd_add_dep(struct yl_opt *yo, int posc) |
| 123 | { |
| 124 | if (yo->interactive && !posc) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 125 | /* no argument */ |
| 126 | cmd_add_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 127 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 128 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 129 | if (!yo->schema_features.count) { |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 130 | /* no features, enable all of them */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 131 | yo->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES; |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 132 | } |
| 133 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | int |
| 138 | cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
| 139 | { |
| 140 | const char *all_features[] = {"*", NULL}; |
| 141 | LY_ERR ret; |
| 142 | uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */ |
| 143 | char *dir, *module; |
| 144 | const char **features = NULL; |
| 145 | struct ly_in *in = NULL; |
| 146 | |
| 147 | assert(posv); |
| 148 | |
| 149 | if (yo->ctx_options) { |
| 150 | ly_ctx_set_options(*ctx, yo->ctx_options); |
| 151 | yo->ctx_options = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 152 | } |
| 153 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 154 | if (parse_schema_path(posv, &dir, &module)) { |
| 155 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 156 | } |
| 157 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 158 | /* add temporarily also the path of the module itself */ |
| 159 | if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) { |
| 160 | path_unset = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 161 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 162 | |
| 163 | /* get features list for this module */ |
| 164 | if (!yo->schema_features.count) { |
| 165 | features = all_features; |
| 166 | } else { |
| 167 | get_features(&yo->schema_features, module, &features); |
| 168 | } |
| 169 | |
| 170 | /* temporary cleanup */ |
| 171 | free(dir); |
| 172 | free(module); |
| 173 | |
| 174 | /* prepare input handler */ |
| 175 | ret = ly_in_new_filepath(posv, 0, &in); |
| 176 | if (ret) { |
| 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | /* parse the file */ |
| 181 | ret = lys_parse(*ctx, in, LYS_IN_UNKNOWN, features, NULL); |
| 182 | ly_in_free(in, 1); |
| 183 | ly_ctx_unset_searchdir_last(*ctx, path_unset); |
| 184 | |
| 185 | return ret; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 186 | } |