[ECOS] tty_write yields written bytes count

Robin Farine robin.farine@acn-group.ch
Mon Feb 25 07:59:00 GMT 2002


Hi,

I'm using the TTY driver with CR/LF translations for console IO. When I
write a single '\n', I get the assertion failure below:

ASSERT FAIL: <4>stream.inl [ 179]
Cyg_ErrNo Cyg_StdioStream::write_byte()
Single byte not written, but no error returned!

The tty_write() routine translates the '\n' into '\r\n' and thus returns
2 which confuses the Cyg_StdioStream::write_byte() routine.

I believe that tty_write() should return the number of bytes consumed
and successfully passed to the underlying device. The attached patch
proposes a possible modification to tty_write() and a ChangeLog entry
follows just in case ...

Robin

2002-02-25  Robin Farine  <robin.farine@terminus.org>

	* src/common/tty.c (tty_write): Instead of the number of bytes
	actually written (which may include extra bytes due to e.g. CR/LF
	translations), yields the number of bytes consumed from the
	caller's buffer and successfully passed to the underlying output
	channel.

-------------- next part --------------
Index: packages/io/serial/current/src/common/tty.c
===================================================================
RCS file: /home/cvs/eCos/base/packages/io/serial/current/src/common/tty.c,v
retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 tty.c
--- packages/io/serial/current/src/common/tty.c	8 Mar 2001 08:57:15 -0000	1.1.1.1
+++ packages/io/serial/current/src/common/tty.c	25 Feb 2002 14:54:47 -0000
@@ -158,35 +158,40 @@ tty_write(cyg_io_handle_t handle, const 
     cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
     struct tty_private_info *priv = (struct tty_private_info *)t->priv;
     cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle;
-    cyg_int32 size, bytes_successful, actually_written;
+    cyg_int32 size, bytes_successful, bytes_consumed;
     cyg_uint8 xbuf[BUFSIZE];
     cyg_uint8 c;
     cyg_uint8 *buf = (cyg_uint8 *)_buf;
-    Cyg_ErrNo res = -EBADF;
+    Cyg_ErrNo res = ENOERR;
     // assert(chan)
     size = 0;
     bytes_successful = 0;
-    actually_written = 0;
-    while (bytes_successful++ < *len) {
+    bytes_consumed = 0;
+    while (bytes_successful < *len) {
+        ++bytes_successful;
         c = *buf++;
         if ((c == '\n') &&
             (priv->dev_info.tty_out_flags & CYG_TTY_OUT_FLAGS_CRLF)) {
             xbuf[size++] = '\r';
+            bytes_consumed = size;
         }
         xbuf[size++] = c;
         // Always leave room for possible CR/LF expansion
-        if ((size >= (BUFSIZE-1)) ||
-            (bytes_successful == *len)) {
+        if ((size >= (BUFSIZE-1) || bytes_successful == *len) &&
+            bytes_consumed == 0)
+            bytes_consumed = size;
+        if (bytes_consumed) { // triggers a flush of the output buffer.
             res = cyg_io_write(chan, xbuf, &size);
             if (res != ENOERR) {
-                *len = actually_written;
-                return res;
+                if (size < bytes_consumed)
+                    bytes_successful -= bytes_consumed - size;
+                break;
             }
-            actually_written += size;
             size = 0;
+            bytes_consumed = 0;
         }
     }
-    *len = actually_written;
+    *len = bytes_successful;
     return res;
 }
 

-------------- next part --------------
-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss


More information about the Ecos-discuss mailing list