cli: add and use nm_cli_global_readline global variable

We should try to avoid access to global variables. For libreadline
callbacks we still need a global variable.

Introduce a global variable nm_cli_global_readline, specially for this
use. It makes the places clear where we use it, and discourages
the use at other places, where we better avoid global variables.
This commit is contained in:
Thomas Haller 2020-04-02 13:33:10 +02:00
parent 2d83df3820
commit 873f4795b2
4 changed files with 10 additions and 6 deletions

View file

@ -1118,7 +1118,7 @@ nmc_rl_gen_func_ifnames (const char *text, int state)
const char **ifnames;
char *ret;
devices = nm_client_get_devices (nm_cli.client);
devices = nm_client_get_devices (nm_cli_global_readline->client);
if (devices->len == 0)
return NULL;

View file

@ -5827,7 +5827,7 @@ gen_vpn_uuids (const char *text, int state)
const char **uuids;
char *ret;
connections = nm_client_get_connections (nm_cli.client);
connections = nm_client_get_connections (nm_cli_global_readline->client);
if (connections->len < 1)
return NULL;
@ -5844,7 +5844,7 @@ gen_vpn_ids (const char *text, int state)
const char **ids;
char *ret;
connections = nm_client_get_connections (nm_cli.client);
connections = nm_client_get_connections (nm_cli_global_readline->client);
if (connections->len < 1)
return NULL;
@ -9253,7 +9253,7 @@ gen_func_connection_names (const char *text, int state)
const char **connection_names;
char *ret;
connections = nm_client_get_connections (nm_cli.client);
connections = nm_client_get_connections (nm_cli_global_readline->client);
if (connections->len == 0)
return NULL;
@ -9276,10 +9276,10 @@ gen_func_active_connection_names (const char *text, int state)
const char **connections;
char *ret;
if (!nm_cli.client)
if (!nm_cli_global_readline->client)
return NULL;
acs = nm_client_get_active_connections (nm_cli.client);
acs = nm_client_get_active_connections (nm_cli_global_readline->client);
if (!acs || acs->len == 0)
return NULL;

View file

@ -101,6 +101,8 @@ NmCli nm_cli = {
.editor_save_confirmation = TRUE,
};
const NmCli *const nm_cli_global_readline = &nm_cli;
/*****************************************************************************/
typedef struct {

View file

@ -137,6 +137,8 @@ typedef struct _NmCli {
extern NmCli nm_cli;
extern const NmCli *const nm_cli_global_readline;
/* Error quark for GError domain */
#define NMCLI_ERROR (nmcli_error_quark ())
GQuark nmcli_error_quark (void);