Sync usage with man page.
[netbsd-mini2440.git] / dist / nvi / ipc / ipc_method.c
blob1abb22db19591e68650ae9c48be22c2adb583994
1 /* $NetBSD$ */
3 /*-
4 * Copyright (c) 1996
5 * Rob Zimmermann. All rights reserved.
6 * Copyright (c) 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/stat.h>
18 #include <bitstring.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include <sys/uio.h>
28 #include "../common/common.h"
29 #include "ip.h"
31 static int vi_send_ __P((IPVIWIN *, int));
32 static int vi_send_1 __P((IPVIWIN *, int, u_int32_t ));
33 static int vi_send_12 __P((IPVIWIN *ipvi, int code, u_int32_t val1, u_int32_t val2));
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 static int vi_send_a __P((IPVIWIN *ipvi, int code, const char *str, u_int32_t len));
41 #include "ipc_gen.c"
43 static int vi_set_ops __P((IPVIWIN *, IPSIOPS *));
44 static int vi_win_close __P((IPVIWIN *));
46 static int vi_close __P((IPVI *));
47 static int vi_new_window __P((IPVI *, IPVIWIN **, int));
49 /*
50 * vi_create
52 * PUBLIC: int vi_create __P((IPVI **, u_int32_t));
54 int
55 vi_create(IPVI **ipvip, 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(IPVIWIN *ipvi, IPSIOPS *ops)
161 ipvi->si_ops = ops;
162 return 0;
165 static int vi_close(IPVI *ipvi)
167 memset(ipvi, 6, sizeof(IPVI));
168 free(ipvi);
169 return 0;
172 static int vi_win_close(IPVIWIN *ipviwin)
174 memset(ipviwin, 6, sizeof(IPVIWIN));
175 free(ipviwin);
176 return 0;
180 static int
181 vi_send_(IPVIWIN *ipvi, int code)
183 IP_BUF ipb;
184 ipb.code = code;
185 return vi_send(ipvi->ofd, NULL, &ipb);
188 static int
189 vi_send_1(IPVIWIN *ipvi, int code, u_int32_t val)
191 IP_BUF ipb;
192 ipb.code = code;
193 ipb.val1 = val;
194 return vi_send(ipvi->ofd, "1", &ipb);
197 static int
198 vi_send_12(IPVIWIN *ipvi, int code, u_int32_t val1, u_int32_t val2)
200 IP_BUF ipb;
202 ipb.val1 = val1;
203 ipb.val2 = val2;
204 ipb.code = code;
205 return vi_send(ipvi->ofd, "12", &ipb);
208 static int
209 vi_send_a(IPVIWIN *ipvi, int code, const char *str, u_int32_t len)
211 IP_BUF ipb;
213 ipb.str1 = str;
214 ipb.len1 = len;
215 ipb.code = code;
216 return vi_send(ipvi->ofd, "a", &ipb);
219 static int
220 vi_send_a1(IPVIWIN *ipvi, int code, const char *str, u_int32_t len,
221 u_int32_t val)
223 IP_BUF ipb;
225 ipb.str1 = str;
226 ipb.len1 = len;
227 ipb.val1 = val;
228 ipb.code = code;
229 return vi_send(ipvi->ofd, "a1", &ipb);
232 static int
233 vi_send_ab1(IPVIWIN *ipvi, int code, const char *str1, u_int32_t len1,
234 const char *str2, u_int32_t len2, u_int32_t val)
236 IP_BUF ipb;
238 ipb.str1 = str1;
239 ipb.len1 = len1;
240 ipb.str2 = str2;
241 ipb.len2 = len2;
242 ipb.val1 = val;
243 ipb.code = code;
244 return vi_send(ipvi->ofd, "ab1", &ipb);