update from From: Sven Verdoolaege <skimo@breughel.ufsia.ac.be>
[nvi.git] / ipc / ip_send.c
blob2f92e628a5773024686b7203b8b4aa40bce7c2e6
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.8 1997/08/02 16:49:33 bostic Exp $ (Berkeley) $Date: 1997/08/02 16:49:33 $";
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((char *, IP_BUF *));
32 int
33 vi_send(fmt, ipbp)
34 char *fmt;
35 IP_BUF *ipbp;
37 static char *bp;
38 static size_t blen;
39 size_t off;
40 u_int32_t ilen;
41 int nlen, n, nw;
42 char *p;
45 * Have not created the channel to vi yet? -- RAZ
47 * XXX
48 * How is that possible!?!?
50 if (vi_ofd == 0)
51 return (0);
53 if (blen == 0 && (bp = malloc(blen = 512)) == NULL)
54 return (1);
56 p = bp;
57 nlen = 0;
58 *p++ = ipbp->code;
59 nlen += IPO_CODE_LEN;
61 if (fmt != NULL)
62 for (; *fmt != '\0'; ++fmt)
63 switch (*fmt) {
64 case '1': /* Value #1. */
65 ilen = htonl(ipbp->val1);
66 goto value;
67 case '2': /* Value #2. */
68 ilen = htonl(ipbp->val2);
69 goto value;
70 case '3': /* Value #3. */
71 ilen = htonl(ipbp->val3);
72 value: nlen += IPO_INT_LEN;
73 if (nlen >= blen) {
74 blen = blen * 2 + nlen;
75 off = p - bp;
76 if ((bp = realloc(bp, blen)) == NULL)
77 return (1);
78 p = bp + off;
80 memcpy(p, &ilen, IPO_INT_LEN);
81 p += IPO_INT_LEN;
82 break;
83 case 'a': /* String #1. */
84 case 'b': /* String #2. */
85 ilen = *fmt == 'a' ? ipbp->len1 : ipbp->len2;
86 nlen += IPO_INT_LEN + ilen;
87 if (nlen >= blen) {
88 blen = blen * 2 + nlen;
89 off = p - bp;
90 if ((bp = realloc(bp, blen)) == NULL)
91 return (1);
92 p = bp + off;
94 ilen = htonl(ilen);
95 memcpy(p, &ilen, IPO_INT_LEN);
96 p += IPO_INT_LEN;
97 if (*fmt == 'a') {
98 memcpy(p, ipbp->str1, ipbp->len1);
99 p += ipbp->len1;
100 } else {
101 memcpy(p, ipbp->str2, ipbp->len2);
102 p += ipbp->len2;
104 break;
106 for (n = p - bp, p = bp; n > 0; n -= nw, p += nw)
107 if ((nw = write(vi_ofd, p, n)) < 0)
108 return (1);
109 return (0);