first cut at new makefile; file reorg, new depend
[unix-history] / usr / src / usr.bin / telnet / utilities.c
index fb038d4..1407b0e 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.12 (Berkeley) %G%";
 #endif /* not lint */
 
 #define        TELOPTS
 #endif /* not lint */
 
 #define        TELOPTS
@@ -22,8 +27,12 @@ static char sccsid[] = "@(#)utilities.c      1.6 (Berkeley) %G%";
 
 #include "general.h"
 
 
 #include "general.h"
 
+#include "fdset.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 */
@@ -102,45 +111,41 @@ int       length;
            fprintf(NetTrace, "%.2x", (*pThis)&0xff);
            pThis++;
        }
            fprintf(NetTrace, "%.2x", (*pThis)&0xff);
            pThis++;
        }
-       fprintf(NetTrace, "\n");
+       if (NetTrace == stdout) {
+           fprintf(NetTrace, "\n");
+       } else {
+           fprintf(NetTrace, "\r\n");
+       }
        length -= BYTES_PER_LINE;
        offset += BYTES_PER_LINE;
        if (length < 0) {
        length -= BYTES_PER_LINE;
        offset += BYTES_PER_LINE;
        if (length < 0) {
+           fflush(NetTrace);
            return;
        }
        /* find next unique line */
     }
            return;
        }
        /* find next unique line */
     }
+    fflush(NetTrace);
 }
 
 
 }
 
 
-/*VARARGS*/
 void
 void
-printoption(direction, fmt, option, what)
+printoption(direction, fmt, option)
        char *direction, *fmt;
        char *direction, *fmt;
-       int option, what;
+       int option;
 {
        if (!showoptions)
                return;
 {
        if (!showoptions)
                return;
-       fprintf(NetTrace, "%s ", direction+1);
-       if (fmt == doopt)
-               fmt = "do";
-       else if (fmt == dont)
-               fmt = "dont";
-       else if (fmt == will)
-               fmt = "will";
-       else if (fmt == wont)
-               fmt = "wont";
-       else
-               fmt = "???";
+       fprintf(NetTrace, "%s ", direction);
        if (option < (sizeof telopts/sizeof telopts[0]))
                fprintf(NetTrace, "%s %s", fmt, telopts[option]);
        else
                fprintf(NetTrace, "%s %d", fmt, option);
        if (option < (sizeof telopts/sizeof telopts[0]))
                fprintf(NetTrace, "%s %s", fmt, telopts[option]);
        else
                fprintf(NetTrace, "%s %d", fmt, option);
-       if (*direction == '<') {
-               fprintf(NetTrace, "\r\n");
-               return;
+       if (NetTrace == stdout) {
+           fprintf(NetTrace, "\r\n");
+       } else {
+           fprintf(NetTrace, "\n");
        }
        }
-       fprintf(NetTrace, " (%s)\r\n", what ? "reply" : "don't reply");
+       return;
 }
 
 void
 }
 
 void
@@ -181,3 +186,92 @@ 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();
+    }
+#else  /* defined(TN3270) */
+    do {
+       telrcv();                       /* Process any incoming data */
+       EmptyTerminal();
+    } while (ring_full_count(&netiring));      /* While there is any */
+#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) */