-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.c
156 lines (137 loc) · 2.66 KB
/
console.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
#include "defs.h"
#include "common.h"
#include "kbd.h"
static struct {
struct spinlock lock;
int locking;
} cons;
struct {
struct spinlock lock;
char buf[128];
uint32_t ri; // read index
uint32_t wi; // write index
} clist;
void initconsole(void)
{
initlock(&clist.lock, "clist");
}
void consoleint(void)
{
uint8_t c;
// get the key and add it to the buffer
c = kbdgetc();
switch(c){
case 'p':
procdump();
break;
case 255:
return;
case '\n':
//clist.buf[clist.wi++] = '\n';
kprintf("WAKEUP",0);
wakeup(&clist.wi);
break;
default:
fb_put_char(c);
if(clist.wi == 128)
PANIC("consoleint: buffer is full!");
if(c == '\b'){
clist.buf[clist.wi--] = 0;
} else {
clist.buf[clist.wi++] = c;
}
}
}
int consoleread(struct inode *ip, char *dst, uint32_t n)
{
int len;
clist.ri = clist.wi = 0;
kprintf("\nconsoleread: loc of clist.buf: %h", &clist.buf);
kprintf("\nconsoleread: loc of clist.loc: %h", &clist.lock);
acquire(&clist.lock);
while(n > 0){
while(clist.ri == clist.wi){
sleep(&clist.wi, &clist.lock); // wait for something to arrive in buffer
}
for(clist.ri = 0; clist.ri<clist.wi;){
*dst++ = clist.buf[clist.ri++];
}
n = 0; // enter always ends the console read
}
release(&clist.lock);
*dst++ = 0;
len = clist.ri;
clist.ri = clist.wi = 0;
kprintf("\nconsoleread: %d", len);
return len;
}
static void
printint(int xx, int base, int sign)
{
static char digits[] = "0123456789abcdef";
char buf[16];
int i;
uint32_t x;
if(sign && (sign = xx < 0))
x = -xx;
else
x = xx;
i = 0;
do{
buf[i++] = digits[x % base];
}while((x /= base) != 0);
if(sign)
buf[i++] = '-';
while(--i >= 0)
fb_put_char(buf[i]);
}
//PAGEBREAK: 50
// Print to the console. only understands %d, %x, %p, %s.
void
kprintf(char *fmt, ...)
{
int i, c, locking;
uint32_t *argp;
char *s;
//locking = cons.locking;
//if(locking)
// acquire(&cons.lock);
if (fmt == 0)
PANIC("null fmt");
argp = (uint32_t*)(void*)(&fmt + 1);
for(i = 0; (c = fmt[i] & 0xff) != 0; i++){
if(c != '%'){
fb_put_char(c);
continue;
}
c = fmt[++i] & 0xff;
if(c == 0)
break;
switch(c){
case 'd':
printint(*argp++, 10, 1);
break;
case 'x':
case 'p':
case 'h':
printint(*argp++, 16, 0);
break;
case 's':
if((s = (char*)*argp++) == 0)
s = "(null)";
for(; *s; s++)
fb_put_char(*s);
break;
case '%':
fb_put_char('%');
break;
default:
// Print unknown % sequence to draw attention.
fb_put_char('%');
fb_put_char(c);
break;
}
}
//if(locking)
// release(&cons.lock);
}