From 826e973afbdccc3c61e587667e63a40bc70f5e22 Mon Sep 17 00:00:00 2001 From: QuzarDC Date: Thu, 25 Dec 2025 13:41:55 -0500 Subject: [PATCH 1/7] Copyright updates --- Makefile | 2 +- include/kosh.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 5e9e17c..2156c6c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # libkosh for KallistiOS ##version## # # Makefile -# (c)2002 Dan Potter +# (c)2002 Megan Potter TARGET = libkosh.a OBJS = builtin.o chdir.o kosh.o input.o diff --git a/include/kosh.h b/include/kosh.h index e26625e..b5d590f 100644 --- a/include/kosh.h +++ b/include/kosh.h @@ -1,7 +1,7 @@ /* Libkosh for KallistiOS ##version## kosh.h - (c)2002 Dan Potter + (c)2002 Megan Potter */ #ifndef __KOSH_KOSH_H From 9806739ae35f56b118964d099ce099371df8a5f1 Mon Sep 17 00:00:00 2001 From: QuzarDC Date: Thu, 25 Dec 2025 13:43:54 -0500 Subject: [PATCH 2/7] Update empty params to `void`. --- builtin.c | 4 ++-- builtin.h | 4 ++-- include/kosh.h | 6 +++--- input.c | 2 +- kosh.c | 6 +++--- kosh.h | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/builtin.c b/builtin.c index c577883..7fd3b8b 100644 --- a/builtin.c +++ b/builtin.c @@ -414,7 +414,7 @@ void kosh_builtin_remove(const char * cmd) { } /* Setup all our builtins */ -void kosh_builtins_init() { +void kosh_builtins_init(void) { SLIST_INIT(&builtins); /* table of the builtin commands, their help mesg, and their handler funcs */ @@ -443,7 +443,7 @@ void kosh_builtins_init() { } /* Kill our list */ -void kosh_builtins_shutdown() { +void kosh_builtins_shutdown(void) { builtin_t * c, * n; c = SLIST_FIRST(&builtins); diff --git a/builtin.h b/builtin.h index 6a8a1aa..07b9db3 100644 --- a/builtin.h +++ b/builtin.h @@ -9,8 +9,8 @@ /* the func */ int kosh_builtin_command(int argc, char *argv[]); -void kosh_builtins_init(); -void kosh_builtins_shutdown(); +void kosh_builtins_init(void); +void kosh_builtins_shutdown(void); #endif diff --git a/include/kosh.h b/include/kosh.h index b5d590f..eb8f8d5 100644 --- a/include/kosh.h +++ b/include/kosh.h @@ -12,10 +12,10 @@ __BEGIN_DECLS /** Call to begin a KOSH thread on the currently open conio. Assumes that you've already initiated the libconio system. */ -int kosh_init(); +int kosh_init(void); /** Call to terminate the KOSH thread on the currently open conio. */ -void kosh_shutdown(); +void kosh_shutdown(void); /** Call to add a built-in command to the shell. */ void kosh_builtin_add(const char * cmd, const char * helpmsg, void (*handler)(int argc, char *argv[])); @@ -24,7 +24,7 @@ void kosh_builtin_add(const char * cmd, const char * helpmsg, void (*handler)(in void kosh_builtin_remove(const char * cmd); /** Call to wait for the KOSH thread to exit (e.g. in a standalone shell) */ -void kosh_join(); +void kosh_join(void); __END_DECLS diff --git a/input.c b/input.c index 1088490..1a02ace 100644 --- a/input.c +++ b/input.c @@ -7,7 +7,7 @@ #include "builtin.h" /* read command line, try to execute builtin commands, otherwise externals (this coming soon) */ -void input_oneloop() { +void input_oneloop(void) { int argc; char *argv[20]; /* limit to 20 args (should be plenty) */ char *str; diff --git a/kosh.c b/kosh.c index adb118f..aa9e116 100644 --- a/kosh.c +++ b/kosh.c @@ -32,13 +32,13 @@ static void * kosh_thread(void *p) { return NULL; } -void kosh_join() { +void kosh_join(void) { assert( kosh_exit != KE_JOINED ); thd_join(thd, NULL); kosh_exit = KE_JOINED; } -int kosh_init() { +int kosh_init(void) { if (kosh_exit != KE_JOINED) return -1; @@ -48,7 +48,7 @@ int kosh_init() { return 0; } -void kosh_shutdown() { +void kosh_shutdown(void) { if (kosh_exit != KE_JOINED) { kosh_exit = KE_YES; kosh_join(); diff --git a/kosh.h b/kosh.h index 451dcca..9287b55 100644 --- a/kosh.h +++ b/kosh.h @@ -11,7 +11,7 @@ extern volatile int kosh_exit; -void input_oneloop(); +void input_oneloop(void); #endif From b275908cf45cb66ae1b21c0ba2cfd999e87ab170 Mon Sep 17 00:00:00 2001 From: QuzarDC Date: Thu, 25 Dec 2025 13:46:13 -0500 Subject: [PATCH 3/7] make: Make paths more flexible. These were built for the old kos-ports paradigm and hard-coded relative paths presuming you are building in the kos-ports folder. Updated to use the env var to find that folder. --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 2156c6c..1df57d2 100644 --- a/Makefile +++ b/Makefile @@ -12,5 +12,5 @@ include $(KOS_BASE)/addons/Makefile.prefab # creates the kos link to the headers create_kos_link: - rm -f ../include/kosh - ln -s ../libkosh/include ../include/kosh + rm -f $(KOS_PORTS)/include/kosh + ln -s $(CURDIR)/include $(KOS_PORTS)/include/kosh From 930a9202165e5c69e8474a67991710b5f7d442dc Mon Sep 17 00:00:00 2001 From: QuzarDC Date: Thu, 25 Dec 2025 14:08:23 -0500 Subject: [PATCH 4/7] Whitespace cleanup. --- builtin.c | 602 ++++++++++++++++++++++++------------------------- builtin.h | 1 - chdir.c | 84 +++---- include/kosh.h | 6 +- input.c | 70 +++--- kosh.c | 52 ++--- 6 files changed, 407 insertions(+), 408 deletions(-) diff --git a/builtin.c b/builtin.c index 7fd3b8b..6e2da26 100644 --- a/builtin.c +++ b/builtin.c @@ -13,445 +13,445 @@ #include "kosh.h" typedef struct builtin { - SLIST_ENTRY(builtin) list; + SLIST_ENTRY(builtin) list; - const char *command; - const char *desc; + const char *command; + const char *desc; - void (*handler)(int argc, char *argv[]); + void (*handler)(int argc, char *argv[]); } builtin_t; static SLIST_HEAD(builtin_list, builtin) builtins; /* print command list/desc */ static void builtin_help(int argc, char *argv[]) { - builtin_t * b; - - if (argc > 1) { - SLIST_FOREACH(b, &builtins, list) { - if (strcmp(argv[1], b->command) == 0) { - conio_printf("%s - %s\n", b->command, b->desc); - return; - } - } - } else { - conio_printf("KOSH commands:\n "); - - SLIST_FOREACH(b, &builtins, list) { - conio_printf("%s ", b->command); - } - - conio_printf("\nType 'help command' for a description.\n"); - } + builtin_t *b; + + if (argc > 1) { + SLIST_FOREACH(b, &builtins, list) { + if (strcmp(argv[1], b->command) == 0) { + conio_printf("%s - %s\n", b->command, b->desc); + return; + } + } + } else { + conio_printf("KOSH commands:\n "); + + SLIST_FOREACH(b, &builtins, list) { + conio_printf("%s ", b->command); + } + + conio_printf("\nType 'help command' for a description.\n"); + } } /* exit the shell */ static void builtin_exit(int argc, char *argv[]) { - if (argc > 1) - conio_printf("Superfluous arguments to exit\n"); - else - kosh_exit = 1; + if (argc > 1) + conio_printf("Superfluous arguments to exit\n"); + else + kosh_exit = 1; } /* list the contents of the directory */ static void builtin_ls(int argc, char *argv[]) { - dirent_t *ent; - uint32 hnd; - char dir[NAME_MAX]; - int lflag; + dirent_t *ent; + uint32 hnd; + char dir[NAME_MAX]; + int lflag; - lflag = 0; + lflag = 0; - if (argc > 1) { - if (strcmp(argv[1], "-l") == 0) - lflag = 1; - } + if (argc > 1) { + if (strcmp(argv[1], "-l") == 0) + lflag = 1; + } - getcwd(dir, NAME_MAX); + getcwd(dir, NAME_MAX); - conio_printf("Reading %s\n", dir); + conio_printf("Reading %s\n", dir); - hnd = fs_open(dir, O_RDONLY | O_DIR); - if (!hnd) { - conio_printf("Error opening %s\n", dir); - return; - } + hnd = fs_open(dir, O_RDONLY | O_DIR); + if (!hnd) { + conio_printf("Error opening %s\n", dir); + return; + } - while ((ent = fs_readdir(hnd)) != NULL) { - if (lflag) - conio_printf("%12d bytes %s\n", ent->size, ent->name); - else - conio_printf("%s\n", ent->name); - } + while ((ent = fs_readdir(hnd)) != NULL) { + if (lflag) + conio_printf("%12d bytes %s\n", ent->size, ent->name); + else + conio_printf("%s\n", ent->name); + } - fs_close(hnd); + fs_close(hnd); } /* change the current directory */ static void builtin_cd(int argc, char *argv[]) { - if (argc != 2) { - conio_printf("usage: cd dir\n"); - return; - } + if (argc != 2) { + conio_printf("usage: cd dir\n"); + return; + } - kosh_chdir(argv[1]); + kosh_chdir(argv[1]); } /* print the current directory */ static void builtin_pwd(int argc, char *argv[]) { - char buff[NAME_MAX]; + char buff[NAME_MAX]; - conio_printf("%s\n", getcwd(buff, NAME_MAX)); + conio_printf("%s\n", getcwd(buff, NAME_MAX)); } /* clear the screen */ static void builtin_clear(int argc, char *argv[]) { - conio_clear(); - conio_gotoxy(0, 0); + conio_clear(); + conio_gotoxy(0, 0); } /* echo, heh */ static void builtin_echo(int argc, char *argv[]) { - int i; + int i; - for (i = 1; i < argc; i++) - conio_printf("%s ", argv[i]); - conio_printf("\n"); + for (i = 1; i < argc; i++) + conio_printf("%s ", argv[i]); + conio_printf("\n"); } /* cat text files to the screen */ static void builtin_cat(int argc, char *argv[]) { - uint32 hnd; - char buff[256]; - char fn[NAME_MAX]; - int cnt; - - if (argc != 2) { - conio_printf("usage: cat filename\n"); - return; - } - - /* the the abs path */ - makeabspath(fn, argv[1], NAME_MAX); - - hnd = fs_open(fn, O_RDONLY); - if (!hnd) { - conio_printf("Error opening %s\n", fn); - return; - } - - while ((cnt = fs_read(hnd, buff, 256)) != 0) { - int i; - - for (i = 0; i < cnt; i++) { - if (buff[i] == '\r') { - if (buff[i + 1] != '\0') - break; - if (buff[++i] != '\n') - conio_putch('\r'); - } - if (isascii(buff[i])) - conio_putch(buff[i]); - } - } - - fs_close(hnd); + uint32 hnd; + char buff[256]; + char fn[NAME_MAX]; + int cnt; + + if (argc != 2) { + conio_printf("usage: cat filename\n"); + return; + } + + /* the the abs path */ + makeabspath(fn, argv[1], NAME_MAX); + + hnd = fs_open(fn, O_RDONLY); + if (!hnd) { + conio_printf("Error opening %s\n", fn); + return; + } + + while ((cnt = fs_read(hnd, buff, 256)) != 0) { + int i; + + for (i = 0; i < cnt; i++) { + if (buff[i] == '\r') { + if (buff[i + 1] != '\0') + break; + if (buff[++i] != '\n') + conio_putch('\r'); + } + if (isascii(buff[i])) + conio_putch(buff[i]); + } + } + + fs_close(hnd); } /* dump files as hex */ static void builtin_hd(int argc, char *argv[]) { - char fn[NAME_MAX]; - char buff[10]; - uint32 hnd; - int cnt; - - if (argc != 2) { - conio_printf("usage: hd file\n"); - return; - } - - makeabspath(fn, argv[1], NAME_MAX); - - hnd = fs_open(fn, O_RDONLY); - if (!hnd) { - conio_printf("Error opening %s\n", fn); - return; - } - - /* do the hexdump */ - while ((cnt = fs_read(hnd, buff, 10)) != 0) { - int i; - - for (i = 0; i < cnt; i++) - conio_printf("%02x ", (unsigned char) buff[i]); - conio_deadvance_cursor(); - //cursor.col = 10 * 3 + 2; - conio_putch('|'); - for (i = 0; i < cnt; i++) { - if (isprint((int)buff[i])) - conio_putch(buff[i]); - else - conio_putch('.'); - } - conio_printf("|\n"); - } - - fs_close(hnd); + char fn[NAME_MAX]; + char buff[10]; + uint32 hnd; + int cnt; + + if (argc != 2) { + conio_printf("usage: hd file\n"); + return; + } + + makeabspath(fn, argv[1], NAME_MAX); + + hnd = fs_open(fn, O_RDONLY); + if (!hnd) { + conio_printf("Error opening %s\n", fn); + return; + } + + /* do the hexdump */ + while ((cnt = fs_read(hnd, buff, 10)) != 0) { + int i; + + for (i = 0; i < cnt; i++) + conio_printf("%02x ", (unsigned char) buff[i]); + conio_deadvance_cursor(); + //cursor.col = 10 * 3 + 2; + conio_putch('|'); + for (i = 0; i < cnt; i++) { + if (isprint((int)buff[i])) + conio_putch(buff[i]); + else + conio_putch('.'); + } + conio_printf("|\n"); + } + + fs_close(hnd); } /* copy files */ static void builtin_cp(int argc, char *argv[]) { - char buff[256]; - char srcfn[NAME_MAX]; - char destfn[NAME_MAX]; - uint32 src; - uint32 dest; - int cnt; + char buff[256]; + char srcfn[NAME_MAX]; + char destfn[NAME_MAX]; + uint32 src; + uint32 dest; + int cnt; - if (argc != 3) { - conio_printf("usage: cp src dest\n"); - return; - } + if (argc != 3) { + conio_printf("usage: cp src dest\n"); + return; + } - /* get abs paths */ - makeabspath(srcfn, argv[1], NAME_MAX); - makeabspath(destfn, argv[2], NAME_MAX); + /* get abs paths */ + makeabspath(srcfn, argv[1], NAME_MAX); + makeabspath(destfn, argv[2], NAME_MAX); - src = fs_open(srcfn, O_RDONLY); - if (!src) { conio_printf("Error opening %s\n", srcfn); return; } + src = fs_open(srcfn, O_RDONLY); + if (!src) { conio_printf("Error opening %s\n", srcfn); return; } - dest = fs_open(destfn, O_WRONLY); - if (!dest) { conio_printf("Error opening %s\n", destfn); fs_close(src); return; } + dest = fs_open(destfn, O_WRONLY); + if (!dest) { conio_printf("Error opening %s\n", destfn); fs_close(src); return; } - while ((cnt = fs_read(src, buff, 256)) != 0) - fs_write(dest, buff, cnt); + while ((cnt = fs_read(src, buff, 256)) != 0) + fs_write(dest, buff, cnt); - fs_close(src); - fs_close(dest); + fs_close(src); + fs_close(dest); } /* unlink files */ static void builtin_rm(int argc, char *argv[]) { - char fn[NAME_MAX]; + char fn[NAME_MAX]; - if (argc != 2) { - conio_printf("usage: rm file\n"); - return; - } + if (argc != 2) { + conio_printf("usage: rm file\n"); + return; + } - /* get the abs path for the fn to rem */ - makeabspath(fn, argv[1], NAME_MAX); + /* get the abs path for the fn to rem */ + makeabspath(fn, argv[1], NAME_MAX); - if (fs_unlink(fn) != 0) - conio_printf("Error unlinking %s.\n", fn); + if (fs_unlink(fn) != 0) + conio_printf("Error unlinking %s.\n", fn); } /* create a directory */ static void builtin_mkdir(int argc, char *argv[]) { - char fn[NAME_MAX]; + char fn[NAME_MAX]; - if (argc != 2) { - conio_printf("usage: mkdir dirname\n"); - return; - } + if (argc != 2) { + conio_printf("usage: mkdir dirname\n"); + return; + } - /* get the abs path for the dir to create */ - makeabspath(fn, argv[1], NAME_MAX); + /* get the abs path for the dir to create */ + makeabspath(fn, argv[1], NAME_MAX); - if (fs_mkdir(fn) != 0) - conio_printf("Error making directory %s.\n", fn); + if (fs_mkdir(fn) != 0) + conio_printf("Error making directory %s.\n", fn); } /* delete a directory */ static void builtin_rmdir(int argc, char *argv[]) { - char fn[NAME_MAX]; + char fn[NAME_MAX]; - if (argc != 2) { - conio_printf("usage: rmdir dirname\n"); - return; - } + if (argc != 2) { + conio_printf("usage: rmdir dirname\n"); + return; + } - /* get the abs path for the dir to delete */ - makeabspath(fn, argv[1], NAME_MAX); + /* get the abs path for the dir to delete */ + makeabspath(fn, argv[1], NAME_MAX); - if (fs_rmdir(fn) != 0) - conio_printf("Error deleting directory %s.\n", fn); + if (fs_rmdir(fn) != 0) + conio_printf("Error deleting directory %s.\n", fn); } /* change console colors */ static void builtin_theme(int argc, char *argv[]) { - if (argc != 2) { - conio_printf("usage: theme themename\nchoices are 'matrix', 'c64' and 'plain'\n"); - return; - } - - if (strcmp(argv[1], "matrix") == 0) - conio_set_theme(CONIO_THEME_MATRIX); - else if (strcmp(argv[1], "c64") == 0) - conio_set_theme(CONIO_THEME_C64); - else if (strcmp(argv[1], "plain") == 0) - conio_set_theme(CONIO_THEME_PLAIN); - else - conio_printf("unknown theme\n"); + if (argc != 2) { + conio_printf("usage: theme themename\nchoices are 'matrix', 'c64' and 'plain'\n"); + return; + } + + if (strcmp(argv[1], "matrix") == 0) + conio_set_theme(CONIO_THEME_MATRIX); + else if (strcmp(argv[1], "c64") == 0) + conio_set_theme(CONIO_THEME_C64); + else if (strcmp(argv[1], "plain") == 0) + conio_set_theme(CONIO_THEME_PLAIN); + else + conio_printf("unknown theme\n"); } /* exit to the DC menus */ static void builtin_menu(int argc, char *argv[]) { - conio_printf("bye!\n"); - arch_menu(); + conio_printf("bye!\n"); + arch_menu(); } /* Mount a romdisk image */ static void builtin_mount_romdisk(int argc, char *argv[]) { - void * data; - - if (argc != 3) { - conio_printf("usage: mount_romdisk srcimg mountpoint\n"); - return; - } - - /* Load up the romdisk image */ - conio_printf("Loading the romdisk image...\n"); - if (fs_load(argv[1], &data) <= 0) { - conio_printf("error: invalid romdisk source image\n"); - if (data != NULL) - free(data); - return; - } - - /* Attempt to mount it */ - conio_printf("Mounting on %s...\n", argv[2]); - if (fs_romdisk_mount(argv[2], (const uint8 *)data, 1) < 0) { - conio_printf("error: can't mount romdisk image\n"); - free(data); - return; - } - - conio_printf("Success!\n"); - return; + void *data; + + if (argc != 3) { + conio_printf("usage: mount_romdisk srcimg mountpoint\n"); + return; + } + + /* Load up the romdisk image */ + conio_printf("Loading the romdisk image...\n"); + if (fs_load(argv[1], &data) <= 0) { + conio_printf("error: invalid romdisk source image\n"); + if (data != NULL) + free(data); + return; + } + + /* Attempt to mount it */ + conio_printf("Mounting on %s...\n", argv[2]); + if (fs_romdisk_mount(argv[2], (const uint8 *)data, 1) < 0) { + conio_printf("error: can't mount romdisk image\n"); + free(data); + return; + } + + conio_printf("Success!\n"); + return; } /* Print out a list of running threads */ static void builtin_threads(int argc, char *argv[]) { - thd_pslist(conio_printf); + thd_pslist(conio_printf); } /* Print out memory usage statistics */ static void builtin_mstats(int argc, char *argv[]) { - struct mallinfo mi = mallinfo(); - - conio_printf("Memory usage:\n"); - conio_printf(" Max system bytes = %10lu\n", - (unsigned long)(mi.usmblks)); - conio_printf(" System bytes = %10lu\n", - (unsigned long)(mi.arena + mi.hblkhd)); - conio_printf(" In use bytes = %10lu\n", - (unsigned long)(mi.uordblks + mi.hblkhd)); - conio_printf(" Current sbrk = %08lx\n", - (unsigned long)(sbrk(0))); + struct mallinfo mi = mallinfo(); + + conio_printf("Memory usage:\n"); + conio_printf(" Max system bytes = %10lu\n", + (unsigned long)(mi.usmblks)); + conio_printf(" System bytes = %10lu\n", + (unsigned long)(mi.arena + mi.hblkhd)); + conio_printf(" In use bytes = %10lu\n", + (unsigned long)(mi.uordblks + mi.hblkhd)); + conio_printf(" Current sbrk = %08lx\n", + (unsigned long)(sbrk(0))); } /* Kill the whole KOS program */ static void builtin_die(int argc, char *argv[]) { - conio_printf("Goodbye cruel world...\n"); - arch_exit(); + conio_printf("Goodbye cruel world...\n"); + arch_exit(); } #ifdef _arch_dreamcast /* Do a screen dump -- note, won't always work 100% due to threading stuff */ static void builtin_sshot(int argc, char *argv[]) { - char fn[NAME_MAX]; + char fn[NAME_MAX]; - if (argc != 2) { - conio_printf("usage: sshot outfile.ppm\n"); - return; - } + if (argc != 2) { + conio_printf("usage: sshot outfile.ppm\n"); + return; + } - /* get the abs path for the dir to create */ - makeabspath(fn, argv[1], NAME_MAX); + /* get the abs path for the dir to create */ + makeabspath(fn, argv[1], NAME_MAX); - conio_printf("Doing a screen shot to %s\n", fn); - vid_screen_shot(fn); - conio_printf("Done!\n"); + conio_printf("Doing a screen shot to %s\n", fn); + vid_screen_shot(fn); + conio_printf("Done!\n"); } #endif /* try to run a builtin command, return 0 if there is no such builtin */ int kosh_builtin_command(int argc, char *argv[]) { - builtin_t * b; + builtin_t *b; - SLIST_FOREACH(b, &builtins, list) { - if (strcmp(argv[0], b->command) == 0) { - b->handler(argc, argv); - return 1; - } - } + SLIST_FOREACH(b, &builtins, list) { + if (strcmp(argv[0], b->command) == 0) { + b->handler(argc, argv); + return 1; + } + } - return 0; + return 0; } -void kosh_builtin_add(const char * cmd, const char * helpmsg, void (*handler)(int argc, char *argv[])) { - builtin_t * b; +void kosh_builtin_add(const char *cmd, const char *helpmsg, void (*handler)(int argc, char *argv[])) { + builtin_t *b; - b = (builtin_t *)calloc(1, sizeof(builtin_t)); - b->command = cmd; - b->desc = helpmsg; - b->handler = handler; + b = (builtin_t *)calloc(1, sizeof(builtin_t)); + b->command = cmd; + b->desc = helpmsg; + b->handler = handler; - SLIST_INSERT_HEAD(&builtins, b, list); + SLIST_INSERT_HEAD(&builtins, b, list); } -void kosh_builtin_remove(const char * cmd) { - builtin_t * b; +void kosh_builtin_remove(const char *cmd) { + builtin_t *b; - SLIST_FOREACH(b, &builtins, list) { - if (strcmp(cmd, b->command) == 0) { - SLIST_REMOVE(&builtins, b, builtin, list); - free(b); - } - } + SLIST_FOREACH(b, &builtins, list) { + if(strcmp(cmd, b->command) == 0) { + SLIST_REMOVE(&builtins, b, builtin, list); + free(b); + } + } } /* Setup all our builtins */ void kosh_builtins_init(void) { - SLIST_INIT(&builtins); - - /* table of the builtin commands, their help mesg, and their handler funcs */ - kosh_builtin_add("help", "Hmm...", builtin_help); - kosh_builtin_add("exit", "Exit kosh", builtin_exit); - kosh_builtin_add("ls", "List contents of directories", builtin_ls); - kosh_builtin_add("cd", "Change directory", builtin_cd); - kosh_builtin_add("pwd", "Print the current directory", builtin_pwd); - kosh_builtin_add("clear", "Clear the screen", builtin_clear); - kosh_builtin_add("echo", "Echo text to the console", builtin_echo); - kosh_builtin_add("cat", "Display text files to the console", builtin_cat); - kosh_builtin_add("hd", "Dump files as hex to the console", builtin_hd); - kosh_builtin_add("cp", "Copy files", builtin_cp); - kosh_builtin_add("rm", "Remove files", builtin_rm); - kosh_builtin_add("mkdir", "Create a directory", builtin_mkdir); - kosh_builtin_add("rmdir", "Delete a directory", builtin_rmdir); - kosh_builtin_add("theme", "Change the console color theme", builtin_theme); - kosh_builtin_add("menu", "Exit KOS to the bios menus", builtin_menu); - kosh_builtin_add("mount_romdisk", "Mount a romdisk image", builtin_mount_romdisk); - kosh_builtin_add("threads", "Get a list of running threads", builtin_threads); - kosh_builtin_add("mstats", "Get memory statistics", builtin_mstats); - kosh_builtin_add("die", "Call arch_exit()", builtin_die); + SLIST_INIT(&builtins); + + /* table of the builtin commands, their help mesg, and their handler funcs */ + kosh_builtin_add("help", "Hmm...", builtin_help); + kosh_builtin_add("exit", "Exit kosh", builtin_exit); + kosh_builtin_add("ls", "List contents of directories", builtin_ls); + kosh_builtin_add("cd", "Change directory", builtin_cd); + kosh_builtin_add("pwd", "Print the current directory", builtin_pwd); + kosh_builtin_add("clear", "Clear the screen", builtin_clear); + kosh_builtin_add("echo", "Echo text to the console", builtin_echo); + kosh_builtin_add("cat", "Display text files to the console", builtin_cat); + kosh_builtin_add("hd", "Dump files as hex to the console", builtin_hd); + kosh_builtin_add("cp", "Copy files", builtin_cp); + kosh_builtin_add("rm", "Remove files", builtin_rm); + kosh_builtin_add("mkdir", "Create a directory", builtin_mkdir); + kosh_builtin_add("rmdir", "Delete a directory", builtin_rmdir); + kosh_builtin_add("theme", "Change the console color theme", builtin_theme); + kosh_builtin_add("menu", "Exit KOS to the bios menus", builtin_menu); + kosh_builtin_add("mount_romdisk", "Mount a romdisk image", builtin_mount_romdisk); + kosh_builtin_add("threads", "Get a list of running threads", builtin_threads); + kosh_builtin_add("mstats", "Get memory statistics", builtin_mstats); + kosh_builtin_add("die", "Call arch_exit()", builtin_die); #ifdef _arch_dreamcast - kosh_builtin_add("sshot", "Make a screen shot", builtin_sshot); + kosh_builtin_add("sshot", "Make a screen shot", builtin_sshot); #endif } /* Kill our list */ void kosh_builtins_shutdown(void) { - builtin_t * c, * n; + builtin_t *c, *n; - c = SLIST_FIRST(&builtins); - while (c) { - n = SLIST_NEXT(c, list); - free(c); - c = n; - } + c = SLIST_FIRST(&builtins); + while (c) { + n = SLIST_NEXT(c, list); + free(c); + c = n; + } - SLIST_INIT(&builtins); + SLIST_INIT(&builtins); } diff --git a/builtin.h b/builtin.h index 07b9db3..db1fe04 100644 --- a/builtin.h +++ b/builtin.h @@ -13,5 +13,4 @@ void kosh_builtins_init(void); void kosh_builtins_shutdown(void); #endif - diff --git a/chdir.c b/chdir.c index 2f6c14a..49e6658 100644 --- a/chdir.c +++ b/chdir.c @@ -14,61 +14,61 @@ static char cwd[NAME_MAX]; /* read a pathname based on the current directory and turn it into an abs one, we don't check for validity, or really do anything except handle ..'s and .'s */ int makeabspath(char *buff, char *path, size_t size) { - int numtxts; - int i; - char *txts[32]; /* max of 32...should be nuff */ - char *rslash; + int numtxts; + int i; + char *txts[32]; /* max of 32...should be nuff */ + char *rslash; - numtxts = 0; + numtxts = 0; - /* check if path is already absolute */ - if (path[0] == '/') { - strncpy(buff, path, size); - return 0; - } + /* check if path is already absolute */ + if (path[0] == '/') { + strncpy(buff, path, size); + return 0; + } - /* split the path into tokens */ - for (numtxts = 0; numtxts < 32;) { - if ((txts[numtxts] = strsep(&path, "/")) == NULL) - break; - if (*txts[numtxts] != '\0') - numtxts++; - } + /* split the path into tokens */ + for (numtxts = 0; numtxts < 32;) { + if ((txts[numtxts] = strsep(&path, "/")) == NULL) + break; + if (*txts[numtxts] != '\0') + numtxts++; + } - /* start from the current directory */ - strncpy(buff, cwd, size); + /* start from the current directory */ + strncpy(buff, cwd, size); - for (i = 0; i < numtxts; i++) { - if (strcmp(txts[i], "..") == 0) { - if ((rslash = strrchr(buff, '/')) != NULL) - *rslash = '\0'; - } else if (strcmp(txts[i], ".") == 0) { - /* do nothing */ - } else { - if (buff[strlen(buff) - 1] != '/') - strncat(buff, "/", size - 1 - strlen(buff)); - strncat(buff, txts[i], size - 1 - strlen(buff)); - } - } + for (i = 0; i < numtxts; i++) { + if (strcmp(txts[i], "..") == 0) { + if ((rslash = strrchr(buff, '/')) != NULL) + *rslash = '\0'; + } else if (strcmp(txts[i], ".") == 0) { + /* do nothing */ + } else { + if (buff[strlen(buff) - 1] != '/') + strncat(buff, "/", size - 1 - strlen(buff)); + strncat(buff, txts[i], size - 1 - strlen(buff)); + } + } - /* make sure it's not empty */ - if (buff[0] == '\0') { - buff[0] = '/'; - buff[1] = '\0'; - } + /* make sure it's not empty */ + if (buff[0] == '\0') { + buff[0] = '/'; + buff[1] = '\0'; + } - return 0; + return 0; } /* change the current directory (dir is an absolute path for now) */ int kosh_chdir(char *dir) { - char buff[NAME_MAX]; + char buff[NAME_MAX]; - makeabspath(buff, dir, NAME_MAX); - strncpy(cwd, buff, NAME_MAX); + makeabspath(buff, dir, NAME_MAX); + strncpy(cwd, buff, NAME_MAX); - fs_chdir(cwd); + fs_chdir(cwd); - return 0; + return 0; } diff --git a/include/kosh.h b/include/kosh.h index eb8f8d5..fca9d8f 100644 --- a/include/kosh.h +++ b/include/kosh.h @@ -18,15 +18,15 @@ int kosh_init(void); void kosh_shutdown(void); /** Call to add a built-in command to the shell. */ -void kosh_builtin_add(const char * cmd, const char * helpmsg, void (*handler)(int argc, char *argv[])); +void kosh_builtin_add(const char *cmd, const char *helpmsg, void (*handler)(int argc, char *argv[])); /** Call to remove an added built-in command from the shell. */ -void kosh_builtin_remove(const char * cmd); +void kosh_builtin_remove(const char *cmd); /** Call to wait for the KOSH thread to exit (e.g. in a standalone shell) */ void kosh_join(void); __END_DECLS -#endif /* __KOSH_KOSH_H */ +#endif /* __KOSH_KOSH_H */ diff --git a/input.c b/input.c index 1a02ace..7e42ae4 100644 --- a/input.c +++ b/input.c @@ -8,40 +8,40 @@ /* read command line, try to execute builtin commands, otherwise externals (this coming soon) */ void input_oneloop(void) { - int argc; - char *argv[20]; /* limit to 20 args (should be plenty) */ - char *str; - char buff[256]; - - /* Wait for a line of input */ - conio_printf("$ "); - if (conio_input_getline(-1, buff, 255) < 0) { - kosh_exit = 1; - return; - } - - str = buff; - - /* we don't care if the user just hit enter */ - if (buff[0] == '\0') - return; - - /* seperate the string into args */ - for (argc = 0; argc < 20;) { - if ((argv[argc] = strsep(&str, " \t\n")) == NULL) - break; - if (*argv[argc] != '\0') - argc++; - } - - /* try to run the command as a builtin */ - if (!kosh_builtin_command(argc, argv)) { - /* makeabspath(buff, argv[0], NAME_MAX); */ - - /* the builtin failed so we try an external */ - /* if (process->load_and_exec(buff, argc, argv)) - conio_printf("kosh: %s: command not found\n", buff); */ - conio_printf("Sorry, OS mode not available yet\n"); - } + int argc; + char *argv[20]; /* limit to 20 args (should be plenty) */ + char *str; + char buff[256]; + + /* Wait for a line of input */ + conio_printf("$ "); + if (conio_input_getline(-1, buff, 255) < 0) { + kosh_exit = 1; + return; + } + + str = buff; + + /* we don't care if the user just hit enter */ + if (buff[0] == '\0') + return; + + /* seperate the string into args */ + for (argc = 0; argc < 20;) { + if ((argv[argc] = strsep(&str, " \t\n")) == NULL) + break; + if (*argv[argc] != '\0') + argc++; + } + + /* try to run the command as a builtin */ + if (!kosh_builtin_command(argc, argv)) { + /* makeabspath(buff, argv[0], NAME_MAX); */ + + /* the builtin failed so we try an external */ + /* if (process->load_and_exec(buff, argc, argv)) + conio_printf("kosh: %s: command not found\n", buff); */ + conio_printf("Sorry, OS mode not available yet\n"); + } } diff --git a/kosh.c b/kosh.c index aa9e116..16af615 100644 --- a/kosh.c +++ b/kosh.c @@ -12,46 +12,46 @@ #include "builtin.h" /* global exit flag: if someone sets this we quit */ -#define KE_YES 1 // Requesting exit -#define KE_NO 0 // Run mode -#define KE_QUITTING -1 // Thread has returned -#define KE_JOINED -2 // Not running +#define KE_YES 1 // Requesting exit +#define KE_NO 0 // Run mode +#define KE_QUITTING -1 // Thread has returned +#define KE_JOINED -2 // Not running volatile int kosh_exit = KE_JOINED; /* Our exit semaphore - signaled when the thread exits */ static kthread_t *thd; -static void * kosh_thread(void *p) { - conio_printf(" **** KOSH, The KallistiOS Shell ****\n"); - kosh_chdir("/"); - while (kosh_exit == KE_NO) - input_oneloop(); - conio_printf("Kosh is done\n"); - kosh_exit = KE_QUITTING; +static void *kosh_thread(void *p) { + conio_printf(" **** KOSH, The KallistiOS Shell ****\n"); + kosh_chdir("/"); + while (kosh_exit == KE_NO) + input_oneloop(); + conio_printf("Kosh is done\n"); + kosh_exit = KE_QUITTING; - return NULL; + return NULL; } void kosh_join(void) { - assert( kosh_exit != KE_JOINED ); - thd_join(thd, NULL); - kosh_exit = KE_JOINED; + assert( kosh_exit != KE_JOINED ); + thd_join(thd, NULL); + kosh_exit = KE_JOINED; } int kosh_init(void) { - if (kosh_exit != KE_JOINED) - return -1; + if (kosh_exit != KE_JOINED) + return -1; - kosh_exit = KE_NO; - kosh_builtins_init(); - thd = thd_create(0, kosh_thread, NULL); - return 0; + kosh_exit = KE_NO; + kosh_builtins_init(); + thd = thd_create(0, kosh_thread, NULL); + return 0; } void kosh_shutdown(void) { - if (kosh_exit != KE_JOINED) { - kosh_exit = KE_YES; - kosh_join(); - } - kosh_builtins_shutdown(); + if (kosh_exit != KE_JOINED) { + kosh_exit = KE_YES; + kosh_join(); + } + kosh_builtins_shutdown(); } From a43bf093311a89b531b346b91be760da1406b857 Mon Sep 17 00:00:00 2001 From: QuzarDC Date: Thu, 25 Dec 2025 14:09:21 -0500 Subject: [PATCH 5/7] Clean up ends of headers. Ensure appropriate extra newline and comment for the header guard endif. --- builtin.h | 2 +- chdir.h | 3 ++- kosh.h | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/builtin.h b/builtin.h index db1fe04..a10bf4b 100644 --- a/builtin.h +++ b/builtin.h @@ -12,5 +12,5 @@ int kosh_builtin_command(int argc, char *argv[]); void kosh_builtins_init(void); void kosh_builtins_shutdown(void); -#endif +#endif /* _KOSH_BUILTIN_H */ diff --git a/chdir.h b/chdir.h index 7c1c183..87b3ac3 100644 --- a/chdir.h +++ b/chdir.h @@ -11,4 +11,5 @@ int makeabspath(char *buff, char *path, size_t size); int kosh_chdir(char *dir); -#endif +#endif /* _KOSH_CHDIR_H */ + diff --git a/kosh.h b/kosh.h index 9287b55..ca6f5fc 100644 --- a/kosh.h +++ b/kosh.h @@ -13,5 +13,5 @@ extern volatile int kosh_exit; void input_oneloop(void); -#endif +#endif /* _KOSH_KOSH_H */ From 8633eaf457c641cac3db9ae698dff67bf38171a8 Mon Sep 17 00:00:00 2001 From: QuzarDC Date: Thu, 25 Dec 2025 14:33:09 -0500 Subject: [PATCH 6/7] Use stdint rather than arch/types.h Additionally rearranged includes to ensure we weren't accidentally still importing arch/types.h through kos.h. --- builtin.c | 25 +++++++++++++++++-------- chdir.c | 3 ++- chdir.h | 2 +- input.c | 2 ++ kosh.c | 2 +- kosh.h | 1 - 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/builtin.c b/builtin.c index 6e2da26..14dad13 100644 --- a/builtin.c +++ b/builtin.c @@ -4,10 +4,19 @@ * (C) 2000 Jordan DeLong */ -#include -#include #include +#include +#include +#include +#include +#include + #include + +#include +#include +#include +#include #include "builtin.h" #include "chdir.h" #include "kosh.h" @@ -56,7 +65,7 @@ static void builtin_exit(int argc, char *argv[]) { /* list the contents of the directory */ static void builtin_ls(int argc, char *argv[]) { dirent_t *ent; - uint32 hnd; + uint32_t hnd; char dir[NAME_MAX]; int lflag; @@ -121,7 +130,7 @@ static void builtin_echo(int argc, char *argv[]) { /* cat text files to the screen */ static void builtin_cat(int argc, char *argv[]) { - uint32 hnd; + uint32_t hnd; char buff[256]; char fn[NAME_MAX]; int cnt; @@ -162,7 +171,7 @@ static void builtin_cat(int argc, char *argv[]) { static void builtin_hd(int argc, char *argv[]) { char fn[NAME_MAX]; char buff[10]; - uint32 hnd; + uint32_t hnd; int cnt; if (argc != 2) { @@ -204,8 +213,8 @@ static void builtin_cp(int argc, char *argv[]) { char buff[256]; char srcfn[NAME_MAX]; char destfn[NAME_MAX]; - uint32 src; - uint32 dest; + uint32_t src; + uint32_t dest; int cnt; if (argc != 3) { @@ -321,7 +330,7 @@ static void builtin_mount_romdisk(int argc, char *argv[]) { /* Attempt to mount it */ conio_printf("Mounting on %s...\n", argv[2]); - if (fs_romdisk_mount(argv[2], (const uint8 *)data, 1) < 0) { + if (fs_romdisk_mount(argv[2], (const uint8_t *)data, 1) < 0) { conio_printf("error: can't mount romdisk image\n"); free(data); return; diff --git a/chdir.c b/chdir.c index 49e6658..a0eaf7e 100644 --- a/chdir.c +++ b/chdir.c @@ -3,7 +3,8 @@ * * (C) 2000 Jordan DeLong */ -#include + +#include #include #include #include "kosh.h" diff --git a/chdir.h b/chdir.h index 87b3ac3..916ea3e 100644 --- a/chdir.h +++ b/chdir.h @@ -6,7 +6,7 @@ #ifndef _KOSH_CHDIR_H #define _KOSH_CHDIR_H -#include +#include int makeabspath(char *buff, char *path, size_t size); int kosh_chdir(char *dir); diff --git a/input.c b/input.c index 7e42ae4..212b8a8 100644 --- a/input.c +++ b/input.c @@ -3,6 +3,8 @@ * * (C) 2000 Jordan DeLong */ + +#include #include "kosh.h" #include "builtin.h" diff --git a/kosh.c b/kosh.c index 16af615..c6149bf 100644 --- a/kosh.c +++ b/kosh.c @@ -6,7 +6,7 @@ #include #include -#include +#include #include "kosh.h" #include "chdir.h" #include "builtin.h" diff --git a/kosh.h b/kosh.h index ca6f5fc..284319f 100644 --- a/kosh.h +++ b/kosh.h @@ -6,7 +6,6 @@ #ifndef _KOSH_KOSH_H #define _KOSH_KOSH_H -#include #include extern volatile int kosh_exit; From 6c5b8313259f9670efdb1272c6daff2df9b8847d Mon Sep 17 00:00:00 2001 From: QuzarDC Date: Thu, 25 Dec 2025 23:32:44 -0500 Subject: [PATCH 7/7] Use FOREACH_SAFE macro rather than building it from scratch. --- builtin.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/builtin.c b/builtin.c index 14dad13..c6b1e9a 100644 --- a/builtin.c +++ b/builtin.c @@ -455,11 +455,9 @@ void kosh_builtins_init(void) { void kosh_builtins_shutdown(void) { builtin_t *c, *n; - c = SLIST_FIRST(&builtins); - while (c) { - n = SLIST_NEXT(c, list); + SLIST_FOREACH_SAFE(c, &builtins, list, n) { + SLIST_REMOVE(&builtins, c, builtin, list); free(c); - c = n; } SLIST_INIT(&builtins);