3 * Keith Bostic. All rights reserved.
5 * See the LICENSE file for redistribution information.
11 static const char sccsid
[] = "$Id: ip_trans.c,v 8.18 2001/06/25 15:19:25 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:25 $";
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #ifdef HAVE_SYS_SELECT_H
17 #include <sys/select.h>
20 #include <bitstring.h>
24 #include <netinet/in.h>
32 #include "../common/common.h"
36 static char ibuf
[2048]; /* Input buffer. */
37 static size_t ibuf_len
; /* Length of current input. */
39 extern IPFUNLIST
const ipfuns
[];
43 * Read from the vi message queue.
45 * PUBLIC: int vi_input __P((IPVIWIN *, int));
48 vi_input(IPVIWIN
*ipviwin
, int fd
)
52 /* Read waiting vi messages and translate to X calls. */
53 switch (nr
= read(fd
, ibuf
+ ibuf_len
, sizeof(ibuf
) - ibuf_len
)) {
63 /* Parse to data end or partial message. */
64 (void)vi_translate(ipviwin
, ibuf
, &ibuf_len
, NULL
);
66 return (ibuf_len
> 0);
71 * Construct and send an IP buffer, and wait for an answer.
73 * PUBLIC: int vi_wsend __P((IPVIWIN*, char *, IP_BUF *));
76 vi_wsend(IPVIWIN
*ipviwin
, char *fmt
, IP_BUF
*ipbp
)
81 if (vi_send(ipviwin
->ofd
, fmt
, ipbp
))
85 ipbp
->code
= CODE_OOB
;
88 FD_SET(ipviwin
->ifd
, &rdfd
);
89 if (select(ipviwin
->ifd
+ 1, &rdfd
, NULL
, NULL
, NULL
) != 0)
92 /* Read waiting vi messages and translate to X calls. */
94 read(ipviwin
->ifd
, ibuf
+ ibuf_len
, sizeof(ibuf
) - ibuf_len
)) {
104 /* Parse to data end or partial message. */
105 (void)vi_translate(ipviwin
, ibuf
, &ibuf_len
, ipbp
);
107 if (ipbp
->code
!= CODE_OOB
)
115 * Translate vi messages into function calls.
117 * PUBLIC: int vi_translate __P((IPVIWIN *, char *, size_t *, IP_BUF *));
120 vi_translate(IPVIWIN
*ipviwin
, char *bp
, size_t *lenp
, IP_BUF
*ipbp
)
125 char *fmt
, *p
, *s_bp
;
129 for (s_bp
= bp
, len
= *lenp
; len
> 0;) {
130 fmt
= ipfuns
[(ipb
.code
= bp
[0])-1].format
;
132 p
= bp
+ IPO_CODE_LEN
;
133 needlen
= IPO_CODE_LEN
;
134 for (; *fmt
!= '\0'; ++fmt
)
136 case '1': /* Value #1. */
139 case '2': /* Value #2. */
142 case '3': /* Value #3. */
144 value
: needlen
+= IPO_INT_LEN
;
147 memcpy(vp
, p
, IPO_INT_LEN
);
151 case 'a': /* String #1. */
155 case 'b': /* String #2. */
158 string
: needlen
+= IPO_INT_LEN
;
161 memcpy(vp
, p
, IPO_INT_LEN
);
178 if (ipb
.code
>= SI_EVENT_SUP
) {
184 * If we're waiting for a reply and we got it, return it, and
185 * leave any unprocessed data in the buffer. If we got a reply
186 * and we're not waiting for one, discard it -- callers wait
189 if (ipb
.code
== SI_REPLY
) {
196 /* Call the underlying routine. */
198 (((char *)ipviwin
->si_ops
)+ipfuns
[ipb
.code
- 1].offset
);
200 ipfuns
[ipb
.code
- 1].unmarshall(ipviwin
, &ipb
, fun
))
204 if ((*lenp
= len
) != 0)
205 memmove(s_bp
, bp
, len
);