break Motif code up into motif and motif_l
[nvi.git] / ipc / ip_send.c
blob77e8b4a0567d0e1f4bcf528563921de44afcd6ec
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.4 1996/12/11 20:57:03 bostic Exp $ (Berkeley) $Date: 1996/12/11 20:57:03 $";
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/ip.h"
25 #include "ipc_extern.h"
27 extern int vi_ofd; /* Output file descriptor. */
30 * __vi_send --
31 * Construct and send an IP buffer.
33 * PUBLIC: int __vi_send __P((char *, IP_BUF *));
35 int
36 __vi_send(fmt, ipbp)
37 char *fmt;
38 IP_BUF *ipbp;
40 static char *bp;
41 static size_t blen;
42 size_t off;
43 u_int32_t ilen;
44 int nlen, n, nw;
45 char *p;
48 * Have not created the channel to vi yet? -- RAZ
50 * XXX
51 * How is that possible!?!?
53 if (vi_ofd == 0)
54 return (0);
56 if (blen == 0 && (bp = malloc(blen = 512)) == NULL)
57 return (1);
59 p = bp;
60 nlen = 0;
61 *p++ = ipbp->code;
62 nlen += IPO_CODE_LEN;
64 if (fmt != NULL)
65 for (; *fmt != '\0'; ++fmt)
66 switch (*fmt) {
67 case '1': /* Value 1. */
68 ilen = htonl(ipbp->val1);
69 goto value;
70 case '2': /* Value 2. */
71 ilen = htonl(ipbp->val2);
72 goto value;
73 case '3': /* Value 3. */
74 ilen = htonl(ipbp->val3);
75 value: nlen += IPO_INT_LEN;
76 if (nlen >= blen) {
77 blen = blen * 2 + nlen;
78 off = p - bp;
79 if ((bp = realloc(bp, blen)) == NULL)
80 return (1);
81 p = bp + off;
83 memmove(p, &ilen, IPO_INT_LEN);
84 p += IPO_INT_LEN;
85 break;
86 case 's': /* String. */
87 ilen = ipbp->len; /* XXX: conversion. */
88 ilen = htonl(ilen);
89 nlen += IPO_INT_LEN + ipbp->len;
90 if (nlen >= blen) {
91 blen = blen * 2 + nlen;
92 off = p - bp;
93 if ((bp = realloc(bp, blen)) == NULL)
94 return (1);
95 p = bp + off;
97 memmove(p, &ilen, IPO_INT_LEN);
98 p += IPO_INT_LEN;
99 memmove(p, ipbp->str, ipbp->len);
100 p += ipbp->len;
101 break;
103 for (n = p - bp, p = bp; n > 0; n -= nw, p += nw)
104 if ((nw = write(vi_ofd, p, n)) < 0)
105 return (1);
106 return (0);