added some useless shell commands
This commit is contained in:
Binary file not shown.
@@ -1,13 +1,46 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
const char* shell_prompt = "rocksh> ";
|
||||
|
||||
char buf[256];
|
||||
size_t buf_offset = 0;
|
||||
|
||||
void parse_cmd() {
|
||||
if (strcmp(buf, "exit") == 0) {
|
||||
printf("bye!\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (strcmp(buf, "motd") == 0) {
|
||||
printf("rockos is the best!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
memset(buf, 0, 256);
|
||||
buf_offset = 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
printf(shell_prompt);
|
||||
while (1) {
|
||||
char c;
|
||||
int rd = read(0, &c, 1);
|
||||
if (rd > 0) {
|
||||
if (c == '\n') {
|
||||
parse_cmd();
|
||||
reset();
|
||||
printf(shell_prompt);
|
||||
continue;
|
||||
}
|
||||
buf[buf_offset++] = c;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#define VGA_TEXT_W 80
|
||||
#define VGA_TEXT_H 25
|
||||
|
||||
void term();
|
||||
|
||||
int ptys;
|
||||
@@ -30,15 +33,37 @@ int main(int argc, char *argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void clear(int fd) {
|
||||
lseek(fd, 0, 0);
|
||||
size_t bytes = VGA_TEXT_W * VGA_TEXT_H;
|
||||
char c = ' ';
|
||||
for (size_t i = 0; i < bytes; i++) {
|
||||
write(fd, &c, 1);
|
||||
}
|
||||
lseek(fd, 0, 0);
|
||||
}
|
||||
|
||||
void term() {
|
||||
int vga_fd = open("/dev/tvga");
|
||||
if (vga_fd == -1) return;
|
||||
|
||||
int kbd_fd = open("/dev/kbd");
|
||||
if (kbd_fd == -1) return;
|
||||
|
||||
clear(vga_fd);
|
||||
|
||||
char c;
|
||||
while (1) {
|
||||
char c;
|
||||
// first, get shell output
|
||||
int rd = read(0, &c, 1);
|
||||
if (rd > 0) {
|
||||
write(vga_fd, &c, 1);
|
||||
}
|
||||
|
||||
// then read a byte from the kbd. If we got one, pass it to the shell
|
||||
rd = read(kbd_fd, &c, 1);
|
||||
if (rd > 0) {
|
||||
write(1, &c, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user