xref: /titanic_41/usr/src/cmd/ttymon/tmpeek.c (revision 8d483882aa3390058094b043f3d62187b5d1de03)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <sys/types.h>
38 #include <sys/stropts.h>
39 #include <poll.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include "ttymon.h"
43 #include "tmstruct.h"
44 #include "tmextern.h"
45 
46 #define BRK	1
47 #define DEL	2
48 
49 struct	strbuf *do_peek();
50 static	int	process();
51 extern	void	sigint();
52 
53 static	int	interrupt;
54 
55 /*
56  *	poll_data	- it polls the device, waiting for data
57  *			- return BADSPEED it <brk> is received
58  *			- return the result of process if data is received
59  *			- write a newline if <del> is received
60  *			- exit if hangup is received
61  */
62 int
63 poll_data()
64 {
65 	int j;
66 	struct strbuf *ptr;
67 	struct pollfd fds[1];
68 	struct sigaction sigact;
69 
70 #ifdef	DEBUG
71 	debug("in poll_data");
72 #endif
73 	if (peek_ptr != NULL) {
74 		for(j=0; j<peek_ptr->len; j++) peek_ptr->buf[j] &= 0x7F;
75 		return(process(0,peek_ptr));
76 	}
77 	fds[0].fd = 0;
78 	fds[0].events = POLLIN;
79 	sigact.sa_flags = 0;
80 	sigact.sa_handler = sigint;
81 	(void)sigemptyset(&sigact.sa_mask);
82 	(void)sigaddset(&sigact.sa_mask, SIGINT);
83 	(void)sigaction(SIGINT, &sigact, NULL);
84 	for (;;) {
85 		interrupt = 0;
86 		if ((j = poll(fds,1,-1)) == -1) {
87 			if (interrupt == BRK) {
88 				return(BADSPEED);
89 			}
90 			if (interrupt == DEL) { /* XXX revisit kmd */
91 				return(BADSPEED);
92 			}
93 		}
94 		else if (j > 0) {
95 			if (fds[0].revents & POLLHUP) {
96 				log( "POLLHUP received, about to exit");
97 				exit(1);
98 			}
99 			if (fds[0].revents & POLLIN) {
100 				ptr = do_peek(fds[0].fd, 255);
101 				if (ptr != NULL) {
102 					return(process(fds[0].fd,ptr));
103 				}
104 			}
105 		}
106 	}
107 }
108 
109 /*
110  *	process	- process the data
111  *		- return GOODNAME if it is a non-empty line
112  *		- return NONAME if a <CR> is received
113  *		- return BADNAME if zero byte is detected
114  *		- except the case of GOODNAME, data will be pulled out
115  *		  of the stream
116  */
117 static int
118 process(
119 	int	fd,		/* fd to read data from if necessary 	*/
120 	struct strbuf *ptr)	/* ptr that holds data in ptr->buf 	*/
121 {
122 	unsigned i;
123 	char	junk[BUFSIZ];
124 
125 	for (i = 0; i < ptr->len; i++) {
126 		if (ptr->buf[i] == '\0') {
127 			(void) read(fd, junk, i+1);
128 			return (BADSPEED);
129 		} else if ((ptr->buf[i] == '\n') || (ptr->buf[i] == '\r')) {
130 			if (i == 0) {
131 				(void) read(fd, junk, ptr->len);
132 				return (NONAME);
133 			} else
134 				return (GOODNAME);
135 		}
136 	}	/* end for loop */
137 	/* end of input is encountered */
138 #ifdef	DEBUG
139 	debug("in process: EOF encountered");
140 #endif
141 	exit(1);
142 	/*NOTREACHED*/
143 }
144 
145 /*
146  *	do_peek	- peek at the stream to get the data
147  *		- this only called when POLLIN is detected,
148  *		- so there should always be something there
149  *		- return a ptr to the buf that contains the data
150  *		- return NULL if nothing to peek at
151  */
152 struct	strbuf	*
153 do_peek(fd,n)
154 int	fd;	/* fd to do the ioctl on */
155 int	n;	/* maxlen of data to peek at */
156 {
157 	int	 ret;
158 	static 	 struct strpeek peek;
159 	register struct strpeek *peekp;
160 	static	 char	buf[BUFSIZ];
161 
162 #ifdef	DEBUG
163 	debug("in do_peek");
164 #endif
165 
166 	peekp = &peek;
167 	peekp->flags = 0;
168 	/* need to ask for ctl info to avoid bug in I_PEEK code */
169 	peekp->ctlbuf.maxlen = 1;
170 	peekp->ctlbuf.buf = buf;
171 	peekp->databuf.maxlen = n;
172 	peekp->databuf.buf = buf;
173 	ret = ioctl(fd, I_PEEK, &peek);
174 	if (ret == -1) {
175 		log("do_peek: I_PEEK failed: %s", errno);
176 		exit(1);
177 	}
178 	if (ret == 0) {
179 		return( (struct strbuf *)NULL );
180 	}
181 	return(&(peekp->databuf));
182 }
183 
184 /*
185  *	sigint	- this is called when SIGINT is caught
186  */
187 void
188 sigint()
189 {
190 	struct strbuf *ptr;
191 	char   junk[2];
192 
193 #ifdef	DEBUG
194 	debug("in sigint");
195 #endif
196 	ptr = do_peek(0, 1);
197 	if (ptr == NULL) {	/* somebody type <del> */
198 		interrupt = DEL;
199 	}
200 	else {
201 		if (ptr->buf[0] == '\0') {
202 			/* somebody type <brk> or frame error */
203 			(void)read(0,junk,1);
204 			interrupt = BRK;
205 		}
206 	}
207 }
208 
209