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