xref: /original-bsd/usr.bin/telnet/terminal.c (revision 22c2ba50)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)terminal.c	1.12 (Berkeley) 06/27/88";
15 #endif /* not lint */
16 
17 #include <arpa/telnet.h>
18 #include <sys/types.h>
19 
20 #include "ring.h"
21 
22 #include "externs.h"
23 #include "types.h"
24 
25 Ring	ttyoring, ttyiring;
26 char	ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ];
27 
28 char
29     termEofChar,
30     termEraseChar,
31     termFlushChar,
32     termIntChar,
33     termKillChar,
34 #if	defined(MSDOS)
35     termLiteralNextChar,
36 #endif	/* defined(MSDOS) */
37     termQuitChar;
38 
39 /*
40  * initialize the terminal data structures.
41  */
42 
43 init_terminal()
44 {
45     if (ring_init(&ttyoring, ttyobuf, sizeof ttyobuf) != 1) {
46 	exit(1);
47     }
48     if (ring_init(&ttyiring, ttyibuf, sizeof ttyibuf) != 1) {
49 	exit(1);
50     }
51     autoflush = TerminalAutoFlush();
52 }
53 
54 
55 /*
56  *		Send as much data as possible to the terminal.
57  *
58  *		The return value indicates whether we did any
59  *	useful work.
60  */
61 
62 
63 int
64 ttyflush(drop)
65 int drop;
66 {
67     register int n, n0, n1;
68 
69     n0 = ring_full_count(&ttyoring);
70     if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
71 	if (drop) {
72 	    TerminalFlushOutput();
73 	    /* we leave 'n' alone! */
74 	} else {
75 	    n = TerminalWrite(ttyoring.consume, n);
76 	}
77     }
78     if (n > 0) {
79 	/*
80 	 * If we wrote everything, and the full count is
81 	 * larger than what we wrote, then write the
82 	 * rest of the buffer.
83 	 */
84 	if (n1 == n && n0 > n) {
85 		n1 = n0 - n;
86 		if (!drop)
87 			n1 = TerminalWrite(ttyoring.bottom, n1);
88 		n += n1;
89 	}
90 	ring_consumed(&ttyoring, n);
91     }
92     return n > 0;
93 }
94 
95 
96 /*
97  * These routines decides on what the mode should be (based on the values
98  * of various global variables).
99  */
100 
101 
102 int
103 getconnmode()
104 {
105     static char newmode[16] =
106 			{ 4, 5, 3, 3, 2, 2, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6 };
107     int modeindex = 0;
108 
109     if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
110 	modeindex += 1;
111     }
112     if (hisopts[TELOPT_ECHO]) {
113 	modeindex += 2;
114     }
115     if (hisopts[TELOPT_SGA]) {
116 	modeindex += 4;
117     }
118     if (In3270) {
119 	modeindex += 8;
120     }
121     return newmode[modeindex];
122 }
123 
124 void
125 setconnmode()
126 {
127     TerminalNewMode(getconnmode());
128 }
129 
130 
131 void
132 setcommandmode()
133 {
134     TerminalNewMode(0);
135 }
136