2fa39ad85a
Self explanatory, we finally have a working ish shell Now we need to do some more stuff or something, probably work on a fork or something Signed-off-by: kaguya <vpshinomiya@protonmail.com> Co-authored-by: Copilot <copilot@github.com>
129 lines
3.2 KiB
C
129 lines
3.2 KiB
C
#include "../include/syscalls.h"
|
|
|
|
#define STDIN 0
|
|
#define STDOUT 1
|
|
|
|
unsigned strlen(const char* str)
|
|
{
|
|
unsigned len = 0;
|
|
while (*str)
|
|
{
|
|
++len;
|
|
++str;
|
|
}
|
|
return len;
|
|
}
|
|
|
|
static int strcmp(const char* a, const char* b)
|
|
{
|
|
while (*a && (*a == *b)) {
|
|
a++;
|
|
b++;
|
|
}
|
|
return *(unsigned char*)a - *(unsigned char*)b;
|
|
}
|
|
|
|
static void print(const char* s)
|
|
{
|
|
syscall(SYS_WRITE, STDOUT, (unsigned long)s, strlen(s), 0, 0, 0);
|
|
}
|
|
|
|
static void readline(char* buf, unsigned long max)
|
|
{
|
|
unsigned long i = 0;
|
|
|
|
while (i < max - 1)
|
|
{
|
|
char c;
|
|
long n = syscall(SYS_READ, STDIN, (unsigned long)&c, 1, 0, 0, 0);
|
|
|
|
if (n <= 0)
|
|
continue;
|
|
|
|
if (c == '\n')
|
|
break;
|
|
|
|
if (c == '\b') {
|
|
if (i > 0) i--;
|
|
continue;
|
|
}
|
|
|
|
buf[i++] = c;
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
}
|
|
|
|
static void cat(const char* path)
|
|
{
|
|
unsigned char buf[256];
|
|
|
|
long fd = syscall(SYS_OPEN, (unsigned long)path, 0, 0, 0, 0, 0);
|
|
if (fd < 0) {
|
|
print("cat: cannot open file\n");
|
|
return;
|
|
}
|
|
|
|
long n = syscall(SYS_READ, fd, (unsigned long)buf, sizeof(buf), 0, 0, 0);
|
|
syscall(SYS_CLOSE, fd, 0, 0, 0, 0, 0);
|
|
|
|
if (n > 0)
|
|
syscall(SYS_WRITE, STDOUT, (unsigned long)buf, n, 0, 0, 0);
|
|
}
|
|
|
|
|
|
void main()
|
|
{
|
|
const unsigned char* path = "/qwerty.txt";
|
|
const unsigned char* msg = "Suki Suki Daisuki Kekkon Shiyo, my honey!";
|
|
|
|
unsigned char buf[128];
|
|
|
|
// ── open file ─────────────────────────────
|
|
unsigned long fd = syscall(SYS_OPEN, (long)path, 0, 0, 0, 0, 0);
|
|
|
|
// ── write message ─────────────────────────
|
|
syscall(SYS_WRITE, fd, (long)msg, strlen(msg), 0, 0, 0);
|
|
|
|
// ── close ────────────────────────────────
|
|
syscall(SYS_CLOSE, fd, 0, 0, 0, 0, 0);
|
|
|
|
// ── reopen ───────────────────────────────
|
|
fd = syscall(SYS_OPEN, (long)path, 0, 0, 0, 0, 0);
|
|
|
|
// ── read into buffer ─────────────────────
|
|
unsigned long n = syscall(SYS_READ, fd, (unsigned long)buf, sizeof(buf), 0, 0, 0);
|
|
|
|
// ── close ────────────────────────────────
|
|
syscall(SYS_CLOSE, fd, 0, 0, 0, 0, 0);
|
|
|
|
// ── print buffer to stdout ───────────────
|
|
syscall(SYS_WRITE, STDOUT, (unsigned long)buf, n, 0, 0, 0);
|
|
|
|
char line[128];
|
|
|
|
while (1)
|
|
{
|
|
print("> ");
|
|
|
|
readline(line, sizeof(line));
|
|
|
|
if (strcmp(line, "exit") == 0)
|
|
break;
|
|
|
|
// simple "cat <path>"
|
|
if (line[0] == 'c' && line[1] == 'a' && line[2] == 't' && line[3] == ' ')
|
|
{
|
|
cat(&line[4]);
|
|
continue;
|
|
}
|
|
|
|
if (line[0] != '\0')
|
|
print("Unknown command\n");
|
|
}
|
|
|
|
while (1);
|
|
|
|
// ── done ────────────────────────────────
|
|
while (1);
|
|
} |