From patchwork Sat Sep 24 15:58:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 16705 X-Patchwork-Delegate: mareklindner@neomailbox.ch Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from open-mesh.org (localhost [IPv6:::1]) by open-mesh.org (Postfix) with ESMTP id 226C482F5F; Sat, 24 Sep 2016 17:58:37 +0200 (CEST) Authentication-Results: open-mesh.org; dmarc=none header.from=narfation.org Authentication-Results: open-mesh.org; dkim=fail reason="verification failed; unprotected key" header.d=narfation.org header.i=@narfation.org header.b=AaP5R4yI; dkim-adsp=fail (unprotected policy); dkim-atps=neutral Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=79.140.41.39; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver=b.a.t.m.a.n@lists.open-mesh.org Authentication-Results: open-mesh.org; dmarc=pass header.from=narfation.org Received: from v3-1039.vlinux.de (narfation.org [79.140.41.39]) by open-mesh.org (Postfix) with ESMTPS id A095D82F03 for ; Sat, 24 Sep 2016 17:58:18 +0200 (CEST) Received: from sven-desktop.home.narfation.org (p200300C593C7D9F90000000000002E17.dip0.t-ipconnect.de [IPv6:2003:c5:93c7:d9f9::2e17]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id 348C51100CF; Sat, 24 Sep 2016 17:58:18 +0200 (CEST) Authentication-Results: v3-1039.vlinux.de; dmarc=none header.from=narfation.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=narfation.org; s=20121; t=1474732698; bh=hTSqDh0u5+x5ljnsJgQdyi8lk529g2nZAN5NPRSBbjk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AaP5R4yISiE+Q7aAskMtQpU9I7KjyqkH22Ge59wtbWTCk/zLP+RusGu+9nIy9AP26 JIyA2NPCyWbrBpCOgDTd23DJvN4S4c0OxByYCJHjefQAvu3lCi1p5VO+yltYTRSchF 2Hn1PzSmTvuv8S/C2HvfQANsg5Qa7IVuuGtCH28k= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Sat, 24 Sep 2016 17:58:03 +0200 Message-Id: <20160924155806.9115-4-sven@narfation.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20160924155806.9115-1-sven@narfation.org> References: <20160924155806.9115-1-sven@narfation.org> Subject: [B.A.T.M.A.N.] [PATCH v2 4/7] batctl: Parse interface arguments relative to last parsed option X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking Errors-To: b.a.t.m.a.n-bounces@lists.open-mesh.org Sender: "B.A.T.M.A.N" 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 --- 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);