xref: /illumos-gate/usr/src/lib/libnsl/dial/line.c (revision d9c3e05c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5de81e71eSTim Marsland  * Common Development and Distribution License (the "License").
6de81e71eSTim Marsland  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
2261961e0fSrobinson /*
23de81e71eSTim Marsland  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2461961e0fSrobinson  * Use is subject to license terms.
2561961e0fSrobinson  */
2661961e0fSrobinson 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
3061961e0fSrobinson /*
3161961e0fSrobinson  * This is a new line.c, which consists of line.c and culine.c
327c478bd9Sstevel@tonic-gate  * merged together.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
35e8031f0aSraf #include "mt.h"
367c478bd9Sstevel@tonic-gate #include "uucp.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static const struct sg_spds {
39*d9c3e05cSJoshua M. Clulow 	int sp_val;
40*d9c3e05cSJoshua M. Clulow 	int sp_name;
417c478bd9Sstevel@tonic-gate } spds[] = {
42*d9c3e05cSJoshua M. Clulow 	{ 50,		B50 },
43*d9c3e05cSJoshua M. Clulow 	{ 75,		B75 },
44*d9c3e05cSJoshua M. Clulow 	{ 110,		B110 },
45*d9c3e05cSJoshua M. Clulow 	{ 134,		B134 },
46*d9c3e05cSJoshua M. Clulow 	{ 150,		B150 },
47*d9c3e05cSJoshua M. Clulow 	{ 200,		B200 },
48*d9c3e05cSJoshua M. Clulow 	{ 300,		B300 },
49*d9c3e05cSJoshua M. Clulow 	{ 600,		B600 },
50*d9c3e05cSJoshua M. Clulow 	{ 1200,		B1200 },
51*d9c3e05cSJoshua M. Clulow 	{ 1800,		B1800 },
52*d9c3e05cSJoshua M. Clulow 	{ 2400,		B2400 },
53*d9c3e05cSJoshua M. Clulow 	{ 4800,		B4800 },
54*d9c3e05cSJoshua M. Clulow 	{ 9600,		B9600 },
55*d9c3e05cSJoshua M. Clulow 	{ 19200,	B19200 },
56*d9c3e05cSJoshua M. Clulow 	{ 38400,	B38400 },
57*d9c3e05cSJoshua M. Clulow 	{ 57600,	B57600 },
58*d9c3e05cSJoshua M. Clulow 	{ 76800,	B76800 },
59*d9c3e05cSJoshua M. Clulow 	{ 115200,	B115200 },
60*d9c3e05cSJoshua M. Clulow 	{ 153600,	B153600 },
61*d9c3e05cSJoshua M. Clulow 	{ 230400,	B230400 },
62*d9c3e05cSJoshua M. Clulow 	{ 307200,	B307200 },
63*d9c3e05cSJoshua M. Clulow 	{ 460800,	B460800 },
64*d9c3e05cSJoshua M. Clulow 	{ 921600,	B921600 },
65*d9c3e05cSJoshua M. Clulow 	{ 1000000,	B1000000 },
66*d9c3e05cSJoshua M. Clulow 	{ 1152000,	B1152000 },
67*d9c3e05cSJoshua M. Clulow 	{ 1500000,	B1500000 },
68*d9c3e05cSJoshua M. Clulow 	{ 2000000,	B2000000 },
69*d9c3e05cSJoshua M. Clulow 	{ 2500000,	B2500000 },
70*d9c3e05cSJoshua M. Clulow 	{ 3000000,	B3000000 },
71*d9c3e05cSJoshua M. Clulow 	{ 3500000,	B3500000 },
72*d9c3e05cSJoshua M. Clulow 	{ 4000000,	B4000000 },
73*d9c3e05cSJoshua M. Clulow 	{ 0,		0}
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
7661961e0fSrobinson #define	HEADERSIZE	6
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static int Saved_line;		/* was savline() successful?	*/
797c478bd9Sstevel@tonic-gate static int Saved_termios;	/* was termios saved?	*/
8061961e0fSrobinson static int
817c478bd9Sstevel@tonic-gate 	Oddflag,	/* Default is no parity */
827c478bd9Sstevel@tonic-gate 	Evenflag,	/* Default is no parity */
837c478bd9Sstevel@tonic-gate 	Duplex = 1,	/* Default is full duplex */
847c478bd9Sstevel@tonic-gate 	Terminal,	/* Default is no terminal */
857c478bd9Sstevel@tonic-gate 	line_8bit = -1;	/* Default is same as terminal */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static const char P_PARITY[] = "Parity option error\r\n";
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static struct termio Savettyb;
907c478bd9Sstevel@tonic-gate static struct termios Savettybs;
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * set speed/echo/mode...
937c478bd9Sstevel@tonic-gate  *	tty 	-> terminal name
947c478bd9Sstevel@tonic-gate  *	spwant 	-> speed
957c478bd9Sstevel@tonic-gate  *	type	-> type
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *	if spwant == 0, speed is untouched
987c478bd9Sstevel@tonic-gate  *	type is unused, but needed for compatibility
997c478bd9Sstevel@tonic-gate  *
10061961e0fSrobinson  * return:
1017c478bd9Sstevel@tonic-gate  *	none
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10461961e0fSrobinson static void
fixline(int tty,int spwant,int type)10561961e0fSrobinson fixline(int tty, int spwant, int type)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	register const struct sg_spds	*ps;
1087c478bd9Sstevel@tonic-gate 	struct termio		ttbuf;
1097c478bd9Sstevel@tonic-gate 	struct termios		ttbufs;
1107c478bd9Sstevel@tonic-gate 	int			speed = -1;
1117c478bd9Sstevel@tonic-gate 	int			i, istermios, ospeed;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	DEBUG(6, "fixline(%d, ", tty);
1147c478bd9Sstevel@tonic-gate 	DEBUG(6, "%d)\n", spwant);
1157c478bd9Sstevel@tonic-gate 	if ((istermios = (*Ioctl)(tty, TCGETS, &ttbufs)) < 0) {
11661961e0fSrobinson 		if ((*Ioctl)(tty, TCGETA, &ttbuf) != 0)
11761961e0fSrobinson 			return;
1187c478bd9Sstevel@tonic-gate 		ttbufs.c_lflag = ttbuf.c_lflag;
1197c478bd9Sstevel@tonic-gate 		ttbufs.c_oflag = ttbuf.c_oflag;
1207c478bd9Sstevel@tonic-gate 		ttbufs.c_iflag = ttbuf.c_iflag;
1217c478bd9Sstevel@tonic-gate 		ttbufs.c_cflag = ttbuf.c_cflag;
12261961e0fSrobinson 		for (i = 0; i < NCC; i++)
1237c478bd9Sstevel@tonic-gate 			ttbufs.c_cc[i] = ttbuf.c_cc[i];
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	if (spwant > 0) {
126*d9c3e05cSJoshua M. Clulow 		for (ps = spds; ps->sp_val != 0; ps++)
1277c478bd9Sstevel@tonic-gate 			if (ps->sp_val == spwant) {
1287c478bd9Sstevel@tonic-gate 				speed = ps->sp_name;
1297c478bd9Sstevel@tonic-gate 				break;
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 		if (speed < 0) {
1327c478bd9Sstevel@tonic-gate 			/*EMPTY*/
13361961e0fSrobinson 			DEBUG(5, "speed (%d) not supported\n", spwant);
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 		ASSERT(speed >= 0, "BAD SPEED", "", spwant);
1367c478bd9Sstevel@tonic-gate 		ttbufs.c_cflag &= 0xffff0000;
13761961e0fSrobinson 		(void) cfsetospeed(&ttbufs, speed);
1387c478bd9Sstevel@tonic-gate 	} else { /* determine the current speed setting */
1397c478bd9Sstevel@tonic-gate 		ospeed = cfgetospeed(&ttbufs);
1407c478bd9Sstevel@tonic-gate 		ttbufs.c_cflag &= 0xffff0000;
14161961e0fSrobinson 		(void) cfsetospeed(&ttbufs, ospeed);
142*d9c3e05cSJoshua M. Clulow 		for (ps = spds; ps->sp_val != 0; ps++)
1437c478bd9Sstevel@tonic-gate 			if (ps->sp_name == ospeed) {
1447c478bd9Sstevel@tonic-gate 				spwant = ps->sp_val;
1457c478bd9Sstevel@tonic-gate 				break;
1467c478bd9Sstevel@tonic-gate 			}
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	ttbufs.c_iflag &= 0xffff0000;
1497c478bd9Sstevel@tonic-gate 	ttbufs.c_oflag &= 0xffff0000;
1507c478bd9Sstevel@tonic-gate 	ttbufs.c_lflag &= 0xffff0000;
1517c478bd9Sstevel@tonic-gate 
15261961e0fSrobinson 	ttbufs.c_cflag &= ~CLOCAL;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (EQUALS(Progname, "cu")) {
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		/* set attributes associated with -h, -t, -e, and -o options */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		ttbufs.c_iflag = (IGNPAR | IGNBRK | IXON | IXOFF);
1597c478bd9Sstevel@tonic-gate 		if (line_8bit) {
16061961e0fSrobinson 			ttbufs.c_cflag |= CS8;
16161961e0fSrobinson 			ttbufs.c_iflag &= ~ISTRIP;
1627c478bd9Sstevel@tonic-gate 		} else {
16361961e0fSrobinson 			ttbufs.c_cflag |= CS7;
16461961e0fSrobinson 			ttbufs.c_iflag |= ISTRIP;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		ttbufs.c_cc[VEOF] = '\1';
1687c478bd9Sstevel@tonic-gate 		ttbufs.c_cflag |= (CREAD | (speed ? HUPCL : 0));
1697c478bd9Sstevel@tonic-gate 
17061961e0fSrobinson 		if (Evenflag) {				/* even parity -e */
17161961e0fSrobinson 			if (ttbufs.c_cflag & PARENB) {
1727c478bd9Sstevel@tonic-gate 				VERBOSE(P_PARITY, 0);
17361961e0fSrobinson 				exit(1);
17461961e0fSrobinson 			}
17561961e0fSrobinson 			ttbufs.c_cflag |= PARENB;
17661961e0fSrobinson 		} else if (Oddflag) {			/* odd parity -o */
17761961e0fSrobinson 			if (ttbufs.c_cflag & PARENB) {
1787c478bd9Sstevel@tonic-gate 				VERBOSE(P_PARITY, 0);
17961961e0fSrobinson 				exit(1);
18061961e0fSrobinson 			}
18161961e0fSrobinson 			ttbufs.c_cflag |= PARODD;
18261961e0fSrobinson 			ttbufs.c_cflag |= PARENB;
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 
18561961e0fSrobinson 		if (!Duplex)				/* half duplex -h */
18661961e0fSrobinson 			ttbufs.c_iflag &= ~(IXON | IXOFF);
1877c478bd9Sstevel@tonic-gate 		if (Terminal)				/* -t */
18861961e0fSrobinson 			ttbufs.c_oflag |= (OPOST | ONLCR);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	} else { /* non-cu */
1917c478bd9Sstevel@tonic-gate 		ttbufs.c_cflag |= (CS8 | CREAD | (speed ? HUPCL : 0));
1927c478bd9Sstevel@tonic-gate 		ttbufs.c_cc[VMIN] = HEADERSIZE;
1937c478bd9Sstevel@tonic-gate 		ttbufs.c_cc[VTIME] = 1;
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (istermios < 0) {
1977c478bd9Sstevel@tonic-gate 		ttbuf.c_lflag = ttbufs.c_lflag;
1987c478bd9Sstevel@tonic-gate 		ttbuf.c_oflag = ttbufs.c_oflag;
1997c478bd9Sstevel@tonic-gate 		ttbuf.c_iflag = ttbufs.c_iflag;
2007c478bd9Sstevel@tonic-gate 		ttbuf.c_cflag = ttbufs.c_cflag;
20161961e0fSrobinson 		for (i = 0; i < NCC; i++)
2027c478bd9Sstevel@tonic-gate 			ttbuf.c_cc[i] = ttbufs.c_cc[i];
2037c478bd9Sstevel@tonic-gate 		ASSERT((*Ioctl)(tty, TCSETAW, &ttbuf) >= 0,
204de81e71eSTim Marsland 		    "RETURN FROM fixline ioctl", "", errno);
2057c478bd9Sstevel@tonic-gate 	} else {
2067c478bd9Sstevel@tonic-gate 		ASSERT((*Ioctl)(tty, TCSETSW, &ttbufs) >= 0,
207de81e71eSTim Marsland 		    "RETURN FROM fixline ioctl", "", errno);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
21161961e0fSrobinson static void
sethup(int dcf)21261961e0fSrobinson sethup(int dcf)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	struct termio ttbuf;
2157c478bd9Sstevel@tonic-gate 
21661961e0fSrobinson 	if ((*Ioctl)(dcf, TCGETA, &ttbuf) != 0)
2177c478bd9Sstevel@tonic-gate 		return;
2187c478bd9Sstevel@tonic-gate 	if (!(ttbuf.c_cflag & HUPCL)) {
2197c478bd9Sstevel@tonic-gate 		ttbuf.c_cflag |= HUPCL;
2207c478bd9Sstevel@tonic-gate 		(void) (*Ioctl)(dcf, TCSETAW, &ttbuf);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
22461961e0fSrobinson static void
ttygenbrk(int fn)22561961e0fSrobinson ttygenbrk(int fn)
2267c478bd9Sstevel@tonic-gate {
22761961e0fSrobinson 	if (isatty(fn))
2287c478bd9Sstevel@tonic-gate 		(void) (*Ioctl)(fn, TCSBRK, 0);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
23161961e0fSrobinson static int
savline(void)23261961e0fSrobinson savline(void)
2337c478bd9Sstevel@tonic-gate {
23461961e0fSrobinson 	if ((Saved_termios = (*Ioctl)(0, TCGETS, &Savettybs)) < 0) {
23561961e0fSrobinson 		if ((*Ioctl)(0, TCGETA, &Savettyb) != 0) {
23661961e0fSrobinson 			Saved_line = FALSE;
2377c478bd9Sstevel@tonic-gate 		} else {
23861961e0fSrobinson 			Saved_line = TRUE;
23961961e0fSrobinson 			Savettyb.c_cflag = (Savettyb.c_cflag & ~CS8) | CS7;
24061961e0fSrobinson 			Savettyb.c_oflag |= OPOST;
24161961e0fSrobinson 			Savettyb.c_lflag |= (ISIG|ICANON|ECHO);
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	} else {
2447c478bd9Sstevel@tonic-gate 		Saved_line = TRUE;
2457c478bd9Sstevel@tonic-gate 		Savettybs.c_cflag = (Savettybs.c_cflag & ~CS8) | CS7;
2467c478bd9Sstevel@tonic-gate 		Savettybs.c_oflag |= OPOST;
2477c478bd9Sstevel@tonic-gate 		Savettybs.c_lflag |= (ISIG|ICANON|ECHO);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	return (0);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
25261961e0fSrobinson static int
restline(void)25361961e0fSrobinson restline(void)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	if (Saved_line == TRUE) {
2567c478bd9Sstevel@tonic-gate 		if (Saved_termios < 0)
2577c478bd9Sstevel@tonic-gate 			return ((*Ioctl)(0, TCSETAW, &Savettyb));
2587c478bd9Sstevel@tonic-gate 		else
2597c478bd9Sstevel@tonic-gate 			return ((*Ioctl)(0, TCSETSW, &Savettybs));
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	return (0);
2627c478bd9Sstevel@tonic-gate }
263