1 /* IBM has disclaimed copyright on this module. */
3 /* Synched up with: FSF 19.30. */
5 /***************************************************************/
10 /* #include <sys/ioctl.h> */
11 /* #include <sys/hft.h> */
13 /* int hftctl(fildes, request, arg ) */
14 /* int fildes, request; */
19 /* Does the following: */
20 /* 1. determines if fildes is pty */
21 /* does normal ioctl it is not */
22 /* 2. places fildes into raw mode */
23 /* 3. converts ioctl arguments to data stream */
24 /* 4. waits for 2 secs for acknowledgement before */
26 /* 5. places response in callers buffer ( just like */
28 /* 6. returns fildes to its original mode */
30 /* User of this program should review steps 1,4, and 3. */
31 /* hftctl makes no check on the request type. It must be */
32 /* a HFT ioctl that is supported remotely. */
33 /* This program will use the SIGALRM and alarm(2). Any */
34 /* Previous alarms are lost. */
36 /* Users of this program are free to modify it any way */
41 /* If ioctl fails, a value of -1 is returned and errno */
42 /* is set to indicate the error. */
44 /***************************************************************/
47 #include "lisp.h" /* encapsulated open, close, read, write */
49 #include <sys/signal.h>
55 #include <sys/ioctl.h>
56 #include <sys/devinfo.h>
62 /* #include <sys/pty.h> */
67 static char SCCSid[] = "com/gnuemacs/src,3.1,9021-90/05/03-5/3/90";
69 /*************** LOCAL DEFINES **********************************/
71 #define QDEV ((HFQPDEVCH<<8)|HFQPDEVCL)
72 #define QLOC ((HFQLOCCH<<8)|HFQLOCCL)
73 #define QPS ((HFQPRESCH<<8)|HFQPRESCL)
82 /*************** EXTERNAL / GLOBAL DATA AREA ********************/
88 static JMP_BUF hftenv;
89 static int is_ack_vtd;
90 static SIGTYPE (*sav_alrm) ();
91 static struct hfctlreq req =
92 { 0x1b,'[','x',0,0,0,21,HFCTLREQCH,HFCTLREQCL};
93 static struct hfctlack ACK =
94 { 0x1b,'[','x',0,0,0,21,HFCTLACKCH,HFCTLACKCL};
98 /*************** LOCAL MACROS ***********************************/
100 #define HFTYPE(p) ((p->hf_typehi<<8)|(p->hf_typelo))
102 #define BYTE4(p) ((p)[0]<<24 | (p)[1]<<16 | (p)[2]<<8 | (p)[3])
105 #define RD_BUF(f,p,l) \
107 if ((j = read((f),(p),(l))) < 0) \
108 if (errno != EINTR) return (-1); \
110 else { (l) -= j; (p) += j; }
112 /*************** function prototypes ***************************/
114 static GT_ACK (int fd, int req, char *buf);
115 static WR_REQ (int fd, int request, int cmdlen, char *cmd, int resplen);
116 static void hft_alrm(int sig);
120 static void hft_alrm ();
123 /*************** HFTCTL FUNCTION *******************************/
125 hftctl (fd, request, arg)
129 struct hfintro *intro;
130 struct hfquery *query;
136 int fd_flag; /* fcntl flags */
138 struct hfintro *cmd; /* p.cmd - intro des. */
139 struct hfqphdevc *ph; /* p.ph - physical dev.*/
140 char *c; /* p.c - char ptr */
141 } p; /* general pointer */
142 int pty_new; /* pty modes */
145 struct termios term_new; /* terminal attributes */
146 struct termios term_old;
147 struct devinfo devInfo; /* defined in sys/devinfo.h */
150 if (ioctl (fd, IOCINFO, &devInfo) == -1) return(-1);
152 if (devInfo.devtype != DD_PSEU) /* is it a pty? */
153 return (ioctl(fd, request, arg)); /* no, do IOCTL */
155 /******* START PTY **************/
156 /** Pty found, possible HFT */
157 /** set new file des as raw */
159 /********************************/
161 /* Get current state of file */
162 /* descriptor & save */
163 if ((fd_flag = fcntl (fd, F_GETFL, 0)) == -1) return (-1);
164 if (ioctl (fd, TCGETS, &term_old) == -1) return (-1);
165 /* set terminal attr to raw */
166 /* and to delay on read */
167 pty_new = pty_old | REMOTE;
168 memcpy (&term_new, &term_old, sizeof (term_new));
169 term_new.c_iflag = 0;
170 term_new.c_oflag = 0;
171 term_new.c_lflag = 0;
172 /* term_new.c_line = 0; */
173 for (i = 1; i <= 5; i++)
174 term_new.c_cc[i] = 0;
175 term_new.c_cc[0] = -1;
176 ioctl (fd, TCSETS, &term_new);
177 if (fcntl (fd, F_SETFL, fd_flag & ~O_NDELAY) == -1)
179 /* call spacific function */
180 if (request == HFSKBD)
181 retcode = hfskbd (fd, request, arg.c);
182 else /* assume HFQUERY */
183 retcode = hfqry (fd, request, arg.c);
185 fcntl (fd, F_SETFL, fd_flag); /* reset terminal to original */
186 ioctl (fd, TCSETS, &term_old);
189 return (retcode); /* return error */
192 /*************** HFSKBD FUNCTION ******************************/
194 hfskbd (fd, request, arg)
199 WR_REQ(fd, request, arg->hf_buflen, arg->hf_bufp,0);
200 return (GT_ACK(fd, request, arg->hf_bufp));
203 /*************** HFQUERY FUNCTION ******************************/
205 hfqry (fd, request, arg)
210 WR_REQ(fd, request, arg->hf_cmdlen, arg->hf_cmd, arg->hf_resplen);
211 return (GT_ACK(fd, request, arg->hf_resp));
215 /*************** GT_ACK FUNCTION ******************************/
217 GT_ACK (fd, req, buf)
223 int i = sizeof (ack);
227 struct hfctlack *ack;
230 is_ack_vtd = 0; /* flag no ACT VTD yet */
232 if (SETJMP (hftenv)) /* set environment in case */
234 errno = ENODEV; /* if time out, set errno */
235 return (-1); /* flag error */
238 alarm(3); /* time out in 3 secs */
239 sav_alrm = signal (SIGALRM, hft_alrm); /* prepare to catch time out */
242 while (! is_ack_vtd) /* do until valid ACK VTD */
244 RD_BUF(fd, p.c, i); /* read until a ACK VTD is fill*/
246 if (! memcmp (&ack, &ACK, sizeof (HFINTROSZ)) /* the ACK intro & */
247 && (ack.hf_request == req)) /* is it the response we want ?*/
248 { /* yes, ACK VTD found */
249 is_ack_vtd = 1; /* quickly, flag it */
250 break; /* get the %$%#@ out of here */
253 p.ack = &ack; /* no, then skip 1st */
254 ++p.c; /* char and start over */
255 i = sizeof (ack) - 1; /* one less ESC to cry over */
257 while ((*p.c != 0x1b) && i) /* scan for next ESC */
258 { ++p.c; --i; } /* if any */
260 (i ? memcpy (&ack, p.c, i) : 0); /* if any left over, then move */
261 p.ack = &ack; /* ESC to front of ack struct */
262 p.c += i; /* skip over what's been read */
263 i = sizeof (ack) - i; /* set what's left to be read */
264 } /***** TRY AGAIN */
266 alarm(0); /* ACK VTD received, reset alrm*/
267 signal (SIGALRM, sav_alrm); /* reset signal */
269 if (i = ack.hf_arg_len) /* any data following ? */
271 RD_BUF(fd,buf,i); /* read until it is received */
274 if (errno = ack.hf_retcode) /* set errno based on returned */
275 return (-1); /* code, if 0, then no error */
277 return (0); /* if set, then error returned */
280 /*************** HFT_ALRM FUNCTION ******************************/
282 hft_alrm (sig) /* Function hft_alrm - handle */
283 int sig; /* alarm signal */
285 signal (SIGALRM, sav_alrm); /* reset to previous */
287 if (is_ack_vtd) /* has ack vtd arrived ? */
288 return; /* yes, then continue */
289 else /* no, then return with error */
290 LONGJMP (hftenv, -1);
294 /*********************************************************************/
296 /*** NOTE: Both the HFCTLREQ and the arg structure should be ***/
297 /*** sent in one io write operation. If terminal ***/
298 /*** emulators are in NODELAY mode then multiple writes ***/
299 /*** may cause bogus information to be read by the emulator ***/
300 /*** depending on the timing. ***/
302 /*********************************************************************/
305 WR_REQ (fd, request, cmdlen, cmd, resplen)
314 struct hfctlreq *req;
318 req.hf_request = request;
319 req.hf_arg_len = cmdlen;
320 req.hf_rsp_len = resplen;
322 if (cmdlen) /* if arg structure to pass */
324 size = sizeof (struct hfctlreq) + cmdlen;
325 if ((p.c = xmalloc(size)) == NULL) /* malloc one area */
328 memcpy (p.c, &req, sizeof (req)); /* copy CTL REQ struct */
329 memcpy (p.c + sizeof (req), cmd, cmdlen); /* copy arg struct */
333 p.req = &req; /* otherwise use only CTL REQ */
337 /* write request to terminal */
338 if (write(fd,p.c,size) == -1) return (-1);
339 if (p.req != &req) /* free if allocated */