categories field doesn't really seem to be used.
[nvi.git] / ipc / ipc_method.c
blob5da27e7628c316b727932fdfd89651eea77c7e09
1 /*-
2 * Copyright (c) 1996
3 * Rob Zimmermann. All rights reserved.
4 * Copyright (c) 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/stat.h>
16 #include <bitstring.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
24 #include <sys/uio.h>
26 #include "../common/common.h"
27 #include "ip.h"
29 static int vi_send_ __P((IPVIWIN *, int));
30 static int vi_send_1 __P((IPVIWIN *, int, u_int32_t ));
31 static int vi_send_12 __P((IPVIWIN *ipvi, int code, u_int32_t val1, u_int32_t val2));
32 static int vi_send_ab1 __P((IPVIWIN *ipvi, int code,
33 const char *str1, u_int32_t len1,
34 const char *str2, u_int32_t len2, u_int32_t val));
35 static int vi_send_a1 __P((IPVIWIN *ipvi, int code, const char *str, u_int32_t len,
36 u_int32_t val));
37 static int vi_send_a __P((IPVIWIN *ipvi, int code, const char *str, u_int32_t len));
39 #include "ipc_gen.c"
41 static int vi_set_ops __P((IPVIWIN *, IPSIOPS *));
42 static int vi_win_close __P((IPVIWIN *));
44 static int vi_close __P((IPVI *));
45 static int vi_new_window __P((IPVI *, IPVIWIN **, int));
47 /*
48 * vi_create
50 * PUBLIC: int vi_create __P((IPVI **, u_int32_t));
52 int
53 vi_create(ipvip, flags)
54 IPVI **ipvip;
55 u_int32_t flags;
57 IPVI *ipvi;
59 MALLOC_GOTO(NULL, ipvi, IPVI*, sizeof(IPVI));
60 memset(ipvi, 0, sizeof(IPVI));
62 ipvi->flags = flags;
64 ipvi->run = vi_run;
65 ipvi->new_window = vi_new_window;
66 ipvi->close = vi_close;
68 *ipvip = ipvi;
70 return 0;
72 alloc_err:
73 return 1;
76 static int
77 vi_new_window (IPVI *ipvi, IPVIWIN **ipviwinp, int fd)
79 IPVIWIN *ipviwin;
81 MALLOC_GOTO(NULL, ipviwin, IPVIWIN*, sizeof(IPVIWIN));
82 memset(ipviwin, 0, sizeof(IPVIWIN));
84 if (0) {
85 ipviwin->ifd = ipvi->ifd;
86 ipviwin->ofd = ipvi->ofd;
87 } else {
88 int sockets[2];
89 struct msghdr mh;
90 IPCMSGHDR ch;
91 char dummy;
92 struct iovec iov;
94 socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets);
96 mh.msg_namelen = 0;
97 mh.msg_iovlen = 1;
98 mh.msg_iov = &iov;
99 mh.msg_controllen = sizeof(ch);
100 mh.msg_control = (void *)&ch;
102 iov.iov_len = 1;
103 iov.iov_base = &dummy;
105 ch.header.cmsg_level = SOL_SOCKET;
106 ch.header.cmsg_type = SCM_RIGHTS;
107 ch.header.cmsg_len = sizeof(ch);
109 *(int *)CMSG_DATA(&ch.header) = sockets[1];
110 sendmsg(ipvi->ofd, &mh, 0);
111 dummy = (fd == -1) ? ' ' : 'F';
112 *(int *)CMSG_DATA(&ch.header) = sockets[1];
113 sendmsg(sockets[0], &mh, 0);
114 close(sockets[1]);
116 if (fd != -1) {
117 *(int *)CMSG_DATA(&ch.header) = fd;
118 sendmsg(sockets[0], &mh, 0);
119 close(fd);
122 ipviwin->ifd = sockets[0];
123 ipviwin->ofd = sockets[0];
126 #define IPVISET(func) \
127 ipviwin->func = vi_##func;
129 IPVISET(c_bol);
130 IPVISET(c_bottom);
131 IPVISET(c_del);
132 IPVISET(c_eol);
133 IPVISET(c_insert);
134 IPVISET(c_left);
135 IPVISET(c_right);
136 IPVISET(c_top);
137 IPVISET(c_settop);
138 IPVISET(resize);
139 IPVISET(string);
140 IPVISET(quit);
141 IPVISET(wq);
143 IPVISET(input);
145 IPVISET(close);
147 ipviwin->close = vi_win_close;
148 IPVISET(set_ops);
150 *ipviwinp = ipviwin;
152 return 0;
154 alloc_err:
155 return 1;
158 static int
159 vi_set_ops(ipvi, ops)
160 IPVIWIN *ipvi;
161 IPSIOPS *ops;
163 ipvi->si_ops = ops;
164 return 0;
167 static int vi_close(ipvi)
168 IPVI *ipvi;
170 memset(ipvi, 6, sizeof(IPVI));
171 free(ipvi);
172 return 0;
175 static int vi_win_close(ipviwin)
176 IPVIWIN *ipviwin;
178 memset(ipviwin, 6, sizeof(IPVIWIN));
179 free(ipviwin);
180 return 0;
184 static int
185 vi_send_(ipvi, code)
186 IPVIWIN *ipvi;
187 int code;
189 IP_BUF ipb;
190 ipb.code = code;
191 return vi_send(ipvi->ofd, NULL, &ipb);
194 static int
195 vi_send_1(ipvi, code, val)
196 IPVIWIN *ipvi;
197 int code;
198 u_int32_t val;
200 IP_BUF ipb;
201 ipb.code = code;
202 ipb.val1 = val;
203 return vi_send(ipvi->ofd, "1", &ipb);
206 static int
207 vi_send_12(IPVIWIN *ipvi, int code, u_int32_t val1, u_int32_t val2)
209 IP_BUF ipb;
211 ipb.val1 = val1;
212 ipb.val2 = val2;
213 ipb.code = code;
214 return vi_send(ipvi->ofd, "12", &ipb);
217 static int
218 vi_send_a(IPVIWIN *ipvi, int code, const char *str, u_int32_t len)
220 IP_BUF ipb;
222 ipb.str1 = str;
223 ipb.len1 = len;
224 ipb.code = code;
225 return vi_send(ipvi->ofd, "a", &ipb);
228 static int
229 vi_send_a1(IPVIWIN *ipvi, int code, const char *str, u_int32_t len,
230 u_int32_t val)
232 IP_BUF ipb;
234 ipb.str1 = str;
235 ipb.len1 = len;
236 ipb.val1 = val;
237 ipb.code = code;
238 return vi_send(ipvi->ofd, "a1", &ipb);
241 static int
242 vi_send_ab1(IPVIWIN *ipvi, int code, const char *str1, u_int32_t len1,
243 const char *str2, u_int32_t len2, u_int32_t val)
245 IP_BUF ipb;
247 ipb.str1 = str1;
248 ipb.len1 = len1;
249 ipb.str2 = str2;
250 ipb.len2 = len2;
251 ipb.val1 = val;
252 ipb.code = code;
253 return vi_send(ipvi->ofd, "ab1", &ipb);