public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Subject: [B.A.T.M.A.N.] [PATCH v3 4/9] batctl: Parse interface arguments relative to last parsed option
Date: Tue, 18 Oct 2016 16:17:26 +0200	[thread overview]
Message-ID: <20161018141731.7970-4-sven@narfation.org> (raw)
In-Reply-To: <2403515.P9aVkiGJpp@bentobox>

Arguments may be added between "interface" and the subcommands "add" and
"del". Thus is should not be hardcoded which positions of argv the
subcommands start and instead the information from getopt should be used to
calculate the correct position.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
v3:
 - rebased on current master
v2:
 - rename "new" command to "create" as requested by Linus Luessing and
   John Harrison
---
 sys.c | 51 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 28 insertions(+), 23 deletions(-)

diff --git a/sys.c b/sys.c
index cefd53c..190fd06 100644
--- a/sys.c
+++ b/sys.c
@@ -383,6 +383,8 @@ int interface(char *mesh_iface, int argc, char **argv)
 	unsigned int ifmaster;
 	const char *long_op;
 	unsigned int cnt;
+	int rest_argc;
+	char **rest_argv;
 
 	while ((optchar = getopt(argc, argv, "h")) != -1) {
 		switch (optchar) {
@@ -395,38 +397,41 @@ int interface(char *mesh_iface, int argc, char **argv)
 		}
 	}
 
-	if (argc == 1)
+	rest_argc = argc - optind;
+	rest_argv = &argv[optind];
+
+	if (rest_argc == 0)
 		return print_interfaces(mesh_iface);
 
-	if ((strcmp(argv[1], "add") != 0) && (strcmp(argv[1], "a") != 0) &&
-	    (strcmp(argv[1], "del") != 0) && (strcmp(argv[1], "d") != 0) &&
-	    (strcmp(argv[1], "create") != 0) && (strcmp(argv[1], "c") != 0) &&
-	    (strcmp(argv[1], "destroy") != 0) && (strcmp(argv[1], "D") != 0)) {
-		fprintf(stderr, "Error - unknown argument specified: %s\n", argv[1]);
+	if ((strcmp(rest_argv[0], "add") != 0) && (strcmp(rest_argv[0], "a") != 0) &&
+	    (strcmp(rest_argv[0], "del") != 0) && (strcmp(rest_argv[0], "d") != 0) &&
+	    (strcmp(rest_argv[0], "create") != 0) && (strcmp(rest_argv[0], "c") != 0) &&
+	    (strcmp(rest_argv[0], "destroy") != 0) && (strcmp(rest_argv[0], "D") != 0)) {
+		fprintf(stderr, "Error - unknown argument specified: %s\n", rest_argv[0]);
 		interface_usage();
 		goto err;
 	}
 
-	if (strcmp(argv[1], "destroy") == 0)
-		argv[1][0] = 'D';
+	if (strcmp(rest_argv[0], "destroy") == 0)
+		rest_argv[0][0] = 'D';
 
-	switch (argv[1][0]) {
+	switch (rest_argv[0][0]) {
 	case 'a':
 	case 'd':
-		if (argc == 2) {
+		if (rest_argc == 1) {
 			fprintf(stderr,
 				"Error - missing interface name(s) after '%s'\n",
-				argv[1]);
+				rest_argv[0]);
 			interface_usage();
 			goto err;
 		}
 		break;
 	case 'c':
 	case 'D':
-		if (argc != 2) {
+		if (rest_argc != 1) {
 			fprintf(stderr,
 				"Error - extra parameter after '%s'\n",
-				argv[1]);
+				rest_argv[0]);
 			interface_usage();
 			goto err;
 		}
@@ -435,7 +440,7 @@ int interface(char *mesh_iface, int argc, char **argv)
 		break;
 	}
 
-	switch (argv[1][0]) {
+	switch (rest_argv[0][0]) {
 	case 'c':
 		ret = create_interface(mesh_iface);
 		if (ret < 0) {
@@ -460,7 +465,7 @@ int interface(char *mesh_iface, int argc, char **argv)
 
 	/* get index of batman-adv interface - or try to create it */
 	ifmaster = if_nametoindex(mesh_iface);
-	if (!ifmaster && argv[1][0] == 'a') {
+	if (!ifmaster && rest_argv[0][0] == 'a') {
 		ret = create_interface(mesh_iface);
 		if (ret < 0) {
 			fprintf(stderr,
@@ -486,34 +491,34 @@ int interface(char *mesh_iface, int argc, char **argv)
 		goto err;
 	}
 
-	for (i = 2; i < argc; i++) {
-		ifindex = if_nametoindex(argv[i]);
+	for (i = 1; i < rest_argc; i++) {
+		ifindex = if_nametoindex(rest_argv[i]);
 
 		if (!ifindex) {
-			fprintf(stderr, "Error - interface does not exist: %s\n", argv[i]);
+			fprintf(stderr, "Error - interface does not exist: %s\n", rest_argv[i]);
 			continue;
 		}
 
-		if (argv[1][0] == 'a')
+		if (rest_argv[0][0] == 'a')
 			ifindex = ifmaster;
 		else
 			ifindex = 0;
 
-		ret = set_master_interface(argv[i], ifindex);
+		ret = set_master_interface(rest_argv[i], ifindex);
 		if (ret < 0) {
-			if (argv[1][0] == 'a')
+			if (rest_argv[0][0] == 'a')
 				long_op = "add";
 			else
 				long_op = "delete";
 
 			fprintf(stderr, "Error - failed to %s interface %s: %s\n",
-				long_op, argv[i], strerror(-ret));
+				long_op, rest_argv[i], strerror(-ret));
 			goto err;
 		}
 	}
 
 	/* check if there is no interface left and then destroy mesh_iface */
-	if (argv[1][0] == 'd') {
+	if (rest_argv[0][0] == 'd') {
 		cnt = count_interfaces(mesh_iface);
 		if (cnt == 0)
 			destroy_interface(mesh_iface);
-- 
2.9.3


  parent reply	other threads:[~2016-10-18 14:17 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-18 14:16 [B.A.T.M.A.N.] [PATCH v3 0/9] batctl: rtnetlink interface manipulation + userspace icmp Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 1/9] batctl: Use rtnl to query list of softif devices Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 2/9] batctl: Add command to create/destroy batman-adv interface Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 3/9] batctl: Use rtnl to add/remove interfaces Sven Eckelmann
2016-10-18 14:17 ` Sven Eckelmann [this message]
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 5/9] batctl: Allow to disable automatic interface create/destroy Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 6/9] batctl: Move interface command to extra file Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 7/9] batctl: Move check_mesh_iface* to functions.c Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 8/9] batctl: Add helper to generate instant random bytes Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 9/9] batctl: Implement non-routing batadv_icmp in userspace Sven Eckelmann
2016-10-24 11:18 ` [B.A.T.M.A.N.] [PATCH v3 0/9] batctl: rtnetlink interface manipulation + userspace icmp Simon Wunderlich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161018141731.7970-4-sven@narfation.org \
    --to=sven@narfation.org \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox