From 78768d172afa01b1a186a7e0857dd10100405d65 Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Sun, 1 Oct 2023 19:36:02 -0400 Subject: [PATCH] Implementing strtok and adding strtok to the shell --- bin/shell/shell.c | 11 ++++------- include/string.h | 2 ++ lib/libc/string.c | 35 +++++++++++++++++++++++++++++++++++ sys/kern/printf.c | 9 ++++++++- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/bin/shell/shell.c b/bin/shell/shell.c index 7e2b3ef..5902607 100644 --- a/bin/shell/shell.c +++ b/bin/shell/shell.c @@ -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 diff --git a/include/string.h b/include/string.h index cfca4d3..ce6025f 100644 --- a/include/string.h +++ b/include/string.h @@ -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__ */ diff --git a/lib/libc/string.c b/lib/libc/string.c index 1b9e50c..a0e9aaf 100644 --- a/lib/libc/string.c +++ b/lib/libc/string.c @@ -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) { diff --git a/sys/kern/printf.c b/sys/kern/printf.c index 1356d09..6fab079 100644 --- a/sys/kern/printf.c +++ b/sys/kern/printf.c @@ -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(""); }