Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linenoise update #1678

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ set(CLI_SRC
commands.c
completion.c
configuration.c
linenoise/linenoise.c)
linenoise/linenoise.c
linenoise/utf8.c)

# netopeer2-cli target
add_executable(netopeer2-cli ${CLI_SRC} ${compatsrc})
Expand Down
30 changes: 6 additions & 24 deletions cli/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,10 @@ static void
cli_ntf_clb(struct nc_session *UNUSED(session), const struct lyd_node *envp, const struct lyd_node *op, void *user_data)
{
FILE *output = user_data;
int was_rawmode = 0;
const struct lyd_node *top;

if (output == stdout) {
if (lss.rawmode) {
was_rawmode = 1;
linenoiseDisableRawMode(lss.ifd);
printf("\n");
} else {
was_rawmode = 0;
}
linenoiseBackgroundPrintStart();
}

for (top = op; top->parent; top = lyd_parent(top)) {}
Expand All @@ -233,9 +226,8 @@ cli_ntf_clb(struct nc_session *UNUSED(session), const struct lyd_node *envp, con
fprintf(output, "\n");
fflush(output);

if ((output == stdout) && was_rawmode) {
linenoiseEnableRawMode(lss.ifd);
linenoiseRefreshLine();
if (output == stdout) {
linenoiseBackgroundPrintEnd();
}

if (!strcmp(op->schema->name, "notificationComplete") && !strcmp(op->schema->module->name, "nc-notifications")) {
Expand Down Expand Up @@ -2566,29 +2558,19 @@ cmd_disconnect(const char *UNUSED(arg), char **UNUSED(tmp_config_file))
void
monitoring_clb(struct nc_session *sess, void *user_data)
{
int was_rawmode = 0;

(void)sess;
(void)user_data;

/* needed for the case that the user is typing a command */
if (lss.rawmode) {
was_rawmode = 1;
linenoiseDisableRawMode(lss.ifd);
printf("\n");
}
linenoiseBackgroundPrintStart();

fprintf(stdout, "Connection reset by peer.\n");
fflush(stdout);

linenoiseBackgroundPrintEnd();

/* free the session and set the global session variable to NULL */
nc_session_free(session, NULL);
session = NULL;

if (was_rawmode) {
linenoiseEnableRawMode(lss.ifd);
linenoiseRefreshLine();
}
}

static int
Expand Down
76 changes: 70 additions & 6 deletions cli/completion.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,70 @@
#include "configuration.h"
#include "linenoise/linenoise.h"

/**
* @brief Fill path completion.
*
* @param[in] hint Path to directory.
* @param[in] lc For adding completions.
*/
static void
path_completion(const char *hint, linenoiseCompletions *lc)
{
const char *ptr;
char *full_path, *hint_ptr, match[FILENAME_MAX + 2];
DIR *dir;
struct dirent *ent;
struct stat st;

lc->path = 1;

ptr = strrchr(hint, '/');

/* new relative path */
if (ptr == NULL) {
full_path = malloc(2 + FILENAME_MAX + 1);
strcpy(full_path, "./");

ptr = hint;
} else {
full_path = malloc((int)(ptr - hint) + FILENAME_MAX + 1);
++ptr;
sprintf(full_path, "%.*s", (int)(ptr - hint), hint);
Dismissed Show dismissed Hide dismissed
}
hint_ptr = full_path + strlen(full_path);

dir = opendir(full_path);
if (dir == NULL) {
free(full_path);
return;
}

while ((ent = readdir(dir))) {
if (ent->d_name[0] == '.') {
continue;
}

if (!strncmp(ptr, ent->d_name, strlen(ptr))) {
/* is it a directory? */
strcpy(hint_ptr, ent->d_name);
if (stat(full_path, &st)) {
/* skip this item */
continue;
}

strcpy(match, ent->d_name);
if (S_ISDIR(st.st_mode)) {
strcat(match, "/");
}

linenoiseAddCompletion(lc, match);
}
}

free(full_path);
closedir(dir);
}

static void
get_cmd_completion(const char *hint, char ***matches, unsigned int *match_count)
{
Expand Down Expand Up @@ -87,18 +151,18 @@ complete_cmd(const char *buf, const char *hint, linenoiseCompletions *lc)
!strncmp(buf, "cert remove ", 12) || !strncmp(buf, "cert replaceown ", 16)
#endif
) {
linenoisePathCompletion(buf, hint, lc);
path_completion(hint, lc);
} else if ((!strncmp(buf, "copy-config ", 12) || !strncmp(buf, "validate ", 9)) && last_opt(buf, hint, "--src-config")) {
linenoisePathCompletion(buf, hint, lc);
path_completion(hint, lc);
} else if (!strncmp(buf, "edit-config ", 12) && last_opt(buf, hint, "--config")) {
linenoisePathCompletion(buf, hint, lc);
path_completion(hint, lc);
} else if ((!strncmp(buf, "get ", 4) || !strncmp(buf, "get-config ", 11) || !strncmp(buf, "subscribe ", 10)) &&
(last_opt(buf, hint, "--filter-subtree") || last_opt(buf, hint, "--out"))) {
linenoisePathCompletion(buf, hint, lc);
path_completion(hint, lc);
} else if (!strncmp(buf, "get-schema ", 11) && last_opt(buf, hint, "--out")) {
linenoisePathCompletion(buf, hint, lc);
path_completion(hint, lc);
} else if (!strncmp(buf, "user-rpc ", 9) && last_opt(buf, hint, "--content")) {
linenoisePathCompletion(buf, hint, lc);
path_completion(hint, lc);
} else if (!strchr(buf, ' ') && hint[0]) {
get_cmd_completion(hint, &matches, &match_count);

Expand Down
Loading
Loading