-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.c
More file actions
executable file
·85 lines (67 loc) · 1.26 KB
/
console.c
File metadata and controls
executable file
·85 lines (67 loc) · 1.26 KB
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
#include "uart.h"
// Parts stolen from LK by Travis Geiselbrecht
int console_tokenize_args(char *inbuf, char **argv){
enum{
WHITESPACE,
TOKEN
} state;
unsigned int argc = 0;
int idx = 0;
if(inbuf[idx] == ' '){
state = WHITESPACE;
}else{
state = TOKEN;
}
for(;;){
switch(state){
case WHITESPACE:
for(;;){
if(inbuf[idx] == '\0')
goto out;
if(inbuf[idx] == ' ')
inbuf[idx++] = '\0';
else
break;
}
state = TOKEN;
break;
case TOKEN:
argv[argc++] = &(inbuf[idx]);
while(inbuf[idx] != ' ' && inbuf[idx] != '\0') idx++;
if(inbuf[idx] == '\0')
goto out;
state = WHITESPACE;
break;
}
}
out:
return argc;
}
int console_read_line(char *outbuffer){
unsigned int pos = 0;
char c;
for(;;){
c = ugetchar();
switch(c){
case '\r':
case '\n':
uputchar('\n');
goto out;
case 0x7f: // backspace or delete
case 0x8:
if(pos > 0){
pos--;
uprintf("\x1b[1D"); // move to the left one
uputchar(' '); // overwrite
uprintf("\x1b[1D"); // move to the left one
}
break;
default:
outbuffer[pos++] = c;
uputchar(c);
}
}
out:
outbuffer[pos] = '\0';
return pos;
}