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 | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 80 | if (yo->ctx_options & LY_CTX_REF_IMPLEMENTED) { |
| 81 | yo->ctx_options &= ~LY_CTX_REF_IMPLEMENTED; |
| 82 | yo->ctx_options |= LY_CTX_ALL_IMPLEMENTED; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 83 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 84 | yo->ctx_options |= LY_CTX_REF_IMPLEMENTED; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 85 | } |
| 86 | break; |
| 87 | |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 88 | case 'X': /* --extended-leafref */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 89 | yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED; |
aPiecek | 88030e5 | 2023-06-02 11:03:57 +0200 | [diff] [blame] | 90 | break; |
| 91 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 92 | default: |
| 93 | YLMSG_E("Unknown option.\n"); |
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 | } |
| 96 | } |
| 97 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 98 | *posv = &yo->argv[optind]; |
| 99 | *posc = argc - optind; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | int |
| 105 | cmd_load_dep(struct yl_opt *yo, int posc) |
| 106 | { |
| 107 | if (yo->interactive && !posc) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 108 | /* no argument */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 109 | cmd_load_help(); |
| 110 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 111 | } |
| 112 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 113 | if (!yo->schema_features.count) { |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 114 | /* no features, enable all of them */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 115 | yo->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES; |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 116 | } |
| 117 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int |
| 122 | cmd_load_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
| 123 | { |
| 124 | const char *all_features[] = {"*", NULL}; |
| 125 | char *revision; |
| 126 | const char **features = NULL; |
| 127 | |
| 128 | assert(posv); |
| 129 | |
| 130 | if (yo->ctx_options) { |
| 131 | ly_ctx_set_options(*ctx, yo->ctx_options); |
| 132 | yo->ctx_options = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 133 | } |
| 134 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 135 | /* get revision */ |
| 136 | revision = strchr(posv, '@'); |
| 137 | if (revision) { |
| 138 | revision[0] = '\0'; |
| 139 | ++revision; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 140 | } |
| 141 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 142 | /* get features list for this module */ |
| 143 | if (!yo->schema_features.count) { |
| 144 | features = all_features; |
| 145 | } else { |
| 146 | get_features(&yo->schema_features, posv, &features); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 147 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame^] | 148 | |
| 149 | /* load the module */ |
| 150 | if (!ly_ctx_load_module(*ctx, posv, revision, features)) { |
| 151 | /* libyang printed the error messages */ |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | return 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 156 | } |