diff --git a/main.cc b/main.cc index 87273b8..f24d5db 100644 --- a/main.cc +++ b/main.cc @@ -37,8 +37,8 @@ int serverSocketDescriptor; // a global variable for storing the server socket int clientSocketDescriptor; // This has not been used: -//vector clientSocketDescriptors; // a global vector for storing client - // socket descriptors +// vector clientSocketDescriptors; // a global vector for storing client +// socket descriptors // keep track of the session time using global variables struct timeval start1, end1; @@ -60,15 +60,32 @@ 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))) { - logFileName = "client.txt"; - mode = CLIENT_MODE; + /* + * 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 if (!strncmp(argv[1], "server", 6)) { + logFileName = "server.txt"; + mode = SERVER_MODE; + } } else { - logFileName = "server.txt"; - mode = SERVER_MODE; + usage(argv[0]); + modeless = true; + goto leave; } // 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 linePos = linesInFile(logFileName) - LOG_LENGTH + 1; - char *userInput = new char[1024]; - bool exit = false; - switch (mode) { case CLIENT_MODE: writeToFile(logFileName, "CLIENT MODE"); @@ -107,21 +121,21 @@ main(int argc, char *argv[]) while (!exit) { clear(); - //Draw outer chat border + // Draw outer chat border box(stdscr, 0, 0); // Get screen size int height, width; - getmaxyx(stdscr, height, width); + getmaxyx(stdscr, height, width); // Draw separator line above input - mvhline(height - 3, 1, 0, width - 2); + mvhline(height - 3, 1, 0, width - 2); - // Labels - mvprintw(0, 2, " Chat "); - mvprintw(height - 3, 2, " Input "); + // Labels + mvprintw(0, 2, " Chat "); + mvprintw(height - 3, 2, " Input "); - //Display Chat Log + // Display Chat Log displayFile(logFileName, linePos, LOG_LENGTH); // scroll along the screen if and when required so that it stays @@ -130,10 +144,10 @@ main(int argc, char *argv[]) linePos++; // Input area - move(height - 2, 2); - clrtoeol(); - printw("You: "); - move(height - 2, 7); + move(height - 2, 2); + clrtoeol(); + printw("You: "); + move(height - 2, 7); refresh(); @@ -161,11 +175,22 @@ leave: closeServer(); break; default: - puts("Warn: program appears to have successfully finished without ever setting " - "mode."); - return 1; + 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); +}