#include "console.h" int initInterface() { setAttribute(BRIGHT); printf("%s", START_MESSAGE); clearAttributes(); showInterfaces(); showHelp(NULL); setAttribute(MAGENTA); setAttribute(UNDERSCORE); printf("\n--ready--"); clearAttributes(); interface_fd = STDIN; FD_SET(interface_fd, &master_fds); max_fd = interface_fd; showPrompt(); return 0; } int closeInterface() { return 0; } int handleInterface() { // TODO: change to while loop :S if (read(interface_fd, data_buffer, 2048) < 0) { perror("couldn't read() STDIN"); } else { if (data_buffer[0] == '/') { // denotes command char **command = getCommand(data_buffer); int command_hash = generateHash(command[1], COMMAND_HASH_SIZE); if (!commands_table[command_hash]) { clearLine(); setAttribute(YELLOW); printf("%s '%s'", UNKNOWN_MESSAGE, command[1]); clearAttributes(); } else { if ((intptr_t)command[0] == 1) { // if only /command (*commands_table[command_hash])(NULL); } else if ((intptr_t)command[0] == 2) { // if 1 args (*commands_table[command_hash])(command[2]); } else if ((intptr_t)command[0] == 3) { // if 2 args (*commands_table[command_hash])(command[2], command[3]); } } //printf("\nhash of command %s: %d\n", command[1], generateHash(command[1], 64)); freeCommand(command); showPrompt(); } else if (data_buffer[0] == '\n') { // return fputs("\033[A\033[2K",stdout); rewind(stdout); ftruncate(1,0); //fputs("\033[A",stdout); fputs("\033[32m",stdout); printf("> "); fputs("\033[0m",stdout); fflush(stdout); } else { if (is_listening) { int i = 0; int done = 0; while (!done) { if (data_buffer[i++] == '\n') done = 1; } char message[i-1]; strncpy(message, data_buffer, i-1); // cut the \n message[i-1] = '\0'; clearLine(); sendOpen(message); // showPrompt(); // sendMulticast(send_socket, &group_socket, data_buffer, strlen(data_buffer)); /* fputs("\033[A\033[2K",stdout); rewind(stdout); ftruncate(1,0); fputs("\033[32m",stdout); printf("> "); fputs("\033[0m",stdout); fflush(stdout);*/ } else { setAttribute(RED); printf("not listening"); clearAttributes(); showPrompt(); } } } return 0; } int handleInput(const char *data_buffer) { fputs("\033[1A", stdout); printf("\n"); fputs("\033[2K",stdout); // move up and clear rewind(stdout); ftruncate(1,0); receiveOpen(data_buffer, DATAGRAM_SIZE); // fputs("\033[1B",stdout); // move cursor back down // fputs("\033[u", stdout); fflush(stdout); showPrompt(); /*fputs("\033[2K",stdout); rewind(stdout); ftruncate(1,0); printf("data_in: %s", data_buffer); showPrompt();*/ return 0; } void messageOpen(Message message) { fputs("\033[1A", stdout); printf("\n"); fputs("\033[2K",stdout); // move up and clear rewind(stdout); ftruncate(1,0); printf("%s/%s: %s", message.nick, inet_ntoa(message.address), message.payload); // fputs("\033[1B",stdout); // move cursor back down // fputs("\033[u", stdout); fflush(stdout); showPrompt(); } /*int handleInput(const char *data_buffer) { fputs("\033[1A", stdout); printf("\n"); fputs("\033[2K",stdout); // move up and clear rewind(stdout); ftruncate(1,0); receiveOpen(data_buffer, DATAGRAM_SIZE); // fputs("\033[1B",stdout); // move cursor back down // fputs("\033[u", stdout); fflush(stdout); showPrompt(); */ /*fputs("\033[2K",stdout); rewind(stdout); ftruncate(1,0); printf("data_in: %s", data_buffer); showPrompt();*/ /* return 0; }*/ void showHelp(const char *value) { setAttribute(MAGENTA); setAttribute(UNDERSCORE); printf("--help--\n"); clearAttributes(); if (value) { int hash = generateHash(value, COMMAND_HASH_SIZE); if (help[hash]) { setAttribute(CYAN); printf("%s", help[hash]); if (syntax[hash]) { printf("\nusage: %s", syntax[hash]); } clearAttributes(); } } else { setAttribute(CYAN); printf("%s", HELP_MESSAGE); clearAttributes(); } } void showUsers() { setAttribute(MAGENTA); setAttribute(UNDERSCORE); printf("--users--"); clearAttributes(); int i; int j; for (i=0;i "); fputs("\033[0m",stdout); fflush(stdout); } } void clearLine() { fputs("\033[A\033[2K",stdout); rewind(stdout); ftruncate(1,0); }