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