Separate rawcon and stdcon; actually make input work
authorhpa <hpa>
Wed, 17 Nov 2004 06:24:12 +0000 (06:24 +0000)
committerhpa <hpa>
Wed, 17 Nov 2004 06:24:12 +0000 (06:24 +0000)
12 files changed:
com32/include/console.h
com32/lib/Makefile
com32/lib/sys/close.c
com32/lib/sys/file.h
com32/lib/sys/line_input.c [new file with mode: 0644]
com32/lib/sys/open.c
com32/lib/sys/opendev.c
com32/lib/sys/rawcon.c [new file with mode: 0644]
com32/lib/sys/rawcon_read.c [new file with mode: 0644]
com32/lib/sys/rawcon_write.c [new file with mode: 0644]
com32/lib/sys/stdcon_read.c
com32/lib/sys/stdcon_write.c

index 78025fb..5db9165 100644 (file)
 struct dev_info;
 __extern int openconsole(const struct dev_info *);
 
+/* Standard line-oriented console */
 extern const struct dev_info dev_stdcon;
+/* Raw character-oriented console */
+extern const struct dev_info dev_rawcon;
 
 #endif /* _CONSOLE_H */
 
index 857f8fb..f89dd13 100644 (file)
@@ -79,8 +79,10 @@ LIBOBJS = \
        sys/entry.o sys/exit.o \
        sys/fileinfo.o sys/opendev.o sys/read.o sys/write.o sys/close.o \
        sys/open.o sys/fileread.o sys/fileclose.o \
-       sys/isatty.o sys/openconsole.o \
-       sys/stdcon.o sys/stdcon_write.o sys/stdcon_read.o
+       sys/isatty.o sys/openconsole.o sys/line_input.o \
+       sys/stdcon.o sys/stdcon_write.o sys/stdcon_read.o \
+       sys/rawcon.o sys/rawcon_write.o sys/rawcon_read.o
+
 
 all: libcom32.a
 
index 7af7ff1..9837d4b 100644 (file)
@@ -50,7 +50,7 @@ int close(int fd)
     if ( rv )
       return rv;
   }
-
-  fp->ops = NULL;              /* File structure unused */
+  
+  memset(fp, 0, sizeof *fp);   /* File structure unused */
   return 0;
 }
index 194c35d..aae8e60 100644 (file)
@@ -80,4 +80,8 @@ struct file_info {
 
 extern struct file_info __file_info[NFILES];
 
+/* Line input discipline */
+ssize_t __line_input(struct file_info *fp, char *buf, size_t bufsize,
+                     ssize_t (*get_char)(struct file_info *, void *, size_t));
+
 #endif /* _COM32_SYS_FILE_H */
diff --git a/com32/lib/sys/line_input.c b/com32/lib/sys/line_input.c
new file mode 100644 (file)
index 0000000..c4dd8a5
--- /dev/null
@@ -0,0 +1,78 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *   
+ *   Copyright 2004 H. Peter Anvin - All Rights Reserved
+ *
+ *   Permission is hereby granted, free of charge, to any person
+ *   obtaining a copy of this software and associated documentation
+ *   files (the "Software"), to deal in the Software without
+ *   restriction, including without limitation the rights to use,
+ *   copy, modify, merge, publish, distribute, sublicense, and/or
+ *   sell copies of the Software, and to permit persons to whom
+ *   the Software is furnished to do so, subject to the following
+ *   conditions:
+ *   
+ *   The above copyright notice and this permission notice shall
+ *   be included in all copies or substantial portions of the Software.
+ *   
+ *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ *   OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * line_input.c
+ *
+ * Line-oriented input discupline
+ */
+
+#include "file.h"
+
+ssize_t __line_input(struct file_info *fp, char *buf, size_t bufsize,
+                    ssize_t (*get_char)(struct file_info *, void *, size_t))
+{
+  size_t n = 0;
+  char ch;
+
+  for(;;) {
+    if ( get_char(fp, &ch, 1) != 1 )
+      return n;
+    
+    switch ( ch ) {
+    case '\r':
+      *buf = '\n';
+      fp->ops->write(fp, "\n", 1);
+      return n+1;
+    
+    case '\b':
+      if ( n > 0 ) {
+       n--; buf--;
+       fp->ops->write(fp, "\b \b", 3);
+      }
+      break;
+
+    case '\x15':               /* Ctrl-U */
+      while ( n ) {
+       n--; buf--;
+       fp->ops->write(fp, "\b \b", 3);
+      }
+      break;
+      
+    default:
+      if ( n < bufsize-1 ) {
+       *buf = ch;
+       fp->ops->write(fp, buf, 1);
+       n++;
+       buf++;
+      }
+      break;
+    }
+  }
+}
+
index 27c22ce..139d104 100644 (file)
@@ -86,7 +86,6 @@ int open(const char *pathname, int flags, ...)
   fp->p.f.filedes   = regs.esi.w[0];
   fp->p.f.offset    = 0;
   fp->p.f.nbytes    = 0;
-  fp->p.f.datap     = fp->p.f.buf;
 
   return fd;
 } 
