xref: /titanic_51/usr/src/lib/libnsl/dial/line.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 /*
34  * This is a new line.c, which consists of line.c and culine.c
35  * merged together.
36  */
37 
38 #include "uucp.h"
39 
40 static const struct sg_spds {
41 	int	sp_val,
42 		sp_name;
43 } spds[] = {
44 	{  50,   B50},
45 	{  75,   B75},
46 	{ 110,  B110},
47 	{ 134,  B134},
48 	{ 150,  B150},
49 	{ 200,  B200},
50 	{ 300,  B300},
51 	{ 600,  B600},
52 	{1200, B1200},
53 	{1800, B1800},
54 	{2400, B2400},
55 	{4800, B4800},
56 	{9600, B9600},
57 #ifdef EXTA
58 	{19200,	EXTA},
59 #endif
60 #ifdef B19200
61 	{19200,	B19200},
62 #endif
63 #ifdef B38400
64 	{38400,	B38400},
65 #endif
66 	{57600, B57600},
67 	{76800, B76800},
68 	{115200, B115200},
69 	{153600, B153600},
70 	{230400, B230400},
71 	{307200, B307200},
72 	{460800, B460800},
73 	{0,    0}
74 };
75 
76 #define	PACKSIZE	64
77 #define	HEADERSIZE	6
78 
79 #define	SNDFILE	'S'
80 #define	RCVFILE 'R'
81 #define	RESET	'X'
82 
83 static int Saved_line;		/* was savline() successful?	*/
84 static int Saved_termios;	/* was termios saved?	*/
85 static int
86 	Oddflag,	/* Default is no parity */
87 	Evenflag,	/* Default is no parity */
88 	Duplex = 1,	/* Default is full duplex */
89 	Terminal,	/* Default is no terminal */
90 	line_8bit = -1;	/* Default is same as terminal */
91 
92 static const char P_PARITY[] = "Parity option error\r\n";
93 
94 static struct termio Savettyb;
95 static struct termios Savettybs;
96 /*
97  * set speed/echo/mode...
98  *	tty 	-> terminal name
99  *	spwant 	-> speed
100  *	type	-> type
101  *
102  *	if spwant == 0, speed is untouched
103  *	type is unused, but needed for compatibility
104  *
105  * return:
106  *	none
107  */
108 /*ARGSUSED*/
109 static void
110 fixline(int tty, int spwant, int type)
111 {
112 	register const struct sg_spds	*ps;
113 	struct termio		ttbuf;
114 	struct termios		ttbufs;
115 	int			speed = -1;
116 	int			i, istermios, ospeed;
117 
118 	DEBUG(6, "fixline(%d, ", tty);
119 	DEBUG(6, "%d)\n", spwant);
120 	if ((istermios = (*Ioctl)(tty, TCGETS, &ttbufs)) < 0) {
121 		if ((*Ioctl)(tty, TCGETA, &ttbuf) != 0)
122 			return;
123 		ttbufs.c_lflag = ttbuf.c_lflag;
124 		ttbufs.c_oflag = ttbuf.c_oflag;
125 		ttbufs.c_iflag = ttbuf.c_iflag;
126 		ttbufs.c_cflag = ttbuf.c_cflag;
127 		for (i = 0; i < NCC; i++)
128 			ttbufs.c_cc[i] = ttbuf.c_cc[i];
129 	}
130 	if (spwant > 0) {
131 		for (ps = spds; ps->sp_val; ps++)
132 			if (ps->sp_val == spwant) {
133 				speed = ps->sp_name;
134 				break;
135 			}
136 		if (speed < 0) {
137 			/*EMPTY*/
138 			DEBUG(5, "speed (%d) not supported\n", spwant);
139 		}
140 		ASSERT(speed >= 0, "BAD SPEED", "", spwant);
141 		ttbufs.c_cflag &= 0xffff0000;
142 		(void) cfsetospeed(&ttbufs, speed);
143 	} else { /* determine the current speed setting */
144 		ospeed = cfgetospeed(&ttbufs);
145 		ttbufs.c_cflag &= 0xffff0000;
146 		(void) cfsetospeed(&ttbufs, ospeed);
147 		for (ps = spds; ps->sp_val; ps++)
148 			if (ps->sp_name == ospeed) {
149 				spwant = ps->sp_val;
150 				break;
151 			}
152 	}
153 	ttbufs.c_iflag &= 0xffff0000;
154 	ttbufs.c_oflag &= 0xffff0000;
155 	ttbufs.c_lflag &= 0xffff0000;
156 
157 	ttbufs.c_cflag &= ~CLOCAL;
158 
159 	if (EQUALS(Progname, "cu")) {
160 
161 		/* set attributes associated with -h, -t, -e, and -o options */
162 
163 		ttbufs.c_iflag = (IGNPAR | IGNBRK | IXON | IXOFF);
164 		if (line_8bit) {
165 			ttbufs.c_cflag |= CS8;
166 			ttbufs.c_iflag &= ~ISTRIP;
167 		} else {
168 			ttbufs.c_cflag |= CS7;
169 			ttbufs.c_iflag |= ISTRIP;
170 		}
171 
172 		ttbufs.c_cc[VEOF] = '\1';
173 		ttbufs.c_cflag |= (CREAD | (speed ? HUPCL : 0));
174 
175 		if (Evenflag) {				/* even parity -e */
176 			if (ttbufs.c_cflag & PARENB) {
177 				VERBOSE(P_PARITY, 0);
178 				exit(1);
179 			}
180 			ttbufs.c_cflag |= PARENB;
181 		} else if (Oddflag) {			/* odd parity -o */
182 			if (ttbufs.c_cflag & PARENB) {
183 				VERBOSE(P_PARITY, 0);
184 				exit(1);
185 			}
186 			ttbufs.c_cflag |= PARODD;
187 			ttbufs.c_cflag |= PARENB;
188 		}
189 
190 		if (!Duplex)				/* half duplex -h */
191 			ttbufs.c_iflag &= ~(IXON | IXOFF);
192 		if (Terminal)				/* -t */
193 			ttbufs.c_oflag |= (OPOST | ONLCR);
194 
195 	} else { /* non-cu */
196 		ttbufs.c_cflag |= (CS8 | CREAD | (speed ? HUPCL : 0));
197 		ttbufs.c_cc[VMIN] = HEADERSIZE;
198 		ttbufs.c_cc[VTIME] = 1;
199 	}
200 
201 	if (istermios < 0) {
202 		ttbuf.c_lflag = ttbufs.c_lflag;
203 		ttbuf.c_oflag = ttbufs.c_oflag;
204 		ttbuf.c_iflag = ttbufs.c_iflag;
205 		ttbuf.c_cflag = ttbufs.c_cflag;
206 		for (i = 0; i < NCC; i++)
207 			ttbuf.c_cc[i] = ttbufs.c_cc[i];
208 		ASSERT((*Ioctl)(tty, TCSETAW, &ttbuf) >= 0,
209 			"RETURN FROM fixline ioctl", "", errno);
210 	} else {
211 		ASSERT((*Ioctl)(tty, TCSETSW, &ttbufs) >= 0,
212 			"RETURN FROM fixline ioctl", "", errno);
213 	}
214 }
215 
216 static void
217 sethup(int dcf)
218 {
219 	struct termio ttbuf;
220 
221 	if ((*Ioctl)(dcf, TCGETA, &ttbuf) != 0)
222 		return;
223 	if (!(ttbuf.c_cflag & HUPCL)) {
224 		ttbuf.c_cflag |= HUPCL;
225 		(void) (*Ioctl)(dcf, TCSETAW, &ttbuf);
226 	}
227 }
228 
229 static void
230 ttygenbrk(int fn)
231 {
232 	if (isatty(fn))
233 		(void) (*Ioctl)(fn, TCSBRK, 0);
234 }
235 
236 static int
237 savline(void)
238 {
239 	if ((Saved_termios = (*Ioctl)(0, TCGETS, &Savettybs)) < 0) {
240 		if ((*Ioctl)(0, TCGETA, &Savettyb) != 0) {
241 			Saved_line = FALSE;
242 		} else {
243 			Saved_line = TRUE;
244 			Savettyb.c_cflag = (Savettyb.c_cflag & ~CS8) | CS7;
245 			Savettyb.c_oflag |= OPOST;
246 			Savettyb.c_lflag |= (ISIG|ICANON|ECHO);
247 		}
248 	} else {
249 		Saved_line = TRUE;
250 		Savettybs.c_cflag = (Savettybs.c_cflag & ~CS8) | CS7;
251 		Savettybs.c_oflag |= OPOST;
252 		Savettybs.c_lflag |= (ISIG|ICANON|ECHO);
253 	}
254 	return (0);
255 }
256 
257 static int
258 restline(void)
259 {
260 	if (Saved_line == TRUE) {
261 		if (Saved_termios < 0)
262 			return ((*Ioctl)(0, TCSETAW, &Savettyb));
263 		else
264 			return ((*Ioctl)(0, TCSETSW, &Savettybs));
265 	}
266 	return (0);
267 }
268