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 #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <ctype.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
39*7c478bd9Sstevel@tonic-gate #include <poll.h>
40*7c478bd9Sstevel@tonic-gate #include <signal.h>
41*7c478bd9Sstevel@tonic-gate #include <errno.h>
42*7c478bd9Sstevel@tonic-gate #include "ttymon.h"
43*7c478bd9Sstevel@tonic-gate #include "tmstruct.h"
44*7c478bd9Sstevel@tonic-gate #include "tmextern.h"
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate #define BRK 1
47*7c478bd9Sstevel@tonic-gate #define DEL 2
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate struct strbuf *do_peek();
50*7c478bd9Sstevel@tonic-gate static int process();
51*7c478bd9Sstevel@tonic-gate extern void sigint();
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate static int interrupt;
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate /*
56*7c478bd9Sstevel@tonic-gate * poll_data - it polls the device, waiting for data
57*7c478bd9Sstevel@tonic-gate * - return BADSPEED it <brk> is received
58*7c478bd9Sstevel@tonic-gate * - return the result of process if data is received
59*7c478bd9Sstevel@tonic-gate * - write a newline if <del> is received
60*7c478bd9Sstevel@tonic-gate * - exit if hangup is received
61*7c478bd9Sstevel@tonic-gate */
62*7c478bd9Sstevel@tonic-gate int
poll_data()63*7c478bd9Sstevel@tonic-gate poll_data()
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate int j;
66*7c478bd9Sstevel@tonic-gate struct strbuf *ptr;
67*7c478bd9Sstevel@tonic-gate struct pollfd fds[1];
68*7c478bd9Sstevel@tonic-gate struct sigaction sigact;
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
71*7c478bd9Sstevel@tonic-gate debug("in poll_data");
72*7c478bd9Sstevel@tonic-gate #endif
73*7c478bd9Sstevel@tonic-gate if (peek_ptr != NULL) {
74*7c478bd9Sstevel@tonic-gate for(j=0; j<peek_ptr->len; j++) peek_ptr->buf[j] &= 0x7F;
75*7c478bd9Sstevel@tonic-gate return(process(0,peek_ptr));
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate fds[0].fd = 0;
78*7c478bd9Sstevel@tonic-gate fds[0].events = POLLIN;
79*7c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
80*7c478bd9Sstevel@tonic-gate sigact.sa_handler = sigint;
81*7c478bd9Sstevel@tonic-gate (void)sigemptyset(&sigact.sa_mask);
82*7c478bd9Sstevel@tonic-gate (void)sigaddset(&sigact.sa_mask, SIGINT);
83*7c478bd9Sstevel@tonic-gate (void)sigaction(SIGINT, &sigact, NULL);
84*7c478bd9Sstevel@tonic-gate for (;;) {
85*7c478bd9Sstevel@tonic-gate interrupt = 0;
86*7c478bd9Sstevel@tonic-gate if ((j = poll(fds,1,-1)) == -1) {
87*7c478bd9Sstevel@tonic-gate if (interrupt == BRK) {
88*7c478bd9Sstevel@tonic-gate return(BADSPEED);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate if (interrupt == DEL) { /* XXX revisit kmd */
91*7c478bd9Sstevel@tonic-gate return(BADSPEED);
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate else if (j > 0) {
95*7c478bd9Sstevel@tonic-gate if (fds[0].revents & POLLHUP) {
96*7c478bd9Sstevel@tonic-gate log( "POLLHUP received, about to exit");
97*7c478bd9Sstevel@tonic-gate exit(1);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate if (fds[0].revents & POLLIN) {
100*7c478bd9Sstevel@tonic-gate ptr = do_peek(fds[0].fd, 255);
101*7c478bd9Sstevel@tonic-gate if (ptr != NULL) {
102*7c478bd9Sstevel@tonic-gate return(process(fds[0].fd,ptr));
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate /*
110*7c478bd9Sstevel@tonic-gate * process - process the data
111*7c478bd9Sstevel@tonic-gate * - return GOODNAME if it is a non-empty line
112*7c478bd9Sstevel@tonic-gate * - return NONAME if a <CR> is received
113*7c478bd9Sstevel@tonic-gate * - return BADNAME if zero byte is detected
114*7c478bd9Sstevel@tonic-gate * - except the case of GOODNAME, data will be pulled out
115*7c478bd9Sstevel@tonic-gate * of the stream
116*7c478bd9Sstevel@tonic-gate */
117*7c478bd9Sstevel@tonic-gate static int
process(int fd,struct strbuf * ptr)118*7c478bd9Sstevel@tonic-gate process(
119*7c478bd9Sstevel@tonic-gate int fd, /* fd to read data from if necessary */
120*7c478bd9Sstevel@tonic-gate struct strbuf *ptr) /* ptr that holds data in ptr->buf */
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate unsigned i;
123*7c478bd9Sstevel@tonic-gate char junk[BUFSIZ];
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate for (i = 0; i < ptr->len; i++) {
126*7c478bd9Sstevel@tonic-gate if (ptr->buf[i] == '\0') {
127*7c478bd9Sstevel@tonic-gate (void) read(fd, junk, i+1);
128*7c478bd9Sstevel@tonic-gate return (BADSPEED);
129*7c478bd9Sstevel@tonic-gate } else if ((ptr->buf[i] == '\n') || (ptr->buf[i] == '\r')) {
130*7c478bd9Sstevel@tonic-gate if (i == 0) {
131*7c478bd9Sstevel@tonic-gate (void) read(fd, junk, ptr->len);
132*7c478bd9Sstevel@tonic-gate return (NONAME);
133*7c478bd9Sstevel@tonic-gate } else
134*7c478bd9Sstevel@tonic-gate return (GOODNAME);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate } /* end for loop */
137*7c478bd9Sstevel@tonic-gate /* end of input is encountered */
138*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
139*7c478bd9Sstevel@tonic-gate debug("in process: EOF encountered");
140*7c478bd9Sstevel@tonic-gate #endif
141*7c478bd9Sstevel@tonic-gate exit(1);
142*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate * do_peek - peek at the stream to get the data
147*7c478bd9Sstevel@tonic-gate * - this only called when POLLIN is detected,
148*7c478bd9Sstevel@tonic-gate * - so there should always be something there
149*7c478bd9Sstevel@tonic-gate * - return a ptr to the buf that contains the data
150*7c478bd9Sstevel@tonic-gate * - return NULL if nothing to peek at
151*7c478bd9Sstevel@tonic-gate */
152*7c478bd9Sstevel@tonic-gate struct strbuf *
do_peek(fd,n)153*7c478bd9Sstevel@tonic-gate do_peek(fd,n)
154*7c478bd9Sstevel@tonic-gate int fd; /* fd to do the ioctl on */
155*7c478bd9Sstevel@tonic-gate int n; /* maxlen of data to peek at */
156*7c478bd9Sstevel@tonic-gate {
157*7c478bd9Sstevel@tonic-gate int ret;
158*7c478bd9Sstevel@tonic-gate static struct strpeek peek;
159*7c478bd9Sstevel@tonic-gate register struct strpeek *peekp;
160*7c478bd9Sstevel@tonic-gate static char buf[BUFSIZ];
161*7c478bd9Sstevel@tonic-gate
162*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
163*7c478bd9Sstevel@tonic-gate debug("in do_peek");
164*7c478bd9Sstevel@tonic-gate #endif
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate peekp = &peek;
167*7c478bd9Sstevel@tonic-gate peekp->flags = 0;
168*7c478bd9Sstevel@tonic-gate /* need to ask for ctl info to avoid bug in I_PEEK code */
169*7c478bd9Sstevel@tonic-gate peekp->ctlbuf.maxlen = 1;
170*7c478bd9Sstevel@tonic-gate peekp->ctlbuf.buf = buf;
171*7c478bd9Sstevel@tonic-gate peekp->databuf.maxlen = n;
172*7c478bd9Sstevel@tonic-gate peekp->databuf.buf = buf;
173*7c478bd9Sstevel@tonic-gate ret = ioctl(fd, I_PEEK, &peek);
174*7c478bd9Sstevel@tonic-gate if (ret == -1) {
175*7c478bd9Sstevel@tonic-gate log("do_peek: I_PEEK failed: %s", errno);
176*7c478bd9Sstevel@tonic-gate exit(1);
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate if (ret == 0) {
179*7c478bd9Sstevel@tonic-gate return( (struct strbuf *)NULL );
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate return(&(peekp->databuf));
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate
184*7c478bd9Sstevel@tonic-gate /*
185*7c478bd9Sstevel@tonic-gate * sigint - this is called when SIGINT is caught
186*7c478bd9Sstevel@tonic-gate */
187*7c478bd9Sstevel@tonic-gate void
sigint()188*7c478bd9Sstevel@tonic-gate sigint()
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate struct strbuf *ptr;
191*7c478bd9Sstevel@tonic-gate char junk[2];
192*7c478bd9Sstevel@tonic-gate
193*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
194*7c478bd9Sstevel@tonic-gate debug("in sigint");
195*7c478bd9Sstevel@tonic-gate #endif
196*7c478bd9Sstevel@tonic-gate ptr = do_peek(0, 1);
197*7c478bd9Sstevel@tonic-gate if (ptr == NULL) { /* somebody type <del> */
198*7c478bd9Sstevel@tonic-gate interrupt = DEL;
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate else {
201*7c478bd9Sstevel@tonic-gate if (ptr->buf[0] == '\0') {
202*7c478bd9Sstevel@tonic-gate /* somebody type <brk> or frame error */
203*7c478bd9Sstevel@tonic-gate (void)read(0,junk,1);
204*7c478bd9Sstevel@tonic-gate interrupt = BRK;
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate
209