2020-12-23 22:21:36 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2019-09-25 13:13:40 +02:00
|
|
|
/*
|
2010-02-25 09:52:30 -08:00
|
|
|
* Jiri Klimes <jklimes@redhat.com>
|
2019-10-01 09:20:35 +02:00
|
|
|
* Copyright (C) 2010 - 2018 Red Hat, Inc.
|
2010-02-25 09:52:30 -08:00
|
|
|
*/
|
|
|
|
|
|
2021-02-19 15:23:34 +01:00
|
|
|
#include "libnm-client-aux-extern/nm-default-client.h"
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2017-04-04 13:52:13 +02:00
|
|
|
#include "nmcli.h"
|
|
|
|
|
|
2010-02-25 09:52:30 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <signal.h>
|
2014-10-30 11:25:55 +01:00
|
|
|
#include <termios.h>
|
|
|
|
|
#include <unistd.h>
|
2010-02-25 09:52:30 -08:00
|
|
|
#include <locale.h>
|
2016-09-02 16:16:34 +02:00
|
|
|
#include <glib-unix.h>
|
2014-06-16 08:30:22 +02:00
|
|
|
#include <readline/readline.h>
|
|
|
|
|
#include <readline/history.h>
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2021-03-01 12:42:48 +01:00
|
|
|
#include "libnmc-base/nm-client-utils.h"
|
2017-03-28 12:16:31 +02:00
|
|
|
|
2014-10-30 11:25:55 +01:00
|
|
|
#include "polkit-agent.h"
|
2010-02-25 09:52:30 -08:00
|
|
|
#include "utils.h"
|
2014-05-15 10:31:09 +02:00
|
|
|
#include "common.h"
|
2010-02-25 09:52:30 -08:00
|
|
|
#include "connections.h"
|
|
|
|
|
#include "devices.h"
|
2016-07-27 16:24:30 +02:00
|
|
|
#include "settings.h"
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2010-04-26 11:05:36 +02:00
|
|
|
#if defined(NM_DIST_VERSION)
|
|
|
|
|
#define NMCLI_VERSION NM_DIST_VERSION
|
|
|
|
|
#else
|
|
|
|
|
#define NMCLI_VERSION VERSION
|
|
|
|
|
#endif
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2020-10-08 12:33:16 +02:00
|
|
|
#define _NMC_COLOR_PALETTE_INIT() \
|
|
|
|
|
{ \
|
|
|
|
|
.ansi_seq = { \
|
|
|
|
|
[NM_META_COLOR_CONNECTION_ACTIVATED] = "32", \
|
|
|
|
|
[NM_META_COLOR_CONNECTION_ACTIVATING] = "33", \
|
|
|
|
|
[NM_META_COLOR_CONNECTION_DISCONNECTING] = "31", \
|
|
|
|
|
[NM_META_COLOR_CONNECTION_INVISIBLE] = "2", \
|
|
|
|
|
[NM_META_COLOR_CONNECTION_EXTERNAL] = "32;2", \
|
|
|
|
|
[NM_META_COLOR_CONNECTIVITY_FULL] = "32", \
|
|
|
|
|
[NM_META_COLOR_CONNECTIVITY_LIMITED] = "33", \
|
|
|
|
|
[NM_META_COLOR_CONNECTIVITY_NONE] = "31", \
|
|
|
|
|
[NM_META_COLOR_CONNECTIVITY_PORTAL] = "33", \
|
|
|
|
|
[NM_META_COLOR_DEVICE_ACTIVATED] = "32", \
|
|
|
|
|
[NM_META_COLOR_DEVICE_ACTIVATING] = "33", \
|
|
|
|
|
[NM_META_COLOR_DEVICE_DISCONNECTED] = "31", \
|
|
|
|
|
[NM_META_COLOR_DEVICE_FIRMWARE_MISSING] = "31", \
|
|
|
|
|
[NM_META_COLOR_DEVICE_PLUGIN_MISSING] = "31", \
|
|
|
|
|
[NM_META_COLOR_DEVICE_UNAVAILABLE] = "2", \
|
|
|
|
|
[NM_META_COLOR_DEVICE_DISABLED] = "31", \
|
|
|
|
|
[NM_META_COLOR_DEVICE_EXTERNAL] = "32;2", \
|
|
|
|
|
[NM_META_COLOR_MANAGER_RUNNING] = "32", \
|
|
|
|
|
[NM_META_COLOR_MANAGER_STARTING] = "33", \
|
|
|
|
|
[NM_META_COLOR_MANAGER_STOPPED] = "31", \
|
|
|
|
|
[NM_META_COLOR_PERMISSION_AUTH] = "33", \
|
|
|
|
|
[NM_META_COLOR_PERMISSION_NO] = "31", \
|
|
|
|
|
[NM_META_COLOR_PERMISSION_YES] = "32", \
|
|
|
|
|
[NM_META_COLOR_STATE_ASLEEP] = "31", \
|
|
|
|
|
[NM_META_COLOR_STATE_CONNECTED_GLOBAL] = "32", \
|
|
|
|
|
[NM_META_COLOR_STATE_CONNECTED_LOCAL] = "32", \
|
|
|
|
|
[NM_META_COLOR_STATE_CONNECTED_SITE] = "32", \
|
|
|
|
|
[NM_META_COLOR_STATE_CONNECTING] = "33", \
|
|
|
|
|
[NM_META_COLOR_STATE_DISCONNECTED] = "31", \
|
|
|
|
|
[NM_META_COLOR_STATE_DISCONNECTING] = "33", \
|
|
|
|
|
[NM_META_COLOR_WIFI_SIGNAL_EXCELLENT] = "32", \
|
|
|
|
|
[NM_META_COLOR_WIFI_SIGNAL_FAIR] = "35", \
|
|
|
|
|
[NM_META_COLOR_WIFI_SIGNAL_GOOD] = "33", \
|
|
|
|
|
[NM_META_COLOR_WIFI_SIGNAL_POOR] = "36", \
|
|
|
|
|
[NM_META_COLOR_WIFI_SIGNAL_UNKNOWN] = "2", \
|
|
|
|
|
[NM_META_COLOR_ENABLED] = "32", \
|
|
|
|
|
[NM_META_COLOR_DISABLED] = "31", \
|
|
|
|
|
}, \
|
|
|
|
|
}
|
2018-06-22 10:43:08 +02:00
|
|
|
|
2020-04-02 19:16:47 +02:00
|
|
|
static NmCli nm_cli = {
|
2018-03-28 22:30:44 +02:00
|
|
|
.client = NULL,
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-28 22:30:44 +02:00
|
|
|
.return_value = NMC_RESULT_SUCCESS,
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-28 22:30:44 +02:00
|
|
|
.timeout = -1,
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-28 22:30:44 +02:00
|
|
|
.secret_agent = NULL,
|
|
|
|
|
.pwds_hash = NULL,
|
|
|
|
|
.pk_listener = NULL,
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-28 22:30:44 +02:00
|
|
|
.should_wait = 0,
|
|
|
|
|
.nowait_flag = TRUE,
|
|
|
|
|
.nmc_config.print_output = NMC_PRINT_NORMAL,
|
|
|
|
|
.nmc_config.multiline_output = FALSE,
|
|
|
|
|
.mode_specified = FALSE,
|
|
|
|
|
.nmc_config.escape_values = TRUE,
|
|
|
|
|
.required_fields = NULL,
|
|
|
|
|
.ask = FALSE,
|
|
|
|
|
.complete = FALSE,
|
|
|
|
|
.nmc_config.show_secrets = FALSE,
|
|
|
|
|
.nmc_config.in_editor = FALSE,
|
2020-10-08 12:33:16 +02:00
|
|
|
.nmc_config.palette = _NMC_COLOR_PALETTE_INIT(),
|
2018-03-28 22:30:44 +02:00
|
|
|
.editor_status_line = FALSE,
|
|
|
|
|
.editor_save_confirmation = TRUE,
|
|
|
|
|
};
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2020-04-02 13:33:10 +02:00
|
|
|
const NmCli *const nm_cli_global_readline = &nm_cli;
|
2020-04-02 19:16:47 +02:00
|
|
|
const NmCli *const nmc_meta_environment_arg = &nm_cli;
|
2020-04-02 13:33:10 +02:00
|
|
|
|
cli: split tracking of meta data out of NmcOutputField
When generating output data, nmcli iterates over a list of
property-descriptors (nmc_fields_ip4_config), creates an intermediate
array (output_data) and finally prints it.
However, previously both the meta data (nmc_fields_ip4_config) and
the intermediate format use the same type NmcOutputField. This means,
certain fields are relevant to describe a property, and other fields
are output/formatting fields.
Split this up. Now, the meta data is tracked in form of an NMMetaAbstractInfo
lists. This separates the information about properties from intermediate steps
during creation of the output.
Note that currently functions like print_ip4_config() still have all the
knowledge about how to generate the output. That is wrong, instead, the
meta data (NMMetaAbstractInfo) should describe how to create the output
and then all those functions could be replaced. This means, later we want
to add more knowledge to the NMMetaAbstractInfo, so it is important to
keep them separate from NmcOutputField.
2017-03-31 19:18:16 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2010-02-25 09:52:30 -08:00
|
|
|
typedef struct {
|
|
|
|
|
NmCli *nmc;
|
|
|
|
|
int argc;
|
|
|
|
|
char **argv;
|
|
|
|
|
} ArgsInfo;
|
|
|
|
|
|
|
|
|
|
/* --- Global variables --- */
|
|
|
|
|
GMainLoop * loop = NULL;
|
2014-10-30 11:25:55 +01:00
|
|
|
struct termios termios_orig;
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2020-09-23 10:53:06 +02:00
|
|
|
NM_CACHED_QUARK_FCN("nmcli-error-quark", nmcli_error_quark);
|
2017-02-03 17:21:21 +01:00
|
|
|
|
2016-07-27 16:24:30 +02:00
|
|
|
static void
|
2017-03-28 13:34:10 +02:00
|
|
|
complete_field_setting(GHashTable *h, NMMetaSettingType setting_type)
|
2017-03-24 17:32:04 +01:00
|
|
|
{
|
2017-03-28 11:02:03 +02:00
|
|
|
const NMMetaSettingInfoEditor *setting_info = &nm_meta_setting_infos_editor[setting_type];
|
2017-03-28 13:34:10 +02:00
|
|
|
guint i;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-03-24 17:32:04 +01:00
|
|
|
for (i = 0; i < setting_info->properties_num; i++) {
|
2017-03-28 13:34:10 +02:00
|
|
|
g_hash_table_add(h,
|
|
|
|
|
g_strdup_printf("%s.%s",
|
|
|
|
|
setting_info->general->setting_name,
|
2017-04-12 19:00:03 +02:00
|
|
|
setting_info->properties[i]->property_name));
|
2017-03-24 17:32:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
cli: split tracking of meta data out of NmcOutputField
When generating output data, nmcli iterates over a list of
property-descriptors (nmc_fields_ip4_config), creates an intermediate
array (output_data) and finally prints it.
However, previously both the meta data (nmc_fields_ip4_config) and
the intermediate format use the same type NmcOutputField. This means,
certain fields are relevant to describe a property, and other fields
are output/formatting fields.
Split this up. Now, the meta data is tracked in form of an NMMetaAbstractInfo
lists. This separates the information about properties from intermediate steps
during creation of the output.
Note that currently functions like print_ip4_config() still have all the
knowledge about how to generate the output. That is wrong, instead, the
meta data (NMMetaAbstractInfo) should describe how to create the output
and then all those functions could be replaced. This means, later we want
to add more knowledge to the NMMetaAbstractInfo, so it is important to
keep them separate from NmcOutputField.
2017-03-31 19:18:16 +02:00
|
|
|
complete_field(GHashTable *h, const NmcMetaGenericInfo *const *field)
|
2016-07-27 16:24:30 +02:00
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
cli: split tracking of meta data out of NmcOutputField
When generating output data, nmcli iterates over a list of
property-descriptors (nmc_fields_ip4_config), creates an intermediate
array (output_data) and finally prints it.
However, previously both the meta data (nmc_fields_ip4_config) and
the intermediate format use the same type NmcOutputField. This means,
certain fields are relevant to describe a property, and other fields
are output/formatting fields.
Split this up. Now, the meta data is tracked in form of an NMMetaAbstractInfo
lists. This separates the information about properties from intermediate steps
during creation of the output.
Note that currently functions like print_ip4_config() still have all the
knowledge about how to generate the output. That is wrong, instead, the
meta data (NMMetaAbstractInfo) should describe how to create the output
and then all those functions could be replaced. This means, later we want
to add more knowledge to the NMMetaAbstractInfo, so it is important to
keep them separate from NmcOutputField.
2017-03-31 19:18:16 +02:00
|
|
|
for (i = 0; field[i]; i++)
|
|
|
|
|
g_hash_table_add(h, g_strdup(field[i]->name));
|
2016-07-27 16:24:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
complete_one(gpointer key, gpointer value, gpointer user_data)
|
|
|
|
|
{
|
2017-05-15 19:19:49 +02:00
|
|
|
const char **option_with_value = user_data;
|
|
|
|
|
const char * option = option_with_value[0];
|
|
|
|
|
const char * prefix = option_with_value[1];
|
2016-07-27 16:24:30 +02:00
|
|
|
const char * name = key;
|
|
|
|
|
const char * last;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2016-07-27 16:24:30 +02:00
|
|
|
last = strrchr(prefix, ',');
|
|
|
|
|
if (last)
|
|
|
|
|
last++;
|
|
|
|
|
else
|
|
|
|
|
last = prefix;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: make match() return boolean
Coccinelle semantic patch:
@@
@@
-int
+gboolean
matches (...);
@@
expression pattern, cmd, len;
@@
-int
+gboolean
matches (...)
{
...
- return memcmp (pattern, cmd, len);
+ return memcmp (pattern, cmd, len) == 0;
}
@@
expression prefix, str;
@@
(
-matches (prefix, str) != 0
+!matches (prefix, str)
|
-matches (prefix, str) == 0
+matches (prefix, str)
)
@@
expression prefix, str;
@@
-(matches (prefix, str))
+matches (prefix, str)
@@
expression prefix, str;
@@
-(!matches (prefix, str))
+!matches (prefix, str)
spatch --smpl-spacing --sp-file match.cocci --dir clients/cli/ \
--include-headers --macro-file shared/nm-utils/gsystem-local-alloc.h
2017-02-15 12:20:55 +01:00
|
|
|
if ((!*last && !strchr(name, '.')) || matches(last, name)) {
|
2017-05-15 19:19:49 +02:00
|
|
|
if (option != prefix) {
|
|
|
|
|
/* value prefix was not a standalone argument,
|
|
|
|
|
* it was part of --option=<value> argument.
|
|
|
|
|
* Repeat the part leading to "=". */
|
|
|
|
|
g_print("%s=", option);
|
|
|
|
|
}
|
2016-07-27 16:24:30 +02:00
|
|
|
g_print("%.*s%s%s\n",
|
|
|
|
|
(int) (last - prefix),
|
|
|
|
|
prefix,
|
|
|
|
|
name,
|
|
|
|
|
strcmp(last, name) == 0 ? "," : "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-15 19:19:49 +02:00
|
|
|
complete_fields(const char *option, const char *prefix)
|
2016-07-27 16:24:30 +02:00
|
|
|
{
|
2017-03-28 13:34:10 +02:00
|
|
|
guint i;
|
2016-07-27 16:24:30 +02:00
|
|
|
GHashTable *h;
|
2017-05-15 19:19:49 +02:00
|
|
|
const char *option_with_value[2] = {option, prefix};
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-10-13 16:12:35 +02:00
|
|
|
h = g_hash_table_new_full(nm_str_hash, g_str_equal, g_free, NULL);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-04-06 15:14:23 +02:00
|
|
|
complete_field(h, metagen_ip4_config);
|
2018-04-26 17:43:13 +02:00
|
|
|
complete_field(h, metagen_dhcp_config);
|
2018-04-26 17:55:27 +02:00
|
|
|
complete_field(h, metagen_ip6_config);
|
cli: rework printing of `nmcli connection` for multiple active connections
The output of `nmcli connection show` contains also information about
whether the profile is currently active, for example the device and
the current (activation) state.
Even when a profile can be activated only once (without supporting
mutiple activations at the same time), there are moments when a
connection is activating and still deactivating on another device.
NetworkManager ensures in the case with single activations that
a profile cannot be in state "activated" multiple times. But that
doesn't mean, that one profile cannot have multiple active connection
which reference it. That was already handled wrongly before, because
`nmcli connection show` would only search the first matching
active-connection. That is, it would arbitrarily pick an active
connection in case there were multiple and only show activation
state about one.
Furthermore, we will soon also add the possibility, that a profile can be
active multiple times (at the same time). Especially then, we need to
extend the output format to show all the devices on which the profile is
currently active.
Rework printing the connection list to use nmc_print(), and fix various
issues.
- as discussed, a profile may have multiple active connections at each time.
There are only two possibilities: if a profile is active multiple
times, show a line for each activation, or otherwise, show the
information about multiple activations combined in one line, e.g. by
printing "DEVICE eth0,eth1". This patch, does the former.
We will now print a line for each active connection, to show
all the devices and activation states in multiple lines.
Yes, this may result in the same profile being printed multiple times.
That is a change in behavior, and inconvenient if you do something
like
for UUID in $(nmcli connection show | awk '{print$2}'); do ...
However, above is anyway wrong because it assumes that there are no
spaces in the connection name. The proper way to do this is like
for UUID in $(nmcli -g UUID connection show); do ...
In the latter case, whenever a user selects a subset of fields
(--fields, --get) which don't print information about active connections,
these multiple lines are combined. So, above still works as expected,
never returning duplicate UUIDs.
- if a user has no permissions to see a connection, we previously
would print "<invisible> $NAME". No longer do this but just print
the ID was it is reported by the active-connection. If the goal
of this was to prevent users from accidentally access the non-existing
connection by $NAME, then this was a bad solution, because a script
would instead try to access "<invisible> $NAME". This is now solved
better by hiding the active connection if the user selects "-g NAME".
- the --order option now sorts according to how the fields are shown.
For example, with --terse mode, it will evaluate type "802-11-wireless"
but with pretty mode it will consider "wifi". This may change the
ordering in which connections are shown. Also, for sorting the name,
we use g_utf8_collate() because it's unicode.
2018-04-25 10:00:40 +02:00
|
|
|
complete_field(h, metagen_con_show);
|
2018-04-26 13:19:20 +02:00
|
|
|
complete_field(h, metagen_con_active_general);
|
2018-04-26 14:41:13 +02:00
|
|
|
complete_field(h, metagen_con_active_vpn);
|
cli: split tracking of meta data out of NmcOutputField
When generating output data, nmcli iterates over a list of
property-descriptors (nmc_fields_ip4_config), creates an intermediate
array (output_data) and finally prints it.
However, previously both the meta data (nmc_fields_ip4_config) and
the intermediate format use the same type NmcOutputField. This means,
certain fields are relevant to describe a property, and other fields
are output/formatting fields.
Split this up. Now, the meta data is tracked in form of an NMMetaAbstractInfo
lists. This separates the information about properties from intermediate steps
during creation of the output.
Note that currently functions like print_ip4_config() still have all the
knowledge about how to generate the output. That is wrong, instead, the
meta data (NMMetaAbstractInfo) should describe how to create the output
and then all those functions could be replaced. This means, later we want
to add more knowledge to the NMMetaAbstractInfo, so it is important to
keep them separate from NmcOutputField.
2017-03-31 19:18:16 +02:00
|
|
|
complete_field(h, nmc_fields_con_active_details_groups);
|
2018-04-26 20:28:02 +02:00
|
|
|
complete_field(h, metagen_device_status);
|
2018-04-26 21:28:14 +02:00
|
|
|
complete_field(h, metagen_device_detail_general);
|
2018-04-27 17:28:43 +02:00
|
|
|
complete_field(h, metagen_device_detail_connections);
|
2018-04-29 19:13:57 +02:00
|
|
|
complete_field(h, metagen_device_detail_capabilities);
|
2018-04-30 09:00:08 +02:00
|
|
|
complete_field(h, metagen_device_detail_wired_properties);
|
|
|
|
|
complete_field(h, metagen_device_detail_wifi_properties);
|
|
|
|
|
complete_field(h, metagen_device_detail_wimax_properties);
|
cli: split tracking of meta data out of NmcOutputField
When generating output data, nmcli iterates over a list of
property-descriptors (nmc_fields_ip4_config), creates an intermediate
array (output_data) and finally prints it.
However, previously both the meta data (nmc_fields_ip4_config) and
the intermediate format use the same type NmcOutputField. This means,
certain fields are relevant to describe a property, and other fields
are output/formatting fields.
Split this up. Now, the meta data is tracked in form of an NMMetaAbstractInfo
lists. This separates the information about properties from intermediate steps
during creation of the output.
Note that currently functions like print_ip4_config() still have all the
knowledge about how to generate the output. That is wrong, instead, the
meta data (NMMetaAbstractInfo) should describe how to create the output
and then all those functions could be replaced. This means, later we want
to add more knowledge to the NMMetaAbstractInfo, so it is important to
keep them separate from NmcOutputField.
2017-03-31 19:18:16 +02:00
|
|
|
complete_field(h, nmc_fields_dev_wifi_list);
|
|
|
|
|
complete_field(h, nmc_fields_dev_wimax_list);
|
|
|
|
|
complete_field(h, nmc_fields_dev_show_master_prop);
|
|
|
|
|
complete_field(h, nmc_fields_dev_show_team_prop);
|
|
|
|
|
complete_field(h, nmc_fields_dev_show_vlan_prop);
|
|
|
|
|
complete_field(h, nmc_fields_dev_show_bluetooth);
|
|
|
|
|
complete_field(h, nmc_fields_dev_show_sections);
|
|
|
|
|
complete_field(h, nmc_fields_dev_lldp_list);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-03-28 13:34:10 +02:00
|
|
|
for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++)
|
|
|
|
|
complete_field_setting(h, i);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-05-15 19:19:49 +02:00
|
|
|
g_hash_table_foreach(h, complete_one, (gpointer) &option_with_value[0]);
|
2016-07-27 16:24:30 +02:00
|
|
|
g_hash_table_destroy(h);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 19:19:49 +02:00
|
|
|
static void
|
|
|
|
|
complete_option_with_value(const char *option, const char *prefix, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
const char *candidate;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-05-15 19:19:49 +02:00
|
|
|
va_start(args, prefix);
|
|
|
|
|
while ((candidate = va_arg(args, const char *))) {
|
|
|
|
|
if (!*prefix || matches(prefix, candidate)) {
|
|
|
|
|
if (option != prefix) {
|
|
|
|
|
/* value prefix was not a standalone argument,
|
|
|
|
|
* it was part of --option=<value> argument.
|
|
|
|
|
* Repeat the part leading to "=". */
|
|
|
|
|
g_print("%s=", option);
|
|
|
|
|
}
|
|
|
|
|
g_print("%s\n", candidate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-25 09:52:30 -08:00
|
|
|
static void
|
2016-07-06 16:25:02 +02:00
|
|
|
usage(void)
|
2010-02-25 09:52:30 -08:00
|
|
|
{
|
2016-07-06 16:25:02 +02:00
|
|
|
g_printerr(_(
|
|
|
|
|
"Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }\n"
|
2014-09-19 16:04:40 -04:00
|
|
|
"\n"
|
|
|
|
|
"OPTIONS\n"
|
2018-10-31 13:46:30 +01:00
|
|
|
" -a, --ask ask for missing parameters\n"
|
|
|
|
|
" -c, --colors auto|yes|no whether to use colors in output\n"
|
|
|
|
|
" -e, --escape yes|no escape columns separators in values\n"
|
|
|
|
|
" -f, --fields <field,...>|all|common specify fields to output\n"
|
|
|
|
|
" -g, --get-values <field,...>|all|common shortcut for -m tabular -t -f\n"
|
|
|
|
|
" -h, --help print this help\n"
|
|
|
|
|
" -m, --mode tabular|multiline output mode\n"
|
|
|
|
|
" -o, --overview overview mode\n"
|
|
|
|
|
" -p, --pretty pretty output\n"
|
|
|
|
|
" -s, --show-secrets allow displaying passwords\n"
|
|
|
|
|
" -t, --terse terse output\n"
|
2019-01-11 17:07:03 -02:00
|
|
|
" -v, --version show program version\n"
|
2018-10-31 13:46:30 +01:00
|
|
|
" -w, --wait <seconds> set timeout waiting for finishing operations\n"
|
2014-09-19 16:04:40 -04:00
|
|
|
"\n"
|
|
|
|
|
"OBJECT\n"
|
|
|
|
|
" g[eneral] NetworkManager's general status and operations\n"
|
|
|
|
|
" n[etworking] overall networking control\n"
|
|
|
|
|
" r[adio] NetworkManager radio switches\n"
|
|
|
|
|
" c[onnection] NetworkManager's connections\n"
|
|
|
|
|
" d[evice] devices managed by NetworkManager\n"
|
2014-10-30 15:45:43 +01:00
|
|
|
" a[gent] NetworkManager secret agent or polkit agent\n"
|
2016-02-03 10:04:47 +01:00
|
|
|
" m[onitor] monitor NetworkManager changes\n"
|
2016-07-06 16:25:02 +02:00
|
|
|
"\n"));
|
2010-02-25 09:52:30 -08:00
|
|
|
}
|
|
|
|
|
|
2017-05-15 19:19:49 +02:00
|
|
|
static gboolean
|
2020-04-05 13:05:58 +02:00
|
|
|
matches_arg(NmCli *nmc, int *argc, const char *const **argv, const char *pattern, char **arg)
|
2017-05-15 19:19:49 +02:00
|
|
|
{
|
2020-04-05 13:05:58 +02:00
|
|
|
gs_free char *opt_free = NULL;
|
|
|
|
|
const char * opt = (*argv)[0];
|
|
|
|
|
gs_free char *arg_tmp = NULL;
|
|
|
|
|
const char * s;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-05 13:05:58 +02:00
|
|
|
nm_assert(opt);
|
|
|
|
|
nm_assert(opt[0] == '-');
|
|
|
|
|
nm_assert(!arg || !*arg);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-05-15 19:19:49 +02:00
|
|
|
if (nmc->return_value != NMC_RESULT_SUCCESS) {
|
|
|
|
|
/* Don't process further matches if there has been an error. */
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-05-15 19:19:49 +02:00
|
|
|
if (opt[1] == '-') {
|
|
|
|
|
/* We know one '-' was already seen by the caller.
|
|
|
|
|
* Skip it if there's a second one*/
|
|
|
|
|
opt++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (arg) {
|
|
|
|
|
/* If there's a "=" separator, replace it with NUL so that matches()
|
2020-07-04 11:37:01 +03:00
|
|
|
* works and consider the part after it to be the argument's value. */
|
2020-04-05 13:05:58 +02:00
|
|
|
s = strchr(opt, '=');
|
|
|
|
|
if (s) {
|
|
|
|
|
opt = nm_strndup_a(300, opt, s - opt, &opt_free);
|
|
|
|
|
arg_tmp = g_strdup(&s[1]);
|
2017-05-15 19:19:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-05 13:05:58 +02:00
|
|
|
if (!matches(opt, pattern))
|
2017-05-15 19:19:49 +02:00
|
|
|
return FALSE;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-05 13:05:58 +02:00
|
|
|
if (arg) {
|
|
|
|
|
if (arg_tmp)
|
|
|
|
|
*arg = g_steal_pointer(&arg_tmp);
|
|
|
|
|
else {
|
|
|
|
|
/* We need a value, but the option didn't contain a "=<value>" part.
|
|
|
|
|
* Proceed to the next argument. */
|
|
|
|
|
if (*argc <= 1) {
|
|
|
|
|
g_string_printf(nmc->return_text,
|
|
|
|
|
_("Error: missing argument for '%s' option."),
|
|
|
|
|
opt);
|
|
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
(*argc)--;
|
|
|
|
|
(*argv)++;
|
|
|
|
|
*arg = g_strdup(*argv[0]);
|
2017-05-15 19:19:49 +02:00
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-15 19:19:49 +02:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
/*************************************************************************************/
|
|
|
|
|
|
2018-03-29 12:25:14 +02:00
|
|
|
typedef enum {
|
|
|
|
|
NMC_USE_COLOR_AUTO,
|
|
|
|
|
NMC_USE_COLOR_YES,
|
|
|
|
|
NMC_USE_COLOR_NO,
|
|
|
|
|
} NmcColorOption;
|
|
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
static char *
|
|
|
|
|
check_colors_construct_filename(const char *base_dir,
|
|
|
|
|
const char *name,
|
|
|
|
|
const char *term,
|
|
|
|
|
const char *type)
|
2018-03-29 12:11:07 +02:00
|
|
|
{
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
return g_strdup_printf("%s/terminal-colors.d/%s%s%s%s%s",
|
|
|
|
|
base_dir,
|
|
|
|
|
name ? name : "",
|
|
|
|
|
term ? "@" : "",
|
|
|
|
|
term ? term : "",
|
|
|
|
|
(name || term) ? "." : "",
|
|
|
|
|
type);
|
2018-03-29 12:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
static NmcColorOption
|
|
|
|
|
check_colors_check_enabled_one_file(const char *base_dir, const char *name, const char *term)
|
2018-03-29 12:25:14 +02:00
|
|
|
{
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
gs_free char *filename_e = NULL;
|
|
|
|
|
gs_free char *filename_d = NULL;
|
2018-03-29 12:11:07 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
filename_e = check_colors_construct_filename(base_dir, name, term, "enable");
|
|
|
|
|
if (g_file_test(filename_e, G_FILE_TEST_EXISTS))
|
|
|
|
|
return NMC_USE_COLOR_YES;
|
|
|
|
|
|
|
|
|
|
filename_d = check_colors_construct_filename(base_dir, name, term, "disable");
|
|
|
|
|
if (g_file_test(filename_d, G_FILE_TEST_EXISTS))
|
|
|
|
|
return NMC_USE_COLOR_NO;
|
|
|
|
|
|
|
|
|
|
return NMC_USE_COLOR_AUTO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
check_colors_check_palette_one_file(const char *base_dir, const char *name, const char *term)
|
|
|
|
|
{
|
2020-10-08 14:28:47 +02:00
|
|
|
static const char *const extensions[] = {
|
|
|
|
|
"scheme",
|
|
|
|
|
"schem",
|
|
|
|
|
};
|
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS(extensions); i++) {
|
|
|
|
|
gs_free char *filename = NULL;
|
|
|
|
|
char * contents;
|
|
|
|
|
|
|
|
|
|
filename = check_colors_construct_filename(base_dir, name, term, extensions[i]);
|
|
|
|
|
if (g_file_get_contents(filename, &contents, NULL, NULL))
|
|
|
|
|
return contents;
|
|
|
|
|
}
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
check_colors_check_enabled(const char *base_dir_1,
|
|
|
|
|
const char *base_dir_2,
|
|
|
|
|
const char *name,
|
|
|
|
|
const char *term)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
if (term && strchr(term, '/'))
|
|
|
|
|
term = NULL;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
#define CHECK_AND_RETURN(cmd) \
|
|
|
|
|
G_STMT_START \
|
|
|
|
|
{ \
|
|
|
|
|
NmcColorOption _color_option; \
|
|
|
|
|
\
|
|
|
|
|
_color_option = (cmd); \
|
|
|
|
|
if (_color_option != NMC_USE_COLOR_AUTO) \
|
|
|
|
|
return _color_option == NMC_USE_COLOR_YES; \
|
|
|
|
|
} \
|
|
|
|
|
G_STMT_END
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
|
const char *base_dir = (i == 0 ? base_dir_1 : base_dir_2);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
if (!base_dir)
|
|
|
|
|
continue;
|
|
|
|
|
if (name && term)
|
|
|
|
|
CHECK_AND_RETURN(check_colors_check_enabled_one_file(base_dir, name, term));
|
|
|
|
|
if (name)
|
|
|
|
|
CHECK_AND_RETURN(check_colors_check_enabled_one_file(base_dir, name, NULL));
|
|
|
|
|
if (term)
|
|
|
|
|
CHECK_AND_RETURN(check_colors_check_enabled_one_file(base_dir, NULL, term));
|
|
|
|
|
if (TRUE)
|
|
|
|
|
CHECK_AND_RETURN(check_colors_check_enabled_one_file(base_dir, NULL, NULL));
|
2018-03-29 12:11:07 +02:00
|
|
|
}
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
#undef CHECK_AND_RETURN
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
check_colors_check_palette(const char *base_dir_1,
|
|
|
|
|
const char *base_dir_2,
|
|
|
|
|
const char *name,
|
|
|
|
|
const char *term)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (term && strchr(term, '/'))
|
|
|
|
|
term = NULL;
|
|
|
|
|
|
|
|
|
|
#define CHECK_AND_RETURN(cmd) \
|
|
|
|
|
G_STMT_START \
|
|
|
|
|
{ \
|
|
|
|
|
char *_palette; \
|
|
|
|
|
\
|
|
|
|
|
_palette = (cmd); \
|
|
|
|
|
if (_palette) \
|
|
|
|
|
return _palette; \
|
|
|
|
|
} \
|
|
|
|
|
G_STMT_END
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
|
const char *base_dir = (i == 0 ? base_dir_1 : base_dir_2);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
if (!base_dir)
|
|
|
|
|
continue;
|
|
|
|
|
if (name && term)
|
|
|
|
|
CHECK_AND_RETURN(check_colors_check_palette_one_file(base_dir, name, term));
|
|
|
|
|
if (name)
|
|
|
|
|
CHECK_AND_RETURN(check_colors_check_palette_one_file(base_dir, name, NULL));
|
|
|
|
|
if (term)
|
|
|
|
|
CHECK_AND_RETURN(check_colors_check_palette_one_file(base_dir, NULL, term));
|
|
|
|
|
if (TRUE)
|
|
|
|
|
CHECK_AND_RETURN(check_colors_check_palette_one_file(base_dir, NULL, NULL));
|
|
|
|
|
}
|
|
|
|
|
#undef CHECK_AND_RETURN
|
|
|
|
|
return NULL;
|
2018-03-29 12:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
static gboolean
|
|
|
|
|
check_colors(NmcColorOption color_option, char **out_palette_str)
|
2018-03-29 12:11:07 +02:00
|
|
|
{
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
const char * base_dir_1, *base_dir_2;
|
|
|
|
|
const char *const NAME = "nmcli";
|
2018-06-21 17:08:05 +02:00
|
|
|
const char * term;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
*out_palette_str = NULL;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
if (!NM_IN_SET(color_option, NMC_USE_COLOR_AUTO, NMC_USE_COLOR_YES)) {
|
|
|
|
|
/* nothing to do. Colors are disabled. */
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2019-10-07 17:42:17 +02:00
|
|
|
if (color_option == NMC_USE_COLOR_AUTO && g_getenv("NO_COLOR")) {
|
|
|
|
|
/* https://no-color.org/ */
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
term = g_getenv("TERM");
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
if (color_option == NMC_USE_COLOR_AUTO) {
|
|
|
|
|
if (nm_streq0(term, "dumb") || !isatty(STDOUT_FILENO))
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
base_dir_1 = g_get_user_config_dir();
|
|
|
|
|
base_dir_2 = "" SYSCONFDIR;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
if (base_dir_1) {
|
|
|
|
|
if (nm_streq(base_dir_1, base_dir_2) || !g_file_test(base_dir_1, G_FILE_TEST_EXISTS))
|
|
|
|
|
base_dir_1 = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (!g_file_test(base_dir_2, G_FILE_TEST_EXISTS))
|
|
|
|
|
base_dir_2 = NULL;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
if (color_option == NMC_USE_COLOR_AUTO
|
|
|
|
|
&& !check_colors_check_enabled(base_dir_1, base_dir_2, NAME, term))
|
|
|
|
|
return FALSE;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
*out_palette_str = check_colors_check_palette(base_dir_1, base_dir_2, NAME, term);
|
|
|
|
|
return TRUE;
|
2018-03-29 12:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-08 13:07:54 +02:00
|
|
|
static NM_UTILS_STRING_TABLE_LOOKUP_DEFINE(
|
|
|
|
|
_resolve_color_alias,
|
|
|
|
|
const char *,
|
|
|
|
|
{ nm_assert(name); },
|
|
|
|
|
{ return NULL; },
|
|
|
|
|
{"black", "30"},
|
|
|
|
|
{"blink", "5"},
|
|
|
|
|
{"blue", "34"},
|
|
|
|
|
{"bold", "1"},
|
|
|
|
|
{"brown", "33"},
|
|
|
|
|
{"cyan", "36"},
|
|
|
|
|
{"darkgray", "90"},
|
|
|
|
|
{"gray", "37"},
|
|
|
|
|
{"green", "32"},
|
|
|
|
|
{"halfbright", "2"},
|
|
|
|
|
{"lightblue", "94"},
|
|
|
|
|
{"lightcyan", "96"},
|
|
|
|
|
{"lightgray", "97"},
|
|
|
|
|
{"lightgreen", "92"},
|
|
|
|
|
{"lightmagenta", "95"},
|
|
|
|
|
{"lightred", "91"},
|
|
|
|
|
{"magenta", "35"},
|
|
|
|
|
{"red", "31"},
|
|
|
|
|
{"reset", "0"},
|
|
|
|
|
{"reverse", "7"},
|
|
|
|
|
{"underscore", "4"},
|
|
|
|
|
{"white", "1;37"},
|
|
|
|
|
{"yellow", "33" /* well, yellow */}, );
|
2018-03-29 12:11:07 +02:00
|
|
|
|
2020-10-08 12:55:37 +02:00
|
|
|
static NM_UTILS_STRING_TABLE_LOOKUP_DEFINE(
|
|
|
|
|
_nm_meta_color_from_name,
|
|
|
|
|
NMMetaColor,
|
|
|
|
|
{ nm_assert(name); },
|
|
|
|
|
{ return NM_META_COLOR_NONE; },
|
|
|
|
|
{"connection-activated", NM_META_COLOR_CONNECTION_ACTIVATED},
|
|
|
|
|
{"connection-activating", NM_META_COLOR_CONNECTION_ACTIVATING},
|
|
|
|
|
{"connection-disconnecting", NM_META_COLOR_CONNECTION_DISCONNECTING},
|
|
|
|
|
{"connection-external", NM_META_COLOR_CONNECTION_EXTERNAL},
|
|
|
|
|
{"connection-invisible", NM_META_COLOR_CONNECTION_INVISIBLE},
|
|
|
|
|
{"connection-unknown", NM_META_COLOR_CONNECTION_UNKNOWN},
|
|
|
|
|
{"connectivity-full", NM_META_COLOR_CONNECTIVITY_FULL},
|
|
|
|
|
{"connectivity-limited", NM_META_COLOR_CONNECTIVITY_LIMITED},
|
|
|
|
|
{"connectivity-none", NM_META_COLOR_CONNECTIVITY_NONE},
|
|
|
|
|
{"connectivity-portal", NM_META_COLOR_CONNECTIVITY_PORTAL},
|
|
|
|
|
{"connectivity-unknown", NM_META_COLOR_CONNECTIVITY_UNKNOWN},
|
|
|
|
|
{"device-activated", NM_META_COLOR_DEVICE_ACTIVATED},
|
|
|
|
|
{"device-activating", NM_META_COLOR_DEVICE_ACTIVATING},
|
|
|
|
|
{"device-disabled", NM_META_COLOR_DEVICE_DISABLED},
|
|
|
|
|
{"device-disconnected", NM_META_COLOR_DEVICE_DISCONNECTED},
|
|
|
|
|
{"device-external", NM_META_COLOR_DEVICE_EXTERNAL},
|
|
|
|
|
{"device-firmware-missing", NM_META_COLOR_DEVICE_FIRMWARE_MISSING},
|
|
|
|
|
{"device-plugin-missing", NM_META_COLOR_DEVICE_PLUGIN_MISSING},
|
|
|
|
|
{"device-unavailable", NM_META_COLOR_DEVICE_UNAVAILABLE},
|
|
|
|
|
{"device-unknown", NM_META_COLOR_DEVICE_UNKNOWN},
|
|
|
|
|
{"disabled", NM_META_COLOR_DISABLED},
|
|
|
|
|
{"enabled", NM_META_COLOR_ENABLED},
|
|
|
|
|
{"manager-running", NM_META_COLOR_MANAGER_RUNNING},
|
|
|
|
|
{"manager-starting", NM_META_COLOR_MANAGER_STARTING},
|
|
|
|
|
{"manager-stopped", NM_META_COLOR_MANAGER_STOPPED},
|
|
|
|
|
{"permission-auth", NM_META_COLOR_PERMISSION_AUTH},
|
|
|
|
|
{"permission-no", NM_META_COLOR_PERMISSION_NO},
|
|
|
|
|
{"permission-unknown", NM_META_COLOR_PERMISSION_UNKNOWN},
|
|
|
|
|
{"permission-yes", NM_META_COLOR_PERMISSION_YES},
|
|
|
|
|
{"prompt", NM_META_COLOR_PROMPT},
|
|
|
|
|
{"state-asleep", NM_META_COLOR_STATE_ASLEEP},
|
|
|
|
|
{"state-connected-global", NM_META_COLOR_STATE_CONNECTED_GLOBAL},
|
|
|
|
|
{"state-connected-local", NM_META_COLOR_STATE_CONNECTED_LOCAL},
|
|
|
|
|
{"state-connected-site", NM_META_COLOR_STATE_CONNECTED_SITE},
|
|
|
|
|
{"state-connecting", NM_META_COLOR_STATE_CONNECTING},
|
|
|
|
|
{"state-disconnected", NM_META_COLOR_STATE_DISCONNECTED},
|
|
|
|
|
{"state-disconnecting", NM_META_COLOR_STATE_DISCONNECTING},
|
|
|
|
|
{"state-unknown", NM_META_COLOR_STATE_UNKNOWN},
|
|
|
|
|
{"wifi-signal-excellent", NM_META_COLOR_WIFI_SIGNAL_EXCELLENT},
|
|
|
|
|
{"wifi-signal-fair", NM_META_COLOR_WIFI_SIGNAL_FAIR},
|
|
|
|
|
{"wifi-signal-good", NM_META_COLOR_WIFI_SIGNAL_GOOD},
|
|
|
|
|
{"wifi-signal-poor", NM_META_COLOR_WIFI_SIGNAL_POOR},
|
|
|
|
|
{"wifi-signal-unknown", NM_META_COLOR_WIFI_SIGNAL_UNKNOWN}, );
|
|
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
static gboolean
|
2020-10-08 12:33:16 +02:00
|
|
|
parse_color_scheme(char *palette_buffer, NmcColorPalette *out_palette, GError **error)
|
2018-03-29 12:11:07 +02:00
|
|
|
{
|
2020-10-08 12:33:16 +02:00
|
|
|
char *p = palette_buffer;
|
|
|
|
|
|
|
|
|
|
nm_assert(out_palette);
|
|
|
|
|
|
|
|
|
|
*out_palette = (NmcColorPalette) _NMC_COLOR_PALETTE_INIT();
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
/* This reads through the raw color scheme file contents, identifying the
|
|
|
|
|
* color names and sequences, putting in terminating NULs in place, so that
|
|
|
|
|
* pointers into the buffer can readily be used as strings in the palette. */
|
|
|
|
|
while (1) {
|
2020-10-08 12:55:37 +02:00
|
|
|
NMMetaColor name_idx;
|
|
|
|
|
const char *name;
|
|
|
|
|
const char *color;
|
|
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
/* Leading whitespace. */
|
|
|
|
|
while (nm_utils_is_separator(*p) || *p == '\n')
|
|
|
|
|
p++;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
if (*p == '\0')
|
|
|
|
|
break;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
/* Comments. */
|
|
|
|
|
if (*p == '#') {
|
|
|
|
|
while (*p != '\n' && *p != '\0')
|
|
|
|
|
p++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
/* Color name. */
|
|
|
|
|
name = p;
|
|
|
|
|
while (g_ascii_isgraph(*p))
|
|
|
|
|
p++;
|
|
|
|
|
if (*p == '\0') {
|
|
|
|
|
g_set_error(error, NMCLI_ERROR, 0, _("Unexpected end of file following '%s'\n"), name);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
/* Separating whitespace. */
|
|
|
|
|
if (!nm_utils_is_separator(*p)) {
|
|
|
|
|
*p = '\0';
|
|
|
|
|
g_set_error(error, NMCLI_ERROR, 0, _("Expected whitespace following '%s'\n"), name);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
while (nm_utils_is_separator(*p)) {
|
|
|
|
|
*p = '\0';
|
|
|
|
|
p++;
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
/* Color sequence. */
|
|
|
|
|
color = p;
|
|
|
|
|
if (!g_ascii_isgraph(*p)) {
|
|
|
|
|
g_set_error(error, NMCLI_ERROR, 0, _("Expected a value for '%s'\n"), name);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
while (g_ascii_isgraph(*p))
|
|
|
|
|
p++;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
/* Trailing whitespace. */
|
|
|
|
|
while (nm_utils_is_separator(*p)) {
|
|
|
|
|
*p = '\0';
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
if (*p != '\0') {
|
|
|
|
|
if (*p != '\n') {
|
|
|
|
|
g_set_error(error,
|
|
|
|
|
NMCLI_ERROR,
|
|
|
|
|
0,
|
|
|
|
|
_("Expected a line break following '%s'\n"),
|
|
|
|
|
color);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
*p = '\0';
|
|
|
|
|
p++;
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-10-08 12:55:37 +02:00
|
|
|
name_idx = _nm_meta_color_from_name(name);
|
|
|
|
|
if (name_idx == NM_META_COLOR_NONE) {
|
2018-03-29 12:11:07 +02:00
|
|
|
g_debug("Ignoring an unrecognized color: '%s'\n", name);
|
2020-10-08 12:55:37 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 12:33:16 +02:00
|
|
|
out_palette->ansi_seq[name_idx] = _resolve_color_alias(color) ?: color;
|
2018-03-29 12:11:07 +02:00
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2020-10-08 12:33:16 +02:00
|
|
|
set_colors(NmcColorOption color_option,
|
|
|
|
|
bool * out_use_colors,
|
|
|
|
|
char ** out_palette_buffer,
|
|
|
|
|
NmcColorPalette *out_palette)
|
2018-03-29 12:11:07 +02:00
|
|
|
{
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
gs_free char *palette_str = NULL;
|
2018-06-21 17:05:04 +02:00
|
|
|
gboolean use_colors;
|
2020-10-08 12:33:16 +02:00
|
|
|
gboolean palette_set = FALSE;
|
|
|
|
|
|
|
|
|
|
nm_assert(out_use_colors);
|
|
|
|
|
nm_assert(out_palette);
|
|
|
|
|
nm_assert(out_palette_buffer && !*out_palette_buffer);
|
2018-03-29 12:11:07 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
use_colors = check_colors(color_option, &palette_str);
|
2018-03-29 12:11:07 +02:00
|
|
|
|
2018-06-21 17:05:04 +02:00
|
|
|
*out_use_colors = use_colors;
|
2020-10-08 12:33:16 +02:00
|
|
|
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
if (use_colors && palette_str) {
|
2020-10-08 12:33:16 +02:00
|
|
|
gs_free_error GError *error = NULL;
|
|
|
|
|
NmcColorPalette palette;
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
|
2020-10-08 12:33:16 +02:00
|
|
|
if (!parse_color_scheme(palette_str, &palette, &error))
|
2018-03-29 12:11:07 +02:00
|
|
|
g_debug("Error parsing color scheme: %s", error->message);
|
2020-10-08 12:33:16 +02:00
|
|
|
else {
|
cli: rework check-colors to first evaluate enabled/disabled
With --color=auto, coloring is enabled based on the .enable/.disable
termcolors files.
Likewise, when we enable coloring, we parse the color palette from the
.schem termcolors files.
The termcolors files are searched by finding the best match depending
on the terminal and application name. Note, that if we find a matching
file like "nmcli@xterm.enable" we still allow loading the palette from
a less specific file like "nmcli.schem" and vice versa. That was already
done before.
Previously, the search was done by calling several layers of functions, and having
in/out arguments "color_option" and "p_palette_buffer". in/out paramters
here seems confusing to me, as they are state that gets modified and carried
along.
Instead, rework the functions to clearly separate between input
and output arguments.
Also, in the auto-case, check_colors() now first determines whether
coloring is enabled, before even starting loading the palette.
This avoids loading the palette until we are sure that we need it.
2018-06-21 18:00:31 +02:00
|
|
|
*out_palette_buffer = g_steal_pointer(&palette_str);
|
2020-10-08 12:33:16 +02:00
|
|
|
*out_palette = palette;
|
|
|
|
|
palette_set = TRUE;
|
|
|
|
|
}
|
2018-03-29 12:11:07 +02:00
|
|
|
}
|
2020-10-08 12:33:16 +02:00
|
|
|
|
|
|
|
|
if (!palette_set)
|
|
|
|
|
*out_palette = (NmcColorPalette) _NMC_COLOR_PALETTE_INIT();
|
2018-03-29 12:25:14 +02:00
|
|
|
}
|
|
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
/*************************************************************************************/
|
|
|
|
|
|
2016-08-31 21:04:33 +02:00
|
|
|
static gboolean
|
2020-04-05 13:05:58 +02:00
|
|
|
process_command_line(NmCli *nmc, int argc, char **argv_orig)
|
2010-02-25 09:52:30 -08:00
|
|
|
{
|
2020-04-05 11:47:07 +02:00
|
|
|
static const NMCCommand nmcli_cmds[] = {
|
|
|
|
|
{"general", nmc_command_func_general, NULL, FALSE, FALSE},
|
|
|
|
|
{"monitor", nmc_command_func_monitor, NULL, TRUE, FALSE},
|
|
|
|
|
{"networking", nmc_command_func_networking, NULL, FALSE, FALSE},
|
|
|
|
|
{"radio", nmc_command_func_radio, NULL, FALSE, FALSE},
|
|
|
|
|
{"connection", nmc_command_func_connection, NULL, FALSE, FALSE},
|
|
|
|
|
{"device", nmc_command_func_device, NULL, FALSE, FALSE},
|
|
|
|
|
{"agent", nmc_command_func_agent, NULL, FALSE, FALSE},
|
|
|
|
|
{NULL, nmc_command_func_overview, usage, TRUE, TRUE},
|
|
|
|
|
};
|
2018-03-29 12:25:14 +02:00
|
|
|
NmcColorOption colors = NMC_USE_COLOR_AUTO;
|
2020-04-05 13:05:58 +02:00
|
|
|
const char * base;
|
|
|
|
|
const char *const *argv;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-05 13:05:58 +02:00
|
|
|
base = strrchr(argv_orig[0], '/');
|
2010-02-25 09:52:30 -08:00
|
|
|
if (base == NULL)
|
2020-04-05 13:05:58 +02:00
|
|
|
base = argv_orig[0];
|
2010-02-25 09:52:30 -08:00
|
|
|
else
|
|
|
|
|
base++;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-05 13:05:58 +02:00
|
|
|
if (argc > 1 && nm_streq(argv_orig[1], "--complete-args")) {
|
2016-06-15 13:12:39 +02:00
|
|
|
nmc->complete = TRUE;
|
2020-04-05 13:05:58 +02:00
|
|
|
argv_orig[1] = argv_orig[0];
|
|
|
|
|
argc--;
|
|
|
|
|
argv_orig++;
|
2016-06-15 13:12:39 +02:00
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-05 13:05:58 +02:00
|
|
|
argv = (const char *const *) argv_orig;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-03-30 16:09:46 +02:00
|
|
|
next_arg(nmc, &argc, &argv, NULL);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2010-02-25 09:52:30 -08:00
|
|
|
/* parse options */
|
2016-07-27 15:16:21 +02:00
|
|
|
while (argc) {
|
2020-04-05 13:05:58 +02:00
|
|
|
gs_free char *value = NULL;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-05-15 19:19:49 +02:00
|
|
|
if (argv[0][0] != '-')
|
2010-02-25 09:52:30 -08:00
|
|
|
break;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2016-07-27 15:21:06 +02:00
|
|
|
if (argc == 1 && nmc->complete) {
|
2018-02-27 16:28:28 +01:00
|
|
|
nmc_complete_strings(argv[0],
|
|
|
|
|
"--terse",
|
|
|
|
|
"--pretty",
|
|
|
|
|
"--mode",
|
|
|
|
|
"--overview",
|
|
|
|
|
"--colors",
|
|
|
|
|
"--escape",
|
|
|
|
|
"--fields",
|
|
|
|
|
"--nocheck",
|
|
|
|
|
"--get-values",
|
2019-04-12 15:40:01 +02:00
|
|
|
"--wait",
|
|
|
|
|
"--version",
|
|
|
|
|
"--help");
|
2016-07-27 15:21:06 +02:00
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-05-15 19:19:49 +02:00
|
|
|
if (argv[0][1] == '-' && argv[0][2] == '\0') {
|
2016-07-27 15:16:21 +02:00
|
|
|
/* '--' ends options */
|
2017-05-15 19:19:49 +02:00
|
|
|
next_arg(nmc, &argc, &argv, NULL);
|
|
|
|
|
break;
|
2016-07-27 15:16:21 +02:00
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-02-27 16:28:28 +01:00
|
|
|
if (matches_arg(nmc, &argc, &argv, "-overview", NULL)) {
|
|
|
|
|
nmc->nmc_config_mutable.overview = TRUE;
|
|
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-terse", NULL)) {
|
2017-03-30 12:45:41 +02:00
|
|
|
if (nmc->nmc_config.print_output == NMC_PRINT_TERSE) {
|
2010-03-24 13:42:47 +01:00
|
|
|
g_string_printf(nmc->return_text,
|
|
|
|
|
_("Error: Option '--terse' is specified the second time."));
|
|
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2017-03-30 12:45:41 +02:00
|
|
|
} else if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY) {
|
2010-03-24 13:42:47 +01:00
|
|
|
g_string_printf(
|
|
|
|
|
nmc->return_text,
|
|
|
|
|
_("Error: Option '--terse' is mutually exclusive with '--pretty'."));
|
|
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2010-03-18 15:39:15 +01:00
|
|
|
} else
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.print_output = NMC_PRINT_TERSE;
|
2017-05-15 19:19:49 +02:00
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-pretty", NULL)) {
|
2017-03-30 12:45:41 +02:00
|
|
|
if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY) {
|
2010-03-24 13:42:47 +01:00
|
|
|
g_string_printf(nmc->return_text,
|
|
|
|
|
_("Error: Option '--pretty' is specified the second time."));
|
|
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2017-03-30 12:45:41 +02:00
|
|
|
} else if (nmc->nmc_config.print_output == NMC_PRINT_TERSE) {
|
2010-03-24 13:42:47 +01:00
|
|
|
g_string_printf(
|
|
|
|
|
nmc->return_text,
|
|
|
|
|
_("Error: Option '--pretty' is mutually exclusive with '--terse'."));
|
|
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2010-03-18 15:39:15 +01:00
|
|
|
} else
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.print_output = NMC_PRINT_PRETTY;
|
2017-05-15 19:19:49 +02:00
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-mode", &value)) {
|
2010-04-06 16:22:01 +02:00
|
|
|
nmc->mode_specified = TRUE;
|
2016-07-27 16:24:30 +02:00
|
|
|
if (argc == 1 && nmc->complete)
|
2017-05-15 19:19:49 +02:00
|
|
|
complete_option_with_value(argv[0], value, "tabular", "multiline", NULL);
|
|
|
|
|
if (matches(value, "tabular"))
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.multiline_output = FALSE;
|
2017-05-15 19:19:49 +02:00
|
|
|
else if (matches(value, "multiline"))
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.multiline_output = TRUE;
|
2010-04-06 16:22:01 +02:00
|
|
|
else {
|
2017-05-15 19:19:49 +02:00
|
|
|
g_string_printf(nmc->return_text,
|
|
|
|
|
_("Error: '%s' is not a valid argument for '%s' option."),
|
|
|
|
|
value,
|
|
|
|
|
argv[0]);
|
2015-02-16 12:18:42 +01:00
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2015-02-16 12:18:42 +01:00
|
|
|
}
|
2017-05-15 19:19:49 +02:00
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-colors", &value)) {
|
2016-07-27 16:24:30 +02:00
|
|
|
if (argc == 1 && nmc->complete)
|
2017-05-15 19:19:49 +02:00
|
|
|
complete_option_with_value(argv[0], value, "yes", "no", "auto", NULL);
|
|
|
|
|
if (matches(value, "auto"))
|
2018-03-29 12:25:14 +02:00
|
|
|
colors = NMC_USE_COLOR_AUTO;
|
2017-05-15 19:19:49 +02:00
|
|
|
else if (matches(value, "yes"))
|
2018-03-29 12:25:14 +02:00
|
|
|
colors = NMC_USE_COLOR_YES;
|
2017-05-15 19:19:49 +02:00
|
|
|
else if (matches(value, "no"))
|
2018-03-29 12:25:14 +02:00
|
|
|
colors = NMC_USE_COLOR_NO;
|
2015-02-16 12:18:42 +01:00
|
|
|
else {
|
2017-05-15 19:19:49 +02:00
|
|
|
g_string_printf(nmc->return_text,
|
|
|
|
|
_("Error: '%s' is not valid argument for '%s' option."),
|
|
|
|
|
value,
|
|
|
|
|
argv[0]);
|
2010-03-24 13:42:47 +01:00
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2010-03-18 15:39:15 +01:00
|
|
|
}
|
2017-05-15 19:19:49 +02:00
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-escape", &value)) {
|
2016-07-27 16:24:30 +02:00
|
|
|
if (argc == 1 && nmc->complete)
|
2017-05-15 19:19:49 +02:00
|
|
|
complete_option_with_value(argv[0], value, "yes", "no", NULL);
|
|
|
|
|
if (matches(value, "yes"))
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.escape_values = TRUE;
|
2017-05-15 19:19:49 +02:00
|
|
|
else if (matches(value, "no"))
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.escape_values = FALSE;
|
2010-03-18 15:39:15 +01:00
|
|
|
else {
|
2017-05-15 19:19:49 +02:00
|
|
|
g_string_printf(nmc->return_text,
|
|
|
|
|
_("Error: '%s' is not valid argument for '%s' option."),
|
|
|
|
|
value,
|
|
|
|
|
argv[0]);
|
2010-03-24 13:42:47 +01:00
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2010-03-18 15:39:15 +01:00
|
|
|
}
|
2017-05-15 19:19:49 +02:00
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-fields", &value)) {
|
2016-07-27 16:24:30 +02:00
|
|
|
if (argc == 1 && nmc->complete)
|
2017-05-15 19:19:49 +02:00
|
|
|
complete_fields(argv[0], value);
|
|
|
|
|
nmc->required_fields = g_strdup(value);
|
|
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-get-values", &value)) {
|
2017-01-17 14:20:56 +01:00
|
|
|
if (argc == 1 && nmc->complete)
|
2017-05-15 19:19:49 +02:00
|
|
|
complete_fields(argv[0], value);
|
|
|
|
|
nmc->required_fields = g_strdup(value);
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.print_output = NMC_PRINT_TERSE;
|
2018-03-28 22:30:44 +02:00
|
|
|
/* We want fixed tabular mode here, but just set the mode specified and rely on defaults:
|
|
|
|
|
* in this way we allow use of "-m multiline" to swap the output mode also if placed
|
2017-01-17 14:20:56 +01:00
|
|
|
* before the "-g <field>" option (-g may be still more practical and easy to remember than -t -f).
|
2020-09-29 09:02:29 +02:00
|
|
|
*/
|
2017-01-17 14:20:56 +01:00
|
|
|
nmc->mode_specified = TRUE;
|
2017-05-15 19:19:49 +02:00
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-nocheck", NULL)) {
|
2016-04-18 18:50:56 +02:00
|
|
|
/* ignore for backward compatibility */
|
2017-05-15 19:19:49 +02:00
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-wait", &value)) {
|
2013-05-14 12:37:12 +02:00
|
|
|
unsigned long timeout;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-05-15 19:19:49 +02:00
|
|
|
if (!nmc_string_to_uint(value, TRUE, 0, G_MAXINT, &timeout)) {
|
|
|
|
|
g_string_printf(nmc->return_text, _("Error: '%s' is not a valid timeout."), value);
|
2013-05-14 12:37:12 +02:00
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2013-05-14 12:37:12 +02:00
|
|
|
}
|
|
|
|
|
nmc->timeout = (int) timeout;
|
2017-05-15 19:19:49 +02:00
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-version", NULL)) {
|
2016-07-27 15:16:21 +02:00
|
|
|
if (!nmc->complete)
|
|
|
|
|
g_print(_("nmcli tool, version %s\n"), NMCLI_VERSION);
|
2010-02-25 09:52:30 -08:00
|
|
|
return NMC_RESULT_SUCCESS;
|
2017-05-15 19:19:49 +02:00
|
|
|
} else if (matches_arg(nmc, &argc, &argv, "-help", NULL)) {
|
2016-07-27 15:16:21 +02:00
|
|
|
if (!nmc->complete)
|
|
|
|
|
usage();
|
2010-02-25 09:52:30 -08:00
|
|
|
return NMC_RESULT_SUCCESS;
|
|
|
|
|
} else {
|
2017-05-15 19:19:49 +02:00
|
|
|
if (nmc->return_value == NMC_RESULT_SUCCESS) {
|
|
|
|
|
g_string_printf(nmc->return_text,
|
|
|
|
|
_("Error: Option '%s' is unknown, try 'nmcli -help'."),
|
|
|
|
|
argv[0]);
|
|
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
|
|
|
|
}
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2010-02-25 09:52:30 -08:00
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-03-30 16:09:46 +02:00
|
|
|
next_arg(nmc, &argc, &argv, NULL);
|
2010-02-25 09:52:30 -08:00
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-02-27 16:28:28 +01:00
|
|
|
/* Ignore --overview when fields are set explicitly */
|
|
|
|
|
if (nmc->required_fields)
|
|
|
|
|
nmc->nmc_config_mutable.overview = FALSE;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-06-21 17:05:04 +02:00
|
|
|
set_colors(colors,
|
|
|
|
|
&nmc->nmc_config_mutable.use_colors,
|
|
|
|
|
&nmc->palette_buffer,
|
2020-10-08 12:33:16 +02:00
|
|
|
&nmc->nmc_config_mutable.palette);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2016-07-01 21:47:01 +02:00
|
|
|
/* Now run the requested command */
|
2016-08-31 21:04:33 +02:00
|
|
|
nmc_do_cmd(nmc, nmcli_cmds, *argv, argc, argv);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2016-08-31 21:04:33 +02:00
|
|
|
return TRUE;
|
2010-02-25 09:52:30 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-16 08:30:22 +02:00
|
|
|
static gboolean nmcli_sigint = FALSE;
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nmc_seen_sigint(void)
|
|
|
|
|
{
|
2016-09-02 16:16:34 +02:00
|
|
|
return nmcli_sigint;
|
2014-06-16 08:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nmc_clear_sigint(void)
|
|
|
|
|
{
|
|
|
|
|
nmcli_sigint = FALSE;
|
2014-06-20 14:50:18 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-02 16:16:34 +02:00
|
|
|
void
|
|
|
|
|
nmc_exit(void)
|
2014-06-16 08:30:22 +02:00
|
|
|
{
|
2016-09-02 16:16:34 +02:00
|
|
|
tcsetattr(STDIN_FILENO, TCSADRAIN, &termios_orig);
|
|
|
|
|
nmc_cleanup_readline();
|
|
|
|
|
exit(1);
|
2014-06-16 08:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-09 18:18:48 +02:00
|
|
|
static gboolean
|
2016-09-02 16:16:34 +02:00
|
|
|
signal_handler(gpointer user_data)
|
2010-02-25 09:52:30 -08:00
|
|
|
{
|
2016-09-02 16:16:34 +02:00
|
|
|
int signo = GPOINTER_TO_INT(user_data);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2016-09-02 16:16:34 +02:00
|
|
|
switch (signo) {
|
|
|
|
|
case SIGINT:
|
|
|
|
|
if (nmc_get_in_readline()) {
|
|
|
|
|
nmcli_sigint = TRUE;
|
|
|
|
|
} else {
|
2018-01-23 14:59:29 +01:00
|
|
|
nm_cli.return_value = 0x80 + signo;
|
|
|
|
|
g_string_printf(nm_cli.return_text,
|
|
|
|
|
_("Error: nmcli terminated by signal %s (%d)"),
|
|
|
|
|
strsignal(signo),
|
|
|
|
|
signo);
|
2016-09-02 16:16:34 +02:00
|
|
|
g_main_loop_quit(loop);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SIGTERM:
|
2018-01-23 14:59:29 +01:00
|
|
|
nm_cli.return_value = 0x80 + signo;
|
|
|
|
|
g_string_printf(nm_cli.return_text,
|
|
|
|
|
_("Error: nmcli terminated by signal %s (%d)"),
|
|
|
|
|
strsignal(signo),
|
|
|
|
|
signo);
|
2016-09-02 16:16:34 +02:00
|
|
|
nmc_exit();
|
|
|
|
|
break;
|
2013-09-09 18:18:48 +02:00
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2016-09-02 16:16:34 +02:00
|
|
|
return G_SOURCE_CONTINUE;
|
2010-02-25 09:52:30 -08:00
|
|
|
}
|
|
|
|
|
|
2018-10-09 13:08:24 +02:00
|
|
|
void
|
2020-04-02 13:59:53 +02:00
|
|
|
nm_cli_spawn_pager(const NmcConfig *nmc_config, NmcPagerData *pager_data)
|
2018-10-09 13:08:24 +02:00
|
|
|
{
|
2020-04-02 13:59:53 +02:00
|
|
|
if (pager_data->pid != 0)
|
2018-10-09 13:08:24 +02:00
|
|
|
return;
|
2020-04-02 13:59:53 +02:00
|
|
|
pager_data->pid = nmc_terminal_spawn_pager(nmc_config);
|
2018-10-09 13:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
2010-02-25 09:52:30 -08:00
|
|
|
static void
|
|
|
|
|
nmc_cleanup(NmCli *nmc)
|
|
|
|
|
{
|
2017-05-15 17:23:51 +02:00
|
|
|
pid_t ret;
|
|
|
|
|
|
|
|
|
|
g_clear_object(&nmc->client);
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2019-01-22 12:52:49 +01:00
|
|
|
if (nmc->return_text)
|
|
|
|
|
g_string_free(g_steal_pointer(&nmc->return_text), TRUE);
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2014-10-06 15:25:53 +02:00
|
|
|
if (nmc->secret_agent) {
|
2019-01-22 12:51:10 +01:00
|
|
|
nm_secret_agent_old_unregister(NM_SECRET_AGENT_OLD(nmc->secret_agent), NULL, NULL);
|
2019-01-22 12:52:49 +01:00
|
|
|
g_clear_object(&nmc->secret_agent);
|
2014-10-06 15:25:53 +02:00
|
|
|
}
|
2019-01-22 12:52:49 +01:00
|
|
|
|
|
|
|
|
nm_clear_pointer(&nmc->pwds_hash, g_hash_table_destroy);
|
2014-10-06 15:25:53 +02:00
|
|
|
|
2018-04-24 13:24:32 +02:00
|
|
|
nm_clear_g_free(&nmc->required_fields);
|
2014-10-30 11:25:55 +01:00
|
|
|
|
2020-04-02 13:59:53 +02:00
|
|
|
if (nmc->pager_data.pid != 0) {
|
|
|
|
|
pid_t pid = nm_steal_int(&nmc->pager_data.pid);
|
|
|
|
|
|
2017-05-15 17:23:51 +02:00
|
|
|
fclose(stdout);
|
|
|
|
|
fclose(stderr);
|
|
|
|
|
do {
|
2020-04-02 13:59:53 +02:00
|
|
|
ret = waitpid(pid, NULL, 0);
|
2017-05-15 17:23:51 +02:00
|
|
|
} while (ret == -1 && errno == EINTR);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-29 12:11:07 +02:00
|
|
|
nm_clear_g_free(&nmc->palette_buffer);
|
|
|
|
|
|
2014-10-30 11:25:55 +01:00
|
|
|
nmc_polkit_agent_fini(nmc);
|
2010-02-25 09:52:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
/* Set locale to use environment variables */
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
|
|
|
|
|
#ifdef GETTEXT_PACKAGE
|
|
|
|
|
/* Set i18n stuff */
|
2018-05-30 10:51:23 +02:00
|
|
|
bindtextdomain(GETTEXT_PACKAGE, NMLOCALEDIR);
|
2010-02-25 09:52:30 -08:00
|
|
|
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
|
|
|
|
|
textdomain(GETTEXT_PACKAGE);
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-10-30 11:25:55 +01:00
|
|
|
/* Save terminal settings */
|
|
|
|
|
tcgetattr(STDIN_FILENO, &termios_orig);
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2018-03-28 22:30:44 +02:00
|
|
|
nm_cli.return_text = g_string_new(_("Success"));
|
2016-08-31 21:04:33 +02:00
|
|
|
loop = g_main_loop_new(NULL, FALSE);
|
2018-01-23 14:59:29 +01:00
|
|
|
|
|
|
|
|
g_unix_signal_add(SIGTERM, signal_handler, GINT_TO_POINTER(SIGTERM));
|
|
|
|
|
g_unix_signal_add(SIGINT, signal_handler, GINT_TO_POINTER(SIGINT));
|
|
|
|
|
|
2016-08-31 21:04:33 +02:00
|
|
|
if (process_command_line(&nm_cli, argc, argv))
|
|
|
|
|
g_main_loop_run(loop);
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2016-07-12 15:43:56 +02:00
|
|
|
if (nm_cli.complete) {
|
|
|
|
|
/* Remove error statuses from command completion runs. */
|
|
|
|
|
if (nm_cli.return_value < NMC_RESULT_COMPLETE_FILE)
|
|
|
|
|
nm_cli.return_value = NMC_RESULT_SUCCESS;
|
|
|
|
|
} else if (nm_cli.return_value != NMC_RESULT_SUCCESS) {
|
|
|
|
|
/* Print result descripting text */
|
2014-09-19 16:04:40 -04:00
|
|
|
g_printerr("%s\n", nm_cli.return_text->str);
|
2010-02-25 09:52:30 -08:00
|
|
|
}
|
|
|
|
|
|
2014-04-16 18:23:50 +02:00
|
|
|
nmc_cleanup(&nm_cli);
|
2020-05-12 13:47:32 +02:00
|
|
|
g_main_loop_unref(loop);
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2014-04-16 18:23:50 +02:00
|
|
|
return nm_cli.return_value;
|
2010-02-25 09:52:30 -08:00
|
|
|
}
|