+/*
+ * ttloop
+ *
+ * A small subroutine to flush the network output buffer, get some data
+ * from the network, and pass it through the telnet state machine. We
+ * also flush the pty input buffer (by dropping its data) if it becomes
+ * too full.
+ */
+
+void
+ttloop()
+{
+ if (nfrontp-nbackp) {
+ netflush();
+ }
+ ncc = read(net, netibuf, sizeof netibuf);
+ if (ncc < 0) {
+ syslog(LOG_INFO, "ttloop: read: %m\n");
+ exit(1);
+ } else if (ncc == 0) {
+ syslog(LOG_INFO, "ttloop: peer died: %m\n");
+ exit(1);
+ }
+ netip = netibuf;
+ telrcv(); /* state machine */
+ if (ncc > 0) {
+ pfrontp = pbackp = ptyobuf;
+ telrcv();
+ }
+}
+
+/*
+ * getterminaltype
+ *
+ * Ask the other end to send along its terminal type.
+ * Output is the variable terminaltype filled in.
+ */
+
+void
+getterminaltype()
+{
+ static char sbuf[] = { IAC, DO, TELOPT_TTYPE };
+
+ settimer(getterminal);
+ bcopy(sbuf, nfrontp, sizeof sbuf);
+ nfrontp += sizeof sbuf;
+ hisopts[TELOPT_TTYPE] = OPT_YES_BUT_ALWAYS_LOOK;
+ while (sequenceIs(ttypeopt, getterminal)) {
+ ttloop();
+ }
+ if (hisopts[TELOPT_TTYPE] == OPT_YES) {
+ static char sbbuf[] = { IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE };
+
+ bcopy(sbbuf, nfrontp, sizeof sbbuf);
+ nfrontp += sizeof sbbuf;
+ while (sequenceIs(ttypesubopt, getterminal)) {
+ ttloop();
+ }
+ }
+}
+