219 lines
4.6 KiB
C
219 lines
4.6 KiB
C
#include "console.h"
|
|
|
|
int initInterface() {
|
|
// setAttribute(DIM);
|
|
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 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 showVariables() {
|
|
setAttribute(MAGENTA);
|
|
setAttribute(UNDERSCORE);
|
|
printf("--variables--\n");
|
|
clearAttributes();
|
|
int i;
|
|
for (i = 0;i < CONFIG_HASH_SIZE;i++) {
|
|
if (config[i]) {
|
|
setAttribute(GREEN);
|
|
printf("%s(%d): ", config_name[i], i);
|
|
clearAttributes();
|
|
printf("%s\n", config[i]);
|
|
}
|
|
}
|
|
clearAttributes();
|
|
}
|
|
|
|
void showInterfaces() {
|
|
int i;
|
|
fputs("\033[4m\033[35m", stdout);
|
|
printf("--%d interfaces detected--\n", interfaces);
|
|
fputs("\033[0m",stdout);
|
|
fputs("\033[32m", stdout);
|
|
for (i = 0; i < interfaces; i++) {
|
|
if (ip_list[i]) {
|
|
printf("%s: %s\n", dev_list[i], ip_list[i]);
|
|
}
|
|
}
|
|
fputs("\033[0m",stdout);
|
|
clearAttributes();
|
|
}
|
|
|
|
void setAttribute(const int attribute) {
|
|
switch(attribute) {
|
|
case RED:
|
|
fputs("\033[31m",stdout);
|
|
break;
|
|
case YELLOW:
|
|
fputs("\033[33m",stdout);
|
|
break;
|
|
case GREEN:
|
|
fputs("\033[32m", stdout);
|
|
break;
|
|
case CYAN:
|
|
fputs("\033[36m", stdout);
|
|
break;
|
|
case MAGENTA:
|
|
fputs("\033[35m", stdout);
|
|
break;
|
|
case BLACK:
|
|
fputs("\033[30m", stdout);
|
|
break;
|
|
case UNDERSCORE:
|
|
fputs("\033[4m", stdout);
|
|
break;
|
|
case DIM:
|
|
fputs("\033[2m", stdout);
|
|
break;
|
|
case BG_WHITE:
|
|
fputs("\033[47m", stdout);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void clearAttributes() {
|
|
fputs("\033[0m", stdout);
|
|
}
|
|
|
|
void showPrompt() {
|
|
fputs("\033[32m",stdout);
|
|
printf("\n> ");
|
|
fputs("\033[0m",stdout);
|
|
fflush(stdout);
|
|
}
|
|
|
|
void clearLine() {
|
|
fputs("\033[A\033[2K",stdout);
|
|
rewind(stdout);
|
|
ftruncate(1,0);
|
|
}
|
|
|