Add subprocesses and convert terminal to an app
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
#include "src/lib/string.h"
|
||||
#include "src/user/syscall.h"
|
||||
|
||||
int main(uint64_t argc, const char **argv) {
|
||||
for (uint64_t i = 1; i < argc; i++) {
|
||||
if (i > 1) {
|
||||
write(1, " ", 1);
|
||||
}
|
||||
write(1, argv[i], string_length(argv[i]));
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#include "src/user/syscall.h"
|
||||
|
||||
int main() {
|
||||
write(1, "Hello from C!\n", 14);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
#include "src/lib/memory.h"
|
||||
#include "src/lib/string.h"
|
||||
#include "src/user/syscall.h"
|
||||
#include <stdint.h>
|
||||
|
||||
static void help(uint8_t argc, char **argv);
|
||||
static void cd(uint8_t argc, char **argv);
|
||||
|
||||
#define WRITE_S(s) write(1, s, sizeof(s) - 1);
|
||||
#define WRITE_D(s) write(1, s, string_length(s));
|
||||
|
||||
#define PROMPT_LENGTH 255
|
||||
|
||||
static const char bs[PROMPT_LENGTH + 1];
|
||||
static const char ws[PROMPT_LENGTH + 1];
|
||||
|
||||
static char prompt[PROMPT_LENGTH + 1];
|
||||
static uint8_t prompt_length = 0;
|
||||
static uint8_t prompt_offset = 0;
|
||||
|
||||
typedef void (*app_t)(uint8_t argc, char **argv);
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
app_t app;
|
||||
} app_entry_t;
|
||||
|
||||
static app_entry_t builtins[] = {
|
||||
{"help", help},
|
||||
{"cd", cd},
|
||||
};
|
||||
|
||||
static void help(uint8_t argc, __attribute__((unused)) char **argv) {
|
||||
if (argc) {
|
||||
WRITE_S("help: expects no arguments");
|
||||
return;
|
||||
}
|
||||
|
||||
WRITE_S("Available commands:\n");
|
||||
for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) {
|
||||
WRITE_D(builtins[i].name);
|
||||
WRITE_S("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void cd(uint8_t argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
WRITE_S("cd: requires a single path\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (chdir(argv[1]) == (uint64_t)-1) {
|
||||
WRITE_S("cd: path does not exist\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void print_prompt() {
|
||||
char path[PROMPT_LENGTH];
|
||||
uint8_t pl = getcwd(PROMPT_LENGTH, path);
|
||||
char *components[16];
|
||||
uint8_t cl = string_split(path, '/', 16, components);
|
||||
|
||||
WRITE_S("[")
|
||||
WRITE_D(pl == 1 ? "/" : components[cl - 1]);
|
||||
WRITE_S("]$ ");
|
||||
}
|
||||
|
||||
static void on_home_pressed() {
|
||||
if (!prompt_offset) {
|
||||
return;
|
||||
}
|
||||
|
||||
write(1, bs, prompt_offset);
|
||||
prompt_offset = 0;
|
||||
}
|
||||
|
||||
static void on_left_pressed() {
|
||||
if (!prompt_offset) {
|
||||
return;
|
||||
}
|
||||
|
||||
write(1, bs, 1);
|
||||
prompt_offset--;
|
||||
}
|
||||
|
||||
static void on_right_pressed() {
|
||||
if (prompt_offset == prompt_length) {
|
||||
return;
|
||||
}
|
||||
|
||||
write(1, prompt + prompt_offset, 1);
|
||||
prompt_offset++;
|
||||
}
|
||||
|
||||
static void on_end_pressed() {
|
||||
if (prompt_offset == prompt_length) {
|
||||
return;
|
||||
}
|
||||
|
||||
write(1, prompt + prompt_offset, prompt_length - prompt_offset);
|
||||
prompt_offset = prompt_length;
|
||||
}
|
||||
|
||||
static void on_enter_pressed() {
|
||||
prompt[prompt_length] = '\0';
|
||||
|
||||
char *argv[16];
|
||||
uint8_t argc = (uint8_t)string_split(prompt, ' ', 16, argv);
|
||||
|
||||
WRITE_S("\n");
|
||||
|
||||
uint8_t i;
|
||||
for (i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) {
|
||||
if (string_equal(argv[0], builtins[i].name)) {
|
||||
builtins[i].app(argc, argv);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == sizeof(builtins) / sizeof(app_entry_t)) {
|
||||
if (spawn(argv[0], argc, (const char **)argv) == (uint64_t)-1){
|
||||
WRITE_S("terminal: program not found\n");
|
||||
}
|
||||
}
|
||||
prompt_length = prompt_offset = 0;
|
||||
print_prompt();
|
||||
}
|
||||
|
||||
static void on_cancel_pressed() {
|
||||
WRITE_S("^C\n");
|
||||
prompt_length = prompt_offset = 0;
|
||||
print_prompt();
|
||||
}
|
||||
|
||||
static void on_disconnect_pressed() {
|
||||
WRITE_S("^D\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
static void on_ctrl_character_pressed(char c) {
|
||||
if (c == 'c') {
|
||||
on_cancel_pressed();
|
||||
} else if (c == 'd') {
|
||||
on_disconnect_pressed();
|
||||
}
|
||||
}
|
||||
|
||||
static void redraw_from_cursor() {
|
||||
uint64_t tail = prompt_length - prompt_offset;
|
||||
write(1, prompt + prompt_offset, tail);
|
||||
write(1, ws, 1); // erase the character past the end
|
||||
write(1, bs, tail + 1); // move back to cursor position
|
||||
}
|
||||
|
||||
static void on_character_pressed(char c) {
|
||||
if (prompt_offset >= PROMPT_LENGTH)
|
||||
return;
|
||||
|
||||
if (prompt_offset != prompt_length) {
|
||||
memory_move(prompt + prompt_offset, prompt_length - prompt_offset, prompt + prompt_offset + 1);
|
||||
}
|
||||
prompt[prompt_offset] = c;
|
||||
prompt_length++;
|
||||
prompt_offset++;
|
||||
|
||||
write(1, &c, 1); // emit the character itself
|
||||
redraw_from_cursor();
|
||||
}
|
||||
|
||||
static void on_backspace_pressed() {
|
||||
if (!prompt_offset)
|
||||
return;
|
||||
|
||||
if (prompt_offset != prompt_length) {
|
||||
memory_move(prompt + prompt_offset, prompt_length - prompt_offset, prompt + prompt_offset - 1);
|
||||
}
|
||||
prompt_offset--;
|
||||
prompt_length--;
|
||||
|
||||
write(1, bs, 1);
|
||||
redraw_from_cursor();
|
||||
}
|
||||
|
||||
static void on_delete_pressed() {
|
||||
if (prompt_offset == prompt_length)
|
||||
return;
|
||||
|
||||
memory_move(prompt + prompt_offset + 1, prompt_length - prompt_offset - 1, prompt + prompt_offset);
|
||||
prompt_length--;
|
||||
|
||||
redraw_from_cursor();
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
PARSE_NORMAL,
|
||||
PARSE_ESC,
|
||||
PARSE_CSI,
|
||||
} parse_state_t;
|
||||
|
||||
static parse_state_t parser_state = PARSE_NORMAL;
|
||||
static char parser_csi_param[8];
|
||||
static uint8_t parser_csi_len;
|
||||
|
||||
static void on_char_received(char c) {
|
||||
switch (parser_state) {
|
||||
case PARSE_NORMAL:
|
||||
if (c == '\x1B') {
|
||||
parser_state = PARSE_ESC;
|
||||
} else if (c == '\b' || c == '\x7F') {
|
||||
on_backspace_pressed();
|
||||
} else if (c == '\n' || c == '\r') {
|
||||
on_enter_pressed();
|
||||
} else if (c >= '\x01' && c <= '\x1A') {
|
||||
on_ctrl_character_pressed(c + 'a' - 1);
|
||||
} else if (c >= ' ') {
|
||||
on_character_pressed(c);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARSE_ESC:
|
||||
if (c == '[') {
|
||||
parser_state = PARSE_CSI;
|
||||
parser_csi_len = 0;
|
||||
} else {
|
||||
parser_state = PARSE_NORMAL;
|
||||
}
|
||||
break;
|
||||
|
||||
case PARSE_CSI:
|
||||
if ((c >= '0' && c <= '9') || c == ';') {
|
||||
if (parser_csi_len < sizeof(parser_csi_param) - 1) {
|
||||
parser_csi_param[parser_csi_len++] = c;
|
||||
}
|
||||
} else {
|
||||
parser_state = PARSE_NORMAL;
|
||||
parser_csi_param[parser_csi_len] = '\0';
|
||||
|
||||
switch (c) {
|
||||
case 'C':
|
||||
on_right_pressed();
|
||||
break;
|
||||
case 'D':
|
||||
on_left_pressed();
|
||||
break;
|
||||
case 'H':
|
||||
on_home_pressed();
|
||||
break;
|
||||
case 'F':
|
||||
on_end_pressed();
|
||||
break;
|
||||
case '~':
|
||||
if (parser_csi_param[0] == '3') {
|
||||
on_delete_pressed();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
memory_set('\b', PROMPT_LENGTH, (char *)bs);
|
||||
memory_set(' ', PROMPT_LENGTH, (char *)ws);
|
||||
|
||||
WRITE_D("Welcome to FreywarOS v" VERSION "!\n\n");
|
||||
|
||||
print_prompt();
|
||||
|
||||
while (1) {
|
||||
char c;
|
||||
if (read(0, 1, &c)) {
|
||||
on_char_received(c);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user