This source file includes following definitions.
- consputc
- consolewrite
- consoleread
- consoleintr
- consoleinit
1
2
3
4
5
6
7
8
9
10
11
12 #include <stdarg.h>
13
14 #include "types.h"
15 #include "param.h"
16 #include "spinlock.h"
17 #include "sleeplock.h"
18 #include "fs.h"
19 #include "file.h"
20 #include "memlayout.h"
21 #include "riscv.h"
22 #include "defs.h"
23 #include "proc.h"
24
25 #define BACKSPACE 0x100
26 #define C(x) ((x)-'@')
27
28
29
30
31
32
33 void
34 consputc(int c)
35 {
36 if(c == BACKSPACE){
37
38 uartputc_sync('\b'); uartputc_sync(' '); uartputc_sync('\b');
39 } else {
40 uartputc_sync(c);
41 }
42 }
43
44 struct {
45 struct spinlock lock;
46
47
48 #define INPUT_BUF_SIZE 128
49 char buf[INPUT_BUF_SIZE];
50 uint r;
51 uint w;
52 uint e;
53 } cons;
54
55
56
57
58 int
59 consolewrite(int user_src, uint64 src, int n)
60 {
61 char buf[32];
62 int i = 0;
63
64 while(i < n){
65 int nn = sizeof(buf);
66 if(nn > n - i)
67 nn = n - i;
68 if(either_copyin(buf, user_src, src+i, nn) == -1)
69 break;
70 uartwrite(buf, nn);
71 i += nn;
72 }
73
74 return i;
75 }
76
77
78
79
80
81
82
83 int
84 consoleread(int user_dst, uint64 dst, int n)
85 {
86 uint target;
87 int c;
88 char cbuf;
89
90 target = n;
91 acquire(&cons.lock);
92 while(n > 0){
93
94
95 while(cons.r == cons.w){
96 if(killed(myproc())){
97 release(&cons.lock);
98 return -1;
99 }
100 sleep(&cons.r, &cons.lock);
101 }
102
103 c = cons.buf[cons.r++ % INPUT_BUF_SIZE];
104
105 if(c == C('D')){
106 if(n < target){
107
108
109 cons.r--;
110 }
111 break;
112 }
113
114
115 cbuf = c;
116 if(either_copyout(user_dst, dst, &cbuf, 1) == -1)
117 break;
118
119 dst++;
120 --n;
121
122 if(c == '\n'){
123
124
125 break;
126 }
127 }
128 release(&cons.lock);
129
130 return target - n;
131 }
132
133
134
135
136
137
138
139 void
140 consoleintr(int c)
141 {
142 acquire(&cons.lock);
143
144 switch(c){
145 case C('P'):
146 procdump();
147 break;
148 case C('U'):
149 while(cons.e != cons.w &&
150 cons.buf[(cons.e-1) % INPUT_BUF_SIZE] != '\n'){
151 cons.e--;
152 consputc(BACKSPACE);
153 }
154 break;
155 case C('H'):
156 case '\x7f':
157 if(cons.e != cons.w){
158 cons.e--;
159 consputc(BACKSPACE);
160 }
161 break;
162 default:
163 if(c != 0 && cons.e-cons.r < INPUT_BUF_SIZE){
164 c = (c == '\r') ? '\n' : c;
165
166
167 consputc(c);
168
169
170 cons.buf[cons.e++ % INPUT_BUF_SIZE] = c;
171
172 if(c == '\n' || c == C('D') || cons.e-cons.r == INPUT_BUF_SIZE){
173
174
175 cons.w = cons.e;
176 wakeup(&cons.r);
177 }
178 }
179 break;
180 }
181
182 release(&cons.lock);
183 }
184
185 void
186 consoleinit(void)
187 {
188 initlock(&cons.lock, "cons");
189
190 uartinit();
191
192
193
194 devsw[CONSOLE].read = consoleread;
195 devsw[CONSOLE].write = consolewrite;
196 }