Add process scheduler, pipes, and separate terminal from shell

This commit is contained in:
2026-06-19 21:49:29 +03:00
parent 452125782e
commit 415f78fd40
17 changed files with 524 additions and 120 deletions
+126
View File
@@ -0,0 +1,126 @@
#include "src/lib/memory.h"
#include "src/lib/string.h"
#include "src/lib/util.h"
#include "src/user/syscall.h"
static void help(uint8_t argc, char **argv);
static void cd(uint8_t argc, char **argv);
#define PATH "/bin/"
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
#define PRINT_D(s) write(1, s, string_length(s));
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 > 1) {
PRINT_S("help: expects no arguments");
return;
}
PRINT_S("Available commands:\n");
for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) {
PRINT_D(builtins[i].name);
PRINT_S("\n");
}
}
static void cd(uint8_t argc, char **argv) {
if (argc != 2) {
PRINT_S("cd: requires a single path\n");
return;
}
if (chdir(argv[1]) == (uint64_t)-1) {
PRINT_S("cd: path does not exist\n");
}
}
static void print_prompt() {
char path[256];
uint8_t pl = getcwd(256, path);
char *components[16];
uint8_t cl = string_split(path, '/', 16, components);
PRINT_S("[")
PRINT_D(pl == 1 ? "/" : components[cl - 1]);
PRINT_S("]$ ");
}
static void execute(char *command) {
char *argv[16];
uint8_t argc = (uint8_t)string_split(command, ' ', 16, argv);
PRINT_S("\n");
if (string_equal(command, "^C")) {
print_prompt();
return;
}
if (string_equal(command, "^D")) {
exit(EXIT_CODE_OK);
}
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)) {
uint64_t size = sizeof(PATH) + string_length(argv[0]) + 1;
char *path = memory_allocate(size);
memory_copy(PATH, sizeof(PATH), path);
memory_copy(argv[0], size - sizeof(PATH), path + sizeof(PATH) - 1);
uint64_t pid = spawn(path, argc, (const char **)argv);
memory_free(path);
if (pid == EXIT_CODE_NOT_FOUND) {
PRINT_S("shell: program not found\n");
} else {
waitpid(pid);
}
}
print_prompt();
}
uint64_t main() {
PRINT_D("Welcome to FreywarOS v" VERSION "!\n\n");
print_prompt();
char line[256];
uint64_t offset;
char chunk[64];
uint64_t received;
while (1) {
if ((received = read(0, sizeof(chunk), chunk))) {
for (uint64_t i = 0; i < received; i++) {
if (chunk[i] == '\n') {
line[offset] = '\0';
execute(line);
offset = 0;
} else if (offset < 255) {
line[offset++] = chunk[i];
}
}
}
}
return 0;
}
+4 -82
View File
@@ -1,13 +1,6 @@
#include "src/lib/memory.h"
#include "src/lib/string.h"
#include "src/lib/util.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 PATH "/bin/"
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
#define PRINT_D(s) write(1, s, string_length(s));
@@ -21,53 +14,6 @@ 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) {
PRINT_S("help: expects no arguments");
return;
}
PRINT_S("Available commands:\n");
for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) {
PRINT_D(builtins[i].name);
PRINT_S("\n");
}
}
static void cd(uint8_t argc, char **argv) {
if (argc != 2) {
PRINT_S("cd: requires a single path\n");
return;
}
if (chdir(argv[1]) == (uint64_t)-1) {
PRINT_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);
PRINT_S("[")
PRINT_D(pl == 1 ? "/" : components[cl - 1]);
PRINT_S("]$ ");
}
static void on_home_pressed() {
if (!prompt_offset) {
return;
@@ -105,42 +51,22 @@ static void on_end_pressed() {
}
static void on_enter_pressed() {
prompt[prompt_length] = '\0';
prompt[prompt_length] = '\n';
char *argv[16];
uint8_t argc = (uint8_t)string_split(prompt, ' ', 16, argv);
write(2, prompt, prompt_length + 1);
PRINT_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)) {
uint64_t size = sizeof(PATH) + string_length(argv[0]) + 1;
char *path = memory_allocate(size);
memory_copy(PATH, sizeof(PATH), path);
memory_copy(argv[0], size - sizeof(PATH), path + sizeof(PATH) - 1);
if (spawn(path, argc, (const char **)argv) == EXIT_CODE_NOT_FOUND) {
PRINT_S("terminal: program not found\n");
}
memory_free(path);
}
prompt_length = prompt_offset = 0;
print_prompt();
}
static void on_cancel_pressed() {
PRINT_S("^C\n");
write(2, "^C\n", 3);
prompt_length = prompt_offset = 0;
print_prompt();
}
static void on_disconnect_pressed() {
PRINT_S("^D\n");
write(2, "^D\n", 3);
exit(EXIT_CODE_OK);
}
@@ -270,10 +196,6 @@ uint64_t main() {
memory_set('\b', PROMPT_LENGTH, (char *)bs);
memory_set(' ', PROMPT_LENGTH, (char *)ws);
PRINT_D("Welcome to FreywarOS v" VERSION "!\n\n");
print_prompt();
while (1) {
char c;
if (read(0, 1, &c)) {