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.17 2000/07/05 11:33:17 skimo Exp $ (Berkeley) $Date: 2000/07/05 11:33:17 $";
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));
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
, fmt
, ipbp
)
86 if (vi_send(ipviwin
->ofd
, fmt
, ipbp
))
90 ipbp
->code
= CODE_OOB
;
93 FD_SET(ipviwin
->ifd
, &rdfd
);
94 if (select(ipviwin
->ifd
+ 1, &rdfd
, NULL
, NULL
, NULL
) != 0)
97 /* Read waiting vi messages and translate to X calls. */
99 read(ipviwin
->ifd
, ibuf
+ ibuf_len
, sizeof(ibuf
) - ibuf_len
)) {
109 /* Parse to data end or partial message. */
110 (void)vi_translate(ipviwin
, ibuf
, &ibuf_len
, ipbp
);
112 if (ipbp
->code
!= CODE_OOB
)
120 * Translate vi messages into function calls.
122 * PUBLIC: int vi_translate __P((IPVIWIN *, char *, size_t *, IP_BUF *));
125 vi_translate(ipviwin
, bp
, lenp
, ipbp
)
134 char *fmt
, *p
, *s_bp
;
138 for (s_bp
= bp
, len
= *lenp
; len
> 0;) {
139 fmt
= ipfuns
[(ipb
.code
= bp
[0])-1].format
;
141 p
= bp
+ IPO_CODE_LEN
;
142 needlen
= IPO_CODE_LEN
;
143 for (; *fmt
!= '\0'; ++fmt
)
145 case '1': /* Value #1. */
148 case '2': /* Value #2. */
151 case '3': /* Value #3. */
153 value
: needlen
+= IPO_INT_LEN
;
156 memcpy(vp
, p
, IPO_INT_LEN
);
160 case 'a': /* String #1. */
164 case 'b': /* String #2. */
167 string
: needlen
+= IPO_INT_LEN
;
170 memcpy(vp
, p
, IPO_INT_LEN
);
187 if (ipb
.code
>= SI_EVENT_SUP
) {
193 * If we're waiting for a reply and we got it, return it, and
194 * leave any unprocessed data in the buffer. If we got a reply
195 * and we're not waiting for one, discard it -- callers wait
198 if (ipb
.code
== SI_REPLY
) {
205 /* Call the underlying routine. */
207 (((char *)ipviwin
->si_ops
)+ipfuns
[ipb
.code
- 1].offset
);
209 ipfuns
[ipb
.code
- 1].unmarshall(ipviwin
, &ipb
, fun
))
213 if ((*lenp
= len
) != 0)
214 memmove(s_bp
, bp
, len
);