From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from integral2.. (unknown [36.81.65.188]) by gnuweeb.org (Postfix) with ESMTPSA id 423D27E398; Fri, 8 Jul 2022 12:11:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1657282265; bh=7H8bnCbSeSRPcH73WdJ0VCyGj6qaGgdwwPDDB/qiDEw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hvilUCIZ1FmGL89KbKlhUZIiE2I6Mg2ewqWbzrE550qzY2MovOvaNHz+EykyAmP5R YQBY4pLRdmNvZCiztjfFQgcRLjTVDhEl3oJ4mrdK1E+zFED6renonrDvojfgUD9n/A W/WzalK181v8GyMjdW8vjkeb5vzeY8lb4S98UUGrgPFFAf0e/YnCltsdMTTfqSp7WR 1Dtsko4XYuFrMewra8w/Y44pwylzyeg6iav1AYwPwFSKnYWCMiUsWkFuouCr59RHAU RhSTKxV2wH+LwsgJjSiX+k/ECoPn8QZQsSGfXbPRiSaP9D3Y0He/0mL4qaa0zQuda9 WDEwqt6jptPtA== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Alviro Iskandar Setiawan , Arthur Lapz , Fernanda Ma'rouf , Sprite , Yonle Subject: [PATCH gwhttpd 10/14] gwhttpd: Add command line options Date: Fri, 8 Jul 2022 19:10:21 +0700 Message-Id: <20220708121025.926162-11-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220708121025.926162-1-ammarfaizi2@gnuweeb.org> References: <20220708121025.926162-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Use getopt_long() to parse command line argument. This is useful for adding SLC support and to make the app more user friendly. Signed-off-by: Ammar Faizi --- gwhttpd.cpp | 126 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 9 deletions(-) diff --git a/gwhttpd.cpp b/gwhttpd.cpp index 0e4c558..c878c5c 100644 --- a/gwhttpd.cpp +++ b/gwhttpd.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #define noinline __attribute__((__noinline__)) #define __hot __attribute__((__hot__)) @@ -1799,23 +1800,24 @@ static __cold void destroy_state(struct server_state *state) free_state(state); } -int main(int argc, char *argv[]) +struct cmd_arg { + const char *bind_addr; + const char *bind_port; + const char *slc_circuit_addr; + const char *slc_circuit_port; +}; + +static int _main(struct cmd_arg *arg) { struct server_state *state = NULL; int ret; - setvbuf(stdout, NULL, _IOLBF, 4096); - if (argc != 3) { - printf("Usage: %s [bind_address] [bind_port]\n", argv[0]); - return 0; - } - ret = init_state(&state); if (unlikely(ret)) return ret; - state->bind_addr = argv[1]; - state->bind_port = (uint16_t)atoi(argv[2]); + state->bind_addr = arg->bind_addr; + state->bind_port = (uint16_t)atoi(arg->bind_port); ret = init_socket(state); if (unlikely(ret)) @@ -1825,3 +1827,109 @@ out: destroy_state(state); return ret; } + +static const struct option long_options[] = { + {"bind-addr", required_argument, 0, 'h'}, + {"bind-port", required_argument, 0, 'p'}, + {"slc-addr", required_argument, 0, 'H'}, + {"slc-port", required_argument, 0, 'P'}, + {NULL, 0, 0, 0} +}; + +static __cold int _parse_cmd_arg(int argc, char *argv[], struct cmd_arg *arg) +{ + int opt_idx; + int c; + + c = getopt_long(argc, argv, "h:p:H:P:", long_options, &opt_idx); + if (c == -1) + return 1; + + switch (c) { + case 'h': + arg->bind_addr = optarg; + break; + case 'p': + arg->bind_port = optarg; + break; + case 'H': + arg->slc_addr = optarg; + break; + case 'P': + arg->slc_port = optarg; + break; + default: + printf("Unknown option: %s\n", optarg); + return -EINVAL; + } + + return 0; +} + +static __cold int parse_cmd_arg(int argc, char *argv[], struct cmd_arg *arg) +{ + int ret; + + while (1) { + ret = _parse_cmd_arg(argc, argv, arg); + if (ret) + break; + } + + if (ret == 1) + return 0; + + return ret; +} + +static noinline __cold void show_help(const char *app) +{ + putchar('\n'); + puts("Usage:\n"); + printf(" %s [options]\n\n", app); + puts("Options:"); + puts(" -h,--bind-addr=\tSet gwhttpd bind address"); + puts(" -p,--bind-port=\tSet gwhttpd bind port"); + puts(" -H,--slc-addr=\tSet gwhttpd SLC address"); + puts(" -P,--slc-port=\tSet gwhttpd SLC port"); + puts("\n"); + puts("GitHub repo: https://github.com/ammarfaizi2/gwhttpd.git\n"); + puts("Copyright (C) 2022 Ammar Faizi "); + puts("This is free software; see the source for copying conditions. There is NO"); + puts("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"); +} + +int main(int argc, char *argv[]) +{ + struct cmd_arg arg; + int ret; + + setvbuf(stdout, NULL, _IOLBF, 4096); + if (argc == 1) { + show_help(argv[0]); + return EINVAL; + } + + memset(&arg, 0, sizeof(arg)); + ret = parse_cmd_arg(argc, argv, &arg); + if (unlikely(ret)) { + if (ret < 0) + ret = -ret; + show_help(argv[0]); + return ret; + } + + if (!arg.bind_addr) { + puts("Error: Missing bind_addr!"); + show_help(argv[0]); + return EINVAL; + } + + if (!arg.bind_port) { + puts("Error: Missing bind_port!"); + show_help(argv[0]); + return EINVAL; + } + + return _main(&arg); +} -- Ammar Faizi