From patchwork Sun Oct 21 22:54:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 17523 X-Patchwork-Delegate: sw@simonwunderlich.de 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 16D13830B7; Mon, 22 Oct 2018 00:56:32 +0200 (CEST) Authentication-Results: open-mesh.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=narfation.org header.i=@narfation.org header.b="GYF4k4Xi"; dkim-atps=neutral Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=79.140.41.39; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver= Received: from v3-1039.vlinux.de (narfation.org [79.140.41.39]) by open-mesh.org (Postfix) with ESMTPS id A749B81834 for ; Mon, 22 Oct 2018 00:56:02 +0200 (CEST) Received: from sven-desktop.home.narfation.org (p200300C593D704FD0000000000008096.dip0.t-ipconnect.de [IPv6:2003:c5:93d7:4fd::8096]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id 28DFD110127; Mon, 22 Oct 2018 00:56:02 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=narfation.org; s=20121; t=1540162562; bh=L1qAA6eKh/dDO3LoTLaaDuDs5lERnc8t8A/lFoI4+8I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GYF4k4Xi56/wy3nLNIDv4XxPsiMx50vMcwhLh76cHHcSykq8UoqM7mOeyY5ZnIlHs el2QDx8/E7z42wbDYkGoVvndXlz5p6HdIXrx13eJ7Z+z92iHeZ98wEk+oOvzku2CRU RpYW43Jl373odaBSDD4L+jgdNwHqmPLwIFZzsrQI= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Mon, 22 Oct 2018 00:54:58 +0200 Message-Id: <20181021225524.8155-13-sven@narfation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181021225524.8155-1-sven@narfation.org> References: <20181021225524.8155-1-sven@narfation.org> MIME-Version: 1.0 Subject: [B.A.T.M.A.N.] [PATCH 12/38] batctl: Use getopt to parse main options X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.23 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" The handwritten option parser is rather fragile and doesn't support all features of a normal commandline option parser. Instead use getopt to handle the most critical parts and keep it more flexible. Signed-off-by: Sven Eckelmann --- main.c | 95 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 39 deletions(-) diff --git a/main.c b/main.c index fa4371c..f5df477 100644 --- a/main.c +++ b/main.c @@ -22,6 +22,7 @@ +#include #include #include #include @@ -87,6 +88,25 @@ static void print_usage(void) #endif } +static void version(void) +{ + int ret; + + printf("batctl %s [batman-adv: ", SOURCE_VERSION); + + ret = read_file("", module_ver_path, USE_READ_BUFF | SILENCE_ERRORS, 0, 0, 0); + if ((line_ptr) && (line_ptr[strlen(line_ptr) - 1] == '\n')) + line_ptr[strlen(line_ptr) - 1] = '\0'; + + if (ret == EXIT_SUCCESS) + printf("%s]\n", line_ptr); + else + printf("module not loaded]\n"); + + free(line_ptr); + exit(EXIT_SUCCESS); +} + static const struct command *find_command(const char *name) { const struct command **p; @@ -109,53 +129,48 @@ int main(int argc, char **argv) const struct command *cmd; int i, ret = EXIT_FAILURE; char *mesh_iface = mesh_dfl_iface; - - if ((argc > 1) && (strcmp(argv[1], "-m") == 0)) { - if (argc < 3) { - fprintf(stderr, "Error - the option '-m' needs a parameter\n"); + int opt; + + while ((opt = getopt(argc, argv, "+hm:v")) != -1) { + switch (opt) { + case 'h': + print_usage(); + exit(EXIT_SUCCESS); + break; + case 'm': + if (mesh_iface != mesh_dfl_iface) { + fprintf(stderr, + "Error - multiple mesh interfaces specified\n"); + goto err; + } + + mesh_iface = argv[2]; + break; + case 'v': + version(); + break; + default: goto err; } - - mesh_iface = argv[2]; - - argv += 2; - argc -= 2; } - if (argc < 2) { + if (optind >= argc) { fprintf(stderr, "Error - no command specified\n"); goto err; } - if (strcmp(argv[1], "-h") == 0) { - print_usage(); - exit(EXIT_SUCCESS); - } - - if (strcmp(argv[1], "-v") == 0) { - printf("batctl %s [batman-adv: ", SOURCE_VERSION); - - ret = read_file("", module_ver_path, USE_READ_BUFF | SILENCE_ERRORS, 0, 0, 0); - if ((line_ptr) && (line_ptr[strlen(line_ptr) - 1] == '\n')) - line_ptr[strlen(line_ptr) - 1] = '\0'; - - if (ret == EXIT_SUCCESS) - printf("%s]\n", line_ptr); - else - printf("module not loaded]\n"); - - free(line_ptr); - exit(EXIT_SUCCESS); - } + argv += optind; + argc -= optind; + optind = 0; - if ((cmd = find_command(argv[1]))) { + if ((cmd = find_command(argv[0]))) { if (cmd->flags & COMMAND_FLAG_MESH_IFACE && check_mesh_iface(mesh_iface) < 0) { fprintf(stderr, "Error - interface %s is not present or not a batman-adv interface\n", mesh_iface); exit(EXIT_FAILURE); } - ret = cmd->handler(mesh_iface, argc - 1, argv + 1); + ret = cmd->handler(mesh_iface, argc, argv); } else { if (check_mesh_iface(mesh_iface) < 0) { fprintf(stderr, "Error - interface %s is not present or not a batman-adv interface\n", mesh_iface); @@ -163,24 +178,26 @@ int main(int argc, char **argv) } for (i = 0; i < BATCTL_SETTINGS_NUM; i++) { - if ((strcmp(argv[1], batctl_settings[i].opt_long) != 0) && - (strcmp(argv[1], batctl_settings[i].opt_short) != 0)) + if ((strcmp(argv[0], batctl_settings[i].opt_long) != 0) && + (strcmp(argv[0], batctl_settings[i].opt_short) != 0)) continue; - ret = handle_sys_setting(mesh_iface, i, argc - 1, argv + 1); + ret = handle_sys_setting(mesh_iface, i, argc, argv); goto out; } for (i = 0; i < BATCTL_TABLE_NUM; i++) { - if ((strcmp(argv[1], batctl_debug_tables[i].opt_long) != 0) && - (strcmp(argv[1], batctl_debug_tables[i].opt_short) != 0)) + if ((strcmp(argv[0], batctl_debug_tables[i].opt_long) != 0) && + (strcmp(argv[0], batctl_debug_tables[i].opt_short) != 0)) continue; - ret = handle_debug_table(mesh_iface, i, argc - 1, argv + 1); + ret = handle_debug_table(mesh_iface, i, argc, argv); goto out; } - fprintf(stderr, "Error - no valid command or debug table specified: %s\n", argv[1]); + fprintf(stderr, + "Error - no valid command or debug table specified: %s\n", + argv[0]); print_usage(); }