Separate log and output streams
This commit is contained in:
@@ -4,18 +4,15 @@
|
||||
|
||||
#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;
|
||||
while ((bytes = read(fd, BLOCK_SIZE, buffer))) {
|
||||
if (bytes == (uint64_t)-1) {
|
||||
PRINT_S("cat: could not read file\n");
|
||||
ERR_S("cat: could not read file\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
write(1, buffer, bytes);
|
||||
write(STDOUT, buffer, bytes);
|
||||
}
|
||||
|
||||
return EXIT_CODE_OK;
|
||||
@@ -25,7 +22,7 @@ exit_code_t cat(const char *path) {
|
||||
uint64_t fd = open(path, OPEN_FILE);
|
||||
|
||||
if (fd == (uint64_t)-1) {
|
||||
PRINT_S("cat: path does not exist\n");
|
||||
ERR_S("cat: path does not exist\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,29 +4,27 @@
|
||||
|
||||
#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 main(uint64_t argc, const char **argv) {
|
||||
if (argc != 3) {
|
||||
PRINT_S("cp: requires source and target");
|
||||
ERR_S("cp: requires source and target\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
uint64_t source = open(argv[1], OPEN_FILE);
|
||||
if (source == (uint64_t)-1) {
|
||||
PRINT_S("cp: source does not exist\n");
|
||||
ERR_S("cp: source does not exist\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
uint64_t target = open(argv[2], OPEN_FILE | OPEN_CREATE);
|
||||
if (target == (uint64_t)-1) {
|
||||
PRINT_S("cp: target path does not exist\n");
|
||||
ERR_S("cp: target path does not exist\n");
|
||||
close(source);
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
if (truncate(target, 0) == (uint64_t)-1) {
|
||||
ERR_S("cp: could not write file\n");
|
||||
close(source);
|
||||
close(target);
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
@@ -36,13 +34,13 @@ exit_code_t main(uint64_t argc, const char **argv) {
|
||||
uint64_t bytes;
|
||||
while ((bytes = read(source, BLOCK_SIZE, buffer))) {
|
||||
if (bytes == (uint64_t)-1) {
|
||||
PRINT_S("cp: could not read file\n");
|
||||
ERR_S("cp: could not read file\n");
|
||||
close(source);
|
||||
close(target);
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
if (write(target, buffer, bytes) == (uint64_t)-1) {
|
||||
PRINT_S("cp: could not write file\n");
|
||||
ERR_S("cp: could not write file\n");
|
||||
close(source);
|
||||
close(target);
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#include "src/lib/string.h"
|
||||
#include "src/lib/util.h"
|
||||
#include "src/user/syscall.h"
|
||||
|
||||
exit_code_t main(uint64_t argc, const char **argv) {
|
||||
for (uint64_t i = 1; i < argc; i++) {
|
||||
if (i > 1) {
|
||||
write(1, " ", 1);
|
||||
OUT_S(" ");
|
||||
}
|
||||
write(1, argv[i], string_length(argv[i]));
|
||||
OUT_D(argv[i]);
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
OUT_S("\n");
|
||||
|
||||
return EXIT_CODE_OK;
|
||||
}
|
||||
|
||||
+10
-12
@@ -1,29 +1,27 @@
|
||||
#include "src/lib/string.h"
|
||||
#include "src/lib/syscall.h"
|
||||
#include "src/lib/util.h"
|
||||
#include "src/user/syscall.h"
|
||||
|
||||
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
|
||||
#define PRINT_D(s) write(1, s, string_length(s));
|
||||
#define BLOCK_SIZE 256
|
||||
|
||||
exit_code_t ls(const char *path) {
|
||||
uint64_t fd = open(path, OPEN_DIRECTORY);
|
||||
|
||||
if (fd == (uint64_t)-1) {
|
||||
PRINT_S("ls: path does not exist\n");
|
||||
ERR_S("ls: path does not exist\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
char buffer[100];
|
||||
char buffer[BLOCK_SIZE];
|
||||
uint64_t bytes;
|
||||
while ((bytes = read(fd, 100, buffer))) {
|
||||
while ((bytes = read(fd, BLOCK_SIZE, buffer))) {
|
||||
if (bytes == (uint64_t)-1) {
|
||||
PRINT_S("ls: could not read directory\n");
|
||||
ERR_S("ls: could not read directory\n");
|
||||
close(fd);
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
write(1, buffer, bytes - 1);
|
||||
PRINT_S("\n");
|
||||
write(STDOUT, buffer, bytes);
|
||||
OUT_S("\n");
|
||||
}
|
||||
|
||||
close(fd);
|
||||
@@ -39,9 +37,9 @@ exit_code_t main(uint64_t argc, const char **argv) {
|
||||
exit_code_t code = EXIT_CODE_OK;
|
||||
for (uint64_t i = 1; i < argc; i++) {
|
||||
if (argc > 2) {
|
||||
PRINT_S("\n");
|
||||
PRINT_D(argv[i]);
|
||||
PRINT_S(":\n");
|
||||
OUT_S("\n");
|
||||
OUT_D(argv[i]);
|
||||
OUT_S(":\n");
|
||||
}
|
||||
exit_code_t c = ls(argv[i]);
|
||||
if (c != EXIT_CODE_OK) {
|
||||
|
||||
@@ -1,28 +1,23 @@
|
||||
#include "src/lib/syscall.h"
|
||||
#include "src/lib/util.h"
|
||||
#include "src/user/syscall.h"
|
||||
#include <stdint.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 main(uint64_t argc, const char **argv) {
|
||||
if (argc < 2) {
|
||||
PRINT_S("mkdir: requires target(s)\n");
|
||||
ERR_S("mkdir: requires target(s)\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
exit_code_t code = EXIT_CODE_OK;
|
||||
for (uint64_t i = 1; i < argc; i++) {
|
||||
uint64_t fd = open(argv[i], OPEN_DIRECTORY | OPEN_CREATE | OPEN_EXCLUSIVE);
|
||||
if (fd == (uint64_t)-1) {
|
||||
PRINT_S("mkdir: could not create directory\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
ERR_S("mkdir: could not create directory\n");
|
||||
code = EXIT_CODE_GENERAL_FAILURE;
|
||||
} else {
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
return EXIT_CODE_OK;
|
||||
return code;
|
||||
}
|
||||
|
||||
+5
-10
@@ -2,23 +2,18 @@
|
||||
#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 main(uint64_t argc, const char **argv) {
|
||||
if (argc < 2) {
|
||||
PRINT_S("rm: requires one or more arguments\n");
|
||||
ERR_S("rm: requires one or more arguments\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
uint64_t code = EXIT_CODE_OK;
|
||||
for (uint64_t i = 1; i < argc; i++) {
|
||||
exit_code_t c = remove(argv[i]);
|
||||
if (c != EXIT_CODE_OK) {
|
||||
PRINT_S("rm: coult not remove file\n");
|
||||
code = c;
|
||||
uint64_t removed = remove(argv[i]);
|
||||
if (removed == (uint64_t)-1) {
|
||||
ERR_S("rm: could not remove file\n");
|
||||
code = EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+27
-22
@@ -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;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
#include "src/lib/util.h"
|
||||
#include "src/user/syscall.h"
|
||||
|
||||
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
|
||||
#define PRINT_D(s) write(1, s, string_length(s));
|
||||
|
||||
#define PROMPT_LENGTH 255
|
||||
|
||||
static const char bs[PROMPT_LENGTH + 1];
|
||||
@@ -19,7 +16,7 @@ static void on_home_pressed() {
|
||||
return;
|
||||
}
|
||||
|
||||
write(1, bs, prompt_offset);
|
||||
write(STDOUT, bs, prompt_offset);
|
||||
prompt_offset = 0;
|
||||
}
|
||||
|
||||
@@ -28,7 +25,7 @@ static void on_left_pressed() {
|
||||
return;
|
||||
}
|
||||
|
||||
write(1, bs, 1);
|
||||
write(STDOUT, bs, 1);
|
||||
prompt_offset--;
|
||||
}
|
||||
|
||||
@@ -37,7 +34,7 @@ static void on_right_pressed() {
|
||||
return;
|
||||
}
|
||||
|
||||
write(1, prompt + prompt_offset, 1);
|
||||
write(STDOUT, prompt + prompt_offset, 1);
|
||||
prompt_offset++;
|
||||
}
|
||||
|
||||
@@ -46,27 +43,27 @@ static void on_end_pressed() {
|
||||
return;
|
||||
}
|
||||
|
||||
write(1, prompt + prompt_offset, prompt_length - prompt_offset);
|
||||
write(STDOUT, prompt + prompt_offset, prompt_length - prompt_offset);
|
||||
prompt_offset = prompt_length;
|
||||
}
|
||||
|
||||
static void on_enter_pressed() {
|
||||
prompt[prompt_length] = '\n';
|
||||
|
||||
write(2, prompt, prompt_length + 1);
|
||||
write(3, prompt, prompt_length + 1);
|
||||
|
||||
prompt_length = prompt_offset = 0;
|
||||
}
|
||||
|
||||
static void on_cancel_pressed() {
|
||||
PRINT_S("^C\n");
|
||||
write(2, "^C\n", 3);
|
||||
OUT_S("^C\n");
|
||||
write(3, "^C\n", 3);
|
||||
prompt_length = prompt_offset = 0;
|
||||
}
|
||||
|
||||
static void on_disconnect_pressed() {
|
||||
PRINT_S("^D\n");
|
||||
write(2, "^D\n", 3);
|
||||
OUT_S("^D\n");
|
||||
write(3, "^D\n", 3);
|
||||
exit(EXIT_CODE_OK);
|
||||
}
|
||||
|
||||
@@ -80,9 +77,9 @@ static void on_ctrl_character_pressed(char c) {
|
||||
|
||||
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
|
||||
write(STDOUT, prompt + prompt_offset, tail);
|
||||
write(STDOUT, ws, 1); // erase the character past the end
|
||||
write(STDOUT, bs, tail + 1); // move back to cursor position
|
||||
}
|
||||
|
||||
static void on_character_pressed(char c) {
|
||||
@@ -96,7 +93,7 @@ static void on_character_pressed(char c) {
|
||||
prompt_length++;
|
||||
prompt_offset++;
|
||||
|
||||
write(1, &c, 1); // emit the character itself
|
||||
write(STDOUT, &c, 1); // emit the character itself
|
||||
redraw_from_cursor();
|
||||
}
|
||||
|
||||
@@ -110,7 +107,7 @@ static void on_backspace_pressed() {
|
||||
prompt_offset--;
|
||||
prompt_length--;
|
||||
|
||||
write(1, bs, 1);
|
||||
write(STDOUT, bs, 1);
|
||||
redraw_from_cursor();
|
||||
}
|
||||
|
||||
@@ -197,7 +194,7 @@ uint64_t main() {
|
||||
memory_set(' ', PROMPT_LENGTH, (char *)ws);
|
||||
|
||||
char c;
|
||||
while (read(0, 1, &c)) {
|
||||
while (read(STDIN, 1, &c)) {
|
||||
on_char_received(c);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,16 +5,13 @@
|
||||
|
||||
#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");
|
||||
ERR_S("wc: could not read file\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
} else {
|
||||
total += bytes;
|
||||
@@ -22,7 +19,7 @@ exit_code_t pass(uint64_t fd) {
|
||||
}
|
||||
|
||||
uint64_t l = string_format("%d\n", BLOCK_SIZE, buffer, total);
|
||||
write(1, buffer, l);
|
||||
write(STDOUT, buffer, l);
|
||||
|
||||
return EXIT_CODE_OK;
|
||||
}
|
||||
@@ -31,7 +28,7 @@ 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");
|
||||
OUT_S("wc: path does not exist\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user