changeset 348:1616cb02ecec

add ConsoleIO.cbc
author anatofuz
date Mon, 02 Mar 2020 15:14:19 +0900
parents ea5ee6e71a3b
children 0e72eb96b6b1
files src/impl/ConsoleIO.cbc src/impl/ConsoleIO.h src/interface/IO.h
diffstat 3 files changed, 99 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /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 <Type, Isa> 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(...);
+}
+
--- 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 <Type, Isa> 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(....);
--- 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 <Type, Impl> {
-    __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(...);