rework to make a bit clearer, minor man page syntax stuff;
[unix-history] / usr / src / usr.bin / telnet / telnet.c
index e14596b..0d1d528 100644 (file)
@@ -1,16 +1,28 @@
-#ifndef lint
-static char copyright[] =
-"@(#) Copyright (c) 1984-1987 Regents of the University of California.\n\
- All rights reserved.\n";
-#endif /* not lint */
+/*
+ * 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
 
 #ifndef lint
-static char sccsid[] = "@(#)telnet.c   1.2 (Berkeley) 9/25/87";
-#endif /* not lint */
+static char sccsid[] = "@(#)telnet.c   5.38 (Berkeley) %G%";
+#endif /* not lint */
 
 #include <sys/types.h>
 
 #if    defined(unix)
 
 #include <sys/types.h>
 
 #if    defined(unix)
+#include <signal.h>
 /* By the way, we need to include curses.h before telnet.h since,
  * among other things, telnet.h #defines 'DO', which is a variable
  * declared in curses.h.
 /* By the way, we need to include curses.h before telnet.h since,
  * among other things, telnet.h #defines 'DO', which is a variable
  * declared in curses.h.
@@ -62,9 +74,12 @@ int
        crmod,
        netdata,        /* Print out network data flow */
        crlf,           /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
        crmod,
        netdata,        /* Print out network data flow */
        crlf,           /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
-       noasynch = 0,   /* User specified "-noasynch" on command line */
+#if    defined(TN3270)
+       noasynchtty = 0,/* User specified "-noasynch" on command line */
+       noasynchnet = 0,/* User specified "-noasynch" on command line */
        askedSGA = 0,   /* We have talked about suppress go ahead */
        askedSGA = 0,   /* We have talked about suppress go ahead */
-       telnetport = 1,
+#endif /* defined(TN3270) */
+       telnetport,
        SYNCHing,       /* we are in TELNET SYNCH mode */
        flushout,       /* flush output */
        autoflush = 0,  /* flush output when interrupting? */
        SYNCHing,       /* we are in TELNET SYNCH mode */
        flushout,       /* flush output */
        autoflush = 0,  /* flush output when interrupting? */
@@ -126,17 +141,11 @@ Modelist modelist[] = {
 
 init_telnet()
 {
 
 init_telnet()
 {
-    /* Don't change telnetport */
     SB_CLEAR();
     ClearArray(hisopts);
     ClearArray(myopts);
 
     connected = In3270 = ISend = donebinarytoggle = 0;
     SB_CLEAR();
     ClearArray(hisopts);
     ClearArray(myopts);
 
     connected = In3270 = ISend = donebinarytoggle = 0;
-    telnetport = 0;
-
-#if    defined(unix) && defined(TN3270)
-    HaveInput = 0;
-#endif /* defined(unix) && defined(TN3270) */
 
     SYNCHing = 0;
 
 
     SYNCHing = 0;
 
@@ -152,6 +161,7 @@ init_telnet()
 
 #include <varargs.h>
 
 
 #include <varargs.h>
 
+/*VARARGS*/
 static void
 printring(va_alist)
 va_dcl
 static void
 printring(va_alist)
 va_dcl
@@ -274,6 +284,7 @@ wontoption(option, reply)
                return;         /* Never reply to TM will's/wont's */
 
        default:
                return;         /* Never reply to TM will's/wont's */
 
        default:
+               hisopts[option] = 0;
                fmt = dont;
        }
        printring(&netoring, fmt, option);
                fmt = dont;
        }
        printring(&netoring, fmt, option);
@@ -347,6 +358,7 @@ suboption()
            name = getenv("TERM");
            if ((name == 0) || ((len = strlen(name)) > 40)) {
                name = "UNKNOWN";
            name = getenv("TERM");
            if ((name == 0) || ((len = strlen(name)) > 40)) {
                name = "UNKNOWN";
+               len = strlen(name);
            }
            if ((len + 4+2) < NETROOM()) {
                strcpy(namebuf, name);
            }
            if ((len + 4+2) < NETROOM()) {
                strcpy(namebuf, name);
@@ -368,7 +380,7 @@ suboption()
 }
 \f
 
 }
 \f
 
-static int
+int
 telrcv()
 {
     register int c;
 telrcv()
 {
     register int c;
@@ -402,10 +414,8 @@ telrcv()
            telrcv_state = TS_DATA;
            if (c == '\0') {
                break;  /* Ignore \0 after CR */
            telrcv_state = TS_DATA;
            if (c == '\0') {
                break;  /* Ignore \0 after CR */
-           } else if (c == '\n') {
-               if (hisopts[TELOPT_ECHO] && !crmod) {
-                   TTYADD(c);
-               }
+           } else if ((c == '\n') && (!hisopts[TELOPT_ECHO]) && !crmod) {
+               TTYADD(c);
                break;
            }
            /* Else, fall through */
                break;
            }
            /* Else, fall through */
@@ -413,7 +423,7 @@ telrcv()
        case TS_DATA:
            if (c == IAC) {
                telrcv_state = TS_IAC;
        case TS_DATA:
            if (c == IAC) {
                telrcv_state = TS_IAC;
-               continue;
+               break;
            }
 #          if defined(TN3270)
            if (In3270) {
            }
 #          if defined(TN3270)
            if (In3270) {
@@ -606,7 +616,8 @@ telrcv()
            }
        }
     }
            }
        }
     }
