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