Home | History | Annotate | Line # | Download | only in ipc
ipc_method.c revision 1.4
      1 /*	$NetBSD: ipc_method.c,v 1.4 2013/11/27 17:51:04 christos Exp $	*/
      2 /*-
      3  * Copyright (c) 1996
      4  *	Rob Zimmermann.  All rights reserved.
      5  * Copyright (c) 1996
      6  *	Keith Bostic.  All rights reserved.
      7  *
      8  * See the LICENSE file for redistribution information.
      9  */
     10 
     11 #include "config.h"
     12 
     13 #include <sys/types.h>
     14 #include <sys/queue.h>
     15 #include <sys/stat.h>
     16 
     17 #include <bitstring.h>
     18 #include <errno.h>
     19 #include <fcntl.h>
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 #include <string.h>
     23 #include <unistd.h>
     24 
     25 #include <sys/uio.h>
     26 
     27 #include "../common/common.h"
     28 #include "ip.h"
     29 
     30 static int vi_send_ __P((IPVIWIN   *, int));
     31 static int vi_send_1 __P((IPVIWIN   *, int, u_int32_t  ));
     32 static int vi_send_12 __P((IPVIWIN *ipvi, int code, u_int32_t val1, u_int32_t val2));
     33 #if 0
     34 static int vi_send_ab1 __P((IPVIWIN *ipvi, int code,
     35 	    const char *str1, u_int32_t len1,
     36 	    const char *str2, u_int32_t len2, u_int32_t val));
     37 static int vi_send_a1 __P((IPVIWIN *ipvi, int code, const char *str, u_int32_t len,
     38 	   u_int32_t val));
     39 #endif
     40 static int vi_send_a __P((IPVIWIN *ipvi, int code, const char *str, u_int32_t len));
     41 
     42 #include "ipc_gen.c"
     43 
     44 static int vi_set_ops __P((IPVIWIN *, IPSIOPS *));
     45 static int vi_win_close __P((IPVIWIN *));
     46 
     47 static int vi_close __P((IPVI *));
     48 static int vi_new_window __P((IPVI *, IPVIWIN **, int));
     49 
     50 /*
     51  * vi_create
     52  *
     53  * PUBLIC: int vi_create __P((IPVI **, u_int32_t));
     54  */
     55 int
     56 vi_create(IPVI **ipvip, u_int32_t flags)
     57 {
     58 	IPVI	*ipvi;
     59 
     60 	MALLOC_GOTO(NULL, ipvi, IPVI*, sizeof(IPVI));
     61 	memset(ipvi, 0, sizeof(IPVI));
     62 
     63 	ipvi->flags = flags;
     64 
     65 	ipvi->run = vi_run;
     66 	ipvi->new_window = vi_new_window;
     67 	ipvi->close = vi_close;
     68 
     69 	*ipvip = ipvi;
     70 
     71 	return 0;
     72 
     73 alloc_err:
     74 	return 1;
     75 }
     76 
     77 static int
     78 vi_new_window (IPVI *ipvi, IPVIWIN **ipviwinp, int fd)
     79 {
     80 	IPVIWIN	*ipviwin;
     81 
     82 	MALLOC_GOTO(NULL, ipviwin, IPVIWIN*, sizeof(IPVIWIN));
     83 	memset(ipviwin, 0, sizeof(IPVIWIN));
     84 
     85 	if (0) {
     86 	ipviwin->ifd = ipvi->ifd;
     87 	ipviwin->ofd = ipvi->ofd;
     88 	} else {
     89 	int sockets[2];
     90 	struct msghdr   mh;
     91 	IPCMSGHDR	    ch;
     92 	char	    dummy;
     93 	struct iovec    iov;
     94 
     95 	socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets);
     96 
     97 	memset(&mh, 0, sizeof(mh));
     98 	mh.msg_namelen = 0;
     99 	mh.msg_iovlen = 1;
    100 	mh.msg_iov = &iov;
    101 	mh.msg_controllen = sizeof(ch);
    102 	mh.msg_control = (void *)&ch;
    103 
    104 	iov.iov_len = 1;
    105 	iov.iov_base = &dummy;
    106 
    107 	ch.header.cmsg_level = SOL_SOCKET;
    108 	ch.header.cmsg_type = SCM_RIGHTS;
    109 	ch.header.cmsg_len = sizeof(ch);
    110 
    111 	*(int *)CMSG_DATA(&ch.header) = sockets[1];
    112 	sendmsg(ipvi->ofd, &mh, 0);
    113 	dummy = (fd == -1) ? ' ' : 'F';
    114 	*(int *)CMSG_DATA(&ch.header) = sockets[1];
    115 	sendmsg(sockets[0], &mh, 0);
    116 	close(sockets[1]);
    117 
    118 	if (fd != -1) {
    119 		*(int *)CMSG_DATA(&ch.header) = fd;
    120 		sendmsg(sockets[0], &mh, 0);
    121 		close(fd);
    122 	}
    123 
    124 	ipviwin->ifd = sockets[0];
    125 	ipviwin->ofd = sockets[0];
    126 	}
    127 
    128 #define IPVISET(func) \
    129 	ipviwin->func = vi_##func;
    130 
    131 	IPVISET(c_bol);
    132 	IPVISET(c_bottom);
    133 	IPVISET(c_del);
    134 	IPVISET(c_eol);
    135 	IPVISET(c_insert);
    136 	IPVISET(c_left);
    137 	IPVISET(c_right);
    138 	IPVISET(c_top);
    139 	IPVISET(c_settop);
    140 	IPVISET(resize);
    141 	IPVISET(string);
    142 	IPVISET(quit);
    143 	IPVISET(wq);
    144 
    145 	IPVISET(input);
    146 	/*
    147 	IPVISET(close);
    148 	*/
    149 	ipviwin->close = vi_win_close;
    150 	IPVISET(set_ops);
    151 
    152 	*ipviwinp = ipviwin;
    153 
    154 	return 0;
    155 
    156 alloc_err:
    157 	return 1;
    158 }
    159 
    160 static int
    161 vi_set_ops(IPVIWIN *ipvi, IPSIOPS *ops)
    162 {
    163 	ipvi->si_ops = ops;
    164 	return 0;
    165 }
    166 
    167 static int  vi_close(IPVI *ipvi)
    168 {
    169 	memset(ipvi, 6, sizeof(IPVI));
    170 	free(ipvi);
    171 	return 0;
    172 }
    173 
    174 static int  vi_win_close(IPVIWIN *ipviwin)
    175 {
    176 	memset(ipviwin, 6, sizeof(IPVIWIN));
    177 	free(ipviwin);
    178 	return 0;
    179 }
    180 
    181 
    182 static int
    183 vi_send_(IPVIWIN *ipvi, int code)
    184 {
    185 	IP_BUF	ipb;
    186 
    187 	memset(&ipb, 0, sizeof(ipb));
    188 	ipb.code = code;
    189 	return vi_send(ipvi->ofd, NULL, &ipb);
    190 }
    191 
    192 static int
    193 vi_send_1(IPVIWIN *ipvi, int code, u_int32_t val)
    194 {
    195 	IP_BUF	ipb;
    196 
    197 	memset(&ipb, 0, sizeof(ipb));
    198 	ipb.code = code;
    199 	ipb.val1 = val;
    200 	return vi_send(ipvi->ofd, "1", &ipb);
    201 }
    202 
    203 static int
    204 vi_send_12(IPVIWIN *ipvi, int code, u_int32_t val1, u_int32_t val2)
    205 {
    206 	IP_BUF	ipb;
    207 
    208 	memset(&ipb, 0, sizeof(ipb));
    209 	ipb.val1 = val1;
    210 	ipb.val2 = val2;
    211 	ipb.code = code;
    212 	return vi_send(ipvi->ofd, "12", &ipb);
    213 }
    214 
    215 static int
    216 vi_send_a(IPVIWIN *ipvi, int code, const char *str, u_int32_t len)
    217 {
    218 	IP_BUF	ipb;
    219 
    220 	memset(&ipb, 0, sizeof(ipb));
    221 	ipb.str1 = str;
    222 	ipb.len1 = len;
    223 	ipb.code = code;
    224 	return vi_send(ipvi->ofd, "a", &ipb);
    225 }
    226 
    227 #if 0
    228 static int
    229 vi_send_a1(IPVIWIN *ipvi, int code, const char *str, u_int32_t len,
    230 	   u_int32_t val)
    231 {
    232 	IP_BUF	ipb;
    233 
    234 	memset(&ipb, 0, sizeof(ipb));
    235 	ipb.str1 = str;
    236 	ipb.len1 = len;
    237 	ipb.val1 = val;
    238 	ipb.code = code;
    239 	return vi_send(ipvi->ofd, "a1", &ipb);
    240 }
    241 
    242 static int
    243 vi_send_ab1(IPVIWIN *ipvi, int code, const char *str1, u_int32_t len1,
    244 	    const char *str2, u_int32_t len2, u_int32_t val)
    245 {
    246 	IP_BUF	ipb;
    247 
    248 	memset(&ipb, 0, sizeof(ipb));
    249 	ipb.str1 = str1;
    250 	ipb.len1 = len1;
    251 	ipb.str2 = str2;
    252 	ipb.len2 = len2;
    253 	ipb.val1 = val;
    254 	ipb.code = code;
    255 	return vi_send(ipvi->ofd, "ab1", &ipb);
    256 }
    257 #endif
    258