/dist/nvi/ipc/ipc_method.c

http://www.minix3.org/ · C · 246 lines · 181 code · 48 blank · 17 comment · 5 complexity · 168639cc512b8377cfb720f5b7f48d88 MD5 · raw file

  1. /* $NetBSD: ipc_method.c,v 1.1.1.2 2008/05/18 14:31:25 aymeric 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. #include "config.h"
  11. #include <sys/types.h>
  12. #include <sys/queue.h>
  13. #include <sys/stat.h>
  14. #include <bitstring.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/uio.h>
  22. #include "../common/common.h"
  23. #include "ip.h"
  24. static int vi_send_ __P((IPVIWIN *, int));
  25. static int vi_send_1 __P((IPVIWIN *, int, u_int32_t ));
  26. static int vi_send_12 __P((IPVIWIN *ipvi, int code, u_int32_t val1, u_int32_t val2));
  27. static int vi_send_ab1 __P((IPVIWIN *ipvi, int code,
  28. const char *str1, u_int32_t len1,
  29. const char *str2, u_int32_t len2, u_int32_t val));
  30. static int vi_send_a1 __P((IPVIWIN *ipvi, int code, const char *str, u_int32_t len,
  31. u_int32_t val));
  32. static int vi_send_a __P((IPVIWIN *ipvi, int code, const char *str, u_int32_t len));
  33. #include "ipc_gen.c"
  34. static int vi_set_ops __P((IPVIWIN *, IPSIOPS *));
  35. static int vi_win_close __P((IPVIWIN *));
  36. static int vi_close __P((IPVI *));
  37. static int vi_new_window __P((IPVI *, IPVIWIN **, int));
  38. /*
  39. * vi_create
  40. *
  41. * PUBLIC: int vi_create __P((IPVI **, u_int32_t));
  42. */
  43. int
  44. vi_create(IPVI **ipvip, u_int32_t flags)
  45. {
  46. IPVI *ipvi;
  47. MALLOC_GOTO(NULL, ipvi, IPVI*, sizeof(IPVI));
  48. memset(ipvi, 0, sizeof(IPVI));
  49. ipvi->flags = flags;
  50. ipvi->run = vi_run;
  51. ipvi->new_window = vi_new_window;
  52. ipvi->close = vi_close;
  53. *ipvip = ipvi;
  54. return 0;
  55. alloc_err:
  56. return 1;
  57. }
  58. static int
  59. vi_new_window (IPVI *ipvi, IPVIWIN **ipviwinp, int fd)
  60. {
  61. IPVIWIN *ipviwin;
  62. MALLOC_GOTO(NULL, ipviwin, IPVIWIN*, sizeof(IPVIWIN));
  63. memset(ipviwin, 0, sizeof(IPVIWIN));
  64. if (0) {
  65. ipviwin->ifd = ipvi->ifd;
  66. ipviwin->ofd = ipvi->ofd;
  67. } else {
  68. int sockets[2];
  69. struct msghdr mh;
  70. IPCMSGHDR ch;
  71. char dummy;
  72. struct iovec iov;
  73. socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets);
  74. mh.msg_namelen = 0;
  75. mh.msg_iovlen = 1;
  76. mh.msg_iov = &iov;
  77. mh.msg_controllen = sizeof(ch);
  78. mh.msg_control = (void *)&ch;
  79. iov.iov_len = 1;
  80. iov.iov_base = &dummy;
  81. ch.header.cmsg_level = SOL_SOCKET;
  82. ch.header.cmsg_type = SCM_RIGHTS;
  83. ch.header.cmsg_len = sizeof(ch);
  84. *(int *)CMSG_DATA(&ch.header) = sockets[1];
  85. sendmsg(ipvi->ofd, &mh, 0);
  86. dummy = (fd == -1) ? ' ' : 'F';
  87. *(int *)CMSG_DATA(&ch.header) = sockets[1];
  88. sendmsg(sockets[0], &mh, 0);
  89. close(sockets[1]);
  90. if (fd != -1) {
  91. *(int *)CMSG_DATA(&ch.header) = fd;
  92. sendmsg(sockets[0], &mh, 0);
  93. close(fd);
  94. }
  95. ipviwin->ifd = sockets[0];
  96. ipviwin->ofd = sockets[0];
  97. }
  98. #define IPVISET(func) \
  99. ipviwin->func = vi_##func;
  100. IPVISET(c_bol);
  101. IPVISET(c_bottom);
  102. IPVISET(c_del);
  103. IPVISET(c_eol);
  104. IPVISET(c_insert);
  105. IPVISET(c_left);
  106. IPVISET(c_right);
  107. IPVISET(c_top);
  108. IPVISET(c_settop);
  109. IPVISET(resize);
  110. IPVISET(string);
  111. IPVISET(quit);
  112. IPVISET(wq);
  113. IPVISET(input);
  114. /*
  115. IPVISET(close);
  116. */
  117. ipviwin->close = vi_win_close;
  118. IPVISET(set_ops);
  119. *ipviwinp = ipviwin;
  120. return 0;
  121. alloc_err:
  122. return 1;
  123. }
  124. static int
  125. vi_set_ops(IPVIWIN *ipvi, IPSIOPS *ops)
  126. {
  127. ipvi->si_ops = ops;
  128. return 0;
  129. }
  130. static int vi_close(IPVI *ipvi)
  131. {
  132. memset(ipvi, 6, sizeof(IPVI));
  133. free(ipvi);
  134. return 0;
  135. }
  136. static int vi_win_close(IPVIWIN *ipviwin)
  137. {
  138. memset(ipviwin, 6, sizeof(IPVIWIN));
  139. free(ipviwin);
  140. return 0;
  141. }
  142. static int
  143. vi_send_(IPVIWIN *ipvi, int code)
  144. {
  145. IP_BUF ipb;
  146. ipb.code = code;
  147. return vi_send(ipvi->ofd, NULL, &ipb);
  148. }
  149. static int
  150. vi_send_1(IPVIWIN *ipvi, int code, u_int32_t val)
  151. {
  152. IP_BUF ipb;
  153. ipb.code = code;
  154. ipb.val1 = val;
  155. return vi_send(ipvi->ofd, "1", &ipb);
  156. }
  157. static int
  158. vi_send_12(IPVIWIN *ipvi, int code, u_int32_t val1, u_int32_t val2)
  159. {
  160. IP_BUF ipb;
  161. ipb.val1 = val1;
  162. ipb.val2 = val2;
  163. ipb.code = code;
  164. return vi_send(ipvi->ofd, "12", &ipb);
  165. }
  166. static int
  167. vi_send_a(IPVIWIN *ipvi, int code, const char *str, u_int32_t len)
  168. {
  169. IP_BUF ipb;
  170. ipb.str1 = str;
  171. ipb.len1 = len;
  172. ipb.code = code;
  173. return vi_send(ipvi->ofd, "a", &ipb);
  174. }
  175. static int
  176. vi_send_a1(IPVIWIN *ipvi, int code, const char *str, u_int32_t len,
  177. u_int32_t val)
  178. {
  179. IP_BUF ipb;
  180. ipb.str1 = str;
  181. ipb.len1 = len;
  182. ipb.val1 = val;
  183. ipb.code = code;
  184. return vi_send(ipvi->ofd, "a1", &ipb);
  185. }
  186. static int
  187. vi_send_ab1(IPVIWIN *ipvi, int code, const char *str1, u_int32_t len1,
  188. const char *str2, u_int32_t len2, u_int32_t val)
  189. {
  190. IP_BUF ipb;
  191. ipb.str1 = str1;
  192. ipb.len1 = len1;
  193. ipb.str2 = str2;
  194. ipb.len2 = len2;
  195. ipb.val1 = val;
  196. ipb.code = code;
  197. return vi_send(ipvi->ofd, "ab1", &ipb);
  198. }