index 0d49a95..4ef2fe9 100644 (file)
@@ -57,6 +57,9 @@ int opendev(const struct dev_info *dev, int flags)
   }
 
   fp->ops = dev;
+  fp->p.f.offset    = 0;
+  fp->p.f.nbytes    = 0;
+  fp->p.f.datap     = fp->p.f.buf;
 
   return fd;
 }
diff --git a/com32/lib/sys/rawcon.c b/com32/lib/sys/rawcon.c
new file mode 100644 (file)
index 0000000..4f168c0
--- /dev/null
@@ -0,0 +1,53 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *   
+ *   Copyright 2004 H. Peter Anvin - All Rights Reserved
+ *
+ *   Permission is hereby granted, free of charge, to any person
+ *   obtaining a copy of this software and associated documentation
+ *   files (the "Software"), to deal in the Software without
+ *   restriction, including without limitation the rights to use,
+ *   copy, modify, merge, publish, distribute, sublicense, and/or
+ *   sell copies of the Software, and to permit persons to whom
+ *   the Software is furnished to do so, subject to the following
+ *   conditions:
+ *   
+ *   The above copyright notice and this permission notice shall
+ *   be included in all copies or substantial portions of the Software.
+ *   
+ *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ *   OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * rawcon.c
+ *
+ * Raw console
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <com32.h>
+#include <minmax.h>
+#include <fcntl.h>
+#include <console.h>
+#include "file.h"
+
+extern ssize_t __rawcon_read(struct file_info *, void *, size_t);
+extern ssize_t __rawcon_write(struct file_info *, const void *, size_t);
+
+const struct dev_info dev_rawcon = {
+  .dev_magic  = __DEV_MAGIC,
+  .flags      = __DEV_TTY,
+  .fileflags  = O_RDWR|O_CREAT|O_TRUNC|O_APPEND,
+  .read       = __rawcon_read,
+  .write      = __rawcon_write,
+  .close      = NULL,
+};
diff --git a/com32/lib/sys/rawcon_read.c b/com32/lib/sys/rawcon_read.c
new file mode 100644 (file)
index 0000000..dfea96b
--- /dev/null
@@ -0,0 +1,68 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *   
+ *   Copyright 2004 H. Peter Anvin - All Rights Reserved
+ *
+ *   Permission is hereby granted, free of charge, to any person
+ *   obtaining a copy of this software and associated documentation
+ *   files (the "Software"), to deal in the Software without
+ *   restriction, including without limitation the rights to use,
+ *   copy, modify, merge, publish, distribute, sublicense, and/or
+ *   sell copies of the Software, and to permit persons to whom
+ *   the Software is furnished to do so, subject to the following
+ *   conditions:
+ *   
+ *   The above copyright notice and this permission notice shall
+ *   be included in all copies or substantial portions of the Software.
+ *   
+ *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ *   OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * rawcon_read.c
+ *
+ * Character-oriented reading from the console without echo
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <com32.h>
+#include <minmax.h>
+#include "file.h"
+
+ssize_t __rawcon_read(struct file_info *fp, void *buf, size_t count)
+{
+  com32sys_t ireg, oreg;
+  char *bufp = buf;
+  size_t n = 0;
+
+  (void)fp;
+
+  memset(&ireg, 0, sizeof ireg); 
+
+  while ( count ) {
+    ireg.eax.b[1]  = 0x08;
+    __intcall(0x21, &ireg, &oreg);
+    *bufp++ = oreg.eax.b[0];
+    n++;
+    
+    if ( ! --count )
+      break;
+
+    /* Only return more than one if there is stuff in the buffer */
+    ireg.eax.b[1] = 0x0B;
+    __intcall(0x21, &ireg, &oreg);
+    if ( !oreg.eax.b[0] )
+      break;
+  }
+
+  return n;
+}
diff --git a/com32/lib/sys/rawcon_write.c b/com32/lib/sys/rawcon_write.c
new file mode 100644 (file)
index 0000000..8a037d2
--- /dev/null
@@ -0,0 +1,59 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *   
+ *   Copyright 2004 H. Peter Anvin - All Rights Reserved
+ *
+ *   Permission is hereby granted, free of charge, to any person
+ *   obtaining a copy of this software and associated documentation
+ *   files (the "Software"), to deal in the Software without
+ *   restriction, including without limitation the rights to use,
+ *   copy, modify, merge, publish, distribute, sublicense, and/or
+ *   sell copies of the Software, and to permit persons to whom
+ *   the Software is furnished to do so, subject to the following
+ *   conditions:
+ *   
+ *   The above copyright notice and this permission notice shall
+ *   be included in all copies or substantial portions of the Software.
+ *   
+ *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ *   OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * rawcon_write.c
+ *
+ * Raw writing to the console; no \n -> \r\n translation
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <com32.h>
+#include <minmax.h>
+#include "file.h"
+
+ssize_t __rawcon_write(struct file_info *fp, const void *buf, size_t count)
+{
+  com32sys_t ireg;
+  const char *bufp = buf;
+  size_t n = 0;
+
+  (void)fp;
+
+  memset(&ireg, 0, sizeof ireg); 
+  ireg.eax.b[1] = 0x02;
+
+  while ( count-- ) {
+    ireg.edx.b[0] = *bufp++;
+    __intcall(0x21, &ireg, NULL);
+    n++;
+  }
+
+  return n;
+}
index 0e5df19..840e095 100644 (file)
@@ -29,7 +29,7 @@
 /*
  * stdcon_read.c
  *
- * Reading from the console, with echo
+ * Line-oriented reading from the standard console
  */
 
 #include <errno.h>
 #include <minmax.h>
 #include "file.h"
 
