A reasonable shell command system
This commit is contained in:
parent
774f757265
commit
89cb3c8dbd
@ -8,6 +8,12 @@
|
||||
#include <version.h>
|
||||
#include <termios.h>
|
||||
|
||||
/*
|
||||
* This is basically the same as a userspace buffered/unbuffered
|
||||
* termio call. These are the same sorts of things I would use in
|
||||
* a text editor in userspace, but with the internal kernel calls
|
||||
* rather than system calls.
|
||||
*/
|
||||
static struct termios old;
|
||||
|
||||
void set_unbuffered(fs_node_t * dev) {
|
||||
@ -21,7 +27,9 @@ void set_buffered(fs_node_t * dev) {
|
||||
ioctl_fs(dev, TCSETSF, &old);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* TODO move this to the printf module
|
||||
*/
|
||||
void fs_printf(fs_node_t * device, char *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
@ -32,6 +40,13 @@ void fs_printf(fs_node_t * device, char *fmt, ...) {
|
||||
write_fs(device, 0, strlen(buffer), (uint8_t *)buffer);
|
||||
}
|
||||
|
||||
/*
|
||||
* Quick readline implementation.
|
||||
*
|
||||
* Most of these TODOs are things I've done already in older code:
|
||||
* TODO tabcompletion would be nice
|
||||
* TODO history is also nice
|
||||
*/
|
||||
int debug_shell_readline(fs_node_t * dev, char * linebuf, int max) {
|
||||
int read = 0;
|
||||
set_unbuffered(dev);
|
||||
@ -62,6 +77,9 @@ int debug_shell_readline(fs_node_t * dev, char * linebuf, int max) {
|
||||
return read;
|
||||
}
|
||||
|
||||
/*
|
||||
* Tasklet for running a userspace application.
|
||||
*/
|
||||
void debug_shell_run_sh(void * data, char * name) {
|
||||
|
||||
fs_node_t * tty = (fs_node_t *)data;
|
||||
@ -83,11 +101,73 @@ void debug_shell_run_sh(void * data, char * name) {
|
||||
task_exit(42);
|
||||
}
|
||||
|
||||
/*
|
||||
* We're going to have a list of shell commands.
|
||||
* We'll search through it linearly because I don't
|
||||
* care to write a hashmap right now. Maybe later.
|
||||
*/
|
||||
struct shell_command {
|
||||
char * name;
|
||||
int (*function) (fs_node_t * tty, int argc, char * argv[]);
|
||||
char * description;
|
||||
};
|
||||
|
||||
#define NUM_COMMANDS 4
|
||||
static struct shell_command shell_commands[NUM_COMMANDS];
|
||||
|
||||
/*
|
||||
* Shell commands
|
||||
*/
|
||||
static int shell_create_userspace_shell(fs_node_t * tty, int argc, char * argv[]) {
|
||||
int pid = create_kernel_tasklet(debug_shell_run_sh, "[[k-sh]]", tty);
|
||||
fs_printf(tty, "Shell started with pid = %d\n", pid);
|
||||
process_t * child_task = process_from_pid(pid);
|
||||
sleep_on(child_task->wait_queue);
|
||||
return child_task->status;
|
||||
}
|
||||
|
||||
static int shell_echo(fs_node_t * tty, int argc, char * argv[]) {
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
fs_printf(tty, "%s ", argv[i]);
|
||||
}
|
||||
fs_printf(tty, "\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int shell_help(fs_node_t * tty, int argc, char * argv[]) {
|
||||
struct shell_command * sh = &shell_commands[0];
|
||||
while (sh->name) {
|
||||
fs_printf(tty, "%s - %s\n", sh->name, sh->description);
|
||||
sh++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct shell_command shell_commands[NUM_COMMANDS] = {
|
||||
{"shell", &shell_create_userspace_shell,
|
||||
"Runs a userspace shell on this tty."},
|
||||
{"echo", &shell_echo,
|
||||
"Prints arguments."},
|
||||
{"help", &shell_help,
|
||||
"Prints a list of possible shell commands and their descriptions."},
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
/*
|
||||
* A TTY object to pass to the tasklets for handling
|
||||
* serial-tty interaction. This probably shouldn't
|
||||
* be done as tasklets - TTYs should just be able
|
||||
* to wrap existing fs_nodes themselves, but that's
|
||||
* a problem for another day.
|
||||
*/
|
||||
struct tty_o {
|
||||
fs_node_t * node;
|
||||
fs_node_t * tty;
|
||||
};
|
||||
|
||||
/*
|
||||
* These tasklets handle tty-serial interaction.
|
||||
*/
|
||||
void debug_shell_handle_in(void * data, char * name) {
|
||||
struct tty_o * tty = (struct tty_o *)data;
|
||||
while (1) {
|
||||
@ -106,10 +186,22 @@ void debug_shell_handle_out(void * data, char * name) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Determine the size of a smart terminal that we don't have direct
|
||||
* termios access to. This is done by sending a cursor-move command
|
||||
* that will put the cursor into the lower right corner and then
|
||||
* requesting the cursor position report. We then read and parse
|
||||
* the position report. In the case where the terminal on the other
|
||||
* end is actually dumb, we end up waiting for some input and
|
||||
* then timing out.
|
||||
* TODO with asyncio support, the timeout should actually work.
|
||||
* consider also using an alarm (which I also don't have)
|
||||
*/
|
||||
void divine_size(fs_node_t * dev, int * width, int * height) {
|
||||
char tmp[100];
|
||||
int read = 0;
|
||||
unsigned long start_tick = timer_ticks;
|
||||
/* Move cursor, Request position, Reset cursor */
|
||||
fs_printf(dev, "\033[1000;1000H\033[6n\033[H");
|
||||
while (1) {
|
||||
char buf[1];
|
||||
@ -125,13 +217,20 @@ void divine_size(fs_node_t * dev, int * width, int * height) {
|
||||
}
|
||||
}
|
||||
if (timer_ticks - start_tick >= 2) {
|
||||
/*
|
||||
* We've timed out. This will only be triggered
|
||||
* when we eventually receive something, though
|
||||
*/
|
||||
*width = 80;
|
||||
*height = 23;
|
||||
/* Clear and return */
|
||||
fs_printf(dev, "\033[J");
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* Clear */
|
||||
fs_printf(dev, "\033[J");
|
||||
/* Break up the result into two strings */
|
||||
for (unsigned int i = 0; i < strlen(tmp); i++) {
|
||||
if (tmp[i] == ';') {
|
||||
tmp[i] = '\0';
|
||||
@ -139,12 +238,25 @@ void divine_size(fs_node_t * dev, int * width, int * height) {
|
||||
}
|
||||
}
|
||||
char * h = (char *)((uintptr_t)tmp + strlen(tmp)+1);
|
||||
/* And then parse it into numbers */
|
||||
*height = atoi(tmp);
|
||||
*width = atoi(h);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tasklet for managing the kernel serial console.
|
||||
* This is basically a very simple shell, with access
|
||||
* to some internal kernel commands, and (eventually)
|
||||
* debugging routines.
|
||||
*/
|
||||
void debug_shell_run(void * data, char * name) {
|
||||
/*
|
||||
* We will run on the first serial port.
|
||||
* TODO detect that this failed
|
||||
*/
|
||||
fs_node_t * tty = kopen("/dev/ttyS0", 0);
|
||||
|
||||
/* Our prompt will include the version number of the current kernel */
|
||||
char version_number[1024];
|
||||
sprintf(version_number, __kernel_version_format,
|
||||
__kernel_version_major,
|
||||
@ -152,6 +264,7 @@ void debug_shell_run(void * data, char * name) {
|
||||
__kernel_version_lower,
|
||||
__kernel_version_suffix);
|
||||
|
||||
/* We will convert the serial interface into an actual TTY */
|
||||
int master, slave;
|
||||
struct winsize size = {0,0,0,0};
|
||||
|
||||
@ -162,8 +275,10 @@ void debug_shell_run(void * data, char * name) {
|
||||
size.ws_row = height;
|
||||
size.ws_col = width;
|
||||
|
||||
/* Convert the serial line into a TTY */
|
||||
openpty(&master, &slave, NULL, NULL, &size);
|
||||
|
||||
/* Attach the serial to the TTY interface */
|
||||
struct tty_o _tty = {.node = current_process->fds->entries[master], .tty = tty};
|
||||
|
||||
create_kernel_tasklet(debug_shell_handle_in, "[kttydebug-in]", (void *)&_tty);
|
||||
@ -172,16 +287,21 @@ void debug_shell_run(void * data, char * name) {
|
||||
/* Set the device to be the actual TTY slave */
|
||||
tty = current_process->fds->entries[slave];
|
||||
|
||||
int retval = 0;
|
||||
|
||||
while (1) {
|
||||
char command[512];
|
||||
|
||||
/* Print out the prompt */
|
||||
fs_printf(tty, "%s-%s %s# ", __kernel_name, version_number, current_process->wd_name);
|
||||
if (retval) {
|
||||
fs_printf(tty, "%s-%s %d %s# ", __kernel_name, version_number, retval, current_process->wd_name);
|
||||
} else {
|
||||
fs_printf(tty, "%s-%s %s# ", __kernel_name, version_number, current_process->wd_name);
|
||||
}
|
||||
|
||||
/* Read a line */
|
||||
debug_shell_readline(tty, command, 511);
|
||||
|
||||
/* Do something with it */
|
||||
char * arg = strdup(command);
|
||||
char * pch; /* Tokenizer pointer */
|
||||
char * save; /* We use the reentrant form of strtok */
|
||||
@ -202,20 +322,16 @@ void debug_shell_run(void * data, char * name) {
|
||||
argv[tokenid] = NULL;
|
||||
/* Tokens are now stored in argv. */
|
||||
|
||||
if (!strcmp(argv[0], "shell")) {
|
||||
int pid = create_kernel_tasklet(debug_shell_run_sh, "[[k-sh]]", tty);
|
||||
fs_printf(tty, "Shell started with pid = %d\n", pid);
|
||||
process_t * child_task = process_from_pid(pid);
|
||||
sleep_on(child_task->wait_queue);
|
||||
fs_printf(tty, "Shell returned: %d\n", child_task->status);
|
||||
} else if (!strcmp(argv[0], "echo")) {
|
||||
for (int i = 1; i < tokenid; ++i) {
|
||||
fs_printf(tty, "%s ", argv[i]);
|
||||
/* Parse the command string */
|
||||
struct shell_command * sh = &shell_commands[0];
|
||||
while (sh->name) {
|
||||
if (!strcmp(sh->name, argv[0])) {
|
||||
retval = sh->function(tty, tokenid, argv);
|
||||
break;
|
||||
}
|
||||
fs_printf(tty, "\n");
|
||||
} else if (!strcmp(argv[0], "setuid")) {
|
||||
current_process->user = atoi(argv[1]);
|
||||
} else {
|
||||
sh++;
|
||||
}
|
||||
if (sh->name == NULL) {
|
||||
fs_printf(tty, "Unrecognized command: %s\n", argv[0]);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user