Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_load.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 'load' 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> |
| 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.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_load_help(void) |
| 35 | { |
| 36 | printf("Usage: load [-i] <module-name1>[@<revision>] [<module-name2>[@revision] ...]\n" |
| 37 | " Add a new module of the specified name, yanglint will find\n" |
aPiecek | 4195527 | 2023-05-10 16:10:17 +0200 | [diff] [blame] | 38 | " them in searchpaths. If the <revision> of the module not\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 39 | " specified, the latest revision available is loaded.\n\n" |
| 40 | " -F FEATURES, --features=FEATURES\n" |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 41 | " Features to support, default all in all implemented modules.\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 42 | " <modname>:[<feature>,]*\n" |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 43 | " -i, --make-implemented\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 44 | " Make the imported modules \"referenced\" from any loaded\n" |
| 45 | " <schema> module also implemented. If specified a second time,\n" |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 46 | " all the modules are set implemented.\n" |
| 47 | " -X, --extended-leafref\n" |
| 48 | " Allow usage of deref() XPath function within leafref.\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 49 | } |
| 50 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 51 | int |
| 52 | cmd_load_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 53 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 54 | int rc, argc = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 55 | int opt, opt_index; |
| 56 | struct option options[] = { |
| 57 | {"features", required_argument, NULL, 'F'}, |
| 58 | {"help", no_argument, NULL, 'h'}, |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 59 | {"make-implemented", no_argument, NULL, 'i'}, |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 60 | {"extended-leafref", no_argument, NULL, 'X'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 61 | {NULL, 0, NULL, 0} |
| 62 | }; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 63 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 64 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 65 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 66 | } |
| 67 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 68 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_LOAD].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 69 | switch (opt) { |
| 70 | case 'F': /* --features */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 71 | if (parse_features(optarg, &yo->schema_features)) { |
| 72 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 73 | } |
| 74 | break; |
| 75 | |
| 76 | case 'h': |
| 77 | cmd_load_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 78 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 79 | |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 80 | case 'i': /* --make-implemented */ |
aPiecek | 7f22c10 | 2023-06-09 09:48:44 +0200 | [diff] [blame] | 81 | yo_opt_update_make_implemented(yo); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 82 | break; |
| 83 | |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 84 | case 'X': /* --extended-leafref */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 85 | yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED; |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 86 | break; |
| 87 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 88 | default: |
| 89 | YLMSG_E("Unknown option.\n"); |
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 | } |
| 92 | } |
| 93 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 94 | *posv = &yo->argv[optind]; |
| 95 | *posc = argc - optind; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | cmd_load_dep(struct yl_opt *yo, int posc) |
| 102 | { |
| 103 | if (yo->interactive && !posc) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 104 | /* no argument */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 105 | cmd_load_help(); |
| 106 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 107 | } |
| 108 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 109 | if (!yo->schema_features.count) { |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 110 | /* no features, enable all of them */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 111 | yo->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES; |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 112 | } |
| 113 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int |
| 118 | cmd_load_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
| 119 | { |
| 120 | const char *all_features[] = {"*", NULL}; |
| 121 | char *revision; |
| 122 | const char **features = NULL; |
| 123 | |
| 124 | assert(posv); |
| 125 | |
| 126 | if (yo->ctx_options) { |
| 127 | ly_ctx_set_options(*ctx, yo->ctx_options); |
| 128 | yo->ctx_options = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 129 | } |
| 130 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 131 | /* get revision */ |
| 132 | revision = strchr(posv, '@'); |
| 133 | if (revision) { |
| 134 | revision[0] = '\0'; |
| 135 | ++revision; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 136 | } |
| 137 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 138 | /* get features list for this module */ |
| 139 | if (!yo->schema_features.count) { |
| 140 | features = all_features; |
| 141 | } else { |
| 142 | get_features(&yo->schema_features, posv, &features); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 143 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 144 | |
| 145 | /* load the module */ |
| 146 | if (!ly_ctx_load_module(*ctx, posv, revision, features)) { |
| 147 | /* libyang printed the error messages */ |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | return 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 152 | } |