blob: 9f0d0279a2dfefd80ef5df868abacfbf4274d14c [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 Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup */
Radek Krejcied5acc52019-04-25 15:57:04 +020017
Radek Krejcie9f13b12020-11-09 17:42:04 +010018#include <stdint.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020019#include <stdio.h>
20#include <stdlib.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020021#include <string.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020022
Radek Krejcied5acc52019-04-25 15:57:04 +020023#include "libyang.h"
24
Radek Krejcie9f13b12020-11-09 17:42:04 +010025#include "cmd.h"
26#include "common.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "completion.h"
28#include "configuration.h"
29#include "linenoise/linenoise.h"
30
Radek Krejcied5acc52019-04-25 15:57:04 +020031int done;
32struct ly_ctx *ctx = NULL;
33
34/* main_ni.c */
35int main_ni(int argc, char *argv[]);
36
37int
Radek Krejcie9f13b12020-11-09 17:42:04 +010038main(int argc, char *argv[])
Radek Krejcied5acc52019-04-25 15:57:04 +020039{
Radek Krejcie9f13b12020-11-09 17:42:04 +010040 char *cmdline;
41 int cmdlen;
Radek Krejcied5acc52019-04-25 15:57:04 +020042
43 if (argc > 1) {
44 /* run in non-interactive mode */
45 return main_ni(argc, argv);
46 }
47
48 /* continue in interactive mode */
49 linenoiseSetCompletionCallback(complete_cmd);
50 load_config();
51
Michal Vaskoc431a0a2021-01-25 14:31:58 +010052 if (ly_ctx_new(NULL, YL_DEFAULT_CTX_OPTIONS, &ctx)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010053 YLMSG_E("Failed to create context.\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020054 return 1;
55 }
56
57 while (!done) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010058 uint8_t executed = 0;
59
Radek Krejcied5acc52019-04-25 15:57:04 +020060 /* get the command from user */
61 cmdline = linenoise(PROMPT);
62
63 /* EOF -> exit */
64 if (cmdline == NULL) {
65 done = 1;
66 cmdline = strdup("quit");
67 }
68
69 /* empty line -> wait for another command */
70 if (*cmdline == '\0') {
71 free(cmdline);
72 continue;
73 }
74
75 /* isolate the command word. */
Radek Krejcie9f13b12020-11-09 17:42:04 +010076 for (cmdlen = 0; cmdline[cmdlen] && (cmdline[cmdlen] != ' '); cmdlen++) {}
Radek Krejcied5acc52019-04-25 15:57:04 +020077
78 /* execute the command if any valid specified */
Radek Krejcie9f13b12020-11-09 17:42:04 +010079 for (uint16_t i = 0; commands[i].name; i++) {
80 if (strncmp(cmdline, commands[i].name, (size_t)cmdlen) || (commands[i].name[cmdlen] != '\0')) {
81 continue;
Radek Krejcied5acc52019-04-25 15:57:04 +020082 }
Radek Krejcied5acc52019-04-25 15:57:04 +020083
Radek Krejcie9f13b12020-11-09 17:42:04 +010084 commands[i].func(&ctx, cmdline);
85 executed = 1;
86 break;
87 }
88
89 if (!executed) {
90 /* if unknown command specified, tell it to user */
91 YLMSG_E("Unknown command \"%.*s\", type 'help' for more information.\n", cmdlen, cmdline);
92 }
93
94 linenoiseHistoryAdd(cmdline);
Radek Krejcied5acc52019-04-25 15:57:04 +020095 free(cmdline);
96 }
97
98 store_config();
Radek Krejci90ed21e2021-04-12 14:47:46 +020099 ly_ctx_destroy(ctx);
Radek Krejcied5acc52019-04-25 15:57:04 +0200100
101 return 0;
102}