hazel/user/shell.c

110 lines
1.9 KiB
C

#include "../lib/strcmp.h"
static const char test[] = "Hello\n";
int strlen(const char *str) {
const char *s;
for (s = str; *s; s++)
;
return s - str;
}
void putc(const char c) {
asm volatile (
"movl $1, %%eax\n\t"
"movl $1, %%edi\n\t"
"movl %0, %%esi\n\t"
"movl $1, %%edx\n\t"
"int $0x80"
:
: "r"(&c)
: "%eax", "%edi", "%esi", "%edx"
);
}
void puts(const char *str) {
asm volatile (
"movl $1, %%eax\n\t"
"movl $1, %%edi\n\t"
"movl %0, %%esi\n\t"
"movl %1, %%edx\n\t"
"int $0x80"
:
: "r"(str), "r"(strlen(str))
: "%eax", "%edi", "%esi", "%edx"
);
}
void read(int fd, void *buf, int size) {
asm volatile (
"movl $0, %%eax\n\t"
"int $0x80"
:
: "D"(fd), "d"(size), "S"(buf)
: "eax"
);
}
void reboot() {
asm volatile (
"movl $2, %%eax\n\t"
"int $0x80"
:
:
: "eax"
);
}
uint64_t uptime() {
uint32_t low = 0;
uint32_t high = 0;
asm volatile (
"movl $3, %%eax\n\t"
"movl %0, %%esi\n\t"
"movl %1, %%edi\n\t"
"int $0x80"
:
: "r" (&low), "r" (&high)
: "eax", "esi"
);
return (uint64_t)low | ((uint64_t)high << 32);
}
extern char *buf;
void print_int(int num){
if (num < 0)
{
putc('-');
num = -num;
}
if (num > 9) print_int(num/10);
putc(('0'+ (num%10)));
}
void _start() {
char buf1[256] = {0};
//puts("Welcome to the Hazel user shell!\n");
puts(test);
while (1) {
puts("$ ");
read(0, buf1, 256);
if (!strcmp(buf1, "SKIBIDI")) {
puts("dop dop yes yes!\n");
} else if (!strcmp(buf1, "REBOOT")) {
reboot();
} else if (!strcmp(buf1, "UPTIME")) {
uint64_t ticks = uptime();
print_int(ticks);
puts("\n");
}
else {
puts("unknown command!\n");
}
}
}