GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/telnet/terminal.c Lines: 0 52 0.0 %
Date: 2017-11-13 Branches: 0 48 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: terminal.c,v 1.14 2017/07/07 09:14:26 fcambus 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
/*
45
 * initialize the terminal data structures.
46
 */
47
48
void
49
init_terminal(void)
50
{
51
	struct termios tc;
52
53
	ring_init(&ttyoring, ttyobuf, sizeof ttyobuf);
54
	ring_init(&ttyiring, ttyibuf, sizeof ttyibuf);
55
56
	tcgetattr(0, &tc);
57
	autoflush = (tc.c_lflag & NOFLSH) == 0;
58
}
59
60
/*
61
 *		Send as much data as possible to the terminal.
62
 *
63
 *		Return value:
64
 *			-1: No useful work done, data waiting to go out.
65
 *			 0: No data was waiting, so nothing was done.
66
 *			 1: All waiting data was written out.
67
 *			 n: All data - n was written out.
68
 */
69
70
int
71
ttyflush(int drop)
72
{
73
    int n, n0, n1;
74
75
    n0 = ring_full_count(&ttyoring);
76
    if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
77
	if (drop) {
78
	    tcflush(fileno(stdout), TCOFLUSH);
79
	    /* we leave 'n' alone! */
80
	} else {
81
	    n = write(tout, ttyoring.consume, n);
82
	}
83
    }
84
    if (n > 0) {
85
	if (termdata && n) {
86
	    Dump('>', ttyoring.consume, n);
87
	}
88
	/*
89
	 * If we wrote everything, and the full count is
90
	 * larger than what we wrote, then write the
91
	 * rest of the buffer.
92
	 */
93
	if (n1 == n && n0 > n) {
94
		n1 = n0 - n;
95
		if (!drop)
96
			n1 = write(tout, ttyoring.bottom, n1);
97
		if (n1 > 0)
98
			n += n1;
99
	}
100
	ring_consumed(&ttyoring, n);
101
    }
102
    if (n < 0) {
103
	if (errno == EPIPE)
104
		kill(0, SIGQUIT);
105
	return -1;
106
    }
107
    if (n == n0) {
108
	if (n0)
109
	    return -1;
110
	return 0;
111
    }
112
    return n0 - n + 1;
113
}
114
115
116
/*
117
 * These routines decides on what the mode should be (based on the values
118
 * of various global variables).
119
 */
120
121
int
122
getconnmode(void)
123
{
124
    int mode = 0;
125
126
    if (my_want_state_is_dont(TELOPT_ECHO))
127
	mode |= MODE_ECHO;
128
129
    if (localflow)
130
	mode |= MODE_FLOW;
131
132
    if ((eight & 1) || my_want_state_is_will(TELOPT_BINARY))
133
	mode |= MODE_INBIN;
134
135
    if (eight & 2)
136
	mode |= MODE_OUT8;
137
    if (his_want_state_is_will(TELOPT_BINARY))
138
	mode |= MODE_OUTBIN;
139
140
#ifdef	KLUDGELINEMODE
141
    if (kludgelinemode) {
142
	if (my_want_state_is_dont(TELOPT_SGA)) {
143
	    mode |= (MODE_TRAPSIG|MODE_EDIT);
144
	    if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
145
		mode &= ~MODE_ECHO;
146
	    }
147
	}
148
	return(mode);
149
    }
150
#endif
151
    if (my_want_state_is_will(TELOPT_LINEMODE))
152
	mode |= linemode;
153
    return(mode);
154
}
155
156
void
157
setconnmode(int force)
158
{
159
    int newmode;
160
161
    newmode = getconnmode()|(force?MODE_FORCE:0);
162
163
    TerminalNewMode(newmode);
164
}
165
166
void
167
setcommandmode(void)
168
{
169
    TerminalNewMode(-1);
170
}