GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/telnet/terminal.c Lines: 0 51 0.0 %
Date: 2016-12-06 Branches: 0 48 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: terminal.c,v 1.13 2014/07/22 07:30:24 jsg Exp $	*/
2
/*	$NetBSD: terminal.c,v 1.5 1996/02/28 21:04:17 thorpej Exp $	*/
3
4
/*
5
 * Copyright (c) 1988, 1990, 1993
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include "telnet_locl.h"
34
35
#include <arpa/telnet.h>
36
#include <errno.h>
37
#include <unistd.h>
38
39
Ring		ttyoring, ttyiring;
40
unsigned char	ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ];
41
42
int termdata;			/* Debugging flag */
43
44
# ifndef VDISCARD
45
cc_t termFlushChar;
46
# endif
47
# ifndef VLNEXT
48
cc_t termLiteralNextChar;
49
# endif
50
# ifndef VWERASE
51
cc_t termWerasChar;
52
# endif
53
# ifndef VREPRINT
54
cc_t termRprntChar;
55
# endif
56
# ifndef VSTART
57
cc_t termStartChar;
58
# endif
59
# ifndef VSTOP
60
cc_t termStopChar;
61
# endif
62
# ifndef VEOL
63
cc_t termForw1Char;
64
# endif
65
# ifndef VEOL2
66
cc_t termForw2Char;
67
# endif
68
# ifndef VSTATUS
69
cc_t termAytChar;
70
# endif
71
72
/*
73
 * initialize the terminal data structures.
74
 */
75
76
void
77
init_terminal(void)
78
{
79
	struct termios tc;
80
81
	ring_init(&ttyoring, ttyobuf, sizeof ttyobuf);
82
	ring_init(&ttyiring, ttyibuf, sizeof ttyibuf);
83
84
	tcgetattr(0, &tc);
85
	autoflush = (tc.c_lflag & NOFLSH) == 0;
86
}
87
88
/*
89
 *		Send as much data as possible to the terminal.
90
 *
91
 *		Return value:
92
 *			-1: No useful work done, data waiting to go out.
93
 *			 0: No data was waiting, so nothing was done.
94
 *			 1: All waiting data was written out.
95
 *			 n: All data - n was written out.
96
 */
97
98
int
99
ttyflush(int drop)
100
{
101
    int n, n0, n1;
102
103
    n0 = ring_full_count(&ttyoring);
104
    if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
105
	if (drop) {
106
	    tcflush(fileno(stdout), TCOFLUSH);
107
	    /* we leave 'n' alone! */
108
	} else {
109
	    n = write(tout, ttyoring.consume, n);
110
	}
111
    }
112
    if (n > 0) {
113
	if (termdata && n) {
114
	    Dump('>', ttyoring.consume, n);
115
	}
116
	/*
117
	 * If we wrote everything, and the full count is
118
	 * larger than what we wrote, then write the
119
	 * rest of the buffer.
120
	 */
121
	if (n1 == n && n0 > n) {
122
		n1 = n0 - n;
123
		if (!drop)
124
			n1 = write(tout, ttyoring.bottom, n1);
125
		if (n1 > 0)
126
			n += n1;
127
	}
128
	ring_consumed(&ttyoring, n);
129
    }
130
    if (n < 0) {
131
	if (errno == EPIPE)
132
		kill(0, SIGQUIT);
133
	return -1;
134
    }
135
    if (n == n0) {
136
	if (n0)
137
	    return -1;
138
	return 0;
139
    }
140
    return n0 - n + 1;
141
}
142
143
144
/*
145
 * These routines decides on what the mode should be (based on the values
146
 * of various global variables).
147
 */
148
149
int
150
getconnmode(void)
151
{
152
    int mode = 0;
153
154
    if (my_want_state_is_dont(TELOPT_ECHO))
155
	mode |= MODE_ECHO;
156
157
    if (localflow)
158
	mode |= MODE_FLOW;
159
160
    if ((eight & 1) || my_want_state_is_will(TELOPT_BINARY))
161
	mode |= MODE_INBIN;
162
163
    if (eight & 2)
164
	mode |= MODE_OUT8;
165
    if (his_want_state_is_will(TELOPT_BINARY))
166
	mode |= MODE_OUTBIN;
167
168
#ifdef	KLUDGELINEMODE
169
    if (kludgelinemode) {
170
	if (my_want_state_is_dont(TELOPT_SGA)) {
171
	    mode |= (MODE_TRAPSIG|MODE_EDIT);
172
	    if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
173
		mode &= ~MODE_ECHO;
174
	    }
175
	}
176
	return(mode);
177
    }
178
#endif
179
    if (my_want_state_is_will(TELOPT_LINEMODE))
180
	mode |= linemode;
181
    return(mode);
182
}
183
184
void
185
setconnmode(int force)
186
{
187
    int newmode;
188
189
    newmode = getconnmode()|(force?MODE_FORCE:0);
190
191
    TerminalNewMode(newmode);
192
}
193
194
void
195
setcommandmode(void)
196
{
197
    TerminalNewMode(-1);
198
}