428
|
1 /* IBM has disclaimed copyright on this module. */
|
|
2
|
|
3 /* Synched up with: FSF 19.30. */
|
|
4
|
|
5 /***************************************************************/
|
|
6 /* */
|
|
7 /* Function: hftctl */
|
|
8 /* */
|
|
9 /* Syntax: */
|
|
10 /* #include <sys/ioctl.h> */
|
|
11 /* #include <sys/hft.h> */
|
|
12 /* */
|
|
13 /* int hftctl(fildes, request, arg ) */
|
|
14 /* int fildes, request; */
|
|
15 /* char *arg; */
|
|
16 /* */
|
|
17 /* Description: */
|
|
18 /* */
|
|
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 */
|
|
25 /* timing out. */
|
|
26 /* 5. places response in callers buffer ( just like */
|
|
27 /* ioctl. */
|
|
28 /* 6. returns fildes to its original mode */
|
|
29 /* */
|
|
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. */
|
|
35 /* */
|
|
36 /* Users of this program are free to modify it any way */
|
|
37 /* they want. */
|
|
38 /* */
|
|
39 /* Return Value: */
|
|
40 /* */
|
|
41 /* If ioctl fails, a value of -1 is returned and errno */
|
|
42 /* is set to indicate the error. */
|
|
43 /* */
|
|
44 /***************************************************************/
|
|
45
|
|
46 #include <config.h>
|
647
|
47 #include "lisp.h"
|
|
48 #include "sysfile.h"
|
428
|
49
|
|
50 #include <sys/signal.h>
|
|
51 #include <errno.h>
|
|
52
|
|
53 #include <stdio.h>
|
|
54 #include <fcntl.h>
|
|
55 #include <setjmp.h>
|
|
56 #include <sys/ioctl.h>
|
|
57 #include <sys/devinfo.h>
|
|
58 #include <termios.h>
|
|
59 #include <termio.h>
|
|
60 #include <sys/hft.h>
|
|
61 #include <sys/uio.h>
|
|
62 #include <sys/tty.h>
|
|
63 /* #include <sys/pty.h> */
|
|
64
|
|
65 #define REMOTE 0x01
|
|
66
|
|
67 #undef ioctl
|
|
68 static char SCCSid[] = "com/gnuemacs/src,3.1,9021-90/05/03-5/3/90";
|
|
69
|
|
70 /*************** LOCAL DEFINES **********************************/
|
|
71
|
|
72 #define QDEV ((HFQPDEVCH<<8)|HFQPDEVCL)
|
|
73 #define QLOC ((HFQLOCCH<<8)|HFQLOCCL)
|
|
74 #define QPS ((HFQPRESCH<<8)|HFQPRESCL)
|
|
75
|
|
76 #ifndef TCGETS
|
|
77 #define TCGETS TCGETA
|
|
78 #endif
|
|
79 #ifndef TCSETS
|
|
80 #define TCSETS TCSETA
|
|
81 #endif
|
|
82
|
|
83 /*************** EXTERNAL / GLOBAL DATA AREA ********************/
|
|
84
|
|
85 static int hfqry();
|
|
86 static int hfskbd();
|
|
87
|
|
88 extern int errno;
|
|
89 static JMP_BUF hftenv;
|
|
90 static int is_ack_vtd;
|
|
91 static SIGTYPE (*sav_alrm) ();
|
|
92 static struct hfctlreq req =
|
|
93 { 0x1b,'[','x',0,0,0,21,HFCTLREQCH,HFCTLREQCL};
|
|
94 static struct hfctlack ACK =
|
|
95 { 0x1b,'[','x',0,0,0,21,HFCTLACKCH,HFCTLACKCL};
|
|
96
|
|
97 /* FUNC signal(); */
|
|
98
|
|
99 /*************** LOCAL MACROS ***********************************/
|
|
100
|
|
101 #define HFTYPE(p) ((p->hf_typehi<<8)|(p->hf_typelo))
|
|
102
|
|
103 #define BYTE4(p) ((p)[0]<<24 | (p)[1]<<16 | (p)[2]<<8 | (p)[3])
|
|
104
|
|
105 /* read a buffer */
|
|
106 #define RD_BUF(f,p,l) \
|
|
107 while ((l)) \
|
647
|
108 if ((j = read ((f),(p),(l))) < 0) \
|
428
|
109 if (errno != EINTR) return (-1); \
|
|
110 else continue; \
|
|
111 else { (l) -= j; (p) += j; }
|
|
112
|
|
113 /*************** function prototypes ***************************/
|
|
114 #ifdef __STDC__
|
|
115 static GT_ACK (int fd, int req, char *buf);
|
|
116 static WR_REQ (int fd, int request, int cmdlen, char *cmd, int resplen);
|
|
117 static void hft_alrm(int sig);
|
|
118 #else
|
|
119 static GT_ACK ();
|
|
120 static WR_REQ ();
|
|
121 static void hft_alrm ();
|
|
122 #endif
|
|
123
|
|
124 /*************** HFTCTL FUNCTION *******************************/
|
|
125
|
|
126 hftctl (fd, request, arg)
|
|
127 int fd;
|
|
128 int request;
|
|
129 union {
|
|
130 struct hfintro *intro;
|
|
131 struct hfquery *query;
|
|
132 char *c;
|
|
133 } arg;
|
|
134 {
|
|
135
|
|
136 int i;
|
|
137 int fd_flag; /* fcntl flags */
|
|
138 union {
|
|
139 struct hfintro *cmd; /* p.cmd - intro des. */
|
|
140 struct hfqphdevc *ph; /* p.ph - physical dev.*/
|
|
141 char *c; /* p.c - char ptr */
|
|
142 } p; /* general pointer */
|
|
143 int pty_new; /* pty modes */
|
|
144 int pty_old;
|
|
145 int retcode;
|
|
146 struct termios term_new; /* terminal attributes */
|
|
147 struct termios term_old;
|
|
148 struct devinfo devInfo; /* defined in sys/devinfo.h */
|
|
149
|
|
150
|
|
151 if (ioctl (fd, IOCINFO, &devInfo) == -1) return(-1);
|
|
152
|
|
153 if (devInfo.devtype != DD_PSEU) /* is it a pty? */
|
|
154 return (ioctl(fd, request, arg)); /* no, do IOCTL */
|
|
155
|
|
156 /******* START PTY **************/
|
|
157 /** Pty found, possible HFT */
|
|
158 /** set new file des as raw */
|
|
159 /** as you can. */
|
|
160 /********************************/
|
|
161
|
|
162 /* Get current state of file */
|
|
163 /* descriptor & save */
|
|
164 if ((fd_flag = fcntl (fd, F_GETFL, 0)) == -1) return (-1);
|
|
165 if (ioctl (fd, TCGETS, &term_old) == -1) return (-1);
|
|
166 /* set terminal attr to raw */
|
|
167 /* and to delay on read */
|
|
168 pty_new = pty_old | REMOTE;
|
|
169 memcpy (&term_new, &term_old, sizeof (term_new));
|
|
170 term_new.c_iflag = 0;
|
|
171 term_new.c_oflag = 0;
|
|
172 term_new.c_lflag = 0;
|
|
173 /* term_new.c_line = 0; */
|
|
174 for (i = 1; i <= 5; i++)
|
|
175 term_new.c_cc[i] = 0;
|
|
176 term_new.c_cc[0] = -1;
|
|
177 ioctl (fd, TCSETS, &term_new);
|
|
178 if (fcntl (fd, F_SETFL, fd_flag & ~O_NDELAY) == -1)
|
|
179 return(-1);
|
|
180 /* call spacific function */
|
|
181 if (request == HFSKBD)
|
|
182 retcode = hfskbd (fd, request, arg.c);
|
|
183 else /* assume HFQUERY */
|
|
184 retcode = hfqry (fd, request, arg.c);
|
|
185
|
|
186 fcntl (fd, F_SETFL, fd_flag); /* reset terminal to original */
|
|
187 ioctl (fd, TCSETS, &term_old);
|
|
188
|
|
189
|
|
190 return (retcode); /* return error */
|
|
191 }
|
|
192
|
|
193 /*************** HFSKBD FUNCTION ******************************/
|
|
194 static int
|
|
195 hfskbd (fd, request, arg)
|
|
196 int fd;
|
|
197 int request;
|
|
198 struct hfbuf *arg;
|
|
199 {
|
|
200 WR_REQ(fd, request, arg->hf_buflen, arg->hf_bufp,0);
|
|
201 return (GT_ACK(fd, request, arg->hf_bufp));
|
|
202 }
|
|
203
|
|
204 /*************** HFQUERY FUNCTION ******************************/
|
|
205 static int
|
|
206 hfqry (fd, request, arg)
|
|
207 int fd;
|
|
208 int request;
|
|
209 struct hfquery *arg;
|
|
210 {
|
|
211 WR_REQ(fd, request, arg->hf_cmdlen, arg->hf_cmd, arg->hf_resplen);
|
|
212 return (GT_ACK(fd, request, arg->hf_resp));
|
|
213 }
|
|
214
|
|
215
|
|
216 /*************** GT_ACK FUNCTION ******************************/
|
|
217 static int
|
|
218 GT_ACK (fd, req, buf)
|
|
219 int fd;
|
|
220 int req;
|
|
221 char *buf;
|
|
222 {
|
|
223 struct hfctlack ack;
|
|
224 int i = sizeof (ack);
|
|
225 int j = 0;
|
|
226 union {
|
|
227 char *c;
|
|
228 struct hfctlack *ack;
|
|
229 } p;
|
|
230
|
|
231 is_ack_vtd = 0; /* flag no ACT VTD yet */
|
|
232
|
|
233 if (SETJMP (hftenv)) /* set environment in case */
|
|
234 { /* of time out */
|
|
235 errno = ENODEV; /* if time out, set errno */
|
|
236 return (-1); /* flag error */
|
|
237 }
|
|
238
|
|
239 alarm(3); /* time out in 3 secs */
|
|
240 sav_alrm = signal (SIGALRM, hft_alrm); /* prepare to catch time out */
|
|
241
|
|
242 p.ack = &ack;
|
|
243 while (! is_ack_vtd) /* do until valid ACK VTD */
|
|
244 {
|
|
245 RD_BUF(fd, p.c, i); /* read until a ACK VTD is fill*/
|
|
246
|
|
247 if (! memcmp (&ack, &ACK, sizeof (HFINTROSZ)) /* the ACK intro & */
|
|
248 && (ack.hf_request == req)) /* is it the response we want ?*/
|
|
249 { /* yes, ACK VTD found */
|
|
250 is_ack_vtd = 1; /* quickly, flag it */
|
|
251 break; /* get the %$%#@ out of here */
|
|
252 }
|
|
253
|
|
254 p.ack = &ack; /* no, then skip 1st */
|
|
255 ++p.c; /* char and start over */
|
|
256 i = sizeof (ack) - 1; /* one less ESC to cry over */
|
|
257
|
|
258 while ((*p.c != 0x1b) && i) /* scan for next ESC */
|
|
259 { ++p.c; --i; } /* if any */
|
|
260
|
|
261 (i ? memcpy (&ack, p.c, i) : 0); /* if any left over, then move */
|
|
262 p.ack = &ack; /* ESC to front of ack struct */
|
|
263 p.c += i; /* skip over what's been read */
|
|
264 i = sizeof (ack) - i; /* set what's left to be read */
|
|
265 } /***** TRY AGAIN */
|
|
266
|
|
267 alarm(0); /* ACK VTD received, reset alrm*/
|
|
268 signal (SIGALRM, sav_alrm); /* reset signal */
|
|
269
|
|
270 if (i = ack.hf_arg_len) /* any data following ? */
|
|
271 { /* yes, */
|
|
272 RD_BUF(fd,buf,i); /* read until it is received */
|
|
273 }
|
|
274
|
|
275 if (errno = ack.hf_retcode) /* set errno based on returned */
|
|
276 return (-1); /* code, if 0, then no error */
|
|
277 else
|
|
278 return (0); /* if set, then error returned */
|
|
279 }
|
|
280
|
|
281 /*************** HFT_ALRM FUNCTION ******************************/
|
|
282 static void
|
|
283 hft_alrm (sig) /* Function hft_alrm - handle */
|
|
284 int sig; /* alarm signal */
|
|
285 {
|
|
286 signal (SIGALRM, sav_alrm); /* reset to previous */
|
|
287
|
|
288 if (is_ack_vtd) /* has ack vtd arrived ? */
|
|
289 return; /* yes, then continue */
|
|
290 else /* no, then return with error */
|
|
291 LONGJMP (hftenv, -1);
|
|
292
|
|
293 }
|
|
294
|
|
295 /*********************************************************************/
|
|
296 /*** ***/
|
|
297 /*** NOTE: Both the HFCTLREQ and the arg structure should be ***/
|
|
298 /*** sent in one io write operation. If terminal ***/
|
|
299 /*** emulators are in NODELAY mode then multiple writes ***/
|
|
300 /*** may cause bogus information to be read by the emulator ***/
|
|
301 /*** depending on the timing. ***/
|
|
302 /*** ***/
|
|
303 /*********************************************************************/
|
|
304
|
|
305 static int
|
|
306 WR_REQ (fd, request, cmdlen, cmd, resplen)
|
|
307 int fd;
|
|
308 int request;
|
|
309 int cmdlen;
|
|
310 char *cmd;
|
|
311 int resplen;
|
|
312 {
|
|
313 struct {
|
|
314 char *c;
|
|
315 struct hfctlreq *req;
|
|
316 } p;
|
|
317 int size;
|
|
318
|
|
319 req.hf_request = request;
|
|
320 req.hf_arg_len = cmdlen;
|
|
321 req.hf_rsp_len = resplen;
|
|
322
|
|
323 if (cmdlen) /* if arg structure to pass */
|
|
324 {
|
|
325 size = sizeof (struct hfctlreq) + cmdlen;
|
2367
|
326 if ((p.c = xmalloc (size)) == NULL) /* malloc one area */
|
428
|
327 return (-1);
|
|
328
|
|
329 memcpy (p.c, &req, sizeof (req)); /* copy CTL REQ struct */
|
|
330 memcpy (p.c + sizeof (req), cmd, cmdlen); /* copy arg struct */
|
|
331 }
|
|
332 else
|
|
333 {
|
|
334 p.req = &req; /* otherwise use only CTL REQ */
|
|
335 size = sizeof (req);
|
|
336 }
|
|
337
|
|
338 /* write request to terminal */
|
|
339 if (write(fd,p.c,size) == -1) return (-1);
|
|
340 if (p.req != &req) /* free if allocated */
|
1726
|
341 xfree (p.c, char *);
|
428
|
342 return (0);
|
|
343 }
|