+extern ssize_t __rawcon_read(struct file_info *fp, void *buf, size_t count);
+
 ssize_t __stdcon_read(struct file_info *fp, void *buf, size_t count)
 {
-  com32sys_t ireg, oreg;
   char *bufp = buf;
   size_t n = 0;
+  char ch;
 
   (void)fp;
 
-  memset(&ireg, 0, sizeof ireg); 
-
-  while ( count ) {
-    ireg.eax.b[1]  = 0x01;
-    __intcall(0x21, &ireg, &oreg);
-    *bufp++ = oreg.eax.b[0];
-    n++;
-    
-    if ( ! --count )
-      break;
+  while ( n < count ) {
+    if ( fp->p.f.nbytes ) {
+      ch = *bufp++ = *fp->p.f.datap++;
+      fp->p.f.nbytes--;
+      n++;
+      if ( ch == '\n' )
+       return n;
+    } else {
+      fp->p.f.nbytes = __line_input(fp, fp->p.f.buf, MAXBLOCK,
+                                   __rawcon_read);
+      fp->p.f.datap = fp->p.f.buf;
 
-    /* Only return more than one if there is stuff in the buffer */
-    ireg.eax.b[1] = 0x0B;
-    __intcall(0x21, &ireg, &oreg);
-    if ( !oreg.eax.b[0] )
-      break;
+      if ( fp->p.f.nbytes == 0 )
+       return n;
+    }
   }
 
   return n;
index 62bc4cc..9cfd07f 100644 (file)
@@ -27,7 +27,7 @@
  * ----------------------------------------------------------------------- */
 
 /*
- * write.c
+ * stdcon_write.c
  *
  * Writing to the console
  */