cli: avoid passing full NmCli global variable to nm_cli_spawn_pager()
We should not use global variables, and we should minimize the state that we pass around. Instead of requiring the full NmCli struct in nm_cli_spawn_pager(), pass only the necessary data. This reduces our use of global variables.
This commit is contained in:
@@ -2093,7 +2093,7 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nm_cli_spawn_pager (nmc);
|
nm_cli_spawn_pager (&nmc->nmc_config, &nmc->pager_data);
|
||||||
|
|
||||||
items = con_show_get_items (nmc, active_only, show_active_fields, order);
|
items = con_show_get_items (nmc, active_only, show_active_fields, order);
|
||||||
g_ptr_array_add (items, NULL);
|
g_ptr_array_add (items, NULL);
|
||||||
|
@@ -556,7 +556,7 @@ print_permissions (void *user_data)
|
|||||||
permissions[i] = GINT_TO_POINTER (nm_auth_permission_sorted[i]);
|
permissions[i] = GINT_TO_POINTER (nm_auth_permission_sorted[i]);
|
||||||
permissions[i] = NULL;
|
permissions[i] = NULL;
|
||||||
|
|
||||||
nm_cli_spawn_pager (nmc);
|
nm_cli_spawn_pager (&nmc->nmc_config, &nmc->pager_data);
|
||||||
|
|
||||||
if (!nmc_print (&nmc->nmc_config,
|
if (!nmc_print (&nmc->nmc_config,
|
||||||
permissions,
|
permissions,
|
||||||
@@ -1447,7 +1447,7 @@ do_overview (NmCli *nmc, int argc, char **argv)
|
|||||||
/* Register polkit agent */
|
/* Register polkit agent */
|
||||||
nmc_start_polkit_agent_start_try (nmc);
|
nmc_start_polkit_agent_start_try (nmc);
|
||||||
|
|
||||||
nm_cli_spawn_pager (nmc);
|
nm_cli_spawn_pager (&nmc->nmc_config, &nmc->pager_data);
|
||||||
|
|
||||||
/* The VPN connections don't have devices (yet?). */
|
/* The VPN connections don't have devices (yet?). */
|
||||||
p = nm_client_get_active_connections (nmc->client);
|
p = nm_client_get_active_connections (nmc->client);
|
||||||
|
@@ -911,11 +911,12 @@ signal_handler (gpointer user_data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
nm_cli_spawn_pager (NmCli *nmc)
|
nm_cli_spawn_pager (const NmcConfig *nmc_config,
|
||||||
|
NmcPagerData *pager_data)
|
||||||
{
|
{
|
||||||
if (nmc->pager_pid > 0)
|
if (pager_data->pid != 0)
|
||||||
return;
|
return;
|
||||||
nmc->pager_pid = nmc_terminal_spawn_pager (&nmc->nmc_config);
|
pager_data->pid = nmc_terminal_spawn_pager (nmc_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -937,13 +938,14 @@ nmc_cleanup (NmCli *nmc)
|
|||||||
|
|
||||||
nm_clear_g_free (&nmc->required_fields);
|
nm_clear_g_free (&nmc->required_fields);
|
||||||
|
|
||||||
if (nmc->pager_pid > 0) {
|
if (nmc->pager_data.pid != 0) {
|
||||||
|
pid_t pid = nm_steal_int (&nmc->pager_data.pid);
|
||||||
|
|
||||||
fclose (stdout);
|
fclose (stdout);
|
||||||
fclose (stderr);
|
fclose (stderr);
|
||||||
do {
|
do {
|
||||||
ret = waitpid (nmc->pager_pid, NULL, 0);
|
ret = waitpid (pid, NULL, 0);
|
||||||
} while (ret == -1 && errno == EINTR);
|
} while (ret == -1 && errno == EINTR);
|
||||||
nmc->pager_pid = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nm_clear_g_free (&nmc->palette_buffer);
|
nm_clear_g_free (&nmc->palette_buffer);
|
||||||
|
@@ -101,6 +101,10 @@ typedef struct _NmcConfig {
|
|||||||
const char *palette[_NM_META_COLOR_NUM]; /* Color palette */
|
const char *palette[_NM_META_COLOR_NUM]; /* Color palette */
|
||||||
} NmcConfig;
|
} NmcConfig;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
pid_t pid;
|
||||||
|
} NmcPagerData;
|
||||||
|
|
||||||
typedef struct _NmcOutputData {
|
typedef struct _NmcOutputData {
|
||||||
GPtrArray *output_data; /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */
|
GPtrArray *output_data; /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */
|
||||||
} NmcOutputData;
|
} NmcOutputData;
|
||||||
@@ -111,7 +115,8 @@ typedef struct _NmCli {
|
|||||||
|
|
||||||
NMCResultCode return_value; /* Return code of nmcli */
|
NMCResultCode return_value; /* Return code of nmcli */
|
||||||
GString *return_text; /* Reason text */
|
GString *return_text; /* Reason text */
|
||||||
pid_t pager_pid; /* PID of a pager, if one was spawned */
|
|
||||||
|
NmcPagerData pager_data;
|
||||||
|
|
||||||
int timeout; /* Operation timeout */
|
int timeout; /* Operation timeout */
|
||||||
|
|
||||||
@@ -150,7 +155,8 @@ void nmc_clear_sigint (void);
|
|||||||
void nmc_set_sigquit_internal (void);
|
void nmc_set_sigquit_internal (void);
|
||||||
void nmc_exit (void);
|
void nmc_exit (void);
|
||||||
|
|
||||||
void nm_cli_spawn_pager (NmCli *nmc);
|
void nm_cli_spawn_pager (const NmcConfig *nmc_config,
|
||||||
|
NmcPagerData *pager_data);
|
||||||
|
|
||||||
void nmc_empty_output_fields (NmcOutputData *output_data);
|
void nmc_empty_output_fields (NmcOutputData *output_data);
|
||||||
|
|
||||||
|
@@ -1587,7 +1587,7 @@ print_required_fields (const NmcConfig *nmc_config,
|
|||||||
gboolean field_names = of_flags & NMC_OF_FLAG_FIELD_NAMES;
|
gboolean field_names = of_flags & NMC_OF_FLAG_FIELD_NAMES;
|
||||||
gboolean section_prefix = of_flags & NMC_OF_FLAG_SECTION_PREFIX;
|
gboolean section_prefix = of_flags & NMC_OF_FLAG_SECTION_PREFIX;
|
||||||
|
|
||||||
nm_cli_spawn_pager (&nm_cli);
|
nm_cli_spawn_pager (nmc_config, &nm_cli.pager_data);
|
||||||
|
|
||||||
/* --- Main header --- */
|
/* --- Main header --- */
|
||||||
if ( nmc_config->print_output == NMC_PRINT_PRETTY
|
if ( nmc_config->print_output == NMC_PRINT_PRETTY
|
||||||
|
Reference in New Issue
Block a user