xref: /illumos-gate/usr/src/cmd/ttymon/tmpeek.c (revision 3bb2c1567625e7b11f8c2a5335125224717af64a)
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 <stdio.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
367c478bd9Sstevel@tonic-gate #include <poll.h>
377c478bd9Sstevel@tonic-gate #include <signal.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include "ttymon.h"
407c478bd9Sstevel@tonic-gate #include "tmstruct.h"
417c478bd9Sstevel@tonic-gate #include "tmextern.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	BRK	1
447c478bd9Sstevel@tonic-gate #define	DEL	2
457c478bd9Sstevel@tonic-gate 
46*3bb2c156SToomas Soome struct	strbuf *do_peek(int, int);
47*3bb2c156SToomas Soome static	int	process(int, struct strbuf *);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static	int	interrupt;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  *	poll_data	- it polls the device, waiting for data
537c478bd9Sstevel@tonic-gate  *			- return BADSPEED it <brk> is received
547c478bd9Sstevel@tonic-gate  *			- return the result of process if data is received
557c478bd9Sstevel@tonic-gate  *			- write a newline if <del> is received
567c478bd9Sstevel@tonic-gate  *			- exit if hangup is received
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate int
poll_data(void)59*3bb2c156SToomas Soome poll_data(void)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	int j;
627c478bd9Sstevel@tonic-gate 	struct strbuf *ptr;
637c478bd9Sstevel@tonic-gate 	struct pollfd fds[1];
647c478bd9Sstevel@tonic-gate 	struct sigaction sigact;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #ifdef	DEBUG
677c478bd9Sstevel@tonic-gate 	debug("in poll_data");
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate 	if (peek_ptr != NULL) {
70*3bb2c156SToomas Soome 		for (j = 0; j < peek_ptr->len; j++)
71*3bb2c156SToomas Soome 			peek_ptr->buf[j] &= 0x7F;
727c478bd9Sstevel@tonic-gate 		return (process(0, peek_ptr));
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	fds[0].fd = 0;
757c478bd9Sstevel@tonic-gate 	fds[0].events = POLLIN;
767c478bd9Sstevel@tonic-gate 	sigact.sa_flags = 0;
777c478bd9Sstevel@tonic-gate 	sigact.sa_handler = sigint;
787c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sigact.sa_mask);
797c478bd9Sstevel@tonic-gate 	(void) sigaddset(&sigact.sa_mask, SIGINT);
807c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sigact, NULL);
817c478bd9Sstevel@tonic-gate 	for (;;) {
827c478bd9Sstevel@tonic-gate 		interrupt = 0;
837c478bd9Sstevel@tonic-gate 		if ((j = poll(fds, 1, -1)) == -1) {
847c478bd9Sstevel@tonic-gate 			if (interrupt == BRK) {
857c478bd9Sstevel@tonic-gate 				return (BADSPEED);
867c478bd9Sstevel@tonic-gate 			}
877c478bd9Sstevel@tonic-gate 			if (interrupt == DEL) { /* XXX revisit kmd */
887c478bd9Sstevel@tonic-gate 				return (BADSPEED);
897c478bd9Sstevel@tonic-gate 			}
90*3bb2c156SToomas Soome 		} else if (j > 0) {
917c478bd9Sstevel@tonic-gate 			if (fds[0].revents & POLLHUP) {
927c478bd9Sstevel@tonic-gate 				log("POLLHUP received, about to exit");
937c478bd9Sstevel@tonic-gate 				exit(1);
947c478bd9Sstevel@tonic-gate 			}
957c478bd9Sstevel@tonic-gate 			if (fds[0].revents & POLLIN) {
967c478bd9Sstevel@tonic-gate 				ptr = do_peek(fds[0].fd, 255);
977c478bd9Sstevel@tonic-gate 				if (ptr != NULL) {
987c478bd9Sstevel@tonic-gate 					return (process(fds[0].fd, ptr));
997c478bd9Sstevel@tonic-gate 				}
1007c478bd9Sstevel@tonic-gate 			}
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  *	process	- process the data
1077c478bd9Sstevel@tonic-gate  *		- return GOODNAME if it is a non-empty line
1087c478bd9Sstevel@tonic-gate  *		- return NONAME if a <CR> is received
1097c478bd9Sstevel@tonic-gate  *		- return BADNAME if zero byte is detected
1107c478bd9Sstevel@tonic-gate  *		- except the case of GOODNAME, data will be pulled out
1117c478bd9Sstevel@tonic-gate  *		  of the stream
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate static int
process(int fd,struct strbuf * ptr)1147c478bd9Sstevel@tonic-gate process(
1157c478bd9Sstevel@tonic-gate 	int	fd,		/* fd to read data from if necessary	*/
1167c478bd9Sstevel@tonic-gate 	struct strbuf *ptr)	/* ptr that holds data in ptr->buf	*/
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	unsigned i;
1197c478bd9Sstevel@tonic-gate 	char	junk[BUFSIZ];
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	for (i = 0; i < ptr->len; i++) {
1227c478bd9Sstevel@tonic-gate 		if (ptr->buf[i] == '\0') {
1237c478bd9Sstevel@tonic-gate 			(void) read(fd, junk, i+1);
1247c478bd9Sstevel@tonic-gate 			return (BADSPEED);
1257c478bd9Sstevel@tonic-gate 		} else if ((ptr->buf[i] == '\n') || (ptr->buf[i] == '\r')) {
1267c478bd9Sstevel@tonic-gate 			if (i == 0) {
1277c478bd9Sstevel@tonic-gate 				(void) read(fd, junk, ptr->len);
1287c478bd9Sstevel@tonic-gate 				return (NONAME);
1297c478bd9Sstevel@tonic-gate 			} else
1307c478bd9Sstevel@tonic-gate 				return (GOODNAME);
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 	}	/* end for loop */
1337c478bd9Sstevel@tonic-gate 	/* end of input is encountered */
1347c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1357c478bd9Sstevel@tonic-gate 	debug("in process: EOF encountered");
1367c478bd9Sstevel@tonic-gate #endif
1377c478bd9Sstevel@tonic-gate 	exit(1);
1387c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  *	do_peek	- peek at the stream to get the data
143*3bb2c156SToomas Soome  *	int	fd;	fd to do the ioctl on
144*3bb2c156SToomas Soome  *	int	n;	maxlen of data to peek at
1457c478bd9Sstevel@tonic-gate  *		- this only called when POLLIN is detected,
1467c478bd9Sstevel@tonic-gate  *		- so there should always be something there
1477c478bd9Sstevel@tonic-gate  *		- return a ptr to the buf that contains the data
1487c478bd9Sstevel@tonic-gate  *		- return NULL if nothing to peek at
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate struct	strbuf	*
do_peek(int fd,int n)151*3bb2c156SToomas Soome do_peek(int fd, int n)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	int	 ret;
1547c478bd9Sstevel@tonic-gate 	static	 struct strpeek peek;
155*3bb2c156SToomas Soome 	struct strpeek *peekp;
1567c478bd9Sstevel@tonic-gate 	static	 char	buf[BUFSIZ];
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1597c478bd9Sstevel@tonic-gate 	debug("in do_peek");
1607c478bd9Sstevel@tonic-gate #endif
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	peekp = &peek;
1637c478bd9Sstevel@tonic-gate 	peekp->flags = 0;
1647c478bd9Sstevel@tonic-gate 	/* need to ask for ctl info to avoid bug in I_PEEK code */
1657c478bd9Sstevel@tonic-gate 	peekp->ctlbuf.maxlen = 1;
1667c478bd9Sstevel@tonic-gate 	peekp->ctlbuf.buf = buf;
1677c478bd9Sstevel@tonic-gate 	peekp->databuf.maxlen = n;
1687c478bd9Sstevel@tonic-gate 	peekp->databuf.buf = buf;
1697c478bd9Sstevel@tonic-gate 	ret = ioctl(fd, I_PEEK, &peek);
1707c478bd9Sstevel@tonic-gate 	if (ret == -1) {
1717c478bd9Sstevel@tonic-gate 		log("do_peek: I_PEEK failed: %s", errno);
1727c478bd9Sstevel@tonic-gate 		exit(1);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	if (ret == 0) {
175*3bb2c156SToomas Soome 		return (NULL);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	return (&(peekp->databuf));
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  *	sigint	- this is called when SIGINT is caught
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate void
sigint(int s __unused)184*3bb2c156SToomas Soome sigint(int s __unused)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	struct strbuf *ptr;
1877c478bd9Sstevel@tonic-gate 	char   junk[2];
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1907c478bd9Sstevel@tonic-gate 	debug("in sigint");
1917c478bd9Sstevel@tonic-gate #endif
1927c478bd9Sstevel@tonic-gate 	ptr = do_peek(0, 1);
1937c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {	/* somebody type <del> */
1947c478bd9Sstevel@tonic-gate 		interrupt = DEL;
195*3bb2c156SToomas Soome 	} else {
1967c478bd9Sstevel@tonic-gate 		if (ptr->buf[0] == '\0') {
1977c478bd9Sstevel@tonic-gate 			/* somebody type <brk> or frame error */
1987c478bd9Sstevel@tonic-gate 			(void) read(0, junk, 1);
1997c478bd9Sstevel@tonic-gate 			interrupt = BRK;
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate }
203