Use streams for inter-"process" communications
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
#include "src/app/cat.h"
|
||||
#include "src/fs.h"
|
||||
#include "src/memory.h"
|
||||
#include "src/string.h"
|
||||
#include "src/util.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
static stream_t *stream_in;
|
||||
static stream_t *stream_out;
|
||||
|
||||
#define WRITE_S(s) stream_out->write(stream_out, s, sizeof(s) - 1);
|
||||
#define WRITE_D(s) stream_out->write(stream_out, s, string_length(s));
|
||||
|
||||
void cat(stream_t *stdin, stream_t *stdout, uint8_t argc, char **argv) {
|
||||
stream_in = stdin;
|
||||
stream_out = stdout;
|
||||
|
||||
if (argc != 2) {
|
||||
WRITE_S("cat: requires a single path\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *path_components[16];
|
||||
uint64_t path_length = string_split(argv[1], '/', 16, path_components);
|
||||
if (!string_empty(path_components[0])) {
|
||||
WRITE_S("cat: relative paths not supported\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fs_node_t *root = fs_mount();
|
||||
|
||||
fs_node_t *prev = NUL;
|
||||
fs_node_t *curr = root;
|
||||
uint8_t i = 1;
|
||||
while (i < path_length) {
|
||||
if (string_empty(path_components[i])) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!curr->is_dir) {
|
||||
WRITE_S("cat: can not navigate into a file\n");
|
||||
fs_close(curr);
|
||||
return;
|
||||
}
|
||||
|
||||
prev = curr;
|
||||
curr = fs_open_by(curr, path_components[i]);
|
||||
if (prev != root) {
|
||||
fs_close(prev);
|
||||
}
|
||||
|
||||
if (!curr) {
|
||||
WRITE_S("cat: invalid path\n");
|
||||
return;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (curr->is_dir) {
|
||||
WRITE_S("cat: can not print a directory\n");
|
||||
if (curr != root) {
|
||||
fs_close(curr);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
char *content = memory_allocate(curr->size + 1);
|
||||
fs_read(curr, 0, curr->size, content);
|
||||
stream_out->write(stream_out, content, curr->size);
|
||||
memory_free(content);
|
||||
fs_close(curr);
|
||||
|
||||
fs_unmount(root);
|
||||
|
||||
stream_in = stream_out = NUL;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "src/stream.h"
|
||||
|
||||
void cat(stream_t *stdin, stream_t *stdout, uint8_t argc, char **argv);
|
||||
@@ -0,0 +1,81 @@
|
||||
#include "src/app/ls.h"
|
||||
#include "src/fs.h"
|
||||
#include "src/string.h"
|
||||
#include "src/util.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
static stream_t *stream_in;
|
||||
static stream_t *stream_out;
|
||||
|
||||
#define WRITE_S(s) stream_out->write(stream_out, s, sizeof(s) - 1);
|
||||
#define WRITE_D(s) stream_out->write(stream_out, s, string_length(s));
|
||||
|
||||
void ls(stream_t *stdin, stream_t *stdout, uint8_t argc, char **argv) {
|
||||
stream_in = stdin;
|
||||
stream_out = stdout;
|
||||
|
||||
if (argc != 2) {
|
||||
WRITE_S("ls: requires a single path\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *path_components[16];
|
||||
uint64_t path_length = string_split(argv[1], '/', 16, path_components);
|
||||
if (!string_empty(path_components[0])) {
|
||||
WRITE_S("ls: relative paths not supported\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fs_node_t *root = fs_mount();
|
||||
|
||||
fs_node_t *prev = NUL;
|
||||
fs_node_t *curr = root;
|
||||
uint8_t i = 1;
|
||||
while (i < path_length) {
|
||||
if (string_empty(path_components[i])) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!curr->is_dir) {
|
||||
WRITE_S("ls: can not navigate into a file\n");
|
||||
fs_close(curr);
|
||||
return;
|
||||
}
|
||||
|
||||
prev = curr;
|
||||
curr = fs_open_by(curr, path_components[i]);
|
||||
if (prev != root) {
|
||||
fs_close(prev);
|
||||
}
|
||||
|
||||
if (!curr) {
|
||||
WRITE_S("ls: invalid path\n");
|
||||
return;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (!curr->is_dir) {
|
||||
WRITE_S("ls: can not list file\n");
|
||||
fs_close(curr);
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t j = 0;
|
||||
fs_node_t *item = fs_open_at(curr, j++);
|
||||
while (item) {
|
||||
WRITE_D(item->name);
|
||||
WRITE_S("\n");
|
||||
fs_close(item);
|
||||
item = fs_open_at(curr, j++);
|
||||
}
|
||||
if (curr != root) {
|
||||
fs_close(curr);
|
||||
}
|
||||
|
||||
fs_unmount(root);
|
||||
|
||||
stream_in = stream_out = NUL;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "src/stream.h"
|
||||
|
||||
void ls(stream_t *stdin, stream_t *stdout, uint8_t argc, char **argv);
|
||||
@@ -0,0 +1,255 @@
|
||||
#include "src/app/terminal.h"
|
||||
#include "src/app/cat.h"
|
||||
#include "src/app/ls.h"
|
||||
#include "src/fs.h"
|
||||
#include "src/memory.h"
|
||||
#include "src/string.h"
|
||||
#include "src/util.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define PROMPT_LENGTH 255
|
||||
|
||||
fs_node_t *root;
|
||||
|
||||
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;
|
||||
|
||||
static stream_t *stream_in;
|
||||
static stream_t *stream_out;
|
||||
|
||||
static uint8_t active = 1;
|
||||
|
||||
#define WRITE_S(s) stream_out->write(stream_out, s, sizeof(s) - 1);
|
||||
#define WRITE_D(s) stream_out->write(stream_out, s, string_length(s));
|
||||
|
||||
static void on_home_pressed() {
|
||||
if (!prompt_offset) {
|
||||
return;
|
||||
}
|
||||
|
||||
stream_out->write(stream_out, bs, prompt_offset);
|
||||
prompt_offset = 0;
|
||||
}
|
||||
|
||||
static void on_left_pressed() {
|
||||
if (!prompt_offset) {
|
||||
return;
|
||||
}
|
||||
|
||||
stream_out->write(stream_out, bs, 1);
|
||||
prompt_offset--;
|
||||
}
|
||||
|
||||
static void on_right_pressed() {
|
||||
if (prompt_offset == prompt_length) {
|
||||
return;
|
||||
}
|
||||
|
||||
stream_out->write(stream_out, prompt + prompt_offset, 1);
|
||||
prompt_offset++;
|
||||
}
|
||||
|
||||
static void on_end_pressed() {
|
||||
if (prompt_offset == prompt_length) {
|
||||
return;
|
||||
}
|
||||
|
||||
stream_out->write(stream_out, prompt + prompt_offset, prompt_length - prompt_offset);
|
||||
prompt_offset = prompt_length;
|
||||
}
|
||||
|
||||
static void help(uint8_t argc, __attribute__((unused)) char **argv) {
|
||||
if (argc > 1) {
|
||||
WRITE_S("help: accepts no arguments\n");
|
||||
}
|
||||
|
||||
WRITE_S("Available commands:\n"
|
||||
"help\n"
|
||||
"ls DIR\n"
|
||||
"cat FILE\n")
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
if (string_equal(argv[0], "help")) {
|
||||
help(argc, argv);
|
||||
} else if (string_equal(argv[0], "ls")) {
|
||||
ls(stream_in, stream_out, argc, argv);
|
||||
} else if (string_equal(argv[0], "cat")) {
|
||||
cat(stream_in, stream_out, argc, argv);
|
||||
} else {
|
||||
WRITE_S("Unknown command\n");
|
||||
}
|
||||
WRITE_S("$ ");
|
||||
prompt_length = prompt_offset = 0;
|
||||
}
|
||||
|
||||
static void on_cancel_pressed() {
|
||||
WRITE_S("^C\n");
|
||||
WRITE_S("$ ");
|
||||
prompt_length = prompt_offset = 0;
|
||||
}
|
||||
|
||||
static void on_disconnect_pressed() {
|
||||
WRITE_S("^D\n");
|
||||
active = 0;
|
||||
}
|
||||
|
||||
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;
|
||||
stream_out->write(stream_out, prompt + prompt_offset, tail);
|
||||
stream_out->write(stream_out, ws, 1); // erase the character past the end
|
||||
stream_out->write(stream_out, 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++;
|
||||
|
||||
stream_out->write(stream_out, &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--;
|
||||
|
||||
stream_out->write(stream_out, 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;
|
||||
}
|
||||
}
|
||||
|
||||
void terminal(stream_t *stdin, stream_t *stdout, __attribute__((unused)) uint8_t argc, __attribute__((unused)) char **argv) {
|
||||
memory_set('\b', PROMPT_LENGTH, (char *)bs);
|
||||
memory_set(' ', PROMPT_LENGTH, (char *)ws);
|
||||
|
||||
stream_in = stdin;
|
||||
stream_out = stdout;
|
||||
|
||||
root = fs_mount();
|
||||
|
||||
char *greeting = string_format("Welcome to FreywarOS v%s!\n\n", VERSION);
|
||||
WRITE_D(greeting);
|
||||
memory_free(greeting);
|
||||
|
||||
WRITE_S("$ ");
|
||||
|
||||
while (active) {
|
||||
char c;
|
||||
if (stream_in->read(stream_in, 1, &c)) {
|
||||
on_char_received(c);
|
||||
}
|
||||
}
|
||||
|
||||
// fs_unmount(root);
|
||||
stream_in = stream_out = NUL;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "src/stream.h"
|
||||
|
||||
void terminal(stream_t *stdin, stream_t *stdout, uint8_t argc, char **argv);
|
||||
Reference in New Issue
Block a user