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