-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlibyehos.c
165 lines (142 loc) · 3.51 KB
/
libyehos.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <ourtypes.h>
int READLINE_SCREEN_WIDTH = 80;
int readline_cursor_row = 0;
int readline_cursor_col = 0;
/* inline requests the compiler to replace function call with actual function code
* (to avoid the overhead of function setup and teardown) */
inline int32_t syscall(int syscall_num, const int32_t *parms)
{
int32_t ret;
/* put syscall_num in eax (same as where return val will come) and parms pointer in ebx*/
asm volatile(
"int $0x30\n"
: "=a" (ret)
: "0" (syscall_num), "b" (parms)
: "memory");
return ret;
}
void *
mmap(void *addr, int length, int prot, int flags,
const char *filename, int offset)
{
int32_t parms[] = { (int32_t) filename, (int32_t) addr };
return (void *) syscall(1, parms);
}
#include "memlib.c"
char
read()
{
return (char) syscall(2, NULL);
}
void
write(char c)
{
int32_t parms[] = { c };
(void) syscall(3, parms);
}
void
setcursor(int x, int y)
{
int32_t parms[] = { y, x };
(void) syscall(4, parms);
}
void
writechar(int x, int y, char c, char color)
{
int32_t parms[] = { color, c, y, x };
(void) syscall(5, parms);
}
void
scroll()
{
(void) syscall(6, NULL);
}
void
yield()
{
(void) syscall(7, NULL);
}
int
get_timer_index() {
return (int) syscall(8, NULL);
}
void clear_screen() {
int col, row;
for (row = 0; row < 25; row++) {
for (col = 0; col < 80; col++){
writechar(col, row, ' ', 0xf);
}
}
}
void _readline_cursor_next_line() {
if (readline_cursor_row < 24) {
readline_cursor_row++;
} else {
scroll();
}
readline_cursor_col = 0;
setcursor(readline_cursor_col, readline_cursor_row);
}
void _readline_cursor_next() {
readline_cursor_col++;
if (readline_cursor_col >= READLINE_SCREEN_WIDTH) {
_readline_cursor_next_line();
}
setcursor(readline_cursor_col, readline_cursor_row);
}
void _readline_writechar(int x, int y, char c, char color) {
writechar(x, y, c, color);
_readline_cursor_next();
}
void _readline_delete() {
if (readline_cursor_col == 0) {
readline_cursor_row--;
readline_cursor_col = 80;
} else {
readline_cursor_col--;
}
writechar(readline_cursor_col, readline_cursor_row, ' ', 0xf);
}
void
puts(char *s) {
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (s[i] == '\r' || s[i] == '\n') {
_readline_cursor_next_line();
} else {
_readline_writechar(readline_cursor_col, readline_cursor_row, s[i], 0xf);
}
}
}
int
readline(char *buf) {
int read_count = 0;
int index = 0;
_readline_writechar(readline_cursor_col, readline_cursor_row, '>', 0xf);
_readline_writechar(readline_cursor_col, readline_cursor_row, ' ', 0xf);
int BACKSPACE = 8;
while(1){
setcursor(readline_cursor_col, readline_cursor_row);
char c = read();
if (c == '\n' || c == '\r'){
_readline_cursor_next_line();
buf[index] = '\0';
return read_count;
} else if (c == BACKSPACE) {
if (index >= 0) {
if (index > 0) {
_readline_delete();
index--;
}
buf[index] = '\0';
}
} else if (c > 0) {
_readline_writechar(readline_cursor_col, readline_cursor_row, c, 0xf);
buf[index++] = c;
read_count++;
} else {
yield();
}
}
return read_count;
}