Flush out the last dregs in the terminal before quitting when
[unix-history] / usr / src / usr.bin / telnet / tn3270.c
index c484b27..74e855d 100644 (file)
@@ -3,20 +3,27 @@
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)tn3270.c   1.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)tn3270.c   1.16 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <arpa/telnet.h>
 
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <arpa/telnet.h>
 
+#include "general.h"
+
 #include "defines.h"
 #include "ring.h"
 #include "externs.h"
 #include "defines.h"
 #include "ring.h"
 #include "externs.h"
@@ -27,7 +34,14 @@ static char sccsid[] = "@(#)tn3270.c 1.7 (Berkeley) %G%";
 #include "../ctlr/screen.h"
 #include "../general/globals.h"
 
 #include "../ctlr/screen.h"
 #include "../general/globals.h"
 
+#include "../telextrn.h"
+#include "../ctlr/externs.h"
+
 #if    defined(unix)
 #if    defined(unix)
+int
+       HaveInput,              /* There is input available to scan */
+       sigiocount;             /* Number of times we got a SIGIO */
+
 char   tline[200];
 char   *transcom = 0;  /* transparent mode command (default: none) */
 #endif /* defined(unix) */
 char   tline[200];
 char   *transcom = 0;  /* transparent mode command (default: none) */
 #endif /* defined(unix) */
@@ -47,9 +61,13 @@ static int
 
 
 void
 
 
 void
-tn3270_init()
+init_3270()
 {
 #if    defined(TN3270)
 {
 #if    defined(TN3270)
+#if    defined(unix)
+    HaveInput = 0;
+    sigiocount = 0;
+#endif /* defined(unix) */
     Sent3270TerminalType = 0;
     Ifrontp = Ibackp = Ibuf;
     init_ctlr();               /* Initialize some things */
     Sent3270TerminalType = 0;
     Ifrontp = Ibackp = Ibuf;
     init_ctlr();               /* Initialize some things */
@@ -83,6 +101,7 @@ int          done;           /* is this the last of a logical block */
     origCount = count;
 
     while (count) {
     origCount = count;
 
     while (count) {
+       /* If not enough room for EORs, IACs, etc., wait */
        if (NETROOM() < 6) {
            fd_set o;
 
        if (NETROOM() < 6) {
            fd_set o;
 
@@ -95,9 +114,13 @@ int         done;           /* is this the last of a logical block */
                netflush();
            }
        }
                netflush();
            }
        }
-       c = loop = ring_empty_consecutive(&netoring);
+       c = ring_empty_count(&netoring);
+       if (c > count) {
+           c = count;
+       }
+       loop = c;
        while (loop) {
        while (loop) {
-           if (*buffer == IAC) {
+           if (((unsigned char)*buffer) == IAC) {
                break;
            }
            buffer++;
                break;
            }
            buffer++;
@@ -110,11 +133,12 @@ int               done;           /* is this the last of a logical block */
        if (loop) {
            NET2ADD(IAC, IAC);
            count--;
        if (loop) {
            NET2ADD(IAC, IAC);
            count--;
+           buffer++;
        }
     }
 
     if (done) {
        }
     }
 
     if (done) {
-       NET2ADD(IAC, IAC);
+       NET2ADD(IAC, EOR);
        netflush();             /* try to move along as quickly as ... */
     }
     return(origCount - count);
        netflush();             /* try to move along as quickly as ... */
     }
     return(origCount - count);
@@ -126,6 +150,7 @@ void
 inputAvailable()
 {
     HaveInput = 1;
 inputAvailable()
 {
     HaveInput = 1;
+    sigiocount++;
 }
 #endif /* defined(unix) */
 
 }
 #endif /* defined(unix) */
 
@@ -141,14 +166,19 @@ outputPurge()
  * routines make calls into telnet.c.
  */
 
  * routines make calls into telnet.c.
  */
 
-/* DataToTerminal - queue up some data to go to terminal. */
+/*
+ * DataToTerminal - queue up some data to go to terminal.
+ *
+ * Note: there are people who call us and depend on our processing
+ * *all* the data at one time (thus the select).
+ */
 
 int
 DataToTerminal(buffer, count)
 register char  *buffer;                /* where the data is */
 register int   count;                  /* how much to send */
 {
 
 int
 DataToTerminal(buffer, count)
 register char  *buffer;                /* where the data is */
 register int   count;                  /* how much to send */
 {
-    register int loop, c;
+    register int c;
     int origCount;
 
     origCount = count;
     int origCount;
 
     origCount = count;
@@ -160,62 +190,25 @@ register int      count;                  /* how much to send */
 
            FD_ZERO(&o);
 #endif /* defined(unix) */
 
            FD_ZERO(&o);
 #endif /* defined(unix) */
-           ttyflush();
+           ttyflush(0);
            while (TTYROOM() == 0) {
 #if    defined(unix)
                FD_SET(tout, &o);
                (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
                                                (struct timeval *) 0);
 #endif /* defined(unix) */
            while (TTYROOM() == 0) {
 #if    defined(unix)
                FD_SET(tout, &o);
                (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
                                                (struct timeval *) 0);
 #endif /* defined(unix) */
-               ttyflush();
-           }
-       }
-       c = loop = ring_empty_consecutive(&ttyoring);
-       while (loop) {
-           if (*buffer == IAC) {
-               break;
+               ttyflush(0);
            }
            }
-           buffer++;
-           loop--;
        }
        }
-       if ((c = c-loop)) {
-           ring_supply_data(&ttyoring, buffer-c, c);
-           count -= c;
-       }
-    }
-    return(origCount - count);
-}
-
-/* EmptyTerminal - called to make sure that the terminal buffer is empty.
- *                     Note that we consider the buffer to run all the
- *                     way to the kernel (thus the select).
- */
-
-void
-EmptyTerminal()
-{
-#if    defined(unix)
-    fd_set     o;
-
-    FD_ZERO(&o);
-#endif /* defined(unix) */
-
-    if (TTYBYTES()) {
-#if    defined(unix)
-       FD_SET(tout, &o);
-       (void) select(tout+1, (int *) 0, &o, (int *) 0,
-                       (struct timeval *) 0);  /* wait for TTLOWAT */
-#endif /* defined(unix) */
-    } else {
-       while (TTYBYTES()) {
-           ttyflush(0);
-#if    defined(unix)
-           FD_SET(tout, &o);
-           (void) select(tout+1, (int *) 0, &o, (int *) 0,
-                               (struct timeval *) 0);  /* wait for TTLOWAT */
-#endif /* defined(unix) */
+       c = TTYROOM();
+       if (c > count) {
+           c = count;
        }
        }
+       ring_supply_data(&ttyoring, buffer, c);
+       count -= c;
+       buffer += c;
     }
     }
