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 */
149 if (ioctl(fd, IOCINFO, &devInfo) == -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)
165 if (ioctl(fd, TCGETS, &term_old) == -1)
167 /* set terminal attr to raw */
168 /* and to delay on read */
169 pty_new = pty_old | REMOTE;
170 memcpy(&term_new, &term_old, sizeof(term_new));
171 term_new.c_iflag = 0;
172 term_new.c_oflag = 0;
173 term_new.c_lflag = 0;
174 /* term_new.c_line = 0; */
175 for (i = 1; i <= 5; i++)
176 term_new.c_cc[i] = 0;
177 term_new.c_cc[0] = -1;
178 ioctl(fd, TCSETS, &term_new);
179 if (fcntl(fd, F_SETFL, fd_flag & ~O_NDELAY) == -1)
181 /* call spacific function */
182 if (request == HFSKBD)
183 retcode = hfskbd(fd, request, arg.c);
184 else /* assume HFQUERY */
185 retcode = hfqry(fd, request, arg.c);
187 fcntl(fd, F_SETFL, fd_flag); /* reset terminal to original */
188 ioctl(fd, TCSETS, &term_old);
190 return (retcode); /* return error */
193 /*************** HFSKBD FUNCTION ******************************/
194 static int 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 ******************************/
204 static int hfqry(fd, request, arg)
209 WR_REQ(fd, request, arg->hf_cmdlen, arg->hf_cmd, arg->hf_resplen);
210 return (GT_ACK(fd, request, arg->hf_resp));
213 /*************** GT_ACK FUNCTION ******************************/
214 static int GT_ACK(fd, req, buf)
224 struct hfctlack *ack;
227 is_ack_vtd = 0; /* flag no ACT VTD yet */
229 if (SETJMP(hftenv)) { /* set environment in case *//* of time out */
230 errno = ENODEV; /* if time out, set errno */
231 return (-1); /* flag error */
234 alarm(3); /* time out in 3 secs */
235 sav_alrm = signal(SIGALRM, hft_alrm); /* prepare to catch time out */
238 while (!is_ack_vtd) { /* do until valid ACK VTD */
239 RD_BUF(fd, p.c, i); /* read until a ACK VTD is fill */
241 if (!memcmp(&ack, &ACK, sizeof(HFINTROSZ)) /* the ACK intro & */
242 &&(ack.hf_request == req)) { /* is it the response we want ? *//* yes, ACK VTD found */
243 is_ack_vtd = 1; /* quickly, flag it */
244 break; /* get the %$%#@ out of here */
247 p.ack = &ack; /* no, then skip 1st */
248 ++p.c; /* char and start over */
249 i = sizeof(ack) - 1; /* one less ESC to cry over */
251 while ((*p.c != 0x1b) && i) { /* scan for next ESC */
256 (i ? memcpy(&ack, p.c, i) : 0); /* if any left over, then move */
257 p.ack = &ack; /* ESC to front of ack struct */
258 p.c += i; /* skip over what's been read */
259 i = sizeof(ack) - i; /* set what's left to be read */
260 } /***** TRY AGAIN */
262 alarm(0); /* ACK VTD received, reset alrm */
263 signal(SIGALRM, sav_alrm); /* reset signal */
265 if (i = ack.hf_arg_len) { /* any data following ? *//* yes, */
266 RD_BUF(fd, buf, i); /* read until it is received */
269 if (errno = ack.hf_retcode) /* set errno based on returned */
270 return (-1); /* code, if 0, then no error */
272 return (0); /* if set, then error returned */
275 /*************** HFT_ALRM FUNCTION ******************************/
276 static void hft_alrm(sig) /* Function hft_alrm - handle */
277 int sig; /* alarm signal */
279 signal(SIGALRM, sav_alrm); /* reset to previous */
281 if (is_ack_vtd) /* has ack vtd arrived ? */
282 return; /* yes, then continue */
283 else /* no, then return with error */
288 /*********************************************************************/
290 /*** NOTE: Both the HFCTLREQ and the arg structure should be ***/
291 /*** sent in one io write operation. If terminal ***/
292 /*** emulators are in NODELAY mode then multiple writes ***/
293 /*** may cause bogus information to be read by the emulator ***/
294 /*** depending on the timing. ***/
296 /*********************************************************************/
298 static int WR_REQ(fd, request, cmdlen, cmd, resplen)
307 struct hfctlreq *req;
311 req.hf_request = request;
312 req.hf_arg_len = cmdlen;
313 req.hf_rsp_len = resplen;
315 if (cmdlen) { /* if arg structure to pass */
316 size = sizeof(struct hfctlreq) + cmdlen;
317 if ((p.c = xmalloc(size)) == NULL) /* malloc one area */
320 memcpy(p.c, &req, sizeof(req)); /* copy CTL REQ struct */
321 memcpy(p.c + sizeof(req), cmd, cmdlen); /* copy arg struct */
323 p.req = &req; /* otherwise use only CTL REQ */
327 /* write request to terminal */
328 if (write(fd, p.c, size) == -1)
330 if (p.req != &req) /* free if allocated */