From d1dad6ae27e05df2c580d81ced808f81498a92eb Mon Sep 17 00:00:00 2001 From: Lukasz Majewski Date: Fri, 2 Jul 2021 10:30:22 +0200 Subject: [PATCH] cli: Provide optional support for libedit instead of readline The libreadline starting from version 6 is licensed as GPLv3. For some use cases it is not acceptable to use this license. In the NetworkManager the libreadline is used by nmcli. This change allows using libedit instead of libreadline. Following adjustments were made: 1. The history_set_history_state() is not supported in the libedit. Instead, the where_history() with remove_history() were used to remove the history content if needed. 2. rl_complete_with_tilde_expansion - it is the binary flag used only when one wants to have the expansion support. The libedit is not supporting and hence exporting this flag. --- src/nmcli/agent.c | 5 ++++- src/nmcli/common.c | 21 +++++++++++++++++++-- src/nmcli/connections.c | 12 +++++++++++- src/nmcli/devices.c | 4 ++++ src/nmcli/nmcli.c | 4 ++++ 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/nmcli/agent.c b/src/nmcli/agent.c index a71e40594b..842843995a 100644 --- a/src/nmcli/agent.c +++ b/src/nmcli/agent.c @@ -7,9 +7,12 @@ #include #include +#if HAVE_EDITLINE_READLINE +#include +#else #include #include - +#endif #include "common.h" #include "utils.h" #include "libnmc-base/nm-secret-agent-simple.h" diff --git a/src/nmcli/common.c b/src/nmcli/common.c index 690bcf0d03..67bbb6a179 100644 --- a/src/nmcli/common.c +++ b/src/nmcli/common.c @@ -10,9 +10,12 @@ #include #include #include -#include +#if HAVE_EDITLINE_READLINE +#include +#else #include - +#include +#endif #include "libnm-client-aux-extern/nm-libnm-aux.h" #include "libnmc-base/nm-vpn-helpers.h" @@ -1007,10 +1010,14 @@ nmc_readline_echo(const NmcConfig *nmc_config, gboolean echo_on, const char *pro va_list args; gs_free char *prompt = NULL; char * str; +#if HAVE_READLINE_HISTORY nm_auto_free HISTORY_STATE *saved_history = NULL; HISTORY_STATE passwd_history = { 0, }; +#else + int start, curpos; +#endif va_start(args, prompt_fmt); prompt = g_strdup_vprintf(prompt_fmt, args); @@ -1020,8 +1027,12 @@ nmc_readline_echo(const NmcConfig *nmc_config, gboolean echo_on, const char *pro /* Hide the actual password */ if (!echo_on) { +#if HAVE_READLINE_HISTORY saved_history = history_get_history_state(); history_set_history_state(&passwd_history); +#else + start = where_history(); +#endif /* stifling history is important as it tells readline to * not store anything, otherwise sensitive data could be * leaked */ @@ -1034,7 +1045,13 @@ nmc_readline_echo(const NmcConfig *nmc_config, gboolean echo_on, const char *pro /* Restore the non-hiding behavior */ if (!echo_on) { rl_redisplay_function = rl_redisplay; +#if HAVE_READLINE_HISTORY history_set_history_state(saved_history); +#else + curpos = where_history(); + while (curpos > start) + remove_history(curpos--); +#endif } return str; diff --git a/src/nmcli/connections.c b/src/nmcli/connections.c index 4f291408af..70fa89608b 100644 --- a/src/nmcli/connections.c +++ b/src/nmcli/connections.c @@ -11,8 +11,12 @@ #include #include #include +#if HAVE_EDITLINE_READLINE +#include +#else #include #include +#endif #include #include "libnm-glib-aux/nm-dbus-aux.h" @@ -6453,8 +6457,10 @@ gen_property_values(const char *text, int state) return nmc_rl_gen_func_basic(text, state, avals); } +#if !HAVE_EDITLINE_READLINE /* from readline */ extern int rl_complete_with_tilde_expansion; +#endif /* * Attempt to complete on the contents of TEXT. START and END show the @@ -6482,8 +6488,10 @@ nmcli_editor_tab_completion(const char *text, int start, int end) /* Disable default filename completion */ rl_attempted_completion_over = 1; +#if !HAVE_EDITLINE_READLINE /* Enable tilde expansion when filenames are completed */ rl_complete_with_tilde_expansion = 1; +#endif /* Filter out possible ANSI color escape sequences */ prompt_tmp = nmc_filter_out_colors((const char *) rl_prompt); @@ -9617,8 +9625,10 @@ nmcli_con_tab_completion(const char *text, int start, int end) nmc_tab_completion.words = _meta_abstract_complete(info, text); generator_func = _meta_abstract_generator; } else if (nm_streq0(rl_prompt, PROMPT_IMPORT_FILE)) { - rl_attempted_completion_over = 0; + rl_attempted_completion_over = 0; +#if !HAVE_EDITLINE_READLINE rl_complete_with_tilde_expansion = 1; +#endif } else if (nm_streq0(rl_prompt, PROMPT_VPN_CONNECTION)) { generator_func = gen_vpn_ids; } diff --git a/src/nmcli/devices.c b/src/nmcli/devices.c index 33be4e7a6d..00339868b6 100644 --- a/src/nmcli/devices.c +++ b/src/nmcli/devices.c @@ -9,7 +9,11 @@ #include #include +#if HAVE_EDITLINE_READLINE +#include +#else #include +#endif #include #include "libnm-glib-aux/nm-secret-utils.h" diff --git a/src/nmcli/nmcli.c b/src/nmcli/nmcli.c index 9c6a791187..b2b2cd8143 100644 --- a/src/nmcli/nmcli.c +++ b/src/nmcli/nmcli.c @@ -14,8 +14,12 @@ #include #include #include +#if HAVE_EDITLINE_READLINE +#include +#else #include #include +#endif #include "libnmc-base/nm-client-utils.h"