Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file main.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 4 | * @author Adam Piecek <piecek@cesnet.cz> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 5 | * @brief libyang's yanglint tool |
| 6 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015-2023 CESNET, z.s.p.o. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 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 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 17 | #define _POSIX_C_SOURCE 200809L /* strdup */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 18 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 19 | #include <stdint.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 22 | #include <string.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 23 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 24 | #include "libyang.h" |
| 25 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 26 | #include "cmd.h" |
| 27 | #include "common.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | #include "completion.h" |
| 29 | #include "configuration.h" |
| 30 | #include "linenoise/linenoise.h" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 31 | #include "yl_opt.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 32 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 33 | int done; |
| 34 | struct ly_ctx *ctx = NULL; |
| 35 | |
| 36 | /* main_ni.c */ |
| 37 | int main_ni(int argc, char *argv[]); |
| 38 | |
| 39 | int |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 40 | main(int argc, char *argv[]) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 41 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 42 | int cmdlen, posc, i, j; |
| 43 | struct yl_opt yo = {0}; |
| 44 | char *empty = NULL, *cmdline; |
| 45 | char **posv; |
| 46 | uint8_t cmd_found; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 47 | |
| 48 | if (argc > 1) { |
| 49 | /* run in non-interactive mode */ |
| 50 | return main_ni(argc, argv); |
| 51 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 52 | yo.interactive = 1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 53 | |
| 54 | /* continue in interactive mode */ |
| 55 | linenoiseSetCompletionCallback(complete_cmd); |
| 56 | load_config(); |
| 57 | |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 58 | if (ly_ctx_new(NULL, YL_DEFAULT_CTX_OPTIONS, &ctx)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 59 | YLMSG_E("Failed to create context."); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | while (!done) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 64 | cmd_found = 0; |
| 65 | |
| 66 | posv = ∅ |
| 67 | posc = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 68 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 69 | /* get the command from user */ |
| 70 | cmdline = linenoise(PROMPT); |
| 71 | |
| 72 | /* EOF -> exit */ |
| 73 | if (cmdline == NULL) { |
| 74 | done = 1; |
| 75 | cmdline = strdup("quit"); |
| 76 | } |
| 77 | |
| 78 | /* empty line -> wait for another command */ |
| 79 | if (*cmdline == '\0') { |
| 80 | free(cmdline); |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | /* isolate the command word. */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 85 | for (cmdlen = 0; cmdline[cmdlen] && (cmdline[cmdlen] != ' '); cmdlen++) {} |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 86 | |
| 87 | /* execute the command if any valid specified */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 88 | for (i = 0; commands[i].name; i++) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 89 | if (strncmp(cmdline, commands[i].name, (size_t)cmdlen) || (commands[i].name[cmdlen] != '\0')) { |
| 90 | continue; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 91 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 92 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 93 | cmd_found = 1; |
| 94 | if (commands[i].opt_func && commands[i].opt_func(&yo, cmdline, &posv, &posc)) { |
| 95 | break; |
| 96 | } |
| 97 | if (commands[i].dep_func && commands[i].dep_func(&yo, posc)) { |
| 98 | break; |
| 99 | } |
| 100 | if (posc) { |
| 101 | for (j = 0; j < posc; j++) { |
| 102 | yo.last_one = (j + 1) == posc; |
| 103 | if (commands[i].exec_func(&ctx, &yo, posv[j])) { |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } else { |
| 108 | commands[i].exec_func(&ctx, &yo, NULL); |
| 109 | } |
| 110 | if (commands[i].fin_func) { |
| 111 | commands[i].fin_func(ctx, &yo); |
| 112 | } |
| 113 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 117 | if (!cmd_found) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 118 | /* if unknown command specified, tell it to user */ |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 119 | YLMSG_E("Unknown command \"%.*s\", type 'help' for more information.", cmdlen, cmdline); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | linenoiseHistoryAdd(cmdline); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 123 | free(cmdline); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 124 | yl_opt_erase(&yo); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 125 | } |
| 126 | |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 127 | /* Global variables in commands are freed. */ |
| 128 | cmd_free(); |
| 129 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 130 | store_config(); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 131 | ly_ctx_destroy(ctx); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |