diff options
| author | Mr. Goblin <mrgoblin@mrgoblin.net> | 2026-09-13 23:32:10 +0300 |
|---|---|---|
| committer | Mr. Goblin <mrgoblin@mrgoblin.net> | 2026-09-13 23:32:10 +0300 |
| commit | 51a1bad32dec2f0ebff5f16345759934c233ef71 (patch) | |
| tree | feee32168e8962521f48003a871ee2b988bf26b8 | |
| parent | f347a5e24fa073888476df92643d55fc61ceef12 (diff) | |
CLI flags
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | src/Makefile | 2 | ||||
| -rw-r--r-- | src/btasktd.c | 108 | ||||
| -rw-r--r-- | src/cliflags.c | 94 | ||||
| -rw-r--r-- | src/cliflags.h | 75 |
5 files changed, 216 insertions, 65 deletions
@@ -1 +1 @@ -# btaskt +# btasktd diff --git a/src/Makefile b/src/Makefile index d05ed6d..fa8ddc8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -20,7 +20,7 @@ clean: $(CC) $(CFLAGS) -c $< -o $@ -BTASKTD_OBJECTS = btasktd.o +BTASKTD_OBJECTS = btasktd.o cliflags.o BTASKTD_LIBS = -lc btasktd: $(BTASKTD_OBJECTS) diff --git a/src/btasktd.c b/src/btasktd.c index 92f1ce4..34e1fae 100644 --- a/src/btasktd.c +++ b/src/btasktd.c @@ -39,83 +39,59 @@ #include <fcntl.h> #include <unistd.h> #include <errno.h> +#include "cliflags.h" -#ifndef BTASKTD_QUEUE_LEN -# define QUEUE_LEN 16 -#endif - -enum flag_val_res { - fvr_not_found = -1, - fvr_min = fvr_not_found, - fvr_ok = 0, - fvr_invalid, - fvr_max = fvr_invalid -}; +#define BTASKTD_HELP_STR \ + "btasktd - task tracking TCP-server\n" \ + "flags:\n" \ + " -p PORT\n" \ + " sets the port server should listen to\n" \ + " -h\n" \ + " prints the help message (the server won't start!)\n" \ + " -V\n" \ + " prints version (the server won't start!)\n" -struct str_flag_val { - enum flag_val_res code; - char *data; -}; +#define BTASKTD_VERSION_STR \ + "0.0.0" -struct int_flag_val { - enum flag_val_res code; - int data; -}; - -/* -f the_str */ -static struct str_flag_val -extract_str_flag(char flagletter, int argc, char **argv) -{ - struct str_flag_val res = {0}; - char flagstr[3] = { '-', ' ', '\0' }; - int i; - flagstr[1] = flagletter; - for (i = 1; i < argc; i++) - if (0 == strcmp(flagstr, argv[i])) - break; - if (i >= argc) { - res.code = fvr_not_found; - } else if (i == argc-1) { - res.code = fvr_invalid; - } else { - res.code = fvr_ok; - res.data = argv[i+1]; - } - return res; -} +#ifndef BTASKTD_QUEUE_LEN +# define BTASKTD_QUEUE_LEN 16 +#endif -/* -f number */ -static struct int_flag_val -extract_int_flag(char flagletter, int argc, char **argv) +static void +fprint_help(FILE *f) { - struct int_flag_val res; - struct str_flag_val strres = - extract_str_flag(flagletter, argc, argv); - char *endptr; - res.code = strres.code; - if (res.code != fvr_ok) { - return res; - } - res.data = strtol(strres.data, &endptr, 10); - if (*endptr != '\0') - res.code = fvr_invalid; - return res; + fputs(BTASKTD_HELP_STR, f); } int main(int argc, char **argv) { - int server_fd, opt = 1, res, flags, port; + int server_fd, opt = 1, res, fflags, port, passed_flags = 1; struct sockaddr_in addr = {0}; - struct int_flag_val port_res = - extract_int_flag('p', argc, argv); + struct int_flag_val port_res; + struct bool_flag_val show_ver, show_help; + + show_ver = extract_bool_flag('V', argc, argv); + if (show_ver.code == fvr_ok && show_ver.data) { + printf("btasktd version %s\n", BTASKTD_VERSION_STR); + return 0; + } + + show_help = extract_bool_flag('h', argc, argv); + if (show_help.code == fvr_ok && show_help.data) { + fprint_help(stdout); + return 0; + } + + port_res = extract_int_flag('p', argc, argv); switch (port_res.code) { case fvr_not_found: # ifdef BTASKTD_DEFAULT_PORT port = BTASKTD_DEFAULT_PORT; # else fputs("No port specified!\n", stderr); - return 1; + passed_flags = 0; # endif break; case fvr_invalid: @@ -123,6 +99,12 @@ main(int argc, char **argv) return 1; case fvr_ok: port = port_res.data; + break; + } + + if (!passed_flags) { + fprint_help(stderr); + return 1; } server_fd = socket(AF_INET, SOCK_STREAM, 0); @@ -139,7 +121,7 @@ main(int argc, char **argv) perror("bind"); return 1; } - res = listen(server_fd, QUEUE_LEN); + res = listen(server_fd, BTASKTD_QUEUE_LEN); if (0 != res) { close(server_fd); perror("listen"); @@ -152,8 +134,8 @@ main(int argc, char **argv) perror("setsockopt"); return 1; } - flags = fcntl(server_fd, F_GETFL); - fcntl(server_fd, F_SETFL, flags|O_NONBLOCK); + fflags = fcntl(server_fd, F_GETFL); + fcntl(server_fd, F_SETFL, fflags|O_NONBLOCK); printf("btaskt server btasktd started on port %d\n", port); diff --git a/src/cliflags.c b/src/cliflags.c new file mode 100644 index 0000000..f34a889 --- /dev/null +++ b/src/cliflags.c @@ -0,0 +1,94 @@ +/*- + * Copyright (c) 2026, Mr. Goblin + * + * SPDX-License-Identifier: BSD-2-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + */ + +#include <stdlib.h> +#include <string.h> + +#include "cliflags.h" + +/* -f */ +struct bool_flag_val +extract_bool_flag(char flagletter, int argc, char **argv) +{ + struct bool_flag_val res = {0}; + char flagstr[3] = { '-', ' ', '\0' }; + int i; + flagstr[1] = flagletter; + res.code = fvr_not_found; + res.data = 0; + for (i = 1; i < argc; i++) { + if (0 == strcmp(flagstr, argv[i])) { + res.code = fvr_ok; + res.data = 1; + break; + } + } + return res; +} + +/* -f the_str */ +struct str_flag_val +extract_str_flag(char flagletter, int argc, char **argv) +{ + struct str_flag_val res = {0}; + char flagstr[3] = { '-', ' ', '\0' }; + int i; + flagstr[1] = flagletter; + for (i = 1; i < argc; i++) + if (0 == strcmp(flagstr, argv[i])) + break; + if (i >= argc) { + res.code = fvr_not_found; + } else if (i == argc-1) { + res.code = fvr_invalid; + } else { + res.code = fvr_ok; + res.data = argv[i+1]; + } + return res; +} + +/* -f number */ +struct int_flag_val +extract_int_flag(char flagletter, int argc, char **argv) +{ + struct int_flag_val res; + struct str_flag_val strres = + extract_str_flag(flagletter, argc, argv); + char *endptr; + res.code = strres.code; + if (res.code != fvr_ok) { + return res; + } + res.data = strtol(strres.data, &endptr, 10); + if (*endptr != '\0') + res.code = fvr_invalid; + return res; +} diff --git a/src/cliflags.h b/src/cliflags.h new file mode 100644 index 0000000..21ffebc --- /dev/null +++ b/src/cliflags.h @@ -0,0 +1,75 @@ +/*- + * Copyright (c) 2026, Mr. Goblin + * + * SPDX-License-Identifier: BSD-2-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + */ + +#ifndef BTASKT_CLIFLAGS_H_SENTRY +#define BTASKT_CLIFLAGS_H_SENTRY +#ifdef __cplusplus +extern "C" { +#endif + +enum flag_val_res { + fvr_not_found = -1, + fvr_min = fvr_not_found, + fvr_ok = 0, + fvr_invalid, + fvr_max = fvr_invalid +}; + +struct str_flag_val { + enum flag_val_res code; + char *data; +}; + +struct int_flag_val { + enum flag_val_res code; + int data; +}; + +struct bool_flag_val { + enum flag_val_res code; + int data; +}; + +/* -f */ +struct bool_flag_val +extract_bool_flag(char flagletter, int argc, char **argv); + +/* -f the_str */ +struct str_flag_val +extract_str_flag(char flagletter, int argc, char **argv); + +/* -f number */ +struct int_flag_val +extract_int_flag(char flagletter, int argc, char **argv); + +#ifdef __cplusplus +} +#endif +#endif /* BTASKT_CLIFLAGS_H_SENTRY */ |
