You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
6.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: xio-stdio.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* this file contains the source for opening addresses stdio type */
  5. #include "xiosysincludes.h"
  6. #include "xioopen.h"
  7. #include "xio-fdnum.h"
  8. #include "xio-stdio.h"
  9. #if WITH_STDIO
  10. static int xioopen_stdio(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *fd, unsigned groups, int dummy1, int dummy2, int dummy3);
  11. static int xioopen_stdfd(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *xfd, unsigned groups, int fd, int dummy2, int dummy3);
  12. /* we specify all option groups that we can imagine for a FD, becasue the
  13. changed parsing mechanism does not allow us to check the type of FD before
  14. applying the options */
  15. const struct addrdesc addr_stdio = { "stdio", 3, xioopen_stdio, GROUP_FD|GROUP_FIFO|GROUP_CHR|GROUP_BLK|GROUP_FILE|GROUP_SOCKET|GROUP_TERMIOS|GROUP_SOCK_UNIX|GROUP_SOCK_IP|GROUP_IPAPP, 0, 0, 0 HELP(NULL) };
  16. const struct addrdesc addr_stdin = { "stdin", 1, xioopen_stdfd, GROUP_FD|GROUP_FIFO|GROUP_CHR|GROUP_BLK|GROUP_FILE|GROUP_SOCKET|GROUP_TERMIOS|GROUP_SOCK_UNIX|GROUP_SOCK_IP|GROUP_IPAPP, 0, 0, 0 HELP(NULL) };
  17. const struct addrdesc addr_stdout = { "stdout", 2, xioopen_stdfd, GROUP_FD|GROUP_FIFO|GROUP_CHR|GROUP_BLK|GROUP_FILE|GROUP_SOCKET|GROUP_TERMIOS|GROUP_SOCK_UNIX|GROUP_SOCK_IP|GROUP_IPAPP, 1, 0, 0 HELP(NULL) };
  18. const struct addrdesc addr_stderr = { "stderr", 2, xioopen_stdfd, GROUP_FD|GROUP_FIFO|GROUP_CHR|GROUP_BLK|GROUP_FILE|GROUP_SOCKET|GROUP_TERMIOS|GROUP_SOCK_UNIX|GROUP_SOCK_IP|GROUP_IPAPP, 2, 0, 0 HELP(NULL) };
  19. /* process a bidirectional "stdio" or "-" argument with options.
  20. generate a dual address. */
  21. int xioopen_stdio_bi(xiofile_t *sock) {
  22. struct opt *optspr;
  23. unsigned int groups1 = addr_stdio.groups;
  24. int result;
  25. if (xioopen_makedual(sock) < 0) {
  26. return -1;
  27. }
  28. sock->dual.stream[0]->tag = XIO_TAG_RDONLY;
  29. sock->dual.stream[0]->fd = 0 /*stdin*/;
  30. sock->dual.stream[1]->tag = XIO_TAG_WRONLY;
  31. sock->dual.stream[1]->fd = 1 /*stdout*/;
  32. sock->dual.stream[0]->howtoend =
  33. sock->dual.stream[1]->howtoend = END_NONE;
  34. #if WITH_TERMIOS
  35. if (Isatty(sock->dual.stream[0]->fd)) {
  36. if (Tcgetattr(sock->dual.stream[0]->fd,
  37. &sock->dual.stream[0]->savetty)
  38. < 0) {
  39. Warn2("cannot query current terminal settings on fd %d: %s",
  40. sock->dual.stream[0]->fd, strerror(errno));
  41. } else {
  42. sock->dual.stream[0]->ttyvalid = true;
  43. }
  44. }
  45. if (Isatty(sock->dual.stream[1]->fd)) {
  46. if (Tcgetattr(sock->dual.stream[1]->fd,
  47. &sock->dual.stream[1]->savetty)
  48. < 0) {
  49. Warn2("cannot query current terminal settings on fd %d: %s",
  50. sock->dual.stream[1]->fd, strerror(errno));
  51. } else {
  52. sock->dual.stream[1]->ttyvalid = true;
  53. }
  54. }
  55. #endif /* WITH_TERMIOS */
  56. /* options here are one-time and one-direction, no second use */
  57. retropt_bool(sock->stream.opts, OPT_IGNOREEOF, &sock->dual.stream[0]->ignoreeof);
  58. /* extract opts that should be applied only once */
  59. if ((optspr = copyopts(sock->stream.opts, GROUP_PROCESS)) == NULL) {
  60. return -1;
  61. }
  62. /* here we copy opts, because most have to be applied twice! */
  63. if ((sock->dual.stream[1]->opts = copyopts(sock->stream.opts, GROUP_FD|GROUP_APPL|(groups1&~GROUP_PROCESS))) == NULL) {
  64. return -1;
  65. }
  66. sock->dual.stream[0]->opts = sock->stream.opts;
  67. sock->stream.opts = NULL;
  68. if (applyopts_single(sock->dual.stream[0],
  69. sock->dual.stream[0]->opts, PH_INIT)
  70. < 0)
  71. return -1;
  72. if (applyopts_single(sock->dual.stream[1],
  73. sock->dual.stream[1]->opts, PH_INIT)
  74. < 0)
  75. return -1;
  76. applyopts(-1, sock->dual.stream[0]->opts, PH_INIT);
  77. applyopts(-1, sock->dual.stream[1]->opts, PH_INIT);
  78. if ((result = applyopts(-1, optspr, PH_EARLY)) < 0)
  79. return result;
  80. if ((result = applyopts(-1, optspr, PH_PREOPEN)) < 0)
  81. return result;
  82. /* apply options to first FD */
  83. if ((result =
  84. applyopts(sock->dual.stream[0]->fd,
  85. sock->dual.stream[0]->opts, PH_ALL))
  86. < 0) {
  87. return result;
  88. }
  89. if ((result = _xio_openlate(sock->dual.stream[0],
  90. sock->dual.stream[0]->opts)) < 0) {
  91. return result;
  92. }
  93. #if 0
  94. /* ignore this opt */
  95. retropt_bool(sock->dual.stream[0]->opts, OPT_COOL_WRITE);
  96. #endif
  97. /* apply options to second FD */
  98. if ((result = applyopts(sock->dual.stream[1]->fd,
  99. sock->dual.stream[1]->opts, PH_ALL)) < 0) {
  100. return result;
  101. }
  102. if ((result = _xio_openlate(sock->dual.stream[1],
  103. sock->dual.stream[1]->opts)) < 0) {
  104. return result;
  105. }
  106. #if 0
  107. if ((result = _xio_openlate(sock->dual.stream[1], optspr)) < 0) {
  108. return result;
  109. }
  110. #endif
  111. Notice("reading from and writing to stdio");
  112. return 0;
  113. }
  114. /* wrap around unidirectional xioopensingle and xioopen_fd to automatically determine stdin or stdout fd depending on rw.
  115. Do not set FD_CLOEXEC flag. */
  116. static int xioopen_stdio(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *fd, unsigned groups, int dummy1, int dummy2, int dummy3) {
  117. int rw = (xioflags&XIO_ACCMODE);
  118. if (argc != 1) {
  119. Error2("%s: wrong number of parameters (%d instead of 0)", argv[0], argc-1);
  120. }
  121. if (rw == XIO_RDWR) {
  122. return xioopen_stdio_bi(fd);
  123. }
  124. Notice2("using %s for %s",
  125. &("stdin\0\0\0stdout"[rw<<3]),
  126. ddirection[rw]);
  127. return xioopen_fd(opts, rw, &fd->stream, rw, dummy2, dummy3);
  128. }
  129. /* wrap around unidirectional xioopensingle and xioopen_fd to automatically determine stdin or stdout fd depending on rw.
  130. Do not set FD_CLOEXEC flag. */
  131. static int xioopen_stdfd(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *xfd, unsigned groups, int fd, int dummy2, int dummy3) {
  132. int rw = (xioflags&XIO_ACCMODE);
  133. if (argc != 1) {
  134. Error2("%s: wrong number of parameters (%d instead of 0)", argv[0], argc-1);
  135. }
  136. Notice2("using %s for %s",
  137. &("stdin\0\0\0stdout\0\0stderr"[fd<<3]),
  138. ddirection[rw]);
  139. return xioopen_fd(opts, rw, &xfd->stream, fd, dummy2, dummy3);
  140. }
  141. #endif /* WITH_STDIO */