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 | * |
| 6 | * Copyright (c) 2015-2017 CESNET, z.s.p.o. |
| 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 | |
| 15 | #include "config.h" |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include "commands.h" |
| 24 | #include "configuration.h" |
| 25 | #include "completion.h" |
| 26 | #include "./linenoise/linenoise.h" |
| 27 | #include "libyang.h" |
| 28 | |
| 29 | int done; |
| 30 | struct ly_ctx *ctx = NULL; |
| 31 | |
| 32 | /* main_ni.c */ |
| 33 | int main_ni(int argc, char *argv[]); |
| 34 | |
| 35 | int |
| 36 | main(int argc, char* argv[]) |
| 37 | { |
| 38 | char *cmd, *cmdline, *cmdstart; |
| 39 | int i, j; |
| 40 | |
| 41 | if (argc > 1) { |
| 42 | /* run in non-interactive mode */ |
| 43 | return main_ni(argc, argv); |
| 44 | } |
| 45 | |
| 46 | /* continue in interactive mode */ |
| 47 | linenoiseSetCompletionCallback(complete_cmd); |
| 48 | load_config(); |
| 49 | |
| 50 | if (ly_ctx_new(NULL, 0, &ctx)) { |
| 51 | fprintf(stderr, "Failed to create context.\n"); |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | while (!done) { |
| 56 | /* get the command from user */ |
| 57 | cmdline = linenoise(PROMPT); |
| 58 | |
| 59 | /* EOF -> exit */ |
| 60 | if (cmdline == NULL) { |
| 61 | done = 1; |
| 62 | cmdline = strdup("quit"); |
| 63 | } |
| 64 | |
| 65 | /* empty line -> wait for another command */ |
| 66 | if (*cmdline == '\0') { |
| 67 | free(cmdline); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | /* isolate the command word. */ |
| 72 | for (i = 0; cmdline[i] && (cmdline[i] == ' '); i++); |
| 73 | cmdstart = cmdline + i; |
| 74 | for (j = 0; cmdline[i] && (cmdline[i] != ' '); i++, j++); |
| 75 | cmd = strndup(cmdstart, j); |
| 76 | |
| 77 | /* parse the command line */ |
| 78 | for (i = 0; commands[i].name; i++) { |
| 79 | if (strcmp(cmd, commands[i].name) == 0) { |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* execute the command if any valid specified */ |
| 85 | if (commands[i].name) { |
| 86 | /* display help */ |
| 87 | if ((strchr(cmdstart, ' ') != NULL) && ((strncmp(strchr(cmdstart, ' ')+1, "-h", 2) == 0) |
| 88 | || (strncmp(strchr(cmdstart, ' ')+1, "--help", 6) == 0))) { |
| 89 | if (commands[i].help_func != NULL) { |
| 90 | commands[i].help_func(); |
| 91 | } else { |
| 92 | printf("%s\n", commands[i].helpstring); |
| 93 | } |
| 94 | } else { |
| 95 | commands[i].func((const char *)cmdstart); |
| 96 | } |
| 97 | } else { |
| 98 | /* if unknown command specified, tell it to user */ |
| 99 | fprintf(stderr, "%s: no such command, type 'help' for more information.\n", cmd); |
| 100 | } |
| 101 | linenoiseHistoryAdd(cmdline); |
| 102 | |
| 103 | free(cmd); |
| 104 | free(cmdline); |
| 105 | } |
| 106 | |
| 107 | store_config(); |
| 108 | ly_ctx_destroy(ctx, NULL); |
| 109 | |
| 110 | return 0; |
| 111 | } |