add options to set address and port, improved usage page
This commit is contained in:
55
main.cc
55
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)) {
|
||||
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(argv[1], "server", 6)) {
|
||||
} else if (!strncmp(optarg, "server", 6)) {
|
||||
logFileName = "server.txt";
|
||||
mode = SERVER_MODE;
|
||||
}
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
modeless = true;
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user