Fixed cmd bugs

This commit is contained in:
2025-03-28 16:02:44 -05:00
parent 4eb933c3f6
commit 5ab7dd1f57
7 changed files with 101 additions and 21 deletions

View File

@ -123,7 +123,7 @@ void assertMemoryRangeMatchesImpl(
assertDeprecatedImpl(__FILE__, __LINE__, message) assertDeprecatedImpl(__FILE__, __LINE__, message)
#define assertStrLenMax(str, len, message) \ #define assertStrLenMax(str, len, message) \
assertTrue(strlen(str) <= len, message) assertTrue(strlen(str) < len, message)
#define assertStrLenMin(str, len, message) \ #define assertStrLenMin(str, len, message) \
assertTrue(strlen(str) >= len, message) assertTrue(strlen(str) >= len, message)

View File

@ -44,13 +44,12 @@ consolevar_t * consoleRegVar(
void consolePrint(const char_t *message, ...) { void consolePrint(const char_t *message, ...) {
char_t buffer[CONSOLE_LINE_MAX]; char_t buffer[CONSOLE_LINE_MAX];
va_list args; va_list args;
va_start(args, message); va_start(args, message);
size_t len = vsnprintf(buffer, CONSOLE_LINE_MAX, message, args); int32_t len = stringFormatVA(buffer, CONSOLE_LINE_MAX, message, args);
va_end(args); va_end(args);
assertTrue(len < CONSOLE_LINE_MAX, "Message is too long.");
// Move all lines back // Move all lines back
memoryMove( memoryMove(
CONSOLE.line[0], CONSOLE.line[0],
@ -75,7 +74,7 @@ void consoleExec(const char_t *line) {
"Too many commands in the buffer." "Too many commands in the buffer."
); );
char_t buffer[CONSOLE_LINE_MAX + 1]; char_t buffer[CONSOLE_LINE_MAX];
size_t i = 0, j = 0; size_t i = 0, j = 0;
char_t c; char_t c;
consoleexecstate_t state = CONSOLE_EXEC_STATE_INITIAL; consoleexecstate_t state = CONSOLE_EXEC_STATE_INITIAL;
@ -109,14 +108,19 @@ void consoleExec(const char_t *line) {
if(c == '"') { if(c == '"') {
// Can't handle quotes within the command. // Can't handle quotes within the command.
consolePrint("Error: Invalid command."); consolePrint("Invalid command");
while(c != '\0' && c != ';') c = line[++i]; while(c != '\0' && c != ';') c = line[++i];
continue; continue;
} }
assertTrue(j < CONSOLE_LINE_MAX, "Command is too long.");
buffer[j++] = c; buffer[j++] = c;
i++; i++;
if(j >= CONSOLE_LINE_MAX) {
consolePrint("Command too long");
state = CONSOLE_EXEC_STATE_FULLY_PARSED;
continue;
}
break; break;
case CONSOLE_EXEC_STATE_CMD_PARSED: case CONSOLE_EXEC_STATE_CMD_PARSED:
@ -139,7 +143,6 @@ void consoleExec(const char_t *line) {
break; break;
case CONSOLE_EXEC_STATE_FIND_ARG: case CONSOLE_EXEC_STATE_FIND_ARG:
if(c == '\0' || c == ';') { if(c == '\0' || c == ';') {
state = CONSOLE_EXEC_STATE_CMD_FINISHED; state = CONSOLE_EXEC_STATE_CMD_FINISHED;
continue; continue;
@ -166,6 +169,12 @@ void consoleExec(const char_t *line) {
buffer[j++] = c; buffer[j++] = c;
i++; i++;
if(j >= CONSOLE_LINE_MAX) {
consolePrint("Arg too long");
state = CONSOLE_EXEC_STATE_FULLY_PARSED;
continue;
}
break; break;
case CONSOLE_EXEC_STATE_PARSE_ARG_QUOTED: case CONSOLE_EXEC_STATE_PARSE_ARG_QUOTED:
@ -176,21 +185,29 @@ void consoleExec(const char_t *line) {
} }
if(c == '\0' || c == ';') { if(c == '\0' || c == ';') {
consolePrint("Error: Unterminated quoted argument."); consolePrint("Unterminated quote");
return; state = CONSOLE_EXEC_STATE_FULLY_PARSED;
continue;
} }
if(c == '\\') { if(c == '\\') {
c = line[++i]; c = line[++i];
if(c == '\0' || c == ';') { if(c == '\0' || c == ';') {
consolePrint("Error: Unterminated quoted argument."); consolePrint("Unterminated quote");
return; state = CONSOLE_EXEC_STATE_FULLY_PARSED;
continue;
} }
} }
buffer[j++] = c; buffer[j++] = c;
i++; i++;
if(j >= CONSOLE_LINE_MAX) {
consolePrint("Arg too long");
state = CONSOLE_EXEC_STATE_FULLY_PARSED;
continue;
}
break; break;
case CONSOLE_EXEC_STATE_ARG_PARSED: case CONSOLE_EXEC_STATE_ARG_PARSED:
@ -233,7 +250,7 @@ void consoleExec(const char_t *line) {
} }
if(exec->cmd == NULL) { if(exec->cmd == NULL) {
consolePrint("Error: Command '%s' not found.", exec->command); consolePrint("Command not found", exec->command);
exec = NULL; exec = NULL;
state = CONSOLE_EXEC_STATE_INITIAL; state = CONSOLE_EXEC_STATE_INITIAL;
break; break;

View File

@ -21,7 +21,7 @@ typedef struct {
typedef void (*consolecmdfunc_t)(const consolecmdexec_t *exec); typedef void (*consolecmdfunc_t)(const consolecmdexec_t *exec);
typedef struct consolecmd_s { typedef struct consolecmd_s {
char_t name[CONSOLE_CMD_NAME_MAX + 1]; char_t name[CONSOLE_CMD_NAME_MAX];
consolecmdfunc_t function; consolecmdfunc_t function;
} consolecmd_t; } consolecmd_t;

View File

@ -14,8 +14,8 @@ typedef struct consolevar_s consolevar_t;
typedef void (*consolevarchanged_t)(const consolevar_t *var); typedef void (*consolevarchanged_t)(const consolevar_t *var);
typedef struct consolevar_s { typedef struct consolevar_s {
char_t name[CONSOLE_VAR_NAME_MAX + 1]; char_t name[CONSOLE_VAR_NAME_MAX];
char_t value[CONSOLE_VAR_VALUE_MAX + 1]; char_t value[CONSOLE_VAR_VALUE_MAX];
consolevarchanged_t events[CONSOLE_VAR_EVENTS_MAX]; consolevarchanged_t events[CONSOLE_VAR_EVENTS_MAX];
uint8_t eventCount; uint8_t eventCount;
} consolevar_t; } consolevar_t;

View File

@ -7,6 +7,7 @@
#include "string.h" #include "string.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "util/memory.h"
bool_t stringIsWhitespace(const char_t c) { bool_t stringIsWhitespace(const char_t c) {
return isspace(c); return isspace(c);
@ -15,8 +16,9 @@ bool_t stringIsWhitespace(const char_t c) {
void stringCopy(char_t *dest, const char_t *src, const size_t destSize) { void stringCopy(char_t *dest, const char_t *src, const size_t destSize) {
assertNotNull(dest, "dest must not be NULL"); assertNotNull(dest, "dest must not be NULL");
assertNotNull(src, "src must not be NULL"); assertNotNull(src, "src must not be NULL");
assertTrue(destSize > 0, "destSize must be greater than 0");
assertStrLenMax(src, destSize, "src is too long"); assertStrLenMax(src, destSize, "src is too long");
strncpy(dest, src, destSize); memoryCopy(dest, src, strlen(src) + 1);
} }
int stringCompare(const char_t *str1, const char_t *str2) { int stringCompare(const char_t *str1, const char_t *str2) {
@ -47,4 +49,37 @@ char_t * stringToken(char_t *str, const char_t *delim) {
assertNotNull(str, "str must not be NULL"); assertNotNull(str, "str must not be NULL");
assertNotNull(delim, "delim must not be NULL"); assertNotNull(delim, "delim must not be NULL");
return strtok(str, delim); return strtok(str, delim);
}
int32_t stringFormat(
char_t *dest,
const size_t destSize,
char_t *format,
...
) {
assertNotNull(dest, "dest must not be NULL");
assertNotNull(format, "format must not be NULL");
assertTrue(destSize > 0, "destSize must be greater than 0");
va_list args;
va_start(args, format);
int32_t result = stringFormatVA(dest, destSize, format, args);
va_end(args);
return result;
}
int32_t stringFormatVA(
char_t *dest,
const size_t destSize,
char_t *format,
va_list args
) {
assertNotNull(dest, "dest must not be NULL");
assertNotNull(format, "format must not be NULL");
assertTrue(destSize > 0, "destSize must be greater than 0");
int32_t ret = vsnprintf(dest, destSize, format, args);
assertTrue(ret >= 0, "Failed to format string.");
assertTrue(ret < destSize, "Formatted string is too long.");
return ret;
} }

View File

@ -52,4 +52,31 @@ void stringTrim(char_t *str);
* @param delim The delimiter to split by. * @param delim The delimiter to split by.
* @return A pointer to the next token in the string. * @return A pointer to the next token in the string.
*/ */
char_t * stringToken(char_t *str, const char_t *delim); char_t * stringToken(char_t *str, const char_t *delim);
/**
* Formats a string.
*
* @param dest The destination string.
* @param destSize The size of the destination string exc. null terminator.
* @param format The format string.
* @param ... The arguments to format.
* @return The number of characters written.
*/
int32_t stringFormat(char_t *dest, const size_t destSize, char_t *format, ...);
/**
* Formats a string using a va_list.
*
* @param dest The destination string.
* @param destSize The size of the destination string exc. null terminator.
* @param format The format string.
* @param args The va_list of arguments.
* @return The number of characters written.
*/
int32_t stringFormatVA(
char_t *dest,
const size_t destSize,
char_t *format,
va_list args
);

View File

@ -25,9 +25,10 @@ int main() {
consoleRegCmd("exit", cmdExit); consoleRegCmd("exit", cmdExit);
consoleRegVar("test", "Hello", testChange); consoleRegVar("test", "Hello", testChange);
consolePrint(" = Dusk Console = "); consolePrint("init");
// consolePrint(" = Dusk Console = ");
char_t buffer[CONSOLE_LINE_MAX]; char_t buffer[CONSOLE_LINE_MAX * 10];
while(fgets(buffer, sizeof(buffer), stdin)) { while(fgets(buffer, sizeof(buffer), stdin)) {
consoleExec(buffer); consoleExec(buffer);
consoleProcess(); consoleProcess();