add db.1.85
[nvi.git] / ipc / ipc_method.c
blob18b17e3ce2053a73d46d2eede8026fe242d82360
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(IPVI **ipvip, u_int32_t flags)
55 IPVI *ipvi;
57 MALLOC_GOTO(NULL, ipvi, IPVI*, sizeof(IPVI));
58 memset(ipvi, 0, sizeof(IPVI));
60 ipvi->flags = flags;
62 ipvi->run = vi_run;
63 ipvi->new_window = vi_new_window;
64 ipvi->close = vi_close;
66 *ipvip = ipvi;
68 return 0;
70 alloc_err:
71 return 1;
74 static int
75 vi_new_window (IPVI *ipvi, IPVIWIN **ipviwinp, int fd)
77 IPVIWIN *ipviwin;
79 MALLOC_GOTO(NULL, ipviwin, IPVIWIN*, sizeof(IPVIWIN));
80 memset(ipviwin, 0, sizeof(IPVIWIN));
82 if (0) {
83 ipviwin->ifd = ipvi->ifd;
84 ipviwin->ofd = ipvi->ofd;
85 } else {
86 int sockets[2];
87 struct msghdr mh;
88 IPCMSGHDR ch;
89 char dummy;
90 struct iovec iov;
92 socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets);
94 mh.msg_namelen = 0;
95 mh.msg_iovlen = 1;
96 mh.msg_iov = &iov;
97 mh.msg_controllen = sizeof(ch);
98 mh.msg_control = (void *)&ch;
100 iov.iov_len = 1;
101 iov.iov_base = &dummy;
103 ch.header.cmsg_level = SOL_SOCKET;
104 ch.header.cmsg_type = SCM_RIGHTS;
105 ch.header.cmsg_len = sizeof(ch);
107 *(int *)CMSG_DATA(&ch.header) = sockets[1];
108 sendmsg(ipvi->ofd, &mh, 0);
109 dummy = (fd == -1) ? ' ' : 'F';
110 *(int *)CMSG_DATA(&ch.header) = sockets[1];
111 sendmsg(sockets[0], &mh, 0);
112 close(sockets[1]);
114 if (fd != -1) {
115 *(int *)CMSG_DATA(&ch.header) = fd;
116 sendmsg(sockets[0], &mh, 0);
117 close(fd);
120 ipviwin->ifd = sockets[0];
121 ipviwin->ofd = sockets[0];
124 #define IPVISET(func) \
125 ipviwin->func = vi_##func;
127 IPVISET(c_bol);
128 IPVISET(c_bottom);
129 IPVISET(c_del);
130 IPVISET(c_eol);
131 IPVISET(c_insert);
132 IPVISET(c_left);
133 IPVISET(c_right);
134 IPVISET(c_top);
135 IPVISET(c_settop);
136 IPVISET(resize);
137 IPVISET(string);
138 IPVISET(quit);
139 IPVISET(wq);
141 IPVISET(input);
143 IPVISET(close);
145 ipviwin->close = vi_win_close;
146 IPVISET(set_ops);
148 *ipviwinp = ipviwin;
150 return 0;
152 alloc_err:
153 return 1;
156 static int
157 vi_set_ops(IPVIWIN *ipvi, IPSIOPS *ops)
159 ipvi->si_ops = ops;
160 return 0;
163 static int vi_close(IPVI *ipvi)
165 memset(ipvi, 6, sizeof(IPVI));
166 free(ipvi);
167 return 0;
170 static int vi_win_close(IPVIWIN *ipviwin)
172 memset(ipviwin, 6, sizeof(IPVIWIN));
173 free(ipviwin);
174 return 0;
178 static int
179 vi_send_(IPVIWIN *ipvi, int code)
181 IP_BUF ipb;
182 ipb.code = code;
183 return vi_send(ipvi->ofd, NULL, &ipb);
186 static int
187 vi_send_1(IPVIWIN *ipvi, int code, u_int32_t val)
189 IP_BUF ipb;
190 ipb.code = code;
191 ipb.val1 = val;
192 return vi_send(ipvi->ofd, "1", &ipb);
195 static int
196 vi_send_12(IPVIWIN *ipvi, int code, u_int32_t val1, u_int32_t val2)
198 IP_BUF ipb;
200 ipb.val1 = val1;
201 ipb.val2 = val2;
202 ipb.code = code;
203 return vi_send(ipvi->ofd, "12", &ipb);
206 static int
207 vi_send_a(IPVIWIN *ipvi, int code, const char *str, u_int32_t len)
209 IP_BUF ipb;
211 ipb.str1 = str;
212 ipb.len1 = len;
213 ipb.code = code;
214 return vi_send(ipvi->ofd, "a", &ipb);
217 static int
218 vi_send_a1(IPVIWIN *ipvi, int code, const char *str, u_int32_t len,
219 u_int32_t val)
221 IP_BUF ipb;
223 ipb.str1 = str;
224 ipb.len1 = len;
225 ipb.val1 = val;
226 ipb.code = code;
227 return vi_send(ipvi->ofd, "a1", &ipb);
230 static int
231 vi_send_ab1(IPVIWIN *ipvi, int code, const char *str1, u_int32_t len1,
232 const char *str2, u_int32_t len2, u_int32_t val)
234 IP_BUF ipb;
236 ipb.str1 = str1;
237 ipb.len1 = len1;
238 ipb.str2 = str2;
239 ipb.len2 = len2;
240 ipb.val1 = val;
241 ipb.code = code;
242 return vi_send(ipvi->ofd, "ab1", &ipb);