2010-02-25 09:52:30 -08:00
|
|
|
/* nmcli - command-line tool to control NetworkManager
|
|
|
|
|
*
|
|
|
|
|
* Jiri Klimes <jklimes@redhat.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
*
|
2015-02-16 12:18:42 +01:00
|
|
|
* Copyright 2010 - 2015 Red Hat, Inc.
|
2010-02-25 09:52:30 -08:00
|
|
|
*/
|
|
|
|
|
|
2016-02-19 14:57:48 +01:00
|
|
|
#include "nm-default.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 <string.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
|
|
|
|
2017-03-28 12:16:31 +02:00
|
|
|
#include "nm-client-utils.h"
|
|
|
|
|
|
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"
|
2014-09-30 15:53:51 +02:00
|
|
|
#include "general.h"
|
2014-10-30 15:45:43 +01:00
|
|
|
#include "agent.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
|
|
|
|
2014-04-16 18:23:50 +02:00
|
|
|
/* Global NmCli object */
|
|
|
|
|
NmCli nm_cli;
|
2010-02-25 09:52:30 -08: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
|
|
|
|
2017-02-03 17:21:21 +01:00
|
|
|
NM_CACHED_QUARK_FCN ("nmcli-error-quark", nmcli_error_quark)
|
|
|
|
|
|
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;
|
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,
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
const char *prefix = user_data;
|
|
|
|
|
const char *name = key;
|
|
|
|
|
const char *last;
|
|
|
|
|
|
|
|
|
|
last = strrchr (prefix, ',');
|
|
|
|
|
if (last)
|
|
|
|
|
last++;
|
|
|
|
|
else
|
|
|
|
|
last = prefix;
|
|
|
|
|
|
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)) {
|
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
|
|
|
|
|
complete_fields (const char *prefix)
|
|
|
|
|
{
|
2017-03-28 13:34:10 +02:00
|
|
|
guint i;
|
2016-07-27 16:24:30 +02:00
|
|
|
GHashTable *h;
|
|
|
|
|
|
|
|
|
|
h = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
|
|
|
|
|
|
2017-04-06 15:14:23 +02:00
|
|
|
complete_field (h, metagen_ip4_config);
|
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_dhcp4_config);
|
|
|
|
|
complete_field (h, nmc_fields_ip6_config);
|
|
|
|
|
complete_field (h, nmc_fields_dhcp6_config);
|
|
|
|
|
complete_field (h, nmc_fields_con_show);
|
|
|
|
|
complete_field (h, nmc_fields_con_active_details_general);
|
|
|
|
|
complete_field (h, nmc_fields_con_active_details_vpn);
|
|
|
|
|
complete_field (h, nmc_fields_con_active_details_groups);
|
|
|
|
|
complete_field (h, nmc_fields_dev_status);
|
|
|
|
|
complete_field (h, nmc_fields_dev_show_general);
|
|
|
|
|
complete_field (h, nmc_fields_dev_show_connections);
|
|
|
|
|
complete_field (h, nmc_fields_dev_show_cap);
|
|
|
|
|
complete_field (h, nmc_fields_dev_show_wired_prop);
|
|
|
|
|
complete_field (h, nmc_fields_dev_show_wifi_prop);
|
|
|
|
|
complete_field (h, nmc_fields_dev_show_wimax_prop);
|
|
|
|
|
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);
|
2016-07-27 16:24:30 +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);
|
2016-07-27 16:24:30 +02:00
|
|
|
|
|
|
|
|
g_hash_table_foreach (h, complete_one, (gpointer) prefix);
|
|
|
|
|
g_hash_table_destroy (h);
|
|
|
|
|
}
|
|
|
|
|
|
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"
|
2017-01-17 14:20:56 +01:00
|
|
|
" -t[erse] terse output\n"
|
|
|
|
|
" -p[retty] pretty output\n"
|
|
|
|
|
" -m[ode] tabular|multiline output mode\n"
|
|
|
|
|
" -c[olors] auto|yes|no whether to use colors in output\n"
|
|
|
|
|
" -f[ields] <field1,field2,...>|all|common specify fields to output\n"
|
|
|
|
|
" -g[et-values] <field1,field2,...>|all|common shortcut for -m tabular -t -f\n"
|
|
|
|
|
" -e[scape] yes|no escape columns separators in values\n"
|
|
|
|
|
" -a[sk] ask for missing parameters\n"
|
|
|
|
|
" -s[how-secrets] allow displaying passwords\n"
|
|
|
|
|
" -w[ait] <seconds> set timeout waiting for finishing operations\n"
|
|
|
|
|
" -v[ersion] show program version\n"
|
|
|
|
|
" -h[elp] print this help\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
|
|
|
}
|
|
|
|
|
|
2016-06-23 12:18:52 +02:00
|
|
|
static const NMCCommand nmcli_cmds[] = {
|
2016-08-31 20:52:48 +02:00
|
|
|
{ "general", do_general, NULL, FALSE, FALSE },
|
|
|
|
|
{ "monitor", do_monitor, NULL, TRUE, FALSE },
|
2016-12-09 13:21:41 +01:00
|
|
|
{ "networking", do_networking, NULL, FALSE, FALSE },
|
2016-08-31 20:52:48 +02:00
|
|
|
{ "radio", do_radio, NULL, FALSE, FALSE },
|
|
|
|
|
{ "connection", do_connections, NULL, FALSE, FALSE },
|
|
|
|
|
{ "device", do_devices, NULL, FALSE, FALSE },
|
|
|
|
|
{ "agent", do_agent, NULL, FALSE, FALSE },
|
|
|
|
|
{ NULL, do_overview, usage, TRUE, TRUE },
|
2010-02-25 09:52:30 -08:00
|
|
|
};
|
|
|
|
|
|
2016-08-31 21:04:33 +02:00
|
|
|
static gboolean
|
|
|
|
|
process_command_line (NmCli *nmc, int argc, char **argv)
|
2010-02-25 09:52:30 -08:00
|
|
|
{
|
|
|
|
|
char *base;
|
|
|
|
|
|
|
|
|
|
base = strrchr (argv[0], '/');
|
|
|
|
|
if (base == NULL)
|
|
|
|
|
base = argv[0];
|
|
|
|
|
else
|
|
|
|
|
base++;
|
2016-06-15 13:12:39 +02:00
|
|
|
if (argc > 1 && nm_streq (argv[1], "--complete-args")) {
|
|
|
|
|
nmc->complete = TRUE;
|
|
|
|
|
argv[1] = argv[0];
|
2017-03-30 16:09:46 +02:00
|
|
|
next_arg (nmc, &argc, &argv, NULL);
|
2016-06-15 13:12:39 +02:00
|
|
|
}
|
2017-03-30 16:09:46 +02:00
|
|
|
next_arg (nmc, &argc, &argv, NULL);
|
2016-07-27 15:16:21 +02:00
|
|
|
|
2010-02-25 09:52:30 -08:00
|
|
|
/* parse options */
|
2016-07-27 15:16:21 +02:00
|
|
|
while (argc) {
|
|
|
|
|
char *opt = argv[0];
|
2010-02-25 09:52:30 -08:00
|
|
|
if (opt[0] != '-')
|
|
|
|
|
break;
|
2016-07-27 15:16:21 +02:00
|
|
|
|
2016-07-27 15:21:06 +02:00
|
|
|
if (argc == 1 && nmc->complete) {
|
|
|
|
|
nmc_complete_strings (opt, "--terse", "--pretty", "--mode", "--colors", "--escape",
|
2017-03-24 14:35:56 +01:00
|
|
|
"--fields", "--nocheck", "--get-values",
|
|
|
|
|
"--wait", "--version", "--help", NULL);
|
2016-07-27 15:21:06 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-27 15:16:21 +02:00
|
|
|
if (opt[1] == '-') {
|
2010-02-25 09:52:30 -08:00
|
|
|
opt++;
|
2016-07-27 15:16:21 +02:00
|
|
|
/* '--' ends options */
|
|
|
|
|
if (opt[1] == '\0') {
|
2017-03-30 16:09:46 +02:00
|
|
|
next_arg (nmc, &argc, &argv, NULL);
|
2016-07-27 15:16:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 (matches (opt, "-terse")) {
|
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;
|
2010-03-18 15:39:15 +01:00
|
|
|
}
|
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;
|
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
|
|
|
} else if (matches (opt, "-pretty")) {
|
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;
|
2010-03-18 15:39:15 +01:00
|
|
|
}
|
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;
|
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
|
|
|
} else if (matches (opt, "-mode")) {
|
2010-04-06 16:22:01 +02:00
|
|
|
nmc->mode_specified = TRUE;
|
2017-03-29 12:02:14 +02:00
|
|
|
argc--;
|
|
|
|
|
argv++;
|
2017-03-24 14:00:25 +01:00
|
|
|
if (!argc) {
|
2016-10-17 16:00:10 +02:00
|
|
|
g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
|
2010-04-06 16:22:01 +02:00
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2010-04-06 16:22:01 +02:00
|
|
|
}
|
2016-07-27 16:24:30 +02:00
|
|
|
if (argc == 1 && nmc->complete)
|
|
|
|
|
nmc_complete_strings (argv[0], "tabular", "multiline", NULL);
|
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 (matches (argv[0], "tabular"))
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.multiline_output = FALSE;
|
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
|
|
|
else if (matches (argv[0], "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 {
|
2016-10-17 16:00:10 +02:00
|
|
|
g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), argv[0], opt);
|
2010-04-06 16:22:01 +02:00
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
2016-08-31 21:04:33 +02:00
|
|
|
return FALSE;
|
2010-04-06 16:22:01 +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
|
|
|
} else if (matches (opt, "-colors")) {
|
2017-03-29 12:02:14 +02:00
|
|
|
argc--;
|
|
|
|
|
argv++;
|
2017-03-24 14:00:25 +01:00
|
|
|
if (!argc) {
|
2016-10-17 16:00:10 +02:00
|
|
|
g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
|
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
|
|
|
}
|
2016-07-27 16:24:30 +02:00
|
|
|
if (argc == 1 && nmc->complete)
|
|
|
|
|
nmc_complete_strings (argv[0], "yes", "no", "auto", NULL);
|
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 (matches (argv[0], "auto"))
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_AUTO;
|
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
|
|
|
else if (matches (argv[0], "yes"))
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_YES;
|
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
|
|
|
else if (matches (argv[0], "no"))
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_NO;
|
2015-02-16 12:18:42 +01:00
|
|
|
else {
|
2016-10-17 16:00:10 +02:00
|
|
|
g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), argv[0], opt);
|
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
|
|
|
}
|
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
|
|
|
} else if (matches (opt, "-escape")) {
|
2017-03-29 12:02:14 +02:00
|
|
|
argc--;
|
|
|
|
|
argv++;
|
2017-03-24 14:00:25 +01:00
|
|
|
if (!argc) {
|
2016-10-17 16:00:10 +02:00
|
|
|
g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
|
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
|
|
|
}
|
2016-07-27 16:24:30 +02:00
|
|
|
if (argc == 1 && nmc->complete)
|
|
|
|
|
nmc_complete_strings (argv[0], "yes", "no", NULL);
|
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 (matches (argv[0], "yes"))
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.escape_values = TRUE;
|
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
|
|
|
else if (matches (argv[0], "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 {
|
2016-10-17 16:00:10 +02:00
|
|
|
g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), argv[0], opt);
|
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
|
|
|
}
|
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
|
|
|
} else if (matches (opt, "-fields")) {
|
2017-03-29 12:02:14 +02:00
|
|
|
argc--;
|
|
|
|
|
argv++;
|
2017-03-24 14:00:25 +01:00
|
|
|
if (!argc) {
|
2016-10-17 16:00:10 +02:00
|
|
|
g_string_printf (nmc->return_text, _("Error: fields for '%s' options are missing."), opt);
|
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
|
|
|
}
|
2016-07-27 16:24:30 +02:00
|
|
|
if (argc == 1 && nmc->complete)
|
|
|
|
|
complete_fields (argv[0]);
|
2016-07-27 15:16:21 +02:00
|
|
|
nmc->required_fields = g_strdup (argv[0]);
|
2017-01-17 14:20:56 +01:00
|
|
|
} else if (matches (opt, "-get-values")) {
|
2017-03-29 12:02:14 +02:00
|
|
|
argc--;
|
|
|
|
|
argv++;
|
2017-03-24 14:00:25 +01:00
|
|
|
if (!argc) {
|
2017-01-17 14:20:56 +01:00
|
|
|
g_string_printf (nmc->return_text, _("Error: fields for '%s' options are missing."), opt);
|
|
|
|
|
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
if (argc == 1 && nmc->complete)
|
|
|
|
|
complete_fields (argv[0]);
|
|
|
|
|
nmc->required_fields = g_strdup (argv[0]);
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.print_output = NMC_PRINT_TERSE;
|
2017-01-17 14:20:56 +01:00
|
|
|
/* We want fixed tabular mode here, but just set the mode specified and rely on the initialization
|
|
|
|
|
* in nmc_init: in this way we allow use of "-m multiline" to swap the output mode also if placed
|
|
|
|
|
* before the "-g <field>" option (-g may be still more practical and easy to remember than -t -f).
|
|
|
|
|
*/
|
|
|
|
|
nmc->mode_specified = TRUE;
|
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
|
|
|
} else if (matches (opt, "-nocheck")) {
|
2016-04-18 18:50:56 +02:00
|
|
|
/* ignore for backward compatibility */
|
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
|
|
|
} else if (matches (opt, "-wait")) {
|
2013-05-14 12:37:12 +02:00
|
|
|
unsigned long timeout;
|
2017-03-29 12:02:14 +02:00
|
|
|
|
|
|
|
|
argc--;
|
|
|
|
|
argv++;
|
2017-03-24 14:00:25 +01:00
|
|
|
if (!argc) {
|
2016-10-17 16:00:10 +02:00
|
|
|
g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
|
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
|
|
|
}
|
2016-07-27 15:16:21 +02:00
|
|
|
if (!nmc_string_to_uint (argv[0], TRUE, 0, G_MAXINT, &timeout)) {
|
2016-10-17 16:00:10 +02:00
|
|
|
g_string_printf (nmc->return_text, _("Error: '%s' is not a valid timeout for '%s' option."),
|
|
|
|
|
argv[0], opt);
|
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;
|
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
|
|
|
} else if (matches (opt, "-version")) {
|
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;
|
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
|
|
|
} else if (matches (opt, "-help")) {
|
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 {
|
2010-03-24 13:42:47 +01:00
|
|
|
g_string_printf (nmc->return_text, _("Error: Option '%s' is unknown, try 'nmcli -help'."), opt);
|
|
|
|
|
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
|
|
|
}
|
2017-03-30 16:09:46 +02:00
|
|
|
next_arg (nmc, &argc, &argv, NULL);
|
2010-02-25 09:52:30 -08: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);
|
|
|
|
|
|
|
|
|
|
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);
|
2013-09-09 18:18:48 +02:00
|
|
|
|
2016-09-02 16:16:34 +02:00
|
|
|
switch (signo) {
|
|
|
|
|
case SIGINT:
|
|
|
|
|
if (nmc_get_in_readline ()) {
|
|
|
|
|
nmcli_sigint = TRUE;
|
|
|
|
|
} else {
|
|
|
|
|
g_print (_("Error: nmcli terminated by signal %s (%d)\n"),
|
|
|
|
|
strsignal (signo),
|
|
|
|
|
signo);
|
|
|
|
|
g_main_loop_quit (loop);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SIGTERM:
|
|
|
|
|
g_print (_("Error: nmcli terminated by signal %s (%d)\n"),
|
|
|
|
|
strsignal (signo), signo);
|
|
|
|
|
nmc_exit ();
|
|
|
|
|
break;
|
2013-09-09 18:18:48 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-02 16:16:34 +02:00
|
|
|
return G_SOURCE_CONTINUE;
|
2010-02-25 09:52:30 -08:00
|
|
|
}
|
|
|
|
|
|
2014-08-21 13:19:53 -04:00
|
|
|
static void
|
|
|
|
|
nmc_convert_strv_to_string (const GValue *src_value, GValue *dest_value)
|
|
|
|
|
{
|
|
|
|
|
char **strings;
|
|
|
|
|
|
|
|
|
|
strings = g_value_get_boxed (src_value);
|
|
|
|
|
if (strings)
|
|
|
|
|
g_value_take_string (dest_value, g_strjoinv (",", strings));
|
|
|
|
|
else
|
|
|
|
|
g_value_set_string (dest_value, "");
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:40:08 -04:00
|
|
|
static void
|
|
|
|
|
nmc_convert_string_hash_to_string (const GValue *src_value, GValue *dest_value)
|
|
|
|
|
{
|
|
|
|
|
GHashTable *hash;
|
|
|
|
|
GHashTableIter iter;
|
|
|
|
|
const char *key, *value;
|
|
|
|
|
GString *string;
|
|
|
|
|
|
|
|
|
|
hash = (GHashTable *) g_value_get_boxed (src_value);
|
|
|
|
|
|
|
|
|
|
string = g_string_new (NULL);
|
|
|
|
|
if (hash) {
|
|
|
|
|
g_hash_table_iter_init (&iter, hash);
|
|
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) {
|
|
|
|
|
if (string->len)
|
|
|
|
|
g_string_append_c (string, ',');
|
|
|
|
|
g_string_append_printf (string, "%s=%s", key, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_value_take_string (dest_value, g_string_free (string, FALSE));
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-26 10:42:11 -04:00
|
|
|
static void
|
|
|
|
|
nmc_convert_bytes_to_string (const GValue *src_value, GValue *dest_value)
|
|
|
|
|
{
|
|
|
|
|
GBytes *bytes;
|
|
|
|
|
const guint8 *array;
|
|
|
|
|
gsize length;
|
|
|
|
|
GString *printable;
|
|
|
|
|
guint i = 0;
|
|
|
|
|
|
|
|
|
|
bytes = g_value_get_boxed (src_value);
|
|
|
|
|
|
|
|
|
|
printable = g_string_new ("[");
|
|
|
|
|
|
|
|
|
|
if (bytes) {
|
|
|
|
|
array = g_bytes_get_data (bytes, &length);
|
|
|
|
|
while (i < MIN (length, 35)) {
|
|
|
|
|
if (i > 0)
|
|
|
|
|
g_string_append_c (printable, ' ');
|
|
|
|
|
g_string_append_printf (printable, "0x%02X", array[i++]);
|
|
|
|
|
}
|
|
|
|
|
if (i < length)
|
|
|
|
|
g_string_append (printable, " ... ");
|
|
|
|
|
}
|
|
|
|
|
g_string_append_c (printable, ']');
|
|
|
|
|
|
|
|
|
|
g_value_take_string (dest_value, g_string_free (printable, FALSE));
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-21 13:19:53 -04:00
|
|
|
static void
|
|
|
|
|
nmc_value_transforms_register (void)
|
|
|
|
|
{
|
|
|
|
|
g_value_register_transform_func (G_TYPE_STRV,
|
|
|
|
|
G_TYPE_STRING,
|
|
|
|
|
nmc_convert_strv_to_string);
|
2014-06-24 17:40:08 -04:00
|
|
|
|
|
|
|
|
/* This depends on the fact that all of the hash-table-valued properties
|
|
|
|
|
* in libnm-core are string->string.
|
|
|
|
|
*/
|
|
|
|
|
g_value_register_transform_func (G_TYPE_HASH_TABLE,
|
|
|
|
|
G_TYPE_STRING,
|
|
|
|
|
nmc_convert_string_hash_to_string);
|
2014-06-26 10:42:11 -04:00
|
|
|
|
|
|
|
|
g_value_register_transform_func (G_TYPE_BYTES,
|
|
|
|
|
G_TYPE_STRING,
|
|
|
|
|
nmc_convert_bytes_to_string);
|
2014-08-21 13:19:53 -04:00
|
|
|
}
|
|
|
|
|
|
2010-02-25 09:52:30 -08:00
|
|
|
/* Initialize NmCli structure - set default values */
|
|
|
|
|
static void
|
|
|
|
|
nmc_init (NmCli *nmc)
|
|
|
|
|
{
|
|
|
|
|
nmc->client = NULL;
|
|
|
|
|
|
|
|
|
|
nmc->return_value = NMC_RESULT_SUCCESS;
|
|
|
|
|
nmc->return_text = g_string_new (_("Success"));
|
|
|
|
|
|
2013-05-14 12:37:12 +02:00
|
|
|
nmc->timeout = -1;
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2014-10-06 15:25:53 +02:00
|
|
|
nmc->secret_agent = NULL;
|
2014-10-14 15:53:34 +02:00
|
|
|
nmc->pwds_hash = NULL;
|
2014-10-30 11:25:55 +01:00
|
|
|
nmc->pk_listener = NULL;
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2015-03-27 12:39:17 +01:00
|
|
|
nmc->should_wait = 0;
|
2010-07-29 16:16:20 +02:00
|
|
|
nmc->nowait_flag = TRUE;
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.print_output = NMC_PRINT_NORMAL;
|
|
|
|
|
nmc->nmc_config_mutable.multiline_output = FALSE;
|
2010-04-06 16:22:01 +02:00
|
|
|
nmc->mode_specified = FALSE;
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.escape_values = TRUE;
|
2010-03-18 15:39:15 +01:00
|
|
|
nmc->required_fields = NULL;
|
2012-11-21 13:10:31 +01:00
|
|
|
nmc->ask = FALSE;
|
2016-06-15 13:12:39 +02:00
|
|
|
nmc->complete = FALSE;
|
2017-04-06 15:26:23 +02:00
|
|
|
nmc->nmc_config_mutable.show_secrets = FALSE;
|
2017-03-30 12:45:41 +02:00
|
|
|
nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_AUTO;
|
|
|
|
|
nmc->nmc_config_mutable.in_editor = FALSE;
|
2013-04-05 23:42:56 +02:00
|
|
|
nmc->editor_status_line = FALSE;
|
2013-07-23 14:25:52 +02:00
|
|
|
nmc->editor_save_confirmation = TRUE;
|
2017-04-04 15:12:00 +02:00
|
|
|
nmc->editor_prompt_color = NM_META_TERM_COLOR_NORMAL;
|
2010-02-25 09:52:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
nmc_cleanup (NmCli *nmc)
|
|
|
|
|
{
|
|
|
|
|
if (nmc->client) g_object_unref (nmc->client);
|
|
|
|
|
|
|
|
|
|
g_string_free (nmc->return_text, TRUE);
|
|
|
|
|
|
2014-10-06 15:25:53 +02:00
|
|
|
if (nmc->secret_agent) {
|
|
|
|
|
/* Destroy secret agent if we have one. */
|
2014-11-20 11:53:19 -05:00
|
|
|
nm_secret_agent_old_unregister (nmc->secret_agent, NULL, NULL);
|
2014-10-06 15:25:53 +02:00
|
|
|
g_object_unref (nmc->secret_agent);
|
|
|
|
|
}
|
2014-10-14 15:53:34 +02:00
|
|
|
if (nmc->pwds_hash)
|
|
|
|
|
g_hash_table_destroy (nmc->pwds_hash);
|
2014-10-06 15:25:53 +02:00
|
|
|
|
2010-03-18 15:39:15 +01:00
|
|
|
g_free (nmc->required_fields);
|
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 */
|
|
|
|
|
bindtextdomain (GETTEXT_PACKAGE, NMCLI_LOCALEDIR);
|
|
|
|
|
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
|
|
|
|
textdomain (GETTEXT_PACKAGE);
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-07-10 10:41:31 +02:00
|
|
|
nm_g_type_init ();
|
|
|
|
|
|
2014-10-30 11:25:55 +01:00
|
|
|
/* Save terminal settings */
|
|
|
|
|
tcgetattr (STDIN_FILENO, &termios_orig);
|
2010-02-25 09:52:30 -08:00
|
|
|
|
2016-09-02 16:16:34 +02:00
|
|
|
g_unix_signal_add (SIGTERM, signal_handler, GINT_TO_POINTER (SIGTERM));
|
|
|
|
|
g_unix_signal_add (SIGINT, signal_handler, GINT_TO_POINTER (SIGINT));
|
2014-06-16 08:30:22 +02:00
|
|
|
|
2014-08-21 13:19:53 -04:00
|
|
|
nmc_value_transforms_register ();
|
|
|
|
|
|
2014-04-16 18:23:50 +02:00
|
|
|
nmc_init (&nm_cli);
|
2016-08-31 21:04:33 +02:00
|
|
|
loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_main_loop_unref (loop);
|
2014-04-16 18:23:50 +02:00
|
|
|
nmc_cleanup (&nm_cli);
|
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
|
|
|
}
|