5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 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";
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #ifdef HAVE_SYS_SELECT_H
19 #include <sys/select.h>
22 #include <bitstring.h>
26 #include <netinet/in.h>
34 #include "../common/common.h"
38 static char ibuf
[2048]; /* Input buffer. */
39 static size_t ibuf_len
; /* Length of current input. */
41 extern IPFUNLIST
const ipfuns
[];
45 * Read from the vi message queue.
47 * PUBLIC: int vi_input __P((IPVIWIN *, int));
50 vi_input(IPVIWIN
*ipviwin
, int fd
)
54 /* Read waiting vi messages and translate to X calls. */
55 switch (nr
= read(fd
, ibuf
+ ibuf_len
, sizeof(ibuf
) - ibuf_len
)) {
65 /* Parse to data end or partial message. */
66 (void)vi_translate(ipviwin
, ibuf
, &ibuf_len
, NULL
);
68 return (ibuf_len
> 0);
73 * Construct and send an IP buffer, and wait for an answer.
75 * PUBLIC: int vi_wsend __P((IPVIWIN*, char *, IP_BUF *));
78 vi_wsend(IPVIWIN
*ipviwin
, char *fmt
, IP_BUF
*ipbp
)
83 if (vi_send(ipviwin
->ofd
, fmt
, ipbp
))
87 ipbp
->code
= CODE_OOB
;
90 FD_SET(ipviwin
->ifd
, &rdfd
);
91 if (select(ipviwin
->ifd
+ 1, &rdfd
, NULL
, NULL
, NULL
) != 0)
94 /* Read waiting vi messages and translate to X calls. */
96 read(ipviwin
->ifd
, ibuf
+ ibuf_len
, sizeof(ibuf
) - ibuf_len
)) {
106 /* Parse to data end or partial message. */
107 (void)vi_translate(ipviwin
, ibuf
, &ibuf_len
, ipbp
);
109 if (ipbp
->code
!= CODE_OOB
)
117 * Translate vi messages into function calls.
119 * PUBLIC: int vi_translate __P((IPVIWIN *, char *, size_t *, IP_BUF *));
122 vi_translate(IPVIWIN
*ipviwin
, char *bp
, size_t *lenp
, IP_BUF
*ipbp
)
127 char *fmt
, *p
, *s_bp
;
131 for (s_bp
= bp
, len
= *lenp
; len
> 0;) {
132 fmt
= ipfuns
[(ipb
.code
= bp
[0])-1].format
;
134 p
= bp
+ IPO_CODE_LEN
;
135 needlen
= IPO_CODE_LEN
;
136 for (; *fmt
!= '\0'; ++fmt
)
138 case '1': /* Value #1. */
141 case '2': /* Value #2. */
144 case '3': /* Value #3. */
146 value
: needlen
+= IPO_INT_LEN
;
149 memcpy(vp
, p
, IPO_INT_LEN
);
153 case 'a': /* String #1. */
157 case 'b': /* String #2. */
160 string
: needlen
+= IPO_INT_LEN
;
163 memcpy(vp
, p
, IPO_INT_LEN
);
180 if (ipb
.code
>= SI_EVENT_SUP
) {
186 * If we're waiting for a reply and we got it, return it, and
187 * leave any unprocessed data in the buffer. If we got a reply
188 * and we're not waiting for one, discard it -- callers wait
191 if (ipb
.code
== SI_REPLY
) {
198 /* Call the underlying routine. */
200 (((char *)ipviwin
->si_ops
)+ipfuns
[ipb
.code
- 1].offset
);
202 ipfuns
[ipb
.code
- 1].unmarshall(ipviwin
, &ipb
, fun
))
206 if ((*lenp
= len
) != 0)
207 memmove(s_bp
, bp
, len
);