cmd: add initial commands ls and help

This commit is contained in:
Michael Shaw 2023-10-20 23:56:01 -04:00
commit 0029621ada
6 changed files with 133 additions and 6 deletions

91
init/cmd.c Normal file
View file

@ -0,0 +1,91 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/cmd.h>
#include <kernel/slab.h>
#include <dev/console.h>
#include <fs/vfs.h>
#include <string.h>
int kcmd_help(int argc, char **argv);
int kcmd_ls(int argc, char **argv);
#define KCMD_DECL(name) \
{ \
#name, kcmd_##name \
}
struct kcmd {
const char *name;
int (*func)(int argc, char **argv);
};
struct kcmd cmd_list[] = { KCMD_DECL(help), KCMD_DECL(ls) };
int kcmd_help(int argc, char **argv)
{
kprintf("Available commands:\n");
for (size_t i = 0; i < sizeof(cmd_list) / sizeof(struct kcmd); i++) {
kprintf(" %s\n", cmd_list[i].name);
}
return 0;
}
int kcmd_ls(int argc, char **argv)
{
char *path;
if (!argv[1] || strempty(argv[1]))
path = "/";
else
path = argv[1];
DIR *dir = opendir(path);
if (!dir) {
kprintf("Failed to open directory %s\n", path);
return 1;
}
struct dirent *ent;
while ((ent = readdir(dir))) {
if (ent->name[0] == '.')
continue;
kprintf("%s\n", ent->name);
}
return 0;
}
void kexec(const char *cmd)
{
int n;
char **spl = strsplit(cmd, ' ', &n);
if (!spl)
return;
if (!spl[0] || strempty(spl[0]))
goto cleanup;
/* do command */
bool found = false;
for (size_t i = 0; i < sizeof(cmd_list) / sizeof(struct kcmd); i++) {
if (!strcmp(cmd_list[i].name, spl[0])) {
cmd_list[i].func(n, spl);
found = true;
break;
}
}
if (!found)
kprintf("Command not found: '%s'\n", spl[0]);
cleanup:
for (int i = 0; i < n; i++) {
kfree(spl[i]);
}
kfree(spl);
kprintf("# ");
}

View file

@ -50,8 +50,8 @@ static inline void _pic_init()
cli();
pic_mask(4, 0);
pic_init();
sti();
console_resize();
sti();
while (!console_ready())
;
@ -129,7 +129,6 @@ void kmain(void)
kprintf(LOG_DEBUG "Root partition: disk=%X-%X part=%X-%X\n", kroot_disk.a, kroot_disk.b, kroot_part.a, kroot_part.b);
#endif /* KDEBUG */
console_init();
acpi_init();
pci_init();
@ -151,6 +150,7 @@ void kmain_post_stack_init()
_pic_init();
console_init();
kprintf("\n\n# ");
yield();
}