cmd: add initial commands ls and help
This commit is contained in:
parent
4362172967
commit
0029621ada
6 changed files with 133 additions and 6 deletions
|
|
@ -1,5 +1,6 @@
|
|||
#include <kernel/common.h>
|
||||
#include <kernel/pio.h>
|
||||
#include <kernel/cmd.h>
|
||||
|
||||
#include <dev/console.h>
|
||||
#include <dev/serial.h>
|
||||
|
|
@ -62,6 +63,8 @@ void console_input(char c)
|
|||
console.l_line++;
|
||||
}
|
||||
} else if (c == '\n' || c == '\r') {
|
||||
console_write('\n');
|
||||
kexec(console.line);
|
||||
memset(console.line, 0, sizeof(console.line));
|
||||
console.l_line = 0;
|
||||
}
|
||||
|
|
@ -98,11 +101,11 @@ void console_input(char c)
|
|||
void console_write(char c)
|
||||
{
|
||||
if (!console.resizemode) {
|
||||
console.cursorpos++;
|
||||
if (console.cursorpos == console.cols)
|
||||
console_write('\n');
|
||||
if (c == '\n')
|
||||
console.cursorpos = 1;
|
||||
console.cursorpos = 0;
|
||||
if (console.cursorpos == console.cols && c != '\n')
|
||||
console_write('\n');
|
||||
console.cursorpos++;
|
||||
}
|
||||
serial_write(c);
|
||||
}
|
||||
|
|
|
|||
9
include/kernel/cmd.h
Normal file
9
include/kernel/cmd.h
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _CMD_H_
|
||||
#define _CMD_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
void kexec(const char *cmd);
|
||||
|
||||
#endif /* _CMD_H_ */
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <kernel/lock.h>
|
||||
|
||||
|
|
@ -17,6 +18,7 @@ void *memset(void *str, int c, size_t n);
|
|||
char *strcpy(char *dest, const char *src);
|
||||
char *strncpy(char *dest, const char *src, size_t n);
|
||||
char *strtok(char *str, const char *delim, char **llast);
|
||||
bool strempty(const char *s);
|
||||
char **strsplit(const char *str, char delim, int *num);
|
||||
size_t strlen(const char *c);
|
||||
int atoi(const char *a);
|
||||
|
|
|
|||
91
init/cmd.c
Normal file
91
init/cmd.c
Normal 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("# ");
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
22
lib/string.c
22
lib/string.c
|
|
@ -144,6 +144,23 @@ char *strtok(char *str, const char *delim, char **llast)
|
|||
return ret;
|
||||
}
|
||||
|
||||
bool strempty(const char *s)
|
||||
{
|
||||
if(!s)
|
||||
return true;
|
||||
|
||||
if(!*s)
|
||||
return true;
|
||||
|
||||
size_t i = strlen(s);
|
||||
while(i--) {
|
||||
if(s[i] != ' ')
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
char **strsplit(const char *s, char delim, int *n)
|
||||
{
|
||||
int num = 0;
|
||||
|
|
@ -162,6 +179,11 @@ char **strsplit(const char *s, char delim, int *n)
|
|||
ret[idx][i - last] = 0;
|
||||
idx++;
|
||||
last = i + 1;
|
||||
|
||||
if(ret[idx - 1][0] == 0) {
|
||||
kfree(ret[idx - 1]);
|
||||
idx--;
|
||||
}
|
||||
}
|
||||
}
|
||||
ret[idx] = kmalloc(strlen(s) - last + 1, ALLOC_KERN);
|
||||
|
|
|
|||
Loading…
Reference in a new issue