blob: ebbd7f8ad479b57c6f06fdbbe130955866e502e3 [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 *
Radek Krejcie9f13b12020-11-09 17:42:04 +01006 * Copyright (c) 2015-2020 CESNET, z.s.p.o.
Radek Krejcied5acc52019-04-25 15:57:04 +02007 *
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
Radek Krejcie9f13b12020-11-09 17:42:04 +010017#include <stdint.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020018#include <stdio.h>
19#include <stdlib.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020020#include <string.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020021
Radek Krejcied5acc52019-04-25 15:57:04 +020022#include "libyang.h"
23
Radek Krejcie9f13b12020-11-09 17:42:04 +010024#include "cmd.h"
25#include "common.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "completion.h"
27#include "configuration.h"
28#include "linenoise/linenoise.h"
29
Radek Krejcied5acc52019-04-25 15:57:04 +020030int done;
31struct ly_ctx *ctx = NULL;
32
33/* main_ni.c */
34int main_ni(int argc, char *argv[]);
35
36int
Radek Krejcie9f13b12020-11-09 17:42:04 +010037main(int argc, char *argv[])
Radek Krejcied5acc52019-04-25 15:57:04 +020038{
Radek Krejcie9f13b12020-11-09 17:42:04 +010039 char *cmdline;
40 int cmdlen;
Radek Krejcied5acc52019-04-25 15:57:04 +020041
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 Krejcie9f13b12020-11-09 17:42:04 +010052 YLMSG_E("Failed to create context.\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020053 return 1;
54 }
55
56 while (!done) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010057 uint8_t executed = 0;
58
Radek Krejcied5acc52019-04-25 15:57:04 +020059 /* 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 Krejcie9f13b12020-11-09 17:42:04 +010075 for (cmdlen = 0; cmdline[cmdlen] && (cmdline[cmdlen] != ' '); cmdlen++) {}
Radek Krejcied5acc52019-04-25 15:57:04 +020076
77 /* execute the command if any valid specified */
Radek Krejcie9f13b12020-11-09 17:42:04 +010078 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 Krejcied5acc52019-04-25 15:57:04 +020081 }
Radek Krejcied5acc52019-04-25 15:57:04 +020082
Radek Krejcie9f13b12020-11-09 17:42:04 +010083 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 Krejcied5acc52019-04-25 15:57:04 +020094 free(cmdline);
95 }
96
97 store_config();
98 ly_ctx_destroy(ctx, NULL);
99
100 return 0;
101}