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 | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 16 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 17 | #include <stdint.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 20 | #include <string.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 21 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 22 | #include "libyang.h" |
| 23 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 24 | #include "cmd.h" |
| 25 | #include "common.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 26 | #include "completion.h" |
| 27 | #include "configuration.h" |
| 28 | #include "linenoise/linenoise.h" |
| 29 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 30 | int done; |
| 31 | struct ly_ctx *ctx = NULL; |
| 32 | |
| 33 | /* main_ni.c */ |
| 34 | int main_ni(int argc, char *argv[]); |
| 35 | |
| 36 | int |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 37 | main(int argc, char *argv[]) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 38 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 39 | char *cmdline; |
| 40 | int cmdlen; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 41 | |
| 42 | if (argc > 1) { |
| 43 | /* run in non-interactive mode */ |
| 44 | return main_ni(argc, argv); |
| 45 | } |
| 46 | |
| 47 | /* continue in interactive mode */ |
| 48 | linenoiseSetCompletionCallback(complete_cmd); |
| 49 | load_config(); |
| 50 | |
| 51 | if (ly_ctx_new(NULL, 0, &ctx)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 52 | YLMSG_E("Failed to create context.\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | while (!done) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 57 | uint8_t executed = 0; |
| 58 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 59 | /* get the command from user */ |
| 60 | cmdline = linenoise(PROMPT); |
| 61 | |
| 62 | /* EOF -> exit */ |
| 63 | if (cmdline == NULL) { |
| 64 | done = 1; |
| 65 | cmdline = strdup("quit"); |
| 66 | } |
| 67 | |
| 68 | /* empty line -> wait for another command */ |
| 69 | if (*cmdline == '\0') { |
| 70 | free(cmdline); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | /* isolate the command word. */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 75 | for (cmdlen = 0; cmdline[cmdlen] && (cmdline[cmdlen] != ' '); cmdlen++) {} |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 76 | |
| 77 | /* execute the command if any valid specified */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 78 | for (uint16_t i = 0; commands[i].name; i++) { |
| 79 | if (strncmp(cmdline, commands[i].name, (size_t)cmdlen) || (commands[i].name[cmdlen] != '\0')) { |
| 80 | continue; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 81 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 82 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 83 | commands[i].func(&ctx, cmdline); |
| 84 | executed = 1; |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | if (!executed) { |
| 89 | /* if unknown command specified, tell it to user */ |
| 90 | YLMSG_E("Unknown command \"%.*s\", type 'help' for more information.\n", cmdlen, cmdline); |
| 91 | } |
| 92 | |
| 93 | linenoiseHistoryAdd(cmdline); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 94 | free(cmdline); |
| 95 | } |
| 96 | |
| 97 | store_config(); |
| 98 | ly_ctx_destroy(ctx, NULL); |
| 99 | |
| 100 | return 0; |
| 101 | } |