xref: /titanic_54/usr/src/lib/libnsl/dial/line.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*61961e0fSrobinson 
23*61961e0fSrobinson /*
24*61961e0fSrobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*61961e0fSrobinson  * Use is subject to license terms.
26*61961e0fSrobinson  */
27*61961e0fSrobinson 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
31*61961e0fSrobinson #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
33*61961e0fSrobinson /*
34*61961e0fSrobinson  * This is a new line.c, which consists of line.c and culine.c
357c478bd9Sstevel@tonic-gate  * merged together.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "uucp.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static const struct sg_spds {
417c478bd9Sstevel@tonic-gate 	int	sp_val,
427c478bd9Sstevel@tonic-gate 		sp_name;
437c478bd9Sstevel@tonic-gate } spds[] = {
447c478bd9Sstevel@tonic-gate 	{  50,   B50},
457c478bd9Sstevel@tonic-gate 	{  75,   B75},
467c478bd9Sstevel@tonic-gate 	{ 110,  B110},
477c478bd9Sstevel@tonic-gate 	{ 134,  B134},
487c478bd9Sstevel@tonic-gate 	{ 150,  B150},
497c478bd9Sstevel@tonic-gate 	{ 200,  B200},
507c478bd9Sstevel@tonic-gate 	{ 300,  B300},
517c478bd9Sstevel@tonic-gate 	{ 600,  B600},
527c478bd9Sstevel@tonic-gate 	{1200, B1200},
537c478bd9Sstevel@tonic-gate 	{1800, B1800},
547c478bd9Sstevel@tonic-gate 	{2400, B2400},
557c478bd9Sstevel@tonic-gate 	{4800, B4800},
567c478bd9Sstevel@tonic-gate 	{9600, B9600},
577c478bd9Sstevel@tonic-gate #ifdef EXTA
587c478bd9Sstevel@tonic-gate 	{19200,	EXTA},
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate #ifdef B19200
617c478bd9Sstevel@tonic-gate 	{19200,	B19200},
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate #ifdef B38400
647c478bd9Sstevel@tonic-gate 	{38400,	B38400},
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 	{57600, B57600},
677c478bd9Sstevel@tonic-gate 	{76800, B76800},
687c478bd9Sstevel@tonic-gate 	{115200, B115200},
697c478bd9Sstevel@tonic-gate 	{153600, B153600},
707c478bd9Sstevel@tonic-gate 	{230400, B230400},
717c478bd9Sstevel@tonic-gate 	{307200, B307200},
727c478bd9Sstevel@tonic-gate 	{460800, B460800},
737c478bd9Sstevel@tonic-gate 	{0,    0}
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	PACKSIZE	64
777c478bd9Sstevel@tonic-gate #define	HEADERSIZE	6
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	SNDFILE	'S'
807c478bd9Sstevel@tonic-gate #define	RCVFILE 'R'
817c478bd9Sstevel@tonic-gate #define	RESET	'X'
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static int Saved_line;		/* was savline() successful?	*/
847c478bd9Sstevel@tonic-gate static int Saved_termios;	/* was termios saved?	*/
85*61961e0fSrobinson static int
867c478bd9Sstevel@tonic-gate 	Oddflag,	/* Default is no parity */
877c478bd9Sstevel@tonic-gate 	Evenflag,	/* Default is no parity */
887c478bd9Sstevel@tonic-gate 	Duplex = 1,	/* Default is full duplex */
897c478bd9Sstevel@tonic-gate 	Terminal,	/* Default is no terminal */
907c478bd9Sstevel@tonic-gate 	line_8bit = -1;	/* Default is same as terminal */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static const char P_PARITY[] = "Parity option error\r\n";
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static struct termio Savettyb;
957c478bd9Sstevel@tonic-gate static struct termios Savettybs;
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * set speed/echo/mode...
987c478bd9Sstevel@tonic-gate  *	tty 	-> terminal name
997c478bd9Sstevel@tonic-gate  *	spwant 	-> speed
1007c478bd9Sstevel@tonic-gate  *	type	-> type
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  *	if spwant == 0, speed is untouched
1037c478bd9Sstevel@tonic-gate  *	type is unused, but needed for compatibility
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * return:
1067c478bd9Sstevel@tonic-gate  *	none
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
109*61961e0fSrobinson static void
110*61961e0fSrobinson fixline(int tty, int spwant, int type)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	register const struct sg_spds	*ps;
1137c478bd9Sstevel@tonic-gate 	struct termio		ttbuf;
1147c478bd9Sstevel@tonic-gate 	struct termios		ttbufs;
1157c478bd9Sstevel@tonic-gate 	int			speed = -1;
1167c478bd9Sstevel@tonic-gate 	int			i, istermios, ospeed;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	DEBUG(6, "fixline(%d, ", tty);
1197c478bd9Sstevel@tonic-gate 	DEBUG(6, "%d)\n", spwant);
1207c478bd9Sstevel@tonic-gate 	if ((istermios = (*Ioctl)(tty, TCGETS, &ttbufs)) < 0) {
121*61961e0fSrobinson 		if ((*Ioctl)(tty, TCGETA, &ttbuf) != 0)
1227c478bd9Sstevel@tonic-gate 			return;
1237c478bd9Sstevel@tonic-gate 		ttbufs.c_lflag = ttbuf.c_lflag;
1247c478bd9Sstevel@tonic-gate 		ttbufs.c_oflag = ttbuf.c_oflag;
1257c478bd9Sstevel@tonic-gate 		ttbufs.c_iflag = ttbuf.c_iflag;
1267c478bd9Sstevel@tonic-gate 		ttbufs.c_cflag = ttbuf.c_cflag;
1277c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
1287c478bd9Sstevel@tonic-gate 			ttbufs.c_cc[i] = ttbuf.c_cc[i];
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	if (spwant > 0) {
1317c478bd9Sstevel@tonic-gate 		for (ps = spds; ps->sp_val; ps++)
1327c478bd9Sstevel@tonic-gate 			if (ps->sp_val == spwant) {
1337c478bd9Sstevel@tonic-gate 				speed = ps->sp_name;
1347c478bd9Sstevel@tonic-gate 				break;
1357c478bd9Sstevel@tonic-gate 			}
1367c478bd9Sstevel@tonic-gate 		if (speed < 0) {
1377c478bd9Sstevel@tonic-gate 			/*EMPTY*/
1387c478bd9Sstevel@tonic-gate 			DEBUG(5, "speed (%d) not supported\n", spwant);
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 		ASSERT(speed >= 0, "BAD SPEED", "", spwant);
1417c478bd9Sstevel@tonic-gate 		ttbufs.c_cflag &= 0xffff0000;
142*61961e0fSrobinson 		(void) cfsetospeed(&ttbufs, speed);
1437c478bd9Sstevel@tonic-gate 	} else { /* determine the current speed setting */
1447c478bd9Sstevel@tonic-gate 		ospeed = cfgetospeed(&ttbufs);
1457c478bd9Sstevel@tonic-gate 		ttbufs.c_cflag &= 0xffff0000;
146*61961e0fSrobinson 		(void) cfsetospeed(&ttbufs, ospeed);
1477c478bd9Sstevel@tonic-gate 		for (ps = spds; ps->sp_val; ps++)
1487c478bd9Sstevel@tonic-gate 			if (ps->sp_name == ospeed) {
1497c478bd9Sstevel@tonic-gate 				spwant = ps->sp_val;
1507c478bd9Sstevel@tonic-gate 				break;
1517c478bd9Sstevel@tonic-gate 			}
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	ttbufs.c_iflag &= 0xffff0000;
1547c478bd9Sstevel@tonic-gate 	ttbufs.c_oflag &= 0xffff0000;
1557c478bd9Sstevel@tonic-gate 	ttbufs.c_lflag &= 0xffff0000;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	ttbufs.c_cflag &= ~CLOCAL;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (EQUALS(Progname, "cu")) {
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		/* set attributes associated with -h, -t, -e, and -o options */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		ttbufs.c_iflag = (IGNPAR | IGNBRK | IXON | IXOFF);
1647c478bd9Sstevel@tonic-gate 		if (line_8bit) {
1657c478bd9Sstevel@tonic-gate 			ttbufs.c_cflag |= CS8;
1667c478bd9Sstevel@tonic-gate 			ttbufs.c_iflag &= ~ISTRIP;
1677c478bd9Sstevel@tonic-gate 		} else {
1687c478bd9Sstevel@tonic-gate 			ttbufs.c_cflag |= CS7;
1697c478bd9Sstevel@tonic-gate 			ttbufs.c_iflag |= ISTRIP;
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		ttbufs.c_cc[VEOF] = '\1';
1737c478bd9Sstevel@tonic-gate 		ttbufs.c_cflag |= (CREAD | (speed ? HUPCL : 0));
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		if (Evenflag) {				/* even parity -e */
1767c478bd9Sstevel@tonic-gate 			if (ttbufs.c_cflag & PARENB) {
1777c478bd9Sstevel@tonic-gate 				VERBOSE(P_PARITY, 0);
1787c478bd9Sstevel@tonic-gate 				exit(1);
179*61961e0fSrobinson 			}
1807c478bd9Sstevel@tonic-gate 			ttbufs.c_cflag |= PARENB;
1817c478bd9Sstevel@tonic-gate 		} else if (Oddflag) {			/* odd parity -o */
1827c478bd9Sstevel@tonic-gate 			if (ttbufs.c_cflag & PARENB) {
1837c478bd9Sstevel@tonic-gate 				VERBOSE(P_PARITY, 0);
1847c478bd9Sstevel@tonic-gate 				exit(1);
185*61961e0fSrobinson 			}
1867c478bd9Sstevel@tonic-gate 			ttbufs.c_cflag |= PARODD;
1877c478bd9Sstevel@tonic-gate 			ttbufs.c_cflag |= PARENB;
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		if (!Duplex)				/* half duplex -h */
1917c478bd9Sstevel@tonic-gate 			ttbufs.c_iflag &= ~(IXON | IXOFF);
1927c478bd9Sstevel@tonic-gate 		if (Terminal)				/* -t */
1937c478bd9Sstevel@tonic-gate 			ttbufs.c_oflag |= (OPOST | ONLCR);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	} else { /* non-cu */
1967c478bd9Sstevel@tonic-gate 		ttbufs.c_cflag |= (CS8 | CREAD | (speed ? HUPCL : 0));
1977c478bd9Sstevel@tonic-gate 		ttbufs.c_cc[VMIN] = HEADERSIZE;
1987c478bd9Sstevel@tonic-gate 		ttbufs.c_cc[VTIME] = 1;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (istermios < 0) {
2027c478bd9Sstevel@tonic-gate 		ttbuf.c_lflag = ttbufs.c_lflag;
2037c478bd9Sstevel@tonic-gate 		ttbuf.c_oflag = ttbufs.c_oflag;
2047c478bd9Sstevel@tonic-gate 		ttbuf.c_iflag = ttbufs.c_iflag;
2057c478bd9Sstevel@tonic-gate 		ttbuf.c_cflag = ttbufs.c_cflag;
2067c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
2077c478bd9Sstevel@tonic-gate 			ttbuf.c_cc[i] = ttbufs.c_cc[i];
2087c478bd9Sstevel@tonic-gate 		ASSERT((*Ioctl)(tty, TCSETAW, &ttbuf) >= 0,
2097c478bd9Sstevel@tonic-gate 			"RETURN FROM fixline ioctl", "", errno);
2107c478bd9Sstevel@tonic-gate 	} else {
2117c478bd9Sstevel@tonic-gate 		ASSERT((*Ioctl)(tty, TCSETSW, &ttbufs) >= 0,
2127c478bd9Sstevel@tonic-gate 			"RETURN FROM fixline ioctl", "", errno);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
216*61961e0fSrobinson static void
217*61961e0fSrobinson sethup(int dcf)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	struct termio ttbuf;
2207c478bd9Sstevel@tonic-gate 
221*61961e0fSrobinson 	if ((*Ioctl)(dcf, TCGETA, &ttbuf) != 0)
2227c478bd9Sstevel@tonic-gate 		return;
2237c478bd9Sstevel@tonic-gate 	if (!(ttbuf.c_cflag & HUPCL)) {
2247c478bd9Sstevel@tonic-gate 		ttbuf.c_cflag |= HUPCL;
2257c478bd9Sstevel@tonic-gate 		(void) (*Ioctl)(dcf, TCSETAW, &ttbuf);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
229*61961e0fSrobinson static void
230*61961e0fSrobinson ttygenbrk(int fn)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	if (isatty(fn))
2337c478bd9Sstevel@tonic-gate 		(void) (*Ioctl)(fn, TCSBRK, 0);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
236*61961e0fSrobinson static int
237*61961e0fSrobinson savline(void)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	if ((Saved_termios = (*Ioctl)(0, TCGETS, &Savettybs)) < 0) {
2407c478bd9Sstevel@tonic-gate 		if ((*Ioctl)(0, TCGETA, &Savettyb) != 0) {
2417c478bd9Sstevel@tonic-gate 			Saved_line = FALSE;
2427c478bd9Sstevel@tonic-gate 		} else {
2437c478bd9Sstevel@tonic-gate 			Saved_line = TRUE;
2447c478bd9Sstevel@tonic-gate 			Savettyb.c_cflag = (Savettyb.c_cflag & ~CS8) | CS7;
2457c478bd9Sstevel@tonic-gate 			Savettyb.c_oflag |= OPOST;
2467c478bd9Sstevel@tonic-gate 			Savettyb.c_lflag |= (ISIG|ICANON|ECHO);
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 	} else {
2497c478bd9Sstevel@tonic-gate 		Saved_line = TRUE;
2507c478bd9Sstevel@tonic-gate 		Savettybs.c_cflag = (Savettybs.c_cflag & ~CS8) | CS7;
2517c478bd9Sstevel@tonic-gate 		Savettybs.c_oflag |= OPOST;
2527c478bd9Sstevel@tonic-gate 		Savettybs.c_lflag |= (ISIG|ICANON|ECHO);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	return (0);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
257*61961e0fSrobinson static int
258*61961e0fSrobinson restline(void)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	if (Saved_line == TRUE) {
2617c478bd9Sstevel@tonic-gate 		if (Saved_termios < 0)
2627c478bd9Sstevel@tonic-gate 			return ((*Ioctl)(0, TCSETAW, &Savettyb));
2637c478bd9Sstevel@tonic-gate 		else
2647c478bd9Sstevel@tonic-gate 			return ((*Ioctl)(0, TCSETSW, &Savettybs));
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	return (0);
2677c478bd9Sstevel@tonic-gate }
268