blob: 43b90c8c8f9a5e4addadb2f3b06531a0097b2f93 [file] [log] [blame]
Radek Krejcied5acc52019-04-25 15:57:04 +02001/**
2 * @file main.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
aPieceka83b8e02023-06-07 15:25:16 +02004 * @author Adam Piecek <piecek@cesnet.cz>
Radek Krejcied5acc52019-04-25 15:57:04 +02005 * @brief libyang's yanglint tool
6 *
aPieceka83b8e02023-06-07 15:25:16 +02007 * Copyright (c) 2015-2023 CESNET, z.s.p.o.
Radek Krejcied5acc52019-04-25 15:57:04 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010017#define _POSIX_C_SOURCE 200809L /* strdup */
Radek Krejcied5acc52019-04-25 15:57:04 +020018
Radek Krejcie9f13b12020-11-09 17:42:04 +010019#include <stdint.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020020#include <stdio.h>
21#include <stdlib.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020022#include <string.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020023
Radek Krejcied5acc52019-04-25 15:57:04 +020024#include "libyang.h"
25
Radek Krejcie9f13b12020-11-09 17:42:04 +010026#include "cmd.h"
27#include "common.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "completion.h"
29#include "configuration.h"
30#include "linenoise/linenoise.h"
aPieceka83b8e02023-06-07 15:25:16 +020031#include "yl_opt.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032
Radek Krejcied5acc52019-04-25 15:57:04 +020033int done;
34struct ly_ctx *ctx = NULL;
35
36/* main_ni.c */
37int main_ni(int argc, char *argv[]);
38
39int
Radek Krejcie9f13b12020-11-09 17:42:04 +010040main(int argc, char *argv[])
Radek Krejcied5acc52019-04-25 15:57:04 +020041{
aPieceka83b8e02023-06-07 15:25:16 +020042 int cmdlen, posc, i, j;
43 struct yl_opt yo = {0};
44 char *empty = NULL, *cmdline;
45 char **posv;
46 uint8_t cmd_found;
Radek Krejcied5acc52019-04-25 15:57:04 +020047
48 if (argc > 1) {
49 /* run in non-interactive mode */
50 return main_ni(argc, argv);
51 }
aPieceka83b8e02023-06-07 15:25:16 +020052 yo.interactive = 1;
Radek Krejcied5acc52019-04-25 15:57:04 +020053
54 /* continue in interactive mode */
55 linenoiseSetCompletionCallback(complete_cmd);
56 load_config();
57
Michal Vaskoc431a0a2021-01-25 14:31:58 +010058 if (ly_ctx_new(NULL, YL_DEFAULT_CTX_OPTIONS, &ctx)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020059 YLMSG_E("Failed to create context.");
Radek Krejcied5acc52019-04-25 15:57:04 +020060 return 1;
61 }
62
63 while (!done) {
aPieceka83b8e02023-06-07 15:25:16 +020064 cmd_found = 0;
65
66 posv = &empty;
67 posc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +010068
Radek Krejcied5acc52019-04-25 15:57:04 +020069 /* get the command from user */
70 cmdline = linenoise(PROMPT);
71
72 /* EOF -> exit */
73 if (cmdline == NULL) {
74 done = 1;
75 cmdline = strdup("quit");
76 }
77
78 /* empty line -> wait for another command */
79 if (*cmdline == '\0') {
80 free(cmdline);
81 continue;
82 }
83
84 /* isolate the command word. */
Radek Krejcie9f13b12020-11-09 17:42:04 +010085 for (cmdlen = 0; cmdline[cmdlen] && (cmdline[cmdlen] != ' '); cmdlen++) {}
Radek Krejcied5acc52019-04-25 15:57:04 +020086
87 /* execute the command if any valid specified */
aPieceka83b8e02023-06-07 15:25:16 +020088 for (i = 0; commands[i].name; i++) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010089 if (strncmp(cmdline, commands[i].name, (size_t)cmdlen) || (commands[i].name[cmdlen] != '\0')) {
90 continue;
Radek Krejcied5acc52019-04-25 15:57:04 +020091 }
Radek Krejcied5acc52019-04-25 15:57:04 +020092
aPieceka83b8e02023-06-07 15:25:16 +020093 cmd_found = 1;
94 if (commands[i].opt_func && commands[i].opt_func(&yo, cmdline, &posv, &posc)) {
95 break;
96 }
97 if (commands[i].dep_func && commands[i].dep_func(&yo, posc)) {
98 break;
99 }
100 if (posc) {
101 for (j = 0; j < posc; j++) {
102 yo.last_one = (j + 1) == posc;
103 if (commands[i].exec_func(&ctx, &yo, posv[j])) {
104 break;
105 }
106 }
107 } else {
108 commands[i].exec_func(&ctx, &yo, NULL);
109 }
110 if (commands[i].fin_func) {
111 commands[i].fin_func(ctx, &yo);
112 }
113
Radek Krejcie9f13b12020-11-09 17:42:04 +0100114 break;
115 }
116
aPieceka83b8e02023-06-07 15:25:16 +0200117 if (!cmd_found) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100118 /* if unknown command specified, tell it to user */
Michal Vasko1407d7d2023-08-21 09:57:54 +0200119 YLMSG_E("Unknown command \"%.*s\", type 'help' for more information.", cmdlen, cmdline);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100120 }
121
122 linenoiseHistoryAdd(cmdline);
Radek Krejcied5acc52019-04-25 15:57:04 +0200123 free(cmdline);
aPieceka83b8e02023-06-07 15:25:16 +0200124 yl_opt_erase(&yo);
Radek Krejcied5acc52019-04-25 15:57:04 +0200125 }
126
aPiecek647f62e2023-05-18 10:55:58 +0200127 /* Global variables in commands are freed. */
128 cmd_free();
129
Radek Krejcied5acc52019-04-25 15:57:04 +0200130 store_config();
Radek Krejci90ed21e2021-04-12 14:47:46 +0200131 ly_ctx_destroy(ctx);
Radek Krejcied5acc52019-04-25 15:57:04 +0200132
133 return 0;
134}