-    ring_consumed(&netiring, count);
+    if (count)
+       ring_consumed(&netiring, count);
     return returnValue||count;
 }
 
     return returnValue||count;
 }
 
@@ -693,7 +704,8 @@ telsnd()
            NETADD(c);
        }
     }
            NETADD(c);
        }
     }
-    ring_consumed(&ttyiring, count);
+    if (count)
+       ring_consumed(&ttyiring, count);
     return returnValue||count;         /* Non-zero if we did anything */
 }
 \f
     return returnValue||count;         /* Non-zero if we did anything */
 }
 \f
@@ -711,7 +723,6 @@ int
 Scheduler(block)
 int    block;                  /* should we block in the select ? */
 {
 Scheduler(block)
 int    block;                  /* should we block in the select ? */
 {
-    register int c;
                /* One wants to be a bit careful about setting returnValue
                 * to one, since a one implies we did some useful work,
                 * and therefore probably won't be called to block next
                /* One wants to be a bit careful about setting returnValue
                 * to one, since a one implies we did some useful work,
                 * and therefore probably won't be called to block next
@@ -757,12 +768,14 @@ int       block;                  /* should we block in the select ? */
     if (ring_full_count(&ttyiring)) {
 #   if defined(TN3270)
        if (In3270) {
     if (ring_full_count(&ttyiring)) {
 #   if defined(TN3270)
        if (In3270) {
-           c = DataFromTerminal(ttyiring.send,
+           int c;
+
+           c = DataFromTerminal(ttyiring.consume,
                                        ring_full_consecutive(&ttyiring));
            if (c) {
                returnValue = 1;
                                        ring_full_consecutive(&ttyiring));
            if (c) {
                returnValue = 1;
+               ring_consumed(&ttyiring, c);
            }
            }
-           ring_consumed(&ttyiring, c);
        } else {
 #   endif /* defined(TN3270) */
            returnValue |= telsnd();
        } else {
 #   endif /* defined(TN3270) */
            returnValue |= telsnd();
@@ -795,7 +808,7 @@ telnet()
            willoption(TELOPT_SGA, 0);
        }
        if (!myopts[TELOPT_TTYPE]) {
            willoption(TELOPT_SGA, 0);
        }
        if (!myopts[TELOPT_TTYPE]) {
-           dooption(TELOPT_TTYPE, 0);
+           dooption(TELOPT_TTYPE);
        }
     }
 #   endif /* !defined(TN3270) */
        }
     }
 #   endif /* !defined(TN3270) */
@@ -836,7 +849,7 @@ telnet()
                /* If there is data waiting to go out to terminal, don't
                 * schedule any more data for the terminal.
                 */
                /* If there is data waiting to go out to terminal, don't
                 * schedule any more data for the terminal.
                 */
-       if (tfrontp-tbackp) {
+       if (ring_full_count(&ttyoring)) {
            schedValue = 1;
        } else {
            if (shell_active) {
            schedValue = 1;
        } else {
            if (shell_active) {
@@ -857,6 +870,7 @@ telnet()
 #   endif /* !defined(TN3270) */
 }
 \f
 #   endif /* !defined(TN3270) */
 }
 \f
+#if    0       /* XXX - this not being in is a bug */
 /*
  * nextitem()
  *
 /*
  * nextitem()
  *
@@ -897,6 +911,7 @@ char        *current;
        return current+2;
     }
 }
        return current+2;
     }
 }
+#endif /* 0 */
 
 /*
  * netclear()
 
 /*
  * netclear()
@@ -1001,7 +1016,9 @@ void
 dosynch()
 {
     netclear();                        /* clear the path to the network */
 dosynch()
 {
     netclear();                        /* clear the path to the network */
-    NET2ADD(IAC, DM);
+    NETADD(IAC);
+    setneturg();
+    NETADD(DM);
 
 #if    defined(NOT43)
     return 0;
 
 #if    defined(NOT43)
     return 0;