Separate log and output streams

This commit is contained in:
2026-07-17 20:30:35 +03:00
parent 85cfbb2050
commit 14c3262c14
14 changed files with 101 additions and 106 deletions
+27 -22
View File
@@ -3,15 +3,15 @@
#include "src/lib/util.h"
#include "src/user/syscall.h"
static void help(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout);
static void cd(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout);
static exit_code_t help(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout);
static exit_code_t cd(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout);
#define PATH "/bin/"
#define PRINT_S(o, s) write(o, s, sizeof(s) - 1);
#define PRINT_D(o, s) write(o, s, string_length(s));
#define WRITE_S(o, s) write(o, s, sizeof(s) - 1);
#define WRITE_D(o, s) write(o, s, string_length(s));
typedef void (*app_t)(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout);
typedef exit_code_t (*app_t)(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout);
typedef struct {
const char *name;
@@ -23,28 +23,33 @@ static app_entry_t builtins[] = {
{"cd", cd},
};
static void help(uint8_t argc, __attribute__((unused)) char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) {
static exit_code_t help(uint8_t argc, __attribute__((unused)) char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) {
if (argc > 1) {
PRINT_S(stdout, "help: expects no arguments\n");
return;
ERR_S("help: expects no arguments\n");
return EXIT_CODE_GENERAL_FAILURE;
}
PRINT_S(stdout, "Available commands:\n");
WRITE_S(stdout, "Available commands:\n");
for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) {
PRINT_D(stdout, builtins[i].name);
PRINT_S(stdout, "\n");
WRITE_D(stdout, builtins[i].name);
WRITE_S(stdout, "\n");
}
return EXIT_CODE_OK;
}
static void cd(uint8_t argc, char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) {
static exit_code_t cd(uint8_t argc, char **argv, __attribute__((unused)) uint64_t stdin, __attribute__((unused)) uint64_t stdout) {
if (argc != 2) {
PRINT_S(stdout, "cd: requires a single path\n");
return;
ERR_S("cd: requires a single path\n");
return EXIT_CODE_GENERAL_FAILURE;
}
if (chdir(argv[1]) == (uint64_t)-1) {
PRINT_S(stdout, "cd: path does not exist\n");
ERR_S("cd: path does not exist\n");
return EXIT_CODE_GENERAL_FAILURE;
}
return EXIT_CODE_OK;
}
static void print_prompt() {
@@ -53,9 +58,9 @@ static void print_prompt() {
char *components[16];
uint8_t cl = string_split(path, '/', 16, components);
PRINT_S(1, "[")
PRINT_D(1, pl == 1 ? "/" : components[cl - 1]);
PRINT_S(1, "]$ ");
OUT_S("[")
OUT_D(pl == 1 ? "/" : components[cl - 1]);
OUT_S("]$ ");
}
static void free_fds(uint64_t *fds, uint8_t fds_count) {
@@ -69,7 +74,7 @@ static void kill_pids(__attribute__((unused)) uint64_t *pids, __attribute__((unu
}
static void execute(char *command) {
PRINT_S(1, "\n");
OUT_S("\n");
if (string_equal(command, "^C")) {
print_prompt();
@@ -155,8 +160,8 @@ static void execute(char *command) {
print_prompt();
}
uint64_t main() {
PRINT_D(1, "Welcome to FreywarOS v" VERSION "!\n\n");
exit_code_t main() {
ERR_S("Welcome to FreywarOS v" VERSION "!\n\n");
print_prompt();
@@ -178,5 +183,5 @@ uint64_t main() {
}
}
return 0;
return EXIT_CODE_OK;
}