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" |
| 34 | " them in searchpaths. if the <revision> of the module not\n" |
| 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; |
| 58 | struct ly_set fset = {0}; |
| 59 | |
| 60 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 61 | goto cleanup; |
| 62 | } |
| 63 | |
| 64 | while ((opt = getopt_long(argc, argv, "F:hi", options, &opt_index)) != -1) { |
| 65 | switch (opt) { |
| 66 | case 'F': /* --features */ |
| 67 | if (parse_features(optarg, &fset)) { |
| 68 | goto cleanup; |
| 69 | } |
| 70 | break; |
| 71 | |
| 72 | case 'h': |
| 73 | cmd_load_help(); |
| 74 | goto cleanup; |
| 75 | |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 76 | case 'i': /* --make-implemented */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 77 | if (options_ctx & LY_CTX_REF_IMPLEMENTED) { |
| 78 | options_ctx &= ~LY_CTX_REF_IMPLEMENTED; |
| 79 | options_ctx |= LY_CTX_ALL_IMPLEMENTED; |
| 80 | } else { |
| 81 | options_ctx |= LY_CTX_REF_IMPLEMENTED; |
| 82 | } |
| 83 | break; |
| 84 | |
| 85 | default: |
| 86 | YLMSG_E("Unknown option.\n"); |
| 87 | goto cleanup; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (argc == optind) { |
| 92 | /* no argument */ |
| 93 | cmd_add_help(); |
| 94 | goto cleanup; |
| 95 | } |
| 96 | |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 97 | if (!fset.count) { |
| 98 | /* no features, enable all of them */ |
| 99 | options_ctx |= LY_CTX_ENABLE_IMP_FEATURES; |
| 100 | } |
| 101 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 102 | if (options_ctx) { |
| 103 | ly_ctx_set_options(*ctx, options_ctx); |
| 104 | } |
| 105 | |
| 106 | for (int i = 0; i < argc - optind; i++) { |
| 107 | /* process the schema module files */ |
| 108 | char *revision; |
| 109 | const char **features = NULL; |
| 110 | |
| 111 | /* get revision */ |
| 112 | revision = strchr(argv[optind + i], '@'); |
| 113 | if (revision) { |
| 114 | revision[0] = '\0'; |
| 115 | ++revision; |
| 116 | } |
| 117 | |
| 118 | /* get features list for this module */ |
| 119 | get_features(&fset, argv[optind + i], &features); |
| 120 | |
| 121 | /* load the module */ |
| 122 | if (!ly_ctx_load_module(*ctx, argv[optind + i], revision, features)) { |
| 123 | /* libyang printed the error messages */ |
| 124 | goto cleanup; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | cleanup: |
| 129 | if (options_ctx) { |
| 130 | ly_ctx_unset_options(*ctx, options_ctx); |
| 131 | } |
| 132 | ly_set_erase(&fset, free_features); |
| 133 | free_cmdline(argv); |
| 134 | } |