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> |
| 5 | * @brief 'load' command of the libyang's yanglint tool. |
| 6 | * |
| 7 | * Copyright (c) 2015-2020 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include "cmd.h" |
| 19 | |
| 20 | #include <getopt.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "libyang.h" |
| 26 | |
| 27 | #include "common.h" |
| 28 | |
| 29 | void |
| 30 | cmd_load_help(void) |
| 31 | { |
| 32 | printf("Usage: load [-i] <module-name1>[@<revision>] [<module-name2>[@revision] ...]\n" |
| 33 | " Add a new module of the specified name, yanglint will find\n" |
aPiecek | 4195527 | 2023-05-10 16:10:17 +0200 | [diff] [blame^] | 34 | " them in searchpaths. If the <revision> of the module not\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 35 | " specified, the latest revision available is loaded.\n\n" |
| 36 | " -F FEATURES, --features=FEATURES\n" |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 37 | " Features to support, default all in all implemented modules.\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 38 | " <modname>:[<feature>,]*\n" |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 39 | " -i, --make-implemented\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 40 | " Make the imported modules \"referenced\" from any loaded\n" |
| 41 | " <schema> module also implemented. If specified a second time,\n" |
| 42 | " all the modules are set implemented.\n"); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | cmd_load(struct ly_ctx **ctx, const char *cmdline) |
| 47 | { |
| 48 | int argc = 0; |
| 49 | char **argv = NULL; |
| 50 | int opt, opt_index; |
| 51 | struct option options[] = { |
| 52 | {"features", required_argument, NULL, 'F'}, |
| 53 | {"help", no_argument, NULL, 'h'}, |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 54 | {"make-implemented", no_argument, NULL, 'i'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 55 | {NULL, 0, NULL, 0} |
| 56 | }; |
| 57 | uint16_t options_ctx = 0; |
Michal Vasko | 584b0c4 | 2022-09-14 10:20:52 +0200 | [diff] [blame] | 58 | const char *all_features[] = {"*", NULL}; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 59 | struct ly_set fset = {0}; |
| 60 | |
| 61 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 62 | goto cleanup; |
| 63 | } |
| 64 | |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 65 | while ((opt = getopt_long(argc, argv, commands[CMD_LOAD].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 66 | switch (opt) { |
| 67 | case 'F': /* --features */ |
| 68 | if (parse_features(optarg, &fset)) { |
| 69 | goto cleanup; |
| 70 | } |
| 71 | break; |
| 72 | |
| 73 | case 'h': |
| 74 | cmd_load_help(); |
| 75 | goto cleanup; |
| 76 | |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 77 | case 'i': /* --make-implemented */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 78 | if (options_ctx & LY_CTX_REF_IMPLEMENTED) { |
| 79 | options_ctx &= ~LY_CTX_REF_IMPLEMENTED; |
| 80 | options_ctx |= LY_CTX_ALL_IMPLEMENTED; |
| 81 | } else { |
| 82 | options_ctx |= LY_CTX_REF_IMPLEMENTED; |
| 83 | } |
| 84 | break; |
| 85 | |
| 86 | default: |
| 87 | YLMSG_E("Unknown option.\n"); |
| 88 | goto cleanup; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (argc == optind) { |
| 93 | /* no argument */ |
| 94 | cmd_add_help(); |
| 95 | goto cleanup; |
| 96 | } |
| 97 | |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 98 | if (!fset.count) { |
| 99 | /* no features, enable all of them */ |
| 100 | options_ctx |= LY_CTX_ENABLE_IMP_FEATURES; |
| 101 | } |
| 102 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 103 | if (options_ctx) { |
| 104 | ly_ctx_set_options(*ctx, options_ctx); |
| 105 | } |
| 106 | |
| 107 | for (int i = 0; i < argc - optind; i++) { |
| 108 | /* process the schema module files */ |
| 109 | char *revision; |
| 110 | const char **features = NULL; |
| 111 | |
| 112 | /* get revision */ |
| 113 | revision = strchr(argv[optind + i], '@'); |
| 114 | if (revision) { |
| 115 | revision[0] = '\0'; |
| 116 | ++revision; |
| 117 | } |
| 118 | |
| 119 | /* get features list for this module */ |
Michal Vasko | 584b0c4 | 2022-09-14 10:20:52 +0200 | [diff] [blame] | 120 | if (!fset.count) { |
| 121 | features = all_features; |
| 122 | } else { |
| 123 | get_features(&fset, argv[optind + i], &features); |
| 124 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 125 | |
| 126 | /* load the module */ |
| 127 | if (!ly_ctx_load_module(*ctx, argv[optind + i], revision, features)) { |
| 128 | /* libyang printed the error messages */ |
| 129 | goto cleanup; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | cleanup: |
| 134 | if (options_ctx) { |
| 135 | ly_ctx_unset_options(*ctx, options_ctx); |
| 136 | } |
| 137 | ly_set_erase(&fset, free_features); |
| 138 | free_cmdline(argv); |
| 139 | } |