Return-Path: Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 2B2DAC0032 for ; Wed, 30 Aug 2023 23:09:50 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id ECF9641839 for ; Wed, 30 Aug 2023 23:09:49 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org ECF9641839 Authentication-Results: smtp2.osuosl.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=TcMgXTLG X-Virus-Scanned: amavisd-new at osuosl.org X-Spam-Flag: NO X-Spam-Score: -4.401 X-Spam-Level: X-Spam-Status: No, score=-4.401 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7t1yBfyRjG8r for ; Wed, 30 Aug 2023 23:09:48 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by smtp2.osuosl.org (Postfix) with ESMTPS id 9854841813 for ; Wed, 30 Aug 2023 23:09:48 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 9854841813 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1693436988; x=1724972988; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=z9/qgHwZKTVzVvRCc54a3AsAV6/D/CQvihse7puJwVE=; b=TcMgXTLGUuHFClg7SgFppHEUinmoo+eOci+oOIA/UE2gfi7Ibl07hJ4F IjAThJi4KM/MMpcXiS6qhzPAdPcE4IyyopA71apvjJg/UEzE+ZbkeBvTa O6v0sbkRrvR2DAHBJns9bfLFAWF2s+LOzF0udb0CjoTV/upGX3cyCVwmV GkA6T+dDNOZ9LWLsxHrZ7uymGgK9tHLS1GT0ytmRN7darvm63bVbAyH5Q Vu0+Z41CCfQbi7SAKe4UViGj5X2oLYuNAjiFqK1W7uN/e0G8EI5qMsKRL XcEQAjTLv7+EG4jAFrMOVloJnpoOZ9Xj0xo/3yMANe9EDWnmW2UWbXXtF g==; X-IronPort-AV: E=McAfee;i="6600,9927,10818"; a="356084358" X-IronPort-AV: E=Sophos;i="6.02,214,1688454000"; d="scan'208";a="356084358" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2023 16:09:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10818"; a="768574492" X-IronPort-AV: E=Sophos;i="6.02,214,1688454000"; d="scan'208";a="768574492" Received: from rthomas.sc.intel.com ([172.25.112.51]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2023 16:09:38 -0700 From: Ramesh Thomas To: accel-config@lists.linuxfoundation.org Date: Wed, 30 Aug 2023 16:06:39 -0700 Message-Id: <20230830230639.2486102-1-ramesh.thomas@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Cc: arjan.van.de.ven@intel.com Subject: [Accel-config] [PATCH] accel-config: Fix --help and other options X-BeenThere: accel-config@lists.linuxfoundation.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: accel-config List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 Aug 2023 23:09:50 -0000 Options like --help, --version and --list-cmds don't need the idxd driver to be loaded. Process them before checking for idxd driver being loaded. Also make the options more intuitive. Signed-off-by: Ramesh Thomas --- accfg/accel-config.c | 60 ++++------------------------ builtin.h | 1 - util/main.c | 94 +++++++++++++++++++------------------------- util/main.h | 2 +- 4 files changed, 50 insertions(+), 107 deletions(-) diff --git a/accfg/accel-config.c b/accfg/accel-config.c index cf3882e..616f2e5 100644 --- a/accfg/accel-config.c +++ b/accfg/accel-config.c @@ -15,50 +15,16 @@ #include #include -const char accfg_usage_string[] = "accel-config [--version] [--help] COMMAND [ARGS]"; -const char accfg_more_info_string[] = - "See 'accel-config help COMMAND' for more information on a specific command.\n" - " accel-config --list-cmds to see all available commands"; - -static int cmd_version(int argc, const char **argv, void *ctx) -{ - printf("%s\n", VERSION); - return 0; -} - -static int cmd_help(int argc, const char **argv, void *ctx) -{ - const char *const builtin_help_subcommands[] = { - "enable-workqueue", "disable-workqueue", - "enable-group", "disable-group", NULL - }; - struct option builtin_help_options[] = { - OPT_END(), - }; - const char *builtin_help_usage[] = { - "accel-config help [command]", - NULL - }; - - parse_options_subcommand(argc, argv, builtin_help_options, - builtin_help_subcommands, - builtin_help_usage, 0); - - if (!argv[0]) { - printf("\n usage: %s\n\n", accfg_usage_string); - printf("\n %s\n\n", accfg_more_info_string); - return 0; - } - - return help_show_man_page(argv[0], "accel-config", "ACCFG_MAN_VIEWER"); -} +const char accfg_usage_string[] = + "accel-config [--list-cmds] [-h | --help] [-v | --version] COMMAND [ARGS]\n\n" + " 'accel-config --help COMMAND' for information on a specific command\n" + " 'accel-config --list-cmds' for a list of available commands\n" + " 'accel-config --version' for version info"; static struct cmd_struct commands[] = { - {"version", cmd_version}, {"list", cmd_list}, {"load-config", cmd_config}, {"save-config", cmd_save}, - {"help", cmd_help}, {"disable-device", cmd_disable_device}, {"enable-device", cmd_enable_device}, {"disable-wq", cmd_disable_wq}, @@ -80,21 +46,9 @@ int main(int argc, const char **argv) int rc; /* Look for flags.. */ - argv++; - argc--; - main_handle_options(&argv, &argc, accfg_usage_string, commands, + main_handle_options(argv, argc, accfg_usage_string, commands, ARRAY_SIZE(commands)); - if (argc > 0) { - if (!prefixcmp(argv[0], "--")) - argv[0] += 2; - } else { - /* The user didn't specify a command; give them help */ - printf("\n usage: %s\n\n", accfg_usage_string); - printf("\n %s\n\n", accfg_more_info_string); - return -EINVAL; - } - if (access("/sys/module/idxd", F_OK)) { fprintf(stderr, "idxd kernel module not loaded\n"); return EXIT_FAILURE; @@ -104,6 +58,8 @@ int main(int argc, const char **argv) if (rc) goto error_exit; + argv++; + argc--; rc = main_handle_internal_command(argc, argv, ctx, commands, ARRAY_SIZE(commands)); diff --git a/builtin.h b/builtin.h index 96a9d61..9554ee5 100644 --- a/builtin.h +++ b/builtin.h @@ -13,7 +13,6 @@ #ifndef _ACCFG_BUILTIN_H_ #define _ACCFG_BUILTIN_H_ extern const char accfg_usage_string[]; -extern const char accfg_more_info_string[]; struct cmd_struct { const char *cmd; diff --git a/util/main.c b/util/main.c index 2bf7796..8ce42dc 100644 --- a/util/main.c +++ b/util/main.c @@ -25,77 +25,65 @@ #include #include +#include -int main_handle_options(const char ***argv, int *argc, const char *usage_msg, +void main_handle_options(const char **argv, int argc, const char *usage_msg, struct cmd_struct *cmds, int num_cmds) { - int handled = 0; - - while (*argc > 0) { - const char *cmd = (*argv)[0]; - if (cmd[0] != '-') - break; - - if (!strcmp(cmd, "--version") || !strcmp(cmd, "--help")) - break; - - /* - * Shortcut for '-h' and '-v' options to invoke help - * and version command. - */ - if (!strcmp(cmd, "-h")) { - (*argv)[0] = "--help"; - break; - } - - if (!strcmp(cmd, "-v")) { - (*argv)[0] = "--version"; - break; - } + int i; - if (!strcmp(cmd, "--list-cmds")) { - int i; + if (argc < 2) { + help_show_man_page(NULL, argv[0], "ACCFG_MAN_VIEWER"); + goto exit_app; + } - for (i = 0; i < num_cmds; i++) { - struct cmd_struct *p = cmds+i; + if (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-v")) { + printf("%s\n", VERSION); + exit(0); + } - /* filter out commands from auto-complete */ - if (strcmp(p->cmd, "create-nfit") == 0) - continue; - if (strcmp(p->cmd, "test") == 0) - continue; - if (strcmp(p->cmd, "bat") == 0) - continue; - printf("%s\n", p->cmd); + if (argv[1][0] != '-') { + for (i = 0; i < num_cmds; i++) + if (!strcmp(argv[1], cmds[i].cmd)) { + if (argc > 2 && + (!strcmp(argv[2], "--help") || + !strcmp(argv[2], "-h"))) { + help_show_man_page(argv[1], argv[0], + "ACCFG_MAN_VIEWER"); + goto exit_app; + } else + return; } - exit(0); - } else { - fprintf(stderr, "Unknown option: %s\n", cmd); - usage(usage_msg); - } + fprintf(stderr, "Unknown command: '%s'\n", argv[1]); + goto exit_app; + } + + if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) { + if (argc > 2) + help_show_man_page(argv[2], argv[0], "ACCFG_MAN_VIEWER"); + else + help_show_man_page(NULL, argv[0], "ACCFG_MAN_VIEWER"); + } - (*argv)++; - (*argc)--; - handled++; + if (!strcmp(argv[1], "--list-cmds")) { + for (i = 0; i < num_cmds; i++) + printf("%s %s\n", argv[0], cmds[i].cmd); + exit(0); } - return handled; + +exit_app: + /* Exits app if not already */ + usage(usage_msg); } int main_handle_internal_command(int argc, const char **argv, void *ctx, struct cmd_struct *cmds, int num_cmds) { - const char *cmd = argv[0]; int i; - /* Turn " cmd --help" into " help cmd" */ - if (argc > 1 && !strcmp(argv[1], "--help")) { - argv[1] = argv[0]; - argv[0] = cmd = "help"; - } - for (i = 0; i < num_cmds; i++) { struct cmd_struct *p = cmds+i; - if (strcmp(p->cmd, cmd)) + if (strcmp(p->cmd, argv[0])) continue; return p->fn(argc, argv, ctx); } diff --git a/util/main.h b/util/main.h index cccba7a..0f031f2 100644 --- a/util/main.h +++ b/util/main.h @@ -17,7 +17,7 @@ #ifndef __MAIN_H__ #define __MAIN_H__ struct cmd_struct; -int main_handle_options(const char ***argv, int *argc, const char *usage_msg, +void main_handle_options(const char **argv, int argc, const char *usage_msg, struct cmd_struct *cmds, int num_cmds); int main_handle_internal_command(int argc, const char **argv, void *ctx, struct cmd_struct *cmds, int num_cmds); -- 2.34.1