From e2be315cfc87395a7301d7eefe88caee2609c06d Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Sun, 1 Oct 2023 19:47:26 -0400 Subject: [PATCH] Reentrant strtok for kernel debugger --- include/string.h | 1 + lib/libc/string.c | 15 +++++++++++---- sys/kern/debug.c | 13 +++++-------- sys/kern/libc.c | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/include/string.h b/include/string.h index ce6025f..e228e61 100644 --- a/include/string.h +++ b/include/string.h @@ -19,6 +19,7 @@ char *strcat(char *s, const char *append); char *strncat(char *s, const char *append, size_t count); char *strtok(char *str, const char *delim); +char *strtok_r(char *str, const char *delim, char **last); #endif /* __STRING_H__ */ diff --git a/lib/libc/string.c b/lib/libc/string.c index a0e9aaf..8915dd0 100644 --- a/lib/libc/string.c +++ b/lib/libc/string.c @@ -119,13 +119,12 @@ strlen(const char *str) } char * -strtok(char *str, const char *delim) +strtok_r(char *str, const char *delim, char **last) { char *rval; - static char *last; if (str == NULL) - str = last; + str = *last; // Skip deliminaters in the front while ((str[0] != '\0') && strchr(delim, str[0])) { @@ -148,11 +147,19 @@ strtok(char *str, const char *delim) str[0] = '\0'; str++; } - last = str; + *last = str; return rval; } +char * +strtok(char *str, const char *delim) +{ + static char *last; + + return strtok_r(str, delim, &last); +} + void * memset(void *dst, int c, size_t length) { diff --git a/sys/kern/debug.c b/sys/kern/debug.c index f61d621..95aa9e3 100644 --- a/sys/kern/debug.c +++ b/sys/kern/debug.c @@ -249,7 +249,7 @@ Debug_Prompt() { int argc; char *argv[DEBUG_MAX_ARGS]; - char *nextArg; + char *nextArg, *last; char buf[DEBUG_MAX_LINE]; kprintf("Entered Debugger!\n"); @@ -266,16 +266,13 @@ Debug_Prompt() Console_Gets((char *)&buf, DEBUG_MAX_LINE); // parse input - argv[0] = buf; - for (argc = 1; argc < DEBUG_MAX_ARGS; argc++) { - nextArg = strchr(argv[argc - 1], ' '); + nextArg = strtok_r(buf, " \t\r\n", &last); + for (argc = 0; argc < DEBUG_MAX_ARGS; argc++) { if (nextArg == NULL) - { break; - } - *nextArg = '\0'; - argv[argc] = nextArg + 1; + argv[argc] = nextArg; + nextArg = strtok_r(NULL, " \t\r\n", &last); } if (strcmp(argv[0], "continue") == 0) { diff --git a/sys/kern/libc.c b/sys/kern/libc.c index 22bc6a3..51c28ec 100644 --- a/sys/kern/libc.c +++ b/sys/kern/libc.c @@ -20,6 +20,40 @@ strchr(const char *s, int c) } } +char * +strtok_r(char *str, const char *delim, char **last) +{ + char *rval; + + if (str == NULL) + str = *last; + + // Skip deliminaters in the front + while ((str[0] != '\0') && strchr(delim, str[0])) { + str++; + } + + // We've reached the end of the string + if (str[0] == '\0') + return NULL; + + // Return first non-delim until the next + rval = str; + + // Skip deliminaters in the front + while ((str[0] != '\0') && !strchr(delim, str[0])) { + str++; + } + + if (str[0] != '\0') { + str[0] = '\0'; + str++; + } + *last = str; + + return rval; +} + char * strcpy(char *to, const char *from) {