Flush out the last dregs in the terminal before quitting when
[unix-history] / usr / src / usr.bin / telnet / utilities.c
index 09548c8..2e5c5de 100644 (file)
@@ -1,11 +1,36 @@
+/*
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * 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
+static char sccsid[] = "@(#)utilities.c        1.8 (Berkeley) %G%";
+#endif /* not lint */
+
 #define        TELOPTS
 #include <arpa/telnet.h>
 #include <sys/types.h>
 
 #include <ctype.h>
 
 #define        TELOPTS
 #include <arpa/telnet.h>
 #include <sys/types.h>
 
 #include <ctype.h>
 
+#include "general.h"
+
 #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 */
@@ -153,7 +178,8 @@ int length;                 /* length of suboption data */
                break;
            default:
                fprintf(NetTrace,
                break;
            default:
                fprintf(NetTrace,
-                               "- unknown qualifier %d (0x%x).\n", pointer[1]);
+                               "- unknown qualifier %d (0x%x).\n",
+                               pointer[1], pointer[1]);
            }
            break;
        default:
            }
            break;
        default:
@@ -162,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) */