Add exec command
This commit is contained in:
@@ -9,10 +9,10 @@
|
||||
#include "console/console.h"
|
||||
|
||||
void cmdEcho(const consolecmdexec_t *exec) {
|
||||
assertTrue(
|
||||
exec->argc >= 1,
|
||||
"echo command requires 1 argument."
|
||||
);
|
||||
if(exec->argc < 1) {
|
||||
consolePrint("Expected 1 argument: <message>");
|
||||
return;
|
||||
}
|
||||
|
||||
consolePrint("%s", exec->argv[0]);
|
||||
}
|
50
src/console/cmd/cmdexec.h
Normal file
50
src/console/cmd/cmdexec.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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 "asset/assetmanager.h"
|
||||
|
||||
void cmdExec(const consolecmdexec_t *exec) {
|
||||
if(exec->argc < 1) {
|
||||
consolePrint("Expected 1 argument: <filename>");
|
||||
return;
|
||||
}
|
||||
|
||||
char_t file[FILENAME_MAX];
|
||||
stringCopy(file, exec->argv[0], FILENAME_MAX);
|
||||
if(!stringEndsWith(file, ".dcf")) {
|
||||
sprintf(
|
||||
file,
|
||||
"%s.dcf",
|
||||
exec->argv[0]
|
||||
);
|
||||
}
|
||||
|
||||
ref_t ref;
|
||||
asset_t asset;
|
||||
errorret_t ret = assetInit(&asset, file);
|
||||
if(ret.code != ERROR_OK) {
|
||||
errorPrint(ret);
|
||||
consolePrint("Failed to load asset %s", file);
|
||||
return;
|
||||
}
|
||||
|
||||
ret = assetLoad(&asset);
|
||||
if(asset.type != ASSET_TYPE_CONFIG) {
|
||||
consolePrint("Asset is not a config: %s", file);
|
||||
assetDispose(&asset);
|
||||
return;
|
||||
}
|
||||
|
||||
assetDispose(&asset);
|
||||
if(ret.code != ERROR_OK) {
|
||||
errorPrint(ret);
|
||||
consolePrint("Failed to load asset %s", file);
|
||||
return;
|
||||
}
|
||||
}
|
@@ -13,6 +13,7 @@
|
||||
#include "console/cmd/cmdecho.h"
|
||||
#include "console/cmd/cmdset.h"
|
||||
#include "console/cmd/cmdget.h"
|
||||
#include "console/cmd/cmdexec.h"
|
||||
#include "input/input.h"
|
||||
|
||||
console_t CONSOLE;
|
||||
@@ -25,6 +26,7 @@ void consoleInit() {
|
||||
CONSOLE.cmdSet = consoleRegCmd("set", cmdSet);
|
||||
consoleRegCmd("quit", cmdQuit);
|
||||
consoleRegCmd("echo", cmdEcho);
|
||||
consoleRegCmd("exec", cmdExec);
|
||||
|
||||
consolePrint(" = Dawn Console = ");
|
||||
|
||||
@@ -316,19 +318,36 @@ void consoleUpdate() {
|
||||
CONSOLE.visible = !CONSOLE.visible;
|
||||
}
|
||||
|
||||
// Exec pending buffer.
|
||||
for(uint32_t i = 0; i < CONSOLE.execBufferCount; i++) {
|
||||
consolecmdexec_t *exec = &CONSOLE.execBuffer[i];
|
||||
assertNotNull(exec->cmd, "Command execution has no command.");
|
||||
exec->cmd->function(exec);
|
||||
// Anything to exec?
|
||||
if(CONSOLE.execBufferCount == 0) {
|
||||
#if CONSOLE_POSIX
|
||||
threadMutexUnlock(&CONSOLE.execMutex);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear the exec buffer
|
||||
CONSOLE.execBufferCount = 0;
|
||||
// Copy the exec buffer, this allows exec command to work
|
||||
consolecmdexec_t execBuffer[CONSOLE_EXEC_BUFFER_MAX];
|
||||
uint32_t execBufferCount = CONSOLE.execBufferCount;
|
||||
memoryCopy(
|
||||
execBuffer,
|
||||
CONSOLE.execBuffer,
|
||||
sizeof(consolecmdexec_t) * execBufferCount
|
||||
);
|
||||
|
||||
// Clear the exec buffer and unlock so new commands can be added while we
|
||||
// process the current ones.
|
||||
CONSOLE.execBufferCount = 0;
|
||||
#if CONSOLE_POSIX
|
||||
threadMutexUnlock(&CONSOLE.execMutex);
|
||||
#endif
|
||||
|
||||
// Exec pending buffer.
|
||||
for(uint32_t i = 0; i < execBufferCount; i++) {
|
||||
consolecmdexec_t *exec = &execBuffer[i];
|
||||
assertNotNull(exec->cmd, "Command execution has no command.");
|
||||
exec->cmd->function(exec);
|
||||
}
|
||||
}
|
||||
|
||||
void consoleDispose(void) {
|
||||
|
Reference in New Issue
Block a user