Initial git import
[sxemacs] / src / hftctl.c
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>
47 #include "lisp.h"               /* encapsulated open, close, read, write */
48
49 #include <sys/signal.h>
50 #include <errno.h>
51
52 #include <stdio.h>
53 #include <fcntl.h>
54 #include <setjmp.h>
55 #include <sys/ioctl.h>
56 #include <sys/devinfo.h>
57 #include <termios.h>
58 #include <termio.h>
59 #include <sys/hft.h>
60 #include <sys/uio.h>
61 #include <sys/tty.h>
62 /* #include <sys/pty.h> */
63
64 #define REMOTE 0x01
65
66 #undef ioctl
67 static char SCCSid[] = "com/gnuemacs/src,3.1,9021-90/05/03-5/3/90";
68
69 /*************** LOCAL DEFINES **********************************/
70
71 #define QDEV   ((HFQPDEVCH<<8)|HFQPDEVCL)
72 #define QLOC   ((HFQLOCCH<<8)|HFQLOCCL)
73 #define QPS    ((HFQPRESCH<<8)|HFQPRESCL)
74
75 #ifndef TCGETS
76 #define TCGETS TCGETA
77 #endif
78 #ifndef TCSETS
79 #define TCSETS TCSETA
80 #endif
81
82 /*************** EXTERNAL / GLOBAL DATA AREA ********************/
83
84 static int hfqry();
85 static int hfskbd();
86
87 extern int errno;
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 };
95
96        /* FUNC             signal(); */
97
98 /*************** LOCAL MACROS ***********************************/
99
100 #define HFTYPE(p)   ((p->hf_typehi<<8)|(p->hf_typelo))
101
102 #define BYTE4(p)    ((p)[0]<<24 | (p)[1]<<16 | (p)[2]<<8 | (p)[3])
103
104                                         /* read a buffer        */
105 #define RD_BUF(f,p,l) \
106         while ((l)) \
107           if ((j = read((f),(p),(l))) < 0) \
108              if (errno != EINTR) return (-1); \
109              else continue; \
110           else { (l) -= j; (p) += j; }
111
112 /*************** function prototypes ***************************/
113 #ifdef __STDC__
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);
117 #else
118 static GT_ACK();
119 static WR_REQ();
120 static void hft_alrm();
121 #endif
122
123 /*************** HFTCTL FUNCTION *******************************/
124
125 hftctl(fd, request, arg)
126 int fd;
127 int request;
128 union {
129         struct hfintro *intro;
130         struct hfquery *query;
131         char *c;
132 } arg;
133 {
134
135         int i;
136         int fd_flag;            /* fcntl flags          */
137         union {
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            */
143         int pty_old;
144         int retcode;
145         struct termios term_new;        /* terminal attributes  */
146         struct termios term_old;
147         struct devinfo devInfo; /* defined in sys/devinfo.h */
148
149         if (ioctl(fd, IOCINFO, &devInfo) == -1)
150                 return (-1);
151
152         if (devInfo.devtype != DD_PSEU) /* is it a pty? */
153                 return (ioctl(fd, request, arg));       /* no, do IOCTL */
154
155   /******* START PTY **************/
156   /**  Pty found, possible HFT    */
157   /** set new file des as raw     */
158   /** as you can.                 */
159   /********************************/
160
161         /* Get current state of file    */
162         /* descriptor & save            */
163         if ((fd_flag = fcntl(fd, F_GETFL, 0)) == -1)
164                 return (-1);
165         if (ioctl(fd, TCGETS, &term_old) == -1)
166                 return (-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)
180                 return (-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);
186
187         fcntl(fd, F_SETFL, fd_flag);    /* reset terminal to original   */
188         ioctl(fd, TCSETS, &term_old);
189
190         return (retcode);       /* return error                 */
191 }
192
193 /*************** HFSKBD  FUNCTION ******************************/
194 static int hfskbd(fd, request, arg)
195 int fd;
196 int request;
197 struct hfbuf *arg;
198 {
199         WR_REQ(fd, request, arg->hf_buflen, arg->hf_bufp, 0);
200         return (GT_ACK(fd, request, arg->hf_bufp));
201 }
202
203 /*************** HFQUERY FUNCTION ******************************/
204 static int hfqry(fd, request, arg)
205 int fd;
206 int request;
207 struct hfquery *arg;
208 {
209         WR_REQ(fd, request, arg->hf_cmdlen, arg->hf_cmd, arg->hf_resplen);
210         return (GT_ACK(fd, request, arg->hf_resp));
211 }
212
213 /*************** GT_ACK FUNCTION ******************************/
214 static int GT_ACK(fd, req, buf)
215 int fd;
216 int req;
217 char *buf;
218 {
219         struct hfctlack ack;
220         int i = sizeof(ack);
221         int j = 0;
222         union {
223                 char *c;
224                 struct hfctlack *ack;
225         } p;
226
227         is_ack_vtd = 0;         /* flag no ACT VTD yet         */
228
229         if (SETJMP(hftenv)) {   /* set environment in case     *//* of time out                 */
230                 errno = ENODEV; /* if time out, set errno      */
231                 return (-1);    /* flag error                  */
232         }
233
234         alarm(3);               /* time out in 3 secs          */
235         sav_alrm = signal(SIGALRM, hft_alrm);   /* prepare to catch time out   */
236
237         p.ack = &ack;
238         while (!is_ack_vtd) {   /* do until valid ACK VTD      */
239                 RD_BUF(fd, p.c, i);     /* read until a ACK VTD is fill */
240
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   */
245                 }
246
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    */
250
251                 while ((*p.c != 0x1b) && i) {   /* scan for next ESC           */
252                         ++p.c;
253                         --i;
254                 }               /* if any                      */
255
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               */
261
262         alarm(0);               /* ACK VTD received, reset alrm */
263         signal(SIGALRM, sav_alrm);      /* reset signal                */
264
265         if (i = ack.hf_arg_len) {       /* any data following ?        *//* yes,                        */
266                 RD_BUF(fd, buf, i);     /* read until it is received   */
267         }
268
269         if (errno = ack.hf_retcode)     /* set errno based on returned */
270                 return (-1);    /* code, if 0, then no error   */
271         else
272                 return (0);     /* if set, then error returned */
273 }
274
275 /*************** HFT_ALRM FUNCTION ******************************/
276 static void hft_alrm(sig)       /* Function hft_alrm - handle  */
277 int sig;                        /* alarm signal               */
278 {
279         signal(SIGALRM, sav_alrm);      /* reset to previous          */
280
281         if (is_ack_vtd)         /* has ack vtd arrived ?      */
282                 return;         /* yes, then continue         */
283         else                    /* no, then return with error */
284                 LONGJMP(hftenv, -1);
285
286 }
287
288 /*********************************************************************/
289 /***                                                               ***/
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.                               ***/
295 /***                                                               ***/
296 /*********************************************************************/
297
298 static int WR_REQ(fd, request, cmdlen, cmd, resplen)
299 int fd;
300 int request;
301 int cmdlen;
302 char *cmd;
303 int resplen;
304 {
305         struct {
306                 char *c;
307                 struct hfctlreq *req;
308         } p;
309         int size;
310
311         req.hf_request = request;
312         req.hf_arg_len = cmdlen;
313         req.hf_rsp_len = resplen;
314
315         if (cmdlen) {           /* if arg structure to pass    */
316                 size = sizeof(struct hfctlreq) + cmdlen;
317                 if ((p.c = xmalloc(size)) == NULL)      /* malloc one area            */
318                         return (-1);
319
320                 memcpy(p.c, &req, sizeof(req)); /* copy CTL REQ struct         */
321                 memcpy(p.c + sizeof(req), cmd, cmdlen); /* copy arg struct     */
322         } else {
323                 p.req = &req;   /* otherwise use only CTL REQ  */
324                 size = sizeof(req);
325         }
326
327         /* write request to terminal   */
328         if (write(fd, p.c, size) == -1)
329                 return (-1);
330         if (p.req != &req)      /* free if allocated           */
331                 xfree(p.c);
332         return (0);
333 }