1 /* IBM has disclaimed copyright on this module. */
3 /***************************************************************/
8 /* #include <sys/ioctl.h> */
9 /* #include <sys/hft.h> */
11 /* int hftctl(fildes, request, arg ) */
12 /* int fildes, request; */
17 /* Does the following: */
18 /* 1. determines if fildes is pty */
19 /* does normal ioctl it is not */
20 /* 2. places fildes into raw mode */
21 /* 3. converts ioctl arguments to datastream */
22 /* 4. waits for 2 secs for acknowledgement before */
24 /* 5. places response in callers buffer ( just like */
26 /* 6. returns fildes to its original mode */
28 /* User of this program should review steps 1,4, and 3. */
29 /* hftctl makes no check on the request type. It must be */
30 /* a HFT ioctl that is supported remotely. */
31 /* This program will use the SIGALRM and alarm(2). Any */
32 /* Previous alarms are lost. */
34 /* Users of this program are free to modify it any way */
39 /* If ioctl fails, a value of -1 is returned and errno */
40 /* is set to indicate the error. */
42 /***************************************************************/
45 #include <sys/signal.h>
50 #include <sys/ioctl.h>
51 #include <sys/devinfo.h>
57 /* #include <sys/pty.h> */
62 static char SCCSid
[] = "com/gnuemacs/src,3.1,9021-90/05/03-5/3/90";
64 /*************** LOCAL DEFINES **********************************/
66 #define QDEV ((HFQPDEVCH<<8)|HFQPDEVCL)
67 #define QLOC ((HFQLOCCH<<8)|HFQLOCCL)
68 #define QPS ((HFQPRESCH<<8)|HFQPRESCL)
77 /*************** EXTERNAL / GLOBAL DATA AREA ********************/
84 static jmp_buf hftenv
;
85 static int is_ack_vtd
;
86 static SIGTYPE (*sav_alrm
) ();
87 static struct hfctlreq req
=
88 { 0x1b,'[','x',0,0,0,21,HFCTLREQCH
,HFCTLREQCL
};
89 static struct hfctlack ACK
=
90 { 0x1b,'[','x',0,0,0,21,HFCTLACKCH
,HFCTLACKCL
};
94 /*************** LOCAL MACROS ***********************************/
96 #define HFTYPE(p) ((p->hf_typehi<<8)|(p->hf_typelo))
98 #define BYTE4(p) ((p)[0]<<24 | (p)[1]<<16 | (p)[2]<<8 | (p)[3])
101 #define RD_BUF(f,p,l) \
103 if ((j = emacs_read (f, p, l)) < 0) \
104 if (errno != EINTR) return (-1); \
106 else { (l) -= j; (p) += j; }
108 /*************** function prototypes ***************************/
110 static GT_ACK (int fd
, int req
, char *buf
);
111 static WR_REQ (int fd
, int request
, int cmdlen
, char *cmd
, int resplen
);
112 static void hft_alrm(int sig
);
116 static void hft_alrm ();
119 /*************** HFTCTL FUNCTION *******************************/
121 hftctl (fd
, request
, arg
)
125 struct hfintro
*intro
;
126 struct hfquery
*query
;
132 int fd_flag
; /* fcntl flags */
134 struct hfintro
*cmd
; /* p.cmd - intro des. */
135 struct hfqphdevc
*ph
; /* p.ph - physical dev.*/
136 char *c
; /* p.c - char ptr */
137 } p
; /* general pointer */
138 int pty_new
; /* pty modes */
141 struct termios term_new
; /* terminal attributes */
142 struct termios term_old
;
143 struct devinfo devInfo
; /* defined in sys/devinfo.h */
146 if (ioctl (fd
, IOCINFO
, &devInfo
) == -1) return(-1);
148 if (devInfo
.devtype
!= DD_PSEU
) /* is it a pty? */
149 return (ioctl(fd
, request
, arg
)); /* no, do IOCTL */
151 /******* START PTY **************/
152 /** Pty found, possible HFT */
153 /** set new file des as raw */
155 /********************************/
157 /* Get current state of file */
158 /* descriptor & save */
159 if ((fd_flag
= fcntl (fd
, F_GETFL
, 0)) == -1) return (-1);
160 if (ioctl (fd
, TCGETS
, &term_old
) == -1) return (-1);
161 /* set terminal attr to raw */
162 /* and to delay on read */
163 pty_new
= pty_old
| REMOTE
;
164 memcpy (&term_new
, &term_old
, sizeof (term_new
));
165 term_new
.c_iflag
= 0;
166 term_new
.c_oflag
= 0;
167 term_new
.c_lflag
= 0;
168 /* term_new.c_line = 0; */
169 for (i
= 1; i
<= 5; i
++)
170 term_new
.c_cc
[i
] = 0;
171 term_new
.c_cc
[0] = -1;
172 ioctl (fd
, TCSETS
, &term_new
);
173 if (fcntl (fd
, F_SETFL
, fd_flag
& ~O_NDELAY
) == -1)
175 /* call spacific function */
176 if (request
== HFSKBD
)
177 retcode
= hfskbd (fd
, request
, arg
.c
);
178 else /* assume HFQUERY */
179 retcode
= hfqry (fd
, request
, arg
.c
);
181 fcntl (fd
, F_SETFL
, fd_flag
); /* reset terminal to original */
182 ioctl (fd
, TCSETS
, &term_old
);
185 return (retcode
); /* return error */
188 /*************** HFSKBD FUNCTION ******************************/
190 hfskbd (fd
, request
, arg
)
195 WR_REQ(fd
, request
, arg
->hf_buflen
, arg
->hf_bufp
,0);
196 return (GT_ACK(fd
, request
, arg
->hf_bufp
));
199 /*************** HFQUERY FUNCTION ******************************/
201 hfqry (fd
, request
, arg
)
206 WR_REQ(fd
, request
, arg
->hf_cmdlen
, arg
->hf_cmd
, arg
->hf_resplen
);
207 return (GT_ACK(fd
, request
, arg
->hf_resp
));
211 /*************** GT_ACK FUNCTION ******************************/
213 GT_ACK (fd
, req
, buf
)
219 int i
= sizeof (ack
);
223 struct hfctlack
*ack
;
226 is_ack_vtd
= 0; /* flag no ACT VTD yet */
228 if (setjmp (hftenv
)) /* set environment in case */
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 */
240 RD_BUF(fd
, p
.c
, i
); /* read until a ACK VTD is fill*/
242 if (! memcmp (&ack
, &ACK
, sizeof (HFINTROSZ
)) /* the ACK intro & */
243 && (ack
.hf_request
== req
)) /* is it the response we want ?*/
244 { /* yes, ACK VTD found */
245 is_ack_vtd
= 1; /* quickly, flag it */
246 break; /* get the %$%#@ out of here */
249 p
.ack
= &ack
; /* no, then skip 1st */
250 ++p
.c
; /* char and start over */
251 i
= sizeof (ack
) - 1; /* one less ESC to cry over */
253 while ((*p
.c
!= 0x1b) && i
) /* scan for next ESC */
254 { ++p
.c
; --i
; } /* if any */
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 whats been read */
259 i
= sizeof (ack
) - i
; /* set whats 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 ? */
267 RD_BUF(fd
,buf
,i
); /* read until it is received */
270 if (errno
= ack
.hf_retcode
) /* set errno based on returned */
271 return (-1); /* code, if 0, then no error */
273 return (0); /* if set, then error returned */
276 /*************** HFT_ALRM FUNCTION ******************************/
278 hft_alrm (sig
) /* Function hft_alrm - handle */
279 int sig
; /* alarm signal */
281 signal (SIGALRM
, sav_alrm
); /* reset to previous */
283 if (is_ack_vtd
) /* has ack vtd arrived ? */
284 return; /* yes, then continue */
285 else /* no, then return with error */
286 longjmp (hftenv
, -1);
290 /*********************************************************************/
292 /*** NOTE: Both the HFCTLREQ and the arg structure should be ***/
293 /*** sent in one io write operation. If terminal ***/
294 /*** emulators are in NODELAY mode then multiple writes ***/
295 /*** may cause bogus information to be read by the emulator ***/
296 /*** depending on the timing. ***/
298 /*********************************************************************/
301 WR_REQ (fd
, request
, cmdlen
, cmd
, resplen
)
310 struct hfctlreq
*req
;
314 req
.hf_request
= request
;
315 req
.hf_arg_len
= cmdlen
;
316 req
.hf_rsp_len
= resplen
;
318 if (cmdlen
) /* if arg structure to pass */
320 size
= sizeof (struct hfctlreq
) + cmdlen
;
321 if ((p
.c
= xmalloc(size
)) == NULL
) /* malloc one area */
324 memcpy (p
.c
, &req
, sizeof (req
)); /* copy CTL REQ struct */
325 memcpy (p
.c
+ sizeof (req
), cmd
, cmdlen
); /* copy arg struct */
329 p
.req
= &req
; /* otherwise use only CTL REQ */
333 /* write request to terminal */
334 if (emacs_write (fd
, p
.c
, size
) == -1) return (-1);
335 if (p
.req
!= &req
) /* free if allocated */