Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file main.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang's yanglint tool |
| 5 | * |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 6 | * Copyright (c) 2015-2020 CESNET, z.s.p.o. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 16 | #define _POSIX_C_SOURCE 200809L /* strdup */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 17 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 18 | #include <stdint.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 21 | #include <string.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 22 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 23 | #include "libyang.h" |
| 24 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 25 | #include "cmd.h" |
| 26 | #include "common.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "completion.h" |
| 28 | #include "configuration.h" |
| 29 | #include "linenoise/linenoise.h" |
| 30 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 31 | int done; |
| 32 | struct ly_ctx *ctx = NULL; |
| 33 | |
| 34 | /* main_ni.c */ |
| 35 | int main_ni(int argc, char *argv[]); |
| 36 | |
| 37 | int |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 38 | main(int argc, char *argv[]) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 39 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 40 | char *cmdline; |
| 41 | int cmdlen; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 42 | |
| 43 | if (argc > 1) { |
| 44 | /* run in non-interactive mode */ |
| 45 | return main_ni(argc, argv); |
| 46 | } |
| 47 | |
| 48 | /* continue in interactive mode */ |
| 49 | linenoiseSetCompletionCallback(complete_cmd); |
| 50 | load_config(); |
| 51 | |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 52 | if (ly_ctx_new(NULL, YL_DEFAULT_CTX_OPTIONS, &ctx)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 53 | YLMSG_E("Failed to create context.\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | while (!done) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 58 | uint8_t executed = 0; |
| 59 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 60 | /* get the command from user */ |
| 61 | cmdline = linenoise(PROMPT); |
| 62 | |
| 63 | /* EOF -> exit */ |
| 64 | if (cmdline == NULL) { |
| 65 | done = 1; |
| 66 | cmdline = strdup("quit"); |
| 67 | } |
| 68 | |
| 69 | /* empty line -> wait for another command */ |
| 70 | if (*cmdline == '\0') { |
| 71 | free(cmdline); |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | /* isolate the command word. */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 76 | for (cmdlen = 0; cmdline[cmdlen] && (cmdline[cmdlen] != ' '); cmdlen++) {} |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 77 | |
| 78 | /* execute the command if any valid specified */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 79 | for (uint16_t i = 0; commands[i].name; i++) { |
| 80 | if (strncmp(cmdline, commands[i].name, (size_t)cmdlen) || (commands[i].name[cmdlen] != '\0')) { |
| 81 | continue; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 82 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 83 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 84 | commands[i].func(&ctx, cmdline); |
| 85 | executed = 1; |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | if (!executed) { |
| 90 | /* if unknown command specified, tell it to user */ |
| 91 | YLMSG_E("Unknown command \"%.*s\", type 'help' for more information.\n", cmdlen, cmdline); |
| 92 | } |
| 93 | |
| 94 | linenoiseHistoryAdd(cmdline); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 95 | free(cmdline); |
| 96 | } |
| 97 | |
| 98 | store_config(); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame^] | 99 | ly_ctx_destroy(ctx); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |