111 lines
3.1 KiB
C
111 lines
3.1 KiB
C
#include "console.h"
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include "main.h" // shouldn't be here
|
|
|
|
void consoleLog(const char *string) {
|
|
struct ConsoleEntry *new_entry;
|
|
new_entry = malloc(sizeof(struct ConsoleEntry));
|
|
int bytes = strlen(string);
|
|
new_entry->string = malloc(bytes);
|
|
new_entry->size = bytes;
|
|
memcpy(new_entry->string, string, bytes+1);
|
|
if (console_last_entry) {
|
|
console_last_entry->next = new_entry;
|
|
new_entry->prev = console_last_entry;
|
|
new_entry->next = NULL;
|
|
} else {
|
|
console_first_entry = new_entry;
|
|
new_entry->next = NULL;
|
|
new_entry->prev = NULL;
|
|
}
|
|
console_last_entry = new_entry;
|
|
}
|
|
|
|
const char *consoleGetEntryString(const struct ConsoleEntry *entry) {
|
|
if (entry->string) {
|
|
return entry->string;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
int consoleGenerateHash(const char* string) {
|
|
int i, sum;
|
|
size_t string_length = strlen(string);
|
|
for (sum=0, i=0; i < string_length; i++) {
|
|
sum += string[i];
|
|
}
|
|
return sum % COMMAND_HASH_SIZE;
|
|
}
|
|
|
|
void consoleAddCommand(const char *command_string, void(*function)) {
|
|
int string_hash = consoleGenerateHash(command_string);
|
|
consoleLog("command added!"); // TODO: consoleLogF(formatted log)
|
|
console_commands_table[string_hash] = function;
|
|
}
|
|
|
|
char **consoleGetCommand(const char *string) {
|
|
char** command_array = NULL;
|
|
int i = 0;
|
|
int j = 1;
|
|
char last_pos = 0;
|
|
|
|
char current_char = string[i];
|
|
short done = 0;
|
|
|
|
while (!done) {
|
|
if (current_char == ' ') {
|
|
command_array = realloc(command_array, sizeof(char*)*j+1);
|
|
if (command_array == NULL) {
|
|
consoleLog("ERROR, couldn't allocate memory for command_array");
|
|
}
|
|
command_array[j] = strndup(string+last_pos, i-last_pos);
|
|
last_pos = i+1; // skip space
|
|
j++;
|
|
} else if (current_char == '\0') {
|
|
command_array = realloc(command_array, sizeof(char*)*j+1);
|
|
if (command_array == NULL) {
|
|
consoleLog("ERROR, couldn't allocate memory for command_array\n");
|
|
}
|
|
command_array[j] = strndup(string+last_pos, i-last_pos);
|
|
last_pos = i;
|
|
done = 1;
|
|
j++;
|
|
}
|
|
i++;
|
|
current_char = string[i];
|
|
}
|
|
command_array[0] = (void*)(size_t)(j-1);
|
|
return command_array;
|
|
}
|
|
|
|
void consoleFreeCommand(char **command_array) {
|
|
int j;
|
|
for (j = 1; j < (size_t)command_array[0]+1; j++) {
|
|
free(command_array[j]);
|
|
}
|
|
free(command_array);
|
|
}
|
|
|
|
void consoleProcessCommand(const char *command_string) {
|
|
char **command = consoleGetCommand(command_string);
|
|
int command_hash = consoleGenerateHash(command[1]);
|
|
consoleLog(command[1]);
|
|
if (console_commands_table[command_hash]) {
|
|
// BUG: command count has to be +10 ??? FIXME
|
|
char test[12];
|
|
itoa((size_t)command[0], test, 10);
|
|
consoleLog(test);
|
|
if ((size_t)command[0] == 1) { // no arguments
|
|
(*console_commands_table[command_hash])(NULL);
|
|
} else if ((size_t)command[0] == 2) { // 1 arg
|
|
(*console_commands_table[command_hash])(command[2]);
|
|
} else if ((size_t)command[0] == 3) { // 2 args
|
|
(*console_commands_table[command_hash])(command[2], command[3]);
|
|
}
|
|
} else {
|
|
consoleLog("Command not found!");
|
|
}
|
|
consoleFreeCommand(command);
|
|
}
|