Flush out the last dregs in the terminal before quitting when
[unix-history] / usr / src / usr.bin / telnet / utilities.c
index fb038d4..2e5c5de 100644 (file)
@@ -3,15 +3,20 @@
  * 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[] = "@(#)utilities.c        1.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)utilities.c        1.8 (Berkeley) %G%";
 #endif /* not lint */
 
 #define        TELOPTS
 #endif /* not lint */
 
 #define        TELOPTS
@@ -24,6 +29,8 @@ static char sccsid[] = "@(#)utilities.c       1.6 (Berkeley) %G%";
 
 #include "ring.h"
 
 
 #include "ring.h"
 
+#include "defines.h"
+
 #include "externs.h"
 
 FILE   *NetTrace = 0;          /* Not in bss, since needs to stay */
 #include "externs.h"
 
 FILE   *NetTrace = 0;          /* Not in bss, since needs to stay */
@@ -181,3 +188,87 @@ int        length;                 /* length of suboption data */
        }
     }
 }
        }
     }
 }
+
+/* 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() == 0) {
+#if    defined(unix)
+       FD_SET(tout, &o);
+       (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 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, (fd_set *) 0, &o, (fd_set *) 0,
+                               (struct timeval *) 0);  /* wait for TTLOWAT */
+#endif /* defined(unix) */
+       }
+    }
+}
+
+void
+SetForExit()
+{
+    setconnmode();
+#if    defined(TN3270)
+    if (In3270) {
+       Finish3270();
+    }
+#endif /* defined(TN3270) */
+    setcommandmode();
+    fflush(stdout);
+    fflush(stderr);
+#if    defined(TN3270)
+    if (In3270) {
+       StopScreen(1);
+    }
+#endif /* defined(TN3270) */
+    setconnmode();
+    EmptyTerminal();                   /* Flush the path to the tty */
+    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);
+}
+
+#if defined(MSDOS)
+void
+ExitPerror(string, returnCode)
+char *string;
+int returnCode;
+{
+    SetForExit();
+    perror(string);
+    exit(returnCode);
+}
+#endif /* defined(MSDOS) */