enforce mode selection, fix indentation, add usage

This commit is contained in:
jazz
2026-04-15 10:32:03 -05:00
parent 99dac0f416
commit 2e70901ef8

39
main.cc
View File

@@ -60,16 +60,33 @@ struct waitClientArgs {
// struct to keep track of
};
void usage(char *progname);
int
main(int argc, char *argv[])
{
if (argc > 1 && (!strncmp(argv[1], "client", 6))) {
/*
* userInput: character buffer for the user's next message
* exit: set to true when infinite loop is to end
* modeless: check whether a modeless exit was accidental
*/
char *userInput = new char[1024];
bool exit = false;
bool modeless = false;
if (argc > 1) {
if (!strncmp(argv[1], "client", 6)) {
logFileName = "client.txt";
mode = CLIENT_MODE;
} else {
} else if (!strncmp(argv[1], "server", 6)) {
logFileName = "server.txt";
mode = SERVER_MODE;
}
} else {
usage(argv[0]);
modeless = true;
goto leave;
}
// putting this lower down to circumvent the terminal brick when the
// socket was already in use results in all network functonality no
@@ -85,9 +102,6 @@ main(int argc, char *argv[])
// there was already content in the log file
linePos = linesInFile(logFileName) - LOG_LENGTH + 1;
char *userInput = new char[1024];
bool exit = false;
switch (mode) {
case CLIENT_MODE:
writeToFile(logFileName, "CLIENT MODE");
@@ -161,11 +175,22 @@ leave:
closeServer();
break;
default:
puts("Warn: program appears to have successfully finished without ever setting "
"mode.");
if (!modeless) {
puts("Warn: program appears to have successfully finished without ever "
"setting mode.");
return 1;
}
break;
}
return 0;
}
void
usage(char *progname)
{
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",
progname, progname);
}