18d5a6048SAxel Dörfler /*
28d5a6048SAxel Dörfler * Copyright (c) 1988, 1990, 1993
38d5a6048SAxel Dörfler * The Regents of the University of California. All rights reserved.
48d5a6048SAxel Dörfler *
58d5a6048SAxel Dörfler * Redistribution and use in source and binary forms, with or without
68d5a6048SAxel Dörfler * modification, are permitted provided that the following conditions
78d5a6048SAxel Dörfler * are met:
88d5a6048SAxel Dörfler * 1. Redistributions of source code must retain the above copyright
98d5a6048SAxel Dörfler * notice, this list of conditions and the following disclaimer.
108d5a6048SAxel Dörfler * 2. Redistributions in binary form must reproduce the above copyright
118d5a6048SAxel Dörfler * notice, this list of conditions and the following disclaimer in the
128d5a6048SAxel Dörfler * documentation and/or other materials provided with the distribution.
13*6b99b206SAugustin Cavalier * 3. Neither the name of the University nor the names of its contributors
148d5a6048SAxel Dörfler * may be used to endorse or promote products derived from this software
158d5a6048SAxel Dörfler * without specific prior written permission.
168d5a6048SAxel Dörfler *
178d5a6048SAxel Dörfler * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
188d5a6048SAxel Dörfler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198d5a6048SAxel Dörfler * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208d5a6048SAxel Dörfler * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
218d5a6048SAxel Dörfler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228d5a6048SAxel Dörfler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238d5a6048SAxel Dörfler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248d5a6048SAxel Dörfler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258d5a6048SAxel Dörfler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268d5a6048SAxel Dörfler * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278d5a6048SAxel Dörfler * SUCH DAMAGE.
288d5a6048SAxel Dörfler */
298d5a6048SAxel Dörfler
308d5a6048SAxel Dörfler #if 0
318d5a6048SAxel Dörfler #ifndef lint
328d5a6048SAxel Dörfler static const char sccsid[] = "@(#)terminal.c 8.2 (Berkeley) 2/16/95";
338d5a6048SAxel Dörfler #endif
348d5a6048SAxel Dörfler #endif
358d5a6048SAxel Dörfler #include <sys/cdefs.h>
36*6b99b206SAugustin Cavalier __FBSDID("$FreeBSD$");
378d5a6048SAxel Dörfler
388d5a6048SAxel Dörfler #include <arpa/telnet.h>
398d5a6048SAxel Dörfler #include <sys/types.h>
408d5a6048SAxel Dörfler
418d5a6048SAxel Dörfler #include <stdlib.h>
428d5a6048SAxel Dörfler
438d5a6048SAxel Dörfler #include "ring.h"
448d5a6048SAxel Dörfler
458d5a6048SAxel Dörfler #include "externs.h"
468d5a6048SAxel Dörfler #include "types.h"
478d5a6048SAxel Dörfler
488d5a6048SAxel Dörfler #ifdef ENCRYPTION
498d5a6048SAxel Dörfler #include <libtelnet/encrypt.h>
508d5a6048SAxel Dörfler #endif
518d5a6048SAxel Dörfler
528d5a6048SAxel Dörfler Ring ttyoring, ttyiring;
538d5a6048SAxel Dörfler unsigned char ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ];
548d5a6048SAxel Dörfler
558d5a6048SAxel Dörfler int termdata; /* Debugging flag */
568d5a6048SAxel Dörfler
578d5a6048SAxel Dörfler #ifdef USE_TERMIO
588d5a6048SAxel Dörfler # ifndef VDISCARD
598d5a6048SAxel Dörfler cc_t termFlushChar;
608d5a6048SAxel Dörfler # endif
618d5a6048SAxel Dörfler # ifndef VLNEXT
628d5a6048SAxel Dörfler cc_t termLiteralNextChar;
638d5a6048SAxel Dörfler # endif
648d5a6048SAxel Dörfler # ifndef VSUSP
658d5a6048SAxel Dörfler cc_t termSuspChar;
668d5a6048SAxel Dörfler # endif
678d5a6048SAxel Dörfler # ifndef VWERASE
688d5a6048SAxel Dörfler cc_t termWerasChar;
698d5a6048SAxel Dörfler # endif
708d5a6048SAxel Dörfler # ifndef VREPRINT
718d5a6048SAxel Dörfler cc_t termRprntChar;
728d5a6048SAxel Dörfler # endif
738d5a6048SAxel Dörfler # ifndef VSTART
748d5a6048SAxel Dörfler cc_t termStartChar;
758d5a6048SAxel Dörfler # endif
768d5a6048SAxel Dörfler # ifndef VSTOP
778d5a6048SAxel Dörfler cc_t termStopChar;
788d5a6048SAxel Dörfler # endif
798d5a6048SAxel Dörfler # ifndef VEOL
808d5a6048SAxel Dörfler cc_t termForw1Char;
818d5a6048SAxel Dörfler # endif
828d5a6048SAxel Dörfler # ifndef VEOL2
838d5a6048SAxel Dörfler cc_t termForw2Char;
848d5a6048SAxel Dörfler # endif
858d5a6048SAxel Dörfler # ifndef VSTATUS
868d5a6048SAxel Dörfler cc_t termAytChar;
878d5a6048SAxel Dörfler # endif
888d5a6048SAxel Dörfler #else
898d5a6048SAxel Dörfler cc_t termForw2Char;
908d5a6048SAxel Dörfler cc_t termAytChar;
918d5a6048SAxel Dörfler #endif
928d5a6048SAxel Dörfler
938d5a6048SAxel Dörfler /*
948d5a6048SAxel Dörfler * initialize the terminal data structures.
958d5a6048SAxel Dörfler */
968d5a6048SAxel Dörfler
978d5a6048SAxel Dörfler void
init_terminal(void)988d5a6048SAxel Dörfler init_terminal(void)
998d5a6048SAxel Dörfler {
1008d5a6048SAxel Dörfler if (ring_init(&ttyoring, ttyobuf, sizeof ttyobuf) != 1) {
1018d5a6048SAxel Dörfler exit(1);
1028d5a6048SAxel Dörfler }
1038d5a6048SAxel Dörfler if (ring_init(&ttyiring, ttyibuf, sizeof ttyibuf) != 1) {
1048d5a6048SAxel Dörfler exit(1);
1058d5a6048SAxel Dörfler }
1068d5a6048SAxel Dörfler autoflush = TerminalAutoFlush();
1078d5a6048SAxel Dörfler }
1088d5a6048SAxel Dörfler
1098d5a6048SAxel Dörfler /*
110*6b99b206SAugustin Cavalier * Send as much data as possible to the terminal, else exits if
111*6b99b206SAugustin Cavalier * it encounters a permanent failure when writing to the tty.
1128d5a6048SAxel Dörfler *
1138d5a6048SAxel Dörfler * Return value:
1148d5a6048SAxel Dörfler * -1: No useful work done, data waiting to go out.
1158d5a6048SAxel Dörfler * 0: No data was waiting, so nothing was done.
1168d5a6048SAxel Dörfler * 1: All waiting data was written out.
1178d5a6048SAxel Dörfler * n: All data - n was written out.
1188d5a6048SAxel Dörfler */
1198d5a6048SAxel Dörfler
1208d5a6048SAxel Dörfler int
ttyflush(int drop)1218d5a6048SAxel Dörfler ttyflush(int drop)
1228d5a6048SAxel Dörfler {
1238d5a6048SAxel Dörfler int n, n0, n1;
1248d5a6048SAxel Dörfler
1258d5a6048SAxel Dörfler n0 = ring_full_count(&ttyoring);
1268d5a6048SAxel Dörfler if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
1278d5a6048SAxel Dörfler if (drop) {
1288d5a6048SAxel Dörfler TerminalFlushOutput();
1298d5a6048SAxel Dörfler /* we leave 'n' alone! */
1308d5a6048SAxel Dörfler } else {
1318d5a6048SAxel Dörfler n = TerminalWrite(ttyoring.consume, n);
1328d5a6048SAxel Dörfler }
1338d5a6048SAxel Dörfler }
1348d5a6048SAxel Dörfler if (n > 0) {
1358d5a6048SAxel Dörfler if (termdata && n) {
1368d5a6048SAxel Dörfler Dump('>', ttyoring.consume, n);
1378d5a6048SAxel Dörfler }
1388d5a6048SAxel Dörfler /*
1398d5a6048SAxel Dörfler * If we wrote everything, and the full count is
1408d5a6048SAxel Dörfler * larger than what we wrote, then write the
1418d5a6048SAxel Dörfler * rest of the buffer.
1428d5a6048SAxel Dörfler */
1438d5a6048SAxel Dörfler if (n1 == n && n0 > n) {
1448d5a6048SAxel Dörfler n1 = n0 - n;
1458d5a6048SAxel Dörfler if (!drop)
1468d5a6048SAxel Dörfler n1 = TerminalWrite(ttyoring.bottom, n1);
1478d5a6048SAxel Dörfler if (n1 > 0)
1488d5a6048SAxel Dörfler n += n1;
1498d5a6048SAxel Dörfler }
1508d5a6048SAxel Dörfler ring_consumed(&ttyoring, n);
1518d5a6048SAxel Dörfler }
152*6b99b206SAugustin Cavalier if (n < 0) {
153*6b99b206SAugustin Cavalier if (errno == EAGAIN || errno == EINTR) {
1548d5a6048SAxel Dörfler return -1;
155*6b99b206SAugustin Cavalier } else {
156*6b99b206SAugustin Cavalier ring_consumed(&ttyoring, ring_full_count(&ttyoring));
157*6b99b206SAugustin Cavalier setconnmode(0);
158*6b99b206SAugustin Cavalier setcommandmode();
159*6b99b206SAugustin Cavalier NetClose(net);
160*6b99b206SAugustin Cavalier fprintf(stderr, "Write error on local output.\n");
161*6b99b206SAugustin Cavalier exit(1);
162*6b99b206SAugustin Cavalier }
163*6b99b206SAugustin Cavalier return -1;
164*6b99b206SAugustin Cavalier }
1658d5a6048SAxel Dörfler if (n == n0) {
1668d5a6048SAxel Dörfler if (n0)
1678d5a6048SAxel Dörfler return -1;
1688d5a6048SAxel Dörfler return 0;
1698d5a6048SAxel Dörfler }
1708d5a6048SAxel Dörfler return n0 - n + 1;
1718d5a6048SAxel Dörfler }
1728d5a6048SAxel Dörfler
1738d5a6048SAxel Dörfler
1748d5a6048SAxel Dörfler /*
1758d5a6048SAxel Dörfler * These routines decides on what the mode should be (based on the values
1768d5a6048SAxel Dörfler * of various global variables).
1778d5a6048SAxel Dörfler */
1788d5a6048SAxel Dörfler
1798d5a6048SAxel Dörfler
1808d5a6048SAxel Dörfler int
getconnmode(void)1818d5a6048SAxel Dörfler getconnmode(void)
1828d5a6048SAxel Dörfler {
1838d5a6048SAxel Dörfler extern int linemode;
1848d5a6048SAxel Dörfler int mode = 0;
1858d5a6048SAxel Dörfler #ifdef KLUDGELINEMODE
1868d5a6048SAxel Dörfler extern int kludgelinemode;
1878d5a6048SAxel Dörfler #endif
1888d5a6048SAxel Dörfler
1898d5a6048SAxel Dörfler if (my_want_state_is_dont(TELOPT_ECHO))
1908d5a6048SAxel Dörfler mode |= MODE_ECHO;
1918d5a6048SAxel Dörfler
1928d5a6048SAxel Dörfler if (localflow)
1938d5a6048SAxel Dörfler mode |= MODE_FLOW;
1948d5a6048SAxel Dörfler
1958d5a6048SAxel Dörfler if (my_want_state_is_will(TELOPT_BINARY))
1968d5a6048SAxel Dörfler mode |= MODE_INBIN;
1978d5a6048SAxel Dörfler
1988d5a6048SAxel Dörfler if (his_want_state_is_will(TELOPT_BINARY))
1998d5a6048SAxel Dörfler mode |= MODE_OUTBIN;
2008d5a6048SAxel Dörfler
2018d5a6048SAxel Dörfler #ifdef KLUDGELINEMODE
2028d5a6048SAxel Dörfler if (kludgelinemode) {
2038d5a6048SAxel Dörfler if (my_want_state_is_dont(TELOPT_SGA)) {
2048d5a6048SAxel Dörfler mode |= (MODE_TRAPSIG|MODE_EDIT);
2058d5a6048SAxel Dörfler if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
2068d5a6048SAxel Dörfler mode &= ~MODE_ECHO;
2078d5a6048SAxel Dörfler }
2088d5a6048SAxel Dörfler }
2098d5a6048SAxel Dörfler return(mode);
2108d5a6048SAxel Dörfler }
2118d5a6048SAxel Dörfler #endif
2128d5a6048SAxel Dörfler if (my_want_state_is_will(TELOPT_LINEMODE))
2138d5a6048SAxel Dörfler mode |= linemode;
2148d5a6048SAxel Dörfler return(mode);
2158d5a6048SAxel Dörfler }
2168d5a6048SAxel Dörfler
2178d5a6048SAxel Dörfler void
setconnmode(int force)2188d5a6048SAxel Dörfler setconnmode(int force)
2198d5a6048SAxel Dörfler {
2208d5a6048SAxel Dörfler #ifdef ENCRYPTION
2218d5a6048SAxel Dörfler static int enc_passwd = 0;
2228d5a6048SAxel Dörfler #endif /* ENCRYPTION */
2238d5a6048SAxel Dörfler int newmode;
2248d5a6048SAxel Dörfler
2258d5a6048SAxel Dörfler newmode = getconnmode()|(force?MODE_FORCE:0);
2268d5a6048SAxel Dörfler
2278d5a6048SAxel Dörfler TerminalNewMode(newmode);
2288d5a6048SAxel Dörfler
2298d5a6048SAxel Dörfler #ifdef ENCRYPTION
2308d5a6048SAxel Dörfler if ((newmode & (MODE_ECHO|MODE_EDIT)) == MODE_EDIT) {
2318d5a6048SAxel Dörfler if (my_want_state_is_will(TELOPT_ENCRYPT)
2328d5a6048SAxel Dörfler && (enc_passwd == 0) && !encrypt_output) {
2338d5a6048SAxel Dörfler encrypt_request_start(0, 0);
2348d5a6048SAxel Dörfler enc_passwd = 1;
2358d5a6048SAxel Dörfler }
2368d5a6048SAxel Dörfler } else {
2378d5a6048SAxel Dörfler if (enc_passwd) {
2388d5a6048SAxel Dörfler encrypt_request_end();
2398d5a6048SAxel Dörfler enc_passwd = 0;
2408d5a6048SAxel Dörfler }
2418d5a6048SAxel Dörfler }
2428d5a6048SAxel Dörfler #endif /* ENCRYPTION */
2438d5a6048SAxel Dörfler
2448d5a6048SAxel Dörfler }
2458d5a6048SAxel Dörfler
2468d5a6048SAxel Dörfler void
setcommandmode(void)2478d5a6048SAxel Dörfler setcommandmode(void)
2488d5a6048SAxel Dörfler {
2498d5a6048SAxel Dörfler TerminalNewMode(-1);
2508d5a6048SAxel Dörfler }
251