diff --git a/assets/init.dcf b/assets/init.dcf index cae331c..7396103 100644 --- a/assets/init.dcf +++ b/assets/init.dcf @@ -11,4 +11,7 @@ bind right right; bind e accept; bind enter accept; bind q cancel; -bind esc quit; \ No newline at end of file +bind esc quit; + +alias test "echo \"test\""; +bind p test; \ No newline at end of file diff --git a/src/console/cmd/cmdalias.h b/src/console/cmd/cmdalias.h new file mode 100644 index 0000000..01380b2 --- /dev/null +++ b/src/console/cmd/cmdalias.h @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "console/console.h" +#include "input/input.h" +#include "util/string.h" + +void cmdAlias(const consolecmdexec_t *exec) { + if(exec->argc < 1) { + consolePrint("Expected 1 argument: "); + return; + } + + if(exec->argc == 1 || strlen(exec->argv[1]) == 0) { + // Removing the alias. + for(uint32_t i = 0; i < CONSOLE.aliasCount; i++) { + if(stringCompare(CONSOLE.aliases[i].alias, exec->argv[0]) != 0) { + continue; + } + + // Move all the later aliases down one. + if(CONSOLE.aliasCount - i - 1 > 0) { + memoryMove( + &CONSOLE.aliases[i], + &CONSOLE.aliases[i + 1], + (CONSOLE.aliasCount - i - 1) * sizeof(consolealias_t) + ); + } + CONSOLE.aliasCount--; + return; + } + + // Alias not found. + return; + } + + // Add alias + if(CONSOLE.aliasCount >= CONSOLE_ALIAS_MAX) { + consolePrint("Max aliases reached"); + return; + } + + // Create alias + consolealias_t *alias = &CONSOLE.aliases[CONSOLE.aliasCount++]; + stringCopy(alias->alias, exec->argv[0], CONSOLE_LINE_MAX); + stringCopy(alias->command, exec->argv[1], CONSOLE_LINE_MAX); + consolePrint("Added alias \"%s\" -> \"%s\"", exec->argv[0], exec->argv[1]); +} \ No newline at end of file diff --git a/src/console/console.c b/src/console/console.c index e36e9cd..0decd27 100644 --- a/src/console/console.c +++ b/src/console/console.c @@ -18,6 +18,7 @@ #include "console/cmd/cmdexec.h" #include "console/cmd/cmdbind.h" #include "console/cmd/cmdtoggleconsole.h" +#include "console/cmd/cmdalias.h" console_t CONSOLE; @@ -32,6 +33,7 @@ void consoleInit() { consoleRegCmd("exec", cmdExec); consoleRegCmd("bind", cmdBind); consoleRegCmd("toggleconsole", cmdToggleConsole); + consoleRegCmd("alias", cmdAlias); #if CONSOLE_POSIX threadInit(&CONSOLE.thread, consoleInputThread); @@ -286,14 +288,34 @@ void consoleExec(const char_t *line) { break; } - if(exec->cmd == NULL) { + // Variable not found, is there an alias that matches? + bool_t aliasFound = false; + for(uint32_t k = 0; k < CONSOLE.aliasCount; k++) { + consolealias_t *alias = &CONSOLE.aliases[k]; + if(stringCompare(alias->alias, exec->command) != 0) continue; + + // Matching alias found, we unlock the mutex and recursively call + // consoleExec to handle the alias command. + #if CONSOLE_POSIX + threadMutexUnlock(&CONSOLE.execMutex); + #endif + consoleExec(alias->command); + #if CONSOLE_POSIX + threadMutexLock(&CONSOLE.execMutex); + #endif + + aliasFound = true; + break; + } + + if(!aliasFound && exec->cmd == NULL) { consolePrint("Command \"%s\" not found", exec->command); exec = NULL; state = CONSOLE_EXEC_STATE_INITIAL; break; } } - + // Prep for next command. exec = NULL; state = CONSOLE_EXEC_STATE_INITIAL; diff --git a/src/console/console.h b/src/console/console.h index b94a5c5..833b268 100644 --- a/src/console/console.h +++ b/src/console/console.h @@ -8,6 +8,7 @@ #pragma once #include "consolevar.h" #include "consolecmd.h" +#include "consolealias.h" #if CONSOLE_POSIX #include "thread/thread.h" @@ -43,6 +44,9 @@ typedef struct { consolecmdexec_t execBuffer[CONSOLE_EXEC_BUFFER_MAX]; uint32_t execBufferCount; + consolealias_t aliases[CONSOLE_ALIAS_MAX]; + uint32_t aliasCount; + consolecmd_t *cmdGet; consolecmd_t *cmdSet; diff --git a/src/console/consolealias.h b/src/console/consolealias.h new file mode 100644 index 0000000..39aba09 --- /dev/null +++ b/src/console/consolealias.h @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "consolecmd.h" + +typedef struct { + char_t command[CONSOLE_LINE_MAX]; + char_t alias[CONSOLE_LINE_MAX]; +} consolealias_t; \ No newline at end of file diff --git a/src/console/consoledefs.h b/src/console/consoledefs.h index 6e6df0d..6c66c92 100644 --- a/src/console/consoledefs.h +++ b/src/console/consoledefs.h @@ -15,7 +15,8 @@ #define CONSOLE_LINE_MAX 256 #define CONSOLE_HISTORY_MAX 32 #define CONSOLE_EXEC_BUFFER_MAX 16 +#define CONSOLE_ALIAS_MAX 32 #define CONSOLE_VAR_NAME_MAX 32 #define CONSOLE_VAR_VALUE_MAX 128 -#define CONSOLE_VAR_EVENTS_MAX 8 \ No newline at end of file +#define CONSOLE_VAR_EVENTS_MAX 8