Command aliasing
This commit is contained in:
@@ -11,4 +11,7 @@ bind right right;
|
|||||||
bind e accept;
|
bind e accept;
|
||||||
bind enter accept;
|
bind enter accept;
|
||||||
bind q cancel;
|
bind q cancel;
|
||||||
bind esc quit;
|
bind esc quit;
|
||||||
|
|
||||||
|
alias test "echo \"test\"";
|
||||||
|
bind p test;
|
53
src/console/cmd/cmdalias.h
Normal file
53
src/console/cmd/cmdalias.h
Normal file
@@ -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: <name> <command>");
|
||||||
|
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]);
|
||||||
|
}
|
@@ -18,6 +18,7 @@
|
|||||||
#include "console/cmd/cmdexec.h"
|
#include "console/cmd/cmdexec.h"
|
||||||
#include "console/cmd/cmdbind.h"
|
#include "console/cmd/cmdbind.h"
|
||||||
#include "console/cmd/cmdtoggleconsole.h"
|
#include "console/cmd/cmdtoggleconsole.h"
|
||||||
|
#include "console/cmd/cmdalias.h"
|
||||||
|
|
||||||
console_t CONSOLE;
|
console_t CONSOLE;
|
||||||
|
|
||||||
@@ -32,6 +33,7 @@ void consoleInit() {
|
|||||||
consoleRegCmd("exec", cmdExec);
|
consoleRegCmd("exec", cmdExec);
|
||||||
consoleRegCmd("bind", cmdBind);
|
consoleRegCmd("bind", cmdBind);
|
||||||
consoleRegCmd("toggleconsole", cmdToggleConsole);
|
consoleRegCmd("toggleconsole", cmdToggleConsole);
|
||||||
|
consoleRegCmd("alias", cmdAlias);
|
||||||
|
|
||||||
#if CONSOLE_POSIX
|
#if CONSOLE_POSIX
|
||||||
threadInit(&CONSOLE.thread, consoleInputThread);
|
threadInit(&CONSOLE.thread, consoleInputThread);
|
||||||
@@ -286,14 +288,34 @@ void consoleExec(const char_t *line) {
|
|||||||
break;
|
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);
|
consolePrint("Command \"%s\" not found", exec->command);
|
||||||
exec = NULL;
|
exec = NULL;
|
||||||
state = CONSOLE_EXEC_STATE_INITIAL;
|
state = CONSOLE_EXEC_STATE_INITIAL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prep for next command.
|
// Prep for next command.
|
||||||
exec = NULL;
|
exec = NULL;
|
||||||
state = CONSOLE_EXEC_STATE_INITIAL;
|
state = CONSOLE_EXEC_STATE_INITIAL;
|
||||||
|
@@ -8,6 +8,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "consolevar.h"
|
#include "consolevar.h"
|
||||||
#include "consolecmd.h"
|
#include "consolecmd.h"
|
||||||
|
#include "consolealias.h"
|
||||||
|
|
||||||
#if CONSOLE_POSIX
|
#if CONSOLE_POSIX
|
||||||
#include "thread/thread.h"
|
#include "thread/thread.h"
|
||||||
@@ -43,6 +44,9 @@ typedef struct {
|
|||||||
consolecmdexec_t execBuffer[CONSOLE_EXEC_BUFFER_MAX];
|
consolecmdexec_t execBuffer[CONSOLE_EXEC_BUFFER_MAX];
|
||||||
uint32_t execBufferCount;
|
uint32_t execBufferCount;
|
||||||
|
|
||||||
|
consolealias_t aliases[CONSOLE_ALIAS_MAX];
|
||||||
|
uint32_t aliasCount;
|
||||||
|
|
||||||
consolecmd_t *cmdGet;
|
consolecmd_t *cmdGet;
|
||||||
consolecmd_t *cmdSet;
|
consolecmd_t *cmdSet;
|
||||||
|
|
||||||
|
14
src/console/consolealias.h
Normal file
14
src/console/consolealias.h
Normal file
@@ -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;
|
@@ -15,7 +15,8 @@
|
|||||||
#define CONSOLE_LINE_MAX 256
|
#define CONSOLE_LINE_MAX 256
|
||||||
#define CONSOLE_HISTORY_MAX 32
|
#define CONSOLE_HISTORY_MAX 32
|
||||||
#define CONSOLE_EXEC_BUFFER_MAX 16
|
#define CONSOLE_EXEC_BUFFER_MAX 16
|
||||||
|
#define CONSOLE_ALIAS_MAX 32
|
||||||
|
|
||||||
#define CONSOLE_VAR_NAME_MAX 32
|
#define CONSOLE_VAR_NAME_MAX 32
|
||||||
#define CONSOLE_VAR_VALUE_MAX 128
|
#define CONSOLE_VAR_VALUE_MAX 128
|
||||||
#define CONSOLE_VAR_EVENTS_MAX 8
|
#define CONSOLE_VAR_EVENTS_MAX 8
|
||||||
|
Reference in New Issue
Block a user