Mercurial > hg > Members > innparusu > xv6-rpi
comparison src/console.c @ 24:36bd61f5c847
rewrite sys_read cbc
author | mir3636 |
---|---|
date | Thu, 17 Jan 2019 19:11:19 +0900 |
parents | 83c23a36980d |
children | a146855e16eb |
comparison
equal
deleted
inserted
replaced
23:ee58360d0e99 | 24:36bd61f5c847 |
---|---|
213 } | 213 } |
214 | 214 |
215 release(&input.lock); | 215 release(&input.lock); |
216 } | 216 } |
217 | 217 |
218 __code cbc_consoleread (struct inode *ip, char *dst, int n, __code(*next)(int ret)) | |
219 { | |
220 uint target; | |
221 int c; | |
222 | |
223 iunlock(ip); | |
224 | |
225 target = n; | |
226 acquire(&input.lock); | |
227 | |
228 while (n > 0) { | |
229 while (input.r == input.w) { | |
230 if (proc->killed) { | |
231 release(&input.lock); | |
232 ilock(ip); | |
233 goto next(-1); | |
234 } | |
235 | |
236 proc->cbc_arg.cbc_console_arg.n = n; | |
237 proc->cbc_arg.cbc_console_arg.dst = dst; | |
238 proc->cbc_arg.cbc_console_arg.ip = ip; | |
239 proc->cbc_arg.cbc_console_arg.next = next; | |
240 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread1); | |
241 } | |
242 } | |
243 | |
244 __code cbc_consoleread1 (__code(*next)(int ret)) | |
245 { | |
246 int cont = 1; | |
247 proc->cbc_arg.cbc_console_arg.n = n; | |
248 proc->cbc_arg.cbc_console_arg.dst = dst; | |
249 proc->cbc_arg.cbc_console_arg.ip = ip; | |
250 | |
251 c = input.buf[input.r++ % INPUT_BUF]; | |
252 | |
253 if (c == C('D')) { // EOF | |
254 if (n < target) { | |
255 // Save ^D for next time, to make sure | |
256 // caller gets a 0-byte result. | |
257 input.r--; | |
258 } | |
259 cont = 0; | |
260 } | |
261 | |
262 *dst++ = c; | |
263 --n; | |
264 | |
265 if (c == '\n') { | |
266 cont = 0; | |
267 } | |
268 | |
269 | |
270 if (cont){ | |
271 proc->cbc_arg.cbc_console_arg.n = n; | |
272 proc->cbc_arg.cbc_console_arg.dst = dst; | |
273 proc->cbc_arg.cbc_console_arg.ip = ip; | |
274 proc->cbc_arg.cbc_console_arg.next = next; | |
275 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread1); | |
276 } | |
277 | |
278 release(&input.lock); | |
279 ilock(ip); | |
280 | |
281 goto next(target - n); | |
282 } | |
283 | |
218 int consoleread (struct inode *ip, char *dst, int n) | 284 int consoleread (struct inode *ip, char *dst, int n) |
219 { | 285 { |
220 uint target; | 286 uint target; |
221 int c; | 287 int c; |
222 | 288 |
286 initlock(&cons.lock, "console"); | 352 initlock(&cons.lock, "console"); |
287 initlock(&input.lock, "input"); | 353 initlock(&input.lock, "input"); |
288 | 354 |
289 devsw[CONSOLE].write = consolewrite; | 355 devsw[CONSOLE].write = consolewrite; |
290 devsw[CONSOLE].read = consoleread; | 356 devsw[CONSOLE].read = consoleread; |
357 cbc_devsw[CONSOLE].write = cbc_consolewrite; | |
358 cbc_devsw[CONSOLE].read = cbc_consoleread; | |
291 | 359 |
292 cons.locking = 1; | 360 cons.locking = 1; |
293 } | 361 } |
294 | 362 |