xref: /illumos-gate/usr/src/cmd/ttymon/tmterm.c (revision 3bb2c156)
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	*/
28*3bb2c156SToomas Soome /*	  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"
42*3bb2c156SToomas Soome #include "ttymon.h"
43*3bb2c156SToomas Soome #include "tmstruct.h"
44*3bb2c156SToomas Soome #include "tmextern.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
47*3bb2c156SToomas Soome  *	set_termio	- set termio on device
487c478bd9Sstevel@tonic-gate  *		fd	- fd for the device
49*3bb2c156SToomas Soome  *		options - stty termio options
50*3bb2c156SToomas Soome  *		aspeed  - autobaud speed
517c478bd9Sstevel@tonic-gate  *		clear	- if TRUE, current flags will be set to some defaults
52*3bb2c156SToomas Soome  *			  before applying the options
53*3bb2c156SToomas Soome  *			- 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)60*3bb2c156SToomas Soome set_termio(int fd, char *options, char *aspeed, int clear, long mode)
617c478bd9Sstevel@tonic-gate {
62*3bb2c156SToomas Soome 	struct	 termio termio;
63*3bb2c156SToomas Soome 	struct	 termios termios;
64*3bb2c156SToomas Soome 	struct	 stio stermio;
65*3bb2c156SToomas Soome 	struct	 termiox termiox;
66*3bb2c156SToomas Soome 	struct	 winsize winsize;
67*3bb2c156SToomas Soome 	struct	 winsize owinsize;
687c478bd9Sstevel@tonic-gate 	int	 term;
697c478bd9Sstevel@tonic-gate 	int	 cnt = 1;
70*3bb2c156SToomas Soome 	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 
79*3bb2c156SToomas Soome 	if ((term = get_ttymode(fd, &termio, &termios, &stermio,
80*3bb2c156SToomas Soome 	    &termiox, &winsize)) < 0) {
817c478bd9Sstevel@tonic-gate 		log("set_termio: get_ttymode failed: %s", strerror(errno));
82*3bb2c156SToomas Soome 		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;
90*3bb2c156SToomas 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;
101*3bb2c156SToomas Soome 		(void) strcpy(buf, options);
102*3bb2c156SToomas Soome 		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;
107*3bb2c156SToomas Soome 		if ((uarg = sttyparse(cnt, argvp, term, &termio, &termios,
108*3bb2c156SToomas Soome 		    &termiox, &winsize)) != NULL) {
1097c478bd9Sstevel@tonic-gate 			log("sttyparse unknown mode: %s", uarg);
110*3bb2c156SToomas Soome 			return (-1);
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 
115*3bb2c156SToomas Soome 	if (set_ttymode(fd, term, &termio, &termios, &stermio,
116*3bb2c156SToomas Soome 	    &termiox, &winsize, &owinsize) != 0) {
1177c478bd9Sstevel@tonic-gate 		log("set_termio: set_ttymode failed", strerror(errno));
118*3bb2c156SToomas Soome 		return (-1);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
121*3bb2c156SToomas Soome 	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)129*3bb2c156SToomas 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));
139*3bb2c156SToomas Soome 		return (-1);
1407c478bd9Sstevel@tonic-gate 	}
141*3bb2c156SToomas Soome 	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));
147*3bb2c156SToomas Soome 		return (-1);
1487c478bd9Sstevel@tonic-gate 	}
149*3bb2c156SToomas Soome 	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)157*3bb2c156SToomas 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 	 */
186*3bb2c156SToomas Soome 	p = strcpy(buf, modules);
1877c478bd9Sstevel@tonic-gate 
188*3bb2c156SToomas Soome 	while (ioctl(fd, I_POP, 0) >= 0)  /* pop everything */
1897c478bd9Sstevel@tonic-gate 		;
190*3bb2c156SToomas 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));
196*3bb2c156SToomas Soome 			return (-1);
197*3bb2c156SToomas Soome 		}
1987c478bd9Sstevel@tonic-gate 	}
199*3bb2c156SToomas Soome 	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
214*3bb2c156SToomas Soome 	if (ioctl(fd, TCGETS, &termios) < 0) {
215*3bb2c156SToomas Soome 		if (ioctl(fd, TCGETA, &termio) < 0) {
216*3bb2c156SToomas Soome 			log("hang_up_line: TCGETA failed: %s", strerror(errno));
217*3bb2c156SToomas Soome 			return (-1);
218*3bb2c156SToomas Soome 		}
219*3bb2c156SToomas Soome 		termio.c_cflag &= ~CBAUD;
220*3bb2c156SToomas Soome 		termio.c_cflag |= B0;
2217c478bd9Sstevel@tonic-gate 
222*3bb2c156SToomas Soome 		if (ioctl(fd, TCSETA, &termio) < 0) {
223*3bb2c156SToomas Soome 			log("hang_up_line: TCSETA failed: %s", strerror(errno));
224*3bb2c156SToomas Soome 			return (-1);
225*3bb2c156SToomas Soome 		}
2267c478bd9Sstevel@tonic-gate 	} else {
227*3bb2c156SToomas Soome 		(void) cfsetospeed(&termios, B0);
2287c478bd9Sstevel@tonic-gate 
229*3bb2c156SToomas Soome 		if (ioctl(fd, TCSETS, &termios) < 0) {
230*3bb2c156SToomas Soome 			log("hang_up_line: TCSETS failed: %s", strerror(errno));
231*3bb2c156SToomas Soome 			return (-1);
232*3bb2c156SToomas Soome 		}
2337c478bd9Sstevel@tonic-gate 	}
234*3bb2c156SToomas Soome 	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)242*3bb2c156SToomas 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 
2477c478bd9Sstevel@tonic-gate 	speedef = get_speed(pmptr->p_ttylabel);
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) {
251*3bb2c156SToomas Soome 			(void) close(fd);
252*3bb2c156SToomas Soome 			return (-1);
2537c478bd9Sstevel@tonic-gate 		}
254*3bb2c156SToomas Soome 	} else {
2557c478bd9Sstevel@tonic-gate 		if (pmptr->p_ttyflags & R_FLAG)
256*3bb2c156SToomas Soome 			ret = set_termio(fd, speedef->g_iflags,
257*3bb2c156SToomas Soome 			    NULL, TRUE, (long)RAW);
258*3bb2c156SToomas Soome 		else
259*3bb2c156SToomas Soome 			ret = set_termio(fd, speedef->g_iflags,
260*3bb2c156SToomas Soome 			    NULL, TRUE, (long)CANON);
2617c478bd9Sstevel@tonic-gate 		if (ret == -1) {
2627c478bd9Sstevel@tonic-gate 			log("initial termio on (%s) failed", pmptr->p_device);
263*3bb2c156SToomas Soome 			(void) close(fd);
264*3bb2c156SToomas Soome 			return (-1);
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 	}
267*3bb2c156SToomas Soome 	return (0);
2687c478bd9Sstevel@tonic-gate }
269