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