Skip to content

Commit

Permalink
yanglint FEATURE enable multiline for linenoise
Browse files Browse the repository at this point in the history
Had to add a new 'cli' command to yanglint in interactive mode.
Although the Multiline is more suitable for interactive mode,
it creates a problem for tcl tests because it often adds ANSI
escape codes. So at the beginning of the tcl tests, multiline
is turned off.
  • Loading branch information
lePici committed Oct 7, 2024
1 parent 4452ad3 commit 2eb5300
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 2 deletions.
5 changes: 4 additions & 1 deletion tests/yanglint/interactive/ly.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ stty columns 720
# default setup for every unit test
variable ly_setup {
spawn $TUT
# turn off linenoise multiline.
ly_skip_warnings
# Searchpath is set, so modules can be loaded via the 'load' command.
ly_cmd "searchpath $::env(YANG_MODULES_DIR)"
Expand All @@ -32,7 +33,9 @@ variable ly_cleanup {
# Skip no dir and/or no history warnings and prompt.
proc ly_skip_warnings {} {
global prompt
expect -re "(YANGLINT.*)*$prompt" {}
expect -re "$prompt" {}
send -- "cli -s\r"
expect -re "\n$prompt" {}
}

# Send command 'cmd' to the process, expect error header and then check output string by 'pattern'.
Expand Down
1 change: 1 addition & 0 deletions tools/lint/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(lintsrc
cmd_help.c
cmd_verb.c
cmd_debug.c
cmd_cli.c
yl_opt.c
yl_schema_features.c
common.c
Expand Down
4 changes: 4 additions & 0 deletions tools/lint/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ COMMAND commands[] = {
"Unsupported for the Release build", "h"
#endif
},
{
"cli", cmd_cli_opt, cmd_cli_dep, cmd_cli_exec, NULL, cmd_cli_help, NULL,
"Settings for the command line interface", "sh"
},
{"quit", NULL, NULL, cmd_quit_exec, NULL, NULL, NULL, "Quit the program", "h"},
/* synonyms for previous commands */
{"?", NULL, NULL, cmd_help_exec, NULL, NULL, NULL, "Display commands description", "h"},
Expand Down
26 changes: 25 additions & 1 deletion tools/lint/cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ enum COMMAND_INDEX {
CMD_EXTDATA,
CMD_CLEAR,
CMD_VERB,
CMD_DEBUG
CMD_DEBUG,
CMD_CLI
};

/**
Expand Down Expand Up @@ -391,4 +392,27 @@ int cmd_debug_store(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
int cmd_debug_setlog(struct ly_ctx *ctx, struct yl_opt *yo);
void cmd_debug_help(void);

/* cmd_cli.c */

/**
* @brief Setup cli.
*
* @param[in,out] ctx context for libyang.
* @param[in,out] yo context for yanglint.
* @param[in] posv Name of the cli mode.
* @return 0 on success.
*/
int cmd_cli_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
void cmd_cli_help(void);

/**
* @copydoc cmd_add_opt
*/
int cmd_cli_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);

/**
* @copydoc cmd_add_dep
*/
int cmd_cli_dep(struct yl_opt *yo, int posc);

#endif /* COMMANDS_H_ */
109 changes: 109 additions & 0 deletions tools/lint/cmd_cli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @file cli_verb.c
* @author Adam Piecek <[email protected]>
* @brief 'cli' command of the libyang's yanglint tool.
*
* Copyright (c) 2024-2024 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 "cmd.h"

#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <strings.h>

#include "libyang.h"
#include "linenoise/linenoise.h"

#include "common.h"
#include "yl_opt.h"

#define LY_CLI_SINGLELINE 0x1

uint8_t cli_flags;

void
cmd_cli_help(void)
{
printf("Usage: cli [-s]\n"
" Settings for the command line interface.\n\n"
" -s, --toggle-singleline\n"
" The singleline settings toggles how the cli handles the overflow of typed text\n"
" across the screen. By default, it puts overflow text on a new line. But this\n"
" setting will make the text will scroll towards right to make room while\n"
" sending much less ANSI escape codes.\n");
}

int
cmd_cli_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
{
int rc = 0, argc = 0;
int opt, opt_index;
struct option options[] = {
{"toggle-singleline", no_argument, NULL, 's'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};

if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
return rc;
}

while ((opt = getopt_long(argc, yo->argv, commands[CMD_CLI].optstring, options, &opt_index)) != -1) {
switch (opt) {
case 's':
yo->cli_flags |= LY_CLI_SINGLELINE;
break;
case 'h':
cmd_cli_help();
return 1;
default:
YLMSG_E("Unknown option.");
return 1;
}
}

*posv = &yo->argv[optind];
*posc = argc - optind;

return 0;
}

int
cmd_cli_dep(struct yl_opt *yo, int posc)
{
(void) yo;

if (posc != 0) {
YLMSG_E("Only options are expected.");
cmd_verb_help();
return 1;
}

return 0;
}

int
cmd_cli_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
{
(void) ctx, (void) posv;

if (yo->cli_flags & LY_CLI_SINGLELINE) {
if (cli_flags & LY_CLI_SINGLELINE) {
cli_flags &= ~LY_CLI_SINGLELINE;
linenoiseSetMultiLine(1);
} else {
cli_flags |= LY_CLI_SINGLELINE;
linenoiseSetMultiLine(0);
}
}

return 0;
}
1 change: 1 addition & 0 deletions tools/lint/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ main(int argc, char *argv[])
/* continue in interactive mode */
linenoiseSetCompletionCallback(complete_cmd);
linenoiseSetEncodingFunctions(linenoiseUtf8PrevCharLen, linenoiseUtf8NextCharLen, linenoiseUtf8ReadCode);
linenoiseSetMultiLine(1);
load_config();

if (ly_ctx_new(NULL, YL_DEFAULT_CTX_OPTIONS, &ctx)) {
Expand Down
3 changes: 3 additions & 0 deletions tools/lint/yl_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ struct yl_opt {

uint32_t dbg_groups;

/* cli command settings */
uint8_t cli_flags;

/*
* schema
*/
Expand Down

0 comments on commit 2eb5300

Please sign in to comment.