summary refs log tree commit diff
path: root/clients/cli/nmcli.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2017-05-11 14:55:55 +0200
committerMichael Biebl <biebl@debian.org>2017-05-11 14:55:55 +0200
commitc333f062ddcba9b35330647bf6cbd0a07f2d786e (patch)
tree257c3a0c74c09f4ad2328eab5b932806405f0c1c /clients/cli/nmcli.c
parenta222e56e103f949b148a6942e385ccca2c26d9f3 (diff)
New upstream version 1.8.0 upstream/1.8.0
Diffstat (limited to 'clients/cli/nmcli.c')
-rw-r--r--clients/cli/nmcli.c144
1 files changed, 79 insertions, 65 deletions
diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c
index 5c47e440..b8019403 100644
--- a/clients/cli/nmcli.c
+++ b/clients/cli/nmcli.c
@@ -63,6 +63,8 @@ typedef struct {
 GMainLoop *loop = NULL;
 struct termios termios_orig;
 
+NM_CACHED_QUARK_FCN ("nmcli-error-quark", nmcli_error_quark)
+
 static void
 complete_field (GHashTable *h, const char *setting, NmcOutputField field[])
 {
@@ -89,7 +91,7 @@ complete_one (gpointer key, gpointer value, gpointer user_data)
 	else
 		last = prefix;
 
-	if ((!*last && !strchr (name, '.')) || matches (last, name) == 0) {
+	if ((!*last && !strchr (name, '.')) || matches (last, name)) {
 		g_print ("%.*s%s%s\n", (int)(last-prefix), prefix, name,
 		                       strcmp (last, name) == 0 ? "," : "");
 	}
@@ -129,12 +131,12 @@ complete_fields (const char *prefix)
 	complete_field (h, NULL, nmc_fields_dev_lldp_list);
 
 	complete_field (h, "connection", nmc_fields_setting_connection);
-	complete_field (h, "wired", nmc_fields_setting_wired);
-	complete_field (h, "8021X", nmc_fields_setting_8021X);
-	complete_field (h, "wireless", nmc_fields_setting_wireless);
-	complete_field (h, "wireless_security", nmc_fields_setting_wireless_security);
-	complete_field (h, "ip4-config", nmc_fields_setting_ip4_config);
-	complete_field (h, "ip6-config", nmc_fields_setting_ip6_config);
+	complete_field (h, "802-3-ethernet", nmc_fields_setting_wired);
+	complete_field (h, "802-1x", nmc_fields_setting_8021X);
+	complete_field (h, "802-11-wireless", nmc_fields_setting_wireless);
+	complete_field (h, "802-11-wireless-security", nmc_fields_setting_wireless_security);
+	complete_field (h, "ipv4", nmc_fields_setting_ip4_config);
+	complete_field (h, "ipv6", nmc_fields_setting_ip6_config);
 	complete_field (h, "serial", nmc_fields_setting_serial);
 	complete_field (h, "ppp", nmc_fields_setting_ppp);
 	complete_field (h, "pppoe", nmc_fields_setting_pppoe);
@@ -142,7 +144,7 @@ complete_fields (const char *prefix)
 	complete_field (h, "gsm", nmc_fields_setting_gsm);
 	complete_field (h, "cdma", nmc_fields_setting_cdma);
 	complete_field (h, "bluetooth", nmc_fields_setting_bluetooth);
-	complete_field (h, "olpc-mesh", nmc_fields_setting_olpc_mesh);
+	complete_field (h, "802-11-olpc-mesh", nmc_fields_setting_olpc_mesh);
 	complete_field (h, "vpn", nmc_fields_setting_vpn);
 	complete_field (h, "wimax", nmc_fields_setting_wimax);
 	complete_field (h, "infiniband", nmc_fields_setting_infiniband);
@@ -151,7 +153,7 @@ complete_fields (const char *prefix)
 	complete_field (h, "bridge", nmc_fields_setting_bridge);
 	complete_field (h, "bridge-port", nmc_fields_setting_bridge_port);
 	complete_field (h, "team", nmc_fields_setting_team);
-	complete_field (h, "team0port", nmc_fields_setting_team_port);
+	complete_field (h, "team-port", nmc_fields_setting_team_port);
 	complete_field (h, "dcb", nmc_fields_setting_dcb);
 	complete_field (h, "tun", nmc_fields_setting_tun);
 	complete_field (h, "ip-tunnel", nmc_fields_setting_ip_tunnel);
@@ -163,35 +165,24 @@ complete_fields (const char *prefix)
 }
 
 
-/* Get an error quark for use with GError */
-GQuark
-nmcli_error_quark (void)
-{
-	static GQuark error_quark = 0;
-
-	if (G_UNLIKELY (error_quark == 0))
-		error_quark = g_quark_from_static_string ("nmcli-error-quark");
-
-	return error_quark;
-}
-
 static void
 usage (void)
 {
 	g_printerr (_("Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }\n"
 	              "\n"
 	              "OPTIONS\n"
-	              "  -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"
-	              "  -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"
+	              "  -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"
 	              "\n"
 	              "OBJECT\n"
 	              "  g[eneral]       NetworkManager's general status and operations\n"
@@ -228,9 +219,9 @@ process_command_line (NmCli *nmc, int argc, char **argv)
 	if (argc > 1 && nm_streq (argv[1], "--complete-args")) {
 		nmc->complete = TRUE;
 		argv[1] = argv[0];
-		argc--; argv++;
+		next_arg (nmc, &argc, &argv, NULL);
 	}
-	argc--; argv++;
+	next_arg (nmc, &argc, &argv, NULL);
 
 	/* parse options */
 	while (argc) {
@@ -240,20 +231,20 @@ process_command_line (NmCli *nmc, int argc, char **argv)
 
 		if (argc == 1 && nmc->complete) {
 			nmc_complete_strings (opt, "--terse", "--pretty", "--mode", "--colors", "--escape",
-			                           "--fields", "--nocheck", "--ask", "--show-secrets",
-			                           "--wait", "--version", "--help", NULL);
+			                           "--fields", "--nocheck", "--get-values",
+			                            "--wait", "--version", "--help", NULL);
 		}
 
 		if (opt[1] == '-') {
 			opt++;
 			/* '--' ends options */
 			if (opt[1] == '\0') {
-				argc--; argv++;
+				next_arg (nmc, &argc, &argv, NULL);
 				break;
 			}
 		}
 
-		if (matches (opt, "-terse") == 0) {
+		if (matches (opt, "-terse")) {
 			if (nmc->print_output == NMC_PRINT_TERSE) {
 				g_string_printf (nmc->return_text, _("Error: Option '--terse' is specified the second time."));
 				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
@@ -266,7 +257,7 @@ process_command_line (NmCli *nmc, int argc, char **argv)
 			}
 			else
 				nmc->print_output = NMC_PRINT_TERSE;
-		} else if (matches (opt, "-pretty") == 0) {
+		} else if (matches (opt, "-pretty")) {
 			if (nmc->print_output == NMC_PRINT_PRETTY) {
 				g_string_printf (nmc->return_text, _("Error: Option '--pretty' is specified the second time."));
 				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
@@ -279,62 +270,70 @@ process_command_line (NmCli *nmc, int argc, char **argv)
 			}
 			else
 				nmc->print_output = NMC_PRINT_PRETTY;
-		} else if (matches (opt, "-mode") == 0) {
+		} else if (matches (opt, "-mode")) {
 			nmc->mode_specified = TRUE;
-			if (next_arg (&argc, &argv) != 0) {
+			argc--;
+			argv++;
+			if (!argc) {
 				g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
 				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
 				return FALSE;
 			}
 			if (argc == 1 && nmc->complete)
 				nmc_complete_strings (argv[0], "tabular", "multiline", NULL);
-			if (matches (argv[0], "tabular") == 0)
+			if (matches (argv[0], "tabular"))
 				nmc->multiline_output = FALSE;
-			else if (matches (argv[0], "multiline") == 0)
+			else if (matches (argv[0], "multiline"))
 				nmc->multiline_output = TRUE;
 			else {
 				g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), argv[0], opt);
 				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
 				return FALSE;
 			}
-		} else if (matches (opt, "-colors") == 0) {
-			if (next_arg (&argc, &argv) != 0) {
+		} else if (matches (opt, "-colors")) {
+			argc--;
+			argv++;
+			if (!argc) {
 				g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
 				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
 				return FALSE;
 			}
 			if (argc == 1 && nmc->complete)
 				nmc_complete_strings (argv[0], "yes", "no", "auto", NULL);
-			if (matches (argv[0], "auto") == 0)
+			if (matches (argv[0], "auto"))
 				nmc->use_colors = NMC_USE_COLOR_AUTO;
-			else if (matches (argv[0], "yes") == 0)
+			else if (matches (argv[0], "yes"))
 				nmc->use_colors = NMC_USE_COLOR_YES;
-			else if (matches (argv[0], "no") == 0)
+			else if (matches (argv[0], "no"))
 				nmc->use_colors = NMC_USE_COLOR_NO;
 			else {
 				g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), argv[0], opt);
 				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
 				return FALSE;
 			}
-		} else if (matches (opt, "-escape") == 0) {
-			if (next_arg (&argc, &argv) != 0) {
+		} else if (matches (opt, "-escape")) {
+			argc--;
+			argv++;
+			if (!argc) {
 				g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
 				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
 				return FALSE;
 			}
 			if (argc == 1 && nmc->complete)
 				nmc_complete_strings (argv[0], "yes", "no", NULL);
-			if (matches (argv[0], "yes") == 0)
+			if (matches (argv[0], "yes"))
 				nmc->escape_values = TRUE;
-			else if (matches (argv[0], "no") == 0)
+			else if (matches (argv[0], "no"))
 				nmc->escape_values = FALSE;
 			else {
 				g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), argv[0], opt);
 				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
 				return FALSE;
 			}
-		} else if (matches (opt, "-fields") == 0) {
-			if (next_arg (&argc, &argv) != 0) {
+		} else if (matches (opt, "-fields")) {
+			argc--;
+			argv++;
+			if (!argc) {
 				g_string_printf (nmc->return_text, _("Error: fields for '%s' options are missing."), opt);
 				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
 				return FALSE;
@@ -342,15 +341,31 @@ process_command_line (NmCli *nmc, int argc, char **argv)
 			if (argc == 1 && nmc->complete)
 				complete_fields (argv[0]);
 			nmc->required_fields = g_strdup (argv[0]);
-		} else if (matches (opt, "-nocheck") == 0) {
+		} else if (matches (opt, "-get-values")) {
+			argc--;
+			argv++;
+			if (!argc) {
+				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]);
+			nmc->print_output = NMC_PRINT_TERSE;
+			/* 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;
+		} else if (matches (opt, "-nocheck")) {
 			/* ignore for backward compatibility */
-		} else if (matches (opt, "-ask") == 0) {
-			nmc->ask = TRUE;
-		} else if (matches (opt, "-show-secrets") == 0) {
-			nmc->show_secrets = TRUE;
-		} else if (matches (opt, "-wait") == 0) {
+		} else if (matches (opt, "-wait")) {
 			unsigned long timeout;
-			if (next_arg (&argc, &argv) != 0) {
+
+			argc--;
+			argv++;
+			if (!argc) {
 				g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
 				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
 				return FALSE;
@@ -362,11 +377,11 @@ process_command_line (NmCli *nmc, int argc, char **argv)
 				return FALSE;
 			}
 			nmc->timeout = (int) timeout;
-		} else if (matches (opt, "-version") == 0) {
+		} else if (matches (opt, "-version")) {
 			if (!nmc->complete)
 				g_print (_("nmcli tool, version %s\n"), NMCLI_VERSION);
 			return NMC_RESULT_SUCCESS;
-		} else if (matches (opt, "-help") == 0) {
+		} else if (matches (opt, "-help")) {
 			if (!nmc->complete)
 				usage ();
 			return NMC_RESULT_SUCCESS;
@@ -375,8 +390,7 @@ process_command_line (NmCli *nmc, int argc, char **argv)
 			nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
 			return FALSE;
 		}
-		argc--;
-		argv++;
+		next_arg (nmc, &argc, &argv, NULL);
 	}
 
 	/* Now run the requested command */