xref: /illumos-gate/usr/src/cmd/ttymon/tmterm.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <errno.h>
37*7c478bd9Sstevel@tonic-gate #include <termio.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/stermio.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/termiox.h>
40*7c478bd9Sstevel@tonic-gate #include <string.h>
41*7c478bd9Sstevel@tonic-gate #include <ctype.h>
42*7c478bd9Sstevel@tonic-gate #include <unistd.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
44*7c478bd9Sstevel@tonic-gate #include "sys/stropts.h"
45*7c478bd9Sstevel@tonic-gate #include "sys/signal.h"
46*7c478bd9Sstevel@tonic-gate #include "ttymon.h"
47*7c478bd9Sstevel@tonic-gate #include "tmstruct.h"
48*7c478bd9Sstevel@tonic-gate #include "tmextern.h"
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate extern	void	mkargv();
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  *	set_termio	- set termio on device
54*7c478bd9Sstevel@tonic-gate  *		fd	- fd for the device
55*7c478bd9Sstevel@tonic-gate  *		options - stty termio options
56*7c478bd9Sstevel@tonic-gate  *		aspeed  - autobaud speed
57*7c478bd9Sstevel@tonic-gate  *		clear	- if TRUE, current flags will be set to some defaults
58*7c478bd9Sstevel@tonic-gate  *			  before applying the options
59*7c478bd9Sstevel@tonic-gate  *		    	- if FALSE, current flags will not be cleared
60*7c478bd9Sstevel@tonic-gate  *		mode	- terminal mode, CANON, RAW
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate int
66*7c478bd9Sstevel@tonic-gate set_termio(fd,options,aspeed,clear,mode)
67*7c478bd9Sstevel@tonic-gate int	fd;
68*7c478bd9Sstevel@tonic-gate char	*options;
69*7c478bd9Sstevel@tonic-gate char	*aspeed;
70*7c478bd9Sstevel@tonic-gate int	clear;
71*7c478bd9Sstevel@tonic-gate long	mode;
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	struct 	 termio termio;
74*7c478bd9Sstevel@tonic-gate 	struct 	 termios termios;
75*7c478bd9Sstevel@tonic-gate 	struct 	 stio stermio;
76*7c478bd9Sstevel@tonic-gate 	struct 	 termiox termiox;
77*7c478bd9Sstevel@tonic-gate 	struct 	 winsize winsize;
78*7c478bd9Sstevel@tonic-gate 	struct 	 winsize owinsize;
79*7c478bd9Sstevel@tonic-gate 	int	 term;
80*7c478bd9Sstevel@tonic-gate 	int	 cnt = 1;
81*7c478bd9Sstevel@tonic-gate 	char	 *uarg;
82*7c478bd9Sstevel@tonic-gate 	char	 *argvp[MAXARGS];	/* stty args */
83*7c478bd9Sstevel@tonic-gate 	static   char	 *binstty = "/usr/bin/stty";
84*7c478bd9Sstevel@tonic-gate 	static	 char	buf[BUFSIZ];
85*7c478bd9Sstevel@tonic-gate 	extern 	 int get_ttymode(), set_ttymode();
86*7c478bd9Sstevel@tonic-gate 	extern	 char	*sttyparse();
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
89*7c478bd9Sstevel@tonic-gate 	debug("in set_termio");
90*7c478bd9Sstevel@tonic-gate #endif
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if ((term = get_ttymode(fd, &termio, &termios, &stermio,
93*7c478bd9Sstevel@tonic-gate 				&termiox, &winsize)) < 0) {
94*7c478bd9Sstevel@tonic-gate 		log("set_termio: get_ttymode failed: %s", strerror(errno));
95*7c478bd9Sstevel@tonic-gate 		return(-1);
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 	owinsize = winsize;
98*7c478bd9Sstevel@tonic-gate 	if (clear) {
99*7c478bd9Sstevel@tonic-gate 		if (mode & CANON) {
100*7c478bd9Sstevel@tonic-gate 			/* could have removed these too - rely on defaults */
101*7c478bd9Sstevel@tonic-gate 			termios.c_cc[VEOF] = CEOF;
102*7c478bd9Sstevel@tonic-gate 			termios.c_cc[VEOL] = CNUL;
103*7c478bd9Sstevel@tonic-gate 		}
104*7c478bd9Sstevel@tonic-gate 		else  {
105*7c478bd9Sstevel@tonic-gate 			termios.c_lflag &= ECHO;
106*7c478bd9Sstevel@tonic-gate 			termios.c_cc[VMIN] = 1;
107*7c478bd9Sstevel@tonic-gate 			termios.c_cc[VTIME] = 0;
108*7c478bd9Sstevel@tonic-gate 		}
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	if (options != NULL && *options != '\0') {
113*7c478bd9Sstevel@tonic-gate 		/* just a place holder to make it look like invoking stty */
114*7c478bd9Sstevel@tonic-gate 		argvp[0] = binstty;
115*7c478bd9Sstevel@tonic-gate 		(void)strcpy(buf,options);
116*7c478bd9Sstevel@tonic-gate 		mkargv(buf,&argvp[1],&cnt,MAXARGS-1);
117*7c478bd9Sstevel@tonic-gate 		if (aspeed != NULL && *aspeed != '\0') {
118*7c478bd9Sstevel@tonic-gate 			argvp[cnt++] = aspeed;
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 		argvp[cnt] = (char *)0;
121*7c478bd9Sstevel@tonic-gate 		if ((uarg = sttyparse(cnt, argvp, term, &termio, &termios,
122*7c478bd9Sstevel@tonic-gate 				&termiox, &winsize)) != NULL) {
123*7c478bd9Sstevel@tonic-gate 			log("sttyparse unknown mode: %s", uarg);
124*7c478bd9Sstevel@tonic-gate 			return(-1);
125*7c478bd9Sstevel@tonic-gate 		}
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	if (set_ttymode(fd, term, &termio, &termios, &stermio,
130*7c478bd9Sstevel@tonic-gate 			&termiox, &winsize, &owinsize) != 0) {
131*7c478bd9Sstevel@tonic-gate 		log("set_termio: set_ttymode failed", strerror(errno));
132*7c478bd9Sstevel@tonic-gate 		return(-1);
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	return(0);
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate #ifdef	NOT_USE
139*7c478bd9Sstevel@tonic-gate /*
140*7c478bd9Sstevel@tonic-gate  *	turnon_canon	- turn on canonical processing
141*7c478bd9Sstevel@tonic-gate  *			- return 0 if succeeds, -1 if fails
142*7c478bd9Sstevel@tonic-gate  */
143*7c478bd9Sstevel@tonic-gate turnon_canon(fd)
144*7c478bd9Sstevel@tonic-gate int	fd;
145*7c478bd9Sstevel@tonic-gate {
146*7c478bd9Sstevel@tonic-gate 	struct termio termio;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
149*7c478bd9Sstevel@tonic-gate 	debug("in turnon_canon");
150*7c478bd9Sstevel@tonic-gate #endif
151*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, TCGETA, &termio) != 0) {
152*7c478bd9Sstevel@tonic-gate 		log("turnon_canon: TCGETA failed, fd = %d: %s", fd,
153*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
154*7c478bd9Sstevel@tonic-gate 		return(-1);
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 	termio.c_lflag |= (ISIG|ICANON|ECHO|ECHOE|ECHOK);
157*7c478bd9Sstevel@tonic-gate 	termio.c_cc[VEOF] = CEOF;
158*7c478bd9Sstevel@tonic-gate 	termio.c_cc[VEOL] = CNUL;
159*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, TCSETA, &termio) != 0) {
160*7c478bd9Sstevel@tonic-gate 		log("turnon_canon: TCSETA failed, fd = %d: %s", fd,
161*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
162*7c478bd9Sstevel@tonic-gate 		return(-1);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 	return(0);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate #endif
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate  *	flush_input	- flush the input queue
170*7c478bd9Sstevel@tonic-gate  */
171*7c478bd9Sstevel@tonic-gate void
172*7c478bd9Sstevel@tonic-gate flush_input(fd)
173*7c478bd9Sstevel@tonic-gate int	fd;
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_FLUSH, FLUSHR) == -1)
176*7c478bd9Sstevel@tonic-gate 		log("flush_input failed, fd = %d: %s", fd, strerror(errno));
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, TCSBRK, 1) == -1)
179*7c478bd9Sstevel@tonic-gate 		log("drain of ouput failed, fd = %d: %s", fd, strerror(errno));
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	return;
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate /*
185*7c478bd9Sstevel@tonic-gate  * push_linedisc	- if modules is not NULL, pop everything
186*7c478bd9Sstevel@tonic-gate  *			- then push modules specified by "modules"
187*7c478bd9Sstevel@tonic-gate  */
188*7c478bd9Sstevel@tonic-gate int
189*7c478bd9Sstevel@tonic-gate push_linedisc(
190*7c478bd9Sstevel@tonic-gate 	int	fd,	/* fd to push modules on */
191*7c478bd9Sstevel@tonic-gate 	char	*modules, /* ptr to a list of comma separated module names */
192*7c478bd9Sstevel@tonic-gate 	char	*device) /* device name for printing msg */
193*7c478bd9Sstevel@tonic-gate {
194*7c478bd9Sstevel@tonic-gate 	char	*p, *tp;
195*7c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
198*7c478bd9Sstevel@tonic-gate 	debug("in push_linedisc");
199*7c478bd9Sstevel@tonic-gate #endif
200*7c478bd9Sstevel@tonic-gate 	/*
201*7c478bd9Sstevel@tonic-gate 	 * copy modules into buf so we won't mess up the original buffer
202*7c478bd9Sstevel@tonic-gate 	 * because strtok will chop the string
203*7c478bd9Sstevel@tonic-gate 	 */
204*7c478bd9Sstevel@tonic-gate 	p = strcpy(buf,modules);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	while(ioctl(fd, I_POP, 0) >= 0)  /* pop everything */
207*7c478bd9Sstevel@tonic-gate 		;
208*7c478bd9Sstevel@tonic-gate 	for (p=(char *)strtok(p,","); p!=(char *)NULL;
209*7c478bd9Sstevel@tonic-gate 		p=(char *)strtok(NULL,",")) {
210*7c478bd9Sstevel@tonic-gate 		for (tp = p + strlen(p) - 1; tp >= p && isspace(*tp); --tp)
211*7c478bd9Sstevel@tonic-gate 			*tp = '\0';
212*7c478bd9Sstevel@tonic-gate 		if (ioctl(fd, I_PUSH, p) == -1) {
213*7c478bd9Sstevel@tonic-gate 			log("push (%s) on %s failed: %s", p, device,
214*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
215*7c478bd9Sstevel@tonic-gate 			return(-1);
216*7c478bd9Sstevel@tonic-gate 		}
217*7c478bd9Sstevel@tonic-gate 	}
218*7c478bd9Sstevel@tonic-gate 	return(0);
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate /*
222*7c478bd9Sstevel@tonic-gate  *	hang_up_line	- set speed to B0. This will drop DTR
223*7c478bd9Sstevel@tonic-gate  */
224*7c478bd9Sstevel@tonic-gate int
225*7c478bd9Sstevel@tonic-gate hang_up_line(int fd)
226*7c478bd9Sstevel@tonic-gate {
227*7c478bd9Sstevel@tonic-gate 	struct termio termio;
228*7c478bd9Sstevel@tonic-gate 	struct termios termios;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
231*7c478bd9Sstevel@tonic-gate 	debug("in hang_up_line");
232*7c478bd9Sstevel@tonic-gate #endif
233*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd,TCGETS,&termios) < 0) {
234*7c478bd9Sstevel@tonic-gate 	    if (ioctl(fd,TCGETA,&termio) < 0) {
235*7c478bd9Sstevel@tonic-gate 		log("hang_up_line: TCGETA failed: %s", strerror(errno));
236*7c478bd9Sstevel@tonic-gate 		return(-1);
237*7c478bd9Sstevel@tonic-gate 	    }
238*7c478bd9Sstevel@tonic-gate 	    termio.c_cflag &= ~CBAUD;
239*7c478bd9Sstevel@tonic-gate 	    termio.c_cflag |= B0;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	    if (ioctl(fd,TCSETA,&termio) < 0) {
242*7c478bd9Sstevel@tonic-gate 		log("hang_up_line: TCSETA failed: %s", strerror(errno));
243*7c478bd9Sstevel@tonic-gate 		return(-1);
244*7c478bd9Sstevel@tonic-gate 	    }
245*7c478bd9Sstevel@tonic-gate 	} else {
246*7c478bd9Sstevel@tonic-gate 	    cfsetospeed(&termios, B0);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	    if (ioctl(fd,TCSETS,&termios) < 0) {
249*7c478bd9Sstevel@tonic-gate 		log("hang_up_line: TCSETS failed: %s", strerror(errno));
250*7c478bd9Sstevel@tonic-gate 		return(-1);
251*7c478bd9Sstevel@tonic-gate 	    }
252*7c478bd9Sstevel@tonic-gate 	}
253*7c478bd9Sstevel@tonic-gate 	return(0);
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate /*
257*7c478bd9Sstevel@tonic-gate  * initial_termio	- set initial termios
258*7c478bd9Sstevel@tonic-gate  *			- return 0 if successful, -1 if failed.
259*7c478bd9Sstevel@tonic-gate  */
260*7c478bd9Sstevel@tonic-gate int
261*7c478bd9Sstevel@tonic-gate initial_termio(fd,pmptr)
262*7c478bd9Sstevel@tonic-gate int	fd;
263*7c478bd9Sstevel@tonic-gate struct	pmtab	*pmptr;
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate 	int	ret;
266*7c478bd9Sstevel@tonic-gate 	struct	Gdef *speedef;
267*7c478bd9Sstevel@tonic-gate 	struct	Gdef *get_speed();
268*7c478bd9Sstevel@tonic-gate 	extern	int  auto_termio();
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	speedef = get_speed(pmptr->p_ttylabel);
271*7c478bd9Sstevel@tonic-gate 	if (speedef->g_autobaud & A_FLAG) {
272*7c478bd9Sstevel@tonic-gate 		pmptr->p_ttyflags |= A_FLAG;
273*7c478bd9Sstevel@tonic-gate 		if (auto_termio(fd) == -1) {
274*7c478bd9Sstevel@tonic-gate 			(void)close(fd);
275*7c478bd9Sstevel@tonic-gate 			return(-1);
276*7c478bd9Sstevel@tonic-gate 		}
277*7c478bd9Sstevel@tonic-gate 	}
278*7c478bd9Sstevel@tonic-gate 	else {
279*7c478bd9Sstevel@tonic-gate 		if (pmptr->p_ttyflags & R_FLAG)
280*7c478bd9Sstevel@tonic-gate 			ret = set_termio(fd,speedef->g_iflags,
281*7c478bd9Sstevel@tonic-gate 				(char *)NULL, TRUE, (long)RAW);
282*7c478bd9Sstevel@tonic-gate 		else
283*7c478bd9Sstevel@tonic-gate 			ret = set_termio(fd,speedef->g_iflags,
284*7c478bd9Sstevel@tonic-gate 				(char *)NULL, TRUE, (long)CANON);
285*7c478bd9Sstevel@tonic-gate 		if (ret == -1) {
286*7c478bd9Sstevel@tonic-gate 			log("initial termio on (%s) failed", pmptr->p_device);
287*7c478bd9Sstevel@tonic-gate 			(void)close(fd);
288*7c478bd9Sstevel@tonic-gate 			return(-1);
289*7c478bd9Sstevel@tonic-gate 		}
290*7c478bd9Sstevel@tonic-gate 	}
291*7c478bd9Sstevel@tonic-gate 	return(0);
292*7c478bd9Sstevel@tonic-gate }
293