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