Reentrant strtok for kernel debugger

This commit is contained in:
Ali Mashtizadeh 2023-10-01 19:47:26 -04:00
parent 78768d172a
commit e2be315cfc
4 changed files with 51 additions and 12 deletions

View File

@ -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__ */

View File

@ -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)
{

View File

@ -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) {

View File

@ -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)
{