blob: 310b822e3643c6a61d0b7e08c00fb41c53e82f75 [file] [log] [blame]
Radek Krejcied5acc52019-04-25 15:57:04 +02001/**
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
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejcied5acc52019-04-25 15:57:04 +020016
17#include <stdio.h>
18#include <stdlib.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020019#include <string.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020020
Radek Krejcied5acc52019-04-25 15:57:04 +020021#include "libyang.h"
22
Michal Vasko5aa44c02020-06-29 11:47:02 +020023#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "tools/config.h"
25
26#include "commands.h"
27#include "completion.h"
28#include "configuration.h"
29#include "linenoise/linenoise.h"
30
31
Radek Krejcied5acc52019-04-25 15:57:04 +020032int done;
33struct ly_ctx *ctx = NULL;
34
35/* main_ni.c */
36int main_ni(int argc, char *argv[]);
37
38int
39main(int argc, char* argv[])
40{
41 char *cmd, *cmdline, *cmdstart;
42 int i, j;
43
44 if (argc > 1) {
45 /* run in non-interactive mode */
46 return main_ni(argc, argv);
47 }
48
49 /* continue in interactive mode */
50 linenoiseSetCompletionCallback(complete_cmd);
51 load_config();
52
53 if (ly_ctx_new(NULL, 0, &ctx)) {
54 fprintf(stderr, "Failed to create context.\n");
55 return 1;
56 }
57
58 while (!done) {
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. */
75 for (i = 0; cmdline[i] && (cmdline[i] == ' '); i++);
76 cmdstart = cmdline + i;
77 for (j = 0; cmdline[i] && (cmdline[i] != ' '); i++, j++);
78 cmd = strndup(cmdstart, j);
79
80 /* parse the command line */
81 for (i = 0; commands[i].name; i++) {
82 if (strcmp(cmd, commands[i].name) == 0) {
83 break;
84 }
85 }
86
87 /* execute the command if any valid specified */
88 if (commands[i].name) {
89 /* display help */
90 if ((strchr(cmdstart, ' ') != NULL) && ((strncmp(strchr(cmdstart, ' ')+1, "-h", 2) == 0)
91 || (strncmp(strchr(cmdstart, ' ')+1, "--help", 6) == 0))) {
92 if (commands[i].help_func != NULL) {
93 commands[i].help_func();
94 } else {
95 printf("%s\n", commands[i].helpstring);
96 }
97 } else {
98 commands[i].func((const char *)cmdstart);
99 }
100 } else {
101 /* if unknown command specified, tell it to user */
102 fprintf(stderr, "%s: no such command, type 'help' for more information.\n", cmd);
103 }
104 linenoiseHistoryAdd(cmdline);
105
106 free(cmd);
107 free(cmdline);
108 }
109
110 store_config();
111 ly_ctx_destroy(ctx, NULL);
112
113 return 0;
114}