# HG changeset patch # User anatofuz # Date 1583129659 -32400 # Node ID 1616cb02ececcaec5a88991c6577059d4df8b110 # Parent ea5ee6e71a3bae0b784d6fd8941d8ee5fa5bd01e add ConsoleIO.cbc diff -r ea5ee6e71a3b -r 1616cb02ecec src/impl/ConsoleIO.cbc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/impl/ConsoleIO.cbc Mon Mar 02 15:14:19 2020 +0900 @@ -0,0 +1,97 @@ +#include "../context.h" +#interface "IO.h" + +// ---- +// typedef struct ConsoleIO impl IO { +// __code consoleread(Type* IO, struct inode* ip, char* dst, int n, __code next(...)); +// __code consoleread1(Type* IO, int n, int target, char* dst, struct inode* ip, __code next(...)); +// __code consoleread2(Type* IO, struct inode* ip, __code next(...)); +// __code next(....); +// } ConsoleIO; +// ---- + +IO* createConsoleIO(struct Context* cbc_context) { + struct IO* io = new IO(); + struct ConsoleIO* console_i_o = new ConsoleIO(); + io->io = (union Data*)console_i_o; + console_i_o->IO = NULL; + console_i_o->ip = NULL; + console_i_o->dst = NULL; + console_i_o->n = 0; + console_i_o->target = 0; + console_i_o->consoleread = C_consoleread; + console_i_o->consoleread1 = C_consoleread1; + console_i_o->consoleread2 = C_consoleread2; + io->read = C_readConsoleIO; + io->write = C_writeConsoleIO; + io->close = C_closeConsoleIO; + return io; +} + +__code readConsoleIO(struct ConsoleIO* io, struct file* file, char* addr, int n, __code next(...)) { + struct inode* ip = file->ip; + // args should be assigned to the impl structure + goto io->consoleread(__code next(...)); +} + +__code consoleread(struct ConsoleIO* IO, struct inode* ip, char* dst, int n, __code next(...)) { + uint target; + + iunlock(ip); + + target = n; + acquire(&input.lock); + + if (n > 0) { + goto IO->consoleread2(IO, n, ip, target, dst, ip, next); + } + goto IO->consoleread1(IO, n, target, dst, ip, next); +} + +__code consoleread1(struct ConsoleIO* IO, int n, int target, char* dst, struct inode* ip, __code next(...)) { + int c = input.buf[input.r++ % INPUT_BUF]; + + if (c == C('D')) { // EOF + if (n < target) { + // Save ^D for next time, to make sure + // caller gets a 0-byte result. + input.r--; + } + cont = 0; + } + + *dst++ = c; + --n; + + if (c == '\n') { + cont = 0; + } + + if (cont == 1) { + if (n > 0) { + goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2); + } + } + + release(&input.lock); + ilock(ip); + + goto next(target - n); +} + +__code consoleread2(struct ConsoleIO* IO, struct inode* ip, __code next(...)) { + + goto next(...); +} + + +__code writeConsoleIO(struct ConsoleIO* io, struct file* file, char* addr, int n, __code next(...)) { + + goto next(...); +} + +__code closeConsoleIO(struct ConsoleIO* io, struct file* file, int fd, __code next(...)) { + + goto next(...); +} + diff -r ea5ee6e71a3b -r 1616cb02ecec src/impl/ConsoleIO.h --- a/src/impl/ConsoleIO.h Mon Mar 02 14:24:17 2020 +0900 +++ b/src/impl/ConsoleIO.h Mon Mar 02 15:14:19 2020 +0900 @@ -1,4 +1,5 @@ typedef struct ConsoleIO impl IO { + __code consoleread(Type* IO, struct inode* ip, char* dst, int n, __code next(...)); __code consoleread1(Type* IO, int n, int target, char* dst, struct inode* ip, __code next(...)); __code consoleread2(Type* IO, struct inode* ip, __code next(...)); __code next(....); diff -r ea5ee6e71a3b -r 1616cb02ecec src/interface/IO.h --- a/src/interface/IO.h Mon Mar 02 14:24:17 2020 +0900 +++ b/src/interface/IO.h Mon Mar 02 15:14:19 2020 +0900 @@ -1,5 +1,5 @@ typedef struct IO { - __code read(Impl* io, struct file* file, char* addr, __code next(...)); + __code read(Impl* io, struct file* file, char* addr, int n, __code next(...)); __code write(Impl* io, struct file* file, char* addr, int n, __code next(...)); __code close(Impl* io, struct file* file, int fd, __code next(...)); __code next(...);