xref: /illumos-gate/usr/src/cmd/ttymon/tmterm.c (revision 902bba376031b794865234f1621102c7f4bf9d2b)
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  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <termio.h>
347c478bd9Sstevel@tonic-gate #include <sys/stermio.h>
357c478bd9Sstevel@tonic-gate #include <sys/termiox.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include "sys/stropts.h"
417c478bd9Sstevel@tonic-gate #include "sys/signal.h"
427c478bd9Sstevel@tonic-gate #include "ttymon.h"
437c478bd9Sstevel@tonic-gate #include "tmstruct.h"
447c478bd9Sstevel@tonic-gate #include "tmextern.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  *	set_termio	- set termio on device
487c478bd9Sstevel@tonic-gate  *		fd	- fd for the device
497c478bd9Sstevel@tonic-gate  *		options - stty termio options
507c478bd9Sstevel@tonic-gate  *		aspeed  - autobaud speed
517c478bd9Sstevel@tonic-gate  *		clear	- if TRUE, current flags will be set to some defaults
527c478bd9Sstevel@tonic-gate  *			  before applying the options
537c478bd9Sstevel@tonic-gate  *			- if FALSE, current flags will not be cleared
547c478bd9Sstevel@tonic-gate  *		mode	- terminal mode, CANON, RAW
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate int
set_termio(int fd,char * options,char * aspeed,int clear,long mode)603bb2c156SToomas Soome set_termio(int fd, char *options, char *aspeed, int clear, long mode)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	struct	 termio termio;
637c478bd9Sstevel@tonic-gate 	struct	 termios termios;
647c478bd9Sstevel@tonic-gate 	struct	 stio stermio;
657c478bd9Sstevel@tonic-gate 	struct	 termiox termiox;
667c478bd9Sstevel@tonic-gate 	struct	 winsize winsize;
677c478bd9Sstevel@tonic-gate 	struct	 winsize owinsize;
687c478bd9Sstevel@tonic-gate 	int	 term;
697c478bd9Sstevel@tonic-gate 	int	 cnt = 1;
707c478bd9Sstevel@tonic-gate 	char	 *uarg;
717c478bd9Sstevel@tonic-gate 	char	 *argvp[MAXARGS];	/* stty args */
727c478bd9Sstevel@tonic-gate 	static   char	 *binstty = "/usr/bin/stty";
737c478bd9Sstevel@tonic-gate 	static	 char	buf[BUFSIZ];
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #ifdef	DEBUG
767c478bd9Sstevel@tonic-gate 	debug("in set_termio");
777c478bd9Sstevel@tonic-gate #endif
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if ((term = get_ttymode(fd, &termio, &termios, &stermio,
807c478bd9Sstevel@tonic-gate 	    &termiox, &winsize)) < 0) {
817c478bd9Sstevel@tonic-gate 		log("set_termio: get_ttymode failed: %s", strerror(errno));
827c478bd9Sstevel@tonic-gate 		return (-1);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 	owinsize = winsize;
857c478bd9Sstevel@tonic-gate 	if (clear) {
867c478bd9Sstevel@tonic-gate 		if (mode & CANON) {
877c478bd9Sstevel@tonic-gate 			/* could have removed these too - rely on defaults */
887c478bd9Sstevel@tonic-gate 			termios.c_cc[VEOF] = CEOF;
897c478bd9Sstevel@tonic-gate 			termios.c_cc[VEOL] = CNUL;
903bb2c156SToomas Soome 		} else {
917c478bd9Sstevel@tonic-gate 			termios.c_lflag &= ECHO;
927c478bd9Sstevel@tonic-gate 			termios.c_cc[VMIN] = 1;
937c478bd9Sstevel@tonic-gate 			termios.c_cc[VTIME] = 0;
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (options != NULL && *options != '\0') {
997c478bd9Sstevel@tonic-gate 		/* just a place holder to make it look like invoking stty */
1007c478bd9Sstevel@tonic-gate 		argvp[0] = binstty;
1017c478bd9Sstevel@tonic-gate 		(void) strcpy(buf, options);
1027c478bd9Sstevel@tonic-gate 		mkargv(buf, &argvp[1], &cnt, MAXARGS - 1);
1037c478bd9Sstevel@tonic-gate 		if (aspeed != NULL && *aspeed != '\0') {
1047c478bd9Sstevel@tonic-gate 			argvp[cnt++] = aspeed;
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 		argvp[cnt] = (char *)0;
1077c478bd9Sstevel@tonic-gate 		if ((uarg = sttyparse(cnt, argvp, term, &termio, &termios,
1087c478bd9Sstevel@tonic-gate 		    &termiox, &winsize)) != NULL) {
1097c478bd9Sstevel@tonic-gate 			log("sttyparse unknown mode: %s", uarg);
1107c478bd9Sstevel@tonic-gate 			return (-1);
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (set_ttymode(fd, term, &termio, &termios, &stermio,
1167c478bd9Sstevel@tonic-gate 	    &termiox, &winsize, &owinsize) != 0) {
1177c478bd9Sstevel@tonic-gate 		log("set_termio: set_ttymode failed", strerror(errno));
1187c478bd9Sstevel@tonic-gate 		return (-1);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	return (0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #ifdef	NOT_USE
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  *	turnon_canon	- turn on canonical processing
1277c478bd9Sstevel@tonic-gate  *			- return 0 if succeeds, -1 if fails
1287c478bd9Sstevel@tonic-gate  */
turnon_canon(int fd)1293bb2c156SToomas Soome turnon_canon(int fd)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	struct termio termio;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1347c478bd9Sstevel@tonic-gate 	debug("in turnon_canon");
1357c478bd9Sstevel@tonic-gate #endif
1367c478bd9Sstevel@tonic-gate 	if (ioctl(fd, TCGETA, &termio) != 0) {
1377c478bd9Sstevel@tonic-gate 		log("turnon_canon: TCGETA failed, fd = %d: %s", fd,
1387c478bd9Sstevel@tonic-gate 		    strerror(errno));
1397c478bd9Sstevel@tonic-gate 		return (-1);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	termio.c_lflag |= (ISIG|ICANON|ECHO|ECHOE|ECHOK);
1427c478bd9Sstevel@tonic-gate 	termio.c_cc[VEOF] = CEOF;
1437c478bd9Sstevel@tonic-gate 	termio.c_cc[VEOL] = CNUL;
1447c478bd9Sstevel@tonic-gate 	if (ioctl(fd, TCSETA, &termio) != 0) {
1457c478bd9Sstevel@tonic-gate 		log("turnon_canon: TCSETA failed, fd = %d: %s", fd,
1467c478bd9Sstevel@tonic-gate 		    strerror(errno));
1477c478bd9Sstevel@tonic-gate 		return (-1);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 	return (0);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate #endif
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  *	flush_input	- flush the input queue
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate void
flush_input(int fd)1573bb2c156SToomas Soome flush_input(int fd)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_FLUSH, FLUSHR) == -1)
1607c478bd9Sstevel@tonic-gate 		log("flush_input failed, fd = %d: %s", fd, strerror(errno));
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (ioctl(fd, TCSBRK, 1) == -1)
1637c478bd9Sstevel@tonic-gate 		log("drain of ouput failed, fd = %d: %s", fd, strerror(errno));
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * push_linedisc	- if modules is not NULL, pop everything
1687c478bd9Sstevel@tonic-gate  *			- then push modules specified by "modules"
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate int
push_linedisc(int fd,char * modules,char * device)1717c478bd9Sstevel@tonic-gate push_linedisc(
1727c478bd9Sstevel@tonic-gate 	int	fd,	/* fd to push modules on */
1737c478bd9Sstevel@tonic-gate 	char	*modules, /* ptr to a list of comma separated module names */
1747c478bd9Sstevel@tonic-gate 	char	*device) /* device name for printing msg */
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	char	*p, *tp;
1777c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1807c478bd9Sstevel@tonic-gate 	debug("in push_linedisc");
1817c478bd9Sstevel@tonic-gate #endif
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * copy modules into buf so we won't mess up the original buffer
1847c478bd9Sstevel@tonic-gate 	 * because strtok will chop the string
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	p = strcpy(buf, modules);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	while (ioctl(fd, I_POP, 0) >= 0)  /* pop everything */
1897c478bd9Sstevel@tonic-gate 		;
1903bb2c156SToomas Soome 	for (p = strtok(p, ","); p != NULL; p = strtok(NULL, ",")) {
1917c478bd9Sstevel@tonic-gate 		for (tp = p + strlen(p) - 1; tp >= p && isspace(*tp); --tp)
1927c478bd9Sstevel@tonic-gate 			*tp = '\0';
1937c478bd9Sstevel@tonic-gate 		if (ioctl(fd, I_PUSH, p) == -1) {
1947c478bd9Sstevel@tonic-gate 			log("push (%s) on %s failed: %s", p, device,
1957c478bd9Sstevel@tonic-gate 			    strerror(errno));
1967c478bd9Sstevel@tonic-gate 			return (-1);
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	return (0);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  *	hang_up_line	- set speed to B0. This will drop DTR
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate int
hang_up_line(int fd)2067c478bd9Sstevel@tonic-gate hang_up_line(int fd)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	struct termio termio;
2097c478bd9Sstevel@tonic-gate 	struct termios termios;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2127c478bd9Sstevel@tonic-gate 	debug("in hang_up_line");
2137c478bd9Sstevel@tonic-gate #endif
2147c478bd9Sstevel@tonic-gate 	if (ioctl(fd, TCGETS, &termios) < 0) {
2157c478bd9Sstevel@tonic-gate 		if (ioctl(fd, TCGETA, &termio) < 0) {
2167c478bd9Sstevel@tonic-gate 			log("hang_up_line: TCGETA failed: %s", strerror(errno));
2177c478bd9Sstevel@tonic-gate 			return (-1);
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 		termio.c_cflag &= ~CBAUD;
2207c478bd9Sstevel@tonic-gate 		termio.c_cflag |= B0;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		if (ioctl(fd, TCSETA, &termio) < 0) {
2237c478bd9Sstevel@tonic-gate 			log("hang_up_line: TCSETA failed: %s", strerror(errno));
2247c478bd9Sstevel@tonic-gate 			return (-1);
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 	} else {
2273bb2c156SToomas Soome 		(void) cfsetospeed(&termios, B0);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		if (ioctl(fd, TCSETS, &termios) < 0) {
2307c478bd9Sstevel@tonic-gate 			log("hang_up_line: TCSETS failed: %s", strerror(errno));
2317c478bd9Sstevel@tonic-gate 			return (-1);
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	return (0);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * initial_termio	- set initial termios
2397c478bd9Sstevel@tonic-gate  *			- return 0 if successful, -1 if failed.
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate int
initial_termio(int fd,struct pmtab * pmptr)2423bb2c156SToomas Soome initial_termio(int fd, struct pmtab *pmptr)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	int	ret;
2457c478bd9Sstevel@tonic-gate 	struct	Gdef *speedef;
2467c478bd9Sstevel@tonic-gate 
247*902bba37SToomas Soome 	speedef = get_speed(pmptr);
2487c478bd9Sstevel@tonic-gate 	if (speedef->g_autobaud & A_FLAG) {
2497c478bd9Sstevel@tonic-gate 		pmptr->p_ttyflags |= A_FLAG;
2507c478bd9Sstevel@tonic-gate 		if (auto_termio(fd) == -1) {
2517c478bd9Sstevel@tonic-gate 			(void) close(fd);
2527c478bd9Sstevel@tonic-gate 			return (-1);
2537c478bd9Sstevel@tonic-gate 		}
2543bb2c156SToomas Soome 	} else {
2557c478bd9Sstevel@tonic-gate 		if (pmptr->p_ttyflags & R_FLAG)
2567c478bd9Sstevel@tonic-gate 			ret = set_termio(fd, speedef->g_iflags,
2573bb2c156SToomas Soome 			    NULL, TRUE, (long)RAW);
2587c478bd9Sstevel@tonic-gate 		else
2597c478bd9Sstevel@tonic-gate 			ret = set_termio(fd, speedef->g_iflags,
2603bb2c156SToomas Soome 			    NULL, TRUE, (long)CANON);
2617c478bd9Sstevel@tonic-gate 		if (ret == -1) {
2627c478bd9Sstevel@tonic-gate 			log("initial termio on (%s) failed", pmptr->p_device);
2637c478bd9Sstevel@tonic-gate 			(void) close(fd);
2647c478bd9Sstevel@tonic-gate 			return (-1);
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 	return (0);
2687c478bd9Sstevel@tonic-gate }
269