Add pipes to shell, add wc

This commit is contained in:
2026-07-16 21:39:00 +03:00
parent 4ad32dda17
commit 85cfbb2050
17 changed files with 293 additions and 63 deletions
+17 -12
View File
@@ -7,6 +7,20 @@
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
#define PRINT_D(s) write(1, s, string_length(s));
exit_code_t pass(uint64_t fd) {
static char buffer[BLOCK_SIZE];
uint64_t bytes;
while ((bytes = read(fd, BLOCK_SIZE, buffer))) {
if (bytes == (uint64_t)-1) {
PRINT_S("cat: could not read file\n");
return EXIT_CODE_GENERAL_FAILURE;
}
write(1, buffer, bytes);
}
return EXIT_CODE_OK;
}
exit_code_t cat(const char *path) {
uint64_t fd = open(path, OPEN_FILE);
@@ -15,25 +29,16 @@ exit_code_t cat(const char *path) {
return EXIT_CODE_GENERAL_FAILURE;
}
static char buffer[BLOCK_SIZE];
uint64_t bytes;
while ((bytes = read(fd, BLOCK_SIZE, buffer))) {
if (bytes == (uint64_t)-1) {
PRINT_S("cat: could not read file\n");
close(fd);
return EXIT_CODE_GENERAL_FAILURE;
}
write(1, buffer, bytes);
}
exit_code_t code = pass(fd);
close(fd);
return 0;
return code;
}
exit_code_t main(uint64_t argc, const char **argv) {
if (argc == 1) {
return cat(".");
return pass(0);
}
uint64_t code = EXIT_CODE_OK;
+97 -39
View File
@@ -3,15 +3,15 @@
#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);
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);
#define PATH "/bin/"
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
#define PRINT_D(s) write(1, s, string_length(s));
#define PRINT_S(o, s) write(o, s, sizeof(s) - 1);
#define PRINT_D(o, s) write(o, s, string_length(s));
typedef void (*app_t)(uint8_t argc, char **argv);
typedef void (*app_t)(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout);
typedef struct {
const char *name;
@@ -23,27 +23,27 @@ static app_entry_t builtins[] = {
{"cd", cd},
};
static void help(uint8_t argc, __attribute__((unused)) char **argv) {
static void help(uint8_t argc, __attribute__((unused)) char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) {
if (argc > 1) {
PRINT_S("help: expects no arguments");
PRINT_S(stdout, "help: expects no arguments\n");
return;
}
PRINT_S("Available commands:\n");
PRINT_S(stdout, "Available commands:\n");
for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) {
PRINT_D(builtins[i].name);
PRINT_S("\n");
PRINT_D(stdout, builtins[i].name);
PRINT_S(stdout, "\n");
}
}
static void cd(uint8_t argc, char **argv) {
static void cd(uint8_t argc, char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) {
if (argc != 2) {
PRINT_S("cd: requires a single path\n");
PRINT_S(stdout, "cd: requires a single path\n");
return;
}
if (chdir(argv[1]) == (uint64_t)-1) {
PRINT_S("cd: path does not exist\n");
PRINT_S(stdout, "cd: path does not exist\n");
}
}
@@ -53,16 +53,23 @@ static void print_prompt() {
char *components[16];
uint8_t cl = string_split(path, '/', 16, components);
PRINT_S("[")
PRINT_D(pl == 1 ? "/" : components[cl - 1]);
PRINT_S("]$ ");
PRINT_S(1, "[")
PRINT_D(1, pl == 1 ? "/" : components[cl - 1]);
PRINT_S(1, "]$ ");
}
static void free_fds(uint64_t *fds, uint8_t fds_count) {
for (uint8_t i = 0; i < fds_count; i++) {
close(fds[i]);
}
}
static void kill_pids(__attribute__((unused)) uint64_t *pids, __attribute__((unused)) uint8_t pid_count) {
// TODO.
}
static void execute(char *command) {
char *argv[16];
uint8_t argc = (uint8_t)string_split(command, ' ', 16, argv);
PRINT_S("\n");
PRINT_S(1, "\n");
if (string_equal(command, "^C")) {
print_prompt();
@@ -71,34 +78,85 @@ static void execute(char *command) {
if (string_equal(command, "^D")) {
exit(EXIT_CODE_OK);
return;
}
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);
char *subcommands[16];
uint8_t subcommands_count = (uint8_t)string_split(command, '|', 16, subcommands);
if (pid == EXIT_CODE_NOT_FOUND) {
PRINT_S("shell: program not found\n");
} else {
waitpid(pid);
uint64_t fds[32];
uint8_t fds_count = 0;
uint64_t pids[16];
uint8_t pids_count = 0;
for (uint8_t i = 0; i < subcommands_count; i++) {
string_trim(subcommands[i], ' ', &subcommands[i]);
char *argv[16];
uint8_t argc = (uint8_t)string_split(subcommands[i], ' ', 16, argv);
uint64_t stdin = 0;
uint64_t stdout = 1;
if (i > 0) {
stdin = fds[fds_count - 1];
}
if (i < subcommands_count - 1) {
if (pipe(&fds[fds_count], &fds[fds_count + 1]) == (uint64_t)-1) {
kill_pids(pids, pids_count);
free_fds(fds, fds_count);
print_prompt();
return;
}
fds_count += 2;
stdout = fds[fds_count - 2];
}
uint8_t j;
for (j = 0; j < sizeof(builtins) / sizeof(app_entry_t); j++) {
if (string_equal(argv[0], builtins[j].name)) {
builtins[j].app(argc, argv, stdin, stdout);
pids[pids_count++] = 0;
break;
}
}
if (j == 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, stdin, stdout);
memory_free(path);
if (pid == (uint64_t)-1) {
kill_pids(pids, pids_count);
free_fds(fds, fds_count);
print_prompt();
return;
} else {
pids[pids_count++] = pid;
}
}
}
for (uint8_t i = 0; i < pids_count; i++) {
if (pids[i]) {
waitpid(pids[i]);
}
if (i > 0) {
close(fds[(i - 1) * 2 + 1]);
}
if (i < pids_count - 1) {
close(fds[i * 2]);
}
}
print_prompt();
}
uint64_t main() {
PRINT_D("Welcome to FreywarOS v" VERSION "!\n\n");
PRINT_D(1, "Welcome to FreywarOS v" VERSION "!\n\n");
print_prompt();
+59
View File
@@ -0,0 +1,59 @@
#include "src/lib/string.h"
#include "src/lib/syscall.h"
#include "src/lib/util.h"
#include "src/user/syscall.h"
#define BLOCK_SIZE 65536
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
#define PRINT_D(s) write(1, s, string_length(s));
exit_code_t pass(uint64_t fd) {
static char buffer[BLOCK_SIZE];
uint64_t bytes;
uint64_t total = 0;
while ((bytes = read(fd, BLOCK_SIZE, buffer))) {
if (bytes == (uint64_t)-1) {
PRINT_S("wc: could not read file\n");
return EXIT_CODE_GENERAL_FAILURE;
} else {
total += bytes;
}
}
uint64_t l = string_format("%d\n", BLOCK_SIZE, buffer, total);
write(1, buffer, l);
return EXIT_CODE_OK;
}
exit_code_t wc(const char *path) {
uint64_t fd = open(path, OPEN_FILE);
if (fd == (uint64_t)-1) {
PRINT_S("wc: path does not exist\n");
return EXIT_CODE_GENERAL_FAILURE;
}
exit_code_t code = pass(fd);
close(fd);
return code;
}
exit_code_t main(uint64_t argc, const char **argv) {
if (argc == 1) {
return pass(0);
}
uint64_t code = EXIT_CODE_OK;
for (uint64_t i = 1; i < argc; i++) {
exit_code_t c = wc(argv[i]);
if (c != EXIT_CODE_OK) {
code = c;
}
}
return code;
}