]>
git.cameronkatri.com Git - mandoc.git/blob - mandocd.c
1 /* $Id: mandocd.c,v 1.3 2017/02/06 19:02:37 schwarze Exp $ */
3 * Copyright (c) 2017 Michael Stapelberg <stapelberg@debian.org>
4 * Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
21 #include <sys/socket.h>
46 static void process(struct mparse
*, enum outt
, void *);
47 static int read_fds(int, int *);
48 static void usage(void) __attribute__((noreturn
));
53 read_fds(int clientfd
, int *fds
)
57 unsigned char dummy
[1];
62 /* Union used for alignment. */
64 uint8_t controlbuf
[CMSG_SPACE(NUM_FDS
* sizeof(int))];
68 memset(&msg
, '\0', sizeof(msg
));
69 msg
.msg_control
= u
.controlbuf
;
70 msg
.msg_controllen
= sizeof(u
.controlbuf
);
73 * Read a dummy byte - sendmsg cannot send an empty message,
74 * even if we are only interested in the OOB data.
77 iov
[0].iov_base
= dummy
;
78 iov
[0].iov_len
= sizeof(dummy
);
82 switch (recvmsg(clientfd
, &msg
, 0)) {
92 if ((cmsg
= CMSG_FIRSTHDR(&msg
)) == NULL
) {
93 warnx("CMSG_FIRSTHDR: missing control message");
97 if (cmsg
->cmsg_level
!= SOL_SOCKET
||
98 cmsg
->cmsg_type
!= SCM_RIGHTS
||
99 cmsg
->cmsg_len
!= CMSG_LEN(NUM_FDS
* sizeof(int))) {
100 warnx("CMSG_FIRSTHDR: invalid control message");
104 walk
= (int *)CMSG_DATA(cmsg
);
105 for (cnt
= 0; cnt
< NUM_FDS
; cnt
++)
112 main(int argc
, char *argv
[])
114 struct manoutput options
;
115 struct mparse
*parser
;
128 outtype
= OUTT_ASCII
;
129 while ((opt
= getopt(argc
, argv
, "I:T:")) != -1) {
132 if (strncmp(optarg
, "os=", 3) == 0)
135 warnx("-I %s: Bad argument", optarg
);
140 if (strcmp(optarg
, "ascii") == 0)
141 outtype
= OUTT_ASCII
;
142 else if (strcmp(optarg
, "utf8") == 0)
144 else if (strcmp(optarg
, "html") == 0)
147 warnx("-T %s: Bad argument", optarg
);
164 clientfd
= strtonum(argv
[0], 3, INT_MAX
, &errstr
);
166 errx(1, "file descriptor %s %s", argv
[1], errstr
);
169 parser
= mparse_alloc(MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
,
170 MANDOCLEVEL_BADARG
, NULL
, defos
);
172 memset(&options
, 0, sizeof(options
));
175 formatter
= ascii_alloc(&options
);
178 formatter
= utf8_alloc(&options
);
181 options
.fragment
= 1;
182 formatter
= html_alloc(&options
);
186 state
= 1; /* work to do */
189 if ((old_stdin
= dup(STDIN_FILENO
)) == -1 ||
190 (old_stdout
= dup(STDOUT_FILENO
)) == -1 ||
191 (old_stderr
= dup(STDERR_FILENO
)) == -1) {
193 state
= -1; /* error */
196 while (state
== 1 && (state
= read_fds(clientfd
, fds
)) == 1) {
197 if (dup2(fds
[0], STDIN_FILENO
) == -1 ||
198 dup2(fds
[1], STDOUT_FILENO
) == -1 ||
199 dup2(fds
[2], STDERR_FILENO
) == -1) {
209 process(parser
, outtype
, formatter
);
210 mparse_reset(parser
);
214 /* Close file descriptors by restoring the old ones. */
215 if (dup2(old_stderr
, STDERR_FILENO
) == -1 ||
216 dup2(old_stdout
, STDOUT_FILENO
) == -1 ||
217 dup2(old_stdin
, STDIN_FILENO
) == -1) {
228 ascii_free(formatter
);
231 html_free(formatter
);
236 return state
== -1 ? 1 : 0;
240 process(struct mparse
*parser
, enum outt outtype
, void *formatter
)
242 struct roff_man
*man
;
244 mparse_readfd(parser
, STDIN_FILENO
, "<unixfd>");
245 mparse_result(parser
, &man
, NULL
);
250 if (man
->macroset
== MACROSET_MDOC
) {
255 terminal_mdoc(formatter
, man
);
258 html_mdoc(formatter
, man
);
262 if (man
->macroset
== MACROSET_MAN
) {
267 terminal_man(formatter
, man
);
270 html_man(formatter
, man
);
279 fprintf(stderr
, "usage: mandocd [-I os=name] [-T output] socket_fd\n");