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

73
main.cc
View File

@@ -37,8 +37,8 @@ int serverSocketDescriptor; // a global variable for storing the server socket
int clientSocketDescriptor; int clientSocketDescriptor;
// This has not been used: // This has not been used:
//vector<int> clientSocketDescriptors; // a global vector for storing client // vector<int> clientSocketDescriptors; // a global vector for storing client
// socket descriptors // socket descriptors
// keep track of the session time using global variables // keep track of the session time using global variables
struct timeval start1, end1; struct timeval start1, end1;
@@ -60,15 +60,32 @@ 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))) { /*
logFileName = "client.txt"; * userInput: character buffer for the user's next message
mode = CLIENT_MODE; * 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 if (!strncmp(argv[1], "server", 6)) {
logFileName = "server.txt";
mode = SERVER_MODE;
}
} else { } else {
logFileName = "server.txt"; usage(argv[0]);
mode = SERVER_MODE; 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
@@ -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");
@@ -107,21 +121,21 @@ main(int argc, char *argv[])
while (!exit) { while (!exit) {
clear(); clear();
//Draw outer chat border // Draw outer chat border
box(stdscr, 0, 0); box(stdscr, 0, 0);
// Get screen size // Get screen size
int height, width; int height, width;
getmaxyx(stdscr, height, width); getmaxyx(stdscr, height, width);
// Draw separator line above input // Draw separator line above input
mvhline(height - 3, 1, 0, width - 2); mvhline(height - 3, 1, 0, width - 2);
// Labels // Labels
mvprintw(0, 2, " Chat "); mvprintw(0, 2, " Chat ");
mvprintw(height - 3, 2, " Input "); mvprintw(height - 3, 2, " Input ");
//Display Chat Log // Display Chat Log
displayFile(logFileName, linePos, LOG_LENGTH); displayFile(logFileName, linePos, LOG_LENGTH);
// scroll along the screen if and when required so that it stays // scroll along the screen if and when required so that it stays
@@ -130,10 +144,10 @@ main(int argc, char *argv[])
linePos++; linePos++;
// Input area // Input area
move(height - 2, 2); move(height - 2, 2);
clrtoeol(); clrtoeol();
printw("You: "); printw("You: ");
move(height - 2, 7); move(height - 2, 7);
refresh(); refresh();
@@ -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 "
return 1; "setting mode.");
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);
}