From: Ammar Faizi <[email protected]>
To: GNU/Weeb Mailing List <[email protected]>
Cc: Ammar Faizi <[email protected]>,
Alviro Iskandar Setiawan <[email protected]>,
Arthur Lapz <[email protected]>,
Fernanda Ma'rouf <[email protected]>,
Sprite <[email protected]>, Yonle <[email protected]>
Subject: [PATCH gwhttpd 10/14] gwhttpd: Add command line options
Date: Fri, 8 Jul 2022 19:10:21 +0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
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 <[email protected]>
---
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 <arpa/inet.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <getopt.h>
#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=<addr>\tSet gwhttpd bind address");
+ puts(" -p,--bind-port=<port>\tSet gwhttpd bind port");
+ puts(" -H,--slc-addr=<addr>\tSet gwhttpd SLC address");
+ puts(" -P,--slc-port=<port>\tSet gwhttpd SLC port");
+ puts("\n");
+ puts("GitHub repo: https://github.com/ammarfaizi2/gwhttpd.git\n");
+ puts("Copyright (C) 2022 Ammar Faizi <[email protected]>");
+ 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
next prev parent reply other threads:[~2022-07-08 12:11 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-08 12:10 [PATCH gwhttpd 00/14] gwhttpd updates Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 01/14] gwhttpd: Do an early return when `parse_http_header()` fails Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 02/14] gwhttpd: Don't print any error when mlock fails Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 03/14] gwhttpd: Replace `send_error_and_close()` with `send_http_error()` Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 04/14] gwhttpd: Add log in the interrupt handler Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 05/14] gwhttpd: Refactor HTTP header parser Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 06/14] gwhttpd: Avoid endless busy spinning on `send()` Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 07/14] Makefile: Add "make clean" command Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 08/14] gwhttpd: Skip interrupt error from `epoll_wait()` Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 09/14] gwhttpd: Add directory listing and download file support Ammar Faizi
2022-07-08 12:10 ` Ammar Faizi [this message]
2022-07-08 12:10 ` [PATCH gwhttpd 11/14] gwhttpd: Add SLC support Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 12/14] gwhttpd: slc: Shut the SLC log up Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 13/14] gwhttpd: Fix 403 HTTP error when accessing an empty file Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 14/14] gwhttpd: Add connecting log for SLC Ammar Faizi
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 \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/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