+    return(origCount);
 }
 \f
 
 }
 \f
 
@@ -285,7 +278,10 @@ void
 _putchar(c)
 char c;
 {
 _putchar(c)
 char c;
 {
-    if (TTYBYTES()) {
+#if    defined(sun)            /* SunOS 4.0 bug */
+    c &= 0x7f;
+#endif /* defined(sun) */
+    if (!TTYROOM()) {
        (void) DataToTerminal(&c, 1);
     } else {
        TTYADD(c);
        (void) DataToTerminal(&c, 1);
     } else {
        TTYADD(c);
@@ -293,51 +289,6 @@ char c;
 }
 #endif /* ((!defined(NOT43)) || defined(PUTCHAR)) */
 
 }
 #endif /* ((!defined(NOT43)) || defined(PUTCHAR)) */
 
-void
-SetForExit()
-{
-    setconnmode();
-    if (In3270) {
-       Finish3270();
-    }
-    setcommandmode();
-    fflush(stdout);
-    fflush(stderr);
-    if (In3270) {
-       StopScreen(1);
-    }
-    setconnmode();
-    setcommandmode();
-}
-
-void
-Exit(returnCode)
-int returnCode;
-{
-    SetForExit();
-    exit(returnCode);
-}
-
-void
-ExitString(string, returnCode)
-char *string;
-int returnCode;
-{
-    SetForExit();
-    fwrite(string, 1, strlen(string), stderr);
-    exit(returnCode);
-}
-
-void
-ExitPerror(string, returnCode)
-char *string;
-int returnCode;
-{
-    SetForExit();
-    perror(string);
-    exit(returnCode);
-}
-
 void
 SetIn3270()
 {
 void
 SetIn3270()
 {
@@ -417,7 +368,7 @@ settranscom(argc, argv)
        int argc;
        char *argv[];
 {
        int argc;
        char *argv[];
 {
-       int i, len = 0;
+       int i;
 
        if (argc == 1 && transcom) {
           transcom = 0;
 
        if (argc == 1 && transcom) {
           transcom = 0;
@@ -425,9 +376,6 @@ settranscom(argc, argv)
        if (argc == 1) {
           return;
        }
        if (argc == 1) {
           return;
        }
-       for (i = 1; i < argc; ++i) {
-           len += 1 + strlen(argv[1]);
-       }
        transcom = tline;
        (void) strcpy(transcom, argv[1]);
        for (i = 2; i < argc; ++i) {
        transcom = tline;
        (void) strcpy(transcom, argv[1]);
        for (i = 2; i < argc; ++i) {