Implementing strtok and adding strtok to the shell

This commit is contained in:
Ali Mashtizadeh 2023-10-01 19:36:02 -04:00
parent 2695dcf2b0
commit 78768d172a
4 changed files with 49 additions and 8 deletions

View File

@ -95,16 +95,13 @@ DispatchCommand(char *buf)
}
// parse input
argv[0] = buf;
for (argc = 1; argc < SHELL_MAX_ARGS; argc++) {
nextArg = strchr(argv[argc - 1], ' ');
nextArg = strtok(buf, " \t\r\n");
for (argc = 0; argc < SHELL_MAX_ARGS; argc++) {
if (nextArg == NULL)
{
break;
}
*nextArg = '\0';
argv[argc] = nextArg + 1;
argv[argc] = nextArg;
nextArg = strtok(NULL, " \t\r\n");
}
// execute command

View File

@ -18,5 +18,7 @@ char *strncpy(char *to, const char *from, size_t len);
char *strcat(char *s, const char *append);
char *strncat(char *s, const char *append, size_t count);
char *strtok(char *str, const char *delim);
#endif /* __STRING_H__ */

View File

@ -118,6 +118,41 @@ strlen(const char *str)
return (s - str);
}
char *
strtok(char *str, const char *delim)
{
char *rval;
static char *last;
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;
}
void *
memset(void *dst, int c, size_t length)
{

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2018 Ali Mashtizadeh
* Copyright (c) 2006-2023 Ali Mashtizadeh
* All rights reserved.
*/
@ -226,6 +226,13 @@ void Debug_Assert(const char *fmt, ...)
va_start(ap, fmt);
kvprintf(fmt, consoleputc, 0, ap);
va_end(ap);
kprintf("PC %lx FP %lx\n", __builtin_return_address(0), __builtin_frame_address(0));
kprintf("PC %lx FP %lx\n", __builtin_return_address(1), __builtin_frame_address(1));
kprintf("PC %lx FP %lx\n", __builtin_return_address(2), __builtin_frame_address(2));
kprintf("PC %lx FP %lx\n", __builtin_return_address(3), __builtin_frame_address(3));
kprintf("PC %lx FP %lx\n", __builtin_return_address(4), __builtin_frame_address(4));
kprintf("PC %lx FP %lx\n", __builtin_return_address(5), __builtin_frame_address(5));
Panic("");
}