forked from CESNET/libyang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
111 lines (94 loc) · 2.83 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* @file main.c
* @author Radek Krejci <rkrejci@cesnet.cz>
* @brief libyang's yanglint tool
*
* Copyright (c) 2015-2017 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include "compat.h"
#include "commands.h"
#include "configuration.h"
#include "completion.h"
#include "../../linenoise/linenoise.h"
#include "libyang.h"
int done;
struct ly_ctx *ctx = NULL;
/* main_ni.c */
int main_ni(int argc, char *argv[]);
int
main(int argc, char* argv[])
{
char *cmd, *cmdline, *cmdstart;
int i, j;
if (argc > 1) {
/* run in non-interactive mode */
return main_ni(argc, argv);
}
/* continue in interactive mode */
linenoiseSetCompletionCallback(complete_cmd);
load_config();
ctx = ly_ctx_new(NULL, 0);
if (!ctx) {
fprintf(stderr, "Failed to create context.\n");
return 1;
}
while (!done) {
/* get the command from user */
cmdline = linenoise(PROMPT);
/* EOF -> exit */
if (cmdline == NULL) {
done = 1;
cmdline = strdup("quit");
}
/* empty line -> wait for another command */
if (*cmdline == '\0') {
free(cmdline);
continue;
}
/* isolate the command word. */
for (i = 0; cmdline[i] && (cmdline[i] == ' '); i++);
cmdstart = cmdline + i;
for (j = 0; cmdline[i] && (cmdline[i] != ' '); i++, j++);
cmd = strndup(cmdstart, j);
/* parse the command line */
for (i = 0; commands[i].name; i++) {
if (strcmp(cmd, commands[i].name) == 0) {
break;
}
}
/* execute the command if any valid specified */
if (commands[i].name) {
/* display help */
if ((strchr(cmdstart, ' ') != NULL) && ((strncmp(strchr(cmdstart, ' ')+1, "-h", 2) == 0)
|| (strncmp(strchr(cmdstart, ' ')+1, "--help", 6) == 0))) {
if (commands[i].help_func != NULL) {
commands[i].help_func();
} else {
printf("%s\n", commands[i].helpstring);
}
} else {
commands[i].func((const char *)cmdstart);
}
} else {
/* if unknown command specified, tell it to user */
fprintf(stderr, "%s: no such command, type 'help' for more information.\n", cmd);
}
linenoiseHistoryAdd(cmdline);
free(cmd);
free(cmdline);
}
store_config();
ly_ctx_destroy(ctx, NULL);
return 0;
}