allow use of <End> key on AT-compatible keyboards (Ruslan Ermilov <ru@FreeBSD.org>)
[nvi.git] / ipc / ip_send.c
blobb20b4681baea9a42011869367a203c21ad468a81
1 /*-
2 * Copyright (c) 1996
3 * Keith Bostic. All rights reserved.
5 * See the LICENSE file for redistribution information.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "$Id: ip_send.c,v 8.9 2000/06/28 20:20:38 skimo Exp $ (Berkeley) $Date: 2000/06/28 20:20:38 $";
12 #endif /* not lint */
14 #include <sys/types.h>
15 #include <sys/queue.h>
17 #include <bitstring.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
23 #include "../common/common.h"
24 #include "ip.h"
27 * vi_send --
28 * Construct and send an IP buffer.
30 * PUBLIC: int vi_send __P((int, char *, IP_BUF *));
32 int
33 vi_send(ofd, fmt, ipbp)
34 int ofd;
35 char *fmt;
36 IP_BUF *ipbp;
38 static char *bp;
39 static size_t blen;
40 size_t off;
41 u_int32_t ilen;
42 int nlen, n, nw;
43 char *p;
46 * Have not created the channel to vi yet? -- RAZ
48 * XXX
49 * How is that possible!?!?
51 * We'll soon find out.
53 if (ofd == 0) {
54 fprintf(stderr, "No channel\n");
55 abort();
58 if (blen == 0 && (bp = malloc(blen = 512)) == NULL)
59 return (1);
61 p = bp;
62 nlen = 0;
63 *p++ = ipbp->code;
64 nlen += IPO_CODE_LEN;
66 if (fmt != NULL)
67 for (; *fmt != '\0'; ++fmt)
68 switch (*fmt) {
69 case '1': /* Value #1. */
70 ilen = htonl(ipbp->val1);
71 goto value;
72 case '2': /* Value #2. */
73 ilen = htonl(ipbp->val2);
74 goto value;
75 case '3': /* Value #3. */
76 ilen = htonl(ipbp->val3);
77 value: nlen += IPO_INT_LEN;
78 if (nlen >= blen) {
79 blen = blen * 2 + nlen;
80 off = p - bp;
81 if ((bp = realloc(bp, blen)) == NULL)
82 return (1);
83 p = bp + off;
85 memcpy(p, &ilen, IPO_INT_LEN);
86 p += IPO_INT_LEN;
87 break;
88 case 'a': /* String #1. */
89 case 'b': /* String #2. */
90 ilen = *fmt == 'a' ? ipbp->len1 : ipbp->len2;
91 nlen += IPO_INT_LEN + ilen;
92 if (nlen >= blen) {
93 blen = blen * 2 + nlen;
94 off = p - bp;
95 if ((bp = realloc(bp, blen)) == NULL)
96 return (1);
97 p = bp + off;
99 ilen = htonl(ilen);
100 memcpy(p, &ilen, IPO_INT_LEN);
101 p += IPO_INT_LEN;
102 if (*fmt == 'a') {
103 memcpy(p, ipbp->str1, ipbp->len1);
104 p += ipbp->len1;
105 } else {
106 memcpy(p, ipbp->str2, ipbp->len2);
107 p += ipbp->len2;
109 break;
111 for (n = p - bp, p = bp; n > 0; n -= nw, p += nw)
112 if ((nw = write(ofd, p, n)) < 0)
113 return (1);
114 return (0);