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