version 1.7.3.0
[socat.git] / xio-stdio.c
blob73f2b7f3a7c8ef5696549fa877dd6eb5560cd383
1 /* source: xio-stdio.c */
2 /* Copyright Gerhard Rieger 2001-2008 */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* this file contains the source for opening addresses stdio type */
7 #include "xiosysincludes.h"
8 #include "xioopen.h"
10 #include "xio-fdnum.h"
11 #include "xio-stdio.h"
14 #if WITH_STDIO
16 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);
17 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);
20 /* we specify all option groups that we can imagine for a FD, becasue the
21 changed parsing mechanism does not allow us to check the type of FD before
22 applying the options */
23 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) };
24 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) };
25 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) };
26 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) };
29 /* process a bidirectional "stdio" or "-" argument with options.
30 generate a dual address. */
31 int xioopen_stdio_bi(xiofile_t *sock) {
32 struct opt *optspr;
33 unsigned int groups1 = addr_stdio.groups;
34 int result;
36 if (xioopen_makedual(sock) < 0) {
37 return -1;
40 sock->dual.stream[0]->tag = XIO_TAG_RDONLY;
41 sock->dual.stream[0]->fd = 0 /*stdin*/;
42 sock->dual.stream[1]->tag = XIO_TAG_WRONLY;
43 sock->dual.stream[1]->fd = 1 /*stdout*/;
44 sock->dual.stream[0]->howtoend =
45 sock->dual.stream[1]->howtoend = END_NONE;
47 #if WITH_TERMIOS
48 if (Isatty(sock->dual.stream[0]->fd)) {
49 if (Tcgetattr(sock->dual.stream[0]->fd,
50 &sock->dual.stream[0]->savetty)
51 < 0) {
52 Warn2("cannot query current terminal settings on fd %d: %s",
53 sock->dual.stream[0]->fd, strerror(errno));
54 } else {
55 sock->dual.stream[0]->ttyvalid = true;
58 if (Isatty(sock->dual.stream[1]->fd)) {
59 if (Tcgetattr(sock->dual.stream[1]->fd,
60 &sock->dual.stream[1]->savetty)
61 < 0) {
62 Warn2("cannot query current terminal settings on fd %d: %s",
63 sock->dual.stream[1]->fd, strerror(errno));
64 } else {
65 sock->dual.stream[1]->ttyvalid = true;
68 #endif /* WITH_TERMIOS */
70 /* options here are one-time and one-direction, no second use */
71 retropt_bool(sock->stream.opts, OPT_IGNOREEOF, &sock->dual.stream[0]->ignoreeof);
73 /* extract opts that should be applied only once */
74 if ((optspr = copyopts(sock->stream.opts, GROUP_PROCESS)) == NULL) {
75 return -1;
77 /* here we copy opts, because most have to be applied twice! */
78 if ((sock->dual.stream[1]->opts = copyopts(sock->stream.opts, GROUP_FD|GROUP_APPL|(groups1&~GROUP_PROCESS))) == NULL) {
79 return -1;
81 sock->dual.stream[0]->opts = sock->stream.opts;
82 sock->stream.opts = NULL;
84 if (applyopts_single(sock->dual.stream[0],
85 sock->dual.stream[0]->opts, PH_INIT)
86 < 0)
87 return -1;
88 if (applyopts_single(sock->dual.stream[1],
89 sock->dual.stream[1]->opts, PH_INIT)
90 < 0)
91 return -1;
92 applyopts(-1, sock->dual.stream[0]->opts, PH_INIT);
93 applyopts(-1, sock->dual.stream[1]->opts, PH_INIT);
94 if ((result = applyopts(-1, optspr, PH_EARLY)) < 0)
95 return result;
96 if ((result = applyopts(-1, optspr, PH_PREOPEN)) < 0)
97 return result;
99 /* apply options to first FD */
100 if ((result =
101 applyopts(sock->dual.stream[0]->fd,
102 sock->dual.stream[0]->opts, PH_ALL))
103 < 0) {
104 return result;
106 if ((result = _xio_openlate(sock->dual.stream[0],
107 sock->dual.stream[0]->opts)) < 0) {
108 return result;
110 #if 0
111 /* ignore this opt */
112 retropt_bool(sock->dual.stream[0]->opts, OPT_COOL_WRITE);
113 #endif
115 /* apply options to second FD */
116 if ((result = applyopts(sock->dual.stream[1]->fd,
117 sock->dual.stream[1]->opts, PH_ALL)) < 0) {
118 return result;
120 if ((result = _xio_openlate(sock->dual.stream[1],
121 sock->dual.stream[1]->opts)) < 0) {
122 return result;
125 #if 0
126 if ((result = _xio_openlate(sock->dual.stream[1], optspr)) < 0) {
127 return result;
129 #endif
131 Notice("reading from and writing to stdio");
132 return 0;
136 /* wrap around unidirectional xioopensingle and xioopen_fd to automatically determine stdin or stdout fd depending on rw.
137 Do not set FD_CLOEXEC flag. */
138 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) {
139 int rw = (xioflags&XIO_ACCMODE);
141 if (argc != 1) {
142 Error2("%s: wrong number of parameters (%d instead of 0)", argv[0], argc-1);
145 if (rw == XIO_RDWR) {
146 return xioopen_stdio_bi(fd);
149 Notice2("using %s for %s",
150 &("stdin\0\0\0stdout"[rw<<3]),
151 ddirection[rw]);
152 return xioopen_fd(opts, rw, &fd->stream, rw, dummy2, dummy3);
155 /* wrap around unidirectional xioopensingle and xioopen_fd to automatically determine stdin or stdout fd depending on rw.
156 Do not set FD_CLOEXEC flag. */
157 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) {
158 int rw = (xioflags&XIO_ACCMODE);
160 if (argc != 1) {
161 Error2("%s: wrong number of parameters (%d instead of 0)", argv[0], argc-1);
163 Notice2("using %s for %s",
164 &("stdin\0\0\0stdout\0\0stderr"[fd<<3]),
165 ddirection[rw]);
166 return xioopen_fd(opts, rw, &xfd->stream, fd, dummy2, dummy3);
168 #endif /* WITH_STDIO */