From 07e742aa4e25206a4a815804bd7c764f3e86c07c Mon Sep 17 00:00:00 2001 From: jazz Date: Wed, 15 Apr 2026 11:12:05 -0500 Subject: [PATCH] add options to set address and port, improved usage page --- main.cc | 65 +++++++++++++++++++++++++++++++++++++++++--------------- public.h | 2 +- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/main.cc b/main.cc index 193c2af..76e76b5 100644 --- a/main.cc +++ b/main.cc @@ -23,7 +23,7 @@ using namespace std; -const int PORT_NUM = 8888; +int PORT_NUM = 8888; string IP_ADDRESS = "0.0.0.0"; const int DEFAULT_CUR_X = 2; // the x position of the preferred default cursor // position for message entry @@ -60,33 +60,52 @@ struct waitClientArgs { // struct to keep track of }; -void usage(char *progname); +void usage(char *progname, bool *m); int main(int argc, char *argv[]) { /* - * userInput: character buffer for the user's next message + * ch: getopt * exit: set to true when infinite loop is to end + * userInput: character buffer for the user's next message * modeless: check whether a modeless exit was accidental */ - char *userInput = new char[1024]; + int ch; bool exit = false; + char *userInput = new char[1024]; bool modeless = false; - if (argc > 1) { - if (!strncmp(argv[1], "client", 6)) { - logFileName = "client.txt"; - mode = CLIENT_MODE; - } else if (!strncmp(argv[1], "server", 6)) { - logFileName = "server.txt"; - mode = SERVER_MODE; - } - } else { - usage(argv[0]); - modeless = true; + if (argc == 1) { + usage(argv[0], &modeless); goto leave; } + + while ((ch = getopt(argc, argv, "m:a:p:h")) != -1) + switch (ch) { + case 'm': + if (!strncmp(optarg, "client", 6)) { + logFileName = "client.txt"; + mode = CLIENT_MODE; + } else if (!strncmp(optarg, "server", 6)) { + logFileName = "server.txt"; + mode = SERVER_MODE; + } else { + usage(argv[0], &modeless); + goto leave; + } + break; + case 'a': + IP_ADDRESS = optarg; + break; + case 'p': + PORT_NUM = atoi(optarg); + break; + case 'h': default: + usage(argv[0], &modeless); + goto leave; + break; + } // putting this lower down to circumvent the terminal brick when the // socket was already in use results in all network functonality no @@ -180,17 +199,29 @@ leave: "setting mode."); return 1; } + return 2; break; } return 0; } +/* remember to call 'goto leave' after running */ void -usage(char *progname) +usage(char *progname, bool *m) // m:a:p:h { printf("\x1b[1mUsage:\x1b[0m\n" "\t%s client\t\x1b[3m# run as client\x1b[0m\n" - "\t%s server\t\x1b[3m# run as server\x1b[0m\n", + "\t%s server\t\x1b[3m# run as server\x1b[0m\n" + "\n\n" + "\x1b[1mOptions:\x1b[0m\n" + "\t\x1b[1m-m [client|server]\x1b[0m\tMode \x1b[1;31m(required)\x1b[0m\n" + "\n" + "\t\x1b[1m-a [255.255.255.255]\x1b[0m\tAddress\n" + "\t\t\t\tserver mode: listen address \x1b[33m(default: 0.0.0.0)\x1b[0m\n" + "\t\t\t\tclient mode: address of server \x1b[33m(default: 127.0.0.1)\x1b[0m\n" + "\n" + "\t\x1b[1m-p [0-65535]\x1b[0m\t\tPort number\n", progname, progname); + *m = true; } diff --git a/public.h b/public.h index 5e8b510..8c5b83e 100644 --- a/public.h +++ b/public.h @@ -8,7 +8,7 @@ using namespace std; typedef enum { NO_MODE = 0, SERVER_MODE = 1, CLIENT_MODE = 2 } chatmode_t; extern chatmode_t mode; -extern const int PORT_NUM; +extern int PORT_NUM; extern string IP_ADDRESS; extern const int DEFAULT_CUR_X; extern const int DEFAULT_CUR_Y;